builtin-attrs.def (DEF_ATTR_FOR_INT): Add for 5 and 6.
[gcc.git] / gcc / tree-optimize.c
1 /* Top-level control of tree optimizations.
2 Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "expr.h"
33 #include "diagnostic.h"
34 #include "basic-block.h"
35 #include "flags.h"
36 #include "tree-flow.h"
37 #include "tree-dump.h"
38 #include "timevar.h"
39 #include "function.h"
40 #include "langhooks.h"
41 #include "toplev.h"
42 #include "flags.h"
43 #include "cgraph.h"
44 #include "tree-inline.h"
45 #include "tree-mudflap.h"
46 #include "tree-pass.h"
47 #include "ggc.h"
48 #include "cgraph.h"
49 #include "graph.h"
50 #include "cfgloop.h"
51 #include "except.h"
52
53 /* Global variables used to communicate with passes. */
54 int dump_flags;
55 bool in_gimple_form;
56
57 /* The root of the compilation pass tree, once constructed. */
58 static struct tree_opt_pass *all_passes, *all_ipa_passes, * all_lowering_passes;
59
60 /* Gate: execute, or not, all of the non-trivial optimizations. */
61
62 static bool
63 gate_all_optimizations (void)
64 {
65 return (optimize >= 1
66 /* Don't bother doing anything if the program has errors. */
67 && !(errorcount || sorrycount));
68 }
69
70 static struct tree_opt_pass pass_all_optimizations =
71 {
72 NULL, /* name */
73 gate_all_optimizations, /* gate */
74 NULL, /* execute */
75 NULL, /* sub */
76 NULL, /* next */
77 0, /* static_pass_number */
78 0, /* tv_id */
79 0, /* properties_required */
80 0, /* properties_provided */
81 0, /* properties_destroyed */
82 0, /* todo_flags_start */
83 0, /* todo_flags_finish */
84 0 /* letter */
85 };
86
87 /* Pass: cleanup the CFG just before expanding trees to RTL.
88 This is just a round of label cleanups and case node grouping
89 because after the tree optimizers have run such cleanups may
90 be necessary. */
91
92 static void
93 execute_cleanup_cfg_post_optimizing (void)
94 {
95 cleanup_tree_cfg ();
96 cleanup_dead_labels ();
97 group_case_labels ();
98 }
99
100 static struct tree_opt_pass pass_cleanup_cfg_post_optimizing =
101 {
102 "final_cleanup", /* name */
103 NULL, /* gate */
104 execute_cleanup_cfg_post_optimizing, /* execute */
105 NULL, /* sub */
106 NULL, /* next */
107 0, /* static_pass_number */
108 0, /* tv_id */
109 PROP_cfg, /* properties_required */
110 0, /* properties_provided */
111 0, /* properties_destroyed */
112 0, /* todo_flags_start */
113 TODO_dump_func, /* todo_flags_finish */
114 0 /* letter */
115 };
116
117 /* Pass: do the actions required to finish with tree-ssa optimization
118 passes. */
119
120 static void
121 execute_free_datastructures (void)
122 {
123 tree *chain;
124
125 /* ??? This isn't the right place for this. Worse, it got computed
126 more or less at random in various passes. */
127 free_dominance_info (CDI_DOMINATORS);
128
129 /* Emit gotos for implicit jumps. */
130 disband_implicit_edges ();
131
132 /* Remove the ssa structures. Do it here since this includes statement
133 annotations that need to be intact during disband_implicit_edges. */
134 delete_tree_ssa ();
135
136 /* Re-chain the statements from the blocks. */
137 chain = &DECL_SAVED_TREE (current_function_decl);
138 *chain = alloc_stmt_list ();
139
140 /* And get rid of annotations we no longer need. */
141 delete_tree_cfg_annotations ();
142 }
143
144 static struct tree_opt_pass pass_free_datastructures =
145 {
146 NULL, /* name */
147 NULL, /* gate */
148 execute_free_datastructures, /* execute */
149 NULL, /* sub */
150 NULL, /* next */
151 0, /* static_pass_number */
152 0, /* tv_id */
153 PROP_cfg, /* properties_required */
154 0, /* properties_provided */
155 0, /* properties_destroyed */
156 0, /* todo_flags_start */
157 0, /* todo_flags_finish */
158 0 /* letter */
159 };
160
161 /* Pass: fixup_cfg - IPA passes or compilation of earlier functions might've
162 changed some properties - such as marked functions nothrow. Remove now
163 redundant edges and basic blocks. */
164
165 static void
166 execute_fixup_cfg (void)
167 {
168 basic_block bb;
169 block_stmt_iterator bsi;
170
171 if (cfun->eh)
172 FOR_EACH_BB (bb)
173 {
174 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
175 {
176 tree stmt = bsi_stmt (bsi);
177 tree call = get_call_expr_in (stmt);
178
179 if (call && call_expr_flags (call) & (ECF_CONST | ECF_PURE))
180 TREE_SIDE_EFFECTS (call) = 0;
181 if (!tree_could_throw_p (stmt) && lookup_stmt_eh_region (stmt))
182 remove_stmt_from_eh_region (stmt);
183 }
184 tree_purge_dead_eh_edges (bb);
185 }
186
187 cleanup_tree_cfg ();
188 }
189
190 static struct tree_opt_pass pass_fixup_cfg =
191 {
192 NULL, /* name */
193 NULL, /* gate */
194 execute_fixup_cfg, /* execute */
195 NULL, /* sub */
196 NULL, /* next */
197 0, /* static_pass_number */
198 0, /* tv_id */
199 PROP_cfg, /* properties_required */
200 0, /* properties_provided */
201 0, /* properties_destroyed */
202 0, /* todo_flags_start */
203 0, /* todo_flags_finish */
204 0 /* letter */
205 };
206
207 /* Do the actions required to initialize internal data structures used
208 in tree-ssa optimization passes. */
209
210 static void
211 execute_init_datastructures (void)
212 {
213 /* Allocate hash tables, arrays and other structures. */
214 init_tree_ssa ();
215 }
216
217 static struct tree_opt_pass pass_init_datastructures =
218 {
219 NULL, /* name */
220 NULL, /* gate */
221 execute_init_datastructures, /* execute */
222 NULL, /* sub */
223 NULL, /* next */
224 0, /* static_pass_number */
225 0, /* tv_id */
226 PROP_cfg, /* properties_required */
227 0, /* properties_provided */
228 0, /* properties_destroyed */
229 0, /* todo_flags_start */
230 0, /* todo_flags_finish */
231 0 /* letter */
232 };
233
234 /* Iterate over the pass tree allocating dump file numbers. We want
235 to do this depth first, and independent of whether the pass is
236 enabled or not. */
237
238 static void
239 register_one_dump_file (struct tree_opt_pass *pass, bool ipa, int n)
240 {
241 char *dot_name, *flag_name, *glob_name;
242 char num[10];
243
244 /* See below in next_pass_1. */
245 num[0] = '\0';
246 if (pass->static_pass_number != -1)
247 sprintf (num, "%d", ((int) pass->static_pass_number < 0
248 ? 1 : pass->static_pass_number));
249
250 dot_name = concat (".", pass->name, num, NULL);
251 if (ipa)
252 {
253 flag_name = concat ("ipa-", pass->name, num, NULL);
254 glob_name = concat ("ipa-", pass->name, NULL);
255 /* First IPA dump is cgraph that is dumped via separate channels. */
256 pass->static_pass_number = dump_register (dot_name, flag_name, glob_name,
257 TDF_IPA, n + 1, 0);
258 }
259 else if (pass->properties_provided & PROP_trees)
260 {
261 flag_name = concat ("tree-", pass->name, num, NULL);
262 glob_name = concat ("tree-", pass->name, NULL);
263 pass->static_pass_number = dump_register (dot_name, flag_name, glob_name,
264 TDF_TREE, n + TDI_tree_all, 0);
265 }
266 else
267 {
268 flag_name = concat ("rtl-", pass->name, num, NULL);
269 glob_name = concat ("rtl-", pass->name, NULL);
270 pass->static_pass_number = dump_register (dot_name, flag_name, glob_name,
271 TDF_RTL, n, pass->letter);
272 }
273 }
274
275 static int
276 register_dump_files (struct tree_opt_pass *pass, bool ipa, int properties)
277 {
278 static int n = 0;
279 do
280 {
281 int new_properties;
282 int pass_number;
283
284 pass->properties_required = properties;
285 new_properties =
286 (properties | pass->properties_provided) & ~pass->properties_destroyed;
287
288 /* Reset the counter when we reach RTL-based passes. */
289 if ((pass->properties_provided ^ pass->properties_required) & PROP_rtl)
290 n = 0;
291
292 pass_number = n;
293 if (pass->name)
294 n++;
295
296 if (pass->sub)
297 new_properties = register_dump_files (pass->sub, ipa, new_properties);
298
299 /* If we have a gate, combine the properties that we could have with
300 and without the pass being examined. */
301 if (pass->gate)
302 properties &= new_properties;
303 else
304 properties = new_properties;
305
306 pass->properties_provided = properties;
307 if (pass->name)
308 register_one_dump_file (pass, ipa, pass_number);
309
310 pass = pass->next;
311 }
312 while (pass);
313
314 return properties;
315 }
316
317 /* Add a pass to the pass list. Duplicate the pass if it's already
318 in the list. */
319
320 static struct tree_opt_pass **
321 next_pass_1 (struct tree_opt_pass **list, struct tree_opt_pass *pass)
322 {
323
324 /* A nonzero static_pass_number indicates that the
325 pass is already in the list. */
326 if (pass->static_pass_number)
327 {
328 struct tree_opt_pass *new;
329
330 new = xmalloc (sizeof (*new));
331 memcpy (new, pass, sizeof (*new));
332
333 /* Indicate to register_dump_files that this pass has duplicates,
334 and so it should rename the dump file. The first instance will
335 be -1, and be number of duplicates = -static_pass_number - 1.
336 Subsequent instances will be > 0 and just the duplicate number. */
337 if (pass->name)
338 {
339 pass->static_pass_number -= 1;
340 new->static_pass_number = -pass->static_pass_number;
341 }
342
343 *list = new;
344 }
345 else
346 {
347 pass->static_pass_number = -1;
348 *list = pass;
349 }
350
351 return &(*list)->next;
352
353 }
354
355 /* Construct the pass tree. */
356
357 void
358 init_tree_optimization_passes (void)
359 {
360 struct tree_opt_pass **p;
361
362 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &PASS))
363 /* Intraprocedural optimization passes. */
364 p = &all_ipa_passes;
365 NEXT_PASS (pass_ipa_inline);
366 *p = NULL;
367
368 /* All passes needed to lower the function into shape optimizers can operate
369 on. These passes are performed before interprocedural passes, unlike rest
370 of local passes (all_passes). */
371 p = &all_lowering_passes;
372 NEXT_PASS (pass_remove_useless_stmts);
373 NEXT_PASS (pass_mudflap_1);
374 NEXT_PASS (pass_lower_cf);
375 NEXT_PASS (pass_lower_eh);
376 NEXT_PASS (pass_build_cfg);
377 NEXT_PASS (pass_lower_complex_O0);
378 NEXT_PASS (pass_lower_vector);
379 NEXT_PASS (pass_warn_function_return);
380 NEXT_PASS (pass_tree_profile);
381 *p = NULL;
382
383 p = &all_passes;
384 NEXT_PASS (pass_fixup_cfg);
385 NEXT_PASS (pass_init_datastructures);
386 NEXT_PASS (pass_all_optimizations);
387 NEXT_PASS (pass_warn_function_noreturn);
388 NEXT_PASS (pass_mudflap_2);
389 NEXT_PASS (pass_free_datastructures);
390 NEXT_PASS (pass_expand);
391 NEXT_PASS (pass_rest_of_compilation);
392 *p = NULL;
393
394 p = &pass_all_optimizations.sub;
395 NEXT_PASS (pass_referenced_vars);
396 NEXT_PASS (pass_create_structure_vars);
397 NEXT_PASS (pass_build_ssa);
398 NEXT_PASS (pass_build_pta);
399 NEXT_PASS (pass_may_alias);
400 NEXT_PASS (pass_return_slot);
401 NEXT_PASS (pass_del_pta);
402 NEXT_PASS (pass_rename_ssa_copies);
403 NEXT_PASS (pass_early_warn_uninitialized);
404
405 /* Initial scalar cleanups. */
406 NEXT_PASS (pass_ccp);
407 NEXT_PASS (pass_fre);
408 NEXT_PASS (pass_dce);
409 NEXT_PASS (pass_forwprop);
410 NEXT_PASS (pass_copy_prop);
411 NEXT_PASS (pass_vrp);
412 NEXT_PASS (pass_dce);
413 NEXT_PASS (pass_merge_phi);
414 NEXT_PASS (pass_dominator);
415
416 NEXT_PASS (pass_phiopt);
417 NEXT_PASS (pass_build_pta);
418 NEXT_PASS (pass_may_alias);
419 NEXT_PASS (pass_del_pta);
420 NEXT_PASS (pass_tail_recursion);
421 NEXT_PASS (pass_profile);
422 NEXT_PASS (pass_ch);
423 NEXT_PASS (pass_stdarg);
424 NEXT_PASS (pass_lower_complex);
425 NEXT_PASS (pass_sra);
426 /* FIXME: SRA may generate arbitrary gimple code, exposing new
427 aliased and call-clobbered variables. As mentioned below,
428 pass_may_alias should be a TODO item. */
429 NEXT_PASS (pass_may_alias);
430 NEXT_PASS (pass_rename_ssa_copies);
431 NEXT_PASS (pass_dominator);
432 NEXT_PASS (pass_copy_prop);
433 NEXT_PASS (pass_dce);
434 NEXT_PASS (pass_dse);
435 NEXT_PASS (pass_may_alias);
436 NEXT_PASS (pass_forwprop);
437 NEXT_PASS (pass_phiopt);
438 NEXT_PASS (pass_object_sizes);
439 NEXT_PASS (pass_store_ccp);
440 NEXT_PASS (pass_store_copy_prop);
441 NEXT_PASS (pass_fold_builtins);
442 /* FIXME: May alias should a TODO but for 4.0.0,
443 we add may_alias right after fold builtins
444 which can create arbitrary GIMPLE. */
445 NEXT_PASS (pass_may_alias);
446 NEXT_PASS (pass_cse_reciprocals);
447 NEXT_PASS (pass_split_crit_edges);
448 NEXT_PASS (pass_reassoc);
449 NEXT_PASS (pass_pre);
450 NEXT_PASS (pass_sink_code);
451 NEXT_PASS (pass_loop);
452 NEXT_PASS (pass_dominator);
453 NEXT_PASS (pass_copy_prop);
454 NEXT_PASS (pass_cd_dce);
455 /* FIXME: If DCE is not run before checking for uninitialized uses,
456 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
457 However, this also causes us to misdiagnose cases that should be
458 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
459
460 To fix the false positives in uninit-5.c, we would have to
461 account for the predicates protecting the set and the use of each
462 variable. Using a representation like Gated Single Assignment
463 may help. */
464 NEXT_PASS (pass_late_warn_uninitialized);
465 NEXT_PASS (pass_dse);
466 NEXT_PASS (pass_forwprop);
467 NEXT_PASS (pass_phiopt);
468 NEXT_PASS (pass_tail_calls);
469 NEXT_PASS (pass_rename_ssa_copies);
470 NEXT_PASS (pass_uncprop);
471 NEXT_PASS (pass_del_ssa);
472 NEXT_PASS (pass_nrv);
473 NEXT_PASS (pass_remove_useless_vars);
474 NEXT_PASS (pass_mark_used_blocks);
475 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
476 *p = NULL;
477
478 p = &pass_loop.sub;
479 NEXT_PASS (pass_loop_init);
480 NEXT_PASS (pass_copy_prop);
481 NEXT_PASS (pass_lim);
482 NEXT_PASS (pass_unswitch);
483 NEXT_PASS (pass_scev_cprop);
484 NEXT_PASS (pass_record_bounds);
485 NEXT_PASS (pass_linear_transform);
486 NEXT_PASS (pass_iv_canon);
487 NEXT_PASS (pass_if_conversion);
488 NEXT_PASS (pass_vectorize);
489 /* NEXT_PASS (pass_may_alias) cannot be done again because the
490 vectorizer creates alias relations that are not supported by
491 pass_may_alias. */
492 NEXT_PASS (pass_lower_vector_ssa);
493 NEXT_PASS (pass_complete_unroll);
494 NEXT_PASS (pass_iv_optimize);
495 NEXT_PASS (pass_loop_done);
496 *p = NULL;
497
498 #undef NEXT_PASS
499
500 register_dump_files (all_lowering_passes, false, PROP_gimple_any);
501 register_dump_files (all_passes, false, PROP_gimple_any
502 | PROP_gimple_lcf
503 | PROP_gimple_leh
504 | PROP_cfg);
505 register_dump_files (all_ipa_passes, true, PROP_gimple_any
506 | PROP_gimple_lcf
507 | PROP_gimple_leh
508 | PROP_cfg);
509 }
510
511 static unsigned int last_verified;
512
513 static void
514 execute_todo (struct tree_opt_pass *pass, unsigned int flags, bool use_required)
515 {
516 int properties
517 = use_required ? pass->properties_required : pass->properties_provided;
518
519 #if defined ENABLE_CHECKING
520 if (need_ssa_update_p ())
521 gcc_assert (flags & TODO_update_ssa_any);
522 #endif
523
524 if (flags & TODO_update_ssa_any)
525 {
526 unsigned update_flags = flags & TODO_update_ssa_any;
527 update_ssa (update_flags);
528 }
529
530 if (flags & TODO_cleanup_cfg)
531 {
532 if (current_loops)
533 cleanup_tree_cfg_loop ();
534 else
535 cleanup_tree_cfg ();
536 }
537
538 if ((flags & TODO_dump_func)
539 && dump_file && current_function_decl)
540 {
541 if (properties & PROP_trees)
542 dump_function_to_file (current_function_decl,
543 dump_file, dump_flags);
544 else if (properties & PROP_cfg)
545 print_rtl_with_bb (dump_file, get_insns ());
546 else
547 print_rtl (dump_file, get_insns ());
548
549 /* Flush the file. If verification fails, we won't be able to
550 close the file before dieing. */
551 fflush (dump_file);
552 }
553 if ((flags & TODO_dump_cgraph)
554 && dump_file && !current_function_decl)
555 {
556 dump_cgraph (dump_file);
557 /* Flush the file. If verification fails, we won't be able to
558 close the file before aborting. */
559 fflush (dump_file);
560 }
561
562 if (flags & TODO_ggc_collect)
563 {
564 ggc_collect ();
565 }
566
567 #if defined ENABLE_CHECKING
568 if ((pass->properties_required & PROP_ssa)
569 && !(pass->properties_destroyed & PROP_ssa))
570 verify_ssa (true);
571 if (flags & TODO_verify_flow)
572 verify_flow_info ();
573 if (flags & TODO_verify_stmts)
574 verify_stmts ();
575 if (flags & TODO_verify_loops)
576 verify_loop_closed_ssa ();
577 #endif
578 }
579
580 static bool
581 execute_one_pass (struct tree_opt_pass *pass)
582 {
583 unsigned int todo;
584
585 /* See if we're supposed to run this pass. */
586 if (pass->gate && !pass->gate ())
587 return false;
588
589 /* Note that the folders should only create gimple expressions.
590 This is a hack until the new folder is ready. */
591 in_gimple_form = (pass->properties_provided & PROP_trees) != 0;
592
593 /* Run pre-pass verification. */
594 todo = pass->todo_flags_start & ~last_verified;
595 if (todo)
596 execute_todo (pass, todo, true);
597
598 /* If a dump file name is present, open it if enabled. */
599 if (pass->static_pass_number != -1)
600 {
601 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
602 dump_file_name = get_dump_file_name (pass->static_pass_number);
603 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
604 if (dump_file && current_function_decl)
605 {
606 const char *dname, *aname;
607 dname = lang_hooks.decl_printable_name (current_function_decl, 2);
608 aname = (IDENTIFIER_POINTER
609 (DECL_ASSEMBLER_NAME (current_function_decl)));
610 fprintf (dump_file, "\n;; Function %s (%s)%s\n\n", dname, aname,
611 cfun->function_frequency == FUNCTION_FREQUENCY_HOT
612 ? " (hot)"
613 : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
614 ? " (unlikely executed)"
615 : "");
616 }
617
618 if (initializing_dump
619 && graph_dump_format != no_graph
620 && (pass->properties_provided & (PROP_cfg | PROP_rtl))
621 == (PROP_cfg | PROP_rtl))
622 clean_graph_dump_file (dump_file_name);
623 }
624
625 /* If a timevar is present, start it. */
626 if (pass->tv_id)
627 timevar_push (pass->tv_id);
628
629 /* Do it! */
630 if (pass->execute)
631 pass->execute ();
632
633 /* Stop timevar. */
634 if (pass->tv_id)
635 timevar_pop (pass->tv_id);
636
637 if (dump_file
638 && (pass->properties_provided & (PROP_cfg | PROP_rtl))
639 == (PROP_cfg | PROP_rtl))
640 print_rtl_with_bb (dump_file, get_insns ());
641
642 /* Run post-pass cleanup and verification. */
643 todo = pass->todo_flags_finish;
644 last_verified = todo & TODO_verify_all;
645 if (todo)
646 execute_todo (pass, todo, false);
647
648 /* Flush and close dump file. */
649 if (dump_file_name)
650 {
651 free ((char *) dump_file_name);
652 dump_file_name = NULL;
653 }
654 if (dump_file)
655 {
656 dump_end (pass->static_pass_number, dump_file);
657 dump_file = NULL;
658 }
659
660 return true;
661 }
662
663 static void
664 execute_pass_list (struct tree_opt_pass *pass)
665 {
666 do
667 {
668 if (execute_one_pass (pass) && pass->sub)
669 execute_pass_list (pass->sub);
670 pass = pass->next;
671 }
672 while (pass);
673 }
674
675 /* Same as execute_pass_list but assume that subpasses of IPA passes
676 are local passes. */
677 static void
678 execute_ipa_pass_list (struct tree_opt_pass *pass)
679 {
680 do
681 {
682 if (execute_one_pass (pass) && pass->sub)
683 {
684 struct cgraph_node *node;
685 for (node = cgraph_nodes; node; node = node->next)
686 if (node->analyzed)
687 {
688 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
689 current_function_decl = node->decl;
690 execute_pass_list (pass);
691 free_dominance_info (CDI_DOMINATORS);
692 free_dominance_info (CDI_POST_DOMINATORS);
693 current_function_decl = NULL;
694 pop_cfun ();
695 ggc_collect ();
696 }
697 }
698 pass = pass->next;
699 }
700 while (pass);
701 }
702
703 void
704 tree_lowering_passes (tree fn)
705 {
706 tree saved_current_function_decl = current_function_decl;
707
708 current_function_decl = fn;
709 push_cfun (DECL_STRUCT_FUNCTION (fn));
710 tree_register_cfg_hooks ();
711 bitmap_obstack_initialize (NULL);
712 execute_pass_list (all_lowering_passes);
713 free_dominance_info (CDI_POST_DOMINATORS);
714 compact_blocks ();
715 current_function_decl = saved_current_function_decl;
716 bitmap_obstack_release (NULL);
717 pop_cfun ();
718 }
719
720 /* Execute all IPA passes. */
721 void
722 ipa_passes (void)
723 {
724 cfun = NULL;
725 tree_register_cfg_hooks ();
726 bitmap_obstack_initialize (NULL);
727 execute_ipa_pass_list (all_ipa_passes);
728 bitmap_obstack_release (NULL);
729 }
730 \f
731
732 /* Update recursively all inlined_to pointers of functions
733 inlined into NODE to INLINED_TO. */
734 static void
735 update_inlined_to_pointers (struct cgraph_node *node,
736 struct cgraph_node *inlined_to)
737 {
738 struct cgraph_edge *e;
739 for (e = node->callees; e; e = e->next_callee)
740 {
741 if (e->callee->global.inlined_to)
742 {
743 e->callee->global.inlined_to = inlined_to;
744 update_inlined_to_pointers (e->callee, inlined_to);
745 }
746 }
747 }
748
749 \f
750 /* For functions-as-trees languages, this performs all optimization and
751 compilation for FNDECL. */
752
753 void
754 tree_rest_of_compilation (tree fndecl)
755 {
756 location_t saved_loc;
757 struct cgraph_node *saved_node = NULL, *node;
758
759 timevar_push (TV_EXPAND);
760
761 gcc_assert (!flag_unit_at_a_time || cgraph_global_info_ready);
762
763 /* Initialize the RTL code for the function. */
764 current_function_decl = fndecl;
765 saved_loc = input_location;
766 input_location = DECL_SOURCE_LOCATION (fndecl);
767 init_function_start (fndecl);
768
769 /* Even though we're inside a function body, we still don't want to
770 call expand_expr to calculate the size of a variable-sized array.
771 We haven't necessarily assigned RTL to all variables yet, so it's
772 not safe to try to expand expressions involving them. */
773 cfun->x_dont_save_pending_sizes_p = 1;
774 cfun->after_inlining = true;
775
776 node = cgraph_node (fndecl);
777
778 /* We might need the body of this function so that we can expand
779 it inline somewhere else. This means not lowering some constructs
780 such as exception handling. */
781 if (cgraph_preserve_function_body_p (fndecl))
782 {
783 if (!flag_unit_at_a_time)
784 {
785 struct cgraph_edge *e;
786
787 saved_node = cgraph_clone_node (node, node->count, 1);
788 for (e = saved_node->callees; e; e = e->next_callee)
789 if (!e->inline_failed)
790 cgraph_clone_inlined_nodes (e, true);
791 }
792 cfun->saved_static_chain_decl = cfun->static_chain_decl;
793 save_body (fndecl, &cfun->saved_args, &cfun->saved_static_chain_decl);
794 }
795
796 if (flag_inline_trees)
797 {
798 struct cgraph_edge *e;
799 for (e = node->callees; e; e = e->next_callee)
800 if (!e->inline_failed || warn_inline)
801 break;
802 if (e)
803 {
804 timevar_push (TV_INTEGRATION);
805 optimize_inline_calls (fndecl);
806 timevar_pop (TV_INTEGRATION);
807 }
808 }
809 /* We are not going to maintain the cgraph edges up to date.
810 Kill it so it won't confuse us. */
811 while (node->callees)
812 {
813 /* In non-unit-at-a-time we must mark all referenced functions as needed.
814 */
815 if (node->callees->callee->analyzed && !flag_unit_at_a_time)
816 cgraph_mark_needed_node (node->callees->callee);
817 cgraph_remove_edge (node->callees);
818 }
819
820 /* We are not going to maintain the cgraph edges up to date.
821 Kill it so it won't confuse us. */
822 cgraph_node_remove_callees (node);
823
824
825 /* Initialize the default bitmap obstack. */
826 bitmap_obstack_initialize (NULL);
827 bitmap_obstack_initialize (&reg_obstack); /* FIXME, only at RTL generation*/
828
829 tree_register_cfg_hooks ();
830 /* Perform all tree transforms and optimizations. */
831 execute_pass_list (all_passes);
832
833 bitmap_obstack_release (&reg_obstack);
834
835 /* Release the default bitmap obstack. */
836 bitmap_obstack_release (NULL);
837
838 /* Restore original body if still needed. */
839 if (cfun->saved_cfg)
840 {
841 DECL_ARGUMENTS (fndecl) = cfun->saved_args;
842 cfun->cfg = cfun->saved_cfg;
843 cfun->eh = cfun->saved_eh;
844 cfun->saved_cfg = NULL;
845 cfun->saved_eh = NULL;
846 cfun->saved_args = NULL_TREE;
847 cfun->static_chain_decl = cfun->saved_static_chain_decl;
848 cfun->saved_static_chain_decl = NULL;
849 /* When not in unit-at-a-time mode, we must preserve out of line copy
850 representing node before inlining. Restore original outgoing edges
851 using clone we created earlier. */
852 if (!flag_unit_at_a_time)
853 {
854 struct cgraph_edge *e;
855
856 node = cgraph_node (current_function_decl);
857 cgraph_node_remove_callees (node);
858 node->callees = saved_node->callees;
859 saved_node->callees = NULL;
860 update_inlined_to_pointers (node, node);
861 for (e = node->callees; e; e = e->next_callee)
862 e->caller = node;
863 cgraph_remove_node (saved_node);
864 }
865 }
866 else
867 DECL_SAVED_TREE (fndecl) = NULL;
868 cfun = 0;
869
870 /* If requested, warn about function definitions where the function will
871 return a value (usually of some struct or union type) which itself will
872 take up a lot of stack space. */
873 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
874 {
875 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
876
877 if (ret_type && TYPE_SIZE_UNIT (ret_type)
878 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
879 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
880 larger_than_size))
881 {
882 unsigned int size_as_int
883 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
884
885 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
886 warning (0, "%Jsize of return value of %qD is %u bytes",
887 fndecl, fndecl, size_as_int);
888 else
889 warning (0, "%Jsize of return value of %qD is larger than %wd bytes",
890 fndecl, fndecl, larger_than_size);
891 }
892 }
893
894 if (!flag_inline_trees)
895 {
896 DECL_SAVED_TREE (fndecl) = NULL;
897 if (DECL_STRUCT_FUNCTION (fndecl) == 0
898 && !cgraph_node (fndecl)->origin)
899 {
900 /* Stop pointing to the local nodes about to be freed.
901 But DECL_INITIAL must remain nonzero so we know this
902 was an actual function definition.
903 For a nested function, this is done in c_pop_function_context.
904 If rest_of_compilation set this to 0, leave it 0. */
905 if (DECL_INITIAL (fndecl) != 0)
906 DECL_INITIAL (fndecl) = error_mark_node;
907 }
908 }
909
910 input_location = saved_loc;
911
912 ggc_collect ();
913 timevar_pop (TV_EXPAND);
914 }