PR target/66563
[official-gcc.git] / gcc / passes.c
blobc66741e45b41337ed98dba37d253b420e44bb44a
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 /* Helper for pass_registry hash table. */
863 struct pass_registry_hasher : default_hashmap_traits
865 static inline hashval_t hash (const char *);
866 static inline bool equal_keys (const char *, const char *);
869 /* Pass registry hash function. */
871 inline hashval_t
872 pass_registry_hasher::hash (const char *name)
874 return htab_hash_string (name);
877 /* Hash equal function */
879 inline bool
880 pass_registry_hasher::equal_keys (const char *s1, const char *s2)
882 return !strcmp (s1, s2);
885 static hash_map<const char *, opt_pass *, pass_registry_hasher>
886 *name_to_pass_map;
888 /* Register PASS with NAME. */
890 static void
891 register_pass_name (opt_pass *pass, const char *name)
893 if (!name_to_pass_map)
894 name_to_pass_map
895 = new hash_map<const char *, opt_pass *, pass_registry_hasher> (256);
897 if (name_to_pass_map->get (name))
898 return; /* Ignore plugin passes. */
900 const char *unique_name = xstrdup (name);
901 name_to_pass_map->put (unique_name, pass);
904 /* Map from pass id to canonicalized pass name. */
906 typedef const char *char_ptr;
907 static vec<char_ptr> pass_tab = vNULL;
909 /* Callback function for traversing NAME_TO_PASS_MAP. */
911 bool
912 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
914 gcc_assert (pass->static_pass_number > 0);
915 gcc_assert (pass_tab.exists ());
917 pass_tab[pass->static_pass_number] = name;
919 return 1;
922 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
923 table for dumping purpose. */
925 static void
926 create_pass_tab (void)
928 if (!flag_dump_passes)
929 return;
931 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
932 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
935 static bool override_gate_status (opt_pass *, tree, bool);
937 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
938 is turned on or not. */
940 static void
941 dump_one_pass (opt_pass *pass, int pass_indent)
943 int indent = 3 * pass_indent;
944 const char *pn;
945 bool is_on, is_really_on;
947 is_on = pass->gate (cfun);
948 is_really_on = override_gate_status (pass, current_function_decl, is_on);
950 if (pass->static_pass_number <= 0)
951 pn = pass->name;
952 else
953 pn = pass_tab[pass->static_pass_number];
955 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
956 (15 - indent < 0 ? 0 : 15 - indent), " ",
957 is_on ? " ON" : " OFF",
958 ((!is_on) == (!is_really_on) ? ""
959 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
962 /* Dump pass list PASS with indentation INDENT. */
964 static void
965 dump_pass_list (opt_pass *pass, int indent)
969 dump_one_pass (pass, indent);
970 if (pass->sub)
971 dump_pass_list (pass->sub, indent + 1);
972 pass = pass->next;
974 while (pass);
977 /* Dump all optimization passes. */
979 void
980 dump_passes (void)
982 g->get_passes ()->dump_passes ();
985 void
986 pass_manager::dump_passes () const
988 push_dummy_function (true);
990 create_pass_tab ();
992 dump_pass_list (all_lowering_passes, 1);
993 dump_pass_list (all_small_ipa_passes, 1);
994 dump_pass_list (all_regular_ipa_passes, 1);
995 dump_pass_list (all_late_ipa_passes, 1);
996 dump_pass_list (all_passes, 1);
998 pop_dummy_function ();
1001 /* Returns the pass with NAME. */
1003 static opt_pass *
1004 get_pass_by_name (const char *name)
1006 opt_pass **p = name_to_pass_map->get (name);
1007 if (p)
1008 return *p;
1010 return NULL;
1014 /* Range [start, last]. */
1016 struct uid_range
1018 unsigned int start;
1019 unsigned int last;
1020 const char *assem_name;
1021 struct uid_range *next;
1024 typedef struct uid_range *uid_range_p;
1027 static vec<uid_range_p>
1028 enabled_pass_uid_range_tab = vNULL;
1029 static vec<uid_range_p>
1030 disabled_pass_uid_range_tab = vNULL;
1033 /* Parse option string for -fdisable- and -fenable-
1034 The syntax of the options:
1036 -fenable-<pass_name>
1037 -fdisable-<pass_name>
1039 -fenable-<pass_name>=s1:e1,s2:e2,...
1040 -fdisable-<pass_name>=s1:e1,s2:e2,...
1043 static void
1044 enable_disable_pass (const char *arg, bool is_enable)
1046 opt_pass *pass;
1047 char *range_str, *phase_name;
1048 char *argstr = xstrdup (arg);
1049 vec<uid_range_p> *tab = 0;
1051 range_str = strchr (argstr,'=');
1052 if (range_str)
1054 *range_str = '\0';
1055 range_str++;
1058 phase_name = argstr;
1059 if (!*phase_name)
1061 if (is_enable)
1062 error ("unrecognized option -fenable");
1063 else
1064 error ("unrecognized option -fdisable");
1065 free (argstr);
1066 return;
1068 pass = get_pass_by_name (phase_name);
1069 if (!pass || pass->static_pass_number == -1)
1071 if (is_enable)
1072 error ("unknown pass %s specified in -fenable", phase_name);
1073 else
1074 error ("unknown pass %s specified in -fdisable", phase_name);
1075 free (argstr);
1076 return;
1079 if (is_enable)
1080 tab = &enabled_pass_uid_range_tab;
1081 else
1082 tab = &disabled_pass_uid_range_tab;
1084 if ((unsigned) pass->static_pass_number >= tab->length ())
1085 tab->safe_grow_cleared (pass->static_pass_number + 1);
1087 if (!range_str)
1089 uid_range_p slot;
1090 uid_range_p new_range = XCNEW (struct uid_range);
1092 new_range->start = 0;
1093 new_range->last = (unsigned)-1;
1095 slot = (*tab)[pass->static_pass_number];
1096 new_range->next = slot;
1097 (*tab)[pass->static_pass_number] = new_range;
1098 if (is_enable)
1099 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1100 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1101 else
1102 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1103 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1105 else
1107 char *next_range = NULL;
1108 char *one_range = range_str;
1109 char *end_val = NULL;
1113 uid_range_p slot;
1114 uid_range_p new_range;
1115 char *invalid = NULL;
1116 long start;
1117 char *func_name = NULL;
1119 next_range = strchr (one_range, ',');
1120 if (next_range)
1122 *next_range = '\0';
1123 next_range++;
1126 end_val = strchr (one_range, ':');
1127 if (end_val)
1129 *end_val = '\0';
1130 end_val++;
1132 start = strtol (one_range, &invalid, 10);
1133 if (*invalid || start < 0)
1135 if (end_val || (one_range[0] >= '0'
1136 && one_range[0] <= '9'))
1138 error ("Invalid range %s in option %s",
1139 one_range,
1140 is_enable ? "-fenable" : "-fdisable");
1141 free (argstr);
1142 return;
1144 func_name = one_range;
1146 if (!end_val)
1148 new_range = XCNEW (struct uid_range);
1149 if (!func_name)
1151 new_range->start = (unsigned) start;
1152 new_range->last = (unsigned) start;
1154 else
1156 new_range->start = (unsigned) -1;
1157 new_range->last = (unsigned) -1;
1158 new_range->assem_name = xstrdup (func_name);
1161 else
1163 long last = strtol (end_val, &invalid, 10);
1164 if (*invalid || last < start)
1166 error ("Invalid range %s in option %s",
1167 end_val,
1168 is_enable ? "-fenable" : "-fdisable");
1169 free (argstr);
1170 return;
1172 new_range = XCNEW (struct uid_range);
1173 new_range->start = (unsigned) start;
1174 new_range->last = (unsigned) last;
1177 slot = (*tab)[pass->static_pass_number];
1178 new_range->next = slot;
1179 (*tab)[pass->static_pass_number] = new_range;
1180 if (is_enable)
1182 if (new_range->assem_name)
1183 inform (UNKNOWN_LOCATION,
1184 "enable pass %s for function %s",
1185 phase_name, new_range->assem_name);
1186 else
1187 inform (UNKNOWN_LOCATION,
1188 "enable pass %s for functions in the range of [%u, %u]",
1189 phase_name, new_range->start, new_range->last);
1191 else
1193 if (new_range->assem_name)
1194 inform (UNKNOWN_LOCATION,
1195 "disable pass %s for function %s",
1196 phase_name, new_range->assem_name);
1197 else
1198 inform (UNKNOWN_LOCATION,
1199 "disable pass %s for functions in the range of [%u, %u]",
1200 phase_name, new_range->start, new_range->last);
1203 one_range = next_range;
1204 } while (next_range);
1207 free (argstr);
1210 /* Enable pass specified by ARG. */
1212 void
1213 enable_pass (const char *arg)
1215 enable_disable_pass (arg, true);
1218 /* Disable pass specified by ARG. */
1220 void
1221 disable_pass (const char *arg)
1223 enable_disable_pass (arg, false);
1226 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1228 static bool
1229 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1230 tree func,
1231 vec<uid_range_p> tab)
1233 uid_range_p slot, range;
1234 int cgraph_uid;
1235 const char *aname = NULL;
1237 if (!tab.exists ()
1238 || (unsigned) pass->static_pass_number >= tab.length ()
1239 || pass->static_pass_number == -1)
1240 return false;
1242 slot = tab[pass->static_pass_number];
1243 if (!slot)
1244 return false;
1246 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1247 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1248 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1250 range = slot;
1251 while (range)
1253 if ((unsigned) cgraph_uid >= range->start
1254 && (unsigned) cgraph_uid <= range->last)
1255 return true;
1256 if (range->assem_name && aname
1257 && !strcmp (range->assem_name, aname))
1258 return true;
1259 range = range->next;
1262 return false;
1266 /* Update static_pass_number for passes (and the flag
1267 TODO_mark_first_instance).
1269 Passes are constructed with static_pass_number preinitialized to 0
1271 This field is used in two different ways: initially as instance numbers
1272 of their kind, and then as ids within the entire pass manager.
1274 Within pass_manager::pass_manager:
1276 * In add_pass_instance(), as called by next_pass_1 in
1277 NEXT_PASS in init_optimization_passes
1279 * When the initial instance of a pass within a pass manager is seen,
1280 it is flagged, and its static_pass_number is set to -1
1282 * On subsequent times that it is seen, the static pass number
1283 is decremented each time, so that if there are e.g. 4 dups,
1284 they have static_pass_number -4, 2, 3, 4 respectively (note
1285 how the initial one is negative and gives the count); these
1286 can be thought of as instance numbers of the specific pass
1288 * Within the register_dump_files () traversal, set_pass_for_id()
1289 is called on each pass, using these instance numbers to create
1290 dumpfile switches, and then overwriting them with a pass id,
1291 which are global to the whole pass manager (based on
1292 (TDI_end + current value of extra_dump_files_in_use) ) */
1294 static void
1295 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1296 opt_pass *initial_pass)
1298 /* Are we dealing with the first pass of its kind, or a clone? */
1299 if (new_pass != initial_pass)
1301 /* We're dealing with a clone. */
1302 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1304 /* Indicate to register_dump_files that this pass has duplicates,
1305 and so it should rename the dump file. The first instance will
1306 be -1, and be number of duplicates = -static_pass_number - 1.
1307 Subsequent instances will be > 0 and just the duplicate number. */
1308 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1310 initial_pass->static_pass_number -= 1;
1311 new_pass->static_pass_number = -initial_pass->static_pass_number;
1314 else
1316 /* We're dealing with the first pass of its kind. */
1317 new_pass->todo_flags_start |= TODO_mark_first_instance;
1318 new_pass->static_pass_number = -1;
1320 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1324 /* Add a pass to the pass list. Duplicate the pass if it's already
1325 in the list. */
1327 static opt_pass **
1328 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1330 /* Every pass should have a name so that plugins can refer to them. */
1331 gcc_assert (pass->name != NULL);
1333 add_pass_instance (pass, false, initial_pass);
1334 *list = pass;
1336 return &(*list)->next;
1339 /* List node for an inserted pass instance. We need to keep track of all
1340 the newly-added pass instances (with 'added_pass_nodes' defined below)
1341 so that we can register their dump files after pass-positioning is finished.
1342 Registering dumping files needs to be post-processed or the
1343 static_pass_number of the opt_pass object would be modified and mess up
1344 the dump file names of future pass instances to be added. */
1346 struct pass_list_node
1348 opt_pass *pass;
1349 struct pass_list_node *next;
1352 static struct pass_list_node *added_pass_nodes = NULL;
1353 static struct pass_list_node *prev_added_pass_node;
1355 /* Insert the pass at the proper position. Return true if the pass
1356 is successfully added.
1358 NEW_PASS_INFO - new pass to be inserted
1359 PASS_LIST - root of the pass list to insert the new pass to */
1361 static bool
1362 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1364 opt_pass *pass = *pass_list, *prev_pass = NULL;
1365 bool success = false;
1367 for ( ; pass; prev_pass = pass, pass = pass->next)
1369 /* Check if the current pass is of the same type as the new pass and
1370 matches the name and the instance number of the reference pass. */
1371 if (pass->type == new_pass_info->pass->type
1372 && pass->name
1373 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1374 && ((new_pass_info->ref_pass_instance_number == 0)
1375 || (new_pass_info->ref_pass_instance_number ==
1376 pass->static_pass_number)
1377 || (new_pass_info->ref_pass_instance_number == 1
1378 && pass->todo_flags_start & TODO_mark_first_instance)))
1380 opt_pass *new_pass;
1381 struct pass_list_node *new_pass_node;
1383 if (new_pass_info->ref_pass_instance_number == 0)
1385 new_pass = new_pass_info->pass->clone ();
1386 add_pass_instance (new_pass, true, new_pass_info->pass);
1388 else
1390 new_pass = new_pass_info->pass;
1391 add_pass_instance (new_pass, true, new_pass);
1394 /* Insert the new pass instance based on the positioning op. */
1395 switch (new_pass_info->pos_op)
1397 case PASS_POS_INSERT_AFTER:
1398 new_pass->next = pass->next;
1399 pass->next = new_pass;
1401 /* Skip newly inserted pass to avoid repeated
1402 insertions in the case where the new pass and the
1403 existing one have the same name. */
1404 pass = new_pass;
1405 break;
1406 case PASS_POS_INSERT_BEFORE:
1407 new_pass->next = pass;
1408 if (prev_pass)
1409 prev_pass->next = new_pass;
1410 else
1411 *pass_list = new_pass;
1412 break;
1413 case PASS_POS_REPLACE:
1414 new_pass->next = pass->next;
1415 if (prev_pass)
1416 prev_pass->next = new_pass;
1417 else
1418 *pass_list = new_pass;
1419 new_pass->sub = pass->sub;
1420 new_pass->tv_id = pass->tv_id;
1421 pass = new_pass;
1422 break;
1423 default:
1424 error ("invalid pass positioning operation");
1425 return false;
1428 /* Save the newly added pass (instance) in the added_pass_nodes
1429 list so that we can register its dump file later. Note that
1430 we cannot register the dump file now because doing so will modify
1431 the static_pass_number of the opt_pass object and therefore
1432 mess up the dump file name of future instances. */
1433 new_pass_node = XCNEW (struct pass_list_node);
1434 new_pass_node->pass = new_pass;
1435 if (!added_pass_nodes)
1436 added_pass_nodes = new_pass_node;
1437 else
1438 prev_added_pass_node->next = new_pass_node;
1439 prev_added_pass_node = new_pass_node;
1441 success = true;
1444 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1445 success = true;
1448 return success;
1451 /* Hooks a new pass into the pass lists.
1453 PASS_INFO - pass information that specifies the opt_pass object,
1454 reference pass, instance number, and how to position
1455 the pass */
1457 void
1458 register_pass (struct register_pass_info *pass_info)
1460 g->get_passes ()->register_pass (pass_info);
1463 void
1464 register_pass (opt_pass* pass, pass_positioning_ops pos,
1465 const char* ref_pass_name, int ref_pass_inst_number)
1467 register_pass_info i;
1468 i.pass = pass;
1469 i.reference_pass_name = ref_pass_name;
1470 i.ref_pass_instance_number = ref_pass_inst_number;
1471 i.pos_op = pos;
1473 g->get_passes ()->register_pass (&i);
1476 void
1477 pass_manager::register_pass (struct register_pass_info *pass_info)
1479 bool all_instances, success;
1480 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1482 /* The checks below could fail in buggy plugins. Existing GCC
1483 passes should never fail these checks, so we mention plugin in
1484 the messages. */
1485 if (!pass_info->pass)
1486 fatal_error (input_location, "plugin cannot register a missing pass");
1488 if (!pass_info->pass->name)
1489 fatal_error (input_location, "plugin cannot register an unnamed pass");
1491 if (!pass_info->reference_pass_name)
1492 fatal_error
1493 (input_location,
1494 "plugin cannot register pass %qs without reference pass name",
1495 pass_info->pass->name);
1497 /* Try to insert the new pass to the pass lists. We need to check
1498 all five lists as the reference pass could be in one (or all) of
1499 them. */
1500 all_instances = pass_info->ref_pass_instance_number == 0;
1501 success = position_pass (pass_info, &all_lowering_passes);
1502 if (!success || all_instances)
1503 success |= position_pass (pass_info, &all_small_ipa_passes);
1504 if (!success || all_instances)
1505 success |= position_pass (pass_info, &all_regular_ipa_passes);
1506 if (!success || all_instances)
1507 success |= position_pass (pass_info, &all_late_ipa_passes);
1508 if (!success || all_instances)
1509 success |= position_pass (pass_info, &all_passes);
1510 if (!success)
1511 fatal_error
1512 (input_location,
1513 "pass %qs not found but is referenced by new pass %qs",
1514 pass_info->reference_pass_name, pass_info->pass->name);
1516 /* OK, we have successfully inserted the new pass. We need to register
1517 the dump files for the newly added pass and its duplicates (if any).
1518 Because the registration of plugin/backend passes happens after the
1519 command-line options are parsed, the options that specify single
1520 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1521 passes. Therefore we currently can only enable dumping of
1522 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1523 are specified. While doing so, we also delete the pass_list_node
1524 objects created during pass positioning. */
1525 while (added_pass_nodes)
1527 struct pass_list_node *next_node = added_pass_nodes->next;
1528 enum tree_dump_index tdi;
1529 register_one_dump_file (added_pass_nodes->pass);
1530 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1531 || added_pass_nodes->pass->type == IPA_PASS)
1532 tdi = TDI_ipa_all;
1533 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1534 tdi = TDI_tree_all;
1535 else
1536 tdi = TDI_rtl_all;
1537 /* Check if dump-all flag is specified. */
1538 if (dumps->get_dump_file_info (tdi)->pstate)
1539 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1540 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1541 XDELETE (added_pass_nodes);
1542 added_pass_nodes = next_node;
1546 /* Construct the pass tree. The sequencing of passes is driven by
1547 the cgraph routines:
1549 finalize_compilation_unit ()
1550 for each node N in the cgraph
1551 cgraph_analyze_function (N)
1552 cgraph_lower_function (N) -> all_lowering_passes
1554 If we are optimizing, compile is then invoked:
1556 compile ()
1557 ipa_passes () -> all_small_ipa_passes
1558 -> Analysis of all_regular_ipa_passes
1559 * possible LTO streaming at copmilation time *
1560 -> Execution of all_regular_ipa_passes
1561 * possible LTO streaming at link time *
1562 -> all_late_ipa_passes
1563 expand_all_functions ()
1564 for each node N in the cgraph
1565 expand_function (N) -> Transformation of all_regular_ipa_passes
1566 -> all_passes
1569 void *
1570 pass_manager::operator new (size_t sz)
1572 /* Ensure that all fields of the pass manager are zero-initialized. */
1573 return xcalloc (1, sz);
1576 void
1577 pass_manager::operator delete (void *ptr)
1579 free (ptr);
1582 pass_manager::pass_manager (context *ctxt)
1583 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1584 all_regular_ipa_passes (NULL),
1585 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1586 m_ctxt (ctxt)
1588 opt_pass **p;
1590 /* Initialize the pass_lists array. */
1591 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1592 GCC_PASS_LISTS
1593 #undef DEF_PASS_LIST
1595 /* Build the tree of passes. */
1597 #define INSERT_PASSES_AFTER(PASS) \
1598 p = &(PASS);
1600 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1602 opt_pass **p = &(PASS ## _1)->sub;
1604 #define POP_INSERT_PASSES() \
1607 #define NEXT_PASS(PASS, NUM) \
1608 do { \
1609 gcc_assert (NULL == PASS ## _ ## NUM); \
1610 if ((NUM) == 1) \
1611 PASS ## _1 = make_##PASS (m_ctxt); \
1612 else \
1614 gcc_assert (PASS ## _1); \
1615 PASS ## _ ## NUM = PASS ## _1->clone (); \
1617 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1618 } while (0)
1620 #define TERMINATE_PASS_LIST() \
1621 *p = NULL;
1623 #include "pass-instances.def"
1625 #undef INSERT_PASSES_AFTER
1626 #undef PUSH_INSERT_PASSES_WITHIN
1627 #undef POP_INSERT_PASSES
1628 #undef NEXT_PASS
1629 #undef TERMINATE_PASS_LIST
1631 /* Register the passes with the tree dump code. */
1632 register_dump_files (all_lowering_passes);
1633 register_dump_files (all_small_ipa_passes);
1634 register_dump_files (all_regular_ipa_passes);
1635 register_dump_files (all_late_ipa_passes);
1636 register_dump_files (all_passes);
1639 static void
1640 delete_pass_tree (opt_pass *pass)
1642 while (pass)
1644 /* Recurse into child passes. */
1645 delete_pass_tree (pass->sub);
1647 opt_pass *next = pass->next;
1649 /* Delete this pass. */
1650 delete pass;
1652 /* Iterate onto sibling passes. */
1653 pass = next;
1657 pass_manager::~pass_manager ()
1659 XDELETEVEC (passes_by_id);
1661 /* Call delete_pass_tree on each of the pass_lists. */
1662 #define DEF_PASS_LIST(LIST) \
1663 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1664 GCC_PASS_LISTS
1665 #undef DEF_PASS_LIST
1669 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1670 function CALLBACK for every function in the call graph. Otherwise,
1671 call CALLBACK on the current function. */
1673 static void
1674 do_per_function (void (*callback) (function *, void *data), void *data)
1676 if (current_function_decl)
1677 callback (cfun, data);
1678 else
1680 struct cgraph_node *node;
1681 FOR_EACH_DEFINED_FUNCTION (node)
1682 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1683 && (!node->clone_of || node->decl != node->clone_of->decl))
1684 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1688 /* Because inlining might remove no-longer reachable nodes, we need to
1689 keep the array visible to garbage collector to avoid reading collected
1690 out nodes. */
1691 static int nnodes;
1692 static GTY ((length ("nnodes"))) cgraph_node **order;
1694 /* Hook called when NODE is removed and therefore should be
1695 excluded from order vector. DATA is an array of integers.
1696 DATA[0] holds max index it may be accessed by. For cgraph
1697 node DATA[node->uid + 1] holds index of this node in order
1698 vector. */
1699 static void
1700 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1702 int *order_idx = (int *)data;
1704 if (node->uid >= order_idx[0])
1705 return;
1707 int idx = order_idx[node->uid + 1];
1708 if (idx >= 0 && idx < nnodes && order[idx] == node)
1709 order[idx] = NULL;
1712 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1713 function CALLBACK for every function in the call graph. Otherwise,
1714 call CALLBACK on the current function.
1715 This function is global so that plugins can use it. */
1716 void
1717 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1719 int i;
1721 if (current_function_decl)
1722 callback (cfun, data);
1723 else
1725 cgraph_node_hook_list *hook;
1726 int *order_idx;
1727 gcc_assert (!order);
1728 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1730 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1731 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1732 order_idx[0] = symtab->cgraph_max_uid;
1734 nnodes = ipa_reverse_postorder (order);
1735 for (i = nnodes - 1; i >= 0; i--)
1737 order[i]->process = 1;
1738 order_idx[order[i]->uid + 1] = i;
1740 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1741 order_idx);
1742 for (i = nnodes - 1; i >= 0; i--)
1744 /* Function could be inlined and removed as unreachable. */
1745 if (!order[i])
1746 continue;
1748 struct cgraph_node *node = order[i];
1750 /* Allow possibly removed nodes to be garbage collected. */
1751 order[i] = NULL;
1752 node->process = 0;
1753 if (node->has_gimple_body_p ())
1754 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1756 symtab->remove_cgraph_removal_hook (hook);
1758 ggc_free (order);
1759 order = NULL;
1760 nnodes = 0;
1763 /* Helper function to perform function body dump. */
1765 static void
1766 execute_function_dump (function *fn, void *data)
1768 opt_pass *pass = (opt_pass *)data;
1770 if (dump_file)
1772 push_cfun (fn);
1774 if (fn->curr_properties & PROP_trees)
1775 dump_function_to_file (fn->decl, dump_file, dump_flags);
1776 else
1777 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1779 /* Flush the file. If verification fails, we won't be able to
1780 close the file before aborting. */
1781 fflush (dump_file);
1783 if ((fn->curr_properties & PROP_cfg)
1784 && (dump_flags & TDF_GRAPH))
1786 if (!pass->graph_dump_initialized)
1788 clean_graph_dump_file (dump_file_name);
1789 pass->graph_dump_initialized = true;
1791 print_graph_cfg (dump_file_name, fn);
1794 pop_cfun ();
1798 static struct profile_record *profile_record;
1800 /* Do profile consistency book-keeping for the pass with static number INDEX.
1801 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1802 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1803 if we are only book-keeping on passes that may have selectively disabled
1804 themselves on a given function. */
1805 static void
1806 check_profile_consistency (int index, int subpass, bool run)
1808 pass_manager *passes = g->get_passes ();
1809 if (index == -1)
1810 return;
1811 if (!profile_record)
1812 profile_record = XCNEWVEC (struct profile_record,
1813 passes->passes_by_id_size);
1814 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1815 gcc_assert (subpass < 2);
1816 profile_record[index].run |= run;
1817 account_profile_record (&profile_record[index], subpass);
1820 /* Output profile consistency. */
1822 void
1823 dump_profile_report (void)
1825 g->get_passes ()->dump_profile_report ();
1828 void
1829 pass_manager::dump_profile_report () const
1831 int i, j;
1832 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1833 gcov_type last_time = 0, last_size = 0;
1834 double rel_time_change, rel_size_change;
1835 int last_reported = 0;
1837 if (!profile_record)
1838 return;
1839 fprintf (stderr, "\nProfile consistency report:\n\n");
1840 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1841 fprintf (stderr, " |freq count |freq count |size time\n");
1843 for (i = 0; i < passes_by_id_size; i++)
1844 for (j = 0 ; j < 2; j++)
1845 if (profile_record[i].run)
1847 if (last_time)
1848 rel_time_change = (profile_record[i].time[j]
1849 - (double)last_time) * 100 / (double)last_time;
1850 else
1851 rel_time_change = 0;
1852 if (last_size)
1853 rel_size_change = (profile_record[i].size[j]
1854 - (double)last_size) * 100 / (double)last_size;
1855 else
1856 rel_size_change = 0;
1858 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1859 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1860 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1861 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1862 || rel_time_change || rel_size_change)
1864 last_reported = i;
1865 fprintf (stderr, "%-20s %s",
1866 passes_by_id [i]->name,
1867 j ? "(after TODO)" : " ");
1868 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1869 fprintf (stderr, "| %+5i",
1870 profile_record[i].num_mismatched_freq_in[j]
1871 - last_freq_in);
1872 else
1873 fprintf (stderr, "| ");
1874 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1875 fprintf (stderr, " %+5i",
1876 profile_record[i].num_mismatched_count_in[j]
1877 - last_count_in);
1878 else
1879 fprintf (stderr, " ");
1880 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1881 fprintf (stderr, "| %+5i",
1882 profile_record[i].num_mismatched_freq_out[j]
1883 - last_freq_out);
1884 else
1885 fprintf (stderr, "| ");
1886 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1887 fprintf (stderr, " %+5i",
1888 profile_record[i].num_mismatched_count_out[j]
1889 - last_count_out);
1890 else
1891 fprintf (stderr, " ");
1893 /* Size/time units change across gimple and RTL. */
1894 if (i == pass_expand_1->static_pass_number)
1895 fprintf (stderr, "|----------");
1896 else
1898 if (rel_size_change)
1899 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1900 else
1901 fprintf (stderr, "| ");
1902 if (rel_time_change)
1903 fprintf (stderr, " %+8.4f%%", rel_time_change);
1905 fprintf (stderr, "\n");
1906 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1907 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1908 last_count_in = profile_record[i].num_mismatched_count_in[j];
1909 last_count_out = profile_record[i].num_mismatched_count_out[j];
1911 else if (j && last_reported != i)
1913 last_reported = i;
1914 fprintf (stderr, "%-20s ------------| | |\n",
1915 passes_by_id [i]->name);
1917 last_time = profile_record[i].time[j];
1918 last_size = profile_record[i].size[j];
1922 /* Perform all TODO actions that ought to be done on each function. */
1924 static void
1925 execute_function_todo (function *fn, void *data)
1927 bool from_ipa_pass = (cfun == NULL);
1928 unsigned int flags = (size_t)data;
1929 flags &= ~fn->last_verified;
1930 if (!flags)
1931 return;
1933 push_cfun (fn);
1935 /* Always cleanup the CFG before trying to update SSA. */
1936 if (flags & TODO_cleanup_cfg)
1938 cleanup_tree_cfg ();
1940 /* When cleanup_tree_cfg merges consecutive blocks, it may
1941 perform some simplistic propagation when removing single
1942 valued PHI nodes. This propagation may, in turn, cause the
1943 SSA form to become out-of-date (see PR 22037). So, even
1944 if the parent pass had not scheduled an SSA update, we may
1945 still need to do one. */
1946 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1947 flags |= TODO_update_ssa;
1950 if (flags & TODO_update_ssa_any)
1952 unsigned update_flags = flags & TODO_update_ssa_any;
1953 update_ssa (update_flags);
1956 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1957 compute_may_aliases ();
1959 if (optimize && (flags & TODO_update_address_taken))
1960 execute_update_addresses_taken ();
1962 if (flags & TODO_remove_unused_locals)
1963 remove_unused_locals ();
1965 if (flags & TODO_rebuild_frequencies)
1966 rebuild_frequencies ();
1968 if (flags & TODO_rebuild_cgraph_edges)
1969 cgraph_edge::rebuild_edges ();
1971 /* If we've seen errors do not bother running any verifiers. */
1972 if (!seen_error ())
1974 #if defined ENABLE_CHECKING
1975 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1976 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1978 if (flags & TODO_verify_il)
1980 if (cfun->curr_properties & PROP_trees)
1982 if (cfun->curr_properties & PROP_cfg)
1983 /* IPA passes leave stmts to be fixed up, so make sure to
1984 not verify stmts really throw. */
1985 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1986 else
1987 verify_gimple_in_seq (gimple_body (cfun->decl));
1989 if (cfun->curr_properties & PROP_ssa)
1990 /* IPA passes leave stmts to be fixed up, so make sure to
1991 not verify SSA operands whose verifier will choke on that. */
1992 verify_ssa (true, !from_ipa_pass);
1993 /* IPA passes leave basic-blocks unsplit, so make sure to
1994 not trip on that. */
1995 if ((cfun->curr_properties & PROP_cfg)
1996 && !from_ipa_pass)
1997 verify_flow_info ();
1998 if (current_loops
1999 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
2000 verify_loop_closed_ssa (false);
2001 if (cfun->curr_properties & PROP_rtl)
2002 verify_rtl_sharing ();
2005 /* Make sure verifiers don't change dominator state. */
2006 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
2007 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
2008 #endif
2011 fn->last_verified = flags & TODO_verify_all;
2013 pop_cfun ();
2015 /* For IPA passes make sure to release dominator info, it can be
2016 computed by non-verifying TODOs. */
2017 if (from_ipa_pass)
2019 free_dominance_info (fn, CDI_DOMINATORS);
2020 free_dominance_info (fn, CDI_POST_DOMINATORS);
2024 /* Perform all TODO actions. */
2025 static void
2026 execute_todo (unsigned int flags)
2028 #if defined ENABLE_CHECKING
2029 if (cfun
2030 && need_ssa_update_p (cfun))
2031 gcc_assert (flags & TODO_update_ssa_any);
2032 #endif
2034 timevar_push (TV_TODO);
2036 /* Inform the pass whether it is the first time it is run. */
2037 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2039 statistics_fini_pass ();
2041 if (flags)
2042 do_per_function (execute_function_todo, (void *)(size_t) flags);
2044 /* Always remove functions just as before inlining: IPA passes might be
2045 interested to see bodies of extern inline functions that are not inlined
2046 to analyze side effects. The full removal is done just at the end
2047 of IPA pass queue. */
2048 if (flags & TODO_remove_functions)
2050 gcc_assert (!cfun);
2051 symtab->remove_unreachable_nodes (dump_file);
2054 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2056 gcc_assert (!cfun);
2057 symtab_node::dump_table (dump_file);
2058 /* Flush the file. If verification fails, we won't be able to
2059 close the file before aborting. */
2060 fflush (dump_file);
2063 /* Now that the dumping has been done, we can get rid of the optional
2064 df problems. */
2065 if (flags & TODO_df_finish)
2066 df_finish_pass ((flags & TODO_df_verify) != 0);
2068 timevar_pop (TV_TODO);
2071 /* Verify invariants that should hold between passes. This is a place
2072 to put simple sanity checks. */
2074 static void
2075 verify_interpass_invariants (void)
2077 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2080 /* Clear the last verified flag. */
2082 static void
2083 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2085 fn->last_verified = 0;
2088 /* Helper function. Verify that the properties has been turn into the
2089 properties expected by the pass. */
2091 #ifdef ENABLE_CHECKING
2092 static void
2093 verify_curr_properties (function *fn, void *data)
2095 unsigned int props = (size_t)data;
2096 gcc_assert ((fn->curr_properties & props) == props);
2098 #endif
2100 /* Initialize pass dump file. */
2101 /* This is non-static so that the plugins can use it. */
2103 bool
2104 pass_init_dump_file (opt_pass *pass)
2106 /* If a dump file name is present, open it if enabled. */
2107 if (pass->static_pass_number != -1)
2109 timevar_push (TV_DUMP);
2110 gcc::dump_manager *dumps = g->get_dumps ();
2111 bool initializing_dump =
2112 !dumps->dump_initialized_p (pass->static_pass_number);
2113 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2114 dumps->dump_start (pass->static_pass_number, &dump_flags);
2115 if (dump_file && current_function_decl)
2116 dump_function_header (dump_file, current_function_decl, dump_flags);
2117 if (initializing_dump
2118 && dump_file && (dump_flags & TDF_GRAPH)
2119 && cfun && (cfun->curr_properties & PROP_cfg))
2121 clean_graph_dump_file (dump_file_name);
2122 pass->graph_dump_initialized = true;
2124 timevar_pop (TV_DUMP);
2125 return initializing_dump;
2127 else
2128 return false;
2131 /* Flush PASS dump file. */
2132 /* This is non-static so that plugins can use it. */
2134 void
2135 pass_fini_dump_file (opt_pass *pass)
2137 timevar_push (TV_DUMP);
2139 /* Flush and close dump file. */
2140 if (dump_file_name)
2142 free (CONST_CAST (char *, dump_file_name));
2143 dump_file_name = NULL;
2146 g->get_dumps ()->dump_finish (pass->static_pass_number);
2147 timevar_pop (TV_DUMP);
2150 /* After executing the pass, apply expected changes to the function
2151 properties. */
2153 static void
2154 update_properties_after_pass (function *fn, void *data)
2156 opt_pass *pass = (opt_pass *) data;
2157 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2158 & ~pass->properties_destroyed;
2161 /* Execute summary generation for all of the passes in IPA_PASS. */
2163 void
2164 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2166 while (ipa_pass)
2168 opt_pass *pass = ipa_pass;
2170 /* Execute all of the IPA_PASSes in the list. */
2171 if (ipa_pass->type == IPA_PASS
2172 && pass->gate (cfun)
2173 && ipa_pass->generate_summary)
2175 pass_init_dump_file (pass);
2177 /* If a timevar is present, start it. */
2178 if (pass->tv_id)
2179 timevar_push (pass->tv_id);
2181 current_pass = pass;
2182 ipa_pass->generate_summary ();
2184 /* Stop timevar. */
2185 if (pass->tv_id)
2186 timevar_pop (pass->tv_id);
2188 pass_fini_dump_file (pass);
2190 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2194 /* Execute IPA_PASS function transform on NODE. */
2196 static void
2197 execute_one_ipa_transform_pass (struct cgraph_node *node,
2198 ipa_opt_pass_d *ipa_pass)
2200 opt_pass *pass = ipa_pass;
2201 unsigned int todo_after = 0;
2203 current_pass = pass;
2204 if (!ipa_pass->function_transform)
2205 return;
2207 /* Note that the folders should only create gimple expressions.
2208 This is a hack until the new folder is ready. */
2209 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2211 pass_init_dump_file (pass);
2213 /* Run pre-pass verification. */
2214 execute_todo (ipa_pass->function_transform_todo_flags_start);
2216 /* If a timevar is present, start it. */
2217 if (pass->tv_id != TV_NONE)
2218 timevar_push (pass->tv_id);
2220 /* Do it! */
2221 todo_after = ipa_pass->function_transform (node);
2223 /* Stop timevar. */
2224 if (pass->tv_id != TV_NONE)
2225 timevar_pop (pass->tv_id);
2227 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2228 check_profile_consistency (pass->static_pass_number, 0, true);
2230 /* Run post-pass cleanup and verification. */
2231 execute_todo (todo_after);
2232 verify_interpass_invariants ();
2233 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2234 check_profile_consistency (pass->static_pass_number, 1, true);
2236 if (dump_file)
2237 do_per_function (execute_function_dump, NULL);
2238 pass_fini_dump_file (pass);
2240 current_pass = NULL;
2242 /* Signal this is a suitable GC collection point. */
2243 if (!(todo_after & TODO_do_not_ggc_collect))
2244 ggc_collect ();
2247 /* For the current function, execute all ipa transforms. */
2249 void
2250 execute_all_ipa_transforms (void)
2252 struct cgraph_node *node;
2253 if (!cfun)
2254 return;
2255 node = cgraph_node::get (current_function_decl);
2257 if (node->ipa_transforms_to_apply.exists ())
2259 unsigned int i;
2261 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2262 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2263 node->ipa_transforms_to_apply.release ();
2267 /* Check if PASS is explicitly disabled or enabled and return
2268 the gate status. FUNC is the function to be processed, and
2269 GATE_STATUS is the gate status determined by pass manager by
2270 default. */
2272 static bool
2273 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2275 bool explicitly_enabled = false;
2276 bool explicitly_disabled = false;
2278 explicitly_enabled
2279 = is_pass_explicitly_enabled_or_disabled (pass, func,
2280 enabled_pass_uid_range_tab);
2281 explicitly_disabled
2282 = is_pass_explicitly_enabled_or_disabled (pass, func,
2283 disabled_pass_uid_range_tab);
2285 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2287 return gate_status;
2291 /* Execute PASS. */
2293 bool
2294 execute_one_pass (opt_pass *pass)
2296 unsigned int todo_after = 0;
2298 bool gate_status;
2300 /* IPA passes are executed on whole program, so cfun should be NULL.
2301 Other passes need function context set. */
2302 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2303 gcc_assert (!cfun && !current_function_decl);
2304 else
2305 gcc_assert (cfun && current_function_decl);
2307 current_pass = pass;
2309 /* Check whether gate check should be avoided.
2310 User controls the value of the gate through the parameter "gate_status". */
2311 gate_status = pass->gate (cfun);
2312 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2314 /* Override gate with plugin. */
2315 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2317 if (!gate_status)
2319 /* Run so passes selectively disabling themselves on a given function
2320 are not miscounted. */
2321 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2323 check_profile_consistency (pass->static_pass_number, 0, false);
2324 check_profile_consistency (pass->static_pass_number, 1, false);
2326 current_pass = NULL;
2327 return false;
2330 /* Pass execution event trigger: useful to identify passes being
2331 executed. */
2332 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2334 if (!quiet_flag && !cfun)
2335 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2337 /* Note that the folders should only create gimple expressions.
2338 This is a hack until the new folder is ready. */
2339 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2341 pass_init_dump_file (pass);
2343 /* Run pre-pass verification. */
2344 execute_todo (pass->todo_flags_start);
2346 #ifdef ENABLE_CHECKING
2347 do_per_function (verify_curr_properties,
2348 (void *)(size_t)pass->properties_required);
2349 #endif
2351 /* If a timevar is present, start it. */
2352 if (pass->tv_id != TV_NONE)
2353 timevar_push (pass->tv_id);
2355 /* Do it! */
2356 todo_after = pass->execute (cfun);
2357 do_per_function (clear_last_verified, NULL);
2359 /* Stop timevar. */
2360 if (pass->tv_id != TV_NONE)
2361 timevar_pop (pass->tv_id);
2363 do_per_function (update_properties_after_pass, pass);
2365 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2366 check_profile_consistency (pass->static_pass_number, 0, true);
2368 /* Run post-pass cleanup and verification. */
2369 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2370 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2371 check_profile_consistency (pass->static_pass_number, 1, true);
2373 verify_interpass_invariants ();
2374 if (dump_file)
2375 do_per_function (execute_function_dump, pass);
2376 if (pass->type == IPA_PASS)
2378 struct cgraph_node *node;
2379 if (((ipa_opt_pass_d *)pass)->function_transform)
2380 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2381 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2384 if (!current_function_decl)
2385 symtab->process_new_functions ();
2387 pass_fini_dump_file (pass);
2389 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2390 gcc_assert (!(cfun->curr_properties & PROP_trees)
2391 || pass->type != RTL_PASS);
2393 current_pass = NULL;
2395 /* Signal this is a suitable GC collection point. */
2396 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2397 ggc_collect ();
2399 return true;
2402 static void
2403 execute_pass_list_1 (opt_pass *pass)
2407 gcc_assert (pass->type == GIMPLE_PASS
2408 || pass->type == RTL_PASS);
2409 if (execute_one_pass (pass) && pass->sub)
2410 execute_pass_list_1 (pass->sub);
2411 pass = pass->next;
2413 while (pass);
2416 void
2417 execute_pass_list (function *fn, opt_pass *pass)
2419 push_cfun (fn);
2420 execute_pass_list_1 (pass);
2421 if (fn->cfg)
2423 free_dominance_info (CDI_DOMINATORS);
2424 free_dominance_info (CDI_POST_DOMINATORS);
2426 pop_cfun ();
2429 /* Write out all LTO data. */
2430 static void
2431 write_lto (void)
2433 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2434 lto_output ();
2435 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2436 timevar_push (TV_IPA_LTO_DECL_OUT);
2437 produce_asm_for_decls ();
2438 timevar_pop (TV_IPA_LTO_DECL_OUT);
2441 /* Same as execute_pass_list but assume that subpasses of IPA passes
2442 are local passes. If SET is not NULL, write out summaries of only
2443 those node in SET. */
2445 static void
2446 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2448 while (pass)
2450 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2451 gcc_assert (!current_function_decl);
2452 gcc_assert (!cfun);
2453 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2454 if (pass->type == IPA_PASS
2455 && ipa_pass->write_summary
2456 && pass->gate (cfun))
2458 /* If a timevar is present, start it. */
2459 if (pass->tv_id)
2460 timevar_push (pass->tv_id);
2462 pass_init_dump_file (pass);
2464 current_pass = pass;
2465 ipa_pass->write_summary ();
2467 pass_fini_dump_file (pass);
2469 /* If a timevar is present, start it. */
2470 if (pass->tv_id)
2471 timevar_pop (pass->tv_id);
2474 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2475 ipa_write_summaries_2 (pass->sub, state);
2477 pass = pass->next;
2481 /* Helper function of ipa_write_summaries. Creates and destroys the
2482 decl state and calls ipa_write_summaries_2 for all passes that have
2483 summaries. SET is the set of nodes to be written. */
2485 static void
2486 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2488 pass_manager *passes = g->get_passes ();
2489 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2490 state->symtab_node_encoder = encoder;
2492 lto_output_init_mode_table ();
2493 lto_push_out_decl_state (state);
2495 gcc_assert (!flag_wpa);
2496 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2498 write_lto ();
2500 gcc_assert (lto_get_out_decl_state () == state);
2501 lto_pop_out_decl_state ();
2502 lto_delete_out_decl_state (state);
2505 /* Write out summaries for all the nodes in the callgraph. */
2507 void
2508 ipa_write_summaries (void)
2510 lto_symtab_encoder_t encoder;
2511 int i, order_pos;
2512 varpool_node *vnode;
2513 struct cgraph_node *node;
2514 struct cgraph_node **order;
2516 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2517 return;
2519 select_what_to_stream ();
2521 encoder = lto_symtab_encoder_new (false);
2523 /* Create the callgraph set in the same order used in
2524 cgraph_expand_all_functions. This mostly facilitates debugging,
2525 since it causes the gimple file to be processed in the same order
2526 as the source code. */
2527 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2528 order_pos = ipa_reverse_postorder (order);
2529 gcc_assert (order_pos == symtab->cgraph_count);
2531 for (i = order_pos - 1; i >= 0; i--)
2533 struct cgraph_node *node = order[i];
2535 if (node->has_gimple_body_p ())
2537 /* When streaming out references to statements as part of some IPA
2538 pass summary, the statements need to have uids assigned and the
2539 following does that for all the IPA passes here. Naturally, this
2540 ordering then matches the one IPA-passes get in their stmt_fixup
2541 hooks. */
2543 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2544 renumber_gimple_stmt_uids ();
2545 pop_cfun ();
2547 if (node->definition && node->need_lto_streaming)
2548 lto_set_symtab_encoder_in_partition (encoder, node);
2551 FOR_EACH_DEFINED_FUNCTION (node)
2552 if (node->alias && node->need_lto_streaming)
2553 lto_set_symtab_encoder_in_partition (encoder, node);
2554 FOR_EACH_DEFINED_VARIABLE (vnode)
2555 if (vnode->need_lto_streaming)
2556 lto_set_symtab_encoder_in_partition (encoder, vnode);
2558 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2560 free (order);
2563 /* Same as execute_pass_list but assume that subpasses of IPA passes
2564 are local passes. If SET is not NULL, write out optimization summaries of
2565 only those node in SET. */
2567 static void
2568 ipa_write_optimization_summaries_1 (opt_pass *pass,
2569 struct lto_out_decl_state *state)
2571 while (pass)
2573 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2574 gcc_assert (!current_function_decl);
2575 gcc_assert (!cfun);
2576 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2577 if (pass->type == IPA_PASS
2578 && ipa_pass->write_optimization_summary
2579 && pass->gate (cfun))
2581 /* If a timevar is present, start it. */
2582 if (pass->tv_id)
2583 timevar_push (pass->tv_id);
2585 pass_init_dump_file (pass);
2587 current_pass = pass;
2588 ipa_pass->write_optimization_summary ();
2590 pass_fini_dump_file (pass);
2592 /* If a timevar is present, start it. */
2593 if (pass->tv_id)
2594 timevar_pop (pass->tv_id);
2597 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2598 ipa_write_optimization_summaries_1 (pass->sub, state);
2600 pass = pass->next;
2604 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2605 NULL, write out all summaries of all nodes. */
2607 void
2608 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2610 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2611 lto_symtab_encoder_iterator lsei;
2612 state->symtab_node_encoder = encoder;
2614 lto_output_init_mode_table ();
2615 lto_push_out_decl_state (state);
2616 for (lsei = lsei_start_function_in_partition (encoder);
2617 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2619 struct cgraph_node *node = lsei_cgraph_node (lsei);
2620 /* When streaming out references to statements as part of some IPA
2621 pass summary, the statements need to have uids assigned.
2623 For functions newly born at WPA stage we need to initialize
2624 the uids here. */
2625 if (node->definition
2626 && gimple_has_body_p (node->decl))
2628 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2629 renumber_gimple_stmt_uids ();
2630 pop_cfun ();
2634 gcc_assert (flag_wpa);
2635 pass_manager *passes = g->get_passes ();
2636 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2638 write_lto ();
2640 gcc_assert (lto_get_out_decl_state () == state);
2641 lto_pop_out_decl_state ();
2642 lto_delete_out_decl_state (state);
2645 /* Same as execute_pass_list but assume that subpasses of IPA passes
2646 are local passes. */
2648 static void
2649 ipa_read_summaries_1 (opt_pass *pass)
2651 while (pass)
2653 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2655 gcc_assert (!current_function_decl);
2656 gcc_assert (!cfun);
2657 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2659 if (pass->gate (cfun))
2661 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2663 /* If a timevar is present, start it. */
2664 if (pass->tv_id)
2665 timevar_push (pass->tv_id);
2667 pass_init_dump_file (pass);
2669 current_pass = pass;
2670 ipa_pass->read_summary ();
2672 pass_fini_dump_file (pass);
2674 /* Stop timevar. */
2675 if (pass->tv_id)
2676 timevar_pop (pass->tv_id);
2679 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2680 ipa_read_summaries_1 (pass->sub);
2682 pass = pass->next;
2687 /* Read all the summaries for all_regular_ipa_passes. */
2689 void
2690 ipa_read_summaries (void)
2692 pass_manager *passes = g->get_passes ();
2693 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2696 /* Same as execute_pass_list but assume that subpasses of IPA passes
2697 are local passes. */
2699 static void
2700 ipa_read_optimization_summaries_1 (opt_pass *pass)
2702 while (pass)
2704 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2706 gcc_assert (!current_function_decl);
2707 gcc_assert (!cfun);
2708 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2710 if (pass->gate (cfun))
2712 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2714 /* If a timevar is present, start it. */
2715 if (pass->tv_id)
2716 timevar_push (pass->tv_id);
2718 pass_init_dump_file (pass);
2720 current_pass = pass;
2721 ipa_pass->read_optimization_summary ();
2723 pass_fini_dump_file (pass);
2725 /* Stop timevar. */
2726 if (pass->tv_id)
2727 timevar_pop (pass->tv_id);
2730 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2731 ipa_read_optimization_summaries_1 (pass->sub);
2733 pass = pass->next;
2737 /* Read all the summaries for all_regular_ipa_passes. */
2739 void
2740 ipa_read_optimization_summaries (void)
2742 pass_manager *passes = g->get_passes ();
2743 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2746 /* Same as execute_pass_list but assume that subpasses of IPA passes
2747 are local passes. */
2748 void
2749 execute_ipa_pass_list (opt_pass *pass)
2753 gcc_assert (!current_function_decl);
2754 gcc_assert (!cfun);
2755 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2756 if (execute_one_pass (pass) && pass->sub)
2758 if (pass->sub->type == GIMPLE_PASS)
2760 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2761 do_per_function_toporder ((void (*)(function *, void *))
2762 execute_pass_list,
2763 pass->sub);
2764 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2766 else if (pass->sub->type == SIMPLE_IPA_PASS
2767 || pass->sub->type == IPA_PASS)
2768 execute_ipa_pass_list (pass->sub);
2769 else
2770 gcc_unreachable ();
2772 gcc_assert (!current_function_decl);
2773 symtab->process_new_functions ();
2774 pass = pass->next;
2776 while (pass);
2779 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2781 static void
2782 execute_ipa_stmt_fixups (opt_pass *pass,
2783 struct cgraph_node *node, gimple *stmts)
2785 while (pass)
2787 /* Execute all of the IPA_PASSes in the list. */
2788 if (pass->type == IPA_PASS
2789 && pass->gate (cfun))
2791 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2793 if (ipa_pass->stmt_fixup)
2795 pass_init_dump_file (pass);
2796 /* If a timevar is present, start it. */
2797 if (pass->tv_id)
2798 timevar_push (pass->tv_id);
2800 current_pass = pass;
2801 ipa_pass->stmt_fixup (node, stmts);
2803 /* Stop timevar. */
2804 if (pass->tv_id)
2805 timevar_pop (pass->tv_id);
2806 pass_fini_dump_file (pass);
2808 if (pass->sub)
2809 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2811 pass = pass->next;
2815 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2817 void
2818 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2820 pass_manager *passes = g->get_passes ();
2821 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2825 extern void debug_properties (unsigned int);
2826 extern void dump_properties (FILE *, unsigned int);
2828 DEBUG_FUNCTION void
2829 dump_properties (FILE *dump, unsigned int props)
2831 fprintf (dump, "Properties:\n");
2832 if (props & PROP_gimple_any)
2833 fprintf (dump, "PROP_gimple_any\n");
2834 if (props & PROP_gimple_lcf)
2835 fprintf (dump, "PROP_gimple_lcf\n");
2836 if (props & PROP_gimple_leh)
2837 fprintf (dump, "PROP_gimple_leh\n");
2838 if (props & PROP_cfg)
2839 fprintf (dump, "PROP_cfg\n");
2840 if (props & PROP_ssa)
2841 fprintf (dump, "PROP_ssa\n");
2842 if (props & PROP_no_crit_edges)
2843 fprintf (dump, "PROP_no_crit_edges\n");
2844 if (props & PROP_rtl)
2845 fprintf (dump, "PROP_rtl\n");
2846 if (props & PROP_gimple_lomp)
2847 fprintf (dump, "PROP_gimple_lomp\n");
2848 if (props & PROP_gimple_lcx)
2849 fprintf (dump, "PROP_gimple_lcx\n");
2850 if (props & PROP_gimple_lvec)
2851 fprintf (dump, "PROP_gimple_lvec\n");
2852 if (props & PROP_cfglayout)
2853 fprintf (dump, "PROP_cfglayout\n");
2856 DEBUG_FUNCTION void
2857 debug_properties (unsigned int props)
2859 dump_properties (stderr, props);
2862 /* Called by local passes to see if function is called by already processed nodes.
2863 Because we process nodes in topological order, this means that function is
2864 in recursive cycle or we introduced new direct calls. */
2865 bool
2866 function_called_by_processed_nodes_p (void)
2868 struct cgraph_edge *e;
2869 for (e = cgraph_node::get (current_function_decl)->callers;
2871 e = e->next_caller)
2873 if (e->caller->decl == current_function_decl)
2874 continue;
2875 if (!e->caller->has_gimple_body_p ())
2876 continue;
2877 if (TREE_ASM_WRITTEN (e->caller->decl))
2878 continue;
2879 if (!e->caller->process && !e->caller->global.inlined_to)
2880 break;
2882 if (dump_file && e)
2884 fprintf (dump_file, "Already processed call to:\n");
2885 e->caller->dump (dump_file);
2887 return e != NULL;
2890 #include "gt-passes.h"