gcc/
[official-gcc.git] / gcc / passes.c
blob4966334206e1b0e834b300cbf8c8893dfa3afd00
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2015 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 "alias.h"
30 #include "symtab.h"
31 #include "tree.h"
32 #include "fold-const.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 "expmed.h"
47 #include "dojump.h"
48 #include "explow.h"
49 #include "calls.h"
50 #include "emit-rtl.h"
51 #include "stmt.h"
52 #include "expr.h"
53 #include "predict.h"
54 #include "basic-block.h"
55 #include "intl.h"
56 #include "graph.h"
57 #include "regs.h"
58 #include "diagnostic-core.h"
59 #include "params.h"
60 #include "reload.h"
61 #include "debug.h"
62 #include "target.h"
63 #include "langhooks.h"
64 #include "cfgloop.h"
65 #include "hosthooks.h"
66 #include "opts.h"
67 #include "coverage.h"
68 #include "value-prof.h"
69 #include "tree-inline.h"
70 #include "tree-ssa-alias.h"
71 #include "internal-fn.h"
72 #include "gimple-expr.h"
73 #include "gimple.h"
74 #include "gimple-ssa.h"
75 #include "tree-cfg.h"
76 #include "stringpool.h"
77 #include "tree-ssanames.h"
78 #include "tree-ssa-loop-manip.h"
79 #include "tree-into-ssa.h"
80 #include "tree-dfa.h"
81 #include "tree-ssa.h"
82 #include "tree-pass.h"
83 #include "tree-dump.h"
84 #include "df.h"
85 #include "cgraph.h"
86 #include "lto-streamer.h"
87 #include "plugin.h"
88 #include "ipa-utils.h"
89 #include "tree-pretty-print.h" /* for dump_function_header */
90 #include "context.h"
91 #include "pass_manager.h"
92 #include "dominance.h"
93 #include "cfg.h"
94 #include "cfgrtl.h"
95 #include "tree-ssa-live.h" /* For remove_unused_locals. */
96 #include "tree-cfgcleanup.h"
98 using namespace gcc;
100 /* This is used for debugging. It allows the current pass to printed
101 from anywhere in compilation.
102 The variable current_pass is also used for statistics and plugins. */
103 opt_pass *current_pass;
105 static void register_pass_name (opt_pass *, const char *);
107 /* Most passes are single-instance (within their context) and thus don't
108 need to implement cloning, but passes that support multiple instances
109 *must* provide their own implementation of the clone method.
111 Handle this by providing a default implemenation, but make it a fatal
112 error to call it. */
114 opt_pass *
115 opt_pass::clone ()
117 internal_error ("pass %s does not support cloning", name);
120 bool
121 opt_pass::gate (function *)
123 return true;
126 unsigned int
127 opt_pass::execute (function *)
129 return 0;
132 opt_pass::opt_pass (const pass_data &data, context *ctxt)
133 : pass_data (data),
134 sub (NULL),
135 next (NULL),
136 static_pass_number (0),
137 m_ctxt (ctxt)
142 void
143 pass_manager::execute_early_local_passes ()
145 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
146 if (flag_check_pointer_bounds)
147 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
148 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
151 unsigned int
152 pass_manager::execute_pass_mode_switching ()
154 return pass_mode_switching_1->execute (cfun);
158 /* Call from anywhere to find out what pass this is. Useful for
159 printing out debugging information deep inside an service
160 routine. */
161 void
162 print_current_pass (FILE *file)
164 if (current_pass)
165 fprintf (file, "current pass = %s (%d)\n",
166 current_pass->name, current_pass->static_pass_number);
167 else
168 fprintf (file, "no current pass.\n");
172 /* Call from the debugger to get the current pass name. */
173 DEBUG_FUNCTION void
174 debug_pass (void)
176 print_current_pass (stderr);
181 /* Global variables used to communicate with passes. */
182 bool in_gimple_form;
183 bool first_pass_instance;
186 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
187 and TYPE_DECL nodes.
189 This does nothing for local (non-static) variables, unless the
190 variable is a register variable with DECL_ASSEMBLER_NAME set. In
191 that case, or if the variable is not an automatic, it sets up the
192 RTL and outputs any assembler code (label definition, storage
193 allocation and initialization).
195 DECL is the declaration. TOP_LEVEL is nonzero
196 if this declaration is not within a function. */
198 void
199 rest_of_decl_compilation (tree decl,
200 int top_level,
201 int at_end)
203 bool finalize = true;
205 /* We deferred calling assemble_alias so that we could collect
206 other attributes such as visibility. Emit the alias now. */
207 if (!in_lto_p)
209 tree alias;
210 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
211 if (alias)
213 alias = TREE_VALUE (TREE_VALUE (alias));
214 alias = get_identifier (TREE_STRING_POINTER (alias));
215 /* A quirk of the initial implementation of aliases required that the
216 user add "extern" to all of them. Which is silly, but now
217 historical. Do note that the symbol is in fact locally defined. */
218 DECL_EXTERNAL (decl) = 0;
219 TREE_STATIC (decl) = 1;
220 assemble_alias (decl, alias);
221 finalize = false;
225 /* Can't defer this, because it needs to happen before any
226 later function definitions are processed. */
227 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
228 make_decl_rtl (decl);
230 /* Forward declarations for nested functions are not "external",
231 but we need to treat them as if they were. */
232 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
233 || TREE_CODE (decl) == FUNCTION_DECL)
235 timevar_push (TV_VARCONST);
237 /* Don't output anything when a tentative file-scope definition
238 is seen. But at end of compilation, do output code for them.
240 We do output all variables and rely on
241 callgraph code to defer them except for forward declarations
242 (see gcc.c-torture/compile/920624-1.c) */
243 if ((at_end
244 || !DECL_DEFER_OUTPUT (decl)
245 || DECL_INITIAL (decl))
246 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
247 && !DECL_EXTERNAL (decl))
249 /* When reading LTO unit, we also read varpool, so do not
250 rebuild it. */
251 if (in_lto_p && !at_end)
253 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
254 varpool_node::finalize_decl (decl);
257 #ifdef ASM_FINISH_DECLARE_OBJECT
258 if (decl == last_assemble_variable_decl)
260 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
261 top_level, at_end);
263 #endif
265 timevar_pop (TV_VARCONST);
267 else if (TREE_CODE (decl) == TYPE_DECL
268 /* Like in rest_of_type_compilation, avoid confusing the debug
269 information machinery when there are errors. */
270 && !seen_error ())
272 timevar_push (TV_SYMOUT);
273 debug_hooks->type_decl (decl, !top_level);
274 timevar_pop (TV_SYMOUT);
277 /* Let cgraph know about the existence of variables. */
278 if (in_lto_p && !at_end)
280 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
281 && TREE_STATIC (decl))
282 varpool_node::get_create (decl);
284 /* Generate early debug for global variables. Any local variables will
285 be handled by either handling reachable functions from
286 finalize_compilation_unit (and by consequence, locally scoped
287 symbols), or by rest_of_type_compilation below.
289 Also, pick up function prototypes, which will be mostly ignored
290 by the different early_global_decl() hooks, but will at least be
291 used by Go's hijack of the debug_hooks to implement
292 -fdump-go-spec. */
293 if (!in_lto_p
294 && (TREE_CODE (decl) != FUNCTION_DECL
295 /* This will pick up function prototypes with no bodies,
296 which are not visible in finalize_compilation_unit()
297 while iterating with FOR_EACH_*_FUNCTION through the
298 symbol table. */
299 || !DECL_SAVED_TREE (decl))
301 /* We need to check both decl_function_context and
302 current_function_decl here to make sure local extern
303 declarations end up with the correct context.
305 For local extern declarations, decl_function_context is
306 empty, but current_function_decl is set to the function where
307 the extern was declared . Without the check for
308 !current_function_decl below, the local extern ends up
309 incorrectly with a top-level context.
311 For example:
313 namespace S
319 int i = 42;
321 extern int i; // Local extern declaration.
322 return i;
328 && !decl_function_context (decl)
329 && !current_function_decl
330 && DECL_SOURCE_LOCATION (decl) != BUILTINS_LOCATION
331 && !decl_type_context (decl))
332 (*debug_hooks->early_global_decl) (decl);
335 /* Called after finishing a record, union or enumeral type. */
337 void
338 rest_of_type_compilation (tree type, int toplev)
340 /* Avoid confusing the debug information machinery when there are
341 errors. */
342 if (seen_error ())
343 return;
345 timevar_push (TV_SYMOUT);
346 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
347 timevar_pop (TV_SYMOUT);
352 void
353 pass_manager::
354 finish_optimization_passes (void)
356 int i;
357 struct dump_file_info *dfi;
358 char *name;
359 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
361 timevar_push (TV_DUMP);
362 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
364 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
365 end_branch_prob ();
366 dumps->dump_finish (pass_profile_1->static_pass_number);
369 if (optimize > 0)
371 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
372 print_combine_total_stats ();
373 dumps->dump_finish (pass_profile_1->static_pass_number);
376 /* Do whatever is necessary to finish printing the graphs. */
377 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
378 if (dumps->dump_initialized_p (i)
379 && (dfi->pflags & TDF_GRAPH) != 0
380 && (name = dumps->get_dump_file_name (i)) != NULL)
382 finish_graph_dump_file (name);
383 free (name);
386 timevar_pop (TV_DUMP);
389 static unsigned int
390 execute_build_ssa_passes (void)
392 /* Once this pass (and its sub-passes) are complete, all functions
393 will be in SSA form. Technically this state change is happening
394 a tad early, since the sub-passes have not yet run, but since
395 none of the sub-passes are IPA passes and do not create new
396 functions, this is ok. We're setting this value for the benefit
397 of IPA passes that follow. */
398 if (symtab->state < IPA_SSA)
399 symtab->state = IPA_SSA;
400 return 0;
403 namespace {
405 const pass_data pass_data_build_ssa_passes =
407 SIMPLE_IPA_PASS, /* type */
408 "build_ssa_passes", /* name */
409 OPTGROUP_NONE, /* optinfo_flags */
410 TV_EARLY_LOCAL, /* tv_id */
411 0, /* properties_required */
412 0, /* properties_provided */
413 0, /* properties_destroyed */
414 0, /* todo_flags_start */
415 /* todo_flags_finish is executed before subpases. For this reason
416 it makes no sense to remove unreachable functions here. */
417 0, /* todo_flags_finish */
420 class pass_build_ssa_passes : public simple_ipa_opt_pass
422 public:
423 pass_build_ssa_passes (gcc::context *ctxt)
424 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
427 /* opt_pass methods: */
428 virtual bool gate (function *)
430 /* Don't bother doing anything if the program has errors. */
431 return (!seen_error () && !in_lto_p);
434 virtual unsigned int execute (function *)
436 return execute_build_ssa_passes ();
439 }; // class pass_build_ssa_passes
441 const pass_data pass_data_chkp_instrumentation_passes =
443 SIMPLE_IPA_PASS, /* type */
444 "chkp_passes", /* name */
445 OPTGROUP_NONE, /* optinfo_flags */
446 TV_NONE, /* 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_chkp_instrumentation_passes : public simple_ipa_opt_pass
456 public:
457 pass_chkp_instrumentation_passes (gcc::context *ctxt)
458 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
461 /* opt_pass methods: */
462 virtual bool gate (function *)
464 /* Don't bother doing anything if the program has errors. */
465 return (flag_check_pointer_bounds
466 && !seen_error () && !in_lto_p);
469 }; // class pass_chkp_instrumentation_passes
471 const pass_data pass_data_local_optimization_passes =
473 SIMPLE_IPA_PASS, /* type */
474 "opt_local_passes", /* name */
475 OPTGROUP_NONE, /* optinfo_flags */
476 TV_NONE, /* tv_id */
477 0, /* properties_required */
478 0, /* properties_provided */
479 0, /* properties_destroyed */
480 0, /* todo_flags_start */
481 0, /* todo_flags_finish */
484 class pass_local_optimization_passes : public simple_ipa_opt_pass
486 public:
487 pass_local_optimization_passes (gcc::context *ctxt)
488 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
491 /* opt_pass methods: */
492 virtual bool gate (function *)
494 /* Don't bother doing anything if the program has errors. */
495 return (!seen_error () && !in_lto_p);
498 }; // class pass_local_optimization_passes
500 } // anon namespace
502 simple_ipa_opt_pass *
503 make_pass_build_ssa_passes (gcc::context *ctxt)
505 return new pass_build_ssa_passes (ctxt);
508 simple_ipa_opt_pass *
509 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
511 return new pass_chkp_instrumentation_passes (ctxt);
514 simple_ipa_opt_pass *
515 make_pass_local_optimization_passes (gcc::context *ctxt)
517 return new pass_local_optimization_passes (ctxt);
520 namespace {
522 const pass_data pass_data_all_early_optimizations =
524 GIMPLE_PASS, /* type */
525 "early_optimizations", /* name */
526 OPTGROUP_NONE, /* optinfo_flags */
527 TV_NONE, /* tv_id */
528 0, /* properties_required */
529 0, /* properties_provided */
530 0, /* properties_destroyed */
531 0, /* todo_flags_start */
532 0, /* todo_flags_finish */
535 class pass_all_early_optimizations : public gimple_opt_pass
537 public:
538 pass_all_early_optimizations (gcc::context *ctxt)
539 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
542 /* opt_pass methods: */
543 virtual bool gate (function *)
545 return (optimize >= 1
546 /* Don't bother doing anything if the program has errors. */
547 && !seen_error ());
550 }; // class pass_all_early_optimizations
552 } // anon namespace
554 static gimple_opt_pass *
555 make_pass_all_early_optimizations (gcc::context *ctxt)
557 return new pass_all_early_optimizations (ctxt);
560 namespace {
562 const pass_data pass_data_all_optimizations =
564 GIMPLE_PASS, /* type */
565 "*all_optimizations", /* name */
566 OPTGROUP_NONE, /* optinfo_flags */
567 TV_OPTIMIZE, /* tv_id */
568 0, /* properties_required */
569 0, /* properties_provided */
570 0, /* properties_destroyed */
571 0, /* todo_flags_start */
572 0, /* todo_flags_finish */
575 class pass_all_optimizations : public gimple_opt_pass
577 public:
578 pass_all_optimizations (gcc::context *ctxt)
579 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
582 /* opt_pass methods: */
583 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
585 }; // class pass_all_optimizations
587 } // anon namespace
589 static gimple_opt_pass *
590 make_pass_all_optimizations (gcc::context *ctxt)
592 return new pass_all_optimizations (ctxt);
595 namespace {
597 const pass_data pass_data_all_optimizations_g =
599 GIMPLE_PASS, /* type */
600 "*all_optimizations_g", /* name */
601 OPTGROUP_NONE, /* optinfo_flags */
602 TV_OPTIMIZE, /* tv_id */
603 0, /* properties_required */
604 0, /* properties_provided */
605 0, /* properties_destroyed */
606 0, /* todo_flags_start */
607 0, /* todo_flags_finish */
610 class pass_all_optimizations_g : public gimple_opt_pass
612 public:
613 pass_all_optimizations_g (gcc::context *ctxt)
614 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
617 /* opt_pass methods: */
618 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
620 }; // class pass_all_optimizations_g
622 } // anon namespace
624 static gimple_opt_pass *
625 make_pass_all_optimizations_g (gcc::context *ctxt)
627 return new pass_all_optimizations_g (ctxt);
630 namespace {
632 const pass_data pass_data_rest_of_compilation =
634 RTL_PASS, /* type */
635 "*rest_of_compilation", /* name */
636 OPTGROUP_NONE, /* optinfo_flags */
637 TV_REST_OF_COMPILATION, /* tv_id */
638 PROP_rtl, /* properties_required */
639 0, /* properties_provided */
640 0, /* properties_destroyed */
641 0, /* todo_flags_start */
642 0, /* todo_flags_finish */
645 class pass_rest_of_compilation : public rtl_opt_pass
647 public:
648 pass_rest_of_compilation (gcc::context *ctxt)
649 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
652 /* opt_pass methods: */
653 virtual bool gate (function *)
655 /* Early return if there were errors. We can run afoul of our
656 consistency checks, and there's not really much point in fixing them. */
657 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
660 }; // class pass_rest_of_compilation
662 } // anon namespace
664 static rtl_opt_pass *
665 make_pass_rest_of_compilation (gcc::context *ctxt)
667 return new pass_rest_of_compilation (ctxt);
670 namespace {
672 const pass_data pass_data_postreload =
674 RTL_PASS, /* type */
675 "*all-postreload", /* name */
676 OPTGROUP_NONE, /* optinfo_flags */
677 TV_POSTRELOAD, /* tv_id */
678 PROP_rtl, /* properties_required */
679 0, /* properties_provided */
680 0, /* properties_destroyed */
681 0, /* todo_flags_start */
682 0, /* todo_flags_finish */
685 class pass_postreload : public rtl_opt_pass
687 public:
688 pass_postreload (gcc::context *ctxt)
689 : rtl_opt_pass (pass_data_postreload, ctxt)
692 /* opt_pass methods: */
693 virtual bool gate (function *) { return reload_completed; }
695 }; // class pass_postreload
697 } // anon namespace
699 static rtl_opt_pass *
700 make_pass_postreload (gcc::context *ctxt)
702 return new pass_postreload (ctxt);
705 namespace {
707 const pass_data pass_data_late_compilation =
709 RTL_PASS, /* type */
710 "*all-late_compilation", /* name */
711 OPTGROUP_NONE, /* optinfo_flags */
712 TV_LATE_COMPILATION, /* tv_id */
713 PROP_rtl, /* properties_required */
714 0, /* properties_provided */
715 0, /* properties_destroyed */
716 0, /* todo_flags_start */
717 0, /* todo_flags_finish */
720 class pass_late_compilation : public rtl_opt_pass
722 public:
723 pass_late_compilation (gcc::context *ctxt)
724 : rtl_opt_pass (pass_data_late_compilation, ctxt)
727 /* opt_pass methods: */
728 virtual bool gate (function *)
730 return reload_completed || targetm.no_register_allocation;
733 }; // class pass_late_compilation
735 } // anon namespace
737 static rtl_opt_pass *
738 make_pass_late_compilation (gcc::context *ctxt)
740 return new pass_late_compilation (ctxt);
745 /* Set the static pass number of pass PASS to ID and record that
746 in the mapping from static pass number to pass. */
748 void
749 pass_manager::
750 set_pass_for_id (int id, opt_pass *pass)
752 pass->static_pass_number = id;
753 if (passes_by_id_size <= id)
755 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
756 memset (passes_by_id + passes_by_id_size, 0,
757 (id + 1 - passes_by_id_size) * sizeof (void *));
758 passes_by_id_size = id + 1;
760 passes_by_id[id] = pass;
763 /* Return the pass with the static pass number ID. */
765 opt_pass *
766 pass_manager::get_pass_for_id (int id) const
768 if (id >= passes_by_id_size)
769 return NULL;
770 return passes_by_id[id];
773 /* Iterate over the pass tree allocating dump file numbers. We want
774 to do this depth first, and independent of whether the pass is
775 enabled or not. */
777 void
778 register_one_dump_file (opt_pass *pass)
780 g->get_passes ()->register_one_dump_file (pass);
783 void
784 pass_manager::register_one_dump_file (opt_pass *pass)
786 char *dot_name, *flag_name, *glob_name;
787 const char *name, *full_name, *prefix;
788 char num[10];
789 int flags, id;
790 int optgroup_flags = OPTGROUP_NONE;
791 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
793 /* See below in next_pass_1. */
794 num[0] = '\0';
795 if (pass->static_pass_number != -1)
796 sprintf (num, "%d", ((int) pass->static_pass_number < 0
797 ? 1 : pass->static_pass_number));
799 /* The name is both used to identify the pass for the purposes of plugins,
800 and to specify dump file name and option.
801 The latter two might want something short which is not quite unique; for
802 that reason, we may have a disambiguating prefix, followed by a space
803 to mark the start of the following dump file name / option string. */
804 name = strchr (pass->name, ' ');
805 name = name ? name + 1 : pass->name;
806 dot_name = concat (".", name, num, NULL);
807 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
809 prefix = "ipa-";
810 flags = TDF_IPA;
811 optgroup_flags |= OPTGROUP_IPA;
813 else if (pass->type == GIMPLE_PASS)
815 prefix = "tree-";
816 flags = TDF_TREE;
818 else
820 prefix = "rtl-";
821 flags = TDF_RTL;
824 flag_name = concat (prefix, name, num, NULL);
825 glob_name = concat (prefix, name, NULL);
826 optgroup_flags |= pass->optinfo_flags;
827 /* For any passes that do not have an optgroup set, and which are not
828 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
829 any dump messages are emitted properly under -fopt-info(-optall). */
830 if (optgroup_flags == OPTGROUP_NONE)
831 optgroup_flags = OPTGROUP_OTHER;
832 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
833 optgroup_flags,
834 true);
835 set_pass_for_id (id, pass);
836 full_name = concat (prefix, pass->name, num, NULL);
837 register_pass_name (pass, full_name);
838 free (CONST_CAST (char *, full_name));
841 /* Register the dump files for the pass_manager starting at PASS. */
843 void
844 pass_manager::register_dump_files (opt_pass *pass)
848 if (pass->name && pass->name[0] != '*')
849 register_one_dump_file (pass);
851 if (pass->sub)
852 register_dump_files (pass->sub);
854 pass = pass->next;
856 while (pass);
859 static hash_map<nofree_string_hash, opt_pass *> *name_to_pass_map;
861 /* Register PASS with NAME. */
863 static void
864 register_pass_name (opt_pass *pass, const char *name)
866 if (!name_to_pass_map)
867 name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
869 if (name_to_pass_map->get (name))
870 return; /* Ignore plugin passes. */
872 const char *unique_name = xstrdup (name);
873 name_to_pass_map->put (unique_name, pass);
876 /* Map from pass id to canonicalized pass name. */
878 typedef const char *char_ptr;
879 static vec<char_ptr> pass_tab = vNULL;
881 /* Callback function for traversing NAME_TO_PASS_MAP. */
883 bool
884 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
886 gcc_assert (pass->static_pass_number > 0);
887 gcc_assert (pass_tab.exists ());
889 pass_tab[pass->static_pass_number] = name;
891 return 1;
894 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
895 table for dumping purpose. */
897 static void
898 create_pass_tab (void)
900 if (!flag_dump_passes)
901 return;
903 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
904 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
907 static bool override_gate_status (opt_pass *, tree, bool);
909 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
910 is turned on or not. */
912 static void
913 dump_one_pass (opt_pass *pass, int pass_indent)
915 int indent = 3 * pass_indent;
916 const char *pn;
917 bool is_on, is_really_on;
919 is_on = pass->gate (cfun);
920 is_really_on = override_gate_status (pass, current_function_decl, is_on);
922 if (pass->static_pass_number <= 0)
923 pn = pass->name;
924 else
925 pn = pass_tab[pass->static_pass_number];
927 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
928 (15 - indent < 0 ? 0 : 15 - indent), " ",
929 is_on ? " ON" : " OFF",
930 ((!is_on) == (!is_really_on) ? ""
931 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
934 /* Dump pass list PASS with indentation INDENT. */
936 static void
937 dump_pass_list (opt_pass *pass, int indent)
941 dump_one_pass (pass, indent);
942 if (pass->sub)
943 dump_pass_list (pass->sub, indent + 1);
944 pass = pass->next;
946 while (pass);
949 /* Dump all optimization passes. */
951 void
952 dump_passes (void)
954 g->get_passes ()->dump_passes ();
957 void
958 pass_manager::dump_passes () const
960 push_dummy_function (true);
962 create_pass_tab ();
964 dump_pass_list (all_lowering_passes, 1);
965 dump_pass_list (all_small_ipa_passes, 1);
966 dump_pass_list (all_regular_ipa_passes, 1);
967 dump_pass_list (all_late_ipa_passes, 1);
968 dump_pass_list (all_passes, 1);
970 pop_dummy_function ();
973 /* Returns the pass with NAME. */
975 static opt_pass *
976 get_pass_by_name (const char *name)
978 opt_pass **p = name_to_pass_map->get (name);
979 if (p)
980 return *p;
982 return NULL;
986 /* Range [start, last]. */
988 struct uid_range
990 unsigned int start;
991 unsigned int last;
992 const char *assem_name;
993 struct uid_range *next;
996 typedef struct uid_range *uid_range_p;
999 static vec<uid_range_p>
1000 enabled_pass_uid_range_tab = vNULL;
1001 static vec<uid_range_p>
1002 disabled_pass_uid_range_tab = vNULL;
1005 /* Parse option string for -fdisable- and -fenable-
1006 The syntax of the options:
1008 -fenable-<pass_name>
1009 -fdisable-<pass_name>
1011 -fenable-<pass_name>=s1:e1,s2:e2,...
1012 -fdisable-<pass_name>=s1:e1,s2:e2,...
1015 static void
1016 enable_disable_pass (const char *arg, bool is_enable)
1018 opt_pass *pass;
1019 char *range_str, *phase_name;
1020 char *argstr = xstrdup (arg);
1021 vec<uid_range_p> *tab = 0;
1023 range_str = strchr (argstr,'=');
1024 if (range_str)
1026 *range_str = '\0';
1027 range_str++;
1030 phase_name = argstr;
1031 if (!*phase_name)
1033 if (is_enable)
1034 error ("unrecognized option -fenable");
1035 else
1036 error ("unrecognized option -fdisable");
1037 free (argstr);
1038 return;
1040 pass = get_pass_by_name (phase_name);
1041 if (!pass || pass->static_pass_number == -1)
1043 if (is_enable)
1044 error ("unknown pass %s specified in -fenable", phase_name);
1045 else
1046 error ("unknown pass %s specified in -fdisable", phase_name);
1047 free (argstr);
1048 return;
1051 if (is_enable)
1052 tab = &enabled_pass_uid_range_tab;
1053 else
1054 tab = &disabled_pass_uid_range_tab;
1056 if ((unsigned) pass->static_pass_number >= tab->length ())
1057 tab->safe_grow_cleared (pass->static_pass_number + 1);
1059 if (!range_str)
1061 uid_range_p slot;
1062 uid_range_p new_range = XCNEW (struct uid_range);
1064 new_range->start = 0;
1065 new_range->last = (unsigned)-1;
1067 slot = (*tab)[pass->static_pass_number];
1068 new_range->next = slot;
1069 (*tab)[pass->static_pass_number] = new_range;
1070 if (is_enable)
1071 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1072 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1073 else
1074 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1075 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1077 else
1079 char *next_range = NULL;
1080 char *one_range = range_str;
1081 char *end_val = NULL;
1085 uid_range_p slot;
1086 uid_range_p new_range;
1087 char *invalid = NULL;
1088 long start;
1089 char *func_name = NULL;
1091 next_range = strchr (one_range, ',');
1092 if (next_range)
1094 *next_range = '\0';
1095 next_range++;
1098 end_val = strchr (one_range, ':');
1099 if (end_val)
1101 *end_val = '\0';
1102 end_val++;
1104 start = strtol (one_range, &invalid, 10);
1105 if (*invalid || start < 0)
1107 if (end_val || (one_range[0] >= '0'
1108 && one_range[0] <= '9'))
1110 error ("Invalid range %s in option %s",
1111 one_range,
1112 is_enable ? "-fenable" : "-fdisable");
1113 free (argstr);
1114 return;
1116 func_name = one_range;
1118 if (!end_val)
1120 new_range = XCNEW (struct uid_range);
1121 if (!func_name)
1123 new_range->start = (unsigned) start;
1124 new_range->last = (unsigned) start;
1126 else
1128 new_range->start = (unsigned) -1;
1129 new_range->last = (unsigned) -1;
1130 new_range->assem_name = xstrdup (func_name);
1133 else
1135 long last = strtol (end_val, &invalid, 10);
1136 if (*invalid || last < start)
1138 error ("Invalid range %s in option %s",
1139 end_val,
1140 is_enable ? "-fenable" : "-fdisable");
1141 free (argstr);
1142 return;
1144 new_range = XCNEW (struct uid_range);
1145 new_range->start = (unsigned) start;
1146 new_range->last = (unsigned) last;
1149 slot = (*tab)[pass->static_pass_number];
1150 new_range->next = slot;
1151 (*tab)[pass->static_pass_number] = new_range;
1152 if (is_enable)
1154 if (new_range->assem_name)
1155 inform (UNKNOWN_LOCATION,
1156 "enable pass %s for function %s",
1157 phase_name, new_range->assem_name);
1158 else
1159 inform (UNKNOWN_LOCATION,
1160 "enable pass %s for functions in the range of [%u, %u]",
1161 phase_name, new_range->start, new_range->last);
1163 else
1165 if (new_range->assem_name)
1166 inform (UNKNOWN_LOCATION,
1167 "disable pass %s for function %s",
1168 phase_name, new_range->assem_name);
1169 else
1170 inform (UNKNOWN_LOCATION,
1171 "disable pass %s for functions in the range of [%u, %u]",
1172 phase_name, new_range->start, new_range->last);
1175 one_range = next_range;
1176 } while (next_range);
1179 free (argstr);
1182 /* Enable pass specified by ARG. */
1184 void
1185 enable_pass (const char *arg)
1187 enable_disable_pass (arg, true);
1190 /* Disable pass specified by ARG. */
1192 void
1193 disable_pass (const char *arg)
1195 enable_disable_pass (arg, false);
1198 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1200 static bool
1201 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1202 tree func,
1203 vec<uid_range_p> tab)
1205 uid_range_p slot, range;
1206 int cgraph_uid;
1207 const char *aname = NULL;
1209 if (!tab.exists ()
1210 || (unsigned) pass->static_pass_number >= tab.length ()
1211 || pass->static_pass_number == -1)
1212 return false;
1214 slot = tab[pass->static_pass_number];
1215 if (!slot)
1216 return false;
1218 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1219 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1220 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1222 range = slot;
1223 while (range)
1225 if ((unsigned) cgraph_uid >= range->start
1226 && (unsigned) cgraph_uid <= range->last)
1227 return true;
1228 if (range->assem_name && aname
1229 && !strcmp (range->assem_name, aname))
1230 return true;
1231 range = range->next;
1234 return false;
1238 /* Update static_pass_number for passes (and the flag
1239 TODO_mark_first_instance).
1241 Passes are constructed with static_pass_number preinitialized to 0
1243 This field is used in two different ways: initially as instance numbers
1244 of their kind, and then as ids within the entire pass manager.
1246 Within pass_manager::pass_manager:
1248 * In add_pass_instance(), as called by next_pass_1 in
1249 NEXT_PASS in init_optimization_passes
1251 * When the initial instance of a pass within a pass manager is seen,
1252 it is flagged, and its static_pass_number is set to -1
1254 * On subsequent times that it is seen, the static pass number
1255 is decremented each time, so that if there are e.g. 4 dups,
1256 they have static_pass_number -4, 2, 3, 4 respectively (note
1257 how the initial one is negative and gives the count); these
1258 can be thought of as instance numbers of the specific pass
1260 * Within the register_dump_files () traversal, set_pass_for_id()
1261 is called on each pass, using these instance numbers to create
1262 dumpfile switches, and then overwriting them with a pass id,
1263 which are global to the whole pass manager (based on
1264 (TDI_end + current value of extra_dump_files_in_use) ) */
1266 static void
1267 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1268 opt_pass *initial_pass)
1270 /* Are we dealing with the first pass of its kind, or a clone? */
1271 if (new_pass != initial_pass)
1273 /* We're dealing with a clone. */
1274 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1276 /* Indicate to register_dump_files that this pass has duplicates,
1277 and so it should rename the dump file. The first instance will
1278 be -1, and be number of duplicates = -static_pass_number - 1.
1279 Subsequent instances will be > 0 and just the duplicate number. */
1280 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1282 initial_pass->static_pass_number -= 1;
1283 new_pass->static_pass_number = -initial_pass->static_pass_number;
1286 else
1288 /* We're dealing with the first pass of its kind. */
1289 new_pass->todo_flags_start |= TODO_mark_first_instance;
1290 new_pass->static_pass_number = -1;
1292 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1296 /* Add a pass to the pass list. Duplicate the pass if it's already
1297 in the list. */
1299 static opt_pass **
1300 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1302 /* Every pass should have a name so that plugins can refer to them. */
1303 gcc_assert (pass->name != NULL);
1305 add_pass_instance (pass, false, initial_pass);
1306 *list = pass;
1308 return &(*list)->next;
1311 /* List node for an inserted pass instance. We need to keep track of all
1312 the newly-added pass instances (with 'added_pass_nodes' defined below)
1313 so that we can register their dump files after pass-positioning is finished.
1314 Registering dumping files needs to be post-processed or the
1315 static_pass_number of the opt_pass object would be modified and mess up
1316 the dump file names of future pass instances to be added. */
1318 struct pass_list_node
1320 opt_pass *pass;
1321 struct pass_list_node *next;
1324 static struct pass_list_node *added_pass_nodes = NULL;
1325 static struct pass_list_node *prev_added_pass_node;
1327 /* Insert the pass at the proper position. Return true if the pass
1328 is successfully added.
1330 NEW_PASS_INFO - new pass to be inserted
1331 PASS_LIST - root of the pass list to insert the new pass to */
1333 static bool
1334 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1336 opt_pass *pass = *pass_list, *prev_pass = NULL;
1337 bool success = false;
1339 for ( ; pass; prev_pass = pass, pass = pass->next)
1341 /* Check if the current pass is of the same type as the new pass and
1342 matches the name and the instance number of the reference pass. */
1343 if (pass->type == new_pass_info->pass->type
1344 && pass->name
1345 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1346 && ((new_pass_info->ref_pass_instance_number == 0)
1347 || (new_pass_info->ref_pass_instance_number ==
1348 pass->static_pass_number)
1349 || (new_pass_info->ref_pass_instance_number == 1
1350 && pass->todo_flags_start & TODO_mark_first_instance)))
1352 opt_pass *new_pass;
1353 struct pass_list_node *new_pass_node;
1355 if (new_pass_info->ref_pass_instance_number == 0)
1357 new_pass = new_pass_info->pass->clone ();
1358 add_pass_instance (new_pass, true, new_pass_info->pass);
1360 else
1362 new_pass = new_pass_info->pass;
1363 add_pass_instance (new_pass, true, new_pass);
1366 /* Insert the new pass instance based on the positioning op. */
1367 switch (new_pass_info->pos_op)
1369 case PASS_POS_INSERT_AFTER:
1370 new_pass->next = pass->next;
1371 pass->next = new_pass;
1373 /* Skip newly inserted pass to avoid repeated
1374 insertions in the case where the new pass and the
1375 existing one have the same name. */
1376 pass = new_pass;
1377 break;
1378 case PASS_POS_INSERT_BEFORE:
1379 new_pass->next = pass;
1380 if (prev_pass)
1381 prev_pass->next = new_pass;
1382 else
1383 *pass_list = new_pass;
1384 break;
1385 case PASS_POS_REPLACE:
1386 new_pass->next = pass->next;
1387 if (prev_pass)
1388 prev_pass->next = new_pass;
1389 else
1390 *pass_list = new_pass;
1391 new_pass->sub = pass->sub;
1392 new_pass->tv_id = pass->tv_id;
1393 pass = new_pass;
1394 break;
1395 default:
1396 error ("invalid pass positioning operation");
1397 return false;
1400 /* Save the newly added pass (instance) in the added_pass_nodes
1401 list so that we can register its dump file later. Note that
1402 we cannot register the dump file now because doing so will modify
1403 the static_pass_number of the opt_pass object and therefore
1404 mess up the dump file name of future instances. */
1405 new_pass_node = XCNEW (struct pass_list_node);
1406 new_pass_node->pass = new_pass;
1407 if (!added_pass_nodes)
1408 added_pass_nodes = new_pass_node;
1409 else
1410 prev_added_pass_node->next = new_pass_node;
1411 prev_added_pass_node = new_pass_node;
1413 success = true;
1416 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1417 success = true;
1420 return success;
1423 /* Hooks a new pass into the pass lists.
1425 PASS_INFO - pass information that specifies the opt_pass object,
1426 reference pass, instance number, and how to position
1427 the pass */
1429 void
1430 register_pass (struct register_pass_info *pass_info)
1432 g->get_passes ()->register_pass (pass_info);
1435 void
1436 register_pass (opt_pass* pass, pass_positioning_ops pos,
1437 const char* ref_pass_name, int ref_pass_inst_number)
1439 register_pass_info i;
1440 i.pass = pass;
1441 i.reference_pass_name = ref_pass_name;
1442 i.ref_pass_instance_number = ref_pass_inst_number;
1443 i.pos_op = pos;
1445 g->get_passes ()->register_pass (&i);
1448 void
1449 pass_manager::register_pass (struct register_pass_info *pass_info)
1451 bool all_instances, success;
1452 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1454 /* The checks below could fail in buggy plugins. Existing GCC
1455 passes should never fail these checks, so we mention plugin in
1456 the messages. */
1457 if (!pass_info->pass)
1458 fatal_error (input_location, "plugin cannot register a missing pass");
1460 if (!pass_info->pass->name)
1461 fatal_error (input_location, "plugin cannot register an unnamed pass");
1463 if (!pass_info->reference_pass_name)
1464 fatal_error
1465 (input_location,
1466 "plugin cannot register pass %qs without reference pass name",
1467 pass_info->pass->name);
1469 /* Try to insert the new pass to the pass lists. We need to check
1470 all five lists as the reference pass could be in one (or all) of
1471 them. */
1472 all_instances = pass_info->ref_pass_instance_number == 0;
1473 success = position_pass (pass_info, &all_lowering_passes);
1474 if (!success || all_instances)
1475 success |= position_pass (pass_info, &all_small_ipa_passes);
1476 if (!success || all_instances)
1477 success |= position_pass (pass_info, &all_regular_ipa_passes);
1478 if (!success || all_instances)
1479 success |= position_pass (pass_info, &all_late_ipa_passes);
1480 if (!success || all_instances)
1481 success |= position_pass (pass_info, &all_passes);
1482 if (!success)
1483 fatal_error
1484 (input_location,
1485 "pass %qs not found but is referenced by new pass %qs",
1486 pass_info->reference_pass_name, pass_info->pass->name);
1488 /* OK, we have successfully inserted the new pass. We need to register
1489 the dump files for the newly added pass and its duplicates (if any).
1490 Because the registration of plugin/backend passes happens after the
1491 command-line options are parsed, the options that specify single
1492 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1493 passes. Therefore we currently can only enable dumping of
1494 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1495 are specified. While doing so, we also delete the pass_list_node
1496 objects created during pass positioning. */
1497 while (added_pass_nodes)
1499 struct pass_list_node *next_node = added_pass_nodes->next;
1500 enum tree_dump_index tdi;
1501 register_one_dump_file (added_pass_nodes->pass);
1502 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1503 || added_pass_nodes->pass->type == IPA_PASS)
1504 tdi = TDI_ipa_all;
1505 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1506 tdi = TDI_tree_all;
1507 else
1508 tdi = TDI_rtl_all;
1509 /* Check if dump-all flag is specified. */
1510 if (dumps->get_dump_file_info (tdi)->pstate)
1511 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1512 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1513 XDELETE (added_pass_nodes);
1514 added_pass_nodes = next_node;
1518 /* Construct the pass tree. The sequencing of passes is driven by
1519 the cgraph routines:
1521 finalize_compilation_unit ()
1522 for each node N in the cgraph
1523 cgraph_analyze_function (N)
1524 cgraph_lower_function (N) -> all_lowering_passes
1526 If we are optimizing, compile is then invoked:
1528 compile ()
1529 ipa_passes () -> all_small_ipa_passes
1530 -> Analysis of all_regular_ipa_passes
1531 * possible LTO streaming at copmilation time *
1532 -> Execution of all_regular_ipa_passes
1533 * possible LTO streaming at link time *
1534 -> all_late_ipa_passes
1535 expand_all_functions ()
1536 for each node N in the cgraph
1537 expand_function (N) -> Transformation of all_regular_ipa_passes
1538 -> all_passes
1541 void *
1542 pass_manager::operator new (size_t sz)
1544 /* Ensure that all fields of the pass manager are zero-initialized. */
1545 return xcalloc (1, sz);
1548 void
1549 pass_manager::operator delete (void *ptr)
1551 free (ptr);
1554 pass_manager::pass_manager (context *ctxt)
1555 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1556 all_regular_ipa_passes (NULL),
1557 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1558 m_ctxt (ctxt)
1560 opt_pass **p;
1562 /* Initialize the pass_lists array. */
1563 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1564 GCC_PASS_LISTS
1565 #undef DEF_PASS_LIST
1567 /* Build the tree of passes. */
1569 #define INSERT_PASSES_AFTER(PASS) \
1570 p = &(PASS);
1572 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1574 opt_pass **p = &(PASS ## _1)->sub;
1576 #define POP_INSERT_PASSES() \
1579 #define NEXT_PASS(PASS, NUM) \
1580 do { \
1581 gcc_assert (NULL == PASS ## _ ## NUM); \
1582 if ((NUM) == 1) \
1583 PASS ## _1 = make_##PASS (m_ctxt); \
1584 else \
1586 gcc_assert (PASS ## _1); \
1587 PASS ## _ ## NUM = PASS ## _1->clone (); \
1589 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1590 } while (0)
1592 #define TERMINATE_PASS_LIST() \
1593 *p = NULL;
1595 #include "pass-instances.def"
1597 #undef INSERT_PASSES_AFTER
1598 #undef PUSH_INSERT_PASSES_WITHIN
1599 #undef POP_INSERT_PASSES
1600 #undef NEXT_PASS
1601 #undef TERMINATE_PASS_LIST
1603 /* Register the passes with the tree dump code. */
1604 register_dump_files (all_lowering_passes);
1605 register_dump_files (all_small_ipa_passes);
1606 register_dump_files (all_regular_ipa_passes);
1607 register_dump_files (all_late_ipa_passes);
1608 register_dump_files (all_passes);
1611 static void
1612 delete_pass_tree (opt_pass *pass)
1614 while (pass)
1616 /* Recurse into child passes. */
1617 delete_pass_tree (pass->sub);
1619 opt_pass *next = pass->next;
1621 /* Delete this pass. */
1622 delete pass;
1624 /* Iterate onto sibling passes. */
1625 pass = next;
1629 pass_manager::~pass_manager ()
1631 XDELETEVEC (passes_by_id);
1633 /* Call delete_pass_tree on each of the pass_lists. */
1634 #define DEF_PASS_LIST(LIST) \
1635 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1636 GCC_PASS_LISTS
1637 #undef DEF_PASS_LIST
1641 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1642 function CALLBACK for every function in the call graph. Otherwise,
1643 call CALLBACK on the current function. */
1645 static void
1646 do_per_function (void (*callback) (function *, void *data), void *data)
1648 if (current_function_decl)
1649 callback (cfun, data);
1650 else
1652 struct cgraph_node *node;
1653 FOR_EACH_DEFINED_FUNCTION (node)
1654 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1655 && (!node->clone_of || node->decl != node->clone_of->decl))
1656 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1660 /* Because inlining might remove no-longer reachable nodes, we need to
1661 keep the array visible to garbage collector to avoid reading collected
1662 out nodes. */
1663 static int nnodes;
1664 static GTY ((length ("nnodes"))) cgraph_node **order;
1666 /* Hook called when NODE is removed and therefore should be
1667 excluded from order vector. DATA is an array of integers.
1668 DATA[0] holds max index it may be accessed by. For cgraph
1669 node DATA[node->uid + 1] holds index of this node in order
1670 vector. */
1671 static void
1672 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1674 int *order_idx = (int *)data;
1676 if (node->uid >= order_idx[0])
1677 return;
1679 int idx = order_idx[node->uid + 1];
1680 if (idx >= 0 && idx < nnodes && order[idx] == node)
1681 order[idx] = NULL;
1684 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1685 function CALLBACK for every function in the call graph. Otherwise,
1686 call CALLBACK on the current function.
1687 This function is global so that plugins can use it. */
1688 void
1689 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1691 int i;
1693 if (current_function_decl)
1694 callback (cfun, data);
1695 else
1697 cgraph_node_hook_list *hook;
1698 int *order_idx;
1699 gcc_assert (!order);
1700 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1702 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1703 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1704 order_idx[0] = symtab->cgraph_max_uid;
1706 nnodes = ipa_reverse_postorder (order);
1707 for (i = nnodes - 1; i >= 0; i--)
1709 order[i]->process = 1;
1710 order_idx[order[i]->uid + 1] = i;
1712 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1713 order_idx);
1714 for (i = nnodes - 1; i >= 0; i--)
1716 /* Function could be inlined and removed as unreachable. */
1717 if (!order[i])
1718 continue;
1720 struct cgraph_node *node = order[i];
1722 /* Allow possibly removed nodes to be garbage collected. */
1723 order[i] = NULL;
1724 node->process = 0;
1725 if (node->has_gimple_body_p ())
1726 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1728 symtab->remove_cgraph_removal_hook (hook);
1730 ggc_free (order);
1731 order = NULL;
1732 nnodes = 0;
1735 /* Helper function to perform function body dump. */
1737 static void
1738 execute_function_dump (function *fn, void *data)
1740 opt_pass *pass = (opt_pass *)data;
1742 if (dump_file)
1744 push_cfun (fn);
1746 if (fn->curr_properties & PROP_trees)
1747 dump_function_to_file (fn->decl, dump_file, dump_flags);
1748 else
1749 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1751 /* Flush the file. If verification fails, we won't be able to
1752 close the file before aborting. */
1753 fflush (dump_file);
1755 if ((fn->curr_properties & PROP_cfg)
1756 && (dump_flags & TDF_GRAPH))
1758 if (!pass->graph_dump_initialized)
1760 clean_graph_dump_file (dump_file_name);
1761 pass->graph_dump_initialized = true;
1763 print_graph_cfg (dump_file_name, fn);
1766 pop_cfun ();
1770 static struct profile_record *profile_record;
1772 /* Do profile consistency book-keeping for the pass with static number INDEX.
1773 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1774 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1775 if we are only book-keeping on passes that may have selectively disabled
1776 themselves on a given function. */
1777 static void
1778 check_profile_consistency (int index, int subpass, bool run)
1780 pass_manager *passes = g->get_passes ();
1781 if (index == -1)
1782 return;
1783 if (!profile_record)
1784 profile_record = XCNEWVEC (struct profile_record,
1785 passes->passes_by_id_size);
1786 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1787 gcc_assert (subpass < 2);
1788 profile_record[index].run |= run;
1789 account_profile_record (&profile_record[index], subpass);
1792 /* Output profile consistency. */
1794 void
1795 dump_profile_report (void)
1797 g->get_passes ()->dump_profile_report ();
1800 void
1801 pass_manager::dump_profile_report () const
1803 int i, j;
1804 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1805 gcov_type last_time = 0, last_size = 0;
1806 double rel_time_change, rel_size_change;
1807 int last_reported = 0;
1809 if (!profile_record)
1810 return;
1811 fprintf (stderr, "\nProfile consistency report:\n\n");
1812 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1813 fprintf (stderr, " |freq count |freq count |size time\n");
1815 for (i = 0; i < passes_by_id_size; i++)
1816 for (j = 0 ; j < 2; j++)
1817 if (profile_record[i].run)
1819 if (last_time)
1820 rel_time_change = (profile_record[i].time[j]
1821 - (double)last_time) * 100 / (double)last_time;
1822 else
1823 rel_time_change = 0;
1824 if (last_size)
1825 rel_size_change = (profile_record[i].size[j]
1826 - (double)last_size) * 100 / (double)last_size;
1827 else
1828 rel_size_change = 0;
1830 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1831 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1832 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1833 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1834 || rel_time_change || rel_size_change)
1836 last_reported = i;
1837 fprintf (stderr, "%-20s %s",
1838 passes_by_id [i]->name,
1839 j ? "(after TODO)" : " ");
1840 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1841 fprintf (stderr, "| %+5i",
1842 profile_record[i].num_mismatched_freq_in[j]
1843 - last_freq_in);
1844 else
1845 fprintf (stderr, "| ");
1846 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1847 fprintf (stderr, " %+5i",
1848 profile_record[i].num_mismatched_count_in[j]
1849 - last_count_in);
1850 else
1851 fprintf (stderr, " ");
1852 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1853 fprintf (stderr, "| %+5i",
1854 profile_record[i].num_mismatched_freq_out[j]
1855 - last_freq_out);
1856 else
1857 fprintf (stderr, "| ");
1858 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1859 fprintf (stderr, " %+5i",
1860 profile_record[i].num_mismatched_count_out[j]
1861 - last_count_out);
1862 else
1863 fprintf (stderr, " ");
1865 /* Size/time units change across gimple and RTL. */
1866 if (i == pass_expand_1->static_pass_number)
1867 fprintf (stderr, "|----------");
1868 else
1870 if (rel_size_change)
1871 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1872 else
1873 fprintf (stderr, "| ");
1874 if (rel_time_change)
1875 fprintf (stderr, " %+8.4f%%", rel_time_change);
1877 fprintf (stderr, "\n");
1878 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1879 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1880 last_count_in = profile_record[i].num_mismatched_count_in[j];
1881 last_count_out = profile_record[i].num_mismatched_count_out[j];
1883 else if (j && last_reported != i)
1885 last_reported = i;
1886 fprintf (stderr, "%-20s ------------| | |\n",
1887 passes_by_id [i]->name);
1889 last_time = profile_record[i].time[j];
1890 last_size = profile_record[i].size[j];
1894 /* Perform all TODO actions that ought to be done on each function. */
1896 static void
1897 execute_function_todo (function *fn, void *data)
1899 bool from_ipa_pass = (cfun == NULL);
1900 unsigned int flags = (size_t)data;
1901 flags &= ~fn->last_verified;
1902 if (!flags)
1903 return;
1905 push_cfun (fn);
1907 /* Always cleanup the CFG before trying to update SSA. */
1908 if (flags & TODO_cleanup_cfg)
1910 cleanup_tree_cfg ();
1912 /* When cleanup_tree_cfg merges consecutive blocks, it may
1913 perform some simplistic propagation when removing single
1914 valued PHI nodes. This propagation may, in turn, cause the
1915 SSA form to become out-of-date (see PR 22037). So, even
1916 if the parent pass had not scheduled an SSA update, we may
1917 still need to do one. */
1918 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1919 flags |= TODO_update_ssa;
1922 if (flags & TODO_update_ssa_any)
1924 unsigned update_flags = flags & TODO_update_ssa_any;
1925 update_ssa (update_flags);
1928 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1929 compute_may_aliases ();
1931 if (optimize && (flags & TODO_update_address_taken))
1932 execute_update_addresses_taken ();
1934 if (flags & TODO_remove_unused_locals)
1935 remove_unused_locals ();
1937 if (flags & TODO_rebuild_frequencies)
1938 rebuild_frequencies ();
1940 if (flags & TODO_rebuild_cgraph_edges)
1941 cgraph_edge::rebuild_edges ();
1943 /* If we've seen errors do not bother running any verifiers. */
1944 if (!seen_error ())
1946 #if defined ENABLE_CHECKING
1947 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1948 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1950 if (flags & TODO_verify_il)
1952 if (cfun->curr_properties & PROP_trees)
1954 if (cfun->curr_properties & PROP_cfg)
1955 /* IPA passes leave stmts to be fixed up, so make sure to
1956 not verify stmts really throw. */
1957 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1958 else
1959 verify_gimple_in_seq (gimple_body (cfun->decl));
1961 if (cfun->curr_properties & PROP_ssa)
1962 /* IPA passes leave stmts to be fixed up, so make sure to
1963 not verify SSA operands whose verifier will choke on that. */
1964 verify_ssa (true, !from_ipa_pass);
1965 /* IPA passes leave basic-blocks unsplit, so make sure to
1966 not trip on that. */
1967 if ((cfun->curr_properties & PROP_cfg)
1968 && !from_ipa_pass)
1969 verify_flow_info ();
1970 if (current_loops
1971 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1972 verify_loop_closed_ssa (false);
1973 if (cfun->curr_properties & PROP_rtl)
1974 verify_rtl_sharing ();
1977 /* Make sure verifiers don't change dominator state. */
1978 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1979 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1980 #endif
1983 fn->last_verified = flags & TODO_verify_all;
1985 pop_cfun ();
1987 /* For IPA passes make sure to release dominator info, it can be
1988 computed by non-verifying TODOs. */
1989 if (from_ipa_pass)
1991 free_dominance_info (fn, CDI_DOMINATORS);
1992 free_dominance_info (fn, CDI_POST_DOMINATORS);
1996 /* Perform all TODO actions. */
1997 static void
1998 execute_todo (unsigned int flags)
2000 #if defined ENABLE_CHECKING
2001 if (cfun
2002 && need_ssa_update_p (cfun))
2003 gcc_assert (flags & TODO_update_ssa_any);
2004 #endif
2006 timevar_push (TV_TODO);
2008 /* Inform the pass whether it is the first time it is run. */
2009 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2011 statistics_fini_pass ();
2013 if (flags)
2014 do_per_function (execute_function_todo, (void *)(size_t) flags);
2016 /* Always remove functions just as before inlining: IPA passes might be
2017 interested to see bodies of extern inline functions that are not inlined
2018 to analyze side effects. The full removal is done just at the end
2019 of IPA pass queue. */
2020 if (flags & TODO_remove_functions)
2022 gcc_assert (!cfun);
2023 symtab->remove_unreachable_nodes (dump_file);
2026 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2028 gcc_assert (!cfun);
2029 symtab_node::dump_table (dump_file);
2030 /* Flush the file. If verification fails, we won't be able to
2031 close the file before aborting. */
2032 fflush (dump_file);
2035 /* Now that the dumping has been done, we can get rid of the optional
2036 df problems. */
2037 if (flags & TODO_df_finish)
2038 df_finish_pass ((flags & TODO_df_verify) != 0);
2040 timevar_pop (TV_TODO);
2043 /* Verify invariants that should hold between passes. This is a place
2044 to put simple sanity checks. */
2046 static void
2047 verify_interpass_invariants (void)
2049 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2052 /* Clear the last verified flag. */
2054 static void
2055 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2057 fn->last_verified = 0;
2060 /* Helper function. Verify that the properties has been turn into the
2061 properties expected by the pass. */
2063 #ifdef ENABLE_CHECKING
2064 static void
2065 verify_curr_properties (function *fn, void *data)
2067 unsigned int props = (size_t)data;
2068 gcc_assert ((fn->curr_properties & props) == props);
2070 #endif
2072 /* Initialize pass dump file. */
2073 /* This is non-static so that the plugins can use it. */
2075 bool
2076 pass_init_dump_file (opt_pass *pass)
2078 /* If a dump file name is present, open it if enabled. */
2079 if (pass->static_pass_number != -1)
2081 timevar_push (TV_DUMP);
2082 gcc::dump_manager *dumps = g->get_dumps ();
2083 bool initializing_dump =
2084 !dumps->dump_initialized_p (pass->static_pass_number);
2085 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2086 dumps->dump_start (pass->static_pass_number, &dump_flags);
2087 if (dump_file && current_function_decl)
2088 dump_function_header (dump_file, current_function_decl, dump_flags);
2089 if (initializing_dump
2090 && dump_file && (dump_flags & TDF_GRAPH)
2091 && cfun && (cfun->curr_properties & PROP_cfg))
2093 clean_graph_dump_file (dump_file_name);
2094 pass->graph_dump_initialized = true;
2096 timevar_pop (TV_DUMP);
2097 return initializing_dump;
2099 else
2100 return false;
2103 /* Flush PASS dump file. */
2104 /* This is non-static so that plugins can use it. */
2106 void
2107 pass_fini_dump_file (opt_pass *pass)
2109 timevar_push (TV_DUMP);
2111 /* Flush and close dump file. */
2112 if (dump_file_name)
2114 free (CONST_CAST (char *, dump_file_name));
2115 dump_file_name = NULL;
2118 g->get_dumps ()->dump_finish (pass->static_pass_number);
2119 timevar_pop (TV_DUMP);
2122 /* After executing the pass, apply expected changes to the function
2123 properties. */
2125 static void
2126 update_properties_after_pass (function *fn, void *data)
2128 opt_pass *pass = (opt_pass *) data;
2129 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2130 & ~pass->properties_destroyed;
2133 /* Execute summary generation for all of the passes in IPA_PASS. */
2135 void
2136 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2138 while (ipa_pass)
2140 opt_pass *pass = ipa_pass;
2142 /* Execute all of the IPA_PASSes in the list. */
2143 if (ipa_pass->type == IPA_PASS
2144 && pass->gate (cfun)
2145 && ipa_pass->generate_summary)
2147 pass_init_dump_file (pass);
2149 /* If a timevar is present, start it. */
2150 if (pass->tv_id)
2151 timevar_push (pass->tv_id);
2153 current_pass = pass;
2154 ipa_pass->generate_summary ();
2156 /* Stop timevar. */
2157 if (pass->tv_id)
2158 timevar_pop (pass->tv_id);
2160 pass_fini_dump_file (pass);
2162 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2166 /* Execute IPA_PASS function transform on NODE. */
2168 static void
2169 execute_one_ipa_transform_pass (struct cgraph_node *node,
2170 ipa_opt_pass_d *ipa_pass)
2172 opt_pass *pass = ipa_pass;
2173 unsigned int todo_after = 0;
2175 current_pass = pass;
2176 if (!ipa_pass->function_transform)
2177 return;
2179 /* Note that the folders should only create gimple expressions.
2180 This is a hack until the new folder is ready. */
2181 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2183 pass_init_dump_file (pass);
2185 /* Run pre-pass verification. */
2186 execute_todo (ipa_pass->function_transform_todo_flags_start);
2188 /* If a timevar is present, start it. */
2189 if (pass->tv_id != TV_NONE)
2190 timevar_push (pass->tv_id);
2192 /* Do it! */
2193 todo_after = ipa_pass->function_transform (node);
2195 /* Stop timevar. */
2196 if (pass->tv_id != TV_NONE)
2197 timevar_pop (pass->tv_id);
2199 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2200 check_profile_consistency (pass->static_pass_number, 0, true);
2202 /* Run post-pass cleanup and verification. */
2203 execute_todo (todo_after);
2204 verify_interpass_invariants ();
2205 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2206 check_profile_consistency (pass->static_pass_number, 1, true);
2208 if (dump_file)
2209 do_per_function (execute_function_dump, NULL);
2210 pass_fini_dump_file (pass);
2212 current_pass = NULL;
2214 /* Signal this is a suitable GC collection point. */
2215 if (!(todo_after & TODO_do_not_ggc_collect))
2216 ggc_collect ();
2219 /* For the current function, execute all ipa transforms. */
2221 void
2222 execute_all_ipa_transforms (void)
2224 struct cgraph_node *node;
2225 if (!cfun)
2226 return;
2227 node = cgraph_node::get (current_function_decl);
2229 if (node->ipa_transforms_to_apply.exists ())
2231 unsigned int i;
2233 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2234 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2235 node->ipa_transforms_to_apply.release ();
2239 /* Check if PASS is explicitly disabled or enabled and return
2240 the gate status. FUNC is the function to be processed, and
2241 GATE_STATUS is the gate status determined by pass manager by
2242 default. */
2244 static bool
2245 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2247 bool explicitly_enabled = false;
2248 bool explicitly_disabled = false;
2250 explicitly_enabled
2251 = is_pass_explicitly_enabled_or_disabled (pass, func,
2252 enabled_pass_uid_range_tab);
2253 explicitly_disabled
2254 = is_pass_explicitly_enabled_or_disabled (pass, func,
2255 disabled_pass_uid_range_tab);
2257 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2259 return gate_status;
2263 /* Execute PASS. */
2265 bool
2266 execute_one_pass (opt_pass *pass)
2268 unsigned int todo_after = 0;
2270 bool gate_status;
2272 /* IPA passes are executed on whole program, so cfun should be NULL.
2273 Other passes need function context set. */
2274 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2275 gcc_assert (!cfun && !current_function_decl);
2276 else
2277 gcc_assert (cfun && current_function_decl);
2279 current_pass = pass;
2281 /* Check whether gate check should be avoided.
2282 User controls the value of the gate through the parameter "gate_status". */
2283 gate_status = pass->gate (cfun);
2284 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2286 /* Override gate with plugin. */
2287 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2289 if (!gate_status)
2291 /* Run so passes selectively disabling themselves on a given function
2292 are not miscounted. */
2293 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2295 check_profile_consistency (pass->static_pass_number, 0, false);
2296 check_profile_consistency (pass->static_pass_number, 1, false);
2298 current_pass = NULL;
2299 return false;
2302 /* Pass execution event trigger: useful to identify passes being
2303 executed. */
2304 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2306 if (!quiet_flag && !cfun)
2307 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2309 /* Note that the folders should only create gimple expressions.
2310 This is a hack until the new folder is ready. */
2311 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2313 pass_init_dump_file (pass);
2315 /* Run pre-pass verification. */
2316 execute_todo (pass->todo_flags_start);
2318 #ifdef ENABLE_CHECKING
2319 do_per_function (verify_curr_properties,
2320 (void *)(size_t)pass->properties_required);
2321 #endif
2323 /* If a timevar is present, start it. */
2324 if (pass->tv_id != TV_NONE)
2325 timevar_push (pass->tv_id);
2327 /* Do it! */
2328 todo_after = pass->execute (cfun);
2329 do_per_function (clear_last_verified, NULL);
2331 /* Stop timevar. */
2332 if (pass->tv_id != TV_NONE)
2333 timevar_pop (pass->tv_id);
2335 do_per_function (update_properties_after_pass, pass);
2337 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2338 check_profile_consistency (pass->static_pass_number, 0, true);
2340 /* Run post-pass cleanup and verification. */
2341 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2342 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2343 check_profile_consistency (pass->static_pass_number, 1, true);
2345 verify_interpass_invariants ();
2346 if (dump_file)
2347 do_per_function (execute_function_dump, pass);
2348 if (pass->type == IPA_PASS)
2350 struct cgraph_node *node;
2351 if (((ipa_opt_pass_d *)pass)->function_transform)
2352 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2353 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2356 if (!current_function_decl)
2357 symtab->process_new_functions ();
2359 pass_fini_dump_file (pass);
2361 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2362 gcc_assert (!(cfun->curr_properties & PROP_trees)
2363 || pass->type != RTL_PASS);
2365 current_pass = NULL;
2367 /* Signal this is a suitable GC collection point. */
2368 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2369 ggc_collect ();
2371 return true;
2374 static void
2375 execute_pass_list_1 (opt_pass *pass)
2379 gcc_assert (pass->type == GIMPLE_PASS
2380 || pass->type == RTL_PASS);
2381 if (execute_one_pass (pass) && pass->sub)
2382 execute_pass_list_1 (pass->sub);
2383 pass = pass->next;
2385 while (pass);
2388 void
2389 execute_pass_list (function *fn, opt_pass *pass)
2391 push_cfun (fn);
2392 execute_pass_list_1 (pass);
2393 if (fn->cfg)
2395 free_dominance_info (CDI_DOMINATORS);
2396 free_dominance_info (CDI_POST_DOMINATORS);
2398 pop_cfun ();
2401 /* Write out all LTO data. */
2402 static void
2403 write_lto (void)
2405 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2406 lto_output ();
2407 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2408 timevar_push (TV_IPA_LTO_DECL_OUT);
2409 produce_asm_for_decls ();
2410 timevar_pop (TV_IPA_LTO_DECL_OUT);
2413 /* Same as execute_pass_list but assume that subpasses of IPA passes
2414 are local passes. If SET is not NULL, write out summaries of only
2415 those node in SET. */
2417 static void
2418 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2420 while (pass)
2422 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2423 gcc_assert (!current_function_decl);
2424 gcc_assert (!cfun);
2425 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2426 if (pass->type == IPA_PASS
2427 && ipa_pass->write_summary
2428 && pass->gate (cfun))
2430 /* If a timevar is present, start it. */
2431 if (pass->tv_id)
2432 timevar_push (pass->tv_id);
2434 pass_init_dump_file (pass);
2436 current_pass = pass;
2437 ipa_pass->write_summary ();
2439 pass_fini_dump_file (pass);
2441 /* If a timevar is present, start it. */
2442 if (pass->tv_id)
2443 timevar_pop (pass->tv_id);
2446 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2447 ipa_write_summaries_2 (pass->sub, state);
2449 pass = pass->next;
2453 /* Helper function of ipa_write_summaries. Creates and destroys the
2454 decl state and calls ipa_write_summaries_2 for all passes that have
2455 summaries. SET is the set of nodes to be written. */
2457 static void
2458 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2460 pass_manager *passes = g->get_passes ();
2461 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2462 state->symtab_node_encoder = encoder;
2464 lto_output_init_mode_table ();
2465 lto_push_out_decl_state (state);
2467 gcc_assert (!flag_wpa);
2468 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2470 write_lto ();
2472 gcc_assert (lto_get_out_decl_state () == state);
2473 lto_pop_out_decl_state ();
2474 lto_delete_out_decl_state (state);
2477 /* Write out summaries for all the nodes in the callgraph. */
2479 void
2480 ipa_write_summaries (void)
2482 lto_symtab_encoder_t encoder;
2483 int i, order_pos;
2484 varpool_node *vnode;
2485 struct cgraph_node *node;
2486 struct cgraph_node **order;
2488 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2489 return;
2491 select_what_to_stream ();
2493 encoder = lto_symtab_encoder_new (false);
2495 /* Create the callgraph set in the same order used in
2496 cgraph_expand_all_functions. This mostly facilitates debugging,
2497 since it causes the gimple file to be processed in the same order
2498 as the source code. */
2499 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2500 order_pos = ipa_reverse_postorder (order);
2501 gcc_assert (order_pos == symtab->cgraph_count);
2503 for (i = order_pos - 1; i >= 0; i--)
2505 struct cgraph_node *node = order[i];
2507 if (node->has_gimple_body_p ())
2509 /* When streaming out references to statements as part of some IPA
2510 pass summary, the statements need to have uids assigned and the
2511 following does that for all the IPA passes here. Naturally, this
2512 ordering then matches the one IPA-passes get in their stmt_fixup
2513 hooks. */
2515 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2516 renumber_gimple_stmt_uids ();
2517 pop_cfun ();
2519 if (node->definition && node->need_lto_streaming)
2520 lto_set_symtab_encoder_in_partition (encoder, node);
2523 FOR_EACH_DEFINED_FUNCTION (node)
2524 if (node->alias && node->need_lto_streaming)
2525 lto_set_symtab_encoder_in_partition (encoder, node);
2526 FOR_EACH_DEFINED_VARIABLE (vnode)
2527 if (vnode->need_lto_streaming)
2528 lto_set_symtab_encoder_in_partition (encoder, vnode);
2530 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2532 free (order);
2535 /* Same as execute_pass_list but assume that subpasses of IPA passes
2536 are local passes. If SET is not NULL, write out optimization summaries of
2537 only those node in SET. */
2539 static void
2540 ipa_write_optimization_summaries_1 (opt_pass *pass,
2541 struct lto_out_decl_state *state)
2543 while (pass)
2545 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2546 gcc_assert (!current_function_decl);
2547 gcc_assert (!cfun);
2548 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2549 if (pass->type == IPA_PASS
2550 && ipa_pass->write_optimization_summary
2551 && pass->gate (cfun))
2553 /* If a timevar is present, start it. */
2554 if (pass->tv_id)
2555 timevar_push (pass->tv_id);
2557 pass_init_dump_file (pass);
2559 current_pass = pass;
2560 ipa_pass->write_optimization_summary ();
2562 pass_fini_dump_file (pass);
2564 /* If a timevar is present, start it. */
2565 if (pass->tv_id)
2566 timevar_pop (pass->tv_id);
2569 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2570 ipa_write_optimization_summaries_1 (pass->sub, state);
2572 pass = pass->next;
2576 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2577 NULL, write out all summaries of all nodes. */
2579 void
2580 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2582 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2583 lto_symtab_encoder_iterator lsei;
2584 state->symtab_node_encoder = encoder;
2586 lto_output_init_mode_table ();
2587 lto_push_out_decl_state (state);
2588 for (lsei = lsei_start_function_in_partition (encoder);
2589 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2591 struct cgraph_node *node = lsei_cgraph_node (lsei);
2592 /* When streaming out references to statements as part of some IPA
2593 pass summary, the statements need to have uids assigned.
2595 For functions newly born at WPA stage we need to initialize
2596 the uids here. */
2597 if (node->definition
2598 && gimple_has_body_p (node->decl))
2600 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2601 renumber_gimple_stmt_uids ();
2602 pop_cfun ();
2606 gcc_assert (flag_wpa);
2607 pass_manager *passes = g->get_passes ();
2608 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2610 write_lto ();
2612 gcc_assert (lto_get_out_decl_state () == state);
2613 lto_pop_out_decl_state ();
2614 lto_delete_out_decl_state (state);
2617 /* Same as execute_pass_list but assume that subpasses of IPA passes
2618 are local passes. */
2620 static void
2621 ipa_read_summaries_1 (opt_pass *pass)
2623 while (pass)
2625 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2627 gcc_assert (!current_function_decl);
2628 gcc_assert (!cfun);
2629 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2631 if (pass->gate (cfun))
2633 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2635 /* If a timevar is present, start it. */
2636 if (pass->tv_id)
2637 timevar_push (pass->tv_id);
2639 pass_init_dump_file (pass);
2641 current_pass = pass;
2642 ipa_pass->read_summary ();
2644 pass_fini_dump_file (pass);
2646 /* Stop timevar. */
2647 if (pass->tv_id)
2648 timevar_pop (pass->tv_id);
2651 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2652 ipa_read_summaries_1 (pass->sub);
2654 pass = pass->next;
2659 /* Read all the summaries for all_regular_ipa_passes. */
2661 void
2662 ipa_read_summaries (void)
2664 pass_manager *passes = g->get_passes ();
2665 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2668 /* Same as execute_pass_list but assume that subpasses of IPA passes
2669 are local passes. */
2671 static void
2672 ipa_read_optimization_summaries_1 (opt_pass *pass)
2674 while (pass)
2676 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2678 gcc_assert (!current_function_decl);
2679 gcc_assert (!cfun);
2680 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2682 if (pass->gate (cfun))
2684 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2686 /* If a timevar is present, start it. */
2687 if (pass->tv_id)
2688 timevar_push (pass->tv_id);
2690 pass_init_dump_file (pass);
2692 current_pass = pass;
2693 ipa_pass->read_optimization_summary ();
2695 pass_fini_dump_file (pass);
2697 /* Stop timevar. */
2698 if (pass->tv_id)
2699 timevar_pop (pass->tv_id);
2702 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2703 ipa_read_optimization_summaries_1 (pass->sub);
2705 pass = pass->next;
2709 /* Read all the summaries for all_regular_ipa_passes. */
2711 void
2712 ipa_read_optimization_summaries (void)
2714 pass_manager *passes = g->get_passes ();
2715 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2718 /* Same as execute_pass_list but assume that subpasses of IPA passes
2719 are local passes. */
2720 void
2721 execute_ipa_pass_list (opt_pass *pass)
2725 gcc_assert (!current_function_decl);
2726 gcc_assert (!cfun);
2727 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2728 if (execute_one_pass (pass) && pass->sub)
2730 if (pass->sub->type == GIMPLE_PASS)
2732 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2733 do_per_function_toporder ((void (*)(function *, void *))
2734 execute_pass_list,
2735 pass->sub);
2736 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2738 else if (pass->sub->type == SIMPLE_IPA_PASS
2739 || pass->sub->type == IPA_PASS)
2740 execute_ipa_pass_list (pass->sub);
2741 else
2742 gcc_unreachable ();
2744 gcc_assert (!current_function_decl);
2745 symtab->process_new_functions ();
2746 pass = pass->next;
2748 while (pass);
2751 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2753 static void
2754 execute_ipa_stmt_fixups (opt_pass *pass,
2755 struct cgraph_node *node, gimple *stmts)
2757 while (pass)
2759 /* Execute all of the IPA_PASSes in the list. */
2760 if (pass->type == IPA_PASS
2761 && pass->gate (cfun))
2763 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2765 if (ipa_pass->stmt_fixup)
2767 pass_init_dump_file (pass);
2768 /* If a timevar is present, start it. */
2769 if (pass->tv_id)
2770 timevar_push (pass->tv_id);
2772 current_pass = pass;
2773 ipa_pass->stmt_fixup (node, stmts);
2775 /* Stop timevar. */
2776 if (pass->tv_id)
2777 timevar_pop (pass->tv_id);
2778 pass_fini_dump_file (pass);
2780 if (pass->sub)
2781 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2783 pass = pass->next;
2787 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2789 void
2790 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2792 pass_manager *passes = g->get_passes ();
2793 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2797 extern void debug_properties (unsigned int);
2798 extern void dump_properties (FILE *, unsigned int);
2800 DEBUG_FUNCTION void
2801 dump_properties (FILE *dump, unsigned int props)
2803 fprintf (dump, "Properties:\n");
2804 if (props & PROP_gimple_any)
2805 fprintf (dump, "PROP_gimple_any\n");
2806 if (props & PROP_gimple_lcf)
2807 fprintf (dump, "PROP_gimple_lcf\n");
2808 if (props & PROP_gimple_leh)
2809 fprintf (dump, "PROP_gimple_leh\n");
2810 if (props & PROP_cfg)
2811 fprintf (dump, "PROP_cfg\n");
2812 if (props & PROP_ssa)
2813 fprintf (dump, "PROP_ssa\n");
2814 if (props & PROP_no_crit_edges)
2815 fprintf (dump, "PROP_no_crit_edges\n");
2816 if (props & PROP_rtl)
2817 fprintf (dump, "PROP_rtl\n");
2818 if (props & PROP_gimple_lomp)
2819 fprintf (dump, "PROP_gimple_lomp\n");
2820 if (props & PROP_gimple_lcx)
2821 fprintf (dump, "PROP_gimple_lcx\n");
2822 if (props & PROP_gimple_lvec)
2823 fprintf (dump, "PROP_gimple_lvec\n");
2824 if (props & PROP_cfglayout)
2825 fprintf (dump, "PROP_cfglayout\n");
2828 DEBUG_FUNCTION void
2829 debug_properties (unsigned int props)
2831 dump_properties (stderr, props);
2834 /* Called by local passes to see if function is called by already processed nodes.
2835 Because we process nodes in topological order, this means that function is
2836 in recursive cycle or we introduced new direct calls. */
2837 bool
2838 function_called_by_processed_nodes_p (void)
2840 struct cgraph_edge *e;
2841 for (e = cgraph_node::get (current_function_decl)->callers;
2843 e = e->next_caller)
2845 if (e->caller->decl == current_function_decl)
2846 continue;
2847 if (!e->caller->has_gimple_body_p ())
2848 continue;
2849 if (TREE_ASM_WRITTEN (e->caller->decl))
2850 continue;
2851 if (!e->caller->process && !e->caller->global.inlined_to)
2852 break;
2854 if (dump_file && e)
2856 fprintf (dump_file, "Already processed call to:\n");
2857 e->caller->dump (dump_file);
2859 return e != NULL;
2862 #include "gt-passes.h"