PR sanitizer/59009
[official-gcc.git] / gcc / passes.c
blobf45ed0aed1c6bbeca7737bac4b7a063088565e71
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_lto_gen_passes, 1);
911 dump_pass_list (all_late_ipa_passes, 1);
912 dump_pass_list (all_passes, 1);
914 pop_cfun ();
918 /* Returns the pass with NAME. */
920 static struct opt_pass *
921 get_pass_by_name (const char *name)
923 struct pass_registry **slot, pr;
925 pr.unique_name = name;
926 slot = name_to_pass_map.find_slot (&pr, NO_INSERT);
928 if (!slot || !*slot)
929 return NULL;
931 return (*slot)->pass;
935 /* Range [start, last]. */
937 struct uid_range
939 unsigned int start;
940 unsigned int last;
941 const char *assem_name;
942 struct uid_range *next;
945 typedef struct uid_range *uid_range_p;
948 static vec<uid_range_p>
949 enabled_pass_uid_range_tab = vNULL;
950 static vec<uid_range_p>
951 disabled_pass_uid_range_tab = vNULL;
954 /* Parse option string for -fdisable- and -fenable-
955 The syntax of the options:
957 -fenable-<pass_name>
958 -fdisable-<pass_name>
960 -fenable-<pass_name>=s1:e1,s2:e2,...
961 -fdisable-<pass_name>=s1:e1,s2:e2,...
964 static void
965 enable_disable_pass (const char *arg, bool is_enable)
967 struct opt_pass *pass;
968 char *range_str, *phase_name;
969 char *argstr = xstrdup (arg);
970 vec<uid_range_p> *tab = 0;
972 range_str = strchr (argstr,'=');
973 if (range_str)
975 *range_str = '\0';
976 range_str++;
979 phase_name = argstr;
980 if (!*phase_name)
982 if (is_enable)
983 error ("unrecognized option -fenable");
984 else
985 error ("unrecognized option -fdisable");
986 free (argstr);
987 return;
989 pass = get_pass_by_name (phase_name);
990 if (!pass || pass->static_pass_number == -1)
992 if (is_enable)
993 error ("unknown pass %s specified in -fenable", phase_name);
994 else
995 error ("unknown pass %s specified in -fdisable", phase_name);
996 free (argstr);
997 return;
1000 if (is_enable)
1001 tab = &enabled_pass_uid_range_tab;
1002 else
1003 tab = &disabled_pass_uid_range_tab;
1005 if ((unsigned) pass->static_pass_number >= tab->length ())
1006 tab->safe_grow_cleared (pass->static_pass_number + 1);
1008 if (!range_str)
1010 uid_range_p slot;
1011 uid_range_p new_range = XCNEW (struct uid_range);
1013 new_range->start = 0;
1014 new_range->last = (unsigned)-1;
1016 slot = (*tab)[pass->static_pass_number];
1017 new_range->next = slot;
1018 (*tab)[pass->static_pass_number] = new_range;
1019 if (is_enable)
1020 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1021 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1022 else
1023 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1024 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1026 else
1028 char *next_range = NULL;
1029 char *one_range = range_str;
1030 char *end_val = NULL;
1034 uid_range_p slot;
1035 uid_range_p new_range;
1036 char *invalid = NULL;
1037 long start;
1038 char *func_name = NULL;
1040 next_range = strchr (one_range, ',');
1041 if (next_range)
1043 *next_range = '\0';
1044 next_range++;
1047 end_val = strchr (one_range, ':');
1048 if (end_val)
1050 *end_val = '\0';
1051 end_val++;
1053 start = strtol (one_range, &invalid, 10);
1054 if (*invalid || start < 0)
1056 if (end_val || (one_range[0] >= '0'
1057 && one_range[0] <= '9'))
1059 error ("Invalid range %s in option %s",
1060 one_range,
1061 is_enable ? "-fenable" : "-fdisable");
1062 free (argstr);
1063 return;
1065 func_name = one_range;
1067 if (!end_val)
1069 new_range = XCNEW (struct uid_range);
1070 if (!func_name)
1072 new_range->start = (unsigned) start;
1073 new_range->last = (unsigned) start;
1075 else
1077 new_range->start = (unsigned) -1;
1078 new_range->last = (unsigned) -1;
1079 new_range->assem_name = xstrdup (func_name);
1082 else
1084 long last = strtol (end_val, &invalid, 10);
1085 if (*invalid || last < start)
1087 error ("Invalid range %s in option %s",
1088 end_val,
1089 is_enable ? "-fenable" : "-fdisable");
1090 free (argstr);
1091 return;
1093 new_range = XCNEW (struct uid_range);
1094 new_range->start = (unsigned) start;
1095 new_range->last = (unsigned) last;
1098 slot = (*tab)[pass->static_pass_number];
1099 new_range->next = slot;
1100 (*tab)[pass->static_pass_number] = new_range;
1101 if (is_enable)
1103 if (new_range->assem_name)
1104 inform (UNKNOWN_LOCATION,
1105 "enable pass %s for function %s",
1106 phase_name, new_range->assem_name);
1107 else
1108 inform (UNKNOWN_LOCATION,
1109 "enable pass %s for functions in the range of [%u, %u]",
1110 phase_name, new_range->start, new_range->last);
1112 else
1114 if (new_range->assem_name)
1115 inform (UNKNOWN_LOCATION,
1116 "disable pass %s for function %s",
1117 phase_name, new_range->assem_name);
1118 else
1119 inform (UNKNOWN_LOCATION,
1120 "disable pass %s for functions in the range of [%u, %u]",
1121 phase_name, new_range->start, new_range->last);
1124 one_range = next_range;
1125 } while (next_range);
1128 free (argstr);
1131 /* Enable pass specified by ARG. */
1133 void
1134 enable_pass (const char *arg)
1136 enable_disable_pass (arg, true);
1139 /* Disable pass specified by ARG. */
1141 void
1142 disable_pass (const char *arg)
1144 enable_disable_pass (arg, false);
1147 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1149 static bool
1150 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
1151 tree func,
1152 vec<uid_range_p> tab)
1154 uid_range_p slot, range;
1155 int cgraph_uid;
1156 const char *aname = NULL;
1158 if (!tab.exists ()
1159 || (unsigned) pass->static_pass_number >= tab.length ()
1160 || pass->static_pass_number == -1)
1161 return false;
1163 slot = tab[pass->static_pass_number];
1164 if (!slot)
1165 return false;
1167 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
1168 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1169 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1171 range = slot;
1172 while (range)
1174 if ((unsigned) cgraph_uid >= range->start
1175 && (unsigned) cgraph_uid <= range->last)
1176 return true;
1177 if (range->assem_name && aname
1178 && !strcmp (range->assem_name, aname))
1179 return true;
1180 range = range->next;
1183 return false;
1187 /* Update static_pass_number for passes (and the flag
1188 TODO_mark_first_instance).
1190 Passes are constructed with static_pass_number preinitialized to 0
1192 This field is used in two different ways: initially as instance numbers
1193 of their kind, and then as ids within the entire pass manager.
1195 Within pass_manager::pass_manager:
1197 * In add_pass_instance(), as called by next_pass_1 in
1198 NEXT_PASS in init_optimization_passes
1200 * When the initial instance of a pass within a pass manager is seen,
1201 it is flagged, and its static_pass_number is set to -1
1203 * On subsequent times that it is seen, the static pass number
1204 is decremented each time, so that if there are e.g. 4 dups,
1205 they have static_pass_number -4, 2, 3, 4 respectively (note
1206 how the initial one is negative and gives the count); these
1207 can be thought of as instance numbers of the specific pass
1209 * Within the register_dump_files () traversal, set_pass_for_id()
1210 is called on each pass, using these instance numbers to create
1211 dumpfile switches, and then overwriting them with a pass id,
1212 which are global to the whole pass manager (based on
1213 (TDI_end + current value of extra_dump_files_in_use) ) */
1215 static void
1216 add_pass_instance (struct opt_pass *new_pass, bool track_duplicates,
1217 opt_pass *initial_pass)
1219 /* Are we dealing with the first pass of its kind, or a clone? */
1220 if (new_pass != initial_pass)
1222 /* We're dealing with a clone. */
1223 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1225 /* Indicate to register_dump_files that this pass has duplicates,
1226 and so it should rename the dump file. The first instance will
1227 be -1, and be number of duplicates = -static_pass_number - 1.
1228 Subsequent instances will be > 0 and just the duplicate number. */
1229 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1231 initial_pass->static_pass_number -= 1;
1232 new_pass->static_pass_number = -initial_pass->static_pass_number;
1235 else
1237 /* We're dealing with the first pass of its kind. */
1238 new_pass->todo_flags_start |= TODO_mark_first_instance;
1239 new_pass->static_pass_number = -1;
1241 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1245 /* Add a pass to the pass list. Duplicate the pass if it's already
1246 in the list. */
1248 static struct opt_pass **
1249 next_pass_1 (struct opt_pass **list, struct opt_pass *pass,
1250 struct opt_pass *initial_pass)
1252 /* Every pass should have a name so that plugins can refer to them. */
1253 gcc_assert (pass->name != NULL);
1255 add_pass_instance (pass, false, initial_pass);
1256 *list = pass;
1258 return &(*list)->next;
1261 /* List node for an inserted pass instance. We need to keep track of all
1262 the newly-added pass instances (with 'added_pass_nodes' defined below)
1263 so that we can register their dump files after pass-positioning is finished.
1264 Registering dumping files needs to be post-processed or the
1265 static_pass_number of the opt_pass object would be modified and mess up
1266 the dump file names of future pass instances to be added. */
1268 struct pass_list_node
1270 struct opt_pass *pass;
1271 struct pass_list_node *next;
1274 static struct pass_list_node *added_pass_nodes = NULL;
1275 static struct pass_list_node *prev_added_pass_node;
1277 /* Insert the pass at the proper position. Return true if the pass
1278 is successfully added.
1280 NEW_PASS_INFO - new pass to be inserted
1281 PASS_LIST - root of the pass list to insert the new pass to */
1283 static bool
1284 position_pass (struct register_pass_info *new_pass_info,
1285 struct opt_pass **pass_list)
1287 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1288 bool success = false;
1290 for ( ; pass; prev_pass = pass, pass = pass->next)
1292 /* Check if the current pass is of the same type as the new pass and
1293 matches the name and the instance number of the reference pass. */
1294 if (pass->type == new_pass_info->pass->type
1295 && pass->name
1296 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1297 && ((new_pass_info->ref_pass_instance_number == 0)
1298 || (new_pass_info->ref_pass_instance_number ==
1299 pass->static_pass_number)
1300 || (new_pass_info->ref_pass_instance_number == 1
1301 && pass->todo_flags_start & TODO_mark_first_instance)))
1303 struct opt_pass *new_pass;
1304 struct pass_list_node *new_pass_node;
1306 if (new_pass_info->ref_pass_instance_number == 0)
1308 new_pass = new_pass_info->pass->clone ();
1309 add_pass_instance (new_pass, true, new_pass_info->pass);
1311 else
1313 new_pass = new_pass_info->pass;
1314 add_pass_instance (new_pass, true, new_pass);
1317 /* Insert the new pass instance based on the positioning op. */
1318 switch (new_pass_info->pos_op)
1320 case PASS_POS_INSERT_AFTER:
1321 new_pass->next = pass->next;
1322 pass->next = new_pass;
1324 /* Skip newly inserted pass to avoid repeated
1325 insertions in the case where the new pass and the
1326 existing one have the same name. */
1327 pass = new_pass;
1328 break;
1329 case PASS_POS_INSERT_BEFORE:
1330 new_pass->next = pass;
1331 if (prev_pass)
1332 prev_pass->next = new_pass;
1333 else
1334 *pass_list = new_pass;
1335 break;
1336 case PASS_POS_REPLACE:
1337 new_pass->next = pass->next;
1338 if (prev_pass)
1339 prev_pass->next = new_pass;
1340 else
1341 *pass_list = new_pass;
1342 new_pass->sub = pass->sub;
1343 new_pass->tv_id = pass->tv_id;
1344 pass = new_pass;
1345 break;
1346 default:
1347 error ("invalid pass positioning operation");
1348 return false;
1351 /* Save the newly added pass (instance) in the added_pass_nodes
1352 list so that we can register its dump file later. Note that
1353 we cannot register the dump file now because doing so will modify
1354 the static_pass_number of the opt_pass object and therefore
1355 mess up the dump file name of future instances. */
1356 new_pass_node = XCNEW (struct pass_list_node);
1357 new_pass_node->pass = new_pass;
1358 if (!added_pass_nodes)
1359 added_pass_nodes = new_pass_node;
1360 else
1361 prev_added_pass_node->next = new_pass_node;
1362 prev_added_pass_node = new_pass_node;
1364 success = true;
1367 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1368 success = true;
1371 return success;
1374 /* Hooks a new pass into the pass lists.
1376 PASS_INFO - pass information that specifies the opt_pass object,
1377 reference pass, instance number, and how to position
1378 the pass */
1380 void
1381 register_pass (struct register_pass_info *pass_info)
1383 g->get_passes ()->register_pass (pass_info);
1386 void
1387 register_pass (opt_pass* pass, pass_positioning_ops pos,
1388 const char* ref_pass_name, int ref_pass_inst_number)
1390 register_pass_info i;
1391 i.pass = pass;
1392 i.reference_pass_name = ref_pass_name;
1393 i.ref_pass_instance_number = ref_pass_inst_number;
1394 i.pos_op = pos;
1396 g->get_passes ()->register_pass (&i);
1399 void
1400 pass_manager::register_pass (struct register_pass_info *pass_info)
1402 bool all_instances, success;
1403 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1405 /* The checks below could fail in buggy plugins. Existing GCC
1406 passes should never fail these checks, so we mention plugin in
1407 the messages. */
1408 if (!pass_info->pass)
1409 fatal_error ("plugin cannot register a missing pass");
1411 if (!pass_info->pass->name)
1412 fatal_error ("plugin cannot register an unnamed pass");
1414 if (!pass_info->reference_pass_name)
1415 fatal_error
1416 ("plugin cannot register pass %qs without reference pass name",
1417 pass_info->pass->name);
1419 /* Try to insert the new pass to the pass lists. We need to check
1420 all five lists as the reference pass could be in one (or all) of
1421 them. */
1422 all_instances = pass_info->ref_pass_instance_number == 0;
1423 success = position_pass (pass_info, &all_lowering_passes);
1424 if (!success || all_instances)
1425 success |= position_pass (pass_info, &all_small_ipa_passes);
1426 if (!success || all_instances)
1427 success |= position_pass (pass_info, &all_regular_ipa_passes);
1428 if (!success || all_instances)
1429 success |= position_pass (pass_info, &all_lto_gen_passes);
1430 if (!success || all_instances)
1431 success |= position_pass (pass_info, &all_late_ipa_passes);
1432 if (!success || all_instances)
1433 success |= position_pass (pass_info, &all_passes);
1434 if (!success)
1435 fatal_error
1436 ("pass %qs not found but is referenced by new pass %qs",
1437 pass_info->reference_pass_name, pass_info->pass->name);
1439 /* OK, we have successfully inserted the new pass. We need to register
1440 the dump files for the newly added pass and its duplicates (if any).
1441 Because the registration of plugin/backend passes happens after the
1442 command-line options are parsed, the options that specify single
1443 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1444 passes. Therefore we currently can only enable dumping of
1445 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1446 are specified. While doing so, we also delete the pass_list_node
1447 objects created during pass positioning. */
1448 while (added_pass_nodes)
1450 struct pass_list_node *next_node = added_pass_nodes->next;
1451 enum tree_dump_index tdi;
1452 register_one_dump_file (added_pass_nodes->pass);
1453 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1454 || added_pass_nodes->pass->type == IPA_PASS)
1455 tdi = TDI_ipa_all;
1456 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1457 tdi = TDI_tree_all;
1458 else
1459 tdi = TDI_rtl_all;
1460 /* Check if dump-all flag is specified. */
1461 if (dumps->get_dump_file_info (tdi)->pstate)
1462 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1463 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1464 XDELETE (added_pass_nodes);
1465 added_pass_nodes = next_node;
1469 /* Construct the pass tree. The sequencing of passes is driven by
1470 the cgraph routines:
1472 finalize_compilation_unit ()
1473 for each node N in the cgraph
1474 cgraph_analyze_function (N)
1475 cgraph_lower_function (N) -> all_lowering_passes
1477 If we are optimizing, compile is then invoked:
1479 compile ()
1480 ipa_passes () -> all_small_ipa_passes
1481 -> Analysis of all_regular_ipa_passes
1482 * possible LTO streaming at copmilation time *
1483 -> Execution of all_regular_ipa_passes
1484 * possible LTO streaming at link time *
1485 -> all_late_ipa_passes
1486 expand_all_functions ()
1487 for each node N in the cgraph
1488 expand_function (N) -> Transformation of all_regular_ipa_passes
1489 -> all_passes
1492 void *
1493 pass_manager::operator new (size_t sz)
1495 /* Ensure that all fields of the pass manager are zero-initialized. */
1496 return xcalloc (1, sz);
1499 pass_manager::pass_manager (context *ctxt)
1500 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1501 all_regular_ipa_passes (NULL), all_lto_gen_passes (NULL),
1502 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1503 m_ctxt (ctxt)
1505 struct opt_pass **p;
1507 /* Initialize the pass_lists array. */
1508 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1509 GCC_PASS_LISTS
1510 #undef DEF_PASS_LIST
1512 /* Build the tree of passes. */
1514 #define INSERT_PASSES_AFTER(PASS) \
1515 p = &(PASS);
1517 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1519 struct opt_pass **p = &(PASS ## _1)->sub;
1521 #define POP_INSERT_PASSES() \
1524 #define NEXT_PASS(PASS, NUM) \
1525 do { \
1526 gcc_assert (NULL == PASS ## _ ## NUM); \
1527 if ((NUM) == 1) \
1528 PASS ## _1 = make_##PASS (m_ctxt); \
1529 else \
1531 gcc_assert (PASS ## _1); \
1532 PASS ## _ ## NUM = PASS ## _1->clone (); \
1534 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1535 } while (0)
1537 #define TERMINATE_PASS_LIST() \
1538 *p = NULL;
1540 #include "pass-instances.def"
1542 #undef INSERT_PASSES_AFTER
1543 #undef PUSH_INSERT_PASSES_WITHIN
1544 #undef POP_INSERT_PASSES
1545 #undef NEXT_PASS
1546 #undef TERMINATE_PASS_LIST
1548 /* Register the passes with the tree dump code. */
1549 register_dump_files (all_lowering_passes, PROP_gimple_any);
1550 register_dump_files (all_small_ipa_passes,
1551 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1552 | PROP_cfg);
1553 register_dump_files (all_regular_ipa_passes,
1554 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1555 | PROP_cfg);
1556 register_dump_files (all_lto_gen_passes,
1557 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1558 | PROP_cfg);
1559 register_dump_files (all_late_ipa_passes,
1560 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1561 | PROP_cfg);
1562 register_dump_files (all_passes,
1563 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1564 | PROP_cfg);
1567 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1568 function CALLBACK for every function in the call graph. Otherwise,
1569 call CALLBACK on the current function. */
1571 static void
1572 do_per_function (void (*callback) (void *data), void *data)
1574 if (current_function_decl)
1575 callback (data);
1576 else
1578 struct cgraph_node *node;
1579 FOR_EACH_DEFINED_FUNCTION (node)
1580 if (node->analyzed && gimple_has_body_p (node->decl)
1581 && (!node->clone_of || node->decl != node->clone_of->decl))
1583 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1584 callback (data);
1585 if (!flag_wpa)
1587 free_dominance_info (CDI_DOMINATORS);
1588 free_dominance_info (CDI_POST_DOMINATORS);
1590 pop_cfun ();
1591 ggc_collect ();
1596 /* Because inlining might remove no-longer reachable nodes, we need to
1597 keep the array visible to garbage collector to avoid reading collected
1598 out nodes. */
1599 static int nnodes;
1600 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1602 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1603 function CALLBACK for every function in the call graph. Otherwise,
1604 call CALLBACK on the current function.
1605 This function is global so that plugins can use it. */
1606 void
1607 do_per_function_toporder (void (*callback) (void *data), void *data)
1609 int i;
1611 if (current_function_decl)
1612 callback (data);
1613 else
1615 gcc_assert (!order);
1616 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1617 nnodes = ipa_reverse_postorder (order);
1618 for (i = nnodes - 1; i >= 0; i--)
1619 order[i]->process = 1;
1620 for (i = nnodes - 1; i >= 0; i--)
1622 struct cgraph_node *node = order[i];
1624 /* Allow possibly removed nodes to be garbage collected. */
1625 order[i] = NULL;
1626 node->process = 0;
1627 if (cgraph_function_with_gimple_body_p (node))
1629 cgraph_get_body (node);
1630 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1631 callback (data);
1632 free_dominance_info (CDI_DOMINATORS);
1633 free_dominance_info (CDI_POST_DOMINATORS);
1634 pop_cfun ();
1635 ggc_collect ();
1639 ggc_free (order);
1640 order = NULL;
1641 nnodes = 0;
1644 /* Helper function to perform function body dump. */
1646 static void
1647 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1649 if (dump_file && current_function_decl)
1651 if (cfun->curr_properties & PROP_trees)
1652 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1653 else
1654 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1656 /* Flush the file. If verification fails, we won't be able to
1657 close the file before aborting. */
1658 fflush (dump_file);
1660 if ((cfun->curr_properties & PROP_cfg)
1661 && (dump_flags & TDF_GRAPH))
1662 print_graph_cfg (dump_file_name, cfun);
1666 static struct profile_record *profile_record;
1668 /* Do profile consistency book-keeping for the pass with static number INDEX.
1669 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1670 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1671 if we are only book-keeping on passes that may have selectively disabled
1672 themselves on a given function. */
1673 static void
1674 check_profile_consistency (int index, int subpass, bool run)
1676 pass_manager *passes = g->get_passes ();
1677 if (index == -1)
1678 return;
1679 if (!profile_record)
1680 profile_record = XCNEWVEC (struct profile_record,
1681 passes->passes_by_id_size);
1682 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1683 gcc_assert (subpass < 2);
1684 profile_record[index].run |= run;
1685 account_profile_record (&profile_record[index], subpass);
1688 /* Output profile consistency. */
1690 void
1691 dump_profile_report (void)
1693 g->get_passes ()->dump_profile_report ();
1696 void
1697 pass_manager::dump_profile_report () const
1699 int i, j;
1700 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1701 gcov_type last_time = 0, last_size = 0;
1702 double rel_time_change, rel_size_change;
1703 int last_reported = 0;
1705 if (!profile_record)
1706 return;
1707 fprintf (stderr, "\nProfile consistency report:\n\n");
1708 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1709 fprintf (stderr, " |freq count |freq count |size time\n");
1711 for (i = 0; i < passes_by_id_size; i++)
1712 for (j = 0 ; j < 2; j++)
1713 if (profile_record[i].run)
1715 if (last_time)
1716 rel_time_change = (profile_record[i].time[j]
1717 - (double)last_time) * 100 / (double)last_time;
1718 else
1719 rel_time_change = 0;
1720 if (last_size)
1721 rel_size_change = (profile_record[i].size[j]
1722 - (double)last_size) * 100 / (double)last_size;
1723 else
1724 rel_size_change = 0;
1726 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1727 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1728 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1729 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1730 || rel_time_change || rel_size_change)
1732 last_reported = i;
1733 fprintf (stderr, "%-20s %s",
1734 passes_by_id [i]->name,
1735 j ? "(after TODO)" : " ");
1736 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1737 fprintf (stderr, "| %+5i",
1738 profile_record[i].num_mismatched_freq_in[j]
1739 - last_freq_in);
1740 else
1741 fprintf (stderr, "| ");
1742 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1743 fprintf (stderr, " %+5i",
1744 profile_record[i].num_mismatched_count_in[j]
1745 - last_count_in);
1746 else
1747 fprintf (stderr, " ");
1748 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1749 fprintf (stderr, "| %+5i",
1750 profile_record[i].num_mismatched_freq_out[j]
1751 - last_freq_out);
1752 else
1753 fprintf (stderr, "| ");
1754 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1755 fprintf (stderr, " %+5i",
1756 profile_record[i].num_mismatched_count_out[j]
1757 - last_count_out);
1758 else
1759 fprintf (stderr, " ");
1761 /* Size/time units change across gimple and RTL. */
1762 if (i == pass_expand_1->static_pass_number)
1763 fprintf (stderr, "|----------");
1764 else
1766 if (rel_size_change)
1767 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1768 else
1769 fprintf (stderr, "| ");
1770 if (rel_time_change)
1771 fprintf (stderr, " %+8.4f%%", rel_time_change);
1773 fprintf (stderr, "\n");
1774 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1775 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1776 last_count_in = profile_record[i].num_mismatched_count_in[j];
1777 last_count_out = profile_record[i].num_mismatched_count_out[j];
1779 else if (j && last_reported != i)
1781 last_reported = i;
1782 fprintf (stderr, "%-20s ------------| | |\n",
1783 passes_by_id [i]->name);
1785 last_time = profile_record[i].time[j];
1786 last_size = profile_record[i].size[j];
1790 /* Perform all TODO actions that ought to be done on each function. */
1792 static void
1793 execute_function_todo (void *data)
1795 unsigned int flags = (size_t)data;
1796 flags &= ~cfun->last_verified;
1797 if (!flags)
1798 return;
1800 /* Always cleanup the CFG before trying to update SSA. */
1801 if (flags & TODO_cleanup_cfg)
1803 cleanup_tree_cfg ();
1805 /* When cleanup_tree_cfg merges consecutive blocks, it may
1806 perform some simplistic propagation when removing single
1807 valued PHI nodes. This propagation may, in turn, cause the
1808 SSA form to become out-of-date (see PR 22037). So, even
1809 if the parent pass had not scheduled an SSA update, we may
1810 still need to do one. */
1811 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1812 flags |= TODO_update_ssa;
1815 if (flags & TODO_update_ssa_any)
1817 unsigned update_flags = flags & TODO_update_ssa_any;
1818 update_ssa (update_flags);
1819 cfun->last_verified &= ~TODO_verify_ssa;
1822 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1823 compute_may_aliases ();
1825 if (optimize && (flags & TODO_update_address_taken))
1826 execute_update_addresses_taken ();
1828 if (flags & TODO_remove_unused_locals)
1829 remove_unused_locals ();
1831 if (flags & TODO_rebuild_frequencies)
1832 rebuild_frequencies ();
1834 if (flags & TODO_rebuild_cgraph_edges)
1835 rebuild_cgraph_edges ();
1837 /* If we've seen errors do not bother running any verifiers. */
1838 if (seen_error ())
1839 return;
1841 #if defined ENABLE_CHECKING
1842 if (flags & TODO_verify_ssa
1843 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1845 verify_gimple_in_cfg (cfun);
1846 verify_ssa (true);
1848 else if (flags & TODO_verify_stmts)
1849 verify_gimple_in_cfg (cfun);
1850 if (flags & TODO_verify_flow)
1851 verify_flow_info ();
1852 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1853 verify_loop_closed_ssa (false);
1854 if (flags & TODO_verify_rtl_sharing)
1855 verify_rtl_sharing ();
1856 #endif
1858 cfun->last_verified = flags & TODO_verify_all;
1861 /* Perform all TODO actions. */
1862 static void
1863 execute_todo (unsigned int flags)
1865 #if defined ENABLE_CHECKING
1866 if (cfun
1867 && need_ssa_update_p (cfun))
1868 gcc_assert (flags & TODO_update_ssa_any);
1869 #endif
1871 timevar_push (TV_TODO);
1873 /* Inform the pass whether it is the first time it is run. */
1874 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1876 statistics_fini_pass ();
1878 if (flags)
1879 do_per_function (execute_function_todo, (void *)(size_t) flags);
1881 /* Always remove functions just as before inlining: IPA passes might be
1882 interested to see bodies of extern inline functions that are not inlined
1883 to analyze side effects. The full removal is done just at the end
1884 of IPA pass queue. */
1885 if (flags & TODO_remove_functions)
1887 gcc_assert (!cfun);
1888 symtab_remove_unreachable_nodes (true, dump_file);
1891 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1893 gcc_assert (!cfun);
1894 dump_symtab (dump_file);
1895 /* Flush the file. If verification fails, we won't be able to
1896 close the file before aborting. */
1897 fflush (dump_file);
1900 /* Now that the dumping has been done, we can get rid of the optional
1901 df problems. */
1902 if (flags & TODO_df_finish)
1903 df_finish_pass ((flags & TODO_df_verify) != 0);
1905 timevar_pop (TV_TODO);
1908 /* Verify invariants that should hold between passes. This is a place
1909 to put simple sanity checks. */
1911 static void
1912 verify_interpass_invariants (void)
1914 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1917 /* Clear the last verified flag. */
1919 static void
1920 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1922 cfun->last_verified = 0;
1925 /* Helper function. Verify that the properties has been turn into the
1926 properties expected by the pass. */
1928 #ifdef ENABLE_CHECKING
1929 static void
1930 verify_curr_properties (void *data)
1932 unsigned int props = (size_t)data;
1933 gcc_assert ((cfun->curr_properties & props) == props);
1935 #endif
1937 /* Initialize pass dump file. */
1938 /* This is non-static so that the plugins can use it. */
1940 bool
1941 pass_init_dump_file (struct opt_pass *pass)
1943 /* If a dump file name is present, open it if enabled. */
1944 if (pass->static_pass_number != -1)
1946 timevar_push (TV_DUMP);
1947 gcc::dump_manager *dumps = g->get_dumps ();
1948 bool initializing_dump =
1949 !dumps->dump_initialized_p (pass->static_pass_number);
1950 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
1951 dumps->dump_start (pass->static_pass_number, &dump_flags);
1952 if (dump_file && current_function_decl)
1953 dump_function_header (dump_file, current_function_decl, dump_flags);
1954 if (initializing_dump
1955 && dump_file && (dump_flags & TDF_GRAPH)
1956 && cfun && (cfun->curr_properties & PROP_cfg))
1957 clean_graph_dump_file (dump_file_name);
1958 timevar_pop (TV_DUMP);
1959 return initializing_dump;
1961 else
1962 return false;
1965 /* Flush PASS dump file. */
1966 /* This is non-static so that plugins can use it. */
1968 void
1969 pass_fini_dump_file (struct opt_pass *pass)
1971 timevar_push (TV_DUMP);
1973 /* Flush and close dump file. */
1974 if (dump_file_name)
1976 free (CONST_CAST (char *, dump_file_name));
1977 dump_file_name = NULL;
1980 g->get_dumps ()->dump_finish (pass->static_pass_number);
1981 timevar_pop (TV_DUMP);
1984 /* After executing the pass, apply expected changes to the function
1985 properties. */
1987 static void
1988 update_properties_after_pass (void *data)
1990 struct opt_pass *pass = (struct opt_pass *) data;
1991 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1992 & ~pass->properties_destroyed;
1995 /* Execute summary generation for all of the passes in IPA_PASS. */
1997 void
1998 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
2000 while (ipa_pass)
2002 struct opt_pass *pass = ipa_pass;
2004 /* Execute all of the IPA_PASSes in the list. */
2005 if (ipa_pass->type == IPA_PASS
2006 && ((!pass->has_gate) || pass->gate ())
2007 && ipa_pass->generate_summary)
2009 pass_init_dump_file (pass);
2011 /* If a timevar is present, start it. */
2012 if (pass->tv_id)
2013 timevar_push (pass->tv_id);
2015 ipa_pass->generate_summary ();
2017 /* Stop timevar. */
2018 if (pass->tv_id)
2019 timevar_pop (pass->tv_id);
2021 pass_fini_dump_file (pass);
2023 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->next;
2027 /* Execute IPA_PASS function transform on NODE. */
2029 static void
2030 execute_one_ipa_transform_pass (struct cgraph_node *node,
2031 struct ipa_opt_pass_d *ipa_pass)
2033 struct opt_pass *pass = ipa_pass;
2034 unsigned int todo_after = 0;
2036 current_pass = pass;
2037 if (!ipa_pass->function_transform)
2038 return;
2040 /* Note that the folders should only create gimple expressions.
2041 This is a hack until the new folder is ready. */
2042 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2044 pass_init_dump_file (pass);
2046 /* Run pre-pass verification. */
2047 execute_todo (ipa_pass->function_transform_todo_flags_start);
2049 /* If a timevar is present, start it. */
2050 if (pass->tv_id != TV_NONE)
2051 timevar_push (pass->tv_id);
2053 /* Do it! */
2054 todo_after = ipa_pass->function_transform (node);
2056 /* Stop timevar. */
2057 if (pass->tv_id != TV_NONE)
2058 timevar_pop (pass->tv_id);
2060 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2061 check_profile_consistency (pass->static_pass_number, 0, true);
2063 /* Run post-pass cleanup and verification. */
2064 execute_todo (todo_after);
2065 verify_interpass_invariants ();
2066 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2067 check_profile_consistency (pass->static_pass_number, 1, true);
2069 if (dump_file)
2070 do_per_function (execute_function_dump, NULL);
2071 pass_fini_dump_file (pass);
2073 current_pass = NULL;
2075 /* Signal this is a suitable GC collection point. */
2076 if (!(todo_after & TODO_do_not_ggc_collect))
2077 ggc_collect ();
2080 /* For the current function, execute all ipa transforms. */
2082 void
2083 execute_all_ipa_transforms (void)
2085 struct cgraph_node *node;
2086 if (!cfun)
2087 return;
2088 node = cgraph_get_node (current_function_decl);
2090 if (node->ipa_transforms_to_apply.exists ())
2092 unsigned int i;
2094 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2095 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2096 node->ipa_transforms_to_apply.release ();
2100 /* Callback for do_per_function to apply all IPA transforms. */
2102 static void
2103 apply_ipa_transforms (void *data)
2105 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2106 if (!node->global.inlined_to && node->ipa_transforms_to_apply.exists ())
2108 *(bool *)data = true;
2109 execute_all_ipa_transforms ();
2110 rebuild_cgraph_edges ();
2114 /* Check if PASS is explicitly disabled or enabled and return
2115 the gate status. FUNC is the function to be processed, and
2116 GATE_STATUS is the gate status determined by pass manager by
2117 default. */
2119 static bool
2120 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2122 bool explicitly_enabled = false;
2123 bool explicitly_disabled = false;
2125 explicitly_enabled
2126 = is_pass_explicitly_enabled_or_disabled (pass, func,
2127 enabled_pass_uid_range_tab);
2128 explicitly_disabled
2129 = is_pass_explicitly_enabled_or_disabled (pass, func,
2130 disabled_pass_uid_range_tab);
2132 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2134 return gate_status;
2138 /* Execute PASS. */
2140 bool
2141 execute_one_pass (struct opt_pass *pass)
2143 unsigned int todo_after = 0;
2145 bool gate_status;
2147 /* IPA passes are executed on whole program, so cfun should be NULL.
2148 Other passes need function context set. */
2149 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2150 gcc_assert (!cfun && !current_function_decl);
2151 else
2152 gcc_assert (cfun && current_function_decl);
2154 current_pass = pass;
2156 /* Check whether gate check should be avoided.
2157 User controls the value of the gate through the parameter "gate_status". */
2158 gate_status = pass->has_gate ? pass->gate () : true;
2159 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2161 /* Override gate with plugin. */
2162 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2164 if (!gate_status)
2166 /* Run so passes selectively disabling themselves on a given function
2167 are not miscounted. */
2168 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2170 check_profile_consistency (pass->static_pass_number, 0, false);
2171 check_profile_consistency (pass->static_pass_number, 1, false);
2173 current_pass = NULL;
2174 return false;
2177 /* Pass execution event trigger: useful to identify passes being
2178 executed. */
2179 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2181 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2182 Apply all trnasforms first. */
2183 if (pass->type == SIMPLE_IPA_PASS)
2185 bool applied = false;
2186 do_per_function (apply_ipa_transforms, (void *)&applied);
2187 if (applied)
2188 symtab_remove_unreachable_nodes (true, dump_file);
2189 /* Restore current_pass. */
2190 current_pass = pass;
2193 if (!quiet_flag && !cfun)
2194 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2196 /* Note that the folders should only create gimple expressions.
2197 This is a hack until the new folder is ready. */
2198 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2200 pass_init_dump_file (pass);
2202 /* Run pre-pass verification. */
2203 execute_todo (pass->todo_flags_start);
2205 #ifdef ENABLE_CHECKING
2206 do_per_function (verify_curr_properties,
2207 (void *)(size_t)pass->properties_required);
2208 #endif
2210 /* If a timevar is present, start it. */
2211 if (pass->tv_id != TV_NONE)
2212 timevar_push (pass->tv_id);
2214 /* Do it! */
2215 if (pass->has_execute)
2217 todo_after = pass->execute ();
2218 do_per_function (clear_last_verified, NULL);
2221 /* Stop timevar. */
2222 if (pass->tv_id != TV_NONE)
2223 timevar_pop (pass->tv_id);
2225 do_per_function (update_properties_after_pass, pass);
2227 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2228 check_profile_consistency (pass->static_pass_number, 0, true);
2230 /* Run post-pass cleanup and verification. */
2231 execute_todo (todo_after | pass->todo_flags_finish);
2232 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2233 check_profile_consistency (pass->static_pass_number, 1, true);
2235 verify_interpass_invariants ();
2236 if (dump_file)
2237 do_per_function (execute_function_dump, NULL);
2238 if (pass->type == IPA_PASS)
2240 struct cgraph_node *node;
2241 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2242 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *)pass);
2245 if (!current_function_decl)
2246 cgraph_process_new_functions ();
2248 pass_fini_dump_file (pass);
2250 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2251 gcc_assert (!(cfun->curr_properties & PROP_trees)
2252 || pass->type != RTL_PASS);
2254 current_pass = NULL;
2256 /* Signal this is a suitable GC collection point. */
2257 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2258 ggc_collect ();
2260 return true;
2263 void
2264 execute_pass_list (struct opt_pass *pass)
2268 gcc_assert (pass->type == GIMPLE_PASS
2269 || pass->type == RTL_PASS);
2270 if (execute_one_pass (pass) && pass->sub)
2271 execute_pass_list (pass->sub);
2272 pass = pass->next;
2274 while (pass);
2277 /* Same as execute_pass_list but assume that subpasses of IPA passes
2278 are local passes. If SET is not NULL, write out summaries of only
2279 those node in SET. */
2281 static void
2282 ipa_write_summaries_2 (struct opt_pass *pass, struct lto_out_decl_state *state)
2284 while (pass)
2286 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2287 gcc_assert (!current_function_decl);
2288 gcc_assert (!cfun);
2289 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2290 if (pass->type == IPA_PASS
2291 && ipa_pass->write_summary
2292 && ((!pass->has_gate) || pass->gate ()))
2294 /* If a timevar is present, start it. */
2295 if (pass->tv_id)
2296 timevar_push (pass->tv_id);
2298 pass_init_dump_file (pass);
2300 ipa_pass->write_summary ();
2302 pass_fini_dump_file (pass);
2304 /* If a timevar is present, start it. */
2305 if (pass->tv_id)
2306 timevar_pop (pass->tv_id);
2309 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2310 ipa_write_summaries_2 (pass->sub, state);
2312 pass = pass->next;
2316 /* Helper function of ipa_write_summaries. Creates and destroys the
2317 decl state and calls ipa_write_summaries_2 for all passes that have
2318 summaries. SET is the set of nodes to be written. */
2320 static void
2321 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2323 pass_manager *passes = g->get_passes ();
2324 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2325 state->symtab_node_encoder = encoder;
2327 lto_push_out_decl_state (state);
2329 gcc_assert (!flag_wpa);
2330 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2331 ipa_write_summaries_2 (passes->all_lto_gen_passes, state);
2333 gcc_assert (lto_get_out_decl_state () == state);
2334 lto_pop_out_decl_state ();
2335 lto_delete_out_decl_state (state);
2338 /* Write out summaries for all the nodes in the callgraph. */
2340 void
2341 ipa_write_summaries (void)
2343 lto_symtab_encoder_t encoder;
2344 int i, order_pos;
2345 struct varpool_node *vnode;
2346 struct cgraph_node *node;
2347 struct cgraph_node **order;
2349 if (!flag_generate_lto || seen_error ())
2350 return;
2352 encoder = lto_symtab_encoder_new (false);
2354 /* Create the callgraph set in the same order used in
2355 cgraph_expand_all_functions. This mostly facilitates debugging,
2356 since it causes the gimple file to be processed in the same order
2357 as the source code. */
2358 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2359 order_pos = ipa_reverse_postorder (order);
2360 gcc_assert (order_pos == cgraph_n_nodes);
2362 for (i = order_pos - 1; i >= 0; i--)
2364 struct cgraph_node *node = order[i];
2366 if (cgraph_function_with_gimple_body_p (node))
2368 /* When streaming out references to statements as part of some IPA
2369 pass summary, the statements need to have uids assigned and the
2370 following does that for all the IPA passes here. Naturally, this
2371 ordering then matches the one IPA-passes get in their stmt_fixup
2372 hooks. */
2374 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2375 renumber_gimple_stmt_uids ();
2376 pop_cfun ();
2378 if (node->definition)
2379 lto_set_symtab_encoder_in_partition (encoder, node);
2382 FOR_EACH_DEFINED_FUNCTION (node)
2383 if (node->alias)
2384 lto_set_symtab_encoder_in_partition (encoder, node);
2385 FOR_EACH_DEFINED_VARIABLE (vnode)
2386 lto_set_symtab_encoder_in_partition (encoder, vnode);
2388 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2390 free (order);
2393 /* Same as execute_pass_list but assume that subpasses of IPA passes
2394 are local passes. If SET is not NULL, write out optimization summaries of
2395 only those node in SET. */
2397 static void
2398 ipa_write_optimization_summaries_1 (struct opt_pass *pass, struct lto_out_decl_state *state)
2400 while (pass)
2402 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2403 gcc_assert (!current_function_decl);
2404 gcc_assert (!cfun);
2405 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2406 if (pass->type == IPA_PASS
2407 && ipa_pass->write_optimization_summary
2408 && ((!pass->has_gate) || pass->gate ()))
2410 /* If a timevar is present, start it. */
2411 if (pass->tv_id)
2412 timevar_push (pass->tv_id);
2414 pass_init_dump_file (pass);
2416 ipa_pass->write_optimization_summary ();
2418 pass_fini_dump_file (pass);
2420 /* If a timevar is present, start it. */
2421 if (pass->tv_id)
2422 timevar_pop (pass->tv_id);
2425 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2426 ipa_write_optimization_summaries_1 (pass->sub, state);
2428 pass = pass->next;
2432 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2433 NULL, write out all summaries of all nodes. */
2435 void
2436 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2438 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2439 lto_symtab_encoder_iterator lsei;
2440 state->symtab_node_encoder = encoder;
2442 lto_push_out_decl_state (state);
2443 for (lsei = lsei_start_function_in_partition (encoder);
2444 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2446 struct cgraph_node *node = lsei_cgraph_node (lsei);
2447 /* When streaming out references to statements as part of some IPA
2448 pass summary, the statements need to have uids assigned.
2450 For functions newly born at WPA stage we need to initialize
2451 the uids here. */
2452 if (node->definition
2453 && gimple_has_body_p (node->decl))
2455 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2456 renumber_gimple_stmt_uids ();
2457 pop_cfun ();
2461 gcc_assert (flag_wpa);
2462 pass_manager *passes = g->get_passes ();
2463 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2464 ipa_write_optimization_summaries_1 (passes->all_lto_gen_passes, state);
2466 gcc_assert (lto_get_out_decl_state () == state);
2467 lto_pop_out_decl_state ();
2468 lto_delete_out_decl_state (state);
2471 /* Same as execute_pass_list but assume that subpasses of IPA passes
2472 are local passes. */
2474 static void
2475 ipa_read_summaries_1 (struct opt_pass *pass)
2477 while (pass)
2479 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2481 gcc_assert (!current_function_decl);
2482 gcc_assert (!cfun);
2483 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2485 if ((!pass->has_gate) || pass->gate ())
2487 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2489 /* If a timevar is present, start it. */
2490 if (pass->tv_id)
2491 timevar_push (pass->tv_id);
2493 pass_init_dump_file (pass);
2495 ipa_pass->read_summary ();
2497 pass_fini_dump_file (pass);
2499 /* Stop timevar. */
2500 if (pass->tv_id)
2501 timevar_pop (pass->tv_id);
2504 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2505 ipa_read_summaries_1 (pass->sub);
2507 pass = pass->next;
2512 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2514 void
2515 ipa_read_summaries (void)
2517 pass_manager *passes = g->get_passes ();
2518 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2519 ipa_read_summaries_1 (passes->all_lto_gen_passes);
2522 /* Same as execute_pass_list but assume that subpasses of IPA passes
2523 are local passes. */
2525 static void
2526 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2528 while (pass)
2530 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2532 gcc_assert (!current_function_decl);
2533 gcc_assert (!cfun);
2534 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2536 if ((!pass->has_gate) || pass->gate ())
2538 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2540 /* If a timevar is present, start it. */
2541 if (pass->tv_id)
2542 timevar_push (pass->tv_id);
2544 pass_init_dump_file (pass);
2546 ipa_pass->read_optimization_summary ();
2548 pass_fini_dump_file (pass);
2550 /* Stop timevar. */
2551 if (pass->tv_id)
2552 timevar_pop (pass->tv_id);
2555 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2556 ipa_read_optimization_summaries_1 (pass->sub);
2558 pass = pass->next;
2562 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2564 void
2565 ipa_read_optimization_summaries (void)
2567 pass_manager *passes = g->get_passes ();
2568 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2569 ipa_read_optimization_summaries_1 (passes->all_lto_gen_passes);
2572 /* Same as execute_pass_list but assume that subpasses of IPA passes
2573 are local passes. */
2574 void
2575 execute_ipa_pass_list (struct opt_pass *pass)
2579 gcc_assert (!current_function_decl);
2580 gcc_assert (!cfun);
2581 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2582 if (execute_one_pass (pass) && pass->sub)
2584 if (pass->sub->type == GIMPLE_PASS)
2586 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2587 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2588 pass->sub);
2589 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2591 else if (pass->sub->type == SIMPLE_IPA_PASS
2592 || pass->sub->type == IPA_PASS)
2593 execute_ipa_pass_list (pass->sub);
2594 else
2595 gcc_unreachable ();
2597 gcc_assert (!current_function_decl);
2598 cgraph_process_new_functions ();
2599 pass = pass->next;
2601 while (pass);
2604 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2606 static void
2607 execute_ipa_stmt_fixups (struct opt_pass *pass,
2608 struct cgraph_node *node, gimple *stmts)
2610 while (pass)
2612 /* Execute all of the IPA_PASSes in the list. */
2613 if (pass->type == IPA_PASS
2614 && ((!pass->has_gate) || pass->gate ()))
2616 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2618 if (ipa_pass->stmt_fixup)
2620 pass_init_dump_file (pass);
2621 /* If a timevar is present, start it. */
2622 if (pass->tv_id)
2623 timevar_push (pass->tv_id);
2625 ipa_pass->stmt_fixup (node, stmts);
2627 /* Stop timevar. */
2628 if (pass->tv_id)
2629 timevar_pop (pass->tv_id);
2630 pass_fini_dump_file (pass);
2632 if (pass->sub)
2633 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2635 pass = pass->next;
2639 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2641 void
2642 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2644 pass_manager *passes = g->get_passes ();
2645 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2649 extern void debug_properties (unsigned int);
2650 extern void dump_properties (FILE *, unsigned int);
2652 DEBUG_FUNCTION void
2653 dump_properties (FILE *dump, unsigned int props)
2655 fprintf (dump, "Properties:\n");
2656 if (props & PROP_gimple_any)
2657 fprintf (dump, "PROP_gimple_any\n");
2658 if (props & PROP_gimple_lcf)
2659 fprintf (dump, "PROP_gimple_lcf\n");
2660 if (props & PROP_gimple_leh)
2661 fprintf (dump, "PROP_gimple_leh\n");
2662 if (props & PROP_cfg)
2663 fprintf (dump, "PROP_cfg\n");
2664 if (props & PROP_ssa)
2665 fprintf (dump, "PROP_ssa\n");
2666 if (props & PROP_no_crit_edges)
2667 fprintf (dump, "PROP_no_crit_edges\n");
2668 if (props & PROP_rtl)
2669 fprintf (dump, "PROP_rtl\n");
2670 if (props & PROP_gimple_lomp)
2671 fprintf (dump, "PROP_gimple_lomp\n");
2672 if (props & PROP_gimple_lcx)
2673 fprintf (dump, "PROP_gimple_lcx\n");
2674 if (props & PROP_gimple_lvec)
2675 fprintf (dump, "PROP_gimple_lvec\n");
2676 if (props & PROP_cfglayout)
2677 fprintf (dump, "PROP_cfglayout\n");
2680 DEBUG_FUNCTION void
2681 debug_properties (unsigned int props)
2683 dump_properties (stderr, props);
2686 /* Called by local passes to see if function is called by already processed nodes.
2687 Because we process nodes in topological order, this means that function is
2688 in recursive cycle or we introduced new direct calls. */
2689 bool
2690 function_called_by_processed_nodes_p (void)
2692 struct cgraph_edge *e;
2693 for (e = cgraph_get_node (current_function_decl)->callers;
2695 e = e->next_caller)
2697 if (e->caller->decl == current_function_decl)
2698 continue;
2699 if (!cgraph_function_with_gimple_body_p (e->caller))
2700 continue;
2701 if (TREE_ASM_WRITTEN (e->caller->decl))
2702 continue;
2703 if (!e->caller->process && !e->caller->global.inlined_to)
2704 break;
2706 if (dump_file && e)
2708 fprintf (dump_file, "Already processed call to:\n");
2709 dump_cgraph_node (dump_file, e->caller);
2711 return e != NULL;
2714 #include "gt-passes.h"