Only allow e500 double in SPE_SIMD_REGNO_P registers.
[official-gcc.git] / gcc / passes.c
blob428c79792b72ddc73530add6ea1c000f1e8d993a
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 "basic-block.h"
51 #include "intl.h"
52 #include "graph.h"
53 #include "regs.h"
54 #include "diagnostic-core.h"
55 #include "params.h"
56 #include "reload.h"
57 #include "debug.h"
58 #include "target.h"
59 #include "langhooks.h"
60 #include "cfgloop.h"
61 #include "hosthooks.h"
62 #include "opts.h"
63 #include "coverage.h"
64 #include "value-prof.h"
65 #include "tree-inline.h"
66 #include "tree-ssa-alias.h"
67 #include "internal-fn.h"
68 #include "gimple-expr.h"
69 #include "is-a.h"
70 #include "gimple.h"
71 #include "gimple-ssa.h"
72 #include "tree-cfg.h"
73 #include "stringpool.h"
74 #include "tree-ssanames.h"
75 #include "tree-ssa-loop-manip.h"
76 #include "tree-into-ssa.h"
77 #include "tree-dfa.h"
78 #include "tree-ssa.h"
79 #include "tree-pass.h"
80 #include "tree-dump.h"
81 #include "df.h"
82 #include "predict.h"
83 #include "lto-streamer.h"
84 #include "plugin.h"
85 #include "ipa-utils.h"
86 #include "tree-pretty-print.h" /* for dump_function_header */
87 #include "context.h"
88 #include "pass_manager.h"
89 #include "tree-ssa-live.h" /* For remove_unused_locals. */
90 #include "tree-cfgcleanup.h"
91 #include "hash-map.h"
93 using namespace gcc;
95 /* This is used for debugging. It allows the current pass to printed
96 from anywhere in compilation.
97 The variable current_pass is also used for statistics and plugins. */
98 opt_pass *current_pass;
100 static void register_pass_name (opt_pass *, const char *);
102 /* Most passes are single-instance (within their context) and thus don't
103 need to implement cloning, but passes that support multiple instances
104 *must* provide their own implementation of the clone method.
106 Handle this by providing a default implemenation, but make it a fatal
107 error to call it. */
109 opt_pass *
110 opt_pass::clone ()
112 internal_error ("pass %s does not support cloning", name);
115 bool
116 opt_pass::gate (function *)
118 return true;
121 unsigned int
122 opt_pass::execute (function *)
124 return 0;
127 opt_pass::opt_pass (const pass_data &data, context *ctxt)
128 : pass_data (data),
129 sub (NULL),
130 next (NULL),
131 static_pass_number (0),
132 m_ctxt (ctxt)
137 void
138 pass_manager::execute_early_local_passes ()
140 execute_pass_list (cfun, pass_early_local_passes_1->sub);
143 unsigned int
144 pass_manager::execute_pass_mode_switching ()
146 return pass_mode_switching_1->execute (cfun);
150 /* Call from anywhere to find out what pass this is. Useful for
151 printing out debugging information deep inside an service
152 routine. */
153 void
154 print_current_pass (FILE *file)
156 if (current_pass)
157 fprintf (file, "current pass = %s (%d)\n",
158 current_pass->name, current_pass->static_pass_number);
159 else
160 fprintf (file, "no current pass.\n");
164 /* Call from the debugger to get the current pass name. */
165 DEBUG_FUNCTION void
166 debug_pass (void)
168 print_current_pass (stderr);
173 /* Global variables used to communicate with passes. */
174 bool in_gimple_form;
175 bool first_pass_instance;
178 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
179 and TYPE_DECL nodes.
181 This does nothing for local (non-static) variables, unless the
182 variable is a register variable with DECL_ASSEMBLER_NAME set. In
183 that case, or if the variable is not an automatic, it sets up the
184 RTL and outputs any assembler code (label definition, storage
185 allocation and initialization).
187 DECL is the declaration. TOP_LEVEL is nonzero
188 if this declaration is not within a function. */
190 void
191 rest_of_decl_compilation (tree decl,
192 int top_level,
193 int at_end)
195 bool finalize = true;
197 /* We deferred calling assemble_alias so that we could collect
198 other attributes such as visibility. Emit the alias now. */
199 if (!in_lto_p)
201 tree alias;
202 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
203 if (alias)
205 alias = TREE_VALUE (TREE_VALUE (alias));
206 alias = get_identifier (TREE_STRING_POINTER (alias));
207 /* A quirk of the initial implementation of aliases required that the
208 user add "extern" to all of them. Which is silly, but now
209 historical. Do note that the symbol is in fact locally defined. */
210 DECL_EXTERNAL (decl) = 0;
211 TREE_STATIC (decl) = 1;
212 assemble_alias (decl, alias);
213 finalize = false;
217 /* Can't defer this, because it needs to happen before any
218 later function definitions are processed. */
219 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
220 make_decl_rtl (decl);
222 /* Forward declarations for nested functions are not "external",
223 but we need to treat them as if they were. */
224 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
225 || TREE_CODE (decl) == FUNCTION_DECL)
227 timevar_push (TV_VARCONST);
229 /* Don't output anything when a tentative file-scope definition
230 is seen. But at end of compilation, do output code for them.
232 We do output all variables and rely on
233 callgraph code to defer them except for forward declarations
234 (see gcc.c-torture/compile/920624-1.c) */
235 if ((at_end
236 || !DECL_DEFER_OUTPUT (decl)
237 || DECL_INITIAL (decl))
238 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
239 && !DECL_EXTERNAL (decl))
241 /* When reading LTO unit, we also read varpool, so do not
242 rebuild it. */
243 if (in_lto_p && !at_end)
245 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
246 varpool_node::finalize_decl (decl);
249 #ifdef ASM_FINISH_DECLARE_OBJECT
250 if (decl == last_assemble_variable_decl)
252 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
253 top_level, at_end);
255 #endif
257 timevar_pop (TV_VARCONST);
259 else if (TREE_CODE (decl) == TYPE_DECL
260 /* Like in rest_of_type_compilation, avoid confusing the debug
261 information machinery when there are errors. */
262 && !seen_error ())
264 timevar_push (TV_SYMOUT);
265 debug_hooks->type_decl (decl, !top_level);
266 timevar_pop (TV_SYMOUT);
269 /* Let cgraph know about the existence of variables. */
270 if (in_lto_p && !at_end)
272 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
273 && TREE_STATIC (decl))
274 varpool_node::get_create (decl);
277 /* Called after finishing a record, union or enumeral type. */
279 void
280 rest_of_type_compilation (tree type, int toplev)
282 /* Avoid confusing the debug information machinery when there are
283 errors. */
284 if (seen_error ())
285 return;
287 timevar_push (TV_SYMOUT);
288 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
289 timevar_pop (TV_SYMOUT);
294 void
295 pass_manager::
296 finish_optimization_passes (void)
298 int i;
299 struct dump_file_info *dfi;
300 char *name;
301 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
303 timevar_push (TV_DUMP);
304 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
306 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
307 end_branch_prob ();
308 dumps->dump_finish (pass_profile_1->static_pass_number);
311 if (optimize > 0)
313 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
314 print_combine_total_stats ();
315 dumps->dump_finish (pass_profile_1->static_pass_number);
318 /* Do whatever is necessary to finish printing the graphs. */
319 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
320 if (dumps->dump_initialized_p (i)
321 && (dfi->pflags & TDF_GRAPH) != 0
322 && (name = dumps->get_dump_file_name (i)) != NULL)
324 finish_graph_dump_file (name);
325 free (name);
328 timevar_pop (TV_DUMP);
331 static unsigned int
332 execute_all_early_local_passes (void)
334 /* Once this pass (and its sub-passes) are complete, all functions
335 will be in SSA form. Technically this state change is happening
336 a tad early, since the sub-passes have not yet run, but since
337 none of the sub-passes are IPA passes and do not create new
338 functions, this is ok. We're setting this value for the benefit
339 of IPA passes that follow. */
340 if (symtab->state < IPA_SSA)
341 symtab->state = IPA_SSA;
342 return 0;
345 namespace {
347 const pass_data pass_data_early_local_passes =
349 SIMPLE_IPA_PASS, /* type */
350 "early_local_cleanups", /* name */
351 OPTGROUP_NONE, /* optinfo_flags */
352 TV_EARLY_LOCAL, /* tv_id */
353 0, /* properties_required */
354 0, /* properties_provided */
355 0, /* properties_destroyed */
356 0, /* todo_flags_start */
357 /* todo_flags_finish is executed before subpases. For this reason
358 it makes no sense to remove unreachable functions here. */
359 0, /* todo_flags_finish */
362 class pass_early_local_passes : public simple_ipa_opt_pass
364 public:
365 pass_early_local_passes (gcc::context *ctxt)
366 : simple_ipa_opt_pass (pass_data_early_local_passes, ctxt)
369 /* opt_pass methods: */
370 virtual bool gate (function *)
372 /* Don't bother doing anything if the program has errors. */
373 return (!seen_error () && !in_lto_p);
376 virtual unsigned int execute (function *)
378 return execute_all_early_local_passes ();
381 }; // class pass_early_local_passes
383 } // anon namespace
385 simple_ipa_opt_pass *
386 make_pass_early_local_passes (gcc::context *ctxt)
388 return new pass_early_local_passes (ctxt);
391 namespace {
393 const pass_data pass_data_all_early_optimizations =
395 GIMPLE_PASS, /* type */
396 "early_optimizations", /* name */
397 OPTGROUP_NONE, /* optinfo_flags */
398 TV_NONE, /* tv_id */
399 0, /* properties_required */
400 0, /* properties_provided */
401 0, /* properties_destroyed */
402 0, /* todo_flags_start */
403 0, /* todo_flags_finish */
406 class pass_all_early_optimizations : public gimple_opt_pass
408 public:
409 pass_all_early_optimizations (gcc::context *ctxt)
410 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
413 /* opt_pass methods: */
414 virtual bool gate (function *)
416 return (optimize >= 1
417 /* Don't bother doing anything if the program has errors. */
418 && !seen_error ());
421 }; // class pass_all_early_optimizations
423 } // anon namespace
425 static gimple_opt_pass *
426 make_pass_all_early_optimizations (gcc::context *ctxt)
428 return new pass_all_early_optimizations (ctxt);
431 namespace {
433 const pass_data pass_data_all_optimizations =
435 GIMPLE_PASS, /* type */
436 "*all_optimizations", /* name */
437 OPTGROUP_NONE, /* optinfo_flags */
438 TV_OPTIMIZE, /* tv_id */
439 0, /* properties_required */
440 0, /* properties_provided */
441 0, /* properties_destroyed */
442 0, /* todo_flags_start */
443 0, /* todo_flags_finish */
446 class pass_all_optimizations : public gimple_opt_pass
448 public:
449 pass_all_optimizations (gcc::context *ctxt)
450 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
453 /* opt_pass methods: */
454 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
456 }; // class pass_all_optimizations
458 } // anon namespace
460 static gimple_opt_pass *
461 make_pass_all_optimizations (gcc::context *ctxt)
463 return new pass_all_optimizations (ctxt);
466 namespace {
468 const pass_data pass_data_all_optimizations_g =
470 GIMPLE_PASS, /* type */
471 "*all_optimizations_g", /* name */
472 OPTGROUP_NONE, /* optinfo_flags */
473 TV_OPTIMIZE, /* 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_optimizations_g : public gimple_opt_pass
483 public:
484 pass_all_optimizations_g (gcc::context *ctxt)
485 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
488 /* opt_pass methods: */
489 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
491 }; // class pass_all_optimizations_g
493 } // anon namespace
495 static gimple_opt_pass *
496 make_pass_all_optimizations_g (gcc::context *ctxt)
498 return new pass_all_optimizations_g (ctxt);
501 namespace {
503 const pass_data pass_data_rest_of_compilation =
505 RTL_PASS, /* type */
506 "*rest_of_compilation", /* name */
507 OPTGROUP_NONE, /* optinfo_flags */
508 TV_REST_OF_COMPILATION, /* tv_id */
509 PROP_rtl, /* properties_required */
510 0, /* properties_provided */
511 0, /* properties_destroyed */
512 0, /* todo_flags_start */
513 0, /* todo_flags_finish */
516 class pass_rest_of_compilation : public rtl_opt_pass
518 public:
519 pass_rest_of_compilation (gcc::context *ctxt)
520 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
523 /* opt_pass methods: */
524 virtual bool gate (function *)
526 /* Early return if there were errors. We can run afoul of our
527 consistency checks, and there's not really much point in fixing them. */
528 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
531 }; // class pass_rest_of_compilation
533 } // anon namespace
535 static rtl_opt_pass *
536 make_pass_rest_of_compilation (gcc::context *ctxt)
538 return new pass_rest_of_compilation (ctxt);
541 namespace {
543 const pass_data pass_data_postreload =
545 RTL_PASS, /* type */
546 "*all-postreload", /* name */
547 OPTGROUP_NONE, /* optinfo_flags */
548 TV_POSTRELOAD, /* tv_id */
549 PROP_rtl, /* properties_required */
550 0, /* properties_provided */
551 0, /* properties_destroyed */
552 0, /* todo_flags_start */
553 0, /* todo_flags_finish */
556 class pass_postreload : public rtl_opt_pass
558 public:
559 pass_postreload (gcc::context *ctxt)
560 : rtl_opt_pass (pass_data_postreload, ctxt)
563 /* opt_pass methods: */
564 virtual bool gate (function *) { return reload_completed; }
566 }; // class pass_postreload
568 } // anon namespace
570 static rtl_opt_pass *
571 make_pass_postreload (gcc::context *ctxt)
573 return new pass_postreload (ctxt);
578 /* Set the static pass number of pass PASS to ID and record that
579 in the mapping from static pass number to pass. */
581 void
582 pass_manager::
583 set_pass_for_id (int id, opt_pass *pass)
585 pass->static_pass_number = id;
586 if (passes_by_id_size <= id)
588 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
589 memset (passes_by_id + passes_by_id_size, 0,
590 (id + 1 - passes_by_id_size) * sizeof (void *));
591 passes_by_id_size = id + 1;
593 passes_by_id[id] = pass;
596 /* Return the pass with the static pass number ID. */
598 opt_pass *
599 pass_manager::get_pass_for_id (int id) const
601 if (id >= passes_by_id_size)
602 return NULL;
603 return passes_by_id[id];
606 /* Iterate over the pass tree allocating dump file numbers. We want
607 to do this depth first, and independent of whether the pass is
608 enabled or not. */
610 void
611 register_one_dump_file (opt_pass *pass)
613 g->get_passes ()->register_one_dump_file (pass);
616 void
617 pass_manager::register_one_dump_file (opt_pass *pass)
619 char *dot_name, *flag_name, *glob_name;
620 const char *name, *full_name, *prefix;
621 char num[10];
622 int flags, id;
623 int optgroup_flags = OPTGROUP_NONE;
624 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
626 /* See below in next_pass_1. */
627 num[0] = '\0';
628 if (pass->static_pass_number != -1)
629 sprintf (num, "%d", ((int) pass->static_pass_number < 0
630 ? 1 : pass->static_pass_number));
632 /* The name is both used to identify the pass for the purposes of plugins,
633 and to specify dump file name and option.
634 The latter two might want something short which is not quite unique; for
635 that reason, we may have a disambiguating prefix, followed by a space
636 to mark the start of the following dump file name / option string. */
637 name = strchr (pass->name, ' ');
638 name = name ? name + 1 : pass->name;
639 dot_name = concat (".", name, num, NULL);
640 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
642 prefix = "ipa-";
643 flags = TDF_IPA;
644 optgroup_flags |= OPTGROUP_IPA;
646 else if (pass->type == GIMPLE_PASS)
648 prefix = "tree-";
649 flags = TDF_TREE;
651 else
653 prefix = "rtl-";
654 flags = TDF_RTL;
657 flag_name = concat (prefix, name, num, NULL);
658 glob_name = concat (prefix, name, NULL);
659 optgroup_flags |= pass->optinfo_flags;
660 /* For any passes that do not have an optgroup set, and which are not
661 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
662 any dump messages are emitted properly under -fopt-info(-optall). */
663 if (optgroup_flags == OPTGROUP_NONE)
664 optgroup_flags = OPTGROUP_OTHER;
665 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
666 optgroup_flags);
667 set_pass_for_id (id, pass);
668 full_name = concat (prefix, pass->name, num, NULL);
669 register_pass_name (pass, full_name);
670 free (CONST_CAST (char *, full_name));
673 /* Register the dump files for the pass_manager starting at PASS. */
675 void
676 pass_manager::register_dump_files (opt_pass *pass)
680 if (pass->name && pass->name[0] != '*')
681 register_one_dump_file (pass);
683 if (pass->sub)
684 register_dump_files (pass->sub);
686 pass = pass->next;
688 while (pass);
691 /* Helper for pass_registry hash table. */
693 struct pass_registry_hasher : default_hashmap_traits
695 static inline hashval_t hash (const char *);
696 static inline bool equal_keys (const char *, const char *);
699 /* Pass registry hash function. */
701 inline hashval_t
702 pass_registry_hasher::hash (const char *name)
704 return htab_hash_string (name);
707 /* Hash equal function */
709 inline bool
710 pass_registry_hasher::equal_keys (const char *s1, const char *s2)
712 return !strcmp (s1, s2);
715 static hash_map<const char *, opt_pass *, pass_registry_hasher>
716 *name_to_pass_map;
718 /* Register PASS with NAME. */
720 static void
721 register_pass_name (opt_pass *pass, const char *name)
723 if (!name_to_pass_map)
724 name_to_pass_map
725 = new hash_map<const char *, opt_pass *, pass_registry_hasher> (256);
727 if (name_to_pass_map->get (name))
728 return; /* Ignore plugin passes. */
730 const char *unique_name = xstrdup (name);
731 name_to_pass_map->put (unique_name, pass);
734 /* Map from pass id to canonicalized pass name. */
736 typedef const char *char_ptr;
737 static vec<char_ptr> pass_tab = vNULL;
739 /* Callback function for traversing NAME_TO_PASS_MAP. */
741 bool
742 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
744 gcc_assert (pass->static_pass_number > 0);
745 gcc_assert (pass_tab.exists ());
747 pass_tab[pass->static_pass_number] = name;
749 return 1;
752 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
753 table for dumping purpose. */
755 static void
756 create_pass_tab (void)
758 if (!flag_dump_passes)
759 return;
761 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
762 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
765 static bool override_gate_status (opt_pass *, tree, bool);
767 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
768 is turned on or not. */
770 static void
771 dump_one_pass (opt_pass *pass, int pass_indent)
773 int indent = 3 * pass_indent;
774 const char *pn;
775 bool is_on, is_really_on;
777 is_on = pass->gate (cfun);
778 is_really_on = override_gate_status (pass, current_function_decl, is_on);
780 if (pass->static_pass_number <= 0)
781 pn = pass->name;
782 else
783 pn = pass_tab[pass->static_pass_number];
785 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
786 (15 - indent < 0 ? 0 : 15 - indent), " ",
787 is_on ? " ON" : " OFF",
788 ((!is_on) == (!is_really_on) ? ""
789 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
792 /* Dump pass list PASS with indentation INDENT. */
794 static void
795 dump_pass_list (opt_pass *pass, int indent)
799 dump_one_pass (pass, indent);
800 if (pass->sub)
801 dump_pass_list (pass->sub, indent + 1);
802 pass = pass->next;
804 while (pass);
807 /* Dump all optimization passes. */
809 void
810 dump_passes (void)
812 g->get_passes ()->dump_passes ();
815 void
816 pass_manager::dump_passes () const
818 struct cgraph_node *n, *node = NULL;
820 create_pass_tab ();
822 FOR_EACH_FUNCTION (n)
823 if (DECL_STRUCT_FUNCTION (n->decl))
825 node = n;
826 break;
829 if (!node)
830 return;
832 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
834 dump_pass_list (all_lowering_passes, 1);
835 dump_pass_list (all_small_ipa_passes, 1);
836 dump_pass_list (all_regular_ipa_passes, 1);
837 dump_pass_list (all_late_ipa_passes, 1);
838 dump_pass_list (all_passes, 1);
840 pop_cfun ();
844 /* Returns the pass with NAME. */
846 static opt_pass *
847 get_pass_by_name (const char *name)
849 opt_pass **p = name_to_pass_map->get (name);
850 if (p)
851 return *p;
853 return NULL;
857 /* Range [start, last]. */
859 struct uid_range
861 unsigned int start;
862 unsigned int last;
863 const char *assem_name;
864 struct uid_range *next;
867 typedef struct uid_range *uid_range_p;
870 static vec<uid_range_p>
871 enabled_pass_uid_range_tab = vNULL;
872 static vec<uid_range_p>
873 disabled_pass_uid_range_tab = vNULL;
876 /* Parse option string for -fdisable- and -fenable-
877 The syntax of the options:
879 -fenable-<pass_name>
880 -fdisable-<pass_name>
882 -fenable-<pass_name>=s1:e1,s2:e2,...
883 -fdisable-<pass_name>=s1:e1,s2:e2,...
886 static void
887 enable_disable_pass (const char *arg, bool is_enable)
889 opt_pass *pass;
890 char *range_str, *phase_name;
891 char *argstr = xstrdup (arg);
892 vec<uid_range_p> *tab = 0;
894 range_str = strchr (argstr,'=');
895 if (range_str)
897 *range_str = '\0';
898 range_str++;
901 phase_name = argstr;
902 if (!*phase_name)
904 if (is_enable)
905 error ("unrecognized option -fenable");
906 else
907 error ("unrecognized option -fdisable");
908 free (argstr);
909 return;
911 pass = get_pass_by_name (phase_name);
912 if (!pass || pass->static_pass_number == -1)
914 if (is_enable)
915 error ("unknown pass %s specified in -fenable", phase_name);
916 else
917 error ("unknown pass %s specified in -fdisable", phase_name);
918 free (argstr);
919 return;
922 if (is_enable)
923 tab = &enabled_pass_uid_range_tab;
924 else
925 tab = &disabled_pass_uid_range_tab;
927 if ((unsigned) pass->static_pass_number >= tab->length ())
928 tab->safe_grow_cleared (pass->static_pass_number + 1);
930 if (!range_str)
932 uid_range_p slot;
933 uid_range_p new_range = XCNEW (struct uid_range);
935 new_range->start = 0;
936 new_range->last = (unsigned)-1;
938 slot = (*tab)[pass->static_pass_number];
939 new_range->next = slot;
940 (*tab)[pass->static_pass_number] = new_range;
941 if (is_enable)
942 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
943 "of [%u, %u]", phase_name, new_range->start, new_range->last);
944 else
945 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
946 "of [%u, %u]", phase_name, new_range->start, new_range->last);
948 else
950 char *next_range = NULL;
951 char *one_range = range_str;
952 char *end_val = NULL;
956 uid_range_p slot;
957 uid_range_p new_range;
958 char *invalid = NULL;
959 long start;
960 char *func_name = NULL;
962 next_range = strchr (one_range, ',');
963 if (next_range)
965 *next_range = '\0';
966 next_range++;
969 end_val = strchr (one_range, ':');
970 if (end_val)
972 *end_val = '\0';
973 end_val++;
975 start = strtol (one_range, &invalid, 10);
976 if (*invalid || start < 0)
978 if (end_val || (one_range[0] >= '0'
979 && one_range[0] <= '9'))
981 error ("Invalid range %s in option %s",
982 one_range,
983 is_enable ? "-fenable" : "-fdisable");
984 free (argstr);
985 return;
987 func_name = one_range;
989 if (!end_val)
991 new_range = XCNEW (struct uid_range);
992 if (!func_name)
994 new_range->start = (unsigned) start;
995 new_range->last = (unsigned) start;
997 else
999 new_range->start = (unsigned) -1;
1000 new_range->last = (unsigned) -1;
1001 new_range->assem_name = xstrdup (func_name);
1004 else
1006 long last = strtol (end_val, &invalid, 10);
1007 if (*invalid || last < start)
1009 error ("Invalid range %s in option %s",
1010 end_val,
1011 is_enable ? "-fenable" : "-fdisable");
1012 free (argstr);
1013 return;
1015 new_range = XCNEW (struct uid_range);
1016 new_range->start = (unsigned) start;
1017 new_range->last = (unsigned) last;
1020 slot = (*tab)[pass->static_pass_number];
1021 new_range->next = slot;
1022 (*tab)[pass->static_pass_number] = new_range;
1023 if (is_enable)
1025 if (new_range->assem_name)
1026 inform (UNKNOWN_LOCATION,
1027 "enable pass %s for function %s",
1028 phase_name, new_range->assem_name);
1029 else
1030 inform (UNKNOWN_LOCATION,
1031 "enable pass %s for functions in the range of [%u, %u]",
1032 phase_name, new_range->start, new_range->last);
1034 else
1036 if (new_range->assem_name)
1037 inform (UNKNOWN_LOCATION,
1038 "disable pass %s for function %s",
1039 phase_name, new_range->assem_name);
1040 else
1041 inform (UNKNOWN_LOCATION,
1042 "disable pass %s for functions in the range of [%u, %u]",
1043 phase_name, new_range->start, new_range->last);
1046 one_range = next_range;
1047 } while (next_range);
1050 free (argstr);
1053 /* Enable pass specified by ARG. */
1055 void
1056 enable_pass (const char *arg)
1058 enable_disable_pass (arg, true);
1061 /* Disable pass specified by ARG. */
1063 void
1064 disable_pass (const char *arg)
1066 enable_disable_pass (arg, false);
1069 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1071 static bool
1072 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1073 tree func,
1074 vec<uid_range_p> tab)
1076 uid_range_p slot, range;
1077 int cgraph_uid;
1078 const char *aname = NULL;
1080 if (!tab.exists ()
1081 || (unsigned) pass->static_pass_number >= tab.length ()
1082 || pass->static_pass_number == -1)
1083 return false;
1085 slot = tab[pass->static_pass_number];
1086 if (!slot)
1087 return false;
1089 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1090 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1091 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1093 range = slot;
1094 while (range)
1096 if ((unsigned) cgraph_uid >= range->start
1097 && (unsigned) cgraph_uid <= range->last)
1098 return true;
1099 if (range->assem_name && aname
1100 && !strcmp (range->assem_name, aname))
1101 return true;
1102 range = range->next;
1105 return false;
1109 /* Update static_pass_number for passes (and the flag
1110 TODO_mark_first_instance).
1112 Passes are constructed with static_pass_number preinitialized to 0
1114 This field is used in two different ways: initially as instance numbers
1115 of their kind, and then as ids within the entire pass manager.
1117 Within pass_manager::pass_manager:
1119 * In add_pass_instance(), as called by next_pass_1 in
1120 NEXT_PASS in init_optimization_passes
1122 * When the initial instance of a pass within a pass manager is seen,
1123 it is flagged, and its static_pass_number is set to -1
1125 * On subsequent times that it is seen, the static pass number
1126 is decremented each time, so that if there are e.g. 4 dups,
1127 they have static_pass_number -4, 2, 3, 4 respectively (note
1128 how the initial one is negative and gives the count); these
1129 can be thought of as instance numbers of the specific pass
1131 * Within the register_dump_files () traversal, set_pass_for_id()
1132 is called on each pass, using these instance numbers to create
1133 dumpfile switches, and then overwriting them with a pass id,
1134 which are global to the whole pass manager (based on
1135 (TDI_end + current value of extra_dump_files_in_use) ) */
1137 static void
1138 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1139 opt_pass *initial_pass)
1141 /* Are we dealing with the first pass of its kind, or a clone? */
1142 if (new_pass != initial_pass)
1144 /* We're dealing with a clone. */
1145 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1147 /* Indicate to register_dump_files that this pass has duplicates,
1148 and so it should rename the dump file. The first instance will
1149 be -1, and be number of duplicates = -static_pass_number - 1.
1150 Subsequent instances will be > 0 and just the duplicate number. */
1151 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1153 initial_pass->static_pass_number -= 1;
1154 new_pass->static_pass_number = -initial_pass->static_pass_number;
1157 else
1159 /* We're dealing with the first pass of its kind. */
1160 new_pass->todo_flags_start |= TODO_mark_first_instance;
1161 new_pass->static_pass_number = -1;
1163 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1167 /* Add a pass to the pass list. Duplicate the pass if it's already
1168 in the list. */
1170 static opt_pass **
1171 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1173 /* Every pass should have a name so that plugins can refer to them. */
1174 gcc_assert (pass->name != NULL);
1176 add_pass_instance (pass, false, initial_pass);
1177 *list = pass;
1179 return &(*list)->next;
1182 /* List node for an inserted pass instance. We need to keep track of all
1183 the newly-added pass instances (with 'added_pass_nodes' defined below)
1184 so that we can register their dump files after pass-positioning is finished.
1185 Registering dumping files needs to be post-processed or the
1186 static_pass_number of the opt_pass object would be modified and mess up
1187 the dump file names of future pass instances to be added. */
1189 struct pass_list_node
1191 opt_pass *pass;
1192 struct pass_list_node *next;
1195 static struct pass_list_node *added_pass_nodes = NULL;
1196 static struct pass_list_node *prev_added_pass_node;
1198 /* Insert the pass at the proper position. Return true if the pass
1199 is successfully added.
1201 NEW_PASS_INFO - new pass to be inserted
1202 PASS_LIST - root of the pass list to insert the new pass to */
1204 static bool
1205 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1207 opt_pass *pass = *pass_list, *prev_pass = NULL;
1208 bool success = false;
1210 for ( ; pass; prev_pass = pass, pass = pass->next)
1212 /* Check if the current pass is of the same type as the new pass and
1213 matches the name and the instance number of the reference pass. */
1214 if (pass->type == new_pass_info->pass->type
1215 && pass->name
1216 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1217 && ((new_pass_info->ref_pass_instance_number == 0)
1218 || (new_pass_info->ref_pass_instance_number ==
1219 pass->static_pass_number)
1220 || (new_pass_info->ref_pass_instance_number == 1
1221 && pass->todo_flags_start & TODO_mark_first_instance)))
1223 opt_pass *new_pass;
1224 struct pass_list_node *new_pass_node;
1226 if (new_pass_info->ref_pass_instance_number == 0)
1228 new_pass = new_pass_info->pass->clone ();
1229 add_pass_instance (new_pass, true, new_pass_info->pass);
1231 else
1233 new_pass = new_pass_info->pass;
1234 add_pass_instance (new_pass, true, new_pass);
1237 /* Insert the new pass instance based on the positioning op. */
1238 switch (new_pass_info->pos_op)
1240 case PASS_POS_INSERT_AFTER:
1241 new_pass->next = pass->next;
1242 pass->next = new_pass;
1244 /* Skip newly inserted pass to avoid repeated
1245 insertions in the case where the new pass and the
1246 existing one have the same name. */
1247 pass = new_pass;
1248 break;
1249 case PASS_POS_INSERT_BEFORE:
1250 new_pass->next = pass;
1251 if (prev_pass)
1252 prev_pass->next = new_pass;
1253 else
1254 *pass_list = new_pass;
1255 break;
1256 case PASS_POS_REPLACE:
1257 new_pass->next = pass->next;
1258 if (prev_pass)
1259 prev_pass->next = new_pass;
1260 else
1261 *pass_list = new_pass;
1262 new_pass->sub = pass->sub;
1263 new_pass->tv_id = pass->tv_id;
1264 pass = new_pass;
1265 break;
1266 default:
1267 error ("invalid pass positioning operation");
1268 return false;
1271 /* Save the newly added pass (instance) in the added_pass_nodes
1272 list so that we can register its dump file later. Note that
1273 we cannot register the dump file now because doing so will modify
1274 the static_pass_number of the opt_pass object and therefore
1275 mess up the dump file name of future instances. */
1276 new_pass_node = XCNEW (struct pass_list_node);
1277 new_pass_node->pass = new_pass;
1278 if (!added_pass_nodes)
1279 added_pass_nodes = new_pass_node;
1280 else
1281 prev_added_pass_node->next = new_pass_node;
1282 prev_added_pass_node = new_pass_node;
1284 success = true;
1287 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1288 success = true;
1291 return success;
1294 /* Hooks a new pass into the pass lists.
1296 PASS_INFO - pass information that specifies the opt_pass object,
1297 reference pass, instance number, and how to position
1298 the pass */
1300 void
1301 register_pass (struct register_pass_info *pass_info)
1303 g->get_passes ()->register_pass (pass_info);
1306 void
1307 register_pass (opt_pass* pass, pass_positioning_ops pos,
1308 const char* ref_pass_name, int ref_pass_inst_number)
1310 register_pass_info i;
1311 i.pass = pass;
1312 i.reference_pass_name = ref_pass_name;
1313 i.ref_pass_instance_number = ref_pass_inst_number;
1314 i.pos_op = pos;
1316 g->get_passes ()->register_pass (&i);
1319 void
1320 pass_manager::register_pass (struct register_pass_info *pass_info)
1322 bool all_instances, success;
1323 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1325 /* The checks below could fail in buggy plugins. Existing GCC
1326 passes should never fail these checks, so we mention plugin in
1327 the messages. */
1328 if (!pass_info->pass)
1329 fatal_error ("plugin cannot register a missing pass");
1331 if (!pass_info->pass->name)
1332 fatal_error ("plugin cannot register an unnamed pass");
1334 if (!pass_info->reference_pass_name)
1335 fatal_error
1336 ("plugin cannot register pass %qs without reference pass name",
1337 pass_info->pass->name);
1339 /* Try to insert the new pass to the pass lists. We need to check
1340 all five lists as the reference pass could be in one (or all) of
1341 them. */
1342 all_instances = pass_info->ref_pass_instance_number == 0;
1343 success = position_pass (pass_info, &all_lowering_passes);
1344 if (!success || all_instances)
1345 success |= position_pass (pass_info, &all_small_ipa_passes);
1346 if (!success || all_instances)
1347 success |= position_pass (pass_info, &all_regular_ipa_passes);
1348 if (!success || all_instances)
1349 success |= position_pass (pass_info, &all_late_ipa_passes);
1350 if (!success || all_instances)
1351 success |= position_pass (pass_info, &all_passes);
1352 if (!success)
1353 fatal_error
1354 ("pass %qs not found but is referenced by new pass %qs",
1355 pass_info->reference_pass_name, pass_info->pass->name);
1357 /* OK, we have successfully inserted the new pass. We need to register
1358 the dump files for the newly added pass and its duplicates (if any).
1359 Because the registration of plugin/backend passes happens after the
1360 command-line options are parsed, the options that specify single
1361 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1362 passes. Therefore we currently can only enable dumping of
1363 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1364 are specified. While doing so, we also delete the pass_list_node
1365 objects created during pass positioning. */
1366 while (added_pass_nodes)
1368 struct pass_list_node *next_node = added_pass_nodes->next;
1369 enum tree_dump_index tdi;
1370 register_one_dump_file (added_pass_nodes->pass);
1371 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1372 || added_pass_nodes->pass->type == IPA_PASS)
1373 tdi = TDI_ipa_all;
1374 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1375 tdi = TDI_tree_all;
1376 else
1377 tdi = TDI_rtl_all;
1378 /* Check if dump-all flag is specified. */
1379 if (dumps->get_dump_file_info (tdi)->pstate)
1380 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1381 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1382 XDELETE (added_pass_nodes);
1383 added_pass_nodes = next_node;
1387 /* Construct the pass tree. The sequencing of passes is driven by
1388 the cgraph routines:
1390 finalize_compilation_unit ()
1391 for each node N in the cgraph
1392 cgraph_analyze_function (N)
1393 cgraph_lower_function (N) -> all_lowering_passes
1395 If we are optimizing, compile is then invoked:
1397 compile ()
1398 ipa_passes () -> all_small_ipa_passes
1399 -> Analysis of all_regular_ipa_passes
1400 * possible LTO streaming at copmilation time *
1401 -> Execution of all_regular_ipa_passes
1402 * possible LTO streaming at link time *
1403 -> all_late_ipa_passes
1404 expand_all_functions ()
1405 for each node N in the cgraph
1406 expand_function (N) -> Transformation of all_regular_ipa_passes
1407 -> all_passes
1410 void *
1411 pass_manager::operator new (size_t sz)
1413 /* Ensure that all fields of the pass manager are zero-initialized. */
1414 return xcalloc (1, sz);
1417 pass_manager::pass_manager (context *ctxt)
1418 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1419 all_regular_ipa_passes (NULL),
1420 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1421 m_ctxt (ctxt)
1423 opt_pass **p;
1425 /* Initialize the pass_lists array. */
1426 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1427 GCC_PASS_LISTS
1428 #undef DEF_PASS_LIST
1430 /* Build the tree of passes. */
1432 #define INSERT_PASSES_AFTER(PASS) \
1433 p = &(PASS);
1435 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1437 opt_pass **p = &(PASS ## _1)->sub;
1439 #define POP_INSERT_PASSES() \
1442 #define NEXT_PASS(PASS, NUM) \
1443 do { \
1444 gcc_assert (NULL == PASS ## _ ## NUM); \
1445 if ((NUM) == 1) \
1446 PASS ## _1 = make_##PASS (m_ctxt); \
1447 else \
1449 gcc_assert (PASS ## _1); \
1450 PASS ## _ ## NUM = PASS ## _1->clone (); \
1452 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1453 } while (0)
1455 #define TERMINATE_PASS_LIST() \
1456 *p = NULL;
1458 #include "pass-instances.def"
1460 #undef INSERT_PASSES_AFTER
1461 #undef PUSH_INSERT_PASSES_WITHIN
1462 #undef POP_INSERT_PASSES
1463 #undef NEXT_PASS
1464 #undef TERMINATE_PASS_LIST
1466 /* Register the passes with the tree dump code. */
1467 register_dump_files (all_lowering_passes);
1468 register_dump_files (all_small_ipa_passes);
1469 register_dump_files (all_regular_ipa_passes);
1470 register_dump_files (all_late_ipa_passes);
1471 register_dump_files (all_passes);
1474 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1475 function CALLBACK for every function in the call graph. Otherwise,
1476 call CALLBACK on the current function. */
1478 static void
1479 do_per_function (void (*callback) (function *, void *data), void *data)
1481 if (current_function_decl)
1482 callback (cfun, data);
1483 else
1485 struct cgraph_node *node;
1486 FOR_EACH_DEFINED_FUNCTION (node)
1487 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1488 && (!node->clone_of || node->decl != node->clone_of->decl))
1489 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1493 /* Because inlining might remove no-longer reachable nodes, we need to
1494 keep the array visible to garbage collector to avoid reading collected
1495 out nodes. */
1496 static int nnodes;
1497 static GTY ((length ("nnodes"))) cgraph_node **order;
1499 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1500 function CALLBACK for every function in the call graph. Otherwise,
1501 call CALLBACK on the current function.
1502 This function is global so that plugins can use it. */
1503 void
1504 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1506 int i;
1508 if (current_function_decl)
1509 callback (cfun, data);
1510 else
1512 gcc_assert (!order);
1513 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1514 nnodes = ipa_reverse_postorder (order);
1515 for (i = nnodes - 1; i >= 0; i--)
1516 order[i]->process = 1;
1517 for (i = nnodes - 1; i >= 0; i--)
1519 struct cgraph_node *node = order[i];
1521 /* Allow possibly removed nodes to be garbage collected. */
1522 order[i] = NULL;
1523 node->process = 0;
1524 if (node->has_gimple_body_p ())
1525 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1528 ggc_free (order);
1529 order = NULL;
1530 nnodes = 0;
1533 /* Helper function to perform function body dump. */
1535 static void
1536 execute_function_dump (function *fn, void *data)
1538 opt_pass *pass = (opt_pass *)data;
1540 if (dump_file)
1542 push_cfun (fn);
1544 if (fn->curr_properties & PROP_trees)
1545 dump_function_to_file (fn->decl, dump_file, dump_flags);
1546 else
1547 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1549 /* Flush the file. If verification fails, we won't be able to
1550 close the file before aborting. */
1551 fflush (dump_file);
1553 if ((fn->curr_properties & PROP_cfg)
1554 && (dump_flags & TDF_GRAPH))
1556 if (!pass->graph_dump_initialized)
1558 clean_graph_dump_file (dump_file_name);
1559 pass->graph_dump_initialized = true;
1561 print_graph_cfg (dump_file_name, fn);
1564 pop_cfun ();
1568 static struct profile_record *profile_record;
1570 /* Do profile consistency book-keeping for the pass with static number INDEX.
1571 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1572 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1573 if we are only book-keeping on passes that may have selectively disabled
1574 themselves on a given function. */
1575 static void
1576 check_profile_consistency (int index, int subpass, bool run)
1578 pass_manager *passes = g->get_passes ();
1579 if (index == -1)
1580 return;
1581 if (!profile_record)
1582 profile_record = XCNEWVEC (struct profile_record,
1583 passes->passes_by_id_size);
1584 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1585 gcc_assert (subpass < 2);
1586 profile_record[index].run |= run;
1587 account_profile_record (&profile_record[index], subpass);
1590 /* Output profile consistency. */
1592 void
1593 dump_profile_report (void)
1595 g->get_passes ()->dump_profile_report ();
1598 void
1599 pass_manager::dump_profile_report () const
1601 int i, j;
1602 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1603 gcov_type last_time = 0, last_size = 0;
1604 double rel_time_change, rel_size_change;
1605 int last_reported = 0;
1607 if (!profile_record)
1608 return;
1609 fprintf (stderr, "\nProfile consistency report:\n\n");
1610 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1611 fprintf (stderr, " |freq count |freq count |size time\n");
1613 for (i = 0; i < passes_by_id_size; i++)
1614 for (j = 0 ; j < 2; j++)
1615 if (profile_record[i].run)
1617 if (last_time)
1618 rel_time_change = (profile_record[i].time[j]
1619 - (double)last_time) * 100 / (double)last_time;
1620 else
1621 rel_time_change = 0;
1622 if (last_size)
1623 rel_size_change = (profile_record[i].size[j]
1624 - (double)last_size) * 100 / (double)last_size;
1625 else
1626 rel_size_change = 0;
1628 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1629 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1630 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1631 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1632 || rel_time_change || rel_size_change)
1634 last_reported = i;
1635 fprintf (stderr, "%-20s %s",
1636 passes_by_id [i]->name,
1637 j ? "(after TODO)" : " ");
1638 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1639 fprintf (stderr, "| %+5i",
1640 profile_record[i].num_mismatched_freq_in[j]
1641 - last_freq_in);
1642 else
1643 fprintf (stderr, "| ");
1644 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1645 fprintf (stderr, " %+5i",
1646 profile_record[i].num_mismatched_count_in[j]
1647 - last_count_in);
1648 else
1649 fprintf (stderr, " ");
1650 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1651 fprintf (stderr, "| %+5i",
1652 profile_record[i].num_mismatched_freq_out[j]
1653 - last_freq_out);
1654 else
1655 fprintf (stderr, "| ");
1656 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1657 fprintf (stderr, " %+5i",
1658 profile_record[i].num_mismatched_count_out[j]
1659 - last_count_out);
1660 else
1661 fprintf (stderr, " ");
1663 /* Size/time units change across gimple and RTL. */
1664 if (i == pass_expand_1->static_pass_number)
1665 fprintf (stderr, "|----------");
1666 else
1668 if (rel_size_change)
1669 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1670 else
1671 fprintf (stderr, "| ");
1672 if (rel_time_change)
1673 fprintf (stderr, " %+8.4f%%", rel_time_change);
1675 fprintf (stderr, "\n");
1676 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1677 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1678 last_count_in = profile_record[i].num_mismatched_count_in[j];
1679 last_count_out = profile_record[i].num_mismatched_count_out[j];
1681 else if (j && last_reported != i)
1683 last_reported = i;
1684 fprintf (stderr, "%-20s ------------| | |\n",
1685 passes_by_id [i]->name);
1687 last_time = profile_record[i].time[j];
1688 last_size = profile_record[i].size[j];
1692 /* Perform all TODO actions that ought to be done on each function. */
1694 static void
1695 execute_function_todo (function *fn, void *data)
1697 bool from_ipa_pass = (cfun == NULL);
1698 unsigned int flags = (size_t)data;
1699 flags &= ~fn->last_verified;
1700 if (!flags)
1701 return;
1703 push_cfun (fn);
1705 /* Always cleanup the CFG before trying to update SSA. */
1706 if (flags & TODO_cleanup_cfg)
1708 cleanup_tree_cfg ();
1710 /* When cleanup_tree_cfg merges consecutive blocks, it may
1711 perform some simplistic propagation when removing single
1712 valued PHI nodes. This propagation may, in turn, cause the
1713 SSA form to become out-of-date (see PR 22037). So, even
1714 if the parent pass had not scheduled an SSA update, we may
1715 still need to do one. */
1716 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1717 flags |= TODO_update_ssa;
1720 if (flags & TODO_update_ssa_any)
1722 unsigned update_flags = flags & TODO_update_ssa_any;
1723 update_ssa (update_flags);
1726 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1727 compute_may_aliases ();
1729 if (optimize && (flags & TODO_update_address_taken))
1730 execute_update_addresses_taken ();
1732 if (flags & TODO_remove_unused_locals)
1733 remove_unused_locals ();
1735 if (flags & TODO_rebuild_frequencies)
1736 rebuild_frequencies ();
1738 if (flags & TODO_rebuild_cgraph_edges)
1739 cgraph_edge::rebuild_edges ();
1741 /* If we've seen errors do not bother running any verifiers. */
1742 if (!seen_error ())
1744 #if defined ENABLE_CHECKING
1745 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1746 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1748 if (flags & TODO_verify_il)
1750 if (cfun->curr_properties & PROP_trees)
1752 if (cfun->curr_properties & PROP_cfg)
1753 /* IPA passes leave stmts to be fixed up, so make sure to
1754 not verify stmts really throw. */
1755 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1756 else
1757 verify_gimple_in_seq (gimple_body (cfun->decl));
1759 if (cfun->curr_properties & PROP_ssa)
1760 /* IPA passes leave stmts to be fixed up, so make sure to
1761 not verify SSA operands whose verifier will choke on that. */
1762 verify_ssa (true, !from_ipa_pass);
1763 /* IPA passes leave basic-blocks unsplit, so make sure to
1764 not trip on that. */
1765 if ((cfun->curr_properties & PROP_cfg)
1766 && !from_ipa_pass)
1767 verify_flow_info ();
1768 if (current_loops
1769 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1770 verify_loop_closed_ssa (false);
1771 if (cfun->curr_properties & PROP_rtl)
1772 verify_rtl_sharing ();
1775 /* Make sure verifiers don't change dominator state. */
1776 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1777 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1778 #endif
1781 fn->last_verified = flags & TODO_verify_all;
1783 pop_cfun ();
1785 /* For IPA passes make sure to release dominator info, it can be
1786 computed by non-verifying TODOs. */
1787 if (from_ipa_pass)
1789 free_dominance_info (fn, CDI_DOMINATORS);
1790 free_dominance_info (fn, CDI_POST_DOMINATORS);
1794 /* Perform all TODO actions. */
1795 static void
1796 execute_todo (unsigned int flags)
1798 #if defined ENABLE_CHECKING
1799 if (cfun
1800 && need_ssa_update_p (cfun))
1801 gcc_assert (flags & TODO_update_ssa_any);
1802 #endif
1804 timevar_push (TV_TODO);
1806 /* Inform the pass whether it is the first time it is run. */
1807 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1809 statistics_fini_pass ();
1811 if (flags)
1812 do_per_function (execute_function_todo, (void *)(size_t) flags);
1814 /* Always remove functions just as before inlining: IPA passes might be
1815 interested to see bodies of extern inline functions that are not inlined
1816 to analyze side effects. The full removal is done just at the end
1817 of IPA pass queue. */
1818 if (flags & TODO_remove_functions)
1820 gcc_assert (!cfun);
1821 symtab->remove_unreachable_nodes (true, dump_file);
1824 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1826 gcc_assert (!cfun);
1827 symtab_node::dump_table (dump_file);
1828 /* Flush the file. If verification fails, we won't be able to
1829 close the file before aborting. */
1830 fflush (dump_file);
1833 /* Now that the dumping has been done, we can get rid of the optional
1834 df problems. */
1835 if (flags & TODO_df_finish)
1836 df_finish_pass ((flags & TODO_df_verify) != 0);
1838 timevar_pop (TV_TODO);
1841 /* Verify invariants that should hold between passes. This is a place
1842 to put simple sanity checks. */
1844 static void
1845 verify_interpass_invariants (void)
1847 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1850 /* Clear the last verified flag. */
1852 static void
1853 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
1855 fn->last_verified = 0;
1858 /* Helper function. Verify that the properties has been turn into the
1859 properties expected by the pass. */
1861 #ifdef ENABLE_CHECKING
1862 static void
1863 verify_curr_properties (function *fn, void *data)
1865 unsigned int props = (size_t)data;
1866 gcc_assert ((fn->curr_properties & props) == props);
1868 #endif
1870 /* Initialize pass dump file. */
1871 /* This is non-static so that the plugins can use it. */
1873 bool
1874 pass_init_dump_file (opt_pass *pass)
1876 /* If a dump file name is present, open it if enabled. */
1877 if (pass->static_pass_number != -1)
1879 timevar_push (TV_DUMP);
1880 gcc::dump_manager *dumps = g->get_dumps ();
1881 bool initializing_dump =
1882 !dumps->dump_initialized_p (pass->static_pass_number);
1883 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
1884 dumps->dump_start (pass->static_pass_number, &dump_flags);
1885 if (dump_file && current_function_decl)
1886 dump_function_header (dump_file, current_function_decl, dump_flags);
1887 if (initializing_dump
1888 && dump_file && (dump_flags & TDF_GRAPH)
1889 && cfun && (cfun->curr_properties & PROP_cfg))
1891 clean_graph_dump_file (dump_file_name);
1892 pass->graph_dump_initialized = true;
1894 timevar_pop (TV_DUMP);
1895 return initializing_dump;
1897 else
1898 return false;
1901 /* Flush PASS dump file. */
1902 /* This is non-static so that plugins can use it. */
1904 void
1905 pass_fini_dump_file (opt_pass *pass)
1907 timevar_push (TV_DUMP);
1909 /* Flush and close dump file. */
1910 if (dump_file_name)
1912 free (CONST_CAST (char *, dump_file_name));
1913 dump_file_name = NULL;
1916 g->get_dumps ()->dump_finish (pass->static_pass_number);
1917 timevar_pop (TV_DUMP);
1920 /* After executing the pass, apply expected changes to the function
1921 properties. */
1923 static void
1924 update_properties_after_pass (function *fn, void *data)
1926 opt_pass *pass = (opt_pass *) data;
1927 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
1928 & ~pass->properties_destroyed;
1931 /* Execute summary generation for all of the passes in IPA_PASS. */
1933 void
1934 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
1936 while (ipa_pass)
1938 opt_pass *pass = ipa_pass;
1940 /* Execute all of the IPA_PASSes in the list. */
1941 if (ipa_pass->type == IPA_PASS
1942 && pass->gate (cfun)
1943 && ipa_pass->generate_summary)
1945 pass_init_dump_file (pass);
1947 /* If a timevar is present, start it. */
1948 if (pass->tv_id)
1949 timevar_push (pass->tv_id);
1951 current_pass = pass;
1952 ipa_pass->generate_summary ();
1954 /* Stop timevar. */
1955 if (pass->tv_id)
1956 timevar_pop (pass->tv_id);
1958 pass_fini_dump_file (pass);
1960 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
1964 /* Execute IPA_PASS function transform on NODE. */
1966 static void
1967 execute_one_ipa_transform_pass (struct cgraph_node *node,
1968 ipa_opt_pass_d *ipa_pass)
1970 opt_pass *pass = ipa_pass;
1971 unsigned int todo_after = 0;
1973 current_pass = pass;
1974 if (!ipa_pass->function_transform)
1975 return;
1977 /* Note that the folders should only create gimple expressions.
1978 This is a hack until the new folder is ready. */
1979 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1981 pass_init_dump_file (pass);
1983 /* Run pre-pass verification. */
1984 execute_todo (ipa_pass->function_transform_todo_flags_start);
1986 /* If a timevar is present, start it. */
1987 if (pass->tv_id != TV_NONE)
1988 timevar_push (pass->tv_id);
1990 /* Do it! */
1991 todo_after = ipa_pass->function_transform (node);
1993 /* Stop timevar. */
1994 if (pass->tv_id != TV_NONE)
1995 timevar_pop (pass->tv_id);
1997 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
1998 check_profile_consistency (pass->static_pass_number, 0, true);
2000 /* Run post-pass cleanup and verification. */
2001 execute_todo (todo_after);
2002 verify_interpass_invariants ();
2003 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2004 check_profile_consistency (pass->static_pass_number, 1, true);
2006 if (dump_file)
2007 do_per_function (execute_function_dump, NULL);
2008 pass_fini_dump_file (pass);
2010 current_pass = NULL;
2012 /* Signal this is a suitable GC collection point. */
2013 if (!(todo_after & TODO_do_not_ggc_collect))
2014 ggc_collect ();
2017 /* For the current function, execute all ipa transforms. */
2019 void
2020 execute_all_ipa_transforms (void)
2022 struct cgraph_node *node;
2023 if (!cfun)
2024 return;
2025 node = cgraph_node::get (current_function_decl);
2027 if (node->ipa_transforms_to_apply.exists ())
2029 unsigned int i;
2031 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2032 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2033 node->ipa_transforms_to_apply.release ();
2037 /* Check if PASS is explicitly disabled or enabled and return
2038 the gate status. FUNC is the function to be processed, and
2039 GATE_STATUS is the gate status determined by pass manager by
2040 default. */
2042 static bool
2043 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2045 bool explicitly_enabled = false;
2046 bool explicitly_disabled = false;
2048 explicitly_enabled
2049 = is_pass_explicitly_enabled_or_disabled (pass, func,
2050 enabled_pass_uid_range_tab);
2051 explicitly_disabled
2052 = is_pass_explicitly_enabled_or_disabled (pass, func,
2053 disabled_pass_uid_range_tab);
2055 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2057 return gate_status;
2061 /* Execute PASS. */
2063 bool
2064 execute_one_pass (opt_pass *pass)
2066 unsigned int todo_after = 0;
2068 bool gate_status;
2070 /* IPA passes are executed on whole program, so cfun should be NULL.
2071 Other passes need function context set. */
2072 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2073 gcc_assert (!cfun && !current_function_decl);
2074 else
2075 gcc_assert (cfun && current_function_decl);
2077 current_pass = pass;
2079 /* Check whether gate check should be avoided.
2080 User controls the value of the gate through the parameter "gate_status". */
2081 gate_status = pass->gate (cfun);
2082 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2084 /* Override gate with plugin. */
2085 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2087 if (!gate_status)
2089 /* Run so passes selectively disabling themselves on a given function
2090 are not miscounted. */
2091 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2093 check_profile_consistency (pass->static_pass_number, 0, false);
2094 check_profile_consistency (pass->static_pass_number, 1, false);
2096 current_pass = NULL;
2097 return false;
2100 /* Pass execution event trigger: useful to identify passes being
2101 executed. */
2102 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2104 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2105 Apply all trnasforms first. */
2106 if (pass->type == SIMPLE_IPA_PASS)
2108 struct cgraph_node *node;
2109 bool applied = false;
2110 FOR_EACH_DEFINED_FUNCTION (node)
2111 if (node->analyzed
2112 && node->has_gimple_body_p ()
2113 && (!node->clone_of || node->decl != node->clone_of->decl))
2115 if (!node->global.inlined_to
2116 && node->ipa_transforms_to_apply.exists ())
2118 node->get_body ();
2119 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2120 execute_all_ipa_transforms ();
2121 cgraph_edge::rebuild_edges ();
2122 free_dominance_info (CDI_DOMINATORS);
2123 free_dominance_info (CDI_POST_DOMINATORS);
2124 pop_cfun ();
2125 applied = true;
2128 if (applied)
2129 symtab->remove_unreachable_nodes (false, dump_file);
2130 /* Restore current_pass. */
2131 current_pass = pass;
2134 if (!quiet_flag && !cfun)
2135 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2137 /* Note that the folders should only create gimple expressions.
2138 This is a hack until the new folder is ready. */
2139 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2141 pass_init_dump_file (pass);
2143 /* Run pre-pass verification. */
2144 execute_todo (pass->todo_flags_start);
2146 #ifdef ENABLE_CHECKING
2147 do_per_function (verify_curr_properties,
2148 (void *)(size_t)pass->properties_required);
2149 #endif
2151 /* If a timevar is present, start it. */
2152 if (pass->tv_id != TV_NONE)
2153 timevar_push (pass->tv_id);
2155 /* Do it! */
2156 todo_after = pass->execute (cfun);
2157 do_per_function (clear_last_verified, NULL);
2159 /* Stop timevar. */
2160 if (pass->tv_id != TV_NONE)
2161 timevar_pop (pass->tv_id);
2163 do_per_function (update_properties_after_pass, pass);
2165 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2166 check_profile_consistency (pass->static_pass_number, 0, true);
2168 /* Run post-pass cleanup and verification. */
2169 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2170 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2171 check_profile_consistency (pass->static_pass_number, 1, true);
2173 verify_interpass_invariants ();
2174 if (dump_file)
2175 do_per_function (execute_function_dump, pass);
2176 if (pass->type == IPA_PASS)
2178 struct cgraph_node *node;
2179 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2180 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2183 if (!current_function_decl)
2184 symtab->process_new_functions ();
2186 pass_fini_dump_file (pass);
2188 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2189 gcc_assert (!(cfun->curr_properties & PROP_trees)
2190 || pass->type != RTL_PASS);
2192 current_pass = NULL;
2194 /* Signal this is a suitable GC collection point. */
2195 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2196 ggc_collect ();
2198 return true;
2201 static void
2202 execute_pass_list_1 (opt_pass *pass)
2206 gcc_assert (pass->type == GIMPLE_PASS
2207 || pass->type == RTL_PASS);
2208 if (execute_one_pass (pass) && pass->sub)
2209 execute_pass_list_1 (pass->sub);
2210 pass = pass->next;
2212 while (pass);
2215 void
2216 execute_pass_list (function *fn, opt_pass *pass)
2218 push_cfun (fn);
2219 execute_pass_list_1 (pass);
2220 if (fn->cfg)
2222 free_dominance_info (CDI_DOMINATORS);
2223 free_dominance_info (CDI_POST_DOMINATORS);
2225 pop_cfun ();
2228 /* Write out all LTO data. */
2229 static void
2230 write_lto (void)
2232 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2233 lto_output ();
2234 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2235 timevar_push (TV_IPA_LTO_DECL_OUT);
2236 produce_asm_for_decls ();
2237 timevar_pop (TV_IPA_LTO_DECL_OUT);
2240 /* Same as execute_pass_list but assume that subpasses of IPA passes
2241 are local passes. If SET is not NULL, write out summaries of only
2242 those node in SET. */
2244 static void
2245 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2247 while (pass)
2249 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2250 gcc_assert (!current_function_decl);
2251 gcc_assert (!cfun);
2252 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2253 if (pass->type == IPA_PASS
2254 && ipa_pass->write_summary
2255 && pass->gate (cfun))
2257 /* If a timevar is present, start it. */
2258 if (pass->tv_id)
2259 timevar_push (pass->tv_id);
2261 pass_init_dump_file (pass);
2263 current_pass = pass;
2264 ipa_pass->write_summary ();
2266 pass_fini_dump_file (pass);
2268 /* If a timevar is present, start it. */
2269 if (pass->tv_id)
2270 timevar_pop (pass->tv_id);
2273 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2274 ipa_write_summaries_2 (pass->sub, state);
2276 pass = pass->next;
2280 /* Helper function of ipa_write_summaries. Creates and destroys the
2281 decl state and calls ipa_write_summaries_2 for all passes that have
2282 summaries. SET is the set of nodes to be written. */
2284 static void
2285 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2287 pass_manager *passes = g->get_passes ();
2288 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2289 state->symtab_node_encoder = encoder;
2291 lto_push_out_decl_state (state);
2293 gcc_assert (!flag_wpa);
2294 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2296 write_lto ();
2298 gcc_assert (lto_get_out_decl_state () == state);
2299 lto_pop_out_decl_state ();
2300 lto_delete_out_decl_state (state);
2303 /* Write out summaries for all the nodes in the callgraph. */
2305 void
2306 ipa_write_summaries (void)
2308 lto_symtab_encoder_t encoder;
2309 int i, order_pos;
2310 varpool_node *vnode;
2311 struct cgraph_node *node;
2312 struct cgraph_node **order;
2314 if (!flag_generate_lto || seen_error ())
2315 return;
2317 encoder = lto_symtab_encoder_new (false);
2319 /* Create the callgraph set in the same order used in
2320 cgraph_expand_all_functions. This mostly facilitates debugging,
2321 since it causes the gimple file to be processed in the same order
2322 as the source code. */
2323 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2324 order_pos = ipa_reverse_postorder (order);
2325 gcc_assert (order_pos == symtab->cgraph_count);
2327 for (i = order_pos - 1; i >= 0; i--)
2329 struct cgraph_node *node = order[i];
2331 if (node->has_gimple_body_p ())
2333 /* When streaming out references to statements as part of some IPA
2334 pass summary, the statements need to have uids assigned and the
2335 following does that for all the IPA passes here. Naturally, this
2336 ordering then matches the one IPA-passes get in their stmt_fixup
2337 hooks. */
2339 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2340 renumber_gimple_stmt_uids ();
2341 pop_cfun ();
2343 if (node->definition)
2344 lto_set_symtab_encoder_in_partition (encoder, node);
2347 FOR_EACH_DEFINED_FUNCTION (node)
2348 if (node->alias)
2349 lto_set_symtab_encoder_in_partition (encoder, node);
2350 FOR_EACH_DEFINED_VARIABLE (vnode)
2351 lto_set_symtab_encoder_in_partition (encoder, vnode);
2353 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2355 free (order);
2358 /* Same as execute_pass_list but assume that subpasses of IPA passes
2359 are local passes. If SET is not NULL, write out optimization summaries of
2360 only those node in SET. */
2362 static void
2363 ipa_write_optimization_summaries_1 (opt_pass *pass,
2364 struct lto_out_decl_state *state)
2366 while (pass)
2368 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2369 gcc_assert (!current_function_decl);
2370 gcc_assert (!cfun);
2371 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2372 if (pass->type == IPA_PASS
2373 && ipa_pass->write_optimization_summary
2374 && pass->gate (cfun))
2376 /* If a timevar is present, start it. */
2377 if (pass->tv_id)
2378 timevar_push (pass->tv_id);
2380 pass_init_dump_file (pass);
2382 current_pass = pass;
2383 ipa_pass->write_optimization_summary ();
2385 pass_fini_dump_file (pass);
2387 /* If a timevar is present, start it. */
2388 if (pass->tv_id)
2389 timevar_pop (pass->tv_id);
2392 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2393 ipa_write_optimization_summaries_1 (pass->sub, state);
2395 pass = pass->next;
2399 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2400 NULL, write out all summaries of all nodes. */
2402 void
2403 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2405 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2406 lto_symtab_encoder_iterator lsei;
2407 state->symtab_node_encoder = encoder;
2409 lto_push_out_decl_state (state);
2410 for (lsei = lsei_start_function_in_partition (encoder);
2411 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2413 struct cgraph_node *node = lsei_cgraph_node (lsei);
2414 /* When streaming out references to statements as part of some IPA
2415 pass summary, the statements need to have uids assigned.
2417 For functions newly born at WPA stage we need to initialize
2418 the uids here. */
2419 if (node->definition
2420 && gimple_has_body_p (node->decl))
2422 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2423 renumber_gimple_stmt_uids ();
2424 pop_cfun ();
2428 gcc_assert (flag_wpa);
2429 pass_manager *passes = g->get_passes ();
2430 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2432 write_lto ();
2434 gcc_assert (lto_get_out_decl_state () == state);
2435 lto_pop_out_decl_state ();
2436 lto_delete_out_decl_state (state);
2439 /* Same as execute_pass_list but assume that subpasses of IPA passes
2440 are local passes. */
2442 static void
2443 ipa_read_summaries_1 (opt_pass *pass)
2445 while (pass)
2447 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2449 gcc_assert (!current_function_decl);
2450 gcc_assert (!cfun);
2451 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2453 if (pass->gate (cfun))
2455 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2457 /* If a timevar is present, start it. */
2458 if (pass->tv_id)
2459 timevar_push (pass->tv_id);
2461 pass_init_dump_file (pass);
2463 current_pass = pass;
2464 ipa_pass->read_summary ();
2466 pass_fini_dump_file (pass);
2468 /* Stop timevar. */
2469 if (pass->tv_id)
2470 timevar_pop (pass->tv_id);
2473 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2474 ipa_read_summaries_1 (pass->sub);
2476 pass = pass->next;
2481 /* Read all the summaries for all_regular_ipa_passes. */
2483 void
2484 ipa_read_summaries (void)
2486 pass_manager *passes = g->get_passes ();
2487 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2490 /* Same as execute_pass_list but assume that subpasses of IPA passes
2491 are local passes. */
2493 static void
2494 ipa_read_optimization_summaries_1 (opt_pass *pass)
2496 while (pass)
2498 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2500 gcc_assert (!current_function_decl);
2501 gcc_assert (!cfun);
2502 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2504 if (pass->gate (cfun))
2506 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2508 /* If a timevar is present, start it. */
2509 if (pass->tv_id)
2510 timevar_push (pass->tv_id);
2512 pass_init_dump_file (pass);
2514 current_pass = pass;
2515 ipa_pass->read_optimization_summary ();
2517 pass_fini_dump_file (pass);
2519 /* Stop timevar. */
2520 if (pass->tv_id)
2521 timevar_pop (pass->tv_id);
2524 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2525 ipa_read_optimization_summaries_1 (pass->sub);
2527 pass = pass->next;
2531 /* Read all the summaries for all_regular_ipa_passes. */
2533 void
2534 ipa_read_optimization_summaries (void)
2536 pass_manager *passes = g->get_passes ();
2537 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2540 /* Same as execute_pass_list but assume that subpasses of IPA passes
2541 are local passes. */
2542 void
2543 execute_ipa_pass_list (opt_pass *pass)
2547 gcc_assert (!current_function_decl);
2548 gcc_assert (!cfun);
2549 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2550 if (execute_one_pass (pass) && pass->sub)
2552 if (pass->sub->type == GIMPLE_PASS)
2554 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2555 do_per_function_toporder ((void (*)(function *, void *))
2556 execute_pass_list,
2557 pass->sub);
2558 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2560 else if (pass->sub->type == SIMPLE_IPA_PASS
2561 || pass->sub->type == IPA_PASS)
2562 execute_ipa_pass_list (pass->sub);
2563 else
2564 gcc_unreachable ();
2566 gcc_assert (!current_function_decl);
2567 symtab->process_new_functions ();
2568 pass = pass->next;
2570 while (pass);
2573 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2575 static void
2576 execute_ipa_stmt_fixups (opt_pass *pass,
2577 struct cgraph_node *node, gimple *stmts)
2579 while (pass)
2581 /* Execute all of the IPA_PASSes in the list. */
2582 if (pass->type == IPA_PASS
2583 && pass->gate (cfun))
2585 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2587 if (ipa_pass->stmt_fixup)
2589 pass_init_dump_file (pass);
2590 /* If a timevar is present, start it. */
2591 if (pass->tv_id)
2592 timevar_push (pass->tv_id);
2594 current_pass = pass;
2595 ipa_pass->stmt_fixup (node, stmts);
2597 /* Stop timevar. */
2598 if (pass->tv_id)
2599 timevar_pop (pass->tv_id);
2600 pass_fini_dump_file (pass);
2602 if (pass->sub)
2603 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2605 pass = pass->next;
2609 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2611 void
2612 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2614 pass_manager *passes = g->get_passes ();
2615 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2619 extern void debug_properties (unsigned int);
2620 extern void dump_properties (FILE *, unsigned int);
2622 DEBUG_FUNCTION void
2623 dump_properties (FILE *dump, unsigned int props)
2625 fprintf (dump, "Properties:\n");
2626 if (props & PROP_gimple_any)
2627 fprintf (dump, "PROP_gimple_any\n");
2628 if (props & PROP_gimple_lcf)
2629 fprintf (dump, "PROP_gimple_lcf\n");
2630 if (props & PROP_gimple_leh)
2631 fprintf (dump, "PROP_gimple_leh\n");
2632 if (props & PROP_cfg)
2633 fprintf (dump, "PROP_cfg\n");
2634 if (props & PROP_ssa)
2635 fprintf (dump, "PROP_ssa\n");
2636 if (props & PROP_no_crit_edges)
2637 fprintf (dump, "PROP_no_crit_edges\n");
2638 if (props & PROP_rtl)
2639 fprintf (dump, "PROP_rtl\n");
2640 if (props & PROP_gimple_lomp)
2641 fprintf (dump, "PROP_gimple_lomp\n");
2642 if (props & PROP_gimple_lcx)
2643 fprintf (dump, "PROP_gimple_lcx\n");
2644 if (props & PROP_gimple_lvec)
2645 fprintf (dump, "PROP_gimple_lvec\n");
2646 if (props & PROP_cfglayout)
2647 fprintf (dump, "PROP_cfglayout\n");
2650 DEBUG_FUNCTION void
2651 debug_properties (unsigned int props)
2653 dump_properties (stderr, props);
2656 /* Called by local passes to see if function is called by already processed nodes.
2657 Because we process nodes in topological order, this means that function is
2658 in recursive cycle or we introduced new direct calls. */
2659 bool
2660 function_called_by_processed_nodes_p (void)
2662 struct cgraph_edge *e;
2663 for (e = cgraph_node::get (current_function_decl)->callers;
2665 e = e->next_caller)
2667 if (e->caller->decl == current_function_decl)
2668 continue;
2669 if (!e->caller->has_gimple_body_p ())
2670 continue;
2671 if (TREE_ASM_WRITTEN (e->caller->decl))
2672 continue;
2673 if (!e->caller->process && !e->caller->global.inlined_to)
2674 break;
2676 if (dump_file && e)
2678 fprintf (dump_file, "Already processed call to:\n");
2679 e->caller->dump (dump_file);
2681 return e != NULL;
2684 #include "gt-passes.h"