This patch fixes the issue of indirect call promotion while using AutoFDO optimized...
[official-gcc.git] / gcc-4_8 / gcc / passes.c
blob1aa060057493eb203629154a31350168610568fa
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 /* pass_build_alias is a dummy pass that ensures that we
1423 execute TODO_rebuild_alias at this point. */
1424 NEXT_PASS (pass_build_alias);
1425 NEXT_PASS (pass_return_slot);
1426 NEXT_PASS (pass_phiprop);
1427 NEXT_PASS (pass_fre);
1428 NEXT_PASS (pass_copy_prop);
1429 NEXT_PASS (pass_merge_phi);
1430 NEXT_PASS (pass_vrp);
1431 NEXT_PASS (pass_dce);
1432 NEXT_PASS (pass_call_cdce);
1433 NEXT_PASS (pass_cselim);
1434 NEXT_PASS (pass_tree_ifcombine);
1435 NEXT_PASS (pass_phiopt);
1436 NEXT_PASS (pass_tail_recursion);
1437 NEXT_PASS (pass_ch);
1438 NEXT_PASS (pass_stdarg);
1439 NEXT_PASS (pass_lower_complex);
1440 NEXT_PASS (pass_sra);
1441 NEXT_PASS (pass_rename_ssa_copies);
1442 /* The dom pass will also resolve all __builtin_constant_p calls
1443 that are still there to 0. This has to be done after some
1444 propagations have already run, but before some more dead code
1445 is removed, and this place fits nicely. Remember this when
1446 trying to move or duplicate pass_dominator somewhere earlier. */
1447 NEXT_PASS (pass_dominator);
1448 /* The only const/copy propagation opportunities left after
1449 DOM should be due to degenerate PHI nodes. So rather than
1450 run the full propagators, run a specialized pass which
1451 only examines PHIs to discover const/copy propagation
1452 opportunities. */
1453 NEXT_PASS (pass_phi_only_cprop);
1454 NEXT_PASS (pass_dse);
1455 NEXT_PASS (pass_reassoc);
1456 NEXT_PASS (pass_dce);
1457 NEXT_PASS (pass_forwprop);
1458 NEXT_PASS (pass_phiopt);
1459 NEXT_PASS (pass_object_sizes);
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 NEXT_PASS (pass_late_warn_uninitialized);
1568 NEXT_PASS (pass_uncprop);
1569 NEXT_PASS (pass_local_pure_const);
1571 NEXT_PASS (pass_tm_init);
1573 struct opt_pass **p = &pass_tm_init.pass.sub;
1574 NEXT_PASS (pass_tm_mark);
1575 NEXT_PASS (pass_tm_memopt);
1576 NEXT_PASS (pass_tm_edges);
1578 NEXT_PASS (pass_lower_complex_O0);
1579 NEXT_PASS (pass_asan_O0);
1580 NEXT_PASS (pass_tsan_O0);
1581 NEXT_PASS (pass_cleanup_eh);
1582 NEXT_PASS (pass_lower_resx);
1583 NEXT_PASS (pass_nrv);
1584 NEXT_PASS (pass_mudflap_2);
1585 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
1586 NEXT_PASS (pass_warn_function_noreturn);
1588 NEXT_PASS (pass_expand);
1590 NEXT_PASS (pass_rest_of_compilation);
1592 struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
1593 NEXT_PASS (pass_instantiate_virtual_regs);
1594 NEXT_PASS (pass_into_cfg_layout_mode);
1595 NEXT_PASS (pass_jump);
1596 NEXT_PASS (pass_lower_subreg);
1597 NEXT_PASS (pass_df_initialize_opt);
1598 NEXT_PASS (pass_cse);
1599 NEXT_PASS (pass_rtl_fwprop);
1600 NEXT_PASS (pass_rtl_cprop);
1601 NEXT_PASS (pass_rtl_pre);
1602 NEXT_PASS (pass_rtl_hoist);
1603 NEXT_PASS (pass_rtl_cprop);
1604 NEXT_PASS (pass_rtl_store_motion);
1605 NEXT_PASS (pass_cse_after_global_opts);
1606 NEXT_PASS (pass_rtl_ifcvt);
1607 NEXT_PASS (pass_reginfo_init);
1608 /* Perform loop optimizations. It might be better to do them a bit
1609 sooner, but we want the profile feedback to work more
1610 efficiently. */
1611 NEXT_PASS (pass_loop2);
1613 struct opt_pass **p = &pass_loop2.pass.sub;
1614 NEXT_PASS (pass_rtl_loop_init);
1615 NEXT_PASS (pass_rtl_move_loop_invariants);
1616 NEXT_PASS (pass_rtl_unswitch);
1617 NEXT_PASS (pass_rtl_unroll_and_peel_loops);
1618 NEXT_PASS (pass_rtl_doloop);
1619 NEXT_PASS (pass_rtl_loop_done);
1620 *p = NULL;
1622 NEXT_PASS (pass_web);
1623 NEXT_PASS (pass_rtl_cprop);
1624 NEXT_PASS (pass_cse2);
1625 NEXT_PASS (pass_rtl_dse1);
1626 NEXT_PASS (pass_rtl_fwprop_addr);
1627 NEXT_PASS (pass_inc_dec);
1628 NEXT_PASS (pass_initialize_regs);
1629 NEXT_PASS (pass_ud_rtl_dce);
1630 NEXT_PASS (pass_combine);
1631 NEXT_PASS (pass_if_after_combine);
1632 NEXT_PASS (pass_partition_blocks);
1633 NEXT_PASS (pass_regmove);
1634 NEXT_PASS (pass_outof_cfg_layout_mode);
1635 NEXT_PASS (pass_split_all_insns);
1636 NEXT_PASS (pass_lower_subreg2);
1637 NEXT_PASS (pass_df_initialize_no_opt);
1638 NEXT_PASS (pass_stack_ptr_mod);
1639 NEXT_PASS (pass_mode_switching);
1640 NEXT_PASS (pass_match_asm_constraints);
1641 NEXT_PASS (pass_sms);
1642 NEXT_PASS (pass_sched);
1643 NEXT_PASS (pass_ira);
1644 NEXT_PASS (pass_reload);
1645 NEXT_PASS (pass_postreload);
1647 struct opt_pass **p = &pass_postreload.pass.sub;
1648 NEXT_PASS (pass_postreload_cse);
1649 NEXT_PASS (pass_gcse2);
1650 NEXT_PASS (pass_split_after_reload);
1651 NEXT_PASS (pass_ree);
1652 NEXT_PASS (pass_compare_elim_after_reload);
1653 NEXT_PASS (pass_branch_target_load_optimize1);
1654 NEXT_PASS (pass_thread_prologue_and_epilogue);
1655 NEXT_PASS (pass_rtl_dse2);
1656 NEXT_PASS (pass_stack_adjustments);
1657 NEXT_PASS (pass_jump2);
1658 NEXT_PASS (pass_peephole2);
1659 NEXT_PASS (pass_if_after_reload);
1660 NEXT_PASS (pass_regrename);
1661 NEXT_PASS (pass_cprop_hardreg);
1662 NEXT_PASS (pass_fast_rtl_dce);
1663 NEXT_PASS (pass_reorder_blocks);
1664 NEXT_PASS (pass_branch_target_load_optimize2);
1665 NEXT_PASS (pass_leaf_regs);
1666 NEXT_PASS (pass_split_before_sched2);
1667 NEXT_PASS (pass_sched2);
1668 NEXT_PASS (pass_stack_regs);
1670 struct opt_pass **p = &pass_stack_regs.pass.sub;
1671 NEXT_PASS (pass_split_before_regstack);
1672 NEXT_PASS (pass_stack_regs_run);
1674 NEXT_PASS (pass_compute_alignments);
1675 NEXT_PASS (pass_duplicate_computed_gotos);
1676 NEXT_PASS (pass_variable_tracking);
1677 NEXT_PASS (pass_free_cfg);
1678 NEXT_PASS (pass_machine_reorg);
1679 NEXT_PASS (pass_cleanup_barriers);
1680 NEXT_PASS (pass_delay_slots);
1681 NEXT_PASS (pass_split_for_shorten_branches);
1682 NEXT_PASS (pass_convert_to_eh_region_ranges);
1683 NEXT_PASS (pass_shorten_branches);
1684 NEXT_PASS (pass_set_nothrow_function_flags);
1685 NEXT_PASS (pass_dwarf2_frame);
1686 NEXT_PASS (pass_final);
1688 NEXT_PASS (pass_df_finish);
1690 NEXT_PASS (pass_clean_state);
1691 *p = NULL;
1693 #undef NEXT_PASS
1695 /* Register the passes with the tree dump code. */
1696 register_dump_files (all_lowering_passes, PROP_gimple_any);
1697 register_dump_files (all_small_ipa_passes,
1698 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1699 | PROP_cfg);
1700 register_dump_files (all_regular_ipa_passes,
1701 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1702 | PROP_cfg);
1703 register_dump_files (all_lto_gen_passes,
1704 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1705 | PROP_cfg);
1706 register_dump_files (all_late_ipa_passes,
1707 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1708 | PROP_cfg);
1709 register_dump_files (all_passes,
1710 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1711 | PROP_cfg);
1714 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1715 function CALLBACK for every function in the call graph. Otherwise,
1716 call CALLBACK on the current function. */
1718 static void
1719 do_per_function (void (*callback) (void *data), void *data)
1721 if (current_function_decl)
1722 callback (data);
1723 else
1725 struct cgraph_node *node;
1726 FOR_EACH_DEFINED_FUNCTION (node)
1727 if (gimple_has_body_p (node->symbol.decl)
1728 && (!node->clone_of || node->symbol.decl != node->clone_of->symbol.decl))
1730 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1731 callback (data);
1732 if (!flag_wpa)
1734 free_dominance_info (CDI_DOMINATORS);
1735 free_dominance_info (CDI_POST_DOMINATORS);
1737 pop_cfun ();
1738 ggc_collect ();
1743 /* Because inlining might remove no-longer reachable nodes, we need to
1744 keep the array visible to garbage collector to avoid reading collected
1745 out nodes. */
1746 static int nnodes;
1747 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1749 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1750 function CALLBACK for every function in the call graph. Otherwise,
1751 call CALLBACK on the current function.
1752 This function is global so that plugins can use it. */
1753 void
1754 do_per_function_toporder (void (*callback) (void *data), void *data)
1756 int i;
1758 if (current_function_decl)
1759 callback (data);
1760 else
1762 gcc_assert (!order);
1763 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1764 nnodes = ipa_reverse_postorder (order);
1765 for (i = nnodes - 1; i >= 0; i--)
1766 order[i]->process = 1;
1767 for (i = nnodes - 1; i >= 0; i--)
1769 struct cgraph_node *node = order[i];
1771 /* Allow possibly removed nodes to be garbage collected. */
1772 order[i] = NULL;
1773 node->process = 0;
1774 if (cgraph_function_with_gimple_body_p (node))
1776 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1777 callback (data);
1778 free_dominance_info (CDI_DOMINATORS);
1779 free_dominance_info (CDI_POST_DOMINATORS);
1780 pop_cfun ();
1781 ggc_collect ();
1785 ggc_free (order);
1786 order = NULL;
1787 nnodes = 0;
1790 /* Helper function to perform function body dump. */
1792 static void
1793 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1795 if (dump_file && current_function_decl)
1797 if (cfun->curr_properties & PROP_trees)
1798 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1799 else
1800 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1802 /* Flush the file. If verification fails, we won't be able to
1803 close the file before aborting. */
1804 fflush (dump_file);
1806 if ((cfun->curr_properties & PROP_cfg)
1807 && (dump_flags & TDF_GRAPH))
1808 print_graph_cfg (dump_file_name, cfun);
1812 static struct profile_record *profile_record;
1814 /* Do profile consistency book-keeping for the pass with static number INDEX.
1815 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1816 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1817 if we are only book-keeping on passes that may have selectively disabled
1818 themselves on a given function. */
1819 static void
1820 check_profile_consistency (int index, int subpass, bool run)
1822 if (index == -1)
1823 return;
1824 if (!profile_record)
1825 profile_record = XCNEWVEC (struct profile_record,
1826 passes_by_id_size);
1827 gcc_assert (index < passes_by_id_size && index >= 0);
1828 gcc_assert (subpass < 2);
1829 profile_record[index].run |= run;
1830 account_profile_record (&profile_record[index], subpass);
1833 /* Output profile consistency. */
1835 void
1836 dump_profile_report (void)
1838 int i, j;
1839 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1840 gcov_type last_time = 0, last_size = 0;
1841 double rel_time_change, rel_size_change;
1842 int last_reported = 0;
1844 if (!profile_record)
1845 return;
1846 fprintf (stderr, "\nProfile consistency report:\n\n");
1847 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1848 fprintf (stderr, " |freq count |freq count |size time\n");
1850 for (i = 0; i < passes_by_id_size; i++)
1851 for (j = 0 ; j < 2; j++)
1852 if (profile_record[i].run)
1854 if (last_time)
1855 rel_time_change = (profile_record[i].time[j]
1856 - (double)last_time) * 100 / (double)last_time;
1857 else
1858 rel_time_change = 0;
1859 if (last_size)
1860 rel_size_change = (profile_record[i].size[j]
1861 - (double)last_size) * 100 / (double)last_size;
1862 else
1863 rel_size_change = 0;
1865 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1866 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1867 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1868 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1869 || rel_time_change || rel_size_change)
1871 last_reported = i;
1872 fprintf (stderr, "%-20s %s",
1873 passes_by_id [i]->name,
1874 j ? "(after TODO)" : " ");
1875 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1876 fprintf (stderr, "| %+5i",
1877 profile_record[i].num_mismatched_freq_in[j]
1878 - last_freq_in);
1879 else
1880 fprintf (stderr, "| ");
1881 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1882 fprintf (stderr, " %+5i",
1883 profile_record[i].num_mismatched_count_in[j]
1884 - last_count_in);
1885 else
1886 fprintf (stderr, " ");
1887 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1888 fprintf (stderr, "| %+5i",
1889 profile_record[i].num_mismatched_freq_out[j]
1890 - last_freq_out);
1891 else
1892 fprintf (stderr, "| ");
1893 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1894 fprintf (stderr, " %+5i",
1895 profile_record[i].num_mismatched_count_out[j]
1896 - last_count_out);
1897 else
1898 fprintf (stderr, " ");
1900 /* Size/time units change across gimple and RTL. */
1901 if (i == pass_expand.pass.static_pass_number)
1902 fprintf (stderr, "|----------");
1903 else
1905 if (rel_size_change)
1906 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1907 else
1908 fprintf (stderr, "| ");
1909 if (rel_time_change)
1910 fprintf (stderr, " %+8.4f%%", rel_time_change);
1912 fprintf (stderr, "\n");
1913 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1914 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1915 last_count_in = profile_record[i].num_mismatched_count_in[j];
1916 last_count_out = profile_record[i].num_mismatched_count_out[j];
1918 else if (j && last_reported != i)
1920 last_reported = i;
1921 fprintf (stderr, "%-20s ------------| | |\n",
1922 passes_by_id [i]->name);
1924 last_time = profile_record[i].time[j];
1925 last_size = profile_record[i].size[j];
1929 /* Perform all TODO actions that ought to be done on each function. */
1931 static void
1932 execute_function_todo (void *data)
1934 unsigned int flags = (size_t)data;
1935 flags &= ~cfun->last_verified;
1936 if (!flags)
1937 return;
1939 /* Always cleanup the CFG before trying to update SSA. */
1940 if (flags & TODO_cleanup_cfg)
1942 cleanup_tree_cfg ();
1944 /* When cleanup_tree_cfg merges consecutive blocks, it may
1945 perform some simplistic propagation when removing single
1946 valued PHI nodes. This propagation may, in turn, cause the
1947 SSA form to become out-of-date (see PR 22037). So, even
1948 if the parent pass had not scheduled an SSA update, we may
1949 still need to do one. */
1950 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1951 flags |= TODO_update_ssa;
1954 if (flags & TODO_update_ssa_any)
1956 unsigned update_flags = flags & TODO_update_ssa_any;
1957 update_ssa (update_flags);
1958 cfun->last_verified &= ~TODO_verify_ssa;
1961 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1962 compute_may_aliases ();
1964 if (optimize && (flags & TODO_update_address_taken))
1965 execute_update_addresses_taken ();
1967 if (flags & TODO_remove_unused_locals)
1968 remove_unused_locals ();
1970 if (flags & TODO_rebuild_frequencies)
1971 rebuild_frequencies ();
1973 if (flags & TODO_rebuild_cgraph_edges)
1974 rebuild_cgraph_edges ();
1976 /* If we've seen errors do not bother running any verifiers. */
1977 if (seen_error ())
1978 return;
1980 #if defined ENABLE_CHECKING
1981 if (flags & TODO_verify_ssa
1982 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1984 verify_gimple_in_cfg (cfun);
1985 verify_ssa (true);
1987 else if (flags & TODO_verify_stmts)
1988 verify_gimple_in_cfg (cfun);
1989 if (flags & TODO_verify_flow)
1990 verify_flow_info ();
1991 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1992 verify_loop_closed_ssa (false);
1993 if (flags & TODO_verify_rtl_sharing)
1994 verify_rtl_sharing ();
1995 #endif
1997 cfun->last_verified = flags & TODO_verify_all;
2000 /* Perform all TODO actions. */
2001 static void
2002 execute_todo (unsigned int flags)
2004 #if defined ENABLE_CHECKING
2005 if (cfun
2006 && need_ssa_update_p (cfun))
2007 gcc_assert (flags & TODO_update_ssa_any);
2008 #endif
2010 timevar_push (TV_TODO);
2012 /* Inform the pass whether it is the first time it is run. */
2013 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
2015 statistics_fini_pass ();
2017 do_per_function (execute_function_todo, (void *)(size_t) flags);
2019 /* Always remove functions just as before inlining: IPA passes might be
2020 interested to see bodies of extern inline functions that are not inlined
2021 to analyze side effects. The full removal is done just at the end
2022 of IPA pass queue. */
2023 if (flags & TODO_remove_functions)
2025 gcc_assert (!cfun);
2026 symtab_remove_unreachable_nodes (true, dump_file);
2029 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2031 gcc_assert (!cfun);
2032 dump_symtab (dump_file);
2033 /* Flush the file. If verification fails, we won't be able to
2034 close the file before aborting. */
2035 fflush (dump_file);
2038 if (flags & TODO_ggc_collect)
2039 ggc_collect ();
2041 /* Now that the dumping has been done, we can get rid of the optional
2042 df problems. */
2043 if (flags & TODO_df_finish)
2044 df_finish_pass ((flags & TODO_df_verify) != 0);
2046 timevar_pop (TV_TODO);
2049 /* Verify invariants that should hold between passes. This is a place
2050 to put simple sanity checks. */
2052 static void
2053 verify_interpass_invariants (void)
2055 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2058 /* Clear the last verified flag. */
2060 static void
2061 clear_last_verified (void *data ATTRIBUTE_UNUSED)
2063 cfun->last_verified = 0;
2066 /* Helper function. Verify that the properties has been turn into the
2067 properties expected by the pass. */
2069 #ifdef ENABLE_CHECKING
2070 static void
2071 verify_curr_properties (void *data)
2073 unsigned int props = (size_t)data;
2074 gcc_assert ((cfun->curr_properties & props) == props);
2076 #endif
2078 /* Initialize pass dump file. */
2079 /* This is non-static so that the plugins can use it. */
2081 bool
2082 pass_init_dump_file (struct opt_pass *pass)
2084 /* If a dump file name is present, open it if enabled. */
2085 if (pass->static_pass_number != -1)
2087 timevar_push (TV_DUMP);
2088 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
2089 dump_file_name = get_dump_file_name (pass->static_pass_number);
2090 dump_start (pass->static_pass_number, &dump_flags);
2091 if (dump_file && current_function_decl)
2092 dump_function_header (dump_file, current_function_decl, dump_flags);
2093 if (initializing_dump
2094 && dump_file && (dump_flags & TDF_GRAPH)
2095 && cfun && (cfun->curr_properties & PROP_cfg))
2096 clean_graph_dump_file (dump_file_name);
2097 timevar_pop (TV_DUMP);
2098 return initializing_dump;
2100 else
2101 return false;
2104 /* Flush PASS dump file. */
2105 /* This is non-static so that plugins can use it. */
2107 void
2108 pass_fini_dump_file (struct opt_pass *pass)
2110 timevar_push (TV_DUMP);
2112 /* Flush and close dump file. */
2113 if (dump_file_name)
2115 free (CONST_CAST (char *, dump_file_name));
2116 dump_file_name = NULL;
2119 dump_finish (pass->static_pass_number);
2120 timevar_pop (TV_DUMP);
2123 /* After executing the pass, apply expected changes to the function
2124 properties. */
2126 static void
2127 update_properties_after_pass (void *data)
2129 struct opt_pass *pass = (struct opt_pass *) data;
2130 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
2131 & ~pass->properties_destroyed;
2134 /* Execute summary generation for all of the passes in IPA_PASS. */
2136 void
2137 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
2139 while (ipa_pass)
2141 struct opt_pass *pass = &ipa_pass->pass;
2143 /* Execute all of the IPA_PASSes in the list. */
2144 if (ipa_pass->pass.type == IPA_PASS
2145 && (!pass->gate || pass->gate ())
2146 && ipa_pass->generate_summary)
2148 pass_init_dump_file (pass);
2150 /* If a timevar is present, start it. */
2151 if (pass->tv_id)
2152 timevar_push (pass->tv_id);
2154 ipa_pass->generate_summary ();
2156 /* Stop timevar. */
2157 if (pass->tv_id)
2158 timevar_pop (pass->tv_id);
2160 pass_fini_dump_file (pass);
2162 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
2166 /* Execute IPA_PASS function transform on NODE. */
2168 static void
2169 execute_one_ipa_transform_pass (struct cgraph_node *node,
2170 struct ipa_opt_pass_d *ipa_pass)
2172 struct opt_pass *pass = &ipa_pass->pass;
2173 unsigned int todo_after = 0;
2175 current_pass = pass;
2176 if (!ipa_pass->function_transform)
2177 return;
2179 /* Note that the folders should only create gimple expressions.
2180 This is a hack until the new folder is ready. */
2181 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2183 pass_init_dump_file (pass);
2185 /* Run pre-pass verification. */
2186 execute_todo (ipa_pass->function_transform_todo_flags_start);
2188 /* If a timevar is present, start it. */
2189 if (pass->tv_id != TV_NONE)
2190 timevar_push (pass->tv_id);
2192 /* Do it! */
2193 todo_after = ipa_pass->function_transform (node);
2195 /* Stop timevar. */
2196 if (pass->tv_id != TV_NONE)
2197 timevar_pop (pass->tv_id);
2199 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2200 check_profile_consistency (pass->static_pass_number, 0, true);
2202 /* Run post-pass cleanup and verification. */
2203 execute_todo (todo_after);
2204 verify_interpass_invariants ();
2205 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2206 check_profile_consistency (pass->static_pass_number, 1, true);
2208 do_per_function (execute_function_dump, NULL);
2209 pass_fini_dump_file (pass);
2211 current_pass = NULL;
2214 /* For the current function, execute all ipa transforms. */
2216 void
2217 execute_all_ipa_transforms (void)
2219 struct cgraph_node *node;
2220 if (!cfun)
2221 return;
2222 node = cgraph_get_node (current_function_decl);
2224 if (node->ipa_transforms_to_apply.exists ())
2226 unsigned int i;
2228 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2229 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2230 node->ipa_transforms_to_apply.release ();
2234 /* Callback for do_per_function to apply all IPA transforms. */
2236 static void
2237 apply_ipa_transforms (void *data)
2239 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2240 if (!node->global.inlined_to && node->ipa_transforms_to_apply.exists ())
2242 *(bool *)data = true;
2243 execute_all_ipa_transforms();
2244 rebuild_cgraph_edges ();
2248 /* Check if PASS is explicitly disabled or enabled and return
2249 the gate status. FUNC is the function to be processed, and
2250 GATE_STATUS is the gate status determined by pass manager by
2251 default. */
2253 static bool
2254 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2256 bool explicitly_enabled = false;
2257 bool explicitly_disabled = false;
2259 explicitly_enabled
2260 = is_pass_explicitly_enabled_or_disabled (pass, func,
2261 enabled_pass_uid_range_tab);
2262 explicitly_disabled
2263 = is_pass_explicitly_enabled_or_disabled (pass, func,
2264 disabled_pass_uid_range_tab);
2266 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2268 return gate_status;
2272 /* Execute PASS. */
2274 bool
2275 execute_one_pass (struct opt_pass *pass)
2277 unsigned int todo_after = 0;
2279 bool gate_status;
2281 /* IPA passes are executed on whole program, so cfun should be NULL.
2282 Other passes need function context set. */
2283 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2284 gcc_assert (!cfun && !current_function_decl);
2285 else
2286 gcc_assert (cfun && current_function_decl);
2288 current_pass = pass;
2290 /* Check whether gate check should be avoided.
2291 User controls the value of the gate through the parameter "gate_status". */
2292 gate_status = (pass->gate == NULL) ? true : pass->gate();
2293 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2295 /* Override gate with plugin. */
2296 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2298 if (!gate_status)
2300 /* Run so passes selectively disabling themselves on a given function
2301 are not miscounted. */
2302 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2304 check_profile_consistency (pass->static_pass_number, 0, false);
2305 check_profile_consistency (pass->static_pass_number, 1, false);
2307 current_pass = NULL;
2308 return false;
2311 /* Pass execution event trigger: useful to identify passes being
2312 executed. */
2313 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2315 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2316 Apply all trnasforms first. */
2317 if (pass->type == SIMPLE_IPA_PASS)
2319 bool applied = false;
2320 do_per_function (apply_ipa_transforms, (void *)&applied);
2321 if (applied)
2322 symtab_remove_unreachable_nodes (true, dump_file);
2323 /* Restore current_pass. */
2324 current_pass = pass;
2327 if (!quiet_flag && !cfun)
2328 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2330 /* Note that the folders should only create gimple expressions.
2331 This is a hack until the new folder is ready. */
2332 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2334 pass_init_dump_file (pass);
2336 /* Run pre-pass verification. */
2337 execute_todo (pass->todo_flags_start);
2339 #ifdef ENABLE_CHECKING
2340 do_per_function (verify_curr_properties,
2341 (void *)(size_t)pass->properties_required);
2342 #endif
2344 /* If a timevar is present, start it. */
2345 if (pass->tv_id != TV_NONE)
2346 timevar_push (pass->tv_id);
2348 /* Do it! */
2349 if (pass->execute)
2351 todo_after = pass->execute ();
2352 do_per_function (clear_last_verified, NULL);
2355 /* Stop timevar. */
2356 if (pass->tv_id != TV_NONE)
2357 timevar_pop (pass->tv_id);
2359 do_per_function (update_properties_after_pass, pass);
2361 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2362 check_profile_consistency (pass->static_pass_number, 0, true);
2364 /* Run post-pass cleanup and verification. */
2365 execute_todo (todo_after | pass->todo_flags_finish);
2366 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2367 check_profile_consistency (pass->static_pass_number, 1, true);
2369 verify_interpass_invariants ();
2370 do_per_function (execute_function_dump, NULL);
2371 if (pass->type == IPA_PASS)
2373 struct cgraph_node *node;
2374 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2375 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *)pass);
2378 if (!current_function_decl)
2379 cgraph_process_new_functions ();
2381 pass_fini_dump_file (pass);
2383 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2384 gcc_assert (!(cfun->curr_properties & PROP_trees)
2385 || pass->type != RTL_PASS);
2387 current_pass = NULL;
2389 return true;
2392 void
2393 execute_pass_list (struct opt_pass *pass)
2397 gcc_assert (pass->type == GIMPLE_PASS
2398 || pass->type == RTL_PASS);
2399 if (execute_one_pass (pass) && pass->sub)
2400 execute_pass_list (pass->sub);
2401 pass = pass->next;
2403 while (pass);
2406 /* Same as execute_pass_list but assume that subpasses of IPA passes
2407 are local passes. If SET is not NULL, write out summaries of only
2408 those node in SET. */
2410 static void
2411 ipa_write_summaries_2 (struct opt_pass *pass, struct lto_out_decl_state *state)
2413 while (pass)
2415 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2416 gcc_assert (!current_function_decl);
2417 gcc_assert (!cfun);
2418 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2419 if (pass->type == IPA_PASS
2420 && ipa_pass->write_summary
2421 && (!pass->gate || pass->gate ()))
2423 /* If a timevar is present, start it. */
2424 if (pass->tv_id)
2425 timevar_push (pass->tv_id);
2427 pass_init_dump_file (pass);
2429 ipa_pass->write_summary ();
2431 pass_fini_dump_file (pass);
2433 /* If a timevar is present, start it. */
2434 if (pass->tv_id)
2435 timevar_pop (pass->tv_id);
2438 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2439 ipa_write_summaries_2 (pass->sub, state);
2441 pass = pass->next;
2445 /* Helper function of ipa_write_summaries. Creates and destroys the
2446 decl state and calls ipa_write_summaries_2 for all passes that have
2447 summaries. SET is the set of nodes to be written. */
2449 static void
2450 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2452 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2453 state->symtab_node_encoder = encoder;
2455 lto_push_out_decl_state (state);
2457 gcc_assert (!flag_wpa);
2458 ipa_write_summaries_2 (all_regular_ipa_passes, state);
2459 ipa_write_summaries_2 (all_lto_gen_passes, state);
2461 gcc_assert (lto_get_out_decl_state () == state);
2462 lto_pop_out_decl_state ();
2463 lto_delete_out_decl_state (state);
2466 /* Write out summaries for all the nodes in the callgraph. */
2468 void
2469 ipa_write_summaries (void)
2471 lto_symtab_encoder_t encoder;
2472 int i, order_pos;
2473 struct varpool_node *vnode;
2474 struct cgraph_node **order;
2476 if (!flag_generate_lto || seen_error ())
2477 return;
2479 encoder = lto_symtab_encoder_new (false);
2481 /* Create the callgraph set in the same order used in
2482 cgraph_expand_all_functions. This mostly facilitates debugging,
2483 since it causes the gimple file to be processed in the same order
2484 as the source code. */
2485 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2486 order_pos = ipa_reverse_postorder (order);
2487 gcc_assert (order_pos == cgraph_n_nodes);
2489 for (i = order_pos - 1; i >= 0; i--)
2491 struct cgraph_node *node = order[i];
2493 if (cgraph_function_with_gimple_body_p (node))
2495 /* When streaming out references to statements as part of some IPA
2496 pass summary, the statements need to have uids assigned and the
2497 following does that for all the IPA passes here. Naturally, this
2498 ordering then matches the one IPA-passes get in their stmt_fixup
2499 hooks. */
2501 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2502 renumber_gimple_stmt_uids ();
2503 pop_cfun ();
2505 if (node->analyzed)
2506 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
2509 FOR_EACH_DEFINED_VARIABLE (vnode)
2510 if ((!vnode->alias || vnode->alias_of))
2511 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
2513 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2515 free (order);
2518 /* Same as execute_pass_list but assume that subpasses of IPA passes
2519 are local passes. If SET is not NULL, write out optimization summaries of
2520 only those node in SET. */
2522 static void
2523 ipa_write_optimization_summaries_1 (struct opt_pass *pass, struct lto_out_decl_state *state)
2525 while (pass)
2527 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2528 gcc_assert (!current_function_decl);
2529 gcc_assert (!cfun);
2530 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2531 if (pass->type == IPA_PASS
2532 && ipa_pass->write_optimization_summary
2533 && (!pass->gate || pass->gate ()))
2535 /* If a timevar is present, start it. */
2536 if (pass->tv_id)
2537 timevar_push (pass->tv_id);
2539 pass_init_dump_file (pass);
2541 ipa_pass->write_optimization_summary ();
2543 pass_fini_dump_file (pass);
2545 /* If a timevar is present, start it. */
2546 if (pass->tv_id)
2547 timevar_pop (pass->tv_id);
2550 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2551 ipa_write_optimization_summaries_1 (pass->sub, state);
2553 pass = pass->next;
2557 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2558 NULL, write out all summaries of all nodes. */
2560 void
2561 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2563 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2564 lto_symtab_encoder_iterator lsei;
2565 state->symtab_node_encoder = encoder;
2567 lto_push_out_decl_state (state);
2568 for (lsei = lsei_start_function_in_partition (encoder);
2569 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2571 struct cgraph_node *node = lsei_cgraph_node (lsei);
2572 /* When streaming out references to statements as part of some IPA
2573 pass summary, the statements need to have uids assigned.
2575 For functions newly born at WPA stage we need to initialize
2576 the uids here. */
2577 if (node->analyzed
2578 && gimple_has_body_p (node->symbol.decl))
2580 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2581 renumber_gimple_stmt_uids ();
2582 pop_cfun ();
2586 gcc_assert (flag_wpa);
2587 ipa_write_optimization_summaries_1 (all_regular_ipa_passes, state);
2588 ipa_write_optimization_summaries_1 (all_lto_gen_passes, state);
2590 gcc_assert (lto_get_out_decl_state () == state);
2591 lto_pop_out_decl_state ();
2592 lto_delete_out_decl_state (state);
2595 /* Same as execute_pass_list but assume that subpasses of IPA passes
2596 are local passes. */
2598 static void
2599 ipa_read_summaries_1 (struct opt_pass *pass)
2601 while (pass)
2603 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2605 gcc_assert (!current_function_decl);
2606 gcc_assert (!cfun);
2607 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2609 if (pass->gate == NULL || pass->gate ())
2611 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2613 /* If a timevar is present, start it. */
2614 if (pass->tv_id)
2615 timevar_push (pass->tv_id);
2617 pass_init_dump_file (pass);
2619 ipa_pass->read_summary ();
2621 pass_fini_dump_file (pass);
2623 /* Stop timevar. */
2624 if (pass->tv_id)
2625 timevar_pop (pass->tv_id);
2628 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2629 ipa_read_summaries_1 (pass->sub);
2631 pass = pass->next;
2636 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2638 void
2639 ipa_read_summaries (void)
2641 ipa_read_summaries_1 (all_regular_ipa_passes);
2642 ipa_read_summaries_1 (all_lto_gen_passes);
2645 /* Same as execute_pass_list but assume that subpasses of IPA passes
2646 are local passes. */
2648 static void
2649 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2651 while (pass)
2653 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2655 gcc_assert (!current_function_decl);
2656 gcc_assert (!cfun);
2657 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2659 if (pass->gate == NULL || pass->gate ())
2661 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2663 /* If a timevar is present, start it. */
2664 if (pass->tv_id)
2665 timevar_push (pass->tv_id);
2667 pass_init_dump_file (pass);
2669 ipa_pass->read_optimization_summary ();
2671 pass_fini_dump_file (pass);
2673 /* Stop timevar. */
2674 if (pass->tv_id)
2675 timevar_pop (pass->tv_id);
2678 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2679 ipa_read_optimization_summaries_1 (pass->sub);
2681 pass = pass->next;
2685 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2687 void
2688 ipa_read_optimization_summaries (void)
2690 ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
2691 ipa_read_optimization_summaries_1 (all_lto_gen_passes);
2694 /* Same as execute_pass_list but assume that subpasses of IPA passes
2695 are local passes. */
2696 void
2697 execute_ipa_pass_list (struct opt_pass *pass)
2701 gcc_assert (!current_function_decl);
2702 gcc_assert (!cfun);
2703 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2704 if (execute_one_pass (pass) && pass->sub)
2706 if (pass->sub->type == GIMPLE_PASS)
2708 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2709 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2710 pass->sub);
2711 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2713 else if (pass->sub->type == SIMPLE_IPA_PASS
2714 || pass->sub->type == IPA_PASS)
2715 execute_ipa_pass_list (pass->sub);
2716 else
2717 gcc_unreachable ();
2719 gcc_assert (!current_function_decl);
2720 cgraph_process_new_functions ();
2721 pass = pass->next;
2723 while (pass);
2726 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2728 static void
2729 execute_ipa_stmt_fixups (struct opt_pass *pass,
2730 struct cgraph_node *node, gimple *stmts)
2732 while (pass)
2734 /* Execute all of the IPA_PASSes in the list. */
2735 if (pass->type == IPA_PASS
2736 && (!pass->gate || pass->gate ()))
2738 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2740 if (ipa_pass->stmt_fixup)
2742 pass_init_dump_file (pass);
2743 /* If a timevar is present, start it. */
2744 if (pass->tv_id)
2745 timevar_push (pass->tv_id);
2747 ipa_pass->stmt_fixup (node, stmts);
2749 /* Stop timevar. */
2750 if (pass->tv_id)
2751 timevar_pop (pass->tv_id);
2752 pass_fini_dump_file (pass);
2754 if (pass->sub)
2755 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2757 pass = pass->next;
2761 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2763 void
2764 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2766 execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
2770 extern void debug_properties (unsigned int);
2771 extern void dump_properties (FILE *, unsigned int);
2773 DEBUG_FUNCTION void
2774 dump_properties (FILE *dump, unsigned int props)
2776 fprintf (dump, "Properties:\n");
2777 if (props & PROP_gimple_any)
2778 fprintf (dump, "PROP_gimple_any\n");
2779 if (props & PROP_gimple_lcf)
2780 fprintf (dump, "PROP_gimple_lcf\n");
2781 if (props & PROP_gimple_leh)
2782 fprintf (dump, "PROP_gimple_leh\n");
2783 if (props & PROP_cfg)
2784 fprintf (dump, "PROP_cfg\n");
2785 if (props & PROP_ssa)
2786 fprintf (dump, "PROP_ssa\n");
2787 if (props & PROP_no_crit_edges)
2788 fprintf (dump, "PROP_no_crit_edges\n");
2789 if (props & PROP_rtl)
2790 fprintf (dump, "PROP_rtl\n");
2791 if (props & PROP_gimple_lomp)
2792 fprintf (dump, "PROP_gimple_lomp\n");
2793 if (props & PROP_gimple_lcx)
2794 fprintf (dump, "PROP_gimple_lcx\n");
2795 if (props & PROP_cfglayout)
2796 fprintf (dump, "PROP_cfglayout\n");
2799 DEBUG_FUNCTION void
2800 debug_properties (unsigned int props)
2802 dump_properties (stderr, props);
2805 /* Called by local passes to see if function is called by already processed nodes.
2806 Because we process nodes in topological order, this means that function is
2807 in recursive cycle or we introduced new direct calls. */
2808 bool
2809 function_called_by_processed_nodes_p (void)
2811 struct cgraph_edge *e;
2812 for (e = cgraph_get_node (current_function_decl)->callers;
2814 e = e->next_caller)
2816 if (e->caller->symbol.decl == current_function_decl)
2817 continue;
2818 if (!cgraph_function_with_gimple_body_p (e->caller))
2819 continue;
2820 if (TREE_ASM_WRITTEN (e->caller->symbol.decl))
2821 continue;
2822 if (!e->caller->process && !e->caller->global.inlined_to)
2823 break;
2825 if (dump_file && e)
2827 fprintf (dump_file, "Already processed call to:\n");
2828 dump_cgraph_node (dump_file, e->caller);
2830 return e != NULL;
2833 #include "gt-passes.h"