PR ipa/64481
[official-gcc.git] / gcc / passes.c
blob52dc067900eb5e234cbc071d84eedb5ec25c1fc5
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 "tm.h"
29 #include "line-map.h"
30 #include "input.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "vec.h"
34 #include "double-int.h"
35 #include "input.h"
36 #include "alias.h"
37 #include "symtab.h"
38 #include "wide-int.h"
39 #include "inchash.h"
40 #include "tree.h"
41 #include "fold-const.h"
42 #include "varasm.h"
43 #include "rtl.h"
44 #include "tm_p.h"
45 #include "flags.h"
46 #include "insn-attr.h"
47 #include "insn-config.h"
48 #include "insn-flags.h"
49 #include "hard-reg-set.h"
50 #include "recog.h"
51 #include "output.h"
52 #include "except.h"
53 #include "function.h"
54 #include "toplev.h"
55 #include "expr.h"
56 #include "predict.h"
57 #include "basic-block.h"
58 #include "intl.h"
59 #include "graph.h"
60 #include "regs.h"
61 #include "diagnostic-core.h"
62 #include "params.h"
63 #include "reload.h"
64 #include "debug.h"
65 #include "target.h"
66 #include "langhooks.h"
67 #include "cfgloop.h"
68 #include "hosthooks.h"
69 #include "opts.h"
70 #include "coverage.h"
71 #include "value-prof.h"
72 #include "tree-inline.h"
73 #include "tree-ssa-alias.h"
74 #include "internal-fn.h"
75 #include "gimple-expr.h"
76 #include "is-a.h"
77 #include "gimple.h"
78 #include "gimple-ssa.h"
79 #include "tree-cfg.h"
80 #include "stringpool.h"
81 #include "tree-ssanames.h"
82 #include "tree-ssa-loop-manip.h"
83 #include "tree-into-ssa.h"
84 #include "tree-dfa.h"
85 #include "tree-ssa.h"
86 #include "tree-pass.h"
87 #include "tree-dump.h"
88 #include "df.h"
89 #include "hash-map.h"
90 #include "plugin-api.h"
91 #include "ipa-ref.h"
92 #include "cgraph.h"
93 #include "lto-streamer.h"
94 #include "plugin.h"
95 #include "ipa-utils.h"
96 #include "tree-pretty-print.h" /* for dump_function_header */
97 #include "context.h"
98 #include "pass_manager.h"
99 #include "tree-ssa-live.h" /* For remove_unused_locals. */
100 #include "tree-cfgcleanup.h"
102 using namespace gcc;
104 /* This is used for debugging. It allows the current pass to printed
105 from anywhere in compilation.
106 The variable current_pass is also used for statistics and plugins. */
107 opt_pass *current_pass;
109 static void register_pass_name (opt_pass *, const char *);
111 /* Most passes are single-instance (within their context) and thus don't
112 need to implement cloning, but passes that support multiple instances
113 *must* provide their own implementation of the clone method.
115 Handle this by providing a default implemenation, but make it a fatal
116 error to call it. */
118 opt_pass *
119 opt_pass::clone ()
121 internal_error ("pass %s does not support cloning", name);
124 bool
125 opt_pass::gate (function *)
127 return true;
130 unsigned int
131 opt_pass::execute (function *)
133 return 0;
136 opt_pass::opt_pass (const pass_data &data, context *ctxt)
137 : pass_data (data),
138 sub (NULL),
139 next (NULL),
140 static_pass_number (0),
141 m_ctxt (ctxt)
146 void
147 pass_manager::execute_early_local_passes ()
149 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
150 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
151 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
154 unsigned int
155 pass_manager::execute_pass_mode_switching ()
157 return pass_mode_switching_1->execute (cfun);
161 /* Call from anywhere to find out what pass this is. Useful for
162 printing out debugging information deep inside an service
163 routine. */
164 void
165 print_current_pass (FILE *file)
167 if (current_pass)
168 fprintf (file, "current pass = %s (%d)\n",
169 current_pass->name, current_pass->static_pass_number);
170 else
171 fprintf (file, "no current pass.\n");
175 /* Call from the debugger to get the current pass name. */
176 DEBUG_FUNCTION void
177 debug_pass (void)
179 print_current_pass (stderr);
184 /* Global variables used to communicate with passes. */
185 bool in_gimple_form;
186 bool first_pass_instance;
189 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
190 and TYPE_DECL nodes.
192 This does nothing for local (non-static) variables, unless the
193 variable is a register variable with DECL_ASSEMBLER_NAME set. In
194 that case, or if the variable is not an automatic, it sets up the
195 RTL and outputs any assembler code (label definition, storage
196 allocation and initialization).
198 DECL is the declaration. TOP_LEVEL is nonzero
199 if this declaration is not within a function. */
201 void
202 rest_of_decl_compilation (tree decl,
203 int top_level,
204 int at_end)
206 bool finalize = true;
208 /* We deferred calling assemble_alias so that we could collect
209 other attributes such as visibility. Emit the alias now. */
210 if (!in_lto_p)
212 tree alias;
213 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
214 if (alias)
216 alias = TREE_VALUE (TREE_VALUE (alias));
217 alias = get_identifier (TREE_STRING_POINTER (alias));
218 /* A quirk of the initial implementation of aliases required that the
219 user add "extern" to all of them. Which is silly, but now
220 historical. Do note that the symbol is in fact locally defined. */
221 DECL_EXTERNAL (decl) = 0;
222 TREE_STATIC (decl) = 1;
223 assemble_alias (decl, alias);
224 finalize = false;
228 /* Can't defer this, because it needs to happen before any
229 later function definitions are processed. */
230 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
231 make_decl_rtl (decl);
233 /* Forward declarations for nested functions are not "external",
234 but we need to treat them as if they were. */
235 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
236 || TREE_CODE (decl) == FUNCTION_DECL)
238 timevar_push (TV_VARCONST);
240 /* Don't output anything when a tentative file-scope definition
241 is seen. But at end of compilation, do output code for them.
243 We do output all variables and rely on
244 callgraph code to defer them except for forward declarations
245 (see gcc.c-torture/compile/920624-1.c) */
246 if ((at_end
247 || !DECL_DEFER_OUTPUT (decl)
248 || DECL_INITIAL (decl))
249 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
250 && !DECL_EXTERNAL (decl))
252 /* When reading LTO unit, we also read varpool, so do not
253 rebuild it. */
254 if (in_lto_p && !at_end)
256 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
257 varpool_node::finalize_decl (decl);
260 #ifdef ASM_FINISH_DECLARE_OBJECT
261 if (decl == last_assemble_variable_decl)
263 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
264 top_level, at_end);
266 #endif
268 timevar_pop (TV_VARCONST);
270 else if (TREE_CODE (decl) == TYPE_DECL
271 /* Like in rest_of_type_compilation, avoid confusing the debug
272 information machinery when there are errors. */
273 && !seen_error ())
275 timevar_push (TV_SYMOUT);
276 debug_hooks->type_decl (decl, !top_level);
277 timevar_pop (TV_SYMOUT);
280 /* Let cgraph know about the existence of variables. */
281 if (in_lto_p && !at_end)
283 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
284 && TREE_STATIC (decl))
285 varpool_node::get_create (decl);
288 /* Called after finishing a record, union or enumeral type. */
290 void
291 rest_of_type_compilation (tree type, int toplev)
293 /* Avoid confusing the debug information machinery when there are
294 errors. */
295 if (seen_error ())
296 return;
298 timevar_push (TV_SYMOUT);
299 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
300 timevar_pop (TV_SYMOUT);
305 void
306 pass_manager::
307 finish_optimization_passes (void)
309 int i;
310 struct dump_file_info *dfi;
311 char *name;
312 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
314 timevar_push (TV_DUMP);
315 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
317 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
318 end_branch_prob ();
319 dumps->dump_finish (pass_profile_1->static_pass_number);
322 if (optimize > 0)
324 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
325 print_combine_total_stats ();
326 dumps->dump_finish (pass_profile_1->static_pass_number);
329 /* Do whatever is necessary to finish printing the graphs. */
330 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
331 if (dumps->dump_initialized_p (i)
332 && (dfi->pflags & TDF_GRAPH) != 0
333 && (name = dumps->get_dump_file_name (i)) != NULL)
335 finish_graph_dump_file (name);
336 free (name);
339 timevar_pop (TV_DUMP);
342 static unsigned int
343 execute_build_ssa_passes (void)
345 /* Once this pass (and its sub-passes) are complete, all functions
346 will be in SSA form. Technically this state change is happening
347 a tad early, since the sub-passes have not yet run, but since
348 none of the sub-passes are IPA passes and do not create new
349 functions, this is ok. We're setting this value for the benefit
350 of IPA passes that follow. */
351 if (symtab->state < IPA_SSA)
352 symtab->state = IPA_SSA;
353 return 0;
356 namespace {
358 const pass_data pass_data_build_ssa_passes =
360 SIMPLE_IPA_PASS, /* type */
361 "build_ssa_passes", /* name */
362 OPTGROUP_NONE, /* optinfo_flags */
363 TV_EARLY_LOCAL, /* tv_id */
364 0, /* properties_required */
365 0, /* properties_provided */
366 0, /* properties_destroyed */
367 0, /* todo_flags_start */
368 /* todo_flags_finish is executed before subpases. For this reason
369 it makes no sense to remove unreachable functions here. */
370 0, /* todo_flags_finish */
373 class pass_build_ssa_passes : public simple_ipa_opt_pass
375 public:
376 pass_build_ssa_passes (gcc::context *ctxt)
377 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
380 /* opt_pass methods: */
381 virtual bool gate (function *)
383 /* Don't bother doing anything if the program has errors. */
384 return (!seen_error () && !in_lto_p);
387 virtual unsigned int execute (function *)
389 return execute_build_ssa_passes ();
392 }; // class pass_build_ssa_passes
394 const pass_data pass_data_chkp_instrumentation_passes =
396 SIMPLE_IPA_PASS, /* type */
397 "chkp_passes", /* name */
398 OPTGROUP_NONE, /* optinfo_flags */
399 TV_NONE, /* tv_id */
400 0, /* properties_required */
401 0, /* properties_provided */
402 0, /* properties_destroyed */
403 0, /* todo_flags_start */
404 0, /* todo_flags_finish */
407 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
409 public:
410 pass_chkp_instrumentation_passes (gcc::context *ctxt)
411 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
414 /* opt_pass methods: */
415 virtual bool gate (function *)
417 /* Don't bother doing anything if the program has errors. */
418 return (!seen_error () && !in_lto_p);
421 }; // class pass_chkp_instrumentation_passes
423 const pass_data pass_data_local_optimization_passes =
425 SIMPLE_IPA_PASS, /* type */
426 "opt_local_passes", /* name */
427 OPTGROUP_NONE, /* optinfo_flags */
428 TV_NONE, /* tv_id */
429 0, /* properties_required */
430 0, /* properties_provided */
431 0, /* properties_destroyed */
432 0, /* todo_flags_start */
433 0, /* todo_flags_finish */
436 class pass_local_optimization_passes : public simple_ipa_opt_pass
438 public:
439 pass_local_optimization_passes (gcc::context *ctxt)
440 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
443 /* opt_pass methods: */
444 virtual bool gate (function *)
446 /* Don't bother doing anything if the program has errors. */
447 return (!seen_error () && !in_lto_p);
450 }; // class pass_local_optimization_passes
452 } // anon namespace
454 simple_ipa_opt_pass *
455 make_pass_build_ssa_passes (gcc::context *ctxt)
457 return new pass_build_ssa_passes (ctxt);
460 simple_ipa_opt_pass *
461 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
463 return new pass_chkp_instrumentation_passes (ctxt);
466 simple_ipa_opt_pass *
467 make_pass_local_optimization_passes (gcc::context *ctxt)
469 return new pass_local_optimization_passes (ctxt);
472 namespace {
474 const pass_data pass_data_all_early_optimizations =
476 GIMPLE_PASS, /* type */
477 "early_optimizations", /* name */
478 OPTGROUP_NONE, /* optinfo_flags */
479 TV_NONE, /* tv_id */
480 0, /* properties_required */
481 0, /* properties_provided */
482 0, /* properties_destroyed */
483 0, /* todo_flags_start */
484 0, /* todo_flags_finish */
487 class pass_all_early_optimizations : public gimple_opt_pass
489 public:
490 pass_all_early_optimizations (gcc::context *ctxt)
491 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
494 /* opt_pass methods: */
495 virtual bool gate (function *)
497 return (optimize >= 1
498 /* Don't bother doing anything if the program has errors. */
499 && !seen_error ());
502 }; // class pass_all_early_optimizations
504 } // anon namespace
506 static gimple_opt_pass *
507 make_pass_all_early_optimizations (gcc::context *ctxt)
509 return new pass_all_early_optimizations (ctxt);
512 namespace {
514 const pass_data pass_data_all_optimizations =
516 GIMPLE_PASS, /* type */
517 "*all_optimizations", /* name */
518 OPTGROUP_NONE, /* optinfo_flags */
519 TV_OPTIMIZE, /* tv_id */
520 0, /* properties_required */
521 0, /* properties_provided */
522 0, /* properties_destroyed */
523 0, /* todo_flags_start */
524 0, /* todo_flags_finish */
527 class pass_all_optimizations : public gimple_opt_pass
529 public:
530 pass_all_optimizations (gcc::context *ctxt)
531 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
534 /* opt_pass methods: */
535 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
537 }; // class pass_all_optimizations
539 } // anon namespace
541 static gimple_opt_pass *
542 make_pass_all_optimizations (gcc::context *ctxt)
544 return new pass_all_optimizations (ctxt);
547 namespace {
549 const pass_data pass_data_all_optimizations_g =
551 GIMPLE_PASS, /* type */
552 "*all_optimizations_g", /* name */
553 OPTGROUP_NONE, /* optinfo_flags */
554 TV_OPTIMIZE, /* tv_id */
555 0, /* properties_required */
556 0, /* properties_provided */
557 0, /* properties_destroyed */
558 0, /* todo_flags_start */
559 0, /* todo_flags_finish */
562 class pass_all_optimizations_g : public gimple_opt_pass
564 public:
565 pass_all_optimizations_g (gcc::context *ctxt)
566 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
569 /* opt_pass methods: */
570 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
572 }; // class pass_all_optimizations_g
574 } // anon namespace
576 static gimple_opt_pass *
577 make_pass_all_optimizations_g (gcc::context *ctxt)
579 return new pass_all_optimizations_g (ctxt);
582 namespace {
584 const pass_data pass_data_rest_of_compilation =
586 RTL_PASS, /* type */
587 "*rest_of_compilation", /* name */
588 OPTGROUP_NONE, /* optinfo_flags */
589 TV_REST_OF_COMPILATION, /* tv_id */
590 PROP_rtl, /* properties_required */
591 0, /* properties_provided */
592 0, /* properties_destroyed */
593 0, /* todo_flags_start */
594 0, /* todo_flags_finish */
597 class pass_rest_of_compilation : public rtl_opt_pass
599 public:
600 pass_rest_of_compilation (gcc::context *ctxt)
601 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
604 /* opt_pass methods: */
605 virtual bool gate (function *)
607 /* Early return if there were errors. We can run afoul of our
608 consistency checks, and there's not really much point in fixing them. */
609 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
612 }; // class pass_rest_of_compilation
614 } // anon namespace
616 static rtl_opt_pass *
617 make_pass_rest_of_compilation (gcc::context *ctxt)
619 return new pass_rest_of_compilation (ctxt);
622 namespace {
624 const pass_data pass_data_postreload =
626 RTL_PASS, /* type */
627 "*all-postreload", /* name */
628 OPTGROUP_NONE, /* optinfo_flags */
629 TV_POSTRELOAD, /* tv_id */
630 PROP_rtl, /* properties_required */
631 0, /* properties_provided */
632 0, /* properties_destroyed */
633 0, /* todo_flags_start */
634 0, /* todo_flags_finish */
637 class pass_postreload : public rtl_opt_pass
639 public:
640 pass_postreload (gcc::context *ctxt)
641 : rtl_opt_pass (pass_data_postreload, ctxt)
644 /* opt_pass methods: */
645 virtual bool gate (function *) { return reload_completed; }
647 }; // class pass_postreload
649 } // anon namespace
651 static rtl_opt_pass *
652 make_pass_postreload (gcc::context *ctxt)
654 return new pass_postreload (ctxt);
657 namespace {
659 const pass_data pass_data_late_compilation =
661 RTL_PASS, /* type */
662 "*all-late_compilation", /* name */
663 OPTGROUP_NONE, /* optinfo_flags */
664 TV_LATE_COMPILATION, /* tv_id */
665 PROP_rtl, /* properties_required */
666 0, /* properties_provided */
667 0, /* properties_destroyed */
668 0, /* todo_flags_start */
669 0, /* todo_flags_finish */
672 class pass_late_compilation : public rtl_opt_pass
674 public:
675 pass_late_compilation (gcc::context *ctxt)
676 : rtl_opt_pass (pass_data_late_compilation, ctxt)
679 /* opt_pass methods: */
680 virtual bool gate (function *)
682 return reload_completed || targetm.no_register_allocation;
685 }; // class pass_late_compilation
687 } // anon namespace
689 static rtl_opt_pass *
690 make_pass_late_compilation (gcc::context *ctxt)
692 return new pass_late_compilation (ctxt);
697 /* Set the static pass number of pass PASS to ID and record that
698 in the mapping from static pass number to pass. */
700 void
701 pass_manager::
702 set_pass_for_id (int id, opt_pass *pass)
704 pass->static_pass_number = id;
705 if (passes_by_id_size <= id)
707 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
708 memset (passes_by_id + passes_by_id_size, 0,
709 (id + 1 - passes_by_id_size) * sizeof (void *));
710 passes_by_id_size = id + 1;
712 passes_by_id[id] = pass;
715 /* Return the pass with the static pass number ID. */
717 opt_pass *
718 pass_manager::get_pass_for_id (int id) const
720 if (id >= passes_by_id_size)
721 return NULL;
722 return passes_by_id[id];
725 /* Iterate over the pass tree allocating dump file numbers. We want
726 to do this depth first, and independent of whether the pass is
727 enabled or not. */
729 void
730 register_one_dump_file (opt_pass *pass)
732 g->get_passes ()->register_one_dump_file (pass);
735 void
736 pass_manager::register_one_dump_file (opt_pass *pass)
738 char *dot_name, *flag_name, *glob_name;
739 const char *name, *full_name, *prefix;
740 char num[10];
741 int flags, id;
742 int optgroup_flags = OPTGROUP_NONE;
743 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
745 /* See below in next_pass_1. */
746 num[0] = '\0';
747 if (pass->static_pass_number != -1)
748 sprintf (num, "%d", ((int) pass->static_pass_number < 0
749 ? 1 : pass->static_pass_number));
751 /* The name is both used to identify the pass for the purposes of plugins,
752 and to specify dump file name and option.
753 The latter two might want something short which is not quite unique; for
754 that reason, we may have a disambiguating prefix, followed by a space
755 to mark the start of the following dump file name / option string. */
756 name = strchr (pass->name, ' ');
757 name = name ? name + 1 : pass->name;
758 dot_name = concat (".", name, num, NULL);
759 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
761 prefix = "ipa-";
762 flags = TDF_IPA;
763 optgroup_flags |= OPTGROUP_IPA;
765 else if (pass->type == GIMPLE_PASS)
767 prefix = "tree-";
768 flags = TDF_TREE;
770 else
772 prefix = "rtl-";
773 flags = TDF_RTL;
776 flag_name = concat (prefix, name, num, NULL);
777 glob_name = concat (prefix, name, NULL);
778 optgroup_flags |= pass->optinfo_flags;
779 /* For any passes that do not have an optgroup set, and which are not
780 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
781 any dump messages are emitted properly under -fopt-info(-optall). */
782 if (optgroup_flags == OPTGROUP_NONE)
783 optgroup_flags = OPTGROUP_OTHER;
784 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
785 optgroup_flags,
786 true);
787 set_pass_for_id (id, pass);
788 full_name = concat (prefix, pass->name, num, NULL);
789 register_pass_name (pass, full_name);
790 free (CONST_CAST (char *, full_name));
793 /* Register the dump files for the pass_manager starting at PASS. */
795 void
796 pass_manager::register_dump_files (opt_pass *pass)
800 if (pass->name && pass->name[0] != '*')
801 register_one_dump_file (pass);
803 if (pass->sub)
804 register_dump_files (pass->sub);
806 pass = pass->next;
808 while (pass);
811 /* Helper for pass_registry hash table. */
813 struct pass_registry_hasher : default_hashmap_traits
815 static inline hashval_t hash (const char *);
816 static inline bool equal_keys (const char *, const char *);
819 /* Pass registry hash function. */
821 inline hashval_t
822 pass_registry_hasher::hash (const char *name)
824 return htab_hash_string (name);
827 /* Hash equal function */
829 inline bool
830 pass_registry_hasher::equal_keys (const char *s1, const char *s2)
832 return !strcmp (s1, s2);
835 static hash_map<const char *, opt_pass *, pass_registry_hasher>
836 *name_to_pass_map;
838 /* Register PASS with NAME. */
840 static void
841 register_pass_name (opt_pass *pass, const char *name)
843 if (!name_to_pass_map)
844 name_to_pass_map
845 = new hash_map<const char *, opt_pass *, pass_registry_hasher> (256);
847 if (name_to_pass_map->get (name))
848 return; /* Ignore plugin passes. */
850 const char *unique_name = xstrdup (name);
851 name_to_pass_map->put (unique_name, pass);
854 /* Map from pass id to canonicalized pass name. */
856 typedef const char *char_ptr;
857 static vec<char_ptr> pass_tab = vNULL;
859 /* Callback function for traversing NAME_TO_PASS_MAP. */
861 bool
862 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
864 gcc_assert (pass->static_pass_number > 0);
865 gcc_assert (pass_tab.exists ());
867 pass_tab[pass->static_pass_number] = name;
869 return 1;
872 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
873 table for dumping purpose. */
875 static void
876 create_pass_tab (void)
878 if (!flag_dump_passes)
879 return;
881 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
882 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
885 static bool override_gate_status (opt_pass *, tree, bool);
887 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
888 is turned on or not. */
890 static void
891 dump_one_pass (opt_pass *pass, int pass_indent)
893 int indent = 3 * pass_indent;
894 const char *pn;
895 bool is_on, is_really_on;
897 is_on = pass->gate (cfun);
898 is_really_on = override_gate_status (pass, current_function_decl, is_on);
900 if (pass->static_pass_number <= 0)
901 pn = pass->name;
902 else
903 pn = pass_tab[pass->static_pass_number];
905 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
906 (15 - indent < 0 ? 0 : 15 - indent), " ",
907 is_on ? " ON" : " OFF",
908 ((!is_on) == (!is_really_on) ? ""
909 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
912 /* Dump pass list PASS with indentation INDENT. */
914 static void
915 dump_pass_list (opt_pass *pass, int indent)
919 dump_one_pass (pass, indent);
920 if (pass->sub)
921 dump_pass_list (pass->sub, indent + 1);
922 pass = pass->next;
924 while (pass);
927 /* Dump all optimization passes. */
929 void
930 dump_passes (void)
932 g->get_passes ()->dump_passes ();
935 void
936 pass_manager::dump_passes () const
938 struct cgraph_node *n, *node = NULL;
940 create_pass_tab ();
942 FOR_EACH_FUNCTION (n)
943 if (DECL_STRUCT_FUNCTION (n->decl))
945 node = n;
946 break;
949 if (!node)
950 return;
952 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
954 dump_pass_list (all_lowering_passes, 1);
955 dump_pass_list (all_small_ipa_passes, 1);
956 dump_pass_list (all_regular_ipa_passes, 1);
957 dump_pass_list (all_late_ipa_passes, 1);
958 dump_pass_list (all_passes, 1);
960 pop_cfun ();
964 /* Returns the pass with NAME. */
966 static opt_pass *
967 get_pass_by_name (const char *name)
969 opt_pass **p = name_to_pass_map->get (name);
970 if (p)
971 return *p;
973 return NULL;
977 /* Range [start, last]. */
979 struct uid_range
981 unsigned int start;
982 unsigned int last;
983 const char *assem_name;
984 struct uid_range *next;
987 typedef struct uid_range *uid_range_p;
990 static vec<uid_range_p>
991 enabled_pass_uid_range_tab = vNULL;
992 static vec<uid_range_p>
993 disabled_pass_uid_range_tab = vNULL;
996 /* Parse option string for -fdisable- and -fenable-
997 The syntax of the options:
999 -fenable-<pass_name>
1000 -fdisable-<pass_name>
1002 -fenable-<pass_name>=s1:e1,s2:e2,...
1003 -fdisable-<pass_name>=s1:e1,s2:e2,...
1006 static void
1007 enable_disable_pass (const char *arg, bool is_enable)
1009 opt_pass *pass;
1010 char *range_str, *phase_name;
1011 char *argstr = xstrdup (arg);
1012 vec<uid_range_p> *tab = 0;
1014 range_str = strchr (argstr,'=');
1015 if (range_str)
1017 *range_str = '\0';
1018 range_str++;
1021 phase_name = argstr;
1022 if (!*phase_name)
1024 if (is_enable)
1025 error ("unrecognized option -fenable");
1026 else
1027 error ("unrecognized option -fdisable");
1028 free (argstr);
1029 return;
1031 pass = get_pass_by_name (phase_name);
1032 if (!pass || pass->static_pass_number == -1)
1034 if (is_enable)
1035 error ("unknown pass %s specified in -fenable", phase_name);
1036 else
1037 error ("unknown pass %s specified in -fdisable", phase_name);
1038 free (argstr);
1039 return;
1042 if (is_enable)
1043 tab = &enabled_pass_uid_range_tab;
1044 else
1045 tab = &disabled_pass_uid_range_tab;
1047 if ((unsigned) pass->static_pass_number >= tab->length ())
1048 tab->safe_grow_cleared (pass->static_pass_number + 1);
1050 if (!range_str)
1052 uid_range_p slot;
1053 uid_range_p new_range = XCNEW (struct uid_range);
1055 new_range->start = 0;
1056 new_range->last = (unsigned)-1;
1058 slot = (*tab)[pass->static_pass_number];
1059 new_range->next = slot;
1060 (*tab)[pass->static_pass_number] = new_range;
1061 if (is_enable)
1062 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1063 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1064 else
1065 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1066 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1068 else
1070 char *next_range = NULL;
1071 char *one_range = range_str;
1072 char *end_val = NULL;
1076 uid_range_p slot;
1077 uid_range_p new_range;
1078 char *invalid = NULL;
1079 long start;
1080 char *func_name = NULL;
1082 next_range = strchr (one_range, ',');
1083 if (next_range)
1085 *next_range = '\0';
1086 next_range++;
1089 end_val = strchr (one_range, ':');
1090 if (end_val)
1092 *end_val = '\0';
1093 end_val++;
1095 start = strtol (one_range, &invalid, 10);
1096 if (*invalid || start < 0)
1098 if (end_val || (one_range[0] >= '0'
1099 && one_range[0] <= '9'))
1101 error ("Invalid range %s in option %s",
1102 one_range,
1103 is_enable ? "-fenable" : "-fdisable");
1104 free (argstr);
1105 return;
1107 func_name = one_range;
1109 if (!end_val)
1111 new_range = XCNEW (struct uid_range);
1112 if (!func_name)
1114 new_range->start = (unsigned) start;
1115 new_range->last = (unsigned) start;
1117 else
1119 new_range->start = (unsigned) -1;
1120 new_range->last = (unsigned) -1;
1121 new_range->assem_name = xstrdup (func_name);
1124 else
1126 long last = strtol (end_val, &invalid, 10);
1127 if (*invalid || last < start)
1129 error ("Invalid range %s in option %s",
1130 end_val,
1131 is_enable ? "-fenable" : "-fdisable");
1132 free (argstr);
1133 return;
1135 new_range = XCNEW (struct uid_range);
1136 new_range->start = (unsigned) start;
1137 new_range->last = (unsigned) last;
1140 slot = (*tab)[pass->static_pass_number];
1141 new_range->next = slot;
1142 (*tab)[pass->static_pass_number] = new_range;
1143 if (is_enable)
1145 if (new_range->assem_name)
1146 inform (UNKNOWN_LOCATION,
1147 "enable pass %s for function %s",
1148 phase_name, new_range->assem_name);
1149 else
1150 inform (UNKNOWN_LOCATION,
1151 "enable pass %s for functions in the range of [%u, %u]",
1152 phase_name, new_range->start, new_range->last);
1154 else
1156 if (new_range->assem_name)
1157 inform (UNKNOWN_LOCATION,
1158 "disable pass %s for function %s",
1159 phase_name, new_range->assem_name);
1160 else
1161 inform (UNKNOWN_LOCATION,
1162 "disable pass %s for functions in the range of [%u, %u]",
1163 phase_name, new_range->start, new_range->last);
1166 one_range = next_range;
1167 } while (next_range);
1170 free (argstr);
1173 /* Enable pass specified by ARG. */
1175 void
1176 enable_pass (const char *arg)
1178 enable_disable_pass (arg, true);
1181 /* Disable pass specified by ARG. */
1183 void
1184 disable_pass (const char *arg)
1186 enable_disable_pass (arg, false);
1189 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1191 static bool
1192 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1193 tree func,
1194 vec<uid_range_p> tab)
1196 uid_range_p slot, range;
1197 int cgraph_uid;
1198 const char *aname = NULL;
1200 if (!tab.exists ()
1201 || (unsigned) pass->static_pass_number >= tab.length ()
1202 || pass->static_pass_number == -1)
1203 return false;
1205 slot = tab[pass->static_pass_number];
1206 if (!slot)
1207 return false;
1209 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1210 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1211 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1213 range = slot;
1214 while (range)
1216 if ((unsigned) cgraph_uid >= range->start
1217 && (unsigned) cgraph_uid <= range->last)
1218 return true;
1219 if (range->assem_name && aname
1220 && !strcmp (range->assem_name, aname))
1221 return true;
1222 range = range->next;
1225 return false;
1229 /* Update static_pass_number for passes (and the flag
1230 TODO_mark_first_instance).
1232 Passes are constructed with static_pass_number preinitialized to 0
1234 This field is used in two different ways: initially as instance numbers
1235 of their kind, and then as ids within the entire pass manager.
1237 Within pass_manager::pass_manager:
1239 * In add_pass_instance(), as called by next_pass_1 in
1240 NEXT_PASS in init_optimization_passes
1242 * When the initial instance of a pass within a pass manager is seen,
1243 it is flagged, and its static_pass_number is set to -1
1245 * On subsequent times that it is seen, the static pass number
1246 is decremented each time, so that if there are e.g. 4 dups,
1247 they have static_pass_number -4, 2, 3, 4 respectively (note
1248 how the initial one is negative and gives the count); these
1249 can be thought of as instance numbers of the specific pass
1251 * Within the register_dump_files () traversal, set_pass_for_id()
1252 is called on each pass, using these instance numbers to create
1253 dumpfile switches, and then overwriting them with a pass id,
1254 which are global to the whole pass manager (based on
1255 (TDI_end + current value of extra_dump_files_in_use) ) */
1257 static void
1258 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1259 opt_pass *initial_pass)
1261 /* Are we dealing with the first pass of its kind, or a clone? */
1262 if (new_pass != initial_pass)
1264 /* We're dealing with a clone. */
1265 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1267 /* Indicate to register_dump_files that this pass has duplicates,
1268 and so it should rename the dump file. The first instance will
1269 be -1, and be number of duplicates = -static_pass_number - 1.
1270 Subsequent instances will be > 0 and just the duplicate number. */
1271 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1273 initial_pass->static_pass_number -= 1;
1274 new_pass->static_pass_number = -initial_pass->static_pass_number;
1277 else
1279 /* We're dealing with the first pass of its kind. */
1280 new_pass->todo_flags_start |= TODO_mark_first_instance;
1281 new_pass->static_pass_number = -1;
1283 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1287 /* Add a pass to the pass list. Duplicate the pass if it's already
1288 in the list. */
1290 static opt_pass **
1291 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1293 /* Every pass should have a name so that plugins can refer to them. */
1294 gcc_assert (pass->name != NULL);
1296 add_pass_instance (pass, false, initial_pass);
1297 *list = pass;
1299 return &(*list)->next;
1302 /* List node for an inserted pass instance. We need to keep track of all
1303 the newly-added pass instances (with 'added_pass_nodes' defined below)
1304 so that we can register their dump files after pass-positioning is finished.
1305 Registering dumping files needs to be post-processed or the
1306 static_pass_number of the opt_pass object would be modified and mess up
1307 the dump file names of future pass instances to be added. */
1309 struct pass_list_node
1311 opt_pass *pass;
1312 struct pass_list_node *next;
1315 static struct pass_list_node *added_pass_nodes = NULL;
1316 static struct pass_list_node *prev_added_pass_node;
1318 /* Insert the pass at the proper position. Return true if the pass
1319 is successfully added.
1321 NEW_PASS_INFO - new pass to be inserted
1322 PASS_LIST - root of the pass list to insert the new pass to */
1324 static bool
1325 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1327 opt_pass *pass = *pass_list, *prev_pass = NULL;
1328 bool success = false;
1330 for ( ; pass; prev_pass = pass, pass = pass->next)
1332 /* Check if the current pass is of the same type as the new pass and
1333 matches the name and the instance number of the reference pass. */
1334 if (pass->type == new_pass_info->pass->type
1335 && pass->name
1336 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1337 && ((new_pass_info->ref_pass_instance_number == 0)
1338 || (new_pass_info->ref_pass_instance_number ==
1339 pass->static_pass_number)
1340 || (new_pass_info->ref_pass_instance_number == 1
1341 && pass->todo_flags_start & TODO_mark_first_instance)))
1343 opt_pass *new_pass;
1344 struct pass_list_node *new_pass_node;
1346 if (new_pass_info->ref_pass_instance_number == 0)
1348 new_pass = new_pass_info->pass->clone ();
1349 add_pass_instance (new_pass, true, new_pass_info->pass);
1351 else
1353 new_pass = new_pass_info->pass;
1354 add_pass_instance (new_pass, true, new_pass);
1357 /* Insert the new pass instance based on the positioning op. */
1358 switch (new_pass_info->pos_op)
1360 case PASS_POS_INSERT_AFTER:
1361 new_pass->next = pass->next;
1362 pass->next = new_pass;
1364 /* Skip newly inserted pass to avoid repeated
1365 insertions in the case where the new pass and the
1366 existing one have the same name. */
1367 pass = new_pass;
1368 break;
1369 case PASS_POS_INSERT_BEFORE:
1370 new_pass->next = pass;
1371 if (prev_pass)
1372 prev_pass->next = new_pass;
1373 else
1374 *pass_list = new_pass;
1375 break;
1376 case PASS_POS_REPLACE:
1377 new_pass->next = pass->next;
1378 if (prev_pass)
1379 prev_pass->next = new_pass;
1380 else
1381 *pass_list = new_pass;
1382 new_pass->sub = pass->sub;
1383 new_pass->tv_id = pass->tv_id;
1384 pass = new_pass;
1385 break;
1386 default:
1387 error ("invalid pass positioning operation");
1388 return false;
1391 /* Save the newly added pass (instance) in the added_pass_nodes
1392 list so that we can register its dump file later. Note that
1393 we cannot register the dump file now because doing so will modify
1394 the static_pass_number of the opt_pass object and therefore
1395 mess up the dump file name of future instances. */
1396 new_pass_node = XCNEW (struct pass_list_node);
1397 new_pass_node->pass = new_pass;
1398 if (!added_pass_nodes)
1399 added_pass_nodes = new_pass_node;
1400 else
1401 prev_added_pass_node->next = new_pass_node;
1402 prev_added_pass_node = new_pass_node;
1404 success = true;
1407 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1408 success = true;
1411 return success;
1414 /* Hooks a new pass into the pass lists.
1416 PASS_INFO - pass information that specifies the opt_pass object,
1417 reference pass, instance number, and how to position
1418 the pass */
1420 void
1421 register_pass (struct register_pass_info *pass_info)
1423 g->get_passes ()->register_pass (pass_info);
1426 void
1427 register_pass (opt_pass* pass, pass_positioning_ops pos,
1428 const char* ref_pass_name, int ref_pass_inst_number)
1430 register_pass_info i;
1431 i.pass = pass;
1432 i.reference_pass_name = ref_pass_name;
1433 i.ref_pass_instance_number = ref_pass_inst_number;
1434 i.pos_op = pos;
1436 g->get_passes ()->register_pass (&i);
1439 void
1440 pass_manager::register_pass (struct register_pass_info *pass_info)
1442 bool all_instances, success;
1443 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1445 /* The checks below could fail in buggy plugins. Existing GCC
1446 passes should never fail these checks, so we mention plugin in
1447 the messages. */
1448 if (!pass_info->pass)
1449 fatal_error ("plugin cannot register a missing pass");
1451 if (!pass_info->pass->name)
1452 fatal_error ("plugin cannot register an unnamed pass");
1454 if (!pass_info->reference_pass_name)
1455 fatal_error
1456 ("plugin cannot register pass %qs without reference pass name",
1457 pass_info->pass->name);
1459 /* Try to insert the new pass to the pass lists. We need to check
1460 all five lists as the reference pass could be in one (or all) of
1461 them. */
1462 all_instances = pass_info->ref_pass_instance_number == 0;
1463 success = position_pass (pass_info, &all_lowering_passes);
1464 if (!success || all_instances)
1465 success |= position_pass (pass_info, &all_small_ipa_passes);
1466 if (!success || all_instances)
1467 success |= position_pass (pass_info, &all_regular_ipa_passes);
1468 if (!success || all_instances)
1469 success |= position_pass (pass_info, &all_late_ipa_passes);
1470 if (!success || all_instances)
1471 success |= position_pass (pass_info, &all_passes);
1472 if (!success)
1473 fatal_error
1474 ("pass %qs not found but is referenced by new pass %qs",
1475 pass_info->reference_pass_name, pass_info->pass->name);
1477 /* OK, we have successfully inserted the new pass. We need to register
1478 the dump files for the newly added pass and its duplicates (if any).
1479 Because the registration of plugin/backend passes happens after the
1480 command-line options are parsed, the options that specify single
1481 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1482 passes. Therefore we currently can only enable dumping of
1483 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1484 are specified. While doing so, we also delete the pass_list_node
1485 objects created during pass positioning. */
1486 while (added_pass_nodes)
1488 struct pass_list_node *next_node = added_pass_nodes->next;
1489 enum tree_dump_index tdi;
1490 register_one_dump_file (added_pass_nodes->pass);
1491 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1492 || added_pass_nodes->pass->type == IPA_PASS)
1493 tdi = TDI_ipa_all;
1494 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1495 tdi = TDI_tree_all;
1496 else
1497 tdi = TDI_rtl_all;
1498 /* Check if dump-all flag is specified. */
1499 if (dumps->get_dump_file_info (tdi)->pstate)
1500 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1501 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1502 XDELETE (added_pass_nodes);
1503 added_pass_nodes = next_node;
1507 /* Construct the pass tree. The sequencing of passes is driven by
1508 the cgraph routines:
1510 finalize_compilation_unit ()
1511 for each node N in the cgraph
1512 cgraph_analyze_function (N)
1513 cgraph_lower_function (N) -> all_lowering_passes
1515 If we are optimizing, compile is then invoked:
1517 compile ()
1518 ipa_passes () -> all_small_ipa_passes
1519 -> Analysis of all_regular_ipa_passes
1520 * possible LTO streaming at copmilation time *
1521 -> Execution of all_regular_ipa_passes
1522 * possible LTO streaming at link time *
1523 -> all_late_ipa_passes
1524 expand_all_functions ()
1525 for each node N in the cgraph
1526 expand_function (N) -> Transformation of all_regular_ipa_passes
1527 -> all_passes
1530 void *
1531 pass_manager::operator new (size_t sz)
1533 /* Ensure that all fields of the pass manager are zero-initialized. */
1534 return xcalloc (1, sz);
1537 void
1538 pass_manager::operator delete (void *ptr)
1540 free (ptr);
1543 pass_manager::pass_manager (context *ctxt)
1544 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1545 all_regular_ipa_passes (NULL),
1546 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1547 m_ctxt (ctxt)
1549 opt_pass **p;
1551 /* Initialize the pass_lists array. */
1552 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1553 GCC_PASS_LISTS
1554 #undef DEF_PASS_LIST
1556 /* Build the tree of passes. */
1558 #define INSERT_PASSES_AFTER(PASS) \
1559 p = &(PASS);
1561 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1563 opt_pass **p = &(PASS ## _1)->sub;
1565 #define POP_INSERT_PASSES() \
1568 #define NEXT_PASS(PASS, NUM) \
1569 do { \
1570 gcc_assert (NULL == PASS ## _ ## NUM); \
1571 if ((NUM) == 1) \
1572 PASS ## _1 = make_##PASS (m_ctxt); \
1573 else \
1575 gcc_assert (PASS ## _1); \
1576 PASS ## _ ## NUM = PASS ## _1->clone (); \
1578 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1579 } while (0)
1581 #define TERMINATE_PASS_LIST() \
1582 *p = NULL;
1584 #include "pass-instances.def"
1586 #undef INSERT_PASSES_AFTER
1587 #undef PUSH_INSERT_PASSES_WITHIN
1588 #undef POP_INSERT_PASSES
1589 #undef NEXT_PASS
1590 #undef TERMINATE_PASS_LIST
1592 /* Register the passes with the tree dump code. */
1593 register_dump_files (all_lowering_passes);
1594 register_dump_files (all_small_ipa_passes);
1595 register_dump_files (all_regular_ipa_passes);
1596 register_dump_files (all_late_ipa_passes);
1597 register_dump_files (all_passes);
1600 static void
1601 delete_pass_tree (opt_pass *pass)
1603 while (pass)
1605 /* Recurse into child passes. */
1606 delete_pass_tree (pass->sub);
1608 opt_pass *next = pass->next;
1610 /* Delete this pass. */
1611 delete pass;
1613 /* Iterate onto sibling passes. */
1614 pass = next;
1618 pass_manager::~pass_manager ()
1620 XDELETEVEC (passes_by_id);
1622 /* Call delete_pass_tree on each of the pass_lists. */
1623 #define DEF_PASS_LIST(LIST) \
1624 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1625 GCC_PASS_LISTS
1626 #undef DEF_PASS_LIST
1630 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1631 function CALLBACK for every function in the call graph. Otherwise,
1632 call CALLBACK on the current function. */
1634 static void
1635 do_per_function (void (*callback) (function *, void *data), void *data)
1637 if (current_function_decl)
1638 callback (cfun, data);
1639 else
1641 struct cgraph_node *node;
1642 FOR_EACH_DEFINED_FUNCTION (node)
1643 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1644 && (!node->clone_of || node->decl != node->clone_of->decl))
1645 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1649 /* Because inlining might remove no-longer reachable nodes, we need to
1650 keep the array visible to garbage collector to avoid reading collected
1651 out nodes. */
1652 static int nnodes;
1653 static GTY ((length ("nnodes"))) cgraph_node **order;
1655 /* Hook called when NODE is removed and therefore should be
1656 excluded from order vector. DATA is an array of integers.
1657 DATA[0] holds max index it may be accessed by. For cgraph
1658 node DATA[node->uid + 1] holds index of this node in order
1659 vector. */
1660 static void
1661 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1663 int *order_idx = (int *)data;
1665 if (node->uid >= order_idx[0])
1666 return;
1668 int idx = order_idx[node->uid + 1];
1669 if (idx >= 0 && idx < nnodes && order[idx] == node)
1670 order[idx] = NULL;
1673 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1674 function CALLBACK for every function in the call graph. Otherwise,
1675 call CALLBACK on the current function.
1676 This function is global so that plugins can use it. */
1677 void
1678 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1680 int i;
1682 if (current_function_decl)
1683 callback (cfun, data);
1684 else
1686 cgraph_node_hook_list *hook;
1687 int *order_idx;
1688 gcc_assert (!order);
1689 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1691 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1692 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1693 order_idx[0] = symtab->cgraph_max_uid;
1695 nnodes = ipa_reverse_postorder (order);
1696 for (i = nnodes - 1; i >= 0; i--)
1698 order[i]->process = 1;
1699 order_idx[order[i]->uid + 1] = i;
1701 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1702 order_idx);
1703 for (i = nnodes - 1; i >= 0; i--)
1705 /* Function could be inlined and removed as unreachable. */
1706 if (!order[i])
1707 continue;
1709 struct cgraph_node *node = order[i];
1711 /* Allow possibly removed nodes to be garbage collected. */
1712 order[i] = NULL;
1713 node->process = 0;
1714 if (node->has_gimple_body_p ())
1715 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1717 symtab->remove_cgraph_removal_hook (hook);
1719 ggc_free (order);
1720 order = NULL;
1721 nnodes = 0;
1724 /* Helper function to perform function body dump. */
1726 static void
1727 execute_function_dump (function *fn, void *data)
1729 opt_pass *pass = (opt_pass *)data;
1731 if (dump_file)
1733 push_cfun (fn);
1735 if (fn->curr_properties & PROP_trees)
1736 dump_function_to_file (fn->decl, dump_file, dump_flags);
1737 else
1738 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1740 /* Flush the file. If verification fails, we won't be able to
1741 close the file before aborting. */
1742 fflush (dump_file);
1744 if ((fn->curr_properties & PROP_cfg)
1745 && (dump_flags & TDF_GRAPH))
1747 if (!pass->graph_dump_initialized)
1749 clean_graph_dump_file (dump_file_name);
1750 pass->graph_dump_initialized = true;
1752 print_graph_cfg (dump_file_name, fn);
1755 pop_cfun ();
1759 static struct profile_record *profile_record;
1761 /* Do profile consistency book-keeping for the pass with static number INDEX.
1762 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1763 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1764 if we are only book-keeping on passes that may have selectively disabled
1765 themselves on a given function. */
1766 static void
1767 check_profile_consistency (int index, int subpass, bool run)
1769 pass_manager *passes = g->get_passes ();
1770 if (index == -1)
1771 return;
1772 if (!profile_record)
1773 profile_record = XCNEWVEC (struct profile_record,
1774 passes->passes_by_id_size);
1775 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1776 gcc_assert (subpass < 2);
1777 profile_record[index].run |= run;
1778 account_profile_record (&profile_record[index], subpass);
1781 /* Output profile consistency. */
1783 void
1784 dump_profile_report (void)
1786 g->get_passes ()->dump_profile_report ();
1789 void
1790 pass_manager::dump_profile_report () const
1792 int i, j;
1793 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1794 gcov_type last_time = 0, last_size = 0;
1795 double rel_time_change, rel_size_change;
1796 int last_reported = 0;
1798 if (!profile_record)
1799 return;
1800 fprintf (stderr, "\nProfile consistency report:\n\n");
1801 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1802 fprintf (stderr, " |freq count |freq count |size time\n");
1804 for (i = 0; i < passes_by_id_size; i++)
1805 for (j = 0 ; j < 2; j++)
1806 if (profile_record[i].run)
1808 if (last_time)
1809 rel_time_change = (profile_record[i].time[j]
1810 - (double)last_time) * 100 / (double)last_time;
1811 else
1812 rel_time_change = 0;
1813 if (last_size)
1814 rel_size_change = (profile_record[i].size[j]
1815 - (double)last_size) * 100 / (double)last_size;
1816 else
1817 rel_size_change = 0;
1819 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1820 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1821 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1822 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1823 || rel_time_change || rel_size_change)
1825 last_reported = i;
1826 fprintf (stderr, "%-20s %s",
1827 passes_by_id [i]->name,
1828 j ? "(after TODO)" : " ");
1829 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1830 fprintf (stderr, "| %+5i",
1831 profile_record[i].num_mismatched_freq_in[j]
1832 - last_freq_in);
1833 else
1834 fprintf (stderr, "| ");
1835 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1836 fprintf (stderr, " %+5i",
1837 profile_record[i].num_mismatched_count_in[j]
1838 - last_count_in);
1839 else
1840 fprintf (stderr, " ");
1841 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1842 fprintf (stderr, "| %+5i",
1843 profile_record[i].num_mismatched_freq_out[j]
1844 - last_freq_out);
1845 else
1846 fprintf (stderr, "| ");
1847 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1848 fprintf (stderr, " %+5i",
1849 profile_record[i].num_mismatched_count_out[j]
1850 - last_count_out);
1851 else
1852 fprintf (stderr, " ");
1854 /* Size/time units change across gimple and RTL. */
1855 if (i == pass_expand_1->static_pass_number)
1856 fprintf (stderr, "|----------");
1857 else
1859 if (rel_size_change)
1860 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1861 else
1862 fprintf (stderr, "| ");
1863 if (rel_time_change)
1864 fprintf (stderr, " %+8.4f%%", rel_time_change);
1866 fprintf (stderr, "\n");
1867 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1868 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1869 last_count_in = profile_record[i].num_mismatched_count_in[j];
1870 last_count_out = profile_record[i].num_mismatched_count_out[j];
1872 else if (j && last_reported != i)
1874 last_reported = i;
1875 fprintf (stderr, "%-20s ------------| | |\n",
1876 passes_by_id [i]->name);
1878 last_time = profile_record[i].time[j];
1879 last_size = profile_record[i].size[j];
1883 /* Perform all TODO actions that ought to be done on each function. */
1885 static void
1886 execute_function_todo (function *fn, void *data)
1888 bool from_ipa_pass = (cfun == NULL);
1889 unsigned int flags = (size_t)data;
1890 flags &= ~fn->last_verified;
1891 if (!flags)
1892 return;
1894 push_cfun (fn);
1896 /* Always cleanup the CFG before trying to update SSA. */
1897 if (flags & TODO_cleanup_cfg)
1899 cleanup_tree_cfg ();
1901 /* When cleanup_tree_cfg merges consecutive blocks, it may
1902 perform some simplistic propagation when removing single
1903 valued PHI nodes. This propagation may, in turn, cause the
1904 SSA form to become out-of-date (see PR 22037). So, even
1905 if the parent pass had not scheduled an SSA update, we may
1906 still need to do one. */
1907 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1908 flags |= TODO_update_ssa;
1911 if (flags & TODO_update_ssa_any)
1913 unsigned update_flags = flags & TODO_update_ssa_any;
1914 update_ssa (update_flags);
1917 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1918 compute_may_aliases ();
1920 if (optimize && (flags & TODO_update_address_taken))
1921 execute_update_addresses_taken ();
1923 if (flags & TODO_remove_unused_locals)
1924 remove_unused_locals ();
1926 if (flags & TODO_rebuild_frequencies)
1927 rebuild_frequencies ();
1929 if (flags & TODO_rebuild_cgraph_edges)
1930 cgraph_edge::rebuild_edges ();
1932 /* If we've seen errors do not bother running any verifiers. */
1933 if (!seen_error ())
1935 #if defined ENABLE_CHECKING
1936 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1937 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1939 if (flags & TODO_verify_il)
1941 if (cfun->curr_properties & PROP_trees)
1943 if (cfun->curr_properties & PROP_cfg)
1944 /* IPA passes leave stmts to be fixed up, so make sure to
1945 not verify stmts really throw. */
1946 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1947 else
1948 verify_gimple_in_seq (gimple_body (cfun->decl));
1950 if (cfun->curr_properties & PROP_ssa)
1951 /* IPA passes leave stmts to be fixed up, so make sure to
1952 not verify SSA operands whose verifier will choke on that. */
1953 verify_ssa (true, !from_ipa_pass);
1954 /* IPA passes leave basic-blocks unsplit, so make sure to
1955 not trip on that. */
1956 if ((cfun->curr_properties & PROP_cfg)
1957 && !from_ipa_pass)
1958 verify_flow_info ();
1959 if (current_loops
1960 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1961 verify_loop_closed_ssa (false);
1962 if (cfun->curr_properties & PROP_rtl)
1963 verify_rtl_sharing ();
1966 /* Make sure verifiers don't change dominator state. */
1967 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1968 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1969 #endif
1972 fn->last_verified = flags & TODO_verify_all;
1974 pop_cfun ();
1976 /* For IPA passes make sure to release dominator info, it can be
1977 computed by non-verifying TODOs. */
1978 if (from_ipa_pass)
1980 free_dominance_info (fn, CDI_DOMINATORS);
1981 free_dominance_info (fn, CDI_POST_DOMINATORS);
1985 /* Perform all TODO actions. */
1986 static void
1987 execute_todo (unsigned int flags)
1989 #if defined ENABLE_CHECKING
1990 if (cfun
1991 && need_ssa_update_p (cfun))
1992 gcc_assert (flags & TODO_update_ssa_any);
1993 #endif
1995 timevar_push (TV_TODO);
1997 /* Inform the pass whether it is the first time it is run. */
1998 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2000 statistics_fini_pass ();
2002 if (flags)
2003 do_per_function (execute_function_todo, (void *)(size_t) flags);
2005 /* Always remove functions just as before inlining: IPA passes might be
2006 interested to see bodies of extern inline functions that are not inlined
2007 to analyze side effects. The full removal is done just at the end
2008 of IPA pass queue. */
2009 if (flags & TODO_remove_functions)
2011 gcc_assert (!cfun);
2012 symtab->remove_unreachable_nodes (dump_file);
2015 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2017 gcc_assert (!cfun);
2018 symtab_node::dump_table (dump_file);
2019 /* Flush the file. If verification fails, we won't be able to
2020 close the file before aborting. */
2021 fflush (dump_file);
2024 /* Now that the dumping has been done, we can get rid of the optional
2025 df problems. */
2026 if (flags & TODO_df_finish)
2027 df_finish_pass ((flags & TODO_df_verify) != 0);
2029 timevar_pop (TV_TODO);
2032 /* Verify invariants that should hold between passes. This is a place
2033 to put simple sanity checks. */
2035 static void
2036 verify_interpass_invariants (void)
2038 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2041 /* Clear the last verified flag. */
2043 static void
2044 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2046 fn->last_verified = 0;
2049 /* Helper function. Verify that the properties has been turn into the
2050 properties expected by the pass. */
2052 #ifdef ENABLE_CHECKING
2053 static void
2054 verify_curr_properties (function *fn, void *data)
2056 unsigned int props = (size_t)data;
2057 gcc_assert ((fn->curr_properties & props) == props);
2059 #endif
2061 /* Initialize pass dump file. */
2062 /* This is non-static so that the plugins can use it. */
2064 bool
2065 pass_init_dump_file (opt_pass *pass)
2067 /* If a dump file name is present, open it if enabled. */
2068 if (pass->static_pass_number != -1)
2070 timevar_push (TV_DUMP);
2071 gcc::dump_manager *dumps = g->get_dumps ();
2072 bool initializing_dump =
2073 !dumps->dump_initialized_p (pass->static_pass_number);
2074 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2075 dumps->dump_start (pass->static_pass_number, &dump_flags);
2076 if (dump_file && current_function_decl)
2077 dump_function_header (dump_file, current_function_decl, dump_flags);
2078 if (initializing_dump
2079 && dump_file && (dump_flags & TDF_GRAPH)
2080 && cfun && (cfun->curr_properties & PROP_cfg))
2082 clean_graph_dump_file (dump_file_name);
2083 pass->graph_dump_initialized = true;
2085 timevar_pop (TV_DUMP);
2086 return initializing_dump;
2088 else
2089 return false;
2092 /* Flush PASS dump file. */
2093 /* This is non-static so that plugins can use it. */
2095 void
2096 pass_fini_dump_file (opt_pass *pass)
2098 timevar_push (TV_DUMP);
2100 /* Flush and close dump file. */
2101 if (dump_file_name)
2103 free (CONST_CAST (char *, dump_file_name));
2104 dump_file_name = NULL;
2107 g->get_dumps ()->dump_finish (pass->static_pass_number);
2108 timevar_pop (TV_DUMP);
2111 /* After executing the pass, apply expected changes to the function
2112 properties. */
2114 static void
2115 update_properties_after_pass (function *fn, void *data)
2117 opt_pass *pass = (opt_pass *) data;
2118 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2119 & ~pass->properties_destroyed;
2122 /* Execute summary generation for all of the passes in IPA_PASS. */
2124 void
2125 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2127 while (ipa_pass)
2129 opt_pass *pass = ipa_pass;
2131 /* Execute all of the IPA_PASSes in the list. */
2132 if (ipa_pass->type == IPA_PASS
2133 && pass->gate (cfun)
2134 && ipa_pass->generate_summary)
2136 pass_init_dump_file (pass);
2138 /* If a timevar is present, start it. */
2139 if (pass->tv_id)
2140 timevar_push (pass->tv_id);
2142 current_pass = pass;
2143 ipa_pass->generate_summary ();
2145 /* Stop timevar. */
2146 if (pass->tv_id)
2147 timevar_pop (pass->tv_id);
2149 pass_fini_dump_file (pass);
2151 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2155 /* Execute IPA_PASS function transform on NODE. */
2157 static void
2158 execute_one_ipa_transform_pass (struct cgraph_node *node,
2159 ipa_opt_pass_d *ipa_pass)
2161 opt_pass *pass = ipa_pass;
2162 unsigned int todo_after = 0;
2164 current_pass = pass;
2165 if (!ipa_pass->function_transform)
2166 return;
2168 /* Note that the folders should only create gimple expressions.
2169 This is a hack until the new folder is ready. */
2170 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2172 pass_init_dump_file (pass);
2174 /* Run pre-pass verification. */
2175 execute_todo (ipa_pass->function_transform_todo_flags_start);
2177 /* If a timevar is present, start it. */
2178 if (pass->tv_id != TV_NONE)
2179 timevar_push (pass->tv_id);
2181 /* Do it! */
2182 todo_after = ipa_pass->function_transform (node);
2184 /* Stop timevar. */
2185 if (pass->tv_id != TV_NONE)
2186 timevar_pop (pass->tv_id);
2188 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2189 check_profile_consistency (pass->static_pass_number, 0, true);
2191 /* Run post-pass cleanup and verification. */
2192 execute_todo (todo_after);
2193 verify_interpass_invariants ();
2194 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2195 check_profile_consistency (pass->static_pass_number, 1, true);
2197 if (dump_file)
2198 do_per_function (execute_function_dump, NULL);
2199 pass_fini_dump_file (pass);
2201 current_pass = NULL;
2203 /* Signal this is a suitable GC collection point. */
2204 if (!(todo_after & TODO_do_not_ggc_collect))
2205 ggc_collect ();
2208 /* For the current function, execute all ipa transforms. */
2210 void
2211 execute_all_ipa_transforms (void)
2213 struct cgraph_node *node;
2214 if (!cfun)
2215 return;
2216 node = cgraph_node::get (current_function_decl);
2218 if (node->ipa_transforms_to_apply.exists ())
2220 unsigned int i;
2222 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2223 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2224 node->ipa_transforms_to_apply.release ();
2228 /* Check if PASS is explicitly disabled or enabled and return
2229 the gate status. FUNC is the function to be processed, and
2230 GATE_STATUS is the gate status determined by pass manager by
2231 default. */
2233 static bool
2234 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2236 bool explicitly_enabled = false;
2237 bool explicitly_disabled = false;
2239 explicitly_enabled
2240 = is_pass_explicitly_enabled_or_disabled (pass, func,
2241 enabled_pass_uid_range_tab);
2242 explicitly_disabled
2243 = is_pass_explicitly_enabled_or_disabled (pass, func,
2244 disabled_pass_uid_range_tab);
2246 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2248 return gate_status;
2252 /* Execute PASS. */
2254 bool
2255 execute_one_pass (opt_pass *pass)
2257 unsigned int todo_after = 0;
2259 bool gate_status;
2261 /* IPA passes are executed on whole program, so cfun should be NULL.
2262 Other passes need function context set. */
2263 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2264 gcc_assert (!cfun && !current_function_decl);
2265 else
2266 gcc_assert (cfun && current_function_decl);
2268 current_pass = pass;
2270 /* Check whether gate check should be avoided.
2271 User controls the value of the gate through the parameter "gate_status". */
2272 gate_status = pass->gate (cfun);
2273 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2275 /* Override gate with plugin. */
2276 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2278 if (!gate_status)
2280 /* Run so passes selectively disabling themselves on a given function
2281 are not miscounted. */
2282 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2284 check_profile_consistency (pass->static_pass_number, 0, false);
2285 check_profile_consistency (pass->static_pass_number, 1, false);
2287 current_pass = NULL;
2288 return false;
2291 /* Pass execution event trigger: useful to identify passes being
2292 executed. */
2293 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2295 if (!quiet_flag && !cfun)
2296 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2298 /* Note that the folders should only create gimple expressions.
2299 This is a hack until the new folder is ready. */
2300 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2302 pass_init_dump_file (pass);
2304 /* Run pre-pass verification. */
2305 execute_todo (pass->todo_flags_start);
2307 #ifdef ENABLE_CHECKING
2308 do_per_function (verify_curr_properties,
2309 (void *)(size_t)pass->properties_required);
2310 #endif
2312 /* If a timevar is present, start it. */
2313 if (pass->tv_id != TV_NONE)
2314 timevar_push (pass->tv_id);
2316 /* Do it! */
2317 todo_after = pass->execute (cfun);
2318 do_per_function (clear_last_verified, NULL);
2320 /* Stop timevar. */
2321 if (pass->tv_id != TV_NONE)
2322 timevar_pop (pass->tv_id);
2324 do_per_function (update_properties_after_pass, pass);
2326 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2327 check_profile_consistency (pass->static_pass_number, 0, true);
2329 /* Run post-pass cleanup and verification. */
2330 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2331 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2332 check_profile_consistency (pass->static_pass_number, 1, true);
2334 verify_interpass_invariants ();
2335 if (dump_file)
2336 do_per_function (execute_function_dump, pass);
2337 if (pass->type == IPA_PASS)
2339 struct cgraph_node *node;
2340 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2341 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2344 if (!current_function_decl)
2345 symtab->process_new_functions ();
2347 pass_fini_dump_file (pass);
2349 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2350 gcc_assert (!(cfun->curr_properties & PROP_trees)
2351 || pass->type != RTL_PASS);
2353 current_pass = NULL;
2355 /* Signal this is a suitable GC collection point. */
2356 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2357 ggc_collect ();
2359 return true;
2362 static void
2363 execute_pass_list_1 (opt_pass *pass)
2367 gcc_assert (pass->type == GIMPLE_PASS
2368 || pass->type == RTL_PASS);
2369 if (execute_one_pass (pass) && pass->sub)
2370 execute_pass_list_1 (pass->sub);
2371 pass = pass->next;
2373 while (pass);
2376 void
2377 execute_pass_list (function *fn, opt_pass *pass)
2379 push_cfun (fn);
2380 execute_pass_list_1 (pass);
2381 if (fn->cfg)
2383 free_dominance_info (CDI_DOMINATORS);
2384 free_dominance_info (CDI_POST_DOMINATORS);
2386 pop_cfun ();
2389 /* Write out all LTO data. */
2390 static void
2391 write_lto (void)
2393 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2394 lto_output ();
2395 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2396 timevar_push (TV_IPA_LTO_DECL_OUT);
2397 produce_asm_for_decls ();
2398 timevar_pop (TV_IPA_LTO_DECL_OUT);
2401 /* Same as execute_pass_list but assume that subpasses of IPA passes
2402 are local passes. If SET is not NULL, write out summaries of only
2403 those node in SET. */
2405 static void
2406 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2408 while (pass)
2410 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2411 gcc_assert (!current_function_decl);
2412 gcc_assert (!cfun);
2413 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2414 if (pass->type == IPA_PASS
2415 && ipa_pass->write_summary
2416 && pass->gate (cfun))
2418 /* If a timevar is present, start it. */
2419 if (pass->tv_id)
2420 timevar_push (pass->tv_id);
2422 pass_init_dump_file (pass);
2424 current_pass = pass;
2425 ipa_pass->write_summary ();
2427 pass_fini_dump_file (pass);
2429 /* If a timevar is present, start it. */
2430 if (pass->tv_id)
2431 timevar_pop (pass->tv_id);
2434 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2435 ipa_write_summaries_2 (pass->sub, state);
2437 pass = pass->next;
2441 /* Helper function of ipa_write_summaries. Creates and destroys the
2442 decl state and calls ipa_write_summaries_2 for all passes that have
2443 summaries. SET is the set of nodes to be written. */
2445 static void
2446 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2448 pass_manager *passes = g->get_passes ();
2449 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2450 state->symtab_node_encoder = encoder;
2452 lto_push_out_decl_state (state);
2454 gcc_assert (!flag_wpa);
2455 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2457 write_lto ();
2459 gcc_assert (lto_get_out_decl_state () == state);
2460 lto_pop_out_decl_state ();
2461 lto_delete_out_decl_state (state);
2464 /* Write out summaries for all the nodes in the callgraph. */
2466 void
2467 ipa_write_summaries (bool offload_lto_mode)
2469 lto_symtab_encoder_t encoder;
2470 int i, order_pos;
2471 varpool_node *vnode;
2472 struct cgraph_node *node;
2473 struct cgraph_node **order;
2475 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2476 return;
2478 select_what_to_stream (offload_lto_mode);
2480 encoder = lto_symtab_encoder_new (false);
2482 /* Create the callgraph set in the same order used in
2483 cgraph_expand_all_functions. This mostly facilitates debugging,
2484 since it causes the gimple file to be processed in the same order
2485 as the source code. */
2486 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2487 order_pos = ipa_reverse_postorder (order);
2488 gcc_assert (order_pos == symtab->cgraph_count);
2490 for (i = order_pos - 1; i >= 0; i--)
2492 struct cgraph_node *node = order[i];
2494 if (node->has_gimple_body_p ())
2496 /* When streaming out references to statements as part of some IPA
2497 pass summary, the statements need to have uids assigned and the
2498 following does that for all the IPA passes here. Naturally, this
2499 ordering then matches the one IPA-passes get in their stmt_fixup
2500 hooks. */
2502 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2503 renumber_gimple_stmt_uids ();
2504 pop_cfun ();
2506 if (node->definition && node->need_lto_streaming)
2507 lto_set_symtab_encoder_in_partition (encoder, node);
2510 FOR_EACH_DEFINED_FUNCTION (node)
2511 if (node->alias && node->need_lto_streaming)
2512 lto_set_symtab_encoder_in_partition (encoder, node);
2513 FOR_EACH_DEFINED_VARIABLE (vnode)
2514 if (vnode->need_lto_streaming)
2515 lto_set_symtab_encoder_in_partition (encoder, vnode);
2517 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2519 free (order);
2522 /* Same as execute_pass_list but assume that subpasses of IPA passes
2523 are local passes. If SET is not NULL, write out optimization summaries of
2524 only those node in SET. */
2526 static void
2527 ipa_write_optimization_summaries_1 (opt_pass *pass,
2528 struct lto_out_decl_state *state)
2530 while (pass)
2532 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2533 gcc_assert (!current_function_decl);
2534 gcc_assert (!cfun);
2535 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2536 if (pass->type == IPA_PASS
2537 && ipa_pass->write_optimization_summary
2538 && pass->gate (cfun))
2540 /* If a timevar is present, start it. */
2541 if (pass->tv_id)
2542 timevar_push (pass->tv_id);
2544 pass_init_dump_file (pass);
2546 current_pass = pass;
2547 ipa_pass->write_optimization_summary ();
2549 pass_fini_dump_file (pass);
2551 /* If a timevar is present, start it. */
2552 if (pass->tv_id)
2553 timevar_pop (pass->tv_id);
2556 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2557 ipa_write_optimization_summaries_1 (pass->sub, state);
2559 pass = pass->next;
2563 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2564 NULL, write out all summaries of all nodes. */
2566 void
2567 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2569 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2570 lto_symtab_encoder_iterator lsei;
2571 state->symtab_node_encoder = encoder;
2573 lto_push_out_decl_state (state);
2574 for (lsei = lsei_start_function_in_partition (encoder);
2575 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2577 struct cgraph_node *node = lsei_cgraph_node (lsei);
2578 /* When streaming out references to statements as part of some IPA
2579 pass summary, the statements need to have uids assigned.
2581 For functions newly born at WPA stage we need to initialize
2582 the uids here. */
2583 if (node->definition
2584 && gimple_has_body_p (node->decl))
2586 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2587 renumber_gimple_stmt_uids ();
2588 pop_cfun ();
2592 gcc_assert (flag_wpa);
2593 pass_manager *passes = g->get_passes ();
2594 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2596 write_lto ();
2598 gcc_assert (lto_get_out_decl_state () == state);
2599 lto_pop_out_decl_state ();
2600 lto_delete_out_decl_state (state);
2603 /* Same as execute_pass_list but assume that subpasses of IPA passes
2604 are local passes. */
2606 static void
2607 ipa_read_summaries_1 (opt_pass *pass)
2609 while (pass)
2611 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2613 gcc_assert (!current_function_decl);
2614 gcc_assert (!cfun);
2615 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2617 if (pass->gate (cfun))
2619 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2621 /* If a timevar is present, start it. */
2622 if (pass->tv_id)
2623 timevar_push (pass->tv_id);
2625 pass_init_dump_file (pass);
2627 current_pass = pass;
2628 ipa_pass->read_summary ();
2630 pass_fini_dump_file (pass);
2632 /* Stop timevar. */
2633 if (pass->tv_id)
2634 timevar_pop (pass->tv_id);
2637 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2638 ipa_read_summaries_1 (pass->sub);
2640 pass = pass->next;
2645 /* Read all the summaries for all_regular_ipa_passes. */
2647 void
2648 ipa_read_summaries (void)
2650 pass_manager *passes = g->get_passes ();
2651 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2654 /* Same as execute_pass_list but assume that subpasses of IPA passes
2655 are local passes. */
2657 static void
2658 ipa_read_optimization_summaries_1 (opt_pass *pass)
2660 while (pass)
2662 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2664 gcc_assert (!current_function_decl);
2665 gcc_assert (!cfun);
2666 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2668 if (pass->gate (cfun))
2670 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2672 /* If a timevar is present, start it. */
2673 if (pass->tv_id)
2674 timevar_push (pass->tv_id);
2676 pass_init_dump_file (pass);
2678 current_pass = pass;
2679 ipa_pass->read_optimization_summary ();
2681 pass_fini_dump_file (pass);
2683 /* Stop timevar. */
2684 if (pass->tv_id)
2685 timevar_pop (pass->tv_id);
2688 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2689 ipa_read_optimization_summaries_1 (pass->sub);
2691 pass = pass->next;
2695 /* Read all the summaries for all_regular_ipa_passes. */
2697 void
2698 ipa_read_optimization_summaries (void)
2700 pass_manager *passes = g->get_passes ();
2701 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2704 /* Same as execute_pass_list but assume that subpasses of IPA passes
2705 are local passes. */
2706 void
2707 execute_ipa_pass_list (opt_pass *pass)
2711 gcc_assert (!current_function_decl);
2712 gcc_assert (!cfun);
2713 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2714 if (execute_one_pass (pass) && pass->sub)
2716 if (pass->sub->type == GIMPLE_PASS)
2718 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2719 do_per_function_toporder ((void (*)(function *, void *))
2720 execute_pass_list,
2721 pass->sub);
2722 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2724 else if (pass->sub->type == SIMPLE_IPA_PASS
2725 || pass->sub->type == IPA_PASS)
2726 execute_ipa_pass_list (pass->sub);
2727 else
2728 gcc_unreachable ();
2730 gcc_assert (!current_function_decl);
2731 symtab->process_new_functions ();
2732 pass = pass->next;
2734 while (pass);
2737 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2739 static void
2740 execute_ipa_stmt_fixups (opt_pass *pass,
2741 struct cgraph_node *node, gimple *stmts)
2743 while (pass)
2745 /* Execute all of the IPA_PASSes in the list. */
2746 if (pass->type == IPA_PASS
2747 && pass->gate (cfun))
2749 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2751 if (ipa_pass->stmt_fixup)
2753 pass_init_dump_file (pass);
2754 /* If a timevar is present, start it. */
2755 if (pass->tv_id)
2756 timevar_push (pass->tv_id);
2758 current_pass = pass;
2759 ipa_pass->stmt_fixup (node, stmts);
2761 /* Stop timevar. */
2762 if (pass->tv_id)
2763 timevar_pop (pass->tv_id);
2764 pass_fini_dump_file (pass);
2766 if (pass->sub)
2767 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2769 pass = pass->next;
2773 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2775 void
2776 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2778 pass_manager *passes = g->get_passes ();
2779 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2783 extern void debug_properties (unsigned int);
2784 extern void dump_properties (FILE *, unsigned int);
2786 DEBUG_FUNCTION void
2787 dump_properties (FILE *dump, unsigned int props)
2789 fprintf (dump, "Properties:\n");
2790 if (props & PROP_gimple_any)
2791 fprintf (dump, "PROP_gimple_any\n");
2792 if (props & PROP_gimple_lcf)
2793 fprintf (dump, "PROP_gimple_lcf\n");
2794 if (props & PROP_gimple_leh)
2795 fprintf (dump, "PROP_gimple_leh\n");
2796 if (props & PROP_cfg)
2797 fprintf (dump, "PROP_cfg\n");
2798 if (props & PROP_ssa)
2799 fprintf (dump, "PROP_ssa\n");
2800 if (props & PROP_no_crit_edges)
2801 fprintf (dump, "PROP_no_crit_edges\n");
2802 if (props & PROP_rtl)
2803 fprintf (dump, "PROP_rtl\n");
2804 if (props & PROP_gimple_lomp)
2805 fprintf (dump, "PROP_gimple_lomp\n");
2806 if (props & PROP_gimple_lcx)
2807 fprintf (dump, "PROP_gimple_lcx\n");
2808 if (props & PROP_gimple_lvec)
2809 fprintf (dump, "PROP_gimple_lvec\n");
2810 if (props & PROP_cfglayout)
2811 fprintf (dump, "PROP_cfglayout\n");
2814 DEBUG_FUNCTION void
2815 debug_properties (unsigned int props)
2817 dump_properties (stderr, props);
2820 /* Called by local passes to see if function is called by already processed nodes.
2821 Because we process nodes in topological order, this means that function is
2822 in recursive cycle or we introduced new direct calls. */
2823 bool
2824 function_called_by_processed_nodes_p (void)
2826 struct cgraph_edge *e;
2827 for (e = cgraph_node::get (current_function_decl)->callers;
2829 e = e->next_caller)
2831 if (e->caller->decl == current_function_decl)
2832 continue;
2833 if (!e->caller->has_gimple_body_p ())
2834 continue;
2835 if (TREE_ASM_WRITTEN (e->caller->decl))
2836 continue;
2837 if (!e->caller->process && !e->caller->global.inlined_to)
2838 break;
2840 if (dump_file && e)
2842 fprintf (dump_file, "Already processed call to:\n");
2843 e->caller->dump (dump_file);
2845 return e != NULL;
2848 #include "gt-passes.h"