re PR tree-optimization/58143 (wrong code at -O3)
[official-gcc.git] / gcc / passes.c
blob5e6b8e5cc0167cb275a38a3cac4ec20fe91361dd
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 "cgraph.h"
60 #include "opts.h"
61 #include "coverage.h"
62 #include "value-prof.h"
63 #include "tree-inline.h"
64 #include "tree-ssa.h"
65 #include "tree-pass.h"
66 #include "tree-dump.h"
67 #include "df.h"
68 #include "predict.h"
69 #include "lto-streamer.h"
70 #include "plugin.h"
71 #include "ipa-utils.h"
72 #include "tree-pretty-print.h" /* for dump_function_header */
73 #include "context.h"
74 #include "pass_manager.h"
75 #include "tree-ssa-live.h" /* For remove_unused_locals. */
77 using namespace gcc;
79 /* This is used for debugging. It allows the current pass to printed
80 from anywhere in compilation.
81 The variable current_pass is also used for statistics and plugins. */
82 struct opt_pass *current_pass;
84 static void register_pass_name (struct opt_pass *, const char *);
86 /* Most passes are single-instance (within their context) and thus don't
87 need to implement cloning, but passes that support multiple instances
88 *must* provide their own implementation of the clone method.
90 Handle this by providing a default implemenation, but make it a fatal
91 error to call it. */
93 opt_pass *
94 opt_pass::clone ()
96 internal_error ("pass %s does not support cloning", name);
99 bool
100 opt_pass::gate ()
102 return true;
105 unsigned int
106 opt_pass::execute ()
108 return 0;
111 opt_pass::opt_pass (const pass_data &data, context *ctxt)
112 : pass_data (data),
113 sub (NULL),
114 next (NULL),
115 static_pass_number (0),
116 m_ctxt (ctxt)
121 void
122 pass_manager::execute_early_local_passes ()
124 execute_pass_list (pass_early_local_passes_1->sub);
127 unsigned int
128 pass_manager::execute_pass_mode_switching ()
130 return pass_mode_switching_1->execute ();
134 /* Call from anywhere to find out what pass this is. Useful for
135 printing out debugging information deep inside an service
136 routine. */
137 void
138 print_current_pass (FILE *file)
140 if (current_pass)
141 fprintf (file, "current pass = %s (%d)\n",
142 current_pass->name, current_pass->static_pass_number);
143 else
144 fprintf (file, "no current pass.\n");
148 /* Call from the debugger to get the current pass name. */
149 DEBUG_FUNCTION void
150 debug_pass (void)
152 print_current_pass (stderr);
157 /* Global variables used to communicate with passes. */
158 bool in_gimple_form;
159 bool first_pass_instance;
162 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
163 and TYPE_DECL nodes.
165 This does nothing for local (non-static) variables, unless the
166 variable is a register variable with DECL_ASSEMBLER_NAME set. In
167 that case, or if the variable is not an automatic, it sets up the
168 RTL and outputs any assembler code (label definition, storage
169 allocation and initialization).
171 DECL is the declaration. TOP_LEVEL is nonzero
172 if this declaration is not within a function. */
174 void
175 rest_of_decl_compilation (tree decl,
176 int top_level,
177 int at_end)
179 /* We deferred calling assemble_alias so that we could collect
180 other attributes such as visibility. Emit the alias now. */
181 if (!in_lto_p)
183 tree alias;
184 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
185 if (alias)
187 alias = TREE_VALUE (TREE_VALUE (alias));
188 alias = get_identifier (TREE_STRING_POINTER (alias));
189 /* A quirk of the initial implementation of aliases required that the
190 user add "extern" to all of them. Which is silly, but now
191 historical. Do note that the symbol is in fact locally defined. */
192 DECL_EXTERNAL (decl) = 0;
193 TREE_STATIC (decl) = 1;
194 assemble_alias (decl, alias);
198 /* Can't defer this, because it needs to happen before any
199 later function definitions are processed. */
200 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
201 make_decl_rtl (decl);
203 /* Forward declarations for nested functions are not "external",
204 but we need to treat them as if they were. */
205 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
206 || TREE_CODE (decl) == FUNCTION_DECL)
208 timevar_push (TV_VARCONST);
210 /* Don't output anything when a tentative file-scope definition
211 is seen. But at end of compilation, do output code for them.
213 We do output all variables and rely on
214 callgraph code to defer them except for forward declarations
215 (see gcc.c-torture/compile/920624-1.c) */
216 if ((at_end
217 || !DECL_DEFER_OUTPUT (decl)
218 || DECL_INITIAL (decl))
219 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
220 && !DECL_EXTERNAL (decl))
222 /* When reading LTO unit, we also read varpool, so do not
223 rebuild it. */
224 if (in_lto_p && !at_end)
226 else if (TREE_CODE (decl) != FUNCTION_DECL)
227 varpool_finalize_decl (decl);
230 #ifdef ASM_FINISH_DECLARE_OBJECT
231 if (decl == last_assemble_variable_decl)
233 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
234 top_level, at_end);
236 #endif
238 timevar_pop (TV_VARCONST);
240 else if (TREE_CODE (decl) == TYPE_DECL
241 /* Like in rest_of_type_compilation, avoid confusing the debug
242 information machinery when there are errors. */
243 && !seen_error ())
245 timevar_push (TV_SYMOUT);
246 debug_hooks->type_decl (decl, !top_level);
247 timevar_pop (TV_SYMOUT);
250 /* Let cgraph know about the existence of variables. */
251 if (in_lto_p && !at_end)
253 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
254 && TREE_STATIC (decl))
255 varpool_node_for_decl (decl);
258 /* Called after finishing a record, union or enumeral type. */
260 void
261 rest_of_type_compilation (tree type, int toplev)
263 /* Avoid confusing the debug information machinery when there are
264 errors. */
265 if (seen_error ())
266 return;
268 timevar_push (TV_SYMOUT);
269 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
270 timevar_pop (TV_SYMOUT);
275 void
276 pass_manager::
277 finish_optimization_passes (void)
279 int i;
280 struct dump_file_info *dfi;
281 char *name;
282 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
284 timevar_push (TV_DUMP);
285 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
287 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
288 end_branch_prob ();
289 dumps->dump_finish (pass_profile_1->static_pass_number);
292 if (optimize > 0)
294 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
295 print_combine_total_stats ();
296 dumps->dump_finish (pass_profile_1->static_pass_number);
299 /* Do whatever is necessary to finish printing the graphs. */
300 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
301 if (dumps->dump_initialized_p (i)
302 && (dfi->pflags & TDF_GRAPH) != 0
303 && (name = dumps->get_dump_file_name (i)) != NULL)
305 finish_graph_dump_file (name);
306 free (name);
309 timevar_pop (TV_DUMP);
312 static unsigned int
313 execute_all_early_local_passes (void)
315 /* Once this pass (and its sub-passes) are complete, all functions
316 will be in SSA form. Technically this state change is happening
317 a tad early, since the sub-passes have not yet run, but since
318 none of the sub-passes are IPA passes and do not create new
319 functions, this is ok. We're setting this value for the benefit
320 of IPA passes that follow. */
321 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
322 cgraph_state = CGRAPH_STATE_IPA_SSA;
323 return 0;
326 /* Gate: execute, or not, all of the non-trivial optimizations. */
328 static bool
329 gate_all_early_local_passes (void)
331 /* Don't bother doing anything if the program has errors. */
332 return (!seen_error () && !in_lto_p);
335 namespace {
337 const pass_data pass_data_early_local_passes =
339 SIMPLE_IPA_PASS, /* type */
340 "early_local_cleanups", /* name */
341 OPTGROUP_NONE, /* optinfo_flags */
342 true, /* has_gate */
343 true, /* has_execute */
344 TV_EARLY_LOCAL, /* tv_id */
345 0, /* properties_required */
346 0, /* properties_provided */
347 0, /* properties_destroyed */
348 0, /* todo_flags_start */
349 TODO_remove_functions, /* todo_flags_finish */
352 class pass_early_local_passes : public simple_ipa_opt_pass
354 public:
355 pass_early_local_passes (gcc::context *ctxt)
356 : simple_ipa_opt_pass (pass_data_early_local_passes, ctxt)
359 /* opt_pass methods: */
360 bool gate () { return gate_all_early_local_passes (); }
361 unsigned int execute () { return execute_all_early_local_passes (); }
363 }; // class pass_early_local_passes
365 } // anon namespace
367 simple_ipa_opt_pass *
368 make_pass_early_local_passes (gcc::context *ctxt)
370 return new pass_early_local_passes (ctxt);
373 /* Gate: execute, or not, all of the non-trivial optimizations. */
375 static bool
376 gate_all_early_optimizations (void)
378 return (optimize >= 1
379 /* Don't bother doing anything if the program has errors. */
380 && !seen_error ());
383 namespace {
385 const pass_data pass_data_all_early_optimizations =
387 GIMPLE_PASS, /* type */
388 "early_optimizations", /* name */
389 OPTGROUP_NONE, /* optinfo_flags */
390 true, /* has_gate */
391 false, /* has_execute */
392 TV_NONE, /* tv_id */
393 0, /* properties_required */
394 0, /* properties_provided */
395 0, /* properties_destroyed */
396 0, /* todo_flags_start */
397 0, /* todo_flags_finish */
400 class pass_all_early_optimizations : public gimple_opt_pass
402 public:
403 pass_all_early_optimizations (gcc::context *ctxt)
404 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
407 /* opt_pass methods: */
408 bool gate () { return gate_all_early_optimizations (); }
410 }; // class pass_all_early_optimizations
412 } // anon namespace
414 static gimple_opt_pass *
415 make_pass_all_early_optimizations (gcc::context *ctxt)
417 return new pass_all_early_optimizations (ctxt);
420 /* Gate: execute, or not, all of the non-trivial optimizations. */
422 static bool
423 gate_all_optimizations (void)
425 return optimize >= 1 && !optimize_debug;
428 namespace {
430 const pass_data pass_data_all_optimizations =
432 GIMPLE_PASS, /* type */
433 "*all_optimizations", /* name */
434 OPTGROUP_NONE, /* optinfo_flags */
435 true, /* has_gate */
436 false, /* has_execute */
437 TV_OPTIMIZE, /* tv_id */
438 0, /* properties_required */
439 0, /* properties_provided */
440 0, /* properties_destroyed */
441 0, /* todo_flags_start */
442 0, /* todo_flags_finish */
445 class pass_all_optimizations : public gimple_opt_pass
447 public:
448 pass_all_optimizations (gcc::context *ctxt)
449 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
452 /* opt_pass methods: */
453 bool gate () { return gate_all_optimizations (); }
455 }; // class pass_all_optimizations
457 } // anon namespace
459 static gimple_opt_pass *
460 make_pass_all_optimizations (gcc::context *ctxt)
462 return new pass_all_optimizations (ctxt);
465 /* Gate: execute, or not, all of the non-trivial optimizations. */
467 static bool
468 gate_all_optimizations_g (void)
470 return optimize >= 1 && optimize_debug;
473 namespace {
475 const pass_data pass_data_all_optimizations_g =
477 GIMPLE_PASS, /* type */
478 "*all_optimizations_g", /* name */
479 OPTGROUP_NONE, /* optinfo_flags */
480 true, /* has_gate */
481 false, /* has_execute */
482 TV_OPTIMIZE, /* tv_id */
483 0, /* properties_required */
484 0, /* properties_provided */
485 0, /* properties_destroyed */
486 0, /* todo_flags_start */
487 0, /* todo_flags_finish */
490 class pass_all_optimizations_g : public gimple_opt_pass
492 public:
493 pass_all_optimizations_g (gcc::context *ctxt)
494 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
497 /* opt_pass methods: */
498 bool gate () { return gate_all_optimizations_g (); }
500 }; // class pass_all_optimizations_g
502 } // anon namespace
504 static gimple_opt_pass *
505 make_pass_all_optimizations_g (gcc::context *ctxt)
507 return new pass_all_optimizations_g (ctxt);
510 static bool
511 gate_rest_of_compilation (void)
513 /* Early return if there were errors. We can run afoul of our
514 consistency checks, and there's not really much point in fixing them. */
515 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
518 namespace {
520 const pass_data pass_data_rest_of_compilation =
522 RTL_PASS, /* type */
523 "*rest_of_compilation", /* name */
524 OPTGROUP_NONE, /* optinfo_flags */
525 true, /* has_gate */
526 false, /* has_execute */
527 TV_REST_OF_COMPILATION, /* tv_id */
528 PROP_rtl, /* properties_required */
529 0, /* properties_provided */
530 0, /* properties_destroyed */
531 0, /* todo_flags_start */
532 0, /* todo_flags_finish */
535 class pass_rest_of_compilation : public rtl_opt_pass
537 public:
538 pass_rest_of_compilation (gcc::context *ctxt)
539 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
542 /* opt_pass methods: */
543 bool gate () { return gate_rest_of_compilation (); }
545 }; // class pass_rest_of_compilation
547 } // anon namespace
549 static rtl_opt_pass *
550 make_pass_rest_of_compilation (gcc::context *ctxt)
552 return new pass_rest_of_compilation (ctxt);
555 static bool
556 gate_postreload (void)
558 return reload_completed;
561 namespace {
563 const pass_data pass_data_postreload =
565 RTL_PASS, /* type */
566 "*all-postreload", /* name */
567 OPTGROUP_NONE, /* optinfo_flags */
568 true, /* has_gate */
569 false, /* has_execute */
570 TV_POSTRELOAD, /* tv_id */
571 PROP_rtl, /* properties_required */
572 0, /* properties_provided */
573 0, /* properties_destroyed */
574 0, /* todo_flags_start */
575 TODO_verify_rtl_sharing, /* todo_flags_finish */
578 class pass_postreload : public rtl_opt_pass
580 public:
581 pass_postreload (gcc::context *ctxt)
582 : rtl_opt_pass (pass_data_postreload, ctxt)
585 /* opt_pass methods: */
586 bool gate () { return gate_postreload (); }
588 }; // class pass_postreload
590 } // anon namespace
592 static rtl_opt_pass *
593 make_pass_postreload (gcc::context *ctxt)
595 return new pass_postreload (ctxt);
600 /* Set the static pass number of pass PASS to ID and record that
601 in the mapping from static pass number to pass. */
603 void
604 pass_manager::
605 set_pass_for_id (int id, struct opt_pass *pass)
607 pass->static_pass_number = id;
608 if (passes_by_id_size <= id)
610 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
611 memset (passes_by_id + passes_by_id_size, 0,
612 (id + 1 - passes_by_id_size) * sizeof (void *));
613 passes_by_id_size = id + 1;
615 passes_by_id[id] = pass;
618 /* Return the pass with the static pass number ID. */
620 struct opt_pass *
621 pass_manager::get_pass_for_id (int id) const
623 if (id >= passes_by_id_size)
624 return NULL;
625 return passes_by_id[id];
628 /* Iterate over the pass tree allocating dump file numbers. We want
629 to do this depth first, and independent of whether the pass is
630 enabled or not. */
632 void
633 register_one_dump_file (struct opt_pass *pass)
635 g->get_passes ()->register_one_dump_file (pass);
638 void
639 pass_manager::register_one_dump_file (struct opt_pass *pass)
641 char *dot_name, *flag_name, *glob_name;
642 const char *name, *full_name, *prefix;
643 char num[10];
644 int flags, id;
645 int optgroup_flags = OPTGROUP_NONE;
646 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
648 /* See below in next_pass_1. */
649 num[0] = '\0';
650 if (pass->static_pass_number != -1)
651 sprintf (num, "%d", ((int) pass->static_pass_number < 0
652 ? 1 : pass->static_pass_number));
654 /* The name is both used to identify the pass for the purposes of plugins,
655 and to specify dump file name and option.
656 The latter two might want something short which is not quite unique; for
657 that reason, we may have a disambiguating prefix, followed by a space
658 to mark the start of the following dump file name / option string. */
659 name = strchr (pass->name, ' ');
660 name = name ? name + 1 : pass->name;
661 dot_name = concat (".", name, num, NULL);
662 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
664 prefix = "ipa-";
665 flags = TDF_IPA;
666 optgroup_flags |= OPTGROUP_IPA;
668 else if (pass->type == GIMPLE_PASS)
670 prefix = "tree-";
671 flags = TDF_TREE;
673 else
675 prefix = "rtl-";
676 flags = TDF_RTL;
679 flag_name = concat (prefix, name, num, NULL);
680 glob_name = concat (prefix, name, NULL);
681 optgroup_flags |= pass->optinfo_flags;
682 /* For any passes that do not have an optgroup set, and which are not
683 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
684 any dump messages are emitted properly under -fopt-info(-optall). */
685 if (optgroup_flags == OPTGROUP_NONE)
686 optgroup_flags = OPTGROUP_OTHER;
687 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
688 optgroup_flags);
689 set_pass_for_id (id, pass);
690 full_name = concat (prefix, pass->name, num, NULL);
691 register_pass_name (pass, full_name);
692 free (CONST_CAST (char *, full_name));
695 /* Recursive worker function for register_dump_files. */
698 pass_manager::
699 register_dump_files_1 (struct opt_pass *pass, int properties)
703 int new_properties = (properties | pass->properties_provided)
704 & ~pass->properties_destroyed;
706 if (pass->name && pass->name[0] != '*')
707 register_one_dump_file (pass);
709 if (pass->sub)
710 new_properties = register_dump_files_1 (pass->sub, new_properties);
712 /* If we have a gate, combine the properties that we could have with
713 and without the pass being examined. */
714 if (pass->has_gate)
715 properties &= new_properties;
716 else
717 properties = new_properties;
719 pass = pass->next;
721 while (pass);
723 return properties;
726 /* Register the dump files for the pass_manager starting at PASS.
727 PROPERTIES reflects the properties that are guaranteed to be available at
728 the beginning of the pipeline. */
730 void
731 pass_manager::
732 register_dump_files (struct opt_pass *pass,int properties)
734 pass->properties_required |= properties;
735 register_dump_files_1 (pass, properties);
738 struct pass_registry
740 const char* unique_name;
741 struct opt_pass *pass;
744 /* Helper for pass_registry hash table. */
746 struct pass_registry_hasher : typed_noop_remove <pass_registry>
748 typedef pass_registry value_type;
749 typedef pass_registry compare_type;
750 static inline hashval_t hash (const value_type *);
751 static inline bool equal (const value_type *, const compare_type *);
754 /* Pass registry hash function. */
756 inline hashval_t
757 pass_registry_hasher::hash (const value_type *s)
759 return htab_hash_string (s->unique_name);
762 /* Hash equal function */
764 inline bool
765 pass_registry_hasher::equal (const value_type *s1, const compare_type *s2)
767 return !strcmp (s1->unique_name, s2->unique_name);
770 static hash_table <pass_registry_hasher> name_to_pass_map;
772 /* Register PASS with NAME. */
774 static void
775 register_pass_name (struct opt_pass *pass, const char *name)
777 struct pass_registry **slot;
778 struct pass_registry pr;
780 if (!name_to_pass_map.is_created ())
781 name_to_pass_map.create (256);
783 pr.unique_name = name;
784 slot = name_to_pass_map.find_slot (&pr, INSERT);
785 if (!*slot)
787 struct pass_registry *new_pr;
789 new_pr = XCNEW (struct pass_registry);
790 new_pr->unique_name = xstrdup (name);
791 new_pr->pass = pass;
792 *slot = new_pr;
794 else
795 return; /* Ignore plugin passes. */
798 /* Map from pass id to canonicalized pass name. */
800 typedef const char *char_ptr;
801 static vec<char_ptr> pass_tab = vNULL;
803 /* Callback function for traversing NAME_TO_PASS_MAP. */
806 passes_pass_traverse (pass_registry **p, void *data ATTRIBUTE_UNUSED)
808 struct opt_pass *pass = (*p)->pass;
810 gcc_assert (pass->static_pass_number > 0);
811 gcc_assert (pass_tab.exists ());
813 pass_tab[pass->static_pass_number] = (*p)->unique_name;
815 return 1;
818 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
819 table for dumping purpose. */
821 static void
822 create_pass_tab (void)
824 if (!flag_dump_passes)
825 return;
827 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
828 name_to_pass_map.traverse <void *, passes_pass_traverse> (NULL);
831 static bool override_gate_status (struct opt_pass *, tree, bool);
833 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
834 is turned on or not. */
836 static void
837 dump_one_pass (struct opt_pass *pass, int pass_indent)
839 int indent = 3 * pass_indent;
840 const char *pn;
841 bool is_on, is_really_on;
843 is_on = pass->has_gate ? pass->gate () : true;
844 is_really_on = override_gate_status (pass, current_function_decl, is_on);
846 if (pass->static_pass_number <= 0)
847 pn = pass->name;
848 else
849 pn = pass_tab[pass->static_pass_number];
851 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
852 (15 - indent < 0 ? 0 : 15 - indent), " ",
853 is_on ? " ON" : " OFF",
854 ((!is_on) == (!is_really_on) ? ""
855 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
858 /* Dump pass list PASS with indentation INDENT. */
860 static void
861 dump_pass_list (struct opt_pass *pass, int indent)
865 dump_one_pass (pass, indent);
866 if (pass->sub)
867 dump_pass_list (pass->sub, indent + 1);
868 pass = pass->next;
870 while (pass);
873 /* Dump all optimization passes. */
875 void
876 dump_passes (void)
878 g->get_passes ()->dump_passes ();
881 void
882 pass_manager::dump_passes () const
884 struct cgraph_node *n, *node = NULL;
886 create_pass_tab ();
888 FOR_EACH_FUNCTION (n)
889 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
891 node = n;
892 break;
895 if (!node)
896 return;
898 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
900 dump_pass_list (all_lowering_passes, 1);
901 dump_pass_list (all_small_ipa_passes, 1);
902 dump_pass_list (all_regular_ipa_passes, 1);
903 dump_pass_list (all_lto_gen_passes, 1);
904 dump_pass_list (all_late_ipa_passes, 1);
905 dump_pass_list (all_passes, 1);
907 pop_cfun ();
911 /* Returns the pass with NAME. */
913 static struct opt_pass *
914 get_pass_by_name (const char *name)
916 struct pass_registry **slot, pr;
918 pr.unique_name = name;
919 slot = name_to_pass_map.find_slot (&pr, NO_INSERT);
921 if (!slot || !*slot)
922 return NULL;
924 return (*slot)->pass;
928 /* Range [start, last]. */
930 struct uid_range
932 unsigned int start;
933 unsigned int last;
934 const char *assem_name;
935 struct uid_range *next;
938 typedef struct uid_range *uid_range_p;
941 static vec<uid_range_p>
942 enabled_pass_uid_range_tab = vNULL;
943 static vec<uid_range_p>
944 disabled_pass_uid_range_tab = vNULL;
947 /* Parse option string for -fdisable- and -fenable-
948 The syntax of the options:
950 -fenable-<pass_name>
951 -fdisable-<pass_name>
953 -fenable-<pass_name>=s1:e1,s2:e2,...
954 -fdisable-<pass_name>=s1:e1,s2:e2,...
957 static void
958 enable_disable_pass (const char *arg, bool is_enable)
960 struct opt_pass *pass;
961 char *range_str, *phase_name;
962 char *argstr = xstrdup (arg);
963 vec<uid_range_p> *tab = 0;
965 range_str = strchr (argstr,'=');
966 if (range_str)
968 *range_str = '\0';
969 range_str++;
972 phase_name = argstr;
973 if (!*phase_name)
975 if (is_enable)
976 error ("unrecognized option -fenable");
977 else
978 error ("unrecognized option -fdisable");
979 free (argstr);
980 return;
982 pass = get_pass_by_name (phase_name);
983 if (!pass || pass->static_pass_number == -1)
985 if (is_enable)
986 error ("unknown pass %s specified in -fenable", phase_name);
987 else
988 error ("unknown pass %s specified in -fdisable", phase_name);
989 free (argstr);
990 return;
993 if (is_enable)
994 tab = &enabled_pass_uid_range_tab;
995 else
996 tab = &disabled_pass_uid_range_tab;
998 if ((unsigned) pass->static_pass_number >= tab->length ())
999 tab->safe_grow_cleared (pass->static_pass_number + 1);
1001 if (!range_str)
1003 uid_range_p slot;
1004 uid_range_p new_range = XCNEW (struct uid_range);
1006 new_range->start = 0;
1007 new_range->last = (unsigned)-1;
1009 slot = (*tab)[pass->static_pass_number];
1010 new_range->next = slot;
1011 (*tab)[pass->static_pass_number] = new_range;
1012 if (is_enable)
1013 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1014 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1015 else
1016 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1017 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1019 else
1021 char *next_range = NULL;
1022 char *one_range = range_str;
1023 char *end_val = NULL;
1027 uid_range_p slot;
1028 uid_range_p new_range;
1029 char *invalid = NULL;
1030 long start;
1031 char *func_name = NULL;
1033 next_range = strchr (one_range, ',');
1034 if (next_range)
1036 *next_range = '\0';
1037 next_range++;
1040 end_val = strchr (one_range, ':');
1041 if (end_val)
1043 *end_val = '\0';
1044 end_val++;
1046 start = strtol (one_range, &invalid, 10);
1047 if (*invalid || start < 0)
1049 if (end_val || (one_range[0] >= '0'
1050 && one_range[0] <= '9'))
1052 error ("Invalid range %s in option %s",
1053 one_range,
1054 is_enable ? "-fenable" : "-fdisable");
1055 free (argstr);
1056 return;
1058 func_name = one_range;
1060 if (!end_val)
1062 new_range = XCNEW (struct uid_range);
1063 if (!func_name)
1065 new_range->start = (unsigned) start;
1066 new_range->last = (unsigned) start;
1068 else
1070 new_range->start = (unsigned) -1;
1071 new_range->last = (unsigned) -1;
1072 new_range->assem_name = xstrdup (func_name);
1075 else
1077 long last = strtol (end_val, &invalid, 10);
1078 if (*invalid || last < start)
1080 error ("Invalid range %s in option %s",
1081 end_val,
1082 is_enable ? "-fenable" : "-fdisable");
1083 free (argstr);
1084 return;
1086 new_range = XCNEW (struct uid_range);
1087 new_range->start = (unsigned) start;
1088 new_range->last = (unsigned) last;
1091 slot = (*tab)[pass->static_pass_number];
1092 new_range->next = slot;
1093 (*tab)[pass->static_pass_number] = new_range;
1094 if (is_enable)
1096 if (new_range->assem_name)
1097 inform (UNKNOWN_LOCATION,
1098 "enable pass %s for function %s",
1099 phase_name, new_range->assem_name);
1100 else
1101 inform (UNKNOWN_LOCATION,
1102 "enable pass %s for functions in the range of [%u, %u]",
1103 phase_name, new_range->start, new_range->last);
1105 else
1107 if (new_range->assem_name)
1108 inform (UNKNOWN_LOCATION,
1109 "disable pass %s for function %s",
1110 phase_name, new_range->assem_name);
1111 else
1112 inform (UNKNOWN_LOCATION,
1113 "disable pass %s for functions in the range of [%u, %u]",
1114 phase_name, new_range->start, new_range->last);
1117 one_range = next_range;
1118 } while (next_range);
1121 free (argstr);
1124 /* Enable pass specified by ARG. */
1126 void
1127 enable_pass (const char *arg)
1129 enable_disable_pass (arg, true);
1132 /* Disable pass specified by ARG. */
1134 void
1135 disable_pass (const char *arg)
1137 enable_disable_pass (arg, false);
1140 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1142 static bool
1143 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
1144 tree func,
1145 vec<uid_range_p> tab)
1147 uid_range_p slot, range;
1148 int cgraph_uid;
1149 const char *aname = NULL;
1151 if (!tab.exists ()
1152 || (unsigned) pass->static_pass_number >= tab.length ()
1153 || pass->static_pass_number == -1)
1154 return false;
1156 slot = tab[pass->static_pass_number];
1157 if (!slot)
1158 return false;
1160 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
1161 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1162 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1164 range = slot;
1165 while (range)
1167 if ((unsigned) cgraph_uid >= range->start
1168 && (unsigned) cgraph_uid <= range->last)
1169 return true;
1170 if (range->assem_name && aname
1171 && !strcmp (range->assem_name, aname))
1172 return true;
1173 range = range->next;
1176 return false;
1180 /* Update static_pass_number for passes (and the flag
1181 TODO_mark_first_instance).
1183 Passes are constructed with static_pass_number preinitialized to 0
1185 This field is used in two different ways: initially as instance numbers
1186 of their kind, and then as ids within the entire pass manager.
1188 Within pass_manager::pass_manager:
1190 * In add_pass_instance(), as called by next_pass_1 in
1191 NEXT_PASS in init_optimization_passes
1193 * When the initial instance of a pass within a pass manager is seen,
1194 it is flagged, and its static_pass_number is set to -1
1196 * On subsequent times that it is seen, the static pass number
1197 is decremented each time, so that if there are e.g. 4 dups,
1198 they have static_pass_number -4, 2, 3, 4 respectively (note
1199 how the initial one is negative and gives the count); these
1200 can be thought of as instance numbers of the specific pass
1202 * Within the register_dump_files () traversal, set_pass_for_id()
1203 is called on each pass, using these instance numbers to create
1204 dumpfile switches, and then overwriting them with a pass id,
1205 which are global to the whole pass manager (based on
1206 (TDI_end + current value of extra_dump_files_in_use) ) */
1208 static void
1209 add_pass_instance (struct opt_pass *new_pass, bool track_duplicates,
1210 opt_pass *initial_pass)
1212 /* Are we dealing with the first pass of its kind, or a clone? */
1213 if (new_pass != initial_pass)
1215 /* We're dealing with a clone. */
1216 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1218 /* Indicate to register_dump_files that this pass has duplicates,
1219 and so it should rename the dump file. The first instance will
1220 be -1, and be number of duplicates = -static_pass_number - 1.
1221 Subsequent instances will be > 0 and just the duplicate number. */
1222 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1224 initial_pass->static_pass_number -= 1;
1225 new_pass->static_pass_number = -initial_pass->static_pass_number;
1228 else
1230 /* We're dealing with the first pass of its kind. */
1231 new_pass->todo_flags_start |= TODO_mark_first_instance;
1232 new_pass->static_pass_number = -1;
1234 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1238 /* Add a pass to the pass list. Duplicate the pass if it's already
1239 in the list. */
1241 static struct opt_pass **
1242 next_pass_1 (struct opt_pass **list, struct opt_pass *pass,
1243 struct opt_pass *initial_pass)
1245 /* Every pass should have a name so that plugins can refer to them. */
1246 gcc_assert (pass->name != NULL);
1248 add_pass_instance (pass, false, initial_pass);
1249 *list = pass;
1251 return &(*list)->next;
1254 /* List node for an inserted pass instance. We need to keep track of all
1255 the newly-added pass instances (with 'added_pass_nodes' defined below)
1256 so that we can register their dump files after pass-positioning is finished.
1257 Registering dumping files needs to be post-processed or the
1258 static_pass_number of the opt_pass object would be modified and mess up
1259 the dump file names of future pass instances to be added. */
1261 struct pass_list_node
1263 struct opt_pass *pass;
1264 struct pass_list_node *next;
1267 static struct pass_list_node *added_pass_nodes = NULL;
1268 static struct pass_list_node *prev_added_pass_node;
1270 /* Insert the pass at the proper position. Return true if the pass
1271 is successfully added.
1273 NEW_PASS_INFO - new pass to be inserted
1274 PASS_LIST - root of the pass list to insert the new pass to */
1276 static bool
1277 position_pass (struct register_pass_info *new_pass_info,
1278 struct opt_pass **pass_list)
1280 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1281 bool success = false;
1283 for ( ; pass; prev_pass = pass, pass = pass->next)
1285 /* Check if the current pass is of the same type as the new pass and
1286 matches the name and the instance number of the reference pass. */
1287 if (pass->type == new_pass_info->pass->type
1288 && pass->name
1289 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1290 && ((new_pass_info->ref_pass_instance_number == 0)
1291 || (new_pass_info->ref_pass_instance_number ==
1292 pass->static_pass_number)
1293 || (new_pass_info->ref_pass_instance_number == 1
1294 && pass->todo_flags_start & TODO_mark_first_instance)))
1296 struct opt_pass *new_pass;
1297 struct pass_list_node *new_pass_node;
1299 if (new_pass_info->ref_pass_instance_number == 0)
1301 new_pass = new_pass_info->pass->clone ();
1302 add_pass_instance (new_pass, true, new_pass_info->pass);
1304 else
1306 new_pass = new_pass_info->pass;
1307 add_pass_instance (new_pass, true, new_pass);
1310 /* Insert the new pass instance based on the positioning op. */
1311 switch (new_pass_info->pos_op)
1313 case PASS_POS_INSERT_AFTER:
1314 new_pass->next = pass->next;
1315 pass->next = new_pass;
1317 /* Skip newly inserted pass to avoid repeated
1318 insertions in the case where the new pass and the
1319 existing one have the same name. */
1320 pass = new_pass;
1321 break;
1322 case PASS_POS_INSERT_BEFORE:
1323 new_pass->next = pass;
1324 if (prev_pass)
1325 prev_pass->next = new_pass;
1326 else
1327 *pass_list = new_pass;
1328 break;
1329 case PASS_POS_REPLACE:
1330 new_pass->next = pass->next;
1331 if (prev_pass)
1332 prev_pass->next = new_pass;
1333 else
1334 *pass_list = new_pass;
1335 new_pass->sub = pass->sub;
1336 new_pass->tv_id = pass->tv_id;
1337 pass = new_pass;
1338 break;
1339 default:
1340 error ("invalid pass positioning operation");
1341 return false;
1344 /* Save the newly added pass (instance) in the added_pass_nodes
1345 list so that we can register its dump file later. Note that
1346 we cannot register the dump file now because doing so will modify
1347 the static_pass_number of the opt_pass object and therefore
1348 mess up the dump file name of future instances. */
1349 new_pass_node = XCNEW (struct pass_list_node);
1350 new_pass_node->pass = new_pass;
1351 if (!added_pass_nodes)
1352 added_pass_nodes = new_pass_node;
1353 else
1354 prev_added_pass_node->next = new_pass_node;
1355 prev_added_pass_node = new_pass_node;
1357 success = true;
1360 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1361 success = true;
1364 return success;
1367 /* Hooks a new pass into the pass lists.
1369 PASS_INFO - pass information that specifies the opt_pass object,
1370 reference pass, instance number, and how to position
1371 the pass */
1373 void
1374 register_pass (struct register_pass_info *pass_info)
1376 g->get_passes ()->register_pass (pass_info);
1379 void
1380 register_pass (opt_pass* pass, pass_positioning_ops pos,
1381 const char* ref_pass_name, int ref_pass_inst_number)
1383 register_pass_info i;
1384 i.pass = pass;
1385 i.reference_pass_name = ref_pass_name;
1386 i.ref_pass_instance_number = ref_pass_inst_number;
1387 i.pos_op = pos;
1389 g->get_passes ()->register_pass (&i);
1392 void
1393 pass_manager::register_pass (struct register_pass_info *pass_info)
1395 bool all_instances, success;
1396 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1398 /* The checks below could fail in buggy plugins. Existing GCC
1399 passes should never fail these checks, so we mention plugin in
1400 the messages. */
1401 if (!pass_info->pass)
1402 fatal_error ("plugin cannot register a missing pass");
1404 if (!pass_info->pass->name)
1405 fatal_error ("plugin cannot register an unnamed pass");
1407 if (!pass_info->reference_pass_name)
1408 fatal_error
1409 ("plugin cannot register pass %qs without reference pass name",
1410 pass_info->pass->name);
1412 /* Try to insert the new pass to the pass lists. We need to check
1413 all five lists as the reference pass could be in one (or all) of
1414 them. */
1415 all_instances = pass_info->ref_pass_instance_number == 0;
1416 success = position_pass (pass_info, &all_lowering_passes);
1417 if (!success || all_instances)
1418 success |= position_pass (pass_info, &all_small_ipa_passes);
1419 if (!success || all_instances)
1420 success |= position_pass (pass_info, &all_regular_ipa_passes);
1421 if (!success || all_instances)
1422 success |= position_pass (pass_info, &all_lto_gen_passes);
1423 if (!success || all_instances)
1424 success |= position_pass (pass_info, &all_late_ipa_passes);
1425 if (!success || all_instances)
1426 success |= position_pass (pass_info, &all_passes);
1427 if (!success)
1428 fatal_error
1429 ("pass %qs not found but is referenced by new pass %qs",
1430 pass_info->reference_pass_name, pass_info->pass->name);
1432 /* OK, we have successfully inserted the new pass. We need to register
1433 the dump files for the newly added pass and its duplicates (if any).
1434 Because the registration of plugin/backend passes happens after the
1435 command-line options are parsed, the options that specify single
1436 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1437 passes. Therefore we currently can only enable dumping of
1438 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1439 are specified. While doing so, we also delete the pass_list_node
1440 objects created during pass positioning. */
1441 while (added_pass_nodes)
1443 struct pass_list_node *next_node = added_pass_nodes->next;
1444 enum tree_dump_index tdi;
1445 register_one_dump_file (added_pass_nodes->pass);
1446 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1447 || added_pass_nodes->pass->type == IPA_PASS)
1448 tdi = TDI_ipa_all;
1449 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1450 tdi = TDI_tree_all;
1451 else
1452 tdi = TDI_rtl_all;
1453 /* Check if dump-all flag is specified. */
1454 if (dumps->get_dump_file_info (tdi)->pstate)
1455 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1456 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1457 XDELETE (added_pass_nodes);
1458 added_pass_nodes = next_node;
1462 /* Construct the pass tree. The sequencing of passes is driven by
1463 the cgraph routines:
1465 finalize_compilation_unit ()
1466 for each node N in the cgraph
1467 cgraph_analyze_function (N)
1468 cgraph_lower_function (N) -> all_lowering_passes
1470 If we are optimizing, compile is then invoked:
1472 compile ()
1473 ipa_passes () -> all_small_ipa_passes
1474 -> Analysis of all_regular_ipa_passes
1475 * possible LTO streaming at copmilation time *
1476 -> Execution of all_regular_ipa_passes
1477 * possible LTO streaming at link time *
1478 -> all_late_ipa_passes
1479 expand_all_functions ()
1480 for each node N in the cgraph
1481 expand_function (N) -> Transformation of all_regular_ipa_passes
1482 -> all_passes
1485 void *
1486 pass_manager::operator new (size_t sz)
1488 /* Ensure that all fields of the pass manager are zero-initialized. */
1489 return xcalloc (1, sz);
1492 pass_manager::pass_manager (context *ctxt)
1493 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1494 all_regular_ipa_passes (NULL), all_lto_gen_passes (NULL),
1495 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1496 m_ctxt (ctxt)
1498 struct opt_pass **p;
1500 /* Initialize the pass_lists array. */
1501 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1502 GCC_PASS_LISTS
1503 #undef DEF_PASS_LIST
1505 /* Build the tree of passes. */
1507 #define INSERT_PASSES_AFTER(PASS) \
1508 p = &(PASS);
1510 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1512 struct opt_pass **p = &(PASS ## _1)->sub;
1514 #define POP_INSERT_PASSES() \
1517 #define NEXT_PASS(PASS, NUM) \
1518 do { \
1519 gcc_assert (NULL == PASS ## _ ## NUM); \
1520 if ((NUM) == 1) \
1521 PASS ## _1 = make_##PASS (m_ctxt); \
1522 else \
1524 gcc_assert (PASS ## _1); \
1525 PASS ## _ ## NUM = PASS ## _1->clone (); \
1527 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1528 } while (0)
1530 #define TERMINATE_PASS_LIST() \
1531 *p = NULL;
1533 #include "pass-instances.def"
1535 #undef INSERT_PASSES_AFTER
1536 #undef PUSH_INSERT_PASSES_WITHIN
1537 #undef POP_INSERT_PASSES
1538 #undef NEXT_PASS
1539 #undef TERMINATE_PASS_LIST
1541 /* Register the passes with the tree dump code. */
1542 register_dump_files (all_lowering_passes, PROP_gimple_any);
1543 register_dump_files (all_small_ipa_passes,
1544 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1545 | PROP_cfg);
1546 register_dump_files (all_regular_ipa_passes,
1547 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1548 | PROP_cfg);
1549 register_dump_files (all_lto_gen_passes,
1550 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1551 | PROP_cfg);
1552 register_dump_files (all_late_ipa_passes,
1553 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1554 | PROP_cfg);
1555 register_dump_files (all_passes,
1556 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1557 | PROP_cfg);
1560 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1561 function CALLBACK for every function in the call graph. Otherwise,
1562 call CALLBACK on the current function. */
1564 static void
1565 do_per_function (void (*callback) (void *data), void *data)
1567 if (current_function_decl)
1568 callback (data);
1569 else
1571 struct cgraph_node *node;
1572 FOR_EACH_DEFINED_FUNCTION (node)
1573 if (node->symbol.analyzed && gimple_has_body_p (node->symbol.decl)
1574 && (!node->clone_of || node->symbol.decl != node->clone_of->symbol.decl))
1576 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1577 callback (data);
1578 if (!flag_wpa)
1580 free_dominance_info (CDI_DOMINATORS);
1581 free_dominance_info (CDI_POST_DOMINATORS);
1583 pop_cfun ();
1584 ggc_collect ();
1589 /* Because inlining might remove no-longer reachable nodes, we need to
1590 keep the array visible to garbage collector to avoid reading collected
1591 out nodes. */
1592 static int nnodes;
1593 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1595 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1596 function CALLBACK for every function in the call graph. Otherwise,
1597 call CALLBACK on the current function.
1598 This function is global so that plugins can use it. */
1599 void
1600 do_per_function_toporder (void (*callback) (void *data), void *data)
1602 int i;
1604 if (current_function_decl)
1605 callback (data);
1606 else
1608 gcc_assert (!order);
1609 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1610 nnodes = ipa_reverse_postorder (order);
1611 for (i = nnodes - 1; i >= 0; i--)
1612 order[i]->process = 1;
1613 for (i = nnodes - 1; i >= 0; i--)
1615 struct cgraph_node *node = order[i];
1617 /* Allow possibly removed nodes to be garbage collected. */
1618 order[i] = NULL;
1619 node->process = 0;
1620 if (cgraph_function_with_gimple_body_p (node))
1622 cgraph_get_body (node);
1623 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1624 callback (data);
1625 free_dominance_info (CDI_DOMINATORS);
1626 free_dominance_info (CDI_POST_DOMINATORS);
1627 pop_cfun ();
1628 ggc_collect ();
1632 ggc_free (order);
1633 order = NULL;
1634 nnodes = 0;
1637 /* Helper function to perform function body dump. */
1639 static void
1640 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1642 if (dump_file && current_function_decl)
1644 if (cfun->curr_properties & PROP_trees)
1645 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1646 else
1647 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1649 /* Flush the file. If verification fails, we won't be able to
1650 close the file before aborting. */
1651 fflush (dump_file);
1653 if ((cfun->curr_properties & PROP_cfg)
1654 && (dump_flags & TDF_GRAPH))
1655 print_graph_cfg (dump_file_name, cfun);
1659 static struct profile_record *profile_record;
1661 /* Do profile consistency book-keeping for the pass with static number INDEX.
1662 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1663 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1664 if we are only book-keeping on passes that may have selectively disabled
1665 themselves on a given function. */
1666 static void
1667 check_profile_consistency (int index, int subpass, bool run)
1669 pass_manager *passes = g->get_passes ();
1670 if (index == -1)
1671 return;
1672 if (!profile_record)
1673 profile_record = XCNEWVEC (struct profile_record,
1674 passes->passes_by_id_size);
1675 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1676 gcc_assert (subpass < 2);
1677 profile_record[index].run |= run;
1678 account_profile_record (&profile_record[index], subpass);
1681 /* Output profile consistency. */
1683 void
1684 dump_profile_report (void)
1686 g->get_passes ()->dump_profile_report ();
1689 void
1690 pass_manager::dump_profile_report () const
1692 int i, j;
1693 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1694 gcov_type last_time = 0, last_size = 0;
1695 double rel_time_change, rel_size_change;
1696 int last_reported = 0;
1698 if (!profile_record)
1699 return;
1700 fprintf (stderr, "\nProfile consistency report:\n\n");
1701 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1702 fprintf (stderr, " |freq count |freq count |size time\n");
1704 for (i = 0; i < passes_by_id_size; i++)
1705 for (j = 0 ; j < 2; j++)
1706 if (profile_record[i].run)
1708 if (last_time)
1709 rel_time_change = (profile_record[i].time[j]
1710 - (double)last_time) * 100 / (double)last_time;
1711 else
1712 rel_time_change = 0;
1713 if (last_size)
1714 rel_size_change = (profile_record[i].size[j]
1715 - (double)last_size) * 100 / (double)last_size;
1716 else
1717 rel_size_change = 0;
1719 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1720 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1721 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1722 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1723 || rel_time_change || rel_size_change)
1725 last_reported = i;
1726 fprintf (stderr, "%-20s %s",
1727 passes_by_id [i]->name,
1728 j ? "(after TODO)" : " ");
1729 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1730 fprintf (stderr, "| %+5i",
1731 profile_record[i].num_mismatched_freq_in[j]
1732 - last_freq_in);
1733 else
1734 fprintf (stderr, "| ");
1735 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1736 fprintf (stderr, " %+5i",
1737 profile_record[i].num_mismatched_count_in[j]
1738 - last_count_in);
1739 else
1740 fprintf (stderr, " ");
1741 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1742 fprintf (stderr, "| %+5i",
1743 profile_record[i].num_mismatched_freq_out[j]
1744 - last_freq_out);
1745 else
1746 fprintf (stderr, "| ");
1747 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1748 fprintf (stderr, " %+5i",
1749 profile_record[i].num_mismatched_count_out[j]
1750 - last_count_out);
1751 else
1752 fprintf (stderr, " ");
1754 /* Size/time units change across gimple and RTL. */
1755 if (i == pass_expand_1->static_pass_number)
1756 fprintf (stderr, "|----------");
1757 else
1759 if (rel_size_change)
1760 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1761 else
1762 fprintf (stderr, "| ");
1763 if (rel_time_change)
1764 fprintf (stderr, " %+8.4f%%", rel_time_change);
1766 fprintf (stderr, "\n");
1767 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1768 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1769 last_count_in = profile_record[i].num_mismatched_count_in[j];
1770 last_count_out = profile_record[i].num_mismatched_count_out[j];
1772 else if (j && last_reported != i)
1774 last_reported = i;
1775 fprintf (stderr, "%-20s ------------| | |\n",
1776 passes_by_id [i]->name);
1778 last_time = profile_record[i].time[j];
1779 last_size = profile_record[i].size[j];
1783 /* Perform all TODO actions that ought to be done on each function. */
1785 static void
1786 execute_function_todo (void *data)
1788 unsigned int flags = (size_t)data;
1789 flags &= ~cfun->last_verified;
1790 if (!flags)
1791 return;
1793 /* Always cleanup the CFG before trying to update SSA. */
1794 if (flags & TODO_cleanup_cfg)
1796 cleanup_tree_cfg ();
1798 /* When cleanup_tree_cfg merges consecutive blocks, it may
1799 perform some simplistic propagation when removing single
1800 valued PHI nodes. This propagation may, in turn, cause the
1801 SSA form to become out-of-date (see PR 22037). So, even
1802 if the parent pass had not scheduled an SSA update, we may
1803 still need to do one. */
1804 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1805 flags |= TODO_update_ssa;
1808 if (flags & TODO_update_ssa_any)
1810 unsigned update_flags = flags & TODO_update_ssa_any;
1811 update_ssa (update_flags);
1812 cfun->last_verified &= ~TODO_verify_ssa;
1815 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1816 compute_may_aliases ();
1818 if (optimize && (flags & TODO_update_address_taken))
1819 execute_update_addresses_taken ();
1821 if (flags & TODO_remove_unused_locals)
1822 remove_unused_locals ();
1824 if (flags & TODO_rebuild_frequencies)
1825 rebuild_frequencies ();
1827 if (flags & TODO_rebuild_cgraph_edges)
1828 rebuild_cgraph_edges ();
1830 /* If we've seen errors do not bother running any verifiers. */
1831 if (seen_error ())
1832 return;
1834 #if defined ENABLE_CHECKING
1835 if (flags & TODO_verify_ssa
1836 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1838 verify_gimple_in_cfg (cfun);
1839 verify_ssa (true);
1841 else if (flags & TODO_verify_stmts)
1842 verify_gimple_in_cfg (cfun);
1843 if (flags & TODO_verify_flow)
1844 verify_flow_info ();
1845 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1846 verify_loop_closed_ssa (false);
1847 if (flags & TODO_verify_rtl_sharing)
1848 verify_rtl_sharing ();
1849 #endif
1851 cfun->last_verified = flags & TODO_verify_all;
1854 /* Perform all TODO actions. */
1855 static void
1856 execute_todo (unsigned int flags)
1858 #if defined ENABLE_CHECKING
1859 if (cfun
1860 && need_ssa_update_p (cfun))
1861 gcc_assert (flags & TODO_update_ssa_any);
1862 #endif
1864 timevar_push (TV_TODO);
1866 /* Inform the pass whether it is the first time it is run. */
1867 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1869 statistics_fini_pass ();
1871 do_per_function (execute_function_todo, (void *)(size_t) flags);
1873 /* Always remove functions just as before inlining: IPA passes might be
1874 interested to see bodies of extern inline functions that are not inlined
1875 to analyze side effects. The full removal is done just at the end
1876 of IPA pass queue. */
1877 if (flags & TODO_remove_functions)
1879 gcc_assert (!cfun);
1880 symtab_remove_unreachable_nodes (true, dump_file);
1883 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1885 gcc_assert (!cfun);
1886 dump_symtab (dump_file);
1887 /* Flush the file. If verification fails, we won't be able to
1888 close the file before aborting. */
1889 fflush (dump_file);
1892 /* Now that the dumping has been done, we can get rid of the optional
1893 df problems. */
1894 if (flags & TODO_df_finish)
1895 df_finish_pass ((flags & TODO_df_verify) != 0);
1897 timevar_pop (TV_TODO);
1900 /* Verify invariants that should hold between passes. This is a place
1901 to put simple sanity checks. */
1903 static void
1904 verify_interpass_invariants (void)
1906 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1909 /* Clear the last verified flag. */
1911 static void
1912 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1914 cfun->last_verified = 0;
1917 /* Helper function. Verify that the properties has been turn into the
1918 properties expected by the pass. */
1920 #ifdef ENABLE_CHECKING
1921 static void
1922 verify_curr_properties (void *data)
1924 unsigned int props = (size_t)data;
1925 gcc_assert ((cfun->curr_properties & props) == props);
1927 #endif
1929 /* Initialize pass dump file. */
1930 /* This is non-static so that the plugins can use it. */
1932 bool
1933 pass_init_dump_file (struct opt_pass *pass)
1935 /* If a dump file name is present, open it if enabled. */
1936 if (pass->static_pass_number != -1)
1938 timevar_push (TV_DUMP);
1939 gcc::dump_manager *dumps = g->get_dumps ();
1940 bool initializing_dump =
1941 !dumps->dump_initialized_p (pass->static_pass_number);
1942 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
1943 dumps->dump_start (pass->static_pass_number, &dump_flags);
1944 if (dump_file && current_function_decl)
1945 dump_function_header (dump_file, current_function_decl, dump_flags);
1946 if (initializing_dump
1947 && dump_file && (dump_flags & TDF_GRAPH)
1948 && cfun && (cfun->curr_properties & PROP_cfg))
1949 clean_graph_dump_file (dump_file_name);
1950 timevar_pop (TV_DUMP);
1951 return initializing_dump;
1953 else
1954 return false;
1957 /* Flush PASS dump file. */
1958 /* This is non-static so that plugins can use it. */
1960 void
1961 pass_fini_dump_file (struct opt_pass *pass)
1963 timevar_push (TV_DUMP);
1965 /* Flush and close dump file. */
1966 if (dump_file_name)
1968 free (CONST_CAST (char *, dump_file_name));
1969 dump_file_name = NULL;
1972 g->get_dumps ()->dump_finish (pass->static_pass_number);
1973 timevar_pop (TV_DUMP);
1976 /* After executing the pass, apply expected changes to the function
1977 properties. */
1979 static void
1980 update_properties_after_pass (void *data)
1982 struct opt_pass *pass = (struct opt_pass *) data;
1983 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1984 & ~pass->properties_destroyed;
1987 /* Execute summary generation for all of the passes in IPA_PASS. */
1989 void
1990 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1992 while (ipa_pass)
1994 struct opt_pass *pass = ipa_pass;
1996 /* Execute all of the IPA_PASSes in the list. */
1997 if (ipa_pass->type == IPA_PASS
1998 && ((!pass->has_gate) || pass->gate ())
1999 && ipa_pass->generate_summary)
2001 pass_init_dump_file (pass);
2003 /* If a timevar is present, start it. */
2004 if (pass->tv_id)
2005 timevar_push (pass->tv_id);
2007 ipa_pass->generate_summary ();
2009 /* Stop timevar. */
2010 if (pass->tv_id)
2011 timevar_pop (pass->tv_id);
2013 pass_fini_dump_file (pass);
2015 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->next;
2019 /* Execute IPA_PASS function transform on NODE. */
2021 static void
2022 execute_one_ipa_transform_pass (struct cgraph_node *node,
2023 struct ipa_opt_pass_d *ipa_pass)
2025 struct opt_pass *pass = ipa_pass;
2026 unsigned int todo_after = 0;
2028 current_pass = pass;
2029 if (!ipa_pass->function_transform)
2030 return;
2032 /* Note that the folders should only create gimple expressions.
2033 This is a hack until the new folder is ready. */
2034 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2036 pass_init_dump_file (pass);
2038 /* Run pre-pass verification. */
2039 execute_todo (ipa_pass->function_transform_todo_flags_start);
2041 /* If a timevar is present, start it. */
2042 if (pass->tv_id != TV_NONE)
2043 timevar_push (pass->tv_id);
2045 /* Do it! */
2046 todo_after = ipa_pass->function_transform (node);
2048 /* Stop timevar. */
2049 if (pass->tv_id != TV_NONE)
2050 timevar_pop (pass->tv_id);
2052 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2053 check_profile_consistency (pass->static_pass_number, 0, true);
2055 /* Run post-pass cleanup and verification. */
2056 execute_todo (todo_after);
2057 verify_interpass_invariants ();
2058 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2059 check_profile_consistency (pass->static_pass_number, 1, true);
2061 do_per_function (execute_function_dump, NULL);
2062 pass_fini_dump_file (pass);
2064 current_pass = NULL;
2066 /* Signal this is a suitable GC collection point. */
2067 if (!(todo_after & TODO_do_not_ggc_collect))
2068 ggc_collect ();
2071 /* For the current function, execute all ipa transforms. */
2073 void
2074 execute_all_ipa_transforms (void)
2076 struct cgraph_node *node;
2077 if (!cfun)
2078 return;
2079 node = cgraph_get_node (current_function_decl);
2081 if (node->ipa_transforms_to_apply.exists ())
2083 unsigned int i;
2085 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2086 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2087 node->ipa_transforms_to_apply.release ();
2091 /* Callback for do_per_function to apply all IPA transforms. */
2093 static void
2094 apply_ipa_transforms (void *data)
2096 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2097 if (!node->global.inlined_to && node->ipa_transforms_to_apply.exists ())
2099 *(bool *)data = true;
2100 execute_all_ipa_transforms ();
2101 rebuild_cgraph_edges ();
2105 /* Check if PASS is explicitly disabled or enabled and return
2106 the gate status. FUNC is the function to be processed, and
2107 GATE_STATUS is the gate status determined by pass manager by
2108 default. */
2110 static bool
2111 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2113 bool explicitly_enabled = false;
2114 bool explicitly_disabled = false;
2116 explicitly_enabled
2117 = is_pass_explicitly_enabled_or_disabled (pass, func,
2118 enabled_pass_uid_range_tab);
2119 explicitly_disabled
2120 = is_pass_explicitly_enabled_or_disabled (pass, func,
2121 disabled_pass_uid_range_tab);
2123 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2125 return gate_status;
2129 /* Execute PASS. */
2131 bool
2132 execute_one_pass (struct opt_pass *pass)
2134 unsigned int todo_after = 0;
2136 bool gate_status;
2138 /* IPA passes are executed on whole program, so cfun should be NULL.
2139 Other passes need function context set. */
2140 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2141 gcc_assert (!cfun && !current_function_decl);
2142 else
2143 gcc_assert (cfun && current_function_decl);
2145 current_pass = pass;
2147 /* Check whether gate check should be avoided.
2148 User controls the value of the gate through the parameter "gate_status". */
2149 gate_status = pass->has_gate ? pass->gate () : true;
2150 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2152 /* Override gate with plugin. */
2153 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2155 if (!gate_status)
2157 /* Run so passes selectively disabling themselves on a given function
2158 are not miscounted. */
2159 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2161 check_profile_consistency (pass->static_pass_number, 0, false);
2162 check_profile_consistency (pass->static_pass_number, 1, false);
2164 current_pass = NULL;
2165 return false;
2168 /* Pass execution event trigger: useful to identify passes being
2169 executed. */
2170 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2172 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2173 Apply all trnasforms first. */
2174 if (pass->type == SIMPLE_IPA_PASS)
2176 bool applied = false;
2177 do_per_function (apply_ipa_transforms, (void *)&applied);
2178 if (applied)
2179 symtab_remove_unreachable_nodes (true, dump_file);
2180 /* Restore current_pass. */
2181 current_pass = pass;
2184 if (!quiet_flag && !cfun)
2185 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2187 /* Note that the folders should only create gimple expressions.
2188 This is a hack until the new folder is ready. */
2189 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2191 pass_init_dump_file (pass);
2193 /* Run pre-pass verification. */
2194 execute_todo (pass->todo_flags_start);
2196 #ifdef ENABLE_CHECKING
2197 do_per_function (verify_curr_properties,
2198 (void *)(size_t)pass->properties_required);
2199 #endif
2201 /* If a timevar is present, start it. */
2202 if (pass->tv_id != TV_NONE)
2203 timevar_push (pass->tv_id);
2205 /* Do it! */
2206 if (pass->has_execute)
2208 todo_after = pass->execute ();
2209 do_per_function (clear_last_verified, NULL);
2212 /* Stop timevar. */
2213 if (pass->tv_id != TV_NONE)
2214 timevar_pop (pass->tv_id);
2216 do_per_function (update_properties_after_pass, pass);
2218 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2219 check_profile_consistency (pass->static_pass_number, 0, true);
2221 /* Run post-pass cleanup and verification. */
2222 execute_todo (todo_after | pass->todo_flags_finish);
2223 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2224 check_profile_consistency (pass->static_pass_number, 1, true);
2226 verify_interpass_invariants ();
2227 do_per_function (execute_function_dump, NULL);
2228 if (pass->type == IPA_PASS)
2230 struct cgraph_node *node;
2231 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2232 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *)pass);
2235 if (!current_function_decl)
2236 cgraph_process_new_functions ();
2238 pass_fini_dump_file (pass);
2240 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2241 gcc_assert (!(cfun->curr_properties & PROP_trees)
2242 || pass->type != RTL_PASS);
2244 current_pass = NULL;
2246 /* Signal this is a suitable GC collection point. */
2247 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2248 ggc_collect ();
2250 return true;
2253 void
2254 execute_pass_list (struct opt_pass *pass)
2258 gcc_assert (pass->type == GIMPLE_PASS
2259 || pass->type == RTL_PASS);
2260 if (execute_one_pass (pass) && pass->sub)
2261 execute_pass_list (pass->sub);
2262 pass = pass->next;
2264 while (pass);
2267 /* Same as execute_pass_list but assume that subpasses of IPA passes
2268 are local passes. If SET is not NULL, write out summaries of only
2269 those node in SET. */
2271 static void
2272 ipa_write_summaries_2 (struct opt_pass *pass, struct lto_out_decl_state *state)
2274 while (pass)
2276 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2277 gcc_assert (!current_function_decl);
2278 gcc_assert (!cfun);
2279 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2280 if (pass->type == IPA_PASS
2281 && ipa_pass->write_summary
2282 && ((!pass->has_gate) || pass->gate ()))
2284 /* If a timevar is present, start it. */
2285 if (pass->tv_id)
2286 timevar_push (pass->tv_id);
2288 pass_init_dump_file (pass);
2290 ipa_pass->write_summary ();
2292 pass_fini_dump_file (pass);
2294 /* If a timevar is present, start it. */
2295 if (pass->tv_id)
2296 timevar_pop (pass->tv_id);
2299 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2300 ipa_write_summaries_2 (pass->sub, state);
2302 pass = pass->next;
2306 /* Helper function of ipa_write_summaries. Creates and destroys the
2307 decl state and calls ipa_write_summaries_2 for all passes that have
2308 summaries. SET is the set of nodes to be written. */
2310 static void
2311 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2313 pass_manager *passes = g->get_passes ();
2314 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2315 state->symtab_node_encoder = encoder;
2317 lto_push_out_decl_state (state);
2319 gcc_assert (!flag_wpa);
2320 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2321 ipa_write_summaries_2 (passes->all_lto_gen_passes, state);
2323 gcc_assert (lto_get_out_decl_state () == state);
2324 lto_pop_out_decl_state ();
2325 lto_delete_out_decl_state (state);
2328 /* Write out summaries for all the nodes in the callgraph. */
2330 void
2331 ipa_write_summaries (void)
2333 lto_symtab_encoder_t encoder;
2334 int i, order_pos;
2335 struct varpool_node *vnode;
2336 struct cgraph_node *node;
2337 struct cgraph_node **order;
2339 if (!flag_generate_lto || seen_error ())
2340 return;
2342 encoder = lto_symtab_encoder_new (false);
2344 /* Create the callgraph set in the same order used in
2345 cgraph_expand_all_functions. This mostly facilitates debugging,
2346 since it causes the gimple file to be processed in the same order
2347 as the source code. */
2348 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2349 order_pos = ipa_reverse_postorder (order);
2350 gcc_assert (order_pos == cgraph_n_nodes);
2352 for (i = order_pos - 1; i >= 0; i--)
2354 struct cgraph_node *node = order[i];
2356 if (cgraph_function_with_gimple_body_p (node))
2358 /* When streaming out references to statements as part of some IPA
2359 pass summary, the statements need to have uids assigned and the
2360 following does that for all the IPA passes here. Naturally, this
2361 ordering then matches the one IPA-passes get in their stmt_fixup
2362 hooks. */
2364 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2365 renumber_gimple_stmt_uids ();
2366 pop_cfun ();
2368 if (node->symbol.definition)
2369 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
2372 FOR_EACH_DEFINED_FUNCTION (node)
2373 if (node->symbol.alias)
2374 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
2375 FOR_EACH_DEFINED_VARIABLE (vnode)
2376 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
2378 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2380 free (order);
2383 /* Same as execute_pass_list but assume that subpasses of IPA passes
2384 are local passes. If SET is not NULL, write out optimization summaries of
2385 only those node in SET. */
2387 static void
2388 ipa_write_optimization_summaries_1 (struct opt_pass *pass, struct lto_out_decl_state *state)
2390 while (pass)
2392 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2393 gcc_assert (!current_function_decl);
2394 gcc_assert (!cfun);
2395 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2396 if (pass->type == IPA_PASS
2397 && ipa_pass->write_optimization_summary
2398 && ((!pass->has_gate) || pass->gate ()))
2400 /* If a timevar is present, start it. */
2401 if (pass->tv_id)
2402 timevar_push (pass->tv_id);
2404 pass_init_dump_file (pass);
2406 ipa_pass->write_optimization_summary ();
2408 pass_fini_dump_file (pass);
2410 /* If a timevar is present, start it. */
2411 if (pass->tv_id)
2412 timevar_pop (pass->tv_id);
2415 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2416 ipa_write_optimization_summaries_1 (pass->sub, state);
2418 pass = pass->next;
2422 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2423 NULL, write out all summaries of all nodes. */
2425 void
2426 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2428 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2429 lto_symtab_encoder_iterator lsei;
2430 state->symtab_node_encoder = encoder;
2432 lto_push_out_decl_state (state);
2433 for (lsei = lsei_start_function_in_partition (encoder);
2434 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2436 struct cgraph_node *node = lsei_cgraph_node (lsei);
2437 /* When streaming out references to statements as part of some IPA
2438 pass summary, the statements need to have uids assigned.
2440 For functions newly born at WPA stage we need to initialize
2441 the uids here. */
2442 if (node->symbol.definition
2443 && gimple_has_body_p (node->symbol.decl))
2445 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2446 renumber_gimple_stmt_uids ();
2447 pop_cfun ();
2451 gcc_assert (flag_wpa);
2452 pass_manager *passes = g->get_passes ();
2453 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2454 ipa_write_optimization_summaries_1 (passes->all_lto_gen_passes, state);
2456 gcc_assert (lto_get_out_decl_state () == state);
2457 lto_pop_out_decl_state ();
2458 lto_delete_out_decl_state (state);
2461 /* Same as execute_pass_list but assume that subpasses of IPA passes
2462 are local passes. */
2464 static void
2465 ipa_read_summaries_1 (struct opt_pass *pass)
2467 while (pass)
2469 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2471 gcc_assert (!current_function_decl);
2472 gcc_assert (!cfun);
2473 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2475 if ((!pass->has_gate) || pass->gate ())
2477 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2479 /* If a timevar is present, start it. */
2480 if (pass->tv_id)
2481 timevar_push (pass->tv_id);
2483 pass_init_dump_file (pass);
2485 ipa_pass->read_summary ();
2487 pass_fini_dump_file (pass);
2489 /* Stop timevar. */
2490 if (pass->tv_id)
2491 timevar_pop (pass->tv_id);
2494 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2495 ipa_read_summaries_1 (pass->sub);
2497 pass = pass->next;
2502 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2504 void
2505 ipa_read_summaries (void)
2507 pass_manager *passes = g->get_passes ();
2508 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2509 ipa_read_summaries_1 (passes->all_lto_gen_passes);
2512 /* Same as execute_pass_list but assume that subpasses of IPA passes
2513 are local passes. */
2515 static void
2516 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2518 while (pass)
2520 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2522 gcc_assert (!current_function_decl);
2523 gcc_assert (!cfun);
2524 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2526 if ((!pass->has_gate) || pass->gate ())
2528 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2530 /* If a timevar is present, start it. */
2531 if (pass->tv_id)
2532 timevar_push (pass->tv_id);
2534 pass_init_dump_file (pass);
2536 ipa_pass->read_optimization_summary ();
2538 pass_fini_dump_file (pass);
2540 /* Stop timevar. */
2541 if (pass->tv_id)
2542 timevar_pop (pass->tv_id);
2545 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2546 ipa_read_optimization_summaries_1 (pass->sub);
2548 pass = pass->next;
2552 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2554 void
2555 ipa_read_optimization_summaries (void)
2557 pass_manager *passes = g->get_passes ();
2558 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2559 ipa_read_optimization_summaries_1 (passes->all_lto_gen_passes);
2562 /* Same as execute_pass_list but assume that subpasses of IPA passes
2563 are local passes. */
2564 void
2565 execute_ipa_pass_list (struct opt_pass *pass)
2569 gcc_assert (!current_function_decl);
2570 gcc_assert (!cfun);
2571 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2572 if (execute_one_pass (pass) && pass->sub)
2574 if (pass->sub->type == GIMPLE_PASS)
2576 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2577 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2578 pass->sub);
2579 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2581 else if (pass->sub->type == SIMPLE_IPA_PASS
2582 || pass->sub->type == IPA_PASS)
2583 execute_ipa_pass_list (pass->sub);
2584 else
2585 gcc_unreachable ();
2587 gcc_assert (!current_function_decl);
2588 cgraph_process_new_functions ();
2589 pass = pass->next;
2591 while (pass);
2594 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2596 static void
2597 execute_ipa_stmt_fixups (struct opt_pass *pass,
2598 struct cgraph_node *node, gimple *stmts)
2600 while (pass)
2602 /* Execute all of the IPA_PASSes in the list. */
2603 if (pass->type == IPA_PASS
2604 && ((!pass->has_gate) || pass->gate ()))
2606 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2608 if (ipa_pass->stmt_fixup)
2610 pass_init_dump_file (pass);
2611 /* If a timevar is present, start it. */
2612 if (pass->tv_id)
2613 timevar_push (pass->tv_id);
2615 ipa_pass->stmt_fixup (node, stmts);
2617 /* Stop timevar. */
2618 if (pass->tv_id)
2619 timevar_pop (pass->tv_id);
2620 pass_fini_dump_file (pass);
2622 if (pass->sub)
2623 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2625 pass = pass->next;
2629 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2631 void
2632 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2634 pass_manager *passes = g->get_passes ();
2635 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2639 extern void debug_properties (unsigned int);
2640 extern void dump_properties (FILE *, unsigned int);
2642 DEBUG_FUNCTION void
2643 dump_properties (FILE *dump, unsigned int props)
2645 fprintf (dump, "Properties:\n");
2646 if (props & PROP_gimple_any)
2647 fprintf (dump, "PROP_gimple_any\n");
2648 if (props & PROP_gimple_lcf)
2649 fprintf (dump, "PROP_gimple_lcf\n");
2650 if (props & PROP_gimple_leh)
2651 fprintf (dump, "PROP_gimple_leh\n");
2652 if (props & PROP_cfg)
2653 fprintf (dump, "PROP_cfg\n");
2654 if (props & PROP_ssa)
2655 fprintf (dump, "PROP_ssa\n");
2656 if (props & PROP_no_crit_edges)
2657 fprintf (dump, "PROP_no_crit_edges\n");
2658 if (props & PROP_rtl)
2659 fprintf (dump, "PROP_rtl\n");
2660 if (props & PROP_gimple_lomp)
2661 fprintf (dump, "PROP_gimple_lomp\n");
2662 if (props & PROP_gimple_lcx)
2663 fprintf (dump, "PROP_gimple_lcx\n");
2664 if (props & PROP_gimple_lvec)
2665 fprintf (dump, "PROP_gimple_lvec\n");
2666 if (props & PROP_cfglayout)
2667 fprintf (dump, "PROP_cfglayout\n");
2670 DEBUG_FUNCTION void
2671 debug_properties (unsigned int props)
2673 dump_properties (stderr, props);
2676 /* Called by local passes to see if function is called by already processed nodes.
2677 Because we process nodes in topological order, this means that function is
2678 in recursive cycle or we introduced new direct calls. */
2679 bool
2680 function_called_by_processed_nodes_p (void)
2682 struct cgraph_edge *e;
2683 for (e = cgraph_get_node (current_function_decl)->callers;
2685 e = e->next_caller)
2687 if (e->caller->symbol.decl == current_function_decl)
2688 continue;
2689 if (!cgraph_function_with_gimple_body_p (e->caller))
2690 continue;
2691 if (TREE_ASM_WRITTEN (e->caller->symbol.decl))
2692 continue;
2693 if (!e->caller->process && !e->caller->global.inlined_to)
2694 break;
2696 if (dump_file && e)
2698 fprintf (dump_file, "Already processed call to:\n");
2699 dump_cgraph_node (dump_file, e->caller);
2701 return e != NULL;
2704 #include "gt-passes.h"