[42/46] Add vec_info::replace_stmt
[official-gcc.git] / gcc / passes.c
blob832f0b3e5f2df80863852f8d0f76cecc46cd83f2
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;
1407 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1409 /* The checks below could fail in buggy plugins. Existing GCC
1410 passes should never fail these checks, so we mention plugin in
1411 the messages. */
1412 if (!pass_info->pass)
1413 fatal_error (input_location, "plugin cannot register a missing pass");
1415 if (!pass_info->pass->name)
1416 fatal_error (input_location, "plugin cannot register an unnamed pass");
1418 if (!pass_info->reference_pass_name)
1419 fatal_error
1420 (input_location,
1421 "plugin cannot register pass %qs without reference pass name",
1422 pass_info->pass->name);
1424 /* Try to insert the new pass to the pass lists. We need to check
1425 all five lists as the reference pass could be in one (or all) of
1426 them. */
1427 all_instances = pass_info->ref_pass_instance_number == 0;
1428 success = position_pass (pass_info, &all_lowering_passes);
1429 if (!success || all_instances)
1430 success |= position_pass (pass_info, &all_small_ipa_passes);
1431 if (!success || all_instances)
1432 success |= position_pass (pass_info, &all_regular_ipa_passes);
1433 if (!success || all_instances)
1434 success |= position_pass (pass_info, &all_late_ipa_passes);
1435 if (!success || all_instances)
1436 success |= position_pass (pass_info, &all_passes);
1437 if (!success)
1438 fatal_error
1439 (input_location,
1440 "pass %qs not found but is referenced by new pass %qs",
1441 pass_info->reference_pass_name, pass_info->pass->name);
1443 /* OK, we have successfully inserted the new pass. We need to register
1444 the dump files for the newly added pass and its duplicates (if any).
1445 Because the registration of plugin/backend passes happens after the
1446 command-line options are parsed, the options that specify single
1447 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1448 passes. Therefore we currently can only enable dumping of
1449 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1450 are specified. While doing so, we also delete the pass_list_node
1451 objects created during pass positioning. */
1452 while (added_pass_nodes)
1454 struct pass_list_node *next_node = added_pass_nodes->next;
1455 enum tree_dump_index tdi;
1456 register_one_dump_file (added_pass_nodes->pass);
1457 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1458 || added_pass_nodes->pass->type == IPA_PASS)
1459 tdi = TDI_ipa_all;
1460 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1461 tdi = TDI_tree_all;
1462 else
1463 tdi = TDI_rtl_all;
1464 /* Check if dump-all flag is specified. */
1465 if (dumps->get_dump_file_info (tdi)->pstate)
1467 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1468 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1469 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1470 ->pflags = dumps->get_dump_file_info (tdi)->pflags;
1472 XDELETE (added_pass_nodes);
1473 added_pass_nodes = next_node;
1477 /* Construct the pass tree. The sequencing of passes is driven by
1478 the cgraph routines:
1480 finalize_compilation_unit ()
1481 for each node N in the cgraph
1482 cgraph_analyze_function (N)
1483 cgraph_lower_function (N) -> all_lowering_passes
1485 If we are optimizing, compile is then invoked:
1487 compile ()
1488 ipa_passes () -> all_small_ipa_passes
1489 -> Analysis of all_regular_ipa_passes
1490 * possible LTO streaming at copmilation time *
1491 -> Execution of all_regular_ipa_passes
1492 * possible LTO streaming at link time *
1493 -> all_late_ipa_passes
1494 expand_all_functions ()
1495 for each node N in the cgraph
1496 expand_function (N) -> Transformation of all_regular_ipa_passes
1497 -> all_passes
1500 pass_manager::pass_manager (context *ctxt)
1501 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1502 all_regular_ipa_passes (NULL),
1503 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1504 m_ctxt (ctxt), m_name_to_pass_map (NULL)
1506 opt_pass **p;
1508 /* Zero-initialize pass members. */
1509 #define INSERT_PASSES_AFTER(PASS)
1510 #define PUSH_INSERT_PASSES_WITHIN(PASS)
1511 #define POP_INSERT_PASSES()
1512 #define NEXT_PASS(PASS, NUM) PASS ## _ ## NUM = NULL
1513 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) NEXT_PASS (PASS, NUM)
1514 #define TERMINATE_PASS_LIST(PASS)
1515 #include "pass-instances.def"
1516 #undef INSERT_PASSES_AFTER
1517 #undef PUSH_INSERT_PASSES_WITHIN
1518 #undef POP_INSERT_PASSES
1519 #undef NEXT_PASS
1520 #undef NEXT_PASS_WITH_ARG
1521 #undef TERMINATE_PASS_LIST
1523 /* Initialize the pass_lists array. */
1524 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1525 GCC_PASS_LISTS
1526 #undef DEF_PASS_LIST
1528 /* Build the tree of passes. */
1530 #define INSERT_PASSES_AFTER(PASS) \
1532 opt_pass **p_start; \
1533 p_start = p = &(PASS);
1535 #define TERMINATE_PASS_LIST(PASS) \
1536 gcc_assert (p_start == &PASS); \
1537 *p = NULL; \
1540 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1542 opt_pass **p = &(PASS ## _1)->sub;
1544 #define POP_INSERT_PASSES() \
1547 #define NEXT_PASS(PASS, NUM) \
1548 do { \
1549 gcc_assert (PASS ## _ ## NUM == NULL); \
1550 if ((NUM) == 1) \
1551 PASS ## _1 = make_##PASS (m_ctxt); \
1552 else \
1554 gcc_assert (PASS ## _1); \
1555 PASS ## _ ## NUM = PASS ## _1->clone (); \
1557 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1558 } while (0)
1560 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) \
1561 do { \
1562 NEXT_PASS (PASS, NUM); \
1563 PASS ## _ ## NUM->set_pass_param (0, ARG); \
1564 } while (0)
1566 #include "pass-instances.def"
1568 #undef INSERT_PASSES_AFTER
1569 #undef PUSH_INSERT_PASSES_WITHIN
1570 #undef POP_INSERT_PASSES
1571 #undef NEXT_PASS
1572 #undef NEXT_PASS_WITH_ARG
1573 #undef TERMINATE_PASS_LIST
1575 /* Register the passes with the tree dump code. */
1576 register_dump_files (all_lowering_passes);
1577 register_dump_files (all_small_ipa_passes);
1578 register_dump_files (all_regular_ipa_passes);
1579 register_dump_files (all_late_ipa_passes);
1580 register_dump_files (all_passes);
1583 static void
1584 delete_pass_tree (opt_pass *pass)
1586 while (pass)
1588 /* Recurse into child passes. */
1589 delete_pass_tree (pass->sub);
1591 opt_pass *next = pass->next;
1593 /* Delete this pass. */
1594 delete pass;
1596 /* Iterate onto sibling passes. */
1597 pass = next;
1601 pass_manager::~pass_manager ()
1603 XDELETEVEC (passes_by_id);
1605 /* Call delete_pass_tree on each of the pass_lists. */
1606 #define DEF_PASS_LIST(LIST) \
1607 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1608 GCC_PASS_LISTS
1609 #undef DEF_PASS_LIST
1613 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1614 function CALLBACK for every function in the call graph. Otherwise,
1615 call CALLBACK on the current function. */
1617 static void
1618 do_per_function (void (*callback) (function *, void *data), void *data)
1620 if (current_function_decl)
1621 callback (cfun, data);
1622 else
1624 struct cgraph_node *node;
1625 FOR_EACH_DEFINED_FUNCTION (node)
1626 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1627 && (!node->clone_of || node->decl != node->clone_of->decl))
1628 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1632 /* Because inlining might remove no-longer reachable nodes, we need to
1633 keep the array visible to garbage collector to avoid reading collected
1634 out nodes. */
1635 static int nnodes;
1636 static GTY ((length ("nnodes"))) cgraph_node **order;
1638 #define uid_hash_t hash_set<int_hash <int, 0, -1> >
1640 /* Hook called when NODE is removed and therefore should be
1641 excluded from order vector. DATA is a hash set with removed nodes. */
1643 static void
1644 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1646 uid_hash_t *removed_nodes = (uid_hash_t *)data;
1647 removed_nodes->add (node->get_uid ());
1650 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1651 function CALLBACK for every function in the call graph. Otherwise,
1652 call CALLBACK on the current function.
1653 This function is global so that plugins can use it. */
1654 void
1655 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1657 int i;
1659 if (current_function_decl)
1660 callback (cfun, data);
1661 else
1663 cgraph_node_hook_list *hook;
1664 uid_hash_t removed_nodes;
1665 gcc_assert (!order);
1666 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1668 nnodes = ipa_reverse_postorder (order);
1669 for (i = nnodes - 1; i >= 0; i--)
1670 order[i]->process = 1;
1671 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1672 &removed_nodes);
1673 for (i = nnodes - 1; i >= 0; i--)
1675 cgraph_node *node = order[i];
1677 /* Function could be inlined and removed as unreachable. */
1678 if (node == NULL || removed_nodes.contains (node->get_uid ()))
1679 continue;
1681 /* Allow possibly removed nodes to be garbage collected. */
1682 order[i] = NULL;
1683 node->process = 0;
1684 if (node->has_gimple_body_p ())
1686 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1687 push_cfun (fn);
1688 callback (fn, data);
1689 pop_cfun ();
1692 symtab->remove_cgraph_removal_hook (hook);
1694 ggc_free (order);
1695 order = NULL;
1696 nnodes = 0;
1699 /* Helper function to perform function body dump. */
1701 static void
1702 execute_function_dump (function *fn, void *data)
1704 opt_pass *pass = (opt_pass *)data;
1706 if (dump_file)
1708 push_cfun (fn);
1710 if (fn->curr_properties & PROP_trees)
1711 dump_function_to_file (fn->decl, dump_file, dump_flags);
1712 else
1713 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1715 /* Flush the file. If verification fails, we won't be able to
1716 close the file before aborting. */
1717 fflush (dump_file);
1719 if ((fn->curr_properties & PROP_cfg)
1720 && (dump_flags & TDF_GRAPH))
1722 gcc::dump_manager *dumps = g->get_dumps ();
1723 struct dump_file_info *dfi
1724 = dumps->get_dump_file_info (pass->static_pass_number);
1725 if (!dfi->graph_dump_initialized)
1727 clean_graph_dump_file (dump_file_name);
1728 dfi->graph_dump_initialized = true;
1730 print_graph_cfg (dump_file_name, fn);
1733 pop_cfun ();
1737 /* This function is called when an internal compiler error is encountered.
1738 Ensure that function dump is made available before compiler is aborted. */
1740 void
1741 emergency_dump_function ()
1743 if (!current_pass)
1744 return;
1745 enum opt_pass_type pt = current_pass->type;
1746 fnotice (stderr, "during %s pass: %s\n",
1747 pt == GIMPLE_PASS ? "GIMPLE" : pt == RTL_PASS ? "RTL" : "IPA",
1748 current_pass->name);
1749 if (!dump_file || !cfun)
1750 return;
1751 fnotice (stderr, "dump file: %s\n", dump_file_name);
1752 fprintf (dump_file, "\n\n\nEMERGENCY DUMP:\n\n");
1753 execute_function_dump (cfun, current_pass);
1756 static struct profile_record *profile_record;
1758 /* Do profile consistency book-keeping for the pass with static number INDEX.
1759 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1760 we run _after_ the pass. 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. */
1763 static void
1764 check_profile_consistency (int index, int subpass, bool run)
1766 pass_manager *passes = g->get_passes ();
1767 if (index == -1)
1768 return;
1769 if (!profile_record)
1770 profile_record = XCNEWVEC (struct profile_record,
1771 passes->passes_by_id_size);
1772 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1773 gcc_assert (subpass < 2);
1774 profile_record[index].run |= run;
1775 account_profile_record (&profile_record[index], subpass);
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 i, j;
1790 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1791 gcov_type last_time = 0, last_size = 0;
1792 double rel_time_change, rel_size_change;
1793 int last_reported = 0;
1795 if (!profile_record)
1796 return;
1797 fprintf (stderr, "\nProfile consistency report:\n\n");
1798 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1799 fprintf (stderr, " |freq count |freq count |size time\n");
1801 for (i = 0; i < passes_by_id_size; i++)
1802 for (j = 0 ; j < 2; j++)
1803 if (profile_record[i].run)
1805 if (last_time)
1806 rel_time_change = (profile_record[i].time[j]
1807 - (double)last_time) * 100 / (double)last_time;
1808 else
1809 rel_time_change = 0;
1810 if (last_size)
1811 rel_size_change = (profile_record[i].size[j]
1812 - (double)last_size) * 100 / (double)last_size;
1813 else
1814 rel_size_change = 0;
1816 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1817 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1818 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1819 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1820 || rel_time_change || rel_size_change)
1822 last_reported = i;
1823 fprintf (stderr, "%-20s %s",
1824 passes_by_id [i]->name,
1825 j ? "(after TODO)" : " ");
1826 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1827 fprintf (stderr, "| %+5i",
1828 profile_record[i].num_mismatched_freq_in[j]
1829 - last_freq_in);
1830 else
1831 fprintf (stderr, "| ");
1832 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1833 fprintf (stderr, " %+5i",
1834 profile_record[i].num_mismatched_count_in[j]
1835 - last_count_in);
1836 else
1837 fprintf (stderr, " ");
1838 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1839 fprintf (stderr, "| %+5i",
1840 profile_record[i].num_mismatched_freq_out[j]
1841 - last_freq_out);
1842 else
1843 fprintf (stderr, "| ");
1844 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1845 fprintf (stderr, " %+5i",
1846 profile_record[i].num_mismatched_count_out[j]
1847 - last_count_out);
1848 else
1849 fprintf (stderr, " ");
1851 /* Size/time units change across gimple and RTL. */
1852 if (i == pass_expand_1->static_pass_number)
1853 fprintf (stderr, "|----------");
1854 else
1856 if (rel_size_change)
1857 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1858 else
1859 fprintf (stderr, "| ");
1860 if (rel_time_change)
1861 fprintf (stderr, " %+8.4f%%", rel_time_change);
1863 fprintf (stderr, "\n");
1864 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1865 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1866 last_count_in = profile_record[i].num_mismatched_count_in[j];
1867 last_count_out = profile_record[i].num_mismatched_count_out[j];
1869 else if (j && last_reported != i)
1871 last_reported = i;
1872 fprintf (stderr, "%-20s ------------| | |\n",
1873 passes_by_id [i]->name);
1875 last_time = profile_record[i].time[j];
1876 last_size = profile_record[i].size[j];
1880 /* Perform all TODO actions that ought to be done on each function. */
1882 static void
1883 execute_function_todo (function *fn, void *data)
1885 bool from_ipa_pass = (cfun == NULL);
1886 unsigned int flags = (size_t)data;
1887 flags &= ~fn->last_verified;
1888 if (!flags)
1889 return;
1891 push_cfun (fn);
1893 /* Always cleanup the CFG before trying to update SSA. */
1894 if (flags & TODO_cleanup_cfg)
1896 cleanup_tree_cfg ();
1898 /* When cleanup_tree_cfg merges consecutive blocks, it may
1899 perform some simplistic propagation when removing single
1900 valued PHI nodes. This propagation may, in turn, cause the
1901 SSA form to become out-of-date (see PR 22037). So, even
1902 if the parent pass had not scheduled an SSA update, we may
1903 still need to do one. */
1904 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1905 flags |= TODO_update_ssa;
1908 if (flags & TODO_update_ssa_any)
1910 unsigned update_flags = flags & TODO_update_ssa_any;
1911 update_ssa (update_flags);
1914 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1915 compute_may_aliases ();
1917 if (optimize && (flags & TODO_update_address_taken))
1918 execute_update_addresses_taken ();
1920 if (flags & TODO_remove_unused_locals)
1921 remove_unused_locals ();
1923 if (flags & TODO_rebuild_frequencies)
1924 rebuild_frequencies ();
1926 if (flags & TODO_rebuild_cgraph_edges)
1927 cgraph_edge::rebuild_edges ();
1929 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1930 /* If we've seen errors do not bother running any verifiers. */
1931 if (flag_checking && !seen_error ())
1933 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1934 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1936 if (flags & TODO_verify_il)
1938 if (cfun->curr_properties & PROP_trees)
1940 if (cfun->curr_properties & PROP_cfg)
1941 /* IPA passes leave stmts to be fixed up, so make sure to
1942 not verify stmts really throw. */
1943 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1944 else
1945 verify_gimple_in_seq (gimple_body (cfun->decl));
1947 if (cfun->curr_properties & PROP_ssa)
1948 /* IPA passes leave stmts to be fixed up, so make sure to
1949 not verify SSA operands whose verifier will choke on that. */
1950 verify_ssa (true, !from_ipa_pass);
1951 /* IPA passes leave basic-blocks unsplit, so make sure to
1952 not trip on that. */
1953 if ((cfun->curr_properties & PROP_cfg)
1954 && !from_ipa_pass)
1955 verify_flow_info ();
1956 if (current_loops
1957 && ! loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1959 verify_loop_structure ();
1960 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1961 verify_loop_closed_ssa (false);
1963 if (cfun->curr_properties & PROP_rtl)
1964 verify_rtl_sharing ();
1967 /* Make sure verifiers don't change dominator state. */
1968 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1969 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1972 fn->last_verified = flags & TODO_verify_all;
1974 pop_cfun ();
1976 /* For IPA passes make sure to release dominator info, it can be
1977 computed by non-verifying TODOs. */
1978 if (from_ipa_pass)
1980 free_dominance_info (fn, CDI_DOMINATORS);
1981 free_dominance_info (fn, CDI_POST_DOMINATORS);
1985 /* Perform all TODO actions. */
1986 static void
1987 execute_todo (unsigned int flags)
1989 if (flag_checking
1990 && cfun
1991 && need_ssa_update_p (cfun))
1992 gcc_assert (flags & TODO_update_ssa_any);
1994 statistics_fini_pass ();
1996 if (flags)
1997 do_per_function (execute_function_todo, (void *)(size_t) flags);
1999 /* At this point we should not have any unreachable code in the
2000 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
2001 if (cfun && cfun->gimple_df)
2002 flush_ssaname_freelist ();
2004 /* Always remove functions just as before inlining: IPA passes might be
2005 interested to see bodies of extern inline functions that are not inlined
2006 to analyze side effects. The full removal is done just at the end
2007 of IPA pass queue. */
2008 if (flags & TODO_remove_functions)
2010 gcc_assert (!cfun);
2011 symtab->remove_unreachable_nodes (dump_file);
2014 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2016 gcc_assert (!cfun);
2017 symtab->dump (dump_file);
2018 /* Flush the file. If verification fails, we won't be able to
2019 close the file before aborting. */
2020 fflush (dump_file);
2023 /* Now that the dumping has been done, we can get rid of the optional
2024 df problems. */
2025 if (flags & TODO_df_finish)
2026 df_finish_pass ((flags & TODO_df_verify) != 0);
2029 /* Verify invariants that should hold between passes. This is a place
2030 to put simple sanity checks. */
2032 static void
2033 verify_interpass_invariants (void)
2035 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2038 /* Clear the last verified flag. */
2040 static void
2041 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2043 fn->last_verified = 0;
2046 /* Helper function. Verify that the properties has been turn into the
2047 properties expected by the pass. */
2049 static void
2050 verify_curr_properties (function *fn, void *data)
2052 unsigned int props = (size_t)data;
2053 gcc_assert ((fn->curr_properties & props) == props);
2056 /* Release dump file name if set. */
2058 static void
2059 release_dump_file_name (void)
2061 if (dump_file_name)
2063 free (CONST_CAST (char *, dump_file_name));
2064 dump_file_name = NULL;
2068 /* Initialize pass dump file. */
2069 /* This is non-static so that the plugins can use it. */
2071 bool
2072 pass_init_dump_file (opt_pass *pass)
2074 /* If a dump file name is present, open it if enabled. */
2075 if (pass->static_pass_number != -1)
2077 timevar_push (TV_DUMP);
2078 gcc::dump_manager *dumps = g->get_dumps ();
2079 bool initializing_dump =
2080 !dumps->dump_initialized_p (pass->static_pass_number);
2081 release_dump_file_name ();
2082 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2083 dumps->dump_start (pass->static_pass_number, &dump_flags);
2084 if (dump_file && current_function_decl && ! (dump_flags & TDF_GIMPLE))
2085 dump_function_header (dump_file, current_function_decl, dump_flags);
2086 if (initializing_dump
2087 && dump_file && (dump_flags & TDF_GRAPH)
2088 && cfun && (cfun->curr_properties & PROP_cfg))
2090 clean_graph_dump_file (dump_file_name);
2091 struct dump_file_info *dfi
2092 = dumps->get_dump_file_info (pass->static_pass_number);
2093 dfi->graph_dump_initialized = true;
2095 timevar_pop (TV_DUMP);
2096 return initializing_dump;
2098 else
2099 return false;
2102 /* Flush PASS dump file. */
2103 /* This is non-static so that plugins can use it. */
2105 void
2106 pass_fini_dump_file (opt_pass *pass)
2108 timevar_push (TV_DUMP);
2110 /* Flush and close dump file. */
2111 release_dump_file_name ();
2113 g->get_dumps ()->dump_finish (pass->static_pass_number);
2114 timevar_pop (TV_DUMP);
2117 /* After executing the pass, apply expected changes to the function
2118 properties. */
2120 static void
2121 update_properties_after_pass (function *fn, void *data)
2123 opt_pass *pass = (opt_pass *) data;
2124 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2125 & ~pass->properties_destroyed;
2128 /* Execute summary generation for all of the passes in IPA_PASS. */
2130 void
2131 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2133 while (ipa_pass)
2135 opt_pass *pass = ipa_pass;
2137 /* Execute all of the IPA_PASSes in the list. */
2138 if (ipa_pass->type == IPA_PASS
2139 && pass->gate (cfun)
2140 && ipa_pass->generate_summary)
2142 pass_init_dump_file (pass);
2144 /* If a timevar is present, start it. */
2145 if (pass->tv_id)
2146 timevar_push (pass->tv_id);
2148 current_pass = pass;
2149 ipa_pass->generate_summary ();
2151 /* Stop timevar. */
2152 if (pass->tv_id)
2153 timevar_pop (pass->tv_id);
2155 pass_fini_dump_file (pass);
2157 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2161 /* Execute IPA_PASS function transform on NODE. */
2163 static void
2164 execute_one_ipa_transform_pass (struct cgraph_node *node,
2165 ipa_opt_pass_d *ipa_pass)
2167 opt_pass *pass = ipa_pass;
2168 unsigned int todo_after = 0;
2170 current_pass = pass;
2171 if (!ipa_pass->function_transform)
2172 return;
2174 /* Note that the folders should only create gimple expressions.
2175 This is a hack until the new folder is ready. */
2176 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2178 pass_init_dump_file (pass);
2180 /* If a timevar is present, start it. */
2181 if (pass->tv_id != TV_NONE)
2182 timevar_push (pass->tv_id);
2184 /* Run pre-pass verification. */
2185 execute_todo (ipa_pass->function_transform_todo_flags_start);
2187 /* Do it! */
2188 todo_after = ipa_pass->function_transform (node);
2190 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2191 check_profile_consistency (pass->static_pass_number, 0, true);
2193 /* Run post-pass cleanup and verification. */
2194 execute_todo (todo_after);
2195 verify_interpass_invariants ();
2196 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2197 check_profile_consistency (pass->static_pass_number, 1, true);
2199 /* Stop timevar. */
2200 if (pass->tv_id != TV_NONE)
2201 timevar_pop (pass->tv_id);
2203 if (dump_file)
2204 do_per_function (execute_function_dump, pass);
2205 pass_fini_dump_file (pass);
2207 current_pass = NULL;
2208 redirect_edge_var_map_empty ();
2210 /* Signal this is a suitable GC collection point. */
2211 if (!(todo_after & TODO_do_not_ggc_collect))
2212 ggc_collect ();
2215 /* For the current function, execute all ipa transforms. */
2217 void
2218 execute_all_ipa_transforms (void)
2220 struct cgraph_node *node;
2221 if (!cfun)
2222 return;
2223 node = cgraph_node::get (current_function_decl);
2225 if (node->ipa_transforms_to_apply.exists ())
2227 unsigned int i;
2229 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2230 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2231 node->ipa_transforms_to_apply.release ();
2235 /* Check if PASS is explicitly disabled or enabled and return
2236 the gate status. FUNC is the function to be processed, and
2237 GATE_STATUS is the gate status determined by pass manager by
2238 default. */
2240 static bool
2241 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2243 bool explicitly_enabled = false;
2244 bool explicitly_disabled = false;
2246 explicitly_enabled
2247 = is_pass_explicitly_enabled_or_disabled (pass, func,
2248 enabled_pass_uid_range_tab);
2249 explicitly_disabled
2250 = is_pass_explicitly_enabled_or_disabled (pass, func,
2251 disabled_pass_uid_range_tab);
2253 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2255 return gate_status;
2258 /* Determine if PASS_NAME matches CRITERION.
2259 Not a pure predicate, since it can update CRITERION, to support
2260 matching the Nth invocation of a pass.
2261 Subroutine of should_skip_pass_p. */
2263 static bool
2264 determine_pass_name_match (const char *pass_name, char *criterion)
2266 size_t namelen = strlen (pass_name);
2267 if (! strncmp (pass_name, criterion, namelen))
2269 /* The following supports starting with the Nth invocation
2270 of a pass (where N does not necessarily is equal to the
2271 dump file suffix). */
2272 if (criterion[namelen] == '\0'
2273 || (criterion[namelen] == '1'
2274 && criterion[namelen + 1] == '\0'))
2275 return true;
2276 else
2278 if (criterion[namelen + 1] == '\0')
2279 --criterion[namelen];
2280 return false;
2283 else
2284 return false;
2287 /* For skipping passes until "startwith" pass.
2288 Return true iff PASS should be skipped.
2289 Clear cfun->pass_startwith when encountering the "startwith" pass,
2290 so that all subsequent passes are run. */
2292 static bool
2293 should_skip_pass_p (opt_pass *pass)
2295 if (!cfun)
2296 return false;
2297 if (!cfun->pass_startwith)
2298 return false;
2300 /* For __GIMPLE functions, we have to at least start when we leave
2301 SSA. Hence, we need to detect the "expand" pass, and stop skipping
2302 when we encounter it. A cheap way to identify "expand" is it to
2303 detect the destruction of PROP_ssa.
2304 For __RTL functions, we invoke "rest_of_compilation" directly, which
2305 is after "expand", and hence we don't reach this conditional. */
2306 if (pass->properties_destroyed & PROP_ssa)
2308 if (!quiet_flag)
2309 fprintf (stderr, "starting anyway when leaving SSA: %s\n", pass->name);
2310 cfun->pass_startwith = NULL;
2311 return false;
2314 if (determine_pass_name_match (pass->name, cfun->pass_startwith))
2316 if (!quiet_flag)
2317 fprintf (stderr, "found starting pass: %s\n", pass->name);
2318 cfun->pass_startwith = NULL;
2319 return false;
2322 /* For GIMPLE passes, run any property provider (but continue skipping
2323 afterwards).
2324 We don't want to force running RTL passes that are property providers:
2325 "expand" is covered above, and the only pass other than "expand" that
2326 provides a property is "into_cfglayout" (PROP_cfglayout), which does
2327 too much for a dumped __RTL function. */
2328 if (pass->type == GIMPLE_PASS
2329 && pass->properties_provided != 0)
2330 return false;
2332 /* Don't skip df init; later RTL passes need it. */
2333 if (strstr (pass->name, "dfinit") != NULL)
2334 return false;
2336 if (!quiet_flag)
2337 fprintf (stderr, "skipping pass: %s\n", pass->name);
2339 /* If we get here, then we have a "startwith" that we haven't seen yet;
2340 skip the pass. */
2341 return true;
2344 /* Skip the given pass, for handling passes before "startwith"
2345 in __GIMPLE and__RTL-marked functions.
2346 In theory, this ought to be a no-op, but some of the RTL passes
2347 need additional processing here. */
2349 static void
2350 skip_pass (opt_pass *pass)
2352 /* Pass "reload" sets the global "reload_completed", and many
2353 things depend on this (e.g. instructions in .md files). */
2354 if (strcmp (pass->name, "reload") == 0)
2355 reload_completed = 1;
2357 /* The INSN_ADDRESSES vec is normally set up by
2358 shorten_branches; set it up for the benefit of passes that
2359 run after this. */
2360 if (strcmp (pass->name, "shorten") == 0)
2361 INSN_ADDRESSES_ALLOC (get_max_uid ());
2363 /* Update the cfg hooks as appropriate. */
2364 if (strcmp (pass->name, "into_cfglayout") == 0)
2366 cfg_layout_rtl_register_cfg_hooks ();
2367 cfun->curr_properties |= PROP_cfglayout;
2369 if (strcmp (pass->name, "outof_cfglayout") == 0)
2371 rtl_register_cfg_hooks ();
2372 cfun->curr_properties &= ~PROP_cfglayout;
2376 /* Execute PASS. */
2378 bool
2379 execute_one_pass (opt_pass *pass)
2381 unsigned int todo_after = 0;
2383 bool gate_status;
2385 /* IPA passes are executed on whole program, so cfun should be NULL.
2386 Other passes need function context set. */
2387 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2388 gcc_assert (!cfun && !current_function_decl);
2389 else
2390 gcc_assert (cfun && current_function_decl);
2392 current_pass = pass;
2394 /* Check whether gate check should be avoided.
2395 User controls the value of the gate through the parameter "gate_status". */
2396 gate_status = pass->gate (cfun);
2397 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2399 /* Override gate with plugin. */
2400 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2402 if (!gate_status)
2404 /* Run so passes selectively disabling themselves on a given function
2405 are not miscounted. */
2406 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2408 check_profile_consistency (pass->static_pass_number, 0, false);
2409 check_profile_consistency (pass->static_pass_number, 1, false);
2411 current_pass = NULL;
2412 return false;
2415 if (should_skip_pass_p (pass))
2417 skip_pass (pass);
2418 return true;
2421 /* Pass execution event trigger: useful to identify passes being
2422 executed. */
2423 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2425 if (!quiet_flag && !cfun)
2426 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2428 /* Note that the folders should only create gimple expressions.
2429 This is a hack until the new folder is ready. */
2430 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2432 pass_init_dump_file (pass);
2434 /* If a timevar is present, start it. */
2435 if (pass->tv_id != TV_NONE)
2436 timevar_push (pass->tv_id);
2438 /* Run pre-pass verification. */
2439 execute_todo (pass->todo_flags_start);
2441 if (flag_checking)
2442 do_per_function (verify_curr_properties,
2443 (void *)(size_t)pass->properties_required);
2445 /* Do it! */
2446 todo_after = pass->execute (cfun);
2448 if (todo_after & TODO_discard_function)
2450 /* Stop timevar. */
2451 if (pass->tv_id != TV_NONE)
2452 timevar_pop (pass->tv_id);
2454 pass_fini_dump_file (pass);
2456 gcc_assert (cfun);
2457 /* As cgraph_node::release_body expects release dominators info,
2458 we have to release it. */
2459 if (dom_info_available_p (CDI_DOMINATORS))
2460 free_dominance_info (CDI_DOMINATORS);
2462 if (dom_info_available_p (CDI_POST_DOMINATORS))
2463 free_dominance_info (CDI_POST_DOMINATORS);
2465 tree fn = cfun->decl;
2466 pop_cfun ();
2467 gcc_assert (!cfun);
2468 cgraph_node::get (fn)->release_body ();
2470 current_pass = NULL;
2471 redirect_edge_var_map_empty ();
2473 ggc_collect ();
2475 return true;
2478 do_per_function (clear_last_verified, NULL);
2480 do_per_function (update_properties_after_pass, pass);
2482 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2483 check_profile_consistency (pass->static_pass_number, 0, true);
2485 /* Run post-pass cleanup and verification. */
2486 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2487 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2488 check_profile_consistency (pass->static_pass_number, 1, true);
2490 verify_interpass_invariants ();
2492 /* Stop timevar. */
2493 if (pass->tv_id != TV_NONE)
2494 timevar_pop (pass->tv_id);
2496 if (pass->type == IPA_PASS
2497 && ((ipa_opt_pass_d *)pass)->function_transform)
2499 struct cgraph_node *node;
2500 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2501 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2503 else if (dump_file)
2504 do_per_function (execute_function_dump, pass);
2506 if (!current_function_decl)
2507 symtab->process_new_functions ();
2509 pass_fini_dump_file (pass);
2511 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2512 gcc_assert (!(cfun->curr_properties & PROP_trees)
2513 || pass->type != RTL_PASS);
2515 current_pass = NULL;
2516 redirect_edge_var_map_empty ();
2518 /* Signal this is a suitable GC collection point. */
2519 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2520 ggc_collect ();
2522 return true;
2525 static void
2526 execute_pass_list_1 (opt_pass *pass)
2530 gcc_assert (pass->type == GIMPLE_PASS
2531 || pass->type == RTL_PASS);
2533 if (cfun == NULL)
2534 return;
2535 if (execute_one_pass (pass) && pass->sub)
2536 execute_pass_list_1 (pass->sub);
2537 pass = pass->next;
2539 while (pass);
2542 void
2543 execute_pass_list (function *fn, opt_pass *pass)
2545 gcc_assert (fn == cfun);
2546 execute_pass_list_1 (pass);
2547 if (cfun && fn->cfg)
2549 free_dominance_info (CDI_DOMINATORS);
2550 free_dominance_info (CDI_POST_DOMINATORS);
2554 /* Write out all LTO data. */
2555 static void
2556 write_lto (void)
2558 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2559 lto_output ();
2560 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2561 timevar_push (TV_IPA_LTO_DECL_OUT);
2562 produce_asm_for_decls ();
2563 timevar_pop (TV_IPA_LTO_DECL_OUT);
2566 /* Same as execute_pass_list but assume that subpasses of IPA passes
2567 are local passes. If SET is not NULL, write out summaries of only
2568 those node in SET. */
2570 static void
2571 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2573 while (pass)
2575 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2576 gcc_assert (!current_function_decl);
2577 gcc_assert (!cfun);
2578 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2579 if (pass->type == IPA_PASS
2580 && ipa_pass->write_summary
2581 && pass->gate (cfun))
2583 /* If a timevar is present, start it. */
2584 if (pass->tv_id)
2585 timevar_push (pass->tv_id);
2587 pass_init_dump_file (pass);
2589 current_pass = pass;
2590 ipa_pass->write_summary ();
2592 pass_fini_dump_file (pass);
2594 /* If a timevar is present, start it. */
2595 if (pass->tv_id)
2596 timevar_pop (pass->tv_id);
2599 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2600 ipa_write_summaries_2 (pass->sub, state);
2602 pass = pass->next;
2606 /* Helper function of ipa_write_summaries. Creates and destroys the
2607 decl state and calls ipa_write_summaries_2 for all passes that have
2608 summaries. SET is the set of nodes to be written. */
2610 static void
2611 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2613 pass_manager *passes = g->get_passes ();
2614 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2615 state->symtab_node_encoder = encoder;
2617 lto_output_init_mode_table ();
2618 lto_push_out_decl_state (state);
2620 gcc_assert (!flag_wpa);
2621 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2623 write_lto ();
2625 gcc_assert (lto_get_out_decl_state () == state);
2626 lto_pop_out_decl_state ();
2627 lto_delete_out_decl_state (state);
2630 /* Write out summaries for all the nodes in the callgraph. */
2632 void
2633 ipa_write_summaries (void)
2635 lto_symtab_encoder_t encoder;
2636 int i, order_pos;
2637 varpool_node *vnode;
2638 struct cgraph_node *node;
2639 struct cgraph_node **order;
2641 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2642 return;
2644 gcc_assert (!dump_file);
2645 streamer_dump_file = dump_begin (TDI_lto_stream_out, NULL);
2647 select_what_to_stream ();
2649 encoder = lto_symtab_encoder_new (false);
2651 /* Create the callgraph set in the same order used in
2652 cgraph_expand_all_functions. This mostly facilitates debugging,
2653 since it causes the gimple file to be processed in the same order
2654 as the source code. */
2655 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2656 order_pos = ipa_reverse_postorder (order);
2657 gcc_assert (order_pos == symtab->cgraph_count);
2659 for (i = order_pos - 1; i >= 0; i--)
2661 struct cgraph_node *node = order[i];
2663 if (gimple_has_body_p (node->decl))
2665 /* When streaming out references to statements as part of some IPA
2666 pass summary, the statements need to have uids assigned and the
2667 following does that for all the IPA passes here. Naturally, this
2668 ordering then matches the one IPA-passes get in their stmt_fixup
2669 hooks. */
2671 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2672 renumber_gimple_stmt_uids ();
2673 pop_cfun ();
2675 if (node->definition && node->need_lto_streaming)
2676 lto_set_symtab_encoder_in_partition (encoder, node);
2679 FOR_EACH_DEFINED_FUNCTION (node)
2680 if (node->alias && node->need_lto_streaming)
2681 lto_set_symtab_encoder_in_partition (encoder, node);
2682 FOR_EACH_DEFINED_VARIABLE (vnode)
2683 if (vnode->need_lto_streaming)
2684 lto_set_symtab_encoder_in_partition (encoder, vnode);
2686 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2688 free (order);
2689 if (streamer_dump_file)
2691 dump_end (TDI_lto_stream_out, streamer_dump_file);
2692 streamer_dump_file = NULL;
2696 /* Same as execute_pass_list but assume that subpasses of IPA passes
2697 are local passes. If SET is not NULL, write out optimization summaries of
2698 only those node in SET. */
2700 static void
2701 ipa_write_optimization_summaries_1 (opt_pass *pass,
2702 struct lto_out_decl_state *state)
2704 while (pass)
2706 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2707 gcc_assert (!current_function_decl);
2708 gcc_assert (!cfun);
2709 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2710 if (pass->type == IPA_PASS
2711 && ipa_pass->write_optimization_summary
2712 && pass->gate (cfun))
2714 /* If a timevar is present, start it. */
2715 if (pass->tv_id)
2716 timevar_push (pass->tv_id);
2718 pass_init_dump_file (pass);
2720 current_pass = pass;
2721 ipa_pass->write_optimization_summary ();
2723 pass_fini_dump_file (pass);
2725 /* If a timevar is present, start it. */
2726 if (pass->tv_id)
2727 timevar_pop (pass->tv_id);
2730 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2731 ipa_write_optimization_summaries_1 (pass->sub, state);
2733 pass = pass->next;
2737 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2738 NULL, write out all summaries of all nodes. */
2740 void
2741 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2743 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2744 lto_symtab_encoder_iterator lsei;
2745 state->symtab_node_encoder = encoder;
2747 lto_output_init_mode_table ();
2748 lto_push_out_decl_state (state);
2749 for (lsei = lsei_start_function_in_partition (encoder);
2750 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2752 struct cgraph_node *node = lsei_cgraph_node (lsei);
2753 /* When streaming out references to statements as part of some IPA
2754 pass summary, the statements need to have uids assigned.
2756 For functions newly born at WPA stage we need to initialize
2757 the uids here. */
2758 if (node->definition
2759 && gimple_has_body_p (node->decl))
2761 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2762 renumber_gimple_stmt_uids ();
2763 pop_cfun ();
2767 gcc_assert (flag_wpa);
2768 pass_manager *passes = g->get_passes ();
2769 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2771 write_lto ();
2773 gcc_assert (lto_get_out_decl_state () == state);
2774 lto_pop_out_decl_state ();
2775 lto_delete_out_decl_state (state);
2778 /* Same as execute_pass_list but assume that subpasses of IPA passes
2779 are local passes. */
2781 static void
2782 ipa_read_summaries_1 (opt_pass *pass)
2784 while (pass)
2786 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2788 gcc_assert (!current_function_decl);
2789 gcc_assert (!cfun);
2790 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2792 if (pass->gate (cfun))
2794 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2796 /* If a timevar is present, start it. */
2797 if (pass->tv_id)
2798 timevar_push (pass->tv_id);
2800 pass_init_dump_file (pass);
2802 current_pass = pass;
2803 ipa_pass->read_summary ();
2805 pass_fini_dump_file (pass);
2807 /* Stop timevar. */
2808 if (pass->tv_id)
2809 timevar_pop (pass->tv_id);
2812 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2813 ipa_read_summaries_1 (pass->sub);
2815 pass = pass->next;
2820 /* Read all the summaries for all_regular_ipa_passes. */
2822 void
2823 ipa_read_summaries (void)
2825 pass_manager *passes = g->get_passes ();
2826 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2829 /* Same as execute_pass_list but assume that subpasses of IPA passes
2830 are local passes. */
2832 static void
2833 ipa_read_optimization_summaries_1 (opt_pass *pass)
2835 while (pass)
2837 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2839 gcc_assert (!current_function_decl);
2840 gcc_assert (!cfun);
2841 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2843 if (pass->gate (cfun))
2845 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2847 /* If a timevar is present, start it. */
2848 if (pass->tv_id)
2849 timevar_push (pass->tv_id);
2851 pass_init_dump_file (pass);
2853 current_pass = pass;
2854 ipa_pass->read_optimization_summary ();
2856 pass_fini_dump_file (pass);
2858 /* Stop timevar. */
2859 if (pass->tv_id)
2860 timevar_pop (pass->tv_id);
2863 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2864 ipa_read_optimization_summaries_1 (pass->sub);
2866 pass = pass->next;
2870 /* Read all the summaries for all_regular_ipa_passes. */
2872 void
2873 ipa_read_optimization_summaries (void)
2875 pass_manager *passes = g->get_passes ();
2876 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2879 /* Same as execute_pass_list but assume that subpasses of IPA passes
2880 are local passes. */
2881 void
2882 execute_ipa_pass_list (opt_pass *pass)
2886 gcc_assert (!current_function_decl);
2887 gcc_assert (!cfun);
2888 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2889 if (execute_one_pass (pass) && pass->sub)
2891 if (pass->sub->type == GIMPLE_PASS)
2893 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2894 do_per_function_toporder ((void (*)(function *, void *))
2895 execute_pass_list,
2896 pass->sub);
2897 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2899 else if (pass->sub->type == SIMPLE_IPA_PASS
2900 || pass->sub->type == IPA_PASS)
2901 execute_ipa_pass_list (pass->sub);
2902 else
2903 gcc_unreachable ();
2905 gcc_assert (!current_function_decl);
2906 symtab->process_new_functions ();
2907 pass = pass->next;
2909 while (pass);
2912 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2914 static void
2915 execute_ipa_stmt_fixups (opt_pass *pass,
2916 struct cgraph_node *node, gimple **stmts)
2918 while (pass)
2920 /* Execute all of the IPA_PASSes in the list. */
2921 if (pass->type == IPA_PASS
2922 && pass->gate (cfun))
2924 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2926 if (ipa_pass->stmt_fixup)
2928 pass_init_dump_file (pass);
2929 /* If a timevar is present, start it. */
2930 if (pass->tv_id)
2931 timevar_push (pass->tv_id);
2933 current_pass = pass;
2934 ipa_pass->stmt_fixup (node, stmts);
2936 /* Stop timevar. */
2937 if (pass->tv_id)
2938 timevar_pop (pass->tv_id);
2939 pass_fini_dump_file (pass);
2941 if (pass->sub)
2942 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2944 pass = pass->next;
2948 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2950 void
2951 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2953 pass_manager *passes = g->get_passes ();
2954 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2958 extern void debug_properties (unsigned int);
2959 extern void dump_properties (FILE *, unsigned int);
2961 DEBUG_FUNCTION void
2962 dump_properties (FILE *dump, unsigned int props)
2964 fprintf (dump, "Properties:\n");
2965 if (props & PROP_gimple_any)
2966 fprintf (dump, "PROP_gimple_any\n");
2967 if (props & PROP_gimple_lcf)
2968 fprintf (dump, "PROP_gimple_lcf\n");
2969 if (props & PROP_gimple_leh)
2970 fprintf (dump, "PROP_gimple_leh\n");
2971 if (props & PROP_cfg)
2972 fprintf (dump, "PROP_cfg\n");
2973 if (props & PROP_ssa)
2974 fprintf (dump, "PROP_ssa\n");
2975 if (props & PROP_no_crit_edges)
2976 fprintf (dump, "PROP_no_crit_edges\n");
2977 if (props & PROP_rtl)
2978 fprintf (dump, "PROP_rtl\n");
2979 if (props & PROP_gimple_lomp)
2980 fprintf (dump, "PROP_gimple_lomp\n");
2981 if (props & PROP_gimple_lomp_dev)
2982 fprintf (dump, "PROP_gimple_lomp_dev\n");
2983 if (props & PROP_gimple_lcx)
2984 fprintf (dump, "PROP_gimple_lcx\n");
2985 if (props & PROP_gimple_lvec)
2986 fprintf (dump, "PROP_gimple_lvec\n");
2987 if (props & PROP_cfglayout)
2988 fprintf (dump, "PROP_cfglayout\n");
2991 DEBUG_FUNCTION void
2992 debug_properties (unsigned int props)
2994 dump_properties (stderr, props);
2997 /* Called by local passes to see if function is called by already processed nodes.
2998 Because we process nodes in topological order, this means that function is
2999 in recursive cycle or we introduced new direct calls. */
3000 bool
3001 function_called_by_processed_nodes_p (void)
3003 struct cgraph_edge *e;
3004 for (e = cgraph_node::get (current_function_decl)->callers;
3006 e = e->next_caller)
3008 if (e->caller->decl == current_function_decl)
3009 continue;
3010 if (!e->caller->has_gimple_body_p ())
3011 continue;
3012 if (TREE_ASM_WRITTEN (e->caller->decl))
3013 continue;
3014 if (!e->caller->process && !e->caller->global.inlined_to)
3015 break;
3017 if (dump_file && e)
3019 fprintf (dump_file, "Already processed call to:\n");
3020 e->caller->dump (dump_file);
3022 return e != NULL;
3025 #include "gt-passes.h"