[PR67828] don't unswitch on default defs of non-parms
[official-gcc.git] / gcc / passes.c
blob5b41102e04f126b19f276ec1267cc28c607ca1be
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This is the top level of cc1/c++.
21 It parses command args, opens files, invokes the various passes
22 in the proper order, and counts the time used by each.
23 Error messages and low-level interface to malloc also handled here. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "cfghooks.h"
30 #include "tree.h"
31 #include "gimple.h"
32 #include "rtl.h"
33 #include "df.h"
34 #include "ssa.h"
35 #include "alias.h"
36 #include "fold-const.h"
37 #include "varasm.h"
38 #include "tm_p.h"
39 #include "flags.h"
40 #include "insn-attr.h"
41 #include "insn-config.h"
42 #include "insn-flags.h"
43 #include "recog.h"
44 #include "output.h"
45 #include "except.h"
46 #include "toplev.h"
47 #include "expmed.h"
48 #include "dojump.h"
49 #include "explow.h"
50 #include "calls.h"
51 #include "emit-rtl.h"
52 #include "stmt.h"
53 #include "expr.h"
54 #include "intl.h"
55 #include "graph.h"
56 #include "regs.h"
57 #include "diagnostic-core.h"
58 #include "params.h"
59 #include "reload.h"
60 #include "debug.h"
61 #include "target.h"
62 #include "langhooks.h"
63 #include "cfgloop.h"
64 #include "hosthooks.h"
65 #include "opts.h"
66 #include "coverage.h"
67 #include "value-prof.h"
68 #include "tree-inline.h"
69 #include "internal-fn.h"
70 #include "tree-cfg.h"
71 #include "tree-ssa-loop-manip.h"
72 #include "tree-into-ssa.h"
73 #include "tree-dfa.h"
74 #include "tree-ssa.h"
75 #include "tree-pass.h"
76 #include "tree-dump.h"
77 #include "cgraph.h"
78 #include "lto-streamer.h"
79 #include "plugin.h"
80 #include "ipa-utils.h"
81 #include "tree-pretty-print.h" /* for dump_function_header */
82 #include "context.h"
83 #include "pass_manager.h"
84 #include "cfgrtl.h"
85 #include "tree-ssa-live.h" /* For remove_unused_locals. */
86 #include "tree-cfgcleanup.h"
87 #include "tree-ssanames.h"
89 using namespace gcc;
91 /* This is used for debugging. It allows the current pass to printed
92 from anywhere in compilation.
93 The variable current_pass is also used for statistics and plugins. */
94 opt_pass *current_pass;
96 static void register_pass_name (opt_pass *, const char *);
98 /* Most passes are single-instance (within their context) and thus don't
99 need to implement cloning, but passes that support multiple instances
100 *must* provide their own implementation of the clone method.
102 Handle this by providing a default implemenation, but make it a fatal
103 error to call it. */
105 opt_pass *
106 opt_pass::clone ()
108 internal_error ("pass %s does not support cloning", name);
111 bool
112 opt_pass::gate (function *)
114 return true;
117 unsigned int
118 opt_pass::execute (function *)
120 return 0;
123 opt_pass::opt_pass (const pass_data &data, context *ctxt)
124 : pass_data (data),
125 sub (NULL),
126 next (NULL),
127 static_pass_number (0),
128 m_ctxt (ctxt)
133 void
134 pass_manager::execute_early_local_passes ()
136 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
137 if (flag_check_pointer_bounds)
138 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
139 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
142 unsigned int
143 pass_manager::execute_pass_mode_switching ()
145 return pass_mode_switching_1->execute (cfun);
149 /* Call from anywhere to find out what pass this is. Useful for
150 printing out debugging information deep inside an service
151 routine. */
152 void
153 print_current_pass (FILE *file)
155 if (current_pass)
156 fprintf (file, "current pass = %s (%d)\n",
157 current_pass->name, current_pass->static_pass_number);
158 else
159 fprintf (file, "no current pass.\n");
163 /* Call from the debugger to get the current pass name. */
164 DEBUG_FUNCTION void
165 debug_pass (void)
167 print_current_pass (stderr);
172 /* Global variables used to communicate with passes. */
173 bool in_gimple_form;
174 bool first_pass_instance;
177 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
178 and TYPE_DECL nodes.
180 This does nothing for local (non-static) variables, unless the
181 variable is a register variable with DECL_ASSEMBLER_NAME set. In
182 that case, or if the variable is not an automatic, it sets up the
183 RTL and outputs any assembler code (label definition, storage
184 allocation and initialization).
186 DECL is the declaration. TOP_LEVEL is nonzero
187 if this declaration is not within a function. */
189 void
190 rest_of_decl_compilation (tree decl,
191 int top_level,
192 int at_end)
194 bool finalize = true;
196 /* We deferred calling assemble_alias so that we could collect
197 other attributes such as visibility. Emit the alias now. */
198 if (!in_lto_p)
200 tree alias;
201 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
202 if (alias)
204 alias = TREE_VALUE (TREE_VALUE (alias));
205 alias = get_identifier (TREE_STRING_POINTER (alias));
206 /* A quirk of the initial implementation of aliases required that the
207 user add "extern" to all of them. Which is silly, but now
208 historical. Do note that the symbol is in fact locally defined. */
209 DECL_EXTERNAL (decl) = 0;
210 TREE_STATIC (decl) = 1;
211 assemble_alias (decl, alias);
212 finalize = false;
216 /* Can't defer this, because it needs to happen before any
217 later function definitions are processed. */
218 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
219 make_decl_rtl (decl);
221 /* Forward declarations for nested functions are not "external",
222 but we need to treat them as if they were. */
223 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
224 || TREE_CODE (decl) == FUNCTION_DECL)
226 timevar_push (TV_VARCONST);
228 /* Don't output anything when a tentative file-scope definition
229 is seen. But at end of compilation, do output code for them.
231 We do output all variables and rely on
232 callgraph code to defer them except for forward declarations
233 (see gcc.c-torture/compile/920624-1.c) */
234 if ((at_end
235 || !DECL_DEFER_OUTPUT (decl)
236 || DECL_INITIAL (decl))
237 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
238 && !DECL_EXTERNAL (decl))
240 /* When reading LTO unit, we also read varpool, so do not
241 rebuild it. */
242 if (in_lto_p && !at_end)
244 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
245 varpool_node::finalize_decl (decl);
248 #ifdef ASM_FINISH_DECLARE_OBJECT
249 if (decl == last_assemble_variable_decl)
251 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
252 top_level, at_end);
254 #endif
256 timevar_pop (TV_VARCONST);
258 else if (TREE_CODE (decl) == TYPE_DECL
259 /* Like in rest_of_type_compilation, avoid confusing the debug
260 information machinery when there are errors. */
261 && !seen_error ())
263 timevar_push (TV_SYMOUT);
264 debug_hooks->type_decl (decl, !top_level);
265 timevar_pop (TV_SYMOUT);
268 /* Let cgraph know about the existence of variables. */
269 if (in_lto_p && !at_end)
271 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
272 && TREE_STATIC (decl))
273 varpool_node::get_create (decl);
275 /* Generate early debug for global variables. Any local variables will
276 be handled by either handling reachable functions from
277 finalize_compilation_unit (and by consequence, locally scoped
278 symbols), or by rest_of_type_compilation below.
280 Also, pick up function prototypes, which will be mostly ignored
281 by the different early_global_decl() hooks, but will at least be
282 used by Go's hijack of the debug_hooks to implement
283 -fdump-go-spec. */
284 if (!in_lto_p
285 && (TREE_CODE (decl) != FUNCTION_DECL
286 /* This will pick up function prototypes with no bodies,
287 which are not visible in finalize_compilation_unit()
288 while iterating with FOR_EACH_*_FUNCTION through the
289 symbol table. */
290 || !DECL_SAVED_TREE (decl))
292 /* We need to check both decl_function_context and
293 current_function_decl here to make sure local extern
294 declarations end up with the correct context.
296 For local extern declarations, decl_function_context is
297 empty, but current_function_decl is set to the function where
298 the extern was declared . Without the check for
299 !current_function_decl below, the local extern ends up
300 incorrectly with a top-level context.
302 For example:
304 namespace S
310 int i = 42;
312 extern int i; // Local extern declaration.
313 return i;
319 && !decl_function_context (decl)
320 && !current_function_decl
321 && DECL_SOURCE_LOCATION (decl) != BUILTINS_LOCATION
322 && (!decl_type_context (decl)
323 /* If we created a varpool node for the decl make sure to
324 call early_global_decl. Otherwise we miss changes
325 introduced by member definitions like
326 struct A { static int staticdatamember; };
327 int A::staticdatamember;
328 and thus have incomplete early debug and late debug
329 called from varpool node removal fails to handle it
330 properly. */
331 || (finalize
332 && TREE_CODE (decl) == VAR_DECL
333 && TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
334 /* Avoid confusing the debug information machinery when there are
335 errors. */
336 && !seen_error ())
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 static hash_map<nofree_string_hash, opt_pass *> *name_to_pass_map;
866 /* Register PASS with NAME. */
868 static void
869 register_pass_name (opt_pass *pass, const char *name)
871 if (!name_to_pass_map)
872 name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
874 if (name_to_pass_map->get (name))
875 return; /* Ignore plugin passes. */
877 const char *unique_name = xstrdup (name);
878 name_to_pass_map->put (unique_name, pass);
881 /* Map from pass id to canonicalized pass name. */
883 typedef const char *char_ptr;
884 static vec<char_ptr> pass_tab = vNULL;
886 /* Callback function for traversing NAME_TO_PASS_MAP. */
888 bool
889 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
891 gcc_assert (pass->static_pass_number > 0);
892 gcc_assert (pass_tab.exists ());
894 pass_tab[pass->static_pass_number] = name;
896 return 1;
899 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
900 table for dumping purpose. */
902 static void
903 create_pass_tab (void)
905 if (!flag_dump_passes)
906 return;
908 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
909 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
912 static bool override_gate_status (opt_pass *, tree, bool);
914 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
915 is turned on or not. */
917 static void
918 dump_one_pass (opt_pass *pass, int pass_indent)
920 int indent = 3 * pass_indent;
921 const char *pn;
922 bool is_on, is_really_on;
924 is_on = pass->gate (cfun);
925 is_really_on = override_gate_status (pass, current_function_decl, is_on);
927 if (pass->static_pass_number <= 0)
928 pn = pass->name;
929 else
930 pn = pass_tab[pass->static_pass_number];
932 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
933 (15 - indent < 0 ? 0 : 15 - indent), " ",
934 is_on ? " ON" : " OFF",
935 ((!is_on) == (!is_really_on) ? ""
936 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
939 /* Dump pass list PASS with indentation INDENT. */
941 static void
942 dump_pass_list (opt_pass *pass, int indent)
946 dump_one_pass (pass, indent);
947 if (pass->sub)
948 dump_pass_list (pass->sub, indent + 1);
949 pass = pass->next;
951 while (pass);
954 /* Dump all optimization passes. */
956 void
957 dump_passes (void)
959 g->get_passes ()->dump_passes ();
962 void
963 pass_manager::dump_passes () const
965 push_dummy_function (true);
967 create_pass_tab ();
969 dump_pass_list (all_lowering_passes, 1);
970 dump_pass_list (all_small_ipa_passes, 1);
971 dump_pass_list (all_regular_ipa_passes, 1);
972 dump_pass_list (all_late_ipa_passes, 1);
973 dump_pass_list (all_passes, 1);
975 pop_dummy_function ();
978 /* Returns the pass with NAME. */
980 static opt_pass *
981 get_pass_by_name (const char *name)
983 opt_pass **p = name_to_pass_map->get (name);
984 if (p)
985 return *p;
987 return NULL;
991 /* Range [start, last]. */
993 struct uid_range
995 unsigned int start;
996 unsigned int last;
997 const char *assem_name;
998 struct uid_range *next;
1001 typedef struct uid_range *uid_range_p;
1004 static vec<uid_range_p>
1005 enabled_pass_uid_range_tab = vNULL;
1006 static vec<uid_range_p>
1007 disabled_pass_uid_range_tab = vNULL;
1010 /* Parse option string for -fdisable- and -fenable-
1011 The syntax of the options:
1013 -fenable-<pass_name>
1014 -fdisable-<pass_name>
1016 -fenable-<pass_name>=s1:e1,s2:e2,...
1017 -fdisable-<pass_name>=s1:e1,s2:e2,...
1020 static void
1021 enable_disable_pass (const char *arg, bool is_enable)
1023 opt_pass *pass;
1024 char *range_str, *phase_name;
1025 char *argstr = xstrdup (arg);
1026 vec<uid_range_p> *tab = 0;
1028 range_str = strchr (argstr,'=');
1029 if (range_str)
1031 *range_str = '\0';
1032 range_str++;
1035 phase_name = argstr;
1036 if (!*phase_name)
1038 if (is_enable)
1039 error ("unrecognized option -fenable");
1040 else
1041 error ("unrecognized option -fdisable");
1042 free (argstr);
1043 return;
1045 pass = get_pass_by_name (phase_name);
1046 if (!pass || pass->static_pass_number == -1)
1048 if (is_enable)
1049 error ("unknown pass %s specified in -fenable", phase_name);
1050 else
1051 error ("unknown pass %s specified in -fdisable", phase_name);
1052 free (argstr);
1053 return;
1056 if (is_enable)
1057 tab = &enabled_pass_uid_range_tab;
1058 else
1059 tab = &disabled_pass_uid_range_tab;
1061 if ((unsigned) pass->static_pass_number >= tab->length ())
1062 tab->safe_grow_cleared (pass->static_pass_number + 1);
1064 if (!range_str)
1066 uid_range_p slot;
1067 uid_range_p new_range = XCNEW (struct uid_range);
1069 new_range->start = 0;
1070 new_range->last = (unsigned)-1;
1072 slot = (*tab)[pass->static_pass_number];
1073 new_range->next = slot;
1074 (*tab)[pass->static_pass_number] = new_range;
1075 if (is_enable)
1076 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1077 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1078 else
1079 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1080 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1082 else
1084 char *next_range = NULL;
1085 char *one_range = range_str;
1086 char *end_val = NULL;
1090 uid_range_p slot;
1091 uid_range_p new_range;
1092 char *invalid = NULL;
1093 long start;
1094 char *func_name = NULL;
1096 next_range = strchr (one_range, ',');
1097 if (next_range)
1099 *next_range = '\0';
1100 next_range++;
1103 end_val = strchr (one_range, ':');
1104 if (end_val)
1106 *end_val = '\0';
1107 end_val++;
1109 start = strtol (one_range, &invalid, 10);
1110 if (*invalid || start < 0)
1112 if (end_val || (one_range[0] >= '0'
1113 && one_range[0] <= '9'))
1115 error ("Invalid range %s in option %s",
1116 one_range,
1117 is_enable ? "-fenable" : "-fdisable");
1118 free (argstr);
1119 return;
1121 func_name = one_range;
1123 if (!end_val)
1125 new_range = XCNEW (struct uid_range);
1126 if (!func_name)
1128 new_range->start = (unsigned) start;
1129 new_range->last = (unsigned) start;
1131 else
1133 new_range->start = (unsigned) -1;
1134 new_range->last = (unsigned) -1;
1135 new_range->assem_name = xstrdup (func_name);
1138 else
1140 long last = strtol (end_val, &invalid, 10);
1141 if (*invalid || last < start)
1143 error ("Invalid range %s in option %s",
1144 end_val,
1145 is_enable ? "-fenable" : "-fdisable");
1146 free (argstr);
1147 return;
1149 new_range = XCNEW (struct uid_range);
1150 new_range->start = (unsigned) start;
1151 new_range->last = (unsigned) last;
1154 slot = (*tab)[pass->static_pass_number];
1155 new_range->next = slot;
1156 (*tab)[pass->static_pass_number] = new_range;
1157 if (is_enable)
1159 if (new_range->assem_name)
1160 inform (UNKNOWN_LOCATION,
1161 "enable pass %s for function %s",
1162 phase_name, new_range->assem_name);
1163 else
1164 inform (UNKNOWN_LOCATION,
1165 "enable pass %s for functions in the range of [%u, %u]",
1166 phase_name, new_range->start, new_range->last);
1168 else
1170 if (new_range->assem_name)
1171 inform (UNKNOWN_LOCATION,
1172 "disable pass %s for function %s",
1173 phase_name, new_range->assem_name);
1174 else
1175 inform (UNKNOWN_LOCATION,
1176 "disable pass %s for functions in the range of [%u, %u]",
1177 phase_name, new_range->start, new_range->last);
1180 one_range = next_range;
1181 } while (next_range);
1184 free (argstr);
1187 /* Enable pass specified by ARG. */
1189 void
1190 enable_pass (const char *arg)
1192 enable_disable_pass (arg, true);
1195 /* Disable pass specified by ARG. */
1197 void
1198 disable_pass (const char *arg)
1200 enable_disable_pass (arg, false);
1203 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1205 static bool
1206 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1207 tree func,
1208 vec<uid_range_p> tab)
1210 uid_range_p slot, range;
1211 int cgraph_uid;
1212 const char *aname = NULL;
1214 if (!tab.exists ()
1215 || (unsigned) pass->static_pass_number >= tab.length ()
1216 || pass->static_pass_number == -1)
1217 return false;
1219 slot = tab[pass->static_pass_number];
1220 if (!slot)
1221 return false;
1223 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1224 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1225 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1227 range = slot;
1228 while (range)
1230 if ((unsigned) cgraph_uid >= range->start
1231 && (unsigned) cgraph_uid <= range->last)
1232 return true;
1233 if (range->assem_name && aname
1234 && !strcmp (range->assem_name, aname))
1235 return true;
1236 range = range->next;
1239 return false;
1243 /* Update static_pass_number for passes (and the flag
1244 TODO_mark_first_instance).
1246 Passes are constructed with static_pass_number preinitialized to 0
1248 This field is used in two different ways: initially as instance numbers
1249 of their kind, and then as ids within the entire pass manager.
1251 Within pass_manager::pass_manager:
1253 * In add_pass_instance(), as called by next_pass_1 in
1254 NEXT_PASS in init_optimization_passes
1256 * When the initial instance of a pass within a pass manager is seen,
1257 it is flagged, and its static_pass_number is set to -1
1259 * On subsequent times that it is seen, the static pass number
1260 is decremented each time, so that if there are e.g. 4 dups,
1261 they have static_pass_number -4, 2, 3, 4 respectively (note
1262 how the initial one is negative and gives the count); these
1263 can be thought of as instance numbers of the specific pass
1265 * Within the register_dump_files () traversal, set_pass_for_id()
1266 is called on each pass, using these instance numbers to create
1267 dumpfile switches, and then overwriting them with a pass id,
1268 which are global to the whole pass manager (based on
1269 (TDI_end + current value of extra_dump_files_in_use) ) */
1271 static void
1272 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1273 opt_pass *initial_pass)
1275 /* Are we dealing with the first pass of its kind, or a clone? */
1276 if (new_pass != initial_pass)
1278 /* We're dealing with a clone. */
1279 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1281 /* Indicate to register_dump_files that this pass has duplicates,
1282 and so it should rename the dump file. The first instance will
1283 be -1, and be number of duplicates = -static_pass_number - 1.
1284 Subsequent instances will be > 0 and just the duplicate number. */
1285 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1287 initial_pass->static_pass_number -= 1;
1288 new_pass->static_pass_number = -initial_pass->static_pass_number;
1291 else
1293 /* We're dealing with the first pass of its kind. */
1294 new_pass->todo_flags_start |= TODO_mark_first_instance;
1295 new_pass->static_pass_number = -1;
1297 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1301 /* Add a pass to the pass list. Duplicate the pass if it's already
1302 in the list. */
1304 static opt_pass **
1305 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1307 /* Every pass should have a name so that plugins can refer to them. */
1308 gcc_assert (pass->name != NULL);
1310 add_pass_instance (pass, false, initial_pass);
1311 *list = pass;
1313 return &(*list)->next;
1316 /* List node for an inserted pass instance. We need to keep track of all
1317 the newly-added pass instances (with 'added_pass_nodes' defined below)
1318 so that we can register their dump files after pass-positioning is finished.
1319 Registering dumping files needs to be post-processed or the
1320 static_pass_number of the opt_pass object would be modified and mess up
1321 the dump file names of future pass instances to be added. */
1323 struct pass_list_node
1325 opt_pass *pass;
1326 struct pass_list_node *next;
1329 static struct pass_list_node *added_pass_nodes = NULL;
1330 static struct pass_list_node *prev_added_pass_node;
1332 /* Insert the pass at the proper position. Return true if the pass
1333 is successfully added.
1335 NEW_PASS_INFO - new pass to be inserted
1336 PASS_LIST - root of the pass list to insert the new pass to */
1338 static bool
1339 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1341 opt_pass *pass = *pass_list, *prev_pass = NULL;
1342 bool success = false;
1344 for ( ; pass; prev_pass = pass, pass = pass->next)
1346 /* Check if the current pass is of the same type as the new pass and
1347 matches the name and the instance number of the reference pass. */
1348 if (pass->type == new_pass_info->pass->type
1349 && pass->name
1350 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1351 && ((new_pass_info->ref_pass_instance_number == 0)
1352 || (new_pass_info->ref_pass_instance_number ==
1353 pass->static_pass_number)
1354 || (new_pass_info->ref_pass_instance_number == 1
1355 && pass->todo_flags_start & TODO_mark_first_instance)))
1357 opt_pass *new_pass;
1358 struct pass_list_node *new_pass_node;
1360 if (new_pass_info->ref_pass_instance_number == 0)
1362 new_pass = new_pass_info->pass->clone ();
1363 add_pass_instance (new_pass, true, new_pass_info->pass);
1365 else
1367 new_pass = new_pass_info->pass;
1368 add_pass_instance (new_pass, true, new_pass);
1371 /* Insert the new pass instance based on the positioning op. */
1372 switch (new_pass_info->pos_op)
1374 case PASS_POS_INSERT_AFTER:
1375 new_pass->next = pass->next;
1376 pass->next = new_pass;
1378 /* Skip newly inserted pass to avoid repeated
1379 insertions in the case where the new pass and the
1380 existing one have the same name. */
1381 pass = new_pass;
1382 break;
1383 case PASS_POS_INSERT_BEFORE:
1384 new_pass->next = pass;
1385 if (prev_pass)
1386 prev_pass->next = new_pass;
1387 else
1388 *pass_list = new_pass;
1389 break;
1390 case PASS_POS_REPLACE:
1391 new_pass->next = pass->next;
1392 if (prev_pass)
1393 prev_pass->next = new_pass;
1394 else
1395 *pass_list = new_pass;
1396 new_pass->sub = pass->sub;
1397 new_pass->tv_id = pass->tv_id;
1398 pass = new_pass;
1399 break;
1400 default:
1401 error ("invalid pass positioning operation");
1402 return false;
1405 /* Save the newly added pass (instance) in the added_pass_nodes
1406 list so that we can register its dump file later. Note that
1407 we cannot register the dump file now because doing so will modify
1408 the static_pass_number of the opt_pass object and therefore
1409 mess up the dump file name of future instances. */
1410 new_pass_node = XCNEW (struct pass_list_node);
1411 new_pass_node->pass = new_pass;
1412 if (!added_pass_nodes)
1413 added_pass_nodes = new_pass_node;
1414 else
1415 prev_added_pass_node->next = new_pass_node;
1416 prev_added_pass_node = new_pass_node;
1418 success = true;
1421 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1422 success = true;
1425 return success;
1428 /* Hooks a new pass into the pass lists.
1430 PASS_INFO - pass information that specifies the opt_pass object,
1431 reference pass, instance number, and how to position
1432 the pass */
1434 void
1435 register_pass (struct register_pass_info *pass_info)
1437 g->get_passes ()->register_pass (pass_info);
1440 void
1441 register_pass (opt_pass* pass, pass_positioning_ops pos,
1442 const char* ref_pass_name, int ref_pass_inst_number)
1444 register_pass_info i;
1445 i.pass = pass;
1446 i.reference_pass_name = ref_pass_name;
1447 i.ref_pass_instance_number = ref_pass_inst_number;
1448 i.pos_op = pos;
1450 g->get_passes ()->register_pass (&i);
1453 void
1454 pass_manager::register_pass (struct register_pass_info *pass_info)
1456 bool all_instances, success;
1457 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1459 /* The checks below could fail in buggy plugins. Existing GCC
1460 passes should never fail these checks, so we mention plugin in
1461 the messages. */
1462 if (!pass_info->pass)
1463 fatal_error (input_location, "plugin cannot register a missing pass");
1465 if (!pass_info->pass->name)
1466 fatal_error (input_location, "plugin cannot register an unnamed pass");
1468 if (!pass_info->reference_pass_name)
1469 fatal_error
1470 (input_location,
1471 "plugin cannot register pass %qs without reference pass name",
1472 pass_info->pass->name);
1474 /* Try to insert the new pass to the pass lists. We need to check
1475 all five lists as the reference pass could be in one (or all) of
1476 them. */
1477 all_instances = pass_info->ref_pass_instance_number == 0;
1478 success = position_pass (pass_info, &all_lowering_passes);
1479 if (!success || all_instances)
1480 success |= position_pass (pass_info, &all_small_ipa_passes);
1481 if (!success || all_instances)
1482 success |= position_pass (pass_info, &all_regular_ipa_passes);
1483 if (!success || all_instances)
1484 success |= position_pass (pass_info, &all_late_ipa_passes);
1485 if (!success || all_instances)
1486 success |= position_pass (pass_info, &all_passes);
1487 if (!success)
1488 fatal_error
1489 (input_location,
1490 "pass %qs not found but is referenced by new pass %qs",
1491 pass_info->reference_pass_name, pass_info->pass->name);
1493 /* OK, we have successfully inserted the new pass. We need to register
1494 the dump files for the newly added pass and its duplicates (if any).
1495 Because the registration of plugin/backend passes happens after the
1496 command-line options are parsed, the options that specify single
1497 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1498 passes. Therefore we currently can only enable dumping of
1499 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1500 are specified. While doing so, we also delete the pass_list_node
1501 objects created during pass positioning. */
1502 while (added_pass_nodes)
1504 struct pass_list_node *next_node = added_pass_nodes->next;
1505 enum tree_dump_index tdi;
1506 register_one_dump_file (added_pass_nodes->pass);
1507 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1508 || added_pass_nodes->pass->type == IPA_PASS)
1509 tdi = TDI_ipa_all;
1510 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1511 tdi = TDI_tree_all;
1512 else
1513 tdi = TDI_rtl_all;
1514 /* Check if dump-all flag is specified. */
1515 if (dumps->get_dump_file_info (tdi)->pstate)
1516 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1517 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1518 XDELETE (added_pass_nodes);
1519 added_pass_nodes = next_node;
1523 /* Construct the pass tree. The sequencing of passes is driven by
1524 the cgraph routines:
1526 finalize_compilation_unit ()
1527 for each node N in the cgraph
1528 cgraph_analyze_function (N)
1529 cgraph_lower_function (N) -> all_lowering_passes
1531 If we are optimizing, compile is then invoked:
1533 compile ()
1534 ipa_passes () -> all_small_ipa_passes
1535 -> Analysis of all_regular_ipa_passes
1536 * possible LTO streaming at copmilation time *
1537 -> Execution of all_regular_ipa_passes
1538 * possible LTO streaming at link time *
1539 -> all_late_ipa_passes
1540 expand_all_functions ()
1541 for each node N in the cgraph
1542 expand_function (N) -> Transformation of all_regular_ipa_passes
1543 -> all_passes
1546 void *
1547 pass_manager::operator new (size_t sz)
1549 /* Ensure that all fields of the pass manager are zero-initialized. */
1550 return xcalloc (1, sz);
1553 void
1554 pass_manager::operator delete (void *ptr)
1556 free (ptr);
1559 pass_manager::pass_manager (context *ctxt)
1560 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1561 all_regular_ipa_passes (NULL),
1562 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1563 m_ctxt (ctxt)
1565 opt_pass **p;
1567 /* Initialize the pass_lists array. */
1568 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1569 GCC_PASS_LISTS
1570 #undef DEF_PASS_LIST
1572 /* Build the tree of passes. */
1574 #define INSERT_PASSES_AFTER(PASS) \
1575 p = &(PASS);
1577 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1579 opt_pass **p = &(PASS ## _1)->sub;
1581 #define POP_INSERT_PASSES() \
1584 #define NEXT_PASS(PASS, NUM) \
1585 do { \
1586 gcc_assert (NULL == PASS ## _ ## NUM); \
1587 if ((NUM) == 1) \
1588 PASS ## _1 = make_##PASS (m_ctxt); \
1589 else \
1591 gcc_assert (PASS ## _1); \
1592 PASS ## _ ## NUM = PASS ## _1->clone (); \
1594 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1595 } while (0)
1597 #define TERMINATE_PASS_LIST() \
1598 *p = NULL;
1600 #include "pass-instances.def"
1602 #undef INSERT_PASSES_AFTER
1603 #undef PUSH_INSERT_PASSES_WITHIN
1604 #undef POP_INSERT_PASSES
1605 #undef NEXT_PASS
1606 #undef TERMINATE_PASS_LIST
1608 /* Register the passes with the tree dump code. */
1609 register_dump_files (all_lowering_passes);
1610 register_dump_files (all_small_ipa_passes);
1611 register_dump_files (all_regular_ipa_passes);
1612 register_dump_files (all_late_ipa_passes);
1613 register_dump_files (all_passes);
1616 static void
1617 delete_pass_tree (opt_pass *pass)
1619 while (pass)
1621 /* Recurse into child passes. */
1622 delete_pass_tree (pass->sub);
1624 opt_pass *next = pass->next;
1626 /* Delete this pass. */
1627 delete pass;
1629 /* Iterate onto sibling passes. */
1630 pass = next;
1634 pass_manager::~pass_manager ()
1636 XDELETEVEC (passes_by_id);
1638 /* Call delete_pass_tree on each of the pass_lists. */
1639 #define DEF_PASS_LIST(LIST) \
1640 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1641 GCC_PASS_LISTS
1642 #undef DEF_PASS_LIST
1646 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1647 function CALLBACK for every function in the call graph. Otherwise,
1648 call CALLBACK on the current function. */
1650 static void
1651 do_per_function (void (*callback) (function *, void *data), void *data)
1653 if (current_function_decl)
1654 callback (cfun, data);
1655 else
1657 struct cgraph_node *node;
1658 FOR_EACH_DEFINED_FUNCTION (node)
1659 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1660 && (!node->clone_of || node->decl != node->clone_of->decl))
1661 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1665 /* Because inlining might remove no-longer reachable nodes, we need to
1666 keep the array visible to garbage collector to avoid reading collected
1667 out nodes. */
1668 static int nnodes;
1669 static GTY ((length ("nnodes"))) cgraph_node **order;
1671 /* Hook called when NODE is removed and therefore should be
1672 excluded from order vector. DATA is an array of integers.
1673 DATA[0] holds max index it may be accessed by. For cgraph
1674 node DATA[node->uid + 1] holds index of this node in order
1675 vector. */
1676 static void
1677 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1679 int *order_idx = (int *)data;
1681 if (node->uid >= order_idx[0])
1682 return;
1684 int idx = order_idx[node->uid + 1];
1685 if (idx >= 0 && idx < nnodes && order[idx] == node)
1686 order[idx] = NULL;
1689 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1690 function CALLBACK for every function in the call graph. Otherwise,
1691 call CALLBACK on the current function.
1692 This function is global so that plugins can use it. */
1693 void
1694 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1696 int i;
1698 if (current_function_decl)
1699 callback (cfun, data);
1700 else
1702 cgraph_node_hook_list *hook;
1703 int *order_idx;
1704 gcc_assert (!order);
1705 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1707 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1708 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1709 order_idx[0] = symtab->cgraph_max_uid;
1711 nnodes = ipa_reverse_postorder (order);
1712 for (i = nnodes - 1; i >= 0; i--)
1714 order[i]->process = 1;
1715 order_idx[order[i]->uid + 1] = i;
1717 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1718 order_idx);
1719 for (i = nnodes - 1; i >= 0; i--)
1721 /* Function could be inlined and removed as unreachable. */
1722 if (!order[i])
1723 continue;
1725 struct cgraph_node *node = order[i];
1727 /* Allow possibly removed nodes to be garbage collected. */
1728 order[i] = NULL;
1729 node->process = 0;
1730 if (node->has_gimple_body_p ())
1731 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1733 symtab->remove_cgraph_removal_hook (hook);
1735 ggc_free (order);
1736 order = NULL;
1737 nnodes = 0;
1740 /* Helper function to perform function body dump. */
1742 static void
1743 execute_function_dump (function *fn, void *data)
1745 opt_pass *pass = (opt_pass *)data;
1747 if (dump_file)
1749 push_cfun (fn);
1751 if (fn->curr_properties & PROP_trees)
1752 dump_function_to_file (fn->decl, dump_file, dump_flags);
1753 else
1754 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1756 /* Flush the file. If verification fails, we won't be able to
1757 close the file before aborting. */
1758 fflush (dump_file);
1760 if ((fn->curr_properties & PROP_cfg)
1761 && (dump_flags & TDF_GRAPH))
1763 if (!pass->graph_dump_initialized)
1765 clean_graph_dump_file (dump_file_name);
1766 pass->graph_dump_initialized = true;
1768 print_graph_cfg (dump_file_name, fn);
1771 pop_cfun ();
1775 static struct profile_record *profile_record;
1777 /* Do profile consistency book-keeping for the pass with static number INDEX.
1778 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1779 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1780 if we are only book-keeping on passes that may have selectively disabled
1781 themselves on a given function. */
1782 static void
1783 check_profile_consistency (int index, int subpass, bool run)
1785 pass_manager *passes = g->get_passes ();
1786 if (index == -1)
1787 return;
1788 if (!profile_record)
1789 profile_record = XCNEWVEC (struct profile_record,
1790 passes->passes_by_id_size);
1791 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1792 gcc_assert (subpass < 2);
1793 profile_record[index].run |= run;
1794 account_profile_record (&profile_record[index], subpass);
1797 /* Output profile consistency. */
1799 void
1800 dump_profile_report (void)
1802 g->get_passes ()->dump_profile_report ();
1805 void
1806 pass_manager::dump_profile_report () const
1808 int i, j;
1809 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1810 gcov_type last_time = 0, last_size = 0;
1811 double rel_time_change, rel_size_change;
1812 int last_reported = 0;
1814 if (!profile_record)
1815 return;
1816 fprintf (stderr, "\nProfile consistency report:\n\n");
1817 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1818 fprintf (stderr, " |freq count |freq count |size time\n");
1820 for (i = 0; i < passes_by_id_size; i++)
1821 for (j = 0 ; j < 2; j++)
1822 if (profile_record[i].run)
1824 if (last_time)
1825 rel_time_change = (profile_record[i].time[j]
1826 - (double)last_time) * 100 / (double)last_time;
1827 else
1828 rel_time_change = 0;
1829 if (last_size)
1830 rel_size_change = (profile_record[i].size[j]
1831 - (double)last_size) * 100 / (double)last_size;
1832 else
1833 rel_size_change = 0;
1835 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1836 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1837 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1838 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1839 || rel_time_change || rel_size_change)
1841 last_reported = i;
1842 fprintf (stderr, "%-20s %s",
1843 passes_by_id [i]->name,
1844 j ? "(after TODO)" : " ");
1845 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1846 fprintf (stderr, "| %+5i",
1847 profile_record[i].num_mismatched_freq_in[j]
1848 - last_freq_in);
1849 else
1850 fprintf (stderr, "| ");
1851 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1852 fprintf (stderr, " %+5i",
1853 profile_record[i].num_mismatched_count_in[j]
1854 - last_count_in);
1855 else
1856 fprintf (stderr, " ");
1857 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1858 fprintf (stderr, "| %+5i",
1859 profile_record[i].num_mismatched_freq_out[j]
1860 - last_freq_out);
1861 else
1862 fprintf (stderr, "| ");
1863 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1864 fprintf (stderr, " %+5i",
1865 profile_record[i].num_mismatched_count_out[j]
1866 - last_count_out);
1867 else
1868 fprintf (stderr, " ");
1870 /* Size/time units change across gimple and RTL. */
1871 if (i == pass_expand_1->static_pass_number)
1872 fprintf (stderr, "|----------");
1873 else
1875 if (rel_size_change)
1876 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1877 else
1878 fprintf (stderr, "| ");
1879 if (rel_time_change)
1880 fprintf (stderr, " %+8.4f%%", rel_time_change);
1882 fprintf (stderr, "\n");
1883 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1884 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1885 last_count_in = profile_record[i].num_mismatched_count_in[j];
1886 last_count_out = profile_record[i].num_mismatched_count_out[j];
1888 else if (j && last_reported != i)
1890 last_reported = i;
1891 fprintf (stderr, "%-20s ------------| | |\n",
1892 passes_by_id [i]->name);
1894 last_time = profile_record[i].time[j];
1895 last_size = profile_record[i].size[j];
1899 /* Perform all TODO actions that ought to be done on each function. */
1901 static void
1902 execute_function_todo (function *fn, void *data)
1904 bool from_ipa_pass = (cfun == NULL);
1905 unsigned int flags = (size_t)data;
1906 flags &= ~fn->last_verified;
1907 if (!flags)
1908 return;
1910 push_cfun (fn);
1912 /* Always cleanup the CFG before trying to update SSA. */
1913 if (flags & TODO_cleanup_cfg)
1915 cleanup_tree_cfg ();
1917 /* Once unreachable nodes have been removed from the CFG,
1918 there can't be any lingering references to released
1919 SSA_NAMES (because there is no more unreachable code).
1921 Thus, now is the time to flush the SSA_NAMEs freelist. */
1922 if (fn->gimple_df)
1923 flush_ssaname_freelist ();
1925 /* When cleanup_tree_cfg merges consecutive blocks, it may
1926 perform some simplistic propagation when removing single
1927 valued PHI nodes. This propagation may, in turn, cause the
1928 SSA form to become out-of-date (see PR 22037). So, even
1929 if the parent pass had not scheduled an SSA update, we may
1930 still need to do one. */
1931 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1932 flags |= TODO_update_ssa;
1935 if (flags & TODO_update_ssa_any)
1937 unsigned update_flags = flags & TODO_update_ssa_any;
1938 update_ssa (update_flags);
1941 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1942 compute_may_aliases ();
1944 if (optimize && (flags & TODO_update_address_taken))
1945 execute_update_addresses_taken ();
1947 if (flags & TODO_remove_unused_locals)
1948 remove_unused_locals ();
1950 if (flags & TODO_rebuild_frequencies)
1951 rebuild_frequencies ();
1953 if (flags & TODO_rebuild_cgraph_edges)
1954 cgraph_edge::rebuild_edges ();
1956 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1957 /* If we've seen errors do not bother running any verifiers. */
1958 if (!seen_error ())
1960 #if defined ENABLE_CHECKING
1961 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1962 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1964 if (flags & TODO_verify_il)
1966 if (cfun->curr_properties & PROP_trees)
1968 if (cfun->curr_properties & PROP_cfg)
1969 /* IPA passes leave stmts to be fixed up, so make sure to
1970 not verify stmts really throw. */
1971 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1972 else
1973 verify_gimple_in_seq (gimple_body (cfun->decl));
1975 if (cfun->curr_properties & PROP_ssa)
1976 /* IPA passes leave stmts to be fixed up, so make sure to
1977 not verify SSA operands whose verifier will choke on that. */
1978 verify_ssa (true, !from_ipa_pass);
1979 /* IPA passes leave basic-blocks unsplit, so make sure to
1980 not trip on that. */
1981 if ((cfun->curr_properties & PROP_cfg)
1982 && !from_ipa_pass)
1983 verify_flow_info ();
1984 if (current_loops
1985 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1986 verify_loop_closed_ssa (false);
1987 if (cfun->curr_properties & PROP_rtl)
1988 verify_rtl_sharing ();
1991 /* Make sure verifiers don't change dominator state. */
1992 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1993 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1994 #endif
1997 fn->last_verified = flags & TODO_verify_all;
1999 pop_cfun ();
2001 /* For IPA passes make sure to release dominator info, it can be
2002 computed by non-verifying TODOs. */
2003 if (from_ipa_pass)
2005 free_dominance_info (fn, CDI_DOMINATORS);
2006 free_dominance_info (fn, CDI_POST_DOMINATORS);
2010 /* Perform all TODO actions. */
2011 static void
2012 execute_todo (unsigned int flags)
2014 #if defined ENABLE_CHECKING
2015 if (cfun
2016 && need_ssa_update_p (cfun))
2017 gcc_assert (flags & TODO_update_ssa_any);
2018 #endif
2020 timevar_push (TV_TODO);
2022 /* Inform the pass whether it is the first time it is run. */
2023 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2025 statistics_fini_pass ();
2027 if (flags)
2028 do_per_function (execute_function_todo, (void *)(size_t) flags);
2030 /* Always remove functions just as before inlining: IPA passes might be
2031 interested to see bodies of extern inline functions that are not inlined
2032 to analyze side effects. The full removal is done just at the end
2033 of IPA pass queue. */
2034 if (flags & TODO_remove_functions)
2036 gcc_assert (!cfun);
2037 symtab->remove_unreachable_nodes (dump_file);
2040 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2042 gcc_assert (!cfun);
2043 symtab_node::dump_table (dump_file);
2044 /* Flush the file. If verification fails, we won't be able to
2045 close the file before aborting. */
2046 fflush (dump_file);
2049 /* Now that the dumping has been done, we can get rid of the optional
2050 df problems. */
2051 if (flags & TODO_df_finish)
2052 df_finish_pass ((flags & TODO_df_verify) != 0);
2054 timevar_pop (TV_TODO);
2057 /* Verify invariants that should hold between passes. This is a place
2058 to put simple sanity checks. */
2060 static void
2061 verify_interpass_invariants (void)
2063 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2066 /* Clear the last verified flag. */
2068 static void
2069 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2071 fn->last_verified = 0;
2074 /* Helper function. Verify that the properties has been turn into the
2075 properties expected by the pass. */
2077 #ifdef ENABLE_CHECKING
2078 static void
2079 verify_curr_properties (function *fn, void *data)
2081 unsigned int props = (size_t)data;
2082 gcc_assert ((fn->curr_properties & props) == props);
2084 #endif
2086 /* Initialize pass dump file. */
2087 /* This is non-static so that the plugins can use it. */
2089 bool
2090 pass_init_dump_file (opt_pass *pass)
2092 /* If a dump file name is present, open it if enabled. */
2093 if (pass->static_pass_number != -1)
2095 timevar_push (TV_DUMP);
2096 gcc::dump_manager *dumps = g->get_dumps ();
2097 bool initializing_dump =
2098 !dumps->dump_initialized_p (pass->static_pass_number);
2099 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2100 dumps->dump_start (pass->static_pass_number, &dump_flags);
2101 if (dump_file && current_function_decl)
2102 dump_function_header (dump_file, current_function_decl, dump_flags);
2103 if (initializing_dump
2104 && dump_file && (dump_flags & TDF_GRAPH)
2105 && cfun && (cfun->curr_properties & PROP_cfg))
2107 clean_graph_dump_file (dump_file_name);
2108 pass->graph_dump_initialized = true;
2110 timevar_pop (TV_DUMP);
2111 return initializing_dump;
2113 else
2114 return false;
2117 /* Flush PASS dump file. */
2118 /* This is non-static so that plugins can use it. */
2120 void
2121 pass_fini_dump_file (opt_pass *pass)
2123 timevar_push (TV_DUMP);
2125 /* Flush and close dump file. */
2126 if (dump_file_name)
2128 free (CONST_CAST (char *, dump_file_name));
2129 dump_file_name = NULL;
2132 g->get_dumps ()->dump_finish (pass->static_pass_number);
2133 timevar_pop (TV_DUMP);
2136 /* After executing the pass, apply expected changes to the function
2137 properties. */
2139 static void
2140 update_properties_after_pass (function *fn, void *data)
2142 opt_pass *pass = (opt_pass *) data;
2143 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2144 & ~pass->properties_destroyed;
2147 /* Execute summary generation for all of the passes in IPA_PASS. */
2149 void
2150 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2152 while (ipa_pass)
2154 opt_pass *pass = ipa_pass;
2156 /* Execute all of the IPA_PASSes in the list. */
2157 if (ipa_pass->type == IPA_PASS
2158 && pass->gate (cfun)
2159 && ipa_pass->generate_summary)
2161 pass_init_dump_file (pass);
2163 /* If a timevar is present, start it. */
2164 if (pass->tv_id)
2165 timevar_push (pass->tv_id);
2167 current_pass = pass;
2168 ipa_pass->generate_summary ();
2170 /* Stop timevar. */
2171 if (pass->tv_id)
2172 timevar_pop (pass->tv_id);
2174 pass_fini_dump_file (pass);
2176 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2180 /* Execute IPA_PASS function transform on NODE. */
2182 static void
2183 execute_one_ipa_transform_pass (struct cgraph_node *node,
2184 ipa_opt_pass_d *ipa_pass)
2186 opt_pass *pass = ipa_pass;
2187 unsigned int todo_after = 0;
2189 current_pass = pass;
2190 if (!ipa_pass->function_transform)
2191 return;
2193 /* Note that the folders should only create gimple expressions.
2194 This is a hack until the new folder is ready. */
2195 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2197 pass_init_dump_file (pass);
2199 /* Run pre-pass verification. */
2200 execute_todo (ipa_pass->function_transform_todo_flags_start);
2202 /* If a timevar is present, start it. */
2203 if (pass->tv_id != TV_NONE)
2204 timevar_push (pass->tv_id);
2206 /* Do it! */
2207 todo_after = ipa_pass->function_transform (node);
2209 /* Stop timevar. */
2210 if (pass->tv_id != TV_NONE)
2211 timevar_pop (pass->tv_id);
2213 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2214 check_profile_consistency (pass->static_pass_number, 0, true);
2216 /* Run post-pass cleanup and verification. */
2217 execute_todo (todo_after);
2218 verify_interpass_invariants ();
2219 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2220 check_profile_consistency (pass->static_pass_number, 1, true);
2222 if (dump_file)
2223 do_per_function (execute_function_dump, NULL);
2224 pass_fini_dump_file (pass);
2226 current_pass = NULL;
2228 /* Signal this is a suitable GC collection point. */
2229 if (!(todo_after & TODO_do_not_ggc_collect))
2230 ggc_collect ();
2233 /* For the current function, execute all ipa transforms. */
2235 void
2236 execute_all_ipa_transforms (void)
2238 struct cgraph_node *node;
2239 if (!cfun)
2240 return;
2241 node = cgraph_node::get (current_function_decl);
2243 if (node->ipa_transforms_to_apply.exists ())
2245 unsigned int i;
2247 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2248 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2249 node->ipa_transforms_to_apply.release ();
2253 /* Check if PASS is explicitly disabled or enabled and return
2254 the gate status. FUNC is the function to be processed, and
2255 GATE_STATUS is the gate status determined by pass manager by
2256 default. */
2258 static bool
2259 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2261 bool explicitly_enabled = false;
2262 bool explicitly_disabled = false;
2264 explicitly_enabled
2265 = is_pass_explicitly_enabled_or_disabled (pass, func,
2266 enabled_pass_uid_range_tab);
2267 explicitly_disabled
2268 = is_pass_explicitly_enabled_or_disabled (pass, func,
2269 disabled_pass_uid_range_tab);
2271 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2273 return gate_status;
2277 /* Execute PASS. */
2279 bool
2280 execute_one_pass (opt_pass *pass)
2282 unsigned int todo_after = 0;
2284 bool gate_status;
2286 /* IPA passes are executed on whole program, so cfun should be NULL.
2287 Other passes need function context set. */
2288 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2289 gcc_assert (!cfun && !current_function_decl);
2290 else
2291 gcc_assert (cfun && current_function_decl);
2293 current_pass = pass;
2295 /* Check whether gate check should be avoided.
2296 User controls the value of the gate through the parameter "gate_status". */
2297 gate_status = pass->gate (cfun);
2298 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2300 /* Override gate with plugin. */
2301 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2303 if (!gate_status)
2305 /* Run so passes selectively disabling themselves on a given function
2306 are not miscounted. */
2307 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2309 check_profile_consistency (pass->static_pass_number, 0, false);
2310 check_profile_consistency (pass->static_pass_number, 1, false);
2312 current_pass = NULL;
2313 return false;
2316 /* Pass execution event trigger: useful to identify passes being
2317 executed. */
2318 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2320 if (!quiet_flag && !cfun)
2321 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2323 /* Note that the folders should only create gimple expressions.
2324 This is a hack until the new folder is ready. */
2325 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2327 pass_init_dump_file (pass);
2329 /* Run pre-pass verification. */
2330 execute_todo (pass->todo_flags_start);
2332 #ifdef ENABLE_CHECKING
2333 do_per_function (verify_curr_properties,
2334 (void *)(size_t)pass->properties_required);
2335 #endif
2337 /* If a timevar is present, start it. */
2338 if (pass->tv_id != TV_NONE)
2339 timevar_push (pass->tv_id);
2341 /* Do it! */
2342 todo_after = pass->execute (cfun);
2343 do_per_function (clear_last_verified, NULL);
2345 /* Stop timevar. */
2346 if (pass->tv_id != TV_NONE)
2347 timevar_pop (pass->tv_id);
2349 do_per_function (update_properties_after_pass, pass);
2351 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2352 check_profile_consistency (pass->static_pass_number, 0, true);
2354 /* Run post-pass cleanup and verification. */
2355 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2356 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2357 check_profile_consistency (pass->static_pass_number, 1, true);
2359 verify_interpass_invariants ();
2360 if (dump_file)
2361 do_per_function (execute_function_dump, pass);
2362 if (pass->type == IPA_PASS)
2364 struct cgraph_node *node;
2365 if (((ipa_opt_pass_d *)pass)->function_transform)
2366 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2367 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2370 if (!current_function_decl)
2371 symtab->process_new_functions ();
2373 pass_fini_dump_file (pass);
2375 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2376 gcc_assert (!(cfun->curr_properties & PROP_trees)
2377 || pass->type != RTL_PASS);
2379 current_pass = NULL;
2381 /* Signal this is a suitable GC collection point. */
2382 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2383 ggc_collect ();
2385 return true;
2388 static void
2389 execute_pass_list_1 (opt_pass *pass)
2393 gcc_assert (pass->type == GIMPLE_PASS
2394 || pass->type == RTL_PASS);
2395 if (execute_one_pass (pass) && pass->sub)
2396 execute_pass_list_1 (pass->sub);
2397 pass = pass->next;
2399 while (pass);
2402 void
2403 execute_pass_list (function *fn, opt_pass *pass)
2405 push_cfun (fn);
2406 execute_pass_list_1 (pass);
2407 if (fn->cfg)
2409 free_dominance_info (CDI_DOMINATORS);
2410 free_dominance_info (CDI_POST_DOMINATORS);
2412 pop_cfun ();
2415 /* Write out all LTO data. */
2416 static void
2417 write_lto (void)
2419 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2420 lto_output ();
2421 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2422 timevar_push (TV_IPA_LTO_DECL_OUT);
2423 produce_asm_for_decls ();
2424 timevar_pop (TV_IPA_LTO_DECL_OUT);
2427 /* Same as execute_pass_list but assume that subpasses of IPA passes
2428 are local passes. If SET is not NULL, write out summaries of only
2429 those node in SET. */
2431 static void
2432 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2434 while (pass)
2436 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2437 gcc_assert (!current_function_decl);
2438 gcc_assert (!cfun);
2439 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2440 if (pass->type == IPA_PASS
2441 && ipa_pass->write_summary
2442 && pass->gate (cfun))
2444 /* If a timevar is present, start it. */
2445 if (pass->tv_id)
2446 timevar_push (pass->tv_id);
2448 pass_init_dump_file (pass);
2450 current_pass = pass;
2451 ipa_pass->write_summary ();
2453 pass_fini_dump_file (pass);
2455 /* If a timevar is present, start it. */
2456 if (pass->tv_id)
2457 timevar_pop (pass->tv_id);
2460 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2461 ipa_write_summaries_2 (pass->sub, state);
2463 pass = pass->next;
2467 /* Helper function of ipa_write_summaries. Creates and destroys the
2468 decl state and calls ipa_write_summaries_2 for all passes that have
2469 summaries. SET is the set of nodes to be written. */
2471 static void
2472 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2474 pass_manager *passes = g->get_passes ();
2475 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2476 state->symtab_node_encoder = encoder;
2478 lto_output_init_mode_table ();
2479 lto_push_out_decl_state (state);
2481 gcc_assert (!flag_wpa);
2482 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2484 write_lto ();
2486 gcc_assert (lto_get_out_decl_state () == state);
2487 lto_pop_out_decl_state ();
2488 lto_delete_out_decl_state (state);
2491 /* Write out summaries for all the nodes in the callgraph. */
2493 void
2494 ipa_write_summaries (void)
2496 lto_symtab_encoder_t encoder;
2497 int i, order_pos;
2498 varpool_node *vnode;
2499 struct cgraph_node *node;
2500 struct cgraph_node **order;
2502 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2503 return;
2505 select_what_to_stream ();
2507 encoder = lto_symtab_encoder_new (false);
2509 /* Create the callgraph set in the same order used in
2510 cgraph_expand_all_functions. This mostly facilitates debugging,
2511 since it causes the gimple file to be processed in the same order
2512 as the source code. */
2513 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2514 order_pos = ipa_reverse_postorder (order);
2515 gcc_assert (order_pos == symtab->cgraph_count);
2517 for (i = order_pos - 1; i >= 0; i--)
2519 struct cgraph_node *node = order[i];
2521 if (node->has_gimple_body_p ())
2523 /* When streaming out references to statements as part of some IPA
2524 pass summary, the statements need to have uids assigned and the
2525 following does that for all the IPA passes here. Naturally, this
2526 ordering then matches the one IPA-passes get in their stmt_fixup
2527 hooks. */
2529 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2530 renumber_gimple_stmt_uids ();
2531 pop_cfun ();
2533 if (node->definition && node->need_lto_streaming)
2534 lto_set_symtab_encoder_in_partition (encoder, node);
2537 FOR_EACH_DEFINED_FUNCTION (node)
2538 if (node->alias && node->need_lto_streaming)
2539 lto_set_symtab_encoder_in_partition (encoder, node);
2540 FOR_EACH_DEFINED_VARIABLE (vnode)
2541 if (vnode->need_lto_streaming)
2542 lto_set_symtab_encoder_in_partition (encoder, vnode);
2544 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2546 free (order);
2549 /* Same as execute_pass_list but assume that subpasses of IPA passes
2550 are local passes. If SET is not NULL, write out optimization summaries of
2551 only those node in SET. */
2553 static void
2554 ipa_write_optimization_summaries_1 (opt_pass *pass,
2555 struct lto_out_decl_state *state)
2557 while (pass)
2559 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2560 gcc_assert (!current_function_decl);
2561 gcc_assert (!cfun);
2562 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2563 if (pass->type == IPA_PASS
2564 && ipa_pass->write_optimization_summary
2565 && pass->gate (cfun))
2567 /* If a timevar is present, start it. */
2568 if (pass->tv_id)
2569 timevar_push (pass->tv_id);
2571 pass_init_dump_file (pass);
2573 current_pass = pass;
2574 ipa_pass->write_optimization_summary ();
2576 pass_fini_dump_file (pass);
2578 /* If a timevar is present, start it. */
2579 if (pass->tv_id)
2580 timevar_pop (pass->tv_id);
2583 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2584 ipa_write_optimization_summaries_1 (pass->sub, state);
2586 pass = pass->next;
2590 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2591 NULL, write out all summaries of all nodes. */
2593 void
2594 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2596 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2597 lto_symtab_encoder_iterator lsei;
2598 state->symtab_node_encoder = encoder;
2600 lto_output_init_mode_table ();
2601 lto_push_out_decl_state (state);
2602 for (lsei = lsei_start_function_in_partition (encoder);
2603 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2605 struct cgraph_node *node = lsei_cgraph_node (lsei);
2606 /* When streaming out references to statements as part of some IPA
2607 pass summary, the statements need to have uids assigned.
2609 For functions newly born at WPA stage we need to initialize
2610 the uids here. */
2611 if (node->definition
2612 && gimple_has_body_p (node->decl))
2614 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2615 renumber_gimple_stmt_uids ();
2616 pop_cfun ();
2620 gcc_assert (flag_wpa);
2621 pass_manager *passes = g->get_passes ();
2622 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2624 write_lto ();
2626 gcc_assert (lto_get_out_decl_state () == state);
2627 lto_pop_out_decl_state ();
2628 lto_delete_out_decl_state (state);
2631 /* Same as execute_pass_list but assume that subpasses of IPA passes
2632 are local passes. */
2634 static void
2635 ipa_read_summaries_1 (opt_pass *pass)
2637 while (pass)
2639 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2641 gcc_assert (!current_function_decl);
2642 gcc_assert (!cfun);
2643 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2645 if (pass->gate (cfun))
2647 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2649 /* If a timevar is present, start it. */
2650 if (pass->tv_id)
2651 timevar_push (pass->tv_id);
2653 pass_init_dump_file (pass);
2655 current_pass = pass;
2656 ipa_pass->read_summary ();
2658 pass_fini_dump_file (pass);
2660 /* Stop timevar. */
2661 if (pass->tv_id)
2662 timevar_pop (pass->tv_id);
2665 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2666 ipa_read_summaries_1 (pass->sub);
2668 pass = pass->next;
2673 /* Read all the summaries for all_regular_ipa_passes. */
2675 void
2676 ipa_read_summaries (void)
2678 pass_manager *passes = g->get_passes ();
2679 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2682 /* Same as execute_pass_list but assume that subpasses of IPA passes
2683 are local passes. */
2685 static void
2686 ipa_read_optimization_summaries_1 (opt_pass *pass)
2688 while (pass)
2690 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2692 gcc_assert (!current_function_decl);
2693 gcc_assert (!cfun);
2694 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2696 if (pass->gate (cfun))
2698 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2700 /* If a timevar is present, start it. */
2701 if (pass->tv_id)
2702 timevar_push (pass->tv_id);
2704 pass_init_dump_file (pass);
2706 current_pass = pass;
2707 ipa_pass->read_optimization_summary ();
2709 pass_fini_dump_file (pass);
2711 /* Stop timevar. */
2712 if (pass->tv_id)
2713 timevar_pop (pass->tv_id);
2716 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2717 ipa_read_optimization_summaries_1 (pass->sub);
2719 pass = pass->next;
2723 /* Read all the summaries for all_regular_ipa_passes. */
2725 void
2726 ipa_read_optimization_summaries (void)
2728 pass_manager *passes = g->get_passes ();
2729 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2732 /* Same as execute_pass_list but assume that subpasses of IPA passes
2733 are local passes. */
2734 void
2735 execute_ipa_pass_list (opt_pass *pass)
2739 gcc_assert (!current_function_decl);
2740 gcc_assert (!cfun);
2741 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2742 if (execute_one_pass (pass) && pass->sub)
2744 if (pass->sub->type == GIMPLE_PASS)
2746 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2747 do_per_function_toporder ((void (*)(function *, void *))
2748 execute_pass_list,
2749 pass->sub);
2750 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2752 else if (pass->sub->type == SIMPLE_IPA_PASS
2753 || pass->sub->type == IPA_PASS)
2754 execute_ipa_pass_list (pass->sub);
2755 else
2756 gcc_unreachable ();
2758 gcc_assert (!current_function_decl);
2759 symtab->process_new_functions ();
2760 pass = pass->next;
2762 while (pass);
2765 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2767 static void
2768 execute_ipa_stmt_fixups (opt_pass *pass,
2769 struct cgraph_node *node, gimple **stmts)
2771 while (pass)
2773 /* Execute all of the IPA_PASSes in the list. */
2774 if (pass->type == IPA_PASS
2775 && pass->gate (cfun))
2777 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2779 if (ipa_pass->stmt_fixup)
2781 pass_init_dump_file (pass);
2782 /* If a timevar is present, start it. */
2783 if (pass->tv_id)
2784 timevar_push (pass->tv_id);
2786 current_pass = pass;
2787 ipa_pass->stmt_fixup (node, stmts);
2789 /* Stop timevar. */
2790 if (pass->tv_id)
2791 timevar_pop (pass->tv_id);
2792 pass_fini_dump_file (pass);
2794 if (pass->sub)
2795 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2797 pass = pass->next;
2801 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2803 void
2804 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2806 pass_manager *passes = g->get_passes ();
2807 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2811 extern void debug_properties (unsigned int);
2812 extern void dump_properties (FILE *, unsigned int);
2814 DEBUG_FUNCTION void
2815 dump_properties (FILE *dump, unsigned int props)
2817 fprintf (dump, "Properties:\n");
2818 if (props & PROP_gimple_any)
2819 fprintf (dump, "PROP_gimple_any\n");
2820 if (props & PROP_gimple_lcf)
2821 fprintf (dump, "PROP_gimple_lcf\n");
2822 if (props & PROP_gimple_leh)
2823 fprintf (dump, "PROP_gimple_leh\n");
2824 if (props & PROP_cfg)
2825 fprintf (dump, "PROP_cfg\n");
2826 if (props & PROP_ssa)
2827 fprintf (dump, "PROP_ssa\n");
2828 if (props & PROP_no_crit_edges)
2829 fprintf (dump, "PROP_no_crit_edges\n");
2830 if (props & PROP_rtl)
2831 fprintf (dump, "PROP_rtl\n");
2832 if (props & PROP_gimple_lomp)
2833 fprintf (dump, "PROP_gimple_lomp\n");
2834 if (props & PROP_gimple_lcx)
2835 fprintf (dump, "PROP_gimple_lcx\n");
2836 if (props & PROP_gimple_lvec)
2837 fprintf (dump, "PROP_gimple_lvec\n");
2838 if (props & PROP_cfglayout)
2839 fprintf (dump, "PROP_cfglayout\n");
2842 DEBUG_FUNCTION void
2843 debug_properties (unsigned int props)
2845 dump_properties (stderr, props);
2848 /* Called by local passes to see if function is called by already processed nodes.
2849 Because we process nodes in topological order, this means that function is
2850 in recursive cycle or we introduced new direct calls. */
2851 bool
2852 function_called_by_processed_nodes_p (void)
2854 struct cgraph_edge *e;
2855 for (e = cgraph_node::get (current_function_decl)->callers;
2857 e = e->next_caller)
2859 if (e->caller->decl == current_function_decl)
2860 continue;
2861 if (!e->caller->has_gimple_body_p ())
2862 continue;
2863 if (TREE_ASM_WRITTEN (e->caller->decl))
2864 continue;
2865 if (!e->caller->process && !e->caller->global.inlined_to)
2866 break;
2868 if (dump_file && e)
2870 fprintf (dump_file, "Already processed call to:\n");
2871 e->caller->dump (dump_file);
2873 return e != NULL;
2876 #include "gt-passes.h"