[ARM] Fix typo in comment in arm_expand_prologue
[official-gcc.git] / gcc / passes.c
blob98e05e4ba7add538d9e2882c2bda5c3096f23a13
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2017 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 "backend.h"
29 #include "target.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "gimple.h"
33 #include "cfghooks.h"
34 #include "df.h"
35 #include "memmodel.h"
36 #include "tm_p.h"
37 #include "ssa.h"
38 #include "emit-rtl.h"
39 #include "cgraph.h"
40 #include "lto-streamer.h"
41 #include "fold-const.h"
42 #include "varasm.h"
43 #include "output.h"
44 #include "graph.h"
45 #include "debug.h"
46 #include "cfgloop.h"
47 #include "value-prof.h"
48 #include "tree-cfg.h"
49 #include "tree-ssa-loop-manip.h"
50 #include "tree-into-ssa.h"
51 #include "tree-dfa.h"
52 #include "tree-ssa.h"
53 #include "tree-pass.h"
54 #include "plugin.h"
55 #include "ipa-utils.h"
56 #include "tree-pretty-print.h" /* for dump_function_header */
57 #include "context.h"
58 #include "pass_manager.h"
59 #include "cfgrtl.h"
60 #include "tree-ssa-live.h" /* For remove_unused_locals. */
61 #include "tree-cfgcleanup.h"
62 #include "insn-addr.h" /* for INSN_ADDRESSES_ALLOC. */
64 using namespace gcc;
66 /* This is used for debugging. It allows the current pass to printed
67 from anywhere in compilation.
68 The variable current_pass is also used for statistics and plugins. */
69 opt_pass *current_pass;
71 /* Most passes are single-instance (within their context) and thus don't
72 need to implement cloning, but passes that support multiple instances
73 *must* provide their own implementation of the clone method.
75 Handle this by providing a default implemenation, but make it a fatal
76 error to call it. */
78 opt_pass *
79 opt_pass::clone ()
81 internal_error ("pass %s does not support cloning", name);
84 void
85 opt_pass::set_pass_param (unsigned int, bool)
87 internal_error ("pass %s needs a set_pass_param implementation to handle the"
88 " extra argument in NEXT_PASS", name);
91 bool
92 opt_pass::gate (function *)
94 return true;
97 unsigned int
98 opt_pass::execute (function *)
100 return 0;
103 opt_pass::opt_pass (const pass_data &data, context *ctxt)
104 : pass_data (data),
105 sub (NULL),
106 next (NULL),
107 static_pass_number (0),
108 m_ctxt (ctxt)
113 void
114 pass_manager::execute_early_local_passes ()
116 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
117 if (flag_check_pointer_bounds)
118 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
119 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
122 unsigned int
123 pass_manager::execute_pass_mode_switching ()
125 return pass_mode_switching_1->execute (cfun);
129 /* Call from anywhere to find out what pass this is. Useful for
130 printing out debugging information deep inside an service
131 routine. */
132 void
133 print_current_pass (FILE *file)
135 if (current_pass)
136 fprintf (file, "current pass = %s (%d)\n",
137 current_pass->name, current_pass->static_pass_number);
138 else
139 fprintf (file, "no current pass.\n");
143 /* Call from the debugger to get the current pass name. */
144 DEBUG_FUNCTION void
145 debug_pass (void)
147 print_current_pass (stderr);
152 /* Global variables used to communicate with passes. */
153 bool in_gimple_form;
156 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
157 and TYPE_DECL nodes.
159 This does nothing for local (non-static) variables, unless the
160 variable is a register variable with DECL_ASSEMBLER_NAME set. In
161 that case, or if the variable is not an automatic, it sets up the
162 RTL and outputs any assembler code (label definition, storage
163 allocation and initialization).
165 DECL is the declaration. TOP_LEVEL is nonzero
166 if this declaration is not within a function. */
168 void
169 rest_of_decl_compilation (tree decl,
170 int top_level,
171 int at_end)
173 bool finalize = true;
175 /* We deferred calling assemble_alias so that we could collect
176 other attributes such as visibility. Emit the alias now. */
177 if (!in_lto_p)
179 tree alias;
180 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
181 if (alias)
183 alias = TREE_VALUE (TREE_VALUE (alias));
184 alias = get_identifier (TREE_STRING_POINTER (alias));
185 /* A quirk of the initial implementation of aliases required that the
186 user add "extern" to all of them. Which is silly, but now
187 historical. Do note that the symbol is in fact locally defined. */
188 DECL_EXTERNAL (decl) = 0;
189 TREE_STATIC (decl) = 1;
190 assemble_alias (decl, alias);
191 finalize = false;
195 /* Can't defer this, because it needs to happen before any
196 later function definitions are processed. */
197 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
198 make_decl_rtl (decl);
200 /* Forward declarations for nested functions are not "external",
201 but we need to treat them as if they were. */
202 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
203 || TREE_CODE (decl) == FUNCTION_DECL)
205 timevar_push (TV_VARCONST);
207 /* Don't output anything when a tentative file-scope definition
208 is seen. But at end of compilation, do output code for them.
210 We do output all variables and rely on
211 callgraph code to defer them except for forward declarations
212 (see gcc.c-torture/compile/920624-1.c) */
213 if ((at_end
214 || !DECL_DEFER_OUTPUT (decl)
215 || DECL_INITIAL (decl))
216 && (!VAR_P (decl) || !DECL_HAS_VALUE_EXPR_P (decl))
217 && !DECL_EXTERNAL (decl))
219 /* When reading LTO unit, we also read varpool, so do not
220 rebuild it. */
221 if (in_lto_p && !at_end)
223 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
224 varpool_node::finalize_decl (decl);
227 #ifdef ASM_FINISH_DECLARE_OBJECT
228 if (decl == last_assemble_variable_decl)
230 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
231 top_level, at_end);
233 #endif
235 /* Now that we have activated any function-specific attributes
236 that might affect function decl, particularly align, relayout it. */
237 if (TREE_CODE (decl) == FUNCTION_DECL)
238 targetm.target_option.relayout_function (decl);
240 timevar_pop (TV_VARCONST);
242 else if (TREE_CODE (decl) == TYPE_DECL
243 /* Like in rest_of_type_compilation, avoid confusing the debug
244 information machinery when there are errors. */
245 && !seen_error ())
247 timevar_push (TV_SYMOUT);
248 debug_hooks->type_decl (decl, !top_level);
249 timevar_pop (TV_SYMOUT);
252 /* Let cgraph know about the existence of variables. */
253 if (in_lto_p && !at_end)
255 else if (VAR_P (decl) && !DECL_EXTERNAL (decl)
256 && TREE_STATIC (decl))
257 varpool_node::get_create (decl);
259 /* Generate early debug for global variables. Any local variables will
260 be handled by either handling reachable functions from
261 finalize_compilation_unit (and by consequence, locally scoped
262 symbols), or by rest_of_type_compilation below.
264 Also, pick up function prototypes, which will be mostly ignored
265 by the different early_global_decl() hooks, but will at least be
266 used by Go's hijack of the debug_hooks to implement
267 -fdump-go-spec. */
268 if (!in_lto_p
269 && (TREE_CODE (decl) != FUNCTION_DECL
270 /* This will pick up function prototypes with no bodies,
271 which are not visible in finalize_compilation_unit()
272 while iterating with FOR_EACH_*_FUNCTION through the
273 symbol table. */
274 || !DECL_SAVED_TREE (decl))
276 /* We need to check both decl_function_context and
277 current_function_decl here to make sure local extern
278 declarations end up with the correct context.
280 For local extern declarations, decl_function_context is
281 empty, but current_function_decl is set to the function where
282 the extern was declared . Without the check for
283 !current_function_decl below, the local extern ends up
284 incorrectly with a top-level context.
286 For example:
288 namespace S
294 int i = 42;
296 extern int i; // Local extern declaration.
297 return i;
303 && !decl_function_context (decl)
304 && !current_function_decl
305 && DECL_SOURCE_LOCATION (decl) != BUILTINS_LOCATION
306 && (!decl_type_context (decl)
307 /* If we created a varpool node for the decl make sure to
308 call early_global_decl. Otherwise we miss changes
309 introduced by member definitions like
310 struct A { static int staticdatamember; };
311 int A::staticdatamember;
312 and thus have incomplete early debug and late debug
313 called from varpool node removal fails to handle it
314 properly. */
315 || (finalize
316 && VAR_P (decl)
317 && TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
318 /* Avoid confusing the debug information machinery when there are
319 errors. */
320 && !seen_error ())
321 (*debug_hooks->early_global_decl) (decl);
324 /* Called after finishing a record, union or enumeral type. */
326 void
327 rest_of_type_compilation (tree type, int toplev)
329 /* Avoid confusing the debug information machinery when there are
330 errors. */
331 if (seen_error ())
332 return;
334 timevar_push (TV_SYMOUT);
335 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
336 timevar_pop (TV_SYMOUT);
341 void
342 pass_manager::
343 finish_optimization_passes (void)
345 int i;
346 struct dump_file_info *dfi;
347 char *name;
348 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
350 timevar_push (TV_DUMP);
351 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
353 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
354 end_branch_prob ();
355 dumps->dump_finish (pass_profile_1->static_pass_number);
358 if (optimize > 0)
360 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
361 print_combine_total_stats ();
362 dumps->dump_finish (pass_profile_1->static_pass_number);
365 /* Do whatever is necessary to finish printing the graphs. */
366 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
367 if (dfi->graph_dump_initialized)
369 name = dumps->get_dump_file_name (dfi);
370 finish_graph_dump_file (name);
371 free (name);
374 timevar_pop (TV_DUMP);
377 static unsigned int
378 execute_build_ssa_passes (void)
380 /* Once this pass (and its sub-passes) are complete, all functions
381 will be in SSA form. Technically this state change is happening
382 a tad early, since the sub-passes have not yet run, but since
383 none of the sub-passes are IPA passes and do not create new
384 functions, this is ok. We're setting this value for the benefit
385 of IPA passes that follow. */
386 if (symtab->state < IPA_SSA)
387 symtab->state = IPA_SSA;
388 return 0;
391 namespace {
393 const pass_data pass_data_build_ssa_passes =
395 SIMPLE_IPA_PASS, /* type */
396 "build_ssa_passes", /* name */
397 OPTGROUP_NONE, /* optinfo_flags */
398 TV_EARLY_LOCAL, /* tv_id */
399 0, /* properties_required */
400 0, /* properties_provided */
401 0, /* properties_destroyed */
402 0, /* todo_flags_start */
403 /* todo_flags_finish is executed before subpases. For this reason
404 it makes no sense to remove unreachable functions here. */
405 0, /* todo_flags_finish */
408 class pass_build_ssa_passes : public simple_ipa_opt_pass
410 public:
411 pass_build_ssa_passes (gcc::context *ctxt)
412 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
415 /* opt_pass methods: */
416 virtual bool gate (function *)
418 /* Don't bother doing anything if the program has errors. */
419 return (!seen_error () && !in_lto_p);
422 virtual unsigned int execute (function *)
424 return execute_build_ssa_passes ();
427 }; // class pass_build_ssa_passes
429 const pass_data pass_data_chkp_instrumentation_passes =
431 SIMPLE_IPA_PASS, /* type */
432 "chkp_passes", /* name */
433 OPTGROUP_NONE, /* optinfo_flags */
434 TV_NONE, /* tv_id */
435 0, /* properties_required */
436 0, /* properties_provided */
437 0, /* properties_destroyed */
438 0, /* todo_flags_start */
439 0, /* todo_flags_finish */
442 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
444 public:
445 pass_chkp_instrumentation_passes (gcc::context *ctxt)
446 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
449 /* opt_pass methods: */
450 virtual bool gate (function *)
452 /* Don't bother doing anything if the program has errors. */
453 return (flag_check_pointer_bounds
454 && !seen_error () && !in_lto_p);
457 }; // class pass_chkp_instrumentation_passes
459 const pass_data pass_data_local_optimization_passes =
461 SIMPLE_IPA_PASS, /* type */
462 "opt_local_passes", /* name */
463 OPTGROUP_NONE, /* optinfo_flags */
464 TV_NONE, /* tv_id */
465 0, /* properties_required */
466 0, /* properties_provided */
467 0, /* properties_destroyed */
468 0, /* todo_flags_start */
469 0, /* todo_flags_finish */
472 class pass_local_optimization_passes : public simple_ipa_opt_pass
474 public:
475 pass_local_optimization_passes (gcc::context *ctxt)
476 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
479 /* opt_pass methods: */
480 virtual bool gate (function *)
482 /* Don't bother doing anything if the program has errors. */
483 return (!seen_error () && !in_lto_p);
486 }; // class pass_local_optimization_passes
488 } // anon namespace
490 simple_ipa_opt_pass *
491 make_pass_build_ssa_passes (gcc::context *ctxt)
493 return new pass_build_ssa_passes (ctxt);
496 simple_ipa_opt_pass *
497 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
499 return new pass_chkp_instrumentation_passes (ctxt);
502 simple_ipa_opt_pass *
503 make_pass_local_optimization_passes (gcc::context *ctxt)
505 return new pass_local_optimization_passes (ctxt);
508 namespace {
510 const pass_data pass_data_all_early_optimizations =
512 GIMPLE_PASS, /* type */
513 "early_optimizations", /* name */
514 OPTGROUP_NONE, /* optinfo_flags */
515 TV_NONE, /* tv_id */
516 0, /* properties_required */
517 0, /* properties_provided */
518 0, /* properties_destroyed */
519 0, /* todo_flags_start */
520 0, /* todo_flags_finish */
523 class pass_all_early_optimizations : public gimple_opt_pass
525 public:
526 pass_all_early_optimizations (gcc::context *ctxt)
527 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
530 /* opt_pass methods: */
531 virtual bool gate (function *)
533 return (optimize >= 1
534 /* Don't bother doing anything if the program has errors. */
535 && !seen_error ());
538 }; // class pass_all_early_optimizations
540 } // anon namespace
542 static gimple_opt_pass *
543 make_pass_all_early_optimizations (gcc::context *ctxt)
545 return new pass_all_early_optimizations (ctxt);
548 namespace {
550 const pass_data pass_data_all_optimizations =
552 GIMPLE_PASS, /* type */
553 "*all_optimizations", /* name */
554 OPTGROUP_NONE, /* optinfo_flags */
555 TV_OPTIMIZE, /* tv_id */
556 0, /* properties_required */
557 0, /* properties_provided */
558 0, /* properties_destroyed */
559 0, /* todo_flags_start */
560 0, /* todo_flags_finish */
563 class pass_all_optimizations : public gimple_opt_pass
565 public:
566 pass_all_optimizations (gcc::context *ctxt)
567 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
570 /* opt_pass methods: */
571 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
573 }; // class pass_all_optimizations
575 } // anon namespace
577 static gimple_opt_pass *
578 make_pass_all_optimizations (gcc::context *ctxt)
580 return new pass_all_optimizations (ctxt);
583 namespace {
585 const pass_data pass_data_all_optimizations_g =
587 GIMPLE_PASS, /* type */
588 "*all_optimizations_g", /* name */
589 OPTGROUP_NONE, /* optinfo_flags */
590 TV_OPTIMIZE, /* tv_id */
591 0, /* properties_required */
592 0, /* properties_provided */
593 0, /* properties_destroyed */
594 0, /* todo_flags_start */
595 0, /* todo_flags_finish */
598 class pass_all_optimizations_g : public gimple_opt_pass
600 public:
601 pass_all_optimizations_g (gcc::context *ctxt)
602 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
605 /* opt_pass methods: */
606 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
608 }; // class pass_all_optimizations_g
610 } // anon namespace
612 static gimple_opt_pass *
613 make_pass_all_optimizations_g (gcc::context *ctxt)
615 return new pass_all_optimizations_g (ctxt);
618 namespace {
620 const pass_data pass_data_rest_of_compilation =
622 RTL_PASS, /* type */
623 "*rest_of_compilation", /* name */
624 OPTGROUP_NONE, /* optinfo_flags */
625 TV_REST_OF_COMPILATION, /* tv_id */
626 PROP_rtl, /* properties_required */
627 0, /* properties_provided */
628 0, /* properties_destroyed */
629 0, /* todo_flags_start */
630 0, /* todo_flags_finish */
633 class pass_rest_of_compilation : public rtl_opt_pass
635 public:
636 pass_rest_of_compilation (gcc::context *ctxt)
637 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
640 /* opt_pass methods: */
641 virtual bool gate (function *)
643 /* Early return if there were errors. We can run afoul of our
644 consistency checks, and there's not really much point in fixing them. */
645 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
648 }; // class pass_rest_of_compilation
650 } // anon namespace
652 static rtl_opt_pass *
653 make_pass_rest_of_compilation (gcc::context *ctxt)
655 return new pass_rest_of_compilation (ctxt);
658 namespace {
660 const pass_data pass_data_postreload =
662 RTL_PASS, /* type */
663 "*all-postreload", /* name */
664 OPTGROUP_NONE, /* optinfo_flags */
665 TV_POSTRELOAD, /* tv_id */
666 PROP_rtl, /* properties_required */
667 0, /* properties_provided */
668 0, /* properties_destroyed */
669 0, /* todo_flags_start */
670 0, /* todo_flags_finish */
673 class pass_postreload : public rtl_opt_pass
675 public:
676 pass_postreload (gcc::context *ctxt)
677 : rtl_opt_pass (pass_data_postreload, ctxt)
680 /* opt_pass methods: */
681 virtual bool gate (function *) { return reload_completed; }
683 }; // class pass_postreload
685 } // anon namespace
687 static rtl_opt_pass *
688 make_pass_postreload (gcc::context *ctxt)
690 return new pass_postreload (ctxt);
693 namespace {
695 const pass_data pass_data_late_compilation =
697 RTL_PASS, /* type */
698 "*all-late_compilation", /* name */
699 OPTGROUP_NONE, /* optinfo_flags */
700 TV_LATE_COMPILATION, /* tv_id */
701 PROP_rtl, /* properties_required */
702 0, /* properties_provided */
703 0, /* properties_destroyed */
704 0, /* todo_flags_start */
705 0, /* todo_flags_finish */
708 class pass_late_compilation : public rtl_opt_pass
710 public:
711 pass_late_compilation (gcc::context *ctxt)
712 : rtl_opt_pass (pass_data_late_compilation, ctxt)
715 /* opt_pass methods: */
716 virtual bool gate (function *)
718 return reload_completed || targetm.no_register_allocation;
721 }; // class pass_late_compilation
723 } // anon namespace
725 static rtl_opt_pass *
726 make_pass_late_compilation (gcc::context *ctxt)
728 return new pass_late_compilation (ctxt);
733 /* Set the static pass number of pass PASS to ID and record that
734 in the mapping from static pass number to pass. */
736 void
737 pass_manager::
738 set_pass_for_id (int id, opt_pass *pass)
740 pass->static_pass_number = id;
741 if (passes_by_id_size <= id)
743 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
744 memset (passes_by_id + passes_by_id_size, 0,
745 (id + 1 - passes_by_id_size) * sizeof (void *));
746 passes_by_id_size = id + 1;
748 passes_by_id[id] = pass;
751 /* Return the pass with the static pass number ID. */
753 opt_pass *
754 pass_manager::get_pass_for_id (int id) const
756 if (id >= passes_by_id_size)
757 return NULL;
758 return passes_by_id[id];
761 /* Iterate over the pass tree allocating dump file numbers. We want
762 to do this depth first, and independent of whether the pass is
763 enabled or not. */
765 void
766 register_one_dump_file (opt_pass *pass)
768 g->get_passes ()->register_one_dump_file (pass);
771 void
772 pass_manager::register_one_dump_file (opt_pass *pass)
774 char *dot_name, *flag_name, *glob_name;
775 const char *name, *full_name, *prefix;
777 /* Buffer big enough to format a 32-bit UINT_MAX into. */
778 char num[11];
779 dump_kind dkind;
780 int id;
781 int optgroup_flags = OPTGROUP_NONE;
782 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
784 /* See below in next_pass_1. */
785 num[0] = '\0';
786 if (pass->static_pass_number != -1)
787 sprintf (num, "%u", ((int) pass->static_pass_number < 0
788 ? 1 : pass->static_pass_number));
790 /* The name is both used to identify the pass for the purposes of plugins,
791 and to specify dump file name and option.
792 The latter two might want something short which is not quite unique; for
793 that reason, we may have a disambiguating prefix, followed by a space
794 to mark the start of the following dump file name / option string. */
795 name = strchr (pass->name, ' ');
796 name = name ? name + 1 : pass->name;
797 dot_name = concat (".", name, num, NULL);
798 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
800 prefix = "ipa-";
801 dkind = DK_ipa;
802 optgroup_flags |= OPTGROUP_IPA;
804 else if (pass->type == GIMPLE_PASS)
806 prefix = "tree-";
807 dkind = DK_tree;
809 else
811 prefix = "rtl-";
812 dkind = DK_rtl;
815 flag_name = concat (prefix, name, num, NULL);
816 glob_name = concat (prefix, name, NULL);
817 optgroup_flags |= pass->optinfo_flags;
818 /* For any passes that do not have an optgroup set, and which are not
819 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
820 any dump messages are emitted properly under -fopt-info(-optall). */
821 if (optgroup_flags == OPTGROUP_NONE)
822 optgroup_flags = OPTGROUP_OTHER;
823 id = dumps->dump_register (dot_name, flag_name, glob_name, dkind,
824 optgroup_flags,
825 true);
826 set_pass_for_id (id, pass);
827 full_name = concat (prefix, pass->name, num, NULL);
828 register_pass_name (pass, full_name);
829 free (CONST_CAST (char *, full_name));
832 /* Register the dump files for the pass_manager starting at PASS. */
834 void
835 pass_manager::register_dump_files (opt_pass *pass)
839 if (pass->name && pass->name[0] != '*')
840 register_one_dump_file (pass);
842 if (pass->sub)
843 register_dump_files (pass->sub);
845 pass = pass->next;
847 while (pass);
850 /* Register PASS with NAME. */
852 void
853 pass_manager::register_pass_name (opt_pass *pass, const char *name)
855 if (!m_name_to_pass_map)
856 m_name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
858 if (m_name_to_pass_map->get (name))
859 return; /* Ignore plugin passes. */
861 const char *unique_name = xstrdup (name);
862 m_name_to_pass_map->put (unique_name, pass);
865 /* Map from pass id to canonicalized pass name. */
867 typedef const char *char_ptr;
868 static vec<char_ptr> pass_tab;
870 /* Callback function for traversing NAME_TO_PASS_MAP. */
872 bool
873 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
875 gcc_assert (pass->static_pass_number > 0);
876 gcc_assert (pass_tab.exists ());
878 pass_tab[pass->static_pass_number] = name;
880 return 1;
883 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
884 table for dumping purpose. */
886 void
887 pass_manager::create_pass_tab (void) const
889 if (!flag_dump_passes)
890 return;
892 pass_tab.safe_grow_cleared (passes_by_id_size + 1);
893 m_name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
896 static bool override_gate_status (opt_pass *, tree, bool);
898 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
899 is turned on or not. */
901 static void
902 dump_one_pass (opt_pass *pass, int pass_indent)
904 int indent = 3 * pass_indent;
905 const char *pn;
906 bool is_on, is_really_on;
908 is_on = pass->gate (cfun);
909 is_really_on = override_gate_status (pass, current_function_decl, is_on);
911 if (pass->static_pass_number <= 0)
912 pn = pass->name;
913 else
914 pn = pass_tab[pass->static_pass_number];
916 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
917 (15 - indent < 0 ? 0 : 15 - indent), " ",
918 is_on ? " ON" : " OFF",
919 ((!is_on) == (!is_really_on) ? ""
920 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
923 /* Dump pass list PASS with indentation INDENT. */
925 static void
926 dump_pass_list (opt_pass *pass, int indent)
930 dump_one_pass (pass, indent);
931 if (pass->sub)
932 dump_pass_list (pass->sub, indent + 1);
933 pass = pass->next;
935 while (pass);
938 /* Dump all optimization passes. */
940 void
941 dump_passes (void)
943 g->get_passes ()->dump_passes ();
946 void
947 pass_manager::dump_passes () const
949 push_dummy_function (true);
951 create_pass_tab ();
953 dump_pass_list (all_lowering_passes, 1);
954 dump_pass_list (all_small_ipa_passes, 1);
955 dump_pass_list (all_regular_ipa_passes, 1);
956 dump_pass_list (all_late_ipa_passes, 1);
957 dump_pass_list (all_passes, 1);
959 pop_dummy_function ();
962 /* Returns the pass with NAME. */
964 opt_pass *
965 pass_manager::get_pass_by_name (const char *name)
967 opt_pass **p = m_name_to_pass_map->get (name);
968 if (p)
969 return *p;
971 return NULL;
975 /* Range [start, last]. */
977 struct uid_range
979 unsigned int start;
980 unsigned int last;
981 const char *assem_name;
982 struct uid_range *next;
985 typedef struct uid_range *uid_range_p;
988 static vec<uid_range_p> enabled_pass_uid_range_tab;
989 static vec<uid_range_p> disabled_pass_uid_range_tab;
992 /* Parse option string for -fdisable- and -fenable-
993 The syntax of the options:
995 -fenable-<pass_name>
996 -fdisable-<pass_name>
998 -fenable-<pass_name>=s1:e1,s2:e2,...
999 -fdisable-<pass_name>=s1:e1,s2:e2,...
1002 static void
1003 enable_disable_pass (const char *arg, bool is_enable)
1005 opt_pass *pass;
1006 char *range_str, *phase_name;
1007 char *argstr = xstrdup (arg);
1008 vec<uid_range_p> *tab = 0;
1010 range_str = strchr (argstr,'=');
1011 if (range_str)
1013 *range_str = '\0';
1014 range_str++;
1017 phase_name = argstr;
1018 if (!*phase_name)
1020 if (is_enable)
1021 error ("unrecognized option -fenable");
1022 else
1023 error ("unrecognized option -fdisable");
1024 free (argstr);
1025 return;
1027 pass = g->get_passes ()->get_pass_by_name (phase_name);
1028 if (!pass || pass->static_pass_number == -1)
1030 if (is_enable)
1031 error ("unknown pass %s specified in -fenable", phase_name);
1032 else
1033 error ("unknown pass %s specified in -fdisable", phase_name);
1034 free (argstr);
1035 return;
1038 if (is_enable)
1039 tab = &enabled_pass_uid_range_tab;
1040 else
1041 tab = &disabled_pass_uid_range_tab;
1043 if ((unsigned) pass->static_pass_number >= tab->length ())
1044 tab->safe_grow_cleared (pass->static_pass_number + 1);
1046 if (!range_str)
1048 uid_range_p slot;
1049 uid_range_p new_range = XCNEW (struct uid_range);
1051 new_range->start = 0;
1052 new_range->last = (unsigned)-1;
1054 slot = (*tab)[pass->static_pass_number];
1055 new_range->next = slot;
1056 (*tab)[pass->static_pass_number] = new_range;
1057 if (is_enable)
1058 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1059 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1060 else
1061 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1062 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1064 else
1066 char *next_range = NULL;
1067 char *one_range = range_str;
1068 char *end_val = NULL;
1072 uid_range_p slot;
1073 uid_range_p new_range;
1074 char *invalid = NULL;
1075 long start;
1076 char *func_name = NULL;
1078 next_range = strchr (one_range, ',');
1079 if (next_range)
1081 *next_range = '\0';
1082 next_range++;
1085 end_val = strchr (one_range, ':');
1086 if (end_val)
1088 *end_val = '\0';
1089 end_val++;
1091 start = strtol (one_range, &invalid, 10);
1092 if (*invalid || start < 0)
1094 if (end_val || (one_range[0] >= '0'
1095 && one_range[0] <= '9'))
1097 error ("Invalid range %s in option %s",
1098 one_range,
1099 is_enable ? "-fenable" : "-fdisable");
1100 free (argstr);
1101 return;
1103 func_name = one_range;
1105 if (!end_val)
1107 new_range = XCNEW (struct uid_range);
1108 if (!func_name)
1110 new_range->start = (unsigned) start;
1111 new_range->last = (unsigned) start;
1113 else
1115 new_range->start = (unsigned) -1;
1116 new_range->last = (unsigned) -1;
1117 new_range->assem_name = xstrdup (func_name);
1120 else
1122 long last = strtol (end_val, &invalid, 10);
1123 if (*invalid || last < start)
1125 error ("Invalid range %s in option %s",
1126 end_val,
1127 is_enable ? "-fenable" : "-fdisable");
1128 free (argstr);
1129 return;
1131 new_range = XCNEW (struct uid_range);
1132 new_range->start = (unsigned) start;
1133 new_range->last = (unsigned) last;
1136 slot = (*tab)[pass->static_pass_number];
1137 new_range->next = slot;
1138 (*tab)[pass->static_pass_number] = new_range;
1139 if (is_enable)
1141 if (new_range->assem_name)
1142 inform (UNKNOWN_LOCATION,
1143 "enable pass %s for function %s",
1144 phase_name, new_range->assem_name);
1145 else
1146 inform (UNKNOWN_LOCATION,
1147 "enable pass %s for functions in the range of [%u, %u]",
1148 phase_name, new_range->start, new_range->last);
1150 else
1152 if (new_range->assem_name)
1153 inform (UNKNOWN_LOCATION,
1154 "disable pass %s for function %s",
1155 phase_name, new_range->assem_name);
1156 else
1157 inform (UNKNOWN_LOCATION,
1158 "disable pass %s for functions in the range of [%u, %u]",
1159 phase_name, new_range->start, new_range->last);
1162 one_range = next_range;
1163 } while (next_range);
1166 free (argstr);
1169 /* Enable pass specified by ARG. */
1171 void
1172 enable_pass (const char *arg)
1174 enable_disable_pass (arg, true);
1177 /* Disable pass specified by ARG. */
1179 void
1180 disable_pass (const char *arg)
1182 enable_disable_pass (arg, false);
1185 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1187 static bool
1188 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1189 tree func,
1190 vec<uid_range_p> tab)
1192 uid_range_p slot, range;
1193 int cgraph_uid;
1194 const char *aname = NULL;
1196 if (!tab.exists ()
1197 || (unsigned) pass->static_pass_number >= tab.length ()
1198 || pass->static_pass_number == -1)
1199 return false;
1201 slot = tab[pass->static_pass_number];
1202 if (!slot)
1203 return false;
1205 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1206 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1207 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1209 range = slot;
1210 while (range)
1212 if ((unsigned) cgraph_uid >= range->start
1213 && (unsigned) cgraph_uid <= range->last)
1214 return true;
1215 if (range->assem_name && aname
1216 && !strcmp (range->assem_name, aname))
1217 return true;
1218 range = range->next;
1221 return false;
1225 /* Update static_pass_number for passes (and the flag
1226 TODO_mark_first_instance).
1228 Passes are constructed with static_pass_number preinitialized to 0
1230 This field is used in two different ways: initially as instance numbers
1231 of their kind, and then as ids within the entire pass manager.
1233 Within pass_manager::pass_manager:
1235 * In add_pass_instance(), as called by next_pass_1 in
1236 NEXT_PASS in init_optimization_passes
1238 * When the initial instance of a pass within a pass manager is seen,
1239 it is flagged, and its static_pass_number is set to -1
1241 * On subsequent times that it is seen, the static pass number
1242 is decremented each time, so that if there are e.g. 4 dups,
1243 they have static_pass_number -4, 2, 3, 4 respectively (note
1244 how the initial one is negative and gives the count); these
1245 can be thought of as instance numbers of the specific pass
1247 * Within the register_dump_files () traversal, set_pass_for_id()
1248 is called on each pass, using these instance numbers to create
1249 dumpfile switches, and then overwriting them with a pass id,
1250 which are global to the whole pass manager (based on
1251 (TDI_end + current value of extra_dump_files_in_use) ) */
1253 static void
1254 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1255 opt_pass *initial_pass)
1257 /* Are we dealing with the first pass of its kind, or a clone? */
1258 if (new_pass != initial_pass)
1260 /* We're dealing with a clone. */
1261 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1263 /* Indicate to register_dump_files that this pass has duplicates,
1264 and so it should rename the dump file. The first instance will
1265 be -1, and be number of duplicates = -static_pass_number - 1.
1266 Subsequent instances will be > 0 and just the duplicate number. */
1267 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1269 initial_pass->static_pass_number -= 1;
1270 new_pass->static_pass_number = -initial_pass->static_pass_number;
1273 else
1275 /* We're dealing with the first pass of its kind. */
1276 new_pass->todo_flags_start |= TODO_mark_first_instance;
1277 new_pass->static_pass_number = -1;
1279 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1283 /* Add a pass to the pass list. Duplicate the pass if it's already
1284 in the list. */
1286 static opt_pass **
1287 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1289 /* Every pass should have a name so that plugins can refer to them. */
1290 gcc_assert (pass->name != NULL);
1292 add_pass_instance (pass, false, initial_pass);
1293 *list = pass;
1295 return &(*list)->next;
1298 /* List node for an inserted pass instance. We need to keep track of all
1299 the newly-added pass instances (with 'added_pass_nodes' defined below)
1300 so that we can register their dump files after pass-positioning is finished.
1301 Registering dumping files needs to be post-processed or the
1302 static_pass_number of the opt_pass object would be modified and mess up
1303 the dump file names of future pass instances to be added. */
1305 struct pass_list_node
1307 opt_pass *pass;
1308 struct pass_list_node *next;
1311 static struct pass_list_node *added_pass_nodes = NULL;
1312 static struct pass_list_node *prev_added_pass_node;
1314 /* Insert the pass at the proper position. Return true if the pass
1315 is successfully added.
1317 NEW_PASS_INFO - new pass to be inserted
1318 PASS_LIST - root of the pass list to insert the new pass to */
1320 static bool
1321 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1323 opt_pass *pass = *pass_list, *prev_pass = NULL;
1324 bool success = false;
1326 for ( ; pass; prev_pass = pass, pass = pass->next)
1328 /* Check if the current pass is of the same type as the new pass and
1329 matches the name and the instance number of the reference pass. */
1330 if (pass->type == new_pass_info->pass->type
1331 && pass->name
1332 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1333 && ((new_pass_info->ref_pass_instance_number == 0)
1334 || (new_pass_info->ref_pass_instance_number ==
1335 pass->static_pass_number)
1336 || (new_pass_info->ref_pass_instance_number == 1
1337 && pass->todo_flags_start & TODO_mark_first_instance)))
1339 opt_pass *new_pass;
1340 struct pass_list_node *new_pass_node;
1342 if (new_pass_info->ref_pass_instance_number == 0)
1344 new_pass = new_pass_info->pass->clone ();
1345 add_pass_instance (new_pass, true, new_pass_info->pass);
1347 else
1349 new_pass = new_pass_info->pass;
1350 add_pass_instance (new_pass, true, new_pass);
1353 /* Insert the new pass instance based on the positioning op. */
1354 switch (new_pass_info->pos_op)
1356 case PASS_POS_INSERT_AFTER:
1357 new_pass->next = pass->next;
1358 pass->next = new_pass;
1360 /* Skip newly inserted pass to avoid repeated
1361 insertions in the case where the new pass and the
1362 existing one have the same name. */
1363 pass = new_pass;
1364 break;
1365 case PASS_POS_INSERT_BEFORE:
1366 new_pass->next = pass;
1367 if (prev_pass)
1368 prev_pass->next = new_pass;
1369 else
1370 *pass_list = new_pass;
1371 break;
1372 case PASS_POS_REPLACE:
1373 new_pass->next = pass->next;
1374 if (prev_pass)
1375 prev_pass->next = new_pass;
1376 else
1377 *pass_list = new_pass;
1378 new_pass->sub = pass->sub;
1379 new_pass->tv_id = pass->tv_id;
1380 pass = new_pass;
1381 break;
1382 default:
1383 error ("invalid pass positioning operation");
1384 return false;
1387 /* Save the newly added pass (instance) in the added_pass_nodes
1388 list so that we can register its dump file later. Note that
1389 we cannot register the dump file now because doing so will modify
1390 the static_pass_number of the opt_pass object and therefore
1391 mess up the dump file name of future instances. */
1392 new_pass_node = XCNEW (struct pass_list_node);
1393 new_pass_node->pass = new_pass;
1394 if (!added_pass_nodes)
1395 added_pass_nodes = new_pass_node;
1396 else
1397 prev_added_pass_node->next = new_pass_node;
1398 prev_added_pass_node = new_pass_node;
1400 success = true;
1403 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1404 success = true;
1407 return success;
1410 /* Hooks a new pass into the pass lists.
1412 PASS_INFO - pass information that specifies the opt_pass object,
1413 reference pass, instance number, and how to position
1414 the pass */
1416 void
1417 register_pass (struct register_pass_info *pass_info)
1419 g->get_passes ()->register_pass (pass_info);
1422 void
1423 register_pass (opt_pass* pass, pass_positioning_ops pos,
1424 const char* ref_pass_name, int ref_pass_inst_number)
1426 register_pass_info i;
1427 i.pass = pass;
1428 i.reference_pass_name = ref_pass_name;
1429 i.ref_pass_instance_number = ref_pass_inst_number;
1430 i.pos_op = pos;
1432 g->get_passes ()->register_pass (&i);
1435 void
1436 pass_manager::register_pass (struct register_pass_info *pass_info)
1438 bool all_instances, success;
1439 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1441 /* The checks below could fail in buggy plugins. Existing GCC
1442 passes should never fail these checks, so we mention plugin in
1443 the messages. */
1444 if (!pass_info->pass)
1445 fatal_error (input_location, "plugin cannot register a missing pass");
1447 if (!pass_info->pass->name)
1448 fatal_error (input_location, "plugin cannot register an unnamed pass");
1450 if (!pass_info->reference_pass_name)
1451 fatal_error
1452 (input_location,
1453 "plugin cannot register pass %qs without reference pass name",
1454 pass_info->pass->name);
1456 /* Try to insert the new pass to the pass lists. We need to check
1457 all five lists as the reference pass could be in one (or all) of
1458 them. */
1459 all_instances = pass_info->ref_pass_instance_number == 0;
1460 success = position_pass (pass_info, &all_lowering_passes);
1461 if (!success || all_instances)
1462 success |= position_pass (pass_info, &all_small_ipa_passes);
1463 if (!success || all_instances)
1464 success |= position_pass (pass_info, &all_regular_ipa_passes);
1465 if (!success || all_instances)
1466 success |= position_pass (pass_info, &all_late_ipa_passes);
1467 if (!success || all_instances)
1468 success |= position_pass (pass_info, &all_passes);
1469 if (!success)
1470 fatal_error
1471 (input_location,
1472 "pass %qs not found but is referenced by new pass %qs",
1473 pass_info->reference_pass_name, pass_info->pass->name);
1475 /* OK, we have successfully inserted the new pass. We need to register
1476 the dump files for the newly added pass and its duplicates (if any).
1477 Because the registration of plugin/backend passes happens after the
1478 command-line options are parsed, the options that specify single
1479 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1480 passes. Therefore we currently can only enable dumping of
1481 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1482 are specified. While doing so, we also delete the pass_list_node
1483 objects created during pass positioning. */
1484 while (added_pass_nodes)
1486 struct pass_list_node *next_node = added_pass_nodes->next;
1487 enum tree_dump_index tdi;
1488 register_one_dump_file (added_pass_nodes->pass);
1489 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1490 || added_pass_nodes->pass->type == IPA_PASS)
1491 tdi = TDI_ipa_all;
1492 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1493 tdi = TDI_tree_all;
1494 else
1495 tdi = TDI_rtl_all;
1496 /* Check if dump-all flag is specified. */
1497 if (dumps->get_dump_file_info (tdi)->pstate)
1499 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1500 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1501 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1502 ->pflags = dumps->get_dump_file_info (tdi)->pflags;
1504 XDELETE (added_pass_nodes);
1505 added_pass_nodes = next_node;
1509 /* Construct the pass tree. The sequencing of passes is driven by
1510 the cgraph routines:
1512 finalize_compilation_unit ()
1513 for each node N in the cgraph
1514 cgraph_analyze_function (N)
1515 cgraph_lower_function (N) -> all_lowering_passes
1517 If we are optimizing, compile is then invoked:
1519 compile ()
1520 ipa_passes () -> all_small_ipa_passes
1521 -> Analysis of all_regular_ipa_passes
1522 * possible LTO streaming at copmilation time *
1523 -> Execution of all_regular_ipa_passes
1524 * possible LTO streaming at link time *
1525 -> all_late_ipa_passes
1526 expand_all_functions ()
1527 for each node N in the cgraph
1528 expand_function (N) -> Transformation of all_regular_ipa_passes
1529 -> all_passes
1532 pass_manager::pass_manager (context *ctxt)
1533 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1534 all_regular_ipa_passes (NULL),
1535 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1536 m_ctxt (ctxt), m_name_to_pass_map (NULL)
1538 opt_pass **p;
1540 /* Zero-initialize pass members. */
1541 #define INSERT_PASSES_AFTER(PASS)
1542 #define PUSH_INSERT_PASSES_WITHIN(PASS)
1543 #define POP_INSERT_PASSES()
1544 #define NEXT_PASS(PASS, NUM) PASS ## _ ## NUM = NULL
1545 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) NEXT_PASS (PASS, NUM)
1546 #define TERMINATE_PASS_LIST(PASS)
1547 #include "pass-instances.def"
1548 #undef INSERT_PASSES_AFTER
1549 #undef PUSH_INSERT_PASSES_WITHIN
1550 #undef POP_INSERT_PASSES
1551 #undef NEXT_PASS
1552 #undef NEXT_PASS_WITH_ARG
1553 #undef TERMINATE_PASS_LIST
1555 /* Initialize the pass_lists array. */
1556 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1557 GCC_PASS_LISTS
1558 #undef DEF_PASS_LIST
1560 /* Build the tree of passes. */
1562 #define INSERT_PASSES_AFTER(PASS) \
1564 opt_pass **p_start; \
1565 p_start = p = &(PASS);
1567 #define TERMINATE_PASS_LIST(PASS) \
1568 gcc_assert (p_start == &PASS); \
1569 *p = NULL; \
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 NEXT_PASS_WITH_ARG(PASS, NUM, ARG) \
1593 do { \
1594 NEXT_PASS (PASS, NUM); \
1595 PASS ## _ ## NUM->set_pass_param (0, ARG); \
1596 } while (0)
1598 #include "pass-instances.def"
1600 #undef INSERT_PASSES_AFTER
1601 #undef PUSH_INSERT_PASSES_WITHIN
1602 #undef POP_INSERT_PASSES
1603 #undef NEXT_PASS
1604 #undef NEXT_PASS_WITH_ARG
1605 #undef TERMINATE_PASS_LIST
1607 /* Register the passes with the tree dump code. */
1608 register_dump_files (all_lowering_passes);
1609 register_dump_files (all_small_ipa_passes);
1610 register_dump_files (all_regular_ipa_passes);
1611 register_dump_files (all_late_ipa_passes);
1612 register_dump_files (all_passes);
1615 static void
1616 delete_pass_tree (opt_pass *pass)
1618 while (pass)
1620 /* Recurse into child passes. */
1621 delete_pass_tree (pass->sub);
1623 opt_pass *next = pass->next;
1625 /* Delete this pass. */
1626 delete pass;
1628 /* Iterate onto sibling passes. */
1629 pass = next;
1633 pass_manager::~pass_manager ()
1635 XDELETEVEC (passes_by_id);
1637 /* Call delete_pass_tree on each of the pass_lists. */
1638 #define DEF_PASS_LIST(LIST) \
1639 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1640 GCC_PASS_LISTS
1641 #undef DEF_PASS_LIST
1645 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1646 function CALLBACK for every function in the call graph. Otherwise,
1647 call CALLBACK on the current function. */
1649 static void
1650 do_per_function (void (*callback) (function *, void *data), void *data)
1652 if (current_function_decl)
1653 callback (cfun, data);
1654 else
1656 struct cgraph_node *node;
1657 FOR_EACH_DEFINED_FUNCTION (node)
1658 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1659 && (!node->clone_of || node->decl != node->clone_of->decl))
1660 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1664 /* Because inlining might remove no-longer reachable nodes, we need to
1665 keep the array visible to garbage collector to avoid reading collected
1666 out nodes. */
1667 static int nnodes;
1668 static GTY ((length ("nnodes"))) cgraph_node **order;
1670 /* Hook called when NODE is removed and therefore should be
1671 excluded from order vector. DATA is an array of integers.
1672 DATA[0] holds max index it may be accessed by. For cgraph
1673 node DATA[node->uid + 1] holds index of this node in order
1674 vector. */
1675 static void
1676 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1678 int *order_idx = (int *)data;
1680 if (node->uid >= order_idx[0])
1681 return;
1683 int idx = order_idx[node->uid + 1];
1684 if (idx >= 0 && idx < nnodes && order[idx] == node)
1685 order[idx] = NULL;
1688 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1689 function CALLBACK for every function in the call graph. Otherwise,
1690 call CALLBACK on the current function.
1691 This function is global so that plugins can use it. */
1692 void
1693 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1695 int i;
1697 if (current_function_decl)
1698 callback (cfun, data);
1699 else
1701 cgraph_node_hook_list *hook;
1702 int *order_idx;
1703 gcc_assert (!order);
1704 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1706 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1707 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1708 order_idx[0] = symtab->cgraph_max_uid;
1710 nnodes = ipa_reverse_postorder (order);
1711 for (i = nnodes - 1; i >= 0; i--)
1713 order[i]->process = 1;
1714 order_idx[order[i]->uid + 1] = i;
1716 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1717 order_idx);
1718 for (i = nnodes - 1; i >= 0; i--)
1720 /* Function could be inlined and removed as unreachable. */
1721 if (!order[i])
1722 continue;
1724 struct cgraph_node *node = order[i];
1726 /* Allow possibly removed nodes to be garbage collected. */
1727 order[i] = NULL;
1728 node->process = 0;
1729 if (node->has_gimple_body_p ())
1731 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1732 push_cfun (fn);
1733 callback (fn, data);
1734 pop_cfun ();
1737 symtab->remove_cgraph_removal_hook (hook);
1739 ggc_free (order);
1740 order = NULL;
1741 nnodes = 0;
1744 /* Helper function to perform function body dump. */
1746 static void
1747 execute_function_dump (function *fn, void *data)
1749 opt_pass *pass = (opt_pass *)data;
1751 if (dump_file)
1753 push_cfun (fn);
1755 if (fn->curr_properties & PROP_trees)
1756 dump_function_to_file (fn->decl, dump_file, dump_flags);
1757 else
1758 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1760 /* Flush the file. If verification fails, we won't be able to
1761 close the file before aborting. */
1762 fflush (dump_file);
1764 if ((fn->curr_properties & PROP_cfg)
1765 && (dump_flags & TDF_GRAPH))
1767 gcc::dump_manager *dumps = g->get_dumps ();
1768 struct dump_file_info *dfi
1769 = dumps->get_dump_file_info (pass->static_pass_number);
1770 if (!dfi->graph_dump_initialized)
1772 clean_graph_dump_file (dump_file_name);
1773 dfi->graph_dump_initialized = true;
1775 print_graph_cfg (dump_file_name, fn);
1778 pop_cfun ();
1782 static struct profile_record *profile_record;
1784 /* Do profile consistency book-keeping for the pass with static number INDEX.
1785 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1786 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1787 if we are only book-keeping on passes that may have selectively disabled
1788 themselves on a given function. */
1789 static void
1790 check_profile_consistency (int index, int subpass, bool run)
1792 pass_manager *passes = g->get_passes ();
1793 if (index == -1)
1794 return;
1795 if (!profile_record)
1796 profile_record = XCNEWVEC (struct profile_record,
1797 passes->passes_by_id_size);
1798 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1799 gcc_assert (subpass < 2);
1800 profile_record[index].run |= run;
1801 account_profile_record (&profile_record[index], subpass);
1804 /* Output profile consistency. */
1806 void
1807 dump_profile_report (void)
1809 g->get_passes ()->dump_profile_report ();
1812 void
1813 pass_manager::dump_profile_report () const
1815 int i, j;
1816 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1817 gcov_type last_time = 0, last_size = 0;
1818 double rel_time_change, rel_size_change;
1819 int last_reported = 0;
1821 if (!profile_record)
1822 return;
1823 fprintf (stderr, "\nProfile consistency report:\n\n");
1824 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1825 fprintf (stderr, " |freq count |freq count |size time\n");
1827 for (i = 0; i < passes_by_id_size; i++)
1828 for (j = 0 ; j < 2; j++)
1829 if (profile_record[i].run)
1831 if (last_time)
1832 rel_time_change = (profile_record[i].time[j]
1833 - (double)last_time) * 100 / (double)last_time;
1834 else
1835 rel_time_change = 0;
1836 if (last_size)
1837 rel_size_change = (profile_record[i].size[j]
1838 - (double)last_size) * 100 / (double)last_size;
1839 else
1840 rel_size_change = 0;
1842 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1843 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1844 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1845 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1846 || rel_time_change || rel_size_change)
1848 last_reported = i;
1849 fprintf (stderr, "%-20s %s",
1850 passes_by_id [i]->name,
1851 j ? "(after TODO)" : " ");
1852 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1853 fprintf (stderr, "| %+5i",
1854 profile_record[i].num_mismatched_freq_in[j]
1855 - last_freq_in);
1856 else
1857 fprintf (stderr, "| ");
1858 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1859 fprintf (stderr, " %+5i",
1860 profile_record[i].num_mismatched_count_in[j]
1861 - last_count_in);
1862 else
1863 fprintf (stderr, " ");
1864 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1865 fprintf (stderr, "| %+5i",
1866 profile_record[i].num_mismatched_freq_out[j]
1867 - last_freq_out);
1868 else
1869 fprintf (stderr, "| ");
1870 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1871 fprintf (stderr, " %+5i",
1872 profile_record[i].num_mismatched_count_out[j]
1873 - last_count_out);
1874 else
1875 fprintf (stderr, " ");
1877 /* Size/time units change across gimple and RTL. */
1878 if (i == pass_expand_1->static_pass_number)
1879 fprintf (stderr, "|----------");
1880 else
1882 if (rel_size_change)
1883 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1884 else
1885 fprintf (stderr, "| ");
1886 if (rel_time_change)
1887 fprintf (stderr, " %+8.4f%%", rel_time_change);
1889 fprintf (stderr, "\n");
1890 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1891 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1892 last_count_in = profile_record[i].num_mismatched_count_in[j];
1893 last_count_out = profile_record[i].num_mismatched_count_out[j];
1895 else if (j && last_reported != i)
1897 last_reported = i;
1898 fprintf (stderr, "%-20s ------------| | |\n",
1899 passes_by_id [i]->name);
1901 last_time = profile_record[i].time[j];
1902 last_size = profile_record[i].size[j];
1906 /* Perform all TODO actions that ought to be done on each function. */
1908 static void
1909 execute_function_todo (function *fn, void *data)
1911 bool from_ipa_pass = (cfun == NULL);
1912 unsigned int flags = (size_t)data;
1913 flags &= ~fn->last_verified;
1914 if (!flags)
1915 return;
1917 push_cfun (fn);
1919 /* Always cleanup the CFG before trying to update SSA. */
1920 if (flags & TODO_cleanup_cfg)
1922 cleanup_tree_cfg ();
1924 /* When cleanup_tree_cfg merges consecutive blocks, it may
1925 perform some simplistic propagation when removing single
1926 valued PHI nodes. This propagation may, in turn, cause the
1927 SSA form to become out-of-date (see PR 22037). So, even
1928 if the parent pass had not scheduled an SSA update, we may
1929 still need to do one. */
1930 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1931 flags |= TODO_update_ssa;
1934 if (flags & TODO_update_ssa_any)
1936 unsigned update_flags = flags & TODO_update_ssa_any;
1937 update_ssa (update_flags);
1940 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1941 compute_may_aliases ();
1943 if (optimize && (flags & TODO_update_address_taken))
1944 execute_update_addresses_taken ();
1946 if (flags & TODO_remove_unused_locals)
1947 remove_unused_locals ();
1949 if (flags & TODO_rebuild_frequencies)
1950 rebuild_frequencies ();
1952 if (flags & TODO_rebuild_cgraph_edges)
1953 cgraph_edge::rebuild_edges ();
1955 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1956 /* If we've seen errors do not bother running any verifiers. */
1957 if (flag_checking && !seen_error ())
1959 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1960 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1962 if (flags & TODO_verify_il)
1964 if (cfun->curr_properties & PROP_trees)
1966 if (cfun->curr_properties & PROP_cfg)
1967 /* IPA passes leave stmts to be fixed up, so make sure to
1968 not verify stmts really throw. */
1969 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1970 else
1971 verify_gimple_in_seq (gimple_body (cfun->decl));
1973 if (cfun->curr_properties & PROP_ssa)
1974 /* IPA passes leave stmts to be fixed up, so make sure to
1975 not verify SSA operands whose verifier will choke on that. */
1976 verify_ssa (true, !from_ipa_pass);
1977 /* IPA passes leave basic-blocks unsplit, so make sure to
1978 not trip on that. */
1979 if ((cfun->curr_properties & PROP_cfg)
1980 && !from_ipa_pass)
1981 verify_flow_info ();
1982 if (current_loops
1983 && ! loops_state_satisfies_p (LOOPS_NEED_FIXUP))
1985 verify_loop_structure ();
1986 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
1987 verify_loop_closed_ssa (false);
1989 if (cfun->curr_properties & PROP_rtl)
1990 verify_rtl_sharing ();
1993 /* Make sure verifiers don't change dominator state. */
1994 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1995 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1998 fn->last_verified = flags & TODO_verify_all;
2000 pop_cfun ();
2002 /* For IPA passes make sure to release dominator info, it can be
2003 computed by non-verifying TODOs. */
2004 if (from_ipa_pass)
2006 free_dominance_info (fn, CDI_DOMINATORS);
2007 free_dominance_info (fn, CDI_POST_DOMINATORS);
2011 /* Perform all TODO actions. */
2012 static void
2013 execute_todo (unsigned int flags)
2015 if (flag_checking
2016 && cfun
2017 && need_ssa_update_p (cfun))
2018 gcc_assert (flags & TODO_update_ssa_any);
2020 statistics_fini_pass ();
2022 if (flags)
2023 do_per_function (execute_function_todo, (void *)(size_t) flags);
2025 /* At this point we should not have any unreachable code in the
2026 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
2027 if (cfun && cfun->gimple_df)
2028 flush_ssaname_freelist ();
2030 /* Always remove functions just as before inlining: IPA passes might be
2031 interested to see bodies of extern inline functions that are not inlined
2032 to analyze side effects. The full removal is done just at the end
2033 of IPA pass queue. */
2034 if (flags & TODO_remove_functions)
2036 gcc_assert (!cfun);
2037 symtab->remove_unreachable_nodes (dump_file);
2040 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2042 gcc_assert (!cfun);
2043 symtab->dump (dump_file);
2044 /* Flush the file. If verification fails, we won't be able to
2045 close the file before aborting. */
2046 fflush (dump_file);
2049 /* Now that the dumping has been done, we can get rid of the optional
2050 df problems. */
2051 if (flags & TODO_df_finish)
2052 df_finish_pass ((flags & TODO_df_verify) != 0);
2055 /* Verify invariants that should hold between passes. This is a place
2056 to put simple sanity checks. */
2058 static void
2059 verify_interpass_invariants (void)
2061 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2064 /* Clear the last verified flag. */
2066 static void
2067 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2069 fn->last_verified = 0;
2072 /* Helper function. Verify that the properties has been turn into the
2073 properties expected by the pass. */
2075 static void
2076 verify_curr_properties (function *fn, void *data)
2078 unsigned int props = (size_t)data;
2079 gcc_assert ((fn->curr_properties & props) == props);
2082 /* Release dump file name if set. */
2084 static void
2085 release_dump_file_name (void)
2087 if (dump_file_name)
2089 free (CONST_CAST (char *, dump_file_name));
2090 dump_file_name = NULL;
2094 /* Initialize pass dump file. */
2095 /* This is non-static so that the plugins can use it. */
2097 bool
2098 pass_init_dump_file (opt_pass *pass)
2100 /* If a dump file name is present, open it if enabled. */
2101 if (pass->static_pass_number != -1)
2103 timevar_push (TV_DUMP);
2104 gcc::dump_manager *dumps = g->get_dumps ();
2105 bool initializing_dump =
2106 !dumps->dump_initialized_p (pass->static_pass_number);
2107 release_dump_file_name ();
2108 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2109 dumps->dump_start (pass->static_pass_number, &dump_flags);
2110 if (dump_file && current_function_decl && ! (dump_flags & TDF_GIMPLE))
2111 dump_function_header (dump_file, current_function_decl, dump_flags);
2112 if (initializing_dump
2113 && dump_file && (dump_flags & TDF_GRAPH)
2114 && cfun && (cfun->curr_properties & PROP_cfg))
2116 clean_graph_dump_file (dump_file_name);
2117 struct dump_file_info *dfi
2118 = dumps->get_dump_file_info (pass->static_pass_number);
2119 dfi->graph_dump_initialized = true;
2121 timevar_pop (TV_DUMP);
2122 return initializing_dump;
2124 else
2125 return false;
2128 /* Flush PASS dump file. */
2129 /* This is non-static so that plugins can use it. */
2131 void
2132 pass_fini_dump_file (opt_pass *pass)
2134 timevar_push (TV_DUMP);
2136 /* Flush and close dump file. */
2137 release_dump_file_name ();
2139 g->get_dumps ()->dump_finish (pass->static_pass_number);
2140 timevar_pop (TV_DUMP);
2143 /* After executing the pass, apply expected changes to the function
2144 properties. */
2146 static void
2147 update_properties_after_pass (function *fn, void *data)
2149 opt_pass *pass = (opt_pass *) data;
2150 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2151 & ~pass->properties_destroyed;
2154 /* Execute summary generation for all of the passes in IPA_PASS. */
2156 void
2157 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2159 while (ipa_pass)
2161 opt_pass *pass = ipa_pass;
2163 /* Execute all of the IPA_PASSes in the list. */
2164 if (ipa_pass->type == IPA_PASS
2165 && pass->gate (cfun)
2166 && ipa_pass->generate_summary)
2168 pass_init_dump_file (pass);
2170 /* If a timevar is present, start it. */
2171 if (pass->tv_id)
2172 timevar_push (pass->tv_id);
2174 current_pass = pass;
2175 ipa_pass->generate_summary ();
2177 /* Stop timevar. */
2178 if (pass->tv_id)
2179 timevar_pop (pass->tv_id);
2181 pass_fini_dump_file (pass);
2183 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2187 /* Execute IPA_PASS function transform on NODE. */
2189 static void
2190 execute_one_ipa_transform_pass (struct cgraph_node *node,
2191 ipa_opt_pass_d *ipa_pass)
2193 opt_pass *pass = ipa_pass;
2194 unsigned int todo_after = 0;
2196 current_pass = pass;
2197 if (!ipa_pass->function_transform)
2198 return;
2200 /* Note that the folders should only create gimple expressions.
2201 This is a hack until the new folder is ready. */
2202 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2204 pass_init_dump_file (pass);
2206 /* If a timevar is present, start it. */
2207 if (pass->tv_id != TV_NONE)
2208 timevar_push (pass->tv_id);
2210 /* Run pre-pass verification. */
2211 execute_todo (ipa_pass->function_transform_todo_flags_start);
2213 /* Do it! */
2214 todo_after = ipa_pass->function_transform (node);
2216 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2217 check_profile_consistency (pass->static_pass_number, 0, true);
2219 /* Run post-pass cleanup and verification. */
2220 execute_todo (todo_after);
2221 verify_interpass_invariants ();
2222 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2223 check_profile_consistency (pass->static_pass_number, 1, true);
2225 /* Stop timevar. */
2226 if (pass->tv_id != TV_NONE)
2227 timevar_pop (pass->tv_id);
2229 if (dump_file)
2230 do_per_function (execute_function_dump, pass);
2231 pass_fini_dump_file (pass);
2233 current_pass = NULL;
2234 redirect_edge_var_map_empty ();
2236 /* Signal this is a suitable GC collection point. */
2237 if (!(todo_after & TODO_do_not_ggc_collect))
2238 ggc_collect ();
2241 /* For the current function, execute all ipa transforms. */
2243 void
2244 execute_all_ipa_transforms (void)
2246 struct cgraph_node *node;
2247 if (!cfun)
2248 return;
2249 node = cgraph_node::get (current_function_decl);
2251 if (node->ipa_transforms_to_apply.exists ())
2253 unsigned int i;
2255 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2256 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2257 node->ipa_transforms_to_apply.release ();
2261 /* Check if PASS is explicitly disabled or enabled and return
2262 the gate status. FUNC is the function to be processed, and
2263 GATE_STATUS is the gate status determined by pass manager by
2264 default. */
2266 static bool
2267 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2269 bool explicitly_enabled = false;
2270 bool explicitly_disabled = false;
2272 explicitly_enabled
2273 = is_pass_explicitly_enabled_or_disabled (pass, func,
2274 enabled_pass_uid_range_tab);
2275 explicitly_disabled
2276 = is_pass_explicitly_enabled_or_disabled (pass, func,
2277 disabled_pass_uid_range_tab);
2279 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2281 return gate_status;
2284 /* Determine if PASS_NAME matches CRITERION.
2285 Not a pure predicate, since it can update CRITERION, to support
2286 matching the Nth invocation of a pass.
2287 Subroutine of should_skip_pass_p. */
2289 static bool
2290 determine_pass_name_match (const char *pass_name, char *criterion)
2292 size_t namelen = strlen (pass_name);
2293 if (! strncmp (pass_name, criterion, namelen))
2295 /* The following supports starting with the Nth invocation
2296 of a pass (where N does not necessarily is equal to the
2297 dump file suffix). */
2298 if (criterion[namelen] == '\0'
2299 || (criterion[namelen] == '1'
2300 && criterion[namelen + 1] == '\0'))
2301 return true;
2302 else
2304 if (criterion[namelen + 1] == '\0')
2305 --criterion[namelen];
2306 return false;
2309 else
2310 return false;
2313 /* For skipping passes until "startwith" pass.
2314 Return true iff PASS should be skipped.
2315 Clear cfun->pass_startwith when encountering the "startwith" pass,
2316 so that all subsequent passes are run. */
2318 static bool
2319 should_skip_pass_p (opt_pass *pass)
2321 if (!cfun)
2322 return false;
2323 if (!cfun->pass_startwith)
2324 return false;
2326 /* For __GIMPLE functions, we have to at least start when we leave
2327 SSA. Hence, we need to detect the "expand" pass, and stop skipping
2328 when we encounter it. A cheap way to identify "expand" is it to
2329 detect the destruction of PROP_ssa.
2330 For __RTL functions, we invoke "rest_of_compilation" directly, which
2331 is after "expand", and hence we don't reach this conditional. */
2332 if (pass->properties_destroyed & PROP_ssa)
2334 if (!quiet_flag)
2335 fprintf (stderr, "starting anyway when leaving SSA: %s\n", pass->name);
2336 cfun->pass_startwith = NULL;
2337 return false;
2340 if (determine_pass_name_match (pass->name, cfun->pass_startwith))
2342 if (!quiet_flag)
2343 fprintf (stderr, "found starting pass: %s\n", pass->name);
2344 cfun->pass_startwith = NULL;
2345 return false;
2348 /* For GIMPLE passes, run any property provider (but continue skipping
2349 afterwards).
2350 We don't want to force running RTL passes that are property providers:
2351 "expand" is covered above, and the only pass other than "expand" that
2352 provides a property is "into_cfglayout" (PROP_cfglayout), which does
2353 too much for a dumped __RTL function. */
2354 if (pass->type == GIMPLE_PASS
2355 && pass->properties_provided != 0)
2356 return false;
2358 /* Don't skip df init; later RTL passes need it. */
2359 if (strstr (pass->name, "dfinit") != NULL)
2360 return false;
2362 if (!quiet_flag)
2363 fprintf (stderr, "skipping pass: %s\n", pass->name);
2365 /* If we get here, then we have a "startwith" that we haven't seen yet;
2366 skip the pass. */
2367 return true;
2370 /* Skip the given pass, for handling passes before "startwith"
2371 in __GIMPLE and__RTL-marked functions.
2372 In theory, this ought to be a no-op, but some of the RTL passes
2373 need additional processing here. */
2375 static void
2376 skip_pass (opt_pass *pass)
2378 /* Pass "reload" sets the global "reload_completed", and many
2379 things depend on this (e.g. instructions in .md files). */
2380 if (strcmp (pass->name, "reload") == 0)
2381 reload_completed = 1;
2383 /* The INSN_ADDRESSES vec is normally set up by
2384 shorten_branches; set it up for the benefit of passes that
2385 run after this. */
2386 if (strcmp (pass->name, "shorten") == 0)
2387 INSN_ADDRESSES_ALLOC (get_max_uid ());
2389 /* Update the cfg hooks as appropriate. */
2390 if (strcmp (pass->name, "into_cfglayout") == 0)
2392 cfg_layout_rtl_register_cfg_hooks ();
2393 cfun->curr_properties |= PROP_cfglayout;
2395 if (strcmp (pass->name, "outof_cfglayout") == 0)
2397 rtl_register_cfg_hooks ();
2398 cfun->curr_properties &= ~PROP_cfglayout;
2402 /* Execute PASS. */
2404 bool
2405 execute_one_pass (opt_pass *pass)
2407 unsigned int todo_after = 0;
2409 bool gate_status;
2411 /* IPA passes are executed on whole program, so cfun should be NULL.
2412 Other passes need function context set. */
2413 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2414 gcc_assert (!cfun && !current_function_decl);
2415 else
2416 gcc_assert (cfun && current_function_decl);
2418 current_pass = pass;
2420 /* Check whether gate check should be avoided.
2421 User controls the value of the gate through the parameter "gate_status". */
2422 gate_status = pass->gate (cfun);
2423 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2425 /* Override gate with plugin. */
2426 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2428 if (!gate_status)
2430 /* Run so passes selectively disabling themselves on a given function
2431 are not miscounted. */
2432 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2434 check_profile_consistency (pass->static_pass_number, 0, false);
2435 check_profile_consistency (pass->static_pass_number, 1, false);
2437 current_pass = NULL;
2438 return false;
2441 if (should_skip_pass_p (pass))
2443 skip_pass (pass);
2444 return true;
2447 /* Pass execution event trigger: useful to identify passes being
2448 executed. */
2449 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2451 if (!quiet_flag && !cfun)
2452 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2454 /* Note that the folders should only create gimple expressions.
2455 This is a hack until the new folder is ready. */
2456 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2458 pass_init_dump_file (pass);
2460 /* If a timevar is present, start it. */
2461 if (pass->tv_id != TV_NONE)
2462 timevar_push (pass->tv_id);
2464 /* Run pre-pass verification. */
2465 execute_todo (pass->todo_flags_start);
2467 if (flag_checking)
2468 do_per_function (verify_curr_properties,
2469 (void *)(size_t)pass->properties_required);
2471 /* Do it! */
2472 todo_after = pass->execute (cfun);
2474 if (todo_after & TODO_discard_function)
2476 /* Stop timevar. */
2477 if (pass->tv_id != TV_NONE)
2478 timevar_pop (pass->tv_id);
2480 pass_fini_dump_file (pass);
2482 gcc_assert (cfun);
2483 /* As cgraph_node::release_body expects release dominators info,
2484 we have to release it. */
2485 if (dom_info_available_p (CDI_DOMINATORS))
2486 free_dominance_info (CDI_DOMINATORS);
2488 if (dom_info_available_p (CDI_POST_DOMINATORS))
2489 free_dominance_info (CDI_POST_DOMINATORS);
2491 tree fn = cfun->decl;
2492 pop_cfun ();
2493 gcc_assert (!cfun);
2494 cgraph_node::get (fn)->release_body ();
2496 current_pass = NULL;
2497 redirect_edge_var_map_empty ();
2499 ggc_collect ();
2501 return true;
2504 do_per_function (clear_last_verified, NULL);
2506 do_per_function (update_properties_after_pass, pass);
2508 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2509 check_profile_consistency (pass->static_pass_number, 0, true);
2511 /* Run post-pass cleanup and verification. */
2512 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2513 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2514 check_profile_consistency (pass->static_pass_number, 1, true);
2516 verify_interpass_invariants ();
2518 /* Stop timevar. */
2519 if (pass->tv_id != TV_NONE)
2520 timevar_pop (pass->tv_id);
2522 if (pass->type == IPA_PASS
2523 && ((ipa_opt_pass_d *)pass)->function_transform)
2525 struct cgraph_node *node;
2526 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2527 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2529 else if (dump_file)
2530 do_per_function (execute_function_dump, pass);
2532 if (!current_function_decl)
2533 symtab->process_new_functions ();
2535 pass_fini_dump_file (pass);
2537 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2538 gcc_assert (!(cfun->curr_properties & PROP_trees)
2539 || pass->type != RTL_PASS);
2541 current_pass = NULL;
2542 redirect_edge_var_map_empty ();
2544 /* Signal this is a suitable GC collection point. */
2545 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2546 ggc_collect ();
2548 return true;
2551 static void
2552 execute_pass_list_1 (opt_pass *pass)
2556 gcc_assert (pass->type == GIMPLE_PASS
2557 || pass->type == RTL_PASS);
2559 if (cfun == NULL)
2560 return;
2561 if (execute_one_pass (pass) && pass->sub)
2562 execute_pass_list_1 (pass->sub);
2563 pass = pass->next;
2565 while (pass);
2568 void
2569 execute_pass_list (function *fn, opt_pass *pass)
2571 gcc_assert (fn == cfun);
2572 execute_pass_list_1 (pass);
2573 if (cfun && fn->cfg)
2575 free_dominance_info (CDI_DOMINATORS);
2576 free_dominance_info (CDI_POST_DOMINATORS);
2580 /* Write out all LTO data. */
2581 static void
2582 write_lto (void)
2584 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2585 lto_output ();
2586 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2587 timevar_push (TV_IPA_LTO_DECL_OUT);
2588 produce_asm_for_decls ();
2589 timevar_pop (TV_IPA_LTO_DECL_OUT);
2592 /* Same as execute_pass_list but assume that subpasses of IPA passes
2593 are local passes. If SET is not NULL, write out summaries of only
2594 those node in SET. */
2596 static void
2597 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2599 while (pass)
2601 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2602 gcc_assert (!current_function_decl);
2603 gcc_assert (!cfun);
2604 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2605 if (pass->type == IPA_PASS
2606 && ipa_pass->write_summary
2607 && pass->gate (cfun))
2609 /* If a timevar is present, start it. */
2610 if (pass->tv_id)
2611 timevar_push (pass->tv_id);
2613 pass_init_dump_file (pass);
2615 current_pass = pass;
2616 ipa_pass->write_summary ();
2618 pass_fini_dump_file (pass);
2620 /* If a timevar is present, start it. */
2621 if (pass->tv_id)
2622 timevar_pop (pass->tv_id);
2625 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2626 ipa_write_summaries_2 (pass->sub, state);
2628 pass = pass->next;
2632 /* Helper function of ipa_write_summaries. Creates and destroys the
2633 decl state and calls ipa_write_summaries_2 for all passes that have
2634 summaries. SET is the set of nodes to be written. */
2636 static void
2637 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2639 pass_manager *passes = g->get_passes ();
2640 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2641 state->symtab_node_encoder = encoder;
2643 lto_output_init_mode_table ();
2644 lto_push_out_decl_state (state);
2646 gcc_assert (!flag_wpa);
2647 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2649 write_lto ();
2651 gcc_assert (lto_get_out_decl_state () == state);
2652 lto_pop_out_decl_state ();
2653 lto_delete_out_decl_state (state);
2656 /* Write out summaries for all the nodes in the callgraph. */
2658 void
2659 ipa_write_summaries (void)
2661 lto_symtab_encoder_t encoder;
2662 int i, order_pos;
2663 varpool_node *vnode;
2664 struct cgraph_node *node;
2665 struct cgraph_node **order;
2667 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2668 return;
2670 select_what_to_stream ();
2672 encoder = lto_symtab_encoder_new (false);
2674 /* Create the callgraph set in the same order used in
2675 cgraph_expand_all_functions. This mostly facilitates debugging,
2676 since it causes the gimple file to be processed in the same order
2677 as the source code. */
2678 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2679 order_pos = ipa_reverse_postorder (order);
2680 gcc_assert (order_pos == symtab->cgraph_count);
2682 for (i = order_pos - 1; i >= 0; i--)
2684 struct cgraph_node *node = order[i];
2686 if (node->has_gimple_body_p ())
2688 /* When streaming out references to statements as part of some IPA
2689 pass summary, the statements need to have uids assigned and the
2690 following does that for all the IPA passes here. Naturally, this
2691 ordering then matches the one IPA-passes get in their stmt_fixup
2692 hooks. */
2694 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2695 renumber_gimple_stmt_uids ();
2696 pop_cfun ();
2698 if (node->definition && node->need_lto_streaming)
2699 lto_set_symtab_encoder_in_partition (encoder, node);
2702 FOR_EACH_DEFINED_FUNCTION (node)
2703 if (node->alias && node->need_lto_streaming)
2704 lto_set_symtab_encoder_in_partition (encoder, node);
2705 FOR_EACH_DEFINED_VARIABLE (vnode)
2706 if (vnode->need_lto_streaming)
2707 lto_set_symtab_encoder_in_partition (encoder, vnode);
2709 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2711 free (order);
2714 /* Same as execute_pass_list but assume that subpasses of IPA passes
2715 are local passes. If SET is not NULL, write out optimization summaries of
2716 only those node in SET. */
2718 static void
2719 ipa_write_optimization_summaries_1 (opt_pass *pass,
2720 struct lto_out_decl_state *state)
2722 while (pass)
2724 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)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 (pass->type == IPA_PASS
2729 && ipa_pass->write_optimization_summary
2730 && pass->gate (cfun))
2732 /* If a timevar is present, start it. */
2733 if (pass->tv_id)
2734 timevar_push (pass->tv_id);
2736 pass_init_dump_file (pass);
2738 current_pass = pass;
2739 ipa_pass->write_optimization_summary ();
2741 pass_fini_dump_file (pass);
2743 /* If a timevar is present, start it. */
2744 if (pass->tv_id)
2745 timevar_pop (pass->tv_id);
2748 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2749 ipa_write_optimization_summaries_1 (pass->sub, state);
2751 pass = pass->next;
2755 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2756 NULL, write out all summaries of all nodes. */
2758 void
2759 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2761 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2762 lto_symtab_encoder_iterator lsei;
2763 state->symtab_node_encoder = encoder;
2765 lto_output_init_mode_table ();
2766 lto_push_out_decl_state (state);
2767 for (lsei = lsei_start_function_in_partition (encoder);
2768 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2770 struct cgraph_node *node = lsei_cgraph_node (lsei);
2771 /* When streaming out references to statements as part of some IPA
2772 pass summary, the statements need to have uids assigned.
2774 For functions newly born at WPA stage we need to initialize
2775 the uids here. */
2776 if (node->definition
2777 && gimple_has_body_p (node->decl))
2779 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2780 renumber_gimple_stmt_uids ();
2781 pop_cfun ();
2785 gcc_assert (flag_wpa);
2786 pass_manager *passes = g->get_passes ();
2787 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2789 write_lto ();
2791 gcc_assert (lto_get_out_decl_state () == state);
2792 lto_pop_out_decl_state ();
2793 lto_delete_out_decl_state (state);
2796 /* Same as execute_pass_list but assume that subpasses of IPA passes
2797 are local passes. */
2799 static void
2800 ipa_read_summaries_1 (opt_pass *pass)
2802 while (pass)
2804 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2806 gcc_assert (!current_function_decl);
2807 gcc_assert (!cfun);
2808 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2810 if (pass->gate (cfun))
2812 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2814 /* If a timevar is present, start it. */
2815 if (pass->tv_id)
2816 timevar_push (pass->tv_id);
2818 pass_init_dump_file (pass);
2820 current_pass = pass;
2821 ipa_pass->read_summary ();
2823 pass_fini_dump_file (pass);
2825 /* Stop timevar. */
2826 if (pass->tv_id)
2827 timevar_pop (pass->tv_id);
2830 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2831 ipa_read_summaries_1 (pass->sub);
2833 pass = pass->next;
2838 /* Read all the summaries for all_regular_ipa_passes. */
2840 void
2841 ipa_read_summaries (void)
2843 pass_manager *passes = g->get_passes ();
2844 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2847 /* Same as execute_pass_list but assume that subpasses of IPA passes
2848 are local passes. */
2850 static void
2851 ipa_read_optimization_summaries_1 (opt_pass *pass)
2853 while (pass)
2855 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2857 gcc_assert (!current_function_decl);
2858 gcc_assert (!cfun);
2859 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2861 if (pass->gate (cfun))
2863 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2865 /* If a timevar is present, start it. */
2866 if (pass->tv_id)
2867 timevar_push (pass->tv_id);
2869 pass_init_dump_file (pass);
2871 current_pass = pass;
2872 ipa_pass->read_optimization_summary ();
2874 pass_fini_dump_file (pass);
2876 /* Stop timevar. */
2877 if (pass->tv_id)
2878 timevar_pop (pass->tv_id);
2881 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2882 ipa_read_optimization_summaries_1 (pass->sub);
2884 pass = pass->next;
2888 /* Read all the summaries for all_regular_ipa_passes. */
2890 void
2891 ipa_read_optimization_summaries (void)
2893 pass_manager *passes = g->get_passes ();
2894 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2897 /* Same as execute_pass_list but assume that subpasses of IPA passes
2898 are local passes. */
2899 void
2900 execute_ipa_pass_list (opt_pass *pass)
2904 gcc_assert (!current_function_decl);
2905 gcc_assert (!cfun);
2906 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2907 if (execute_one_pass (pass) && pass->sub)
2909 if (pass->sub->type == GIMPLE_PASS)
2911 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2912 do_per_function_toporder ((void (*)(function *, void *))
2913 execute_pass_list,
2914 pass->sub);
2915 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2917 else if (pass->sub->type == SIMPLE_IPA_PASS
2918 || pass->sub->type == IPA_PASS)
2919 execute_ipa_pass_list (pass->sub);
2920 else
2921 gcc_unreachable ();
2923 gcc_assert (!current_function_decl);
2924 symtab->process_new_functions ();
2925 pass = pass->next;
2927 while (pass);
2930 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2932 static void
2933 execute_ipa_stmt_fixups (opt_pass *pass,
2934 struct cgraph_node *node, gimple **stmts)
2936 while (pass)
2938 /* Execute all of the IPA_PASSes in the list. */
2939 if (pass->type == IPA_PASS
2940 && pass->gate (cfun))
2942 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2944 if (ipa_pass->stmt_fixup)
2946 pass_init_dump_file (pass);
2947 /* If a timevar is present, start it. */
2948 if (pass->tv_id)
2949 timevar_push (pass->tv_id);
2951 current_pass = pass;
2952 ipa_pass->stmt_fixup (node, stmts);
2954 /* Stop timevar. */
2955 if (pass->tv_id)
2956 timevar_pop (pass->tv_id);
2957 pass_fini_dump_file (pass);
2959 if (pass->sub)
2960 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2962 pass = pass->next;
2966 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2968 void
2969 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2971 pass_manager *passes = g->get_passes ();
2972 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2976 extern void debug_properties (unsigned int);
2977 extern void dump_properties (FILE *, unsigned int);
2979 DEBUG_FUNCTION void
2980 dump_properties (FILE *dump, unsigned int props)
2982 fprintf (dump, "Properties:\n");
2983 if (props & PROP_gimple_any)
2984 fprintf (dump, "PROP_gimple_any\n");
2985 if (props & PROP_gimple_lcf)
2986 fprintf (dump, "PROP_gimple_lcf\n");
2987 if (props & PROP_gimple_leh)
2988 fprintf (dump, "PROP_gimple_leh\n");
2989 if (props & PROP_cfg)
2990 fprintf (dump, "PROP_cfg\n");
2991 if (props & PROP_ssa)
2992 fprintf (dump, "PROP_ssa\n");
2993 if (props & PROP_no_crit_edges)
2994 fprintf (dump, "PROP_no_crit_edges\n");
2995 if (props & PROP_rtl)
2996 fprintf (dump, "PROP_rtl\n");
2997 if (props & PROP_gimple_lomp)
2998 fprintf (dump, "PROP_gimple_lomp\n");
2999 if (props & PROP_gimple_lomp_dev)
3000 fprintf (dump, "PROP_gimple_lomp_dev\n");
3001 if (props & PROP_gimple_lcx)
3002 fprintf (dump, "PROP_gimple_lcx\n");
3003 if (props & PROP_gimple_lvec)
3004 fprintf (dump, "PROP_gimple_lvec\n");
3005 if (props & PROP_cfglayout)
3006 fprintf (dump, "PROP_cfglayout\n");
3009 DEBUG_FUNCTION void
3010 debug_properties (unsigned int props)
3012 dump_properties (stderr, props);
3015 /* Called by local passes to see if function is called by already processed nodes.
3016 Because we process nodes in topological order, this means that function is
3017 in recursive cycle or we introduced new direct calls. */
3018 bool
3019 function_called_by_processed_nodes_p (void)
3021 struct cgraph_edge *e;
3022 for (e = cgraph_node::get (current_function_decl)->callers;
3024 e = e->next_caller)
3026 if (e->caller->decl == current_function_decl)
3027 continue;
3028 if (!e->caller->has_gimple_body_p ())
3029 continue;
3030 if (TREE_ASM_WRITTEN (e->caller->decl))
3031 continue;
3032 if (!e->caller->process && !e->caller->global.inlined_to)
3033 break;
3035 if (dump_file && e)
3037 fprintf (dump_file, "Already processed call to:\n");
3038 e->caller->dump (dump_file);
3040 return e != NULL;
3043 #include "gt-passes.h"