2015-08-31 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / passes.c
blob1b677ac5a67ee373912844a705a4a6f5a3e18c4b
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 /* Avoid confusing the debug information machinery when there are
323 errors. */
324 && !seen_error ())
325 (*debug_hooks->early_global_decl) (decl);
328 /* Called after finishing a record, union or enumeral type. */
330 void
331 rest_of_type_compilation (tree type, int toplev)
333 /* Avoid confusing the debug information machinery when there are
334 errors. */
335 if (seen_error ())
336 return;
338 timevar_push (TV_SYMOUT);
339 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
340 timevar_pop (TV_SYMOUT);
345 void
346 pass_manager::
347 finish_optimization_passes (void)
349 int i;
350 struct dump_file_info *dfi;
351 char *name;
352 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
354 timevar_push (TV_DUMP);
355 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
357 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
358 end_branch_prob ();
359 dumps->dump_finish (pass_profile_1->static_pass_number);
362 if (optimize > 0)
364 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
365 print_combine_total_stats ();
366 dumps->dump_finish (pass_profile_1->static_pass_number);
369 /* Do whatever is necessary to finish printing the graphs. */
370 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
371 if (dumps->dump_initialized_p (i)
372 && (dfi->pflags & TDF_GRAPH) != 0
373 && (name = dumps->get_dump_file_name (i)) != NULL)
375 finish_graph_dump_file (name);
376 free (name);
379 timevar_pop (TV_DUMP);
382 static unsigned int
383 execute_build_ssa_passes (void)
385 /* Once this pass (and its sub-passes) are complete, all functions
386 will be in SSA form. Technically this state change is happening
387 a tad early, since the sub-passes have not yet run, but since
388 none of the sub-passes are IPA passes and do not create new
389 functions, this is ok. We're setting this value for the benefit
390 of IPA passes that follow. */
391 if (symtab->state < IPA_SSA)
392 symtab->state = IPA_SSA;
393 return 0;
396 namespace {
398 const pass_data pass_data_build_ssa_passes =
400 SIMPLE_IPA_PASS, /* type */
401 "build_ssa_passes", /* name */
402 OPTGROUP_NONE, /* optinfo_flags */
403 TV_EARLY_LOCAL, /* tv_id */
404 0, /* properties_required */
405 0, /* properties_provided */
406 0, /* properties_destroyed */
407 0, /* todo_flags_start */
408 /* todo_flags_finish is executed before subpases. For this reason
409 it makes no sense to remove unreachable functions here. */
410 0, /* todo_flags_finish */
413 class pass_build_ssa_passes : public simple_ipa_opt_pass
415 public:
416 pass_build_ssa_passes (gcc::context *ctxt)
417 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
420 /* opt_pass methods: */
421 virtual bool gate (function *)
423 /* Don't bother doing anything if the program has errors. */
424 return (!seen_error () && !in_lto_p);
427 virtual unsigned int execute (function *)
429 return execute_build_ssa_passes ();
432 }; // class pass_build_ssa_passes
434 const pass_data pass_data_chkp_instrumentation_passes =
436 SIMPLE_IPA_PASS, /* type */
437 "chkp_passes", /* name */
438 OPTGROUP_NONE, /* optinfo_flags */
439 TV_NONE, /* tv_id */
440 0, /* properties_required */
441 0, /* properties_provided */
442 0, /* properties_destroyed */
443 0, /* todo_flags_start */
444 0, /* todo_flags_finish */
447 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
449 public:
450 pass_chkp_instrumentation_passes (gcc::context *ctxt)
451 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
454 /* opt_pass methods: */
455 virtual bool gate (function *)
457 /* Don't bother doing anything if the program has errors. */
458 return (flag_check_pointer_bounds
459 && !seen_error () && !in_lto_p);
462 }; // class pass_chkp_instrumentation_passes
464 const pass_data pass_data_local_optimization_passes =
466 SIMPLE_IPA_PASS, /* type */
467 "opt_local_passes", /* name */
468 OPTGROUP_NONE, /* optinfo_flags */
469 TV_NONE, /* tv_id */
470 0, /* properties_required */
471 0, /* properties_provided */
472 0, /* properties_destroyed */
473 0, /* todo_flags_start */
474 0, /* todo_flags_finish */
477 class pass_local_optimization_passes : public simple_ipa_opt_pass
479 public:
480 pass_local_optimization_passes (gcc::context *ctxt)
481 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
484 /* opt_pass methods: */
485 virtual bool gate (function *)
487 /* Don't bother doing anything if the program has errors. */
488 return (!seen_error () && !in_lto_p);
491 }; // class pass_local_optimization_passes
493 } // anon namespace
495 simple_ipa_opt_pass *
496 make_pass_build_ssa_passes (gcc::context *ctxt)
498 return new pass_build_ssa_passes (ctxt);
501 simple_ipa_opt_pass *
502 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
504 return new pass_chkp_instrumentation_passes (ctxt);
507 simple_ipa_opt_pass *
508 make_pass_local_optimization_passes (gcc::context *ctxt)
510 return new pass_local_optimization_passes (ctxt);
513 namespace {
515 const pass_data pass_data_all_early_optimizations =
517 GIMPLE_PASS, /* type */
518 "early_optimizations", /* name */
519 OPTGROUP_NONE, /* optinfo_flags */
520 TV_NONE, /* tv_id */
521 0, /* properties_required */
522 0, /* properties_provided */
523 0, /* properties_destroyed */
524 0, /* todo_flags_start */
525 0, /* todo_flags_finish */
528 class pass_all_early_optimizations : public gimple_opt_pass
530 public:
531 pass_all_early_optimizations (gcc::context *ctxt)
532 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
535 /* opt_pass methods: */
536 virtual bool gate (function *)
538 return (optimize >= 1
539 /* Don't bother doing anything if the program has errors. */
540 && !seen_error ());
543 }; // class pass_all_early_optimizations
545 } // anon namespace
547 static gimple_opt_pass *
548 make_pass_all_early_optimizations (gcc::context *ctxt)
550 return new pass_all_early_optimizations (ctxt);
553 namespace {
555 const pass_data pass_data_all_optimizations =
557 GIMPLE_PASS, /* type */
558 "*all_optimizations", /* name */
559 OPTGROUP_NONE, /* optinfo_flags */
560 TV_OPTIMIZE, /* tv_id */
561 0, /* properties_required */
562 0, /* properties_provided */
563 0, /* properties_destroyed */
564 0, /* todo_flags_start */
565 0, /* todo_flags_finish */
568 class pass_all_optimizations : public gimple_opt_pass
570 public:
571 pass_all_optimizations (gcc::context *ctxt)
572 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
575 /* opt_pass methods: */
576 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
578 }; // class pass_all_optimizations
580 } // anon namespace
582 static gimple_opt_pass *
583 make_pass_all_optimizations (gcc::context *ctxt)
585 return new pass_all_optimizations (ctxt);
588 namespace {
590 const pass_data pass_data_all_optimizations_g =
592 GIMPLE_PASS, /* type */
593 "*all_optimizations_g", /* name */
594 OPTGROUP_NONE, /* optinfo_flags */
595 TV_OPTIMIZE, /* tv_id */
596 0, /* properties_required */
597 0, /* properties_provided */
598 0, /* properties_destroyed */
599 0, /* todo_flags_start */
600 0, /* todo_flags_finish */
603 class pass_all_optimizations_g : public gimple_opt_pass
605 public:
606 pass_all_optimizations_g (gcc::context *ctxt)
607 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
610 /* opt_pass methods: */
611 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
613 }; // class pass_all_optimizations_g
615 } // anon namespace
617 static gimple_opt_pass *
618 make_pass_all_optimizations_g (gcc::context *ctxt)
620 return new pass_all_optimizations_g (ctxt);
623 namespace {
625 const pass_data pass_data_rest_of_compilation =
627 RTL_PASS, /* type */
628 "*rest_of_compilation", /* name */
629 OPTGROUP_NONE, /* optinfo_flags */
630 TV_REST_OF_COMPILATION, /* tv_id */
631 PROP_rtl, /* properties_required */
632 0, /* properties_provided */
633 0, /* properties_destroyed */
634 0, /* todo_flags_start */
635 0, /* todo_flags_finish */
638 class pass_rest_of_compilation : public rtl_opt_pass
640 public:
641 pass_rest_of_compilation (gcc::context *ctxt)
642 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
645 /* opt_pass methods: */
646 virtual bool gate (function *)
648 /* Early return if there were errors. We can run afoul of our
649 consistency checks, and there's not really much point in fixing them. */
650 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
653 }; // class pass_rest_of_compilation
655 } // anon namespace
657 static rtl_opt_pass *
658 make_pass_rest_of_compilation (gcc::context *ctxt)
660 return new pass_rest_of_compilation (ctxt);
663 namespace {
665 const pass_data pass_data_postreload =
667 RTL_PASS, /* type */
668 "*all-postreload", /* name */
669 OPTGROUP_NONE, /* optinfo_flags */
670 TV_POSTRELOAD, /* tv_id */
671 PROP_rtl, /* properties_required */
672 0, /* properties_provided */
673 0, /* properties_destroyed */
674 0, /* todo_flags_start */
675 0, /* todo_flags_finish */
678 class pass_postreload : public rtl_opt_pass
680 public:
681 pass_postreload (gcc::context *ctxt)
682 : rtl_opt_pass (pass_data_postreload, ctxt)
685 /* opt_pass methods: */
686 virtual bool gate (function *) { return reload_completed; }
688 }; // class pass_postreload
690 } // anon namespace
692 static rtl_opt_pass *
693 make_pass_postreload (gcc::context *ctxt)
695 return new pass_postreload (ctxt);
698 namespace {
700 const pass_data pass_data_late_compilation =
702 RTL_PASS, /* type */
703 "*all-late_compilation", /* name */
704 OPTGROUP_NONE, /* optinfo_flags */
705 TV_LATE_COMPILATION, /* tv_id */
706 PROP_rtl, /* properties_required */
707 0, /* properties_provided */
708 0, /* properties_destroyed */
709 0, /* todo_flags_start */
710 0, /* todo_flags_finish */
713 class pass_late_compilation : public rtl_opt_pass
715 public:
716 pass_late_compilation (gcc::context *ctxt)
717 : rtl_opt_pass (pass_data_late_compilation, ctxt)
720 /* opt_pass methods: */
721 virtual bool gate (function *)
723 return reload_completed || targetm.no_register_allocation;
726 }; // class pass_late_compilation
728 } // anon namespace
730 static rtl_opt_pass *
731 make_pass_late_compilation (gcc::context *ctxt)
733 return new pass_late_compilation (ctxt);
738 /* Set the static pass number of pass PASS to ID and record that
739 in the mapping from static pass number to pass. */
741 void
742 pass_manager::
743 set_pass_for_id (int id, opt_pass *pass)
745 pass->static_pass_number = id;
746 if (passes_by_id_size <= id)
748 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
749 memset (passes_by_id + passes_by_id_size, 0,
750 (id + 1 - passes_by_id_size) * sizeof (void *));
751 passes_by_id_size = id + 1;
753 passes_by_id[id] = pass;
756 /* Return the pass with the static pass number ID. */
758 opt_pass *
759 pass_manager::get_pass_for_id (int id) const
761 if (id >= passes_by_id_size)
762 return NULL;
763 return passes_by_id[id];
766 /* Iterate over the pass tree allocating dump file numbers. We want
767 to do this depth first, and independent of whether the pass is
768 enabled or not. */
770 void
771 register_one_dump_file (opt_pass *pass)
773 g->get_passes ()->register_one_dump_file (pass);
776 void
777 pass_manager::register_one_dump_file (opt_pass *pass)
779 char *dot_name, *flag_name, *glob_name;
780 const char *name, *full_name, *prefix;
781 char num[10];
782 int flags, id;
783 int optgroup_flags = OPTGROUP_NONE;
784 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
786 /* See below in next_pass_1. */
787 num[0] = '\0';
788 if (pass->static_pass_number != -1)
789 sprintf (num, "%d", ((int) pass->static_pass_number < 0
790 ? 1 : pass->static_pass_number));
792 /* The name is both used to identify the pass for the purposes of plugins,
793 and to specify dump file name and option.
794 The latter two might want something short which is not quite unique; for
795 that reason, we may have a disambiguating prefix, followed by a space
796 to mark the start of the following dump file name / option string. */
797 name = strchr (pass->name, ' ');
798 name = name ? name + 1 : pass->name;
799 dot_name = concat (".", name, num, NULL);
800 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
802 prefix = "ipa-";
803 flags = TDF_IPA;
804 optgroup_flags |= OPTGROUP_IPA;
806 else if (pass->type == GIMPLE_PASS)
808 prefix = "tree-";
809 flags = TDF_TREE;
811 else
813 prefix = "rtl-";
814 flags = TDF_RTL;
817 flag_name = concat (prefix, name, num, NULL);
818 glob_name = concat (prefix, name, NULL);
819 optgroup_flags |= pass->optinfo_flags;
820 /* For any passes that do not have an optgroup set, and which are not
821 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
822 any dump messages are emitted properly under -fopt-info(-optall). */
823 if (optgroup_flags == OPTGROUP_NONE)
824 optgroup_flags = OPTGROUP_OTHER;
825 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
826 optgroup_flags,
827 true);
828 set_pass_for_id (id, pass);
829 full_name = concat (prefix, pass->name, num, NULL);
830 register_pass_name (pass, full_name);
831 free (CONST_CAST (char *, full_name));
834 /* Register the dump files for the pass_manager starting at PASS. */
836 void
837 pass_manager::register_dump_files (opt_pass *pass)
841 if (pass->name && pass->name[0] != '*')
842 register_one_dump_file (pass);
844 if (pass->sub)
845 register_dump_files (pass->sub);
847 pass = pass->next;
849 while (pass);
852 static hash_map<nofree_string_hash, opt_pass *> *name_to_pass_map;
854 /* Register PASS with NAME. */
856 static void
857 register_pass_name (opt_pass *pass, const char *name)
859 if (!name_to_pass_map)
860 name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
862 if (name_to_pass_map->get (name))
863 return; /* Ignore plugin passes. */
865 const char *unique_name = xstrdup (name);
866 name_to_pass_map->put (unique_name, pass);
869 /* Map from pass id to canonicalized pass name. */
871 typedef const char *char_ptr;
872 static vec<char_ptr> pass_tab = vNULL;
874 /* Callback function for traversing NAME_TO_PASS_MAP. */
876 bool
877 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
879 gcc_assert (pass->static_pass_number > 0);
880 gcc_assert (pass_tab.exists ());
882 pass_tab[pass->static_pass_number] = name;
884 return 1;
887 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
888 table for dumping purpose. */
890 static void
891 create_pass_tab (void)
893 if (!flag_dump_passes)
894 return;
896 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
897 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
900 static bool override_gate_status (opt_pass *, tree, bool);
902 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
903 is turned on or not. */
905 static void
906 dump_one_pass (opt_pass *pass, int pass_indent)
908 int indent = 3 * pass_indent;
909 const char *pn;
910 bool is_on, is_really_on;
912 is_on = pass->gate (cfun);
913 is_really_on = override_gate_status (pass, current_function_decl, is_on);
915 if (pass->static_pass_number <= 0)
916 pn = pass->name;
917 else
918 pn = pass_tab[pass->static_pass_number];
920 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
921 (15 - indent < 0 ? 0 : 15 - indent), " ",
922 is_on ? " ON" : " OFF",
923 ((!is_on) == (!is_really_on) ? ""
924 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
927 /* Dump pass list PASS with indentation INDENT. */
929 static void
930 dump_pass_list (opt_pass *pass, int indent)
934 dump_one_pass (pass, indent);
935 if (pass->sub)
936 dump_pass_list (pass->sub, indent + 1);
937 pass = pass->next;
939 while (pass);
942 /* Dump all optimization passes. */
944 void
945 dump_passes (void)
947 g->get_passes ()->dump_passes ();
950 void
951 pass_manager::dump_passes () const
953 push_dummy_function (true);
955 create_pass_tab ();
957 dump_pass_list (all_lowering_passes, 1);
958 dump_pass_list (all_small_ipa_passes, 1);
959 dump_pass_list (all_regular_ipa_passes, 1);
960 dump_pass_list (all_late_ipa_passes, 1);
961 dump_pass_list (all_passes, 1);
963 pop_dummy_function ();
966 /* Returns the pass with NAME. */
968 static opt_pass *
969 get_pass_by_name (const char *name)
971 opt_pass **p = name_to_pass_map->get (name);
972 if (p)
973 return *p;
975 return NULL;
979 /* Range [start, last]. */
981 struct uid_range
983 unsigned int start;
984 unsigned int last;
985 const char *assem_name;
986 struct uid_range *next;
989 typedef struct uid_range *uid_range_p;
992 static vec<uid_range_p>
993 enabled_pass_uid_range_tab = vNULL;
994 static vec<uid_range_p>
995 disabled_pass_uid_range_tab = vNULL;
998 /* Parse option string for -fdisable- and -fenable-
999 The syntax of the options:
1001 -fenable-<pass_name>
1002 -fdisable-<pass_name>
1004 -fenable-<pass_name>=s1:e1,s2:e2,...
1005 -fdisable-<pass_name>=s1:e1,s2:e2,...
1008 static void
1009 enable_disable_pass (const char *arg, bool is_enable)
1011 opt_pass *pass;
1012 char *range_str, *phase_name;
1013 char *argstr = xstrdup (arg);
1014 vec<uid_range_p> *tab = 0;
1016 range_str = strchr (argstr,'=');
1017 if (range_str)
1019 *range_str = '\0';
1020 range_str++;
1023 phase_name = argstr;
1024 if (!*phase_name)
1026 if (is_enable)
1027 error ("unrecognized option -fenable");
1028 else
1029 error ("unrecognized option -fdisable");
1030 free (argstr);
1031 return;
1033 pass = get_pass_by_name (phase_name);
1034 if (!pass || pass->static_pass_number == -1)
1036 if (is_enable)
1037 error ("unknown pass %s specified in -fenable", phase_name);
1038 else
1039 error ("unknown pass %s specified in -fdisable", phase_name);
1040 free (argstr);
1041 return;
1044 if (is_enable)
1045 tab = &enabled_pass_uid_range_tab;
1046 else
1047 tab = &disabled_pass_uid_range_tab;
1049 if ((unsigned) pass->static_pass_number >= tab->length ())
1050 tab->safe_grow_cleared (pass->static_pass_number + 1);
1052 if (!range_str)
1054 uid_range_p slot;
1055 uid_range_p new_range = XCNEW (struct uid_range);
1057 new_range->start = 0;
1058 new_range->last = (unsigned)-1;
1060 slot = (*tab)[pass->static_pass_number];
1061 new_range->next = slot;
1062 (*tab)[pass->static_pass_number] = new_range;
1063 if (is_enable)
1064 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1065 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1066 else
1067 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1068 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1070 else
1072 char *next_range = NULL;
1073 char *one_range = range_str;
1074 char *end_val = NULL;
1078 uid_range_p slot;
1079 uid_range_p new_range;
1080 char *invalid = NULL;
1081 long start;
1082 char *func_name = NULL;
1084 next_range = strchr (one_range, ',');
1085 if (next_range)
1087 *next_range = '\0';
1088 next_range++;
1091 end_val = strchr (one_range, ':');
1092 if (end_val)
1094 *end_val = '\0';
1095 end_val++;
1097 start = strtol (one_range, &invalid, 10);
1098 if (*invalid || start < 0)
1100 if (end_val || (one_range[0] >= '0'
1101 && one_range[0] <= '9'))
1103 error ("Invalid range %s in option %s",
1104 one_range,
1105 is_enable ? "-fenable" : "-fdisable");
1106 free (argstr);
1107 return;
1109 func_name = one_range;
1111 if (!end_val)
1113 new_range = XCNEW (struct uid_range);
1114 if (!func_name)
1116 new_range->start = (unsigned) start;
1117 new_range->last = (unsigned) start;
1119 else
1121 new_range->start = (unsigned) -1;
1122 new_range->last = (unsigned) -1;
1123 new_range->assem_name = xstrdup (func_name);
1126 else
1128 long last = strtol (end_val, &invalid, 10);
1129 if (*invalid || last < start)
1131 error ("Invalid range %s in option %s",
1132 end_val,
1133 is_enable ? "-fenable" : "-fdisable");
1134 free (argstr);
1135 return;
1137 new_range = XCNEW (struct uid_range);
1138 new_range->start = (unsigned) start;
1139 new_range->last = (unsigned) last;
1142 slot = (*tab)[pass->static_pass_number];
1143 new_range->next = slot;
1144 (*tab)[pass->static_pass_number] = new_range;
1145 if (is_enable)
1147 if (new_range->assem_name)
1148 inform (UNKNOWN_LOCATION,
1149 "enable pass %s for function %s",
1150 phase_name, new_range->assem_name);
1151 else
1152 inform (UNKNOWN_LOCATION,
1153 "enable pass %s for functions in the range of [%u, %u]",
1154 phase_name, new_range->start, new_range->last);
1156 else
1158 if (new_range->assem_name)
1159 inform (UNKNOWN_LOCATION,
1160 "disable pass %s for function %s",
1161 phase_name, new_range->assem_name);
1162 else
1163 inform (UNKNOWN_LOCATION,
1164 "disable pass %s for functions in the range of [%u, %u]",
1165 phase_name, new_range->start, new_range->last);
1168 one_range = next_range;
1169 } while (next_range);
1172 free (argstr);
1175 /* Enable pass specified by ARG. */
1177 void
1178 enable_pass (const char *arg)
1180 enable_disable_pass (arg, true);
1183 /* Disable pass specified by ARG. */
1185 void
1186 disable_pass (const char *arg)
1188 enable_disable_pass (arg, false);
1191 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1193 static bool
1194 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1195 tree func,
1196 vec<uid_range_p> tab)
1198 uid_range_p slot, range;
1199 int cgraph_uid;
1200 const char *aname = NULL;
1202 if (!tab.exists ()
1203 || (unsigned) pass->static_pass_number >= tab.length ()
1204 || pass->static_pass_number == -1)
1205 return false;
1207 slot = tab[pass->static_pass_number];
1208 if (!slot)
1209 return false;
1211 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1212 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1213 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1215 range = slot;
1216 while (range)
1218 if ((unsigned) cgraph_uid >= range->start
1219 && (unsigned) cgraph_uid <= range->last)
1220 return true;
1221 if (range->assem_name && aname
1222 && !strcmp (range->assem_name, aname))
1223 return true;
1224 range = range->next;
1227 return false;
1231 /* Update static_pass_number for passes (and the flag
1232 TODO_mark_first_instance).
1234 Passes are constructed with static_pass_number preinitialized to 0
1236 This field is used in two different ways: initially as instance numbers
1237 of their kind, and then as ids within the entire pass manager.
1239 Within pass_manager::pass_manager:
1241 * In add_pass_instance(), as called by next_pass_1 in
1242 NEXT_PASS in init_optimization_passes
1244 * When the initial instance of a pass within a pass manager is seen,
1245 it is flagged, and its static_pass_number is set to -1
1247 * On subsequent times that it is seen, the static pass number
1248 is decremented each time, so that if there are e.g. 4 dups,
1249 they have static_pass_number -4, 2, 3, 4 respectively (note
1250 how the initial one is negative and gives the count); these
1251 can be thought of as instance numbers of the specific pass
1253 * Within the register_dump_files () traversal, set_pass_for_id()
1254 is called on each pass, using these instance numbers to create
1255 dumpfile switches, and then overwriting them with a pass id,
1256 which are global to the whole pass manager (based on
1257 (TDI_end + current value of extra_dump_files_in_use) ) */
1259 static void
1260 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1261 opt_pass *initial_pass)
1263 /* Are we dealing with the first pass of its kind, or a clone? */
1264 if (new_pass != initial_pass)
1266 /* We're dealing with a clone. */
1267 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1269 /* Indicate to register_dump_files that this pass has duplicates,
1270 and so it should rename the dump file. The first instance will
1271 be -1, and be number of duplicates = -static_pass_number - 1.
1272 Subsequent instances will be > 0 and just the duplicate number. */
1273 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1275 initial_pass->static_pass_number -= 1;
1276 new_pass->static_pass_number = -initial_pass->static_pass_number;
1279 else
1281 /* We're dealing with the first pass of its kind. */
1282 new_pass->todo_flags_start |= TODO_mark_first_instance;
1283 new_pass->static_pass_number = -1;
1285 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1289 /* Add a pass to the pass list. Duplicate the pass if it's already
1290 in the list. */
1292 static opt_pass **
1293 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1295 /* Every pass should have a name so that plugins can refer to them. */
1296 gcc_assert (pass->name != NULL);
1298 add_pass_instance (pass, false, initial_pass);
1299 *list = pass;
1301 return &(*list)->next;
1304 /* List node for an inserted pass instance. We need to keep track of all
1305 the newly-added pass instances (with 'added_pass_nodes' defined below)
1306 so that we can register their dump files after pass-positioning is finished.
1307 Registering dumping files needs to be post-processed or the
1308 static_pass_number of the opt_pass object would be modified and mess up
1309 the dump file names of future pass instances to be added. */
1311 struct pass_list_node
1313 opt_pass *pass;
1314 struct pass_list_node *next;
1317 static struct pass_list_node *added_pass_nodes = NULL;
1318 static struct pass_list_node *prev_added_pass_node;
1320 /* Insert the pass at the proper position. Return true if the pass
1321 is successfully added.
1323 NEW_PASS_INFO - new pass to be inserted
1324 PASS_LIST - root of the pass list to insert the new pass to */
1326 static bool
1327 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1329 opt_pass *pass = *pass_list, *prev_pass = NULL;
1330 bool success = false;
1332 for ( ; pass; prev_pass = pass, pass = pass->next)
1334 /* Check if the current pass is of the same type as the new pass and
1335 matches the name and the instance number of the reference pass. */
1336 if (pass->type == new_pass_info->pass->type
1337 && pass->name
1338 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1339 && ((new_pass_info->ref_pass_instance_number == 0)
1340 || (new_pass_info->ref_pass_instance_number ==
1341 pass->static_pass_number)
1342 || (new_pass_info->ref_pass_instance_number == 1
1343 && pass->todo_flags_start & TODO_mark_first_instance)))
1345 opt_pass *new_pass;
1346 struct pass_list_node *new_pass_node;
1348 if (new_pass_info->ref_pass_instance_number == 0)
1350 new_pass = new_pass_info->pass->clone ();
1351 add_pass_instance (new_pass, true, new_pass_info->pass);
1353 else
1355 new_pass = new_pass_info->pass;
1356 add_pass_instance (new_pass, true, new_pass);
1359 /* Insert the new pass instance based on the positioning op. */
1360 switch (new_pass_info->pos_op)
1362 case PASS_POS_INSERT_AFTER:
1363 new_pass->next = pass->next;
1364 pass->next = new_pass;
1366 /* Skip newly inserted pass to avoid repeated
1367 insertions in the case where the new pass and the
1368 existing one have the same name. */
1369 pass = new_pass;
1370 break;
1371 case PASS_POS_INSERT_BEFORE:
1372 new_pass->next = pass;
1373 if (prev_pass)
1374 prev_pass->next = new_pass;
1375 else
1376 *pass_list = new_pass;
1377 break;
1378 case PASS_POS_REPLACE:
1379 new_pass->next = pass->next;
1380 if (prev_pass)
1381 prev_pass->next = new_pass;
1382 else
1383 *pass_list = new_pass;
1384 new_pass->sub = pass->sub;
1385 new_pass->tv_id = pass->tv_id;
1386 pass = new_pass;
1387 break;
1388 default:
1389 error ("invalid pass positioning operation");
1390 return false;
1393 /* Save the newly added pass (instance) in the added_pass_nodes
1394 list so that we can register its dump file later. Note that
1395 we cannot register the dump file now because doing so will modify
1396 the static_pass_number of the opt_pass object and therefore
1397 mess up the dump file name of future instances. */
1398 new_pass_node = XCNEW (struct pass_list_node);
1399 new_pass_node->pass = new_pass;
1400 if (!added_pass_nodes)
1401 added_pass_nodes = new_pass_node;
1402 else
1403 prev_added_pass_node->next = new_pass_node;
1404 prev_added_pass_node = new_pass_node;
1406 success = true;
1409 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1410 success = true;
1413 return success;
1416 /* Hooks a new pass into the pass lists.
1418 PASS_INFO - pass information that specifies the opt_pass object,
1419 reference pass, instance number, and how to position
1420 the pass */
1422 void
1423 register_pass (struct register_pass_info *pass_info)
1425 g->get_passes ()->register_pass (pass_info);
1428 void
1429 register_pass (opt_pass* pass, pass_positioning_ops pos,
1430 const char* ref_pass_name, int ref_pass_inst_number)
1432 register_pass_info i;
1433 i.pass = pass;
1434 i.reference_pass_name = ref_pass_name;
1435 i.ref_pass_instance_number = ref_pass_inst_number;
1436 i.pos_op = pos;
1438 g->get_passes ()->register_pass (&i);
1441 void
1442 pass_manager::register_pass (struct register_pass_info *pass_info)
1444 bool all_instances, success;
1445 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1447 /* The checks below could fail in buggy plugins. Existing GCC
1448 passes should never fail these checks, so we mention plugin in
1449 the messages. */
1450 if (!pass_info->pass)
1451 fatal_error (input_location, "plugin cannot register a missing pass");
1453 if (!pass_info->pass->name)
1454 fatal_error (input_location, "plugin cannot register an unnamed pass");
1456 if (!pass_info->reference_pass_name)
1457 fatal_error
1458 (input_location,
1459 "plugin cannot register pass %qs without reference pass name",
1460 pass_info->pass->name);
1462 /* Try to insert the new pass to the pass lists. We need to check
1463 all five lists as the reference pass could be in one (or all) of
1464 them. */
1465 all_instances = pass_info->ref_pass_instance_number == 0;
1466 success = position_pass (pass_info, &all_lowering_passes);
1467 if (!success || all_instances)
1468 success |= position_pass (pass_info, &all_small_ipa_passes);
1469 if (!success || all_instances)
1470 success |= position_pass (pass_info, &all_regular_ipa_passes);
1471 if (!success || all_instances)
1472 success |= position_pass (pass_info, &all_late_ipa_passes);
1473 if (!success || all_instances)
1474 success |= position_pass (pass_info, &all_passes);
1475 if (!success)
1476 fatal_error
1477 (input_location,
1478 "pass %qs not found but is referenced by new pass %qs",
1479 pass_info->reference_pass_name, pass_info->pass->name);
1481 /* OK, we have successfully inserted the new pass. We need to register
1482 the dump files for the newly added pass and its duplicates (if any).
1483 Because the registration of plugin/backend passes happens after the
1484 command-line options are parsed, the options that specify single
1485 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1486 passes. Therefore we currently can only enable dumping of
1487 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1488 are specified. While doing so, we also delete the pass_list_node
1489 objects created during pass positioning. */
1490 while (added_pass_nodes)
1492 struct pass_list_node *next_node = added_pass_nodes->next;
1493 enum tree_dump_index tdi;
1494 register_one_dump_file (added_pass_nodes->pass);
1495 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1496 || added_pass_nodes->pass->type == IPA_PASS)
1497 tdi = TDI_ipa_all;
1498 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1499 tdi = TDI_tree_all;
1500 else
1501 tdi = TDI_rtl_all;
1502 /* Check if dump-all flag is specified. */
1503 if (dumps->get_dump_file_info (tdi)->pstate)
1504 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1505 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1506 XDELETE (added_pass_nodes);
1507 added_pass_nodes = next_node;
1511 /* Construct the pass tree. The sequencing of passes is driven by
1512 the cgraph routines:
1514 finalize_compilation_unit ()
1515 for each node N in the cgraph
1516 cgraph_analyze_function (N)
1517 cgraph_lower_function (N) -> all_lowering_passes
1519 If we are optimizing, compile is then invoked:
1521 compile ()
1522 ipa_passes () -> all_small_ipa_passes
1523 -> Analysis of all_regular_ipa_passes
1524 * possible LTO streaming at copmilation time *
1525 -> Execution of all_regular_ipa_passes
1526 * possible LTO streaming at link time *
1527 -> all_late_ipa_passes
1528 expand_all_functions ()
1529 for each node N in the cgraph
1530 expand_function (N) -> Transformation of all_regular_ipa_passes
1531 -> all_passes
1534 void *
1535 pass_manager::operator new (size_t sz)
1537 /* Ensure that all fields of the pass manager are zero-initialized. */
1538 return xcalloc (1, sz);
1541 void
1542 pass_manager::operator delete (void *ptr)
1544 free (ptr);
1547 pass_manager::pass_manager (context *ctxt)
1548 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1549 all_regular_ipa_passes (NULL),
1550 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1551 m_ctxt (ctxt)
1553 opt_pass **p;
1555 /* Initialize the pass_lists array. */
1556 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1557 GCC_PASS_LISTS
1558 #undef DEF_PASS_LIST
1560 /* Build the tree of passes. */
1562 #define INSERT_PASSES_AFTER(PASS) \
1563 p = &(PASS);
1565 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1567 opt_pass **p = &(PASS ## _1)->sub;
1569 #define POP_INSERT_PASSES() \
1572 #define NEXT_PASS(PASS, NUM) \
1573 do { \
1574 gcc_assert (NULL == PASS ## _ ## NUM); \
1575 if ((NUM) == 1) \
1576 PASS ## _1 = make_##PASS (m_ctxt); \
1577 else \
1579 gcc_assert (PASS ## _1); \
1580 PASS ## _ ## NUM = PASS ## _1->clone (); \
1582 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1583 } while (0)
1585 #define TERMINATE_PASS_LIST() \
1586 *p = NULL;
1588 #include "pass-instances.def"
1590 #undef INSERT_PASSES_AFTER
1591 #undef PUSH_INSERT_PASSES_WITHIN
1592 #undef POP_INSERT_PASSES
1593 #undef NEXT_PASS
1594 #undef TERMINATE_PASS_LIST
1596 /* Register the passes with the tree dump code. */
1597 register_dump_files (all_lowering_passes);
1598 register_dump_files (all_small_ipa_passes);
1599 register_dump_files (all_regular_ipa_passes);
1600 register_dump_files (all_late_ipa_passes);
1601 register_dump_files (all_passes);
1604 static void
1605 delete_pass_tree (opt_pass *pass)
1607 while (pass)
1609 /* Recurse into child passes. */
1610 delete_pass_tree (pass->sub);
1612 opt_pass *next = pass->next;
1614 /* Delete this pass. */
1615 delete pass;
1617 /* Iterate onto sibling passes. */
1618 pass = next;
1622 pass_manager::~pass_manager ()
1624 XDELETEVEC (passes_by_id);
1626 /* Call delete_pass_tree on each of the pass_lists. */
1627 #define DEF_PASS_LIST(LIST) \
1628 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1629 GCC_PASS_LISTS
1630 #undef DEF_PASS_LIST
1634 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1635 function CALLBACK for every function in the call graph. Otherwise,
1636 call CALLBACK on the current function. */
1638 static void
1639 do_per_function (void (*callback) (function *, void *data), void *data)
1641 if (current_function_decl)
1642 callback (cfun, data);
1643 else
1645 struct cgraph_node *node;
1646 FOR_EACH_DEFINED_FUNCTION (node)
1647 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1648 && (!node->clone_of || node->decl != node->clone_of->decl))
1649 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1653 /* Because inlining might remove no-longer reachable nodes, we need to
1654 keep the array visible to garbage collector to avoid reading collected
1655 out nodes. */
1656 static int nnodes;
1657 static GTY ((length ("nnodes"))) cgraph_node **order;
1659 /* Hook called when NODE is removed and therefore should be
1660 excluded from order vector. DATA is an array of integers.
1661 DATA[0] holds max index it may be accessed by. For cgraph
1662 node DATA[node->uid + 1] holds index of this node in order
1663 vector. */
1664 static void
1665 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1667 int *order_idx = (int *)data;
1669 if (node->uid >= order_idx[0])
1670 return;
1672 int idx = order_idx[node->uid + 1];
1673 if (idx >= 0 && idx < nnodes && order[idx] == node)
1674 order[idx] = NULL;
1677 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1678 function CALLBACK for every function in the call graph. Otherwise,
1679 call CALLBACK on the current function.
1680 This function is global so that plugins can use it. */
1681 void
1682 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1684 int i;
1686 if (current_function_decl)
1687 callback (cfun, data);
1688 else
1690 cgraph_node_hook_list *hook;
1691 int *order_idx;
1692 gcc_assert (!order);
1693 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1695 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1696 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1697 order_idx[0] = symtab->cgraph_max_uid;
1699 nnodes = ipa_reverse_postorder (order);
1700 for (i = nnodes - 1; i >= 0; i--)
1702 order[i]->process = 1;
1703 order_idx[order[i]->uid + 1] = i;
1705 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1706 order_idx);
1707 for (i = nnodes - 1; i >= 0; i--)
1709 /* Function could be inlined and removed as unreachable. */
1710 if (!order[i])
1711 continue;
1713 struct cgraph_node *node = order[i];
1715 /* Allow possibly removed nodes to be garbage collected. */
1716 order[i] = NULL;
1717 node->process = 0;
1718 if (node->has_gimple_body_p ())
1719 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1721 symtab->remove_cgraph_removal_hook (hook);
1723 ggc_free (order);
1724 order = NULL;
1725 nnodes = 0;
1728 /* Helper function to perform function body dump. */
1730 static void
1731 execute_function_dump (function *fn, void *data)
1733 opt_pass *pass = (opt_pass *)data;
1735 if (dump_file)
1737 push_cfun (fn);
1739 if (fn->curr_properties & PROP_trees)
1740 dump_function_to_file (fn->decl, dump_file, dump_flags);
1741 else
1742 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1744 /* Flush the file. If verification fails, we won't be able to
1745 close the file before aborting. */
1746 fflush (dump_file);
1748 if ((fn->curr_properties & PROP_cfg)
1749 && (dump_flags & TDF_GRAPH))
1751 if (!pass->graph_dump_initialized)
1753 clean_graph_dump_file (dump_file_name);
1754 pass->graph_dump_initialized = true;
1756 print_graph_cfg (dump_file_name, fn);
1759 pop_cfun ();
1763 static struct profile_record *profile_record;
1765 /* Do profile consistency book-keeping for the pass with static number INDEX.
1766 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1767 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1768 if we are only book-keeping on passes that may have selectively disabled
1769 themselves on a given function. */
1770 static void
1771 check_profile_consistency (int index, int subpass, bool run)
1773 pass_manager *passes = g->get_passes ();
1774 if (index == -1)
1775 return;
1776 if (!profile_record)
1777 profile_record = XCNEWVEC (struct profile_record,
1778 passes->passes_by_id_size);
1779 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1780 gcc_assert (subpass < 2);
1781 profile_record[index].run |= run;
1782 account_profile_record (&profile_record[index], subpass);
1785 /* Output profile consistency. */
1787 void
1788 dump_profile_report (void)
1790 g->get_passes ()->dump_profile_report ();
1793 void
1794 pass_manager::dump_profile_report () const
1796 int i, j;
1797 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1798 gcov_type last_time = 0, last_size = 0;
1799 double rel_time_change, rel_size_change;
1800 int last_reported = 0;
1802 if (!profile_record)
1803 return;
1804 fprintf (stderr, "\nProfile consistency report:\n\n");
1805 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1806 fprintf (stderr, " |freq count |freq count |size time\n");
1808 for (i = 0; i < passes_by_id_size; i++)
1809 for (j = 0 ; j < 2; j++)
1810 if (profile_record[i].run)
1812 if (last_time)
1813 rel_time_change = (profile_record[i].time[j]
1814 - (double)last_time) * 100 / (double)last_time;
1815 else
1816 rel_time_change = 0;
1817 if (last_size)
1818 rel_size_change = (profile_record[i].size[j]
1819 - (double)last_size) * 100 / (double)last_size;
1820 else
1821 rel_size_change = 0;
1823 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1824 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1825 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1826 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1827 || rel_time_change || rel_size_change)
1829 last_reported = i;
1830 fprintf (stderr, "%-20s %s",
1831 passes_by_id [i]->name,
1832 j ? "(after TODO)" : " ");
1833 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1834 fprintf (stderr, "| %+5i",
1835 profile_record[i].num_mismatched_freq_in[j]
1836 - last_freq_in);
1837 else
1838 fprintf (stderr, "| ");
1839 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1840 fprintf (stderr, " %+5i",
1841 profile_record[i].num_mismatched_count_in[j]
1842 - last_count_in);
1843 else
1844 fprintf (stderr, " ");
1845 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1846 fprintf (stderr, "| %+5i",
1847 profile_record[i].num_mismatched_freq_out[j]
1848 - last_freq_out);
1849 else
1850 fprintf (stderr, "| ");
1851 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1852 fprintf (stderr, " %+5i",
1853 profile_record[i].num_mismatched_count_out[j]
1854 - last_count_out);
1855 else
1856 fprintf (stderr, " ");
1858 /* Size/time units change across gimple and RTL. */
1859 if (i == pass_expand_1->static_pass_number)
1860 fprintf (stderr, "|----------");
1861 else
1863 if (rel_size_change)
1864 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1865 else
1866 fprintf (stderr, "| ");
1867 if (rel_time_change)
1868 fprintf (stderr, " %+8.4f%%", rel_time_change);
1870 fprintf (stderr, "\n");
1871 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1872 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1873 last_count_in = profile_record[i].num_mismatched_count_in[j];
1874 last_count_out = profile_record[i].num_mismatched_count_out[j];
1876 else if (j && last_reported != i)
1878 last_reported = i;
1879 fprintf (stderr, "%-20s ------------| | |\n",
1880 passes_by_id [i]->name);
1882 last_time = profile_record[i].time[j];
1883 last_size = profile_record[i].size[j];
1887 /* Perform all TODO actions that ought to be done on each function. */
1889 static void
1890 execute_function_todo (function *fn, void *data)
1892 bool from_ipa_pass = (cfun == NULL);
1893 unsigned int flags = (size_t)data;
1894 flags &= ~fn->last_verified;
1895 if (!flags)
1896 return;
1898 push_cfun (fn);
1900 /* Always cleanup the CFG before trying to update SSA. */
1901 if (flags & TODO_cleanup_cfg)
1903 cleanup_tree_cfg ();
1905 /* When cleanup_tree_cfg merges consecutive blocks, it may
1906 perform some simplistic propagation when removing single
1907 valued PHI nodes. This propagation may, in turn, cause the
1908 SSA form to become out-of-date (see PR 22037). So, even
1909 if the parent pass had not scheduled an SSA update, we may
1910 still need to do one. */
1911 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1912 flags |= TODO_update_ssa;
1915 if (flags & TODO_update_ssa_any)
1917 unsigned update_flags = flags & TODO_update_ssa_any;
1918 update_ssa (update_flags);
1921 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1922 compute_may_aliases ();
1924 if (optimize && (flags & TODO_update_address_taken))
1925 execute_update_addresses_taken ();
1927 if (flags & TODO_remove_unused_locals)
1928 remove_unused_locals ();
1930 if (flags & TODO_rebuild_frequencies)
1931 rebuild_frequencies ();
1933 if (flags & TODO_rebuild_cgraph_edges)
1934 cgraph_edge::rebuild_edges ();
1936 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1937 /* If we've seen errors do not bother running any verifiers. */
1938 if (!seen_error ())
1940 #if defined ENABLE_CHECKING
1941 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1942 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1944 if (flags & TODO_verify_il)
1946 if (cfun->curr_properties & PROP_trees)
1948 if (cfun->curr_properties & PROP_cfg)
1949 /* IPA passes leave stmts to be fixed up, so make sure to
1950 not verify stmts really throw. */
1951 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1952 else
1953 verify_gimple_in_seq (gimple_body (cfun->decl));
1955 if (cfun->curr_properties & PROP_ssa)
1956 /* IPA passes leave stmts to be fixed up, so make sure to
1957 not verify SSA operands whose verifier will choke on that. */
1958 verify_ssa (true, !from_ipa_pass);
1959 /* IPA passes leave basic-blocks unsplit, so make sure to
1960 not trip on that. */
1961 if ((cfun->curr_properties & PROP_cfg)
1962 && !from_ipa_pass)
1963 verify_flow_info ();
1964 if (current_loops
1965 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1966 verify_loop_closed_ssa (false);
1967 if (cfun->curr_properties & PROP_rtl)
1968 verify_rtl_sharing ();
1971 /* Make sure verifiers don't change dominator state. */
1972 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1973 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1974 #endif
1977 fn->last_verified = flags & TODO_verify_all;
1979 pop_cfun ();
1981 /* For IPA passes make sure to release dominator info, it can be
1982 computed by non-verifying TODOs. */
1983 if (from_ipa_pass)
1985 free_dominance_info (fn, CDI_DOMINATORS);
1986 free_dominance_info (fn, CDI_POST_DOMINATORS);
1990 /* Perform all TODO actions. */
1991 static void
1992 execute_todo (unsigned int flags)
1994 #if defined ENABLE_CHECKING
1995 if (cfun
1996 && need_ssa_update_p (cfun))
1997 gcc_assert (flags & TODO_update_ssa_any);
1998 #endif
2000 timevar_push (TV_TODO);
2002 /* Inform the pass whether it is the first time it is run. */
2003 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2005 statistics_fini_pass ();
2007 if (flags)
2008 do_per_function (execute_function_todo, (void *)(size_t) flags);
2010 /* Always remove functions just as before inlining: IPA passes might be
2011 interested to see bodies of extern inline functions that are not inlined
2012 to analyze side effects. The full removal is done just at the end
2013 of IPA pass queue. */
2014 if (flags & TODO_remove_functions)
2016 gcc_assert (!cfun);
2017 symtab->remove_unreachable_nodes (dump_file);
2020 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2022 gcc_assert (!cfun);
2023 symtab_node::dump_table (dump_file);
2024 /* Flush the file. If verification fails, we won't be able to
2025 close the file before aborting. */
2026 fflush (dump_file);
2029 /* Now that the dumping has been done, we can get rid of the optional
2030 df problems. */
2031 if (flags & TODO_df_finish)
2032 df_finish_pass ((flags & TODO_df_verify) != 0);
2034 timevar_pop (TV_TODO);
2037 /* Verify invariants that should hold between passes. This is a place
2038 to put simple sanity checks. */
2040 static void
2041 verify_interpass_invariants (void)
2043 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2046 /* Clear the last verified flag. */
2048 static void
2049 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2051 fn->last_verified = 0;
2054 /* Helper function. Verify that the properties has been turn into the
2055 properties expected by the pass. */
2057 #ifdef ENABLE_CHECKING
2058 static void
2059 verify_curr_properties (function *fn, void *data)
2061 unsigned int props = (size_t)data;
2062 gcc_assert ((fn->curr_properties & props) == props);
2064 #endif
2066 /* Initialize pass dump file. */
2067 /* This is non-static so that the plugins can use it. */
2069 bool
2070 pass_init_dump_file (opt_pass *pass)
2072 /* If a dump file name is present, open it if enabled. */
2073 if (pass->static_pass_number != -1)
2075 timevar_push (TV_DUMP);
2076 gcc::dump_manager *dumps = g->get_dumps ();
2077 bool initializing_dump =
2078 !dumps->dump_initialized_p (pass->static_pass_number);
2079 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2080 dumps->dump_start (pass->static_pass_number, &dump_flags);
2081 if (dump_file && current_function_decl)
2082 dump_function_header (dump_file, current_function_decl, dump_flags);
2083 if (initializing_dump
2084 && dump_file && (dump_flags & TDF_GRAPH)
2085 && cfun && (cfun->curr_properties & PROP_cfg))
2087 clean_graph_dump_file (dump_file_name);
2088 pass->graph_dump_initialized = true;
2090 timevar_pop (TV_DUMP);
2091 return initializing_dump;
2093 else
2094 return false;
2097 /* Flush PASS dump file. */
2098 /* This is non-static so that plugins can use it. */
2100 void
2101 pass_fini_dump_file (opt_pass *pass)
2103 timevar_push (TV_DUMP);
2105 /* Flush and close dump file. */
2106 if (dump_file_name)
2108 free (CONST_CAST (char *, dump_file_name));
2109 dump_file_name = NULL;
2112 g->get_dumps ()->dump_finish (pass->static_pass_number);
2113 timevar_pop (TV_DUMP);
2116 /* After executing the pass, apply expected changes to the function
2117 properties. */
2119 static void
2120 update_properties_after_pass (function *fn, void *data)
2122 opt_pass *pass = (opt_pass *) data;
2123 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2124 & ~pass->properties_destroyed;
2127 /* Execute summary generation for all of the passes in IPA_PASS. */
2129 void
2130 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2132 while (ipa_pass)
2134 opt_pass *pass = ipa_pass;
2136 /* Execute all of the IPA_PASSes in the list. */
2137 if (ipa_pass->type == IPA_PASS
2138 && pass->gate (cfun)
2139 && ipa_pass->generate_summary)
2141 pass_init_dump_file (pass);
2143 /* If a timevar is present, start it. */
2144 if (pass->tv_id)
2145 timevar_push (pass->tv_id);
2147 current_pass = pass;
2148 ipa_pass->generate_summary ();
2150 /* Stop timevar. */
2151 if (pass->tv_id)
2152 timevar_pop (pass->tv_id);
2154 pass_fini_dump_file (pass);
2156 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2160 /* Execute IPA_PASS function transform on NODE. */
2162 static void
2163 execute_one_ipa_transform_pass (struct cgraph_node *node,
2164 ipa_opt_pass_d *ipa_pass)
2166 opt_pass *pass = ipa_pass;
2167 unsigned int todo_after = 0;
2169 current_pass = pass;
2170 if (!ipa_pass->function_transform)
2171 return;
2173 /* Note that the folders should only create gimple expressions.
2174 This is a hack until the new folder is ready. */
2175 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2177 pass_init_dump_file (pass);
2179 /* Run pre-pass verification. */
2180 execute_todo (ipa_pass->function_transform_todo_flags_start);
2182 /* If a timevar is present, start it. */
2183 if (pass->tv_id != TV_NONE)
2184 timevar_push (pass->tv_id);
2186 /* Do it! */
2187 todo_after = ipa_pass->function_transform (node);
2189 /* Stop timevar. */
2190 if (pass->tv_id != TV_NONE)
2191 timevar_pop (pass->tv_id);
2193 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2194 check_profile_consistency (pass->static_pass_number, 0, true);
2196 /* Run post-pass cleanup and verification. */
2197 execute_todo (todo_after);
2198 verify_interpass_invariants ();
2199 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2200 check_profile_consistency (pass->static_pass_number, 1, true);
2202 if (dump_file)
2203 do_per_function (execute_function_dump, NULL);
2204 pass_fini_dump_file (pass);
2206 current_pass = NULL;
2208 /* Signal this is a suitable GC collection point. */
2209 if (!(todo_after & TODO_do_not_ggc_collect))
2210 ggc_collect ();
2213 /* For the current function, execute all ipa transforms. */
2215 void
2216 execute_all_ipa_transforms (void)
2218 struct cgraph_node *node;
2219 if (!cfun)
2220 return;
2221 node = cgraph_node::get (current_function_decl);
2223 if (node->ipa_transforms_to_apply.exists ())
2225 unsigned int i;
2227 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2228 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2229 node->ipa_transforms_to_apply.release ();
2233 /* Check if PASS is explicitly disabled or enabled and return
2234 the gate status. FUNC is the function to be processed, and
2235 GATE_STATUS is the gate status determined by pass manager by
2236 default. */
2238 static bool
2239 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2241 bool explicitly_enabled = false;
2242 bool explicitly_disabled = false;
2244 explicitly_enabled
2245 = is_pass_explicitly_enabled_or_disabled (pass, func,
2246 enabled_pass_uid_range_tab);
2247 explicitly_disabled
2248 = is_pass_explicitly_enabled_or_disabled (pass, func,
2249 disabled_pass_uid_range_tab);
2251 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2253 return gate_status;
2257 /* Execute PASS. */
2259 bool
2260 execute_one_pass (opt_pass *pass)
2262 unsigned int todo_after = 0;
2264 bool gate_status;
2266 /* IPA passes are executed on whole program, so cfun should be NULL.
2267 Other passes need function context set. */
2268 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2269 gcc_assert (!cfun && !current_function_decl);
2270 else
2271 gcc_assert (cfun && current_function_decl);
2273 current_pass = pass;
2275 /* Check whether gate check should be avoided.
2276 User controls the value of the gate through the parameter "gate_status". */
2277 gate_status = pass->gate (cfun);
2278 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2280 /* Override gate with plugin. */
2281 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2283 if (!gate_status)
2285 /* Run so passes selectively disabling themselves on a given function
2286 are not miscounted. */
2287 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2289 check_profile_consistency (pass->static_pass_number, 0, false);
2290 check_profile_consistency (pass->static_pass_number, 1, false);
2292 current_pass = NULL;
2293 return false;
2296 /* Pass execution event trigger: useful to identify passes being
2297 executed. */
2298 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2300 if (!quiet_flag && !cfun)
2301 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2303 /* Note that the folders should only create gimple expressions.
2304 This is a hack until the new folder is ready. */
2305 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2307 pass_init_dump_file (pass);
2309 /* Run pre-pass verification. */
2310 execute_todo (pass->todo_flags_start);
2312 #ifdef ENABLE_CHECKING
2313 do_per_function (verify_curr_properties,
2314 (void *)(size_t)pass->properties_required);
2315 #endif
2317 /* If a timevar is present, start it. */
2318 if (pass->tv_id != TV_NONE)
2319 timevar_push (pass->tv_id);
2321 /* Do it! */
2322 todo_after = pass->execute (cfun);
2323 do_per_function (clear_last_verified, NULL);
2325 /* Stop timevar. */
2326 if (pass->tv_id != TV_NONE)
2327 timevar_pop (pass->tv_id);
2329 do_per_function (update_properties_after_pass, pass);
2331 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2332 check_profile_consistency (pass->static_pass_number, 0, true);
2334 /* Run post-pass cleanup and verification. */
2335 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2336 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2337 check_profile_consistency (pass->static_pass_number, 1, true);
2339 verify_interpass_invariants ();
2340 if (dump_file)
2341 do_per_function (execute_function_dump, pass);
2342 if (pass->type == IPA_PASS)
2344 struct cgraph_node *node;
2345 if (((ipa_opt_pass_d *)pass)->function_transform)
2346 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2347 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2350 if (!current_function_decl)
2351 symtab->process_new_functions ();
2353 pass_fini_dump_file (pass);
2355 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2356 gcc_assert (!(cfun->curr_properties & PROP_trees)
2357 || pass->type != RTL_PASS);
2359 current_pass = NULL;
2361 /* Signal this is a suitable GC collection point. */
2362 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2363 ggc_collect ();
2365 return true;
2368 static void
2369 execute_pass_list_1 (opt_pass *pass)
2373 gcc_assert (pass->type == GIMPLE_PASS
2374 || pass->type == RTL_PASS);
2375 if (execute_one_pass (pass) && pass->sub)
2376 execute_pass_list_1 (pass->sub);
2377 pass = pass->next;
2379 while (pass);
2382 void
2383 execute_pass_list (function *fn, opt_pass *pass)
2385 push_cfun (fn);
2386 execute_pass_list_1 (pass);
2387 if (fn->cfg)
2389 free_dominance_info (CDI_DOMINATORS);
2390 free_dominance_info (CDI_POST_DOMINATORS);
2392 pop_cfun ();
2395 /* Write out all LTO data. */
2396 static void
2397 write_lto (void)
2399 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2400 lto_output ();
2401 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2402 timevar_push (TV_IPA_LTO_DECL_OUT);
2403 produce_asm_for_decls ();
2404 timevar_pop (TV_IPA_LTO_DECL_OUT);
2407 /* Same as execute_pass_list but assume that subpasses of IPA passes
2408 are local passes. If SET is not NULL, write out summaries of only
2409 those node in SET. */
2411 static void
2412 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2414 while (pass)
2416 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2417 gcc_assert (!current_function_decl);
2418 gcc_assert (!cfun);
2419 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2420 if (pass->type == IPA_PASS
2421 && ipa_pass->write_summary
2422 && pass->gate (cfun))
2424 /* If a timevar is present, start it. */
2425 if (pass->tv_id)
2426 timevar_push (pass->tv_id);
2428 pass_init_dump_file (pass);
2430 current_pass = pass;
2431 ipa_pass->write_summary ();
2433 pass_fini_dump_file (pass);
2435 /* If a timevar is present, start it. */
2436 if (pass->tv_id)
2437 timevar_pop (pass->tv_id);
2440 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2441 ipa_write_summaries_2 (pass->sub, state);
2443 pass = pass->next;
2447 /* Helper function of ipa_write_summaries. Creates and destroys the
2448 decl state and calls ipa_write_summaries_2 for all passes that have
2449 summaries. SET is the set of nodes to be written. */
2451 static void
2452 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2454 pass_manager *passes = g->get_passes ();
2455 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2456 state->symtab_node_encoder = encoder;
2458 lto_output_init_mode_table ();
2459 lto_push_out_decl_state (state);
2461 gcc_assert (!flag_wpa);
2462 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2464 write_lto ();
2466 gcc_assert (lto_get_out_decl_state () == state);
2467 lto_pop_out_decl_state ();
2468 lto_delete_out_decl_state (state);
2471 /* Write out summaries for all the nodes in the callgraph. */
2473 void
2474 ipa_write_summaries (void)
2476 lto_symtab_encoder_t encoder;
2477 int i, order_pos;
2478 varpool_node *vnode;
2479 struct cgraph_node *node;
2480 struct cgraph_node **order;
2482 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2483 return;
2485 select_what_to_stream ();
2487 encoder = lto_symtab_encoder_new (false);
2489 /* Create the callgraph set in the same order used in
2490 cgraph_expand_all_functions. This mostly facilitates debugging,
2491 since it causes the gimple file to be processed in the same order
2492 as the source code. */
2493 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2494 order_pos = ipa_reverse_postorder (order);
2495 gcc_assert (order_pos == symtab->cgraph_count);
2497 for (i = order_pos - 1; i >= 0; i--)
2499 struct cgraph_node *node = order[i];
2501 if (node->has_gimple_body_p ())
2503 /* When streaming out references to statements as part of some IPA
2504 pass summary, the statements need to have uids assigned and the
2505 following does that for all the IPA passes here. Naturally, this
2506 ordering then matches the one IPA-passes get in their stmt_fixup
2507 hooks. */
2509 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2510 renumber_gimple_stmt_uids ();
2511 pop_cfun ();
2513 if (node->definition && node->need_lto_streaming)
2514 lto_set_symtab_encoder_in_partition (encoder, node);
2517 FOR_EACH_DEFINED_FUNCTION (node)
2518 if (node->alias && node->need_lto_streaming)
2519 lto_set_symtab_encoder_in_partition (encoder, node);
2520 FOR_EACH_DEFINED_VARIABLE (vnode)
2521 if (vnode->need_lto_streaming)
2522 lto_set_symtab_encoder_in_partition (encoder, vnode);
2524 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2526 free (order);
2529 /* Same as execute_pass_list but assume that subpasses of IPA passes
2530 are local passes. If SET is not NULL, write out optimization summaries of
2531 only those node in SET. */
2533 static void
2534 ipa_write_optimization_summaries_1 (opt_pass *pass,
2535 struct lto_out_decl_state *state)
2537 while (pass)
2539 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2540 gcc_assert (!current_function_decl);
2541 gcc_assert (!cfun);
2542 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2543 if (pass->type == IPA_PASS
2544 && ipa_pass->write_optimization_summary
2545 && pass->gate (cfun))
2547 /* If a timevar is present, start it. */
2548 if (pass->tv_id)
2549 timevar_push (pass->tv_id);
2551 pass_init_dump_file (pass);
2553 current_pass = pass;
2554 ipa_pass->write_optimization_summary ();
2556 pass_fini_dump_file (pass);
2558 /* If a timevar is present, start it. */
2559 if (pass->tv_id)
2560 timevar_pop (pass->tv_id);
2563 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2564 ipa_write_optimization_summaries_1 (pass->sub, state);
2566 pass = pass->next;
2570 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2571 NULL, write out all summaries of all nodes. */
2573 void
2574 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2576 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2577 lto_symtab_encoder_iterator lsei;
2578 state->symtab_node_encoder = encoder;
2580 lto_output_init_mode_table ();
2581 lto_push_out_decl_state (state);
2582 for (lsei = lsei_start_function_in_partition (encoder);
2583 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2585 struct cgraph_node *node = lsei_cgraph_node (lsei);
2586 /* When streaming out references to statements as part of some IPA
2587 pass summary, the statements need to have uids assigned.
2589 For functions newly born at WPA stage we need to initialize
2590 the uids here. */
2591 if (node->definition
2592 && gimple_has_body_p (node->decl))
2594 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2595 renumber_gimple_stmt_uids ();
2596 pop_cfun ();
2600 gcc_assert (flag_wpa);
2601 pass_manager *passes = g->get_passes ();
2602 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2604 write_lto ();
2606 gcc_assert (lto_get_out_decl_state () == state);
2607 lto_pop_out_decl_state ();
2608 lto_delete_out_decl_state (state);
2611 /* Same as execute_pass_list but assume that subpasses of IPA passes
2612 are local passes. */
2614 static void
2615 ipa_read_summaries_1 (opt_pass *pass)
2617 while (pass)
2619 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2621 gcc_assert (!current_function_decl);
2622 gcc_assert (!cfun);
2623 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2625 if (pass->gate (cfun))
2627 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2629 /* If a timevar is present, start it. */
2630 if (pass->tv_id)
2631 timevar_push (pass->tv_id);
2633 pass_init_dump_file (pass);
2635 current_pass = pass;
2636 ipa_pass->read_summary ();
2638 pass_fini_dump_file (pass);
2640 /* Stop timevar. */
2641 if (pass->tv_id)
2642 timevar_pop (pass->tv_id);
2645 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2646 ipa_read_summaries_1 (pass->sub);
2648 pass = pass->next;
2653 /* Read all the summaries for all_regular_ipa_passes. */
2655 void
2656 ipa_read_summaries (void)
2658 pass_manager *passes = g->get_passes ();
2659 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2662 /* Same as execute_pass_list but assume that subpasses of IPA passes
2663 are local passes. */
2665 static void
2666 ipa_read_optimization_summaries_1 (opt_pass *pass)
2668 while (pass)
2670 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2672 gcc_assert (!current_function_decl);
2673 gcc_assert (!cfun);
2674 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2676 if (pass->gate (cfun))
2678 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2680 /* If a timevar is present, start it. */
2681 if (pass->tv_id)
2682 timevar_push (pass->tv_id);
2684 pass_init_dump_file (pass);
2686 current_pass = pass;
2687 ipa_pass->read_optimization_summary ();
2689 pass_fini_dump_file (pass);
2691 /* Stop timevar. */
2692 if (pass->tv_id)
2693 timevar_pop (pass->tv_id);
2696 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2697 ipa_read_optimization_summaries_1 (pass->sub);
2699 pass = pass->next;
2703 /* Read all the summaries for all_regular_ipa_passes. */
2705 void
2706 ipa_read_optimization_summaries (void)
2708 pass_manager *passes = g->get_passes ();
2709 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2712 /* Same as execute_pass_list but assume that subpasses of IPA passes
2713 are local passes. */
2714 void
2715 execute_ipa_pass_list (opt_pass *pass)
2719 gcc_assert (!current_function_decl);
2720 gcc_assert (!cfun);
2721 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2722 if (execute_one_pass (pass) && pass->sub)
2724 if (pass->sub->type == GIMPLE_PASS)
2726 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2727 do_per_function_toporder ((void (*)(function *, void *))
2728 execute_pass_list,
2729 pass->sub);
2730 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2732 else if (pass->sub->type == SIMPLE_IPA_PASS
2733 || pass->sub->type == IPA_PASS)
2734 execute_ipa_pass_list (pass->sub);
2735 else
2736 gcc_unreachable ();
2738 gcc_assert (!current_function_decl);
2739 symtab->process_new_functions ();
2740 pass = pass->next;
2742 while (pass);
2745 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2747 static void
2748 execute_ipa_stmt_fixups (opt_pass *pass,
2749 struct cgraph_node *node, gimple *stmts)
2751 while (pass)
2753 /* Execute all of the IPA_PASSes in the list. */
2754 if (pass->type == IPA_PASS
2755 && pass->gate (cfun))
2757 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2759 if (ipa_pass->stmt_fixup)
2761 pass_init_dump_file (pass);
2762 /* If a timevar is present, start it. */
2763 if (pass->tv_id)
2764 timevar_push (pass->tv_id);
2766 current_pass = pass;
2767 ipa_pass->stmt_fixup (node, stmts);
2769 /* Stop timevar. */
2770 if (pass->tv_id)
2771 timevar_pop (pass->tv_id);
2772 pass_fini_dump_file (pass);
2774 if (pass->sub)
2775 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2777 pass = pass->next;
2781 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2783 void
2784 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2786 pass_manager *passes = g->get_passes ();
2787 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2791 extern void debug_properties (unsigned int);
2792 extern void dump_properties (FILE *, unsigned int);
2794 DEBUG_FUNCTION void
2795 dump_properties (FILE *dump, unsigned int props)
2797 fprintf (dump, "Properties:\n");
2798 if (props & PROP_gimple_any)
2799 fprintf (dump, "PROP_gimple_any\n");
2800 if (props & PROP_gimple_lcf)
2801 fprintf (dump, "PROP_gimple_lcf\n");
2802 if (props & PROP_gimple_leh)
2803 fprintf (dump, "PROP_gimple_leh\n");
2804 if (props & PROP_cfg)
2805 fprintf (dump, "PROP_cfg\n");
2806 if (props & PROP_ssa)
2807 fprintf (dump, "PROP_ssa\n");
2808 if (props & PROP_no_crit_edges)
2809 fprintf (dump, "PROP_no_crit_edges\n");
2810 if (props & PROP_rtl)
2811 fprintf (dump, "PROP_rtl\n");
2812 if (props & PROP_gimple_lomp)
2813 fprintf (dump, "PROP_gimple_lomp\n");
2814 if (props & PROP_gimple_lcx)
2815 fprintf (dump, "PROP_gimple_lcx\n");
2816 if (props & PROP_gimple_lvec)
2817 fprintf (dump, "PROP_gimple_lvec\n");
2818 if (props & PROP_cfglayout)
2819 fprintf (dump, "PROP_cfglayout\n");
2822 DEBUG_FUNCTION void
2823 debug_properties (unsigned int props)
2825 dump_properties (stderr, props);
2828 /* Called by local passes to see if function is called by already processed nodes.
2829 Because we process nodes in topological order, this means that function is
2830 in recursive cycle or we introduced new direct calls. */
2831 bool
2832 function_called_by_processed_nodes_p (void)
2834 struct cgraph_edge *e;
2835 for (e = cgraph_node::get (current_function_decl)->callers;
2837 e = e->next_caller)
2839 if (e->caller->decl == current_function_decl)
2840 continue;
2841 if (!e->caller->has_gimple_body_p ())
2842 continue;
2843 if (TREE_ASM_WRITTEN (e->caller->decl))
2844 continue;
2845 if (!e->caller->process && !e->caller->global.inlined_to)
2846 break;
2848 if (dump_file && e)
2850 fprintf (dump_file, "Already processed call to:\n");
2851 e->caller->dump (dump_file);
2853 return e != NULL;
2856 #include "gt-passes.h"