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>
5 This file is part of GCC.
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)
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.
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
33 #include "diagnostic.h"
34 #include "basic-block.h"
36 #include "tree-flow.h"
37 #include "tree-dump.h"
40 #include "langhooks.h"
44 #include "tree-inline.h"
45 #include "tree-mudflap.h"
46 #include "tree-pass.h"
53 /* Global variables used to communicate with passes. */
57 /* The root of the compilation pass tree, once constructed. */
58 static struct tree_opt_pass
*all_passes
, *all_ipa_passes
;
60 /* Gate: execute, or not, all of the non-trivial optimizations. */
63 gate_all_optimizations (void)
66 /* Don't bother doing anything if the program has errors. */
67 && !(errorcount
|| sorrycount
));
70 static struct tree_opt_pass pass_all_optimizations
=
73 gate_all_optimizations
, /* gate */
77 0, /* static_pass_number */
79 0, /* properties_required */
80 0, /* properties_provided */
81 0, /* properties_destroyed */
82 0, /* todo_flags_start */
83 0, /* todo_flags_finish */
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
93 execute_cleanup_cfg_post_optimizing (void)
96 cleanup_dead_labels ();
100 static struct tree_opt_pass pass_cleanup_cfg_post_optimizing
=
102 "final_cleanup", /* name */
104 execute_cleanup_cfg_post_optimizing
, /* execute */
107 0, /* static_pass_number */
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 */
117 /* Pass: do the actions required to finish with tree-ssa optimization
121 execute_free_datastructures (void)
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
);
129 /* Emit gotos for implicit jumps. */
130 disband_implicit_edges ();
132 /* Remove the ssa structures. Do it here since this includes statement
133 annotations that need to be intact during disband_implicit_edges. */
136 /* Re-chain the statements from the blocks. */
137 chain
= &DECL_SAVED_TREE (current_function_decl
);
138 *chain
= alloc_stmt_list ();
140 /* And get rid of annotations we no longer need. */
141 delete_tree_cfg_annotations ();
144 static struct tree_opt_pass pass_free_datastructures
=
148 execute_free_datastructures
, /* execute */
151 0, /* static_pass_number */
153 PROP_cfg
, /* properties_required */
154 0, /* properties_provided */
155 0, /* properties_destroyed */
156 0, /* todo_flags_start */
157 0, /* todo_flags_finish */
162 /* Do the actions required to initialize internal data structures used
163 in tree-ssa optimization passes. */
166 execute_init_datastructures (void)
168 /* Allocate hash tables, arrays and other structures. */
172 static struct tree_opt_pass pass_init_datastructures
=
176 execute_init_datastructures
, /* execute */
179 0, /* static_pass_number */
181 PROP_cfg
, /* properties_required */
182 0, /* properties_provided */
183 0, /* properties_destroyed */
184 0, /* todo_flags_start */
185 0, /* todo_flags_finish */
189 /* Iterate over the pass tree allocating dump file numbers. We want
190 to do this depth first, and independent of whether the pass is
194 register_one_dump_file (struct tree_opt_pass
*pass
, bool ipa
, int n
)
196 char *dot_name
, *flag_name
, *glob_name
;
199 /* See below in next_pass_1. */
201 if (pass
->static_pass_number
!= -1)
202 sprintf (num
, "%d", ((int) pass
->static_pass_number
< 0
203 ? 1 : pass
->static_pass_number
));
205 dot_name
= concat (".", pass
->name
, num
, NULL
);
208 flag_name
= concat ("ipa-", pass
->name
, num
, NULL
);
209 glob_name
= concat ("ipa-", pass
->name
, NULL
);
210 /* First IPA dump is cgraph that is dumped via separate channels. */
211 pass
->static_pass_number
= dump_register (dot_name
, flag_name
, glob_name
,
214 else if (pass
->properties_provided
& PROP_trees
)
216 flag_name
= concat ("tree-", pass
->name
, num
, NULL
);
217 glob_name
= concat ("tree-", pass
->name
, NULL
);
218 pass
->static_pass_number
= dump_register (dot_name
, flag_name
, glob_name
,
219 TDF_TREE
, n
+ TDI_tree_all
, 0);
223 flag_name
= concat ("rtl-", pass
->name
, num
, NULL
);
224 glob_name
= concat ("rtl-", pass
->name
, NULL
);
225 pass
->static_pass_number
= dump_register (dot_name
, flag_name
, glob_name
,
226 TDF_RTL
, n
, pass
->letter
);
231 register_dump_files (struct tree_opt_pass
*pass
, bool ipa
, int properties
)
239 pass
->properties_required
= properties
;
241 (properties
| pass
->properties_provided
) & ~pass
->properties_destroyed
;
243 /* Reset the counter when we reach RTL-based passes. */
244 if ((pass
->properties_provided
^ pass
->properties_required
) & PROP_rtl
)
252 new_properties
= register_dump_files (pass
->sub
, ipa
, new_properties
);
254 /* If we have a gate, combine the properties that we could have with
255 and without the pass being examined. */
257 properties
&= new_properties
;
259 properties
= new_properties
;
261 pass
->properties_provided
= properties
;
263 register_one_dump_file (pass
, ipa
, pass_number
);
272 /* Add a pass to the pass list. Duplicate the pass if it's already
275 static struct tree_opt_pass
**
276 next_pass_1 (struct tree_opt_pass
**list
, struct tree_opt_pass
*pass
)
279 /* A nonzero static_pass_number indicates that the
280 pass is already in the list. */
281 if (pass
->static_pass_number
)
283 struct tree_opt_pass
*new;
285 new = xmalloc (sizeof (*new));
286 memcpy (new, pass
, sizeof (*new));
288 /* Indicate to register_dump_files that this pass has duplicates,
289 and so it should rename the dump file. The first instance will
290 be -1, and be number of duplicates = -static_pass_number - 1.
291 Subsequent instances will be > 0 and just the duplicate number. */
294 pass
->static_pass_number
-= 1;
295 new->static_pass_number
= -pass
->static_pass_number
;
302 pass
->static_pass_number
= -1;
306 return &(*list
)->next
;
310 /* Construct the pass tree. */
313 init_tree_optimization_passes (void)
315 struct tree_opt_pass
**p
;
317 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &PASS))
318 /* Intraprocedural optimization passes. */
320 NEXT_PASS (pass_ipa_inline
);
324 NEXT_PASS (pass_remove_useless_stmts
);
325 NEXT_PASS (pass_mudflap_1
);
326 NEXT_PASS (pass_lower_cf
);
327 NEXT_PASS (pass_lower_eh
);
328 NEXT_PASS (pass_build_cfg
);
329 NEXT_PASS (pass_pre_expand
);
330 NEXT_PASS (pass_warn_function_return
);
331 NEXT_PASS (pass_tree_profile
);
332 NEXT_PASS (pass_init_datastructures
);
333 NEXT_PASS (pass_all_optimizations
);
334 NEXT_PASS (pass_warn_function_noreturn
);
335 NEXT_PASS (pass_mudflap_2
);
336 NEXT_PASS (pass_free_datastructures
);
337 NEXT_PASS (pass_expand
);
338 NEXT_PASS (pass_rest_of_compilation
);
341 p
= &pass_all_optimizations
.sub
;
342 NEXT_PASS (pass_referenced_vars
);
343 NEXT_PASS (pass_create_structure_vars
);
344 NEXT_PASS (pass_build_ssa
);
345 NEXT_PASS (pass_may_alias
);
346 NEXT_PASS (pass_rename_ssa_copies
);
347 NEXT_PASS (pass_early_warn_uninitialized
);
349 /* Initial scalar cleanups. */
350 NEXT_PASS (pass_ccp
);
351 NEXT_PASS (pass_fre
);
352 NEXT_PASS (pass_dce
);
353 NEXT_PASS (pass_forwprop
);
354 NEXT_PASS (pass_vrp
);
355 NEXT_PASS (pass_copy_prop
);
356 NEXT_PASS (pass_dce
);
357 NEXT_PASS (pass_merge_phi
);
358 NEXT_PASS (pass_dominator
);
360 NEXT_PASS (pass_phiopt
);
361 NEXT_PASS (pass_may_alias
);
362 NEXT_PASS (pass_tail_recursion
);
363 NEXT_PASS (pass_profile
);
365 NEXT_PASS (pass_stdarg
);
366 NEXT_PASS (pass_sra
);
367 /* FIXME: SRA may generate arbitrary gimple code, exposing new
368 aliased and call-clobbered variables. As mentioned below,
369 pass_may_alias should be a TODO item. */
370 NEXT_PASS (pass_may_alias
);
371 NEXT_PASS (pass_rename_ssa_copies
);
372 NEXT_PASS (pass_dominator
);
373 NEXT_PASS (pass_copy_prop
);
374 NEXT_PASS (pass_dce
);
375 NEXT_PASS (pass_dse
);
376 NEXT_PASS (pass_may_alias
);
377 NEXT_PASS (pass_forwprop
);
378 NEXT_PASS (pass_phiopt
);
379 NEXT_PASS (pass_store_ccp
);
380 NEXT_PASS (pass_store_copy_prop
);
381 NEXT_PASS (pass_fold_builtins
);
382 /* FIXME: May alias should a TODO but for 4.0.0,
383 we add may_alias right after fold builtins
384 which can create arbitrary GIMPLE. */
385 NEXT_PASS (pass_may_alias
);
386 NEXT_PASS (pass_split_crit_edges
);
387 NEXT_PASS (pass_pre
);
388 NEXT_PASS (pass_sink_code
);
389 NEXT_PASS (pass_loop
);
390 NEXT_PASS (pass_dominator
);
391 NEXT_PASS (pass_copy_prop
);
392 NEXT_PASS (pass_cd_dce
);
393 /* FIXME: If DCE is not run before checking for uninitialized uses,
394 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
395 However, this also causes us to misdiagnose cases that should be
396 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
398 To fix the false positives in uninit-5.c, we would have to
399 account for the predicates protecting the set and the use of each
400 variable. Using a representation like Gated Single Assignment
402 NEXT_PASS (pass_late_warn_uninitialized
);
403 NEXT_PASS (pass_dse
);
404 NEXT_PASS (pass_forwprop
);
405 NEXT_PASS (pass_phiopt
);
406 NEXT_PASS (pass_tail_calls
);
407 NEXT_PASS (pass_rename_ssa_copies
);
408 NEXT_PASS (pass_uncprop
);
409 NEXT_PASS (pass_del_ssa
);
410 NEXT_PASS (pass_nrv
);
411 NEXT_PASS (pass_remove_useless_vars
);
412 NEXT_PASS (pass_mark_used_blocks
);
413 NEXT_PASS (pass_cleanup_cfg_post_optimizing
);
417 NEXT_PASS (pass_loop_init
);
418 NEXT_PASS (pass_copy_prop
);
419 NEXT_PASS (pass_lim
);
420 NEXT_PASS (pass_unswitch
);
421 NEXT_PASS (pass_record_bounds
);
422 NEXT_PASS (pass_linear_transform
);
423 NEXT_PASS (pass_iv_canon
);
424 NEXT_PASS (pass_if_conversion
);
425 NEXT_PASS (pass_vectorize
);
426 NEXT_PASS (pass_lower_vector_ssa
);
427 NEXT_PASS (pass_complete_unroll
);
428 NEXT_PASS (pass_iv_optimize
);
429 NEXT_PASS (pass_loop_done
);
434 register_dump_files (all_passes
, false, PROP_gimple_any
438 register_dump_files (all_ipa_passes
, true, PROP_gimple_any
444 static unsigned int last_verified
;
447 execute_todo (struct tree_opt_pass
*pass
, unsigned int flags
, bool use_required
)
450 = use_required
? pass
->properties_required
: pass
->properties_provided
;
452 #if defined ENABLE_CHECKING
453 if (need_ssa_update_p ())
454 gcc_assert (flags
& TODO_update_ssa_any
);
457 if (flags
& TODO_update_ssa_any
)
459 unsigned update_flags
= flags
& TODO_update_ssa_any
;
460 update_ssa (update_flags
);
463 if (flags
& TODO_cleanup_cfg
)
466 cleanup_tree_cfg_loop ();
471 if ((flags
& TODO_dump_func
)
472 && dump_file
&& current_function_decl
)
474 if (properties
& PROP_trees
)
475 dump_function_to_file (current_function_decl
,
476 dump_file
, dump_flags
);
477 else if (properties
& PROP_cfg
)
478 print_rtl_with_bb (dump_file
, get_insns ());
480 print_rtl (dump_file
, get_insns ());
482 /* Flush the file. If verification fails, we won't be able to
483 close the file before dieing. */
486 if ((flags
& TODO_dump_cgraph
)
487 && dump_file
&& !current_function_decl
)
489 dump_cgraph (dump_file
);
490 /* Flush the file. If verification fails, we won't be able to
491 close the file before aborting. */
495 if (flags
& TODO_ggc_collect
)
500 #if defined ENABLE_CHECKING
501 if ((pass
->properties_required
& PROP_ssa
)
502 && !(pass
->properties_destroyed
& PROP_ssa
))
504 if (flags
& TODO_verify_flow
)
506 if (flags
& TODO_verify_stmts
)
508 if (flags
& TODO_verify_loops
)
509 verify_loop_closed_ssa ();
514 execute_one_pass (struct tree_opt_pass
*pass
)
518 /* See if we're supposed to run this pass. */
519 if (pass
->gate
&& !pass
->gate ())
522 /* Note that the folders should only create gimple expressions.
523 This is a hack until the new folder is ready. */
524 in_gimple_form
= (pass
->properties_provided
& PROP_trees
) != 0;
526 /* Run pre-pass verification. */
527 todo
= pass
->todo_flags_start
& ~last_verified
;
529 execute_todo (pass
, todo
, true);
531 /* If a dump file name is present, open it if enabled. */
532 if (pass
->static_pass_number
!= -1)
534 bool initializing_dump
= !dump_initialized_p (pass
->static_pass_number
);
535 dump_file_name
= get_dump_file_name (pass
->static_pass_number
);
536 dump_file
= dump_begin (pass
->static_pass_number
, &dump_flags
);
537 if (dump_file
&& current_function_decl
)
539 const char *dname
, *aname
;
540 dname
= lang_hooks
.decl_printable_name (current_function_decl
, 2);
541 aname
= (IDENTIFIER_POINTER
542 (DECL_ASSEMBLER_NAME (current_function_decl
)));
543 fprintf (dump_file
, "\n;; Function %s (%s)%s\n\n", dname
, aname
,
544 cfun
->function_frequency
== FUNCTION_FREQUENCY_HOT
546 : cfun
->function_frequency
== FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
547 ? " (unlikely executed)"
551 if (initializing_dump
552 && graph_dump_format
!= no_graph
553 && (pass
->properties_provided
& (PROP_cfg
| PROP_rtl
))
554 == (PROP_cfg
| PROP_rtl
))
555 clean_graph_dump_file (dump_file_name
);
558 /* If a timevar is present, start it. */
560 timevar_push (pass
->tv_id
);
568 timevar_pop (pass
->tv_id
);
571 && (pass
->properties_provided
& (PROP_cfg
| PROP_rtl
))
572 == (PROP_cfg
| PROP_rtl
))
573 print_rtl_with_bb (dump_file
, get_insns ());
575 /* Run post-pass cleanup and verification. */
576 todo
= pass
->todo_flags_finish
;
577 last_verified
= todo
& TODO_verify_all
;
579 execute_todo (pass
, todo
, false);
581 /* Flush and close dump file. */
584 free ((char *) dump_file_name
);
585 dump_file_name
= NULL
;
589 dump_end (pass
->static_pass_number
, dump_file
);
597 execute_pass_list (struct tree_opt_pass
*pass
)
601 if (execute_one_pass (pass
) && pass
->sub
)
602 execute_pass_list (pass
->sub
);
608 /* Execute all IPA passes. */
612 execute_pass_list (all_ipa_passes
);
616 /* Update recursively all inlined_to pointers of functions
617 inlined into NODE to INLINED_TO. */
619 update_inlined_to_pointers (struct cgraph_node
*node
,
620 struct cgraph_node
*inlined_to
)
622 struct cgraph_edge
*e
;
623 for (e
= node
->callees
; e
; e
= e
->next_callee
)
625 if (e
->callee
->global
.inlined_to
)
627 e
->callee
->global
.inlined_to
= inlined_to
;
628 update_inlined_to_pointers (e
->callee
, inlined_to
);
634 /* For functions-as-trees languages, this performs all optimization and
635 compilation for FNDECL. */
638 tree_rest_of_compilation (tree fndecl
)
640 location_t saved_loc
;
641 struct cgraph_node
*saved_node
= NULL
, *node
;
643 timevar_push (TV_EXPAND
);
645 gcc_assert (!flag_unit_at_a_time
|| cgraph_global_info_ready
);
647 /* Initialize the RTL code for the function. */
648 current_function_decl
= fndecl
;
649 saved_loc
= input_location
;
650 input_location
= DECL_SOURCE_LOCATION (fndecl
);
651 init_function_start (fndecl
);
653 /* Even though we're inside a function body, we still don't want to
654 call expand_expr to calculate the size of a variable-sized array.
655 We haven't necessarily assigned RTL to all variables yet, so it's
656 not safe to try to expand expressions involving them. */
657 cfun
->x_dont_save_pending_sizes_p
= 1;
658 cfun
->after_inlining
= true;
660 node
= cgraph_node (fndecl
);
662 /* We might need the body of this function so that we can expand
663 it inline somewhere else. This means not lowering some constructs
664 such as exception handling. */
665 if (cgraph_preserve_function_body_p (fndecl
))
667 if (!flag_unit_at_a_time
)
669 struct cgraph_edge
*e
;
671 saved_node
= cgraph_clone_node (node
);
672 for (e
= saved_node
->callees
; e
; e
= e
->next_callee
)
673 if (!e
->inline_failed
)
674 cgraph_clone_inlined_nodes (e
, true);
676 cfun
->saved_static_chain_decl
= cfun
->static_chain_decl
;
677 cfun
->saved_tree
= save_body (fndecl
, &cfun
->saved_args
,
678 &cfun
->saved_static_chain_decl
);
681 if (flag_inline_trees
)
683 struct cgraph_edge
*e
;
684 for (e
= node
->callees
; e
; e
= e
->next_callee
)
685 if (!e
->inline_failed
|| warn_inline
)
689 timevar_push (TV_INTEGRATION
);
690 optimize_inline_calls (fndecl
);
691 timevar_pop (TV_INTEGRATION
);
694 /* We are not going to maintain the cgraph edges up to date.
695 Kill it so it won't confuse us. */
696 while (node
->callees
)
698 /* In non-unit-at-a-time we must mark all referenced functions as needed.
700 if (node
->callees
->callee
->analyzed
&& !flag_unit_at_a_time
)
701 cgraph_mark_needed_node (node
->callees
->callee
);
702 cgraph_remove_edge (node
->callees
);
705 /* We are not going to maintain the cgraph edges up to date.
706 Kill it so it won't confuse us. */
707 cgraph_node_remove_callees (node
);
710 /* Initialize the default bitmap obstack. */
711 bitmap_obstack_initialize (NULL
);
712 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
714 /* Perform all tree transforms and optimizations. */
715 execute_pass_list (all_passes
);
717 bitmap_obstack_release (®_obstack
);
719 /* Release the default bitmap obstack. */
720 bitmap_obstack_release (NULL
);
722 /* Restore original body if still needed. */
723 if (cfun
->saved_tree
)
725 DECL_SAVED_TREE (fndecl
) = cfun
->saved_tree
;
726 DECL_ARGUMENTS (fndecl
) = cfun
->saved_args
;
727 cfun
->static_chain_decl
= cfun
->saved_static_chain_decl
;
729 /* When not in unit-at-a-time mode, we must preserve out of line copy
730 representing node before inlining. Restore original outgoing edges
731 using clone we created earlier. */
732 if (!flag_unit_at_a_time
)
734 struct cgraph_edge
*e
;
736 cgraph_node_remove_callees (node
);
737 node
->callees
= saved_node
->callees
;
738 saved_node
->callees
= NULL
;
739 update_inlined_to_pointers (node
, node
);
740 for (e
= node
->callees
; e
; e
= e
->next_callee
)
742 cgraph_remove_node (saved_node
);
746 DECL_SAVED_TREE (fndecl
) = NULL
;
749 /* If requested, warn about function definitions where the function will
750 return a value (usually of some struct or union type) which itself will
751 take up a lot of stack space. */
752 if (warn_larger_than
&& !DECL_EXTERNAL (fndecl
) && TREE_TYPE (fndecl
))
754 tree ret_type
= TREE_TYPE (TREE_TYPE (fndecl
));
756 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
757 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
758 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
761 unsigned int size_as_int
762 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
764 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
765 warning (0, "%Jsize of return value of %qD is %u bytes",
766 fndecl
, fndecl
, size_as_int
);
768 warning (0, "%Jsize of return value of %qD is larger than %wd bytes",
769 fndecl
, fndecl
, larger_than_size
);
773 if (!flag_inline_trees
)
775 DECL_SAVED_TREE (fndecl
) = NULL
;
776 if (DECL_STRUCT_FUNCTION (fndecl
) == 0
777 && !cgraph_node (fndecl
)->origin
)
779 /* Stop pointing to the local nodes about to be freed.
780 But DECL_INITIAL must remain nonzero so we know this
781 was an actual function definition.
782 For a nested function, this is done in c_pop_function_context.
783 If rest_of_compilation set this to 0, leave it 0. */
784 if (DECL_INITIAL (fndecl
) != 0)
785 DECL_INITIAL (fndecl
) = error_mark_node
;
789 input_location
= saved_loc
;
792 timevar_pop (TV_EXPAND
);