Merge trunk version 193672 into gupc branch.
[official-gcc.git] / gcc / passes.c
blob7e224fb7a4cea213ee0a459e775aaed794c2c482
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011, 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This is the top level of cc1/c++.
23 It parses command args, opens files, invokes the various passes
24 in the proper order, and counts the time used by each.
25 Error messages and low-level interface to malloc also handled here. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "line-map.h"
32 #include "input.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "insn-attr.h"
38 #include "insn-config.h"
39 #include "insn-flags.h"
40 #include "hard-reg-set.h"
41 #include "recog.h"
42 #include "output.h"
43 #include "except.h"
44 #include "function.h"
45 #include "toplev.h"
46 #include "expr.h"
47 #include "basic-block.h"
48 #include "intl.h"
49 #include "ggc.h"
50 #include "graph.h"
51 #include "regs.h"
52 #include "diagnostic-core.h"
53 #include "params.h"
54 #include "reload.h"
55 #include "debug.h"
56 #include "target.h"
57 #include "langhooks.h"
58 #include "cfgloop.h"
59 #include "hosthooks.h"
60 #include "cgraph.h"
61 #include "opts.h"
62 #include "coverage.h"
63 #include "value-prof.h"
64 #include "tree-inline.h"
65 #include "tree-flow.h"
66 #include "tree-pass.h"
67 #include "tree-dump.h"
68 #include "df.h"
69 #include "predict.h"
70 #include "lto-streamer.h"
71 #include "plugin.h"
72 #include "ipa-utils.h"
73 #include "tree-pretty-print.h" /* for dump_function_header */
75 /* This is used for debugging. It allows the current pass to printed
76 from anywhere in compilation.
77 The variable current_pass is also used for statistics and plugins. */
78 struct opt_pass *current_pass;
80 static void register_pass_name (struct opt_pass *, const char *);
82 /* Call from anywhere to find out what pass this is. Useful for
83 printing out debugging information deep inside an service
84 routine. */
85 void
86 print_current_pass (FILE *file)
88 if (current_pass)
89 fprintf (file, "current pass = %s (%d)\n",
90 current_pass->name, current_pass->static_pass_number);
91 else
92 fprintf (file, "no current pass.\n");
96 /* Call from the debugger to get the current pass name. */
97 DEBUG_FUNCTION void
98 debug_pass (void)
100 print_current_pass (stderr);
105 /* Global variables used to communicate with passes. */
106 bool in_gimple_form;
107 bool first_pass_instance;
110 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
111 and TYPE_DECL nodes.
113 This does nothing for local (non-static) variables, unless the
114 variable is a register variable with DECL_ASSEMBLER_NAME set. In
115 that case, or if the variable is not an automatic, it sets up the
116 RTL and outputs any assembler code (label definition, storage
117 allocation and initialization).
119 DECL is the declaration. TOP_LEVEL is nonzero
120 if this declaration is not within a function. */
122 void
123 rest_of_decl_compilation (tree decl,
124 int top_level,
125 int at_end)
127 /* We deferred calling assemble_alias so that we could collect
128 other attributes such as visibility. Emit the alias now. */
129 if (!in_lto_p)
131 tree alias;
132 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
133 if (alias)
135 alias = TREE_VALUE (TREE_VALUE (alias));
136 alias = get_identifier (TREE_STRING_POINTER (alias));
137 /* A quirk of the initial implementation of aliases required that the
138 user add "extern" to all of them. Which is silly, but now
139 historical. Do note that the symbol is in fact locally defined. */
140 if (!lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
141 DECL_EXTERNAL (decl) = 0;
142 assemble_alias (decl, alias);
146 /* Can't defer this, because it needs to happen before any
147 later function definitions are processed. */
148 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
149 make_decl_rtl (decl);
151 /* Forward declarations for nested functions are not "external",
152 but we need to treat them as if they were. */
153 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
154 || TREE_CODE (decl) == FUNCTION_DECL)
156 timevar_push (TV_VARCONST);
158 /* Don't output anything when a tentative file-scope definition
159 is seen. But at end of compilation, do output code for them.
161 We do output all variables and rely on
162 callgraph code to defer them except for forward declarations
163 (see gcc.c-torture/compile/920624-1.c) */
164 if ((at_end
165 || !DECL_DEFER_OUTPUT (decl)
166 || DECL_INITIAL (decl))
167 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
168 && !DECL_EXTERNAL (decl))
170 /* When reading LTO unit, we also read varpool, so do not
171 rebuild it. */
172 if (in_lto_p && !at_end)
174 else if (TREE_CODE (decl) != FUNCTION_DECL)
175 varpool_finalize_decl (decl);
178 #ifdef ASM_FINISH_DECLARE_OBJECT
179 if (decl == last_assemble_variable_decl)
181 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
182 top_level, at_end);
184 #endif
186 timevar_pop (TV_VARCONST);
188 else if (TREE_CODE (decl) == TYPE_DECL
189 /* Like in rest_of_type_compilation, avoid confusing the debug
190 information machinery when there are errors. */
191 && !seen_error ())
193 timevar_push (TV_SYMOUT);
194 debug_hooks->type_decl (decl, !top_level);
195 timevar_pop (TV_SYMOUT);
198 /* Let cgraph know about the existence of variables. */
199 if (in_lto_p && !at_end)
201 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
202 && TREE_STATIC (decl))
203 varpool_node_for_decl (decl);
206 /* Called after finishing a record, union or enumeral type. */
208 void
209 rest_of_type_compilation (tree type, int toplev)
211 /* Avoid confusing the debug information machinery when there are
212 errors. */
213 if (seen_error ())
214 return;
216 timevar_push (TV_SYMOUT);
217 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
218 timevar_pop (TV_SYMOUT);
223 void
224 finish_optimization_passes (void)
226 int i;
227 struct dump_file_info *dfi;
228 char *name;
230 timevar_push (TV_DUMP);
231 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
233 dump_start (pass_profile.pass.static_pass_number, NULL);
234 end_branch_prob ();
235 dump_finish (pass_profile.pass.static_pass_number);
238 if (optimize > 0)
240 dump_start (pass_profile.pass.static_pass_number, NULL);
241 print_combine_total_stats ();
242 dump_finish (pass_combine.pass.static_pass_number);
245 /* Do whatever is necessary to finish printing the graphs. */
246 if (graph_dump_format != no_graph)
247 for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
248 if (dump_initialized_p (i)
249 && (dfi->pflags & TDF_GRAPH) != 0
250 && (name = get_dump_file_name (i)) != NULL)
252 finish_graph_dump_file (name);
253 free (name);
256 timevar_pop (TV_DUMP);
259 static unsigned int
260 execute_all_early_local_passes (void)
262 /* Once this pass (and its sub-passes) are complete, all functions
263 will be in SSA form. Technically this state change is happening
264 a tad early, since the sub-passes have not yet run, but since
265 none of the sub-passes are IPA passes and do not create new
266 functions, this is ok. We're setting this value for the benefit
267 of IPA passes that follow. */
268 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
269 cgraph_state = CGRAPH_STATE_IPA_SSA;
270 return 0;
273 /* Gate: execute, or not, all of the non-trivial optimizations. */
275 static bool
276 gate_all_early_local_passes (void)
278 /* Don't bother doing anything if the program has errors. */
279 return (!seen_error () && !in_lto_p);
282 struct simple_ipa_opt_pass pass_early_local_passes =
285 SIMPLE_IPA_PASS,
286 "early_local_cleanups", /* name */
287 OPTGROUP_NONE, /* optinfo_flags */
288 gate_all_early_local_passes, /* gate */
289 execute_all_early_local_passes, /* execute */
290 NULL, /* sub */
291 NULL, /* next */
292 0, /* static_pass_number */
293 TV_EARLY_LOCAL, /* tv_id */
294 0, /* properties_required */
295 0, /* properties_provided */
296 0, /* properties_destroyed */
297 0, /* todo_flags_start */
298 TODO_remove_functions /* todo_flags_finish */
302 /* Gate: execute, or not, all of the non-trivial optimizations. */
304 static bool
305 gate_all_early_optimizations (void)
307 return (optimize >= 1
308 /* Don't bother doing anything if the program has errors. */
309 && !seen_error ());
312 static struct gimple_opt_pass pass_all_early_optimizations =
315 GIMPLE_PASS,
316 "early_optimizations", /* name */
317 OPTGROUP_NONE, /* optinfo_flags */
318 gate_all_early_optimizations, /* gate */
319 NULL, /* execute */
320 NULL, /* sub */
321 NULL, /* next */
322 0, /* static_pass_number */
323 TV_NONE, /* tv_id */
324 0, /* properties_required */
325 0, /* properties_provided */
326 0, /* properties_destroyed */
327 0, /* todo_flags_start */
328 0 /* todo_flags_finish */
332 /* Gate: execute, or not, all of the non-trivial optimizations. */
334 static bool
335 gate_all_optimizations (void)
337 return optimize >= 1 && !optimize_debug;
340 static struct gimple_opt_pass pass_all_optimizations =
343 GIMPLE_PASS,
344 "*all_optimizations", /* name */
345 OPTGROUP_NONE, /* optinfo_flags */
346 gate_all_optimizations, /* gate */
347 NULL, /* execute */
348 NULL, /* sub */
349 NULL, /* next */
350 0, /* static_pass_number */
351 TV_OPTIMIZE, /* tv_id */
352 0, /* properties_required */
353 0, /* properties_provided */
354 0, /* properties_destroyed */
355 0, /* todo_flags_start */
356 0 /* todo_flags_finish */
360 /* Gate: execute, or not, all of the non-trivial optimizations. */
362 static bool
363 gate_all_optimizations_g (void)
365 return optimize >= 1 && optimize_debug;
368 static struct gimple_opt_pass pass_all_optimizations_g =
371 GIMPLE_PASS,
372 "*all_optimizations_g", /* name */
373 OPTGROUP_NONE, /* optinfo_flags */
374 gate_all_optimizations_g, /* gate */
375 NULL, /* execute */
376 NULL, /* sub */
377 NULL, /* next */
378 0, /* static_pass_number */
379 TV_OPTIMIZE, /* tv_id */
380 0, /* properties_required */
381 0, /* properties_provided */
382 0, /* properties_destroyed */
383 0, /* todo_flags_start */
384 0 /* todo_flags_finish */
388 static bool
389 gate_rest_of_compilation (void)
391 /* Early return if there were errors. We can run afoul of our
392 consistency checks, and there's not really much point in fixing them. */
393 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
396 static struct rtl_opt_pass pass_rest_of_compilation =
399 RTL_PASS,
400 "*rest_of_compilation", /* name */
401 OPTGROUP_NONE, /* optinfo_flags */
402 gate_rest_of_compilation, /* gate */
403 NULL, /* execute */
404 NULL, /* sub */
405 NULL, /* next */
406 0, /* static_pass_number */
407 TV_REST_OF_COMPILATION, /* tv_id */
408 PROP_rtl, /* properties_required */
409 0, /* properties_provided */
410 0, /* properties_destroyed */
411 0, /* todo_flags_start */
412 TODO_ggc_collect /* todo_flags_finish */
416 static bool
417 gate_postreload (void)
419 return reload_completed;
422 static struct rtl_opt_pass pass_postreload =
425 RTL_PASS,
426 "*all-postreload", /* name */
427 OPTGROUP_NONE, /* optinfo_flags */
428 gate_postreload, /* gate */
429 NULL, /* execute */
430 NULL, /* sub */
431 NULL, /* next */
432 0, /* static_pass_number */
433 TV_POSTRELOAD, /* tv_id */
434 PROP_rtl, /* properties_required */
435 0, /* properties_provided */
436 0, /* properties_destroyed */
437 0, /* todo_flags_start */
438 TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
444 /* The root of the compilation pass tree, once constructed. */
445 struct opt_pass *all_passes, *all_small_ipa_passes, *all_lowering_passes,
446 *all_regular_ipa_passes, *all_late_ipa_passes, *all_lto_gen_passes;
448 /* This is used by plugins, and should also be used in register_pass. */
449 #define DEF_PASS_LIST(LIST) &LIST,
450 struct opt_pass **gcc_pass_lists[] = { GCC_PASS_LISTS NULL };
451 #undef DEF_PASS_LIST
453 /* A map from static pass id to optimization pass. */
454 struct opt_pass **passes_by_id;
455 int passes_by_id_size;
457 /* Set the static pass number of pass PASS to ID and record that
458 in the mapping from static pass number to pass. */
460 static void
461 set_pass_for_id (int id, struct opt_pass *pass)
463 pass->static_pass_number = id;
464 if (passes_by_id_size <= id)
466 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
467 memset (passes_by_id + passes_by_id_size, 0,
468 (id + 1 - passes_by_id_size) * sizeof (void *));
469 passes_by_id_size = id + 1;
471 passes_by_id[id] = pass;
474 /* Return the pass with the static pass number ID. */
476 struct opt_pass *
477 get_pass_for_id (int id)
479 if (id >= passes_by_id_size)
480 return NULL;
481 return passes_by_id[id];
484 /* Iterate over the pass tree allocating dump file numbers. We want
485 to do this depth first, and independent of whether the pass is
486 enabled or not. */
488 void
489 register_one_dump_file (struct opt_pass *pass)
491 char *dot_name, *flag_name, *glob_name;
492 const char *name, *full_name, *prefix;
493 char num[10];
494 int flags, id;
495 int optgroup_flags = OPTGROUP_NONE;
497 /* See below in next_pass_1. */
498 num[0] = '\0';
499 if (pass->static_pass_number != -1)
500 sprintf (num, "%d", ((int) pass->static_pass_number < 0
501 ? 1 : pass->static_pass_number));
503 /* The name is both used to identify the pass for the purposes of plugins,
504 and to specify dump file name and option.
505 The latter two might want something short which is not quite unique; for
506 that reason, we may have a disambiguating prefix, followed by a space
507 to mark the start of the following dump file name / option string. */
508 name = strchr (pass->name, ' ');
509 name = name ? name + 1 : pass->name;
510 dot_name = concat (".", name, num, NULL);
511 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
513 prefix = "ipa-";
514 flags = TDF_IPA;
515 optgroup_flags |= OPTGROUP_IPA;
517 else if (pass->type == GIMPLE_PASS)
519 prefix = "tree-";
520 flags = TDF_TREE;
522 else
524 prefix = "rtl-";
525 flags = TDF_RTL;
528 flag_name = concat (prefix, name, num, NULL);
529 glob_name = concat (prefix, name, NULL);
530 optgroup_flags |= pass->optinfo_flags;
531 id = dump_register (dot_name, flag_name, glob_name, flags, optgroup_flags);
532 set_pass_for_id (id, pass);
533 full_name = concat (prefix, pass->name, num, NULL);
534 register_pass_name (pass, full_name);
535 free (CONST_CAST (char *, full_name));
538 /* Recursive worker function for register_dump_files. */
540 static int
541 register_dump_files_1 (struct opt_pass *pass, int properties)
545 int new_properties = (properties | pass->properties_provided)
546 & ~pass->properties_destroyed;
548 if (pass->name && pass->name[0] != '*')
549 register_one_dump_file (pass);
551 if (pass->sub)
552 new_properties = register_dump_files_1 (pass->sub, new_properties);
554 /* If we have a gate, combine the properties that we could have with
555 and without the pass being examined. */
556 if (pass->gate)
557 properties &= new_properties;
558 else
559 properties = new_properties;
561 pass = pass->next;
563 while (pass);
565 return properties;
568 /* Register the dump files for the pipeline starting at PASS.
569 PROPERTIES reflects the properties that are guaranteed to be available at
570 the beginning of the pipeline. */
572 static void
573 register_dump_files (struct opt_pass *pass,int properties)
575 pass->properties_required |= properties;
576 register_dump_files_1 (pass, properties);
579 struct pass_registry
581 const char* unique_name;
582 struct opt_pass *pass;
585 /* Pass registry hash function. */
587 static hashval_t
588 passr_hash (const void *p)
590 const struct pass_registry *const s = (const struct pass_registry *const) p;
591 return htab_hash_string (s->unique_name);
594 /* Hash equal function */
596 static int
597 passr_eq (const void *p1, const void *p2)
599 const struct pass_registry *const s1 = (const struct pass_registry *const) p1;
600 const struct pass_registry *const s2 = (const struct pass_registry *const) p2;
602 return !strcmp (s1->unique_name, s2->unique_name);
605 static htab_t name_to_pass_map = NULL;
607 /* Register PASS with NAME. */
609 static void
610 register_pass_name (struct opt_pass *pass, const char *name)
612 struct pass_registry **slot;
613 struct pass_registry pr;
615 if (!name_to_pass_map)
616 name_to_pass_map = htab_create (256, passr_hash, passr_eq, NULL);
618 pr.unique_name = name;
619 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map, &pr, INSERT);
620 if (!*slot)
622 struct pass_registry *new_pr;
624 new_pr = XCNEW (struct pass_registry);
625 new_pr->unique_name = xstrdup (name);
626 new_pr->pass = pass;
627 *slot = new_pr;
629 else
630 return; /* Ignore plugin passes. */
633 /* Map from pass id to canonicalized pass name. */
635 typedef const char *char_ptr;
636 static vec<char_ptr> pass_tab = vec<char_ptr>();
638 /* Callback function for traversing NAME_TO_PASS_MAP. */
640 static int
641 pass_traverse (void **slot, void *data ATTRIBUTE_UNUSED)
643 struct pass_registry **p = (struct pass_registry **)slot;
644 struct opt_pass *pass = (*p)->pass;
646 gcc_assert (pass->static_pass_number > 0);
647 gcc_assert (pass_tab.exists ());
649 pass_tab[pass->static_pass_number] = (*p)->unique_name;
651 return 1;
654 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
655 table for dumping purpose. */
657 static void
658 create_pass_tab (void)
660 if (!flag_dump_passes)
661 return;
663 pass_tab.safe_grow_cleared (passes_by_id_size + 1);
664 htab_traverse (name_to_pass_map, pass_traverse, NULL);
667 static bool override_gate_status (struct opt_pass *, tree, bool);
669 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
670 is turned on or not. */
672 static void
673 dump_one_pass (struct opt_pass *pass, int pass_indent)
675 int indent = 3 * pass_indent;
676 const char *pn;
677 bool is_on, is_really_on;
679 is_on = (pass->gate == NULL) ? true : pass->gate();
680 is_really_on = override_gate_status (pass, current_function_decl, is_on);
682 if (pass->static_pass_number <= 0)
683 pn = pass->name;
684 else
685 pn = pass_tab[pass->static_pass_number];
687 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
688 (15 - indent < 0 ? 0 : 15 - indent), " ",
689 is_on ? " ON" : " OFF",
690 ((!is_on) == (!is_really_on) ? ""
691 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
694 /* Dump pass list PASS with indentation INDENT. */
696 static void
697 dump_pass_list (struct opt_pass *pass, int indent)
701 dump_one_pass (pass, indent);
702 if (pass->sub)
703 dump_pass_list (pass->sub, indent + 1);
704 pass = pass->next;
706 while (pass);
709 /* Dump all optimization passes. */
711 void
712 dump_passes (void)
714 struct cgraph_node *n, *node = NULL;
716 create_pass_tab();
718 FOR_EACH_DEFINED_FUNCTION (n)
719 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
721 node = n;
722 break;
725 if (!node)
726 return;
728 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
730 dump_pass_list (all_lowering_passes, 1);
731 dump_pass_list (all_small_ipa_passes, 1);
732 dump_pass_list (all_regular_ipa_passes, 1);
733 dump_pass_list (all_lto_gen_passes, 1);
734 dump_pass_list (all_late_ipa_passes, 1);
735 dump_pass_list (all_passes, 1);
737 pop_cfun ();
741 /* Returns the pass with NAME. */
743 static struct opt_pass *
744 get_pass_by_name (const char *name)
746 struct pass_registry **slot, pr;
748 pr.unique_name = name;
749 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map,
750 &pr, NO_INSERT);
752 if (!slot || !*slot)
753 return NULL;
755 return (*slot)->pass;
759 /* Range [start, last]. */
761 struct uid_range
763 unsigned int start;
764 unsigned int last;
765 const char *assem_name;
766 struct uid_range *next;
769 typedef struct uid_range *uid_range_p;
772 static vec<uid_range_p>
773 enabled_pass_uid_range_tab = vec<uid_range_p>();
774 static vec<uid_range_p>
775 disabled_pass_uid_range_tab = vec<uid_range_p>();
778 /* Parse option string for -fdisable- and -fenable-
779 The syntax of the options:
781 -fenable-<pass_name>
782 -fdisable-<pass_name>
784 -fenable-<pass_name>=s1:e1,s2:e2,...
785 -fdisable-<pass_name>=s1:e1,s2:e2,...
788 static void
789 enable_disable_pass (const char *arg, bool is_enable)
791 struct opt_pass *pass;
792 char *range_str, *phase_name;
793 char *argstr = xstrdup (arg);
794 vec<uid_range_p> *tab = 0;
796 range_str = strchr (argstr,'=');
797 if (range_str)
799 *range_str = '\0';
800 range_str++;
803 phase_name = argstr;
804 if (!*phase_name)
806 if (is_enable)
807 error ("unrecognized option -fenable");
808 else
809 error ("unrecognized option -fdisable");
810 free (argstr);
811 return;
813 pass = get_pass_by_name (phase_name);
814 if (!pass || pass->static_pass_number == -1)
816 if (is_enable)
817 error ("unknown pass %s specified in -fenable", phase_name);
818 else
819 error ("unknown pass %s specified in -fdisable", phase_name);
820 free (argstr);
821 return;
824 if (is_enable)
825 tab = &enabled_pass_uid_range_tab;
826 else
827 tab = &disabled_pass_uid_range_tab;
829 if ((unsigned) pass->static_pass_number >= tab->length ())
830 tab->safe_grow_cleared (pass->static_pass_number + 1);
832 if (!range_str)
834 uid_range_p slot;
835 uid_range_p new_range = XCNEW (struct uid_range);
837 new_range->start = 0;
838 new_range->last = (unsigned)-1;
840 slot = (*tab)[pass->static_pass_number];
841 new_range->next = slot;
842 (*tab)[pass->static_pass_number] = new_range;
843 if (is_enable)
844 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
845 "of [%u, %u]", phase_name, new_range->start, new_range->last);
846 else
847 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
848 "of [%u, %u]", phase_name, new_range->start, new_range->last);
850 else
852 char *next_range = NULL;
853 char *one_range = range_str;
854 char *end_val = NULL;
858 uid_range_p slot;
859 uid_range_p new_range;
860 char *invalid = NULL;
861 long start;
862 char *func_name = NULL;
864 next_range = strchr (one_range, ',');
865 if (next_range)
867 *next_range = '\0';
868 next_range++;
871 end_val = strchr (one_range, ':');
872 if (end_val)
874 *end_val = '\0';
875 end_val++;
877 start = strtol (one_range, &invalid, 10);
878 if (*invalid || start < 0)
880 if (end_val || (one_range[0] >= '0'
881 && one_range[0] <= '9'))
883 error ("Invalid range %s in option %s",
884 one_range,
885 is_enable ? "-fenable" : "-fdisable");
886 free (argstr);
887 return;
889 func_name = one_range;
891 if (!end_val)
893 new_range = XCNEW (struct uid_range);
894 if (!func_name)
896 new_range->start = (unsigned) start;
897 new_range->last = (unsigned) start;
899 else
901 new_range->start = (unsigned) -1;
902 new_range->last = (unsigned) -1;
903 new_range->assem_name = xstrdup (func_name);
906 else
908 long last = strtol (end_val, &invalid, 10);
909 if (*invalid || last < start)
911 error ("Invalid range %s in option %s",
912 end_val,
913 is_enable ? "-fenable" : "-fdisable");
914 free (argstr);
915 return;
917 new_range = XCNEW (struct uid_range);
918 new_range->start = (unsigned) start;
919 new_range->last = (unsigned) last;
922 slot = (*tab)[pass->static_pass_number];
923 new_range->next = slot;
924 (*tab)[pass->static_pass_number] = new_range;
925 if (is_enable)
927 if (new_range->assem_name)
928 inform (UNKNOWN_LOCATION,
929 "enable pass %s for function %s",
930 phase_name, new_range->assem_name);
931 else
932 inform (UNKNOWN_LOCATION,
933 "enable pass %s for functions in the range of [%u, %u]",
934 phase_name, new_range->start, new_range->last);
936 else
938 if (new_range->assem_name)
939 inform (UNKNOWN_LOCATION,
940 "disable pass %s for function %s",
941 phase_name, new_range->assem_name);
942 else
943 inform (UNKNOWN_LOCATION,
944 "disable pass %s for functions in the range of [%u, %u]",
945 phase_name, new_range->start, new_range->last);
948 one_range = next_range;
949 } while (next_range);
952 free (argstr);
955 /* Enable pass specified by ARG. */
957 void
958 enable_pass (const char *arg)
960 enable_disable_pass (arg, true);
963 /* Disable pass specified by ARG. */
965 void
966 disable_pass (const char *arg)
968 enable_disable_pass (arg, false);
971 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
973 static bool
974 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
975 tree func,
976 vec<uid_range_p> tab)
978 uid_range_p slot, range;
979 int cgraph_uid;
980 const char *aname = NULL;
982 if (!tab.exists ()
983 || (unsigned) pass->static_pass_number >= tab.length ()
984 || pass->static_pass_number == -1)
985 return false;
987 slot = tab[pass->static_pass_number];
988 if (!slot)
989 return false;
991 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
992 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
993 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
995 range = slot;
996 while (range)
998 if ((unsigned) cgraph_uid >= range->start
999 && (unsigned) cgraph_uid <= range->last)
1000 return true;
1001 if (range->assem_name && aname
1002 && !strcmp (range->assem_name, aname))
1003 return true;
1004 range = range->next;
1007 return false;
1010 /* Look at the static_pass_number and duplicate the pass
1011 if it is already added to a list. */
1013 static struct opt_pass *
1014 make_pass_instance (struct opt_pass *pass, bool track_duplicates)
1016 /* A nonzero static_pass_number indicates that the
1017 pass is already in the list. */
1018 if (pass->static_pass_number)
1020 struct opt_pass *new_pass;
1022 if (pass->type == GIMPLE_PASS
1023 || pass->type == RTL_PASS
1024 || pass->type == SIMPLE_IPA_PASS)
1026 new_pass = XNEW (struct opt_pass);
1027 memcpy (new_pass, pass, sizeof (struct opt_pass));
1029 else if (pass->type == IPA_PASS)
1031 new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
1032 memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
1034 else
1035 gcc_unreachable ();
1037 new_pass->next = NULL;
1039 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1041 /* Indicate to register_dump_files that this pass has duplicates,
1042 and so it should rename the dump file. The first instance will
1043 be -1, and be number of duplicates = -static_pass_number - 1.
1044 Subsequent instances will be > 0 and just the duplicate number. */
1045 if ((pass->name && pass->name[0] != '*') || track_duplicates)
1047 pass->static_pass_number -= 1;
1048 new_pass->static_pass_number = -pass->static_pass_number;
1050 return new_pass;
1052 else
1054 pass->todo_flags_start |= TODO_mark_first_instance;
1055 pass->static_pass_number = -1;
1057 invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
1059 return pass;
1062 /* Add a pass to the pass list. Duplicate the pass if it's already
1063 in the list. */
1065 static struct opt_pass **
1066 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
1068 /* Every pass should have a name so that plugins can refer to them. */
1069 gcc_assert (pass->name != NULL);
1071 *list = make_pass_instance (pass, false);
1073 return &(*list)->next;
1076 /* List node for an inserted pass instance. We need to keep track of all
1077 the newly-added pass instances (with 'added_pass_nodes' defined below)
1078 so that we can register their dump files after pass-positioning is finished.
1079 Registering dumping files needs to be post-processed or the
1080 static_pass_number of the opt_pass object would be modified and mess up
1081 the dump file names of future pass instances to be added. */
1083 struct pass_list_node
1085 struct opt_pass *pass;
1086 struct pass_list_node *next;
1089 static struct pass_list_node *added_pass_nodes = NULL;
1090 static struct pass_list_node *prev_added_pass_node;
1092 /* Insert the pass at the proper position. Return true if the pass
1093 is successfully added.
1095 NEW_PASS_INFO - new pass to be inserted
1096 PASS_LIST - root of the pass list to insert the new pass to */
1098 static bool
1099 position_pass (struct register_pass_info *new_pass_info,
1100 struct opt_pass **pass_list)
1102 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1103 bool success = false;
1105 for ( ; pass; prev_pass = pass, pass = pass->next)
1107 /* Check if the current pass is of the same type as the new pass and
1108 matches the name and the instance number of the reference pass. */
1109 if (pass->type == new_pass_info->pass->type
1110 && pass->name
1111 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1112 && ((new_pass_info->ref_pass_instance_number == 0)
1113 || (new_pass_info->ref_pass_instance_number ==
1114 pass->static_pass_number)
1115 || (new_pass_info->ref_pass_instance_number == 1
1116 && pass->todo_flags_start & TODO_mark_first_instance)))
1118 struct opt_pass *new_pass;
1119 struct pass_list_node *new_pass_node;
1121 new_pass = make_pass_instance (new_pass_info->pass, true);
1123 /* Insert the new pass instance based on the positioning op. */
1124 switch (new_pass_info->pos_op)
1126 case PASS_POS_INSERT_AFTER:
1127 new_pass->next = pass->next;
1128 pass->next = new_pass;
1130 /* Skip newly inserted pass to avoid repeated
1131 insertions in the case where the new pass and the
1132 existing one have the same name. */
1133 pass = new_pass;
1134 break;
1135 case PASS_POS_INSERT_BEFORE:
1136 new_pass->next = pass;
1137 if (prev_pass)
1138 prev_pass->next = new_pass;
1139 else
1140 *pass_list = new_pass;
1141 break;
1142 case PASS_POS_REPLACE:
1143 new_pass->next = pass->next;
1144 if (prev_pass)
1145 prev_pass->next = new_pass;
1146 else
1147 *pass_list = new_pass;
1148 new_pass->sub = pass->sub;
1149 new_pass->tv_id = pass->tv_id;
1150 pass = new_pass;
1151 break;
1152 default:
1153 error ("invalid pass positioning operation");
1154 return false;
1157 /* Save the newly added pass (instance) in the added_pass_nodes
1158 list so that we can register its dump file later. Note that
1159 we cannot register the dump file now because doing so will modify
1160 the static_pass_number of the opt_pass object and therefore
1161 mess up the dump file name of future instances. */
1162 new_pass_node = XCNEW (struct pass_list_node);
1163 new_pass_node->pass = new_pass;
1164 if (!added_pass_nodes)
1165 added_pass_nodes = new_pass_node;
1166 else
1167 prev_added_pass_node->next = new_pass_node;
1168 prev_added_pass_node = new_pass_node;
1170 success = true;
1173 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1174 success = true;
1177 return success;
1180 /* Hooks a new pass into the pass lists.
1182 PASS_INFO - pass information that specifies the opt_pass object,
1183 reference pass, instance number, and how to position
1184 the pass */
1186 void
1187 register_pass (struct register_pass_info *pass_info)
1189 bool all_instances, success;
1191 /* The checks below could fail in buggy plugins. Existing GCC
1192 passes should never fail these checks, so we mention plugin in
1193 the messages. */
1194 if (!pass_info->pass)
1195 fatal_error ("plugin cannot register a missing pass");
1197 if (!pass_info->pass->name)
1198 fatal_error ("plugin cannot register an unnamed pass");
1200 if (!pass_info->reference_pass_name)
1201 fatal_error
1202 ("plugin cannot register pass %qs without reference pass name",
1203 pass_info->pass->name);
1205 /* Try to insert the new pass to the pass lists. We need to check
1206 all five lists as the reference pass could be in one (or all) of
1207 them. */
1208 all_instances = pass_info->ref_pass_instance_number == 0;
1209 success = position_pass (pass_info, &all_lowering_passes);
1210 if (!success || all_instances)
1211 success |= position_pass (pass_info, &all_small_ipa_passes);
1212 if (!success || all_instances)
1213 success |= position_pass (pass_info, &all_regular_ipa_passes);
1214 if (!success || all_instances)
1215 success |= position_pass (pass_info, &all_lto_gen_passes);
1216 if (!success || all_instances)
1217 success |= position_pass (pass_info, &all_late_ipa_passes);
1218 if (!success || all_instances)
1219 success |= position_pass (pass_info, &all_passes);
1220 if (!success)
1221 fatal_error
1222 ("pass %qs not found but is referenced by new pass %qs",
1223 pass_info->reference_pass_name, pass_info->pass->name);
1225 /* OK, we have successfully inserted the new pass. We need to register
1226 the dump files for the newly added pass and its duplicates (if any).
1227 Because the registration of plugin/backend passes happens after the
1228 command-line options are parsed, the options that specify single
1229 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1230 passes. Therefore we currently can only enable dumping of
1231 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1232 are specified. While doing so, we also delete the pass_list_node
1233 objects created during pass positioning. */
1234 while (added_pass_nodes)
1236 struct pass_list_node *next_node = added_pass_nodes->next;
1237 enum tree_dump_index tdi;
1238 register_one_dump_file (added_pass_nodes->pass);
1239 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1240 || added_pass_nodes->pass->type == IPA_PASS)
1241 tdi = TDI_ipa_all;
1242 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1243 tdi = TDI_tree_all;
1244 else
1245 tdi = TDI_rtl_all;
1246 /* Check if dump-all flag is specified. */
1247 if (get_dump_file_info (tdi)->pstate)
1248 get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1249 ->pstate = get_dump_file_info (tdi)->pstate;
1250 XDELETE (added_pass_nodes);
1251 added_pass_nodes = next_node;
1255 /* Construct the pass tree. The sequencing of passes is driven by
1256 the cgraph routines:
1258 finalize_compilation_unit ()
1259 for each node N in the cgraph
1260 cgraph_analyze_function (N)
1261 cgraph_lower_function (N) -> all_lowering_passes
1263 If we are optimizing, compile is then invoked:
1265 compile ()
1266 ipa_passes () -> all_small_ipa_passes
1267 -> Analysis of all_regular_ipa_passes
1268 * possible LTO streaming at copmilation time *
1269 -> Execution of all_regular_ipa_passes
1270 * possible LTO streaming at link time *
1271 -> all_late_ipa_passes
1272 expand_all_functions ()
1273 for each node N in the cgraph
1274 expand_function (N) -> Transformation of all_regular_ipa_passes
1275 -> all_passes
1278 void
1279 init_optimization_passes (void)
1281 struct opt_pass **p;
1283 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &((PASS).pass)))
1285 /* All passes needed to lower the function into shape optimizers can
1286 operate on. These passes are always run first on the function, but
1287 backend might produce already lowered functions that are not processed
1288 by these passes. */
1289 p = &all_lowering_passes;
1290 NEXT_PASS (pass_warn_unused_result);
1291 NEXT_PASS (pass_diagnose_omp_blocks);
1292 NEXT_PASS (pass_diagnose_tm_blocks);
1293 NEXT_PASS (pass_mudflap_1);
1294 NEXT_PASS (pass_lower_omp);
1295 NEXT_PASS (pass_lower_cf);
1296 NEXT_PASS (pass_lower_tm);
1297 NEXT_PASS (pass_refactor_eh);
1298 NEXT_PASS (pass_lower_eh);
1299 NEXT_PASS (pass_build_cfg);
1300 NEXT_PASS (pass_warn_function_return);
1301 NEXT_PASS (pass_build_cgraph_edges);
1302 *p = NULL;
1304 /* Interprocedural optimization passes. */
1305 p = &all_small_ipa_passes;
1306 NEXT_PASS (pass_ipa_free_lang_data);
1307 NEXT_PASS (pass_ipa_function_and_variable_visibility);
1308 NEXT_PASS (pass_early_local_passes);
1310 struct opt_pass **p = &pass_early_local_passes.pass.sub;
1311 NEXT_PASS (pass_fixup_cfg);
1312 NEXT_PASS (pass_init_datastructures);
1313 NEXT_PASS (pass_expand_omp);
1315 NEXT_PASS (pass_build_ssa);
1316 NEXT_PASS (pass_lower_vector);
1317 NEXT_PASS (pass_early_warn_uninitialized);
1318 NEXT_PASS (pass_rebuild_cgraph_edges);
1319 NEXT_PASS (pass_inline_parameters);
1320 NEXT_PASS (pass_early_inline);
1321 NEXT_PASS (pass_all_early_optimizations);
1323 struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
1324 NEXT_PASS (pass_remove_cgraph_callee_edges);
1325 NEXT_PASS (pass_rename_ssa_copies);
1326 NEXT_PASS (pass_ccp);
1327 /* After CCP we rewrite no longer addressed locals into SSA
1328 form if possible. */
1329 NEXT_PASS (pass_forwprop);
1330 /* pass_build_ealias is a dummy pass that ensures that we
1331 execute TODO_rebuild_alias at this point. */
1332 NEXT_PASS (pass_build_ealias);
1333 NEXT_PASS (pass_sra_early);
1334 NEXT_PASS (pass_fre);
1335 NEXT_PASS (pass_copy_prop);
1336 NEXT_PASS (pass_merge_phi);
1337 NEXT_PASS (pass_cd_dce);
1338 NEXT_PASS (pass_early_ipa_sra);
1339 NEXT_PASS (pass_tail_recursion);
1340 NEXT_PASS (pass_convert_switch);
1341 NEXT_PASS (pass_cleanup_eh);
1342 NEXT_PASS (pass_profile);
1343 NEXT_PASS (pass_local_pure_const);
1344 /* Split functions creates parts that are not run through
1345 early optimizations again. It is thus good idea to do this
1346 late. */
1347 NEXT_PASS (pass_split_functions);
1349 NEXT_PASS (pass_release_ssa_names);
1350 NEXT_PASS (pass_rebuild_cgraph_edges);
1351 NEXT_PASS (pass_inline_parameters);
1353 NEXT_PASS (pass_ipa_free_inline_summary);
1354 NEXT_PASS (pass_ipa_tree_profile);
1356 struct opt_pass **p = &pass_ipa_tree_profile.pass.sub;
1357 NEXT_PASS (pass_feedback_split_functions);
1359 NEXT_PASS (pass_ipa_increase_alignment);
1360 NEXT_PASS (pass_ipa_tm);
1361 NEXT_PASS (pass_ipa_lower_emutls);
1362 *p = NULL;
1364 p = &all_regular_ipa_passes;
1365 NEXT_PASS (pass_ipa_whole_program_visibility);
1366 NEXT_PASS (pass_ipa_profile);
1367 NEXT_PASS (pass_ipa_cp);
1368 NEXT_PASS (pass_ipa_cdtor_merge);
1369 NEXT_PASS (pass_ipa_inline);
1370 NEXT_PASS (pass_ipa_pure_const);
1371 NEXT_PASS (pass_ipa_reference);
1372 *p = NULL;
1374 p = &all_lto_gen_passes;
1375 NEXT_PASS (pass_ipa_lto_gimple_out);
1376 NEXT_PASS (pass_ipa_lto_finish_out); /* This must be the last LTO pass. */
1377 *p = NULL;
1379 /* Simple IPA passes executed after the regular passes. In WHOPR mode the
1380 passes are executed after partitioning and thus see just parts of the
1381 compiled unit. */
1382 p = &all_late_ipa_passes;
1383 NEXT_PASS (pass_ipa_pta);
1384 *p = NULL;
1386 /* These passes are run after IPA passes on every function that is being
1387 output to the assembler file. */
1388 p = &all_passes;
1389 NEXT_PASS (pass_fixup_cfg);
1390 NEXT_PASS (pass_lower_eh_dispatch);
1391 NEXT_PASS (pass_all_optimizations);
1393 struct opt_pass **p = &pass_all_optimizations.pass.sub;
1394 NEXT_PASS (pass_remove_cgraph_callee_edges);
1395 /* Initial scalar cleanups before alias computation.
1396 They ensure memory accesses are not indirect wherever possible. */
1397 NEXT_PASS (pass_strip_predict_hints);
1398 NEXT_PASS (pass_rename_ssa_copies);
1399 NEXT_PASS (pass_complete_unrolli);
1400 NEXT_PASS (pass_ccp);
1401 /* After CCP we rewrite no longer addressed locals into SSA
1402 form if possible. */
1403 NEXT_PASS (pass_forwprop);
1404 /* pass_build_alias is a dummy pass that ensures that we
1405 execute TODO_rebuild_alias at this point. */
1406 NEXT_PASS (pass_build_alias);
1407 NEXT_PASS (pass_return_slot);
1408 NEXT_PASS (pass_phiprop);
1409 NEXT_PASS (pass_fre);
1410 NEXT_PASS (pass_copy_prop);
1411 NEXT_PASS (pass_merge_phi);
1412 NEXT_PASS (pass_vrp);
1413 NEXT_PASS (pass_dce);
1414 NEXT_PASS (pass_call_cdce);
1415 NEXT_PASS (pass_cselim);
1416 NEXT_PASS (pass_tree_ifcombine);
1417 NEXT_PASS (pass_phiopt);
1418 NEXT_PASS (pass_tail_recursion);
1419 NEXT_PASS (pass_ch);
1420 NEXT_PASS (pass_stdarg);
1421 NEXT_PASS (pass_lower_complex);
1422 NEXT_PASS (pass_sra);
1423 NEXT_PASS (pass_rename_ssa_copies);
1424 /* The dom pass will also resolve all __builtin_constant_p calls
1425 that are still there to 0. This has to be done after some
1426 propagations have already run, but before some more dead code
1427 is removed, and this place fits nicely. Remember this when
1428 trying to move or duplicate pass_dominator somewhere earlier. */
1429 NEXT_PASS (pass_dominator);
1430 /* The only const/copy propagation opportunities left after
1431 DOM should be due to degenerate PHI nodes. So rather than
1432 run the full propagators, run a specialized pass which
1433 only examines PHIs to discover const/copy propagation
1434 opportunities. */
1435 NEXT_PASS (pass_phi_only_cprop);
1436 NEXT_PASS (pass_dse);
1437 NEXT_PASS (pass_reassoc);
1438 NEXT_PASS (pass_dce);
1439 NEXT_PASS (pass_forwprop);
1440 NEXT_PASS (pass_phiopt);
1441 NEXT_PASS (pass_object_sizes);
1442 NEXT_PASS (pass_strlen);
1443 NEXT_PASS (pass_ccp);
1444 /* After CCP we rewrite no longer addressed locals into SSA
1445 form if possible. */
1446 NEXT_PASS (pass_copy_prop);
1447 NEXT_PASS (pass_cse_sincos);
1448 NEXT_PASS (pass_optimize_bswap);
1449 NEXT_PASS (pass_split_crit_edges);
1450 NEXT_PASS (pass_pre);
1451 NEXT_PASS (pass_sink_code);
1452 NEXT_PASS (pass_asan);
1453 NEXT_PASS (pass_tree_loop);
1455 struct opt_pass **p = &pass_tree_loop.pass.sub;
1456 NEXT_PASS (pass_tree_loop_init);
1457 NEXT_PASS (pass_lim);
1458 NEXT_PASS (pass_copy_prop);
1459 NEXT_PASS (pass_dce_loop);
1460 NEXT_PASS (pass_tree_unswitch);
1461 NEXT_PASS (pass_scev_cprop);
1462 NEXT_PASS (pass_record_bounds);
1463 NEXT_PASS (pass_check_data_deps);
1464 NEXT_PASS (pass_loop_distribution);
1465 NEXT_PASS (pass_copy_prop);
1466 NEXT_PASS (pass_graphite);
1468 struct opt_pass **p = &pass_graphite.pass.sub;
1469 NEXT_PASS (pass_graphite_transforms);
1470 NEXT_PASS (pass_lim);
1471 NEXT_PASS (pass_copy_prop);
1472 NEXT_PASS (pass_dce_loop);
1474 NEXT_PASS (pass_iv_canon);
1475 NEXT_PASS (pass_if_conversion);
1476 NEXT_PASS (pass_vectorize);
1478 struct opt_pass **p = &pass_vectorize.pass.sub;
1479 NEXT_PASS (pass_dce_loop);
1481 NEXT_PASS (pass_predcom);
1482 NEXT_PASS (pass_complete_unroll);
1483 NEXT_PASS (pass_slp_vectorize);
1484 NEXT_PASS (pass_parallelize_loops);
1485 NEXT_PASS (pass_loop_prefetch);
1486 NEXT_PASS (pass_iv_optimize);
1487 NEXT_PASS (pass_lim);
1488 NEXT_PASS (pass_tree_loop_done);
1490 NEXT_PASS (pass_lower_vector_ssa);
1491 NEXT_PASS (pass_cse_reciprocals);
1492 NEXT_PASS (pass_reassoc);
1493 NEXT_PASS (pass_vrp);
1494 NEXT_PASS (pass_strength_reduction);
1495 NEXT_PASS (pass_dominator);
1496 /* The only const/copy propagation opportunities left after
1497 DOM should be due to degenerate PHI nodes. So rather than
1498 run the full propagators, run a specialized pass which
1499 only examines PHIs to discover const/copy propagation
1500 opportunities. */
1501 NEXT_PASS (pass_phi_only_cprop);
1502 NEXT_PASS (pass_cd_dce);
1503 NEXT_PASS (pass_tracer);
1505 /* FIXME: If DCE is not run before checking for uninitialized uses,
1506 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
1507 However, this also causes us to misdiagnose cases that should be
1508 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
1510 To fix the false positives in uninit-5.c, we would have to
1511 account for the predicates protecting the set and the use of each
1512 variable. Using a representation like Gated Single Assignment
1513 may help. */
1514 NEXT_PASS (pass_late_warn_uninitialized);
1515 NEXT_PASS (pass_dse);
1516 NEXT_PASS (pass_forwprop);
1517 NEXT_PASS (pass_phiopt);
1518 NEXT_PASS (pass_fold_builtins);
1519 NEXT_PASS (pass_optimize_widening_mul);
1520 NEXT_PASS (pass_tail_calls);
1521 NEXT_PASS (pass_rename_ssa_copies);
1522 NEXT_PASS (pass_uncprop);
1523 NEXT_PASS (pass_local_pure_const);
1525 NEXT_PASS (pass_all_optimizations_g);
1527 struct opt_pass **p = &pass_all_optimizations_g.pass.sub;
1528 NEXT_PASS (pass_remove_cgraph_callee_edges);
1529 NEXT_PASS (pass_strip_predict_hints);
1530 /* Lower remaining pieces of GIMPLE. */
1531 NEXT_PASS (pass_lower_complex);
1532 NEXT_PASS (pass_lower_vector_ssa);
1533 /* Perform simple scalar cleanup which is constant/copy propagation. */
1534 NEXT_PASS (pass_ccp);
1535 NEXT_PASS (pass_object_sizes);
1536 /* Copy propagation also copy-propagates constants, this is necessary
1537 to forward object-size results properly. */
1538 NEXT_PASS (pass_copy_prop);
1539 NEXT_PASS (pass_rename_ssa_copies);
1540 NEXT_PASS (pass_dce);
1541 /* Fold remaining builtins. */
1542 NEXT_PASS (pass_fold_builtins);
1543 /* ??? We do want some kind of loop invariant motion, but we possibly
1544 need to adjust LIM to be more friendly towards preserving accurate
1545 debug information here. */
1546 NEXT_PASS (pass_late_warn_uninitialized);
1547 NEXT_PASS (pass_uncprop);
1548 NEXT_PASS (pass_local_pure_const);
1550 NEXT_PASS (pass_tm_init);
1552 struct opt_pass **p = &pass_tm_init.pass.sub;
1553 NEXT_PASS (pass_tm_mark);
1554 NEXT_PASS (pass_tm_memopt);
1555 NEXT_PASS (pass_tm_edges);
1557 NEXT_PASS (pass_lower_complex_O0);
1558 NEXT_PASS (pass_asan_O0);
1559 NEXT_PASS (pass_cleanup_eh);
1560 NEXT_PASS (pass_lower_resx);
1561 NEXT_PASS (pass_nrv);
1562 NEXT_PASS (pass_mudflap_2);
1563 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
1564 NEXT_PASS (pass_warn_function_noreturn);
1566 NEXT_PASS (pass_expand);
1568 NEXT_PASS (pass_rest_of_compilation);
1570 struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
1571 NEXT_PASS (pass_instantiate_virtual_regs);
1572 NEXT_PASS (pass_into_cfg_layout_mode);
1573 NEXT_PASS (pass_jump);
1574 NEXT_PASS (pass_lower_subreg);
1575 NEXT_PASS (pass_df_initialize_opt);
1576 NEXT_PASS (pass_cse);
1577 NEXT_PASS (pass_rtl_fwprop);
1578 NEXT_PASS (pass_rtl_cprop);
1579 NEXT_PASS (pass_rtl_pre);
1580 NEXT_PASS (pass_rtl_hoist);
1581 NEXT_PASS (pass_rtl_cprop);
1582 NEXT_PASS (pass_rtl_store_motion);
1583 NEXT_PASS (pass_cse_after_global_opts);
1584 NEXT_PASS (pass_rtl_ifcvt);
1585 NEXT_PASS (pass_reginfo_init);
1586 /* Perform loop optimizations. It might be better to do them a bit
1587 sooner, but we want the profile feedback to work more
1588 efficiently. */
1589 NEXT_PASS (pass_loop2);
1591 struct opt_pass **p = &pass_loop2.pass.sub;
1592 NEXT_PASS (pass_rtl_loop_init);
1593 NEXT_PASS (pass_rtl_move_loop_invariants);
1594 NEXT_PASS (pass_rtl_unswitch);
1595 NEXT_PASS (pass_rtl_unroll_and_peel_loops);
1596 NEXT_PASS (pass_rtl_doloop);
1597 NEXT_PASS (pass_rtl_loop_done);
1598 *p = NULL;
1600 NEXT_PASS (pass_web);
1601 NEXT_PASS (pass_rtl_cprop);
1602 NEXT_PASS (pass_cse2);
1603 NEXT_PASS (pass_rtl_dse1);
1604 NEXT_PASS (pass_rtl_fwprop_addr);
1605 NEXT_PASS (pass_inc_dec);
1606 NEXT_PASS (pass_initialize_regs);
1607 NEXT_PASS (pass_ud_rtl_dce);
1608 NEXT_PASS (pass_combine);
1609 NEXT_PASS (pass_if_after_combine);
1610 NEXT_PASS (pass_partition_blocks);
1611 NEXT_PASS (pass_regmove);
1612 NEXT_PASS (pass_outof_cfg_layout_mode);
1613 NEXT_PASS (pass_split_all_insns);
1614 NEXT_PASS (pass_lower_subreg2);
1615 NEXT_PASS (pass_df_initialize_no_opt);
1616 NEXT_PASS (pass_stack_ptr_mod);
1617 NEXT_PASS (pass_mode_switching);
1618 NEXT_PASS (pass_match_asm_constraints);
1619 NEXT_PASS (pass_sms);
1620 NEXT_PASS (pass_sched);
1621 NEXT_PASS (pass_ira);
1622 NEXT_PASS (pass_reload);
1623 NEXT_PASS (pass_postreload);
1625 struct opt_pass **p = &pass_postreload.pass.sub;
1626 NEXT_PASS (pass_postreload_cse);
1627 NEXT_PASS (pass_gcse2);
1628 NEXT_PASS (pass_split_after_reload);
1629 NEXT_PASS (pass_ree);
1630 NEXT_PASS (pass_compare_elim_after_reload);
1631 NEXT_PASS (pass_branch_target_load_optimize1);
1632 NEXT_PASS (pass_thread_prologue_and_epilogue);
1633 NEXT_PASS (pass_rtl_dse2);
1634 NEXT_PASS (pass_stack_adjustments);
1635 NEXT_PASS (pass_jump2);
1636 NEXT_PASS (pass_peephole2);
1637 NEXT_PASS (pass_if_after_reload);
1638 NEXT_PASS (pass_regrename);
1639 NEXT_PASS (pass_cprop_hardreg);
1640 NEXT_PASS (pass_fast_rtl_dce);
1641 NEXT_PASS (pass_reorder_blocks);
1642 NEXT_PASS (pass_branch_target_load_optimize2);
1643 NEXT_PASS (pass_leaf_regs);
1644 NEXT_PASS (pass_split_before_sched2);
1645 NEXT_PASS (pass_sched2);
1646 NEXT_PASS (pass_stack_regs);
1648 struct opt_pass **p = &pass_stack_regs.pass.sub;
1649 NEXT_PASS (pass_split_before_regstack);
1650 NEXT_PASS (pass_stack_regs_run);
1652 NEXT_PASS (pass_compute_alignments);
1653 NEXT_PASS (pass_duplicate_computed_gotos);
1654 NEXT_PASS (pass_variable_tracking);
1655 NEXT_PASS (pass_free_cfg);
1656 NEXT_PASS (pass_machine_reorg);
1657 NEXT_PASS (pass_cleanup_barriers);
1658 NEXT_PASS (pass_delay_slots);
1659 NEXT_PASS (pass_split_for_shorten_branches);
1660 NEXT_PASS (pass_convert_to_eh_region_ranges);
1661 NEXT_PASS (pass_shorten_branches);
1662 NEXT_PASS (pass_set_nothrow_function_flags);
1663 NEXT_PASS (pass_dwarf2_frame);
1664 NEXT_PASS (pass_final);
1666 NEXT_PASS (pass_df_finish);
1668 NEXT_PASS (pass_clean_state);
1669 *p = NULL;
1671 #undef NEXT_PASS
1673 /* Register the passes with the tree dump code. */
1674 register_dump_files (all_lowering_passes, PROP_gimple_any);
1675 register_dump_files (all_small_ipa_passes,
1676 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1677 | PROP_cfg);
1678 register_dump_files (all_regular_ipa_passes,
1679 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1680 | PROP_cfg);
1681 register_dump_files (all_lto_gen_passes,
1682 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1683 | PROP_cfg);
1684 register_dump_files (all_late_ipa_passes,
1685 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1686 | PROP_cfg);
1687 register_dump_files (all_passes,
1688 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1689 | PROP_cfg);
1692 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1693 function CALLBACK for every function in the call graph. Otherwise,
1694 call CALLBACK on the current function. */
1696 static void
1697 do_per_function (void (*callback) (void *data), void *data)
1699 if (current_function_decl)
1700 callback (data);
1701 else
1703 struct cgraph_node *node;
1704 FOR_EACH_DEFINED_FUNCTION (node)
1705 if (gimple_has_body_p (node->symbol.decl)
1706 && (!node->clone_of || node->symbol.decl != node->clone_of->symbol.decl))
1708 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1709 callback (data);
1710 if (!flag_wpa)
1712 free_dominance_info (CDI_DOMINATORS);
1713 free_dominance_info (CDI_POST_DOMINATORS);
1715 pop_cfun ();
1716 ggc_collect ();
1721 /* Because inlining might remove no-longer reachable nodes, we need to
1722 keep the array visible to garbage collector to avoid reading collected
1723 out nodes. */
1724 static int nnodes;
1725 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1727 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1728 function CALLBACK for every function in the call graph. Otherwise,
1729 call CALLBACK on the current function.
1730 This function is global so that plugins can use it. */
1731 void
1732 do_per_function_toporder (void (*callback) (void *data), void *data)
1734 int i;
1736 if (current_function_decl)
1737 callback (data);
1738 else
1740 gcc_assert (!order);
1741 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1742 nnodes = ipa_reverse_postorder (order);
1743 for (i = nnodes - 1; i >= 0; i--)
1744 order[i]->process = 1;
1745 for (i = nnodes - 1; i >= 0; i--)
1747 struct cgraph_node *node = order[i];
1749 /* Allow possibly removed nodes to be garbage collected. */
1750 order[i] = NULL;
1751 node->process = 0;
1752 if (cgraph_function_with_gimple_body_p (node))
1754 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1755 callback (data);
1756 free_dominance_info (CDI_DOMINATORS);
1757 free_dominance_info (CDI_POST_DOMINATORS);
1758 pop_cfun ();
1759 ggc_collect ();
1763 ggc_free (order);
1764 order = NULL;
1765 nnodes = 0;
1768 /* Helper function to perform function body dump. */
1770 static void
1771 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1773 if (dump_file && current_function_decl)
1775 if (cfun->curr_properties & PROP_trees)
1776 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1777 else
1779 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1781 if ((cfun->curr_properties & PROP_cfg)
1782 && graph_dump_format != no_graph
1783 && (dump_flags & TDF_GRAPH))
1784 print_rtl_graph_with_bb (dump_file_name, get_insns ());
1787 /* Flush the file. If verification fails, we won't be able to
1788 close the file before aborting. */
1789 fflush (dump_file);
1793 static struct profile_record *profile_record;
1795 /* Do profile consistency book-keeping for the pass with static number INDEX.
1796 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1797 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1798 if we are only book-keeping on passes that may have selectively disabled
1799 themselves on a given function. */
1800 static void
1801 check_profile_consistency (int index, int subpass, bool run)
1803 if (index == -1)
1804 return;
1805 if (!profile_record)
1806 profile_record = XCNEWVEC (struct profile_record,
1807 passes_by_id_size);
1808 gcc_assert (index < passes_by_id_size && index >= 0);
1809 gcc_assert (subpass < 2);
1810 profile_record[index].run |= run;
1811 account_profile_record (&profile_record[index], subpass);
1814 /* Output profile consistency. */
1816 void
1817 dump_profile_report (void)
1819 int i, j;
1820 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1821 gcov_type last_time = 0, last_size = 0;
1822 double rel_time_change, rel_size_change;
1823 int last_reported = 0;
1825 if (!profile_record)
1826 return;
1827 fprintf (stderr, "\nProfile consistency report:\n\n");
1828 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1829 fprintf (stderr, " |freq count |freq count |size time\n");
1831 for (i = 0; i < passes_by_id_size; i++)
1832 for (j = 0 ; j < 2; j++)
1833 if (profile_record[i].run)
1835 if (last_time)
1836 rel_time_change = (profile_record[i].time[j]
1837 - (double)last_time) * 100 / (double)last_time;
1838 else
1839 rel_time_change = 0;
1840 if (last_size)
1841 rel_size_change = (profile_record[i].size[j]
1842 - (double)last_size) * 100 / (double)last_size;
1843 else
1844 rel_size_change = 0;
1846 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1847 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1848 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1849 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1850 || rel_time_change || rel_size_change)
1852 last_reported = i;
1853 fprintf (stderr, "%-20s %s",
1854 passes_by_id [i]->name,
1855 j ? "(after TODO)" : " ");
1856 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1857 fprintf (stderr, "| %+5i",
1858 profile_record[i].num_mismatched_freq_in[j]
1859 - last_freq_in);
1860 else
1861 fprintf (stderr, "| ");
1862 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1863 fprintf (stderr, " %+5i",
1864 profile_record[i].num_mismatched_count_in[j]
1865 - last_count_in);
1866 else
1867 fprintf (stderr, " ");
1868 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1869 fprintf (stderr, "| %+5i",
1870 profile_record[i].num_mismatched_freq_out[j]
1871 - last_freq_out);
1872 else
1873 fprintf (stderr, "| ");
1874 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1875 fprintf (stderr, " %+5i",
1876 profile_record[i].num_mismatched_count_out[j]
1877 - last_count_out);
1878 else
1879 fprintf (stderr, " ");
1881 /* Size/time units change across gimple and RTL. */
1882 if (i == pass_expand.pass.static_pass_number)
1883 fprintf (stderr, "|----------");
1884 else
1886 if (rel_size_change)
1887 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1888 else
1889 fprintf (stderr, "| ");
1890 if (rel_time_change)
1891 fprintf (stderr, " %+8.4f%%", rel_time_change);
1893 fprintf (stderr, "\n");
1894 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1895 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1896 last_count_in = profile_record[i].num_mismatched_count_in[j];
1897 last_count_out = profile_record[i].num_mismatched_count_out[j];
1899 else if (j && last_reported != i)
1901 last_reported = i;
1902 fprintf (stderr, "%-20s ------------| | |\n",
1903 passes_by_id [i]->name);
1905 last_time = profile_record[i].time[j];
1906 last_size = profile_record[i].size[j];
1910 /* Perform all TODO actions that ought to be done on each function. */
1912 static void
1913 execute_function_todo (void *data)
1915 unsigned int flags = (size_t)data;
1916 flags &= ~cfun->last_verified;
1917 if (!flags)
1918 return;
1920 /* Always cleanup the CFG before trying to update SSA. */
1921 if (flags & TODO_cleanup_cfg)
1923 bool cleanup = cleanup_tree_cfg ();
1925 if (cleanup && (cfun->curr_properties & PROP_ssa))
1926 flags |= TODO_remove_unused_locals;
1928 /* When cleanup_tree_cfg merges consecutive blocks, it may
1929 perform some simplistic propagation when removing single
1930 valued PHI nodes. This propagation may, in turn, cause the
1931 SSA form to become out-of-date (see PR 22037). So, even
1932 if the parent pass had not scheduled an SSA update, we may
1933 still need to do one. */
1934 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1935 flags |= TODO_update_ssa;
1938 if (flags & TODO_update_ssa_any)
1940 unsigned update_flags = flags & TODO_update_ssa_any;
1941 update_ssa (update_flags);
1942 cfun->last_verified &= ~TODO_verify_ssa;
1945 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1946 compute_may_aliases ();
1948 if (optimize && (flags & TODO_update_address_taken))
1949 execute_update_addresses_taken ();
1951 if (flags & TODO_remove_unused_locals)
1952 remove_unused_locals ();
1954 if (flags & TODO_rebuild_frequencies)
1955 rebuild_frequencies ();
1957 if (flags & TODO_rebuild_cgraph_edges)
1958 rebuild_cgraph_edges ();
1960 /* If we've seen errors do not bother running any verifiers. */
1961 if (seen_error ())
1962 return;
1964 #if defined ENABLE_CHECKING
1965 if (flags & TODO_verify_ssa
1966 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1968 verify_gimple_in_cfg (cfun);
1969 verify_ssa (true);
1971 else if (flags & TODO_verify_stmts)
1972 verify_gimple_in_cfg (cfun);
1973 if (flags & TODO_verify_flow)
1974 verify_flow_info ();
1975 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1976 verify_loop_closed_ssa (false);
1977 if (flags & TODO_verify_rtl_sharing)
1978 verify_rtl_sharing ();
1979 #endif
1981 cfun->last_verified = flags & TODO_verify_all;
1984 /* Perform all TODO actions. */
1985 static void
1986 execute_todo (unsigned int flags)
1988 #if defined ENABLE_CHECKING
1989 if (cfun
1990 && need_ssa_update_p (cfun))
1991 gcc_assert (flags & TODO_update_ssa_any);
1992 #endif
1994 timevar_push (TV_TODO);
1996 /* Inform the pass whether it is the first time it is run. */
1997 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1999 statistics_fini_pass ();
2001 do_per_function (execute_function_todo, (void *)(size_t) flags);
2003 /* Always remove functions just as before inlining: IPA passes might be
2004 interested to see bodies of extern inline functions that are not inlined
2005 to analyze side effects. The full removal is done just at the end
2006 of IPA pass queue. */
2007 if (flags & TODO_remove_functions)
2009 gcc_assert (!cfun);
2010 symtab_remove_unreachable_nodes (true, dump_file);
2013 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2015 gcc_assert (!cfun);
2016 dump_symtab (dump_file);
2017 /* Flush the file. If verification fails, we won't be able to
2018 close the file before aborting. */
2019 fflush (dump_file);
2022 if (flags & TODO_ggc_collect)
2023 ggc_collect ();
2025 /* Now that the dumping has been done, we can get rid of the optional
2026 df problems. */
2027 if (flags & TODO_df_finish)
2028 df_finish_pass ((flags & TODO_df_verify) != 0);
2030 timevar_pop (TV_TODO);
2033 /* Verify invariants that should hold between passes. This is a place
2034 to put simple sanity checks. */
2036 static void
2037 verify_interpass_invariants (void)
2039 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2042 /* Clear the last verified flag. */
2044 static void
2045 clear_last_verified (void *data ATTRIBUTE_UNUSED)
2047 cfun->last_verified = 0;
2050 /* Helper function. Verify that the properties has been turn into the
2051 properties expected by the pass. */
2053 #ifdef ENABLE_CHECKING
2054 static void
2055 verify_curr_properties (void *data)
2057 unsigned int props = (size_t)data;
2058 gcc_assert ((cfun->curr_properties & props) == props);
2060 #endif
2062 /* Initialize pass dump file. */
2063 /* This is non-static so that the plugins can use it. */
2065 bool
2066 pass_init_dump_file (struct opt_pass *pass)
2068 /* If a dump file name is present, open it if enabled. */
2069 if (pass->static_pass_number != -1)
2071 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
2072 dump_file_name = get_dump_file_name (pass->static_pass_number);
2073 dump_start (pass->static_pass_number, &dump_flags);
2074 if (dump_file && current_function_decl)
2075 dump_function_header (dump_file, current_function_decl, dump_flags);
2076 return initializing_dump;
2078 else
2079 return false;
2082 /* Flush PASS dump file. */
2083 /* This is non-static so that plugins can use it. */
2085 void
2086 pass_fini_dump_file (struct opt_pass *pass)
2088 /* Flush and close dump file. */
2089 if (dump_file_name)
2091 free (CONST_CAST (char *, dump_file_name));
2092 dump_file_name = NULL;
2095 dump_finish (pass->static_pass_number);
2098 /* After executing the pass, apply expected changes to the function
2099 properties. */
2101 static void
2102 update_properties_after_pass (void *data)
2104 struct opt_pass *pass = (struct opt_pass *) data;
2105 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
2106 & ~pass->properties_destroyed;
2109 /* Execute summary generation for all of the passes in IPA_PASS. */
2111 void
2112 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
2114 while (ipa_pass)
2116 struct opt_pass *pass = &ipa_pass->pass;
2118 /* Execute all of the IPA_PASSes in the list. */
2119 if (ipa_pass->pass.type == IPA_PASS
2120 && (!pass->gate || pass->gate ())
2121 && ipa_pass->generate_summary)
2123 pass_init_dump_file (pass);
2125 /* If a timevar is present, start it. */
2126 if (pass->tv_id)
2127 timevar_push (pass->tv_id);
2129 ipa_pass->generate_summary ();
2131 /* Stop timevar. */
2132 if (pass->tv_id)
2133 timevar_pop (pass->tv_id);
2135 pass_fini_dump_file (pass);
2137 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
2141 /* Execute IPA_PASS function transform on NODE. */
2143 static void
2144 execute_one_ipa_transform_pass (struct cgraph_node *node,
2145 struct ipa_opt_pass_d *ipa_pass)
2147 struct opt_pass *pass = &ipa_pass->pass;
2148 unsigned int todo_after = 0;
2150 current_pass = pass;
2151 if (!ipa_pass->function_transform)
2152 return;
2154 /* Note that the folders should only create gimple expressions.
2155 This is a hack until the new folder is ready. */
2156 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2158 pass_init_dump_file (pass);
2160 /* Run pre-pass verification. */
2161 execute_todo (ipa_pass->function_transform_todo_flags_start);
2163 /* If a timevar is present, start it. */
2164 if (pass->tv_id != TV_NONE)
2165 timevar_push (pass->tv_id);
2167 /* Do it! */
2168 todo_after = ipa_pass->function_transform (node);
2170 /* Stop timevar. */
2171 if (pass->tv_id != TV_NONE)
2172 timevar_pop (pass->tv_id);
2174 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2175 check_profile_consistency (pass->static_pass_number, 0, true);
2177 /* Run post-pass cleanup and verification. */
2178 execute_todo (todo_after);
2179 verify_interpass_invariants ();
2180 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2181 check_profile_consistency (pass->static_pass_number, 1, true);
2183 do_per_function (execute_function_dump, NULL);
2184 pass_fini_dump_file (pass);
2186 current_pass = NULL;
2189 /* For the current function, execute all ipa transforms. */
2191 void
2192 execute_all_ipa_transforms (void)
2194 struct cgraph_node *node;
2195 if (!cfun)
2196 return;
2197 node = cgraph_get_node (current_function_decl);
2199 if (node->ipa_transforms_to_apply.exists ())
2201 unsigned int i;
2203 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2204 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2205 node->ipa_transforms_to_apply.release ();
2209 /* Callback for do_per_function to apply all IPA transforms. */
2211 static void
2212 apply_ipa_transforms (void *data)
2214 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2215 if (!node->global.inlined_to && node->ipa_transforms_to_apply.exists ())
2217 *(bool *)data = true;
2218 execute_all_ipa_transforms();
2219 rebuild_cgraph_edges ();
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 (struct 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 (struct opt_pass *pass)
2252 bool initializing_dump;
2253 unsigned int todo_after = 0;
2255 bool gate_status;
2257 /* IPA passes are executed on whole program, so cfun should be NULL.
2258 Other passes need function context set. */
2259 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2260 gcc_assert (!cfun && !current_function_decl);
2261 else
2262 gcc_assert (cfun && current_function_decl);
2264 current_pass = pass;
2266 /* Check whether gate check should be avoided.
2267 User controls the value of the gate through the parameter "gate_status". */
2268 gate_status = (pass->gate == NULL) ? true : pass->gate();
2269 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2271 /* Override gate with plugin. */
2272 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2274 if (!gate_status)
2276 /* Run so passes selectively disabling themselves on a given function
2277 are not miscounted. */
2278 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2280 check_profile_consistency (pass->static_pass_number, 0, false);
2281 check_profile_consistency (pass->static_pass_number, 1, false);
2283 current_pass = NULL;
2284 return false;
2287 /* Pass execution event trigger: useful to identify passes being
2288 executed. */
2289 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2291 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2292 Apply all trnasforms first. */
2293 if (pass->type == SIMPLE_IPA_PASS)
2295 bool applied = false;
2296 do_per_function (apply_ipa_transforms, (void *)&applied);
2297 if (applied)
2298 symtab_remove_unreachable_nodes (true, dump_file);
2299 /* Restore current_pass. */
2300 current_pass = pass;
2303 if (!quiet_flag && !cfun)
2304 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2306 /* Note that the folders should only create gimple expressions.
2307 This is a hack until the new folder is ready. */
2308 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2310 initializing_dump = pass_init_dump_file (pass);
2312 /* Run pre-pass verification. */
2313 execute_todo (pass->todo_flags_start);
2315 #ifdef ENABLE_CHECKING
2316 do_per_function (verify_curr_properties,
2317 (void *)(size_t)pass->properties_required);
2318 #endif
2320 /* If a timevar is present, start it. */
2321 if (pass->tv_id != TV_NONE)
2322 timevar_push (pass->tv_id);
2324 /* Do it! */
2325 if (pass->execute)
2327 todo_after = pass->execute ();
2328 do_per_function (clear_last_verified, NULL);
2331 /* Stop timevar. */
2332 if (pass->tv_id != TV_NONE)
2333 timevar_pop (pass->tv_id);
2335 do_per_function (update_properties_after_pass, pass);
2337 if (initializing_dump
2338 && dump_file
2339 && graph_dump_format != no_graph
2340 && cfun
2341 && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
2342 == (PROP_cfg | PROP_rtl))
2344 get_dump_file_info (pass->static_pass_number)->pflags |= TDF_GRAPH;
2345 dump_flags |= TDF_GRAPH;
2346 clean_graph_dump_file (dump_file_name);
2349 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2350 check_profile_consistency (pass->static_pass_number, 0, true);
2352 /* Run post-pass cleanup and verification. */
2353 execute_todo (todo_after | pass->todo_flags_finish);
2354 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2355 check_profile_consistency (pass->static_pass_number, 1, true);
2357 verify_interpass_invariants ();
2358 do_per_function (execute_function_dump, NULL);
2359 if (pass->type == IPA_PASS)
2361 struct cgraph_node *node;
2362 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2363 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *)pass);
2366 if (!current_function_decl)
2367 cgraph_process_new_functions ();
2369 pass_fini_dump_file (pass);
2371 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2372 gcc_assert (!(cfun->curr_properties & PROP_trees)
2373 || pass->type != RTL_PASS);
2375 current_pass = NULL;
2377 return true;
2380 void
2381 execute_pass_list (struct opt_pass *pass)
2385 gcc_assert (pass->type == GIMPLE_PASS
2386 || pass->type == RTL_PASS);
2387 if (execute_one_pass (pass) && pass->sub)
2388 execute_pass_list (pass->sub);
2389 pass = pass->next;
2391 while (pass);
2394 /* Same as execute_pass_list but assume that subpasses of IPA passes
2395 are local passes. If SET is not NULL, write out summaries of only
2396 those node in SET. */
2398 static void
2399 ipa_write_summaries_2 (struct opt_pass *pass, struct lto_out_decl_state *state)
2401 while (pass)
2403 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2404 gcc_assert (!current_function_decl);
2405 gcc_assert (!cfun);
2406 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2407 if (pass->type == IPA_PASS
2408 && ipa_pass->write_summary
2409 && (!pass->gate || pass->gate ()))
2411 /* If a timevar is present, start it. */
2412 if (pass->tv_id)
2413 timevar_push (pass->tv_id);
2415 pass_init_dump_file (pass);
2417 ipa_pass->write_summary ();
2419 pass_fini_dump_file (pass);
2421 /* If a timevar is present, start it. */
2422 if (pass->tv_id)
2423 timevar_pop (pass->tv_id);
2426 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2427 ipa_write_summaries_2 (pass->sub, state);
2429 pass = pass->next;
2433 /* Helper function of ipa_write_summaries. Creates and destroys the
2434 decl state and calls ipa_write_summaries_2 for all passes that have
2435 summaries. SET is the set of nodes to be written. */
2437 static void
2438 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2440 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2441 state->symtab_node_encoder = encoder;
2443 lto_push_out_decl_state (state);
2445 gcc_assert (!flag_wpa);
2446 ipa_write_summaries_2 (all_regular_ipa_passes, state);
2447 ipa_write_summaries_2 (all_lto_gen_passes, state);
2449 gcc_assert (lto_get_out_decl_state () == state);
2450 lto_pop_out_decl_state ();
2451 lto_delete_out_decl_state (state);
2454 /* Write out summaries for all the nodes in the callgraph. */
2456 void
2457 ipa_write_summaries (void)
2459 lto_symtab_encoder_t encoder;
2460 int i, order_pos;
2461 struct varpool_node *vnode;
2462 struct cgraph_node **order;
2464 if (!flag_generate_lto || seen_error ())
2465 return;
2467 encoder = lto_symtab_encoder_new (false);
2469 /* Create the callgraph set in the same order used in
2470 cgraph_expand_all_functions. This mostly facilitates debugging,
2471 since it causes the gimple file to be processed in the same order
2472 as the source code. */
2473 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2474 order_pos = ipa_reverse_postorder (order);
2475 gcc_assert (order_pos == cgraph_n_nodes);
2477 for (i = order_pos - 1; i >= 0; i--)
2479 struct cgraph_node *node = order[i];
2481 if (cgraph_function_with_gimple_body_p (node))
2483 /* When streaming out references to statements as part of some IPA
2484 pass summary, the statements need to have uids assigned and the
2485 following does that for all the IPA passes here. Naturally, this
2486 ordering then matches the one IPA-passes get in their stmt_fixup
2487 hooks. */
2489 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2490 renumber_gimple_stmt_uids ();
2491 pop_cfun ();
2493 if (node->analyzed)
2494 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
2497 FOR_EACH_DEFINED_VARIABLE (vnode)
2498 if ((!vnode->alias || vnode->alias_of))
2499 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
2501 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2503 free (order);
2506 /* Same as execute_pass_list but assume that subpasses of IPA passes
2507 are local passes. If SET is not NULL, write out optimization summaries of
2508 only those node in SET. */
2510 static void
2511 ipa_write_optimization_summaries_1 (struct opt_pass *pass, struct lto_out_decl_state *state)
2513 while (pass)
2515 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2516 gcc_assert (!current_function_decl);
2517 gcc_assert (!cfun);
2518 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2519 if (pass->type == IPA_PASS
2520 && ipa_pass->write_optimization_summary
2521 && (!pass->gate || pass->gate ()))
2523 /* If a timevar is present, start it. */
2524 if (pass->tv_id)
2525 timevar_push (pass->tv_id);
2527 pass_init_dump_file (pass);
2529 ipa_pass->write_optimization_summary ();
2531 pass_fini_dump_file (pass);
2533 /* If a timevar is present, start it. */
2534 if (pass->tv_id)
2535 timevar_pop (pass->tv_id);
2538 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2539 ipa_write_optimization_summaries_1 (pass->sub, state);
2541 pass = pass->next;
2545 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2546 NULL, write out all summaries of all nodes. */
2548 void
2549 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2551 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2552 lto_symtab_encoder_iterator lsei;
2553 state->symtab_node_encoder = encoder;
2555 lto_push_out_decl_state (state);
2556 for (lsei = lsei_start_function_in_partition (encoder);
2557 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2559 struct cgraph_node *node = lsei_cgraph_node (lsei);
2560 /* When streaming out references to statements as part of some IPA
2561 pass summary, the statements need to have uids assigned.
2563 For functions newly born at WPA stage we need to initialize
2564 the uids here. */
2565 if (node->analyzed
2566 && gimple_has_body_p (node->symbol.decl))
2568 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2569 renumber_gimple_stmt_uids ();
2570 pop_cfun ();
2574 gcc_assert (flag_wpa);
2575 ipa_write_optimization_summaries_1 (all_regular_ipa_passes, state);
2576 ipa_write_optimization_summaries_1 (all_lto_gen_passes, state);
2578 gcc_assert (lto_get_out_decl_state () == state);
2579 lto_pop_out_decl_state ();
2580 lto_delete_out_decl_state (state);
2583 /* Same as execute_pass_list but assume that subpasses of IPA passes
2584 are local passes. */
2586 static void
2587 ipa_read_summaries_1 (struct opt_pass *pass)
2589 while (pass)
2591 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2593 gcc_assert (!current_function_decl);
2594 gcc_assert (!cfun);
2595 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2597 if (pass->gate == NULL || pass->gate ())
2599 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2601 /* If a timevar is present, start it. */
2602 if (pass->tv_id)
2603 timevar_push (pass->tv_id);
2605 pass_init_dump_file (pass);
2607 ipa_pass->read_summary ();
2609 pass_fini_dump_file (pass);
2611 /* Stop timevar. */
2612 if (pass->tv_id)
2613 timevar_pop (pass->tv_id);
2616 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2617 ipa_read_summaries_1 (pass->sub);
2619 pass = pass->next;
2624 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2626 void
2627 ipa_read_summaries (void)
2629 ipa_read_summaries_1 (all_regular_ipa_passes);
2630 ipa_read_summaries_1 (all_lto_gen_passes);
2633 /* Same as execute_pass_list but assume that subpasses of IPA passes
2634 are local passes. */
2636 static void
2637 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2639 while (pass)
2641 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2643 gcc_assert (!current_function_decl);
2644 gcc_assert (!cfun);
2645 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2647 if (pass->gate == NULL || pass->gate ())
2649 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2651 /* If a timevar is present, start it. */
2652 if (pass->tv_id)
2653 timevar_push (pass->tv_id);
2655 pass_init_dump_file (pass);
2657 ipa_pass->read_optimization_summary ();
2659 pass_fini_dump_file (pass);
2661 /* Stop timevar. */
2662 if (pass->tv_id)
2663 timevar_pop (pass->tv_id);
2666 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2667 ipa_read_optimization_summaries_1 (pass->sub);
2669 pass = pass->next;
2673 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2675 void
2676 ipa_read_optimization_summaries (void)
2678 ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
2679 ipa_read_optimization_summaries_1 (all_lto_gen_passes);
2682 /* Same as execute_pass_list but assume that subpasses of IPA passes
2683 are local passes. */
2684 void
2685 execute_ipa_pass_list (struct opt_pass *pass)
2689 gcc_assert (!current_function_decl);
2690 gcc_assert (!cfun);
2691 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2692 if (execute_one_pass (pass) && pass->sub)
2694 if (pass->sub->type == GIMPLE_PASS)
2696 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2697 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2698 pass->sub);
2699 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2701 else if (pass->sub->type == SIMPLE_IPA_PASS
2702 || pass->sub->type == IPA_PASS)
2703 execute_ipa_pass_list (pass->sub);
2704 else
2705 gcc_unreachable ();
2707 gcc_assert (!current_function_decl);
2708 cgraph_process_new_functions ();
2709 pass = pass->next;
2711 while (pass);
2714 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2716 static void
2717 execute_ipa_stmt_fixups (struct opt_pass *pass,
2718 struct cgraph_node *node, gimple *stmts)
2720 while (pass)
2722 /* Execute all of the IPA_PASSes in the list. */
2723 if (pass->type == IPA_PASS
2724 && (!pass->gate || pass->gate ()))
2726 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2728 if (ipa_pass->stmt_fixup)
2730 pass_init_dump_file (pass);
2731 /* If a timevar is present, start it. */
2732 if (pass->tv_id)
2733 timevar_push (pass->tv_id);
2735 ipa_pass->stmt_fixup (node, stmts);
2737 /* Stop timevar. */
2738 if (pass->tv_id)
2739 timevar_pop (pass->tv_id);
2740 pass_fini_dump_file (pass);
2742 if (pass->sub)
2743 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2745 pass = pass->next;
2749 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2751 void
2752 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2754 execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
2758 extern void debug_properties (unsigned int);
2759 extern void dump_properties (FILE *, unsigned int);
2761 DEBUG_FUNCTION void
2762 dump_properties (FILE *dump, unsigned int props)
2764 fprintf (dump, "Properties:\n");
2765 if (props & PROP_gimple_any)
2766 fprintf (dump, "PROP_gimple_any\n");
2767 if (props & PROP_gimple_lcf)
2768 fprintf (dump, "PROP_gimple_lcf\n");
2769 if (props & PROP_gimple_leh)
2770 fprintf (dump, "PROP_gimple_leh\n");
2771 if (props & PROP_cfg)
2772 fprintf (dump, "PROP_cfg\n");
2773 if (props & PROP_ssa)
2774 fprintf (dump, "PROP_ssa\n");
2775 if (props & PROP_no_crit_edges)
2776 fprintf (dump, "PROP_no_crit_edges\n");
2777 if (props & PROP_rtl)
2778 fprintf (dump, "PROP_rtl\n");
2779 if (props & PROP_gimple_lomp)
2780 fprintf (dump, "PROP_gimple_lomp\n");
2781 if (props & PROP_gimple_lcx)
2782 fprintf (dump, "PROP_gimple_lcx\n");
2783 if (props & PROP_cfglayout)
2784 fprintf (dump, "PROP_cfglayout\n");
2787 DEBUG_FUNCTION void
2788 debug_properties (unsigned int props)
2790 dump_properties (stderr, props);
2793 /* Called by local passes to see if function is called by already processed nodes.
2794 Because we process nodes in topological order, this means that function is
2795 in recursive cycle or we introduced new direct calls. */
2796 bool
2797 function_called_by_processed_nodes_p (void)
2799 struct cgraph_edge *e;
2800 for (e = cgraph_get_node (current_function_decl)->callers;
2802 e = e->next_caller)
2804 if (e->caller->symbol.decl == current_function_decl)
2805 continue;
2806 if (!cgraph_function_with_gimple_body_p (e->caller))
2807 continue;
2808 if (TREE_ASM_WRITTEN (e->caller->symbol.decl))
2809 continue;
2810 if (!e->caller->process && !e->caller->global.inlined_to)
2811 break;
2813 if (dump_file && e)
2815 fprintf (dump_file, "Already processed call to:\n");
2816 dump_cgraph_node (dump_file, e->caller);
2818 return e != NULL;
2821 #include "gt-passes.h"