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"
52 /* Global variables used to communicate with passes. */
54 bitmap vars_to_rename
;
57 /* The root of the compilation pass tree, once constructed. */
58 static struct tree_opt_pass
*all_passes
;
60 /* Pass: dump the gimplified, inlined, functions. */
62 static struct tree_opt_pass pass_gimple
=
69 0, /* static_pass_number */
71 0, /* properties_required */
72 PROP_gimple_any
, /* properties_provided */
73 0, /* properties_destroyed */
74 0, /* todo_flags_start */
75 TODO_dump_func
, /* todo_flags_finish */
79 /* Gate: execute, or not, all of the non-trivial optimizations. */
82 gate_all_optimizations (void)
85 /* Don't bother doing anything if the program has errors. */
86 && !(errorcount
|| sorrycount
));
89 static struct tree_opt_pass pass_all_optimizations
=
92 gate_all_optimizations
, /* gate */
96 0, /* static_pass_number */
98 0, /* properties_required */
99 0, /* properties_provided */
100 0, /* properties_destroyed */
101 0, /* todo_flags_start */
102 0, /* todo_flags_finish */
106 /* Pass: cleanup the CFG just before expanding trees to RTL.
107 This is just a round of label cleanups and case node grouping
108 because after the tree optimizers have run such cleanups may
112 execute_cleanup_cfg_post_optimizing (void)
115 cleanup_dead_labels ();
116 group_case_labels ();
119 static struct tree_opt_pass pass_cleanup_cfg_post_optimizing
=
121 "final_cleanup", /* name */
123 execute_cleanup_cfg_post_optimizing
, /* execute */
126 0, /* static_pass_number */
128 PROP_cfg
, /* properties_required */
129 0, /* properties_provided */
130 0, /* properties_destroyed */
131 0, /* todo_flags_start */
132 TODO_dump_func
, /* todo_flags_finish */
136 /* Pass: do the actions required to finish with tree-ssa optimization
140 execute_free_datastructures (void)
144 /* ??? This isn't the right place for this. Worse, it got computed
145 more or less at random in various passes. */
146 free_dominance_info (CDI_DOMINATORS
);
148 /* Emit gotos for implicit jumps. */
149 disband_implicit_edges ();
151 /* Remove the ssa structures. Do it here since this includes statement
152 annotations that need to be intact during disband_implicit_edges. */
155 /* Re-chain the statements from the blocks. */
156 chain
= &DECL_SAVED_TREE (current_function_decl
);
157 *chain
= alloc_stmt_list ();
159 /* And get rid of annotations we no longer need. */
160 delete_tree_cfg_annotations ();
163 static struct tree_opt_pass pass_free_datastructures
=
167 execute_free_datastructures
, /* execute */
170 0, /* static_pass_number */
172 PROP_cfg
, /* properties_required */
173 0, /* properties_provided */
174 0, /* properties_destroyed */
175 0, /* todo_flags_start */
176 0, /* todo_flags_finish */
181 /* Do the actions required to initialize internal data structures used
182 in tree-ssa optimization passes. */
185 execute_init_datastructures (void)
187 /* Allocate hash tables, arrays and other structures. */
191 static struct tree_opt_pass pass_init_datastructures
=
195 execute_init_datastructures
, /* execute */
198 0, /* static_pass_number */
200 PROP_cfg
, /* properties_required */
201 0, /* properties_provided */
202 0, /* properties_destroyed */
203 0, /* todo_flags_start */
204 0, /* todo_flags_finish */
208 /* Iterate over the pass tree allocating dump file numbers. We want
209 to do this depth first, and independent of whether the pass is
213 register_one_dump_file (struct tree_opt_pass
*pass
, int n
)
215 char *dot_name
, *flag_name
;
218 /* See below in next_pass_1. */
220 if (pass
->static_pass_number
!= -1)
221 sprintf (num
, "%d", ((int) pass
->static_pass_number
< 0
222 ? 1 : pass
->static_pass_number
));
224 dot_name
= concat (".", pass
->name
, num
, NULL
);
225 if (pass
->properties_provided
& PROP_trees
)
227 flag_name
= concat ("tree-", pass
->name
, num
, NULL
);
228 pass
->static_pass_number
= dump_register (dot_name
, flag_name
,
229 TDF_TREE
, n
+ TDI_tree_all
, 0);
233 flag_name
= concat ("rtl-", pass
->name
, num
, NULL
);
234 pass
->static_pass_number
= dump_register (dot_name
, flag_name
,
235 TDF_RTL
, n
, pass
->letter
);
240 register_dump_files (struct tree_opt_pass
*pass
, int properties
)
248 pass
->properties_required
= properties
;
250 (properties
| pass
->properties_provided
) & ~pass
->properties_destroyed
;
252 /* Reset the counter when we reach RTL-based passes. */
253 if ((pass
->properties_provided
^ pass
->properties_required
) & PROP_rtl
)
261 new_properties
= register_dump_files (pass
->sub
, new_properties
);
263 /* If we have a gate, combine the properties that we could have with
264 and without the pass being examined. */
266 properties
&= new_properties
;
268 properties
= new_properties
;
270 pass
->properties_provided
= properties
;
272 register_one_dump_file (pass
, pass_number
);
281 /* Add a pass to the pass list. Duplicate the pass if it's already
284 static struct tree_opt_pass
**
285 next_pass_1 (struct tree_opt_pass
**list
, struct tree_opt_pass
*pass
)
288 /* A nonzero static_pass_number indicates that the
289 pass is already in the list. */
290 if (pass
->static_pass_number
)
292 struct tree_opt_pass
*new;
294 new = xmalloc (sizeof (*new));
295 memcpy (new, pass
, sizeof (*new));
297 /* Indicate to register_dump_files that this pass has duplicates,
298 and so it should rename the dump file. The first instance will
299 be -1, and be number of duplicates = -static_pass_number - 1.
300 Subsequent instances will be > 0 and just the duplicate number. */
303 pass
->static_pass_number
-= 1;
304 new->static_pass_number
= -pass
->static_pass_number
;
311 pass
->static_pass_number
= -1;
315 return &(*list
)->next
;
319 /* Construct the pass tree. */
322 init_tree_optimization_passes (void)
324 struct tree_opt_pass
**p
;
326 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &PASS))
329 NEXT_PASS (pass_gimple
);
330 NEXT_PASS (pass_remove_useless_stmts
);
331 NEXT_PASS (pass_mudflap_1
);
332 NEXT_PASS (pass_lower_cf
);
333 NEXT_PASS (pass_lower_eh
);
334 NEXT_PASS (pass_build_cfg
);
335 NEXT_PASS (pass_pre_expand
);
336 NEXT_PASS (pass_tree_profile
);
337 NEXT_PASS (pass_init_datastructures
);
338 NEXT_PASS (pass_all_optimizations
);
339 NEXT_PASS (pass_warn_function_return
);
340 NEXT_PASS (pass_mudflap_2
);
341 NEXT_PASS (pass_free_datastructures
);
342 NEXT_PASS (pass_expand
);
343 NEXT_PASS (pass_rest_of_compilation
);
346 p
= &pass_all_optimizations
.sub
;
347 NEXT_PASS (pass_referenced_vars
);
348 NEXT_PASS (pass_maybe_create_global_var
);
349 NEXT_PASS (pass_create_structure_vars
);
350 NEXT_PASS (pass_build_ssa
);
351 /* NEXT_PASS (pass_build_pta); */
352 NEXT_PASS (pass_may_alias
);
353 /* NEXT_PASS (pass_del_pta); */
354 NEXT_PASS (pass_rename_ssa_copies
);
355 NEXT_PASS (pass_early_warn_uninitialized
);
356 NEXT_PASS (pass_dce
);
357 NEXT_PASS (pass_dominator
);
358 NEXT_PASS (pass_redundant_phi
);
359 NEXT_PASS (pass_dce
);
360 NEXT_PASS (pass_merge_phi
);
361 NEXT_PASS (pass_forwprop
);
362 NEXT_PASS (pass_phiopt
);
363 /* NEXT_PASS (pass_build_pta); */
364 NEXT_PASS (pass_may_alias
);
365 /* NEXT_PASS (pass_del_pta); */
366 NEXT_PASS (pass_tail_recursion
);
368 NEXT_PASS (pass_profile
);
369 NEXT_PASS (pass_sra
);
370 NEXT_PASS (pass_rename_ssa_copies
);
371 NEXT_PASS (pass_dominator
);
372 NEXT_PASS (pass_redundant_phi
);
373 NEXT_PASS (pass_dce
);
374 NEXT_PASS (pass_dse
);
375 /* NEXT_PASS (pass_build_pta); */
376 NEXT_PASS (pass_may_alias
);
377 /* NEXT_PASS (pass_del_pta); */
378 NEXT_PASS (pass_forwprop
);
379 NEXT_PASS (pass_phiopt
);
380 NEXT_PASS (pass_ccp
);
381 NEXT_PASS (pass_redundant_phi
);
382 NEXT_PASS (pass_fold_builtins
);
383 /* FIXME: May alias should a TODO but for 4.0.0,
384 we add may_alias right after fold builtins
385 which can create arbitrary GIMPLE. */
386 /* NEXT_PASS (pass_build_pta); */
387 NEXT_PASS (pass_may_alias
);
388 /* NEXT_PASS (pass_del_pta); */
389 NEXT_PASS (pass_split_crit_edges
);
390 NEXT_PASS (pass_pre
);
391 NEXT_PASS (pass_loop
);
392 NEXT_PASS (pass_dominator
);
393 NEXT_PASS (pass_redundant_phi
);
394 /* FIXME: If DCE is not run before checking for uninitialized uses,
395 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
396 However, this also causes us to misdiagnose cases that should be
397 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
399 To fix the false positives in uninit-5.c, we would have to
400 account for the predicates protecting the set and the use of each
401 variable. Using a representation like Gated Single Assignment
403 NEXT_PASS (pass_late_warn_uninitialized
);
404 NEXT_PASS (pass_cd_dce
);
405 NEXT_PASS (pass_dse
);
406 NEXT_PASS (pass_forwprop
);
407 NEXT_PASS (pass_phiopt
);
408 NEXT_PASS (pass_tail_calls
);
409 NEXT_PASS (pass_rename_ssa_copies
);
410 NEXT_PASS (pass_del_ssa
);
411 NEXT_PASS (pass_nrv
);
412 NEXT_PASS (pass_remove_useless_vars
);
413 NEXT_PASS (pass_mark_used_blocks
);
414 NEXT_PASS (pass_cleanup_cfg_post_optimizing
);
418 NEXT_PASS (pass_loop_init
);
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_complete_unroll
);
427 /* XXX: ivopts thinks it knows what its doing in creating type/name tags,
428 but it doesn't, and should be calling may_alias instead. */
429 /* NEXT_PASS (pass_iv_optimize); */
430 NEXT_PASS (pass_loop_done
);
435 /* Register the passes with the tree dump code. */
436 register_dump_files (all_passes
, 0);
439 static void execute_pass_list (struct tree_opt_pass
*);
441 static unsigned int last_verified
;
444 execute_todo (int properties
, unsigned int flags
)
446 if (flags
& TODO_rename_vars
)
448 rewrite_into_ssa (false);
449 bitmap_clear (vars_to_rename
);
451 if (flags
& TODO_fix_def_def_chains
)
453 rewrite_def_def_chains ();
454 bitmap_clear (vars_to_rename
);
457 if (flags
& TODO_cleanup_cfg
)
460 if ((flags
& TODO_dump_func
) && dump_file
)
462 if (properties
& PROP_trees
)
463 dump_function_to_file (current_function_decl
,
464 dump_file
, dump_flags
);
465 else if (properties
& PROP_cfg
)
466 print_rtl_with_bb (dump_file
, get_insns ());
468 print_rtl (dump_file
, get_insns ());
470 /* Flush the file. If verification fails, we won't be able to
471 close the file before aborting. */
475 if (flags
& TODO_ggc_collect
)
478 #ifdef ENABLE_CHECKING
479 if (flags
& TODO_verify_ssa
)
481 if (flags
& TODO_verify_flow
)
483 if (flags
& TODO_verify_stmts
)
489 execute_one_pass (struct tree_opt_pass
*pass
)
493 /* See if we're supposed to run this pass. */
494 if (pass
->gate
&& !pass
->gate ())
497 /* Note that the folders should only create gimple expressions.
498 This is a hack until the new folder is ready. */
499 in_gimple_form
= (pass
->properties_provided
& PROP_trees
) != 0;
501 /* Run pre-pass verification. */
502 todo
= pass
->todo_flags_start
& ~last_verified
;
504 execute_todo (pass
->properties_required
, todo
);
506 /* If a dump file name is present, open it if enabled. */
507 if (pass
->static_pass_number
!= -1)
509 bool initializing_dump
= !dump_initialized_p (pass
->static_pass_number
);
510 dump_file_name
= get_dump_file_name (pass
->static_pass_number
);
511 dump_file
= dump_begin (pass
->static_pass_number
, &dump_flags
);
514 const char *dname
, *aname
;
515 dname
= lang_hooks
.decl_printable_name (current_function_decl
, 2);
516 aname
= (IDENTIFIER_POINTER
517 (DECL_ASSEMBLER_NAME (current_function_decl
)));
518 fprintf (dump_file
, "\n;; Function %s (%s)%s\n\n", dname
, aname
,
519 cfun
->function_frequency
== FUNCTION_FREQUENCY_HOT
521 : cfun
->function_frequency
== FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
522 ? " (unlikely executed)"
526 if (initializing_dump
527 && graph_dump_format
!= no_graph
528 && (pass
->properties_provided
& (PROP_cfg
| PROP_rtl
))
529 == (PROP_cfg
| PROP_rtl
))
530 clean_graph_dump_file (dump_file_name
);
533 /* If a timevar is present, start it. */
535 timevar_push (pass
->tv_id
);
543 timevar_pop (pass
->tv_id
);
546 && (pass
->properties_provided
& (PROP_cfg
| PROP_rtl
))
547 == (PROP_cfg
| PROP_rtl
))
548 print_rtl_with_bb (dump_file
, get_insns ());
550 /* Run post-pass cleanup and verification. */
551 todo
= pass
->todo_flags_finish
;
552 last_verified
= todo
& TODO_verify_all
;
554 execute_todo (pass
->properties_provided
, todo
);
556 /* Flush and close dump file. */
559 free ((char *) dump_file_name
);
560 dump_file_name
= NULL
;
564 dump_end (pass
->static_pass_number
, dump_file
);
572 execute_pass_list (struct tree_opt_pass
*pass
)
576 if (execute_one_pass (pass
) && pass
->sub
)
577 execute_pass_list (pass
->sub
);
584 /* Update recursively all inlined_to pointers of functions
585 inlined into NODE to INLINED_TO. */
587 update_inlined_to_pointers (struct cgraph_node
*node
,
588 struct cgraph_node
*inlined_to
)
590 struct cgraph_edge
*e
;
591 for (e
= node
->callees
; e
; e
= e
->next_callee
)
593 if (e
->callee
->global
.inlined_to
)
595 e
->callee
->global
.inlined_to
= inlined_to
;
596 update_inlined_to_pointers (e
->callee
, inlined_to
);
602 /* For functions-as-trees languages, this performs all optimization and
603 compilation for FNDECL. */
606 tree_rest_of_compilation (tree fndecl
)
608 location_t saved_loc
;
609 struct cgraph_node
*saved_node
= NULL
, *node
;
611 timevar_push (TV_EXPAND
);
613 gcc_assert (!flag_unit_at_a_time
|| cgraph_global_info_ready
);
615 /* Initialize the RTL code for the function. */
616 current_function_decl
= fndecl
;
617 saved_loc
= input_location
;
618 input_location
= DECL_SOURCE_LOCATION (fndecl
);
619 init_function_start (fndecl
);
621 /* Even though we're inside a function body, we still don't want to
622 call expand_expr to calculate the size of a variable-sized array.
623 We haven't necessarily assigned RTL to all variables yet, so it's
624 not safe to try to expand expressions involving them. */
625 cfun
->x_dont_save_pending_sizes_p
= 1;
627 node
= cgraph_node (fndecl
);
629 /* We might need the body of this function so that we can expand
630 it inline somewhere else. This means not lowering some constructs
631 such as exception handling. */
632 if (cgraph_preserve_function_body_p (fndecl
))
634 if (!flag_unit_at_a_time
)
636 struct cgraph_edge
*e
;
638 saved_node
= cgraph_clone_node (node
);
639 for (e
= saved_node
->callees
; e
; e
= e
->next_callee
)
640 if (!e
->inline_failed
)
641 cgraph_clone_inlined_nodes (e
, true);
643 cfun
->saved_static_chain_decl
= cfun
->static_chain_decl
;
644 cfun
->saved_tree
= save_body (fndecl
, &cfun
->saved_args
,
645 &cfun
->saved_static_chain_decl
);
648 if (flag_inline_trees
)
650 struct cgraph_edge
*e
;
651 for (e
= node
->callees
; e
; e
= e
->next_callee
)
652 if (!e
->inline_failed
|| warn_inline
)
656 timevar_push (TV_INTEGRATION
);
657 optimize_inline_calls (fndecl
);
658 timevar_pop (TV_INTEGRATION
);
662 /* We are not going to maintain the cgraph edges up to date.
663 Kill it so it won't confuse us. */
664 while (node
->callees
)
665 cgraph_remove_edge (node
->callees
);
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_XMALLOC ();
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 while (node
->callees
)
697 cgraph_remove_edge (node
->callees
);
698 node
->callees
= saved_node
->callees
;
699 saved_node
->callees
= NULL
;
700 update_inlined_to_pointers (node
, node
);
701 for (e
= node
->callees
; e
; e
= e
->next_callee
)
703 cgraph_remove_node (saved_node
);
707 DECL_SAVED_TREE (fndecl
) = NULL
;
710 /* If requested, warn about function definitions where the function will
711 return a value (usually of some struct or union type) which itself will
712 take up a lot of stack space. */
713 if (warn_larger_than
&& !DECL_EXTERNAL (fndecl
) && TREE_TYPE (fndecl
))
715 tree ret_type
= TREE_TYPE (TREE_TYPE (fndecl
));
717 if (ret_type
&& TYPE_SIZE_UNIT (ret_type
)
718 && TREE_CODE (TYPE_SIZE_UNIT (ret_type
)) == INTEGER_CST
719 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type
),
722 unsigned int size_as_int
723 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type
));
725 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type
), size_as_int
) == 0)
726 warning ("%Jsize of return value of %qD is %u bytes",
727 fndecl
, fndecl
, size_as_int
);
729 warning ("%Jsize of return value of %qD is larger than %wd bytes",
730 fndecl
, fndecl
, larger_than_size
);
734 if (!flag_inline_trees
)
736 DECL_SAVED_TREE (fndecl
) = NULL
;
737 if (DECL_STRUCT_FUNCTION (fndecl
) == 0
738 && !cgraph_node (fndecl
)->origin
)
740 /* Stop pointing to the local nodes about to be freed.
741 But DECL_INITIAL must remain nonzero so we know this
742 was an actual function definition.
743 For a nested function, this is done in c_pop_function_context.
744 If rest_of_compilation set this to 0, leave it 0. */
745 if (DECL_INITIAL (fndecl
) != 0)
746 DECL_INITIAL (fndecl
) = error_mark_node
;
750 input_location
= saved_loc
;
753 timevar_pop (TV_EXPAND
);