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_cse_reciprocals);
387 NEXT_PASS (pass_split_crit_edges);
388 NEXT_PASS (pass_pre);
389 NEXT_PASS (pass_sink_code);
390 NEXT_PASS (pass_loop);
391 NEXT_PASS (pass_dominator);
392 NEXT_PASS (pass_copy_prop);
393 NEXT_PASS (pass_cd_dce);
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_dse);
405 NEXT_PASS (pass_forwprop);
406 NEXT_PASS (pass_phiopt);
407 NEXT_PASS (pass_tail_calls);
408 NEXT_PASS (pass_rename_ssa_copies);
409 NEXT_PASS (pass_uncprop);
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_copy_prop);
420 NEXT_PASS (pass_lim);
421 NEXT_PASS (pass_unswitch);
422 NEXT_PASS (pass_record_bounds);
423 NEXT_PASS (pass_linear_transform);
424 NEXT_PASS (pass_iv_canon);
425 NEXT_PASS (pass_if_conversion);
426 NEXT_PASS (pass_vectorize);
427 NEXT_PASS (pass_lower_vector_ssa);
428 NEXT_PASS (pass_complete_unroll);
429 NEXT_PASS (pass_iv_optimize);
430 NEXT_PASS (pass_loop_done);
435 register_dump_files (all_passes, false, PROP_gimple_any
439 register_dump_files (all_ipa_passes, true, PROP_gimple_any
445 static unsigned int last_verified;
448 execute_todo (struct tree_opt_pass *pass, unsigned int flags, bool use_required)
451 = use_required ? pass->properties_required : pass->properties_provided;
453 #if defined ENABLE_CHECKING
454 if (need_ssa_update_p ())
455 gcc_assert (flags & TODO_update_ssa_any);
458 if (flags & TODO_update_ssa_any)
460 unsigned update_flags = flags & TODO_update_ssa_any;
461 update_ssa (update_flags);
464 if (flags & TODO_cleanup_cfg)
467 cleanup_tree_cfg_loop ();
472 if ((flags & TODO_dump_func)
473 && dump_file && current_function_decl)
475 if (properties & PROP_trees)
476 dump_function_to_file (current_function_decl,
477 dump_file, dump_flags);
478 else if (properties & PROP_cfg)
479 print_rtl_with_bb (dump_file, get_insns ());
481 print_rtl (dump_file, get_insns ());
483 /* Flush the file. If verification fails, we won't be able to
484 close the file before dieing. */
487 if ((flags & TODO_dump_cgraph)
488 && dump_file && !current_function_decl)
490 dump_cgraph (dump_file);
491 /* Flush the file. If verification fails, we won't be able to
492 close the file before aborting. */
496 if (flags & TODO_ggc_collect)
501 #if defined ENABLE_CHECKING
502 if ((pass->properties_required & PROP_ssa)
503 && !(pass->properties_destroyed & PROP_ssa))
505 if (flags & TODO_verify_flow)
507 if (flags & TODO_verify_stmts)
509 if (flags & TODO_verify_loops)
510 verify_loop_closed_ssa ();
515 execute_one_pass (struct tree_opt_pass *pass)
519 /* See if we're supposed to run this pass. */
520 if (pass->gate && !pass->gate ())
523 /* Note that the folders should only create gimple expressions.
524 This is a hack until the new folder is ready. */
525 in_gimple_form = (pass->properties_provided & PROP_trees) != 0;
527 /* Run pre-pass verification. */
528 todo = pass->todo_flags_start & ~last_verified;
530 execute_todo (pass, todo, true);
532 /* If a dump file name is present, open it if enabled. */
533 if (pass->static_pass_number != -1)
535 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
536 dump_file_name = get_dump_file_name (pass->static_pass_number);
537 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
538 if (dump_file && current_function_decl)
540 const char *dname, *aname;
541 dname = lang_hooks.decl_printable_name (current_function_decl, 2);
542 aname = (IDENTIFIER_POINTER
543 (DECL_ASSEMBLER_NAME (current_function_decl)));
544 fprintf (dump_file, "\n;; Function %s (%s)%s\n\n", dname, aname,
545 cfun->function_frequency == FUNCTION_FREQUENCY_HOT
547 : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
548 ? " (unlikely executed)"
552 if (initializing_dump
553 && graph_dump_format != no_graph
554 && (pass->properties_provided & (PROP_cfg | PROP_rtl))
555 == (PROP_cfg | PROP_rtl))
556 clean_graph_dump_file (dump_file_name);
559 /* If a timevar is present, start it. */
561 timevar_push (pass->tv_id);
569 timevar_pop (pass->tv_id);
572 && (pass->properties_provided & (PROP_cfg | PROP_rtl))
573 == (PROP_cfg | PROP_rtl))
574 print_rtl_with_bb (dump_file, get_insns ());
576 /* Run post-pass cleanup and verification. */
577 todo = pass->todo_flags_finish;
578 last_verified = todo & TODO_verify_all;
580 execute_todo (pass, todo, false);
582 /* Flush and close dump file. */
585 free ((char *) dump_file_name);
586 dump_file_name = NULL;
590 dump_end (pass->static_pass_number, dump_file);
598 execute_pass_list (struct tree_opt_pass *pass)
602 if (execute_one_pass (pass) && pass->sub)
603 execute_pass_list (pass->sub);
609 /* Execute all IPA passes. */
613 execute_pass_list (all_ipa_passes);
617 /* Update recursively all inlined_to pointers of functions
618 inlined into NODE to INLINED_TO. */
620 update_inlined_to_pointers (struct cgraph_node *node,
621 struct cgraph_node *inlined_to)
623 struct cgraph_edge *e;
624 for (e = node->callees; e; e = e->next_callee)
626 if (e->callee->global.inlined_to)
628 e->callee->global.inlined_to = inlined_to;
629 update_inlined_to_pointers (e->callee, inlined_to);
635 /* For functions-as-trees languages, this performs all optimization and
636 compilation for FNDECL. */
639 tree_rest_of_compilation (tree fndecl)
641 location_t saved_loc;
642 struct cgraph_node *saved_node = NULL, *node;
644 timevar_push (TV_EXPAND);
646 gcc_assert (!flag_unit_at_a_time || cgraph_global_info_ready);
648 /* Initialize the RTL code for the function. */
649 current_function_decl = fndecl;
650 saved_loc = input_location;
651 input_location = DECL_SOURCE_LOCATION (fndecl);
652 init_function_start (fndecl);
654 /* Even though we're inside a function body, we still don't want to
655 call expand_expr to calculate the size of a variable-sized array.
656 We haven't necessarily assigned RTL to all variables yet, so it's
657 not safe to try to expand expressions involving them. */
658 cfun->x_dont_save_pending_sizes_p = 1;
659 cfun->after_inlining = true;
661 node = cgraph_node (fndecl);
663 /* We might need the body of this function so that we can expand
664 it inline somewhere else. This means not lowering some constructs
665 such as exception handling. */
666 if (cgraph_preserve_function_body_p (fndecl))
668 if (!flag_unit_at_a_time)
670 struct cgraph_edge *e;
672 saved_node = cgraph_clone_node (node);
673 for (e = saved_node->callees; e; e = e->next_callee)
674 if (!e->inline_failed)
675 cgraph_clone_inlined_nodes (e, true);
677 cfun->saved_static_chain_decl = cfun->static_chain_decl;
678 cfun->saved_tree = save_body (fndecl, &cfun->saved_args,
679 &cfun->saved_static_chain_decl);
682 if (flag_inline_trees)
684 struct cgraph_edge *e;
685 for (e = node->callees; e; e = e->next_callee)
686 if (!e->inline_failed || warn_inline)
690 timevar_push (TV_INTEGRATION);
691 optimize_inline_calls (fndecl);
692 timevar_pop (TV_INTEGRATION);
695 /* We are not going to maintain the cgraph edges up to date.
696 Kill it so it won't confuse us. */
697 while (node->callees)
699 /* In non-unit-at-a-time we must mark all referenced functions as needed.
701 if (node->callees->callee->analyzed && !flag_unit_at_a_time)
702 cgraph_mark_needed_node (node->callees->callee);
703 cgraph_remove_edge (node->callees);
706 /* We are not going to maintain the cgraph edges up to date.
707 Kill it so it won't confuse us. */
708 cgraph_node_remove_callees (node);
711 /* Initialize the default bitmap obstack. */
712 bitmap_obstack_initialize (NULL);
713 bitmap_obstack_initialize (®_obstack); /* FIXME, only at RTL generation*/
715 /* Perform all tree transforms and optimizations. */
716 execute_pass_list (all_passes);
718 bitmap_obstack_release (®_obstack);
720 /* Release the default bitmap obstack. */
721 bitmap_obstack_release (NULL);
723 /* Restore original body if still needed. */
724 if (cfun->saved_tree)
726 DECL_SAVED_TREE (fndecl) = cfun->saved_tree;
727 DECL_ARGUMENTS (fndecl) = cfun->saved_args;
728 cfun->static_chain_decl = cfun->saved_static_chain_decl;
730 /* When not in unit-at-a-time mode, we must preserve out of line copy
731 representing node before inlining. Restore original outgoing edges
732 using clone we created earlier. */
733 if (!flag_unit_at_a_time)
735 struct cgraph_edge *e;
737 cgraph_node_remove_callees (node);
738 node->callees = saved_node->callees;
739 saved_node->callees = NULL;
740 update_inlined_to_pointers (node, node);
741 for (e = node->callees; e; e = e->next_callee)
743 cgraph_remove_node (saved_node);
747 DECL_SAVED_TREE (fndecl) = NULL;
750 /* If requested, warn about function definitions where the function will
751 return a value (usually of some struct or union type) which itself will
752 take up a lot of stack space. */
753 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
755 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
757 if (ret_type && TYPE_SIZE_UNIT (ret_type)
758 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
759 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
762 unsigned int size_as_int
763 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
765 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
766 warning (0, "%Jsize of return value of %qD is %u bytes",
767 fndecl, fndecl, size_as_int);
769 warning (0, "%Jsize of return value of %qD is larger than %wd bytes",
770 fndecl, fndecl, larger_than_size);
774 if (!flag_inline_trees)
776 DECL_SAVED_TREE (fndecl) = NULL;
777 if (DECL_STRUCT_FUNCTION (fndecl) == 0
778 && !cgraph_node (fndecl)->origin)
780 /* Stop pointing to the local nodes about to be freed.
781 But DECL_INITIAL must remain nonzero so we know this
782 was an actual function definition.
783 For a nested function, this is done in c_pop_function_context.
784 If rest_of_compilation set this to 0, leave it 0. */
785 if (DECL_INITIAL (fndecl) != 0)
786 DECL_INITIAL (fndecl) = error_mark_node;
790 input_location = saved_loc;
793 timevar_pop (TV_EXPAND);