Improve costs for DImode shifts of interger constants.
[official-gcc.git] / gcc / passes.c
blob6ff31ec37d7fd9f21bfa49a3102c112cebd0cbe0
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2020 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 "
91 "to handle the 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 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
123 unsigned int
124 pass_manager::execute_pass_mode_switching ()
126 return pass_mode_switching_1->execute (cfun);
130 /* Call from anywhere to find out what pass this is. Useful for
131 printing out debugging information deep inside an service
132 routine. */
133 void
134 print_current_pass (FILE *file)
136 if (current_pass)
137 fprintf (file, "current pass = %s (%d)\n",
138 current_pass->name, current_pass->static_pass_number);
139 else
140 fprintf (file, "no current pass.\n");
144 /* Call from the debugger to get the current pass name. */
145 DEBUG_FUNCTION void
146 debug_pass (void)
148 print_current_pass (stderr);
153 /* Global variables used to communicate with passes. */
154 bool in_gimple_form;
157 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
158 and TYPE_DECL nodes.
160 This does nothing for local (non-static) variables, unless the
161 variable is a register variable with DECL_ASSEMBLER_NAME set. In
162 that case, or if the variable is not an automatic, it sets up the
163 RTL and outputs any assembler code (label definition, storage
164 allocation and initialization).
166 DECL is the declaration. TOP_LEVEL is nonzero
167 if this declaration is not within a function. */
169 void
170 rest_of_decl_compilation (tree decl,
171 int top_level,
172 int at_end)
174 bool finalize = true;
176 /* We deferred calling assemble_alias so that we could collect
177 other attributes such as visibility. Emit the alias now. */
178 if (!in_lto_p)
180 tree alias;
181 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
182 if (alias)
184 alias = TREE_VALUE (TREE_VALUE (alias));
185 alias = get_identifier (TREE_STRING_POINTER (alias));
186 /* A quirk of the initial implementation of aliases required that the
187 user add "extern" to all of them. Which is silly, but now
188 historical. Do note that the symbol is in fact locally defined. */
189 DECL_EXTERNAL (decl) = 0;
190 TREE_STATIC (decl) = 1;
191 assemble_alias (decl, alias);
192 finalize = false;
196 /* Can't defer this, because it needs to happen before any
197 later function definitions are processed. */
198 if (HAS_DECL_ASSEMBLER_NAME_P (decl)
199 && DECL_ASSEMBLER_NAME_SET_P (decl)
200 && 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_combine_1->static_pass_number, NULL);
365 print_combine_total_stats ();
366 dumps->dump_finish (pass_combine_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_local_optimization_passes =
435 SIMPLE_IPA_PASS, /* type */
436 "opt_local_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_local_optimization_passes : public simple_ipa_opt_pass
448 public:
449 pass_local_optimization_passes (gcc::context *ctxt)
450 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
453 /* opt_pass methods: */
454 virtual bool gate (function *)
456 /* Don't bother doing anything if the program has errors. */
457 return (!seen_error () && !in_lto_p);
460 }; // class pass_local_optimization_passes
462 const pass_data pass_data_ipa_remove_symbols =
464 SIMPLE_IPA_PASS, /* type */
465 "remove_symbols", /* name */
466 OPTGROUP_NONE, /* optinfo_flags */
467 TV_NONE, /* tv_id */
468 0, /* properties_required */
469 0, /* properties_provided */
470 0, /* properties_destroyed */
471 0, /* todo_flags_start */
472 TODO_remove_functions | TODO_dump_symtab, /* todo_flags_finish */
475 class pass_ipa_remove_symbols : public simple_ipa_opt_pass
477 public:
478 pass_ipa_remove_symbols (gcc::context *ctxt)
479 : simple_ipa_opt_pass (pass_data_ipa_remove_symbols, ctxt)
482 /* opt_pass methods: */
483 virtual bool gate (function *)
485 /* Don't bother doing anything if the program has errors. */
486 return (!seen_error () && !in_lto_p);
489 }; // class pass_local_optimization_passes
491 } // anon namespace
493 simple_ipa_opt_pass *
494 make_pass_build_ssa_passes (gcc::context *ctxt)
496 return new pass_build_ssa_passes (ctxt);
499 simple_ipa_opt_pass *
500 make_pass_local_optimization_passes (gcc::context *ctxt)
502 return new pass_local_optimization_passes (ctxt);
505 simple_ipa_opt_pass *
506 make_pass_ipa_remove_symbols (gcc::context *ctxt)
508 return new pass_ipa_remove_symbols (ctxt);
511 namespace {
513 const pass_data pass_data_all_early_optimizations =
515 GIMPLE_PASS, /* type */
516 "early_optimizations", /* name */
517 OPTGROUP_NONE, /* optinfo_flags */
518 TV_NONE, /* tv_id */
519 0, /* properties_required */
520 0, /* properties_provided */
521 0, /* properties_destroyed */
522 0, /* todo_flags_start */
523 0, /* todo_flags_finish */
526 class pass_all_early_optimizations : public gimple_opt_pass
528 public:
529 pass_all_early_optimizations (gcc::context *ctxt)
530 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
533 /* opt_pass methods: */
534 virtual bool gate (function *)
536 return (optimize >= 1
537 /* Don't bother doing anything if the program has errors. */
538 && !seen_error ());
541 }; // class pass_all_early_optimizations
543 } // anon namespace
545 static gimple_opt_pass *
546 make_pass_all_early_optimizations (gcc::context *ctxt)
548 return new pass_all_early_optimizations (ctxt);
551 namespace {
553 const pass_data pass_data_all_optimizations =
555 GIMPLE_PASS, /* type */
556 "*all_optimizations", /* name */
557 OPTGROUP_NONE, /* optinfo_flags */
558 TV_OPTIMIZE, /* tv_id */
559 0, /* properties_required */
560 0, /* properties_provided */
561 0, /* properties_destroyed */
562 0, /* todo_flags_start */
563 0, /* todo_flags_finish */
566 class pass_all_optimizations : public gimple_opt_pass
568 public:
569 pass_all_optimizations (gcc::context *ctxt)
570 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
573 /* opt_pass methods: */
574 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
576 }; // class pass_all_optimizations
578 } // anon namespace
580 static gimple_opt_pass *
581 make_pass_all_optimizations (gcc::context *ctxt)
583 return new pass_all_optimizations (ctxt);
586 namespace {
588 const pass_data pass_data_all_optimizations_g =
590 GIMPLE_PASS, /* type */
591 "*all_optimizations_g", /* name */
592 OPTGROUP_NONE, /* optinfo_flags */
593 TV_OPTIMIZE, /* tv_id */
594 0, /* properties_required */
595 0, /* properties_provided */
596 0, /* properties_destroyed */
597 0, /* todo_flags_start */
598 0, /* todo_flags_finish */
601 class pass_all_optimizations_g : public gimple_opt_pass
603 public:
604 pass_all_optimizations_g (gcc::context *ctxt)
605 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
608 /* opt_pass methods: */
609 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
611 }; // class pass_all_optimizations_g
613 } // anon namespace
615 static gimple_opt_pass *
616 make_pass_all_optimizations_g (gcc::context *ctxt)
618 return new pass_all_optimizations_g (ctxt);
621 namespace {
623 const pass_data pass_data_rest_of_compilation =
625 RTL_PASS, /* type */
626 "*rest_of_compilation", /* name */
627 OPTGROUP_NONE, /* optinfo_flags */
628 TV_REST_OF_COMPILATION, /* tv_id */
629 PROP_rtl, /* properties_required */
630 0, /* properties_provided */
631 0, /* properties_destroyed */
632 0, /* todo_flags_start */
633 0, /* todo_flags_finish */
636 class pass_rest_of_compilation : public rtl_opt_pass
638 public:
639 pass_rest_of_compilation (gcc::context *ctxt)
640 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
643 /* opt_pass methods: */
644 virtual bool gate (function *)
646 /* Early return if there were errors. We can run afoul of our
647 consistency checks, and there's not really much point in fixing them. */
648 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
651 }; // class pass_rest_of_compilation
653 } // anon namespace
655 static rtl_opt_pass *
656 make_pass_rest_of_compilation (gcc::context *ctxt)
658 return new pass_rest_of_compilation (ctxt);
661 namespace {
663 const pass_data pass_data_postreload =
665 RTL_PASS, /* type */
666 "*all-postreload", /* name */
667 OPTGROUP_NONE, /* optinfo_flags */
668 TV_POSTRELOAD, /* tv_id */
669 PROP_rtl, /* properties_required */
670 0, /* properties_provided */
671 0, /* properties_destroyed */
672 0, /* todo_flags_start */
673 0, /* todo_flags_finish */
676 class pass_postreload : public rtl_opt_pass
678 public:
679 pass_postreload (gcc::context *ctxt)
680 : rtl_opt_pass (pass_data_postreload, ctxt)
683 /* opt_pass methods: */
684 virtual bool gate (function *) { return reload_completed; }
686 }; // class pass_postreload
688 } // anon namespace
690 static rtl_opt_pass *
691 make_pass_postreload (gcc::context *ctxt)
693 return new pass_postreload (ctxt);
696 namespace {
698 const pass_data pass_data_late_compilation =
700 RTL_PASS, /* type */
701 "*all-late_compilation", /* name */
702 OPTGROUP_NONE, /* optinfo_flags */
703 TV_LATE_COMPILATION, /* tv_id */
704 PROP_rtl, /* properties_required */
705 0, /* properties_provided */
706 0, /* properties_destroyed */
707 0, /* todo_flags_start */
708 0, /* todo_flags_finish */
711 class pass_late_compilation : public rtl_opt_pass
713 public:
714 pass_late_compilation (gcc::context *ctxt)
715 : rtl_opt_pass (pass_data_late_compilation, ctxt)
718 /* opt_pass methods: */
719 virtual bool gate (function *)
721 return reload_completed || targetm.no_register_allocation;
724 }; // class pass_late_compilation
726 } // anon namespace
728 static rtl_opt_pass *
729 make_pass_late_compilation (gcc::context *ctxt)
731 return new pass_late_compilation (ctxt);
736 /* Set the static pass number of pass PASS to ID and record that
737 in the mapping from static pass number to pass. */
739 void
740 pass_manager::
741 set_pass_for_id (int id, opt_pass *pass)
743 pass->static_pass_number = id;
744 if (passes_by_id_size <= id)
746 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
747 memset (passes_by_id + passes_by_id_size, 0,
748 (id + 1 - passes_by_id_size) * sizeof (void *));
749 passes_by_id_size = id + 1;
751 passes_by_id[id] = pass;
754 /* Return the pass with the static pass number ID. */
756 opt_pass *
757 pass_manager::get_pass_for_id (int id) const
759 if (id >= passes_by_id_size)
760 return NULL;
761 return passes_by_id[id];
764 /* Iterate over the pass tree allocating dump file numbers. We want
765 to do this depth first, and independent of whether the pass is
766 enabled or not. */
768 void
769 register_one_dump_file (opt_pass *pass)
771 g->get_passes ()->register_one_dump_file (pass);
774 void
775 pass_manager::register_one_dump_file (opt_pass *pass)
777 char *dot_name, *flag_name, *glob_name;
778 const char *name, *full_name, *prefix;
780 /* Buffer big enough to format a 32-bit UINT_MAX into. */
781 char num[11];
782 dump_kind dkind;
783 int id;
784 optgroup_flags_t optgroup_flags = OPTGROUP_NONE;
785 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
787 /* See below in next_pass_1. */
788 num[0] = '\0';
789 if (pass->static_pass_number != -1)
790 sprintf (num, "%u", ((int) pass->static_pass_number < 0
791 ? 1 : pass->static_pass_number));
793 /* The name is both used to identify the pass for the purposes of plugins,
794 and to specify dump file name and option.
795 The latter two might want something short which is not quite unique; for
796 that reason, we may have a disambiguating prefix, followed by a space
797 to mark the start of the following dump file name / option string. */
798 name = strchr (pass->name, ' ');
799 name = name ? name + 1 : pass->name;
800 dot_name = concat (".", name, num, NULL);
801 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
803 prefix = "ipa-";
804 dkind = DK_ipa;
805 optgroup_flags |= OPTGROUP_IPA;
807 else if (pass->type == GIMPLE_PASS)
809 prefix = "tree-";
810 dkind = DK_tree;
812 else
814 prefix = "rtl-";
815 dkind = DK_rtl;
818 flag_name = concat (prefix, name, num, NULL);
819 glob_name = concat (prefix, name, NULL);
820 optgroup_flags |= pass->optinfo_flags;
821 /* For any passes that do not have an optgroup set, and which are not
822 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
823 any dump messages are emitted properly under -fopt-info(-optall). */
824 if (optgroup_flags == OPTGROUP_NONE)
825 optgroup_flags = OPTGROUP_OTHER;
826 id = dumps->dump_register (dot_name, flag_name, glob_name, dkind,
827 optgroup_flags,
828 true);
829 set_pass_for_id (id, pass);
830 full_name = concat (prefix, pass->name, num, NULL);
831 register_pass_name (pass, full_name);
832 free (CONST_CAST (char *, full_name));
835 /* Register the dump files for the pass_manager starting at PASS. */
837 void
838 pass_manager::register_dump_files (opt_pass *pass)
842 if (pass->name && pass->name[0] != '*')
843 register_one_dump_file (pass);
845 if (pass->sub)
846 register_dump_files (pass->sub);
848 pass = pass->next;
850 while (pass);
853 /* Register PASS with NAME. */
855 void
856 pass_manager::register_pass_name (opt_pass *pass, const char *name)
858 if (!m_name_to_pass_map)
859 m_name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
861 if (m_name_to_pass_map->get (name))
862 return; /* Ignore plugin passes. */
864 const char *unique_name = xstrdup (name);
865 m_name_to_pass_map->put (unique_name, pass);
868 /* Map from pass id to canonicalized pass name. */
870 typedef const char *char_ptr;
871 static vec<char_ptr> pass_tab;
873 /* Callback function for traversing NAME_TO_PASS_MAP. */
875 bool
876 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
878 gcc_assert (pass->static_pass_number > 0);
879 gcc_assert (pass_tab.exists ());
881 pass_tab[pass->static_pass_number] = name;
883 return 1;
886 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
887 table for dumping purpose. */
889 void
890 pass_manager::create_pass_tab (void) const
892 if (!flag_dump_passes)
893 return;
895 pass_tab.safe_grow_cleared (passes_by_id_size + 1, true);
896 m_name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
899 static bool override_gate_status (opt_pass *, tree, bool);
901 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
902 is turned on or not. */
904 static void
905 dump_one_pass (opt_pass *pass, int pass_indent)
907 int indent = 3 * pass_indent;
908 const char *pn;
909 bool is_on, is_really_on;
911 is_on = pass->gate (cfun);
912 is_really_on = override_gate_status (pass, current_function_decl, is_on);
914 if (pass->static_pass_number <= 0)
915 pn = pass->name;
916 else
917 pn = pass_tab[pass->static_pass_number];
919 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
920 (15 - indent < 0 ? 0 : 15 - indent), " ",
921 is_on ? " ON" : " OFF",
922 ((!is_on) == (!is_really_on) ? ""
923 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
926 /* Dump pass list PASS with indentation INDENT. */
928 static void
929 dump_pass_list (opt_pass *pass, int indent)
933 dump_one_pass (pass, indent);
934 if (pass->sub)
935 dump_pass_list (pass->sub, indent + 1);
936 pass = pass->next;
938 while (pass);
941 /* Dump all optimization passes. */
943 void
944 dump_passes (void)
946 g->get_passes ()->dump_passes ();
949 void
950 pass_manager::dump_passes () const
952 push_dummy_function (true);
953 cgraph_node *node = cgraph_node::get_create (current_function_decl);
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 node->remove ();
964 pop_dummy_function ();
967 /* Returns the pass with NAME. */
969 opt_pass *
970 pass_manager::get_pass_by_name (const char *name)
972 opt_pass **p = m_name_to_pass_map->get (name);
973 if (p)
974 return *p;
976 return NULL;
980 /* Range [start, last]. */
982 struct uid_range
984 unsigned int start;
985 unsigned int last;
986 const char *assem_name;
987 struct uid_range *next;
990 typedef struct uid_range *uid_range_p;
993 static vec<uid_range_p> enabled_pass_uid_range_tab;
994 static vec<uid_range_p> disabled_pass_uid_range_tab;
997 /* Parse option string for -fdisable- and -fenable-
998 The syntax of the options:
1000 -fenable-<pass_name>
1001 -fdisable-<pass_name>
1003 -fenable-<pass_name>=s1:e1,s2:e2,...
1004 -fdisable-<pass_name>=s1:e1,s2:e2,...
1007 static void
1008 enable_disable_pass (const char *arg, bool is_enable)
1010 opt_pass *pass;
1011 char *range_str, *phase_name;
1012 char *argstr = xstrdup (arg);
1013 vec<uid_range_p> *tab = 0;
1015 range_str = strchr (argstr,'=');
1016 if (range_str)
1018 *range_str = '\0';
1019 range_str++;
1022 phase_name = argstr;
1023 if (!*phase_name)
1025 if (is_enable)
1026 error ("unrecognized option %<-fenable%>");
1027 else
1028 error ("unrecognized option %<-fdisable%>");
1029 free (argstr);
1030 return;
1032 pass = g->get_passes ()->get_pass_by_name (phase_name);
1033 if (!pass || pass->static_pass_number == -1)
1035 if (is_enable)
1036 error ("unknown pass %s specified in %<-fenable%>", phase_name);
1037 else
1038 error ("unknown pass %s specified in %<-fdisable%>", phase_name);
1039 free (argstr);
1040 return;
1043 if (is_enable)
1044 tab = &enabled_pass_uid_range_tab;
1045 else
1046 tab = &disabled_pass_uid_range_tab;
1048 if ((unsigned) pass->static_pass_number >= tab->length ())
1049 tab->safe_grow_cleared (pass->static_pass_number + 1, true);
1051 if (!range_str)
1053 uid_range_p slot;
1054 uid_range_p new_range = XCNEW (struct uid_range);
1056 new_range->start = 0;
1057 new_range->last = (unsigned)-1;
1059 slot = (*tab)[pass->static_pass_number];
1060 new_range->next = slot;
1061 (*tab)[pass->static_pass_number] = new_range;
1062 if (is_enable)
1063 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1064 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1065 else
1066 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1067 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1069 else
1071 char *next_range = NULL;
1072 char *one_range = range_str;
1073 char *end_val = NULL;
1077 uid_range_p slot;
1078 uid_range_p new_range;
1079 char *invalid = NULL;
1080 long start;
1081 char *func_name = NULL;
1083 next_range = strchr (one_range, ',');
1084 if (next_range)
1086 *next_range = '\0';
1087 next_range++;
1090 end_val = strchr (one_range, ':');
1091 if (end_val)
1093 *end_val = '\0';
1094 end_val++;
1096 start = strtol (one_range, &invalid, 10);
1097 if (*invalid || start < 0)
1099 if (end_val || (one_range[0] >= '0'
1100 && one_range[0] <= '9'))
1102 error ("Invalid range %s in option %s",
1103 one_range,
1104 is_enable ? "-fenable" : "-fdisable");
1105 free (argstr);
1106 return;
1108 func_name = one_range;
1110 if (!end_val)
1112 new_range = XCNEW (struct uid_range);
1113 if (!func_name)
1115 new_range->start = (unsigned) start;
1116 new_range->last = (unsigned) start;
1118 else
1120 new_range->start = (unsigned) -1;
1121 new_range->last = (unsigned) -1;
1122 new_range->assem_name = xstrdup (func_name);
1125 else
1127 long last = strtol (end_val, &invalid, 10);
1128 if (*invalid || last < start)
1130 error ("Invalid range %s in option %s",
1131 end_val,
1132 is_enable ? "-fenable" : "-fdisable");
1133 free (argstr);
1134 return;
1136 new_range = XCNEW (struct uid_range);
1137 new_range->start = (unsigned) start;
1138 new_range->last = (unsigned) last;
1141 slot = (*tab)[pass->static_pass_number];
1142 new_range->next = slot;
1143 (*tab)[pass->static_pass_number] = new_range;
1144 if (is_enable)
1146 if (new_range->assem_name)
1147 inform (UNKNOWN_LOCATION,
1148 "enable pass %s for function %s",
1149 phase_name, new_range->assem_name);
1150 else
1151 inform (UNKNOWN_LOCATION,
1152 "enable pass %s for functions in the range of [%u, %u]",
1153 phase_name, new_range->start, new_range->last);
1155 else
1157 if (new_range->assem_name)
1158 inform (UNKNOWN_LOCATION,
1159 "disable pass %s for function %s",
1160 phase_name, new_range->assem_name);
1161 else
1162 inform (UNKNOWN_LOCATION,
1163 "disable pass %s for functions in the range of [%u, %u]",
1164 phase_name, new_range->start, new_range->last);
1167 one_range = next_range;
1168 } while (next_range);
1171 free (argstr);
1174 /* Enable pass specified by ARG. */
1176 void
1177 enable_pass (const char *arg)
1179 enable_disable_pass (arg, true);
1182 /* Disable pass specified by ARG. */
1184 void
1185 disable_pass (const char *arg)
1187 enable_disable_pass (arg, false);
1190 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1192 static bool
1193 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1194 tree func,
1195 vec<uid_range_p> tab)
1197 uid_range_p slot, range;
1198 int cgraph_uid;
1199 const char *aname = NULL;
1201 if (!tab.exists ()
1202 || (unsigned) pass->static_pass_number >= tab.length ()
1203 || pass->static_pass_number == -1)
1204 return false;
1206 slot = tab[pass->static_pass_number];
1207 if (!slot)
1208 return false;
1210 cgraph_uid = func ? cgraph_node::get (func)->get_uid () : 0;
1211 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1212 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1214 range = slot;
1215 while (range)
1217 if ((unsigned) cgraph_uid >= range->start
1218 && (unsigned) cgraph_uid <= range->last)
1219 return true;
1220 if (range->assem_name && aname
1221 && !strcmp (range->assem_name, aname))
1222 return true;
1223 range = range->next;
1226 return false;
1230 /* Update static_pass_number for passes (and the flag
1231 TODO_mark_first_instance).
1233 Passes are constructed with static_pass_number preinitialized to 0
1235 This field is used in two different ways: initially as instance numbers
1236 of their kind, and then as ids within the entire pass manager.
1238 Within pass_manager::pass_manager:
1240 * In add_pass_instance(), as called by next_pass_1 in
1241 NEXT_PASS in init_optimization_passes
1243 * When the initial instance of a pass within a pass manager is seen,
1244 it is flagged, and its static_pass_number is set to -1
1246 * On subsequent times that it is seen, the static pass number
1247 is decremented each time, so that if there are e.g. 4 dups,
1248 they have static_pass_number -4, 2, 3, 4 respectively (note
1249 how the initial one is negative and gives the count); these
1250 can be thought of as instance numbers of the specific pass
1252 * Within the register_dump_files () traversal, set_pass_for_id()
1253 is called on each pass, using these instance numbers to create
1254 dumpfile switches, and then overwriting them with a pass id,
1255 which are global to the whole pass manager (based on
1256 (TDI_end + current value of extra_dump_files_in_use) ) */
1258 static void
1259 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1260 opt_pass *initial_pass)
1262 /* Are we dealing with the first pass of its kind, or a clone? */
1263 if (new_pass != initial_pass)
1265 /* We're dealing with a clone. */
1266 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1268 /* Indicate to register_dump_files that this pass has duplicates,
1269 and so it should rename the dump file. The first instance will
1270 be -1, and be number of duplicates = -static_pass_number - 1.
1271 Subsequent instances will be > 0 and just the duplicate number. */
1272 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1274 initial_pass->static_pass_number -= 1;
1275 new_pass->static_pass_number = -initial_pass->static_pass_number;
1278 else
1280 /* We're dealing with the first pass of its kind. */
1281 new_pass->todo_flags_start |= TODO_mark_first_instance;
1282 new_pass->static_pass_number = -1;
1284 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1288 /* Add a pass to the pass list. Duplicate the pass if it's already
1289 in the list. */
1291 static opt_pass **
1292 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1294 /* Every pass should have a name so that plugins can refer to them. */
1295 gcc_assert (pass->name != NULL);
1297 add_pass_instance (pass, false, initial_pass);
1298 *list = pass;
1300 return &(*list)->next;
1303 /* List node for an inserted pass instance. We need to keep track of all
1304 the newly-added pass instances (with 'added_pass_nodes' defined below)
1305 so that we can register their dump files after pass-positioning is finished.
1306 Registering dumping files needs to be post-processed or the
1307 static_pass_number of the opt_pass object would be modified and mess up
1308 the dump file names of future pass instances to be added. */
1310 struct pass_list_node
1312 opt_pass *pass;
1313 struct pass_list_node *next;
1316 static struct pass_list_node *added_pass_nodes = NULL;
1317 static struct pass_list_node *prev_added_pass_node;
1319 /* Insert the pass at the proper position. Return true if the pass
1320 is successfully added.
1322 NEW_PASS_INFO - new pass to be inserted
1323 PASS_LIST - root of the pass list to insert the new pass to */
1325 static bool
1326 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1328 opt_pass *pass = *pass_list, *prev_pass = NULL;
1329 bool success = false;
1331 for ( ; pass; prev_pass = pass, pass = pass->next)
1333 /* Check if the current pass is of the same type as the new pass and
1334 matches the name and the instance number of the reference pass. */
1335 if (pass->type == new_pass_info->pass->type
1336 && pass->name
1337 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1338 && ((new_pass_info->ref_pass_instance_number == 0)
1339 || (new_pass_info->ref_pass_instance_number ==
1340 pass->static_pass_number)
1341 || (new_pass_info->ref_pass_instance_number == 1
1342 && pass->todo_flags_start & TODO_mark_first_instance)))
1344 opt_pass *new_pass;
1345 struct pass_list_node *new_pass_node;
1347 if (new_pass_info->ref_pass_instance_number == 0)
1349 new_pass = new_pass_info->pass->clone ();
1350 add_pass_instance (new_pass, true, new_pass_info->pass);
1352 else
1354 new_pass = new_pass_info->pass;
1355 add_pass_instance (new_pass, true, new_pass);
1358 /* Insert the new pass instance based on the positioning op. */
1359 switch (new_pass_info->pos_op)
1361 case PASS_POS_INSERT_AFTER:
1362 new_pass->next = pass->next;
1363 pass->next = new_pass;
1365 /* Skip newly inserted pass to avoid repeated
1366 insertions in the case where the new pass and the
1367 existing one have the same name. */
1368 pass = new_pass;
1369 break;
1370 case PASS_POS_INSERT_BEFORE:
1371 new_pass->next = pass;
1372 if (prev_pass)
1373 prev_pass->next = new_pass;
1374 else
1375 *pass_list = new_pass;
1376 break;
1377 case PASS_POS_REPLACE:
1378 new_pass->next = pass->next;
1379 if (prev_pass)
1380 prev_pass->next = new_pass;
1381 else
1382 *pass_list = new_pass;
1383 new_pass->sub = pass->sub;
1384 new_pass->tv_id = pass->tv_id;
1385 pass = new_pass;
1386 break;
1387 default:
1388 error ("invalid pass positioning operation");
1389 return false;
1392 /* Save the newly added pass (instance) in the added_pass_nodes
1393 list so that we can register its dump file later. Note that
1394 we cannot register the dump file now because doing so will modify
1395 the static_pass_number of the opt_pass object and therefore
1396 mess up the dump file name of future instances. */
1397 new_pass_node = XCNEW (struct pass_list_node);
1398 new_pass_node->pass = new_pass;
1399 if (!added_pass_nodes)
1400 added_pass_nodes = new_pass_node;
1401 else
1402 prev_added_pass_node->next = new_pass_node;
1403 prev_added_pass_node = new_pass_node;
1405 success = true;
1408 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1409 success = true;
1412 return success;
1415 /* Hooks a new pass into the pass lists.
1417 PASS_INFO - pass information that specifies the opt_pass object,
1418 reference pass, instance number, and how to position
1419 the pass */
1421 void
1422 register_pass (struct register_pass_info *pass_info)
1424 g->get_passes ()->register_pass (pass_info);
1427 void
1428 register_pass (opt_pass* pass, pass_positioning_ops pos,
1429 const char* ref_pass_name, int ref_pass_inst_number)
1431 register_pass_info i;
1432 i.pass = pass;
1433 i.reference_pass_name = ref_pass_name;
1434 i.ref_pass_instance_number = ref_pass_inst_number;
1435 i.pos_op = pos;
1437 g->get_passes ()->register_pass (&i);
1440 void
1441 pass_manager::register_pass (struct register_pass_info *pass_info)
1443 bool all_instances, success;
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 While doing so, we also delete the pass_list_node
1482 objects created during pass positioning. */
1483 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1484 while (added_pass_nodes)
1486 struct pass_list_node *next_node = added_pass_nodes->next;
1488 /* Handle -fdump-* and -fopt-info. */
1489 dumps->register_pass (added_pass_nodes->pass);
1491 XDELETE (added_pass_nodes);
1492 added_pass_nodes = next_node;
1496 /* Construct the pass tree. The sequencing of passes is driven by
1497 the cgraph routines:
1499 finalize_compilation_unit ()
1500 for each node N in the cgraph
1501 cgraph_analyze_function (N)
1502 cgraph_lower_function (N) -> all_lowering_passes
1504 If we are optimizing, compile is then invoked:
1506 compile ()
1507 ipa_passes () -> all_small_ipa_passes
1508 -> Analysis of all_regular_ipa_passes
1509 * possible LTO streaming at copmilation time *
1510 -> Execution of all_regular_ipa_passes
1511 * possible LTO streaming at link time *
1512 -> all_late_ipa_passes
1513 expand_all_functions ()
1514 for each node N in the cgraph
1515 expand_function (N) -> Transformation of all_regular_ipa_passes
1516 -> all_passes
1519 pass_manager::pass_manager (context *ctxt)
1520 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1521 all_regular_ipa_passes (NULL),
1522 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1523 m_ctxt (ctxt), m_name_to_pass_map (NULL)
1525 opt_pass **p;
1527 /* Zero-initialize pass members. */
1528 #define INSERT_PASSES_AFTER(PASS)
1529 #define PUSH_INSERT_PASSES_WITHIN(PASS)
1530 #define POP_INSERT_PASSES()
1531 #define NEXT_PASS(PASS, NUM) PASS ## _ ## NUM = NULL
1532 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) NEXT_PASS (PASS, NUM)
1533 #define TERMINATE_PASS_LIST(PASS)
1534 #include "pass-instances.def"
1535 #undef INSERT_PASSES_AFTER
1536 #undef PUSH_INSERT_PASSES_WITHIN
1537 #undef POP_INSERT_PASSES
1538 #undef NEXT_PASS
1539 #undef NEXT_PASS_WITH_ARG
1540 #undef TERMINATE_PASS_LIST
1542 /* Initialize the pass_lists array. */
1543 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1544 GCC_PASS_LISTS
1545 #undef DEF_PASS_LIST
1547 /* Build the tree of passes. */
1549 #define INSERT_PASSES_AFTER(PASS) \
1551 opt_pass **p_start; \
1552 p_start = p = &(PASS);
1554 #define TERMINATE_PASS_LIST(PASS) \
1555 gcc_assert (p_start == &PASS); \
1556 *p = NULL; \
1559 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1561 opt_pass **p = &(PASS ## _1)->sub;
1563 #define POP_INSERT_PASSES() \
1566 #define NEXT_PASS(PASS, NUM) \
1567 do { \
1568 gcc_assert (PASS ## _ ## NUM == NULL); \
1569 if ((NUM) == 1) \
1570 PASS ## _1 = make_##PASS (m_ctxt); \
1571 else \
1573 gcc_assert (PASS ## _1); \
1574 PASS ## _ ## NUM = PASS ## _1->clone (); \
1576 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1577 } while (0)
1579 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) \
1580 do { \
1581 NEXT_PASS (PASS, NUM); \
1582 PASS ## _ ## NUM->set_pass_param (0, ARG); \
1583 } while (0)
1585 #include "pass-instances.def"
1587 #undef INSERT_PASSES_AFTER
1588 #undef PUSH_INSERT_PASSES_WITHIN
1589 #undef POP_INSERT_PASSES
1590 #undef NEXT_PASS
1591 #undef NEXT_PASS_WITH_ARG
1592 #undef TERMINATE_PASS_LIST
1594 /* Register the passes with the tree dump code. */
1595 register_dump_files (all_lowering_passes);
1596 register_dump_files (all_small_ipa_passes);
1597 register_dump_files (all_regular_ipa_passes);
1598 register_dump_files (all_late_ipa_passes);
1599 register_dump_files (all_passes);
1602 static void
1603 delete_pass_tree (opt_pass *pass)
1605 while (pass)
1607 /* Recurse into child passes. */
1608 delete_pass_tree (pass->sub);
1610 opt_pass *next = pass->next;
1612 /* Delete this pass. */
1613 delete pass;
1615 /* Iterate onto sibling passes. */
1616 pass = next;
1620 pass_manager::~pass_manager ()
1622 XDELETEVEC (passes_by_id);
1624 /* Call delete_pass_tree on each of the pass_lists. */
1625 #define DEF_PASS_LIST(LIST) \
1626 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1627 GCC_PASS_LISTS
1628 #undef DEF_PASS_LIST
1632 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1633 function CALLBACK for every function in the call graph. Otherwise,
1634 call CALLBACK on the current function. */
1636 static void
1637 do_per_function (void (*callback) (function *, void *data), void *data)
1639 if (current_function_decl)
1640 callback (cfun, data);
1641 else
1643 struct cgraph_node *node;
1644 FOR_EACH_DEFINED_FUNCTION (node)
1645 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1646 && (!node->clone_of || node->decl != node->clone_of->decl))
1647 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1651 /* Hook called when NODE is removed and therefore should be
1652 excluded from order vector. DATA is a hash set with removed nodes. */
1654 static void
1655 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1657 hash_set<cgraph_node *> *removed_nodes = (hash_set<cgraph_node *> *)data;
1658 removed_nodes->add (node);
1661 /* Hook called when NODE is insert and therefore should be
1662 excluded from removed_nodes. DATA is a hash set with removed nodes. */
1664 static void
1665 insert_cgraph_node_to_order (cgraph_node *node, void *data)
1667 hash_set<cgraph_node *> *removed_nodes = (hash_set<cgraph_node *> *)data;
1668 removed_nodes->remove (node);
1671 /* Hook called when NODE is duplicated and therefore should be
1672 excluded from removed_nodes. DATA is a hash set with removed nodes. */
1674 static void
1675 duplicate_cgraph_node_to_order (cgraph_node *node, cgraph_node *node2,
1676 void *data)
1678 hash_set<cgraph_node *> *removed_nodes = (hash_set<cgraph_node *> *)data;
1679 gcc_checking_assert (!removed_nodes->contains (node));
1680 removed_nodes->remove (node2);
1684 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1685 function CALLBACK for every function in the call graph. Otherwise,
1686 call CALLBACK on the current function.
1687 This function is global so that plugins can use it. */
1688 void
1689 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1691 int i;
1693 if (current_function_decl)
1694 callback (cfun, data);
1695 else
1697 hash_set<cgraph_node *> removed_nodes;
1698 unsigned nnodes = symtab->cgraph_count;
1699 cgraph_node **order = XNEWVEC (cgraph_node *, nnodes);
1701 nnodes = ipa_reverse_postorder (order);
1702 for (i = nnodes - 1; i >= 0; i--)
1703 order[i]->process = 1;
1704 cgraph_node_hook_list *removal_hook
1705 = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1706 &removed_nodes);
1707 cgraph_node_hook_list *insertion_hook
1708 = symtab->add_cgraph_insertion_hook (insert_cgraph_node_to_order,
1709 &removed_nodes);
1710 cgraph_2node_hook_list *duplication_hook
1711 = symtab->add_cgraph_duplication_hook (duplicate_cgraph_node_to_order,
1712 &removed_nodes);
1713 for (i = nnodes - 1; i >= 0; i--)
1715 cgraph_node *node = order[i];
1717 /* Function could be inlined and removed as unreachable. */
1718 if (node == NULL || removed_nodes.contains (node))
1719 continue;
1721 node->process = 0;
1722 if (node->has_gimple_body_p ())
1724 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1725 push_cfun (fn);
1726 callback (fn, data);
1727 pop_cfun ();
1730 symtab->remove_cgraph_removal_hook (removal_hook);
1731 symtab->remove_cgraph_insertion_hook (insertion_hook);
1732 symtab->remove_cgraph_duplication_hook (duplication_hook);
1734 free (order);
1738 /* Helper function to perform function body dump. */
1740 static void
1741 execute_function_dump (function *fn, void *data)
1743 opt_pass *pass = (opt_pass *)data;
1745 if (dump_file)
1747 push_cfun (fn);
1749 if (fn->curr_properties & PROP_trees)
1750 dump_function_to_file (fn->decl, dump_file, dump_flags);
1751 else
1752 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1754 /* Flush the file. If verification fails, we won't be able to
1755 close the file before aborting. */
1756 fflush (dump_file);
1758 if ((fn->curr_properties & PROP_cfg)
1759 && (dump_flags & TDF_GRAPH))
1761 gcc::dump_manager *dumps = g->get_dumps ();
1762 struct dump_file_info *dfi
1763 = dumps->get_dump_file_info (pass->static_pass_number);
1764 if (!dfi->graph_dump_initialized)
1766 clean_graph_dump_file (dump_file_name);
1767 dfi->graph_dump_initialized = true;
1769 print_graph_cfg (dump_file_name, fn);
1772 pop_cfun ();
1776 /* This function is called when an internal compiler error is encountered.
1777 Ensure that function dump is made available before compiler is aborted. */
1779 void
1780 emergency_dump_function ()
1782 if (!current_pass)
1783 return;
1784 enum opt_pass_type pt = current_pass->type;
1785 fnotice (stderr, "during %s pass: %s\n",
1786 pt == GIMPLE_PASS ? "GIMPLE" : pt == RTL_PASS ? "RTL" : "IPA",
1787 current_pass->name);
1788 if (!dump_file || !cfun)
1789 return;
1790 fnotice (stderr, "dump file: %s\n", dump_file_name);
1791 fprintf (dump_file, "\n\n\nEMERGENCY DUMP:\n\n");
1792 execute_function_dump (cfun, current_pass);
1795 static struct profile_record *profile_record;
1797 /* Do profile consistency book-keeping for the pass with static number INDEX.
1798 RUN is true if the pass really runs, or FALSE
1799 if we are only book-keeping on passes that may have selectively disabled
1800 themselves on a given function. */
1802 static void
1803 check_profile_consistency (int index, bool run)
1805 pass_manager *passes = g->get_passes ();
1806 if (index == -1)
1807 return;
1808 if (!profile_record)
1809 profile_record = XCNEWVEC (struct profile_record,
1810 passes->passes_by_id_size);
1811 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1812 profile_record[index].run |= run;
1813 profile_record_check_consistency (&profile_record[index]);
1816 /* Account profile the pass with static number INDEX.
1817 RUN is true if the pass really runs, or FALSE
1818 if we are only book-keeping on passes that may have selectively disabled
1819 themselves on a given function. */
1821 static void
1822 account_profile (int index, bool run)
1824 pass_manager *passes = g->get_passes ();
1825 if (index == -1)
1826 return;
1827 if (!profile_record)
1828 profile_record = XCNEWVEC (struct profile_record,
1829 passes->passes_by_id_size);
1830 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1831 profile_record[index].run |= run;
1832 profile_record_account_profile (&profile_record[index]);
1835 /* Output profile consistency. */
1837 void
1838 dump_profile_report (void)
1840 g->get_passes ()->dump_profile_report ();
1843 void
1844 pass_manager::dump_profile_report () const
1846 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1847 gcov_type last_time = 0, last_size = 0;
1848 double rel_time_change, rel_size_change;
1849 int last_reported = 0;
1851 if (!profile_record)
1852 return;
1854 FILE *dump_file = dump_begin (TDI_profile_report, NULL);
1855 if (dump_file == NULL)
1856 dump_file = stderr;
1858 fprintf (dump_file, "Profile consistency report:\n\n");
1859 fprintf (dump_file, " |mismatch |mismatch | |\n");
1860 fprintf (dump_file, "Pass name |IN |IN |OUT |OUT |overall |\n");
1861 fprintf (dump_file, " |freq |count |freq |count |size |time |\n");
1863 for (int i = 1; i < passes_by_id_size; i++)
1864 if (profile_record[i].run)
1866 if (last_time)
1867 rel_time_change = (profile_record[i].time
1868 - (double)last_time) * 100 / (double)last_time;
1869 else
1870 rel_time_change = 0;
1871 if (last_size)
1872 rel_size_change = (profile_record[i].size
1873 - (double)last_size) * 100 / (double)last_size;
1874 else
1875 rel_size_change = 0;
1877 if (profile_record[i].num_mismatched_freq_in != last_freq_in
1878 || profile_record[i].num_mismatched_freq_out != last_freq_out
1879 || profile_record[i].num_mismatched_count_in != last_count_in
1880 || profile_record[i].num_mismatched_count_out != last_count_out
1881 || rel_time_change || rel_size_change)
1883 last_reported = i;
1884 fprintf (dump_file, "%-33s", passes_by_id[i]->name);
1885 if (profile_record[i].num_mismatched_freq_in != last_freq_in)
1886 fprintf (dump_file, "| %+5i",
1887 profile_record[i].num_mismatched_freq_in
1888 - last_freq_in);
1889 else
1890 fprintf (dump_file, "| ");
1891 if (profile_record[i].num_mismatched_count_in != last_count_in)
1892 fprintf (dump_file, "| %+5i",
1893 profile_record[i].num_mismatched_count_in
1894 - last_count_in);
1895 else
1896 fprintf (dump_file, "| ");
1897 if (profile_record[i].num_mismatched_freq_out != last_freq_out)
1898 fprintf (dump_file, "| %+5i",
1899 profile_record[i].num_mismatched_freq_out
1900 - last_freq_out);
1901 else
1902 fprintf (dump_file, "| ");
1903 if (profile_record[i].num_mismatched_count_out != last_count_out)
1904 fprintf (dump_file, "| %+5i",
1905 profile_record[i].num_mismatched_count_out
1906 - last_count_out);
1907 else
1908 fprintf (dump_file, "| ");
1910 /* Size/time units change across gimple and RTL. */
1911 if (i == pass_expand_1->static_pass_number)
1912 fprintf (dump_file, "|----------|----------");
1913 else
1915 if (rel_size_change)
1916 fprintf (dump_file, "| %+8.1f%%", rel_size_change);
1917 else
1918 fprintf (dump_file, "| ");
1919 if (rel_time_change)
1920 fprintf (dump_file, "| %+8.1f%%", rel_time_change);
1921 else
1922 fprintf (dump_file, "| ");
1924 fprintf (dump_file, "|\n");
1925 last_freq_in = profile_record[i].num_mismatched_freq_in;
1926 last_freq_out = profile_record[i].num_mismatched_freq_out;
1927 last_count_in = profile_record[i].num_mismatched_count_in;
1928 last_count_out = profile_record[i].num_mismatched_count_out;
1930 else if (last_reported != i)
1932 last_reported = i;
1933 fprintf (dump_file, "%-20s ------------| | | | | | |\n",
1934 passes_by_id[i]->name);
1936 last_time = profile_record[i].time;
1937 last_size = profile_record[i].size;
1940 dump_end (TDI_profile_report, dump_file);
1943 /* Perform all TODO actions that ought to be done on each function. */
1945 static void
1946 execute_function_todo (function *fn, void *data)
1948 bool from_ipa_pass = (cfun == NULL);
1949 unsigned int flags = (size_t)data;
1950 flags &= ~fn->last_verified;
1951 if (!flags)
1952 return;
1954 push_cfun (fn);
1956 /* If we need to cleanup the CFG let it perform a needed SSA update. */
1957 if (flags & TODO_cleanup_cfg)
1958 cleanup_tree_cfg (flags & TODO_update_ssa_any);
1959 else if (flags & TODO_update_ssa_any)
1960 update_ssa (flags & TODO_update_ssa_any);
1961 gcc_assert (!need_ssa_update_p (fn));
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, bool do_not_collect)
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 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2234 check_profile_consistency (pass->static_pass_number, true);
2236 /* Run pre-pass verification. */
2237 execute_todo (ipa_pass->function_transform_todo_flags_start);
2239 /* Do it! */
2240 todo_after = ipa_pass->function_transform (node);
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 account_profile (pass->static_pass_number, 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 (!do_not_collect && !(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 (bool do_not_collect)
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 do_not_collect);
2281 node->ipa_transforms_to_apply.release ();
2285 /* Check if PASS is explicitly disabled or enabled and return
2286 the gate status. FUNC is the function to be processed, and
2287 GATE_STATUS is the gate status determined by pass manager by
2288 default. */
2290 static bool
2291 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2293 bool explicitly_enabled = false;
2294 bool explicitly_disabled = false;
2296 explicitly_enabled
2297 = is_pass_explicitly_enabled_or_disabled (pass, func,
2298 enabled_pass_uid_range_tab);
2299 explicitly_disabled
2300 = is_pass_explicitly_enabled_or_disabled (pass, func,
2301 disabled_pass_uid_range_tab);
2303 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2305 return gate_status;
2308 /* Determine if PASS_NAME matches CRITERION.
2309 Not a pure predicate, since it can update CRITERION, to support
2310 matching the Nth invocation of a pass.
2311 Subroutine of should_skip_pass_p. */
2313 static bool
2314 determine_pass_name_match (const char *pass_name, char *criterion)
2316 size_t namelen = strlen (pass_name);
2317 if (! strncmp (pass_name, criterion, namelen))
2319 /* The following supports starting with the Nth invocation
2320 of a pass (where N does not necessarily is equal to the
2321 dump file suffix). */
2322 if (criterion[namelen] == '\0'
2323 || (criterion[namelen] == '1'
2324 && criterion[namelen + 1] == '\0'))
2325 return true;
2326 else
2328 if (criterion[namelen + 1] == '\0')
2329 --criterion[namelen];
2330 return false;
2333 else
2334 return false;
2337 /* For skipping passes until "startwith" pass.
2338 Return true iff PASS should be skipped.
2339 Clear cfun->pass_startwith when encountering the "startwith" pass,
2340 so that all subsequent passes are run. */
2342 static bool
2343 should_skip_pass_p (opt_pass *pass)
2345 if (!cfun)
2346 return false;
2347 if (!cfun->pass_startwith)
2348 return false;
2350 /* For __GIMPLE functions, we have to at least start when we leave
2351 SSA. Hence, we need to detect the "expand" pass, and stop skipping
2352 when we encounter it. A cheap way to identify "expand" is it to
2353 detect the destruction of PROP_ssa.
2354 For __RTL functions, we invoke "rest_of_compilation" directly, which
2355 is after "expand", and hence we don't reach this conditional. */
2356 if (pass->properties_destroyed & PROP_ssa)
2358 if (!quiet_flag)
2359 fprintf (stderr, "starting anyway when leaving SSA: %s\n", pass->name);
2360 cfun->pass_startwith = NULL;
2361 return false;
2364 if (determine_pass_name_match (pass->name, cfun->pass_startwith))
2366 if (!quiet_flag)
2367 fprintf (stderr, "found starting pass: %s\n", pass->name);
2368 cfun->pass_startwith = NULL;
2369 return false;
2372 /* For GIMPLE passes, run any property provider (but continue skipping
2373 afterwards).
2374 We don't want to force running RTL passes that are property providers:
2375 "expand" is covered above, and the only pass other than "expand" that
2376 provides a property is "into_cfglayout" (PROP_cfglayout), which does
2377 too much for a dumped __RTL function. */
2378 if (pass->type == GIMPLE_PASS
2379 && pass->properties_provided != 0)
2380 return false;
2382 /* We need to (re-)build cgraph edges as needed. */
2383 if (strstr (pass->name, "build_cgraph_edges") != NULL)
2384 return false;
2386 /* Don't skip df init; later RTL passes need it. */
2387 if (strstr (pass->name, "dfinit") != NULL
2388 || strstr (pass->name, "dfinish") != NULL)
2389 return false;
2391 if (!quiet_flag)
2392 fprintf (stderr, "skipping pass: %s\n", pass->name);
2394 /* If we get here, then we have a "startwith" that we haven't seen yet;
2395 skip the pass. */
2396 return true;
2399 /* Skip the given pass, for handling passes before "startwith"
2400 in __GIMPLE and__RTL-marked functions.
2401 In theory, this ought to be a no-op, but some of the RTL passes
2402 need additional processing here. */
2404 static void
2405 skip_pass (opt_pass *pass)
2407 /* Pass "reload" sets the global "reload_completed", and many
2408 things depend on this (e.g. instructions in .md files). */
2409 if (strcmp (pass->name, "reload") == 0)
2410 reload_completed = 1;
2412 /* Similar for pass "pro_and_epilogue" and the "epilogue_completed" global
2413 variable. */
2414 if (strcmp (pass->name, "pro_and_epilogue") == 0)
2415 epilogue_completed = 1;
2417 /* The INSN_ADDRESSES vec is normally set up by
2418 shorten_branches; set it up for the benefit of passes that
2419 run after this. */
2420 if (strcmp (pass->name, "shorten") == 0)
2421 INSN_ADDRESSES_ALLOC (get_max_uid ());
2423 /* Update the cfg hooks as appropriate. */
2424 if (strcmp (pass->name, "into_cfglayout") == 0)
2426 cfg_layout_rtl_register_cfg_hooks ();
2427 cfun->curr_properties |= PROP_cfglayout;
2429 if (strcmp (pass->name, "outof_cfglayout") == 0)
2431 rtl_register_cfg_hooks ();
2432 cfun->curr_properties &= ~PROP_cfglayout;
2436 /* Execute PASS. */
2438 bool
2439 execute_one_pass (opt_pass *pass)
2441 unsigned int todo_after = 0;
2443 bool gate_status;
2445 /* IPA passes are executed on whole program, so cfun should be NULL.
2446 Other passes need function context set. */
2447 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2448 gcc_assert (!cfun && !current_function_decl);
2449 else
2450 gcc_assert (cfun && current_function_decl);
2452 current_pass = pass;
2454 /* Check whether gate check should be avoided.
2455 User controls the value of the gate through the parameter "gate_status". */
2456 gate_status = pass->gate (cfun);
2457 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2459 /* Override gate with plugin. */
2460 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2462 if (!gate_status)
2464 /* Run so passes selectively disabling themselves on a given function
2465 are not miscounted. */
2466 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2468 check_profile_consistency (pass->static_pass_number, false);
2469 account_profile (pass->static_pass_number, false);
2471 current_pass = NULL;
2472 return false;
2475 if (should_skip_pass_p (pass))
2477 skip_pass (pass);
2478 return true;
2481 /* Pass execution event trigger: useful to identify passes being
2482 executed. */
2483 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2485 if (!quiet_flag && !cfun)
2486 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2488 /* Note that the folders should only create gimple expressions.
2489 This is a hack until the new folder is ready. */
2490 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2492 pass_init_dump_file (pass);
2494 /* If a timevar is present, start it. */
2495 if (pass->tv_id != TV_NONE)
2496 timevar_push (pass->tv_id);
2498 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2499 check_profile_consistency (pass->static_pass_number, true);
2501 /* Run pre-pass verification. */
2502 execute_todo (pass->todo_flags_start);
2504 if (flag_checking)
2505 do_per_function (verify_curr_properties,
2506 (void *)(size_t)pass->properties_required);
2508 /* Do it! */
2509 todo_after = pass->execute (cfun);
2511 if (todo_after & TODO_discard_function)
2513 /* Stop timevar. */
2514 if (pass->tv_id != TV_NONE)
2515 timevar_pop (pass->tv_id);
2517 pass_fini_dump_file (pass);
2519 gcc_assert (cfun);
2520 /* As cgraph_node::release_body expects release dominators info,
2521 we have to release it. */
2522 if (dom_info_available_p (CDI_DOMINATORS))
2523 free_dominance_info (CDI_DOMINATORS);
2525 if (dom_info_available_p (CDI_POST_DOMINATORS))
2526 free_dominance_info (CDI_POST_DOMINATORS);
2528 tree fn = cfun->decl;
2529 pop_cfun ();
2530 gcc_assert (!cfun);
2531 cgraph_node::get (fn)->release_body ();
2533 current_pass = NULL;
2534 redirect_edge_var_map_empty ();
2536 ggc_collect ();
2538 return true;
2541 do_per_function (clear_last_verified, NULL);
2543 do_per_function (update_properties_after_pass, pass);
2545 /* Run post-pass cleanup and verification. */
2546 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2547 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2548 account_profile (pass->static_pass_number, true);
2550 verify_interpass_invariants ();
2552 /* Stop timevar. */
2553 if (pass->tv_id != TV_NONE)
2554 timevar_pop (pass->tv_id);
2556 if (pass->type == IPA_PASS
2557 && ((ipa_opt_pass_d *)pass)->function_transform)
2559 struct cgraph_node *node;
2560 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2561 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2563 else if (dump_file)
2564 do_per_function (execute_function_dump, pass);
2566 if (!current_function_decl)
2567 symtab->process_new_functions ();
2569 pass_fini_dump_file (pass);
2571 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2572 gcc_assert (!(cfun->curr_properties & PROP_trees)
2573 || pass->type != RTL_PASS);
2575 current_pass = NULL;
2576 redirect_edge_var_map_empty ();
2578 /* Signal this is a suitable GC collection point. */
2579 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2580 ggc_collect ();
2582 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2583 report_heap_memory_use ();
2584 return true;
2587 static void
2588 execute_pass_list_1 (opt_pass *pass)
2592 gcc_assert (pass->type == GIMPLE_PASS
2593 || pass->type == RTL_PASS);
2595 if (cfun == NULL)
2596 return;
2597 if (execute_one_pass (pass) && pass->sub)
2598 execute_pass_list_1 (pass->sub);
2599 pass = pass->next;
2601 while (pass);
2604 void
2605 execute_pass_list (function *fn, opt_pass *pass)
2607 gcc_assert (fn == cfun);
2608 execute_pass_list_1 (pass);
2609 if (cfun && fn->cfg)
2611 free_dominance_info (CDI_DOMINATORS);
2612 free_dominance_info (CDI_POST_DOMINATORS);
2616 /* Write out all LTO data. */
2617 static void
2618 write_lto (void)
2620 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2621 lto_output ();
2622 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2623 timevar_push (TV_IPA_LTO_DECL_OUT);
2624 produce_asm_for_decls ();
2625 timevar_pop (TV_IPA_LTO_DECL_OUT);
2628 /* Same as execute_pass_list but assume that subpasses of IPA passes
2629 are local passes. If SET is not NULL, write out summaries of only
2630 those node in SET. */
2632 static void
2633 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2635 while (pass)
2637 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2638 gcc_assert (!current_function_decl);
2639 gcc_assert (!cfun);
2640 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2641 if (pass->type == IPA_PASS
2642 && ipa_pass->write_summary
2643 && pass->gate (cfun))
2645 /* If a timevar is present, start it. */
2646 if (pass->tv_id)
2647 timevar_push (pass->tv_id);
2649 pass_init_dump_file (pass);
2651 current_pass = pass;
2652 ipa_pass->write_summary ();
2654 pass_fini_dump_file (pass);
2656 /* If a timevar is present, start it. */
2657 if (pass->tv_id)
2658 timevar_pop (pass->tv_id);
2661 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2662 ipa_write_summaries_2 (pass->sub, state);
2664 pass = pass->next;
2668 /* Helper function of ipa_write_summaries. Creates and destroys the
2669 decl state and calls ipa_write_summaries_2 for all passes that have
2670 summaries. SET is the set of nodes to be written. */
2672 static void
2673 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2675 pass_manager *passes = g->get_passes ();
2676 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2677 state->symtab_node_encoder = encoder;
2679 lto_output_init_mode_table ();
2680 lto_push_out_decl_state (state);
2682 gcc_assert (!flag_wpa);
2683 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2685 write_lto ();
2687 gcc_assert (lto_get_out_decl_state () == state);
2688 lto_pop_out_decl_state ();
2689 lto_delete_out_decl_state (state);
2692 /* Write out summaries for all the nodes in the callgraph. */
2694 void
2695 ipa_write_summaries (void)
2697 lto_symtab_encoder_t encoder;
2698 int i, order_pos;
2699 varpool_node *vnode;
2700 struct cgraph_node *node;
2701 struct cgraph_node **order;
2703 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2704 return;
2706 gcc_assert (!dump_file);
2707 streamer_dump_file = dump_begin (TDI_lto_stream_out, NULL);
2709 select_what_to_stream ();
2711 encoder = lto_symtab_encoder_new (false);
2713 /* Create the callgraph set in the same order used in
2714 cgraph_expand_all_functions. This mostly facilitates debugging,
2715 since it causes the gimple file to be processed in the same order
2716 as the source code. */
2717 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2718 order_pos = ipa_reverse_postorder (order);
2719 gcc_assert (order_pos == symtab->cgraph_count);
2721 for (i = order_pos - 1; i >= 0; i--)
2723 struct cgraph_node *node = order[i];
2725 if (node->definition && node->need_lto_streaming)
2727 if (gimple_has_body_p (node->decl))
2728 lto_prepare_function_for_streaming (node);
2729 lto_set_symtab_encoder_in_partition (encoder, node);
2733 FOR_EACH_DEFINED_FUNCTION (node)
2734 if (node->alias && node->need_lto_streaming)
2735 lto_set_symtab_encoder_in_partition (encoder, node);
2736 FOR_EACH_DEFINED_VARIABLE (vnode)
2737 if (vnode->need_lto_streaming)
2738 lto_set_symtab_encoder_in_partition (encoder, vnode);
2740 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2742 free (order);
2743 if (streamer_dump_file)
2745 dump_end (TDI_lto_stream_out, streamer_dump_file);
2746 streamer_dump_file = NULL;
2750 /* Same as execute_pass_list but assume that subpasses of IPA passes
2751 are local passes. If SET is not NULL, write out optimization summaries of
2752 only those node in SET. */
2754 static void
2755 ipa_write_optimization_summaries_1 (opt_pass *pass,
2756 struct lto_out_decl_state *state)
2758 while (pass)
2760 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2761 gcc_assert (!current_function_decl);
2762 gcc_assert (!cfun);
2763 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2764 if (pass->type == IPA_PASS
2765 && ipa_pass->write_optimization_summary
2766 && pass->gate (cfun))
2768 /* If a timevar is present, start it. */
2769 if (pass->tv_id)
2770 timevar_push (pass->tv_id);
2772 pass_init_dump_file (pass);
2774 current_pass = pass;
2775 ipa_pass->write_optimization_summary ();
2777 pass_fini_dump_file (pass);
2779 /* If a timevar is present, start it. */
2780 if (pass->tv_id)
2781 timevar_pop (pass->tv_id);
2784 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2785 ipa_write_optimization_summaries_1 (pass->sub, state);
2787 pass = pass->next;
2791 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2792 NULL, write out all summaries of all nodes. */
2794 void
2795 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2797 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2798 state->symtab_node_encoder = encoder;
2800 lto_output_init_mode_table ();
2801 lto_push_out_decl_state (state);
2803 /* Be sure that we did not forget to renumber stmt uids. */
2804 gcc_checking_assert (flag_wpa);
2806 gcc_assert (flag_wpa);
2807 pass_manager *passes = g->get_passes ();
2808 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2810 write_lto ();
2812 gcc_assert (lto_get_out_decl_state () == state);
2813 lto_pop_out_decl_state ();
2814 lto_delete_out_decl_state (state);
2817 /* Same as execute_pass_list but assume that subpasses of IPA passes
2818 are local passes. */
2820 static void
2821 ipa_read_summaries_1 (opt_pass *pass)
2823 while (pass)
2825 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2827 gcc_assert (!current_function_decl);
2828 gcc_assert (!cfun);
2829 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2831 if (pass->gate (cfun))
2833 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2835 /* If a timevar is present, start it. */
2836 if (pass->tv_id)
2837 timevar_push (pass->tv_id);
2838 if (!quiet_flag)
2839 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
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);
2851 ggc_grow ();
2852 report_heap_memory_use ();
2855 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2856 ipa_read_summaries_1 (pass->sub);
2858 pass = pass->next;
2863 /* Read all the summaries for all_regular_ipa_passes. */
2865 void
2866 ipa_read_summaries (void)
2868 pass_manager *passes = g->get_passes ();
2869 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2872 /* Same as execute_pass_list but assume that subpasses of IPA passes
2873 are local passes. */
2875 static void
2876 ipa_read_optimization_summaries_1 (opt_pass *pass)
2878 while (pass)
2880 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2882 gcc_assert (!current_function_decl);
2883 gcc_assert (!cfun);
2884 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2886 if (pass->gate (cfun))
2888 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2890 /* If a timevar is present, start it. */
2891 if (pass->tv_id)
2892 timevar_push (pass->tv_id);
2893 if (!quiet_flag)
2894 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2896 pass_init_dump_file (pass);
2898 current_pass = pass;
2899 ipa_pass->read_optimization_summary ();
2901 pass_fini_dump_file (pass);
2903 /* Stop timevar. */
2904 if (pass->tv_id)
2905 timevar_pop (pass->tv_id);
2908 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2909 ipa_read_optimization_summaries_1 (pass->sub);
2910 ggc_grow ();
2911 report_heap_memory_use ();
2913 pass = pass->next;
2917 /* Read all the summaries for all_regular_ipa_passes. */
2919 void
2920 ipa_read_optimization_summaries (void)
2922 pass_manager *passes = g->get_passes ();
2923 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2926 /* Same as execute_pass_list but assume that subpasses of IPA passes
2927 are local passes. */
2928 void
2929 execute_ipa_pass_list (opt_pass *pass)
2933 gcc_assert (!current_function_decl);
2934 gcc_assert (!cfun);
2935 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2936 if (execute_one_pass (pass) && pass->sub)
2938 if (pass->sub->type == GIMPLE_PASS)
2940 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2941 do_per_function_toporder ((void (*)(function *, void *))
2942 execute_pass_list,
2943 pass->sub);
2944 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2946 else if (pass->sub->type == SIMPLE_IPA_PASS
2947 || pass->sub->type == IPA_PASS)
2948 execute_ipa_pass_list (pass->sub);
2949 else
2950 gcc_unreachable ();
2952 gcc_assert (!current_function_decl);
2953 symtab->process_new_functions ();
2954 pass = pass->next;
2956 while (pass);
2959 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2961 static void
2962 execute_ipa_stmt_fixups (opt_pass *pass,
2963 struct cgraph_node *node, gimple **stmts)
2965 while (pass)
2967 /* Execute all of the IPA_PASSes in the list. */
2968 if (pass->type == IPA_PASS
2969 && pass->gate (cfun))
2971 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2973 if (ipa_pass->stmt_fixup)
2975 pass_init_dump_file (pass);
2976 /* If a timevar is present, start it. */
2977 if (pass->tv_id)
2978 timevar_push (pass->tv_id);
2980 current_pass = pass;
2981 ipa_pass->stmt_fixup (node, stmts);
2983 /* Stop timevar. */
2984 if (pass->tv_id)
2985 timevar_pop (pass->tv_id);
2986 pass_fini_dump_file (pass);
2988 if (pass->sub)
2989 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2991 pass = pass->next;
2995 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2997 void
2998 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
3000 pass_manager *passes = g->get_passes ();
3001 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
3005 extern void debug_properties (unsigned int);
3006 extern void dump_properties (FILE *, unsigned int);
3008 DEBUG_FUNCTION void
3009 dump_properties (FILE *dump, unsigned int props)
3011 fprintf (dump, "Properties:\n");
3012 if (props & PROP_gimple_any)
3013 fprintf (dump, "PROP_gimple_any\n");
3014 if (props & PROP_gimple_lcf)
3015 fprintf (dump, "PROP_gimple_lcf\n");
3016 if (props & PROP_gimple_leh)
3017 fprintf (dump, "PROP_gimple_leh\n");
3018 if (props & PROP_cfg)
3019 fprintf (dump, "PROP_cfg\n");
3020 if (props & PROP_ssa)
3021 fprintf (dump, "PROP_ssa\n");
3022 if (props & PROP_no_crit_edges)
3023 fprintf (dump, "PROP_no_crit_edges\n");
3024 if (props & PROP_rtl)
3025 fprintf (dump, "PROP_rtl\n");
3026 if (props & PROP_gimple_lomp)
3027 fprintf (dump, "PROP_gimple_lomp\n");
3028 if (props & PROP_gimple_lomp_dev)
3029 fprintf (dump, "PROP_gimple_lomp_dev\n");
3030 if (props & PROP_gimple_lcx)
3031 fprintf (dump, "PROP_gimple_lcx\n");
3032 if (props & PROP_gimple_lvec)
3033 fprintf (dump, "PROP_gimple_lvec\n");
3034 if (props & PROP_cfglayout)
3035 fprintf (dump, "PROP_cfglayout\n");
3038 DEBUG_FUNCTION void
3039 debug_properties (unsigned int props)
3041 dump_properties (stderr, props);
3044 /* Called by local passes to see if function is called by already processed nodes.
3045 Because we process nodes in topological order, this means that function is
3046 in recursive cycle or we introduced new direct calls. */
3047 bool
3048 function_called_by_processed_nodes_p (void)
3050 struct cgraph_edge *e;
3051 for (e = cgraph_node::get (current_function_decl)->callers;
3053 e = e->next_caller)
3055 if (e->caller->decl == current_function_decl)
3056 continue;
3057 if (!e->caller->has_gimple_body_p ())
3058 continue;
3059 if (TREE_ASM_WRITTEN (e->caller->decl))
3060 continue;
3061 if (!e->caller->process && !e->caller->inlined_to)
3062 break;
3064 if (dump_file && e)
3066 fprintf (dump_file, "Already processed call to:\n");
3067 e->caller->dump (dump_file);
3069 return e != NULL;