* Tippo
[official-gcc.git] / gcc / passes.c
blobba4e98e068276b430a4e0ebb1a81da889de8deff
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 && !optimize_debug;
343 static struct gimple_opt_pass pass_all_optimizations =
346 GIMPLE_PASS,
347 "*all_optimizations", /* name */
348 gate_all_optimizations, /* gate */
349 NULL, /* execute */
350 NULL, /* sub */
351 NULL, /* next */
352 0, /* static_pass_number */
353 TV_OPTIMIZE, /* tv_id */
354 0, /* properties_required */
355 0, /* properties_provided */
356 0, /* properties_destroyed */
357 0, /* todo_flags_start */
358 0 /* todo_flags_finish */
362 /* Gate: execute, or not, all of the non-trivial optimizations. */
364 static bool
365 gate_all_optimizations_g (void)
367 return optimize >= 1 && optimize_debug;
370 static struct gimple_opt_pass pass_all_optimizations_g =
373 GIMPLE_PASS,
374 "*all_optimizations_g", /* name */
375 gate_all_optimizations_g, /* gate */
376 NULL, /* execute */
377 NULL, /* sub */
378 NULL, /* next */
379 0, /* static_pass_number */
380 TV_OPTIMIZE, /* tv_id */
381 0, /* properties_required */
382 0, /* properties_provided */
383 0, /* properties_destroyed */
384 0, /* todo_flags_start */
385 0 /* todo_flags_finish */
389 static bool
390 gate_rest_of_compilation (void)
392 /* Early return if there were errors. We can run afoul of our
393 consistency checks, and there's not really much point in fixing them. */
394 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
397 static struct rtl_opt_pass pass_rest_of_compilation =
400 RTL_PASS,
401 "*rest_of_compilation", /* name */
402 gate_rest_of_compilation, /* gate */
403 NULL, /* execute */
404 NULL, /* sub */
405 NULL, /* next */
406 0, /* static_pass_number */
407 TV_REST_OF_COMPILATION, /* tv_id */
408 PROP_rtl, /* properties_required */
409 0, /* properties_provided */
410 0, /* properties_destroyed */
411 0, /* todo_flags_start */
412 TODO_ggc_collect /* todo_flags_finish */
416 static bool
417 gate_postreload (void)
419 return reload_completed;
422 static struct rtl_opt_pass pass_postreload =
425 RTL_PASS,
426 "*all-postreload", /* name */
427 gate_postreload, /* gate */
428 NULL, /* execute */
429 NULL, /* sub */
430 NULL, /* next */
431 0, /* static_pass_number */
432 TV_POSTRELOAD, /* tv_id */
433 PROP_rtl, /* properties_required */
434 0, /* properties_provided */
435 0, /* properties_destroyed */
436 0, /* todo_flags_start */
437 TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
443 /* The root of the compilation pass tree, once constructed. */
444 struct opt_pass *all_passes, *all_small_ipa_passes, *all_lowering_passes,
445 *all_regular_ipa_passes, *all_late_ipa_passes, *all_lto_gen_passes;
447 /* This is used by plugins, and should also be used in register_pass. */
448 #define DEF_PASS_LIST(LIST) &LIST,
449 struct opt_pass **gcc_pass_lists[] = { GCC_PASS_LISTS NULL };
450 #undef DEF_PASS_LIST
452 /* A map from static pass id to optimization pass. */
453 struct opt_pass **passes_by_id;
454 int passes_by_id_size;
456 /* Set the static pass number of pass PASS to ID and record that
457 in the mapping from static pass number to pass. */
459 static void
460 set_pass_for_id (int id, struct opt_pass *pass)
462 pass->static_pass_number = id;
463 if (passes_by_id_size <= id)
465 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
466 memset (passes_by_id + passes_by_id_size, 0,
467 (id + 1 - passes_by_id_size) * sizeof (void *));
468 passes_by_id_size = id + 1;
470 passes_by_id[id] = pass;
473 /* Return the pass with the static pass number ID. */
475 struct opt_pass *
476 get_pass_for_id (int id)
478 if (id >= passes_by_id_size)
479 return NULL;
480 return passes_by_id[id];
483 /* Iterate over the pass tree allocating dump file numbers. We want
484 to do this depth first, and independent of whether the pass is
485 enabled or not. */
487 void
488 register_one_dump_file (struct opt_pass *pass)
490 char *dot_name, *flag_name, *glob_name;
491 const char *name, *full_name, *prefix;
492 char num[10];
493 int flags, id;
495 /* See below in next_pass_1. */
496 num[0] = '\0';
497 if (pass->static_pass_number != -1)
498 sprintf (num, "%d", ((int) pass->static_pass_number < 0
499 ? 1 : pass->static_pass_number));
501 /* The name is both used to identify the pass for the purposes of plugins,
502 and to specify dump file name and option.
503 The latter two might want something short which is not quite unique; for
504 that reason, we may have a disambiguating prefix, followed by a space
505 to mark the start of the following dump file name / option string. */
506 name = strchr (pass->name, ' ');
507 name = name ? name + 1 : pass->name;
508 dot_name = concat (".", name, num, NULL);
509 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
510 prefix = "ipa-", flags = TDF_IPA;
511 else if (pass->type == GIMPLE_PASS)
512 prefix = "tree-", flags = TDF_TREE;
513 else
514 prefix = "rtl-", flags = TDF_RTL;
516 flag_name = concat (prefix, name, num, NULL);
517 glob_name = concat (prefix, name, NULL);
518 id = dump_register (dot_name, flag_name, glob_name, flags);
519 set_pass_for_id (id, pass);
520 full_name = concat (prefix, pass->name, num, NULL);
521 register_pass_name (pass, full_name);
522 free (CONST_CAST (char *, full_name));
525 /* Recursive worker function for register_dump_files. */
527 static int
528 register_dump_files_1 (struct opt_pass *pass, int properties)
532 int new_properties = (properties | pass->properties_provided)
533 & ~pass->properties_destroyed;
535 if (pass->name && pass->name[0] != '*')
536 register_one_dump_file (pass);
538 if (pass->sub)
539 new_properties = register_dump_files_1 (pass->sub, new_properties);
541 /* If we have a gate, combine the properties that we could have with
542 and without the pass being examined. */
543 if (pass->gate)
544 properties &= new_properties;
545 else
546 properties = new_properties;
548 pass = pass->next;
550 while (pass);
552 return properties;
555 /* Register the dump files for the pipeline starting at PASS.
556 PROPERTIES reflects the properties that are guaranteed to be available at
557 the beginning of the pipeline. */
559 static void
560 register_dump_files (struct opt_pass *pass,int properties)
562 pass->properties_required |= properties;
563 register_dump_files_1 (pass, properties);
566 struct pass_registry
568 const char* unique_name;
569 struct opt_pass *pass;
572 /* Pass registry hash function. */
574 static hashval_t
575 passr_hash (const void *p)
577 const struct pass_registry *const s = (const struct pass_registry *const) p;
578 return htab_hash_string (s->unique_name);
581 /* Hash equal function */
583 static int
584 passr_eq (const void *p1, const void *p2)
586 const struct pass_registry *const s1 = (const struct pass_registry *const) p1;
587 const struct pass_registry *const s2 = (const struct pass_registry *const) p2;
589 return !strcmp (s1->unique_name, s2->unique_name);
592 static htab_t name_to_pass_map = NULL;
594 /* Register PASS with NAME. */
596 static void
597 register_pass_name (struct opt_pass *pass, const char *name)
599 struct pass_registry **slot;
600 struct pass_registry pr;
602 if (!name_to_pass_map)
603 name_to_pass_map = htab_create (256, passr_hash, passr_eq, NULL);
605 pr.unique_name = name;
606 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map, &pr, INSERT);
607 if (!*slot)
609 struct pass_registry *new_pr;
611 new_pr = XCNEW (struct pass_registry);
612 new_pr->unique_name = xstrdup (name);
613 new_pr->pass = pass;
614 *slot = new_pr;
616 else
617 return; /* Ignore plugin passes. */
620 /* Map from pass id to canonicalized pass name. */
622 typedef const char *char_ptr;
623 DEF_VEC_P(char_ptr);
624 DEF_VEC_ALLOC_P(char_ptr, heap);
625 static VEC(char_ptr, heap) *pass_tab = NULL;
627 /* Callback function for traversing NAME_TO_PASS_MAP. */
629 static int
630 pass_traverse (void **slot, void *data ATTRIBUTE_UNUSED)
632 struct pass_registry **p = (struct pass_registry **)slot;
633 struct opt_pass *pass = (*p)->pass;
635 gcc_assert (pass->static_pass_number > 0);
636 gcc_assert (pass_tab);
638 VEC_replace (char_ptr, pass_tab, pass->static_pass_number,
639 (*p)->unique_name);
641 return 1;
644 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
645 table for dumping purpose. */
647 static void
648 create_pass_tab (void)
650 if (!flag_dump_passes)
651 return;
653 VEC_safe_grow_cleared (char_ptr, heap,
654 pass_tab, passes_by_id_size + 1);
655 htab_traverse (name_to_pass_map, pass_traverse, NULL);
658 static bool override_gate_status (struct opt_pass *, tree, bool);
660 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
661 is turned on or not. */
663 static void
664 dump_one_pass (struct opt_pass *pass, int pass_indent)
666 int indent = 3 * pass_indent;
667 const char *pn;
668 bool is_on, is_really_on;
670 is_on = (pass->gate == NULL) ? true : pass->gate();
671 is_really_on = override_gate_status (pass, current_function_decl, is_on);
673 if (pass->static_pass_number <= 0)
674 pn = pass->name;
675 else
676 pn = VEC_index (char_ptr, pass_tab, pass->static_pass_number);
678 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
679 (15 - indent < 0 ? 0 : 15 - indent), " ",
680 is_on ? " ON" : " OFF",
681 ((!is_on) == (!is_really_on) ? ""
682 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
685 /* Dump pass list PASS with indentation INDENT. */
687 static void
688 dump_pass_list (struct opt_pass *pass, int indent)
692 dump_one_pass (pass, indent);
693 if (pass->sub)
694 dump_pass_list (pass->sub, indent + 1);
695 pass = pass->next;
697 while (pass);
700 /* Dump all optimization passes. */
702 void
703 dump_passes (void)
705 struct cgraph_node *n, *node = NULL;
707 create_pass_tab();
709 FOR_EACH_DEFINED_FUNCTION (n)
710 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
712 node = n;
713 break;
716 if (!node)
717 return;
719 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
721 dump_pass_list (all_lowering_passes, 1);
722 dump_pass_list (all_small_ipa_passes, 1);
723 dump_pass_list (all_regular_ipa_passes, 1);
724 dump_pass_list (all_lto_gen_passes, 1);
725 dump_pass_list (all_late_ipa_passes, 1);
726 dump_pass_list (all_passes, 1);
728 pop_cfun ();
732 /* Returns the pass with NAME. */
734 static struct opt_pass *
735 get_pass_by_name (const char *name)
737 struct pass_registry **slot, pr;
739 pr.unique_name = name;
740 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map,
741 &pr, NO_INSERT);
743 if (!slot || !*slot)
744 return NULL;
746 return (*slot)->pass;
750 /* Range [start, last]. */
752 struct uid_range
754 unsigned int start;
755 unsigned int last;
756 const char *assem_name;
757 struct uid_range *next;
760 typedef struct uid_range *uid_range_p;
762 DEF_VEC_P(uid_range_p);
763 DEF_VEC_ALLOC_P(uid_range_p, heap);
765 static VEC(uid_range_p, heap) *enabled_pass_uid_range_tab = NULL;
766 static VEC(uid_range_p, heap) *disabled_pass_uid_range_tab = NULL;
769 /* Parse option string for -fdisable- and -fenable-
770 The syntax of the options:
772 -fenable-<pass_name>
773 -fdisable-<pass_name>
775 -fenable-<pass_name>=s1:e1,s2:e2,...
776 -fdisable-<pass_name>=s1:e1,s2:e2,...
779 static void
780 enable_disable_pass (const char *arg, bool is_enable)
782 struct opt_pass *pass;
783 char *range_str, *phase_name;
784 char *argstr = xstrdup (arg);
785 VEC(uid_range_p, heap) **tab = 0;
787 range_str = strchr (argstr,'=');
788 if (range_str)
790 *range_str = '\0';
791 range_str++;
794 phase_name = argstr;
795 if (!*phase_name)
797 if (is_enable)
798 error ("unrecognized option -fenable");
799 else
800 error ("unrecognized option -fdisable");
801 free (argstr);
802 return;
804 pass = get_pass_by_name (phase_name);
805 if (!pass || pass->static_pass_number == -1)
807 if (is_enable)
808 error ("unknown pass %s specified in -fenable", phase_name);
809 else
810 error ("unknown pass %s specified in -fdisable", phase_name);
811 free (argstr);
812 return;
815 if (is_enable)
816 tab = &enabled_pass_uid_range_tab;
817 else
818 tab = &disabled_pass_uid_range_tab;
820 if ((unsigned) pass->static_pass_number >= VEC_length (uid_range_p, *tab))
821 VEC_safe_grow_cleared (uid_range_p, heap,
822 *tab, pass->static_pass_number + 1);
824 if (!range_str)
826 uid_range_p slot;
827 uid_range_p new_range = XCNEW (struct uid_range);
829 new_range->start = 0;
830 new_range->last = (unsigned)-1;
832 slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
833 new_range->next = slot;
834 VEC_replace (uid_range_p, *tab, pass->static_pass_number,
835 new_range);
836 if (is_enable)
837 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
838 "of [%u, %u]", phase_name, new_range->start, new_range->last);
839 else
840 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
841 "of [%u, %u]", phase_name, new_range->start, new_range->last);
843 else
845 char *next_range = NULL;
846 char *one_range = range_str;
847 char *end_val = NULL;
851 uid_range_p slot;
852 uid_range_p new_range;
853 char *invalid = NULL;
854 long start;
855 char *func_name = NULL;
857 next_range = strchr (one_range, ',');
858 if (next_range)
860 *next_range = '\0';
861 next_range++;
864 end_val = strchr (one_range, ':');
865 if (end_val)
867 *end_val = '\0';
868 end_val++;
870 start = strtol (one_range, &invalid, 10);
871 if (*invalid || start < 0)
873 if (end_val || (one_range[0] >= '0'
874 && one_range[0] <= '9'))
876 error ("Invalid range %s in option %s",
877 one_range,
878 is_enable ? "-fenable" : "-fdisable");
879 free (argstr);
880 return;
882 func_name = one_range;
884 if (!end_val)
886 new_range = XCNEW (struct uid_range);
887 if (!func_name)
889 new_range->start = (unsigned) start;
890 new_range->last = (unsigned) start;
892 else
894 new_range->start = (unsigned) -1;
895 new_range->last = (unsigned) -1;
896 new_range->assem_name = xstrdup (func_name);
899 else
901 long last = strtol (end_val, &invalid, 10);
902 if (*invalid || last < start)
904 error ("Invalid range %s in option %s",
905 end_val,
906 is_enable ? "-fenable" : "-fdisable");
907 free (argstr);
908 return;
910 new_range = XCNEW (struct uid_range);
911 new_range->start = (unsigned) start;
912 new_range->last = (unsigned) last;
915 slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
916 new_range->next = slot;
917 VEC_replace (uid_range_p, *tab, pass->static_pass_number,
918 new_range);
919 if (is_enable)
921 if (new_range->assem_name)
922 inform (UNKNOWN_LOCATION,
923 "enable pass %s for function %s",
924 phase_name, new_range->assem_name);
925 else
926 inform (UNKNOWN_LOCATION,
927 "enable pass %s for functions in the range of [%u, %u]",
928 phase_name, new_range->start, new_range->last);
930 else
932 if (new_range->assem_name)
933 inform (UNKNOWN_LOCATION,
934 "disable pass %s for function %s",
935 phase_name, new_range->assem_name);
936 else
937 inform (UNKNOWN_LOCATION,
938 "disable pass %s for functions in the range of [%u, %u]",
939 phase_name, new_range->start, new_range->last);
942 one_range = next_range;
943 } while (next_range);
946 free (argstr);
949 /* Enable pass specified by ARG. */
951 void
952 enable_pass (const char *arg)
954 enable_disable_pass (arg, true);
957 /* Disable pass specified by ARG. */
959 void
960 disable_pass (const char *arg)
962 enable_disable_pass (arg, false);
965 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
967 static bool
968 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
969 tree func,
970 VEC(uid_range_p, heap) *tab)
972 uid_range_p slot, range;
973 int cgraph_uid;
974 const char *aname = NULL;
976 if (!tab
977 || (unsigned) pass->static_pass_number >= VEC_length (uid_range_p, tab)
978 || pass->static_pass_number == -1)
979 return false;
981 slot = VEC_index (uid_range_p, tab, pass->static_pass_number);
982 if (!slot)
983 return false;
985 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
986 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
987 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
989 range = slot;
990 while (range)
992 if ((unsigned) cgraph_uid >= range->start
993 && (unsigned) cgraph_uid <= range->last)
994 return true;
995 if (range->assem_name && aname
996 && !strcmp (range->assem_name, aname))
997 return true;
998 range = range->next;
1001 return false;
1004 /* Look at the static_pass_number and duplicate the pass
1005 if it is already added to a list. */
1007 static struct opt_pass *
1008 make_pass_instance (struct opt_pass *pass, bool track_duplicates)
1010 /* A nonzero static_pass_number indicates that the
1011 pass is already in the list. */
1012 if (pass->static_pass_number)
1014 struct opt_pass *new_pass;
1016 if (pass->type == GIMPLE_PASS
1017 || pass->type == RTL_PASS
1018 || pass->type == SIMPLE_IPA_PASS)
1020 new_pass = XNEW (struct opt_pass);
1021 memcpy (new_pass, pass, sizeof (struct opt_pass));
1023 else if (pass->type == IPA_PASS)
1025 new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
1026 memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
1028 else
1029 gcc_unreachable ();
1031 new_pass->next = NULL;
1033 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1035 /* Indicate to register_dump_files that this pass has duplicates,
1036 and so it should rename the dump file. The first instance will
1037 be -1, and be number of duplicates = -static_pass_number - 1.
1038 Subsequent instances will be > 0 and just the duplicate number. */
1039 if ((pass->name && pass->name[0] != '*') || track_duplicates)
1041 pass->static_pass_number -= 1;
1042 new_pass->static_pass_number = -pass->static_pass_number;
1044 return new_pass;
1046 else
1048 pass->todo_flags_start |= TODO_mark_first_instance;
1049 pass->static_pass_number = -1;
1051 invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
1053 return pass;
1056 /* Add a pass to the pass list. Duplicate the pass if it's already
1057 in the list. */
1059 static struct opt_pass **
1060 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
1062 /* Every pass should have a name so that plugins can refer to them. */
1063 gcc_assert (pass->name != NULL);
1065 *list = make_pass_instance (pass, false);
1067 return &(*list)->next;
1070 /* List node for an inserted pass instance. We need to keep track of all
1071 the newly-added pass instances (with 'added_pass_nodes' defined below)
1072 so that we can register their dump files after pass-positioning is finished.
1073 Registering dumping files needs to be post-processed or the
1074 static_pass_number of the opt_pass object would be modified and mess up
1075 the dump file names of future pass instances to be added. */
1077 struct pass_list_node
1079 struct opt_pass *pass;
1080 struct pass_list_node *next;
1083 static struct pass_list_node *added_pass_nodes = NULL;
1084 static struct pass_list_node *prev_added_pass_node;
1086 /* Insert the pass at the proper position. Return true if the pass
1087 is successfully added.
1089 NEW_PASS_INFO - new pass to be inserted
1090 PASS_LIST - root of the pass list to insert the new pass to */
1092 static bool
1093 position_pass (struct register_pass_info *new_pass_info,
1094 struct opt_pass **pass_list)
1096 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1097 bool success = false;
1099 for ( ; pass; prev_pass = pass, pass = pass->next)
1101 /* Check if the current pass is of the same type as the new pass and
1102 matches the name and the instance number of the reference pass. */
1103 if (pass->type == new_pass_info->pass->type
1104 && pass->name
1105 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1106 && ((new_pass_info->ref_pass_instance_number == 0)
1107 || (new_pass_info->ref_pass_instance_number ==
1108 pass->static_pass_number)
1109 || (new_pass_info->ref_pass_instance_number == 1
1110 && pass->todo_flags_start & TODO_mark_first_instance)))
1112 struct opt_pass *new_pass;
1113 struct pass_list_node *new_pass_node;
1115 new_pass = make_pass_instance (new_pass_info->pass, true);
1117 /* Insert the new pass instance based on the positioning op. */
1118 switch (new_pass_info->pos_op)
1120 case PASS_POS_INSERT_AFTER:
1121 new_pass->next = pass->next;
1122 pass->next = new_pass;
1124 /* Skip newly inserted pass to avoid repeated
1125 insertions in the case where the new pass and the
1126 existing one have the same name. */
1127 pass = new_pass;
1128 break;
1129 case PASS_POS_INSERT_BEFORE:
1130 new_pass->next = pass;
1131 if (prev_pass)
1132 prev_pass->next = new_pass;
1133 else
1134 *pass_list = new_pass;
1135 break;
1136 case PASS_POS_REPLACE:
1137 new_pass->next = pass->next;
1138 if (prev_pass)
1139 prev_pass->next = new_pass;
1140 else
1141 *pass_list = new_pass;
1142 new_pass->sub = pass->sub;
1143 new_pass->tv_id = pass->tv_id;
1144 pass = new_pass;
1145 break;
1146 default:
1147 error ("invalid pass positioning operation");
1148 return false;
1151 /* Save the newly added pass (instance) in the added_pass_nodes
1152 list so that we can register its dump file later. Note that
1153 we cannot register the dump file now because doing so will modify
1154 the static_pass_number of the opt_pass object and therefore
1155 mess up the dump file name of future instances. */
1156 new_pass_node = XCNEW (struct pass_list_node);
1157 new_pass_node->pass = new_pass;
1158 if (!added_pass_nodes)
1159 added_pass_nodes = new_pass_node;
1160 else
1161 prev_added_pass_node->next = new_pass_node;
1162 prev_added_pass_node = new_pass_node;
1164 success = true;
1167 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1168 success = true;
1171 return success;
1174 /* Hooks a new pass into the pass lists.
1176 PASS_INFO - pass information that specifies the opt_pass object,
1177 reference pass, instance number, and how to position
1178 the pass */
1180 void
1181 register_pass (struct register_pass_info *pass_info)
1183 bool all_instances, success;
1185 /* The checks below could fail in buggy plugins. Existing GCC
1186 passes should never fail these checks, so we mention plugin in
1187 the messages. */
1188 if (!pass_info->pass)
1189 fatal_error ("plugin cannot register a missing pass");
1191 if (!pass_info->pass->name)
1192 fatal_error ("plugin cannot register an unnamed pass");
1194 if (!pass_info->reference_pass_name)
1195 fatal_error
1196 ("plugin cannot register pass %qs without reference pass name",
1197 pass_info->pass->name);
1199 /* Try to insert the new pass to the pass lists. We need to check
1200 all five lists as the reference pass could be in one (or all) of
1201 them. */
1202 all_instances = pass_info->ref_pass_instance_number == 0;
1203 success = position_pass (pass_info, &all_lowering_passes);
1204 if (!success || all_instances)
1205 success |= position_pass (pass_info, &all_small_ipa_passes);
1206 if (!success || all_instances)
1207 success |= position_pass (pass_info, &all_regular_ipa_passes);
1208 if (!success || all_instances)
1209 success |= position_pass (pass_info, &all_lto_gen_passes);
1210 if (!success || all_instances)
1211 success |= position_pass (pass_info, &all_late_ipa_passes);
1212 if (!success || all_instances)
1213 success |= position_pass (pass_info, &all_passes);
1214 if (!success)
1215 fatal_error
1216 ("pass %qs not found but is referenced by new pass %qs",
1217 pass_info->reference_pass_name, pass_info->pass->name);
1219 /* OK, we have successfully inserted the new pass. We need to register
1220 the dump files for the newly added pass and its duplicates (if any).
1221 Because the registration of plugin/backend passes happens after the
1222 command-line options are parsed, the options that specify single
1223 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1224 passes. Therefore we currently can only enable dumping of
1225 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1226 are specified. While doing so, we also delete the pass_list_node
1227 objects created during pass positioning. */
1228 while (added_pass_nodes)
1230 struct pass_list_node *next_node = added_pass_nodes->next;
1231 enum tree_dump_index tdi;
1232 register_one_dump_file (added_pass_nodes->pass);
1233 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1234 || added_pass_nodes->pass->type == IPA_PASS)
1235 tdi = TDI_ipa_all;
1236 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1237 tdi = TDI_tree_all;
1238 else
1239 tdi = TDI_rtl_all;
1240 /* Check if dump-all flag is specified. */
1241 if (get_dump_file_info (tdi)->state)
1242 get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1243 ->state = get_dump_file_info (tdi)->state;
1244 XDELETE (added_pass_nodes);
1245 added_pass_nodes = next_node;
1249 /* Construct the pass tree. The sequencing of passes is driven by
1250 the cgraph routines:
1252 finalize_compilation_unit ()
1253 for each node N in the cgraph
1254 cgraph_analyze_function (N)
1255 cgraph_lower_function (N) -> all_lowering_passes
1257 If we are optimizing, compile is then invoked:
1259 compile ()
1260 ipa_passes () -> all_small_ipa_passes
1261 -> Analysis of all_regular_ipa_passes
1262 * possible LTO streaming at copmilation time *
1263 -> Execution of all_regular_ipa_passes
1264 * possible LTO streaming at link time *
1265 -> all_late_ipa_passes
1266 expand_all_functions ()
1267 for each node N in the cgraph
1268 expand_function (N) -> Transformation of all_regular_ipa_passes
1269 -> all_passes
1272 void
1273 init_optimization_passes (void)
1275 struct opt_pass **p;
1277 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &((PASS).pass)))
1279 /* All passes needed to lower the function into shape optimizers can
1280 operate on. These passes are always run first on the function, but
1281 backend might produce already lowered functions that are not processed
1282 by these passes. */
1283 p = &all_lowering_passes;
1284 NEXT_PASS (pass_warn_unused_result);
1285 NEXT_PASS (pass_diagnose_omp_blocks);
1286 NEXT_PASS (pass_diagnose_tm_blocks);
1287 NEXT_PASS (pass_mudflap_1);
1288 NEXT_PASS (pass_lower_omp);
1289 NEXT_PASS (pass_lower_cf);
1290 NEXT_PASS (pass_lower_tm);
1291 NEXT_PASS (pass_refactor_eh);
1292 NEXT_PASS (pass_lower_eh);
1293 NEXT_PASS (pass_build_cfg);
1294 NEXT_PASS (pass_warn_function_return);
1295 NEXT_PASS (pass_build_cgraph_edges);
1296 *p = NULL;
1298 /* Interprocedural optimization passes. */
1299 p = &all_small_ipa_passes;
1300 NEXT_PASS (pass_ipa_free_lang_data);
1301 NEXT_PASS (pass_ipa_function_and_variable_visibility);
1302 NEXT_PASS (pass_early_local_passes);
1304 struct opt_pass **p = &pass_early_local_passes.pass.sub;
1305 NEXT_PASS (pass_fixup_cfg);
1306 NEXT_PASS (pass_init_datastructures);
1307 NEXT_PASS (pass_expand_omp);
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 /* After CCP we rewrite no longer addressed locals into SSA
1322 form if possible. */
1323 NEXT_PASS (pass_forwprop);
1324 /* pass_build_ealias is a dummy pass that ensures that we
1325 execute TODO_rebuild_alias at this point. */
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_free_inline_summary);
1348 NEXT_PASS (pass_ipa_tree_profile);
1350 struct opt_pass **p = &pass_ipa_tree_profile.pass.sub;
1351 NEXT_PASS (pass_feedback_split_functions);
1353 NEXT_PASS (pass_ipa_increase_alignment);
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;
1380 /* These passes are run after IPA passes on every function that is being
1381 output to the assembler file. */
1382 p = &all_passes;
1383 NEXT_PASS (pass_fixup_cfg);
1384 NEXT_PASS (pass_lower_eh_dispatch);
1385 NEXT_PASS (pass_all_optimizations);
1387 struct opt_pass **p = &pass_all_optimizations.pass.sub;
1388 NEXT_PASS (pass_remove_cgraph_callee_edges);
1389 /* Initial scalar cleanups before alias computation.
1390 They ensure memory accesses are not indirect wherever possible. */
1391 NEXT_PASS (pass_strip_predict_hints);
1392 NEXT_PASS (pass_rename_ssa_copies);
1393 NEXT_PASS (pass_complete_unrolli);
1394 NEXT_PASS (pass_ccp);
1395 /* After CCP we rewrite no longer addressed locals into SSA
1396 form if possible. */
1397 NEXT_PASS (pass_forwprop);
1398 /* pass_build_alias is a dummy pass that ensures that we
1399 execute TODO_rebuild_alias at this point. */
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_call_cdce);
1409 NEXT_PASS (pass_cselim);
1410 NEXT_PASS (pass_tree_ifcombine);
1411 NEXT_PASS (pass_phiopt);
1412 NEXT_PASS (pass_tail_recursion);
1413 NEXT_PASS (pass_ch);
1414 NEXT_PASS (pass_stdarg);
1415 NEXT_PASS (pass_lower_complex);
1416 NEXT_PASS (pass_sra);
1417 NEXT_PASS (pass_rename_ssa_copies);
1418 /* The dom pass will also resolve all __builtin_constant_p calls
1419 that are still there to 0. This has to be done after some
1420 propagations have already run, but before some more dead code
1421 is removed, and this place fits nicely. Remember this when
1422 trying to move or duplicate pass_dominator somewhere earlier. */
1423 NEXT_PASS (pass_dominator);
1424 /* The only const/copy propagation opportunities left after
1425 DOM should be due to degenerate PHI nodes. So rather than
1426 run the full propagators, run a specialized pass which
1427 only examines PHIs to discover const/copy propagation
1428 opportunities. */
1429 NEXT_PASS (pass_phi_only_cprop);
1430 NEXT_PASS (pass_dse);
1431 NEXT_PASS (pass_reassoc);
1432 NEXT_PASS (pass_dce);
1433 NEXT_PASS (pass_forwprop);
1434 NEXT_PASS (pass_phiopt);
1435 NEXT_PASS (pass_object_sizes);
1436 NEXT_PASS (pass_strlen);
1437 NEXT_PASS (pass_ccp);
1438 /* After CCP we rewrite no longer addressed locals into SSA
1439 form if possible. */
1440 NEXT_PASS (pass_copy_prop);
1441 NEXT_PASS (pass_cse_sincos);
1442 NEXT_PASS (pass_optimize_bswap);
1443 NEXT_PASS (pass_split_crit_edges);
1444 NEXT_PASS (pass_pre);
1445 NEXT_PASS (pass_sink_code);
1446 NEXT_PASS (pass_tree_loop);
1448 struct opt_pass **p = &pass_tree_loop.pass.sub;
1449 NEXT_PASS (pass_tree_loop_init);
1450 NEXT_PASS (pass_lim);
1451 NEXT_PASS (pass_copy_prop);
1452 NEXT_PASS (pass_dce_loop);
1453 NEXT_PASS (pass_tree_unswitch);
1454 NEXT_PASS (pass_scev_cprop);
1455 NEXT_PASS (pass_record_bounds);
1456 NEXT_PASS (pass_check_data_deps);
1457 NEXT_PASS (pass_loop_distribution);
1458 NEXT_PASS (pass_copy_prop);
1459 NEXT_PASS (pass_graphite);
1461 struct opt_pass **p = &pass_graphite.pass.sub;
1462 NEXT_PASS (pass_graphite_transforms);
1463 NEXT_PASS (pass_lim);
1464 NEXT_PASS (pass_copy_prop);
1465 NEXT_PASS (pass_dce_loop);
1467 NEXT_PASS (pass_iv_canon);
1468 NEXT_PASS (pass_if_conversion);
1469 NEXT_PASS (pass_vectorize);
1471 struct opt_pass **p = &pass_vectorize.pass.sub;
1472 NEXT_PASS (pass_dce_loop);
1474 NEXT_PASS (pass_predcom);
1475 NEXT_PASS (pass_complete_unroll);
1476 NEXT_PASS (pass_slp_vectorize);
1477 NEXT_PASS (pass_parallelize_loops);
1478 NEXT_PASS (pass_loop_prefetch);
1479 NEXT_PASS (pass_iv_optimize);
1480 NEXT_PASS (pass_lim);
1481 NEXT_PASS (pass_tree_loop_done);
1483 NEXT_PASS (pass_lower_vector_ssa);
1484 NEXT_PASS (pass_cse_reciprocals);
1485 NEXT_PASS (pass_reassoc);
1486 NEXT_PASS (pass_vrp);
1487 NEXT_PASS (pass_strength_reduction);
1488 NEXT_PASS (pass_dominator);
1489 /* The only const/copy propagation opportunities left after
1490 DOM should be due to degenerate PHI nodes. So rather than
1491 run the full propagators, run a specialized pass which
1492 only examines PHIs to discover const/copy propagation
1493 opportunities. */
1494 NEXT_PASS (pass_phi_only_cprop);
1495 NEXT_PASS (pass_cd_dce);
1496 NEXT_PASS (pass_tracer);
1498 /* FIXME: If DCE is not run before checking for uninitialized uses,
1499 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
1500 However, this also causes us to misdiagnose cases that should be
1501 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
1503 To fix the false positives in uninit-5.c, we would have to
1504 account for the predicates protecting the set and the use of each
1505 variable. Using a representation like Gated Single Assignment
1506 may help. */
1507 NEXT_PASS (pass_late_warn_uninitialized);
1508 NEXT_PASS (pass_dse);
1509 NEXT_PASS (pass_forwprop);
1510 NEXT_PASS (pass_phiopt);
1511 NEXT_PASS (pass_fold_builtins);
1512 NEXT_PASS (pass_optimize_widening_mul);
1513 NEXT_PASS (pass_tail_calls);
1514 NEXT_PASS (pass_rename_ssa_copies);
1515 NEXT_PASS (pass_uncprop);
1516 NEXT_PASS (pass_local_pure_const);
1518 NEXT_PASS (pass_all_optimizations_g);
1520 struct opt_pass **p = &pass_all_optimizations_g.pass.sub;
1521 NEXT_PASS (pass_remove_cgraph_callee_edges);
1522 NEXT_PASS (pass_strip_predict_hints);
1523 /* Lower remaining pieces of GIMPLE. */
1524 NEXT_PASS (pass_lower_complex);
1525 NEXT_PASS (pass_lower_vector_ssa);
1526 /* Perform simple scalar cleanup which is constant/copy propagation. */
1527 NEXT_PASS (pass_ccp);
1528 NEXT_PASS (pass_object_sizes);
1529 /* Copy propagation also copy-propagates constants, this is necessary
1530 to forward object-size results properly. */
1531 NEXT_PASS (pass_copy_prop);
1532 NEXT_PASS (pass_rename_ssa_copies);
1533 NEXT_PASS (pass_dce);
1534 /* Fold remaining builtins. */
1535 NEXT_PASS (pass_fold_builtins);
1536 /* ??? We do want some kind of loop invariant motion, but we possibly
1537 need to adjust LIM to be more friendly towards preserving accurate
1538 debug information here. */
1539 NEXT_PASS (pass_late_warn_uninitialized);
1540 NEXT_PASS (pass_uncprop);
1541 NEXT_PASS (pass_local_pure_const);
1543 NEXT_PASS (pass_tm_init);
1545 struct opt_pass **p = &pass_tm_init.pass.sub;
1546 NEXT_PASS (pass_tm_mark);
1547 NEXT_PASS (pass_tm_memopt);
1548 NEXT_PASS (pass_tm_edges);
1550 NEXT_PASS (pass_lower_complex_O0);
1551 NEXT_PASS (pass_cleanup_eh);
1552 NEXT_PASS (pass_lower_resx);
1553 NEXT_PASS (pass_nrv);
1554 NEXT_PASS (pass_mudflap_2);
1555 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
1556 NEXT_PASS (pass_warn_function_noreturn);
1558 NEXT_PASS (pass_expand);
1560 NEXT_PASS (pass_rest_of_compilation);
1562 struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
1563 NEXT_PASS (pass_instantiate_virtual_regs);
1564 NEXT_PASS (pass_into_cfg_layout_mode);
1565 NEXT_PASS (pass_jump);
1566 NEXT_PASS (pass_lower_subreg);
1567 NEXT_PASS (pass_df_initialize_opt);
1568 NEXT_PASS (pass_cse);
1569 NEXT_PASS (pass_rtl_fwprop);
1570 NEXT_PASS (pass_rtl_cprop);
1571 NEXT_PASS (pass_rtl_pre);
1572 NEXT_PASS (pass_rtl_hoist);
1573 NEXT_PASS (pass_rtl_cprop);
1574 NEXT_PASS (pass_rtl_store_motion);
1575 NEXT_PASS (pass_cse_after_global_opts);
1576 NEXT_PASS (pass_rtl_ifcvt);
1577 NEXT_PASS (pass_reginfo_init);
1578 /* Perform loop optimizations. It might be better to do them a bit
1579 sooner, but we want the profile feedback to work more
1580 efficiently. */
1581 NEXT_PASS (pass_loop2);
1583 struct opt_pass **p = &pass_loop2.pass.sub;
1584 NEXT_PASS (pass_rtl_loop_init);
1585 NEXT_PASS (pass_rtl_move_loop_invariants);
1586 NEXT_PASS (pass_rtl_unswitch);
1587 NEXT_PASS (pass_rtl_unroll_and_peel_loops);
1588 NEXT_PASS (pass_rtl_doloop);
1589 NEXT_PASS (pass_rtl_loop_done);
1590 *p = NULL;
1592 NEXT_PASS (pass_web);
1593 NEXT_PASS (pass_rtl_cprop);
1594 NEXT_PASS (pass_cse2);
1595 NEXT_PASS (pass_rtl_dse1);
1596 NEXT_PASS (pass_rtl_fwprop_addr);
1597 NEXT_PASS (pass_inc_dec);
1598 NEXT_PASS (pass_initialize_regs);
1599 NEXT_PASS (pass_ud_rtl_dce);
1600 NEXT_PASS (pass_combine);
1601 NEXT_PASS (pass_if_after_combine);
1602 NEXT_PASS (pass_partition_blocks);
1603 NEXT_PASS (pass_regmove);
1604 NEXT_PASS (pass_outof_cfg_layout_mode);
1605 NEXT_PASS (pass_split_all_insns);
1606 NEXT_PASS (pass_lower_subreg2);
1607 NEXT_PASS (pass_df_initialize_no_opt);
1608 NEXT_PASS (pass_stack_ptr_mod);
1609 NEXT_PASS (pass_mode_switching);
1610 NEXT_PASS (pass_match_asm_constraints);
1611 NEXT_PASS (pass_sms);
1612 NEXT_PASS (pass_sched);
1613 NEXT_PASS (pass_ira);
1614 NEXT_PASS (pass_reload);
1615 NEXT_PASS (pass_postreload);
1617 struct opt_pass **p = &pass_postreload.pass.sub;
1618 NEXT_PASS (pass_postreload_cse);
1619 NEXT_PASS (pass_gcse2);
1620 NEXT_PASS (pass_split_after_reload);
1621 NEXT_PASS (pass_ree);
1622 NEXT_PASS (pass_compare_elim_after_reload);
1623 NEXT_PASS (pass_branch_target_load_optimize1);
1624 NEXT_PASS (pass_thread_prologue_and_epilogue);
1625 NEXT_PASS (pass_rtl_dse2);
1626 NEXT_PASS (pass_stack_adjustments);
1627 NEXT_PASS (pass_jump2);
1628 NEXT_PASS (pass_peephole2);
1629 NEXT_PASS (pass_if_after_reload);
1630 NEXT_PASS (pass_regrename);
1631 NEXT_PASS (pass_cprop_hardreg);
1632 NEXT_PASS (pass_fast_rtl_dce);
1633 NEXT_PASS (pass_reorder_blocks);
1634 NEXT_PASS (pass_branch_target_load_optimize2);
1635 NEXT_PASS (pass_leaf_regs);
1636 NEXT_PASS (pass_split_before_sched2);
1637 NEXT_PASS (pass_sched2);
1638 NEXT_PASS (pass_stack_regs);
1640 struct opt_pass **p = &pass_stack_regs.pass.sub;
1641 NEXT_PASS (pass_split_before_regstack);
1642 NEXT_PASS (pass_stack_regs_run);
1644 NEXT_PASS (pass_compute_alignments);
1645 NEXT_PASS (pass_duplicate_computed_gotos);
1646 NEXT_PASS (pass_variable_tracking);
1647 NEXT_PASS (pass_free_cfg);
1648 NEXT_PASS (pass_machine_reorg);
1649 NEXT_PASS (pass_cleanup_barriers);
1650 NEXT_PASS (pass_delay_slots);
1651 NEXT_PASS (pass_split_for_shorten_branches);
1652 NEXT_PASS (pass_convert_to_eh_region_ranges);
1653 NEXT_PASS (pass_shorten_branches);
1654 NEXT_PASS (pass_set_nothrow_function_flags);
1655 NEXT_PASS (pass_dwarf2_frame);
1656 NEXT_PASS (pass_final);
1658 NEXT_PASS (pass_df_finish);
1660 NEXT_PASS (pass_clean_state);
1661 *p = NULL;
1663 #undef NEXT_PASS
1665 /* Register the passes with the tree dump code. */
1666 register_dump_files (all_lowering_passes, PROP_gimple_any);
1667 register_dump_files (all_small_ipa_passes,
1668 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1669 | PROP_cfg);
1670 register_dump_files (all_regular_ipa_passes,
1671 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1672 | PROP_cfg);
1673 register_dump_files (all_lto_gen_passes,
1674 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1675 | PROP_cfg);
1676 register_dump_files (all_late_ipa_passes,
1677 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1678 | PROP_cfg);
1679 register_dump_files (all_passes,
1680 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1681 | PROP_cfg);
1684 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1685 function CALLBACK for every function in the call graph. Otherwise,
1686 call CALLBACK on the current function. */
1688 static void
1689 do_per_function (void (*callback) (void *data), void *data)
1691 if (current_function_decl)
1692 callback (data);
1693 else
1695 struct cgraph_node *node;
1696 FOR_EACH_DEFINED_FUNCTION (node)
1697 if (gimple_has_body_p (node->symbol.decl)
1698 && (!node->clone_of || node->symbol.decl != node->clone_of->symbol.decl))
1700 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1701 callback (data);
1702 if (!flag_wpa)
1704 free_dominance_info (CDI_DOMINATORS);
1705 free_dominance_info (CDI_POST_DOMINATORS);
1707 pop_cfun ();
1708 ggc_collect ();
1713 /* Because inlining might remove no-longer reachable nodes, we need to
1714 keep the array visible to garbage collector to avoid reading collected
1715 out nodes. */
1716 static int nnodes;
1717 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1719 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1720 function CALLBACK for every function in the call graph. Otherwise,
1721 call CALLBACK on the current function.
1722 This function is global so that plugins can use it. */
1723 void
1724 do_per_function_toporder (void (*callback) (void *data), void *data)
1726 int i;
1728 if (current_function_decl)
1729 callback (data);
1730 else
1732 gcc_assert (!order);
1733 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1734 nnodes = ipa_reverse_postorder (order);
1735 for (i = nnodes - 1; i >= 0; i--)
1736 order[i]->process = 1;
1737 for (i = nnodes - 1; i >= 0; i--)
1739 struct cgraph_node *node = order[i];
1741 /* Allow possibly removed nodes to be garbage collected. */
1742 order[i] = NULL;
1743 node->process = 0;
1744 if (cgraph_function_with_gimple_body_p (node))
1746 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1747 callback (data);
1748 free_dominance_info (CDI_DOMINATORS);
1749 free_dominance_info (CDI_POST_DOMINATORS);
1750 pop_cfun ();
1751 ggc_collect ();
1755 ggc_free (order);
1756 order = NULL;
1757 nnodes = 0;
1760 /* Helper function to perform function body dump. */
1762 static void
1763 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1765 if (dump_file && current_function_decl)
1767 if (cfun->curr_properties & PROP_trees)
1768 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1769 else
1771 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1773 if ((cfun->curr_properties & PROP_cfg)
1774 && graph_dump_format != no_graph
1775 && (dump_flags & TDF_GRAPH))
1776 print_rtl_graph_with_bb (dump_file_name, get_insns ());
1779 /* Flush the file. If verification fails, we won't be able to
1780 close the file before aborting. */
1781 fflush (dump_file);
1785 /* Perform all TODO actions that ought to be done on each function. */
1787 static void
1788 execute_function_todo (void *data)
1790 unsigned int flags = (size_t)data;
1791 flags &= ~cfun->last_verified;
1792 if (!flags)
1793 return;
1795 /* Always cleanup the CFG before trying to update SSA. */
1796 if (flags & TODO_cleanup_cfg)
1798 bool cleanup = cleanup_tree_cfg ();
1800 if (cleanup && (cfun->curr_properties & PROP_ssa))
1801 flags |= TODO_remove_unused_locals;
1803 /* When cleanup_tree_cfg merges consecutive blocks, it may
1804 perform some simplistic propagation when removing single
1805 valued PHI nodes. This propagation may, in turn, cause the
1806 SSA form to become out-of-date (see PR 22037). So, even
1807 if the parent pass had not scheduled an SSA update, we may
1808 still need to do one. */
1809 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1810 flags |= TODO_update_ssa;
1813 if (flags & TODO_update_ssa_any)
1815 unsigned update_flags = flags & TODO_update_ssa_any;
1816 update_ssa (update_flags);
1817 cfun->last_verified &= ~TODO_verify_ssa;
1820 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1821 compute_may_aliases ();
1823 if (optimize && (flags & TODO_update_address_taken))
1824 execute_update_addresses_taken ();
1826 if (flags & TODO_remove_unused_locals)
1827 remove_unused_locals ();
1829 if (flags & TODO_rebuild_frequencies)
1830 rebuild_frequencies ();
1832 if (flags & TODO_rebuild_cgraph_edges)
1833 rebuild_cgraph_edges ();
1835 /* If we've seen errors do not bother running any verifiers. */
1836 if (seen_error ())
1837 return;
1839 #if defined ENABLE_CHECKING
1840 if (flags & TODO_verify_ssa
1841 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1843 verify_gimple_in_cfg (cfun);
1844 verify_ssa (true);
1846 else if (flags & TODO_verify_stmts)
1847 verify_gimple_in_cfg (cfun);
1848 if (flags & TODO_verify_flow)
1849 verify_flow_info ();
1850 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1851 verify_loop_closed_ssa (false);
1852 if (flags & TODO_verify_rtl_sharing)
1853 verify_rtl_sharing ();
1854 #endif
1856 cfun->last_verified = flags & TODO_verify_all;
1859 /* Perform all TODO actions. */
1860 static void
1861 execute_todo (unsigned int flags)
1863 #if defined ENABLE_CHECKING
1864 if (cfun
1865 && need_ssa_update_p (cfun))
1866 gcc_assert (flags & TODO_update_ssa_any);
1867 #endif
1869 timevar_push (TV_TODO);
1871 /* Inform the pass whether it is the first time it is run. */
1872 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1874 statistics_fini_pass ();
1876 do_per_function (execute_function_todo, (void *)(size_t) flags);
1878 /* Always remove functions just as before inlining: IPA passes might be
1879 interested to see bodies of extern inline functions that are not inlined
1880 to analyze side effects. The full removal is done just at the end
1881 of IPA pass queue. */
1882 if (flags & TODO_remove_functions)
1884 gcc_assert (!cfun);
1885 symtab_remove_unreachable_nodes (true, dump_file);
1888 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1890 gcc_assert (!cfun);
1891 dump_symtab (dump_file);
1892 /* Flush the file. If verification fails, we won't be able to
1893 close the file before aborting. */
1894 fflush (dump_file);
1897 if (flags & TODO_ggc_collect)
1898 ggc_collect ();
1900 /* Now that the dumping has been done, we can get rid of the optional
1901 df problems. */
1902 if (flags & TODO_df_finish)
1903 df_finish_pass ((flags & TODO_df_verify) != 0);
1905 timevar_pop (TV_TODO);
1908 /* Verify invariants that should hold between passes. This is a place
1909 to put simple sanity checks. */
1911 static void
1912 verify_interpass_invariants (void)
1914 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1917 /* Clear the last verified flag. */
1919 static void
1920 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1922 cfun->last_verified = 0;
1925 /* Helper function. Verify that the properties has been turn into the
1926 properties expected by the pass. */
1928 #ifdef ENABLE_CHECKING
1929 static void
1930 verify_curr_properties (void *data)
1932 unsigned int props = (size_t)data;
1933 gcc_assert ((cfun->curr_properties & props) == props);
1935 #endif
1937 /* Initialize pass dump file. */
1938 /* This is non-static so that the plugins can use it. */
1940 bool
1941 pass_init_dump_file (struct opt_pass *pass)
1943 /* If a dump file name is present, open it if enabled. */
1944 if (pass->static_pass_number != -1)
1946 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
1947 dump_file_name = get_dump_file_name (pass->static_pass_number);
1948 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
1949 if (dump_file && current_function_decl)
1950 dump_function_header (dump_file, current_function_decl, dump_flags);
1951 return initializing_dump;
1953 else
1954 return false;
1957 /* Flush PASS dump file. */
1958 /* This is non-static so that plugins can use it. */
1960 void
1961 pass_fini_dump_file (struct opt_pass *pass)
1963 /* Flush and close dump file. */
1964 if (dump_file_name)
1966 free (CONST_CAST (char *, dump_file_name));
1967 dump_file_name = NULL;
1970 if (dump_file)
1972 dump_end (pass->static_pass_number, dump_file);
1973 dump_file = NULL;
1977 /* After executing the pass, apply expected changes to the function
1978 properties. */
1980 static void
1981 update_properties_after_pass (void *data)
1983 struct opt_pass *pass = (struct opt_pass *) data;
1984 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1985 & ~pass->properties_destroyed;
1988 /* Execute summary generation for all of the passes in IPA_PASS. */
1990 void
1991 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1993 while (ipa_pass)
1995 struct opt_pass *pass = &ipa_pass->pass;
1997 /* Execute all of the IPA_PASSes in the list. */
1998 if (ipa_pass->pass.type == IPA_PASS
1999 && (!pass->gate || pass->gate ())
2000 && ipa_pass->generate_summary)
2002 pass_init_dump_file (pass);
2004 /* If a timevar is present, start it. */
2005 if (pass->tv_id)
2006 timevar_push (pass->tv_id);
2008 ipa_pass->generate_summary ();
2010 /* Stop timevar. */
2011 if (pass->tv_id)
2012 timevar_pop (pass->tv_id);
2014 pass_fini_dump_file (pass);
2016 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
2020 /* Execute IPA_PASS function transform on NODE. */
2022 static void
2023 execute_one_ipa_transform_pass (struct cgraph_node *node,
2024 struct ipa_opt_pass_d *ipa_pass)
2026 struct opt_pass *pass = &ipa_pass->pass;
2027 unsigned int todo_after = 0;
2029 current_pass = pass;
2030 if (!ipa_pass->function_transform)
2031 return;
2033 /* Note that the folders should only create gimple expressions.
2034 This is a hack until the new folder is ready. */
2035 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2037 pass_init_dump_file (pass);
2039 /* Run pre-pass verification. */
2040 execute_todo (ipa_pass->function_transform_todo_flags_start);
2042 /* If a timevar is present, start it. */
2043 if (pass->tv_id != TV_NONE)
2044 timevar_push (pass->tv_id);
2046 /* Do it! */
2047 todo_after = ipa_pass->function_transform (node);
2049 /* Stop timevar. */
2050 if (pass->tv_id != TV_NONE)
2051 timevar_pop (pass->tv_id);
2053 /* Run post-pass cleanup and verification. */
2054 execute_todo (todo_after);
2055 verify_interpass_invariants ();
2057 do_per_function (execute_function_dump, NULL);
2058 pass_fini_dump_file (pass);
2060 current_pass = NULL;
2063 /* For the current function, execute all ipa transforms. */
2065 void
2066 execute_all_ipa_transforms (void)
2068 struct cgraph_node *node;
2069 if (!cfun)
2070 return;
2071 node = cgraph_get_node (current_function_decl);
2073 if (node->ipa_transforms_to_apply)
2075 unsigned int i;
2077 for (i = 0; i < VEC_length (ipa_opt_pass, node->ipa_transforms_to_apply);
2078 i++)
2079 execute_one_ipa_transform_pass (node,
2080 VEC_index (ipa_opt_pass,
2081 node->ipa_transforms_to_apply,
2082 i));
2083 VEC_free (ipa_opt_pass, heap, node->ipa_transforms_to_apply);
2084 node->ipa_transforms_to_apply = NULL;
2088 /* Callback for do_per_function to apply all IPA transforms. */
2090 static void
2091 apply_ipa_transforms (void *data)
2093 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2094 if (!node->global.inlined_to && node->ipa_transforms_to_apply)
2096 *(bool *)data = true;
2097 execute_all_ipa_transforms();
2098 rebuild_cgraph_edges ();
2102 /* Check if PASS is explicitly disabled or enabled and return
2103 the gate status. FUNC is the function to be processed, and
2104 GATE_STATUS is the gate status determined by pass manager by
2105 default. */
2107 static bool
2108 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2110 bool explicitly_enabled = false;
2111 bool explicitly_disabled = false;
2113 explicitly_enabled
2114 = is_pass_explicitly_enabled_or_disabled (pass, func,
2115 enabled_pass_uid_range_tab);
2116 explicitly_disabled
2117 = is_pass_explicitly_enabled_or_disabled (pass, func,
2118 disabled_pass_uid_range_tab);
2120 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2122 return gate_status;
2126 /* Execute PASS. */
2128 bool
2129 execute_one_pass (struct opt_pass *pass)
2131 bool initializing_dump;
2132 unsigned int todo_after = 0;
2134 bool gate_status;
2136 /* IPA passes are executed on whole program, so cfun should be NULL.
2137 Other passes need function context set. */
2138 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2139 gcc_assert (!cfun && !current_function_decl);
2140 else
2141 gcc_assert (cfun && current_function_decl);
2143 current_pass = pass;
2145 /* Check whether gate check should be avoided.
2146 User controls the value of the gate through the parameter "gate_status". */
2147 gate_status = (pass->gate == NULL) ? true : pass->gate();
2148 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2150 /* Override gate with plugin. */
2151 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2153 if (!gate_status)
2155 current_pass = NULL;
2156 return false;
2159 /* Pass execution event trigger: useful to identify passes being
2160 executed. */
2161 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2163 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2164 Apply all trnasforms first. */
2165 if (pass->type == SIMPLE_IPA_PASS)
2167 bool applied = false;
2168 do_per_function (apply_ipa_transforms, (void *)&applied);
2169 if (applied)
2170 symtab_remove_unreachable_nodes (true, dump_file);
2171 /* Restore current_pass. */
2172 current_pass = pass;
2175 if (!quiet_flag && !cfun)
2176 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2178 /* Note that the folders should only create gimple expressions.
2179 This is a hack until the new folder is ready. */
2180 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2182 initializing_dump = pass_init_dump_file (pass);
2184 /* Run pre-pass verification. */
2185 execute_todo (pass->todo_flags_start);
2187 #ifdef ENABLE_CHECKING
2188 do_per_function (verify_curr_properties,
2189 (void *)(size_t)pass->properties_required);
2190 #endif
2192 /* If a timevar is present, start it. */
2193 if (pass->tv_id != TV_NONE)
2194 timevar_push (pass->tv_id);
2196 /* Do it! */
2197 if (pass->execute)
2199 todo_after = pass->execute ();
2200 do_per_function (clear_last_verified, NULL);
2203 /* Stop timevar. */
2204 if (pass->tv_id != TV_NONE)
2205 timevar_pop (pass->tv_id);
2207 do_per_function (update_properties_after_pass, pass);
2209 if (initializing_dump
2210 && dump_file
2211 && graph_dump_format != no_graph
2212 && cfun
2213 && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
2214 == (PROP_cfg | PROP_rtl))
2216 get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
2217 dump_flags |= TDF_GRAPH;
2218 clean_graph_dump_file (dump_file_name);
2221 /* Run post-pass cleanup and verification. */
2222 execute_todo (todo_after | pass->todo_flags_finish);
2223 verify_interpass_invariants ();
2224 do_per_function (execute_function_dump, NULL);
2225 if (pass->type == IPA_PASS)
2227 struct cgraph_node *node;
2228 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2229 VEC_safe_push (ipa_opt_pass, heap, node->ipa_transforms_to_apply,
2230 (struct ipa_opt_pass_d *)pass);
2233 if (!current_function_decl)
2234 cgraph_process_new_functions ();
2236 pass_fini_dump_file (pass);
2238 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2239 gcc_assert (!(cfun->curr_properties & PROP_trees)
2240 || pass->type != RTL_PASS);
2242 current_pass = NULL;
2244 return true;
2247 void
2248 execute_pass_list (struct opt_pass *pass)
2252 gcc_assert (pass->type == GIMPLE_PASS
2253 || pass->type == RTL_PASS);
2254 if (execute_one_pass (pass) && pass->sub)
2255 execute_pass_list (pass->sub);
2256 pass = pass->next;
2258 while (pass);
2261 /* Same as execute_pass_list but assume that subpasses of IPA passes
2262 are local passes. If SET is not NULL, write out summaries of only
2263 those node in SET. */
2265 static void
2266 ipa_write_summaries_2 (struct opt_pass *pass, struct lto_out_decl_state *state)
2268 while (pass)
2270 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2271 gcc_assert (!current_function_decl);
2272 gcc_assert (!cfun);
2273 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2274 if (pass->type == IPA_PASS
2275 && ipa_pass->write_summary
2276 && (!pass->gate || pass->gate ()))
2278 /* If a timevar is present, start it. */
2279 if (pass->tv_id)
2280 timevar_push (pass->tv_id);
2282 pass_init_dump_file (pass);
2284 ipa_pass->write_summary ();
2286 pass_fini_dump_file (pass);
2288 /* If a timevar is present, start it. */
2289 if (pass->tv_id)
2290 timevar_pop (pass->tv_id);
2293 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2294 ipa_write_summaries_2 (pass->sub, state);
2296 pass = pass->next;
2300 /* Helper function of ipa_write_summaries. Creates and destroys the
2301 decl state and calls ipa_write_summaries_2 for all passes that have
2302 summaries. SET is the set of nodes to be written. */
2304 static void
2305 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2307 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2308 state->symtab_node_encoder = encoder;
2310 lto_push_out_decl_state (state);
2312 gcc_assert (!flag_wpa);
2313 ipa_write_summaries_2 (all_regular_ipa_passes, state);
2314 ipa_write_summaries_2 (all_lto_gen_passes, state);
2316 gcc_assert (lto_get_out_decl_state () == state);
2317 lto_pop_out_decl_state ();
2318 lto_delete_out_decl_state (state);
2321 /* Write out summaries for all the nodes in the callgraph. */
2323 void
2324 ipa_write_summaries (void)
2326 lto_symtab_encoder_t encoder;
2327 int i, order_pos;
2328 struct varpool_node *vnode;
2329 struct cgraph_node **order;
2331 if (!flag_generate_lto || seen_error ())
2332 return;
2334 encoder = lto_symtab_encoder_new ();
2336 /* Create the callgraph set in the same order used in
2337 cgraph_expand_all_functions. This mostly facilitates debugging,
2338 since it causes the gimple file to be processed in the same order
2339 as the source code. */
2340 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2341 order_pos = ipa_reverse_postorder (order);
2342 gcc_assert (order_pos == cgraph_n_nodes);
2344 for (i = order_pos - 1; i >= 0; i--)
2346 struct cgraph_node *node = order[i];
2348 if (cgraph_function_with_gimple_body_p (node))
2350 /* When streaming out references to statements as part of some IPA
2351 pass summary, the statements need to have uids assigned and the
2352 following does that for all the IPA passes here. Naturally, this
2353 ordering then matches the one IPA-passes get in their stmt_fixup
2354 hooks. */
2356 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2357 renumber_gimple_stmt_uids ();
2358 pop_cfun ();
2360 if (node->analyzed)
2361 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
2364 FOR_EACH_DEFINED_VARIABLE (vnode)
2365 if ((!vnode->alias || vnode->alias_of))
2366 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
2368 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2370 free (order);
2373 /* Same as execute_pass_list but assume that subpasses of IPA passes
2374 are local passes. If SET is not NULL, write out optimization summaries of
2375 only those node in SET. */
2377 static void
2378 ipa_write_optimization_summaries_1 (struct opt_pass *pass, struct lto_out_decl_state *state)
2380 while (pass)
2382 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2383 gcc_assert (!current_function_decl);
2384 gcc_assert (!cfun);
2385 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2386 if (pass->type == IPA_PASS
2387 && ipa_pass->write_optimization_summary
2388 && (!pass->gate || pass->gate ()))
2390 /* If a timevar is present, start it. */
2391 if (pass->tv_id)
2392 timevar_push (pass->tv_id);
2394 pass_init_dump_file (pass);
2396 ipa_pass->write_optimization_summary ();
2398 pass_fini_dump_file (pass);
2400 /* If a timevar is present, start it. */
2401 if (pass->tv_id)
2402 timevar_pop (pass->tv_id);
2405 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2406 ipa_write_optimization_summaries_1 (pass->sub, state);
2408 pass = pass->next;
2412 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2413 NULL, write out all summaries of all nodes. */
2415 void
2416 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2418 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2419 lto_symtab_encoder_iterator lsei;
2420 state->symtab_node_encoder = encoder;
2422 lto_push_out_decl_state (state);
2423 for (lsei = lsei_start_function_in_partition (encoder);
2424 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2426 struct cgraph_node *node = lsei_cgraph_node (lsei);
2427 /* When streaming out references to statements as part of some IPA
2428 pass summary, the statements need to have uids assigned.
2430 For functions newly born at WPA stage we need to initialize
2431 the uids here. */
2432 if (node->analyzed
2433 && gimple_has_body_p (node->symbol.decl))
2435 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2436 renumber_gimple_stmt_uids ();
2437 pop_cfun ();
2441 gcc_assert (flag_wpa);
2442 ipa_write_optimization_summaries_1 (all_regular_ipa_passes, state);
2443 ipa_write_optimization_summaries_1 (all_lto_gen_passes, state);
2445 gcc_assert (lto_get_out_decl_state () == state);
2446 lto_pop_out_decl_state ();
2447 lto_delete_out_decl_state (state);
2450 /* Same as execute_pass_list but assume that subpasses of IPA passes
2451 are local passes. */
2453 static void
2454 ipa_read_summaries_1 (struct opt_pass *pass)
2456 while (pass)
2458 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2460 gcc_assert (!current_function_decl);
2461 gcc_assert (!cfun);
2462 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2464 if (pass->gate == NULL || pass->gate ())
2466 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2468 /* If a timevar is present, start it. */
2469 if (pass->tv_id)
2470 timevar_push (pass->tv_id);
2472 pass_init_dump_file (pass);
2474 ipa_pass->read_summary ();
2476 pass_fini_dump_file (pass);
2478 /* Stop timevar. */
2479 if (pass->tv_id)
2480 timevar_pop (pass->tv_id);
2483 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2484 ipa_read_summaries_1 (pass->sub);
2486 pass = pass->next;
2491 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2493 void
2494 ipa_read_summaries (void)
2496 ipa_read_summaries_1 (all_regular_ipa_passes);
2497 ipa_read_summaries_1 (all_lto_gen_passes);
2500 /* Same as execute_pass_list but assume that subpasses of IPA passes
2501 are local passes. */
2503 static void
2504 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2506 while (pass)
2508 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2510 gcc_assert (!current_function_decl);
2511 gcc_assert (!cfun);
2512 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2514 if (pass->gate == NULL || pass->gate ())
2516 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2518 /* If a timevar is present, start it. */
2519 if (pass->tv_id)
2520 timevar_push (pass->tv_id);
2522 pass_init_dump_file (pass);
2524 ipa_pass->read_optimization_summary ();
2526 pass_fini_dump_file (pass);
2528 /* Stop timevar. */
2529 if (pass->tv_id)
2530 timevar_pop (pass->tv_id);
2533 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2534 ipa_read_optimization_summaries_1 (pass->sub);
2536 pass = pass->next;
2540 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2542 void
2543 ipa_read_optimization_summaries (void)
2545 ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
2546 ipa_read_optimization_summaries_1 (all_lto_gen_passes);
2549 /* Same as execute_pass_list but assume that subpasses of IPA passes
2550 are local passes. */
2551 void
2552 execute_ipa_pass_list (struct opt_pass *pass)
2556 gcc_assert (!current_function_decl);
2557 gcc_assert (!cfun);
2558 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2559 if (execute_one_pass (pass) && pass->sub)
2561 if (pass->sub->type == GIMPLE_PASS)
2563 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2564 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2565 pass->sub);
2566 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2568 else if (pass->sub->type == SIMPLE_IPA_PASS
2569 || pass->sub->type == IPA_PASS)
2570 execute_ipa_pass_list (pass->sub);
2571 else
2572 gcc_unreachable ();
2574 gcc_assert (!current_function_decl);
2575 cgraph_process_new_functions ();
2576 pass = pass->next;
2578 while (pass);
2581 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2583 static void
2584 execute_ipa_stmt_fixups (struct opt_pass *pass,
2585 struct cgraph_node *node, gimple *stmts)
2587 while (pass)
2589 /* Execute all of the IPA_PASSes in the list. */
2590 if (pass->type == IPA_PASS
2591 && (!pass->gate || pass->gate ()))
2593 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2595 if (ipa_pass->stmt_fixup)
2597 pass_init_dump_file (pass);
2598 /* If a timevar is present, start it. */
2599 if (pass->tv_id)
2600 timevar_push (pass->tv_id);
2602 ipa_pass->stmt_fixup (node, stmts);
2604 /* Stop timevar. */
2605 if (pass->tv_id)
2606 timevar_pop (pass->tv_id);
2607 pass_fini_dump_file (pass);
2609 if (pass->sub)
2610 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2612 pass = pass->next;
2616 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2618 void
2619 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2621 execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
2625 extern void debug_properties (unsigned int);
2626 extern void dump_properties (FILE *, unsigned int);
2628 DEBUG_FUNCTION void
2629 dump_properties (FILE *dump, unsigned int props)
2631 fprintf (dump, "Properties:\n");
2632 if (props & PROP_gimple_any)
2633 fprintf (dump, "PROP_gimple_any\n");
2634 if (props & PROP_gimple_lcf)
2635 fprintf (dump, "PROP_gimple_lcf\n");
2636 if (props & PROP_gimple_leh)
2637 fprintf (dump, "PROP_gimple_leh\n");
2638 if (props & PROP_cfg)
2639 fprintf (dump, "PROP_cfg\n");
2640 if (props & PROP_ssa)
2641 fprintf (dump, "PROP_ssa\n");
2642 if (props & PROP_no_crit_edges)
2643 fprintf (dump, "PROP_no_crit_edges\n");
2644 if (props & PROP_rtl)
2645 fprintf (dump, "PROP_rtl\n");
2646 if (props & PROP_gimple_lomp)
2647 fprintf (dump, "PROP_gimple_lomp\n");
2648 if (props & PROP_gimple_lcx)
2649 fprintf (dump, "PROP_gimple_lcx\n");
2650 if (props & PROP_cfglayout)
2651 fprintf (dump, "PROP_cfglayout\n");
2654 DEBUG_FUNCTION void
2655 debug_properties (unsigned int props)
2657 dump_properties (stderr, props);
2660 /* Called by local passes to see if function is called by already processed nodes.
2661 Because we process nodes in topological order, this means that function is
2662 in recursive cycle or we introduced new direct calls. */
2663 bool
2664 function_called_by_processed_nodes_p (void)
2666 struct cgraph_edge *e;
2667 for (e = cgraph_get_node (current_function_decl)->callers;
2669 e = e->next_caller)
2671 if (e->caller->symbol.decl == current_function_decl)
2672 continue;
2673 if (!cgraph_function_with_gimple_body_p (e->caller))
2674 continue;
2675 if (TREE_ASM_WRITTEN (e->caller->symbol.decl))
2676 continue;
2677 if (!e->caller->process && !e->caller->global.inlined_to)
2678 break;
2680 if (dump_file && e)
2682 fprintf (dump_file, "Already processed call to:\n");
2683 dump_cgraph_node (dump_file, e->caller);
2685 return e != NULL;
2688 #include "gt-passes.h"