Fix ICE in lto_symtab_merge_symbols_1 (PR lto/88004).
[official-gcc.git] / gcc / passes.c
blob5d2372bac24408d9d1abf1085bcbe35ef82ef829
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2018 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 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_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_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 } // anon namespace
464 simple_ipa_opt_pass *
465 make_pass_build_ssa_passes (gcc::context *ctxt)
467 return new pass_build_ssa_passes (ctxt);
470 simple_ipa_opt_pass *
471 make_pass_local_optimization_passes (gcc::context *ctxt)
473 return new pass_local_optimization_passes (ctxt);
476 namespace {
478 const pass_data pass_data_all_early_optimizations =
480 GIMPLE_PASS, /* type */
481 "early_optimizations", /* name */
482 OPTGROUP_NONE, /* optinfo_flags */
483 TV_NONE, /* tv_id */
484 0, /* properties_required */
485 0, /* properties_provided */
486 0, /* properties_destroyed */
487 0, /* todo_flags_start */
488 0, /* todo_flags_finish */
491 class pass_all_early_optimizations : public gimple_opt_pass
493 public:
494 pass_all_early_optimizations (gcc::context *ctxt)
495 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
498 /* opt_pass methods: */
499 virtual bool gate (function *)
501 return (optimize >= 1
502 /* Don't bother doing anything if the program has errors. */
503 && !seen_error ());
506 }; // class pass_all_early_optimizations
508 } // anon namespace
510 static gimple_opt_pass *
511 make_pass_all_early_optimizations (gcc::context *ctxt)
513 return new pass_all_early_optimizations (ctxt);
516 namespace {
518 const pass_data pass_data_all_optimizations =
520 GIMPLE_PASS, /* type */
521 "*all_optimizations", /* name */
522 OPTGROUP_NONE, /* optinfo_flags */
523 TV_OPTIMIZE, /* tv_id */
524 0, /* properties_required */
525 0, /* properties_provided */
526 0, /* properties_destroyed */
527 0, /* todo_flags_start */
528 0, /* todo_flags_finish */
531 class pass_all_optimizations : public gimple_opt_pass
533 public:
534 pass_all_optimizations (gcc::context *ctxt)
535 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
538 /* opt_pass methods: */
539 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
541 }; // class pass_all_optimizations
543 } // anon namespace
545 static gimple_opt_pass *
546 make_pass_all_optimizations (gcc::context *ctxt)
548 return new pass_all_optimizations (ctxt);
551 namespace {
553 const pass_data pass_data_all_optimizations_g =
555 GIMPLE_PASS, /* type */
556 "*all_optimizations_g", /* 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_g : public gimple_opt_pass
568 public:
569 pass_all_optimizations_g (gcc::context *ctxt)
570 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
573 /* opt_pass methods: */
574 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
576 }; // class pass_all_optimizations_g
578 } // anon namespace
580 static gimple_opt_pass *
581 make_pass_all_optimizations_g (gcc::context *ctxt)
583 return new pass_all_optimizations_g (ctxt);
586 namespace {
588 const pass_data pass_data_rest_of_compilation =
590 RTL_PASS, /* type */
591 "*rest_of_compilation", /* name */
592 OPTGROUP_NONE, /* optinfo_flags */
593 TV_REST_OF_COMPILATION, /* tv_id */
594 PROP_rtl, /* properties_required */
595 0, /* properties_provided */
596 0, /* properties_destroyed */
597 0, /* todo_flags_start */
598 0, /* todo_flags_finish */
601 class pass_rest_of_compilation : public rtl_opt_pass
603 public:
604 pass_rest_of_compilation (gcc::context *ctxt)
605 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
608 /* opt_pass methods: */
609 virtual bool gate (function *)
611 /* Early return if there were errors. We can run afoul of our
612 consistency checks, and there's not really much point in fixing them. */
613 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
616 }; // class pass_rest_of_compilation
618 } // anon namespace
620 static rtl_opt_pass *
621 make_pass_rest_of_compilation (gcc::context *ctxt)
623 return new pass_rest_of_compilation (ctxt);
626 namespace {
628 const pass_data pass_data_postreload =
630 RTL_PASS, /* type */
631 "*all-postreload", /* name */
632 OPTGROUP_NONE, /* optinfo_flags */
633 TV_POSTRELOAD, /* tv_id */
634 PROP_rtl, /* properties_required */
635 0, /* properties_provided */
636 0, /* properties_destroyed */
637 0, /* todo_flags_start */
638 0, /* todo_flags_finish */
641 class pass_postreload : public rtl_opt_pass
643 public:
644 pass_postreload (gcc::context *ctxt)
645 : rtl_opt_pass (pass_data_postreload, ctxt)
648 /* opt_pass methods: */
649 virtual bool gate (function *) { return reload_completed; }
651 }; // class pass_postreload
653 } // anon namespace
655 static rtl_opt_pass *
656 make_pass_postreload (gcc::context *ctxt)
658 return new pass_postreload (ctxt);
661 namespace {
663 const pass_data pass_data_late_compilation =
665 RTL_PASS, /* type */
666 "*all-late_compilation", /* name */
667 OPTGROUP_NONE, /* optinfo_flags */
668 TV_LATE_COMPILATION, /* 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_late_compilation : public rtl_opt_pass
678 public:
679 pass_late_compilation (gcc::context *ctxt)
680 : rtl_opt_pass (pass_data_late_compilation, ctxt)
683 /* opt_pass methods: */
684 virtual bool gate (function *)
686 return reload_completed || targetm.no_register_allocation;
689 }; // class pass_late_compilation
691 } // anon namespace
693 static rtl_opt_pass *
694 make_pass_late_compilation (gcc::context *ctxt)
696 return new pass_late_compilation (ctxt);
701 /* Set the static pass number of pass PASS to ID and record that
702 in the mapping from static pass number to pass. */
704 void
705 pass_manager::
706 set_pass_for_id (int id, opt_pass *pass)
708 pass->static_pass_number = id;
709 if (passes_by_id_size <= id)
711 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
712 memset (passes_by_id + passes_by_id_size, 0,
713 (id + 1 - passes_by_id_size) * sizeof (void *));
714 passes_by_id_size = id + 1;
716 passes_by_id[id] = pass;
719 /* Return the pass with the static pass number ID. */
721 opt_pass *
722 pass_manager::get_pass_for_id (int id) const
724 if (id >= passes_by_id_size)
725 return NULL;
726 return passes_by_id[id];
729 /* Iterate over the pass tree allocating dump file numbers. We want
730 to do this depth first, and independent of whether the pass is
731 enabled or not. */
733 void
734 register_one_dump_file (opt_pass *pass)
736 g->get_passes ()->register_one_dump_file (pass);
739 void
740 pass_manager::register_one_dump_file (opt_pass *pass)
742 char *dot_name, *flag_name, *glob_name;
743 const char *name, *full_name, *prefix;
745 /* Buffer big enough to format a 32-bit UINT_MAX into. */
746 char num[11];
747 dump_kind dkind;
748 int id;
749 optgroup_flags_t optgroup_flags = OPTGROUP_NONE;
750 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
752 /* See below in next_pass_1. */
753 num[0] = '\0';
754 if (pass->static_pass_number != -1)
755 sprintf (num, "%u", ((int) pass->static_pass_number < 0
756 ? 1 : pass->static_pass_number));
758 /* The name is both used to identify the pass for the purposes of plugins,
759 and to specify dump file name and option.
760 The latter two might want something short which is not quite unique; for
761 that reason, we may have a disambiguating prefix, followed by a space
762 to mark the start of the following dump file name / option string. */
763 name = strchr (pass->name, ' ');
764 name = name ? name + 1 : pass->name;
765 dot_name = concat (".", name, num, NULL);
766 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
768 prefix = "ipa-";
769 dkind = DK_ipa;
770 optgroup_flags |= OPTGROUP_IPA;
772 else if (pass->type == GIMPLE_PASS)
774 prefix = "tree-";
775 dkind = DK_tree;
777 else
779 prefix = "rtl-";
780 dkind = DK_rtl;
783 flag_name = concat (prefix, name, num, NULL);
784 glob_name = concat (prefix, name, NULL);
785 optgroup_flags |= pass->optinfo_flags;
786 /* For any passes that do not have an optgroup set, and which are not
787 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
788 any dump messages are emitted properly under -fopt-info(-optall). */
789 if (optgroup_flags == OPTGROUP_NONE)
790 optgroup_flags = OPTGROUP_OTHER;
791 id = dumps->dump_register (dot_name, flag_name, glob_name, dkind,
792 optgroup_flags,
793 true);
794 set_pass_for_id (id, pass);
795 full_name = concat (prefix, pass->name, num, NULL);
796 register_pass_name (pass, full_name);
797 free (CONST_CAST (char *, full_name));
800 /* Register the dump files for the pass_manager starting at PASS. */
802 void
803 pass_manager::register_dump_files (opt_pass *pass)
807 if (pass->name && pass->name[0] != '*')
808 register_one_dump_file (pass);
810 if (pass->sub)
811 register_dump_files (pass->sub);
813 pass = pass->next;
815 while (pass);
818 /* Register PASS with NAME. */
820 void
821 pass_manager::register_pass_name (opt_pass *pass, const char *name)
823 if (!m_name_to_pass_map)
824 m_name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
826 if (m_name_to_pass_map->get (name))
827 return; /* Ignore plugin passes. */
829 const char *unique_name = xstrdup (name);
830 m_name_to_pass_map->put (unique_name, pass);
833 /* Map from pass id to canonicalized pass name. */
835 typedef const char *char_ptr;
836 static vec<char_ptr> pass_tab;
838 /* Callback function for traversing NAME_TO_PASS_MAP. */
840 bool
841 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
843 gcc_assert (pass->static_pass_number > 0);
844 gcc_assert (pass_tab.exists ());
846 pass_tab[pass->static_pass_number] = name;
848 return 1;
851 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
852 table for dumping purpose. */
854 void
855 pass_manager::create_pass_tab (void) const
857 if (!flag_dump_passes)
858 return;
860 pass_tab.safe_grow_cleared (passes_by_id_size + 1);
861 m_name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
864 static bool override_gate_status (opt_pass *, tree, bool);
866 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
867 is turned on or not. */
869 static void
870 dump_one_pass (opt_pass *pass, int pass_indent)
872 int indent = 3 * pass_indent;
873 const char *pn;
874 bool is_on, is_really_on;
876 is_on = pass->gate (cfun);
877 is_really_on = override_gate_status (pass, current_function_decl, is_on);
879 if (pass->static_pass_number <= 0)
880 pn = pass->name;
881 else
882 pn = pass_tab[pass->static_pass_number];
884 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
885 (15 - indent < 0 ? 0 : 15 - indent), " ",
886 is_on ? " ON" : " OFF",
887 ((!is_on) == (!is_really_on) ? ""
888 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
891 /* Dump pass list PASS with indentation INDENT. */
893 static void
894 dump_pass_list (opt_pass *pass, int indent)
898 dump_one_pass (pass, indent);
899 if (pass->sub)
900 dump_pass_list (pass->sub, indent + 1);
901 pass = pass->next;
903 while (pass);
906 /* Dump all optimization passes. */
908 void
909 dump_passes (void)
911 g->get_passes ()->dump_passes ();
914 void
915 pass_manager::dump_passes () const
917 push_dummy_function (true);
919 create_pass_tab ();
921 dump_pass_list (all_lowering_passes, 1);
922 dump_pass_list (all_small_ipa_passes, 1);
923 dump_pass_list (all_regular_ipa_passes, 1);
924 dump_pass_list (all_late_ipa_passes, 1);
925 dump_pass_list (all_passes, 1);
927 pop_dummy_function ();
930 /* Returns the pass with NAME. */
932 opt_pass *
933 pass_manager::get_pass_by_name (const char *name)
935 opt_pass **p = m_name_to_pass_map->get (name);
936 if (p)
937 return *p;
939 return NULL;
943 /* Range [start, last]. */
945 struct uid_range
947 unsigned int start;
948 unsigned int last;
949 const char *assem_name;
950 struct uid_range *next;
953 typedef struct uid_range *uid_range_p;
956 static vec<uid_range_p> enabled_pass_uid_range_tab;
957 static vec<uid_range_p> disabled_pass_uid_range_tab;
960 /* Parse option string for -fdisable- and -fenable-
961 The syntax of the options:
963 -fenable-<pass_name>
964 -fdisable-<pass_name>
966 -fenable-<pass_name>=s1:e1,s2:e2,...
967 -fdisable-<pass_name>=s1:e1,s2:e2,...
970 static void
971 enable_disable_pass (const char *arg, bool is_enable)
973 opt_pass *pass;
974 char *range_str, *phase_name;
975 char *argstr = xstrdup (arg);
976 vec<uid_range_p> *tab = 0;
978 range_str = strchr (argstr,'=');
979 if (range_str)
981 *range_str = '\0';
982 range_str++;
985 phase_name = argstr;
986 if (!*phase_name)
988 if (is_enable)
989 error ("unrecognized option -fenable");
990 else
991 error ("unrecognized option -fdisable");
992 free (argstr);
993 return;
995 pass = g->get_passes ()->get_pass_by_name (phase_name);
996 if (!pass || pass->static_pass_number == -1)
998 if (is_enable)
999 error ("unknown pass %s specified in -fenable", phase_name);
1000 else
1001 error ("unknown pass %s specified in -fdisable", phase_name);
1002 free (argstr);
1003 return;
1006 if (is_enable)
1007 tab = &enabled_pass_uid_range_tab;
1008 else
1009 tab = &disabled_pass_uid_range_tab;
1011 if ((unsigned) pass->static_pass_number >= tab->length ())
1012 tab->safe_grow_cleared (pass->static_pass_number + 1);
1014 if (!range_str)
1016 uid_range_p slot;
1017 uid_range_p new_range = XCNEW (struct uid_range);
1019 new_range->start = 0;
1020 new_range->last = (unsigned)-1;
1022 slot = (*tab)[pass->static_pass_number];
1023 new_range->next = slot;
1024 (*tab)[pass->static_pass_number] = new_range;
1025 if (is_enable)
1026 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1027 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1028 else
1029 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1030 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1032 else
1034 char *next_range = NULL;
1035 char *one_range = range_str;
1036 char *end_val = NULL;
1040 uid_range_p slot;
1041 uid_range_p new_range;
1042 char *invalid = NULL;
1043 long start;
1044 char *func_name = NULL;
1046 next_range = strchr (one_range, ',');
1047 if (next_range)
1049 *next_range = '\0';
1050 next_range++;
1053 end_val = strchr (one_range, ':');
1054 if (end_val)
1056 *end_val = '\0';
1057 end_val++;
1059 start = strtol (one_range, &invalid, 10);
1060 if (*invalid || start < 0)
1062 if (end_val || (one_range[0] >= '0'
1063 && one_range[0] <= '9'))
1065 error ("Invalid range %s in option %s",
1066 one_range,
1067 is_enable ? "-fenable" : "-fdisable");
1068 free (argstr);
1069 return;
1071 func_name = one_range;
1073 if (!end_val)
1075 new_range = XCNEW (struct uid_range);
1076 if (!func_name)
1078 new_range->start = (unsigned) start;
1079 new_range->last = (unsigned) start;
1081 else
1083 new_range->start = (unsigned) -1;
1084 new_range->last = (unsigned) -1;
1085 new_range->assem_name = xstrdup (func_name);
1088 else
1090 long last = strtol (end_val, &invalid, 10);
1091 if (*invalid || last < start)
1093 error ("Invalid range %s in option %s",
1094 end_val,
1095 is_enable ? "-fenable" : "-fdisable");
1096 free (argstr);
1097 return;
1099 new_range = XCNEW (struct uid_range);
1100 new_range->start = (unsigned) start;
1101 new_range->last = (unsigned) last;
1104 slot = (*tab)[pass->static_pass_number];
1105 new_range->next = slot;
1106 (*tab)[pass->static_pass_number] = new_range;
1107 if (is_enable)
1109 if (new_range->assem_name)
1110 inform (UNKNOWN_LOCATION,
1111 "enable pass %s for function %s",
1112 phase_name, new_range->assem_name);
1113 else
1114 inform (UNKNOWN_LOCATION,
1115 "enable pass %s for functions in the range of [%u, %u]",
1116 phase_name, new_range->start, new_range->last);
1118 else
1120 if (new_range->assem_name)
1121 inform (UNKNOWN_LOCATION,
1122 "disable pass %s for function %s",
1123 phase_name, new_range->assem_name);
1124 else
1125 inform (UNKNOWN_LOCATION,
1126 "disable pass %s for functions in the range of [%u, %u]",
1127 phase_name, new_range->start, new_range->last);
1130 one_range = next_range;
1131 } while (next_range);
1134 free (argstr);
1137 /* Enable pass specified by ARG. */
1139 void
1140 enable_pass (const char *arg)
1142 enable_disable_pass (arg, true);
1145 /* Disable pass specified by ARG. */
1147 void
1148 disable_pass (const char *arg)
1150 enable_disable_pass (arg, false);
1153 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1155 static bool
1156 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1157 tree func,
1158 vec<uid_range_p> tab)
1160 uid_range_p slot, range;
1161 int cgraph_uid;
1162 const char *aname = NULL;
1164 if (!tab.exists ()
1165 || (unsigned) pass->static_pass_number >= tab.length ()
1166 || pass->static_pass_number == -1)
1167 return false;
1169 slot = tab[pass->static_pass_number];
1170 if (!slot)
1171 return false;
1173 cgraph_uid = func ? cgraph_node::get (func)->get_uid () : 0;
1174 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1175 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1177 range = slot;
1178 while (range)
1180 if ((unsigned) cgraph_uid >= range->start
1181 && (unsigned) cgraph_uid <= range->last)
1182 return true;
1183 if (range->assem_name && aname
1184 && !strcmp (range->assem_name, aname))
1185 return true;
1186 range = range->next;
1189 return false;
1193 /* Update static_pass_number for passes (and the flag
1194 TODO_mark_first_instance).
1196 Passes are constructed with static_pass_number preinitialized to 0
1198 This field is used in two different ways: initially as instance numbers
1199 of their kind, and then as ids within the entire pass manager.
1201 Within pass_manager::pass_manager:
1203 * In add_pass_instance(), as called by next_pass_1 in
1204 NEXT_PASS in init_optimization_passes
1206 * When the initial instance of a pass within a pass manager is seen,
1207 it is flagged, and its static_pass_number is set to -1
1209 * On subsequent times that it is seen, the static pass number
1210 is decremented each time, so that if there are e.g. 4 dups,
1211 they have static_pass_number -4, 2, 3, 4 respectively (note
1212 how the initial one is negative and gives the count); these
1213 can be thought of as instance numbers of the specific pass
1215 * Within the register_dump_files () traversal, set_pass_for_id()
1216 is called on each pass, using these instance numbers to create
1217 dumpfile switches, and then overwriting them with a pass id,
1218 which are global to the whole pass manager (based on
1219 (TDI_end + current value of extra_dump_files_in_use) ) */
1221 static void
1222 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1223 opt_pass *initial_pass)
1225 /* Are we dealing with the first pass of its kind, or a clone? */
1226 if (new_pass != initial_pass)
1228 /* We're dealing with a clone. */
1229 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1231 /* Indicate to register_dump_files that this pass has duplicates,
1232 and so it should rename the dump file. The first instance will
1233 be -1, and be number of duplicates = -static_pass_number - 1.
1234 Subsequent instances will be > 0 and just the duplicate number. */
1235 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1237 initial_pass->static_pass_number -= 1;
1238 new_pass->static_pass_number = -initial_pass->static_pass_number;
1241 else
1243 /* We're dealing with the first pass of its kind. */
1244 new_pass->todo_flags_start |= TODO_mark_first_instance;
1245 new_pass->static_pass_number = -1;
1247 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1251 /* Add a pass to the pass list. Duplicate the pass if it's already
1252 in the list. */
1254 static opt_pass **
1255 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1257 /* Every pass should have a name so that plugins can refer to them. */
1258 gcc_assert (pass->name != NULL);
1260 add_pass_instance (pass, false, initial_pass);
1261 *list = pass;
1263 return &(*list)->next;
1266 /* List node for an inserted pass instance. We need to keep track of all
1267 the newly-added pass instances (with 'added_pass_nodes' defined below)
1268 so that we can register their dump files after pass-positioning is finished.
1269 Registering dumping files needs to be post-processed or the
1270 static_pass_number of the opt_pass object would be modified and mess up
1271 the dump file names of future pass instances to be added. */
1273 struct pass_list_node
1275 opt_pass *pass;
1276 struct pass_list_node *next;
1279 static struct pass_list_node *added_pass_nodes = NULL;
1280 static struct pass_list_node *prev_added_pass_node;
1282 /* Insert the pass at the proper position. Return true if the pass
1283 is successfully added.
1285 NEW_PASS_INFO - new pass to be inserted
1286 PASS_LIST - root of the pass list to insert the new pass to */
1288 static bool
1289 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1291 opt_pass *pass = *pass_list, *prev_pass = NULL;
1292 bool success = false;
1294 for ( ; pass; prev_pass = pass, pass = pass->next)
1296 /* Check if the current pass is of the same type as the new pass and
1297 matches the name and the instance number of the reference pass. */
1298 if (pass->type == new_pass_info->pass->type
1299 && pass->name
1300 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1301 && ((new_pass_info->ref_pass_instance_number == 0)
1302 || (new_pass_info->ref_pass_instance_number ==
1303 pass->static_pass_number)
1304 || (new_pass_info->ref_pass_instance_number == 1
1305 && pass->todo_flags_start & TODO_mark_first_instance)))
1307 opt_pass *new_pass;
1308 struct pass_list_node *new_pass_node;
1310 if (new_pass_info->ref_pass_instance_number == 0)
1312 new_pass = new_pass_info->pass->clone ();
1313 add_pass_instance (new_pass, true, new_pass_info->pass);
1315 else
1317 new_pass = new_pass_info->pass;
1318 add_pass_instance (new_pass, true, new_pass);
1321 /* Insert the new pass instance based on the positioning op. */
1322 switch (new_pass_info->pos_op)
1324 case PASS_POS_INSERT_AFTER:
1325 new_pass->next = pass->next;
1326 pass->next = new_pass;
1328 /* Skip newly inserted pass to avoid repeated
1329 insertions in the case where the new pass and the
1330 existing one have the same name. */
1331 pass = new_pass;
1332 break;
1333 case PASS_POS_INSERT_BEFORE:
1334 new_pass->next = pass;
1335 if (prev_pass)
1336 prev_pass->next = new_pass;
1337 else
1338 *pass_list = new_pass;
1339 break;
1340 case PASS_POS_REPLACE:
1341 new_pass->next = pass->next;
1342 if (prev_pass)
1343 prev_pass->next = new_pass;
1344 else
1345 *pass_list = new_pass;
1346 new_pass->sub = pass->sub;
1347 new_pass->tv_id = pass->tv_id;
1348 pass = new_pass;
1349 break;
1350 default:
1351 error ("invalid pass positioning operation");
1352 return false;
1355 /* Save the newly added pass (instance) in the added_pass_nodes
1356 list so that we can register its dump file later. Note that
1357 we cannot register the dump file now because doing so will modify
1358 the static_pass_number of the opt_pass object and therefore
1359 mess up the dump file name of future instances. */
1360 new_pass_node = XCNEW (struct pass_list_node);
1361 new_pass_node->pass = new_pass;
1362 if (!added_pass_nodes)
1363 added_pass_nodes = new_pass_node;
1364 else
1365 prev_added_pass_node->next = new_pass_node;
1366 prev_added_pass_node = new_pass_node;
1368 success = true;
1371 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1372 success = true;
1375 return success;
1378 /* Hooks a new pass into the pass lists.
1380 PASS_INFO - pass information that specifies the opt_pass object,
1381 reference pass, instance number, and how to position
1382 the pass */
1384 void
1385 register_pass (struct register_pass_info *pass_info)
1387 g->get_passes ()->register_pass (pass_info);
1390 void
1391 register_pass (opt_pass* pass, pass_positioning_ops pos,
1392 const char* ref_pass_name, int ref_pass_inst_number)
1394 register_pass_info i;
1395 i.pass = pass;
1396 i.reference_pass_name = ref_pass_name;
1397 i.ref_pass_instance_number = ref_pass_inst_number;
1398 i.pos_op = pos;
1400 g->get_passes ()->register_pass (&i);
1403 void
1404 pass_manager::register_pass (struct register_pass_info *pass_info)
1406 bool all_instances, success;
1408 /* The checks below could fail in buggy plugins. Existing GCC
1409 passes should never fail these checks, so we mention plugin in
1410 the messages. */
1411 if (!pass_info->pass)
1412 fatal_error (input_location, "plugin cannot register a missing pass");
1414 if (!pass_info->pass->name)
1415 fatal_error (input_location, "plugin cannot register an unnamed pass");
1417 if (!pass_info->reference_pass_name)
1418 fatal_error
1419 (input_location,
1420 "plugin cannot register pass %qs without reference pass name",
1421 pass_info->pass->name);
1423 /* Try to insert the new pass to the pass lists. We need to check
1424 all five lists as the reference pass could be in one (or all) of
1425 them. */
1426 all_instances = pass_info->ref_pass_instance_number == 0;
1427 success = position_pass (pass_info, &all_lowering_passes);
1428 if (!success || all_instances)
1429 success |= position_pass (pass_info, &all_small_ipa_passes);
1430 if (!success || all_instances)
1431 success |= position_pass (pass_info, &all_regular_ipa_passes);
1432 if (!success || all_instances)
1433 success |= position_pass (pass_info, &all_late_ipa_passes);
1434 if (!success || all_instances)
1435 success |= position_pass (pass_info, &all_passes);
1436 if (!success)
1437 fatal_error
1438 (input_location,
1439 "pass %qs not found but is referenced by new pass %qs",
1440 pass_info->reference_pass_name, pass_info->pass->name);
1442 /* OK, we have successfully inserted the new pass. We need to register
1443 the dump files for the newly added pass and its duplicates (if any).
1444 While doing so, we also delete the pass_list_node
1445 objects created during pass positioning. */
1446 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1447 while (added_pass_nodes)
1449 struct pass_list_node *next_node = added_pass_nodes->next;
1451 /* Handle -fdump-* and -fopt-info. */
1452 dumps->register_pass (added_pass_nodes->pass);
1454 XDELETE (added_pass_nodes);
1455 added_pass_nodes = next_node;
1459 /* Construct the pass tree. The sequencing of passes is driven by
1460 the cgraph routines:
1462 finalize_compilation_unit ()
1463 for each node N in the cgraph
1464 cgraph_analyze_function (N)
1465 cgraph_lower_function (N) -> all_lowering_passes
1467 If we are optimizing, compile is then invoked:
1469 compile ()
1470 ipa_passes () -> all_small_ipa_passes
1471 -> Analysis of all_regular_ipa_passes
1472 * possible LTO streaming at copmilation time *
1473 -> Execution of all_regular_ipa_passes
1474 * possible LTO streaming at link time *
1475 -> all_late_ipa_passes
1476 expand_all_functions ()
1477 for each node N in the cgraph
1478 expand_function (N) -> Transformation of all_regular_ipa_passes
1479 -> all_passes
1482 pass_manager::pass_manager (context *ctxt)
1483 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1484 all_regular_ipa_passes (NULL),
1485 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1486 m_ctxt (ctxt), m_name_to_pass_map (NULL)
1488 opt_pass **p;
1490 /* Zero-initialize pass members. */
1491 #define INSERT_PASSES_AFTER(PASS)
1492 #define PUSH_INSERT_PASSES_WITHIN(PASS)
1493 #define POP_INSERT_PASSES()
1494 #define NEXT_PASS(PASS, NUM) PASS ## _ ## NUM = NULL
1495 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) NEXT_PASS (PASS, NUM)
1496 #define TERMINATE_PASS_LIST(PASS)
1497 #include "pass-instances.def"
1498 #undef INSERT_PASSES_AFTER
1499 #undef PUSH_INSERT_PASSES_WITHIN
1500 #undef POP_INSERT_PASSES
1501 #undef NEXT_PASS
1502 #undef NEXT_PASS_WITH_ARG
1503 #undef TERMINATE_PASS_LIST
1505 /* Initialize the pass_lists array. */
1506 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1507 GCC_PASS_LISTS
1508 #undef DEF_PASS_LIST
1510 /* Build the tree of passes. */
1512 #define INSERT_PASSES_AFTER(PASS) \
1514 opt_pass **p_start; \
1515 p_start = p = &(PASS);
1517 #define TERMINATE_PASS_LIST(PASS) \
1518 gcc_assert (p_start == &PASS); \
1519 *p = NULL; \
1522 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1524 opt_pass **p = &(PASS ## _1)->sub;
1526 #define POP_INSERT_PASSES() \
1529 #define NEXT_PASS(PASS, NUM) \
1530 do { \
1531 gcc_assert (PASS ## _ ## NUM == NULL); \
1532 if ((NUM) == 1) \
1533 PASS ## _1 = make_##PASS (m_ctxt); \
1534 else \
1536 gcc_assert (PASS ## _1); \
1537 PASS ## _ ## NUM = PASS ## _1->clone (); \
1539 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1540 } while (0)
1542 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) \
1543 do { \
1544 NEXT_PASS (PASS, NUM); \
1545 PASS ## _ ## NUM->set_pass_param (0, ARG); \
1546 } while (0)
1548 #include "pass-instances.def"
1550 #undef INSERT_PASSES_AFTER
1551 #undef PUSH_INSERT_PASSES_WITHIN
1552 #undef POP_INSERT_PASSES
1553 #undef NEXT_PASS
1554 #undef NEXT_PASS_WITH_ARG
1555 #undef TERMINATE_PASS_LIST
1557 /* Register the passes with the tree dump code. */
1558 register_dump_files (all_lowering_passes);
1559 register_dump_files (all_small_ipa_passes);
1560 register_dump_files (all_regular_ipa_passes);
1561 register_dump_files (all_late_ipa_passes);
1562 register_dump_files (all_passes);
1565 static void
1566 delete_pass_tree (opt_pass *pass)
1568 while (pass)
1570 /* Recurse into child passes. */
1571 delete_pass_tree (pass->sub);
1573 opt_pass *next = pass->next;
1575 /* Delete this pass. */
1576 delete pass;
1578 /* Iterate onto sibling passes. */
1579 pass = next;
1583 pass_manager::~pass_manager ()
1585 XDELETEVEC (passes_by_id);
1587 /* Call delete_pass_tree on each of the pass_lists. */
1588 #define DEF_PASS_LIST(LIST) \
1589 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1590 GCC_PASS_LISTS
1591 #undef DEF_PASS_LIST
1595 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1596 function CALLBACK for every function in the call graph. Otherwise,
1597 call CALLBACK on the current function. */
1599 static void
1600 do_per_function (void (*callback) (function *, void *data), void *data)
1602 if (current_function_decl)
1603 callback (cfun, data);
1604 else
1606 struct cgraph_node *node;
1607 FOR_EACH_DEFINED_FUNCTION (node)
1608 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1609 && (!node->clone_of || node->decl != node->clone_of->decl))
1610 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1614 /* Because inlining might remove no-longer reachable nodes, we need to
1615 keep the array visible to garbage collector to avoid reading collected
1616 out nodes. */
1617 static int nnodes;
1618 static GTY ((length ("nnodes"))) cgraph_node **order;
1620 #define uid_hash_t hash_set<int_hash <int, 0, -1> >
1622 /* Hook called when NODE is removed and therefore should be
1623 excluded from order vector. DATA is a hash set with removed nodes. */
1625 static void
1626 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1628 uid_hash_t *removed_nodes = (uid_hash_t *)data;
1629 removed_nodes->add (node->get_uid ());
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.
1635 This function is global so that plugins can use it. */
1636 void
1637 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1639 int i;
1641 if (current_function_decl)
1642 callback (cfun, data);
1643 else
1645 cgraph_node_hook_list *hook;
1646 uid_hash_t removed_nodes;
1647 gcc_assert (!order);
1648 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1650 nnodes = ipa_reverse_postorder (order);
1651 for (i = nnodes - 1; i >= 0; i--)
1652 order[i]->process = 1;
1653 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1654 &removed_nodes);
1655 for (i = nnodes - 1; i >= 0; i--)
1657 cgraph_node *node = order[i];
1659 /* Function could be inlined and removed as unreachable. */
1660 if (node == NULL || removed_nodes.contains (node->get_uid ()))
1661 continue;
1663 /* Allow possibly removed nodes to be garbage collected. */
1664 order[i] = NULL;
1665 node->process = 0;
1666 if (node->has_gimple_body_p ())
1668 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1669 push_cfun (fn);
1670 callback (fn, data);
1671 pop_cfun ();
1674 symtab->remove_cgraph_removal_hook (hook);
1676 ggc_free (order);
1677 order = NULL;
1678 nnodes = 0;
1681 /* Helper function to perform function body dump. */
1683 static void
1684 execute_function_dump (function *fn, void *data)
1686 opt_pass *pass = (opt_pass *)data;
1688 if (dump_file)
1690 push_cfun (fn);
1692 if (fn->curr_properties & PROP_trees)
1693 dump_function_to_file (fn->decl, dump_file, dump_flags);
1694 else
1695 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1697 /* Flush the file. If verification fails, we won't be able to
1698 close the file before aborting. */
1699 fflush (dump_file);
1701 if ((fn->curr_properties & PROP_cfg)
1702 && (dump_flags & TDF_GRAPH))
1704 gcc::dump_manager *dumps = g->get_dumps ();
1705 struct dump_file_info *dfi
1706 = dumps->get_dump_file_info (pass->static_pass_number);
1707 if (!dfi->graph_dump_initialized)
1709 clean_graph_dump_file (dump_file_name);
1710 dfi->graph_dump_initialized = true;
1712 print_graph_cfg (dump_file_name, fn);
1715 pop_cfun ();
1719 /* This function is called when an internal compiler error is encountered.
1720 Ensure that function dump is made available before compiler is aborted. */
1722 void
1723 emergency_dump_function ()
1725 if (!current_pass)
1726 return;
1727 enum opt_pass_type pt = current_pass->type;
1728 fnotice (stderr, "during %s pass: %s\n",
1729 pt == GIMPLE_PASS ? "GIMPLE" : pt == RTL_PASS ? "RTL" : "IPA",
1730 current_pass->name);
1731 if (!dump_file || !cfun)
1732 return;
1733 fnotice (stderr, "dump file: %s\n", dump_file_name);
1734 fprintf (dump_file, "\n\n\nEMERGENCY DUMP:\n\n");
1735 execute_function_dump (cfun, current_pass);
1738 static struct profile_record *profile_record;
1740 /* Do profile consistency book-keeping for the pass with static number INDEX.
1741 RUN is true if the pass really runs, or FALSE
1742 if we are only book-keeping on passes that may have selectively disabled
1743 themselves on a given function. */
1745 static void
1746 check_profile_consistency (int index, bool run)
1748 pass_manager *passes = g->get_passes ();
1749 if (index == -1)
1750 return;
1751 if (!profile_record)
1752 profile_record = XCNEWVEC (struct profile_record,
1753 passes->passes_by_id_size);
1754 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1755 profile_record[index].run |= run;
1756 profile_record_check_consistency (&profile_record[index]);
1759 /* Account profile the pass with static number INDEX.
1760 RUN is true if the pass really runs, or FALSE
1761 if we are only book-keeping on passes that may have selectively disabled
1762 themselves on a given function. */
1764 static void
1765 account_profile (int index, bool run)
1767 pass_manager *passes = g->get_passes ();
1768 if (index == -1)
1769 return;
1770 if (!profile_record)
1771 profile_record = XCNEWVEC (struct profile_record,
1772 passes->passes_by_id_size);
1773 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1774 profile_record[index].run |= run;
1775 profile_record_account_profile (&profile_record[index]);
1778 /* Output profile consistency. */
1780 void
1781 dump_profile_report (void)
1783 g->get_passes ()->dump_profile_report ();
1786 void
1787 pass_manager::dump_profile_report () const
1789 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1790 gcov_type last_time = 0, last_size = 0;
1791 double rel_time_change, rel_size_change;
1792 int last_reported = 0;
1794 if (!profile_record)
1795 return;
1796 fprintf (stderr, "\nProfile consistency report:\n\n");
1797 fprintf (stderr, " |mismatch |mismatch | |\n");
1798 fprintf (stderr, "Pass name |IN |IN |OUT |OUT |overall |\n");
1799 fprintf (stderr, " |freq |count |freq |count |size |time |\n");
1801 for (int i = 1; i < passes_by_id_size; i++)
1802 if (profile_record[i].run)
1804 if (last_time)
1805 rel_time_change = (profile_record[i].time
1806 - (double)last_time) * 100 / (double)last_time;
1807 else
1808 rel_time_change = 0;
1809 if (last_size)
1810 rel_size_change = (profile_record[i].size
1811 - (double)last_size) * 100 / (double)last_size;
1812 else
1813 rel_size_change = 0;
1815 if (profile_record[i].num_mismatched_freq_in != last_freq_in
1816 || profile_record[i].num_mismatched_freq_out != last_freq_out
1817 || profile_record[i].num_mismatched_count_in != last_count_in
1818 || profile_record[i].num_mismatched_count_out != last_count_out
1819 || rel_time_change || rel_size_change)
1821 last_reported = i;
1822 fprintf (stderr, "%-33s", passes_by_id[i]->name);
1823 if (profile_record[i].num_mismatched_freq_in != last_freq_in)
1824 fprintf (stderr, "| %+5i",
1825 profile_record[i].num_mismatched_freq_in
1826 - last_freq_in);
1827 else
1828 fprintf (stderr, "| ");
1829 if (profile_record[i].num_mismatched_count_in != last_count_in)
1830 fprintf (stderr, "| %+5i",
1831 profile_record[i].num_mismatched_count_in
1832 - last_count_in);
1833 else
1834 fprintf (stderr, "| ");
1835 if (profile_record[i].num_mismatched_freq_out != last_freq_out)
1836 fprintf (stderr, "| %+5i",
1837 profile_record[i].num_mismatched_freq_out
1838 - last_freq_out);
1839 else
1840 fprintf (stderr, "| ");
1841 if (profile_record[i].num_mismatched_count_out != last_count_out)
1842 fprintf (stderr, "| %+5i",
1843 profile_record[i].num_mismatched_count_out
1844 - last_count_out);
1845 else
1846 fprintf (stderr, "| ");
1848 /* Size/time units change across gimple and RTL. */
1849 if (i == pass_expand_1->static_pass_number)
1850 fprintf (stderr, "|----------|----------");
1851 else
1853 if (rel_size_change)
1854 fprintf (stderr, "| %+8.1f%%", rel_size_change);
1855 else
1856 fprintf (stderr, "| ");
1857 if (rel_time_change)
1858 fprintf (stderr, "| %+8.1f%%", rel_time_change);
1859 else
1860 fprintf (stderr, "| ");
1862 fprintf (stderr, "|\n");
1863 last_freq_in = profile_record[i].num_mismatched_freq_in;
1864 last_freq_out = profile_record[i].num_mismatched_freq_out;
1865 last_count_in = profile_record[i].num_mismatched_count_in;
1866 last_count_out = profile_record[i].num_mismatched_count_out;
1868 else if (last_reported != i)
1870 last_reported = i;
1871 fprintf (stderr, "%-20s ------------| | | | | | |\n",
1872 passes_by_id[i]->name);
1874 last_time = profile_record[i].time;
1875 last_size = profile_record[i].size;
1879 /* Perform all TODO actions that ought to be done on each function. */
1881 static void
1882 execute_function_todo (function *fn, void *data)
1884 bool from_ipa_pass = (cfun == NULL);
1885 unsigned int flags = (size_t)data;
1886 flags &= ~fn->last_verified;
1887 if (!flags)
1888 return;
1890 push_cfun (fn);
1892 /* Always cleanup the CFG before trying to update SSA. */
1893 if (flags & TODO_cleanup_cfg)
1895 cleanup_tree_cfg ();
1897 /* When cleanup_tree_cfg merges consecutive blocks, it may
1898 perform some simplistic propagation when removing single
1899 valued PHI nodes. This propagation may, in turn, cause the
1900 SSA form to become out-of-date (see PR 22037). So, even
1901 if the parent pass had not scheduled an SSA update, we may
1902 still need to do one. */
1903 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1904 flags |= TODO_update_ssa;
1907 if (flags & TODO_update_ssa_any)
1909 unsigned update_flags = flags & TODO_update_ssa_any;
1910 update_ssa (update_flags);
1913 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1914 compute_may_aliases ();
1916 if (optimize && (flags & TODO_update_address_taken))
1917 execute_update_addresses_taken ();
1919 if (flags & TODO_remove_unused_locals)
1920 remove_unused_locals ();
1922 if (flags & TODO_rebuild_frequencies)
1923 rebuild_frequencies ();
1925 if (flags & TODO_rebuild_cgraph_edges)
1926 cgraph_edge::rebuild_edges ();
1928 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1929 /* If we've seen errors do not bother running any verifiers. */
1930 if (flag_checking && !seen_error ())
1932 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1933 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1935 if (flags & TODO_verify_il)
1937 if (cfun->curr_properties & PROP_trees)
1939 if (cfun->curr_properties & PROP_cfg)
1940 /* IPA passes leave stmts to be fixed up, so make sure to
1941 not verify stmts really throw. */
1942 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1943 else
1944 verify_gimple_in_seq (gimple_body (cfun->decl));
1946 if (cfun->curr_properties & PROP_ssa)
1947 /* IPA passes leave stmts to be fixed up, so make sure to
1948 not verify SSA operands whose verifier will choke on that. */
1949 verify_ssa (true, !from_ipa_pass);
1950 /* IPA passes leave basic-blocks unsplit, so make sure to
1951 not trip on that. */
1952 if ((cfun->curr_properties & PROP_cfg)
1953 && !from_ipa_pass)
1954 verify_flow_info ();
1955 if (current_loops
1956 && ! loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1958 verify_loop_structure ();
1959 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1960 verify_loop_closed_ssa (false);
1962 if (cfun->curr_properties & PROP_rtl)
1963 verify_rtl_sharing ();
1966 /* Make sure verifiers don't change dominator state. */
1967 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1968 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1971 fn->last_verified = flags & TODO_verify_all;
1973 pop_cfun ();
1975 /* For IPA passes make sure to release dominator info, it can be
1976 computed by non-verifying TODOs. */
1977 if (from_ipa_pass)
1979 free_dominance_info (fn, CDI_DOMINATORS);
1980 free_dominance_info (fn, CDI_POST_DOMINATORS);
1984 /* Perform all TODO actions. */
1985 static void
1986 execute_todo (unsigned int flags)
1988 if (flag_checking
1989 && cfun
1990 && need_ssa_update_p (cfun))
1991 gcc_assert (flags & TODO_update_ssa_any);
1993 statistics_fini_pass ();
1995 if (flags)
1996 do_per_function (execute_function_todo, (void *)(size_t) flags);
1998 /* At this point we should not have any unreachable code in the
1999 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
2000 if (cfun && cfun->gimple_df)
2001 flush_ssaname_freelist ();
2003 /* Always remove functions just as before inlining: IPA passes might be
2004 interested to see bodies of extern inline functions that are not inlined
2005 to analyze side effects. The full removal is done just at the end
2006 of IPA pass queue. */
2007 if (flags & TODO_remove_functions)
2009 gcc_assert (!cfun);
2010 symtab->remove_unreachable_nodes (dump_file);
2013 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2015 gcc_assert (!cfun);
2016 symtab->dump (dump_file);
2017 /* Flush the file. If verification fails, we won't be able to
2018 close the file before aborting. */
2019 fflush (dump_file);
2022 /* Now that the dumping has been done, we can get rid of the optional
2023 df problems. */
2024 if (flags & TODO_df_finish)
2025 df_finish_pass ((flags & TODO_df_verify) != 0);
2028 /* Verify invariants that should hold between passes. This is a place
2029 to put simple sanity checks. */
2031 static void
2032 verify_interpass_invariants (void)
2034 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2037 /* Clear the last verified flag. */
2039 static void
2040 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2042 fn->last_verified = 0;
2045 /* Helper function. Verify that the properties has been turn into the
2046 properties expected by the pass. */
2048 static void
2049 verify_curr_properties (function *fn, void *data)
2051 unsigned int props = (size_t)data;
2052 gcc_assert ((fn->curr_properties & props) == props);
2055 /* Release dump file name if set. */
2057 static void
2058 release_dump_file_name (void)
2060 if (dump_file_name)
2062 free (CONST_CAST (char *, dump_file_name));
2063 dump_file_name = NULL;
2067 /* Initialize pass dump file. */
2068 /* This is non-static so that the plugins can use it. */
2070 bool
2071 pass_init_dump_file (opt_pass *pass)
2073 /* If a dump file name is present, open it if enabled. */
2074 if (pass->static_pass_number != -1)
2076 timevar_push (TV_DUMP);
2077 gcc::dump_manager *dumps = g->get_dumps ();
2078 bool initializing_dump =
2079 !dumps->dump_initialized_p (pass->static_pass_number);
2080 release_dump_file_name ();
2081 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2082 dumps->dump_start (pass->static_pass_number, &dump_flags);
2083 if (dump_file && current_function_decl && ! (dump_flags & TDF_GIMPLE))
2084 dump_function_header (dump_file, current_function_decl, dump_flags);
2085 if (initializing_dump
2086 && dump_file && (dump_flags & TDF_GRAPH)
2087 && cfun && (cfun->curr_properties & PROP_cfg))
2089 clean_graph_dump_file (dump_file_name);
2090 struct dump_file_info *dfi
2091 = dumps->get_dump_file_info (pass->static_pass_number);
2092 dfi->graph_dump_initialized = true;
2094 timevar_pop (TV_DUMP);
2095 return initializing_dump;
2097 else
2098 return false;
2101 /* Flush PASS dump file. */
2102 /* This is non-static so that plugins can use it. */
2104 void
2105 pass_fini_dump_file (opt_pass *pass)
2107 timevar_push (TV_DUMP);
2109 /* Flush and close dump file. */
2110 release_dump_file_name ();
2112 g->get_dumps ()->dump_finish (pass->static_pass_number);
2113 timevar_pop (TV_DUMP);
2116 /* After executing the pass, apply expected changes to the function
2117 properties. */
2119 static void
2120 update_properties_after_pass (function *fn, void *data)
2122 opt_pass *pass = (opt_pass *) data;
2123 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2124 & ~pass->properties_destroyed;
2127 /* Execute summary generation for all of the passes in IPA_PASS. */
2129 void
2130 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2132 while (ipa_pass)
2134 opt_pass *pass = ipa_pass;
2136 /* Execute all of the IPA_PASSes in the list. */
2137 if (ipa_pass->type == IPA_PASS
2138 && pass->gate (cfun)
2139 && ipa_pass->generate_summary)
2141 pass_init_dump_file (pass);
2143 /* If a timevar is present, start it. */
2144 if (pass->tv_id)
2145 timevar_push (pass->tv_id);
2147 current_pass = pass;
2148 ipa_pass->generate_summary ();
2150 /* Stop timevar. */
2151 if (pass->tv_id)
2152 timevar_pop (pass->tv_id);
2154 pass_fini_dump_file (pass);
2156 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2160 /* Execute IPA_PASS function transform on NODE. */
2162 static void
2163 execute_one_ipa_transform_pass (struct cgraph_node *node,
2164 ipa_opt_pass_d *ipa_pass)
2166 opt_pass *pass = ipa_pass;
2167 unsigned int todo_after = 0;
2169 current_pass = pass;
2170 if (!ipa_pass->function_transform)
2171 return;
2173 /* Note that the folders should only create gimple expressions.
2174 This is a hack until the new folder is ready. */
2175 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2177 pass_init_dump_file (pass);
2179 /* If a timevar is present, start it. */
2180 if (pass->tv_id != TV_NONE)
2181 timevar_push (pass->tv_id);
2183 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2184 check_profile_consistency (pass->static_pass_number, true);
2186 /* Run pre-pass verification. */
2187 execute_todo (ipa_pass->function_transform_todo_flags_start);
2189 /* Do it! */
2190 todo_after = ipa_pass->function_transform (node);
2192 /* Run post-pass cleanup and verification. */
2193 execute_todo (todo_after);
2194 verify_interpass_invariants ();
2195 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2196 account_profile (pass->static_pass_number, true);
2198 /* Stop timevar. */
2199 if (pass->tv_id != TV_NONE)
2200 timevar_pop (pass->tv_id);
2202 if (dump_file)
2203 do_per_function (execute_function_dump, pass);
2204 pass_fini_dump_file (pass);
2206 current_pass = NULL;
2207 redirect_edge_var_map_empty ();
2209 /* Signal this is a suitable GC collection point. */
2210 if (!(todo_after & TODO_do_not_ggc_collect))
2211 ggc_collect ();
2214 /* For the current function, execute all ipa transforms. */
2216 void
2217 execute_all_ipa_transforms (void)
2219 struct cgraph_node *node;
2220 if (!cfun)
2221 return;
2222 node = cgraph_node::get (current_function_decl);
2224 if (node->ipa_transforms_to_apply.exists ())
2226 unsigned int i;
2228 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2229 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2230 node->ipa_transforms_to_apply.release ();
2234 /* Check if PASS is explicitly disabled or enabled and return
2235 the gate status. FUNC is the function to be processed, and
2236 GATE_STATUS is the gate status determined by pass manager by
2237 default. */
2239 static bool
2240 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2242 bool explicitly_enabled = false;
2243 bool explicitly_disabled = false;
2245 explicitly_enabled
2246 = is_pass_explicitly_enabled_or_disabled (pass, func,
2247 enabled_pass_uid_range_tab);
2248 explicitly_disabled
2249 = is_pass_explicitly_enabled_or_disabled (pass, func,
2250 disabled_pass_uid_range_tab);
2252 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2254 return gate_status;
2257 /* Determine if PASS_NAME matches CRITERION.
2258 Not a pure predicate, since it can update CRITERION, to support
2259 matching the Nth invocation of a pass.
2260 Subroutine of should_skip_pass_p. */
2262 static bool
2263 determine_pass_name_match (const char *pass_name, char *criterion)
2265 size_t namelen = strlen (pass_name);
2266 if (! strncmp (pass_name, criterion, namelen))
2268 /* The following supports starting with the Nth invocation
2269 of a pass (where N does not necessarily is equal to the
2270 dump file suffix). */
2271 if (criterion[namelen] == '\0'
2272 || (criterion[namelen] == '1'
2273 && criterion[namelen + 1] == '\0'))
2274 return true;
2275 else
2277 if (criterion[namelen + 1] == '\0')
2278 --criterion[namelen];
2279 return false;
2282 else
2283 return false;
2286 /* For skipping passes until "startwith" pass.
2287 Return true iff PASS should be skipped.
2288 Clear cfun->pass_startwith when encountering the "startwith" pass,
2289 so that all subsequent passes are run. */
2291 static bool
2292 should_skip_pass_p (opt_pass *pass)
2294 if (!cfun)
2295 return false;
2296 if (!cfun->pass_startwith)
2297 return false;
2299 /* For __GIMPLE functions, we have to at least start when we leave
2300 SSA. Hence, we need to detect the "expand" pass, and stop skipping
2301 when we encounter it. A cheap way to identify "expand" is it to
2302 detect the destruction of PROP_ssa.
2303 For __RTL functions, we invoke "rest_of_compilation" directly, which
2304 is after "expand", and hence we don't reach this conditional. */
2305 if (pass->properties_destroyed & PROP_ssa)
2307 if (!quiet_flag)
2308 fprintf (stderr, "starting anyway when leaving SSA: %s\n", pass->name);
2309 cfun->pass_startwith = NULL;
2310 return false;
2313 if (determine_pass_name_match (pass->name, cfun->pass_startwith))
2315 if (!quiet_flag)
2316 fprintf (stderr, "found starting pass: %s\n", pass->name);
2317 cfun->pass_startwith = NULL;
2318 return false;
2321 /* For GIMPLE passes, run any property provider (but continue skipping
2322 afterwards).
2323 We don't want to force running RTL passes that are property providers:
2324 "expand" is covered above, and the only pass other than "expand" that
2325 provides a property is "into_cfglayout" (PROP_cfglayout), which does
2326 too much for a dumped __RTL function. */
2327 if (pass->type == GIMPLE_PASS
2328 && pass->properties_provided != 0)
2329 return false;
2331 /* Don't skip df init; later RTL passes need it. */
2332 if (strstr (pass->name, "dfinit") != NULL)
2333 return false;
2335 if (!quiet_flag)
2336 fprintf (stderr, "skipping pass: %s\n", pass->name);
2338 /* If we get here, then we have a "startwith" that we haven't seen yet;
2339 skip the pass. */
2340 return true;
2343 /* Skip the given pass, for handling passes before "startwith"
2344 in __GIMPLE and__RTL-marked functions.
2345 In theory, this ought to be a no-op, but some of the RTL passes
2346 need additional processing here. */
2348 static void
2349 skip_pass (opt_pass *pass)
2351 /* Pass "reload" sets the global "reload_completed", and many
2352 things depend on this (e.g. instructions in .md files). */
2353 if (strcmp (pass->name, "reload") == 0)
2354 reload_completed = 1;
2356 /* The INSN_ADDRESSES vec is normally set up by
2357 shorten_branches; set it up for the benefit of passes that
2358 run after this. */
2359 if (strcmp (pass->name, "shorten") == 0)
2360 INSN_ADDRESSES_ALLOC (get_max_uid ());
2362 /* Update the cfg hooks as appropriate. */
2363 if (strcmp (pass->name, "into_cfglayout") == 0)
2365 cfg_layout_rtl_register_cfg_hooks ();
2366 cfun->curr_properties |= PROP_cfglayout;
2368 if (strcmp (pass->name, "outof_cfglayout") == 0)
2370 rtl_register_cfg_hooks ();
2371 cfun->curr_properties &= ~PROP_cfglayout;
2375 /* Execute PASS. */
2377 bool
2378 execute_one_pass (opt_pass *pass)
2380 unsigned int todo_after = 0;
2382 bool gate_status;
2384 /* IPA passes are executed on whole program, so cfun should be NULL.
2385 Other passes need function context set. */
2386 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2387 gcc_assert (!cfun && !current_function_decl);
2388 else
2389 gcc_assert (cfun && current_function_decl);
2391 current_pass = pass;
2393 /* Check whether gate check should be avoided.
2394 User controls the value of the gate through the parameter "gate_status". */
2395 gate_status = pass->gate (cfun);
2396 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2398 /* Override gate with plugin. */
2399 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2401 if (!gate_status)
2403 /* Run so passes selectively disabling themselves on a given function
2404 are not miscounted. */
2405 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2407 check_profile_consistency (pass->static_pass_number, false);
2408 account_profile (pass->static_pass_number, false);
2410 current_pass = NULL;
2411 return false;
2414 if (should_skip_pass_p (pass))
2416 skip_pass (pass);
2417 return true;
2420 /* Pass execution event trigger: useful to identify passes being
2421 executed. */
2422 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2424 if (!quiet_flag && !cfun)
2425 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2427 /* Note that the folders should only create gimple expressions.
2428 This is a hack until the new folder is ready. */
2429 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2431 pass_init_dump_file (pass);
2433 /* If a timevar is present, start it. */
2434 if (pass->tv_id != TV_NONE)
2435 timevar_push (pass->tv_id);
2437 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2438 check_profile_consistency (pass->static_pass_number, true);
2440 /* Run pre-pass verification. */
2441 execute_todo (pass->todo_flags_start);
2443 if (flag_checking)
2444 do_per_function (verify_curr_properties,
2445 (void *)(size_t)pass->properties_required);
2447 /* Do it! */
2448 todo_after = pass->execute (cfun);
2450 if (todo_after & TODO_discard_function)
2452 /* Stop timevar. */
2453 if (pass->tv_id != TV_NONE)
2454 timevar_pop (pass->tv_id);
2456 pass_fini_dump_file (pass);
2458 gcc_assert (cfun);
2459 /* As cgraph_node::release_body expects release dominators info,
2460 we have to release it. */
2461 if (dom_info_available_p (CDI_DOMINATORS))
2462 free_dominance_info (CDI_DOMINATORS);
2464 if (dom_info_available_p (CDI_POST_DOMINATORS))
2465 free_dominance_info (CDI_POST_DOMINATORS);
2467 tree fn = cfun->decl;
2468 pop_cfun ();
2469 gcc_assert (!cfun);
2470 cgraph_node::get (fn)->release_body ();
2472 current_pass = NULL;
2473 redirect_edge_var_map_empty ();
2475 ggc_collect ();
2477 return true;
2480 do_per_function (clear_last_verified, NULL);
2482 do_per_function (update_properties_after_pass, pass);
2484 /* Run post-pass cleanup and verification. */
2485 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2486 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2487 account_profile (pass->static_pass_number, true);
2489 verify_interpass_invariants ();
2491 /* Stop timevar. */
2492 if (pass->tv_id != TV_NONE)
2493 timevar_pop (pass->tv_id);
2495 if (pass->type == IPA_PASS
2496 && ((ipa_opt_pass_d *)pass)->function_transform)
2498 struct cgraph_node *node;
2499 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2500 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2502 else if (dump_file)
2503 do_per_function (execute_function_dump, pass);
2505 if (!current_function_decl)
2506 symtab->process_new_functions ();
2508 pass_fini_dump_file (pass);
2510 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2511 gcc_assert (!(cfun->curr_properties & PROP_trees)
2512 || pass->type != RTL_PASS);
2514 current_pass = NULL;
2515 redirect_edge_var_map_empty ();
2517 /* Signal this is a suitable GC collection point. */
2518 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2519 ggc_collect ();
2521 return true;
2524 static void
2525 execute_pass_list_1 (opt_pass *pass)
2529 gcc_assert (pass->type == GIMPLE_PASS
2530 || pass->type == RTL_PASS);
2532 if (cfun == NULL)
2533 return;
2534 if (execute_one_pass (pass) && pass->sub)
2535 execute_pass_list_1 (pass->sub);
2536 pass = pass->next;
2538 while (pass);
2541 void
2542 execute_pass_list (function *fn, opt_pass *pass)
2544 gcc_assert (fn == cfun);
2545 execute_pass_list_1 (pass);
2546 if (cfun && fn->cfg)
2548 free_dominance_info (CDI_DOMINATORS);
2549 free_dominance_info (CDI_POST_DOMINATORS);
2553 /* Write out all LTO data. */
2554 static void
2555 write_lto (void)
2557 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2558 lto_output ();
2559 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2560 timevar_push (TV_IPA_LTO_DECL_OUT);
2561 produce_asm_for_decls ();
2562 timevar_pop (TV_IPA_LTO_DECL_OUT);
2565 /* Same as execute_pass_list but assume that subpasses of IPA passes
2566 are local passes. If SET is not NULL, write out summaries of only
2567 those node in SET. */
2569 static void
2570 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2572 while (pass)
2574 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2575 gcc_assert (!current_function_decl);
2576 gcc_assert (!cfun);
2577 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2578 if (pass->type == IPA_PASS
2579 && ipa_pass->write_summary
2580 && pass->gate (cfun))
2582 /* If a timevar is present, start it. */
2583 if (pass->tv_id)
2584 timevar_push (pass->tv_id);
2586 pass_init_dump_file (pass);
2588 current_pass = pass;
2589 ipa_pass->write_summary ();
2591 pass_fini_dump_file (pass);
2593 /* If a timevar is present, start it. */
2594 if (pass->tv_id)
2595 timevar_pop (pass->tv_id);
2598 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2599 ipa_write_summaries_2 (pass->sub, state);
2601 pass = pass->next;
2605 /* Helper function of ipa_write_summaries. Creates and destroys the
2606 decl state and calls ipa_write_summaries_2 for all passes that have
2607 summaries. SET is the set of nodes to be written. */
2609 static void
2610 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2612 pass_manager *passes = g->get_passes ();
2613 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2614 state->symtab_node_encoder = encoder;
2616 lto_output_init_mode_table ();
2617 lto_push_out_decl_state (state);
2619 gcc_assert (!flag_wpa);
2620 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2622 write_lto ();
2624 gcc_assert (lto_get_out_decl_state () == state);
2625 lto_pop_out_decl_state ();
2626 lto_delete_out_decl_state (state);
2629 /* Write out summaries for all the nodes in the callgraph. */
2631 void
2632 ipa_write_summaries (void)
2634 lto_symtab_encoder_t encoder;
2635 int i, order_pos;
2636 varpool_node *vnode;
2637 struct cgraph_node *node;
2638 struct cgraph_node **order;
2640 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2641 return;
2643 gcc_assert (!dump_file);
2644 streamer_dump_file = dump_begin (TDI_lto_stream_out, NULL);
2646 select_what_to_stream ();
2648 encoder = lto_symtab_encoder_new (false);
2650 /* Create the callgraph set in the same order used in
2651 cgraph_expand_all_functions. This mostly facilitates debugging,
2652 since it causes the gimple file to be processed in the same order
2653 as the source code. */
2654 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2655 order_pos = ipa_reverse_postorder (order);
2656 gcc_assert (order_pos == symtab->cgraph_count);
2658 for (i = order_pos - 1; i >= 0; i--)
2660 struct cgraph_node *node = order[i];
2662 if (gimple_has_body_p (node->decl))
2664 /* When streaming out references to statements as part of some IPA
2665 pass summary, the statements need to have uids assigned and the
2666 following does that for all the IPA passes here. Naturally, this
2667 ordering then matches the one IPA-passes get in their stmt_fixup
2668 hooks. */
2670 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2671 renumber_gimple_stmt_uids ();
2672 pop_cfun ();
2674 if (node->definition && node->need_lto_streaming)
2675 lto_set_symtab_encoder_in_partition (encoder, node);
2678 FOR_EACH_DEFINED_FUNCTION (node)
2679 if (node->alias && node->need_lto_streaming)
2680 lto_set_symtab_encoder_in_partition (encoder, node);
2681 FOR_EACH_DEFINED_VARIABLE (vnode)
2682 if (vnode->need_lto_streaming)
2683 lto_set_symtab_encoder_in_partition (encoder, vnode);
2685 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2687 free (order);
2688 if (streamer_dump_file)
2690 dump_end (TDI_lto_stream_out, streamer_dump_file);
2691 streamer_dump_file = NULL;
2695 /* Same as execute_pass_list but assume that subpasses of IPA passes
2696 are local passes. If SET is not NULL, write out optimization summaries of
2697 only those node in SET. */
2699 static void
2700 ipa_write_optimization_summaries_1 (opt_pass *pass,
2701 struct lto_out_decl_state *state)
2703 while (pass)
2705 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2706 gcc_assert (!current_function_decl);
2707 gcc_assert (!cfun);
2708 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2709 if (pass->type == IPA_PASS
2710 && ipa_pass->write_optimization_summary
2711 && pass->gate (cfun))
2713 /* If a timevar is present, start it. */
2714 if (pass->tv_id)
2715 timevar_push (pass->tv_id);
2717 pass_init_dump_file (pass);
2719 current_pass = pass;
2720 ipa_pass->write_optimization_summary ();
2722 pass_fini_dump_file (pass);
2724 /* If a timevar is present, start it. */
2725 if (pass->tv_id)
2726 timevar_pop (pass->tv_id);
2729 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2730 ipa_write_optimization_summaries_1 (pass->sub, state);
2732 pass = pass->next;
2736 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2737 NULL, write out all summaries of all nodes. */
2739 void
2740 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2742 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2743 lto_symtab_encoder_iterator lsei;
2744 state->symtab_node_encoder = encoder;
2746 lto_output_init_mode_table ();
2747 lto_push_out_decl_state (state);
2748 for (lsei = lsei_start_function_in_partition (encoder);
2749 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2751 struct cgraph_node *node = lsei_cgraph_node (lsei);
2752 /* When streaming out references to statements as part of some IPA
2753 pass summary, the statements need to have uids assigned.
2755 For functions newly born at WPA stage we need to initialize
2756 the uids here. */
2757 if (node->definition
2758 && gimple_has_body_p (node->decl))
2760 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2761 renumber_gimple_stmt_uids ();
2762 pop_cfun ();
2766 gcc_assert (flag_wpa);
2767 pass_manager *passes = g->get_passes ();
2768 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2770 write_lto ();
2772 gcc_assert (lto_get_out_decl_state () == state);
2773 lto_pop_out_decl_state ();
2774 lto_delete_out_decl_state (state);
2777 /* Same as execute_pass_list but assume that subpasses of IPA passes
2778 are local passes. */
2780 static void
2781 ipa_read_summaries_1 (opt_pass *pass)
2783 while (pass)
2785 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2787 gcc_assert (!current_function_decl);
2788 gcc_assert (!cfun);
2789 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2791 if (pass->gate (cfun))
2793 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2795 /* If a timevar is present, start it. */
2796 if (pass->tv_id)
2797 timevar_push (pass->tv_id);
2799 pass_init_dump_file (pass);
2801 current_pass = pass;
2802 ipa_pass->read_summary ();
2804 pass_fini_dump_file (pass);
2806 /* Stop timevar. */
2807 if (pass->tv_id)
2808 timevar_pop (pass->tv_id);
2811 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2812 ipa_read_summaries_1 (pass->sub);
2814 pass = pass->next;
2819 /* Read all the summaries for all_regular_ipa_passes. */
2821 void
2822 ipa_read_summaries (void)
2824 pass_manager *passes = g->get_passes ();
2825 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2828 /* Same as execute_pass_list but assume that subpasses of IPA passes
2829 are local passes. */
2831 static void
2832 ipa_read_optimization_summaries_1 (opt_pass *pass)
2834 while (pass)
2836 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2838 gcc_assert (!current_function_decl);
2839 gcc_assert (!cfun);
2840 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2842 if (pass->gate (cfun))
2844 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2846 /* If a timevar is present, start it. */
2847 if (pass->tv_id)
2848 timevar_push (pass->tv_id);
2850 pass_init_dump_file (pass);
2852 current_pass = pass;
2853 ipa_pass->read_optimization_summary ();
2855 pass_fini_dump_file (pass);
2857 /* Stop timevar. */
2858 if (pass->tv_id)
2859 timevar_pop (pass->tv_id);
2862 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2863 ipa_read_optimization_summaries_1 (pass->sub);
2865 pass = pass->next;
2869 /* Read all the summaries for all_regular_ipa_passes. */
2871 void
2872 ipa_read_optimization_summaries (void)
2874 pass_manager *passes = g->get_passes ();
2875 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2878 /* Same as execute_pass_list but assume that subpasses of IPA passes
2879 are local passes. */
2880 void
2881 execute_ipa_pass_list (opt_pass *pass)
2885 gcc_assert (!current_function_decl);
2886 gcc_assert (!cfun);
2887 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2888 if (execute_one_pass (pass) && pass->sub)
2890 if (pass->sub->type == GIMPLE_PASS)
2892 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2893 do_per_function_toporder ((void (*)(function *, void *))
2894 execute_pass_list,
2895 pass->sub);
2896 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2898 else if (pass->sub->type == SIMPLE_IPA_PASS
2899 || pass->sub->type == IPA_PASS)
2900 execute_ipa_pass_list (pass->sub);
2901 else
2902 gcc_unreachable ();
2904 gcc_assert (!current_function_decl);
2905 symtab->process_new_functions ();
2906 pass = pass->next;
2908 while (pass);
2911 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2913 static void
2914 execute_ipa_stmt_fixups (opt_pass *pass,
2915 struct cgraph_node *node, gimple **stmts)
2917 while (pass)
2919 /* Execute all of the IPA_PASSes in the list. */
2920 if (pass->type == IPA_PASS
2921 && pass->gate (cfun))
2923 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2925 if (ipa_pass->stmt_fixup)
2927 pass_init_dump_file (pass);
2928 /* If a timevar is present, start it. */
2929 if (pass->tv_id)
2930 timevar_push (pass->tv_id);
2932 current_pass = pass;
2933 ipa_pass->stmt_fixup (node, stmts);
2935 /* Stop timevar. */
2936 if (pass->tv_id)
2937 timevar_pop (pass->tv_id);
2938 pass_fini_dump_file (pass);
2940 if (pass->sub)
2941 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2943 pass = pass->next;
2947 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2949 void
2950 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2952 pass_manager *passes = g->get_passes ();
2953 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2957 extern void debug_properties (unsigned int);
2958 extern void dump_properties (FILE *, unsigned int);
2960 DEBUG_FUNCTION void
2961 dump_properties (FILE *dump, unsigned int props)
2963 fprintf (dump, "Properties:\n");
2964 if (props & PROP_gimple_any)
2965 fprintf (dump, "PROP_gimple_any\n");
2966 if (props & PROP_gimple_lcf)
2967 fprintf (dump, "PROP_gimple_lcf\n");
2968 if (props & PROP_gimple_leh)
2969 fprintf (dump, "PROP_gimple_leh\n");
2970 if (props & PROP_cfg)
2971 fprintf (dump, "PROP_cfg\n");
2972 if (props & PROP_ssa)
2973 fprintf (dump, "PROP_ssa\n");
2974 if (props & PROP_no_crit_edges)
2975 fprintf (dump, "PROP_no_crit_edges\n");
2976 if (props & PROP_rtl)
2977 fprintf (dump, "PROP_rtl\n");
2978 if (props & PROP_gimple_lomp)
2979 fprintf (dump, "PROP_gimple_lomp\n");
2980 if (props & PROP_gimple_lomp_dev)
2981 fprintf (dump, "PROP_gimple_lomp_dev\n");
2982 if (props & PROP_gimple_lcx)
2983 fprintf (dump, "PROP_gimple_lcx\n");
2984 if (props & PROP_gimple_lvec)
2985 fprintf (dump, "PROP_gimple_lvec\n");
2986 if (props & PROP_cfglayout)
2987 fprintf (dump, "PROP_cfglayout\n");
2990 DEBUG_FUNCTION void
2991 debug_properties (unsigned int props)
2993 dump_properties (stderr, props);
2996 /* Called by local passes to see if function is called by already processed nodes.
2997 Because we process nodes in topological order, this means that function is
2998 in recursive cycle or we introduced new direct calls. */
2999 bool
3000 function_called_by_processed_nodes_p (void)
3002 struct cgraph_edge *e;
3003 for (e = cgraph_node::get (current_function_decl)->callers;
3005 e = e->next_caller)
3007 if (e->caller->decl == current_function_decl)
3008 continue;
3009 if (!e->caller->has_gimple_body_p ())
3010 continue;
3011 if (TREE_ASM_WRITTEN (e->caller->decl))
3012 continue;
3013 if (!e->caller->process && !e->caller->global.inlined_to)
3014 break;
3016 if (dump_file && e)
3018 fprintf (dump_file, "Already processed call to:\n");
3019 e->caller->dump (dump_file);
3021 return e != NULL;
3024 #include "gt-passes.h"