tree-ssa-threadupdate.c: Include tree-cfg.h and tree-pass.h
[official-gcc.git] / gcc / passes.c
blobfee1513b4042293927328c963d585c6815a50831
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 "varasm.h"
34 #include "rtl.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "insn-attr.h"
38 #include "insn-config.h"
39 #include "insn-flags.h"
40 #include "hard-reg-set.h"
41 #include "recog.h"
42 #include "output.h"
43 #include "except.h"
44 #include "function.h"
45 #include "toplev.h"
46 #include "expr.h"
47 #include "basic-block.h"
48 #include "intl.h"
49 #include "ggc.h"
50 #include "graph.h"
51 #include "regs.h"
52 #include "diagnostic-core.h"
53 #include "params.h"
54 #include "reload.h"
55 #include "debug.h"
56 #include "target.h"
57 #include "langhooks.h"
58 #include "cfgloop.h"
59 #include "hosthooks.h"
60 #include "opts.h"
61 #include "coverage.h"
62 #include "value-prof.h"
63 #include "tree-inline.h"
64 #include "gimple.h"
65 #include "gimple-ssa.h"
66 #include "tree-cfg.h"
67 #include "stringpool.h"
68 #include "tree-ssanames.h"
69 #include "tree-ssa-loop-manip.h"
70 #include "tree-into-ssa.h"
71 #include "tree-dfa.h"
72 #include "tree-ssa.h"
73 #include "tree-pass.h"
74 #include "tree-dump.h"
75 #include "df.h"
76 #include "predict.h"
77 #include "lto-streamer.h"
78 #include "plugin.h"
79 #include "ipa-utils.h"
80 #include "tree-pretty-print.h" /* for dump_function_header */
81 #include "context.h"
82 #include "pass_manager.h"
83 #include "tree-ssa-live.h" /* For remove_unused_locals. */
84 #include "tree-cfgcleanup.h"
86 using namespace gcc;
88 /* This is used for debugging. It allows the current pass to printed
89 from anywhere in compilation.
90 The variable current_pass is also used for statistics and plugins. */
91 struct opt_pass *current_pass;
93 static void register_pass_name (struct opt_pass *, const char *);
95 /* Most passes are single-instance (within their context) and thus don't
96 need to implement cloning, but passes that support multiple instances
97 *must* provide their own implementation of the clone method.
99 Handle this by providing a default implemenation, but make it a fatal
100 error to call it. */
102 opt_pass *
103 opt_pass::clone ()
105 internal_error ("pass %s does not support cloning", name);
108 bool
109 opt_pass::gate ()
111 return true;
114 unsigned int
115 opt_pass::execute ()
117 return 0;
120 opt_pass::opt_pass (const pass_data &data, context *ctxt)
121 : pass_data (data),
122 sub (NULL),
123 next (NULL),
124 static_pass_number (0),
125 m_ctxt (ctxt)
130 void
131 pass_manager::execute_early_local_passes ()
133 execute_pass_list (pass_early_local_passes_1->sub);
136 unsigned int
137 pass_manager::execute_pass_mode_switching ()
139 return pass_mode_switching_1->execute ();
143 /* Call from anywhere to find out what pass this is. Useful for
144 printing out debugging information deep inside an service
145 routine. */
146 void
147 print_current_pass (FILE *file)
149 if (current_pass)
150 fprintf (file, "current pass = %s (%d)\n",
151 current_pass->name, current_pass->static_pass_number);
152 else
153 fprintf (file, "no current pass.\n");
157 /* Call from the debugger to get the current pass name. */
158 DEBUG_FUNCTION void
159 debug_pass (void)
161 print_current_pass (stderr);
166 /* Global variables used to communicate with passes. */
167 bool in_gimple_form;
168 bool first_pass_instance;
171 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
172 and TYPE_DECL nodes.
174 This does nothing for local (non-static) variables, unless the
175 variable is a register variable with DECL_ASSEMBLER_NAME set. In
176 that case, or if the variable is not an automatic, it sets up the
177 RTL and outputs any assembler code (label definition, storage
178 allocation and initialization).
180 DECL is the declaration. TOP_LEVEL is nonzero
181 if this declaration is not within a function. */
183 void
184 rest_of_decl_compilation (tree decl,
185 int top_level,
186 int at_end)
188 /* We deferred calling assemble_alias so that we could collect
189 other attributes such as visibility. Emit the alias now. */
190 if (!in_lto_p)
192 tree alias;
193 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
194 if (alias)
196 alias = TREE_VALUE (TREE_VALUE (alias));
197 alias = get_identifier (TREE_STRING_POINTER (alias));
198 /* A quirk of the initial implementation of aliases required that the
199 user add "extern" to all of them. Which is silly, but now
200 historical. Do note that the symbol is in fact locally defined. */
201 DECL_EXTERNAL (decl) = 0;
202 TREE_STATIC (decl) = 1;
203 assemble_alias (decl, alias);
207 /* Can't defer this, because it needs to happen before any
208 later function definitions are processed. */
209 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
210 make_decl_rtl (decl);
212 /* Forward declarations for nested functions are not "external",
213 but we need to treat them as if they were. */
214 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
215 || TREE_CODE (decl) == FUNCTION_DECL)
217 timevar_push (TV_VARCONST);
219 /* Don't output anything when a tentative file-scope definition
220 is seen. But at end of compilation, do output code for them.
222 We do output all variables and rely on
223 callgraph code to defer them except for forward declarations
224 (see gcc.c-torture/compile/920624-1.c) */
225 if ((at_end
226 || !DECL_DEFER_OUTPUT (decl)
227 || DECL_INITIAL (decl))
228 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
229 && !DECL_EXTERNAL (decl))
231 /* When reading LTO unit, we also read varpool, so do not
232 rebuild it. */
233 if (in_lto_p && !at_end)
235 else if (TREE_CODE (decl) != FUNCTION_DECL)
236 varpool_finalize_decl (decl);
239 #ifdef ASM_FINISH_DECLARE_OBJECT
240 if (decl == last_assemble_variable_decl)
242 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
243 top_level, at_end);
245 #endif
247 timevar_pop (TV_VARCONST);
249 else if (TREE_CODE (decl) == TYPE_DECL
250 /* Like in rest_of_type_compilation, avoid confusing the debug
251 information machinery when there are errors. */
252 && !seen_error ())
254 timevar_push (TV_SYMOUT);
255 debug_hooks->type_decl (decl, !top_level);
256 timevar_pop (TV_SYMOUT);
259 /* Let cgraph know about the existence of variables. */
260 if (in_lto_p && !at_end)
262 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
263 && TREE_STATIC (decl))
264 varpool_node_for_decl (decl);
267 /* Called after finishing a record, union or enumeral type. */
269 void
270 rest_of_type_compilation (tree type, int toplev)
272 /* Avoid confusing the debug information machinery when there are
273 errors. */
274 if (seen_error ())
275 return;
277 timevar_push (TV_SYMOUT);
278 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
279 timevar_pop (TV_SYMOUT);
284 void
285 pass_manager::
286 finish_optimization_passes (void)
288 int i;
289 struct dump_file_info *dfi;
290 char *name;
291 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
293 timevar_push (TV_DUMP);
294 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
296 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
297 end_branch_prob ();
298 dumps->dump_finish (pass_profile_1->static_pass_number);
301 if (optimize > 0)
303 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
304 print_combine_total_stats ();
305 dumps->dump_finish (pass_profile_1->static_pass_number);
308 /* Do whatever is necessary to finish printing the graphs. */
309 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
310 if (dumps->dump_initialized_p (i)
311 && (dfi->pflags & TDF_GRAPH) != 0
312 && (name = dumps->get_dump_file_name (i)) != NULL)
314 finish_graph_dump_file (name);
315 free (name);
318 timevar_pop (TV_DUMP);
321 static unsigned int
322 execute_all_early_local_passes (void)
324 /* Once this pass (and its sub-passes) are complete, all functions
325 will be in SSA form. Technically this state change is happening
326 a tad early, since the sub-passes have not yet run, but since
327 none of the sub-passes are IPA passes and do not create new
328 functions, this is ok. We're setting this value for the benefit
329 of IPA passes that follow. */
330 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
331 cgraph_state = CGRAPH_STATE_IPA_SSA;
332 return 0;
335 /* Gate: execute, or not, all of the non-trivial optimizations. */
337 static bool
338 gate_all_early_local_passes (void)
340 /* Don't bother doing anything if the program has errors. */
341 return (!seen_error () && !in_lto_p);
344 namespace {
346 const pass_data pass_data_early_local_passes =
348 SIMPLE_IPA_PASS, /* type */
349 "early_local_cleanups", /* name */
350 OPTGROUP_NONE, /* optinfo_flags */
351 true, /* has_gate */
352 true, /* has_execute */
353 TV_EARLY_LOCAL, /* tv_id */
354 0, /* properties_required */
355 0, /* properties_provided */
356 0, /* properties_destroyed */
357 0, /* todo_flags_start */
358 TODO_remove_functions, /* todo_flags_finish */
361 class pass_early_local_passes : public simple_ipa_opt_pass
363 public:
364 pass_early_local_passes (gcc::context *ctxt)
365 : simple_ipa_opt_pass (pass_data_early_local_passes, ctxt)
368 /* opt_pass methods: */
369 bool gate () { return gate_all_early_local_passes (); }
370 unsigned int execute () { return execute_all_early_local_passes (); }
372 }; // class pass_early_local_passes
374 } // anon namespace
376 simple_ipa_opt_pass *
377 make_pass_early_local_passes (gcc::context *ctxt)
379 return new pass_early_local_passes (ctxt);
382 /* Gate: execute, or not, all of the non-trivial optimizations. */
384 static bool
385 gate_all_early_optimizations (void)
387 return (optimize >= 1
388 /* Don't bother doing anything if the program has errors. */
389 && !seen_error ());
392 namespace {
394 const pass_data pass_data_all_early_optimizations =
396 GIMPLE_PASS, /* type */
397 "early_optimizations", /* name */
398 OPTGROUP_NONE, /* optinfo_flags */
399 true, /* has_gate */
400 false, /* has_execute */
401 TV_NONE, /* tv_id */
402 0, /* properties_required */
403 0, /* properties_provided */
404 0, /* properties_destroyed */
405 0, /* todo_flags_start */
406 0, /* todo_flags_finish */
409 class pass_all_early_optimizations : public gimple_opt_pass
411 public:
412 pass_all_early_optimizations (gcc::context *ctxt)
413 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
416 /* opt_pass methods: */
417 bool gate () { return gate_all_early_optimizations (); }
419 }; // class pass_all_early_optimizations
421 } // anon namespace
423 static gimple_opt_pass *
424 make_pass_all_early_optimizations (gcc::context *ctxt)
426 return new pass_all_early_optimizations (ctxt);
429 /* Gate: execute, or not, all of the non-trivial optimizations. */
431 static bool
432 gate_all_optimizations (void)
434 return optimize >= 1 && !optimize_debug;
437 namespace {
439 const pass_data pass_data_all_optimizations =
441 GIMPLE_PASS, /* type */
442 "*all_optimizations", /* name */
443 OPTGROUP_NONE, /* optinfo_flags */
444 true, /* has_gate */
445 false, /* has_execute */
446 TV_OPTIMIZE, /* tv_id */
447 0, /* properties_required */
448 0, /* properties_provided */
449 0, /* properties_destroyed */
450 0, /* todo_flags_start */
451 0, /* todo_flags_finish */
454 class pass_all_optimizations : public gimple_opt_pass
456 public:
457 pass_all_optimizations (gcc::context *ctxt)
458 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
461 /* opt_pass methods: */
462 bool gate () { return gate_all_optimizations (); }
464 }; // class pass_all_optimizations
466 } // anon namespace
468 static gimple_opt_pass *
469 make_pass_all_optimizations (gcc::context *ctxt)
471 return new pass_all_optimizations (ctxt);
474 /* Gate: execute, or not, all of the non-trivial optimizations. */
476 static bool
477 gate_all_optimizations_g (void)
479 return optimize >= 1 && optimize_debug;
482 namespace {
484 const pass_data pass_data_all_optimizations_g =
486 GIMPLE_PASS, /* type */
487 "*all_optimizations_g", /* name */
488 OPTGROUP_NONE, /* optinfo_flags */
489 true, /* has_gate */
490 false, /* has_execute */
491 TV_OPTIMIZE, /* tv_id */
492 0, /* properties_required */
493 0, /* properties_provided */
494 0, /* properties_destroyed */
495 0, /* todo_flags_start */
496 0, /* todo_flags_finish */
499 class pass_all_optimizations_g : public gimple_opt_pass
501 public:
502 pass_all_optimizations_g (gcc::context *ctxt)
503 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
506 /* opt_pass methods: */
507 bool gate () { return gate_all_optimizations_g (); }
509 }; // class pass_all_optimizations_g
511 } // anon namespace
513 static gimple_opt_pass *
514 make_pass_all_optimizations_g (gcc::context *ctxt)
516 return new pass_all_optimizations_g (ctxt);
519 static bool
520 gate_rest_of_compilation (void)
522 /* Early return if there were errors. We can run afoul of our
523 consistency checks, and there's not really much point in fixing them. */
524 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
527 namespace {
529 const pass_data pass_data_rest_of_compilation =
531 RTL_PASS, /* type */
532 "*rest_of_compilation", /* name */
533 OPTGROUP_NONE, /* optinfo_flags */
534 true, /* has_gate */
535 false, /* has_execute */
536 TV_REST_OF_COMPILATION, /* tv_id */
537 PROP_rtl, /* properties_required */
538 0, /* properties_provided */
539 0, /* properties_destroyed */
540 0, /* todo_flags_start */
541 0, /* todo_flags_finish */
544 class pass_rest_of_compilation : public rtl_opt_pass
546 public:
547 pass_rest_of_compilation (gcc::context *ctxt)
548 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
551 /* opt_pass methods: */
552 bool gate () { return gate_rest_of_compilation (); }
554 }; // class pass_rest_of_compilation
556 } // anon namespace
558 static rtl_opt_pass *
559 make_pass_rest_of_compilation (gcc::context *ctxt)
561 return new pass_rest_of_compilation (ctxt);
564 static bool
565 gate_postreload (void)
567 return reload_completed;
570 namespace {
572 const pass_data pass_data_postreload =
574 RTL_PASS, /* type */
575 "*all-postreload", /* name */
576 OPTGROUP_NONE, /* optinfo_flags */
577 true, /* has_gate */
578 false, /* has_execute */
579 TV_POSTRELOAD, /* tv_id */
580 PROP_rtl, /* properties_required */
581 0, /* properties_provided */
582 0, /* properties_destroyed */
583 0, /* todo_flags_start */
584 TODO_verify_rtl_sharing, /* todo_flags_finish */
587 class pass_postreload : public rtl_opt_pass
589 public:
590 pass_postreload (gcc::context *ctxt)
591 : rtl_opt_pass (pass_data_postreload, ctxt)
594 /* opt_pass methods: */
595 bool gate () { return gate_postreload (); }
597 }; // class pass_postreload
599 } // anon namespace
601 static rtl_opt_pass *
602 make_pass_postreload (gcc::context *ctxt)
604 return new pass_postreload (ctxt);
609 /* Set the static pass number of pass PASS to ID and record that
610 in the mapping from static pass number to pass. */
612 void
613 pass_manager::
614 set_pass_for_id (int id, struct opt_pass *pass)
616 pass->static_pass_number = id;
617 if (passes_by_id_size <= id)
619 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
620 memset (passes_by_id + passes_by_id_size, 0,
621 (id + 1 - passes_by_id_size) * sizeof (void *));
622 passes_by_id_size = id + 1;
624 passes_by_id[id] = pass;
627 /* Return the pass with the static pass number ID. */
629 struct opt_pass *
630 pass_manager::get_pass_for_id (int id) const
632 if (id >= passes_by_id_size)
633 return NULL;
634 return passes_by_id[id];
637 /* Iterate over the pass tree allocating dump file numbers. We want
638 to do this depth first, and independent of whether the pass is
639 enabled or not. */
641 void
642 register_one_dump_file (struct opt_pass *pass)
644 g->get_passes ()->register_one_dump_file (pass);
647 void
648 pass_manager::register_one_dump_file (struct opt_pass *pass)
650 char *dot_name, *flag_name, *glob_name;
651 const char *name, *full_name, *prefix;
652 char num[10];
653 int flags, id;
654 int optgroup_flags = OPTGROUP_NONE;
655 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
657 /* See below in next_pass_1. */
658 num[0] = '\0';
659 if (pass->static_pass_number != -1)
660 sprintf (num, "%d", ((int) pass->static_pass_number < 0
661 ? 1 : pass->static_pass_number));
663 /* The name is both used to identify the pass for the purposes of plugins,
664 and to specify dump file name and option.
665 The latter two might want something short which is not quite unique; for
666 that reason, we may have a disambiguating prefix, followed by a space
667 to mark the start of the following dump file name / option string. */
668 name = strchr (pass->name, ' ');
669 name = name ? name + 1 : pass->name;
670 dot_name = concat (".", name, num, NULL);
671 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
673 prefix = "ipa-";
674 flags = TDF_IPA;
675 optgroup_flags |= OPTGROUP_IPA;
677 else if (pass->type == GIMPLE_PASS)
679 prefix = "tree-";
680 flags = TDF_TREE;
682 else
684 prefix = "rtl-";
685 flags = TDF_RTL;
688 flag_name = concat (prefix, name, num, NULL);
689 glob_name = concat (prefix, name, NULL);
690 optgroup_flags |= pass->optinfo_flags;
691 /* For any passes that do not have an optgroup set, and which are not
692 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
693 any dump messages are emitted properly under -fopt-info(-optall). */
694 if (optgroup_flags == OPTGROUP_NONE)
695 optgroup_flags = OPTGROUP_OTHER;
696 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
697 optgroup_flags);
698 set_pass_for_id (id, pass);
699 full_name = concat (prefix, pass->name, num, NULL);
700 register_pass_name (pass, full_name);
701 free (CONST_CAST (char *, full_name));
704 /* Recursive worker function for register_dump_files. */
707 pass_manager::
708 register_dump_files_1 (struct opt_pass *pass, int properties)
712 int new_properties = (properties | pass->properties_provided)
713 & ~pass->properties_destroyed;
715 if (pass->name && pass->name[0] != '*')
716 register_one_dump_file (pass);
718 if (pass->sub)
719 new_properties = register_dump_files_1 (pass->sub, new_properties);
721 /* If we have a gate, combine the properties that we could have with
722 and without the pass being examined. */
723 if (pass->has_gate)
724 properties &= new_properties;
725 else
726 properties = new_properties;
728 pass = pass->next;
730 while (pass);
732 return properties;
735 /* Register the dump files for the pass_manager starting at PASS.
736 PROPERTIES reflects the properties that are guaranteed to be available at
737 the beginning of the pipeline. */
739 void
740 pass_manager::
741 register_dump_files (struct opt_pass *pass,int properties)
743 pass->properties_required |= properties;
744 register_dump_files_1 (pass, properties);
747 struct pass_registry
749 const char* unique_name;
750 struct opt_pass *pass;
753 /* Helper for pass_registry hash table. */
755 struct pass_registry_hasher : typed_noop_remove <pass_registry>
757 typedef pass_registry value_type;
758 typedef pass_registry compare_type;
759 static inline hashval_t hash (const value_type *);
760 static inline bool equal (const value_type *, const compare_type *);
763 /* Pass registry hash function. */
765 inline hashval_t
766 pass_registry_hasher::hash (const value_type *s)
768 return htab_hash_string (s->unique_name);
771 /* Hash equal function */
773 inline bool
774 pass_registry_hasher::equal (const value_type *s1, const compare_type *s2)
776 return !strcmp (s1->unique_name, s2->unique_name);
779 static hash_table <pass_registry_hasher> name_to_pass_map;
781 /* Register PASS with NAME. */
783 static void
784 register_pass_name (struct opt_pass *pass, const char *name)
786 struct pass_registry **slot;
787 struct pass_registry pr;
789 if (!name_to_pass_map.is_created ())
790 name_to_pass_map.create (256);
792 pr.unique_name = name;
793 slot = name_to_pass_map.find_slot (&pr, INSERT);
794 if (!*slot)
796 struct pass_registry *new_pr;
798 new_pr = XCNEW (struct pass_registry);
799 new_pr->unique_name = xstrdup (name);
800 new_pr->pass = pass;
801 *slot = new_pr;
803 else
804 return; /* Ignore plugin passes. */
807 /* Map from pass id to canonicalized pass name. */
809 typedef const char *char_ptr;
810 static vec<char_ptr> pass_tab = vNULL;
812 /* Callback function for traversing NAME_TO_PASS_MAP. */
815 passes_pass_traverse (pass_registry **p, void *data ATTRIBUTE_UNUSED)
817 struct opt_pass *pass = (*p)->pass;
819 gcc_assert (pass->static_pass_number > 0);
820 gcc_assert (pass_tab.exists ());
822 pass_tab[pass->static_pass_number] = (*p)->unique_name;
824 return 1;
827 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
828 table for dumping purpose. */
830 static void
831 create_pass_tab (void)
833 if (!flag_dump_passes)
834 return;
836 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
837 name_to_pass_map.traverse <void *, passes_pass_traverse> (NULL);
840 static bool override_gate_status (struct opt_pass *, tree, bool);
842 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
843 is turned on or not. */
845 static void
846 dump_one_pass (struct opt_pass *pass, int pass_indent)
848 int indent = 3 * pass_indent;
849 const char *pn;
850 bool is_on, is_really_on;
852 is_on = pass->has_gate ? pass->gate () : true;
853 is_really_on = override_gate_status (pass, current_function_decl, is_on);
855 if (pass->static_pass_number <= 0)
856 pn = pass->name;
857 else
858 pn = pass_tab[pass->static_pass_number];
860 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
861 (15 - indent < 0 ? 0 : 15 - indent), " ",
862 is_on ? " ON" : " OFF",
863 ((!is_on) == (!is_really_on) ? ""
864 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
867 /* Dump pass list PASS with indentation INDENT. */
869 static void
870 dump_pass_list (struct opt_pass *pass, int indent)
874 dump_one_pass (pass, indent);
875 if (pass->sub)
876 dump_pass_list (pass->sub, indent + 1);
877 pass = pass->next;
879 while (pass);
882 /* Dump all optimization passes. */
884 void
885 dump_passes (void)
887 g->get_passes ()->dump_passes ();
890 void
891 pass_manager::dump_passes () const
893 struct cgraph_node *n, *node = NULL;
895 create_pass_tab ();
897 FOR_EACH_FUNCTION (n)
898 if (DECL_STRUCT_FUNCTION (n->decl))
900 node = n;
901 break;
904 if (!node)
905 return;
907 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
909 dump_pass_list (all_lowering_passes, 1);
910 dump_pass_list (all_small_ipa_passes, 1);
911 dump_pass_list (all_regular_ipa_passes, 1);
912 dump_pass_list (all_late_ipa_passes, 1);
913 dump_pass_list (all_passes, 1);
915 pop_cfun ();
919 /* Returns the pass with NAME. */
921 static struct opt_pass *
922 get_pass_by_name (const char *name)
924 struct pass_registry **slot, pr;
926 pr.unique_name = name;
927 slot = name_to_pass_map.find_slot (&pr, NO_INSERT);
929 if (!slot || !*slot)
930 return NULL;
932 return (*slot)->pass;
936 /* Range [start, last]. */
938 struct uid_range
940 unsigned int start;
941 unsigned int last;
942 const char *assem_name;
943 struct uid_range *next;
946 typedef struct uid_range *uid_range_p;
949 static vec<uid_range_p>
950 enabled_pass_uid_range_tab = vNULL;
951 static vec<uid_range_p>
952 disabled_pass_uid_range_tab = vNULL;
955 /* Parse option string for -fdisable- and -fenable-
956 The syntax of the options:
958 -fenable-<pass_name>
959 -fdisable-<pass_name>
961 -fenable-<pass_name>=s1:e1,s2:e2,...
962 -fdisable-<pass_name>=s1:e1,s2:e2,...
965 static void
966 enable_disable_pass (const char *arg, bool is_enable)
968 struct opt_pass *pass;
969 char *range_str, *phase_name;
970 char *argstr = xstrdup (arg);
971 vec<uid_range_p> *tab = 0;
973 range_str = strchr (argstr,'=');
974 if (range_str)
976 *range_str = '\0';
977 range_str++;
980 phase_name = argstr;
981 if (!*phase_name)
983 if (is_enable)
984 error ("unrecognized option -fenable");
985 else
986 error ("unrecognized option -fdisable");
987 free (argstr);
988 return;
990 pass = get_pass_by_name (phase_name);
991 if (!pass || pass->static_pass_number == -1)
993 if (is_enable)
994 error ("unknown pass %s specified in -fenable", phase_name);
995 else
996 error ("unknown pass %s specified in -fdisable", phase_name);
997 free (argstr);
998 return;
1001 if (is_enable)
1002 tab = &enabled_pass_uid_range_tab;
1003 else
1004 tab = &disabled_pass_uid_range_tab;
1006 if ((unsigned) pass->static_pass_number >= tab->length ())
1007 tab->safe_grow_cleared (pass->static_pass_number + 1);
1009 if (!range_str)
1011 uid_range_p slot;
1012 uid_range_p new_range = XCNEW (struct uid_range);
1014 new_range->start = 0;
1015 new_range->last = (unsigned)-1;
1017 slot = (*tab)[pass->static_pass_number];
1018 new_range->next = slot;
1019 (*tab)[pass->static_pass_number] = new_range;
1020 if (is_enable)
1021 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1022 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1023 else
1024 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1025 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1027 else
1029 char *next_range = NULL;
1030 char *one_range = range_str;
1031 char *end_val = NULL;
1035 uid_range_p slot;
1036 uid_range_p new_range;
1037 char *invalid = NULL;
1038 long start;
1039 char *func_name = NULL;
1041 next_range = strchr (one_range, ',');
1042 if (next_range)
1044 *next_range = '\0';
1045 next_range++;
1048 end_val = strchr (one_range, ':');
1049 if (end_val)
1051 *end_val = '\0';
1052 end_val++;
1054 start = strtol (one_range, &invalid, 10);
1055 if (*invalid || start < 0)
1057 if (end_val || (one_range[0] >= '0'
1058 && one_range[0] <= '9'))
1060 error ("Invalid range %s in option %s",
1061 one_range,
1062 is_enable ? "-fenable" : "-fdisable");
1063 free (argstr);
1064 return;
1066 func_name = one_range;
1068 if (!end_val)
1070 new_range = XCNEW (struct uid_range);
1071 if (!func_name)
1073 new_range->start = (unsigned) start;
1074 new_range->last = (unsigned) start;
1076 else
1078 new_range->start = (unsigned) -1;
1079 new_range->last = (unsigned) -1;
1080 new_range->assem_name = xstrdup (func_name);
1083 else
1085 long last = strtol (end_val, &invalid, 10);
1086 if (*invalid || last < start)
1088 error ("Invalid range %s in option %s",
1089 end_val,
1090 is_enable ? "-fenable" : "-fdisable");
1091 free (argstr);
1092 return;
1094 new_range = XCNEW (struct uid_range);
1095 new_range->start = (unsigned) start;
1096 new_range->last = (unsigned) last;
1099 slot = (*tab)[pass->static_pass_number];
1100 new_range->next = slot;
1101 (*tab)[pass->static_pass_number] = new_range;
1102 if (is_enable)
1104 if (new_range->assem_name)
1105 inform (UNKNOWN_LOCATION,
1106 "enable pass %s for function %s",
1107 phase_name, new_range->assem_name);
1108 else
1109 inform (UNKNOWN_LOCATION,
1110 "enable pass %s for functions in the range of [%u, %u]",
1111 phase_name, new_range->start, new_range->last);
1113 else
1115 if (new_range->assem_name)
1116 inform (UNKNOWN_LOCATION,
1117 "disable pass %s for function %s",
1118 phase_name, new_range->assem_name);
1119 else
1120 inform (UNKNOWN_LOCATION,
1121 "disable pass %s for functions in the range of [%u, %u]",
1122 phase_name, new_range->start, new_range->last);
1125 one_range = next_range;
1126 } while (next_range);
1129 free (argstr);
1132 /* Enable pass specified by ARG. */
1134 void
1135 enable_pass (const char *arg)
1137 enable_disable_pass (arg, true);
1140 /* Disable pass specified by ARG. */
1142 void
1143 disable_pass (const char *arg)
1145 enable_disable_pass (arg, false);
1148 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1150 static bool
1151 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
1152 tree func,
1153 vec<uid_range_p> tab)
1155 uid_range_p slot, range;
1156 int cgraph_uid;
1157 const char *aname = NULL;
1159 if (!tab.exists ()
1160 || (unsigned) pass->static_pass_number >= tab.length ()
1161 || pass->static_pass_number == -1)
1162 return false;
1164 slot = tab[pass->static_pass_number];
1165 if (!slot)
1166 return false;
1168 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
1169 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1170 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1172 range = slot;
1173 while (range)
1175 if ((unsigned) cgraph_uid >= range->start
1176 && (unsigned) cgraph_uid <= range->last)
1177 return true;
1178 if (range->assem_name && aname
1179 && !strcmp (range->assem_name, aname))
1180 return true;
1181 range = range->next;
1184 return false;
1188 /* Update static_pass_number for passes (and the flag
1189 TODO_mark_first_instance).
1191 Passes are constructed with static_pass_number preinitialized to 0
1193 This field is used in two different ways: initially as instance numbers
1194 of their kind, and then as ids within the entire pass manager.
1196 Within pass_manager::pass_manager:
1198 * In add_pass_instance(), as called by next_pass_1 in
1199 NEXT_PASS in init_optimization_passes
1201 * When the initial instance of a pass within a pass manager is seen,
1202 it is flagged, and its static_pass_number is set to -1
1204 * On subsequent times that it is seen, the static pass number
1205 is decremented each time, so that if there are e.g. 4 dups,
1206 they have static_pass_number -4, 2, 3, 4 respectively (note
1207 how the initial one is negative and gives the count); these
1208 can be thought of as instance numbers of the specific pass
1210 * Within the register_dump_files () traversal, set_pass_for_id()
1211 is called on each pass, using these instance numbers to create
1212 dumpfile switches, and then overwriting them with a pass id,
1213 which are global to the whole pass manager (based on
1214 (TDI_end + current value of extra_dump_files_in_use) ) */
1216 static void
1217 add_pass_instance (struct opt_pass *new_pass, bool track_duplicates,
1218 opt_pass *initial_pass)
1220 /* Are we dealing with the first pass of its kind, or a clone? */
1221 if (new_pass != initial_pass)
1223 /* We're dealing with a clone. */
1224 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1226 /* Indicate to register_dump_files that this pass has duplicates,
1227 and so it should rename the dump file. The first instance will
1228 be -1, and be number of duplicates = -static_pass_number - 1.
1229 Subsequent instances will be > 0 and just the duplicate number. */
1230 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1232 initial_pass->static_pass_number -= 1;
1233 new_pass->static_pass_number = -initial_pass->static_pass_number;
1236 else
1238 /* We're dealing with the first pass of its kind. */
1239 new_pass->todo_flags_start |= TODO_mark_first_instance;
1240 new_pass->static_pass_number = -1;
1242 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1246 /* Add a pass to the pass list. Duplicate the pass if it's already
1247 in the list. */
1249 static struct opt_pass **
1250 next_pass_1 (struct opt_pass **list, struct opt_pass *pass,
1251 struct opt_pass *initial_pass)
1253 /* Every pass should have a name so that plugins can refer to them. */
1254 gcc_assert (pass->name != NULL);
1256 add_pass_instance (pass, false, initial_pass);
1257 *list = pass;
1259 return &(*list)->next;
1262 /* List node for an inserted pass instance. We need to keep track of all
1263 the newly-added pass instances (with 'added_pass_nodes' defined below)
1264 so that we can register their dump files after pass-positioning is finished.
1265 Registering dumping files needs to be post-processed or the
1266 static_pass_number of the opt_pass object would be modified and mess up
1267 the dump file names of future pass instances to be added. */
1269 struct pass_list_node
1271 struct opt_pass *pass;
1272 struct pass_list_node *next;
1275 static struct pass_list_node *added_pass_nodes = NULL;
1276 static struct pass_list_node *prev_added_pass_node;
1278 /* Insert the pass at the proper position. Return true if the pass
1279 is successfully added.
1281 NEW_PASS_INFO - new pass to be inserted
1282 PASS_LIST - root of the pass list to insert the new pass to */
1284 static bool
1285 position_pass (struct register_pass_info *new_pass_info,
1286 struct opt_pass **pass_list)
1288 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1289 bool success = false;
1291 for ( ; pass; prev_pass = pass, pass = pass->next)
1293 /* Check if the current pass is of the same type as the new pass and
1294 matches the name and the instance number of the reference pass. */
1295 if (pass->type == new_pass_info->pass->type
1296 && pass->name
1297 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1298 && ((new_pass_info->ref_pass_instance_number == 0)
1299 || (new_pass_info->ref_pass_instance_number ==
1300 pass->static_pass_number)
1301 || (new_pass_info->ref_pass_instance_number == 1
1302 && pass->todo_flags_start & TODO_mark_first_instance)))
1304 struct opt_pass *new_pass;
1305 struct pass_list_node *new_pass_node;
1307 if (new_pass_info->ref_pass_instance_number == 0)
1309 new_pass = new_pass_info->pass->clone ();
1310 add_pass_instance (new_pass, true, new_pass_info->pass);
1312 else
1314 new_pass = new_pass_info->pass;
1315 add_pass_instance (new_pass, true, new_pass);
1318 /* Insert the new pass instance based on the positioning op. */
1319 switch (new_pass_info->pos_op)
1321 case PASS_POS_INSERT_AFTER:
1322 new_pass->next = pass->next;
1323 pass->next = new_pass;
1325 /* Skip newly inserted pass to avoid repeated
1326 insertions in the case where the new pass and the
1327 existing one have the same name. */
1328 pass = new_pass;
1329 break;
1330 case PASS_POS_INSERT_BEFORE:
1331 new_pass->next = pass;
1332 if (prev_pass)
1333 prev_pass->next = new_pass;
1334 else
1335 *pass_list = new_pass;
1336 break;
1337 case PASS_POS_REPLACE:
1338 new_pass->next = pass->next;
1339 if (prev_pass)
1340 prev_pass->next = new_pass;
1341 else
1342 *pass_list = new_pass;
1343 new_pass->sub = pass->sub;
1344 new_pass->tv_id = pass->tv_id;
1345 pass = new_pass;
1346 break;
1347 default:
1348 error ("invalid pass positioning operation");
1349 return false;
1352 /* Save the newly added pass (instance) in the added_pass_nodes
1353 list so that we can register its dump file later. Note that
1354 we cannot register the dump file now because doing so will modify
1355 the static_pass_number of the opt_pass object and therefore
1356 mess up the dump file name of future instances. */
1357 new_pass_node = XCNEW (struct pass_list_node);
1358 new_pass_node->pass = new_pass;
1359 if (!added_pass_nodes)
1360 added_pass_nodes = new_pass_node;
1361 else
1362 prev_added_pass_node->next = new_pass_node;
1363 prev_added_pass_node = new_pass_node;
1365 success = true;
1368 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1369 success = true;
1372 return success;
1375 /* Hooks a new pass into the pass lists.
1377 PASS_INFO - pass information that specifies the opt_pass object,
1378 reference pass, instance number, and how to position
1379 the pass */
1381 void
1382 register_pass (struct register_pass_info *pass_info)
1384 g->get_passes ()->register_pass (pass_info);
1387 void
1388 register_pass (opt_pass* pass, pass_positioning_ops pos,
1389 const char* ref_pass_name, int ref_pass_inst_number)
1391 register_pass_info i;
1392 i.pass = pass;
1393 i.reference_pass_name = ref_pass_name;
1394 i.ref_pass_instance_number = ref_pass_inst_number;
1395 i.pos_op = pos;
1397 g->get_passes ()->register_pass (&i);
1400 void
1401 pass_manager::register_pass (struct register_pass_info *pass_info)
1403 bool all_instances, success;
1404 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1406 /* The checks below could fail in buggy plugins. Existing GCC
1407 passes should never fail these checks, so we mention plugin in
1408 the messages. */
1409 if (!pass_info->pass)
1410 fatal_error ("plugin cannot register a missing pass");
1412 if (!pass_info->pass->name)
1413 fatal_error ("plugin cannot register an unnamed pass");
1415 if (!pass_info->reference_pass_name)
1416 fatal_error
1417 ("plugin cannot register pass %qs without reference pass name",
1418 pass_info->pass->name);
1420 /* Try to insert the new pass to the pass lists. We need to check
1421 all five lists as the reference pass could be in one (or all) of
1422 them. */
1423 all_instances = pass_info->ref_pass_instance_number == 0;
1424 success = position_pass (pass_info, &all_lowering_passes);
1425 if (!success || all_instances)
1426 success |= position_pass (pass_info, &all_small_ipa_passes);
1427 if (!success || all_instances)
1428 success |= position_pass (pass_info, &all_regular_ipa_passes);
1429 if (!success || all_instances)
1430 success |= position_pass (pass_info, &all_late_ipa_passes);
1431 if (!success || all_instances)
1432 success |= position_pass (pass_info, &all_passes);
1433 if (!success)
1434 fatal_error
1435 ("pass %qs not found but is referenced by new pass %qs",
1436 pass_info->reference_pass_name, pass_info->pass->name);
1438 /* OK, we have successfully inserted the new pass. We need to register
1439 the dump files for the newly added pass and its duplicates (if any).
1440 Because the registration of plugin/backend passes happens after the
1441 command-line options are parsed, the options that specify single
1442 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1443 passes. Therefore we currently can only enable dumping of
1444 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1445 are specified. While doing so, we also delete the pass_list_node
1446 objects created during pass positioning. */
1447 while (added_pass_nodes)
1449 struct pass_list_node *next_node = added_pass_nodes->next;
1450 enum tree_dump_index tdi;
1451 register_one_dump_file (added_pass_nodes->pass);
1452 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1453 || added_pass_nodes->pass->type == IPA_PASS)
1454 tdi = TDI_ipa_all;
1455 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1456 tdi = TDI_tree_all;
1457 else
1458 tdi = TDI_rtl_all;
1459 /* Check if dump-all flag is specified. */
1460 if (dumps->get_dump_file_info (tdi)->pstate)
1461 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1462 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1463 XDELETE (added_pass_nodes);
1464 added_pass_nodes = next_node;
1468 /* Construct the pass tree. The sequencing of passes is driven by
1469 the cgraph routines:
1471 finalize_compilation_unit ()
1472 for each node N in the cgraph
1473 cgraph_analyze_function (N)
1474 cgraph_lower_function (N) -> all_lowering_passes
1476 If we are optimizing, compile is then invoked:
1478 compile ()
1479 ipa_passes () -> all_small_ipa_passes
1480 -> Analysis of all_regular_ipa_passes
1481 * possible LTO streaming at copmilation time *
1482 -> Execution of all_regular_ipa_passes
1483 * possible LTO streaming at link time *
1484 -> all_late_ipa_passes
1485 expand_all_functions ()
1486 for each node N in the cgraph
1487 expand_function (N) -> Transformation of all_regular_ipa_passes
1488 -> all_passes
1491 void *
1492 pass_manager::operator new (size_t sz)
1494 /* Ensure that all fields of the pass manager are zero-initialized. */
1495 return xcalloc (1, sz);
1498 pass_manager::pass_manager (context *ctxt)
1499 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1500 all_regular_ipa_passes (NULL),
1501 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1502 m_ctxt (ctxt)
1504 struct opt_pass **p;
1506 /* Initialize the pass_lists array. */
1507 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1508 GCC_PASS_LISTS
1509 #undef DEF_PASS_LIST
1511 /* Build the tree of passes. */
1513 #define INSERT_PASSES_AFTER(PASS) \
1514 p = &(PASS);
1516 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1518 struct opt_pass **p = &(PASS ## _1)->sub;
1520 #define POP_INSERT_PASSES() \
1523 #define NEXT_PASS(PASS, NUM) \
1524 do { \
1525 gcc_assert (NULL == PASS ## _ ## NUM); \
1526 if ((NUM) == 1) \
1527 PASS ## _1 = make_##PASS (m_ctxt); \
1528 else \
1530 gcc_assert (PASS ## _1); \
1531 PASS ## _ ## NUM = PASS ## _1->clone (); \
1533 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1534 } while (0)
1536 #define TERMINATE_PASS_LIST() \
1537 *p = NULL;
1539 #include "pass-instances.def"
1541 #undef INSERT_PASSES_AFTER
1542 #undef PUSH_INSERT_PASSES_WITHIN
1543 #undef POP_INSERT_PASSES
1544 #undef NEXT_PASS
1545 #undef TERMINATE_PASS_LIST
1547 /* Register the passes with the tree dump code. */
1548 register_dump_files (all_lowering_passes, PROP_gimple_any);
1549 register_dump_files (all_small_ipa_passes,
1550 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1551 | PROP_cfg);
1552 register_dump_files (all_regular_ipa_passes,
1553 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1554 | PROP_cfg);
1555 register_dump_files (all_late_ipa_passes,
1556 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1557 | PROP_cfg);
1558 register_dump_files (all_passes,
1559 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1560 | PROP_cfg);
1563 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1564 function CALLBACK for every function in the call graph. Otherwise,
1565 call CALLBACK on the current function. */
1567 static void
1568 do_per_function (void (*callback) (void *data), void *data)
1570 if (current_function_decl)
1571 callback (data);
1572 else
1574 struct cgraph_node *node;
1575 FOR_EACH_DEFINED_FUNCTION (node)
1576 if (node->analyzed && gimple_has_body_p (node->decl)
1577 && (!node->clone_of || node->decl != node->clone_of->decl))
1579 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1580 callback (data);
1581 if (!flag_wpa)
1583 free_dominance_info (CDI_DOMINATORS);
1584 free_dominance_info (CDI_POST_DOMINATORS);
1586 pop_cfun ();
1587 ggc_collect ();
1592 /* Because inlining might remove no-longer reachable nodes, we need to
1593 keep the array visible to garbage collector to avoid reading collected
1594 out nodes. */
1595 static int nnodes;
1596 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1598 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1599 function CALLBACK for every function in the call graph. Otherwise,
1600 call CALLBACK on the current function.
1601 This function is global so that plugins can use it. */
1602 void
1603 do_per_function_toporder (void (*callback) (void *data), void *data)
1605 int i;
1607 if (current_function_decl)
1608 callback (data);
1609 else
1611 gcc_assert (!order);
1612 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1613 nnodes = ipa_reverse_postorder (order);
1614 for (i = nnodes - 1; i >= 0; i--)
1615 order[i]->process = 1;
1616 for (i = nnodes - 1; i >= 0; i--)
1618 struct cgraph_node *node = order[i];
1620 /* Allow possibly removed nodes to be garbage collected. */
1621 order[i] = NULL;
1622 node->process = 0;
1623 if (cgraph_function_with_gimple_body_p (node))
1625 cgraph_get_body (node);
1626 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1627 callback (data);
1628 free_dominance_info (CDI_DOMINATORS);
1629 free_dominance_info (CDI_POST_DOMINATORS);
1630 pop_cfun ();
1631 ggc_collect ();
1635 ggc_free (order);
1636 order = NULL;
1637 nnodes = 0;
1640 /* Helper function to perform function body dump. */
1642 static void
1643 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1645 if (dump_file && current_function_decl)
1647 if (cfun->curr_properties & PROP_trees)
1648 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1649 else
1650 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1652 /* Flush the file. If verification fails, we won't be able to
1653 close the file before aborting. */
1654 fflush (dump_file);
1656 if ((cfun->curr_properties & PROP_cfg)
1657 && (dump_flags & TDF_GRAPH))
1658 print_graph_cfg (dump_file_name, cfun);
1662 static struct profile_record *profile_record;
1664 /* Do profile consistency book-keeping for the pass with static number INDEX.
1665 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1666 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1667 if we are only book-keeping on passes that may have selectively disabled
1668 themselves on a given function. */
1669 static void
1670 check_profile_consistency (int index, int subpass, bool run)
1672 pass_manager *passes = g->get_passes ();
1673 if (index == -1)
1674 return;
1675 if (!profile_record)
1676 profile_record = XCNEWVEC (struct profile_record,
1677 passes->passes_by_id_size);
1678 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1679 gcc_assert (subpass < 2);
1680 profile_record[index].run |= run;
1681 account_profile_record (&profile_record[index], subpass);
1684 /* Output profile consistency. */
1686 void
1687 dump_profile_report (void)
1689 g->get_passes ()->dump_profile_report ();
1692 void
1693 pass_manager::dump_profile_report () const
1695 int i, j;
1696 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1697 gcov_type last_time = 0, last_size = 0;
1698 double rel_time_change, rel_size_change;
1699 int last_reported = 0;
1701 if (!profile_record)
1702 return;
1703 fprintf (stderr, "\nProfile consistency report:\n\n");
1704 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1705 fprintf (stderr, " |freq count |freq count |size time\n");
1707 for (i = 0; i < passes_by_id_size; i++)
1708 for (j = 0 ; j < 2; j++)
1709 if (profile_record[i].run)
1711 if (last_time)
1712 rel_time_change = (profile_record[i].time[j]
1713 - (double)last_time) * 100 / (double)last_time;
1714 else
1715 rel_time_change = 0;
1716 if (last_size)
1717 rel_size_change = (profile_record[i].size[j]
1718 - (double)last_size) * 100 / (double)last_size;
1719 else
1720 rel_size_change = 0;
1722 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1723 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1724 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1725 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1726 || rel_time_change || rel_size_change)
1728 last_reported = i;
1729 fprintf (stderr, "%-20s %s",
1730 passes_by_id [i]->name,
1731 j ? "(after TODO)" : " ");
1732 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1733 fprintf (stderr, "| %+5i",
1734 profile_record[i].num_mismatched_freq_in[j]
1735 - last_freq_in);
1736 else
1737 fprintf (stderr, "| ");
1738 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1739 fprintf (stderr, " %+5i",
1740 profile_record[i].num_mismatched_count_in[j]
1741 - last_count_in);
1742 else
1743 fprintf (stderr, " ");
1744 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1745 fprintf (stderr, "| %+5i",
1746 profile_record[i].num_mismatched_freq_out[j]
1747 - last_freq_out);
1748 else
1749 fprintf (stderr, "| ");
1750 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1751 fprintf (stderr, " %+5i",
1752 profile_record[i].num_mismatched_count_out[j]
1753 - last_count_out);
1754 else
1755 fprintf (stderr, " ");
1757 /* Size/time units change across gimple and RTL. */
1758 if (i == pass_expand_1->static_pass_number)
1759 fprintf (stderr, "|----------");
1760 else
1762 if (rel_size_change)
1763 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1764 else
1765 fprintf (stderr, "| ");
1766 if (rel_time_change)
1767 fprintf (stderr, " %+8.4f%%", rel_time_change);
1769 fprintf (stderr, "\n");
1770 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1771 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1772 last_count_in = profile_record[i].num_mismatched_count_in[j];
1773 last_count_out = profile_record[i].num_mismatched_count_out[j];
1775 else if (j && last_reported != i)
1777 last_reported = i;
1778 fprintf (stderr, "%-20s ------------| | |\n",
1779 passes_by_id [i]->name);
1781 last_time = profile_record[i].time[j];
1782 last_size = profile_record[i].size[j];
1786 /* Perform all TODO actions that ought to be done on each function. */
1788 static void
1789 execute_function_todo (void *data)
1791 unsigned int flags = (size_t)data;
1792 flags &= ~cfun->last_verified;
1793 if (!flags)
1794 return;
1796 /* Always cleanup the CFG before trying to update SSA. */
1797 if (flags & TODO_cleanup_cfg)
1799 cleanup_tree_cfg ();
1801 /* When cleanup_tree_cfg merges consecutive blocks, it may
1802 perform some simplistic propagation when removing single
1803 valued PHI nodes. This propagation may, in turn, cause the
1804 SSA form to become out-of-date (see PR 22037). So, even
1805 if the parent pass had not scheduled an SSA update, we may
1806 still need to do one. */
1807 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1808 flags |= TODO_update_ssa;
1811 if (flags & TODO_update_ssa_any)
1813 unsigned update_flags = flags & TODO_update_ssa_any;
1814 update_ssa (update_flags);
1815 cfun->last_verified &= ~TODO_verify_ssa;
1818 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1819 compute_may_aliases ();
1821 if (optimize && (flags & TODO_update_address_taken))
1822 execute_update_addresses_taken ();
1824 if (flags & TODO_remove_unused_locals)
1825 remove_unused_locals ();
1827 if (flags & TODO_rebuild_frequencies)
1828 rebuild_frequencies ();
1830 if (flags & TODO_rebuild_cgraph_edges)
1831 rebuild_cgraph_edges ();
1833 /* If we've seen errors do not bother running any verifiers. */
1834 if (seen_error ())
1835 return;
1837 #if defined ENABLE_CHECKING
1838 if (flags & TODO_verify_ssa
1839 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1841 verify_gimple_in_cfg (cfun);
1842 verify_ssa (true);
1844 else if (flags & TODO_verify_stmts)
1845 verify_gimple_in_cfg (cfun);
1846 if (flags & TODO_verify_flow)
1847 verify_flow_info ();
1848 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1849 verify_loop_closed_ssa (false);
1850 if (flags & TODO_verify_rtl_sharing)
1851 verify_rtl_sharing ();
1852 #endif
1854 cfun->last_verified = flags & TODO_verify_all;
1857 /* Perform all TODO actions. */
1858 static void
1859 execute_todo (unsigned int flags)
1861 #if defined ENABLE_CHECKING
1862 if (cfun
1863 && need_ssa_update_p (cfun))
1864 gcc_assert (flags & TODO_update_ssa_any);
1865 #endif
1867 timevar_push (TV_TODO);
1869 /* Inform the pass whether it is the first time it is run. */
1870 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1872 statistics_fini_pass ();
1874 if (flags)
1875 do_per_function (execute_function_todo, (void *)(size_t) flags);
1877 /* Always remove functions just as before inlining: IPA passes might be
1878 interested to see bodies of extern inline functions that are not inlined
1879 to analyze side effects. The full removal is done just at the end
1880 of IPA pass queue. */
1881 if (flags & TODO_remove_functions)
1883 gcc_assert (!cfun);
1884 symtab_remove_unreachable_nodes (true, dump_file);
1887 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1889 gcc_assert (!cfun);
1890 dump_symtab (dump_file);
1891 /* Flush the file. If verification fails, we won't be able to
1892 close the file before aborting. */
1893 fflush (dump_file);
1896 /* Now that the dumping has been done, we can get rid of the optional
1897 df problems. */
1898 if (flags & TODO_df_finish)
1899 df_finish_pass ((flags & TODO_df_verify) != 0);
1901 timevar_pop (TV_TODO);
1904 /* Verify invariants that should hold between passes. This is a place
1905 to put simple sanity checks. */
1907 static void
1908 verify_interpass_invariants (void)
1910 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1913 /* Clear the last verified flag. */
1915 static void
1916 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1918 cfun->last_verified = 0;
1921 /* Helper function. Verify that the properties has been turn into the
1922 properties expected by the pass. */
1924 #ifdef ENABLE_CHECKING
1925 static void
1926 verify_curr_properties (void *data)
1928 unsigned int props = (size_t)data;
1929 gcc_assert ((cfun->curr_properties & props) == props);
1931 #endif
1933 /* Initialize pass dump file. */
1934 /* This is non-static so that the plugins can use it. */
1936 bool
1937 pass_init_dump_file (struct opt_pass *pass)
1939 /* If a dump file name is present, open it if enabled. */
1940 if (pass->static_pass_number != -1)
1942 timevar_push (TV_DUMP);
1943 gcc::dump_manager *dumps = g->get_dumps ();
1944 bool initializing_dump =
1945 !dumps->dump_initialized_p (pass->static_pass_number);
1946 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
1947 dumps->dump_start (pass->static_pass_number, &dump_flags);
1948 if (dump_file && current_function_decl)
1949 dump_function_header (dump_file, current_function_decl, dump_flags);
1950 if (initializing_dump
1951 && dump_file && (dump_flags & TDF_GRAPH)
1952 && cfun && (cfun->curr_properties & PROP_cfg))
1953 clean_graph_dump_file (dump_file_name);
1954 timevar_pop (TV_DUMP);
1955 return initializing_dump;
1957 else
1958 return false;
1961 /* Flush PASS dump file. */
1962 /* This is non-static so that plugins can use it. */
1964 void
1965 pass_fini_dump_file (struct opt_pass *pass)
1967 timevar_push (TV_DUMP);
1969 /* Flush and close dump file. */
1970 if (dump_file_name)
1972 free (CONST_CAST (char *, dump_file_name));
1973 dump_file_name = NULL;
1976 g->get_dumps ()->dump_finish (pass->static_pass_number);
1977 timevar_pop (TV_DUMP);
1980 /* After executing the pass, apply expected changes to the function
1981 properties. */
1983 static void
1984 update_properties_after_pass (void *data)
1986 struct opt_pass *pass = (struct opt_pass *) data;
1987 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1988 & ~pass->properties_destroyed;
1991 /* Execute summary generation for all of the passes in IPA_PASS. */
1993 void
1994 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1996 while (ipa_pass)
1998 struct opt_pass *pass = ipa_pass;
2000 /* Execute all of the IPA_PASSes in the list. */
2001 if (ipa_pass->type == IPA_PASS
2002 && ((!pass->has_gate) || pass->gate ())
2003 && ipa_pass->generate_summary)
2005 pass_init_dump_file (pass);
2007 /* If a timevar is present, start it. */
2008 if (pass->tv_id)
2009 timevar_push (pass->tv_id);
2011 ipa_pass->generate_summary ();
2013 /* Stop timevar. */
2014 if (pass->tv_id)
2015 timevar_pop (pass->tv_id);
2017 pass_fini_dump_file (pass);
2019 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->next;
2023 /* Execute IPA_PASS function transform on NODE. */
2025 static void
2026 execute_one_ipa_transform_pass (struct cgraph_node *node,
2027 struct ipa_opt_pass_d *ipa_pass)
2029 struct opt_pass *pass = ipa_pass;
2030 unsigned int todo_after = 0;
2032 current_pass = pass;
2033 if (!ipa_pass->function_transform)
2034 return;
2036 /* Note that the folders should only create gimple expressions.
2037 This is a hack until the new folder is ready. */
2038 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2040 pass_init_dump_file (pass);
2042 /* Run pre-pass verification. */
2043 execute_todo (ipa_pass->function_transform_todo_flags_start);
2045 /* If a timevar is present, start it. */
2046 if (pass->tv_id != TV_NONE)
2047 timevar_push (pass->tv_id);
2049 /* Do it! */
2050 todo_after = ipa_pass->function_transform (node);
2052 /* Stop timevar. */
2053 if (pass->tv_id != TV_NONE)
2054 timevar_pop (pass->tv_id);
2056 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2057 check_profile_consistency (pass->static_pass_number, 0, true);
2059 /* Run post-pass cleanup and verification. */
2060 execute_todo (todo_after);
2061 verify_interpass_invariants ();
2062 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2063 check_profile_consistency (pass->static_pass_number, 1, true);
2065 if (dump_file)
2066 do_per_function (execute_function_dump, NULL);
2067 pass_fini_dump_file (pass);
2069 current_pass = NULL;
2071 /* Signal this is a suitable GC collection point. */
2072 if (!(todo_after & TODO_do_not_ggc_collect))
2073 ggc_collect ();
2076 /* For the current function, execute all ipa transforms. */
2078 void
2079 execute_all_ipa_transforms (void)
2081 struct cgraph_node *node;
2082 if (!cfun)
2083 return;
2084 node = cgraph_get_node (current_function_decl);
2086 if (node->ipa_transforms_to_apply.exists ())
2088 unsigned int i;
2090 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2091 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2092 node->ipa_transforms_to_apply.release ();
2096 /* Callback for do_per_function to apply all IPA transforms. */
2098 static void
2099 apply_ipa_transforms (void *data)
2101 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2102 if (!node->global.inlined_to && node->ipa_transforms_to_apply.exists ())
2104 *(bool *)data = true;
2105 execute_all_ipa_transforms ();
2106 rebuild_cgraph_edges ();
2110 /* Check if PASS is explicitly disabled or enabled and return
2111 the gate status. FUNC is the function to be processed, and
2112 GATE_STATUS is the gate status determined by pass manager by
2113 default. */
2115 static bool
2116 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2118 bool explicitly_enabled = false;
2119 bool explicitly_disabled = false;
2121 explicitly_enabled
2122 = is_pass_explicitly_enabled_or_disabled (pass, func,
2123 enabled_pass_uid_range_tab);
2124 explicitly_disabled
2125 = is_pass_explicitly_enabled_or_disabled (pass, func,
2126 disabled_pass_uid_range_tab);
2128 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2130 return gate_status;
2134 /* Execute PASS. */
2136 bool
2137 execute_one_pass (struct opt_pass *pass)
2139 unsigned int todo_after = 0;
2141 bool gate_status;
2143 /* IPA passes are executed on whole program, so cfun should be NULL.
2144 Other passes need function context set. */
2145 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2146 gcc_assert (!cfun && !current_function_decl);
2147 else
2148 gcc_assert (cfun && current_function_decl);
2150 current_pass = pass;
2152 /* Check whether gate check should be avoided.
2153 User controls the value of the gate through the parameter "gate_status". */
2154 gate_status = pass->has_gate ? pass->gate () : true;
2155 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2157 /* Override gate with plugin. */
2158 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2160 if (!gate_status)
2162 /* Run so passes selectively disabling themselves on a given function
2163 are not miscounted. */
2164 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2166 check_profile_consistency (pass->static_pass_number, 0, false);
2167 check_profile_consistency (pass->static_pass_number, 1, false);
2169 current_pass = NULL;
2170 return false;
2173 /* Pass execution event trigger: useful to identify passes being
2174 executed. */
2175 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2177 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2178 Apply all trnasforms first. */
2179 if (pass->type == SIMPLE_IPA_PASS)
2181 bool applied = false;
2182 do_per_function (apply_ipa_transforms, (void *)&applied);
2183 if (applied)
2184 symtab_remove_unreachable_nodes (true, dump_file);
2185 /* Restore current_pass. */
2186 current_pass = pass;
2189 if (!quiet_flag && !cfun)
2190 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2192 /* Note that the folders should only create gimple expressions.
2193 This is a hack until the new folder is ready. */
2194 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2196 pass_init_dump_file (pass);
2198 /* Run pre-pass verification. */
2199 execute_todo (pass->todo_flags_start);
2201 #ifdef ENABLE_CHECKING
2202 do_per_function (verify_curr_properties,
2203 (void *)(size_t)pass->properties_required);
2204 #endif
2206 /* If a timevar is present, start it. */
2207 if (pass->tv_id != TV_NONE)
2208 timevar_push (pass->tv_id);
2210 /* Do it! */
2211 if (pass->has_execute)
2213 todo_after = pass->execute ();
2214 do_per_function (clear_last_verified, NULL);
2217 /* Stop timevar. */
2218 if (pass->tv_id != TV_NONE)
2219 timevar_pop (pass->tv_id);
2221 do_per_function (update_properties_after_pass, pass);
2223 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2224 check_profile_consistency (pass->static_pass_number, 0, true);
2226 /* Run post-pass cleanup and verification. */
2227 execute_todo (todo_after | pass->todo_flags_finish);
2228 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2229 check_profile_consistency (pass->static_pass_number, 1, true);
2231 verify_interpass_invariants ();
2232 if (dump_file)
2233 do_per_function (execute_function_dump, NULL);
2234 if (pass->type == IPA_PASS)
2236 struct cgraph_node *node;
2237 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2238 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *)pass);
2241 if (!current_function_decl)
2242 cgraph_process_new_functions ();
2244 pass_fini_dump_file (pass);
2246 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2247 gcc_assert (!(cfun->curr_properties & PROP_trees)
2248 || pass->type != RTL_PASS);
2250 current_pass = NULL;
2252 /* Signal this is a suitable GC collection point. */
2253 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2254 ggc_collect ();
2256 return true;
2259 void
2260 execute_pass_list (struct opt_pass *pass)
2264 gcc_assert (pass->type == GIMPLE_PASS
2265 || pass->type == RTL_PASS);
2266 if (execute_one_pass (pass) && pass->sub)
2267 execute_pass_list (pass->sub);
2268 pass = pass->next;
2270 while (pass);
2273 /* Write out all LTO data. */
2274 static void
2275 write_lto (void)
2277 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2278 lto_output ();
2279 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2280 timevar_push (TV_IPA_LTO_DECL_OUT);
2281 produce_asm_for_decls ();
2282 timevar_pop (TV_IPA_LTO_DECL_OUT);
2285 /* Same as execute_pass_list but assume that subpasses of IPA passes
2286 are local passes. If SET is not NULL, write out summaries of only
2287 those node in SET. */
2289 static void
2290 ipa_write_summaries_2 (struct opt_pass *pass, struct lto_out_decl_state *state)
2292 while (pass)
2294 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2295 gcc_assert (!current_function_decl);
2296 gcc_assert (!cfun);
2297 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2298 if (pass->type == IPA_PASS
2299 && ipa_pass->write_summary
2300 && ((!pass->has_gate) || pass->gate ()))
2302 /* If a timevar is present, start it. */
2303 if (pass->tv_id)
2304 timevar_push (pass->tv_id);
2306 pass_init_dump_file (pass);
2308 ipa_pass->write_summary ();
2310 pass_fini_dump_file (pass);
2312 /* If a timevar is present, start it. */
2313 if (pass->tv_id)
2314 timevar_pop (pass->tv_id);
2317 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2318 ipa_write_summaries_2 (pass->sub, state);
2320 pass = pass->next;
2324 /* Helper function of ipa_write_summaries. Creates and destroys the
2325 decl state and calls ipa_write_summaries_2 for all passes that have
2326 summaries. SET is the set of nodes to be written. */
2328 static void
2329 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2331 pass_manager *passes = g->get_passes ();
2332 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2333 state->symtab_node_encoder = encoder;
2335 lto_push_out_decl_state (state);
2337 gcc_assert (!flag_wpa);
2338 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2340 write_lto ();
2342 gcc_assert (lto_get_out_decl_state () == state);
2343 lto_pop_out_decl_state ();
2344 lto_delete_out_decl_state (state);
2347 /* Write out summaries for all the nodes in the callgraph. */
2349 void
2350 ipa_write_summaries (void)
2352 lto_symtab_encoder_t encoder;
2353 int i, order_pos;
2354 struct varpool_node *vnode;
2355 struct cgraph_node *node;
2356 struct cgraph_node **order;
2358 if (!flag_generate_lto || seen_error ())
2359 return;
2361 encoder = lto_symtab_encoder_new (false);
2363 /* Create the callgraph set in the same order used in
2364 cgraph_expand_all_functions. This mostly facilitates debugging,
2365 since it causes the gimple file to be processed in the same order
2366 as the source code. */
2367 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2368 order_pos = ipa_reverse_postorder (order);
2369 gcc_assert (order_pos == cgraph_n_nodes);
2371 for (i = order_pos - 1; i >= 0; i--)
2373 struct cgraph_node *node = order[i];
2375 if (cgraph_function_with_gimple_body_p (node))
2377 /* When streaming out references to statements as part of some IPA
2378 pass summary, the statements need to have uids assigned and the
2379 following does that for all the IPA passes here. Naturally, this
2380 ordering then matches the one IPA-passes get in their stmt_fixup
2381 hooks. */
2383 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2384 renumber_gimple_stmt_uids ();
2385 pop_cfun ();
2387 if (node->definition)
2388 lto_set_symtab_encoder_in_partition (encoder, node);
2391 FOR_EACH_DEFINED_FUNCTION (node)
2392 if (node->alias)
2393 lto_set_symtab_encoder_in_partition (encoder, node);
2394 FOR_EACH_DEFINED_VARIABLE (vnode)
2395 lto_set_symtab_encoder_in_partition (encoder, vnode);
2397 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2399 free (order);
2402 /* Same as execute_pass_list but assume that subpasses of IPA passes
2403 are local passes. If SET is not NULL, write out optimization summaries of
2404 only those node in SET. */
2406 static void
2407 ipa_write_optimization_summaries_1 (struct opt_pass *pass, struct lto_out_decl_state *state)
2409 while (pass)
2411 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2412 gcc_assert (!current_function_decl);
2413 gcc_assert (!cfun);
2414 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2415 if (pass->type == IPA_PASS
2416 && ipa_pass->write_optimization_summary
2417 && ((!pass->has_gate) || pass->gate ()))
2419 /* If a timevar is present, start it. */
2420 if (pass->tv_id)
2421 timevar_push (pass->tv_id);
2423 pass_init_dump_file (pass);
2425 ipa_pass->write_optimization_summary ();
2427 pass_fini_dump_file (pass);
2429 /* If a timevar is present, start it. */
2430 if (pass->tv_id)
2431 timevar_pop (pass->tv_id);
2434 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2435 ipa_write_optimization_summaries_1 (pass->sub, state);
2437 pass = pass->next;
2441 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2442 NULL, write out all summaries of all nodes. */
2444 void
2445 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2447 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2448 lto_symtab_encoder_iterator lsei;
2449 state->symtab_node_encoder = encoder;
2451 lto_push_out_decl_state (state);
2452 for (lsei = lsei_start_function_in_partition (encoder);
2453 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2455 struct cgraph_node *node = lsei_cgraph_node (lsei);
2456 /* When streaming out references to statements as part of some IPA
2457 pass summary, the statements need to have uids assigned.
2459 For functions newly born at WPA stage we need to initialize
2460 the uids here. */
2461 if (node->definition
2462 && gimple_has_body_p (node->decl))
2464 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2465 renumber_gimple_stmt_uids ();
2466 pop_cfun ();
2470 gcc_assert (flag_wpa);
2471 pass_manager *passes = g->get_passes ();
2472 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2474 write_lto ();
2476 gcc_assert (lto_get_out_decl_state () == state);
2477 lto_pop_out_decl_state ();
2478 lto_delete_out_decl_state (state);
2481 /* Same as execute_pass_list but assume that subpasses of IPA passes
2482 are local passes. */
2484 static void
2485 ipa_read_summaries_1 (struct opt_pass *pass)
2487 while (pass)
2489 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2491 gcc_assert (!current_function_decl);
2492 gcc_assert (!cfun);
2493 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2495 if ((!pass->has_gate) || pass->gate ())
2497 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2499 /* If a timevar is present, start it. */
2500 if (pass->tv_id)
2501 timevar_push (pass->tv_id);
2503 pass_init_dump_file (pass);
2505 ipa_pass->read_summary ();
2507 pass_fini_dump_file (pass);
2509 /* Stop timevar. */
2510 if (pass->tv_id)
2511 timevar_pop (pass->tv_id);
2514 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2515 ipa_read_summaries_1 (pass->sub);
2517 pass = pass->next;
2522 /* Read all the summaries for all_regular_ipa_passes. */
2524 void
2525 ipa_read_summaries (void)
2527 pass_manager *passes = g->get_passes ();
2528 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2531 /* Same as execute_pass_list but assume that subpasses of IPA passes
2532 are local passes. */
2534 static void
2535 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2537 while (pass)
2539 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2541 gcc_assert (!current_function_decl);
2542 gcc_assert (!cfun);
2543 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2545 if ((!pass->has_gate) || pass->gate ())
2547 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2549 /* If a timevar is present, start it. */
2550 if (pass->tv_id)
2551 timevar_push (pass->tv_id);
2553 pass_init_dump_file (pass);
2555 ipa_pass->read_optimization_summary ();
2557 pass_fini_dump_file (pass);
2559 /* Stop timevar. */
2560 if (pass->tv_id)
2561 timevar_pop (pass->tv_id);
2564 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2565 ipa_read_optimization_summaries_1 (pass->sub);
2567 pass = pass->next;
2571 /* Read all the summaries for all_regular_ipa_passes. */
2573 void
2574 ipa_read_optimization_summaries (void)
2576 pass_manager *passes = g->get_passes ();
2577 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2580 /* Same as execute_pass_list but assume that subpasses of IPA passes
2581 are local passes. */
2582 void
2583 execute_ipa_pass_list (struct opt_pass *pass)
2587 gcc_assert (!current_function_decl);
2588 gcc_assert (!cfun);
2589 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2590 if (execute_one_pass (pass) && pass->sub)
2592 if (pass->sub->type == GIMPLE_PASS)
2594 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2595 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2596 pass->sub);
2597 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2599 else if (pass->sub->type == SIMPLE_IPA_PASS
2600 || pass->sub->type == IPA_PASS)
2601 execute_ipa_pass_list (pass->sub);
2602 else
2603 gcc_unreachable ();
2605 gcc_assert (!current_function_decl);
2606 cgraph_process_new_functions ();
2607 pass = pass->next;
2609 while (pass);
2612 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2614 static void
2615 execute_ipa_stmt_fixups (struct opt_pass *pass,
2616 struct cgraph_node *node, gimple *stmts)
2618 while (pass)
2620 /* Execute all of the IPA_PASSes in the list. */
2621 if (pass->type == IPA_PASS
2622 && ((!pass->has_gate) || pass->gate ()))
2624 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2626 if (ipa_pass->stmt_fixup)
2628 pass_init_dump_file (pass);
2629 /* If a timevar is present, start it. */
2630 if (pass->tv_id)
2631 timevar_push (pass->tv_id);
2633 ipa_pass->stmt_fixup (node, stmts);
2635 /* Stop timevar. */
2636 if (pass->tv_id)
2637 timevar_pop (pass->tv_id);
2638 pass_fini_dump_file (pass);
2640 if (pass->sub)
2641 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2643 pass = pass->next;
2647 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2649 void
2650 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2652 pass_manager *passes = g->get_passes ();
2653 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2657 extern void debug_properties (unsigned int);
2658 extern void dump_properties (FILE *, unsigned int);
2660 DEBUG_FUNCTION void
2661 dump_properties (FILE *dump, unsigned int props)
2663 fprintf (dump, "Properties:\n");
2664 if (props & PROP_gimple_any)
2665 fprintf (dump, "PROP_gimple_any\n");
2666 if (props & PROP_gimple_lcf)
2667 fprintf (dump, "PROP_gimple_lcf\n");
2668 if (props & PROP_gimple_leh)
2669 fprintf (dump, "PROP_gimple_leh\n");
2670 if (props & PROP_cfg)
2671 fprintf (dump, "PROP_cfg\n");
2672 if (props & PROP_ssa)
2673 fprintf (dump, "PROP_ssa\n");
2674 if (props & PROP_no_crit_edges)
2675 fprintf (dump, "PROP_no_crit_edges\n");
2676 if (props & PROP_rtl)
2677 fprintf (dump, "PROP_rtl\n");
2678 if (props & PROP_gimple_lomp)
2679 fprintf (dump, "PROP_gimple_lomp\n");
2680 if (props & PROP_gimple_lcx)
2681 fprintf (dump, "PROP_gimple_lcx\n");
2682 if (props & PROP_gimple_lvec)
2683 fprintf (dump, "PROP_gimple_lvec\n");
2684 if (props & PROP_cfglayout)
2685 fprintf (dump, "PROP_cfglayout\n");
2688 DEBUG_FUNCTION void
2689 debug_properties (unsigned int props)
2691 dump_properties (stderr, props);
2694 /* Called by local passes to see if function is called by already processed nodes.
2695 Because we process nodes in topological order, this means that function is
2696 in recursive cycle or we introduced new direct calls. */
2697 bool
2698 function_called_by_processed_nodes_p (void)
2700 struct cgraph_edge *e;
2701 for (e = cgraph_get_node (current_function_decl)->callers;
2703 e = e->next_caller)
2705 if (e->caller->decl == current_function_decl)
2706 continue;
2707 if (!cgraph_function_with_gimple_body_p (e->caller))
2708 continue;
2709 if (TREE_ASM_WRITTEN (e->caller->decl))
2710 continue;
2711 if (!e->caller->process && !e->caller->global.inlined_to)
2712 break;
2714 if (dump_file && e)
2716 fprintf (dump_file, "Already processed call to:\n");
2717 dump_cgraph_node (dump_file, e->caller);
2719 return e != NULL;
2722 #include "gt-passes.h"