* builtins.def (BUILT_IN_SETJMP): Revert latest change.
[official-gcc.git] / gcc / passes.c
blob2c9add84c1d883de7de90872c4ef862f84320180
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2017 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 "target.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "gimple.h"
33 #include "cfghooks.h"
34 #include "df.h"
35 #include "memmodel.h"
36 #include "tm_p.h"
37 #include "ssa.h"
38 #include "emit-rtl.h"
39 #include "cgraph.h"
40 #include "lto-streamer.h"
41 #include "fold-const.h"
42 #include "varasm.h"
43 #include "output.h"
44 #include "graph.h"
45 #include "debug.h"
46 #include "cfgloop.h"
47 #include "value-prof.h"
48 #include "tree-cfg.h"
49 #include "tree-ssa-loop-manip.h"
50 #include "tree-into-ssa.h"
51 #include "tree-dfa.h"
52 #include "tree-ssa.h"
53 #include "tree-pass.h"
54 #include "plugin.h"
55 #include "ipa-utils.h"
56 #include "tree-pretty-print.h" /* for dump_function_header */
57 #include "context.h"
58 #include "pass_manager.h"
59 #include "cfgrtl.h"
60 #include "tree-ssa-live.h" /* For remove_unused_locals. */
61 #include "tree-cfgcleanup.h"
62 #include "insn-addr.h" /* for INSN_ADDRESSES_ALLOC. */
63 #include "diagnostic-core.h" /* for fnotice */
64 #include "stringpool.h"
65 #include "attribs.h"
67 using namespace gcc;
69 /* This is used for debugging. It allows the current pass to printed
70 from anywhere in compilation.
71 The variable current_pass is also used for statistics and plugins. */
72 opt_pass *current_pass;
74 /* Most passes are single-instance (within their context) and thus don't
75 need to implement cloning, but passes that support multiple instances
76 *must* provide their own implementation of the clone method.
78 Handle this by providing a default implemenation, but make it a fatal
79 error to call it. */
81 opt_pass *
82 opt_pass::clone ()
84 internal_error ("pass %s does not support cloning", name);
87 void
88 opt_pass::set_pass_param (unsigned int, bool)
90 internal_error ("pass %s needs a set_pass_param implementation to handle the"
91 " extra argument in NEXT_PASS", name);
94 bool
95 opt_pass::gate (function *)
97 return true;
100 unsigned int
101 opt_pass::execute (function *)
103 return 0;
106 opt_pass::opt_pass (const pass_data &data, context *ctxt)
107 : pass_data (data),
108 sub (NULL),
109 next (NULL),
110 static_pass_number (0),
111 m_ctxt (ctxt)
116 void
117 pass_manager::execute_early_local_passes ()
119 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
120 if (flag_check_pointer_bounds)
121 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
122 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
125 unsigned int
126 pass_manager::execute_pass_mode_switching ()
128 return pass_mode_switching_1->execute (cfun);
132 /* Call from anywhere to find out what pass this is. Useful for
133 printing out debugging information deep inside an service
134 routine. */
135 void
136 print_current_pass (FILE *file)
138 if (current_pass)
139 fprintf (file, "current pass = %s (%d)\n",
140 current_pass->name, current_pass->static_pass_number);
141 else
142 fprintf (file, "no current pass.\n");
146 /* Call from the debugger to get the current pass name. */
147 DEBUG_FUNCTION void
148 debug_pass (void)
150 print_current_pass (stderr);
155 /* Global variables used to communicate with passes. */
156 bool in_gimple_form;
159 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
160 and TYPE_DECL nodes.
162 This does nothing for local (non-static) variables, unless the
163 variable is a register variable with DECL_ASSEMBLER_NAME set. In
164 that case, or if the variable is not an automatic, it sets up the
165 RTL and outputs any assembler code (label definition, storage
166 allocation and initialization).
168 DECL is the declaration. TOP_LEVEL is nonzero
169 if this declaration is not within a function. */
171 void
172 rest_of_decl_compilation (tree decl,
173 int top_level,
174 int at_end)
176 bool finalize = true;
178 /* We deferred calling assemble_alias so that we could collect
179 other attributes such as visibility. Emit the alias now. */
180 if (!in_lto_p)
182 tree alias;
183 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
184 if (alias)
186 alias = TREE_VALUE (TREE_VALUE (alias));
187 alias = get_identifier (TREE_STRING_POINTER (alias));
188 /* A quirk of the initial implementation of aliases required that the
189 user add "extern" to all of them. Which is silly, but now
190 historical. Do note that the symbol is in fact locally defined. */
191 DECL_EXTERNAL (decl) = 0;
192 TREE_STATIC (decl) = 1;
193 assemble_alias (decl, alias);
194 finalize = false;
198 /* Can't defer this, because it needs to happen before any
199 later function definitions are processed. */
200 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
201 make_decl_rtl (decl);
203 /* Forward declarations for nested functions are not "external",
204 but we need to treat them as if they were. */
205 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
206 || TREE_CODE (decl) == FUNCTION_DECL)
208 timevar_push (TV_VARCONST);
210 /* Don't output anything when a tentative file-scope definition
211 is seen. But at end of compilation, do output code for them.
213 We do output all variables and rely on
214 callgraph code to defer them except for forward declarations
215 (see gcc.c-torture/compile/920624-1.c) */
216 if ((at_end
217 || !DECL_DEFER_OUTPUT (decl)
218 || DECL_INITIAL (decl))
219 && (!VAR_P (decl) || !DECL_HAS_VALUE_EXPR_P (decl))
220 && !DECL_EXTERNAL (decl))
222 /* When reading LTO unit, we also read varpool, so do not
223 rebuild it. */
224 if (in_lto_p && !at_end)
226 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
227 varpool_node::finalize_decl (decl);
230 #ifdef ASM_FINISH_DECLARE_OBJECT
231 if (decl == last_assemble_variable_decl)
233 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
234 top_level, at_end);
236 #endif
238 /* Now that we have activated any function-specific attributes
239 that might affect function decl, particularly align, relayout it. */
240 if (TREE_CODE (decl) == FUNCTION_DECL)
241 targetm.target_option.relayout_function (decl);
243 timevar_pop (TV_VARCONST);
245 else if (TREE_CODE (decl) == TYPE_DECL
246 /* Like in rest_of_type_compilation, avoid confusing the debug
247 information machinery when there are errors. */
248 && !seen_error ())
250 timevar_push (TV_SYMOUT);
251 debug_hooks->type_decl (decl, !top_level);
252 timevar_pop (TV_SYMOUT);
255 /* Let cgraph know about the existence of variables. */
256 if (in_lto_p && !at_end)
258 else if (VAR_P (decl) && !DECL_EXTERNAL (decl)
259 && TREE_STATIC (decl))
260 varpool_node::get_create (decl);
262 /* Generate early debug for global variables. Any local variables will
263 be handled by either handling reachable functions from
264 finalize_compilation_unit (and by consequence, locally scoped
265 symbols), or by rest_of_type_compilation below.
267 For Go's hijack of the debug_hooks to implement -fdump-go-spec, pick up
268 function prototypes. Go's debug_hooks will not forward them to the
269 wrapped hooks. */
270 if (!in_lto_p
271 && (TREE_CODE (decl) != FUNCTION_DECL
272 /* This will pick up function prototypes with no bodies,
273 which are not visible in finalize_compilation_unit()
274 while iterating with FOR_EACH_*_FUNCTION through the
275 symbol table. */
276 || (flag_dump_go_spec != NULL
277 && !DECL_SAVED_TREE (decl)
278 && DECL_STRUCT_FUNCTION (decl) == NULL))
280 /* We need to check both decl_function_context and
281 current_function_decl here to make sure local extern
282 declarations end up with the correct context.
284 For local extern declarations, decl_function_context is
285 empty, but current_function_decl is set to the function where
286 the extern was declared . Without the check for
287 !current_function_decl below, the local extern ends up
288 incorrectly with a top-level context.
290 For example:
292 namespace S
298 int i = 42;
300 extern int i; // Local extern declaration.
301 return i;
307 && !decl_function_context (decl)
308 && !current_function_decl
309 && DECL_SOURCE_LOCATION (decl) != BUILTINS_LOCATION
310 && (!decl_type_context (decl)
311 /* If we created a varpool node for the decl make sure to
312 call early_global_decl. Otherwise we miss changes
313 introduced by member definitions like
314 struct A { static int staticdatamember; };
315 int A::staticdatamember;
316 and thus have incomplete early debug and late debug
317 called from varpool node removal fails to handle it
318 properly. */
319 || (finalize
320 && VAR_P (decl)
321 && TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
322 /* Avoid confusing the debug information machinery when there are
323 errors. */
324 && !seen_error ())
325 (*debug_hooks->early_global_decl) (decl);
328 /* Called after finishing a record, union or enumeral type. */
330 void
331 rest_of_type_compilation (tree type, int toplev)
333 /* Avoid confusing the debug information machinery when there are
334 errors. */
335 if (seen_error ())
336 return;
338 timevar_push (TV_SYMOUT);
339 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
340 timevar_pop (TV_SYMOUT);
345 void
346 pass_manager::
347 finish_optimization_passes (void)
349 int i;
350 struct dump_file_info *dfi;
351 char *name;
352 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
354 timevar_push (TV_DUMP);
355 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
357 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
358 end_branch_prob ();
359 dumps->dump_finish (pass_profile_1->static_pass_number);
362 if (optimize > 0)
364 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
365 print_combine_total_stats ();
366 dumps->dump_finish (pass_profile_1->static_pass_number);
369 /* Do whatever is necessary to finish printing the graphs. */
370 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
371 if (dfi->graph_dump_initialized)
373 name = dumps->get_dump_file_name (dfi);
374 finish_graph_dump_file (name);
375 free (name);
378 timevar_pop (TV_DUMP);
381 static unsigned int
382 execute_build_ssa_passes (void)
384 /* Once this pass (and its sub-passes) are complete, all functions
385 will be in SSA form. Technically this state change is happening
386 a tad early, since the sub-passes have not yet run, but since
387 none of the sub-passes are IPA passes and do not create new
388 functions, this is ok. We're setting this value for the benefit
389 of IPA passes that follow. */
390 if (symtab->state < IPA_SSA)
391 symtab->state = IPA_SSA;
392 return 0;
395 namespace {
397 const pass_data pass_data_build_ssa_passes =
399 SIMPLE_IPA_PASS, /* type */
400 "build_ssa_passes", /* name */
401 OPTGROUP_NONE, /* optinfo_flags */
402 TV_EARLY_LOCAL, /* tv_id */
403 0, /* properties_required */
404 0, /* properties_provided */
405 0, /* properties_destroyed */
406 0, /* todo_flags_start */
407 /* todo_flags_finish is executed before subpases. For this reason
408 it makes no sense to remove unreachable functions here. */
409 0, /* todo_flags_finish */
412 class pass_build_ssa_passes : public simple_ipa_opt_pass
414 public:
415 pass_build_ssa_passes (gcc::context *ctxt)
416 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
419 /* opt_pass methods: */
420 virtual bool gate (function *)
422 /* Don't bother doing anything if the program has errors. */
423 return (!seen_error () && !in_lto_p);
426 virtual unsigned int execute (function *)
428 return execute_build_ssa_passes ();
431 }; // class pass_build_ssa_passes
433 const pass_data pass_data_chkp_instrumentation_passes =
435 SIMPLE_IPA_PASS, /* type */
436 "chkp_passes", /* name */
437 OPTGROUP_NONE, /* optinfo_flags */
438 TV_NONE, /* tv_id */
439 0, /* properties_required */
440 0, /* properties_provided */
441 0, /* properties_destroyed */
442 0, /* todo_flags_start */
443 0, /* todo_flags_finish */
446 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
448 public:
449 pass_chkp_instrumentation_passes (gcc::context *ctxt)
450 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
453 /* opt_pass methods: */
454 virtual bool gate (function *)
456 /* Don't bother doing anything if the program has errors. */
457 return (flag_check_pointer_bounds
458 && !seen_error () && !in_lto_p);
461 }; // class pass_chkp_instrumentation_passes
463 const pass_data pass_data_local_optimization_passes =
465 SIMPLE_IPA_PASS, /* type */
466 "opt_local_passes", /* name */
467 OPTGROUP_NONE, /* optinfo_flags */
468 TV_NONE, /* tv_id */
469 0, /* properties_required */
470 0, /* properties_provided */
471 0, /* properties_destroyed */
472 0, /* todo_flags_start */
473 0, /* todo_flags_finish */
476 class pass_local_optimization_passes : public simple_ipa_opt_pass
478 public:
479 pass_local_optimization_passes (gcc::context *ctxt)
480 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
483 /* opt_pass methods: */
484 virtual bool gate (function *)
486 /* Don't bother doing anything if the program has errors. */
487 return (!seen_error () && !in_lto_p);
490 }; // class pass_local_optimization_passes
492 } // anon namespace
494 simple_ipa_opt_pass *
495 make_pass_build_ssa_passes (gcc::context *ctxt)
497 return new pass_build_ssa_passes (ctxt);
500 simple_ipa_opt_pass *
501 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
503 return new pass_chkp_instrumentation_passes (ctxt);
506 simple_ipa_opt_pass *
507 make_pass_local_optimization_passes (gcc::context *ctxt)
509 return new pass_local_optimization_passes (ctxt);
512 namespace {
514 const pass_data pass_data_all_early_optimizations =
516 GIMPLE_PASS, /* type */
517 "early_optimizations", /* name */
518 OPTGROUP_NONE, /* optinfo_flags */
519 TV_NONE, /* tv_id */
520 0, /* properties_required */
521 0, /* properties_provided */
522 0, /* properties_destroyed */
523 0, /* todo_flags_start */
524 0, /* todo_flags_finish */
527 class pass_all_early_optimizations : public gimple_opt_pass
529 public:
530 pass_all_early_optimizations (gcc::context *ctxt)
531 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
534 /* opt_pass methods: */
535 virtual bool gate (function *)
537 return (optimize >= 1
538 /* Don't bother doing anything if the program has errors. */
539 && !seen_error ());
542 }; // class pass_all_early_optimizations
544 } // anon namespace
546 static gimple_opt_pass *
547 make_pass_all_early_optimizations (gcc::context *ctxt)
549 return new pass_all_early_optimizations (ctxt);
552 namespace {
554 const pass_data pass_data_all_optimizations =
556 GIMPLE_PASS, /* type */
557 "*all_optimizations", /* name */
558 OPTGROUP_NONE, /* optinfo_flags */
559 TV_OPTIMIZE, /* tv_id */
560 0, /* properties_required */
561 0, /* properties_provided */
562 0, /* properties_destroyed */
563 0, /* todo_flags_start */
564 0, /* todo_flags_finish */
567 class pass_all_optimizations : public gimple_opt_pass
569 public:
570 pass_all_optimizations (gcc::context *ctxt)
571 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
574 /* opt_pass methods: */
575 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
577 }; // class pass_all_optimizations
579 } // anon namespace
581 static gimple_opt_pass *
582 make_pass_all_optimizations (gcc::context *ctxt)
584 return new pass_all_optimizations (ctxt);
587 namespace {
589 const pass_data pass_data_all_optimizations_g =
591 GIMPLE_PASS, /* type */
592 "*all_optimizations_g", /* name */
593 OPTGROUP_NONE, /* optinfo_flags */
594 TV_OPTIMIZE, /* tv_id */
595 0, /* properties_required */
596 0, /* properties_provided */
597 0, /* properties_destroyed */
598 0, /* todo_flags_start */
599 0, /* todo_flags_finish */
602 class pass_all_optimizations_g : public gimple_opt_pass
604 public:
605 pass_all_optimizations_g (gcc::context *ctxt)
606 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
609 /* opt_pass methods: */
610 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
612 }; // class pass_all_optimizations_g
614 } // anon namespace
616 static gimple_opt_pass *
617 make_pass_all_optimizations_g (gcc::context *ctxt)
619 return new pass_all_optimizations_g (ctxt);
622 namespace {
624 const pass_data pass_data_rest_of_compilation =
626 RTL_PASS, /* type */
627 "*rest_of_compilation", /* name */
628 OPTGROUP_NONE, /* optinfo_flags */
629 TV_REST_OF_COMPILATION, /* tv_id */
630 PROP_rtl, /* properties_required */
631 0, /* properties_provided */
632 0, /* properties_destroyed */
633 0, /* todo_flags_start */
634 0, /* todo_flags_finish */
637 class pass_rest_of_compilation : public rtl_opt_pass
639 public:
640 pass_rest_of_compilation (gcc::context *ctxt)
641 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
644 /* opt_pass methods: */
645 virtual bool gate (function *)
647 /* Early return if there were errors. We can run afoul of our
648 consistency checks, and there's not really much point in fixing them. */
649 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
652 }; // class pass_rest_of_compilation
654 } // anon namespace
656 static rtl_opt_pass *
657 make_pass_rest_of_compilation (gcc::context *ctxt)
659 return new pass_rest_of_compilation (ctxt);
662 namespace {
664 const pass_data pass_data_postreload =
666 RTL_PASS, /* type */
667 "*all-postreload", /* name */
668 OPTGROUP_NONE, /* optinfo_flags */
669 TV_POSTRELOAD, /* tv_id */
670 PROP_rtl, /* properties_required */
671 0, /* properties_provided */
672 0, /* properties_destroyed */
673 0, /* todo_flags_start */
674 0, /* todo_flags_finish */
677 class pass_postreload : public rtl_opt_pass
679 public:
680 pass_postreload (gcc::context *ctxt)
681 : rtl_opt_pass (pass_data_postreload, ctxt)
684 /* opt_pass methods: */
685 virtual bool gate (function *) { return reload_completed; }
687 }; // class pass_postreload
689 } // anon namespace
691 static rtl_opt_pass *
692 make_pass_postreload (gcc::context *ctxt)
694 return new pass_postreload (ctxt);
697 namespace {
699 const pass_data pass_data_late_compilation =
701 RTL_PASS, /* type */
702 "*all-late_compilation", /* name */
703 OPTGROUP_NONE, /* optinfo_flags */
704 TV_LATE_COMPILATION, /* tv_id */
705 PROP_rtl, /* properties_required */
706 0, /* properties_provided */
707 0, /* properties_destroyed */
708 0, /* todo_flags_start */
709 0, /* todo_flags_finish */
712 class pass_late_compilation : public rtl_opt_pass
714 public:
715 pass_late_compilation (gcc::context *ctxt)
716 : rtl_opt_pass (pass_data_late_compilation, ctxt)
719 /* opt_pass methods: */
720 virtual bool gate (function *)
722 return reload_completed || targetm.no_register_allocation;
725 }; // class pass_late_compilation
727 } // anon namespace
729 static rtl_opt_pass *
730 make_pass_late_compilation (gcc::context *ctxt)
732 return new pass_late_compilation (ctxt);
737 /* Set the static pass number of pass PASS to ID and record that
738 in the mapping from static pass number to pass. */
740 void
741 pass_manager::
742 set_pass_for_id (int id, opt_pass *pass)
744 pass->static_pass_number = id;
745 if (passes_by_id_size <= id)
747 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
748 memset (passes_by_id + passes_by_id_size, 0,
749 (id + 1 - passes_by_id_size) * sizeof (void *));
750 passes_by_id_size = id + 1;
752 passes_by_id[id] = pass;
755 /* Return the pass with the static pass number ID. */
757 opt_pass *
758 pass_manager::get_pass_for_id (int id) const
760 if (id >= passes_by_id_size)
761 return NULL;
762 return passes_by_id[id];
765 /* Iterate over the pass tree allocating dump file numbers. We want
766 to do this depth first, and independent of whether the pass is
767 enabled or not. */
769 void
770 register_one_dump_file (opt_pass *pass)
772 g->get_passes ()->register_one_dump_file (pass);
775 void
776 pass_manager::register_one_dump_file (opt_pass *pass)
778 char *dot_name, *flag_name, *glob_name;
779 const char *name, *full_name, *prefix;
781 /* Buffer big enough to format a 32-bit UINT_MAX into. */
782 char num[11];
783 dump_kind dkind;
784 int id;
785 int optgroup_flags = OPTGROUP_NONE;
786 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
788 /* See below in next_pass_1. */
789 num[0] = '\0';
790 if (pass->static_pass_number != -1)
791 sprintf (num, "%u", ((int) pass->static_pass_number < 0
792 ? 1 : pass->static_pass_number));
794 /* The name is both used to identify the pass for the purposes of plugins,
795 and to specify dump file name and option.
796 The latter two might want something short which is not quite unique; for
797 that reason, we may have a disambiguating prefix, followed by a space
798 to mark the start of the following dump file name / option string. */
799 name = strchr (pass->name, ' ');
800 name = name ? name + 1 : pass->name;
801 dot_name = concat (".", name, num, NULL);
802 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
804 prefix = "ipa-";
805 dkind = DK_ipa;
806 optgroup_flags |= OPTGROUP_IPA;
808 else if (pass->type == GIMPLE_PASS)
810 prefix = "tree-";
811 dkind = DK_tree;
813 else
815 prefix = "rtl-";
816 dkind = DK_rtl;
819 flag_name = concat (prefix, name, num, NULL);
820 glob_name = concat (prefix, name, NULL);
821 optgroup_flags |= pass->optinfo_flags;
822 /* For any passes that do not have an optgroup set, and which are not
823 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
824 any dump messages are emitted properly under -fopt-info(-optall). */
825 if (optgroup_flags == OPTGROUP_NONE)
826 optgroup_flags = OPTGROUP_OTHER;
827 id = dumps->dump_register (dot_name, flag_name, glob_name, dkind,
828 optgroup_flags,
829 true);
830 set_pass_for_id (id, pass);
831 full_name = concat (prefix, pass->name, num, NULL);
832 register_pass_name (pass, full_name);
833 free (CONST_CAST (char *, full_name));
836 /* Register the dump files for the pass_manager starting at PASS. */
838 void
839 pass_manager::register_dump_files (opt_pass *pass)
843 if (pass->name && pass->name[0] != '*')
844 register_one_dump_file (pass);
846 if (pass->sub)
847 register_dump_files (pass->sub);
849 pass = pass->next;
851 while (pass);
854 /* Register PASS with NAME. */
856 void
857 pass_manager::register_pass_name (opt_pass *pass, const char *name)
859 if (!m_name_to_pass_map)
860 m_name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
862 if (m_name_to_pass_map->get (name))
863 return; /* Ignore plugin passes. */
865 const char *unique_name = xstrdup (name);
866 m_name_to_pass_map->put (unique_name, pass);
869 /* Map from pass id to canonicalized pass name. */
871 typedef const char *char_ptr;
872 static vec<char_ptr> pass_tab;
874 /* Callback function for traversing NAME_TO_PASS_MAP. */
876 bool
877 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
879 gcc_assert (pass->static_pass_number > 0);
880 gcc_assert (pass_tab.exists ());
882 pass_tab[pass->static_pass_number] = name;
884 return 1;
887 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
888 table for dumping purpose. */
890 void
891 pass_manager::create_pass_tab (void) const
893 if (!flag_dump_passes)
894 return;
896 pass_tab.safe_grow_cleared (passes_by_id_size + 1);
897 m_name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
900 static bool override_gate_status (opt_pass *, tree, bool);
902 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
903 is turned on or not. */
905 static void
906 dump_one_pass (opt_pass *pass, int pass_indent)
908 int indent = 3 * pass_indent;
909 const char *pn;
910 bool is_on, is_really_on;
912 is_on = pass->gate (cfun);
913 is_really_on = override_gate_status (pass, current_function_decl, is_on);
915 if (pass->static_pass_number <= 0)
916 pn = pass->name;
917 else
918 pn = pass_tab[pass->static_pass_number];
920 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
921 (15 - indent < 0 ? 0 : 15 - indent), " ",
922 is_on ? " ON" : " OFF",
923 ((!is_on) == (!is_really_on) ? ""
924 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
927 /* Dump pass list PASS with indentation INDENT. */
929 static void
930 dump_pass_list (opt_pass *pass, int indent)
934 dump_one_pass (pass, indent);
935 if (pass->sub)
936 dump_pass_list (pass->sub, indent + 1);
937 pass = pass->next;
939 while (pass);
942 /* Dump all optimization passes. */
944 void
945 dump_passes (void)
947 g->get_passes ()->dump_passes ();
950 void
951 pass_manager::dump_passes () const
953 push_dummy_function (true);
955 create_pass_tab ();
957 dump_pass_list (all_lowering_passes, 1);
958 dump_pass_list (all_small_ipa_passes, 1);
959 dump_pass_list (all_regular_ipa_passes, 1);
960 dump_pass_list (all_late_ipa_passes, 1);
961 dump_pass_list (all_passes, 1);
963 pop_dummy_function ();
966 /* Returns the pass with NAME. */
968 opt_pass *
969 pass_manager::get_pass_by_name (const char *name)
971 opt_pass **p = m_name_to_pass_map->get (name);
972 if (p)
973 return *p;
975 return NULL;
979 /* Range [start, last]. */
981 struct uid_range
983 unsigned int start;
984 unsigned int last;
985 const char *assem_name;
986 struct uid_range *next;
989 typedef struct uid_range *uid_range_p;
992 static vec<uid_range_p> enabled_pass_uid_range_tab;
993 static vec<uid_range_p> disabled_pass_uid_range_tab;
996 /* Parse option string for -fdisable- and -fenable-
997 The syntax of the options:
999 -fenable-<pass_name>
1000 -fdisable-<pass_name>
1002 -fenable-<pass_name>=s1:e1,s2:e2,...
1003 -fdisable-<pass_name>=s1:e1,s2:e2,...
1006 static void
1007 enable_disable_pass (const char *arg, bool is_enable)
1009 opt_pass *pass;
1010 char *range_str, *phase_name;
1011 char *argstr = xstrdup (arg);
1012 vec<uid_range_p> *tab = 0;
1014 range_str = strchr (argstr,'=');
1015 if (range_str)
1017 *range_str = '\0';
1018 range_str++;
1021 phase_name = argstr;
1022 if (!*phase_name)
1024 if (is_enable)
1025 error ("unrecognized option -fenable");
1026 else
1027 error ("unrecognized option -fdisable");
1028 free (argstr);
1029 return;
1031 pass = g->get_passes ()->get_pass_by_name (phase_name);
1032 if (!pass || pass->static_pass_number == -1)
1034 if (is_enable)
1035 error ("unknown pass %s specified in -fenable", phase_name);
1036 else
1037 error ("unknown pass %s specified in -fdisable", phase_name);
1038 free (argstr);
1039 return;
1042 if (is_enable)
1043 tab = &enabled_pass_uid_range_tab;
1044 else
1045 tab = &disabled_pass_uid_range_tab;
1047 if ((unsigned) pass->static_pass_number >= tab->length ())
1048 tab->safe_grow_cleared (pass->static_pass_number + 1);
1050 if (!range_str)
1052 uid_range_p slot;
1053 uid_range_p new_range = XCNEW (struct uid_range);
1055 new_range->start = 0;
1056 new_range->last = (unsigned)-1;
1058 slot = (*tab)[pass->static_pass_number];
1059 new_range->next = slot;
1060 (*tab)[pass->static_pass_number] = new_range;
1061 if (is_enable)
1062 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1063 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1064 else
1065 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1066 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1068 else
1070 char *next_range = NULL;
1071 char *one_range = range_str;
1072 char *end_val = NULL;
1076 uid_range_p slot;
1077 uid_range_p new_range;
1078 char *invalid = NULL;
1079 long start;
1080 char *func_name = NULL;
1082 next_range = strchr (one_range, ',');
1083 if (next_range)
1085 *next_range = '\0';
1086 next_range++;
1089 end_val = strchr (one_range, ':');
1090 if (end_val)
1092 *end_val = '\0';
1093 end_val++;
1095 start = strtol (one_range, &invalid, 10);
1096 if (*invalid || start < 0)
1098 if (end_val || (one_range[0] >= '0'
1099 && one_range[0] <= '9'))
1101 error ("Invalid range %s in option %s",
1102 one_range,
1103 is_enable ? "-fenable" : "-fdisable");
1104 free (argstr);
1105 return;
1107 func_name = one_range;
1109 if (!end_val)
1111 new_range = XCNEW (struct uid_range);
1112 if (!func_name)
1114 new_range->start = (unsigned) start;
1115 new_range->last = (unsigned) start;
1117 else
1119 new_range->start = (unsigned) -1;
1120 new_range->last = (unsigned) -1;
1121 new_range->assem_name = xstrdup (func_name);
1124 else
1126 long last = strtol (end_val, &invalid, 10);
1127 if (*invalid || last < start)
1129 error ("Invalid range %s in option %s",
1130 end_val,
1131 is_enable ? "-fenable" : "-fdisable");
1132 free (argstr);
1133 return;
1135 new_range = XCNEW (struct uid_range);
1136 new_range->start = (unsigned) start;
1137 new_range->last = (unsigned) last;
1140 slot = (*tab)[pass->static_pass_number];
1141 new_range->next = slot;
1142 (*tab)[pass->static_pass_number] = new_range;
1143 if (is_enable)
1145 if (new_range->assem_name)
1146 inform (UNKNOWN_LOCATION,
1147 "enable pass %s for function %s",
1148 phase_name, new_range->assem_name);
1149 else
1150 inform (UNKNOWN_LOCATION,
1151 "enable pass %s for functions in the range of [%u, %u]",
1152 phase_name, new_range->start, new_range->last);
1154 else
1156 if (new_range->assem_name)
1157 inform (UNKNOWN_LOCATION,
1158 "disable pass %s for function %s",
1159 phase_name, new_range->assem_name);
1160 else
1161 inform (UNKNOWN_LOCATION,
1162 "disable pass %s for functions in the range of [%u, %u]",
1163 phase_name, new_range->start, new_range->last);
1166 one_range = next_range;
1167 } while (next_range);
1170 free (argstr);
1173 /* Enable pass specified by ARG. */
1175 void
1176 enable_pass (const char *arg)
1178 enable_disable_pass (arg, true);
1181 /* Disable pass specified by ARG. */
1183 void
1184 disable_pass (const char *arg)
1186 enable_disable_pass (arg, false);
1189 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1191 static bool
1192 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1193 tree func,
1194 vec<uid_range_p> tab)
1196 uid_range_p slot, range;
1197 int cgraph_uid;
1198 const char *aname = NULL;
1200 if (!tab.exists ()
1201 || (unsigned) pass->static_pass_number >= tab.length ()
1202 || pass->static_pass_number == -1)
1203 return false;
1205 slot = tab[pass->static_pass_number];
1206 if (!slot)
1207 return false;
1209 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1210 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1211 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1213 range = slot;
1214 while (range)
1216 if ((unsigned) cgraph_uid >= range->start
1217 && (unsigned) cgraph_uid <= range->last)
1218 return true;
1219 if (range->assem_name && aname
1220 && !strcmp (range->assem_name, aname))
1221 return true;
1222 range = range->next;
1225 return false;
1229 /* Update static_pass_number for passes (and the flag
1230 TODO_mark_first_instance).
1232 Passes are constructed with static_pass_number preinitialized to 0
1234 This field is used in two different ways: initially as instance numbers
1235 of their kind, and then as ids within the entire pass manager.
1237 Within pass_manager::pass_manager:
1239 * In add_pass_instance(), as called by next_pass_1 in
1240 NEXT_PASS in init_optimization_passes
1242 * When the initial instance of a pass within a pass manager is seen,
1243 it is flagged, and its static_pass_number is set to -1
1245 * On subsequent times that it is seen, the static pass number
1246 is decremented each time, so that if there are e.g. 4 dups,
1247 they have static_pass_number -4, 2, 3, 4 respectively (note
1248 how the initial one is negative and gives the count); these
1249 can be thought of as instance numbers of the specific pass
1251 * Within the register_dump_files () traversal, set_pass_for_id()
1252 is called on each pass, using these instance numbers to create
1253 dumpfile switches, and then overwriting them with a pass id,
1254 which are global to the whole pass manager (based on
1255 (TDI_end + current value of extra_dump_files_in_use) ) */
1257 static void
1258 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1259 opt_pass *initial_pass)
1261 /* Are we dealing with the first pass of its kind, or a clone? */
1262 if (new_pass != initial_pass)
1264 /* We're dealing with a clone. */
1265 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1267 /* Indicate to register_dump_files that this pass has duplicates,
1268 and so it should rename the dump file. The first instance will
1269 be -1, and be number of duplicates = -static_pass_number - 1.
1270 Subsequent instances will be > 0 and just the duplicate number. */
1271 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1273 initial_pass->static_pass_number -= 1;
1274 new_pass->static_pass_number = -initial_pass->static_pass_number;
1277 else
1279 /* We're dealing with the first pass of its kind. */
1280 new_pass->todo_flags_start |= TODO_mark_first_instance;
1281 new_pass->static_pass_number = -1;
1283 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1287 /* Add a pass to the pass list. Duplicate the pass if it's already
1288 in the list. */
1290 static opt_pass **
1291 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1293 /* Every pass should have a name so that plugins can refer to them. */
1294 gcc_assert (pass->name != NULL);
1296 add_pass_instance (pass, false, initial_pass);
1297 *list = pass;
1299 return &(*list)->next;
1302 /* List node for an inserted pass instance. We need to keep track of all
1303 the newly-added pass instances (with 'added_pass_nodes' defined below)
1304 so that we can register their dump files after pass-positioning is finished.
1305 Registering dumping files needs to be post-processed or the
1306 static_pass_number of the opt_pass object would be modified and mess up
1307 the dump file names of future pass instances to be added. */
1309 struct pass_list_node
1311 opt_pass *pass;
1312 struct pass_list_node *next;
1315 static struct pass_list_node *added_pass_nodes = NULL;
1316 static struct pass_list_node *prev_added_pass_node;
1318 /* Insert the pass at the proper position. Return true if the pass
1319 is successfully added.
1321 NEW_PASS_INFO - new pass to be inserted
1322 PASS_LIST - root of the pass list to insert the new pass to */
1324 static bool
1325 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1327 opt_pass *pass = *pass_list, *prev_pass = NULL;
1328 bool success = false;
1330 for ( ; pass; prev_pass = pass, pass = pass->next)
1332 /* Check if the current pass is of the same type as the new pass and
1333 matches the name and the instance number of the reference pass. */
1334 if (pass->type == new_pass_info->pass->type
1335 && pass->name
1336 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1337 && ((new_pass_info->ref_pass_instance_number == 0)
1338 || (new_pass_info->ref_pass_instance_number ==
1339 pass->static_pass_number)
1340 || (new_pass_info->ref_pass_instance_number == 1
1341 && pass->todo_flags_start & TODO_mark_first_instance)))
1343 opt_pass *new_pass;
1344 struct pass_list_node *new_pass_node;
1346 if (new_pass_info->ref_pass_instance_number == 0)
1348 new_pass = new_pass_info->pass->clone ();
1349 add_pass_instance (new_pass, true, new_pass_info->pass);
1351 else
1353 new_pass = new_pass_info->pass;
1354 add_pass_instance (new_pass, true, new_pass);
1357 /* Insert the new pass instance based on the positioning op. */
1358 switch (new_pass_info->pos_op)
1360 case PASS_POS_INSERT_AFTER:
1361 new_pass->next = pass->next;
1362 pass->next = new_pass;
1364 /* Skip newly inserted pass to avoid repeated
1365 insertions in the case where the new pass and the
1366 existing one have the same name. */
1367 pass = new_pass;
1368 break;
1369 case PASS_POS_INSERT_BEFORE:
1370 new_pass->next = pass;
1371 if (prev_pass)
1372 prev_pass->next = new_pass;
1373 else
1374 *pass_list = new_pass;
1375 break;
1376 case PASS_POS_REPLACE:
1377 new_pass->next = pass->next;
1378 if (prev_pass)
1379 prev_pass->next = new_pass;
1380 else
1381 *pass_list = new_pass;
1382 new_pass->sub = pass->sub;
1383 new_pass->tv_id = pass->tv_id;
1384 pass = new_pass;
1385 break;
1386 default:
1387 error ("invalid pass positioning operation");
1388 return false;
1391 /* Save the newly added pass (instance) in the added_pass_nodes
1392 list so that we can register its dump file later. Note that
1393 we cannot register the dump file now because doing so will modify
1394 the static_pass_number of the opt_pass object and therefore
1395 mess up the dump file name of future instances. */
1396 new_pass_node = XCNEW (struct pass_list_node);
1397 new_pass_node->pass = new_pass;
1398 if (!added_pass_nodes)
1399 added_pass_nodes = new_pass_node;
1400 else
1401 prev_added_pass_node->next = new_pass_node;
1402 prev_added_pass_node = new_pass_node;
1404 success = true;
1407 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1408 success = true;
1411 return success;
1414 /* Hooks a new pass into the pass lists.
1416 PASS_INFO - pass information that specifies the opt_pass object,
1417 reference pass, instance number, and how to position
1418 the pass */
1420 void
1421 register_pass (struct register_pass_info *pass_info)
1423 g->get_passes ()->register_pass (pass_info);
1426 void
1427 register_pass (opt_pass* pass, pass_positioning_ops pos,
1428 const char* ref_pass_name, int ref_pass_inst_number)
1430 register_pass_info i;
1431 i.pass = pass;
1432 i.reference_pass_name = ref_pass_name;
1433 i.ref_pass_instance_number = ref_pass_inst_number;
1434 i.pos_op = pos;
1436 g->get_passes ()->register_pass (&i);
1439 void
1440 pass_manager::register_pass (struct register_pass_info *pass_info)
1442 bool all_instances, success;
1443 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1445 /* The checks below could fail in buggy plugins. Existing GCC
1446 passes should never fail these checks, so we mention plugin in
1447 the messages. */
1448 if (!pass_info->pass)
1449 fatal_error (input_location, "plugin cannot register a missing pass");
1451 if (!pass_info->pass->name)
1452 fatal_error (input_location, "plugin cannot register an unnamed pass");
1454 if (!pass_info->reference_pass_name)
1455 fatal_error
1456 (input_location,
1457 "plugin cannot register pass %qs without reference pass name",
1458 pass_info->pass->name);
1460 /* Try to insert the new pass to the pass lists. We need to check
1461 all five lists as the reference pass could be in one (or all) of
1462 them. */
1463 all_instances = pass_info->ref_pass_instance_number == 0;
1464 success = position_pass (pass_info, &all_lowering_passes);
1465 if (!success || all_instances)
1466 success |= position_pass (pass_info, &all_small_ipa_passes);
1467 if (!success || all_instances)
1468 success |= position_pass (pass_info, &all_regular_ipa_passes);
1469 if (!success || all_instances)
1470 success |= position_pass (pass_info, &all_late_ipa_passes);
1471 if (!success || all_instances)
1472 success |= position_pass (pass_info, &all_passes);
1473 if (!success)
1474 fatal_error
1475 (input_location,
1476 "pass %qs not found but is referenced by new pass %qs",
1477 pass_info->reference_pass_name, pass_info->pass->name);
1479 /* OK, we have successfully inserted the new pass. We need to register
1480 the dump files for the newly added pass and its duplicates (if any).
1481 Because the registration of plugin/backend passes happens after the
1482 command-line options are parsed, the options that specify single
1483 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1484 passes. Therefore we currently can only enable dumping of
1485 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1486 are specified. While doing so, we also delete the pass_list_node
1487 objects created during pass positioning. */
1488 while (added_pass_nodes)
1490 struct pass_list_node *next_node = added_pass_nodes->next;
1491 enum tree_dump_index tdi;
1492 register_one_dump_file (added_pass_nodes->pass);
1493 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1494 || added_pass_nodes->pass->type == IPA_PASS)
1495 tdi = TDI_ipa_all;
1496 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1497 tdi = TDI_tree_all;
1498 else
1499 tdi = TDI_rtl_all;
1500 /* Check if dump-all flag is specified. */
1501 if (dumps->get_dump_file_info (tdi)->pstate)
1503 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1504 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1505 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1506 ->pflags = dumps->get_dump_file_info (tdi)->pflags;
1508 XDELETE (added_pass_nodes);
1509 added_pass_nodes = next_node;
1513 /* Construct the pass tree. The sequencing of passes is driven by
1514 the cgraph routines:
1516 finalize_compilation_unit ()
1517 for each node N in the cgraph
1518 cgraph_analyze_function (N)
1519 cgraph_lower_function (N) -> all_lowering_passes
1521 If we are optimizing, compile is then invoked:
1523 compile ()
1524 ipa_passes () -> all_small_ipa_passes
1525 -> Analysis of all_regular_ipa_passes
1526 * possible LTO streaming at copmilation time *
1527 -> Execution of all_regular_ipa_passes
1528 * possible LTO streaming at link time *
1529 -> all_late_ipa_passes
1530 expand_all_functions ()
1531 for each node N in the cgraph
1532 expand_function (N) -> Transformation of all_regular_ipa_passes
1533 -> all_passes
1536 pass_manager::pass_manager (context *ctxt)
1537 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1538 all_regular_ipa_passes (NULL),
1539 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1540 m_ctxt (ctxt), m_name_to_pass_map (NULL)
1542 opt_pass **p;
1544 /* Zero-initialize pass members. */
1545 #define INSERT_PASSES_AFTER(PASS)
1546 #define PUSH_INSERT_PASSES_WITHIN(PASS)
1547 #define POP_INSERT_PASSES()
1548 #define NEXT_PASS(PASS, NUM) PASS ## _ ## NUM = NULL
1549 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) NEXT_PASS (PASS, NUM)
1550 #define TERMINATE_PASS_LIST(PASS)
1551 #include "pass-instances.def"
1552 #undef INSERT_PASSES_AFTER
1553 #undef PUSH_INSERT_PASSES_WITHIN
1554 #undef POP_INSERT_PASSES
1555 #undef NEXT_PASS
1556 #undef NEXT_PASS_WITH_ARG
1557 #undef TERMINATE_PASS_LIST
1559 /* Initialize the pass_lists array. */
1560 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1561 GCC_PASS_LISTS
1562 #undef DEF_PASS_LIST
1564 /* Build the tree of passes. */
1566 #define INSERT_PASSES_AFTER(PASS) \
1568 opt_pass **p_start; \
1569 p_start = p = &(PASS);
1571 #define TERMINATE_PASS_LIST(PASS) \
1572 gcc_assert (p_start == &PASS); \
1573 *p = NULL; \
1576 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1578 opt_pass **p = &(PASS ## _1)->sub;
1580 #define POP_INSERT_PASSES() \
1583 #define NEXT_PASS(PASS, NUM) \
1584 do { \
1585 gcc_assert (NULL == PASS ## _ ## NUM); \
1586 if ((NUM) == 1) \
1587 PASS ## _1 = make_##PASS (m_ctxt); \
1588 else \
1590 gcc_assert (PASS ## _1); \
1591 PASS ## _ ## NUM = PASS ## _1->clone (); \
1593 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1594 } while (0)
1596 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) \
1597 do { \
1598 NEXT_PASS (PASS, NUM); \
1599 PASS ## _ ## NUM->set_pass_param (0, ARG); \
1600 } while (0)
1602 #include "pass-instances.def"
1604 #undef INSERT_PASSES_AFTER
1605 #undef PUSH_INSERT_PASSES_WITHIN
1606 #undef POP_INSERT_PASSES
1607 #undef NEXT_PASS
1608 #undef NEXT_PASS_WITH_ARG
1609 #undef TERMINATE_PASS_LIST
1611 /* Register the passes with the tree dump code. */
1612 register_dump_files (all_lowering_passes);
1613 register_dump_files (all_small_ipa_passes);
1614 register_dump_files (all_regular_ipa_passes);
1615 register_dump_files (all_late_ipa_passes);
1616 register_dump_files (all_passes);
1619 static void
1620 delete_pass_tree (opt_pass *pass)
1622 while (pass)
1624 /* Recurse into child passes. */
1625 delete_pass_tree (pass->sub);
1627 opt_pass *next = pass->next;
1629 /* Delete this pass. */
1630 delete pass;
1632 /* Iterate onto sibling passes. */
1633 pass = next;
1637 pass_manager::~pass_manager ()
1639 XDELETEVEC (passes_by_id);
1641 /* Call delete_pass_tree on each of the pass_lists. */
1642 #define DEF_PASS_LIST(LIST) \
1643 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1644 GCC_PASS_LISTS
1645 #undef DEF_PASS_LIST
1649 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1650 function CALLBACK for every function in the call graph. Otherwise,
1651 call CALLBACK on the current function. */
1653 static void
1654 do_per_function (void (*callback) (function *, void *data), void *data)
1656 if (current_function_decl)
1657 callback (cfun, data);
1658 else
1660 struct cgraph_node *node;
1661 FOR_EACH_DEFINED_FUNCTION (node)
1662 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1663 && (!node->clone_of || node->decl != node->clone_of->decl))
1664 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1668 /* Because inlining might remove no-longer reachable nodes, we need to
1669 keep the array visible to garbage collector to avoid reading collected
1670 out nodes. */
1671 static int nnodes;
1672 static GTY ((length ("nnodes"))) cgraph_node **order;
1674 /* Hook called when NODE is removed and therefore should be
1675 excluded from order vector. DATA is an array of integers.
1676 DATA[0] holds max index it may be accessed by. For cgraph
1677 node DATA[node->uid + 1] holds index of this node in order
1678 vector. */
1679 static void
1680 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1682 int *order_idx = (int *)data;
1684 if (node->uid >= order_idx[0])
1685 return;
1687 int idx = order_idx[node->uid + 1];
1688 if (idx >= 0 && idx < nnodes && order[idx] == node)
1689 order[idx] = NULL;
1692 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1693 function CALLBACK for every function in the call graph. Otherwise,
1694 call CALLBACK on the current function.
1695 This function is global so that plugins can use it. */
1696 void
1697 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1699 int i;
1701 if (current_function_decl)
1702 callback (cfun, data);
1703 else
1705 cgraph_node_hook_list *hook;
1706 int *order_idx;
1707 gcc_assert (!order);
1708 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1710 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1711 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1712 order_idx[0] = symtab->cgraph_max_uid;
1714 nnodes = ipa_reverse_postorder (order);
1715 for (i = nnodes - 1; i >= 0; i--)
1717 order[i]->process = 1;
1718 order_idx[order[i]->uid + 1] = i;
1720 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1721 order_idx);
1722 for (i = nnodes - 1; i >= 0; i--)
1724 /* Function could be inlined and removed as unreachable. */
1725 if (!order[i])
1726 continue;
1728 struct cgraph_node *node = order[i];
1730 /* Allow possibly removed nodes to be garbage collected. */
1731 order[i] = NULL;
1732 node->process = 0;
1733 if (node->has_gimple_body_p ())
1735 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1736 push_cfun (fn);
1737 callback (fn, data);
1738 pop_cfun ();
1741 symtab->remove_cgraph_removal_hook (hook);
1743 ggc_free (order);
1744 order = NULL;
1745 nnodes = 0;
1748 /* Helper function to perform function body dump. */
1750 static void
1751 execute_function_dump (function *fn, void *data)
1753 opt_pass *pass = (opt_pass *)data;
1755 if (dump_file)
1757 push_cfun (fn);
1759 if (fn->curr_properties & PROP_trees)
1760 dump_function_to_file (fn->decl, dump_file, dump_flags);
1761 else
1762 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1764 /* Flush the file. If verification fails, we won't be able to
1765 close the file before aborting. */
1766 fflush (dump_file);
1768 if ((fn->curr_properties & PROP_cfg)
1769 && (dump_flags & TDF_GRAPH))
1771 gcc::dump_manager *dumps = g->get_dumps ();
1772 struct dump_file_info *dfi
1773 = dumps->get_dump_file_info (pass->static_pass_number);
1774 if (!dfi->graph_dump_initialized)
1776 clean_graph_dump_file (dump_file_name);
1777 dfi->graph_dump_initialized = true;
1779 print_graph_cfg (dump_file_name, fn);
1782 pop_cfun ();
1786 /* This function is called when an internal compiler error is encountered.
1787 Ensure that function dump is made available before compiler is aborted. */
1789 void
1790 emergency_dump_function ()
1792 if (!current_pass)
1793 return;
1794 enum opt_pass_type pt = current_pass->type;
1795 fnotice (stderr, "during %s pass: %s\n",
1796 pt == GIMPLE_PASS ? "GIMPLE" : pt == RTL_PASS ? "RTL" : "IPA",
1797 current_pass->name);
1798 if (!dump_file || !cfun)
1799 return;
1800 fnotice (stderr, "dump file: %s\n", dump_file_name);
1801 fprintf (dump_file, "\n\n\nEMERGENCY DUMP:\n\n");
1802 execute_function_dump (cfun, current_pass);
1805 static struct profile_record *profile_record;
1807 /* Do profile consistency book-keeping for the pass with static number INDEX.
1808 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1809 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1810 if we are only book-keeping on passes that may have selectively disabled
1811 themselves on a given function. */
1812 static void
1813 check_profile_consistency (int index, int subpass, bool run)
1815 pass_manager *passes = g->get_passes ();
1816 if (index == -1)
1817 return;
1818 if (!profile_record)
1819 profile_record = XCNEWVEC (struct profile_record,
1820 passes->passes_by_id_size);
1821 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1822 gcc_assert (subpass < 2);
1823 profile_record[index].run |= run;
1824 account_profile_record (&profile_record[index], subpass);
1827 /* Output profile consistency. */
1829 void
1830 dump_profile_report (void)
1832 g->get_passes ()->dump_profile_report ();
1835 void
1836 pass_manager::dump_profile_report () const
1838 int i, j;
1839 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1840 gcov_type last_time = 0, last_size = 0;
1841 double rel_time_change, rel_size_change;
1842 int last_reported = 0;
1844 if (!profile_record)
1845 return;
1846 fprintf (stderr, "\nProfile consistency report:\n\n");
1847 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1848 fprintf (stderr, " |freq count |freq count |size time\n");
1850 for (i = 0; i < passes_by_id_size; i++)
1851 for (j = 0 ; j < 2; j++)
1852 if (profile_record[i].run)
1854 if (last_time)
1855 rel_time_change = (profile_record[i].time[j]
1856 - (double)last_time) * 100 / (double)last_time;
1857 else
1858 rel_time_change = 0;
1859 if (last_size)
1860 rel_size_change = (profile_record[i].size[j]
1861 - (double)last_size) * 100 / (double)last_size;
1862 else
1863 rel_size_change = 0;
1865 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1866 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1867 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1868 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1869 || rel_time_change || rel_size_change)
1871 last_reported = i;
1872 fprintf (stderr, "%-20s %s",
1873 passes_by_id [i]->name,
1874 j ? "(after TODO)" : " ");
1875 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1876 fprintf (stderr, "| %+5i",
1877 profile_record[i].num_mismatched_freq_in[j]
1878 - last_freq_in);
1879 else
1880 fprintf (stderr, "| ");
1881 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1882 fprintf (stderr, " %+5i",
1883 profile_record[i].num_mismatched_count_in[j]
1884 - last_count_in);
1885 else
1886 fprintf (stderr, " ");
1887 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1888 fprintf (stderr, "| %+5i",
1889 profile_record[i].num_mismatched_freq_out[j]
1890 - last_freq_out);
1891 else
1892 fprintf (stderr, "| ");
1893 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1894 fprintf (stderr, " %+5i",
1895 profile_record[i].num_mismatched_count_out[j]
1896 - last_count_out);
1897 else
1898 fprintf (stderr, " ");
1900 /* Size/time units change across gimple and RTL. */
1901 if (i == pass_expand_1->static_pass_number)
1902 fprintf (stderr, "|----------");
1903 else
1905 if (rel_size_change)
1906 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1907 else
1908 fprintf (stderr, "| ");
1909 if (rel_time_change)
1910 fprintf (stderr, " %+8.4f%%", rel_time_change);
1912 fprintf (stderr, "\n");
1913 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1914 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1915 last_count_in = profile_record[i].num_mismatched_count_in[j];
1916 last_count_out = profile_record[i].num_mismatched_count_out[j];
1918 else if (j && last_reported != i)
1920 last_reported = i;
1921 fprintf (stderr, "%-20s ------------| | |\n",
1922 passes_by_id [i]->name);
1924 last_time = profile_record[i].time[j];
1925 last_size = profile_record[i].size[j];
1929 /* Perform all TODO actions that ought to be done on each function. */
1931 static void
1932 execute_function_todo (function *fn, void *data)
1934 bool from_ipa_pass = (cfun == NULL);
1935 unsigned int flags = (size_t)data;
1936 flags &= ~fn->last_verified;
1937 if (!flags)
1938 return;
1940 push_cfun (fn);
1942 /* Always cleanup the CFG before trying to update SSA. */
1943 if (flags & TODO_cleanup_cfg)
1945 cleanup_tree_cfg ();
1947 /* When cleanup_tree_cfg merges consecutive blocks, it may
1948 perform some simplistic propagation when removing single
1949 valued PHI nodes. This propagation may, in turn, cause the
1950 SSA form to become out-of-date (see PR 22037). So, even
1951 if the parent pass had not scheduled an SSA update, we may
1952 still need to do one. */
1953 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1954 flags |= TODO_update_ssa;
1957 if (flags & TODO_update_ssa_any)
1959 unsigned update_flags = flags & TODO_update_ssa_any;
1960 update_ssa (update_flags);
1963 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1964 compute_may_aliases ();
1966 if (optimize && (flags & TODO_update_address_taken))
1967 execute_update_addresses_taken ();
1969 if (flags & TODO_remove_unused_locals)
1970 remove_unused_locals ();
1972 if (flags & TODO_rebuild_frequencies)
1973 rebuild_frequencies ();
1975 if (flags & TODO_rebuild_cgraph_edges)
1976 cgraph_edge::rebuild_edges ();
1978 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1979 /* If we've seen errors do not bother running any verifiers. */
1980 if (flag_checking && !seen_error ())
1982 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1983 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1985 if (flags & TODO_verify_il)
1987 if (cfun->curr_properties & PROP_trees)
1989 if (cfun->curr_properties & PROP_cfg)
1990 /* IPA passes leave stmts to be fixed up, so make sure to
1991 not verify stmts really throw. */
1992 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1993 else
1994 verify_gimple_in_seq (gimple_body (cfun->decl));
1996 if (cfun->curr_properties & PROP_ssa)
1997 /* IPA passes leave stmts to be fixed up, so make sure to
1998 not verify SSA operands whose verifier will choke on that. */
1999 verify_ssa (true, !from_ipa_pass);
2000 /* IPA passes leave basic-blocks unsplit, so make sure to
2001 not trip on that. */
2002 if ((cfun->curr_properties & PROP_cfg)
2003 && !from_ipa_pass)
2004 verify_flow_info ();
2005 if (current_loops
2006 && ! loops_state_satisfies_p (LOOPS_NEED_FIXUP))
2008 verify_loop_structure ();
2009 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
2010 verify_loop_closed_ssa (false);
2012 if (cfun->curr_properties & PROP_rtl)
2013 verify_rtl_sharing ();
2016 /* Make sure verifiers don't change dominator state. */
2017 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
2018 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
2021 fn->last_verified = flags & TODO_verify_all;
2023 pop_cfun ();
2025 /* For IPA passes make sure to release dominator info, it can be
2026 computed by non-verifying TODOs. */
2027 if (from_ipa_pass)
2029 free_dominance_info (fn, CDI_DOMINATORS);
2030 free_dominance_info (fn, CDI_POST_DOMINATORS);
2034 /* Perform all TODO actions. */
2035 static void
2036 execute_todo (unsigned int flags)
2038 if (flag_checking
2039 && cfun
2040 && need_ssa_update_p (cfun))
2041 gcc_assert (flags & TODO_update_ssa_any);
2043 statistics_fini_pass ();
2045 if (flags)
2046 do_per_function (execute_function_todo, (void *)(size_t) flags);
2048 /* At this point we should not have any unreachable code in the
2049 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
2050 if (cfun && cfun->gimple_df)
2051 flush_ssaname_freelist ();
2053 /* Always remove functions just as before inlining: IPA passes might be
2054 interested to see bodies of extern inline functions that are not inlined
2055 to analyze side effects. The full removal is done just at the end
2056 of IPA pass queue. */
2057 if (flags & TODO_remove_functions)
2059 gcc_assert (!cfun);
2060 symtab->remove_unreachable_nodes (dump_file);
2063 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2065 gcc_assert (!cfun);
2066 symtab->dump (dump_file);
2067 /* Flush the file. If verification fails, we won't be able to
2068 close the file before aborting. */
2069 fflush (dump_file);
2072 /* Now that the dumping has been done, we can get rid of the optional
2073 df problems. */
2074 if (flags & TODO_df_finish)
2075 df_finish_pass ((flags & TODO_df_verify) != 0);
2078 /* Verify invariants that should hold between passes. This is a place
2079 to put simple sanity checks. */
2081 static void
2082 verify_interpass_invariants (void)
2084 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2087 /* Clear the last verified flag. */
2089 static void
2090 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2092 fn->last_verified = 0;
2095 /* Helper function. Verify that the properties has been turn into the
2096 properties expected by the pass. */
2098 static void
2099 verify_curr_properties (function *fn, void *data)
2101 unsigned int props = (size_t)data;
2102 gcc_assert ((fn->curr_properties & props) == props);
2105 /* Release dump file name if set. */
2107 static void
2108 release_dump_file_name (void)
2110 if (dump_file_name)
2112 free (CONST_CAST (char *, dump_file_name));
2113 dump_file_name = NULL;
2117 /* Initialize pass dump file. */
2118 /* This is non-static so that the plugins can use it. */
2120 bool
2121 pass_init_dump_file (opt_pass *pass)
2123 /* If a dump file name is present, open it if enabled. */
2124 if (pass->static_pass_number != -1)
2126 timevar_push (TV_DUMP);
2127 gcc::dump_manager *dumps = g->get_dumps ();
2128 bool initializing_dump =
2129 !dumps->dump_initialized_p (pass->static_pass_number);
2130 release_dump_file_name ();
2131 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2132 dumps->dump_start (pass->static_pass_number, &dump_flags);
2133 if (dump_file && current_function_decl && ! (dump_flags & TDF_GIMPLE))
2134 dump_function_header (dump_file, current_function_decl, dump_flags);
2135 if (initializing_dump
2136 && dump_file && (dump_flags & TDF_GRAPH)
2137 && cfun && (cfun->curr_properties & PROP_cfg))
2139 clean_graph_dump_file (dump_file_name);
2140 struct dump_file_info *dfi
2141 = dumps->get_dump_file_info (pass->static_pass_number);
2142 dfi->graph_dump_initialized = true;
2144 timevar_pop (TV_DUMP);
2145 return initializing_dump;
2147 else
2148 return false;
2151 /* Flush PASS dump file. */
2152 /* This is non-static so that plugins can use it. */
2154 void
2155 pass_fini_dump_file (opt_pass *pass)
2157 timevar_push (TV_DUMP);
2159 /* Flush and close dump file. */
2160 release_dump_file_name ();
2162 g->get_dumps ()->dump_finish (pass->static_pass_number);
2163 timevar_pop (TV_DUMP);
2166 /* After executing the pass, apply expected changes to the function
2167 properties. */
2169 static void
2170 update_properties_after_pass (function *fn, void *data)
2172 opt_pass *pass = (opt_pass *) data;
2173 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2174 & ~pass->properties_destroyed;
2177 /* Execute summary generation for all of the passes in IPA_PASS. */
2179 void
2180 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2182 while (ipa_pass)
2184 opt_pass *pass = ipa_pass;
2186 /* Execute all of the IPA_PASSes in the list. */
2187 if (ipa_pass->type == IPA_PASS
2188 && pass->gate (cfun)
2189 && ipa_pass->generate_summary)
2191 pass_init_dump_file (pass);
2193 /* If a timevar is present, start it. */
2194 if (pass->tv_id)
2195 timevar_push (pass->tv_id);
2197 current_pass = pass;
2198 ipa_pass->generate_summary ();
2200 /* Stop timevar. */
2201 if (pass->tv_id)
2202 timevar_pop (pass->tv_id);
2204 pass_fini_dump_file (pass);
2206 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2210 /* Execute IPA_PASS function transform on NODE. */
2212 static void
2213 execute_one_ipa_transform_pass (struct cgraph_node *node,
2214 ipa_opt_pass_d *ipa_pass)
2216 opt_pass *pass = ipa_pass;
2217 unsigned int todo_after = 0;
2219 current_pass = pass;
2220 if (!ipa_pass->function_transform)
2221 return;
2223 /* Note that the folders should only create gimple expressions.
2224 This is a hack until the new folder is ready. */
2225 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2227 pass_init_dump_file (pass);
2229 /* If a timevar is present, start it. */
2230 if (pass->tv_id != TV_NONE)
2231 timevar_push (pass->tv_id);
2233 /* Run pre-pass verification. */
2234 execute_todo (ipa_pass->function_transform_todo_flags_start);
2236 /* Do it! */
2237 todo_after = ipa_pass->function_transform (node);
2239 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2240 check_profile_consistency (pass->static_pass_number, 0, true);
2242 /* Run post-pass cleanup and verification. */
2243 execute_todo (todo_after);
2244 verify_interpass_invariants ();
2245 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2246 check_profile_consistency (pass->static_pass_number, 1, true);
2248 /* Stop timevar. */
2249 if (pass->tv_id != TV_NONE)
2250 timevar_pop (pass->tv_id);
2252 if (dump_file)
2253 do_per_function (execute_function_dump, pass);
2254 pass_fini_dump_file (pass);
2256 current_pass = NULL;
2257 redirect_edge_var_map_empty ();
2259 /* Signal this is a suitable GC collection point. */
2260 if (!(todo_after & TODO_do_not_ggc_collect))
2261 ggc_collect ();
2264 /* For the current function, execute all ipa transforms. */
2266 void
2267 execute_all_ipa_transforms (void)
2269 struct cgraph_node *node;
2270 if (!cfun)
2271 return;
2272 node = cgraph_node::get (current_function_decl);
2274 if (node->ipa_transforms_to_apply.exists ())
2276 unsigned int i;
2278 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2279 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2280 node->ipa_transforms_to_apply.release ();
2284 /* Check if PASS is explicitly disabled or enabled and return
2285 the gate status. FUNC is the function to be processed, and
2286 GATE_STATUS is the gate status determined by pass manager by
2287 default. */
2289 static bool
2290 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2292 bool explicitly_enabled = false;
2293 bool explicitly_disabled = false;
2295 explicitly_enabled
2296 = is_pass_explicitly_enabled_or_disabled (pass, func,
2297 enabled_pass_uid_range_tab);
2298 explicitly_disabled
2299 = is_pass_explicitly_enabled_or_disabled (pass, func,
2300 disabled_pass_uid_range_tab);
2302 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2304 return gate_status;
2307 /* Determine if PASS_NAME matches CRITERION.
2308 Not a pure predicate, since it can update CRITERION, to support
2309 matching the Nth invocation of a pass.
2310 Subroutine of should_skip_pass_p. */
2312 static bool
2313 determine_pass_name_match (const char *pass_name, char *criterion)
2315 size_t namelen = strlen (pass_name);
2316 if (! strncmp (pass_name, criterion, namelen))
2318 /* The following supports starting with the Nth invocation
2319 of a pass (where N does not necessarily is equal to the
2320 dump file suffix). */
2321 if (criterion[namelen] == '\0'
2322 || (criterion[namelen] == '1'
2323 && criterion[namelen + 1] == '\0'))
2324 return true;
2325 else
2327 if (criterion[namelen + 1] == '\0')
2328 --criterion[namelen];
2329 return false;
2332 else
2333 return false;
2336 /* For skipping passes until "startwith" pass.
2337 Return true iff PASS should be skipped.
2338 Clear cfun->pass_startwith when encountering the "startwith" pass,
2339 so that all subsequent passes are run. */
2341 static bool
2342 should_skip_pass_p (opt_pass *pass)
2344 if (!cfun)
2345 return false;
2346 if (!cfun->pass_startwith)
2347 return false;
2349 /* For __GIMPLE functions, we have to at least start when we leave
2350 SSA. Hence, we need to detect the "expand" pass, and stop skipping
2351 when we encounter it. A cheap way to identify "expand" is it to
2352 detect the destruction of PROP_ssa.
2353 For __RTL functions, we invoke "rest_of_compilation" directly, which
2354 is after "expand", and hence we don't reach this conditional. */
2355 if (pass->properties_destroyed & PROP_ssa)
2357 if (!quiet_flag)
2358 fprintf (stderr, "starting anyway when leaving SSA: %s\n", pass->name);
2359 cfun->pass_startwith = NULL;
2360 return false;
2363 if (determine_pass_name_match (pass->name, cfun->pass_startwith))
2365 if (!quiet_flag)
2366 fprintf (stderr, "found starting pass: %s\n", pass->name);
2367 cfun->pass_startwith = NULL;
2368 return false;
2371 /* For GIMPLE passes, run any property provider (but continue skipping
2372 afterwards).
2373 We don't want to force running RTL passes that are property providers:
2374 "expand" is covered above, and the only pass other than "expand" that
2375 provides a property is "into_cfglayout" (PROP_cfglayout), which does
2376 too much for a dumped __RTL function. */
2377 if (pass->type == GIMPLE_PASS
2378 && pass->properties_provided != 0)
2379 return false;
2381 /* Don't skip df init; later RTL passes need it. */
2382 if (strstr (pass->name, "dfinit") != NULL)
2383 return false;
2385 if (!quiet_flag)
2386 fprintf (stderr, "skipping pass: %s\n", pass->name);
2388 /* If we get here, then we have a "startwith" that we haven't seen yet;
2389 skip the pass. */
2390 return true;
2393 /* Skip the given pass, for handling passes before "startwith"
2394 in __GIMPLE and__RTL-marked functions.
2395 In theory, this ought to be a no-op, but some of the RTL passes
2396 need additional processing here. */
2398 static void
2399 skip_pass (opt_pass *pass)
2401 /* Pass "reload" sets the global "reload_completed", and many
2402 things depend on this (e.g. instructions in .md files). */
2403 if (strcmp (pass->name, "reload") == 0)
2404 reload_completed = 1;
2406 /* The INSN_ADDRESSES vec is normally set up by
2407 shorten_branches; set it up for the benefit of passes that
2408 run after this. */
2409 if (strcmp (pass->name, "shorten") == 0)
2410 INSN_ADDRESSES_ALLOC (get_max_uid ());
2412 /* Update the cfg hooks as appropriate. */
2413 if (strcmp (pass->name, "into_cfglayout") == 0)
2415 cfg_layout_rtl_register_cfg_hooks ();
2416 cfun->curr_properties |= PROP_cfglayout;
2418 if (strcmp (pass->name, "outof_cfglayout") == 0)
2420 rtl_register_cfg_hooks ();
2421 cfun->curr_properties &= ~PROP_cfglayout;
2425 /* Execute PASS. */
2427 bool
2428 execute_one_pass (opt_pass *pass)
2430 unsigned int todo_after = 0;
2432 bool gate_status;
2434 /* IPA passes are executed on whole program, so cfun should be NULL.
2435 Other passes need function context set. */
2436 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2437 gcc_assert (!cfun && !current_function_decl);
2438 else
2439 gcc_assert (cfun && current_function_decl);
2441 current_pass = pass;
2443 /* Check whether gate check should be avoided.
2444 User controls the value of the gate through the parameter "gate_status". */
2445 gate_status = pass->gate (cfun);
2446 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2448 /* Override gate with plugin. */
2449 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2451 if (!gate_status)
2453 /* Run so passes selectively disabling themselves on a given function
2454 are not miscounted. */
2455 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2457 check_profile_consistency (pass->static_pass_number, 0, false);
2458 check_profile_consistency (pass->static_pass_number, 1, false);
2460 current_pass = NULL;
2461 return false;
2464 if (should_skip_pass_p (pass))
2466 skip_pass (pass);
2467 return true;
2470 /* Pass execution event trigger: useful to identify passes being
2471 executed. */
2472 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2474 if (!quiet_flag && !cfun)
2475 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2477 /* Note that the folders should only create gimple expressions.
2478 This is a hack until the new folder is ready. */
2479 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2481 pass_init_dump_file (pass);
2483 /* If a timevar is present, start it. */
2484 if (pass->tv_id != TV_NONE)
2485 timevar_push (pass->tv_id);
2487 /* Run pre-pass verification. */
2488 execute_todo (pass->todo_flags_start);
2490 if (flag_checking)
2491 do_per_function (verify_curr_properties,
2492 (void *)(size_t)pass->properties_required);
2494 /* Do it! */
2495 todo_after = pass->execute (cfun);
2497 if (todo_after & TODO_discard_function)
2499 /* Stop timevar. */
2500 if (pass->tv_id != TV_NONE)
2501 timevar_pop (pass->tv_id);
2503 pass_fini_dump_file (pass);
2505 gcc_assert (cfun);
2506 /* As cgraph_node::release_body expects release dominators info,
2507 we have to release it. */
2508 if (dom_info_available_p (CDI_DOMINATORS))
2509 free_dominance_info (CDI_DOMINATORS);
2511 if (dom_info_available_p (CDI_POST_DOMINATORS))
2512 free_dominance_info (CDI_POST_DOMINATORS);
2514 tree fn = cfun->decl;
2515 pop_cfun ();
2516 gcc_assert (!cfun);
2517 cgraph_node::get (fn)->release_body ();
2519 current_pass = NULL;
2520 redirect_edge_var_map_empty ();
2522 ggc_collect ();
2524 return true;
2527 do_per_function (clear_last_verified, NULL);
2529 do_per_function (update_properties_after_pass, pass);
2531 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2532 check_profile_consistency (pass->static_pass_number, 0, true);
2534 /* Run post-pass cleanup and verification. */
2535 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2536 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2537 check_profile_consistency (pass->static_pass_number, 1, true);
2539 verify_interpass_invariants ();
2541 /* Stop timevar. */
2542 if (pass->tv_id != TV_NONE)
2543 timevar_pop (pass->tv_id);
2545 if (pass->type == IPA_PASS
2546 && ((ipa_opt_pass_d *)pass)->function_transform)
2548 struct cgraph_node *node;
2549 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2550 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2552 else if (dump_file)
2553 do_per_function (execute_function_dump, pass);
2555 if (!current_function_decl)
2556 symtab->process_new_functions ();
2558 pass_fini_dump_file (pass);
2560 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2561 gcc_assert (!(cfun->curr_properties & PROP_trees)
2562 || pass->type != RTL_PASS);
2564 current_pass = NULL;
2565 redirect_edge_var_map_empty ();
2567 /* Signal this is a suitable GC collection point. */
2568 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2569 ggc_collect ();
2571 return true;
2574 static void
2575 execute_pass_list_1 (opt_pass *pass)
2579 gcc_assert (pass->type == GIMPLE_PASS
2580 || pass->type == RTL_PASS);
2582 if (cfun == NULL)
2583 return;
2584 if (execute_one_pass (pass) && pass->sub)
2585 execute_pass_list_1 (pass->sub);
2586 pass = pass->next;
2588 while (pass);
2591 void
2592 execute_pass_list (function *fn, opt_pass *pass)
2594 gcc_assert (fn == cfun);
2595 execute_pass_list_1 (pass);
2596 if (cfun && fn->cfg)
2598 free_dominance_info (CDI_DOMINATORS);
2599 free_dominance_info (CDI_POST_DOMINATORS);
2603 /* Write out all LTO data. */
2604 static void
2605 write_lto (void)
2607 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2608 lto_output ();
2609 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2610 timevar_push (TV_IPA_LTO_DECL_OUT);
2611 produce_asm_for_decls ();
2612 timevar_pop (TV_IPA_LTO_DECL_OUT);
2615 /* Same as execute_pass_list but assume that subpasses of IPA passes
2616 are local passes. If SET is not NULL, write out summaries of only
2617 those node in SET. */
2619 static void
2620 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2622 while (pass)
2624 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2625 gcc_assert (!current_function_decl);
2626 gcc_assert (!cfun);
2627 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2628 if (pass->type == IPA_PASS
2629 && ipa_pass->write_summary
2630 && pass->gate (cfun))
2632 /* If a timevar is present, start it. */
2633 if (pass->tv_id)
2634 timevar_push (pass->tv_id);
2636 pass_init_dump_file (pass);
2638 current_pass = pass;
2639 ipa_pass->write_summary ();
2641 pass_fini_dump_file (pass);
2643 /* If a timevar is present, start it. */
2644 if (pass->tv_id)
2645 timevar_pop (pass->tv_id);
2648 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2649 ipa_write_summaries_2 (pass->sub, state);
2651 pass = pass->next;
2655 /* Helper function of ipa_write_summaries. Creates and destroys the
2656 decl state and calls ipa_write_summaries_2 for all passes that have
2657 summaries. SET is the set of nodes to be written. */
2659 static void
2660 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2662 pass_manager *passes = g->get_passes ();
2663 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2664 state->symtab_node_encoder = encoder;
2666 lto_output_init_mode_table ();
2667 lto_push_out_decl_state (state);
2669 gcc_assert (!flag_wpa);
2670 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2672 write_lto ();
2674 gcc_assert (lto_get_out_decl_state () == state);
2675 lto_pop_out_decl_state ();
2676 lto_delete_out_decl_state (state);
2679 /* Write out summaries for all the nodes in the callgraph. */
2681 void
2682 ipa_write_summaries (void)
2684 lto_symtab_encoder_t encoder;
2685 int i, order_pos;
2686 varpool_node *vnode;
2687 struct cgraph_node *node;
2688 struct cgraph_node **order;
2690 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2691 return;
2693 select_what_to_stream ();
2695 encoder = lto_symtab_encoder_new (false);
2697 /* Create the callgraph set in the same order used in
2698 cgraph_expand_all_functions. This mostly facilitates debugging,
2699 since it causes the gimple file to be processed in the same order
2700 as the source code. */
2701 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2702 order_pos = ipa_reverse_postorder (order);
2703 gcc_assert (order_pos == symtab->cgraph_count);
2705 for (i = order_pos - 1; i >= 0; i--)
2707 struct cgraph_node *node = order[i];
2709 if (node->has_gimple_body_p ())
2711 /* When streaming out references to statements as part of some IPA
2712 pass summary, the statements need to have uids assigned and the
2713 following does that for all the IPA passes here. Naturally, this
2714 ordering then matches the one IPA-passes get in their stmt_fixup
2715 hooks. */
2717 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2718 renumber_gimple_stmt_uids ();
2719 pop_cfun ();
2721 if (node->definition && node->need_lto_streaming)
2722 lto_set_symtab_encoder_in_partition (encoder, node);
2725 FOR_EACH_DEFINED_FUNCTION (node)
2726 if (node->alias && node->need_lto_streaming)
2727 lto_set_symtab_encoder_in_partition (encoder, node);
2728 FOR_EACH_DEFINED_VARIABLE (vnode)
2729 if (vnode->need_lto_streaming)
2730 lto_set_symtab_encoder_in_partition (encoder, vnode);
2732 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2734 free (order);
2737 /* Same as execute_pass_list but assume that subpasses of IPA passes
2738 are local passes. If SET is not NULL, write out optimization summaries of
2739 only those node in SET. */
2741 static void
2742 ipa_write_optimization_summaries_1 (opt_pass *pass,
2743 struct lto_out_decl_state *state)
2745 while (pass)
2747 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2748 gcc_assert (!current_function_decl);
2749 gcc_assert (!cfun);
2750 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2751 if (pass->type == IPA_PASS
2752 && ipa_pass->write_optimization_summary
2753 && pass->gate (cfun))
2755 /* If a timevar is present, start it. */
2756 if (pass->tv_id)
2757 timevar_push (pass->tv_id);
2759 pass_init_dump_file (pass);
2761 current_pass = pass;
2762 ipa_pass->write_optimization_summary ();
2764 pass_fini_dump_file (pass);
2766 /* If a timevar is present, start it. */
2767 if (pass->tv_id)
2768 timevar_pop (pass->tv_id);
2771 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2772 ipa_write_optimization_summaries_1 (pass->sub, state);
2774 pass = pass->next;
2778 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2779 NULL, write out all summaries of all nodes. */
2781 void
2782 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2784 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2785 lto_symtab_encoder_iterator lsei;
2786 state->symtab_node_encoder = encoder;
2788 lto_output_init_mode_table ();
2789 lto_push_out_decl_state (state);
2790 for (lsei = lsei_start_function_in_partition (encoder);
2791 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2793 struct cgraph_node *node = lsei_cgraph_node (lsei);
2794 /* When streaming out references to statements as part of some IPA
2795 pass summary, the statements need to have uids assigned.
2797 For functions newly born at WPA stage we need to initialize
2798 the uids here. */
2799 if (node->definition
2800 && gimple_has_body_p (node->decl))
2802 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2803 renumber_gimple_stmt_uids ();
2804 pop_cfun ();
2808 gcc_assert (flag_wpa);
2809 pass_manager *passes = g->get_passes ();
2810 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2812 write_lto ();
2814 gcc_assert (lto_get_out_decl_state () == state);
2815 lto_pop_out_decl_state ();
2816 lto_delete_out_decl_state (state);
2819 /* Same as execute_pass_list but assume that subpasses of IPA passes
2820 are local passes. */
2822 static void
2823 ipa_read_summaries_1 (opt_pass *pass)
2825 while (pass)
2827 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2829 gcc_assert (!current_function_decl);
2830 gcc_assert (!cfun);
2831 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2833 if (pass->gate (cfun))
2835 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2837 /* If a timevar is present, start it. */
2838 if (pass->tv_id)
2839 timevar_push (pass->tv_id);
2841 pass_init_dump_file (pass);
2843 current_pass = pass;
2844 ipa_pass->read_summary ();
2846 pass_fini_dump_file (pass);
2848 /* Stop timevar. */
2849 if (pass->tv_id)
2850 timevar_pop (pass->tv_id);
2853 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2854 ipa_read_summaries_1 (pass->sub);
2856 pass = pass->next;
2861 /* Read all the summaries for all_regular_ipa_passes. */
2863 void
2864 ipa_read_summaries (void)
2866 pass_manager *passes = g->get_passes ();
2867 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2870 /* Same as execute_pass_list but assume that subpasses of IPA passes
2871 are local passes. */
2873 static void
2874 ipa_read_optimization_summaries_1 (opt_pass *pass)
2876 while (pass)
2878 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2880 gcc_assert (!current_function_decl);
2881 gcc_assert (!cfun);
2882 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2884 if (pass->gate (cfun))
2886 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2888 /* If a timevar is present, start it. */
2889 if (pass->tv_id)
2890 timevar_push (pass->tv_id);
2892 pass_init_dump_file (pass);
2894 current_pass = pass;
2895 ipa_pass->read_optimization_summary ();
2897 pass_fini_dump_file (pass);
2899 /* Stop timevar. */
2900 if (pass->tv_id)
2901 timevar_pop (pass->tv_id);
2904 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2905 ipa_read_optimization_summaries_1 (pass->sub);
2907 pass = pass->next;
2911 /* Read all the summaries for all_regular_ipa_passes. */
2913 void
2914 ipa_read_optimization_summaries (void)
2916 pass_manager *passes = g->get_passes ();
2917 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2920 /* Same as execute_pass_list but assume that subpasses of IPA passes
2921 are local passes. */
2922 void
2923 execute_ipa_pass_list (opt_pass *pass)
2927 gcc_assert (!current_function_decl);
2928 gcc_assert (!cfun);
2929 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2930 if (execute_one_pass (pass) && pass->sub)
2932 if (pass->sub->type == GIMPLE_PASS)
2934 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2935 do_per_function_toporder ((void (*)(function *, void *))
2936 execute_pass_list,
2937 pass->sub);
2938 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2940 else if (pass->sub->type == SIMPLE_IPA_PASS
2941 || pass->sub->type == IPA_PASS)
2942 execute_ipa_pass_list (pass->sub);
2943 else
2944 gcc_unreachable ();
2946 gcc_assert (!current_function_decl);
2947 symtab->process_new_functions ();
2948 pass = pass->next;
2950 while (pass);
2953 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2955 static void
2956 execute_ipa_stmt_fixups (opt_pass *pass,
2957 struct cgraph_node *node, gimple **stmts)
2959 while (pass)
2961 /* Execute all of the IPA_PASSes in the list. */
2962 if (pass->type == IPA_PASS
2963 && pass->gate (cfun))
2965 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2967 if (ipa_pass->stmt_fixup)
2969 pass_init_dump_file (pass);
2970 /* If a timevar is present, start it. */
2971 if (pass->tv_id)
2972 timevar_push (pass->tv_id);
2974 current_pass = pass;
2975 ipa_pass->stmt_fixup (node, stmts);
2977 /* Stop timevar. */
2978 if (pass->tv_id)
2979 timevar_pop (pass->tv_id);
2980 pass_fini_dump_file (pass);
2982 if (pass->sub)
2983 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2985 pass = pass->next;
2989 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2991 void
2992 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2994 pass_manager *passes = g->get_passes ();
2995 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2999 extern void debug_properties (unsigned int);
3000 extern void dump_properties (FILE *, unsigned int);
3002 DEBUG_FUNCTION void
3003 dump_properties (FILE *dump, unsigned int props)
3005 fprintf (dump, "Properties:\n");
3006 if (props & PROP_gimple_any)
3007 fprintf (dump, "PROP_gimple_any\n");
3008 if (props & PROP_gimple_lcf)
3009 fprintf (dump, "PROP_gimple_lcf\n");
3010 if (props & PROP_gimple_leh)
3011 fprintf (dump, "PROP_gimple_leh\n");
3012 if (props & PROP_cfg)
3013 fprintf (dump, "PROP_cfg\n");
3014 if (props & PROP_ssa)
3015 fprintf (dump, "PROP_ssa\n");
3016 if (props & PROP_no_crit_edges)
3017 fprintf (dump, "PROP_no_crit_edges\n");
3018 if (props & PROP_rtl)
3019 fprintf (dump, "PROP_rtl\n");
3020 if (props & PROP_gimple_lomp)
3021 fprintf (dump, "PROP_gimple_lomp\n");
3022 if (props & PROP_gimple_lomp_dev)
3023 fprintf (dump, "PROP_gimple_lomp_dev\n");
3024 if (props & PROP_gimple_lcx)
3025 fprintf (dump, "PROP_gimple_lcx\n");
3026 if (props & PROP_gimple_lvec)
3027 fprintf (dump, "PROP_gimple_lvec\n");
3028 if (props & PROP_cfglayout)
3029 fprintf (dump, "PROP_cfglayout\n");
3032 DEBUG_FUNCTION void
3033 debug_properties (unsigned int props)
3035 dump_properties (stderr, props);
3038 /* Called by local passes to see if function is called by already processed nodes.
3039 Because we process nodes in topological order, this means that function is
3040 in recursive cycle or we introduced new direct calls. */
3041 bool
3042 function_called_by_processed_nodes_p (void)
3044 struct cgraph_edge *e;
3045 for (e = cgraph_node::get (current_function_decl)->callers;
3047 e = e->next_caller)
3049 if (e->caller->decl == current_function_decl)
3050 continue;
3051 if (!e->caller->has_gimple_body_p ())
3052 continue;
3053 if (TREE_ASM_WRITTEN (e->caller->decl))
3054 continue;
3055 if (!e->caller->process && !e->caller->global.inlined_to)
3056 break;
3058 if (dump_file && e)
3060 fprintf (dump_file, "Already processed call to:\n");
3061 e->caller->dump (dump_file);
3063 return e != NULL;
3066 #include "gt-passes.h"