2016-04-21 Arnaud Charlet <charlet@adacore.com>
[official-gcc.git] / gcc / passes.c
blob2b70846dd4012ff0f27d61dc06474414cca017fc
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2016 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 "tm_p.h"
36 #include "ssa.h"
37 #include "emit-rtl.h"
38 #include "cgraph.h"
39 #include "lto-streamer.h"
40 #include "fold-const.h"
41 #include "varasm.h"
42 #include "output.h"
43 #include "graph.h"
44 #include "debug.h"
45 #include "cfgloop.h"
46 #include "value-prof.h"
47 #include "tree-cfg.h"
48 #include "tree-ssa-loop-manip.h"
49 #include "tree-into-ssa.h"
50 #include "tree-dfa.h"
51 #include "tree-ssa.h"
52 #include "tree-pass.h"
53 #include "plugin.h"
54 #include "ipa-utils.h"
55 #include "tree-pretty-print.h" /* for dump_function_header */
56 #include "context.h"
57 #include "pass_manager.h"
58 #include "cfgrtl.h"
59 #include "tree-ssa-live.h" /* For remove_unused_locals. */
60 #include "tree-cfgcleanup.h"
62 using namespace gcc;
64 /* This is used for debugging. It allows the current pass to printed
65 from anywhere in compilation.
66 The variable current_pass is also used for statistics and plugins. */
67 opt_pass *current_pass;
69 static void register_pass_name (opt_pass *, const char *);
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 && (TREE_CODE (decl) != VAR_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 (TREE_CODE (decl) == VAR_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 && TREE_CODE (decl) == VAR_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;
776 char num[10];
777 int flags, id;
778 int optgroup_flags = OPTGROUP_NONE;
779 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
781 /* See below in next_pass_1. */
782 num[0] = '\0';
783 if (pass->static_pass_number != -1)
784 sprintf (num, "%d", ((int) pass->static_pass_number < 0
785 ? 1 : pass->static_pass_number));
787 /* The name is both used to identify the pass for the purposes of plugins,
788 and to specify dump file name and option.
789 The latter two might want something short which is not quite unique; for
790 that reason, we may have a disambiguating prefix, followed by a space
791 to mark the start of the following dump file name / option string. */
792 name = strchr (pass->name, ' ');
793 name = name ? name + 1 : pass->name;
794 dot_name = concat (".", name, num, NULL);
795 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
797 prefix = "ipa-";
798 flags = TDF_IPA;
799 optgroup_flags |= OPTGROUP_IPA;
801 else if (pass->type == GIMPLE_PASS)
803 prefix = "tree-";
804 flags = TDF_TREE;
806 else
808 prefix = "rtl-";
809 flags = TDF_RTL;
812 flag_name = concat (prefix, name, num, NULL);
813 glob_name = concat (prefix, name, NULL);
814 optgroup_flags |= pass->optinfo_flags;
815 /* For any passes that do not have an optgroup set, and which are not
816 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
817 any dump messages are emitted properly under -fopt-info(-optall). */
818 if (optgroup_flags == OPTGROUP_NONE)
819 optgroup_flags = OPTGROUP_OTHER;
820 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
821 optgroup_flags,
822 true);
823 set_pass_for_id (id, pass);
824 full_name = concat (prefix, pass->name, num, NULL);
825 register_pass_name (pass, full_name);
826 free (CONST_CAST (char *, full_name));
829 /* Register the dump files for the pass_manager starting at PASS. */
831 void
832 pass_manager::register_dump_files (opt_pass *pass)
836 if (pass->name && pass->name[0] != '*')
837 register_one_dump_file (pass);
839 if (pass->sub)
840 register_dump_files (pass->sub);
842 pass = pass->next;
844 while (pass);
847 static hash_map<nofree_string_hash, opt_pass *> *name_to_pass_map;
849 /* Register PASS with NAME. */
851 static void
852 register_pass_name (opt_pass *pass, const char *name)
854 if (!name_to_pass_map)
855 name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
857 if (name_to_pass_map->get (name))
858 return; /* Ignore plugin passes. */
860 const char *unique_name = xstrdup (name);
861 name_to_pass_map->put (unique_name, pass);
864 /* Map from pass id to canonicalized pass name. */
866 typedef const char *char_ptr;
867 static vec<char_ptr> pass_tab = vNULL;
869 /* Callback function for traversing NAME_TO_PASS_MAP. */
871 bool
872 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
874 gcc_assert (pass->static_pass_number > 0);
875 gcc_assert (pass_tab.exists ());
877 pass_tab[pass->static_pass_number] = name;
879 return 1;
882 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
883 table for dumping purpose. */
885 static void
886 create_pass_tab (void)
888 if (!flag_dump_passes)
889 return;
891 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
892 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
895 static bool override_gate_status (opt_pass *, tree, bool);
897 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
898 is turned on or not. */
900 static void
901 dump_one_pass (opt_pass *pass, int pass_indent)
903 int indent = 3 * pass_indent;
904 const char *pn;
905 bool is_on, is_really_on;
907 is_on = pass->gate (cfun);
908 is_really_on = override_gate_status (pass, current_function_decl, is_on);
910 if (pass->static_pass_number <= 0)
911 pn = pass->name;
912 else
913 pn = pass_tab[pass->static_pass_number];
915 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
916 (15 - indent < 0 ? 0 : 15 - indent), " ",
917 is_on ? " ON" : " OFF",
918 ((!is_on) == (!is_really_on) ? ""
919 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
922 /* Dump pass list PASS with indentation INDENT. */
924 static void
925 dump_pass_list (opt_pass *pass, int indent)
929 dump_one_pass (pass, indent);
930 if (pass->sub)
931 dump_pass_list (pass->sub, indent + 1);
932 pass = pass->next;
934 while (pass);
937 /* Dump all optimization passes. */
939 void
940 dump_passes (void)
942 g->get_passes ()->dump_passes ();
945 void
946 pass_manager::dump_passes () const
948 push_dummy_function (true);
950 create_pass_tab ();
952 dump_pass_list (all_lowering_passes, 1);
953 dump_pass_list (all_small_ipa_passes, 1);
954 dump_pass_list (all_regular_ipa_passes, 1);
955 dump_pass_list (all_late_ipa_passes, 1);
956 dump_pass_list (all_passes, 1);
958 pop_dummy_function ();
961 /* Returns the pass with NAME. */
963 static opt_pass *
964 get_pass_by_name (const char *name)
966 opt_pass **p = name_to_pass_map->get (name);
967 if (p)
968 return *p;
970 return NULL;
974 /* Range [start, last]. */
976 struct uid_range
978 unsigned int start;
979 unsigned int last;
980 const char *assem_name;
981 struct uid_range *next;
984 typedef struct uid_range *uid_range_p;
987 static vec<uid_range_p>
988 enabled_pass_uid_range_tab = vNULL;
989 static vec<uid_range_p>
990 disabled_pass_uid_range_tab = vNULL;
993 /* Parse option string for -fdisable- and -fenable-
994 The syntax of the options:
996 -fenable-<pass_name>
997 -fdisable-<pass_name>
999 -fenable-<pass_name>=s1:e1,s2:e2,...
1000 -fdisable-<pass_name>=s1:e1,s2:e2,...
1003 static void
1004 enable_disable_pass (const char *arg, bool is_enable)
1006 opt_pass *pass;
1007 char *range_str, *phase_name;
1008 char *argstr = xstrdup (arg);
1009 vec<uid_range_p> *tab = 0;
1011 range_str = strchr (argstr,'=');
1012 if (range_str)
1014 *range_str = '\0';
1015 range_str++;
1018 phase_name = argstr;
1019 if (!*phase_name)
1021 if (is_enable)
1022 error ("unrecognized option -fenable");
1023 else
1024 error ("unrecognized option -fdisable");
1025 free (argstr);
1026 return;
1028 pass = get_pass_by_name (phase_name);
1029 if (!pass || pass->static_pass_number == -1)
1031 if (is_enable)
1032 error ("unknown pass %s specified in -fenable", phase_name);
1033 else
1034 error ("unknown pass %s specified in -fdisable", phase_name);
1035 free (argstr);
1036 return;
1039 if (is_enable)
1040 tab = &enabled_pass_uid_range_tab;
1041 else
1042 tab = &disabled_pass_uid_range_tab;
1044 if ((unsigned) pass->static_pass_number >= tab->length ())
1045 tab->safe_grow_cleared (pass->static_pass_number + 1);
1047 if (!range_str)
1049 uid_range_p slot;
1050 uid_range_p new_range = XCNEW (struct uid_range);
1052 new_range->start = 0;
1053 new_range->last = (unsigned)-1;
1055 slot = (*tab)[pass->static_pass_number];
1056 new_range->next = slot;
1057 (*tab)[pass->static_pass_number] = new_range;
1058 if (is_enable)
1059 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1060 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1061 else
1062 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1063 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1065 else
1067 char *next_range = NULL;
1068 char *one_range = range_str;
1069 char *end_val = NULL;
1073 uid_range_p slot;
1074 uid_range_p new_range;
1075 char *invalid = NULL;
1076 long start;
1077 char *func_name = NULL;
1079 next_range = strchr (one_range, ',');
1080 if (next_range)
1082 *next_range = '\0';
1083 next_range++;
1086 end_val = strchr (one_range, ':');
1087 if (end_val)
1089 *end_val = '\0';
1090 end_val++;
1092 start = strtol (one_range, &invalid, 10);
1093 if (*invalid || start < 0)
1095 if (end_val || (one_range[0] >= '0'
1096 && one_range[0] <= '9'))
1098 error ("Invalid range %s in option %s",
1099 one_range,
1100 is_enable ? "-fenable" : "-fdisable");
1101 free (argstr);
1102 return;
1104 func_name = one_range;
1106 if (!end_val)
1108 new_range = XCNEW (struct uid_range);
1109 if (!func_name)
1111 new_range->start = (unsigned) start;
1112 new_range->last = (unsigned) start;
1114 else
1116 new_range->start = (unsigned) -1;
1117 new_range->last = (unsigned) -1;
1118 new_range->assem_name = xstrdup (func_name);
1121 else
1123 long last = strtol (end_val, &invalid, 10);
1124 if (*invalid || last < start)
1126 error ("Invalid range %s in option %s",
1127 end_val,
1128 is_enable ? "-fenable" : "-fdisable");
1129 free (argstr);
1130 return;
1132 new_range = XCNEW (struct uid_range);
1133 new_range->start = (unsigned) start;
1134 new_range->last = (unsigned) last;
1137 slot = (*tab)[pass->static_pass_number];
1138 new_range->next = slot;
1139 (*tab)[pass->static_pass_number] = new_range;
1140 if (is_enable)
1142 if (new_range->assem_name)
1143 inform (UNKNOWN_LOCATION,
1144 "enable pass %s for function %s",
1145 phase_name, new_range->assem_name);
1146 else
1147 inform (UNKNOWN_LOCATION,
1148 "enable pass %s for functions in the range of [%u, %u]",
1149 phase_name, new_range->start, new_range->last);
1151 else
1153 if (new_range->assem_name)
1154 inform (UNKNOWN_LOCATION,
1155 "disable pass %s for function %s",
1156 phase_name, new_range->assem_name);
1157 else
1158 inform (UNKNOWN_LOCATION,
1159 "disable pass %s for functions in the range of [%u, %u]",
1160 phase_name, new_range->start, new_range->last);
1163 one_range = next_range;
1164 } while (next_range);
1167 free (argstr);
1170 /* Enable pass specified by ARG. */
1172 void
1173 enable_pass (const char *arg)
1175 enable_disable_pass (arg, true);
1178 /* Disable pass specified by ARG. */
1180 void
1181 disable_pass (const char *arg)
1183 enable_disable_pass (arg, false);
1186 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1188 static bool
1189 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1190 tree func,
1191 vec<uid_range_p> tab)
1193 uid_range_p slot, range;
1194 int cgraph_uid;
1195 const char *aname = NULL;
1197 if (!tab.exists ()
1198 || (unsigned) pass->static_pass_number >= tab.length ()
1199 || pass->static_pass_number == -1)
1200 return false;
1202 slot = tab[pass->static_pass_number];
1203 if (!slot)
1204 return false;
1206 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1207 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1208 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1210 range = slot;
1211 while (range)
1213 if ((unsigned) cgraph_uid >= range->start
1214 && (unsigned) cgraph_uid <= range->last)
1215 return true;
1216 if (range->assem_name && aname
1217 && !strcmp (range->assem_name, aname))
1218 return true;
1219 range = range->next;
1222 return false;
1226 /* Update static_pass_number for passes (and the flag
1227 TODO_mark_first_instance).
1229 Passes are constructed with static_pass_number preinitialized to 0
1231 This field is used in two different ways: initially as instance numbers
1232 of their kind, and then as ids within the entire pass manager.
1234 Within pass_manager::pass_manager:
1236 * In add_pass_instance(), as called by next_pass_1 in
1237 NEXT_PASS in init_optimization_passes
1239 * When the initial instance of a pass within a pass manager is seen,
1240 it is flagged, and its static_pass_number is set to -1
1242 * On subsequent times that it is seen, the static pass number
1243 is decremented each time, so that if there are e.g. 4 dups,
1244 they have static_pass_number -4, 2, 3, 4 respectively (note
1245 how the initial one is negative and gives the count); these
1246 can be thought of as instance numbers of the specific pass
1248 * Within the register_dump_files () traversal, set_pass_for_id()
1249 is called on each pass, using these instance numbers to create
1250 dumpfile switches, and then overwriting them with a pass id,
1251 which are global to the whole pass manager (based on
1252 (TDI_end + current value of extra_dump_files_in_use) ) */
1254 static void
1255 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1256 opt_pass *initial_pass)
1258 /* Are we dealing with the first pass of its kind, or a clone? */
1259 if (new_pass != initial_pass)
1261 /* We're dealing with a clone. */
1262 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1264 /* Indicate to register_dump_files that this pass has duplicates,
1265 and so it should rename the dump file. The first instance will
1266 be -1, and be number of duplicates = -static_pass_number - 1.
1267 Subsequent instances will be > 0 and just the duplicate number. */
1268 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1270 initial_pass->static_pass_number -= 1;
1271 new_pass->static_pass_number = -initial_pass->static_pass_number;
1274 else
1276 /* We're dealing with the first pass of its kind. */
1277 new_pass->todo_flags_start |= TODO_mark_first_instance;
1278 new_pass->static_pass_number = -1;
1280 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1284 /* Add a pass to the pass list. Duplicate the pass if it's already
1285 in the list. */
1287 static opt_pass **
1288 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1290 /* Every pass should have a name so that plugins can refer to them. */
1291 gcc_assert (pass->name != NULL);
1293 add_pass_instance (pass, false, initial_pass);
1294 *list = pass;
1296 return &(*list)->next;
1299 /* List node for an inserted pass instance. We need to keep track of all
1300 the newly-added pass instances (with 'added_pass_nodes' defined below)
1301 so that we can register their dump files after pass-positioning is finished.
1302 Registering dumping files needs to be post-processed or the
1303 static_pass_number of the opt_pass object would be modified and mess up
1304 the dump file names of future pass instances to be added. */
1306 struct pass_list_node
1308 opt_pass *pass;
1309 struct pass_list_node *next;
1312 static struct pass_list_node *added_pass_nodes = NULL;
1313 static struct pass_list_node *prev_added_pass_node;
1315 /* Insert the pass at the proper position. Return true if the pass
1316 is successfully added.
1318 NEW_PASS_INFO - new pass to be inserted
1319 PASS_LIST - root of the pass list to insert the new pass to */
1321 static bool
1322 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1324 opt_pass *pass = *pass_list, *prev_pass = NULL;
1325 bool success = false;
1327 for ( ; pass; prev_pass = pass, pass = pass->next)
1329 /* Check if the current pass is of the same type as the new pass and
1330 matches the name and the instance number of the reference pass. */
1331 if (pass->type == new_pass_info->pass->type
1332 && pass->name
1333 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1334 && ((new_pass_info->ref_pass_instance_number == 0)
1335 || (new_pass_info->ref_pass_instance_number ==
1336 pass->static_pass_number)
1337 || (new_pass_info->ref_pass_instance_number == 1
1338 && pass->todo_flags_start & TODO_mark_first_instance)))
1340 opt_pass *new_pass;
1341 struct pass_list_node *new_pass_node;
1343 if (new_pass_info->ref_pass_instance_number == 0)
1345 new_pass = new_pass_info->pass->clone ();
1346 add_pass_instance (new_pass, true, new_pass_info->pass);
1348 else
1350 new_pass = new_pass_info->pass;
1351 add_pass_instance (new_pass, true, new_pass);
1354 /* Insert the new pass instance based on the positioning op. */
1355 switch (new_pass_info->pos_op)
1357 case PASS_POS_INSERT_AFTER:
1358 new_pass->next = pass->next;
1359 pass->next = new_pass;
1361 /* Skip newly inserted pass to avoid repeated
1362 insertions in the case where the new pass and the
1363 existing one have the same name. */
1364 pass = new_pass;
1365 break;
1366 case PASS_POS_INSERT_BEFORE:
1367 new_pass->next = pass;
1368 if (prev_pass)
1369 prev_pass->next = new_pass;
1370 else
1371 *pass_list = new_pass;
1372 break;
1373 case PASS_POS_REPLACE:
1374 new_pass->next = pass->next;
1375 if (prev_pass)
1376 prev_pass->next = new_pass;
1377 else
1378 *pass_list = new_pass;
1379 new_pass->sub = pass->sub;
1380 new_pass->tv_id = pass->tv_id;
1381 pass = new_pass;
1382 break;
1383 default:
1384 error ("invalid pass positioning operation");
1385 return false;
1388 /* Save the newly added pass (instance) in the added_pass_nodes
1389 list so that we can register its dump file later. Note that
1390 we cannot register the dump file now because doing so will modify
1391 the static_pass_number of the opt_pass object and therefore
1392 mess up the dump file name of future instances. */
1393 new_pass_node = XCNEW (struct pass_list_node);
1394 new_pass_node->pass = new_pass;
1395 if (!added_pass_nodes)
1396 added_pass_nodes = new_pass_node;
1397 else
1398 prev_added_pass_node->next = new_pass_node;
1399 prev_added_pass_node = new_pass_node;
1401 success = true;
1404 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1405 success = true;
1408 return success;
1411 /* Hooks a new pass into the pass lists.
1413 PASS_INFO - pass information that specifies the opt_pass object,
1414 reference pass, instance number, and how to position
1415 the pass */
1417 void
1418 register_pass (struct register_pass_info *pass_info)
1420 g->get_passes ()->register_pass (pass_info);
1423 void
1424 register_pass (opt_pass* pass, pass_positioning_ops pos,
1425 const char* ref_pass_name, int ref_pass_inst_number)
1427 register_pass_info i;
1428 i.pass = pass;
1429 i.reference_pass_name = ref_pass_name;
1430 i.ref_pass_instance_number = ref_pass_inst_number;
1431 i.pos_op = pos;
1433 g->get_passes ()->register_pass (&i);
1436 void
1437 pass_manager::register_pass (struct register_pass_info *pass_info)
1439 bool all_instances, success;
1440 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1442 /* The checks below could fail in buggy plugins. Existing GCC
1443 passes should never fail these checks, so we mention plugin in
1444 the messages. */
1445 if (!pass_info->pass)
1446 fatal_error (input_location, "plugin cannot register a missing pass");
1448 if (!pass_info->pass->name)
1449 fatal_error (input_location, "plugin cannot register an unnamed pass");
1451 if (!pass_info->reference_pass_name)
1452 fatal_error
1453 (input_location,
1454 "plugin cannot register pass %qs without reference pass name",
1455 pass_info->pass->name);
1457 /* Try to insert the new pass to the pass lists. We need to check
1458 all five lists as the reference pass could be in one (or all) of
1459 them. */
1460 all_instances = pass_info->ref_pass_instance_number == 0;
1461 success = position_pass (pass_info, &all_lowering_passes);
1462 if (!success || all_instances)
1463 success |= position_pass (pass_info, &all_small_ipa_passes);
1464 if (!success || all_instances)
1465 success |= position_pass (pass_info, &all_regular_ipa_passes);
1466 if (!success || all_instances)
1467 success |= position_pass (pass_info, &all_late_ipa_passes);
1468 if (!success || all_instances)
1469 success |= position_pass (pass_info, &all_passes);
1470 if (!success)
1471 fatal_error
1472 (input_location,
1473 "pass %qs not found but is referenced by new pass %qs",
1474 pass_info->reference_pass_name, pass_info->pass->name);
1476 /* OK, we have successfully inserted the new pass. We need to register
1477 the dump files for the newly added pass and its duplicates (if any).
1478 Because the registration of plugin/backend passes happens after the
1479 command-line options are parsed, the options that specify single
1480 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1481 passes. Therefore we currently can only enable dumping of
1482 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1483 are specified. While doing so, we also delete the pass_list_node
1484 objects created during pass positioning. */
1485 while (added_pass_nodes)
1487 struct pass_list_node *next_node = added_pass_nodes->next;
1488 enum tree_dump_index tdi;
1489 register_one_dump_file (added_pass_nodes->pass);
1490 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1491 || added_pass_nodes->pass->type == IPA_PASS)
1492 tdi = TDI_ipa_all;
1493 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1494 tdi = TDI_tree_all;
1495 else
1496 tdi = TDI_rtl_all;
1497 /* Check if dump-all flag is specified. */
1498 if (dumps->get_dump_file_info (tdi)->pstate)
1500 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1501 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1502 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1503 ->pflags = dumps->get_dump_file_info (tdi)->pflags;
1505 XDELETE (added_pass_nodes);
1506 added_pass_nodes = next_node;
1510 /* Construct the pass tree. The sequencing of passes is driven by
1511 the cgraph routines:
1513 finalize_compilation_unit ()
1514 for each node N in the cgraph
1515 cgraph_analyze_function (N)
1516 cgraph_lower_function (N) -> all_lowering_passes
1518 If we are optimizing, compile is then invoked:
1520 compile ()
1521 ipa_passes () -> all_small_ipa_passes
1522 -> Analysis of all_regular_ipa_passes
1523 * possible LTO streaming at copmilation time *
1524 -> Execution of all_regular_ipa_passes
1525 * possible LTO streaming at link time *
1526 -> all_late_ipa_passes
1527 expand_all_functions ()
1528 for each node N in the cgraph
1529 expand_function (N) -> Transformation of all_regular_ipa_passes
1530 -> all_passes
1533 void *
1534 pass_manager::operator new (size_t sz)
1536 /* Ensure that all fields of the pass manager are zero-initialized. */
1537 return xcalloc (1, sz);
1540 void
1541 pass_manager::operator delete (void *ptr)
1543 free (ptr);
1546 pass_manager::pass_manager (context *ctxt)
1547 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1548 all_regular_ipa_passes (NULL),
1549 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1550 m_ctxt (ctxt)
1552 opt_pass **p;
1554 /* Initialize the pass_lists array. */
1555 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1556 GCC_PASS_LISTS
1557 #undef DEF_PASS_LIST
1559 /* Build the tree of passes. */
1561 #define INSERT_PASSES_AFTER(PASS) \
1563 opt_pass **p_start; \
1564 p_start = p = &(PASS);
1566 #define TERMINATE_PASS_LIST(PASS) \
1567 gcc_assert (p_start == &PASS); \
1568 *p = NULL; \
1571 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1573 opt_pass **p = &(PASS ## _1)->sub;
1575 #define POP_INSERT_PASSES() \
1578 #define NEXT_PASS(PASS, NUM) \
1579 do { \
1580 gcc_assert (NULL == PASS ## _ ## NUM); \
1581 if ((NUM) == 1) \
1582 PASS ## _1 = make_##PASS (m_ctxt); \
1583 else \
1585 gcc_assert (PASS ## _1); \
1586 PASS ## _ ## NUM = PASS ## _1->clone (); \
1588 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1589 } while (0)
1591 #define NEXT_PASS_WITH_ARG(PASS, NUM, ARG) \
1592 do { \
1593 NEXT_PASS (PASS, NUM); \
1594 PASS ## _ ## NUM->set_pass_param (0, ARG); \
1595 } while (0)
1597 #include "pass-instances.def"
1599 #undef INSERT_PASSES_AFTER
1600 #undef PUSH_INSERT_PASSES_WITHIN
1601 #undef POP_INSERT_PASSES
1602 #undef NEXT_PASS
1603 #undef NEXT_PASS_WITH_ARG
1604 #undef TERMINATE_PASS_LIST
1606 /* Register the passes with the tree dump code. */
1607 register_dump_files (all_lowering_passes);
1608 register_dump_files (all_small_ipa_passes);
1609 register_dump_files (all_regular_ipa_passes);
1610 register_dump_files (all_late_ipa_passes);
1611 register_dump_files (all_passes);
1614 static void
1615 delete_pass_tree (opt_pass *pass)
1617 while (pass)
1619 /* Recurse into child passes. */
1620 delete_pass_tree (pass->sub);
1622 opt_pass *next = pass->next;
1624 /* Delete this pass. */
1625 delete pass;
1627 /* Iterate onto sibling passes. */
1628 pass = next;
1632 pass_manager::~pass_manager ()
1634 XDELETEVEC (passes_by_id);
1636 /* Call delete_pass_tree on each of the pass_lists. */
1637 #define DEF_PASS_LIST(LIST) \
1638 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1639 GCC_PASS_LISTS
1640 #undef DEF_PASS_LIST
1644 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1645 function CALLBACK for every function in the call graph. Otherwise,
1646 call CALLBACK on the current function. */
1648 static void
1649 do_per_function (void (*callback) (function *, void *data), void *data)
1651 if (current_function_decl)
1652 callback (cfun, data);
1653 else
1655 struct cgraph_node *node;
1656 FOR_EACH_DEFINED_FUNCTION (node)
1657 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1658 && (!node->clone_of || node->decl != node->clone_of->decl))
1659 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1663 /* Because inlining might remove no-longer reachable nodes, we need to
1664 keep the array visible to garbage collector to avoid reading collected
1665 out nodes. */
1666 static int nnodes;
1667 static GTY ((length ("nnodes"))) cgraph_node **order;
1669 /* Hook called when NODE is removed and therefore should be
1670 excluded from order vector. DATA is an array of integers.
1671 DATA[0] holds max index it may be accessed by. For cgraph
1672 node DATA[node->uid + 1] holds index of this node in order
1673 vector. */
1674 static void
1675 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1677 int *order_idx = (int *)data;
1679 if (node->uid >= order_idx[0])
1680 return;
1682 int idx = order_idx[node->uid + 1];
1683 if (idx >= 0 && idx < nnodes && order[idx] == node)
1684 order[idx] = NULL;
1687 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1688 function CALLBACK for every function in the call graph. Otherwise,
1689 call CALLBACK on the current function.
1690 This function is global so that plugins can use it. */
1691 void
1692 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1694 int i;
1696 if (current_function_decl)
1697 callback (cfun, data);
1698 else
1700 cgraph_node_hook_list *hook;
1701 int *order_idx;
1702 gcc_assert (!order);
1703 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1705 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1706 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1707 order_idx[0] = symtab->cgraph_max_uid;
1709 nnodes = ipa_reverse_postorder (order);
1710 for (i = nnodes - 1; i >= 0; i--)
1712 order[i]->process = 1;
1713 order_idx[order[i]->uid + 1] = i;
1715 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1716 order_idx);
1717 for (i = nnodes - 1; i >= 0; i--)
1719 /* Function could be inlined and removed as unreachable. */
1720 if (!order[i])
1721 continue;
1723 struct cgraph_node *node = order[i];
1725 /* Allow possibly removed nodes to be garbage collected. */
1726 order[i] = NULL;
1727 node->process = 0;
1728 if (node->has_gimple_body_p ())
1730 struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
1731 push_cfun (fn);
1732 callback (fn, data);
1733 pop_cfun ();
1736 symtab->remove_cgraph_removal_hook (hook);
1738 ggc_free (order);
1739 order = NULL;
1740 nnodes = 0;
1743 /* Helper function to perform function body dump. */
1745 static void
1746 execute_function_dump (function *fn, void *data)
1748 opt_pass *pass = (opt_pass *)data;
1750 if (dump_file)
1752 push_cfun (fn);
1754 if (fn->curr_properties & PROP_trees)
1755 dump_function_to_file (fn->decl, dump_file, dump_flags);
1756 else
1757 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1759 /* Flush the file. If verification fails, we won't be able to
1760 close the file before aborting. */
1761 fflush (dump_file);
1763 if ((fn->curr_properties & PROP_cfg)
1764 && (dump_flags & TDF_GRAPH))
1766 gcc::dump_manager *dumps = g->get_dumps ();
1767 struct dump_file_info *dfi
1768 = dumps->get_dump_file_info (pass->static_pass_number);
1769 if (!dfi->graph_dump_initialized)
1771 clean_graph_dump_file (dump_file_name);
1772 dfi->graph_dump_initialized = true;
1774 print_graph_cfg (dump_file_name, fn);
1777 pop_cfun ();
1781 static struct profile_record *profile_record;
1783 /* Do profile consistency book-keeping for the pass with static number INDEX.
1784 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1785 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1786 if we are only book-keeping on passes that may have selectively disabled
1787 themselves on a given function. */
1788 static void
1789 check_profile_consistency (int index, int subpass, bool run)
1791 pass_manager *passes = g->get_passes ();
1792 if (index == -1)
1793 return;
1794 if (!profile_record)
1795 profile_record = XCNEWVEC (struct profile_record,
1796 passes->passes_by_id_size);
1797 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1798 gcc_assert (subpass < 2);
1799 profile_record[index].run |= run;
1800 account_profile_record (&profile_record[index], subpass);
1803 /* Output profile consistency. */
1805 void
1806 dump_profile_report (void)
1808 g->get_passes ()->dump_profile_report ();
1811 void
1812 pass_manager::dump_profile_report () const
1814 int i, j;
1815 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1816 gcov_type last_time = 0, last_size = 0;
1817 double rel_time_change, rel_size_change;
1818 int last_reported = 0;
1820 if (!profile_record)
1821 return;
1822 fprintf (stderr, "\nProfile consistency report:\n\n");
1823 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1824 fprintf (stderr, " |freq count |freq count |size time\n");
1826 for (i = 0; i < passes_by_id_size; i++)
1827 for (j = 0 ; j < 2; j++)
1828 if (profile_record[i].run)
1830 if (last_time)
1831 rel_time_change = (profile_record[i].time[j]
1832 - (double)last_time) * 100 / (double)last_time;
1833 else
1834 rel_time_change = 0;
1835 if (last_size)
1836 rel_size_change = (profile_record[i].size[j]
1837 - (double)last_size) * 100 / (double)last_size;
1838 else
1839 rel_size_change = 0;
1841 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1842 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1843 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1844 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1845 || rel_time_change || rel_size_change)
1847 last_reported = i;
1848 fprintf (stderr, "%-20s %s",
1849 passes_by_id [i]->name,
1850 j ? "(after TODO)" : " ");
1851 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1852 fprintf (stderr, "| %+5i",
1853 profile_record[i].num_mismatched_freq_in[j]
1854 - last_freq_in);
1855 else
1856 fprintf (stderr, "| ");
1857 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1858 fprintf (stderr, " %+5i",
1859 profile_record[i].num_mismatched_count_in[j]
1860 - last_count_in);
1861 else
1862 fprintf (stderr, " ");
1863 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1864 fprintf (stderr, "| %+5i",
1865 profile_record[i].num_mismatched_freq_out[j]
1866 - last_freq_out);
1867 else
1868 fprintf (stderr, "| ");
1869 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1870 fprintf (stderr, " %+5i",
1871 profile_record[i].num_mismatched_count_out[j]
1872 - last_count_out);
1873 else
1874 fprintf (stderr, " ");
1876 /* Size/time units change across gimple and RTL. */
1877 if (i == pass_expand_1->static_pass_number)
1878 fprintf (stderr, "|----------");
1879 else
1881 if (rel_size_change)
1882 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1883 else
1884 fprintf (stderr, "| ");
1885 if (rel_time_change)
1886 fprintf (stderr, " %+8.4f%%", rel_time_change);
1888 fprintf (stderr, "\n");
1889 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1890 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1891 last_count_in = profile_record[i].num_mismatched_count_in[j];
1892 last_count_out = profile_record[i].num_mismatched_count_out[j];
1894 else if (j && last_reported != i)
1896 last_reported = i;
1897 fprintf (stderr, "%-20s ------------| | |\n",
1898 passes_by_id [i]->name);
1900 last_time = profile_record[i].time[j];
1901 last_size = profile_record[i].size[j];
1905 /* Perform all TODO actions that ought to be done on each function. */
1907 static void
1908 execute_function_todo (function *fn, void *data)
1910 bool from_ipa_pass = (cfun == NULL);
1911 unsigned int flags = (size_t)data;
1912 flags &= ~fn->last_verified;
1913 if (!flags)
1914 return;
1916 push_cfun (fn);
1918 /* Always cleanup the CFG before trying to update SSA. */
1919 if (flags & TODO_cleanup_cfg)
1921 cleanup_tree_cfg ();
1923 /* When cleanup_tree_cfg merges consecutive blocks, it may
1924 perform some simplistic propagation when removing single
1925 valued PHI nodes. This propagation may, in turn, cause the
1926 SSA form to become out-of-date (see PR 22037). So, even
1927 if the parent pass had not scheduled an SSA update, we may
1928 still need to do one. */
1929 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1930 flags |= TODO_update_ssa;
1933 if (flags & TODO_update_ssa_any)
1935 unsigned update_flags = flags & TODO_update_ssa_any;
1936 update_ssa (update_flags);
1939 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1940 compute_may_aliases ();
1942 if (optimize && (flags & TODO_update_address_taken))
1943 execute_update_addresses_taken ();
1945 if (flags & TODO_remove_unused_locals)
1946 remove_unused_locals ();
1948 if (flags & TODO_rebuild_frequencies)
1949 rebuild_frequencies ();
1951 if (flags & TODO_rebuild_cgraph_edges)
1952 cgraph_edge::rebuild_edges ();
1954 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1955 /* If we've seen errors do not bother running any verifiers. */
1956 if (flag_checking && !seen_error ())
1958 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1959 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1961 if (flags & TODO_verify_il)
1963 if (cfun->curr_properties & PROP_trees)
1965 if (cfun->curr_properties & PROP_cfg)
1966 /* IPA passes leave stmts to be fixed up, so make sure to
1967 not verify stmts really throw. */
1968 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1969 else
1970 verify_gimple_in_seq (gimple_body (cfun->decl));
1972 if (cfun->curr_properties & PROP_ssa)
1973 /* IPA passes leave stmts to be fixed up, so make sure to
1974 not verify SSA operands whose verifier will choke on that. */
1975 verify_ssa (true, !from_ipa_pass);
1976 /* IPA passes leave basic-blocks unsplit, so make sure to
1977 not trip on that. */
1978 if ((cfun->curr_properties & PROP_cfg)
1979 && !from_ipa_pass)
1980 verify_flow_info ();
1981 if (current_loops
1982 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1983 verify_loop_closed_ssa (false);
1984 if (cfun->curr_properties & PROP_rtl)
1985 verify_rtl_sharing ();
1988 /* Make sure verifiers don't change dominator state. */
1989 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1990 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1993 fn->last_verified = flags & TODO_verify_all;
1995 pop_cfun ();
1997 /* For IPA passes make sure to release dominator info, it can be
1998 computed by non-verifying TODOs. */
1999 if (from_ipa_pass)
2001 free_dominance_info (fn, CDI_DOMINATORS);
2002 free_dominance_info (fn, CDI_POST_DOMINATORS);
2006 /* Perform all TODO actions. */
2007 static void
2008 execute_todo (unsigned int flags)
2010 if (flag_checking
2011 && cfun
2012 && need_ssa_update_p (cfun))
2013 gcc_assert (flags & TODO_update_ssa_any);
2015 timevar_push (TV_TODO);
2017 statistics_fini_pass ();
2019 if (flags)
2020 do_per_function (execute_function_todo, (void *)(size_t) flags);
2022 /* At this point we should not have any unreachable code in the
2023 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
2024 if (cfun && cfun->gimple_df)
2025 flush_ssaname_freelist ();
2027 /* Always remove functions just as before inlining: IPA passes might be
2028 interested to see bodies of extern inline functions that are not inlined
2029 to analyze side effects. The full removal is done just at the end
2030 of IPA pass queue. */
2031 if (flags & TODO_remove_functions)
2033 gcc_assert (!cfun);
2034 symtab->remove_unreachable_nodes (dump_file);
2037 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2039 gcc_assert (!cfun);
2040 symtab_node::dump_table (dump_file);
2041 /* Flush the file. If verification fails, we won't be able to
2042 close the file before aborting. */
2043 fflush (dump_file);
2046 /* Now that the dumping has been done, we can get rid of the optional
2047 df problems. */
2048 if (flags & TODO_df_finish)
2049 df_finish_pass ((flags & TODO_df_verify) != 0);
2051 timevar_pop (TV_TODO);
2054 /* Verify invariants that should hold between passes. This is a place
2055 to put simple sanity checks. */
2057 static void
2058 verify_interpass_invariants (void)
2060 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2063 /* Clear the last verified flag. */
2065 static void
2066 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2068 fn->last_verified = 0;
2071 /* Helper function. Verify that the properties has been turn into the
2072 properties expected by the pass. */
2074 static void
2075 verify_curr_properties (function *fn, void *data)
2077 unsigned int props = (size_t)data;
2078 gcc_assert ((fn->curr_properties & props) == props);
2081 /* Release dump file name if set. */
2083 static void
2084 release_dump_file_name (void)
2086 if (dump_file_name)
2088 free (CONST_CAST (char *, dump_file_name));
2089 dump_file_name = NULL;
2093 /* Initialize pass dump file. */
2094 /* This is non-static so that the plugins can use it. */
2096 bool
2097 pass_init_dump_file (opt_pass *pass)
2099 /* If a dump file name is present, open it if enabled. */
2100 if (pass->static_pass_number != -1)
2102 timevar_push (TV_DUMP);
2103 gcc::dump_manager *dumps = g->get_dumps ();
2104 bool initializing_dump =
2105 !dumps->dump_initialized_p (pass->static_pass_number);
2106 release_dump_file_name ();
2107 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2108 dumps->dump_start (pass->static_pass_number, &dump_flags);
2109 if (dump_file && current_function_decl)
2110 dump_function_header (dump_file, current_function_decl, dump_flags);
2111 if (initializing_dump
2112 && dump_file && (dump_flags & TDF_GRAPH)
2113 && cfun && (cfun->curr_properties & PROP_cfg))
2115 clean_graph_dump_file (dump_file_name);
2116 struct dump_file_info *dfi
2117 = dumps->get_dump_file_info (pass->static_pass_number);
2118 dfi->graph_dump_initialized = true;
2120 timevar_pop (TV_DUMP);
2121 return initializing_dump;
2123 else
2124 return false;
2127 /* Flush PASS dump file. */
2128 /* This is non-static so that plugins can use it. */
2130 void
2131 pass_fini_dump_file (opt_pass *pass)
2133 timevar_push (TV_DUMP);
2135 /* Flush and close dump file. */
2136 release_dump_file_name ();
2138 g->get_dumps ()->dump_finish (pass->static_pass_number);
2139 timevar_pop (TV_DUMP);
2142 /* After executing the pass, apply expected changes to the function
2143 properties. */
2145 static void
2146 update_properties_after_pass (function *fn, void *data)
2148 opt_pass *pass = (opt_pass *) data;
2149 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2150 & ~pass->properties_destroyed;
2153 /* Execute summary generation for all of the passes in IPA_PASS. */
2155 void
2156 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2158 while (ipa_pass)
2160 opt_pass *pass = ipa_pass;
2162 /* Execute all of the IPA_PASSes in the list. */
2163 if (ipa_pass->type == IPA_PASS
2164 && pass->gate (cfun)
2165 && ipa_pass->generate_summary)
2167 pass_init_dump_file (pass);
2169 /* If a timevar is present, start it. */
2170 if (pass->tv_id)
2171 timevar_push (pass->tv_id);
2173 current_pass = pass;
2174 ipa_pass->generate_summary ();
2176 /* Stop timevar. */
2177 if (pass->tv_id)
2178 timevar_pop (pass->tv_id);
2180 pass_fini_dump_file (pass);
2182 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2186 /* Execute IPA_PASS function transform on NODE. */
2188 static void
2189 execute_one_ipa_transform_pass (struct cgraph_node *node,
2190 ipa_opt_pass_d *ipa_pass)
2192 opt_pass *pass = ipa_pass;
2193 unsigned int todo_after = 0;
2195 current_pass = pass;
2196 if (!ipa_pass->function_transform)
2197 return;
2199 /* Note that the folders should only create gimple expressions.
2200 This is a hack until the new folder is ready. */
2201 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2203 pass_init_dump_file (pass);
2205 /* Run pre-pass verification. */
2206 execute_todo (ipa_pass->function_transform_todo_flags_start);
2208 /* If a timevar is present, start it. */
2209 if (pass->tv_id != TV_NONE)
2210 timevar_push (pass->tv_id);
2212 /* Do it! */
2213 todo_after = ipa_pass->function_transform (node);
2215 /* Stop timevar. */
2216 if (pass->tv_id != TV_NONE)
2217 timevar_pop (pass->tv_id);
2219 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2220 check_profile_consistency (pass->static_pass_number, 0, true);
2222 /* Run post-pass cleanup and verification. */
2223 execute_todo (todo_after);
2224 verify_interpass_invariants ();
2225 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2226 check_profile_consistency (pass->static_pass_number, 1, true);
2228 if (dump_file)
2229 do_per_function (execute_function_dump, pass);
2230 pass_fini_dump_file (pass);
2232 current_pass = NULL;
2233 redirect_edge_var_map_empty ();
2235 /* Signal this is a suitable GC collection point. */
2236 if (!(todo_after & TODO_do_not_ggc_collect))
2237 ggc_collect ();
2240 /* For the current function, execute all ipa transforms. */
2242 void
2243 execute_all_ipa_transforms (void)
2245 struct cgraph_node *node;
2246 if (!cfun)
2247 return;
2248 node = cgraph_node::get (current_function_decl);
2250 if (node->ipa_transforms_to_apply.exists ())
2252 unsigned int i;
2254 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2255 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2256 node->ipa_transforms_to_apply.release ();
2260 /* Check if PASS is explicitly disabled or enabled and return
2261 the gate status. FUNC is the function to be processed, and
2262 GATE_STATUS is the gate status determined by pass manager by
2263 default. */
2265 static bool
2266 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2268 bool explicitly_enabled = false;
2269 bool explicitly_disabled = false;
2271 explicitly_enabled
2272 = is_pass_explicitly_enabled_or_disabled (pass, func,
2273 enabled_pass_uid_range_tab);
2274 explicitly_disabled
2275 = is_pass_explicitly_enabled_or_disabled (pass, func,
2276 disabled_pass_uid_range_tab);
2278 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2280 return gate_status;
2284 /* Execute PASS. */
2286 bool
2287 execute_one_pass (opt_pass *pass)
2289 unsigned int todo_after = 0;
2291 bool gate_status;
2293 /* IPA passes are executed on whole program, so cfun should be NULL.
2294 Other passes need function context set. */
2295 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2296 gcc_assert (!cfun && !current_function_decl);
2297 else
2298 gcc_assert (cfun && current_function_decl);
2300 current_pass = pass;
2302 /* Check whether gate check should be avoided.
2303 User controls the value of the gate through the parameter "gate_status". */
2304 gate_status = pass->gate (cfun);
2305 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2307 /* Override gate with plugin. */
2308 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2310 if (!gate_status)
2312 /* Run so passes selectively disabling themselves on a given function
2313 are not miscounted. */
2314 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2316 check_profile_consistency (pass->static_pass_number, 0, false);
2317 check_profile_consistency (pass->static_pass_number, 1, false);
2319 current_pass = NULL;
2320 return false;
2323 /* Pass execution event trigger: useful to identify passes being
2324 executed. */
2325 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2327 if (!quiet_flag && !cfun)
2328 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2330 /* Note that the folders should only create gimple expressions.
2331 This is a hack until the new folder is ready. */
2332 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2334 pass_init_dump_file (pass);
2336 /* Run pre-pass verification. */
2337 execute_todo (pass->todo_flags_start);
2339 if (flag_checking)
2340 do_per_function (verify_curr_properties,
2341 (void *)(size_t)pass->properties_required);
2343 /* If a timevar is present, start it. */
2344 if (pass->tv_id != TV_NONE)
2345 timevar_push (pass->tv_id);
2347 /* Do it! */
2348 todo_after = pass->execute (cfun);
2350 if (todo_after & TODO_discard_function)
2352 pass_fini_dump_file (pass);
2354 gcc_assert (cfun);
2355 /* As cgraph_node::release_body expects release dominators info,
2356 we have to release it. */
2357 if (dom_info_available_p (CDI_DOMINATORS))
2358 free_dominance_info (CDI_DOMINATORS);
2360 if (dom_info_available_p (CDI_POST_DOMINATORS))
2361 free_dominance_info (CDI_POST_DOMINATORS);
2363 tree fn = cfun->decl;
2364 pop_cfun ();
2365 gcc_assert (!cfun);
2366 cgraph_node::get (fn)->release_body ();
2368 current_pass = NULL;
2369 redirect_edge_var_map_empty ();
2371 ggc_collect ();
2373 return true;
2376 do_per_function (clear_last_verified, NULL);
2378 /* Stop timevar. */
2379 if (pass->tv_id != TV_NONE)
2380 timevar_pop (pass->tv_id);
2382 do_per_function (update_properties_after_pass, pass);
2384 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2385 check_profile_consistency (pass->static_pass_number, 0, true);
2387 /* Run post-pass cleanup and verification. */
2388 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2389 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2390 check_profile_consistency (pass->static_pass_number, 1, true);
2392 verify_interpass_invariants ();
2393 if (pass->type == IPA_PASS
2394 && ((ipa_opt_pass_d *)pass)->function_transform)
2396 struct cgraph_node *node;
2397 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2398 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2400 else if (dump_file)
2401 do_per_function (execute_function_dump, pass);
2403 if (!current_function_decl)
2404 symtab->process_new_functions ();
2406 pass_fini_dump_file (pass);
2408 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2409 gcc_assert (!(cfun->curr_properties & PROP_trees)
2410 || pass->type != RTL_PASS);
2412 current_pass = NULL;
2413 redirect_edge_var_map_empty ();
2415 /* Signal this is a suitable GC collection point. */
2416 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2417 ggc_collect ();
2419 return true;
2422 static void
2423 execute_pass_list_1 (opt_pass *pass)
2427 gcc_assert (pass->type == GIMPLE_PASS
2428 || pass->type == RTL_PASS);
2430 if (cfun == NULL)
2431 return;
2432 if (execute_one_pass (pass) && pass->sub)
2433 execute_pass_list_1 (pass->sub);
2434 pass = pass->next;
2436 while (pass);
2439 void
2440 execute_pass_list (function *fn, opt_pass *pass)
2442 gcc_assert (fn == cfun);
2443 execute_pass_list_1 (pass);
2444 if (cfun && fn->cfg)
2446 free_dominance_info (CDI_DOMINATORS);
2447 free_dominance_info (CDI_POST_DOMINATORS);
2451 /* Write out all LTO data. */
2452 static void
2453 write_lto (void)
2455 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2456 lto_output ();
2457 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2458 timevar_push (TV_IPA_LTO_DECL_OUT);
2459 produce_asm_for_decls ();
2460 timevar_pop (TV_IPA_LTO_DECL_OUT);
2463 /* Same as execute_pass_list but assume that subpasses of IPA passes
2464 are local passes. If SET is not NULL, write out summaries of only
2465 those node in SET. */
2467 static void
2468 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2470 while (pass)
2472 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2473 gcc_assert (!current_function_decl);
2474 gcc_assert (!cfun);
2475 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2476 if (pass->type == IPA_PASS
2477 && ipa_pass->write_summary
2478 && pass->gate (cfun))
2480 /* If a timevar is present, start it. */
2481 if (pass->tv_id)
2482 timevar_push (pass->tv_id);
2484 pass_init_dump_file (pass);
2486 current_pass = pass;
2487 ipa_pass->write_summary ();
2489 pass_fini_dump_file (pass);
2491 /* If a timevar is present, start it. */
2492 if (pass->tv_id)
2493 timevar_pop (pass->tv_id);
2496 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2497 ipa_write_summaries_2 (pass->sub, state);
2499 pass = pass->next;
2503 /* Helper function of ipa_write_summaries. Creates and destroys the
2504 decl state and calls ipa_write_summaries_2 for all passes that have
2505 summaries. SET is the set of nodes to be written. */
2507 static void
2508 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2510 pass_manager *passes = g->get_passes ();
2511 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2512 state->symtab_node_encoder = encoder;
2514 lto_output_init_mode_table ();
2515 lto_push_out_decl_state (state);
2517 gcc_assert (!flag_wpa);
2518 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2520 write_lto ();
2522 gcc_assert (lto_get_out_decl_state () == state);
2523 lto_pop_out_decl_state ();
2524 lto_delete_out_decl_state (state);
2527 /* Write out summaries for all the nodes in the callgraph. */
2529 void
2530 ipa_write_summaries (void)
2532 lto_symtab_encoder_t encoder;
2533 int i, order_pos;
2534 varpool_node *vnode;
2535 struct cgraph_node *node;
2536 struct cgraph_node **order;
2538 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2539 return;
2541 select_what_to_stream ();
2543 encoder = lto_symtab_encoder_new (false);
2545 /* Create the callgraph set in the same order used in
2546 cgraph_expand_all_functions. This mostly facilitates debugging,
2547 since it causes the gimple file to be processed in the same order
2548 as the source code. */
2549 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2550 order_pos = ipa_reverse_postorder (order);
2551 gcc_assert (order_pos == symtab->cgraph_count);
2553 for (i = order_pos - 1; i >= 0; i--)
2555 struct cgraph_node *node = order[i];
2557 if (node->has_gimple_body_p ())
2559 /* When streaming out references to statements as part of some IPA
2560 pass summary, the statements need to have uids assigned and the
2561 following does that for all the IPA passes here. Naturally, this
2562 ordering then matches the one IPA-passes get in their stmt_fixup
2563 hooks. */
2565 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2566 renumber_gimple_stmt_uids ();
2567 pop_cfun ();
2569 if (node->definition && node->need_lto_streaming)
2570 lto_set_symtab_encoder_in_partition (encoder, node);
2573 FOR_EACH_DEFINED_FUNCTION (node)
2574 if (node->alias && node->need_lto_streaming)
2575 lto_set_symtab_encoder_in_partition (encoder, node);
2576 FOR_EACH_DEFINED_VARIABLE (vnode)
2577 if (vnode->need_lto_streaming)
2578 lto_set_symtab_encoder_in_partition (encoder, vnode);
2580 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2582 free (order);
2585 /* Same as execute_pass_list but assume that subpasses of IPA passes
2586 are local passes. If SET is not NULL, write out optimization summaries of
2587 only those node in SET. */
2589 static void
2590 ipa_write_optimization_summaries_1 (opt_pass *pass,
2591 struct lto_out_decl_state *state)
2593 while (pass)
2595 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2596 gcc_assert (!current_function_decl);
2597 gcc_assert (!cfun);
2598 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2599 if (pass->type == IPA_PASS
2600 && ipa_pass->write_optimization_summary
2601 && pass->gate (cfun))
2603 /* If a timevar is present, start it. */
2604 if (pass->tv_id)
2605 timevar_push (pass->tv_id);
2607 pass_init_dump_file (pass);
2609 current_pass = pass;
2610 ipa_pass->write_optimization_summary ();
2612 pass_fini_dump_file (pass);
2614 /* If a timevar is present, start it. */
2615 if (pass->tv_id)
2616 timevar_pop (pass->tv_id);
2619 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2620 ipa_write_optimization_summaries_1 (pass->sub, state);
2622 pass = pass->next;
2626 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2627 NULL, write out all summaries of all nodes. */
2629 void
2630 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2632 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2633 lto_symtab_encoder_iterator lsei;
2634 state->symtab_node_encoder = encoder;
2636 lto_output_init_mode_table ();
2637 lto_push_out_decl_state (state);
2638 for (lsei = lsei_start_function_in_partition (encoder);
2639 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2641 struct cgraph_node *node = lsei_cgraph_node (lsei);
2642 /* When streaming out references to statements as part of some IPA
2643 pass summary, the statements need to have uids assigned.
2645 For functions newly born at WPA stage we need to initialize
2646 the uids here. */
2647 if (node->definition
2648 && gimple_has_body_p (node->decl))
2650 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2651 renumber_gimple_stmt_uids ();
2652 pop_cfun ();
2656 gcc_assert (flag_wpa);
2657 pass_manager *passes = g->get_passes ();
2658 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2660 write_lto ();
2662 gcc_assert (lto_get_out_decl_state () == state);
2663 lto_pop_out_decl_state ();
2664 lto_delete_out_decl_state (state);
2667 /* Same as execute_pass_list but assume that subpasses of IPA passes
2668 are local passes. */
2670 static void
2671 ipa_read_summaries_1 (opt_pass *pass)
2673 while (pass)
2675 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2677 gcc_assert (!current_function_decl);
2678 gcc_assert (!cfun);
2679 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2681 if (pass->gate (cfun))
2683 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2685 /* If a timevar is present, start it. */
2686 if (pass->tv_id)
2687 timevar_push (pass->tv_id);
2689 pass_init_dump_file (pass);
2691 current_pass = pass;
2692 ipa_pass->read_summary ();
2694 pass_fini_dump_file (pass);
2696 /* Stop timevar. */
2697 if (pass->tv_id)
2698 timevar_pop (pass->tv_id);
2701 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2702 ipa_read_summaries_1 (pass->sub);
2704 pass = pass->next;
2709 /* Read all the summaries for all_regular_ipa_passes. */
2711 void
2712 ipa_read_summaries (void)
2714 pass_manager *passes = g->get_passes ();
2715 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2718 /* Same as execute_pass_list but assume that subpasses of IPA passes
2719 are local passes. */
2721 static void
2722 ipa_read_optimization_summaries_1 (opt_pass *pass)
2724 while (pass)
2726 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2728 gcc_assert (!current_function_decl);
2729 gcc_assert (!cfun);
2730 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2732 if (pass->gate (cfun))
2734 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2736 /* If a timevar is present, start it. */
2737 if (pass->tv_id)
2738 timevar_push (pass->tv_id);
2740 pass_init_dump_file (pass);
2742 current_pass = pass;
2743 ipa_pass->read_optimization_summary ();
2745 pass_fini_dump_file (pass);
2747 /* Stop timevar. */
2748 if (pass->tv_id)
2749 timevar_pop (pass->tv_id);
2752 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2753 ipa_read_optimization_summaries_1 (pass->sub);
2755 pass = pass->next;
2759 /* Read all the summaries for all_regular_ipa_passes. */
2761 void
2762 ipa_read_optimization_summaries (void)
2764 pass_manager *passes = g->get_passes ();
2765 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2768 /* Same as execute_pass_list but assume that subpasses of IPA passes
2769 are local passes. */
2770 void
2771 execute_ipa_pass_list (opt_pass *pass)
2775 gcc_assert (!current_function_decl);
2776 gcc_assert (!cfun);
2777 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2778 if (execute_one_pass (pass) && pass->sub)
2780 if (pass->sub->type == GIMPLE_PASS)
2782 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2783 do_per_function_toporder ((void (*)(function *, void *))
2784 execute_pass_list,
2785 pass->sub);
2786 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2788 else if (pass->sub->type == SIMPLE_IPA_PASS
2789 || pass->sub->type == IPA_PASS)
2790 execute_ipa_pass_list (pass->sub);
2791 else
2792 gcc_unreachable ();
2794 gcc_assert (!current_function_decl);
2795 symtab->process_new_functions ();
2796 pass = pass->next;
2798 while (pass);
2801 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2803 static void
2804 execute_ipa_stmt_fixups (opt_pass *pass,
2805 struct cgraph_node *node, gimple **stmts)
2807 while (pass)
2809 /* Execute all of the IPA_PASSes in the list. */
2810 if (pass->type == IPA_PASS
2811 && pass->gate (cfun))
2813 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2815 if (ipa_pass->stmt_fixup)
2817 pass_init_dump_file (pass);
2818 /* If a timevar is present, start it. */
2819 if (pass->tv_id)
2820 timevar_push (pass->tv_id);
2822 current_pass = pass;
2823 ipa_pass->stmt_fixup (node, stmts);
2825 /* Stop timevar. */
2826 if (pass->tv_id)
2827 timevar_pop (pass->tv_id);
2828 pass_fini_dump_file (pass);
2830 if (pass->sub)
2831 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2833 pass = pass->next;
2837 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2839 void
2840 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2842 pass_manager *passes = g->get_passes ();
2843 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2847 extern void debug_properties (unsigned int);
2848 extern void dump_properties (FILE *, unsigned int);
2850 DEBUG_FUNCTION void
2851 dump_properties (FILE *dump, unsigned int props)
2853 fprintf (dump, "Properties:\n");
2854 if (props & PROP_gimple_any)
2855 fprintf (dump, "PROP_gimple_any\n");
2856 if (props & PROP_gimple_lcf)
2857 fprintf (dump, "PROP_gimple_lcf\n");
2858 if (props & PROP_gimple_leh)
2859 fprintf (dump, "PROP_gimple_leh\n");
2860 if (props & PROP_cfg)
2861 fprintf (dump, "PROP_cfg\n");
2862 if (props & PROP_ssa)
2863 fprintf (dump, "PROP_ssa\n");
2864 if (props & PROP_no_crit_edges)
2865 fprintf (dump, "PROP_no_crit_edges\n");
2866 if (props & PROP_rtl)
2867 fprintf (dump, "PROP_rtl\n");
2868 if (props & PROP_gimple_lomp)
2869 fprintf (dump, "PROP_gimple_lomp\n");
2870 if (props & PROP_gimple_lcx)
2871 fprintf (dump, "PROP_gimple_lcx\n");
2872 if (props & PROP_gimple_lvec)
2873 fprintf (dump, "PROP_gimple_lvec\n");
2874 if (props & PROP_cfglayout)
2875 fprintf (dump, "PROP_cfglayout\n");
2878 DEBUG_FUNCTION void
2879 debug_properties (unsigned int props)
2881 dump_properties (stderr, props);
2884 /* Called by local passes to see if function is called by already processed nodes.
2885 Because we process nodes in topological order, this means that function is
2886 in recursive cycle or we introduced new direct calls. */
2887 bool
2888 function_called_by_processed_nodes_p (void)
2890 struct cgraph_edge *e;
2891 for (e = cgraph_node::get (current_function_decl)->callers;
2893 e = e->next_caller)
2895 if (e->caller->decl == current_function_decl)
2896 continue;
2897 if (!e->caller->has_gimple_body_p ())
2898 continue;
2899 if (TREE_ASM_WRITTEN (e->caller->decl))
2900 continue;
2901 if (!e->caller->process && !e->caller->global.inlined_to)
2902 break;
2904 if (dump_file && e)
2906 fprintf (dump_file, "Already processed call to:\n");
2907 e->caller->dump (dump_file);
2909 return e != NULL;
2912 #include "gt-passes.h"