2014-08-01 Eric Botcazou <ebotcazou@adacore.com>
[official-gcc.git] / gcc / passes.c
blobdea9de1432d7a455f00c0ec193767580763285a7
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 (cgraph_state < CGRAPH_STATE_IPA_SSA)
337 cgraph_state = CGRAPH_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_remove_functions, /* todo_flags_finish */
356 class pass_early_local_passes : public simple_ipa_opt_pass
358 public:
359 pass_early_local_passes (gcc::context *ctxt)
360 : simple_ipa_opt_pass (pass_data_early_local_passes, ctxt)
363 /* opt_pass methods: */
364 virtual bool gate (function *)
366 /* Don't bother doing anything if the program has errors. */
367 return (!seen_error () && !in_lto_p);
370 virtual unsigned int execute (function *)
372 return execute_all_early_local_passes ();
375 }; // class pass_early_local_passes
377 } // anon namespace
379 simple_ipa_opt_pass *
380 make_pass_early_local_passes (gcc::context *ctxt)
382 return new pass_early_local_passes (ctxt);
385 namespace {
387 const pass_data pass_data_all_early_optimizations =
389 GIMPLE_PASS, /* type */
390 "early_optimizations", /* name */
391 OPTGROUP_NONE, /* optinfo_flags */
392 TV_NONE, /* tv_id */
393 0, /* properties_required */
394 0, /* properties_provided */
395 0, /* properties_destroyed */
396 0, /* todo_flags_start */
397 0, /* todo_flags_finish */
400 class pass_all_early_optimizations : public gimple_opt_pass
402 public:
403 pass_all_early_optimizations (gcc::context *ctxt)
404 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
407 /* opt_pass methods: */
408 virtual bool gate (function *)
410 return (optimize >= 1
411 /* Don't bother doing anything if the program has errors. */
412 && !seen_error ());
415 }; // class pass_all_early_optimizations
417 } // anon namespace
419 static gimple_opt_pass *
420 make_pass_all_early_optimizations (gcc::context *ctxt)
422 return new pass_all_early_optimizations (ctxt);
425 namespace {
427 const pass_data pass_data_all_optimizations =
429 GIMPLE_PASS, /* type */
430 "*all_optimizations", /* name */
431 OPTGROUP_NONE, /* optinfo_flags */
432 TV_OPTIMIZE, /* tv_id */
433 0, /* properties_required */
434 0, /* properties_provided */
435 0, /* properties_destroyed */
436 0, /* todo_flags_start */
437 0, /* todo_flags_finish */
440 class pass_all_optimizations : public gimple_opt_pass
442 public:
443 pass_all_optimizations (gcc::context *ctxt)
444 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
447 /* opt_pass methods: */
448 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
450 }; // class pass_all_optimizations
452 } // anon namespace
454 static gimple_opt_pass *
455 make_pass_all_optimizations (gcc::context *ctxt)
457 return new pass_all_optimizations (ctxt);
460 namespace {
462 const pass_data pass_data_all_optimizations_g =
464 GIMPLE_PASS, /* type */
465 "*all_optimizations_g", /* name */
466 OPTGROUP_NONE, /* optinfo_flags */
467 TV_OPTIMIZE, /* tv_id */
468 0, /* properties_required */
469 0, /* properties_provided */
470 0, /* properties_destroyed */
471 0, /* todo_flags_start */
472 0, /* todo_flags_finish */
475 class pass_all_optimizations_g : public gimple_opt_pass
477 public:
478 pass_all_optimizations_g (gcc::context *ctxt)
479 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
482 /* opt_pass methods: */
483 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
485 }; // class pass_all_optimizations_g
487 } // anon namespace
489 static gimple_opt_pass *
490 make_pass_all_optimizations_g (gcc::context *ctxt)
492 return new pass_all_optimizations_g (ctxt);
495 namespace {
497 const pass_data pass_data_rest_of_compilation =
499 RTL_PASS, /* type */
500 "*rest_of_compilation", /* name */
501 OPTGROUP_NONE, /* optinfo_flags */
502 TV_REST_OF_COMPILATION, /* tv_id */
503 PROP_rtl, /* properties_required */
504 0, /* properties_provided */
505 0, /* properties_destroyed */
506 0, /* todo_flags_start */
507 0, /* todo_flags_finish */
510 class pass_rest_of_compilation : public rtl_opt_pass
512 public:
513 pass_rest_of_compilation (gcc::context *ctxt)
514 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
517 /* opt_pass methods: */
518 virtual bool gate (function *)
520 /* Early return if there were errors. We can run afoul of our
521 consistency checks, and there's not really much point in fixing them. */
522 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
525 }; // class pass_rest_of_compilation
527 } // anon namespace
529 static rtl_opt_pass *
530 make_pass_rest_of_compilation (gcc::context *ctxt)
532 return new pass_rest_of_compilation (ctxt);
535 namespace {
537 const pass_data pass_data_postreload =
539 RTL_PASS, /* type */
540 "*all-postreload", /* name */
541 OPTGROUP_NONE, /* optinfo_flags */
542 TV_POSTRELOAD, /* tv_id */
543 PROP_rtl, /* properties_required */
544 0, /* properties_provided */
545 0, /* properties_destroyed */
546 0, /* todo_flags_start */
547 0, /* todo_flags_finish */
550 class pass_postreload : public rtl_opt_pass
552 public:
553 pass_postreload (gcc::context *ctxt)
554 : rtl_opt_pass (pass_data_postreload, ctxt)
557 /* opt_pass methods: */
558 virtual bool gate (function *) { return reload_completed; }
560 }; // class pass_postreload
562 } // anon namespace
564 static rtl_opt_pass *
565 make_pass_postreload (gcc::context *ctxt)
567 return new pass_postreload (ctxt);
572 /* Set the static pass number of pass PASS to ID and record that
573 in the mapping from static pass number to pass. */
575 void
576 pass_manager::
577 set_pass_for_id (int id, opt_pass *pass)
579 pass->static_pass_number = id;
580 if (passes_by_id_size <= id)
582 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
583 memset (passes_by_id + passes_by_id_size, 0,
584 (id + 1 - passes_by_id_size) * sizeof (void *));
585 passes_by_id_size = id + 1;
587 passes_by_id[id] = pass;
590 /* Return the pass with the static pass number ID. */
592 opt_pass *
593 pass_manager::get_pass_for_id (int id) const
595 if (id >= passes_by_id_size)
596 return NULL;
597 return passes_by_id[id];
600 /* Iterate over the pass tree allocating dump file numbers. We want
601 to do this depth first, and independent of whether the pass is
602 enabled or not. */
604 void
605 register_one_dump_file (opt_pass *pass)
607 g->get_passes ()->register_one_dump_file (pass);
610 void
611 pass_manager::register_one_dump_file (opt_pass *pass)
613 char *dot_name, *flag_name, *glob_name;
614 const char *name, *full_name, *prefix;
615 char num[10];
616 int flags, id;
617 int optgroup_flags = OPTGROUP_NONE;
618 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
620 /* See below in next_pass_1. */
621 num[0] = '\0';
622 if (pass->static_pass_number != -1)
623 sprintf (num, "%d", ((int) pass->static_pass_number < 0
624 ? 1 : pass->static_pass_number));
626 /* The name is both used to identify the pass for the purposes of plugins,
627 and to specify dump file name and option.
628 The latter two might want something short which is not quite unique; for
629 that reason, we may have a disambiguating prefix, followed by a space
630 to mark the start of the following dump file name / option string. */
631 name = strchr (pass->name, ' ');
632 name = name ? name + 1 : pass->name;
633 dot_name = concat (".", name, num, NULL);
634 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
636 prefix = "ipa-";
637 flags = TDF_IPA;
638 optgroup_flags |= OPTGROUP_IPA;
640 else if (pass->type == GIMPLE_PASS)
642 prefix = "tree-";
643 flags = TDF_TREE;
645 else
647 prefix = "rtl-";
648 flags = TDF_RTL;
651 flag_name = concat (prefix, name, num, NULL);
652 glob_name = concat (prefix, name, NULL);
653 optgroup_flags |= pass->optinfo_flags;
654 /* For any passes that do not have an optgroup set, and which are not
655 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
656 any dump messages are emitted properly under -fopt-info(-optall). */
657 if (optgroup_flags == OPTGROUP_NONE)
658 optgroup_flags = OPTGROUP_OTHER;
659 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
660 optgroup_flags);
661 set_pass_for_id (id, pass);
662 full_name = concat (prefix, pass->name, num, NULL);
663 register_pass_name (pass, full_name);
664 free (CONST_CAST (char *, full_name));
667 /* Register the dump files for the pass_manager starting at PASS. */
669 void
670 pass_manager::register_dump_files (opt_pass *pass)
674 if (pass->name && pass->name[0] != '*')
675 register_one_dump_file (pass);
677 if (pass->sub)
678 register_dump_files (pass->sub);
680 pass = pass->next;
682 while (pass);
685 /* Helper for pass_registry hash table. */
687 struct pass_registry_hasher : default_hashmap_traits
689 static inline hashval_t hash (const char *);
690 static inline bool equal_keys (const char *, const char *);
693 /* Pass registry hash function. */
695 inline hashval_t
696 pass_registry_hasher::hash (const char *name)
698 return htab_hash_string (name);
701 /* Hash equal function */
703 inline bool
704 pass_registry_hasher::equal_keys (const char *s1, const char *s2)
706 return !strcmp (s1, s2);
709 static hash_map<const char *, opt_pass *, pass_registry_hasher>
710 *name_to_pass_map;
712 /* Register PASS with NAME. */
714 static void
715 register_pass_name (opt_pass *pass, const char *name)
717 if (!name_to_pass_map)
718 name_to_pass_map
719 = new hash_map<const char *, opt_pass *, pass_registry_hasher> (256);
721 if (name_to_pass_map->get (name))
722 return; /* Ignore plugin passes. */
724 const char *unique_name = xstrdup (name);
725 name_to_pass_map->put (unique_name, pass);
728 /* Map from pass id to canonicalized pass name. */
730 typedef const char *char_ptr;
731 static vec<char_ptr> pass_tab = vNULL;
733 /* Callback function for traversing NAME_TO_PASS_MAP. */
735 bool
736 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
738 gcc_assert (pass->static_pass_number > 0);
739 gcc_assert (pass_tab.exists ());
741 pass_tab[pass->static_pass_number] = name;
743 return 1;
746 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
747 table for dumping purpose. */
749 static void
750 create_pass_tab (void)
752 if (!flag_dump_passes)
753 return;
755 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
756 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
759 static bool override_gate_status (opt_pass *, tree, bool);
761 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
762 is turned on or not. */
764 static void
765 dump_one_pass (opt_pass *pass, int pass_indent)
767 int indent = 3 * pass_indent;
768 const char *pn;
769 bool is_on, is_really_on;
771 is_on = pass->gate (cfun);
772 is_really_on = override_gate_status (pass, current_function_decl, is_on);
774 if (pass->static_pass_number <= 0)
775 pn = pass->name;
776 else
777 pn = pass_tab[pass->static_pass_number];
779 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
780 (15 - indent < 0 ? 0 : 15 - indent), " ",
781 is_on ? " ON" : " OFF",
782 ((!is_on) == (!is_really_on) ? ""
783 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
786 /* Dump pass list PASS with indentation INDENT. */
788 static void
789 dump_pass_list (opt_pass *pass, int indent)
793 dump_one_pass (pass, indent);
794 if (pass->sub)
795 dump_pass_list (pass->sub, indent + 1);
796 pass = pass->next;
798 while (pass);
801 /* Dump all optimization passes. */
803 void
804 dump_passes (void)
806 g->get_passes ()->dump_passes ();
809 void
810 pass_manager::dump_passes () const
812 struct cgraph_node *n, *node = NULL;
814 create_pass_tab ();
816 FOR_EACH_FUNCTION (n)
817 if (DECL_STRUCT_FUNCTION (n->decl))
819 node = n;
820 break;
823 if (!node)
824 return;
826 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
828 dump_pass_list (all_lowering_passes, 1);
829 dump_pass_list (all_small_ipa_passes, 1);
830 dump_pass_list (all_regular_ipa_passes, 1);
831 dump_pass_list (all_late_ipa_passes, 1);
832 dump_pass_list (all_passes, 1);
834 pop_cfun ();
838 /* Returns the pass with NAME. */
840 static opt_pass *
841 get_pass_by_name (const char *name)
843 opt_pass **p = name_to_pass_map->get (name);
844 if (p)
845 return *p;
847 return NULL;
851 /* Range [start, last]. */
853 struct uid_range
855 unsigned int start;
856 unsigned int last;
857 const char *assem_name;
858 struct uid_range *next;
861 typedef struct uid_range *uid_range_p;
864 static vec<uid_range_p>
865 enabled_pass_uid_range_tab = vNULL;
866 static vec<uid_range_p>
867 disabled_pass_uid_range_tab = vNULL;
870 /* Parse option string for -fdisable- and -fenable-
871 The syntax of the options:
873 -fenable-<pass_name>
874 -fdisable-<pass_name>
876 -fenable-<pass_name>=s1:e1,s2:e2,...
877 -fdisable-<pass_name>=s1:e1,s2:e2,...
880 static void
881 enable_disable_pass (const char *arg, bool is_enable)
883 opt_pass *pass;
884 char *range_str, *phase_name;
885 char *argstr = xstrdup (arg);
886 vec<uid_range_p> *tab = 0;
888 range_str = strchr (argstr,'=');
889 if (range_str)
891 *range_str = '\0';
892 range_str++;
895 phase_name = argstr;
896 if (!*phase_name)
898 if (is_enable)
899 error ("unrecognized option -fenable");
900 else
901 error ("unrecognized option -fdisable");
902 free (argstr);
903 return;
905 pass = get_pass_by_name (phase_name);
906 if (!pass || pass->static_pass_number == -1)
908 if (is_enable)
909 error ("unknown pass %s specified in -fenable", phase_name);
910 else
911 error ("unknown pass %s specified in -fdisable", phase_name);
912 free (argstr);
913 return;
916 if (is_enable)
917 tab = &enabled_pass_uid_range_tab;
918 else
919 tab = &disabled_pass_uid_range_tab;
921 if ((unsigned) pass->static_pass_number >= tab->length ())
922 tab->safe_grow_cleared (pass->static_pass_number + 1);
924 if (!range_str)
926 uid_range_p slot;
927 uid_range_p new_range = XCNEW (struct uid_range);
929 new_range->start = 0;
930 new_range->last = (unsigned)-1;
932 slot = (*tab)[pass->static_pass_number];
933 new_range->next = slot;
934 (*tab)[pass->static_pass_number] = new_range;
935 if (is_enable)
936 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
937 "of [%u, %u]", phase_name, new_range->start, new_range->last);
938 else
939 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
940 "of [%u, %u]", phase_name, new_range->start, new_range->last);
942 else
944 char *next_range = NULL;
945 char *one_range = range_str;
946 char *end_val = NULL;
950 uid_range_p slot;
951 uid_range_p new_range;
952 char *invalid = NULL;
953 long start;
954 char *func_name = NULL;
956 next_range = strchr (one_range, ',');
957 if (next_range)
959 *next_range = '\0';
960 next_range++;
963 end_val = strchr (one_range, ':');
964 if (end_val)
966 *end_val = '\0';
967 end_val++;
969 start = strtol (one_range, &invalid, 10);
970 if (*invalid || start < 0)
972 if (end_val || (one_range[0] >= '0'
973 && one_range[0] <= '9'))
975 error ("Invalid range %s in option %s",
976 one_range,
977 is_enable ? "-fenable" : "-fdisable");
978 free (argstr);
979 return;
981 func_name = one_range;
983 if (!end_val)
985 new_range = XCNEW (struct uid_range);
986 if (!func_name)
988 new_range->start = (unsigned) start;
989 new_range->last = (unsigned) start;
991 else
993 new_range->start = (unsigned) -1;
994 new_range->last = (unsigned) -1;
995 new_range->assem_name = xstrdup (func_name);
998 else
1000 long last = strtol (end_val, &invalid, 10);
1001 if (*invalid || last < start)
1003 error ("Invalid range %s in option %s",
1004 end_val,
1005 is_enable ? "-fenable" : "-fdisable");
1006 free (argstr);
1007 return;
1009 new_range = XCNEW (struct uid_range);
1010 new_range->start = (unsigned) start;
1011 new_range->last = (unsigned) last;
1014 slot = (*tab)[pass->static_pass_number];
1015 new_range->next = slot;
1016 (*tab)[pass->static_pass_number] = new_range;
1017 if (is_enable)
1019 if (new_range->assem_name)
1020 inform (UNKNOWN_LOCATION,
1021 "enable pass %s for function %s",
1022 phase_name, new_range->assem_name);
1023 else
1024 inform (UNKNOWN_LOCATION,
1025 "enable pass %s for functions in the range of [%u, %u]",
1026 phase_name, new_range->start, new_range->last);
1028 else
1030 if (new_range->assem_name)
1031 inform (UNKNOWN_LOCATION,
1032 "disable pass %s for function %s",
1033 phase_name, new_range->assem_name);
1034 else
1035 inform (UNKNOWN_LOCATION,
1036 "disable pass %s for functions in the range of [%u, %u]",
1037 phase_name, new_range->start, new_range->last);
1040 one_range = next_range;
1041 } while (next_range);
1044 free (argstr);
1047 /* Enable pass specified by ARG. */
1049 void
1050 enable_pass (const char *arg)
1052 enable_disable_pass (arg, true);
1055 /* Disable pass specified by ARG. */
1057 void
1058 disable_pass (const char *arg)
1060 enable_disable_pass (arg, false);
1063 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1065 static bool
1066 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1067 tree func,
1068 vec<uid_range_p> tab)
1070 uid_range_p slot, range;
1071 int cgraph_uid;
1072 const char *aname = NULL;
1074 if (!tab.exists ()
1075 || (unsigned) pass->static_pass_number >= tab.length ()
1076 || pass->static_pass_number == -1)
1077 return false;
1079 slot = tab[pass->static_pass_number];
1080 if (!slot)
1081 return false;
1083 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1084 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1085 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1087 range = slot;
1088 while (range)
1090 if ((unsigned) cgraph_uid >= range->start
1091 && (unsigned) cgraph_uid <= range->last)
1092 return true;
1093 if (range->assem_name && aname
1094 && !strcmp (range->assem_name, aname))
1095 return true;
1096 range = range->next;
1099 return false;
1103 /* Update static_pass_number for passes (and the flag
1104 TODO_mark_first_instance).
1106 Passes are constructed with static_pass_number preinitialized to 0
1108 This field is used in two different ways: initially as instance numbers
1109 of their kind, and then as ids within the entire pass manager.
1111 Within pass_manager::pass_manager:
1113 * In add_pass_instance(), as called by next_pass_1 in
1114 NEXT_PASS in init_optimization_passes
1116 * When the initial instance of a pass within a pass manager is seen,
1117 it is flagged, and its static_pass_number is set to -1
1119 * On subsequent times that it is seen, the static pass number
1120 is decremented each time, so that if there are e.g. 4 dups,
1121 they have static_pass_number -4, 2, 3, 4 respectively (note
1122 how the initial one is negative and gives the count); these
1123 can be thought of as instance numbers of the specific pass
1125 * Within the register_dump_files () traversal, set_pass_for_id()
1126 is called on each pass, using these instance numbers to create
1127 dumpfile switches, and then overwriting them with a pass id,
1128 which are global to the whole pass manager (based on
1129 (TDI_end + current value of extra_dump_files_in_use) ) */
1131 static void
1132 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1133 opt_pass *initial_pass)
1135 /* Are we dealing with the first pass of its kind, or a clone? */
1136 if (new_pass != initial_pass)
1138 /* We're dealing with a clone. */
1139 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1141 /* Indicate to register_dump_files that this pass has duplicates,
1142 and so it should rename the dump file. The first instance will
1143 be -1, and be number of duplicates = -static_pass_number - 1.
1144 Subsequent instances will be > 0 and just the duplicate number. */
1145 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1147 initial_pass->static_pass_number -= 1;
1148 new_pass->static_pass_number = -initial_pass->static_pass_number;
1151 else
1153 /* We're dealing with the first pass of its kind. */
1154 new_pass->todo_flags_start |= TODO_mark_first_instance;
1155 new_pass->static_pass_number = -1;
1157 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1161 /* Add a pass to the pass list. Duplicate the pass if it's already
1162 in the list. */
1164 static opt_pass **
1165 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1167 /* Every pass should have a name so that plugins can refer to them. */
1168 gcc_assert (pass->name != NULL);
1170 add_pass_instance (pass, false, initial_pass);
1171 *list = pass;
1173 return &(*list)->next;
1176 /* List node for an inserted pass instance. We need to keep track of all
1177 the newly-added pass instances (with 'added_pass_nodes' defined below)
1178 so that we can register their dump files after pass-positioning is finished.
1179 Registering dumping files needs to be post-processed or the
1180 static_pass_number of the opt_pass object would be modified and mess up
1181 the dump file names of future pass instances to be added. */
1183 struct pass_list_node
1185 opt_pass *pass;
1186 struct pass_list_node *next;
1189 static struct pass_list_node *added_pass_nodes = NULL;
1190 static struct pass_list_node *prev_added_pass_node;
1192 /* Insert the pass at the proper position. Return true if the pass
1193 is successfully added.
1195 NEW_PASS_INFO - new pass to be inserted
1196 PASS_LIST - root of the pass list to insert the new pass to */
1198 static bool
1199 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1201 opt_pass *pass = *pass_list, *prev_pass = NULL;
1202 bool success = false;
1204 for ( ; pass; prev_pass = pass, pass = pass->next)
1206 /* Check if the current pass is of the same type as the new pass and
1207 matches the name and the instance number of the reference pass. */
1208 if (pass->type == new_pass_info->pass->type
1209 && pass->name
1210 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1211 && ((new_pass_info->ref_pass_instance_number == 0)
1212 || (new_pass_info->ref_pass_instance_number ==
1213 pass->static_pass_number)
1214 || (new_pass_info->ref_pass_instance_number == 1
1215 && pass->todo_flags_start & TODO_mark_first_instance)))
1217 opt_pass *new_pass;
1218 struct pass_list_node *new_pass_node;
1220 if (new_pass_info->ref_pass_instance_number == 0)
1222 new_pass = new_pass_info->pass->clone ();
1223 add_pass_instance (new_pass, true, new_pass_info->pass);
1225 else
1227 new_pass = new_pass_info->pass;
1228 add_pass_instance (new_pass, true, new_pass);
1231 /* Insert the new pass instance based on the positioning op. */
1232 switch (new_pass_info->pos_op)
1234 case PASS_POS_INSERT_AFTER:
1235 new_pass->next = pass->next;
1236 pass->next = new_pass;
1238 /* Skip newly inserted pass to avoid repeated
1239 insertions in the case where the new pass and the
1240 existing one have the same name. */
1241 pass = new_pass;
1242 break;
1243 case PASS_POS_INSERT_BEFORE:
1244 new_pass->next = pass;
1245 if (prev_pass)
1246 prev_pass->next = new_pass;
1247 else
1248 *pass_list = new_pass;
1249 break;
1250 case PASS_POS_REPLACE:
1251 new_pass->next = pass->next;
1252 if (prev_pass)
1253 prev_pass->next = new_pass;
1254 else
1255 *pass_list = new_pass;
1256 new_pass->sub = pass->sub;
1257 new_pass->tv_id = pass->tv_id;
1258 pass = new_pass;
1259 break;
1260 default:
1261 error ("invalid pass positioning operation");
1262 return false;
1265 /* Save the newly added pass (instance) in the added_pass_nodes
1266 list so that we can register its dump file later. Note that
1267 we cannot register the dump file now because doing so will modify
1268 the static_pass_number of the opt_pass object and therefore
1269 mess up the dump file name of future instances. */
1270 new_pass_node = XCNEW (struct pass_list_node);
1271 new_pass_node->pass = new_pass;
1272 if (!added_pass_nodes)
1273 added_pass_nodes = new_pass_node;
1274 else
1275 prev_added_pass_node->next = new_pass_node;
1276 prev_added_pass_node = new_pass_node;
1278 success = true;
1281 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1282 success = true;
1285 return success;
1288 /* Hooks a new pass into the pass lists.
1290 PASS_INFO - pass information that specifies the opt_pass object,
1291 reference pass, instance number, and how to position
1292 the pass */
1294 void
1295 register_pass (struct register_pass_info *pass_info)
1297 g->get_passes ()->register_pass (pass_info);
1300 void
1301 register_pass (opt_pass* pass, pass_positioning_ops pos,
1302 const char* ref_pass_name, int ref_pass_inst_number)
1304 register_pass_info i;
1305 i.pass = pass;
1306 i.reference_pass_name = ref_pass_name;
1307 i.ref_pass_instance_number = ref_pass_inst_number;
1308 i.pos_op = pos;
1310 g->get_passes ()->register_pass (&i);
1313 void
1314 pass_manager::register_pass (struct register_pass_info *pass_info)
1316 bool all_instances, success;
1317 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1319 /* The checks below could fail in buggy plugins. Existing GCC
1320 passes should never fail these checks, so we mention plugin in
1321 the messages. */
1322 if (!pass_info->pass)
1323 fatal_error ("plugin cannot register a missing pass");
1325 if (!pass_info->pass->name)
1326 fatal_error ("plugin cannot register an unnamed pass");
1328 if (!pass_info->reference_pass_name)
1329 fatal_error
1330 ("plugin cannot register pass %qs without reference pass name",
1331 pass_info->pass->name);
1333 /* Try to insert the new pass to the pass lists. We need to check
1334 all five lists as the reference pass could be in one (or all) of
1335 them. */
1336 all_instances = pass_info->ref_pass_instance_number == 0;
1337 success = position_pass (pass_info, &all_lowering_passes);
1338 if (!success || all_instances)
1339 success |= position_pass (pass_info, &all_small_ipa_passes);
1340 if (!success || all_instances)
1341 success |= position_pass (pass_info, &all_regular_ipa_passes);
1342 if (!success || all_instances)
1343 success |= position_pass (pass_info, &all_late_ipa_passes);
1344 if (!success || all_instances)
1345 success |= position_pass (pass_info, &all_passes);
1346 if (!success)
1347 fatal_error
1348 ("pass %qs not found but is referenced by new pass %qs",
1349 pass_info->reference_pass_name, pass_info->pass->name);
1351 /* OK, we have successfully inserted the new pass. We need to register
1352 the dump files for the newly added pass and its duplicates (if any).
1353 Because the registration of plugin/backend passes happens after the
1354 command-line options are parsed, the options that specify single
1355 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1356 passes. Therefore we currently can only enable dumping of
1357 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1358 are specified. While doing so, we also delete the pass_list_node
1359 objects created during pass positioning. */
1360 while (added_pass_nodes)
1362 struct pass_list_node *next_node = added_pass_nodes->next;
1363 enum tree_dump_index tdi;
1364 register_one_dump_file (added_pass_nodes->pass);
1365 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1366 || added_pass_nodes->pass->type == IPA_PASS)
1367 tdi = TDI_ipa_all;
1368 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1369 tdi = TDI_tree_all;
1370 else
1371 tdi = TDI_rtl_all;
1372 /* Check if dump-all flag is specified. */
1373 if (dumps->get_dump_file_info (tdi)->pstate)
1374 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1375 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1376 XDELETE (added_pass_nodes);
1377 added_pass_nodes = next_node;
1381 /* Construct the pass tree. The sequencing of passes is driven by
1382 the cgraph routines:
1384 finalize_compilation_unit ()
1385 for each node N in the cgraph
1386 cgraph_analyze_function (N)
1387 cgraph_lower_function (N) -> all_lowering_passes
1389 If we are optimizing, compile is then invoked:
1391 compile ()
1392 ipa_passes () -> all_small_ipa_passes
1393 -> Analysis of all_regular_ipa_passes
1394 * possible LTO streaming at copmilation time *
1395 -> Execution of all_regular_ipa_passes
1396 * possible LTO streaming at link time *
1397 -> all_late_ipa_passes
1398 expand_all_functions ()
1399 for each node N in the cgraph
1400 expand_function (N) -> Transformation of all_regular_ipa_passes
1401 -> all_passes
1404 void *
1405 pass_manager::operator new (size_t sz)
1407 /* Ensure that all fields of the pass manager are zero-initialized. */
1408 return xcalloc (1, sz);
1411 pass_manager::pass_manager (context *ctxt)
1412 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1413 all_regular_ipa_passes (NULL),
1414 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1415 m_ctxt (ctxt)
1417 opt_pass **p;
1419 /* Initialize the pass_lists array. */
1420 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1421 GCC_PASS_LISTS
1422 #undef DEF_PASS_LIST
1424 /* Build the tree of passes. */
1426 #define INSERT_PASSES_AFTER(PASS) \
1427 p = &(PASS);
1429 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1431 opt_pass **p = &(PASS ## _1)->sub;
1433 #define POP_INSERT_PASSES() \
1436 #define NEXT_PASS(PASS, NUM) \
1437 do { \
1438 gcc_assert (NULL == PASS ## _ ## NUM); \
1439 if ((NUM) == 1) \
1440 PASS ## _1 = make_##PASS (m_ctxt); \
1441 else \
1443 gcc_assert (PASS ## _1); \
1444 PASS ## _ ## NUM = PASS ## _1->clone (); \
1446 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1447 } while (0)
1449 #define TERMINATE_PASS_LIST() \
1450 *p = NULL;
1452 #include "pass-instances.def"
1454 #undef INSERT_PASSES_AFTER
1455 #undef PUSH_INSERT_PASSES_WITHIN
1456 #undef POP_INSERT_PASSES
1457 #undef NEXT_PASS
1458 #undef TERMINATE_PASS_LIST
1460 /* Register the passes with the tree dump code. */
1461 register_dump_files (all_lowering_passes);
1462 register_dump_files (all_small_ipa_passes);
1463 register_dump_files (all_regular_ipa_passes);
1464 register_dump_files (all_late_ipa_passes);
1465 register_dump_files (all_passes);
1468 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1469 function CALLBACK for every function in the call graph. Otherwise,
1470 call CALLBACK on the current function. */
1472 static void
1473 do_per_function (void (*callback) (function *, void *data), void *data)
1475 if (current_function_decl)
1476 callback (cfun, data);
1477 else
1479 struct cgraph_node *node;
1480 FOR_EACH_DEFINED_FUNCTION (node)
1481 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1482 && (!node->clone_of || node->decl != node->clone_of->decl))
1483 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1487 /* Because inlining might remove no-longer reachable nodes, we need to
1488 keep the array visible to garbage collector to avoid reading collected
1489 out nodes. */
1490 static int nnodes;
1491 static GTY ((length ("nnodes"))) cgraph_node **order;
1493 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1494 function CALLBACK for every function in the call graph. Otherwise,
1495 call CALLBACK on the current function.
1496 This function is global so that plugins can use it. */
1497 void
1498 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1500 int i;
1502 if (current_function_decl)
1503 callback (cfun, data);
1504 else
1506 gcc_assert (!order);
1507 order = ggc_vec_alloc<cgraph_node *> (cgraph_n_nodes);
1508 nnodes = ipa_reverse_postorder (order);
1509 for (i = nnodes - 1; i >= 0; i--)
1510 order[i]->process = 1;
1511 for (i = nnodes - 1; i >= 0; i--)
1513 struct cgraph_node *node = order[i];
1515 /* Allow possibly removed nodes to be garbage collected. */
1516 order[i] = NULL;
1517 node->process = 0;
1518 if (node->has_gimple_body_p ())
1519 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1522 ggc_free (order);
1523 order = NULL;
1524 nnodes = 0;
1527 /* Helper function to perform function body dump. */
1529 static void
1530 execute_function_dump (function *fn, void *data)
1532 opt_pass *pass = (opt_pass *)data;
1534 if (dump_file)
1536 push_cfun (fn);
1538 if (fn->curr_properties & PROP_trees)
1539 dump_function_to_file (fn->decl, dump_file, dump_flags);
1540 else
1541 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1543 /* Flush the file. If verification fails, we won't be able to
1544 close the file before aborting. */
1545 fflush (dump_file);
1547 if ((fn->curr_properties & PROP_cfg)
1548 && (dump_flags & TDF_GRAPH))
1550 if (!pass->graph_dump_initialized)
1552 clean_graph_dump_file (dump_file_name);
1553 pass->graph_dump_initialized = true;
1555 print_graph_cfg (dump_file_name, fn);
1558 pop_cfun ();
1562 static struct profile_record *profile_record;
1564 /* Do profile consistency book-keeping for the pass with static number INDEX.
1565 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1566 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1567 if we are only book-keeping on passes that may have selectively disabled
1568 themselves on a given function. */
1569 static void
1570 check_profile_consistency (int index, int subpass, bool run)
1572 pass_manager *passes = g->get_passes ();
1573 if (index == -1)
1574 return;
1575 if (!profile_record)
1576 profile_record = XCNEWVEC (struct profile_record,
1577 passes->passes_by_id_size);
1578 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1579 gcc_assert (subpass < 2);
1580 profile_record[index].run |= run;
1581 account_profile_record (&profile_record[index], subpass);
1584 /* Output profile consistency. */
1586 void
1587 dump_profile_report (void)
1589 g->get_passes ()->dump_profile_report ();
1592 void
1593 pass_manager::dump_profile_report () const
1595 int i, j;
1596 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1597 gcov_type last_time = 0, last_size = 0;
1598 double rel_time_change, rel_size_change;
1599 int last_reported = 0;
1601 if (!profile_record)
1602 return;
1603 fprintf (stderr, "\nProfile consistency report:\n\n");
1604 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1605 fprintf (stderr, " |freq count |freq count |size time\n");
1607 for (i = 0; i < passes_by_id_size; i++)
1608 for (j = 0 ; j < 2; j++)
1609 if (profile_record[i].run)
1611 if (last_time)
1612 rel_time_change = (profile_record[i].time[j]
1613 - (double)last_time) * 100 / (double)last_time;
1614 else
1615 rel_time_change = 0;
1616 if (last_size)
1617 rel_size_change = (profile_record[i].size[j]
1618 - (double)last_size) * 100 / (double)last_size;
1619 else
1620 rel_size_change = 0;
1622 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1623 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1624 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1625 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1626 || rel_time_change || rel_size_change)
1628 last_reported = i;
1629 fprintf (stderr, "%-20s %s",
1630 passes_by_id [i]->name,
1631 j ? "(after TODO)" : " ");
1632 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1633 fprintf (stderr, "| %+5i",
1634 profile_record[i].num_mismatched_freq_in[j]
1635 - last_freq_in);
1636 else
1637 fprintf (stderr, "| ");
1638 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1639 fprintf (stderr, " %+5i",
1640 profile_record[i].num_mismatched_count_in[j]
1641 - last_count_in);
1642 else
1643 fprintf (stderr, " ");
1644 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1645 fprintf (stderr, "| %+5i",
1646 profile_record[i].num_mismatched_freq_out[j]
1647 - last_freq_out);
1648 else
1649 fprintf (stderr, "| ");
1650 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1651 fprintf (stderr, " %+5i",
1652 profile_record[i].num_mismatched_count_out[j]
1653 - last_count_out);
1654 else
1655 fprintf (stderr, " ");
1657 /* Size/time units change across gimple and RTL. */
1658 if (i == pass_expand_1->static_pass_number)
1659 fprintf (stderr, "|----------");
1660 else
1662 if (rel_size_change)
1663 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1664 else
1665 fprintf (stderr, "| ");
1666 if (rel_time_change)
1667 fprintf (stderr, " %+8.4f%%", rel_time_change);
1669 fprintf (stderr, "\n");
1670 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1671 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1672 last_count_in = profile_record[i].num_mismatched_count_in[j];
1673 last_count_out = profile_record[i].num_mismatched_count_out[j];
1675 else if (j && last_reported != i)
1677 last_reported = i;
1678 fprintf (stderr, "%-20s ------------| | |\n",
1679 passes_by_id [i]->name);
1681 last_time = profile_record[i].time[j];
1682 last_size = profile_record[i].size[j];
1686 /* Perform all TODO actions that ought to be done on each function. */
1688 static void
1689 execute_function_todo (function *fn, void *data)
1691 bool from_ipa_pass = (cfun == NULL);
1692 unsigned int flags = (size_t)data;
1693 flags &= ~fn->last_verified;
1694 if (!flags)
1695 return;
1697 push_cfun (fn);
1699 /* Always cleanup the CFG before trying to update SSA. */
1700 if (flags & TODO_cleanup_cfg)
1702 cleanup_tree_cfg ();
1704 /* When cleanup_tree_cfg merges consecutive blocks, it may
1705 perform some simplistic propagation when removing single
1706 valued PHI nodes. This propagation may, in turn, cause the
1707 SSA form to become out-of-date (see PR 22037). So, even
1708 if the parent pass had not scheduled an SSA update, we may
1709 still need to do one. */
1710 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1711 flags |= TODO_update_ssa;
1714 if (flags & TODO_update_ssa_any)
1716 unsigned update_flags = flags & TODO_update_ssa_any;
1717 update_ssa (update_flags);
1720 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1721 compute_may_aliases ();
1723 if (optimize && (flags & TODO_update_address_taken))
1724 execute_update_addresses_taken ();
1726 if (flags & TODO_remove_unused_locals)
1727 remove_unused_locals ();
1729 if (flags & TODO_rebuild_frequencies)
1730 rebuild_frequencies ();
1732 if (flags & TODO_rebuild_cgraph_edges)
1733 rebuild_cgraph_edges ();
1735 /* If we've seen errors do not bother running any verifiers. */
1736 if (!seen_error ())
1738 #if defined ENABLE_CHECKING
1739 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1740 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1742 if (flags & TODO_verify_il)
1744 if (cfun->curr_properties & PROP_trees)
1746 if (cfun->curr_properties & PROP_cfg)
1747 /* IPA passes leave stmts to be fixed up, so make sure to
1748 not verify stmts really throw. */
1749 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1750 else
1751 verify_gimple_in_seq (gimple_body (cfun->decl));
1753 if (cfun->curr_properties & PROP_ssa)
1754 /* IPA passes leave stmts to be fixed up, so make sure to
1755 not verify SSA operands whose verifier will choke on that. */
1756 verify_ssa (true, !from_ipa_pass);
1757 /* IPA passes leave basic-blocks unsplit, so make sure to
1758 not trip on that. */
1759 if ((cfun->curr_properties & PROP_cfg)
1760 && !from_ipa_pass)
1761 verify_flow_info ();
1762 if (current_loops
1763 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1764 verify_loop_closed_ssa (false);
1765 if (cfun->curr_properties & PROP_rtl)
1766 verify_rtl_sharing ();
1769 /* Make sure verifiers don't change dominator state. */
1770 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1771 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1772 #endif
1775 fn->last_verified = flags & TODO_verify_all;
1777 pop_cfun ();
1779 /* For IPA passes make sure to release dominator info, it can be
1780 computed by non-verifying TODOs. */
1781 if (from_ipa_pass)
1783 free_dominance_info (fn, CDI_DOMINATORS);
1784 free_dominance_info (fn, CDI_POST_DOMINATORS);
1788 /* Perform all TODO actions. */
1789 static void
1790 execute_todo (unsigned int flags)
1792 #if defined ENABLE_CHECKING
1793 if (cfun
1794 && need_ssa_update_p (cfun))
1795 gcc_assert (flags & TODO_update_ssa_any);
1796 #endif
1798 timevar_push (TV_TODO);
1800 /* Inform the pass whether it is the first time it is run. */
1801 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1803 statistics_fini_pass ();
1805 if (flags)
1806 do_per_function (execute_function_todo, (void *)(size_t) flags);
1808 /* Always remove functions just as before inlining: IPA passes might be
1809 interested to see bodies of extern inline functions that are not inlined
1810 to analyze side effects. The full removal is done just at the end
1811 of IPA pass queue. */
1812 if (flags & TODO_remove_functions)
1814 gcc_assert (!cfun);
1815 symtab_remove_unreachable_nodes (true, dump_file);
1818 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1820 gcc_assert (!cfun);
1821 symtab_node::dump_table (dump_file);
1822 /* Flush the file. If verification fails, we won't be able to
1823 close the file before aborting. */
1824 fflush (dump_file);
1827 /* Now that the dumping has been done, we can get rid of the optional
1828 df problems. */
1829 if (flags & TODO_df_finish)
1830 df_finish_pass ((flags & TODO_df_verify) != 0);
1832 timevar_pop (TV_TODO);
1835 /* Verify invariants that should hold between passes. This is a place
1836 to put simple sanity checks. */
1838 static void
1839 verify_interpass_invariants (void)
1841 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1844 /* Clear the last verified flag. */
1846 static void
1847 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
1849 fn->last_verified = 0;
1852 /* Helper function. Verify that the properties has been turn into the
1853 properties expected by the pass. */
1855 #ifdef ENABLE_CHECKING
1856 static void
1857 verify_curr_properties (function *fn, void *data)
1859 unsigned int props = (size_t)data;
1860 gcc_assert ((fn->curr_properties & props) == props);
1862 #endif
1864 /* Initialize pass dump file. */
1865 /* This is non-static so that the plugins can use it. */
1867 bool
1868 pass_init_dump_file (opt_pass *pass)
1870 /* If a dump file name is present, open it if enabled. */
1871 if (pass->static_pass_number != -1)
1873 timevar_push (TV_DUMP);
1874 gcc::dump_manager *dumps = g->get_dumps ();
1875 bool initializing_dump =
1876 !dumps->dump_initialized_p (pass->static_pass_number);
1877 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
1878 dumps->dump_start (pass->static_pass_number, &dump_flags);
1879 if (dump_file && current_function_decl)
1880 dump_function_header (dump_file, current_function_decl, dump_flags);
1881 if (initializing_dump
1882 && dump_file && (dump_flags & TDF_GRAPH)
1883 && cfun && (cfun->curr_properties & PROP_cfg))
1885 clean_graph_dump_file (dump_file_name);
1886 pass->graph_dump_initialized = true;
1888 timevar_pop (TV_DUMP);
1889 return initializing_dump;
1891 else
1892 return false;
1895 /* Flush PASS dump file. */
1896 /* This is non-static so that plugins can use it. */
1898 void
1899 pass_fini_dump_file (opt_pass *pass)
1901 timevar_push (TV_DUMP);
1903 /* Flush and close dump file. */
1904 if (dump_file_name)
1906 free (CONST_CAST (char *, dump_file_name));
1907 dump_file_name = NULL;
1910 g->get_dumps ()->dump_finish (pass->static_pass_number);
1911 timevar_pop (TV_DUMP);
1914 /* After executing the pass, apply expected changes to the function
1915 properties. */
1917 static void
1918 update_properties_after_pass (function *fn, void *data)
1920 opt_pass *pass = (opt_pass *) data;
1921 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
1922 & ~pass->properties_destroyed;
1925 /* Execute summary generation for all of the passes in IPA_PASS. */
1927 void
1928 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
1930 while (ipa_pass)
1932 opt_pass *pass = ipa_pass;
1934 /* Execute all of the IPA_PASSes in the list. */
1935 if (ipa_pass->type == IPA_PASS
1936 && pass->gate (cfun)
1937 && ipa_pass->generate_summary)
1939 pass_init_dump_file (pass);
1941 /* If a timevar is present, start it. */
1942 if (pass->tv_id)
1943 timevar_push (pass->tv_id);
1945 ipa_pass->generate_summary ();
1947 /* Stop timevar. */
1948 if (pass->tv_id)
1949 timevar_pop (pass->tv_id);
1951 pass_fini_dump_file (pass);
1953 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
1957 /* Execute IPA_PASS function transform on NODE. */
1959 static void
1960 execute_one_ipa_transform_pass (struct cgraph_node *node,
1961 ipa_opt_pass_d *ipa_pass)
1963 opt_pass *pass = ipa_pass;
1964 unsigned int todo_after = 0;
1966 current_pass = pass;
1967 if (!ipa_pass->function_transform)
1968 return;
1970 /* Note that the folders should only create gimple expressions.
1971 This is a hack until the new folder is ready. */
1972 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1974 pass_init_dump_file (pass);
1976 /* Run pre-pass verification. */
1977 execute_todo (ipa_pass->function_transform_todo_flags_start);
1979 /* If a timevar is present, start it. */
1980 if (pass->tv_id != TV_NONE)
1981 timevar_push (pass->tv_id);
1983 /* Do it! */
1984 todo_after = ipa_pass->function_transform (node);
1986 /* Stop timevar. */
1987 if (pass->tv_id != TV_NONE)
1988 timevar_pop (pass->tv_id);
1990 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
1991 check_profile_consistency (pass->static_pass_number, 0, true);
1993 /* Run post-pass cleanup and verification. */
1994 execute_todo (todo_after);
1995 verify_interpass_invariants ();
1996 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
1997 check_profile_consistency (pass->static_pass_number, 1, true);
1999 if (dump_file)
2000 do_per_function (execute_function_dump, NULL);
2001 pass_fini_dump_file (pass);
2003 current_pass = NULL;
2005 /* Signal this is a suitable GC collection point. */
2006 if (!(todo_after & TODO_do_not_ggc_collect))
2007 ggc_collect ();
2010 /* For the current function, execute all ipa transforms. */
2012 void
2013 execute_all_ipa_transforms (void)
2015 struct cgraph_node *node;
2016 if (!cfun)
2017 return;
2018 node = cgraph_node::get (current_function_decl);
2020 if (node->ipa_transforms_to_apply.exists ())
2022 unsigned int i;
2024 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2025 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2026 node->ipa_transforms_to_apply.release ();
2030 /* Check if PASS is explicitly disabled or enabled and return
2031 the gate status. FUNC is the function to be processed, and
2032 GATE_STATUS is the gate status determined by pass manager by
2033 default. */
2035 static bool
2036 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2038 bool explicitly_enabled = false;
2039 bool explicitly_disabled = false;
2041 explicitly_enabled
2042 = is_pass_explicitly_enabled_or_disabled (pass, func,
2043 enabled_pass_uid_range_tab);
2044 explicitly_disabled
2045 = is_pass_explicitly_enabled_or_disabled (pass, func,
2046 disabled_pass_uid_range_tab);
2048 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2050 return gate_status;
2054 /* Execute PASS. */
2056 bool
2057 execute_one_pass (opt_pass *pass)
2059 unsigned int todo_after = 0;
2061 bool gate_status;
2063 /* IPA passes are executed on whole program, so cfun should be NULL.
2064 Other passes need function context set. */
2065 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2066 gcc_assert (!cfun && !current_function_decl);
2067 else
2068 gcc_assert (cfun && current_function_decl);
2070 current_pass = pass;
2072 /* Check whether gate check should be avoided.
2073 User controls the value of the gate through the parameter "gate_status". */
2074 gate_status = pass->gate (cfun);
2075 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2077 /* Override gate with plugin. */
2078 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2080 if (!gate_status)
2082 /* Run so passes selectively disabling themselves on a given function
2083 are not miscounted. */
2084 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2086 check_profile_consistency (pass->static_pass_number, 0, false);
2087 check_profile_consistency (pass->static_pass_number, 1, false);
2089 current_pass = NULL;
2090 return false;
2093 /* Pass execution event trigger: useful to identify passes being
2094 executed. */
2095 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2097 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2098 Apply all trnasforms first. */
2099 if (pass->type == SIMPLE_IPA_PASS)
2101 struct cgraph_node *node;
2102 bool applied = false;
2103 FOR_EACH_DEFINED_FUNCTION (node)
2104 if (node->analyzed
2105 && node->has_gimple_body_p ()
2106 && (!node->clone_of || node->decl != node->clone_of->decl))
2108 if (!node->global.inlined_to
2109 && node->ipa_transforms_to_apply.exists ())
2111 node->get_body ();
2112 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2113 execute_all_ipa_transforms ();
2114 rebuild_cgraph_edges ();
2115 free_dominance_info (CDI_DOMINATORS);
2116 free_dominance_info (CDI_POST_DOMINATORS);
2117 pop_cfun ();
2118 applied = true;
2121 if (applied)
2122 symtab_remove_unreachable_nodes (true, dump_file);
2123 /* Restore current_pass. */
2124 current_pass = pass;
2127 if (!quiet_flag && !cfun)
2128 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2130 /* Note that the folders should only create gimple expressions.
2131 This is a hack until the new folder is ready. */
2132 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2134 pass_init_dump_file (pass);
2136 /* Run pre-pass verification. */
2137 execute_todo (pass->todo_flags_start);
2139 #ifdef ENABLE_CHECKING
2140 do_per_function (verify_curr_properties,
2141 (void *)(size_t)pass->properties_required);
2142 #endif
2144 /* If a timevar is present, start it. */
2145 if (pass->tv_id != TV_NONE)
2146 timevar_push (pass->tv_id);
2148 /* Do it! */
2149 todo_after = pass->execute (cfun);
2150 do_per_function (clear_last_verified, NULL);
2152 /* Stop timevar. */
2153 if (pass->tv_id != TV_NONE)
2154 timevar_pop (pass->tv_id);
2156 do_per_function (update_properties_after_pass, pass);
2158 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2159 check_profile_consistency (pass->static_pass_number, 0, true);
2161 /* Run post-pass cleanup and verification. */
2162 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2163 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2164 check_profile_consistency (pass->static_pass_number, 1, true);
2166 verify_interpass_invariants ();
2167 if (dump_file)
2168 do_per_function (execute_function_dump, pass);
2169 if (pass->type == IPA_PASS)
2171 struct cgraph_node *node;
2172 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2173 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2176 if (!current_function_decl)
2177 cgraph_process_new_functions ();
2179 pass_fini_dump_file (pass);
2181 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2182 gcc_assert (!(cfun->curr_properties & PROP_trees)
2183 || pass->type != RTL_PASS);
2185 current_pass = NULL;
2187 /* Signal this is a suitable GC collection point. */
2188 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2189 ggc_collect ();
2191 return true;
2194 static void
2195 execute_pass_list_1 (opt_pass *pass)
2199 gcc_assert (pass->type == GIMPLE_PASS
2200 || pass->type == RTL_PASS);
2201 if (execute_one_pass (pass) && pass->sub)
2202 execute_pass_list_1 (pass->sub);
2203 pass = pass->next;
2205 while (pass);
2208 void
2209 execute_pass_list (function *fn, opt_pass *pass)
2211 push_cfun (fn);
2212 execute_pass_list_1 (pass);
2213 if (fn->cfg)
2215 free_dominance_info (CDI_DOMINATORS);
2216 free_dominance_info (CDI_POST_DOMINATORS);
2218 pop_cfun ();
2221 /* Write out all LTO data. */
2222 static void
2223 write_lto (void)
2225 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2226 lto_output ();
2227 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2228 timevar_push (TV_IPA_LTO_DECL_OUT);
2229 produce_asm_for_decls ();
2230 timevar_pop (TV_IPA_LTO_DECL_OUT);
2233 /* Same as execute_pass_list but assume that subpasses of IPA passes
2234 are local passes. If SET is not NULL, write out summaries of only
2235 those node in SET. */
2237 static void
2238 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2240 while (pass)
2242 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2243 gcc_assert (!current_function_decl);
2244 gcc_assert (!cfun);
2245 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2246 if (pass->type == IPA_PASS
2247 && ipa_pass->write_summary
2248 && pass->gate (cfun))
2250 /* If a timevar is present, start it. */
2251 if (pass->tv_id)
2252 timevar_push (pass->tv_id);
2254 pass_init_dump_file (pass);
2256 ipa_pass->write_summary ();
2258 pass_fini_dump_file (pass);
2260 /* If a timevar is present, start it. */
2261 if (pass->tv_id)
2262 timevar_pop (pass->tv_id);
2265 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2266 ipa_write_summaries_2 (pass->sub, state);
2268 pass = pass->next;
2272 /* Helper function of ipa_write_summaries. Creates and destroys the
2273 decl state and calls ipa_write_summaries_2 for all passes that have
2274 summaries. SET is the set of nodes to be written. */
2276 static void
2277 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2279 pass_manager *passes = g->get_passes ();
2280 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2281 state->symtab_node_encoder = encoder;
2283 lto_push_out_decl_state (state);
2285 gcc_assert (!flag_wpa);
2286 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2288 write_lto ();
2290 gcc_assert (lto_get_out_decl_state () == state);
2291 lto_pop_out_decl_state ();
2292 lto_delete_out_decl_state (state);
2295 /* Write out summaries for all the nodes in the callgraph. */
2297 void
2298 ipa_write_summaries (void)
2300 lto_symtab_encoder_t encoder;
2301 int i, order_pos;
2302 varpool_node *vnode;
2303 struct cgraph_node *node;
2304 struct cgraph_node **order;
2306 if (!flag_generate_lto || seen_error ())
2307 return;
2309 encoder = lto_symtab_encoder_new (false);
2311 /* Create the callgraph set in the same order used in
2312 cgraph_expand_all_functions. This mostly facilitates debugging,
2313 since it causes the gimple file to be processed in the same order
2314 as the source code. */
2315 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2316 order_pos = ipa_reverse_postorder (order);
2317 gcc_assert (order_pos == cgraph_n_nodes);
2319 for (i = order_pos - 1; i >= 0; i--)
2321 struct cgraph_node *node = order[i];
2323 if (node->has_gimple_body_p ())
2325 /* When streaming out references to statements as part of some IPA
2326 pass summary, the statements need to have uids assigned and the
2327 following does that for all the IPA passes here. Naturally, this
2328 ordering then matches the one IPA-passes get in their stmt_fixup
2329 hooks. */
2331 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2332 renumber_gimple_stmt_uids ();
2333 pop_cfun ();
2335 if (node->definition)
2336 lto_set_symtab_encoder_in_partition (encoder, node);
2339 FOR_EACH_DEFINED_FUNCTION (node)
2340 if (node->alias)
2341 lto_set_symtab_encoder_in_partition (encoder, node);
2342 FOR_EACH_DEFINED_VARIABLE (vnode)
2343 lto_set_symtab_encoder_in_partition (encoder, vnode);
2345 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2347 free (order);
2350 /* Same as execute_pass_list but assume that subpasses of IPA passes
2351 are local passes. If SET is not NULL, write out optimization summaries of
2352 only those node in SET. */
2354 static void
2355 ipa_write_optimization_summaries_1 (opt_pass *pass,
2356 struct lto_out_decl_state *state)
2358 while (pass)
2360 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2361 gcc_assert (!current_function_decl);
2362 gcc_assert (!cfun);
2363 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2364 if (pass->type == IPA_PASS
2365 && ipa_pass->write_optimization_summary
2366 && pass->gate (cfun))
2368 /* If a timevar is present, start it. */
2369 if (pass->tv_id)
2370 timevar_push (pass->tv_id);
2372 pass_init_dump_file (pass);
2374 ipa_pass->write_optimization_summary ();
2376 pass_fini_dump_file (pass);
2378 /* If a timevar is present, start it. */
2379 if (pass->tv_id)
2380 timevar_pop (pass->tv_id);
2383 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2384 ipa_write_optimization_summaries_1 (pass->sub, state);
2386 pass = pass->next;
2390 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2391 NULL, write out all summaries of all nodes. */
2393 void
2394 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2396 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2397 lto_symtab_encoder_iterator lsei;
2398 state->symtab_node_encoder = encoder;
2400 lto_push_out_decl_state (state);
2401 for (lsei = lsei_start_function_in_partition (encoder);
2402 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2404 struct cgraph_node *node = lsei_cgraph_node (lsei);
2405 /* When streaming out references to statements as part of some IPA
2406 pass summary, the statements need to have uids assigned.
2408 For functions newly born at WPA stage we need to initialize
2409 the uids here. */
2410 if (node->definition
2411 && gimple_has_body_p (node->decl))
2413 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2414 renumber_gimple_stmt_uids ();
2415 pop_cfun ();
2419 gcc_assert (flag_wpa);
2420 pass_manager *passes = g->get_passes ();
2421 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2423 write_lto ();
2425 gcc_assert (lto_get_out_decl_state () == state);
2426 lto_pop_out_decl_state ();
2427 lto_delete_out_decl_state (state);
2430 /* Same as execute_pass_list but assume that subpasses of IPA passes
2431 are local passes. */
2433 static void
2434 ipa_read_summaries_1 (opt_pass *pass)
2436 while (pass)
2438 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2440 gcc_assert (!current_function_decl);
2441 gcc_assert (!cfun);
2442 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2444 if (pass->gate (cfun))
2446 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2448 /* If a timevar is present, start it. */
2449 if (pass->tv_id)
2450 timevar_push (pass->tv_id);
2452 pass_init_dump_file (pass);
2454 ipa_pass->read_summary ();
2456 pass_fini_dump_file (pass);
2458 /* Stop timevar. */
2459 if (pass->tv_id)
2460 timevar_pop (pass->tv_id);
2463 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2464 ipa_read_summaries_1 (pass->sub);
2466 pass = pass->next;
2471 /* Read all the summaries for all_regular_ipa_passes. */
2473 void
2474 ipa_read_summaries (void)
2476 pass_manager *passes = g->get_passes ();
2477 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2480 /* Same as execute_pass_list but assume that subpasses of IPA passes
2481 are local passes. */
2483 static void
2484 ipa_read_optimization_summaries_1 (opt_pass *pass)
2486 while (pass)
2488 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2490 gcc_assert (!current_function_decl);
2491 gcc_assert (!cfun);
2492 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2494 if (pass->gate (cfun))
2496 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2498 /* If a timevar is present, start it. */
2499 if (pass->tv_id)
2500 timevar_push (pass->tv_id);
2502 pass_init_dump_file (pass);
2504 ipa_pass->read_optimization_summary ();
2506 pass_fini_dump_file (pass);
2508 /* Stop timevar. */
2509 if (pass->tv_id)
2510 timevar_pop (pass->tv_id);
2513 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2514 ipa_read_optimization_summaries_1 (pass->sub);
2516 pass = pass->next;
2520 /* Read all the summaries for all_regular_ipa_passes. */
2522 void
2523 ipa_read_optimization_summaries (void)
2525 pass_manager *passes = g->get_passes ();
2526 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2529 /* Same as execute_pass_list but assume that subpasses of IPA passes
2530 are local passes. */
2531 void
2532 execute_ipa_pass_list (opt_pass *pass)
2536 gcc_assert (!current_function_decl);
2537 gcc_assert (!cfun);
2538 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2539 if (execute_one_pass (pass) && pass->sub)
2541 if (pass->sub->type == GIMPLE_PASS)
2543 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2544 do_per_function_toporder ((void (*)(function *, void *))
2545 execute_pass_list,
2546 pass->sub);
2547 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2549 else if (pass->sub->type == SIMPLE_IPA_PASS
2550 || pass->sub->type == IPA_PASS)
2551 execute_ipa_pass_list (pass->sub);
2552 else
2553 gcc_unreachable ();
2555 gcc_assert (!current_function_decl);
2556 cgraph_process_new_functions ();
2557 pass = pass->next;
2559 while (pass);
2562 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2564 static void
2565 execute_ipa_stmt_fixups (opt_pass *pass,
2566 struct cgraph_node *node, gimple *stmts)
2568 while (pass)
2570 /* Execute all of the IPA_PASSes in the list. */
2571 if (pass->type == IPA_PASS
2572 && pass->gate (cfun))
2574 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2576 if (ipa_pass->stmt_fixup)
2578 pass_init_dump_file (pass);
2579 /* If a timevar is present, start it. */
2580 if (pass->tv_id)
2581 timevar_push (pass->tv_id);
2583 ipa_pass->stmt_fixup (node, stmts);
2585 /* Stop timevar. */
2586 if (pass->tv_id)
2587 timevar_pop (pass->tv_id);
2588 pass_fini_dump_file (pass);
2590 if (pass->sub)
2591 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2593 pass = pass->next;
2597 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2599 void
2600 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2602 pass_manager *passes = g->get_passes ();
2603 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2607 extern void debug_properties (unsigned int);
2608 extern void dump_properties (FILE *, unsigned int);
2610 DEBUG_FUNCTION void
2611 dump_properties (FILE *dump, unsigned int props)
2613 fprintf (dump, "Properties:\n");
2614 if (props & PROP_gimple_any)
2615 fprintf (dump, "PROP_gimple_any\n");
2616 if (props & PROP_gimple_lcf)
2617 fprintf (dump, "PROP_gimple_lcf\n");
2618 if (props & PROP_gimple_leh)
2619 fprintf (dump, "PROP_gimple_leh\n");
2620 if (props & PROP_cfg)
2621 fprintf (dump, "PROP_cfg\n");
2622 if (props & PROP_ssa)
2623 fprintf (dump, "PROP_ssa\n");
2624 if (props & PROP_no_crit_edges)
2625 fprintf (dump, "PROP_no_crit_edges\n");
2626 if (props & PROP_rtl)
2627 fprintf (dump, "PROP_rtl\n");
2628 if (props & PROP_gimple_lomp)
2629 fprintf (dump, "PROP_gimple_lomp\n");
2630 if (props & PROP_gimple_lcx)
2631 fprintf (dump, "PROP_gimple_lcx\n");
2632 if (props & PROP_gimple_lvec)
2633 fprintf (dump, "PROP_gimple_lvec\n");
2634 if (props & PROP_cfglayout)
2635 fprintf (dump, "PROP_cfglayout\n");
2638 DEBUG_FUNCTION void
2639 debug_properties (unsigned int props)
2641 dump_properties (stderr, props);
2644 /* Called by local passes to see if function is called by already processed nodes.
2645 Because we process nodes in topological order, this means that function is
2646 in recursive cycle or we introduced new direct calls. */
2647 bool
2648 function_called_by_processed_nodes_p (void)
2650 struct cgraph_edge *e;
2651 for (e = cgraph_node::get (current_function_decl)->callers;
2653 e = e->next_caller)
2655 if (e->caller->decl == current_function_decl)
2656 continue;
2657 if (!e->caller->has_gimple_body_p ())
2658 continue;
2659 if (TREE_ASM_WRITTEN (e->caller->decl))
2660 continue;
2661 if (!e->caller->process && !e->caller->global.inlined_to)
2662 break;
2664 if (dump_file && e)
2666 fprintf (dump_file, "Already processed call to:\n");
2667 e->caller->dump (dump_file);
2669 return e != NULL;
2672 #include "gt-passes.h"