PR middle-end/78642
[official-gcc.git] / gcc / passes.c
blob51d0d84b74de3a0db6ead04a26cd517844f59d83
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2016 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"
63 using namespace gcc;
65 /* This is used for debugging. It allows the current pass to printed
66 from anywhere in compilation.
67 The variable current_pass is also used for statistics and plugins. */
68 opt_pass *current_pass;
70 /* Most passes are single-instance (within their context) and thus don't
71 need to implement cloning, but passes that support multiple instances
72 *must* provide their own implementation of the clone method.
74 Handle this by providing a default implemenation, but make it a fatal
75 error to call it. */
77 opt_pass *
78 opt_pass::clone ()
80 internal_error ("pass %s does not support cloning", name);
83 void
84 opt_pass::set_pass_param (unsigned int, bool)
86 internal_error ("pass %s needs a set_pass_param implementation to handle the"
87 " extra argument in NEXT_PASS", name);
90 bool
91 opt_pass::gate (function *)
93 return true;
96 unsigned int
97 opt_pass::execute (function *)
99 return 0;
102 opt_pass::opt_pass (const pass_data &data, context *ctxt)
103 : pass_data (data),
104 sub (NULL),
105 next (NULL),
106 static_pass_number (0),
107 m_ctxt (ctxt)
112 void
113 pass_manager::execute_early_local_passes ()
115 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
116 if (flag_check_pointer_bounds)
117 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
118 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
121 unsigned int
122 pass_manager::execute_pass_mode_switching ()
124 return pass_mode_switching_1->execute (cfun);
128 /* Call from anywhere to find out what pass this is. Useful for
129 printing out debugging information deep inside an service
130 routine. */
131 void
132 print_current_pass (FILE *file)
134 if (current_pass)
135 fprintf (file, "current pass = %s (%d)\n",
136 current_pass->name, current_pass->static_pass_number);
137 else
138 fprintf (file, "no current pass.\n");
142 /* Call from the debugger to get the current pass name. */
143 DEBUG_FUNCTION void
144 debug_pass (void)
146 print_current_pass (stderr);
151 /* Global variables used to communicate with passes. */
152 bool in_gimple_form;
155 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
156 and TYPE_DECL nodes.
158 This does nothing for local (non-static) variables, unless the
159 variable is a register variable with DECL_ASSEMBLER_NAME set. In
160 that case, or if the variable is not an automatic, it sets up the
161 RTL and outputs any assembler code (label definition, storage
162 allocation and initialization).
164 DECL is the declaration. TOP_LEVEL is nonzero
165 if this declaration is not within a function. */
167 void
168 rest_of_decl_compilation (tree decl,
169 int top_level,
170 int at_end)
172 bool finalize = true;
174 /* We deferred calling assemble_alias so that we could collect
175 other attributes such as visibility. Emit the alias now. */
176 if (!in_lto_p)
178 tree alias;
179 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
180 if (alias)
182 alias = TREE_VALUE (TREE_VALUE (alias));
183 alias = get_identifier (TREE_STRING_POINTER (alias));
184 /* A quirk of the initial implementation of aliases required that the
185 user add "extern" to all of them. Which is silly, but now
186 historical. Do note that the symbol is in fact locally defined. */
187 DECL_EXTERNAL (decl) = 0;
188 TREE_STATIC (decl) = 1;
189 assemble_alias (decl, alias);
190 finalize = false;
194 /* Can't defer this, because it needs to happen before any
195 later function definitions are processed. */
196 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
197 make_decl_rtl (decl);
199 /* Forward declarations for nested functions are not "external",
200 but we need to treat them as if they were. */
201 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
202 || TREE_CODE (decl) == FUNCTION_DECL)
204 timevar_push (TV_VARCONST);
206 /* Don't output anything when a tentative file-scope definition
207 is seen. But at end of compilation, do output code for them.
209 We do output all variables and rely on
210 callgraph code to defer them except for forward declarations
211 (see gcc.c-torture/compile/920624-1.c) */
212 if ((at_end
213 || !DECL_DEFER_OUTPUT (decl)
214 || DECL_INITIAL (decl))
215 && (!VAR_P (decl) || !DECL_HAS_VALUE_EXPR_P (decl))
216 && !DECL_EXTERNAL (decl))
218 /* When reading LTO unit, we also read varpool, so do not
219 rebuild it. */
220 if (in_lto_p && !at_end)
222 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
223 varpool_node::finalize_decl (decl);
226 #ifdef ASM_FINISH_DECLARE_OBJECT
227 if (decl == last_assemble_variable_decl)
229 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
230 top_level, at_end);
232 #endif
234 /* Now that we have activated any function-specific attributes
235 that might affect function decl, particularly align, relayout it. */
236 if (TREE_CODE (decl) == FUNCTION_DECL)
237 targetm.target_option.relayout_function (decl);
239 timevar_pop (TV_VARCONST);
241 else if (TREE_CODE (decl) == TYPE_DECL
242 /* Like in rest_of_type_compilation, avoid confusing the debug
243 information machinery when there are errors. */
244 && !seen_error ())
246 timevar_push (TV_SYMOUT);
247 debug_hooks->type_decl (decl, !top_level);
248 timevar_pop (TV_SYMOUT);
251 /* Let cgraph know about the existence of variables. */
252 if (in_lto_p && !at_end)
254 else if (VAR_P (decl) && !DECL_EXTERNAL (decl)
255 && TREE_STATIC (decl))
256 varpool_node::get_create (decl);
258 /* Generate early debug for global variables. Any local variables will
259 be handled by either handling reachable functions from
260 finalize_compilation_unit (and by consequence, locally scoped
261 symbols), or by rest_of_type_compilation below.
263 Also, pick up function prototypes, which will be mostly ignored
264 by the different early_global_decl() hooks, but will at least be
265 used by Go's hijack of the debug_hooks to implement
266 -fdump-go-spec. */
267 if (!in_lto_p
268 && (TREE_CODE (decl) != FUNCTION_DECL
269 /* This will pick up function prototypes with no bodies,
270 which are not visible in finalize_compilation_unit()
271 while iterating with FOR_EACH_*_FUNCTION through the
272 symbol table. */
273 || !DECL_SAVED_TREE (decl))
275 /* We need to check both decl_function_context and
276 current_function_decl here to make sure local extern
277 declarations end up with the correct context.
279 For local extern declarations, decl_function_context is
280 empty, but current_function_decl is set to the function where
281 the extern was declared . Without the check for
282 !current_function_decl below, the local extern ends up
283 incorrectly with a top-level context.
285 For example:
287 namespace S
293 int i = 42;
295 extern int i; // Local extern declaration.
296 return i;
302 && !decl_function_context (decl)
303 && !current_function_decl
304 && DECL_SOURCE_LOCATION (decl) != BUILTINS_LOCATION
305 && (!decl_type_context (decl)
306 /* If we created a varpool node for the decl make sure to
307 call early_global_decl. Otherwise we miss changes
308 introduced by member definitions like
309 struct A { static int staticdatamember; };
310 int A::staticdatamember;
311 and thus have incomplete early debug and late debug
312 called from varpool node removal fails to handle it
313 properly. */
314 || (finalize
315 && VAR_P (decl)
316 && TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
317 /* Avoid confusing the debug information machinery when there are
318 errors. */
319 && !seen_error ())
320 (*debug_hooks->early_global_decl) (decl);
323 /* Called after finishing a record, union or enumeral type. */
325 void
326 rest_of_type_compilation (tree type, int toplev)
328 /* Avoid confusing the debug information machinery when there are
329 errors. */
330 if (seen_error ())
331 return;
333 timevar_push (TV_SYMOUT);
334 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
335 timevar_pop (TV_SYMOUT);
340 void
341 pass_manager::
342 finish_optimization_passes (void)
344 int i;
345 struct dump_file_info *dfi;
346 char *name;
347 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
349 timevar_push (TV_DUMP);
350 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
352 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
353 end_branch_prob ();
354 dumps->dump_finish (pass_profile_1->static_pass_number);
357 if (optimize > 0)
359 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
360 print_combine_total_stats ();
361 dumps->dump_finish (pass_profile_1->static_pass_number);
364 /* Do whatever is necessary to finish printing the graphs. */
365 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
366 if (dfi->graph_dump_initialized)
368 name = dumps->get_dump_file_name (dfi);
369 finish_graph_dump_file (name);
370 free (name);
373 timevar_pop (TV_DUMP);
376 static unsigned int
377 execute_build_ssa_passes (void)
379 /* Once this pass (and its sub-passes) are complete, all functions
380 will be in SSA form. Technically this state change is happening
381 a tad early, since the sub-passes have not yet run, but since
382 none of the sub-passes are IPA passes and do not create new
383 functions, this is ok. We're setting this value for the benefit
384 of IPA passes that follow. */
385 if (symtab->state < IPA_SSA)
386 symtab->state = IPA_SSA;
387 return 0;
390 namespace {
392 const pass_data pass_data_build_ssa_passes =
394 SIMPLE_IPA_PASS, /* type */
395 "build_ssa_passes", /* name */
396 OPTGROUP_NONE, /* optinfo_flags */
397 TV_EARLY_LOCAL, /* tv_id */
398 0, /* properties_required */
399 0, /* properties_provided */
400 0, /* properties_destroyed */
401 0, /* todo_flags_start */
402 /* todo_flags_finish is executed before subpases. For this reason
403 it makes no sense to remove unreachable functions here. */
404 0, /* todo_flags_finish */
407 class pass_build_ssa_passes : public simple_ipa_opt_pass
409 public:
410 pass_build_ssa_passes (gcc::context *ctxt)
411 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
414 /* opt_pass methods: */
415 virtual bool gate (function *)
417 /* Don't bother doing anything if the program has errors. */
418 return (!seen_error () && !in_lto_p);
421 virtual unsigned int execute (function *)
423 return execute_build_ssa_passes ();
426 }; // class pass_build_ssa_passes
428 const pass_data pass_data_chkp_instrumentation_passes =
430 SIMPLE_IPA_PASS, /* type */
431 "chkp_passes", /* name */
432 OPTGROUP_NONE, /* optinfo_flags */
433 TV_NONE, /* tv_id */
434 0, /* properties_required */
435 0, /* properties_provided */
436 0, /* properties_destroyed */
437 0, /* todo_flags_start */
438 0, /* todo_flags_finish */
441 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
443 public:
444 pass_chkp_instrumentation_passes (gcc::context *ctxt)
445 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
448 /* opt_pass methods: */
449 virtual bool gate (function *)
451 /* Don't bother doing anything if the program has errors. */
452 return (flag_check_pointer_bounds
453 && !seen_error () && !in_lto_p);
456 }; // class pass_chkp_instrumentation_passes
458 const pass_data pass_data_local_optimization_passes =
460 SIMPLE_IPA_PASS, /* type */
461 "opt_local_passes", /* name */
462 OPTGROUP_NONE, /* optinfo_flags */
463 TV_NONE, /* tv_id */
464 0, /* properties_required */
465 0, /* properties_provided */
466 0, /* properties_destroyed */
467 0, /* todo_flags_start */
468 0, /* todo_flags_finish */
471 class pass_local_optimization_passes : public simple_ipa_opt_pass
473 public:
474 pass_local_optimization_passes (gcc::context *ctxt)
475 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
478 /* opt_pass methods: */
479 virtual bool gate (function *)
481 /* Don't bother doing anything if the program has errors. */
482 return (!seen_error () && !in_lto_p);
485 }; // class pass_local_optimization_passes
487 } // anon namespace
489 simple_ipa_opt_pass *
490 make_pass_build_ssa_passes (gcc::context *ctxt)
492 return new pass_build_ssa_passes (ctxt);
495 simple_ipa_opt_pass *
496 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
498 return new pass_chkp_instrumentation_passes (ctxt);
501 simple_ipa_opt_pass *
502 make_pass_local_optimization_passes (gcc::context *ctxt)
504 return new pass_local_optimization_passes (ctxt);
507 namespace {
509 const pass_data pass_data_all_early_optimizations =
511 GIMPLE_PASS, /* type */
512 "early_optimizations", /* name */
513 OPTGROUP_NONE, /* optinfo_flags */
514 TV_NONE, /* tv_id */
515 0, /* properties_required */
516 0, /* properties_provided */
517 0, /* properties_destroyed */
518 0, /* todo_flags_start */
519 0, /* todo_flags_finish */
522 class pass_all_early_optimizations : public gimple_opt_pass
524 public:
525 pass_all_early_optimizations (gcc::context *ctxt)
526 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
529 /* opt_pass methods: */
530 virtual bool gate (function *)
532 return (optimize >= 1
533 /* Don't bother doing anything if the program has errors. */
534 && !seen_error ());
537 }; // class pass_all_early_optimizations
539 } // anon namespace
541 static gimple_opt_pass *
542 make_pass_all_early_optimizations (gcc::context *ctxt)
544 return new pass_all_early_optimizations (ctxt);
547 namespace {
549 const pass_data pass_data_all_optimizations =
551 GIMPLE_PASS, /* type */
552 "*all_optimizations", /* name */
553 OPTGROUP_NONE, /* optinfo_flags */
554 TV_OPTIMIZE, /* tv_id */
555 0, /* properties_required */
556 0, /* properties_provided */
557 0, /* properties_destroyed */
558 0, /* todo_flags_start */
559 0, /* todo_flags_finish */
562 class pass_all_optimizations : public gimple_opt_pass
564 public:
565 pass_all_optimizations (gcc::context *ctxt)
566 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
569 /* opt_pass methods: */
570 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
572 }; // class pass_all_optimizations
574 } // anon namespace
576 static gimple_opt_pass *
577 make_pass_all_optimizations (gcc::context *ctxt)
579 return new pass_all_optimizations (ctxt);
582 namespace {
584 const pass_data pass_data_all_optimizations_g =
586 GIMPLE_PASS, /* type */
587 "*all_optimizations_g", /* name */
588 OPTGROUP_NONE, /* optinfo_flags */
589 TV_OPTIMIZE, /* tv_id */
590 0, /* properties_required */
591 0, /* properties_provided */
592 0, /* properties_destroyed */
593 0, /* todo_flags_start */
594 0, /* todo_flags_finish */
597 class pass_all_optimizations_g : public gimple_opt_pass
599 public:
600 pass_all_optimizations_g (gcc::context *ctxt)
601 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
604 /* opt_pass methods: */
605 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
607 }; // class pass_all_optimizations_g
609 } // anon namespace
611 static gimple_opt_pass *
612 make_pass_all_optimizations_g (gcc::context *ctxt)
614 return new pass_all_optimizations_g (ctxt);
617 namespace {
619 const pass_data pass_data_rest_of_compilation =
621 RTL_PASS, /* type */
622 "*rest_of_compilation", /* name */
623 OPTGROUP_NONE, /* optinfo_flags */
624 TV_REST_OF_COMPILATION, /* tv_id */
625 PROP_rtl, /* properties_required */
626 0, /* properties_provided */
627 0, /* properties_destroyed */
628 0, /* todo_flags_start */
629 0, /* todo_flags_finish */
632 class pass_rest_of_compilation : public rtl_opt_pass
634 public:
635 pass_rest_of_compilation (gcc::context *ctxt)
636 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
639 /* opt_pass methods: */
640 virtual bool gate (function *)
642 /* Early return if there were errors. We can run afoul of our
643 consistency checks, and there's not really much point in fixing them. */
644 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
647 }; // class pass_rest_of_compilation
649 } // anon namespace
651 static rtl_opt_pass *
652 make_pass_rest_of_compilation (gcc::context *ctxt)
654 return new pass_rest_of_compilation (ctxt);
657 namespace {
659 const pass_data pass_data_postreload =
661 RTL_PASS, /* type */
662 "*all-postreload", /* name */
663 OPTGROUP_NONE, /* optinfo_flags */
664 TV_POSTRELOAD, /* tv_id */
665 PROP_rtl, /* properties_required */
666 0, /* properties_provided */
667 0, /* properties_destroyed */
668 0, /* todo_flags_start */
669 0, /* todo_flags_finish */
672 class pass_postreload : public rtl_opt_pass
674 public:
675 pass_postreload (gcc::context *ctxt)
676 : rtl_opt_pass (pass_data_postreload, ctxt)
679 /* opt_pass methods: */
680 virtual bool gate (function *) { return reload_completed; }
682 }; // class pass_postreload
684 } // anon namespace
686 static rtl_opt_pass *
687 make_pass_postreload (gcc::context *ctxt)
689 return new pass_postreload (ctxt);
692 namespace {
694 const pass_data pass_data_late_compilation =
696 RTL_PASS, /* type */
697 "*all-late_compilation", /* name */
698 OPTGROUP_NONE, /* optinfo_flags */
699 TV_LATE_COMPILATION, /* tv_id */
700 PROP_rtl, /* properties_required */
701 0, /* properties_provided */
702 0, /* properties_destroyed */
703 0, /* todo_flags_start */
704 0, /* todo_flags_finish */
707 class pass_late_compilation : public rtl_opt_pass
709 public:
710 pass_late_compilation (gcc::context *ctxt)
711 : rtl_opt_pass (pass_data_late_compilation, ctxt)
714 /* opt_pass methods: */
715 virtual bool gate (function *)
717 return reload_completed || targetm.no_register_allocation;
720 }; // class pass_late_compilation
722 } // anon namespace
724 static rtl_opt_pass *
725 make_pass_late_compilation (gcc::context *ctxt)
727 return new pass_late_compilation (ctxt);
732 /* Set the static pass number of pass PASS to ID and record that
733 in the mapping from static pass number to pass. */
735 void
736 pass_manager::
737 set_pass_for_id (int id, opt_pass *pass)
739 pass->static_pass_number = id;
740 if (passes_by_id_size <= id)
742 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
743 memset (passes_by_id + passes_by_id_size, 0,
744 (id + 1 - passes_by_id_size) * sizeof (void *));
745 passes_by_id_size = id + 1;
747 passes_by_id[id] = pass;
750 /* Return the pass with the static pass number ID. */
752 opt_pass *
753 pass_manager::get_pass_for_id (int id) const
755 if (id >= passes_by_id_size)
756 return NULL;
757 return passes_by_id[id];
760 /* Iterate over the pass tree allocating dump file numbers. We want
761 to do this depth first, and independent of whether the pass is
762 enabled or not. */
764 void
765 register_one_dump_file (opt_pass *pass)
767 g->get_passes ()->register_one_dump_file (pass);
770 void
771 pass_manager::register_one_dump_file (opt_pass *pass)
773 char *dot_name, *flag_name, *glob_name;
774 const char *name, *full_name, *prefix;
776 /* Buffer big enough to format a 32-bit UINT_MAX into. */
777 char num[11];
778 int flags, id;
779 int optgroup_flags = OPTGROUP_NONE;
780 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
782 /* See below in next_pass_1. */
783 num[0] = '\0';
784 if (pass->static_pass_number != -1)
785 sprintf (num, "%u", ((int) pass->static_pass_number < 0
786 ? 1 : pass->static_pass_number));
788 /* The name is both used to identify the pass for the purposes of plugins,
789 and to specify dump file name and option.
790 The latter two might want something short which is not quite unique; for
791 that reason, we may have a disambiguating prefix, followed by a space
792 to mark the start of the following dump file name / option string. */
793 name = strchr (pass->name, ' ');
794 name = name ? name + 1 : pass->name;
795 dot_name = concat (".", name, num, NULL);
796 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
798 prefix = "ipa-";
799 flags = TDF_IPA;
800 optgroup_flags |= OPTGROUP_IPA;
802 else if (pass->type == GIMPLE_PASS)
804 prefix = "tree-";
805 flags = TDF_TREE;
807 else
809 prefix = "rtl-";
810 flags = TDF_RTL;
813 flag_name = concat (prefix, name, num, NULL);
814 glob_name = concat (prefix, name, NULL);
815 optgroup_flags |= pass->optinfo_flags;
816 /* For any passes that do not have an optgroup set, and which are not
817 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
818 any dump messages are emitted properly under -fopt-info(-optall). */
819 if (optgroup_flags == OPTGROUP_NONE)
820 optgroup_flags = OPTGROUP_OTHER;
821 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
822 optgroup_flags,
823 true);
824 set_pass_for_id (id, pass);
825 full_name = concat (prefix, pass->name, num, NULL);
826 register_pass_name (pass, full_name);
827 free (CONST_CAST (char *, full_name));
830 /* Register the dump files for the pass_manager starting at PASS. */
832 void
833 pass_manager::register_dump_files (opt_pass *pass)
837 if (pass->name && pass->name[0] != '*')
838 register_one_dump_file (pass);
840 if (pass->sub)
841 register_dump_files (pass->sub);
843 pass = pass->next;
845 while (pass);
848 /* Register PASS with NAME. */
850 void
851 pass_manager::register_pass_name (opt_pass *pass, const char *name)
853 if (!m_name_to_pass_map)
854 m_name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
856 if (m_name_to_pass_map->get (name))
857 return; /* Ignore plugin passes. */
859 const char *unique_name = xstrdup (name);
860 m_name_to_pass_map->put (unique_name, pass);
863 /* Map from pass id to canonicalized pass name. */
865 typedef const char *char_ptr;
866 static vec<char_ptr> pass_tab;
868 /* Callback function for traversing NAME_TO_PASS_MAP. */
870 bool
871 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
873 gcc_assert (pass->static_pass_number > 0);
874 gcc_assert (pass_tab.exists ());
876 pass_tab[pass->static_pass_number] = name;
878 return 1;
881 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
882 table for dumping purpose. */
884 void
885 pass_manager::create_pass_tab (void) const
887 if (!flag_dump_passes)
888 return;
890 pass_tab.safe_grow_cleared (passes_by_id_size + 1);
891 m_name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
894 static bool override_gate_status (opt_pass *, tree, bool);
896 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
897 is turned on or not. */
899 static void
900 dump_one_pass (opt_pass *pass, int pass_indent)
902 int indent = 3 * pass_indent;
903 const char *pn;
904 bool is_on, is_really_on;
906 is_on = pass->gate (cfun);
907 is_really_on = override_gate_status (pass, current_function_decl, is_on);
909 if (pass->static_pass_number <= 0)
910 pn = pass->name;
911 else
912 pn = pass_tab[pass->static_pass_number];
914 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
915 (15 - indent < 0 ? 0 : 15 - indent), " ",
916 is_on ? " ON" : " OFF",
917 ((!is_on) == (!is_really_on) ? ""
918 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
921 /* Dump pass list PASS with indentation INDENT. */
923 static void
924 dump_pass_list (opt_pass *pass, int indent)
928 dump_one_pass (pass, indent);
929 if (pass->sub)
930 dump_pass_list (pass->sub, indent + 1);
931 pass = pass->next;
933 while (pass);
936 /* Dump all optimization passes. */
938 void
939 dump_passes (void)
941 g->get_passes ()->dump_passes ();
944 void
945 pass_manager::dump_passes () const
947 push_dummy_function (true);
949 create_pass_tab ();
951 dump_pass_list (all_lowering_passes, 1);
952 dump_pass_list (all_small_ipa_passes, 1);
953 dump_pass_list (all_regular_ipa_passes, 1);
954 dump_pass_list (all_late_ipa_passes, 1);
955 dump_pass_list (all_passes, 1);
957 pop_dummy_function ();
960 /* Returns the pass with NAME. */
962 opt_pass *
963 pass_manager::get_pass_by_name (const char *name)
965 opt_pass **p = m_name_to_pass_map->get (name);
966 if (p)
967 return *p;
969 return NULL;
973 /* Range [start, last]. */
975 struct uid_range
977 unsigned int start;
978 unsigned int last;
979 const char *assem_name;
980 struct uid_range *next;
983 typedef struct uid_range *uid_range_p;
986 static vec<uid_range_p> enabled_pass_uid_range_tab;
987 static vec<uid_range_p> disabled_pass_uid_range_tab;
990 /* Parse option string for -fdisable- and -fenable-
991 The syntax of the options:
993 -fenable-<pass_name>
994 -fdisable-<pass_name>
996 -fenable-<pass_name>=s1:e1,s2:e2,...
997 -fdisable-<pass_name>=s1:e1,s2:e2,...
1000 static void
1001 enable_disable_pass (const char *arg, bool is_enable)
1003 opt_pass *pass;
1004 char *range_str, *phase_name;
1005 char *argstr = xstrdup (arg);
1006 vec<uid_range_p> *tab = 0;
1008 range_str = strchr (argstr,'=');
1009 if (range_str)
1011 *range_str = '\0';
1012 range_str++;
1015 phase_name = argstr;
1016 if (!*phase_name)
1018 if (is_enable)
1019 error ("unrecognized option -fenable");
1020 else
1021 error ("unrecognized option -fdisable");
1022 free (argstr);
1023 return;
1025 pass = g->get_passes ()->get_pass_by_name (phase_name);
1026 if (!pass || pass->static_pass_number == -1)
1028 if (is_enable)
1029 error ("unknown pass %s specified in -fenable", phase_name);
1030 else
1031 error ("unknown pass %s specified in -fdisable", phase_name);
1032 free (argstr);
1033 return;
1036 if (is_enable)
1037 tab = &enabled_pass_uid_range_tab;
1038 else
1039 tab = &disabled_pass_uid_range_tab;
1041 if ((unsigned) pass->static_pass_number >= tab->length ())
1042 tab->safe_grow_cleared (pass->static_pass_number + 1);
1044 if (!range_str)
1046 uid_range_p slot;
1047 uid_range_p new_range = XCNEW (struct uid_range);
1049 new_range->start = 0;
1050 new_range->last = (unsigned)-1;
1052 slot = (*tab)[pass->static_pass_number];
1053 new_range->next = slot;
1054 (*tab)[pass->static_pass_number] = new_range;
1055 if (is_enable)
1056 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1057 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1058 else
1059 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1060 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1062 else
1064 char *next_range = NULL;
1065 char *one_range = range_str;
1066 char *end_val = NULL;
1070 uid_range_p slot;
1071 uid_range_p new_range;
1072 char *invalid = NULL;
1073 long start;
1074 char *func_name = NULL;
1076 next_range = strchr (one_range, ',');
1077 if (next_range)
1079 *next_range = '\0';
1080 next_range++;
1083 end_val = strchr (one_range, ':');
1084 if (end_val)
1086 *end_val = '\0';
1087 end_val++;
1089 start = strtol (one_range, &invalid, 10);
1090 if (*invalid || start < 0)
1092 if (end_val || (one_range[0] >= '0'
1093 && one_range[0] <= '9'))
1095 error ("Invalid range %s in option %s",
1096 one_range,
1097 is_enable ? "-fenable" : "-fdisable");
1098 free (argstr);
1099 return;
1101 func_name = one_range;
1103 if (!end_val)
1105 new_range = XCNEW (struct uid_range);
1106 if (!func_name)
1108 new_range->start = (unsigned) start;
1109 new_range->last = (unsigned) start;
1111 else
1113 new_range->start = (unsigned) -1;
1114 new_range->last = (unsigned) -1;
1115 new_range->assem_name = xstrdup (func_name);
1118 else
1120 long last = strtol (end_val, &invalid, 10);
1121 if (*invalid || last < start)
1123 error ("Invalid range %s in option %s",
1124 end_val,
1125 is_enable ? "-fenable" : "-fdisable");
1126 free (argstr);
1127 return;
1129 new_range = XCNEW (struct uid_range);
1130 new_range->start = (unsigned) start;
1131 new_range->last = (unsigned) last;
1134 slot = (*tab)[pass->static_pass_number];
1135 new_range->next = slot;
1136 (*tab)[pass->static_pass_number] = new_range;
1137 if (is_enable)
1139 if (new_range->assem_name)
1140 inform (UNKNOWN_LOCATION,
1141 "enable pass %s for function %s",
1142 phase_name, new_range->assem_name);
1143 else
1144 inform (UNKNOWN_LOCATION,
1145 "enable pass %s for functions in the range of [%u, %u]",
1146 phase_name, new_range->start, new_range->last);
1148 else
1150 if (new_range->assem_name)
1151 inform (UNKNOWN_LOCATION,
1152 "disable pass %s for function %s",
1153 phase_name, new_range->assem_name);
1154 else
1155 inform (UNKNOWN_LOCATION,
1156 "disable pass %s for functions in the range of [%u, %u]",
1157 phase_name, new_range->start, new_range->last);
1160 one_range = next_range;
1161 } while (next_range);
1164 free (argstr);
1167 /* Enable pass specified by ARG. */
1169 void
1170 enable_pass (const char *arg)
1172 enable_disable_pass (arg, true);
1175 /* Disable pass specified by ARG. */
1177 void
1178 disable_pass (const char *arg)
1180 enable_disable_pass (arg, false);
1183 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1185 static bool
1186 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1187 tree func,
1188 vec<uid_range_p> tab)
1190 uid_range_p slot, range;
1191 int cgraph_uid;
1192 const char *aname = NULL;
1194 if (!tab.exists ()
1195 || (unsigned) pass->static_pass_number >= tab.length ()
1196 || pass->static_pass_number == -1)
1197 return false;
1199 slot = tab[pass->static_pass_number];
1200 if (!slot)
1201 return false;
1203 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1204 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1205 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1207 range = slot;
1208 while (range)
1210 if ((unsigned) cgraph_uid >= range->start
1211 && (unsigned) cgraph_uid <= range->last)
1212 return true;
1213 if (range->assem_name && aname
1214 && !strcmp (range->assem_name, aname))
1215 return true;
1216 range = range->next;
1219 return false;
1223 /* Update static_pass_number for passes (and the flag
1224 TODO_mark_first_instance).
1226 Passes are constructed with static_pass_number preinitialized to 0
1228 This field is used in two different ways: initially as instance numbers
1229 of their kind, and then as ids within the entire pass manager.
1231 Within pass_manager::pass_manager:
1233 * In add_pass_instance(), as called by next_pass_1 in
1234 NEXT_PASS in init_optimization_passes
1236 * When the initial instance of a pass within a pass manager is seen,
1237 it is flagged, and its static_pass_number is set to -1
1239 * On subsequent times that it is seen, the static pass number
1240 is decremented each time, so that if there are e.g. 4 dups,
1241 they have static_pass_number -4, 2, 3, 4 respectively (note
1242 how the initial one is negative and gives the count); these
1243 can be thought of as instance numbers of the specific pass
1245 * Within the register_dump_files () traversal, set_pass_for_id()
1246 is called on each pass, using these instance numbers to create
1247 dumpfile switches, and then overwriting them with a pass id,
1248 which are global to the whole pass manager (based on
1249 (TDI_end + current value of extra_dump_files_in_use) ) */
1251 static void
1252 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1253 opt_pass *initial_pass)
1255 /* Are we dealing with the first pass of its kind, or a clone? */
1256 if (new_pass != initial_pass)
1258 /* We're dealing with a clone. */
1259 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1261 /* Indicate to register_dump_files that this pass has duplicates,
1262 and so it should rename the dump file. The first instance will
1263 be -1, and be number of duplicates = -static_pass_number - 1.
1264 Subsequent instances will be > 0 and just the duplicate number. */
1265 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1267 initial_pass->static_pass_number -= 1;
1268 new_pass->static_pass_number = -initial_pass->static_pass_number;
1271 else
1273 /* We're dealing with the first pass of its kind. */
1274 new_pass->todo_flags_start |= TODO_mark_first_instance;
1275 new_pass->static_pass_number = -1;
1277 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1281 /* Add a pass to the pass list. Duplicate the pass if it's already
1282 in the list. */
1284 static opt_pass **
1285 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1287 /* Every pass should have a name so that plugins can refer to them. */
1288 gcc_assert (pass->name != NULL);
1290 add_pass_instance (pass, false, initial_pass);
1291 *list = pass;
1293 return &(*list)->next;
1296 /* List node for an inserted pass instance. We need to keep track of all
1297 the newly-added pass instances (with 'added_pass_nodes' defined below)
1298 so that we can register their dump files after pass-positioning is finished.
1299 Registering dumping files needs to be post-processed or the
1300 static_pass_number of the opt_pass object would be modified and mess up
1301 the dump file names of future pass instances to be added. */
1303 struct pass_list_node
1305 opt_pass *pass;
1306 struct pass_list_node *next;
1309 static struct pass_list_node *added_pass_nodes = NULL;
1310 static struct pass_list_node *prev_added_pass_node;
1312 /* Insert the pass at the proper position. Return true if the pass
1313 is successfully added.
1315 NEW_PASS_INFO - new pass to be inserted
1316 PASS_LIST - root of the pass list to insert the new pass to */
1318 static bool
1319 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1321 opt_pass *pass = *pass_list, *prev_pass = NULL;
1322 bool success = false;
1324 for ( ; pass; prev_pass = pass, pass = pass->next)
1326 /* Check if the current pass is of the same type as the new pass and
1327 matches the name and the instance number of the reference pass. */
1328 if (pass->type == new_pass_info->pass->type
1329 && pass->name
1330 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1331 && ((new_pass_info->ref_pass_instance_number == 0)
1332 || (new_pass_info->ref_pass_instance_number ==
1333 pass->static_pass_number)
1334 || (new_pass_info->ref_pass_instance_number == 1
1335 && pass->todo_flags_start & TODO_mark_first_instance)))
1337 opt_pass *new_pass;
1338 struct pass_list_node *new_pass_node;
1340 if (new_pass_info->ref_pass_instance_number == 0)
1342 new_pass = new_pass_info->pass->clone ();
1343 add_pass_instance (new_pass, true, new_pass_info->pass);
1345 else
1347 new_pass = new_pass_info->pass;
1348 add_pass_instance (new_pass, true, new_pass);
1351 /* Insert the new pass instance based on the positioning op. */
1352 switch (new_pass_info->pos_op)
1354 case PASS_POS_INSERT_AFTER:
1355 new_pass->next = pass->next;
1356 pass->next = new_pass;
1358 /* Skip newly inserted pass to avoid repeated
1359 insertions in the case where the new pass and the
1360 existing one have the same name. */
1361 pass = new_pass;
1362 break;
1363 case PASS_POS_INSERT_BEFORE:
1364 new_pass->next = pass;
1365 if (prev_pass)
1366 prev_pass->next = new_pass;
1367 else
1368 *pass_list = new_pass;
1369 break;
1370 case PASS_POS_REPLACE:
1371 new_pass->next = pass->next;
1372 if (prev_pass)
1373 prev_pass->next = new_pass;
1374 else
1375 *pass_list = new_pass;
1376 new_pass->sub = pass->sub;
1377 new_pass->tv_id = pass->tv_id;
1378 pass = new_pass;
1379 break;
1380 default:
1381 error ("invalid pass positioning operation");
1382 return false;
1385 /* Save the newly added pass (instance) in the added_pass_nodes
1386 list so that we can register its dump file later. Note that
1387 we cannot register the dump file now because doing so will modify
1388 the static_pass_number of the opt_pass object and therefore
1389 mess up the dump file name of future instances. */
1390 new_pass_node = XCNEW (struct pass_list_node);
1391 new_pass_node->pass = new_pass;
1392 if (!added_pass_nodes)
1393 added_pass_nodes = new_pass_node;
1394 else
1395 prev_added_pass_node->next = new_pass_node;
1396 prev_added_pass_node = new_pass_node;
1398 success = true;
1401 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1402 success = true;
1405 return success;
1408 /* Hooks a new pass into the pass lists.
1410 PASS_INFO - pass information that specifies the opt_pass object,
1411 reference pass, instance number, and how to position
1412 the pass */
1414 void
1415 register_pass (struct register_pass_info *pass_info)
1417 g->get_passes ()->register_pass (pass_info);
1420 void
1421 register_pass (opt_pass* pass, pass_positioning_ops pos,
1422 const char* ref_pass_name, int ref_pass_inst_number)
1424 register_pass_info i;
1425 i.pass = pass;
1426 i.reference_pass_name = ref_pass_name;
1427 i.ref_pass_instance_number = ref_pass_inst_number;
1428 i.pos_op = pos;
1430 g->get_passes ()->register_pass (&i);
1433 void
1434 pass_manager::register_pass (struct register_pass_info *pass_info)
1436 bool all_instances, success;
1437 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1439 /* The checks below could fail in buggy plugins. Existing GCC
1440 passes should never fail these checks, so we mention plugin in
1441 the messages. */
1442 if (!pass_info->pass)
1443 fatal_error (input_location, "plugin cannot register a missing pass");
1445 if (!pass_info->pass->name)
1446 fatal_error (input_location, "plugin cannot register an unnamed pass");
1448 if (!pass_info->reference_pass_name)
1449 fatal_error
1450 (input_location,
1451 "plugin cannot register pass %qs without reference pass name",
1452 pass_info->pass->name);
1454 /* Try to insert the new pass to the pass lists. We need to check
1455 all five lists as the reference pass could be in one (or all) of
1456 them. */
1457 all_instances = pass_info->ref_pass_instance_number == 0;
1458 success = position_pass (pass_info, &all_lowering_passes);
1459 if (!success || all_instances)
1460 success |= position_pass (pass_info, &all_small_ipa_passes);
1461 if (!success || all_instances)
1462 success |= position_pass (pass_info, &all_regular_ipa_passes);
1463 if (!success || all_instances)
1464 success |= position_pass (pass_info, &all_late_ipa_passes);
1465 if (!success || all_instances)
1466 success |= position_pass (pass_info, &all_passes);
1467 if (!success)
1468 fatal_error
1469 (input_location,
1470 "pass %qs not found but is referenced by new pass %qs",
1471 pass_info->reference_pass_name, pass_info->pass->name);
1473 /* OK, we have successfully inserted the new pass. We need to register
1474 the dump files for the newly added pass and its duplicates (if any).
1475 Because the registration of plugin/backend passes happens after the
1476 command-line options are parsed, the options that specify single
1477 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1478 passes. Therefore we currently can only enable dumping of
1479 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1480 are specified. While doing so, we also delete the pass_list_node
1481 objects created during pass positioning. */
1482 while (added_pass_nodes)
1484 struct pass_list_node *next_node = added_pass_nodes->next;
1485 enum tree_dump_index tdi;
1486 register_one_dump_file (added_pass_nodes->pass);
1487 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1488 || added_pass_nodes->pass->type == IPA_PASS)
1489 tdi = TDI_ipa_all;
1490 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1491 tdi = TDI_tree_all;
1492 else
1493 tdi = TDI_rtl_all;
1494 /* Check if dump-all flag is specified. */
1495 if (dumps->get_dump_file_info (tdi)->pstate)
1497 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1498 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1499 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1500 ->pflags = dumps->get_dump_file_info (tdi)->pflags;
1502 XDELETE (added_pass_nodes);
1503 added_pass_nodes = next_node;
1507 /* Construct the pass tree. The sequencing of passes is driven by
1508 the cgraph routines:
1510 finalize_compilation_unit ()
1511 for each node N in the cgraph
1512 cgraph_analyze_function (N)
1513 cgraph_lower_function (N) -> all_lowering_passes
1515 If we are optimizing, compile is then invoked:
1517 compile ()
1518 ipa_passes () -> all_small_ipa_passes
1519 -> Analysis of all_regular_ipa_passes
1520 * possible LTO streaming at copmilation time *
1521 -> Execution of all_regular_ipa_passes
1522 * possible LTO streaming at link time *
1523 -> all_late_ipa_passes
1524 expand_all_functions ()
1525 for each node N in the cgraph
1526 expand_function (N) -> Transformation of all_regular_ipa_passes
1527 -> all_passes
1530 void *
1531 pass_manager::operator new (size_t sz)
1533 /* Ensure that all fields of the pass manager are zero-initialized. */
1534 return xcalloc (1, sz);
1537 void
1538 pass_manager::operator delete (void *ptr)
1540 free (ptr);
1543 pass_manager::pass_manager (context *ctxt)
1544 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1545 all_regular_ipa_passes (NULL),
1546 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1547 m_ctxt (ctxt)
1549 opt_pass **p;
1551 /* Initialize the pass_lists array. */
1552 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1553 GCC_PASS_LISTS
1554 #undef DEF_PASS_LIST
1556 /* Build the tree of passes. */
1558 #define INSERT_PASSES_AFTER(PASS) \
1560 opt_pass **p_start; \
1561 p_start = p = &(PASS);
1563 #define TERMINATE_PASS_LIST(PASS) \
1564 gcc_assert (p_start == &PASS); \
1565 *p = NULL; \
1568 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1570 opt_pass **p = &(PASS ## _1)->sub;
1572 #define POP_INSERT_PASSES() \
1575 #define NEXT_PASS(PASS, NUM) \
1576 do { \
1577 gcc_assert (NULL == PASS ## _ ## NUM); \
1578 if ((NUM) == 1) \
1579 PASS ## _1 = make_##PASS (m_ctxt); \
1580 else \
1582 gcc_assert (PASS ## _1); \
1583 PASS ## _ ## NUM = PASS ## _1->clone (); \
1585 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1586 } while (0)
1588 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) \
1589 do { \
1590 NEXT_PASS (PASS, NUM); \
1591 PASS ## _ ## NUM->set_pass_param (0, ARG); \
1592 } while (0)
1594 #include "pass-instances.def"
1596 #undef INSERT_PASSES_AFTER
1597 #undef PUSH_INSERT_PASSES_WITHIN
1598 #undef POP_INSERT_PASSES
1599 #undef NEXT_PASS
1600 #undef NEXT_PASS_WITH_ARG
1601 #undef TERMINATE_PASS_LIST
1603 /* Register the passes with the tree dump code. */
1604 register_dump_files (all_lowering_passes);
1605 register_dump_files (all_small_ipa_passes);
1606 register_dump_files (all_regular_ipa_passes);
1607 register_dump_files (all_late_ipa_passes);
1608 register_dump_files (all_passes);
1611 static void
1612 delete_pass_tree (opt_pass *pass)
1614 while (pass)
1616 /* Recurse into child passes. */
1617 delete_pass_tree (pass->sub);
1619 opt_pass *next = pass->next;
1621 /* Delete this pass. */
1622 delete pass;
1624 /* Iterate onto sibling passes. */
1625 pass = next;
1629 pass_manager::~pass_manager ()
1631 XDELETEVEC (passes_by_id);
1633 /* Call delete_pass_tree on each of the pass_lists. */
1634 #define DEF_PASS_LIST(LIST) \
1635 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1636 GCC_PASS_LISTS
1637 #undef DEF_PASS_LIST
1641 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1642 function CALLBACK for every function in the call graph. Otherwise,
1643 call CALLBACK on the current function. */
1645 static void
1646 do_per_function (void (*callback) (function *, void *data), void *data)
1648 if (current_function_decl)
1649 callback (cfun, data);
1650 else
1652 struct cgraph_node *node;
1653 FOR_EACH_DEFINED_FUNCTION (node)
1654 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1655 && (!node->clone_of || node->decl != node->clone_of->decl))
1656 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1660 /* Because inlining might remove no-longer reachable nodes, we need to
1661 keep the array visible to garbage collector to avoid reading collected
1662 out nodes. */
1663 static int nnodes;
1664 static GTY ((length ("nnodes"))) cgraph_node **order;
1666 /* Hook called when NODE is removed and therefore should be
1667 excluded from order vector. DATA is an array of integers.
1668 DATA[0] holds max index it may be accessed by. For cgraph
1669 node DATA[node->uid + 1] holds index of this node in order
1670 vector. */
1671 static void
1672 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1674 int *order_idx = (int *)data;
1676 if (node->uid >= order_idx[0])
1677 return;
1679 int idx = order_idx[node->uid + 1];
1680 if (idx >= 0 && idx < nnodes && order[idx] == node)
1681 order[idx] = NULL;
1684 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1685 function CALLBACK for every function in the call graph. Otherwise,
1686 call CALLBACK on the current function.
1687 This function is global so that plugins can use it. */
1688 void
1689 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1691 int i;
1693 if (current_function_decl)
1694 callback (cfun, data);
1695 else
1697 cgraph_node_hook_list *hook;
1698 int *order_idx;
1699 gcc_assert (!order);
1700 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1702 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1703 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1704 order_idx[0] = symtab->cgraph_max_uid;
1706 nnodes = ipa_reverse_postorder (order);
1707 for (i = nnodes - 1; i >= 0; i--)
1709 order[i]->process = 1;
1710 order_idx[order[i]->uid + 1] = i;
1712 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1713 order_idx);
1714 for (i = nnodes - 1; i >= 0; i--)
1716 /* Function could be inlined and removed as unreachable. */
1717 if (!order[i])
1718 continue;
1720 struct cgraph_node *node = order[i];
1722 /* Allow possibly removed nodes to be garbage collected. */
1723 order[i] = NULL;
1724 node->process = 0;
1725 if (node->has_gimple_body_p ())
1727 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1728 push_cfun (fn);
1729 callback (fn, data);
1730 pop_cfun ();
1733 symtab->remove_cgraph_removal_hook (hook);
1735 ggc_free (order);
1736 order = NULL;
1737 nnodes = 0;
1740 /* Helper function to perform function body dump. */
1742 static void
1743 execute_function_dump (function *fn, void *data)
1745 opt_pass *pass = (opt_pass *)data;
1747 if (dump_file)
1749 push_cfun (fn);
1751 if (fn->curr_properties & PROP_trees)
1752 dump_function_to_file (fn->decl, dump_file, dump_flags);
1753 else
1754 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1756 /* Flush the file. If verification fails, we won't be able to
1757 close the file before aborting. */
1758 fflush (dump_file);
1760 if ((fn->curr_properties & PROP_cfg)
1761 && (dump_flags & TDF_GRAPH))
1763 gcc::dump_manager *dumps = g->get_dumps ();
1764 struct dump_file_info *dfi
1765 = dumps->get_dump_file_info (pass->static_pass_number);
1766 if (!dfi->graph_dump_initialized)
1768 clean_graph_dump_file (dump_file_name);
1769 dfi->graph_dump_initialized = true;
1771 print_graph_cfg (dump_file_name, fn);
1774 pop_cfun ();
1778 static struct profile_record *profile_record;
1780 /* Do profile consistency book-keeping for the pass with static number INDEX.
1781 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1782 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1783 if we are only book-keeping on passes that may have selectively disabled
1784 themselves on a given function. */
1785 static void
1786 check_profile_consistency (int index, int subpass, bool run)
1788 pass_manager *passes = g->get_passes ();
1789 if (index == -1)
1790 return;
1791 if (!profile_record)
1792 profile_record = XCNEWVEC (struct profile_record,
1793 passes->passes_by_id_size);
1794 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1795 gcc_assert (subpass < 2);
1796 profile_record[index].run |= run;
1797 account_profile_record (&profile_record[index], subpass);
1800 /* Output profile consistency. */
1802 void
1803 dump_profile_report (void)
1805 g->get_passes ()->dump_profile_report ();
1808 void
1809 pass_manager::dump_profile_report () const
1811 int i, j;
1812 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1813 gcov_type last_time = 0, last_size = 0;
1814 double rel_time_change, rel_size_change;
1815 int last_reported = 0;
1817 if (!profile_record)
1818 return;
1819 fprintf (stderr, "\nProfile consistency report:\n\n");
1820 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1821 fprintf (stderr, " |freq count |freq count |size time\n");
1823 for (i = 0; i < passes_by_id_size; i++)
1824 for (j = 0 ; j < 2; j++)
1825 if (profile_record[i].run)
1827 if (last_time)
1828 rel_time_change = (profile_record[i].time[j]
1829 - (double)last_time) * 100 / (double)last_time;
1830 else
1831 rel_time_change = 0;
1832 if (last_size)
1833 rel_size_change = (profile_record[i].size[j]
1834 - (double)last_size) * 100 / (double)last_size;
1835 else
1836 rel_size_change = 0;
1838 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1839 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1840 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1841 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1842 || rel_time_change || rel_size_change)
1844 last_reported = i;
1845 fprintf (stderr, "%-20s %s",
1846 passes_by_id [i]->name,
1847 j ? "(after TODO)" : " ");
1848 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1849 fprintf (stderr, "| %+5i",
1850 profile_record[i].num_mismatched_freq_in[j]
1851 - last_freq_in);
1852 else
1853 fprintf (stderr, "| ");
1854 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1855 fprintf (stderr, " %+5i",
1856 profile_record[i].num_mismatched_count_in[j]
1857 - last_count_in);
1858 else
1859 fprintf (stderr, " ");
1860 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1861 fprintf (stderr, "| %+5i",
1862 profile_record[i].num_mismatched_freq_out[j]
1863 - last_freq_out);
1864 else
1865 fprintf (stderr, "| ");
1866 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1867 fprintf (stderr, " %+5i",
1868 profile_record[i].num_mismatched_count_out[j]
1869 - last_count_out);
1870 else
1871 fprintf (stderr, " ");
1873 /* Size/time units change across gimple and RTL. */
1874 if (i == pass_expand_1->static_pass_number)
1875 fprintf (stderr, "|----------");
1876 else
1878 if (rel_size_change)
1879 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1880 else
1881 fprintf (stderr, "| ");
1882 if (rel_time_change)
1883 fprintf (stderr, " %+8.4f%%", rel_time_change);
1885 fprintf (stderr, "\n");
1886 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1887 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1888 last_count_in = profile_record[i].num_mismatched_count_in[j];
1889 last_count_out = profile_record[i].num_mismatched_count_out[j];
1891 else if (j && last_reported != i)
1893 last_reported = i;
1894 fprintf (stderr, "%-20s ------------| | |\n",
1895 passes_by_id [i]->name);
1897 last_time = profile_record[i].time[j];
1898 last_size = profile_record[i].size[j];
1902 /* Perform all TODO actions that ought to be done on each function. */
1904 static void
1905 execute_function_todo (function *fn, void *data)
1907 bool from_ipa_pass = (cfun == NULL);
1908 unsigned int flags = (size_t)data;
1909 flags &= ~fn->last_verified;
1910 if (!flags)
1911 return;
1913 push_cfun (fn);
1915 /* Always cleanup the CFG before trying to update SSA. */
1916 if (flags & TODO_cleanup_cfg)
1918 cleanup_tree_cfg ();
1920 /* When cleanup_tree_cfg merges consecutive blocks, it may
1921 perform some simplistic propagation when removing single
1922 valued PHI nodes. This propagation may, in turn, cause the
1923 SSA form to become out-of-date (see PR 22037). So, even
1924 if the parent pass had not scheduled an SSA update, we may
1925 still need to do one. */
1926 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1927 flags |= TODO_update_ssa;
1930 if (flags & TODO_update_ssa_any)
1932 unsigned update_flags = flags & TODO_update_ssa_any;
1933 update_ssa (update_flags);
1936 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1937 compute_may_aliases ();
1939 if (optimize && (flags & TODO_update_address_taken))
1940 execute_update_addresses_taken ();
1942 if (flags & TODO_remove_unused_locals)
1943 remove_unused_locals ();
1945 if (flags & TODO_rebuild_frequencies)
1946 rebuild_frequencies ();
1948 if (flags & TODO_rebuild_cgraph_edges)
1949 cgraph_edge::rebuild_edges ();
1951 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1952 /* If we've seen errors do not bother running any verifiers. */
1953 if (flag_checking && !seen_error ())
1955 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1956 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1958 if (flags & TODO_verify_il)
1960 if (cfun->curr_properties & PROP_trees)
1962 if (cfun->curr_properties & PROP_cfg)
1963 /* IPA passes leave stmts to be fixed up, so make sure to
1964 not verify stmts really throw. */
1965 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1966 else
1967 verify_gimple_in_seq (gimple_body (cfun->decl));
1969 if (cfun->curr_properties & PROP_ssa)
1970 /* IPA passes leave stmts to be fixed up, so make sure to
1971 not verify SSA operands whose verifier will choke on that. */
1972 verify_ssa (true, !from_ipa_pass);
1973 /* IPA passes leave basic-blocks unsplit, so make sure to
1974 not trip on that. */
1975 if ((cfun->curr_properties & PROP_cfg)
1976 && !from_ipa_pass)
1977 verify_flow_info ();
1978 if (current_loops
1979 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1980 verify_loop_closed_ssa (false);
1981 if (cfun->curr_properties & PROP_rtl)
1982 verify_rtl_sharing ();
1985 /* Make sure verifiers don't change dominator state. */
1986 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1987 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1990 fn->last_verified = flags & TODO_verify_all;
1992 pop_cfun ();
1994 /* For IPA passes make sure to release dominator info, it can be
1995 computed by non-verifying TODOs. */
1996 if (from_ipa_pass)
1998 free_dominance_info (fn, CDI_DOMINATORS);
1999 free_dominance_info (fn, CDI_POST_DOMINATORS);
2003 /* Perform all TODO actions. */
2004 static void
2005 execute_todo (unsigned int flags)
2007 if (flag_checking
2008 && cfun
2009 && need_ssa_update_p (cfun))
2010 gcc_assert (flags & TODO_update_ssa_any);
2012 statistics_fini_pass ();
2014 if (flags)
2015 do_per_function (execute_function_todo, (void *)(size_t) flags);
2017 /* At this point we should not have any unreachable code in the
2018 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
2019 if (cfun && cfun->gimple_df)
2020 flush_ssaname_freelist ();
2022 /* Always remove functions just as before inlining: IPA passes might be
2023 interested to see bodies of extern inline functions that are not inlined
2024 to analyze side effects. The full removal is done just at the end
2025 of IPA pass queue. */
2026 if (flags & TODO_remove_functions)
2028 gcc_assert (!cfun);
2029 symtab->remove_unreachable_nodes (dump_file);
2032 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2034 gcc_assert (!cfun);
2035 symtab_node::dump_table (dump_file);
2036 /* Flush the file. If verification fails, we won't be able to
2037 close the file before aborting. */
2038 fflush (dump_file);
2041 /* Now that the dumping has been done, we can get rid of the optional
2042 df problems. */
2043 if (flags & TODO_df_finish)
2044 df_finish_pass ((flags & TODO_df_verify) != 0);
2047 /* Verify invariants that should hold between passes. This is a place
2048 to put simple sanity checks. */
2050 static void
2051 verify_interpass_invariants (void)
2053 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2056 /* Clear the last verified flag. */
2058 static void
2059 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2061 fn->last_verified = 0;
2064 /* Helper function. Verify that the properties has been turn into the
2065 properties expected by the pass. */
2067 static void
2068 verify_curr_properties (function *fn, void *data)
2070 unsigned int props = (size_t)data;
2071 gcc_assert ((fn->curr_properties & props) == props);
2074 /* Release dump file name if set. */
2076 static void
2077 release_dump_file_name (void)
2079 if (dump_file_name)
2081 free (CONST_CAST (char *, dump_file_name));
2082 dump_file_name = NULL;
2086 /* Initialize pass dump file. */
2087 /* This is non-static so that the plugins can use it. */
2089 bool
2090 pass_init_dump_file (opt_pass *pass)
2092 /* If a dump file name is present, open it if enabled. */
2093 if (pass->static_pass_number != -1)
2095 timevar_push (TV_DUMP);
2096 gcc::dump_manager *dumps = g->get_dumps ();
2097 bool initializing_dump =
2098 !dumps->dump_initialized_p (pass->static_pass_number);
2099 release_dump_file_name ();
2100 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2101 dumps->dump_start (pass->static_pass_number, &dump_flags);
2102 if (dump_file && current_function_decl && ! (dump_flags & TDF_GIMPLE))
2103 dump_function_header (dump_file, current_function_decl, dump_flags);
2104 if (initializing_dump
2105 && dump_file && (dump_flags & TDF_GRAPH)
2106 && cfun && (cfun->curr_properties & PROP_cfg))
2108 clean_graph_dump_file (dump_file_name);
2109 struct dump_file_info *dfi
2110 = dumps->get_dump_file_info (pass->static_pass_number);
2111 dfi->graph_dump_initialized = true;
2113 timevar_pop (TV_DUMP);
2114 return initializing_dump;
2116 else
2117 return false;
2120 /* Flush PASS dump file. */
2121 /* This is non-static so that plugins can use it. */
2123 void
2124 pass_fini_dump_file (opt_pass *pass)
2126 timevar_push (TV_DUMP);
2128 /* Flush and close dump file. */
2129 release_dump_file_name ();
2131 g->get_dumps ()->dump_finish (pass->static_pass_number);
2132 timevar_pop (TV_DUMP);
2135 /* After executing the pass, apply expected changes to the function
2136 properties. */
2138 static void
2139 update_properties_after_pass (function *fn, void *data)
2141 opt_pass *pass = (opt_pass *) data;
2142 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2143 & ~pass->properties_destroyed;
2146 /* Execute summary generation for all of the passes in IPA_PASS. */
2148 void
2149 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2151 while (ipa_pass)
2153 opt_pass *pass = ipa_pass;
2155 /* Execute all of the IPA_PASSes in the list. */
2156 if (ipa_pass->type == IPA_PASS
2157 && pass->gate (cfun)
2158 && ipa_pass->generate_summary)
2160 pass_init_dump_file (pass);
2162 /* If a timevar is present, start it. */
2163 if (pass->tv_id)
2164 timevar_push (pass->tv_id);
2166 current_pass = pass;
2167 ipa_pass->generate_summary ();
2169 /* Stop timevar. */
2170 if (pass->tv_id)
2171 timevar_pop (pass->tv_id);
2173 pass_fini_dump_file (pass);
2175 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2179 /* Execute IPA_PASS function transform on NODE. */
2181 static void
2182 execute_one_ipa_transform_pass (struct cgraph_node *node,
2183 ipa_opt_pass_d *ipa_pass)
2185 opt_pass *pass = ipa_pass;
2186 unsigned int todo_after = 0;
2188 current_pass = pass;
2189 if (!ipa_pass->function_transform)
2190 return;
2192 /* Note that the folders should only create gimple expressions.
2193 This is a hack until the new folder is ready. */
2194 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2196 pass_init_dump_file (pass);
2198 /* If a timevar is present, start it. */
2199 if (pass->tv_id != TV_NONE)
2200 timevar_push (pass->tv_id);
2202 /* Run pre-pass verification. */
2203 execute_todo (ipa_pass->function_transform_todo_flags_start);
2205 /* Do it! */
2206 todo_after = ipa_pass->function_transform (node);
2208 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2209 check_profile_consistency (pass->static_pass_number, 0, true);
2211 /* Run post-pass cleanup and verification. */
2212 execute_todo (todo_after);
2213 verify_interpass_invariants ();
2214 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2215 check_profile_consistency (pass->static_pass_number, 1, true);
2217 /* Stop timevar. */
2218 if (pass->tv_id != TV_NONE)
2219 timevar_pop (pass->tv_id);
2221 if (dump_file)
2222 do_per_function (execute_function_dump, pass);
2223 pass_fini_dump_file (pass);
2225 current_pass = NULL;
2226 redirect_edge_var_map_empty ();
2228 /* Signal this is a suitable GC collection point. */
2229 if (!(todo_after & TODO_do_not_ggc_collect))
2230 ggc_collect ();
2233 /* For the current function, execute all ipa transforms. */
2235 void
2236 execute_all_ipa_transforms (void)
2238 struct cgraph_node *node;
2239 if (!cfun)
2240 return;
2241 node = cgraph_node::get (current_function_decl);
2243 if (node->ipa_transforms_to_apply.exists ())
2245 unsigned int i;
2247 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2248 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2249 node->ipa_transforms_to_apply.release ();
2253 /* Check if PASS is explicitly disabled or enabled and return
2254 the gate status. FUNC is the function to be processed, and
2255 GATE_STATUS is the gate status determined by pass manager by
2256 default. */
2258 static bool
2259 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2261 bool explicitly_enabled = false;
2262 bool explicitly_disabled = false;
2264 explicitly_enabled
2265 = is_pass_explicitly_enabled_or_disabled (pass, func,
2266 enabled_pass_uid_range_tab);
2267 explicitly_disabled
2268 = is_pass_explicitly_enabled_or_disabled (pass, func,
2269 disabled_pass_uid_range_tab);
2271 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2273 return gate_status;
2277 /* Execute PASS. */
2279 bool
2280 execute_one_pass (opt_pass *pass)
2282 unsigned int todo_after = 0;
2284 bool gate_status;
2286 /* IPA passes are executed on whole program, so cfun should be NULL.
2287 Other passes need function context set. */
2288 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2289 gcc_assert (!cfun && !current_function_decl);
2290 else
2291 gcc_assert (cfun && current_function_decl);
2293 current_pass = pass;
2295 /* Check whether gate check should be avoided.
2296 User controls the value of the gate through the parameter "gate_status". */
2297 gate_status = pass->gate (cfun);
2298 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2300 /* Override gate with plugin. */
2301 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2303 if (!gate_status)
2305 /* Run so passes selectively disabling themselves on a given function
2306 are not miscounted. */
2307 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2309 check_profile_consistency (pass->static_pass_number, 0, false);
2310 check_profile_consistency (pass->static_pass_number, 1, false);
2312 current_pass = NULL;
2313 return false;
2316 /* For skipping passes until startwith pass */
2317 if (cfun
2318 && cfun->pass_startwith
2319 /* But we can't skip the lowering phase yet -- ideally we'd
2320 drive that phase fully via properties. */
2321 && (cfun->curr_properties & PROP_ssa))
2323 size_t namelen = strlen (pass->name);
2324 if (! strncmp (pass->name, cfun->pass_startwith, namelen))
2326 /* The following supports starting with the Nth invocation
2327 of a pass (where N does not necessarily is equal to the
2328 dump file suffix). */
2329 if (cfun->pass_startwith[namelen] == '\0'
2330 || (cfun->pass_startwith[namelen] == '1'
2331 && cfun->pass_startwith[namelen + 1] == '\0'))
2332 cfun->pass_startwith = NULL;
2333 else
2335 if (cfun->pass_startwith[namelen + 1] != '\0')
2336 return true;
2337 --cfun->pass_startwith[namelen];
2338 return true;
2341 else
2342 return true;
2345 /* Pass execution event trigger: useful to identify passes being
2346 executed. */
2347 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2349 if (!quiet_flag && !cfun)
2350 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2352 /* Note that the folders should only create gimple expressions.
2353 This is a hack until the new folder is ready. */
2354 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2356 pass_init_dump_file (pass);
2358 /* If a timevar is present, start it. */
2359 if (pass->tv_id != TV_NONE)
2360 timevar_push (pass->tv_id);
2362 /* Run pre-pass verification. */
2363 execute_todo (pass->todo_flags_start);
2365 if (flag_checking)
2366 do_per_function (verify_curr_properties,
2367 (void *)(size_t)pass->properties_required);
2369 /* Do it! */
2370 todo_after = pass->execute (cfun);
2372 if (todo_after & TODO_discard_function)
2374 /* Stop timevar. */
2375 if (pass->tv_id != TV_NONE)
2376 timevar_pop (pass->tv_id);
2378 pass_fini_dump_file (pass);
2380 gcc_assert (cfun);
2381 /* As cgraph_node::release_body expects release dominators info,
2382 we have to release it. */
2383 if (dom_info_available_p (CDI_DOMINATORS))
2384 free_dominance_info (CDI_DOMINATORS);
2386 if (dom_info_available_p (CDI_POST_DOMINATORS))
2387 free_dominance_info (CDI_POST_DOMINATORS);
2389 tree fn = cfun->decl;
2390 pop_cfun ();
2391 gcc_assert (!cfun);
2392 cgraph_node::get (fn)->release_body ();
2394 current_pass = NULL;
2395 redirect_edge_var_map_empty ();
2397 ggc_collect ();
2399 return true;
2402 do_per_function (clear_last_verified, NULL);
2404 do_per_function (update_properties_after_pass, pass);
2406 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2407 check_profile_consistency (pass->static_pass_number, 0, true);
2409 /* Run post-pass cleanup and verification. */
2410 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2411 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2412 check_profile_consistency (pass->static_pass_number, 1, true);
2414 verify_interpass_invariants ();
2416 /* Stop timevar. */
2417 if (pass->tv_id != TV_NONE)
2418 timevar_pop (pass->tv_id);
2420 if (pass->type == IPA_PASS
2421 && ((ipa_opt_pass_d *)pass)->function_transform)
2423 struct cgraph_node *node;
2424 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2425 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2427 else if (dump_file)
2428 do_per_function (execute_function_dump, pass);
2430 if (!current_function_decl)
2431 symtab->process_new_functions ();
2433 pass_fini_dump_file (pass);
2435 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2436 gcc_assert (!(cfun->curr_properties & PROP_trees)
2437 || pass->type != RTL_PASS);
2439 current_pass = NULL;
2440 redirect_edge_var_map_empty ();
2442 /* Signal this is a suitable GC collection point. */
2443 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2444 ggc_collect ();
2446 return true;
2449 static void
2450 execute_pass_list_1 (opt_pass *pass)
2454 gcc_assert (pass->type == GIMPLE_PASS
2455 || pass->type == RTL_PASS);
2457 if (cfun == NULL)
2458 return;
2459 if (execute_one_pass (pass) && pass->sub)
2460 execute_pass_list_1 (pass->sub);
2461 pass = pass->next;
2463 while (pass);
2466 void
2467 execute_pass_list (function *fn, opt_pass *pass)
2469 gcc_assert (fn == cfun);
2470 execute_pass_list_1 (pass);
2471 if (cfun && fn->cfg)
2473 free_dominance_info (CDI_DOMINATORS);
2474 free_dominance_info (CDI_POST_DOMINATORS);
2478 /* Write out all LTO data. */
2479 static void
2480 write_lto (void)
2482 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2483 lto_output ();
2484 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2485 timevar_push (TV_IPA_LTO_DECL_OUT);
2486 produce_asm_for_decls ();
2487 timevar_pop (TV_IPA_LTO_DECL_OUT);
2490 /* Same as execute_pass_list but assume that subpasses of IPA passes
2491 are local passes. If SET is not NULL, write out summaries of only
2492 those node in SET. */
2494 static void
2495 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2497 while (pass)
2499 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2500 gcc_assert (!current_function_decl);
2501 gcc_assert (!cfun);
2502 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2503 if (pass->type == IPA_PASS
2504 && ipa_pass->write_summary
2505 && pass->gate (cfun))
2507 /* If a timevar is present, start it. */
2508 if (pass->tv_id)
2509 timevar_push (pass->tv_id);
2511 pass_init_dump_file (pass);
2513 current_pass = pass;
2514 ipa_pass->write_summary ();
2516 pass_fini_dump_file (pass);
2518 /* If a timevar is present, start it. */
2519 if (pass->tv_id)
2520 timevar_pop (pass->tv_id);
2523 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2524 ipa_write_summaries_2 (pass->sub, state);
2526 pass = pass->next;
2530 /* Helper function of ipa_write_summaries. Creates and destroys the
2531 decl state and calls ipa_write_summaries_2 for all passes that have
2532 summaries. SET is the set of nodes to be written. */
2534 static void
2535 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2537 pass_manager *passes = g->get_passes ();
2538 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2539 state->symtab_node_encoder = encoder;
2541 lto_output_init_mode_table ();
2542 lto_push_out_decl_state (state);
2544 gcc_assert (!flag_wpa);
2545 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2547 write_lto ();
2549 gcc_assert (lto_get_out_decl_state () == state);
2550 lto_pop_out_decl_state ();
2551 lto_delete_out_decl_state (state);
2554 /* Write out summaries for all the nodes in the callgraph. */
2556 void
2557 ipa_write_summaries (void)
2559 lto_symtab_encoder_t encoder;
2560 int i, order_pos;
2561 varpool_node *vnode;
2562 struct cgraph_node *node;
2563 struct cgraph_node **order;
2565 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2566 return;
2568 select_what_to_stream ();
2570 encoder = lto_symtab_encoder_new (false);
2572 /* Create the callgraph set in the same order used in
2573 cgraph_expand_all_functions. This mostly facilitates debugging,
2574 since it causes the gimple file to be processed in the same order
2575 as the source code. */
2576 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2577 order_pos = ipa_reverse_postorder (order);
2578 gcc_assert (order_pos == symtab->cgraph_count);
2580 for (i = order_pos - 1; i >= 0; i--)
2582 struct cgraph_node *node = order[i];
2584 if (node->has_gimple_body_p ())
2586 /* When streaming out references to statements as part of some IPA
2587 pass summary, the statements need to have uids assigned and the
2588 following does that for all the IPA passes here. Naturally, this
2589 ordering then matches the one IPA-passes get in their stmt_fixup
2590 hooks. */
2592 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2593 renumber_gimple_stmt_uids ();
2594 pop_cfun ();
2596 if (node->definition && node->need_lto_streaming)
2597 lto_set_symtab_encoder_in_partition (encoder, node);
2600 FOR_EACH_DEFINED_FUNCTION (node)
2601 if (node->alias && node->need_lto_streaming)
2602 lto_set_symtab_encoder_in_partition (encoder, node);
2603 FOR_EACH_DEFINED_VARIABLE (vnode)
2604 if (vnode->need_lto_streaming)
2605 lto_set_symtab_encoder_in_partition (encoder, vnode);
2607 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2609 free (order);
2612 /* Same as execute_pass_list but assume that subpasses of IPA passes
2613 are local passes. If SET is not NULL, write out optimization summaries of
2614 only those node in SET. */
2616 static void
2617 ipa_write_optimization_summaries_1 (opt_pass *pass,
2618 struct lto_out_decl_state *state)
2620 while (pass)
2622 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2623 gcc_assert (!current_function_decl);
2624 gcc_assert (!cfun);
2625 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2626 if (pass->type == IPA_PASS
2627 && ipa_pass->write_optimization_summary
2628 && pass->gate (cfun))
2630 /* If a timevar is present, start it. */
2631 if (pass->tv_id)
2632 timevar_push (pass->tv_id);
2634 pass_init_dump_file (pass);
2636 current_pass = pass;
2637 ipa_pass->write_optimization_summary ();
2639 pass_fini_dump_file (pass);
2641 /* If a timevar is present, start it. */
2642 if (pass->tv_id)
2643 timevar_pop (pass->tv_id);
2646 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2647 ipa_write_optimization_summaries_1 (pass->sub, state);
2649 pass = pass->next;
2653 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2654 NULL, write out all summaries of all nodes. */
2656 void
2657 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2659 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2660 lto_symtab_encoder_iterator lsei;
2661 state->symtab_node_encoder = encoder;
2663 lto_output_init_mode_table ();
2664 lto_push_out_decl_state (state);
2665 for (lsei = lsei_start_function_in_partition (encoder);
2666 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2668 struct cgraph_node *node = lsei_cgraph_node (lsei);
2669 /* When streaming out references to statements as part of some IPA
2670 pass summary, the statements need to have uids assigned.
2672 For functions newly born at WPA stage we need to initialize
2673 the uids here. */
2674 if (node->definition
2675 && gimple_has_body_p (node->decl))
2677 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2678 renumber_gimple_stmt_uids ();
2679 pop_cfun ();
2683 gcc_assert (flag_wpa);
2684 pass_manager *passes = g->get_passes ();
2685 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2687 write_lto ();
2689 gcc_assert (lto_get_out_decl_state () == state);
2690 lto_pop_out_decl_state ();
2691 lto_delete_out_decl_state (state);
2694 /* Same as execute_pass_list but assume that subpasses of IPA passes
2695 are local passes. */
2697 static void
2698 ipa_read_summaries_1 (opt_pass *pass)
2700 while (pass)
2702 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2704 gcc_assert (!current_function_decl);
2705 gcc_assert (!cfun);
2706 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2708 if (pass->gate (cfun))
2710 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2712 /* If a timevar is present, start it. */
2713 if (pass->tv_id)
2714 timevar_push (pass->tv_id);
2716 pass_init_dump_file (pass);
2718 current_pass = pass;
2719 ipa_pass->read_summary ();
2721 pass_fini_dump_file (pass);
2723 /* Stop timevar. */
2724 if (pass->tv_id)
2725 timevar_pop (pass->tv_id);
2728 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2729 ipa_read_summaries_1 (pass->sub);
2731 pass = pass->next;
2736 /* Read all the summaries for all_regular_ipa_passes. */
2738 void
2739 ipa_read_summaries (void)
2741 pass_manager *passes = g->get_passes ();
2742 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2745 /* Same as execute_pass_list but assume that subpasses of IPA passes
2746 are local passes. */
2748 static void
2749 ipa_read_optimization_summaries_1 (opt_pass *pass)
2751 while (pass)
2753 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2755 gcc_assert (!current_function_decl);
2756 gcc_assert (!cfun);
2757 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2759 if (pass->gate (cfun))
2761 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2763 /* If a timevar is present, start it. */
2764 if (pass->tv_id)
2765 timevar_push (pass->tv_id);
2767 pass_init_dump_file (pass);
2769 current_pass = pass;
2770 ipa_pass->read_optimization_summary ();
2772 pass_fini_dump_file (pass);
2774 /* Stop timevar. */
2775 if (pass->tv_id)
2776 timevar_pop (pass->tv_id);
2779 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2780 ipa_read_optimization_summaries_1 (pass->sub);
2782 pass = pass->next;
2786 /* Read all the summaries for all_regular_ipa_passes. */
2788 void
2789 ipa_read_optimization_summaries (void)
2791 pass_manager *passes = g->get_passes ();
2792 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2795 /* Same as execute_pass_list but assume that subpasses of IPA passes
2796 are local passes. */
2797 void
2798 execute_ipa_pass_list (opt_pass *pass)
2802 gcc_assert (!current_function_decl);
2803 gcc_assert (!cfun);
2804 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2805 if (execute_one_pass (pass) && pass->sub)
2807 if (pass->sub->type == GIMPLE_PASS)
2809 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2810 do_per_function_toporder ((void (*)(function *, void *))
2811 execute_pass_list,
2812 pass->sub);
2813 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2815 else if (pass->sub->type == SIMPLE_IPA_PASS
2816 || pass->sub->type == IPA_PASS)
2817 execute_ipa_pass_list (pass->sub);
2818 else
2819 gcc_unreachable ();
2821 gcc_assert (!current_function_decl);
2822 symtab->process_new_functions ();
2823 pass = pass->next;
2825 while (pass);
2828 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2830 static void
2831 execute_ipa_stmt_fixups (opt_pass *pass,
2832 struct cgraph_node *node, gimple **stmts)
2834 while (pass)
2836 /* Execute all of the IPA_PASSes in the list. */
2837 if (pass->type == IPA_PASS
2838 && pass->gate (cfun))
2840 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2842 if (ipa_pass->stmt_fixup)
2844 pass_init_dump_file (pass);
2845 /* If a timevar is present, start it. */
2846 if (pass->tv_id)
2847 timevar_push (pass->tv_id);
2849 current_pass = pass;
2850 ipa_pass->stmt_fixup (node, stmts);
2852 /* Stop timevar. */
2853 if (pass->tv_id)
2854 timevar_pop (pass->tv_id);
2855 pass_fini_dump_file (pass);
2857 if (pass->sub)
2858 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2860 pass = pass->next;
2864 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2866 void
2867 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2869 pass_manager *passes = g->get_passes ();
2870 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2874 extern void debug_properties (unsigned int);
2875 extern void dump_properties (FILE *, unsigned int);
2877 DEBUG_FUNCTION void
2878 dump_properties (FILE *dump, unsigned int props)
2880 fprintf (dump, "Properties:\n");
2881 if (props & PROP_gimple_any)
2882 fprintf (dump, "PROP_gimple_any\n");
2883 if (props & PROP_gimple_lcf)
2884 fprintf (dump, "PROP_gimple_lcf\n");
2885 if (props & PROP_gimple_leh)
2886 fprintf (dump, "PROP_gimple_leh\n");
2887 if (props & PROP_cfg)
2888 fprintf (dump, "PROP_cfg\n");
2889 if (props & PROP_ssa)
2890 fprintf (dump, "PROP_ssa\n");
2891 if (props & PROP_no_crit_edges)
2892 fprintf (dump, "PROP_no_crit_edges\n");
2893 if (props & PROP_rtl)
2894 fprintf (dump, "PROP_rtl\n");
2895 if (props & PROP_gimple_lomp)
2896 fprintf (dump, "PROP_gimple_lomp\n");
2897 if (props & PROP_gimple_lcx)
2898 fprintf (dump, "PROP_gimple_lcx\n");
2899 if (props & PROP_gimple_lvec)
2900 fprintf (dump, "PROP_gimple_lvec\n");
2901 if (props & PROP_cfglayout)
2902 fprintf (dump, "PROP_cfglayout\n");
2905 DEBUG_FUNCTION void
2906 debug_properties (unsigned int props)
2908 dump_properties (stderr, props);
2911 /* Called by local passes to see if function is called by already processed nodes.
2912 Because we process nodes in topological order, this means that function is
2913 in recursive cycle or we introduced new direct calls. */
2914 bool
2915 function_called_by_processed_nodes_p (void)
2917 struct cgraph_edge *e;
2918 for (e = cgraph_node::get (current_function_decl)->callers;
2920 e = e->next_caller)
2922 if (e->caller->decl == current_function_decl)
2923 continue;
2924 if (!e->caller->has_gimple_body_p ())
2925 continue;
2926 if (TREE_ASM_WRITTEN (e->caller->decl))
2927 continue;
2928 if (!e->caller->process && !e->caller->global.inlined_to)
2929 break;
2931 if (dump_file && e)
2933 fprintf (dump_file, "Already processed call to:\n");
2934 e->caller->dump (dump_file);
2936 return e != NULL;
2939 #include "gt-passes.h"