* gfortran.dg/debug/pr46756.f: Remove XFAIL for AIX.
[official-gcc.git] / gcc / passes.c
blobf87dcf4bb184fe54a2a3cd96c03388ea717d9e62
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This is the top level of cc1/c++.
21 It parses command args, opens files, invokes the various passes
22 in the proper order, and counts the time used by each.
23 Error messages and low-level interface to malloc also handled here. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "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 bool
85 opt_pass::gate (function *)
87 return true;
90 unsigned int
91 opt_pass::execute (function *)
93 return 0;
96 opt_pass::opt_pass (const pass_data &data, context *ctxt)
97 : pass_data (data),
98 sub (NULL),
99 next (NULL),
100 static_pass_number (0),
101 m_ctxt (ctxt)
106 void
107 pass_manager::execute_early_local_passes ()
109 execute_pass_list (cfun, pass_build_ssa_passes_1->sub);
110 if (flag_check_pointer_bounds)
111 execute_pass_list (cfun, pass_chkp_instrumentation_passes_1->sub);
112 execute_pass_list (cfun, pass_local_optimization_passes_1->sub);
115 unsigned int
116 pass_manager::execute_pass_mode_switching ()
118 return pass_mode_switching_1->execute (cfun);
122 /* Call from anywhere to find out what pass this is. Useful for
123 printing out debugging information deep inside an service
124 routine. */
125 void
126 print_current_pass (FILE *file)
128 if (current_pass)
129 fprintf (file, "current pass = %s (%d)\n",
130 current_pass->name, current_pass->static_pass_number);
131 else
132 fprintf (file, "no current pass.\n");
136 /* Call from the debugger to get the current pass name. */
137 DEBUG_FUNCTION void
138 debug_pass (void)
140 print_current_pass (stderr);
145 /* Global variables used to communicate with passes. */
146 bool in_gimple_form;
147 bool first_pass_instance;
150 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
151 and TYPE_DECL nodes.
153 This does nothing for local (non-static) variables, unless the
154 variable is a register variable with DECL_ASSEMBLER_NAME set. In
155 that case, or if the variable is not an automatic, it sets up the
156 RTL and outputs any assembler code (label definition, storage
157 allocation and initialization).
159 DECL is the declaration. TOP_LEVEL is nonzero
160 if this declaration is not within a function. */
162 void
163 rest_of_decl_compilation (tree decl,
164 int top_level,
165 int at_end)
167 bool finalize = true;
169 /* We deferred calling assemble_alias so that we could collect
170 other attributes such as visibility. Emit the alias now. */
171 if (!in_lto_p)
173 tree alias;
174 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
175 if (alias)
177 alias = TREE_VALUE (TREE_VALUE (alias));
178 alias = get_identifier (TREE_STRING_POINTER (alias));
179 /* A quirk of the initial implementation of aliases required that the
180 user add "extern" to all of them. Which is silly, but now
181 historical. Do note that the symbol is in fact locally defined. */
182 DECL_EXTERNAL (decl) = 0;
183 TREE_STATIC (decl) = 1;
184 assemble_alias (decl, alias);
185 finalize = false;
189 /* Can't defer this, because it needs to happen before any
190 later function definitions are processed. */
191 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
192 make_decl_rtl (decl);
194 /* Forward declarations for nested functions are not "external",
195 but we need to treat them as if they were. */
196 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
197 || TREE_CODE (decl) == FUNCTION_DECL)
199 timevar_push (TV_VARCONST);
201 /* Don't output anything when a tentative file-scope definition
202 is seen. But at end of compilation, do output code for them.
204 We do output all variables and rely on
205 callgraph code to defer them except for forward declarations
206 (see gcc.c-torture/compile/920624-1.c) */
207 if ((at_end
208 || !DECL_DEFER_OUTPUT (decl)
209 || DECL_INITIAL (decl))
210 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
211 && !DECL_EXTERNAL (decl))
213 /* When reading LTO unit, we also read varpool, so do not
214 rebuild it. */
215 if (in_lto_p && !at_end)
217 else if (finalize && TREE_CODE (decl) != FUNCTION_DECL)
218 varpool_node::finalize_decl (decl);
221 #ifdef ASM_FINISH_DECLARE_OBJECT
222 if (decl == last_assemble_variable_decl)
224 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
225 top_level, at_end);
227 #endif
229 /* Now that we have activated any function-specific attributes
230 that might affect function decl, particularly align, relayout it. */
231 if (TREE_CODE (decl) == FUNCTION_DECL)
232 targetm.target_option.relayout_function (decl);
234 timevar_pop (TV_VARCONST);
236 else if (TREE_CODE (decl) == TYPE_DECL
237 /* Like in rest_of_type_compilation, avoid confusing the debug
238 information machinery when there are errors. */
239 && !seen_error ())
241 timevar_push (TV_SYMOUT);
242 debug_hooks->type_decl (decl, !top_level);
243 timevar_pop (TV_SYMOUT);
246 /* Let cgraph know about the existence of variables. */
247 if (in_lto_p && !at_end)
249 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
250 && TREE_STATIC (decl))
251 varpool_node::get_create (decl);
253 /* Generate early debug for global variables. Any local variables will
254 be handled by either handling reachable functions from
255 finalize_compilation_unit (and by consequence, locally scoped
256 symbols), or by rest_of_type_compilation below.
258 Also, pick up function prototypes, which will be mostly ignored
259 by the different early_global_decl() hooks, but will at least be
260 used by Go's hijack of the debug_hooks to implement
261 -fdump-go-spec. */
262 if (!in_lto_p
263 && (TREE_CODE (decl) != FUNCTION_DECL
264 /* This will pick up function prototypes with no bodies,
265 which are not visible in finalize_compilation_unit()
266 while iterating with FOR_EACH_*_FUNCTION through the
267 symbol table. */
268 || !DECL_SAVED_TREE (decl))
270 /* We need to check both decl_function_context and
271 current_function_decl here to make sure local extern
272 declarations end up with the correct context.
274 For local extern declarations, decl_function_context is
275 empty, but current_function_decl is set to the function where
276 the extern was declared . Without the check for
277 !current_function_decl below, the local extern ends up
278 incorrectly with a top-level context.
280 For example:
282 namespace S
288 int i = 42;
290 extern int i; // Local extern declaration.
291 return i;
297 && !decl_function_context (decl)
298 && !current_function_decl
299 && DECL_SOURCE_LOCATION (decl) != BUILTINS_LOCATION
300 && (!decl_type_context (decl)
301 /* If we created a varpool node for the decl make sure to
302 call early_global_decl. Otherwise we miss changes
303 introduced by member definitions like
304 struct A { static int staticdatamember; };
305 int A::staticdatamember;
306 and thus have incomplete early debug and late debug
307 called from varpool node removal fails to handle it
308 properly. */
309 || (finalize
310 && TREE_CODE (decl) == VAR_DECL
311 && TREE_STATIC (decl) && !DECL_EXTERNAL (decl)))
312 /* Avoid confusing the debug information machinery when there are
313 errors. */
314 && !seen_error ())
315 (*debug_hooks->early_global_decl) (decl);
318 /* Called after finishing a record, union or enumeral type. */
320 void
321 rest_of_type_compilation (tree type, int toplev)
323 /* Avoid confusing the debug information machinery when there are
324 errors. */
325 if (seen_error ())
326 return;
328 timevar_push (TV_SYMOUT);
329 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
330 timevar_pop (TV_SYMOUT);
335 void
336 pass_manager::
337 finish_optimization_passes (void)
339 int i;
340 struct dump_file_info *dfi;
341 char *name;
342 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
344 timevar_push (TV_DUMP);
345 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
347 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
348 end_branch_prob ();
349 dumps->dump_finish (pass_profile_1->static_pass_number);
352 if (optimize > 0)
354 dumps->dump_start (pass_profile_1->static_pass_number, NULL);
355 print_combine_total_stats ();
356 dumps->dump_finish (pass_profile_1->static_pass_number);
359 /* Do whatever is necessary to finish printing the graphs. */
360 for (i = TDI_end; (dfi = dumps->get_dump_file_info (i)) != NULL; ++i)
361 if (dumps->dump_initialized_p (i)
362 && (dfi->pflags & TDF_GRAPH) != 0
363 && (name = dumps->get_dump_file_name (i)) != NULL)
365 finish_graph_dump_file (name);
366 free (name);
369 timevar_pop (TV_DUMP);
372 static unsigned int
373 execute_build_ssa_passes (void)
375 /* Once this pass (and its sub-passes) are complete, all functions
376 will be in SSA form. Technically this state change is happening
377 a tad early, since the sub-passes have not yet run, but since
378 none of the sub-passes are IPA passes and do not create new
379 functions, this is ok. We're setting this value for the benefit
380 of IPA passes that follow. */
381 if (symtab->state < IPA_SSA)
382 symtab->state = IPA_SSA;
383 return 0;
386 namespace {
388 const pass_data pass_data_build_ssa_passes =
390 SIMPLE_IPA_PASS, /* type */
391 "build_ssa_passes", /* name */
392 OPTGROUP_NONE, /* optinfo_flags */
393 TV_EARLY_LOCAL, /* tv_id */
394 0, /* properties_required */
395 0, /* properties_provided */
396 0, /* properties_destroyed */
397 0, /* todo_flags_start */
398 /* todo_flags_finish is executed before subpases. For this reason
399 it makes no sense to remove unreachable functions here. */
400 0, /* todo_flags_finish */
403 class pass_build_ssa_passes : public simple_ipa_opt_pass
405 public:
406 pass_build_ssa_passes (gcc::context *ctxt)
407 : simple_ipa_opt_pass (pass_data_build_ssa_passes, ctxt)
410 /* opt_pass methods: */
411 virtual bool gate (function *)
413 /* Don't bother doing anything if the program has errors. */
414 return (!seen_error () && !in_lto_p);
417 virtual unsigned int execute (function *)
419 return execute_build_ssa_passes ();
422 }; // class pass_build_ssa_passes
424 const pass_data pass_data_chkp_instrumentation_passes =
426 SIMPLE_IPA_PASS, /* type */
427 "chkp_passes", /* name */
428 OPTGROUP_NONE, /* optinfo_flags */
429 TV_NONE, /* tv_id */
430 0, /* properties_required */
431 0, /* properties_provided */
432 0, /* properties_destroyed */
433 0, /* todo_flags_start */
434 0, /* todo_flags_finish */
437 class pass_chkp_instrumentation_passes : public simple_ipa_opt_pass
439 public:
440 pass_chkp_instrumentation_passes (gcc::context *ctxt)
441 : simple_ipa_opt_pass (pass_data_chkp_instrumentation_passes, ctxt)
444 /* opt_pass methods: */
445 virtual bool gate (function *)
447 /* Don't bother doing anything if the program has errors. */
448 return (flag_check_pointer_bounds
449 && !seen_error () && !in_lto_p);
452 }; // class pass_chkp_instrumentation_passes
454 const pass_data pass_data_local_optimization_passes =
456 SIMPLE_IPA_PASS, /* type */
457 "opt_local_passes", /* name */
458 OPTGROUP_NONE, /* optinfo_flags */
459 TV_NONE, /* tv_id */
460 0, /* properties_required */
461 0, /* properties_provided */
462 0, /* properties_destroyed */
463 0, /* todo_flags_start */
464 0, /* todo_flags_finish */
467 class pass_local_optimization_passes : public simple_ipa_opt_pass
469 public:
470 pass_local_optimization_passes (gcc::context *ctxt)
471 : simple_ipa_opt_pass (pass_data_local_optimization_passes, ctxt)
474 /* opt_pass methods: */
475 virtual bool gate (function *)
477 /* Don't bother doing anything if the program has errors. */
478 return (!seen_error () && !in_lto_p);
481 }; // class pass_local_optimization_passes
483 } // anon namespace
485 simple_ipa_opt_pass *
486 make_pass_build_ssa_passes (gcc::context *ctxt)
488 return new pass_build_ssa_passes (ctxt);
491 simple_ipa_opt_pass *
492 make_pass_chkp_instrumentation_passes (gcc::context *ctxt)
494 return new pass_chkp_instrumentation_passes (ctxt);
497 simple_ipa_opt_pass *
498 make_pass_local_optimization_passes (gcc::context *ctxt)
500 return new pass_local_optimization_passes (ctxt);
503 namespace {
505 const pass_data pass_data_all_early_optimizations =
507 GIMPLE_PASS, /* type */
508 "early_optimizations", /* name */
509 OPTGROUP_NONE, /* optinfo_flags */
510 TV_NONE, /* tv_id */
511 0, /* properties_required */
512 0, /* properties_provided */
513 0, /* properties_destroyed */
514 0, /* todo_flags_start */
515 0, /* todo_flags_finish */
518 class pass_all_early_optimizations : public gimple_opt_pass
520 public:
521 pass_all_early_optimizations (gcc::context *ctxt)
522 : gimple_opt_pass (pass_data_all_early_optimizations, ctxt)
525 /* opt_pass methods: */
526 virtual bool gate (function *)
528 return (optimize >= 1
529 /* Don't bother doing anything if the program has errors. */
530 && !seen_error ());
533 }; // class pass_all_early_optimizations
535 } // anon namespace
537 static gimple_opt_pass *
538 make_pass_all_early_optimizations (gcc::context *ctxt)
540 return new pass_all_early_optimizations (ctxt);
543 namespace {
545 const pass_data pass_data_all_optimizations =
547 GIMPLE_PASS, /* type */
548 "*all_optimizations", /* name */
549 OPTGROUP_NONE, /* optinfo_flags */
550 TV_OPTIMIZE, /* tv_id */
551 0, /* properties_required */
552 0, /* properties_provided */
553 0, /* properties_destroyed */
554 0, /* todo_flags_start */
555 0, /* todo_flags_finish */
558 class pass_all_optimizations : public gimple_opt_pass
560 public:
561 pass_all_optimizations (gcc::context *ctxt)
562 : gimple_opt_pass (pass_data_all_optimizations, ctxt)
565 /* opt_pass methods: */
566 virtual bool gate (function *) { return optimize >= 1 && !optimize_debug; }
568 }; // class pass_all_optimizations
570 } // anon namespace
572 static gimple_opt_pass *
573 make_pass_all_optimizations (gcc::context *ctxt)
575 return new pass_all_optimizations (ctxt);
578 namespace {
580 const pass_data pass_data_all_optimizations_g =
582 GIMPLE_PASS, /* type */
583 "*all_optimizations_g", /* name */
584 OPTGROUP_NONE, /* optinfo_flags */
585 TV_OPTIMIZE, /* tv_id */
586 0, /* properties_required */
587 0, /* properties_provided */
588 0, /* properties_destroyed */
589 0, /* todo_flags_start */
590 0, /* todo_flags_finish */
593 class pass_all_optimizations_g : public gimple_opt_pass
595 public:
596 pass_all_optimizations_g (gcc::context *ctxt)
597 : gimple_opt_pass (pass_data_all_optimizations_g, ctxt)
600 /* opt_pass methods: */
601 virtual bool gate (function *) { return optimize >= 1 && optimize_debug; }
603 }; // class pass_all_optimizations_g
605 } // anon namespace
607 static gimple_opt_pass *
608 make_pass_all_optimizations_g (gcc::context *ctxt)
610 return new pass_all_optimizations_g (ctxt);
613 namespace {
615 const pass_data pass_data_rest_of_compilation =
617 RTL_PASS, /* type */
618 "*rest_of_compilation", /* name */
619 OPTGROUP_NONE, /* optinfo_flags */
620 TV_REST_OF_COMPILATION, /* tv_id */
621 PROP_rtl, /* properties_required */
622 0, /* properties_provided */
623 0, /* properties_destroyed */
624 0, /* todo_flags_start */
625 0, /* todo_flags_finish */
628 class pass_rest_of_compilation : public rtl_opt_pass
630 public:
631 pass_rest_of_compilation (gcc::context *ctxt)
632 : rtl_opt_pass (pass_data_rest_of_compilation, ctxt)
635 /* opt_pass methods: */
636 virtual bool gate (function *)
638 /* Early return if there were errors. We can run afoul of our
639 consistency checks, and there's not really much point in fixing them. */
640 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
643 }; // class pass_rest_of_compilation
645 } // anon namespace
647 static rtl_opt_pass *
648 make_pass_rest_of_compilation (gcc::context *ctxt)
650 return new pass_rest_of_compilation (ctxt);
653 namespace {
655 const pass_data pass_data_postreload =
657 RTL_PASS, /* type */
658 "*all-postreload", /* name */
659 OPTGROUP_NONE, /* optinfo_flags */
660 TV_POSTRELOAD, /* tv_id */
661 PROP_rtl, /* properties_required */
662 0, /* properties_provided */
663 0, /* properties_destroyed */
664 0, /* todo_flags_start */
665 0, /* todo_flags_finish */
668 class pass_postreload : public rtl_opt_pass
670 public:
671 pass_postreload (gcc::context *ctxt)
672 : rtl_opt_pass (pass_data_postreload, ctxt)
675 /* opt_pass methods: */
676 virtual bool gate (function *) { return reload_completed; }
678 }; // class pass_postreload
680 } // anon namespace
682 static rtl_opt_pass *
683 make_pass_postreload (gcc::context *ctxt)
685 return new pass_postreload (ctxt);
688 namespace {
690 const pass_data pass_data_late_compilation =
692 RTL_PASS, /* type */
693 "*all-late_compilation", /* name */
694 OPTGROUP_NONE, /* optinfo_flags */
695 TV_LATE_COMPILATION, /* tv_id */
696 PROP_rtl, /* properties_required */
697 0, /* properties_provided */
698 0, /* properties_destroyed */
699 0, /* todo_flags_start */
700 0, /* todo_flags_finish */
703 class pass_late_compilation : public rtl_opt_pass
705 public:
706 pass_late_compilation (gcc::context *ctxt)
707 : rtl_opt_pass (pass_data_late_compilation, ctxt)
710 /* opt_pass methods: */
711 virtual bool gate (function *)
713 return reload_completed || targetm.no_register_allocation;
716 }; // class pass_late_compilation
718 } // anon namespace
720 static rtl_opt_pass *
721 make_pass_late_compilation (gcc::context *ctxt)
723 return new pass_late_compilation (ctxt);
728 /* Set the static pass number of pass PASS to ID and record that
729 in the mapping from static pass number to pass. */
731 void
732 pass_manager::
733 set_pass_for_id (int id, opt_pass *pass)
735 pass->static_pass_number = id;
736 if (passes_by_id_size <= id)
738 passes_by_id = XRESIZEVEC (opt_pass *, passes_by_id, id + 1);
739 memset (passes_by_id + passes_by_id_size, 0,
740 (id + 1 - passes_by_id_size) * sizeof (void *));
741 passes_by_id_size = id + 1;
743 passes_by_id[id] = pass;
746 /* Return the pass with the static pass number ID. */
748 opt_pass *
749 pass_manager::get_pass_for_id (int id) const
751 if (id >= passes_by_id_size)
752 return NULL;
753 return passes_by_id[id];
756 /* Iterate over the pass tree allocating dump file numbers. We want
757 to do this depth first, and independent of whether the pass is
758 enabled or not. */
760 void
761 register_one_dump_file (opt_pass *pass)
763 g->get_passes ()->register_one_dump_file (pass);
766 void
767 pass_manager::register_one_dump_file (opt_pass *pass)
769 char *dot_name, *flag_name, *glob_name;
770 const char *name, *full_name, *prefix;
771 char num[10];
772 int flags, id;
773 int optgroup_flags = OPTGROUP_NONE;
774 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
776 /* See below in next_pass_1. */
777 num[0] = '\0';
778 if (pass->static_pass_number != -1)
779 sprintf (num, "%d", ((int) pass->static_pass_number < 0
780 ? 1 : pass->static_pass_number));
782 /* The name is both used to identify the pass for the purposes of plugins,
783 and to specify dump file name and option.
784 The latter two might want something short which is not quite unique; for
785 that reason, we may have a disambiguating prefix, followed by a space
786 to mark the start of the following dump file name / option string. */
787 name = strchr (pass->name, ' ');
788 name = name ? name + 1 : pass->name;
789 dot_name = concat (".", name, num, NULL);
790 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
792 prefix = "ipa-";
793 flags = TDF_IPA;
794 optgroup_flags |= OPTGROUP_IPA;
796 else if (pass->type == GIMPLE_PASS)
798 prefix = "tree-";
799 flags = TDF_TREE;
801 else
803 prefix = "rtl-";
804 flags = TDF_RTL;
807 flag_name = concat (prefix, name, num, NULL);
808 glob_name = concat (prefix, name, NULL);
809 optgroup_flags |= pass->optinfo_flags;
810 /* For any passes that do not have an optgroup set, and which are not
811 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
812 any dump messages are emitted properly under -fopt-info(-optall). */
813 if (optgroup_flags == OPTGROUP_NONE)
814 optgroup_flags = OPTGROUP_OTHER;
815 id = dumps->dump_register (dot_name, flag_name, glob_name, flags,
816 optgroup_flags,
817 true);
818 set_pass_for_id (id, pass);
819 full_name = concat (prefix, pass->name, num, NULL);
820 register_pass_name (pass, full_name);
821 free (CONST_CAST (char *, full_name));
824 /* Register the dump files for the pass_manager starting at PASS. */
826 void
827 pass_manager::register_dump_files (opt_pass *pass)
831 if (pass->name && pass->name[0] != '*')
832 register_one_dump_file (pass);
834 if (pass->sub)
835 register_dump_files (pass->sub);
837 pass = pass->next;
839 while (pass);
842 static hash_map<nofree_string_hash, opt_pass *> *name_to_pass_map;
844 /* Register PASS with NAME. */
846 static void
847 register_pass_name (opt_pass *pass, const char *name)
849 if (!name_to_pass_map)
850 name_to_pass_map = new hash_map<nofree_string_hash, opt_pass *> (256);
852 if (name_to_pass_map->get (name))
853 return; /* Ignore plugin passes. */
855 const char *unique_name = xstrdup (name);
856 name_to_pass_map->put (unique_name, pass);
859 /* Map from pass id to canonicalized pass name. */
861 typedef const char *char_ptr;
862 static vec<char_ptr> pass_tab = vNULL;
864 /* Callback function for traversing NAME_TO_PASS_MAP. */
866 bool
867 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
869 gcc_assert (pass->static_pass_number > 0);
870 gcc_assert (pass_tab.exists ());
872 pass_tab[pass->static_pass_number] = name;
874 return 1;
877 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
878 table for dumping purpose. */
880 static void
881 create_pass_tab (void)
883 if (!flag_dump_passes)
884 return;
886 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
887 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
890 static bool override_gate_status (opt_pass *, tree, bool);
892 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
893 is turned on or not. */
895 static void
896 dump_one_pass (opt_pass *pass, int pass_indent)
898 int indent = 3 * pass_indent;
899 const char *pn;
900 bool is_on, is_really_on;
902 is_on = pass->gate (cfun);
903 is_really_on = override_gate_status (pass, current_function_decl, is_on);
905 if (pass->static_pass_number <= 0)
906 pn = pass->name;
907 else
908 pn = pass_tab[pass->static_pass_number];
910 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
911 (15 - indent < 0 ? 0 : 15 - indent), " ",
912 is_on ? " ON" : " OFF",
913 ((!is_on) == (!is_really_on) ? ""
914 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
917 /* Dump pass list PASS with indentation INDENT. */
919 static void
920 dump_pass_list (opt_pass *pass, int indent)
924 dump_one_pass (pass, indent);
925 if (pass->sub)
926 dump_pass_list (pass->sub, indent + 1);
927 pass = pass->next;
929 while (pass);
932 /* Dump all optimization passes. */
934 void
935 dump_passes (void)
937 g->get_passes ()->dump_passes ();
940 void
941 pass_manager::dump_passes () const
943 push_dummy_function (true);
945 create_pass_tab ();
947 dump_pass_list (all_lowering_passes, 1);
948 dump_pass_list (all_small_ipa_passes, 1);
949 dump_pass_list (all_regular_ipa_passes, 1);
950 dump_pass_list (all_late_ipa_passes, 1);
951 dump_pass_list (all_passes, 1);
953 pop_dummy_function ();
956 /* Returns the pass with NAME. */
958 static opt_pass *
959 get_pass_by_name (const char *name)
961 opt_pass **p = name_to_pass_map->get (name);
962 if (p)
963 return *p;
965 return NULL;
969 /* Range [start, last]. */
971 struct uid_range
973 unsigned int start;
974 unsigned int last;
975 const char *assem_name;
976 struct uid_range *next;
979 typedef struct uid_range *uid_range_p;
982 static vec<uid_range_p>
983 enabled_pass_uid_range_tab = vNULL;
984 static vec<uid_range_p>
985 disabled_pass_uid_range_tab = vNULL;
988 /* Parse option string for -fdisable- and -fenable-
989 The syntax of the options:
991 -fenable-<pass_name>
992 -fdisable-<pass_name>
994 -fenable-<pass_name>=s1:e1,s2:e2,...
995 -fdisable-<pass_name>=s1:e1,s2:e2,...
998 static void
999 enable_disable_pass (const char *arg, bool is_enable)
1001 opt_pass *pass;
1002 char *range_str, *phase_name;
1003 char *argstr = xstrdup (arg);
1004 vec<uid_range_p> *tab = 0;
1006 range_str = strchr (argstr,'=');
1007 if (range_str)
1009 *range_str = '\0';
1010 range_str++;
1013 phase_name = argstr;
1014 if (!*phase_name)
1016 if (is_enable)
1017 error ("unrecognized option -fenable");
1018 else
1019 error ("unrecognized option -fdisable");
1020 free (argstr);
1021 return;
1023 pass = get_pass_by_name (phase_name);
1024 if (!pass || pass->static_pass_number == -1)
1026 if (is_enable)
1027 error ("unknown pass %s specified in -fenable", phase_name);
1028 else
1029 error ("unknown pass %s specified in -fdisable", phase_name);
1030 free (argstr);
1031 return;
1034 if (is_enable)
1035 tab = &enabled_pass_uid_range_tab;
1036 else
1037 tab = &disabled_pass_uid_range_tab;
1039 if ((unsigned) pass->static_pass_number >= tab->length ())
1040 tab->safe_grow_cleared (pass->static_pass_number + 1);
1042 if (!range_str)
1044 uid_range_p slot;
1045 uid_range_p new_range = XCNEW (struct uid_range);
1047 new_range->start = 0;
1048 new_range->last = (unsigned)-1;
1050 slot = (*tab)[pass->static_pass_number];
1051 new_range->next = slot;
1052 (*tab)[pass->static_pass_number] = new_range;
1053 if (is_enable)
1054 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1055 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1056 else
1057 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1058 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1060 else
1062 char *next_range = NULL;
1063 char *one_range = range_str;
1064 char *end_val = NULL;
1068 uid_range_p slot;
1069 uid_range_p new_range;
1070 char *invalid = NULL;
1071 long start;
1072 char *func_name = NULL;
1074 next_range = strchr (one_range, ',');
1075 if (next_range)
1077 *next_range = '\0';
1078 next_range++;
1081 end_val = strchr (one_range, ':');
1082 if (end_val)
1084 *end_val = '\0';
1085 end_val++;
1087 start = strtol (one_range, &invalid, 10);
1088 if (*invalid || start < 0)
1090 if (end_val || (one_range[0] >= '0'
1091 && one_range[0] <= '9'))
1093 error ("Invalid range %s in option %s",
1094 one_range,
1095 is_enable ? "-fenable" : "-fdisable");
1096 free (argstr);
1097 return;
1099 func_name = one_range;
1101 if (!end_val)
1103 new_range = XCNEW (struct uid_range);
1104 if (!func_name)
1106 new_range->start = (unsigned) start;
1107 new_range->last = (unsigned) start;
1109 else
1111 new_range->start = (unsigned) -1;
1112 new_range->last = (unsigned) -1;
1113 new_range->assem_name = xstrdup (func_name);
1116 else
1118 long last = strtol (end_val, &invalid, 10);
1119 if (*invalid || last < start)
1121 error ("Invalid range %s in option %s",
1122 end_val,
1123 is_enable ? "-fenable" : "-fdisable");
1124 free (argstr);
1125 return;
1127 new_range = XCNEW (struct uid_range);
1128 new_range->start = (unsigned) start;
1129 new_range->last = (unsigned) last;
1132 slot = (*tab)[pass->static_pass_number];
1133 new_range->next = slot;
1134 (*tab)[pass->static_pass_number] = new_range;
1135 if (is_enable)
1137 if (new_range->assem_name)
1138 inform (UNKNOWN_LOCATION,
1139 "enable pass %s for function %s",
1140 phase_name, new_range->assem_name);
1141 else
1142 inform (UNKNOWN_LOCATION,
1143 "enable pass %s for functions in the range of [%u, %u]",
1144 phase_name, new_range->start, new_range->last);
1146 else
1148 if (new_range->assem_name)
1149 inform (UNKNOWN_LOCATION,
1150 "disable pass %s for function %s",
1151 phase_name, new_range->assem_name);
1152 else
1153 inform (UNKNOWN_LOCATION,
1154 "disable pass %s for functions in the range of [%u, %u]",
1155 phase_name, new_range->start, new_range->last);
1158 one_range = next_range;
1159 } while (next_range);
1162 free (argstr);
1165 /* Enable pass specified by ARG. */
1167 void
1168 enable_pass (const char *arg)
1170 enable_disable_pass (arg, true);
1173 /* Disable pass specified by ARG. */
1175 void
1176 disable_pass (const char *arg)
1178 enable_disable_pass (arg, false);
1181 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1183 static bool
1184 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1185 tree func,
1186 vec<uid_range_p> tab)
1188 uid_range_p slot, range;
1189 int cgraph_uid;
1190 const char *aname = NULL;
1192 if (!tab.exists ()
1193 || (unsigned) pass->static_pass_number >= tab.length ()
1194 || pass->static_pass_number == -1)
1195 return false;
1197 slot = tab[pass->static_pass_number];
1198 if (!slot)
1199 return false;
1201 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1202 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1203 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1205 range = slot;
1206 while (range)
1208 if ((unsigned) cgraph_uid >= range->start
1209 && (unsigned) cgraph_uid <= range->last)
1210 return true;
1211 if (range->assem_name && aname
1212 && !strcmp (range->assem_name, aname))
1213 return true;
1214 range = range->next;
1217 return false;
1221 /* Update static_pass_number for passes (and the flag
1222 TODO_mark_first_instance).
1224 Passes are constructed with static_pass_number preinitialized to 0
1226 This field is used in two different ways: initially as instance numbers
1227 of their kind, and then as ids within the entire pass manager.
1229 Within pass_manager::pass_manager:
1231 * In add_pass_instance(), as called by next_pass_1 in
1232 NEXT_PASS in init_optimization_passes
1234 * When the initial instance of a pass within a pass manager is seen,
1235 it is flagged, and its static_pass_number is set to -1
1237 * On subsequent times that it is seen, the static pass number
1238 is decremented each time, so that if there are e.g. 4 dups,
1239 they have static_pass_number -4, 2, 3, 4 respectively (note
1240 how the initial one is negative and gives the count); these
1241 can be thought of as instance numbers of the specific pass
1243 * Within the register_dump_files () traversal, set_pass_for_id()
1244 is called on each pass, using these instance numbers to create
1245 dumpfile switches, and then overwriting them with a pass id,
1246 which are global to the whole pass manager (based on
1247 (TDI_end + current value of extra_dump_files_in_use) ) */
1249 static void
1250 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1251 opt_pass *initial_pass)
1253 /* Are we dealing with the first pass of its kind, or a clone? */
1254 if (new_pass != initial_pass)
1256 /* We're dealing with a clone. */
1257 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1259 /* Indicate to register_dump_files that this pass has duplicates,
1260 and so it should rename the dump file. The first instance will
1261 be -1, and be number of duplicates = -static_pass_number - 1.
1262 Subsequent instances will be > 0 and just the duplicate number. */
1263 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1265 initial_pass->static_pass_number -= 1;
1266 new_pass->static_pass_number = -initial_pass->static_pass_number;
1269 else
1271 /* We're dealing with the first pass of its kind. */
1272 new_pass->todo_flags_start |= TODO_mark_first_instance;
1273 new_pass->static_pass_number = -1;
1275 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1279 /* Add a pass to the pass list. Duplicate the pass if it's already
1280 in the list. */
1282 static opt_pass **
1283 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1285 /* Every pass should have a name so that plugins can refer to them. */
1286 gcc_assert (pass->name != NULL);
1288 add_pass_instance (pass, false, initial_pass);
1289 *list = pass;
1291 return &(*list)->next;
1294 /* List node for an inserted pass instance. We need to keep track of all
1295 the newly-added pass instances (with 'added_pass_nodes' defined below)
1296 so that we can register their dump files after pass-positioning is finished.
1297 Registering dumping files needs to be post-processed or the
1298 static_pass_number of the opt_pass object would be modified and mess up
1299 the dump file names of future pass instances to be added. */
1301 struct pass_list_node
1303 opt_pass *pass;
1304 struct pass_list_node *next;
1307 static struct pass_list_node *added_pass_nodes = NULL;
1308 static struct pass_list_node *prev_added_pass_node;
1310 /* Insert the pass at the proper position. Return true if the pass
1311 is successfully added.
1313 NEW_PASS_INFO - new pass to be inserted
1314 PASS_LIST - root of the pass list to insert the new pass to */
1316 static bool
1317 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1319 opt_pass *pass = *pass_list, *prev_pass = NULL;
1320 bool success = false;
1322 for ( ; pass; prev_pass = pass, pass = pass->next)
1324 /* Check if the current pass is of the same type as the new pass and
1325 matches the name and the instance number of the reference pass. */
1326 if (pass->type == new_pass_info->pass->type
1327 && pass->name
1328 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1329 && ((new_pass_info->ref_pass_instance_number == 0)
1330 || (new_pass_info->ref_pass_instance_number ==
1331 pass->static_pass_number)
1332 || (new_pass_info->ref_pass_instance_number == 1
1333 && pass->todo_flags_start & TODO_mark_first_instance)))
1335 opt_pass *new_pass;
1336 struct pass_list_node *new_pass_node;
1338 if (new_pass_info->ref_pass_instance_number == 0)
1340 new_pass = new_pass_info->pass->clone ();
1341 add_pass_instance (new_pass, true, new_pass_info->pass);
1343 else
1345 new_pass = new_pass_info->pass;
1346 add_pass_instance (new_pass, true, new_pass);
1349 /* Insert the new pass instance based on the positioning op. */
1350 switch (new_pass_info->pos_op)
1352 case PASS_POS_INSERT_AFTER:
1353 new_pass->next = pass->next;
1354 pass->next = new_pass;
1356 /* Skip newly inserted pass to avoid repeated
1357 insertions in the case where the new pass and the
1358 existing one have the same name. */
1359 pass = new_pass;
1360 break;
1361 case PASS_POS_INSERT_BEFORE:
1362 new_pass->next = pass;
1363 if (prev_pass)
1364 prev_pass->next = new_pass;
1365 else
1366 *pass_list = new_pass;
1367 break;
1368 case PASS_POS_REPLACE:
1369 new_pass->next = pass->next;
1370 if (prev_pass)
1371 prev_pass->next = new_pass;
1372 else
1373 *pass_list = new_pass;
1374 new_pass->sub = pass->sub;
1375 new_pass->tv_id = pass->tv_id;
1376 pass = new_pass;
1377 break;
1378 default:
1379 error ("invalid pass positioning operation");
1380 return false;
1383 /* Save the newly added pass (instance) in the added_pass_nodes
1384 list so that we can register its dump file later. Note that
1385 we cannot register the dump file now because doing so will modify
1386 the static_pass_number of the opt_pass object and therefore
1387 mess up the dump file name of future instances. */
1388 new_pass_node = XCNEW (struct pass_list_node);
1389 new_pass_node->pass = new_pass;
1390 if (!added_pass_nodes)
1391 added_pass_nodes = new_pass_node;
1392 else
1393 prev_added_pass_node->next = new_pass_node;
1394 prev_added_pass_node = new_pass_node;
1396 success = true;
1399 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1400 success = true;
1403 return success;
1406 /* Hooks a new pass into the pass lists.
1408 PASS_INFO - pass information that specifies the opt_pass object,
1409 reference pass, instance number, and how to position
1410 the pass */
1412 void
1413 register_pass (struct register_pass_info *pass_info)
1415 g->get_passes ()->register_pass (pass_info);
1418 void
1419 register_pass (opt_pass* pass, pass_positioning_ops pos,
1420 const char* ref_pass_name, int ref_pass_inst_number)
1422 register_pass_info i;
1423 i.pass = pass;
1424 i.reference_pass_name = ref_pass_name;
1425 i.ref_pass_instance_number = ref_pass_inst_number;
1426 i.pos_op = pos;
1428 g->get_passes ()->register_pass (&i);
1431 void
1432 pass_manager::register_pass (struct register_pass_info *pass_info)
1434 bool all_instances, success;
1435 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1437 /* The checks below could fail in buggy plugins. Existing GCC
1438 passes should never fail these checks, so we mention plugin in
1439 the messages. */
1440 if (!pass_info->pass)
1441 fatal_error (input_location, "plugin cannot register a missing pass");
1443 if (!pass_info->pass->name)
1444 fatal_error (input_location, "plugin cannot register an unnamed pass");
1446 if (!pass_info->reference_pass_name)
1447 fatal_error
1448 (input_location,
1449 "plugin cannot register pass %qs without reference pass name",
1450 pass_info->pass->name);
1452 /* Try to insert the new pass to the pass lists. We need to check
1453 all five lists as the reference pass could be in one (or all) of
1454 them. */
1455 all_instances = pass_info->ref_pass_instance_number == 0;
1456 success = position_pass (pass_info, &all_lowering_passes);
1457 if (!success || all_instances)
1458 success |= position_pass (pass_info, &all_small_ipa_passes);
1459 if (!success || all_instances)
1460 success |= position_pass (pass_info, &all_regular_ipa_passes);
1461 if (!success || all_instances)
1462 success |= position_pass (pass_info, &all_late_ipa_passes);
1463 if (!success || all_instances)
1464 success |= position_pass (pass_info, &all_passes);
1465 if (!success)
1466 fatal_error
1467 (input_location,
1468 "pass %qs not found but is referenced by new pass %qs",
1469 pass_info->reference_pass_name, pass_info->pass->name);
1471 /* OK, we have successfully inserted the new pass. We need to register
1472 the dump files for the newly added pass and its duplicates (if any).
1473 Because the registration of plugin/backend passes happens after the
1474 command-line options are parsed, the options that specify single
1475 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1476 passes. Therefore we currently can only enable dumping of
1477 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1478 are specified. While doing so, we also delete the pass_list_node
1479 objects created during pass positioning. */
1480 while (added_pass_nodes)
1482 struct pass_list_node *next_node = added_pass_nodes->next;
1483 enum tree_dump_index tdi;
1484 register_one_dump_file (added_pass_nodes->pass);
1485 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1486 || added_pass_nodes->pass->type == IPA_PASS)
1487 tdi = TDI_ipa_all;
1488 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1489 tdi = TDI_tree_all;
1490 else
1491 tdi = TDI_rtl_all;
1492 /* Check if dump-all flag is specified. */
1493 if (dumps->get_dump_file_info (tdi)->pstate)
1494 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1495 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1496 XDELETE (added_pass_nodes);
1497 added_pass_nodes = next_node;
1501 /* Construct the pass tree. The sequencing of passes is driven by
1502 the cgraph routines:
1504 finalize_compilation_unit ()
1505 for each node N in the cgraph
1506 cgraph_analyze_function (N)
1507 cgraph_lower_function (N) -> all_lowering_passes
1509 If we are optimizing, compile is then invoked:
1511 compile ()
1512 ipa_passes () -> all_small_ipa_passes
1513 -> Analysis of all_regular_ipa_passes
1514 * possible LTO streaming at copmilation time *
1515 -> Execution of all_regular_ipa_passes
1516 * possible LTO streaming at link time *
1517 -> all_late_ipa_passes
1518 expand_all_functions ()
1519 for each node N in the cgraph
1520 expand_function (N) -> Transformation of all_regular_ipa_passes
1521 -> all_passes
1524 void *
1525 pass_manager::operator new (size_t sz)
1527 /* Ensure that all fields of the pass manager are zero-initialized. */
1528 return xcalloc (1, sz);
1531 void
1532 pass_manager::operator delete (void *ptr)
1534 free (ptr);
1537 pass_manager::pass_manager (context *ctxt)
1538 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1539 all_regular_ipa_passes (NULL),
1540 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1541 m_ctxt (ctxt)
1543 opt_pass **p;
1545 /* Initialize the pass_lists array. */
1546 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1547 GCC_PASS_LISTS
1548 #undef DEF_PASS_LIST
1550 /* Build the tree of passes. */
1552 #define INSERT_PASSES_AFTER(PASS) \
1553 p = &(PASS);
1555 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1557 opt_pass **p = &(PASS ## _1)->sub;
1559 #define POP_INSERT_PASSES() \
1562 #define NEXT_PASS(PASS, NUM) \
1563 do { \
1564 gcc_assert (NULL == PASS ## _ ## NUM); \
1565 if ((NUM) == 1) \
1566 PASS ## _1 = make_##PASS (m_ctxt); \
1567 else \
1569 gcc_assert (PASS ## _1); \
1570 PASS ## _ ## NUM = PASS ## _1->clone (); \
1572 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1573 } while (0)
1575 #define TERMINATE_PASS_LIST() \
1576 *p = NULL;
1578 #include "pass-instances.def"
1580 #undef INSERT_PASSES_AFTER
1581 #undef PUSH_INSERT_PASSES_WITHIN
1582 #undef POP_INSERT_PASSES
1583 #undef NEXT_PASS
1584 #undef TERMINATE_PASS_LIST
1586 /* Register the passes with the tree dump code. */
1587 register_dump_files (all_lowering_passes);
1588 register_dump_files (all_small_ipa_passes);
1589 register_dump_files (all_regular_ipa_passes);
1590 register_dump_files (all_late_ipa_passes);
1591 register_dump_files (all_passes);
1594 static void
1595 delete_pass_tree (opt_pass *pass)
1597 while (pass)
1599 /* Recurse into child passes. */
1600 delete_pass_tree (pass->sub);
1602 opt_pass *next = pass->next;
1604 /* Delete this pass. */
1605 delete pass;
1607 /* Iterate onto sibling passes. */
1608 pass = next;
1612 pass_manager::~pass_manager ()
1614 XDELETEVEC (passes_by_id);
1616 /* Call delete_pass_tree on each of the pass_lists. */
1617 #define DEF_PASS_LIST(LIST) \
1618 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1619 GCC_PASS_LISTS
1620 #undef DEF_PASS_LIST
1624 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1625 function CALLBACK for every function in the call graph. Otherwise,
1626 call CALLBACK on the current function. */
1628 static void
1629 do_per_function (void (*callback) (function *, void *data), void *data)
1631 if (current_function_decl)
1632 callback (cfun, data);
1633 else
1635 struct cgraph_node *node;
1636 FOR_EACH_DEFINED_FUNCTION (node)
1637 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1638 && (!node->clone_of || node->decl != node->clone_of->decl))
1639 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1643 /* Because inlining might remove no-longer reachable nodes, we need to
1644 keep the array visible to garbage collector to avoid reading collected
1645 out nodes. */
1646 static int nnodes;
1647 static GTY ((length ("nnodes"))) cgraph_node **order;
1649 /* Hook called when NODE is removed and therefore should be
1650 excluded from order vector. DATA is an array of integers.
1651 DATA[0] holds max index it may be accessed by. For cgraph
1652 node DATA[node->uid + 1] holds index of this node in order
1653 vector. */
1654 static void
1655 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1657 int *order_idx = (int *)data;
1659 if (node->uid >= order_idx[0])
1660 return;
1662 int idx = order_idx[node->uid + 1];
1663 if (idx >= 0 && idx < nnodes && order[idx] == node)
1664 order[idx] = NULL;
1667 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1668 function CALLBACK for every function in the call graph. Otherwise,
1669 call CALLBACK on the current function.
1670 This function is global so that plugins can use it. */
1671 void
1672 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1674 int i;
1676 if (current_function_decl)
1677 callback (cfun, data);
1678 else
1680 cgraph_node_hook_list *hook;
1681 int *order_idx;
1682 gcc_assert (!order);
1683 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1685 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1686 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1687 order_idx[0] = symtab->cgraph_max_uid;
1689 nnodes = ipa_reverse_postorder (order);
1690 for (i = nnodes - 1; i >= 0; i--)
1692 order[i]->process = 1;
1693 order_idx[order[i]->uid + 1] = i;
1695 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1696 order_idx);
1697 for (i = nnodes - 1; i >= 0; i--)
1699 /* Function could be inlined and removed as unreachable. */
1700 if (!order[i])
1701 continue;
1703 struct cgraph_node *node = order[i];
1705 /* Allow possibly removed nodes to be garbage collected. */
1706 order[i] = NULL;
1707 node->process = 0;
1708 if (node->has_gimple_body_p ())
1709 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1711 symtab->remove_cgraph_removal_hook (hook);
1713 ggc_free (order);
1714 order = NULL;
1715 nnodes = 0;
1718 /* Helper function to perform function body dump. */
1720 static void
1721 execute_function_dump (function *fn, void *data)
1723 opt_pass *pass = (opt_pass *)data;
1725 if (dump_file)
1727 push_cfun (fn);
1729 if (fn->curr_properties & PROP_trees)
1730 dump_function_to_file (fn->decl, dump_file, dump_flags);
1731 else
1732 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1734 /* Flush the file. If verification fails, we won't be able to
1735 close the file before aborting. */
1736 fflush (dump_file);
1738 if ((fn->curr_properties & PROP_cfg)
1739 && (dump_flags & TDF_GRAPH))
1741 if (!pass->graph_dump_initialized)
1743 clean_graph_dump_file (dump_file_name);
1744 pass->graph_dump_initialized = true;
1746 print_graph_cfg (dump_file_name, fn);
1749 pop_cfun ();
1753 static struct profile_record *profile_record;
1755 /* Do profile consistency book-keeping for the pass with static number INDEX.
1756 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1757 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1758 if we are only book-keeping on passes that may have selectively disabled
1759 themselves on a given function. */
1760 static void
1761 check_profile_consistency (int index, int subpass, bool run)
1763 pass_manager *passes = g->get_passes ();
1764 if (index == -1)
1765 return;
1766 if (!profile_record)
1767 profile_record = XCNEWVEC (struct profile_record,
1768 passes->passes_by_id_size);
1769 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1770 gcc_assert (subpass < 2);
1771 profile_record[index].run |= run;
1772 account_profile_record (&profile_record[index], subpass);
1775 /* Output profile consistency. */
1777 void
1778 dump_profile_report (void)
1780 g->get_passes ()->dump_profile_report ();
1783 void
1784 pass_manager::dump_profile_report () const
1786 int i, j;
1787 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1788 gcov_type last_time = 0, last_size = 0;
1789 double rel_time_change, rel_size_change;
1790 int last_reported = 0;
1792 if (!profile_record)
1793 return;
1794 fprintf (stderr, "\nProfile consistency report:\n\n");
1795 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1796 fprintf (stderr, " |freq count |freq count |size time\n");
1798 for (i = 0; i < passes_by_id_size; i++)
1799 for (j = 0 ; j < 2; j++)
1800 if (profile_record[i].run)
1802 if (last_time)
1803 rel_time_change = (profile_record[i].time[j]
1804 - (double)last_time) * 100 / (double)last_time;
1805 else
1806 rel_time_change = 0;
1807 if (last_size)
1808 rel_size_change = (profile_record[i].size[j]
1809 - (double)last_size) * 100 / (double)last_size;
1810 else
1811 rel_size_change = 0;
1813 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1814 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1815 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1816 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1817 || rel_time_change || rel_size_change)
1819 last_reported = i;
1820 fprintf (stderr, "%-20s %s",
1821 passes_by_id [i]->name,
1822 j ? "(after TODO)" : " ");
1823 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1824 fprintf (stderr, "| %+5i",
1825 profile_record[i].num_mismatched_freq_in[j]
1826 - last_freq_in);
1827 else
1828 fprintf (stderr, "| ");
1829 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1830 fprintf (stderr, " %+5i",
1831 profile_record[i].num_mismatched_count_in[j]
1832 - last_count_in);
1833 else
1834 fprintf (stderr, " ");
1835 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1836 fprintf (stderr, "| %+5i",
1837 profile_record[i].num_mismatched_freq_out[j]
1838 - last_freq_out);
1839 else
1840 fprintf (stderr, "| ");
1841 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1842 fprintf (stderr, " %+5i",
1843 profile_record[i].num_mismatched_count_out[j]
1844 - last_count_out);
1845 else
1846 fprintf (stderr, " ");
1848 /* Size/time units change across gimple and RTL. */
1849 if (i == pass_expand_1->static_pass_number)
1850 fprintf (stderr, "|----------");
1851 else
1853 if (rel_size_change)
1854 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1855 else
1856 fprintf (stderr, "| ");
1857 if (rel_time_change)
1858 fprintf (stderr, " %+8.4f%%", rel_time_change);
1860 fprintf (stderr, "\n");
1861 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1862 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1863 last_count_in = profile_record[i].num_mismatched_count_in[j];
1864 last_count_out = profile_record[i].num_mismatched_count_out[j];
1866 else if (j && last_reported != i)
1868 last_reported = i;
1869 fprintf (stderr, "%-20s ------------| | |\n",
1870 passes_by_id [i]->name);
1872 last_time = profile_record[i].time[j];
1873 last_size = profile_record[i].size[j];
1877 /* Perform all TODO actions that ought to be done on each function. */
1879 static void
1880 execute_function_todo (function *fn, void *data)
1882 bool from_ipa_pass = (cfun == NULL);
1883 unsigned int flags = (size_t)data;
1884 flags &= ~fn->last_verified;
1885 if (!flags)
1886 return;
1888 push_cfun (fn);
1890 /* Always cleanup the CFG before trying to update SSA. */
1891 if (flags & TODO_cleanup_cfg)
1893 cleanup_tree_cfg ();
1895 /* When cleanup_tree_cfg merges consecutive blocks, it may
1896 perform some simplistic propagation when removing single
1897 valued PHI nodes. This propagation may, in turn, cause the
1898 SSA form to become out-of-date (see PR 22037). So, even
1899 if the parent pass had not scheduled an SSA update, we may
1900 still need to do one. */
1901 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1902 flags |= TODO_update_ssa;
1905 if (flags & TODO_update_ssa_any)
1907 unsigned update_flags = flags & TODO_update_ssa_any;
1908 update_ssa (update_flags);
1911 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1912 compute_may_aliases ();
1914 if (optimize && (flags & TODO_update_address_taken))
1915 execute_update_addresses_taken ();
1917 if (flags & TODO_remove_unused_locals)
1918 remove_unused_locals ();
1920 if (flags & TODO_rebuild_frequencies)
1921 rebuild_frequencies ();
1923 if (flags & TODO_rebuild_cgraph_edges)
1924 cgraph_edge::rebuild_edges ();
1926 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == DOM_NONE);
1927 /* If we've seen errors do not bother running any verifiers. */
1928 if (flag_checking && !seen_error ())
1930 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1931 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1933 if (flags & TODO_verify_il)
1935 if (cfun->curr_properties & PROP_trees)
1937 if (cfun->curr_properties & PROP_cfg)
1938 /* IPA passes leave stmts to be fixed up, so make sure to
1939 not verify stmts really throw. */
1940 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1941 else
1942 verify_gimple_in_seq (gimple_body (cfun->decl));
1944 if (cfun->curr_properties & PROP_ssa)
1945 /* IPA passes leave stmts to be fixed up, so make sure to
1946 not verify SSA operands whose verifier will choke on that. */
1947 verify_ssa (true, !from_ipa_pass);
1948 /* IPA passes leave basic-blocks unsplit, so make sure to
1949 not trip on that. */
1950 if ((cfun->curr_properties & PROP_cfg)
1951 && !from_ipa_pass)
1952 verify_flow_info ();
1953 if (current_loops
1954 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1955 verify_loop_closed_ssa (false);
1956 if (cfun->curr_properties & PROP_rtl)
1957 verify_rtl_sharing ();
1960 /* Make sure verifiers don't change dominator state. */
1961 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1962 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1965 fn->last_verified = flags & TODO_verify_all;
1967 pop_cfun ();
1969 /* For IPA passes make sure to release dominator info, it can be
1970 computed by non-verifying TODOs. */
1971 if (from_ipa_pass)
1973 free_dominance_info (fn, CDI_DOMINATORS);
1974 free_dominance_info (fn, CDI_POST_DOMINATORS);
1978 /* Perform all TODO actions. */
1979 static void
1980 execute_todo (unsigned int flags)
1982 if (flag_checking
1983 && cfun
1984 && need_ssa_update_p (cfun))
1985 gcc_assert (flags & TODO_update_ssa_any);
1987 timevar_push (TV_TODO);
1989 /* Inform the pass whether it is the first time it is run. */
1990 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1992 statistics_fini_pass ();
1994 if (flags)
1995 do_per_function (execute_function_todo, (void *)(size_t) flags);
1997 /* At this point we should not have any unreachable code in the
1998 CFG, so it is safe to flush the pending freelist for SSA_NAMES. */
1999 if (cfun && cfun->gimple_df)
2000 flush_ssaname_freelist ();
2002 /* Always remove functions just as before inlining: IPA passes might be
2003 interested to see bodies of extern inline functions that are not inlined
2004 to analyze side effects. The full removal is done just at the end
2005 of IPA pass queue. */
2006 if (flags & TODO_remove_functions)
2008 gcc_assert (!cfun);
2009 symtab->remove_unreachable_nodes (dump_file);
2012 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2014 gcc_assert (!cfun);
2015 symtab_node::dump_table (dump_file);
2016 /* Flush the file. If verification fails, we won't be able to
2017 close the file before aborting. */
2018 fflush (dump_file);
2021 /* Now that the dumping has been done, we can get rid of the optional
2022 df problems. */
2023 if (flags & TODO_df_finish)
2024 df_finish_pass ((flags & TODO_df_verify) != 0);
2026 timevar_pop (TV_TODO);
2029 /* Verify invariants that should hold between passes. This is a place
2030 to put simple sanity checks. */
2032 static void
2033 verify_interpass_invariants (void)
2035 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2038 /* Clear the last verified flag. */
2040 static void
2041 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2043 fn->last_verified = 0;
2046 /* Helper function. Verify that the properties has been turn into the
2047 properties expected by the pass. */
2049 static void
2050 verify_curr_properties (function *fn, void *data)
2052 unsigned int props = (size_t)data;
2053 gcc_assert ((fn->curr_properties & props) == props);
2056 /* Initialize pass dump file. */
2057 /* This is non-static so that the plugins can use it. */
2059 bool
2060 pass_init_dump_file (opt_pass *pass)
2062 /* If a dump file name is present, open it if enabled. */
2063 if (pass->static_pass_number != -1)
2065 timevar_push (TV_DUMP);
2066 gcc::dump_manager *dumps = g->get_dumps ();
2067 bool initializing_dump =
2068 !dumps->dump_initialized_p (pass->static_pass_number);
2069 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2070 dumps->dump_start (pass->static_pass_number, &dump_flags);
2071 if (dump_file && current_function_decl)
2072 dump_function_header (dump_file, current_function_decl, dump_flags);
2073 if (initializing_dump
2074 && dump_file && (dump_flags & TDF_GRAPH)
2075 && cfun && (cfun->curr_properties & PROP_cfg))
2077 clean_graph_dump_file (dump_file_name);
2078 pass->graph_dump_initialized = true;
2080 timevar_pop (TV_DUMP);
2081 return initializing_dump;
2083 else
2084 return false;
2087 /* Flush PASS dump file. */
2088 /* This is non-static so that plugins can use it. */
2090 void
2091 pass_fini_dump_file (opt_pass *pass)
2093 timevar_push (TV_DUMP);
2095 /* Flush and close dump file. */
2096 if (dump_file_name)
2098 free (CONST_CAST (char *, dump_file_name));
2099 dump_file_name = NULL;
2102 g->get_dumps ()->dump_finish (pass->static_pass_number);
2103 timevar_pop (TV_DUMP);
2106 /* After executing the pass, apply expected changes to the function
2107 properties. */
2109 static void
2110 update_properties_after_pass (function *fn, void *data)
2112 opt_pass *pass = (opt_pass *) data;
2113 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2114 & ~pass->properties_destroyed;
2117 /* Execute summary generation for all of the passes in IPA_PASS. */
2119 void
2120 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2122 while (ipa_pass)
2124 opt_pass *pass = ipa_pass;
2126 /* Execute all of the IPA_PASSes in the list. */
2127 if (ipa_pass->type == IPA_PASS
2128 && pass->gate (cfun)
2129 && ipa_pass->generate_summary)
2131 pass_init_dump_file (pass);
2133 /* If a timevar is present, start it. */
2134 if (pass->tv_id)
2135 timevar_push (pass->tv_id);
2137 current_pass = pass;
2138 ipa_pass->generate_summary ();
2140 /* Stop timevar. */
2141 if (pass->tv_id)
2142 timevar_pop (pass->tv_id);
2144 pass_fini_dump_file (pass);
2146 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2150 /* Execute IPA_PASS function transform on NODE. */
2152 static void
2153 execute_one_ipa_transform_pass (struct cgraph_node *node,
2154 ipa_opt_pass_d *ipa_pass)
2156 opt_pass *pass = ipa_pass;
2157 unsigned int todo_after = 0;
2159 current_pass = pass;
2160 if (!ipa_pass->function_transform)
2161 return;
2163 /* Note that the folders should only create gimple expressions.
2164 This is a hack until the new folder is ready. */
2165 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2167 pass_init_dump_file (pass);
2169 /* Run pre-pass verification. */
2170 execute_todo (ipa_pass->function_transform_todo_flags_start);
2172 /* If a timevar is present, start it. */
2173 if (pass->tv_id != TV_NONE)
2174 timevar_push (pass->tv_id);
2176 /* Do it! */
2177 todo_after = ipa_pass->function_transform (node);
2179 /* Stop timevar. */
2180 if (pass->tv_id != TV_NONE)
2181 timevar_pop (pass->tv_id);
2183 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2184 check_profile_consistency (pass->static_pass_number, 0, true);
2186 /* Run post-pass cleanup and verification. */
2187 execute_todo (todo_after);
2188 verify_interpass_invariants ();
2189 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2190 check_profile_consistency (pass->static_pass_number, 1, true);
2192 if (dump_file)
2193 do_per_function (execute_function_dump, NULL);
2194 pass_fini_dump_file (pass);
2196 current_pass = NULL;
2198 /* Signal this is a suitable GC collection point. */
2199 if (!(todo_after & TODO_do_not_ggc_collect))
2200 ggc_collect ();
2203 /* For the current function, execute all ipa transforms. */
2205 void
2206 execute_all_ipa_transforms (void)
2208 struct cgraph_node *node;
2209 if (!cfun)
2210 return;
2211 node = cgraph_node::get (current_function_decl);
2213 if (node->ipa_transforms_to_apply.exists ())
2215 unsigned int i;
2217 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2218 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2219 node->ipa_transforms_to_apply.release ();
2223 /* Check if PASS is explicitly disabled or enabled and return
2224 the gate status. FUNC is the function to be processed, and
2225 GATE_STATUS is the gate status determined by pass manager by
2226 default. */
2228 static bool
2229 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2231 bool explicitly_enabled = false;
2232 bool explicitly_disabled = false;
2234 explicitly_enabled
2235 = is_pass_explicitly_enabled_or_disabled (pass, func,
2236 enabled_pass_uid_range_tab);
2237 explicitly_disabled
2238 = is_pass_explicitly_enabled_or_disabled (pass, func,
2239 disabled_pass_uid_range_tab);
2241 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2243 return gate_status;
2247 /* Execute PASS. */
2249 bool
2250 execute_one_pass (opt_pass *pass)
2252 unsigned int todo_after = 0;
2254 bool gate_status;
2256 /* IPA passes are executed on whole program, so cfun should be NULL.
2257 Other passes need function context set. */
2258 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2259 gcc_assert (!cfun && !current_function_decl);
2260 else
2261 gcc_assert (cfun && current_function_decl);
2263 current_pass = pass;
2265 /* Check whether gate check should be avoided.
2266 User controls the value of the gate through the parameter "gate_status". */
2267 gate_status = pass->gate (cfun);
2268 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2270 /* Override gate with plugin. */
2271 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2273 if (!gate_status)
2275 /* Run so passes selectively disabling themselves on a given function
2276 are not miscounted. */
2277 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2279 check_profile_consistency (pass->static_pass_number, 0, false);
2280 check_profile_consistency (pass->static_pass_number, 1, false);
2282 current_pass = NULL;
2283 return false;
2286 /* Pass execution event trigger: useful to identify passes being
2287 executed. */
2288 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2290 if (!quiet_flag && !cfun)
2291 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2293 /* Note that the folders should only create gimple expressions.
2294 This is a hack until the new folder is ready. */
2295 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2297 pass_init_dump_file (pass);
2299 /* Run pre-pass verification. */
2300 execute_todo (pass->todo_flags_start);
2302 if (flag_checking)
2303 do_per_function (verify_curr_properties,
2304 (void *)(size_t)pass->properties_required);
2306 /* If a timevar is present, start it. */
2307 if (pass->tv_id != TV_NONE)
2308 timevar_push (pass->tv_id);
2310 /* Do it! */
2311 todo_after = pass->execute (cfun);
2312 do_per_function (clear_last_verified, NULL);
2314 /* Stop timevar. */
2315 if (pass->tv_id != TV_NONE)
2316 timevar_pop (pass->tv_id);
2318 do_per_function (update_properties_after_pass, pass);
2320 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2321 check_profile_consistency (pass->static_pass_number, 0, true);
2323 /* Run post-pass cleanup and verification. */
2324 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2325 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2326 check_profile_consistency (pass->static_pass_number, 1, true);
2328 verify_interpass_invariants ();
2329 if (dump_file)
2330 do_per_function (execute_function_dump, pass);
2331 if (pass->type == IPA_PASS)
2333 struct cgraph_node *node;
2334 if (((ipa_opt_pass_d *)pass)->function_transform)
2335 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2336 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2339 if (!current_function_decl)
2340 symtab->process_new_functions ();
2342 pass_fini_dump_file (pass);
2344 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2345 gcc_assert (!(cfun->curr_properties & PROP_trees)
2346 || pass->type != RTL_PASS);
2348 current_pass = NULL;
2350 /* Signal this is a suitable GC collection point. */
2351 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2352 ggc_collect ();
2354 return true;
2357 static void
2358 execute_pass_list_1 (opt_pass *pass)
2362 gcc_assert (pass->type == GIMPLE_PASS
2363 || pass->type == RTL_PASS);
2364 if (execute_one_pass (pass) && pass->sub)
2365 execute_pass_list_1 (pass->sub);
2366 pass = pass->next;
2368 while (pass);
2371 void
2372 execute_pass_list (function *fn, opt_pass *pass)
2374 push_cfun (fn);
2375 execute_pass_list_1 (pass);
2376 if (fn->cfg)
2378 free_dominance_info (CDI_DOMINATORS);
2379 free_dominance_info (CDI_POST_DOMINATORS);
2381 pop_cfun ();
2384 /* Write out all LTO data. */
2385 static void
2386 write_lto (void)
2388 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2389 lto_output ();
2390 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2391 timevar_push (TV_IPA_LTO_DECL_OUT);
2392 produce_asm_for_decls ();
2393 timevar_pop (TV_IPA_LTO_DECL_OUT);
2396 /* Same as execute_pass_list but assume that subpasses of IPA passes
2397 are local passes. If SET is not NULL, write out summaries of only
2398 those node in SET. */
2400 static void
2401 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2403 while (pass)
2405 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2406 gcc_assert (!current_function_decl);
2407 gcc_assert (!cfun);
2408 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2409 if (pass->type == IPA_PASS
2410 && ipa_pass->write_summary
2411 && pass->gate (cfun))
2413 /* If a timevar is present, start it. */
2414 if (pass->tv_id)
2415 timevar_push (pass->tv_id);
2417 pass_init_dump_file (pass);
2419 current_pass = pass;
2420 ipa_pass->write_summary ();
2422 pass_fini_dump_file (pass);
2424 /* If a timevar is present, start it. */
2425 if (pass->tv_id)
2426 timevar_pop (pass->tv_id);
2429 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2430 ipa_write_summaries_2 (pass->sub, state);
2432 pass = pass->next;
2436 /* Helper function of ipa_write_summaries. Creates and destroys the
2437 decl state and calls ipa_write_summaries_2 for all passes that have
2438 summaries. SET is the set of nodes to be written. */
2440 static void
2441 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2443 pass_manager *passes = g->get_passes ();
2444 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2445 state->symtab_node_encoder = encoder;
2447 lto_output_init_mode_table ();
2448 lto_push_out_decl_state (state);
2450 gcc_assert (!flag_wpa);
2451 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2453 write_lto ();
2455 gcc_assert (lto_get_out_decl_state () == state);
2456 lto_pop_out_decl_state ();
2457 lto_delete_out_decl_state (state);
2460 /* Write out summaries for all the nodes in the callgraph. */
2462 void
2463 ipa_write_summaries (void)
2465 lto_symtab_encoder_t encoder;
2466 int i, order_pos;
2467 varpool_node *vnode;
2468 struct cgraph_node *node;
2469 struct cgraph_node **order;
2471 if ((!flag_generate_lto && !flag_generate_offload) || seen_error ())
2472 return;
2474 select_what_to_stream ();
2476 encoder = lto_symtab_encoder_new (false);
2478 /* Create the callgraph set in the same order used in
2479 cgraph_expand_all_functions. This mostly facilitates debugging,
2480 since it causes the gimple file to be processed in the same order
2481 as the source code. */
2482 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2483 order_pos = ipa_reverse_postorder (order);
2484 gcc_assert (order_pos == symtab->cgraph_count);
2486 for (i = order_pos - 1; i >= 0; i--)
2488 struct cgraph_node *node = order[i];
2490 if (node->has_gimple_body_p ())
2492 /* When streaming out references to statements as part of some IPA
2493 pass summary, the statements need to have uids assigned and the
2494 following does that for all the IPA passes here. Naturally, this
2495 ordering then matches the one IPA-passes get in their stmt_fixup
2496 hooks. */
2498 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2499 renumber_gimple_stmt_uids ();
2500 pop_cfun ();
2502 if (node->definition && node->need_lto_streaming)
2503 lto_set_symtab_encoder_in_partition (encoder, node);
2506 FOR_EACH_DEFINED_FUNCTION (node)
2507 if (node->alias && node->need_lto_streaming)
2508 lto_set_symtab_encoder_in_partition (encoder, node);
2509 FOR_EACH_DEFINED_VARIABLE (vnode)
2510 if (vnode->need_lto_streaming)
2511 lto_set_symtab_encoder_in_partition (encoder, vnode);
2513 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2515 free (order);
2518 /* Same as execute_pass_list but assume that subpasses of IPA passes
2519 are local passes. If SET is not NULL, write out optimization summaries of
2520 only those node in SET. */
2522 static void
2523 ipa_write_optimization_summaries_1 (opt_pass *pass,
2524 struct lto_out_decl_state *state)
2526 while (pass)
2528 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2529 gcc_assert (!current_function_decl);
2530 gcc_assert (!cfun);
2531 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2532 if (pass->type == IPA_PASS
2533 && ipa_pass->write_optimization_summary
2534 && pass->gate (cfun))
2536 /* If a timevar is present, start it. */
2537 if (pass->tv_id)
2538 timevar_push (pass->tv_id);
2540 pass_init_dump_file (pass);
2542 current_pass = pass;
2543 ipa_pass->write_optimization_summary ();
2545 pass_fini_dump_file (pass);
2547 /* If a timevar is present, start it. */
2548 if (pass->tv_id)
2549 timevar_pop (pass->tv_id);
2552 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2553 ipa_write_optimization_summaries_1 (pass->sub, state);
2555 pass = pass->next;
2559 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2560 NULL, write out all summaries of all nodes. */
2562 void
2563 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2565 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2566 lto_symtab_encoder_iterator lsei;
2567 state->symtab_node_encoder = encoder;
2569 lto_output_init_mode_table ();
2570 lto_push_out_decl_state (state);
2571 for (lsei = lsei_start_function_in_partition (encoder);
2572 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2574 struct cgraph_node *node = lsei_cgraph_node (lsei);
2575 /* When streaming out references to statements as part of some IPA
2576 pass summary, the statements need to have uids assigned.
2578 For functions newly born at WPA stage we need to initialize
2579 the uids here. */
2580 if (node->definition
2581 && gimple_has_body_p (node->decl))
2583 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2584 renumber_gimple_stmt_uids ();
2585 pop_cfun ();
2589 gcc_assert (flag_wpa);
2590 pass_manager *passes = g->get_passes ();
2591 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2593 write_lto ();
2595 gcc_assert (lto_get_out_decl_state () == state);
2596 lto_pop_out_decl_state ();
2597 lto_delete_out_decl_state (state);
2600 /* Same as execute_pass_list but assume that subpasses of IPA passes
2601 are local passes. */
2603 static void
2604 ipa_read_summaries_1 (opt_pass *pass)
2606 while (pass)
2608 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2610 gcc_assert (!current_function_decl);
2611 gcc_assert (!cfun);
2612 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2614 if (pass->gate (cfun))
2616 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2618 /* If a timevar is present, start it. */
2619 if (pass->tv_id)
2620 timevar_push (pass->tv_id);
2622 pass_init_dump_file (pass);
2624 current_pass = pass;
2625 ipa_pass->read_summary ();
2627 pass_fini_dump_file (pass);
2629 /* Stop timevar. */
2630 if (pass->tv_id)
2631 timevar_pop (pass->tv_id);
2634 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2635 ipa_read_summaries_1 (pass->sub);
2637 pass = pass->next;
2642 /* Read all the summaries for all_regular_ipa_passes. */
2644 void
2645 ipa_read_summaries (void)
2647 pass_manager *passes = g->get_passes ();
2648 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2651 /* Same as execute_pass_list but assume that subpasses of IPA passes
2652 are local passes. */
2654 static void
2655 ipa_read_optimization_summaries_1 (opt_pass *pass)
2657 while (pass)
2659 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2661 gcc_assert (!current_function_decl);
2662 gcc_assert (!cfun);
2663 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2665 if (pass->gate (cfun))
2667 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2669 /* If a timevar is present, start it. */
2670 if (pass->tv_id)
2671 timevar_push (pass->tv_id);
2673 pass_init_dump_file (pass);
2675 current_pass = pass;
2676 ipa_pass->read_optimization_summary ();
2678 pass_fini_dump_file (pass);
2680 /* Stop timevar. */
2681 if (pass->tv_id)
2682 timevar_pop (pass->tv_id);
2685 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2686 ipa_read_optimization_summaries_1 (pass->sub);
2688 pass = pass->next;
2692 /* Read all the summaries for all_regular_ipa_passes. */
2694 void
2695 ipa_read_optimization_summaries (void)
2697 pass_manager *passes = g->get_passes ();
2698 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2701 /* Same as execute_pass_list but assume that subpasses of IPA passes
2702 are local passes. */
2703 void
2704 execute_ipa_pass_list (opt_pass *pass)
2708 gcc_assert (!current_function_decl);
2709 gcc_assert (!cfun);
2710 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2711 if (execute_one_pass (pass) && pass->sub)
2713 if (pass->sub->type == GIMPLE_PASS)
2715 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2716 do_per_function_toporder ((void (*)(function *, void *))
2717 execute_pass_list,
2718 pass->sub);
2719 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2721 else if (pass->sub->type == SIMPLE_IPA_PASS
2722 || pass->sub->type == IPA_PASS)
2723 execute_ipa_pass_list (pass->sub);
2724 else
2725 gcc_unreachable ();
2727 gcc_assert (!current_function_decl);
2728 symtab->process_new_functions ();
2729 pass = pass->next;
2731 while (pass);
2734 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2736 static void
2737 execute_ipa_stmt_fixups (opt_pass *pass,
2738 struct cgraph_node *node, gimple **stmts)
2740 while (pass)
2742 /* Execute all of the IPA_PASSes in the list. */
2743 if (pass->type == IPA_PASS
2744 && pass->gate (cfun))
2746 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2748 if (ipa_pass->stmt_fixup)
2750 pass_init_dump_file (pass);
2751 /* If a timevar is present, start it. */
2752 if (pass->tv_id)
2753 timevar_push (pass->tv_id);
2755 current_pass = pass;
2756 ipa_pass->stmt_fixup (node, stmts);
2758 /* Stop timevar. */
2759 if (pass->tv_id)
2760 timevar_pop (pass->tv_id);
2761 pass_fini_dump_file (pass);
2763 if (pass->sub)
2764 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2766 pass = pass->next;
2770 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2772 void
2773 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple **stmts)
2775 pass_manager *passes = g->get_passes ();
2776 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2780 extern void debug_properties (unsigned int);
2781 extern void dump_properties (FILE *, unsigned int);
2783 DEBUG_FUNCTION void
2784 dump_properties (FILE *dump, unsigned int props)
2786 fprintf (dump, "Properties:\n");
2787 if (props & PROP_gimple_any)
2788 fprintf (dump, "PROP_gimple_any\n");
2789 if (props & PROP_gimple_lcf)
2790 fprintf (dump, "PROP_gimple_lcf\n");
2791 if (props & PROP_gimple_leh)
2792 fprintf (dump, "PROP_gimple_leh\n");
2793 if (props & PROP_cfg)
2794 fprintf (dump, "PROP_cfg\n");
2795 if (props & PROP_ssa)
2796 fprintf (dump, "PROP_ssa\n");
2797 if (props & PROP_no_crit_edges)
2798 fprintf (dump, "PROP_no_crit_edges\n");
2799 if (props & PROP_rtl)
2800 fprintf (dump, "PROP_rtl\n");
2801 if (props & PROP_gimple_lomp)
2802 fprintf (dump, "PROP_gimple_lomp\n");
2803 if (props & PROP_gimple_lcx)
2804 fprintf (dump, "PROP_gimple_lcx\n");
2805 if (props & PROP_gimple_lvec)
2806 fprintf (dump, "PROP_gimple_lvec\n");
2807 if (props & PROP_cfglayout)
2808 fprintf (dump, "PROP_cfglayout\n");
2811 DEBUG_FUNCTION void
2812 debug_properties (unsigned int props)
2814 dump_properties (stderr, props);
2817 /* Called by local passes to see if function is called by already processed nodes.
2818 Because we process nodes in topological order, this means that function is
2819 in recursive cycle or we introduced new direct calls. */
2820 bool
2821 function_called_by_processed_nodes_p (void)
2823 struct cgraph_edge *e;
2824 for (e = cgraph_node::get (current_function_decl)->callers;
2826 e = e->next_caller)
2828 if (e->caller->decl == current_function_decl)
2829 continue;
2830 if (!e->caller->has_gimple_body_p ())
2831 continue;
2832 if (TREE_ASM_WRITTEN (e->caller->decl))
2833 continue;
2834 if (!e->caller->process && !e->caller->global.inlined_to)
2835 break;
2837 if (dump_file && e)
2839 fprintf (dump_file, "Already processed call to:\n");
2840 e->caller->dump (dump_file);
2842 return e != NULL;
2845 #include "gt-passes.h"