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. */
55 bitmap vars_to_rename
;
58 /* The root of the compilation pass tree, once constructed. */
59 static struct tree_opt_pass
*all_passes
;
61 /* Pass: dump the gimplified, inlined, functions. */
63 static struct tree_opt_pass pass_gimple
=
70 0, /* static_pass_number */
72 0, /* properties_required */
73 PROP_gimple_any
, /* properties_provided */
74 0, /* properties_destroyed */
75 0, /* todo_flags_start */
76 TODO_dump_func
, /* todo_flags_finish */
80 /* Gate: execute, or not, all of the non-trivial optimizations. */
83 gate_all_optimizations (void)
86 /* Don't bother doing anything if the program has errors. */
87 && !(errorcount
|| sorrycount
));
90 static struct tree_opt_pass pass_all_optimizations
=
93 gate_all_optimizations
, /* gate */
97 0, /* static_pass_number */
99 0, /* properties_required */
100 0, /* properties_provided */
101 0, /* properties_destroyed */
102 0, /* todo_flags_start */
103 0, /* todo_flags_finish */
107 /* Pass: cleanup the CFG just before expanding trees to RTL.
108 This is just a round of label cleanups and case node grouping
109 because after the tree optimizers have run such cleanups may
113 execute_cleanup_cfg_post_optimizing (void)
116 cleanup_dead_labels ();
117 group_case_labels ();
120 static struct tree_opt_pass pass_cleanup_cfg_post_optimizing
=
122 "final_cleanup", /* name */
124 execute_cleanup_cfg_post_optimizing
, /* execute */
127 0, /* static_pass_number */
129 PROP_cfg
, /* properties_required */
130 0, /* properties_provided */
131 0, /* properties_destroyed */
132 0, /* todo_flags_start */
133 TODO_dump_func
, /* todo_flags_finish */
137 /* Pass: do the actions required to finish with tree-ssa optimization
141 execute_free_datastructures (void)
145 /* ??? This isn't the right place for this. Worse, it got computed
146 more or less at random in various passes. */
147 free_dominance_info (CDI_DOMINATORS
);
149 /* Emit gotos for implicit jumps. */
150 disband_implicit_edges ();
152 /* Remove the ssa structures. Do it here since this includes statement
153 annotations that need to be intact during disband_implicit_edges. */
156 /* Re-chain the statements from the blocks. */
157 chain
= &DECL_SAVED_TREE (current_function_decl
);
158 *chain
= alloc_stmt_list ();
160 /* And get rid of annotations we no longer need. */
161 delete_tree_cfg_annotations ();
164 static struct tree_opt_pass pass_free_datastructures
=
168 execute_free_datastructures
, /* execute */
171 0, /* static_pass_number */
173 PROP_cfg
, /* properties_required */
174 0, /* properties_provided */
175 0, /* properties_destroyed */
176 0, /* todo_flags_start */
177 0, /* todo_flags_finish */
182 /* Do the actions required to initialize internal data structures used
183 in tree-ssa optimization passes. */
186 execute_init_datastructures (void)
188 /* Allocate hash tables, arrays and other structures. */
192 static struct tree_opt_pass pass_init_datastructures
=
196 execute_init_datastructures
, /* execute */
199 0, /* static_pass_number */
201 PROP_cfg
, /* properties_required */
202 0, /* properties_provided */
203 0, /* properties_destroyed */
204 0, /* todo_flags_start */
205 0, /* todo_flags_finish */
209 /* Iterate over the pass tree allocating dump file numbers. We want
210 to do this depth first, and independent of whether the pass is
214 register_one_dump_file (struct tree_opt_pass
*pass
, int n
)
216 char *dot_name
, *flag_name
, *glob_name
;
219 /* See below in next_pass_1. */
221 if (pass
->static_pass_number
!= -1)
222 sprintf (num
, "%d", ((int) pass
->static_pass_number
< 0
223 ? 1 : pass
->static_pass_number
));
225 dot_name
= concat (".", pass
->name
, num
, NULL
);
226 if (pass
->properties_provided
& PROP_trees
)
228 flag_name
= concat ("tree-", pass
->name
, num
, NULL
);
229 glob_name
= concat ("tree-", pass
->name
, NULL
);
230 pass
->static_pass_number
= dump_register (dot_name
, flag_name
, glob_name
,
231 TDF_TREE
, n
+ TDI_tree_all
, 0);
235 flag_name
= concat ("rtl-", pass
->name
, num
, NULL
);
236 glob_name
= concat ("rtl-", pass
->name
, NULL
);
237 pass
->static_pass_number
= dump_register (dot_name
, flag_name
, glob_name
,
238 TDF_RTL
, n
, pass
->letter
);
243 register_dump_files (struct tree_opt_pass
*pass
, int properties
)
251 pass
->properties_required
= properties
;
253 (properties
| pass
->properties_provided
) & ~pass
->properties_destroyed
;
255 /* Reset the counter when we reach RTL-based passes. */
256 if ((pass
->properties_provided
^ pass
->properties_required
) & PROP_rtl
)
264 new_properties
= register_dump_files (pass
->sub
, new_properties
);
266 /* If we have a gate, combine the properties that we could have with
267 and without the pass being examined. */
269 properties
&= new_properties
;
271 properties
= new_properties
;
273 pass
->properties_provided
= properties
;
275 register_one_dump_file (pass
, pass_number
);
284 /* Add a pass to the pass list. Duplicate the pass if it's already
287 static struct tree_opt_pass
**
288 next_pass_1 (struct tree_opt_pass
**list
, struct tree_opt_pass
*pass
)
291 /* A nonzero static_pass_number indicates that the
292 pass is already in the list. */
293 if (pass
->static_pass_number
)
295 struct tree_opt_pass
*new;
297 new = xmalloc (sizeof (*new));
298 memcpy (new, pass
, sizeof (*new));
300 /* Indicate to register_dump_files that this pass has duplicates,
301 and so it should rename the dump file. The first instance will
302 be -1, and be number of duplicates = -static_pass_number - 1.
303 Subsequent instances will be > 0 and just the duplicate number. */
306 pass
->static_pass_number
-= 1;
307 new->static_pass_number
= -pass
->static_pass_number
;
314 pass
->static_pass_number
= -1;
318 return &(*list
)->next
;
322 /* Construct the pass tree. */
325 init_tree_optimization_passes (void)
327 struct tree_opt_pass
**p
;
329 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &PASS))
332 NEXT_PASS (pass_gimple
);
333 NEXT_PASS (pass_remove_useless_stmts
);
334 NEXT_PASS (pass_mudflap_1
);
335 NEXT_PASS (pass_lower_cf
);
336 NEXT_PASS (pass_lower_eh
);
337 NEXT_PASS (pass_build_cfg
);
338 NEXT_PASS (pass_pre_expand
);
339 NEXT_PASS (pass_tree_profile
);
340 NEXT_PASS (pass_init_datastructures
);
341 NEXT_PASS (pass_all_optimizations
);
342 NEXT_PASS (pass_warn_function_return
);
343 NEXT_PASS (pass_mudflap_2
);
344 NEXT_PASS (pass_free_datastructures
);
345 NEXT_PASS (pass_expand
);
346 NEXT_PASS (pass_rest_of_compilation
);
349 p
= &pass_all_optimizations
.sub
;
350 NEXT_PASS (pass_referenced_vars
);
351 NEXT_PASS (pass_build_ssa
);
352 NEXT_PASS (pass_may_alias
);
353 NEXT_PASS (pass_rename_ssa_copies
);
354 NEXT_PASS (pass_early_warn_uninitialized
);
355 NEXT_PASS (pass_dce
);
356 NEXT_PASS (pass_dominator
);
357 NEXT_PASS (pass_redundant_phi
);
358 NEXT_PASS (pass_dce
);
359 NEXT_PASS (pass_merge_phi
);
360 NEXT_PASS (pass_forwprop
);
361 NEXT_PASS (pass_phiopt
);
362 NEXT_PASS (pass_may_alias
);
363 NEXT_PASS (pass_tail_recursion
);
365 NEXT_PASS (pass_profile
);
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_redundant_phi
);
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_ccp
);
380 NEXT_PASS (pass_redundant_phi
);
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_redundant_phi
);
392 /* FIXME: If DCE is not run before checking for uninitialized uses,
393 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
394 However, this also causes us to misdiagnose cases that should be
395 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
397 To fix the false positives in uninit-5.c, we would have to
398 account for the predicates protecting the set and the use of each
399 variable. Using a representation like Gated Single Assignment
401 NEXT_PASS (pass_late_warn_uninitialized
);
402 NEXT_PASS (pass_cd_dce
);
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_del_ssa
);
409 NEXT_PASS (pass_nrv
);
410 NEXT_PASS (pass_remove_useless_vars
);
411 NEXT_PASS (pass_mark_used_blocks
);
412 NEXT_PASS (pass_cleanup_cfg_post_optimizing
);
416 NEXT_PASS (pass_loop_init
);
417 NEXT_PASS (pass_lim
);
418 NEXT_PASS (pass_unswitch
);
419 NEXT_PASS (pass_record_bounds
);
420 NEXT_PASS (pass_linear_transform
);
421 NEXT_PASS (pass_iv_canon
);
422 NEXT_PASS (pass_if_conversion
);
423 NEXT_PASS (pass_vectorize
);
424 NEXT_PASS (pass_complete_unroll
);
425 NEXT_PASS (pass_iv_optimize
);
426 NEXT_PASS (pass_loop_done
);
431 /* Register the passes with the tree dump code. */
432 register_dump_files (all_passes
, 0);
435 static void execute_pass_list (struct tree_opt_pass
*);
437 static unsigned int last_verified
;
440 execute_todo (int properties
, unsigned int flags
)
442 if (flags
& TODO_rename_vars
)
444 rewrite_into_ssa (false);
445 bitmap_clear (vars_to_rename
);
447 if (flags
& TODO_fix_def_def_chains
)
449 rewrite_def_def_chains ();
450 bitmap_clear (vars_to_rename
);
453 if (flags
& TODO_cleanup_cfg
)
456 cleanup_tree_cfg_loop ();
461 if ((flags
& TODO_dump_func
) && dump_file
)
463 if (properties
& PROP_trees
)
464 dump_function_to_file (current_function_decl
,
465 dump_file
, dump_flags
);
466 else if (properties
& PROP_cfg
)
467 print_rtl_with_bb (dump_file
, get_insns ());
469 print_rtl (dump_file
, get_insns ());
471 /* Flush the file. If verification fails, we won't be able to
472 close the file before aborting. */
476 if (flags
& TODO_ggc_collect
)
479 #ifdef ENABLE_CHECKING
480 if (flags
& TODO_verify_ssa
)
482 if (flags
& TODO_verify_flow
)
484 if (flags
& TODO_verify_stmts
)
490 execute_one_pass (struct tree_opt_pass
*pass
)
494 /* See if we're supposed to run this pass. */
495 if (pass
->gate
&& !pass
->gate ())
498 /* Note that the folders should only create gimple expressions.
499 This is a hack until the new folder is ready. */
500 in_gimple_form
= (pass
->properties_provided
& PROP_trees
) != 0;
502 /* Run pre-pass verification. */
503 todo
= pass
->todo_flags_start
& ~last_verified
;
505 execute_todo (pass
->properties_required
, todo
);
507 /* If a dump file name is present, open it if enabled. */
508 if (pass
->static_pass_number
!= -1)
510 bool initializing_dump
= !dump_initialized_p (pass
->static_pass_number
);
511 dump_file_name
= get_dump_file_name (pass
->static_pass_number
);
512 dump_file
= dump_begin (pass
->static_pass_number
, &dump_flags
);
515 const char *dname
, *aname
;
516 dname
= lang_hooks
.decl_printable_name (current_function_decl
, 2);
517 aname
= (IDENTIFIER_POINTER
518 (DECL_ASSEMBLER_NAME (current_function_decl
)));
519 fprintf (dump_file
, "\n;; Function %s (%s)%s\n\n", dname
, aname
,
520 cfun
->function_frequency
== FUNCTION_FREQUENCY_HOT
522 : cfun
->function_frequency
== FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
523 ? " (unlikely executed)"
527 if (initializing_dump
528 && graph_dump_format
!= no_graph
529 && (pass
->properties_provided
& (PROP_cfg
| PROP_rtl
))
530 == (PROP_cfg
| PROP_rtl
))
531 clean_graph_dump_file (dump_file_name
);
534 /* If a timevar is present, start it. */
536 timevar_push (pass
->tv_id
);
544 timevar_pop (pass
->tv_id
);
547 && (pass
->properties_provided
& (PROP_cfg
| PROP_rtl
))
548 == (PROP_cfg
| PROP_rtl
))
549 print_rtl_with_bb (dump_file
, get_insns ());
551 /* Run post-pass cleanup and verification. */
552 todo
= pass
->todo_flags_finish
;
553 last_verified
= todo
& TODO_verify_all
;
555 execute_todo (pass
->properties_provided
, todo
);
557 /* Flush and close dump file. */
560 free ((char *) dump_file_name
);
561 dump_file_name
= NULL
;
565 dump_end (pass
->static_pass_number
, dump_file
);
573 execute_pass_list (struct tree_opt_pass
*pass
)
577 if (execute_one_pass (pass
) && pass
->sub
)
578 execute_pass_list (pass
->sub
);
585 /* Update recursively all inlined_to pointers of functions
586 inlined into NODE to INLINED_TO. */
588 update_inlined_to_pointers (struct cgraph_node
*node
,
589 struct cgraph_node
*inlined_to
)
591 struct cgraph_edge
*e
;
592 for (e
= node
->callees
; e
; e
= e
->next_callee
)
594 if (e
->callee
->global
.inlined_to
)
596 e
->callee
->global
.inlined_to
= inlined_to
;
597 update_inlined_to_pointers (e
->callee
, inlined_to
);
603 /* For functions-as-trees languages, this performs all optimization and
604 compilation for FNDECL. */
607 tree_rest_of_compilation (tree fndecl
)
609 location_t saved_loc
;
610 struct cgraph_node
*saved_node
= NULL
, *node
;
612 timevar_push (TV_EXPAND
);
614 gcc_assert (!flag_unit_at_a_time
|| cgraph_global_info_ready
);
616 /* Initialize the RTL code for the function. */
617 current_function_decl
= fndecl
;
618 saved_loc
= input_location
;
619 input_location
= DECL_SOURCE_LOCATION (fndecl
);
620 init_function_start (fndecl
);
622 /* Even though we're inside a function body, we still don't want to
623 call expand_expr to calculate the size of a variable-sized array.
624 We haven't necessarily assigned RTL to all variables yet, so it's
625 not safe to try to expand expressions involving them. */
626 cfun
->x_dont_save_pending_sizes_p
= 1;
628 node
= cgraph_node (fndecl
);
630 /* We might need the body of this function so that we can expand
631 it inline somewhere else. This means not lowering some constructs
632 such as exception handling. */
633 if (cgraph_preserve_function_body_p (fndecl
))
635 if (!flag_unit_at_a_time
)
637 struct cgraph_edge
*e
;
639 saved_node
= cgraph_clone_node (node
);
640 for (e
= saved_node
->callees
; e
; e
= e
->next_callee
)
641 if (!e
->inline_failed
)
642 cgraph_clone_inlined_nodes (e
, true);
644 cfun
->saved_static_chain_decl
= cfun
->static_chain_decl
;
645 cfun
->saved_tree
= save_body (fndecl
, &cfun
->saved_args
,
646 &cfun
->saved_static_chain_decl
);
649 if (flag_inline_trees
)
651 struct cgraph_edge
*e
;
652 for (e
= node
->callees
; e
; e
= e
->next_callee
)
653 if (!e
->inline_failed
|| warn_inline
)
657 timevar_push (TV_INTEGRATION
);
658 optimize_inline_calls (fndecl
);
659 timevar_pop (TV_INTEGRATION
);
663 /* We are not going to maintain the cgraph edges up to date.
664 Kill it so it won't confuse us. */
665 cgraph_node_remove_callees (node
);
668 /* Initialize the default bitmap obstack. */
669 bitmap_obstack_initialize (NULL
);
670 bitmap_obstack_initialize (®_obstack
); /* FIXME, only at RTL generation*/
672 vars_to_rename
= BITMAP_ALLOC (NULL
);
674 /* Perform all tree transforms and optimizations. */
675 execute_pass_list (all_passes
);
677 bitmap_obstack_release (®_obstack
);
679 /* Release the default bitmap obstack. */
680 bitmap_obstack_release (NULL
);
682 /* Restore original body if still needed. */
683 if (cfun
->saved_tree
)
685 DECL_SAVED_TREE (fndecl
) = cfun
->saved_tree
;
686 DECL_ARGUMENTS (fndecl
) = cfun
->saved_args
;
687 cfun
->static_chain_decl
= cfun
->saved_static_chain_decl
;
689 /* When not in unit-at-a-time mode, we must preserve out of line copy
690 representing node before inlining. Restore original outgoing edges
691 using clone we created earlier. */
692 if (!flag_unit_at_a_time
)
694 struct cgraph_edge
*e
;
696 cgraph_node_remove_callees (node
);
697 node
->callees
= saved_node
->callees
;
698 saved_node
->callees
= NULL
;
699 update_inlined_to_pointers (node
, node
);
700 for (e
= node
->callees
; e
; e
= e
->next_callee
)
702 cgraph_remove_node (saved_node
);
706 DECL_SAVED_TREE (fndecl
) = NULL
;
709 /* If requested, warn about function definitions where the function will
710 return a value (usually of some struct or union type) which itself will
711 take up a lot of stack space. */
712 if (warn_larger_than
&& !DECL_EXTERNAL (fndecl
) && TREE_TYPE (fndecl
))
714 tree ret_type
= TREE_TYPE (TREE_TYPE (fndecl
));
716 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
717 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
718 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
721 unsigned int size_as_int
722 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
724 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
725 warning ("%Jsize of return value of %qD is %u bytes",
726 fndecl
, fndecl
, size_as_int
);
728 warning ("%Jsize of return value of %qD is larger than %wd bytes",
729 fndecl
, fndecl
, larger_than_size
);
733 if (!flag_inline_trees
)
735 DECL_SAVED_TREE (fndecl
) = NULL
;
736 if (DECL_STRUCT_FUNCTION (fndecl
) == 0
737 && !cgraph_node (fndecl
)->origin
)
739 /* Stop pointing to the local nodes about to be freed.
740 But DECL_INITIAL must remain nonzero so we know this
741 was an actual function definition.
742 For a nested function, this is done in c_pop_function_context.
743 If rest_of_compilation set this to 0, leave it 0. */
744 if (DECL_INITIAL (fndecl
) != 0)
745 DECL_INITIAL (fndecl
) = error_mark_node
;
749 input_location
= saved_loc
;
752 timevar_pop (TV_EXPAND
);