2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / passes.c
blobd3ffe33540b886cf0a5f13c0000a72eb192b04b9
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 "line-map.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "tree.h"
34 #include "fold-const.h"
35 #include "varasm.h"
36 #include "rtl.h"
37 #include "tm_p.h"
38 #include "flags.h"
39 #include "insn-attr.h"
40 #include "insn-config.h"
41 #include "insn-flags.h"
42 #include "hard-reg-set.h"
43 #include "recog.h"
44 #include "output.h"
45 #include "except.h"
46 #include "function.h"
47 #include "toplev.h"
48 #include "expmed.h"
49 #include "dojump.h"
50 #include "explow.h"
51 #include "calls.h"
52 #include "emit-rtl.h"
53 #include "stmt.h"
54 #include "expr.h"
55 #include "predict.h"
56 #include "basic-block.h"
57 #include "intl.h"
58 #include "graph.h"
59 #include "regs.h"
60 #include "diagnostic-core.h"
61 #include "params.h"
62 #include "reload.h"
63 #include "debug.h"
64 #include "target.h"
65 #include "langhooks.h"
66 #include "cfgloop.h"
67 #include "hosthooks.h"
68 #include "opts.h"
69 #include "coverage.h"
70 #include "value-prof.h"
71 #include "tree-inline.h"
72 #include "tree-ssa-alias.h"
73 #include "internal-fn.h"
74 #include "gimple-expr.h"
75 #include "is-a.h"
76 #include "gimple.h"
77 #include "gimple-ssa.h"
78 #include "tree-cfg.h"
79 #include "stringpool.h"
80 #include "tree-ssanames.h"
81 #include "tree-ssa-loop-manip.h"
82 #include "tree-into-ssa.h"
83 #include "tree-dfa.h"
84 #include "tree-ssa.h"
85 #include "tree-pass.h"
86 #include "tree-dump.h"
87 #include "df.h"
88 #include "plugin-api.h"
89 #include "ipa-ref.h"
90 #include "cgraph.h"
91 #include "lto-streamer.h"
92 #include "plugin.h"
93 #include "ipa-utils.h"
94 #include "tree-pretty-print.h" /* for dump_function_header */
95 #include "context.h"
96 #include "pass_manager.h"
97 #include "dominance.h"
98 #include "cfg.h"
99 #include "cfgrtl.h"
100 #include "tree-ssa-live.h" /* For remove_unused_locals. */
101 #include "tree-cfgcleanup.h"
103 using namespace gcc;
105 /* This is used for debugging. It allows the current pass to printed
106 from anywhere in compilation.
107 The variable current_pass is also used for statistics and plugins. */
108 opt_pass *current_pass;
110 static void register_pass_name (opt_pass *, const char *);
112 /* Most passes are single-instance (within their context) and thus don't
113 need to implement cloning, but passes that support multiple instances
114 *must* provide their own implementation of the clone method.
116 Handle this by providing a default implemenation, but make it a fatal
117 error to call it. */
119 opt_pass *
120 opt_pass::clone ()
122 internal_error ("pass %s does not support cloning", name);
125 bool
126 opt_pass::gate (function *)
128 return true;
131 unsigned int
132 opt_pass::execute (function *)
134 return 0;
137 opt_pass::opt_pass (const pass_data &data, context *ctxt)
138 : pass_data (data),
139 sub (NULL),
140 next (NULL),
141 static_pass_number (0),
142 m_ctxt (ctxt)
147 void
148 pass_manager::execute_early_local_passes ()
150 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
151 if (flag_check_pointer_bounds)
152 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
153 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
156 unsigned int
157 pass_manager::execute_pass_mode_switching ()
159 return pass_mode_switching_1->execute (cfun);
163 /* Call from anywhere to find out what pass this is. Useful for
164 printing out debugging information deep inside an service
165 routine. */
166 void
167 print_current_pass (FILE *file)
169 if (current_pass)
170 fprintf (file, "current pass = %s (%d)\n",
171 current_pass->name, current_pass->static_pass_number);
172 else
173 fprintf (file, "no current pass.\n");
177 /* Call from the debugger to get the current pass name. */
178 DEBUG_FUNCTION void
179 debug_pass (void)
181 print_current_pass (stderr);
186 /* Global variables used to communicate with passes. */
187 bool in_gimple_form;
188 bool first_pass_instance;
191 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
192 and TYPE_DECL nodes.
194 This does nothing for local (non-static) variables, unless the
195 variable is a register variable with DECL_ASSEMBLER_NAME set. In
196 that case, or if the variable is not an automatic, it sets up the
197 RTL and outputs any assembler code (label definition, storage
198 allocation and initialization).
200 DECL is the declaration. TOP_LEVEL is nonzero
201 if this declaration is not within a function. */
203 void
204 rest_of_decl_compilation (tree decl,
205 int top_level,
206 int at_end)
208 bool finalize = true;
210 /* We deferred calling assemble_alias so that we could collect
211 other attributes such as visibility. Emit the alias now. */
212 if (!in_lto_p)
214 tree alias;
215 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
216 if (alias)
218 alias = TREE_VALUE (TREE_VALUE (alias));
219 alias = get_identifier (TREE_STRING_POINTER (alias));
220 /* A quirk of the initial implementation of aliases required that the
221 user add "extern" to all of them. Which is silly, but now
222 historical. Do note that the symbol is in fact locally defined. */
223 DECL_EXTERNAL (decl) = 0;
224 TREE_STATIC (decl) = 1;
225 assemble_alias (decl, alias);
226 finalize = false;
230 /* Can't defer this, because it needs to happen before any
231 later function definitions are processed. */
232 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
233 make_decl_rtl (decl);
235 /* Forward declarations for nested functions are not "external",
236 but we need to treat them as if they were. */
237 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
238 || TREE_CODE (decl) == FUNCTION_DECL)
240 timevar_push (TV_VARCONST);
242 /* Don't output anything when a tentative file-scope definition
243 is seen. But at end of compilation, do output code for them.
245 We do output all variables and rely on
246 callgraph code to defer them except for forward declarations
247 (see gcc.c-torture/compile/920624-1.c) */
248 if ((at_end
249 || !DECL_DEFER_OUTPUT (decl)
250 || DECL_INITIAL (decl))
251 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
252 && !DECL_EXTERNAL (decl))
254 /* When reading LTO unit, we also read varpool, so do not
255 rebuild it. */
256 if (in_lto_p && !at_end)
258 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
259 varpool_node::finalize_decl (decl);
262 #ifdef ASM_FINISH_DECLARE_OBJECT
263 if (decl == last_assemble_variable_decl)
265 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
266 top_level, at_end);
268 #endif
270 timevar_pop (TV_VARCONST);
272 else if (TREE_CODE (decl) == TYPE_DECL
273 /* Like in rest_of_type_compilation, avoid confusing the debug
274 information machinery when there are errors. */
275 && !seen_error ())
277 timevar_push (TV_SYMOUT);
278 debug_hooks->type_decl (decl, !top_level);
279 timevar_pop (TV_SYMOUT);
282 /* Let cgraph know about the existence of variables. */
283 if (in_lto_p && !at_end)
285 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
286 && TREE_STATIC (decl))
287 varpool_node::get_create (decl);
289 /* Generate early debug for global variables. Any local variables will
290 be handled by either handling reachable functions from
291 finalize_compilation_unit (and by consequence, locally scoped
292 symbols), or by rest_of_type_compilation below.
294 Also, pick up function prototypes, which will be mostly ignored
295 by the different early_global_decl() hooks, but will at least be
296 used by Go's hijack of the debug_hooks to implement
297 -fdump-go-spec. */
298 if (!in_lto_p
299 && (TREE_CODE (decl) != FUNCTION_DECL
300 /* This will pick up function prototypes with no bodies,
301 which are not visible in finalize_compilation_unit()
302 while iterating with FOR_EACH_*_FUNCTION through the
303 symbol table. */
304 || !DECL_SAVED_TREE (decl))
306 /* We need to check both decl_function_context and
307 current_function_decl here to make sure local extern
308 declarations end up with the correct context.
310 For local extern declarations, decl_function_context is
311 empty, but current_function_decl is set to the function where
312 the extern was declared . Without the check for
313 !current_function_decl below, the local extern ends up
314 incorrectly with a top-level context.
316 For example:
318 namespace S
324 int i = 42;
326 extern int i; // Local extern declaration.
327 return i;
333 && !decl_function_context (decl)
334 && !current_function_decl
336 && !decl_type_context (decl))
337 (*debug_hooks->early_global_decl) (decl);
340 /* Called after finishing a record, union or enumeral type. */
342 void
343 rest_of_type_compilation (tree type, int toplev)
345 /* Avoid confusing the debug information machinery when there are
346 errors. */
347 if (seen_error ())
348 return;
350 timevar_push (TV_SYMOUT);
351 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
352 timevar_pop (TV_SYMOUT);
357 void
358 pass_manager::
359 finish_optimization_passes (void)
361 int i;
362 struct dump_file_info *dfi;
363 char *name;
364 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
366 timevar_push (TV_DUMP);
367 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
369 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
370 end_branch_prob ();
371 dumps->dump_finish (pass_profile_1->static_pass_number);
374 if (optimize > 0)
376 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
377 print_combine_total_stats ();
378 dumps->dump_finish (pass_profile_1->static_pass_number);
381 /* Do whatever is necessary to finish printing the graphs. */
382 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
383 if (dumps->dump_initialized_p (i)
384 && (dfi->pflags & TDF_GRAPH) != 0
385 && (name = dumps->get_dump_file_name (i)) != NULL)
387 finish_graph_dump_file (name);
388 free (name);
391 timevar_pop (TV_DUMP);
394 static unsigned int
395 execute_build_ssa_passes (void)
397 /* Once this pass (and its sub-passes) are complete, all functions
398 will be in SSA form. Technically this state change is happening
399 a tad early, since the sub-passes have not yet run, but since
400 none of the sub-passes are IPA passes and do not create new
401 functions, this is ok. We're setting this value for the benefit
402 of IPA passes that follow. */
403 if (symtab->state < IPA_SSA)
404 symtab->state = IPA_SSA;
405 return 0;
408 namespace {
410 const pass_data pass_data_build_ssa_passes =
412 SIMPLE_IPA_PASS, /* type */
413 "build_ssa_passes", /* name */
414 OPTGROUP_NONE, /* optinfo_flags */
415 TV_EARLY_LOCAL, /* tv_id */
416 0, /* properties_required */
417 0, /* properties_provided */
418 0, /* properties_destroyed */
419 0, /* todo_flags_start */
420 /* todo_flags_finish is executed before subpases. For this reason
421 it makes no sense to remove unreachable functions here. */
422 0, /* todo_flags_finish */
425 class pass_build_ssa_passes : public simple_ipa_opt_pass
427 public:
428 pass_build_ssa_passes (gcc::context *ctxt)
429 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
432 /* opt_pass methods: */
433 virtual bool gate (function *)
435 /* Don't bother doing anything if the program has errors. */
436 return (!seen_error () && !in_lto_p);
439 virtual unsigned int execute (function *)
441 return execute_build_ssa_passes ();
444 }; // class pass_build_ssa_passes
446 const pass_data pass_data_chkp_instrumentation_passes =
448 SIMPLE_IPA_PASS, /* type */
449 "chkp_passes", /* name */
450 OPTGROUP_NONE, /* optinfo_flags */
451 TV_NONE, /* tv_id */
452 0, /* properties_required */
453 0, /* properties_provided */
454 0, /* properties_destroyed */
455 0, /* todo_flags_start */
456 0, /* todo_flags_finish */
459 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
461 public:
462 pass_chkp_instrumentation_passes (gcc::context *ctxt)
463 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
466 /* opt_pass methods: */
467 virtual bool gate (function *)
469 /* Don't bother doing anything if the program has errors. */
470 return (flag_check_pointer_bounds
471 && !seen_error () && !in_lto_p);
474 }; // class pass_chkp_instrumentation_passes
476 const pass_data pass_data_local_optimization_passes =
478 SIMPLE_IPA_PASS, /* type */
479 "opt_local_passes", /* name */
480 OPTGROUP_NONE, /* optinfo_flags */
481 TV_NONE, /* tv_id */
482 0, /* properties_required */
483 0, /* properties_provided */
484 0, /* properties_destroyed */
485 0, /* todo_flags_start */
486 0, /* todo_flags_finish */
489 class pass_local_optimization_passes : public simple_ipa_opt_pass
491 public:
492 pass_local_optimization_passes (gcc::context *ctxt)
493 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
496 /* opt_pass methods: */
497 virtual bool gate (function *)
499 /* Don't bother doing anything if the program has errors. */
500 return (!seen_error () && !in_lto_p);
503 }; // class pass_local_optimization_passes
505 } // anon namespace
507 simple_ipa_opt_pass *
508 make_pass_build_ssa_passes (gcc::context *ctxt)
510 return new pass_build_ssa_passes (ctxt);
513 simple_ipa_opt_pass *
514 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
516 return new pass_chkp_instrumentation_passes (ctxt);
519 simple_ipa_opt_pass *
520 make_pass_local_optimization_passes (gcc::context *ctxt)
522 return new pass_local_optimization_passes (ctxt);
525 namespace {
527 const pass_data pass_data_all_early_optimizations =
529 GIMPLE_PASS, /* type */
530 "early_optimizations", /* name */
531 OPTGROUP_NONE, /* optinfo_flags */
532 TV_NONE, /* tv_id */
533 0, /* properties_required */
534 0, /* properties_provided */
535 0, /* properties_destroyed */
536 0, /* todo_flags_start */
537 0, /* todo_flags_finish */
540 class pass_all_early_optimizations : public gimple_opt_pass
542 public:
543 pass_all_early_optimizations (gcc::context *ctxt)
544 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
547 /* opt_pass methods: */
548 virtual bool gate (function *)
550 return (optimize >= 1
551 /* Don't bother doing anything if the program has errors. */
552 && !seen_error ());
555 }; // class pass_all_early_optimizations
557 } // anon namespace
559 static gimple_opt_pass *
560 make_pass_all_early_optimizations (gcc::context *ctxt)
562 return new pass_all_early_optimizations (ctxt);
565 namespace {
567 const pass_data pass_data_all_optimizations =
569 GIMPLE_PASS, /* type */
570 "*all_optimizations", /* name */
571 OPTGROUP_NONE, /* optinfo_flags */
572 TV_OPTIMIZE, /* tv_id */
573 0, /* properties_required */
574 0, /* properties_provided */
575 0, /* properties_destroyed */
576 0, /* todo_flags_start */
577 0, /* todo_flags_finish */
580 class pass_all_optimizations : public gimple_opt_pass
582 public:
583 pass_all_optimizations (gcc::context *ctxt)
584 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
587 /* opt_pass methods: */
588 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
590 }; // class pass_all_optimizations
592 } // anon namespace
594 static gimple_opt_pass *
595 make_pass_all_optimizations (gcc::context *ctxt)
597 return new pass_all_optimizations (ctxt);
600 namespace {
602 const pass_data pass_data_all_optimizations_g =
604 GIMPLE_PASS, /* type */
605 "*all_optimizations_g", /* name */
606 OPTGROUP_NONE, /* optinfo_flags */
607 TV_OPTIMIZE, /* tv_id */
608 0, /* properties_required */
609 0, /* properties_provided */
610 0, /* properties_destroyed */
611 0, /* todo_flags_start */
612 0, /* todo_flags_finish */
615 class pass_all_optimizations_g : public gimple_opt_pass
617 public:
618 pass_all_optimizations_g (gcc::context *ctxt)
619 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
622 /* opt_pass methods: */
623 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
625 }; // class pass_all_optimizations_g
627 } // anon namespace
629 static gimple_opt_pass *
630 make_pass_all_optimizations_g (gcc::context *ctxt)
632 return new pass_all_optimizations_g (ctxt);
635 namespace {
637 const pass_data pass_data_rest_of_compilation =
639 RTL_PASS, /* type */
640 "*rest_of_compilation", /* name */
641 OPTGROUP_NONE, /* optinfo_flags */
642 TV_REST_OF_COMPILATION, /* tv_id */
643 PROP_rtl, /* properties_required */
644 0, /* properties_provided */
645 0, /* properties_destroyed */
646 0, /* todo_flags_start */
647 0, /* todo_flags_finish */
650 class pass_rest_of_compilation : public rtl_opt_pass
652 public:
653 pass_rest_of_compilation (gcc::context *ctxt)
654 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
657 /* opt_pass methods: */
658 virtual bool gate (function *)
660 /* Early return if there were errors. We can run afoul of our
661 consistency checks, and there's not really much point in fixing them. */
662 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
665 }; // class pass_rest_of_compilation
667 } // anon namespace
669 static rtl_opt_pass *
670 make_pass_rest_of_compilation (gcc::context *ctxt)
672 return new pass_rest_of_compilation (ctxt);
675 namespace {
677 const pass_data pass_data_postreload =
679 RTL_PASS, /* type */
680 "*all-postreload", /* name */
681 OPTGROUP_NONE, /* optinfo_flags */
682 TV_POSTRELOAD, /* tv_id */
683 PROP_rtl, /* properties_required */
684 0, /* properties_provided */
685 0, /* properties_destroyed */
686 0, /* todo_flags_start */
687 0, /* todo_flags_finish */
690 class pass_postreload : public rtl_opt_pass
692 public:
693 pass_postreload (gcc::context *ctxt)
694 : rtl_opt_pass (pass_data_postreload, ctxt)
697 /* opt_pass methods: */
698 virtual bool gate (function *) { return reload_completed; }
700 }; // class pass_postreload
702 } // anon namespace
704 static rtl_opt_pass *
705 make_pass_postreload (gcc::context *ctxt)
707 return new pass_postreload (ctxt);
710 namespace {
712 const pass_data pass_data_late_compilation =
714 RTL_PASS, /* type */
715 "*all-late_compilation", /* name */
716 OPTGROUP_NONE, /* optinfo_flags */
717 TV_LATE_COMPILATION, /* tv_id */
718 PROP_rtl, /* properties_required */
719 0, /* properties_provided */
720 0, /* properties_destroyed */
721 0, /* todo_flags_start */
722 0, /* todo_flags_finish */
725 class pass_late_compilation : public rtl_opt_pass
727 public:
728 pass_late_compilation (gcc::context *ctxt)
729 : rtl_opt_pass (pass_data_late_compilation, ctxt)
732 /* opt_pass methods: */
733 virtual bool gate (function *)
735 return reload_completed || targetm.no_register_allocation;
738 }; // class pass_late_compilation
740 } // anon namespace
742 static rtl_opt_pass *
743 make_pass_late_compilation (gcc::context *ctxt)
745 return new pass_late_compilation (ctxt);
750 /* Set the static pass number of pass PASS to ID and record that
751 in the mapping from static pass number to pass. */
753 void
754 pass_manager::
755 set_pass_for_id (int id, opt_pass *pass)
757 pass->static_pass_number = id;
758 if (passes_by_id_size <= id)
760 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
761 memset (passes_by_id + passes_by_id_size, 0,
762 (id + 1 - passes_by_id_size) * sizeof (void *));
763 passes_by_id_size = id + 1;
765 passes_by_id[id] = pass;
768 /* Return the pass with the static pass number ID. */
770 opt_pass *
771 pass_manager::get_pass_for_id (int id) const
773 if (id >= passes_by_id_size)
774 return NULL;
775 return passes_by_id[id];
778 /* Iterate over the pass tree allocating dump file numbers. We want
779 to do this depth first, and independent of whether the pass is
780 enabled or not. */
782 void
783 register_one_dump_file (opt_pass *pass)
785 g->get_passes ()->register_one_dump_file (pass);
788 void
789 pass_manager::register_one_dump_file (opt_pass *pass)
791 char *dot_name, *flag_name, *glob_name;
792 const char *name, *full_name, *prefix;
793 char num[10];
794 int flags, id;
795 int optgroup_flags = OPTGROUP_NONE;
796 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
798 /* See below in next_pass_1. */
799 num[0] = '\0';
800 if (pass->static_pass_number != -1)
801 sprintf (num, "%d", ((int) pass->static_pass_number < 0
802 ? 1 : pass->static_pass_number));
804 /* The name is both used to identify the pass for the purposes of plugins,
805 and to specify dump file name and option.
806 The latter two might want something short which is not quite unique; for
807 that reason, we may have a disambiguating prefix, followed by a space
808 to mark the start of the following dump file name / option string. */
809 name = strchr (pass->name, ' ');
810 name = name ? name + 1 : pass->name;
811 dot_name = concat (".", name, num, NULL);
812 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
814 prefix = "ipa-";
815 flags = TDF_IPA;
816 optgroup_flags |= OPTGROUP_IPA;
818 else if (pass->type == GIMPLE_PASS)
820 prefix = "tree-";
821 flags = TDF_TREE;
823 else
825 prefix = "rtl-";
826 flags = TDF_RTL;
829 flag_name = concat (prefix, name, num, NULL);
830 glob_name = concat (prefix, name, NULL);
831 optgroup_flags |= pass->optinfo_flags;
832 /* For any passes that do not have an optgroup set, and which are not
833 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
834 any dump messages are emitted properly under -fopt-info(-optall). */
835 if (optgroup_flags == OPTGROUP_NONE)
836 optgroup_flags = OPTGROUP_OTHER;
837 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
838 optgroup_flags,
839 true);
840 set_pass_for_id (id, pass);
841 full_name = concat (prefix, pass->name, num, NULL);
842 register_pass_name (pass, full_name);
843 free (CONST_CAST (char *, full_name));
846 /* Register the dump files for the pass_manager starting at PASS. */
848 void
849 pass_manager::register_dump_files (opt_pass *pass)
853 if (pass->name && pass->name[0] != '*')
854 register_one_dump_file (pass);
856 if (pass->sub)
857 register_dump_files (pass->sub);
859 pass = pass->next;
861 while (pass);
864 /* Helper for pass_registry hash table. */
866 struct pass_registry_hasher : default_hashmap_traits
868 static inline hashval_t hash (const char *);
869 static inline bool equal_keys (const char *, const char *);
872 /* Pass registry hash function. */
874 inline hashval_t
875 pass_registry_hasher::hash (const char *name)
877 return htab_hash_string (name);
880 /* Hash equal function */
882 inline bool
883 pass_registry_hasher::equal_keys (const char *s1, const char *s2)
885 return !strcmp (s1, s2);
888 static hash_map<const char *, opt_pass *, pass_registry_hasher>
889 *name_to_pass_map;
891 /* Register PASS with NAME. */
893 static void
894 register_pass_name (opt_pass *pass, const char *name)
896 if (!name_to_pass_map)
897 name_to_pass_map
898 = new hash_map<const char *, opt_pass *, pass_registry_hasher> (256);
900 if (name_to_pass_map->get (name))
901 return; /* Ignore plugin passes. */
903 const char *unique_name = xstrdup (name);
904 name_to_pass_map->put (unique_name, pass);
907 /* Map from pass id to canonicalized pass name. */
909 typedef const char *char_ptr;
910 static vec<char_ptr> pass_tab = vNULL;
912 /* Callback function for traversing NAME_TO_PASS_MAP. */
914 bool
915 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
917 gcc_assert (pass->static_pass_number > 0);
918 gcc_assert (pass_tab.exists ());
920 pass_tab[pass->static_pass_number] = name;
922 return 1;
925 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
926 table for dumping purpose. */
928 static void
929 create_pass_tab (void)
931 if (!flag_dump_passes)
932 return;
934 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
935 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
938 static bool override_gate_status (opt_pass *, tree, bool);
940 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
941 is turned on or not. */
943 static void
944 dump_one_pass (opt_pass *pass, int pass_indent)
946 int indent = 3 * pass_indent;
947 const char *pn;
948 bool is_on, is_really_on;
950 is_on = pass->gate (cfun);
951 is_really_on = override_gate_status (pass, current_function_decl, is_on);
953 if (pass->static_pass_number <= 0)
954 pn = pass->name;
955 else
956 pn = pass_tab[pass->static_pass_number];
958 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
959 (15 - indent < 0 ? 0 : 15 - indent), " ",
960 is_on ? " ON" : " OFF",
961 ((!is_on) == (!is_really_on) ? ""
962 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
965 /* Dump pass list PASS with indentation INDENT. */
967 static void
968 dump_pass_list (opt_pass *pass, int indent)
972 dump_one_pass (pass, indent);
973 if (pass->sub)
974 dump_pass_list (pass->sub, indent + 1);
975 pass = pass->next;
977 while (pass);
980 /* Dump all optimization passes. */
982 void
983 dump_passes (void)
985 g->get_passes ()->dump_passes ();
988 void
989 pass_manager::dump_passes () const
991 push_dummy_function (true);
993 create_pass_tab ();
995 dump_pass_list (all_lowering_passes, 1);
996 dump_pass_list (all_small_ipa_passes, 1);
997 dump_pass_list (all_regular_ipa_passes, 1);
998 dump_pass_list (all_late_ipa_passes, 1);
999 dump_pass_list (all_passes, 1);
1001 pop_dummy_function ();
1004 /* Returns the pass with NAME. */
1006 static opt_pass *
1007 get_pass_by_name (const char *name)
1009 opt_pass **p = name_to_pass_map->get (name);
1010 if (p)
1011 return *p;
1013 return NULL;
1017 /* Range [start, last]. */
1019 struct uid_range
1021 unsigned int start;
1022 unsigned int last;
1023 const char *assem_name;
1024 struct uid_range *next;
1027 typedef struct uid_range *uid_range_p;
1030 static vec<uid_range_p>
1031 enabled_pass_uid_range_tab = vNULL;
1032 static vec<uid_range_p>
1033 disabled_pass_uid_range_tab = vNULL;
1036 /* Parse option string for -fdisable- and -fenable-
1037 The syntax of the options:
1039 -fenable-<pass_name>
1040 -fdisable-<pass_name>
1042 -fenable-<pass_name>=s1:e1,s2:e2,...
1043 -fdisable-<pass_name>=s1:e1,s2:e2,...
1046 static void
1047 enable_disable_pass (const char *arg, bool is_enable)
1049 opt_pass *pass;
1050 char *range_str, *phase_name;
1051 char *argstr = xstrdup (arg);
1052 vec<uid_range_p> *tab = 0;
1054 range_str = strchr (argstr,'=');
1055 if (range_str)
1057 *range_str = '\0';
1058 range_str++;
1061 phase_name = argstr;
1062 if (!*phase_name)
1064 if (is_enable)
1065 error ("unrecognized option -fenable");
1066 else
1067 error ("unrecognized option -fdisable");
1068 free (argstr);
1069 return;
1071 pass = get_pass_by_name (phase_name);
1072 if (!pass || pass->static_pass_number == -1)
1074 if (is_enable)
1075 error ("unknown pass %s specified in -fenable", phase_name);
1076 else
1077 error ("unknown pass %s specified in -fdisable", phase_name);
1078 free (argstr);
1079 return;
1082 if (is_enable)
1083 tab = &enabled_pass_uid_range_tab;
1084 else
1085 tab = &disabled_pass_uid_range_tab;
1087 if ((unsigned) pass->static_pass_number >= tab->length ())
1088 tab->safe_grow_cleared (pass->static_pass_number + 1);
1090 if (!range_str)
1092 uid_range_p slot;
1093 uid_range_p new_range = XCNEW (struct uid_range);
1095 new_range->start = 0;
1096 new_range->last = (unsigned)-1;
1098 slot = (*tab)[pass->static_pass_number];
1099 new_range->next = slot;
1100 (*tab)[pass->static_pass_number] = new_range;
1101 if (is_enable)
1102 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1103 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1104 else
1105 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1106 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1108 else
1110 char *next_range = NULL;
1111 char *one_range = range_str;
1112 char *end_val = NULL;
1116 uid_range_p slot;
1117 uid_range_p new_range;
1118 char *invalid = NULL;
1119 long start;
1120 char *func_name = NULL;
1122 next_range = strchr (one_range, ',');
1123 if (next_range)
1125 *next_range = '\0';
1126 next_range++;
1129 end_val = strchr (one_range, ':');
1130 if (end_val)
1132 *end_val = '\0';
1133 end_val++;
1135 start = strtol (one_range, &invalid, 10);
1136 if (*invalid || start < 0)
1138 if (end_val || (one_range[0] >= '0'
1139 && one_range[0] <= '9'))
1141 error ("Invalid range %s in option %s",
1142 one_range,
1143 is_enable ? "-fenable" : "-fdisable");
1144 free (argstr);
1145 return;
1147 func_name = one_range;
1149 if (!end_val)
1151 new_range = XCNEW (struct uid_range);
1152 if (!func_name)
1154 new_range->start = (unsigned) start;
1155 new_range->last = (unsigned) start;
1157 else
1159 new_range->start = (unsigned) -1;
1160 new_range->last = (unsigned) -1;
1161 new_range->assem_name = xstrdup (func_name);
1164 else
1166 long last = strtol (end_val, &invalid, 10);
1167 if (*invalid || last < start)
1169 error ("Invalid range %s in option %s",
1170 end_val,
1171 is_enable ? "-fenable" : "-fdisable");
1172 free (argstr);
1173 return;
1175 new_range = XCNEW (struct uid_range);
1176 new_range->start = (unsigned) start;
1177 new_range->last = (unsigned) last;
1180 slot = (*tab)[pass->static_pass_number];
1181 new_range->next = slot;
1182 (*tab)[pass->static_pass_number] = new_range;
1183 if (is_enable)
1185 if (new_range->assem_name)
1186 inform (UNKNOWN_LOCATION,
1187 "enable pass %s for function %s",
1188 phase_name, new_range->assem_name);
1189 else
1190 inform (UNKNOWN_LOCATION,
1191 "enable pass %s for functions in the range of [%u, %u]",
1192 phase_name, new_range->start, new_range->last);
1194 else
1196 if (new_range->assem_name)
1197 inform (UNKNOWN_LOCATION,
1198 "disable pass %s for function %s",
1199 phase_name, new_range->assem_name);
1200 else
1201 inform (UNKNOWN_LOCATION,
1202 "disable pass %s for functions in the range of [%u, %u]",
1203 phase_name, new_range->start, new_range->last);
1206 one_range = next_range;
1207 } while (next_range);
1210 free (argstr);
1213 /* Enable pass specified by ARG. */
1215 void
1216 enable_pass (const char *arg)
1218 enable_disable_pass (arg, true);
1221 /* Disable pass specified by ARG. */
1223 void
1224 disable_pass (const char *arg)
1226 enable_disable_pass (arg, false);
1229 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1231 static bool
1232 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1233 tree func,
1234 vec<uid_range_p> tab)
1236 uid_range_p slot, range;
1237 int cgraph_uid;
1238 const char *aname = NULL;
1240 if (!tab.exists ()
1241 || (unsigned) pass->static_pass_number >= tab.length ()
1242 || pass->static_pass_number == -1)
1243 return false;
1245 slot = tab[pass->static_pass_number];
1246 if (!slot)
1247 return false;
1249 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1250 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1251 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1253 range = slot;
1254 while (range)
1256 if ((unsigned) cgraph_uid >= range->start
1257 && (unsigned) cgraph_uid <= range->last)
1258 return true;
1259 if (range->assem_name && aname
1260 && !strcmp (range->assem_name, aname))
1261 return true;
1262 range = range->next;
1265 return false;
1269 /* Update static_pass_number for passes (and the flag
1270 TODO_mark_first_instance).
1272 Passes are constructed with static_pass_number preinitialized to 0
1274 This field is used in two different ways: initially as instance numbers
1275 of their kind, and then as ids within the entire pass manager.
1277 Within pass_manager::pass_manager:
1279 * In add_pass_instance(), as called by next_pass_1 in
1280 NEXT_PASS in init_optimization_passes
1282 * When the initial instance of a pass within a pass manager is seen,
1283 it is flagged, and its static_pass_number is set to -1
1285 * On subsequent times that it is seen, the static pass number
1286 is decremented each time, so that if there are e.g. 4 dups,
1287 they have static_pass_number -4, 2, 3, 4 respectively (note
1288 how the initial one is negative and gives the count); these
1289 can be thought of as instance numbers of the specific pass
1291 * Within the register_dump_files () traversal, set_pass_for_id()
1292 is called on each pass, using these instance numbers to create
1293 dumpfile switches, and then overwriting them with a pass id,
1294 which are global to the whole pass manager (based on
1295 (TDI_end + current value of extra_dump_files_in_use) ) */
1297 static void
1298 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1299 opt_pass *initial_pass)
1301 /* Are we dealing with the first pass of its kind, or a clone? */
1302 if (new_pass != initial_pass)
1304 /* We're dealing with a clone. */
1305 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1307 /* Indicate to register_dump_files that this pass has duplicates,
1308 and so it should rename the dump file. The first instance will
1309 be -1, and be number of duplicates = -static_pass_number - 1.
1310 Subsequent instances will be > 0 and just the duplicate number. */
1311 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1313 initial_pass->static_pass_number -= 1;
1314 new_pass->static_pass_number = -initial_pass->static_pass_number;
1317 else
1319 /* We're dealing with the first pass of its kind. */
1320 new_pass->todo_flags_start |= TODO_mark_first_instance;
1321 new_pass->static_pass_number = -1;
1323 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1327 /* Add a pass to the pass list. Duplicate the pass if it's already
1328 in the list. */
1330 static opt_pass **
1331 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1333 /* Every pass should have a name so that plugins can refer to them. */
1334 gcc_assert (pass->name != NULL);
1336 add_pass_instance (pass, false, initial_pass);
1337 *list = pass;
1339 return &(*list)->next;
1342 /* List node for an inserted pass instance. We need to keep track of all
1343 the newly-added pass instances (with 'added_pass_nodes' defined below)
1344 so that we can register their dump files after pass-positioning is finished.
1345 Registering dumping files needs to be post-processed or the
1346 static_pass_number of the opt_pass object would be modified and mess up
1347 the dump file names of future pass instances to be added. */
1349 struct pass_list_node
1351 opt_pass *pass;
1352 struct pass_list_node *next;
1355 static struct pass_list_node *added_pass_nodes = NULL;
1356 static struct pass_list_node *prev_added_pass_node;
1358 /* Insert the pass at the proper position. Return true if the pass
1359 is successfully added.
1361 NEW_PASS_INFO - new pass to be inserted
1362 PASS_LIST - root of the pass list to insert the new pass to */
1364 static bool
1365 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1367 opt_pass *pass = *pass_list, *prev_pass = NULL;
1368 bool success = false;
1370 for ( ; pass; prev_pass = pass, pass = pass->next)
1372 /* Check if the current pass is of the same type as the new pass and
1373 matches the name and the instance number of the reference pass. */
1374 if (pass->type == new_pass_info->pass->type
1375 && pass->name
1376 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1377 && ((new_pass_info->ref_pass_instance_number == 0)
1378 || (new_pass_info->ref_pass_instance_number ==
1379 pass->static_pass_number)
1380 || (new_pass_info->ref_pass_instance_number == 1
1381 && pass->todo_flags_start & TODO_mark_first_instance)))
1383 opt_pass *new_pass;
1384 struct pass_list_node *new_pass_node;
1386 if (new_pass_info->ref_pass_instance_number == 0)
1388 new_pass = new_pass_info->pass->clone ();
1389 add_pass_instance (new_pass, true, new_pass_info->pass);
1391 else
1393 new_pass = new_pass_info->pass;
1394 add_pass_instance (new_pass, true, new_pass);
1397 /* Insert the new pass instance based on the positioning op. */
1398 switch (new_pass_info->pos_op)
1400 case PASS_POS_INSERT_AFTER:
1401 new_pass->next = pass->next;
1402 pass->next = new_pass;
1404 /* Skip newly inserted pass to avoid repeated
1405 insertions in the case where the new pass and the
1406 existing one have the same name. */
1407 pass = new_pass;
1408 break;
1409 case PASS_POS_INSERT_BEFORE:
1410 new_pass->next = pass;
1411 if (prev_pass)
1412 prev_pass->next = new_pass;
1413 else
1414 *pass_list = new_pass;
1415 break;
1416 case PASS_POS_REPLACE:
1417 new_pass->next = pass->next;
1418 if (prev_pass)
1419 prev_pass->next = new_pass;
1420 else
1421 *pass_list = new_pass;
1422 new_pass->sub = pass->sub;
1423 new_pass->tv_id = pass->tv_id;
1424 pass = new_pass;
1425 break;
1426 default:
1427 error ("invalid pass positioning operation");
1428 return false;
1431 /* Save the newly added pass (instance) in the added_pass_nodes
1432 list so that we can register its dump file later. Note that
1433 we cannot register the dump file now because doing so will modify
1434 the static_pass_number of the opt_pass object and therefore
1435 mess up the dump file name of future instances. */
1436 new_pass_node = XCNEW (struct pass_list_node);
1437 new_pass_node->pass = new_pass;
1438 if (!added_pass_nodes)
1439 added_pass_nodes = new_pass_node;
1440 else
1441 prev_added_pass_node->next = new_pass_node;
1442 prev_added_pass_node = new_pass_node;
1444 success = true;
1447 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1448 success = true;
1451 return success;
1454 /* Hooks a new pass into the pass lists.
1456 PASS_INFO - pass information that specifies the opt_pass object,
1457 reference pass, instance number, and how to position
1458 the pass */
1460 void
1461 register_pass (struct register_pass_info *pass_info)
1463 g->get_passes ()->register_pass (pass_info);
1466 void
1467 register_pass (opt_pass* pass, pass_positioning_ops pos,
1468 const char* ref_pass_name, int ref_pass_inst_number)
1470 register_pass_info i;
1471 i.pass = pass;
1472 i.reference_pass_name = ref_pass_name;
1473 i.ref_pass_instance_number = ref_pass_inst_number;
1474 i.pos_op = pos;
1476 g->get_passes ()->register_pass (&i);
1479 void
1480 pass_manager::register_pass (struct register_pass_info *pass_info)
1482 bool all_instances, success;
1483 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1485 /* The checks below could fail in buggy plugins. Existing GCC
1486 passes should never fail these checks, so we mention plugin in
1487 the messages. */
1488 if (!pass_info->pass)
1489 fatal_error (input_location, "plugin cannot register a missing pass");
1491 if (!pass_info->pass->name)
1492 fatal_error (input_location, "plugin cannot register an unnamed pass");
1494 if (!pass_info->reference_pass_name)
1495 fatal_error
1496 (input_location,
1497 "plugin cannot register pass %qs without reference pass name",
1498 pass_info->pass->name);
1500 /* Try to insert the new pass to the pass lists. We need to check
1501 all five lists as the reference pass could be in one (or all) of
1502 them. */
1503 all_instances = pass_info->ref_pass_instance_number == 0;
1504 success = position_pass (pass_info, &all_lowering_passes);
1505 if (!success || all_instances)
1506 success |= position_pass (pass_info, &all_small_ipa_passes);
1507 if (!success || all_instances)
1508 success |= position_pass (pass_info, &all_regular_ipa_passes);
1509 if (!success || all_instances)
1510 success |= position_pass (pass_info, &all_late_ipa_passes);
1511 if (!success || all_instances)
1512 success |= position_pass (pass_info, &all_passes);
1513 if (!success)
1514 fatal_error
1515 (input_location,
1516 "pass %qs not found but is referenced by new pass %qs",
1517 pass_info->reference_pass_name, pass_info->pass->name);
1519 /* OK, we have successfully inserted the new pass. We need to register
1520 the dump files for the newly added pass and its duplicates (if any).
1521 Because the registration of plugin/backend passes happens after the
1522 command-line options are parsed, the options that specify single
1523 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1524 passes. Therefore we currently can only enable dumping of
1525 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1526 are specified. While doing so, we also delete the pass_list_node
1527 objects created during pass positioning. */
1528 while (added_pass_nodes)
1530 struct pass_list_node *next_node = added_pass_nodes->next;
1531 enum tree_dump_index tdi;
1532 register_one_dump_file (added_pass_nodes->pass);
1533 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1534 || added_pass_nodes->pass->type == IPA_PASS)
1535 tdi = TDI_ipa_all;
1536 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1537 tdi = TDI_tree_all;
1538 else
1539 tdi = TDI_rtl_all;
1540 /* Check if dump-all flag is specified. */
1541 if (dumps->get_dump_file_info (tdi)->pstate)
1542 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1543 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1544 XDELETE (added_pass_nodes);
1545 added_pass_nodes = next_node;
1549 /* Construct the pass tree. The sequencing of passes is driven by
1550 the cgraph routines:
1552 finalize_compilation_unit ()
1553 for each node N in the cgraph
1554 cgraph_analyze_function (N)
1555 cgraph_lower_function (N) -> all_lowering_passes
1557 If we are optimizing, compile is then invoked:
1559 compile ()
1560 ipa_passes () -> all_small_ipa_passes
1561 -> Analysis of all_regular_ipa_passes
1562 * possible LTO streaming at copmilation time *
1563 -> Execution of all_regular_ipa_passes
1564 * possible LTO streaming at link time *
1565 -> all_late_ipa_passes
1566 expand_all_functions ()
1567 for each node N in the cgraph
1568 expand_function (N) -> Transformation of all_regular_ipa_passes
1569 -> all_passes
1572 void *
1573 pass_manager::operator new (size_t sz)
1575 /* Ensure that all fields of the pass manager are zero-initialized. */
1576 return xcalloc (1, sz);
1579 void
1580 pass_manager::operator delete (void *ptr)
1582 free (ptr);
1585 pass_manager::pass_manager (context *ctxt)
1586 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1587 all_regular_ipa_passes (NULL),
1588 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1589 m_ctxt (ctxt)
1591 opt_pass **p;
1593 /* Initialize the pass_lists array. */
1594 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1595 GCC_PASS_LISTS
1596 #undef DEF_PASS_LIST
1598 /* Build the tree of passes. */
1600 #define INSERT_PASSES_AFTER(PASS) \
1601 p = &(PASS);
1603 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1605 opt_pass **p = &(PASS ## _1)->sub;
1607 #define POP_INSERT_PASSES() \
1610 #define NEXT_PASS(PASS, NUM) \
1611 do { \
1612 gcc_assert (NULL == PASS ## _ ## NUM); \
1613 if ((NUM) == 1) \
1614 PASS ## _1 = make_##PASS (m_ctxt); \
1615 else \
1617 gcc_assert (PASS ## _1); \
1618 PASS ## _ ## NUM = PASS ## _1->clone (); \
1620 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1621 } while (0)
1623 #define TERMINATE_PASS_LIST() \
1624 *p = NULL;
1626 #include "pass-instances.def"
1628 #undef INSERT_PASSES_AFTER
1629 #undef PUSH_INSERT_PASSES_WITHIN
1630 #undef POP_INSERT_PASSES
1631 #undef NEXT_PASS
1632 #undef TERMINATE_PASS_LIST
1634 /* Register the passes with the tree dump code. */
1635 register_dump_files (all_lowering_passes);
1636 register_dump_files (all_small_ipa_passes);
1637 register_dump_files (all_regular_ipa_passes);
1638 register_dump_files (all_late_ipa_passes);
1639 register_dump_files (all_passes);
1642 static void
1643 delete_pass_tree (opt_pass *pass)
1645 while (pass)
1647 /* Recurse into child passes. */
1648 delete_pass_tree (pass->sub);
1650 opt_pass *next = pass->next;
1652 /* Delete this pass. */
1653 delete pass;
1655 /* Iterate onto sibling passes. */
1656 pass = next;
1660 pass_manager::~pass_manager ()
1662 XDELETEVEC (passes_by_id);
1664 /* Call delete_pass_tree on each of the pass_lists. */
1665 #define DEF_PASS_LIST(LIST) \
1666 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1667 GCC_PASS_LISTS
1668 #undef DEF_PASS_LIST
1672 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1673 function CALLBACK for every function in the call graph. Otherwise,
1674 call CALLBACK on the current function. */
1676 static void
1677 do_per_function (void (*callback) (function *, void *data), void *data)
1679 if (current_function_decl)
1680 callback (cfun, data);
1681 else
1683 struct cgraph_node *node;
1684 FOR_EACH_DEFINED_FUNCTION (node)
1685 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1686 && (!node->clone_of || node->decl != node->clone_of->decl))
1687 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1691 /* Because inlining might remove no-longer reachable nodes, we need to
1692 keep the array visible to garbage collector to avoid reading collected
1693 out nodes. */
1694 static int nnodes;
1695 static GTY ((length ("nnodes"))) cgraph_node **order;
1697 /* Hook called when NODE is removed and therefore should be
1698 excluded from order vector. DATA is an array of integers.
1699 DATA[0] holds max index it may be accessed by. For cgraph
1700 node DATA[node->uid + 1] holds index of this node in order
1701 vector. */
1702 static void
1703 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1705 int *order_idx = (int *)data;
1707 if (node->uid >= order_idx[0])
1708 return;
1710 int idx = order_idx[node->uid + 1];
1711 if (idx >= 0 && idx < nnodes && order[idx] == node)
1712 order[idx] = NULL;
1715 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1716 function CALLBACK for every function in the call graph. Otherwise,
1717 call CALLBACK on the current function.
1718 This function is global so that plugins can use it. */
1719 void
1720 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1722 int i;
1724 if (current_function_decl)
1725 callback (cfun, data);
1726 else
1728 cgraph_node_hook_list *hook;
1729 int *order_idx;
1730 gcc_assert (!order);
1731 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1733 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1734 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1735 order_idx[0] = symtab->cgraph_max_uid;
1737 nnodes = ipa_reverse_postorder (order);
1738 for (i = nnodes - 1; i >= 0; i--)
1740 order[i]->process = 1;
1741 order_idx[order[i]->uid + 1] = i;
1743 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1744 order_idx);
1745 for (i = nnodes - 1; i >= 0; i--)
1747 /* Function could be inlined and removed as unreachable. */
1748 if (!order[i])
1749 continue;
1751 struct cgraph_node *node = order[i];
1753 /* Allow possibly removed nodes to be garbage collected. */
1754 order[i] = NULL;
1755 node->process = 0;
1756 if (node->has_gimple_body_p ())
1757 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1759 symtab->remove_cgraph_removal_hook (hook);
1761 ggc_free (order);
1762 order = NULL;
1763 nnodes = 0;
1766 /* Helper function to perform function body dump. */
1768 static void
1769 execute_function_dump (function *fn, void *data)
1771 opt_pass *pass = (opt_pass *)data;
1773 if (dump_file)
1775 push_cfun (fn);
1777 if (fn->curr_properties & PROP_trees)
1778 dump_function_to_file (fn->decl, dump_file, dump_flags);
1779 else
1780 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1782 /* Flush the file. If verification fails, we won't be able to
1783 close the file before aborting. */
1784 fflush (dump_file);
1786 if ((fn->curr_properties & PROP_cfg)
1787 && (dump_flags & TDF_GRAPH))
1789 if (!pass->graph_dump_initialized)
1791 clean_graph_dump_file (dump_file_name);
1792 pass->graph_dump_initialized = true;
1794 print_graph_cfg (dump_file_name, fn);
1797 pop_cfun ();
1801 static struct profile_record *profile_record;
1803 /* Do profile consistency book-keeping for the pass with static number INDEX.
1804 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1805 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1806 if we are only book-keeping on passes that may have selectively disabled
1807 themselves on a given function. */
1808 static void
1809 check_profile_consistency (int index, int subpass, bool run)
1811 pass_manager *passes = g->get_passes ();
1812 if (index == -1)
1813 return;
1814 if (!profile_record)
1815 profile_record = XCNEWVEC (struct profile_record,
1816 passes->passes_by_id_size);
1817 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1818 gcc_assert (subpass < 2);
1819 profile_record[index].run |= run;
1820 account_profile_record (&profile_record[index], subpass);
1823 /* Output profile consistency. */
1825 void
1826 dump_profile_report (void)
1828 g->get_passes ()->dump_profile_report ();
1831 void
1832 pass_manager::dump_profile_report () const
1834 int i, j;
1835 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1836 gcov_type last_time = 0, last_size = 0;
1837 double rel_time_change, rel_size_change;
1838 int last_reported = 0;
1840 if (!profile_record)
1841 return;
1842 fprintf (stderr, "\nProfile consistency report:\n\n");
1843 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1844 fprintf (stderr, " |freq count |freq count |size time\n");
1846 for (i = 0; i < passes_by_id_size; i++)
1847 for (j = 0 ; j < 2; j++)
1848 if (profile_record[i].run)
1850 if (last_time)
1851 rel_time_change = (profile_record[i].time[j]
1852 - (double)last_time) * 100 / (double)last_time;
1853 else
1854 rel_time_change = 0;
1855 if (last_size)
1856 rel_size_change = (profile_record[i].size[j]
1857 - (double)last_size) * 100 / (double)last_size;
1858 else
1859 rel_size_change = 0;
1861 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1862 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1863 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1864 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1865 || rel_time_change || rel_size_change)
1867 last_reported = i;
1868 fprintf (stderr, "%-20s %s",
1869 passes_by_id [i]->name,
1870 j ? "(after TODO)" : " ");
1871 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1872 fprintf (stderr, "| %+5i",
1873 profile_record[i].num_mismatched_freq_in[j]
1874 - last_freq_in);
1875 else
1876 fprintf (stderr, "| ");
1877 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1878 fprintf (stderr, " %+5i",
1879 profile_record[i].num_mismatched_count_in[j]
1880 - last_count_in);
1881 else
1882 fprintf (stderr, " ");
1883 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1884 fprintf (stderr, "| %+5i",
1885 profile_record[i].num_mismatched_freq_out[j]
1886 - last_freq_out);
1887 else
1888 fprintf (stderr, "| ");
1889 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1890 fprintf (stderr, " %+5i",
1891 profile_record[i].num_mismatched_count_out[j]
1892 - last_count_out);
1893 else
1894 fprintf (stderr, " ");
1896 /* Size/time units change across gimple and RTL. */
1897 if (i == pass_expand_1->static_pass_number)
1898 fprintf (stderr, "|----------");
1899 else
1901 if (rel_size_change)
1902 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1903 else
1904 fprintf (stderr, "| ");
1905 if (rel_time_change)
1906 fprintf (stderr, " %+8.4f%%", rel_time_change);
1908 fprintf (stderr, "\n");
1909 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1910 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1911 last_count_in = profile_record[i].num_mismatched_count_in[j];
1912 last_count_out = profile_record[i].num_mismatched_count_out[j];
1914 else if (j && last_reported != i)
1916 last_reported = i;
1917 fprintf (stderr, "%-20s ------------| | |\n",
1918 passes_by_id [i]->name);
1920 last_time = profile_record[i].time[j];
1921 last_size = profile_record[i].size[j];
1925 /* Perform all TODO actions that ought to be done on each function. */
1927 static void
1928 execute_function_todo (function *fn, void *data)
1930 bool from_ipa_pass = (cfun == NULL);
1931 unsigned int flags = (size_t)data;
1932 flags &= ~fn->last_verified;
1933 if (!flags)
1934 return;
1936 push_cfun (fn);
1938 /* Always cleanup the CFG before trying to update SSA. */
1939 if (flags & TODO_cleanup_cfg)
1941 cleanup_tree_cfg ();
1943 /* When cleanup_tree_cfg merges consecutive blocks, it may
1944 perform some simplistic propagation when removing single
1945 valued PHI nodes. This propagation may, in turn, cause the
1946 SSA form to become out-of-date (see PR 22037). So, even
1947 if the parent pass had not scheduled an SSA update, we may
1948 still need to do one. */
1949 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1950 flags |= TODO_update_ssa;
1953 if (flags & TODO_update_ssa_any)
1955 unsigned update_flags = flags & TODO_update_ssa_any;
1956 update_ssa (update_flags);
1959 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1960 compute_may_aliases ();
1962 if (optimize && (flags & TODO_update_address_taken))
1963 execute_update_addresses_taken ();
1965 if (flags & TODO_remove_unused_locals)
1966 remove_unused_locals ();
1968 if (flags & TODO_rebuild_frequencies)
1969 rebuild_frequencies ();
1971 if (flags & TODO_rebuild_cgraph_edges)
1972 cgraph_edge::rebuild_edges ();
1974 /* If we've seen errors do not bother running any verifiers. */
1975 if (!seen_error ())
1977 #if defined ENABLE_CHECKING
1978 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1979 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1981 if (flags & TODO_verify_il)
1983 if (cfun->curr_properties & PROP_trees)
1985 if (cfun->curr_properties & PROP_cfg)
1986 /* IPA passes leave stmts to be fixed up, so make sure to
1987 not verify stmts really throw. */
1988 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1989 else
1990 verify_gimple_in_seq (gimple_body (cfun->decl));
1992 if (cfun->curr_properties & PROP_ssa)
1993 /* IPA passes leave stmts to be fixed up, so make sure to
1994 not verify SSA operands whose verifier will choke on that. */
1995 verify_ssa (true, !from_ipa_pass);
1996 /* IPA passes leave basic-blocks unsplit, so make sure to
1997 not trip on that. */
1998 if ((cfun->curr_properties & PROP_cfg)
1999 && !from_ipa_pass)
2000 verify_flow_info ();
2001 if (current_loops
2002 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
2003 verify_loop_closed_ssa (false);
2004 if (cfun->curr_properties & PROP_rtl)
2005 verify_rtl_sharing ();
2008 /* Make sure verifiers don't change dominator state. */
2009 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
2010 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
2011 #endif
2014 fn->last_verified = flags & TODO_verify_all;
2016 pop_cfun ();
2018 /* For IPA passes make sure to release dominator info, it can be
2019 computed by non-verifying TODOs. */
2020 if (from_ipa_pass)
2022 free_dominance_info (fn, CDI_DOMINATORS);
2023 free_dominance_info (fn, CDI_POST_DOMINATORS);
2027 /* Perform all TODO actions. */
2028 static void
2029 execute_todo (unsigned int flags)
2031 #if defined ENABLE_CHECKING
2032 if (cfun
2033 && need_ssa_update_p (cfun))
2034 gcc_assert (flags & TODO_update_ssa_any);
2035 #endif
2037 timevar_push (TV_TODO);
2039 /* Inform the pass whether it is the first time it is run. */
2040 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2042 statistics_fini_pass ();
2044 if (flags)
2045 do_per_function (execute_function_todo, (void *)(size_t) flags);
2047 /* Always remove functions just as before inlining: IPA passes might be
2048 interested to see bodies of extern inline functions that are not inlined
2049 to analyze side effects. The full removal is done just at the end
2050 of IPA pass queue. */
2051 if (flags & TODO_remove_functions)
2053 gcc_assert (!cfun);
2054 symtab->remove_unreachable_nodes (dump_file);
2057 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2059 gcc_assert (!cfun);
2060 symtab_node::dump_table (dump_file);
2061 /* Flush the file. If verification fails, we won't be able to
2062 close the file before aborting. */
2063 fflush (dump_file);
2066 /* Now that the dumping has been done, we can get rid of the optional
2067 df problems. */
2068 if (flags & TODO_df_finish)
2069 df_finish_pass ((flags & TODO_df_verify) != 0);
2071 timevar_pop (TV_TODO);
2074 /* Verify invariants that should hold between passes. This is a place
2075 to put simple sanity checks. */
2077 static void
2078 verify_interpass_invariants (void)
2080 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2083 /* Clear the last verified flag. */
2085 static void
2086 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2088 fn->last_verified = 0;
2091 /* Helper function. Verify that the properties has been turn into the
2092 properties expected by the pass. */
2094 #ifdef ENABLE_CHECKING
2095 static void
2096 verify_curr_properties (function *fn, void *data)
2098 unsigned int props = (size_t)data;
2099 gcc_assert ((fn->curr_properties & props) == props);
2101 #endif
2103 /* Initialize pass dump file. */
2104 /* This is non-static so that the plugins can use it. */
2106 bool
2107 pass_init_dump_file (opt_pass *pass)
2109 /* If a dump file name is present, open it if enabled. */
2110 if (pass->static_pass_number != -1)
2112 timevar_push (TV_DUMP);
2113 gcc::dump_manager *dumps = g->get_dumps ();
2114 bool initializing_dump =
2115 !dumps->dump_initialized_p (pass->static_pass_number);
2116 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2117 dumps->dump_start (pass->static_pass_number, &dump_flags);
2118 if (dump_file && current_function_decl)
2119 dump_function_header (dump_file, current_function_decl, dump_flags);
2120 if (initializing_dump
2121 && dump_file && (dump_flags & TDF_GRAPH)
2122 && cfun && (cfun->curr_properties & PROP_cfg))
2124 clean_graph_dump_file (dump_file_name);
2125 pass->graph_dump_initialized = true;
2127 timevar_pop (TV_DUMP);
2128 return initializing_dump;
2130 else
2131 return false;
2134 /* Flush PASS dump file. */
2135 /* This is non-static so that plugins can use it. */
2137 void
2138 pass_fini_dump_file (opt_pass *pass)
2140 timevar_push (TV_DUMP);
2142 /* Flush and close dump file. */
2143 if (dump_file_name)
2145 free (CONST_CAST (char *, dump_file_name));
2146 dump_file_name = NULL;
2149 g->get_dumps ()->dump_finish (pass->static_pass_number);
2150 timevar_pop (TV_DUMP);
2153 /* After executing the pass, apply expected changes to the function
2154 properties. */
2156 static void
2157 update_properties_after_pass (function *fn, void *data)
2159 opt_pass *pass = (opt_pass *) data;
2160 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2161 & ~pass->properties_destroyed;
2164 /* Execute summary generation for all of the passes in IPA_PASS. */
2166 void
2167 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2169 while (ipa_pass)
2171 opt_pass *pass = ipa_pass;
2173 /* Execute all of the IPA_PASSes in the list. */
2174 if (ipa_pass->type == IPA_PASS
2175 && pass->gate (cfun)
2176 && ipa_pass->generate_summary)
2178 pass_init_dump_file (pass);
2180 /* If a timevar is present, start it. */
2181 if (pass->tv_id)
2182 timevar_push (pass->tv_id);
2184 current_pass = pass;
2185 ipa_pass->generate_summary ();
2187 /* Stop timevar. */
2188 if (pass->tv_id)
2189 timevar_pop (pass->tv_id);
2191 pass_fini_dump_file (pass);
2193 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2197 /* Execute IPA_PASS function transform on NODE. */
2199 static void
2200 execute_one_ipa_transform_pass (struct cgraph_node *node,
2201 ipa_opt_pass_d *ipa_pass)
2203 opt_pass *pass = ipa_pass;
2204 unsigned int todo_after = 0;
2206 current_pass = pass;
2207 if (!ipa_pass->function_transform)
2208 return;
2210 /* Note that the folders should only create gimple expressions.
2211 This is a hack until the new folder is ready. */
2212 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2214 pass_init_dump_file (pass);
2216 /* Run pre-pass verification. */
2217 execute_todo (ipa_pass->function_transform_todo_flags_start);
2219 /* If a timevar is present, start it. */
2220 if (pass->tv_id != TV_NONE)
2221 timevar_push (pass->tv_id);
2223 /* Do it! */
2224 todo_after = ipa_pass->function_transform (node);
2226 /* Stop timevar. */
2227 if (pass->tv_id != TV_NONE)
2228 timevar_pop (pass->tv_id);
2230 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2231 check_profile_consistency (pass->static_pass_number, 0, true);
2233 /* Run post-pass cleanup and verification. */
2234 execute_todo (todo_after);
2235 verify_interpass_invariants ();
2236 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2237 check_profile_consistency (pass->static_pass_number, 1, true);
2239 if (dump_file)
2240 do_per_function (execute_function_dump, NULL);
2241 pass_fini_dump_file (pass);
2243 current_pass = NULL;
2245 /* Signal this is a suitable GC collection point. */
2246 if (!(todo_after & TODO_do_not_ggc_collect))
2247 ggc_collect ();
2250 /* For the current function, execute all ipa transforms. */
2252 void
2253 execute_all_ipa_transforms (void)
2255 struct cgraph_node *node;
2256 if (!cfun)
2257 return;
2258 node = cgraph_node::get (current_function_decl);
2260 if (node->ipa_transforms_to_apply.exists ())
2262 unsigned int i;
2264 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2265 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2266 node->ipa_transforms_to_apply.release ();
2270 /* Check if PASS is explicitly disabled or enabled and return
2271 the gate status. FUNC is the function to be processed, and
2272 GATE_STATUS is the gate status determined by pass manager by
2273 default. */
2275 static bool
2276 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2278 bool explicitly_enabled = false;
2279 bool explicitly_disabled = false;
2281 explicitly_enabled
2282 = is_pass_explicitly_enabled_or_disabled (pass, func,
2283 enabled_pass_uid_range_tab);
2284 explicitly_disabled
2285 = is_pass_explicitly_enabled_or_disabled (pass, func,
2286 disabled_pass_uid_range_tab);
2288 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2290 return gate_status;
2294 /* Execute PASS. */
2296 bool
2297 execute_one_pass (opt_pass *pass)
2299 unsigned int todo_after = 0;
2301 bool gate_status;
2303 /* IPA passes are executed on whole program, so cfun should be NULL.
2304 Other passes need function context set. */
2305 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2306 gcc_assert (!cfun && !current_function_decl);
2307 else
2308 gcc_assert (cfun && current_function_decl);
2310 current_pass = pass;
2312 /* Check whether gate check should be avoided.
2313 User controls the value of the gate through the parameter "gate_status". */
2314 gate_status = pass->gate (cfun);
2315 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2317 /* Override gate with plugin. */
2318 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2320 if (!gate_status)
2322 /* Run so passes selectively disabling themselves on a given function
2323 are not miscounted. */
2324 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2326 check_profile_consistency (pass->static_pass_number, 0, false);
2327 check_profile_consistency (pass->static_pass_number, 1, false);
2329 current_pass = NULL;
2330 return false;
2333 /* Pass execution event trigger: useful to identify passes being
2334 executed. */
2335 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2337 if (!quiet_flag && !cfun)
2338 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2340 /* Note that the folders should only create gimple expressions.
2341 This is a hack until the new folder is ready. */
2342 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2344 pass_init_dump_file (pass);
2346 /* Run pre-pass verification. */
2347 execute_todo (pass->todo_flags_start);
2349 #ifdef ENABLE_CHECKING
2350 do_per_function (verify_curr_properties,
2351 (void *)(size_t)pass->properties_required);
2352 #endif
2354 /* If a timevar is present, start it. */
2355 if (pass->tv_id != TV_NONE)
2356 timevar_push (pass->tv_id);
2358 /* Do it! */
2359 todo_after = pass->execute (cfun);
2360 do_per_function (clear_last_verified, NULL);
2362 /* Stop timevar. */
2363 if (pass->tv_id != TV_NONE)
2364 timevar_pop (pass->tv_id);
2366 do_per_function (update_properties_after_pass, pass);
2368 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2369 check_profile_consistency (pass->static_pass_number, 0, true);
2371 /* Run post-pass cleanup and verification. */
2372 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2373 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2374 check_profile_consistency (pass->static_pass_number, 1, true);
2376 verify_interpass_invariants ();
2377 if (dump_file)
2378 do_per_function (execute_function_dump, pass);
2379 if (pass->type == IPA_PASS)
2381 struct cgraph_node *node;
2382 if (((ipa_opt_pass_d *)pass)->function_transform)
2383 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2384 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2387 if (!current_function_decl)
2388 symtab->process_new_functions ();
2390 pass_fini_dump_file (pass);
2392 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2393 gcc_assert (!(cfun->curr_properties & PROP_trees)
2394 || pass->type != RTL_PASS);
2396 current_pass = NULL;
2398 /* Signal this is a suitable GC collection point. */
2399 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2400 ggc_collect ();
2402 return true;
2405 static void
2406 execute_pass_list_1 (opt_pass *pass)
2410 gcc_assert (pass->type == GIMPLE_PASS
2411 || pass->type == RTL_PASS);
2412 if (execute_one_pass (pass) && pass->sub)
2413 execute_pass_list_1 (pass->sub);
2414 pass = pass->next;
2416 while (pass);
2419 void
2420 execute_pass_list (function *fn, opt_pass *pass)
2422 push_cfun (fn);
2423 execute_pass_list_1 (pass);
2424 if (fn->cfg)
2426 free_dominance_info (CDI_DOMINATORS);
2427 free_dominance_info (CDI_POST_DOMINATORS);
2429 pop_cfun ();
2432 /* Write out all LTO data. */
2433 static void
2434 write_lto (void)
2436 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2437 lto_output ();
2438 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2439 timevar_push (TV_IPA_LTO_DECL_OUT);
2440 produce_asm_for_decls ();
2441 timevar_pop (TV_IPA_LTO_DECL_OUT);
2444 /* Same as execute_pass_list but assume that subpasses of IPA passes
2445 are local passes. If SET is not NULL, write out summaries of only
2446 those node in SET. */
2448 static void
2449 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2451 while (pass)
2453 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2454 gcc_assert (!current_function_decl);
2455 gcc_assert (!cfun);
2456 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2457 if (pass->type == IPA_PASS
2458 && ipa_pass->write_summary
2459 && pass->gate (cfun))
2461 /* If a timevar is present, start it. */
2462 if (pass->tv_id)
2463 timevar_push (pass->tv_id);
2465 pass_init_dump_file (pass);
2467 current_pass = pass;
2468 ipa_pass->write_summary ();
2470 pass_fini_dump_file (pass);
2472 /* If a timevar is present, start it. */
2473 if (pass->tv_id)
2474 timevar_pop (pass->tv_id);
2477 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2478 ipa_write_summaries_2 (pass->sub, state);
2480 pass = pass->next;
2484 /* Helper function of ipa_write_summaries. Creates and destroys the
2485 decl state and calls ipa_write_summaries_2 for all passes that have
2486 summaries. SET is the set of nodes to be written. */
2488 static void
2489 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2491 pass_manager *passes = g->get_passes ();
2492 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2493 state->symtab_node_encoder = encoder;
2495 lto_output_init_mode_table ();
2496 lto_push_out_decl_state (state);
2498 gcc_assert (!flag_wpa);
2499 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2501 write_lto ();
2503 gcc_assert (lto_get_out_decl_state () == state);
2504 lto_pop_out_decl_state ();
2505 lto_delete_out_decl_state (state);
2508 /* Write out summaries for all the nodes in the callgraph. */
2510 void
2511 ipa_write_summaries (void)
2513 lto_symtab_encoder_t encoder;
2514 int i, order_pos;
2515 varpool_node *vnode;
2516 struct cgraph_node *node;
2517 struct cgraph_node **order;
2519 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2520 return;
2522 select_what_to_stream ();
2524 encoder = lto_symtab_encoder_new (false);
2526 /* Create the callgraph set in the same order used in
2527 cgraph_expand_all_functions. This mostly facilitates debugging,
2528 since it causes the gimple file to be processed in the same order
2529 as the source code. */
2530 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2531 order_pos = ipa_reverse_postorder (order);
2532 gcc_assert (order_pos == symtab->cgraph_count);
2534 for (i = order_pos - 1; i >= 0; i--)
2536 struct cgraph_node *node = order[i];
2538 if (node->has_gimple_body_p ())
2540 /* When streaming out references to statements as part of some IPA
2541 pass summary, the statements need to have uids assigned and the
2542 following does that for all the IPA passes here. Naturally, this
2543 ordering then matches the one IPA-passes get in their stmt_fixup
2544 hooks. */
2546 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2547 renumber_gimple_stmt_uids ();
2548 pop_cfun ();
2550 if (node->definition && node->need_lto_streaming)
2551 lto_set_symtab_encoder_in_partition (encoder, node);
2554 FOR_EACH_DEFINED_FUNCTION (node)
2555 if (node->alias && node->need_lto_streaming)
2556 lto_set_symtab_encoder_in_partition (encoder, node);
2557 FOR_EACH_DEFINED_VARIABLE (vnode)
2558 if (vnode->need_lto_streaming)
2559 lto_set_symtab_encoder_in_partition (encoder, vnode);
2561 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2563 free (order);
2566 /* Same as execute_pass_list but assume that subpasses of IPA passes
2567 are local passes. If SET is not NULL, write out optimization summaries of
2568 only those node in SET. */
2570 static void
2571 ipa_write_optimization_summaries_1 (opt_pass *pass,
2572 struct lto_out_decl_state *state)
2574 while (pass)
2576 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2577 gcc_assert (!current_function_decl);
2578 gcc_assert (!cfun);
2579 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2580 if (pass->type == IPA_PASS
2581 && ipa_pass->write_optimization_summary
2582 && pass->gate (cfun))
2584 /* If a timevar is present, start it. */
2585 if (pass->tv_id)
2586 timevar_push (pass->tv_id);
2588 pass_init_dump_file (pass);
2590 current_pass = pass;
2591 ipa_pass->write_optimization_summary ();
2593 pass_fini_dump_file (pass);
2595 /* If a timevar is present, start it. */
2596 if (pass->tv_id)
2597 timevar_pop (pass->tv_id);
2600 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2601 ipa_write_optimization_summaries_1 (pass->sub, state);
2603 pass = pass->next;
2607 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2608 NULL, write out all summaries of all nodes. */
2610 void
2611 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2613 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2614 lto_symtab_encoder_iterator lsei;
2615 state->symtab_node_encoder = encoder;
2617 lto_output_init_mode_table ();
2618 lto_push_out_decl_state (state);
2619 for (lsei = lsei_start_function_in_partition (encoder);
2620 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2622 struct cgraph_node *node = lsei_cgraph_node (lsei);
2623 /* When streaming out references to statements as part of some IPA
2624 pass summary, the statements need to have uids assigned.
2626 For functions newly born at WPA stage we need to initialize
2627 the uids here. */
2628 if (node->definition
2629 && gimple_has_body_p (node->decl))
2631 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2632 renumber_gimple_stmt_uids ();
2633 pop_cfun ();
2637 gcc_assert (flag_wpa);
2638 pass_manager *passes = g->get_passes ();
2639 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2641 write_lto ();
2643 gcc_assert (lto_get_out_decl_state () == state);
2644 lto_pop_out_decl_state ();
2645 lto_delete_out_decl_state (state);
2648 /* Same as execute_pass_list but assume that subpasses of IPA passes
2649 are local passes. */
2651 static void
2652 ipa_read_summaries_1 (opt_pass *pass)
2654 while (pass)
2656 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2658 gcc_assert (!current_function_decl);
2659 gcc_assert (!cfun);
2660 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2662 if (pass->gate (cfun))
2664 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2666 /* If a timevar is present, start it. */
2667 if (pass->tv_id)
2668 timevar_push (pass->tv_id);
2670 pass_init_dump_file (pass);
2672 current_pass = pass;
2673 ipa_pass->read_summary ();
2675 pass_fini_dump_file (pass);
2677 /* Stop timevar. */
2678 if (pass->tv_id)
2679 timevar_pop (pass->tv_id);
2682 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2683 ipa_read_summaries_1 (pass->sub);
2685 pass = pass->next;
2690 /* Read all the summaries for all_regular_ipa_passes. */
2692 void
2693 ipa_read_summaries (void)
2695 pass_manager *passes = g->get_passes ();
2696 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2699 /* Same as execute_pass_list but assume that subpasses of IPA passes
2700 are local passes. */
2702 static void
2703 ipa_read_optimization_summaries_1 (opt_pass *pass)
2705 while (pass)
2707 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2709 gcc_assert (!current_function_decl);
2710 gcc_assert (!cfun);
2711 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2713 if (pass->gate (cfun))
2715 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2717 /* If a timevar is present, start it. */
2718 if (pass->tv_id)
2719 timevar_push (pass->tv_id);
2721 pass_init_dump_file (pass);
2723 current_pass = pass;
2724 ipa_pass->read_optimization_summary ();
2726 pass_fini_dump_file (pass);
2728 /* Stop timevar. */
2729 if (pass->tv_id)
2730 timevar_pop (pass->tv_id);
2733 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2734 ipa_read_optimization_summaries_1 (pass->sub);
2736 pass = pass->next;
2740 /* Read all the summaries for all_regular_ipa_passes. */
2742 void
2743 ipa_read_optimization_summaries (void)
2745 pass_manager *passes = g->get_passes ();
2746 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2749 /* Same as execute_pass_list but assume that subpasses of IPA passes
2750 are local passes. */
2751 void
2752 execute_ipa_pass_list (opt_pass *pass)
2756 gcc_assert (!current_function_decl);
2757 gcc_assert (!cfun);
2758 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2759 if (execute_one_pass (pass) && pass->sub)
2761 if (pass->sub->type == GIMPLE_PASS)
2763 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2764 do_per_function_toporder ((void (*)(function *, void *))
2765 execute_pass_list,
2766 pass->sub);
2767 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2769 else if (pass->sub->type == SIMPLE_IPA_PASS
2770 || pass->sub->type == IPA_PASS)
2771 execute_ipa_pass_list (pass->sub);
2772 else
2773 gcc_unreachable ();
2775 gcc_assert (!current_function_decl);
2776 symtab->process_new_functions ();
2777 pass = pass->next;
2779 while (pass);
2782 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2784 static void
2785 execute_ipa_stmt_fixups (opt_pass *pass,
2786 struct cgraph_node *node, gimple *stmts)
2788 while (pass)
2790 /* Execute all of the IPA_PASSes in the list. */
2791 if (pass->type == IPA_PASS
2792 && pass->gate (cfun))
2794 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2796 if (ipa_pass->stmt_fixup)
2798 pass_init_dump_file (pass);
2799 /* If a timevar is present, start it. */
2800 if (pass->tv_id)
2801 timevar_push (pass->tv_id);
2803 current_pass = pass;
2804 ipa_pass->stmt_fixup (node, stmts);
2806 /* Stop timevar. */
2807 if (pass->tv_id)
2808 timevar_pop (pass->tv_id);
2809 pass_fini_dump_file (pass);
2811 if (pass->sub)
2812 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2814 pass = pass->next;
2818 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2820 void
2821 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2823 pass_manager *passes = g->get_passes ();
2824 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2828 extern void debug_properties (unsigned int);
2829 extern void dump_properties (FILE *, unsigned int);
2831 DEBUG_FUNCTION void
2832 dump_properties (FILE *dump, unsigned int props)
2834 fprintf (dump, "Properties:\n");
2835 if (props & PROP_gimple_any)
2836 fprintf (dump, "PROP_gimple_any\n");
2837 if (props & PROP_gimple_lcf)
2838 fprintf (dump, "PROP_gimple_lcf\n");
2839 if (props & PROP_gimple_leh)
2840 fprintf (dump, "PROP_gimple_leh\n");
2841 if (props & PROP_cfg)
2842 fprintf (dump, "PROP_cfg\n");
2843 if (props & PROP_ssa)
2844 fprintf (dump, "PROP_ssa\n");
2845 if (props & PROP_no_crit_edges)
2846 fprintf (dump, "PROP_no_crit_edges\n");
2847 if (props & PROP_rtl)
2848 fprintf (dump, "PROP_rtl\n");
2849 if (props & PROP_gimple_lomp)
2850 fprintf (dump, "PROP_gimple_lomp\n");
2851 if (props & PROP_gimple_lcx)
2852 fprintf (dump, "PROP_gimple_lcx\n");
2853 if (props & PROP_gimple_lvec)
2854 fprintf (dump, "PROP_gimple_lvec\n");
2855 if (props & PROP_cfglayout)
2856 fprintf (dump, "PROP_cfglayout\n");
2859 DEBUG_FUNCTION void
2860 debug_properties (unsigned int props)
2862 dump_properties (stderr, props);
2865 /* Called by local passes to see if function is called by already processed nodes.
2866 Because we process nodes in topological order, this means that function is
2867 in recursive cycle or we introduced new direct calls. */
2868 bool
2869 function_called_by_processed_nodes_p (void)
2871 struct cgraph_edge *e;
2872 for (e = cgraph_node::get (current_function_decl)->callers;
2874 e = e->next_caller)
2876 if (e->caller->decl == current_function_decl)
2877 continue;
2878 if (!e->caller->has_gimple_body_p ())
2879 continue;
2880 if (TREE_ASM_WRITTEN (e->caller->decl))
2881 continue;
2882 if (!e->caller->process && !e->caller->global.inlined_to)
2883 break;
2885 if (dump_file && e)
2887 fprintf (dump_file, "Already processed call to:\n");
2888 e->caller->dump (dump_file);
2890 return e != NULL;
2893 #include "gt-passes.h"