Backport r203445 from v17
[official-gcc.git] / gcc-4_8 / gcc / passes.c
blob279dd7e0755c7f558bcfc0cbae4be2d94c082e6d
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This is the top level of cc1/c++.
21 It parses command args, opens files, invokes the various passes
22 in the proper order, and counts the time used by each.
23 Error messages and low-level interface to malloc also handled here. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "line-map.h"
30 #include "input.h"
31 #include "tree.h"
32 #include "rtl.h"
33 #include "tm_p.h"
34 #include "flags.h"
35 #include "insn-attr.h"
36 #include "insn-config.h"
37 #include "insn-flags.h"
38 #include "hard-reg-set.h"
39 #include "recog.h"
40 #include "output.h"
41 #include "except.h"
42 #include "function.h"
43 #include "toplev.h"
44 #include "expr.h"
45 #include "basic-block.h"
46 #include "intl.h"
47 #include "ggc.h"
48 #include "graph.h"
49 #include "regs.h"
50 #include "diagnostic-core.h"
51 #include "params.h"
52 #include "reload.h"
53 #include "debug.h"
54 #include "target.h"
55 #include "langhooks.h"
56 #include "cfgloop.h"
57 #include "hosthooks.h"
58 #include "cgraph.h"
59 #include "opts.h"
60 #include "coverage.h"
61 #include "value-prof.h"
62 #include "tree-inline.h"
63 #include "tree-flow.h"
64 #include "tree-pass.h"
65 #include "tree-dump.h"
66 #include "df.h"
67 #include "predict.h"
68 #include "lto-streamer.h"
69 #include "plugin.h"
70 #include "l-ipo.h"
71 #include "ipa-utils.h"
72 #include "tree-pretty-print.h" /* for dump_function_header */
74 /* This is used for debugging. It allows the current pass to printed
75 from anywhere in compilation.
76 The variable current_pass is also used for statistics and plugins. */
77 struct opt_pass *current_pass;
79 static void register_pass_name (struct opt_pass *, const char *);
81 /* Call from anywhere to find out what pass this is. Useful for
82 printing out debugging information deep inside an service
83 routine. */
84 void
85 print_current_pass (FILE *file)
87 if (current_pass)
88 fprintf (file, "current pass = %s (%d)\n",
89 current_pass->name, current_pass->static_pass_number);
90 else
91 fprintf (file, "no current pass.\n");
95 /* Call from the debugger to get the current pass name. */
96 DEBUG_FUNCTION void
97 debug_pass (void)
99 print_current_pass (stderr);
104 /* Global variables used to communicate with passes. */
105 bool in_gimple_form;
106 bool first_pass_instance;
109 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
110 and TYPE_DECL nodes.
112 This does nothing for local (non-static) variables, unless the
113 variable is a register variable with DECL_ASSEMBLER_NAME set. In
114 that case, or if the variable is not an automatic, it sets up the
115 RTL and outputs any assembler code (label definition, storage
116 allocation and initialization).
118 DECL is the declaration. TOP_LEVEL is nonzero
119 if this declaration is not within a function. */
121 void
122 rest_of_decl_compilation (tree decl,
123 int top_level,
124 int at_end)
126 /* We deferred calling assemble_alias so that we could collect
127 other attributes such as visibility. Emit the alias now. */
128 if (!in_lto_p)
130 tree alias;
131 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
132 if (alias)
134 alias = TREE_VALUE (TREE_VALUE (alias));
135 alias = get_identifier (TREE_STRING_POINTER (alias));
136 /* A quirk of the initial implementation of aliases required that the
137 user add "extern" to all of them. Which is silly, but now
138 historical. Do note that the symbol is in fact locally defined. */
139 if (!lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
140 DECL_EXTERNAL (decl) = 0;
141 assemble_alias (decl, alias);
145 /* Can't defer this, because it needs to happen before any
146 later function definitions are processed. */
147 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
148 make_decl_rtl (decl);
150 /* Forward declarations for nested functions are not "external",
151 but we need to treat them as if they were. */
152 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
153 || TREE_CODE (decl) == FUNCTION_DECL)
155 timevar_push (TV_VARCONST);
157 /* Don't output anything when a tentative file-scope definition
158 is seen. But at end of compilation, do output code for them.
160 We do output all variables and rely on
161 callgraph code to defer them except for forward declarations
162 (see gcc.c-torture/compile/920624-1.c) */
163 if ((at_end
164 || !DECL_DEFER_OUTPUT (decl)
165 || DECL_INITIAL (decl))
166 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
167 && !DECL_EXTERNAL (decl))
169 /* When reading LTO unit, we also read varpool, so do not
170 rebuild it. */
171 if (in_lto_p && !at_end)
173 else if (TREE_CODE (decl) != FUNCTION_DECL)
174 varpool_finalize_decl (decl);
177 #ifdef ASM_FINISH_DECLARE_OBJECT
178 if (decl == last_assemble_variable_decl)
180 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
181 top_level, at_end);
183 #endif
184 if (L_IPO_COMP_MODE)
186 /* Create the node early during parsing so
187 that module id can be captured. */
188 if (TREE_CODE (decl) == VAR_DECL)
189 varpool_node_for_decl (decl);
192 timevar_pop (TV_VARCONST);
194 else if (TREE_CODE (decl) == TYPE_DECL
195 /* Like in rest_of_type_compilation, avoid confusing the debug
196 information machinery when there are errors. */
197 && !seen_error ())
199 timevar_push (TV_SYMOUT);
200 debug_hooks->type_decl (decl, !top_level);
201 timevar_pop (TV_SYMOUT);
204 /* Let cgraph know about the existence of variables. */
205 if (in_lto_p && !at_end)
207 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
208 && TREE_STATIC (decl))
209 varpool_node_for_decl (decl);
212 /* Called after finishing a record, union or enumeral type. */
214 void
215 rest_of_type_compilation (tree type, int toplev)
217 /* Avoid confusing the debug information machinery when there are
218 errors. */
219 if (seen_error ())
220 return;
222 timevar_push (TV_SYMOUT);
223 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
224 timevar_pop (TV_SYMOUT);
229 void
230 finish_optimization_passes (void)
232 int i;
233 struct dump_file_info *dfi;
234 char *name;
236 timevar_push (TV_DUMP);
237 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
239 dump_start (pass_profile.pass.static_pass_number, NULL);
240 end_branch_prob ();
241 dump_finish (pass_profile.pass.static_pass_number);
244 if (optimize > 0)
246 dump_start (pass_profile.pass.static_pass_number, NULL);
247 print_combine_total_stats ();
248 dump_finish (pass_profile.pass.static_pass_number);
251 /* Do whatever is necessary to finish printing the graphs. */
252 for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
253 if (dump_initialized_p (i)
254 && (dfi->pflags & TDF_GRAPH) != 0
255 && (name = get_dump_file_name (i)) != NULL)
257 finish_graph_dump_file (name);
258 free (name);
261 timevar_pop (TV_DUMP);
264 static unsigned int
265 execute_all_early_local_passes (void)
267 /* Once this pass (and its sub-passes) are complete, all functions
268 will be in SSA form. Technically this state change is happening
269 a tad early, since the sub-passes have not yet run, but since
270 none of the sub-passes are IPA passes and do not create new
271 functions, this is ok. We're setting this value for the benefit
272 of IPA passes that follow. */
273 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
274 cgraph_state = CGRAPH_STATE_IPA_SSA;
275 return 0;
278 /* Gate: execute, or not, all of the non-trivial optimizations. */
280 static bool
281 gate_all_early_local_passes (void)
283 /* Don't bother doing anything if the program has errors. */
284 return (!seen_error () && !in_lto_p);
287 struct simple_ipa_opt_pass pass_early_local_passes =
290 SIMPLE_IPA_PASS,
291 "early_local_cleanups", /* name */
292 OPTGROUP_NONE, /* optinfo_flags */
293 gate_all_early_local_passes, /* gate */
294 execute_all_early_local_passes, /* execute */
295 NULL, /* sub */
296 NULL, /* next */
297 0, /* static_pass_number */
298 TV_EARLY_LOCAL, /* tv_id */
299 0, /* properties_required */
300 0, /* properties_provided */
301 0, /* properties_destroyed */
302 0, /* todo_flags_start */
303 TODO_remove_functions /* todo_flags_finish */
307 /* Decides if the cgraph callee edges are being cleaned up for the
308 last time. */
309 bool cgraph_callee_edges_final_cleanup = false;
311 /* Gate: execute, or not, all of the non-trivial optimizations. */
313 static bool
314 gate_all_early_optimizations (void)
316 return (optimize >= 1
317 /* Don't bother doing anything if the program has errors. */
318 && !seen_error ());
321 static struct gimple_opt_pass pass_all_early_optimizations =
324 GIMPLE_PASS,
325 "early_optimizations", /* name */
326 OPTGROUP_NONE, /* optinfo_flags */
327 gate_all_early_optimizations, /* gate */
328 NULL, /* execute */
329 NULL, /* sub */
330 NULL, /* next */
331 0, /* static_pass_number */
332 TV_NONE, /* tv_id */
333 0, /* properties_required */
334 0, /* properties_provided */
335 0, /* properties_destroyed */
336 0, /* todo_flags_start */
337 0 /* todo_flags_finish */
341 /* Gate: execute, or not, all of the non-trivial optimizations. */
343 static bool
344 gate_all_optimizations (void)
346 /* The cgraph callee edges are being cleaned up for the last time. */
347 cgraph_callee_edges_final_cleanup = true;
348 return optimize >= 1 && !optimize_debug;
351 static struct gimple_opt_pass pass_all_optimizations =
354 GIMPLE_PASS,
355 "*all_optimizations", /* name */
356 OPTGROUP_NONE, /* optinfo_flags */
357 gate_all_optimizations, /* gate */
358 NULL, /* execute */
359 NULL, /* sub */
360 NULL, /* next */
361 0, /* static_pass_number */
362 TV_OPTIMIZE, /* tv_id */
363 0, /* properties_required */
364 0, /* properties_provided */
365 0, /* properties_destroyed */
366 0, /* todo_flags_start */
367 0 /* todo_flags_finish */
371 /* Gate: execute, or not, all of the non-trivial optimizations. */
373 static bool
374 gate_all_optimizations_g (void)
376 return optimize >= 1 && optimize_debug;
379 static struct gimple_opt_pass pass_all_optimizations_g =
382 GIMPLE_PASS,
383 "*all_optimizations_g", /* name */
384 OPTGROUP_NONE, /* optinfo_flags */
385 gate_all_optimizations_g, /* gate */
386 NULL, /* execute */
387 NULL, /* sub */
388 NULL, /* next */
389 0, /* static_pass_number */
390 TV_OPTIMIZE, /* tv_id */
391 0, /* properties_required */
392 0, /* properties_provided */
393 0, /* properties_destroyed */
394 0, /* todo_flags_start */
395 0 /* todo_flags_finish */
399 static bool
400 gate_rest_of_compilation (void)
402 /* Early return if there were errors. We can run afoul of our
403 consistency checks, and there's not really much point in fixing them. */
404 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
407 static struct rtl_opt_pass pass_rest_of_compilation =
410 RTL_PASS,
411 "*rest_of_compilation", /* name */
412 OPTGROUP_NONE, /* optinfo_flags */
413 gate_rest_of_compilation, /* gate */
414 NULL, /* execute */
415 NULL, /* sub */
416 NULL, /* next */
417 0, /* static_pass_number */
418 TV_REST_OF_COMPILATION, /* tv_id */
419 PROP_rtl, /* properties_required */
420 0, /* properties_provided */
421 0, /* properties_destroyed */
422 0, /* todo_flags_start */
423 TODO_ggc_collect /* todo_flags_finish */
427 static bool
428 gate_postreload (void)
430 return reload_completed;
433 static struct rtl_opt_pass pass_postreload =
436 RTL_PASS,
437 "*all-postreload", /* name */
438 OPTGROUP_NONE, /* optinfo_flags */
439 gate_postreload, /* gate */
440 NULL, /* execute */
441 NULL, /* sub */
442 NULL, /* next */
443 0, /* static_pass_number */
444 TV_POSTRELOAD, /* tv_id */
445 PROP_rtl, /* properties_required */
446 0, /* properties_provided */
447 0, /* properties_destroyed */
448 0, /* todo_flags_start */
449 TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
455 /* The root of the compilation pass tree, once constructed. */
456 struct opt_pass *all_passes, *all_small_ipa_passes, *all_lowering_passes,
457 *all_regular_ipa_passes, *all_late_ipa_passes, *all_lto_gen_passes;
459 /* This is used by plugins, and should also be used in register_pass. */
460 #define DEF_PASS_LIST(LIST) &LIST,
461 struct opt_pass **gcc_pass_lists[] = { GCC_PASS_LISTS NULL };
462 #undef DEF_PASS_LIST
464 /* A map from static pass id to optimization pass. */
465 struct opt_pass **passes_by_id;
466 int passes_by_id_size;
468 /* Set the static pass number of pass PASS to ID and record that
469 in the mapping from static pass number to pass. */
471 static void
472 set_pass_for_id (int id, struct opt_pass *pass)
474 pass->static_pass_number = id;
475 if (passes_by_id_size <= id)
477 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
478 memset (passes_by_id + passes_by_id_size, 0,
479 (id + 1 - passes_by_id_size) * sizeof (void *));
480 passes_by_id_size = id + 1;
482 passes_by_id[id] = pass;
485 /* Return the pass with the static pass number ID. */
487 struct opt_pass *
488 get_pass_for_id (int id)
490 if (id >= passes_by_id_size)
491 return NULL;
492 return passes_by_id[id];
495 /* Iterate over the pass tree allocating dump file numbers. We want
496 to do this depth first, and independent of whether the pass is
497 enabled or not. */
499 void
500 register_one_dump_file (struct opt_pass *pass)
502 char *dot_name, *flag_name, *glob_name;
503 const char *name, *full_name, *prefix;
504 char num[10];
505 int flags, id;
506 int optgroup_flags = OPTGROUP_NONE;
508 /* See below in next_pass_1. */
509 num[0] = '\0';
510 if (pass->static_pass_number != -1)
511 sprintf (num, "%d", ((int) pass->static_pass_number < 0
512 ? 1 : pass->static_pass_number));
514 /* The name is both used to identify the pass for the purposes of plugins,
515 and to specify dump file name and option.
516 The latter two might want something short which is not quite unique; for
517 that reason, we may have a disambiguating prefix, followed by a space
518 to mark the start of the following dump file name / option string. */
519 name = strchr (pass->name, ' ');
520 name = name ? name + 1 : pass->name;
521 dot_name = concat (".", name, num, NULL);
522 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
524 prefix = "ipa-";
525 flags = TDF_IPA;
526 optgroup_flags |= OPTGROUP_IPA;
528 else if (pass->type == GIMPLE_PASS)
530 prefix = "tree-";
531 flags = TDF_TREE;
533 else
535 prefix = "rtl-";
536 flags = TDF_RTL;
539 flag_name = concat (prefix, name, num, NULL);
540 glob_name = concat (prefix, name, NULL);
541 optgroup_flags |= pass->optinfo_flags;
542 /* For any passes that do not have an optgroup set, and which are not
543 IPA passes setup above, set the optgroup to OPTGROUP_OTHER so that
544 any dump messages are emitted properly under -fopt-info(-optall). */
545 if (optgroup_flags == OPTGROUP_NONE)
546 optgroup_flags = OPTGROUP_OTHER;
547 id = dump_register (dot_name, flag_name, glob_name, flags, optgroup_flags);
548 set_pass_for_id (id, pass);
549 full_name = concat (prefix, pass->name, num, NULL);
550 register_pass_name (pass, full_name);
551 free (CONST_CAST (char *, full_name));
554 /* Recursive worker function for register_dump_files. */
556 static int
557 register_dump_files_1 (struct opt_pass *pass, int properties)
561 int new_properties = (properties | pass->properties_provided)
562 & ~pass->properties_destroyed;
564 if (pass->name && pass->name[0] != '*')
565 register_one_dump_file (pass);
567 if (pass->sub)
568 new_properties = register_dump_files_1 (pass->sub, new_properties);
570 /* If we have a gate, combine the properties that we could have with
571 and without the pass being examined. */
572 if (pass->gate)
573 properties &= new_properties;
574 else
575 properties = new_properties;
577 pass = pass->next;
579 while (pass);
581 return properties;
584 /* Register the dump files for the pipeline starting at PASS.
585 PROPERTIES reflects the properties that are guaranteed to be available at
586 the beginning of the pipeline. */
588 static void
589 register_dump_files (struct opt_pass *pass,int properties)
591 pass->properties_required |= properties;
592 register_dump_files_1 (pass, properties);
595 struct pass_registry
597 const char* unique_name;
598 struct opt_pass *pass;
601 /* Pass registry hash function. */
603 static hashval_t
604 passr_hash (const void *p)
606 const struct pass_registry *const s = (const struct pass_registry *const) p;
607 return htab_hash_string (s->unique_name);
610 /* Hash equal function */
612 static int
613 passr_eq (const void *p1, const void *p2)
615 const struct pass_registry *const s1 = (const struct pass_registry *const) p1;
616 const struct pass_registry *const s2 = (const struct pass_registry *const) p2;
618 return !strcmp (s1->unique_name, s2->unique_name);
621 static htab_t name_to_pass_map = NULL;
623 /* Register PASS with NAME. */
625 static void
626 register_pass_name (struct opt_pass *pass, const char *name)
628 struct pass_registry **slot;
629 struct pass_registry pr;
631 if (!name_to_pass_map)
632 name_to_pass_map = htab_create (256, passr_hash, passr_eq, NULL);
634 pr.unique_name = name;
635 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map, &pr, INSERT);
636 if (!*slot)
638 struct pass_registry *new_pr;
640 new_pr = XCNEW (struct pass_registry);
641 new_pr->unique_name = xstrdup (name);
642 new_pr->pass = pass;
643 *slot = new_pr;
645 else
646 return; /* Ignore plugin passes. */
649 /* Map from pass id to canonicalized pass name. */
651 typedef const char *char_ptr;
652 static vec<char_ptr> pass_tab = vNULL;
654 /* Callback function for traversing NAME_TO_PASS_MAP. */
656 static int
657 pass_traverse (void **slot, void *data ATTRIBUTE_UNUSED)
659 struct pass_registry **p = (struct pass_registry **)slot;
660 struct opt_pass *pass = (*p)->pass;
662 gcc_assert (pass->static_pass_number > 0);
663 gcc_assert (pass_tab.exists ());
665 pass_tab[pass->static_pass_number] = (*p)->unique_name;
667 return 1;
670 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
671 table for dumping purpose. */
673 static void
674 create_pass_tab (void)
676 if (!flag_dump_passes)
677 return;
679 pass_tab.safe_grow_cleared (passes_by_id_size + 1);
680 htab_traverse (name_to_pass_map, pass_traverse, NULL);
683 static bool override_gate_status (struct opt_pass *, tree, bool);
685 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
686 is turned on or not. */
688 static void
689 dump_one_pass (struct opt_pass *pass, int pass_indent)
691 int indent = 3 * pass_indent;
692 const char *pn;
693 bool is_on, is_really_on;
695 is_on = (pass->gate == NULL) ? true : pass->gate();
696 is_really_on = override_gate_status (pass, current_function_decl, is_on);
698 if (pass->static_pass_number <= 0)
699 pn = pass->name;
700 else
701 pn = pass_tab[pass->static_pass_number];
703 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
704 (15 - indent < 0 ? 0 : 15 - indent), " ",
705 is_on ? " ON" : " OFF",
706 ((!is_on) == (!is_really_on) ? ""
707 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
710 /* Dump pass list PASS with indentation INDENT. */
712 static void
713 dump_pass_list (struct opt_pass *pass, int indent)
717 dump_one_pass (pass, indent);
718 if (pass->sub)
719 dump_pass_list (pass->sub, indent + 1);
720 pass = pass->next;
722 while (pass);
725 /* Dump all optimization passes. */
727 void
728 dump_passes (void)
730 struct cgraph_node *n, *node = NULL;
732 create_pass_tab();
734 FOR_EACH_FUNCTION (n)
735 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
737 node = n;
738 break;
741 if (!node)
742 return;
744 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
746 dump_pass_list (all_lowering_passes, 1);
747 dump_pass_list (all_small_ipa_passes, 1);
748 dump_pass_list (all_regular_ipa_passes, 1);
749 dump_pass_list (all_lto_gen_passes, 1);
750 dump_pass_list (all_late_ipa_passes, 1);
751 dump_pass_list (all_passes, 1);
753 pop_cfun ();
757 /* Returns the pass with NAME. */
759 static struct opt_pass *
760 get_pass_by_name (const char *name)
762 struct pass_registry **slot, pr;
764 pr.unique_name = name;
765 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map,
766 &pr, NO_INSERT);
768 if (!slot || !*slot)
769 return NULL;
771 return (*slot)->pass;
775 /* Range [start, last]. */
777 struct uid_range
779 unsigned int start;
780 unsigned int last;
781 const char *assem_name;
782 struct uid_range *next;
785 typedef struct uid_range *uid_range_p;
788 static vec<uid_range_p>
789 enabled_pass_uid_range_tab = vNULL;
790 static vec<uid_range_p>
791 disabled_pass_uid_range_tab = vNULL;
794 /* Parse option string for -fdisable- and -fenable-
795 The syntax of the options:
797 -fenable-<pass_name>
798 -fdisable-<pass_name>
800 -fenable-<pass_name>=s1:e1,s2:e2,...
801 -fdisable-<pass_name>=s1:e1,s2:e2,...
804 static void
805 enable_disable_pass (const char *arg, bool is_enable)
807 struct opt_pass *pass;
808 char *range_str, *phase_name;
809 char *argstr = xstrdup (arg);
810 vec<uid_range_p> *tab = 0;
812 range_str = strchr (argstr,'=');
813 if (range_str)
815 *range_str = '\0';
816 range_str++;
819 phase_name = argstr;
820 if (!*phase_name)
822 if (is_enable)
823 error ("unrecognized option -fenable");
824 else
825 error ("unrecognized option -fdisable");
826 free (argstr);
827 return;
829 pass = get_pass_by_name (phase_name);
830 if (!pass || pass->static_pass_number == -1)
832 if (is_enable)
833 error ("unknown pass %s specified in -fenable", phase_name);
834 else
835 error ("unknown pass %s specified in -fdisable", phase_name);
836 free (argstr);
837 return;
840 if (is_enable)
841 tab = &enabled_pass_uid_range_tab;
842 else
843 tab = &disabled_pass_uid_range_tab;
845 if ((unsigned) pass->static_pass_number >= tab->length ())
846 tab->safe_grow_cleared (pass->static_pass_number + 1);
848 if (!range_str)
850 uid_range_p slot;
851 uid_range_p new_range = XCNEW (struct uid_range);
853 new_range->start = 0;
854 new_range->last = (unsigned)-1;
856 slot = (*tab)[pass->static_pass_number];
857 new_range->next = slot;
858 (*tab)[pass->static_pass_number] = new_range;
859 if (is_enable)
860 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
861 "of [%u, %u]", phase_name, new_range->start, new_range->last);
862 else
863 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
864 "of [%u, %u]", phase_name, new_range->start, new_range->last);
866 else
868 char *next_range = NULL;
869 char *one_range = range_str;
870 char *end_val = NULL;
874 uid_range_p slot;
875 uid_range_p new_range;
876 char *invalid = NULL;
877 long start;
878 char *func_name = NULL;
880 next_range = strchr (one_range, ',');
881 if (next_range)
883 *next_range = '\0';
884 next_range++;
887 end_val = strchr (one_range, ':');
888 if (end_val)
890 *end_val = '\0';
891 end_val++;
893 start = strtol (one_range, &invalid, 10);
894 if (*invalid || start < 0)
896 if (end_val || (one_range[0] >= '0'
897 && one_range[0] <= '9'))
899 error ("Invalid range %s in option %s",
900 one_range,
901 is_enable ? "-fenable" : "-fdisable");
902 free (argstr);
903 return;
905 func_name = one_range;
907 if (!end_val)
909 new_range = XCNEW (struct uid_range);
910 if (!func_name)
912 new_range->start = (unsigned) start;
913 new_range->last = (unsigned) start;
915 else
917 new_range->start = (unsigned) -1;
918 new_range->last = (unsigned) -1;
919 new_range->assem_name = xstrdup (func_name);
922 else
924 long last = strtol (end_val, &invalid, 10);
925 if (*invalid || last < start)
927 error ("Invalid range %s in option %s",
928 end_val,
929 is_enable ? "-fenable" : "-fdisable");
930 free (argstr);
931 return;
933 new_range = XCNEW (struct uid_range);
934 new_range->start = (unsigned) start;
935 new_range->last = (unsigned) last;
938 slot = (*tab)[pass->static_pass_number];
939 new_range->next = slot;
940 (*tab)[pass->static_pass_number] = new_range;
941 if (is_enable)
943 if (new_range->assem_name)
944 inform (UNKNOWN_LOCATION,
945 "enable pass %s for function %s",
946 phase_name, new_range->assem_name);
947 else
948 inform (UNKNOWN_LOCATION,
949 "enable pass %s for functions in the range of [%u, %u]",
950 phase_name, new_range->start, new_range->last);
952 else
954 if (new_range->assem_name)
955 inform (UNKNOWN_LOCATION,
956 "disable pass %s for function %s",
957 phase_name, new_range->assem_name);
958 else
959 inform (UNKNOWN_LOCATION,
960 "disable pass %s for functions in the range of [%u, %u]",
961 phase_name, new_range->start, new_range->last);
964 one_range = next_range;
965 } while (next_range);
968 free (argstr);
971 /* Enable pass specified by ARG. */
973 void
974 enable_pass (const char *arg)
976 enable_disable_pass (arg, true);
979 /* Disable pass specified by ARG. */
981 void
982 disable_pass (const char *arg)
984 enable_disable_pass (arg, false);
987 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
989 static bool
990 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
991 tree func,
992 vec<uid_range_p> tab)
994 uid_range_p slot, range;
995 int cgraph_uid;
996 const char *aname = NULL;
998 if (!tab.exists ()
999 || (unsigned) pass->static_pass_number >= tab.length ()
1000 || pass->static_pass_number == -1)
1001 return false;
1003 slot = tab[pass->static_pass_number];
1004 if (!slot)
1005 return false;
1007 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
1008 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1009 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1011 range = slot;
1012 while (range)
1014 if ((unsigned) cgraph_uid >= range->start
1015 && (unsigned) cgraph_uid <= range->last)
1016 return true;
1017 if (range->assem_name && aname
1018 && !strcmp (range->assem_name, aname))
1019 return true;
1020 range = range->next;
1023 return false;
1026 /* Look at the static_pass_number and duplicate the pass
1027 if it is already added to a list. */
1029 static struct opt_pass *
1030 make_pass_instance (struct opt_pass *pass, bool track_duplicates)
1032 /* A nonzero static_pass_number indicates that the
1033 pass is already in the list. */
1034 if (pass->static_pass_number)
1036 struct opt_pass *new_pass;
1038 if (pass->type == GIMPLE_PASS
1039 || pass->type == RTL_PASS
1040 || pass->type == SIMPLE_IPA_PASS)
1042 new_pass = XNEW (struct opt_pass);
1043 memcpy (new_pass, pass, sizeof (struct opt_pass));
1045 else if (pass->type == IPA_PASS)
1047 new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
1048 memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
1050 else
1051 gcc_unreachable ();
1053 new_pass->next = NULL;
1055 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1057 /* Indicate to register_dump_files that this pass has duplicates,
1058 and so it should rename the dump file. The first instance will
1059 be -1, and be number of duplicates = -static_pass_number - 1.
1060 Subsequent instances will be > 0 and just the duplicate number. */
1061 if ((pass->name && pass->name[0] != '*') || track_duplicates)
1063 pass->static_pass_number -= 1;
1064 new_pass->static_pass_number = -pass->static_pass_number;
1066 return new_pass;
1068 else
1070 pass->todo_flags_start |= TODO_mark_first_instance;
1071 pass->static_pass_number = -1;
1073 invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
1075 return pass;
1078 /* Add a pass to the pass list. Duplicate the pass if it's already
1079 in the list. */
1081 static struct opt_pass **
1082 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
1084 /* Every pass should have a name so that plugins can refer to them. */
1085 gcc_assert (pass->name != NULL);
1087 *list = make_pass_instance (pass, false);
1089 return &(*list)->next;
1092 /* List node for an inserted pass instance. We need to keep track of all
1093 the newly-added pass instances (with 'added_pass_nodes' defined below)
1094 so that we can register their dump files after pass-positioning is finished.
1095 Registering dumping files needs to be post-processed or the
1096 static_pass_number of the opt_pass object would be modified and mess up
1097 the dump file names of future pass instances to be added. */
1099 struct pass_list_node
1101 struct opt_pass *pass;
1102 struct pass_list_node *next;
1105 static struct pass_list_node *added_pass_nodes = NULL;
1106 static struct pass_list_node *prev_added_pass_node;
1108 /* Insert the pass at the proper position. Return true if the pass
1109 is successfully added.
1111 NEW_PASS_INFO - new pass to be inserted
1112 PASS_LIST - root of the pass list to insert the new pass to */
1114 static bool
1115 position_pass (struct register_pass_info *new_pass_info,
1116 struct opt_pass **pass_list)
1118 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1119 bool success = false;
1121 for ( ; pass; prev_pass = pass, pass = pass->next)
1123 /* Check if the current pass is of the same type as the new pass and
1124 matches the name and the instance number of the reference pass. */
1125 if (pass->type == new_pass_info->pass->type
1126 && pass->name
1127 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1128 && ((new_pass_info->ref_pass_instance_number == 0)
1129 || (new_pass_info->ref_pass_instance_number ==
1130 pass->static_pass_number)
1131 || (new_pass_info->ref_pass_instance_number == 1
1132 && pass->todo_flags_start & TODO_mark_first_instance)))
1134 struct opt_pass *new_pass;
1135 struct pass_list_node *new_pass_node;
1137 new_pass = make_pass_instance (new_pass_info->pass, true);
1139 /* Insert the new pass instance based on the positioning op. */
1140 switch (new_pass_info->pos_op)
1142 case PASS_POS_INSERT_AFTER:
1143 new_pass->next = pass->next;
1144 pass->next = new_pass;
1146 /* Skip newly inserted pass to avoid repeated
1147 insertions in the case where the new pass and the
1148 existing one have the same name. */
1149 pass = new_pass;
1150 break;
1151 case PASS_POS_INSERT_BEFORE:
1152 new_pass->next = pass;
1153 if (prev_pass)
1154 prev_pass->next = new_pass;
1155 else
1156 *pass_list = new_pass;
1157 break;
1158 case PASS_POS_REPLACE:
1159 new_pass->next = pass->next;
1160 if (prev_pass)
1161 prev_pass->next = new_pass;
1162 else
1163 *pass_list = new_pass;
1164 new_pass->sub = pass->sub;
1165 new_pass->tv_id = pass->tv_id;
1166 pass = new_pass;
1167 break;
1168 default:
1169 error ("invalid pass positioning operation");
1170 return false;
1173 /* Save the newly added pass (instance) in the added_pass_nodes
1174 list so that we can register its dump file later. Note that
1175 we cannot register the dump file now because doing so will modify
1176 the static_pass_number of the opt_pass object and therefore
1177 mess up the dump file name of future instances. */
1178 new_pass_node = XCNEW (struct pass_list_node);
1179 new_pass_node->pass = new_pass;
1180 if (!added_pass_nodes)
1181 added_pass_nodes = new_pass_node;
1182 else
1183 prev_added_pass_node->next = new_pass_node;
1184 prev_added_pass_node = new_pass_node;
1186 success = true;
1189 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1190 success = true;
1193 return success;
1196 /* Hooks a new pass into the pass lists.
1198 PASS_INFO - pass information that specifies the opt_pass object,
1199 reference pass, instance number, and how to position
1200 the pass */
1202 void
1203 register_pass (struct register_pass_info *pass_info)
1205 bool all_instances, success;
1207 /* The checks below could fail in buggy plugins. Existing GCC
1208 passes should never fail these checks, so we mention plugin in
1209 the messages. */
1210 if (!pass_info->pass)
1211 fatal_error ("plugin cannot register a missing pass");
1213 if (!pass_info->pass->name)
1214 fatal_error ("plugin cannot register an unnamed pass");
1216 if (!pass_info->reference_pass_name)
1217 fatal_error
1218 ("plugin cannot register pass %qs without reference pass name",
1219 pass_info->pass->name);
1221 /* Try to insert the new pass to the pass lists. We need to check
1222 all five lists as the reference pass could be in one (or all) of
1223 them. */
1224 all_instances = pass_info->ref_pass_instance_number == 0;
1225 success = position_pass (pass_info, &all_lowering_passes);
1226 if (!success || all_instances)
1227 success |= position_pass (pass_info, &all_small_ipa_passes);
1228 if (!success || all_instances)
1229 success |= position_pass (pass_info, &all_regular_ipa_passes);
1230 if (!success || all_instances)
1231 success |= position_pass (pass_info, &all_lto_gen_passes);
1232 if (!success || all_instances)
1233 success |= position_pass (pass_info, &all_late_ipa_passes);
1234 if (!success || all_instances)
1235 success |= position_pass (pass_info, &all_passes);
1236 if (!success)
1237 fatal_error
1238 ("pass %qs not found but is referenced by new pass %qs",
1239 pass_info->reference_pass_name, pass_info->pass->name);
1241 /* OK, we have successfully inserted the new pass. We need to register
1242 the dump files for the newly added pass and its duplicates (if any).
1243 Because the registration of plugin/backend passes happens after the
1244 command-line options are parsed, the options that specify single
1245 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1246 passes. Therefore we currently can only enable dumping of
1247 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1248 are specified. While doing so, we also delete the pass_list_node
1249 objects created during pass positioning. */
1250 while (added_pass_nodes)
1252 struct pass_list_node *next_node = added_pass_nodes->next;
1253 enum tree_dump_index tdi;
1254 register_one_dump_file (added_pass_nodes->pass);
1255 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1256 || added_pass_nodes->pass->type == IPA_PASS)
1257 tdi = TDI_ipa_all;
1258 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1259 tdi = TDI_tree_all;
1260 else
1261 tdi = TDI_rtl_all;
1262 /* Check if dump-all flag is specified. */
1263 if (get_dump_file_info (tdi)->pstate)
1264 get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1265 ->pstate = get_dump_file_info (tdi)->pstate;
1266 XDELETE (added_pass_nodes);
1267 added_pass_nodes = next_node;
1271 /* Construct the pass tree. The sequencing of passes is driven by
1272 the cgraph routines:
1274 finalize_compilation_unit ()
1275 for each node N in the cgraph
1276 cgraph_analyze_function (N)
1277 cgraph_lower_function (N) -> all_lowering_passes
1279 If we are optimizing, compile is then invoked:
1281 compile ()
1282 ipa_passes () -> all_small_ipa_passes
1283 -> Analysis of all_regular_ipa_passes
1284 * possible LTO streaming at copmilation time *
1285 -> Execution of all_regular_ipa_passes
1286 * possible LTO streaming at link time *
1287 -> all_late_ipa_passes
1288 expand_all_functions ()
1289 for each node N in the cgraph
1290 expand_function (N) -> Transformation of all_regular_ipa_passes
1291 -> all_passes
1294 void
1295 init_optimization_passes (void)
1297 struct opt_pass **p;
1299 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &((PASS).pass)))
1301 /* All passes needed to lower the function into shape optimizers can
1302 operate on. These passes are always run first on the function, but
1303 backend might produce already lowered functions that are not processed
1304 by these passes. */
1305 p = &all_lowering_passes;
1306 NEXT_PASS (pass_warn_unused_result);
1307 NEXT_PASS (pass_diagnose_omp_blocks);
1308 NEXT_PASS (pass_diagnose_tm_blocks);
1309 NEXT_PASS (pass_mudflap_1);
1310 NEXT_PASS (pass_lower_omp);
1311 NEXT_PASS (pass_lower_cf);
1312 NEXT_PASS (pass_lower_tm);
1313 NEXT_PASS (pass_refactor_eh);
1314 NEXT_PASS (pass_lower_eh);
1315 NEXT_PASS (pass_build_cfg);
1316 NEXT_PASS (pass_warn_function_return);
1317 NEXT_PASS (pass_build_cgraph_edges);
1318 *p = NULL;
1320 /* Interprocedural optimization passes. */
1321 p = &all_small_ipa_passes;
1322 NEXT_PASS (pass_ipa_free_lang_data);
1323 NEXT_PASS (pass_ipa_function_and_variable_visibility);
1324 NEXT_PASS (pass_early_local_passes);
1326 struct opt_pass **p = &pass_early_local_passes.pass.sub;
1327 NEXT_PASS (pass_fixup_cfg);
1328 NEXT_PASS (pass_init_datastructures);
1329 NEXT_PASS (pass_expand_omp);
1331 NEXT_PASS (pass_build_ssa);
1332 NEXT_PASS (pass_lower_vector);
1333 NEXT_PASS (pass_early_warn_uninitialized);
1334 NEXT_PASS (pass_rebuild_cgraph_edges);
1335 NEXT_PASS (pass_inline_parameters);
1336 NEXT_PASS (pass_early_inline);
1337 NEXT_PASS (pass_all_early_optimizations);
1339 struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
1340 NEXT_PASS (pass_remove_cgraph_callee_edges);
1341 NEXT_PASS (pass_rename_ssa_copies);
1342 NEXT_PASS (pass_ccp);
1343 /* After CCP we rewrite no longer addressed locals into SSA
1344 form if possible. */
1345 NEXT_PASS (pass_forwprop);
1346 /* pass_build_ealias is a dummy pass that ensures that we
1347 execute TODO_rebuild_alias at this point. */
1348 NEXT_PASS (pass_build_ealias);
1349 NEXT_PASS (pass_sra_early);
1350 NEXT_PASS (pass_fre);
1351 NEXT_PASS (pass_copy_prop);
1352 NEXT_PASS (pass_merge_phi);
1353 NEXT_PASS (pass_cd_dce);
1354 NEXT_PASS (pass_early_ipa_sra);
1355 NEXT_PASS (pass_tail_recursion);
1356 NEXT_PASS (pass_convert_switch);
1357 NEXT_PASS (pass_cleanup_eh);
1358 NEXT_PASS (pass_profile);
1359 NEXT_PASS (pass_local_pure_const);
1360 /* Split functions creates parts that are not run through
1361 early optimizations again. It is thus good idea to do this
1362 late. */
1363 NEXT_PASS (pass_split_functions);
1365 NEXT_PASS (pass_release_ssa_names);
1366 NEXT_PASS (pass_rebuild_cgraph_edges);
1367 NEXT_PASS (pass_inline_parameters);
1369 NEXT_PASS (pass_ipa_auto_profile);
1370 NEXT_PASS (pass_ipa_free_inline_summary);
1371 NEXT_PASS (pass_ipa_tree_profile);
1373 struct opt_pass **p = &pass_ipa_tree_profile.pass.sub;
1374 NEXT_PASS (pass_feedback_split_functions);
1376 NEXT_PASS (pass_ipa_increase_alignment);
1377 NEXT_PASS (pass_ipa_tm);
1378 NEXT_PASS (pass_ipa_lower_emutls);
1379 *p = NULL;
1381 p = &all_regular_ipa_passes;
1382 NEXT_PASS (pass_ipa_whole_program_visibility);
1383 NEXT_PASS (pass_ipa_profile);
1384 NEXT_PASS (pass_ipa_cp);
1385 NEXT_PASS (pass_ipa_cdtor_merge);
1386 NEXT_PASS (pass_ipa_inline);
1387 NEXT_PASS (pass_ipa_pure_const);
1388 NEXT_PASS (pass_ipa_reference);
1389 *p = NULL;
1391 p = &all_lto_gen_passes;
1392 NEXT_PASS (pass_ipa_lto_gimple_out);
1393 NEXT_PASS (pass_ipa_lto_finish_out); /* This must be the last LTO pass. */
1394 *p = NULL;
1396 /* Simple IPA passes executed after the regular passes. In WHOPR mode the
1397 passes are executed after partitioning and thus see just parts of the
1398 compiled unit. */
1399 p = &all_late_ipa_passes;
1400 NEXT_PASS (pass_ipa_pta);
1401 *p = NULL;
1403 /* These passes are run after IPA passes on every function that is being
1404 output to the assembler file. */
1405 p = &all_passes;
1406 NEXT_PASS (pass_direct_call_profile);
1407 NEXT_PASS (pass_fixup_cfg);
1408 NEXT_PASS (pass_lower_eh_dispatch);
1409 NEXT_PASS (pass_all_optimizations);
1411 struct opt_pass **p = &pass_all_optimizations.pass.sub;
1412 NEXT_PASS (pass_remove_cgraph_callee_edges);
1413 /* Initial scalar cleanups before alias computation.
1414 They ensure memory accesses are not indirect wherever possible. */
1415 NEXT_PASS (pass_strip_predict_hints);
1416 NEXT_PASS (pass_rename_ssa_copies);
1417 NEXT_PASS (pass_complete_unrolli);
1418 NEXT_PASS (pass_ccp);
1419 /* After CCP we rewrite no longer addressed locals into SSA
1420 form if possible. */
1421 NEXT_PASS (pass_forwprop);
1422 NEXT_PASS (pass_object_sizes);
1423 /* pass_build_alias is a dummy pass that ensures that we
1424 execute TODO_rebuild_alias at this point. */
1425 NEXT_PASS (pass_build_alias);
1426 NEXT_PASS (pass_return_slot);
1427 NEXT_PASS (pass_phiprop);
1428 NEXT_PASS (pass_fre);
1429 NEXT_PASS (pass_copy_prop);
1430 NEXT_PASS (pass_merge_phi);
1431 NEXT_PASS (pass_vrp);
1432 NEXT_PASS (pass_dce);
1433 NEXT_PASS (pass_call_cdce);
1434 NEXT_PASS (pass_cselim);
1435 NEXT_PASS (pass_tree_ifcombine);
1436 NEXT_PASS (pass_phiopt);
1437 NEXT_PASS (pass_tail_recursion);
1438 NEXT_PASS (pass_ch);
1439 NEXT_PASS (pass_stdarg);
1440 NEXT_PASS (pass_lower_complex);
1441 NEXT_PASS (pass_sra);
1442 NEXT_PASS (pass_rename_ssa_copies);
1443 /* The dom pass will also resolve all __builtin_constant_p calls
1444 that are still there to 0. This has to be done after some
1445 propagations have already run, but before some more dead code
1446 is removed, and this place fits nicely. Remember this when
1447 trying to move or duplicate pass_dominator somewhere earlier. */
1448 NEXT_PASS (pass_dominator);
1449 /* The only const/copy propagation opportunities left after
1450 DOM should be due to degenerate PHI nodes. So rather than
1451 run the full propagators, run a specialized pass which
1452 only examines PHIs to discover const/copy propagation
1453 opportunities. */
1454 NEXT_PASS (pass_phi_only_cprop);
1455 NEXT_PASS (pass_dse);
1456 NEXT_PASS (pass_reassoc);
1457 NEXT_PASS (pass_dce);
1458 NEXT_PASS (pass_forwprop);
1459 NEXT_PASS (pass_phiopt);
1460 NEXT_PASS (pass_strlen);
1461 NEXT_PASS (pass_ccp);
1462 /* After CCP we rewrite no longer addressed locals into SSA
1463 form if possible. */
1464 NEXT_PASS (pass_copy_prop);
1465 NEXT_PASS (pass_cse_sincos);
1466 NEXT_PASS (pass_optimize_bswap);
1467 NEXT_PASS (pass_split_crit_edges);
1468 NEXT_PASS (pass_pre);
1469 NEXT_PASS (pass_sink_code);
1470 NEXT_PASS (pass_asan);
1471 NEXT_PASS (pass_tsan);
1472 NEXT_PASS (pass_tree_loop);
1474 struct opt_pass **p = &pass_tree_loop.pass.sub;
1475 NEXT_PASS (pass_tree_loop_init);
1476 NEXT_PASS (pass_lim);
1477 NEXT_PASS (pass_copy_prop);
1478 NEXT_PASS (pass_dce_loop);
1479 NEXT_PASS (pass_tree_unswitch);
1480 NEXT_PASS (pass_scev_cprop);
1481 NEXT_PASS (pass_record_bounds);
1482 NEXT_PASS (pass_check_data_deps);
1483 NEXT_PASS (pass_loop_distribution);
1484 NEXT_PASS (pass_copy_prop);
1485 NEXT_PASS (pass_graphite);
1487 struct opt_pass **p = &pass_graphite.pass.sub;
1488 NEXT_PASS (pass_graphite_transforms);
1489 NEXT_PASS (pass_lim);
1490 NEXT_PASS (pass_copy_prop);
1491 NEXT_PASS (pass_dce_loop);
1493 NEXT_PASS (pass_iv_canon);
1494 NEXT_PASS (pass_if_conversion);
1495 NEXT_PASS (pass_vectorize);
1497 struct opt_pass **p = &pass_vectorize.pass.sub;
1498 NEXT_PASS (pass_dce_loop);
1500 NEXT_PASS (pass_predcom);
1501 NEXT_PASS (pass_complete_unroll);
1502 NEXT_PASS (pass_slp_vectorize);
1503 NEXT_PASS (pass_parallelize_loops);
1504 NEXT_PASS (pass_loop_prefetch);
1505 NEXT_PASS (pass_iv_optimize);
1506 NEXT_PASS (pass_lim);
1507 NEXT_PASS (pass_tree_loop_done);
1509 NEXT_PASS (pass_lower_vector_ssa);
1510 NEXT_PASS (pass_cse_reciprocals);
1511 NEXT_PASS (pass_reassoc);
1512 NEXT_PASS (pass_vrp);
1513 NEXT_PASS (pass_strength_reduction);
1514 NEXT_PASS (pass_dominator);
1515 /* The only const/copy propagation opportunities left after
1516 DOM should be due to degenerate PHI nodes. So rather than
1517 run the full propagators, run a specialized pass which
1518 only examines PHIs to discover const/copy propagation
1519 opportunities. */
1520 NEXT_PASS (pass_phi_only_cprop);
1521 NEXT_PASS (pass_cd_dce);
1522 NEXT_PASS (pass_tracer);
1524 /* FIXME: If DCE is not run before checking for uninitialized uses,
1525 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
1526 However, this also causes us to misdiagnose cases that should be
1527 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
1529 To fix the false positives in uninit-5.c, we would have to
1530 account for the predicates protecting the set and the use of each
1531 variable. Using a representation like Gated Single Assignment
1532 may help. */
1533 NEXT_PASS (pass_late_warn_uninitialized);
1534 NEXT_PASS (pass_dse);
1535 NEXT_PASS (pass_forwprop);
1536 NEXT_PASS (pass_phiopt);
1537 NEXT_PASS (pass_fold_builtins);
1538 NEXT_PASS (pass_optimize_widening_mul);
1539 NEXT_PASS (pass_tail_calls);
1540 NEXT_PASS (pass_rename_ssa_copies);
1541 NEXT_PASS (pass_uncprop);
1542 NEXT_PASS (pass_local_pure_const);
1544 NEXT_PASS (pass_all_optimizations_g);
1546 struct opt_pass **p = &pass_all_optimizations_g.pass.sub;
1547 NEXT_PASS (pass_remove_cgraph_callee_edges);
1548 NEXT_PASS (pass_strip_predict_hints);
1549 /* Lower remaining pieces of GIMPLE. */
1550 NEXT_PASS (pass_lower_complex);
1551 NEXT_PASS (pass_lower_vector_ssa);
1552 /* Perform simple scalar cleanup which is constant/copy propagation. */
1553 NEXT_PASS (pass_ccp);
1554 NEXT_PASS (pass_object_sizes);
1555 /* Fold remaining builtins. */
1556 NEXT_PASS (pass_fold_builtins);
1557 /* Copy propagation also copy-propagates constants, this is necessary
1558 to forward object-size and builtin folding results properly. */
1559 NEXT_PASS (pass_copy_prop);
1560 NEXT_PASS (pass_dce);
1561 NEXT_PASS (pass_asan);
1562 NEXT_PASS (pass_tsan);
1563 NEXT_PASS (pass_rename_ssa_copies);
1564 /* ??? We do want some kind of loop invariant motion, but we possibly
1565 need to adjust LIM to be more friendly towards preserving accurate
1566 debug information here. */
1567 /* Split critical edges before late uninit warning to reduce the
1568 number of false positives from it. */
1569 NEXT_PASS (pass_split_crit_edges);
1570 NEXT_PASS (pass_late_warn_uninitialized);
1571 NEXT_PASS (pass_uncprop);
1572 NEXT_PASS (pass_local_pure_const);
1574 NEXT_PASS (pass_tm_init);
1576 struct opt_pass **p = &pass_tm_init.pass.sub;
1577 NEXT_PASS (pass_tm_mark);
1578 NEXT_PASS (pass_tm_memopt);
1579 NEXT_PASS (pass_tm_edges);
1581 NEXT_PASS (pass_lower_complex_O0);
1582 NEXT_PASS (pass_asan_O0);
1583 NEXT_PASS (pass_tsan_O0);
1584 NEXT_PASS (pass_cleanup_eh);
1585 NEXT_PASS (pass_lower_resx);
1586 NEXT_PASS (pass_nrv);
1587 NEXT_PASS (pass_mudflap_2);
1588 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
1589 NEXT_PASS (pass_warn_function_noreturn);
1591 NEXT_PASS (pass_expand);
1593 NEXT_PASS (pass_rest_of_compilation);
1595 struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
1596 NEXT_PASS (pass_instantiate_virtual_regs);
1597 NEXT_PASS (pass_into_cfg_layout_mode);
1598 NEXT_PASS (pass_jump);
1599 NEXT_PASS (pass_lower_subreg);
1600 NEXT_PASS (pass_df_initialize_opt);
1601 NEXT_PASS (pass_cse);
1602 NEXT_PASS (pass_rtl_fwprop);
1603 NEXT_PASS (pass_rtl_cprop);
1604 NEXT_PASS (pass_rtl_pre);
1605 NEXT_PASS (pass_rtl_hoist);
1606 NEXT_PASS (pass_rtl_cprop);
1607 NEXT_PASS (pass_rtl_store_motion);
1608 NEXT_PASS (pass_cse_after_global_opts);
1609 NEXT_PASS (pass_rtl_ifcvt);
1610 NEXT_PASS (pass_reginfo_init);
1611 /* Perform loop optimizations. It might be better to do them a bit
1612 sooner, but we want the profile feedback to work more
1613 efficiently. */
1614 NEXT_PASS (pass_loop2);
1616 struct opt_pass **p = &pass_loop2.pass.sub;
1617 NEXT_PASS (pass_rtl_loop_init);
1618 NEXT_PASS (pass_rtl_move_loop_invariants);
1619 NEXT_PASS (pass_rtl_unswitch);
1620 NEXT_PASS (pass_rtl_unroll_and_peel_loops);
1621 NEXT_PASS (pass_rtl_doloop);
1622 NEXT_PASS (pass_rtl_loop_done);
1623 *p = NULL;
1625 NEXT_PASS (pass_web);
1626 NEXT_PASS (pass_rtl_cprop);
1627 NEXT_PASS (pass_cse2);
1628 NEXT_PASS (pass_rtl_dse1);
1629 NEXT_PASS (pass_rtl_fwprop_addr);
1630 NEXT_PASS (pass_inc_dec);
1631 NEXT_PASS (pass_initialize_regs);
1632 NEXT_PASS (pass_ud_rtl_dce);
1633 NEXT_PASS (pass_combine);
1634 NEXT_PASS (pass_if_after_combine);
1635 NEXT_PASS (pass_partition_blocks);
1636 NEXT_PASS (pass_regmove);
1637 NEXT_PASS (pass_outof_cfg_layout_mode);
1638 NEXT_PASS (pass_split_all_insns);
1639 NEXT_PASS (pass_lower_subreg2);
1640 NEXT_PASS (pass_df_initialize_no_opt);
1641 NEXT_PASS (pass_stack_ptr_mod);
1642 NEXT_PASS (pass_mode_switching);
1643 NEXT_PASS (pass_match_asm_constraints);
1644 NEXT_PASS (pass_sms);
1645 NEXT_PASS (pass_sched);
1646 NEXT_PASS (pass_ira);
1647 NEXT_PASS (pass_reload);
1648 NEXT_PASS (pass_postreload);
1650 struct opt_pass **p = &pass_postreload.pass.sub;
1651 NEXT_PASS (pass_postreload_cse);
1652 NEXT_PASS (pass_gcse2);
1653 NEXT_PASS (pass_split_after_reload);
1654 NEXT_PASS (pass_ree);
1655 NEXT_PASS (pass_compare_elim_after_reload);
1656 NEXT_PASS (pass_branch_target_load_optimize1);
1657 NEXT_PASS (pass_thread_prologue_and_epilogue);
1658 NEXT_PASS (pass_rtl_dse2);
1659 NEXT_PASS (pass_stack_adjustments);
1660 NEXT_PASS (pass_jump2);
1661 NEXT_PASS (pass_peephole2);
1662 NEXT_PASS (pass_if_after_reload);
1663 NEXT_PASS (pass_regrename);
1664 NEXT_PASS (pass_cprop_hardreg);
1665 NEXT_PASS (pass_fast_rtl_dce);
1666 NEXT_PASS (pass_duplicate_computed_gotos);
1667 NEXT_PASS (pass_reorder_blocks);
1668 NEXT_PASS (pass_branch_target_load_optimize2);
1669 NEXT_PASS (pass_leaf_regs);
1670 NEXT_PASS (pass_split_before_sched2);
1671 NEXT_PASS (pass_sched2);
1672 NEXT_PASS (pass_stack_regs);
1674 struct opt_pass **p = &pass_stack_regs.pass.sub;
1675 NEXT_PASS (pass_split_before_regstack);
1676 NEXT_PASS (pass_stack_regs_run);
1678 NEXT_PASS (pass_compute_alignments);
1679 NEXT_PASS (pass_variable_tracking);
1680 NEXT_PASS (pass_free_cfg);
1681 NEXT_PASS (pass_machine_reorg);
1682 NEXT_PASS (pass_cleanup_barriers);
1683 NEXT_PASS (pass_delay_slots);
1684 NEXT_PASS (pass_split_for_shorten_branches);
1685 NEXT_PASS (pass_convert_to_eh_region_ranges);
1686 NEXT_PASS (pass_shorten_branches);
1687 NEXT_PASS (pass_set_nothrow_function_flags);
1688 NEXT_PASS (pass_dwarf2_frame);
1689 NEXT_PASS (pass_final);
1691 NEXT_PASS (pass_df_finish);
1693 NEXT_PASS (pass_clean_state);
1694 *p = NULL;
1696 #undef NEXT_PASS
1698 /* Register the passes with the tree dump code. */
1699 register_dump_files (all_lowering_passes, PROP_gimple_any);
1700 register_dump_files (all_small_ipa_passes,
1701 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1702 | PROP_cfg);
1703 register_dump_files (all_regular_ipa_passes,
1704 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1705 | PROP_cfg);
1706 register_dump_files (all_lto_gen_passes,
1707 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1708 | PROP_cfg);
1709 register_dump_files (all_late_ipa_passes,
1710 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1711 | PROP_cfg);
1712 register_dump_files (all_passes,
1713 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1714 | PROP_cfg);
1717 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1718 function CALLBACK for every function in the call graph. Otherwise,
1719 call CALLBACK on the current function. */
1721 static void
1722 do_per_function (void (*callback) (void *data), void *data)
1724 if (current_function_decl)
1725 callback (data);
1726 else
1728 struct cgraph_node *node;
1729 FOR_EACH_DEFINED_FUNCTION (node)
1730 if (gimple_has_body_p (node->symbol.decl)
1731 && (!node->clone_of || node->symbol.decl != node->clone_of->symbol.decl))
1733 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1734 callback (data);
1735 if (!flag_wpa)
1737 free_dominance_info (CDI_DOMINATORS);
1738 free_dominance_info (CDI_POST_DOMINATORS);
1740 pop_cfun ();
1741 ggc_collect ();
1746 /* Because inlining might remove no-longer reachable nodes, we need to
1747 keep the array visible to garbage collector to avoid reading collected
1748 out nodes. */
1749 static int nnodes;
1750 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1752 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1753 function CALLBACK for every function in the call graph. Otherwise,
1754 call CALLBACK on the current function.
1755 This function is global so that plugins can use it. */
1756 void
1757 do_per_function_toporder (void (*callback) (void *data), void *data)
1759 int i;
1761 if (current_function_decl)
1762 callback (data);
1763 else
1765 gcc_assert (!order);
1766 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1767 nnodes = ipa_reverse_postorder (order);
1768 for (i = nnodes - 1; i >= 0; i--)
1769 order[i]->process = 1;
1770 for (i = nnodes - 1; i >= 0; i--)
1772 struct cgraph_node *node = order[i];
1774 /* Allow possibly removed nodes to be garbage collected. */
1775 order[i] = NULL;
1776 node->process = 0;
1777 if (cgraph_function_with_gimple_body_p (node))
1779 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1780 callback (data);
1781 free_dominance_info (CDI_DOMINATORS);
1782 free_dominance_info (CDI_POST_DOMINATORS);
1783 pop_cfun ();
1784 ggc_collect ();
1788 ggc_free (order);
1789 order = NULL;
1790 nnodes = 0;
1793 /* Helper function to perform function body dump. */
1795 static void
1796 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1798 if (dump_file && current_function_decl)
1800 if (cfun->curr_properties & PROP_trees)
1801 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1802 else
1803 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1805 /* Flush the file. If verification fails, we won't be able to
1806 close the file before aborting. */
1807 fflush (dump_file);
1809 if ((cfun->curr_properties & PROP_cfg)
1810 && (dump_flags & TDF_GRAPH))
1811 print_graph_cfg (dump_file_name, cfun);
1815 static struct profile_record *profile_record;
1817 /* Do profile consistency book-keeping for the pass with static number INDEX.
1818 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1819 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1820 if we are only book-keeping on passes that may have selectively disabled
1821 themselves on a given function. */
1822 static void
1823 check_profile_consistency (int index, int subpass, bool run)
1825 if (index == -1)
1826 return;
1827 if (!profile_record)
1828 profile_record = XCNEWVEC (struct profile_record,
1829 passes_by_id_size);
1830 gcc_assert (index < passes_by_id_size && index >= 0);
1831 gcc_assert (subpass < 2);
1832 profile_record[index].run |= run;
1833 account_profile_record (&profile_record[index], subpass);
1836 /* Output profile consistency. */
1838 void
1839 dump_profile_report (void)
1841 int i, j;
1842 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1843 gcov_type last_time = 0, last_size = 0;
1844 double rel_time_change, rel_size_change;
1845 int last_reported = 0;
1847 if (!profile_record)
1848 return;
1849 fprintf (stderr, "\nProfile consistency report:\n\n");
1850 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1851 fprintf (stderr, " |freq count |freq count |size time\n");
1853 for (i = 0; i < passes_by_id_size; i++)
1854 for (j = 0 ; j < 2; j++)
1855 if (profile_record[i].run)
1857 if (last_time)
1858 rel_time_change = (profile_record[i].time[j]
1859 - (double)last_time) * 100 / (double)last_time;
1860 else
1861 rel_time_change = 0;
1862 if (last_size)
1863 rel_size_change = (profile_record[i].size[j]
1864 - (double)last_size) * 100 / (double)last_size;
1865 else
1866 rel_size_change = 0;
1868 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1869 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1870 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1871 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1872 || rel_time_change || rel_size_change)
1874 last_reported = i;
1875 fprintf (stderr, "%-20s %s",
1876 passes_by_id [i]->name,
1877 j ? "(after TODO)" : " ");
1878 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1879 fprintf (stderr, "| %+5i",
1880 profile_record[i].num_mismatched_freq_in[j]
1881 - last_freq_in);
1882 else
1883 fprintf (stderr, "| ");
1884 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1885 fprintf (stderr, " %+5i",
1886 profile_record[i].num_mismatched_count_in[j]
1887 - last_count_in);
1888 else
1889 fprintf (stderr, " ");
1890 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1891 fprintf (stderr, "| %+5i",
1892 profile_record[i].num_mismatched_freq_out[j]
1893 - last_freq_out);
1894 else
1895 fprintf (stderr, "| ");
1896 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1897 fprintf (stderr, " %+5i",
1898 profile_record[i].num_mismatched_count_out[j]
1899 - last_count_out);
1900 else
1901 fprintf (stderr, " ");
1903 /* Size/time units change across gimple and RTL. */
1904 if (i == pass_expand.pass.static_pass_number)
1905 fprintf (stderr, "|----------");
1906 else
1908 if (rel_size_change)
1909 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1910 else
1911 fprintf (stderr, "| ");
1912 if (rel_time_change)
1913 fprintf (stderr, " %+8.4f%%", rel_time_change);
1915 fprintf (stderr, "\n");
1916 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1917 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1918 last_count_in = profile_record[i].num_mismatched_count_in[j];
1919 last_count_out = profile_record[i].num_mismatched_count_out[j];
1921 else if (j && last_reported != i)
1923 last_reported = i;
1924 fprintf (stderr, "%-20s ------------| | |\n",
1925 passes_by_id [i]->name);
1927 last_time = profile_record[i].time[j];
1928 last_size = profile_record[i].size[j];
1932 /* Perform all TODO actions that ought to be done on each function. */
1934 static void
1935 execute_function_todo (void *data)
1937 unsigned int flags = (size_t)data;
1938 flags &= ~cfun->last_verified;
1939 if (!flags)
1940 return;
1942 /* Always cleanup the CFG before trying to update SSA. */
1943 if (flags & TODO_cleanup_cfg)
1945 cleanup_tree_cfg ();
1947 /* When cleanup_tree_cfg merges consecutive blocks, it may
1948 perform some simplistic propagation when removing single
1949 valued PHI nodes. This propagation may, in turn, cause the
1950 SSA form to become out-of-date (see PR 22037). So, even
1951 if the parent pass had not scheduled an SSA update, we may
1952 still need to do one. */
1953 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1954 flags |= TODO_update_ssa;
1957 if (flags & TODO_update_ssa_any)
1959 unsigned update_flags = flags & TODO_update_ssa_any;
1960 update_ssa (update_flags);
1961 cfun->last_verified &= ~TODO_verify_ssa;
1964 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1965 compute_may_aliases ();
1967 if (optimize && (flags & TODO_update_address_taken))
1968 execute_update_addresses_taken ();
1970 if (flags & TODO_remove_unused_locals)
1971 remove_unused_locals ();
1973 if (flags & TODO_rebuild_frequencies)
1974 rebuild_frequencies ();
1976 if (flags & TODO_rebuild_cgraph_edges)
1977 rebuild_cgraph_edges ();
1979 /* If we've seen errors do not bother running any verifiers. */
1980 if (seen_error ())
1981 return;
1983 #if defined ENABLE_CHECKING
1984 if (flags & TODO_verify_ssa
1985 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1987 verify_gimple_in_cfg (cfun);
1988 verify_ssa (true);
1990 else if (flags & TODO_verify_stmts)
1991 verify_gimple_in_cfg (cfun);
1992 if (flags & TODO_verify_flow)
1993 verify_flow_info ();
1994 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1995 verify_loop_closed_ssa (false);
1996 if (flags & TODO_verify_rtl_sharing)
1997 verify_rtl_sharing ();
1998 #endif
2000 cfun->last_verified = flags & TODO_verify_all;
2003 /* Perform all TODO actions. */
2004 static void
2005 execute_todo (unsigned int flags)
2007 #if defined ENABLE_CHECKING
2008 if (cfun
2009 && need_ssa_update_p (cfun))
2010 gcc_assert (flags & TODO_update_ssa_any);
2011 #endif
2013 timevar_push (TV_TODO);
2015 /* Inform the pass whether it is the first time it is run. */
2016 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2018 statistics_fini_pass ();
2020 do_per_function (execute_function_todo, (void *)(size_t) flags);
2022 /* Always remove functions just as before inlining: IPA passes might be
2023 interested to see bodies of extern inline functions that are not inlined
2024 to analyze side effects. The full removal is done just at the end
2025 of IPA pass queue. */
2026 if (flags & TODO_remove_functions)
2028 gcc_assert (!cfun);
2029 symtab_remove_unreachable_nodes (true, dump_file);
2032 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2034 gcc_assert (!cfun);
2035 dump_symtab (dump_file);
2036 /* Flush the file. If verification fails, we won't be able to
2037 close the file before aborting. */
2038 fflush (dump_file);
2041 if (flags & TODO_ggc_collect)
2042 ggc_collect ();
2044 /* Now that the dumping has been done, we can get rid of the optional
2045 df problems. */
2046 if (flags & TODO_df_finish)
2047 df_finish_pass ((flags & TODO_df_verify) != 0);
2049 timevar_pop (TV_TODO);
2052 /* Verify invariants that should hold between passes. This is a place
2053 to put simple sanity checks. */
2055 static void
2056 verify_interpass_invariants (void)
2058 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2061 /* Clear the last verified flag. */
2063 static void
2064 clear_last_verified (void *data ATTRIBUTE_UNUSED)
2066 cfun->last_verified = 0;
2069 /* Helper function. Verify that the properties has been turn into the
2070 properties expected by the pass. */
2072 #ifdef ENABLE_CHECKING
2073 static void
2074 verify_curr_properties (void *data)
2076 unsigned int props = (size_t)data;
2077 gcc_assert ((cfun->curr_properties & props) == props);
2079 #endif
2081 /* Initialize pass dump file. */
2082 /* This is non-static so that the plugins can use it. */
2084 bool
2085 pass_init_dump_file (struct opt_pass *pass)
2087 /* If a dump file name is present, open it if enabled. */
2088 if (pass->static_pass_number != -1)
2090 timevar_push (TV_DUMP);
2091 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
2092 dump_file_name = get_dump_file_name (pass->static_pass_number);
2093 dump_start (pass->static_pass_number, &dump_flags);
2094 if (dump_file && current_function_decl)
2095 dump_function_header (dump_file, current_function_decl, dump_flags);
2096 if (initializing_dump
2097 && dump_file && (dump_flags & TDF_GRAPH)
2098 && cfun && (cfun->curr_properties & PROP_cfg))
2099 clean_graph_dump_file (dump_file_name);
2100 timevar_pop (TV_DUMP);
2101 return initializing_dump;
2103 else
2104 return false;
2107 /* Flush PASS dump file. */
2108 /* This is non-static so that plugins can use it. */
2110 void
2111 pass_fini_dump_file (struct opt_pass *pass)
2113 timevar_push (TV_DUMP);
2115 /* Flush and close dump file. */
2116 if (dump_file_name)
2118 free (CONST_CAST (char *, dump_file_name));
2119 dump_file_name = NULL;
2122 dump_finish (pass->static_pass_number);
2123 timevar_pop (TV_DUMP);
2126 /* After executing the pass, apply expected changes to the function
2127 properties. */
2129 static void
2130 update_properties_after_pass (void *data)
2132 struct opt_pass *pass = (struct opt_pass *) data;
2133 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
2134 & ~pass->properties_destroyed;
2137 /* Execute summary generation for all of the passes in IPA_PASS. */
2139 void
2140 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
2142 while (ipa_pass)
2144 struct opt_pass *pass = &ipa_pass->pass;
2146 /* Execute all of the IPA_PASSes in the list. */
2147 if (ipa_pass->pass.type == IPA_PASS
2148 && (!pass->gate || pass->gate ())
2149 && ipa_pass->generate_summary)
2151 pass_init_dump_file (pass);
2153 /* If a timevar is present, start it. */
2154 if (pass->tv_id)
2155 timevar_push (pass->tv_id);
2157 ipa_pass->generate_summary ();
2159 /* Stop timevar. */
2160 if (pass->tv_id)
2161 timevar_pop (pass->tv_id);
2163 pass_fini_dump_file (pass);
2165 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
2169 /* Execute IPA_PASS function transform on NODE. */
2171 static void
2172 execute_one_ipa_transform_pass (struct cgraph_node *node,
2173 struct ipa_opt_pass_d *ipa_pass)
2175 struct opt_pass *pass = &ipa_pass->pass;
2176 unsigned int todo_after = 0;
2178 current_pass = pass;
2179 if (!ipa_pass->function_transform)
2180 return;
2182 /* Note that the folders should only create gimple expressions.
2183 This is a hack until the new folder is ready. */
2184 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2186 pass_init_dump_file (pass);
2188 /* Run pre-pass verification. */
2189 execute_todo (ipa_pass->function_transform_todo_flags_start);
2191 /* If a timevar is present, start it. */
2192 if (pass->tv_id != TV_NONE)
2193 timevar_push (pass->tv_id);
2195 /* Do it! */
2196 todo_after = ipa_pass->function_transform (node);
2198 /* Stop timevar. */
2199 if (pass->tv_id != TV_NONE)
2200 timevar_pop (pass->tv_id);
2202 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2203 check_profile_consistency (pass->static_pass_number, 0, true);
2205 /* Run post-pass cleanup and verification. */
2206 execute_todo (todo_after);
2207 verify_interpass_invariants ();
2208 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2209 check_profile_consistency (pass->static_pass_number, 1, true);
2211 do_per_function (execute_function_dump, NULL);
2212 pass_fini_dump_file (pass);
2214 current_pass = NULL;
2217 /* For the current function, execute all ipa transforms. */
2219 void
2220 execute_all_ipa_transforms (void)
2222 struct cgraph_node *node;
2223 if (!cfun)
2224 return;
2225 node = cgraph_get_node (current_function_decl);
2227 if (node->ipa_transforms_to_apply.exists ())
2229 unsigned int i;
2231 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2232 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2233 node->ipa_transforms_to_apply.release ();
2237 /* Callback for do_per_function to apply all IPA transforms. */
2239 static void
2240 apply_ipa_transforms (void *data)
2242 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2243 if (!node->global.inlined_to && node->ipa_transforms_to_apply.exists ())
2245 *(bool *)data = true;
2246 execute_all_ipa_transforms();
2247 rebuild_cgraph_edges ();
2251 /* Check if PASS is explicitly disabled or enabled and return
2252 the gate status. FUNC is the function to be processed, and
2253 GATE_STATUS is the gate status determined by pass manager by
2254 default. */
2256 static bool
2257 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2259 bool explicitly_enabled = false;
2260 bool explicitly_disabled = false;
2262 explicitly_enabled
2263 = is_pass_explicitly_enabled_or_disabled (pass, func,
2264 enabled_pass_uid_range_tab);
2265 explicitly_disabled
2266 = is_pass_explicitly_enabled_or_disabled (pass, func,
2267 disabled_pass_uid_range_tab);
2269 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2271 return gate_status;
2275 /* Execute PASS. */
2277 bool
2278 execute_one_pass (struct opt_pass *pass)
2280 unsigned int todo_after = 0;
2282 bool gate_status;
2284 /* IPA passes are executed on whole program, so cfun should be NULL.
2285 Other passes need function context set. */
2286 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2287 gcc_assert (!cfun && !current_function_decl);
2288 else
2289 gcc_assert (cfun && current_function_decl);
2291 current_pass = pass;
2293 /* Check whether gate check should be avoided.
2294 User controls the value of the gate through the parameter "gate_status". */
2295 gate_status = (pass->gate == NULL) ? true : pass->gate();
2296 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2298 /* Override gate with plugin. */
2299 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2301 if (!gate_status)
2303 /* Run so passes selectively disabling themselves on a given function
2304 are not miscounted. */
2305 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2307 check_profile_consistency (pass->static_pass_number, 0, false);
2308 check_profile_consistency (pass->static_pass_number, 1, false);
2310 current_pass = NULL;
2311 return false;
2314 /* Pass execution event trigger: useful to identify passes being
2315 executed. */
2316 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2318 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2319 Apply all trnasforms first. */
2320 if (pass->type == SIMPLE_IPA_PASS)
2322 bool applied = false;
2323 do_per_function (apply_ipa_transforms, (void *)&applied);
2324 if (applied)
2325 symtab_remove_unreachable_nodes (true, dump_file);
2326 /* Restore current_pass. */
2327 current_pass = pass;
2330 if (!quiet_flag && !cfun)
2331 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2333 /* Note that the folders should only create gimple expressions.
2334 This is a hack until the new folder is ready. */
2335 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2337 pass_init_dump_file (pass);
2339 /* Run pre-pass verification. */
2340 execute_todo (pass->todo_flags_start);
2342 #ifdef ENABLE_CHECKING
2343 do_per_function (verify_curr_properties,
2344 (void *)(size_t)pass->properties_required);
2345 #endif
2347 /* If a timevar is present, start it. */
2348 if (pass->tv_id != TV_NONE)
2349 timevar_push (pass->tv_id);
2351 /* Do it! */
2352 if (pass->execute)
2354 todo_after = pass->execute ();
2355 do_per_function (clear_last_verified, NULL);
2358 /* Stop timevar. */
2359 if (pass->tv_id != TV_NONE)
2360 timevar_pop (pass->tv_id);
2362 do_per_function (update_properties_after_pass, pass);
2364 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2365 check_profile_consistency (pass->static_pass_number, 0, true);
2367 /* Run post-pass cleanup and verification. */
2368 execute_todo (todo_after | pass->todo_flags_finish);
2369 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2370 check_profile_consistency (pass->static_pass_number, 1, true);
2372 verify_interpass_invariants ();
2373 do_per_function (execute_function_dump, NULL);
2374 if (pass->type == IPA_PASS)
2376 struct cgraph_node *node;
2377 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2378 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *)pass);
2381 if (!current_function_decl)
2382 cgraph_process_new_functions ();
2384 pass_fini_dump_file (pass);
2386 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2387 gcc_assert (!(cfun->curr_properties & PROP_trees)
2388 || pass->type != RTL_PASS);
2390 current_pass = NULL;
2392 return true;
2395 void
2396 execute_pass_list (struct opt_pass *pass)
2400 gcc_assert (pass->type == GIMPLE_PASS
2401 || pass->type == RTL_PASS);
2402 if (execute_one_pass (pass) && pass->sub)
2403 execute_pass_list (pass->sub);
2404 pass = pass->next;
2406 while (pass);
2409 /* Same as execute_pass_list but assume that subpasses of IPA passes
2410 are local passes. If SET is not NULL, write out summaries of only
2411 those node in SET. */
2413 static void
2414 ipa_write_summaries_2 (struct opt_pass *pass, struct lto_out_decl_state *state)
2416 while (pass)
2418 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2419 gcc_assert (!current_function_decl);
2420 gcc_assert (!cfun);
2421 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2422 if (pass->type == IPA_PASS
2423 && ipa_pass->write_summary
2424 && (!pass->gate || pass->gate ()))
2426 /* If a timevar is present, start it. */
2427 if (pass->tv_id)
2428 timevar_push (pass->tv_id);
2430 pass_init_dump_file (pass);
2432 ipa_pass->write_summary ();
2434 pass_fini_dump_file (pass);
2436 /* If a timevar is present, start it. */
2437 if (pass->tv_id)
2438 timevar_pop (pass->tv_id);
2441 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2442 ipa_write_summaries_2 (pass->sub, state);
2444 pass = pass->next;
2448 /* Helper function of ipa_write_summaries. Creates and destroys the
2449 decl state and calls ipa_write_summaries_2 for all passes that have
2450 summaries. SET is the set of nodes to be written. */
2452 static void
2453 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2455 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2456 state->symtab_node_encoder = encoder;
2458 lto_push_out_decl_state (state);
2460 gcc_assert (!flag_wpa);
2461 ipa_write_summaries_2 (all_regular_ipa_passes, state);
2462 ipa_write_summaries_2 (all_lto_gen_passes, state);
2464 gcc_assert (lto_get_out_decl_state () == state);
2465 lto_pop_out_decl_state ();
2466 lto_delete_out_decl_state (state);
2469 /* Write out summaries for all the nodes in the callgraph. */
2471 void
2472 ipa_write_summaries (void)
2474 lto_symtab_encoder_t encoder;
2475 int i, order_pos;
2476 struct varpool_node *vnode;
2477 struct cgraph_node **order;
2479 if (!flag_generate_lto || seen_error ())
2480 return;
2482 encoder = lto_symtab_encoder_new (false);
2484 /* Create the callgraph set in the same order used in
2485 cgraph_expand_all_functions. This mostly facilitates debugging,
2486 since it causes the gimple file to be processed in the same order
2487 as the source code. */
2488 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2489 order_pos = ipa_reverse_postorder (order);
2490 gcc_assert (order_pos == cgraph_n_nodes);
2492 for (i = order_pos - 1; i >= 0; i--)
2494 struct cgraph_node *node = order[i];
2496 if (cgraph_function_with_gimple_body_p (node))
2498 /* When streaming out references to statements as part of some IPA
2499 pass summary, the statements need to have uids assigned and the
2500 following does that for all the IPA passes here. Naturally, this
2501 ordering then matches the one IPA-passes get in their stmt_fixup
2502 hooks. */
2504 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2505 renumber_gimple_stmt_uids ();
2506 pop_cfun ();
2508 if (node->analyzed)
2509 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
2512 FOR_EACH_DEFINED_VARIABLE (vnode)
2513 if ((!vnode->alias || vnode->alias_of))
2514 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
2516 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2518 free (order);
2521 /* Same as execute_pass_list but assume that subpasses of IPA passes
2522 are local passes. If SET is not NULL, write out optimization summaries of
2523 only those node in SET. */
2525 static void
2526 ipa_write_optimization_summaries_1 (struct opt_pass *pass, struct lto_out_decl_state *state)
2528 while (pass)
2530 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2531 gcc_assert (!current_function_decl);
2532 gcc_assert (!cfun);
2533 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2534 if (pass->type == IPA_PASS
2535 && ipa_pass->write_optimization_summary
2536 && (!pass->gate || pass->gate ()))
2538 /* If a timevar is present, start it. */
2539 if (pass->tv_id)
2540 timevar_push (pass->tv_id);
2542 pass_init_dump_file (pass);
2544 ipa_pass->write_optimization_summary ();
2546 pass_fini_dump_file (pass);
2548 /* If a timevar is present, start it. */
2549 if (pass->tv_id)
2550 timevar_pop (pass->tv_id);
2553 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2554 ipa_write_optimization_summaries_1 (pass->sub, state);
2556 pass = pass->next;
2560 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2561 NULL, write out all summaries of all nodes. */
2563 void
2564 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2566 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2567 lto_symtab_encoder_iterator lsei;
2568 state->symtab_node_encoder = encoder;
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->analyzed
2581 && gimple_has_body_p (node->symbol.decl))
2583 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2584 renumber_gimple_stmt_uids ();
2585 pop_cfun ();
2589 gcc_assert (flag_wpa);
2590 ipa_write_optimization_summaries_1 (all_regular_ipa_passes, state);
2591 ipa_write_optimization_summaries_1 (all_lto_gen_passes, state);
2593 gcc_assert (lto_get_out_decl_state () == state);
2594 lto_pop_out_decl_state ();
2595 lto_delete_out_decl_state (state);
2598 /* Same as execute_pass_list but assume that subpasses of IPA passes
2599 are local passes. */
2601 static void
2602 ipa_read_summaries_1 (struct opt_pass *pass)
2604 while (pass)
2606 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2608 gcc_assert (!current_function_decl);
2609 gcc_assert (!cfun);
2610 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2612 if (pass->gate == NULL || pass->gate ())
2614 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2616 /* If a timevar is present, start it. */
2617 if (pass->tv_id)
2618 timevar_push (pass->tv_id);
2620 pass_init_dump_file (pass);
2622 ipa_pass->read_summary ();
2624 pass_fini_dump_file (pass);
2626 /* Stop timevar. */
2627 if (pass->tv_id)
2628 timevar_pop (pass->tv_id);
2631 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2632 ipa_read_summaries_1 (pass->sub);
2634 pass = pass->next;
2639 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2641 void
2642 ipa_read_summaries (void)
2644 ipa_read_summaries_1 (all_regular_ipa_passes);
2645 ipa_read_summaries_1 (all_lto_gen_passes);
2648 /* Same as execute_pass_list but assume that subpasses of IPA passes
2649 are local passes. */
2651 static void
2652 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2654 while (pass)
2656 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2658 gcc_assert (!current_function_decl);
2659 gcc_assert (!cfun);
2660 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2662 if (pass->gate == NULL || pass->gate ())
2664 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2666 /* If a timevar is present, start it. */
2667 if (pass->tv_id)
2668 timevar_push (pass->tv_id);
2670 pass_init_dump_file (pass);
2672 ipa_pass->read_optimization_summary ();
2674 pass_fini_dump_file (pass);
2676 /* Stop timevar. */
2677 if (pass->tv_id)
2678 timevar_pop (pass->tv_id);
2681 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2682 ipa_read_optimization_summaries_1 (pass->sub);
2684 pass = pass->next;
2688 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2690 void
2691 ipa_read_optimization_summaries (void)
2693 ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
2694 ipa_read_optimization_summaries_1 (all_lto_gen_passes);
2697 /* Same as execute_pass_list but assume that subpasses of IPA passes
2698 are local passes. */
2699 void
2700 execute_ipa_pass_list (struct opt_pass *pass)
2704 gcc_assert (!current_function_decl);
2705 gcc_assert (!cfun);
2706 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2707 if (execute_one_pass (pass) && pass->sub)
2709 if (pass->sub->type == GIMPLE_PASS)
2711 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2712 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2713 pass->sub);
2714 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2716 else if (pass->sub->type == SIMPLE_IPA_PASS
2717 || pass->sub->type == IPA_PASS)
2718 execute_ipa_pass_list (pass->sub);
2719 else
2720 gcc_unreachable ();
2722 gcc_assert (!current_function_decl);
2723 cgraph_process_new_functions ();
2724 pass = pass->next;
2726 while (pass);
2729 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2731 static void
2732 execute_ipa_stmt_fixups (struct opt_pass *pass,
2733 struct cgraph_node *node, gimple *stmts)
2735 while (pass)
2737 /* Execute all of the IPA_PASSes in the list. */
2738 if (pass->type == IPA_PASS
2739 && (!pass->gate || pass->gate ()))
2741 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2743 if (ipa_pass->stmt_fixup)
2745 pass_init_dump_file (pass);
2746 /* If a timevar is present, start it. */
2747 if (pass->tv_id)
2748 timevar_push (pass->tv_id);
2750 ipa_pass->stmt_fixup (node, stmts);
2752 /* Stop timevar. */
2753 if (pass->tv_id)
2754 timevar_pop (pass->tv_id);
2755 pass_fini_dump_file (pass);
2757 if (pass->sub)
2758 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2760 pass = pass->next;
2764 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2766 void
2767 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2769 execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
2773 extern void debug_properties (unsigned int);
2774 extern void dump_properties (FILE *, unsigned int);
2776 DEBUG_FUNCTION void
2777 dump_properties (FILE *dump, unsigned int props)
2779 fprintf (dump, "Properties:\n");
2780 if (props & PROP_gimple_any)
2781 fprintf (dump, "PROP_gimple_any\n");
2782 if (props & PROP_gimple_lcf)
2783 fprintf (dump, "PROP_gimple_lcf\n");
2784 if (props & PROP_gimple_leh)
2785 fprintf (dump, "PROP_gimple_leh\n");
2786 if (props & PROP_cfg)
2787 fprintf (dump, "PROP_cfg\n");
2788 if (props & PROP_ssa)
2789 fprintf (dump, "PROP_ssa\n");
2790 if (props & PROP_no_crit_edges)
2791 fprintf (dump, "PROP_no_crit_edges\n");
2792 if (props & PROP_rtl)
2793 fprintf (dump, "PROP_rtl\n");
2794 if (props & PROP_gimple_lomp)
2795 fprintf (dump, "PROP_gimple_lomp\n");
2796 if (props & PROP_gimple_lcx)
2797 fprintf (dump, "PROP_gimple_lcx\n");
2798 if (props & PROP_cfglayout)
2799 fprintf (dump, "PROP_cfglayout\n");
2802 DEBUG_FUNCTION void
2803 debug_properties (unsigned int props)
2805 dump_properties (stderr, props);
2808 /* Called by local passes to see if function is called by already processed nodes.
2809 Because we process nodes in topological order, this means that function is
2810 in recursive cycle or we introduced new direct calls. */
2811 bool
2812 function_called_by_processed_nodes_p (void)
2814 struct cgraph_edge *e;
2815 for (e = cgraph_get_node (current_function_decl)->callers;
2817 e = e->next_caller)
2819 if (e->caller->symbol.decl == current_function_decl)
2820 continue;
2821 if (!cgraph_function_with_gimple_body_p (e->caller))
2822 continue;
2823 if (TREE_ASM_WRITTEN (e->caller->symbol.decl))
2824 continue;
2825 if (!e->caller->process && !e->caller->global.inlined_to)
2826 break;
2828 if (dump_file && e)
2830 fprintf (dump_file, "Already processed call to:\n");
2831 dump_cgraph_node (dump_file, e->caller);
2833 return e != NULL;
2836 #include "gt-passes.h"