libgo: add misc/cgo files
[official-gcc.git] / gcc / passes.c
blob374f6f77897644de89be0748a203e772fae4294d
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 execute_function_dump (cfun, current_pass);
1802 static struct profile_record *profile_record;
1804 /* Do profile consistency book-keeping for the pass with static number INDEX.
1805 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1806 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1807 if we are only book-keeping on passes that may have selectively disabled
1808 themselves on a given function. */
1809 static void
1810 check_profile_consistency (int index, int subpass, bool run)
1812 pass_manager *passes = g->get_passes ();
1813 if (index == -1)
1814 return;
1815 if (!profile_record)
1816 profile_record = XCNEWVEC (struct profile_record,
1817 passes->passes_by_id_size);
1818 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1819 gcc_assert (subpass < 2);
1820 profile_record[index].run |= run;
1821 account_profile_record (&profile_record[index], subpass);
1824 /* Output profile consistency. */
1826 void
1827 dump_profile_report (void)
1829 g->get_passes ()->dump_profile_report ();
1832 void
1833 pass_manager::dump_profile_report () const
1835 int i, j;
1836 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1837 gcov_type last_time = 0, last_size = 0;
1838 double rel_time_change, rel_size_change;
1839 int last_reported = 0;
1841 if (!profile_record)
1842 return;
1843 fprintf (stderr, "\nProfile consistency report:\n\n");
1844 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1845 fprintf (stderr, " |freq count |freq count |size time\n");
1847 for (i = 0; i < passes_by_id_size; i++)
1848 for (j = 0 ; j < 2; j++)
1849 if (profile_record[i].run)
1851 if (last_time)
1852 rel_time_change = (profile_record[i].time[j]
1853 - (double)last_time) * 100 / (double)last_time;
1854 else
1855 rel_time_change = 0;
1856 if (last_size)
1857 rel_size_change = (profile_record[i].size[j]
1858 - (double)last_size) * 100 / (double)last_size;
1859 else
1860 rel_size_change = 0;
1862 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1863 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1864 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1865 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1866 || rel_time_change || rel_size_change)
1868 last_reported = i;
1869 fprintf (stderr, "%-20s %s",
1870 passes_by_id [i]->name,
1871 j ? "(after TODO)" : " ");
1872 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1873 fprintf (stderr, "| %+5i",
1874 profile_record[i].num_mismatched_freq_in[j]
1875 - last_freq_in);
1876 else
1877 fprintf (stderr, "| ");
1878 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1879 fprintf (stderr, " %+5i",
1880 profile_record[i].num_mismatched_count_in[j]
1881 - last_count_in);
1882 else
1883 fprintf (stderr, " ");
1884 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1885 fprintf (stderr, "| %+5i",
1886 profile_record[i].num_mismatched_freq_out[j]
1887 - last_freq_out);
1888 else
1889 fprintf (stderr, "| ");
1890 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1891 fprintf (stderr, " %+5i",
1892 profile_record[i].num_mismatched_count_out[j]
1893 - last_count_out);
1894 else
1895 fprintf (stderr, " ");
1897 /* Size/time units change across gimple and RTL. */
1898 if (i == pass_expand_1->static_pass_number)
1899 fprintf (stderr, "|----------");
1900 else
1902 if (rel_size_change)
1903 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1904 else
1905 fprintf (stderr, "| ");
1906 if (rel_time_change)
1907 fprintf (stderr, " %+8.4f%%", rel_time_change);
1909 fprintf (stderr, "\n");
1910 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1911 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1912 last_count_in = profile_record[i].num_mismatched_count_in[j];
1913 last_count_out = profile_record[i].num_mismatched_count_out[j];
1915 else if (j && last_reported != i)
1917 last_reported = i;
1918 fprintf (stderr, "%-20s ------------| | |\n",
1919 passes_by_id [i]->name);
1921 last_time = profile_record[i].time[j];
1922 last_size = profile_record[i].size[j];
1926 /* Perform all TODO actions that ought to be done on each function. */
1928 static void
1929 execute_function_todo (function *fn, void *data)
1931 bool from_ipa_pass = (cfun == NULL);
1932 unsigned int flags = (size_t)data;
1933 flags &= ~fn->last_verified;
1934 if (!flags)
1935 return;
1937 push_cfun (fn);
1939 /* Always cleanup the CFG before trying to update SSA. */
1940 if (flags & TODO_cleanup_cfg)
1942 cleanup_tree_cfg ();
1944 /* When cleanup_tree_cfg merges consecutive blocks, it may
1945 perform some simplistic propagation when removing single
1946 valued PHI nodes. This propagation may, in turn, cause the
1947 SSA form to become out-of-date (see PR 22037). So, even
1948 if the parent pass had not scheduled an SSA update, we may
1949 still need to do one. */
1950 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1951 flags |= TODO_update_ssa;
1954 if (flags & TODO_update_ssa_any)
1956 unsigned update_flags = flags & TODO_update_ssa_any;
1957 update_ssa (update_flags);
1960 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1961 compute_may_aliases ();
1963 if (optimize && (flags & TODO_update_address_taken))
1964 execute_update_addresses_taken ();
1966 if (flags & TODO_remove_unused_locals)
1967 remove_unused_locals ();
1969 if (flags & TODO_rebuild_frequencies)
1970 rebuild_frequencies ();
1972 if (flags & TODO_rebuild_cgraph_edges)
1973 cgraph_edge::rebuild_edges ();
1975 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1976 /* If we've seen errors do not bother running any verifiers. */
1977 if (flag_checking && !seen_error ())
1979 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1980 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1982 if (flags & TODO_verify_il)
1984 if (cfun->curr_properties & PROP_trees)
1986 if (cfun->curr_properties & PROP_cfg)
1987 /* IPA passes leave stmts to be fixed up, so make sure to
1988 not verify stmts really throw. */
1989 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1990 else
1991 verify_gimple_in_seq (gimple_body (cfun->decl));
1993 if (cfun->curr_properties & PROP_ssa)
1994 /* IPA passes leave stmts to be fixed up, so make sure to
1995 not verify SSA operands whose verifier will choke on that. */
1996 verify_ssa (true, !from_ipa_pass);
1997 /* IPA passes leave basic-blocks unsplit, so make sure to
1998 not trip on that. */
1999 if ((cfun->curr_properties & PROP_cfg)
2000 && !from_ipa_pass)
2001 verify_flow_info ();
2002 if (current_loops
2003 && ! loops_state_satisfies_p (LOOPS_NEED_FIXUP))
2005 verify_loop_structure ();
2006 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
2007 verify_loop_closed_ssa (false);
2009 if (cfun->curr_properties & PROP_rtl)
2010 verify_rtl_sharing ();
2013 /* Make sure verifiers don't change dominator state. */
2014 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
2015 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
2018 fn->last_verified = flags & TODO_verify_all;
2020 pop_cfun ();
2022 /* For IPA passes make sure to release dominator info, it can be
2023 computed by non-verifying TODOs. */
2024 if (from_ipa_pass)
2026 free_dominance_info (fn, CDI_DOMINATORS);
2027 free_dominance_info (fn, CDI_POST_DOMINATORS);
2031 /* Perform all TODO actions. */
2032 static void
2033 execute_todo (unsigned int flags)
2035 if (flag_checking
2036 && cfun
2037 && need_ssa_update_p (cfun))
2038 gcc_assert (flags & TODO_update_ssa_any);
2040 statistics_fini_pass ();
2042 if (flags)
2043 do_per_function (execute_function_todo, (void *)(size_t) flags);
2045 /* At this point we should not have any unreachable code in the
2046 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
2047 if (cfun && cfun->gimple_df)
2048 flush_ssaname_freelist ();
2050 /* Always remove functions just as before inlining: IPA passes might be
2051 interested to see bodies of extern inline functions that are not inlined
2052 to analyze side effects. The full removal is done just at the end
2053 of IPA pass queue. */
2054 if (flags & TODO_remove_functions)
2056 gcc_assert (!cfun);
2057 symtab->remove_unreachable_nodes (dump_file);
2060 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2062 gcc_assert (!cfun);
2063 symtab->dump (dump_file);
2064 /* Flush the file. If verification fails, we won't be able to
2065 close the file before aborting. */
2066 fflush (dump_file);
2069 /* Now that the dumping has been done, we can get rid of the optional
2070 df problems. */
2071 if (flags & TODO_df_finish)
2072 df_finish_pass ((flags & TODO_df_verify) != 0);
2075 /* Verify invariants that should hold between passes. This is a place
2076 to put simple sanity checks. */
2078 static void
2079 verify_interpass_invariants (void)
2081 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2084 /* Clear the last verified flag. */
2086 static void
2087 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2089 fn->last_verified = 0;
2092 /* Helper function. Verify that the properties has been turn into the
2093 properties expected by the pass. */
2095 static void
2096 verify_curr_properties (function *fn, void *data)
2098 unsigned int props = (size_t)data;
2099 gcc_assert ((fn->curr_properties & props) == props);
2102 /* Release dump file name if set. */
2104 static void
2105 release_dump_file_name (void)
2107 if (dump_file_name)
2109 free (CONST_CAST (char *, dump_file_name));
2110 dump_file_name = NULL;
2114 /* Initialize pass dump file. */
2115 /* This is non-static so that the plugins can use it. */
2117 bool
2118 pass_init_dump_file (opt_pass *pass)
2120 /* If a dump file name is present, open it if enabled. */
2121 if (pass->static_pass_number != -1)
2123 timevar_push (TV_DUMP);
2124 gcc::dump_manager *dumps = g->get_dumps ();
2125 bool initializing_dump =
2126 !dumps->dump_initialized_p (pass->static_pass_number);
2127 release_dump_file_name ();
2128 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2129 dumps->dump_start (pass->static_pass_number, &dump_flags);
2130 if (dump_file && current_function_decl && ! (dump_flags & TDF_GIMPLE))
2131 dump_function_header (dump_file, current_function_decl, dump_flags);
2132 if (initializing_dump
2133 && dump_file && (dump_flags & TDF_GRAPH)
2134 && cfun && (cfun->curr_properties & PROP_cfg))
2136 clean_graph_dump_file (dump_file_name);
2137 struct dump_file_info *dfi
2138 = dumps->get_dump_file_info (pass->static_pass_number);
2139 dfi->graph_dump_initialized = true;
2141 timevar_pop (TV_DUMP);
2142 return initializing_dump;
2144 else
2145 return false;
2148 /* Flush PASS dump file. */
2149 /* This is non-static so that plugins can use it. */
2151 void
2152 pass_fini_dump_file (opt_pass *pass)
2154 timevar_push (TV_DUMP);
2156 /* Flush and close dump file. */
2157 release_dump_file_name ();
2159 g->get_dumps ()->dump_finish (pass->static_pass_number);
2160 timevar_pop (TV_DUMP);
2163 /* After executing the pass, apply expected changes to the function
2164 properties. */
2166 static void
2167 update_properties_after_pass (function *fn, void *data)
2169 opt_pass *pass = (opt_pass *) data;
2170 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2171 & ~pass->properties_destroyed;
2174 /* Execute summary generation for all of the passes in IPA_PASS. */
2176 void
2177 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2179 while (ipa_pass)
2181 opt_pass *pass = ipa_pass;
2183 /* Execute all of the IPA_PASSes in the list. */
2184 if (ipa_pass->type == IPA_PASS
2185 && pass->gate (cfun)
2186 && ipa_pass->generate_summary)
2188 pass_init_dump_file (pass);
2190 /* If a timevar is present, start it. */
2191 if (pass->tv_id)
2192 timevar_push (pass->tv_id);
2194 current_pass = pass;
2195 ipa_pass->generate_summary ();
2197 /* Stop timevar. */
2198 if (pass->tv_id)
2199 timevar_pop (pass->tv_id);
2201 pass_fini_dump_file (pass);
2203 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2207 /* Execute IPA_PASS function transform on NODE. */
2209 static void
2210 execute_one_ipa_transform_pass (struct cgraph_node *node,
2211 ipa_opt_pass_d *ipa_pass)
2213 opt_pass *pass = ipa_pass;
2214 unsigned int todo_after = 0;
2216 current_pass = pass;
2217 if (!ipa_pass->function_transform)
2218 return;
2220 /* Note that the folders should only create gimple expressions.
2221 This is a hack until the new folder is ready. */
2222 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2224 pass_init_dump_file (pass);
2226 /* If a timevar is present, start it. */
2227 if (pass->tv_id != TV_NONE)
2228 timevar_push (pass->tv_id);
2230 /* Run pre-pass verification. */
2231 execute_todo (ipa_pass->function_transform_todo_flags_start);
2233 /* Do it! */
2234 todo_after = ipa_pass->function_transform (node);
2236 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2237 check_profile_consistency (pass->static_pass_number, 0, true);
2239 /* Run post-pass cleanup and verification. */
2240 execute_todo (todo_after);
2241 verify_interpass_invariants ();
2242 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2243 check_profile_consistency (pass->static_pass_number, 1, true);
2245 /* Stop timevar. */
2246 if (pass->tv_id != TV_NONE)
2247 timevar_pop (pass->tv_id);
2249 if (dump_file)
2250 do_per_function (execute_function_dump, pass);
2251 pass_fini_dump_file (pass);
2253 current_pass = NULL;
2254 redirect_edge_var_map_empty ();
2256 /* Signal this is a suitable GC collection point. */
2257 if (!(todo_after & TODO_do_not_ggc_collect))
2258 ggc_collect ();
2261 /* For the current function, execute all ipa transforms. */
2263 void
2264 execute_all_ipa_transforms (void)
2266 struct cgraph_node *node;
2267 if (!cfun)
2268 return;
2269 node = cgraph_node::get (current_function_decl);
2271 if (node->ipa_transforms_to_apply.exists ())
2273 unsigned int i;
2275 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2276 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2277 node->ipa_transforms_to_apply.release ();
2281 /* Check if PASS is explicitly disabled or enabled and return
2282 the gate status. FUNC is the function to be processed, and
2283 GATE_STATUS is the gate status determined by pass manager by
2284 default. */
2286 static bool
2287 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2289 bool explicitly_enabled = false;
2290 bool explicitly_disabled = false;
2292 explicitly_enabled
2293 = is_pass_explicitly_enabled_or_disabled (pass, func,
2294 enabled_pass_uid_range_tab);
2295 explicitly_disabled
2296 = is_pass_explicitly_enabled_or_disabled (pass, func,
2297 disabled_pass_uid_range_tab);
2299 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2301 return gate_status;
2304 /* Determine if PASS_NAME matches CRITERION.
2305 Not a pure predicate, since it can update CRITERION, to support
2306 matching the Nth invocation of a pass.
2307 Subroutine of should_skip_pass_p. */
2309 static bool
2310 determine_pass_name_match (const char *pass_name, char *criterion)
2312 size_t namelen = strlen (pass_name);
2313 if (! strncmp (pass_name, criterion, namelen))
2315 /* The following supports starting with the Nth invocation
2316 of a pass (where N does not necessarily is equal to the
2317 dump file suffix). */
2318 if (criterion[namelen] == '\0'
2319 || (criterion[namelen] == '1'
2320 && criterion[namelen + 1] == '\0'))
2321 return true;
2322 else
2324 if (criterion[namelen + 1] == '\0')
2325 --criterion[namelen];
2326 return false;
2329 else
2330 return false;
2333 /* For skipping passes until "startwith" pass.
2334 Return true iff PASS should be skipped.
2335 Clear cfun->pass_startwith when encountering the "startwith" pass,
2336 so that all subsequent passes are run. */
2338 static bool
2339 should_skip_pass_p (opt_pass *pass)
2341 if (!cfun)
2342 return false;
2343 if (!cfun->pass_startwith)
2344 return false;
2346 /* For __GIMPLE functions, we have to at least start when we leave
2347 SSA. Hence, we need to detect the "expand" pass, and stop skipping
2348 when we encounter it. A cheap way to identify "expand" is it to
2349 detect the destruction of PROP_ssa.
2350 For __RTL functions, we invoke "rest_of_compilation" directly, which
2351 is after "expand", and hence we don't reach this conditional. */
2352 if (pass->properties_destroyed & PROP_ssa)
2354 if (!quiet_flag)
2355 fprintf (stderr, "starting anyway when leaving SSA: %s\n", pass->name);
2356 cfun->pass_startwith = NULL;
2357 return false;
2360 if (determine_pass_name_match (pass->name, cfun->pass_startwith))
2362 if (!quiet_flag)
2363 fprintf (stderr, "found starting pass: %s\n", pass->name);
2364 cfun->pass_startwith = NULL;
2365 return false;
2368 /* For GIMPLE passes, run any property provider (but continue skipping
2369 afterwards).
2370 We don't want to force running RTL passes that are property providers:
2371 "expand" is covered above, and the only pass other than "expand" that
2372 provides a property is "into_cfglayout" (PROP_cfglayout), which does
2373 too much for a dumped __RTL function. */
2374 if (pass->type == GIMPLE_PASS
2375 && pass->properties_provided != 0)
2376 return false;
2378 /* Don't skip df init; later RTL passes need it. */
2379 if (strstr (pass->name, "dfinit") != NULL)
2380 return false;
2382 if (!quiet_flag)
2383 fprintf (stderr, "skipping pass: %s\n", pass->name);
2385 /* If we get here, then we have a "startwith" that we haven't seen yet;
2386 skip the pass. */
2387 return true;
2390 /* Skip the given pass, for handling passes before "startwith"
2391 in __GIMPLE and__RTL-marked functions.
2392 In theory, this ought to be a no-op, but some of the RTL passes
2393 need additional processing here. */
2395 static void
2396 skip_pass (opt_pass *pass)
2398 /* Pass "reload" sets the global "reload_completed", and many
2399 things depend on this (e.g. instructions in .md files). */
2400 if (strcmp (pass->name, "reload") == 0)
2401 reload_completed = 1;
2403 /* The INSN_ADDRESSES vec is normally set up by
2404 shorten_branches; set it up for the benefit of passes that
2405 run after this. */
2406 if (strcmp (pass->name, "shorten") == 0)
2407 INSN_ADDRESSES_ALLOC (get_max_uid ());
2409 /* Update the cfg hooks as appropriate. */
2410 if (strcmp (pass->name, "into_cfglayout") == 0)
2412 cfg_layout_rtl_register_cfg_hooks ();
2413 cfun->curr_properties |= PROP_cfglayout;
2415 if (strcmp (pass->name, "outof_cfglayout") == 0)
2417 rtl_register_cfg_hooks ();
2418 cfun->curr_properties &= ~PROP_cfglayout;
2422 /* Execute PASS. */
2424 bool
2425 execute_one_pass (opt_pass *pass)
2427 unsigned int todo_after = 0;
2429 bool gate_status;
2431 /* IPA passes are executed on whole program, so cfun should be NULL.
2432 Other passes need function context set. */
2433 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2434 gcc_assert (!cfun && !current_function_decl);
2435 else
2436 gcc_assert (cfun && current_function_decl);
2438 current_pass = pass;
2440 /* Check whether gate check should be avoided.
2441 User controls the value of the gate through the parameter "gate_status". */
2442 gate_status = pass->gate (cfun);
2443 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2445 /* Override gate with plugin. */
2446 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2448 if (!gate_status)
2450 /* Run so passes selectively disabling themselves on a given function
2451 are not miscounted. */
2452 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2454 check_profile_consistency (pass->static_pass_number, 0, false);
2455 check_profile_consistency (pass->static_pass_number, 1, false);
2457 current_pass = NULL;
2458 return false;
2461 if (should_skip_pass_p (pass))
2463 skip_pass (pass);
2464 return true;
2467 /* Pass execution event trigger: useful to identify passes being
2468 executed. */
2469 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2471 if (!quiet_flag && !cfun)
2472 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2474 /* Note that the folders should only create gimple expressions.
2475 This is a hack until the new folder is ready. */
2476 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2478 pass_init_dump_file (pass);
2480 /* If a timevar is present, start it. */
2481 if (pass->tv_id != TV_NONE)
2482 timevar_push (pass->tv_id);
2484 /* Run pre-pass verification. */
2485 execute_todo (pass->todo_flags_start);
2487 if (flag_checking)
2488 do_per_function (verify_curr_properties,
2489 (void *)(size_t)pass->properties_required);
2491 /* Do it! */
2492 todo_after = pass->execute (cfun);
2494 if (todo_after & TODO_discard_function)
2496 /* Stop timevar. */
2497 if (pass->tv_id != TV_NONE)
2498 timevar_pop (pass->tv_id);
2500 pass_fini_dump_file (pass);
2502 gcc_assert (cfun);
2503 /* As cgraph_node::release_body expects release dominators info,
2504 we have to release it. */
2505 if (dom_info_available_p (CDI_DOMINATORS))
2506 free_dominance_info (CDI_DOMINATORS);
2508 if (dom_info_available_p (CDI_POST_DOMINATORS))
2509 free_dominance_info (CDI_POST_DOMINATORS);
2511 tree fn = cfun->decl;
2512 pop_cfun ();
2513 gcc_assert (!cfun);
2514 cgraph_node::get (fn)->release_body ();
2516 current_pass = NULL;
2517 redirect_edge_var_map_empty ();
2519 ggc_collect ();
2521 return true;
2524 do_per_function (clear_last_verified, NULL);
2526 do_per_function (update_properties_after_pass, pass);
2528 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2529 check_profile_consistency (pass->static_pass_number, 0, true);
2531 /* Run post-pass cleanup and verification. */
2532 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2533 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2534 check_profile_consistency (pass->static_pass_number, 1, true);
2536 verify_interpass_invariants ();
2538 /* Stop timevar. */
2539 if (pass->tv_id != TV_NONE)
2540 timevar_pop (pass->tv_id);
2542 if (pass->type == IPA_PASS
2543 && ((ipa_opt_pass_d *)pass)->function_transform)
2545 struct cgraph_node *node;
2546 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2547 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2549 else if (dump_file)
2550 do_per_function (execute_function_dump, pass);
2552 if (!current_function_decl)
2553 symtab->process_new_functions ();
2555 pass_fini_dump_file (pass);
2557 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2558 gcc_assert (!(cfun->curr_properties & PROP_trees)
2559 || pass->type != RTL_PASS);
2561 current_pass = NULL;
2562 redirect_edge_var_map_empty ();
2564 /* Signal this is a suitable GC collection point. */
2565 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2566 ggc_collect ();
2568 return true;
2571 static void
2572 execute_pass_list_1 (opt_pass *pass)
2576 gcc_assert (pass->type == GIMPLE_PASS
2577 || pass->type == RTL_PASS);
2579 if (cfun == NULL)
2580 return;
2581 if (execute_one_pass (pass) && pass->sub)
2582 execute_pass_list_1 (pass->sub);
2583 pass = pass->next;
2585 while (pass);
2588 void
2589 execute_pass_list (function *fn, opt_pass *pass)
2591 gcc_assert (fn == cfun);
2592 execute_pass_list_1 (pass);
2593 if (cfun && fn->cfg)
2595 free_dominance_info (CDI_DOMINATORS);
2596 free_dominance_info (CDI_POST_DOMINATORS);
2600 /* Write out all LTO data. */
2601 static void
2602 write_lto (void)
2604 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2605 lto_output ();
2606 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2607 timevar_push (TV_IPA_LTO_DECL_OUT);
2608 produce_asm_for_decls ();
2609 timevar_pop (TV_IPA_LTO_DECL_OUT);
2612 /* Same as execute_pass_list but assume that subpasses of IPA passes
2613 are local passes. If SET is not NULL, write out summaries of only
2614 those node in SET. */
2616 static void
2617 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2619 while (pass)
2621 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2622 gcc_assert (!current_function_decl);
2623 gcc_assert (!cfun);
2624 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2625 if (pass->type == IPA_PASS
2626 && ipa_pass->write_summary
2627 && pass->gate (cfun))
2629 /* If a timevar is present, start it. */
2630 if (pass->tv_id)
2631 timevar_push (pass->tv_id);
2633 pass_init_dump_file (pass);
2635 current_pass = pass;
2636 ipa_pass->write_summary ();
2638 pass_fini_dump_file (pass);
2640 /* If a timevar is present, start it. */
2641 if (pass->tv_id)
2642 timevar_pop (pass->tv_id);
2645 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2646 ipa_write_summaries_2 (pass->sub, state);
2648 pass = pass->next;
2652 /* Helper function of ipa_write_summaries. Creates and destroys the
2653 decl state and calls ipa_write_summaries_2 for all passes that have
2654 summaries. SET is the set of nodes to be written. */
2656 static void
2657 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2659 pass_manager *passes = g->get_passes ();
2660 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2661 state->symtab_node_encoder = encoder;
2663 lto_output_init_mode_table ();
2664 lto_push_out_decl_state (state);
2666 gcc_assert (!flag_wpa);
2667 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2669 write_lto ();
2671 gcc_assert (lto_get_out_decl_state () == state);
2672 lto_pop_out_decl_state ();
2673 lto_delete_out_decl_state (state);
2676 /* Write out summaries for all the nodes in the callgraph. */
2678 void
2679 ipa_write_summaries (void)
2681 lto_symtab_encoder_t encoder;
2682 int i, order_pos;
2683 varpool_node *vnode;
2684 struct cgraph_node *node;
2685 struct cgraph_node **order;
2687 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2688 return;
2690 select_what_to_stream ();
2692 encoder = lto_symtab_encoder_new (false);
2694 /* Create the callgraph set in the same order used in
2695 cgraph_expand_all_functions. This mostly facilitates debugging,
2696 since it causes the gimple file to be processed in the same order
2697 as the source code. */
2698 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2699 order_pos = ipa_reverse_postorder (order);
2700 gcc_assert (order_pos == symtab->cgraph_count);
2702 for (i = order_pos - 1; i >= 0; i--)
2704 struct cgraph_node *node = order[i];
2706 if (node->has_gimple_body_p ())
2708 /* When streaming out references to statements as part of some IPA
2709 pass summary, the statements need to have uids assigned and the
2710 following does that for all the IPA passes here. Naturally, this
2711 ordering then matches the one IPA-passes get in their stmt_fixup
2712 hooks. */
2714 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2715 renumber_gimple_stmt_uids ();
2716 pop_cfun ();
2718 if (node->definition && node->need_lto_streaming)
2719 lto_set_symtab_encoder_in_partition (encoder, node);
2722 FOR_EACH_DEFINED_FUNCTION (node)
2723 if (node->alias && node->need_lto_streaming)
2724 lto_set_symtab_encoder_in_partition (encoder, node);
2725 FOR_EACH_DEFINED_VARIABLE (vnode)
2726 if (vnode->need_lto_streaming)
2727 lto_set_symtab_encoder_in_partition (encoder, vnode);
2729 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2731 free (order);
2734 /* Same as execute_pass_list but assume that subpasses of IPA passes
2735 are local passes. If SET is not NULL, write out optimization summaries of
2736 only those node in SET. */
2738 static void
2739 ipa_write_optimization_summaries_1 (opt_pass *pass,
2740 struct lto_out_decl_state *state)
2742 while (pass)
2744 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2745 gcc_assert (!current_function_decl);
2746 gcc_assert (!cfun);
2747 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2748 if (pass->type == IPA_PASS
2749 && ipa_pass->write_optimization_summary
2750 && pass->gate (cfun))
2752 /* If a timevar is present, start it. */
2753 if (pass->tv_id)
2754 timevar_push (pass->tv_id);
2756 pass_init_dump_file (pass);
2758 current_pass = pass;
2759 ipa_pass->write_optimization_summary ();
2761 pass_fini_dump_file (pass);
2763 /* If a timevar is present, start it. */
2764 if (pass->tv_id)
2765 timevar_pop (pass->tv_id);
2768 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2769 ipa_write_optimization_summaries_1 (pass->sub, state);
2771 pass = pass->next;
2775 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2776 NULL, write out all summaries of all nodes. */
2778 void
2779 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2781 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2782 lto_symtab_encoder_iterator lsei;
2783 state->symtab_node_encoder = encoder;
2785 lto_output_init_mode_table ();
2786 lto_push_out_decl_state (state);
2787 for (lsei = lsei_start_function_in_partition (encoder);
2788 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2790 struct cgraph_node *node = lsei_cgraph_node (lsei);
2791 /* When streaming out references to statements as part of some IPA
2792 pass summary, the statements need to have uids assigned.
2794 For functions newly born at WPA stage we need to initialize
2795 the uids here. */
2796 if (node->definition
2797 && gimple_has_body_p (node->decl))
2799 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2800 renumber_gimple_stmt_uids ();
2801 pop_cfun ();
2805 gcc_assert (flag_wpa);
2806 pass_manager *passes = g->get_passes ();
2807 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2809 write_lto ();
2811 gcc_assert (lto_get_out_decl_state () == state);
2812 lto_pop_out_decl_state ();
2813 lto_delete_out_decl_state (state);
2816 /* Same as execute_pass_list but assume that subpasses of IPA passes
2817 are local passes. */
2819 static void
2820 ipa_read_summaries_1 (opt_pass *pass)
2822 while (pass)
2824 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2826 gcc_assert (!current_function_decl);
2827 gcc_assert (!cfun);
2828 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2830 if (pass->gate (cfun))
2832 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2834 /* If a timevar is present, start it. */
2835 if (pass->tv_id)
2836 timevar_push (pass->tv_id);
2838 pass_init_dump_file (pass);
2840 current_pass = pass;
2841 ipa_pass->read_summary ();
2843 pass_fini_dump_file (pass);
2845 /* Stop timevar. */
2846 if (pass->tv_id)
2847 timevar_pop (pass->tv_id);
2850 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2851 ipa_read_summaries_1 (pass->sub);
2853 pass = pass->next;
2858 /* Read all the summaries for all_regular_ipa_passes. */
2860 void
2861 ipa_read_summaries (void)
2863 pass_manager *passes = g->get_passes ();
2864 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2867 /* Same as execute_pass_list but assume that subpasses of IPA passes
2868 are local passes. */
2870 static void
2871 ipa_read_optimization_summaries_1 (opt_pass *pass)
2873 while (pass)
2875 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2877 gcc_assert (!current_function_decl);
2878 gcc_assert (!cfun);
2879 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2881 if (pass->gate (cfun))
2883 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2885 /* If a timevar is present, start it. */
2886 if (pass->tv_id)
2887 timevar_push (pass->tv_id);
2889 pass_init_dump_file (pass);
2891 current_pass = pass;
2892 ipa_pass->read_optimization_summary ();
2894 pass_fini_dump_file (pass);
2896 /* Stop timevar. */
2897 if (pass->tv_id)
2898 timevar_pop (pass->tv_id);
2901 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2902 ipa_read_optimization_summaries_1 (pass->sub);
2904 pass = pass->next;
2908 /* Read all the summaries for all_regular_ipa_passes. */
2910 void
2911 ipa_read_optimization_summaries (void)
2913 pass_manager *passes = g->get_passes ();
2914 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2917 /* Same as execute_pass_list but assume that subpasses of IPA passes
2918 are local passes. */
2919 void
2920 execute_ipa_pass_list (opt_pass *pass)
2924 gcc_assert (!current_function_decl);
2925 gcc_assert (!cfun);
2926 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2927 if (execute_one_pass (pass) && pass->sub)
2929 if (pass->sub->type == GIMPLE_PASS)
2931 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2932 do_per_function_toporder ((void (*)(function *, void *))
2933 execute_pass_list,
2934 pass->sub);
2935 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2937 else if (pass->sub->type == SIMPLE_IPA_PASS
2938 || pass->sub->type == IPA_PASS)
2939 execute_ipa_pass_list (pass->sub);
2940 else
2941 gcc_unreachable ();
2943 gcc_assert (!current_function_decl);
2944 symtab->process_new_functions ();
2945 pass = pass->next;
2947 while (pass);
2950 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2952 static void
2953 execute_ipa_stmt_fixups (opt_pass *pass,
2954 struct cgraph_node *node, gimple **stmts)
2956 while (pass)
2958 /* Execute all of the IPA_PASSes in the list. */
2959 if (pass->type == IPA_PASS
2960 && pass->gate (cfun))
2962 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2964 if (ipa_pass->stmt_fixup)
2966 pass_init_dump_file (pass);
2967 /* If a timevar is present, start it. */
2968 if (pass->tv_id)
2969 timevar_push (pass->tv_id);
2971 current_pass = pass;
2972 ipa_pass->stmt_fixup (node, stmts);
2974 /* Stop timevar. */
2975 if (pass->tv_id)
2976 timevar_pop (pass->tv_id);
2977 pass_fini_dump_file (pass);
2979 if (pass->sub)
2980 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2982 pass = pass->next;
2986 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2988 void
2989 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2991 pass_manager *passes = g->get_passes ();
2992 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2996 extern void debug_properties (unsigned int);
2997 extern void dump_properties (FILE *, unsigned int);
2999 DEBUG_FUNCTION void
3000 dump_properties (FILE *dump, unsigned int props)
3002 fprintf (dump, "Properties:\n");
3003 if (props & PROP_gimple_any)
3004 fprintf (dump, "PROP_gimple_any\n");
3005 if (props & PROP_gimple_lcf)
3006 fprintf (dump, "PROP_gimple_lcf\n");
3007 if (props & PROP_gimple_leh)
3008 fprintf (dump, "PROP_gimple_leh\n");
3009 if (props & PROP_cfg)
3010 fprintf (dump, "PROP_cfg\n");
3011 if (props & PROP_ssa)
3012 fprintf (dump, "PROP_ssa\n");
3013 if (props & PROP_no_crit_edges)
3014 fprintf (dump, "PROP_no_crit_edges\n");
3015 if (props & PROP_rtl)
3016 fprintf (dump, "PROP_rtl\n");
3017 if (props & PROP_gimple_lomp)
3018 fprintf (dump, "PROP_gimple_lomp\n");
3019 if (props & PROP_gimple_lomp_dev)
3020 fprintf (dump, "PROP_gimple_lomp_dev\n");
3021 if (props & PROP_gimple_lcx)
3022 fprintf (dump, "PROP_gimple_lcx\n");
3023 if (props & PROP_gimple_lvec)
3024 fprintf (dump, "PROP_gimple_lvec\n");
3025 if (props & PROP_cfglayout)
3026 fprintf (dump, "PROP_cfglayout\n");
3029 DEBUG_FUNCTION void
3030 debug_properties (unsigned int props)
3032 dump_properties (stderr, props);
3035 /* Called by local passes to see if function is called by already processed nodes.
3036 Because we process nodes in topological order, this means that function is
3037 in recursive cycle or we introduced new direct calls. */
3038 bool
3039 function_called_by_processed_nodes_p (void)
3041 struct cgraph_edge *e;
3042 for (e = cgraph_node::get (current_function_decl)->callers;
3044 e = e->next_caller)
3046 if (e->caller->decl == current_function_decl)
3047 continue;
3048 if (!e->caller->has_gimple_body_p ())
3049 continue;
3050 if (TREE_ASM_WRITTEN (e->caller->decl))
3051 continue;
3052 if (!e->caller->process && !e->caller->global.inlined_to)
3053 break;
3055 if (dump_file && e)
3057 fprintf (dump_file, "Already processed call to:\n");
3058 e->caller->dump (dump_file);
3060 return e != NULL;
3063 #include "gt-passes.h"