diagnostic.c (get_terminal_width): Renamed from
[official-gcc.git] / gcc / passes.c
blob74b40e5d80b3ea80ab18d8825d830c54d01b2068
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2014 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 "tree.h"
32 #include "varasm.h"
33 #include "rtl.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "insn-attr.h"
37 #include "insn-config.h"
38 #include "insn-flags.h"
39 #include "hard-reg-set.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "except.h"
43 #include "hashtab.h"
44 #include "hash-set.h"
45 #include "vec.h"
46 #include "machmode.h"
47 #include "function.h"
48 #include "toplev.h"
49 #include "expr.h"
50 #include "predict.h"
51 #include "basic-block.h"
52 #include "intl.h"
53 #include "graph.h"
54 #include "regs.h"
55 #include "diagnostic-core.h"
56 #include "params.h"
57 #include "reload.h"
58 #include "debug.h"
59 #include "target.h"
60 #include "langhooks.h"
61 #include "cfgloop.h"
62 #include "hosthooks.h"
63 #include "opts.h"
64 #include "coverage.h"
65 #include "value-prof.h"
66 #include "tree-inline.h"
67 #include "tree-ssa-alias.h"
68 #include "internal-fn.h"
69 #include "gimple-expr.h"
70 #include "is-a.h"
71 #include "gimple.h"
72 #include "gimple-ssa.h"
73 #include "tree-cfg.h"
74 #include "stringpool.h"
75 #include "tree-ssanames.h"
76 #include "tree-ssa-loop-manip.h"
77 #include "tree-into-ssa.h"
78 #include "tree-dfa.h"
79 #include "tree-ssa.h"
80 #include "tree-pass.h"
81 #include "tree-dump.h"
82 #include "df.h"
83 #include "hash-map.h"
84 #include "plugin-api.h"
85 #include "ipa-ref.h"
86 #include "cgraph.h"
87 #include "lto-streamer.h"
88 #include "plugin.h"
89 #include "ipa-utils.h"
90 #include "tree-pretty-print.h" /* for dump_function_header */
91 #include "context.h"
92 #include "pass_manager.h"
93 #include "tree-ssa-live.h" /* For remove_unused_locals. */
94 #include "tree-cfgcleanup.h"
96 using namespace gcc;
98 /* This is used for debugging. It allows the current pass to printed
99 from anywhere in compilation.
100 The variable current_pass is also used for statistics and plugins. */
101 opt_pass *current_pass;
103 static void register_pass_name (opt_pass *, const char *);
105 /* Most passes are single-instance (within their context) and thus don't
106 need to implement cloning, but passes that support multiple instances
107 *must* provide their own implementation of the clone method.
109 Handle this by providing a default implemenation, but make it a fatal
110 error to call it. */
112 opt_pass *
113 opt_pass::clone ()
115 internal_error ("pass %s does not support cloning", name);
118 bool
119 opt_pass::gate (function *)
121 return true;
124 unsigned int
125 opt_pass::execute (function *)
127 return 0;
130 opt_pass::opt_pass (const pass_data &data, context *ctxt)
131 : pass_data (data),
132 sub (NULL),
133 next (NULL),
134 static_pass_number (0),
135 m_ctxt (ctxt)
140 void
141 pass_manager::execute_early_local_passes ()
143 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
144 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
145 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
148 unsigned int
149 pass_manager::execute_pass_mode_switching ()
151 return pass_mode_switching_1->execute (cfun);
155 /* Call from anywhere to find out what pass this is. Useful for
156 printing out debugging information deep inside an service
157 routine. */
158 void
159 print_current_pass (FILE *file)
161 if (current_pass)
162 fprintf (file, "current pass = %s (%d)\n",
163 current_pass->name, current_pass->static_pass_number);
164 else
165 fprintf (file, "no current pass.\n");
169 /* Call from the debugger to get the current pass name. */
170 DEBUG_FUNCTION void
171 debug_pass (void)
173 print_current_pass (stderr);
178 /* Global variables used to communicate with passes. */
179 bool in_gimple_form;
180 bool first_pass_instance;
183 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
184 and TYPE_DECL nodes.
186 This does nothing for local (non-static) variables, unless the
187 variable is a register variable with DECL_ASSEMBLER_NAME set. In
188 that case, or if the variable is not an automatic, it sets up the
189 RTL and outputs any assembler code (label definition, storage
190 allocation and initialization).
192 DECL is the declaration. TOP_LEVEL is nonzero
193 if this declaration is not within a function. */
195 void
196 rest_of_decl_compilation (tree decl,
197 int top_level,
198 int at_end)
200 bool finalize = true;
202 /* We deferred calling assemble_alias so that we could collect
203 other attributes such as visibility. Emit the alias now. */
204 if (!in_lto_p)
206 tree alias;
207 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
208 if (alias)
210 alias = TREE_VALUE (TREE_VALUE (alias));
211 alias = get_identifier (TREE_STRING_POINTER (alias));
212 /* A quirk of the initial implementation of aliases required that the
213 user add "extern" to all of them. Which is silly, but now
214 historical. Do note that the symbol is in fact locally defined. */
215 DECL_EXTERNAL (decl) = 0;
216 TREE_STATIC (decl) = 1;
217 assemble_alias (decl, alias);
218 finalize = false;
222 /* Can't defer this, because it needs to happen before any
223 later function definitions are processed. */
224 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
225 make_decl_rtl (decl);
227 /* Forward declarations for nested functions are not "external",
228 but we need to treat them as if they were. */
229 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
230 || TREE_CODE (decl) == FUNCTION_DECL)
232 timevar_push (TV_VARCONST);
234 /* Don't output anything when a tentative file-scope definition
235 is seen. But at end of compilation, do output code for them.
237 We do output all variables and rely on
238 callgraph code to defer them except for forward declarations
239 (see gcc.c-torture/compile/920624-1.c) */
240 if ((at_end
241 || !DECL_DEFER_OUTPUT (decl)
242 || DECL_INITIAL (decl))
243 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
244 && !DECL_EXTERNAL (decl))
246 /* When reading LTO unit, we also read varpool, so do not
247 rebuild it. */
248 if (in_lto_p && !at_end)
250 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
251 varpool_node::finalize_decl (decl);
254 #ifdef ASM_FINISH_DECLARE_OBJECT
255 if (decl == last_assemble_variable_decl)
257 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
258 top_level, at_end);
260 #endif
262 timevar_pop (TV_VARCONST);
264 else if (TREE_CODE (decl) == TYPE_DECL
265 /* Like in rest_of_type_compilation, avoid confusing the debug
266 information machinery when there are errors. */
267 && !seen_error ())
269 timevar_push (TV_SYMOUT);
270 debug_hooks->type_decl (decl, !top_level);
271 timevar_pop (TV_SYMOUT);
274 /* Let cgraph know about the existence of variables. */
275 if (in_lto_p && !at_end)
277 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
278 && TREE_STATIC (decl))
279 varpool_node::get_create (decl);
282 /* Called after finishing a record, union or enumeral type. */
284 void
285 rest_of_type_compilation (tree type, int toplev)
287 /* Avoid confusing the debug information machinery when there are
288 errors. */
289 if (seen_error ())
290 return;
292 timevar_push (TV_SYMOUT);
293 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
294 timevar_pop (TV_SYMOUT);
299 void
300 pass_manager::
301 finish_optimization_passes (void)
303 int i;
304 struct dump_file_info *dfi;
305 char *name;
306 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
308 timevar_push (TV_DUMP);
309 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
311 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
312 end_branch_prob ();
313 dumps->dump_finish (pass_profile_1->static_pass_number);
316 if (optimize > 0)
318 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
319 print_combine_total_stats ();
320 dumps->dump_finish (pass_profile_1->static_pass_number);
323 /* Do whatever is necessary to finish printing the graphs. */
324 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
325 if (dumps->dump_initialized_p (i)
326 && (dfi->pflags & TDF_GRAPH) != 0
327 && (name = dumps->get_dump_file_name (i)) != NULL)
329 finish_graph_dump_file (name);
330 free (name);
333 timevar_pop (TV_DUMP);
336 static unsigned int
337 execute_build_ssa_passes (void)
339 /* Once this pass (and its sub-passes) are complete, all functions
340 will be in SSA form. Technically this state change is happening
341 a tad early, since the sub-passes have not yet run, but since
342 none of the sub-passes are IPA passes and do not create new
343 functions, this is ok. We're setting this value for the benefit
344 of IPA passes that follow. */
345 if (symtab->state < IPA_SSA)
346 symtab->state = IPA_SSA;
347 return 0;
350 namespace {
352 const pass_data pass_data_build_ssa_passes =
354 SIMPLE_IPA_PASS, /* type */
355 "build_ssa_passes", /* name */
356 OPTGROUP_NONE, /* optinfo_flags */
357 TV_EARLY_LOCAL, /* tv_id */
358 0, /* properties_required */
359 0, /* properties_provided */
360 0, /* properties_destroyed */
361 0, /* todo_flags_start */
362 /* todo_flags_finish is executed before subpases. For this reason
363 it makes no sense to remove unreachable functions here. */
364 0, /* todo_flags_finish */
367 class pass_build_ssa_passes : public simple_ipa_opt_pass
369 public:
370 pass_build_ssa_passes (gcc::context *ctxt)
371 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
374 /* opt_pass methods: */
375 virtual bool gate (function *)
377 /* Don't bother doing anything if the program has errors. */
378 return (!seen_error () && !in_lto_p);
381 virtual unsigned int execute (function *)
383 return execute_build_ssa_passes ();
386 }; // class pass_build_ssa_passes
388 const pass_data pass_data_chkp_instrumentation_passes =
390 SIMPLE_IPA_PASS, /* type */
391 "chkp_passes", /* name */
392 OPTGROUP_NONE, /* optinfo_flags */
393 TV_NONE, /* tv_id */
394 0, /* properties_required */
395 0, /* properties_provided */
396 0, /* properties_destroyed */
397 0, /* todo_flags_start */
398 0, /* todo_flags_finish */
401 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
403 public:
404 pass_chkp_instrumentation_passes (gcc::context *ctxt)
405 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
408 /* opt_pass methods: */
409 virtual bool gate (function *)
411 /* Don't bother doing anything if the program has errors. */
412 return (!seen_error () && !in_lto_p);
415 }; // class pass_chkp_instrumentation_passes
417 const pass_data pass_data_local_optimization_passes =
419 SIMPLE_IPA_PASS, /* type */
420 "opt_local_passes", /* name */
421 OPTGROUP_NONE, /* optinfo_flags */
422 TV_NONE, /* tv_id */
423 0, /* properties_required */
424 0, /* properties_provided */
425 0, /* properties_destroyed */
426 0, /* todo_flags_start */
427 0, /* todo_flags_finish */
430 class pass_local_optimization_passes : public simple_ipa_opt_pass
432 public:
433 pass_local_optimization_passes (gcc::context *ctxt)
434 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
437 /* opt_pass methods: */
438 virtual bool gate (function *)
440 /* Don't bother doing anything if the program has errors. */
441 return (!seen_error () && !in_lto_p);
444 }; // class pass_local_optimization_passes
446 } // anon namespace
448 simple_ipa_opt_pass *
449 make_pass_build_ssa_passes (gcc::context *ctxt)
451 return new pass_build_ssa_passes (ctxt);
454 simple_ipa_opt_pass *
455 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
457 return new pass_chkp_instrumentation_passes (ctxt);
460 simple_ipa_opt_pass *
461 make_pass_local_optimization_passes (gcc::context *ctxt)
463 return new pass_local_optimization_passes (ctxt);
466 namespace {
468 const pass_data pass_data_all_early_optimizations =
470 GIMPLE_PASS, /* type */
471 "early_optimizations", /* name */
472 OPTGROUP_NONE, /* optinfo_flags */
473 TV_NONE, /* tv_id */
474 0, /* properties_required */
475 0, /* properties_provided */
476 0, /* properties_destroyed */
477 0, /* todo_flags_start */
478 0, /* todo_flags_finish */
481 class pass_all_early_optimizations : public gimple_opt_pass
483 public:
484 pass_all_early_optimizations (gcc::context *ctxt)
485 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
488 /* opt_pass methods: */
489 virtual bool gate (function *)
491 return (optimize >= 1
492 /* Don't bother doing anything if the program has errors. */
493 && !seen_error ());
496 }; // class pass_all_early_optimizations
498 } // anon namespace
500 static gimple_opt_pass *
501 make_pass_all_early_optimizations (gcc::context *ctxt)
503 return new pass_all_early_optimizations (ctxt);
506 namespace {
508 const pass_data pass_data_all_optimizations =
510 GIMPLE_PASS, /* type */
511 "*all_optimizations", /* name */
512 OPTGROUP_NONE, /* optinfo_flags */
513 TV_OPTIMIZE, /* tv_id */
514 0, /* properties_required */
515 0, /* properties_provided */
516 0, /* properties_destroyed */
517 0, /* todo_flags_start */
518 0, /* todo_flags_finish */
521 class pass_all_optimizations : public gimple_opt_pass
523 public:
524 pass_all_optimizations (gcc::context *ctxt)
525 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
528 /* opt_pass methods: */
529 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
531 }; // class pass_all_optimizations
533 } // anon namespace
535 static gimple_opt_pass *
536 make_pass_all_optimizations (gcc::context *ctxt)
538 return new pass_all_optimizations (ctxt);
541 namespace {
543 const pass_data pass_data_all_optimizations_g =
545 GIMPLE_PASS, /* type */
546 "*all_optimizations_g", /* name */
547 OPTGROUP_NONE, /* optinfo_flags */
548 TV_OPTIMIZE, /* tv_id */
549 0, /* properties_required */
550 0, /* properties_provided */
551 0, /* properties_destroyed */
552 0, /* todo_flags_start */
553 0, /* todo_flags_finish */
556 class pass_all_optimizations_g : public gimple_opt_pass
558 public:
559 pass_all_optimizations_g (gcc::context *ctxt)
560 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
563 /* opt_pass methods: */
564 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
566 }; // class pass_all_optimizations_g
568 } // anon namespace
570 static gimple_opt_pass *
571 make_pass_all_optimizations_g (gcc::context *ctxt)
573 return new pass_all_optimizations_g (ctxt);
576 namespace {
578 const pass_data pass_data_rest_of_compilation =
580 RTL_PASS, /* type */
581 "*rest_of_compilation", /* name */
582 OPTGROUP_NONE, /* optinfo_flags */
583 TV_REST_OF_COMPILATION, /* tv_id */
584 PROP_rtl, /* properties_required */
585 0, /* properties_provided */
586 0, /* properties_destroyed */
587 0, /* todo_flags_start */
588 0, /* todo_flags_finish */
591 class pass_rest_of_compilation : public rtl_opt_pass
593 public:
594 pass_rest_of_compilation (gcc::context *ctxt)
595 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
598 /* opt_pass methods: */
599 virtual bool gate (function *)
601 /* Early return if there were errors. We can run afoul of our
602 consistency checks, and there's not really much point in fixing them. */
603 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
606 }; // class pass_rest_of_compilation
608 } // anon namespace
610 static rtl_opt_pass *
611 make_pass_rest_of_compilation (gcc::context *ctxt)
613 return new pass_rest_of_compilation (ctxt);
616 namespace {
618 const pass_data pass_data_postreload =
620 RTL_PASS, /* type */
621 "*all-postreload", /* name */
622 OPTGROUP_NONE, /* optinfo_flags */
623 TV_POSTRELOAD, /* tv_id */
624 PROP_rtl, /* properties_required */
625 0, /* properties_provided */
626 0, /* properties_destroyed */
627 0, /* todo_flags_start */
628 0, /* todo_flags_finish */
631 class pass_postreload : public rtl_opt_pass
633 public:
634 pass_postreload (gcc::context *ctxt)
635 : rtl_opt_pass (pass_data_postreload, ctxt)
638 /* opt_pass methods: */
639 virtual bool gate (function *) { return reload_completed; }
641 }; // class pass_postreload
643 } // anon namespace
645 static rtl_opt_pass *
646 make_pass_postreload (gcc::context *ctxt)
648 return new pass_postreload (ctxt);
651 namespace {
653 const pass_data pass_data_late_compilation =
655 RTL_PASS, /* type */
656 "*all-late_compilation", /* name */
657 OPTGROUP_NONE, /* optinfo_flags */
658 TV_LATE_COMPILATION, /* tv_id */
659 PROP_rtl, /* properties_required */
660 0, /* properties_provided */
661 0, /* properties_destroyed */
662 0, /* todo_flags_start */
663 0, /* todo_flags_finish */
666 class pass_late_compilation : public rtl_opt_pass
668 public:
669 pass_late_compilation (gcc::context *ctxt)
670 : rtl_opt_pass (pass_data_late_compilation, ctxt)
673 /* opt_pass methods: */
674 virtual bool gate (function *)
676 return reload_completed || targetm.no_register_allocation;
679 }; // class pass_late_compilation
681 } // anon namespace
683 static rtl_opt_pass *
684 make_pass_late_compilation (gcc::context *ctxt)
686 return new pass_late_compilation (ctxt);
691 /* Set the static pass number of pass PASS to ID and record that
692 in the mapping from static pass number to pass. */
694 void
695 pass_manager::
696 set_pass_for_id (int id, opt_pass *pass)
698 pass->static_pass_number = id;
699 if (passes_by_id_size <= id)
701 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
702 memset (passes_by_id + passes_by_id_size, 0,
703 (id + 1 - passes_by_id_size) * sizeof (void *));
704 passes_by_id_size = id + 1;
706 passes_by_id[id] = pass;
709 /* Return the pass with the static pass number ID. */
711 opt_pass *
712 pass_manager::get_pass_for_id (int id) const
714 if (id >= passes_by_id_size)
715 return NULL;
716 return passes_by_id[id];
719 /* Iterate over the pass tree allocating dump file numbers. We want
720 to do this depth first, and independent of whether the pass is
721 enabled or not. */
723 void
724 register_one_dump_file (opt_pass *pass)
726 g->get_passes ()->register_one_dump_file (pass);
729 void
730 pass_manager::register_one_dump_file (opt_pass *pass)
732 char *dot_name, *flag_name, *glob_name;
733 const char *name, *full_name, *prefix;
734 char num[10];
735 int flags, id;
736 int optgroup_flags = OPTGROUP_NONE;
737 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
739 /* See below in next_pass_1. */
740 num[0] = '\0';
741 if (pass->static_pass_number != -1)
742 sprintf (num, "%d", ((int) pass->static_pass_number < 0
743 ? 1 : pass->static_pass_number));
745 /* The name is both used to identify the pass for the purposes of plugins,
746 and to specify dump file name and option.
747 The latter two might want something short which is not quite unique; for
748 that reason, we may have a disambiguating prefix, followed by a space
749 to mark the start of the following dump file name / option string. */
750 name = strchr (pass->name, ' ');
751 name = name ? name + 1 : pass->name;
752 dot_name = concat (".", name, num, NULL);
753 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
755 prefix = "ipa-";
756 flags = TDF_IPA;
757 optgroup_flags |= OPTGROUP_IPA;
759 else if (pass->type == GIMPLE_PASS)
761 prefix = "tree-";
762 flags = TDF_TREE;
764 else
766 prefix = "rtl-";
767 flags = TDF_RTL;
770 flag_name = concat (prefix, name, num, NULL);
771 glob_name = concat (prefix, name, NULL);
772 optgroup_flags |= pass->optinfo_flags;
773 /* For any passes that do not have an optgroup set, and which are not
774 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
775 any dump messages are emitted properly under -fopt-info(-optall). */
776 if (optgroup_flags == OPTGROUP_NONE)
777 optgroup_flags = OPTGROUP_OTHER;
778 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
779 optgroup_flags,
780 true);
781 set_pass_for_id (id, pass);
782 full_name = concat (prefix, pass->name, num, NULL);
783 register_pass_name (pass, full_name);
784 free (CONST_CAST (char *, full_name));
787 /* Register the dump files for the pass_manager starting at PASS. */
789 void
790 pass_manager::register_dump_files (opt_pass *pass)
794 if (pass->name && pass->name[0] != '*')
795 register_one_dump_file (pass);
797 if (pass->sub)
798 register_dump_files (pass->sub);
800 pass = pass->next;
802 while (pass);
805 /* Helper for pass_registry hash table. */
807 struct pass_registry_hasher : default_hashmap_traits
809 static inline hashval_t hash (const char *);
810 static inline bool equal_keys (const char *, const char *);
813 /* Pass registry hash function. */
815 inline hashval_t
816 pass_registry_hasher::hash (const char *name)
818 return htab_hash_string (name);
821 /* Hash equal function */
823 inline bool
824 pass_registry_hasher::equal_keys (const char *s1, const char *s2)
826 return !strcmp (s1, s2);
829 static hash_map<const char *, opt_pass *, pass_registry_hasher>
830 *name_to_pass_map;
832 /* Register PASS with NAME. */
834 static void
835 register_pass_name (opt_pass *pass, const char *name)
837 if (!name_to_pass_map)
838 name_to_pass_map
839 = new hash_map<const char *, opt_pass *, pass_registry_hasher> (256);
841 if (name_to_pass_map->get (name))
842 return; /* Ignore plugin passes. */
844 const char *unique_name = xstrdup (name);
845 name_to_pass_map->put (unique_name, pass);
848 /* Map from pass id to canonicalized pass name. */
850 typedef const char *char_ptr;
851 static vec<char_ptr> pass_tab = vNULL;
853 /* Callback function for traversing NAME_TO_PASS_MAP. */
855 bool
856 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
858 gcc_assert (pass->static_pass_number > 0);
859 gcc_assert (pass_tab.exists ());
861 pass_tab[pass->static_pass_number] = name;
863 return 1;
866 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
867 table for dumping purpose. */
869 static void
870 create_pass_tab (void)
872 if (!flag_dump_passes)
873 return;
875 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
876 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
879 static bool override_gate_status (opt_pass *, tree, bool);
881 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
882 is turned on or not. */
884 static void
885 dump_one_pass (opt_pass *pass, int pass_indent)
887 int indent = 3 * pass_indent;
888 const char *pn;
889 bool is_on, is_really_on;
891 is_on = pass->gate (cfun);
892 is_really_on = override_gate_status (pass, current_function_decl, is_on);
894 if (pass->static_pass_number <= 0)
895 pn = pass->name;
896 else
897 pn = pass_tab[pass->static_pass_number];
899 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
900 (15 - indent < 0 ? 0 : 15 - indent), " ",
901 is_on ? " ON" : " OFF",
902 ((!is_on) == (!is_really_on) ? ""
903 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
906 /* Dump pass list PASS with indentation INDENT. */
908 static void
909 dump_pass_list (opt_pass *pass, int indent)
913 dump_one_pass (pass, indent);
914 if (pass->sub)
915 dump_pass_list (pass->sub, indent + 1);
916 pass = pass->next;
918 while (pass);
921 /* Dump all optimization passes. */
923 void
924 dump_passes (void)
926 g->get_passes ()->dump_passes ();
929 void
930 pass_manager::dump_passes () const
932 struct cgraph_node *n, *node = NULL;
934 create_pass_tab ();
936 FOR_EACH_FUNCTION (n)
937 if (DECL_STRUCT_FUNCTION (n->decl))
939 node = n;
940 break;
943 if (!node)
944 return;
946 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
948 dump_pass_list (all_lowering_passes, 1);
949 dump_pass_list (all_small_ipa_passes, 1);
950 dump_pass_list (all_regular_ipa_passes, 1);
951 dump_pass_list (all_late_ipa_passes, 1);
952 dump_pass_list (all_passes, 1);
954 pop_cfun ();
958 /* Returns the pass with NAME. */
960 static opt_pass *
961 get_pass_by_name (const char *name)
963 opt_pass **p = name_to_pass_map->get (name);
964 if (p)
965 return *p;
967 return NULL;
971 /* Range [start, last]. */
973 struct uid_range
975 unsigned int start;
976 unsigned int last;
977 const char *assem_name;
978 struct uid_range *next;
981 typedef struct uid_range *uid_range_p;
984 static vec<uid_range_p>
985 enabled_pass_uid_range_tab = vNULL;
986 static vec<uid_range_p>
987 disabled_pass_uid_range_tab = vNULL;
990 /* Parse option string for -fdisable- and -fenable-
991 The syntax of the options:
993 -fenable-<pass_name>
994 -fdisable-<pass_name>
996 -fenable-<pass_name>=s1:e1,s2:e2,...
997 -fdisable-<pass_name>=s1:e1,s2:e2,...
1000 static void
1001 enable_disable_pass (const char *arg, bool is_enable)
1003 opt_pass *pass;
1004 char *range_str, *phase_name;
1005 char *argstr = xstrdup (arg);
1006 vec<uid_range_p> *tab = 0;
1008 range_str = strchr (argstr,'=');
1009 if (range_str)
1011 *range_str = '\0';
1012 range_str++;
1015 phase_name = argstr;
1016 if (!*phase_name)
1018 if (is_enable)
1019 error ("unrecognized option -fenable");
1020 else
1021 error ("unrecognized option -fdisable");
1022 free (argstr);
1023 return;
1025 pass = get_pass_by_name (phase_name);
1026 if (!pass || pass->static_pass_number == -1)
1028 if (is_enable)
1029 error ("unknown pass %s specified in -fenable", phase_name);
1030 else
1031 error ("unknown pass %s specified in -fdisable", phase_name);
1032 free (argstr);
1033 return;
1036 if (is_enable)
1037 tab = &enabled_pass_uid_range_tab;
1038 else
1039 tab = &disabled_pass_uid_range_tab;
1041 if ((unsigned) pass->static_pass_number >= tab->length ())
1042 tab->safe_grow_cleared (pass->static_pass_number + 1);
1044 if (!range_str)
1046 uid_range_p slot;
1047 uid_range_p new_range = XCNEW (struct uid_range);
1049 new_range->start = 0;
1050 new_range->last = (unsigned)-1;
1052 slot = (*tab)[pass->static_pass_number];
1053 new_range->next = slot;
1054 (*tab)[pass->static_pass_number] = new_range;
1055 if (is_enable)
1056 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1057 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1058 else
1059 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1060 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1062 else
1064 char *next_range = NULL;
1065 char *one_range = range_str;
1066 char *end_val = NULL;
1070 uid_range_p slot;
1071 uid_range_p new_range;
1072 char *invalid = NULL;
1073 long start;
1074 char *func_name = NULL;
1076 next_range = strchr (one_range, ',');
1077 if (next_range)
1079 *next_range = '\0';
1080 next_range++;
1083 end_val = strchr (one_range, ':');
1084 if (end_val)
1086 *end_val = '\0';
1087 end_val++;
1089 start = strtol (one_range, &invalid, 10);
1090 if (*invalid || start < 0)
1092 if (end_val || (one_range[0] >= '0'
1093 && one_range[0] <= '9'))
1095 error ("Invalid range %s in option %s",
1096 one_range,
1097 is_enable ? "-fenable" : "-fdisable");
1098 free (argstr);
1099 return;
1101 func_name = one_range;
1103 if (!end_val)
1105 new_range = XCNEW (struct uid_range);
1106 if (!func_name)
1108 new_range->start = (unsigned) start;
1109 new_range->last = (unsigned) start;
1111 else
1113 new_range->start = (unsigned) -1;
1114 new_range->last = (unsigned) -1;
1115 new_range->assem_name = xstrdup (func_name);
1118 else
1120 long last = strtol (end_val, &invalid, 10);
1121 if (*invalid || last < start)
1123 error ("Invalid range %s in option %s",
1124 end_val,
1125 is_enable ? "-fenable" : "-fdisable");
1126 free (argstr);
1127 return;
1129 new_range = XCNEW (struct uid_range);
1130 new_range->start = (unsigned) start;
1131 new_range->last = (unsigned) last;
1134 slot = (*tab)[pass->static_pass_number];
1135 new_range->next = slot;
1136 (*tab)[pass->static_pass_number] = new_range;
1137 if (is_enable)
1139 if (new_range->assem_name)
1140 inform (UNKNOWN_LOCATION,
1141 "enable pass %s for function %s",
1142 phase_name, new_range->assem_name);
1143 else
1144 inform (UNKNOWN_LOCATION,
1145 "enable pass %s for functions in the range of [%u, %u]",
1146 phase_name, new_range->start, new_range->last);
1148 else
1150 if (new_range->assem_name)
1151 inform (UNKNOWN_LOCATION,
1152 "disable pass %s for function %s",
1153 phase_name, new_range->assem_name);
1154 else
1155 inform (UNKNOWN_LOCATION,
1156 "disable pass %s for functions in the range of [%u, %u]",
1157 phase_name, new_range->start, new_range->last);
1160 one_range = next_range;
1161 } while (next_range);
1164 free (argstr);
1167 /* Enable pass specified by ARG. */
1169 void
1170 enable_pass (const char *arg)
1172 enable_disable_pass (arg, true);
1175 /* Disable pass specified by ARG. */
1177 void
1178 disable_pass (const char *arg)
1180 enable_disable_pass (arg, false);
1183 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1185 static bool
1186 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1187 tree func,
1188 vec<uid_range_p> tab)
1190 uid_range_p slot, range;
1191 int cgraph_uid;
1192 const char *aname = NULL;
1194 if (!tab.exists ()
1195 || (unsigned) pass->static_pass_number >= tab.length ()
1196 || pass->static_pass_number == -1)
1197 return false;
1199 slot = tab[pass->static_pass_number];
1200 if (!slot)
1201 return false;
1203 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1204 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1205 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1207 range = slot;
1208 while (range)
1210 if ((unsigned) cgraph_uid >= range->start
1211 && (unsigned) cgraph_uid <= range->last)
1212 return true;
1213 if (range->assem_name && aname
1214 && !strcmp (range->assem_name, aname))
1215 return true;
1216 range = range->next;
1219 return false;
1223 /* Update static_pass_number for passes (and the flag
1224 TODO_mark_first_instance).
1226 Passes are constructed with static_pass_number preinitialized to 0
1228 This field is used in two different ways: initially as instance numbers
1229 of their kind, and then as ids within the entire pass manager.
1231 Within pass_manager::pass_manager:
1233 * In add_pass_instance(), as called by next_pass_1 in
1234 NEXT_PASS in init_optimization_passes
1236 * When the initial instance of a pass within a pass manager is seen,
1237 it is flagged, and its static_pass_number is set to -1
1239 * On subsequent times that it is seen, the static pass number
1240 is decremented each time, so that if there are e.g. 4 dups,
1241 they have static_pass_number -4, 2, 3, 4 respectively (note
1242 how the initial one is negative and gives the count); these
1243 can be thought of as instance numbers of the specific pass
1245 * Within the register_dump_files () traversal, set_pass_for_id()
1246 is called on each pass, using these instance numbers to create
1247 dumpfile switches, and then overwriting them with a pass id,
1248 which are global to the whole pass manager (based on
1249 (TDI_end + current value of extra_dump_files_in_use) ) */
1251 static void
1252 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1253 opt_pass *initial_pass)
1255 /* Are we dealing with the first pass of its kind, or a clone? */
1256 if (new_pass != initial_pass)
1258 /* We're dealing with a clone. */
1259 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1261 /* Indicate to register_dump_files that this pass has duplicates,
1262 and so it should rename the dump file. The first instance will
1263 be -1, and be number of duplicates = -static_pass_number - 1.
1264 Subsequent instances will be > 0 and just the duplicate number. */
1265 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1267 initial_pass->static_pass_number -= 1;
1268 new_pass->static_pass_number = -initial_pass->static_pass_number;
1271 else
1273 /* We're dealing with the first pass of its kind. */
1274 new_pass->todo_flags_start |= TODO_mark_first_instance;
1275 new_pass->static_pass_number = -1;
1277 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1281 /* Add a pass to the pass list. Duplicate the pass if it's already
1282 in the list. */
1284 static opt_pass **
1285 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1287 /* Every pass should have a name so that plugins can refer to them. */
1288 gcc_assert (pass->name != NULL);
1290 add_pass_instance (pass, false, initial_pass);
1291 *list = pass;
1293 return &(*list)->next;
1296 /* List node for an inserted pass instance. We need to keep track of all
1297 the newly-added pass instances (with 'added_pass_nodes' defined below)
1298 so that we can register their dump files after pass-positioning is finished.
1299 Registering dumping files needs to be post-processed or the
1300 static_pass_number of the opt_pass object would be modified and mess up
1301 the dump file names of future pass instances to be added. */
1303 struct pass_list_node
1305 opt_pass *pass;
1306 struct pass_list_node *next;
1309 static struct pass_list_node *added_pass_nodes = NULL;
1310 static struct pass_list_node *prev_added_pass_node;
1312 /* Insert the pass at the proper position. Return true if the pass
1313 is successfully added.
1315 NEW_PASS_INFO - new pass to be inserted
1316 PASS_LIST - root of the pass list to insert the new pass to */
1318 static bool
1319 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1321 opt_pass *pass = *pass_list, *prev_pass = NULL;
1322 bool success = false;
1324 for ( ; pass; prev_pass = pass, pass = pass->next)
1326 /* Check if the current pass is of the same type as the new pass and
1327 matches the name and the instance number of the reference pass. */
1328 if (pass->type == new_pass_info->pass->type
1329 && pass->name
1330 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1331 && ((new_pass_info->ref_pass_instance_number == 0)
1332 || (new_pass_info->ref_pass_instance_number ==
1333 pass->static_pass_number)
1334 || (new_pass_info->ref_pass_instance_number == 1
1335 && pass->todo_flags_start & TODO_mark_first_instance)))
1337 opt_pass *new_pass;
1338 struct pass_list_node *new_pass_node;
1340 if (new_pass_info->ref_pass_instance_number == 0)
1342 new_pass = new_pass_info->pass->clone ();
1343 add_pass_instance (new_pass, true, new_pass_info->pass);
1345 else
1347 new_pass = new_pass_info->pass;
1348 add_pass_instance (new_pass, true, new_pass);
1351 /* Insert the new pass instance based on the positioning op. */
1352 switch (new_pass_info->pos_op)
1354 case PASS_POS_INSERT_AFTER:
1355 new_pass->next = pass->next;
1356 pass->next = new_pass;
1358 /* Skip newly inserted pass to avoid repeated
1359 insertions in the case where the new pass and the
1360 existing one have the same name. */
1361 pass = new_pass;
1362 break;
1363 case PASS_POS_INSERT_BEFORE:
1364 new_pass->next = pass;
1365 if (prev_pass)
1366 prev_pass->next = new_pass;
1367 else
1368 *pass_list = new_pass;
1369 break;
1370 case PASS_POS_REPLACE:
1371 new_pass->next = pass->next;
1372 if (prev_pass)
1373 prev_pass->next = new_pass;
1374 else
1375 *pass_list = new_pass;
1376 new_pass->sub = pass->sub;
1377 new_pass->tv_id = pass->tv_id;
1378 pass = new_pass;
1379 break;
1380 default:
1381 error ("invalid pass positioning operation");
1382 return false;
1385 /* Save the newly added pass (instance) in the added_pass_nodes
1386 list so that we can register its dump file later. Note that
1387 we cannot register the dump file now because doing so will modify
1388 the static_pass_number of the opt_pass object and therefore
1389 mess up the dump file name of future instances. */
1390 new_pass_node = XCNEW (struct pass_list_node);
1391 new_pass_node->pass = new_pass;
1392 if (!added_pass_nodes)
1393 added_pass_nodes = new_pass_node;
1394 else
1395 prev_added_pass_node->next = new_pass_node;
1396 prev_added_pass_node = new_pass_node;
1398 success = true;
1401 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1402 success = true;
1405 return success;
1408 /* Hooks a new pass into the pass lists.
1410 PASS_INFO - pass information that specifies the opt_pass object,
1411 reference pass, instance number, and how to position
1412 the pass */
1414 void
1415 register_pass (struct register_pass_info *pass_info)
1417 g->get_passes ()->register_pass (pass_info);
1420 void
1421 register_pass (opt_pass* pass, pass_positioning_ops pos,
1422 const char* ref_pass_name, int ref_pass_inst_number)
1424 register_pass_info i;
1425 i.pass = pass;
1426 i.reference_pass_name = ref_pass_name;
1427 i.ref_pass_instance_number = ref_pass_inst_number;
1428 i.pos_op = pos;
1430 g->get_passes ()->register_pass (&i);
1433 void
1434 pass_manager::register_pass (struct register_pass_info *pass_info)
1436 bool all_instances, success;
1437 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1439 /* The checks below could fail in buggy plugins. Existing GCC
1440 passes should never fail these checks, so we mention plugin in
1441 the messages. */
1442 if (!pass_info->pass)
1443 fatal_error ("plugin cannot register a missing pass");
1445 if (!pass_info->pass->name)
1446 fatal_error ("plugin cannot register an unnamed pass");
1448 if (!pass_info->reference_pass_name)
1449 fatal_error
1450 ("plugin cannot register pass %qs without reference pass name",
1451 pass_info->pass->name);
1453 /* Try to insert the new pass to the pass lists. We need to check
1454 all five lists as the reference pass could be in one (or all) of
1455 them. */
1456 all_instances = pass_info->ref_pass_instance_number == 0;
1457 success = position_pass (pass_info, &all_lowering_passes);
1458 if (!success || all_instances)
1459 success |= position_pass (pass_info, &all_small_ipa_passes);
1460 if (!success || all_instances)
1461 success |= position_pass (pass_info, &all_regular_ipa_passes);
1462 if (!success || all_instances)
1463 success |= position_pass (pass_info, &all_late_ipa_passes);
1464 if (!success || all_instances)
1465 success |= position_pass (pass_info, &all_passes);
1466 if (!success)
1467 fatal_error
1468 ("pass %qs not found but is referenced by new pass %qs",
1469 pass_info->reference_pass_name, pass_info->pass->name);
1471 /* OK, we have successfully inserted the new pass. We need to register
1472 the dump files for the newly added pass and its duplicates (if any).
1473 Because the registration of plugin/backend passes happens after the
1474 command-line options are parsed, the options that specify single
1475 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1476 passes. Therefore we currently can only enable dumping of
1477 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1478 are specified. While doing so, we also delete the pass_list_node
1479 objects created during pass positioning. */
1480 while (added_pass_nodes)
1482 struct pass_list_node *next_node = added_pass_nodes->next;
1483 enum tree_dump_index tdi;
1484 register_one_dump_file (added_pass_nodes->pass);
1485 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1486 || added_pass_nodes->pass->type == IPA_PASS)
1487 tdi = TDI_ipa_all;
1488 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1489 tdi = TDI_tree_all;
1490 else
1491 tdi = TDI_rtl_all;
1492 /* Check if dump-all flag is specified. */
1493 if (dumps->get_dump_file_info (tdi)->pstate)
1494 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1495 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1496 XDELETE (added_pass_nodes);
1497 added_pass_nodes = next_node;
1501 /* Construct the pass tree. The sequencing of passes is driven by
1502 the cgraph routines:
1504 finalize_compilation_unit ()
1505 for each node N in the cgraph
1506 cgraph_analyze_function (N)
1507 cgraph_lower_function (N) -> all_lowering_passes
1509 If we are optimizing, compile is then invoked:
1511 compile ()
1512 ipa_passes () -> all_small_ipa_passes
1513 -> Analysis of all_regular_ipa_passes
1514 * possible LTO streaming at copmilation time *
1515 -> Execution of all_regular_ipa_passes
1516 * possible LTO streaming at link time *
1517 -> all_late_ipa_passes
1518 expand_all_functions ()
1519 for each node N in the cgraph
1520 expand_function (N) -> Transformation of all_regular_ipa_passes
1521 -> all_passes
1524 void *
1525 pass_manager::operator new (size_t sz)
1527 /* Ensure that all fields of the pass manager are zero-initialized. */
1528 return xcalloc (1, sz);
1531 void
1532 pass_manager::operator delete (void *ptr)
1534 free (ptr);
1537 pass_manager::pass_manager (context *ctxt)
1538 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1539 all_regular_ipa_passes (NULL),
1540 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1541 m_ctxt (ctxt)
1543 opt_pass **p;
1545 /* Initialize the pass_lists array. */
1546 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1547 GCC_PASS_LISTS
1548 #undef DEF_PASS_LIST
1550 /* Build the tree of passes. */
1552 #define INSERT_PASSES_AFTER(PASS) \
1553 p = &(PASS);
1555 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1557 opt_pass **p = &(PASS ## _1)->sub;
1559 #define POP_INSERT_PASSES() \
1562 #define NEXT_PASS(PASS, NUM) \
1563 do { \
1564 gcc_assert (NULL == PASS ## _ ## NUM); \
1565 if ((NUM) == 1) \
1566 PASS ## _1 = make_##PASS (m_ctxt); \
1567 else \
1569 gcc_assert (PASS ## _1); \
1570 PASS ## _ ## NUM = PASS ## _1->clone (); \
1572 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1573 } while (0)
1575 #define TERMINATE_PASS_LIST() \
1576 *p = NULL;
1578 #include "pass-instances.def"
1580 #undef INSERT_PASSES_AFTER
1581 #undef PUSH_INSERT_PASSES_WITHIN
1582 #undef POP_INSERT_PASSES
1583 #undef NEXT_PASS
1584 #undef TERMINATE_PASS_LIST
1586 /* Register the passes with the tree dump code. */
1587 register_dump_files (all_lowering_passes);
1588 register_dump_files (all_small_ipa_passes);
1589 register_dump_files (all_regular_ipa_passes);
1590 register_dump_files (all_late_ipa_passes);
1591 register_dump_files (all_passes);
1594 static void
1595 delete_pass_tree (opt_pass *pass)
1597 while (pass)
1599 /* Recurse into child passes. */
1600 delete_pass_tree (pass->sub);
1602 opt_pass *next = pass->next;
1604 /* Delete this pass. */
1605 delete pass;
1607 /* Iterate onto sibling passes. */
1608 pass = next;
1612 pass_manager::~pass_manager ()
1614 XDELETEVEC (passes_by_id);
1616 /* Call delete_pass_tree on each of the pass_lists. */
1617 #define DEF_PASS_LIST(LIST) \
1618 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1619 GCC_PASS_LISTS
1620 #undef DEF_PASS_LIST
1624 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1625 function CALLBACK for every function in the call graph. Otherwise,
1626 call CALLBACK on the current function. */
1628 static void
1629 do_per_function (void (*callback) (function *, void *data), void *data)
1631 if (current_function_decl)
1632 callback (cfun, data);
1633 else
1635 struct cgraph_node *node;
1636 FOR_EACH_DEFINED_FUNCTION (node)
1637 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1638 && (!node->clone_of || node->decl != node->clone_of->decl))
1639 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1643 /* Because inlining might remove no-longer reachable nodes, we need to
1644 keep the array visible to garbage collector to avoid reading collected
1645 out nodes. */
1646 static int nnodes;
1647 static GTY ((length ("nnodes"))) cgraph_node **order;
1649 /* Hook called when NODE is removed and therefore should be
1650 excluded from order vector. DATA is an array of integers.
1651 DATA[0] holds max index it may be accessed by. For cgraph
1652 node DATA[node->uid + 1] holds index of this node in order
1653 vector. */
1654 static void
1655 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1657 int *order_idx = (int *)data;
1659 if (node->uid >= order_idx[0])
1660 return;
1662 int idx = order_idx[node->uid + 1];
1663 if (idx >= 0 && idx < nnodes && order[idx] == node)
1664 order[idx] = NULL;
1667 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1668 function CALLBACK for every function in the call graph. Otherwise,
1669 call CALLBACK on the current function.
1670 This function is global so that plugins can use it. */
1671 void
1672 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1674 int i;
1676 if (current_function_decl)
1677 callback (cfun, data);
1678 else
1680 cgraph_node_hook_list *hook;
1681 int *order_idx;
1682 gcc_assert (!order);
1683 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1685 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1686 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1687 order_idx[0] = symtab->cgraph_max_uid;
1689 nnodes = ipa_reverse_postorder (order);
1690 for (i = nnodes - 1; i >= 0; i--)
1692 order[i]->process = 1;
1693 order_idx[order[i]->uid + 1] = i;
1695 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1696 order_idx);
1697 for (i = nnodes - 1; i >= 0; i--)
1699 /* Function could be inlined and removed as unreachable. */
1700 if (!order[i])
1701 continue;
1703 struct cgraph_node *node = order[i];
1705 /* Allow possibly removed nodes to be garbage collected. */
1706 order[i] = NULL;
1707 node->process = 0;
1708 if (node->has_gimple_body_p ())
1709 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1711 symtab->remove_cgraph_removal_hook (hook);
1713 ggc_free (order);
1714 order = NULL;
1715 nnodes = 0;
1718 /* Helper function to perform function body dump. */
1720 static void
1721 execute_function_dump (function *fn, void *data)
1723 opt_pass *pass = (opt_pass *)data;
1725 if (dump_file)
1727 push_cfun (fn);
1729 if (fn->curr_properties & PROP_trees)
1730 dump_function_to_file (fn->decl, dump_file, dump_flags);
1731 else
1732 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1734 /* Flush the file. If verification fails, we won't be able to
1735 close the file before aborting. */
1736 fflush (dump_file);
1738 if ((fn->curr_properties & PROP_cfg)
1739 && (dump_flags & TDF_GRAPH))
1741 if (!pass->graph_dump_initialized)
1743 clean_graph_dump_file (dump_file_name);
1744 pass->graph_dump_initialized = true;
1746 print_graph_cfg (dump_file_name, fn);
1749 pop_cfun ();
1753 static struct profile_record *profile_record;
1755 /* Do profile consistency book-keeping for the pass with static number INDEX.
1756 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1757 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1758 if we are only book-keeping on passes that may have selectively disabled
1759 themselves on a given function. */
1760 static void
1761 check_profile_consistency (int index, int subpass, bool run)
1763 pass_manager *passes = g->get_passes ();
1764 if (index == -1)
1765 return;
1766 if (!profile_record)
1767 profile_record = XCNEWVEC (struct profile_record,
1768 passes->passes_by_id_size);
1769 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1770 gcc_assert (subpass < 2);
1771 profile_record[index].run |= run;
1772 account_profile_record (&profile_record[index], subpass);
1775 /* Output profile consistency. */
1777 void
1778 dump_profile_report (void)
1780 g->get_passes ()->dump_profile_report ();
1783 void
1784 pass_manager::dump_profile_report () const
1786 int i, j;
1787 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1788 gcov_type last_time = 0, last_size = 0;
1789 double rel_time_change, rel_size_change;
1790 int last_reported = 0;
1792 if (!profile_record)
1793 return;
1794 fprintf (stderr, "\nProfile consistency report:\n\n");
1795 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1796 fprintf (stderr, " |freq count |freq count |size time\n");
1798 for (i = 0; i < passes_by_id_size; i++)
1799 for (j = 0 ; j < 2; j++)
1800 if (profile_record[i].run)
1802 if (last_time)
1803 rel_time_change = (profile_record[i].time[j]
1804 - (double)last_time) * 100 / (double)last_time;
1805 else
1806 rel_time_change = 0;
1807 if (last_size)
1808 rel_size_change = (profile_record[i].size[j]
1809 - (double)last_size) * 100 / (double)last_size;
1810 else
1811 rel_size_change = 0;
1813 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1814 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1815 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1816 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1817 || rel_time_change || rel_size_change)
1819 last_reported = i;
1820 fprintf (stderr, "%-20s %s",
1821 passes_by_id [i]->name,
1822 j ? "(after TODO)" : " ");
1823 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1824 fprintf (stderr, "| %+5i",
1825 profile_record[i].num_mismatched_freq_in[j]
1826 - last_freq_in);
1827 else
1828 fprintf (stderr, "| ");
1829 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1830 fprintf (stderr, " %+5i",
1831 profile_record[i].num_mismatched_count_in[j]
1832 - last_count_in);
1833 else
1834 fprintf (stderr, " ");
1835 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1836 fprintf (stderr, "| %+5i",
1837 profile_record[i].num_mismatched_freq_out[j]
1838 - last_freq_out);
1839 else
1840 fprintf (stderr, "| ");
1841 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1842 fprintf (stderr, " %+5i",
1843 profile_record[i].num_mismatched_count_out[j]
1844 - last_count_out);
1845 else
1846 fprintf (stderr, " ");
1848 /* Size/time units change across gimple and RTL. */
1849 if (i == pass_expand_1->static_pass_number)
1850 fprintf (stderr, "|----------");
1851 else
1853 if (rel_size_change)
1854 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1855 else
1856 fprintf (stderr, "| ");
1857 if (rel_time_change)
1858 fprintf (stderr, " %+8.4f%%", rel_time_change);
1860 fprintf (stderr, "\n");
1861 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1862 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1863 last_count_in = profile_record[i].num_mismatched_count_in[j];
1864 last_count_out = profile_record[i].num_mismatched_count_out[j];
1866 else if (j && last_reported != i)
1868 last_reported = i;
1869 fprintf (stderr, "%-20s ------------| | |\n",
1870 passes_by_id [i]->name);
1872 last_time = profile_record[i].time[j];
1873 last_size = profile_record[i].size[j];
1877 /* Perform all TODO actions that ought to be done on each function. */
1879 static void
1880 execute_function_todo (function *fn, void *data)
1882 bool from_ipa_pass = (cfun == NULL);
1883 unsigned int flags = (size_t)data;
1884 flags &= ~fn->last_verified;
1885 if (!flags)
1886 return;
1888 push_cfun (fn);
1890 /* Always cleanup the CFG before trying to update SSA. */
1891 if (flags & TODO_cleanup_cfg)
1893 cleanup_tree_cfg ();
1895 /* When cleanup_tree_cfg merges consecutive blocks, it may
1896 perform some simplistic propagation when removing single
1897 valued PHI nodes. This propagation may, in turn, cause the
1898 SSA form to become out-of-date (see PR 22037). So, even
1899 if the parent pass had not scheduled an SSA update, we may
1900 still need to do one. */
1901 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1902 flags |= TODO_update_ssa;
1905 if (flags & TODO_update_ssa_any)
1907 unsigned update_flags = flags & TODO_update_ssa_any;
1908 update_ssa (update_flags);
1911 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1912 compute_may_aliases ();
1914 if (optimize && (flags & TODO_update_address_taken))
1915 execute_update_addresses_taken ();
1917 if (flags & TODO_remove_unused_locals)
1918 remove_unused_locals ();
1920 if (flags & TODO_rebuild_frequencies)
1921 rebuild_frequencies ();
1923 if (flags & TODO_rebuild_cgraph_edges)
1924 cgraph_edge::rebuild_edges ();
1926 /* If we've seen errors do not bother running any verifiers. */
1927 if (!seen_error ())
1929 #if defined ENABLE_CHECKING
1930 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1931 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1933 if (flags & TODO_verify_il)
1935 if (cfun->curr_properties & PROP_trees)
1937 if (cfun->curr_properties & PROP_cfg)
1938 /* IPA passes leave stmts to be fixed up, so make sure to
1939 not verify stmts really throw. */
1940 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1941 else
1942 verify_gimple_in_seq (gimple_body (cfun->decl));
1944 if (cfun->curr_properties & PROP_ssa)
1945 /* IPA passes leave stmts to be fixed up, so make sure to
1946 not verify SSA operands whose verifier will choke on that. */
1947 verify_ssa (true, !from_ipa_pass);
1948 /* IPA passes leave basic-blocks unsplit, so make sure to
1949 not trip on that. */
1950 if ((cfun->curr_properties & PROP_cfg)
1951 && !from_ipa_pass)
1952 verify_flow_info ();
1953 if (current_loops
1954 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1955 verify_loop_closed_ssa (false);
1956 if (cfun->curr_properties & PROP_rtl)
1957 verify_rtl_sharing ();
1960 /* Make sure verifiers don't change dominator state. */
1961 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1962 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1963 #endif
1966 fn->last_verified = flags & TODO_verify_all;
1968 pop_cfun ();
1970 /* For IPA passes make sure to release dominator info, it can be
1971 computed by non-verifying TODOs. */
1972 if (from_ipa_pass)
1974 free_dominance_info (fn, CDI_DOMINATORS);
1975 free_dominance_info (fn, CDI_POST_DOMINATORS);
1979 /* Perform all TODO actions. */
1980 static void
1981 execute_todo (unsigned int flags)
1983 #if defined ENABLE_CHECKING
1984 if (cfun
1985 && need_ssa_update_p (cfun))
1986 gcc_assert (flags & TODO_update_ssa_any);
1987 #endif
1989 timevar_push (TV_TODO);
1991 /* Inform the pass whether it is the first time it is run. */
1992 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1994 statistics_fini_pass ();
1996 if (flags)
1997 do_per_function (execute_function_todo, (void *)(size_t) flags);
1999 /* Always remove functions just as before inlining: IPA passes might be
2000 interested to see bodies of extern inline functions that are not inlined
2001 to analyze side effects. The full removal is done just at the end
2002 of IPA pass queue. */
2003 if (flags & TODO_remove_functions)
2005 gcc_assert (!cfun);
2006 symtab->remove_unreachable_nodes (true, dump_file);
2009 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2011 gcc_assert (!cfun);
2012 symtab_node::dump_table (dump_file);
2013 /* Flush the file. If verification fails, we won't be able to
2014 close the file before aborting. */
2015 fflush (dump_file);
2018 /* Now that the dumping has been done, we can get rid of the optional
2019 df problems. */
2020 if (flags & TODO_df_finish)
2021 df_finish_pass ((flags & TODO_df_verify) != 0);
2023 timevar_pop (TV_TODO);
2026 /* Verify invariants that should hold between passes. This is a place
2027 to put simple sanity checks. */
2029 static void
2030 verify_interpass_invariants (void)
2032 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2035 /* Clear the last verified flag. */
2037 static void
2038 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2040 fn->last_verified = 0;
2043 /* Helper function. Verify that the properties has been turn into the
2044 properties expected by the pass. */
2046 #ifdef ENABLE_CHECKING
2047 static void
2048 verify_curr_properties (function *fn, void *data)
2050 unsigned int props = (size_t)data;
2051 gcc_assert ((fn->curr_properties & props) == props);
2053 #endif
2055 /* Initialize pass dump file. */
2056 /* This is non-static so that the plugins can use it. */
2058 bool
2059 pass_init_dump_file (opt_pass *pass)
2061 /* If a dump file name is present, open it if enabled. */
2062 if (pass->static_pass_number != -1)
2064 timevar_push (TV_DUMP);
2065 gcc::dump_manager *dumps = g->get_dumps ();
2066 bool initializing_dump =
2067 !dumps->dump_initialized_p (pass->static_pass_number);
2068 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2069 dumps->dump_start (pass->static_pass_number, &dump_flags);
2070 if (dump_file && current_function_decl)
2071 dump_function_header (dump_file, current_function_decl, dump_flags);
2072 if (initializing_dump
2073 && dump_file && (dump_flags & TDF_GRAPH)
2074 && cfun && (cfun->curr_properties & PROP_cfg))
2076 clean_graph_dump_file (dump_file_name);
2077 pass->graph_dump_initialized = true;
2079 timevar_pop (TV_DUMP);
2080 return initializing_dump;
2082 else
2083 return false;
2086 /* Flush PASS dump file. */
2087 /* This is non-static so that plugins can use it. */
2089 void
2090 pass_fini_dump_file (opt_pass *pass)
2092 timevar_push (TV_DUMP);
2094 /* Flush and close dump file. */
2095 if (dump_file_name)
2097 free (CONST_CAST (char *, dump_file_name));
2098 dump_file_name = NULL;
2101 g->get_dumps ()->dump_finish (pass->static_pass_number);
2102 timevar_pop (TV_DUMP);
2105 /* After executing the pass, apply expected changes to the function
2106 properties. */
2108 static void
2109 update_properties_after_pass (function *fn, void *data)
2111 opt_pass *pass = (opt_pass *) data;
2112 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2113 & ~pass->properties_destroyed;
2116 /* Execute summary generation for all of the passes in IPA_PASS. */
2118 void
2119 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2121 while (ipa_pass)
2123 opt_pass *pass = ipa_pass;
2125 /* Execute all of the IPA_PASSes in the list. */
2126 if (ipa_pass->type == IPA_PASS
2127 && pass->gate (cfun)
2128 && ipa_pass->generate_summary)
2130 pass_init_dump_file (pass);
2132 /* If a timevar is present, start it. */
2133 if (pass->tv_id)
2134 timevar_push (pass->tv_id);
2136 current_pass = pass;
2137 ipa_pass->generate_summary ();
2139 /* Stop timevar. */
2140 if (pass->tv_id)
2141 timevar_pop (pass->tv_id);
2143 pass_fini_dump_file (pass);
2145 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2149 /* Execute IPA_PASS function transform on NODE. */
2151 static void
2152 execute_one_ipa_transform_pass (struct cgraph_node *node,
2153 ipa_opt_pass_d *ipa_pass)
2155 opt_pass *pass = ipa_pass;
2156 unsigned int todo_after = 0;
2158 current_pass = pass;
2159 if (!ipa_pass->function_transform)
2160 return;
2162 /* Note that the folders should only create gimple expressions.
2163 This is a hack until the new folder is ready. */
2164 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2166 pass_init_dump_file (pass);
2168 /* Run pre-pass verification. */
2169 execute_todo (ipa_pass->function_transform_todo_flags_start);
2171 /* If a timevar is present, start it. */
2172 if (pass->tv_id != TV_NONE)
2173 timevar_push (pass->tv_id);
2175 /* Do it! */
2176 todo_after = ipa_pass->function_transform (node);
2178 /* Stop timevar. */
2179 if (pass->tv_id != TV_NONE)
2180 timevar_pop (pass->tv_id);
2182 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2183 check_profile_consistency (pass->static_pass_number, 0, true);
2185 /* Run post-pass cleanup and verification. */
2186 execute_todo (todo_after);
2187 verify_interpass_invariants ();
2188 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2189 check_profile_consistency (pass->static_pass_number, 1, true);
2191 if (dump_file)
2192 do_per_function (execute_function_dump, NULL);
2193 pass_fini_dump_file (pass);
2195 current_pass = NULL;
2197 /* Signal this is a suitable GC collection point. */
2198 if (!(todo_after & TODO_do_not_ggc_collect))
2199 ggc_collect ();
2202 /* For the current function, execute all ipa transforms. */
2204 void
2205 execute_all_ipa_transforms (void)
2207 struct cgraph_node *node;
2208 if (!cfun)
2209 return;
2210 node = cgraph_node::get (current_function_decl);
2212 if (node->ipa_transforms_to_apply.exists ())
2214 unsigned int i;
2216 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2217 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2218 node->ipa_transforms_to_apply.release ();
2222 /* Check if PASS is explicitly disabled or enabled and return
2223 the gate status. FUNC is the function to be processed, and
2224 GATE_STATUS is the gate status determined by pass manager by
2225 default. */
2227 static bool
2228 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2230 bool explicitly_enabled = false;
2231 bool explicitly_disabled = false;
2233 explicitly_enabled
2234 = is_pass_explicitly_enabled_or_disabled (pass, func,
2235 enabled_pass_uid_range_tab);
2236 explicitly_disabled
2237 = is_pass_explicitly_enabled_or_disabled (pass, func,
2238 disabled_pass_uid_range_tab);
2240 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2242 return gate_status;
2246 /* Execute PASS. */
2248 bool
2249 execute_one_pass (opt_pass *pass)
2251 unsigned int todo_after = 0;
2253 bool gate_status;
2255 /* IPA passes are executed on whole program, so cfun should be NULL.
2256 Other passes need function context set. */
2257 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2258 gcc_assert (!cfun && !current_function_decl);
2259 else
2260 gcc_assert (cfun && current_function_decl);
2262 current_pass = pass;
2264 /* Check whether gate check should be avoided.
2265 User controls the value of the gate through the parameter "gate_status". */
2266 gate_status = pass->gate (cfun);
2267 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2269 /* Override gate with plugin. */
2270 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2272 if (!gate_status)
2274 /* Run so passes selectively disabling themselves on a given function
2275 are not miscounted. */
2276 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2278 check_profile_consistency (pass->static_pass_number, 0, false);
2279 check_profile_consistency (pass->static_pass_number, 1, false);
2281 current_pass = NULL;
2282 return false;
2285 /* Pass execution event trigger: useful to identify passes being
2286 executed. */
2287 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2289 if (!quiet_flag && !cfun)
2290 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2292 /* Note that the folders should only create gimple expressions.
2293 This is a hack until the new folder is ready. */
2294 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2296 pass_init_dump_file (pass);
2298 /* Run pre-pass verification. */
2299 execute_todo (pass->todo_flags_start);
2301 #ifdef ENABLE_CHECKING
2302 do_per_function (verify_curr_properties,
2303 (void *)(size_t)pass->properties_required);
2304 #endif
2306 /* If a timevar is present, start it. */
2307 if (pass->tv_id != TV_NONE)
2308 timevar_push (pass->tv_id);
2310 /* Do it! */
2311 todo_after = pass->execute (cfun);
2312 do_per_function (clear_last_verified, NULL);
2314 /* Stop timevar. */
2315 if (pass->tv_id != TV_NONE)
2316 timevar_pop (pass->tv_id);
2318 do_per_function (update_properties_after_pass, pass);
2320 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2321 check_profile_consistency (pass->static_pass_number, 0, true);
2323 /* Run post-pass cleanup and verification. */
2324 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2325 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2326 check_profile_consistency (pass->static_pass_number, 1, true);
2328 verify_interpass_invariants ();
2329 if (dump_file)
2330 do_per_function (execute_function_dump, pass);
2331 if (pass->type == IPA_PASS)
2333 struct cgraph_node *node;
2334 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2335 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2338 if (!current_function_decl)
2339 symtab->process_new_functions ();
2341 pass_fini_dump_file (pass);
2343 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2344 gcc_assert (!(cfun->curr_properties & PROP_trees)
2345 || pass->type != RTL_PASS);
2347 current_pass = NULL;
2349 /* Signal this is a suitable GC collection point. */
2350 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2351 ggc_collect ();
2353 return true;
2356 static void
2357 execute_pass_list_1 (opt_pass *pass)
2361 gcc_assert (pass->type == GIMPLE_PASS
2362 || pass->type == RTL_PASS);
2363 if (execute_one_pass (pass) && pass->sub)
2364 execute_pass_list_1 (pass->sub);
2365 pass = pass->next;
2367 while (pass);
2370 void
2371 execute_pass_list (function *fn, opt_pass *pass)
2373 push_cfun (fn);
2374 execute_pass_list_1 (pass);
2375 if (fn->cfg)
2377 free_dominance_info (CDI_DOMINATORS);
2378 free_dominance_info (CDI_POST_DOMINATORS);
2380 pop_cfun ();
2383 /* Write out all LTO data. */
2384 static void
2385 write_lto (void)
2387 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2388 lto_output ();
2389 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2390 timevar_push (TV_IPA_LTO_DECL_OUT);
2391 produce_asm_for_decls ();
2392 timevar_pop (TV_IPA_LTO_DECL_OUT);
2395 /* Same as execute_pass_list but assume that subpasses of IPA passes
2396 are local passes. If SET is not NULL, write out summaries of only
2397 those node in SET. */
2399 static void
2400 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2402 while (pass)
2404 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2405 gcc_assert (!current_function_decl);
2406 gcc_assert (!cfun);
2407 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2408 if (pass->type == IPA_PASS
2409 && ipa_pass->write_summary
2410 && pass->gate (cfun))
2412 /* If a timevar is present, start it. */
2413 if (pass->tv_id)
2414 timevar_push (pass->tv_id);
2416 pass_init_dump_file (pass);
2418 current_pass = pass;
2419 ipa_pass->write_summary ();
2421 pass_fini_dump_file (pass);
2423 /* If a timevar is present, start it. */
2424 if (pass->tv_id)
2425 timevar_pop (pass->tv_id);
2428 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2429 ipa_write_summaries_2 (pass->sub, state);
2431 pass = pass->next;
2435 /* Helper function of ipa_write_summaries. Creates and destroys the
2436 decl state and calls ipa_write_summaries_2 for all passes that have
2437 summaries. SET is the set of nodes to be written. */
2439 static void
2440 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2442 pass_manager *passes = g->get_passes ();
2443 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2444 state->symtab_node_encoder = encoder;
2446 lto_push_out_decl_state (state);
2448 gcc_assert (!flag_wpa);
2449 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2451 write_lto ();
2453 gcc_assert (lto_get_out_decl_state () == state);
2454 lto_pop_out_decl_state ();
2455 lto_delete_out_decl_state (state);
2458 /* Write out summaries for all the nodes in the callgraph. */
2460 void
2461 ipa_write_summaries (bool offload_lto_mode)
2463 lto_symtab_encoder_t encoder;
2464 int i, order_pos;
2465 varpool_node *vnode;
2466 struct cgraph_node *node;
2467 struct cgraph_node **order;
2469 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2470 return;
2472 select_what_to_stream (offload_lto_mode);
2474 encoder = lto_symtab_encoder_new (false);
2476 /* Create the callgraph set in the same order used in
2477 cgraph_expand_all_functions. This mostly facilitates debugging,
2478 since it causes the gimple file to be processed in the same order
2479 as the source code. */
2480 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2481 order_pos = ipa_reverse_postorder (order);
2482 gcc_assert (order_pos == symtab->cgraph_count);
2484 for (i = order_pos - 1; i >= 0; i--)
2486 struct cgraph_node *node = order[i];
2488 if (node->has_gimple_body_p ())
2490 /* When streaming out references to statements as part of some IPA
2491 pass summary, the statements need to have uids assigned and the
2492 following does that for all the IPA passes here. Naturally, this
2493 ordering then matches the one IPA-passes get in their stmt_fixup
2494 hooks. */
2496 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2497 renumber_gimple_stmt_uids ();
2498 pop_cfun ();
2500 if (node->definition && node->need_lto_streaming)
2501 lto_set_symtab_encoder_in_partition (encoder, node);
2504 FOR_EACH_DEFINED_FUNCTION (node)
2505 if (node->alias && node->need_lto_streaming)
2506 lto_set_symtab_encoder_in_partition (encoder, node);
2507 FOR_EACH_DEFINED_VARIABLE (vnode)
2508 if (vnode->need_lto_streaming)
2509 lto_set_symtab_encoder_in_partition (encoder, vnode);
2511 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2513 free (order);
2516 /* Same as execute_pass_list but assume that subpasses of IPA passes
2517 are local passes. If SET is not NULL, write out optimization summaries of
2518 only those node in SET. */
2520 static void
2521 ipa_write_optimization_summaries_1 (opt_pass *pass,
2522 struct lto_out_decl_state *state)
2524 while (pass)
2526 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2527 gcc_assert (!current_function_decl);
2528 gcc_assert (!cfun);
2529 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2530 if (pass->type == IPA_PASS
2531 && ipa_pass->write_optimization_summary
2532 && pass->gate (cfun))
2534 /* If a timevar is present, start it. */
2535 if (pass->tv_id)
2536 timevar_push (pass->tv_id);
2538 pass_init_dump_file (pass);
2540 current_pass = pass;
2541 ipa_pass->write_optimization_summary ();
2543 pass_fini_dump_file (pass);
2545 /* If a timevar is present, start it. */
2546 if (pass->tv_id)
2547 timevar_pop (pass->tv_id);
2550 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2551 ipa_write_optimization_summaries_1 (pass->sub, state);
2553 pass = pass->next;
2557 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2558 NULL, write out all summaries of all nodes. */
2560 void
2561 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2563 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2564 lto_symtab_encoder_iterator lsei;
2565 state->symtab_node_encoder = encoder;
2567 lto_push_out_decl_state (state);
2568 for (lsei = lsei_start_function_in_partition (encoder);
2569 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2571 struct cgraph_node *node = lsei_cgraph_node (lsei);
2572 /* When streaming out references to statements as part of some IPA
2573 pass summary, the statements need to have uids assigned.
2575 For functions newly born at WPA stage we need to initialize
2576 the uids here. */
2577 if (node->definition
2578 && gimple_has_body_p (node->decl))
2580 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2581 renumber_gimple_stmt_uids ();
2582 pop_cfun ();
2586 gcc_assert (flag_wpa);
2587 pass_manager *passes = g->get_passes ();
2588 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2590 write_lto ();
2592 gcc_assert (lto_get_out_decl_state () == state);
2593 lto_pop_out_decl_state ();
2594 lto_delete_out_decl_state (state);
2597 /* Same as execute_pass_list but assume that subpasses of IPA passes
2598 are local passes. */
2600 static void
2601 ipa_read_summaries_1 (opt_pass *pass)
2603 while (pass)
2605 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2607 gcc_assert (!current_function_decl);
2608 gcc_assert (!cfun);
2609 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2611 if (pass->gate (cfun))
2613 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2615 /* If a timevar is present, start it. */
2616 if (pass->tv_id)
2617 timevar_push (pass->tv_id);
2619 pass_init_dump_file (pass);
2621 current_pass = pass;
2622 ipa_pass->read_summary ();
2624 pass_fini_dump_file (pass);
2626 /* Stop timevar. */
2627 if (pass->tv_id)
2628 timevar_pop (pass->tv_id);
2631 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2632 ipa_read_summaries_1 (pass->sub);
2634 pass = pass->next;
2639 /* Read all the summaries for all_regular_ipa_passes. */
2641 void
2642 ipa_read_summaries (void)
2644 pass_manager *passes = g->get_passes ();
2645 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2648 /* Same as execute_pass_list but assume that subpasses of IPA passes
2649 are local passes. */
2651 static void
2652 ipa_read_optimization_summaries_1 (opt_pass *pass)
2654 while (pass)
2656 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2658 gcc_assert (!current_function_decl);
2659 gcc_assert (!cfun);
2660 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2662 if (pass->gate (cfun))
2664 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2666 /* If a timevar is present, start it. */
2667 if (pass->tv_id)
2668 timevar_push (pass->tv_id);
2670 pass_init_dump_file (pass);
2672 current_pass = pass;
2673 ipa_pass->read_optimization_summary ();
2675 pass_fini_dump_file (pass);
2677 /* Stop timevar. */
2678 if (pass->tv_id)
2679 timevar_pop (pass->tv_id);
2682 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2683 ipa_read_optimization_summaries_1 (pass->sub);
2685 pass = pass->next;
2689 /* Read all the summaries for all_regular_ipa_passes. */
2691 void
2692 ipa_read_optimization_summaries (void)
2694 pass_manager *passes = g->get_passes ();
2695 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2698 /* Same as execute_pass_list but assume that subpasses of IPA passes
2699 are local passes. */
2700 void
2701 execute_ipa_pass_list (opt_pass *pass)
2705 gcc_assert (!current_function_decl);
2706 gcc_assert (!cfun);
2707 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2708 if (execute_one_pass (pass) && pass->sub)
2710 if (pass->sub->type == GIMPLE_PASS)
2712 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2713 do_per_function_toporder ((void (*)(function *, void *))
2714 execute_pass_list,
2715 pass->sub);
2716 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2718 else if (pass->sub->type == SIMPLE_IPA_PASS
2719 || pass->sub->type == IPA_PASS)
2720 execute_ipa_pass_list (pass->sub);
2721 else
2722 gcc_unreachable ();
2724 gcc_assert (!current_function_decl);
2725 symtab->process_new_functions ();
2726 pass = pass->next;
2728 while (pass);
2731 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2733 static void
2734 execute_ipa_stmt_fixups (opt_pass *pass,
2735 struct cgraph_node *node, gimple *stmts)
2737 while (pass)
2739 /* Execute all of the IPA_PASSes in the list. */
2740 if (pass->type == IPA_PASS
2741 && pass->gate (cfun))
2743 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2745 if (ipa_pass->stmt_fixup)
2747 pass_init_dump_file (pass);
2748 /* If a timevar is present, start it. */
2749 if (pass->tv_id)
2750 timevar_push (pass->tv_id);
2752 current_pass = pass;
2753 ipa_pass->stmt_fixup (node, stmts);
2755 /* Stop timevar. */
2756 if (pass->tv_id)
2757 timevar_pop (pass->tv_id);
2758 pass_fini_dump_file (pass);
2760 if (pass->sub)
2761 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2763 pass = pass->next;
2767 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2769 void
2770 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2772 pass_manager *passes = g->get_passes ();
2773 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2777 extern void debug_properties (unsigned int);
2778 extern void dump_properties (FILE *, unsigned int);
2780 DEBUG_FUNCTION void
2781 dump_properties (FILE *dump, unsigned int props)
2783 fprintf (dump, "Properties:\n");
2784 if (props & PROP_gimple_any)
2785 fprintf (dump, "PROP_gimple_any\n");
2786 if (props & PROP_gimple_lcf)
2787 fprintf (dump, "PROP_gimple_lcf\n");
2788 if (props & PROP_gimple_leh)
2789 fprintf (dump, "PROP_gimple_leh\n");
2790 if (props & PROP_cfg)
2791 fprintf (dump, "PROP_cfg\n");
2792 if (props & PROP_ssa)
2793 fprintf (dump, "PROP_ssa\n");
2794 if (props & PROP_no_crit_edges)
2795 fprintf (dump, "PROP_no_crit_edges\n");
2796 if (props & PROP_rtl)
2797 fprintf (dump, "PROP_rtl\n");
2798 if (props & PROP_gimple_lomp)
2799 fprintf (dump, "PROP_gimple_lomp\n");
2800 if (props & PROP_gimple_lcx)
2801 fprintf (dump, "PROP_gimple_lcx\n");
2802 if (props & PROP_gimple_lvec)
2803 fprintf (dump, "PROP_gimple_lvec\n");
2804 if (props & PROP_cfglayout)
2805 fprintf (dump, "PROP_cfglayout\n");
2808 DEBUG_FUNCTION void
2809 debug_properties (unsigned int props)
2811 dump_properties (stderr, props);
2814 /* Called by local passes to see if function is called by already processed nodes.
2815 Because we process nodes in topological order, this means that function is
2816 in recursive cycle or we introduced new direct calls. */
2817 bool
2818 function_called_by_processed_nodes_p (void)
2820 struct cgraph_edge *e;
2821 for (e = cgraph_node::get (current_function_decl)->callers;
2823 e = e->next_caller)
2825 if (e->caller->decl == current_function_decl)
2826 continue;
2827 if (!e->caller->has_gimple_body_p ())
2828 continue;
2829 if (TREE_ASM_WRITTEN (e->caller->decl))
2830 continue;
2831 if (!e->caller->process && !e->caller->global.inlined_to)
2832 break;
2834 if (dump_file && e)
2836 fprintf (dump_file, "Already processed call to:\n");
2837 e->caller->dump (dump_file);
2839 return e != NULL;
2842 #include "gt-passes.h"