2015-10-28 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / passes.c
blob43dd4e0840763ef0ac0960f4bf1a863fb0c01a08
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This is the top level of cc1/c++.
21 It parses command args, opens files, invokes the various passes
22 in the proper order, and counts the time used by each.
23 Error messages and low-level interface to malloc also handled here. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "cfghooks.h"
30 #include "tree.h"
31 #include "gimple.h"
32 #include "rtl.h"
33 #include "df.h"
34 #include "ssa.h"
35 #include "alias.h"
36 #include "fold-const.h"
37 #include "varasm.h"
38 #include "tm_p.h"
39 #include "flags.h"
40 #include "insn-attr.h"
41 #include "insn-config.h"
42 #include "insn-flags.h"
43 #include "recog.h"
44 #include "output.h"
45 #include "except.h"
46 #include "toplev.h"
47 #include "expmed.h"
48 #include "dojump.h"
49 #include "explow.h"
50 #include "calls.h"
51 #include "emit-rtl.h"
52 #include "stmt.h"
53 #include "expr.h"
54 #include "intl.h"
55 #include "graph.h"
56 #include "regs.h"
57 #include "diagnostic-core.h"
58 #include "params.h"
59 #include "reload.h"
60 #include "debug.h"
61 #include "target.h"
62 #include "langhooks.h"
63 #include "cfgloop.h"
64 #include "hosthooks.h"
65 #include "opts.h"
66 #include "coverage.h"
67 #include "value-prof.h"
68 #include "tree-inline.h"
69 #include "internal-fn.h"
70 #include "tree-cfg.h"
71 #include "tree-ssa-loop-manip.h"
72 #include "tree-into-ssa.h"
73 #include "tree-dfa.h"
74 #include "tree-ssa.h"
75 #include "tree-pass.h"
76 #include "tree-dump.h"
77 #include "cgraph.h"
78 #include "lto-streamer.h"
79 #include "plugin.h"
80 #include "ipa-utils.h"
81 #include "tree-pretty-print.h" /* for dump_function_header */
82 #include "context.h"
83 #include "pass_manager.h"
84 #include "cfgrtl.h"
85 #include "tree-ssa-live.h" /* For remove_unused_locals. */
86 #include "tree-cfgcleanup.h"
87 #include "tree-ssanames.h"
89 using namespace gcc;
91 /* This is used for debugging. It allows the current pass to printed
92 from anywhere in compilation.
93 The variable current_pass is also used for statistics and plugins. */
94 opt_pass *current_pass;
96 static void register_pass_name (opt_pass *, const char *);
98 /* Most passes are single-instance (within their context) and thus don't
99 need to implement cloning, but passes that support multiple instances
100 *must* provide their own implementation of the clone method.
102 Handle this by providing a default implemenation, but make it a fatal
103 error to call it. */
105 opt_pass *
106 opt_pass::clone ()
108 internal_error ("pass %s does not support cloning", name);
111 bool
112 opt_pass::gate (function *)
114 return true;
117 unsigned int
118 opt_pass::execute (function *)
120 return 0;
123 opt_pass::opt_pass (const pass_data &data, context *ctxt)
124 : pass_data (data),
125 sub (NULL),
126 next (NULL),
127 static_pass_number (0),
128 m_ctxt (ctxt)
133 void
134 pass_manager::execute_early_local_passes ()
136 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
137 if (flag_check_pointer_bounds)
138 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
139 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
142 unsigned int
143 pass_manager::execute_pass_mode_switching ()
145 return pass_mode_switching_1->execute (cfun);
149 /* Call from anywhere to find out what pass this is. Useful for
150 printing out debugging information deep inside an service
151 routine. */
152 void
153 print_current_pass (FILE *file)
155 if (current_pass)
156 fprintf (file, "current pass = %s (%d)\n",
157 current_pass->name, current_pass->static_pass_number);
158 else
159 fprintf (file, "no current pass.\n");
163 /* Call from the debugger to get the current pass name. */
164 DEBUG_FUNCTION void
165 debug_pass (void)
167 print_current_pass (stderr);
172 /* Global variables used to communicate with passes. */
173 bool in_gimple_form;
174 bool first_pass_instance;
177 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
178 and TYPE_DECL nodes.
180 This does nothing for local (non-static) variables, unless the
181 variable is a register variable with DECL_ASSEMBLER_NAME set. In
182 that case, or if the variable is not an automatic, it sets up the
183 RTL and outputs any assembler code (label definition, storage
184 allocation and initialization).
186 DECL is the declaration. TOP_LEVEL is nonzero
187 if this declaration is not within a function. */
189 void
190 rest_of_decl_compilation (tree decl,
191 int top_level,
192 int at_end)
194 bool finalize = true;
196 /* We deferred calling assemble_alias so that we could collect
197 other attributes such as visibility. Emit the alias now. */
198 if (!in_lto_p)
200 tree alias;
201 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
202 if (alias)
204 alias = TREE_VALUE (TREE_VALUE (alias));
205 alias = get_identifier (TREE_STRING_POINTER (alias));
206 /* A quirk of the initial implementation of aliases required that the
207 user add "extern" to all of them. Which is silly, but now
208 historical. Do note that the symbol is in fact locally defined. */
209 DECL_EXTERNAL (decl) = 0;
210 TREE_STATIC (decl) = 1;
211 assemble_alias (decl, alias);
212 finalize = false;
216 /* Can't defer this, because it needs to happen before any
217 later function definitions are processed. */
218 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
219 make_decl_rtl (decl);
221 /* Forward declarations for nested functions are not "external",
222 but we need to treat them as if they were. */
223 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
224 || TREE_CODE (decl) == FUNCTION_DECL)
226 timevar_push (TV_VARCONST);
228 /* Don't output anything when a tentative file-scope definition
229 is seen. But at end of compilation, do output code for them.
231 We do output all variables and rely on
232 callgraph code to defer them except for forward declarations
233 (see gcc.c-torture/compile/920624-1.c) */
234 if ((at_end
235 || !DECL_DEFER_OUTPUT (decl)
236 || DECL_INITIAL (decl))
237 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
238 && !DECL_EXTERNAL (decl))
240 /* When reading LTO unit, we also read varpool, so do not
241 rebuild it. */
242 if (in_lto_p && !at_end)
244 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
245 varpool_node::finalize_decl (decl);
248 #ifdef ASM_FINISH_DECLARE_OBJECT
249 if (decl == last_assemble_variable_decl)
251 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
252 top_level, at_end);
254 #endif
256 /* Now that we have activated any function-specific attributes
257 that might affect function decl, particularly align, relayout it. */
258 if (TREE_CODE (decl) == FUNCTION_DECL)
259 targetm.target_option.relayout_function (decl);
261 timevar_pop (TV_VARCONST);
263 else if (TREE_CODE (decl) == TYPE_DECL
264 /* Like in rest_of_type_compilation, avoid confusing the debug
265 information machinery when there are errors. */
266 && !seen_error ())
268 timevar_push (TV_SYMOUT);
269 debug_hooks->type_decl (decl, !top_level);
270 timevar_pop (TV_SYMOUT);
273 /* Let cgraph know about the existence of variables. */
274 if (in_lto_p && !at_end)
276 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
277 && TREE_STATIC (decl))
278 varpool_node::get_create (decl);
280 /* Generate early debug for global variables. Any local variables will
281 be handled by either handling reachable functions from
282 finalize_compilation_unit (and by consequence, locally scoped
283 symbols), or by rest_of_type_compilation below.
285 Also, pick up function prototypes, which will be mostly ignored
286 by the different early_global_decl() hooks, but will at least be
287 used by Go's hijack of the debug_hooks to implement
288 -fdump-go-spec. */
289 if (!in_lto_p
290 && (TREE_CODE (decl) != FUNCTION_DECL
291 /* This will pick up function prototypes with no bodies,
292 which are not visible in finalize_compilation_unit()
293 while iterating with FOR_EACH_*_FUNCTION through the
294 symbol table. */
295 || !DECL_SAVED_TREE (decl))
297 /* We need to check both decl_function_context and
298 current_function_decl here to make sure local extern
299 declarations end up with the correct context.
301 For local extern declarations, decl_function_context is
302 empty, but current_function_decl is set to the function where
303 the extern was declared . Without the check for
304 !current_function_decl below, the local extern ends up
305 incorrectly with a top-level context.
307 For example:
309 namespace S
315 int i = 42;
317 extern int i; // Local extern declaration.
318 return i;
324 && !decl_function_context (decl)
325 && !current_function_decl
326 && DECL_SOURCE_LOCATION (decl) != BUILTINS_LOCATION
327 && (!decl_type_context (decl)
328 /* If we created a varpool node for the decl make sure to
329 call early_global_decl. Otherwise we miss changes
330 introduced by member definitions like
331 struct A { static int staticdatamember; };
332 int A::staticdatamember;
333 and thus have incomplete early debug and late debug
334 called from varpool node removal fails to handle it
335 properly. */
336 || (finalize
337 && TREE_CODE (decl) == VAR_DECL
338 && TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
339 /* Avoid confusing the debug information machinery when there are
340 errors. */
341 && !seen_error ())
342 (*debug_hooks->early_global_decl) (decl);
345 /* Called after finishing a record, union or enumeral type. */
347 void
348 rest_of_type_compilation (tree type, int toplev)
350 /* Avoid confusing the debug information machinery when there are
351 errors. */
352 if (seen_error ())
353 return;
355 timevar_push (TV_SYMOUT);
356 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
357 timevar_pop (TV_SYMOUT);
362 void
363 pass_manager::
364 finish_optimization_passes (void)
366 int i;
367 struct dump_file_info *dfi;
368 char *name;
369 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
371 timevar_push (TV_DUMP);
372 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
374 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
375 end_branch_prob ();
376 dumps->dump_finish (pass_profile_1->static_pass_number);
379 if (optimize > 0)
381 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
382 print_combine_total_stats ();
383 dumps->dump_finish (pass_profile_1->static_pass_number);
386 /* Do whatever is necessary to finish printing the graphs. */
387 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
388 if (dumps->dump_initialized_p (i)
389 && (dfi->pflags & TDF_GRAPH) != 0
390 && (name = dumps->get_dump_file_name (i)) != NULL)
392 finish_graph_dump_file (name);
393 free (name);
396 timevar_pop (TV_DUMP);
399 static unsigned int
400 execute_build_ssa_passes (void)
402 /* Once this pass (and its sub-passes) are complete, all functions
403 will be in SSA form. Technically this state change is happening
404 a tad early, since the sub-passes have not yet run, but since
405 none of the sub-passes are IPA passes and do not create new
406 functions, this is ok. We're setting this value for the benefit
407 of IPA passes that follow. */
408 if (symtab->state < IPA_SSA)
409 symtab->state = IPA_SSA;
410 return 0;
413 namespace {
415 const pass_data pass_data_build_ssa_passes =
417 SIMPLE_IPA_PASS, /* type */
418 "build_ssa_passes", /* name */
419 OPTGROUP_NONE, /* optinfo_flags */
420 TV_EARLY_LOCAL, /* tv_id */
421 0, /* properties_required */
422 0, /* properties_provided */
423 0, /* properties_destroyed */
424 0, /* todo_flags_start */
425 /* todo_flags_finish is executed before subpases. For this reason
426 it makes no sense to remove unreachable functions here. */
427 0, /* todo_flags_finish */
430 class pass_build_ssa_passes : public simple_ipa_opt_pass
432 public:
433 pass_build_ssa_passes (gcc::context *ctxt)
434 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
437 /* opt_pass methods: */
438 virtual bool gate (function *)
440 /* Don't bother doing anything if the program has errors. */
441 return (!seen_error () && !in_lto_p);
444 virtual unsigned int execute (function *)
446 return execute_build_ssa_passes ();
449 }; // class pass_build_ssa_passes
451 const pass_data pass_data_chkp_instrumentation_passes =
453 SIMPLE_IPA_PASS, /* type */
454 "chkp_passes", /* name */
455 OPTGROUP_NONE, /* optinfo_flags */
456 TV_NONE, /* tv_id */
457 0, /* properties_required */
458 0, /* properties_provided */
459 0, /* properties_destroyed */
460 0, /* todo_flags_start */
461 0, /* todo_flags_finish */
464 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
466 public:
467 pass_chkp_instrumentation_passes (gcc::context *ctxt)
468 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
471 /* opt_pass methods: */
472 virtual bool gate (function *)
474 /* Don't bother doing anything if the program has errors. */
475 return (flag_check_pointer_bounds
476 && !seen_error () && !in_lto_p);
479 }; // class pass_chkp_instrumentation_passes
481 const pass_data pass_data_local_optimization_passes =
483 SIMPLE_IPA_PASS, /* type */
484 "opt_local_passes", /* name */
485 OPTGROUP_NONE, /* optinfo_flags */
486 TV_NONE, /* tv_id */
487 0, /* properties_required */
488 0, /* properties_provided */
489 0, /* properties_destroyed */
490 0, /* todo_flags_start */
491 0, /* todo_flags_finish */
494 class pass_local_optimization_passes : public simple_ipa_opt_pass
496 public:
497 pass_local_optimization_passes (gcc::context *ctxt)
498 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
501 /* opt_pass methods: */
502 virtual bool gate (function *)
504 /* Don't bother doing anything if the program has errors. */
505 return (!seen_error () && !in_lto_p);
508 }; // class pass_local_optimization_passes
510 } // anon namespace
512 simple_ipa_opt_pass *
513 make_pass_build_ssa_passes (gcc::context *ctxt)
515 return new pass_build_ssa_passes (ctxt);
518 simple_ipa_opt_pass *
519 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
521 return new pass_chkp_instrumentation_passes (ctxt);
524 simple_ipa_opt_pass *
525 make_pass_local_optimization_passes (gcc::context *ctxt)
527 return new pass_local_optimization_passes (ctxt);
530 namespace {
532 const pass_data pass_data_all_early_optimizations =
534 GIMPLE_PASS, /* type */
535 "early_optimizations", /* name */
536 OPTGROUP_NONE, /* optinfo_flags */
537 TV_NONE, /* tv_id */
538 0, /* properties_required */
539 0, /* properties_provided */
540 0, /* properties_destroyed */
541 0, /* todo_flags_start */
542 0, /* todo_flags_finish */
545 class pass_all_early_optimizations : public gimple_opt_pass
547 public:
548 pass_all_early_optimizations (gcc::context *ctxt)
549 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
552 /* opt_pass methods: */
553 virtual bool gate (function *)
555 return (optimize >= 1
556 /* Don't bother doing anything if the program has errors. */
557 && !seen_error ());
560 }; // class pass_all_early_optimizations
562 } // anon namespace
564 static gimple_opt_pass *
565 make_pass_all_early_optimizations (gcc::context *ctxt)
567 return new pass_all_early_optimizations (ctxt);
570 namespace {
572 const pass_data pass_data_all_optimizations =
574 GIMPLE_PASS, /* type */
575 "*all_optimizations", /* name */
576 OPTGROUP_NONE, /* optinfo_flags */
577 TV_OPTIMIZE, /* tv_id */
578 0, /* properties_required */
579 0, /* properties_provided */
580 0, /* properties_destroyed */
581 0, /* todo_flags_start */
582 0, /* todo_flags_finish */
585 class pass_all_optimizations : public gimple_opt_pass
587 public:
588 pass_all_optimizations (gcc::context *ctxt)
589 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
592 /* opt_pass methods: */
593 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
595 }; // class pass_all_optimizations
597 } // anon namespace
599 static gimple_opt_pass *
600 make_pass_all_optimizations (gcc::context *ctxt)
602 return new pass_all_optimizations (ctxt);
605 namespace {
607 const pass_data pass_data_all_optimizations_g =
609 GIMPLE_PASS, /* type */
610 "*all_optimizations_g", /* name */
611 OPTGROUP_NONE, /* optinfo_flags */
612 TV_OPTIMIZE, /* tv_id */
613 0, /* properties_required */
614 0, /* properties_provided */
615 0, /* properties_destroyed */
616 0, /* todo_flags_start */
617 0, /* todo_flags_finish */
620 class pass_all_optimizations_g : public gimple_opt_pass
622 public:
623 pass_all_optimizations_g (gcc::context *ctxt)
624 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
627 /* opt_pass methods: */
628 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
630 }; // class pass_all_optimizations_g
632 } // anon namespace
634 static gimple_opt_pass *
635 make_pass_all_optimizations_g (gcc::context *ctxt)
637 return new pass_all_optimizations_g (ctxt);
640 namespace {
642 const pass_data pass_data_rest_of_compilation =
644 RTL_PASS, /* type */
645 "*rest_of_compilation", /* name */
646 OPTGROUP_NONE, /* optinfo_flags */
647 TV_REST_OF_COMPILATION, /* tv_id */
648 PROP_rtl, /* properties_required */
649 0, /* properties_provided */
650 0, /* properties_destroyed */
651 0, /* todo_flags_start */
652 0, /* todo_flags_finish */
655 class pass_rest_of_compilation : public rtl_opt_pass
657 public:
658 pass_rest_of_compilation (gcc::context *ctxt)
659 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
662 /* opt_pass methods: */
663 virtual bool gate (function *)
665 /* Early return if there were errors. We can run afoul of our
666 consistency checks, and there's not really much point in fixing them. */
667 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
670 }; // class pass_rest_of_compilation
672 } // anon namespace
674 static rtl_opt_pass *
675 make_pass_rest_of_compilation (gcc::context *ctxt)
677 return new pass_rest_of_compilation (ctxt);
680 namespace {
682 const pass_data pass_data_postreload =
684 RTL_PASS, /* type */
685 "*all-postreload", /* name */
686 OPTGROUP_NONE, /* optinfo_flags */
687 TV_POSTRELOAD, /* tv_id */
688 PROP_rtl, /* properties_required */
689 0, /* properties_provided */
690 0, /* properties_destroyed */
691 0, /* todo_flags_start */
692 0, /* todo_flags_finish */
695 class pass_postreload : public rtl_opt_pass
697 public:
698 pass_postreload (gcc::context *ctxt)
699 : rtl_opt_pass (pass_data_postreload, ctxt)
702 /* opt_pass methods: */
703 virtual bool gate (function *) { return reload_completed; }
705 }; // class pass_postreload
707 } // anon namespace
709 static rtl_opt_pass *
710 make_pass_postreload (gcc::context *ctxt)
712 return new pass_postreload (ctxt);
715 namespace {
717 const pass_data pass_data_late_compilation =
719 RTL_PASS, /* type */
720 "*all-late_compilation", /* name */
721 OPTGROUP_NONE, /* optinfo_flags */
722 TV_LATE_COMPILATION, /* tv_id */
723 PROP_rtl, /* properties_required */
724 0, /* properties_provided */
725 0, /* properties_destroyed */
726 0, /* todo_flags_start */
727 0, /* todo_flags_finish */
730 class pass_late_compilation : public rtl_opt_pass
732 public:
733 pass_late_compilation (gcc::context *ctxt)
734 : rtl_opt_pass (pass_data_late_compilation, ctxt)
737 /* opt_pass methods: */
738 virtual bool gate (function *)
740 return reload_completed || targetm.no_register_allocation;
743 }; // class pass_late_compilation
745 } // anon namespace
747 static rtl_opt_pass *
748 make_pass_late_compilation (gcc::context *ctxt)
750 return new pass_late_compilation (ctxt);
755 /* Set the static pass number of pass PASS to ID and record that
756 in the mapping from static pass number to pass. */
758 void
759 pass_manager::
760 set_pass_for_id (int id, opt_pass *pass)
762 pass->static_pass_number = id;
763 if (passes_by_id_size <= id)
765 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
766 memset (passes_by_id + passes_by_id_size, 0,
767 (id + 1 - passes_by_id_size) * sizeof (void *));
768 passes_by_id_size = id + 1;
770 passes_by_id[id] = pass;
773 /* Return the pass with the static pass number ID. */
775 opt_pass *
776 pass_manager::get_pass_for_id (int id) const
778 if (id >= passes_by_id_size)
779 return NULL;
780 return passes_by_id[id];
783 /* Iterate over the pass tree allocating dump file numbers. We want
784 to do this depth first, and independent of whether the pass is
785 enabled or not. */
787 void
788 register_one_dump_file (opt_pass *pass)
790 g->get_passes ()->register_one_dump_file (pass);
793 void
794 pass_manager::register_one_dump_file (opt_pass *pass)
796 char *dot_name, *flag_name, *glob_name;
797 const char *name, *full_name, *prefix;
798 char num[10];
799 int flags, id;
800 int optgroup_flags = OPTGROUP_NONE;
801 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
803 /* See below in next_pass_1. */
804 num[0] = '\0';
805 if (pass->static_pass_number != -1)
806 sprintf (num, "%d", ((int) pass->static_pass_number < 0
807 ? 1 : pass->static_pass_number));
809 /* The name is both used to identify the pass for the purposes of plugins,
810 and to specify dump file name and option.
811 The latter two might want something short which is not quite unique; for
812 that reason, we may have a disambiguating prefix, followed by a space
813 to mark the start of the following dump file name / option string. */
814 name = strchr (pass->name, ' ');
815 name = name ? name + 1 : pass->name;
816 dot_name = concat (".", name, num, NULL);
817 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
819 prefix = "ipa-";
820 flags = TDF_IPA;
821 optgroup_flags |= OPTGROUP_IPA;
823 else if (pass->type == GIMPLE_PASS)
825 prefix = "tree-";
826 flags = TDF_TREE;
828 else
830 prefix = "rtl-";
831 flags = TDF_RTL;
834 flag_name = concat (prefix, name, num, NULL);
835 glob_name = concat (prefix, name, NULL);
836 optgroup_flags |= pass->optinfo_flags;
837 /* For any passes that do not have an optgroup set, and which are not
838 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
839 any dump messages are emitted properly under -fopt-info(-optall). */
840 if (optgroup_flags == OPTGROUP_NONE)
841 optgroup_flags = OPTGROUP_OTHER;
842 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
843 optgroup_flags,
844 true);
845 set_pass_for_id (id, pass);
846 full_name = concat (prefix, pass->name, num, NULL);
847 register_pass_name (pass, full_name);
848 free (CONST_CAST (char *, full_name));
851 /* Register the dump files for the pass_manager starting at PASS. */
853 void
854 pass_manager::register_dump_files (opt_pass *pass)
858 if (pass->name && pass->name[0] != '*')
859 register_one_dump_file (pass);
861 if (pass->sub)
862 register_dump_files (pass->sub);
864 pass = pass->next;
866 while (pass);
869 static hash_map<nofree_string_hash, opt_pass *> *name_to_pass_map;
871 /* Register PASS with NAME. */
873 static void
874 register_pass_name (opt_pass *pass, const char *name)
876 if (!name_to_pass_map)
877 name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
879 if (name_to_pass_map->get (name))
880 return; /* Ignore plugin passes. */
882 const char *unique_name = xstrdup (name);
883 name_to_pass_map->put (unique_name, pass);
886 /* Map from pass id to canonicalized pass name. */
888 typedef const char *char_ptr;
889 static vec<char_ptr> pass_tab = vNULL;
891 /* Callback function for traversing NAME_TO_PASS_MAP. */
893 bool
894 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
896 gcc_assert (pass->static_pass_number > 0);
897 gcc_assert (pass_tab.exists ());
899 pass_tab[pass->static_pass_number] = name;
901 return 1;
904 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
905 table for dumping purpose. */
907 static void
908 create_pass_tab (void)
910 if (!flag_dump_passes)
911 return;
913 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
914 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
917 static bool override_gate_status (opt_pass *, tree, bool);
919 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
920 is turned on or not. */
922 static void
923 dump_one_pass (opt_pass *pass, int pass_indent)
925 int indent = 3 * pass_indent;
926 const char *pn;
927 bool is_on, is_really_on;
929 is_on = pass->gate (cfun);
930 is_really_on = override_gate_status (pass, current_function_decl, is_on);
932 if (pass->static_pass_number <= 0)
933 pn = pass->name;
934 else
935 pn = pass_tab[pass->static_pass_number];
937 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
938 (15 - indent < 0 ? 0 : 15 - indent), " ",
939 is_on ? " ON" : " OFF",
940 ((!is_on) == (!is_really_on) ? ""
941 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
944 /* Dump pass list PASS with indentation INDENT. */
946 static void
947 dump_pass_list (opt_pass *pass, int indent)
951 dump_one_pass (pass, indent);
952 if (pass->sub)
953 dump_pass_list (pass->sub, indent + 1);
954 pass = pass->next;
956 while (pass);
959 /* Dump all optimization passes. */
961 void
962 dump_passes (void)
964 g->get_passes ()->dump_passes ();
967 void
968 pass_manager::dump_passes () const
970 push_dummy_function (true);
972 create_pass_tab ();
974 dump_pass_list (all_lowering_passes, 1);
975 dump_pass_list (all_small_ipa_passes, 1);
976 dump_pass_list (all_regular_ipa_passes, 1);
977 dump_pass_list (all_late_ipa_passes, 1);
978 dump_pass_list (all_passes, 1);
980 pop_dummy_function ();
983 /* Returns the pass with NAME. */
985 static opt_pass *
986 get_pass_by_name (const char *name)
988 opt_pass **p = name_to_pass_map->get (name);
989 if (p)
990 return *p;
992 return NULL;
996 /* Range [start, last]. */
998 struct uid_range
1000 unsigned int start;
1001 unsigned int last;
1002 const char *assem_name;
1003 struct uid_range *next;
1006 typedef struct uid_range *uid_range_p;
1009 static vec<uid_range_p>
1010 enabled_pass_uid_range_tab = vNULL;
1011 static vec<uid_range_p>
1012 disabled_pass_uid_range_tab = vNULL;
1015 /* Parse option string for -fdisable- and -fenable-
1016 The syntax of the options:
1018 -fenable-<pass_name>
1019 -fdisable-<pass_name>
1021 -fenable-<pass_name>=s1:e1,s2:e2,...
1022 -fdisable-<pass_name>=s1:e1,s2:e2,...
1025 static void
1026 enable_disable_pass (const char *arg, bool is_enable)
1028 opt_pass *pass;
1029 char *range_str, *phase_name;
1030 char *argstr = xstrdup (arg);
1031 vec<uid_range_p> *tab = 0;
1033 range_str = strchr (argstr,'=');
1034 if (range_str)
1036 *range_str = '\0';
1037 range_str++;
1040 phase_name = argstr;
1041 if (!*phase_name)
1043 if (is_enable)
1044 error ("unrecognized option -fenable");
1045 else
1046 error ("unrecognized option -fdisable");
1047 free (argstr);
1048 return;
1050 pass = get_pass_by_name (phase_name);
1051 if (!pass || pass->static_pass_number == -1)
1053 if (is_enable)
1054 error ("unknown pass %s specified in -fenable", phase_name);
1055 else
1056 error ("unknown pass %s specified in -fdisable", phase_name);
1057 free (argstr);
1058 return;
1061 if (is_enable)
1062 tab = &enabled_pass_uid_range_tab;
1063 else
1064 tab = &disabled_pass_uid_range_tab;
1066 if ((unsigned) pass->static_pass_number >= tab->length ())
1067 tab->safe_grow_cleared (pass->static_pass_number + 1);
1069 if (!range_str)
1071 uid_range_p slot;
1072 uid_range_p new_range = XCNEW (struct uid_range);
1074 new_range->start = 0;
1075 new_range->last = (unsigned)-1;
1077 slot = (*tab)[pass->static_pass_number];
1078 new_range->next = slot;
1079 (*tab)[pass->static_pass_number] = new_range;
1080 if (is_enable)
1081 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1082 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1083 else
1084 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1085 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1087 else
1089 char *next_range = NULL;
1090 char *one_range = range_str;
1091 char *end_val = NULL;
1095 uid_range_p slot;
1096 uid_range_p new_range;
1097 char *invalid = NULL;
1098 long start;
1099 char *func_name = NULL;
1101 next_range = strchr (one_range, ',');
1102 if (next_range)
1104 *next_range = '\0';
1105 next_range++;
1108 end_val = strchr (one_range, ':');
1109 if (end_val)
1111 *end_val = '\0';
1112 end_val++;
1114 start = strtol (one_range, &invalid, 10);
1115 if (*invalid || start < 0)
1117 if (end_val || (one_range[0] >= '0'
1118 && one_range[0] <= '9'))
1120 error ("Invalid range %s in option %s",
1121 one_range,
1122 is_enable ? "-fenable" : "-fdisable");
1123 free (argstr);
1124 return;
1126 func_name = one_range;
1128 if (!end_val)
1130 new_range = XCNEW (struct uid_range);
1131 if (!func_name)
1133 new_range->start = (unsigned) start;
1134 new_range->last = (unsigned) start;
1136 else
1138 new_range->start = (unsigned) -1;
1139 new_range->last = (unsigned) -1;
1140 new_range->assem_name = xstrdup (func_name);
1143 else
1145 long last = strtol (end_val, &invalid, 10);
1146 if (*invalid || last < start)
1148 error ("Invalid range %s in option %s",
1149 end_val,
1150 is_enable ? "-fenable" : "-fdisable");
1151 free (argstr);
1152 return;
1154 new_range = XCNEW (struct uid_range);
1155 new_range->start = (unsigned) start;
1156 new_range->last = (unsigned) last;
1159 slot = (*tab)[pass->static_pass_number];
1160 new_range->next = slot;
1161 (*tab)[pass->static_pass_number] = new_range;
1162 if (is_enable)
1164 if (new_range->assem_name)
1165 inform (UNKNOWN_LOCATION,
1166 "enable pass %s for function %s",
1167 phase_name, new_range->assem_name);
1168 else
1169 inform (UNKNOWN_LOCATION,
1170 "enable pass %s for functions in the range of [%u, %u]",
1171 phase_name, new_range->start, new_range->last);
1173 else
1175 if (new_range->assem_name)
1176 inform (UNKNOWN_LOCATION,
1177 "disable pass %s for function %s",
1178 phase_name, new_range->assem_name);
1179 else
1180 inform (UNKNOWN_LOCATION,
1181 "disable pass %s for functions in the range of [%u, %u]",
1182 phase_name, new_range->start, new_range->last);
1185 one_range = next_range;
1186 } while (next_range);
1189 free (argstr);
1192 /* Enable pass specified by ARG. */
1194 void
1195 enable_pass (const char *arg)
1197 enable_disable_pass (arg, true);
1200 /* Disable pass specified by ARG. */
1202 void
1203 disable_pass (const char *arg)
1205 enable_disable_pass (arg, false);
1208 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1210 static bool
1211 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1212 tree func,
1213 vec<uid_range_p> tab)
1215 uid_range_p slot, range;
1216 int cgraph_uid;
1217 const char *aname = NULL;
1219 if (!tab.exists ()
1220 || (unsigned) pass->static_pass_number >= tab.length ()
1221 || pass->static_pass_number == -1)
1222 return false;
1224 slot = tab[pass->static_pass_number];
1225 if (!slot)
1226 return false;
1228 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1229 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1230 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1232 range = slot;
1233 while (range)
1235 if ((unsigned) cgraph_uid >= range->start
1236 && (unsigned) cgraph_uid <= range->last)
1237 return true;
1238 if (range->assem_name && aname
1239 && !strcmp (range->assem_name, aname))
1240 return true;
1241 range = range->next;
1244 return false;
1248 /* Update static_pass_number for passes (and the flag
1249 TODO_mark_first_instance).
1251 Passes are constructed with static_pass_number preinitialized to 0
1253 This field is used in two different ways: initially as instance numbers
1254 of their kind, and then as ids within the entire pass manager.
1256 Within pass_manager::pass_manager:
1258 * In add_pass_instance(), as called by next_pass_1 in
1259 NEXT_PASS in init_optimization_passes
1261 * When the initial instance of a pass within a pass manager is seen,
1262 it is flagged, and its static_pass_number is set to -1
1264 * On subsequent times that it is seen, the static pass number
1265 is decremented each time, so that if there are e.g. 4 dups,
1266 they have static_pass_number -4, 2, 3, 4 respectively (note
1267 how the initial one is negative and gives the count); these
1268 can be thought of as instance numbers of the specific pass
1270 * Within the register_dump_files () traversal, set_pass_for_id()
1271 is called on each pass, using these instance numbers to create
1272 dumpfile switches, and then overwriting them with a pass id,
1273 which are global to the whole pass manager (based on
1274 (TDI_end + current value of extra_dump_files_in_use) ) */
1276 static void
1277 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1278 opt_pass *initial_pass)
1280 /* Are we dealing with the first pass of its kind, or a clone? */
1281 if (new_pass != initial_pass)
1283 /* We're dealing with a clone. */
1284 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1286 /* Indicate to register_dump_files that this pass has duplicates,
1287 and so it should rename the dump file. The first instance will
1288 be -1, and be number of duplicates = -static_pass_number - 1.
1289 Subsequent instances will be > 0 and just the duplicate number. */
1290 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1292 initial_pass->static_pass_number -= 1;
1293 new_pass->static_pass_number = -initial_pass->static_pass_number;
1296 else
1298 /* We're dealing with the first pass of its kind. */
1299 new_pass->todo_flags_start |= TODO_mark_first_instance;
1300 new_pass->static_pass_number = -1;
1302 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1306 /* Add a pass to the pass list. Duplicate the pass if it's already
1307 in the list. */
1309 static opt_pass **
1310 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1312 /* Every pass should have a name so that plugins can refer to them. */
1313 gcc_assert (pass->name != NULL);
1315 add_pass_instance (pass, false, initial_pass);
1316 *list = pass;
1318 return &(*list)->next;
1321 /* List node for an inserted pass instance. We need to keep track of all
1322 the newly-added pass instances (with 'added_pass_nodes' defined below)
1323 so that we can register their dump files after pass-positioning is finished.
1324 Registering dumping files needs to be post-processed or the
1325 static_pass_number of the opt_pass object would be modified and mess up
1326 the dump file names of future pass instances to be added. */
1328 struct pass_list_node
1330 opt_pass *pass;
1331 struct pass_list_node *next;
1334 static struct pass_list_node *added_pass_nodes = NULL;
1335 static struct pass_list_node *prev_added_pass_node;
1337 /* Insert the pass at the proper position. Return true if the pass
1338 is successfully added.
1340 NEW_PASS_INFO - new pass to be inserted
1341 PASS_LIST - root of the pass list to insert the new pass to */
1343 static bool
1344 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1346 opt_pass *pass = *pass_list, *prev_pass = NULL;
1347 bool success = false;
1349 for ( ; pass; prev_pass = pass, pass = pass->next)
1351 /* Check if the current pass is of the same type as the new pass and
1352 matches the name and the instance number of the reference pass. */
1353 if (pass->type == new_pass_info->pass->type
1354 && pass->name
1355 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1356 && ((new_pass_info->ref_pass_instance_number == 0)
1357 || (new_pass_info->ref_pass_instance_number ==
1358 pass->static_pass_number)
1359 || (new_pass_info->ref_pass_instance_number == 1
1360 && pass->todo_flags_start & TODO_mark_first_instance)))
1362 opt_pass *new_pass;
1363 struct pass_list_node *new_pass_node;
1365 if (new_pass_info->ref_pass_instance_number == 0)
1367 new_pass = new_pass_info->pass->clone ();
1368 add_pass_instance (new_pass, true, new_pass_info->pass);
1370 else
1372 new_pass = new_pass_info->pass;
1373 add_pass_instance (new_pass, true, new_pass);
1376 /* Insert the new pass instance based on the positioning op. */
1377 switch (new_pass_info->pos_op)
1379 case PASS_POS_INSERT_AFTER:
1380 new_pass->next = pass->next;
1381 pass->next = new_pass;
1383 /* Skip newly inserted pass to avoid repeated
1384 insertions in the case where the new pass and the
1385 existing one have the same name. */
1386 pass = new_pass;
1387 break;
1388 case PASS_POS_INSERT_BEFORE:
1389 new_pass->next = pass;
1390 if (prev_pass)
1391 prev_pass->next = new_pass;
1392 else
1393 *pass_list = new_pass;
1394 break;
1395 case PASS_POS_REPLACE:
1396 new_pass->next = pass->next;
1397 if (prev_pass)
1398 prev_pass->next = new_pass;
1399 else
1400 *pass_list = new_pass;
1401 new_pass->sub = pass->sub;
1402 new_pass->tv_id = pass->tv_id;
1403 pass = new_pass;
1404 break;
1405 default:
1406 error ("invalid pass positioning operation");
1407 return false;
1410 /* Save the newly added pass (instance) in the added_pass_nodes
1411 list so that we can register its dump file later. Note that
1412 we cannot register the dump file now because doing so will modify
1413 the static_pass_number of the opt_pass object and therefore
1414 mess up the dump file name of future instances. */
1415 new_pass_node = XCNEW (struct pass_list_node);
1416 new_pass_node->pass = new_pass;
1417 if (!added_pass_nodes)
1418 added_pass_nodes = new_pass_node;
1419 else
1420 prev_added_pass_node->next = new_pass_node;
1421 prev_added_pass_node = new_pass_node;
1423 success = true;
1426 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1427 success = true;
1430 return success;
1433 /* Hooks a new pass into the pass lists.
1435 PASS_INFO - pass information that specifies the opt_pass object,
1436 reference pass, instance number, and how to position
1437 the pass */
1439 void
1440 register_pass (struct register_pass_info *pass_info)
1442 g->get_passes ()->register_pass (pass_info);
1445 void
1446 register_pass (opt_pass* pass, pass_positioning_ops pos,
1447 const char* ref_pass_name, int ref_pass_inst_number)
1449 register_pass_info i;
1450 i.pass = pass;
1451 i.reference_pass_name = ref_pass_name;
1452 i.ref_pass_instance_number = ref_pass_inst_number;
1453 i.pos_op = pos;
1455 g->get_passes ()->register_pass (&i);
1458 void
1459 pass_manager::register_pass (struct register_pass_info *pass_info)
1461 bool all_instances, success;
1462 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1464 /* The checks below could fail in buggy plugins. Existing GCC
1465 passes should never fail these checks, so we mention plugin in
1466 the messages. */
1467 if (!pass_info->pass)
1468 fatal_error (input_location, "plugin cannot register a missing pass");
1470 if (!pass_info->pass->name)
1471 fatal_error (input_location, "plugin cannot register an unnamed pass");
1473 if (!pass_info->reference_pass_name)
1474 fatal_error
1475 (input_location,
1476 "plugin cannot register pass %qs without reference pass name",
1477 pass_info->pass->name);
1479 /* Try to insert the new pass to the pass lists. We need to check
1480 all five lists as the reference pass could be in one (or all) of
1481 them. */
1482 all_instances = pass_info->ref_pass_instance_number == 0;
1483 success = position_pass (pass_info, &all_lowering_passes);
1484 if (!success || all_instances)
1485 success |= position_pass (pass_info, &all_small_ipa_passes);
1486 if (!success || all_instances)
1487 success |= position_pass (pass_info, &all_regular_ipa_passes);
1488 if (!success || all_instances)
1489 success |= position_pass (pass_info, &all_late_ipa_passes);
1490 if (!success || all_instances)
1491 success |= position_pass (pass_info, &all_passes);
1492 if (!success)
1493 fatal_error
1494 (input_location,
1495 "pass %qs not found but is referenced by new pass %qs",
1496 pass_info->reference_pass_name, pass_info->pass->name);
1498 /* OK, we have successfully inserted the new pass. We need to register
1499 the dump files for the newly added pass and its duplicates (if any).
1500 Because the registration of plugin/backend passes happens after the
1501 command-line options are parsed, the options that specify single
1502 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1503 passes. Therefore we currently can only enable dumping of
1504 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1505 are specified. While doing so, we also delete the pass_list_node
1506 objects created during pass positioning. */
1507 while (added_pass_nodes)
1509 struct pass_list_node *next_node = added_pass_nodes->next;
1510 enum tree_dump_index tdi;
1511 register_one_dump_file (added_pass_nodes->pass);
1512 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1513 || added_pass_nodes->pass->type == IPA_PASS)
1514 tdi = TDI_ipa_all;
1515 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1516 tdi = TDI_tree_all;
1517 else
1518 tdi = TDI_rtl_all;
1519 /* Check if dump-all flag is specified. */
1520 if (dumps->get_dump_file_info (tdi)->pstate)
1521 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1522 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1523 XDELETE (added_pass_nodes);
1524 added_pass_nodes = next_node;
1528 /* Construct the pass tree. The sequencing of passes is driven by
1529 the cgraph routines:
1531 finalize_compilation_unit ()
1532 for each node N in the cgraph
1533 cgraph_analyze_function (N)
1534 cgraph_lower_function (N) -> all_lowering_passes
1536 If we are optimizing, compile is then invoked:
1538 compile ()
1539 ipa_passes () -> all_small_ipa_passes
1540 -> Analysis of all_regular_ipa_passes
1541 * possible LTO streaming at copmilation time *
1542 -> Execution of all_regular_ipa_passes
1543 * possible LTO streaming at link time *
1544 -> all_late_ipa_passes
1545 expand_all_functions ()
1546 for each node N in the cgraph
1547 expand_function (N) -> Transformation of all_regular_ipa_passes
1548 -> all_passes
1551 void *
1552 pass_manager::operator new (size_t sz)
1554 /* Ensure that all fields of the pass manager are zero-initialized. */
1555 return xcalloc (1, sz);
1558 void
1559 pass_manager::operator delete (void *ptr)
1561 free (ptr);
1564 pass_manager::pass_manager (context *ctxt)
1565 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1566 all_regular_ipa_passes (NULL),
1567 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1568 m_ctxt (ctxt)
1570 opt_pass **p;
1572 /* Initialize the pass_lists array. */
1573 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1574 GCC_PASS_LISTS
1575 #undef DEF_PASS_LIST
1577 /* Build the tree of passes. */
1579 #define INSERT_PASSES_AFTER(PASS) \
1580 p = &(PASS);
1582 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1584 opt_pass **p = &(PASS ## _1)->sub;
1586 #define POP_INSERT_PASSES() \
1589 #define NEXT_PASS(PASS, NUM) \
1590 do { \
1591 gcc_assert (NULL == PASS ## _ ## NUM); \
1592 if ((NUM) == 1) \
1593 PASS ## _1 = make_##PASS (m_ctxt); \
1594 else \
1596 gcc_assert (PASS ## _1); \
1597 PASS ## _ ## NUM = PASS ## _1->clone (); \
1599 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1600 } while (0)
1602 #define TERMINATE_PASS_LIST() \
1603 *p = NULL;
1605 #include "pass-instances.def"
1607 #undef INSERT_PASSES_AFTER
1608 #undef PUSH_INSERT_PASSES_WITHIN
1609 #undef POP_INSERT_PASSES
1610 #undef NEXT_PASS
1611 #undef TERMINATE_PASS_LIST
1613 /* Register the passes with the tree dump code. */
1614 register_dump_files (all_lowering_passes);
1615 register_dump_files (all_small_ipa_passes);
1616 register_dump_files (all_regular_ipa_passes);
1617 register_dump_files (all_late_ipa_passes);
1618 register_dump_files (all_passes);
1621 static void
1622 delete_pass_tree (opt_pass *pass)
1624 while (pass)
1626 /* Recurse into child passes. */
1627 delete_pass_tree (pass->sub);
1629 opt_pass *next = pass->next;
1631 /* Delete this pass. */
1632 delete pass;
1634 /* Iterate onto sibling passes. */
1635 pass = next;
1639 pass_manager::~pass_manager ()
1641 XDELETEVEC (passes_by_id);
1643 /* Call delete_pass_tree on each of the pass_lists. */
1644 #define DEF_PASS_LIST(LIST) \
1645 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1646 GCC_PASS_LISTS
1647 #undef DEF_PASS_LIST
1651 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1652 function CALLBACK for every function in the call graph. Otherwise,
1653 call CALLBACK on the current function. */
1655 static void
1656 do_per_function (void (*callback) (function *, void *data), void *data)
1658 if (current_function_decl)
1659 callback (cfun, data);
1660 else
1662 struct cgraph_node *node;
1663 FOR_EACH_DEFINED_FUNCTION (node)
1664 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1665 && (!node->clone_of || node->decl != node->clone_of->decl))
1666 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1670 /* Because inlining might remove no-longer reachable nodes, we need to
1671 keep the array visible to garbage collector to avoid reading collected
1672 out nodes. */
1673 static int nnodes;
1674 static GTY ((length ("nnodes"))) cgraph_node **order;
1676 /* Hook called when NODE is removed and therefore should be
1677 excluded from order vector. DATA is an array of integers.
1678 DATA[0] holds max index it may be accessed by. For cgraph
1679 node DATA[node->uid + 1] holds index of this node in order
1680 vector. */
1681 static void
1682 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1684 int *order_idx = (int *)data;
1686 if (node->uid >= order_idx[0])
1687 return;
1689 int idx = order_idx[node->uid + 1];
1690 if (idx >= 0 && idx < nnodes && order[idx] == node)
1691 order[idx] = NULL;
1694 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1695 function CALLBACK for every function in the call graph. Otherwise,
1696 call CALLBACK on the current function.
1697 This function is global so that plugins can use it. */
1698 void
1699 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1701 int i;
1703 if (current_function_decl)
1704 callback (cfun, data);
1705 else
1707 cgraph_node_hook_list *hook;
1708 int *order_idx;
1709 gcc_assert (!order);
1710 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1712 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1713 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1714 order_idx[0] = symtab->cgraph_max_uid;
1716 nnodes = ipa_reverse_postorder (order);
1717 for (i = nnodes - 1; i >= 0; i--)
1719 order[i]->process = 1;
1720 order_idx[order[i]->uid + 1] = i;
1722 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1723 order_idx);
1724 for (i = nnodes - 1; i >= 0; i--)
1726 /* Function could be inlined and removed as unreachable. */
1727 if (!order[i])
1728 continue;
1730 struct cgraph_node *node = order[i];
1732 /* Allow possibly removed nodes to be garbage collected. */
1733 order[i] = NULL;
1734 node->process = 0;
1735 if (node->has_gimple_body_p ())
1736 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1738 symtab->remove_cgraph_removal_hook (hook);
1740 ggc_free (order);
1741 order = NULL;
1742 nnodes = 0;
1745 /* Helper function to perform function body dump. */
1747 static void
1748 execute_function_dump (function *fn, void *data)
1750 opt_pass *pass = (opt_pass *)data;
1752 if (dump_file)
1754 push_cfun (fn);
1756 if (fn->curr_properties & PROP_trees)
1757 dump_function_to_file (fn->decl, dump_file, dump_flags);
1758 else
1759 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1761 /* Flush the file. If verification fails, we won't be able to
1762 close the file before aborting. */
1763 fflush (dump_file);
1765 if ((fn->curr_properties & PROP_cfg)
1766 && (dump_flags & TDF_GRAPH))
1768 if (!pass->graph_dump_initialized)
1770 clean_graph_dump_file (dump_file_name);
1771 pass->graph_dump_initialized = true;
1773 print_graph_cfg (dump_file_name, fn);
1776 pop_cfun ();
1780 static struct profile_record *profile_record;
1782 /* Do profile consistency book-keeping for the pass with static number INDEX.
1783 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1784 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1785 if we are only book-keeping on passes that may have selectively disabled
1786 themselves on a given function. */
1787 static void
1788 check_profile_consistency (int index, int subpass, bool run)
1790 pass_manager *passes = g->get_passes ();
1791 if (index == -1)
1792 return;
1793 if (!profile_record)
1794 profile_record = XCNEWVEC (struct profile_record,
1795 passes->passes_by_id_size);
1796 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1797 gcc_assert (subpass < 2);
1798 profile_record[index].run |= run;
1799 account_profile_record (&profile_record[index], subpass);
1802 /* Output profile consistency. */
1804 void
1805 dump_profile_report (void)
1807 g->get_passes ()->dump_profile_report ();
1810 void
1811 pass_manager::dump_profile_report () const
1813 int i, j;
1814 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1815 gcov_type last_time = 0, last_size = 0;
1816 double rel_time_change, rel_size_change;
1817 int last_reported = 0;
1819 if (!profile_record)
1820 return;
1821 fprintf (stderr, "\nProfile consistency report:\n\n");
1822 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1823 fprintf (stderr, " |freq count |freq count |size time\n");
1825 for (i = 0; i < passes_by_id_size; i++)
1826 for (j = 0 ; j < 2; j++)
1827 if (profile_record[i].run)
1829 if (last_time)
1830 rel_time_change = (profile_record[i].time[j]
1831 - (double)last_time) * 100 / (double)last_time;
1832 else
1833 rel_time_change = 0;
1834 if (last_size)
1835 rel_size_change = (profile_record[i].size[j]
1836 - (double)last_size) * 100 / (double)last_size;
1837 else
1838 rel_size_change = 0;
1840 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1841 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1842 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1843 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1844 || rel_time_change || rel_size_change)
1846 last_reported = i;
1847 fprintf (stderr, "%-20s %s",
1848 passes_by_id [i]->name,
1849 j ? "(after TODO)" : " ");
1850 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1851 fprintf (stderr, "| %+5i",
1852 profile_record[i].num_mismatched_freq_in[j]
1853 - last_freq_in);
1854 else
1855 fprintf (stderr, "| ");
1856 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1857 fprintf (stderr, " %+5i",
1858 profile_record[i].num_mismatched_count_in[j]
1859 - last_count_in);
1860 else
1861 fprintf (stderr, " ");
1862 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1863 fprintf (stderr, "| %+5i",
1864 profile_record[i].num_mismatched_freq_out[j]
1865 - last_freq_out);
1866 else
1867 fprintf (stderr, "| ");
1868 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1869 fprintf (stderr, " %+5i",
1870 profile_record[i].num_mismatched_count_out[j]
1871 - last_count_out);
1872 else
1873 fprintf (stderr, " ");
1875 /* Size/time units change across gimple and RTL. */
1876 if (i == pass_expand_1->static_pass_number)
1877 fprintf (stderr, "|----------");
1878 else
1880 if (rel_size_change)
1881 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1882 else
1883 fprintf (stderr, "| ");
1884 if (rel_time_change)
1885 fprintf (stderr, " %+8.4f%%", rel_time_change);
1887 fprintf (stderr, "\n");
1888 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1889 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1890 last_count_in = profile_record[i].num_mismatched_count_in[j];
1891 last_count_out = profile_record[i].num_mismatched_count_out[j];
1893 else if (j && last_reported != i)
1895 last_reported = i;
1896 fprintf (stderr, "%-20s ------------| | |\n",
1897 passes_by_id [i]->name);
1899 last_time = profile_record[i].time[j];
1900 last_size = profile_record[i].size[j];
1904 /* Perform all TODO actions that ought to be done on each function. */
1906 static void
1907 execute_function_todo (function *fn, void *data)
1909 bool from_ipa_pass = (cfun == NULL);
1910 unsigned int flags = (size_t)data;
1911 flags &= ~fn->last_verified;
1912 if (!flags)
1913 return;
1915 push_cfun (fn);
1917 /* Always cleanup the CFG before trying to update SSA. */
1918 if (flags & TODO_cleanup_cfg)
1920 cleanup_tree_cfg ();
1922 /* When cleanup_tree_cfg merges consecutive blocks, it may
1923 perform some simplistic propagation when removing single
1924 valued PHI nodes. This propagation may, in turn, cause the
1925 SSA form to become out-of-date (see PR 22037). So, even
1926 if the parent pass had not scheduled an SSA update, we may
1927 still need to do one. */
1928 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1929 flags |= TODO_update_ssa;
1932 if (flags & TODO_update_ssa_any)
1934 unsigned update_flags = flags & TODO_update_ssa_any;
1935 update_ssa (update_flags);
1938 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1939 compute_may_aliases ();
1941 if (optimize && (flags & TODO_update_address_taken))
1942 execute_update_addresses_taken ();
1944 if (flags & TODO_remove_unused_locals)
1945 remove_unused_locals ();
1947 if (flags & TODO_rebuild_frequencies)
1948 rebuild_frequencies ();
1950 if (flags & TODO_rebuild_cgraph_edges)
1951 cgraph_edge::rebuild_edges ();
1953 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1954 /* If we've seen errors do not bother running any verifiers. */
1955 if (flag_checking && !seen_error ())
1957 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1958 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1960 if (flags & TODO_verify_il)
1962 if (cfun->curr_properties & PROP_trees)
1964 if (cfun->curr_properties & PROP_cfg)
1965 /* IPA passes leave stmts to be fixed up, so make sure to
1966 not verify stmts really throw. */
1967 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1968 else
1969 verify_gimple_in_seq (gimple_body (cfun->decl));
1971 if (cfun->curr_properties & PROP_ssa)
1972 /* IPA passes leave stmts to be fixed up, so make sure to
1973 not verify SSA operands whose verifier will choke on that. */
1974 verify_ssa (true, !from_ipa_pass);
1975 /* IPA passes leave basic-blocks unsplit, so make sure to
1976 not trip on that. */
1977 if ((cfun->curr_properties & PROP_cfg)
1978 && !from_ipa_pass)
1979 verify_flow_info ();
1980 if (current_loops
1981 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1982 verify_loop_closed_ssa (false);
1983 if (cfun->curr_properties & PROP_rtl)
1984 verify_rtl_sharing ();
1987 /* Make sure verifiers don't change dominator state. */
1988 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1989 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1992 fn->last_verified = flags & TODO_verify_all;
1994 pop_cfun ();
1996 /* For IPA passes make sure to release dominator info, it can be
1997 computed by non-verifying TODOs. */
1998 if (from_ipa_pass)
2000 free_dominance_info (fn, CDI_DOMINATORS);
2001 free_dominance_info (fn, CDI_POST_DOMINATORS);
2005 /* Perform all TODO actions. */
2006 static void
2007 execute_todo (unsigned int flags)
2009 if (flag_checking
2010 && cfun
2011 && need_ssa_update_p (cfun))
2012 gcc_assert (flags & TODO_update_ssa_any);
2014 timevar_push (TV_TODO);
2016 /* Inform the pass whether it is the first time it is run. */
2017 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2019 statistics_fini_pass ();
2021 if (flags)
2022 do_per_function (execute_function_todo, (void *)(size_t) flags);
2024 /* At this point we should not have any unreachable code in the
2025 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
2026 if (cfun && cfun->gimple_df)
2027 flush_ssaname_freelist ();
2029 /* Always remove functions just as before inlining: IPA passes might be
2030 interested to see bodies of extern inline functions that are not inlined
2031 to analyze side effects. The full removal is done just at the end
2032 of IPA pass queue. */
2033 if (flags & TODO_remove_functions)
2035 gcc_assert (!cfun);
2036 symtab->remove_unreachable_nodes (dump_file);
2039 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2041 gcc_assert (!cfun);
2042 symtab_node::dump_table (dump_file);
2043 /* Flush the file. If verification fails, we won't be able to
2044 close the file before aborting. */
2045 fflush (dump_file);
2048 /* Now that the dumping has been done, we can get rid of the optional
2049 df problems. */
2050 if (flags & TODO_df_finish)
2051 df_finish_pass ((flags & TODO_df_verify) != 0);
2053 timevar_pop (TV_TODO);
2056 /* Verify invariants that should hold between passes. This is a place
2057 to put simple sanity checks. */
2059 static void
2060 verify_interpass_invariants (void)
2062 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2065 /* Clear the last verified flag. */
2067 static void
2068 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2070 fn->last_verified = 0;
2073 /* Helper function. Verify that the properties has been turn into the
2074 properties expected by the pass. */
2076 static void DEBUG_FUNCTION
2077 verify_curr_properties (function *fn, void *data)
2079 unsigned int props = (size_t)data;
2080 gcc_assert ((fn->curr_properties & props) == props);
2083 /* Initialize pass dump file. */
2084 /* This is non-static so that the plugins can use it. */
2086 bool
2087 pass_init_dump_file (opt_pass *pass)
2089 /* If a dump file name is present, open it if enabled. */
2090 if (pass->static_pass_number != -1)
2092 timevar_push (TV_DUMP);
2093 gcc::dump_manager *dumps = g->get_dumps ();
2094 bool initializing_dump =
2095 !dumps->dump_initialized_p (pass->static_pass_number);
2096 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2097 dumps->dump_start (pass->static_pass_number, &dump_flags);
2098 if (dump_file && current_function_decl)
2099 dump_function_header (dump_file, current_function_decl, dump_flags);
2100 if (initializing_dump
2101 && dump_file && (dump_flags & TDF_GRAPH)
2102 && cfun && (cfun->curr_properties & PROP_cfg))
2104 clean_graph_dump_file (dump_file_name);
2105 pass->graph_dump_initialized = true;
2107 timevar_pop (TV_DUMP);
2108 return initializing_dump;
2110 else
2111 return false;
2114 /* Flush PASS dump file. */
2115 /* This is non-static so that plugins can use it. */
2117 void
2118 pass_fini_dump_file (opt_pass *pass)
2120 timevar_push (TV_DUMP);
2122 /* Flush and close dump file. */
2123 if (dump_file_name)
2125 free (CONST_CAST (char *, dump_file_name));
2126 dump_file_name = NULL;
2129 g->get_dumps ()->dump_finish (pass->static_pass_number);
2130 timevar_pop (TV_DUMP);
2133 /* After executing the pass, apply expected changes to the function
2134 properties. */
2136 static void
2137 update_properties_after_pass (function *fn, void *data)
2139 opt_pass *pass = (opt_pass *) data;
2140 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2141 & ~pass->properties_destroyed;
2144 /* Execute summary generation for all of the passes in IPA_PASS. */
2146 void
2147 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2149 while (ipa_pass)
2151 opt_pass *pass = ipa_pass;
2153 /* Execute all of the IPA_PASSes in the list. */
2154 if (ipa_pass->type == IPA_PASS
2155 && pass->gate (cfun)
2156 && ipa_pass->generate_summary)
2158 pass_init_dump_file (pass);
2160 /* If a timevar is present, start it. */
2161 if (pass->tv_id)
2162 timevar_push (pass->tv_id);
2164 current_pass = pass;
2165 ipa_pass->generate_summary ();
2167 /* Stop timevar. */
2168 if (pass->tv_id)
2169 timevar_pop (pass->tv_id);
2171 pass_fini_dump_file (pass);
2173 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2177 /* Execute IPA_PASS function transform on NODE. */
2179 static void
2180 execute_one_ipa_transform_pass (struct cgraph_node *node,
2181 ipa_opt_pass_d *ipa_pass)
2183 opt_pass *pass = ipa_pass;
2184 unsigned int todo_after = 0;
2186 current_pass = pass;
2187 if (!ipa_pass->function_transform)
2188 return;
2190 /* Note that the folders should only create gimple expressions.
2191 This is a hack until the new folder is ready. */
2192 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2194 pass_init_dump_file (pass);
2196 /* Run pre-pass verification. */
2197 execute_todo (ipa_pass->function_transform_todo_flags_start);
2199 /* If a timevar is present, start it. */
2200 if (pass->tv_id != TV_NONE)
2201 timevar_push (pass->tv_id);
2203 /* Do it! */
2204 todo_after = ipa_pass->function_transform (node);
2206 /* Stop timevar. */
2207 if (pass->tv_id != TV_NONE)
2208 timevar_pop (pass->tv_id);
2210 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2211 check_profile_consistency (pass->static_pass_number, 0, true);
2213 /* Run post-pass cleanup and verification. */
2214 execute_todo (todo_after);
2215 verify_interpass_invariants ();
2216 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2217 check_profile_consistency (pass->static_pass_number, 1, true);
2219 if (dump_file)
2220 do_per_function (execute_function_dump, NULL);
2221 pass_fini_dump_file (pass);
2223 current_pass = NULL;
2225 /* Signal this is a suitable GC collection point. */
2226 if (!(todo_after & TODO_do_not_ggc_collect))
2227 ggc_collect ();
2230 /* For the current function, execute all ipa transforms. */
2232 void
2233 execute_all_ipa_transforms (void)
2235 struct cgraph_node *node;
2236 if (!cfun)
2237 return;
2238 node = cgraph_node::get (current_function_decl);
2240 if (node->ipa_transforms_to_apply.exists ())
2242 unsigned int i;
2244 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2245 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2246 node->ipa_transforms_to_apply.release ();
2250 /* Check if PASS is explicitly disabled or enabled and return
2251 the gate status. FUNC is the function to be processed, and
2252 GATE_STATUS is the gate status determined by pass manager by
2253 default. */
2255 static bool
2256 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2258 bool explicitly_enabled = false;
2259 bool explicitly_disabled = false;
2261 explicitly_enabled
2262 = is_pass_explicitly_enabled_or_disabled (pass, func,
2263 enabled_pass_uid_range_tab);
2264 explicitly_disabled
2265 = is_pass_explicitly_enabled_or_disabled (pass, func,
2266 disabled_pass_uid_range_tab);
2268 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2270 return gate_status;
2274 /* Execute PASS. */
2276 bool
2277 execute_one_pass (opt_pass *pass)
2279 unsigned int todo_after = 0;
2281 bool gate_status;
2283 /* IPA passes are executed on whole program, so cfun should be NULL.
2284 Other passes need function context set. */
2285 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2286 gcc_assert (!cfun && !current_function_decl);
2287 else
2288 gcc_assert (cfun && current_function_decl);
2290 current_pass = pass;
2292 /* Check whether gate check should be avoided.
2293 User controls the value of the gate through the parameter "gate_status". */
2294 gate_status = pass->gate (cfun);
2295 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2297 /* Override gate with plugin. */
2298 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2300 if (!gate_status)
2302 /* Run so passes selectively disabling themselves on a given function
2303 are not miscounted. */
2304 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2306 check_profile_consistency (pass->static_pass_number, 0, false);
2307 check_profile_consistency (pass->static_pass_number, 1, false);
2309 current_pass = NULL;
2310 return false;
2313 /* Pass execution event trigger: useful to identify passes being
2314 executed. */
2315 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2317 if (!quiet_flag && !cfun)
2318 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2320 /* Note that the folders should only create gimple expressions.
2321 This is a hack until the new folder is ready. */
2322 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2324 pass_init_dump_file (pass);
2326 /* Run pre-pass verification. */
2327 execute_todo (pass->todo_flags_start);
2329 if (flag_checking)
2330 do_per_function (verify_curr_properties,
2331 (void *)(size_t)pass->properties_required);
2333 /* If a timevar is present, start it. */
2334 if (pass->tv_id != TV_NONE)
2335 timevar_push (pass->tv_id);
2337 /* Do it! */
2338 todo_after = pass->execute (cfun);
2339 do_per_function (clear_last_verified, NULL);
2341 /* Stop timevar. */
2342 if (pass->tv_id != TV_NONE)
2343 timevar_pop (pass->tv_id);
2345 do_per_function (update_properties_after_pass, pass);
2347 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2348 check_profile_consistency (pass->static_pass_number, 0, true);
2350 /* Run post-pass cleanup and verification. */
2351 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2352 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2353 check_profile_consistency (pass->static_pass_number, 1, true);
2355 verify_interpass_invariants ();
2356 if (dump_file)
2357 do_per_function (execute_function_dump, pass);
2358 if (pass->type == IPA_PASS)
2360 struct cgraph_node *node;
2361 if (((ipa_opt_pass_d *)pass)->function_transform)
2362 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2363 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2366 if (!current_function_decl)
2367 symtab->process_new_functions ();
2369 pass_fini_dump_file (pass);
2371 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2372 gcc_assert (!(cfun->curr_properties & PROP_trees)
2373 || pass->type != RTL_PASS);
2375 current_pass = NULL;
2377 /* Signal this is a suitable GC collection point. */
2378 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2379 ggc_collect ();
2381 return true;
2384 static void
2385 execute_pass_list_1 (opt_pass *pass)
2389 gcc_assert (pass->type == GIMPLE_PASS
2390 || pass->type == RTL_PASS);
2391 if (execute_one_pass (pass) && pass->sub)
2392 execute_pass_list_1 (pass->sub);
2393 pass = pass->next;
2395 while (pass);
2398 void
2399 execute_pass_list (function *fn, opt_pass *pass)
2401 push_cfun (fn);
2402 execute_pass_list_1 (pass);
2403 if (fn->cfg)
2405 free_dominance_info (CDI_DOMINATORS);
2406 free_dominance_info (CDI_POST_DOMINATORS);
2408 pop_cfun ();
2411 /* Write out all LTO data. */
2412 static void
2413 write_lto (void)
2415 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2416 lto_output ();
2417 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2418 timevar_push (TV_IPA_LTO_DECL_OUT);
2419 produce_asm_for_decls ();
2420 timevar_pop (TV_IPA_LTO_DECL_OUT);
2423 /* Same as execute_pass_list but assume that subpasses of IPA passes
2424 are local passes. If SET is not NULL, write out summaries of only
2425 those node in SET. */
2427 static void
2428 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2430 while (pass)
2432 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2433 gcc_assert (!current_function_decl);
2434 gcc_assert (!cfun);
2435 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2436 if (pass->type == IPA_PASS
2437 && ipa_pass->write_summary
2438 && pass->gate (cfun))
2440 /* If a timevar is present, start it. */
2441 if (pass->tv_id)
2442 timevar_push (pass->tv_id);
2444 pass_init_dump_file (pass);
2446 current_pass = pass;
2447 ipa_pass->write_summary ();
2449 pass_fini_dump_file (pass);
2451 /* If a timevar is present, start it. */
2452 if (pass->tv_id)
2453 timevar_pop (pass->tv_id);
2456 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2457 ipa_write_summaries_2 (pass->sub, state);
2459 pass = pass->next;
2463 /* Helper function of ipa_write_summaries. Creates and destroys the
2464 decl state and calls ipa_write_summaries_2 for all passes that have
2465 summaries. SET is the set of nodes to be written. */
2467 static void
2468 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2470 pass_manager *passes = g->get_passes ();
2471 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2472 state->symtab_node_encoder = encoder;
2474 lto_output_init_mode_table ();
2475 lto_push_out_decl_state (state);
2477 gcc_assert (!flag_wpa);
2478 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2480 write_lto ();
2482 gcc_assert (lto_get_out_decl_state () == state);
2483 lto_pop_out_decl_state ();
2484 lto_delete_out_decl_state (state);
2487 /* Write out summaries for all the nodes in the callgraph. */
2489 void
2490 ipa_write_summaries (void)
2492 lto_symtab_encoder_t encoder;
2493 int i, order_pos;
2494 varpool_node *vnode;
2495 struct cgraph_node *node;
2496 struct cgraph_node **order;
2498 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2499 return;
2501 select_what_to_stream ();
2503 encoder = lto_symtab_encoder_new (false);
2505 /* Create the callgraph set in the same order used in
2506 cgraph_expand_all_functions. This mostly facilitates debugging,
2507 since it causes the gimple file to be processed in the same order
2508 as the source code. */
2509 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2510 order_pos = ipa_reverse_postorder (order);
2511 gcc_assert (order_pos == symtab->cgraph_count);
2513 for (i = order_pos - 1; i >= 0; i--)
2515 struct cgraph_node *node = order[i];
2517 if (node->has_gimple_body_p ())
2519 /* When streaming out references to statements as part of some IPA
2520 pass summary, the statements need to have uids assigned and the
2521 following does that for all the IPA passes here. Naturally, this
2522 ordering then matches the one IPA-passes get in their stmt_fixup
2523 hooks. */
2525 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2526 renumber_gimple_stmt_uids ();
2527 pop_cfun ();
2529 if (node->definition && node->need_lto_streaming)
2530 lto_set_symtab_encoder_in_partition (encoder, node);
2533 FOR_EACH_DEFINED_FUNCTION (node)
2534 if (node->alias && node->need_lto_streaming)
2535 lto_set_symtab_encoder_in_partition (encoder, node);
2536 FOR_EACH_DEFINED_VARIABLE (vnode)
2537 if (vnode->need_lto_streaming)
2538 lto_set_symtab_encoder_in_partition (encoder, vnode);
2540 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2542 free (order);
2545 /* Same as execute_pass_list but assume that subpasses of IPA passes
2546 are local passes. If SET is not NULL, write out optimization summaries of
2547 only those node in SET. */
2549 static void
2550 ipa_write_optimization_summaries_1 (opt_pass *pass,
2551 struct lto_out_decl_state *state)
2553 while (pass)
2555 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2556 gcc_assert (!current_function_decl);
2557 gcc_assert (!cfun);
2558 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2559 if (pass->type == IPA_PASS
2560 && ipa_pass->write_optimization_summary
2561 && pass->gate (cfun))
2563 /* If a timevar is present, start it. */
2564 if (pass->tv_id)
2565 timevar_push (pass->tv_id);
2567 pass_init_dump_file (pass);
2569 current_pass = pass;
2570 ipa_pass->write_optimization_summary ();
2572 pass_fini_dump_file (pass);
2574 /* If a timevar is present, start it. */
2575 if (pass->tv_id)
2576 timevar_pop (pass->tv_id);
2579 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2580 ipa_write_optimization_summaries_1 (pass->sub, state);
2582 pass = pass->next;
2586 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2587 NULL, write out all summaries of all nodes. */
2589 void
2590 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2592 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2593 lto_symtab_encoder_iterator lsei;
2594 state->symtab_node_encoder = encoder;
2596 lto_output_init_mode_table ();
2597 lto_push_out_decl_state (state);
2598 for (lsei = lsei_start_function_in_partition (encoder);
2599 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2601 struct cgraph_node *node = lsei_cgraph_node (lsei);
2602 /* When streaming out references to statements as part of some IPA
2603 pass summary, the statements need to have uids assigned.
2605 For functions newly born at WPA stage we need to initialize
2606 the uids here. */
2607 if (node->definition
2608 && gimple_has_body_p (node->decl))
2610 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2611 renumber_gimple_stmt_uids ();
2612 pop_cfun ();
2616 gcc_assert (flag_wpa);
2617 pass_manager *passes = g->get_passes ();
2618 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2620 write_lto ();
2622 gcc_assert (lto_get_out_decl_state () == state);
2623 lto_pop_out_decl_state ();
2624 lto_delete_out_decl_state (state);
2627 /* Same as execute_pass_list but assume that subpasses of IPA passes
2628 are local passes. */
2630 static void
2631 ipa_read_summaries_1 (opt_pass *pass)
2633 while (pass)
2635 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2637 gcc_assert (!current_function_decl);
2638 gcc_assert (!cfun);
2639 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2641 if (pass->gate (cfun))
2643 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2645 /* If a timevar is present, start it. */
2646 if (pass->tv_id)
2647 timevar_push (pass->tv_id);
2649 pass_init_dump_file (pass);
2651 current_pass = pass;
2652 ipa_pass->read_summary ();
2654 pass_fini_dump_file (pass);
2656 /* Stop timevar. */
2657 if (pass->tv_id)
2658 timevar_pop (pass->tv_id);
2661 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2662 ipa_read_summaries_1 (pass->sub);
2664 pass = pass->next;
2669 /* Read all the summaries for all_regular_ipa_passes. */
2671 void
2672 ipa_read_summaries (void)
2674 pass_manager *passes = g->get_passes ();
2675 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2678 /* Same as execute_pass_list but assume that subpasses of IPA passes
2679 are local passes. */
2681 static void
2682 ipa_read_optimization_summaries_1 (opt_pass *pass)
2684 while (pass)
2686 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2688 gcc_assert (!current_function_decl);
2689 gcc_assert (!cfun);
2690 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2692 if (pass->gate (cfun))
2694 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2696 /* If a timevar is present, start it. */
2697 if (pass->tv_id)
2698 timevar_push (pass->tv_id);
2700 pass_init_dump_file (pass);
2702 current_pass = pass;
2703 ipa_pass->read_optimization_summary ();
2705 pass_fini_dump_file (pass);
2707 /* Stop timevar. */
2708 if (pass->tv_id)
2709 timevar_pop (pass->tv_id);
2712 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2713 ipa_read_optimization_summaries_1 (pass->sub);
2715 pass = pass->next;
2719 /* Read all the summaries for all_regular_ipa_passes. */
2721 void
2722 ipa_read_optimization_summaries (void)
2724 pass_manager *passes = g->get_passes ();
2725 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2728 /* Same as execute_pass_list but assume that subpasses of IPA passes
2729 are local passes. */
2730 void
2731 execute_ipa_pass_list (opt_pass *pass)
2735 gcc_assert (!current_function_decl);
2736 gcc_assert (!cfun);
2737 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2738 if (execute_one_pass (pass) && pass->sub)
2740 if (pass->sub->type == GIMPLE_PASS)
2742 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2743 do_per_function_toporder ((void (*)(function *, void *))
2744 execute_pass_list,
2745 pass->sub);
2746 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2748 else if (pass->sub->type == SIMPLE_IPA_PASS
2749 || pass->sub->type == IPA_PASS)
2750 execute_ipa_pass_list (pass->sub);
2751 else
2752 gcc_unreachable ();
2754 gcc_assert (!current_function_decl);
2755 symtab->process_new_functions ();
2756 pass = pass->next;
2758 while (pass);
2761 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2763 static void
2764 execute_ipa_stmt_fixups (opt_pass *pass,
2765 struct cgraph_node *node, gimple **stmts)
2767 while (pass)
2769 /* Execute all of the IPA_PASSes in the list. */
2770 if (pass->type == IPA_PASS
2771 && pass->gate (cfun))
2773 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2775 if (ipa_pass->stmt_fixup)
2777 pass_init_dump_file (pass);
2778 /* If a timevar is present, start it. */
2779 if (pass->tv_id)
2780 timevar_push (pass->tv_id);
2782 current_pass = pass;
2783 ipa_pass->stmt_fixup (node, stmts);
2785 /* Stop timevar. */
2786 if (pass->tv_id)
2787 timevar_pop (pass->tv_id);
2788 pass_fini_dump_file (pass);
2790 if (pass->sub)
2791 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2793 pass = pass->next;
2797 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2799 void
2800 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2802 pass_manager *passes = g->get_passes ();
2803 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2807 extern void debug_properties (unsigned int);
2808 extern void dump_properties (FILE *, unsigned int);
2810 DEBUG_FUNCTION void
2811 dump_properties (FILE *dump, unsigned int props)
2813 fprintf (dump, "Properties:\n");
2814 if (props & PROP_gimple_any)
2815 fprintf (dump, "PROP_gimple_any\n");
2816 if (props & PROP_gimple_lcf)
2817 fprintf (dump, "PROP_gimple_lcf\n");
2818 if (props & PROP_gimple_leh)
2819 fprintf (dump, "PROP_gimple_leh\n");
2820 if (props & PROP_cfg)
2821 fprintf (dump, "PROP_cfg\n");
2822 if (props & PROP_ssa)
2823 fprintf (dump, "PROP_ssa\n");
2824 if (props & PROP_no_crit_edges)
2825 fprintf (dump, "PROP_no_crit_edges\n");
2826 if (props & PROP_rtl)
2827 fprintf (dump, "PROP_rtl\n");
2828 if (props & PROP_gimple_lomp)
2829 fprintf (dump, "PROP_gimple_lomp\n");
2830 if (props & PROP_gimple_lcx)
2831 fprintf (dump, "PROP_gimple_lcx\n");
2832 if (props & PROP_gimple_lvec)
2833 fprintf (dump, "PROP_gimple_lvec\n");
2834 if (props & PROP_cfglayout)
2835 fprintf (dump, "PROP_cfglayout\n");
2838 DEBUG_FUNCTION void
2839 debug_properties (unsigned int props)
2841 dump_properties (stderr, props);
2844 /* Called by local passes to see if function is called by already processed nodes.
2845 Because we process nodes in topological order, this means that function is
2846 in recursive cycle or we introduced new direct calls. */
2847 bool
2848 function_called_by_processed_nodes_p (void)
2850 struct cgraph_edge *e;
2851 for (e = cgraph_node::get (current_function_decl)->callers;
2853 e = e->next_caller)
2855 if (e->caller->decl == current_function_decl)
2856 continue;
2857 if (!e->caller->has_gimple_body_p ())
2858 continue;
2859 if (TREE_ASM_WRITTEN (e->caller->decl))
2860 continue;
2861 if (!e->caller->process && !e->caller->global.inlined_to)
2862 break;
2864 if (dump_file && e)
2866 fprintf (dump_file, "Already processed call to:\n");
2867 e->caller->dump (dump_file);
2869 return e != NULL;
2872 #include "gt-passes.h"