2012-05-01 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / passes.c
blobd6b9b9bc84f23a01632d1911028537c877e8db5e
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 "timevar.h"
53 #include "diagnostic-core.h"
54 #include "params.h"
55 #include "reload.h"
56 #include "dwarf2asm.h"
57 #include "integrate.h"
58 #include "debug.h"
59 #include "target.h"
60 #include "langhooks.h"
61 #include "cfglayout.h"
62 #include "cfgloop.h"
63 #include "hosthooks.h"
64 #include "cgraph.h"
65 #include "opts.h"
66 #include "coverage.h"
67 #include "value-prof.h"
68 #include "tree-inline.h"
69 #include "tree-flow.h"
70 #include "tree-pass.h"
71 #include "tree-dump.h"
72 #include "df.h"
73 #include "predict.h"
74 #include "lto-streamer.h"
75 #include "plugin.h"
76 #include "ipa-utils.h"
77 #include "tree-pretty-print.h"
79 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
80 #include "dwarf2out.h"
81 #endif
83 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
84 #include "dbxout.h"
85 #endif
87 #ifdef SDB_DEBUGGING_INFO
88 #include "sdbout.h"
89 #endif
91 #ifdef XCOFF_DEBUGGING_INFO
92 #include "xcoffout.h" /* Needed for external data
93 declarations for e.g. AIX 4.x. */
94 #endif
96 /* This is used for debugging. It allows the current pass to printed
97 from anywhere in compilation.
98 The variable current_pass is also used for statistics and plugins. */
99 struct opt_pass *current_pass;
101 static void register_pass_name (struct opt_pass *, const char *);
103 /* Call from anywhere to find out what pass this is. Useful for
104 printing out debugging information deep inside an service
105 routine. */
106 void
107 print_current_pass (FILE *file)
109 if (current_pass)
110 fprintf (file, "current pass = %s (%d)\n",
111 current_pass->name, current_pass->static_pass_number);
112 else
113 fprintf (file, "no current pass.\n");
117 /* Call from the debugger to get the current pass name. */
118 DEBUG_FUNCTION void
119 debug_pass (void)
121 print_current_pass (stderr);
126 /* Global variables used to communicate with passes. */
127 int dump_flags;
128 bool in_gimple_form;
129 bool first_pass_instance;
132 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
133 and TYPE_DECL nodes.
135 This does nothing for local (non-static) variables, unless the
136 variable is a register variable with DECL_ASSEMBLER_NAME set. In
137 that case, or if the variable is not an automatic, it sets up the
138 RTL and outputs any assembler code (label definition, storage
139 allocation and initialization).
141 DECL is the declaration. TOP_LEVEL is nonzero
142 if this declaration is not within a function. */
144 void
145 rest_of_decl_compilation (tree decl,
146 int top_level,
147 int at_end)
149 /* We deferred calling assemble_alias so that we could collect
150 other attributes such as visibility. Emit the alias now. */
151 if (!in_lto_p)
153 tree alias;
154 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
155 if (alias)
157 alias = TREE_VALUE (TREE_VALUE (alias));
158 alias = get_identifier (TREE_STRING_POINTER (alias));
159 /* A quirk of the initial implementation of aliases required that the
160 user add "extern" to all of them. Which is silly, but now
161 historical. Do note that the symbol is in fact locally defined. */
162 if (!lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
163 DECL_EXTERNAL (decl) = 0;
164 assemble_alias (decl, alias);
168 /* Can't defer this, because it needs to happen before any
169 later function definitions are processed. */
170 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
171 make_decl_rtl (decl);
173 /* Forward declarations for nested functions are not "external",
174 but we need to treat them as if they were. */
175 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
176 || TREE_CODE (decl) == FUNCTION_DECL)
178 timevar_push (TV_VARCONST);
180 /* Don't output anything when a tentative file-scope definition
181 is seen. But at end of compilation, do output code for them.
183 We do output all variables and rely on
184 callgraph code to defer them except for forward declarations
185 (see gcc.c-torture/compile/920624-1.c) */
186 if ((at_end
187 || !DECL_DEFER_OUTPUT (decl)
188 || DECL_INITIAL (decl))
189 && !DECL_EXTERNAL (decl))
191 /* When reading LTO unit, we also read varpool, so do not
192 rebuild it. */
193 if (in_lto_p && !at_end)
195 else if (TREE_CODE (decl) != FUNCTION_DECL)
196 varpool_finalize_decl (decl);
199 #ifdef ASM_FINISH_DECLARE_OBJECT
200 if (decl == last_assemble_variable_decl)
202 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
203 top_level, at_end);
205 #endif
207 timevar_pop (TV_VARCONST);
209 else if (TREE_CODE (decl) == TYPE_DECL
210 /* Like in rest_of_type_compilation, avoid confusing the debug
211 information machinery when there are errors. */
212 && !seen_error ())
214 timevar_push (TV_SYMOUT);
215 debug_hooks->type_decl (decl, !top_level);
216 timevar_pop (TV_SYMOUT);
219 /* Let cgraph know about the existence of variables. */
220 if (in_lto_p && !at_end)
222 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
223 && TREE_STATIC (decl))
224 varpool_node (decl);
227 /* Called after finishing a record, union or enumeral type. */
229 void
230 rest_of_type_compilation (tree type, int toplev)
232 /* Avoid confusing the debug information machinery when there are
233 errors. */
234 if (seen_error ())
235 return;
237 timevar_push (TV_SYMOUT);
238 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
239 timevar_pop (TV_SYMOUT);
244 void
245 finish_optimization_passes (void)
247 int i;
248 struct dump_file_info *dfi;
249 char *name;
251 timevar_push (TV_DUMP);
252 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
254 dump_file = dump_begin (pass_profile.pass.static_pass_number, NULL);
255 end_branch_prob ();
256 if (dump_file)
257 dump_end (pass_profile.pass.static_pass_number, dump_file);
260 if (optimize > 0)
262 dump_file = dump_begin (pass_combine.pass.static_pass_number, NULL);
263 if (dump_file)
265 dump_combine_total_stats (dump_file);
266 dump_end (pass_combine.pass.static_pass_number, dump_file);
270 /* Do whatever is necessary to finish printing the graphs. */
271 if (graph_dump_format != no_graph)
272 for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
273 if (dump_initialized_p (i)
274 && (dfi->flags & TDF_GRAPH) != 0
275 && (name = get_dump_file_name (i)) != NULL)
277 finish_graph_dump_file (name);
278 free (name);
281 timevar_pop (TV_DUMP);
284 static unsigned int
285 execute_all_early_local_passes (void)
287 /* Once this pass (and its sub-passes) are complete, all functions
288 will be in SSA form. Technically this state change is happening
289 a tad early, since the sub-passes have not yet run, but since
290 none of the sub-passes are IPA passes and do not create new
291 functions, this is ok. We're setting this value for the benefit
292 of IPA passes that follow. */
293 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
294 cgraph_state = CGRAPH_STATE_IPA_SSA;
295 return 0;
298 /* Gate: execute, or not, all of the non-trivial optimizations. */
300 static bool
301 gate_all_early_local_passes (void)
303 /* Don't bother doing anything if the program has errors. */
304 return (!seen_error () && !in_lto_p);
307 struct simple_ipa_opt_pass pass_early_local_passes =
310 SIMPLE_IPA_PASS,
311 "early_local_cleanups", /* name */
312 gate_all_early_local_passes, /* gate */
313 execute_all_early_local_passes, /* execute */
314 NULL, /* sub */
315 NULL, /* next */
316 0, /* static_pass_number */
317 TV_EARLY_LOCAL, /* tv_id */
318 0, /* properties_required */
319 0, /* properties_provided */
320 0, /* properties_destroyed */
321 0, /* todo_flags_start */
322 TODO_remove_functions /* todo_flags_finish */
326 /* Gate: execute, or not, all of the non-trivial optimizations. */
328 static bool
329 gate_all_early_optimizations (void)
331 return (optimize >= 1
332 /* Don't bother doing anything if the program has errors. */
333 && !seen_error ());
336 struct gimple_opt_pass pass_all_early_optimizations =
339 GIMPLE_PASS,
340 "early_optimizations", /* name */
341 gate_all_early_optimizations, /* gate */
342 NULL, /* execute */
343 NULL, /* sub */
344 NULL, /* next */
345 0, /* static_pass_number */
346 TV_NONE, /* tv_id */
347 0, /* properties_required */
348 0, /* properties_provided */
349 0, /* properties_destroyed */
350 0, /* todo_flags_start */
351 0 /* todo_flags_finish */
355 /* Gate: execute, or not, all of the non-trivial optimizations. */
357 static bool
358 gate_all_optimizations (void)
360 return (optimize >= 1
361 /* Don't bother doing anything if the program has errors.
362 We have to pass down the queue if we already went into SSA */
363 && (!seen_error () || gimple_in_ssa_p (cfun)));
366 struct gimple_opt_pass pass_all_optimizations =
369 GIMPLE_PASS,
370 "*all_optimizations", /* name */
371 gate_all_optimizations, /* gate */
372 NULL, /* execute */
373 NULL, /* sub */
374 NULL, /* next */
375 0, /* static_pass_number */
376 TV_OPTIMIZE, /* tv_id */
377 0, /* properties_required */
378 0, /* properties_provided */
379 0, /* properties_destroyed */
380 0, /* todo_flags_start */
381 0 /* todo_flags_finish */
385 static bool
386 gate_rest_of_compilation (void)
388 /* Early return if there were errors. We can run afoul of our
389 consistency checks, and there's not really much point in fixing them. */
390 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
393 struct gimple_opt_pass pass_rest_of_compilation =
396 GIMPLE_PASS,
397 "*rest_of_compilation", /* name */
398 gate_rest_of_compilation, /* gate */
399 NULL, /* execute */
400 NULL, /* sub */
401 NULL, /* next */
402 0, /* static_pass_number */
403 TV_REST_OF_COMPILATION, /* tv_id */
404 PROP_rtl, /* properties_required */
405 0, /* properties_provided */
406 0, /* properties_destroyed */
407 0, /* todo_flags_start */
408 TODO_ggc_collect /* todo_flags_finish */
412 static bool
413 gate_postreload (void)
415 return reload_completed;
418 struct rtl_opt_pass pass_postreload =
421 RTL_PASS,
422 "*all-postreload", /* name */
423 gate_postreload, /* gate */
424 NULL, /* execute */
425 NULL, /* sub */
426 NULL, /* next */
427 0, /* static_pass_number */
428 TV_POSTRELOAD, /* tv_id */
429 PROP_rtl, /* properties_required */
430 0, /* properties_provided */
431 0, /* properties_destroyed */
432 0, /* todo_flags_start */
433 TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
439 /* The root of the compilation pass tree, once constructed. */
440 struct opt_pass *all_passes, *all_small_ipa_passes, *all_lowering_passes,
441 *all_regular_ipa_passes, *all_late_ipa_passes, *all_lto_gen_passes;
443 /* This is used by plugins, and should also be used in register_pass. */
444 #define DEF_PASS_LIST(LIST) &LIST,
445 struct opt_pass **gcc_pass_lists[] = { GCC_PASS_LISTS NULL };
446 #undef DEF_PASS_LIST
448 /* A map from static pass id to optimization pass. */
449 struct opt_pass **passes_by_id;
450 int passes_by_id_size;
452 /* Set the static pass number of pass PASS to ID and record that
453 in the mapping from static pass number to pass. */
455 static void
456 set_pass_for_id (int id, struct opt_pass *pass)
458 pass->static_pass_number = id;
459 if (passes_by_id_size <= id)
461 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
462 memset (passes_by_id + passes_by_id_size, 0,
463 (id + 1 - passes_by_id_size) * sizeof (void *));
464 passes_by_id_size = id + 1;
466 passes_by_id[id] = pass;
469 /* Return the pass with the static pass number ID. */
471 struct opt_pass *
472 get_pass_for_id (int id)
474 if (id >= passes_by_id_size)
475 return NULL;
476 return passes_by_id[id];
479 /* Iterate over the pass tree allocating dump file numbers. We want
480 to do this depth first, and independent of whether the pass is
481 enabled or not. */
483 void
484 register_one_dump_file (struct opt_pass *pass)
486 char *dot_name, *flag_name, *glob_name;
487 const char *name, *full_name, *prefix;
488 char num[10];
489 int flags, id;
491 /* See below in next_pass_1. */
492 num[0] = '\0';
493 if (pass->static_pass_number != -1)
494 sprintf (num, "%d", ((int) pass->static_pass_number < 0
495 ? 1 : pass->static_pass_number));
497 /* The name is both used to identify the pass for the purposes of plugins,
498 and to specify dump file name and option.
499 The latter two might want something short which is not quite unique; for
500 that reason, we may have a disambiguating prefix, followed by a space
501 to mark the start of the following dump file name / option string. */
502 name = strchr (pass->name, ' ');
503 name = name ? name + 1 : pass->name;
504 dot_name = concat (".", name, num, NULL);
505 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
506 prefix = "ipa-", flags = TDF_IPA;
507 else if (pass->type == GIMPLE_PASS)
508 prefix = "tree-", flags = TDF_TREE;
509 else
510 prefix = "rtl-", flags = TDF_RTL;
512 flag_name = concat (prefix, name, num, NULL);
513 glob_name = concat (prefix, name, NULL);
514 id = dump_register (dot_name, flag_name, glob_name, flags);
515 set_pass_for_id (id, pass);
516 full_name = concat (prefix, pass->name, num, NULL);
517 register_pass_name (pass, full_name);
518 free (CONST_CAST (char *, full_name));
521 /* Recursive worker function for register_dump_files. */
523 static int
524 register_dump_files_1 (struct opt_pass *pass, int properties)
528 int new_properties = (properties | pass->properties_provided)
529 & ~pass->properties_destroyed;
531 if (pass->name && pass->name[0] != '*')
532 register_one_dump_file (pass);
534 if (pass->sub)
535 new_properties = register_dump_files_1 (pass->sub, new_properties);
537 /* If we have a gate, combine the properties that we could have with
538 and without the pass being examined. */
539 if (pass->gate)
540 properties &= new_properties;
541 else
542 properties = new_properties;
544 pass = pass->next;
546 while (pass);
548 return properties;
551 /* Register the dump files for the pipeline starting at PASS.
552 PROPERTIES reflects the properties that are guaranteed to be available at
553 the beginning of the pipeline. */
555 static void
556 register_dump_files (struct opt_pass *pass,int properties)
558 pass->properties_required |= properties;
559 register_dump_files_1 (pass, properties);
562 struct pass_registry
564 const char* unique_name;
565 struct opt_pass *pass;
568 /* Pass registry hash function. */
570 static hashval_t
571 passr_hash (const void *p)
573 const struct pass_registry *const s = (const struct pass_registry *const) p;
574 return htab_hash_string (s->unique_name);
577 /* Hash equal function */
579 static int
580 passr_eq (const void *p1, const void *p2)
582 const struct pass_registry *const s1 = (const struct pass_registry *const) p1;
583 const struct pass_registry *const s2 = (const struct pass_registry *const) p2;
585 return !strcmp (s1->unique_name, s2->unique_name);
588 static htab_t name_to_pass_map = NULL;
590 /* Register PASS with NAME. */
592 static void
593 register_pass_name (struct opt_pass *pass, const char *name)
595 struct pass_registry **slot;
596 struct pass_registry pr;
598 if (!name_to_pass_map)
599 name_to_pass_map = htab_create (256, passr_hash, passr_eq, NULL);
601 pr.unique_name = name;
602 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map, &pr, INSERT);
603 if (!*slot)
605 struct pass_registry *new_pr;
607 new_pr = XCNEW (struct pass_registry);
608 new_pr->unique_name = xstrdup (name);
609 new_pr->pass = pass;
610 *slot = new_pr;
612 else
613 return; /* Ignore plugin passes. */
616 /* Map from pass id to canonicalized pass name. */
618 typedef const char *char_ptr;
619 DEF_VEC_P(char_ptr);
620 DEF_VEC_ALLOC_P(char_ptr, heap);
621 static VEC(char_ptr, heap) *pass_tab = NULL;
623 /* Callback function for traversing NAME_TO_PASS_MAP. */
625 static int
626 pass_traverse (void **slot, void *data ATTRIBUTE_UNUSED)
628 struct pass_registry **p = (struct pass_registry **)slot;
629 struct opt_pass *pass = (*p)->pass;
631 gcc_assert (pass->static_pass_number > 0);
632 gcc_assert (pass_tab);
634 VEC_replace (char_ptr, pass_tab, pass->static_pass_number,
635 (*p)->unique_name);
637 return 1;
640 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
641 table for dumping purpose. */
643 static void
644 create_pass_tab (void)
646 if (!flag_dump_passes)
647 return;
649 VEC_safe_grow_cleared (char_ptr, heap,
650 pass_tab, passes_by_id_size + 1);
651 htab_traverse (name_to_pass_map, pass_traverse, NULL);
654 static bool override_gate_status (struct opt_pass *, tree, bool);
656 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
657 is turned on or not. */
659 static void
660 dump_one_pass (struct opt_pass *pass, int pass_indent)
662 int indent = 3 * pass_indent;
663 const char *pn;
664 bool is_on, is_really_on;
666 is_on = (pass->gate == NULL) ? true : pass->gate();
667 is_really_on = override_gate_status (pass, current_function_decl, is_on);
669 if (pass->static_pass_number <= 0)
670 pn = pass->name;
671 else
672 pn = VEC_index (char_ptr, pass_tab, pass->static_pass_number);
674 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
675 (15 - indent < 0 ? 0 : 15 - indent), " ",
676 is_on ? " ON" : " OFF",
677 ((!is_on) == (!is_really_on) ? ""
678 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
681 /* Dump pass list PASS with indentation INDENT. */
683 static void
684 dump_pass_list (struct opt_pass *pass, int indent)
688 dump_one_pass (pass, indent);
689 if (pass->sub)
690 dump_pass_list (pass->sub, indent + 1);
691 pass = pass->next;
693 while (pass);
696 /* Dump all optimization passes. */
698 void
699 dump_passes (void)
701 struct cgraph_node *n, *node = NULL;
702 tree save_fndecl = current_function_decl;
704 create_pass_tab();
706 FOR_EACH_DEFINED_FUNCTION (n)
707 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
709 node = n;
710 break;
713 if (!node)
714 return;
716 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
717 current_function_decl = node->symbol.decl;
719 dump_pass_list (all_lowering_passes, 1);
720 dump_pass_list (all_small_ipa_passes, 1);
721 dump_pass_list (all_regular_ipa_passes, 1);
722 dump_pass_list (all_lto_gen_passes, 1);
723 dump_pass_list (all_late_ipa_passes, 1);
724 dump_pass_list (all_passes, 1);
726 pop_cfun ();
727 current_function_decl = save_fndecl;
731 /* Returns the pass with NAME. */
733 static struct opt_pass *
734 get_pass_by_name (const char *name)
736 struct pass_registry **slot, pr;
738 pr.unique_name = name;
739 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map,
740 &pr, NO_INSERT);
742 if (!slot || !*slot)
743 return NULL;
745 return (*slot)->pass;
749 /* Range [start, last]. */
751 struct uid_range
753 unsigned int start;
754 unsigned int last;
755 const char *assem_name;
756 struct uid_range *next;
759 typedef struct uid_range *uid_range_p;
761 DEF_VEC_P(uid_range_p);
762 DEF_VEC_ALLOC_P(uid_range_p, heap);
764 static VEC(uid_range_p, heap) *enabled_pass_uid_range_tab = NULL;
765 static VEC(uid_range_p, heap) *disabled_pass_uid_range_tab = NULL;
768 /* Parse option string for -fdisable- and -fenable-
769 The syntax of the options:
771 -fenable-<pass_name>
772 -fdisable-<pass_name>
774 -fenable-<pass_name>=s1:e1,s2:e2,...
775 -fdisable-<pass_name>=s1:e1,s2:e2,...
778 static void
779 enable_disable_pass (const char *arg, bool is_enable)
781 struct opt_pass *pass;
782 char *range_str, *phase_name;
783 char *argstr = xstrdup (arg);
784 VEC(uid_range_p, heap) **tab = 0;
786 range_str = strchr (argstr,'=');
787 if (range_str)
789 *range_str = '\0';
790 range_str++;
793 phase_name = argstr;
794 if (!*phase_name)
796 if (is_enable)
797 error ("unrecognized option -fenable");
798 else
799 error ("unrecognized option -fdisable");
800 free (argstr);
801 return;
803 pass = get_pass_by_name (phase_name);
804 if (!pass || pass->static_pass_number == -1)
806 if (is_enable)
807 error ("unknown pass %s specified in -fenable", phase_name);
808 else
809 error ("unknown pass %s specified in -fdisable", phase_name);
810 free (argstr);
811 return;
814 if (is_enable)
815 tab = &enabled_pass_uid_range_tab;
816 else
817 tab = &disabled_pass_uid_range_tab;
819 if ((unsigned) pass->static_pass_number >= VEC_length (uid_range_p, *tab))
820 VEC_safe_grow_cleared (uid_range_p, heap,
821 *tab, pass->static_pass_number + 1);
823 if (!range_str)
825 uid_range_p slot;
826 uid_range_p new_range = XCNEW (struct uid_range);
828 new_range->start = 0;
829 new_range->last = (unsigned)-1;
831 slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
832 new_range->next = slot;
833 VEC_replace (uid_range_p, *tab, pass->static_pass_number,
834 new_range);
835 if (is_enable)
836 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
837 "of [%u, %u]", phase_name, new_range->start, new_range->last);
838 else
839 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
840 "of [%u, %u]", phase_name, new_range->start, new_range->last);
842 else
844 char *next_range = NULL;
845 char *one_range = range_str;
846 char *end_val = NULL;
850 uid_range_p slot;
851 uid_range_p new_range;
852 char *invalid = NULL;
853 long start;
854 char *func_name = NULL;
856 next_range = strchr (one_range, ',');
857 if (next_range)
859 *next_range = '\0';
860 next_range++;
863 end_val = strchr (one_range, ':');
864 if (end_val)
866 *end_val = '\0';
867 end_val++;
869 start = strtol (one_range, &invalid, 10);
870 if (*invalid || start < 0)
872 if (end_val || (one_range[0] >= '0'
873 && one_range[0] <= '9'))
875 error ("Invalid range %s in option %s",
876 one_range,
877 is_enable ? "-fenable" : "-fdisable");
878 free (argstr);
879 return;
881 func_name = one_range;
883 if (!end_val)
885 new_range = XCNEW (struct uid_range);
886 if (!func_name)
888 new_range->start = (unsigned) start;
889 new_range->last = (unsigned) start;
891 else
893 new_range->start = (unsigned) -1;
894 new_range->last = (unsigned) -1;
895 new_range->assem_name = xstrdup (func_name);
898 else
900 long last = strtol (end_val, &invalid, 10);
901 if (*invalid || last < start)
903 error ("Invalid range %s in option %s",
904 end_val,
905 is_enable ? "-fenable" : "-fdisable");
906 free (argstr);
907 return;
909 new_range = XCNEW (struct uid_range);
910 new_range->start = (unsigned) start;
911 new_range->last = (unsigned) last;
914 slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
915 new_range->next = slot;
916 VEC_replace (uid_range_p, *tab, pass->static_pass_number,
917 new_range);
918 if (is_enable)
920 if (new_range->assem_name)
921 inform (UNKNOWN_LOCATION,
922 "enable pass %s for function %s",
923 phase_name, new_range->assem_name);
924 else
925 inform (UNKNOWN_LOCATION,
926 "enable pass %s for functions in the range of [%u, %u]",
927 phase_name, new_range->start, new_range->last);
929 else
931 if (new_range->assem_name)
932 inform (UNKNOWN_LOCATION,
933 "disable pass %s for function %s",
934 phase_name, new_range->assem_name);
935 else
936 inform (UNKNOWN_LOCATION,
937 "disable pass %s for functions in the range of [%u, %u]",
938 phase_name, new_range->start, new_range->last);
941 one_range = next_range;
942 } while (next_range);
945 free (argstr);
948 /* Enable pass specified by ARG. */
950 void
951 enable_pass (const char *arg)
953 enable_disable_pass (arg, true);
956 /* Disable pass specified by ARG. */
958 void
959 disable_pass (const char *arg)
961 enable_disable_pass (arg, false);
964 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
966 static bool
967 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
968 tree func,
969 VEC(uid_range_p, heap) *tab)
971 uid_range_p slot, range;
972 int cgraph_uid;
973 const char *aname = NULL;
975 if (!tab
976 || (unsigned) pass->static_pass_number >= VEC_length (uid_range_p, tab)
977 || pass->static_pass_number == -1)
978 return false;
980 slot = VEC_index (uid_range_p, tab, pass->static_pass_number);
981 if (!slot)
982 return false;
984 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
985 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
986 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
988 range = slot;
989 while (range)
991 if ((unsigned) cgraph_uid >= range->start
992 && (unsigned) cgraph_uid <= range->last)
993 return true;
994 if (range->assem_name && aname
995 && !strcmp (range->assem_name, aname))
996 return true;
997 range = range->next;
1000 return false;
1003 /* Look at the static_pass_number and duplicate the pass
1004 if it is already added to a list. */
1006 static struct opt_pass *
1007 make_pass_instance (struct opt_pass *pass, bool track_duplicates)
1009 /* A nonzero static_pass_number indicates that the
1010 pass is already in the list. */
1011 if (pass->static_pass_number)
1013 struct opt_pass *new_pass;
1015 if (pass->type == GIMPLE_PASS
1016 || pass->type == RTL_PASS
1017 || pass->type == SIMPLE_IPA_PASS)
1019 new_pass = XNEW (struct opt_pass);
1020 memcpy (new_pass, pass, sizeof (struct opt_pass));
1022 else if (pass->type == IPA_PASS)
1024 new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
1025 memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
1027 else
1028 gcc_unreachable ();
1030 new_pass->next = NULL;
1032 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1034 /* Indicate to register_dump_files that this pass has duplicates,
1035 and so it should rename the dump file. The first instance will
1036 be -1, and be number of duplicates = -static_pass_number - 1.
1037 Subsequent instances will be > 0 and just the duplicate number. */
1038 if ((pass->name && pass->name[0] != '*') || track_duplicates)
1040 pass->static_pass_number -= 1;
1041 new_pass->static_pass_number = -pass->static_pass_number;
1043 return new_pass;
1045 else
1047 pass->todo_flags_start |= TODO_mark_first_instance;
1048 pass->static_pass_number = -1;
1050 invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
1052 return pass;
1055 /* Add a pass to the pass list. Duplicate the pass if it's already
1056 in the list. */
1058 static struct opt_pass **
1059 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
1061 /* Every pass should have a name so that plugins can refer to them. */
1062 gcc_assert (pass->name != NULL);
1064 *list = make_pass_instance (pass, false);
1066 return &(*list)->next;
1069 /* List node for an inserted pass instance. We need to keep track of all
1070 the newly-added pass instances (with 'added_pass_nodes' defined below)
1071 so that we can register their dump files after pass-positioning is finished.
1072 Registering dumping files needs to be post-processed or the
1073 static_pass_number of the opt_pass object would be modified and mess up
1074 the dump file names of future pass instances to be added. */
1076 struct pass_list_node
1078 struct opt_pass *pass;
1079 struct pass_list_node *next;
1082 static struct pass_list_node *added_pass_nodes = NULL;
1083 static struct pass_list_node *prev_added_pass_node;
1085 /* Insert the pass at the proper position. Return true if the pass
1086 is successfully added.
1088 NEW_PASS_INFO - new pass to be inserted
1089 PASS_LIST - root of the pass list to insert the new pass to */
1091 static bool
1092 position_pass (struct register_pass_info *new_pass_info,
1093 struct opt_pass **pass_list)
1095 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1096 bool success = false;
1098 for ( ; pass; prev_pass = pass, pass = pass->next)
1100 /* Check if the current pass is of the same type as the new pass and
1101 matches the name and the instance number of the reference pass. */
1102 if (pass->type == new_pass_info->pass->type
1103 && pass->name
1104 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1105 && ((new_pass_info->ref_pass_instance_number == 0)
1106 || (new_pass_info->ref_pass_instance_number ==
1107 pass->static_pass_number)
1108 || (new_pass_info->ref_pass_instance_number == 1
1109 && pass->todo_flags_start & TODO_mark_first_instance)))
1111 struct opt_pass *new_pass;
1112 struct pass_list_node *new_pass_node;
1114 new_pass = make_pass_instance (new_pass_info->pass, true);
1116 /* Insert the new pass instance based on the positioning op. */
1117 switch (new_pass_info->pos_op)
1119 case PASS_POS_INSERT_AFTER:
1120 new_pass->next = pass->next;
1121 pass->next = new_pass;
1123 /* Skip newly inserted pass to avoid repeated
1124 insertions in the case where the new pass and the
1125 existing one have the same name. */
1126 pass = new_pass;
1127 break;
1128 case PASS_POS_INSERT_BEFORE:
1129 new_pass->next = pass;
1130 if (prev_pass)
1131 prev_pass->next = new_pass;
1132 else
1133 *pass_list = new_pass;
1134 break;
1135 case PASS_POS_REPLACE:
1136 new_pass->next = pass->next;
1137 if (prev_pass)
1138 prev_pass->next = new_pass;
1139 else
1140 *pass_list = new_pass;
1141 new_pass->sub = pass->sub;
1142 new_pass->tv_id = pass->tv_id;
1143 pass = new_pass;
1144 break;
1145 default:
1146 error ("invalid pass positioning operation");
1147 return false;
1150 /* Save the newly added pass (instance) in the added_pass_nodes
1151 list so that we can register its dump file later. Note that
1152 we cannot register the dump file now because doing so will modify
1153 the static_pass_number of the opt_pass object and therefore
1154 mess up the dump file name of future instances. */
1155 new_pass_node = XCNEW (struct pass_list_node);
1156 new_pass_node->pass = new_pass;
1157 if (!added_pass_nodes)
1158 added_pass_nodes = new_pass_node;
1159 else
1160 prev_added_pass_node->next = new_pass_node;
1161 prev_added_pass_node = new_pass_node;
1163 success = true;
1166 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1167 success = true;
1170 return success;
1173 /* Hooks a new pass into the pass lists.
1175 PASS_INFO - pass information that specifies the opt_pass object,
1176 reference pass, instance number, and how to position
1177 the pass */
1179 void
1180 register_pass (struct register_pass_info *pass_info)
1182 bool all_instances, success;
1184 /* The checks below could fail in buggy plugins. Existing GCC
1185 passes should never fail these checks, so we mention plugin in
1186 the messages. */
1187 if (!pass_info->pass)
1188 fatal_error ("plugin cannot register a missing pass");
1190 if (!pass_info->pass->name)
1191 fatal_error ("plugin cannot register an unnamed pass");
1193 if (!pass_info->reference_pass_name)
1194 fatal_error
1195 ("plugin cannot register pass %qs without reference pass name",
1196 pass_info->pass->name);
1198 /* Try to insert the new pass to the pass lists. We need to check
1199 all five lists as the reference pass could be in one (or all) of
1200 them. */
1201 all_instances = pass_info->ref_pass_instance_number == 0;
1202 success = position_pass (pass_info, &all_lowering_passes);
1203 if (!success || all_instances)
1204 success |= position_pass (pass_info, &all_small_ipa_passes);
1205 if (!success || all_instances)
1206 success |= position_pass (pass_info, &all_regular_ipa_passes);
1207 if (!success || all_instances)
1208 success |= position_pass (pass_info, &all_lto_gen_passes);
1209 if (!success || all_instances)
1210 success |= position_pass (pass_info, &all_late_ipa_passes);
1211 if (!success || all_instances)
1212 success |= position_pass (pass_info, &all_passes);
1213 if (!success)
1214 fatal_error
1215 ("pass %qs not found but is referenced by new pass %qs",
1216 pass_info->reference_pass_name, pass_info->pass->name);
1218 /* OK, we have successfully inserted the new pass. We need to register
1219 the dump files for the newly added pass and its duplicates (if any).
1220 Because the registration of plugin/backend passes happens after the
1221 command-line options are parsed, the options that specify single
1222 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1223 passes. Therefore we currently can only enable dumping of
1224 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1225 are specified. While doing so, we also delete the pass_list_node
1226 objects created during pass positioning. */
1227 while (added_pass_nodes)
1229 struct pass_list_node *next_node = added_pass_nodes->next;
1230 enum tree_dump_index tdi;
1231 register_one_dump_file (added_pass_nodes->pass);
1232 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1233 || added_pass_nodes->pass->type == IPA_PASS)
1234 tdi = TDI_ipa_all;
1235 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1236 tdi = TDI_tree_all;
1237 else
1238 tdi = TDI_rtl_all;
1239 /* Check if dump-all flag is specified. */
1240 if (get_dump_file_info (tdi)->state)
1241 get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1242 ->state = get_dump_file_info (tdi)->state;
1243 XDELETE (added_pass_nodes);
1244 added_pass_nodes = next_node;
1248 /* Construct the pass tree. The sequencing of passes is driven by
1249 the cgraph routines:
1251 finalize_compilation_unit ()
1252 for each node N in the cgraph
1253 cgraph_analyze_function (N)
1254 cgraph_lower_function (N) -> all_lowering_passes
1256 If we are optimizing, compile is then invoked:
1258 compile ()
1259 ipa_passes () -> all_small_ipa_passes
1260 -> Analysis of all_regular_ipa_passes
1261 * possible LTO streaming at copmilation time *
1262 -> Execution of all_regular_ipa_passes
1263 * possible LTO streaming at link time *
1264 -> all_late_ipa_passes
1265 expand_all_functions ()
1266 for each node N in the cgraph
1267 expand_function (N) -> Transformation of all_regular_ipa_passes
1268 -> all_passes
1271 void
1272 init_optimization_passes (void)
1274 struct opt_pass **p;
1276 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &((PASS).pass)))
1278 /* All passes needed to lower the function into shape optimizers can
1279 operate on. These passes are always run first on the function, but
1280 backend might produce already lowered functions that are not processed
1281 by these passes. */
1282 p = &all_lowering_passes;
1283 NEXT_PASS (pass_warn_unused_result);
1284 NEXT_PASS (pass_diagnose_omp_blocks);
1285 NEXT_PASS (pass_diagnose_tm_blocks);
1286 NEXT_PASS (pass_mudflap_1);
1287 NEXT_PASS (pass_lower_omp);
1288 NEXT_PASS (pass_lower_cf);
1289 NEXT_PASS (pass_lower_tm);
1290 NEXT_PASS (pass_refactor_eh);
1291 NEXT_PASS (pass_lower_eh);
1292 NEXT_PASS (pass_build_cfg);
1293 NEXT_PASS (pass_warn_function_return);
1294 NEXT_PASS (pass_build_cgraph_edges);
1295 *p = NULL;
1297 /* Interprocedural optimization passes. */
1298 p = &all_small_ipa_passes;
1299 NEXT_PASS (pass_ipa_free_lang_data);
1300 NEXT_PASS (pass_ipa_function_and_variable_visibility);
1301 NEXT_PASS (pass_early_local_passes);
1303 struct opt_pass **p = &pass_early_local_passes.pass.sub;
1304 NEXT_PASS (pass_fixup_cfg);
1305 NEXT_PASS (pass_init_datastructures);
1306 NEXT_PASS (pass_expand_omp);
1308 NEXT_PASS (pass_referenced_vars);
1309 NEXT_PASS (pass_build_ssa);
1310 NEXT_PASS (pass_lower_vector);
1311 NEXT_PASS (pass_early_warn_uninitialized);
1312 NEXT_PASS (pass_rebuild_cgraph_edges);
1313 NEXT_PASS (pass_inline_parameters);
1314 NEXT_PASS (pass_early_inline);
1315 NEXT_PASS (pass_all_early_optimizations);
1317 struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
1318 NEXT_PASS (pass_remove_cgraph_callee_edges);
1319 NEXT_PASS (pass_rename_ssa_copies);
1320 NEXT_PASS (pass_ccp);
1321 NEXT_PASS (pass_forwprop);
1322 /* pass_build_ealias is a dummy pass that ensures that we
1323 execute TODO_rebuild_alias at this point. Re-building
1324 alias information also rewrites no longer addressed
1325 locals into SSA form if possible. */
1326 NEXT_PASS (pass_build_ealias);
1327 NEXT_PASS (pass_sra_early);
1328 NEXT_PASS (pass_fre);
1329 NEXT_PASS (pass_copy_prop);
1330 NEXT_PASS (pass_merge_phi);
1331 NEXT_PASS (pass_cd_dce);
1332 NEXT_PASS (pass_early_ipa_sra);
1333 NEXT_PASS (pass_tail_recursion);
1334 NEXT_PASS (pass_convert_switch);
1335 NEXT_PASS (pass_cleanup_eh);
1336 NEXT_PASS (pass_profile);
1337 NEXT_PASS (pass_local_pure_const);
1338 /* Split functions creates parts that are not run through
1339 early optimizations again. It is thus good idea to do this
1340 late. */
1341 NEXT_PASS (pass_split_functions);
1343 NEXT_PASS (pass_release_ssa_names);
1344 NEXT_PASS (pass_rebuild_cgraph_edges);
1345 NEXT_PASS (pass_inline_parameters);
1347 NEXT_PASS (pass_ipa_tree_profile);
1349 struct opt_pass **p = &pass_ipa_tree_profile.pass.sub;
1350 NEXT_PASS (pass_feedback_split_functions);
1352 NEXT_PASS (pass_ipa_increase_alignment);
1353 NEXT_PASS (pass_ipa_matrix_reorg);
1354 NEXT_PASS (pass_ipa_tm);
1355 NEXT_PASS (pass_ipa_lower_emutls);
1356 *p = NULL;
1358 p = &all_regular_ipa_passes;
1359 NEXT_PASS (pass_ipa_whole_program_visibility);
1360 NEXT_PASS (pass_ipa_profile);
1361 NEXT_PASS (pass_ipa_cp);
1362 NEXT_PASS (pass_ipa_cdtor_merge);
1363 NEXT_PASS (pass_ipa_inline);
1364 NEXT_PASS (pass_ipa_pure_const);
1365 NEXT_PASS (pass_ipa_reference);
1366 *p = NULL;
1368 p = &all_lto_gen_passes;
1369 NEXT_PASS (pass_ipa_lto_gimple_out);
1370 NEXT_PASS (pass_ipa_lto_finish_out); /* This must be the last LTO pass. */
1371 *p = NULL;
1373 /* Simple IPA passes executed after the regular passes. In WHOPR mode the
1374 passes are executed after partitioning and thus see just parts of the
1375 compiled unit. */
1376 p = &all_late_ipa_passes;
1377 NEXT_PASS (pass_ipa_pta);
1378 *p = NULL;
1379 /* These passes are run after IPA passes on every function that is being
1380 output to the assembler file. */
1381 p = &all_passes;
1382 NEXT_PASS (pass_fixup_cfg);
1383 NEXT_PASS (pass_lower_eh_dispatch);
1384 NEXT_PASS (pass_all_optimizations);
1386 struct opt_pass **p = &pass_all_optimizations.pass.sub;
1387 NEXT_PASS (pass_remove_cgraph_callee_edges);
1388 /* Initial scalar cleanups before alias computation.
1389 They ensure memory accesses are not indirect wherever possible. */
1390 NEXT_PASS (pass_strip_predict_hints);
1391 NEXT_PASS (pass_rename_ssa_copies);
1392 NEXT_PASS (pass_complete_unrolli);
1393 NEXT_PASS (pass_ccp);
1394 NEXT_PASS (pass_forwprop);
1395 NEXT_PASS (pass_call_cdce);
1396 /* pass_build_alias is a dummy pass that ensures that we
1397 execute TODO_rebuild_alias at this point. Re-building
1398 alias information also rewrites no longer addressed
1399 locals into SSA form if possible. */
1400 NEXT_PASS (pass_build_alias);
1401 NEXT_PASS (pass_return_slot);
1402 NEXT_PASS (pass_phiprop);
1403 NEXT_PASS (pass_fre);
1404 NEXT_PASS (pass_copy_prop);
1405 NEXT_PASS (pass_merge_phi);
1406 NEXT_PASS (pass_vrp);
1407 NEXT_PASS (pass_dce);
1408 NEXT_PASS (pass_cselim);
1409 NEXT_PASS (pass_tree_ifcombine);
1410 NEXT_PASS (pass_phiopt);
1411 NEXT_PASS (pass_tail_recursion);
1412 NEXT_PASS (pass_ch);
1413 NEXT_PASS (pass_stdarg);
1414 NEXT_PASS (pass_lower_complex);
1415 NEXT_PASS (pass_sra);
1416 NEXT_PASS (pass_rename_ssa_copies);
1417 /* The dom pass will also resolve all __builtin_constant_p calls
1418 that are still there to 0. This has to be done after some
1419 propagations have already run, but before some more dead code
1420 is removed, and this place fits nicely. Remember this when
1421 trying to move or duplicate pass_dominator somewhere earlier. */
1422 NEXT_PASS (pass_dominator);
1423 /* The only const/copy propagation opportunities left after
1424 DOM should be due to degenerate PHI nodes. So rather than
1425 run the full propagators, run a specialized pass which
1426 only examines PHIs to discover const/copy propagation
1427 opportunities. */
1428 NEXT_PASS (pass_phi_only_cprop);
1429 NEXT_PASS (pass_dse);
1430 NEXT_PASS (pass_reassoc);
1431 NEXT_PASS (pass_dce);
1432 NEXT_PASS (pass_forwprop);
1433 NEXT_PASS (pass_phiopt);
1434 NEXT_PASS (pass_object_sizes);
1435 NEXT_PASS (pass_strlen);
1436 NEXT_PASS (pass_ccp);
1437 NEXT_PASS (pass_copy_prop);
1438 NEXT_PASS (pass_cse_sincos);
1439 NEXT_PASS (pass_optimize_bswap);
1440 NEXT_PASS (pass_split_crit_edges);
1441 NEXT_PASS (pass_pre);
1442 NEXT_PASS (pass_sink_code);
1443 NEXT_PASS (pass_tree_loop);
1445 struct opt_pass **p = &pass_tree_loop.pass.sub;
1446 NEXT_PASS (pass_tree_loop_init);
1447 NEXT_PASS (pass_lim);
1448 NEXT_PASS (pass_copy_prop);
1449 NEXT_PASS (pass_dce_loop);
1450 NEXT_PASS (pass_tree_unswitch);
1451 NEXT_PASS (pass_scev_cprop);
1452 NEXT_PASS (pass_record_bounds);
1453 NEXT_PASS (pass_check_data_deps);
1454 NEXT_PASS (pass_loop_distribution);
1455 NEXT_PASS (pass_copy_prop);
1456 NEXT_PASS (pass_graphite);
1458 struct opt_pass **p = &pass_graphite.pass.sub;
1459 NEXT_PASS (pass_graphite_transforms);
1460 NEXT_PASS (pass_lim);
1461 NEXT_PASS (pass_copy_prop);
1462 NEXT_PASS (pass_dce_loop);
1464 NEXT_PASS (pass_iv_canon);
1465 NEXT_PASS (pass_if_conversion);
1466 NEXT_PASS (pass_vectorize);
1468 struct opt_pass **p = &pass_vectorize.pass.sub;
1469 NEXT_PASS (pass_dce_loop);
1471 NEXT_PASS (pass_predcom);
1472 NEXT_PASS (pass_complete_unroll);
1473 NEXT_PASS (pass_slp_vectorize);
1474 NEXT_PASS (pass_parallelize_loops);
1475 NEXT_PASS (pass_loop_prefetch);
1476 NEXT_PASS (pass_iv_optimize);
1477 NEXT_PASS (pass_lim);
1478 NEXT_PASS (pass_tree_loop_done);
1480 NEXT_PASS (pass_lower_vector_ssa);
1481 NEXT_PASS (pass_cse_reciprocals);
1482 NEXT_PASS (pass_reassoc);
1483 NEXT_PASS (pass_vrp);
1484 NEXT_PASS (pass_dominator);
1485 /* The only const/copy propagation opportunities left after
1486 DOM should be due to degenerate PHI nodes. So rather than
1487 run the full propagators, run a specialized pass which
1488 only examines PHIs to discover const/copy propagation
1489 opportunities. */
1490 NEXT_PASS (pass_phi_only_cprop);
1491 NEXT_PASS (pass_cd_dce);
1492 NEXT_PASS (pass_tracer);
1494 /* FIXME: If DCE is not run before checking for uninitialized uses,
1495 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
1496 However, this also causes us to misdiagnose cases that should be
1497 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
1499 To fix the false positives in uninit-5.c, we would have to
1500 account for the predicates protecting the set and the use of each
1501 variable. Using a representation like Gated Single Assignment
1502 may help. */
1503 NEXT_PASS (pass_late_warn_uninitialized);
1504 NEXT_PASS (pass_dse);
1505 NEXT_PASS (pass_forwprop);
1506 NEXT_PASS (pass_phiopt);
1507 NEXT_PASS (pass_fold_builtins);
1508 NEXT_PASS (pass_optimize_widening_mul);
1509 NEXT_PASS (pass_tail_calls);
1510 NEXT_PASS (pass_rename_ssa_copies);
1511 NEXT_PASS (pass_uncprop);
1512 NEXT_PASS (pass_local_pure_const);
1514 NEXT_PASS (pass_tm_init);
1516 struct opt_pass **p = &pass_tm_init.pass.sub;
1517 NEXT_PASS (pass_tm_mark);
1518 NEXT_PASS (pass_tm_memopt);
1519 NEXT_PASS (pass_tm_edges);
1521 NEXT_PASS (pass_lower_complex_O0);
1522 NEXT_PASS (pass_cleanup_eh);
1523 NEXT_PASS (pass_lower_resx);
1524 NEXT_PASS (pass_nrv);
1525 NEXT_PASS (pass_mudflap_2);
1526 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
1527 NEXT_PASS (pass_warn_function_noreturn);
1529 NEXT_PASS (pass_expand);
1531 NEXT_PASS (pass_rest_of_compilation);
1533 struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
1534 NEXT_PASS (pass_instantiate_virtual_regs);
1535 NEXT_PASS (pass_into_cfg_layout_mode);
1536 NEXT_PASS (pass_jump2);
1537 NEXT_PASS (pass_lower_subreg);
1538 NEXT_PASS (pass_df_initialize_opt);
1539 NEXT_PASS (pass_cse);
1540 NEXT_PASS (pass_rtl_fwprop);
1541 NEXT_PASS (pass_rtl_cprop);
1542 NEXT_PASS (pass_rtl_pre);
1543 NEXT_PASS (pass_rtl_hoist);
1544 NEXT_PASS (pass_rtl_cprop);
1545 NEXT_PASS (pass_rtl_store_motion);
1546 NEXT_PASS (pass_cse_after_global_opts);
1547 NEXT_PASS (pass_rtl_ifcvt);
1548 NEXT_PASS (pass_reginfo_init);
1549 /* Perform loop optimizations. It might be better to do them a bit
1550 sooner, but we want the profile feedback to work more
1551 efficiently. */
1552 NEXT_PASS (pass_loop2);
1554 struct opt_pass **p = &pass_loop2.pass.sub;
1555 NEXT_PASS (pass_rtl_loop_init);
1556 NEXT_PASS (pass_rtl_move_loop_invariants);
1557 NEXT_PASS (pass_rtl_unswitch);
1558 NEXT_PASS (pass_rtl_unroll_and_peel_loops);
1559 NEXT_PASS (pass_rtl_doloop);
1560 NEXT_PASS (pass_rtl_loop_done);
1561 *p = NULL;
1563 NEXT_PASS (pass_web);
1564 NEXT_PASS (pass_rtl_cprop);
1565 NEXT_PASS (pass_cse2);
1566 NEXT_PASS (pass_rtl_dse1);
1567 NEXT_PASS (pass_rtl_fwprop_addr);
1568 NEXT_PASS (pass_inc_dec);
1569 NEXT_PASS (pass_initialize_regs);
1570 NEXT_PASS (pass_ud_rtl_dce);
1571 NEXT_PASS (pass_combine);
1572 NEXT_PASS (pass_if_after_combine);
1573 NEXT_PASS (pass_partition_blocks);
1574 NEXT_PASS (pass_regmove);
1575 NEXT_PASS (pass_outof_cfg_layout_mode);
1576 NEXT_PASS (pass_split_all_insns);
1577 NEXT_PASS (pass_lower_subreg2);
1578 NEXT_PASS (pass_df_initialize_no_opt);
1579 NEXT_PASS (pass_stack_ptr_mod);
1580 NEXT_PASS (pass_mode_switching);
1581 NEXT_PASS (pass_match_asm_constraints);
1582 NEXT_PASS (pass_sms);
1583 NEXT_PASS (pass_sched);
1584 NEXT_PASS (pass_ira);
1585 NEXT_PASS (pass_reload);
1586 NEXT_PASS (pass_postreload);
1588 struct opt_pass **p = &pass_postreload.pass.sub;
1589 NEXT_PASS (pass_postreload_cse);
1590 NEXT_PASS (pass_gcse2);
1591 NEXT_PASS (pass_split_after_reload);
1592 NEXT_PASS (pass_ree);
1593 NEXT_PASS (pass_compare_elim_after_reload);
1594 NEXT_PASS (pass_branch_target_load_optimize1);
1595 NEXT_PASS (pass_thread_prologue_and_epilogue);
1596 NEXT_PASS (pass_rtl_dse2);
1597 NEXT_PASS (pass_stack_adjustments);
1598 NEXT_PASS (pass_peephole2);
1599 NEXT_PASS (pass_if_after_reload);
1600 NEXT_PASS (pass_regrename);
1601 NEXT_PASS (pass_cprop_hardreg);
1602 NEXT_PASS (pass_fast_rtl_dce);
1603 NEXT_PASS (pass_reorder_blocks);
1604 NEXT_PASS (pass_branch_target_load_optimize2);
1605 NEXT_PASS (pass_leaf_regs);
1606 NEXT_PASS (pass_split_before_sched2);
1607 NEXT_PASS (pass_sched2);
1608 NEXT_PASS (pass_stack_regs);
1610 struct opt_pass **p = &pass_stack_regs.pass.sub;
1611 NEXT_PASS (pass_split_before_regstack);
1612 NEXT_PASS (pass_stack_regs_run);
1614 NEXT_PASS (pass_compute_alignments);
1615 NEXT_PASS (pass_duplicate_computed_gotos);
1616 NEXT_PASS (pass_variable_tracking);
1617 NEXT_PASS (pass_free_cfg);
1618 NEXT_PASS (pass_machine_reorg);
1619 NEXT_PASS (pass_cleanup_barriers);
1620 NEXT_PASS (pass_delay_slots);
1621 NEXT_PASS (pass_split_for_shorten_branches);
1622 NEXT_PASS (pass_convert_to_eh_region_ranges);
1623 NEXT_PASS (pass_shorten_branches);
1624 NEXT_PASS (pass_set_nothrow_function_flags);
1625 NEXT_PASS (pass_dwarf2_frame);
1626 NEXT_PASS (pass_final);
1628 NEXT_PASS (pass_df_finish);
1630 NEXT_PASS (pass_clean_state);
1631 *p = NULL;
1633 #undef NEXT_PASS
1635 /* Register the passes with the tree dump code. */
1636 register_dump_files (all_lowering_passes, PROP_gimple_any);
1637 register_dump_files (all_small_ipa_passes,
1638 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1639 | PROP_cfg);
1640 register_dump_files (all_regular_ipa_passes,
1641 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1642 | PROP_cfg);
1643 register_dump_files (all_lto_gen_passes,
1644 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1645 | PROP_cfg);
1646 register_dump_files (all_late_ipa_passes,
1647 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1648 | PROP_cfg);
1649 register_dump_files (all_passes,
1650 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1651 | PROP_cfg);
1654 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1655 function CALLBACK for every function in the call graph. Otherwise,
1656 call CALLBACK on the current function. */
1658 static void
1659 do_per_function (void (*callback) (void *data), void *data)
1661 if (current_function_decl)
1662 callback (data);
1663 else
1665 struct cgraph_node *node;
1666 FOR_EACH_DEFINED_FUNCTION (node)
1667 if (gimple_has_body_p (node->symbol.decl)
1668 && (!node->clone_of || node->symbol.decl != node->clone_of->symbol.decl))
1670 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1671 current_function_decl = node->symbol.decl;
1672 callback (data);
1673 if (!flag_wpa)
1675 free_dominance_info (CDI_DOMINATORS);
1676 free_dominance_info (CDI_POST_DOMINATORS);
1678 current_function_decl = NULL;
1679 pop_cfun ();
1680 ggc_collect ();
1685 /* Because inlining might remove no-longer reachable nodes, we need to
1686 keep the array visible to garbage collector to avoid reading collected
1687 out nodes. */
1688 static int nnodes;
1689 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1691 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1692 function CALLBACK for every function in the call graph. Otherwise,
1693 call CALLBACK on the current function.
1694 This function is global so that plugins can use it. */
1695 void
1696 do_per_function_toporder (void (*callback) (void *data), void *data)
1698 int i;
1700 if (current_function_decl)
1701 callback (data);
1702 else
1704 gcc_assert (!order);
1705 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1706 nnodes = ipa_reverse_postorder (order);
1707 for (i = nnodes - 1; i >= 0; i--)
1708 order[i]->process = 1;
1709 for (i = nnodes - 1; i >= 0; i--)
1711 struct cgraph_node *node = order[i];
1713 /* Allow possibly removed nodes to be garbage collected. */
1714 order[i] = NULL;
1715 node->process = 0;
1716 if (cgraph_function_with_gimple_body_p (node))
1718 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1719 current_function_decl = node->symbol.decl;
1720 callback (data);
1721 free_dominance_info (CDI_DOMINATORS);
1722 free_dominance_info (CDI_POST_DOMINATORS);
1723 current_function_decl = NULL;
1724 pop_cfun ();
1725 ggc_collect ();
1729 ggc_free (order);
1730 order = NULL;
1731 nnodes = 0;
1734 /* Helper function to perform function body dump. */
1736 static void
1737 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1739 if (dump_file && current_function_decl)
1741 if (cfun->curr_properties & PROP_trees)
1742 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1743 else
1745 if (dump_flags & TDF_SLIM)
1746 print_rtl_slim_with_bb (dump_file, get_insns (), dump_flags);
1747 else if ((cfun->curr_properties & PROP_cfg)
1748 && (dump_flags & TDF_BLOCKS))
1749 print_rtl_with_bb (dump_file, get_insns ());
1750 else
1751 print_rtl (dump_file, get_insns ());
1753 if ((cfun->curr_properties & PROP_cfg)
1754 && graph_dump_format != no_graph
1755 && (dump_flags & TDF_GRAPH))
1756 print_rtl_graph_with_bb (dump_file_name, get_insns ());
1759 /* Flush the file. If verification fails, we won't be able to
1760 close the file before aborting. */
1761 fflush (dump_file);
1765 /* Perform all TODO actions that ought to be done on each function. */
1767 static void
1768 execute_function_todo (void *data)
1770 unsigned int flags = (size_t)data;
1771 flags &= ~cfun->last_verified;
1772 if (!flags)
1773 return;
1775 /* Always cleanup the CFG before trying to update SSA. */
1776 if (flags & TODO_cleanup_cfg)
1778 bool cleanup = cleanup_tree_cfg ();
1780 if (cleanup && (cfun->curr_properties & PROP_ssa))
1781 flags |= TODO_remove_unused_locals;
1783 /* When cleanup_tree_cfg merges consecutive blocks, it may
1784 perform some simplistic propagation when removing single
1785 valued PHI nodes. This propagation may, in turn, cause the
1786 SSA form to become out-of-date (see PR 22037). So, even
1787 if the parent pass had not scheduled an SSA update, we may
1788 still need to do one. */
1789 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1790 flags |= TODO_update_ssa;
1793 if (flags & TODO_update_ssa_any)
1795 unsigned update_flags = flags & TODO_update_ssa_any;
1796 update_ssa (update_flags);
1797 cfun->last_verified &= ~TODO_verify_ssa;
1800 if (flags & TODO_rebuild_alias)
1802 execute_update_addresses_taken ();
1803 compute_may_aliases ();
1805 else if (optimize && (flags & TODO_update_address_taken))
1806 execute_update_addresses_taken ();
1808 if (flags & TODO_remove_unused_locals)
1809 remove_unused_locals ();
1811 if (flags & TODO_rebuild_frequencies)
1812 rebuild_frequencies ();
1814 if (flags & TODO_rebuild_cgraph_edges)
1815 rebuild_cgraph_edges ();
1817 /* If we've seen errors do not bother running any verifiers. */
1818 if (seen_error ())
1819 return;
1821 #if defined ENABLE_CHECKING
1822 if (flags & TODO_verify_ssa
1823 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1825 verify_gimple_in_cfg (cfun);
1826 verify_ssa (true);
1828 else if (flags & TODO_verify_stmts)
1829 verify_gimple_in_cfg (cfun);
1830 if (flags & TODO_verify_flow)
1831 verify_flow_info ();
1832 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1833 verify_loop_closed_ssa (false);
1834 if (flags & TODO_verify_rtl_sharing)
1835 verify_rtl_sharing ();
1836 #endif
1838 cfun->last_verified = flags & TODO_verify_all;
1841 /* Perform all TODO actions. */
1842 static void
1843 execute_todo (unsigned int flags)
1845 #if defined ENABLE_CHECKING
1846 if (cfun
1847 && need_ssa_update_p (cfun))
1848 gcc_assert (flags & TODO_update_ssa_any);
1849 #endif
1851 timevar_push (TV_TODO);
1853 /* Inform the pass whether it is the first time it is run. */
1854 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1856 statistics_fini_pass ();
1858 do_per_function (execute_function_todo, (void *)(size_t) flags);
1860 /* Always remove functions just as before inlining: IPA passes might be
1861 interested to see bodies of extern inline functions that are not inlined
1862 to analyze side effects. The full removal is done just at the end
1863 of IPA pass queue. */
1864 if (flags & TODO_remove_functions)
1866 gcc_assert (!cfun);
1867 cgraph_remove_unreachable_nodes (true, dump_file);
1870 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1872 gcc_assert (!cfun);
1873 dump_symtab (dump_file);
1874 /* Flush the file. If verification fails, we won't be able to
1875 close the file before aborting. */
1876 fflush (dump_file);
1879 if (flags & TODO_ggc_collect)
1880 ggc_collect ();
1882 /* Now that the dumping has been done, we can get rid of the optional
1883 df problems. */
1884 if (flags & TODO_df_finish)
1885 df_finish_pass ((flags & TODO_df_verify) != 0);
1887 timevar_pop (TV_TODO);
1890 /* Verify invariants that should hold between passes. This is a place
1891 to put simple sanity checks. */
1893 static void
1894 verify_interpass_invariants (void)
1896 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1899 /* Clear the last verified flag. */
1901 static void
1902 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1904 cfun->last_verified = 0;
1907 /* Helper function. Verify that the properties has been turn into the
1908 properties expected by the pass. */
1910 #ifdef ENABLE_CHECKING
1911 static void
1912 verify_curr_properties (void *data)
1914 unsigned int props = (size_t)data;
1915 gcc_assert ((cfun->curr_properties & props) == props);
1917 #endif
1919 /* Initialize pass dump file. */
1920 /* This is non-static so that the plugins can use it. */
1922 bool
1923 pass_init_dump_file (struct opt_pass *pass)
1925 /* If a dump file name is present, open it if enabled. */
1926 if (pass->static_pass_number != -1)
1928 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
1929 dump_file_name = get_dump_file_name (pass->static_pass_number);
1930 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
1931 if (dump_file && current_function_decl)
1932 dump_function_header (dump_file, current_function_decl, dump_flags);
1933 return initializing_dump;
1935 else
1936 return false;
1939 /* Flush PASS dump file. */
1940 /* This is non-static so that plugins can use it. */
1942 void
1943 pass_fini_dump_file (struct opt_pass *pass)
1945 /* Flush and close dump file. */
1946 if (dump_file_name)
1948 free (CONST_CAST (char *, dump_file_name));
1949 dump_file_name = NULL;
1952 if (dump_file)
1954 dump_end (pass->static_pass_number, dump_file);
1955 dump_file = NULL;
1959 /* After executing the pass, apply expected changes to the function
1960 properties. */
1962 static void
1963 update_properties_after_pass (void *data)
1965 struct opt_pass *pass = (struct opt_pass *) data;
1966 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1967 & ~pass->properties_destroyed;
1970 /* Execute summary generation for all of the passes in IPA_PASS. */
1972 void
1973 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1975 while (ipa_pass)
1977 struct opt_pass *pass = &ipa_pass->pass;
1979 /* Execute all of the IPA_PASSes in the list. */
1980 if (ipa_pass->pass.type == IPA_PASS
1981 && (!pass->gate || pass->gate ())
1982 && ipa_pass->generate_summary)
1984 pass_init_dump_file (pass);
1986 /* If a timevar is present, start it. */
1987 if (pass->tv_id)
1988 timevar_push (pass->tv_id);
1990 ipa_pass->generate_summary ();
1992 /* Stop timevar. */
1993 if (pass->tv_id)
1994 timevar_pop (pass->tv_id);
1996 pass_fini_dump_file (pass);
1998 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
2002 /* Execute IPA_PASS function transform on NODE. */
2004 static void
2005 execute_one_ipa_transform_pass (struct cgraph_node *node,
2006 struct ipa_opt_pass_d *ipa_pass)
2008 struct opt_pass *pass = &ipa_pass->pass;
2009 unsigned int todo_after = 0;
2011 current_pass = pass;
2012 if (!ipa_pass->function_transform)
2013 return;
2015 /* Note that the folders should only create gimple expressions.
2016 This is a hack until the new folder is ready. */
2017 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2019 pass_init_dump_file (pass);
2021 /* Run pre-pass verification. */
2022 execute_todo (ipa_pass->function_transform_todo_flags_start);
2024 /* If a timevar is present, start it. */
2025 if (pass->tv_id != TV_NONE)
2026 timevar_push (pass->tv_id);
2028 /* Do it! */
2029 todo_after = ipa_pass->function_transform (node);
2031 /* Stop timevar. */
2032 if (pass->tv_id != TV_NONE)
2033 timevar_pop (pass->tv_id);
2035 /* Run post-pass cleanup and verification. */
2036 execute_todo (todo_after);
2037 verify_interpass_invariants ();
2039 do_per_function (execute_function_dump, NULL);
2040 pass_fini_dump_file (pass);
2042 current_pass = NULL;
2045 /* For the current function, execute all ipa transforms. */
2047 void
2048 execute_all_ipa_transforms (void)
2050 struct cgraph_node *node;
2051 if (!cfun)
2052 return;
2053 node = cgraph_get_node (current_function_decl);
2055 if (node->ipa_transforms_to_apply)
2057 unsigned int i;
2059 for (i = 0; i < VEC_length (ipa_opt_pass, node->ipa_transforms_to_apply);
2060 i++)
2061 execute_one_ipa_transform_pass (node,
2062 VEC_index (ipa_opt_pass,
2063 node->ipa_transforms_to_apply,
2064 i));
2065 VEC_free (ipa_opt_pass, heap, node->ipa_transforms_to_apply);
2066 node->ipa_transforms_to_apply = NULL;
2070 /* Callback for do_per_function to apply all IPA transforms. */
2072 static void
2073 apply_ipa_transforms (void *data)
2075 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2076 if (!node->global.inlined_to && node->ipa_transforms_to_apply)
2078 *(bool *)data = true;
2079 execute_all_ipa_transforms();
2080 rebuild_cgraph_edges ();
2084 /* Check if PASS is explicitly disabled or enabled and return
2085 the gate status. FUNC is the function to be processed, and
2086 GATE_STATUS is the gate status determined by pass manager by
2087 default. */
2089 static bool
2090 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2092 bool explicitly_enabled = false;
2093 bool explicitly_disabled = false;
2095 explicitly_enabled
2096 = is_pass_explicitly_enabled_or_disabled (pass, func,
2097 enabled_pass_uid_range_tab);
2098 explicitly_disabled
2099 = is_pass_explicitly_enabled_or_disabled (pass, func,
2100 disabled_pass_uid_range_tab);
2102 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2104 return gate_status;
2108 /* Execute PASS. */
2110 bool
2111 execute_one_pass (struct opt_pass *pass)
2113 bool initializing_dump;
2114 unsigned int todo_after = 0;
2116 bool gate_status;
2118 /* IPA passes are executed on whole program, so cfun should be NULL.
2119 Other passes need function context set. */
2120 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2121 gcc_assert (!cfun && !current_function_decl);
2122 else
2123 gcc_assert (cfun && current_function_decl);
2125 current_pass = pass;
2127 /* Check whether gate check should be avoided.
2128 User controls the value of the gate through the parameter "gate_status". */
2129 gate_status = (pass->gate == NULL) ? true : pass->gate();
2130 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2132 /* Override gate with plugin. */
2133 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2135 if (!gate_status)
2137 current_pass = NULL;
2138 return false;
2141 /* Pass execution event trigger: useful to identify passes being
2142 executed. */
2143 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2145 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2146 Apply all trnasforms first. */
2147 if (pass->type == SIMPLE_IPA_PASS)
2149 bool applied = false;
2150 do_per_function (apply_ipa_transforms, (void *)&applied);
2151 if (applied)
2152 cgraph_remove_unreachable_nodes (true, dump_file);
2153 /* Restore current_pass. */
2154 current_pass = pass;
2157 if (!quiet_flag && !cfun)
2158 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2160 /* Note that the folders should only create gimple expressions.
2161 This is a hack until the new folder is ready. */
2162 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2164 initializing_dump = pass_init_dump_file (pass);
2166 /* Run pre-pass verification. */
2167 execute_todo (pass->todo_flags_start);
2169 #ifdef ENABLE_CHECKING
2170 do_per_function (verify_curr_properties,
2171 (void *)(size_t)pass->properties_required);
2172 #endif
2174 /* If a timevar is present, start it. */
2175 if (pass->tv_id != TV_NONE)
2176 timevar_push (pass->tv_id);
2178 /* Do it! */
2179 if (pass->execute)
2181 todo_after = pass->execute ();
2182 do_per_function (clear_last_verified, NULL);
2185 /* Stop timevar. */
2186 if (pass->tv_id != TV_NONE)
2187 timevar_pop (pass->tv_id);
2189 do_per_function (update_properties_after_pass, pass);
2191 if (initializing_dump
2192 && dump_file
2193 && graph_dump_format != no_graph
2194 && cfun
2195 && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
2196 == (PROP_cfg | PROP_rtl))
2198 get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
2199 dump_flags |= TDF_GRAPH;
2200 clean_graph_dump_file (dump_file_name);
2203 /* Run post-pass cleanup and verification. */
2204 execute_todo (todo_after | pass->todo_flags_finish);
2205 verify_interpass_invariants ();
2206 do_per_function (execute_function_dump, NULL);
2207 if (pass->type == IPA_PASS)
2209 struct cgraph_node *node;
2210 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2211 VEC_safe_push (ipa_opt_pass, heap, node->ipa_transforms_to_apply,
2212 (struct ipa_opt_pass_d *)pass);
2215 if (!current_function_decl)
2216 cgraph_process_new_functions ();
2218 pass_fini_dump_file (pass);
2220 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2221 gcc_assert (!(cfun->curr_properties & PROP_trees)
2222 || pass->type != RTL_PASS);
2224 current_pass = NULL;
2226 return true;
2229 void
2230 execute_pass_list (struct opt_pass *pass)
2234 gcc_assert (pass->type == GIMPLE_PASS
2235 || pass->type == RTL_PASS);
2236 if (execute_one_pass (pass) && pass->sub)
2237 execute_pass_list (pass->sub);
2238 pass = pass->next;
2240 while (pass);
2243 /* Same as execute_pass_list but assume that subpasses of IPA passes
2244 are local passes. If SET is not NULL, write out summaries of only
2245 those node in SET. */
2247 static void
2248 ipa_write_summaries_2 (struct opt_pass *pass, cgraph_node_set set,
2249 varpool_node_set vset,
2250 struct lto_out_decl_state *state)
2252 while (pass)
2254 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2255 gcc_assert (!current_function_decl);
2256 gcc_assert (!cfun);
2257 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2258 if (pass->type == IPA_PASS
2259 && ipa_pass->write_summary
2260 && (!pass->gate || pass->gate ()))
2262 /* If a timevar is present, start it. */
2263 if (pass->tv_id)
2264 timevar_push (pass->tv_id);
2266 pass_init_dump_file (pass);
2268 ipa_pass->write_summary (set,vset);
2270 pass_fini_dump_file (pass);
2272 /* If a timevar is present, start it. */
2273 if (pass->tv_id)
2274 timevar_pop (pass->tv_id);
2277 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2278 ipa_write_summaries_2 (pass->sub, set, vset, state);
2280 pass = pass->next;
2284 /* Helper function of ipa_write_summaries. Creates and destroys the
2285 decl state and calls ipa_write_summaries_2 for all passes that have
2286 summaries. SET is the set of nodes to be written. */
2288 static void
2289 ipa_write_summaries_1 (cgraph_node_set set, varpool_node_set vset)
2291 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2292 compute_ltrans_boundary (state, set, vset);
2294 lto_push_out_decl_state (state);
2296 gcc_assert (!flag_wpa);
2297 ipa_write_summaries_2 (all_regular_ipa_passes, set, vset, state);
2298 ipa_write_summaries_2 (all_lto_gen_passes, set, vset, state);
2300 gcc_assert (lto_get_out_decl_state () == state);
2301 lto_pop_out_decl_state ();
2302 lto_delete_out_decl_state (state);
2305 /* Write out summaries for all the nodes in the callgraph. */
2307 void
2308 ipa_write_summaries (void)
2310 cgraph_node_set set;
2311 varpool_node_set vset;
2312 struct cgraph_node **order;
2313 struct varpool_node *vnode;
2314 int i, order_pos;
2316 if (!flag_generate_lto || seen_error ())
2317 return;
2319 set = cgraph_node_set_new ();
2321 /* Create the callgraph set in the same order used in
2322 cgraph_expand_all_functions. This mostly facilitates debugging,
2323 since it causes the gimple file to be processed in the same order
2324 as the source code. */
2325 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2326 order_pos = ipa_reverse_postorder (order);
2327 gcc_assert (order_pos == cgraph_n_nodes);
2329 for (i = order_pos - 1; i >= 0; i--)
2331 struct cgraph_node *node = order[i];
2333 if (cgraph_function_with_gimple_body_p (node))
2335 /* When streaming out references to statements as part of some IPA
2336 pass summary, the statements need to have uids assigned and the
2337 following does that for all the IPA passes here. Naturally, this
2338 ordering then matches the one IPA-passes get in their stmt_fixup
2339 hooks. */
2341 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2342 renumber_gimple_stmt_uids ();
2343 pop_cfun ();
2345 if (node->analyzed)
2346 cgraph_node_set_add (set, node);
2348 vset = varpool_node_set_new ();
2350 FOR_EACH_DEFINED_VARIABLE (vnode)
2351 if ((!vnode->alias || vnode->alias_of))
2352 varpool_node_set_add (vset, vnode);
2354 ipa_write_summaries_1 (set, vset);
2356 free (order);
2357 free_cgraph_node_set (set);
2358 free_varpool_node_set (vset);
2361 /* Same as execute_pass_list but assume that subpasses of IPA passes
2362 are local passes. If SET is not NULL, write out optimization summaries of
2363 only those node in SET. */
2365 static void
2366 ipa_write_optimization_summaries_1 (struct opt_pass *pass, cgraph_node_set set,
2367 varpool_node_set vset,
2368 struct lto_out_decl_state *state)
2370 while (pass)
2372 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2373 gcc_assert (!current_function_decl);
2374 gcc_assert (!cfun);
2375 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2376 if (pass->type == IPA_PASS
2377 && ipa_pass->write_optimization_summary
2378 && (!pass->gate || pass->gate ()))
2380 /* If a timevar is present, start it. */
2381 if (pass->tv_id)
2382 timevar_push (pass->tv_id);
2384 pass_init_dump_file (pass);
2386 ipa_pass->write_optimization_summary (set, vset);
2388 pass_fini_dump_file (pass);
2390 /* If a timevar is present, start it. */
2391 if (pass->tv_id)
2392 timevar_pop (pass->tv_id);
2395 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2396 ipa_write_optimization_summaries_1 (pass->sub, set, vset, state);
2398 pass = pass->next;
2402 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2403 NULL, write out all summaries of all nodes. */
2405 void
2406 ipa_write_optimization_summaries (cgraph_node_set set, varpool_node_set vset)
2408 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2409 cgraph_node_set_iterator csi;
2410 compute_ltrans_boundary (state, set, vset);
2412 lto_push_out_decl_state (state);
2413 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2415 struct cgraph_node *node = csi_node (csi);
2416 /* When streaming out references to statements as part of some IPA
2417 pass summary, the statements need to have uids assigned.
2419 For functions newly born at WPA stage we need to initialize
2420 the uids here. */
2421 if (node->analyzed
2422 && gimple_has_body_p (node->symbol.decl))
2424 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2425 renumber_gimple_stmt_uids ();
2426 pop_cfun ();
2430 gcc_assert (flag_wpa);
2431 ipa_write_optimization_summaries_1 (all_regular_ipa_passes, set, vset, state);
2432 ipa_write_optimization_summaries_1 (all_lto_gen_passes, set, vset, state);
2434 gcc_assert (lto_get_out_decl_state () == state);
2435 lto_pop_out_decl_state ();
2436 lto_delete_out_decl_state (state);
2439 /* Same as execute_pass_list but assume that subpasses of IPA passes
2440 are local passes. */
2442 static void
2443 ipa_read_summaries_1 (struct opt_pass *pass)
2445 while (pass)
2447 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2449 gcc_assert (!current_function_decl);
2450 gcc_assert (!cfun);
2451 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2453 if (pass->gate == NULL || pass->gate ())
2455 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2457 /* If a timevar is present, start it. */
2458 if (pass->tv_id)
2459 timevar_push (pass->tv_id);
2461 pass_init_dump_file (pass);
2463 ipa_pass->read_summary ();
2465 pass_fini_dump_file (pass);
2467 /* Stop timevar. */
2468 if (pass->tv_id)
2469 timevar_pop (pass->tv_id);
2472 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2473 ipa_read_summaries_1 (pass->sub);
2475 pass = pass->next;
2480 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2482 void
2483 ipa_read_summaries (void)
2485 ipa_read_summaries_1 (all_regular_ipa_passes);
2486 ipa_read_summaries_1 (all_lto_gen_passes);
2489 /* Same as execute_pass_list but assume that subpasses of IPA passes
2490 are local passes. */
2492 static void
2493 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2495 while (pass)
2497 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2499 gcc_assert (!current_function_decl);
2500 gcc_assert (!cfun);
2501 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2503 if (pass->gate == NULL || pass->gate ())
2505 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2507 /* If a timevar is present, start it. */
2508 if (pass->tv_id)
2509 timevar_push (pass->tv_id);
2511 pass_init_dump_file (pass);
2513 ipa_pass->read_optimization_summary ();
2515 pass_fini_dump_file (pass);
2517 /* Stop timevar. */
2518 if (pass->tv_id)
2519 timevar_pop (pass->tv_id);
2522 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2523 ipa_read_optimization_summaries_1 (pass->sub);
2525 pass = pass->next;
2529 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2531 void
2532 ipa_read_optimization_summaries (void)
2534 ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
2535 ipa_read_optimization_summaries_1 (all_lto_gen_passes);
2538 /* Same as execute_pass_list but assume that subpasses of IPA passes
2539 are local passes. */
2540 void
2541 execute_ipa_pass_list (struct opt_pass *pass)
2545 gcc_assert (!current_function_decl);
2546 gcc_assert (!cfun);
2547 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2548 if (execute_one_pass (pass) && pass->sub)
2550 if (pass->sub->type == GIMPLE_PASS)
2552 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2553 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2554 pass->sub);
2555 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2557 else if (pass->sub->type == SIMPLE_IPA_PASS
2558 || pass->sub->type == IPA_PASS)
2559 execute_ipa_pass_list (pass->sub);
2560 else
2561 gcc_unreachable ();
2563 gcc_assert (!current_function_decl);
2564 cgraph_process_new_functions ();
2565 pass = pass->next;
2567 while (pass);
2570 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2572 static void
2573 execute_ipa_stmt_fixups (struct opt_pass *pass,
2574 struct cgraph_node *node, gimple *stmts)
2576 while (pass)
2578 /* Execute all of the IPA_PASSes in the list. */
2579 if (pass->type == IPA_PASS
2580 && (!pass->gate || pass->gate ()))
2582 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2584 if (ipa_pass->stmt_fixup)
2586 pass_init_dump_file (pass);
2587 /* If a timevar is present, start it. */
2588 if (pass->tv_id)
2589 timevar_push (pass->tv_id);
2591 ipa_pass->stmt_fixup (node, stmts);
2593 /* Stop timevar. */
2594 if (pass->tv_id)
2595 timevar_pop (pass->tv_id);
2596 pass_fini_dump_file (pass);
2598 if (pass->sub)
2599 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2601 pass = pass->next;
2605 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2607 void
2608 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2610 execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
2614 extern void debug_properties (unsigned int);
2615 extern void dump_properties (FILE *, unsigned int);
2617 DEBUG_FUNCTION void
2618 dump_properties (FILE *dump, unsigned int props)
2620 fprintf (dump, "Properties:\n");
2621 if (props & PROP_gimple_any)
2622 fprintf (dump, "PROP_gimple_any\n");
2623 if (props & PROP_gimple_lcf)
2624 fprintf (dump, "PROP_gimple_lcf\n");
2625 if (props & PROP_gimple_leh)
2626 fprintf (dump, "PROP_gimple_leh\n");
2627 if (props & PROP_cfg)
2628 fprintf (dump, "PROP_cfg\n");
2629 if (props & PROP_referenced_vars)
2630 fprintf (dump, "PROP_referenced_vars\n");
2631 if (props & PROP_ssa)
2632 fprintf (dump, "PROP_ssa\n");
2633 if (props & PROP_no_crit_edges)
2634 fprintf (dump, "PROP_no_crit_edges\n");
2635 if (props & PROP_rtl)
2636 fprintf (dump, "PROP_rtl\n");
2637 if (props & PROP_gimple_lomp)
2638 fprintf (dump, "PROP_gimple_lomp\n");
2639 if (props & PROP_gimple_lcx)
2640 fprintf (dump, "PROP_gimple_lcx\n");
2641 if (props & PROP_cfglayout)
2642 fprintf (dump, "PROP_cfglayout\n");
2645 DEBUG_FUNCTION void
2646 debug_properties (unsigned int props)
2648 dump_properties (stderr, props);
2651 /* Called by local passes to see if function is called by already processed nodes.
2652 Because we process nodes in topological order, this means that function is
2653 in recursive cycle or we introduced new direct calls. */
2654 bool
2655 function_called_by_processed_nodes_p (void)
2657 struct cgraph_edge *e;
2658 for (e = cgraph_get_node (current_function_decl)->callers;
2660 e = e->next_caller)
2662 if (e->caller->symbol.decl == current_function_decl)
2663 continue;
2664 if (!cgraph_function_with_gimple_body_p (e->caller))
2665 continue;
2666 if (TREE_ASM_WRITTEN (e->caller->symbol.decl))
2667 continue;
2668 if (!e->caller->process && !e->caller->global.inlined_to)
2669 break;
2671 if (dump_file && e)
2673 fprintf (dump_file, "Already processed call to:\n");
2674 dump_cgraph_node (dump_file, e->caller);
2676 return e != NULL;
2679 #include "gt-passes.h"