gcc/
[official-gcc.git] / gcc / passes.c
blobc0a90063bb815ccce6c950ab1a336d44fd158d7d
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 "tm.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "tree.h"
32 #include "fold-const.h"
33 #include "varasm.h"
34 #include "rtl.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "insn-attr.h"
38 #include "insn-config.h"
39 #include "insn-flags.h"
40 #include "hard-reg-set.h"
41 #include "recog.h"
42 #include "output.h"
43 #include "except.h"
44 #include "function.h"
45 #include "toplev.h"
46 #include "expmed.h"
47 #include "dojump.h"
48 #include "explow.h"
49 #include "calls.h"
50 #include "emit-rtl.h"
51 #include "stmt.h"
52 #include "expr.h"
53 #include "predict.h"
54 #include "basic-block.h"
55 #include "intl.h"
56 #include "graph.h"
57 #include "regs.h"
58 #include "diagnostic-core.h"
59 #include "params.h"
60 #include "reload.h"
61 #include "debug.h"
62 #include "target.h"
63 #include "langhooks.h"
64 #include "cfgloop.h"
65 #include "hosthooks.h"
66 #include "opts.h"
67 #include "coverage.h"
68 #include "value-prof.h"
69 #include "tree-inline.h"
70 #include "tree-ssa-alias.h"
71 #include "internal-fn.h"
72 #include "gimple-expr.h"
73 #include "gimple.h"
74 #include "gimple-ssa.h"
75 #include "tree-cfg.h"
76 #include "stringpool.h"
77 #include "tree-ssanames.h"
78 #include "tree-ssa-loop-manip.h"
79 #include "tree-into-ssa.h"
80 #include "tree-dfa.h"
81 #include "tree-ssa.h"
82 #include "tree-pass.h"
83 #include "tree-dump.h"
84 #include "df.h"
85 #include "plugin-api.h"
86 #include "ipa-ref.h"
87 #include "cgraph.h"
88 #include "lto-streamer.h"
89 #include "plugin.h"
90 #include "ipa-utils.h"
91 #include "tree-pretty-print.h" /* for dump_function_header */
92 #include "context.h"
93 #include "pass_manager.h"
94 #include "dominance.h"
95 #include "cfg.h"
96 #include "cfgrtl.h"
97 #include "tree-ssa-live.h" /* For remove_unused_locals. */
98 #include "tree-cfgcleanup.h"
100 using namespace gcc;
102 /* This is used for debugging. It allows the current pass to printed
103 from anywhere in compilation.
104 The variable current_pass is also used for statistics and plugins. */
105 opt_pass *current_pass;
107 static void register_pass_name (opt_pass *, const char *);
109 /* Most passes are single-instance (within their context) and thus don't
110 need to implement cloning, but passes that support multiple instances
111 *must* provide their own implementation of the clone method.
113 Handle this by providing a default implemenation, but make it a fatal
114 error to call it. */
116 opt_pass *
117 opt_pass::clone ()
119 internal_error ("pass %s does not support cloning", name);
122 bool
123 opt_pass::gate (function *)
125 return true;
128 unsigned int
129 opt_pass::execute (function *)
131 return 0;
134 opt_pass::opt_pass (const pass_data &data, context *ctxt)
135 : pass_data (data),
136 sub (NULL),
137 next (NULL),
138 static_pass_number (0),
139 m_ctxt (ctxt)
144 void
145 pass_manager::execute_early_local_passes ()
147 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
148 if (flag_check_pointer_bounds)
149 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
150 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
153 unsigned int
154 pass_manager::execute_pass_mode_switching ()
156 return pass_mode_switching_1->execute (cfun);
160 /* Call from anywhere to find out what pass this is. Useful for
161 printing out debugging information deep inside an service
162 routine. */
163 void
164 print_current_pass (FILE *file)
166 if (current_pass)
167 fprintf (file, "current pass = %s (%d)\n",
168 current_pass->name, current_pass->static_pass_number);
169 else
170 fprintf (file, "no current pass.\n");
174 /* Call from the debugger to get the current pass name. */
175 DEBUG_FUNCTION void
176 debug_pass (void)
178 print_current_pass (stderr);
183 /* Global variables used to communicate with passes. */
184 bool in_gimple_form;
185 bool first_pass_instance;
188 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
189 and TYPE_DECL nodes.
191 This does nothing for local (non-static) variables, unless the
192 variable is a register variable with DECL_ASSEMBLER_NAME set. In
193 that case, or if the variable is not an automatic, it sets up the
194 RTL and outputs any assembler code (label definition, storage
195 allocation and initialization).
197 DECL is the declaration. TOP_LEVEL is nonzero
198 if this declaration is not within a function. */
200 void
201 rest_of_decl_compilation (tree decl,
202 int top_level,
203 int at_end)
205 bool finalize = true;
207 /* We deferred calling assemble_alias so that we could collect
208 other attributes such as visibility. Emit the alias now. */
209 if (!in_lto_p)
211 tree alias;
212 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
213 if (alias)
215 alias = TREE_VALUE (TREE_VALUE (alias));
216 alias = get_identifier (TREE_STRING_POINTER (alias));
217 /* A quirk of the initial implementation of aliases required that the
218 user add "extern" to all of them. Which is silly, but now
219 historical. Do note that the symbol is in fact locally defined. */
220 DECL_EXTERNAL (decl) = 0;
221 TREE_STATIC (decl) = 1;
222 assemble_alias (decl, alias);
223 finalize = false;
227 /* Can't defer this, because it needs to happen before any
228 later function definitions are processed. */
229 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
230 make_decl_rtl (decl);
232 /* Forward declarations for nested functions are not "external",
233 but we need to treat them as if they were. */
234 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
235 || TREE_CODE (decl) == FUNCTION_DECL)
237 timevar_push (TV_VARCONST);
239 /* Don't output anything when a tentative file-scope definition
240 is seen. But at end of compilation, do output code for them.
242 We do output all variables and rely on
243 callgraph code to defer them except for forward declarations
244 (see gcc.c-torture/compile/920624-1.c) */
245 if ((at_end
246 || !DECL_DEFER_OUTPUT (decl)
247 || DECL_INITIAL (decl))
248 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
249 && !DECL_EXTERNAL (decl))
251 /* When reading LTO unit, we also read varpool, so do not
252 rebuild it. */
253 if (in_lto_p && !at_end)
255 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
256 varpool_node::finalize_decl (decl);
259 #ifdef ASM_FINISH_DECLARE_OBJECT
260 if (decl == last_assemble_variable_decl)
262 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
263 top_level, at_end);
265 #endif
267 timevar_pop (TV_VARCONST);
269 else if (TREE_CODE (decl) == TYPE_DECL
270 /* Like in rest_of_type_compilation, avoid confusing the debug
271 information machinery when there are errors. */
272 && !seen_error ())
274 timevar_push (TV_SYMOUT);
275 debug_hooks->type_decl (decl, !top_level);
276 timevar_pop (TV_SYMOUT);
279 /* Let cgraph know about the existence of variables. */
280 if (in_lto_p && !at_end)
282 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
283 && TREE_STATIC (decl))
284 varpool_node::get_create (decl);
286 /* Generate early debug for global variables. Any local variables will
287 be handled by either handling reachable functions from
288 finalize_compilation_unit (and by consequence, locally scoped
289 symbols), or by rest_of_type_compilation below.
291 Also, pick up function prototypes, which will be mostly ignored
292 by the different early_global_decl() hooks, but will at least be
293 used by Go's hijack of the debug_hooks to implement
294 -fdump-go-spec. */
295 if (!in_lto_p
296 && (TREE_CODE (decl) != FUNCTION_DECL
297 /* This will pick up function prototypes with no bodies,
298 which are not visible in finalize_compilation_unit()
299 while iterating with FOR_EACH_*_FUNCTION through the
300 symbol table. */
301 || !DECL_SAVED_TREE (decl))
303 /* We need to check both decl_function_context and
304 current_function_decl here to make sure local extern
305 declarations end up with the correct context.
307 For local extern declarations, decl_function_context is
308 empty, but current_function_decl is set to the function where
309 the extern was declared . Without the check for
310 !current_function_decl below, the local extern ends up
311 incorrectly with a top-level context.
313 For example:
315 namespace S
321 int i = 42;
323 extern int i; // Local extern declaration.
324 return i;
330 && !decl_function_context (decl)
331 && !current_function_decl
332 && DECL_SOURCE_LOCATION (decl) != BUILTINS_LOCATION
333 && !decl_type_context (decl))
334 (*debug_hooks->early_global_decl) (decl);
337 /* Called after finishing a record, union or enumeral type. */
339 void
340 rest_of_type_compilation (tree type, int toplev)
342 /* Avoid confusing the debug information machinery when there are
343 errors. */
344 if (seen_error ())
345 return;
347 timevar_push (TV_SYMOUT);
348 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
349 timevar_pop (TV_SYMOUT);
354 void
355 pass_manager::
356 finish_optimization_passes (void)
358 int i;
359 struct dump_file_info *dfi;
360 char *name;
361 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
363 timevar_push (TV_DUMP);
364 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
366 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
367 end_branch_prob ();
368 dumps->dump_finish (pass_profile_1->static_pass_number);
371 if (optimize > 0)
373 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
374 print_combine_total_stats ();
375 dumps->dump_finish (pass_profile_1->static_pass_number);
378 /* Do whatever is necessary to finish printing the graphs. */
379 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
380 if (dumps->dump_initialized_p (i)
381 && (dfi->pflags & TDF_GRAPH) != 0
382 && (name = dumps->get_dump_file_name (i)) != NULL)
384 finish_graph_dump_file (name);
385 free (name);
388 timevar_pop (TV_DUMP);
391 static unsigned int
392 execute_build_ssa_passes (void)
394 /* Once this pass (and its sub-passes) are complete, all functions
395 will be in SSA form. Technically this state change is happening
396 a tad early, since the sub-passes have not yet run, but since
397 none of the sub-passes are IPA passes and do not create new
398 functions, this is ok. We're setting this value for the benefit
399 of IPA passes that follow. */
400 if (symtab->state < IPA_SSA)
401 symtab->state = IPA_SSA;
402 return 0;
405 namespace {
407 const pass_data pass_data_build_ssa_passes =
409 SIMPLE_IPA_PASS, /* type */
410 "build_ssa_passes", /* name */
411 OPTGROUP_NONE, /* optinfo_flags */
412 TV_EARLY_LOCAL, /* tv_id */
413 0, /* properties_required */
414 0, /* properties_provided */
415 0, /* properties_destroyed */
416 0, /* todo_flags_start */
417 /* todo_flags_finish is executed before subpases. For this reason
418 it makes no sense to remove unreachable functions here. */
419 0, /* todo_flags_finish */
422 class pass_build_ssa_passes : public simple_ipa_opt_pass
424 public:
425 pass_build_ssa_passes (gcc::context *ctxt)
426 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
429 /* opt_pass methods: */
430 virtual bool gate (function *)
432 /* Don't bother doing anything if the program has errors. */
433 return (!seen_error () && !in_lto_p);
436 virtual unsigned int execute (function *)
438 return execute_build_ssa_passes ();
441 }; // class pass_build_ssa_passes
443 const pass_data pass_data_chkp_instrumentation_passes =
445 SIMPLE_IPA_PASS, /* type */
446 "chkp_passes", /* name */
447 OPTGROUP_NONE, /* optinfo_flags */
448 TV_NONE, /* tv_id */
449 0, /* properties_required */
450 0, /* properties_provided */
451 0, /* properties_destroyed */
452 0, /* todo_flags_start */
453 0, /* todo_flags_finish */
456 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
458 public:
459 pass_chkp_instrumentation_passes (gcc::context *ctxt)
460 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
463 /* opt_pass methods: */
464 virtual bool gate (function *)
466 /* Don't bother doing anything if the program has errors. */
467 return (flag_check_pointer_bounds
468 && !seen_error () && !in_lto_p);
471 }; // class pass_chkp_instrumentation_passes
473 const pass_data pass_data_local_optimization_passes =
475 SIMPLE_IPA_PASS, /* type */
476 "opt_local_passes", /* name */
477 OPTGROUP_NONE, /* optinfo_flags */
478 TV_NONE, /* tv_id */
479 0, /* properties_required */
480 0, /* properties_provided */
481 0, /* properties_destroyed */
482 0, /* todo_flags_start */
483 0, /* todo_flags_finish */
486 class pass_local_optimization_passes : public simple_ipa_opt_pass
488 public:
489 pass_local_optimization_passes (gcc::context *ctxt)
490 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
493 /* opt_pass methods: */
494 virtual bool gate (function *)
496 /* Don't bother doing anything if the program has errors. */
497 return (!seen_error () && !in_lto_p);
500 }; // class pass_local_optimization_passes
502 } // anon namespace
504 simple_ipa_opt_pass *
505 make_pass_build_ssa_passes (gcc::context *ctxt)
507 return new pass_build_ssa_passes (ctxt);
510 simple_ipa_opt_pass *
511 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
513 return new pass_chkp_instrumentation_passes (ctxt);
516 simple_ipa_opt_pass *
517 make_pass_local_optimization_passes (gcc::context *ctxt)
519 return new pass_local_optimization_passes (ctxt);
522 namespace {
524 const pass_data pass_data_all_early_optimizations =
526 GIMPLE_PASS, /* type */
527 "early_optimizations", /* name */
528 OPTGROUP_NONE, /* optinfo_flags */
529 TV_NONE, /* tv_id */
530 0, /* properties_required */
531 0, /* properties_provided */
532 0, /* properties_destroyed */
533 0, /* todo_flags_start */
534 0, /* todo_flags_finish */
537 class pass_all_early_optimizations : public gimple_opt_pass
539 public:
540 pass_all_early_optimizations (gcc::context *ctxt)
541 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
544 /* opt_pass methods: */
545 virtual bool gate (function *)
547 return (optimize >= 1
548 /* Don't bother doing anything if the program has errors. */
549 && !seen_error ());
552 }; // class pass_all_early_optimizations
554 } // anon namespace
556 static gimple_opt_pass *
557 make_pass_all_early_optimizations (gcc::context *ctxt)
559 return new pass_all_early_optimizations (ctxt);
562 namespace {
564 const pass_data pass_data_all_optimizations =
566 GIMPLE_PASS, /* type */
567 "*all_optimizations", /* name */
568 OPTGROUP_NONE, /* optinfo_flags */
569 TV_OPTIMIZE, /* tv_id */
570 0, /* properties_required */
571 0, /* properties_provided */
572 0, /* properties_destroyed */
573 0, /* todo_flags_start */
574 0, /* todo_flags_finish */
577 class pass_all_optimizations : public gimple_opt_pass
579 public:
580 pass_all_optimizations (gcc::context *ctxt)
581 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
584 /* opt_pass methods: */
585 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
587 }; // class pass_all_optimizations
589 } // anon namespace
591 static gimple_opt_pass *
592 make_pass_all_optimizations (gcc::context *ctxt)
594 return new pass_all_optimizations (ctxt);
597 namespace {
599 const pass_data pass_data_all_optimizations_g =
601 GIMPLE_PASS, /* type */
602 "*all_optimizations_g", /* name */
603 OPTGROUP_NONE, /* optinfo_flags */
604 TV_OPTIMIZE, /* tv_id */
605 0, /* properties_required */
606 0, /* properties_provided */
607 0, /* properties_destroyed */
608 0, /* todo_flags_start */
609 0, /* todo_flags_finish */
612 class pass_all_optimizations_g : public gimple_opt_pass
614 public:
615 pass_all_optimizations_g (gcc::context *ctxt)
616 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
619 /* opt_pass methods: */
620 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
622 }; // class pass_all_optimizations_g
624 } // anon namespace
626 static gimple_opt_pass *
627 make_pass_all_optimizations_g (gcc::context *ctxt)
629 return new pass_all_optimizations_g (ctxt);
632 namespace {
634 const pass_data pass_data_rest_of_compilation =
636 RTL_PASS, /* type */
637 "*rest_of_compilation", /* name */
638 OPTGROUP_NONE, /* optinfo_flags */
639 TV_REST_OF_COMPILATION, /* tv_id */
640 PROP_rtl, /* properties_required */
641 0, /* properties_provided */
642 0, /* properties_destroyed */
643 0, /* todo_flags_start */
644 0, /* todo_flags_finish */
647 class pass_rest_of_compilation : public rtl_opt_pass
649 public:
650 pass_rest_of_compilation (gcc::context *ctxt)
651 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
654 /* opt_pass methods: */
655 virtual bool gate (function *)
657 /* Early return if there were errors. We can run afoul of our
658 consistency checks, and there's not really much point in fixing them. */
659 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
662 }; // class pass_rest_of_compilation
664 } // anon namespace
666 static rtl_opt_pass *
667 make_pass_rest_of_compilation (gcc::context *ctxt)
669 return new pass_rest_of_compilation (ctxt);
672 namespace {
674 const pass_data pass_data_postreload =
676 RTL_PASS, /* type */
677 "*all-postreload", /* name */
678 OPTGROUP_NONE, /* optinfo_flags */
679 TV_POSTRELOAD, /* tv_id */
680 PROP_rtl, /* properties_required */
681 0, /* properties_provided */
682 0, /* properties_destroyed */
683 0, /* todo_flags_start */
684 0, /* todo_flags_finish */
687 class pass_postreload : public rtl_opt_pass
689 public:
690 pass_postreload (gcc::context *ctxt)
691 : rtl_opt_pass (pass_data_postreload, ctxt)
694 /* opt_pass methods: */
695 virtual bool gate (function *) { return reload_completed; }
697 }; // class pass_postreload
699 } // anon namespace
701 static rtl_opt_pass *
702 make_pass_postreload (gcc::context *ctxt)
704 return new pass_postreload (ctxt);
707 namespace {
709 const pass_data pass_data_late_compilation =
711 RTL_PASS, /* type */
712 "*all-late_compilation", /* name */
713 OPTGROUP_NONE, /* optinfo_flags */
714 TV_LATE_COMPILATION, /* tv_id */
715 PROP_rtl, /* properties_required */
716 0, /* properties_provided */
717 0, /* properties_destroyed */
718 0, /* todo_flags_start */
719 0, /* todo_flags_finish */
722 class pass_late_compilation : public rtl_opt_pass
724 public:
725 pass_late_compilation (gcc::context *ctxt)
726 : rtl_opt_pass (pass_data_late_compilation, ctxt)
729 /* opt_pass methods: */
730 virtual bool gate (function *)
732 return reload_completed || targetm.no_register_allocation;
735 }; // class pass_late_compilation
737 } // anon namespace
739 static rtl_opt_pass *
740 make_pass_late_compilation (gcc::context *ctxt)
742 return new pass_late_compilation (ctxt);
747 /* Set the static pass number of pass PASS to ID and record that
748 in the mapping from static pass number to pass. */
750 void
751 pass_manager::
752 set_pass_for_id (int id, opt_pass *pass)
754 pass->static_pass_number = id;
755 if (passes_by_id_size <= id)
757 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
758 memset (passes_by_id + passes_by_id_size, 0,
759 (id + 1 - passes_by_id_size) * sizeof (void *));
760 passes_by_id_size = id + 1;
762 passes_by_id[id] = pass;
765 /* Return the pass with the static pass number ID. */
767 opt_pass *
768 pass_manager::get_pass_for_id (int id) const
770 if (id >= passes_by_id_size)
771 return NULL;
772 return passes_by_id[id];
775 /* Iterate over the pass tree allocating dump file numbers. We want
776 to do this depth first, and independent of whether the pass is
777 enabled or not. */
779 void
780 register_one_dump_file (opt_pass *pass)
782 g->get_passes ()->register_one_dump_file (pass);
785 void
786 pass_manager::register_one_dump_file (opt_pass *pass)
788 char *dot_name, *flag_name, *glob_name;
789 const char *name, *full_name, *prefix;
790 char num[10];
791 int flags, id;
792 int optgroup_flags = OPTGROUP_NONE;
793 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
795 /* See below in next_pass_1. */
796 num[0] = '\0';
797 if (pass->static_pass_number != -1)
798 sprintf (num, "%d", ((int) pass->static_pass_number < 0
799 ? 1 : pass->static_pass_number));
801 /* The name is both used to identify the pass for the purposes of plugins,
802 and to specify dump file name and option.
803 The latter two might want something short which is not quite unique; for
804 that reason, we may have a disambiguating prefix, followed by a space
805 to mark the start of the following dump file name / option string. */
806 name = strchr (pass->name, ' ');
807 name = name ? name + 1 : pass->name;
808 dot_name = concat (".", name, num, NULL);
809 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
811 prefix = "ipa-";
812 flags = TDF_IPA;
813 optgroup_flags |= OPTGROUP_IPA;
815 else if (pass->type == GIMPLE_PASS)
817 prefix = "tree-";
818 flags = TDF_TREE;
820 else
822 prefix = "rtl-";
823 flags = TDF_RTL;
826 flag_name = concat (prefix, name, num, NULL);
827 glob_name = concat (prefix, name, NULL);
828 optgroup_flags |= pass->optinfo_flags;
829 /* For any passes that do not have an optgroup set, and which are not
830 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
831 any dump messages are emitted properly under -fopt-info(-optall). */
832 if (optgroup_flags == OPTGROUP_NONE)
833 optgroup_flags = OPTGROUP_OTHER;
834 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
835 optgroup_flags,
836 true);
837 set_pass_for_id (id, pass);
838 full_name = concat (prefix, pass->name, num, NULL);
839 register_pass_name (pass, full_name);
840 free (CONST_CAST (char *, full_name));
843 /* Register the dump files for the pass_manager starting at PASS. */
845 void
846 pass_manager::register_dump_files (opt_pass *pass)
850 if (pass->name && pass->name[0] != '*')
851 register_one_dump_file (pass);
853 if (pass->sub)
854 register_dump_files (pass->sub);
856 pass = pass->next;
858 while (pass);
861 typedef simple_hashmap_traits<nofree_string_hash> pass_registry_hasher;
863 static hash_map<const char *, opt_pass *, pass_registry_hasher>
864 *name_to_pass_map;
866 /* Register PASS with NAME. */
868 static void
869 register_pass_name (opt_pass *pass, const char *name)
871 if (!name_to_pass_map)
872 name_to_pass_map
873 = new hash_map<const char *, opt_pass *, pass_registry_hasher> (256);
875 if (name_to_pass_map->get (name))
876 return; /* Ignore plugin passes. */
878 const char *unique_name = xstrdup (name);
879 name_to_pass_map->put (unique_name, pass);
882 /* Map from pass id to canonicalized pass name. */
884 typedef const char *char_ptr;
885 static vec<char_ptr> pass_tab = vNULL;
887 /* Callback function for traversing NAME_TO_PASS_MAP. */
889 bool
890 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
892 gcc_assert (pass->static_pass_number > 0);
893 gcc_assert (pass_tab.exists ());
895 pass_tab[pass->static_pass_number] = name;
897 return 1;
900 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
901 table for dumping purpose. */
903 static void
904 create_pass_tab (void)
906 if (!flag_dump_passes)
907 return;
909 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
910 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
913 static bool override_gate_status (opt_pass *, tree, bool);
915 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
916 is turned on or not. */
918 static void
919 dump_one_pass (opt_pass *pass, int pass_indent)
921 int indent = 3 * pass_indent;
922 const char *pn;
923 bool is_on, is_really_on;
925 is_on = pass->gate (cfun);
926 is_really_on = override_gate_status (pass, current_function_decl, is_on);
928 if (pass->static_pass_number <= 0)
929 pn = pass->name;
930 else
931 pn = pass_tab[pass->static_pass_number];
933 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
934 (15 - indent < 0 ? 0 : 15 - indent), " ",
935 is_on ? " ON" : " OFF",
936 ((!is_on) == (!is_really_on) ? ""
937 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
940 /* Dump pass list PASS with indentation INDENT. */
942 static void
943 dump_pass_list (opt_pass *pass, int indent)
947 dump_one_pass (pass, indent);
948 if (pass->sub)
949 dump_pass_list (pass->sub, indent + 1);
950 pass = pass->next;
952 while (pass);
955 /* Dump all optimization passes. */
957 void
958 dump_passes (void)
960 g->get_passes ()->dump_passes ();
963 void
964 pass_manager::dump_passes () const
966 push_dummy_function (true);
968 create_pass_tab ();
970 dump_pass_list (all_lowering_passes, 1);
971 dump_pass_list (all_small_ipa_passes, 1);
972 dump_pass_list (all_regular_ipa_passes, 1);
973 dump_pass_list (all_late_ipa_passes, 1);
974 dump_pass_list (all_passes, 1);
976 pop_dummy_function ();
979 /* Returns the pass with NAME. */
981 static opt_pass *
982 get_pass_by_name (const char *name)
984 opt_pass **p = name_to_pass_map->get (name);
985 if (p)
986 return *p;
988 return NULL;
992 /* Range [start, last]. */
994 struct uid_range
996 unsigned int start;
997 unsigned int last;
998 const char *assem_name;
999 struct uid_range *next;
1002 typedef struct uid_range *uid_range_p;
1005 static vec<uid_range_p>
1006 enabled_pass_uid_range_tab = vNULL;
1007 static vec<uid_range_p>
1008 disabled_pass_uid_range_tab = vNULL;
1011 /* Parse option string for -fdisable- and -fenable-
1012 The syntax of the options:
1014 -fenable-<pass_name>
1015 -fdisable-<pass_name>
1017 -fenable-<pass_name>=s1:e1,s2:e2,...
1018 -fdisable-<pass_name>=s1:e1,s2:e2,...
1021 static void
1022 enable_disable_pass (const char *arg, bool is_enable)
1024 opt_pass *pass;
1025 char *range_str, *phase_name;
1026 char *argstr = xstrdup (arg);
1027 vec<uid_range_p> *tab = 0;
1029 range_str = strchr (argstr,'=');
1030 if (range_str)
1032 *range_str = '\0';
1033 range_str++;
1036 phase_name = argstr;
1037 if (!*phase_name)
1039 if (is_enable)
1040 error ("unrecognized option -fenable");
1041 else
1042 error ("unrecognized option -fdisable");
1043 free (argstr);
1044 return;
1046 pass = get_pass_by_name (phase_name);
1047 if (!pass || pass->static_pass_number == -1)
1049 if (is_enable)
1050 error ("unknown pass %s specified in -fenable", phase_name);
1051 else
1052 error ("unknown pass %s specified in -fdisable", phase_name);
1053 free (argstr);
1054 return;
1057 if (is_enable)
1058 tab = &enabled_pass_uid_range_tab;
1059 else
1060 tab = &disabled_pass_uid_range_tab;
1062 if ((unsigned) pass->static_pass_number >= tab->length ())
1063 tab->safe_grow_cleared (pass->static_pass_number + 1);
1065 if (!range_str)
1067 uid_range_p slot;
1068 uid_range_p new_range = XCNEW (struct uid_range);
1070 new_range->start = 0;
1071 new_range->last = (unsigned)-1;
1073 slot = (*tab)[pass->static_pass_number];
1074 new_range->next = slot;
1075 (*tab)[pass->static_pass_number] = new_range;
1076 if (is_enable)
1077 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1078 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1079 else
1080 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1081 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1083 else
1085 char *next_range = NULL;
1086 char *one_range = range_str;
1087 char *end_val = NULL;
1091 uid_range_p slot;
1092 uid_range_p new_range;
1093 char *invalid = NULL;
1094 long start;
1095 char *func_name = NULL;
1097 next_range = strchr (one_range, ',');
1098 if (next_range)
1100 *next_range = '\0';
1101 next_range++;
1104 end_val = strchr (one_range, ':');
1105 if (end_val)
1107 *end_val = '\0';
1108 end_val++;
1110 start = strtol (one_range, &invalid, 10);
1111 if (*invalid || start < 0)
1113 if (end_val || (one_range[0] >= '0'
1114 && one_range[0] <= '9'))
1116 error ("Invalid range %s in option %s",
1117 one_range,
1118 is_enable ? "-fenable" : "-fdisable");
1119 free (argstr);
1120 return;
1122 func_name = one_range;
1124 if (!end_val)
1126 new_range = XCNEW (struct uid_range);
1127 if (!func_name)
1129 new_range->start = (unsigned) start;
1130 new_range->last = (unsigned) start;
1132 else
1134 new_range->start = (unsigned) -1;
1135 new_range->last = (unsigned) -1;
1136 new_range->assem_name = xstrdup (func_name);
1139 else
1141 long last = strtol (end_val, &invalid, 10);
1142 if (*invalid || last < start)
1144 error ("Invalid range %s in option %s",
1145 end_val,
1146 is_enable ? "-fenable" : "-fdisable");
1147 free (argstr);
1148 return;
1150 new_range = XCNEW (struct uid_range);
1151 new_range->start = (unsigned) start;
1152 new_range->last = (unsigned) last;
1155 slot = (*tab)[pass->static_pass_number];
1156 new_range->next = slot;
1157 (*tab)[pass->static_pass_number] = new_range;
1158 if (is_enable)
1160 if (new_range->assem_name)
1161 inform (UNKNOWN_LOCATION,
1162 "enable pass %s for function %s",
1163 phase_name, new_range->assem_name);
1164 else
1165 inform (UNKNOWN_LOCATION,
1166 "enable pass %s for functions in the range of [%u, %u]",
1167 phase_name, new_range->start, new_range->last);
1169 else
1171 if (new_range->assem_name)
1172 inform (UNKNOWN_LOCATION,
1173 "disable pass %s for function %s",
1174 phase_name, new_range->assem_name);
1175 else
1176 inform (UNKNOWN_LOCATION,
1177 "disable pass %s for functions in the range of [%u, %u]",
1178 phase_name, new_range->start, new_range->last);
1181 one_range = next_range;
1182 } while (next_range);
1185 free (argstr);
1188 /* Enable pass specified by ARG. */
1190 void
1191 enable_pass (const char *arg)
1193 enable_disable_pass (arg, true);
1196 /* Disable pass specified by ARG. */
1198 void
1199 disable_pass (const char *arg)
1201 enable_disable_pass (arg, false);
1204 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1206 static bool
1207 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1208 tree func,
1209 vec<uid_range_p> tab)
1211 uid_range_p slot, range;
1212 int cgraph_uid;
1213 const char *aname = NULL;
1215 if (!tab.exists ()
1216 || (unsigned) pass->static_pass_number >= tab.length ()
1217 || pass->static_pass_number == -1)
1218 return false;
1220 slot = tab[pass->static_pass_number];
1221 if (!slot)
1222 return false;
1224 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1225 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1226 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1228 range = slot;
1229 while (range)
1231 if ((unsigned) cgraph_uid >= range->start
1232 && (unsigned) cgraph_uid <= range->last)
1233 return true;
1234 if (range->assem_name && aname
1235 && !strcmp (range->assem_name, aname))
1236 return true;
1237 range = range->next;
1240 return false;
1244 /* Update static_pass_number for passes (and the flag
1245 TODO_mark_first_instance).
1247 Passes are constructed with static_pass_number preinitialized to 0
1249 This field is used in two different ways: initially as instance numbers
1250 of their kind, and then as ids within the entire pass manager.
1252 Within pass_manager::pass_manager:
1254 * In add_pass_instance(), as called by next_pass_1 in
1255 NEXT_PASS in init_optimization_passes
1257 * When the initial instance of a pass within a pass manager is seen,
1258 it is flagged, and its static_pass_number is set to -1
1260 * On subsequent times that it is seen, the static pass number
1261 is decremented each time, so that if there are e.g. 4 dups,
1262 they have static_pass_number -4, 2, 3, 4 respectively (note
1263 how the initial one is negative and gives the count); these
1264 can be thought of as instance numbers of the specific pass
1266 * Within the register_dump_files () traversal, set_pass_for_id()
1267 is called on each pass, using these instance numbers to create
1268 dumpfile switches, and then overwriting them with a pass id,
1269 which are global to the whole pass manager (based on
1270 (TDI_end + current value of extra_dump_files_in_use) ) */
1272 static void
1273 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1274 opt_pass *initial_pass)
1276 /* Are we dealing with the first pass of its kind, or a clone? */
1277 if (new_pass != initial_pass)
1279 /* We're dealing with a clone. */
1280 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1282 /* Indicate to register_dump_files that this pass has duplicates,
1283 and so it should rename the dump file. The first instance will
1284 be -1, and be number of duplicates = -static_pass_number - 1.
1285 Subsequent instances will be > 0 and just the duplicate number. */
1286 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1288 initial_pass->static_pass_number -= 1;
1289 new_pass->static_pass_number = -initial_pass->static_pass_number;
1292 else
1294 /* We're dealing with the first pass of its kind. */
1295 new_pass->todo_flags_start |= TODO_mark_first_instance;
1296 new_pass->static_pass_number = -1;
1298 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1302 /* Add a pass to the pass list. Duplicate the pass if it's already
1303 in the list. */
1305 static opt_pass **
1306 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1308 /* Every pass should have a name so that plugins can refer to them. */
1309 gcc_assert (pass->name != NULL);
1311 add_pass_instance (pass, false, initial_pass);
1312 *list = pass;
1314 return &(*list)->next;
1317 /* List node for an inserted pass instance. We need to keep track of all
1318 the newly-added pass instances (with 'added_pass_nodes' defined below)
1319 so that we can register their dump files after pass-positioning is finished.
1320 Registering dumping files needs to be post-processed or the
1321 static_pass_number of the opt_pass object would be modified and mess up
1322 the dump file names of future pass instances to be added. */
1324 struct pass_list_node
1326 opt_pass *pass;
1327 struct pass_list_node *next;
1330 static struct pass_list_node *added_pass_nodes = NULL;
1331 static struct pass_list_node *prev_added_pass_node;
1333 /* Insert the pass at the proper position. Return true if the pass
1334 is successfully added.
1336 NEW_PASS_INFO - new pass to be inserted
1337 PASS_LIST - root of the pass list to insert the new pass to */
1339 static bool
1340 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1342 opt_pass *pass = *pass_list, *prev_pass = NULL;
1343 bool success = false;
1345 for ( ; pass; prev_pass = pass, pass = pass->next)
1347 /* Check if the current pass is of the same type as the new pass and
1348 matches the name and the instance number of the reference pass. */
1349 if (pass->type == new_pass_info->pass->type
1350 && pass->name
1351 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1352 && ((new_pass_info->ref_pass_instance_number == 0)
1353 || (new_pass_info->ref_pass_instance_number ==
1354 pass->static_pass_number)
1355 || (new_pass_info->ref_pass_instance_number == 1
1356 && pass->todo_flags_start & TODO_mark_first_instance)))
1358 opt_pass *new_pass;
1359 struct pass_list_node *new_pass_node;
1361 if (new_pass_info->ref_pass_instance_number == 0)
1363 new_pass = new_pass_info->pass->clone ();
1364 add_pass_instance (new_pass, true, new_pass_info->pass);
1366 else
1368 new_pass = new_pass_info->pass;
1369 add_pass_instance (new_pass, true, new_pass);
1372 /* Insert the new pass instance based on the positioning op. */
1373 switch (new_pass_info->pos_op)
1375 case PASS_POS_INSERT_AFTER:
1376 new_pass->next = pass->next;
1377 pass->next = new_pass;
1379 /* Skip newly inserted pass to avoid repeated
1380 insertions in the case where the new pass and the
1381 existing one have the same name. */
1382 pass = new_pass;
1383 break;
1384 case PASS_POS_INSERT_BEFORE:
1385 new_pass->next = pass;
1386 if (prev_pass)
1387 prev_pass->next = new_pass;
1388 else
1389 *pass_list = new_pass;
1390 break;
1391 case PASS_POS_REPLACE:
1392 new_pass->next = pass->next;
1393 if (prev_pass)
1394 prev_pass->next = new_pass;
1395 else
1396 *pass_list = new_pass;
1397 new_pass->sub = pass->sub;
1398 new_pass->tv_id = pass->tv_id;
1399 pass = new_pass;
1400 break;
1401 default:
1402 error ("invalid pass positioning operation");
1403 return false;
1406 /* Save the newly added pass (instance) in the added_pass_nodes
1407 list so that we can register its dump file later. Note that
1408 we cannot register the dump file now because doing so will modify
1409 the static_pass_number of the opt_pass object and therefore
1410 mess up the dump file name of future instances. */
1411 new_pass_node = XCNEW (struct pass_list_node);
1412 new_pass_node->pass = new_pass;
1413 if (!added_pass_nodes)
1414 added_pass_nodes = new_pass_node;
1415 else
1416 prev_added_pass_node->next = new_pass_node;
1417 prev_added_pass_node = new_pass_node;
1419 success = true;
1422 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1423 success = true;
1426 return success;
1429 /* Hooks a new pass into the pass lists.
1431 PASS_INFO - pass information that specifies the opt_pass object,
1432 reference pass, instance number, and how to position
1433 the pass */
1435 void
1436 register_pass (struct register_pass_info *pass_info)
1438 g->get_passes ()->register_pass (pass_info);
1441 void
1442 register_pass (opt_pass* pass, pass_positioning_ops pos,
1443 const char* ref_pass_name, int ref_pass_inst_number)
1445 register_pass_info i;
1446 i.pass = pass;
1447 i.reference_pass_name = ref_pass_name;
1448 i.ref_pass_instance_number = ref_pass_inst_number;
1449 i.pos_op = pos;
1451 g->get_passes ()->register_pass (&i);
1454 void
1455 pass_manager::register_pass (struct register_pass_info *pass_info)
1457 bool all_instances, success;
1458 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1460 /* The checks below could fail in buggy plugins. Existing GCC
1461 passes should never fail these checks, so we mention plugin in
1462 the messages. */
1463 if (!pass_info->pass)
1464 fatal_error (input_location, "plugin cannot register a missing pass");
1466 if (!pass_info->pass->name)
1467 fatal_error (input_location, "plugin cannot register an unnamed pass");
1469 if (!pass_info->reference_pass_name)
1470 fatal_error
1471 (input_location,
1472 "plugin cannot register pass %qs without reference pass name",
1473 pass_info->pass->name);
1475 /* Try to insert the new pass to the pass lists. We need to check
1476 all five lists as the reference pass could be in one (or all) of
1477 them. */
1478 all_instances = pass_info->ref_pass_instance_number == 0;
1479 success = position_pass (pass_info, &all_lowering_passes);
1480 if (!success || all_instances)
1481 success |= position_pass (pass_info, &all_small_ipa_passes);
1482 if (!success || all_instances)
1483 success |= position_pass (pass_info, &all_regular_ipa_passes);
1484 if (!success || all_instances)
1485 success |= position_pass (pass_info, &all_late_ipa_passes);
1486 if (!success || all_instances)
1487 success |= position_pass (pass_info, &all_passes);
1488 if (!success)
1489 fatal_error
1490 (input_location,
1491 "pass %qs not found but is referenced by new pass %qs",
1492 pass_info->reference_pass_name, pass_info->pass->name);
1494 /* OK, we have successfully inserted the new pass. We need to register
1495 the dump files for the newly added pass and its duplicates (if any).
1496 Because the registration of plugin/backend passes happens after the
1497 command-line options are parsed, the options that specify single
1498 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1499 passes. Therefore we currently can only enable dumping of
1500 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1501 are specified. While doing so, we also delete the pass_list_node
1502 objects created during pass positioning. */
1503 while (added_pass_nodes)
1505 struct pass_list_node *next_node = added_pass_nodes->next;
1506 enum tree_dump_index tdi;
1507 register_one_dump_file (added_pass_nodes->pass);
1508 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1509 || added_pass_nodes->pass->type == IPA_PASS)
1510 tdi = TDI_ipa_all;
1511 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1512 tdi = TDI_tree_all;
1513 else
1514 tdi = TDI_rtl_all;
1515 /* Check if dump-all flag is specified. */
1516 if (dumps->get_dump_file_info (tdi)->pstate)
1517 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1518 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1519 XDELETE (added_pass_nodes);
1520 added_pass_nodes = next_node;
1524 /* Construct the pass tree. The sequencing of passes is driven by
1525 the cgraph routines:
1527 finalize_compilation_unit ()
1528 for each node N in the cgraph
1529 cgraph_analyze_function (N)
1530 cgraph_lower_function (N) -> all_lowering_passes
1532 If we are optimizing, compile is then invoked:
1534 compile ()
1535 ipa_passes () -> all_small_ipa_passes
1536 -> Analysis of all_regular_ipa_passes
1537 * possible LTO streaming at copmilation time *
1538 -> Execution of all_regular_ipa_passes
1539 * possible LTO streaming at link time *
1540 -> all_late_ipa_passes
1541 expand_all_functions ()
1542 for each node N in the cgraph
1543 expand_function (N) -> Transformation of all_regular_ipa_passes
1544 -> all_passes
1547 void *
1548 pass_manager::operator new (size_t sz)
1550 /* Ensure that all fields of the pass manager are zero-initialized. */
1551 return xcalloc (1, sz);
1554 void
1555 pass_manager::operator delete (void *ptr)
1557 free (ptr);
1560 pass_manager::pass_manager (context *ctxt)
1561 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1562 all_regular_ipa_passes (NULL),
1563 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1564 m_ctxt (ctxt)
1566 opt_pass **p;
1568 /* Initialize the pass_lists array. */
1569 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1570 GCC_PASS_LISTS
1571 #undef DEF_PASS_LIST
1573 /* Build the tree of passes. */
1575 #define INSERT_PASSES_AFTER(PASS) \
1576 p = &(PASS);
1578 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1580 opt_pass **p = &(PASS ## _1)->sub;
1582 #define POP_INSERT_PASSES() \
1585 #define NEXT_PASS(PASS, NUM) \
1586 do { \
1587 gcc_assert (NULL == PASS ## _ ## NUM); \
1588 if ((NUM) == 1) \
1589 PASS ## _1 = make_##PASS (m_ctxt); \
1590 else \
1592 gcc_assert (PASS ## _1); \
1593 PASS ## _ ## NUM = PASS ## _1->clone (); \
1595 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1596 } while (0)
1598 #define TERMINATE_PASS_LIST() \
1599 *p = NULL;
1601 #include "pass-instances.def"
1603 #undef INSERT_PASSES_AFTER
1604 #undef PUSH_INSERT_PASSES_WITHIN
1605 #undef POP_INSERT_PASSES
1606 #undef NEXT_PASS
1607 #undef TERMINATE_PASS_LIST
1609 /* Register the passes with the tree dump code. */
1610 register_dump_files (all_lowering_passes);
1611 register_dump_files (all_small_ipa_passes);
1612 register_dump_files (all_regular_ipa_passes);
1613 register_dump_files (all_late_ipa_passes);
1614 register_dump_files (all_passes);
1617 static void
1618 delete_pass_tree (opt_pass *pass)
1620 while (pass)
1622 /* Recurse into child passes. */
1623 delete_pass_tree (pass->sub);
1625 opt_pass *next = pass->next;
1627 /* Delete this pass. */
1628 delete pass;
1630 /* Iterate onto sibling passes. */
1631 pass = next;
1635 pass_manager::~pass_manager ()
1637 XDELETEVEC (passes_by_id);
1639 /* Call delete_pass_tree on each of the pass_lists. */
1640 #define DEF_PASS_LIST(LIST) \
1641 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1642 GCC_PASS_LISTS
1643 #undef DEF_PASS_LIST
1647 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1648 function CALLBACK for every function in the call graph. Otherwise,
1649 call CALLBACK on the current function. */
1651 static void
1652 do_per_function (void (*callback) (function *, void *data), void *data)
1654 if (current_function_decl)
1655 callback (cfun, data);
1656 else
1658 struct cgraph_node *node;
1659 FOR_EACH_DEFINED_FUNCTION (node)
1660 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1661 && (!node->clone_of || node->decl != node->clone_of->decl))
1662 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1666 /* Because inlining might remove no-longer reachable nodes, we need to
1667 keep the array visible to garbage collector to avoid reading collected
1668 out nodes. */
1669 static int nnodes;
1670 static GTY ((length ("nnodes"))) cgraph_node **order;
1672 /* Hook called when NODE is removed and therefore should be
1673 excluded from order vector. DATA is an array of integers.
1674 DATA[0] holds max index it may be accessed by. For cgraph
1675 node DATA[node->uid + 1] holds index of this node in order
1676 vector. */
1677 static void
1678 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1680 int *order_idx = (int *)data;
1682 if (node->uid >= order_idx[0])
1683 return;
1685 int idx = order_idx[node->uid + 1];
1686 if (idx >= 0 && idx < nnodes && order[idx] == node)
1687 order[idx] = NULL;
1690 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1691 function CALLBACK for every function in the call graph. Otherwise,
1692 call CALLBACK on the current function.
1693 This function is global so that plugins can use it. */
1694 void
1695 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1697 int i;
1699 if (current_function_decl)
1700 callback (cfun, data);
1701 else
1703 cgraph_node_hook_list *hook;
1704 int *order_idx;
1705 gcc_assert (!order);
1706 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1708 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1709 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1710 order_idx[0] = symtab->cgraph_max_uid;
1712 nnodes = ipa_reverse_postorder (order);
1713 for (i = nnodes - 1; i >= 0; i--)
1715 order[i]->process = 1;
1716 order_idx[order[i]->uid + 1] = i;
1718 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1719 order_idx);
1720 for (i = nnodes - 1; i >= 0; i--)
1722 /* Function could be inlined and removed as unreachable. */
1723 if (!order[i])
1724 continue;
1726 struct cgraph_node *node = order[i];
1728 /* Allow possibly removed nodes to be garbage collected. */
1729 order[i] = NULL;
1730 node->process = 0;
1731 if (node->has_gimple_body_p ())
1732 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1734 symtab->remove_cgraph_removal_hook (hook);
1736 ggc_free (order);
1737 order = NULL;
1738 nnodes = 0;
1741 /* Helper function to perform function body dump. */
1743 static void
1744 execute_function_dump (function *fn, void *data)
1746 opt_pass *pass = (opt_pass *)data;
1748 if (dump_file)
1750 push_cfun (fn);
1752 if (fn->curr_properties & PROP_trees)
1753 dump_function_to_file (fn->decl, dump_file, dump_flags);
1754 else
1755 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1757 /* Flush the file. If verification fails, we won't be able to
1758 close the file before aborting. */
1759 fflush (dump_file);
1761 if ((fn->curr_properties & PROP_cfg)
1762 && (dump_flags & TDF_GRAPH))
1764 if (!pass->graph_dump_initialized)
1766 clean_graph_dump_file (dump_file_name);
1767 pass->graph_dump_initialized = true;
1769 print_graph_cfg (dump_file_name, fn);
1772 pop_cfun ();
1776 static struct profile_record *profile_record;
1778 /* Do profile consistency book-keeping for the pass with static number INDEX.
1779 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1780 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1781 if we are only book-keeping on passes that may have selectively disabled
1782 themselves on a given function. */
1783 static void
1784 check_profile_consistency (int index, int subpass, bool run)
1786 pass_manager *passes = g->get_passes ();
1787 if (index == -1)
1788 return;
1789 if (!profile_record)
1790 profile_record = XCNEWVEC (struct profile_record,
1791 passes->passes_by_id_size);
1792 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1793 gcc_assert (subpass < 2);
1794 profile_record[index].run |= run;
1795 account_profile_record (&profile_record[index], subpass);
1798 /* Output profile consistency. */
1800 void
1801 dump_profile_report (void)
1803 g->get_passes ()->dump_profile_report ();
1806 void
1807 pass_manager::dump_profile_report () const
1809 int i, j;
1810 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1811 gcov_type last_time = 0, last_size = 0;
1812 double rel_time_change, rel_size_change;
1813 int last_reported = 0;
1815 if (!profile_record)
1816 return;
1817 fprintf (stderr, "\nProfile consistency report:\n\n");
1818 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1819 fprintf (stderr, " |freq count |freq count |size time\n");
1821 for (i = 0; i < passes_by_id_size; i++)
1822 for (j = 0 ; j < 2; j++)
1823 if (profile_record[i].run)
1825 if (last_time)
1826 rel_time_change = (profile_record[i].time[j]
1827 - (double)last_time) * 100 / (double)last_time;
1828 else
1829 rel_time_change = 0;
1830 if (last_size)
1831 rel_size_change = (profile_record[i].size[j]
1832 - (double)last_size) * 100 / (double)last_size;
1833 else
1834 rel_size_change = 0;
1836 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1837 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1838 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1839 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1840 || rel_time_change || rel_size_change)
1842 last_reported = i;
1843 fprintf (stderr, "%-20s %s",
1844 passes_by_id [i]->name,
1845 j ? "(after TODO)" : " ");
1846 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1847 fprintf (stderr, "| %+5i",
1848 profile_record[i].num_mismatched_freq_in[j]
1849 - last_freq_in);
1850 else
1851 fprintf (stderr, "| ");
1852 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1853 fprintf (stderr, " %+5i",
1854 profile_record[i].num_mismatched_count_in[j]
1855 - last_count_in);
1856 else
1857 fprintf (stderr, " ");
1858 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1859 fprintf (stderr, "| %+5i",
1860 profile_record[i].num_mismatched_freq_out[j]
1861 - last_freq_out);
1862 else
1863 fprintf (stderr, "| ");
1864 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1865 fprintf (stderr, " %+5i",
1866 profile_record[i].num_mismatched_count_out[j]
1867 - last_count_out);
1868 else
1869 fprintf (stderr, " ");
1871 /* Size/time units change across gimple and RTL. */
1872 if (i == pass_expand_1->static_pass_number)
1873 fprintf (stderr, "|----------");
1874 else
1876 if (rel_size_change)
1877 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1878 else
1879 fprintf (stderr, "| ");
1880 if (rel_time_change)
1881 fprintf (stderr, " %+8.4f%%", rel_time_change);
1883 fprintf (stderr, "\n");
1884 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1885 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1886 last_count_in = profile_record[i].num_mismatched_count_in[j];
1887 last_count_out = profile_record[i].num_mismatched_count_out[j];
1889 else if (j && last_reported != i)
1891 last_reported = i;
1892 fprintf (stderr, "%-20s ------------| | |\n",
1893 passes_by_id [i]->name);
1895 last_time = profile_record[i].time[j];
1896 last_size = profile_record[i].size[j];
1900 /* Perform all TODO actions that ought to be done on each function. */
1902 static void
1903 execute_function_todo (function *fn, void *data)
1905 bool from_ipa_pass = (cfun == NULL);
1906 unsigned int flags = (size_t)data;
1907 flags &= ~fn->last_verified;
1908 if (!flags)
1909 return;
1911 push_cfun (fn);
1913 /* Always cleanup the CFG before trying to update SSA. */
1914 if (flags & TODO_cleanup_cfg)
1916 cleanup_tree_cfg ();
1918 /* When cleanup_tree_cfg merges consecutive blocks, it may
1919 perform some simplistic propagation when removing single
1920 valued PHI nodes. This propagation may, in turn, cause the
1921 SSA form to become out-of-date (see PR 22037). So, even
1922 if the parent pass had not scheduled an SSA update, we may
1923 still need to do one. */
1924 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1925 flags |= TODO_update_ssa;
1928 if (flags & TODO_update_ssa_any)
1930 unsigned update_flags = flags & TODO_update_ssa_any;
1931 update_ssa (update_flags);
1934 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1935 compute_may_aliases ();
1937 if (optimize && (flags & TODO_update_address_taken))
1938 execute_update_addresses_taken ();
1940 if (flags & TODO_remove_unused_locals)
1941 remove_unused_locals ();
1943 if (flags & TODO_rebuild_frequencies)
1944 rebuild_frequencies ();
1946 if (flags & TODO_rebuild_cgraph_edges)
1947 cgraph_edge::rebuild_edges ();
1949 /* If we've seen errors do not bother running any verifiers. */
1950 if (!seen_error ())
1952 #if defined ENABLE_CHECKING
1953 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1954 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1956 if (flags & TODO_verify_il)
1958 if (cfun->curr_properties & PROP_trees)
1960 if (cfun->curr_properties & PROP_cfg)
1961 /* IPA passes leave stmts to be fixed up, so make sure to
1962 not verify stmts really throw. */
1963 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1964 else
1965 verify_gimple_in_seq (gimple_body (cfun->decl));
1967 if (cfun->curr_properties & PROP_ssa)
1968 /* IPA passes leave stmts to be fixed up, so make sure to
1969 not verify SSA operands whose verifier will choke on that. */
1970 verify_ssa (true, !from_ipa_pass);
1971 /* IPA passes leave basic-blocks unsplit, so make sure to
1972 not trip on that. */
1973 if ((cfun->curr_properties & PROP_cfg)
1974 && !from_ipa_pass)
1975 verify_flow_info ();
1976 if (current_loops
1977 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1978 verify_loop_closed_ssa (false);
1979 if (cfun->curr_properties & PROP_rtl)
1980 verify_rtl_sharing ();
1983 /* Make sure verifiers don't change dominator state. */
1984 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1985 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1986 #endif
1989 fn->last_verified = flags & TODO_verify_all;
1991 pop_cfun ();
1993 /* For IPA passes make sure to release dominator info, it can be
1994 computed by non-verifying TODOs. */
1995 if (from_ipa_pass)
1997 free_dominance_info (fn, CDI_DOMINATORS);
1998 free_dominance_info (fn, CDI_POST_DOMINATORS);
2002 /* Perform all TODO actions. */
2003 static void
2004 execute_todo (unsigned int flags)
2006 #if defined ENABLE_CHECKING
2007 if (cfun
2008 && need_ssa_update_p (cfun))
2009 gcc_assert (flags & TODO_update_ssa_any);
2010 #endif
2012 timevar_push (TV_TODO);
2014 /* Inform the pass whether it is the first time it is run. */
2015 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2017 statistics_fini_pass ();
2019 if (flags)
2020 do_per_function (execute_function_todo, (void *)(size_t) flags);
2022 /* Always remove functions just as before inlining: IPA passes might be
2023 interested to see bodies of extern inline functions that are not inlined
2024 to analyze side effects. The full removal is done just at the end
2025 of IPA pass queue. */
2026 if (flags & TODO_remove_functions)
2028 gcc_assert (!cfun);
2029 symtab->remove_unreachable_nodes (dump_file);
2032 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2034 gcc_assert (!cfun);
2035 symtab_node::dump_table (dump_file);
2036 /* Flush the file. If verification fails, we won't be able to
2037 close the file before aborting. */
2038 fflush (dump_file);
2041 /* Now that the dumping has been done, we can get rid of the optional
2042 df problems. */
2043 if (flags & TODO_df_finish)
2044 df_finish_pass ((flags & TODO_df_verify) != 0);
2046 timevar_pop (TV_TODO);
2049 /* Verify invariants that should hold between passes. This is a place
2050 to put simple sanity checks. */
2052 static void
2053 verify_interpass_invariants (void)
2055 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2058 /* Clear the last verified flag. */
2060 static void
2061 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2063 fn->last_verified = 0;
2066 /* Helper function. Verify that the properties has been turn into the
2067 properties expected by the pass. */
2069 #ifdef ENABLE_CHECKING
2070 static void
2071 verify_curr_properties (function *fn, void *data)
2073 unsigned int props = (size_t)data;
2074 gcc_assert ((fn->curr_properties & props) == props);
2076 #endif
2078 /* Initialize pass dump file. */
2079 /* This is non-static so that the plugins can use it. */
2081 bool
2082 pass_init_dump_file (opt_pass *pass)
2084 /* If a dump file name is present, open it if enabled. */
2085 if (pass->static_pass_number != -1)
2087 timevar_push (TV_DUMP);
2088 gcc::dump_manager *dumps = g->get_dumps ();
2089 bool initializing_dump =
2090 !dumps->dump_initialized_p (pass->static_pass_number);
2091 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2092 dumps->dump_start (pass->static_pass_number, &dump_flags);
2093 if (dump_file && current_function_decl)
2094 dump_function_header (dump_file, current_function_decl, dump_flags);
2095 if (initializing_dump
2096 && dump_file && (dump_flags & TDF_GRAPH)
2097 && cfun && (cfun->curr_properties & PROP_cfg))
2099 clean_graph_dump_file (dump_file_name);
2100 pass->graph_dump_initialized = true;
2102 timevar_pop (TV_DUMP);
2103 return initializing_dump;
2105 else
2106 return false;
2109 /* Flush PASS dump file. */
2110 /* This is non-static so that plugins can use it. */
2112 void
2113 pass_fini_dump_file (opt_pass *pass)
2115 timevar_push (TV_DUMP);
2117 /* Flush and close dump file. */
2118 if (dump_file_name)
2120 free (CONST_CAST (char *, dump_file_name));
2121 dump_file_name = NULL;
2124 g->get_dumps ()->dump_finish (pass->static_pass_number);
2125 timevar_pop (TV_DUMP);
2128 /* After executing the pass, apply expected changes to the function
2129 properties. */
2131 static void
2132 update_properties_after_pass (function *fn, void *data)
2134 opt_pass *pass = (opt_pass *) data;
2135 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2136 & ~pass->properties_destroyed;
2139 /* Execute summary generation for all of the passes in IPA_PASS. */
2141 void
2142 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2144 while (ipa_pass)
2146 opt_pass *pass = ipa_pass;
2148 /* Execute all of the IPA_PASSes in the list. */
2149 if (ipa_pass->type == IPA_PASS
2150 && pass->gate (cfun)
2151 && ipa_pass->generate_summary)
2153 pass_init_dump_file (pass);
2155 /* If a timevar is present, start it. */
2156 if (pass->tv_id)
2157 timevar_push (pass->tv_id);
2159 current_pass = pass;
2160 ipa_pass->generate_summary ();
2162 /* Stop timevar. */
2163 if (pass->tv_id)
2164 timevar_pop (pass->tv_id);
2166 pass_fini_dump_file (pass);
2168 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2172 /* Execute IPA_PASS function transform on NODE. */
2174 static void
2175 execute_one_ipa_transform_pass (struct cgraph_node *node,
2176 ipa_opt_pass_d *ipa_pass)
2178 opt_pass *pass = ipa_pass;
2179 unsigned int todo_after = 0;
2181 current_pass = pass;
2182 if (!ipa_pass->function_transform)
2183 return;
2185 /* Note that the folders should only create gimple expressions.
2186 This is a hack until the new folder is ready. */
2187 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2189 pass_init_dump_file (pass);
2191 /* Run pre-pass verification. */
2192 execute_todo (ipa_pass->function_transform_todo_flags_start);
2194 /* If a timevar is present, start it. */
2195 if (pass->tv_id != TV_NONE)
2196 timevar_push (pass->tv_id);
2198 /* Do it! */
2199 todo_after = ipa_pass->function_transform (node);
2201 /* Stop timevar. */
2202 if (pass->tv_id != TV_NONE)
2203 timevar_pop (pass->tv_id);
2205 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2206 check_profile_consistency (pass->static_pass_number, 0, true);
2208 /* Run post-pass cleanup and verification. */
2209 execute_todo (todo_after);
2210 verify_interpass_invariants ();
2211 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2212 check_profile_consistency (pass->static_pass_number, 1, true);
2214 if (dump_file)
2215 do_per_function (execute_function_dump, NULL);
2216 pass_fini_dump_file (pass);
2218 current_pass = NULL;
2220 /* Signal this is a suitable GC collection point. */
2221 if (!(todo_after & TODO_do_not_ggc_collect))
2222 ggc_collect ();
2225 /* For the current function, execute all ipa transforms. */
2227 void
2228 execute_all_ipa_transforms (void)
2230 struct cgraph_node *node;
2231 if (!cfun)
2232 return;
2233 node = cgraph_node::get (current_function_decl);
2235 if (node->ipa_transforms_to_apply.exists ())
2237 unsigned int i;
2239 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2240 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2241 node->ipa_transforms_to_apply.release ();
2245 /* Check if PASS is explicitly disabled or enabled and return
2246 the gate status. FUNC is the function to be processed, and
2247 GATE_STATUS is the gate status determined by pass manager by
2248 default. */
2250 static bool
2251 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2253 bool explicitly_enabled = false;
2254 bool explicitly_disabled = false;
2256 explicitly_enabled
2257 = is_pass_explicitly_enabled_or_disabled (pass, func,
2258 enabled_pass_uid_range_tab);
2259 explicitly_disabled
2260 = is_pass_explicitly_enabled_or_disabled (pass, func,
2261 disabled_pass_uid_range_tab);
2263 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2265 return gate_status;
2269 /* Execute PASS. */
2271 bool
2272 execute_one_pass (opt_pass *pass)
2274 unsigned int todo_after = 0;
2276 bool gate_status;
2278 /* IPA passes are executed on whole program, so cfun should be NULL.
2279 Other passes need function context set. */
2280 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2281 gcc_assert (!cfun && !current_function_decl);
2282 else
2283 gcc_assert (cfun && current_function_decl);
2285 current_pass = pass;
2287 /* Check whether gate check should be avoided.
2288 User controls the value of the gate through the parameter "gate_status". */
2289 gate_status = pass->gate (cfun);
2290 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2292 /* Override gate with plugin. */
2293 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2295 if (!gate_status)
2297 /* Run so passes selectively disabling themselves on a given function
2298 are not miscounted. */
2299 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2301 check_profile_consistency (pass->static_pass_number, 0, false);
2302 check_profile_consistency (pass->static_pass_number, 1, false);
2304 current_pass = NULL;
2305 return false;
2308 /* Pass execution event trigger: useful to identify passes being
2309 executed. */
2310 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2312 if (!quiet_flag && !cfun)
2313 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2315 /* Note that the folders should only create gimple expressions.
2316 This is a hack until the new folder is ready. */
2317 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2319 pass_init_dump_file (pass);
2321 /* Run pre-pass verification. */
2322 execute_todo (pass->todo_flags_start);
2324 #ifdef ENABLE_CHECKING
2325 do_per_function (verify_curr_properties,
2326 (void *)(size_t)pass->properties_required);
2327 #endif
2329 /* If a timevar is present, start it. */
2330 if (pass->tv_id != TV_NONE)
2331 timevar_push (pass->tv_id);
2333 /* Do it! */
2334 todo_after = pass->execute (cfun);
2335 do_per_function (clear_last_verified, NULL);
2337 /* Stop timevar. */
2338 if (pass->tv_id != TV_NONE)
2339 timevar_pop (pass->tv_id);
2341 do_per_function (update_properties_after_pass, pass);
2343 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2344 check_profile_consistency (pass->static_pass_number, 0, true);
2346 /* Run post-pass cleanup and verification. */
2347 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2348 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2349 check_profile_consistency (pass->static_pass_number, 1, true);
2351 verify_interpass_invariants ();
2352 if (dump_file)
2353 do_per_function (execute_function_dump, pass);
2354 if (pass->type == IPA_PASS)
2356 struct cgraph_node *node;
2357 if (((ipa_opt_pass_d *)pass)->function_transform)
2358 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2359 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2362 if (!current_function_decl)
2363 symtab->process_new_functions ();
2365 pass_fini_dump_file (pass);
2367 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2368 gcc_assert (!(cfun->curr_properties & PROP_trees)
2369 || pass->type != RTL_PASS);
2371 current_pass = NULL;
2373 /* Signal this is a suitable GC collection point. */
2374 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2375 ggc_collect ();
2377 return true;
2380 static void
2381 execute_pass_list_1 (opt_pass *pass)
2385 gcc_assert (pass->type == GIMPLE_PASS
2386 || pass->type == RTL_PASS);
2387 if (execute_one_pass (pass) && pass->sub)
2388 execute_pass_list_1 (pass->sub);
2389 pass = pass->next;
2391 while (pass);
2394 void
2395 execute_pass_list (function *fn, opt_pass *pass)
2397 push_cfun (fn);
2398 execute_pass_list_1 (pass);
2399 if (fn->cfg)
2401 free_dominance_info (CDI_DOMINATORS);
2402 free_dominance_info (CDI_POST_DOMINATORS);
2404 pop_cfun ();
2407 /* Write out all LTO data. */
2408 static void
2409 write_lto (void)
2411 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2412 lto_output ();
2413 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2414 timevar_push (TV_IPA_LTO_DECL_OUT);
2415 produce_asm_for_decls ();
2416 timevar_pop (TV_IPA_LTO_DECL_OUT);
2419 /* Same as execute_pass_list but assume that subpasses of IPA passes
2420 are local passes. If SET is not NULL, write out summaries of only
2421 those node in SET. */
2423 static void
2424 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2426 while (pass)
2428 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2429 gcc_assert (!current_function_decl);
2430 gcc_assert (!cfun);
2431 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2432 if (pass->type == IPA_PASS
2433 && ipa_pass->write_summary
2434 && pass->gate (cfun))
2436 /* If a timevar is present, start it. */
2437 if (pass->tv_id)
2438 timevar_push (pass->tv_id);
2440 pass_init_dump_file (pass);
2442 current_pass = pass;
2443 ipa_pass->write_summary ();
2445 pass_fini_dump_file (pass);
2447 /* If a timevar is present, start it. */
2448 if (pass->tv_id)
2449 timevar_pop (pass->tv_id);
2452 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2453 ipa_write_summaries_2 (pass->sub, state);
2455 pass = pass->next;
2459 /* Helper function of ipa_write_summaries. Creates and destroys the
2460 decl state and calls ipa_write_summaries_2 for all passes that have
2461 summaries. SET is the set of nodes to be written. */
2463 static void
2464 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2466 pass_manager *passes = g->get_passes ();
2467 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2468 state->symtab_node_encoder = encoder;
2470 lto_output_init_mode_table ();
2471 lto_push_out_decl_state (state);
2473 gcc_assert (!flag_wpa);
2474 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2476 write_lto ();
2478 gcc_assert (lto_get_out_decl_state () == state);
2479 lto_pop_out_decl_state ();
2480 lto_delete_out_decl_state (state);
2483 /* Write out summaries for all the nodes in the callgraph. */
2485 void
2486 ipa_write_summaries (void)
2488 lto_symtab_encoder_t encoder;
2489 int i, order_pos;
2490 varpool_node *vnode;
2491 struct cgraph_node *node;
2492 struct cgraph_node **order;
2494 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2495 return;
2497 select_what_to_stream ();
2499 encoder = lto_symtab_encoder_new (false);
2501 /* Create the callgraph set in the same order used in
2502 cgraph_expand_all_functions. This mostly facilitates debugging,
2503 since it causes the gimple file to be processed in the same order
2504 as the source code. */
2505 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2506 order_pos = ipa_reverse_postorder (order);
2507 gcc_assert (order_pos == symtab->cgraph_count);
2509 for (i = order_pos - 1; i >= 0; i--)
2511 struct cgraph_node *node = order[i];
2513 if (node->has_gimple_body_p ())
2515 /* When streaming out references to statements as part of some IPA
2516 pass summary, the statements need to have uids assigned and the
2517 following does that for all the IPA passes here. Naturally, this
2518 ordering then matches the one IPA-passes get in their stmt_fixup
2519 hooks. */
2521 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2522 renumber_gimple_stmt_uids ();
2523 pop_cfun ();
2525 if (node->definition && node->need_lto_streaming)
2526 lto_set_symtab_encoder_in_partition (encoder, node);
2529 FOR_EACH_DEFINED_FUNCTION (node)
2530 if (node->alias && node->need_lto_streaming)
2531 lto_set_symtab_encoder_in_partition (encoder, node);
2532 FOR_EACH_DEFINED_VARIABLE (vnode)
2533 if (vnode->need_lto_streaming)
2534 lto_set_symtab_encoder_in_partition (encoder, vnode);
2536 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2538 free (order);
2541 /* Same as execute_pass_list but assume that subpasses of IPA passes
2542 are local passes. If SET is not NULL, write out optimization summaries of
2543 only those node in SET. */
2545 static void
2546 ipa_write_optimization_summaries_1 (opt_pass *pass,
2547 struct lto_out_decl_state *state)
2549 while (pass)
2551 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2552 gcc_assert (!current_function_decl);
2553 gcc_assert (!cfun);
2554 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2555 if (pass->type == IPA_PASS
2556 && ipa_pass->write_optimization_summary
2557 && pass->gate (cfun))
2559 /* If a timevar is present, start it. */
2560 if (pass->tv_id)
2561 timevar_push (pass->tv_id);
2563 pass_init_dump_file (pass);
2565 current_pass = pass;
2566 ipa_pass->write_optimization_summary ();
2568 pass_fini_dump_file (pass);
2570 /* If a timevar is present, start it. */
2571 if (pass->tv_id)
2572 timevar_pop (pass->tv_id);
2575 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2576 ipa_write_optimization_summaries_1 (pass->sub, state);
2578 pass = pass->next;
2582 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2583 NULL, write out all summaries of all nodes. */
2585 void
2586 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2588 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2589 lto_symtab_encoder_iterator lsei;
2590 state->symtab_node_encoder = encoder;
2592 lto_output_init_mode_table ();
2593 lto_push_out_decl_state (state);
2594 for (lsei = lsei_start_function_in_partition (encoder);
2595 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2597 struct cgraph_node *node = lsei_cgraph_node (lsei);
2598 /* When streaming out references to statements as part of some IPA
2599 pass summary, the statements need to have uids assigned.
2601 For functions newly born at WPA stage we need to initialize
2602 the uids here. */
2603 if (node->definition
2604 && gimple_has_body_p (node->decl))
2606 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2607 renumber_gimple_stmt_uids ();
2608 pop_cfun ();
2612 gcc_assert (flag_wpa);
2613 pass_manager *passes = g->get_passes ();
2614 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2616 write_lto ();
2618 gcc_assert (lto_get_out_decl_state () == state);
2619 lto_pop_out_decl_state ();
2620 lto_delete_out_decl_state (state);
2623 /* Same as execute_pass_list but assume that subpasses of IPA passes
2624 are local passes. */
2626 static void
2627 ipa_read_summaries_1 (opt_pass *pass)
2629 while (pass)
2631 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2633 gcc_assert (!current_function_decl);
2634 gcc_assert (!cfun);
2635 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2637 if (pass->gate (cfun))
2639 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2641 /* If a timevar is present, start it. */
2642 if (pass->tv_id)
2643 timevar_push (pass->tv_id);
2645 pass_init_dump_file (pass);
2647 current_pass = pass;
2648 ipa_pass->read_summary ();
2650 pass_fini_dump_file (pass);
2652 /* Stop timevar. */
2653 if (pass->tv_id)
2654 timevar_pop (pass->tv_id);
2657 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2658 ipa_read_summaries_1 (pass->sub);
2660 pass = pass->next;
2665 /* Read all the summaries for all_regular_ipa_passes. */
2667 void
2668 ipa_read_summaries (void)
2670 pass_manager *passes = g->get_passes ();
2671 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2674 /* Same as execute_pass_list but assume that subpasses of IPA passes
2675 are local passes. */
2677 static void
2678 ipa_read_optimization_summaries_1 (opt_pass *pass)
2680 while (pass)
2682 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2684 gcc_assert (!current_function_decl);
2685 gcc_assert (!cfun);
2686 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2688 if (pass->gate (cfun))
2690 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2692 /* If a timevar is present, start it. */
2693 if (pass->tv_id)
2694 timevar_push (pass->tv_id);
2696 pass_init_dump_file (pass);
2698 current_pass = pass;
2699 ipa_pass->read_optimization_summary ();
2701 pass_fini_dump_file (pass);
2703 /* Stop timevar. */
2704 if (pass->tv_id)
2705 timevar_pop (pass->tv_id);
2708 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2709 ipa_read_optimization_summaries_1 (pass->sub);
2711 pass = pass->next;
2715 /* Read all the summaries for all_regular_ipa_passes. */
2717 void
2718 ipa_read_optimization_summaries (void)
2720 pass_manager *passes = g->get_passes ();
2721 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2724 /* Same as execute_pass_list but assume that subpasses of IPA passes
2725 are local passes. */
2726 void
2727 execute_ipa_pass_list (opt_pass *pass)
2731 gcc_assert (!current_function_decl);
2732 gcc_assert (!cfun);
2733 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2734 if (execute_one_pass (pass) && pass->sub)
2736 if (pass->sub->type == GIMPLE_PASS)
2738 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2739 do_per_function_toporder ((void (*)(function *, void *))
2740 execute_pass_list,
2741 pass->sub);
2742 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2744 else if (pass->sub->type == SIMPLE_IPA_PASS
2745 || pass->sub->type == IPA_PASS)
2746 execute_ipa_pass_list (pass->sub);
2747 else
2748 gcc_unreachable ();
2750 gcc_assert (!current_function_decl);
2751 symtab->process_new_functions ();
2752 pass = pass->next;
2754 while (pass);
2757 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2759 static void
2760 execute_ipa_stmt_fixups (opt_pass *pass,
2761 struct cgraph_node *node, gimple *stmts)
2763 while (pass)
2765 /* Execute all of the IPA_PASSes in the list. */
2766 if (pass->type == IPA_PASS
2767 && pass->gate (cfun))
2769 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2771 if (ipa_pass->stmt_fixup)
2773 pass_init_dump_file (pass);
2774 /* If a timevar is present, start it. */
2775 if (pass->tv_id)
2776 timevar_push (pass->tv_id);
2778 current_pass = pass;
2779 ipa_pass->stmt_fixup (node, stmts);
2781 /* Stop timevar. */
2782 if (pass->tv_id)
2783 timevar_pop (pass->tv_id);
2784 pass_fini_dump_file (pass);
2786 if (pass->sub)
2787 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2789 pass = pass->next;
2793 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2795 void
2796 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2798 pass_manager *passes = g->get_passes ();
2799 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2803 extern void debug_properties (unsigned int);
2804 extern void dump_properties (FILE *, unsigned int);
2806 DEBUG_FUNCTION void
2807 dump_properties (FILE *dump, unsigned int props)
2809 fprintf (dump, "Properties:\n");
2810 if (props & PROP_gimple_any)
2811 fprintf (dump, "PROP_gimple_any\n");
2812 if (props & PROP_gimple_lcf)
2813 fprintf (dump, "PROP_gimple_lcf\n");
2814 if (props & PROP_gimple_leh)
2815 fprintf (dump, "PROP_gimple_leh\n");
2816 if (props & PROP_cfg)
2817 fprintf (dump, "PROP_cfg\n");
2818 if (props & PROP_ssa)
2819 fprintf (dump, "PROP_ssa\n");
2820 if (props & PROP_no_crit_edges)
2821 fprintf (dump, "PROP_no_crit_edges\n");
2822 if (props & PROP_rtl)
2823 fprintf (dump, "PROP_rtl\n");
2824 if (props & PROP_gimple_lomp)
2825 fprintf (dump, "PROP_gimple_lomp\n");
2826 if (props & PROP_gimple_lcx)
2827 fprintf (dump, "PROP_gimple_lcx\n");
2828 if (props & PROP_gimple_lvec)
2829 fprintf (dump, "PROP_gimple_lvec\n");
2830 if (props & PROP_cfglayout)
2831 fprintf (dump, "PROP_cfglayout\n");
2834 DEBUG_FUNCTION void
2835 debug_properties (unsigned int props)
2837 dump_properties (stderr, props);
2840 /* Called by local passes to see if function is called by already processed nodes.
2841 Because we process nodes in topological order, this means that function is
2842 in recursive cycle or we introduced new direct calls. */
2843 bool
2844 function_called_by_processed_nodes_p (void)
2846 struct cgraph_edge *e;
2847 for (e = cgraph_node::get (current_function_decl)->callers;
2849 e = e->next_caller)
2851 if (e->caller->decl == current_function_decl)
2852 continue;
2853 if (!e->caller->has_gimple_body_p ())
2854 continue;
2855 if (TREE_ASM_WRITTEN (e->caller->decl))
2856 continue;
2857 if (!e->caller->process && !e->caller->global.inlined_to)
2858 break;
2860 if (dump_file && e)
2862 fprintf (dump_file, "Already processed call to:\n");
2863 e->caller->dump (dump_file);
2865 return e != NULL;
2868 #include "gt-passes.h"