PR middle-end/59175
[official-gcc.git] / gcc / passes.c
blob55ec70f8962b492b8b2e05c76637b54ee9448524
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2013 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 "hash-table.h"
31 #include "input.h"
32 #include "tree.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 "ggc.h"
49 #include "graph.h"
50 #include "regs.h"
51 #include "diagnostic-core.h"
52 #include "params.h"
53 #include "reload.h"
54 #include "debug.h"
55 #include "target.h"
56 #include "langhooks.h"
57 #include "cfgloop.h"
58 #include "hosthooks.h"
59 #include "opts.h"
60 #include "coverage.h"
61 #include "value-prof.h"
62 #include "tree-inline.h"
63 #include "gimple.h"
64 #include "gimple-ssa.h"
65 #include "tree-cfg.h"
66 #include "tree-ssanames.h"
67 #include "tree-ssa-loop-manip.h"
68 #include "tree-into-ssa.h"
69 #include "tree-dfa.h"
70 #include "tree-ssa.h"
71 #include "tree-pass.h"
72 #include "tree-dump.h"
73 #include "df.h"
74 #include "predict.h"
75 #include "lto-streamer.h"
76 #include "plugin.h"
77 #include "ipa-utils.h"
78 #include "tree-pretty-print.h" /* for dump_function_header */
79 #include "context.h"
80 #include "pass_manager.h"
81 #include "tree-ssa-live.h" /* For remove_unused_locals. */
82 #include "tree-cfgcleanup.h"
84 using namespace gcc;
86 /* This is used for debugging. It allows the current pass to printed
87 from anywhere in compilation.
88 The variable current_pass is also used for statistics and plugins. */
89 struct opt_pass *current_pass;
91 static void register_pass_name (struct opt_pass *, const char *);
93 /* Most passes are single-instance (within their context) and thus don't
94 need to implement cloning, but passes that support multiple instances
95 *must* provide their own implementation of the clone method.
97 Handle this by providing a default implemenation, but make it a fatal
98 error to call it. */
100 opt_pass *
101 opt_pass::clone ()
103 internal_error ("pass %s does not support cloning", name);
106 bool
107 opt_pass::gate ()
109 return true;
112 unsigned int
113 opt_pass::execute ()
115 return 0;
118 opt_pass::opt_pass (const pass_data &data, context *ctxt)
119 : pass_data (data),
120 sub (NULL),
121 next (NULL),
122 static_pass_number (0),
123 m_ctxt (ctxt)
128 void
129 pass_manager::execute_early_local_passes ()
131 execute_pass_list (pass_early_local_passes_1->sub);
134 unsigned int
135 pass_manager::execute_pass_mode_switching ()
137 return pass_mode_switching_1->execute ();
141 /* Call from anywhere to find out what pass this is. Useful for
142 printing out debugging information deep inside an service
143 routine. */
144 void
145 print_current_pass (FILE *file)
147 if (current_pass)
148 fprintf (file, "current pass = %s (%d)\n",
149 current_pass->name, current_pass->static_pass_number);
150 else
151 fprintf (file, "no current pass.\n");
155 /* Call from the debugger to get the current pass name. */
156 DEBUG_FUNCTION void
157 debug_pass (void)
159 print_current_pass (stderr);
164 /* Global variables used to communicate with passes. */
165 bool in_gimple_form;
166 bool first_pass_instance;
169 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
170 and TYPE_DECL nodes.
172 This does nothing for local (non-static) variables, unless the
173 variable is a register variable with DECL_ASSEMBLER_NAME set. In
174 that case, or if the variable is not an automatic, it sets up the
175 RTL and outputs any assembler code (label definition, storage
176 allocation and initialization).
178 DECL is the declaration. TOP_LEVEL is nonzero
179 if this declaration is not within a function. */
181 void
182 rest_of_decl_compilation (tree decl,
183 int top_level,
184 int at_end)
186 /* We deferred calling assemble_alias so that we could collect
187 other attributes such as visibility. Emit the alias now. */
188 if (!in_lto_p)
190 tree alias;
191 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
192 if (alias)
194 alias = TREE_VALUE (TREE_VALUE (alias));
195 alias = get_identifier (TREE_STRING_POINTER (alias));
196 /* A quirk of the initial implementation of aliases required that the
197 user add "extern" to all of them. Which is silly, but now
198 historical. Do note that the symbol is in fact locally defined. */
199 DECL_EXTERNAL (decl) = 0;
200 TREE_STATIC (decl) = 1;
201 assemble_alias (decl, alias);
205 /* Can't defer this, because it needs to happen before any
206 later function definitions are processed. */
207 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
208 make_decl_rtl (decl);
210 /* Forward declarations for nested functions are not "external",
211 but we need to treat them as if they were. */
212 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
213 || TREE_CODE (decl) == FUNCTION_DECL)
215 timevar_push (TV_VARCONST);
217 /* Don't output anything when a tentative file-scope definition
218 is seen. But at end of compilation, do output code for them.
220 We do output all variables and rely on
221 callgraph code to defer them except for forward declarations
222 (see gcc.c-torture/compile/920624-1.c) */
223 if ((at_end
224 || !DECL_DEFER_OUTPUT (decl)
225 || DECL_INITIAL (decl))
226 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
227 && !DECL_EXTERNAL (decl))
229 /* When reading LTO unit, we also read varpool, so do not
230 rebuild it. */
231 if (in_lto_p && !at_end)
233 else if (TREE_CODE (decl) != FUNCTION_DECL)
234 varpool_finalize_decl (decl);
237 #ifdef ASM_FINISH_DECLARE_OBJECT
238 if (decl == last_assemble_variable_decl)
240 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
241 top_level, at_end);
243 #endif
245 timevar_pop (TV_VARCONST);
247 else if (TREE_CODE (decl) == TYPE_DECL
248 /* Like in rest_of_type_compilation, avoid confusing the debug
249 information machinery when there are errors. */
250 && !seen_error ())
252 timevar_push (TV_SYMOUT);
253 debug_hooks->type_decl (decl, !top_level);
254 timevar_pop (TV_SYMOUT);
257 /* Let cgraph know about the existence of variables. */
258 if (in_lto_p && !at_end)
260 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
261 && TREE_STATIC (decl))
262 varpool_node_for_decl (decl);
265 /* Called after finishing a record, union or enumeral type. */
267 void
268 rest_of_type_compilation (tree type, int toplev)
270 /* Avoid confusing the debug information machinery when there are
271 errors. */
272 if (seen_error ())
273 return;
275 timevar_push (TV_SYMOUT);
276 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
277 timevar_pop (TV_SYMOUT);
282 void
283 pass_manager::
284 finish_optimization_passes (void)
286 int i;
287 struct dump_file_info *dfi;
288 char *name;
289 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
291 timevar_push (TV_DUMP);
292 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
294 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
295 end_branch_prob ();
296 dumps->dump_finish (pass_profile_1->static_pass_number);
299 if (optimize > 0)
301 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
302 print_combine_total_stats ();
303 dumps->dump_finish (pass_profile_1->static_pass_number);
306 /* Do whatever is necessary to finish printing the graphs. */
307 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
308 if (dumps->dump_initialized_p (i)
309 && (dfi->pflags & TDF_GRAPH) != 0
310 && (name = dumps->get_dump_file_name (i)) != NULL)
312 finish_graph_dump_file (name);
313 free (name);
316 timevar_pop (TV_DUMP);
319 static unsigned int
320 execute_all_early_local_passes (void)
322 /* Once this pass (and its sub-passes) are complete, all functions
323 will be in SSA form. Technically this state change is happening
324 a tad early, since the sub-passes have not yet run, but since
325 none of the sub-passes are IPA passes and do not create new
326 functions, this is ok. We're setting this value for the benefit
327 of IPA passes that follow. */
328 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
329 cgraph_state = CGRAPH_STATE_IPA_SSA;
330 return 0;
333 /* Gate: execute, or not, all of the non-trivial optimizations. */
335 static bool
336 gate_all_early_local_passes (void)
338 /* Don't bother doing anything if the program has errors. */
339 return (!seen_error () && !in_lto_p);
342 namespace {
344 const pass_data pass_data_early_local_passes =
346 SIMPLE_IPA_PASS, /* type */
347 "early_local_cleanups", /* name */
348 OPTGROUP_NONE, /* optinfo_flags */
349 true, /* has_gate */
350 true, /* has_execute */
351 TV_EARLY_LOCAL, /* tv_id */
352 0, /* properties_required */
353 0, /* properties_provided */
354 0, /* properties_destroyed */
355 0, /* todo_flags_start */
356 TODO_remove_functions, /* todo_flags_finish */
359 class pass_early_local_passes : public simple_ipa_opt_pass
361 public:
362 pass_early_local_passes (gcc::context *ctxt)
363 : simple_ipa_opt_pass (pass_data_early_local_passes, ctxt)
366 /* opt_pass methods: */
367 bool gate () { return gate_all_early_local_passes (); }
368 unsigned int execute () { return execute_all_early_local_passes (); }
370 }; // class pass_early_local_passes
372 } // anon namespace
374 simple_ipa_opt_pass *
375 make_pass_early_local_passes (gcc::context *ctxt)
377 return new pass_early_local_passes (ctxt);
380 /* Gate: execute, or not, all of the non-trivial optimizations. */
382 static bool
383 gate_all_early_optimizations (void)
385 return (optimize >= 1
386 /* Don't bother doing anything if the program has errors. */
387 && !seen_error ());
390 namespace {
392 const pass_data pass_data_all_early_optimizations =
394 GIMPLE_PASS, /* type */
395 "early_optimizations", /* name */
396 OPTGROUP_NONE, /* optinfo_flags */
397 true, /* has_gate */
398 false, /* has_execute */
399 TV_NONE, /* tv_id */
400 0, /* properties_required */
401 0, /* properties_provided */
402 0, /* properties_destroyed */
403 0, /* todo_flags_start */
404 0, /* todo_flags_finish */
407 class pass_all_early_optimizations : public gimple_opt_pass
409 public:
410 pass_all_early_optimizations (gcc::context *ctxt)
411 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
414 /* opt_pass methods: */
415 bool gate () { return gate_all_early_optimizations (); }
417 }; // class pass_all_early_optimizations
419 } // anon namespace
421 static gimple_opt_pass *
422 make_pass_all_early_optimizations (gcc::context *ctxt)
424 return new pass_all_early_optimizations (ctxt);
427 /* Gate: execute, or not, all of the non-trivial optimizations. */
429 static bool
430 gate_all_optimizations (void)
432 return optimize >= 1 && !optimize_debug;
435 namespace {
437 const pass_data pass_data_all_optimizations =
439 GIMPLE_PASS, /* type */
440 "*all_optimizations", /* name */
441 OPTGROUP_NONE, /* optinfo_flags */
442 true, /* has_gate */
443 false, /* has_execute */
444 TV_OPTIMIZE, /* tv_id */
445 0, /* properties_required */
446 0, /* properties_provided */
447 0, /* properties_destroyed */
448 0, /* todo_flags_start */
449 0, /* todo_flags_finish */
452 class pass_all_optimizations : public gimple_opt_pass
454 public:
455 pass_all_optimizations (gcc::context *ctxt)
456 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
459 /* opt_pass methods: */
460 bool gate () { return gate_all_optimizations (); }
462 }; // class pass_all_optimizations
464 } // anon namespace
466 static gimple_opt_pass *
467 make_pass_all_optimizations (gcc::context *ctxt)
469 return new pass_all_optimizations (ctxt);
472 /* Gate: execute, or not, all of the non-trivial optimizations. */
474 static bool
475 gate_all_optimizations_g (void)
477 return optimize >= 1 && optimize_debug;
480 namespace {
482 const pass_data pass_data_all_optimizations_g =
484 GIMPLE_PASS, /* type */
485 "*all_optimizations_g", /* name */
486 OPTGROUP_NONE, /* optinfo_flags */
487 true, /* has_gate */
488 false, /* has_execute */
489 TV_OPTIMIZE, /* tv_id */
490 0, /* properties_required */
491 0, /* properties_provided */
492 0, /* properties_destroyed */
493 0, /* todo_flags_start */
494 0, /* todo_flags_finish */
497 class pass_all_optimizations_g : public gimple_opt_pass
499 public:
500 pass_all_optimizations_g (gcc::context *ctxt)
501 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
504 /* opt_pass methods: */
505 bool gate () { return gate_all_optimizations_g (); }
507 }; // class pass_all_optimizations_g
509 } // anon namespace
511 static gimple_opt_pass *
512 make_pass_all_optimizations_g (gcc::context *ctxt)
514 return new pass_all_optimizations_g (ctxt);
517 static bool
518 gate_rest_of_compilation (void)
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 namespace {
527 const pass_data pass_data_rest_of_compilation =
529 RTL_PASS, /* type */
530 "*rest_of_compilation", /* name */
531 OPTGROUP_NONE, /* optinfo_flags */
532 true, /* has_gate */
533 false, /* has_execute */
534 TV_REST_OF_COMPILATION, /* tv_id */
535 PROP_rtl, /* properties_required */
536 0, /* properties_provided */
537 0, /* properties_destroyed */
538 0, /* todo_flags_start */
539 0, /* todo_flags_finish */
542 class pass_rest_of_compilation : public rtl_opt_pass
544 public:
545 pass_rest_of_compilation (gcc::context *ctxt)
546 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
549 /* opt_pass methods: */
550 bool gate () { return gate_rest_of_compilation (); }
552 }; // class pass_rest_of_compilation
554 } // anon namespace
556 static rtl_opt_pass *
557 make_pass_rest_of_compilation (gcc::context *ctxt)
559 return new pass_rest_of_compilation (ctxt);
562 static bool
563 gate_postreload (void)
565 return reload_completed;
568 namespace {
570 const pass_data pass_data_postreload =
572 RTL_PASS, /* type */
573 "*all-postreload", /* name */
574 OPTGROUP_NONE, /* optinfo_flags */
575 true, /* has_gate */
576 false, /* has_execute */
577 TV_POSTRELOAD, /* tv_id */
578 PROP_rtl, /* properties_required */
579 0, /* properties_provided */
580 0, /* properties_destroyed */
581 0, /* todo_flags_start */
582 TODO_verify_rtl_sharing, /* todo_flags_finish */
585 class pass_postreload : public rtl_opt_pass
587 public:
588 pass_postreload (gcc::context *ctxt)
589 : rtl_opt_pass (pass_data_postreload, ctxt)
592 /* opt_pass methods: */
593 bool gate () { return gate_postreload (); }
595 }; // class pass_postreload
597 } // anon namespace
599 static rtl_opt_pass *
600 make_pass_postreload (gcc::context *ctxt)
602 return new pass_postreload (ctxt);
607 /* Set the static pass number of pass PASS to ID and record that
608 in the mapping from static pass number to pass. */
610 void
611 pass_manager::
612 set_pass_for_id (int id, struct opt_pass *pass)
614 pass->static_pass_number = id;
615 if (passes_by_id_size <= id)
617 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
618 memset (passes_by_id + passes_by_id_size, 0,
619 (id + 1 - passes_by_id_size) * sizeof (void *));
620 passes_by_id_size = id + 1;
622 passes_by_id[id] = pass;
625 /* Return the pass with the static pass number ID. */
627 struct opt_pass *
628 pass_manager::get_pass_for_id (int id) const
630 if (id >= passes_by_id_size)
631 return NULL;
632 return passes_by_id[id];
635 /* Iterate over the pass tree allocating dump file numbers. We want
636 to do this depth first, and independent of whether the pass is
637 enabled or not. */
639 void
640 register_one_dump_file (struct opt_pass *pass)
642 g->get_passes ()->register_one_dump_file (pass);
645 void
646 pass_manager::register_one_dump_file (struct opt_pass *pass)
648 char *dot_name, *flag_name, *glob_name;
649 const char *name, *full_name, *prefix;
650 char num[10];
651 int flags, id;
652 int optgroup_flags = OPTGROUP_NONE;
653 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
655 /* See below in next_pass_1. */
656 num[0] = '\0';
657 if (pass->static_pass_number != -1)
658 sprintf (num, "%d", ((int) pass->static_pass_number < 0
659 ? 1 : pass->static_pass_number));
661 /* The name is both used to identify the pass for the purposes of plugins,
662 and to specify dump file name and option.
663 The latter two might want something short which is not quite unique; for
664 that reason, we may have a disambiguating prefix, followed by a space
665 to mark the start of the following dump file name / option string. */
666 name = strchr (pass->name, ' ');
667 name = name ? name + 1 : pass->name;
668 dot_name = concat (".", name, num, NULL);
669 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
671 prefix = "ipa-";
672 flags = TDF_IPA;
673 optgroup_flags |= OPTGROUP_IPA;
675 else if (pass->type == GIMPLE_PASS)
677 prefix = "tree-";
678 flags = TDF_TREE;
680 else
682 prefix = "rtl-";
683 flags = TDF_RTL;
686 flag_name = concat (prefix, name, num, NULL);
687 glob_name = concat (prefix, name, NULL);
688 optgroup_flags |= pass->optinfo_flags;
689 /* For any passes that do not have an optgroup set, and which are not
690 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
691 any dump messages are emitted properly under -fopt-info(-optall). */
692 if (optgroup_flags == OPTGROUP_NONE)
693 optgroup_flags = OPTGROUP_OTHER;
694 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
695 optgroup_flags);
696 set_pass_for_id (id, pass);
697 full_name = concat (prefix, pass->name, num, NULL);
698 register_pass_name (pass, full_name);
699 free (CONST_CAST (char *, full_name));
702 /* Recursive worker function for register_dump_files. */
705 pass_manager::
706 register_dump_files_1 (struct opt_pass *pass, int properties)
710 int new_properties = (properties | pass->properties_provided)
711 & ~pass->properties_destroyed;
713 if (pass->name && pass->name[0] != '*')
714 register_one_dump_file (pass);
716 if (pass->sub)
717 new_properties = register_dump_files_1 (pass->sub, new_properties);
719 /* If we have a gate, combine the properties that we could have with
720 and without the pass being examined. */
721 if (pass->has_gate)
722 properties &= new_properties;
723 else
724 properties = new_properties;
726 pass = pass->next;
728 while (pass);
730 return properties;
733 /* Register the dump files for the pass_manager starting at PASS.
734 PROPERTIES reflects the properties that are guaranteed to be available at
735 the beginning of the pipeline. */
737 void
738 pass_manager::
739 register_dump_files (struct opt_pass *pass,int properties)
741 pass->properties_required |= properties;
742 register_dump_files_1 (pass, properties);
745 struct pass_registry
747 const char* unique_name;
748 struct opt_pass *pass;
751 /* Helper for pass_registry hash table. */
753 struct pass_registry_hasher : typed_noop_remove <pass_registry>
755 typedef pass_registry value_type;
756 typedef pass_registry compare_type;
757 static inline hashval_t hash (const value_type *);
758 static inline bool equal (const value_type *, const compare_type *);
761 /* Pass registry hash function. */
763 inline hashval_t
764 pass_registry_hasher::hash (const value_type *s)
766 return htab_hash_string (s->unique_name);
769 /* Hash equal function */
771 inline bool
772 pass_registry_hasher::equal (const value_type *s1, const compare_type *s2)
774 return !strcmp (s1->unique_name, s2->unique_name);
777 static hash_table <pass_registry_hasher> name_to_pass_map;
779 /* Register PASS with NAME. */
781 static void
782 register_pass_name (struct opt_pass *pass, const char *name)
784 struct pass_registry **slot;
785 struct pass_registry pr;
787 if (!name_to_pass_map.is_created ())
788 name_to_pass_map.create (256);
790 pr.unique_name = name;
791 slot = name_to_pass_map.find_slot (&pr, INSERT);
792 if (!*slot)
794 struct pass_registry *new_pr;
796 new_pr = XCNEW (struct pass_registry);
797 new_pr->unique_name = xstrdup (name);
798 new_pr->pass = pass;
799 *slot = new_pr;
801 else
802 return; /* Ignore plugin passes. */
805 /* Map from pass id to canonicalized pass name. */
807 typedef const char *char_ptr;
808 static vec<char_ptr> pass_tab = vNULL;
810 /* Callback function for traversing NAME_TO_PASS_MAP. */
813 passes_pass_traverse (pass_registry **p, void *data ATTRIBUTE_UNUSED)
815 struct opt_pass *pass = (*p)->pass;
817 gcc_assert (pass->static_pass_number > 0);
818 gcc_assert (pass_tab.exists ());
820 pass_tab[pass->static_pass_number] = (*p)->unique_name;
822 return 1;
825 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
826 table for dumping purpose. */
828 static void
829 create_pass_tab (void)
831 if (!flag_dump_passes)
832 return;
834 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
835 name_to_pass_map.traverse <void *, passes_pass_traverse> (NULL);
838 static bool override_gate_status (struct opt_pass *, tree, bool);
840 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
841 is turned on or not. */
843 static void
844 dump_one_pass (struct opt_pass *pass, int pass_indent)
846 int indent = 3 * pass_indent;
847 const char *pn;
848 bool is_on, is_really_on;
850 is_on = pass->has_gate ? pass->gate () : true;
851 is_really_on = override_gate_status (pass, current_function_decl, is_on);
853 if (pass->static_pass_number <= 0)
854 pn = pass->name;
855 else
856 pn = pass_tab[pass->static_pass_number];
858 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
859 (15 - indent < 0 ? 0 : 15 - indent), " ",
860 is_on ? " ON" : " OFF",
861 ((!is_on) == (!is_really_on) ? ""
862 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
865 /* Dump pass list PASS with indentation INDENT. */
867 static void
868 dump_pass_list (struct opt_pass *pass, int indent)
872 dump_one_pass (pass, indent);
873 if (pass->sub)
874 dump_pass_list (pass->sub, indent + 1);
875 pass = pass->next;
877 while (pass);
880 /* Dump all optimization passes. */
882 void
883 dump_passes (void)
885 g->get_passes ()->dump_passes ();
888 void
889 pass_manager::dump_passes () const
891 struct cgraph_node *n, *node = NULL;
893 create_pass_tab ();
895 FOR_EACH_FUNCTION (n)
896 if (DECL_STRUCT_FUNCTION (n->decl))
898 node = n;
899 break;
902 if (!node)
903 return;
905 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
907 dump_pass_list (all_lowering_passes, 1);
908 dump_pass_list (all_small_ipa_passes, 1);
909 dump_pass_list (all_regular_ipa_passes, 1);
910 dump_pass_list (all_late_ipa_passes, 1);
911 dump_pass_list (all_passes, 1);
913 pop_cfun ();
917 /* Returns the pass with NAME. */
919 static struct opt_pass *
920 get_pass_by_name (const char *name)
922 struct pass_registry **slot, pr;
924 pr.unique_name = name;
925 slot = name_to_pass_map.find_slot (&pr, NO_INSERT);
927 if (!slot || !*slot)
928 return NULL;
930 return (*slot)->pass;
934 /* Range [start, last]. */
936 struct uid_range
938 unsigned int start;
939 unsigned int last;
940 const char *assem_name;
941 struct uid_range *next;
944 typedef struct uid_range *uid_range_p;
947 static vec<uid_range_p>
948 enabled_pass_uid_range_tab = vNULL;
949 static vec<uid_range_p>
950 disabled_pass_uid_range_tab = vNULL;
953 /* Parse option string for -fdisable- and -fenable-
954 The syntax of the options:
956 -fenable-<pass_name>
957 -fdisable-<pass_name>
959 -fenable-<pass_name>=s1:e1,s2:e2,...
960 -fdisable-<pass_name>=s1:e1,s2:e2,...
963 static void
964 enable_disable_pass (const char *arg, bool is_enable)
966 struct opt_pass *pass;
967 char *range_str, *phase_name;
968 char *argstr = xstrdup (arg);
969 vec<uid_range_p> *tab = 0;
971 range_str = strchr (argstr,'=');
972 if (range_str)
974 *range_str = '\0';
975 range_str++;
978 phase_name = argstr;
979 if (!*phase_name)
981 if (is_enable)
982 error ("unrecognized option -fenable");
983 else
984 error ("unrecognized option -fdisable");
985 free (argstr);
986 return;
988 pass = get_pass_by_name (phase_name);
989 if (!pass || pass->static_pass_number == -1)
991 if (is_enable)
992 error ("unknown pass %s specified in -fenable", phase_name);
993 else
994 error ("unknown pass %s specified in -fdisable", phase_name);
995 free (argstr);
996 return;
999 if (is_enable)
1000 tab = &enabled_pass_uid_range_tab;
1001 else
1002 tab = &disabled_pass_uid_range_tab;
1004 if ((unsigned) pass->static_pass_number >= tab->length ())
1005 tab->safe_grow_cleared (pass->static_pass_number + 1);
1007 if (!range_str)
1009 uid_range_p slot;
1010 uid_range_p new_range = XCNEW (struct uid_range);
1012 new_range->start = 0;
1013 new_range->last = (unsigned)-1;
1015 slot = (*tab)[pass->static_pass_number];
1016 new_range->next = slot;
1017 (*tab)[pass->static_pass_number] = new_range;
1018 if (is_enable)
1019 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1020 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1021 else
1022 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1023 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1025 else
1027 char *next_range = NULL;
1028 char *one_range = range_str;
1029 char *end_val = NULL;
1033 uid_range_p slot;
1034 uid_range_p new_range;
1035 char *invalid = NULL;
1036 long start;
1037 char *func_name = NULL;
1039 next_range = strchr (one_range, ',');
1040 if (next_range)
1042 *next_range = '\0';
1043 next_range++;
1046 end_val = strchr (one_range, ':');
1047 if (end_val)
1049 *end_val = '\0';
1050 end_val++;
1052 start = strtol (one_range, &invalid, 10);
1053 if (*invalid || start < 0)
1055 if (end_val || (one_range[0] >= '0'
1056 && one_range[0] <= '9'))
1058 error ("Invalid range %s in option %s",
1059 one_range,
1060 is_enable ? "-fenable" : "-fdisable");
1061 free (argstr);
1062 return;
1064 func_name = one_range;
1066 if (!end_val)
1068 new_range = XCNEW (struct uid_range);
1069 if (!func_name)
1071 new_range->start = (unsigned) start;
1072 new_range->last = (unsigned) start;
1074 else
1076 new_range->start = (unsigned) -1;
1077 new_range->last = (unsigned) -1;
1078 new_range->assem_name = xstrdup (func_name);
1081 else
1083 long last = strtol (end_val, &invalid, 10);
1084 if (*invalid || last < start)
1086 error ("Invalid range %s in option %s",
1087 end_val,
1088 is_enable ? "-fenable" : "-fdisable");
1089 free (argstr);
1090 return;
1092 new_range = XCNEW (struct uid_range);
1093 new_range->start = (unsigned) start;
1094 new_range->last = (unsigned) last;
1097 slot = (*tab)[pass->static_pass_number];
1098 new_range->next = slot;
1099 (*tab)[pass->static_pass_number] = new_range;
1100 if (is_enable)
1102 if (new_range->assem_name)
1103 inform (UNKNOWN_LOCATION,
1104 "enable pass %s for function %s",
1105 phase_name, new_range->assem_name);
1106 else
1107 inform (UNKNOWN_LOCATION,
1108 "enable pass %s for functions in the range of [%u, %u]",
1109 phase_name, new_range->start, new_range->last);
1111 else
1113 if (new_range->assem_name)
1114 inform (UNKNOWN_LOCATION,
1115 "disable pass %s for function %s",
1116 phase_name, new_range->assem_name);
1117 else
1118 inform (UNKNOWN_LOCATION,
1119 "disable pass %s for functions in the range of [%u, %u]",
1120 phase_name, new_range->start, new_range->last);
1123 one_range = next_range;
1124 } while (next_range);
1127 free (argstr);
1130 /* Enable pass specified by ARG. */
1132 void
1133 enable_pass (const char *arg)
1135 enable_disable_pass (arg, true);
1138 /* Disable pass specified by ARG. */
1140 void
1141 disable_pass (const char *arg)
1143 enable_disable_pass (arg, false);
1146 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1148 static bool
1149 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
1150 tree func,
1151 vec<uid_range_p> tab)
1153 uid_range_p slot, range;
1154 int cgraph_uid;
1155 const char *aname = NULL;
1157 if (!tab.exists ()
1158 || (unsigned) pass->static_pass_number >= tab.length ()
1159 || pass->static_pass_number == -1)
1160 return false;
1162 slot = tab[pass->static_pass_number];
1163 if (!slot)
1164 return false;
1166 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
1167 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1168 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1170 range = slot;
1171 while (range)
1173 if ((unsigned) cgraph_uid >= range->start
1174 && (unsigned) cgraph_uid <= range->last)
1175 return true;
1176 if (range->assem_name && aname
1177 && !strcmp (range->assem_name, aname))
1178 return true;
1179 range = range->next;
1182 return false;
1186 /* Update static_pass_number for passes (and the flag
1187 TODO_mark_first_instance).
1189 Passes are constructed with static_pass_number preinitialized to 0
1191 This field is used in two different ways: initially as instance numbers
1192 of their kind, and then as ids within the entire pass manager.
1194 Within pass_manager::pass_manager:
1196 * In add_pass_instance(), as called by next_pass_1 in
1197 NEXT_PASS in init_optimization_passes
1199 * When the initial instance of a pass within a pass manager is seen,
1200 it is flagged, and its static_pass_number is set to -1
1202 * On subsequent times that it is seen, the static pass number
1203 is decremented each time, so that if there are e.g. 4 dups,
1204 they have static_pass_number -4, 2, 3, 4 respectively (note
1205 how the initial one is negative and gives the count); these
1206 can be thought of as instance numbers of the specific pass
1208 * Within the register_dump_files () traversal, set_pass_for_id()
1209 is called on each pass, using these instance numbers to create
1210 dumpfile switches, and then overwriting them with a pass id,
1211 which are global to the whole pass manager (based on
1212 (TDI_end + current value of extra_dump_files_in_use) ) */
1214 static void
1215 add_pass_instance (struct opt_pass *new_pass, bool track_duplicates,
1216 opt_pass *initial_pass)
1218 /* Are we dealing with the first pass of its kind, or a clone? */
1219 if (new_pass != initial_pass)
1221 /* We're dealing with a clone. */
1222 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1224 /* Indicate to register_dump_files that this pass has duplicates,
1225 and so it should rename the dump file. The first instance will
1226 be -1, and be number of duplicates = -static_pass_number - 1.
1227 Subsequent instances will be > 0 and just the duplicate number. */
1228 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1230 initial_pass->static_pass_number -= 1;
1231 new_pass->static_pass_number = -initial_pass->static_pass_number;
1234 else
1236 /* We're dealing with the first pass of its kind. */
1237 new_pass->todo_flags_start |= TODO_mark_first_instance;
1238 new_pass->static_pass_number = -1;
1240 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1244 /* Add a pass to the pass list. Duplicate the pass if it's already
1245 in the list. */
1247 static struct opt_pass **
1248 next_pass_1 (struct opt_pass **list, struct opt_pass *pass,
1249 struct opt_pass *initial_pass)
1251 /* Every pass should have a name so that plugins can refer to them. */
1252 gcc_assert (pass->name != NULL);
1254 add_pass_instance (pass, false, initial_pass);
1255 *list = pass;
1257 return &(*list)->next;
1260 /* List node for an inserted pass instance. We need to keep track of all
1261 the newly-added pass instances (with 'added_pass_nodes' defined below)
1262 so that we can register their dump files after pass-positioning is finished.
1263 Registering dumping files needs to be post-processed or the
1264 static_pass_number of the opt_pass object would be modified and mess up
1265 the dump file names of future pass instances to be added. */
1267 struct pass_list_node
1269 struct opt_pass *pass;
1270 struct pass_list_node *next;
1273 static struct pass_list_node *added_pass_nodes = NULL;
1274 static struct pass_list_node *prev_added_pass_node;
1276 /* Insert the pass at the proper position. Return true if the pass
1277 is successfully added.
1279 NEW_PASS_INFO - new pass to be inserted
1280 PASS_LIST - root of the pass list to insert the new pass to */
1282 static bool
1283 position_pass (struct register_pass_info *new_pass_info,
1284 struct opt_pass **pass_list)
1286 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1287 bool success = false;
1289 for ( ; pass; prev_pass = pass, pass = pass->next)
1291 /* Check if the current pass is of the same type as the new pass and
1292 matches the name and the instance number of the reference pass. */
1293 if (pass->type == new_pass_info->pass->type
1294 && pass->name
1295 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1296 && ((new_pass_info->ref_pass_instance_number == 0)
1297 || (new_pass_info->ref_pass_instance_number ==
1298 pass->static_pass_number)
1299 || (new_pass_info->ref_pass_instance_number == 1
1300 && pass->todo_flags_start & TODO_mark_first_instance)))
1302 struct opt_pass *new_pass;
1303 struct pass_list_node *new_pass_node;
1305 if (new_pass_info->ref_pass_instance_number == 0)
1307 new_pass = new_pass_info->pass->clone ();
1308 add_pass_instance (new_pass, true, new_pass_info->pass);
1310 else
1312 new_pass = new_pass_info->pass;
1313 add_pass_instance (new_pass, true, new_pass);
1316 /* Insert the new pass instance based on the positioning op. */
1317 switch (new_pass_info->pos_op)
1319 case PASS_POS_INSERT_AFTER:
1320 new_pass->next = pass->next;
1321 pass->next = new_pass;
1323 /* Skip newly inserted pass to avoid repeated
1324 insertions in the case where the new pass and the
1325 existing one have the same name. */
1326 pass = new_pass;
1327 break;
1328 case PASS_POS_INSERT_BEFORE:
1329 new_pass->next = pass;
1330 if (prev_pass)
1331 prev_pass->next = new_pass;
1332 else
1333 *pass_list = new_pass;
1334 break;
1335 case PASS_POS_REPLACE:
1336 new_pass->next = pass->next;
1337 if (prev_pass)
1338 prev_pass->next = new_pass;
1339 else
1340 *pass_list = new_pass;
1341 new_pass->sub = pass->sub;
1342 new_pass->tv_id = pass->tv_id;
1343 pass = new_pass;
1344 break;
1345 default:
1346 error ("invalid pass positioning operation");
1347 return false;
1350 /* Save the newly added pass (instance) in the added_pass_nodes
1351 list so that we can register its dump file later. Note that
1352 we cannot register the dump file now because doing so will modify
1353 the static_pass_number of the opt_pass object and therefore
1354 mess up the dump file name of future instances. */
1355 new_pass_node = XCNEW (struct pass_list_node);
1356 new_pass_node->pass = new_pass;
1357 if (!added_pass_nodes)
1358 added_pass_nodes = new_pass_node;
1359 else
1360 prev_added_pass_node->next = new_pass_node;
1361 prev_added_pass_node = new_pass_node;
1363 success = true;
1366 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1367 success = true;
1370 return success;
1373 /* Hooks a new pass into the pass lists.
1375 PASS_INFO - pass information that specifies the opt_pass object,
1376 reference pass, instance number, and how to position
1377 the pass */
1379 void
1380 register_pass (struct register_pass_info *pass_info)
1382 g->get_passes ()->register_pass (pass_info);
1385 void
1386 register_pass (opt_pass* pass, pass_positioning_ops pos,
1387 const char* ref_pass_name, int ref_pass_inst_number)
1389 register_pass_info i;
1390 i.pass = pass;
1391 i.reference_pass_name = ref_pass_name;
1392 i.ref_pass_instance_number = ref_pass_inst_number;
1393 i.pos_op = pos;
1395 g->get_passes ()->register_pass (&i);
1398 void
1399 pass_manager::register_pass (struct register_pass_info *pass_info)
1401 bool all_instances, success;
1402 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1404 /* The checks below could fail in buggy plugins. Existing GCC
1405 passes should never fail these checks, so we mention plugin in
1406 the messages. */
1407 if (!pass_info->pass)
1408 fatal_error ("plugin cannot register a missing pass");
1410 if (!pass_info->pass->name)
1411 fatal_error ("plugin cannot register an unnamed pass");
1413 if (!pass_info->reference_pass_name)
1414 fatal_error
1415 ("plugin cannot register pass %qs without reference pass name",
1416 pass_info->pass->name);
1418 /* Try to insert the new pass to the pass lists. We need to check
1419 all five lists as the reference pass could be in one (or all) of
1420 them. */
1421 all_instances = pass_info->ref_pass_instance_number == 0;
1422 success = position_pass (pass_info, &all_lowering_passes);
1423 if (!success || all_instances)
1424 success |= position_pass (pass_info, &all_small_ipa_passes);
1425 if (!success || all_instances)
1426 success |= position_pass (pass_info, &all_regular_ipa_passes);
1427 if (!success || all_instances)
1428 success |= position_pass (pass_info, &all_late_ipa_passes);
1429 if (!success || all_instances)
1430 success |= position_pass (pass_info, &all_passes);
1431 if (!success)
1432 fatal_error
1433 ("pass %qs not found but is referenced by new pass %qs",
1434 pass_info->reference_pass_name, pass_info->pass->name);
1436 /* OK, we have successfully inserted the new pass. We need to register
1437 the dump files for the newly added pass and its duplicates (if any).
1438 Because the registration of plugin/backend passes happens after the
1439 command-line options are parsed, the options that specify single
1440 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1441 passes. Therefore we currently can only enable dumping of
1442 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1443 are specified. While doing so, we also delete the pass_list_node
1444 objects created during pass positioning. */
1445 while (added_pass_nodes)
1447 struct pass_list_node *next_node = added_pass_nodes->next;
1448 enum tree_dump_index tdi;
1449 register_one_dump_file (added_pass_nodes->pass);
1450 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1451 || added_pass_nodes->pass->type == IPA_PASS)
1452 tdi = TDI_ipa_all;
1453 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1454 tdi = TDI_tree_all;
1455 else
1456 tdi = TDI_rtl_all;
1457 /* Check if dump-all flag is specified. */
1458 if (dumps->get_dump_file_info (tdi)->pstate)
1459 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1460 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1461 XDELETE (added_pass_nodes);
1462 added_pass_nodes = next_node;
1466 /* Construct the pass tree. The sequencing of passes is driven by
1467 the cgraph routines:
1469 finalize_compilation_unit ()
1470 for each node N in the cgraph
1471 cgraph_analyze_function (N)
1472 cgraph_lower_function (N) -> all_lowering_passes
1474 If we are optimizing, compile is then invoked:
1476 compile ()
1477 ipa_passes () -> all_small_ipa_passes
1478 -> Analysis of all_regular_ipa_passes
1479 * possible LTO streaming at copmilation time *
1480 -> Execution of all_regular_ipa_passes
1481 * possible LTO streaming at link time *
1482 -> all_late_ipa_passes
1483 expand_all_functions ()
1484 for each node N in the cgraph
1485 expand_function (N) -> Transformation of all_regular_ipa_passes
1486 -> all_passes
1489 void *
1490 pass_manager::operator new (size_t sz)
1492 /* Ensure that all fields of the pass manager are zero-initialized. */
1493 return xcalloc (1, sz);
1496 pass_manager::pass_manager (context *ctxt)
1497 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1498 all_regular_ipa_passes (NULL),
1499 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1500 m_ctxt (ctxt)
1502 struct opt_pass **p;
1504 /* Initialize the pass_lists array. */
1505 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1506 GCC_PASS_LISTS
1507 #undef DEF_PASS_LIST
1509 /* Build the tree of passes. */
1511 #define INSERT_PASSES_AFTER(PASS) \
1512 p = &(PASS);
1514 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1516 struct opt_pass **p = &(PASS ## _1)->sub;
1518 #define POP_INSERT_PASSES() \
1521 #define NEXT_PASS(PASS, NUM) \
1522 do { \
1523 gcc_assert (NULL == PASS ## _ ## NUM); \
1524 if ((NUM) == 1) \
1525 PASS ## _1 = make_##PASS (m_ctxt); \
1526 else \
1528 gcc_assert (PASS ## _1); \
1529 PASS ## _ ## NUM = PASS ## _1->clone (); \
1531 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1532 } while (0)
1534 #define TERMINATE_PASS_LIST() \
1535 *p = NULL;
1537 #include "pass-instances.def"
1539 #undef INSERT_PASSES_AFTER
1540 #undef PUSH_INSERT_PASSES_WITHIN
1541 #undef POP_INSERT_PASSES
1542 #undef NEXT_PASS
1543 #undef TERMINATE_PASS_LIST
1545 /* Register the passes with the tree dump code. */
1546 register_dump_files (all_lowering_passes, PROP_gimple_any);
1547 register_dump_files (all_small_ipa_passes,
1548 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1549 | PROP_cfg);
1550 register_dump_files (all_regular_ipa_passes,
1551 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1552 | PROP_cfg);
1553 register_dump_files (all_late_ipa_passes,
1554 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1555 | PROP_cfg);
1556 register_dump_files (all_passes,
1557 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1558 | PROP_cfg);
1561 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1562 function CALLBACK for every function in the call graph. Otherwise,
1563 call CALLBACK on the current function. */
1565 static void
1566 do_per_function (void (*callback) (void *data), void *data)
1568 if (current_function_decl)
1569 callback (data);
1570 else
1572 struct cgraph_node *node;
1573 FOR_EACH_DEFINED_FUNCTION (node)
1574 if (node->analyzed && gimple_has_body_p (node->decl)
1575 && (!node->clone_of || node->decl != node->clone_of->decl))
1577 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1578 callback (data);
1579 if (!flag_wpa)
1581 free_dominance_info (CDI_DOMINATORS);
1582 free_dominance_info (CDI_POST_DOMINATORS);
1584 pop_cfun ();
1585 ggc_collect ();
1590 /* Because inlining might remove no-longer reachable nodes, we need to
1591 keep the array visible to garbage collector to avoid reading collected
1592 out nodes. */
1593 static int nnodes;
1594 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1596 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1597 function CALLBACK for every function in the call graph. Otherwise,
1598 call CALLBACK on the current function.
1599 This function is global so that plugins can use it. */
1600 void
1601 do_per_function_toporder (void (*callback) (void *data), void *data)
1603 int i;
1605 if (current_function_decl)
1606 callback (data);
1607 else
1609 gcc_assert (!order);
1610 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1611 nnodes = ipa_reverse_postorder (order);
1612 for (i = nnodes - 1; i >= 0; i--)
1613 order[i]->process = 1;
1614 for (i = nnodes - 1; i >= 0; i--)
1616 struct cgraph_node *node = order[i];
1618 /* Allow possibly removed nodes to be garbage collected. */
1619 order[i] = NULL;
1620 node->process = 0;
1621 if (cgraph_function_with_gimple_body_p (node))
1623 cgraph_get_body (node);
1624 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1625 callback (data);
1626 free_dominance_info (CDI_DOMINATORS);
1627 free_dominance_info (CDI_POST_DOMINATORS);
1628 pop_cfun ();
1629 ggc_collect ();
1633 ggc_free (order);
1634 order = NULL;
1635 nnodes = 0;
1638 /* Helper function to perform function body dump. */
1640 static void
1641 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1643 if (dump_file && current_function_decl)
1645 if (cfun->curr_properties & PROP_trees)
1646 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1647 else
1648 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1650 /* Flush the file. If verification fails, we won't be able to
1651 close the file before aborting. */
1652 fflush (dump_file);
1654 if ((cfun->curr_properties & PROP_cfg)
1655 && (dump_flags & TDF_GRAPH))
1656 print_graph_cfg (dump_file_name, cfun);
1660 static struct profile_record *profile_record;
1662 /* Do profile consistency book-keeping for the pass with static number INDEX.
1663 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1664 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1665 if we are only book-keeping on passes that may have selectively disabled
1666 themselves on a given function. */
1667 static void
1668 check_profile_consistency (int index, int subpass, bool run)
1670 pass_manager *passes = g->get_passes ();
1671 if (index == -1)
1672 return;
1673 if (!profile_record)
1674 profile_record = XCNEWVEC (struct profile_record,
1675 passes->passes_by_id_size);
1676 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1677 gcc_assert (subpass < 2);
1678 profile_record[index].run |= run;
1679 account_profile_record (&profile_record[index], subpass);
1682 /* Output profile consistency. */
1684 void
1685 dump_profile_report (void)
1687 g->get_passes ()->dump_profile_report ();
1690 void
1691 pass_manager::dump_profile_report () const
1693 int i, j;
1694 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1695 gcov_type last_time = 0, last_size = 0;
1696 double rel_time_change, rel_size_change;
1697 int last_reported = 0;
1699 if (!profile_record)
1700 return;
1701 fprintf (stderr, "\nProfile consistency report:\n\n");
1702 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1703 fprintf (stderr, " |freq count |freq count |size time\n");
1705 for (i = 0; i < passes_by_id_size; i++)
1706 for (j = 0 ; j < 2; j++)
1707 if (profile_record[i].run)
1709 if (last_time)
1710 rel_time_change = (profile_record[i].time[j]
1711 - (double)last_time) * 100 / (double)last_time;
1712 else
1713 rel_time_change = 0;
1714 if (last_size)
1715 rel_size_change = (profile_record[i].size[j]
1716 - (double)last_size) * 100 / (double)last_size;
1717 else
1718 rel_size_change = 0;
1720 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1721 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1722 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1723 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1724 || rel_time_change || rel_size_change)
1726 last_reported = i;
1727 fprintf (stderr, "%-20s %s",
1728 passes_by_id [i]->name,
1729 j ? "(after TODO)" : " ");
1730 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1731 fprintf (stderr, "| %+5i",
1732 profile_record[i].num_mismatched_freq_in[j]
1733 - last_freq_in);
1734 else
1735 fprintf (stderr, "| ");
1736 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1737 fprintf (stderr, " %+5i",
1738 profile_record[i].num_mismatched_count_in[j]
1739 - last_count_in);
1740 else
1741 fprintf (stderr, " ");
1742 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1743 fprintf (stderr, "| %+5i",
1744 profile_record[i].num_mismatched_freq_out[j]
1745 - last_freq_out);
1746 else
1747 fprintf (stderr, "| ");
1748 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1749 fprintf (stderr, " %+5i",
1750 profile_record[i].num_mismatched_count_out[j]
1751 - last_count_out);
1752 else
1753 fprintf (stderr, " ");
1755 /* Size/time units change across gimple and RTL. */
1756 if (i == pass_expand_1->static_pass_number)
1757 fprintf (stderr, "|----------");
1758 else
1760 if (rel_size_change)
1761 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1762 else
1763 fprintf (stderr, "| ");
1764 if (rel_time_change)
1765 fprintf (stderr, " %+8.4f%%", rel_time_change);
1767 fprintf (stderr, "\n");
1768 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1769 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1770 last_count_in = profile_record[i].num_mismatched_count_in[j];
1771 last_count_out = profile_record[i].num_mismatched_count_out[j];
1773 else if (j && last_reported != i)
1775 last_reported = i;
1776 fprintf (stderr, "%-20s ------------| | |\n",
1777 passes_by_id [i]->name);
1779 last_time = profile_record[i].time[j];
1780 last_size = profile_record[i].size[j];
1784 /* Perform all TODO actions that ought to be done on each function. */
1786 static void
1787 execute_function_todo (void *data)
1789 unsigned int flags = (size_t)data;
1790 flags &= ~cfun->last_verified;
1791 if (!flags)
1792 return;
1794 /* Always cleanup the CFG before trying to update SSA. */
1795 if (flags & TODO_cleanup_cfg)
1797 cleanup_tree_cfg ();
1799 /* When cleanup_tree_cfg merges consecutive blocks, it may
1800 perform some simplistic propagation when removing single
1801 valued PHI nodes. This propagation may, in turn, cause the
1802 SSA form to become out-of-date (see PR 22037). So, even
1803 if the parent pass had not scheduled an SSA update, we may
1804 still need to do one. */
1805 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1806 flags |= TODO_update_ssa;
1809 if (flags & TODO_update_ssa_any)
1811 unsigned update_flags = flags & TODO_update_ssa_any;
1812 update_ssa (update_flags);
1813 cfun->last_verified &= ~TODO_verify_ssa;
1816 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1817 compute_may_aliases ();
1819 if (optimize && (flags & TODO_update_address_taken))
1820 execute_update_addresses_taken ();
1822 if (flags & TODO_remove_unused_locals)
1823 remove_unused_locals ();
1825 if (flags & TODO_rebuild_frequencies)
1826 rebuild_frequencies ();
1828 if (flags & TODO_rebuild_cgraph_edges)
1829 rebuild_cgraph_edges ();
1831 /* If we've seen errors do not bother running any verifiers. */
1832 if (seen_error ())
1833 return;
1835 #if defined ENABLE_CHECKING
1836 if (flags & TODO_verify_ssa
1837 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1839 verify_gimple_in_cfg (cfun);
1840 verify_ssa (true);
1842 else if (flags & TODO_verify_stmts)
1843 verify_gimple_in_cfg (cfun);
1844 if (flags & TODO_verify_flow)
1845 verify_flow_info ();
1846 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1847 verify_loop_closed_ssa (false);
1848 if (flags & TODO_verify_rtl_sharing)
1849 verify_rtl_sharing ();
1850 #endif
1852 cfun->last_verified = flags & TODO_verify_all;
1855 /* Perform all TODO actions. */
1856 static void
1857 execute_todo (unsigned int flags)
1859 #if defined ENABLE_CHECKING
1860 if (cfun
1861 && need_ssa_update_p (cfun))
1862 gcc_assert (flags & TODO_update_ssa_any);
1863 #endif
1865 timevar_push (TV_TODO);
1867 /* Inform the pass whether it is the first time it is run. */
1868 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1870 statistics_fini_pass ();
1872 if (flags)
1873 do_per_function (execute_function_todo, (void *)(size_t) flags);
1875 /* Always remove functions just as before inlining: IPA passes might be
1876 interested to see bodies of extern inline functions that are not inlined
1877 to analyze side effects. The full removal is done just at the end
1878 of IPA pass queue. */
1879 if (flags & TODO_remove_functions)
1881 gcc_assert (!cfun);
1882 symtab_remove_unreachable_nodes (true, dump_file);
1885 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1887 gcc_assert (!cfun);
1888 dump_symtab (dump_file);
1889 /* Flush the file. If verification fails, we won't be able to
1890 close the file before aborting. */
1891 fflush (dump_file);
1894 /* Now that the dumping has been done, we can get rid of the optional
1895 df problems. */
1896 if (flags & TODO_df_finish)
1897 df_finish_pass ((flags & TODO_df_verify) != 0);
1899 timevar_pop (TV_TODO);
1902 /* Verify invariants that should hold between passes. This is a place
1903 to put simple sanity checks. */
1905 static void
1906 verify_interpass_invariants (void)
1908 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1911 /* Clear the last verified flag. */
1913 static void
1914 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1916 cfun->last_verified = 0;
1919 /* Helper function. Verify that the properties has been turn into the
1920 properties expected by the pass. */
1922 #ifdef ENABLE_CHECKING
1923 static void
1924 verify_curr_properties (void *data)
1926 unsigned int props = (size_t)data;
1927 gcc_assert ((cfun->curr_properties & props) == props);
1929 #endif
1931 /* Initialize pass dump file. */
1932 /* This is non-static so that the plugins can use it. */
1934 bool
1935 pass_init_dump_file (struct opt_pass *pass)
1937 /* If a dump file name is present, open it if enabled. */
1938 if (pass->static_pass_number != -1)
1940 timevar_push (TV_DUMP);
1941 gcc::dump_manager *dumps = g->get_dumps ();
1942 bool initializing_dump =
1943 !dumps->dump_initialized_p (pass->static_pass_number);
1944 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
1945 dumps->dump_start (pass->static_pass_number, &dump_flags);
1946 if (dump_file && current_function_decl)
1947 dump_function_header (dump_file, current_function_decl, dump_flags);
1948 if (initializing_dump
1949 && dump_file && (dump_flags & TDF_GRAPH)
1950 && cfun && (cfun->curr_properties & PROP_cfg))
1951 clean_graph_dump_file (dump_file_name);
1952 timevar_pop (TV_DUMP);
1953 return initializing_dump;
1955 else
1956 return false;
1959 /* Flush PASS dump file. */
1960 /* This is non-static so that plugins can use it. */
1962 void
1963 pass_fini_dump_file (struct opt_pass *pass)
1965 timevar_push (TV_DUMP);
1967 /* Flush and close dump file. */
1968 if (dump_file_name)
1970 free (CONST_CAST (char *, dump_file_name));
1971 dump_file_name = NULL;
1974 g->get_dumps ()->dump_finish (pass->static_pass_number);
1975 timevar_pop (TV_DUMP);
1978 /* After executing the pass, apply expected changes to the function
1979 properties. */
1981 static void
1982 update_properties_after_pass (void *data)
1984 struct opt_pass *pass = (struct opt_pass *) data;
1985 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1986 & ~pass->properties_destroyed;
1989 /* Execute summary generation for all of the passes in IPA_PASS. */
1991 void
1992 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1994 while (ipa_pass)
1996 struct opt_pass *pass = ipa_pass;
1998 /* Execute all of the IPA_PASSes in the list. */
1999 if (ipa_pass->type == IPA_PASS
2000 && ((!pass->has_gate) || pass->gate ())
2001 && ipa_pass->generate_summary)
2003 pass_init_dump_file (pass);
2005 /* If a timevar is present, start it. */
2006 if (pass->tv_id)
2007 timevar_push (pass->tv_id);
2009 ipa_pass->generate_summary ();
2011 /* Stop timevar. */
2012 if (pass->tv_id)
2013 timevar_pop (pass->tv_id);
2015 pass_fini_dump_file (pass);
2017 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->next;
2021 /* Execute IPA_PASS function transform on NODE. */
2023 static void
2024 execute_one_ipa_transform_pass (struct cgraph_node *node,
2025 struct ipa_opt_pass_d *ipa_pass)
2027 struct opt_pass *pass = ipa_pass;
2028 unsigned int todo_after = 0;
2030 current_pass = pass;
2031 if (!ipa_pass->function_transform)
2032 return;
2034 /* Note that the folders should only create gimple expressions.
2035 This is a hack until the new folder is ready. */
2036 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2038 pass_init_dump_file (pass);
2040 /* Run pre-pass verification. */
2041 execute_todo (ipa_pass->function_transform_todo_flags_start);
2043 /* If a timevar is present, start it. */
2044 if (pass->tv_id != TV_NONE)
2045 timevar_push (pass->tv_id);
2047 /* Do it! */
2048 todo_after = ipa_pass->function_transform (node);
2050 /* Stop timevar. */
2051 if (pass->tv_id != TV_NONE)
2052 timevar_pop (pass->tv_id);
2054 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2055 check_profile_consistency (pass->static_pass_number, 0, true);
2057 /* Run post-pass cleanup and verification. */
2058 execute_todo (todo_after);
2059 verify_interpass_invariants ();
2060 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2061 check_profile_consistency (pass->static_pass_number, 1, true);
2063 if (dump_file)
2064 do_per_function (execute_function_dump, NULL);
2065 pass_fini_dump_file (pass);
2067 current_pass = NULL;
2069 /* Signal this is a suitable GC collection point. */
2070 if (!(todo_after & TODO_do_not_ggc_collect))
2071 ggc_collect ();
2074 /* For the current function, execute all ipa transforms. */
2076 void
2077 execute_all_ipa_transforms (void)
2079 struct cgraph_node *node;
2080 if (!cfun)
2081 return;
2082 node = cgraph_get_node (current_function_decl);
2084 if (node->ipa_transforms_to_apply.exists ())
2086 unsigned int i;
2088 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2089 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2090 node->ipa_transforms_to_apply.release ();
2094 /* Callback for do_per_function to apply all IPA transforms. */
2096 static void
2097 apply_ipa_transforms (void *data)
2099 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2100 if (!node->global.inlined_to && node->ipa_transforms_to_apply.exists ())
2102 *(bool *)data = true;
2103 execute_all_ipa_transforms ();
2104 rebuild_cgraph_edges ();
2108 /* Check if PASS is explicitly disabled or enabled and return
2109 the gate status. FUNC is the function to be processed, and
2110 GATE_STATUS is the gate status determined by pass manager by
2111 default. */
2113 static bool
2114 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2116 bool explicitly_enabled = false;
2117 bool explicitly_disabled = false;
2119 explicitly_enabled
2120 = is_pass_explicitly_enabled_or_disabled (pass, func,
2121 enabled_pass_uid_range_tab);
2122 explicitly_disabled
2123 = is_pass_explicitly_enabled_or_disabled (pass, func,
2124 disabled_pass_uid_range_tab);
2126 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2128 return gate_status;
2132 /* Execute PASS. */
2134 bool
2135 execute_one_pass (struct opt_pass *pass)
2137 unsigned int todo_after = 0;
2139 bool gate_status;
2141 /* IPA passes are executed on whole program, so cfun should be NULL.
2142 Other passes need function context set. */
2143 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2144 gcc_assert (!cfun && !current_function_decl);
2145 else
2146 gcc_assert (cfun && current_function_decl);
2148 current_pass = pass;
2150 /* Check whether gate check should be avoided.
2151 User controls the value of the gate through the parameter "gate_status". */
2152 gate_status = pass->has_gate ? pass->gate () : true;
2153 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2155 /* Override gate with plugin. */
2156 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2158 if (!gate_status)
2160 /* Run so passes selectively disabling themselves on a given function
2161 are not miscounted. */
2162 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2164 check_profile_consistency (pass->static_pass_number, 0, false);
2165 check_profile_consistency (pass->static_pass_number, 1, false);
2167 current_pass = NULL;
2168 return false;
2171 /* Pass execution event trigger: useful to identify passes being
2172 executed. */
2173 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2175 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2176 Apply all trnasforms first. */
2177 if (pass->type == SIMPLE_IPA_PASS)
2179 bool applied = false;
2180 do_per_function (apply_ipa_transforms, (void *)&applied);
2181 if (applied)
2182 symtab_remove_unreachable_nodes (true, dump_file);
2183 /* Restore current_pass. */
2184 current_pass = pass;
2187 if (!quiet_flag && !cfun)
2188 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2190 /* Note that the folders should only create gimple expressions.
2191 This is a hack until the new folder is ready. */
2192 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2194 pass_init_dump_file (pass);
2196 /* Run pre-pass verification. */
2197 execute_todo (pass->todo_flags_start);
2199 #ifdef ENABLE_CHECKING
2200 do_per_function (verify_curr_properties,
2201 (void *)(size_t)pass->properties_required);
2202 #endif
2204 /* If a timevar is present, start it. */
2205 if (pass->tv_id != TV_NONE)
2206 timevar_push (pass->tv_id);
2208 /* Do it! */
2209 if (pass->has_execute)
2211 todo_after = pass->execute ();
2212 do_per_function (clear_last_verified, NULL);
2215 /* Stop timevar. */
2216 if (pass->tv_id != TV_NONE)
2217 timevar_pop (pass->tv_id);
2219 do_per_function (update_properties_after_pass, pass);
2221 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2222 check_profile_consistency (pass->static_pass_number, 0, true);
2224 /* Run post-pass cleanup and verification. */
2225 execute_todo (todo_after | pass->todo_flags_finish);
2226 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2227 check_profile_consistency (pass->static_pass_number, 1, true);
2229 verify_interpass_invariants ();
2230 if (dump_file)
2231 do_per_function (execute_function_dump, NULL);
2232 if (pass->type == IPA_PASS)
2234 struct cgraph_node *node;
2235 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2236 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *)pass);
2239 if (!current_function_decl)
2240 cgraph_process_new_functions ();
2242 pass_fini_dump_file (pass);
2244 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2245 gcc_assert (!(cfun->curr_properties & PROP_trees)
2246 || pass->type != RTL_PASS);
2248 current_pass = NULL;
2250 /* Signal this is a suitable GC collection point. */
2251 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2252 ggc_collect ();
2254 return true;
2257 void
2258 execute_pass_list (struct opt_pass *pass)
2262 gcc_assert (pass->type == GIMPLE_PASS
2263 || pass->type == RTL_PASS);
2264 if (execute_one_pass (pass) && pass->sub)
2265 execute_pass_list (pass->sub);
2266 pass = pass->next;
2268 while (pass);
2271 /* Write out all LTO data. */
2272 static void
2273 write_lto (void)
2275 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2276 lto_output ();
2277 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2278 timevar_push (TV_IPA_LTO_DECL_OUT);
2279 produce_asm_for_decls ();
2280 timevar_pop (TV_IPA_LTO_DECL_OUT);
2283 /* Same as execute_pass_list but assume that subpasses of IPA passes
2284 are local passes. If SET is not NULL, write out summaries of only
2285 those node in SET. */
2287 static void
2288 ipa_write_summaries_2 (struct opt_pass *pass, struct lto_out_decl_state *state)
2290 while (pass)
2292 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2293 gcc_assert (!current_function_decl);
2294 gcc_assert (!cfun);
2295 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2296 if (pass->type == IPA_PASS
2297 && ipa_pass->write_summary
2298 && ((!pass->has_gate) || pass->gate ()))
2300 /* If a timevar is present, start it. */
2301 if (pass->tv_id)
2302 timevar_push (pass->tv_id);
2304 pass_init_dump_file (pass);
2306 ipa_pass->write_summary ();
2308 pass_fini_dump_file (pass);
2310 /* If a timevar is present, start it. */
2311 if (pass->tv_id)
2312 timevar_pop (pass->tv_id);
2315 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2316 ipa_write_summaries_2 (pass->sub, state);
2318 pass = pass->next;
2322 /* Helper function of ipa_write_summaries. Creates and destroys the
2323 decl state and calls ipa_write_summaries_2 for all passes that have
2324 summaries. SET is the set of nodes to be written. */
2326 static void
2327 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2329 pass_manager *passes = g->get_passes ();
2330 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2331 state->symtab_node_encoder = encoder;
2333 lto_push_out_decl_state (state);
2335 gcc_assert (!flag_wpa);
2336 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2338 write_lto ();
2340 gcc_assert (lto_get_out_decl_state () == state);
2341 lto_pop_out_decl_state ();
2342 lto_delete_out_decl_state (state);
2345 /* Write out summaries for all the nodes in the callgraph. */
2347 void
2348 ipa_write_summaries (void)
2350 lto_symtab_encoder_t encoder;
2351 int i, order_pos;
2352 struct varpool_node *vnode;
2353 struct cgraph_node *node;
2354 struct cgraph_node **order;
2356 if (!flag_generate_lto || seen_error ())
2357 return;
2359 encoder = lto_symtab_encoder_new (false);
2361 /* Create the callgraph set in the same order used in
2362 cgraph_expand_all_functions. This mostly facilitates debugging,
2363 since it causes the gimple file to be processed in the same order
2364 as the source code. */
2365 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2366 order_pos = ipa_reverse_postorder (order);
2367 gcc_assert (order_pos == cgraph_n_nodes);
2369 for (i = order_pos - 1; i >= 0; i--)
2371 struct cgraph_node *node = order[i];
2373 if (cgraph_function_with_gimple_body_p (node))
2375 /* When streaming out references to statements as part of some IPA
2376 pass summary, the statements need to have uids assigned and the
2377 following does that for all the IPA passes here. Naturally, this
2378 ordering then matches the one IPA-passes get in their stmt_fixup
2379 hooks. */
2381 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2382 renumber_gimple_stmt_uids ();
2383 pop_cfun ();
2385 if (node->definition)
2386 lto_set_symtab_encoder_in_partition (encoder, node);
2389 FOR_EACH_DEFINED_FUNCTION (node)
2390 if (node->alias)
2391 lto_set_symtab_encoder_in_partition (encoder, node);
2392 FOR_EACH_DEFINED_VARIABLE (vnode)
2393 lto_set_symtab_encoder_in_partition (encoder, vnode);
2395 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2397 free (order);
2400 /* Same as execute_pass_list but assume that subpasses of IPA passes
2401 are local passes. If SET is not NULL, write out optimization summaries of
2402 only those node in SET. */
2404 static void
2405 ipa_write_optimization_summaries_1 (struct opt_pass *pass, struct lto_out_decl_state *state)
2407 while (pass)
2409 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2410 gcc_assert (!current_function_decl);
2411 gcc_assert (!cfun);
2412 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2413 if (pass->type == IPA_PASS
2414 && ipa_pass->write_optimization_summary
2415 && ((!pass->has_gate) || pass->gate ()))
2417 /* If a timevar is present, start it. */
2418 if (pass->tv_id)
2419 timevar_push (pass->tv_id);
2421 pass_init_dump_file (pass);
2423 ipa_pass->write_optimization_summary ();
2425 pass_fini_dump_file (pass);
2427 /* If a timevar is present, start it. */
2428 if (pass->tv_id)
2429 timevar_pop (pass->tv_id);
2432 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2433 ipa_write_optimization_summaries_1 (pass->sub, state);
2435 pass = pass->next;
2439 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2440 NULL, write out all summaries of all nodes. */
2442 void
2443 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2445 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2446 lto_symtab_encoder_iterator lsei;
2447 state->symtab_node_encoder = encoder;
2449 lto_push_out_decl_state (state);
2450 for (lsei = lsei_start_function_in_partition (encoder);
2451 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2453 struct cgraph_node *node = lsei_cgraph_node (lsei);
2454 /* When streaming out references to statements as part of some IPA
2455 pass summary, the statements need to have uids assigned.
2457 For functions newly born at WPA stage we need to initialize
2458 the uids here. */
2459 if (node->definition
2460 && gimple_has_body_p (node->decl))
2462 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2463 renumber_gimple_stmt_uids ();
2464 pop_cfun ();
2468 gcc_assert (flag_wpa);
2469 pass_manager *passes = g->get_passes ();
2470 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2472 write_lto ();
2474 gcc_assert (lto_get_out_decl_state () == state);
2475 lto_pop_out_decl_state ();
2476 lto_delete_out_decl_state (state);
2479 /* Same as execute_pass_list but assume that subpasses of IPA passes
2480 are local passes. */
2482 static void
2483 ipa_read_summaries_1 (struct opt_pass *pass)
2485 while (pass)
2487 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2489 gcc_assert (!current_function_decl);
2490 gcc_assert (!cfun);
2491 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2493 if ((!pass->has_gate) || pass->gate ())
2495 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2497 /* If a timevar is present, start it. */
2498 if (pass->tv_id)
2499 timevar_push (pass->tv_id);
2501 pass_init_dump_file (pass);
2503 ipa_pass->read_summary ();
2505 pass_fini_dump_file (pass);
2507 /* Stop timevar. */
2508 if (pass->tv_id)
2509 timevar_pop (pass->tv_id);
2512 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2513 ipa_read_summaries_1 (pass->sub);
2515 pass = pass->next;
2520 /* Read all the summaries for all_regular_ipa_passes. */
2522 void
2523 ipa_read_summaries (void)
2525 pass_manager *passes = g->get_passes ();
2526 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2529 /* Same as execute_pass_list but assume that subpasses of IPA passes
2530 are local passes. */
2532 static void
2533 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2535 while (pass)
2537 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2539 gcc_assert (!current_function_decl);
2540 gcc_assert (!cfun);
2541 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2543 if ((!pass->has_gate) || pass->gate ())
2545 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2547 /* If a timevar is present, start it. */
2548 if (pass->tv_id)
2549 timevar_push (pass->tv_id);
2551 pass_init_dump_file (pass);
2553 ipa_pass->read_optimization_summary ();
2555 pass_fini_dump_file (pass);
2557 /* Stop timevar. */
2558 if (pass->tv_id)
2559 timevar_pop (pass->tv_id);
2562 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2563 ipa_read_optimization_summaries_1 (pass->sub);
2565 pass = pass->next;
2569 /* Read all the summaries for all_regular_ipa_passes. */
2571 void
2572 ipa_read_optimization_summaries (void)
2574 pass_manager *passes = g->get_passes ();
2575 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2578 /* Same as execute_pass_list but assume that subpasses of IPA passes
2579 are local passes. */
2580 void
2581 execute_ipa_pass_list (struct opt_pass *pass)
2585 gcc_assert (!current_function_decl);
2586 gcc_assert (!cfun);
2587 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2588 if (execute_one_pass (pass) && pass->sub)
2590 if (pass->sub->type == GIMPLE_PASS)
2592 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2593 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2594 pass->sub);
2595 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2597 else if (pass->sub->type == SIMPLE_IPA_PASS
2598 || pass->sub->type == IPA_PASS)
2599 execute_ipa_pass_list (pass->sub);
2600 else
2601 gcc_unreachable ();
2603 gcc_assert (!current_function_decl);
2604 cgraph_process_new_functions ();
2605 pass = pass->next;
2607 while (pass);
2610 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2612 static void
2613 execute_ipa_stmt_fixups (struct opt_pass *pass,
2614 struct cgraph_node *node, gimple *stmts)
2616 while (pass)
2618 /* Execute all of the IPA_PASSes in the list. */
2619 if (pass->type == IPA_PASS
2620 && ((!pass->has_gate) || pass->gate ()))
2622 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2624 if (ipa_pass->stmt_fixup)
2626 pass_init_dump_file (pass);
2627 /* If a timevar is present, start it. */
2628 if (pass->tv_id)
2629 timevar_push (pass->tv_id);
2631 ipa_pass->stmt_fixup (node, stmts);
2633 /* Stop timevar. */
2634 if (pass->tv_id)
2635 timevar_pop (pass->tv_id);
2636 pass_fini_dump_file (pass);
2638 if (pass->sub)
2639 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2641 pass = pass->next;
2645 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2647 void
2648 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2650 pass_manager *passes = g->get_passes ();
2651 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2655 extern void debug_properties (unsigned int);
2656 extern void dump_properties (FILE *, unsigned int);
2658 DEBUG_FUNCTION void
2659 dump_properties (FILE *dump, unsigned int props)
2661 fprintf (dump, "Properties:\n");
2662 if (props & PROP_gimple_any)
2663 fprintf (dump, "PROP_gimple_any\n");
2664 if (props & PROP_gimple_lcf)
2665 fprintf (dump, "PROP_gimple_lcf\n");
2666 if (props & PROP_gimple_leh)
2667 fprintf (dump, "PROP_gimple_leh\n");
2668 if (props & PROP_cfg)
2669 fprintf (dump, "PROP_cfg\n");
2670 if (props & PROP_ssa)
2671 fprintf (dump, "PROP_ssa\n");
2672 if (props & PROP_no_crit_edges)
2673 fprintf (dump, "PROP_no_crit_edges\n");
2674 if (props & PROP_rtl)
2675 fprintf (dump, "PROP_rtl\n");
2676 if (props & PROP_gimple_lomp)
2677 fprintf (dump, "PROP_gimple_lomp\n");
2678 if (props & PROP_gimple_lcx)
2679 fprintf (dump, "PROP_gimple_lcx\n");
2680 if (props & PROP_gimple_lvec)
2681 fprintf (dump, "PROP_gimple_lvec\n");
2682 if (props & PROP_cfglayout)
2683 fprintf (dump, "PROP_cfglayout\n");
2686 DEBUG_FUNCTION void
2687 debug_properties (unsigned int props)
2689 dump_properties (stderr, props);
2692 /* Called by local passes to see if function is called by already processed nodes.
2693 Because we process nodes in topological order, this means that function is
2694 in recursive cycle or we introduced new direct calls. */
2695 bool
2696 function_called_by_processed_nodes_p (void)
2698 struct cgraph_edge *e;
2699 for (e = cgraph_get_node (current_function_decl)->callers;
2701 e = e->next_caller)
2703 if (e->caller->decl == current_function_decl)
2704 continue;
2705 if (!cgraph_function_with_gimple_body_p (e->caller))
2706 continue;
2707 if (TREE_ASM_WRITTEN (e->caller->decl))
2708 continue;
2709 if (!e->caller->process && !e->caller->global.inlined_to)
2710 break;
2712 if (dump_file && e)
2714 fprintf (dump_file, "Already processed call to:\n");
2715 dump_cgraph_node (dump_file, e->caller);
2717 return e != NULL;
2720 #include "gt-passes.h"