2015-01-20 Jeff Law <law@redhat.com>
[official-gcc.git] / gcc / passes.c
blob411e3bb4d6a0413ad77de139eab9c472072bcb0b
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 "alias.h"
36 #include "symtab.h"
37 #include "wide-int.h"
38 #include "inchash.h"
39 #include "tree.h"
40 #include "fold-const.h"
41 #include "varasm.h"
42 #include "rtl.h"
43 #include "tm_p.h"
44 #include "flags.h"
45 #include "insn-attr.h"
46 #include "insn-config.h"
47 #include "insn-flags.h"
48 #include "hard-reg-set.h"
49 #include "recog.h"
50 #include "output.h"
51 #include "except.h"
52 #include "function.h"
53 #include "toplev.h"
54 #include "hashtab.h"
55 #include "statistics.h"
56 #include "real.h"
57 #include "fixed-value.h"
58 #include "expmed.h"
59 #include "dojump.h"
60 #include "explow.h"
61 #include "calls.h"
62 #include "emit-rtl.h"
63 #include "stmt.h"
64 #include "expr.h"
65 #include "predict.h"
66 #include "basic-block.h"
67 #include "intl.h"
68 #include "graph.h"
69 #include "regs.h"
70 #include "diagnostic-core.h"
71 #include "params.h"
72 #include "reload.h"
73 #include "debug.h"
74 #include "target.h"
75 #include "langhooks.h"
76 #include "cfgloop.h"
77 #include "hosthooks.h"
78 #include "opts.h"
79 #include "coverage.h"
80 #include "value-prof.h"
81 #include "tree-inline.h"
82 #include "tree-ssa-alias.h"
83 #include "internal-fn.h"
84 #include "gimple-expr.h"
85 #include "is-a.h"
86 #include "gimple.h"
87 #include "gimple-ssa.h"
88 #include "tree-cfg.h"
89 #include "stringpool.h"
90 #include "tree-ssanames.h"
91 #include "tree-ssa-loop-manip.h"
92 #include "tree-into-ssa.h"
93 #include "tree-dfa.h"
94 #include "tree-ssa.h"
95 #include "tree-pass.h"
96 #include "tree-dump.h"
97 #include "df.h"
98 #include "hash-map.h"
99 #include "plugin-api.h"
100 #include "ipa-ref.h"
101 #include "cgraph.h"
102 #include "lto-streamer.h"
103 #include "plugin.h"
104 #include "ipa-utils.h"
105 #include "tree-pretty-print.h" /* for dump_function_header */
106 #include "context.h"
107 #include "pass_manager.h"
108 #include "tree-ssa-live.h" /* For remove_unused_locals. */
109 #include "tree-cfgcleanup.h"
111 using namespace gcc;
113 /* This is used for debugging. It allows the current pass to printed
114 from anywhere in compilation.
115 The variable current_pass is also used for statistics and plugins. */
116 opt_pass *current_pass;
118 static void register_pass_name (opt_pass *, const char *);
120 /* Most passes are single-instance (within their context) and thus don't
121 need to implement cloning, but passes that support multiple instances
122 *must* provide their own implementation of the clone method.
124 Handle this by providing a default implemenation, but make it a fatal
125 error to call it. */
127 opt_pass *
128 opt_pass::clone ()
130 internal_error ("pass %s does not support cloning", name);
133 bool
134 opt_pass::gate (function *)
136 return true;
139 unsigned int
140 opt_pass::execute (function *)
142 return 0;
145 opt_pass::opt_pass (const pass_data &data, context *ctxt)
146 : pass_data (data),
147 sub (NULL),
148 next (NULL),
149 static_pass_number (0),
150 m_ctxt (ctxt)
155 void
156 pass_manager::execute_early_local_passes ()
158 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
159 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
160 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
163 unsigned int
164 pass_manager::execute_pass_mode_switching ()
166 return pass_mode_switching_1->execute (cfun);
170 /* Call from anywhere to find out what pass this is. Useful for
171 printing out debugging information deep inside an service
172 routine. */
173 void
174 print_current_pass (FILE *file)
176 if (current_pass)
177 fprintf (file, "current pass = %s (%d)\n",
178 current_pass->name, current_pass->static_pass_number);
179 else
180 fprintf (file, "no current pass.\n");
184 /* Call from the debugger to get the current pass name. */
185 DEBUG_FUNCTION void
186 debug_pass (void)
188 print_current_pass (stderr);
193 /* Global variables used to communicate with passes. */
194 bool in_gimple_form;
195 bool first_pass_instance;
198 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
199 and TYPE_DECL nodes.
201 This does nothing for local (non-static) variables, unless the
202 variable is a register variable with DECL_ASSEMBLER_NAME set. In
203 that case, or if the variable is not an automatic, it sets up the
204 RTL and outputs any assembler code (label definition, storage
205 allocation and initialization).
207 DECL is the declaration. TOP_LEVEL is nonzero
208 if this declaration is not within a function. */
210 void
211 rest_of_decl_compilation (tree decl,
212 int top_level,
213 int at_end)
215 bool finalize = true;
217 /* We deferred calling assemble_alias so that we could collect
218 other attributes such as visibility. Emit the alias now. */
219 if (!in_lto_p)
221 tree alias;
222 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
223 if (alias)
225 alias = TREE_VALUE (TREE_VALUE (alias));
226 alias = get_identifier (TREE_STRING_POINTER (alias));
227 /* A quirk of the initial implementation of aliases required that the
228 user add "extern" to all of them. Which is silly, but now
229 historical. Do note that the symbol is in fact locally defined. */
230 DECL_EXTERNAL (decl) = 0;
231 TREE_STATIC (decl) = 1;
232 assemble_alias (decl, alias);
233 finalize = false;
237 /* Can't defer this, because it needs to happen before any
238 later function definitions are processed. */
239 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
240 make_decl_rtl (decl);
242 /* Forward declarations for nested functions are not "external",
243 but we need to treat them as if they were. */
244 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
245 || TREE_CODE (decl) == FUNCTION_DECL)
247 timevar_push (TV_VARCONST);
249 /* Don't output anything when a tentative file-scope definition
250 is seen. But at end of compilation, do output code for them.
252 We do output all variables and rely on
253 callgraph code to defer them except for forward declarations
254 (see gcc.c-torture/compile/920624-1.c) */
255 if ((at_end
256 || !DECL_DEFER_OUTPUT (decl)
257 || DECL_INITIAL (decl))
258 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
259 && !DECL_EXTERNAL (decl))
261 /* When reading LTO unit, we also read varpool, so do not
262 rebuild it. */
263 if (in_lto_p && !at_end)
265 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
266 varpool_node::finalize_decl (decl);
269 #ifdef ASM_FINISH_DECLARE_OBJECT
270 if (decl == last_assemble_variable_decl)
272 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
273 top_level, at_end);
275 #endif
277 timevar_pop (TV_VARCONST);
279 else if (TREE_CODE (decl) == TYPE_DECL
280 /* Like in rest_of_type_compilation, avoid confusing the debug
281 information machinery when there are errors. */
282 && !seen_error ())
284 timevar_push (TV_SYMOUT);
285 debug_hooks->type_decl (decl, !top_level);
286 timevar_pop (TV_SYMOUT);
289 /* Let cgraph know about the existence of variables. */
290 if (in_lto_p && !at_end)
292 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
293 && TREE_STATIC (decl))
294 varpool_node::get_create (decl);
297 /* Called after finishing a record, union or enumeral type. */
299 void
300 rest_of_type_compilation (tree type, int toplev)
302 /* Avoid confusing the debug information machinery when there are
303 errors. */
304 if (seen_error ())
305 return;
307 timevar_push (TV_SYMOUT);
308 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
309 timevar_pop (TV_SYMOUT);
314 void
315 pass_manager::
316 finish_optimization_passes (void)
318 int i;
319 struct dump_file_info *dfi;
320 char *name;
321 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
323 timevar_push (TV_DUMP);
324 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
326 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
327 end_branch_prob ();
328 dumps->dump_finish (pass_profile_1->static_pass_number);
331 if (optimize > 0)
333 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
334 print_combine_total_stats ();
335 dumps->dump_finish (pass_profile_1->static_pass_number);
338 /* Do whatever is necessary to finish printing the graphs. */
339 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
340 if (dumps->dump_initialized_p (i)
341 && (dfi->pflags & TDF_GRAPH) != 0
342 && (name = dumps->get_dump_file_name (i)) != NULL)
344 finish_graph_dump_file (name);
345 free (name);
348 timevar_pop (TV_DUMP);
351 static unsigned int
352 execute_build_ssa_passes (void)
354 /* Once this pass (and its sub-passes) are complete, all functions
355 will be in SSA form. Technically this state change is happening
356 a tad early, since the sub-passes have not yet run, but since
357 none of the sub-passes are IPA passes and do not create new
358 functions, this is ok. We're setting this value for the benefit
359 of IPA passes that follow. */
360 if (symtab->state < IPA_SSA)
361 symtab->state = IPA_SSA;
362 return 0;
365 namespace {
367 const pass_data pass_data_build_ssa_passes =
369 SIMPLE_IPA_PASS, /* type */
370 "build_ssa_passes", /* name */
371 OPTGROUP_NONE, /* optinfo_flags */
372 TV_EARLY_LOCAL, /* tv_id */
373 0, /* properties_required */
374 0, /* properties_provided */
375 0, /* properties_destroyed */
376 0, /* todo_flags_start */
377 /* todo_flags_finish is executed before subpases. For this reason
378 it makes no sense to remove unreachable functions here. */
379 0, /* todo_flags_finish */
382 class pass_build_ssa_passes : public simple_ipa_opt_pass
384 public:
385 pass_build_ssa_passes (gcc::context *ctxt)
386 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
389 /* opt_pass methods: */
390 virtual bool gate (function *)
392 /* Don't bother doing anything if the program has errors. */
393 return (!seen_error () && !in_lto_p);
396 virtual unsigned int execute (function *)
398 return execute_build_ssa_passes ();
401 }; // class pass_build_ssa_passes
403 const pass_data pass_data_chkp_instrumentation_passes =
405 SIMPLE_IPA_PASS, /* type */
406 "chkp_passes", /* name */
407 OPTGROUP_NONE, /* optinfo_flags */
408 TV_NONE, /* tv_id */
409 0, /* properties_required */
410 0, /* properties_provided */
411 0, /* properties_destroyed */
412 0, /* todo_flags_start */
413 0, /* todo_flags_finish */
416 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
418 public:
419 pass_chkp_instrumentation_passes (gcc::context *ctxt)
420 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
423 /* opt_pass methods: */
424 virtual bool gate (function *)
426 /* Don't bother doing anything if the program has errors. */
427 return (!seen_error () && !in_lto_p);
430 }; // class pass_chkp_instrumentation_passes
432 const pass_data pass_data_local_optimization_passes =
434 SIMPLE_IPA_PASS, /* type */
435 "opt_local_passes", /* name */
436 OPTGROUP_NONE, /* optinfo_flags */
437 TV_NONE, /* tv_id */
438 0, /* properties_required */
439 0, /* properties_provided */
440 0, /* properties_destroyed */
441 0, /* todo_flags_start */
442 0, /* todo_flags_finish */
445 class pass_local_optimization_passes : public simple_ipa_opt_pass
447 public:
448 pass_local_optimization_passes (gcc::context *ctxt)
449 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
452 /* opt_pass methods: */
453 virtual bool gate (function *)
455 /* Don't bother doing anything if the program has errors. */
456 return (!seen_error () && !in_lto_p);
459 }; // class pass_local_optimization_passes
461 } // anon namespace
463 simple_ipa_opt_pass *
464 make_pass_build_ssa_passes (gcc::context *ctxt)
466 return new pass_build_ssa_passes (ctxt);
469 simple_ipa_opt_pass *
470 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
472 return new pass_chkp_instrumentation_passes (ctxt);
475 simple_ipa_opt_pass *
476 make_pass_local_optimization_passes (gcc::context *ctxt)
478 return new pass_local_optimization_passes (ctxt);
481 namespace {
483 const pass_data pass_data_all_early_optimizations =
485 GIMPLE_PASS, /* type */
486 "early_optimizations", /* name */
487 OPTGROUP_NONE, /* optinfo_flags */
488 TV_NONE, /* tv_id */
489 0, /* properties_required */
490 0, /* properties_provided */
491 0, /* properties_destroyed */
492 0, /* todo_flags_start */
493 0, /* todo_flags_finish */
496 class pass_all_early_optimizations : public gimple_opt_pass
498 public:
499 pass_all_early_optimizations (gcc::context *ctxt)
500 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
503 /* opt_pass methods: */
504 virtual bool gate (function *)
506 return (optimize >= 1
507 /* Don't bother doing anything if the program has errors. */
508 && !seen_error ());
511 }; // class pass_all_early_optimizations
513 } // anon namespace
515 static gimple_opt_pass *
516 make_pass_all_early_optimizations (gcc::context *ctxt)
518 return new pass_all_early_optimizations (ctxt);
521 namespace {
523 const pass_data pass_data_all_optimizations =
525 GIMPLE_PASS, /* type */
526 "*all_optimizations", /* name */
527 OPTGROUP_NONE, /* optinfo_flags */
528 TV_OPTIMIZE, /* tv_id */
529 0, /* properties_required */
530 0, /* properties_provided */
531 0, /* properties_destroyed */
532 0, /* todo_flags_start */
533 0, /* todo_flags_finish */
536 class pass_all_optimizations : public gimple_opt_pass
538 public:
539 pass_all_optimizations (gcc::context *ctxt)
540 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
543 /* opt_pass methods: */
544 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
546 }; // class pass_all_optimizations
548 } // anon namespace
550 static gimple_opt_pass *
551 make_pass_all_optimizations (gcc::context *ctxt)
553 return new pass_all_optimizations (ctxt);
556 namespace {
558 const pass_data pass_data_all_optimizations_g =
560 GIMPLE_PASS, /* type */
561 "*all_optimizations_g", /* name */
562 OPTGROUP_NONE, /* optinfo_flags */
563 TV_OPTIMIZE, /* tv_id */
564 0, /* properties_required */
565 0, /* properties_provided */
566 0, /* properties_destroyed */
567 0, /* todo_flags_start */
568 0, /* todo_flags_finish */
571 class pass_all_optimizations_g : public gimple_opt_pass
573 public:
574 pass_all_optimizations_g (gcc::context *ctxt)
575 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
578 /* opt_pass methods: */
579 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
581 }; // class pass_all_optimizations_g
583 } // anon namespace
585 static gimple_opt_pass *
586 make_pass_all_optimizations_g (gcc::context *ctxt)
588 return new pass_all_optimizations_g (ctxt);
591 namespace {
593 const pass_data pass_data_rest_of_compilation =
595 RTL_PASS, /* type */
596 "*rest_of_compilation", /* name */
597 OPTGROUP_NONE, /* optinfo_flags */
598 TV_REST_OF_COMPILATION, /* tv_id */
599 PROP_rtl, /* properties_required */
600 0, /* properties_provided */
601 0, /* properties_destroyed */
602 0, /* todo_flags_start */
603 0, /* todo_flags_finish */
606 class pass_rest_of_compilation : public rtl_opt_pass
608 public:
609 pass_rest_of_compilation (gcc::context *ctxt)
610 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
613 /* opt_pass methods: */
614 virtual bool gate (function *)
616 /* Early return if there were errors. We can run afoul of our
617 consistency checks, and there's not really much point in fixing them. */
618 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
621 }; // class pass_rest_of_compilation
623 } // anon namespace
625 static rtl_opt_pass *
626 make_pass_rest_of_compilation (gcc::context *ctxt)
628 return new pass_rest_of_compilation (ctxt);
631 namespace {
633 const pass_data pass_data_postreload =
635 RTL_PASS, /* type */
636 "*all-postreload", /* name */
637 OPTGROUP_NONE, /* optinfo_flags */
638 TV_POSTRELOAD, /* tv_id */
639 PROP_rtl, /* properties_required */
640 0, /* properties_provided */
641 0, /* properties_destroyed */
642 0, /* todo_flags_start */
643 0, /* todo_flags_finish */
646 class pass_postreload : public rtl_opt_pass
648 public:
649 pass_postreload (gcc::context *ctxt)
650 : rtl_opt_pass (pass_data_postreload, ctxt)
653 /* opt_pass methods: */
654 virtual bool gate (function *) { return reload_completed; }
656 }; // class pass_postreload
658 } // anon namespace
660 static rtl_opt_pass *
661 make_pass_postreload (gcc::context *ctxt)
663 return new pass_postreload (ctxt);
666 namespace {
668 const pass_data pass_data_late_compilation =
670 RTL_PASS, /* type */
671 "*all-late_compilation", /* name */
672 OPTGROUP_NONE, /* optinfo_flags */
673 TV_LATE_COMPILATION, /* tv_id */
674 PROP_rtl, /* properties_required */
675 0, /* properties_provided */
676 0, /* properties_destroyed */
677 0, /* todo_flags_start */
678 0, /* todo_flags_finish */
681 class pass_late_compilation : public rtl_opt_pass
683 public:
684 pass_late_compilation (gcc::context *ctxt)
685 : rtl_opt_pass (pass_data_late_compilation, ctxt)
688 /* opt_pass methods: */
689 virtual bool gate (function *)
691 return reload_completed || targetm.no_register_allocation;
694 }; // class pass_late_compilation
696 } // anon namespace
698 static rtl_opt_pass *
699 make_pass_late_compilation (gcc::context *ctxt)
701 return new pass_late_compilation (ctxt);
706 /* Set the static pass number of pass PASS to ID and record that
707 in the mapping from static pass number to pass. */
709 void
710 pass_manager::
711 set_pass_for_id (int id, opt_pass *pass)
713 pass->static_pass_number = id;
714 if (passes_by_id_size <= id)
716 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
717 memset (passes_by_id + passes_by_id_size, 0,
718 (id + 1 - passes_by_id_size) * sizeof (void *));
719 passes_by_id_size = id + 1;
721 passes_by_id[id] = pass;
724 /* Return the pass with the static pass number ID. */
726 opt_pass *
727 pass_manager::get_pass_for_id (int id) const
729 if (id >= passes_by_id_size)
730 return NULL;
731 return passes_by_id[id];
734 /* Iterate over the pass tree allocating dump file numbers. We want
735 to do this depth first, and independent of whether the pass is
736 enabled or not. */
738 void
739 register_one_dump_file (opt_pass *pass)
741 g->get_passes ()->register_one_dump_file (pass);
744 void
745 pass_manager::register_one_dump_file (opt_pass *pass)
747 char *dot_name, *flag_name, *glob_name;
748 const char *name, *full_name, *prefix;
749 char num[10];
750 int flags, id;
751 int optgroup_flags = OPTGROUP_NONE;
752 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
754 /* See below in next_pass_1. */
755 num[0] = '\0';
756 if (pass->static_pass_number != -1)
757 sprintf (num, "%d", ((int) pass->static_pass_number < 0
758 ? 1 : pass->static_pass_number));
760 /* The name is both used to identify the pass for the purposes of plugins,
761 and to specify dump file name and option.
762 The latter two might want something short which is not quite unique; for
763 that reason, we may have a disambiguating prefix, followed by a space
764 to mark the start of the following dump file name / option string. */
765 name = strchr (pass->name, ' ');
766 name = name ? name + 1 : pass->name;
767 dot_name = concat (".", name, num, NULL);
768 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
770 prefix = "ipa-";
771 flags = TDF_IPA;
772 optgroup_flags |= OPTGROUP_IPA;
774 else if (pass->type == GIMPLE_PASS)
776 prefix = "tree-";
777 flags = TDF_TREE;
779 else
781 prefix = "rtl-";
782 flags = TDF_RTL;
785 flag_name = concat (prefix, name, num, NULL);
786 glob_name = concat (prefix, name, NULL);
787 optgroup_flags |= pass->optinfo_flags;
788 /* For any passes that do not have an optgroup set, and which are not
789 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
790 any dump messages are emitted properly under -fopt-info(-optall). */
791 if (optgroup_flags == OPTGROUP_NONE)
792 optgroup_flags = OPTGROUP_OTHER;
793 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
794 optgroup_flags,
795 true);
796 set_pass_for_id (id, pass);
797 full_name = concat (prefix, pass->name, num, NULL);
798 register_pass_name (pass, full_name);
799 free (CONST_CAST (char *, full_name));
802 /* Register the dump files for the pass_manager starting at PASS. */
804 void
805 pass_manager::register_dump_files (opt_pass *pass)
809 if (pass->name && pass->name[0] != '*')
810 register_one_dump_file (pass);
812 if (pass->sub)
813 register_dump_files (pass->sub);
815 pass = pass->next;
817 while (pass);
820 /* Helper for pass_registry hash table. */
822 struct pass_registry_hasher : default_hashmap_traits
824 static inline hashval_t hash (const char *);
825 static inline bool equal_keys (const char *, const char *);
828 /* Pass registry hash function. */
830 inline hashval_t
831 pass_registry_hasher::hash (const char *name)
833 return htab_hash_string (name);
836 /* Hash equal function */
838 inline bool
839 pass_registry_hasher::equal_keys (const char *s1, const char *s2)
841 return !strcmp (s1, s2);
844 static hash_map<const char *, opt_pass *, pass_registry_hasher>
845 *name_to_pass_map;
847 /* Register PASS with NAME. */
849 static void
850 register_pass_name (opt_pass *pass, const char *name)
852 if (!name_to_pass_map)
853 name_to_pass_map
854 = new hash_map<const char *, opt_pass *, pass_registry_hasher> (256);
856 if (name_to_pass_map->get (name))
857 return; /* Ignore plugin passes. */
859 const char *unique_name = xstrdup (name);
860 name_to_pass_map->put (unique_name, pass);
863 /* Map from pass id to canonicalized pass name. */
865 typedef const char *char_ptr;
866 static vec<char_ptr> pass_tab = vNULL;
868 /* Callback function for traversing NAME_TO_PASS_MAP. */
870 bool
871 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
873 gcc_assert (pass->static_pass_number > 0);
874 gcc_assert (pass_tab.exists ());
876 pass_tab[pass->static_pass_number] = name;
878 return 1;
881 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
882 table for dumping purpose. */
884 static void
885 create_pass_tab (void)
887 if (!flag_dump_passes)
888 return;
890 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
891 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
894 static bool override_gate_status (opt_pass *, tree, bool);
896 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
897 is turned on or not. */
899 static void
900 dump_one_pass (opt_pass *pass, int pass_indent)
902 int indent = 3 * pass_indent;
903 const char *pn;
904 bool is_on, is_really_on;
906 is_on = pass->gate (cfun);
907 is_really_on = override_gate_status (pass, current_function_decl, is_on);
909 if (pass->static_pass_number <= 0)
910 pn = pass->name;
911 else
912 pn = pass_tab[pass->static_pass_number];
914 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
915 (15 - indent < 0 ? 0 : 15 - indent), " ",
916 is_on ? " ON" : " OFF",
917 ((!is_on) == (!is_really_on) ? ""
918 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
921 /* Dump pass list PASS with indentation INDENT. */
923 static void
924 dump_pass_list (opt_pass *pass, int indent)
928 dump_one_pass (pass, indent);
929 if (pass->sub)
930 dump_pass_list (pass->sub, indent + 1);
931 pass = pass->next;
933 while (pass);
936 /* Dump all optimization passes. */
938 void
939 dump_passes (void)
941 g->get_passes ()->dump_passes ();
944 void
945 pass_manager::dump_passes () const
947 struct cgraph_node *n, *node = NULL;
949 create_pass_tab ();
951 FOR_EACH_FUNCTION (n)
952 if (DECL_STRUCT_FUNCTION (n->decl))
954 node = n;
955 break;
958 if (!node)
959 return;
961 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
963 dump_pass_list (all_lowering_passes, 1);
964 dump_pass_list (all_small_ipa_passes, 1);
965 dump_pass_list (all_regular_ipa_passes, 1);
966 dump_pass_list (all_late_ipa_passes, 1);
967 dump_pass_list (all_passes, 1);
969 pop_cfun ();
973 /* Returns the pass with NAME. */
975 static opt_pass *
976 get_pass_by_name (const char *name)
978 opt_pass **p = name_to_pass_map->get (name);
979 if (p)
980 return *p;
982 return NULL;
986 /* Range [start, last]. */
988 struct uid_range
990 unsigned int start;
991 unsigned int last;
992 const char *assem_name;
993 struct uid_range *next;
996 typedef struct uid_range *uid_range_p;
999 static vec<uid_range_p>
1000 enabled_pass_uid_range_tab = vNULL;
1001 static vec<uid_range_p>
1002 disabled_pass_uid_range_tab = vNULL;
1005 /* Parse option string for -fdisable- and -fenable-
1006 The syntax of the options:
1008 -fenable-<pass_name>
1009 -fdisable-<pass_name>
1011 -fenable-<pass_name>=s1:e1,s2:e2,...
1012 -fdisable-<pass_name>=s1:e1,s2:e2,...
1015 static void
1016 enable_disable_pass (const char *arg, bool is_enable)
1018 opt_pass *pass;
1019 char *range_str, *phase_name;
1020 char *argstr = xstrdup (arg);
1021 vec<uid_range_p> *tab = 0;
1023 range_str = strchr (argstr,'=');
1024 if (range_str)
1026 *range_str = '\0';
1027 range_str++;
1030 phase_name = argstr;
1031 if (!*phase_name)
1033 if (is_enable)
1034 error ("unrecognized option -fenable");
1035 else
1036 error ("unrecognized option -fdisable");
1037 free (argstr);
1038 return;
1040 pass = get_pass_by_name (phase_name);
1041 if (!pass || pass->static_pass_number == -1)
1043 if (is_enable)
1044 error ("unknown pass %s specified in -fenable", phase_name);
1045 else
1046 error ("unknown pass %s specified in -fdisable", phase_name);
1047 free (argstr);
1048 return;
1051 if (is_enable)
1052 tab = &enabled_pass_uid_range_tab;
1053 else
1054 tab = &disabled_pass_uid_range_tab;
1056 if ((unsigned) pass->static_pass_number >= tab->length ())
1057 tab->safe_grow_cleared (pass->static_pass_number + 1);
1059 if (!range_str)
1061 uid_range_p slot;
1062 uid_range_p new_range = XCNEW (struct uid_range);
1064 new_range->start = 0;
1065 new_range->last = (unsigned)-1;
1067 slot = (*tab)[pass->static_pass_number];
1068 new_range->next = slot;
1069 (*tab)[pass->static_pass_number] = new_range;
1070 if (is_enable)
1071 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1072 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1073 else
1074 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1075 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1077 else
1079 char *next_range = NULL;
1080 char *one_range = range_str;
1081 char *end_val = NULL;
1085 uid_range_p slot;
1086 uid_range_p new_range;
1087 char *invalid = NULL;
1088 long start;
1089 char *func_name = NULL;
1091 next_range = strchr (one_range, ',');
1092 if (next_range)
1094 *next_range = '\0';
1095 next_range++;
1098 end_val = strchr (one_range, ':');
1099 if (end_val)
1101 *end_val = '\0';
1102 end_val++;
1104 start = strtol (one_range, &invalid, 10);
1105 if (*invalid || start < 0)
1107 if (end_val || (one_range[0] >= '0'
1108 && one_range[0] <= '9'))
1110 error ("Invalid range %s in option %s",
1111 one_range,
1112 is_enable ? "-fenable" : "-fdisable");
1113 free (argstr);
1114 return;
1116 func_name = one_range;
1118 if (!end_val)
1120 new_range = XCNEW (struct uid_range);
1121 if (!func_name)
1123 new_range->start = (unsigned) start;
1124 new_range->last = (unsigned) start;
1126 else
1128 new_range->start = (unsigned) -1;
1129 new_range->last = (unsigned) -1;
1130 new_range->assem_name = xstrdup (func_name);
1133 else
1135 long last = strtol (end_val, &invalid, 10);
1136 if (*invalid || last < start)
1138 error ("Invalid range %s in option %s",
1139 end_val,
1140 is_enable ? "-fenable" : "-fdisable");
1141 free (argstr);
1142 return;
1144 new_range = XCNEW (struct uid_range);
1145 new_range->start = (unsigned) start;
1146 new_range->last = (unsigned) last;
1149 slot = (*tab)[pass->static_pass_number];
1150 new_range->next = slot;
1151 (*tab)[pass->static_pass_number] = new_range;
1152 if (is_enable)
1154 if (new_range->assem_name)
1155 inform (UNKNOWN_LOCATION,
1156 "enable pass %s for function %s",
1157 phase_name, new_range->assem_name);
1158 else
1159 inform (UNKNOWN_LOCATION,
1160 "enable pass %s for functions in the range of [%u, %u]",
1161 phase_name, new_range->start, new_range->last);
1163 else
1165 if (new_range->assem_name)
1166 inform (UNKNOWN_LOCATION,
1167 "disable pass %s for function %s",
1168 phase_name, new_range->assem_name);
1169 else
1170 inform (UNKNOWN_LOCATION,
1171 "disable pass %s for functions in the range of [%u, %u]",
1172 phase_name, new_range->start, new_range->last);
1175 one_range = next_range;
1176 } while (next_range);
1179 free (argstr);
1182 /* Enable pass specified by ARG. */
1184 void
1185 enable_pass (const char *arg)
1187 enable_disable_pass (arg, true);
1190 /* Disable pass specified by ARG. */
1192 void
1193 disable_pass (const char *arg)
1195 enable_disable_pass (arg, false);
1198 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1200 static bool
1201 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1202 tree func,
1203 vec<uid_range_p> tab)
1205 uid_range_p slot, range;
1206 int cgraph_uid;
1207 const char *aname = NULL;
1209 if (!tab.exists ()
1210 || (unsigned) pass->static_pass_number >= tab.length ()
1211 || pass->static_pass_number == -1)
1212 return false;
1214 slot = tab[pass->static_pass_number];
1215 if (!slot)
1216 return false;
1218 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1219 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1220 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1222 range = slot;
1223 while (range)
1225 if ((unsigned) cgraph_uid >= range->start
1226 && (unsigned) cgraph_uid <= range->last)
1227 return true;
1228 if (range->assem_name && aname
1229 && !strcmp (range->assem_name, aname))
1230 return true;
1231 range = range->next;
1234 return false;
1238 /* Update static_pass_number for passes (and the flag
1239 TODO_mark_first_instance).
1241 Passes are constructed with static_pass_number preinitialized to 0
1243 This field is used in two different ways: initially as instance numbers
1244 of their kind, and then as ids within the entire pass manager.
1246 Within pass_manager::pass_manager:
1248 * In add_pass_instance(), as called by next_pass_1 in
1249 NEXT_PASS in init_optimization_passes
1251 * When the initial instance of a pass within a pass manager is seen,
1252 it is flagged, and its static_pass_number is set to -1
1254 * On subsequent times that it is seen, the static pass number
1255 is decremented each time, so that if there are e.g. 4 dups,
1256 they have static_pass_number -4, 2, 3, 4 respectively (note
1257 how the initial one is negative and gives the count); these
1258 can be thought of as instance numbers of the specific pass
1260 * Within the register_dump_files () traversal, set_pass_for_id()
1261 is called on each pass, using these instance numbers to create
1262 dumpfile switches, and then overwriting them with a pass id,
1263 which are global to the whole pass manager (based on
1264 (TDI_end + current value of extra_dump_files_in_use) ) */
1266 static void
1267 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1268 opt_pass *initial_pass)
1270 /* Are we dealing with the first pass of its kind, or a clone? */
1271 if (new_pass != initial_pass)
1273 /* We're dealing with a clone. */
1274 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1276 /* Indicate to register_dump_files that this pass has duplicates,
1277 and so it should rename the dump file. The first instance will
1278 be -1, and be number of duplicates = -static_pass_number - 1.
1279 Subsequent instances will be > 0 and just the duplicate number. */
1280 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1282 initial_pass->static_pass_number -= 1;
1283 new_pass->static_pass_number = -initial_pass->static_pass_number;
1286 else
1288 /* We're dealing with the first pass of its kind. */
1289 new_pass->todo_flags_start |= TODO_mark_first_instance;
1290 new_pass->static_pass_number = -1;
1292 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1296 /* Add a pass to the pass list. Duplicate the pass if it's already
1297 in the list. */
1299 static opt_pass **
1300 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1302 /* Every pass should have a name so that plugins can refer to them. */
1303 gcc_assert (pass->name != NULL);
1305 add_pass_instance (pass, false, initial_pass);
1306 *list = pass;
1308 return &(*list)->next;
1311 /* List node for an inserted pass instance. We need to keep track of all
1312 the newly-added pass instances (with 'added_pass_nodes' defined below)
1313 so that we can register their dump files after pass-positioning is finished.
1314 Registering dumping files needs to be post-processed or the
1315 static_pass_number of the opt_pass object would be modified and mess up
1316 the dump file names of future pass instances to be added. */
1318 struct pass_list_node
1320 opt_pass *pass;
1321 struct pass_list_node *next;
1324 static struct pass_list_node *added_pass_nodes = NULL;
1325 static struct pass_list_node *prev_added_pass_node;
1327 /* Insert the pass at the proper position. Return true if the pass
1328 is successfully added.
1330 NEW_PASS_INFO - new pass to be inserted
1331 PASS_LIST - root of the pass list to insert the new pass to */
1333 static bool
1334 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1336 opt_pass *pass = *pass_list, *prev_pass = NULL;
1337 bool success = false;
1339 for ( ; pass; prev_pass = pass, pass = pass->next)
1341 /* Check if the current pass is of the same type as the new pass and
1342 matches the name and the instance number of the reference pass. */
1343 if (pass->type == new_pass_info->pass->type
1344 && pass->name
1345 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1346 && ((new_pass_info->ref_pass_instance_number == 0)
1347 || (new_pass_info->ref_pass_instance_number ==
1348 pass->static_pass_number)
1349 || (new_pass_info->ref_pass_instance_number == 1
1350 && pass->todo_flags_start & TODO_mark_first_instance)))
1352 opt_pass *new_pass;
1353 struct pass_list_node *new_pass_node;
1355 if (new_pass_info->ref_pass_instance_number == 0)
1357 new_pass = new_pass_info->pass->clone ();
1358 add_pass_instance (new_pass, true, new_pass_info->pass);
1360 else
1362 new_pass = new_pass_info->pass;
1363 add_pass_instance (new_pass, true, new_pass);
1366 /* Insert the new pass instance based on the positioning op. */
1367 switch (new_pass_info->pos_op)
1369 case PASS_POS_INSERT_AFTER:
1370 new_pass->next = pass->next;
1371 pass->next = new_pass;
1373 /* Skip newly inserted pass to avoid repeated
1374 insertions in the case where the new pass and the
1375 existing one have the same name. */
1376 pass = new_pass;
1377 break;
1378 case PASS_POS_INSERT_BEFORE:
1379 new_pass->next = pass;
1380 if (prev_pass)
1381 prev_pass->next = new_pass;
1382 else
1383 *pass_list = new_pass;
1384 break;
1385 case PASS_POS_REPLACE:
1386 new_pass->next = pass->next;
1387 if (prev_pass)
1388 prev_pass->next = new_pass;
1389 else
1390 *pass_list = new_pass;
1391 new_pass->sub = pass->sub;
1392 new_pass->tv_id = pass->tv_id;
1393 pass = new_pass;
1394 break;
1395 default:
1396 error ("invalid pass positioning operation");
1397 return false;
1400 /* Save the newly added pass (instance) in the added_pass_nodes
1401 list so that we can register its dump file later. Note that
1402 we cannot register the dump file now because doing so will modify
1403 the static_pass_number of the opt_pass object and therefore
1404 mess up the dump file name of future instances. */
1405 new_pass_node = XCNEW (struct pass_list_node);
1406 new_pass_node->pass = new_pass;
1407 if (!added_pass_nodes)
1408 added_pass_nodes = new_pass_node;
1409 else
1410 prev_added_pass_node->next = new_pass_node;
1411 prev_added_pass_node = new_pass_node;
1413 success = true;
1416 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1417 success = true;
1420 return success;
1423 /* Hooks a new pass into the pass lists.
1425 PASS_INFO - pass information that specifies the opt_pass object,
1426 reference pass, instance number, and how to position
1427 the pass */
1429 void
1430 register_pass (struct register_pass_info *pass_info)
1432 g->get_passes ()->register_pass (pass_info);
1435 void
1436 register_pass (opt_pass* pass, pass_positioning_ops pos,
1437 const char* ref_pass_name, int ref_pass_inst_number)
1439 register_pass_info i;
1440 i.pass = pass;
1441 i.reference_pass_name = ref_pass_name;
1442 i.ref_pass_instance_number = ref_pass_inst_number;
1443 i.pos_op = pos;
1445 g->get_passes ()->register_pass (&i);
1448 void
1449 pass_manager::register_pass (struct register_pass_info *pass_info)
1451 bool all_instances, success;
1452 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1454 /* The checks below could fail in buggy plugins. Existing GCC
1455 passes should never fail these checks, so we mention plugin in
1456 the messages. */
1457 if (!pass_info->pass)
1458 fatal_error ("plugin cannot register a missing pass");
1460 if (!pass_info->pass->name)
1461 fatal_error ("plugin cannot register an unnamed pass");
1463 if (!pass_info->reference_pass_name)
1464 fatal_error
1465 ("plugin cannot register pass %qs without reference pass name",
1466 pass_info->pass->name);
1468 /* Try to insert the new pass to the pass lists. We need to check
1469 all five lists as the reference pass could be in one (or all) of
1470 them. */
1471 all_instances = pass_info->ref_pass_instance_number == 0;
1472 success = position_pass (pass_info, &all_lowering_passes);
1473 if (!success || all_instances)
1474 success |= position_pass (pass_info, &all_small_ipa_passes);
1475 if (!success || all_instances)
1476 success |= position_pass (pass_info, &all_regular_ipa_passes);
1477 if (!success || all_instances)
1478 success |= position_pass (pass_info, &all_late_ipa_passes);
1479 if (!success || all_instances)
1480 success |= position_pass (pass_info, &all_passes);
1481 if (!success)
1482 fatal_error
1483 ("pass %qs not found but is referenced by new pass %qs",
1484 pass_info->reference_pass_name, pass_info->pass->name);
1486 /* OK, we have successfully inserted the new pass. We need to register
1487 the dump files for the newly added pass and its duplicates (if any).
1488 Because the registration of plugin/backend passes happens after the
1489 command-line options are parsed, the options that specify single
1490 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1491 passes. Therefore we currently can only enable dumping of
1492 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1493 are specified. While doing so, we also delete the pass_list_node
1494 objects created during pass positioning. */
1495 while (added_pass_nodes)
1497 struct pass_list_node *next_node = added_pass_nodes->next;
1498 enum tree_dump_index tdi;
1499 register_one_dump_file (added_pass_nodes->pass);
1500 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1501 || added_pass_nodes->pass->type == IPA_PASS)
1502 tdi = TDI_ipa_all;
1503 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1504 tdi = TDI_tree_all;
1505 else
1506 tdi = TDI_rtl_all;
1507 /* Check if dump-all flag is specified. */
1508 if (dumps->get_dump_file_info (tdi)->pstate)
1509 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1510 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1511 XDELETE (added_pass_nodes);
1512 added_pass_nodes = next_node;
1516 /* Construct the pass tree. The sequencing of passes is driven by
1517 the cgraph routines:
1519 finalize_compilation_unit ()
1520 for each node N in the cgraph
1521 cgraph_analyze_function (N)
1522 cgraph_lower_function (N) -> all_lowering_passes
1524 If we are optimizing, compile is then invoked:
1526 compile ()
1527 ipa_passes () -> all_small_ipa_passes
1528 -> Analysis of all_regular_ipa_passes
1529 * possible LTO streaming at copmilation time *
1530 -> Execution of all_regular_ipa_passes
1531 * possible LTO streaming at link time *
1532 -> all_late_ipa_passes
1533 expand_all_functions ()
1534 for each node N in the cgraph
1535 expand_function (N) -> Transformation of all_regular_ipa_passes
1536 -> all_passes
1539 void *
1540 pass_manager::operator new (size_t sz)
1542 /* Ensure that all fields of the pass manager are zero-initialized. */
1543 return xcalloc (1, sz);
1546 void
1547 pass_manager::operator delete (void *ptr)
1549 free (ptr);
1552 pass_manager::pass_manager (context *ctxt)
1553 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1554 all_regular_ipa_passes (NULL),
1555 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1556 m_ctxt (ctxt)
1558 opt_pass **p;
1560 /* Initialize the pass_lists array. */
1561 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1562 GCC_PASS_LISTS
1563 #undef DEF_PASS_LIST
1565 /* Build the tree of passes. */
1567 #define INSERT_PASSES_AFTER(PASS) \
1568 p = &(PASS);
1570 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1572 opt_pass **p = &(PASS ## _1)->sub;
1574 #define POP_INSERT_PASSES() \
1577 #define NEXT_PASS(PASS, NUM) \
1578 do { \
1579 gcc_assert (NULL == PASS ## _ ## NUM); \
1580 if ((NUM) == 1) \
1581 PASS ## _1 = make_##PASS (m_ctxt); \
1582 else \
1584 gcc_assert (PASS ## _1); \
1585 PASS ## _ ## NUM = PASS ## _1->clone (); \
1587 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1588 } while (0)
1590 #define TERMINATE_PASS_LIST() \
1591 *p = NULL;
1593 #include "pass-instances.def"
1595 #undef INSERT_PASSES_AFTER
1596 #undef PUSH_INSERT_PASSES_WITHIN
1597 #undef POP_INSERT_PASSES
1598 #undef NEXT_PASS
1599 #undef TERMINATE_PASS_LIST
1601 /* Register the passes with the tree dump code. */
1602 register_dump_files (all_lowering_passes);
1603 register_dump_files (all_small_ipa_passes);
1604 register_dump_files (all_regular_ipa_passes);
1605 register_dump_files (all_late_ipa_passes);
1606 register_dump_files (all_passes);
1609 static void
1610 delete_pass_tree (opt_pass *pass)
1612 while (pass)
1614 /* Recurse into child passes. */
1615 delete_pass_tree (pass->sub);
1617 opt_pass *next = pass->next;
1619 /* Delete this pass. */
1620 delete pass;
1622 /* Iterate onto sibling passes. */
1623 pass = next;
1627 pass_manager::~pass_manager ()
1629 XDELETEVEC (passes_by_id);
1631 /* Call delete_pass_tree on each of the pass_lists. */
1632 #define DEF_PASS_LIST(LIST) \
1633 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1634 GCC_PASS_LISTS
1635 #undef DEF_PASS_LIST
1639 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1640 function CALLBACK for every function in the call graph. Otherwise,
1641 call CALLBACK on the current function. */
1643 static void
1644 do_per_function (void (*callback) (function *, void *data), void *data)
1646 if (current_function_decl)
1647 callback (cfun, data);
1648 else
1650 struct cgraph_node *node;
1651 FOR_EACH_DEFINED_FUNCTION (node)
1652 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1653 && (!node->clone_of || node->decl != node->clone_of->decl))
1654 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1658 /* Because inlining might remove no-longer reachable nodes, we need to
1659 keep the array visible to garbage collector to avoid reading collected
1660 out nodes. */
1661 static int nnodes;
1662 static GTY ((length ("nnodes"))) cgraph_node **order;
1664 /* Hook called when NODE is removed and therefore should be
1665 excluded from order vector. DATA is an array of integers.
1666 DATA[0] holds max index it may be accessed by. For cgraph
1667 node DATA[node->uid + 1] holds index of this node in order
1668 vector. */
1669 static void
1670 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1672 int *order_idx = (int *)data;
1674 if (node->uid >= order_idx[0])
1675 return;
1677 int idx = order_idx[node->uid + 1];
1678 if (idx >= 0 && idx < nnodes && order[idx] == node)
1679 order[idx] = NULL;
1682 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1683 function CALLBACK for every function in the call graph. Otherwise,
1684 call CALLBACK on the current function.
1685 This function is global so that plugins can use it. */
1686 void
1687 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1689 int i;
1691 if (current_function_decl)
1692 callback (cfun, data);
1693 else
1695 cgraph_node_hook_list *hook;
1696 int *order_idx;
1697 gcc_assert (!order);
1698 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1700 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1701 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1702 order_idx[0] = symtab->cgraph_max_uid;
1704 nnodes = ipa_reverse_postorder (order);
1705 for (i = nnodes - 1; i >= 0; i--)
1707 order[i]->process = 1;
1708 order_idx[order[i]->uid + 1] = i;
1710 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1711 order_idx);
1712 for (i = nnodes - 1; i >= 0; i--)
1714 /* Function could be inlined and removed as unreachable. */
1715 if (!order[i])
1716 continue;
1718 struct cgraph_node *node = order[i];
1720 /* Allow possibly removed nodes to be garbage collected. */
1721 order[i] = NULL;
1722 node->process = 0;
1723 if (node->has_gimple_body_p ())
1724 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1726 symtab->remove_cgraph_removal_hook (hook);
1728 ggc_free (order);
1729 order = NULL;
1730 nnodes = 0;
1733 /* Helper function to perform function body dump. */
1735 static void
1736 execute_function_dump (function *fn, void *data)
1738 opt_pass *pass = (opt_pass *)data;
1740 if (dump_file)
1742 push_cfun (fn);
1744 if (fn->curr_properties & PROP_trees)
1745 dump_function_to_file (fn->decl, dump_file, dump_flags);
1746 else
1747 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1749 /* Flush the file. If verification fails, we won't be able to
1750 close the file before aborting. */
1751 fflush (dump_file);
1753 if ((fn->curr_properties & PROP_cfg)
1754 && (dump_flags & TDF_GRAPH))
1756 if (!pass->graph_dump_initialized)
1758 clean_graph_dump_file (dump_file_name);
1759 pass->graph_dump_initialized = true;
1761 print_graph_cfg (dump_file_name, fn);
1764 pop_cfun ();
1768 static struct profile_record *profile_record;
1770 /* Do profile consistency book-keeping for the pass with static number INDEX.
1771 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1772 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1773 if we are only book-keeping on passes that may have selectively disabled
1774 themselves on a given function. */
1775 static void
1776 check_profile_consistency (int index, int subpass, bool run)
1778 pass_manager *passes = g->get_passes ();
1779 if (index == -1)
1780 return;
1781 if (!profile_record)
1782 profile_record = XCNEWVEC (struct profile_record,
1783 passes->passes_by_id_size);
1784 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1785 gcc_assert (subpass < 2);
1786 profile_record[index].run |= run;
1787 account_profile_record (&profile_record[index], subpass);
1790 /* Output profile consistency. */
1792 void
1793 dump_profile_report (void)
1795 g->get_passes ()->dump_profile_report ();
1798 void
1799 pass_manager::dump_profile_report () const
1801 int i, j;
1802 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1803 gcov_type last_time = 0, last_size = 0;
1804 double rel_time_change, rel_size_change;
1805 int last_reported = 0;
1807 if (!profile_record)
1808 return;
1809 fprintf (stderr, "\nProfile consistency report:\n\n");
1810 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1811 fprintf (stderr, " |freq count |freq count |size time\n");
1813 for (i = 0; i < passes_by_id_size; i++)
1814 for (j = 0 ; j < 2; j++)
1815 if (profile_record[i].run)
1817 if (last_time)
1818 rel_time_change = (profile_record[i].time[j]
1819 - (double)last_time) * 100 / (double)last_time;
1820 else
1821 rel_time_change = 0;
1822 if (last_size)
1823 rel_size_change = (profile_record[i].size[j]
1824 - (double)last_size) * 100 / (double)last_size;
1825 else
1826 rel_size_change = 0;
1828 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1829 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1830 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1831 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1832 || rel_time_change || rel_size_change)
1834 last_reported = i;
1835 fprintf (stderr, "%-20s %s",
1836 passes_by_id [i]->name,
1837 j ? "(after TODO)" : " ");
1838 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1839 fprintf (stderr, "| %+5i",
1840 profile_record[i].num_mismatched_freq_in[j]
1841 - last_freq_in);
1842 else
1843 fprintf (stderr, "| ");
1844 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1845 fprintf (stderr, " %+5i",
1846 profile_record[i].num_mismatched_count_in[j]
1847 - last_count_in);
1848 else
1849 fprintf (stderr, " ");
1850 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1851 fprintf (stderr, "| %+5i",
1852 profile_record[i].num_mismatched_freq_out[j]
1853 - last_freq_out);
1854 else
1855 fprintf (stderr, "| ");
1856 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1857 fprintf (stderr, " %+5i",
1858 profile_record[i].num_mismatched_count_out[j]
1859 - last_count_out);
1860 else
1861 fprintf (stderr, " ");
1863 /* Size/time units change across gimple and RTL. */
1864 if (i == pass_expand_1->static_pass_number)
1865 fprintf (stderr, "|----------");
1866 else
1868 if (rel_size_change)
1869 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1870 else
1871 fprintf (stderr, "| ");
1872 if (rel_time_change)
1873 fprintf (stderr, " %+8.4f%%", rel_time_change);
1875 fprintf (stderr, "\n");
1876 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1877 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1878 last_count_in = profile_record[i].num_mismatched_count_in[j];
1879 last_count_out = profile_record[i].num_mismatched_count_out[j];
1881 else if (j && last_reported != i)
1883 last_reported = i;
1884 fprintf (stderr, "%-20s ------------| | |\n",
1885 passes_by_id [i]->name);
1887 last_time = profile_record[i].time[j];
1888 last_size = profile_record[i].size[j];
1892 /* Perform all TODO actions that ought to be done on each function. */
1894 static void
1895 execute_function_todo (function *fn, void *data)
1897 bool from_ipa_pass = (cfun == NULL);
1898 unsigned int flags = (size_t)data;
1899 flags &= ~fn->last_verified;
1900 if (!flags)
1901 return;
1903 push_cfun (fn);
1905 /* Always cleanup the CFG before trying to update SSA. */
1906 if (flags & TODO_cleanup_cfg)
1908 cleanup_tree_cfg ();
1910 /* When cleanup_tree_cfg merges consecutive blocks, it may
1911 perform some simplistic propagation when removing single
1912 valued PHI nodes. This propagation may, in turn, cause the
1913 SSA form to become out-of-date (see PR 22037). So, even
1914 if the parent pass had not scheduled an SSA update, we may
1915 still need to do one. */
1916 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1917 flags |= TODO_update_ssa;
1920 if (flags & TODO_update_ssa_any)
1922 unsigned update_flags = flags & TODO_update_ssa_any;
1923 update_ssa (update_flags);
1926 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1927 compute_may_aliases ();
1929 if (optimize && (flags & TODO_update_address_taken))
1930 execute_update_addresses_taken ();
1932 if (flags & TODO_remove_unused_locals)
1933 remove_unused_locals ();
1935 if (flags & TODO_rebuild_frequencies)
1936 rebuild_frequencies ();
1938 if (flags & TODO_rebuild_cgraph_edges)
1939 cgraph_edge::rebuild_edges ();
1941 /* If we've seen errors do not bother running any verifiers. */
1942 if (!seen_error ())
1944 #if defined ENABLE_CHECKING
1945 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1946 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1948 if (flags & TODO_verify_il)
1950 if (cfun->curr_properties & PROP_trees)
1952 if (cfun->curr_properties & PROP_cfg)
1953 /* IPA passes leave stmts to be fixed up, so make sure to
1954 not verify stmts really throw. */
1955 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1956 else
1957 verify_gimple_in_seq (gimple_body (cfun->decl));
1959 if (cfun->curr_properties & PROP_ssa)
1960 /* IPA passes leave stmts to be fixed up, so make sure to
1961 not verify SSA operands whose verifier will choke on that. */
1962 verify_ssa (true, !from_ipa_pass);
1963 /* IPA passes leave basic-blocks unsplit, so make sure to
1964 not trip on that. */
1965 if ((cfun->curr_properties & PROP_cfg)
1966 && !from_ipa_pass)
1967 verify_flow_info ();
1968 if (current_loops
1969 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1970 verify_loop_closed_ssa (false);
1971 if (cfun->curr_properties & PROP_rtl)
1972 verify_rtl_sharing ();
1975 /* Make sure verifiers don't change dominator state. */
1976 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1977 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1978 #endif
1981 fn->last_verified = flags & TODO_verify_all;
1983 pop_cfun ();
1985 /* For IPA passes make sure to release dominator info, it can be
1986 computed by non-verifying TODOs. */
1987 if (from_ipa_pass)
1989 free_dominance_info (fn, CDI_DOMINATORS);
1990 free_dominance_info (fn, CDI_POST_DOMINATORS);
1994 /* Perform all TODO actions. */
1995 static void
1996 execute_todo (unsigned int flags)
1998 #if defined ENABLE_CHECKING
1999 if (cfun
2000 && need_ssa_update_p (cfun))
2001 gcc_assert (flags & TODO_update_ssa_any);
2002 #endif
2004 timevar_push (TV_TODO);
2006 /* Inform the pass whether it is the first time it is run. */
2007 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2009 statistics_fini_pass ();
2011 if (flags)
2012 do_per_function (execute_function_todo, (void *)(size_t) flags);
2014 /* Always remove functions just as before inlining: IPA passes might be
2015 interested to see bodies of extern inline functions that are not inlined
2016 to analyze side effects. The full removal is done just at the end
2017 of IPA pass queue. */
2018 if (flags & TODO_remove_functions)
2020 gcc_assert (!cfun);
2021 symtab->remove_unreachable_nodes (dump_file);
2024 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2026 gcc_assert (!cfun);
2027 symtab_node::dump_table (dump_file);
2028 /* Flush the file. If verification fails, we won't be able to
2029 close the file before aborting. */
2030 fflush (dump_file);
2033 /* Now that the dumping has been done, we can get rid of the optional
2034 df problems. */
2035 if (flags & TODO_df_finish)
2036 df_finish_pass ((flags & TODO_df_verify) != 0);
2038 timevar_pop (TV_TODO);
2041 /* Verify invariants that should hold between passes. This is a place
2042 to put simple sanity checks. */
2044 static void
2045 verify_interpass_invariants (void)
2047 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2050 /* Clear the last verified flag. */
2052 static void
2053 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2055 fn->last_verified = 0;
2058 /* Helper function. Verify that the properties has been turn into the
2059 properties expected by the pass. */
2061 #ifdef ENABLE_CHECKING
2062 static void
2063 verify_curr_properties (function *fn, void *data)
2065 unsigned int props = (size_t)data;
2066 gcc_assert ((fn->curr_properties & props) == props);
2068 #endif
2070 /* Initialize pass dump file. */
2071 /* This is non-static so that the plugins can use it. */
2073 bool
2074 pass_init_dump_file (opt_pass *pass)
2076 /* If a dump file name is present, open it if enabled. */
2077 if (pass->static_pass_number != -1)
2079 timevar_push (TV_DUMP);
2080 gcc::dump_manager *dumps = g->get_dumps ();
2081 bool initializing_dump =
2082 !dumps->dump_initialized_p (pass->static_pass_number);
2083 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2084 dumps->dump_start (pass->static_pass_number, &dump_flags);
2085 if (dump_file && current_function_decl)
2086 dump_function_header (dump_file, current_function_decl, dump_flags);
2087 if (initializing_dump
2088 && dump_file && (dump_flags & TDF_GRAPH)
2089 && cfun && (cfun->curr_properties & PROP_cfg))
2091 clean_graph_dump_file (dump_file_name);
2092 pass->graph_dump_initialized = true;
2094 timevar_pop (TV_DUMP);
2095 return initializing_dump;
2097 else
2098 return false;
2101 /* Flush PASS dump file. */
2102 /* This is non-static so that plugins can use it. */
2104 void
2105 pass_fini_dump_file (opt_pass *pass)
2107 timevar_push (TV_DUMP);
2109 /* Flush and close dump file. */
2110 if (dump_file_name)
2112 free (CONST_CAST (char *, dump_file_name));
2113 dump_file_name = NULL;
2116 g->get_dumps ()->dump_finish (pass->static_pass_number);
2117 timevar_pop (TV_DUMP);
2120 /* After executing the pass, apply expected changes to the function
2121 properties. */
2123 static void
2124 update_properties_after_pass (function *fn, void *data)
2126 opt_pass *pass = (opt_pass *) data;
2127 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2128 & ~pass->properties_destroyed;
2131 /* Execute summary generation for all of the passes in IPA_PASS. */
2133 void
2134 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2136 while (ipa_pass)
2138 opt_pass *pass = ipa_pass;
2140 /* Execute all of the IPA_PASSes in the list. */
2141 if (ipa_pass->type == IPA_PASS
2142 && pass->gate (cfun)
2143 && ipa_pass->generate_summary)
2145 pass_init_dump_file (pass);
2147 /* If a timevar is present, start it. */
2148 if (pass->tv_id)
2149 timevar_push (pass->tv_id);
2151 current_pass = pass;
2152 ipa_pass->generate_summary ();
2154 /* Stop timevar. */
2155 if (pass->tv_id)
2156 timevar_pop (pass->tv_id);
2158 pass_fini_dump_file (pass);
2160 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2164 /* Execute IPA_PASS function transform on NODE. */
2166 static void
2167 execute_one_ipa_transform_pass (struct cgraph_node *node,
2168 ipa_opt_pass_d *ipa_pass)
2170 opt_pass *pass = ipa_pass;
2171 unsigned int todo_after = 0;
2173 current_pass = pass;
2174 if (!ipa_pass->function_transform)
2175 return;
2177 /* Note that the folders should only create gimple expressions.
2178 This is a hack until the new folder is ready. */
2179 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2181 pass_init_dump_file (pass);
2183 /* Run pre-pass verification. */
2184 execute_todo (ipa_pass->function_transform_todo_flags_start);
2186 /* If a timevar is present, start it. */
2187 if (pass->tv_id != TV_NONE)
2188 timevar_push (pass->tv_id);
2190 /* Do it! */
2191 todo_after = ipa_pass->function_transform (node);
2193 /* Stop timevar. */
2194 if (pass->tv_id != TV_NONE)
2195 timevar_pop (pass->tv_id);
2197 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2198 check_profile_consistency (pass->static_pass_number, 0, true);
2200 /* Run post-pass cleanup and verification. */
2201 execute_todo (todo_after);
2202 verify_interpass_invariants ();
2203 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2204 check_profile_consistency (pass->static_pass_number, 1, true);
2206 if (dump_file)
2207 do_per_function (execute_function_dump, NULL);
2208 pass_fini_dump_file (pass);
2210 current_pass = NULL;
2212 /* Signal this is a suitable GC collection point. */
2213 if (!(todo_after & TODO_do_not_ggc_collect))
2214 ggc_collect ();
2217 /* For the current function, execute all ipa transforms. */
2219 void
2220 execute_all_ipa_transforms (void)
2222 struct cgraph_node *node;
2223 if (!cfun)
2224 return;
2225 node = cgraph_node::get (current_function_decl);
2227 if (node->ipa_transforms_to_apply.exists ())
2229 unsigned int i;
2231 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2232 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2233 node->ipa_transforms_to_apply.release ();
2237 /* Check if PASS is explicitly disabled or enabled and return
2238 the gate status. FUNC is the function to be processed, and
2239 GATE_STATUS is the gate status determined by pass manager by
2240 default. */
2242 static bool
2243 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2245 bool explicitly_enabled = false;
2246 bool explicitly_disabled = false;
2248 explicitly_enabled
2249 = is_pass_explicitly_enabled_or_disabled (pass, func,
2250 enabled_pass_uid_range_tab);
2251 explicitly_disabled
2252 = is_pass_explicitly_enabled_or_disabled (pass, func,
2253 disabled_pass_uid_range_tab);
2255 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2257 return gate_status;
2261 /* Execute PASS. */
2263 bool
2264 execute_one_pass (opt_pass *pass)
2266 unsigned int todo_after = 0;
2268 bool gate_status;
2270 /* IPA passes are executed on whole program, so cfun should be NULL.
2271 Other passes need function context set. */
2272 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2273 gcc_assert (!cfun && !current_function_decl);
2274 else
2275 gcc_assert (cfun && current_function_decl);
2277 current_pass = pass;
2279 /* Check whether gate check should be avoided.
2280 User controls the value of the gate through the parameter "gate_status". */
2281 gate_status = pass->gate (cfun);
2282 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2284 /* Override gate with plugin. */
2285 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2287 if (!gate_status)
2289 /* Run so passes selectively disabling themselves on a given function
2290 are not miscounted. */
2291 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2293 check_profile_consistency (pass->static_pass_number, 0, false);
2294 check_profile_consistency (pass->static_pass_number, 1, false);
2296 current_pass = NULL;
2297 return false;
2300 /* Pass execution event trigger: useful to identify passes being
2301 executed. */
2302 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2304 if (!quiet_flag && !cfun)
2305 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2307 /* Note that the folders should only create gimple expressions.
2308 This is a hack until the new folder is ready. */
2309 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2311 pass_init_dump_file (pass);
2313 /* Run pre-pass verification. */
2314 execute_todo (pass->todo_flags_start);
2316 #ifdef ENABLE_CHECKING
2317 do_per_function (verify_curr_properties,
2318 (void *)(size_t)pass->properties_required);
2319 #endif
2321 /* If a timevar is present, start it. */
2322 if (pass->tv_id != TV_NONE)
2323 timevar_push (pass->tv_id);
2325 /* Do it! */
2326 todo_after = pass->execute (cfun);
2327 do_per_function (clear_last_verified, NULL);
2329 /* Stop timevar. */
2330 if (pass->tv_id != TV_NONE)
2331 timevar_pop (pass->tv_id);
2333 do_per_function (update_properties_after_pass, pass);
2335 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2336 check_profile_consistency (pass->static_pass_number, 0, true);
2338 /* Run post-pass cleanup and verification. */
2339 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2340 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2341 check_profile_consistency (pass->static_pass_number, 1, true);
2343 verify_interpass_invariants ();
2344 if (dump_file)
2345 do_per_function (execute_function_dump, pass);
2346 if (pass->type == IPA_PASS)
2348 struct cgraph_node *node;
2349 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2350 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2353 if (!current_function_decl)
2354 symtab->process_new_functions ();
2356 pass_fini_dump_file (pass);
2358 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2359 gcc_assert (!(cfun->curr_properties & PROP_trees)
2360 || pass->type != RTL_PASS);
2362 current_pass = NULL;
2364 /* Signal this is a suitable GC collection point. */
2365 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2366 ggc_collect ();
2368 return true;
2371 static void
2372 execute_pass_list_1 (opt_pass *pass)
2376 gcc_assert (pass->type == GIMPLE_PASS
2377 || pass->type == RTL_PASS);
2378 if (execute_one_pass (pass) && pass->sub)
2379 execute_pass_list_1 (pass->sub);
2380 pass = pass->next;
2382 while (pass);
2385 void
2386 execute_pass_list (function *fn, opt_pass *pass)
2388 push_cfun (fn);
2389 execute_pass_list_1 (pass);
2390 if (fn->cfg)
2392 free_dominance_info (CDI_DOMINATORS);
2393 free_dominance_info (CDI_POST_DOMINATORS);
2395 pop_cfun ();
2398 /* Write out all LTO data. */
2399 static void
2400 write_lto (void)
2402 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2403 lto_output ();
2404 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2405 timevar_push (TV_IPA_LTO_DECL_OUT);
2406 produce_asm_for_decls ();
2407 timevar_pop (TV_IPA_LTO_DECL_OUT);
2410 /* Same as execute_pass_list but assume that subpasses of IPA passes
2411 are local passes. If SET is not NULL, write out summaries of only
2412 those node in SET. */
2414 static void
2415 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2417 while (pass)
2419 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2420 gcc_assert (!current_function_decl);
2421 gcc_assert (!cfun);
2422 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2423 if (pass->type == IPA_PASS
2424 && ipa_pass->write_summary
2425 && pass->gate (cfun))
2427 /* If a timevar is present, start it. */
2428 if (pass->tv_id)
2429 timevar_push (pass->tv_id);
2431 pass_init_dump_file (pass);
2433 current_pass = pass;
2434 ipa_pass->write_summary ();
2436 pass_fini_dump_file (pass);
2438 /* If a timevar is present, start it. */
2439 if (pass->tv_id)
2440 timevar_pop (pass->tv_id);
2443 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2444 ipa_write_summaries_2 (pass->sub, state);
2446 pass = pass->next;
2450 /* Helper function of ipa_write_summaries. Creates and destroys the
2451 decl state and calls ipa_write_summaries_2 for all passes that have
2452 summaries. SET is the set of nodes to be written. */
2454 static void
2455 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2457 pass_manager *passes = g->get_passes ();
2458 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2459 state->symtab_node_encoder = encoder;
2461 lto_push_out_decl_state (state);
2463 gcc_assert (!flag_wpa);
2464 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2466 write_lto ();
2468 gcc_assert (lto_get_out_decl_state () == state);
2469 lto_pop_out_decl_state ();
2470 lto_delete_out_decl_state (state);
2473 /* Write out summaries for all the nodes in the callgraph. */
2475 void
2476 ipa_write_summaries (void)
2478 lto_symtab_encoder_t encoder;
2479 int i, order_pos;
2480 varpool_node *vnode;
2481 struct cgraph_node *node;
2482 struct cgraph_node **order;
2484 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2485 return;
2487 select_what_to_stream ();
2489 encoder = lto_symtab_encoder_new (false);
2491 /* Create the callgraph set in the same order used in
2492 cgraph_expand_all_functions. This mostly facilitates debugging,
2493 since it causes the gimple file to be processed in the same order
2494 as the source code. */
2495 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2496 order_pos = ipa_reverse_postorder (order);
2497 gcc_assert (order_pos == symtab->cgraph_count);
2499 for (i = order_pos - 1; i >= 0; i--)
2501 struct cgraph_node *node = order[i];
2503 if (node->has_gimple_body_p ())
2505 /* When streaming out references to statements as part of some IPA
2506 pass summary, the statements need to have uids assigned and the
2507 following does that for all the IPA passes here. Naturally, this
2508 ordering then matches the one IPA-passes get in their stmt_fixup
2509 hooks. */
2511 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2512 renumber_gimple_stmt_uids ();
2513 pop_cfun ();
2515 if (node->definition && node->need_lto_streaming)
2516 lto_set_symtab_encoder_in_partition (encoder, node);
2519 FOR_EACH_DEFINED_FUNCTION (node)
2520 if (node->alias && node->need_lto_streaming)
2521 lto_set_symtab_encoder_in_partition (encoder, node);
2522 FOR_EACH_DEFINED_VARIABLE (vnode)
2523 if (vnode->need_lto_streaming)
2524 lto_set_symtab_encoder_in_partition (encoder, vnode);
2526 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2528 free (order);
2531 /* Same as execute_pass_list but assume that subpasses of IPA passes
2532 are local passes. If SET is not NULL, write out optimization summaries of
2533 only those node in SET. */
2535 static void
2536 ipa_write_optimization_summaries_1 (opt_pass *pass,
2537 struct lto_out_decl_state *state)
2539 while (pass)
2541 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2542 gcc_assert (!current_function_decl);
2543 gcc_assert (!cfun);
2544 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2545 if (pass->type == IPA_PASS
2546 && ipa_pass->write_optimization_summary
2547 && pass->gate (cfun))
2549 /* If a timevar is present, start it. */
2550 if (pass->tv_id)
2551 timevar_push (pass->tv_id);
2553 pass_init_dump_file (pass);
2555 current_pass = pass;
2556 ipa_pass->write_optimization_summary ();
2558 pass_fini_dump_file (pass);
2560 /* If a timevar is present, start it. */
2561 if (pass->tv_id)
2562 timevar_pop (pass->tv_id);
2565 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2566 ipa_write_optimization_summaries_1 (pass->sub, state);
2568 pass = pass->next;
2572 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2573 NULL, write out all summaries of all nodes. */
2575 void
2576 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2578 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2579 lto_symtab_encoder_iterator lsei;
2580 state->symtab_node_encoder = encoder;
2582 lto_push_out_decl_state (state);
2583 for (lsei = lsei_start_function_in_partition (encoder);
2584 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2586 struct cgraph_node *node = lsei_cgraph_node (lsei);
2587 /* When streaming out references to statements as part of some IPA
2588 pass summary, the statements need to have uids assigned.
2590 For functions newly born at WPA stage we need to initialize
2591 the uids here. */
2592 if (node->definition
2593 && gimple_has_body_p (node->decl))
2595 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2596 renumber_gimple_stmt_uids ();
2597 pop_cfun ();
2601 gcc_assert (flag_wpa);
2602 pass_manager *passes = g->get_passes ();
2603 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2605 write_lto ();
2607 gcc_assert (lto_get_out_decl_state () == state);
2608 lto_pop_out_decl_state ();
2609 lto_delete_out_decl_state (state);
2612 /* Same as execute_pass_list but assume that subpasses of IPA passes
2613 are local passes. */
2615 static void
2616 ipa_read_summaries_1 (opt_pass *pass)
2618 while (pass)
2620 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2622 gcc_assert (!current_function_decl);
2623 gcc_assert (!cfun);
2624 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2626 if (pass->gate (cfun))
2628 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2630 /* If a timevar is present, start it. */
2631 if (pass->tv_id)
2632 timevar_push (pass->tv_id);
2634 pass_init_dump_file (pass);
2636 current_pass = pass;
2637 ipa_pass->read_summary ();
2639 pass_fini_dump_file (pass);
2641 /* Stop timevar. */
2642 if (pass->tv_id)
2643 timevar_pop (pass->tv_id);
2646 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2647 ipa_read_summaries_1 (pass->sub);
2649 pass = pass->next;
2654 /* Read all the summaries for all_regular_ipa_passes. */
2656 void
2657 ipa_read_summaries (void)
2659 pass_manager *passes = g->get_passes ();
2660 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2663 /* Same as execute_pass_list but assume that subpasses of IPA passes
2664 are local passes. */
2666 static void
2667 ipa_read_optimization_summaries_1 (opt_pass *pass)
2669 while (pass)
2671 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2673 gcc_assert (!current_function_decl);
2674 gcc_assert (!cfun);
2675 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2677 if (pass->gate (cfun))
2679 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2681 /* If a timevar is present, start it. */
2682 if (pass->tv_id)
2683 timevar_push (pass->tv_id);
2685 pass_init_dump_file (pass);
2687 current_pass = pass;
2688 ipa_pass->read_optimization_summary ();
2690 pass_fini_dump_file (pass);
2692 /* Stop timevar. */
2693 if (pass->tv_id)
2694 timevar_pop (pass->tv_id);
2697 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2698 ipa_read_optimization_summaries_1 (pass->sub);
2700 pass = pass->next;
2704 /* Read all the summaries for all_regular_ipa_passes. */
2706 void
2707 ipa_read_optimization_summaries (void)
2709 pass_manager *passes = g->get_passes ();
2710 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2713 /* Same as execute_pass_list but assume that subpasses of IPA passes
2714 are local passes. */
2715 void
2716 execute_ipa_pass_list (opt_pass *pass)
2720 gcc_assert (!current_function_decl);
2721 gcc_assert (!cfun);
2722 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2723 if (execute_one_pass (pass) && pass->sub)
2725 if (pass->sub->type == GIMPLE_PASS)
2727 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2728 do_per_function_toporder ((void (*)(function *, void *))
2729 execute_pass_list,
2730 pass->sub);
2731 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2733 else if (pass->sub->type == SIMPLE_IPA_PASS
2734 || pass->sub->type == IPA_PASS)
2735 execute_ipa_pass_list (pass->sub);
2736 else
2737 gcc_unreachable ();
2739 gcc_assert (!current_function_decl);
2740 symtab->process_new_functions ();
2741 pass = pass->next;
2743 while (pass);
2746 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2748 static void
2749 execute_ipa_stmt_fixups (opt_pass *pass,
2750 struct cgraph_node *node, gimple *stmts)
2752 while (pass)
2754 /* Execute all of the IPA_PASSes in the list. */
2755 if (pass->type == IPA_PASS
2756 && pass->gate (cfun))
2758 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2760 if (ipa_pass->stmt_fixup)
2762 pass_init_dump_file (pass);
2763 /* If a timevar is present, start it. */
2764 if (pass->tv_id)
2765 timevar_push (pass->tv_id);
2767 current_pass = pass;
2768 ipa_pass->stmt_fixup (node, stmts);
2770 /* Stop timevar. */
2771 if (pass->tv_id)
2772 timevar_pop (pass->tv_id);
2773 pass_fini_dump_file (pass);
2775 if (pass->sub)
2776 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2778 pass = pass->next;
2782 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2784 void
2785 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2787 pass_manager *passes = g->get_passes ();
2788 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2792 extern void debug_properties (unsigned int);
2793 extern void dump_properties (FILE *, unsigned int);
2795 DEBUG_FUNCTION void
2796 dump_properties (FILE *dump, unsigned int props)
2798 fprintf (dump, "Properties:\n");
2799 if (props & PROP_gimple_any)
2800 fprintf (dump, "PROP_gimple_any\n");
2801 if (props & PROP_gimple_lcf)
2802 fprintf (dump, "PROP_gimple_lcf\n");
2803 if (props & PROP_gimple_leh)
2804 fprintf (dump, "PROP_gimple_leh\n");
2805 if (props & PROP_cfg)
2806 fprintf (dump, "PROP_cfg\n");
2807 if (props & PROP_ssa)
2808 fprintf (dump, "PROP_ssa\n");
2809 if (props & PROP_no_crit_edges)
2810 fprintf (dump, "PROP_no_crit_edges\n");
2811 if (props & PROP_rtl)
2812 fprintf (dump, "PROP_rtl\n");
2813 if (props & PROP_gimple_lomp)
2814 fprintf (dump, "PROP_gimple_lomp\n");
2815 if (props & PROP_gimple_lcx)
2816 fprintf (dump, "PROP_gimple_lcx\n");
2817 if (props & PROP_gimple_lvec)
2818 fprintf (dump, "PROP_gimple_lvec\n");
2819 if (props & PROP_cfglayout)
2820 fprintf (dump, "PROP_cfglayout\n");
2823 DEBUG_FUNCTION void
2824 debug_properties (unsigned int props)
2826 dump_properties (stderr, props);
2829 /* Called by local passes to see if function is called by already processed nodes.
2830 Because we process nodes in topological order, this means that function is
2831 in recursive cycle or we introduced new direct calls. */
2832 bool
2833 function_called_by_processed_nodes_p (void)
2835 struct cgraph_edge *e;
2836 for (e = cgraph_node::get (current_function_decl)->callers;
2838 e = e->next_caller)
2840 if (e->caller->decl == current_function_decl)
2841 continue;
2842 if (!e->caller->has_gimple_body_p ())
2843 continue;
2844 if (TREE_ASM_WRITTEN (e->caller->decl))
2845 continue;
2846 if (!e->caller->process && !e->caller->global.inlined_to)
2847 break;
2849 if (dump_file && e)
2851 fprintf (dump_file, "Already processed call to:\n");
2852 e->caller->dump (dump_file);
2854 return e != NULL;
2857 #include "gt-passes.h"