PR go/67101
[official-gcc.git] / gcc / passes.c
blobd8e9271fa89b2922b06632a8513a17d5849fc685
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2015 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 "cfghooks.h"
30 #include "tree.h"
31 #include "gimple.h"
32 #include "rtl.h"
33 #include "df.h"
34 #include "ssa.h"
35 #include "alias.h"
36 #include "fold-const.h"
37 #include "varasm.h"
38 #include "tm_p.h"
39 #include "flags.h"
40 #include "insn-attr.h"
41 #include "insn-config.h"
42 #include "insn-flags.h"
43 #include "recog.h"
44 #include "output.h"
45 #include "except.h"
46 #include "toplev.h"
47 #include "expmed.h"
48 #include "dojump.h"
49 #include "explow.h"
50 #include "calls.h"
51 #include "emit-rtl.h"
52 #include "stmt.h"
53 #include "expr.h"
54 #include "intl.h"
55 #include "graph.h"
56 #include "regs.h"
57 #include "diagnostic-core.h"
58 #include "params.h"
59 #include "reload.h"
60 #include "debug.h"
61 #include "target.h"
62 #include "langhooks.h"
63 #include "cfgloop.h"
64 #include "hosthooks.h"
65 #include "opts.h"
66 #include "coverage.h"
67 #include "value-prof.h"
68 #include "tree-inline.h"
69 #include "internal-fn.h"
70 #include "tree-cfg.h"
71 #include "tree-ssa-loop-manip.h"
72 #include "tree-into-ssa.h"
73 #include "tree-dfa.h"
74 #include "tree-ssa.h"
75 #include "tree-pass.h"
76 #include "tree-dump.h"
77 #include "cgraph.h"
78 #include "lto-streamer.h"
79 #include "plugin.h"
80 #include "ipa-utils.h"
81 #include "tree-pretty-print.h" /* for dump_function_header */
82 #include "context.h"
83 #include "pass_manager.h"
84 #include "cfgrtl.h"
85 #include "tree-ssa-live.h" /* For remove_unused_locals. */
86 #include "tree-cfgcleanup.h"
88 using namespace gcc;
90 /* This is used for debugging. It allows the current pass to printed
91 from anywhere in compilation.
92 The variable current_pass is also used for statistics and plugins. */
93 opt_pass *current_pass;
95 static void register_pass_name (opt_pass *, const char *);
97 /* Most passes are single-instance (within their context) and thus don't
98 need to implement cloning, but passes that support multiple instances
99 *must* provide their own implementation of the clone method.
101 Handle this by providing a default implemenation, but make it a fatal
102 error to call it. */
104 opt_pass *
105 opt_pass::clone ()
107 internal_error ("pass %s does not support cloning", name);
110 bool
111 opt_pass::gate (function *)
113 return true;
116 unsigned int
117 opt_pass::execute (function *)
119 return 0;
122 opt_pass::opt_pass (const pass_data &data, context *ctxt)
123 : pass_data (data),
124 sub (NULL),
125 next (NULL),
126 static_pass_number (0),
127 m_ctxt (ctxt)
132 void
133 pass_manager::execute_early_local_passes ()
135 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
136 if (flag_check_pointer_bounds)
137 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
138 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
141 unsigned int
142 pass_manager::execute_pass_mode_switching ()
144 return pass_mode_switching_1->execute (cfun);
148 /* Call from anywhere to find out what pass this is. Useful for
149 printing out debugging information deep inside an service
150 routine. */
151 void
152 print_current_pass (FILE *file)
154 if (current_pass)
155 fprintf (file, "current pass = %s (%d)\n",
156 current_pass->name, current_pass->static_pass_number);
157 else
158 fprintf (file, "no current pass.\n");
162 /* Call from the debugger to get the current pass name. */
163 DEBUG_FUNCTION void
164 debug_pass (void)
166 print_current_pass (stderr);
171 /* Global variables used to communicate with passes. */
172 bool in_gimple_form;
173 bool first_pass_instance;
176 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
177 and TYPE_DECL nodes.
179 This does nothing for local (non-static) variables, unless the
180 variable is a register variable with DECL_ASSEMBLER_NAME set. In
181 that case, or if the variable is not an automatic, it sets up the
182 RTL and outputs any assembler code (label definition, storage
183 allocation and initialization).
185 DECL is the declaration. TOP_LEVEL is nonzero
186 if this declaration is not within a function. */
188 void
189 rest_of_decl_compilation (tree decl,
190 int top_level,
191 int at_end)
193 bool finalize = true;
195 /* We deferred calling assemble_alias so that we could collect
196 other attributes such as visibility. Emit the alias now. */
197 if (!in_lto_p)
199 tree alias;
200 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
201 if (alias)
203 alias = TREE_VALUE (TREE_VALUE (alias));
204 alias = get_identifier (TREE_STRING_POINTER (alias));
205 /* A quirk of the initial implementation of aliases required that the
206 user add "extern" to all of them. Which is silly, but now
207 historical. Do note that the symbol is in fact locally defined. */
208 DECL_EXTERNAL (decl) = 0;
209 TREE_STATIC (decl) = 1;
210 assemble_alias (decl, alias);
211 finalize = false;
215 /* Can't defer this, because it needs to happen before any
216 later function definitions are processed. */
217 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
218 make_decl_rtl (decl);
220 /* Forward declarations for nested functions are not "external",
221 but we need to treat them as if they were. */
222 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
223 || TREE_CODE (decl) == FUNCTION_DECL)
225 timevar_push (TV_VARCONST);
227 /* Don't output anything when a tentative file-scope definition
228 is seen. But at end of compilation, do output code for them.
230 We do output all variables and rely on
231 callgraph code to defer them except for forward declarations
232 (see gcc.c-torture/compile/920624-1.c) */
233 if ((at_end
234 || !DECL_DEFER_OUTPUT (decl)
235 || DECL_INITIAL (decl))
236 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
237 && !DECL_EXTERNAL (decl))
239 /* When reading LTO unit, we also read varpool, so do not
240 rebuild it. */
241 if (in_lto_p && !at_end)
243 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
244 varpool_node::finalize_decl (decl);
247 #ifdef ASM_FINISH_DECLARE_OBJECT
248 if (decl == last_assemble_variable_decl)
250 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
251 top_level, at_end);
253 #endif
255 timevar_pop (TV_VARCONST);
257 else if (TREE_CODE (decl) == TYPE_DECL
258 /* Like in rest_of_type_compilation, avoid confusing the debug
259 information machinery when there are errors. */
260 && !seen_error ())
262 timevar_push (TV_SYMOUT);
263 debug_hooks->type_decl (decl, !top_level);
264 timevar_pop (TV_SYMOUT);
267 /* Let cgraph know about the existence of variables. */
268 if (in_lto_p && !at_end)
270 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
271 && TREE_STATIC (decl))
272 varpool_node::get_create (decl);
274 /* Generate early debug for global variables. Any local variables will
275 be handled by either handling reachable functions from
276 finalize_compilation_unit (and by consequence, locally scoped
277 symbols), or by rest_of_type_compilation below.
279 Also, pick up function prototypes, which will be mostly ignored
280 by the different early_global_decl() hooks, but will at least be
281 used by Go's hijack of the debug_hooks to implement
282 -fdump-go-spec. */
283 if (!in_lto_p
284 && (TREE_CODE (decl) != FUNCTION_DECL
285 /* This will pick up function prototypes with no bodies,
286 which are not visible in finalize_compilation_unit()
287 while iterating with FOR_EACH_*_FUNCTION through the
288 symbol table. */
289 || !DECL_SAVED_TREE (decl))
291 /* We need to check both decl_function_context and
292 current_function_decl here to make sure local extern
293 declarations end up with the correct context.
295 For local extern declarations, decl_function_context is
296 empty, but current_function_decl is set to the function where
297 the extern was declared . Without the check for
298 !current_function_decl below, the local extern ends up
299 incorrectly with a top-level context.
301 For example:
303 namespace S
309 int i = 42;
311 extern int i; // Local extern declaration.
312 return i;
318 && !decl_function_context (decl)
319 && !current_function_decl
320 && DECL_SOURCE_LOCATION (decl) != BUILTINS_LOCATION
321 && !decl_type_context (decl))
322 (*debug_hooks->early_global_decl) (decl);
325 /* Called after finishing a record, union or enumeral type. */
327 void
328 rest_of_type_compilation (tree type, int toplev)
330 /* Avoid confusing the debug information machinery when there are
331 errors. */
332 if (seen_error ())
333 return;
335 timevar_push (TV_SYMOUT);
336 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
337 timevar_pop (TV_SYMOUT);
342 void
343 pass_manager::
344 finish_optimization_passes (void)
346 int i;
347 struct dump_file_info *dfi;
348 char *name;
349 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
351 timevar_push (TV_DUMP);
352 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
354 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
355 end_branch_prob ();
356 dumps->dump_finish (pass_profile_1->static_pass_number);
359 if (optimize > 0)
361 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
362 print_combine_total_stats ();
363 dumps->dump_finish (pass_profile_1->static_pass_number);
366 /* Do whatever is necessary to finish printing the graphs. */
367 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
368 if (dumps->dump_initialized_p (i)
369 && (dfi->pflags & TDF_GRAPH) != 0
370 && (name = dumps->get_dump_file_name (i)) != NULL)
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;
778 char num[10];
779 int flags, id;
780 int optgroup_flags = OPTGROUP_NONE;
781 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
783 /* See below in next_pass_1. */
784 num[0] = '\0';
785 if (pass->static_pass_number != -1)
786 sprintf (num, "%d", ((int) pass->static_pass_number < 0
787 ? 1 : pass->static_pass_number));
789 /* The name is both used to identify the pass for the purposes of plugins,
790 and to specify dump file name and option.
791 The latter two might want something short which is not quite unique; for
792 that reason, we may have a disambiguating prefix, followed by a space
793 to mark the start of the following dump file name / option string. */
794 name = strchr (pass->name, ' ');
795 name = name ? name + 1 : pass->name;
796 dot_name = concat (".", name, num, NULL);
797 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
799 prefix = "ipa-";
800 flags = TDF_IPA;
801 optgroup_flags |= OPTGROUP_IPA;
803 else if (pass->type == GIMPLE_PASS)
805 prefix = "tree-";
806 flags = TDF_TREE;
808 else
810 prefix = "rtl-";
811 flags = TDF_RTL;
814 flag_name = concat (prefix, name, num, NULL);
815 glob_name = concat (prefix, name, NULL);
816 optgroup_flags |= pass->optinfo_flags;
817 /* For any passes that do not have an optgroup set, and which are not
818 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
819 any dump messages are emitted properly under -fopt-info(-optall). */
820 if (optgroup_flags == OPTGROUP_NONE)
821 optgroup_flags = OPTGROUP_OTHER;
822 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
823 optgroup_flags,
824 true);
825 set_pass_for_id (id, pass);
826 full_name = concat (prefix, pass->name, num, NULL);
827 register_pass_name (pass, full_name);
828 free (CONST_CAST (char *, full_name));
831 /* Register the dump files for the pass_manager starting at PASS. */
833 void
834 pass_manager::register_dump_files (opt_pass *pass)
838 if (pass->name && pass->name[0] != '*')
839 register_one_dump_file (pass);
841 if (pass->sub)
842 register_dump_files (pass->sub);
844 pass = pass->next;
846 while (pass);
849 static hash_map<nofree_string_hash, opt_pass *> *name_to_pass_map;
851 /* Register PASS with NAME. */
853 static void
854 register_pass_name (opt_pass *pass, const char *name)
856 if (!name_to_pass_map)
857 name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
859 if (name_to_pass_map->get (name))
860 return; /* Ignore plugin passes. */
862 const char *unique_name = xstrdup (name);
863 name_to_pass_map->put (unique_name, pass);
866 /* Map from pass id to canonicalized pass name. */
868 typedef const char *char_ptr;
869 static vec<char_ptr> pass_tab = vNULL;
871 /* Callback function for traversing NAME_TO_PASS_MAP. */
873 bool
874 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
876 gcc_assert (pass->static_pass_number > 0);
877 gcc_assert (pass_tab.exists ());
879 pass_tab[pass->static_pass_number] = name;
881 return 1;
884 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
885 table for dumping purpose. */
887 static void
888 create_pass_tab (void)
890 if (!flag_dump_passes)
891 return;
893 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
894 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
897 static bool override_gate_status (opt_pass *, tree, bool);
899 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
900 is turned on or not. */
902 static void
903 dump_one_pass (opt_pass *pass, int pass_indent)
905 int indent = 3 * pass_indent;
906 const char *pn;
907 bool is_on, is_really_on;
909 is_on = pass->gate (cfun);
910 is_really_on = override_gate_status (pass, current_function_decl, is_on);
912 if (pass->static_pass_number <= 0)
913 pn = pass->name;
914 else
915 pn = pass_tab[pass->static_pass_number];
917 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
918 (15 - indent < 0 ? 0 : 15 - indent), " ",
919 is_on ? " ON" : " OFF",
920 ((!is_on) == (!is_really_on) ? ""
921 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
924 /* Dump pass list PASS with indentation INDENT. */
926 static void
927 dump_pass_list (opt_pass *pass, int indent)
931 dump_one_pass (pass, indent);
932 if (pass->sub)
933 dump_pass_list (pass->sub, indent + 1);
934 pass = pass->next;
936 while (pass);
939 /* Dump all optimization passes. */
941 void
942 dump_passes (void)
944 g->get_passes ()->dump_passes ();
947 void
948 pass_manager::dump_passes () const
950 push_dummy_function (true);
952 create_pass_tab ();
954 dump_pass_list (all_lowering_passes, 1);
955 dump_pass_list (all_small_ipa_passes, 1);
956 dump_pass_list (all_regular_ipa_passes, 1);
957 dump_pass_list (all_late_ipa_passes, 1);
958 dump_pass_list (all_passes, 1);
960 pop_dummy_function ();
963 /* Returns the pass with NAME. */
965 static opt_pass *
966 get_pass_by_name (const char *name)
968 opt_pass **p = name_to_pass_map->get (name);
969 if (p)
970 return *p;
972 return NULL;
976 /* Range [start, last]. */
978 struct uid_range
980 unsigned int start;
981 unsigned int last;
982 const char *assem_name;
983 struct uid_range *next;
986 typedef struct uid_range *uid_range_p;
989 static vec<uid_range_p>
990 enabled_pass_uid_range_tab = vNULL;
991 static vec<uid_range_p>
992 disabled_pass_uid_range_tab = vNULL;
995 /* Parse option string for -fdisable- and -fenable-
996 The syntax of the options:
998 -fenable-<pass_name>
999 -fdisable-<pass_name>
1001 -fenable-<pass_name>=s1:e1,s2:e2,...
1002 -fdisable-<pass_name>=s1:e1,s2:e2,...
1005 static void
1006 enable_disable_pass (const char *arg, bool is_enable)
1008 opt_pass *pass;
1009 char *range_str, *phase_name;
1010 char *argstr = xstrdup (arg);
1011 vec<uid_range_p> *tab = 0;
1013 range_str = strchr (argstr,'=');
1014 if (range_str)
1016 *range_str = '\0';
1017 range_str++;
1020 phase_name = argstr;
1021 if (!*phase_name)
1023 if (is_enable)
1024 error ("unrecognized option -fenable");
1025 else
1026 error ("unrecognized option -fdisable");
1027 free (argstr);
1028 return;
1030 pass = get_pass_by_name (phase_name);
1031 if (!pass || pass->static_pass_number == -1)
1033 if (is_enable)
1034 error ("unknown pass %s specified in -fenable", phase_name);
1035 else
1036 error ("unknown pass %s specified in -fdisable", phase_name);
1037 free (argstr);
1038 return;
1041 if (is_enable)
1042 tab = &enabled_pass_uid_range_tab;
1043 else
1044 tab = &disabled_pass_uid_range_tab;
1046 if ((unsigned) pass->static_pass_number >= tab->length ())
1047 tab->safe_grow_cleared (pass->static_pass_number + 1);
1049 if (!range_str)
1051 uid_range_p slot;
1052 uid_range_p new_range = XCNEW (struct uid_range);
1054 new_range->start = 0;
1055 new_range->last = (unsigned)-1;
1057 slot = (*tab)[pass->static_pass_number];
1058 new_range->next = slot;
1059 (*tab)[pass->static_pass_number] = new_range;
1060 if (is_enable)
1061 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1062 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1063 else
1064 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1065 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1067 else
1069 char *next_range = NULL;
1070 char *one_range = range_str;
1071 char *end_val = NULL;
1075 uid_range_p slot;
1076 uid_range_p new_range;
1077 char *invalid = NULL;
1078 long start;
1079 char *func_name = NULL;
1081 next_range = strchr (one_range, ',');
1082 if (next_range)
1084 *next_range = '\0';
1085 next_range++;
1088 end_val = strchr (one_range, ':');
1089 if (end_val)
1091 *end_val = '\0';
1092 end_val++;
1094 start = strtol (one_range, &invalid, 10);
1095 if (*invalid || start < 0)
1097 if (end_val || (one_range[0] >= '0'
1098 && one_range[0] <= '9'))
1100 error ("Invalid range %s in option %s",
1101 one_range,
1102 is_enable ? "-fenable" : "-fdisable");
1103 free (argstr);
1104 return;
1106 func_name = one_range;
1108 if (!end_val)
1110 new_range = XCNEW (struct uid_range);
1111 if (!func_name)
1113 new_range->start = (unsigned) start;
1114 new_range->last = (unsigned) start;
1116 else
1118 new_range->start = (unsigned) -1;
1119 new_range->last = (unsigned) -1;
1120 new_range->assem_name = xstrdup (func_name);
1123 else
1125 long last = strtol (end_val, &invalid, 10);
1126 if (*invalid || last < start)
1128 error ("Invalid range %s in option %s",
1129 end_val,
1130 is_enable ? "-fenable" : "-fdisable");
1131 free (argstr);
1132 return;
1134 new_range = XCNEW (struct uid_range);
1135 new_range->start = (unsigned) start;
1136 new_range->last = (unsigned) last;
1139 slot = (*tab)[pass->static_pass_number];
1140 new_range->next = slot;
1141 (*tab)[pass->static_pass_number] = new_range;
1142 if (is_enable)
1144 if (new_range->assem_name)
1145 inform (UNKNOWN_LOCATION,
1146 "enable pass %s for function %s",
1147 phase_name, new_range->assem_name);
1148 else
1149 inform (UNKNOWN_LOCATION,
1150 "enable pass %s for functions in the range of [%u, %u]",
1151 phase_name, new_range->start, new_range->last);
1153 else
1155 if (new_range->assem_name)
1156 inform (UNKNOWN_LOCATION,
1157 "disable pass %s for function %s",
1158 phase_name, new_range->assem_name);
1159 else
1160 inform (UNKNOWN_LOCATION,
1161 "disable pass %s for functions in the range of [%u, %u]",
1162 phase_name, new_range->start, new_range->last);
1165 one_range = next_range;
1166 } while (next_range);
1169 free (argstr);
1172 /* Enable pass specified by ARG. */
1174 void
1175 enable_pass (const char *arg)
1177 enable_disable_pass (arg, true);
1180 /* Disable pass specified by ARG. */
1182 void
1183 disable_pass (const char *arg)
1185 enable_disable_pass (arg, false);
1188 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1190 static bool
1191 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1192 tree func,
1193 vec<uid_range_p> tab)
1195 uid_range_p slot, range;
1196 int cgraph_uid;
1197 const char *aname = NULL;
1199 if (!tab.exists ()
1200 || (unsigned) pass->static_pass_number >= tab.length ()
1201 || pass->static_pass_number == -1)
1202 return false;
1204 slot = tab[pass->static_pass_number];
1205 if (!slot)
1206 return false;
1208 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1209 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1210 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1212 range = slot;
1213 while (range)
1215 if ((unsigned) cgraph_uid >= range->start
1216 && (unsigned) cgraph_uid <= range->last)
1217 return true;
1218 if (range->assem_name && aname
1219 && !strcmp (range->assem_name, aname))
1220 return true;
1221 range = range->next;
1224 return false;
1228 /* Update static_pass_number for passes (and the flag
1229 TODO_mark_first_instance).
1231 Passes are constructed with static_pass_number preinitialized to 0
1233 This field is used in two different ways: initially as instance numbers
1234 of their kind, and then as ids within the entire pass manager.
1236 Within pass_manager::pass_manager:
1238 * In add_pass_instance(), as called by next_pass_1 in
1239 NEXT_PASS in init_optimization_passes
1241 * When the initial instance of a pass within a pass manager is seen,
1242 it is flagged, and its static_pass_number is set to -1
1244 * On subsequent times that it is seen, the static pass number
1245 is decremented each time, so that if there are e.g. 4 dups,
1246 they have static_pass_number -4, 2, 3, 4 respectively (note
1247 how the initial one is negative and gives the count); these
1248 can be thought of as instance numbers of the specific pass
1250 * Within the register_dump_files () traversal, set_pass_for_id()
1251 is called on each pass, using these instance numbers to create
1252 dumpfile switches, and then overwriting them with a pass id,
1253 which are global to the whole pass manager (based on
1254 (TDI_end + current value of extra_dump_files_in_use) ) */
1256 static void
1257 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1258 opt_pass *initial_pass)
1260 /* Are we dealing with the first pass of its kind, or a clone? */
1261 if (new_pass != initial_pass)
1263 /* We're dealing with a clone. */
1264 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1266 /* Indicate to register_dump_files that this pass has duplicates,
1267 and so it should rename the dump file. The first instance will
1268 be -1, and be number of duplicates = -static_pass_number - 1.
1269 Subsequent instances will be > 0 and just the duplicate number. */
1270 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1272 initial_pass->static_pass_number -= 1;
1273 new_pass->static_pass_number = -initial_pass->static_pass_number;
1276 else
1278 /* We're dealing with the first pass of its kind. */
1279 new_pass->todo_flags_start |= TODO_mark_first_instance;
1280 new_pass->static_pass_number = -1;
1282 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1286 /* Add a pass to the pass list. Duplicate the pass if it's already
1287 in the list. */
1289 static opt_pass **
1290 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1292 /* Every pass should have a name so that plugins can refer to them. */
1293 gcc_assert (pass->name != NULL);
1295 add_pass_instance (pass, false, initial_pass);
1296 *list = pass;
1298 return &(*list)->next;
1301 /* List node for an inserted pass instance. We need to keep track of all
1302 the newly-added pass instances (with 'added_pass_nodes' defined below)
1303 so that we can register their dump files after pass-positioning is finished.
1304 Registering dumping files needs to be post-processed or the
1305 static_pass_number of the opt_pass object would be modified and mess up
1306 the dump file names of future pass instances to be added. */
1308 struct pass_list_node
1310 opt_pass *pass;
1311 struct pass_list_node *next;
1314 static struct pass_list_node *added_pass_nodes = NULL;
1315 static struct pass_list_node *prev_added_pass_node;
1317 /* Insert the pass at the proper position. Return true if the pass
1318 is successfully added.
1320 NEW_PASS_INFO - new pass to be inserted
1321 PASS_LIST - root of the pass list to insert the new pass to */
1323 static bool
1324 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1326 opt_pass *pass = *pass_list, *prev_pass = NULL;
1327 bool success = false;
1329 for ( ; pass; prev_pass = pass, pass = pass->next)
1331 /* Check if the current pass is of the same type as the new pass and
1332 matches the name and the instance number of the reference pass. */
1333 if (pass->type == new_pass_info->pass->type
1334 && pass->name
1335 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1336 && ((new_pass_info->ref_pass_instance_number == 0)
1337 || (new_pass_info->ref_pass_instance_number ==
1338 pass->static_pass_number)
1339 || (new_pass_info->ref_pass_instance_number == 1
1340 && pass->todo_flags_start & TODO_mark_first_instance)))
1342 opt_pass *new_pass;
1343 struct pass_list_node *new_pass_node;
1345 if (new_pass_info->ref_pass_instance_number == 0)
1347 new_pass = new_pass_info->pass->clone ();
1348 add_pass_instance (new_pass, true, new_pass_info->pass);
1350 else
1352 new_pass = new_pass_info->pass;
1353 add_pass_instance (new_pass, true, new_pass);
1356 /* Insert the new pass instance based on the positioning op. */
1357 switch (new_pass_info->pos_op)
1359 case PASS_POS_INSERT_AFTER:
1360 new_pass->next = pass->next;
1361 pass->next = new_pass;
1363 /* Skip newly inserted pass to avoid repeated
1364 insertions in the case where the new pass and the
1365 existing one have the same name. */
1366 pass = new_pass;
1367 break;
1368 case PASS_POS_INSERT_BEFORE:
1369 new_pass->next = pass;
1370 if (prev_pass)
1371 prev_pass->next = new_pass;
1372 else
1373 *pass_list = new_pass;
1374 break;
1375 case PASS_POS_REPLACE:
1376 new_pass->next = pass->next;
1377 if (prev_pass)
1378 prev_pass->next = new_pass;
1379 else
1380 *pass_list = new_pass;
1381 new_pass->sub = pass->sub;
1382 new_pass->tv_id = pass->tv_id;
1383 pass = new_pass;
1384 break;
1385 default:
1386 error ("invalid pass positioning operation");
1387 return false;
1390 /* Save the newly added pass (instance) in the added_pass_nodes
1391 list so that we can register its dump file later. Note that
1392 we cannot register the dump file now because doing so will modify
1393 the static_pass_number of the opt_pass object and therefore
1394 mess up the dump file name of future instances. */
1395 new_pass_node = XCNEW (struct pass_list_node);
1396 new_pass_node->pass = new_pass;
1397 if (!added_pass_nodes)
1398 added_pass_nodes = new_pass_node;
1399 else
1400 prev_added_pass_node->next = new_pass_node;
1401 prev_added_pass_node = new_pass_node;
1403 success = true;
1406 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1407 success = true;
1410 return success;
1413 /* Hooks a new pass into the pass lists.
1415 PASS_INFO - pass information that specifies the opt_pass object,
1416 reference pass, instance number, and how to position
1417 the pass */
1419 void
1420 register_pass (struct register_pass_info *pass_info)
1422 g->get_passes ()->register_pass (pass_info);
1425 void
1426 register_pass (opt_pass* pass, pass_positioning_ops pos,
1427 const char* ref_pass_name, int ref_pass_inst_number)
1429 register_pass_info i;
1430 i.pass = pass;
1431 i.reference_pass_name = ref_pass_name;
1432 i.ref_pass_instance_number = ref_pass_inst_number;
1433 i.pos_op = pos;
1435 g->get_passes ()->register_pass (&i);
1438 void
1439 pass_manager::register_pass (struct register_pass_info *pass_info)
1441 bool all_instances, success;
1442 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1444 /* The checks below could fail in buggy plugins. Existing GCC
1445 passes should never fail these checks, so we mention plugin in
1446 the messages. */
1447 if (!pass_info->pass)
1448 fatal_error (input_location, "plugin cannot register a missing pass");
1450 if (!pass_info->pass->name)
1451 fatal_error (input_location, "plugin cannot register an unnamed pass");
1453 if (!pass_info->reference_pass_name)
1454 fatal_error
1455 (input_location,
1456 "plugin cannot register pass %qs without reference pass name",
1457 pass_info->pass->name);
1459 /* Try to insert the new pass to the pass lists. We need to check
1460 all five lists as the reference pass could be in one (or all) of
1461 them. */
1462 all_instances = pass_info->ref_pass_instance_number == 0;
1463 success = position_pass (pass_info, &all_lowering_passes);
1464 if (!success || all_instances)
1465 success |= position_pass (pass_info, &all_small_ipa_passes);
1466 if (!success || all_instances)
1467 success |= position_pass (pass_info, &all_regular_ipa_passes);
1468 if (!success || all_instances)
1469 success |= position_pass (pass_info, &all_late_ipa_passes);
1470 if (!success || all_instances)
1471 success |= position_pass (pass_info, &all_passes);
1472 if (!success)
1473 fatal_error
1474 (input_location,
1475 "pass %qs not found but is referenced by new pass %qs",
1476 pass_info->reference_pass_name, pass_info->pass->name);
1478 /* OK, we have successfully inserted the new pass. We need to register
1479 the dump files for the newly added pass and its duplicates (if any).
1480 Because the registration of plugin/backend passes happens after the
1481 command-line options are parsed, the options that specify single
1482 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1483 passes. Therefore we currently can only enable dumping of
1484 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1485 are specified. While doing so, we also delete the pass_list_node
1486 objects created during pass positioning. */
1487 while (added_pass_nodes)
1489 struct pass_list_node *next_node = added_pass_nodes->next;
1490 enum tree_dump_index tdi;
1491 register_one_dump_file (added_pass_nodes->pass);
1492 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1493 || added_pass_nodes->pass->type == IPA_PASS)
1494 tdi = TDI_ipa_all;
1495 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1496 tdi = TDI_tree_all;
1497 else
1498 tdi = TDI_rtl_all;
1499 /* Check if dump-all flag is specified. */
1500 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 XDELETE (added_pass_nodes);
1504 added_pass_nodes = next_node;
1508 /* Construct the pass tree. The sequencing of passes is driven by
1509 the cgraph routines:
1511 finalize_compilation_unit ()
1512 for each node N in the cgraph
1513 cgraph_analyze_function (N)
1514 cgraph_lower_function (N) -> all_lowering_passes
1516 If we are optimizing, compile is then invoked:
1518 compile ()
1519 ipa_passes () -> all_small_ipa_passes
1520 -> Analysis of all_regular_ipa_passes
1521 * possible LTO streaming at copmilation time *
1522 -> Execution of all_regular_ipa_passes
1523 * possible LTO streaming at link time *
1524 -> all_late_ipa_passes
1525 expand_all_functions ()
1526 for each node N in the cgraph
1527 expand_function (N) -> Transformation of all_regular_ipa_passes
1528 -> all_passes
1531 void *
1532 pass_manager::operator new (size_t sz)
1534 /* Ensure that all fields of the pass manager are zero-initialized. */
1535 return xcalloc (1, sz);
1538 void
1539 pass_manager::operator delete (void *ptr)
1541 free (ptr);
1544 pass_manager::pass_manager (context *ctxt)
1545 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1546 all_regular_ipa_passes (NULL),
1547 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1548 m_ctxt (ctxt)
1550 opt_pass **p;
1552 /* Initialize the pass_lists array. */
1553 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1554 GCC_PASS_LISTS
1555 #undef DEF_PASS_LIST
1557 /* Build the tree of passes. */
1559 #define INSERT_PASSES_AFTER(PASS) \
1560 p = &(PASS);
1562 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1564 opt_pass **p = &(PASS ## _1)->sub;
1566 #define POP_INSERT_PASSES() \
1569 #define NEXT_PASS(PASS, NUM) \
1570 do { \
1571 gcc_assert (NULL == PASS ## _ ## NUM); \
1572 if ((NUM) == 1) \
1573 PASS ## _1 = make_##PASS (m_ctxt); \
1574 else \
1576 gcc_assert (PASS ## _1); \
1577 PASS ## _ ## NUM = PASS ## _1->clone (); \
1579 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1580 } while (0)
1582 #define TERMINATE_PASS_LIST() \
1583 *p = NULL;
1585 #include "pass-instances.def"
1587 #undef INSERT_PASSES_AFTER
1588 #undef PUSH_INSERT_PASSES_WITHIN
1589 #undef POP_INSERT_PASSES
1590 #undef NEXT_PASS
1591 #undef TERMINATE_PASS_LIST
1593 /* Register the passes with the tree dump code. */
1594 register_dump_files (all_lowering_passes);
1595 register_dump_files (all_small_ipa_passes);
1596 register_dump_files (all_regular_ipa_passes);
1597 register_dump_files (all_late_ipa_passes);
1598 register_dump_files (all_passes);
1601 static void
1602 delete_pass_tree (opt_pass *pass)
1604 while (pass)
1606 /* Recurse into child passes. */
1607 delete_pass_tree (pass->sub);
1609 opt_pass *next = pass->next;
1611 /* Delete this pass. */
1612 delete pass;
1614 /* Iterate onto sibling passes. */
1615 pass = next;
1619 pass_manager::~pass_manager ()
1621 XDELETEVEC (passes_by_id);
1623 /* Call delete_pass_tree on each of the pass_lists. */
1624 #define DEF_PASS_LIST(LIST) \
1625 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1626 GCC_PASS_LISTS
1627 #undef DEF_PASS_LIST
1631 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1632 function CALLBACK for every function in the call graph. Otherwise,
1633 call CALLBACK on the current function. */
1635 static void
1636 do_per_function (void (*callback) (function *, void *data), void *data)
1638 if (current_function_decl)
1639 callback (cfun, data);
1640 else
1642 struct cgraph_node *node;
1643 FOR_EACH_DEFINED_FUNCTION (node)
1644 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1645 && (!node->clone_of || node->decl != node->clone_of->decl))
1646 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1650 /* Because inlining might remove no-longer reachable nodes, we need to
1651 keep the array visible to garbage collector to avoid reading collected
1652 out nodes. */
1653 static int nnodes;
1654 static GTY ((length ("nnodes"))) cgraph_node **order;
1656 /* Hook called when NODE is removed and therefore should be
1657 excluded from order vector. DATA is an array of integers.
1658 DATA[0] holds max index it may be accessed by. For cgraph
1659 node DATA[node->uid + 1] holds index of this node in order
1660 vector. */
1661 static void
1662 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1664 int *order_idx = (int *)data;
1666 if (node->uid >= order_idx[0])
1667 return;
1669 int idx = order_idx[node->uid + 1];
1670 if (idx >= 0 && idx < nnodes && order[idx] == node)
1671 order[idx] = NULL;
1674 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1675 function CALLBACK for every function in the call graph. Otherwise,
1676 call CALLBACK on the current function.
1677 This function is global so that plugins can use it. */
1678 void
1679 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1681 int i;
1683 if (current_function_decl)
1684 callback (cfun, data);
1685 else
1687 cgraph_node_hook_list *hook;
1688 int *order_idx;
1689 gcc_assert (!order);
1690 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1692 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1693 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1694 order_idx[0] = symtab->cgraph_max_uid;
1696 nnodes = ipa_reverse_postorder (order);
1697 for (i = nnodes - 1; i >= 0; i--)
1699 order[i]->process = 1;
1700 order_idx[order[i]->uid + 1] = i;
1702 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1703 order_idx);
1704 for (i = nnodes - 1; i >= 0; i--)
1706 /* Function could be inlined and removed as unreachable. */
1707 if (!order[i])
1708 continue;
1710 struct cgraph_node *node = order[i];
1712 /* Allow possibly removed nodes to be garbage collected. */
1713 order[i] = NULL;
1714 node->process = 0;
1715 if (node->has_gimple_body_p ())
1716 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1718 symtab->remove_cgraph_removal_hook (hook);
1720 ggc_free (order);
1721 order = NULL;
1722 nnodes = 0;
1725 /* Helper function to perform function body dump. */
1727 static void
1728 execute_function_dump (function *fn, void *data)
1730 opt_pass *pass = (opt_pass *)data;
1732 if (dump_file)
1734 push_cfun (fn);
1736 if (fn->curr_properties & PROP_trees)
1737 dump_function_to_file (fn->decl, dump_file, dump_flags);
1738 else
1739 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1741 /* Flush the file. If verification fails, we won't be able to
1742 close the file before aborting. */
1743 fflush (dump_file);
1745 if ((fn->curr_properties & PROP_cfg)
1746 && (dump_flags & TDF_GRAPH))
1748 if (!pass->graph_dump_initialized)
1750 clean_graph_dump_file (dump_file_name);
1751 pass->graph_dump_initialized = true;
1753 print_graph_cfg (dump_file_name, fn);
1756 pop_cfun ();
1760 static struct profile_record *profile_record;
1762 /* Do profile consistency book-keeping for the pass with static number INDEX.
1763 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1764 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1765 if we are only book-keeping on passes that may have selectively disabled
1766 themselves on a given function. */
1767 static void
1768 check_profile_consistency (int index, int subpass, bool run)
1770 pass_manager *passes = g->get_passes ();
1771 if (index == -1)
1772 return;
1773 if (!profile_record)
1774 profile_record = XCNEWVEC (struct profile_record,
1775 passes->passes_by_id_size);
1776 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1777 gcc_assert (subpass < 2);
1778 profile_record[index].run |= run;
1779 account_profile_record (&profile_record[index], subpass);
1782 /* Output profile consistency. */
1784 void
1785 dump_profile_report (void)
1787 g->get_passes ()->dump_profile_report ();
1790 void
1791 pass_manager::dump_profile_report () const
1793 int i, j;
1794 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1795 gcov_type last_time = 0, last_size = 0;
1796 double rel_time_change, rel_size_change;
1797 int last_reported = 0;
1799 if (!profile_record)
1800 return;
1801 fprintf (stderr, "\nProfile consistency report:\n\n");
1802 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1803 fprintf (stderr, " |freq count |freq count |size time\n");
1805 for (i = 0; i < passes_by_id_size; i++)
1806 for (j = 0 ; j < 2; j++)
1807 if (profile_record[i].run)
1809 if (last_time)
1810 rel_time_change = (profile_record[i].time[j]
1811 - (double)last_time) * 100 / (double)last_time;
1812 else
1813 rel_time_change = 0;
1814 if (last_size)
1815 rel_size_change = (profile_record[i].size[j]
1816 - (double)last_size) * 100 / (double)last_size;
1817 else
1818 rel_size_change = 0;
1820 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1821 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1822 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1823 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1824 || rel_time_change || rel_size_change)
1826 last_reported = i;
1827 fprintf (stderr, "%-20s %s",
1828 passes_by_id [i]->name,
1829 j ? "(after TODO)" : " ");
1830 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1831 fprintf (stderr, "| %+5i",
1832 profile_record[i].num_mismatched_freq_in[j]
1833 - last_freq_in);
1834 else
1835 fprintf (stderr, "| ");
1836 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1837 fprintf (stderr, " %+5i",
1838 profile_record[i].num_mismatched_count_in[j]
1839 - last_count_in);
1840 else
1841 fprintf (stderr, " ");
1842 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1843 fprintf (stderr, "| %+5i",
1844 profile_record[i].num_mismatched_freq_out[j]
1845 - last_freq_out);
1846 else
1847 fprintf (stderr, "| ");
1848 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1849 fprintf (stderr, " %+5i",
1850 profile_record[i].num_mismatched_count_out[j]
1851 - last_count_out);
1852 else
1853 fprintf (stderr, " ");
1855 /* Size/time units change across gimple and RTL. */
1856 if (i == pass_expand_1->static_pass_number)
1857 fprintf (stderr, "|----------");
1858 else
1860 if (rel_size_change)
1861 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1862 else
1863 fprintf (stderr, "| ");
1864 if (rel_time_change)
1865 fprintf (stderr, " %+8.4f%%", rel_time_change);
1867 fprintf (stderr, "\n");
1868 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1869 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1870 last_count_in = profile_record[i].num_mismatched_count_in[j];
1871 last_count_out = profile_record[i].num_mismatched_count_out[j];
1873 else if (j && last_reported != i)
1875 last_reported = i;
1876 fprintf (stderr, "%-20s ------------| | |\n",
1877 passes_by_id [i]->name);
1879 last_time = profile_record[i].time[j];
1880 last_size = profile_record[i].size[j];
1884 /* Perform all TODO actions that ought to be done on each function. */
1886 static void
1887 execute_function_todo (function *fn, void *data)
1889 bool from_ipa_pass = (cfun == NULL);
1890 unsigned int flags = (size_t)data;
1891 flags &= ~fn->last_verified;
1892 if (!flags)
1893 return;
1895 push_cfun (fn);
1897 /* Always cleanup the CFG before trying to update SSA. */
1898 if (flags & TODO_cleanup_cfg)
1900 cleanup_tree_cfg ();
1902 /* When cleanup_tree_cfg merges consecutive blocks, it may
1903 perform some simplistic propagation when removing single
1904 valued PHI nodes. This propagation may, in turn, cause the
1905 SSA form to become out-of-date (see PR 22037). So, even
1906 if the parent pass had not scheduled an SSA update, we may
1907 still need to do one. */
1908 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1909 flags |= TODO_update_ssa;
1912 if (flags & TODO_update_ssa_any)
1914 unsigned update_flags = flags & TODO_update_ssa_any;
1915 update_ssa (update_flags);
1918 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1919 compute_may_aliases ();
1921 if (optimize && (flags & TODO_update_address_taken))
1922 execute_update_addresses_taken ();
1924 if (flags & TODO_remove_unused_locals)
1925 remove_unused_locals ();
1927 if (flags & TODO_rebuild_frequencies)
1928 rebuild_frequencies ();
1930 if (flags & TODO_rebuild_cgraph_edges)
1931 cgraph_edge::rebuild_edges ();
1933 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1934 /* If we've seen errors do not bother running any verifiers. */
1935 if (!seen_error ())
1937 #if defined ENABLE_CHECKING
1938 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1939 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1941 if (flags & TODO_verify_il)
1943 if (cfun->curr_properties & PROP_trees)
1945 if (cfun->curr_properties & PROP_cfg)
1946 /* IPA passes leave stmts to be fixed up, so make sure to
1947 not verify stmts really throw. */
1948 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1949 else
1950 verify_gimple_in_seq (gimple_body (cfun->decl));
1952 if (cfun->curr_properties & PROP_ssa)
1953 /* IPA passes leave stmts to be fixed up, so make sure to
1954 not verify SSA operands whose verifier will choke on that. */
1955 verify_ssa (true, !from_ipa_pass);
1956 /* IPA passes leave basic-blocks unsplit, so make sure to
1957 not trip on that. */
1958 if ((cfun->curr_properties & PROP_cfg)
1959 && !from_ipa_pass)
1960 verify_flow_info ();
1961 if (current_loops
1962 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1963 verify_loop_closed_ssa (false);
1964 if (cfun->curr_properties & PROP_rtl)
1965 verify_rtl_sharing ();
1968 /* Make sure verifiers don't change dominator state. */
1969 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1970 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1971 #endif
1974 fn->last_verified = flags & TODO_verify_all;
1976 pop_cfun ();
1978 /* For IPA passes make sure to release dominator info, it can be
1979 computed by non-verifying TODOs. */
1980 if (from_ipa_pass)
1982 free_dominance_info (fn, CDI_DOMINATORS);
1983 free_dominance_info (fn, CDI_POST_DOMINATORS);
1987 /* Perform all TODO actions. */
1988 static void
1989 execute_todo (unsigned int flags)
1991 #if defined ENABLE_CHECKING
1992 if (cfun
1993 && need_ssa_update_p (cfun))
1994 gcc_assert (flags & TODO_update_ssa_any);
1995 #endif
1997 timevar_push (TV_TODO);
1999 /* Inform the pass whether it is the first time it is run. */
2000 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2002 statistics_fini_pass ();
2004 if (flags)
2005 do_per_function (execute_function_todo, (void *)(size_t) flags);
2007 /* Always remove functions just as before inlining: IPA passes might be
2008 interested to see bodies of extern inline functions that are not inlined
2009 to analyze side effects. The full removal is done just at the end
2010 of IPA pass queue. */
2011 if (flags & TODO_remove_functions)
2013 gcc_assert (!cfun);
2014 symtab->remove_unreachable_nodes (dump_file);
2017 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2019 gcc_assert (!cfun);
2020 symtab_node::dump_table (dump_file);
2021 /* Flush the file. If verification fails, we won't be able to
2022 close the file before aborting. */
2023 fflush (dump_file);
2026 /* Now that the dumping has been done, we can get rid of the optional
2027 df problems. */
2028 if (flags & TODO_df_finish)
2029 df_finish_pass ((flags & TODO_df_verify) != 0);
2031 timevar_pop (TV_TODO);
2034 /* Verify invariants that should hold between passes. This is a place
2035 to put simple sanity checks. */
2037 static void
2038 verify_interpass_invariants (void)
2040 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2043 /* Clear the last verified flag. */
2045 static void
2046 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2048 fn->last_verified = 0;
2051 /* Helper function. Verify that the properties has been turn into the
2052 properties expected by the pass. */
2054 #ifdef ENABLE_CHECKING
2055 static void
2056 verify_curr_properties (function *fn, void *data)
2058 unsigned int props = (size_t)data;
2059 gcc_assert ((fn->curr_properties & props) == props);
2061 #endif
2063 /* Initialize pass dump file. */
2064 /* This is non-static so that the plugins can use it. */
2066 bool
2067 pass_init_dump_file (opt_pass *pass)
2069 /* If a dump file name is present, open it if enabled. */
2070 if (pass->static_pass_number != -1)
2072 timevar_push (TV_DUMP);
2073 gcc::dump_manager *dumps = g->get_dumps ();
2074 bool initializing_dump =
2075 !dumps->dump_initialized_p (pass->static_pass_number);
2076 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2077 dumps->dump_start (pass->static_pass_number, &dump_flags);
2078 if (dump_file && current_function_decl)
2079 dump_function_header (dump_file, current_function_decl, dump_flags);
2080 if (initializing_dump
2081 && dump_file && (dump_flags & TDF_GRAPH)
2082 && cfun && (cfun->curr_properties & PROP_cfg))
2084 clean_graph_dump_file (dump_file_name);
2085 pass->graph_dump_initialized = true;
2087 timevar_pop (TV_DUMP);
2088 return initializing_dump;
2090 else
2091 return false;
2094 /* Flush PASS dump file. */
2095 /* This is non-static so that plugins can use it. */
2097 void
2098 pass_fini_dump_file (opt_pass *pass)
2100 timevar_push (TV_DUMP);
2102 /* Flush and close dump file. */
2103 if (dump_file_name)
2105 free (CONST_CAST (char *, dump_file_name));
2106 dump_file_name = NULL;
2109 g->get_dumps ()->dump_finish (pass->static_pass_number);
2110 timevar_pop (TV_DUMP);
2113 /* After executing the pass, apply expected changes to the function
2114 properties. */
2116 static void
2117 update_properties_after_pass (function *fn, void *data)
2119 opt_pass *pass = (opt_pass *) data;
2120 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2121 & ~pass->properties_destroyed;
2124 /* Execute summary generation for all of the passes in IPA_PASS. */
2126 void
2127 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2129 while (ipa_pass)
2131 opt_pass *pass = ipa_pass;
2133 /* Execute all of the IPA_PASSes in the list. */
2134 if (ipa_pass->type == IPA_PASS
2135 && pass->gate (cfun)
2136 && ipa_pass->generate_summary)
2138 pass_init_dump_file (pass);
2140 /* If a timevar is present, start it. */
2141 if (pass->tv_id)
2142 timevar_push (pass->tv_id);
2144 current_pass = pass;
2145 ipa_pass->generate_summary ();
2147 /* Stop timevar. */
2148 if (pass->tv_id)
2149 timevar_pop (pass->tv_id);
2151 pass_fini_dump_file (pass);
2153 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2157 /* Execute IPA_PASS function transform on NODE. */
2159 static void
2160 execute_one_ipa_transform_pass (struct cgraph_node *node,
2161 ipa_opt_pass_d *ipa_pass)
2163 opt_pass *pass = ipa_pass;
2164 unsigned int todo_after = 0;
2166 current_pass = pass;
2167 if (!ipa_pass->function_transform)
2168 return;
2170 /* Note that the folders should only create gimple expressions.
2171 This is a hack until the new folder is ready. */
2172 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2174 pass_init_dump_file (pass);
2176 /* Run pre-pass verification. */
2177 execute_todo (ipa_pass->function_transform_todo_flags_start);
2179 /* If a timevar is present, start it. */
2180 if (pass->tv_id != TV_NONE)
2181 timevar_push (pass->tv_id);
2183 /* Do it! */
2184 todo_after = ipa_pass->function_transform (node);
2186 /* Stop timevar. */
2187 if (pass->tv_id != TV_NONE)
2188 timevar_pop (pass->tv_id);
2190 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2191 check_profile_consistency (pass->static_pass_number, 0, true);
2193 /* Run post-pass cleanup and verification. */
2194 execute_todo (todo_after);
2195 verify_interpass_invariants ();
2196 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2197 check_profile_consistency (pass->static_pass_number, 1, true);
2199 if (dump_file)
2200 do_per_function (execute_function_dump, NULL);
2201 pass_fini_dump_file (pass);
2203 current_pass = NULL;
2205 /* Signal this is a suitable GC collection point. */
2206 if (!(todo_after & TODO_do_not_ggc_collect))
2207 ggc_collect ();
2210 /* For the current function, execute all ipa transforms. */
2212 void
2213 execute_all_ipa_transforms (void)
2215 struct cgraph_node *node;
2216 if (!cfun)
2217 return;
2218 node = cgraph_node::get (current_function_decl);
2220 if (node->ipa_transforms_to_apply.exists ())
2222 unsigned int i;
2224 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2225 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2226 node->ipa_transforms_to_apply.release ();
2230 /* Check if PASS is explicitly disabled or enabled and return
2231 the gate status. FUNC is the function to be processed, and
2232 GATE_STATUS is the gate status determined by pass manager by
2233 default. */
2235 static bool
2236 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2238 bool explicitly_enabled = false;
2239 bool explicitly_disabled = false;
2241 explicitly_enabled
2242 = is_pass_explicitly_enabled_or_disabled (pass, func,
2243 enabled_pass_uid_range_tab);
2244 explicitly_disabled
2245 = is_pass_explicitly_enabled_or_disabled (pass, func,
2246 disabled_pass_uid_range_tab);
2248 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2250 return gate_status;
2254 /* Execute PASS. */
2256 bool
2257 execute_one_pass (opt_pass *pass)
2259 unsigned int todo_after = 0;
2261 bool gate_status;
2263 /* IPA passes are executed on whole program, so cfun should be NULL.
2264 Other passes need function context set. */
2265 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2266 gcc_assert (!cfun && !current_function_decl);
2267 else
2268 gcc_assert (cfun && current_function_decl);
2270 current_pass = pass;
2272 /* Check whether gate check should be avoided.
2273 User controls the value of the gate through the parameter "gate_status". */
2274 gate_status = pass->gate (cfun);
2275 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2277 /* Override gate with plugin. */
2278 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2280 if (!gate_status)
2282 /* Run so passes selectively disabling themselves on a given function
2283 are not miscounted. */
2284 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2286 check_profile_consistency (pass->static_pass_number, 0, false);
2287 check_profile_consistency (pass->static_pass_number, 1, false);
2289 current_pass = NULL;
2290 return false;
2293 /* Pass execution event trigger: useful to identify passes being
2294 executed. */
2295 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2297 if (!quiet_flag && !cfun)
2298 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2300 /* Note that the folders should only create gimple expressions.
2301 This is a hack until the new folder is ready. */
2302 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2304 pass_init_dump_file (pass);
2306 /* Run pre-pass verification. */
2307 execute_todo (pass->todo_flags_start);
2309 #ifdef ENABLE_CHECKING
2310 do_per_function (verify_curr_properties,
2311 (void *)(size_t)pass->properties_required);
2312 #endif
2314 /* If a timevar is present, start it. */
2315 if (pass->tv_id != TV_NONE)
2316 timevar_push (pass->tv_id);
2318 /* Do it! */
2319 todo_after = pass->execute (cfun);
2320 do_per_function (clear_last_verified, NULL);
2322 /* Stop timevar. */
2323 if (pass->tv_id != TV_NONE)
2324 timevar_pop (pass->tv_id);
2326 do_per_function (update_properties_after_pass, pass);
2328 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2329 check_profile_consistency (pass->static_pass_number, 0, true);
2331 /* Run post-pass cleanup and verification. */
2332 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2333 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2334 check_profile_consistency (pass->static_pass_number, 1, true);
2336 verify_interpass_invariants ();
2337 if (dump_file)
2338 do_per_function (execute_function_dump, pass);
2339 if (pass->type == IPA_PASS)
2341 struct cgraph_node *node;
2342 if (((ipa_opt_pass_d *)pass)->function_transform)
2343 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2344 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2347 if (!current_function_decl)
2348 symtab->process_new_functions ();
2350 pass_fini_dump_file (pass);
2352 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2353 gcc_assert (!(cfun->curr_properties & PROP_trees)
2354 || pass->type != RTL_PASS);
2356 current_pass = NULL;
2358 /* Signal this is a suitable GC collection point. */
2359 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2360 ggc_collect ();
2362 return true;
2365 static void
2366 execute_pass_list_1 (opt_pass *pass)
2370 gcc_assert (pass->type == GIMPLE_PASS
2371 || pass->type == RTL_PASS);
2372 if (execute_one_pass (pass) && pass->sub)
2373 execute_pass_list_1 (pass->sub);
2374 pass = pass->next;
2376 while (pass);
2379 void
2380 execute_pass_list (function *fn, opt_pass *pass)
2382 push_cfun (fn);
2383 execute_pass_list_1 (pass);
2384 if (fn->cfg)
2386 free_dominance_info (CDI_DOMINATORS);
2387 free_dominance_info (CDI_POST_DOMINATORS);
2389 pop_cfun ();
2392 /* Write out all LTO data. */
2393 static void
2394 write_lto (void)
2396 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2397 lto_output ();
2398 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2399 timevar_push (TV_IPA_LTO_DECL_OUT);
2400 produce_asm_for_decls ();
2401 timevar_pop (TV_IPA_LTO_DECL_OUT);
2404 /* Same as execute_pass_list but assume that subpasses of IPA passes
2405 are local passes. If SET is not NULL, write out summaries of only
2406 those node in SET. */
2408 static void
2409 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2411 while (pass)
2413 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2414 gcc_assert (!current_function_decl);
2415 gcc_assert (!cfun);
2416 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2417 if (pass->type == IPA_PASS
2418 && ipa_pass->write_summary
2419 && pass->gate (cfun))
2421 /* If a timevar is present, start it. */
2422 if (pass->tv_id)
2423 timevar_push (pass->tv_id);
2425 pass_init_dump_file (pass);
2427 current_pass = pass;
2428 ipa_pass->write_summary ();
2430 pass_fini_dump_file (pass);
2432 /* If a timevar is present, start it. */
2433 if (pass->tv_id)
2434 timevar_pop (pass->tv_id);
2437 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2438 ipa_write_summaries_2 (pass->sub, state);
2440 pass = pass->next;
2444 /* Helper function of ipa_write_summaries. Creates and destroys the
2445 decl state and calls ipa_write_summaries_2 for all passes that have
2446 summaries. SET is the set of nodes to be written. */
2448 static void
2449 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2451 pass_manager *passes = g->get_passes ();
2452 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2453 state->symtab_node_encoder = encoder;
2455 lto_output_init_mode_table ();
2456 lto_push_out_decl_state (state);
2458 gcc_assert (!flag_wpa);
2459 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2461 write_lto ();
2463 gcc_assert (lto_get_out_decl_state () == state);
2464 lto_pop_out_decl_state ();
2465 lto_delete_out_decl_state (state);
2468 /* Write out summaries for all the nodes in the callgraph. */
2470 void
2471 ipa_write_summaries (void)
2473 lto_symtab_encoder_t encoder;
2474 int i, order_pos;
2475 varpool_node *vnode;
2476 struct cgraph_node *node;
2477 struct cgraph_node **order;
2479 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2480 return;
2482 select_what_to_stream ();
2484 encoder = lto_symtab_encoder_new (false);
2486 /* Create the callgraph set in the same order used in
2487 cgraph_expand_all_functions. This mostly facilitates debugging,
2488 since it causes the gimple file to be processed in the same order
2489 as the source code. */
2490 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2491 order_pos = ipa_reverse_postorder (order);
2492 gcc_assert (order_pos == symtab->cgraph_count);
2494 for (i = order_pos - 1; i >= 0; i--)
2496 struct cgraph_node *node = order[i];
2498 if (node->has_gimple_body_p ())
2500 /* When streaming out references to statements as part of some IPA
2501 pass summary, the statements need to have uids assigned and the
2502 following does that for all the IPA passes here. Naturally, this
2503 ordering then matches the one IPA-passes get in their stmt_fixup
2504 hooks. */
2506 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2507 renumber_gimple_stmt_uids ();
2508 pop_cfun ();
2510 if (node->definition && node->need_lto_streaming)
2511 lto_set_symtab_encoder_in_partition (encoder, node);
2514 FOR_EACH_DEFINED_FUNCTION (node)
2515 if (node->alias && node->need_lto_streaming)
2516 lto_set_symtab_encoder_in_partition (encoder, node);
2517 FOR_EACH_DEFINED_VARIABLE (vnode)
2518 if (vnode->need_lto_streaming)
2519 lto_set_symtab_encoder_in_partition (encoder, vnode);
2521 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2523 free (order);
2526 /* Same as execute_pass_list but assume that subpasses of IPA passes
2527 are local passes. If SET is not NULL, write out optimization summaries of
2528 only those node in SET. */
2530 static void
2531 ipa_write_optimization_summaries_1 (opt_pass *pass,
2532 struct lto_out_decl_state *state)
2534 while (pass)
2536 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2537 gcc_assert (!current_function_decl);
2538 gcc_assert (!cfun);
2539 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2540 if (pass->type == IPA_PASS
2541 && ipa_pass->write_optimization_summary
2542 && pass->gate (cfun))
2544 /* If a timevar is present, start it. */
2545 if (pass->tv_id)
2546 timevar_push (pass->tv_id);
2548 pass_init_dump_file (pass);
2550 current_pass = pass;
2551 ipa_pass->write_optimization_summary ();
2553 pass_fini_dump_file (pass);
2555 /* If a timevar is present, start it. */
2556 if (pass->tv_id)
2557 timevar_pop (pass->tv_id);
2560 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2561 ipa_write_optimization_summaries_1 (pass->sub, state);
2563 pass = pass->next;
2567 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2568 NULL, write out all summaries of all nodes. */
2570 void
2571 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2573 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2574 lto_symtab_encoder_iterator lsei;
2575 state->symtab_node_encoder = encoder;
2577 lto_output_init_mode_table ();
2578 lto_push_out_decl_state (state);
2579 for (lsei = lsei_start_function_in_partition (encoder);
2580 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2582 struct cgraph_node *node = lsei_cgraph_node (lsei);
2583 /* When streaming out references to statements as part of some IPA
2584 pass summary, the statements need to have uids assigned.
2586 For functions newly born at WPA stage we need to initialize
2587 the uids here. */
2588 if (node->definition
2589 && gimple_has_body_p (node->decl))
2591 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2592 renumber_gimple_stmt_uids ();
2593 pop_cfun ();
2597 gcc_assert (flag_wpa);
2598 pass_manager *passes = g->get_passes ();
2599 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2601 write_lto ();
2603 gcc_assert (lto_get_out_decl_state () == state);
2604 lto_pop_out_decl_state ();
2605 lto_delete_out_decl_state (state);
2608 /* Same as execute_pass_list but assume that subpasses of IPA passes
2609 are local passes. */
2611 static void
2612 ipa_read_summaries_1 (opt_pass *pass)
2614 while (pass)
2616 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2618 gcc_assert (!current_function_decl);
2619 gcc_assert (!cfun);
2620 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2622 if (pass->gate (cfun))
2624 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2626 /* If a timevar is present, start it. */
2627 if (pass->tv_id)
2628 timevar_push (pass->tv_id);
2630 pass_init_dump_file (pass);
2632 current_pass = pass;
2633 ipa_pass->read_summary ();
2635 pass_fini_dump_file (pass);
2637 /* Stop timevar. */
2638 if (pass->tv_id)
2639 timevar_pop (pass->tv_id);
2642 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2643 ipa_read_summaries_1 (pass->sub);
2645 pass = pass->next;
2650 /* Read all the summaries for all_regular_ipa_passes. */
2652 void
2653 ipa_read_summaries (void)
2655 pass_manager *passes = g->get_passes ();
2656 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2659 /* Same as execute_pass_list but assume that subpasses of IPA passes
2660 are local passes. */
2662 static void
2663 ipa_read_optimization_summaries_1 (opt_pass *pass)
2665 while (pass)
2667 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2669 gcc_assert (!current_function_decl);
2670 gcc_assert (!cfun);
2671 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2673 if (pass->gate (cfun))
2675 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2677 /* If a timevar is present, start it. */
2678 if (pass->tv_id)
2679 timevar_push (pass->tv_id);
2681 pass_init_dump_file (pass);
2683 current_pass = pass;
2684 ipa_pass->read_optimization_summary ();
2686 pass_fini_dump_file (pass);
2688 /* Stop timevar. */
2689 if (pass->tv_id)
2690 timevar_pop (pass->tv_id);
2693 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2694 ipa_read_optimization_summaries_1 (pass->sub);
2696 pass = pass->next;
2700 /* Read all the summaries for all_regular_ipa_passes. */
2702 void
2703 ipa_read_optimization_summaries (void)
2705 pass_manager *passes = g->get_passes ();
2706 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2709 /* Same as execute_pass_list but assume that subpasses of IPA passes
2710 are local passes. */
2711 void
2712 execute_ipa_pass_list (opt_pass *pass)
2716 gcc_assert (!current_function_decl);
2717 gcc_assert (!cfun);
2718 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2719 if (execute_one_pass (pass) && pass->sub)
2721 if (pass->sub->type == GIMPLE_PASS)
2723 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2724 do_per_function_toporder ((void (*)(function *, void *))
2725 execute_pass_list,
2726 pass->sub);
2727 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2729 else if (pass->sub->type == SIMPLE_IPA_PASS
2730 || pass->sub->type == IPA_PASS)
2731 execute_ipa_pass_list (pass->sub);
2732 else
2733 gcc_unreachable ();
2735 gcc_assert (!current_function_decl);
2736 symtab->process_new_functions ();
2737 pass = pass->next;
2739 while (pass);
2742 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2744 static void
2745 execute_ipa_stmt_fixups (opt_pass *pass,
2746 struct cgraph_node *node, gimple *stmts)
2748 while (pass)
2750 /* Execute all of the IPA_PASSes in the list. */
2751 if (pass->type == IPA_PASS
2752 && pass->gate (cfun))
2754 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2756 if (ipa_pass->stmt_fixup)
2758 pass_init_dump_file (pass);
2759 /* If a timevar is present, start it. */
2760 if (pass->tv_id)
2761 timevar_push (pass->tv_id);
2763 current_pass = pass;
2764 ipa_pass->stmt_fixup (node, stmts);
2766 /* Stop timevar. */
2767 if (pass->tv_id)
2768 timevar_pop (pass->tv_id);
2769 pass_fini_dump_file (pass);
2771 if (pass->sub)
2772 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2774 pass = pass->next;
2778 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2780 void
2781 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2783 pass_manager *passes = g->get_passes ();
2784 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2788 extern void debug_properties (unsigned int);
2789 extern void dump_properties (FILE *, unsigned int);
2791 DEBUG_FUNCTION void
2792 dump_properties (FILE *dump, unsigned int props)
2794 fprintf (dump, "Properties:\n");
2795 if (props & PROP_gimple_any)
2796 fprintf (dump, "PROP_gimple_any\n");
2797 if (props & PROP_gimple_lcf)
2798 fprintf (dump, "PROP_gimple_lcf\n");
2799 if (props & PROP_gimple_leh)
2800 fprintf (dump, "PROP_gimple_leh\n");
2801 if (props & PROP_cfg)
2802 fprintf (dump, "PROP_cfg\n");
2803 if (props & PROP_ssa)
2804 fprintf (dump, "PROP_ssa\n");
2805 if (props & PROP_no_crit_edges)
2806 fprintf (dump, "PROP_no_crit_edges\n");
2807 if (props & PROP_rtl)
2808 fprintf (dump, "PROP_rtl\n");
2809 if (props & PROP_gimple_lomp)
2810 fprintf (dump, "PROP_gimple_lomp\n");
2811 if (props & PROP_gimple_lcx)
2812 fprintf (dump, "PROP_gimple_lcx\n");
2813 if (props & PROP_gimple_lvec)
2814 fprintf (dump, "PROP_gimple_lvec\n");
2815 if (props & PROP_cfglayout)
2816 fprintf (dump, "PROP_cfglayout\n");
2819 DEBUG_FUNCTION void
2820 debug_properties (unsigned int props)
2822 dump_properties (stderr, props);
2825 /* Called by local passes to see if function is called by already processed nodes.
2826 Because we process nodes in topological order, this means that function is
2827 in recursive cycle or we introduced new direct calls. */
2828 bool
2829 function_called_by_processed_nodes_p (void)
2831 struct cgraph_edge *e;
2832 for (e = cgraph_node::get (current_function_decl)->callers;
2834 e = e->next_caller)
2836 if (e->caller->decl == current_function_decl)
2837 continue;
2838 if (!e->caller->has_gimple_body_p ())
2839 continue;
2840 if (TREE_ASM_WRITTEN (e->caller->decl))
2841 continue;
2842 if (!e->caller->process && !e->caller->global.inlined_to)
2843 break;
2845 if (dump_file && e)
2847 fprintf (dump_file, "Already processed call to:\n");
2848 e->caller->dump (dump_file);
2850 return e != NULL;
2853 #include "gt-passes.h"