i386: Rewrite check for AVX512 features
[official-gcc.git] / gcc / passes.c
blobf5791ac806afb8a251549a2c2e27dca1d2295686
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2017 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 */
65 using namespace gcc;
67 /* This is used for debugging. It allows the current pass to printed
68 from anywhere in compilation.
69 The variable current_pass is also used for statistics and plugins. */
70 opt_pass *current_pass;
72 /* Most passes are single-instance (within their context) and thus don't
73 need to implement cloning, but passes that support multiple instances
74 *must* provide their own implementation of the clone method.
76 Handle this by providing a default implemenation, but make it a fatal
77 error to call it. */
79 opt_pass *
80 opt_pass::clone ()
82 internal_error ("pass %s does not support cloning", name);
85 void
86 opt_pass::set_pass_param (unsigned int, bool)
88 internal_error ("pass %s needs a set_pass_param implementation to handle the"
89 " extra argument in NEXT_PASS", name);
92 bool
93 opt_pass::gate (function *)
95 return true;
98 unsigned int
99 opt_pass::execute (function *)
101 return 0;
104 opt_pass::opt_pass (const pass_data &data, context *ctxt)
105 : pass_data (data),
106 sub (NULL),
107 next (NULL),
108 static_pass_number (0),
109 m_ctxt (ctxt)
114 void
115 pass_manager::execute_early_local_passes ()
117 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
118 if (flag_check_pointer_bounds)
119 execute_pass_list (cfun, pass_chkp_instrumentation_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 (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
199 make_decl_rtl (decl);
201 /* Forward declarations for nested functions are not "external",
202 but we need to treat them as if they were. */
203 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
204 || TREE_CODE (decl) == FUNCTION_DECL)
206 timevar_push (TV_VARCONST);
208 /* Don't output anything when a tentative file-scope definition
209 is seen. But at end of compilation, do output code for them.
211 We do output all variables and rely on
212 callgraph code to defer them except for forward declarations
213 (see gcc.c-torture/compile/920624-1.c) */
214 if ((at_end
215 || !DECL_DEFER_OUTPUT (decl)
216 || DECL_INITIAL (decl))
217 && (!VAR_P (decl) || !DECL_HAS_VALUE_EXPR_P (decl))
218 && !DECL_EXTERNAL (decl))
220 /* When reading LTO unit, we also read varpool, so do not
221 rebuild it. */
222 if (in_lto_p && !at_end)
224 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
225 varpool_node::finalize_decl (decl);
228 #ifdef ASM_FINISH_DECLARE_OBJECT
229 if (decl == last_assemble_variable_decl)
231 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
232 top_level, at_end);
234 #endif
236 /* Now that we have activated any function-specific attributes
237 that might affect function decl, particularly align, relayout it. */
238 if (TREE_CODE (decl) == FUNCTION_DECL)
239 targetm.target_option.relayout_function (decl);
241 timevar_pop (TV_VARCONST);
243 else if (TREE_CODE (decl) == TYPE_DECL
244 /* Like in rest_of_type_compilation, avoid confusing the debug
245 information machinery when there are errors. */
246 && !seen_error ())
248 timevar_push (TV_SYMOUT);
249 debug_hooks->type_decl (decl, !top_level);
250 timevar_pop (TV_SYMOUT);
253 /* Let cgraph know about the existence of variables. */
254 if (in_lto_p && !at_end)
256 else if (VAR_P (decl) && !DECL_EXTERNAL (decl)
257 && TREE_STATIC (decl))
258 varpool_node::get_create (decl);
260 /* Generate early debug for global variables. Any local variables will
261 be handled by either handling reachable functions from
262 finalize_compilation_unit (and by consequence, locally scoped
263 symbols), or by rest_of_type_compilation below.
265 For Go's hijack of the debug_hooks to implement -fdump-go-spec, pick up
266 function prototypes. Go's debug_hooks will not forward them to the
267 wrapped hooks. */
268 if (!in_lto_p
269 && (TREE_CODE (decl) != FUNCTION_DECL
270 /* This will pick up function prototypes with no bodies,
271 which are not visible in finalize_compilation_unit()
272 while iterating with FOR_EACH_*_FUNCTION through the
273 symbol table. */
274 || (flag_dump_go_spec != NULL
275 && !DECL_SAVED_TREE (decl)
276 && DECL_STRUCT_FUNCTION (decl) == NULL))
278 /* We need to check both decl_function_context and
279 current_function_decl here to make sure local extern
280 declarations end up with the correct context.
282 For local extern declarations, decl_function_context is
283 empty, but current_function_decl is set to the function where
284 the extern was declared . Without the check for
285 !current_function_decl below, the local extern ends up
286 incorrectly with a top-level context.
288 For example:
290 namespace S
296 int i = 42;
298 extern int i; // Local extern declaration.
299 return i;
305 && !decl_function_context (decl)
306 && !current_function_decl
307 && DECL_SOURCE_LOCATION (decl) != BUILTINS_LOCATION
308 && (!decl_type_context (decl)
309 /* If we created a varpool node for the decl make sure to
310 call early_global_decl. Otherwise we miss changes
311 introduced by member definitions like
312 struct A { static int staticdatamember; };
313 int A::staticdatamember;
314 and thus have incomplete early debug and late debug
315 called from varpool node removal fails to handle it
316 properly. */
317 || (finalize
318 && VAR_P (decl)
319 && TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
320 /* Avoid confusing the debug information machinery when there are
321 errors. */
322 && !seen_error ())
323 (*debug_hooks->early_global_decl) (decl);
326 /* Called after finishing a record, union or enumeral type. */
328 void
329 rest_of_type_compilation (tree type, int toplev)
331 /* Avoid confusing the debug information machinery when there are
332 errors. */
333 if (seen_error ())
334 return;
336 timevar_push (TV_SYMOUT);
337 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
338 timevar_pop (TV_SYMOUT);
343 void
344 pass_manager::
345 finish_optimization_passes (void)
347 int i;
348 struct dump_file_info *dfi;
349 char *name;
350 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
352 timevar_push (TV_DUMP);
353 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
355 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
356 end_branch_prob ();
357 dumps->dump_finish (pass_profile_1->static_pass_number);
360 if (optimize > 0)
362 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
363 print_combine_total_stats ();
364 dumps->dump_finish (pass_profile_1->static_pass_number);
367 /* Do whatever is necessary to finish printing the graphs. */
368 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
369 if (dfi->graph_dump_initialized)
371 name = dumps->get_dump_file_name (dfi);
372 finish_graph_dump_file (name);
373 free (name);
376 timevar_pop (TV_DUMP);
379 static unsigned int
380 execute_build_ssa_passes (void)
382 /* Once this pass (and its sub-passes) are complete, all functions
383 will be in SSA form. Technically this state change is happening
384 a tad early, since the sub-passes have not yet run, but since
385 none of the sub-passes are IPA passes and do not create new
386 functions, this is ok. We're setting this value for the benefit
387 of IPA passes that follow. */
388 if (symtab->state < IPA_SSA)
389 symtab->state = IPA_SSA;
390 return 0;
393 namespace {
395 const pass_data pass_data_build_ssa_passes =
397 SIMPLE_IPA_PASS, /* type */
398 "build_ssa_passes", /* name */
399 OPTGROUP_NONE, /* optinfo_flags */
400 TV_EARLY_LOCAL, /* tv_id */
401 0, /* properties_required */
402 0, /* properties_provided */
403 0, /* properties_destroyed */
404 0, /* todo_flags_start */
405 /* todo_flags_finish is executed before subpases. For this reason
406 it makes no sense to remove unreachable functions here. */
407 0, /* todo_flags_finish */
410 class pass_build_ssa_passes : public simple_ipa_opt_pass
412 public:
413 pass_build_ssa_passes (gcc::context *ctxt)
414 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
417 /* opt_pass methods: */
418 virtual bool gate (function *)
420 /* Don't bother doing anything if the program has errors. */
421 return (!seen_error () && !in_lto_p);
424 virtual unsigned int execute (function *)
426 return execute_build_ssa_passes ();
429 }; // class pass_build_ssa_passes
431 const pass_data pass_data_chkp_instrumentation_passes =
433 SIMPLE_IPA_PASS, /* type */
434 "chkp_passes", /* name */
435 OPTGROUP_NONE, /* optinfo_flags */
436 TV_NONE, /* tv_id */
437 0, /* properties_required */
438 0, /* properties_provided */
439 0, /* properties_destroyed */
440 0, /* todo_flags_start */
441 0, /* todo_flags_finish */
444 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
446 public:
447 pass_chkp_instrumentation_passes (gcc::context *ctxt)
448 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
451 /* opt_pass methods: */
452 virtual bool gate (function *)
454 /* Don't bother doing anything if the program has errors. */
455 return (flag_check_pointer_bounds
456 && !seen_error () && !in_lto_p);
459 }; // class pass_chkp_instrumentation_passes
461 const pass_data pass_data_local_optimization_passes =
463 SIMPLE_IPA_PASS, /* type */
464 "opt_local_passes", /* name */
465 OPTGROUP_NONE, /* optinfo_flags */
466 TV_NONE, /* tv_id */
467 0, /* properties_required */
468 0, /* properties_provided */
469 0, /* properties_destroyed */
470 0, /* todo_flags_start */
471 0, /* todo_flags_finish */
474 class pass_local_optimization_passes : public simple_ipa_opt_pass
476 public:
477 pass_local_optimization_passes (gcc::context *ctxt)
478 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
481 /* opt_pass methods: */
482 virtual bool gate (function *)
484 /* Don't bother doing anything if the program has errors. */
485 return (!seen_error () && !in_lto_p);
488 }; // class pass_local_optimization_passes
490 } // anon namespace
492 simple_ipa_opt_pass *
493 make_pass_build_ssa_passes (gcc::context *ctxt)
495 return new pass_build_ssa_passes (ctxt);
498 simple_ipa_opt_pass *
499 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
501 return new pass_chkp_instrumentation_passes (ctxt);
504 simple_ipa_opt_pass *
505 make_pass_local_optimization_passes (gcc::context *ctxt)
507 return new pass_local_optimization_passes (ctxt);
510 namespace {
512 const pass_data pass_data_all_early_optimizations =
514 GIMPLE_PASS, /* type */
515 "early_optimizations", /* name */
516 OPTGROUP_NONE, /* optinfo_flags */
517 TV_NONE, /* tv_id */
518 0, /* properties_required */
519 0, /* properties_provided */
520 0, /* properties_destroyed */
521 0, /* todo_flags_start */
522 0, /* todo_flags_finish */
525 class pass_all_early_optimizations : public gimple_opt_pass
527 public:
528 pass_all_early_optimizations (gcc::context *ctxt)
529 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
532 /* opt_pass methods: */
533 virtual bool gate (function *)
535 return (optimize >= 1
536 /* Don't bother doing anything if the program has errors. */
537 && !seen_error ());
540 }; // class pass_all_early_optimizations
542 } // anon namespace
544 static gimple_opt_pass *
545 make_pass_all_early_optimizations (gcc::context *ctxt)
547 return new pass_all_early_optimizations (ctxt);
550 namespace {
552 const pass_data pass_data_all_optimizations =
554 GIMPLE_PASS, /* type */
555 "*all_optimizations", /* name */
556 OPTGROUP_NONE, /* optinfo_flags */
557 TV_OPTIMIZE, /* tv_id */
558 0, /* properties_required */
559 0, /* properties_provided */
560 0, /* properties_destroyed */
561 0, /* todo_flags_start */
562 0, /* todo_flags_finish */
565 class pass_all_optimizations : public gimple_opt_pass
567 public:
568 pass_all_optimizations (gcc::context *ctxt)
569 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
572 /* opt_pass methods: */
573 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
575 }; // class pass_all_optimizations
577 } // anon namespace
579 static gimple_opt_pass *
580 make_pass_all_optimizations (gcc::context *ctxt)
582 return new pass_all_optimizations (ctxt);
585 namespace {
587 const pass_data pass_data_all_optimizations_g =
589 GIMPLE_PASS, /* type */
590 "*all_optimizations_g", /* name */
591 OPTGROUP_NONE, /* optinfo_flags */
592 TV_OPTIMIZE, /* tv_id */
593 0, /* properties_required */
594 0, /* properties_provided */
595 0, /* properties_destroyed */
596 0, /* todo_flags_start */
597 0, /* todo_flags_finish */
600 class pass_all_optimizations_g : public gimple_opt_pass
602 public:
603 pass_all_optimizations_g (gcc::context *ctxt)
604 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
607 /* opt_pass methods: */
608 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
610 }; // class pass_all_optimizations_g
612 } // anon namespace
614 static gimple_opt_pass *
615 make_pass_all_optimizations_g (gcc::context *ctxt)
617 return new pass_all_optimizations_g (ctxt);
620 namespace {
622 const pass_data pass_data_rest_of_compilation =
624 RTL_PASS, /* type */
625 "*rest_of_compilation", /* name */
626 OPTGROUP_NONE, /* optinfo_flags */
627 TV_REST_OF_COMPILATION, /* tv_id */
628 PROP_rtl, /* properties_required */
629 0, /* properties_provided */
630 0, /* properties_destroyed */
631 0, /* todo_flags_start */
632 0, /* todo_flags_finish */
635 class pass_rest_of_compilation : public rtl_opt_pass
637 public:
638 pass_rest_of_compilation (gcc::context *ctxt)
639 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
642 /* opt_pass methods: */
643 virtual bool gate (function *)
645 /* Early return if there were errors. We can run afoul of our
646 consistency checks, and there's not really much point in fixing them. */
647 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
650 }; // class pass_rest_of_compilation
652 } // anon namespace
654 static rtl_opt_pass *
655 make_pass_rest_of_compilation (gcc::context *ctxt)
657 return new pass_rest_of_compilation (ctxt);
660 namespace {
662 const pass_data pass_data_postreload =
664 RTL_PASS, /* type */
665 "*all-postreload", /* name */
666 OPTGROUP_NONE, /* optinfo_flags */
667 TV_POSTRELOAD, /* tv_id */
668 PROP_rtl, /* properties_required */
669 0, /* properties_provided */
670 0, /* properties_destroyed */
671 0, /* todo_flags_start */
672 0, /* todo_flags_finish */
675 class pass_postreload : public rtl_opt_pass
677 public:
678 pass_postreload (gcc::context *ctxt)
679 : rtl_opt_pass (pass_data_postreload, ctxt)
682 /* opt_pass methods: */
683 virtual bool gate (function *) { return reload_completed; }
685 }; // class pass_postreload
687 } // anon namespace
689 static rtl_opt_pass *
690 make_pass_postreload (gcc::context *ctxt)
692 return new pass_postreload (ctxt);
695 namespace {
697 const pass_data pass_data_late_compilation =
699 RTL_PASS, /* type */
700 "*all-late_compilation", /* name */
701 OPTGROUP_NONE, /* optinfo_flags */
702 TV_LATE_COMPILATION, /* tv_id */
703 PROP_rtl, /* properties_required */
704 0, /* properties_provided */
705 0, /* properties_destroyed */
706 0, /* todo_flags_start */
707 0, /* todo_flags_finish */
710 class pass_late_compilation : public rtl_opt_pass
712 public:
713 pass_late_compilation (gcc::context *ctxt)
714 : rtl_opt_pass (pass_data_late_compilation, ctxt)
717 /* opt_pass methods: */
718 virtual bool gate (function *)
720 return reload_completed || targetm.no_register_allocation;
723 }; // class pass_late_compilation
725 } // anon namespace
727 static rtl_opt_pass *
728 make_pass_late_compilation (gcc::context *ctxt)
730 return new pass_late_compilation (ctxt);
735 /* Set the static pass number of pass PASS to ID and record that
736 in the mapping from static pass number to pass. */
738 void
739 pass_manager::
740 set_pass_for_id (int id, opt_pass *pass)
742 pass->static_pass_number = id;
743 if (passes_by_id_size <= id)
745 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
746 memset (passes_by_id + passes_by_id_size, 0,
747 (id + 1 - passes_by_id_size) * sizeof (void *));
748 passes_by_id_size = id + 1;
750 passes_by_id[id] = pass;
753 /* Return the pass with the static pass number ID. */
755 opt_pass *
756 pass_manager::get_pass_for_id (int id) const
758 if (id >= passes_by_id_size)
759 return NULL;
760 return passes_by_id[id];
763 /* Iterate over the pass tree allocating dump file numbers. We want
764 to do this depth first, and independent of whether the pass is
765 enabled or not. */
767 void
768 register_one_dump_file (opt_pass *pass)
770 g->get_passes ()->register_one_dump_file (pass);
773 void
774 pass_manager::register_one_dump_file (opt_pass *pass)
776 char *dot_name, *flag_name, *glob_name;
777 const char *name, *full_name, *prefix;
779 /* Buffer big enough to format a 32-bit UINT_MAX into. */
780 char num[11];
781 dump_kind dkind;
782 int id;
783 int optgroup_flags = OPTGROUP_NONE;
784 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
786 /* See below in next_pass_1. */
787 num[0] = '\0';
788 if (pass->static_pass_number != -1)
789 sprintf (num, "%u", ((int) pass->static_pass_number < 0
790 ? 1 : pass->static_pass_number));
792 /* The name is both used to identify the pass for the purposes of plugins,
793 and to specify dump file name and option.
794 The latter two might want something short which is not quite unique; for
795 that reason, we may have a disambiguating prefix, followed by a space
796 to mark the start of the following dump file name / option string. */
797 name = strchr (pass->name, ' ');
798 name = name ? name + 1 : pass->name;
799 dot_name = concat (".", name, num, NULL);
800 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
802 prefix = "ipa-";
803 dkind = DK_ipa;
804 optgroup_flags |= OPTGROUP_IPA;
806 else if (pass->type == GIMPLE_PASS)
808 prefix = "tree-";
809 dkind = DK_tree;
811 else
813 prefix = "rtl-";
814 dkind = DK_rtl;
817 flag_name = concat (prefix, name, num, NULL);
818 glob_name = concat (prefix, name, NULL);
819 optgroup_flags |= pass->optinfo_flags;
820 /* For any passes that do not have an optgroup set, and which are not
821 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
822 any dump messages are emitted properly under -fopt-info(-optall). */
823 if (optgroup_flags == OPTGROUP_NONE)
824 optgroup_flags = OPTGROUP_OTHER;
825 id = dumps->dump_register (dot_name, flag_name, glob_name, dkind,
826 optgroup_flags,
827 true);
828 set_pass_for_id (id, pass);
829 full_name = concat (prefix, pass->name, num, NULL);
830 register_pass_name (pass, full_name);
831 free (CONST_CAST (char *, full_name));
834 /* Register the dump files for the pass_manager starting at PASS. */
836 void
837 pass_manager::register_dump_files (opt_pass *pass)
841 if (pass->name && pass->name[0] != '*')
842 register_one_dump_file (pass);
844 if (pass->sub)
845 register_dump_files (pass->sub);
847 pass = pass->next;
849 while (pass);
852 /* Register PASS with NAME. */
854 void
855 pass_manager::register_pass_name (opt_pass *pass, const char *name)
857 if (!m_name_to_pass_map)
858 m_name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
860 if (m_name_to_pass_map->get (name))
861 return; /* Ignore plugin passes. */
863 const char *unique_name = xstrdup (name);
864 m_name_to_pass_map->put (unique_name, pass);
867 /* Map from pass id to canonicalized pass name. */
869 typedef const char *char_ptr;
870 static vec<char_ptr> pass_tab;
872 /* Callback function for traversing NAME_TO_PASS_MAP. */
874 bool
875 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
877 gcc_assert (pass->static_pass_number > 0);
878 gcc_assert (pass_tab.exists ());
880 pass_tab[pass->static_pass_number] = name;
882 return 1;
885 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
886 table for dumping purpose. */
888 void
889 pass_manager::create_pass_tab (void) const
891 if (!flag_dump_passes)
892 return;
894 pass_tab.safe_grow_cleared (passes_by_id_size + 1);
895 m_name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
898 static bool override_gate_status (opt_pass *, tree, bool);
900 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
901 is turned on or not. */
903 static void
904 dump_one_pass (opt_pass *pass, int pass_indent)
906 int indent = 3 * pass_indent;
907 const char *pn;
908 bool is_on, is_really_on;
910 is_on = pass->gate (cfun);
911 is_really_on = override_gate_status (pass, current_function_decl, is_on);
913 if (pass->static_pass_number <= 0)
914 pn = pass->name;
915 else
916 pn = pass_tab[pass->static_pass_number];
918 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
919 (15 - indent < 0 ? 0 : 15 - indent), " ",
920 is_on ? " ON" : " OFF",
921 ((!is_on) == (!is_really_on) ? ""
922 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
925 /* Dump pass list PASS with indentation INDENT. */
927 static void
928 dump_pass_list (opt_pass *pass, int indent)
932 dump_one_pass (pass, indent);
933 if (pass->sub)
934 dump_pass_list (pass->sub, indent + 1);
935 pass = pass->next;
937 while (pass);
940 /* Dump all optimization passes. */
942 void
943 dump_passes (void)
945 g->get_passes ()->dump_passes ();
948 void
949 pass_manager::dump_passes () const
951 push_dummy_function (true);
953 create_pass_tab ();
955 dump_pass_list (all_lowering_passes, 1);
956 dump_pass_list (all_small_ipa_passes, 1);
957 dump_pass_list (all_regular_ipa_passes, 1);
958 dump_pass_list (all_late_ipa_passes, 1);
959 dump_pass_list (all_passes, 1);
961 pop_dummy_function ();
964 /* Returns the pass with NAME. */
966 opt_pass *
967 pass_manager::get_pass_by_name (const char *name)
969 opt_pass **p = m_name_to_pass_map->get (name);
970 if (p)
971 return *p;
973 return NULL;
977 /* Range [start, last]. */
979 struct uid_range
981 unsigned int start;
982 unsigned int last;
983 const char *assem_name;
984 struct uid_range *next;
987 typedef struct uid_range *uid_range_p;
990 static vec<uid_range_p> enabled_pass_uid_range_tab;
991 static vec<uid_range_p> disabled_pass_uid_range_tab;
994 /* Parse option string for -fdisable- and -fenable-
995 The syntax of the options:
997 -fenable-<pass_name>
998 -fdisable-<pass_name>
1000 -fenable-<pass_name>=s1:e1,s2:e2,...
1001 -fdisable-<pass_name>=s1:e1,s2:e2,...
1004 static void
1005 enable_disable_pass (const char *arg, bool is_enable)
1007 opt_pass *pass;
1008 char *range_str, *phase_name;
1009 char *argstr = xstrdup (arg);
1010 vec<uid_range_p> *tab = 0;
1012 range_str = strchr (argstr,'=');
1013 if (range_str)
1015 *range_str = '\0';
1016 range_str++;
1019 phase_name = argstr;
1020 if (!*phase_name)
1022 if (is_enable)
1023 error ("unrecognized option -fenable");
1024 else
1025 error ("unrecognized option -fdisable");
1026 free (argstr);
1027 return;
1029 pass = g->get_passes ()->get_pass_by_name (phase_name);
1030 if (!pass || pass->static_pass_number == -1)
1032 if (is_enable)
1033 error ("unknown pass %s specified in -fenable", phase_name);
1034 else
1035 error ("unknown pass %s specified in -fdisable", phase_name);
1036 free (argstr);
1037 return;
1040 if (is_enable)
1041 tab = &enabled_pass_uid_range_tab;
1042 else
1043 tab = &disabled_pass_uid_range_tab;
1045 if ((unsigned) pass->static_pass_number >= tab->length ())
1046 tab->safe_grow_cleared (pass->static_pass_number + 1);
1048 if (!range_str)
1050 uid_range_p slot;
1051 uid_range_p new_range = XCNEW (struct uid_range);
1053 new_range->start = 0;
1054 new_range->last = (unsigned)-1;
1056 slot = (*tab)[pass->static_pass_number];
1057 new_range->next = slot;
1058 (*tab)[pass->static_pass_number] = new_range;
1059 if (is_enable)
1060 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1061 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1062 else
1063 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1064 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1066 else
1068 char *next_range = NULL;
1069 char *one_range = range_str;
1070 char *end_val = NULL;
1074 uid_range_p slot;
1075 uid_range_p new_range;
1076 char *invalid = NULL;
1077 long start;
1078 char *func_name = NULL;
1080 next_range = strchr (one_range, ',');
1081 if (next_range)
1083 *next_range = '\0';
1084 next_range++;
1087 end_val = strchr (one_range, ':');
1088 if (end_val)
1090 *end_val = '\0';
1091 end_val++;
1093 start = strtol (one_range, &invalid, 10);
1094 if (*invalid || start < 0)
1096 if (end_val || (one_range[0] >= '0'
1097 && one_range[0] <= '9'))
1099 error ("Invalid range %s in option %s",
1100 one_range,
1101 is_enable ? "-fenable" : "-fdisable");
1102 free (argstr);
1103 return;
1105 func_name = one_range;
1107 if (!end_val)
1109 new_range = XCNEW (struct uid_range);
1110 if (!func_name)
1112 new_range->start = (unsigned) start;
1113 new_range->last = (unsigned) start;
1115 else
1117 new_range->start = (unsigned) -1;
1118 new_range->last = (unsigned) -1;
1119 new_range->assem_name = xstrdup (func_name);
1122 else
1124 long last = strtol (end_val, &invalid, 10);
1125 if (*invalid || last < start)
1127 error ("Invalid range %s in option %s",
1128 end_val,
1129 is_enable ? "-fenable" : "-fdisable");
1130 free (argstr);
1131 return;
1133 new_range = XCNEW (struct uid_range);
1134 new_range->start = (unsigned) start;
1135 new_range->last = (unsigned) last;
1138 slot = (*tab)[pass->static_pass_number];
1139 new_range->next = slot;
1140 (*tab)[pass->static_pass_number] = new_range;
1141 if (is_enable)
1143 if (new_range->assem_name)
1144 inform (UNKNOWN_LOCATION,
1145 "enable pass %s for function %s",
1146 phase_name, new_range->assem_name);
1147 else
1148 inform (UNKNOWN_LOCATION,
1149 "enable pass %s for functions in the range of [%u, %u]",
1150 phase_name, new_range->start, new_range->last);
1152 else
1154 if (new_range->assem_name)
1155 inform (UNKNOWN_LOCATION,
1156 "disable pass %s for function %s",
1157 phase_name, new_range->assem_name);
1158 else
1159 inform (UNKNOWN_LOCATION,
1160 "disable pass %s for functions in the range of [%u, %u]",
1161 phase_name, new_range->start, new_range->last);
1164 one_range = next_range;
1165 } while (next_range);
1168 free (argstr);
1171 /* Enable pass specified by ARG. */
1173 void
1174 enable_pass (const char *arg)
1176 enable_disable_pass (arg, true);
1179 /* Disable pass specified by ARG. */
1181 void
1182 disable_pass (const char *arg)
1184 enable_disable_pass (arg, false);
1187 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1189 static bool
1190 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1191 tree func,
1192 vec<uid_range_p> tab)
1194 uid_range_p slot, range;
1195 int cgraph_uid;
1196 const char *aname = NULL;
1198 if (!tab.exists ()
1199 || (unsigned) pass->static_pass_number >= tab.length ()
1200 || pass->static_pass_number == -1)
1201 return false;
1203 slot = tab[pass->static_pass_number];
1204 if (!slot)
1205 return false;
1207 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1208 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1209 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1211 range = slot;
1212 while (range)
1214 if ((unsigned) cgraph_uid >= range->start
1215 && (unsigned) cgraph_uid <= range->last)
1216 return true;
1217 if (range->assem_name && aname
1218 && !strcmp (range->assem_name, aname))
1219 return true;
1220 range = range->next;
1223 return false;
1227 /* Update static_pass_number for passes (and the flag
1228 TODO_mark_first_instance).
1230 Passes are constructed with static_pass_number preinitialized to 0
1232 This field is used in two different ways: initially as instance numbers
1233 of their kind, and then as ids within the entire pass manager.
1235 Within pass_manager::pass_manager:
1237 * In add_pass_instance(), as called by next_pass_1 in
1238 NEXT_PASS in init_optimization_passes
1240 * When the initial instance of a pass within a pass manager is seen,
1241 it is flagged, and its static_pass_number is set to -1
1243 * On subsequent times that it is seen, the static pass number
1244 is decremented each time, so that if there are e.g. 4 dups,
1245 they have static_pass_number -4, 2, 3, 4 respectively (note
1246 how the initial one is negative and gives the count); these
1247 can be thought of as instance numbers of the specific pass
1249 * Within the register_dump_files () traversal, set_pass_for_id()
1250 is called on each pass, using these instance numbers to create
1251 dumpfile switches, and then overwriting them with a pass id,
1252 which are global to the whole pass manager (based on
1253 (TDI_end + current value of extra_dump_files_in_use) ) */
1255 static void
1256 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1257 opt_pass *initial_pass)
1259 /* Are we dealing with the first pass of its kind, or a clone? */
1260 if (new_pass != initial_pass)
1262 /* We're dealing with a clone. */
1263 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1265 /* Indicate to register_dump_files that this pass has duplicates,
1266 and so it should rename the dump file. The first instance will
1267 be -1, and be number of duplicates = -static_pass_number - 1.
1268 Subsequent instances will be > 0 and just the duplicate number. */
1269 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1271 initial_pass->static_pass_number -= 1;
1272 new_pass->static_pass_number = -initial_pass->static_pass_number;
1275 else
1277 /* We're dealing with the first pass of its kind. */
1278 new_pass->todo_flags_start |= TODO_mark_first_instance;
1279 new_pass->static_pass_number = -1;
1281 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1285 /* Add a pass to the pass list. Duplicate the pass if it's already
1286 in the list. */
1288 static opt_pass **
1289 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1291 /* Every pass should have a name so that plugins can refer to them. */
1292 gcc_assert (pass->name != NULL);
1294 add_pass_instance (pass, false, initial_pass);
1295 *list = pass;
1297 return &(*list)->next;
1300 /* List node for an inserted pass instance. We need to keep track of all
1301 the newly-added pass instances (with 'added_pass_nodes' defined below)
1302 so that we can register their dump files after pass-positioning is finished.
1303 Registering dumping files needs to be post-processed or the
1304 static_pass_number of the opt_pass object would be modified and mess up
1305 the dump file names of future pass instances to be added. */
1307 struct pass_list_node
1309 opt_pass *pass;
1310 struct pass_list_node *next;
1313 static struct pass_list_node *added_pass_nodes = NULL;
1314 static struct pass_list_node *prev_added_pass_node;
1316 /* Insert the pass at the proper position. Return true if the pass
1317 is successfully added.
1319 NEW_PASS_INFO - new pass to be inserted
1320 PASS_LIST - root of the pass list to insert the new pass to */
1322 static bool
1323 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1325 opt_pass *pass = *pass_list, *prev_pass = NULL;
1326 bool success = false;
1328 for ( ; pass; prev_pass = pass, pass = pass->next)
1330 /* Check if the current pass is of the same type as the new pass and
1331 matches the name and the instance number of the reference pass. */
1332 if (pass->type == new_pass_info->pass->type
1333 && pass->name
1334 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1335 && ((new_pass_info->ref_pass_instance_number == 0)
1336 || (new_pass_info->ref_pass_instance_number ==
1337 pass->static_pass_number)
1338 || (new_pass_info->ref_pass_instance_number == 1
1339 && pass->todo_flags_start & TODO_mark_first_instance)))
1341 opt_pass *new_pass;
1342 struct pass_list_node *new_pass_node;
1344 if (new_pass_info->ref_pass_instance_number == 0)
1346 new_pass = new_pass_info->pass->clone ();
1347 add_pass_instance (new_pass, true, new_pass_info->pass);
1349 else
1351 new_pass = new_pass_info->pass;
1352 add_pass_instance (new_pass, true, new_pass);
1355 /* Insert the new pass instance based on the positioning op. */
1356 switch (new_pass_info->pos_op)
1358 case PASS_POS_INSERT_AFTER:
1359 new_pass->next = pass->next;
1360 pass->next = new_pass;
1362 /* Skip newly inserted pass to avoid repeated
1363 insertions in the case where the new pass and the
1364 existing one have the same name. */
1365 pass = new_pass;
1366 break;
1367 case PASS_POS_INSERT_BEFORE:
1368 new_pass->next = pass;
1369 if (prev_pass)
1370 prev_pass->next = new_pass;
1371 else
1372 *pass_list = new_pass;
1373 break;
1374 case PASS_POS_REPLACE:
1375 new_pass->next = pass->next;
1376 if (prev_pass)
1377 prev_pass->next = new_pass;
1378 else
1379 *pass_list = new_pass;
1380 new_pass->sub = pass->sub;
1381 new_pass->tv_id = pass->tv_id;
1382 pass = new_pass;
1383 break;
1384 default:
1385 error ("invalid pass positioning operation");
1386 return false;
1389 /* Save the newly added pass (instance) in the added_pass_nodes
1390 list so that we can register its dump file later. Note that
1391 we cannot register the dump file now because doing so will modify
1392 the static_pass_number of the opt_pass object and therefore
1393 mess up the dump file name of future instances. */
1394 new_pass_node = XCNEW (struct pass_list_node);
1395 new_pass_node->pass = new_pass;
1396 if (!added_pass_nodes)
1397 added_pass_nodes = new_pass_node;
1398 else
1399 prev_added_pass_node->next = new_pass_node;
1400 prev_added_pass_node = new_pass_node;
1402 success = true;
1405 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1406 success = true;
1409 return success;
1412 /* Hooks a new pass into the pass lists.
1414 PASS_INFO - pass information that specifies the opt_pass object,
1415 reference pass, instance number, and how to position
1416 the pass */
1418 void
1419 register_pass (struct register_pass_info *pass_info)
1421 g->get_passes ()->register_pass (pass_info);
1424 void
1425 register_pass (opt_pass* pass, pass_positioning_ops pos,
1426 const char* ref_pass_name, int ref_pass_inst_number)
1428 register_pass_info i;
1429 i.pass = pass;
1430 i.reference_pass_name = ref_pass_name;
1431 i.ref_pass_instance_number = ref_pass_inst_number;
1432 i.pos_op = pos;
1434 g->get_passes ()->register_pass (&i);
1437 void
1438 pass_manager::register_pass (struct register_pass_info *pass_info)
1440 bool all_instances, success;
1441 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1443 /* The checks below could fail in buggy plugins. Existing GCC
1444 passes should never fail these checks, so we mention plugin in
1445 the messages. */
1446 if (!pass_info->pass)
1447 fatal_error (input_location, "plugin cannot register a missing pass");
1449 if (!pass_info->pass->name)
1450 fatal_error (input_location, "plugin cannot register an unnamed pass");
1452 if (!pass_info->reference_pass_name)
1453 fatal_error
1454 (input_location,
1455 "plugin cannot register pass %qs without reference pass name",
1456 pass_info->pass->name);
1458 /* Try to insert the new pass to the pass lists. We need to check
1459 all five lists as the reference pass could be in one (or all) of
1460 them. */
1461 all_instances = pass_info->ref_pass_instance_number == 0;
1462 success = position_pass (pass_info, &all_lowering_passes);
1463 if (!success || all_instances)
1464 success |= position_pass (pass_info, &all_small_ipa_passes);
1465 if (!success || all_instances)
1466 success |= position_pass (pass_info, &all_regular_ipa_passes);
1467 if (!success || all_instances)
1468 success |= position_pass (pass_info, &all_late_ipa_passes);
1469 if (!success || all_instances)
1470 success |= position_pass (pass_info, &all_passes);
1471 if (!success)
1472 fatal_error
1473 (input_location,
1474 "pass %qs not found but is referenced by new pass %qs",
1475 pass_info->reference_pass_name, pass_info->pass->name);
1477 /* OK, we have successfully inserted the new pass. We need to register
1478 the dump files for the newly added pass and its duplicates (if any).
1479 Because the registration of plugin/backend passes happens after the
1480 command-line options are parsed, the options that specify single
1481 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1482 passes. Therefore we currently can only enable dumping of
1483 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1484 are specified. While doing so, we also delete the pass_list_node
1485 objects created during pass positioning. */
1486 while (added_pass_nodes)
1488 struct pass_list_node *next_node = added_pass_nodes->next;
1489 enum tree_dump_index tdi;
1490 register_one_dump_file (added_pass_nodes->pass);
1491 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1492 || added_pass_nodes->pass->type == IPA_PASS)
1493 tdi = TDI_ipa_all;
1494 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1495 tdi = TDI_tree_all;
1496 else
1497 tdi = TDI_rtl_all;
1498 /* Check if dump-all flag is specified. */
1499 if (dumps->get_dump_file_info (tdi)->pstate)
1501 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1502 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1503 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1504 ->pflags = dumps->get_dump_file_info (tdi)->pflags;
1506 XDELETE (added_pass_nodes);
1507 added_pass_nodes = next_node;
1511 /* Construct the pass tree. The sequencing of passes is driven by
1512 the cgraph routines:
1514 finalize_compilation_unit ()
1515 for each node N in the cgraph
1516 cgraph_analyze_function (N)
1517 cgraph_lower_function (N) -> all_lowering_passes
1519 If we are optimizing, compile is then invoked:
1521 compile ()
1522 ipa_passes () -> all_small_ipa_passes
1523 -> Analysis of all_regular_ipa_passes
1524 * possible LTO streaming at copmilation time *
1525 -> Execution of all_regular_ipa_passes
1526 * possible LTO streaming at link time *
1527 -> all_late_ipa_passes
1528 expand_all_functions ()
1529 for each node N in the cgraph
1530 expand_function (N) -> Transformation of all_regular_ipa_passes
1531 -> all_passes
1534 pass_manager::pass_manager (context *ctxt)
1535 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1536 all_regular_ipa_passes (NULL),
1537 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1538 m_ctxt (ctxt), m_name_to_pass_map (NULL)
1540 opt_pass **p;
1542 /* Zero-initialize pass members. */
1543 #define INSERT_PASSES_AFTER(PASS)
1544 #define PUSH_INSERT_PASSES_WITHIN(PASS)
1545 #define POP_INSERT_PASSES()
1546 #define NEXT_PASS(PASS, NUM) PASS ## _ ## NUM = NULL
1547 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) NEXT_PASS (PASS, NUM)
1548 #define TERMINATE_PASS_LIST(PASS)
1549 #include "pass-instances.def"
1550 #undef INSERT_PASSES_AFTER
1551 #undef PUSH_INSERT_PASSES_WITHIN
1552 #undef POP_INSERT_PASSES
1553 #undef NEXT_PASS
1554 #undef NEXT_PASS_WITH_ARG
1555 #undef TERMINATE_PASS_LIST
1557 /* Initialize the pass_lists array. */
1558 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1559 GCC_PASS_LISTS
1560 #undef DEF_PASS_LIST
1562 /* Build the tree of passes. */
1564 #define INSERT_PASSES_AFTER(PASS) \
1566 opt_pass **p_start; \
1567 p_start = p = &(PASS);
1569 #define TERMINATE_PASS_LIST(PASS) \
1570 gcc_assert (p_start == &PASS); \
1571 *p = NULL; \
1574 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1576 opt_pass **p = &(PASS ## _1)->sub;
1578 #define POP_INSERT_PASSES() \
1581 #define NEXT_PASS(PASS, NUM) \
1582 do { \
1583 gcc_assert (NULL == PASS ## _ ## NUM); \
1584 if ((NUM) == 1) \
1585 PASS ## _1 = make_##PASS (m_ctxt); \
1586 else \
1588 gcc_assert (PASS ## _1); \
1589 PASS ## _ ## NUM = PASS ## _1->clone (); \
1591 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1592 } while (0)
1594 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) \
1595 do { \
1596 NEXT_PASS (PASS, NUM); \
1597 PASS ## _ ## NUM->set_pass_param (0, ARG); \
1598 } while (0)
1600 #include "pass-instances.def"
1602 #undef INSERT_PASSES_AFTER
1603 #undef PUSH_INSERT_PASSES_WITHIN
1604 #undef POP_INSERT_PASSES
1605 #undef NEXT_PASS
1606 #undef NEXT_PASS_WITH_ARG
1607 #undef TERMINATE_PASS_LIST
1609 /* Register the passes with the tree dump code. */
1610 register_dump_files (all_lowering_passes);
1611 register_dump_files (all_small_ipa_passes);
1612 register_dump_files (all_regular_ipa_passes);
1613 register_dump_files (all_late_ipa_passes);
1614 register_dump_files (all_passes);
1617 static void
1618 delete_pass_tree (opt_pass *pass)
1620 while (pass)
1622 /* Recurse into child passes. */
1623 delete_pass_tree (pass->sub);
1625 opt_pass *next = pass->next;
1627 /* Delete this pass. */
1628 delete pass;
1630 /* Iterate onto sibling passes. */
1631 pass = next;
1635 pass_manager::~pass_manager ()
1637 XDELETEVEC (passes_by_id);
1639 /* Call delete_pass_tree on each of the pass_lists. */
1640 #define DEF_PASS_LIST(LIST) \
1641 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1642 GCC_PASS_LISTS
1643 #undef DEF_PASS_LIST
1647 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1648 function CALLBACK for every function in the call graph. Otherwise,
1649 call CALLBACK on the current function. */
1651 static void
1652 do_per_function (void (*callback) (function *, void *data), void *data)
1654 if (current_function_decl)
1655 callback (cfun, data);
1656 else
1658 struct cgraph_node *node;
1659 FOR_EACH_DEFINED_FUNCTION (node)
1660 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1661 && (!node->clone_of || node->decl != node->clone_of->decl))
1662 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1666 /* Because inlining might remove no-longer reachable nodes, we need to
1667 keep the array visible to garbage collector to avoid reading collected
1668 out nodes. */
1669 static int nnodes;
1670 static GTY ((length ("nnodes"))) cgraph_node **order;
1672 /* Hook called when NODE is removed and therefore should be
1673 excluded from order vector. DATA is an array of integers.
1674 DATA[0] holds max index it may be accessed by. For cgraph
1675 node DATA[node->uid + 1] holds index of this node in order
1676 vector. */
1677 static void
1678 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1680 int *order_idx = (int *)data;
1682 if (node->uid >= order_idx[0])
1683 return;
1685 int idx = order_idx[node->uid + 1];
1686 if (idx >= 0 && idx < nnodes && order[idx] == node)
1687 order[idx] = NULL;
1690 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1691 function CALLBACK for every function in the call graph. Otherwise,
1692 call CALLBACK on the current function.
1693 This function is global so that plugins can use it. */
1694 void
1695 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1697 int i;
1699 if (current_function_decl)
1700 callback (cfun, data);
1701 else
1703 cgraph_node_hook_list *hook;
1704 int *order_idx;
1705 gcc_assert (!order);
1706 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1708 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1709 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1710 order_idx[0] = symtab->cgraph_max_uid;
1712 nnodes = ipa_reverse_postorder (order);
1713 for (i = nnodes - 1; i >= 0; i--)
1715 order[i]->process = 1;
1716 order_idx[order[i]->uid + 1] = i;
1718 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1719 order_idx);
1720 for (i = nnodes - 1; i >= 0; i--)
1722 /* Function could be inlined and removed as unreachable. */
1723 if (!order[i])
1724 continue;
1726 struct cgraph_node *node = order[i];
1728 /* Allow possibly removed nodes to be garbage collected. */
1729 order[i] = NULL;
1730 node->process = 0;
1731 if (node->has_gimple_body_p ())
1733 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1734 push_cfun (fn);
1735 callback (fn, data);
1736 pop_cfun ();
1739 symtab->remove_cgraph_removal_hook (hook);
1741 ggc_free (order);
1742 order = NULL;
1743 nnodes = 0;
1746 /* Helper function to perform function body dump. */
1748 static void
1749 execute_function_dump (function *fn, void *data)
1751 opt_pass *pass = (opt_pass *)data;
1753 if (dump_file)
1755 push_cfun (fn);
1757 if (fn->curr_properties & PROP_trees)
1758 dump_function_to_file (fn->decl, dump_file, dump_flags);
1759 else
1760 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1762 /* Flush the file. If verification fails, we won't be able to
1763 close the file before aborting. */
1764 fflush (dump_file);
1766 if ((fn->curr_properties & PROP_cfg)
1767 && (dump_flags & TDF_GRAPH))
1769 gcc::dump_manager *dumps = g->get_dumps ();
1770 struct dump_file_info *dfi
1771 = dumps->get_dump_file_info (pass->static_pass_number);
1772 if (!dfi->graph_dump_initialized)
1774 clean_graph_dump_file (dump_file_name);
1775 dfi->graph_dump_initialized = true;
1777 print_graph_cfg (dump_file_name, fn);
1780 pop_cfun ();
1784 /* This function is called when an internal compiler error is encountered.
1785 Ensure that function dump is made available before compiler is aborted. */
1787 void
1788 emergency_dump_function ()
1790 if (!current_pass)
1791 return;
1792 enum opt_pass_type pt = current_pass->type;
1793 fnotice (stderr, "during %s pass: %s\n",
1794 pt == GIMPLE_PASS ? "GIMPLE" : pt == RTL_PASS ? "RTL" : "IPA",
1795 current_pass->name);
1796 if (!dump_file || !cfun)
1797 return;
1798 fnotice (stderr, "dump file: %s\n", dump_file_name);
1799 fprintf (dump_file, "\n\n\nEMERGENCY DUMP:\n\n");
1800 execute_function_dump (cfun, current_pass);
1803 static struct profile_record *profile_record;
1805 /* Do profile consistency book-keeping for the pass with static number INDEX.
1806 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1807 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1808 if we are only book-keeping on passes that may have selectively disabled
1809 themselves on a given function. */
1810 static void
1811 check_profile_consistency (int index, int subpass, bool run)
1813 pass_manager *passes = g->get_passes ();
1814 if (index == -1)
1815 return;
1816 if (!profile_record)
1817 profile_record = XCNEWVEC (struct profile_record,
1818 passes->passes_by_id_size);
1819 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1820 gcc_assert (subpass < 2);
1821 profile_record[index].run |= run;
1822 account_profile_record (&profile_record[index], subpass);
1825 /* Output profile consistency. */
1827 void
1828 dump_profile_report (void)
1830 g->get_passes ()->dump_profile_report ();
1833 void
1834 pass_manager::dump_profile_report () const
1836 int i, j;
1837 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1838 gcov_type last_time = 0, last_size = 0;
1839 double rel_time_change, rel_size_change;
1840 int last_reported = 0;
1842 if (!profile_record)
1843 return;
1844 fprintf (stderr, "\nProfile consistency report:\n\n");
1845 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1846 fprintf (stderr, " |freq count |freq count |size time\n");
1848 for (i = 0; i < passes_by_id_size; i++)
1849 for (j = 0 ; j < 2; j++)
1850 if (profile_record[i].run)
1852 if (last_time)
1853 rel_time_change = (profile_record[i].time[j]
1854 - (double)last_time) * 100 / (double)last_time;
1855 else
1856 rel_time_change = 0;
1857 if (last_size)
1858 rel_size_change = (profile_record[i].size[j]
1859 - (double)last_size) * 100 / (double)last_size;
1860 else
1861 rel_size_change = 0;
1863 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1864 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1865 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1866 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1867 || rel_time_change || rel_size_change)
1869 last_reported = i;
1870 fprintf (stderr, "%-20s %s",
1871 passes_by_id [i]->name,
1872 j ? "(after TODO)" : " ");
1873 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1874 fprintf (stderr, "| %+5i",
1875 profile_record[i].num_mismatched_freq_in[j]
1876 - last_freq_in);
1877 else
1878 fprintf (stderr, "| ");
1879 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1880 fprintf (stderr, " %+5i",
1881 profile_record[i].num_mismatched_count_in[j]
1882 - last_count_in);
1883 else
1884 fprintf (stderr, " ");
1885 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1886 fprintf (stderr, "| %+5i",
1887 profile_record[i].num_mismatched_freq_out[j]
1888 - last_freq_out);
1889 else
1890 fprintf (stderr, "| ");
1891 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1892 fprintf (stderr, " %+5i",
1893 profile_record[i].num_mismatched_count_out[j]
1894 - last_count_out);
1895 else
1896 fprintf (stderr, " ");
1898 /* Size/time units change across gimple and RTL. */
1899 if (i == pass_expand_1->static_pass_number)
1900 fprintf (stderr, "|----------");
1901 else
1903 if (rel_size_change)
1904 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1905 else
1906 fprintf (stderr, "| ");
1907 if (rel_time_change)
1908 fprintf (stderr, " %+8.4f%%", rel_time_change);
1910 fprintf (stderr, "\n");
1911 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1912 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1913 last_count_in = profile_record[i].num_mismatched_count_in[j];
1914 last_count_out = profile_record[i].num_mismatched_count_out[j];
1916 else if (j && last_reported != i)
1918 last_reported = i;
1919 fprintf (stderr, "%-20s ------------| | |\n",
1920 passes_by_id [i]->name);
1922 last_time = profile_record[i].time[j];
1923 last_size = profile_record[i].size[j];
1927 /* Perform all TODO actions that ought to be done on each function. */
1929 static void
1930 execute_function_todo (function *fn, void *data)
1932 bool from_ipa_pass = (cfun == NULL);
1933 unsigned int flags = (size_t)data;
1934 flags &= ~fn->last_verified;
1935 if (!flags)
1936 return;
1938 push_cfun (fn);
1940 /* Always cleanup the CFG before trying to update SSA. */
1941 if (flags & TODO_cleanup_cfg)
1943 cleanup_tree_cfg ();
1945 /* When cleanup_tree_cfg merges consecutive blocks, it may
1946 perform some simplistic propagation when removing single
1947 valued PHI nodes. This propagation may, in turn, cause the
1948 SSA form to become out-of-date (see PR 22037). So, even
1949 if the parent pass had not scheduled an SSA update, we may
1950 still need to do one. */
1951 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1952 flags |= TODO_update_ssa;
1955 if (flags & TODO_update_ssa_any)
1957 unsigned update_flags = flags & TODO_update_ssa_any;
1958 update_ssa (update_flags);
1961 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1962 compute_may_aliases ();
1964 if (optimize && (flags & TODO_update_address_taken))
1965 execute_update_addresses_taken ();
1967 if (flags & TODO_remove_unused_locals)
1968 remove_unused_locals ();
1970 if (flags & TODO_rebuild_frequencies)
1971 rebuild_frequencies ();
1973 if (flags & TODO_rebuild_cgraph_edges)
1974 cgraph_edge::rebuild_edges ();
1976 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1977 /* If we've seen errors do not bother running any verifiers. */
1978 if (flag_checking && !seen_error ())
1980 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1981 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1983 if (flags & TODO_verify_il)
1985 if (cfun->curr_properties & PROP_trees)
1987 if (cfun->curr_properties & PROP_cfg)
1988 /* IPA passes leave stmts to be fixed up, so make sure to
1989 not verify stmts really throw. */
1990 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1991 else
1992 verify_gimple_in_seq (gimple_body (cfun->decl));
1994 if (cfun->curr_properties & PROP_ssa)
1995 /* IPA passes leave stmts to be fixed up, so make sure to
1996 not verify SSA operands whose verifier will choke on that. */
1997 verify_ssa (true, !from_ipa_pass);
1998 /* IPA passes leave basic-blocks unsplit, so make sure to
1999 not trip on that. */
2000 if ((cfun->curr_properties & PROP_cfg)
2001 && !from_ipa_pass)
2002 verify_flow_info ();
2003 if (current_loops
2004 && ! loops_state_satisfies_p (LOOPS_NEED_FIXUP))
2006 verify_loop_structure ();
2007 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
2008 verify_loop_closed_ssa (false);
2010 if (cfun->curr_properties & PROP_rtl)
2011 verify_rtl_sharing ();
2014 /* Make sure verifiers don't change dominator state. */
2015 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
2016 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
2019 fn->last_verified = flags & TODO_verify_all;
2021 pop_cfun ();
2023 /* For IPA passes make sure to release dominator info, it can be
2024 computed by non-verifying TODOs. */
2025 if (from_ipa_pass)
2027 free_dominance_info (fn, CDI_DOMINATORS);
2028 free_dominance_info (fn, CDI_POST_DOMINATORS);
2032 /* Perform all TODO actions. */
2033 static void
2034 execute_todo (unsigned int flags)
2036 if (flag_checking
2037 && cfun
2038 && need_ssa_update_p (cfun))
2039 gcc_assert (flags & TODO_update_ssa_any);
2041 statistics_fini_pass ();
2043 if (flags)
2044 do_per_function (execute_function_todo, (void *)(size_t) flags);
2046 /* At this point we should not have any unreachable code in the
2047 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
2048 if (cfun && cfun->gimple_df)
2049 flush_ssaname_freelist ();
2051 /* Always remove functions just as before inlining: IPA passes might be
2052 interested to see bodies of extern inline functions that are not inlined
2053 to analyze side effects. The full removal is done just at the end
2054 of IPA pass queue. */
2055 if (flags & TODO_remove_functions)
2057 gcc_assert (!cfun);
2058 symtab->remove_unreachable_nodes (dump_file);
2061 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2063 gcc_assert (!cfun);
2064 symtab->dump (dump_file);
2065 /* Flush the file. If verification fails, we won't be able to
2066 close the file before aborting. */
2067 fflush (dump_file);
2070 /* Now that the dumping has been done, we can get rid of the optional
2071 df problems. */
2072 if (flags & TODO_df_finish)
2073 df_finish_pass ((flags & TODO_df_verify) != 0);
2076 /* Verify invariants that should hold between passes. This is a place
2077 to put simple sanity checks. */
2079 static void
2080 verify_interpass_invariants (void)
2082 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2085 /* Clear the last verified flag. */
2087 static void
2088 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2090 fn->last_verified = 0;
2093 /* Helper function. Verify that the properties has been turn into the
2094 properties expected by the pass. */
2096 static void
2097 verify_curr_properties (function *fn, void *data)
2099 unsigned int props = (size_t)data;
2100 gcc_assert ((fn->curr_properties & props) == props);
2103 /* Release dump file name if set. */
2105 static void
2106 release_dump_file_name (void)
2108 if (dump_file_name)
2110 free (CONST_CAST (char *, dump_file_name));
2111 dump_file_name = NULL;
2115 /* Initialize pass dump file. */
2116 /* This is non-static so that the plugins can use it. */
2118 bool
2119 pass_init_dump_file (opt_pass *pass)
2121 /* If a dump file name is present, open it if enabled. */
2122 if (pass->static_pass_number != -1)
2124 timevar_push (TV_DUMP);
2125 gcc::dump_manager *dumps = g->get_dumps ();
2126 bool initializing_dump =
2127 !dumps->dump_initialized_p (pass->static_pass_number);
2128 release_dump_file_name ();
2129 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2130 dumps->dump_start (pass->static_pass_number, &dump_flags);
2131 if (dump_file && current_function_decl && ! (dump_flags & TDF_GIMPLE))
2132 dump_function_header (dump_file, current_function_decl, dump_flags);
2133 if (initializing_dump
2134 && dump_file && (dump_flags & TDF_GRAPH)
2135 && cfun && (cfun->curr_properties & PROP_cfg))
2137 clean_graph_dump_file (dump_file_name);
2138 struct dump_file_info *dfi
2139 = dumps->get_dump_file_info (pass->static_pass_number);
2140 dfi->graph_dump_initialized = true;
2142 timevar_pop (TV_DUMP);
2143 return initializing_dump;
2145 else
2146 return false;
2149 /* Flush PASS dump file. */
2150 /* This is non-static so that plugins can use it. */
2152 void
2153 pass_fini_dump_file (opt_pass *pass)
2155 timevar_push (TV_DUMP);
2157 /* Flush and close dump file. */
2158 release_dump_file_name ();
2160 g->get_dumps ()->dump_finish (pass->static_pass_number);
2161 timevar_pop (TV_DUMP);
2164 /* After executing the pass, apply expected changes to the function
2165 properties. */
2167 static void
2168 update_properties_after_pass (function *fn, void *data)
2170 opt_pass *pass = (opt_pass *) data;
2171 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2172 & ~pass->properties_destroyed;
2175 /* Execute summary generation for all of the passes in IPA_PASS. */
2177 void
2178 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2180 while (ipa_pass)
2182 opt_pass *pass = ipa_pass;
2184 /* Execute all of the IPA_PASSes in the list. */
2185 if (ipa_pass->type == IPA_PASS
2186 && pass->gate (cfun)
2187 && ipa_pass->generate_summary)
2189 pass_init_dump_file (pass);
2191 /* If a timevar is present, start it. */
2192 if (pass->tv_id)
2193 timevar_push (pass->tv_id);
2195 current_pass = pass;
2196 ipa_pass->generate_summary ();
2198 /* Stop timevar. */
2199 if (pass->tv_id)
2200 timevar_pop (pass->tv_id);
2202 pass_fini_dump_file (pass);
2204 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2208 /* Execute IPA_PASS function transform on NODE. */
2210 static void
2211 execute_one_ipa_transform_pass (struct cgraph_node *node,
2212 ipa_opt_pass_d *ipa_pass)
2214 opt_pass *pass = ipa_pass;
2215 unsigned int todo_after = 0;
2217 current_pass = pass;
2218 if (!ipa_pass->function_transform)
2219 return;
2221 /* Note that the folders should only create gimple expressions.
2222 This is a hack until the new folder is ready. */
2223 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2225 pass_init_dump_file (pass);
2227 /* If a timevar is present, start it. */
2228 if (pass->tv_id != TV_NONE)
2229 timevar_push (pass->tv_id);
2231 /* Run pre-pass verification. */
2232 execute_todo (ipa_pass->function_transform_todo_flags_start);
2234 /* Do it! */
2235 todo_after = ipa_pass->function_transform (node);
2237 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2238 check_profile_consistency (pass->static_pass_number, 0, true);
2240 /* Run post-pass cleanup and verification. */
2241 execute_todo (todo_after);
2242 verify_interpass_invariants ();
2243 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2244 check_profile_consistency (pass->static_pass_number, 1, true);
2246 /* Stop timevar. */
2247 if (pass->tv_id != TV_NONE)
2248 timevar_pop (pass->tv_id);
2250 if (dump_file)
2251 do_per_function (execute_function_dump, pass);
2252 pass_fini_dump_file (pass);
2254 current_pass = NULL;
2255 redirect_edge_var_map_empty ();
2257 /* Signal this is a suitable GC collection point. */
2258 if (!(todo_after & TODO_do_not_ggc_collect))
2259 ggc_collect ();
2262 /* For the current function, execute all ipa transforms. */
2264 void
2265 execute_all_ipa_transforms (void)
2267 struct cgraph_node *node;
2268 if (!cfun)
2269 return;
2270 node = cgraph_node::get (current_function_decl);
2272 if (node->ipa_transforms_to_apply.exists ())
2274 unsigned int i;
2276 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2277 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2278 node->ipa_transforms_to_apply.release ();
2282 /* Check if PASS is explicitly disabled or enabled and return
2283 the gate status. FUNC is the function to be processed, and
2284 GATE_STATUS is the gate status determined by pass manager by
2285 default. */
2287 static bool
2288 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2290 bool explicitly_enabled = false;
2291 bool explicitly_disabled = false;
2293 explicitly_enabled
2294 = is_pass_explicitly_enabled_or_disabled (pass, func,
2295 enabled_pass_uid_range_tab);
2296 explicitly_disabled
2297 = is_pass_explicitly_enabled_or_disabled (pass, func,
2298 disabled_pass_uid_range_tab);
2300 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2302 return gate_status;
2305 /* Determine if PASS_NAME matches CRITERION.
2306 Not a pure predicate, since it can update CRITERION, to support
2307 matching the Nth invocation of a pass.
2308 Subroutine of should_skip_pass_p. */
2310 static bool
2311 determine_pass_name_match (const char *pass_name, char *criterion)
2313 size_t namelen = strlen (pass_name);
2314 if (! strncmp (pass_name, criterion, namelen))
2316 /* The following supports starting with the Nth invocation
2317 of a pass (where N does not necessarily is equal to the
2318 dump file suffix). */
2319 if (criterion[namelen] == '\0'
2320 || (criterion[namelen] == '1'
2321 && criterion[namelen + 1] == '\0'))
2322 return true;
2323 else
2325 if (criterion[namelen + 1] == '\0')
2326 --criterion[namelen];
2327 return false;
2330 else
2331 return false;
2334 /* For skipping passes until "startwith" pass.
2335 Return true iff PASS should be skipped.
2336 Clear cfun->pass_startwith when encountering the "startwith" pass,
2337 so that all subsequent passes are run. */
2339 static bool
2340 should_skip_pass_p (opt_pass *pass)
2342 if (!cfun)
2343 return false;
2344 if (!cfun->pass_startwith)
2345 return false;
2347 /* For __GIMPLE functions, we have to at least start when we leave
2348 SSA. Hence, we need to detect the "expand" pass, and stop skipping
2349 when we encounter it. A cheap way to identify "expand" is it to
2350 detect the destruction of PROP_ssa.
2351 For __RTL functions, we invoke "rest_of_compilation" directly, which
2352 is after "expand", and hence we don't reach this conditional. */
2353 if (pass->properties_destroyed & PROP_ssa)
2355 if (!quiet_flag)
2356 fprintf (stderr, "starting anyway when leaving SSA: %s\n", pass->name);
2357 cfun->pass_startwith = NULL;
2358 return false;
2361 if (determine_pass_name_match (pass->name, cfun->pass_startwith))
2363 if (!quiet_flag)
2364 fprintf (stderr, "found starting pass: %s\n", pass->name);
2365 cfun->pass_startwith = NULL;
2366 return false;
2369 /* For GIMPLE passes, run any property provider (but continue skipping
2370 afterwards).
2371 We don't want to force running RTL passes that are property providers:
2372 "expand" is covered above, and the only pass other than "expand" that
2373 provides a property is "into_cfglayout" (PROP_cfglayout), which does
2374 too much for a dumped __RTL function. */
2375 if (pass->type == GIMPLE_PASS
2376 && pass->properties_provided != 0)
2377 return false;
2379 /* Don't skip df init; later RTL passes need it. */
2380 if (strstr (pass->name, "dfinit") != NULL)
2381 return false;
2383 if (!quiet_flag)
2384 fprintf (stderr, "skipping pass: %s\n", pass->name);
2386 /* If we get here, then we have a "startwith" that we haven't seen yet;
2387 skip the pass. */
2388 return true;
2391 /* Skip the given pass, for handling passes before "startwith"
2392 in __GIMPLE and__RTL-marked functions.
2393 In theory, this ought to be a no-op, but some of the RTL passes
2394 need additional processing here. */
2396 static void
2397 skip_pass (opt_pass *pass)
2399 /* Pass "reload" sets the global "reload_completed", and many
2400 things depend on this (e.g. instructions in .md files). */
2401 if (strcmp (pass->name, "reload") == 0)
2402 reload_completed = 1;
2404 /* The INSN_ADDRESSES vec is normally set up by
2405 shorten_branches; set it up for the benefit of passes that
2406 run after this. */
2407 if (strcmp (pass->name, "shorten") == 0)
2408 INSN_ADDRESSES_ALLOC (get_max_uid ());
2410 /* Update the cfg hooks as appropriate. */
2411 if (strcmp (pass->name, "into_cfglayout") == 0)
2413 cfg_layout_rtl_register_cfg_hooks ();
2414 cfun->curr_properties |= PROP_cfglayout;
2416 if (strcmp (pass->name, "outof_cfglayout") == 0)
2418 rtl_register_cfg_hooks ();
2419 cfun->curr_properties &= ~PROP_cfglayout;
2423 /* Execute PASS. */
2425 bool
2426 execute_one_pass (opt_pass *pass)
2428 unsigned int todo_after = 0;
2430 bool gate_status;
2432 /* IPA passes are executed on whole program, so cfun should be NULL.
2433 Other passes need function context set. */
2434 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2435 gcc_assert (!cfun && !current_function_decl);
2436 else
2437 gcc_assert (cfun && current_function_decl);
2439 current_pass = pass;
2441 /* Check whether gate check should be avoided.
2442 User controls the value of the gate through the parameter "gate_status". */
2443 gate_status = pass->gate (cfun);
2444 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2446 /* Override gate with plugin. */
2447 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2449 if (!gate_status)
2451 /* Run so passes selectively disabling themselves on a given function
2452 are not miscounted. */
2453 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2455 check_profile_consistency (pass->static_pass_number, 0, false);
2456 check_profile_consistency (pass->static_pass_number, 1, false);
2458 current_pass = NULL;
2459 return false;
2462 if (should_skip_pass_p (pass))
2464 skip_pass (pass);
2465 return true;
2468 /* Pass execution event trigger: useful to identify passes being
2469 executed. */
2470 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2472 if (!quiet_flag && !cfun)
2473 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2475 /* Note that the folders should only create gimple expressions.
2476 This is a hack until the new folder is ready. */
2477 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2479 pass_init_dump_file (pass);
2481 /* If a timevar is present, start it. */
2482 if (pass->tv_id != TV_NONE)
2483 timevar_push (pass->tv_id);
2485 /* Run pre-pass verification. */
2486 execute_todo (pass->todo_flags_start);
2488 if (flag_checking)
2489 do_per_function (verify_curr_properties,
2490 (void *)(size_t)pass->properties_required);
2492 /* Do it! */
2493 todo_after = pass->execute (cfun);
2495 if (todo_after & TODO_discard_function)
2497 /* Stop timevar. */
2498 if (pass->tv_id != TV_NONE)
2499 timevar_pop (pass->tv_id);
2501 pass_fini_dump_file (pass);
2503 gcc_assert (cfun);
2504 /* As cgraph_node::release_body expects release dominators info,
2505 we have to release it. */
2506 if (dom_info_available_p (CDI_DOMINATORS))
2507 free_dominance_info (CDI_DOMINATORS);
2509 if (dom_info_available_p (CDI_POST_DOMINATORS))
2510 free_dominance_info (CDI_POST_DOMINATORS);
2512 tree fn = cfun->decl;
2513 pop_cfun ();
2514 gcc_assert (!cfun);
2515 cgraph_node::get (fn)->release_body ();
2517 current_pass = NULL;
2518 redirect_edge_var_map_empty ();
2520 ggc_collect ();
2522 return true;
2525 do_per_function (clear_last_verified, NULL);
2527 do_per_function (update_properties_after_pass, pass);
2529 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2530 check_profile_consistency (pass->static_pass_number, 0, true);
2532 /* Run post-pass cleanup and verification. */
2533 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2534 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2535 check_profile_consistency (pass->static_pass_number, 1, true);
2537 verify_interpass_invariants ();
2539 /* Stop timevar. */
2540 if (pass->tv_id != TV_NONE)
2541 timevar_pop (pass->tv_id);
2543 if (pass->type == IPA_PASS
2544 && ((ipa_opt_pass_d *)pass)->function_transform)
2546 struct cgraph_node *node;
2547 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2548 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2550 else if (dump_file)
2551 do_per_function (execute_function_dump, pass);
2553 if (!current_function_decl)
2554 symtab->process_new_functions ();
2556 pass_fini_dump_file (pass);
2558 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2559 gcc_assert (!(cfun->curr_properties & PROP_trees)
2560 || pass->type != RTL_PASS);
2562 current_pass = NULL;
2563 redirect_edge_var_map_empty ();
2565 /* Signal this is a suitable GC collection point. */
2566 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2567 ggc_collect ();
2569 return true;
2572 static void
2573 execute_pass_list_1 (opt_pass *pass)
2577 gcc_assert (pass->type == GIMPLE_PASS
2578 || pass->type == RTL_PASS);
2580 if (cfun == NULL)
2581 return;
2582 if (execute_one_pass (pass) && pass->sub)
2583 execute_pass_list_1 (pass->sub);
2584 pass = pass->next;
2586 while (pass);
2589 void
2590 execute_pass_list (function *fn, opt_pass *pass)
2592 gcc_assert (fn == cfun);
2593 execute_pass_list_1 (pass);
2594 if (cfun && fn->cfg)
2596 free_dominance_info (CDI_DOMINATORS);
2597 free_dominance_info (CDI_POST_DOMINATORS);
2601 /* Write out all LTO data. */
2602 static void
2603 write_lto (void)
2605 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2606 lto_output ();
2607 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2608 timevar_push (TV_IPA_LTO_DECL_OUT);
2609 produce_asm_for_decls ();
2610 timevar_pop (TV_IPA_LTO_DECL_OUT);
2613 /* Same as execute_pass_list but assume that subpasses of IPA passes
2614 are local passes. If SET is not NULL, write out summaries of only
2615 those node in SET. */
2617 static void
2618 ipa_write_summaries_2 (opt_pass *pass, 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_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_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_summaries_2 (pass->sub, state);
2649 pass = pass->next;
2653 /* Helper function of ipa_write_summaries. Creates and destroys the
2654 decl state and calls ipa_write_summaries_2 for all passes that have
2655 summaries. SET is the set of nodes to be written. */
2657 static void
2658 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2660 pass_manager *passes = g->get_passes ();
2661 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2662 state->symtab_node_encoder = encoder;
2664 lto_output_init_mode_table ();
2665 lto_push_out_decl_state (state);
2667 gcc_assert (!flag_wpa);
2668 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2670 write_lto ();
2672 gcc_assert (lto_get_out_decl_state () == state);
2673 lto_pop_out_decl_state ();
2674 lto_delete_out_decl_state (state);
2677 /* Write out summaries for all the nodes in the callgraph. */
2679 void
2680 ipa_write_summaries (void)
2682 lto_symtab_encoder_t encoder;
2683 int i, order_pos;
2684 varpool_node *vnode;
2685 struct cgraph_node *node;
2686 struct cgraph_node **order;
2688 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2689 return;
2691 select_what_to_stream ();
2693 encoder = lto_symtab_encoder_new (false);
2695 /* Create the callgraph set in the same order used in
2696 cgraph_expand_all_functions. This mostly facilitates debugging,
2697 since it causes the gimple file to be processed in the same order
2698 as the source code. */
2699 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2700 order_pos = ipa_reverse_postorder (order);
2701 gcc_assert (order_pos == symtab->cgraph_count);
2703 for (i = order_pos - 1; i >= 0; i--)
2705 struct cgraph_node *node = order[i];
2707 if (node->has_gimple_body_p ())
2709 /* When streaming out references to statements as part of some IPA
2710 pass summary, the statements need to have uids assigned and the
2711 following does that for all the IPA passes here. Naturally, this
2712 ordering then matches the one IPA-passes get in their stmt_fixup
2713 hooks. */
2715 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2716 renumber_gimple_stmt_uids ();
2717 pop_cfun ();
2719 if (node->definition && node->need_lto_streaming)
2720 lto_set_symtab_encoder_in_partition (encoder, node);
2723 FOR_EACH_DEFINED_FUNCTION (node)
2724 if (node->alias && node->need_lto_streaming)
2725 lto_set_symtab_encoder_in_partition (encoder, node);
2726 FOR_EACH_DEFINED_VARIABLE (vnode)
2727 if (vnode->need_lto_streaming)
2728 lto_set_symtab_encoder_in_partition (encoder, vnode);
2730 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2732 free (order);
2735 /* Same as execute_pass_list but assume that subpasses of IPA passes
2736 are local passes. If SET is not NULL, write out optimization summaries of
2737 only those node in SET. */
2739 static void
2740 ipa_write_optimization_summaries_1 (opt_pass *pass,
2741 struct lto_out_decl_state *state)
2743 while (pass)
2745 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2746 gcc_assert (!current_function_decl);
2747 gcc_assert (!cfun);
2748 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2749 if (pass->type == IPA_PASS
2750 && ipa_pass->write_optimization_summary
2751 && pass->gate (cfun))
2753 /* If a timevar is present, start it. */
2754 if (pass->tv_id)
2755 timevar_push (pass->tv_id);
2757 pass_init_dump_file (pass);
2759 current_pass = pass;
2760 ipa_pass->write_optimization_summary ();
2762 pass_fini_dump_file (pass);
2764 /* If a timevar is present, start it. */
2765 if (pass->tv_id)
2766 timevar_pop (pass->tv_id);
2769 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2770 ipa_write_optimization_summaries_1 (pass->sub, state);
2772 pass = pass->next;
2776 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2777 NULL, write out all summaries of all nodes. */
2779 void
2780 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2782 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2783 lto_symtab_encoder_iterator lsei;
2784 state->symtab_node_encoder = encoder;
2786 lto_output_init_mode_table ();
2787 lto_push_out_decl_state (state);
2788 for (lsei = lsei_start_function_in_partition (encoder);
2789 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2791 struct cgraph_node *node = lsei_cgraph_node (lsei);
2792 /* When streaming out references to statements as part of some IPA
2793 pass summary, the statements need to have uids assigned.
2795 For functions newly born at WPA stage we need to initialize
2796 the uids here. */
2797 if (node->definition
2798 && gimple_has_body_p (node->decl))
2800 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2801 renumber_gimple_stmt_uids ();
2802 pop_cfun ();
2806 gcc_assert (flag_wpa);
2807 pass_manager *passes = g->get_passes ();
2808 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2810 write_lto ();
2812 gcc_assert (lto_get_out_decl_state () == state);
2813 lto_pop_out_decl_state ();
2814 lto_delete_out_decl_state (state);
2817 /* Same as execute_pass_list but assume that subpasses of IPA passes
2818 are local passes. */
2820 static void
2821 ipa_read_summaries_1 (opt_pass *pass)
2823 while (pass)
2825 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2827 gcc_assert (!current_function_decl);
2828 gcc_assert (!cfun);
2829 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2831 if (pass->gate (cfun))
2833 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2835 /* If a timevar is present, start it. */
2836 if (pass->tv_id)
2837 timevar_push (pass->tv_id);
2839 pass_init_dump_file (pass);
2841 current_pass = pass;
2842 ipa_pass->read_summary ();
2844 pass_fini_dump_file (pass);
2846 /* Stop timevar. */
2847 if (pass->tv_id)
2848 timevar_pop (pass->tv_id);
2851 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2852 ipa_read_summaries_1 (pass->sub);
2854 pass = pass->next;
2859 /* Read all the summaries for all_regular_ipa_passes. */
2861 void
2862 ipa_read_summaries (void)
2864 pass_manager *passes = g->get_passes ();
2865 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2868 /* Same as execute_pass_list but assume that subpasses of IPA passes
2869 are local passes. */
2871 static void
2872 ipa_read_optimization_summaries_1 (opt_pass *pass)
2874 while (pass)
2876 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2878 gcc_assert (!current_function_decl);
2879 gcc_assert (!cfun);
2880 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2882 if (pass->gate (cfun))
2884 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2886 /* If a timevar is present, start it. */
2887 if (pass->tv_id)
2888 timevar_push (pass->tv_id);
2890 pass_init_dump_file (pass);
2892 current_pass = pass;
2893 ipa_pass->read_optimization_summary ();
2895 pass_fini_dump_file (pass);
2897 /* Stop timevar. */
2898 if (pass->tv_id)
2899 timevar_pop (pass->tv_id);
2902 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2903 ipa_read_optimization_summaries_1 (pass->sub);
2905 pass = pass->next;
2909 /* Read all the summaries for all_regular_ipa_passes. */
2911 void
2912 ipa_read_optimization_summaries (void)
2914 pass_manager *passes = g->get_passes ();
2915 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2918 /* Same as execute_pass_list but assume that subpasses of IPA passes
2919 are local passes. */
2920 void
2921 execute_ipa_pass_list (opt_pass *pass)
2925 gcc_assert (!current_function_decl);
2926 gcc_assert (!cfun);
2927 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2928 if (execute_one_pass (pass) && pass->sub)
2930 if (pass->sub->type == GIMPLE_PASS)
2932 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2933 do_per_function_toporder ((void (*)(function *, void *))
2934 execute_pass_list,
2935 pass->sub);
2936 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2938 else if (pass->sub->type == SIMPLE_IPA_PASS
2939 || pass->sub->type == IPA_PASS)
2940 execute_ipa_pass_list (pass->sub);
2941 else
2942 gcc_unreachable ();
2944 gcc_assert (!current_function_decl);
2945 symtab->process_new_functions ();
2946 pass = pass->next;
2948 while (pass);
2951 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2953 static void
2954 execute_ipa_stmt_fixups (opt_pass *pass,
2955 struct cgraph_node *node, gimple **stmts)
2957 while (pass)
2959 /* Execute all of the IPA_PASSes in the list. */
2960 if (pass->type == IPA_PASS
2961 && pass->gate (cfun))
2963 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2965 if (ipa_pass->stmt_fixup)
2967 pass_init_dump_file (pass);
2968 /* If a timevar is present, start it. */
2969 if (pass->tv_id)
2970 timevar_push (pass->tv_id);
2972 current_pass = pass;
2973 ipa_pass->stmt_fixup (node, stmts);
2975 /* Stop timevar. */
2976 if (pass->tv_id)
2977 timevar_pop (pass->tv_id);
2978 pass_fini_dump_file (pass);
2980 if (pass->sub)
2981 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2983 pass = pass->next;
2987 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2989 void
2990 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2992 pass_manager *passes = g->get_passes ();
2993 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2997 extern void debug_properties (unsigned int);
2998 extern void dump_properties (FILE *, unsigned int);
3000 DEBUG_FUNCTION void
3001 dump_properties (FILE *dump, unsigned int props)
3003 fprintf (dump, "Properties:\n");
3004 if (props & PROP_gimple_any)
3005 fprintf (dump, "PROP_gimple_any\n");
3006 if (props & PROP_gimple_lcf)
3007 fprintf (dump, "PROP_gimple_lcf\n");
3008 if (props & PROP_gimple_leh)
3009 fprintf (dump, "PROP_gimple_leh\n");
3010 if (props & PROP_cfg)
3011 fprintf (dump, "PROP_cfg\n");
3012 if (props & PROP_ssa)
3013 fprintf (dump, "PROP_ssa\n");
3014 if (props & PROP_no_crit_edges)
3015 fprintf (dump, "PROP_no_crit_edges\n");
3016 if (props & PROP_rtl)
3017 fprintf (dump, "PROP_rtl\n");
3018 if (props & PROP_gimple_lomp)
3019 fprintf (dump, "PROP_gimple_lomp\n");
3020 if (props & PROP_gimple_lomp_dev)
3021 fprintf (dump, "PROP_gimple_lomp_dev\n");
3022 if (props & PROP_gimple_lcx)
3023 fprintf (dump, "PROP_gimple_lcx\n");
3024 if (props & PROP_gimple_lvec)
3025 fprintf (dump, "PROP_gimple_lvec\n");
3026 if (props & PROP_cfglayout)
3027 fprintf (dump, "PROP_cfglayout\n");
3030 DEBUG_FUNCTION void
3031 debug_properties (unsigned int props)
3033 dump_properties (stderr, props);
3036 /* Called by local passes to see if function is called by already processed nodes.
3037 Because we process nodes in topological order, this means that function is
3038 in recursive cycle or we introduced new direct calls. */
3039 bool
3040 function_called_by_processed_nodes_p (void)
3042 struct cgraph_edge *e;
3043 for (e = cgraph_node::get (current_function_decl)->callers;
3045 e = e->next_caller)
3047 if (e->caller->decl == current_function_decl)
3048 continue;
3049 if (!e->caller->has_gimple_body_p ())
3050 continue;
3051 if (TREE_ASM_WRITTEN (e->caller->decl))
3052 continue;
3053 if (!e->caller->process && !e->caller->global.inlined_to)
3054 break;
3056 if (dump_file && e)
3058 fprintf (dump_file, "Already processed call to:\n");
3059 e->caller->dump (dump_file);
3061 return e != NULL;
3064 #include "gt-passes.h"