2015-09-25 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / passes.c
blobd06a2936905ca6ebecf9d37df5c0c1875c1b4628
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 /* If we created a varpool node for the decl make sure to
323 call early_global_decl. Otherwise we miss changes
324 introduced by member definitions like
325 struct A { static int staticdatamember; };
326 int A::staticdatamember;
327 and thus have incomplete early debug and late debug
328 called from varpool node removal fails to handle it
329 properly. */
330 || (finalize
331 && TREE_CODE (decl) == VAR_DECL
332 && TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
333 /* Avoid confusing the debug information machinery when there are
334 errors. */
335 && !seen_error ())
336 (*debug_hooks->early_global_decl) (decl);
339 /* Called after finishing a record, union or enumeral type. */
341 void
342 rest_of_type_compilation (tree type, int toplev)
344 /* Avoid confusing the debug information machinery when there are
345 errors. */
346 if (seen_error ())
347 return;
349 timevar_push (TV_SYMOUT);
350 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
351 timevar_pop (TV_SYMOUT);
356 void
357 pass_manager::
358 finish_optimization_passes (void)
360 int i;
361 struct dump_file_info *dfi;
362 char *name;
363 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
365 timevar_push (TV_DUMP);
366 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
368 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
369 end_branch_prob ();
370 dumps->dump_finish (pass_profile_1->static_pass_number);
373 if (optimize > 0)
375 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
376 print_combine_total_stats ();
377 dumps->dump_finish (pass_profile_1->static_pass_number);
380 /* Do whatever is necessary to finish printing the graphs. */
381 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
382 if (dumps->dump_initialized_p (i)
383 && (dfi->pflags & TDF_GRAPH) != 0
384 && (name = dumps->get_dump_file_name (i)) != NULL)
386 finish_graph_dump_file (name);
387 free (name);
390 timevar_pop (TV_DUMP);
393 static unsigned int
394 execute_build_ssa_passes (void)
396 /* Once this pass (and its sub-passes) are complete, all functions
397 will be in SSA form. Technically this state change is happening
398 a tad early, since the sub-passes have not yet run, but since
399 none of the sub-passes are IPA passes and do not create new
400 functions, this is ok. We're setting this value for the benefit
401 of IPA passes that follow. */
402 if (symtab->state < IPA_SSA)
403 symtab->state = IPA_SSA;
404 return 0;
407 namespace {
409 const pass_data pass_data_build_ssa_passes =
411 SIMPLE_IPA_PASS, /* type */
412 "build_ssa_passes", /* name */
413 OPTGROUP_NONE, /* optinfo_flags */
414 TV_EARLY_LOCAL, /* tv_id */
415 0, /* properties_required */
416 0, /* properties_provided */
417 0, /* properties_destroyed */
418 0, /* todo_flags_start */
419 /* todo_flags_finish is executed before subpases. For this reason
420 it makes no sense to remove unreachable functions here. */
421 0, /* todo_flags_finish */
424 class pass_build_ssa_passes : public simple_ipa_opt_pass
426 public:
427 pass_build_ssa_passes (gcc::context *ctxt)
428 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
431 /* opt_pass methods: */
432 virtual bool gate (function *)
434 /* Don't bother doing anything if the program has errors. */
435 return (!seen_error () && !in_lto_p);
438 virtual unsigned int execute (function *)
440 return execute_build_ssa_passes ();
443 }; // class pass_build_ssa_passes
445 const pass_data pass_data_chkp_instrumentation_passes =
447 SIMPLE_IPA_PASS, /* type */
448 "chkp_passes", /* name */
449 OPTGROUP_NONE, /* optinfo_flags */
450 TV_NONE, /* tv_id */
451 0, /* properties_required */
452 0, /* properties_provided */
453 0, /* properties_destroyed */
454 0, /* todo_flags_start */
455 0, /* todo_flags_finish */
458 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
460 public:
461 pass_chkp_instrumentation_passes (gcc::context *ctxt)
462 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
465 /* opt_pass methods: */
466 virtual bool gate (function *)
468 /* Don't bother doing anything if the program has errors. */
469 return (flag_check_pointer_bounds
470 && !seen_error () && !in_lto_p);
473 }; // class pass_chkp_instrumentation_passes
475 const pass_data pass_data_local_optimization_passes =
477 SIMPLE_IPA_PASS, /* type */
478 "opt_local_passes", /* name */
479 OPTGROUP_NONE, /* optinfo_flags */
480 TV_NONE, /* tv_id */
481 0, /* properties_required */
482 0, /* properties_provided */
483 0, /* properties_destroyed */
484 0, /* todo_flags_start */
485 0, /* todo_flags_finish */
488 class pass_local_optimization_passes : public simple_ipa_opt_pass
490 public:
491 pass_local_optimization_passes (gcc::context *ctxt)
492 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
495 /* opt_pass methods: */
496 virtual bool gate (function *)
498 /* Don't bother doing anything if the program has errors. */
499 return (!seen_error () && !in_lto_p);
502 }; // class pass_local_optimization_passes
504 } // anon namespace
506 simple_ipa_opt_pass *
507 make_pass_build_ssa_passes (gcc::context *ctxt)
509 return new pass_build_ssa_passes (ctxt);
512 simple_ipa_opt_pass *
513 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
515 return new pass_chkp_instrumentation_passes (ctxt);
518 simple_ipa_opt_pass *
519 make_pass_local_optimization_passes (gcc::context *ctxt)
521 return new pass_local_optimization_passes (ctxt);
524 namespace {
526 const pass_data pass_data_all_early_optimizations =
528 GIMPLE_PASS, /* type */
529 "early_optimizations", /* name */
530 OPTGROUP_NONE, /* optinfo_flags */
531 TV_NONE, /* tv_id */
532 0, /* properties_required */
533 0, /* properties_provided */
534 0, /* properties_destroyed */
535 0, /* todo_flags_start */
536 0, /* todo_flags_finish */
539 class pass_all_early_optimizations : public gimple_opt_pass
541 public:
542 pass_all_early_optimizations (gcc::context *ctxt)
543 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
546 /* opt_pass methods: */
547 virtual bool gate (function *)
549 return (optimize >= 1
550 /* Don't bother doing anything if the program has errors. */
551 && !seen_error ());
554 }; // class pass_all_early_optimizations
556 } // anon namespace
558 static gimple_opt_pass *
559 make_pass_all_early_optimizations (gcc::context *ctxt)
561 return new pass_all_early_optimizations (ctxt);
564 namespace {
566 const pass_data pass_data_all_optimizations =
568 GIMPLE_PASS, /* type */
569 "*all_optimizations", /* name */
570 OPTGROUP_NONE, /* optinfo_flags */
571 TV_OPTIMIZE, /* tv_id */
572 0, /* properties_required */
573 0, /* properties_provided */
574 0, /* properties_destroyed */
575 0, /* todo_flags_start */
576 0, /* todo_flags_finish */
579 class pass_all_optimizations : public gimple_opt_pass
581 public:
582 pass_all_optimizations (gcc::context *ctxt)
583 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
586 /* opt_pass methods: */
587 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
589 }; // class pass_all_optimizations
591 } // anon namespace
593 static gimple_opt_pass *
594 make_pass_all_optimizations (gcc::context *ctxt)
596 return new pass_all_optimizations (ctxt);
599 namespace {
601 const pass_data pass_data_all_optimizations_g =
603 GIMPLE_PASS, /* type */
604 "*all_optimizations_g", /* name */
605 OPTGROUP_NONE, /* optinfo_flags */
606 TV_OPTIMIZE, /* tv_id */
607 0, /* properties_required */
608 0, /* properties_provided */
609 0, /* properties_destroyed */
610 0, /* todo_flags_start */
611 0, /* todo_flags_finish */
614 class pass_all_optimizations_g : public gimple_opt_pass
616 public:
617 pass_all_optimizations_g (gcc::context *ctxt)
618 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
621 /* opt_pass methods: */
622 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
624 }; // class pass_all_optimizations_g
626 } // anon namespace
628 static gimple_opt_pass *
629 make_pass_all_optimizations_g (gcc::context *ctxt)
631 return new pass_all_optimizations_g (ctxt);
634 namespace {
636 const pass_data pass_data_rest_of_compilation =
638 RTL_PASS, /* type */
639 "*rest_of_compilation", /* name */
640 OPTGROUP_NONE, /* optinfo_flags */
641 TV_REST_OF_COMPILATION, /* tv_id */
642 PROP_rtl, /* properties_required */
643 0, /* properties_provided */
644 0, /* properties_destroyed */
645 0, /* todo_flags_start */
646 0, /* todo_flags_finish */
649 class pass_rest_of_compilation : public rtl_opt_pass
651 public:
652 pass_rest_of_compilation (gcc::context *ctxt)
653 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
656 /* opt_pass methods: */
657 virtual bool gate (function *)
659 /* Early return if there were errors. We can run afoul of our
660 consistency checks, and there's not really much point in fixing them. */
661 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
664 }; // class pass_rest_of_compilation
666 } // anon namespace
668 static rtl_opt_pass *
669 make_pass_rest_of_compilation (gcc::context *ctxt)
671 return new pass_rest_of_compilation (ctxt);
674 namespace {
676 const pass_data pass_data_postreload =
678 RTL_PASS, /* type */
679 "*all-postreload", /* name */
680 OPTGROUP_NONE, /* optinfo_flags */
681 TV_POSTRELOAD, /* tv_id */
682 PROP_rtl, /* properties_required */
683 0, /* properties_provided */
684 0, /* properties_destroyed */
685 0, /* todo_flags_start */
686 0, /* todo_flags_finish */
689 class pass_postreload : public rtl_opt_pass
691 public:
692 pass_postreload (gcc::context *ctxt)
693 : rtl_opt_pass (pass_data_postreload, ctxt)
696 /* opt_pass methods: */
697 virtual bool gate (function *) { return reload_completed; }
699 }; // class pass_postreload
701 } // anon namespace
703 static rtl_opt_pass *
704 make_pass_postreload (gcc::context *ctxt)
706 return new pass_postreload (ctxt);
709 namespace {
711 const pass_data pass_data_late_compilation =
713 RTL_PASS, /* type */
714 "*all-late_compilation", /* name */
715 OPTGROUP_NONE, /* optinfo_flags */
716 TV_LATE_COMPILATION, /* tv_id */
717 PROP_rtl, /* properties_required */
718 0, /* properties_provided */
719 0, /* properties_destroyed */
720 0, /* todo_flags_start */
721 0, /* todo_flags_finish */
724 class pass_late_compilation : public rtl_opt_pass
726 public:
727 pass_late_compilation (gcc::context *ctxt)
728 : rtl_opt_pass (pass_data_late_compilation, ctxt)
731 /* opt_pass methods: */
732 virtual bool gate (function *)
734 return reload_completed || targetm.no_register_allocation;
737 }; // class pass_late_compilation
739 } // anon namespace
741 static rtl_opt_pass *
742 make_pass_late_compilation (gcc::context *ctxt)
744 return new pass_late_compilation (ctxt);
749 /* Set the static pass number of pass PASS to ID and record that
750 in the mapping from static pass number to pass. */
752 void
753 pass_manager::
754 set_pass_for_id (int id, opt_pass *pass)
756 pass->static_pass_number = id;
757 if (passes_by_id_size <= id)
759 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
760 memset (passes_by_id + passes_by_id_size, 0,
761 (id + 1 - passes_by_id_size) * sizeof (void *));
762 passes_by_id_size = id + 1;
764 passes_by_id[id] = pass;
767 /* Return the pass with the static pass number ID. */
769 opt_pass *
770 pass_manager::get_pass_for_id (int id) const
772 if (id >= passes_by_id_size)
773 return NULL;
774 return passes_by_id[id];
777 /* Iterate over the pass tree allocating dump file numbers. We want
778 to do this depth first, and independent of whether the pass is
779 enabled or not. */
781 void
782 register_one_dump_file (opt_pass *pass)
784 g->get_passes ()->register_one_dump_file (pass);
787 void
788 pass_manager::register_one_dump_file (opt_pass *pass)
790 char *dot_name, *flag_name, *glob_name;
791 const char *name, *full_name, *prefix;
792 char num[10];
793 int flags, id;
794 int optgroup_flags = OPTGROUP_NONE;
795 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
797 /* See below in next_pass_1. */
798 num[0] = '\0';
799 if (pass->static_pass_number != -1)
800 sprintf (num, "%d", ((int) pass->static_pass_number < 0
801 ? 1 : pass->static_pass_number));
803 /* The name is both used to identify the pass for the purposes of plugins,
804 and to specify dump file name and option.
805 The latter two might want something short which is not quite unique; for
806 that reason, we may have a disambiguating prefix, followed by a space
807 to mark the start of the following dump file name / option string. */
808 name = strchr (pass->name, ' ');
809 name = name ? name + 1 : pass->name;
810 dot_name = concat (".", name, num, NULL);
811 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
813 prefix = "ipa-";
814 flags = TDF_IPA;
815 optgroup_flags |= OPTGROUP_IPA;
817 else if (pass->type == GIMPLE_PASS)
819 prefix = "tree-";
820 flags = TDF_TREE;
822 else
824 prefix = "rtl-";
825 flags = TDF_RTL;
828 flag_name = concat (prefix, name, num, NULL);
829 glob_name = concat (prefix, name, NULL);
830 optgroup_flags |= pass->optinfo_flags;
831 /* For any passes that do not have an optgroup set, and which are not
832 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
833 any dump messages are emitted properly under -fopt-info(-optall). */
834 if (optgroup_flags == OPTGROUP_NONE)
835 optgroup_flags = OPTGROUP_OTHER;
836 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
837 optgroup_flags,
838 true);
839 set_pass_for_id (id, pass);
840 full_name = concat (prefix, pass->name, num, NULL);
841 register_pass_name (pass, full_name);
842 free (CONST_CAST (char *, full_name));
845 /* Register the dump files for the pass_manager starting at PASS. */
847 void
848 pass_manager::register_dump_files (opt_pass *pass)
852 if (pass->name && pass->name[0] != '*')
853 register_one_dump_file (pass);
855 if (pass->sub)
856 register_dump_files (pass->sub);
858 pass = pass->next;
860 while (pass);
863 static hash_map<nofree_string_hash, opt_pass *> *name_to_pass_map;
865 /* Register PASS with NAME. */
867 static void
868 register_pass_name (opt_pass *pass, const char *name)
870 if (!name_to_pass_map)
871 name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
873 if (name_to_pass_map->get (name))
874 return; /* Ignore plugin passes. */
876 const char *unique_name = xstrdup (name);
877 name_to_pass_map->put (unique_name, pass);
880 /* Map from pass id to canonicalized pass name. */
882 typedef const char *char_ptr;
883 static vec<char_ptr> pass_tab = vNULL;
885 /* Callback function for traversing NAME_TO_PASS_MAP. */
887 bool
888 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
890 gcc_assert (pass->static_pass_number > 0);
891 gcc_assert (pass_tab.exists ());
893 pass_tab[pass->static_pass_number] = name;
895 return 1;
898 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
899 table for dumping purpose. */
901 static void
902 create_pass_tab (void)
904 if (!flag_dump_passes)
905 return;
907 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
908 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
911 static bool override_gate_status (opt_pass *, tree, bool);
913 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
914 is turned on or not. */
916 static void
917 dump_one_pass (opt_pass *pass, int pass_indent)
919 int indent = 3 * pass_indent;
920 const char *pn;
921 bool is_on, is_really_on;
923 is_on = pass->gate (cfun);
924 is_really_on = override_gate_status (pass, current_function_decl, is_on);
926 if (pass->static_pass_number <= 0)
927 pn = pass->name;
928 else
929 pn = pass_tab[pass->static_pass_number];
931 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
932 (15 - indent < 0 ? 0 : 15 - indent), " ",
933 is_on ? " ON" : " OFF",
934 ((!is_on) == (!is_really_on) ? ""
935 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
938 /* Dump pass list PASS with indentation INDENT. */
940 static void
941 dump_pass_list (opt_pass *pass, int indent)
945 dump_one_pass (pass, indent);
946 if (pass->sub)
947 dump_pass_list (pass->sub, indent + 1);
948 pass = pass->next;
950 while (pass);
953 /* Dump all optimization passes. */
955 void
956 dump_passes (void)
958 g->get_passes ()->dump_passes ();
961 void
962 pass_manager::dump_passes () const
964 push_dummy_function (true);
966 create_pass_tab ();
968 dump_pass_list (all_lowering_passes, 1);
969 dump_pass_list (all_small_ipa_passes, 1);
970 dump_pass_list (all_regular_ipa_passes, 1);
971 dump_pass_list (all_late_ipa_passes, 1);
972 dump_pass_list (all_passes, 1);
974 pop_dummy_function ();
977 /* Returns the pass with NAME. */
979 static opt_pass *
980 get_pass_by_name (const char *name)
982 opt_pass **p = name_to_pass_map->get (name);
983 if (p)
984 return *p;
986 return NULL;
990 /* Range [start, last]. */
992 struct uid_range
994 unsigned int start;
995 unsigned int last;
996 const char *assem_name;
997 struct uid_range *next;
1000 typedef struct uid_range *uid_range_p;
1003 static vec<uid_range_p>
1004 enabled_pass_uid_range_tab = vNULL;
1005 static vec<uid_range_p>
1006 disabled_pass_uid_range_tab = vNULL;
1009 /* Parse option string for -fdisable- and -fenable-
1010 The syntax of the options:
1012 -fenable-<pass_name>
1013 -fdisable-<pass_name>
1015 -fenable-<pass_name>=s1:e1,s2:e2,...
1016 -fdisable-<pass_name>=s1:e1,s2:e2,...
1019 static void
1020 enable_disable_pass (const char *arg, bool is_enable)
1022 opt_pass *pass;
1023 char *range_str, *phase_name;
1024 char *argstr = xstrdup (arg);
1025 vec<uid_range_p> *tab = 0;
1027 range_str = strchr (argstr,'=');
1028 if (range_str)
1030 *range_str = '\0';
1031 range_str++;
1034 phase_name = argstr;
1035 if (!*phase_name)
1037 if (is_enable)
1038 error ("unrecognized option -fenable");
1039 else
1040 error ("unrecognized option -fdisable");
1041 free (argstr);
1042 return;
1044 pass = get_pass_by_name (phase_name);
1045 if (!pass || pass->static_pass_number == -1)
1047 if (is_enable)
1048 error ("unknown pass %s specified in -fenable", phase_name);
1049 else
1050 error ("unknown pass %s specified in -fdisable", phase_name);
1051 free (argstr);
1052 return;
1055 if (is_enable)
1056 tab = &enabled_pass_uid_range_tab;
1057 else
1058 tab = &disabled_pass_uid_range_tab;
1060 if ((unsigned) pass->static_pass_number >= tab->length ())
1061 tab->safe_grow_cleared (pass->static_pass_number + 1);
1063 if (!range_str)
1065 uid_range_p slot;
1066 uid_range_p new_range = XCNEW (struct uid_range);
1068 new_range->start = 0;
1069 new_range->last = (unsigned)-1;
1071 slot = (*tab)[pass->static_pass_number];
1072 new_range->next = slot;
1073 (*tab)[pass->static_pass_number] = new_range;
1074 if (is_enable)
1075 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1076 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1077 else
1078 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1079 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1081 else
1083 char *next_range = NULL;
1084 char *one_range = range_str;
1085 char *end_val = NULL;
1089 uid_range_p slot;
1090 uid_range_p new_range;
1091 char *invalid = NULL;
1092 long start;
1093 char *func_name = NULL;
1095 next_range = strchr (one_range, ',');
1096 if (next_range)
1098 *next_range = '\0';
1099 next_range++;
1102 end_val = strchr (one_range, ':');
1103 if (end_val)
1105 *end_val = '\0';
1106 end_val++;
1108 start = strtol (one_range, &invalid, 10);
1109 if (*invalid || start < 0)
1111 if (end_val || (one_range[0] >= '0'
1112 && one_range[0] <= '9'))
1114 error ("Invalid range %s in option %s",
1115 one_range,
1116 is_enable ? "-fenable" : "-fdisable");
1117 free (argstr);
1118 return;
1120 func_name = one_range;
1122 if (!end_val)
1124 new_range = XCNEW (struct uid_range);
1125 if (!func_name)
1127 new_range->start = (unsigned) start;
1128 new_range->last = (unsigned) start;
1130 else
1132 new_range->start = (unsigned) -1;
1133 new_range->last = (unsigned) -1;
1134 new_range->assem_name = xstrdup (func_name);
1137 else
1139 long last = strtol (end_val, &invalid, 10);
1140 if (*invalid || last < start)
1142 error ("Invalid range %s in option %s",
1143 end_val,
1144 is_enable ? "-fenable" : "-fdisable");
1145 free (argstr);
1146 return;
1148 new_range = XCNEW (struct uid_range);
1149 new_range->start = (unsigned) start;
1150 new_range->last = (unsigned) last;
1153 slot = (*tab)[pass->static_pass_number];
1154 new_range->next = slot;
1155 (*tab)[pass->static_pass_number] = new_range;
1156 if (is_enable)
1158 if (new_range->assem_name)
1159 inform (UNKNOWN_LOCATION,
1160 "enable pass %s for function %s",
1161 phase_name, new_range->assem_name);
1162 else
1163 inform (UNKNOWN_LOCATION,
1164 "enable pass %s for functions in the range of [%u, %u]",
1165 phase_name, new_range->start, new_range->last);
1167 else
1169 if (new_range->assem_name)
1170 inform (UNKNOWN_LOCATION,
1171 "disable pass %s for function %s",
1172 phase_name, new_range->assem_name);
1173 else
1174 inform (UNKNOWN_LOCATION,
1175 "disable pass %s for functions in the range of [%u, %u]",
1176 phase_name, new_range->start, new_range->last);
1179 one_range = next_range;
1180 } while (next_range);
1183 free (argstr);
1186 /* Enable pass specified by ARG. */
1188 void
1189 enable_pass (const char *arg)
1191 enable_disable_pass (arg, true);
1194 /* Disable pass specified by ARG. */
1196 void
1197 disable_pass (const char *arg)
1199 enable_disable_pass (arg, false);
1202 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1204 static bool
1205 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1206 tree func,
1207 vec<uid_range_p> tab)
1209 uid_range_p slot, range;
1210 int cgraph_uid;
1211 const char *aname = NULL;
1213 if (!tab.exists ()
1214 || (unsigned) pass->static_pass_number >= tab.length ()
1215 || pass->static_pass_number == -1)
1216 return false;
1218 slot = tab[pass->static_pass_number];
1219 if (!slot)
1220 return false;
1222 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1223 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1224 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1226 range = slot;
1227 while (range)
1229 if ((unsigned) cgraph_uid >= range->start
1230 && (unsigned) cgraph_uid <= range->last)
1231 return true;
1232 if (range->assem_name && aname
1233 && !strcmp (range->assem_name, aname))
1234 return true;
1235 range = range->next;
1238 return false;
1242 /* Update static_pass_number for passes (and the flag
1243 TODO_mark_first_instance).
1245 Passes are constructed with static_pass_number preinitialized to 0
1247 This field is used in two different ways: initially as instance numbers
1248 of their kind, and then as ids within the entire pass manager.
1250 Within pass_manager::pass_manager:
1252 * In add_pass_instance(), as called by next_pass_1 in
1253 NEXT_PASS in init_optimization_passes
1255 * When the initial instance of a pass within a pass manager is seen,
1256 it is flagged, and its static_pass_number is set to -1
1258 * On subsequent times that it is seen, the static pass number
1259 is decremented each time, so that if there are e.g. 4 dups,
1260 they have static_pass_number -4, 2, 3, 4 respectively (note
1261 how the initial one is negative and gives the count); these
1262 can be thought of as instance numbers of the specific pass
1264 * Within the register_dump_files () traversal, set_pass_for_id()
1265 is called on each pass, using these instance numbers to create
1266 dumpfile switches, and then overwriting them with a pass id,
1267 which are global to the whole pass manager (based on
1268 (TDI_end + current value of extra_dump_files_in_use) ) */
1270 static void
1271 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1272 opt_pass *initial_pass)
1274 /* Are we dealing with the first pass of its kind, or a clone? */
1275 if (new_pass != initial_pass)
1277 /* We're dealing with a clone. */
1278 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1280 /* Indicate to register_dump_files that this pass has duplicates,
1281 and so it should rename the dump file. The first instance will
1282 be -1, and be number of duplicates = -static_pass_number - 1.
1283 Subsequent instances will be > 0 and just the duplicate number. */
1284 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1286 initial_pass->static_pass_number -= 1;
1287 new_pass->static_pass_number = -initial_pass->static_pass_number;
1290 else
1292 /* We're dealing with the first pass of its kind. */
1293 new_pass->todo_flags_start |= TODO_mark_first_instance;
1294 new_pass->static_pass_number = -1;
1296 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1300 /* Add a pass to the pass list. Duplicate the pass if it's already
1301 in the list. */
1303 static opt_pass **
1304 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1306 /* Every pass should have a name so that plugins can refer to them. */
1307 gcc_assert (pass->name != NULL);
1309 add_pass_instance (pass, false, initial_pass);
1310 *list = pass;
1312 return &(*list)->next;
1315 /* List node for an inserted pass instance. We need to keep track of all
1316 the newly-added pass instances (with 'added_pass_nodes' defined below)
1317 so that we can register their dump files after pass-positioning is finished.
1318 Registering dumping files needs to be post-processed or the
1319 static_pass_number of the opt_pass object would be modified and mess up
1320 the dump file names of future pass instances to be added. */
1322 struct pass_list_node
1324 opt_pass *pass;
1325 struct pass_list_node *next;
1328 static struct pass_list_node *added_pass_nodes = NULL;
1329 static struct pass_list_node *prev_added_pass_node;
1331 /* Insert the pass at the proper position. Return true if the pass
1332 is successfully added.
1334 NEW_PASS_INFO - new pass to be inserted
1335 PASS_LIST - root of the pass list to insert the new pass to */
1337 static bool
1338 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1340 opt_pass *pass = *pass_list, *prev_pass = NULL;
1341 bool success = false;
1343 for ( ; pass; prev_pass = pass, pass = pass->next)
1345 /* Check if the current pass is of the same type as the new pass and
1346 matches the name and the instance number of the reference pass. */
1347 if (pass->type == new_pass_info->pass->type
1348 && pass->name
1349 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1350 && ((new_pass_info->ref_pass_instance_number == 0)
1351 || (new_pass_info->ref_pass_instance_number ==
1352 pass->static_pass_number)
1353 || (new_pass_info->ref_pass_instance_number == 1
1354 && pass->todo_flags_start & TODO_mark_first_instance)))
1356 opt_pass *new_pass;
1357 struct pass_list_node *new_pass_node;
1359 if (new_pass_info->ref_pass_instance_number == 0)
1361 new_pass = new_pass_info->pass->clone ();
1362 add_pass_instance (new_pass, true, new_pass_info->pass);
1364 else
1366 new_pass = new_pass_info->pass;
1367 add_pass_instance (new_pass, true, new_pass);
1370 /* Insert the new pass instance based on the positioning op. */
1371 switch (new_pass_info->pos_op)
1373 case PASS_POS_INSERT_AFTER:
1374 new_pass->next = pass->next;
1375 pass->next = new_pass;
1377 /* Skip newly inserted pass to avoid repeated
1378 insertions in the case where the new pass and the
1379 existing one have the same name. */
1380 pass = new_pass;
1381 break;
1382 case PASS_POS_INSERT_BEFORE:
1383 new_pass->next = pass;
1384 if (prev_pass)
1385 prev_pass->next = new_pass;
1386 else
1387 *pass_list = new_pass;
1388 break;
1389 case PASS_POS_REPLACE:
1390 new_pass->next = pass->next;
1391 if (prev_pass)
1392 prev_pass->next = new_pass;
1393 else
1394 *pass_list = new_pass;
1395 new_pass->sub = pass->sub;
1396 new_pass->tv_id = pass->tv_id;
1397 pass = new_pass;
1398 break;
1399 default:
1400 error ("invalid pass positioning operation");
1401 return false;
1404 /* Save the newly added pass (instance) in the added_pass_nodes
1405 list so that we can register its dump file later. Note that
1406 we cannot register the dump file now because doing so will modify
1407 the static_pass_number of the opt_pass object and therefore
1408 mess up the dump file name of future instances. */
1409 new_pass_node = XCNEW (struct pass_list_node);
1410 new_pass_node->pass = new_pass;
1411 if (!added_pass_nodes)
1412 added_pass_nodes = new_pass_node;
1413 else
1414 prev_added_pass_node->next = new_pass_node;
1415 prev_added_pass_node = new_pass_node;
1417 success = true;
1420 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1421 success = true;
1424 return success;
1427 /* Hooks a new pass into the pass lists.
1429 PASS_INFO - pass information that specifies the opt_pass object,
1430 reference pass, instance number, and how to position
1431 the pass */
1433 void
1434 register_pass (struct register_pass_info *pass_info)
1436 g->get_passes ()->register_pass (pass_info);
1439 void
1440 register_pass (opt_pass* pass, pass_positioning_ops pos,
1441 const char* ref_pass_name, int ref_pass_inst_number)
1443 register_pass_info i;
1444 i.pass = pass;
1445 i.reference_pass_name = ref_pass_name;
1446 i.ref_pass_instance_number = ref_pass_inst_number;
1447 i.pos_op = pos;
1449 g->get_passes ()->register_pass (&i);
1452 void
1453 pass_manager::register_pass (struct register_pass_info *pass_info)
1455 bool all_instances, success;
1456 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1458 /* The checks below could fail in buggy plugins. Existing GCC
1459 passes should never fail these checks, so we mention plugin in
1460 the messages. */
1461 if (!pass_info->pass)
1462 fatal_error (input_location, "plugin cannot register a missing pass");
1464 if (!pass_info->pass->name)
1465 fatal_error (input_location, "plugin cannot register an unnamed pass");
1467 if (!pass_info->reference_pass_name)
1468 fatal_error
1469 (input_location,
1470 "plugin cannot register pass %qs without reference pass name",
1471 pass_info->pass->name);
1473 /* Try to insert the new pass to the pass lists. We need to check
1474 all five lists as the reference pass could be in one (or all) of
1475 them. */
1476 all_instances = pass_info->ref_pass_instance_number == 0;
1477 success = position_pass (pass_info, &all_lowering_passes);
1478 if (!success || all_instances)
1479 success |= position_pass (pass_info, &all_small_ipa_passes);
1480 if (!success || all_instances)
1481 success |= position_pass (pass_info, &all_regular_ipa_passes);
1482 if (!success || all_instances)
1483 success |= position_pass (pass_info, &all_late_ipa_passes);
1484 if (!success || all_instances)
1485 success |= position_pass (pass_info, &all_passes);
1486 if (!success)
1487 fatal_error
1488 (input_location,
1489 "pass %qs not found but is referenced by new pass %qs",
1490 pass_info->reference_pass_name, pass_info->pass->name);
1492 /* OK, we have successfully inserted the new pass. We need to register
1493 the dump files for the newly added pass and its duplicates (if any).
1494 Because the registration of plugin/backend passes happens after the
1495 command-line options are parsed, the options that specify single
1496 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1497 passes. Therefore we currently can only enable dumping of
1498 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1499 are specified. While doing so, we also delete the pass_list_node
1500 objects created during pass positioning. */
1501 while (added_pass_nodes)
1503 struct pass_list_node *next_node = added_pass_nodes->next;
1504 enum tree_dump_index tdi;
1505 register_one_dump_file (added_pass_nodes->pass);
1506 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1507 || added_pass_nodes->pass->type == IPA_PASS)
1508 tdi = TDI_ipa_all;
1509 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1510 tdi = TDI_tree_all;
1511 else
1512 tdi = TDI_rtl_all;
1513 /* Check if dump-all flag is specified. */
1514 if (dumps->get_dump_file_info (tdi)->pstate)
1515 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1516 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1517 XDELETE (added_pass_nodes);
1518 added_pass_nodes = next_node;
1522 /* Construct the pass tree. The sequencing of passes is driven by
1523 the cgraph routines:
1525 finalize_compilation_unit ()
1526 for each node N in the cgraph
1527 cgraph_analyze_function (N)
1528 cgraph_lower_function (N) -> all_lowering_passes
1530 If we are optimizing, compile is then invoked:
1532 compile ()
1533 ipa_passes () -> all_small_ipa_passes
1534 -> Analysis of all_regular_ipa_passes
1535 * possible LTO streaming at copmilation time *
1536 -> Execution of all_regular_ipa_passes
1537 * possible LTO streaming at link time *
1538 -> all_late_ipa_passes
1539 expand_all_functions ()
1540 for each node N in the cgraph
1541 expand_function (N) -> Transformation of all_regular_ipa_passes
1542 -> all_passes
1545 void *
1546 pass_manager::operator new (size_t sz)
1548 /* Ensure that all fields of the pass manager are zero-initialized. */
1549 return xcalloc (1, sz);
1552 void
1553 pass_manager::operator delete (void *ptr)
1555 free (ptr);
1558 pass_manager::pass_manager (context *ctxt)
1559 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1560 all_regular_ipa_passes (NULL),
1561 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1562 m_ctxt (ctxt)
1564 opt_pass **p;
1566 /* Initialize the pass_lists array. */
1567 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1568 GCC_PASS_LISTS
1569 #undef DEF_PASS_LIST
1571 /* Build the tree of passes. */
1573 #define INSERT_PASSES_AFTER(PASS) \
1574 p = &(PASS);
1576 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1578 opt_pass **p = &(PASS ## _1)->sub;
1580 #define POP_INSERT_PASSES() \
1583 #define NEXT_PASS(PASS, NUM) \
1584 do { \
1585 gcc_assert (NULL == PASS ## _ ## NUM); \
1586 if ((NUM) == 1) \
1587 PASS ## _1 = make_##PASS (m_ctxt); \
1588 else \
1590 gcc_assert (PASS ## _1); \
1591 PASS ## _ ## NUM = PASS ## _1->clone (); \
1593 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1594 } while (0)
1596 #define TERMINATE_PASS_LIST() \
1597 *p = NULL;
1599 #include "pass-instances.def"
1601 #undef INSERT_PASSES_AFTER
1602 #undef PUSH_INSERT_PASSES_WITHIN
1603 #undef POP_INSERT_PASSES
1604 #undef NEXT_PASS
1605 #undef TERMINATE_PASS_LIST
1607 /* Register the passes with the tree dump code. */
1608 register_dump_files (all_lowering_passes);
1609 register_dump_files (all_small_ipa_passes);
1610 register_dump_files (all_regular_ipa_passes);
1611 register_dump_files (all_late_ipa_passes);
1612 register_dump_files (all_passes);
1615 static void
1616 delete_pass_tree (opt_pass *pass)
1618 while (pass)
1620 /* Recurse into child passes. */
1621 delete_pass_tree (pass->sub);
1623 opt_pass *next = pass->next;
1625 /* Delete this pass. */
1626 delete pass;
1628 /* Iterate onto sibling passes. */
1629 pass = next;
1633 pass_manager::~pass_manager ()
1635 XDELETEVEC (passes_by_id);
1637 /* Call delete_pass_tree on each of the pass_lists. */
1638 #define DEF_PASS_LIST(LIST) \
1639 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1640 GCC_PASS_LISTS
1641 #undef DEF_PASS_LIST
1645 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1646 function CALLBACK for every function in the call graph. Otherwise,
1647 call CALLBACK on the current function. */
1649 static void
1650 do_per_function (void (*callback) (function *, void *data), void *data)
1652 if (current_function_decl)
1653 callback (cfun, data);
1654 else
1656 struct cgraph_node *node;
1657 FOR_EACH_DEFINED_FUNCTION (node)
1658 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1659 && (!node->clone_of || node->decl != node->clone_of->decl))
1660 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1664 /* Because inlining might remove no-longer reachable nodes, we need to
1665 keep the array visible to garbage collector to avoid reading collected
1666 out nodes. */
1667 static int nnodes;
1668 static GTY ((length ("nnodes"))) cgraph_node **order;
1670 /* Hook called when NODE is removed and therefore should be
1671 excluded from order vector. DATA is an array of integers.
1672 DATA[0] holds max index it may be accessed by. For cgraph
1673 node DATA[node->uid + 1] holds index of this node in order
1674 vector. */
1675 static void
1676 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1678 int *order_idx = (int *)data;
1680 if (node->uid >= order_idx[0])
1681 return;
1683 int idx = order_idx[node->uid + 1];
1684 if (idx >= 0 && idx < nnodes && order[idx] == node)
1685 order[idx] = NULL;
1688 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1689 function CALLBACK for every function in the call graph. Otherwise,
1690 call CALLBACK on the current function.
1691 This function is global so that plugins can use it. */
1692 void
1693 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1695 int i;
1697 if (current_function_decl)
1698 callback (cfun, data);
1699 else
1701 cgraph_node_hook_list *hook;
1702 int *order_idx;
1703 gcc_assert (!order);
1704 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1706 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1707 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1708 order_idx[0] = symtab->cgraph_max_uid;
1710 nnodes = ipa_reverse_postorder (order);
1711 for (i = nnodes - 1; i >= 0; i--)
1713 order[i]->process = 1;
1714 order_idx[order[i]->uid + 1] = i;
1716 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1717 order_idx);
1718 for (i = nnodes - 1; i >= 0; i--)
1720 /* Function could be inlined and removed as unreachable. */
1721 if (!order[i])
1722 continue;
1724 struct cgraph_node *node = order[i];
1726 /* Allow possibly removed nodes to be garbage collected. */
1727 order[i] = NULL;
1728 node->process = 0;
1729 if (node->has_gimple_body_p ())
1730 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1732 symtab->remove_cgraph_removal_hook (hook);
1734 ggc_free (order);
1735 order = NULL;
1736 nnodes = 0;
1739 /* Helper function to perform function body dump. */
1741 static void
1742 execute_function_dump (function *fn, void *data)
1744 opt_pass *pass = (opt_pass *)data;
1746 if (dump_file)
1748 push_cfun (fn);
1750 if (fn->curr_properties & PROP_trees)
1751 dump_function_to_file (fn->decl, dump_file, dump_flags);
1752 else
1753 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1755 /* Flush the file. If verification fails, we won't be able to
1756 close the file before aborting. */
1757 fflush (dump_file);
1759 if ((fn->curr_properties & PROP_cfg)
1760 && (dump_flags & TDF_GRAPH))
1762 if (!pass->graph_dump_initialized)
1764 clean_graph_dump_file (dump_file_name);
1765 pass->graph_dump_initialized = true;
1767 print_graph_cfg (dump_file_name, fn);
1770 pop_cfun ();
1774 static struct profile_record *profile_record;
1776 /* Do profile consistency book-keeping for the pass with static number INDEX.
1777 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1778 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1779 if we are only book-keeping on passes that may have selectively disabled
1780 themselves on a given function. */
1781 static void
1782 check_profile_consistency (int index, int subpass, bool run)
1784 pass_manager *passes = g->get_passes ();
1785 if (index == -1)
1786 return;
1787 if (!profile_record)
1788 profile_record = XCNEWVEC (struct profile_record,
1789 passes->passes_by_id_size);
1790 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1791 gcc_assert (subpass < 2);
1792 profile_record[index].run |= run;
1793 account_profile_record (&profile_record[index], subpass);
1796 /* Output profile consistency. */
1798 void
1799 dump_profile_report (void)
1801 g->get_passes ()->dump_profile_report ();
1804 void
1805 pass_manager::dump_profile_report () const
1807 int i, j;
1808 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1809 gcov_type last_time = 0, last_size = 0;
1810 double rel_time_change, rel_size_change;
1811 int last_reported = 0;
1813 if (!profile_record)
1814 return;
1815 fprintf (stderr, "\nProfile consistency report:\n\n");
1816 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1817 fprintf (stderr, " |freq count |freq count |size time\n");
1819 for (i = 0; i < passes_by_id_size; i++)
1820 for (j = 0 ; j < 2; j++)
1821 if (profile_record[i].run)
1823 if (last_time)
1824 rel_time_change = (profile_record[i].time[j]
1825 - (double)last_time) * 100 / (double)last_time;
1826 else
1827 rel_time_change = 0;
1828 if (last_size)
1829 rel_size_change = (profile_record[i].size[j]
1830 - (double)last_size) * 100 / (double)last_size;
1831 else
1832 rel_size_change = 0;
1834 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1835 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1836 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1837 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1838 || rel_time_change || rel_size_change)
1840 last_reported = i;
1841 fprintf (stderr, "%-20s %s",
1842 passes_by_id [i]->name,
1843 j ? "(after TODO)" : " ");
1844 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1845 fprintf (stderr, "| %+5i",
1846 profile_record[i].num_mismatched_freq_in[j]
1847 - last_freq_in);
1848 else
1849 fprintf (stderr, "| ");
1850 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1851 fprintf (stderr, " %+5i",
1852 profile_record[i].num_mismatched_count_in[j]
1853 - last_count_in);
1854 else
1855 fprintf (stderr, " ");
1856 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1857 fprintf (stderr, "| %+5i",
1858 profile_record[i].num_mismatched_freq_out[j]
1859 - last_freq_out);
1860 else
1861 fprintf (stderr, "| ");
1862 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1863 fprintf (stderr, " %+5i",
1864 profile_record[i].num_mismatched_count_out[j]
1865 - last_count_out);
1866 else
1867 fprintf (stderr, " ");
1869 /* Size/time units change across gimple and RTL. */
1870 if (i == pass_expand_1->static_pass_number)
1871 fprintf (stderr, "|----------");
1872 else
1874 if (rel_size_change)
1875 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1876 else
1877 fprintf (stderr, "| ");
1878 if (rel_time_change)
1879 fprintf (stderr, " %+8.4f%%", rel_time_change);
1881 fprintf (stderr, "\n");
1882 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1883 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1884 last_count_in = profile_record[i].num_mismatched_count_in[j];
1885 last_count_out = profile_record[i].num_mismatched_count_out[j];
1887 else if (j && last_reported != i)
1889 last_reported = i;
1890 fprintf (stderr, "%-20s ------------| | |\n",
1891 passes_by_id [i]->name);
1893 last_time = profile_record[i].time[j];
1894 last_size = profile_record[i].size[j];
1898 /* Perform all TODO actions that ought to be done on each function. */
1900 static void
1901 execute_function_todo (function *fn, void *data)
1903 bool from_ipa_pass = (cfun == NULL);
1904 unsigned int flags = (size_t)data;
1905 flags &= ~fn->last_verified;
1906 if (!flags)
1907 return;
1909 push_cfun (fn);
1911 /* Always cleanup the CFG before trying to update SSA. */
1912 if (flags & TODO_cleanup_cfg)
1914 cleanup_tree_cfg ();
1916 /* When cleanup_tree_cfg merges consecutive blocks, it may
1917 perform some simplistic propagation when removing single
1918 valued PHI nodes. This propagation may, in turn, cause the
1919 SSA form to become out-of-date (see PR 22037). So, even
1920 if the parent pass had not scheduled an SSA update, we may
1921 still need to do one. */
1922 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1923 flags |= TODO_update_ssa;
1926 if (flags & TODO_update_ssa_any)
1928 unsigned update_flags = flags & TODO_update_ssa_any;
1929 update_ssa (update_flags);
1932 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1933 compute_may_aliases ();
1935 if (optimize && (flags & TODO_update_address_taken))
1936 execute_update_addresses_taken ();
1938 if (flags & TODO_remove_unused_locals)
1939 remove_unused_locals ();
1941 if (flags & TODO_rebuild_frequencies)
1942 rebuild_frequencies ();
1944 if (flags & TODO_rebuild_cgraph_edges)
1945 cgraph_edge::rebuild_edges ();
1947 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1948 /* If we've seen errors do not bother running any verifiers. */
1949 if (!seen_error ())
1951 #if defined ENABLE_CHECKING
1952 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1953 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1955 if (flags & TODO_verify_il)
1957 if (cfun->curr_properties & PROP_trees)
1959 if (cfun->curr_properties & PROP_cfg)
1960 /* IPA passes leave stmts to be fixed up, so make sure to
1961 not verify stmts really throw. */
1962 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1963 else
1964 verify_gimple_in_seq (gimple_body (cfun->decl));
1966 if (cfun->curr_properties & PROP_ssa)
1967 /* IPA passes leave stmts to be fixed up, so make sure to
1968 not verify SSA operands whose verifier will choke on that. */
1969 verify_ssa (true, !from_ipa_pass);
1970 /* IPA passes leave basic-blocks unsplit, so make sure to
1971 not trip on that. */
1972 if ((cfun->curr_properties & PROP_cfg)
1973 && !from_ipa_pass)
1974 verify_flow_info ();
1975 if (current_loops
1976 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1977 verify_loop_closed_ssa (false);
1978 if (cfun->curr_properties & PROP_rtl)
1979 verify_rtl_sharing ();
1982 /* Make sure verifiers don't change dominator state. */
1983 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1984 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1985 #endif
1988 fn->last_verified = flags & TODO_verify_all;
1990 pop_cfun ();
1992 /* For IPA passes make sure to release dominator info, it can be
1993 computed by non-verifying TODOs. */
1994 if (from_ipa_pass)
1996 free_dominance_info (fn, CDI_DOMINATORS);
1997 free_dominance_info (fn, CDI_POST_DOMINATORS);
2001 /* Perform all TODO actions. */
2002 static void
2003 execute_todo (unsigned int flags)
2005 #if defined ENABLE_CHECKING
2006 if (cfun
2007 && need_ssa_update_p (cfun))
2008 gcc_assert (flags & TODO_update_ssa_any);
2009 #endif
2011 timevar_push (TV_TODO);
2013 /* Inform the pass whether it is the first time it is run. */
2014 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2016 statistics_fini_pass ();
2018 if (flags)
2019 do_per_function (execute_function_todo, (void *)(size_t) flags);
2021 /* Always remove functions just as before inlining: IPA passes might be
2022 interested to see bodies of extern inline functions that are not inlined
2023 to analyze side effects. The full removal is done just at the end
2024 of IPA pass queue. */
2025 if (flags & TODO_remove_functions)
2027 gcc_assert (!cfun);
2028 symtab->remove_unreachable_nodes (dump_file);
2031 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2033 gcc_assert (!cfun);
2034 symtab_node::dump_table (dump_file);
2035 /* Flush the file. If verification fails, we won't be able to
2036 close the file before aborting. */
2037 fflush (dump_file);
2040 /* Now that the dumping has been done, we can get rid of the optional
2041 df problems. */
2042 if (flags & TODO_df_finish)
2043 df_finish_pass ((flags & TODO_df_verify) != 0);
2045 timevar_pop (TV_TODO);
2048 /* Verify invariants that should hold between passes. This is a place
2049 to put simple sanity checks. */
2051 static void
2052 verify_interpass_invariants (void)
2054 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2057 /* Clear the last verified flag. */
2059 static void
2060 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2062 fn->last_verified = 0;
2065 /* Helper function. Verify that the properties has been turn into the
2066 properties expected by the pass. */
2068 #ifdef ENABLE_CHECKING
2069 static void
2070 verify_curr_properties (function *fn, void *data)
2072 unsigned int props = (size_t)data;
2073 gcc_assert ((fn->curr_properties & props) == props);
2075 #endif
2077 /* Initialize pass dump file. */
2078 /* This is non-static so that the plugins can use it. */
2080 bool
2081 pass_init_dump_file (opt_pass *pass)
2083 /* If a dump file name is present, open it if enabled. */
2084 if (pass->static_pass_number != -1)
2086 timevar_push (TV_DUMP);
2087 gcc::dump_manager *dumps = g->get_dumps ();
2088 bool initializing_dump =
2089 !dumps->dump_initialized_p (pass->static_pass_number);
2090 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2091 dumps->dump_start (pass->static_pass_number, &dump_flags);
2092 if (dump_file && current_function_decl)
2093 dump_function_header (dump_file, current_function_decl, dump_flags);
2094 if (initializing_dump
2095 && dump_file && (dump_flags & TDF_GRAPH)
2096 && cfun && (cfun->curr_properties & PROP_cfg))
2098 clean_graph_dump_file (dump_file_name);
2099 pass->graph_dump_initialized = true;
2101 timevar_pop (TV_DUMP);
2102 return initializing_dump;
2104 else
2105 return false;
2108 /* Flush PASS dump file. */
2109 /* This is non-static so that plugins can use it. */
2111 void
2112 pass_fini_dump_file (opt_pass *pass)
2114 timevar_push (TV_DUMP);
2116 /* Flush and close dump file. */
2117 if (dump_file_name)
2119 free (CONST_CAST (char *, dump_file_name));
2120 dump_file_name = NULL;
2123 g->get_dumps ()->dump_finish (pass->static_pass_number);
2124 timevar_pop (TV_DUMP);
2127 /* After executing the pass, apply expected changes to the function
2128 properties. */
2130 static void
2131 update_properties_after_pass (function *fn, void *data)
2133 opt_pass *pass = (opt_pass *) data;
2134 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2135 & ~pass->properties_destroyed;
2138 /* Execute summary generation for all of the passes in IPA_PASS. */
2140 void
2141 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2143 while (ipa_pass)
2145 opt_pass *pass = ipa_pass;
2147 /* Execute all of the IPA_PASSes in the list. */
2148 if (ipa_pass->type == IPA_PASS
2149 && pass->gate (cfun)
2150 && ipa_pass->generate_summary)
2152 pass_init_dump_file (pass);
2154 /* If a timevar is present, start it. */
2155 if (pass->tv_id)
2156 timevar_push (pass->tv_id);
2158 current_pass = pass;
2159 ipa_pass->generate_summary ();
2161 /* Stop timevar. */
2162 if (pass->tv_id)
2163 timevar_pop (pass->tv_id);
2165 pass_fini_dump_file (pass);
2167 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2171 /* Execute IPA_PASS function transform on NODE. */
2173 static void
2174 execute_one_ipa_transform_pass (struct cgraph_node *node,
2175 ipa_opt_pass_d *ipa_pass)
2177 opt_pass *pass = ipa_pass;
2178 unsigned int todo_after = 0;
2180 current_pass = pass;
2181 if (!ipa_pass->function_transform)
2182 return;
2184 /* Note that the folders should only create gimple expressions.
2185 This is a hack until the new folder is ready. */
2186 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2188 pass_init_dump_file (pass);
2190 /* Run pre-pass verification. */
2191 execute_todo (ipa_pass->function_transform_todo_flags_start);
2193 /* If a timevar is present, start it. */
2194 if (pass->tv_id != TV_NONE)
2195 timevar_push (pass->tv_id);
2197 /* Do it! */
2198 todo_after = ipa_pass->function_transform (node);
2200 /* Stop timevar. */
2201 if (pass->tv_id != TV_NONE)
2202 timevar_pop (pass->tv_id);
2204 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2205 check_profile_consistency (pass->static_pass_number, 0, true);
2207 /* Run post-pass cleanup and verification. */
2208 execute_todo (todo_after);
2209 verify_interpass_invariants ();
2210 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2211 check_profile_consistency (pass->static_pass_number, 1, true);
2213 if (dump_file)
2214 do_per_function (execute_function_dump, NULL);
2215 pass_fini_dump_file (pass);
2217 current_pass = NULL;
2219 /* Signal this is a suitable GC collection point. */
2220 if (!(todo_after & TODO_do_not_ggc_collect))
2221 ggc_collect ();
2224 /* For the current function, execute all ipa transforms. */
2226 void
2227 execute_all_ipa_transforms (void)
2229 struct cgraph_node *node;
2230 if (!cfun)
2231 return;
2232 node = cgraph_node::get (current_function_decl);
2234 if (node->ipa_transforms_to_apply.exists ())
2236 unsigned int i;
2238 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2239 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2240 node->ipa_transforms_to_apply.release ();
2244 /* Check if PASS is explicitly disabled or enabled and return
2245 the gate status. FUNC is the function to be processed, and
2246 GATE_STATUS is the gate status determined by pass manager by
2247 default. */
2249 static bool
2250 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2252 bool explicitly_enabled = false;
2253 bool explicitly_disabled = false;
2255 explicitly_enabled
2256 = is_pass_explicitly_enabled_or_disabled (pass, func,
2257 enabled_pass_uid_range_tab);
2258 explicitly_disabled
2259 = is_pass_explicitly_enabled_or_disabled (pass, func,
2260 disabled_pass_uid_range_tab);
2262 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2264 return gate_status;
2268 /* Execute PASS. */
2270 bool
2271 execute_one_pass (opt_pass *pass)
2273 unsigned int todo_after = 0;
2275 bool gate_status;
2277 /* IPA passes are executed on whole program, so cfun should be NULL.
2278 Other passes need function context set. */
2279 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2280 gcc_assert (!cfun && !current_function_decl);
2281 else
2282 gcc_assert (cfun && current_function_decl);
2284 current_pass = pass;
2286 /* Check whether gate check should be avoided.
2287 User controls the value of the gate through the parameter "gate_status". */
2288 gate_status = pass->gate (cfun);
2289 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2291 /* Override gate with plugin. */
2292 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2294 if (!gate_status)
2296 /* Run so passes selectively disabling themselves on a given function
2297 are not miscounted. */
2298 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2300 check_profile_consistency (pass->static_pass_number, 0, false);
2301 check_profile_consistency (pass->static_pass_number, 1, false);
2303 current_pass = NULL;
2304 return false;
2307 /* Pass execution event trigger: useful to identify passes being
2308 executed. */
2309 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2311 if (!quiet_flag && !cfun)
2312 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2314 /* Note that the folders should only create gimple expressions.
2315 This is a hack until the new folder is ready. */
2316 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2318 pass_init_dump_file (pass);
2320 /* Run pre-pass verification. */
2321 execute_todo (pass->todo_flags_start);
2323 #ifdef ENABLE_CHECKING
2324 do_per_function (verify_curr_properties,
2325 (void *)(size_t)pass->properties_required);
2326 #endif
2328 /* If a timevar is present, start it. */
2329 if (pass->tv_id != TV_NONE)
2330 timevar_push (pass->tv_id);
2332 /* Do it! */
2333 todo_after = pass->execute (cfun);
2334 do_per_function (clear_last_verified, NULL);
2336 /* Stop timevar. */
2337 if (pass->tv_id != TV_NONE)
2338 timevar_pop (pass->tv_id);
2340 do_per_function (update_properties_after_pass, pass);
2342 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2343 check_profile_consistency (pass->static_pass_number, 0, true);
2345 /* Run post-pass cleanup and verification. */
2346 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2347 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2348 check_profile_consistency (pass->static_pass_number, 1, true);
2350 verify_interpass_invariants ();
2351 if (dump_file)
2352 do_per_function (execute_function_dump, pass);
2353 if (pass->type == IPA_PASS)
2355 struct cgraph_node *node;
2356 if (((ipa_opt_pass_d *)pass)->function_transform)
2357 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2358 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2361 if (!current_function_decl)
2362 symtab->process_new_functions ();
2364 pass_fini_dump_file (pass);
2366 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2367 gcc_assert (!(cfun->curr_properties & PROP_trees)
2368 || pass->type != RTL_PASS);
2370 current_pass = NULL;
2372 /* Signal this is a suitable GC collection point. */
2373 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2374 ggc_collect ();
2376 return true;
2379 static void
2380 execute_pass_list_1 (opt_pass *pass)
2384 gcc_assert (pass->type == GIMPLE_PASS
2385 || pass->type == RTL_PASS);
2386 if (execute_one_pass (pass) && pass->sub)
2387 execute_pass_list_1 (pass->sub);
2388 pass = pass->next;
2390 while (pass);
2393 void
2394 execute_pass_list (function *fn, opt_pass *pass)
2396 push_cfun (fn);
2397 execute_pass_list_1 (pass);
2398 if (fn->cfg)
2400 free_dominance_info (CDI_DOMINATORS);
2401 free_dominance_info (CDI_POST_DOMINATORS);
2403 pop_cfun ();
2406 /* Write out all LTO data. */
2407 static void
2408 write_lto (void)
2410 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2411 lto_output ();
2412 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2413 timevar_push (TV_IPA_LTO_DECL_OUT);
2414 produce_asm_for_decls ();
2415 timevar_pop (TV_IPA_LTO_DECL_OUT);
2418 /* Same as execute_pass_list but assume that subpasses of IPA passes
2419 are local passes. If SET is not NULL, write out summaries of only
2420 those node in SET. */
2422 static void
2423 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2425 while (pass)
2427 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2428 gcc_assert (!current_function_decl);
2429 gcc_assert (!cfun);
2430 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2431 if (pass->type == IPA_PASS
2432 && ipa_pass->write_summary
2433 && pass->gate (cfun))
2435 /* If a timevar is present, start it. */
2436 if (pass->tv_id)
2437 timevar_push (pass->tv_id);
2439 pass_init_dump_file (pass);
2441 current_pass = pass;
2442 ipa_pass->write_summary ();
2444 pass_fini_dump_file (pass);
2446 /* If a timevar is present, start it. */
2447 if (pass->tv_id)
2448 timevar_pop (pass->tv_id);
2451 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2452 ipa_write_summaries_2 (pass->sub, state);
2454 pass = pass->next;
2458 /* Helper function of ipa_write_summaries. Creates and destroys the
2459 decl state and calls ipa_write_summaries_2 for all passes that have
2460 summaries. SET is the set of nodes to be written. */
2462 static void
2463 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2465 pass_manager *passes = g->get_passes ();
2466 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2467 state->symtab_node_encoder = encoder;
2469 lto_output_init_mode_table ();
2470 lto_push_out_decl_state (state);
2472 gcc_assert (!flag_wpa);
2473 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2475 write_lto ();
2477 gcc_assert (lto_get_out_decl_state () == state);
2478 lto_pop_out_decl_state ();
2479 lto_delete_out_decl_state (state);
2482 /* Write out summaries for all the nodes in the callgraph. */
2484 void
2485 ipa_write_summaries (void)
2487 lto_symtab_encoder_t encoder;
2488 int i, order_pos;
2489 varpool_node *vnode;
2490 struct cgraph_node *node;
2491 struct cgraph_node **order;
2493 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2494 return;
2496 select_what_to_stream ();
2498 encoder = lto_symtab_encoder_new (false);
2500 /* Create the callgraph set in the same order used in
2501 cgraph_expand_all_functions. This mostly facilitates debugging,
2502 since it causes the gimple file to be processed in the same order
2503 as the source code. */
2504 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2505 order_pos = ipa_reverse_postorder (order);
2506 gcc_assert (order_pos == symtab->cgraph_count);
2508 for (i = order_pos - 1; i >= 0; i--)
2510 struct cgraph_node *node = order[i];
2512 if (node->has_gimple_body_p ())
2514 /* When streaming out references to statements as part of some IPA
2515 pass summary, the statements need to have uids assigned and the
2516 following does that for all the IPA passes here. Naturally, this
2517 ordering then matches the one IPA-passes get in their stmt_fixup
2518 hooks. */
2520 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2521 renumber_gimple_stmt_uids ();
2522 pop_cfun ();
2524 if (node->definition && node->need_lto_streaming)
2525 lto_set_symtab_encoder_in_partition (encoder, node);
2528 FOR_EACH_DEFINED_FUNCTION (node)
2529 if (node->alias && node->need_lto_streaming)
2530 lto_set_symtab_encoder_in_partition (encoder, node);
2531 FOR_EACH_DEFINED_VARIABLE (vnode)
2532 if (vnode->need_lto_streaming)
2533 lto_set_symtab_encoder_in_partition (encoder, vnode);
2535 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2537 free (order);
2540 /* Same as execute_pass_list but assume that subpasses of IPA passes
2541 are local passes. If SET is not NULL, write out optimization summaries of
2542 only those node in SET. */
2544 static void
2545 ipa_write_optimization_summaries_1 (opt_pass *pass,
2546 struct lto_out_decl_state *state)
2548 while (pass)
2550 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2551 gcc_assert (!current_function_decl);
2552 gcc_assert (!cfun);
2553 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2554 if (pass->type == IPA_PASS
2555 && ipa_pass->write_optimization_summary
2556 && pass->gate (cfun))
2558 /* If a timevar is present, start it. */
2559 if (pass->tv_id)
2560 timevar_push (pass->tv_id);
2562 pass_init_dump_file (pass);
2564 current_pass = pass;
2565 ipa_pass->write_optimization_summary ();
2567 pass_fini_dump_file (pass);
2569 /* If a timevar is present, start it. */
2570 if (pass->tv_id)
2571 timevar_pop (pass->tv_id);
2574 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2575 ipa_write_optimization_summaries_1 (pass->sub, state);
2577 pass = pass->next;
2581 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2582 NULL, write out all summaries of all nodes. */
2584 void
2585 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2587 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2588 lto_symtab_encoder_iterator lsei;
2589 state->symtab_node_encoder = encoder;
2591 lto_output_init_mode_table ();
2592 lto_push_out_decl_state (state);
2593 for (lsei = lsei_start_function_in_partition (encoder);
2594 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2596 struct cgraph_node *node = lsei_cgraph_node (lsei);
2597 /* When streaming out references to statements as part of some IPA
2598 pass summary, the statements need to have uids assigned.
2600 For functions newly born at WPA stage we need to initialize
2601 the uids here. */
2602 if (node->definition
2603 && gimple_has_body_p (node->decl))
2605 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2606 renumber_gimple_stmt_uids ();
2607 pop_cfun ();
2611 gcc_assert (flag_wpa);
2612 pass_manager *passes = g->get_passes ();
2613 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2615 write_lto ();
2617 gcc_assert (lto_get_out_decl_state () == state);
2618 lto_pop_out_decl_state ();
2619 lto_delete_out_decl_state (state);
2622 /* Same as execute_pass_list but assume that subpasses of IPA passes
2623 are local passes. */
2625 static void
2626 ipa_read_summaries_1 (opt_pass *pass)
2628 while (pass)
2630 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2632 gcc_assert (!current_function_decl);
2633 gcc_assert (!cfun);
2634 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2636 if (pass->gate (cfun))
2638 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2640 /* If a timevar is present, start it. */
2641 if (pass->tv_id)
2642 timevar_push (pass->tv_id);
2644 pass_init_dump_file (pass);
2646 current_pass = pass;
2647 ipa_pass->read_summary ();
2649 pass_fini_dump_file (pass);
2651 /* Stop timevar. */
2652 if (pass->tv_id)
2653 timevar_pop (pass->tv_id);
2656 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2657 ipa_read_summaries_1 (pass->sub);
2659 pass = pass->next;
2664 /* Read all the summaries for all_regular_ipa_passes. */
2666 void
2667 ipa_read_summaries (void)
2669 pass_manager *passes = g->get_passes ();
2670 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2673 /* Same as execute_pass_list but assume that subpasses of IPA passes
2674 are local passes. */
2676 static void
2677 ipa_read_optimization_summaries_1 (opt_pass *pass)
2679 while (pass)
2681 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2683 gcc_assert (!current_function_decl);
2684 gcc_assert (!cfun);
2685 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2687 if (pass->gate (cfun))
2689 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2691 /* If a timevar is present, start it. */
2692 if (pass->tv_id)
2693 timevar_push (pass->tv_id);
2695 pass_init_dump_file (pass);
2697 current_pass = pass;
2698 ipa_pass->read_optimization_summary ();
2700 pass_fini_dump_file (pass);
2702 /* Stop timevar. */
2703 if (pass->tv_id)
2704 timevar_pop (pass->tv_id);
2707 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2708 ipa_read_optimization_summaries_1 (pass->sub);
2710 pass = pass->next;
2714 /* Read all the summaries for all_regular_ipa_passes. */
2716 void
2717 ipa_read_optimization_summaries (void)
2719 pass_manager *passes = g->get_passes ();
2720 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2723 /* Same as execute_pass_list but assume that subpasses of IPA passes
2724 are local passes. */
2725 void
2726 execute_ipa_pass_list (opt_pass *pass)
2730 gcc_assert (!current_function_decl);
2731 gcc_assert (!cfun);
2732 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2733 if (execute_one_pass (pass) && pass->sub)
2735 if (pass->sub->type == GIMPLE_PASS)
2737 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2738 do_per_function_toporder ((void (*)(function *, void *))
2739 execute_pass_list,
2740 pass->sub);
2741 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2743 else if (pass->sub->type == SIMPLE_IPA_PASS
2744 || pass->sub->type == IPA_PASS)
2745 execute_ipa_pass_list (pass->sub);
2746 else
2747 gcc_unreachable ();
2749 gcc_assert (!current_function_decl);
2750 symtab->process_new_functions ();
2751 pass = pass->next;
2753 while (pass);
2756 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2758 static void
2759 execute_ipa_stmt_fixups (opt_pass *pass,
2760 struct cgraph_node *node, gimple **stmts)
2762 while (pass)
2764 /* Execute all of the IPA_PASSes in the list. */
2765 if (pass->type == IPA_PASS
2766 && pass->gate (cfun))
2768 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2770 if (ipa_pass->stmt_fixup)
2772 pass_init_dump_file (pass);
2773 /* If a timevar is present, start it. */
2774 if (pass->tv_id)
2775 timevar_push (pass->tv_id);
2777 current_pass = pass;
2778 ipa_pass->stmt_fixup (node, stmts);
2780 /* Stop timevar. */
2781 if (pass->tv_id)
2782 timevar_pop (pass->tv_id);
2783 pass_fini_dump_file (pass);
2785 if (pass->sub)
2786 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2788 pass = pass->next;
2792 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2794 void
2795 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2797 pass_manager *passes = g->get_passes ();
2798 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2802 extern void debug_properties (unsigned int);
2803 extern void dump_properties (FILE *, unsigned int);
2805 DEBUG_FUNCTION void
2806 dump_properties (FILE *dump, unsigned int props)
2808 fprintf (dump, "Properties:\n");
2809 if (props & PROP_gimple_any)
2810 fprintf (dump, "PROP_gimple_any\n");
2811 if (props & PROP_gimple_lcf)
2812 fprintf (dump, "PROP_gimple_lcf\n");
2813 if (props & PROP_gimple_leh)
2814 fprintf (dump, "PROP_gimple_leh\n");
2815 if (props & PROP_cfg)
2816 fprintf (dump, "PROP_cfg\n");
2817 if (props & PROP_ssa)
2818 fprintf (dump, "PROP_ssa\n");
2819 if (props & PROP_no_crit_edges)
2820 fprintf (dump, "PROP_no_crit_edges\n");
2821 if (props & PROP_rtl)
2822 fprintf (dump, "PROP_rtl\n");
2823 if (props & PROP_gimple_lomp)
2824 fprintf (dump, "PROP_gimple_lomp\n");
2825 if (props & PROP_gimple_lcx)
2826 fprintf (dump, "PROP_gimple_lcx\n");
2827 if (props & PROP_gimple_lvec)
2828 fprintf (dump, "PROP_gimple_lvec\n");
2829 if (props & PROP_cfglayout)
2830 fprintf (dump, "PROP_cfglayout\n");
2833 DEBUG_FUNCTION void
2834 debug_properties (unsigned int props)
2836 dump_properties (stderr, props);
2839 /* Called by local passes to see if function is called by already processed nodes.
2840 Because we process nodes in topological order, this means that function is
2841 in recursive cycle or we introduced new direct calls. */
2842 bool
2843 function_called_by_processed_nodes_p (void)
2845 struct cgraph_edge *e;
2846 for (e = cgraph_node::get (current_function_decl)->callers;
2848 e = e->next_caller)
2850 if (e->caller->decl == current_function_decl)
2851 continue;
2852 if (!e->caller->has_gimple_body_p ())
2853 continue;
2854 if (TREE_ASM_WRITTEN (e->caller->decl))
2855 continue;
2856 if (!e->caller->process && !e->caller->global.inlined_to)
2857 break;
2859 if (dump_file && e)
2861 fprintf (dump_file, "Already processed call to:\n");
2862 e->caller->dump (dump_file);
2864 return e != NULL;
2867 #include "gt-passes.h"