Fix build on sparc64-linux-gnu.
[official-gcc.git] / gcc / passes.c
blobd838d9099417b3b19e3c27b7bfa123888ae7341b
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 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1742 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1743 if we are only book-keeping on passes that may have selectively disabled
1744 themselves on a given function. */
1745 static void
1746 check_profile_consistency (int index, int subpass, 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 gcc_assert (subpass < 2);
1756 profile_record[index].run |= run;
1757 account_profile_record (&profile_record[index], subpass);
1760 /* Output profile consistency. */
1762 void
1763 dump_profile_report (void)
1765 g->get_passes ()->dump_profile_report ();
1768 void
1769 pass_manager::dump_profile_report () const
1771 int i, j;
1772 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1773 gcov_type last_time = 0, last_size = 0;
1774 double rel_time_change, rel_size_change;
1775 int last_reported = 0;
1777 if (!profile_record)
1778 return;
1779 fprintf (stderr, "\nProfile consistency report:\n\n");
1780 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1781 fprintf (stderr, " |freq count |freq count |size time\n");
1783 for (i = 0; i < passes_by_id_size; i++)
1784 for (j = 0 ; j < 2; j++)
1785 if (profile_record[i].run)
1787 if (last_time)
1788 rel_time_change = (profile_record[i].time[j]
1789 - (double)last_time) * 100 / (double)last_time;
1790 else
1791 rel_time_change = 0;
1792 if (last_size)
1793 rel_size_change = (profile_record[i].size[j]
1794 - (double)last_size) * 100 / (double)last_size;
1795 else
1796 rel_size_change = 0;
1798 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1799 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1800 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1801 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1802 || rel_time_change || rel_size_change)
1804 last_reported = i;
1805 fprintf (stderr, "%-20s %s",
1806 passes_by_id [i]->name,
1807 j ? "(after TODO)" : " ");
1808 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1809 fprintf (stderr, "| %+5i",
1810 profile_record[i].num_mismatched_freq_in[j]
1811 - last_freq_in);
1812 else
1813 fprintf (stderr, "| ");
1814 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1815 fprintf (stderr, " %+5i",
1816 profile_record[i].num_mismatched_count_in[j]
1817 - last_count_in);
1818 else
1819 fprintf (stderr, " ");
1820 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1821 fprintf (stderr, "| %+5i",
1822 profile_record[i].num_mismatched_freq_out[j]
1823 - last_freq_out);
1824 else
1825 fprintf (stderr, "| ");
1826 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1827 fprintf (stderr, " %+5i",
1828 profile_record[i].num_mismatched_count_out[j]
1829 - last_count_out);
1830 else
1831 fprintf (stderr, " ");
1833 /* Size/time units change across gimple and RTL. */
1834 if (i == pass_expand_1->static_pass_number)
1835 fprintf (stderr, "|----------");
1836 else
1838 if (rel_size_change)
1839 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1840 else
1841 fprintf (stderr, "| ");
1842 if (rel_time_change)
1843 fprintf (stderr, " %+8.4f%%", rel_time_change);
1845 fprintf (stderr, "\n");
1846 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1847 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1848 last_count_in = profile_record[i].num_mismatched_count_in[j];
1849 last_count_out = profile_record[i].num_mismatched_count_out[j];
1851 else if (j && last_reported != i)
1853 last_reported = i;
1854 fprintf (stderr, "%-20s ------------| | |\n",
1855 passes_by_id [i]->name);
1857 last_time = profile_record[i].time[j];
1858 last_size = profile_record[i].size[j];
1862 /* Perform all TODO actions that ought to be done on each function. */
1864 static void
1865 execute_function_todo (function *fn, void *data)
1867 bool from_ipa_pass = (cfun == NULL);
1868 unsigned int flags = (size_t)data;
1869 flags &= ~fn->last_verified;
1870 if (!flags)
1871 return;
1873 push_cfun (fn);
1875 /* Always cleanup the CFG before trying to update SSA. */
1876 if (flags & TODO_cleanup_cfg)
1878 cleanup_tree_cfg ();
1880 /* When cleanup_tree_cfg merges consecutive blocks, it may
1881 perform some simplistic propagation when removing single
1882 valued PHI nodes. This propagation may, in turn, cause the
1883 SSA form to become out-of-date (see PR 22037). So, even
1884 if the parent pass had not scheduled an SSA update, we may
1885 still need to do one. */
1886 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1887 flags |= TODO_update_ssa;
1890 if (flags & TODO_update_ssa_any)
1892 unsigned update_flags = flags & TODO_update_ssa_any;
1893 update_ssa (update_flags);
1896 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1897 compute_may_aliases ();
1899 if (optimize && (flags & TODO_update_address_taken))
1900 execute_update_addresses_taken ();
1902 if (flags & TODO_remove_unused_locals)
1903 remove_unused_locals ();
1905 if (flags & TODO_rebuild_frequencies)
1906 rebuild_frequencies ();
1908 if (flags & TODO_rebuild_cgraph_edges)
1909 cgraph_edge::rebuild_edges ();
1911 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1912 /* If we've seen errors do not bother running any verifiers. */
1913 if (flag_checking && !seen_error ())
1915 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1916 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1918 if (flags & TODO_verify_il)
1920 if (cfun->curr_properties & PROP_trees)
1922 if (cfun->curr_properties & PROP_cfg)
1923 /* IPA passes leave stmts to be fixed up, so make sure to
1924 not verify stmts really throw. */
1925 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1926 else
1927 verify_gimple_in_seq (gimple_body (cfun->decl));
1929 if (cfun->curr_properties & PROP_ssa)
1930 /* IPA passes leave stmts to be fixed up, so make sure to
1931 not verify SSA operands whose verifier will choke on that. */
1932 verify_ssa (true, !from_ipa_pass);
1933 /* IPA passes leave basic-blocks unsplit, so make sure to
1934 not trip on that. */
1935 if ((cfun->curr_properties & PROP_cfg)
1936 && !from_ipa_pass)
1937 verify_flow_info ();
1938 if (current_loops
1939 && ! loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1941 verify_loop_structure ();
1942 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1943 verify_loop_closed_ssa (false);
1945 if (cfun->curr_properties & PROP_rtl)
1946 verify_rtl_sharing ();
1949 /* Make sure verifiers don't change dominator state. */
1950 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1951 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1954 fn->last_verified = flags & TODO_verify_all;
1956 pop_cfun ();
1958 /* For IPA passes make sure to release dominator info, it can be
1959 computed by non-verifying TODOs. */
1960 if (from_ipa_pass)
1962 free_dominance_info (fn, CDI_DOMINATORS);
1963 free_dominance_info (fn, CDI_POST_DOMINATORS);
1967 /* Perform all TODO actions. */
1968 static void
1969 execute_todo (unsigned int flags)
1971 if (flag_checking
1972 && cfun
1973 && need_ssa_update_p (cfun))
1974 gcc_assert (flags & TODO_update_ssa_any);
1976 statistics_fini_pass ();
1978 if (flags)
1979 do_per_function (execute_function_todo, (void *)(size_t) flags);
1981 /* At this point we should not have any unreachable code in the
1982 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
1983 if (cfun && cfun->gimple_df)
1984 flush_ssaname_freelist ();
1986 /* Always remove functions just as before inlining: IPA passes might be
1987 interested to see bodies of extern inline functions that are not inlined
1988 to analyze side effects. The full removal is done just at the end
1989 of IPA pass queue. */
1990 if (flags & TODO_remove_functions)
1992 gcc_assert (!cfun);
1993 symtab->remove_unreachable_nodes (dump_file);
1996 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1998 gcc_assert (!cfun);
1999 symtab->dump (dump_file);
2000 /* Flush the file. If verification fails, we won't be able to
2001 close the file before aborting. */
2002 fflush (dump_file);
2005 /* Now that the dumping has been done, we can get rid of the optional
2006 df problems. */
2007 if (flags & TODO_df_finish)
2008 df_finish_pass ((flags & TODO_df_verify) != 0);
2011 /* Verify invariants that should hold between passes. This is a place
2012 to put simple sanity checks. */
2014 static void
2015 verify_interpass_invariants (void)
2017 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2020 /* Clear the last verified flag. */
2022 static void
2023 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2025 fn->last_verified = 0;
2028 /* Helper function. Verify that the properties has been turn into the
2029 properties expected by the pass. */
2031 static void
2032 verify_curr_properties (function *fn, void *data)
2034 unsigned int props = (size_t)data;
2035 gcc_assert ((fn->curr_properties & props) == props);
2038 /* Release dump file name if set. */
2040 static void
2041 release_dump_file_name (void)
2043 if (dump_file_name)
2045 free (CONST_CAST (char *, dump_file_name));
2046 dump_file_name = NULL;
2050 /* Initialize pass dump file. */
2051 /* This is non-static so that the plugins can use it. */
2053 bool
2054 pass_init_dump_file (opt_pass *pass)
2056 /* If a dump file name is present, open it if enabled. */
2057 if (pass->static_pass_number != -1)
2059 timevar_push (TV_DUMP);
2060 gcc::dump_manager *dumps = g->get_dumps ();
2061 bool initializing_dump =
2062 !dumps->dump_initialized_p (pass->static_pass_number);
2063 release_dump_file_name ();
2064 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2065 dumps->dump_start (pass->static_pass_number, &dump_flags);
2066 if (dump_file && current_function_decl && ! (dump_flags & TDF_GIMPLE))
2067 dump_function_header (dump_file, current_function_decl, dump_flags);
2068 if (initializing_dump
2069 && dump_file && (dump_flags & TDF_GRAPH)
2070 && cfun && (cfun->curr_properties & PROP_cfg))
2072 clean_graph_dump_file (dump_file_name);
2073 struct dump_file_info *dfi
2074 = dumps->get_dump_file_info (pass->static_pass_number);
2075 dfi->graph_dump_initialized = true;
2077 timevar_pop (TV_DUMP);
2078 return initializing_dump;
2080 else
2081 return false;
2084 /* Flush PASS dump file. */
2085 /* This is non-static so that plugins can use it. */
2087 void
2088 pass_fini_dump_file (opt_pass *pass)
2090 timevar_push (TV_DUMP);
2092 /* Flush and close dump file. */
2093 release_dump_file_name ();
2095 g->get_dumps ()->dump_finish (pass->static_pass_number);
2096 timevar_pop (TV_DUMP);
2099 /* After executing the pass, apply expected changes to the function
2100 properties. */
2102 static void
2103 update_properties_after_pass (function *fn, void *data)
2105 opt_pass *pass = (opt_pass *) data;
2106 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2107 & ~pass->properties_destroyed;
2110 /* Execute summary generation for all of the passes in IPA_PASS. */
2112 void
2113 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2115 while (ipa_pass)
2117 opt_pass *pass = ipa_pass;
2119 /* Execute all of the IPA_PASSes in the list. */
2120 if (ipa_pass->type == IPA_PASS
2121 && pass->gate (cfun)
2122 && ipa_pass->generate_summary)
2124 pass_init_dump_file (pass);
2126 /* If a timevar is present, start it. */
2127 if (pass->tv_id)
2128 timevar_push (pass->tv_id);
2130 current_pass = pass;
2131 ipa_pass->generate_summary ();
2133 /* Stop timevar. */
2134 if (pass->tv_id)
2135 timevar_pop (pass->tv_id);
2137 pass_fini_dump_file (pass);
2139 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2143 /* Execute IPA_PASS function transform on NODE. */
2145 static void
2146 execute_one_ipa_transform_pass (struct cgraph_node *node,
2147 ipa_opt_pass_d *ipa_pass)
2149 opt_pass *pass = ipa_pass;
2150 unsigned int todo_after = 0;
2152 current_pass = pass;
2153 if (!ipa_pass->function_transform)
2154 return;
2156 /* Note that the folders should only create gimple expressions.
2157 This is a hack until the new folder is ready. */
2158 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2160 pass_init_dump_file (pass);
2162 /* If a timevar is present, start it. */
2163 if (pass->tv_id != TV_NONE)
2164 timevar_push (pass->tv_id);
2166 /* Run pre-pass verification. */
2167 execute_todo (ipa_pass->function_transform_todo_flags_start);
2169 /* Do it! */
2170 todo_after = ipa_pass->function_transform (node);
2172 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2173 check_profile_consistency (pass->static_pass_number, 0, true);
2175 /* Run post-pass cleanup and verification. */
2176 execute_todo (todo_after);
2177 verify_interpass_invariants ();
2178 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2179 check_profile_consistency (pass->static_pass_number, 1, true);
2181 /* Stop timevar. */
2182 if (pass->tv_id != TV_NONE)
2183 timevar_pop (pass->tv_id);
2185 if (dump_file)
2186 do_per_function (execute_function_dump, pass);
2187 pass_fini_dump_file (pass);
2189 current_pass = NULL;
2190 redirect_edge_var_map_empty ();
2192 /* Signal this is a suitable GC collection point. */
2193 if (!(todo_after & TODO_do_not_ggc_collect))
2194 ggc_collect ();
2197 /* For the current function, execute all ipa transforms. */
2199 void
2200 execute_all_ipa_transforms (void)
2202 struct cgraph_node *node;
2203 if (!cfun)
2204 return;
2205 node = cgraph_node::get (current_function_decl);
2207 if (node->ipa_transforms_to_apply.exists ())
2209 unsigned int i;
2211 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2212 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2213 node->ipa_transforms_to_apply.release ();
2217 /* Check if PASS is explicitly disabled or enabled and return
2218 the gate status. FUNC is the function to be processed, and
2219 GATE_STATUS is the gate status determined by pass manager by
2220 default. */
2222 static bool
2223 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2225 bool explicitly_enabled = false;
2226 bool explicitly_disabled = false;
2228 explicitly_enabled
2229 = is_pass_explicitly_enabled_or_disabled (pass, func,
2230 enabled_pass_uid_range_tab);
2231 explicitly_disabled
2232 = is_pass_explicitly_enabled_or_disabled (pass, func,
2233 disabled_pass_uid_range_tab);
2235 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2237 return gate_status;
2240 /* Determine if PASS_NAME matches CRITERION.
2241 Not a pure predicate, since it can update CRITERION, to support
2242 matching the Nth invocation of a pass.
2243 Subroutine of should_skip_pass_p. */
2245 static bool
2246 determine_pass_name_match (const char *pass_name, char *criterion)
2248 size_t namelen = strlen (pass_name);
2249 if (! strncmp (pass_name, criterion, namelen))
2251 /* The following supports starting with the Nth invocation
2252 of a pass (where N does not necessarily is equal to the
2253 dump file suffix). */
2254 if (criterion[namelen] == '\0'
2255 || (criterion[namelen] == '1'
2256 && criterion[namelen + 1] == '\0'))
2257 return true;
2258 else
2260 if (criterion[namelen + 1] == '\0')
2261 --criterion[namelen];
2262 return false;
2265 else
2266 return false;
2269 /* For skipping passes until "startwith" pass.
2270 Return true iff PASS should be skipped.
2271 Clear cfun->pass_startwith when encountering the "startwith" pass,
2272 so that all subsequent passes are run. */
2274 static bool
2275 should_skip_pass_p (opt_pass *pass)
2277 if (!cfun)
2278 return false;
2279 if (!cfun->pass_startwith)
2280 return false;
2282 /* For __GIMPLE functions, we have to at least start when we leave
2283 SSA. Hence, we need to detect the "expand" pass, and stop skipping
2284 when we encounter it. A cheap way to identify "expand" is it to
2285 detect the destruction of PROP_ssa.
2286 For __RTL functions, we invoke "rest_of_compilation" directly, which
2287 is after "expand", and hence we don't reach this conditional. */
2288 if (pass->properties_destroyed & PROP_ssa)
2290 if (!quiet_flag)
2291 fprintf (stderr, "starting anyway when leaving SSA: %s\n", pass->name);
2292 cfun->pass_startwith = NULL;
2293 return false;
2296 if (determine_pass_name_match (pass->name, cfun->pass_startwith))
2298 if (!quiet_flag)
2299 fprintf (stderr, "found starting pass: %s\n", pass->name);
2300 cfun->pass_startwith = NULL;
2301 return false;
2304 /* For GIMPLE passes, run any property provider (but continue skipping
2305 afterwards).
2306 We don't want to force running RTL passes that are property providers:
2307 "expand" is covered above, and the only pass other than "expand" that
2308 provides a property is "into_cfglayout" (PROP_cfglayout), which does
2309 too much for a dumped __RTL function. */
2310 if (pass->type == GIMPLE_PASS
2311 && pass->properties_provided != 0)
2312 return false;
2314 /* Don't skip df init; later RTL passes need it. */
2315 if (strstr (pass->name, "dfinit") != NULL)
2316 return false;
2318 if (!quiet_flag)
2319 fprintf (stderr, "skipping pass: %s\n", pass->name);
2321 /* If we get here, then we have a "startwith" that we haven't seen yet;
2322 skip the pass. */
2323 return true;
2326 /* Skip the given pass, for handling passes before "startwith"
2327 in __GIMPLE and__RTL-marked functions.
2328 In theory, this ought to be a no-op, but some of the RTL passes
2329 need additional processing here. */
2331 static void
2332 skip_pass (opt_pass *pass)
2334 /* Pass "reload" sets the global "reload_completed", and many
2335 things depend on this (e.g. instructions in .md files). */
2336 if (strcmp (pass->name, "reload") == 0)
2337 reload_completed = 1;
2339 /* The INSN_ADDRESSES vec is normally set up by
2340 shorten_branches; set it up for the benefit of passes that
2341 run after this. */
2342 if (strcmp (pass->name, "shorten") == 0)
2343 INSN_ADDRESSES_ALLOC (get_max_uid ());
2345 /* Update the cfg hooks as appropriate. */
2346 if (strcmp (pass->name, "into_cfglayout") == 0)
2348 cfg_layout_rtl_register_cfg_hooks ();
2349 cfun->curr_properties |= PROP_cfglayout;
2351 if (strcmp (pass->name, "outof_cfglayout") == 0)
2353 rtl_register_cfg_hooks ();
2354 cfun->curr_properties &= ~PROP_cfglayout;
2358 /* Execute PASS. */
2360 bool
2361 execute_one_pass (opt_pass *pass)
2363 unsigned int todo_after = 0;
2365 bool gate_status;
2367 /* IPA passes are executed on whole program, so cfun should be NULL.
2368 Other passes need function context set. */
2369 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2370 gcc_assert (!cfun && !current_function_decl);
2371 else
2372 gcc_assert (cfun && current_function_decl);
2374 current_pass = pass;
2376 /* Check whether gate check should be avoided.
2377 User controls the value of the gate through the parameter "gate_status". */
2378 gate_status = pass->gate (cfun);
2379 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2381 /* Override gate with plugin. */
2382 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2384 if (!gate_status)
2386 /* Run so passes selectively disabling themselves on a given function
2387 are not miscounted. */
2388 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2390 check_profile_consistency (pass->static_pass_number, 0, false);
2391 check_profile_consistency (pass->static_pass_number, 1, false);
2393 current_pass = NULL;
2394 return false;
2397 if (should_skip_pass_p (pass))
2399 skip_pass (pass);
2400 return true;
2403 /* Pass execution event trigger: useful to identify passes being
2404 executed. */
2405 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2407 if (!quiet_flag && !cfun)
2408 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2410 /* Note that the folders should only create gimple expressions.
2411 This is a hack until the new folder is ready. */
2412 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2414 pass_init_dump_file (pass);
2416 /* If a timevar is present, start it. */
2417 if (pass->tv_id != TV_NONE)
2418 timevar_push (pass->tv_id);
2420 /* Run pre-pass verification. */
2421 execute_todo (pass->todo_flags_start);
2423 if (flag_checking)
2424 do_per_function (verify_curr_properties,
2425 (void *)(size_t)pass->properties_required);
2427 /* Do it! */
2428 todo_after = pass->execute (cfun);
2430 if (todo_after & TODO_discard_function)
2432 /* Stop timevar. */
2433 if (pass->tv_id != TV_NONE)
2434 timevar_pop (pass->tv_id);
2436 pass_fini_dump_file (pass);
2438 gcc_assert (cfun);
2439 /* As cgraph_node::release_body expects release dominators info,
2440 we have to release it. */
2441 if (dom_info_available_p (CDI_DOMINATORS))
2442 free_dominance_info (CDI_DOMINATORS);
2444 if (dom_info_available_p (CDI_POST_DOMINATORS))
2445 free_dominance_info (CDI_POST_DOMINATORS);
2447 tree fn = cfun->decl;
2448 pop_cfun ();
2449 gcc_assert (!cfun);
2450 cgraph_node::get (fn)->release_body ();
2452 current_pass = NULL;
2453 redirect_edge_var_map_empty ();
2455 ggc_collect ();
2457 return true;
2460 do_per_function (clear_last_verified, NULL);
2462 do_per_function (update_properties_after_pass, pass);
2464 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2465 check_profile_consistency (pass->static_pass_number, 0, true);
2467 /* Run post-pass cleanup and verification. */
2468 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2469 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2470 check_profile_consistency (pass->static_pass_number, 1, true);
2472 verify_interpass_invariants ();
2474 /* Stop timevar. */
2475 if (pass->tv_id != TV_NONE)
2476 timevar_pop (pass->tv_id);
2478 if (pass->type == IPA_PASS
2479 && ((ipa_opt_pass_d *)pass)->function_transform)
2481 struct cgraph_node *node;
2482 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2483 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2485 else if (dump_file)
2486 do_per_function (execute_function_dump, pass);
2488 if (!current_function_decl)
2489 symtab->process_new_functions ();
2491 pass_fini_dump_file (pass);
2493 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2494 gcc_assert (!(cfun->curr_properties & PROP_trees)
2495 || pass->type != RTL_PASS);
2497 current_pass = NULL;
2498 redirect_edge_var_map_empty ();
2500 /* Signal this is a suitable GC collection point. */
2501 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2502 ggc_collect ();
2504 return true;
2507 static void
2508 execute_pass_list_1 (opt_pass *pass)
2512 gcc_assert (pass->type == GIMPLE_PASS
2513 || pass->type == RTL_PASS);
2515 if (cfun == NULL)
2516 return;
2517 if (execute_one_pass (pass) && pass->sub)
2518 execute_pass_list_1 (pass->sub);
2519 pass = pass->next;
2521 while (pass);
2524 void
2525 execute_pass_list (function *fn, opt_pass *pass)
2527 gcc_assert (fn == cfun);
2528 execute_pass_list_1 (pass);
2529 if (cfun && fn->cfg)
2531 free_dominance_info (CDI_DOMINATORS);
2532 free_dominance_info (CDI_POST_DOMINATORS);
2536 /* Write out all LTO data. */
2537 static void
2538 write_lto (void)
2540 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2541 lto_output ();
2542 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2543 timevar_push (TV_IPA_LTO_DECL_OUT);
2544 produce_asm_for_decls ();
2545 timevar_pop (TV_IPA_LTO_DECL_OUT);
2548 /* Same as execute_pass_list but assume that subpasses of IPA passes
2549 are local passes. If SET is not NULL, write out summaries of only
2550 those node in SET. */
2552 static void
2553 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2555 while (pass)
2557 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2558 gcc_assert (!current_function_decl);
2559 gcc_assert (!cfun);
2560 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2561 if (pass->type == IPA_PASS
2562 && ipa_pass->write_summary
2563 && pass->gate (cfun))
2565 /* If a timevar is present, start it. */
2566 if (pass->tv_id)
2567 timevar_push (pass->tv_id);
2569 pass_init_dump_file (pass);
2571 current_pass = pass;
2572 ipa_pass->write_summary ();
2574 pass_fini_dump_file (pass);
2576 /* If a timevar is present, start it. */
2577 if (pass->tv_id)
2578 timevar_pop (pass->tv_id);
2581 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2582 ipa_write_summaries_2 (pass->sub, state);
2584 pass = pass->next;
2588 /* Helper function of ipa_write_summaries. Creates and destroys the
2589 decl state and calls ipa_write_summaries_2 for all passes that have
2590 summaries. SET is the set of nodes to be written. */
2592 static void
2593 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2595 pass_manager *passes = g->get_passes ();
2596 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2597 state->symtab_node_encoder = encoder;
2599 lto_output_init_mode_table ();
2600 lto_push_out_decl_state (state);
2602 gcc_assert (!flag_wpa);
2603 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2605 write_lto ();
2607 gcc_assert (lto_get_out_decl_state () == state);
2608 lto_pop_out_decl_state ();
2609 lto_delete_out_decl_state (state);
2612 /* Write out summaries for all the nodes in the callgraph. */
2614 void
2615 ipa_write_summaries (void)
2617 lto_symtab_encoder_t encoder;
2618 int i, order_pos;
2619 varpool_node *vnode;
2620 struct cgraph_node *node;
2621 struct cgraph_node **order;
2623 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2624 return;
2626 gcc_assert (!dump_file);
2627 streamer_dump_file = dump_begin (TDI_lto_stream_out, NULL);
2629 select_what_to_stream ();
2631 encoder = lto_symtab_encoder_new (false);
2633 /* Create the callgraph set in the same order used in
2634 cgraph_expand_all_functions. This mostly facilitates debugging,
2635 since it causes the gimple file to be processed in the same order
2636 as the source code. */
2637 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2638 order_pos = ipa_reverse_postorder (order);
2639 gcc_assert (order_pos == symtab->cgraph_count);
2641 for (i = order_pos - 1; i >= 0; i--)
2643 struct cgraph_node *node = order[i];
2645 if (gimple_has_body_p (node->decl))
2647 /* When streaming out references to statements as part of some IPA
2648 pass summary, the statements need to have uids assigned and the
2649 following does that for all the IPA passes here. Naturally, this
2650 ordering then matches the one IPA-passes get in their stmt_fixup
2651 hooks. */
2653 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2654 renumber_gimple_stmt_uids ();
2655 pop_cfun ();
2657 if (node->definition && node->need_lto_streaming)
2658 lto_set_symtab_encoder_in_partition (encoder, node);
2661 FOR_EACH_DEFINED_FUNCTION (node)
2662 if (node->alias && node->need_lto_streaming)
2663 lto_set_symtab_encoder_in_partition (encoder, node);
2664 FOR_EACH_DEFINED_VARIABLE (vnode)
2665 if (vnode->need_lto_streaming)
2666 lto_set_symtab_encoder_in_partition (encoder, vnode);
2668 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2670 free (order);
2671 if (streamer_dump_file)
2673 dump_end (TDI_lto_stream_out, streamer_dump_file);
2674 streamer_dump_file = NULL;
2678 /* Same as execute_pass_list but assume that subpasses of IPA passes
2679 are local passes. If SET is not NULL, write out optimization summaries of
2680 only those node in SET. */
2682 static void
2683 ipa_write_optimization_summaries_1 (opt_pass *pass,
2684 struct lto_out_decl_state *state)
2686 while (pass)
2688 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2689 gcc_assert (!current_function_decl);
2690 gcc_assert (!cfun);
2691 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2692 if (pass->type == IPA_PASS
2693 && ipa_pass->write_optimization_summary
2694 && pass->gate (cfun))
2696 /* If a timevar is present, start it. */
2697 if (pass->tv_id)
2698 timevar_push (pass->tv_id);
2700 pass_init_dump_file (pass);
2702 current_pass = pass;
2703 ipa_pass->write_optimization_summary ();
2705 pass_fini_dump_file (pass);
2707 /* If a timevar is present, start it. */
2708 if (pass->tv_id)
2709 timevar_pop (pass->tv_id);
2712 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2713 ipa_write_optimization_summaries_1 (pass->sub, state);
2715 pass = pass->next;
2719 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2720 NULL, write out all summaries of all nodes. */
2722 void
2723 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2725 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2726 lto_symtab_encoder_iterator lsei;
2727 state->symtab_node_encoder = encoder;
2729 lto_output_init_mode_table ();
2730 lto_push_out_decl_state (state);
2731 for (lsei = lsei_start_function_in_partition (encoder);
2732 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2734 struct cgraph_node *node = lsei_cgraph_node (lsei);
2735 /* When streaming out references to statements as part of some IPA
2736 pass summary, the statements need to have uids assigned.
2738 For functions newly born at WPA stage we need to initialize
2739 the uids here. */
2740 if (node->definition
2741 && gimple_has_body_p (node->decl))
2743 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2744 renumber_gimple_stmt_uids ();
2745 pop_cfun ();
2749 gcc_assert (flag_wpa);
2750 pass_manager *passes = g->get_passes ();
2751 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2753 write_lto ();
2755 gcc_assert (lto_get_out_decl_state () == state);
2756 lto_pop_out_decl_state ();
2757 lto_delete_out_decl_state (state);
2760 /* Same as execute_pass_list but assume that subpasses of IPA passes
2761 are local passes. */
2763 static void
2764 ipa_read_summaries_1 (opt_pass *pass)
2766 while (pass)
2768 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2770 gcc_assert (!current_function_decl);
2771 gcc_assert (!cfun);
2772 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2774 if (pass->gate (cfun))
2776 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2778 /* If a timevar is present, start it. */
2779 if (pass->tv_id)
2780 timevar_push (pass->tv_id);
2782 pass_init_dump_file (pass);
2784 current_pass = pass;
2785 ipa_pass->read_summary ();
2787 pass_fini_dump_file (pass);
2789 /* Stop timevar. */
2790 if (pass->tv_id)
2791 timevar_pop (pass->tv_id);
2794 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2795 ipa_read_summaries_1 (pass->sub);
2797 pass = pass->next;
2802 /* Read all the summaries for all_regular_ipa_passes. */
2804 void
2805 ipa_read_summaries (void)
2807 pass_manager *passes = g->get_passes ();
2808 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2811 /* Same as execute_pass_list but assume that subpasses of IPA passes
2812 are local passes. */
2814 static void
2815 ipa_read_optimization_summaries_1 (opt_pass *pass)
2817 while (pass)
2819 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2821 gcc_assert (!current_function_decl);
2822 gcc_assert (!cfun);
2823 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2825 if (pass->gate (cfun))
2827 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2829 /* If a timevar is present, start it. */
2830 if (pass->tv_id)
2831 timevar_push (pass->tv_id);
2833 pass_init_dump_file (pass);
2835 current_pass = pass;
2836 ipa_pass->read_optimization_summary ();
2838 pass_fini_dump_file (pass);
2840 /* Stop timevar. */
2841 if (pass->tv_id)
2842 timevar_pop (pass->tv_id);
2845 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2846 ipa_read_optimization_summaries_1 (pass->sub);
2848 pass = pass->next;
2852 /* Read all the summaries for all_regular_ipa_passes. */
2854 void
2855 ipa_read_optimization_summaries (void)
2857 pass_manager *passes = g->get_passes ();
2858 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2861 /* Same as execute_pass_list but assume that subpasses of IPA passes
2862 are local passes. */
2863 void
2864 execute_ipa_pass_list (opt_pass *pass)
2868 gcc_assert (!current_function_decl);
2869 gcc_assert (!cfun);
2870 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2871 if (execute_one_pass (pass) && pass->sub)
2873 if (pass->sub->type == GIMPLE_PASS)
2875 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2876 do_per_function_toporder ((void (*)(function *, void *))
2877 execute_pass_list,
2878 pass->sub);
2879 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2881 else if (pass->sub->type == SIMPLE_IPA_PASS
2882 || pass->sub->type == IPA_PASS)
2883 execute_ipa_pass_list (pass->sub);
2884 else
2885 gcc_unreachable ();
2887 gcc_assert (!current_function_decl);
2888 symtab->process_new_functions ();
2889 pass = pass->next;
2891 while (pass);
2894 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2896 static void
2897 execute_ipa_stmt_fixups (opt_pass *pass,
2898 struct cgraph_node *node, gimple **stmts)
2900 while (pass)
2902 /* Execute all of the IPA_PASSes in the list. */
2903 if (pass->type == IPA_PASS
2904 && pass->gate (cfun))
2906 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2908 if (ipa_pass->stmt_fixup)
2910 pass_init_dump_file (pass);
2911 /* If a timevar is present, start it. */
2912 if (pass->tv_id)
2913 timevar_push (pass->tv_id);
2915 current_pass = pass;
2916 ipa_pass->stmt_fixup (node, stmts);
2918 /* Stop timevar. */
2919 if (pass->tv_id)
2920 timevar_pop (pass->tv_id);
2921 pass_fini_dump_file (pass);
2923 if (pass->sub)
2924 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2926 pass = pass->next;
2930 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2932 void
2933 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2935 pass_manager *passes = g->get_passes ();
2936 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2940 extern void debug_properties (unsigned int);
2941 extern void dump_properties (FILE *, unsigned int);
2943 DEBUG_FUNCTION void
2944 dump_properties (FILE *dump, unsigned int props)
2946 fprintf (dump, "Properties:\n");
2947 if (props & PROP_gimple_any)
2948 fprintf (dump, "PROP_gimple_any\n");
2949 if (props & PROP_gimple_lcf)
2950 fprintf (dump, "PROP_gimple_lcf\n");
2951 if (props & PROP_gimple_leh)
2952 fprintf (dump, "PROP_gimple_leh\n");
2953 if (props & PROP_cfg)
2954 fprintf (dump, "PROP_cfg\n");
2955 if (props & PROP_ssa)
2956 fprintf (dump, "PROP_ssa\n");
2957 if (props & PROP_no_crit_edges)
2958 fprintf (dump, "PROP_no_crit_edges\n");
2959 if (props & PROP_rtl)
2960 fprintf (dump, "PROP_rtl\n");
2961 if (props & PROP_gimple_lomp)
2962 fprintf (dump, "PROP_gimple_lomp\n");
2963 if (props & PROP_gimple_lomp_dev)
2964 fprintf (dump, "PROP_gimple_lomp_dev\n");
2965 if (props & PROP_gimple_lcx)
2966 fprintf (dump, "PROP_gimple_lcx\n");
2967 if (props & PROP_gimple_lvec)
2968 fprintf (dump, "PROP_gimple_lvec\n");
2969 if (props & PROP_cfglayout)
2970 fprintf (dump, "PROP_cfglayout\n");
2973 DEBUG_FUNCTION void
2974 debug_properties (unsigned int props)
2976 dump_properties (stderr, props);
2979 /* Called by local passes to see if function is called by already processed nodes.
2980 Because we process nodes in topological order, this means that function is
2981 in recursive cycle or we introduced new direct calls. */
2982 bool
2983 function_called_by_processed_nodes_p (void)
2985 struct cgraph_edge *e;
2986 for (e = cgraph_node::get (current_function_decl)->callers;
2988 e = e->next_caller)
2990 if (e->caller->decl == current_function_decl)
2991 continue;
2992 if (!e->caller->has_gimple_body_p ())
2993 continue;
2994 if (TREE_ASM_WRITTEN (e->caller->decl))
2995 continue;
2996 if (!e->caller->process && !e->caller->global.inlined_to)
2997 break;
2999 if (dump_file && e)
3001 fprintf (dump_file, "Already processed call to:\n");
3002 e->caller->dump (dump_file);
3004 return e != NULL;
3007 #include "gt-passes.h"