2014-01-20 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / passes.c
blob60fb13536fa60fcb3acf3101940bf3afb8ae86e5
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"
88 using namespace gcc;
90 /* This is used for debugging. It allows the current pass to printed
91 from anywhere in compilation.
92 The variable current_pass is also used for statistics and plugins. */
93 opt_pass *current_pass;
95 static void register_pass_name (opt_pass *, const char *);
97 /* Most passes are single-instance (within their context) and thus don't
98 need to implement cloning, but passes that support multiple instances
99 *must* provide their own implementation of the clone method.
101 Handle this by providing a default implemenation, but make it a fatal
102 error to call it. */
104 opt_pass *
105 opt_pass::clone ()
107 internal_error ("pass %s does not support cloning", name);
110 bool
111 opt_pass::gate ()
113 return true;
116 unsigned int
117 opt_pass::execute ()
119 return 0;
122 opt_pass::opt_pass (const pass_data &data, context *ctxt)
123 : pass_data (data),
124 sub (NULL),
125 next (NULL),
126 static_pass_number (0),
127 m_ctxt (ctxt)
132 void
133 pass_manager::execute_early_local_passes ()
135 execute_pass_list (pass_early_local_passes_1->sub);
138 unsigned int
139 pass_manager::execute_pass_mode_switching ()
141 return pass_mode_switching_1->execute ();
145 /* Call from anywhere to find out what pass this is. Useful for
146 printing out debugging information deep inside an service
147 routine. */
148 void
149 print_current_pass (FILE *file)
151 if (current_pass)
152 fprintf (file, "current pass = %s (%d)\n",
153 current_pass->name, current_pass->static_pass_number);
154 else
155 fprintf (file, "no current pass.\n");
159 /* Call from the debugger to get the current pass name. */
160 DEBUG_FUNCTION void
161 debug_pass (void)
163 print_current_pass (stderr);
168 /* Global variables used to communicate with passes. */
169 bool in_gimple_form;
170 bool first_pass_instance;
173 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
174 and TYPE_DECL nodes.
176 This does nothing for local (non-static) variables, unless the
177 variable is a register variable with DECL_ASSEMBLER_NAME set. In
178 that case, or if the variable is not an automatic, it sets up the
179 RTL and outputs any assembler code (label definition, storage
180 allocation and initialization).
182 DECL is the declaration. TOP_LEVEL is nonzero
183 if this declaration is not within a function. */
185 void
186 rest_of_decl_compilation (tree decl,
187 int top_level,
188 int at_end)
190 bool finalize = true;
192 /* We deferred calling assemble_alias so that we could collect
193 other attributes such as visibility. Emit the alias now. */
194 if (!in_lto_p)
196 tree alias;
197 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
198 if (alias)
200 alias = TREE_VALUE (TREE_VALUE (alias));
201 alias = get_identifier (TREE_STRING_POINTER (alias));
202 /* A quirk of the initial implementation of aliases required that the
203 user add "extern" to all of them. Which is silly, but now
204 historical. Do note that the symbol is in fact locally defined. */
205 DECL_EXTERNAL (decl) = 0;
206 TREE_STATIC (decl) = 1;
207 assemble_alias (decl, alias);
208 finalize = false;
212 /* Can't defer this, because it needs to happen before any
213 later function definitions are processed. */
214 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
215 make_decl_rtl (decl);
217 /* Forward declarations for nested functions are not "external",
218 but we need to treat them as if they were. */
219 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
220 || TREE_CODE (decl) == FUNCTION_DECL)
222 timevar_push (TV_VARCONST);
224 /* Don't output anything when a tentative file-scope definition
225 is seen. But at end of compilation, do output code for them.
227 We do output all variables and rely on
228 callgraph code to defer them except for forward declarations
229 (see gcc.c-torture/compile/920624-1.c) */
230 if ((at_end
231 || !DECL_DEFER_OUTPUT (decl)
232 || DECL_INITIAL (decl))
233 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
234 && !DECL_EXTERNAL (decl))
236 /* When reading LTO unit, we also read varpool, so do not
237 rebuild it. */
238 if (in_lto_p && !at_end)
240 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
241 varpool_finalize_decl (decl);
244 #ifdef ASM_FINISH_DECLARE_OBJECT
245 if (decl == last_assemble_variable_decl)
247 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
248 top_level, at_end);
250 #endif
252 timevar_pop (TV_VARCONST);
254 else if (TREE_CODE (decl) == TYPE_DECL
255 /* Like in rest_of_type_compilation, avoid confusing the debug
256 information machinery when there are errors. */
257 && !seen_error ())
259 timevar_push (TV_SYMOUT);
260 debug_hooks->type_decl (decl, !top_level);
261 timevar_pop (TV_SYMOUT);
264 /* Let cgraph know about the existence of variables. */
265 if (in_lto_p && !at_end)
267 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
268 && TREE_STATIC (decl))
269 varpool_node_for_decl (decl);
272 /* Called after finishing a record, union or enumeral type. */
274 void
275 rest_of_type_compilation (tree type, int toplev)
277 /* Avoid confusing the debug information machinery when there are
278 errors. */
279 if (seen_error ())
280 return;
282 timevar_push (TV_SYMOUT);
283 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
284 timevar_pop (TV_SYMOUT);
289 void
290 pass_manager::
291 finish_optimization_passes (void)
293 int i;
294 struct dump_file_info *dfi;
295 char *name;
296 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
298 timevar_push (TV_DUMP);
299 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
301 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
302 end_branch_prob ();
303 dumps->dump_finish (pass_profile_1->static_pass_number);
306 if (optimize > 0)
308 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
309 print_combine_total_stats ();
310 dumps->dump_finish (pass_profile_1->static_pass_number);
313 /* Do whatever is necessary to finish printing the graphs. */
314 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
315 if (dumps->dump_initialized_p (i)
316 && (dfi->pflags & TDF_GRAPH) != 0
317 && (name = dumps->get_dump_file_name (i)) != NULL)
319 finish_graph_dump_file (name);
320 free (name);
323 timevar_pop (TV_DUMP);
326 static unsigned int
327 execute_all_early_local_passes (void)
329 /* Once this pass (and its sub-passes) are complete, all functions
330 will be in SSA form. Technically this state change is happening
331 a tad early, since the sub-passes have not yet run, but since
332 none of the sub-passes are IPA passes and do not create new
333 functions, this is ok. We're setting this value for the benefit
334 of IPA passes that follow. */
335 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
336 cgraph_state = CGRAPH_STATE_IPA_SSA;
337 return 0;
340 /* Gate: execute, or not, all of the non-trivial optimizations. */
342 static bool
343 gate_all_early_local_passes (void)
345 /* Don't bother doing anything if the program has errors. */
346 return (!seen_error () && !in_lto_p);
349 namespace {
351 const pass_data pass_data_early_local_passes =
353 SIMPLE_IPA_PASS, /* type */
354 "early_local_cleanups", /* name */
355 OPTGROUP_NONE, /* optinfo_flags */
356 true, /* has_gate */
357 true, /* has_execute */
358 TV_EARLY_LOCAL, /* tv_id */
359 0, /* properties_required */
360 0, /* properties_provided */
361 0, /* properties_destroyed */
362 0, /* todo_flags_start */
363 TODO_remove_functions, /* todo_flags_finish */
366 class pass_early_local_passes : public simple_ipa_opt_pass
368 public:
369 pass_early_local_passes (gcc::context *ctxt)
370 : simple_ipa_opt_pass (pass_data_early_local_passes, ctxt)
373 /* opt_pass methods: */
374 bool gate () { return gate_all_early_local_passes (); }
375 unsigned int execute () { 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 /* Gate: execute, or not, all of the non-trivial optimizations. */
389 static bool
390 gate_all_early_optimizations (void)
392 return (optimize >= 1
393 /* Don't bother doing anything if the program has errors. */
394 && !seen_error ());
397 namespace {
399 const pass_data pass_data_all_early_optimizations =
401 GIMPLE_PASS, /* type */
402 "early_optimizations", /* name */
403 OPTGROUP_NONE, /* optinfo_flags */
404 true, /* has_gate */
405 false, /* has_execute */
406 TV_NONE, /* tv_id */
407 0, /* properties_required */
408 0, /* properties_provided */
409 0, /* properties_destroyed */
410 0, /* todo_flags_start */
411 0, /* todo_flags_finish */
414 class pass_all_early_optimizations : public gimple_opt_pass
416 public:
417 pass_all_early_optimizations (gcc::context *ctxt)
418 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
421 /* opt_pass methods: */
422 bool gate () { return gate_all_early_optimizations (); }
424 }; // class pass_all_early_optimizations
426 } // anon namespace
428 static gimple_opt_pass *
429 make_pass_all_early_optimizations (gcc::context *ctxt)
431 return new pass_all_early_optimizations (ctxt);
434 /* Gate: execute, or not, all of the non-trivial optimizations. */
436 static bool
437 gate_all_optimizations (void)
439 return optimize >= 1 && !optimize_debug;
442 namespace {
444 const pass_data pass_data_all_optimizations =
446 GIMPLE_PASS, /* type */
447 "*all_optimizations", /* name */
448 OPTGROUP_NONE, /* optinfo_flags */
449 true, /* has_gate */
450 false, /* has_execute */
451 TV_OPTIMIZE, /* tv_id */
452 0, /* properties_required */
453 0, /* properties_provided */
454 0, /* properties_destroyed */
455 0, /* todo_flags_start */
456 0, /* todo_flags_finish */
459 class pass_all_optimizations : public gimple_opt_pass
461 public:
462 pass_all_optimizations (gcc::context *ctxt)
463 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
466 /* opt_pass methods: */
467 bool gate () { return gate_all_optimizations (); }
469 }; // class pass_all_optimizations
471 } // anon namespace
473 static gimple_opt_pass *
474 make_pass_all_optimizations (gcc::context *ctxt)
476 return new pass_all_optimizations (ctxt);
479 /* Gate: execute, or not, all of the non-trivial optimizations. */
481 static bool
482 gate_all_optimizations_g (void)
484 return optimize >= 1 && optimize_debug;
487 namespace {
489 const pass_data pass_data_all_optimizations_g =
491 GIMPLE_PASS, /* type */
492 "*all_optimizations_g", /* name */
493 OPTGROUP_NONE, /* optinfo_flags */
494 true, /* has_gate */
495 false, /* has_execute */
496 TV_OPTIMIZE, /* tv_id */
497 0, /* properties_required */
498 0, /* properties_provided */
499 0, /* properties_destroyed */
500 0, /* todo_flags_start */
501 0, /* todo_flags_finish */
504 class pass_all_optimizations_g : public gimple_opt_pass
506 public:
507 pass_all_optimizations_g (gcc::context *ctxt)
508 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
511 /* opt_pass methods: */
512 bool gate () { return gate_all_optimizations_g (); }
514 }; // class pass_all_optimizations_g
516 } // anon namespace
518 static gimple_opt_pass *
519 make_pass_all_optimizations_g (gcc::context *ctxt)
521 return new pass_all_optimizations_g (ctxt);
524 static bool
525 gate_rest_of_compilation (void)
527 /* Early return if there were errors. We can run afoul of our
528 consistency checks, and there's not really much point in fixing them. */
529 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
532 namespace {
534 const pass_data pass_data_rest_of_compilation =
536 RTL_PASS, /* type */
537 "*rest_of_compilation", /* name */
538 OPTGROUP_NONE, /* optinfo_flags */
539 true, /* has_gate */
540 false, /* has_execute */
541 TV_REST_OF_COMPILATION, /* tv_id */
542 PROP_rtl, /* properties_required */
543 0, /* properties_provided */
544 0, /* properties_destroyed */
545 0, /* todo_flags_start */
546 0, /* todo_flags_finish */
549 class pass_rest_of_compilation : public rtl_opt_pass
551 public:
552 pass_rest_of_compilation (gcc::context *ctxt)
553 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
556 /* opt_pass methods: */
557 bool gate () { return gate_rest_of_compilation (); }
559 }; // class pass_rest_of_compilation
561 } // anon namespace
563 static rtl_opt_pass *
564 make_pass_rest_of_compilation (gcc::context *ctxt)
566 return new pass_rest_of_compilation (ctxt);
569 static bool
570 gate_postreload (void)
572 return reload_completed;
575 namespace {
577 const pass_data pass_data_postreload =
579 RTL_PASS, /* type */
580 "*all-postreload", /* name */
581 OPTGROUP_NONE, /* optinfo_flags */
582 true, /* has_gate */
583 false, /* has_execute */
584 TV_POSTRELOAD, /* tv_id */
585 PROP_rtl, /* properties_required */
586 0, /* properties_provided */
587 0, /* properties_destroyed */
588 0, /* todo_flags_start */
589 TODO_verify_rtl_sharing, /* todo_flags_finish */
592 class pass_postreload : public rtl_opt_pass
594 public:
595 pass_postreload (gcc::context *ctxt)
596 : rtl_opt_pass (pass_data_postreload, ctxt)
599 /* opt_pass methods: */
600 bool gate () { return gate_postreload (); }
602 }; // class pass_postreload
604 } // anon namespace
606 static rtl_opt_pass *
607 make_pass_postreload (gcc::context *ctxt)
609 return new pass_postreload (ctxt);
614 /* Set the static pass number of pass PASS to ID and record that
615 in the mapping from static pass number to pass. */
617 void
618 pass_manager::
619 set_pass_for_id (int id, opt_pass *pass)
621 pass->static_pass_number = id;
622 if (passes_by_id_size <= id)
624 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
625 memset (passes_by_id + passes_by_id_size, 0,
626 (id + 1 - passes_by_id_size) * sizeof (void *));
627 passes_by_id_size = id + 1;
629 passes_by_id[id] = pass;
632 /* Return the pass with the static pass number ID. */
634 opt_pass *
635 pass_manager::get_pass_for_id (int id) const
637 if (id >= passes_by_id_size)
638 return NULL;
639 return passes_by_id[id];
642 /* Iterate over the pass tree allocating dump file numbers. We want
643 to do this depth first, and independent of whether the pass is
644 enabled or not. */
646 void
647 register_one_dump_file (opt_pass *pass)
649 g->get_passes ()->register_one_dump_file (pass);
652 void
653 pass_manager::register_one_dump_file (opt_pass *pass)
655 char *dot_name, *flag_name, *glob_name;
656 const char *name, *full_name, *prefix;
657 char num[10];
658 int flags, id;
659 int optgroup_flags = OPTGROUP_NONE;
660 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
662 /* See below in next_pass_1. */
663 num[0] = '\0';
664 if (pass->static_pass_number != -1)
665 sprintf (num, "%d", ((int) pass->static_pass_number < 0
666 ? 1 : pass->static_pass_number));
668 /* The name is both used to identify the pass for the purposes of plugins,
669 and to specify dump file name and option.
670 The latter two might want something short which is not quite unique; for
671 that reason, we may have a disambiguating prefix, followed by a space
672 to mark the start of the following dump file name / option string. */
673 name = strchr (pass->name, ' ');
674 name = name ? name + 1 : pass->name;
675 dot_name = concat (".", name, num, NULL);
676 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
678 prefix = "ipa-";
679 flags = TDF_IPA;
680 optgroup_flags |= OPTGROUP_IPA;
682 else if (pass->type == GIMPLE_PASS)
684 prefix = "tree-";
685 flags = TDF_TREE;
687 else
689 prefix = "rtl-";
690 flags = TDF_RTL;
693 flag_name = concat (prefix, name, num, NULL);
694 glob_name = concat (prefix, name, NULL);
695 optgroup_flags |= pass->optinfo_flags;
696 /* For any passes that do not have an optgroup set, and which are not
697 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
698 any dump messages are emitted properly under -fopt-info(-optall). */
699 if (optgroup_flags == OPTGROUP_NONE)
700 optgroup_flags = OPTGROUP_OTHER;
701 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
702 optgroup_flags);
703 set_pass_for_id (id, pass);
704 full_name = concat (prefix, pass->name, num, NULL);
705 register_pass_name (pass, full_name);
706 free (CONST_CAST (char *, full_name));
709 /* Recursive worker function for register_dump_files. */
712 pass_manager::
713 register_dump_files_1 (opt_pass *pass, int properties)
717 int new_properties = (properties | pass->properties_provided)
718 & ~pass->properties_destroyed;
720 if (pass->name && pass->name[0] != '*')
721 register_one_dump_file (pass);
723 if (pass->sub)
724 new_properties = register_dump_files_1 (pass->sub, new_properties);
726 /* If we have a gate, combine the properties that we could have with
727 and without the pass being examined. */
728 if (pass->has_gate)
729 properties &= new_properties;
730 else
731 properties = new_properties;
733 pass = pass->next;
735 while (pass);
737 return properties;
740 /* Register the dump files for the pass_manager starting at PASS.
741 PROPERTIES reflects the properties that are guaranteed to be available at
742 the beginning of the pipeline. */
744 void
745 pass_manager::
746 register_dump_files (opt_pass *pass,int properties)
748 pass->properties_required |= properties;
749 register_dump_files_1 (pass, properties);
752 struct pass_registry
754 const char* unique_name;
755 opt_pass *pass;
758 /* Helper for pass_registry hash table. */
760 struct pass_registry_hasher : typed_noop_remove <pass_registry>
762 typedef pass_registry value_type;
763 typedef pass_registry compare_type;
764 static inline hashval_t hash (const value_type *);
765 static inline bool equal (const value_type *, const compare_type *);
768 /* Pass registry hash function. */
770 inline hashval_t
771 pass_registry_hasher::hash (const value_type *s)
773 return htab_hash_string (s->unique_name);
776 /* Hash equal function */
778 inline bool
779 pass_registry_hasher::equal (const value_type *s1, const compare_type *s2)
781 return !strcmp (s1->unique_name, s2->unique_name);
784 static hash_table <pass_registry_hasher> name_to_pass_map;
786 /* Register PASS with NAME. */
788 static void
789 register_pass_name (opt_pass *pass, const char *name)
791 struct pass_registry **slot;
792 struct pass_registry pr;
794 if (!name_to_pass_map.is_created ())
795 name_to_pass_map.create (256);
797 pr.unique_name = name;
798 slot = name_to_pass_map.find_slot (&pr, INSERT);
799 if (!*slot)
801 struct pass_registry *new_pr;
803 new_pr = XCNEW (struct pass_registry);
804 new_pr->unique_name = xstrdup (name);
805 new_pr->pass = pass;
806 *slot = new_pr;
808 else
809 return; /* Ignore plugin passes. */
812 /* Map from pass id to canonicalized pass name. */
814 typedef const char *char_ptr;
815 static vec<char_ptr> pass_tab = vNULL;
817 /* Callback function for traversing NAME_TO_PASS_MAP. */
820 passes_pass_traverse (pass_registry **p, void *data ATTRIBUTE_UNUSED)
822 opt_pass *pass = (*p)->pass;
824 gcc_assert (pass->static_pass_number > 0);
825 gcc_assert (pass_tab.exists ());
827 pass_tab[pass->static_pass_number] = (*p)->unique_name;
829 return 1;
832 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
833 table for dumping purpose. */
835 static void
836 create_pass_tab (void)
838 if (!flag_dump_passes)
839 return;
841 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
842 name_to_pass_map.traverse <void *, passes_pass_traverse> (NULL);
845 static bool override_gate_status (opt_pass *, tree, bool);
847 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
848 is turned on or not. */
850 static void
851 dump_one_pass (opt_pass *pass, int pass_indent)
853 int indent = 3 * pass_indent;
854 const char *pn;
855 bool is_on, is_really_on;
857 is_on = pass->has_gate ? pass->gate () : true;
858 is_really_on = override_gate_status (pass, current_function_decl, is_on);
860 if (pass->static_pass_number <= 0)
861 pn = pass->name;
862 else
863 pn = pass_tab[pass->static_pass_number];
865 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
866 (15 - indent < 0 ? 0 : 15 - indent), " ",
867 is_on ? " ON" : " OFF",
868 ((!is_on) == (!is_really_on) ? ""
869 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
872 /* Dump pass list PASS with indentation INDENT. */
874 static void
875 dump_pass_list (opt_pass *pass, int indent)
879 dump_one_pass (pass, indent);
880 if (pass->sub)
881 dump_pass_list (pass->sub, indent + 1);
882 pass = pass->next;
884 while (pass);
887 /* Dump all optimization passes. */
889 void
890 dump_passes (void)
892 g->get_passes ()->dump_passes ();
895 void
896 pass_manager::dump_passes () const
898 struct cgraph_node *n, *node = NULL;
900 create_pass_tab ();
902 FOR_EACH_FUNCTION (n)
903 if (DECL_STRUCT_FUNCTION (n->decl))
905 node = n;
906 break;
909 if (!node)
910 return;
912 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
914 dump_pass_list (all_lowering_passes, 1);
915 dump_pass_list (all_small_ipa_passes, 1);
916 dump_pass_list (all_regular_ipa_passes, 1);
917 dump_pass_list (all_late_ipa_passes, 1);
918 dump_pass_list (all_passes, 1);
920 pop_cfun ();
924 /* Returns the pass with NAME. */
926 static opt_pass *
927 get_pass_by_name (const char *name)
929 struct pass_registry **slot, pr;
931 pr.unique_name = name;
932 slot = name_to_pass_map.find_slot (&pr, NO_INSERT);
934 if (!slot || !*slot)
935 return NULL;
937 return (*slot)->pass;
941 /* Range [start, last]. */
943 struct uid_range
945 unsigned int start;
946 unsigned int last;
947 const char *assem_name;
948 struct uid_range *next;
951 typedef struct uid_range *uid_range_p;
954 static vec<uid_range_p>
955 enabled_pass_uid_range_tab = vNULL;
956 static vec<uid_range_p>
957 disabled_pass_uid_range_tab = vNULL;
960 /* Parse option string for -fdisable- and -fenable-
961 The syntax of the options:
963 -fenable-<pass_name>
964 -fdisable-<pass_name>
966 -fenable-<pass_name>=s1:e1,s2:e2,...
967 -fdisable-<pass_name>=s1:e1,s2:e2,...
970 static void
971 enable_disable_pass (const char *arg, bool is_enable)
973 opt_pass *pass;
974 char *range_str, *phase_name;
975 char *argstr = xstrdup (arg);
976 vec<uid_range_p> *tab = 0;
978 range_str = strchr (argstr,'=');
979 if (range_str)
981 *range_str = '\0';
982 range_str++;
985 phase_name = argstr;
986 if (!*phase_name)
988 if (is_enable)
989 error ("unrecognized option -fenable");
990 else
991 error ("unrecognized option -fdisable");
992 free (argstr);
993 return;
995 pass = get_pass_by_name (phase_name);
996 if (!pass || pass->static_pass_number == -1)
998 if (is_enable)
999 error ("unknown pass %s specified in -fenable", phase_name);
1000 else
1001 error ("unknown pass %s specified in -fdisable", phase_name);
1002 free (argstr);
1003 return;
1006 if (is_enable)
1007 tab = &enabled_pass_uid_range_tab;
1008 else
1009 tab = &disabled_pass_uid_range_tab;
1011 if ((unsigned) pass->static_pass_number >= tab->length ())
1012 tab->safe_grow_cleared (pass->static_pass_number + 1);
1014 if (!range_str)
1016 uid_range_p slot;
1017 uid_range_p new_range = XCNEW (struct uid_range);
1019 new_range->start = 0;
1020 new_range->last = (unsigned)-1;
1022 slot = (*tab)[pass->static_pass_number];
1023 new_range->next = slot;
1024 (*tab)[pass->static_pass_number] = new_range;
1025 if (is_enable)
1026 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1027 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1028 else
1029 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1030 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1032 else
1034 char *next_range = NULL;
1035 char *one_range = range_str;
1036 char *end_val = NULL;
1040 uid_range_p slot;
1041 uid_range_p new_range;
1042 char *invalid = NULL;
1043 long start;
1044 char *func_name = NULL;
1046 next_range = strchr (one_range, ',');
1047 if (next_range)
1049 *next_range = '\0';
1050 next_range++;
1053 end_val = strchr (one_range, ':');
1054 if (end_val)
1056 *end_val = '\0';
1057 end_val++;
1059 start = strtol (one_range, &invalid, 10);
1060 if (*invalid || start < 0)
1062 if (end_val || (one_range[0] >= '0'
1063 && one_range[0] <= '9'))
1065 error ("Invalid range %s in option %s",
1066 one_range,
1067 is_enable ? "-fenable" : "-fdisable");
1068 free (argstr);
1069 return;
1071 func_name = one_range;
1073 if (!end_val)
1075 new_range = XCNEW (struct uid_range);
1076 if (!func_name)
1078 new_range->start = (unsigned) start;
1079 new_range->last = (unsigned) start;
1081 else
1083 new_range->start = (unsigned) -1;
1084 new_range->last = (unsigned) -1;
1085 new_range->assem_name = xstrdup (func_name);
1088 else
1090 long last = strtol (end_val, &invalid, 10);
1091 if (*invalid || last < start)
1093 error ("Invalid range %s in option %s",
1094 end_val,
1095 is_enable ? "-fenable" : "-fdisable");
1096 free (argstr);
1097 return;
1099 new_range = XCNEW (struct uid_range);
1100 new_range->start = (unsigned) start;
1101 new_range->last = (unsigned) last;
1104 slot = (*tab)[pass->static_pass_number];
1105 new_range->next = slot;
1106 (*tab)[pass->static_pass_number] = new_range;
1107 if (is_enable)
1109 if (new_range->assem_name)
1110 inform (UNKNOWN_LOCATION,
1111 "enable pass %s for function %s",
1112 phase_name, new_range->assem_name);
1113 else
1114 inform (UNKNOWN_LOCATION,
1115 "enable pass %s for functions in the range of [%u, %u]",
1116 phase_name, new_range->start, new_range->last);
1118 else
1120 if (new_range->assem_name)
1121 inform (UNKNOWN_LOCATION,
1122 "disable pass %s for function %s",
1123 phase_name, new_range->assem_name);
1124 else
1125 inform (UNKNOWN_LOCATION,
1126 "disable pass %s for functions in the range of [%u, %u]",
1127 phase_name, new_range->start, new_range->last);
1130 one_range = next_range;
1131 } while (next_range);
1134 free (argstr);
1137 /* Enable pass specified by ARG. */
1139 void
1140 enable_pass (const char *arg)
1142 enable_disable_pass (arg, true);
1145 /* Disable pass specified by ARG. */
1147 void
1148 disable_pass (const char *arg)
1150 enable_disable_pass (arg, false);
1153 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1155 static bool
1156 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1157 tree func,
1158 vec<uid_range_p> tab)
1160 uid_range_p slot, range;
1161 int cgraph_uid;
1162 const char *aname = NULL;
1164 if (!tab.exists ()
1165 || (unsigned) pass->static_pass_number >= tab.length ()
1166 || pass->static_pass_number == -1)
1167 return false;
1169 slot = tab[pass->static_pass_number];
1170 if (!slot)
1171 return false;
1173 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
1174 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1175 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1177 range = slot;
1178 while (range)
1180 if ((unsigned) cgraph_uid >= range->start
1181 && (unsigned) cgraph_uid <= range->last)
1182 return true;
1183 if (range->assem_name && aname
1184 && !strcmp (range->assem_name, aname))
1185 return true;
1186 range = range->next;
1189 return false;
1193 /* Update static_pass_number for passes (and the flag
1194 TODO_mark_first_instance).
1196 Passes are constructed with static_pass_number preinitialized to 0
1198 This field is used in two different ways: initially as instance numbers
1199 of their kind, and then as ids within the entire pass manager.
1201 Within pass_manager::pass_manager:
1203 * In add_pass_instance(), as called by next_pass_1 in
1204 NEXT_PASS in init_optimization_passes
1206 * When the initial instance of a pass within a pass manager is seen,
1207 it is flagged, and its static_pass_number is set to -1
1209 * On subsequent times that it is seen, the static pass number
1210 is decremented each time, so that if there are e.g. 4 dups,
1211 they have static_pass_number -4, 2, 3, 4 respectively (note
1212 how the initial one is negative and gives the count); these
1213 can be thought of as instance numbers of the specific pass
1215 * Within the register_dump_files () traversal, set_pass_for_id()
1216 is called on each pass, using these instance numbers to create
1217 dumpfile switches, and then overwriting them with a pass id,
1218 which are global to the whole pass manager (based on
1219 (TDI_end + current value of extra_dump_files_in_use) ) */
1221 static void
1222 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1223 opt_pass *initial_pass)
1225 /* Are we dealing with the first pass of its kind, or a clone? */
1226 if (new_pass != initial_pass)
1228 /* We're dealing with a clone. */
1229 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1231 /* Indicate to register_dump_files that this pass has duplicates,
1232 and so it should rename the dump file. The first instance will
1233 be -1, and be number of duplicates = -static_pass_number - 1.
1234 Subsequent instances will be > 0 and just the duplicate number. */
1235 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1237 initial_pass->static_pass_number -= 1;
1238 new_pass->static_pass_number = -initial_pass->static_pass_number;
1241 else
1243 /* We're dealing with the first pass of its kind. */
1244 new_pass->todo_flags_start |= TODO_mark_first_instance;
1245 new_pass->static_pass_number = -1;
1247 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1251 /* Add a pass to the pass list. Duplicate the pass if it's already
1252 in the list. */
1254 static opt_pass **
1255 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1257 /* Every pass should have a name so that plugins can refer to them. */
1258 gcc_assert (pass->name != NULL);
1260 add_pass_instance (pass, false, initial_pass);
1261 *list = pass;
1263 return &(*list)->next;
1266 /* List node for an inserted pass instance. We need to keep track of all
1267 the newly-added pass instances (with 'added_pass_nodes' defined below)
1268 so that we can register their dump files after pass-positioning is finished.
1269 Registering dumping files needs to be post-processed or the
1270 static_pass_number of the opt_pass object would be modified and mess up
1271 the dump file names of future pass instances to be added. */
1273 struct pass_list_node
1275 opt_pass *pass;
1276 struct pass_list_node *next;
1279 static struct pass_list_node *added_pass_nodes = NULL;
1280 static struct pass_list_node *prev_added_pass_node;
1282 /* Insert the pass at the proper position. Return true if the pass
1283 is successfully added.
1285 NEW_PASS_INFO - new pass to be inserted
1286 PASS_LIST - root of the pass list to insert the new pass to */
1288 static bool
1289 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1291 opt_pass *pass = *pass_list, *prev_pass = NULL;
1292 bool success = false;
1294 for ( ; pass; prev_pass = pass, pass = pass->next)
1296 /* Check if the current pass is of the same type as the new pass and
1297 matches the name and the instance number of the reference pass. */
1298 if (pass->type == new_pass_info->pass->type
1299 && pass->name
1300 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1301 && ((new_pass_info->ref_pass_instance_number == 0)
1302 || (new_pass_info->ref_pass_instance_number ==
1303 pass->static_pass_number)
1304 || (new_pass_info->ref_pass_instance_number == 1
1305 && pass->todo_flags_start & TODO_mark_first_instance)))
1307 opt_pass *new_pass;
1308 struct pass_list_node *new_pass_node;
1310 if (new_pass_info->ref_pass_instance_number == 0)
1312 new_pass = new_pass_info->pass->clone ();
1313 add_pass_instance (new_pass, true, new_pass_info->pass);
1315 else
1317 new_pass = new_pass_info->pass;
1318 add_pass_instance (new_pass, true, new_pass);
1321 /* Insert the new pass instance based on the positioning op. */
1322 switch (new_pass_info->pos_op)
1324 case PASS_POS_INSERT_AFTER:
1325 new_pass->next = pass->next;
1326 pass->next = new_pass;
1328 /* Skip newly inserted pass to avoid repeated
1329 insertions in the case where the new pass and the
1330 existing one have the same name. */
1331 pass = new_pass;
1332 break;
1333 case PASS_POS_INSERT_BEFORE:
1334 new_pass->next = pass;
1335 if (prev_pass)
1336 prev_pass->next = new_pass;
1337 else
1338 *pass_list = new_pass;
1339 break;
1340 case PASS_POS_REPLACE:
1341 new_pass->next = pass->next;
1342 if (prev_pass)
1343 prev_pass->next = new_pass;
1344 else
1345 *pass_list = new_pass;
1346 new_pass->sub = pass->sub;
1347 new_pass->tv_id = pass->tv_id;
1348 pass = new_pass;
1349 break;
1350 default:
1351 error ("invalid pass positioning operation");
1352 return false;
1355 /* Save the newly added pass (instance) in the added_pass_nodes
1356 list so that we can register its dump file later. Note that
1357 we cannot register the dump file now because doing so will modify
1358 the static_pass_number of the opt_pass object and therefore
1359 mess up the dump file name of future instances. */
1360 new_pass_node = XCNEW (struct pass_list_node);
1361 new_pass_node->pass = new_pass;
1362 if (!added_pass_nodes)
1363 added_pass_nodes = new_pass_node;
1364 else
1365 prev_added_pass_node->next = new_pass_node;
1366 prev_added_pass_node = new_pass_node;
1368 success = true;
1371 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1372 success = true;
1375 return success;
1378 /* Hooks a new pass into the pass lists.
1380 PASS_INFO - pass information that specifies the opt_pass object,
1381 reference pass, instance number, and how to position
1382 the pass */
1384 void
1385 register_pass (struct register_pass_info *pass_info)
1387 g->get_passes ()->register_pass (pass_info);
1390 void
1391 register_pass (opt_pass* pass, pass_positioning_ops pos,
1392 const char* ref_pass_name, int ref_pass_inst_number)
1394 register_pass_info i;
1395 i.pass = pass;
1396 i.reference_pass_name = ref_pass_name;
1397 i.ref_pass_instance_number = ref_pass_inst_number;
1398 i.pos_op = pos;
1400 g->get_passes ()->register_pass (&i);
1403 void
1404 pass_manager::register_pass (struct register_pass_info *pass_info)
1406 bool all_instances, success;
1407 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1409 /* The checks below could fail in buggy plugins. Existing GCC
1410 passes should never fail these checks, so we mention plugin in
1411 the messages. */
1412 if (!pass_info->pass)
1413 fatal_error ("plugin cannot register a missing pass");
1415 if (!pass_info->pass->name)
1416 fatal_error ("plugin cannot register an unnamed pass");
1418 if (!pass_info->reference_pass_name)
1419 fatal_error
1420 ("plugin cannot register pass %qs without reference pass name",
1421 pass_info->pass->name);
1423 /* Try to insert the new pass to the pass lists. We need to check
1424 all five lists as the reference pass could be in one (or all) of
1425 them. */
1426 all_instances = pass_info->ref_pass_instance_number == 0;
1427 success = position_pass (pass_info, &all_lowering_passes);
1428 if (!success || all_instances)
1429 success |= position_pass (pass_info, &all_small_ipa_passes);
1430 if (!success || all_instances)
1431 success |= position_pass (pass_info, &all_regular_ipa_passes);
1432 if (!success || all_instances)
1433 success |= position_pass (pass_info, &all_late_ipa_passes);
1434 if (!success || all_instances)
1435 success |= position_pass (pass_info, &all_passes);
1436 if (!success)
1437 fatal_error
1438 ("pass %qs not found but is referenced by new pass %qs",
1439 pass_info->reference_pass_name, pass_info->pass->name);
1441 /* OK, we have successfully inserted the new pass. We need to register
1442 the dump files for the newly added pass and its duplicates (if any).
1443 Because the registration of plugin/backend passes happens after the
1444 command-line options are parsed, the options that specify single
1445 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1446 passes. Therefore we currently can only enable dumping of
1447 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1448 are specified. While doing so, we also delete the pass_list_node
1449 objects created during pass positioning. */
1450 while (added_pass_nodes)
1452 struct pass_list_node *next_node = added_pass_nodes->next;
1453 enum tree_dump_index tdi;
1454 register_one_dump_file (added_pass_nodes->pass);
1455 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1456 || added_pass_nodes->pass->type == IPA_PASS)
1457 tdi = TDI_ipa_all;
1458 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1459 tdi = TDI_tree_all;
1460 else
1461 tdi = TDI_rtl_all;
1462 /* Check if dump-all flag is specified. */
1463 if (dumps->get_dump_file_info (tdi)->pstate)
1464 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1465 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1466 XDELETE (added_pass_nodes);
1467 added_pass_nodes = next_node;
1471 /* Construct the pass tree. The sequencing of passes is driven by
1472 the cgraph routines:
1474 finalize_compilation_unit ()
1475 for each node N in the cgraph
1476 cgraph_analyze_function (N)
1477 cgraph_lower_function (N) -> all_lowering_passes
1479 If we are optimizing, compile is then invoked:
1481 compile ()
1482 ipa_passes () -> all_small_ipa_passes
1483 -> Analysis of all_regular_ipa_passes
1484 * possible LTO streaming at copmilation time *
1485 -> Execution of all_regular_ipa_passes
1486 * possible LTO streaming at link time *
1487 -> all_late_ipa_passes
1488 expand_all_functions ()
1489 for each node N in the cgraph
1490 expand_function (N) -> Transformation of all_regular_ipa_passes
1491 -> all_passes
1494 void *
1495 pass_manager::operator new (size_t sz)
1497 /* Ensure that all fields of the pass manager are zero-initialized. */
1498 return xcalloc (1, sz);
1501 pass_manager::pass_manager (context *ctxt)
1502 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1503 all_regular_ipa_passes (NULL),
1504 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1505 m_ctxt (ctxt)
1507 opt_pass **p;
1509 /* Initialize the pass_lists array. */
1510 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1511 GCC_PASS_LISTS
1512 #undef DEF_PASS_LIST
1514 /* Build the tree of passes. */
1516 #define INSERT_PASSES_AFTER(PASS) \
1517 p = &(PASS);
1519 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1521 opt_pass **p = &(PASS ## _1)->sub;
1523 #define POP_INSERT_PASSES() \
1526 #define NEXT_PASS(PASS, NUM) \
1527 do { \
1528 gcc_assert (NULL == PASS ## _ ## NUM); \
1529 if ((NUM) == 1) \
1530 PASS ## _1 = make_##PASS (m_ctxt); \
1531 else \
1533 gcc_assert (PASS ## _1); \
1534 PASS ## _ ## NUM = PASS ## _1->clone (); \
1536 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1537 } while (0)
1539 #define TERMINATE_PASS_LIST() \
1540 *p = NULL;
1542 #include "pass-instances.def"
1544 #undef INSERT_PASSES_AFTER
1545 #undef PUSH_INSERT_PASSES_WITHIN
1546 #undef POP_INSERT_PASSES
1547 #undef NEXT_PASS
1548 #undef TERMINATE_PASS_LIST
1550 /* Register the passes with the tree dump code. */
1551 register_dump_files (all_lowering_passes, PROP_gimple_any);
1552 register_dump_files (all_small_ipa_passes,
1553 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1554 | PROP_cfg);
1555 register_dump_files (all_regular_ipa_passes,
1556 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1557 | PROP_cfg);
1558 register_dump_files (all_late_ipa_passes,
1559 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1560 | PROP_cfg);
1561 register_dump_files (all_passes,
1562 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1563 | PROP_cfg);
1566 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1567 function CALLBACK for every function in the call graph. Otherwise,
1568 call CALLBACK on the current function. */
1570 static void
1571 do_per_function (void (*callback) (void *data), void *data)
1573 if (current_function_decl)
1574 callback (data);
1575 else
1577 struct cgraph_node *node;
1578 FOR_EACH_DEFINED_FUNCTION (node)
1579 if (node->analyzed && gimple_has_body_p (node->decl)
1580 && (!node->clone_of || node->decl != node->clone_of->decl))
1582 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1583 callback (data);
1584 if (!flag_wpa)
1586 free_dominance_info (CDI_DOMINATORS);
1587 free_dominance_info (CDI_POST_DOMINATORS);
1589 pop_cfun ();
1590 ggc_collect ();
1595 /* Because inlining might remove no-longer reachable nodes, we need to
1596 keep the array visible to garbage collector to avoid reading collected
1597 out nodes. */
1598 static int nnodes;
1599 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1601 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1602 function CALLBACK for every function in the call graph. Otherwise,
1603 call CALLBACK on the current function.
1604 This function is global so that plugins can use it. */
1605 void
1606 do_per_function_toporder (void (*callback) (void *data), void *data)
1608 int i;
1610 if (current_function_decl)
1611 callback (data);
1612 else
1614 gcc_assert (!order);
1615 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1616 nnodes = ipa_reverse_postorder (order);
1617 for (i = nnodes - 1; i >= 0; i--)
1618 order[i]->process = 1;
1619 for (i = nnodes - 1; i >= 0; i--)
1621 struct cgraph_node *node = order[i];
1623 /* Allow possibly removed nodes to be garbage collected. */
1624 order[i] = NULL;
1625 node->process = 0;
1626 if (cgraph_function_with_gimple_body_p (node))
1628 cgraph_get_body (node);
1629 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1630 callback (data);
1631 free_dominance_info (CDI_DOMINATORS);
1632 free_dominance_info (CDI_POST_DOMINATORS);
1633 pop_cfun ();
1634 ggc_collect ();
1638 ggc_free (order);
1639 order = NULL;
1640 nnodes = 0;
1643 /* Helper function to perform function body dump. */
1645 static void
1646 execute_function_dump (void *data)
1648 opt_pass *pass = (opt_pass *)data;
1650 if (dump_file && current_function_decl)
1652 if (cfun->curr_properties & PROP_trees)
1653 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1654 else
1655 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1657 /* Flush the file. If verification fails, we won't be able to
1658 close the file before aborting. */
1659 fflush (dump_file);
1661 if ((cfun->curr_properties & PROP_cfg)
1662 && (dump_flags & TDF_GRAPH))
1664 if (!pass->graph_dump_initialized)
1666 clean_graph_dump_file (dump_file_name);
1667 pass->graph_dump_initialized = true;
1669 print_graph_cfg (dump_file_name, cfun);
1674 static struct profile_record *profile_record;
1676 /* Do profile consistency book-keeping for the pass with static number INDEX.
1677 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1678 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1679 if we are only book-keeping on passes that may have selectively disabled
1680 themselves on a given function. */
1681 static void
1682 check_profile_consistency (int index, int subpass, bool run)
1684 pass_manager *passes = g->get_passes ();
1685 if (index == -1)
1686 return;
1687 if (!profile_record)
1688 profile_record = XCNEWVEC (struct profile_record,
1689 passes->passes_by_id_size);
1690 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1691 gcc_assert (subpass < 2);
1692 profile_record[index].run |= run;
1693 account_profile_record (&profile_record[index], subpass);
1696 /* Output profile consistency. */
1698 void
1699 dump_profile_report (void)
1701 g->get_passes ()->dump_profile_report ();
1704 void
1705 pass_manager::dump_profile_report () const
1707 int i, j;
1708 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1709 gcov_type last_time = 0, last_size = 0;
1710 double rel_time_change, rel_size_change;
1711 int last_reported = 0;
1713 if (!profile_record)
1714 return;
1715 fprintf (stderr, "\nProfile consistency report:\n\n");
1716 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1717 fprintf (stderr, " |freq count |freq count |size time\n");
1719 for (i = 0; i < passes_by_id_size; i++)
1720 for (j = 0 ; j < 2; j++)
1721 if (profile_record[i].run)
1723 if (last_time)
1724 rel_time_change = (profile_record[i].time[j]
1725 - (double)last_time) * 100 / (double)last_time;
1726 else
1727 rel_time_change = 0;
1728 if (last_size)
1729 rel_size_change = (profile_record[i].size[j]
1730 - (double)last_size) * 100 / (double)last_size;
1731 else
1732 rel_size_change = 0;
1734 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1735 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1736 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1737 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1738 || rel_time_change || rel_size_change)
1740 last_reported = i;
1741 fprintf (stderr, "%-20s %s",
1742 passes_by_id [i]->name,
1743 j ? "(after TODO)" : " ");
1744 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1745 fprintf (stderr, "| %+5i",
1746 profile_record[i].num_mismatched_freq_in[j]
1747 - last_freq_in);
1748 else
1749 fprintf (stderr, "| ");
1750 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1751 fprintf (stderr, " %+5i",
1752 profile_record[i].num_mismatched_count_in[j]
1753 - last_count_in);
1754 else
1755 fprintf (stderr, " ");
1756 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1757 fprintf (stderr, "| %+5i",
1758 profile_record[i].num_mismatched_freq_out[j]
1759 - last_freq_out);
1760 else
1761 fprintf (stderr, "| ");
1762 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1763 fprintf (stderr, " %+5i",
1764 profile_record[i].num_mismatched_count_out[j]
1765 - last_count_out);
1766 else
1767 fprintf (stderr, " ");
1769 /* Size/time units change across gimple and RTL. */
1770 if (i == pass_expand_1->static_pass_number)
1771 fprintf (stderr, "|----------");
1772 else
1774 if (rel_size_change)
1775 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1776 else
1777 fprintf (stderr, "| ");
1778 if (rel_time_change)
1779 fprintf (stderr, " %+8.4f%%", rel_time_change);
1781 fprintf (stderr, "\n");
1782 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1783 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1784 last_count_in = profile_record[i].num_mismatched_count_in[j];
1785 last_count_out = profile_record[i].num_mismatched_count_out[j];
1787 else if (j && last_reported != i)
1789 last_reported = i;
1790 fprintf (stderr, "%-20s ------------| | |\n",
1791 passes_by_id [i]->name);
1793 last_time = profile_record[i].time[j];
1794 last_size = profile_record[i].size[j];
1798 /* Perform all TODO actions that ought to be done on each function. */
1800 static void
1801 execute_function_todo (void *data)
1803 unsigned int flags = (size_t)data;
1804 flags &= ~cfun->last_verified;
1805 if (!flags)
1806 return;
1808 /* Always cleanup the CFG before trying to update SSA. */
1809 if (flags & TODO_cleanup_cfg)
1811 cleanup_tree_cfg ();
1813 /* When cleanup_tree_cfg merges consecutive blocks, it may
1814 perform some simplistic propagation when removing single
1815 valued PHI nodes. This propagation may, in turn, cause the
1816 SSA form to become out-of-date (see PR 22037). So, even
1817 if the parent pass had not scheduled an SSA update, we may
1818 still need to do one. */
1819 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1820 flags |= TODO_update_ssa;
1823 if (flags & TODO_update_ssa_any)
1825 unsigned update_flags = flags & TODO_update_ssa_any;
1826 update_ssa (update_flags);
1827 cfun->last_verified &= ~TODO_verify_ssa;
1830 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1831 compute_may_aliases ();
1833 if (optimize && (flags & TODO_update_address_taken))
1834 execute_update_addresses_taken ();
1836 if (flags & TODO_remove_unused_locals)
1837 remove_unused_locals ();
1839 if (flags & TODO_rebuild_frequencies)
1840 rebuild_frequencies ();
1842 if (flags & TODO_rebuild_cgraph_edges)
1843 rebuild_cgraph_edges ();
1845 /* If we've seen errors do not bother running any verifiers. */
1846 if (seen_error ())
1847 return;
1849 #if defined ENABLE_CHECKING
1850 if (flags & TODO_verify_ssa
1851 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1853 verify_gimple_in_cfg (cfun);
1854 verify_ssa (true);
1856 else if (flags & TODO_verify_stmts)
1857 verify_gimple_in_cfg (cfun);
1858 if (flags & TODO_verify_flow)
1859 verify_flow_info ();
1860 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1861 verify_loop_closed_ssa (false);
1862 if (flags & TODO_verify_rtl_sharing)
1863 verify_rtl_sharing ();
1864 #endif
1866 cfun->last_verified = flags & TODO_verify_all;
1869 /* Perform all TODO actions. */
1870 static void
1871 execute_todo (unsigned int flags)
1873 #if defined ENABLE_CHECKING
1874 if (cfun
1875 && need_ssa_update_p (cfun))
1876 gcc_assert (flags & TODO_update_ssa_any);
1877 #endif
1879 timevar_push (TV_TODO);
1881 /* Inform the pass whether it is the first time it is run. */
1882 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1884 statistics_fini_pass ();
1886 if (flags)
1887 do_per_function (execute_function_todo, (void *)(size_t) flags);
1889 /* Always remove functions just as before inlining: IPA passes might be
1890 interested to see bodies of extern inline functions that are not inlined
1891 to analyze side effects. The full removal is done just at the end
1892 of IPA pass queue. */
1893 if (flags & TODO_remove_functions)
1895 gcc_assert (!cfun);
1896 symtab_remove_unreachable_nodes (true, dump_file);
1899 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1901 gcc_assert (!cfun);
1902 dump_symtab (dump_file);
1903 /* Flush the file. If verification fails, we won't be able to
1904 close the file before aborting. */
1905 fflush (dump_file);
1908 /* Now that the dumping has been done, we can get rid of the optional
1909 df problems. */
1910 if (flags & TODO_df_finish)
1911 df_finish_pass ((flags & TODO_df_verify) != 0);
1913 timevar_pop (TV_TODO);
1916 /* Verify invariants that should hold between passes. This is a place
1917 to put simple sanity checks. */
1919 static void
1920 verify_interpass_invariants (void)
1922 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1925 /* Clear the last verified flag. */
1927 static void
1928 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1930 cfun->last_verified = 0;
1933 /* Helper function. Verify that the properties has been turn into the
1934 properties expected by the pass. */
1936 #ifdef ENABLE_CHECKING
1937 static void
1938 verify_curr_properties (void *data)
1940 unsigned int props = (size_t)data;
1941 gcc_assert ((cfun->curr_properties & props) == props);
1943 #endif
1945 /* Initialize pass dump file. */
1946 /* This is non-static so that the plugins can use it. */
1948 bool
1949 pass_init_dump_file (opt_pass *pass)
1951 pass->graph_dump_initialized = false;
1952 /* If a dump file name is present, open it if enabled. */
1953 if (pass->static_pass_number != -1)
1955 timevar_push (TV_DUMP);
1956 gcc::dump_manager *dumps = g->get_dumps ();
1957 bool initializing_dump =
1958 !dumps->dump_initialized_p (pass->static_pass_number);
1959 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
1960 dumps->dump_start (pass->static_pass_number, &dump_flags);
1961 if (dump_file && current_function_decl)
1962 dump_function_header (dump_file, current_function_decl, dump_flags);
1963 if (initializing_dump
1964 && dump_file && (dump_flags & TDF_GRAPH)
1965 && cfun && (cfun->curr_properties & PROP_cfg))
1967 clean_graph_dump_file (dump_file_name);
1968 pass->graph_dump_initialized = true;
1970 timevar_pop (TV_DUMP);
1971 return initializing_dump;
1973 else
1974 return false;
1977 /* Flush PASS dump file. */
1978 /* This is non-static so that plugins can use it. */
1980 void
1981 pass_fini_dump_file (opt_pass *pass)
1983 timevar_push (TV_DUMP);
1985 /* Flush and close dump file. */
1986 if (dump_file_name)
1988 free (CONST_CAST (char *, dump_file_name));
1989 dump_file_name = NULL;
1992 g->get_dumps ()->dump_finish (pass->static_pass_number);
1993 timevar_pop (TV_DUMP);
1996 /* After executing the pass, apply expected changes to the function
1997 properties. */
1999 static void
2000 update_properties_after_pass (void *data)
2002 opt_pass *pass = (opt_pass *) data;
2003 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
2004 & ~pass->properties_destroyed;
2007 /* Execute summary generation for all of the passes in IPA_PASS. */
2009 void
2010 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2012 while (ipa_pass)
2014 opt_pass *pass = ipa_pass;
2016 /* Execute all of the IPA_PASSes in the list. */
2017 if (ipa_pass->type == IPA_PASS
2018 && ((!pass->has_gate) || pass->gate ())
2019 && ipa_pass->generate_summary)
2021 pass_init_dump_file (pass);
2023 /* If a timevar is present, start it. */
2024 if (pass->tv_id)
2025 timevar_push (pass->tv_id);
2027 ipa_pass->generate_summary ();
2029 /* Stop timevar. */
2030 if (pass->tv_id)
2031 timevar_pop (pass->tv_id);
2033 pass_fini_dump_file (pass);
2035 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2039 /* Execute IPA_PASS function transform on NODE. */
2041 static void
2042 execute_one_ipa_transform_pass (struct cgraph_node *node,
2043 ipa_opt_pass_d *ipa_pass)
2045 opt_pass *pass = ipa_pass;
2046 unsigned int todo_after = 0;
2048 current_pass = pass;
2049 if (!ipa_pass->function_transform)
2050 return;
2052 /* Note that the folders should only create gimple expressions.
2053 This is a hack until the new folder is ready. */
2054 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2056 pass_init_dump_file (pass);
2058 /* Run pre-pass verification. */
2059 execute_todo (ipa_pass->function_transform_todo_flags_start);
2061 /* If a timevar is present, start it. */
2062 if (pass->tv_id != TV_NONE)
2063 timevar_push (pass->tv_id);
2065 /* Do it! */
2066 todo_after = ipa_pass->function_transform (node);
2068 /* Stop timevar. */
2069 if (pass->tv_id != TV_NONE)
2070 timevar_pop (pass->tv_id);
2072 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2073 check_profile_consistency (pass->static_pass_number, 0, true);
2075 /* Run post-pass cleanup and verification. */
2076 execute_todo (todo_after);
2077 verify_interpass_invariants ();
2078 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2079 check_profile_consistency (pass->static_pass_number, 1, true);
2081 if (dump_file)
2082 do_per_function (execute_function_dump, NULL);
2083 pass_fini_dump_file (pass);
2085 current_pass = NULL;
2087 /* Signal this is a suitable GC collection point. */
2088 if (!(todo_after & TODO_do_not_ggc_collect))
2089 ggc_collect ();
2092 /* For the current function, execute all ipa transforms. */
2094 void
2095 execute_all_ipa_transforms (void)
2097 struct cgraph_node *node;
2098 if (!cfun)
2099 return;
2100 node = cgraph_get_node (current_function_decl);
2102 if (node->ipa_transforms_to_apply.exists ())
2104 unsigned int i;
2106 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2107 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2108 node->ipa_transforms_to_apply.release ();
2112 /* Callback for do_per_function to apply all IPA transforms. */
2114 static void
2115 apply_ipa_transforms (void *data)
2117 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2118 if (!node->global.inlined_to && node->ipa_transforms_to_apply.exists ())
2120 *(bool *)data = true;
2121 execute_all_ipa_transforms ();
2122 rebuild_cgraph_edges ();
2126 /* Check if PASS is explicitly disabled or enabled and return
2127 the gate status. FUNC is the function to be processed, and
2128 GATE_STATUS is the gate status determined by pass manager by
2129 default. */
2131 static bool
2132 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2134 bool explicitly_enabled = false;
2135 bool explicitly_disabled = false;
2137 explicitly_enabled
2138 = is_pass_explicitly_enabled_or_disabled (pass, func,
2139 enabled_pass_uid_range_tab);
2140 explicitly_disabled
2141 = is_pass_explicitly_enabled_or_disabled (pass, func,
2142 disabled_pass_uid_range_tab);
2144 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2146 return gate_status;
2150 /* Execute PASS. */
2152 bool
2153 execute_one_pass (opt_pass *pass)
2155 unsigned int todo_after = 0;
2157 bool gate_status;
2159 /* IPA passes are executed on whole program, so cfun should be NULL.
2160 Other passes need function context set. */
2161 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2162 gcc_assert (!cfun && !current_function_decl);
2163 else
2164 gcc_assert (cfun && current_function_decl);
2166 current_pass = pass;
2168 /* Check whether gate check should be avoided.
2169 User controls the value of the gate through the parameter "gate_status". */
2170 gate_status = pass->has_gate ? pass->gate () : true;
2171 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2173 /* Override gate with plugin. */
2174 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2176 if (!gate_status)
2178 /* Run so passes selectively disabling themselves on a given function
2179 are not miscounted. */
2180 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2182 check_profile_consistency (pass->static_pass_number, 0, false);
2183 check_profile_consistency (pass->static_pass_number, 1, false);
2185 current_pass = NULL;
2186 return false;
2189 /* Pass execution event trigger: useful to identify passes being
2190 executed. */
2191 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2193 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2194 Apply all trnasforms first. */
2195 if (pass->type == SIMPLE_IPA_PASS)
2197 bool applied = false;
2198 do_per_function (apply_ipa_transforms, (void *)&applied);
2199 if (applied)
2200 symtab_remove_unreachable_nodes (true, dump_file);
2201 /* Restore current_pass. */
2202 current_pass = pass;
2205 if (!quiet_flag && !cfun)
2206 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2208 /* Note that the folders should only create gimple expressions.
2209 This is a hack until the new folder is ready. */
2210 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2212 pass_init_dump_file (pass);
2214 /* Run pre-pass verification. */
2215 execute_todo (pass->todo_flags_start);
2217 #ifdef ENABLE_CHECKING
2218 do_per_function (verify_curr_properties,
2219 (void *)(size_t)pass->properties_required);
2220 #endif
2222 /* If a timevar is present, start it. */
2223 if (pass->tv_id != TV_NONE)
2224 timevar_push (pass->tv_id);
2226 /* Do it! */
2227 if (pass->has_execute)
2229 todo_after = pass->execute ();
2230 do_per_function (clear_last_verified, NULL);
2233 /* Stop timevar. */
2234 if (pass->tv_id != TV_NONE)
2235 timevar_pop (pass->tv_id);
2237 do_per_function (update_properties_after_pass, pass);
2239 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2240 check_profile_consistency (pass->static_pass_number, 0, true);
2242 /* Run post-pass cleanup and verification. */
2243 execute_todo (todo_after | pass->todo_flags_finish);
2244 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2245 check_profile_consistency (pass->static_pass_number, 1, true);
2247 verify_interpass_invariants ();
2248 if (dump_file)
2249 do_per_function (execute_function_dump, pass);
2250 if (pass->type == IPA_PASS)
2252 struct cgraph_node *node;
2253 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2254 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2257 if (!current_function_decl)
2258 cgraph_process_new_functions ();
2260 pass_fini_dump_file (pass);
2262 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2263 gcc_assert (!(cfun->curr_properties & PROP_trees)
2264 || pass->type != RTL_PASS);
2266 current_pass = NULL;
2268 /* Signal this is a suitable GC collection point. */
2269 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2270 ggc_collect ();
2272 return true;
2275 void
2276 execute_pass_list (opt_pass *pass)
2280 gcc_assert (pass->type == GIMPLE_PASS
2281 || pass->type == RTL_PASS);
2282 if (execute_one_pass (pass) && pass->sub)
2283 execute_pass_list (pass->sub);
2284 pass = pass->next;
2286 while (pass);
2289 /* Write out all LTO data. */
2290 static void
2291 write_lto (void)
2293 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2294 lto_output ();
2295 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2296 timevar_push (TV_IPA_LTO_DECL_OUT);
2297 produce_asm_for_decls ();
2298 timevar_pop (TV_IPA_LTO_DECL_OUT);
2301 /* Same as execute_pass_list but assume that subpasses of IPA passes
2302 are local passes. If SET is not NULL, write out summaries of only
2303 those node in SET. */
2305 static void
2306 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2308 while (pass)
2310 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2311 gcc_assert (!current_function_decl);
2312 gcc_assert (!cfun);
2313 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2314 if (pass->type == IPA_PASS
2315 && ipa_pass->write_summary
2316 && ((!pass->has_gate) || pass->gate ()))
2318 /* If a timevar is present, start it. */
2319 if (pass->tv_id)
2320 timevar_push (pass->tv_id);
2322 pass_init_dump_file (pass);
2324 ipa_pass->write_summary ();
2326 pass_fini_dump_file (pass);
2328 /* If a timevar is present, start it. */
2329 if (pass->tv_id)
2330 timevar_pop (pass->tv_id);
2333 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2334 ipa_write_summaries_2 (pass->sub, state);
2336 pass = pass->next;
2340 /* Helper function of ipa_write_summaries. Creates and destroys the
2341 decl state and calls ipa_write_summaries_2 for all passes that have
2342 summaries. SET is the set of nodes to be written. */
2344 static void
2345 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2347 pass_manager *passes = g->get_passes ();
2348 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2349 state->symtab_node_encoder = encoder;
2351 lto_push_out_decl_state (state);
2353 gcc_assert (!flag_wpa);
2354 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2356 write_lto ();
2358 gcc_assert (lto_get_out_decl_state () == state);
2359 lto_pop_out_decl_state ();
2360 lto_delete_out_decl_state (state);
2363 /* Write out summaries for all the nodes in the callgraph. */
2365 void
2366 ipa_write_summaries (void)
2368 lto_symtab_encoder_t encoder;
2369 int i, order_pos;
2370 varpool_node *vnode;
2371 struct cgraph_node *node;
2372 struct cgraph_node **order;
2374 if (!flag_generate_lto || seen_error ())
2375 return;
2377 encoder = lto_symtab_encoder_new (false);
2379 /* Create the callgraph set in the same order used in
2380 cgraph_expand_all_functions. This mostly facilitates debugging,
2381 since it causes the gimple file to be processed in the same order
2382 as the source code. */
2383 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2384 order_pos = ipa_reverse_postorder (order);
2385 gcc_assert (order_pos == cgraph_n_nodes);
2387 for (i = order_pos - 1; i >= 0; i--)
2389 struct cgraph_node *node = order[i];
2391 if (cgraph_function_with_gimple_body_p (node))
2393 /* When streaming out references to statements as part of some IPA
2394 pass summary, the statements need to have uids assigned and the
2395 following does that for all the IPA passes here. Naturally, this
2396 ordering then matches the one IPA-passes get in their stmt_fixup
2397 hooks. */
2399 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2400 renumber_gimple_stmt_uids ();
2401 pop_cfun ();
2403 if (node->definition)
2404 lto_set_symtab_encoder_in_partition (encoder, node);
2407 FOR_EACH_DEFINED_FUNCTION (node)
2408 if (node->alias)
2409 lto_set_symtab_encoder_in_partition (encoder, node);
2410 FOR_EACH_DEFINED_VARIABLE (vnode)
2411 lto_set_symtab_encoder_in_partition (encoder, vnode);
2413 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2415 free (order);
2418 /* Same as execute_pass_list but assume that subpasses of IPA passes
2419 are local passes. If SET is not NULL, write out optimization summaries of
2420 only those node in SET. */
2422 static void
2423 ipa_write_optimization_summaries_1 (opt_pass *pass,
2424 struct lto_out_decl_state *state)
2426 while (pass)
2428 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2429 gcc_assert (!current_function_decl);
2430 gcc_assert (!cfun);
2431 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2432 if (pass->type == IPA_PASS
2433 && ipa_pass->write_optimization_summary
2434 && ((!pass->has_gate) || pass->gate ()))
2436 /* If a timevar is present, start it. */
2437 if (pass->tv_id)
2438 timevar_push (pass->tv_id);
2440 pass_init_dump_file (pass);
2442 ipa_pass->write_optimization_summary ();
2444 pass_fini_dump_file (pass);
2446 /* If a timevar is present, start it. */
2447 if (pass->tv_id)
2448 timevar_pop (pass->tv_id);
2451 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2452 ipa_write_optimization_summaries_1 (pass->sub, state);
2454 pass = pass->next;
2458 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2459 NULL, write out all summaries of all nodes. */
2461 void
2462 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2464 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2465 lto_symtab_encoder_iterator lsei;
2466 state->symtab_node_encoder = encoder;
2468 lto_push_out_decl_state (state);
2469 for (lsei = lsei_start_function_in_partition (encoder);
2470 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2472 struct cgraph_node *node = lsei_cgraph_node (lsei);
2473 /* When streaming out references to statements as part of some IPA
2474 pass summary, the statements need to have uids assigned.
2476 For functions newly born at WPA stage we need to initialize
2477 the uids here. */
2478 if (node->definition
2479 && gimple_has_body_p (node->decl))
2481 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2482 renumber_gimple_stmt_uids ();
2483 pop_cfun ();
2487 gcc_assert (flag_wpa);
2488 pass_manager *passes = g->get_passes ();
2489 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2491 write_lto ();
2493 gcc_assert (lto_get_out_decl_state () == state);
2494 lto_pop_out_decl_state ();
2495 lto_delete_out_decl_state (state);
2498 /* Same as execute_pass_list but assume that subpasses of IPA passes
2499 are local passes. */
2501 static void
2502 ipa_read_summaries_1 (opt_pass *pass)
2504 while (pass)
2506 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2508 gcc_assert (!current_function_decl);
2509 gcc_assert (!cfun);
2510 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2512 if ((!pass->has_gate) || pass->gate ())
2514 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2516 /* If a timevar is present, start it. */
2517 if (pass->tv_id)
2518 timevar_push (pass->tv_id);
2520 pass_init_dump_file (pass);
2522 ipa_pass->read_summary ();
2524 pass_fini_dump_file (pass);
2526 /* Stop timevar. */
2527 if (pass->tv_id)
2528 timevar_pop (pass->tv_id);
2531 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2532 ipa_read_summaries_1 (pass->sub);
2534 pass = pass->next;
2539 /* Read all the summaries for all_regular_ipa_passes. */
2541 void
2542 ipa_read_summaries (void)
2544 pass_manager *passes = g->get_passes ();
2545 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2548 /* Same as execute_pass_list but assume that subpasses of IPA passes
2549 are local passes. */
2551 static void
2552 ipa_read_optimization_summaries_1 (opt_pass *pass)
2554 while (pass)
2556 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2558 gcc_assert (!current_function_decl);
2559 gcc_assert (!cfun);
2560 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2562 if ((!pass->has_gate) || pass->gate ())
2564 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2566 /* If a timevar is present, start it. */
2567 if (pass->tv_id)
2568 timevar_push (pass->tv_id);
2570 pass_init_dump_file (pass);
2572 ipa_pass->read_optimization_summary ();
2574 pass_fini_dump_file (pass);
2576 /* Stop timevar. */
2577 if (pass->tv_id)
2578 timevar_pop (pass->tv_id);
2581 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2582 ipa_read_optimization_summaries_1 (pass->sub);
2584 pass = pass->next;
2588 /* Read all the summaries for all_regular_ipa_passes. */
2590 void
2591 ipa_read_optimization_summaries (void)
2593 pass_manager *passes = g->get_passes ();
2594 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2597 /* Same as execute_pass_list but assume that subpasses of IPA passes
2598 are local passes. */
2599 void
2600 execute_ipa_pass_list (opt_pass *pass)
2604 gcc_assert (!current_function_decl);
2605 gcc_assert (!cfun);
2606 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2607 if (execute_one_pass (pass) && pass->sub)
2609 if (pass->sub->type == GIMPLE_PASS)
2611 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2612 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2613 pass->sub);
2614 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2616 else if (pass->sub->type == SIMPLE_IPA_PASS
2617 || pass->sub->type == IPA_PASS)
2618 execute_ipa_pass_list (pass->sub);
2619 else
2620 gcc_unreachable ();
2622 gcc_assert (!current_function_decl);
2623 cgraph_process_new_functions ();
2624 pass = pass->next;
2626 while (pass);
2629 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2631 static void
2632 execute_ipa_stmt_fixups (opt_pass *pass,
2633 struct cgraph_node *node, gimple *stmts)
2635 while (pass)
2637 /* Execute all of the IPA_PASSes in the list. */
2638 if (pass->type == IPA_PASS
2639 && ((!pass->has_gate) || pass->gate ()))
2641 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2643 if (ipa_pass->stmt_fixup)
2645 pass_init_dump_file (pass);
2646 /* If a timevar is present, start it. */
2647 if (pass->tv_id)
2648 timevar_push (pass->tv_id);
2650 ipa_pass->stmt_fixup (node, stmts);
2652 /* Stop timevar. */
2653 if (pass->tv_id)
2654 timevar_pop (pass->tv_id);
2655 pass_fini_dump_file (pass);
2657 if (pass->sub)
2658 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2660 pass = pass->next;
2664 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2666 void
2667 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2669 pass_manager *passes = g->get_passes ();
2670 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2674 extern void debug_properties (unsigned int);
2675 extern void dump_properties (FILE *, unsigned int);
2677 DEBUG_FUNCTION void
2678 dump_properties (FILE *dump, unsigned int props)
2680 fprintf (dump, "Properties:\n");
2681 if (props & PROP_gimple_any)
2682 fprintf (dump, "PROP_gimple_any\n");
2683 if (props & PROP_gimple_lcf)
2684 fprintf (dump, "PROP_gimple_lcf\n");
2685 if (props & PROP_gimple_leh)
2686 fprintf (dump, "PROP_gimple_leh\n");
2687 if (props & PROP_cfg)
2688 fprintf (dump, "PROP_cfg\n");
2689 if (props & PROP_ssa)
2690 fprintf (dump, "PROP_ssa\n");
2691 if (props & PROP_no_crit_edges)
2692 fprintf (dump, "PROP_no_crit_edges\n");
2693 if (props & PROP_rtl)
2694 fprintf (dump, "PROP_rtl\n");
2695 if (props & PROP_gimple_lomp)
2696 fprintf (dump, "PROP_gimple_lomp\n");
2697 if (props & PROP_gimple_lcx)
2698 fprintf (dump, "PROP_gimple_lcx\n");
2699 if (props & PROP_gimple_lvec)
2700 fprintf (dump, "PROP_gimple_lvec\n");
2701 if (props & PROP_cfglayout)
2702 fprintf (dump, "PROP_cfglayout\n");
2705 DEBUG_FUNCTION void
2706 debug_properties (unsigned int props)
2708 dump_properties (stderr, props);
2711 /* Called by local passes to see if function is called by already processed nodes.
2712 Because we process nodes in topological order, this means that function is
2713 in recursive cycle or we introduced new direct calls. */
2714 bool
2715 function_called_by_processed_nodes_p (void)
2717 struct cgraph_edge *e;
2718 for (e = cgraph_get_node (current_function_decl)->callers;
2720 e = e->next_caller)
2722 if (e->caller->decl == current_function_decl)
2723 continue;
2724 if (!cgraph_function_with_gimple_body_p (e->caller))
2725 continue;
2726 if (TREE_ASM_WRITTEN (e->caller->decl))
2727 continue;
2728 if (!e->caller->process && !e->caller->global.inlined_to)
2729 break;
2731 if (dump_file && e)
2733 fprintf (dump_file, "Already processed call to:\n");
2734 dump_cgraph_node (dump_file, e->caller);
2736 return e != NULL;
2739 #include "gt-passes.h"