gcc/upc/
[official-gcc.git] / gcc / passes.c
blob7709671a052feac38af2dc434fafee7e4c033af1
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011, 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This is the top level of cc1/c++.
23 It parses command args, opens files, invokes the various passes
24 in the proper order, and counts the time used by each.
25 Error messages and low-level interface to malloc also handled here. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "line-map.h"
32 #include "input.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "insn-attr.h"
38 #include "insn-config.h"
39 #include "insn-flags.h"
40 #include "hard-reg-set.h"
41 #include "recog.h"
42 #include "output.h"
43 #include "except.h"
44 #include "function.h"
45 #include "toplev.h"
46 #include "expr.h"
47 #include "basic-block.h"
48 #include "intl.h"
49 #include "ggc.h"
50 #include "graph.h"
51 #include "regs.h"
52 #include "diagnostic-core.h"
53 #include "params.h"
54 #include "reload.h"
55 #include "debug.h"
56 #include "target.h"
57 #include "langhooks.h"
58 #include "cfgloop.h"
59 #include "hosthooks.h"
60 #include "cgraph.h"
61 #include "opts.h"
62 #include "coverage.h"
63 #include "value-prof.h"
64 #include "tree-inline.h"
65 #include "tree-flow.h"
66 #include "tree-pass.h"
67 #include "tree-dump.h"
68 #include "df.h"
69 #include "predict.h"
70 #include "lto-streamer.h"
71 #include "plugin.h"
72 #include "ipa-utils.h"
73 #include "tree-pretty-print.h" /* for dump_function_header */
75 /* This is used for debugging. It allows the current pass to printed
76 from anywhere in compilation.
77 The variable current_pass is also used for statistics and plugins. */
78 struct opt_pass *current_pass;
80 static void register_pass_name (struct opt_pass *, const char *);
82 /* Call from anywhere to find out what pass this is. Useful for
83 printing out debugging information deep inside an service
84 routine. */
85 void
86 print_current_pass (FILE *file)
88 if (current_pass)
89 fprintf (file, "current pass = %s (%d)\n",
90 current_pass->name, current_pass->static_pass_number);
91 else
92 fprintf (file, "no current pass.\n");
96 /* Call from the debugger to get the current pass name. */
97 DEBUG_FUNCTION void
98 debug_pass (void)
100 print_current_pass (stderr);
105 /* Global variables used to communicate with passes. */
106 int dump_flags;
107 bool in_gimple_form;
108 bool first_pass_instance;
111 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
112 and TYPE_DECL nodes.
114 This does nothing for local (non-static) variables, unless the
115 variable is a register variable with DECL_ASSEMBLER_NAME set. In
116 that case, or if the variable is not an automatic, it sets up the
117 RTL and outputs any assembler code (label definition, storage
118 allocation and initialization).
120 DECL is the declaration. TOP_LEVEL is nonzero
121 if this declaration is not within a function. */
123 void
124 rest_of_decl_compilation (tree decl,
125 int top_level,
126 int at_end)
128 /* We deferred calling assemble_alias so that we could collect
129 other attributes such as visibility. Emit the alias now. */
130 if (!in_lto_p)
132 tree alias;
133 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
134 if (alias)
136 alias = TREE_VALUE (TREE_VALUE (alias));
137 alias = get_identifier (TREE_STRING_POINTER (alias));
138 /* A quirk of the initial implementation of aliases required that the
139 user add "extern" to all of them. Which is silly, but now
140 historical. Do note that the symbol is in fact locally defined. */
141 if (!lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
142 DECL_EXTERNAL (decl) = 0;
143 assemble_alias (decl, alias);
147 /* Can't defer this, because it needs to happen before any
148 later function definitions are processed. */
149 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
150 make_decl_rtl (decl);
152 /* Forward declarations for nested functions are not "external",
153 but we need to treat them as if they were. */
154 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
155 || TREE_CODE (decl) == FUNCTION_DECL)
157 timevar_push (TV_VARCONST);
159 /* Don't output anything when a tentative file-scope definition
160 is seen. But at end of compilation, do output code for them.
162 We do output all variables and rely on
163 callgraph code to defer them except for forward declarations
164 (see gcc.c-torture/compile/920624-1.c) */
165 if ((at_end
166 || !DECL_DEFER_OUTPUT (decl)
167 || DECL_INITIAL (decl))
168 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
169 && !DECL_EXTERNAL (decl))
171 /* When reading LTO unit, we also read varpool, so do not
172 rebuild it. */
173 if (in_lto_p && !at_end)
175 else if (TREE_CODE (decl) != FUNCTION_DECL)
176 varpool_finalize_decl (decl);
179 #ifdef ASM_FINISH_DECLARE_OBJECT
180 if (decl == last_assemble_variable_decl)
182 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
183 top_level, at_end);
185 #endif
187 timevar_pop (TV_VARCONST);
189 else if (TREE_CODE (decl) == TYPE_DECL
190 /* Like in rest_of_type_compilation, avoid confusing the debug
191 information machinery when there are errors. */
192 && !seen_error ())
194 timevar_push (TV_SYMOUT);
195 debug_hooks->type_decl (decl, !top_level);
196 timevar_pop (TV_SYMOUT);
199 /* Let cgraph know about the existence of variables. */
200 if (in_lto_p && !at_end)
202 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
203 && TREE_STATIC (decl))
204 varpool_node (decl);
207 /* Called after finishing a record, union or enumeral type. */
209 void
210 rest_of_type_compilation (tree type, int toplev)
212 /* Avoid confusing the debug information machinery when there are
213 errors. */
214 if (seen_error ())
215 return;
217 timevar_push (TV_SYMOUT);
218 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
219 timevar_pop (TV_SYMOUT);
224 void
225 finish_optimization_passes (void)
227 int i;
228 struct dump_file_info *dfi;
229 char *name;
231 timevar_push (TV_DUMP);
232 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
234 dump_file = dump_begin (pass_profile.pass.static_pass_number, NULL);
235 end_branch_prob ();
236 if (dump_file)
237 dump_end (pass_profile.pass.static_pass_number, dump_file);
240 if (optimize > 0)
242 dump_file = dump_begin (pass_combine.pass.static_pass_number, NULL);
243 if (dump_file)
245 dump_combine_total_stats (dump_file);
246 dump_end (pass_combine.pass.static_pass_number, dump_file);
250 /* Do whatever is necessary to finish printing the graphs. */
251 if (graph_dump_format != no_graph)
252 for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
253 if (dump_initialized_p (i)
254 && (dfi->flags & 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 gate_all_early_local_passes, /* gate */
293 execute_all_early_local_passes, /* execute */
294 NULL, /* sub */
295 NULL, /* next */
296 0, /* static_pass_number */
297 TV_EARLY_LOCAL, /* tv_id */
298 0, /* properties_required */
299 0, /* properties_provided */
300 0, /* properties_destroyed */
301 0, /* todo_flags_start */
302 TODO_remove_functions /* todo_flags_finish */
306 /* Gate: execute, or not, all of the non-trivial optimizations. */
308 static bool
309 gate_all_early_optimizations (void)
311 return (optimize >= 1
312 /* Don't bother doing anything if the program has errors. */
313 && !seen_error ());
316 static struct gimple_opt_pass pass_all_early_optimizations =
319 GIMPLE_PASS,
320 "early_optimizations", /* name */
321 gate_all_early_optimizations, /* gate */
322 NULL, /* execute */
323 NULL, /* sub */
324 NULL, /* next */
325 0, /* static_pass_number */
326 TV_NONE, /* tv_id */
327 0, /* properties_required */
328 0, /* properties_provided */
329 0, /* properties_destroyed */
330 0, /* todo_flags_start */
331 0 /* todo_flags_finish */
335 /* Gate: execute, or not, all of the non-trivial optimizations. */
337 static bool
338 gate_all_optimizations (void)
340 return (optimize >= 1
341 /* Don't bother doing anything if the program has errors.
342 We have to pass down the queue if we already went into SSA */
343 && (!seen_error () || gimple_in_ssa_p (cfun)));
346 static struct gimple_opt_pass pass_all_optimizations =
349 GIMPLE_PASS,
350 "*all_optimizations", /* name */
351 gate_all_optimizations, /* gate */
352 NULL, /* execute */
353 NULL, /* sub */
354 NULL, /* next */
355 0, /* static_pass_number */
356 TV_OPTIMIZE, /* tv_id */
357 0, /* properties_required */
358 0, /* properties_provided */
359 0, /* properties_destroyed */
360 0, /* todo_flags_start */
361 0 /* todo_flags_finish */
365 static bool
366 gate_rest_of_compilation (void)
368 /* Early return if there were errors. We can run afoul of our
369 consistency checks, and there's not really much point in fixing them. */
370 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
373 static struct rtl_opt_pass pass_rest_of_compilation =
376 RTL_PASS,
377 "*rest_of_compilation", /* name */
378 gate_rest_of_compilation, /* gate */
379 NULL, /* execute */
380 NULL, /* sub */
381 NULL, /* next */
382 0, /* static_pass_number */
383 TV_REST_OF_COMPILATION, /* tv_id */
384 PROP_rtl, /* properties_required */
385 0, /* properties_provided */
386 0, /* properties_destroyed */
387 0, /* todo_flags_start */
388 TODO_ggc_collect /* todo_flags_finish */
392 static bool
393 gate_postreload (void)
395 return reload_completed;
398 static struct rtl_opt_pass pass_postreload =
401 RTL_PASS,
402 "*all-postreload", /* name */
403 gate_postreload, /* gate */
404 NULL, /* execute */
405 NULL, /* sub */
406 NULL, /* next */
407 0, /* static_pass_number */
408 TV_POSTRELOAD, /* tv_id */
409 PROP_rtl, /* properties_required */
410 0, /* properties_provided */
411 0, /* properties_destroyed */
412 0, /* todo_flags_start */
413 TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
419 /* The root of the compilation pass tree, once constructed. */
420 struct opt_pass *all_passes, *all_small_ipa_passes, *all_lowering_passes,
421 *all_regular_ipa_passes, *all_late_ipa_passes, *all_lto_gen_passes;
423 /* This is used by plugins, and should also be used in register_pass. */
424 #define DEF_PASS_LIST(LIST) &LIST,
425 struct opt_pass **gcc_pass_lists[] = { GCC_PASS_LISTS NULL };
426 #undef DEF_PASS_LIST
428 /* A map from static pass id to optimization pass. */
429 struct opt_pass **passes_by_id;
430 int passes_by_id_size;
432 /* Set the static pass number of pass PASS to ID and record that
433 in the mapping from static pass number to pass. */
435 static void
436 set_pass_for_id (int id, struct opt_pass *pass)
438 pass->static_pass_number = id;
439 if (passes_by_id_size <= id)
441 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
442 memset (passes_by_id + passes_by_id_size, 0,
443 (id + 1 - passes_by_id_size) * sizeof (void *));
444 passes_by_id_size = id + 1;
446 passes_by_id[id] = pass;
449 /* Return the pass with the static pass number ID. */
451 struct opt_pass *
452 get_pass_for_id (int id)
454 if (id >= passes_by_id_size)
455 return NULL;
456 return passes_by_id[id];
459 /* Iterate over the pass tree allocating dump file numbers. We want
460 to do this depth first, and independent of whether the pass is
461 enabled or not. */
463 void
464 register_one_dump_file (struct opt_pass *pass)
466 char *dot_name, *flag_name, *glob_name;
467 const char *name, *full_name, *prefix;
468 char num[10];
469 int flags, id;
471 /* See below in next_pass_1. */
472 num[0] = '\0';
473 if (pass->static_pass_number != -1)
474 sprintf (num, "%d", ((int) pass->static_pass_number < 0
475 ? 1 : pass->static_pass_number));
477 /* The name is both used to identify the pass for the purposes of plugins,
478 and to specify dump file name and option.
479 The latter two might want something short which is not quite unique; for
480 that reason, we may have a disambiguating prefix, followed by a space
481 to mark the start of the following dump file name / option string. */
482 name = strchr (pass->name, ' ');
483 name = name ? name + 1 : pass->name;
484 dot_name = concat (".", name, num, NULL);
485 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
486 prefix = "ipa-", flags = TDF_IPA;
487 else if (pass->type == GIMPLE_PASS)
488 prefix = "tree-", flags = TDF_TREE;
489 else
490 prefix = "rtl-", flags = TDF_RTL;
492 flag_name = concat (prefix, name, num, NULL);
493 glob_name = concat (prefix, name, NULL);
494 id = dump_register (dot_name, flag_name, glob_name, flags);
495 set_pass_for_id (id, pass);
496 full_name = concat (prefix, pass->name, num, NULL);
497 register_pass_name (pass, full_name);
498 free (CONST_CAST (char *, full_name));
501 /* Recursive worker function for register_dump_files. */
503 static int
504 register_dump_files_1 (struct opt_pass *pass, int properties)
508 int new_properties = (properties | pass->properties_provided)
509 & ~pass->properties_destroyed;
511 if (pass->name && pass->name[0] != '*')
512 register_one_dump_file (pass);
514 if (pass->sub)
515 new_properties = register_dump_files_1 (pass->sub, new_properties);
517 /* If we have a gate, combine the properties that we could have with
518 and without the pass being examined. */
519 if (pass->gate)
520 properties &= new_properties;
521 else
522 properties = new_properties;
524 pass = pass->next;
526 while (pass);
528 return properties;
531 /* Register the dump files for the pipeline starting at PASS.
532 PROPERTIES reflects the properties that are guaranteed to be available at
533 the beginning of the pipeline. */
535 static void
536 register_dump_files (struct opt_pass *pass,int properties)
538 pass->properties_required |= properties;
539 register_dump_files_1 (pass, properties);
542 struct pass_registry
544 const char* unique_name;
545 struct opt_pass *pass;
548 /* Pass registry hash function. */
550 static hashval_t
551 passr_hash (const void *p)
553 const struct pass_registry *const s = (const struct pass_registry *const) p;
554 return htab_hash_string (s->unique_name);
557 /* Hash equal function */
559 static int
560 passr_eq (const void *p1, const void *p2)
562 const struct pass_registry *const s1 = (const struct pass_registry *const) p1;
563 const struct pass_registry *const s2 = (const struct pass_registry *const) p2;
565 return !strcmp (s1->unique_name, s2->unique_name);
568 static htab_t name_to_pass_map = NULL;
570 /* Register PASS with NAME. */
572 static void
573 register_pass_name (struct opt_pass *pass, const char *name)
575 struct pass_registry **slot;
576 struct pass_registry pr;
578 if (!name_to_pass_map)
579 name_to_pass_map = htab_create (256, passr_hash, passr_eq, NULL);
581 pr.unique_name = name;
582 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map, &pr, INSERT);
583 if (!*slot)
585 struct pass_registry *new_pr;
587 new_pr = XCNEW (struct pass_registry);
588 new_pr->unique_name = xstrdup (name);
589 new_pr->pass = pass;
590 *slot = new_pr;
592 else
593 return; /* Ignore plugin passes. */
596 /* Map from pass id to canonicalized pass name. */
598 typedef const char *char_ptr;
599 DEF_VEC_P(char_ptr);
600 DEF_VEC_ALLOC_P(char_ptr, heap);
601 static VEC(char_ptr, heap) *pass_tab = NULL;
603 /* Callback function for traversing NAME_TO_PASS_MAP. */
605 static int
606 pass_traverse (void **slot, void *data ATTRIBUTE_UNUSED)
608 struct pass_registry **p = (struct pass_registry **)slot;
609 struct opt_pass *pass = (*p)->pass;
611 gcc_assert (pass->static_pass_number > 0);
612 gcc_assert (pass_tab);
614 VEC_replace (char_ptr, pass_tab, pass->static_pass_number,
615 (*p)->unique_name);
617 return 1;
620 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
621 table for dumping purpose. */
623 static void
624 create_pass_tab (void)
626 if (!flag_dump_passes)
627 return;
629 VEC_safe_grow_cleared (char_ptr, heap,
630 pass_tab, passes_by_id_size + 1);
631 htab_traverse (name_to_pass_map, pass_traverse, NULL);
634 static bool override_gate_status (struct opt_pass *, tree, bool);
636 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
637 is turned on or not. */
639 static void
640 dump_one_pass (struct opt_pass *pass, int pass_indent)
642 int indent = 3 * pass_indent;
643 const char *pn;
644 bool is_on, is_really_on;
646 is_on = (pass->gate == NULL) ? true : pass->gate();
647 is_really_on = override_gate_status (pass, current_function_decl, is_on);
649 if (pass->static_pass_number <= 0)
650 pn = pass->name;
651 else
652 pn = VEC_index (char_ptr, pass_tab, pass->static_pass_number);
654 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
655 (15 - indent < 0 ? 0 : 15 - indent), " ",
656 is_on ? " ON" : " OFF",
657 ((!is_on) == (!is_really_on) ? ""
658 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
661 /* Dump pass list PASS with indentation INDENT. */
663 static void
664 dump_pass_list (struct opt_pass *pass, int indent)
668 dump_one_pass (pass, indent);
669 if (pass->sub)
670 dump_pass_list (pass->sub, indent + 1);
671 pass = pass->next;
673 while (pass);
676 /* Dump all optimization passes. */
678 void
679 dump_passes (void)
681 struct cgraph_node *n, *node = NULL;
682 tree save_fndecl = current_function_decl;
684 create_pass_tab();
686 FOR_EACH_DEFINED_FUNCTION (n)
687 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
689 node = n;
690 break;
693 if (!node)
694 return;
696 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
697 current_function_decl = node->symbol.decl;
699 dump_pass_list (all_lowering_passes, 1);
700 dump_pass_list (all_small_ipa_passes, 1);
701 dump_pass_list (all_regular_ipa_passes, 1);
702 dump_pass_list (all_lto_gen_passes, 1);
703 dump_pass_list (all_late_ipa_passes, 1);
704 dump_pass_list (all_passes, 1);
706 pop_cfun ();
707 current_function_decl = save_fndecl;
711 /* Returns the pass with NAME. */
713 static struct opt_pass *
714 get_pass_by_name (const char *name)
716 struct pass_registry **slot, pr;
718 pr.unique_name = name;
719 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map,
720 &pr, NO_INSERT);
722 if (!slot || !*slot)
723 return NULL;
725 return (*slot)->pass;
729 /* Range [start, last]. */
731 struct uid_range
733 unsigned int start;
734 unsigned int last;
735 const char *assem_name;
736 struct uid_range *next;
739 typedef struct uid_range *uid_range_p;
741 DEF_VEC_P(uid_range_p);
742 DEF_VEC_ALLOC_P(uid_range_p, heap);
744 static VEC(uid_range_p, heap) *enabled_pass_uid_range_tab = NULL;
745 static VEC(uid_range_p, heap) *disabled_pass_uid_range_tab = NULL;
748 /* Parse option string for -fdisable- and -fenable-
749 The syntax of the options:
751 -fenable-<pass_name>
752 -fdisable-<pass_name>
754 -fenable-<pass_name>=s1:e1,s2:e2,...
755 -fdisable-<pass_name>=s1:e1,s2:e2,...
758 static void
759 enable_disable_pass (const char *arg, bool is_enable)
761 struct opt_pass *pass;
762 char *range_str, *phase_name;
763 char *argstr = xstrdup (arg);
764 VEC(uid_range_p, heap) **tab = 0;
766 range_str = strchr (argstr,'=');
767 if (range_str)
769 *range_str = '\0';
770 range_str++;
773 phase_name = argstr;
774 if (!*phase_name)
776 if (is_enable)
777 error ("unrecognized option -fenable");
778 else
779 error ("unrecognized option -fdisable");
780 free (argstr);
781 return;
783 pass = get_pass_by_name (phase_name);
784 if (!pass || pass->static_pass_number == -1)
786 if (is_enable)
787 error ("unknown pass %s specified in -fenable", phase_name);
788 else
789 error ("unknown pass %s specified in -fdisable", phase_name);
790 free (argstr);
791 return;
794 if (is_enable)
795 tab = &enabled_pass_uid_range_tab;
796 else
797 tab = &disabled_pass_uid_range_tab;
799 if ((unsigned) pass->static_pass_number >= VEC_length (uid_range_p, *tab))
800 VEC_safe_grow_cleared (uid_range_p, heap,
801 *tab, pass->static_pass_number + 1);
803 if (!range_str)
805 uid_range_p slot;
806 uid_range_p new_range = XCNEW (struct uid_range);
808 new_range->start = 0;
809 new_range->last = (unsigned)-1;
811 slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
812 new_range->next = slot;
813 VEC_replace (uid_range_p, *tab, pass->static_pass_number,
814 new_range);
815 if (is_enable)
816 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
817 "of [%u, %u]", phase_name, new_range->start, new_range->last);
818 else
819 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
820 "of [%u, %u]", phase_name, new_range->start, new_range->last);
822 else
824 char *next_range = NULL;
825 char *one_range = range_str;
826 char *end_val = NULL;
830 uid_range_p slot;
831 uid_range_p new_range;
832 char *invalid = NULL;
833 long start;
834 char *func_name = NULL;
836 next_range = strchr (one_range, ',');
837 if (next_range)
839 *next_range = '\0';
840 next_range++;
843 end_val = strchr (one_range, ':');
844 if (end_val)
846 *end_val = '\0';
847 end_val++;
849 start = strtol (one_range, &invalid, 10);
850 if (*invalid || start < 0)
852 if (end_val || (one_range[0] >= '0'
853 && one_range[0] <= '9'))
855 error ("Invalid range %s in option %s",
856 one_range,
857 is_enable ? "-fenable" : "-fdisable");
858 free (argstr);
859 return;
861 func_name = one_range;
863 if (!end_val)
865 new_range = XCNEW (struct uid_range);
866 if (!func_name)
868 new_range->start = (unsigned) start;
869 new_range->last = (unsigned) start;
871 else
873 new_range->start = (unsigned) -1;
874 new_range->last = (unsigned) -1;
875 new_range->assem_name = xstrdup (func_name);
878 else
880 long last = strtol (end_val, &invalid, 10);
881 if (*invalid || last < start)
883 error ("Invalid range %s in option %s",
884 end_val,
885 is_enable ? "-fenable" : "-fdisable");
886 free (argstr);
887 return;
889 new_range = XCNEW (struct uid_range);
890 new_range->start = (unsigned) start;
891 new_range->last = (unsigned) last;
894 slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
895 new_range->next = slot;
896 VEC_replace (uid_range_p, *tab, pass->static_pass_number,
897 new_range);
898 if (is_enable)
900 if (new_range->assem_name)
901 inform (UNKNOWN_LOCATION,
902 "enable pass %s for function %s",
903 phase_name, new_range->assem_name);
904 else
905 inform (UNKNOWN_LOCATION,
906 "enable pass %s for functions in the range of [%u, %u]",
907 phase_name, new_range->start, new_range->last);
909 else
911 if (new_range->assem_name)
912 inform (UNKNOWN_LOCATION,
913 "disable pass %s for function %s",
914 phase_name, new_range->assem_name);
915 else
916 inform (UNKNOWN_LOCATION,
917 "disable pass %s for functions in the range of [%u, %u]",
918 phase_name, new_range->start, new_range->last);
921 one_range = next_range;
922 } while (next_range);
925 free (argstr);
928 /* Enable pass specified by ARG. */
930 void
931 enable_pass (const char *arg)
933 enable_disable_pass (arg, true);
936 /* Disable pass specified by ARG. */
938 void
939 disable_pass (const char *arg)
941 enable_disable_pass (arg, false);
944 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
946 static bool
947 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
948 tree func,
949 VEC(uid_range_p, heap) *tab)
951 uid_range_p slot, range;
952 int cgraph_uid;
953 const char *aname = NULL;
955 if (!tab
956 || (unsigned) pass->static_pass_number >= VEC_length (uid_range_p, tab)
957 || pass->static_pass_number == -1)
958 return false;
960 slot = VEC_index (uid_range_p, tab, pass->static_pass_number);
961 if (!slot)
962 return false;
964 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
965 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
966 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
968 range = slot;
969 while (range)
971 if ((unsigned) cgraph_uid >= range->start
972 && (unsigned) cgraph_uid <= range->last)
973 return true;
974 if (range->assem_name && aname
975 && !strcmp (range->assem_name, aname))
976 return true;
977 range = range->next;
980 return false;
983 /* Look at the static_pass_number and duplicate the pass
984 if it is already added to a list. */
986 static struct opt_pass *
987 make_pass_instance (struct opt_pass *pass, bool track_duplicates)
989 /* A nonzero static_pass_number indicates that the
990 pass is already in the list. */
991 if (pass->static_pass_number)
993 struct opt_pass *new_pass;
995 if (pass->type == GIMPLE_PASS
996 || pass->type == RTL_PASS
997 || pass->type == SIMPLE_IPA_PASS)
999 new_pass = XNEW (struct opt_pass);
1000 memcpy (new_pass, pass, sizeof (struct opt_pass));
1002 else if (pass->type == IPA_PASS)
1004 new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
1005 memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
1007 else
1008 gcc_unreachable ();
1010 new_pass->next = NULL;
1012 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1014 /* Indicate to register_dump_files that this pass has duplicates,
1015 and so it should rename the dump file. The first instance will
1016 be -1, and be number of duplicates = -static_pass_number - 1.
1017 Subsequent instances will be > 0 and just the duplicate number. */
1018 if ((pass->name && pass->name[0] != '*') || track_duplicates)
1020 pass->static_pass_number -= 1;
1021 new_pass->static_pass_number = -pass->static_pass_number;
1023 return new_pass;
1025 else
1027 pass->todo_flags_start |= TODO_mark_first_instance;
1028 pass->static_pass_number = -1;
1030 invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
1032 return pass;
1035 /* Add a pass to the pass list. Duplicate the pass if it's already
1036 in the list. */
1038 static struct opt_pass **
1039 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
1041 /* Every pass should have a name so that plugins can refer to them. */
1042 gcc_assert (pass->name != NULL);
1044 *list = make_pass_instance (pass, false);
1046 return &(*list)->next;
1049 /* List node for an inserted pass instance. We need to keep track of all
1050 the newly-added pass instances (with 'added_pass_nodes' defined below)
1051 so that we can register their dump files after pass-positioning is finished.
1052 Registering dumping files needs to be post-processed or the
1053 static_pass_number of the opt_pass object would be modified and mess up
1054 the dump file names of future pass instances to be added. */
1056 struct pass_list_node
1058 struct opt_pass *pass;
1059 struct pass_list_node *next;
1062 static struct pass_list_node *added_pass_nodes = NULL;
1063 static struct pass_list_node *prev_added_pass_node;
1065 /* Insert the pass at the proper position. Return true if the pass
1066 is successfully added.
1068 NEW_PASS_INFO - new pass to be inserted
1069 PASS_LIST - root of the pass list to insert the new pass to */
1071 static bool
1072 position_pass (struct register_pass_info *new_pass_info,
1073 struct opt_pass **pass_list)
1075 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1076 bool success = false;
1078 for ( ; pass; prev_pass = pass, pass = pass->next)
1080 /* Check if the current pass is of the same type as the new pass and
1081 matches the name and the instance number of the reference pass. */
1082 if (pass->type == new_pass_info->pass->type
1083 && pass->name
1084 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1085 && ((new_pass_info->ref_pass_instance_number == 0)
1086 || (new_pass_info->ref_pass_instance_number ==
1087 pass->static_pass_number)
1088 || (new_pass_info->ref_pass_instance_number == 1
1089 && pass->todo_flags_start & TODO_mark_first_instance)))
1091 struct opt_pass *new_pass;
1092 struct pass_list_node *new_pass_node;
1094 new_pass = make_pass_instance (new_pass_info->pass, true);
1096 /* Insert the new pass instance based on the positioning op. */
1097 switch (new_pass_info->pos_op)
1099 case PASS_POS_INSERT_AFTER:
1100 new_pass->next = pass->next;
1101 pass->next = new_pass;
1103 /* Skip newly inserted pass to avoid repeated
1104 insertions in the case where the new pass and the
1105 existing one have the same name. */
1106 pass = new_pass;
1107 break;
1108 case PASS_POS_INSERT_BEFORE:
1109 new_pass->next = pass;
1110 if (prev_pass)
1111 prev_pass->next = new_pass;
1112 else
1113 *pass_list = new_pass;
1114 break;
1115 case PASS_POS_REPLACE:
1116 new_pass->next = pass->next;
1117 if (prev_pass)
1118 prev_pass->next = new_pass;
1119 else
1120 *pass_list = new_pass;
1121 new_pass->sub = pass->sub;
1122 new_pass->tv_id = pass->tv_id;
1123 pass = new_pass;
1124 break;
1125 default:
1126 error ("invalid pass positioning operation");
1127 return false;
1130 /* Save the newly added pass (instance) in the added_pass_nodes
1131 list so that we can register its dump file later. Note that
1132 we cannot register the dump file now because doing so will modify
1133 the static_pass_number of the opt_pass object and therefore
1134 mess up the dump file name of future instances. */
1135 new_pass_node = XCNEW (struct pass_list_node);
1136 new_pass_node->pass = new_pass;
1137 if (!added_pass_nodes)
1138 added_pass_nodes = new_pass_node;
1139 else
1140 prev_added_pass_node->next = new_pass_node;
1141 prev_added_pass_node = new_pass_node;
1143 success = true;
1146 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1147 success = true;
1150 return success;
1153 /* Hooks a new pass into the pass lists.
1155 PASS_INFO - pass information that specifies the opt_pass object,
1156 reference pass, instance number, and how to position
1157 the pass */
1159 void
1160 register_pass (struct register_pass_info *pass_info)
1162 bool all_instances, success;
1164 /* The checks below could fail in buggy plugins. Existing GCC
1165 passes should never fail these checks, so we mention plugin in
1166 the messages. */
1167 if (!pass_info->pass)
1168 fatal_error ("plugin cannot register a missing pass");
1170 if (!pass_info->pass->name)
1171 fatal_error ("plugin cannot register an unnamed pass");
1173 if (!pass_info->reference_pass_name)
1174 fatal_error
1175 ("plugin cannot register pass %qs without reference pass name",
1176 pass_info->pass->name);
1178 /* Try to insert the new pass to the pass lists. We need to check
1179 all five lists as the reference pass could be in one (or all) of
1180 them. */
1181 all_instances = pass_info->ref_pass_instance_number == 0;
1182 success = position_pass (pass_info, &all_lowering_passes);
1183 if (!success || all_instances)
1184 success |= position_pass (pass_info, &all_small_ipa_passes);
1185 if (!success || all_instances)
1186 success |= position_pass (pass_info, &all_regular_ipa_passes);
1187 if (!success || all_instances)
1188 success |= position_pass (pass_info, &all_lto_gen_passes);
1189 if (!success || all_instances)
1190 success |= position_pass (pass_info, &all_late_ipa_passes);
1191 if (!success || all_instances)
1192 success |= position_pass (pass_info, &all_passes);
1193 if (!success)
1194 fatal_error
1195 ("pass %qs not found but is referenced by new pass %qs",
1196 pass_info->reference_pass_name, pass_info->pass->name);
1198 /* OK, we have successfully inserted the new pass. We need to register
1199 the dump files for the newly added pass and its duplicates (if any).
1200 Because the registration of plugin/backend passes happens after the
1201 command-line options are parsed, the options that specify single
1202 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1203 passes. Therefore we currently can only enable dumping of
1204 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1205 are specified. While doing so, we also delete the pass_list_node
1206 objects created during pass positioning. */
1207 while (added_pass_nodes)
1209 struct pass_list_node *next_node = added_pass_nodes->next;
1210 enum tree_dump_index tdi;
1211 register_one_dump_file (added_pass_nodes->pass);
1212 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1213 || added_pass_nodes->pass->type == IPA_PASS)
1214 tdi = TDI_ipa_all;
1215 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1216 tdi = TDI_tree_all;
1217 else
1218 tdi = TDI_rtl_all;
1219 /* Check if dump-all flag is specified. */
1220 if (get_dump_file_info (tdi)->state)
1221 get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1222 ->state = get_dump_file_info (tdi)->state;
1223 XDELETE (added_pass_nodes);
1224 added_pass_nodes = next_node;
1228 /* Construct the pass tree. The sequencing of passes is driven by
1229 the cgraph routines:
1231 finalize_compilation_unit ()
1232 for each node N in the cgraph
1233 cgraph_analyze_function (N)
1234 cgraph_lower_function (N) -> all_lowering_passes
1236 If we are optimizing, compile is then invoked:
1238 compile ()
1239 ipa_passes () -> all_small_ipa_passes
1240 -> Analysis of all_regular_ipa_passes
1241 * possible LTO streaming at copmilation time *
1242 -> Execution of all_regular_ipa_passes
1243 * possible LTO streaming at link time *
1244 -> all_late_ipa_passes
1245 expand_all_functions ()
1246 for each node N in the cgraph
1247 expand_function (N) -> Transformation of all_regular_ipa_passes
1248 -> all_passes
1251 void
1252 init_optimization_passes (void)
1254 struct opt_pass **p;
1256 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &((PASS).pass)))
1258 /* All passes needed to lower the function into shape optimizers can
1259 operate on. These passes are always run first on the function, but
1260 backend might produce already lowered functions that are not processed
1261 by these passes. */
1262 p = &all_lowering_passes;
1263 NEXT_PASS (pass_warn_unused_result);
1264 NEXT_PASS (pass_diagnose_omp_blocks);
1265 NEXT_PASS (pass_diagnose_tm_blocks);
1266 NEXT_PASS (pass_mudflap_1);
1267 NEXT_PASS (pass_lower_omp);
1268 NEXT_PASS (pass_lower_cf);
1269 NEXT_PASS (pass_lower_tm);
1270 NEXT_PASS (pass_refactor_eh);
1271 NEXT_PASS (pass_lower_eh);
1272 NEXT_PASS (pass_build_cfg);
1273 NEXT_PASS (pass_warn_function_return);
1274 NEXT_PASS (pass_build_cgraph_edges);
1275 *p = NULL;
1277 /* Interprocedural optimization passes. */
1278 p = &all_small_ipa_passes;
1279 NEXT_PASS (pass_ipa_free_lang_data);
1280 NEXT_PASS (pass_ipa_function_and_variable_visibility);
1281 NEXT_PASS (pass_early_local_passes);
1283 struct opt_pass **p = &pass_early_local_passes.pass.sub;
1284 NEXT_PASS (pass_fixup_cfg);
1285 NEXT_PASS (pass_init_datastructures);
1286 NEXT_PASS (pass_expand_omp);
1288 NEXT_PASS (pass_referenced_vars);
1289 NEXT_PASS (pass_build_ssa);
1290 NEXT_PASS (pass_lower_vector);
1291 NEXT_PASS (pass_early_warn_uninitialized);
1292 NEXT_PASS (pass_rebuild_cgraph_edges);
1293 NEXT_PASS (pass_inline_parameters);
1294 NEXT_PASS (pass_early_inline);
1295 NEXT_PASS (pass_all_early_optimizations);
1297 struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
1298 NEXT_PASS (pass_remove_cgraph_callee_edges);
1299 NEXT_PASS (pass_rename_ssa_copies);
1300 NEXT_PASS (pass_ccp);
1301 NEXT_PASS (pass_forwprop);
1302 /* pass_build_ealias is a dummy pass that ensures that we
1303 execute TODO_rebuild_alias at this point. Re-building
1304 alias information also rewrites no longer addressed
1305 locals into SSA form if possible. */
1306 NEXT_PASS (pass_build_ealias);
1307 NEXT_PASS (pass_sra_early);
1308 NEXT_PASS (pass_fre);
1309 NEXT_PASS (pass_copy_prop);
1310 NEXT_PASS (pass_merge_phi);
1311 NEXT_PASS (pass_cd_dce);
1312 NEXT_PASS (pass_early_ipa_sra);
1313 NEXT_PASS (pass_tail_recursion);
1314 NEXT_PASS (pass_convert_switch);
1315 NEXT_PASS (pass_cleanup_eh);
1316 NEXT_PASS (pass_profile);
1317 NEXT_PASS (pass_local_pure_const);
1318 /* Split functions creates parts that are not run through
1319 early optimizations again. It is thus good idea to do this
1320 late. */
1321 NEXT_PASS (pass_split_functions);
1323 NEXT_PASS (pass_release_ssa_names);
1324 NEXT_PASS (pass_rebuild_cgraph_edges);
1325 NEXT_PASS (pass_inline_parameters);
1327 NEXT_PASS (pass_ipa_tree_profile);
1329 struct opt_pass **p = &pass_ipa_tree_profile.pass.sub;
1330 NEXT_PASS (pass_feedback_split_functions);
1332 NEXT_PASS (pass_ipa_increase_alignment);
1333 NEXT_PASS (pass_ipa_matrix_reorg);
1334 NEXT_PASS (pass_ipa_tm);
1335 NEXT_PASS (pass_ipa_lower_emutls);
1336 *p = NULL;
1338 p = &all_regular_ipa_passes;
1339 NEXT_PASS (pass_ipa_whole_program_visibility);
1340 NEXT_PASS (pass_ipa_profile);
1341 NEXT_PASS (pass_ipa_cp);
1342 NEXT_PASS (pass_ipa_cdtor_merge);
1343 NEXT_PASS (pass_ipa_inline);
1344 NEXT_PASS (pass_ipa_pure_const);
1345 NEXT_PASS (pass_ipa_reference);
1346 *p = NULL;
1348 p = &all_lto_gen_passes;
1349 NEXT_PASS (pass_ipa_lto_gimple_out);
1350 NEXT_PASS (pass_ipa_lto_finish_out); /* This must be the last LTO pass. */
1351 *p = NULL;
1353 /* Simple IPA passes executed after the regular passes. In WHOPR mode the
1354 passes are executed after partitioning and thus see just parts of the
1355 compiled unit. */
1356 p = &all_late_ipa_passes;
1357 NEXT_PASS (pass_ipa_pta);
1358 *p = NULL;
1360 /* These passes are run after IPA passes on every function that is being
1361 output to the assembler file. */
1362 p = &all_passes;
1363 NEXT_PASS (pass_fixup_cfg);
1364 NEXT_PASS (pass_lower_eh_dispatch);
1365 NEXT_PASS (pass_all_optimizations);
1367 struct opt_pass **p = &pass_all_optimizations.pass.sub;
1368 NEXT_PASS (pass_remove_cgraph_callee_edges);
1369 /* Initial scalar cleanups before alias computation.
1370 They ensure memory accesses are not indirect wherever possible. */
1371 NEXT_PASS (pass_strip_predict_hints);
1372 NEXT_PASS (pass_rename_ssa_copies);
1373 NEXT_PASS (pass_complete_unrolli);
1374 NEXT_PASS (pass_ccp);
1375 NEXT_PASS (pass_forwprop);
1376 /* pass_build_alias is a dummy pass that ensures that we
1377 execute TODO_rebuild_alias at this point. Re-building
1378 alias information also rewrites no longer addressed
1379 locals into SSA form if possible. */
1380 NEXT_PASS (pass_build_alias);
1381 NEXT_PASS (pass_return_slot);
1382 NEXT_PASS (pass_phiprop);
1383 NEXT_PASS (pass_fre);
1384 NEXT_PASS (pass_copy_prop);
1385 NEXT_PASS (pass_merge_phi);
1386 NEXT_PASS (pass_vrp);
1387 NEXT_PASS (pass_dce);
1388 NEXT_PASS (pass_call_cdce);
1389 NEXT_PASS (pass_cselim);
1390 NEXT_PASS (pass_tree_ifcombine);
1391 NEXT_PASS (pass_phiopt);
1392 NEXT_PASS (pass_tail_recursion);
1393 NEXT_PASS (pass_ch);
1394 NEXT_PASS (pass_stdarg);
1395 NEXT_PASS (pass_lower_complex);
1396 NEXT_PASS (pass_sra);
1397 NEXT_PASS (pass_rename_ssa_copies);
1398 /* The dom pass will also resolve all __builtin_constant_p calls
1399 that are still there to 0. This has to be done after some
1400 propagations have already run, but before some more dead code
1401 is removed, and this place fits nicely. Remember this when
1402 trying to move or duplicate pass_dominator somewhere earlier. */
1403 NEXT_PASS (pass_dominator);
1404 /* The only const/copy propagation opportunities left after
1405 DOM should be due to degenerate PHI nodes. So rather than
1406 run the full propagators, run a specialized pass which
1407 only examines PHIs to discover const/copy propagation
1408 opportunities. */
1409 NEXT_PASS (pass_phi_only_cprop);
1410 NEXT_PASS (pass_dse);
1411 NEXT_PASS (pass_reassoc);
1412 NEXT_PASS (pass_dce);
1413 NEXT_PASS (pass_forwprop);
1414 NEXT_PASS (pass_phiopt);
1415 NEXT_PASS (pass_object_sizes);
1416 NEXT_PASS (pass_strlen);
1417 NEXT_PASS (pass_ccp);
1418 NEXT_PASS (pass_copy_prop);
1419 NEXT_PASS (pass_cse_sincos);
1420 NEXT_PASS (pass_optimize_bswap);
1421 NEXT_PASS (pass_split_crit_edges);
1422 NEXT_PASS (pass_pre);
1423 NEXT_PASS (pass_sink_code);
1424 NEXT_PASS (pass_tree_loop);
1426 struct opt_pass **p = &pass_tree_loop.pass.sub;
1427 NEXT_PASS (pass_tree_loop_init);
1428 NEXT_PASS (pass_lim);
1429 NEXT_PASS (pass_copy_prop);
1430 NEXT_PASS (pass_dce_loop);
1431 NEXT_PASS (pass_tree_unswitch);
1432 NEXT_PASS (pass_scev_cprop);
1433 NEXT_PASS (pass_record_bounds);
1434 NEXT_PASS (pass_check_data_deps);
1435 NEXT_PASS (pass_loop_distribution);
1436 NEXT_PASS (pass_copy_prop);
1437 NEXT_PASS (pass_graphite);
1439 struct opt_pass **p = &pass_graphite.pass.sub;
1440 NEXT_PASS (pass_graphite_transforms);
1441 NEXT_PASS (pass_lim);
1442 NEXT_PASS (pass_copy_prop);
1443 NEXT_PASS (pass_dce_loop);
1445 NEXT_PASS (pass_iv_canon);
1446 NEXT_PASS (pass_if_conversion);
1447 NEXT_PASS (pass_vectorize);
1449 struct opt_pass **p = &pass_vectorize.pass.sub;
1450 NEXT_PASS (pass_dce_loop);
1452 NEXT_PASS (pass_predcom);
1453 NEXT_PASS (pass_complete_unroll);
1454 NEXT_PASS (pass_slp_vectorize);
1455 NEXT_PASS (pass_parallelize_loops);
1456 NEXT_PASS (pass_loop_prefetch);
1457 NEXT_PASS (pass_iv_optimize);
1458 NEXT_PASS (pass_lim);
1459 NEXT_PASS (pass_tree_loop_done);
1461 NEXT_PASS (pass_lower_vector_ssa);
1462 NEXT_PASS (pass_cse_reciprocals);
1463 NEXT_PASS (pass_reassoc);
1464 NEXT_PASS (pass_vrp);
1465 NEXT_PASS (pass_strength_reduction);
1466 NEXT_PASS (pass_dominator);
1467 /* The only const/copy propagation opportunities left after
1468 DOM should be due to degenerate PHI nodes. So rather than
1469 run the full propagators, run a specialized pass which
1470 only examines PHIs to discover const/copy propagation
1471 opportunities. */
1472 NEXT_PASS (pass_phi_only_cprop);
1473 NEXT_PASS (pass_cd_dce);
1474 NEXT_PASS (pass_tracer);
1476 /* FIXME: If DCE is not run before checking for uninitialized uses,
1477 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
1478 However, this also causes us to misdiagnose cases that should be
1479 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
1481 To fix the false positives in uninit-5.c, we would have to
1482 account for the predicates protecting the set and the use of each
1483 variable. Using a representation like Gated Single Assignment
1484 may help. */
1485 NEXT_PASS (pass_late_warn_uninitialized);
1486 NEXT_PASS (pass_dse);
1487 NEXT_PASS (pass_forwprop);
1488 NEXT_PASS (pass_phiopt);
1489 NEXT_PASS (pass_fold_builtins);
1490 NEXT_PASS (pass_optimize_widening_mul);
1491 NEXT_PASS (pass_tail_calls);
1492 NEXT_PASS (pass_rename_ssa_copies);
1493 NEXT_PASS (pass_uncprop);
1494 NEXT_PASS (pass_local_pure_const);
1496 NEXT_PASS (pass_tm_init);
1498 struct opt_pass **p = &pass_tm_init.pass.sub;
1499 NEXT_PASS (pass_tm_mark);
1500 NEXT_PASS (pass_tm_memopt);
1501 NEXT_PASS (pass_tm_edges);
1503 NEXT_PASS (pass_lower_complex_O0);
1504 NEXT_PASS (pass_cleanup_eh);
1505 NEXT_PASS (pass_lower_resx);
1506 NEXT_PASS (pass_nrv);
1507 NEXT_PASS (pass_mudflap_2);
1508 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
1509 NEXT_PASS (pass_warn_function_noreturn);
1511 NEXT_PASS (pass_expand);
1513 NEXT_PASS (pass_rest_of_compilation);
1515 struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
1516 NEXT_PASS (pass_instantiate_virtual_regs);
1517 NEXT_PASS (pass_into_cfg_layout_mode);
1518 NEXT_PASS (pass_jump);
1519 NEXT_PASS (pass_lower_subreg);
1520 NEXT_PASS (pass_df_initialize_opt);
1521 NEXT_PASS (pass_cse);
1522 NEXT_PASS (pass_rtl_fwprop);
1523 NEXT_PASS (pass_rtl_cprop);
1524 NEXT_PASS (pass_rtl_pre);
1525 NEXT_PASS (pass_rtl_hoist);
1526 NEXT_PASS (pass_rtl_cprop);
1527 NEXT_PASS (pass_rtl_store_motion);
1528 NEXT_PASS (pass_cse_after_global_opts);
1529 NEXT_PASS (pass_rtl_ifcvt);
1530 NEXT_PASS (pass_reginfo_init);
1531 /* Perform loop optimizations. It might be better to do them a bit
1532 sooner, but we want the profile feedback to work more
1533 efficiently. */
1534 NEXT_PASS (pass_loop2);
1536 struct opt_pass **p = &pass_loop2.pass.sub;
1537 NEXT_PASS (pass_rtl_loop_init);
1538 NEXT_PASS (pass_rtl_move_loop_invariants);
1539 NEXT_PASS (pass_rtl_unswitch);
1540 NEXT_PASS (pass_rtl_unroll_and_peel_loops);
1541 NEXT_PASS (pass_rtl_doloop);
1542 NEXT_PASS (pass_rtl_loop_done);
1543 *p = NULL;
1545 NEXT_PASS (pass_web);
1546 NEXT_PASS (pass_rtl_cprop);
1547 NEXT_PASS (pass_cse2);
1548 NEXT_PASS (pass_rtl_dse1);
1549 NEXT_PASS (pass_rtl_fwprop_addr);
1550 NEXT_PASS (pass_inc_dec);
1551 NEXT_PASS (pass_initialize_regs);
1552 NEXT_PASS (pass_ud_rtl_dce);
1553 NEXT_PASS (pass_combine);
1554 NEXT_PASS (pass_if_after_combine);
1555 NEXT_PASS (pass_partition_blocks);
1556 NEXT_PASS (pass_regmove);
1557 NEXT_PASS (pass_outof_cfg_layout_mode);
1558 NEXT_PASS (pass_split_all_insns);
1559 NEXT_PASS (pass_lower_subreg2);
1560 NEXT_PASS (pass_df_initialize_no_opt);
1561 NEXT_PASS (pass_stack_ptr_mod);
1562 NEXT_PASS (pass_mode_switching);
1563 NEXT_PASS (pass_match_asm_constraints);
1564 NEXT_PASS (pass_sms);
1565 NEXT_PASS (pass_sched);
1566 NEXT_PASS (pass_ira);
1567 NEXT_PASS (pass_reload);
1568 NEXT_PASS (pass_postreload);
1570 struct opt_pass **p = &pass_postreload.pass.sub;
1571 NEXT_PASS (pass_postreload_cse);
1572 NEXT_PASS (pass_gcse2);
1573 NEXT_PASS (pass_split_after_reload);
1574 NEXT_PASS (pass_ree);
1575 NEXT_PASS (pass_compare_elim_after_reload);
1576 NEXT_PASS (pass_branch_target_load_optimize1);
1577 NEXT_PASS (pass_thread_prologue_and_epilogue);
1578 NEXT_PASS (pass_rtl_dse2);
1579 NEXT_PASS (pass_stack_adjustments);
1580 NEXT_PASS (pass_jump2);
1581 NEXT_PASS (pass_peephole2);
1582 NEXT_PASS (pass_if_after_reload);
1583 NEXT_PASS (pass_regrename);
1584 NEXT_PASS (pass_cprop_hardreg);
1585 NEXT_PASS (pass_fast_rtl_dce);
1586 NEXT_PASS (pass_reorder_blocks);
1587 NEXT_PASS (pass_branch_target_load_optimize2);
1588 NEXT_PASS (pass_leaf_regs);
1589 NEXT_PASS (pass_split_before_sched2);
1590 NEXT_PASS (pass_sched2);
1591 NEXT_PASS (pass_stack_regs);
1593 struct opt_pass **p = &pass_stack_regs.pass.sub;
1594 NEXT_PASS (pass_split_before_regstack);
1595 NEXT_PASS (pass_stack_regs_run);
1597 NEXT_PASS (pass_compute_alignments);
1598 NEXT_PASS (pass_duplicate_computed_gotos);
1599 NEXT_PASS (pass_variable_tracking);
1600 NEXT_PASS (pass_free_cfg);
1601 NEXT_PASS (pass_machine_reorg);
1602 NEXT_PASS (pass_cleanup_barriers);
1603 NEXT_PASS (pass_delay_slots);
1604 NEXT_PASS (pass_split_for_shorten_branches);
1605 NEXT_PASS (pass_convert_to_eh_region_ranges);
1606 NEXT_PASS (pass_shorten_branches);
1607 NEXT_PASS (pass_set_nothrow_function_flags);
1608 NEXT_PASS (pass_dwarf2_frame);
1609 NEXT_PASS (pass_final);
1611 NEXT_PASS (pass_df_finish);
1613 NEXT_PASS (pass_clean_state);
1614 *p = NULL;
1616 #undef NEXT_PASS
1618 /* Register the passes with the tree dump code. */
1619 register_dump_files (all_lowering_passes, PROP_gimple_any);
1620 register_dump_files (all_small_ipa_passes,
1621 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1622 | PROP_cfg);
1623 register_dump_files (all_regular_ipa_passes,
1624 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1625 | PROP_cfg);
1626 register_dump_files (all_lto_gen_passes,
1627 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1628 | PROP_cfg);
1629 register_dump_files (all_late_ipa_passes,
1630 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1631 | PROP_cfg);
1632 register_dump_files (all_passes,
1633 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1634 | PROP_cfg);
1637 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1638 function CALLBACK for every function in the call graph. Otherwise,
1639 call CALLBACK on the current function. */
1641 static void
1642 do_per_function (void (*callback) (void *data), void *data)
1644 if (current_function_decl)
1645 callback (data);
1646 else
1648 struct cgraph_node *node;
1649 FOR_EACH_DEFINED_FUNCTION (node)
1650 if (gimple_has_body_p (node->symbol.decl)
1651 && (!node->clone_of || node->symbol.decl != node->clone_of->symbol.decl))
1653 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1654 current_function_decl = node->symbol.decl;
1655 callback (data);
1656 if (!flag_wpa)
1658 free_dominance_info (CDI_DOMINATORS);
1659 free_dominance_info (CDI_POST_DOMINATORS);
1661 current_function_decl = NULL;
1662 pop_cfun ();
1663 ggc_collect ();
1668 /* Because inlining might remove no-longer reachable nodes, we need to
1669 keep the array visible to garbage collector to avoid reading collected
1670 out nodes. */
1671 static int nnodes;
1672 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1674 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1675 function CALLBACK for every function in the call graph. Otherwise,
1676 call CALLBACK on the current function.
1677 This function is global so that plugins can use it. */
1678 void
1679 do_per_function_toporder (void (*callback) (void *data), void *data)
1681 int i;
1683 if (current_function_decl)
1684 callback (data);
1685 else
1687 gcc_assert (!order);
1688 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1689 nnodes = ipa_reverse_postorder (order);
1690 for (i = nnodes - 1; i >= 0; i--)
1691 order[i]->process = 1;
1692 for (i = nnodes - 1; i >= 0; i--)
1694 struct cgraph_node *node = order[i];
1696 /* Allow possibly removed nodes to be garbage collected. */
1697 order[i] = NULL;
1698 node->process = 0;
1699 if (cgraph_function_with_gimple_body_p (node))
1701 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1702 current_function_decl = node->symbol.decl;
1703 callback (data);
1704 free_dominance_info (CDI_DOMINATORS);
1705 free_dominance_info (CDI_POST_DOMINATORS);
1706 current_function_decl = NULL;
1707 pop_cfun ();
1708 ggc_collect ();
1712 ggc_free (order);
1713 order = NULL;
1714 nnodes = 0;
1717 /* Helper function to perform function body dump. */
1719 static void
1720 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1722 if (dump_file && current_function_decl)
1724 if (cfun->curr_properties & PROP_trees)
1725 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1726 else
1728 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1730 if ((cfun->curr_properties & PROP_cfg)
1731 && graph_dump_format != no_graph
1732 && (dump_flags & TDF_GRAPH))
1733 print_rtl_graph_with_bb (dump_file_name, get_insns ());
1736 /* Flush the file. If verification fails, we won't be able to
1737 close the file before aborting. */
1738 fflush (dump_file);
1742 /* Perform all TODO actions that ought to be done on each function. */
1744 static void
1745 execute_function_todo (void *data)
1747 unsigned int flags = (size_t)data;
1748 flags &= ~cfun->last_verified;
1749 if (!flags)
1750 return;
1752 /* Always cleanup the CFG before trying to update SSA. */
1753 if (flags & TODO_cleanup_cfg)
1755 bool cleanup = cleanup_tree_cfg ();
1757 if (cleanup && (cfun->curr_properties & PROP_ssa))
1758 flags |= TODO_remove_unused_locals;
1760 /* When cleanup_tree_cfg merges consecutive blocks, it may
1761 perform some simplistic propagation when removing single
1762 valued PHI nodes. This propagation may, in turn, cause the
1763 SSA form to become out-of-date (see PR 22037). So, even
1764 if the parent pass had not scheduled an SSA update, we may
1765 still need to do one. */
1766 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1767 flags |= TODO_update_ssa;
1770 if (flags & TODO_update_ssa_any)
1772 unsigned update_flags = flags & TODO_update_ssa_any;
1773 update_ssa (update_flags);
1774 cfun->last_verified &= ~TODO_verify_ssa;
1777 if (flags & TODO_rebuild_alias)
1779 execute_update_addresses_taken ();
1780 compute_may_aliases ();
1782 else if (optimize && (flags & TODO_update_address_taken))
1783 execute_update_addresses_taken ();
1785 if (flags & TODO_remove_unused_locals)
1786 remove_unused_locals ();
1788 if (flags & TODO_rebuild_frequencies)
1789 rebuild_frequencies ();
1791 if (flags & TODO_rebuild_cgraph_edges)
1792 rebuild_cgraph_edges ();
1794 /* If we've seen errors do not bother running any verifiers. */
1795 if (seen_error ())
1796 return;
1798 #if defined ENABLE_CHECKING
1799 if (flags & TODO_verify_ssa
1800 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1802 verify_gimple_in_cfg (cfun);
1803 verify_ssa (true);
1805 else if (flags & TODO_verify_stmts)
1806 verify_gimple_in_cfg (cfun);
1807 if (flags & TODO_verify_flow)
1808 verify_flow_info ();
1809 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1810 verify_loop_closed_ssa (false);
1811 if (flags & TODO_verify_rtl_sharing)
1812 verify_rtl_sharing ();
1813 #endif
1815 cfun->last_verified = flags & TODO_verify_all;
1818 /* Perform all TODO actions. */
1819 static void
1820 execute_todo (unsigned int flags)
1822 #if defined ENABLE_CHECKING
1823 if (cfun
1824 && need_ssa_update_p (cfun))
1825 gcc_assert (flags & TODO_update_ssa_any);
1826 #endif
1828 timevar_push (TV_TODO);
1830 /* Inform the pass whether it is the first time it is run. */
1831 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1833 statistics_fini_pass ();
1835 do_per_function (execute_function_todo, (void *)(size_t) flags);
1837 /* Always remove functions just as before inlining: IPA passes might be
1838 interested to see bodies of extern inline functions that are not inlined
1839 to analyze side effects. The full removal is done just at the end
1840 of IPA pass queue. */
1841 if (flags & TODO_remove_functions)
1843 gcc_assert (!cfun);
1844 symtab_remove_unreachable_nodes (true, dump_file);
1847 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1849 gcc_assert (!cfun);
1850 dump_symtab (dump_file);
1851 /* Flush the file. If verification fails, we won't be able to
1852 close the file before aborting. */
1853 fflush (dump_file);
1856 if (flags & TODO_ggc_collect)
1857 ggc_collect ();
1859 /* Now that the dumping has been done, we can get rid of the optional
1860 df problems. */
1861 if (flags & TODO_df_finish)
1862 df_finish_pass ((flags & TODO_df_verify) != 0);
1864 timevar_pop (TV_TODO);
1867 /* Verify invariants that should hold between passes. This is a place
1868 to put simple sanity checks. */
1870 static void
1871 verify_interpass_invariants (void)
1873 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1876 /* Clear the last verified flag. */
1878 static void
1879 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1881 cfun->last_verified = 0;
1884 /* Helper function. Verify that the properties has been turn into the
1885 properties expected by the pass. */
1887 #ifdef ENABLE_CHECKING
1888 static void
1889 verify_curr_properties (void *data)
1891 unsigned int props = (size_t)data;
1892 gcc_assert ((cfun->curr_properties & props) == props);
1894 #endif
1896 /* Initialize pass dump file. */
1897 /* This is non-static so that the plugins can use it. */
1899 bool
1900 pass_init_dump_file (struct opt_pass *pass)
1902 /* If a dump file name is present, open it if enabled. */
1903 if (pass->static_pass_number != -1)
1905 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
1906 dump_file_name = get_dump_file_name (pass->static_pass_number);
1907 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
1908 if (dump_file && current_function_decl)
1909 dump_function_header (dump_file, current_function_decl, dump_flags);
1910 return initializing_dump;
1912 else
1913 return false;
1916 /* Flush PASS dump file. */
1917 /* This is non-static so that plugins can use it. */
1919 void
1920 pass_fini_dump_file (struct opt_pass *pass)
1922 /* Flush and close dump file. */
1923 if (dump_file_name)
1925 free (CONST_CAST (char *, dump_file_name));
1926 dump_file_name = NULL;
1929 if (dump_file)
1931 dump_end (pass->static_pass_number, dump_file);
1932 dump_file = NULL;
1936 /* After executing the pass, apply expected changes to the function
1937 properties. */
1939 static void
1940 update_properties_after_pass (void *data)
1942 struct opt_pass *pass = (struct opt_pass *) data;
1943 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1944 & ~pass->properties_destroyed;
1947 /* Execute summary generation for all of the passes in IPA_PASS. */
1949 void
1950 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1952 while (ipa_pass)
1954 struct opt_pass *pass = &ipa_pass->pass;
1956 /* Execute all of the IPA_PASSes in the list. */
1957 if (ipa_pass->pass.type == IPA_PASS
1958 && (!pass->gate || pass->gate ())
1959 && ipa_pass->generate_summary)
1961 pass_init_dump_file (pass);
1963 /* If a timevar is present, start it. */
1964 if (pass->tv_id)
1965 timevar_push (pass->tv_id);
1967 ipa_pass->generate_summary ();
1969 /* Stop timevar. */
1970 if (pass->tv_id)
1971 timevar_pop (pass->tv_id);
1973 pass_fini_dump_file (pass);
1975 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
1979 /* Execute IPA_PASS function transform on NODE. */
1981 static void
1982 execute_one_ipa_transform_pass (struct cgraph_node *node,
1983 struct ipa_opt_pass_d *ipa_pass)
1985 struct opt_pass *pass = &ipa_pass->pass;
1986 unsigned int todo_after = 0;
1988 current_pass = pass;
1989 if (!ipa_pass->function_transform)
1990 return;
1992 /* Note that the folders should only create gimple expressions.
1993 This is a hack until the new folder is ready. */
1994 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1996 pass_init_dump_file (pass);
1998 /* Run pre-pass verification. */
1999 execute_todo (ipa_pass->function_transform_todo_flags_start);
2001 /* If a timevar is present, start it. */
2002 if (pass->tv_id != TV_NONE)
2003 timevar_push (pass->tv_id);
2005 /* Do it! */
2006 todo_after = ipa_pass->function_transform (node);
2008 /* Stop timevar. */
2009 if (pass->tv_id != TV_NONE)
2010 timevar_pop (pass->tv_id);
2012 /* Run post-pass cleanup and verification. */
2013 execute_todo (todo_after);
2014 verify_interpass_invariants ();
2016 do_per_function (execute_function_dump, NULL);
2017 pass_fini_dump_file (pass);
2019 current_pass = NULL;
2022 /* For the current function, execute all ipa transforms. */
2024 void
2025 execute_all_ipa_transforms (void)
2027 struct cgraph_node *node;
2028 if (!cfun)
2029 return;
2030 node = cgraph_get_node (current_function_decl);
2032 if (node->ipa_transforms_to_apply)
2034 unsigned int i;
2036 for (i = 0; i < VEC_length (ipa_opt_pass, node->ipa_transforms_to_apply);
2037 i++)
2038 execute_one_ipa_transform_pass (node,
2039 VEC_index (ipa_opt_pass,
2040 node->ipa_transforms_to_apply,
2041 i));
2042 VEC_free (ipa_opt_pass, heap, node->ipa_transforms_to_apply);
2043 node->ipa_transforms_to_apply = NULL;
2047 /* Callback for do_per_function to apply all IPA transforms. */
2049 static void
2050 apply_ipa_transforms (void *data)
2052 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2053 if (!node->global.inlined_to && node->ipa_transforms_to_apply)
2055 *(bool *)data = true;
2056 execute_all_ipa_transforms();
2057 rebuild_cgraph_edges ();
2061 /* Check if PASS is explicitly disabled or enabled and return
2062 the gate status. FUNC is the function to be processed, and
2063 GATE_STATUS is the gate status determined by pass manager by
2064 default. */
2066 static bool
2067 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2069 bool explicitly_enabled = false;
2070 bool explicitly_disabled = false;
2072 explicitly_enabled
2073 = is_pass_explicitly_enabled_or_disabled (pass, func,
2074 enabled_pass_uid_range_tab);
2075 explicitly_disabled
2076 = is_pass_explicitly_enabled_or_disabled (pass, func,
2077 disabled_pass_uid_range_tab);
2079 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2081 return gate_status;
2085 /* Execute PASS. */
2087 bool
2088 execute_one_pass (struct opt_pass *pass)
2090 bool initializing_dump;
2091 unsigned int todo_after = 0;
2093 bool gate_status;
2095 /* IPA passes are executed on whole program, so cfun should be NULL.
2096 Other passes need function context set. */
2097 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2098 gcc_assert (!cfun && !current_function_decl);
2099 else
2100 gcc_assert (cfun && current_function_decl);
2102 current_pass = pass;
2104 /* Check whether gate check should be avoided.
2105 User controls the value of the gate through the parameter "gate_status". */
2106 gate_status = (pass->gate == NULL) ? true : pass->gate();
2107 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2109 /* Override gate with plugin. */
2110 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2112 if (!gate_status)
2114 current_pass = NULL;
2115 return false;
2118 /* Pass execution event trigger: useful to identify passes being
2119 executed. */
2120 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2122 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2123 Apply all trnasforms first. */
2124 if (pass->type == SIMPLE_IPA_PASS)
2126 bool applied = false;
2127 do_per_function (apply_ipa_transforms, (void *)&applied);
2128 if (applied)
2129 symtab_remove_unreachable_nodes (true, dump_file);
2130 /* Restore current_pass. */
2131 current_pass = pass;
2134 if (!quiet_flag && !cfun)
2135 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2137 /* Note that the folders should only create gimple expressions.
2138 This is a hack until the new folder is ready. */
2139 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2141 initializing_dump = pass_init_dump_file (pass);
2143 /* Run pre-pass verification. */
2144 execute_todo (pass->todo_flags_start);
2146 #ifdef ENABLE_CHECKING
2147 do_per_function (verify_curr_properties,
2148 (void *)(size_t)pass->properties_required);
2149 #endif
2151 /* If a timevar is present, start it. */
2152 if (pass->tv_id != TV_NONE)
2153 timevar_push (pass->tv_id);
2155 /* Do it! */
2156 if (pass->execute)
2158 todo_after = pass->execute ();
2159 do_per_function (clear_last_verified, NULL);
2162 /* Stop timevar. */
2163 if (pass->tv_id != TV_NONE)
2164 timevar_pop (pass->tv_id);
2166 do_per_function (update_properties_after_pass, pass);
2168 if (initializing_dump
2169 && dump_file
2170 && graph_dump_format != no_graph
2171 && cfun
2172 && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
2173 == (PROP_cfg | PROP_rtl))
2175 get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
2176 dump_flags |= TDF_GRAPH;
2177 clean_graph_dump_file (dump_file_name);
2180 /* Run post-pass cleanup and verification. */
2181 execute_todo (todo_after | pass->todo_flags_finish);
2182 verify_interpass_invariants ();
2183 do_per_function (execute_function_dump, NULL);
2184 if (pass->type == IPA_PASS)
2186 struct cgraph_node *node;
2187 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2188 VEC_safe_push (ipa_opt_pass, heap, node->ipa_transforms_to_apply,
2189 (struct ipa_opt_pass_d *)pass);
2192 if (!current_function_decl)
2193 cgraph_process_new_functions ();
2195 pass_fini_dump_file (pass);
2197 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2198 gcc_assert (!(cfun->curr_properties & PROP_trees)
2199 || pass->type != RTL_PASS);
2201 current_pass = NULL;
2203 return true;
2206 void
2207 execute_pass_list (struct opt_pass *pass)
2211 gcc_assert (pass->type == GIMPLE_PASS
2212 || pass->type == RTL_PASS);
2213 if (execute_one_pass (pass) && pass->sub)
2214 execute_pass_list (pass->sub);
2215 pass = pass->next;
2217 while (pass);
2220 /* Same as execute_pass_list but assume that subpasses of IPA passes
2221 are local passes. If SET is not NULL, write out summaries of only
2222 those node in SET. */
2224 static void
2225 ipa_write_summaries_2 (struct opt_pass *pass, cgraph_node_set set,
2226 varpool_node_set vset,
2227 struct lto_out_decl_state *state)
2229 while (pass)
2231 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2232 gcc_assert (!current_function_decl);
2233 gcc_assert (!cfun);
2234 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2235 if (pass->type == IPA_PASS
2236 && ipa_pass->write_summary
2237 && (!pass->gate || pass->gate ()))
2239 /* If a timevar is present, start it. */
2240 if (pass->tv_id)
2241 timevar_push (pass->tv_id);
2243 pass_init_dump_file (pass);
2245 ipa_pass->write_summary (set,vset);
2247 pass_fini_dump_file (pass);
2249 /* If a timevar is present, start it. */
2250 if (pass->tv_id)
2251 timevar_pop (pass->tv_id);
2254 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2255 ipa_write_summaries_2 (pass->sub, set, vset, state);
2257 pass = pass->next;
2261 /* Helper function of ipa_write_summaries. Creates and destroys the
2262 decl state and calls ipa_write_summaries_2 for all passes that have
2263 summaries. SET is the set of nodes to be written. */
2265 static void
2266 ipa_write_summaries_1 (cgraph_node_set set, varpool_node_set vset)
2268 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2269 compute_ltrans_boundary (state, set, vset);
2271 lto_push_out_decl_state (state);
2273 gcc_assert (!flag_wpa);
2274 ipa_write_summaries_2 (all_regular_ipa_passes, set, vset, state);
2275 ipa_write_summaries_2 (all_lto_gen_passes, set, vset, state);
2277 gcc_assert (lto_get_out_decl_state () == state);
2278 lto_pop_out_decl_state ();
2279 lto_delete_out_decl_state (state);
2282 /* Write out summaries for all the nodes in the callgraph. */
2284 void
2285 ipa_write_summaries (void)
2287 cgraph_node_set set;
2288 varpool_node_set vset;
2289 struct cgraph_node **order;
2290 struct varpool_node *vnode;
2291 int i, order_pos;
2293 if (!flag_generate_lto || seen_error ())
2294 return;
2296 set = cgraph_node_set_new ();
2298 /* Create the callgraph set in the same order used in
2299 cgraph_expand_all_functions. This mostly facilitates debugging,
2300 since it causes the gimple file to be processed in the same order
2301 as the source code. */
2302 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2303 order_pos = ipa_reverse_postorder (order);
2304 gcc_assert (order_pos == cgraph_n_nodes);
2306 for (i = order_pos - 1; i >= 0; i--)
2308 struct cgraph_node *node = order[i];
2310 if (cgraph_function_with_gimple_body_p (node))
2312 /* When streaming out references to statements as part of some IPA
2313 pass summary, the statements need to have uids assigned and the
2314 following does that for all the IPA passes here. Naturally, this
2315 ordering then matches the one IPA-passes get in their stmt_fixup
2316 hooks. */
2318 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2319 renumber_gimple_stmt_uids ();
2320 pop_cfun ();
2322 if (node->analyzed)
2323 cgraph_node_set_add (set, node);
2325 vset = varpool_node_set_new ();
2327 FOR_EACH_DEFINED_VARIABLE (vnode)
2328 if ((!vnode->alias || vnode->alias_of))
2329 varpool_node_set_add (vset, vnode);
2331 ipa_write_summaries_1 (set, vset);
2333 free (order);
2334 free_cgraph_node_set (set);
2335 free_varpool_node_set (vset);
2338 /* Same as execute_pass_list but assume that subpasses of IPA passes
2339 are local passes. If SET is not NULL, write out optimization summaries of
2340 only those node in SET. */
2342 static void
2343 ipa_write_optimization_summaries_1 (struct opt_pass *pass, cgraph_node_set set,
2344 varpool_node_set vset,
2345 struct lto_out_decl_state *state)
2347 while (pass)
2349 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2350 gcc_assert (!current_function_decl);
2351 gcc_assert (!cfun);
2352 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2353 if (pass->type == IPA_PASS
2354 && ipa_pass->write_optimization_summary
2355 && (!pass->gate || pass->gate ()))
2357 /* If a timevar is present, start it. */
2358 if (pass->tv_id)
2359 timevar_push (pass->tv_id);
2361 pass_init_dump_file (pass);
2363 ipa_pass->write_optimization_summary (set, vset);
2365 pass_fini_dump_file (pass);
2367 /* If a timevar is present, start it. */
2368 if (pass->tv_id)
2369 timevar_pop (pass->tv_id);
2372 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2373 ipa_write_optimization_summaries_1 (pass->sub, set, vset, state);
2375 pass = pass->next;
2379 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2380 NULL, write out all summaries of all nodes. */
2382 void
2383 ipa_write_optimization_summaries (cgraph_node_set set, varpool_node_set vset)
2385 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2386 cgraph_node_set_iterator csi;
2387 compute_ltrans_boundary (state, set, vset);
2389 lto_push_out_decl_state (state);
2390 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2392 struct cgraph_node *node = csi_node (csi);
2393 /* When streaming out references to statements as part of some IPA
2394 pass summary, the statements need to have uids assigned.
2396 For functions newly born at WPA stage we need to initialize
2397 the uids here. */
2398 if (node->analyzed
2399 && gimple_has_body_p (node->symbol.decl))
2401 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2402 renumber_gimple_stmt_uids ();
2403 pop_cfun ();
2407 gcc_assert (flag_wpa);
2408 ipa_write_optimization_summaries_1 (all_regular_ipa_passes, set, vset, state);
2409 ipa_write_optimization_summaries_1 (all_lto_gen_passes, set, vset, state);
2411 gcc_assert (lto_get_out_decl_state () == state);
2412 lto_pop_out_decl_state ();
2413 lto_delete_out_decl_state (state);
2416 /* Same as execute_pass_list but assume that subpasses of IPA passes
2417 are local passes. */
2419 static void
2420 ipa_read_summaries_1 (struct opt_pass *pass)
2422 while (pass)
2424 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2426 gcc_assert (!current_function_decl);
2427 gcc_assert (!cfun);
2428 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2430 if (pass->gate == NULL || pass->gate ())
2432 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2434 /* If a timevar is present, start it. */
2435 if (pass->tv_id)
2436 timevar_push (pass->tv_id);
2438 pass_init_dump_file (pass);
2440 ipa_pass->read_summary ();
2442 pass_fini_dump_file (pass);
2444 /* Stop timevar. */
2445 if (pass->tv_id)
2446 timevar_pop (pass->tv_id);
2449 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2450 ipa_read_summaries_1 (pass->sub);
2452 pass = pass->next;
2457 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2459 void
2460 ipa_read_summaries (void)
2462 ipa_read_summaries_1 (all_regular_ipa_passes);
2463 ipa_read_summaries_1 (all_lto_gen_passes);
2466 /* Same as execute_pass_list but assume that subpasses of IPA passes
2467 are local passes. */
2469 static void
2470 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2472 while (pass)
2474 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2476 gcc_assert (!current_function_decl);
2477 gcc_assert (!cfun);
2478 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2480 if (pass->gate == NULL || pass->gate ())
2482 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2484 /* If a timevar is present, start it. */
2485 if (pass->tv_id)
2486 timevar_push (pass->tv_id);
2488 pass_init_dump_file (pass);
2490 ipa_pass->read_optimization_summary ();
2492 pass_fini_dump_file (pass);
2494 /* Stop timevar. */
2495 if (pass->tv_id)
2496 timevar_pop (pass->tv_id);
2499 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2500 ipa_read_optimization_summaries_1 (pass->sub);
2502 pass = pass->next;
2506 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2508 void
2509 ipa_read_optimization_summaries (void)
2511 ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
2512 ipa_read_optimization_summaries_1 (all_lto_gen_passes);
2515 /* Same as execute_pass_list but assume that subpasses of IPA passes
2516 are local passes. */
2517 void
2518 execute_ipa_pass_list (struct opt_pass *pass)
2522 gcc_assert (!current_function_decl);
2523 gcc_assert (!cfun);
2524 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2525 if (execute_one_pass (pass) && pass->sub)
2527 if (pass->sub->type == GIMPLE_PASS)
2529 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2530 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2531 pass->sub);
2532 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2534 else if (pass->sub->type == SIMPLE_IPA_PASS
2535 || pass->sub->type == IPA_PASS)
2536 execute_ipa_pass_list (pass->sub);
2537 else
2538 gcc_unreachable ();
2540 gcc_assert (!current_function_decl);
2541 cgraph_process_new_functions ();
2542 pass = pass->next;
2544 while (pass);
2547 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2549 static void
2550 execute_ipa_stmt_fixups (struct opt_pass *pass,
2551 struct cgraph_node *node, gimple *stmts)
2553 while (pass)
2555 /* Execute all of the IPA_PASSes in the list. */
2556 if (pass->type == IPA_PASS
2557 && (!pass->gate || pass->gate ()))
2559 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2561 if (ipa_pass->stmt_fixup)
2563 pass_init_dump_file (pass);
2564 /* If a timevar is present, start it. */
2565 if (pass->tv_id)
2566 timevar_push (pass->tv_id);
2568 ipa_pass->stmt_fixup (node, stmts);
2570 /* Stop timevar. */
2571 if (pass->tv_id)
2572 timevar_pop (pass->tv_id);
2573 pass_fini_dump_file (pass);
2575 if (pass->sub)
2576 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2578 pass = pass->next;
2582 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2584 void
2585 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2587 execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
2591 extern void debug_properties (unsigned int);
2592 extern void dump_properties (FILE *, unsigned int);
2594 DEBUG_FUNCTION void
2595 dump_properties (FILE *dump, unsigned int props)
2597 fprintf (dump, "Properties:\n");
2598 if (props & PROP_gimple_any)
2599 fprintf (dump, "PROP_gimple_any\n");
2600 if (props & PROP_gimple_lcf)
2601 fprintf (dump, "PROP_gimple_lcf\n");
2602 if (props & PROP_gimple_leh)
2603 fprintf (dump, "PROP_gimple_leh\n");
2604 if (props & PROP_cfg)
2605 fprintf (dump, "PROP_cfg\n");
2606 if (props & PROP_referenced_vars)
2607 fprintf (dump, "PROP_referenced_vars\n");
2608 if (props & PROP_ssa)
2609 fprintf (dump, "PROP_ssa\n");
2610 if (props & PROP_no_crit_edges)
2611 fprintf (dump, "PROP_no_crit_edges\n");
2612 if (props & PROP_rtl)
2613 fprintf (dump, "PROP_rtl\n");
2614 if (props & PROP_gimple_lomp)
2615 fprintf (dump, "PROP_gimple_lomp\n");
2616 if (props & PROP_gimple_lcx)
2617 fprintf (dump, "PROP_gimple_lcx\n");
2618 if (props & PROP_cfglayout)
2619 fprintf (dump, "PROP_cfglayout\n");
2622 DEBUG_FUNCTION void
2623 debug_properties (unsigned int props)
2625 dump_properties (stderr, props);
2628 /* Called by local passes to see if function is called by already processed nodes.
2629 Because we process nodes in topological order, this means that function is
2630 in recursive cycle or we introduced new direct calls. */
2631 bool
2632 function_called_by_processed_nodes_p (void)
2634 struct cgraph_edge *e;
2635 for (e = cgraph_get_node (current_function_decl)->callers;
2637 e = e->next_caller)
2639 if (e->caller->symbol.decl == current_function_decl)
2640 continue;
2641 if (!cgraph_function_with_gimple_body_p (e->caller))
2642 continue;
2643 if (TREE_ASM_WRITTEN (e->caller->symbol.decl))
2644 continue;
2645 if (!e->caller->process && !e->caller->global.inlined_to)
2646 break;
2648 if (dump_file && e)
2650 fprintf (dump_file, "Already processed call to:\n");
2651 dump_cgraph_node (dump_file, e->caller);
2653 return e != NULL;
2656 #include "gt-passes.h"