2015-03-02 Hristian Kirtchev <kirtchev@adacore.com>
[official-gcc.git] / gcc / passes.c
blob23a90d9e2ebf680574ccecf8c631bda6fd236fef
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 (input_location, "plugin cannot register a missing pass");
1460 if (!pass_info->pass->name)
1461 fatal_error (input_location, "plugin cannot register an unnamed pass");
1463 if (!pass_info->reference_pass_name)
1464 fatal_error
1465 (input_location,
1466 "plugin cannot register pass %qs without reference pass name",
1467 pass_info->pass->name);
1469 /* Try to insert the new pass to the pass lists. We need to check
1470 all five lists as the reference pass could be in one (or all) of
1471 them. */
1472 all_instances = pass_info->ref_pass_instance_number == 0;
1473 success = position_pass (pass_info, &all_lowering_passes);
1474 if (!success || all_instances)
1475 success |= position_pass (pass_info, &all_small_ipa_passes);
1476 if (!success || all_instances)
1477 success |= position_pass (pass_info, &all_regular_ipa_passes);
1478 if (!success || all_instances)
1479 success |= position_pass (pass_info, &all_late_ipa_passes);
1480 if (!success || all_instances)
1481 success |= position_pass (pass_info, &all_passes);
1482 if (!success)
1483 fatal_error
1484 (input_location,
1485 "pass %qs not found but is referenced by new pass %qs",
1486 pass_info->reference_pass_name, pass_info->pass->name);
1488 /* OK, we have successfully inserted the new pass. We need to register
1489 the dump files for the newly added pass and its duplicates (if any).
1490 Because the registration of plugin/backend passes happens after the
1491 command-line options are parsed, the options that specify single
1492 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1493 passes. Therefore we currently can only enable dumping of
1494 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1495 are specified. While doing so, we also delete the pass_list_node
1496 objects created during pass positioning. */
1497 while (added_pass_nodes)
1499 struct pass_list_node *next_node = added_pass_nodes->next;
1500 enum tree_dump_index tdi;
1501 register_one_dump_file (added_pass_nodes->pass);
1502 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1503 || added_pass_nodes->pass->type == IPA_PASS)
1504 tdi = TDI_ipa_all;
1505 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1506 tdi = TDI_tree_all;
1507 else
1508 tdi = TDI_rtl_all;
1509 /* Check if dump-all flag is specified. */
1510 if (dumps->get_dump_file_info (tdi)->pstate)
1511 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1512 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1513 XDELETE (added_pass_nodes);
1514 added_pass_nodes = next_node;
1518 /* Construct the pass tree. The sequencing of passes is driven by
1519 the cgraph routines:
1521 finalize_compilation_unit ()
1522 for each node N in the cgraph
1523 cgraph_analyze_function (N)
1524 cgraph_lower_function (N) -> all_lowering_passes
1526 If we are optimizing, compile is then invoked:
1528 compile ()
1529 ipa_passes () -> all_small_ipa_passes
1530 -> Analysis of all_regular_ipa_passes
1531 * possible LTO streaming at copmilation time *
1532 -> Execution of all_regular_ipa_passes
1533 * possible LTO streaming at link time *
1534 -> all_late_ipa_passes
1535 expand_all_functions ()
1536 for each node N in the cgraph
1537 expand_function (N) -> Transformation of all_regular_ipa_passes
1538 -> all_passes
1541 void *
1542 pass_manager::operator new (size_t sz)
1544 /* Ensure that all fields of the pass manager are zero-initialized. */
1545 return xcalloc (1, sz);
1548 void
1549 pass_manager::operator delete (void *ptr)
1551 free (ptr);
1554 pass_manager::pass_manager (context *ctxt)
1555 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1556 all_regular_ipa_passes (NULL),
1557 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1558 m_ctxt (ctxt)
1560 opt_pass **p;
1562 /* Initialize the pass_lists array. */
1563 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1564 GCC_PASS_LISTS
1565 #undef DEF_PASS_LIST
1567 /* Build the tree of passes. */
1569 #define INSERT_PASSES_AFTER(PASS) \
1570 p = &(PASS);
1572 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1574 opt_pass **p = &(PASS ## _1)->sub;
1576 #define POP_INSERT_PASSES() \
1579 #define NEXT_PASS(PASS, NUM) \
1580 do { \
1581 gcc_assert (NULL == PASS ## _ ## NUM); \
1582 if ((NUM) == 1) \
1583 PASS ## _1 = make_##PASS (m_ctxt); \
1584 else \
1586 gcc_assert (PASS ## _1); \
1587 PASS ## _ ## NUM = PASS ## _1->clone (); \
1589 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1590 } while (0)
1592 #define TERMINATE_PASS_LIST() \
1593 *p = NULL;
1595 #include "pass-instances.def"
1597 #undef INSERT_PASSES_AFTER
1598 #undef PUSH_INSERT_PASSES_WITHIN
1599 #undef POP_INSERT_PASSES
1600 #undef NEXT_PASS
1601 #undef TERMINATE_PASS_LIST
1603 /* Register the passes with the tree dump code. */
1604 register_dump_files (all_lowering_passes);
1605 register_dump_files (all_small_ipa_passes);
1606 register_dump_files (all_regular_ipa_passes);
1607 register_dump_files (all_late_ipa_passes);
1608 register_dump_files (all_passes);
1611 static void
1612 delete_pass_tree (opt_pass *pass)
1614 while (pass)
1616 /* Recurse into child passes. */
1617 delete_pass_tree (pass->sub);
1619 opt_pass *next = pass->next;
1621 /* Delete this pass. */
1622 delete pass;
1624 /* Iterate onto sibling passes. */
1625 pass = next;
1629 pass_manager::~pass_manager ()
1631 XDELETEVEC (passes_by_id);
1633 /* Call delete_pass_tree on each of the pass_lists. */
1634 #define DEF_PASS_LIST(LIST) \
1635 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1636 GCC_PASS_LISTS
1637 #undef DEF_PASS_LIST
1641 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1642 function CALLBACK for every function in the call graph. Otherwise,
1643 call CALLBACK on the current function. */
1645 static void
1646 do_per_function (void (*callback) (function *, void *data), void *data)
1648 if (current_function_decl)
1649 callback (cfun, data);
1650 else
1652 struct cgraph_node *node;
1653 FOR_EACH_DEFINED_FUNCTION (node)
1654 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1655 && (!node->clone_of || node->decl != node->clone_of->decl))
1656 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1660 /* Because inlining might remove no-longer reachable nodes, we need to
1661 keep the array visible to garbage collector to avoid reading collected
1662 out nodes. */
1663 static int nnodes;
1664 static GTY ((length ("nnodes"))) cgraph_node **order;
1666 /* Hook called when NODE is removed and therefore should be
1667 excluded from order vector. DATA is an array of integers.
1668 DATA[0] holds max index it may be accessed by. For cgraph
1669 node DATA[node->uid + 1] holds index of this node in order
1670 vector. */
1671 static void
1672 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1674 int *order_idx = (int *)data;
1676 if (node->uid >= order_idx[0])
1677 return;
1679 int idx = order_idx[node->uid + 1];
1680 if (idx >= 0 && idx < nnodes && order[idx] == node)
1681 order[idx] = NULL;
1684 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1685 function CALLBACK for every function in the call graph. Otherwise,
1686 call CALLBACK on the current function.
1687 This function is global so that plugins can use it. */
1688 void
1689 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1691 int i;
1693 if (current_function_decl)
1694 callback (cfun, data);
1695 else
1697 cgraph_node_hook_list *hook;
1698 int *order_idx;
1699 gcc_assert (!order);
1700 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1702 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1703 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1704 order_idx[0] = symtab->cgraph_max_uid;
1706 nnodes = ipa_reverse_postorder (order);
1707 for (i = nnodes - 1; i >= 0; i--)
1709 order[i]->process = 1;
1710 order_idx[order[i]->uid + 1] = i;
1712 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1713 order_idx);
1714 for (i = nnodes - 1; i >= 0; i--)
1716 /* Function could be inlined and removed as unreachable. */
1717 if (!order[i])
1718 continue;
1720 struct cgraph_node *node = order[i];
1722 /* Allow possibly removed nodes to be garbage collected. */
1723 order[i] = NULL;
1724 node->process = 0;
1725 if (node->has_gimple_body_p ())
1726 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1728 symtab->remove_cgraph_removal_hook (hook);
1730 ggc_free (order);
1731 order = NULL;
1732 nnodes = 0;
1735 /* Helper function to perform function body dump. */
1737 static void
1738 execute_function_dump (function *fn, void *data)
1740 opt_pass *pass = (opt_pass *)data;
1742 if (dump_file)
1744 push_cfun (fn);
1746 if (fn->curr_properties & PROP_trees)
1747 dump_function_to_file (fn->decl, dump_file, dump_flags);
1748 else
1749 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1751 /* Flush the file. If verification fails, we won't be able to
1752 close the file before aborting. */
1753 fflush (dump_file);
1755 if ((fn->curr_properties & PROP_cfg)
1756 && (dump_flags & TDF_GRAPH))
1758 if (!pass->graph_dump_initialized)
1760 clean_graph_dump_file (dump_file_name);
1761 pass->graph_dump_initialized = true;
1763 print_graph_cfg (dump_file_name, fn);
1766 pop_cfun ();
1770 static struct profile_record *profile_record;
1772 /* Do profile consistency book-keeping for the pass with static number INDEX.
1773 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1774 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1775 if we are only book-keeping on passes that may have selectively disabled
1776 themselves on a given function. */
1777 static void
1778 check_profile_consistency (int index, int subpass, bool run)
1780 pass_manager *passes = g->get_passes ();
1781 if (index == -1)
1782 return;
1783 if (!profile_record)
1784 profile_record = XCNEWVEC (struct profile_record,
1785 passes->passes_by_id_size);
1786 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1787 gcc_assert (subpass < 2);
1788 profile_record[index].run |= run;
1789 account_profile_record (&profile_record[index], subpass);
1792 /* Output profile consistency. */
1794 void
1795 dump_profile_report (void)
1797 g->get_passes ()->dump_profile_report ();
1800 void
1801 pass_manager::dump_profile_report () const
1803 int i, j;
1804 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1805 gcov_type last_time = 0, last_size = 0;
1806 double rel_time_change, rel_size_change;
1807 int last_reported = 0;
1809 if (!profile_record)
1810 return;
1811 fprintf (stderr, "\nProfile consistency report:\n\n");
1812 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1813 fprintf (stderr, " |freq count |freq count |size time\n");
1815 for (i = 0; i < passes_by_id_size; i++)
1816 for (j = 0 ; j < 2; j++)
1817 if (profile_record[i].run)
1819 if (last_time)
1820 rel_time_change = (profile_record[i].time[j]
1821 - (double)last_time) * 100 / (double)last_time;
1822 else
1823 rel_time_change = 0;
1824 if (last_size)
1825 rel_size_change = (profile_record[i].size[j]
1826 - (double)last_size) * 100 / (double)last_size;
1827 else
1828 rel_size_change = 0;
1830 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1831 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1832 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1833 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1834 || rel_time_change || rel_size_change)
1836 last_reported = i;
1837 fprintf (stderr, "%-20s %s",
1838 passes_by_id [i]->name,
1839 j ? "(after TODO)" : " ");
1840 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1841 fprintf (stderr, "| %+5i",
1842 profile_record[i].num_mismatched_freq_in[j]
1843 - last_freq_in);
1844 else
1845 fprintf (stderr, "| ");
1846 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1847 fprintf (stderr, " %+5i",
1848 profile_record[i].num_mismatched_count_in[j]
1849 - last_count_in);
1850 else
1851 fprintf (stderr, " ");
1852 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1853 fprintf (stderr, "| %+5i",
1854 profile_record[i].num_mismatched_freq_out[j]
1855 - last_freq_out);
1856 else
1857 fprintf (stderr, "| ");
1858 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1859 fprintf (stderr, " %+5i",
1860 profile_record[i].num_mismatched_count_out[j]
1861 - last_count_out);
1862 else
1863 fprintf (stderr, " ");
1865 /* Size/time units change across gimple and RTL. */
1866 if (i == pass_expand_1->static_pass_number)
1867 fprintf (stderr, "|----------");
1868 else
1870 if (rel_size_change)
1871 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1872 else
1873 fprintf (stderr, "| ");
1874 if (rel_time_change)
1875 fprintf (stderr, " %+8.4f%%", rel_time_change);
1877 fprintf (stderr, "\n");
1878 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1879 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1880 last_count_in = profile_record[i].num_mismatched_count_in[j];
1881 last_count_out = profile_record[i].num_mismatched_count_out[j];
1883 else if (j && last_reported != i)
1885 last_reported = i;
1886 fprintf (stderr, "%-20s ------------| | |\n",
1887 passes_by_id [i]->name);
1889 last_time = profile_record[i].time[j];
1890 last_size = profile_record[i].size[j];
1894 /* Perform all TODO actions that ought to be done on each function. */
1896 static void
1897 execute_function_todo (function *fn, void *data)
1899 bool from_ipa_pass = (cfun == NULL);
1900 unsigned int flags = (size_t)data;
1901 flags &= ~fn->last_verified;
1902 if (!flags)
1903 return;
1905 push_cfun (fn);
1907 /* Always cleanup the CFG before trying to update SSA. */
1908 if (flags & TODO_cleanup_cfg)
1910 cleanup_tree_cfg ();
1912 /* When cleanup_tree_cfg merges consecutive blocks, it may
1913 perform some simplistic propagation when removing single
1914 valued PHI nodes. This propagation may, in turn, cause the
1915 SSA form to become out-of-date (see PR 22037). So, even
1916 if the parent pass had not scheduled an SSA update, we may
1917 still need to do one. */
1918 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1919 flags |= TODO_update_ssa;
1922 if (flags & TODO_update_ssa_any)
1924 unsigned update_flags = flags & TODO_update_ssa_any;
1925 update_ssa (update_flags);
1928 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1929 compute_may_aliases ();
1931 if (optimize && (flags & TODO_update_address_taken))
1932 execute_update_addresses_taken ();
1934 if (flags & TODO_remove_unused_locals)
1935 remove_unused_locals ();
1937 if (flags & TODO_rebuild_frequencies)
1938 rebuild_frequencies ();
1940 if (flags & TODO_rebuild_cgraph_edges)
1941 cgraph_edge::rebuild_edges ();
1943 /* If we've seen errors do not bother running any verifiers. */
1944 if (!seen_error ())
1946 #if defined ENABLE_CHECKING
1947 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1948 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1950 if (flags & TODO_verify_il)
1952 if (cfun->curr_properties & PROP_trees)
1954 if (cfun->curr_properties & PROP_cfg)
1955 /* IPA passes leave stmts to be fixed up, so make sure to
1956 not verify stmts really throw. */
1957 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1958 else
1959 verify_gimple_in_seq (gimple_body (cfun->decl));
1961 if (cfun->curr_properties & PROP_ssa)
1962 /* IPA passes leave stmts to be fixed up, so make sure to
1963 not verify SSA operands whose verifier will choke on that. */
1964 verify_ssa (true, !from_ipa_pass);
1965 /* IPA passes leave basic-blocks unsplit, so make sure to
1966 not trip on that. */
1967 if ((cfun->curr_properties & PROP_cfg)
1968 && !from_ipa_pass)
1969 verify_flow_info ();
1970 if (current_loops
1971 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1972 verify_loop_closed_ssa (false);
1973 if (cfun->curr_properties & PROP_rtl)
1974 verify_rtl_sharing ();
1977 /* Make sure verifiers don't change dominator state. */
1978 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1979 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1980 #endif
1983 fn->last_verified = flags & TODO_verify_all;
1985 pop_cfun ();
1987 /* For IPA passes make sure to release dominator info, it can be
1988 computed by non-verifying TODOs. */
1989 if (from_ipa_pass)
1991 free_dominance_info (fn, CDI_DOMINATORS);
1992 free_dominance_info (fn, CDI_POST_DOMINATORS);
1996 /* Perform all TODO actions. */
1997 static void
1998 execute_todo (unsigned int flags)
2000 #if defined ENABLE_CHECKING
2001 if (cfun
2002 && need_ssa_update_p (cfun))
2003 gcc_assert (flags & TODO_update_ssa_any);
2004 #endif
2006 timevar_push (TV_TODO);
2008 /* Inform the pass whether it is the first time it is run. */
2009 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2011 statistics_fini_pass ();
2013 if (flags)
2014 do_per_function (execute_function_todo, (void *)(size_t) flags);
2016 /* Always remove functions just as before inlining: IPA passes might be
2017 interested to see bodies of extern inline functions that are not inlined
2018 to analyze side effects. The full removal is done just at the end
2019 of IPA pass queue. */
2020 if (flags & TODO_remove_functions)
2022 gcc_assert (!cfun);
2023 symtab->remove_unreachable_nodes (dump_file);
2026 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2028 gcc_assert (!cfun);
2029 symtab_node::dump_table (dump_file);
2030 /* Flush the file. If verification fails, we won't be able to
2031 close the file before aborting. */
2032 fflush (dump_file);
2035 /* Now that the dumping has been done, we can get rid of the optional
2036 df problems. */
2037 if (flags & TODO_df_finish)
2038 df_finish_pass ((flags & TODO_df_verify) != 0);
2040 timevar_pop (TV_TODO);
2043 /* Verify invariants that should hold between passes. This is a place
2044 to put simple sanity checks. */
2046 static void
2047 verify_interpass_invariants (void)
2049 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2052 /* Clear the last verified flag. */
2054 static void
2055 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2057 fn->last_verified = 0;
2060 /* Helper function. Verify that the properties has been turn into the
2061 properties expected by the pass. */
2063 #ifdef ENABLE_CHECKING
2064 static void
2065 verify_curr_properties (function *fn, void *data)
2067 unsigned int props = (size_t)data;
2068 gcc_assert ((fn->curr_properties & props) == props);
2070 #endif
2072 /* Initialize pass dump file. */
2073 /* This is non-static so that the plugins can use it. */
2075 bool
2076 pass_init_dump_file (opt_pass *pass)
2078 /* If a dump file name is present, open it if enabled. */
2079 if (pass->static_pass_number != -1)
2081 timevar_push (TV_DUMP);
2082 gcc::dump_manager *dumps = g->get_dumps ();
2083 bool initializing_dump =
2084 !dumps->dump_initialized_p (pass->static_pass_number);
2085 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2086 dumps->dump_start (pass->static_pass_number, &dump_flags);
2087 if (dump_file && current_function_decl)
2088 dump_function_header (dump_file, current_function_decl, dump_flags);
2089 if (initializing_dump
2090 && dump_file && (dump_flags & TDF_GRAPH)
2091 && cfun && (cfun->curr_properties & PROP_cfg))
2093 clean_graph_dump_file (dump_file_name);
2094 pass->graph_dump_initialized = true;
2096 timevar_pop (TV_DUMP);
2097 return initializing_dump;
2099 else
2100 return false;
2103 /* Flush PASS dump file. */
2104 /* This is non-static so that plugins can use it. */
2106 void
2107 pass_fini_dump_file (opt_pass *pass)
2109 timevar_push (TV_DUMP);
2111 /* Flush and close dump file. */
2112 if (dump_file_name)
2114 free (CONST_CAST (char *, dump_file_name));
2115 dump_file_name = NULL;
2118 g->get_dumps ()->dump_finish (pass->static_pass_number);
2119 timevar_pop (TV_DUMP);
2122 /* After executing the pass, apply expected changes to the function
2123 properties. */
2125 static void
2126 update_properties_after_pass (function *fn, void *data)
2128 opt_pass *pass = (opt_pass *) data;
2129 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2130 & ~pass->properties_destroyed;
2133 /* Execute summary generation for all of the passes in IPA_PASS. */
2135 void
2136 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2138 while (ipa_pass)
2140 opt_pass *pass = ipa_pass;
2142 /* Execute all of the IPA_PASSes in the list. */
2143 if (ipa_pass->type == IPA_PASS
2144 && pass->gate (cfun)
2145 && ipa_pass->generate_summary)
2147 pass_init_dump_file (pass);
2149 /* If a timevar is present, start it. */
2150 if (pass->tv_id)
2151 timevar_push (pass->tv_id);
2153 current_pass = pass;
2154 ipa_pass->generate_summary ();
2156 /* Stop timevar. */
2157 if (pass->tv_id)
2158 timevar_pop (pass->tv_id);
2160 pass_fini_dump_file (pass);
2162 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2166 /* Execute IPA_PASS function transform on NODE. */
2168 static void
2169 execute_one_ipa_transform_pass (struct cgraph_node *node,
2170 ipa_opt_pass_d *ipa_pass)
2172 opt_pass *pass = ipa_pass;
2173 unsigned int todo_after = 0;
2175 current_pass = pass;
2176 if (!ipa_pass->function_transform)
2177 return;
2179 /* Note that the folders should only create gimple expressions.
2180 This is a hack until the new folder is ready. */
2181 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2183 pass_init_dump_file (pass);
2185 /* Run pre-pass verification. */
2186 execute_todo (ipa_pass->function_transform_todo_flags_start);
2188 /* If a timevar is present, start it. */
2189 if (pass->tv_id != TV_NONE)
2190 timevar_push (pass->tv_id);
2192 /* Do it! */
2193 todo_after = ipa_pass->function_transform (node);
2195 /* Stop timevar. */
2196 if (pass->tv_id != TV_NONE)
2197 timevar_pop (pass->tv_id);
2199 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2200 check_profile_consistency (pass->static_pass_number, 0, true);
2202 /* Run post-pass cleanup and verification. */
2203 execute_todo (todo_after);
2204 verify_interpass_invariants ();
2205 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2206 check_profile_consistency (pass->static_pass_number, 1, true);
2208 if (dump_file)
2209 do_per_function (execute_function_dump, NULL);
2210 pass_fini_dump_file (pass);
2212 current_pass = NULL;
2214 /* Signal this is a suitable GC collection point. */
2215 if (!(todo_after & TODO_do_not_ggc_collect))
2216 ggc_collect ();
2219 /* For the current function, execute all ipa transforms. */
2221 void
2222 execute_all_ipa_transforms (void)
2224 struct cgraph_node *node;
2225 if (!cfun)
2226 return;
2227 node = cgraph_node::get (current_function_decl);
2229 if (node->ipa_transforms_to_apply.exists ())
2231 unsigned int i;
2233 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2234 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2235 node->ipa_transforms_to_apply.release ();
2239 /* Check if PASS is explicitly disabled or enabled and return
2240 the gate status. FUNC is the function to be processed, and
2241 GATE_STATUS is the gate status determined by pass manager by
2242 default. */
2244 static bool
2245 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2247 bool explicitly_enabled = false;
2248 bool explicitly_disabled = false;
2250 explicitly_enabled
2251 = is_pass_explicitly_enabled_or_disabled (pass, func,
2252 enabled_pass_uid_range_tab);
2253 explicitly_disabled
2254 = is_pass_explicitly_enabled_or_disabled (pass, func,
2255 disabled_pass_uid_range_tab);
2257 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2259 return gate_status;
2263 /* Execute PASS. */
2265 bool
2266 execute_one_pass (opt_pass *pass)
2268 unsigned int todo_after = 0;
2270 bool gate_status;
2272 /* IPA passes are executed on whole program, so cfun should be NULL.
2273 Other passes need function context set. */
2274 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2275 gcc_assert (!cfun && !current_function_decl);
2276 else
2277 gcc_assert (cfun && current_function_decl);
2279 current_pass = pass;
2281 /* Check whether gate check should be avoided.
2282 User controls the value of the gate through the parameter "gate_status". */
2283 gate_status = pass->gate (cfun);
2284 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2286 /* Override gate with plugin. */
2287 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2289 if (!gate_status)
2291 /* Run so passes selectively disabling themselves on a given function
2292 are not miscounted. */
2293 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2295 check_profile_consistency (pass->static_pass_number, 0, false);
2296 check_profile_consistency (pass->static_pass_number, 1, false);
2298 current_pass = NULL;
2299 return false;
2302 /* Pass execution event trigger: useful to identify passes being
2303 executed. */
2304 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2306 if (!quiet_flag && !cfun)
2307 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2309 /* Note that the folders should only create gimple expressions.
2310 This is a hack until the new folder is ready. */
2311 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2313 pass_init_dump_file (pass);
2315 /* Run pre-pass verification. */
2316 execute_todo (pass->todo_flags_start);
2318 #ifdef ENABLE_CHECKING
2319 do_per_function (verify_curr_properties,
2320 (void *)(size_t)pass->properties_required);
2321 #endif
2323 /* If a timevar is present, start it. */
2324 if (pass->tv_id != TV_NONE)
2325 timevar_push (pass->tv_id);
2327 /* Do it! */
2328 todo_after = pass->execute (cfun);
2329 do_per_function (clear_last_verified, NULL);
2331 /* Stop timevar. */
2332 if (pass->tv_id != TV_NONE)
2333 timevar_pop (pass->tv_id);
2335 do_per_function (update_properties_after_pass, pass);
2337 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2338 check_profile_consistency (pass->static_pass_number, 0, true);
2340 /* Run post-pass cleanup and verification. */
2341 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2342 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2343 check_profile_consistency (pass->static_pass_number, 1, true);
2345 verify_interpass_invariants ();
2346 if (dump_file)
2347 do_per_function (execute_function_dump, pass);
2348 if (pass->type == IPA_PASS)
2350 struct cgraph_node *node;
2351 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2352 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2355 if (!current_function_decl)
2356 symtab->process_new_functions ();
2358 pass_fini_dump_file (pass);
2360 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2361 gcc_assert (!(cfun->curr_properties & PROP_trees)
2362 || pass->type != RTL_PASS);
2364 current_pass = NULL;
2366 /* Signal this is a suitable GC collection point. */
2367 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2368 ggc_collect ();
2370 return true;
2373 static void
2374 execute_pass_list_1 (opt_pass *pass)
2378 gcc_assert (pass->type == GIMPLE_PASS
2379 || pass->type == RTL_PASS);
2380 if (execute_one_pass (pass) && pass->sub)
2381 execute_pass_list_1 (pass->sub);
2382 pass = pass->next;
2384 while (pass);
2387 void
2388 execute_pass_list (function *fn, opt_pass *pass)
2390 push_cfun (fn);
2391 execute_pass_list_1 (pass);
2392 if (fn->cfg)
2394 free_dominance_info (CDI_DOMINATORS);
2395 free_dominance_info (CDI_POST_DOMINATORS);
2397 pop_cfun ();
2400 /* Write out all LTO data. */
2401 static void
2402 write_lto (void)
2404 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2405 lto_output ();
2406 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2407 timevar_push (TV_IPA_LTO_DECL_OUT);
2408 produce_asm_for_decls ();
2409 timevar_pop (TV_IPA_LTO_DECL_OUT);
2412 /* Same as execute_pass_list but assume that subpasses of IPA passes
2413 are local passes. If SET is not NULL, write out summaries of only
2414 those node in SET. */
2416 static void
2417 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2419 while (pass)
2421 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2422 gcc_assert (!current_function_decl);
2423 gcc_assert (!cfun);
2424 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2425 if (pass->type == IPA_PASS
2426 && ipa_pass->write_summary
2427 && pass->gate (cfun))
2429 /* If a timevar is present, start it. */
2430 if (pass->tv_id)
2431 timevar_push (pass->tv_id);
2433 pass_init_dump_file (pass);
2435 current_pass = pass;
2436 ipa_pass->write_summary ();
2438 pass_fini_dump_file (pass);
2440 /* If a timevar is present, start it. */
2441 if (pass->tv_id)
2442 timevar_pop (pass->tv_id);
2445 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2446 ipa_write_summaries_2 (pass->sub, state);
2448 pass = pass->next;
2452 /* Helper function of ipa_write_summaries. Creates and destroys the
2453 decl state and calls ipa_write_summaries_2 for all passes that have
2454 summaries. SET is the set of nodes to be written. */
2456 static void
2457 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2459 pass_manager *passes = g->get_passes ();
2460 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2461 state->symtab_node_encoder = encoder;
2463 lto_output_init_mode_table ();
2464 lto_push_out_decl_state (state);
2466 gcc_assert (!flag_wpa);
2467 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2469 write_lto ();
2471 gcc_assert (lto_get_out_decl_state () == state);
2472 lto_pop_out_decl_state ();
2473 lto_delete_out_decl_state (state);
2476 /* Write out summaries for all the nodes in the callgraph. */
2478 void
2479 ipa_write_summaries (void)
2481 lto_symtab_encoder_t encoder;
2482 int i, order_pos;
2483 varpool_node *vnode;
2484 struct cgraph_node *node;
2485 struct cgraph_node **order;
2487 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2488 return;
2490 select_what_to_stream ();
2492 encoder = lto_symtab_encoder_new (false);
2494 /* Create the callgraph set in the same order used in
2495 cgraph_expand_all_functions. This mostly facilitates debugging,
2496 since it causes the gimple file to be processed in the same order
2497 as the source code. */
2498 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2499 order_pos = ipa_reverse_postorder (order);
2500 gcc_assert (order_pos == symtab->cgraph_count);
2502 for (i = order_pos - 1; i >= 0; i--)
2504 struct cgraph_node *node = order[i];
2506 if (node->has_gimple_body_p ())
2508 /* When streaming out references to statements as part of some IPA
2509 pass summary, the statements need to have uids assigned and the
2510 following does that for all the IPA passes here. Naturally, this
2511 ordering then matches the one IPA-passes get in their stmt_fixup
2512 hooks. */
2514 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2515 renumber_gimple_stmt_uids ();
2516 pop_cfun ();
2518 if (node->definition && node->need_lto_streaming)
2519 lto_set_symtab_encoder_in_partition (encoder, node);
2522 FOR_EACH_DEFINED_FUNCTION (node)
2523 if (node->alias && node->need_lto_streaming)
2524 lto_set_symtab_encoder_in_partition (encoder, node);
2525 FOR_EACH_DEFINED_VARIABLE (vnode)
2526 if (vnode->need_lto_streaming)
2527 lto_set_symtab_encoder_in_partition (encoder, vnode);
2529 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2531 free (order);
2534 /* Same as execute_pass_list but assume that subpasses of IPA passes
2535 are local passes. If SET is not NULL, write out optimization summaries of
2536 only those node in SET. */
2538 static void
2539 ipa_write_optimization_summaries_1 (opt_pass *pass,
2540 struct lto_out_decl_state *state)
2542 while (pass)
2544 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2545 gcc_assert (!current_function_decl);
2546 gcc_assert (!cfun);
2547 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2548 if (pass->type == IPA_PASS
2549 && ipa_pass->write_optimization_summary
2550 && pass->gate (cfun))
2552 /* If a timevar is present, start it. */
2553 if (pass->tv_id)
2554 timevar_push (pass->tv_id);
2556 pass_init_dump_file (pass);
2558 current_pass = pass;
2559 ipa_pass->write_optimization_summary ();
2561 pass_fini_dump_file (pass);
2563 /* If a timevar is present, start it. */
2564 if (pass->tv_id)
2565 timevar_pop (pass->tv_id);
2568 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2569 ipa_write_optimization_summaries_1 (pass->sub, state);
2571 pass = pass->next;
2575 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2576 NULL, write out all summaries of all nodes. */
2578 void
2579 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2581 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2582 lto_symtab_encoder_iterator lsei;
2583 state->symtab_node_encoder = encoder;
2585 lto_output_init_mode_table ();
2586 lto_push_out_decl_state (state);
2587 for (lsei = lsei_start_function_in_partition (encoder);
2588 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2590 struct cgraph_node *node = lsei_cgraph_node (lsei);
2591 /* When streaming out references to statements as part of some IPA
2592 pass summary, the statements need to have uids assigned.
2594 For functions newly born at WPA stage we need to initialize
2595 the uids here. */
2596 if (node->definition
2597 && gimple_has_body_p (node->decl))
2599 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2600 renumber_gimple_stmt_uids ();
2601 pop_cfun ();
2605 gcc_assert (flag_wpa);
2606 pass_manager *passes = g->get_passes ();
2607 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2609 write_lto ();
2611 gcc_assert (lto_get_out_decl_state () == state);
2612 lto_pop_out_decl_state ();
2613 lto_delete_out_decl_state (state);
2616 /* Same as execute_pass_list but assume that subpasses of IPA passes
2617 are local passes. */
2619 static void
2620 ipa_read_summaries_1 (opt_pass *pass)
2622 while (pass)
2624 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2626 gcc_assert (!current_function_decl);
2627 gcc_assert (!cfun);
2628 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2630 if (pass->gate (cfun))
2632 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2634 /* If a timevar is present, start it. */
2635 if (pass->tv_id)
2636 timevar_push (pass->tv_id);
2638 pass_init_dump_file (pass);
2640 current_pass = pass;
2641 ipa_pass->read_summary ();
2643 pass_fini_dump_file (pass);
2645 /* Stop timevar. */
2646 if (pass->tv_id)
2647 timevar_pop (pass->tv_id);
2650 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2651 ipa_read_summaries_1 (pass->sub);
2653 pass = pass->next;
2658 /* Read all the summaries for all_regular_ipa_passes. */
2660 void
2661 ipa_read_summaries (void)
2663 pass_manager *passes = g->get_passes ();
2664 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2667 /* Same as execute_pass_list but assume that subpasses of IPA passes
2668 are local passes. */
2670 static void
2671 ipa_read_optimization_summaries_1 (opt_pass *pass)
2673 while (pass)
2675 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2677 gcc_assert (!current_function_decl);
2678 gcc_assert (!cfun);
2679 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2681 if (pass->gate (cfun))
2683 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2685 /* If a timevar is present, start it. */
2686 if (pass->tv_id)
2687 timevar_push (pass->tv_id);
2689 pass_init_dump_file (pass);
2691 current_pass = pass;
2692 ipa_pass->read_optimization_summary ();
2694 pass_fini_dump_file (pass);
2696 /* Stop timevar. */
2697 if (pass->tv_id)
2698 timevar_pop (pass->tv_id);
2701 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2702 ipa_read_optimization_summaries_1 (pass->sub);
2704 pass = pass->next;
2708 /* Read all the summaries for all_regular_ipa_passes. */
2710 void
2711 ipa_read_optimization_summaries (void)
2713 pass_manager *passes = g->get_passes ();
2714 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2717 /* Same as execute_pass_list but assume that subpasses of IPA passes
2718 are local passes. */
2719 void
2720 execute_ipa_pass_list (opt_pass *pass)
2724 gcc_assert (!current_function_decl);
2725 gcc_assert (!cfun);
2726 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2727 if (execute_one_pass (pass) && pass->sub)
2729 if (pass->sub->type == GIMPLE_PASS)
2731 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2732 do_per_function_toporder ((void (*)(function *, void *))
2733 execute_pass_list,
2734 pass->sub);
2735 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2737 else if (pass->sub->type == SIMPLE_IPA_PASS
2738 || pass->sub->type == IPA_PASS)
2739 execute_ipa_pass_list (pass->sub);
2740 else
2741 gcc_unreachable ();
2743 gcc_assert (!current_function_decl);
2744 symtab->process_new_functions ();
2745 pass = pass->next;
2747 while (pass);
2750 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2752 static void
2753 execute_ipa_stmt_fixups (opt_pass *pass,
2754 struct cgraph_node *node, gimple *stmts)
2756 while (pass)
2758 /* Execute all of the IPA_PASSes in the list. */
2759 if (pass->type == IPA_PASS
2760 && pass->gate (cfun))
2762 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2764 if (ipa_pass->stmt_fixup)
2766 pass_init_dump_file (pass);
2767 /* If a timevar is present, start it. */
2768 if (pass->tv_id)
2769 timevar_push (pass->tv_id);
2771 current_pass = pass;
2772 ipa_pass->stmt_fixup (node, stmts);
2774 /* Stop timevar. */
2775 if (pass->tv_id)
2776 timevar_pop (pass->tv_id);
2777 pass_fini_dump_file (pass);
2779 if (pass->sub)
2780 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2782 pass = pass->next;
2786 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2788 void
2789 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2791 pass_manager *passes = g->get_passes ();
2792 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2796 extern void debug_properties (unsigned int);
2797 extern void dump_properties (FILE *, unsigned int);
2799 DEBUG_FUNCTION void
2800 dump_properties (FILE *dump, unsigned int props)
2802 fprintf (dump, "Properties:\n");
2803 if (props & PROP_gimple_any)
2804 fprintf (dump, "PROP_gimple_any\n");
2805 if (props & PROP_gimple_lcf)
2806 fprintf (dump, "PROP_gimple_lcf\n");
2807 if (props & PROP_gimple_leh)
2808 fprintf (dump, "PROP_gimple_leh\n");
2809 if (props & PROP_cfg)
2810 fprintf (dump, "PROP_cfg\n");
2811 if (props & PROP_ssa)
2812 fprintf (dump, "PROP_ssa\n");
2813 if (props & PROP_no_crit_edges)
2814 fprintf (dump, "PROP_no_crit_edges\n");
2815 if (props & PROP_rtl)
2816 fprintf (dump, "PROP_rtl\n");
2817 if (props & PROP_gimple_lomp)
2818 fprintf (dump, "PROP_gimple_lomp\n");
2819 if (props & PROP_gimple_lcx)
2820 fprintf (dump, "PROP_gimple_lcx\n");
2821 if (props & PROP_gimple_lvec)
2822 fprintf (dump, "PROP_gimple_lvec\n");
2823 if (props & PROP_cfglayout)
2824 fprintf (dump, "PROP_cfglayout\n");
2827 DEBUG_FUNCTION void
2828 debug_properties (unsigned int props)
2830 dump_properties (stderr, props);
2833 /* Called by local passes to see if function is called by already processed nodes.
2834 Because we process nodes in topological order, this means that function is
2835 in recursive cycle or we introduced new direct calls. */
2836 bool
2837 function_called_by_processed_nodes_p (void)
2839 struct cgraph_edge *e;
2840 for (e = cgraph_node::get (current_function_decl)->callers;
2842 e = e->next_caller)
2844 if (e->caller->decl == current_function_decl)
2845 continue;
2846 if (!e->caller->has_gimple_body_p ())
2847 continue;
2848 if (TREE_ASM_WRITTEN (e->caller->decl))
2849 continue;
2850 if (!e->caller->process && !e->caller->global.inlined_to)
2851 break;
2853 if (dump_file && e)
2855 fprintf (dump_file, "Already processed call to:\n");
2856 e->caller->dump (dump_file);
2858 return e != NULL;
2861 #include "gt-passes.h"