re PR testsuite/40567 (Revision 149002 caused many failures)
[official-gcc.git] / gcc / passes.c
blob36ffd22213563f89d9781bf3da4462017c140b97
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This is the top level of cc1/c++.
23 It parses command args, opens files, invokes the various passes
24 in the proper order, and counts the time used by each.
25 Error messages and low-level interface to malloc also handled here. */
27 #include "config.h"
28 #undef FLOAT /* This is for hpux. They should change hpux. */
29 #undef FFS /* Some systems define this in param.h. */
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include <signal.h>
35 #ifdef HAVE_SYS_RESOURCE_H
36 # include <sys/resource.h>
37 #endif
39 #ifdef HAVE_SYS_TIMES_H
40 # include <sys/times.h>
41 #endif
43 #include "line-map.h"
44 #include "input.h"
45 #include "tree.h"
46 #include "rtl.h"
47 #include "tm_p.h"
48 #include "flags.h"
49 #include "insn-attr.h"
50 #include "insn-config.h"
51 #include "insn-flags.h"
52 #include "hard-reg-set.h"
53 #include "recog.h"
54 #include "output.h"
55 #include "except.h"
56 #include "function.h"
57 #include "toplev.h"
58 #include "expr.h"
59 #include "basic-block.h"
60 #include "intl.h"
61 #include "ggc.h"
62 #include "graph.h"
63 #include "regs.h"
64 #include "timevar.h"
65 #include "diagnostic.h"
66 #include "params.h"
67 #include "reload.h"
68 #include "dwarf2asm.h"
69 #include "integrate.h"
70 #include "real.h"
71 #include "debug.h"
72 #include "target.h"
73 #include "langhooks.h"
74 #include "cfglayout.h"
75 #include "cfgloop.h"
76 #include "hosthooks.h"
77 #include "cgraph.h"
78 #include "opts.h"
79 #include "coverage.h"
80 #include "value-prof.h"
81 #include "tree-inline.h"
82 #include "tree-flow.h"
83 #include "tree-pass.h"
84 #include "tree-dump.h"
85 #include "df.h"
86 #include "predict.h"
88 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
89 #include "dwarf2out.h"
90 #endif
92 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
93 #include "dbxout.h"
94 #endif
96 #ifdef SDB_DEBUGGING_INFO
97 #include "sdbout.h"
98 #endif
100 #ifdef XCOFF_DEBUGGING_INFO
101 #include "xcoffout.h" /* Needed for external data
102 declarations for e.g. AIX 4.x. */
103 #endif
105 /* This is used for debugging. It allows the current pass to printed
106 from anywhere in compilation. */
107 struct opt_pass *current_pass;
109 /* Call from anywhere to find out what pass this is. Useful for
110 printing out debugging information deep inside an service
111 routine. */
112 void
113 print_current_pass (FILE *file)
115 if (current_pass)
116 fprintf (file, "current pass = %s (%d)\n",
117 current_pass->name, current_pass->static_pass_number);
118 else
119 fprintf (file, "no current pass.\n");
123 /* Call from the debugger to get the current pass name. */
124 void
125 debug_pass (void)
127 print_current_pass (stderr);
132 /* Global variables used to communicate with passes. */
133 int dump_flags;
134 bool in_gimple_form;
135 bool first_pass_instance;
138 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
139 and TYPE_DECL nodes.
141 This does nothing for local (non-static) variables, unless the
142 variable is a register variable with DECL_ASSEMBLER_NAME set. In
143 that case, or if the variable is not an automatic, it sets up the
144 RTL and outputs any assembler code (label definition, storage
145 allocation and initialization).
147 DECL is the declaration. TOP_LEVEL is nonzero
148 if this declaration is not within a function. */
150 void
151 rest_of_decl_compilation (tree decl,
152 int top_level,
153 int at_end)
155 /* We deferred calling assemble_alias so that we could collect
156 other attributes such as visibility. Emit the alias now. */
158 tree alias;
159 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
160 if (alias)
162 alias = TREE_VALUE (TREE_VALUE (alias));
163 alias = get_identifier (TREE_STRING_POINTER (alias));
164 assemble_alias (decl, alias);
168 /* Can't defer this, because it needs to happen before any
169 later function definitions are processed. */
170 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
171 make_decl_rtl (decl);
173 /* Forward declarations for nested functions are not "external",
174 but we need to treat them as if they were. */
175 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
176 || TREE_CODE (decl) == FUNCTION_DECL)
178 timevar_push (TV_VARCONST);
180 /* Don't output anything when a tentative file-scope definition
181 is seen. But at end of compilation, do output code for them.
183 We do output all variables and rely on
184 callgraph code to defer them except for forward declarations
185 (see gcc.c-torture/compile/920624-1.c) */
186 if ((at_end
187 || !DECL_DEFER_OUTPUT (decl)
188 || DECL_INITIAL (decl))
189 && !DECL_EXTERNAL (decl))
191 if (TREE_CODE (decl) != FUNCTION_DECL)
192 varpool_finalize_decl (decl);
193 else
194 assemble_variable (decl, top_level, at_end, 0);
197 #ifdef ASM_FINISH_DECLARE_OBJECT
198 if (decl == last_assemble_variable_decl)
200 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
201 top_level, at_end);
203 #endif
205 timevar_pop (TV_VARCONST);
207 else if (TREE_CODE (decl) == TYPE_DECL
208 /* Like in rest_of_type_compilation, avoid confusing the debug
209 information machinery when there are errors. */
210 && !(sorrycount || errorcount))
212 timevar_push (TV_SYMOUT);
213 debug_hooks->type_decl (decl, !top_level);
214 timevar_pop (TV_SYMOUT);
217 /* Let cgraph know about the existence of variables. */
218 if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
219 varpool_node (decl);
222 /* Called after finishing a record, union or enumeral type. */
224 void
225 rest_of_type_compilation (tree type, int toplev)
227 /* Avoid confusing the debug information machinery when there are
228 errors. */
229 if (errorcount != 0 || sorrycount != 0)
230 return;
232 timevar_push (TV_SYMOUT);
233 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
234 timevar_pop (TV_SYMOUT);
239 void
240 finish_optimization_passes (void)
242 int i;
243 struct dump_file_info *dfi;
244 char *name;
246 timevar_push (TV_DUMP);
247 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
249 dump_file = dump_begin (pass_profile.pass.static_pass_number, NULL);
250 end_branch_prob ();
251 if (dump_file)
252 dump_end (pass_profile.pass.static_pass_number, dump_file);
255 if (optimize > 0)
257 dump_file = dump_begin (pass_combine.pass.static_pass_number, NULL);
258 if (dump_file)
260 dump_combine_total_stats (dump_file);
261 dump_end (pass_combine.pass.static_pass_number, dump_file);
265 /* Do whatever is necessary to finish printing the graphs. */
266 if (graph_dump_format != no_graph)
267 for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
268 if (dump_initialized_p (i)
269 && (dfi->flags & TDF_GRAPH) != 0
270 && (name = get_dump_file_name (i)) != NULL)
272 finish_graph_dump_file (name);
273 free (name);
276 timevar_pop (TV_DUMP);
279 static bool
280 gate_rest_of_compilation (void)
282 /* Early return if there were errors. We can run afoul of our
283 consistency checks, and there's not really much point in fixing them. */
284 return !(rtl_dump_and_exit || flag_syntax_only || errorcount || sorrycount);
287 struct gimple_opt_pass pass_rest_of_compilation =
290 GIMPLE_PASS,
291 NULL, /* name */
292 gate_rest_of_compilation, /* gate */
293 NULL, /* execute */
294 NULL, /* sub */
295 NULL, /* next */
296 0, /* static_pass_number */
297 TV_REST_OF_COMPILATION, /* tv_id */
298 PROP_rtl, /* properties_required */
299 0, /* properties_provided */
300 0, /* properties_destroyed */
301 0, /* todo_flags_start */
302 TODO_ggc_collect /* todo_flags_finish */
306 static bool
307 gate_postreload (void)
309 return reload_completed;
312 struct rtl_opt_pass pass_postreload =
315 RTL_PASS,
316 NULL, /* name */
317 gate_postreload, /* gate */
318 NULL, /* execute */
319 NULL, /* sub */
320 NULL, /* next */
321 0, /* static_pass_number */
322 TV_NONE, /* tv_id */
323 PROP_rtl, /* properties_required */
324 0, /* properties_provided */
325 0, /* properties_destroyed */
326 0, /* todo_flags_start */
327 TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
333 /* The root of the compilation pass tree, once constructed. */
334 struct opt_pass *all_passes, *all_ipa_passes, *all_lowering_passes;
336 /* A map from static pass id to optimization pass. */
337 struct opt_pass **passes_by_id;
338 int passes_by_id_size;
340 /* Set the static pass number of pass PASS to ID and record that
341 in the mapping from static pass number to pass. */
343 static void
344 set_pass_for_id (int id, struct opt_pass *pass)
346 pass->static_pass_number = id;
347 if (passes_by_id_size <= id)
349 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
350 memset (passes_by_id + passes_by_id_size, 0,
351 (id + 1 - passes_by_id_size) * sizeof (void *));
352 passes_by_id_size = id + 1;
354 passes_by_id[id] = pass;
357 /* Return the pass with the static pass number ID. */
359 struct opt_pass *
360 get_pass_for_id (int id)
362 if (id >= passes_by_id_size)
363 return NULL;
364 return passes_by_id[id];
367 /* Iterate over the pass tree allocating dump file numbers. We want
368 to do this depth first, and independent of whether the pass is
369 enabled or not. */
371 void
372 register_one_dump_file (struct opt_pass *pass)
374 char *dot_name, *flag_name, *glob_name;
375 const char *prefix;
376 char num[10];
377 int flags, id;
379 /* See below in next_pass_1. */
380 num[0] = '\0';
381 if (pass->static_pass_number != -1)
382 sprintf (num, "%d", ((int) pass->static_pass_number < 0
383 ? 1 : pass->static_pass_number));
385 dot_name = concat (".", pass->name, num, NULL);
386 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
387 prefix = "ipa-", flags = TDF_IPA;
388 else if (pass->type == GIMPLE_PASS)
389 prefix = "tree-", flags = TDF_TREE;
390 else
391 prefix = "rtl-", flags = TDF_RTL;
393 flag_name = concat (prefix, pass->name, num, NULL);
394 glob_name = concat (prefix, pass->name, NULL);
395 id = dump_register (dot_name, flag_name, glob_name, flags);
396 set_pass_for_id (id, pass);
399 /* Recursive worker function for register_dump_files. */
401 static int
402 register_dump_files_1 (struct opt_pass *pass, int properties)
406 int new_properties = (properties | pass->properties_provided)
407 & ~pass->properties_destroyed;
409 if (pass->name && pass->name[0] != '*')
410 register_one_dump_file (pass);
412 if (pass->sub)
413 new_properties = register_dump_files_1 (pass->sub, new_properties);
415 /* If we have a gate, combine the properties that we could have with
416 and without the pass being examined. */
417 if (pass->gate)
418 properties &= new_properties;
419 else
420 properties = new_properties;
422 pass = pass->next;
424 while (pass);
426 return properties;
429 /* Register the dump files for the pipeline starting at PASS.
430 PROPERTIES reflects the properties that are guaranteed to be available at
431 the beginning of the pipeline. */
433 static void
434 register_dump_files (struct opt_pass *pass,int properties)
436 pass->properties_required |= properties;
437 register_dump_files_1 (pass, properties);
440 /* Add a pass to the pass list. Duplicate the pass if it's already
441 in the list. */
443 static struct opt_pass **
444 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
446 /* A nonzero static_pass_number indicates that the
447 pass is already in the list. */
448 if (pass->static_pass_number)
450 struct opt_pass *new_pass;
452 new_pass = XNEW (struct opt_pass);
453 memcpy (new_pass, pass, sizeof (*new_pass));
454 new_pass->next = NULL;
456 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
458 /* Indicate to register_dump_files that this pass has duplicates,
459 and so it should rename the dump file. The first instance will
460 be -1, and be number of duplicates = -static_pass_number - 1.
461 Subsequent instances will be > 0 and just the duplicate number. */
462 if (pass->name)
464 pass->static_pass_number -= 1;
465 new_pass->static_pass_number = -pass->static_pass_number;
468 *list = new_pass;
470 else
472 pass->todo_flags_start |= TODO_mark_first_instance;
473 pass->static_pass_number = -1;
474 *list = pass;
477 return &(*list)->next;
482 /* Construct the pass tree. The sequencing of passes is driven by
483 the cgraph routines:
485 cgraph_finalize_compilation_unit ()
486 for each node N in the cgraph
487 cgraph_analyze_function (N)
488 cgraph_lower_function (N) -> all_lowering_passes
490 If we are optimizing, cgraph_optimize is then invoked:
492 cgraph_optimize ()
493 ipa_passes () -> all_ipa_passes
494 cgraph_expand_all_functions ()
495 for each node N in the cgraph
496 cgraph_expand_function (N)
497 tree_rest_of_compilation (DECL (N)) -> all_passes
500 void
501 init_optimization_passes (void)
503 struct opt_pass **p;
505 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &((PASS).pass)))
507 /* All passes needed to lower the function into shape optimizers can
508 operate on. These passes are always run first on the function, but
509 backend might produce already lowered functions that are not processed
510 by these passes. */
511 p = &all_lowering_passes;
512 NEXT_PASS (pass_remove_useless_stmts);
513 NEXT_PASS (pass_mudflap_1);
514 NEXT_PASS (pass_lower_omp);
515 NEXT_PASS (pass_lower_cf);
516 NEXT_PASS (pass_refactor_eh);
517 NEXT_PASS (pass_lower_eh);
518 NEXT_PASS (pass_build_cfg);
519 NEXT_PASS (pass_lower_complex_O0);
520 NEXT_PASS (pass_lower_vector);
521 NEXT_PASS (pass_warn_function_return);
522 NEXT_PASS (pass_build_cgraph_edges);
523 NEXT_PASS (pass_inline_parameters);
524 *p = NULL;
526 /* Interprocedural optimization passes. */
527 p = &all_ipa_passes;
528 NEXT_PASS (pass_ipa_function_and_variable_visibility);
529 NEXT_PASS (pass_ipa_early_inline);
531 struct opt_pass **p = &pass_ipa_early_inline.pass.sub;
532 NEXT_PASS (pass_early_inline);
533 NEXT_PASS (pass_inline_parameters);
534 NEXT_PASS (pass_rebuild_cgraph_edges);
536 NEXT_PASS (pass_early_local_passes);
538 struct opt_pass **p = &pass_early_local_passes.pass.sub;
539 NEXT_PASS (pass_fixup_cfg);
540 NEXT_PASS (pass_tree_profile);
541 NEXT_PASS (pass_cleanup_cfg);
542 NEXT_PASS (pass_init_datastructures);
543 NEXT_PASS (pass_expand_omp);
545 NEXT_PASS (pass_referenced_vars);
546 NEXT_PASS (pass_build_ssa);
547 NEXT_PASS (pass_early_warn_uninitialized);
548 NEXT_PASS (pass_all_early_optimizations);
550 struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
551 NEXT_PASS (pass_rebuild_cgraph_edges);
552 NEXT_PASS (pass_early_inline);
553 NEXT_PASS (pass_remove_cgraph_callee_edges);
554 NEXT_PASS (pass_rename_ssa_copies);
555 NEXT_PASS (pass_ccp);
556 NEXT_PASS (pass_forwprop);
557 /* pass_build_ealias is a dummy pass that ensures that we
558 execute TODO_rebuild_alias at this point. Re-building
559 alias information also rewrites no longer addressed
560 locals into SSA form if possible. */
561 NEXT_PASS (pass_build_ealias);
562 NEXT_PASS (pass_sra_early);
563 NEXT_PASS (pass_copy_prop);
564 NEXT_PASS (pass_merge_phi);
565 NEXT_PASS (pass_cd_dce);
566 NEXT_PASS (pass_tail_recursion);
567 NEXT_PASS (pass_convert_switch);
568 NEXT_PASS (pass_cleanup_eh);
569 NEXT_PASS (pass_profile);
570 NEXT_PASS (pass_local_pure_const);
572 NEXT_PASS (pass_release_ssa_names);
573 NEXT_PASS (pass_rebuild_cgraph_edges);
574 NEXT_PASS (pass_inline_parameters);
576 NEXT_PASS (pass_ipa_increase_alignment);
577 NEXT_PASS (pass_ipa_matrix_reorg);
578 NEXT_PASS (pass_ipa_cp);
579 NEXT_PASS (pass_ipa_inline);
580 NEXT_PASS (pass_ipa_reference);
581 NEXT_PASS (pass_ipa_pure_const);
582 NEXT_PASS (pass_ipa_type_escape);
583 NEXT_PASS (pass_ipa_pta);
584 NEXT_PASS (pass_ipa_struct_reorg);
585 *p = NULL;
587 /* These passes are run after IPA passes on every function that is being
588 output to the assembler file. */
589 p = &all_passes;
590 NEXT_PASS (pass_all_optimizations);
592 struct opt_pass **p = &pass_all_optimizations.pass.sub;
593 NEXT_PASS (pass_remove_cgraph_callee_edges);
594 /* Initial scalar cleanups before alias computation.
595 They ensure memory accesses are not indirect wherever possible. */
596 NEXT_PASS (pass_strip_predict_hints);
597 NEXT_PASS (pass_update_address_taken);
598 NEXT_PASS (pass_rename_ssa_copies);
599 NEXT_PASS (pass_complete_unrolli);
600 NEXT_PASS (pass_ccp);
601 NEXT_PASS (pass_forwprop);
602 NEXT_PASS (pass_call_cdce);
603 /* pass_build_alias is a dummy pass that ensures that we
604 execute TODO_rebuild_alias at this point. Re-building
605 alias information also rewrites no longer addressed
606 locals into SSA form if possible. */
607 NEXT_PASS (pass_build_alias);
608 NEXT_PASS (pass_return_slot);
609 NEXT_PASS (pass_phiprop);
610 NEXT_PASS (pass_fre);
611 NEXT_PASS (pass_copy_prop);
612 NEXT_PASS (pass_merge_phi);
613 NEXT_PASS (pass_vrp);
614 NEXT_PASS (pass_dce);
615 NEXT_PASS (pass_cselim);
616 NEXT_PASS (pass_tree_ifcombine);
617 NEXT_PASS (pass_phiopt);
618 NEXT_PASS (pass_tail_recursion);
619 NEXT_PASS (pass_ch);
620 NEXT_PASS (pass_stdarg);
621 NEXT_PASS (pass_lower_complex);
622 NEXT_PASS (pass_sra);
623 NEXT_PASS (pass_rename_ssa_copies);
624 NEXT_PASS (pass_dominator);
625 /* The only const/copy propagation opportunities left after
626 DOM should be due to degenerate PHI nodes. So rather than
627 run the full propagators, run a specialized pass which
628 only examines PHIs to discover const/copy propagation
629 opportunities. */
630 NEXT_PASS (pass_phi_only_cprop);
631 NEXT_PASS (pass_dse);
632 NEXT_PASS (pass_reassoc);
633 NEXT_PASS (pass_dce);
634 NEXT_PASS (pass_forwprop);
635 NEXT_PASS (pass_phiopt);
636 NEXT_PASS (pass_object_sizes);
637 NEXT_PASS (pass_ccp);
638 NEXT_PASS (pass_copy_prop);
639 NEXT_PASS (pass_fold_builtins);
640 NEXT_PASS (pass_cse_sincos);
641 NEXT_PASS (pass_optimize_bswap);
642 NEXT_PASS (pass_split_crit_edges);
643 NEXT_PASS (pass_pre);
644 NEXT_PASS (pass_sink_code);
645 NEXT_PASS (pass_tree_loop);
647 struct opt_pass **p = &pass_tree_loop.pass.sub;
648 NEXT_PASS (pass_tree_loop_init);
649 NEXT_PASS (pass_copy_prop);
650 NEXT_PASS (pass_dce_loop);
651 NEXT_PASS (pass_lim);
652 NEXT_PASS (pass_tree_unswitch);
653 NEXT_PASS (pass_scev_cprop);
654 NEXT_PASS (pass_empty_loop);
655 NEXT_PASS (pass_record_bounds);
656 NEXT_PASS (pass_check_data_deps);
657 NEXT_PASS (pass_loop_distribution);
658 NEXT_PASS (pass_linear_transform);
659 NEXT_PASS (pass_graphite_transforms);
660 NEXT_PASS (pass_iv_canon);
661 NEXT_PASS (pass_if_conversion);
662 NEXT_PASS (pass_vectorize);
664 struct opt_pass **p = &pass_vectorize.pass.sub;
665 NEXT_PASS (pass_lower_vector_ssa);
666 NEXT_PASS (pass_dce_loop);
668 NEXT_PASS (pass_predcom);
669 NEXT_PASS (pass_complete_unroll);
670 NEXT_PASS (pass_slp_vectorize);
671 NEXT_PASS (pass_parallelize_loops);
672 NEXT_PASS (pass_loop_prefetch);
673 NEXT_PASS (pass_iv_optimize);
674 NEXT_PASS (pass_tree_loop_done);
676 NEXT_PASS (pass_cse_reciprocals);
677 NEXT_PASS (pass_convert_to_rsqrt);
678 NEXT_PASS (pass_reassoc);
679 NEXT_PASS (pass_vrp);
680 NEXT_PASS (pass_dominator);
681 /* The only const/copy propagation opportunities left after
682 DOM should be due to degenerate PHI nodes. So rather than
683 run the full propagators, run a specialized pass which
684 only examines PHIs to discover const/copy propagation
685 opportunities. */
686 NEXT_PASS (pass_phi_only_cprop);
687 NEXT_PASS (pass_cd_dce);
688 NEXT_PASS (pass_tracer);
690 /* FIXME: If DCE is not run before checking for uninitialized uses,
691 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
692 However, this also causes us to misdiagnose cases that should be
693 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
695 To fix the false positives in uninit-5.c, we would have to
696 account for the predicates protecting the set and the use of each
697 variable. Using a representation like Gated Single Assignment
698 may help. */
699 NEXT_PASS (pass_late_warn_uninitialized);
700 NEXT_PASS (pass_dse);
701 NEXT_PASS (pass_forwprop);
702 NEXT_PASS (pass_phiopt);
703 NEXT_PASS (pass_tail_calls);
704 NEXT_PASS (pass_rename_ssa_copies);
705 NEXT_PASS (pass_uncprop);
706 NEXT_PASS (pass_local_pure_const);
708 NEXT_PASS (pass_cleanup_eh);
709 NEXT_PASS (pass_nrv);
710 NEXT_PASS (pass_mudflap_2);
711 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
712 NEXT_PASS (pass_warn_function_noreturn);
714 NEXT_PASS (pass_expand);
716 NEXT_PASS (pass_rest_of_compilation);
718 struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
719 NEXT_PASS (pass_init_function);
720 NEXT_PASS (pass_jump);
721 NEXT_PASS (pass_rtl_eh);
722 NEXT_PASS (pass_initial_value_sets);
723 NEXT_PASS (pass_unshare_all_rtl);
724 NEXT_PASS (pass_instantiate_virtual_regs);
725 NEXT_PASS (pass_into_cfg_layout_mode);
726 NEXT_PASS (pass_jump2);
727 NEXT_PASS (pass_lower_subreg);
728 NEXT_PASS (pass_df_initialize_opt);
729 NEXT_PASS (pass_cse);
730 NEXT_PASS (pass_rtl_fwprop);
731 NEXT_PASS (pass_rtl_cprop);
732 NEXT_PASS (pass_rtl_pre);
733 NEXT_PASS (pass_rtl_hoist);
734 NEXT_PASS (pass_rtl_cprop);
735 NEXT_PASS (pass_rtl_store_motion);
736 NEXT_PASS (pass_cse_after_global_opts);
737 NEXT_PASS (pass_rtl_ifcvt);
738 /* Perform loop optimizations. It might be better to do them a bit
739 sooner, but we want the profile feedback to work more
740 efficiently. */
741 NEXT_PASS (pass_loop2);
743 struct opt_pass **p = &pass_loop2.pass.sub;
744 NEXT_PASS (pass_rtl_loop_init);
745 NEXT_PASS (pass_rtl_move_loop_invariants);
746 NEXT_PASS (pass_rtl_unswitch);
747 NEXT_PASS (pass_rtl_unroll_and_peel_loops);
748 NEXT_PASS (pass_rtl_doloop);
749 NEXT_PASS (pass_rtl_loop_done);
750 *p = NULL;
752 NEXT_PASS (pass_web);
753 NEXT_PASS (pass_rtl_cprop);
754 NEXT_PASS (pass_cse2);
755 NEXT_PASS (pass_rtl_dse1);
756 NEXT_PASS (pass_rtl_fwprop_addr);
757 NEXT_PASS (pass_reginfo_init);
758 NEXT_PASS (pass_inc_dec);
759 NEXT_PASS (pass_initialize_regs);
760 NEXT_PASS (pass_ud_rtl_dce);
761 NEXT_PASS (pass_combine);
762 NEXT_PASS (pass_if_after_combine);
763 NEXT_PASS (pass_partition_blocks);
764 NEXT_PASS (pass_regmove);
765 NEXT_PASS (pass_outof_cfg_layout_mode);
766 NEXT_PASS (pass_split_all_insns);
767 NEXT_PASS (pass_lower_subreg2);
768 NEXT_PASS (pass_df_initialize_no_opt);
769 NEXT_PASS (pass_stack_ptr_mod);
770 NEXT_PASS (pass_mode_switching);
771 NEXT_PASS (pass_match_asm_constraints);
772 NEXT_PASS (pass_sms);
773 NEXT_PASS (pass_sched);
774 NEXT_PASS (pass_subregs_of_mode_init);
775 NEXT_PASS (pass_ira);
776 NEXT_PASS (pass_subregs_of_mode_finish);
777 NEXT_PASS (pass_postreload);
779 struct opt_pass **p = &pass_postreload.pass.sub;
780 NEXT_PASS (pass_postreload_cse);
781 NEXT_PASS (pass_gcse2);
782 NEXT_PASS (pass_split_after_reload);
783 NEXT_PASS (pass_branch_target_load_optimize1);
784 NEXT_PASS (pass_thread_prologue_and_epilogue);
785 NEXT_PASS (pass_rtl_dse2);
786 NEXT_PASS (pass_stack_adjustments);
787 NEXT_PASS (pass_peephole2);
788 NEXT_PASS (pass_if_after_reload);
789 NEXT_PASS (pass_regrename);
790 NEXT_PASS (pass_cprop_hardreg);
791 NEXT_PASS (pass_fast_rtl_dce);
792 NEXT_PASS (pass_reorder_blocks);
793 NEXT_PASS (pass_branch_target_load_optimize2);
794 NEXT_PASS (pass_leaf_regs);
795 NEXT_PASS (pass_split_before_sched2);
796 NEXT_PASS (pass_sched2);
797 NEXT_PASS (pass_stack_regs);
799 struct opt_pass **p = &pass_stack_regs.pass.sub;
800 NEXT_PASS (pass_split_before_regstack);
801 NEXT_PASS (pass_stack_regs_run);
803 NEXT_PASS (pass_compute_alignments);
804 NEXT_PASS (pass_duplicate_computed_gotos);
805 NEXT_PASS (pass_variable_tracking);
806 NEXT_PASS (pass_free_cfg);
807 NEXT_PASS (pass_machine_reorg);
808 NEXT_PASS (pass_cleanup_barriers);
809 NEXT_PASS (pass_delay_slots);
810 NEXT_PASS (pass_split_for_shorten_branches);
811 NEXT_PASS (pass_convert_to_eh_region_ranges);
812 NEXT_PASS (pass_shorten_branches);
813 NEXT_PASS (pass_set_nothrow_function_flags);
814 NEXT_PASS (pass_final);
816 NEXT_PASS (pass_df_finish);
818 NEXT_PASS (pass_clean_state);
819 *p = NULL;
821 #undef NEXT_PASS
823 /* Register the passes with the tree dump code. */
824 register_dump_files (all_lowering_passes, PROP_gimple_any);
825 all_lowering_passes->todo_flags_start |= TODO_set_props;
826 register_dump_files (all_ipa_passes,
827 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
828 | PROP_cfg);
829 register_dump_files (all_passes,
830 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
831 | PROP_cfg);
834 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
835 function CALLBACK for every function in the call graph. Otherwise,
836 call CALLBACK on the current function. */
838 static void
839 do_per_function (void (*callback) (void *data), void *data)
841 if (current_function_decl)
842 callback (data);
843 else
845 struct cgraph_node *node;
846 for (node = cgraph_nodes; node; node = node->next)
847 if (node->analyzed && gimple_has_body_p (node->decl)
848 && (!node->clone_of || node->decl != node->clone_of->decl))
850 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
851 current_function_decl = node->decl;
852 callback (data);
853 free_dominance_info (CDI_DOMINATORS);
854 free_dominance_info (CDI_POST_DOMINATORS);
855 current_function_decl = NULL;
856 pop_cfun ();
857 ggc_collect ();
862 /* Because inlining might remove no-longer reachable nodes, we need to
863 keep the array visible to garbage collector to avoid reading collected
864 out nodes. */
865 static int nnodes;
866 static GTY ((length ("nnodes"))) struct cgraph_node **order;
868 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
869 function CALLBACK for every function in the call graph. Otherwise,
870 call CALLBACK on the current function. */
872 static void
873 do_per_function_toporder (void (*callback) (void *data), void *data)
875 int i;
877 if (current_function_decl)
878 callback (data);
879 else
881 gcc_assert (!order);
882 order = GGC_NEWVEC (struct cgraph_node *, cgraph_n_nodes);
883 nnodes = cgraph_postorder (order);
884 for (i = nnodes - 1; i >= 0; i--)
885 order[i]->process = 1;
886 for (i = nnodes - 1; i >= 0; i--)
888 struct cgraph_node *node = order[i];
890 /* Allow possibly removed nodes to be garbage collected. */
891 order[i] = NULL;
892 node->process = 0;
893 if (node->analyzed && (node->needed || node->reachable))
895 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
896 current_function_decl = node->decl;
897 callback (data);
898 free_dominance_info (CDI_DOMINATORS);
899 free_dominance_info (CDI_POST_DOMINATORS);
900 current_function_decl = NULL;
901 pop_cfun ();
902 ggc_collect ();
906 ggc_free (order);
907 order = NULL;
908 nnodes = 0;
911 /* Perform all TODO actions that ought to be done on each function. */
913 static void
914 execute_function_todo (void *data)
916 unsigned int flags = (size_t)data;
917 if (cfun->curr_properties & PROP_ssa)
918 flags |= TODO_verify_ssa;
919 flags &= ~cfun->last_verified;
920 if (!flags)
921 return;
923 statistics_fini_pass ();
925 /* Always cleanup the CFG before trying to update SSA. */
926 if (flags & TODO_cleanup_cfg)
928 bool cleanup = cleanup_tree_cfg ();
930 if (cleanup && (cfun->curr_properties & PROP_ssa))
931 flags |= TODO_remove_unused_locals;
933 /* When cleanup_tree_cfg merges consecutive blocks, it may
934 perform some simplistic propagation when removing single
935 valued PHI nodes. This propagation may, in turn, cause the
936 SSA form to become out-of-date (see PR 22037). So, even
937 if the parent pass had not scheduled an SSA update, we may
938 still need to do one. */
939 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
940 flags |= TODO_update_ssa;
943 if (flags & TODO_update_ssa_any)
945 unsigned update_flags = flags & TODO_update_ssa_any;
946 update_ssa (update_flags);
947 cfun->last_verified &= ~TODO_verify_ssa;
950 if (flags & TODO_update_address_taken)
951 execute_update_addresses_taken (true);
953 if (flags & TODO_rebuild_alias)
955 if (!(flags & TODO_update_address_taken))
956 execute_update_addresses_taken (true);
957 compute_may_aliases ();
960 if (flags & TODO_remove_unused_locals)
961 remove_unused_locals ();
963 if ((flags & TODO_dump_func) && dump_file && current_function_decl)
965 if (cfun->curr_properties & PROP_trees)
966 dump_function_to_file (current_function_decl, dump_file, dump_flags);
967 else
969 if (dump_flags & TDF_SLIM)
970 print_rtl_slim_with_bb (dump_file, get_insns (), dump_flags);
971 else if ((cfun->curr_properties & PROP_cfg)
972 && (dump_flags & TDF_BLOCKS))
973 print_rtl_with_bb (dump_file, get_insns ());
974 else
975 print_rtl (dump_file, get_insns ());
977 if ((cfun->curr_properties & PROP_cfg)
978 && graph_dump_format != no_graph
979 && (dump_flags & TDF_GRAPH))
980 print_rtl_graph_with_bb (dump_file_name, get_insns ());
983 /* Flush the file. If verification fails, we won't be able to
984 close the file before aborting. */
985 fflush (dump_file);
988 if (flags & TODO_rebuild_frequencies)
990 if (profile_status == PROFILE_GUESSED)
992 loop_optimizer_init (0);
993 add_noreturn_fake_exit_edges ();
994 mark_irreducible_loops ();
995 connect_infinite_loops_to_exit ();
996 estimate_bb_frequencies ();
997 remove_fake_exit_edges ();
998 loop_optimizer_finalize ();
1000 else if (profile_status == PROFILE_READ)
1001 counts_to_freqs ();
1002 else
1003 gcc_unreachable ();
1006 #if defined ENABLE_CHECKING
1007 if (flags & TODO_verify_ssa)
1008 verify_ssa (true);
1009 if (flags & TODO_verify_flow)
1010 verify_flow_info ();
1011 if (flags & TODO_verify_stmts)
1012 verify_stmts ();
1013 if (flags & TODO_verify_loops)
1014 verify_loop_closed_ssa ();
1015 if (flags & TODO_verify_rtl_sharing)
1016 verify_rtl_sharing ();
1017 #endif
1019 cfun->last_verified = flags & TODO_verify_all;
1022 /* Perform all TODO actions. */
1023 static void
1024 execute_todo (unsigned int flags)
1026 #if defined ENABLE_CHECKING
1027 if (cfun
1028 && need_ssa_update_p (cfun))
1029 gcc_assert (flags & TODO_update_ssa_any);
1030 #endif
1032 /* Inform the pass whether it is the first time it is run. */
1033 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1035 do_per_function (execute_function_todo, (void *)(size_t) flags);
1037 /* Always remove functions just as before inlining: IPA passes might be
1038 interested to see bodies of extern inline functions that are not inlined
1039 to analyze side effects. The full removal is done just at the end
1040 of IPA pass queue. */
1041 if (flags & TODO_remove_functions)
1043 gcc_assert (!cfun);
1044 cgraph_remove_unreachable_nodes (true, dump_file);
1047 if ((flags & TODO_dump_cgraph) && dump_file && !current_function_decl)
1049 gcc_assert (!cfun);
1050 dump_cgraph (dump_file);
1051 /* Flush the file. If verification fails, we won't be able to
1052 close the file before aborting. */
1053 fflush (dump_file);
1056 if (flags & TODO_ggc_collect)
1057 ggc_collect ();
1059 /* Now that the dumping has been done, we can get rid of the optional
1060 df problems. */
1061 if (flags & TODO_df_finish)
1062 df_finish_pass ((flags & TODO_df_verify) != 0);
1065 /* Verify invariants that should hold between passes. This is a place
1066 to put simple sanity checks. */
1068 static void
1069 verify_interpass_invariants (void)
1071 #ifdef ENABLE_CHECKING
1072 gcc_assert (!fold_deferring_overflow_warnings_p ());
1073 #endif
1076 /* Clear the last verified flag. */
1078 static void
1079 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1081 cfun->last_verified = 0;
1084 /* Helper function. Verify that the properties has been turn into the
1085 properties expected by the pass. */
1087 #ifdef ENABLE_CHECKING
1088 static void
1089 verify_curr_properties (void *data)
1091 unsigned int props = (size_t)data;
1092 gcc_assert ((cfun->curr_properties & props) == props);
1094 #endif
1096 /* Initialize pass dump file. */
1098 static bool
1099 pass_init_dump_file (struct opt_pass *pass)
1101 /* If a dump file name is present, open it if enabled. */
1102 if (pass->static_pass_number != -1)
1104 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
1105 dump_file_name = get_dump_file_name (pass->static_pass_number);
1106 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
1107 if (dump_file && current_function_decl)
1109 const char *dname, *aname;
1110 dname = lang_hooks.decl_printable_name (current_function_decl, 2);
1111 aname = (IDENTIFIER_POINTER
1112 (DECL_ASSEMBLER_NAME (current_function_decl)));
1113 fprintf (dump_file, "\n;; Function %s (%s)%s\n\n", dname, aname,
1114 cfun->function_frequency == FUNCTION_FREQUENCY_HOT
1115 ? " (hot)"
1116 : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
1117 ? " (unlikely executed)"
1118 : "");
1120 return initializing_dump;
1122 else
1123 return false;
1126 /* Flush PASS dump file. */
1128 static void
1129 pass_fini_dump_file (struct opt_pass *pass)
1131 /* Flush and close dump file. */
1132 if (dump_file_name)
1134 free (CONST_CAST (char *, dump_file_name));
1135 dump_file_name = NULL;
1138 if (dump_file)
1140 dump_end (pass->static_pass_number, dump_file);
1141 dump_file = NULL;
1145 /* After executing the pass, apply expected changes to the function
1146 properties. */
1148 static void
1149 update_properties_after_pass (void *data)
1151 struct opt_pass *pass = (struct opt_pass *) data;
1152 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1153 & ~pass->properties_destroyed;
1156 /* Schedule IPA transform pass DATA for CFUN. */
1158 static void
1159 add_ipa_transform_pass (void *data)
1161 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) data;
1162 VEC_safe_push (ipa_opt_pass, heap, cfun->ipa_transforms_to_apply, ipa_pass);
1165 /* Execute summary generation for all of the passes in IPA_PASS. */
1167 static void
1168 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1170 while (ipa_pass)
1172 struct opt_pass *pass = &ipa_pass->pass;
1174 /* Execute all of the IPA_PASSes in the list. */
1175 if (ipa_pass->pass.type == IPA_PASS
1176 && (!pass->gate || pass->gate ()))
1178 pass_init_dump_file (pass);
1179 ipa_pass->generate_summary ();
1180 pass_fini_dump_file (pass);
1182 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
1186 /* Execute IPA_PASS function transform on NODE. */
1188 static void
1189 execute_one_ipa_transform_pass (struct cgraph_node *node,
1190 struct ipa_opt_pass_d *ipa_pass)
1192 struct opt_pass *pass = &ipa_pass->pass;
1193 unsigned int todo_after = 0;
1195 current_pass = pass;
1196 if (!ipa_pass->function_transform)
1197 return;
1199 /* Note that the folders should only create gimple expressions.
1200 This is a hack until the new folder is ready. */
1201 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1203 pass_init_dump_file (pass);
1205 /* Run pre-pass verification. */
1206 execute_todo (ipa_pass->function_transform_todo_flags_start);
1208 /* If a timevar is present, start it. */
1209 if (pass->tv_id != TV_NONE)
1210 timevar_push (pass->tv_id);
1212 /* Do it! */
1213 todo_after = ipa_pass->function_transform (node);
1215 /* Stop timevar. */
1216 if (pass->tv_id != TV_NONE)
1217 timevar_pop (pass->tv_id);
1219 /* Run post-pass cleanup and verification. */
1220 execute_todo (todo_after);
1221 verify_interpass_invariants ();
1223 pass_fini_dump_file (pass);
1225 current_pass = NULL;
1228 static bool
1229 execute_one_pass (struct opt_pass *pass)
1231 bool initializing_dump;
1232 unsigned int todo_after = 0;
1234 /* IPA passes are executed on whole program, so cfun should be NULL.
1235 Other passes need function context set. */
1236 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
1237 gcc_assert (!cfun && !current_function_decl);
1238 else
1239 gcc_assert (cfun && current_function_decl);
1241 if (cfun && cfun->ipa_transforms_to_apply)
1243 unsigned int i;
1244 struct cgraph_node *node = cgraph_node (current_function_decl);
1246 for (i = 0; i < VEC_length (ipa_opt_pass, cfun->ipa_transforms_to_apply);
1247 i++)
1248 execute_one_ipa_transform_pass (node,
1249 VEC_index (ipa_opt_pass,
1250 cfun->ipa_transforms_to_apply,
1251 i));
1252 VEC_free (ipa_opt_pass, heap, cfun->ipa_transforms_to_apply);
1253 cfun->ipa_transforms_to_apply = NULL;
1256 current_pass = pass;
1258 /* See if we're supposed to run this pass. */
1259 if (pass->gate && !pass->gate ())
1260 return false;
1262 if (!quiet_flag && !cfun)
1263 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
1265 if (pass->todo_flags_start & TODO_set_props)
1266 cfun->curr_properties = pass->properties_required;
1268 /* Note that the folders should only create gimple expressions.
1269 This is a hack until the new folder is ready. */
1270 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1272 initializing_dump = pass_init_dump_file (pass);
1274 /* Run pre-pass verification. */
1275 execute_todo (pass->todo_flags_start);
1277 #ifdef ENABLE_CHECKING
1278 do_per_function (verify_curr_properties,
1279 (void *)(size_t)pass->properties_required);
1280 #endif
1282 /* If a timevar is present, start it. */
1283 if (pass->tv_id != TV_NONE)
1284 timevar_push (pass->tv_id);
1286 /* Do it! */
1287 if (pass->execute)
1289 todo_after = pass->execute ();
1290 do_per_function (clear_last_verified, NULL);
1293 /* Stop timevar. */
1294 if (pass->tv_id != TV_NONE)
1295 timevar_pop (pass->tv_id);
1297 do_per_function (update_properties_after_pass, pass);
1299 if (initializing_dump
1300 && dump_file
1301 && graph_dump_format != no_graph
1302 && cfun
1303 && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
1304 == (PROP_cfg | PROP_rtl))
1306 get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
1307 dump_flags |= TDF_GRAPH;
1308 clean_graph_dump_file (dump_file_name);
1311 /* Run post-pass cleanup and verification. */
1312 execute_todo (todo_after | pass->todo_flags_finish);
1313 verify_interpass_invariants ();
1314 if (pass->type == IPA_PASS)
1315 do_per_function (add_ipa_transform_pass, pass);
1317 if (!current_function_decl)
1318 cgraph_process_new_functions ();
1320 pass_fini_dump_file (pass);
1322 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
1323 gcc_assert (!(cfun->curr_properties & PROP_trees)
1324 || pass->type != RTL_PASS);
1326 current_pass = NULL;
1328 return true;
1331 void
1332 execute_pass_list (struct opt_pass *pass)
1336 gcc_assert (pass->type == GIMPLE_PASS
1337 || pass->type == RTL_PASS);
1338 if (execute_one_pass (pass) && pass->sub)
1339 execute_pass_list (pass->sub);
1340 pass = pass->next;
1342 while (pass);
1345 /* Same as execute_pass_list but assume that subpasses of IPA passes
1346 are local passes. */
1347 void
1348 execute_ipa_pass_list (struct opt_pass *pass)
1350 bool summaries_generated = false;
1353 gcc_assert (!current_function_decl);
1354 gcc_assert (!cfun);
1355 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1356 if (pass->type == IPA_PASS && (!pass->gate || pass->gate ()))
1358 if (!summaries_generated)
1360 if (!quiet_flag && !cfun)
1361 fprintf (stderr, " <summary generate>");
1362 execute_ipa_summary_passes ((struct ipa_opt_pass_d *) pass);
1364 summaries_generated = true;
1366 if (execute_one_pass (pass) && pass->sub)
1368 if (pass->sub->type == GIMPLE_PASS)
1369 do_per_function_toporder ((void (*)(void *))execute_pass_list,
1370 pass->sub);
1371 else if (pass->sub->type == SIMPLE_IPA_PASS
1372 || pass->sub->type == IPA_PASS)
1373 execute_ipa_pass_list (pass->sub);
1374 else
1375 gcc_unreachable ();
1377 if (!current_function_decl)
1378 cgraph_process_new_functions ();
1379 pass = pass->next;
1381 while (pass);
1384 /* Called by local passes to see if function is called by already processed nodes.
1385 Because we process nodes in topological order, this means that function is
1386 in recursive cycle or we introduced new direct calls. */
1387 bool
1388 function_called_by_processed_nodes_p (void)
1390 struct cgraph_edge *e;
1391 for (e = cgraph_node (current_function_decl)->callers; e; e = e->next_caller)
1393 if (e->caller->decl == current_function_decl)
1394 continue;
1395 if (!e->caller->analyzed || (!e->caller->needed && !e->caller->reachable))
1396 continue;
1397 if (TREE_ASM_WRITTEN (e->caller->decl))
1398 continue;
1399 if (!e->caller->process && !e->caller->global.inlined_to)
1400 break;
1402 if (dump_file && e)
1404 fprintf (dump_file, "Already processed call to:\n");
1405 dump_cgraph_node (dump_file, e->caller);
1407 return e != NULL;
1410 #include "gt-passes.h"