Introduce LABEL_REF_LABEL
[official-gcc.git] / gcc / passes.c
blob5001c3d047dc5da10cbbffd0ae9dcc836654806d
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 "function.h"
44 #include "toplev.h"
45 #include "expr.h"
46 #include "basic-block.h"
47 #include "intl.h"
48 #include "graph.h"
49 #include "regs.h"
50 #include "diagnostic-core.h"
51 #include "params.h"
52 #include "reload.h"
53 #include "debug.h"
54 #include "target.h"
55 #include "langhooks.h"
56 #include "cfgloop.h"
57 #include "hosthooks.h"
58 #include "opts.h"
59 #include "coverage.h"
60 #include "value-prof.h"
61 #include "tree-inline.h"
62 #include "tree-ssa-alias.h"
63 #include "internal-fn.h"
64 #include "gimple-expr.h"
65 #include "is-a.h"
66 #include "gimple.h"
67 #include "gimple-ssa.h"
68 #include "tree-cfg.h"
69 #include "stringpool.h"
70 #include "tree-ssanames.h"
71 #include "tree-ssa-loop-manip.h"
72 #include "tree-into-ssa.h"
73 #include "tree-dfa.h"
74 #include "tree-ssa.h"
75 #include "tree-pass.h"
76 #include "tree-dump.h"
77 #include "df.h"
78 #include "predict.h"
79 #include "lto-streamer.h"
80 #include "plugin.h"
81 #include "ipa-utils.h"
82 #include "tree-pretty-print.h" /* for dump_function_header */
83 #include "context.h"
84 #include "pass_manager.h"
85 #include "tree-ssa-live.h" /* For remove_unused_locals. */
86 #include "tree-cfgcleanup.h"
87 #include "hash-map.h"
89 using namespace gcc;
91 /* This is used for debugging. It allows the current pass to printed
92 from anywhere in compilation.
93 The variable current_pass is also used for statistics and plugins. */
94 opt_pass *current_pass;
96 static void register_pass_name (opt_pass *, const char *);
98 /* Most passes are single-instance (within their context) and thus don't
99 need to implement cloning, but passes that support multiple instances
100 *must* provide their own implementation of the clone method.
102 Handle this by providing a default implemenation, but make it a fatal
103 error to call it. */
105 opt_pass *
106 opt_pass::clone ()
108 internal_error ("pass %s does not support cloning", name);
111 bool
112 opt_pass::gate (function *)
114 return true;
117 unsigned int
118 opt_pass::execute (function *)
120 return 0;
123 opt_pass::opt_pass (const pass_data &data, context *ctxt)
124 : pass_data (data),
125 sub (NULL),
126 next (NULL),
127 static_pass_number (0),
128 m_ctxt (ctxt)
133 void
134 pass_manager::execute_early_local_passes ()
136 execute_pass_list (cfun, pass_early_local_passes_1->sub);
139 unsigned int
140 pass_manager::execute_pass_mode_switching ()
142 return pass_mode_switching_1->execute (cfun);
146 /* Call from anywhere to find out what pass this is. Useful for
147 printing out debugging information deep inside an service
148 routine. */
149 void
150 print_current_pass (FILE *file)
152 if (current_pass)
153 fprintf (file, "current pass = %s (%d)\n",
154 current_pass->name, current_pass->static_pass_number);
155 else
156 fprintf (file, "no current pass.\n");
160 /* Call from the debugger to get the current pass name. */
161 DEBUG_FUNCTION void
162 debug_pass (void)
164 print_current_pass (stderr);
169 /* Global variables used to communicate with passes. */
170 bool in_gimple_form;
171 bool first_pass_instance;
174 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
175 and TYPE_DECL nodes.
177 This does nothing for local (non-static) variables, unless the
178 variable is a register variable with DECL_ASSEMBLER_NAME set. In
179 that case, or if the variable is not an automatic, it sets up the
180 RTL and outputs any assembler code (label definition, storage
181 allocation and initialization).
183 DECL is the declaration. TOP_LEVEL is nonzero
184 if this declaration is not within a function. */
186 void
187 rest_of_decl_compilation (tree decl,
188 int top_level,
189 int at_end)
191 bool finalize = true;
193 /* We deferred calling assemble_alias so that we could collect
194 other attributes such as visibility. Emit the alias now. */
195 if (!in_lto_p)
197 tree alias;
198 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
199 if (alias)
201 alias = TREE_VALUE (TREE_VALUE (alias));
202 alias = get_identifier (TREE_STRING_POINTER (alias));
203 /* A quirk of the initial implementation of aliases required that the
204 user add "extern" to all of them. Which is silly, but now
205 historical. Do note that the symbol is in fact locally defined. */
206 DECL_EXTERNAL (decl) = 0;
207 TREE_STATIC (decl) = 1;
208 assemble_alias (decl, alias);
209 finalize = false;
213 /* Can't defer this, because it needs to happen before any
214 later function definitions are processed. */
215 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
216 make_decl_rtl (decl);
218 /* Forward declarations for nested functions are not "external",
219 but we need to treat them as if they were. */
220 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
221 || TREE_CODE (decl) == FUNCTION_DECL)
223 timevar_push (TV_VARCONST);
225 /* Don't output anything when a tentative file-scope definition
226 is seen. But at end of compilation, do output code for them.
228 We do output all variables and rely on
229 callgraph code to defer them except for forward declarations
230 (see gcc.c-torture/compile/920624-1.c) */
231 if ((at_end
232 || !DECL_DEFER_OUTPUT (decl)
233 || DECL_INITIAL (decl))
234 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
235 && !DECL_EXTERNAL (decl))
237 /* When reading LTO unit, we also read varpool, so do not
238 rebuild it. */
239 if (in_lto_p && !at_end)
241 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
242 varpool_node::finalize_decl (decl);
245 #ifdef ASM_FINISH_DECLARE_OBJECT
246 if (decl == last_assemble_variable_decl)
248 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
249 top_level, at_end);
251 #endif
253 timevar_pop (TV_VARCONST);
255 else if (TREE_CODE (decl) == TYPE_DECL
256 /* Like in rest_of_type_compilation, avoid confusing the debug
257 information machinery when there are errors. */
258 && !seen_error ())
260 timevar_push (TV_SYMOUT);
261 debug_hooks->type_decl (decl, !top_level);
262 timevar_pop (TV_SYMOUT);
265 /* Let cgraph know about the existence of variables. */
266 if (in_lto_p && !at_end)
268 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
269 && TREE_STATIC (decl))
270 varpool_node::get_create (decl);
273 /* Called after finishing a record, union or enumeral type. */
275 void
276 rest_of_type_compilation (tree type, int toplev)
278 /* Avoid confusing the debug information machinery when there are
279 errors. */
280 if (seen_error ())
281 return;
283 timevar_push (TV_SYMOUT);
284 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
285 timevar_pop (TV_SYMOUT);
290 void
291 pass_manager::
292 finish_optimization_passes (void)
294 int i;
295 struct dump_file_info *dfi;
296 char *name;
297 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
299 timevar_push (TV_DUMP);
300 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
302 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
303 end_branch_prob ();
304 dumps->dump_finish (pass_profile_1->static_pass_number);
307 if (optimize > 0)
309 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
310 print_combine_total_stats ();
311 dumps->dump_finish (pass_profile_1->static_pass_number);
314 /* Do whatever is necessary to finish printing the graphs. */
315 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
316 if (dumps->dump_initialized_p (i)
317 && (dfi->pflags & TDF_GRAPH) != 0
318 && (name = dumps->get_dump_file_name (i)) != NULL)
320 finish_graph_dump_file (name);
321 free (name);
324 timevar_pop (TV_DUMP);
327 static unsigned int
328 execute_all_early_local_passes (void)
330 /* Once this pass (and its sub-passes) are complete, all functions
331 will be in SSA form. Technically this state change is happening
332 a tad early, since the sub-passes have not yet run, but since
333 none of the sub-passes are IPA passes and do not create new
334 functions, this is ok. We're setting this value for the benefit
335 of IPA passes that follow. */
336 if (symtab->state < IPA_SSA)
337 symtab->state = IPA_SSA;
338 return 0;
341 namespace {
343 const pass_data pass_data_early_local_passes =
345 SIMPLE_IPA_PASS, /* type */
346 "early_local_cleanups", /* name */
347 OPTGROUP_NONE, /* optinfo_flags */
348 TV_EARLY_LOCAL, /* tv_id */
349 0, /* properties_required */
350 0, /* properties_provided */
351 0, /* properties_destroyed */
352 0, /* todo_flags_start */
353 /* todo_flags_finish is executed before subpases. For this reason
354 it makes no sense to remove unreachable functions here. */
355 0, /* todo_flags_finish */
358 class pass_early_local_passes : public simple_ipa_opt_pass
360 public:
361 pass_early_local_passes (gcc::context *ctxt)
362 : simple_ipa_opt_pass (pass_data_early_local_passes, ctxt)
365 /* opt_pass methods: */
366 virtual bool gate (function *)
368 /* Don't bother doing anything if the program has errors. */
369 return (!seen_error () && !in_lto_p);
372 virtual unsigned int execute (function *)
374 return execute_all_early_local_passes ();
377 }; // class pass_early_local_passes
379 } // anon namespace
381 simple_ipa_opt_pass *
382 make_pass_early_local_passes (gcc::context *ctxt)
384 return new pass_early_local_passes (ctxt);
387 namespace {
389 const pass_data pass_data_all_early_optimizations =
391 GIMPLE_PASS, /* type */
392 "early_optimizations", /* name */
393 OPTGROUP_NONE, /* optinfo_flags */
394 TV_NONE, /* tv_id */
395 0, /* properties_required */
396 0, /* properties_provided */
397 0, /* properties_destroyed */
398 0, /* todo_flags_start */
399 0, /* todo_flags_finish */
402 class pass_all_early_optimizations : public gimple_opt_pass
404 public:
405 pass_all_early_optimizations (gcc::context *ctxt)
406 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
409 /* opt_pass methods: */
410 virtual bool gate (function *)
412 return (optimize >= 1
413 /* Don't bother doing anything if the program has errors. */
414 && !seen_error ());
417 }; // class pass_all_early_optimizations
419 } // anon namespace
421 static gimple_opt_pass *
422 make_pass_all_early_optimizations (gcc::context *ctxt)
424 return new pass_all_early_optimizations (ctxt);
427 namespace {
429 const pass_data pass_data_all_optimizations =
431 GIMPLE_PASS, /* type */
432 "*all_optimizations", /* name */
433 OPTGROUP_NONE, /* optinfo_flags */
434 TV_OPTIMIZE, /* tv_id */
435 0, /* properties_required */
436 0, /* properties_provided */
437 0, /* properties_destroyed */
438 0, /* todo_flags_start */
439 0, /* todo_flags_finish */
442 class pass_all_optimizations : public gimple_opt_pass
444 public:
445 pass_all_optimizations (gcc::context *ctxt)
446 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
449 /* opt_pass methods: */
450 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
452 }; // class pass_all_optimizations
454 } // anon namespace
456 static gimple_opt_pass *
457 make_pass_all_optimizations (gcc::context *ctxt)
459 return new pass_all_optimizations (ctxt);
462 namespace {
464 const pass_data pass_data_all_optimizations_g =
466 GIMPLE_PASS, /* type */
467 "*all_optimizations_g", /* name */
468 OPTGROUP_NONE, /* optinfo_flags */
469 TV_OPTIMIZE, /* tv_id */
470 0, /* properties_required */
471 0, /* properties_provided */
472 0, /* properties_destroyed */
473 0, /* todo_flags_start */
474 0, /* todo_flags_finish */
477 class pass_all_optimizations_g : public gimple_opt_pass
479 public:
480 pass_all_optimizations_g (gcc::context *ctxt)
481 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
484 /* opt_pass methods: */
485 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
487 }; // class pass_all_optimizations_g
489 } // anon namespace
491 static gimple_opt_pass *
492 make_pass_all_optimizations_g (gcc::context *ctxt)
494 return new pass_all_optimizations_g (ctxt);
497 namespace {
499 const pass_data pass_data_rest_of_compilation =
501 RTL_PASS, /* type */
502 "*rest_of_compilation", /* name */
503 OPTGROUP_NONE, /* optinfo_flags */
504 TV_REST_OF_COMPILATION, /* tv_id */
505 PROP_rtl, /* properties_required */
506 0, /* properties_provided */
507 0, /* properties_destroyed */
508 0, /* todo_flags_start */
509 0, /* todo_flags_finish */
512 class pass_rest_of_compilation : public rtl_opt_pass
514 public:
515 pass_rest_of_compilation (gcc::context *ctxt)
516 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
519 /* opt_pass methods: */
520 virtual bool gate (function *)
522 /* Early return if there were errors. We can run afoul of our
523 consistency checks, and there's not really much point in fixing them. */
524 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
527 }; // class pass_rest_of_compilation
529 } // anon namespace
531 static rtl_opt_pass *
532 make_pass_rest_of_compilation (gcc::context *ctxt)
534 return new pass_rest_of_compilation (ctxt);
537 namespace {
539 const pass_data pass_data_postreload =
541 RTL_PASS, /* type */
542 "*all-postreload", /* name */
543 OPTGROUP_NONE, /* optinfo_flags */
544 TV_POSTRELOAD, /* tv_id */
545 PROP_rtl, /* properties_required */
546 0, /* properties_provided */
547 0, /* properties_destroyed */
548 0, /* todo_flags_start */
549 0, /* todo_flags_finish */
552 class pass_postreload : public rtl_opt_pass
554 public:
555 pass_postreload (gcc::context *ctxt)
556 : rtl_opt_pass (pass_data_postreload, ctxt)
559 /* opt_pass methods: */
560 virtual bool gate (function *) { return reload_completed; }
562 }; // class pass_postreload
564 } // anon namespace
566 static rtl_opt_pass *
567 make_pass_postreload (gcc::context *ctxt)
569 return new pass_postreload (ctxt);
574 /* Set the static pass number of pass PASS to ID and record that
575 in the mapping from static pass number to pass. */
577 void
578 pass_manager::
579 set_pass_for_id (int id, opt_pass *pass)
581 pass->static_pass_number = id;
582 if (passes_by_id_size <= id)
584 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
585 memset (passes_by_id + passes_by_id_size, 0,
586 (id + 1 - passes_by_id_size) * sizeof (void *));
587 passes_by_id_size = id + 1;
589 passes_by_id[id] = pass;
592 /* Return the pass with the static pass number ID. */
594 opt_pass *
595 pass_manager::get_pass_for_id (int id) const
597 if (id >= passes_by_id_size)
598 return NULL;
599 return passes_by_id[id];
602 /* Iterate over the pass tree allocating dump file numbers. We want
603 to do this depth first, and independent of whether the pass is
604 enabled or not. */
606 void
607 register_one_dump_file (opt_pass *pass)
609 g->get_passes ()->register_one_dump_file (pass);
612 void
613 pass_manager::register_one_dump_file (opt_pass *pass)
615 char *dot_name, *flag_name, *glob_name;
616 const char *name, *full_name, *prefix;
617 char num[10];
618 int flags, id;
619 int optgroup_flags = OPTGROUP_NONE;
620 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
622 /* See below in next_pass_1. */
623 num[0] = '\0';
624 if (pass->static_pass_number != -1)
625 sprintf (num, "%d", ((int) pass->static_pass_number < 0
626 ? 1 : pass->static_pass_number));
628 /* The name is both used to identify the pass for the purposes of plugins,
629 and to specify dump file name and option.
630 The latter two might want something short which is not quite unique; for
631 that reason, we may have a disambiguating prefix, followed by a space
632 to mark the start of the following dump file name / option string. */
633 name = strchr (pass->name, ' ');
634 name = name ? name + 1 : pass->name;
635 dot_name = concat (".", name, num, NULL);
636 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
638 prefix = "ipa-";
639 flags = TDF_IPA;
640 optgroup_flags |= OPTGROUP_IPA;
642 else if (pass->type == GIMPLE_PASS)
644 prefix = "tree-";
645 flags = TDF_TREE;
647 else
649 prefix = "rtl-";
650 flags = TDF_RTL;
653 flag_name = concat (prefix, name, num, NULL);
654 glob_name = concat (prefix, name, NULL);
655 optgroup_flags |= pass->optinfo_flags;
656 /* For any passes that do not have an optgroup set, and which are not
657 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
658 any dump messages are emitted properly under -fopt-info(-optall). */
659 if (optgroup_flags == OPTGROUP_NONE)
660 optgroup_flags = OPTGROUP_OTHER;
661 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
662 optgroup_flags);
663 set_pass_for_id (id, pass);
664 full_name = concat (prefix, pass->name, num, NULL);
665 register_pass_name (pass, full_name);
666 free (CONST_CAST (char *, full_name));
669 /* Register the dump files for the pass_manager starting at PASS. */
671 void
672 pass_manager::register_dump_files (opt_pass *pass)
676 if (pass->name && pass->name[0] != '*')
677 register_one_dump_file (pass);
679 if (pass->sub)
680 register_dump_files (pass->sub);
682 pass = pass->next;
684 while (pass);
687 /* Helper for pass_registry hash table. */
689 struct pass_registry_hasher : default_hashmap_traits
691 static inline hashval_t hash (const char *);
692 static inline bool equal_keys (const char *, const char *);
695 /* Pass registry hash function. */
697 inline hashval_t
698 pass_registry_hasher::hash (const char *name)
700 return htab_hash_string (name);
703 /* Hash equal function */
705 inline bool
706 pass_registry_hasher::equal_keys (const char *s1, const char *s2)
708 return !strcmp (s1, s2);
711 static hash_map<const char *, opt_pass *, pass_registry_hasher>
712 *name_to_pass_map;
714 /* Register PASS with NAME. */
716 static void
717 register_pass_name (opt_pass *pass, const char *name)
719 if (!name_to_pass_map)
720 name_to_pass_map
721 = new hash_map<const char *, opt_pass *, pass_registry_hasher> (256);
723 if (name_to_pass_map->get (name))
724 return; /* Ignore plugin passes. */
726 const char *unique_name = xstrdup (name);
727 name_to_pass_map->put (unique_name, pass);
730 /* Map from pass id to canonicalized pass name. */
732 typedef const char *char_ptr;
733 static vec<char_ptr> pass_tab = vNULL;
735 /* Callback function for traversing NAME_TO_PASS_MAP. */
737 bool
738 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
740 gcc_assert (pass->static_pass_number > 0);
741 gcc_assert (pass_tab.exists ());
743 pass_tab[pass->static_pass_number] = name;
745 return 1;
748 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
749 table for dumping purpose. */
751 static void
752 create_pass_tab (void)
754 if (!flag_dump_passes)
755 return;
757 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
758 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
761 static bool override_gate_status (opt_pass *, tree, bool);
763 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
764 is turned on or not. */
766 static void
767 dump_one_pass (opt_pass *pass, int pass_indent)
769 int indent = 3 * pass_indent;
770 const char *pn;
771 bool is_on, is_really_on;
773 is_on = pass->gate (cfun);
774 is_really_on = override_gate_status (pass, current_function_decl, is_on);
776 if (pass->static_pass_number <= 0)
777 pn = pass->name;
778 else
779 pn = pass_tab[pass->static_pass_number];
781 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
782 (15 - indent < 0 ? 0 : 15 - indent), " ",
783 is_on ? " ON" : " OFF",
784 ((!is_on) == (!is_really_on) ? ""
785 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
788 /* Dump pass list PASS with indentation INDENT. */
790 static void
791 dump_pass_list (opt_pass *pass, int indent)
795 dump_one_pass (pass, indent);
796 if (pass->sub)
797 dump_pass_list (pass->sub, indent + 1);
798 pass = pass->next;
800 while (pass);
803 /* Dump all optimization passes. */
805 void
806 dump_passes (void)
808 g->get_passes ()->dump_passes ();
811 void
812 pass_manager::dump_passes () const
814 struct cgraph_node *n, *node = NULL;
816 create_pass_tab ();
818 FOR_EACH_FUNCTION (n)
819 if (DECL_STRUCT_FUNCTION (n->decl))
821 node = n;
822 break;
825 if (!node)
826 return;
828 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
830 dump_pass_list (all_lowering_passes, 1);
831 dump_pass_list (all_small_ipa_passes, 1);
832 dump_pass_list (all_regular_ipa_passes, 1);
833 dump_pass_list (all_late_ipa_passes, 1);
834 dump_pass_list (all_passes, 1);
836 pop_cfun ();
840 /* Returns the pass with NAME. */
842 static opt_pass *
843 get_pass_by_name (const char *name)
845 opt_pass **p = name_to_pass_map->get (name);
846 if (p)
847 return *p;
849 return NULL;
853 /* Range [start, last]. */
855 struct uid_range
857 unsigned int start;
858 unsigned int last;
859 const char *assem_name;
860 struct uid_range *next;
863 typedef struct uid_range *uid_range_p;
866 static vec<uid_range_p>
867 enabled_pass_uid_range_tab = vNULL;
868 static vec<uid_range_p>
869 disabled_pass_uid_range_tab = vNULL;
872 /* Parse option string for -fdisable- and -fenable-
873 The syntax of the options:
875 -fenable-<pass_name>
876 -fdisable-<pass_name>
878 -fenable-<pass_name>=s1:e1,s2:e2,...
879 -fdisable-<pass_name>=s1:e1,s2:e2,...
882 static void
883 enable_disable_pass (const char *arg, bool is_enable)
885 opt_pass *pass;
886 char *range_str, *phase_name;
887 char *argstr = xstrdup (arg);
888 vec<uid_range_p> *tab = 0;
890 range_str = strchr (argstr,'=');
891 if (range_str)
893 *range_str = '\0';
894 range_str++;
897 phase_name = argstr;
898 if (!*phase_name)
900 if (is_enable)
901 error ("unrecognized option -fenable");
902 else
903 error ("unrecognized option -fdisable");
904 free (argstr);
905 return;
907 pass = get_pass_by_name (phase_name);
908 if (!pass || pass->static_pass_number == -1)
910 if (is_enable)
911 error ("unknown pass %s specified in -fenable", phase_name);
912 else
913 error ("unknown pass %s specified in -fdisable", phase_name);
914 free (argstr);
915 return;
918 if (is_enable)
919 tab = &enabled_pass_uid_range_tab;
920 else
921 tab = &disabled_pass_uid_range_tab;
923 if ((unsigned) pass->static_pass_number >= tab->length ())
924 tab->safe_grow_cleared (pass->static_pass_number + 1);
926 if (!range_str)
928 uid_range_p slot;
929 uid_range_p new_range = XCNEW (struct uid_range);
931 new_range->start = 0;
932 new_range->last = (unsigned)-1;
934 slot = (*tab)[pass->static_pass_number];
935 new_range->next = slot;
936 (*tab)[pass->static_pass_number] = new_range;
937 if (is_enable)
938 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
939 "of [%u, %u]", phase_name, new_range->start, new_range->last);
940 else
941 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
942 "of [%u, %u]", phase_name, new_range->start, new_range->last);
944 else
946 char *next_range = NULL;
947 char *one_range = range_str;
948 char *end_val = NULL;
952 uid_range_p slot;
953 uid_range_p new_range;
954 char *invalid = NULL;
955 long start;
956 char *func_name = NULL;
958 next_range = strchr (one_range, ',');
959 if (next_range)
961 *next_range = '\0';
962 next_range++;
965 end_val = strchr (one_range, ':');
966 if (end_val)
968 *end_val = '\0';
969 end_val++;
971 start = strtol (one_range, &invalid, 10);
972 if (*invalid || start < 0)
974 if (end_val || (one_range[0] >= '0'
975 && one_range[0] <= '9'))
977 error ("Invalid range %s in option %s",
978 one_range,
979 is_enable ? "-fenable" : "-fdisable");
980 free (argstr);
981 return;
983 func_name = one_range;
985 if (!end_val)
987 new_range = XCNEW (struct uid_range);
988 if (!func_name)
990 new_range->start = (unsigned) start;
991 new_range->last = (unsigned) start;
993 else
995 new_range->start = (unsigned) -1;
996 new_range->last = (unsigned) -1;
997 new_range->assem_name = xstrdup (func_name);
1000 else
1002 long last = strtol (end_val, &invalid, 10);
1003 if (*invalid || last < start)
1005 error ("Invalid range %s in option %s",
1006 end_val,
1007 is_enable ? "-fenable" : "-fdisable");
1008 free (argstr);
1009 return;
1011 new_range = XCNEW (struct uid_range);
1012 new_range->start = (unsigned) start;
1013 new_range->last = (unsigned) last;
1016 slot = (*tab)[pass->static_pass_number];
1017 new_range->next = slot;
1018 (*tab)[pass->static_pass_number] = new_range;
1019 if (is_enable)
1021 if (new_range->assem_name)
1022 inform (UNKNOWN_LOCATION,
1023 "enable pass %s for function %s",
1024 phase_name, new_range->assem_name);
1025 else
1026 inform (UNKNOWN_LOCATION,
1027 "enable pass %s for functions in the range of [%u, %u]",
1028 phase_name, new_range->start, new_range->last);
1030 else
1032 if (new_range->assem_name)
1033 inform (UNKNOWN_LOCATION,
1034 "disable pass %s for function %s",
1035 phase_name, new_range->assem_name);
1036 else
1037 inform (UNKNOWN_LOCATION,
1038 "disable pass %s for functions in the range of [%u, %u]",
1039 phase_name, new_range->start, new_range->last);
1042 one_range = next_range;
1043 } while (next_range);
1046 free (argstr);
1049 /* Enable pass specified by ARG. */
1051 void
1052 enable_pass (const char *arg)
1054 enable_disable_pass (arg, true);
1057 /* Disable pass specified by ARG. */
1059 void
1060 disable_pass (const char *arg)
1062 enable_disable_pass (arg, false);
1065 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1067 static bool
1068 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1069 tree func,
1070 vec<uid_range_p> tab)
1072 uid_range_p slot, range;
1073 int cgraph_uid;
1074 const char *aname = NULL;
1076 if (!tab.exists ()
1077 || (unsigned) pass->static_pass_number >= tab.length ()
1078 || pass->static_pass_number == -1)
1079 return false;
1081 slot = tab[pass->static_pass_number];
1082 if (!slot)
1083 return false;
1085 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1086 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1087 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1089 range = slot;
1090 while (range)
1092 if ((unsigned) cgraph_uid >= range->start
1093 && (unsigned) cgraph_uid <= range->last)
1094 return true;
1095 if (range->assem_name && aname
1096 && !strcmp (range->assem_name, aname))
1097 return true;
1098 range = range->next;
1101 return false;
1105 /* Update static_pass_number for passes (and the flag
1106 TODO_mark_first_instance).
1108 Passes are constructed with static_pass_number preinitialized to 0
1110 This field is used in two different ways: initially as instance numbers
1111 of their kind, and then as ids within the entire pass manager.
1113 Within pass_manager::pass_manager:
1115 * In add_pass_instance(), as called by next_pass_1 in
1116 NEXT_PASS in init_optimization_passes
1118 * When the initial instance of a pass within a pass manager is seen,
1119 it is flagged, and its static_pass_number is set to -1
1121 * On subsequent times that it is seen, the static pass number
1122 is decremented each time, so that if there are e.g. 4 dups,
1123 they have static_pass_number -4, 2, 3, 4 respectively (note
1124 how the initial one is negative and gives the count); these
1125 can be thought of as instance numbers of the specific pass
1127 * Within the register_dump_files () traversal, set_pass_for_id()
1128 is called on each pass, using these instance numbers to create
1129 dumpfile switches, and then overwriting them with a pass id,
1130 which are global to the whole pass manager (based on
1131 (TDI_end + current value of extra_dump_files_in_use) ) */
1133 static void
1134 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1135 opt_pass *initial_pass)
1137 /* Are we dealing with the first pass of its kind, or a clone? */
1138 if (new_pass != initial_pass)
1140 /* We're dealing with a clone. */
1141 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1143 /* Indicate to register_dump_files that this pass has duplicates,
1144 and so it should rename the dump file. The first instance will
1145 be -1, and be number of duplicates = -static_pass_number - 1.
1146 Subsequent instances will be > 0 and just the duplicate number. */
1147 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1149 initial_pass->static_pass_number -= 1;
1150 new_pass->static_pass_number = -initial_pass->static_pass_number;
1153 else
1155 /* We're dealing with the first pass of its kind. */
1156 new_pass->todo_flags_start |= TODO_mark_first_instance;
1157 new_pass->static_pass_number = -1;
1159 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1163 /* Add a pass to the pass list. Duplicate the pass if it's already
1164 in the list. */
1166 static opt_pass **
1167 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1169 /* Every pass should have a name so that plugins can refer to them. */
1170 gcc_assert (pass->name != NULL);
1172 add_pass_instance (pass, false, initial_pass);
1173 *list = pass;
1175 return &(*list)->next;
1178 /* List node for an inserted pass instance. We need to keep track of all
1179 the newly-added pass instances (with 'added_pass_nodes' defined below)
1180 so that we can register their dump files after pass-positioning is finished.
1181 Registering dumping files needs to be post-processed or the
1182 static_pass_number of the opt_pass object would be modified and mess up
1183 the dump file names of future pass instances to be added. */
1185 struct pass_list_node
1187 opt_pass *pass;
1188 struct pass_list_node *next;
1191 static struct pass_list_node *added_pass_nodes = NULL;
1192 static struct pass_list_node *prev_added_pass_node;
1194 /* Insert the pass at the proper position. Return true if the pass
1195 is successfully added.
1197 NEW_PASS_INFO - new pass to be inserted
1198 PASS_LIST - root of the pass list to insert the new pass to */
1200 static bool
1201 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1203 opt_pass *pass = *pass_list, *prev_pass = NULL;
1204 bool success = false;
1206 for ( ; pass; prev_pass = pass, pass = pass->next)
1208 /* Check if the current pass is of the same type as the new pass and
1209 matches the name and the instance number of the reference pass. */
1210 if (pass->type == new_pass_info->pass->type
1211 && pass->name
1212 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1213 && ((new_pass_info->ref_pass_instance_number == 0)
1214 || (new_pass_info->ref_pass_instance_number ==
1215 pass->static_pass_number)
1216 || (new_pass_info->ref_pass_instance_number == 1
1217 && pass->todo_flags_start & TODO_mark_first_instance)))
1219 opt_pass *new_pass;
1220 struct pass_list_node *new_pass_node;
1222 if (new_pass_info->ref_pass_instance_number == 0)
1224 new_pass = new_pass_info->pass->clone ();
1225 add_pass_instance (new_pass, true, new_pass_info->pass);
1227 else
1229 new_pass = new_pass_info->pass;
1230 add_pass_instance (new_pass, true, new_pass);
1233 /* Insert the new pass instance based on the positioning op. */
1234 switch (new_pass_info->pos_op)
1236 case PASS_POS_INSERT_AFTER:
1237 new_pass->next = pass->next;
1238 pass->next = new_pass;
1240 /* Skip newly inserted pass to avoid repeated
1241 insertions in the case where the new pass and the
1242 existing one have the same name. */
1243 pass = new_pass;
1244 break;
1245 case PASS_POS_INSERT_BEFORE:
1246 new_pass->next = pass;
1247 if (prev_pass)
1248 prev_pass->next = new_pass;
1249 else
1250 *pass_list = new_pass;
1251 break;
1252 case PASS_POS_REPLACE:
1253 new_pass->next = pass->next;
1254 if (prev_pass)
1255 prev_pass->next = new_pass;
1256 else
1257 *pass_list = new_pass;
1258 new_pass->sub = pass->sub;
1259 new_pass->tv_id = pass->tv_id;
1260 pass = new_pass;
1261 break;
1262 default:
1263 error ("invalid pass positioning operation");
1264 return false;
1267 /* Save the newly added pass (instance) in the added_pass_nodes
1268 list so that we can register its dump file later. Note that
1269 we cannot register the dump file now because doing so will modify
1270 the static_pass_number of the opt_pass object and therefore
1271 mess up the dump file name of future instances. */
1272 new_pass_node = XCNEW (struct pass_list_node);
1273 new_pass_node->pass = new_pass;
1274 if (!added_pass_nodes)
1275 added_pass_nodes = new_pass_node;
1276 else
1277 prev_added_pass_node->next = new_pass_node;
1278 prev_added_pass_node = new_pass_node;
1280 success = true;
1283 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1284 success = true;
1287 return success;
1290 /* Hooks a new pass into the pass lists.
1292 PASS_INFO - pass information that specifies the opt_pass object,
1293 reference pass, instance number, and how to position
1294 the pass */
1296 void
1297 register_pass (struct register_pass_info *pass_info)
1299 g->get_passes ()->register_pass (pass_info);
1302 void
1303 register_pass (opt_pass* pass, pass_positioning_ops pos,
1304 const char* ref_pass_name, int ref_pass_inst_number)
1306 register_pass_info i;
1307 i.pass = pass;
1308 i.reference_pass_name = ref_pass_name;
1309 i.ref_pass_instance_number = ref_pass_inst_number;
1310 i.pos_op = pos;
1312 g->get_passes ()->register_pass (&i);
1315 void
1316 pass_manager::register_pass (struct register_pass_info *pass_info)
1318 bool all_instances, success;
1319 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1321 /* The checks below could fail in buggy plugins. Existing GCC
1322 passes should never fail these checks, so we mention plugin in
1323 the messages. */
1324 if (!pass_info->pass)
1325 fatal_error ("plugin cannot register a missing pass");
1327 if (!pass_info->pass->name)
1328 fatal_error ("plugin cannot register an unnamed pass");
1330 if (!pass_info->reference_pass_name)
1331 fatal_error
1332 ("plugin cannot register pass %qs without reference pass name",
1333 pass_info->pass->name);
1335 /* Try to insert the new pass to the pass lists. We need to check
1336 all five lists as the reference pass could be in one (or all) of
1337 them. */
1338 all_instances = pass_info->ref_pass_instance_number == 0;
1339 success = position_pass (pass_info, &all_lowering_passes);
1340 if (!success || all_instances)
1341 success |= position_pass (pass_info, &all_small_ipa_passes);
1342 if (!success || all_instances)
1343 success |= position_pass (pass_info, &all_regular_ipa_passes);
1344 if (!success || all_instances)
1345 success |= position_pass (pass_info, &all_late_ipa_passes);
1346 if (!success || all_instances)
1347 success |= position_pass (pass_info, &all_passes);
1348 if (!success)
1349 fatal_error
1350 ("pass %qs not found but is referenced by new pass %qs",
1351 pass_info->reference_pass_name, pass_info->pass->name);
1353 /* OK, we have successfully inserted the new pass. We need to register
1354 the dump files for the newly added pass and its duplicates (if any).
1355 Because the registration of plugin/backend passes happens after the
1356 command-line options are parsed, the options that specify single
1357 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1358 passes. Therefore we currently can only enable dumping of
1359 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1360 are specified. While doing so, we also delete the pass_list_node
1361 objects created during pass positioning. */
1362 while (added_pass_nodes)
1364 struct pass_list_node *next_node = added_pass_nodes->next;
1365 enum tree_dump_index tdi;
1366 register_one_dump_file (added_pass_nodes->pass);
1367 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1368 || added_pass_nodes->pass->type == IPA_PASS)
1369 tdi = TDI_ipa_all;
1370 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1371 tdi = TDI_tree_all;
1372 else
1373 tdi = TDI_rtl_all;
1374 /* Check if dump-all flag is specified. */
1375 if (dumps->get_dump_file_info (tdi)->pstate)
1376 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1377 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1378 XDELETE (added_pass_nodes);
1379 added_pass_nodes = next_node;
1383 /* Construct the pass tree. The sequencing of passes is driven by
1384 the cgraph routines:
1386 finalize_compilation_unit ()
1387 for each node N in the cgraph
1388 cgraph_analyze_function (N)
1389 cgraph_lower_function (N) -> all_lowering_passes
1391 If we are optimizing, compile is then invoked:
1393 compile ()
1394 ipa_passes () -> all_small_ipa_passes
1395 -> Analysis of all_regular_ipa_passes
1396 * possible LTO streaming at copmilation time *
1397 -> Execution of all_regular_ipa_passes
1398 * possible LTO streaming at link time *
1399 -> all_late_ipa_passes
1400 expand_all_functions ()
1401 for each node N in the cgraph
1402 expand_function (N) -> Transformation of all_regular_ipa_passes
1403 -> all_passes
1406 void *
1407 pass_manager::operator new (size_t sz)
1409 /* Ensure that all fields of the pass manager are zero-initialized. */
1410 return xcalloc (1, sz);
1413 pass_manager::pass_manager (context *ctxt)
1414 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1415 all_regular_ipa_passes (NULL),
1416 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1417 m_ctxt (ctxt)
1419 opt_pass **p;
1421 /* Initialize the pass_lists array. */
1422 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1423 GCC_PASS_LISTS
1424 #undef DEF_PASS_LIST
1426 /* Build the tree of passes. */
1428 #define INSERT_PASSES_AFTER(PASS) \
1429 p = &(PASS);
1431 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1433 opt_pass **p = &(PASS ## _1)->sub;
1435 #define POP_INSERT_PASSES() \
1438 #define NEXT_PASS(PASS, NUM) \
1439 do { \
1440 gcc_assert (NULL == PASS ## _ ## NUM); \
1441 if ((NUM) == 1) \
1442 PASS ## _1 = make_##PASS (m_ctxt); \
1443 else \
1445 gcc_assert (PASS ## _1); \
1446 PASS ## _ ## NUM = PASS ## _1->clone (); \
1448 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1449 } while (0)
1451 #define TERMINATE_PASS_LIST() \
1452 *p = NULL;
1454 #include "pass-instances.def"
1456 #undef INSERT_PASSES_AFTER
1457 #undef PUSH_INSERT_PASSES_WITHIN
1458 #undef POP_INSERT_PASSES
1459 #undef NEXT_PASS
1460 #undef TERMINATE_PASS_LIST
1462 /* Register the passes with the tree dump code. */
1463 register_dump_files (all_lowering_passes);
1464 register_dump_files (all_small_ipa_passes);
1465 register_dump_files (all_regular_ipa_passes);
1466 register_dump_files (all_late_ipa_passes);
1467 register_dump_files (all_passes);
1470 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1471 function CALLBACK for every function in the call graph. Otherwise,
1472 call CALLBACK on the current function. */
1474 static void
1475 do_per_function (void (*callback) (function *, void *data), void *data)
1477 if (current_function_decl)
1478 callback (cfun, data);
1479 else
1481 struct cgraph_node *node;
1482 FOR_EACH_DEFINED_FUNCTION (node)
1483 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1484 && (!node->clone_of || node->decl != node->clone_of->decl))
1485 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1489 /* Because inlining might remove no-longer reachable nodes, we need to
1490 keep the array visible to garbage collector to avoid reading collected
1491 out nodes. */
1492 static int nnodes;
1493 static GTY ((length ("nnodes"))) cgraph_node **order;
1495 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1496 function CALLBACK for every function in the call graph. Otherwise,
1497 call CALLBACK on the current function.
1498 This function is global so that plugins can use it. */
1499 void
1500 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1502 int i;
1504 if (current_function_decl)
1505 callback (cfun, data);
1506 else
1508 gcc_assert (!order);
1509 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1510 nnodes = ipa_reverse_postorder (order);
1511 for (i = nnodes - 1; i >= 0; i--)
1512 order[i]->process = 1;
1513 for (i = nnodes - 1; i >= 0; i--)
1515 struct cgraph_node *node = order[i];
1517 /* Allow possibly removed nodes to be garbage collected. */
1518 order[i] = NULL;
1519 node->process = 0;
1520 if (node->has_gimple_body_p ())
1521 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1524 ggc_free (order);
1525 order = NULL;
1526 nnodes = 0;
1529 /* Helper function to perform function body dump. */
1531 static void
1532 execute_function_dump (function *fn, void *data)
1534 opt_pass *pass = (opt_pass *)data;
1536 if (dump_file)
1538 push_cfun (fn);
1540 if (fn->curr_properties & PROP_trees)
1541 dump_function_to_file (fn->decl, dump_file, dump_flags);
1542 else
1543 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1545 /* Flush the file. If verification fails, we won't be able to
1546 close the file before aborting. */
1547 fflush (dump_file);
1549 if ((fn->curr_properties & PROP_cfg)
1550 && (dump_flags & TDF_GRAPH))
1552 if (!pass->graph_dump_initialized)
1554 clean_graph_dump_file (dump_file_name);
1555 pass->graph_dump_initialized = true;
1557 print_graph_cfg (dump_file_name, fn);
1560 pop_cfun ();
1564 static struct profile_record *profile_record;
1566 /* Do profile consistency book-keeping for the pass with static number INDEX.
1567 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1568 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1569 if we are only book-keeping on passes that may have selectively disabled
1570 themselves on a given function. */
1571 static void
1572 check_profile_consistency (int index, int subpass, bool run)
1574 pass_manager *passes = g->get_passes ();
1575 if (index == -1)
1576 return;
1577 if (!profile_record)
1578 profile_record = XCNEWVEC (struct profile_record,
1579 passes->passes_by_id_size);
1580 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1581 gcc_assert (subpass < 2);
1582 profile_record[index].run |= run;
1583 account_profile_record (&profile_record[index], subpass);
1586 /* Output profile consistency. */
1588 void
1589 dump_profile_report (void)
1591 g->get_passes ()->dump_profile_report ();
1594 void
1595 pass_manager::dump_profile_report () const
1597 int i, j;
1598 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1599 gcov_type last_time = 0, last_size = 0;
1600 double rel_time_change, rel_size_change;
1601 int last_reported = 0;
1603 if (!profile_record)
1604 return;
1605 fprintf (stderr, "\nProfile consistency report:\n\n");
1606 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1607 fprintf (stderr, " |freq count |freq count |size time\n");
1609 for (i = 0; i < passes_by_id_size; i++)
1610 for (j = 0 ; j < 2; j++)
1611 if (profile_record[i].run)
1613 if (last_time)
1614 rel_time_change = (profile_record[i].time[j]
1615 - (double)last_time) * 100 / (double)last_time;
1616 else
1617 rel_time_change = 0;
1618 if (last_size)
1619 rel_size_change = (profile_record[i].size[j]
1620 - (double)last_size) * 100 / (double)last_size;
1621 else
1622 rel_size_change = 0;
1624 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1625 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1626 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1627 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1628 || rel_time_change || rel_size_change)
1630 last_reported = i;
1631 fprintf (stderr, "%-20s %s",
1632 passes_by_id [i]->name,
1633 j ? "(after TODO)" : " ");
1634 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1635 fprintf (stderr, "| %+5i",
1636 profile_record[i].num_mismatched_freq_in[j]
1637 - last_freq_in);
1638 else
1639 fprintf (stderr, "| ");
1640 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1641 fprintf (stderr, " %+5i",
1642 profile_record[i].num_mismatched_count_in[j]
1643 - last_count_in);
1644 else
1645 fprintf (stderr, " ");
1646 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1647 fprintf (stderr, "| %+5i",
1648 profile_record[i].num_mismatched_freq_out[j]
1649 - last_freq_out);
1650 else
1651 fprintf (stderr, "| ");
1652 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1653 fprintf (stderr, " %+5i",
1654 profile_record[i].num_mismatched_count_out[j]
1655 - last_count_out);
1656 else
1657 fprintf (stderr, " ");
1659 /* Size/time units change across gimple and RTL. */
1660 if (i == pass_expand_1->static_pass_number)
1661 fprintf (stderr, "|----------");
1662 else
1664 if (rel_size_change)
1665 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1666 else
1667 fprintf (stderr, "| ");
1668 if (rel_time_change)
1669 fprintf (stderr, " %+8.4f%%", rel_time_change);
1671 fprintf (stderr, "\n");
1672 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1673 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1674 last_count_in = profile_record[i].num_mismatched_count_in[j];
1675 last_count_out = profile_record[i].num_mismatched_count_out[j];
1677 else if (j && last_reported != i)
1679 last_reported = i;
1680 fprintf (stderr, "%-20s ------------| | |\n",
1681 passes_by_id [i]->name);
1683 last_time = profile_record[i].time[j];
1684 last_size = profile_record[i].size[j];
1688 /* Perform all TODO actions that ought to be done on each function. */
1690 static void
1691 execute_function_todo (function *fn, void *data)
1693 bool from_ipa_pass = (cfun == NULL);
1694 unsigned int flags = (size_t)data;
1695 flags &= ~fn->last_verified;
1696 if (!flags)
1697 return;
1699 push_cfun (fn);
1701 /* Always cleanup the CFG before trying to update SSA. */
1702 if (flags & TODO_cleanup_cfg)
1704 cleanup_tree_cfg ();
1706 /* When cleanup_tree_cfg merges consecutive blocks, it may
1707 perform some simplistic propagation when removing single
1708 valued PHI nodes. This propagation may, in turn, cause the
1709 SSA form to become out-of-date (see PR 22037). So, even
1710 if the parent pass had not scheduled an SSA update, we may
1711 still need to do one. */
1712 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1713 flags |= TODO_update_ssa;
1716 if (flags & TODO_update_ssa_any)
1718 unsigned update_flags = flags & TODO_update_ssa_any;
1719 update_ssa (update_flags);
1722 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1723 compute_may_aliases ();
1725 if (optimize && (flags & TODO_update_address_taken))
1726 execute_update_addresses_taken ();
1728 if (flags & TODO_remove_unused_locals)
1729 remove_unused_locals ();
1731 if (flags & TODO_rebuild_frequencies)
1732 rebuild_frequencies ();
1734 if (flags & TODO_rebuild_cgraph_edges)
1735 cgraph_edge::rebuild_edges ();
1737 /* If we've seen errors do not bother running any verifiers. */
1738 if (!seen_error ())
1740 #if defined ENABLE_CHECKING
1741 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1742 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1744 if (flags & TODO_verify_il)
1746 if (cfun->curr_properties & PROP_trees)
1748 if (cfun->curr_properties & PROP_cfg)
1749 /* IPA passes leave stmts to be fixed up, so make sure to
1750 not verify stmts really throw. */
1751 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1752 else
1753 verify_gimple_in_seq (gimple_body (cfun->decl));
1755 if (cfun->curr_properties & PROP_ssa)
1756 /* IPA passes leave stmts to be fixed up, so make sure to
1757 not verify SSA operands whose verifier will choke on that. */
1758 verify_ssa (true, !from_ipa_pass);
1759 /* IPA passes leave basic-blocks unsplit, so make sure to
1760 not trip on that. */
1761 if ((cfun->curr_properties & PROP_cfg)
1762 && !from_ipa_pass)
1763 verify_flow_info ();
1764 if (current_loops
1765 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1766 verify_loop_closed_ssa (false);
1767 if (cfun->curr_properties & PROP_rtl)
1768 verify_rtl_sharing ();
1771 /* Make sure verifiers don't change dominator state. */
1772 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1773 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1774 #endif
1777 fn->last_verified = flags & TODO_verify_all;
1779 pop_cfun ();
1781 /* For IPA passes make sure to release dominator info, it can be
1782 computed by non-verifying TODOs. */
1783 if (from_ipa_pass)
1785 free_dominance_info (fn, CDI_DOMINATORS);
1786 free_dominance_info (fn, CDI_POST_DOMINATORS);
1790 /* Perform all TODO actions. */
1791 static void
1792 execute_todo (unsigned int flags)
1794 #if defined ENABLE_CHECKING
1795 if (cfun
1796 && need_ssa_update_p (cfun))
1797 gcc_assert (flags & TODO_update_ssa_any);
1798 #endif
1800 timevar_push (TV_TODO);
1802 /* Inform the pass whether it is the first time it is run. */
1803 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1805 statistics_fini_pass ();
1807 if (flags)
1808 do_per_function (execute_function_todo, (void *)(size_t) flags);
1810 /* Always remove functions just as before inlining: IPA passes might be
1811 interested to see bodies of extern inline functions that are not inlined
1812 to analyze side effects. The full removal is done just at the end
1813 of IPA pass queue. */
1814 if (flags & TODO_remove_functions)
1816 gcc_assert (!cfun);
1817 symtab->remove_unreachable_nodes (true, dump_file);
1820 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1822 gcc_assert (!cfun);
1823 symtab_node::dump_table (dump_file);
1824 /* Flush the file. If verification fails, we won't be able to
1825 close the file before aborting. */
1826 fflush (dump_file);
1829 /* Now that the dumping has been done, we can get rid of the optional
1830 df problems. */
1831 if (flags & TODO_df_finish)
1832 df_finish_pass ((flags & TODO_df_verify) != 0);
1834 timevar_pop (TV_TODO);
1837 /* Verify invariants that should hold between passes. This is a place
1838 to put simple sanity checks. */
1840 static void
1841 verify_interpass_invariants (void)
1843 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1846 /* Clear the last verified flag. */
1848 static void
1849 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
1851 fn->last_verified = 0;
1854 /* Helper function. Verify that the properties has been turn into the
1855 properties expected by the pass. */
1857 #ifdef ENABLE_CHECKING
1858 static void
1859 verify_curr_properties (function *fn, void *data)
1861 unsigned int props = (size_t)data;
1862 gcc_assert ((fn->curr_properties & props) == props);
1864 #endif
1866 /* Initialize pass dump file. */
1867 /* This is non-static so that the plugins can use it. */
1869 bool
1870 pass_init_dump_file (opt_pass *pass)
1872 /* If a dump file name is present, open it if enabled. */
1873 if (pass->static_pass_number != -1)
1875 timevar_push (TV_DUMP);
1876 gcc::dump_manager *dumps = g->get_dumps ();
1877 bool initializing_dump =
1878 !dumps->dump_initialized_p (pass->static_pass_number);
1879 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
1880 dumps->dump_start (pass->static_pass_number, &dump_flags);
1881 if (dump_file && current_function_decl)
1882 dump_function_header (dump_file, current_function_decl, dump_flags);
1883 if (initializing_dump
1884 && dump_file && (dump_flags & TDF_GRAPH)
1885 && cfun && (cfun->curr_properties & PROP_cfg))
1887 clean_graph_dump_file (dump_file_name);
1888 pass->graph_dump_initialized = true;
1890 timevar_pop (TV_DUMP);
1891 return initializing_dump;
1893 else
1894 return false;
1897 /* Flush PASS dump file. */
1898 /* This is non-static so that plugins can use it. */
1900 void
1901 pass_fini_dump_file (opt_pass *pass)
1903 timevar_push (TV_DUMP);
1905 /* Flush and close dump file. */
1906 if (dump_file_name)
1908 free (CONST_CAST (char *, dump_file_name));
1909 dump_file_name = NULL;
1912 g->get_dumps ()->dump_finish (pass->static_pass_number);
1913 timevar_pop (TV_DUMP);
1916 /* After executing the pass, apply expected changes to the function
1917 properties. */
1919 static void
1920 update_properties_after_pass (function *fn, void *data)
1922 opt_pass *pass = (opt_pass *) data;
1923 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
1924 & ~pass->properties_destroyed;
1927 /* Execute summary generation for all of the passes in IPA_PASS. */
1929 void
1930 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
1932 while (ipa_pass)
1934 opt_pass *pass = ipa_pass;
1936 /* Execute all of the IPA_PASSes in the list. */
1937 if (ipa_pass->type == IPA_PASS
1938 && pass->gate (cfun)
1939 && ipa_pass->generate_summary)
1941 pass_init_dump_file (pass);
1943 /* If a timevar is present, start it. */
1944 if (pass->tv_id)
1945 timevar_push (pass->tv_id);
1947 ipa_pass->generate_summary ();
1949 /* Stop timevar. */
1950 if (pass->tv_id)
1951 timevar_pop (pass->tv_id);
1953 pass_fini_dump_file (pass);
1955 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
1959 /* Execute IPA_PASS function transform on NODE. */
1961 static void
1962 execute_one_ipa_transform_pass (struct cgraph_node *node,
1963 ipa_opt_pass_d *ipa_pass)
1965 opt_pass *pass = ipa_pass;
1966 unsigned int todo_after = 0;
1968 current_pass = pass;
1969 if (!ipa_pass->function_transform)
1970 return;
1972 /* Note that the folders should only create gimple expressions.
1973 This is a hack until the new folder is ready. */
1974 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1976 pass_init_dump_file (pass);
1978 /* Run pre-pass verification. */
1979 execute_todo (ipa_pass->function_transform_todo_flags_start);
1981 /* If a timevar is present, start it. */
1982 if (pass->tv_id != TV_NONE)
1983 timevar_push (pass->tv_id);
1985 /* Do it! */
1986 todo_after = ipa_pass->function_transform (node);
1988 /* Stop timevar. */
1989 if (pass->tv_id != TV_NONE)
1990 timevar_pop (pass->tv_id);
1992 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
1993 check_profile_consistency (pass->static_pass_number, 0, true);
1995 /* Run post-pass cleanup and verification. */
1996 execute_todo (todo_after);
1997 verify_interpass_invariants ();
1998 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
1999 check_profile_consistency (pass->static_pass_number, 1, true);
2001 if (dump_file)
2002 do_per_function (execute_function_dump, NULL);
2003 pass_fini_dump_file (pass);
2005 current_pass = NULL;
2007 /* Signal this is a suitable GC collection point. */
2008 if (!(todo_after & TODO_do_not_ggc_collect))
2009 ggc_collect ();
2012 /* For the current function, execute all ipa transforms. */
2014 void
2015 execute_all_ipa_transforms (void)
2017 struct cgraph_node *node;
2018 if (!cfun)
2019 return;
2020 node = cgraph_node::get (current_function_decl);
2022 if (node->ipa_transforms_to_apply.exists ())
2024 unsigned int i;
2026 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2027 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2028 node->ipa_transforms_to_apply.release ();
2032 /* Check if PASS is explicitly disabled or enabled and return
2033 the gate status. FUNC is the function to be processed, and
2034 GATE_STATUS is the gate status determined by pass manager by
2035 default. */
2037 static bool
2038 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2040 bool explicitly_enabled = false;
2041 bool explicitly_disabled = false;
2043 explicitly_enabled
2044 = is_pass_explicitly_enabled_or_disabled (pass, func,
2045 enabled_pass_uid_range_tab);
2046 explicitly_disabled
2047 = is_pass_explicitly_enabled_or_disabled (pass, func,
2048 disabled_pass_uid_range_tab);
2050 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2052 return gate_status;
2056 /* Execute PASS. */
2058 bool
2059 execute_one_pass (opt_pass *pass)
2061 unsigned int todo_after = 0;
2063 bool gate_status;
2065 /* IPA passes are executed on whole program, so cfun should be NULL.
2066 Other passes need function context set. */
2067 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2068 gcc_assert (!cfun && !current_function_decl);
2069 else
2070 gcc_assert (cfun && current_function_decl);
2072 current_pass = pass;
2074 /* Check whether gate check should be avoided.
2075 User controls the value of the gate through the parameter "gate_status". */
2076 gate_status = pass->gate (cfun);
2077 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2079 /* Override gate with plugin. */
2080 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2082 if (!gate_status)
2084 /* Run so passes selectively disabling themselves on a given function
2085 are not miscounted. */
2086 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2088 check_profile_consistency (pass->static_pass_number, 0, false);
2089 check_profile_consistency (pass->static_pass_number, 1, false);
2091 current_pass = NULL;
2092 return false;
2095 /* Pass execution event trigger: useful to identify passes being
2096 executed. */
2097 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2099 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2100 Apply all trnasforms first. */
2101 if (pass->type == SIMPLE_IPA_PASS)
2103 struct cgraph_node *node;
2104 bool applied = false;
2105 FOR_EACH_DEFINED_FUNCTION (node)
2106 if (node->analyzed
2107 && node->has_gimple_body_p ()
2108 && (!node->clone_of || node->decl != node->clone_of->decl))
2110 if (!node->global.inlined_to
2111 && node->ipa_transforms_to_apply.exists ())
2113 node->get_body ();
2114 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2115 execute_all_ipa_transforms ();
2116 cgraph_edge::rebuild_edges ();
2117 free_dominance_info (CDI_DOMINATORS);
2118 free_dominance_info (CDI_POST_DOMINATORS);
2119 pop_cfun ();
2120 applied = true;
2123 if (applied)
2124 symtab->remove_unreachable_nodes (false, dump_file);
2125 /* Restore current_pass. */
2126 current_pass = pass;
2129 if (!quiet_flag && !cfun)
2130 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2132 /* Note that the folders should only create gimple expressions.
2133 This is a hack until the new folder is ready. */
2134 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2136 pass_init_dump_file (pass);
2138 /* Run pre-pass verification. */
2139 execute_todo (pass->todo_flags_start);
2141 #ifdef ENABLE_CHECKING
2142 do_per_function (verify_curr_properties,
2143 (void *)(size_t)pass->properties_required);
2144 #endif
2146 /* If a timevar is present, start it. */
2147 if (pass->tv_id != TV_NONE)
2148 timevar_push (pass->tv_id);
2150 /* Do it! */
2151 todo_after = pass->execute (cfun);
2152 do_per_function (clear_last_verified, NULL);
2154 /* Stop timevar. */
2155 if (pass->tv_id != TV_NONE)
2156 timevar_pop (pass->tv_id);
2158 do_per_function (update_properties_after_pass, pass);
2160 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2161 check_profile_consistency (pass->static_pass_number, 0, true);
2163 /* Run post-pass cleanup and verification. */
2164 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2165 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2166 check_profile_consistency (pass->static_pass_number, 1, true);
2168 verify_interpass_invariants ();
2169 if (dump_file)
2170 do_per_function (execute_function_dump, pass);
2171 if (pass->type == IPA_PASS)
2173 struct cgraph_node *node;
2174 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2175 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2178 if (!current_function_decl)
2179 symtab->process_new_functions ();
2181 pass_fini_dump_file (pass);
2183 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2184 gcc_assert (!(cfun->curr_properties & PROP_trees)
2185 || pass->type != RTL_PASS);
2187 current_pass = NULL;
2189 /* Signal this is a suitable GC collection point. */
2190 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2191 ggc_collect ();
2193 return true;
2196 static void
2197 execute_pass_list_1 (opt_pass *pass)
2201 gcc_assert (pass->type == GIMPLE_PASS
2202 || pass->type == RTL_PASS);
2203 if (execute_one_pass (pass) && pass->sub)
2204 execute_pass_list_1 (pass->sub);
2205 pass = pass->next;
2207 while (pass);
2210 void
2211 execute_pass_list (function *fn, opt_pass *pass)
2213 push_cfun (fn);
2214 execute_pass_list_1 (pass);
2215 if (fn->cfg)
2217 free_dominance_info (CDI_DOMINATORS);
2218 free_dominance_info (CDI_POST_DOMINATORS);
2220 pop_cfun ();
2223 /* Write out all LTO data. */
2224 static void
2225 write_lto (void)
2227 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2228 lto_output ();
2229 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2230 timevar_push (TV_IPA_LTO_DECL_OUT);
2231 produce_asm_for_decls ();
2232 timevar_pop (TV_IPA_LTO_DECL_OUT);
2235 /* Same as execute_pass_list but assume that subpasses of IPA passes
2236 are local passes. If SET is not NULL, write out summaries of only
2237 those node in SET. */
2239 static void
2240 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2242 while (pass)
2244 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2245 gcc_assert (!current_function_decl);
2246 gcc_assert (!cfun);
2247 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2248 if (pass->type == IPA_PASS
2249 && ipa_pass->write_summary
2250 && pass->gate (cfun))
2252 /* If a timevar is present, start it. */
2253 if (pass->tv_id)
2254 timevar_push (pass->tv_id);
2256 pass_init_dump_file (pass);
2258 ipa_pass->write_summary ();
2260 pass_fini_dump_file (pass);
2262 /* If a timevar is present, start it. */
2263 if (pass->tv_id)
2264 timevar_pop (pass->tv_id);
2267 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2268 ipa_write_summaries_2 (pass->sub, state);
2270 pass = pass->next;
2274 /* Helper function of ipa_write_summaries. Creates and destroys the
2275 decl state and calls ipa_write_summaries_2 for all passes that have
2276 summaries. SET is the set of nodes to be written. */
2278 static void
2279 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2281 pass_manager *passes = g->get_passes ();
2282 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2283 state->symtab_node_encoder = encoder;
2285 lto_push_out_decl_state (state);
2287 gcc_assert (!flag_wpa);
2288 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2290 write_lto ();
2292 gcc_assert (lto_get_out_decl_state () == state);
2293 lto_pop_out_decl_state ();
2294 lto_delete_out_decl_state (state);
2297 /* Write out summaries for all the nodes in the callgraph. */
2299 void
2300 ipa_write_summaries (void)
2302 lto_symtab_encoder_t encoder;
2303 int i, order_pos;
2304 varpool_node *vnode;
2305 struct cgraph_node *node;
2306 struct cgraph_node **order;
2308 if (!flag_generate_lto || seen_error ())
2309 return;
2311 encoder = lto_symtab_encoder_new (false);
2313 /* Create the callgraph set in the same order used in
2314 cgraph_expand_all_functions. This mostly facilitates debugging,
2315 since it causes the gimple file to be processed in the same order
2316 as the source code. */
2317 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2318 order_pos = ipa_reverse_postorder (order);
2319 gcc_assert (order_pos == symtab->cgraph_count);
2321 for (i = order_pos - 1; i >= 0; i--)
2323 struct cgraph_node *node = order[i];
2325 if (node->has_gimple_body_p ())
2327 /* When streaming out references to statements as part of some IPA
2328 pass summary, the statements need to have uids assigned and the
2329 following does that for all the IPA passes here. Naturally, this
2330 ordering then matches the one IPA-passes get in their stmt_fixup
2331 hooks. */
2333 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2334 renumber_gimple_stmt_uids ();
2335 pop_cfun ();
2337 if (node->definition)
2338 lto_set_symtab_encoder_in_partition (encoder, node);
2341 FOR_EACH_DEFINED_FUNCTION (node)
2342 if (node->alias)
2343 lto_set_symtab_encoder_in_partition (encoder, node);
2344 FOR_EACH_DEFINED_VARIABLE (vnode)
2345 lto_set_symtab_encoder_in_partition (encoder, vnode);
2347 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2349 free (order);
2352 /* Same as execute_pass_list but assume that subpasses of IPA passes
2353 are local passes. If SET is not NULL, write out optimization summaries of
2354 only those node in SET. */
2356 static void
2357 ipa_write_optimization_summaries_1 (opt_pass *pass,
2358 struct lto_out_decl_state *state)
2360 while (pass)
2362 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2363 gcc_assert (!current_function_decl);
2364 gcc_assert (!cfun);
2365 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2366 if (pass->type == IPA_PASS
2367 && ipa_pass->write_optimization_summary
2368 && pass->gate (cfun))
2370 /* If a timevar is present, start it. */
2371 if (pass->tv_id)
2372 timevar_push (pass->tv_id);
2374 pass_init_dump_file (pass);
2376 ipa_pass->write_optimization_summary ();
2378 pass_fini_dump_file (pass);
2380 /* If a timevar is present, start it. */
2381 if (pass->tv_id)
2382 timevar_pop (pass->tv_id);
2385 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2386 ipa_write_optimization_summaries_1 (pass->sub, state);
2388 pass = pass->next;
2392 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2393 NULL, write out all summaries of all nodes. */
2395 void
2396 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2398 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2399 lto_symtab_encoder_iterator lsei;
2400 state->symtab_node_encoder = encoder;
2402 lto_push_out_decl_state (state);
2403 for (lsei = lsei_start_function_in_partition (encoder);
2404 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2406 struct cgraph_node *node = lsei_cgraph_node (lsei);
2407 /* When streaming out references to statements as part of some IPA
2408 pass summary, the statements need to have uids assigned.
2410 For functions newly born at WPA stage we need to initialize
2411 the uids here. */
2412 if (node->definition
2413 && gimple_has_body_p (node->decl))
2415 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2416 renumber_gimple_stmt_uids ();
2417 pop_cfun ();
2421 gcc_assert (flag_wpa);
2422 pass_manager *passes = g->get_passes ();
2423 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2425 write_lto ();
2427 gcc_assert (lto_get_out_decl_state () == state);
2428 lto_pop_out_decl_state ();
2429 lto_delete_out_decl_state (state);
2432 /* Same as execute_pass_list but assume that subpasses of IPA passes
2433 are local passes. */
2435 static void
2436 ipa_read_summaries_1 (opt_pass *pass)
2438 while (pass)
2440 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2442 gcc_assert (!current_function_decl);
2443 gcc_assert (!cfun);
2444 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2446 if (pass->gate (cfun))
2448 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2450 /* If a timevar is present, start it. */
2451 if (pass->tv_id)
2452 timevar_push (pass->tv_id);
2454 pass_init_dump_file (pass);
2456 ipa_pass->read_summary ();
2458 pass_fini_dump_file (pass);
2460 /* Stop timevar. */
2461 if (pass->tv_id)
2462 timevar_pop (pass->tv_id);
2465 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2466 ipa_read_summaries_1 (pass->sub);
2468 pass = pass->next;
2473 /* Read all the summaries for all_regular_ipa_passes. */
2475 void
2476 ipa_read_summaries (void)
2478 pass_manager *passes = g->get_passes ();
2479 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2482 /* Same as execute_pass_list but assume that subpasses of IPA passes
2483 are local passes. */
2485 static void
2486 ipa_read_optimization_summaries_1 (opt_pass *pass)
2488 while (pass)
2490 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2492 gcc_assert (!current_function_decl);
2493 gcc_assert (!cfun);
2494 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2496 if (pass->gate (cfun))
2498 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2500 /* If a timevar is present, start it. */
2501 if (pass->tv_id)
2502 timevar_push (pass->tv_id);
2504 pass_init_dump_file (pass);
2506 ipa_pass->read_optimization_summary ();
2508 pass_fini_dump_file (pass);
2510 /* Stop timevar. */
2511 if (pass->tv_id)
2512 timevar_pop (pass->tv_id);
2515 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2516 ipa_read_optimization_summaries_1 (pass->sub);
2518 pass = pass->next;
2522 /* Read all the summaries for all_regular_ipa_passes. */
2524 void
2525 ipa_read_optimization_summaries (void)
2527 pass_manager *passes = g->get_passes ();
2528 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2531 /* Same as execute_pass_list but assume that subpasses of IPA passes
2532 are local passes. */
2533 void
2534 execute_ipa_pass_list (opt_pass *pass)
2538 gcc_assert (!current_function_decl);
2539 gcc_assert (!cfun);
2540 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2541 if (execute_one_pass (pass) && pass->sub)
2543 if (pass->sub->type == GIMPLE_PASS)
2545 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2546 do_per_function_toporder ((void (*)(function *, void *))
2547 execute_pass_list,
2548 pass->sub);
2549 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2551 else if (pass->sub->type == SIMPLE_IPA_PASS
2552 || pass->sub->type == IPA_PASS)
2553 execute_ipa_pass_list (pass->sub);
2554 else
2555 gcc_unreachable ();
2557 gcc_assert (!current_function_decl);
2558 symtab->process_new_functions ();
2559 pass = pass->next;
2561 while (pass);
2564 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2566 static void
2567 execute_ipa_stmt_fixups (opt_pass *pass,
2568 struct cgraph_node *node, gimple *stmts)
2570 while (pass)
2572 /* Execute all of the IPA_PASSes in the list. */
2573 if (pass->type == IPA_PASS
2574 && pass->gate (cfun))
2576 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2578 if (ipa_pass->stmt_fixup)
2580 pass_init_dump_file (pass);
2581 /* If a timevar is present, start it. */
2582 if (pass->tv_id)
2583 timevar_push (pass->tv_id);
2585 ipa_pass->stmt_fixup (node, stmts);
2587 /* Stop timevar. */
2588 if (pass->tv_id)
2589 timevar_pop (pass->tv_id);
2590 pass_fini_dump_file (pass);
2592 if (pass->sub)
2593 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2595 pass = pass->next;
2599 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2601 void
2602 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2604 pass_manager *passes = g->get_passes ();
2605 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2609 extern void debug_properties (unsigned int);
2610 extern void dump_properties (FILE *, unsigned int);
2612 DEBUG_FUNCTION void
2613 dump_properties (FILE *dump, unsigned int props)
2615 fprintf (dump, "Properties:\n");
2616 if (props & PROP_gimple_any)
2617 fprintf (dump, "PROP_gimple_any\n");
2618 if (props & PROP_gimple_lcf)
2619 fprintf (dump, "PROP_gimple_lcf\n");
2620 if (props & PROP_gimple_leh)
2621 fprintf (dump, "PROP_gimple_leh\n");
2622 if (props & PROP_cfg)
2623 fprintf (dump, "PROP_cfg\n");
2624 if (props & PROP_ssa)
2625 fprintf (dump, "PROP_ssa\n");
2626 if (props & PROP_no_crit_edges)
2627 fprintf (dump, "PROP_no_crit_edges\n");
2628 if (props & PROP_rtl)
2629 fprintf (dump, "PROP_rtl\n");
2630 if (props & PROP_gimple_lomp)
2631 fprintf (dump, "PROP_gimple_lomp\n");
2632 if (props & PROP_gimple_lcx)
2633 fprintf (dump, "PROP_gimple_lcx\n");
2634 if (props & PROP_gimple_lvec)
2635 fprintf (dump, "PROP_gimple_lvec\n");
2636 if (props & PROP_cfglayout)
2637 fprintf (dump, "PROP_cfglayout\n");
2640 DEBUG_FUNCTION void
2641 debug_properties (unsigned int props)
2643 dump_properties (stderr, props);
2646 /* Called by local passes to see if function is called by already processed nodes.
2647 Because we process nodes in topological order, this means that function is
2648 in recursive cycle or we introduced new direct calls. */
2649 bool
2650 function_called_by_processed_nodes_p (void)
2652 struct cgraph_edge *e;
2653 for (e = cgraph_node::get (current_function_decl)->callers;
2655 e = e->next_caller)
2657 if (e->caller->decl == current_function_decl)
2658 continue;
2659 if (!e->caller->has_gimple_body_p ())
2660 continue;
2661 if (TREE_ASM_WRITTEN (e->caller->decl))
2662 continue;
2663 if (!e->caller->process && !e->caller->global.inlined_to)
2664 break;
2666 if (dump_file && e)
2668 fprintf (dump_file, "Already processed call to:\n");
2669 e->caller->dump (dump_file);
2671 return e != NULL;
2674 #include "gt-passes.h"