Merge from mainline (168000:168310).
[official-gcc/graphite-test-results.git] / gcc / passes.c
blob4be61a9455e577594d1d23c3948afb14ff7df631
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 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"
77 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
78 #include "dwarf2out.h"
79 #endif
81 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
82 #include "dbxout.h"
83 #endif
85 #ifdef SDB_DEBUGGING_INFO
86 #include "sdbout.h"
87 #endif
89 #ifdef XCOFF_DEBUGGING_INFO
90 #include "xcoffout.h" /* Needed for external data
91 declarations for e.g. AIX 4.x. */
92 #endif
94 /* This is used for debugging. It allows the current pass to printed
95 from anywhere in compilation.
96 The variable current_pass is also used for statistics and plugins. */
97 struct opt_pass *current_pass;
99 /* Call from anywhere to find out what pass this is. Useful for
100 printing out debugging information deep inside an service
101 routine. */
102 void
103 print_current_pass (FILE *file)
105 if (current_pass)
106 fprintf (file, "current pass = %s (%d)\n",
107 current_pass->name, current_pass->static_pass_number);
108 else
109 fprintf (file, "no current pass.\n");
113 /* Call from the debugger to get the current pass name. */
114 DEBUG_FUNCTION void
115 debug_pass (void)
117 print_current_pass (stderr);
122 /* Global variables used to communicate with passes. */
123 int dump_flags;
124 bool in_gimple_form;
125 bool first_pass_instance;
128 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
129 and TYPE_DECL nodes.
131 This does nothing for local (non-static) variables, unless the
132 variable is a register variable with DECL_ASSEMBLER_NAME set. In
133 that case, or if the variable is not an automatic, it sets up the
134 RTL and outputs any assembler code (label definition, storage
135 allocation and initialization).
137 DECL is the declaration. TOP_LEVEL is nonzero
138 if this declaration is not within a function. */
140 void
141 rest_of_decl_compilation (tree decl,
142 int top_level,
143 int at_end)
145 /* We deferred calling assemble_alias so that we could collect
146 other attributes such as visibility. Emit the alias now. */
148 tree alias;
149 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
150 if (alias)
152 alias = TREE_VALUE (TREE_VALUE (alias));
153 alias = get_identifier (TREE_STRING_POINTER (alias));
154 assemble_alias (decl, alias);
158 /* Can't defer this, because it needs to happen before any
159 later function definitions are processed. */
160 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
161 make_decl_rtl (decl);
163 /* Forward declarations for nested functions are not "external",
164 but we need to treat them as if they were. */
165 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
166 || TREE_CODE (decl) == FUNCTION_DECL)
168 timevar_push (TV_VARCONST);
170 /* Don't output anything when a tentative file-scope definition
171 is seen. But at end of compilation, do output code for them.
173 We do output all variables and rely on
174 callgraph code to defer them except for forward declarations
175 (see gcc.c-torture/compile/920624-1.c) */
176 if ((at_end
177 || !DECL_DEFER_OUTPUT (decl)
178 || DECL_INITIAL (decl))
179 && !DECL_EXTERNAL (decl))
181 /* When reading LTO unit, we also read varpool, so do not
182 rebuild it. */
183 if (in_lto_p && !at_end)
185 else if (TREE_CODE (decl) != FUNCTION_DECL)
186 varpool_finalize_decl (decl);
189 #ifdef ASM_FINISH_DECLARE_OBJECT
190 if (decl == last_assemble_variable_decl)
192 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
193 top_level, at_end);
195 #endif
197 timevar_pop (TV_VARCONST);
199 else if (TREE_CODE (decl) == TYPE_DECL
200 /* Like in rest_of_type_compilation, avoid confusing the debug
201 information machinery when there are errors. */
202 && !seen_error ())
204 timevar_push (TV_SYMOUT);
205 debug_hooks->type_decl (decl, !top_level);
206 timevar_pop (TV_SYMOUT);
209 /* Let cgraph know about the existence of variables. */
210 if (in_lto_p && !at_end)
212 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
213 && TREE_STATIC (decl))
214 varpool_node (decl);
217 /* Called after finishing a record, union or enumeral type. */
219 void
220 rest_of_type_compilation (tree type, int toplev)
222 /* Avoid confusing the debug information machinery when there are
223 errors. */
224 if (seen_error ())
225 return;
227 timevar_push (TV_SYMOUT);
228 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
229 timevar_pop (TV_SYMOUT);
234 void
235 finish_optimization_passes (void)
237 int i;
238 struct dump_file_info *dfi;
239 char *name;
241 timevar_push (TV_DUMP);
242 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
244 dump_file = dump_begin (pass_profile.pass.static_pass_number, NULL);
245 end_branch_prob ();
246 if (dump_file)
247 dump_end (pass_profile.pass.static_pass_number, dump_file);
250 if (optimize > 0)
252 dump_file = dump_begin (pass_combine.pass.static_pass_number, NULL);
253 if (dump_file)
255 dump_combine_total_stats (dump_file);
256 dump_end (pass_combine.pass.static_pass_number, dump_file);
260 /* Do whatever is necessary to finish printing the graphs. */
261 if (graph_dump_format != no_graph)
262 for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
263 if (dump_initialized_p (i)
264 && (dfi->flags & TDF_GRAPH) != 0
265 && (name = get_dump_file_name (i)) != NULL)
267 finish_graph_dump_file (name);
268 free (name);
271 timevar_pop (TV_DUMP);
274 static bool
275 gate_rest_of_compilation (void)
277 /* Early return if there were errors. We can run afoul of our
278 consistency checks, and there's not really much point in fixing them. */
279 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
282 struct gimple_opt_pass pass_rest_of_compilation =
285 GIMPLE_PASS,
286 "*rest_of_compilation", /* name */
287 gate_rest_of_compilation, /* gate */
288 NULL, /* execute */
289 NULL, /* sub */
290 NULL, /* next */
291 0, /* static_pass_number */
292 TV_REST_OF_COMPILATION, /* tv_id */
293 PROP_rtl, /* properties_required */
294 0, /* properties_provided */
295 0, /* properties_destroyed */
296 0, /* todo_flags_start */
297 TODO_ggc_collect /* todo_flags_finish */
301 static bool
302 gate_postreload (void)
304 return reload_completed;
307 struct rtl_opt_pass pass_postreload =
310 RTL_PASS,
311 "*all-postreload", /* name */
312 gate_postreload, /* gate */
313 NULL, /* execute */
314 NULL, /* sub */
315 NULL, /* next */
316 0, /* static_pass_number */
317 TV_POSTRELOAD, /* tv_id */
318 PROP_rtl, /* properties_required */
319 0, /* properties_provided */
320 0, /* properties_destroyed */
321 0, /* todo_flags_start */
322 TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
328 /* The root of the compilation pass tree, once constructed. */
329 struct opt_pass *all_passes, *all_small_ipa_passes, *all_lowering_passes,
330 *all_regular_ipa_passes, *all_lto_gen_passes;
332 /* This is used by plugins, and should also be used in register_pass. */
333 #define DEF_PASS_LIST(LIST) &LIST,
334 struct opt_pass **gcc_pass_lists[] = { GCC_PASS_LISTS NULL };
335 #undef DEF_PASS_LIST
337 /* A map from static pass id to optimization pass. */
338 struct opt_pass **passes_by_id;
339 int passes_by_id_size;
341 /* Set the static pass number of pass PASS to ID and record that
342 in the mapping from static pass number to pass. */
344 static void
345 set_pass_for_id (int id, struct opt_pass *pass)
347 pass->static_pass_number = id;
348 if (passes_by_id_size <= id)
350 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
351 memset (passes_by_id + passes_by_id_size, 0,
352 (id + 1 - passes_by_id_size) * sizeof (void *));
353 passes_by_id_size = id + 1;
355 passes_by_id[id] = pass;
358 /* Return the pass with the static pass number ID. */
360 struct opt_pass *
361 get_pass_for_id (int id)
363 if (id >= passes_by_id_size)
364 return NULL;
365 return passes_by_id[id];
368 /* Iterate over the pass tree allocating dump file numbers. We want
369 to do this depth first, and independent of whether the pass is
370 enabled or not. */
372 void
373 register_one_dump_file (struct opt_pass *pass)
375 char *dot_name, *flag_name, *glob_name;
376 const char *name, *prefix;
377 char num[10];
378 int flags, id;
380 /* See below in next_pass_1. */
381 num[0] = '\0';
382 if (pass->static_pass_number != -1)
383 sprintf (num, "%d", ((int) pass->static_pass_number < 0
384 ? 1 : pass->static_pass_number));
386 /* The name is both used to identify the pass for the purposes of plugins,
387 and to specify dump file name and option.
388 The latter two might want something short which is not quite unique; for
389 that reason, we may have a disambiguating prefix, followed by a space
390 to mark the start of the following dump file name / option string. */
391 name = strchr (pass->name, ' ');
392 name = name ? name + 1 : pass->name;
393 dot_name = concat (".", name, num, NULL);
394 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
395 prefix = "ipa-", flags = TDF_IPA;
396 else if (pass->type == GIMPLE_PASS)
397 prefix = "tree-", flags = TDF_TREE;
398 else
399 prefix = "rtl-", flags = TDF_RTL;
401 flag_name = concat (prefix, name, num, NULL);
402 glob_name = concat (prefix, name, NULL);
403 id = dump_register (dot_name, flag_name, glob_name, flags);
404 set_pass_for_id (id, pass);
407 /* Recursive worker function for register_dump_files. */
409 static int
410 register_dump_files_1 (struct opt_pass *pass, int properties)
414 int new_properties = (properties | pass->properties_provided)
415 & ~pass->properties_destroyed;
417 if (pass->name && pass->name[0] != '*')
418 register_one_dump_file (pass);
420 if (pass->sub)
421 new_properties = register_dump_files_1 (pass->sub, new_properties);
423 /* If we have a gate, combine the properties that we could have with
424 and without the pass being examined. */
425 if (pass->gate)
426 properties &= new_properties;
427 else
428 properties = new_properties;
430 pass = pass->next;
432 while (pass);
434 return properties;
437 /* Register the dump files for the pipeline starting at PASS.
438 PROPERTIES reflects the properties that are guaranteed to be available at
439 the beginning of the pipeline. */
441 static void
442 register_dump_files (struct opt_pass *pass,int properties)
444 pass->properties_required |= properties;
445 register_dump_files_1 (pass, properties);
448 /* Look at the static_pass_number and duplicate the pass
449 if it is already added to a list. */
451 static struct opt_pass *
452 make_pass_instance (struct opt_pass *pass, bool track_duplicates)
454 /* A nonzero static_pass_number indicates that the
455 pass is already in the list. */
456 if (pass->static_pass_number)
458 struct opt_pass *new_pass;
460 if (pass->type == GIMPLE_PASS
461 || pass->type == RTL_PASS
462 || pass->type == SIMPLE_IPA_PASS)
464 new_pass = XNEW (struct opt_pass);
465 memcpy (new_pass, pass, sizeof (struct opt_pass));
467 else if (pass->type == IPA_PASS)
469 new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
470 memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
472 else
473 gcc_unreachable ();
475 new_pass->next = NULL;
477 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
479 /* Indicate to register_dump_files that this pass has duplicates,
480 and so it should rename the dump file. The first instance will
481 be -1, and be number of duplicates = -static_pass_number - 1.
482 Subsequent instances will be > 0 and just the duplicate number. */
483 if ((pass->name && pass->name[0] != '*') || track_duplicates)
485 pass->static_pass_number -= 1;
486 new_pass->static_pass_number = -pass->static_pass_number;
488 return new_pass;
490 else
492 pass->todo_flags_start |= TODO_mark_first_instance;
493 pass->static_pass_number = -1;
495 invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
497 return pass;
500 /* Add a pass to the pass list. Duplicate the pass if it's already
501 in the list. */
503 static struct opt_pass **
504 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
506 /* Every pass should have a name so that plugins can refer to them. */
507 gcc_assert (pass->name != NULL);
509 *list = make_pass_instance (pass, false);
511 return &(*list)->next;
514 /* List node for an inserted pass instance. We need to keep track of all
515 the newly-added pass instances (with 'added_pass_nodes' defined below)
516 so that we can register their dump files after pass-positioning is finished.
517 Registering dumping files needs to be post-processed or the
518 static_pass_number of the opt_pass object would be modified and mess up
519 the dump file names of future pass instances to be added. */
521 struct pass_list_node
523 struct opt_pass *pass;
524 struct pass_list_node *next;
527 static struct pass_list_node *added_pass_nodes = NULL;
528 static struct pass_list_node *prev_added_pass_node;
530 /* Insert the pass at the proper position. Return true if the pass
531 is successfully added.
533 NEW_PASS_INFO - new pass to be inserted
534 PASS_LIST - root of the pass list to insert the new pass to */
536 static bool
537 position_pass (struct register_pass_info *new_pass_info,
538 struct opt_pass **pass_list)
540 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
541 bool success = false;
543 for ( ; pass; prev_pass = pass, pass = pass->next)
545 /* Check if the current pass is of the same type as the new pass and
546 matches the name and the instance number of the reference pass. */
547 if (pass->type == new_pass_info->pass->type
548 && pass->name
549 && !strcmp (pass->name, new_pass_info->reference_pass_name)
550 && ((new_pass_info->ref_pass_instance_number == 0)
551 || (new_pass_info->ref_pass_instance_number ==
552 pass->static_pass_number)
553 || (new_pass_info->ref_pass_instance_number == 1
554 && pass->todo_flags_start & TODO_mark_first_instance)))
556 struct opt_pass *new_pass;
557 struct pass_list_node *new_pass_node;
559 new_pass = make_pass_instance (new_pass_info->pass, true);
561 /* Insert the new pass instance based on the positioning op. */
562 switch (new_pass_info->pos_op)
564 case PASS_POS_INSERT_AFTER:
565 new_pass->next = pass->next;
566 pass->next = new_pass;
568 /* Skip newly inserted pass to avoid repeated
569 insertions in the case where the new pass and the
570 existing one have the same name. */
571 pass = new_pass;
572 break;
573 case PASS_POS_INSERT_BEFORE:
574 new_pass->next = pass;
575 if (prev_pass)
576 prev_pass->next = new_pass;
577 else
578 *pass_list = new_pass;
579 break;
580 case PASS_POS_REPLACE:
581 new_pass->next = pass->next;
582 if (prev_pass)
583 prev_pass->next = new_pass;
584 else
585 *pass_list = new_pass;
586 new_pass->sub = pass->sub;
587 new_pass->tv_id = pass->tv_id;
588 pass = new_pass;
589 break;
590 default:
591 error ("invalid pass positioning operation");
592 return false;
595 /* Save the newly added pass (instance) in the added_pass_nodes
596 list so that we can register its dump file later. Note that
597 we cannot register the dump file now because doing so will modify
598 the static_pass_number of the opt_pass object and therefore
599 mess up the dump file name of future instances. */
600 new_pass_node = XCNEW (struct pass_list_node);
601 new_pass_node->pass = new_pass;
602 if (!added_pass_nodes)
603 added_pass_nodes = new_pass_node;
604 else
605 prev_added_pass_node->next = new_pass_node;
606 prev_added_pass_node = new_pass_node;
608 success = true;
611 if (pass->sub && position_pass (new_pass_info, &pass->sub))
612 success = true;
615 return success;
618 /* Hooks a new pass into the pass lists.
620 PASS_INFO - pass information that specifies the opt_pass object,
621 reference pass, instance number, and how to position
622 the pass */
624 void
625 register_pass (struct register_pass_info *pass_info)
627 bool all_instances, success;
629 /* The checks below could fail in buggy plugins. Existing GCC
630 passes should never fail these checks, so we mention plugin in
631 the messages. */
632 if (!pass_info->pass)
633 fatal_error ("plugin cannot register a missing pass");
635 if (!pass_info->pass->name)
636 fatal_error ("plugin cannot register an unnamed pass");
638 if (!pass_info->reference_pass_name)
639 fatal_error
640 ("plugin cannot register pass %qs without reference pass name",
641 pass_info->pass->name);
643 /* Try to insert the new pass to the pass lists. We need to check
644 all five lists as the reference pass could be in one (or all) of
645 them. */
646 all_instances = pass_info->ref_pass_instance_number == 0;
647 success = position_pass (pass_info, &all_lowering_passes);
648 if (!success || all_instances)
649 success |= position_pass (pass_info, &all_small_ipa_passes);
650 if (!success || all_instances)
651 success |= position_pass (pass_info, &all_regular_ipa_passes);
652 if (!success || all_instances)
653 success |= position_pass (pass_info, &all_lto_gen_passes);
654 if (!success || all_instances)
655 success |= position_pass (pass_info, &all_passes);
656 if (!success)
657 fatal_error
658 ("pass %qs not found but is referenced by new pass %qs",
659 pass_info->reference_pass_name, pass_info->pass->name);
661 /* OK, we have successfully inserted the new pass. We need to register
662 the dump files for the newly added pass and its duplicates (if any).
663 Because the registration of plugin/backend passes happens after the
664 command-line options are parsed, the options that specify single
665 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
666 passes. Therefore we currently can only enable dumping of
667 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
668 are specified. While doing so, we also delete the pass_list_node
669 objects created during pass positioning. */
670 while (added_pass_nodes)
672 struct pass_list_node *next_node = added_pass_nodes->next;
673 enum tree_dump_index tdi;
674 register_one_dump_file (added_pass_nodes->pass);
675 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
676 || added_pass_nodes->pass->type == IPA_PASS)
677 tdi = TDI_ipa_all;
678 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
679 tdi = TDI_tree_all;
680 else
681 tdi = TDI_rtl_all;
682 /* Check if dump-all flag is specified. */
683 if (get_dump_file_info (tdi)->state)
684 get_dump_file_info (added_pass_nodes->pass->static_pass_number)
685 ->state = get_dump_file_info (tdi)->state;
686 XDELETE (added_pass_nodes);
687 added_pass_nodes = next_node;
691 /* Construct the pass tree. The sequencing of passes is driven by
692 the cgraph routines:
694 cgraph_finalize_compilation_unit ()
695 for each node N in the cgraph
696 cgraph_analyze_function (N)
697 cgraph_lower_function (N) -> all_lowering_passes
699 If we are optimizing, cgraph_optimize is then invoked:
701 cgraph_optimize ()
702 ipa_passes () -> all_small_ipa_passes
703 cgraph_expand_all_functions ()
704 for each node N in the cgraph
705 cgraph_expand_function (N)
706 tree_rest_of_compilation (DECL (N)) -> all_passes
709 void
710 init_optimization_passes (void)
712 struct opt_pass **p;
714 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &((PASS).pass)))
716 /* All passes needed to lower the function into shape optimizers can
717 operate on. These passes are always run first on the function, but
718 backend might produce already lowered functions that are not processed
719 by these passes. */
720 p = &all_lowering_passes;
721 NEXT_PASS (pass_warn_unused_result);
722 NEXT_PASS (pass_diagnose_omp_blocks);
723 NEXT_PASS (pass_mudflap_1);
724 NEXT_PASS (pass_lower_omp);
725 NEXT_PASS (pass_lower_cf);
726 NEXT_PASS (pass_refactor_eh);
727 NEXT_PASS (pass_lower_eh);
728 NEXT_PASS (pass_build_cfg);
729 NEXT_PASS (pass_warn_function_return);
730 NEXT_PASS (pass_build_cgraph_edges);
731 NEXT_PASS (pass_inline_parameters);
732 *p = NULL;
734 /* Interprocedural optimization passes. */
735 p = &all_small_ipa_passes;
736 NEXT_PASS (pass_ipa_free_lang_data);
737 NEXT_PASS (pass_ipa_function_and_variable_visibility);
738 NEXT_PASS (pass_early_local_passes);
740 struct opt_pass **p = &pass_early_local_passes.pass.sub;
741 NEXT_PASS (pass_fixup_cfg);
742 NEXT_PASS (pass_init_datastructures);
743 NEXT_PASS (pass_expand_omp);
745 NEXT_PASS (pass_referenced_vars);
746 NEXT_PASS (pass_build_ssa);
747 NEXT_PASS (pass_lower_vector);
748 NEXT_PASS (pass_early_warn_uninitialized);
749 /* Note that it is not strictly necessary to schedule an early
750 inline pass here. However, some test cases (e.g.,
751 g++.dg/other/p334435.C g++.dg/other/i386-1.C) expect extern
752 inline functions to be inlined even at -O0. This does not
753 happen during the first early inline pass. */
754 NEXT_PASS (pass_rebuild_cgraph_edges);
755 NEXT_PASS (pass_early_inline);
756 NEXT_PASS (pass_all_early_optimizations);
758 struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
759 NEXT_PASS (pass_remove_cgraph_callee_edges);
760 NEXT_PASS (pass_rename_ssa_copies);
761 NEXT_PASS (pass_ccp);
762 NEXT_PASS (pass_forwprop);
763 /* pass_build_ealias is a dummy pass that ensures that we
764 execute TODO_rebuild_alias at this point. Re-building
765 alias information also rewrites no longer addressed
766 locals into SSA form if possible. */
767 NEXT_PASS (pass_build_ealias);
768 NEXT_PASS (pass_sra_early);
769 NEXT_PASS (pass_copy_prop);
770 NEXT_PASS (pass_merge_phi);
771 NEXT_PASS (pass_cd_dce);
772 NEXT_PASS (pass_early_ipa_sra);
773 NEXT_PASS (pass_tail_recursion);
774 NEXT_PASS (pass_convert_switch);
775 NEXT_PASS (pass_cleanup_eh);
776 NEXT_PASS (pass_profile);
777 NEXT_PASS (pass_local_pure_const);
778 /* Split functions creates parts that are not run through
779 early optimizations again. It is thus good idea to do this
780 late. */
781 NEXT_PASS (pass_split_functions);
783 NEXT_PASS (pass_release_ssa_names);
784 NEXT_PASS (pass_rebuild_cgraph_edges);
785 NEXT_PASS (pass_inline_parameters);
787 NEXT_PASS (pass_ipa_tree_profile);
788 NEXT_PASS (pass_ipa_increase_alignment);
789 NEXT_PASS (pass_ipa_matrix_reorg);
790 NEXT_PASS (pass_ipa_lower_emutls);
791 *p = NULL;
793 p = &all_regular_ipa_passes;
794 NEXT_PASS (pass_ipa_whole_program_visibility);
795 NEXT_PASS (pass_ipa_profile);
796 NEXT_PASS (pass_ipa_cp);
797 NEXT_PASS (pass_ipa_cdtor_merge);
798 NEXT_PASS (pass_ipa_inline);
799 NEXT_PASS (pass_ipa_pure_const);
800 NEXT_PASS (pass_ipa_reference);
801 NEXT_PASS (pass_ipa_type_escape);
802 NEXT_PASS (pass_ipa_pta);
803 NEXT_PASS (pass_ipa_struct_reorg);
804 *p = NULL;
806 p = &all_lto_gen_passes;
807 NEXT_PASS (pass_ipa_lto_gimple_out);
808 NEXT_PASS (pass_ipa_lto_finish_out); /* This must be the last LTO pass. */
809 *p = NULL;
811 /* These passes are run after IPA passes on every function that is being
812 output to the assembler file. */
813 p = &all_passes;
814 NEXT_PASS (pass_lower_eh_dispatch);
815 NEXT_PASS (pass_all_optimizations);
817 struct opt_pass **p = &pass_all_optimizations.pass.sub;
818 NEXT_PASS (pass_remove_cgraph_callee_edges);
819 /* Initial scalar cleanups before alias computation.
820 They ensure memory accesses are not indirect wherever possible. */
821 NEXT_PASS (pass_strip_predict_hints);
822 NEXT_PASS (pass_rename_ssa_copies);
823 NEXT_PASS (pass_complete_unrolli);
824 NEXT_PASS (pass_ccp);
825 NEXT_PASS (pass_forwprop);
826 NEXT_PASS (pass_call_cdce);
827 /* pass_build_alias is a dummy pass that ensures that we
828 execute TODO_rebuild_alias at this point. Re-building
829 alias information also rewrites no longer addressed
830 locals into SSA form if possible. */
831 NEXT_PASS (pass_build_alias);
832 NEXT_PASS (pass_return_slot);
833 NEXT_PASS (pass_phiprop);
834 NEXT_PASS (pass_fre);
835 NEXT_PASS (pass_copy_prop);
836 NEXT_PASS (pass_merge_phi);
837 NEXT_PASS (pass_vrp);
838 NEXT_PASS (pass_dce);
839 NEXT_PASS (pass_cselim);
840 NEXT_PASS (pass_tree_ifcombine);
841 NEXT_PASS (pass_phiopt);
842 NEXT_PASS (pass_tail_recursion);
843 NEXT_PASS (pass_ch);
844 NEXT_PASS (pass_stdarg);
845 NEXT_PASS (pass_lower_complex);
846 NEXT_PASS (pass_sra);
847 NEXT_PASS (pass_rename_ssa_copies);
848 /* The dom pass will also resolve all __builtin_constant_p calls
849 that are still there to 0. This has to be done after some
850 propagations have already run, but before some more dead code
851 is removed, and this place fits nicely. Remember this when
852 trying to move or duplicate pass_dominator somewhere earlier. */
853 NEXT_PASS (pass_dominator);
854 /* The only const/copy propagation opportunities left after
855 DOM should be due to degenerate PHI nodes. So rather than
856 run the full propagators, run a specialized pass which
857 only examines PHIs to discover const/copy propagation
858 opportunities. */
859 NEXT_PASS (pass_phi_only_cprop);
860 NEXT_PASS (pass_dse);
861 NEXT_PASS (pass_reassoc);
862 NEXT_PASS (pass_dce);
863 NEXT_PASS (pass_forwprop);
864 NEXT_PASS (pass_phiopt);
865 NEXT_PASS (pass_object_sizes);
866 NEXT_PASS (pass_ccp);
867 NEXT_PASS (pass_copy_prop);
868 NEXT_PASS (pass_cse_sincos);
869 NEXT_PASS (pass_optimize_bswap);
870 NEXT_PASS (pass_split_crit_edges);
871 NEXT_PASS (pass_pre);
872 NEXT_PASS (pass_sink_code);
873 NEXT_PASS (pass_tree_loop);
875 struct opt_pass **p = &pass_tree_loop.pass.sub;
876 NEXT_PASS (pass_tree_loop_init);
877 NEXT_PASS (pass_lim);
878 NEXT_PASS (pass_copy_prop);
879 NEXT_PASS (pass_dce_loop);
880 NEXT_PASS (pass_tree_unswitch);
881 NEXT_PASS (pass_scev_cprop);
882 NEXT_PASS (pass_record_bounds);
883 NEXT_PASS (pass_check_data_deps);
884 NEXT_PASS (pass_loop_distribution);
885 NEXT_PASS (pass_linear_transform);
886 NEXT_PASS (pass_copy_prop);
887 NEXT_PASS (pass_graphite);
889 struct opt_pass **p = &pass_graphite.pass.sub;
890 NEXT_PASS (pass_graphite_transforms);
891 NEXT_PASS (pass_copy_prop);
892 NEXT_PASS (pass_dce_loop);
893 NEXT_PASS (pass_lim);
895 NEXT_PASS (pass_iv_canon);
896 NEXT_PASS (pass_if_conversion);
897 NEXT_PASS (pass_vectorize);
899 struct opt_pass **p = &pass_vectorize.pass.sub;
900 NEXT_PASS (pass_lower_vector_ssa);
901 NEXT_PASS (pass_dce_loop);
903 NEXT_PASS (pass_predcom);
904 NEXT_PASS (pass_complete_unroll);
905 NEXT_PASS (pass_slp_vectorize);
906 NEXT_PASS (pass_parallelize_loops);
907 NEXT_PASS (pass_loop_prefetch);
908 NEXT_PASS (pass_iv_optimize);
909 NEXT_PASS (pass_tree_loop_done);
911 NEXT_PASS (pass_cse_reciprocals);
912 NEXT_PASS (pass_reassoc);
913 NEXT_PASS (pass_vrp);
914 NEXT_PASS (pass_dominator);
915 /* The only const/copy propagation opportunities left after
916 DOM should be due to degenerate PHI nodes. So rather than
917 run the full propagators, run a specialized pass which
918 only examines PHIs to discover const/copy propagation
919 opportunities. */
920 NEXT_PASS (pass_phi_only_cprop);
921 NEXT_PASS (pass_cd_dce);
922 NEXT_PASS (pass_tracer);
924 /* FIXME: If DCE is not run before checking for uninitialized uses,
925 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
926 However, this also causes us to misdiagnose cases that should be
927 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
929 To fix the false positives in uninit-5.c, we would have to
930 account for the predicates protecting the set and the use of each
931 variable. Using a representation like Gated Single Assignment
932 may help. */
933 NEXT_PASS (pass_late_warn_uninitialized);
934 NEXT_PASS (pass_dse);
935 NEXT_PASS (pass_forwprop);
936 NEXT_PASS (pass_phiopt);
937 NEXT_PASS (pass_fold_builtins);
938 NEXT_PASS (pass_optimize_widening_mul);
939 NEXT_PASS (pass_tail_calls);
940 NEXT_PASS (pass_rename_ssa_copies);
941 NEXT_PASS (pass_uncprop);
942 NEXT_PASS (pass_local_pure_const);
944 NEXT_PASS (pass_lower_complex_O0);
945 NEXT_PASS (pass_cleanup_eh);
946 NEXT_PASS (pass_lower_resx);
947 NEXT_PASS (pass_nrv);
948 NEXT_PASS (pass_mudflap_2);
949 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
950 NEXT_PASS (pass_warn_function_noreturn);
952 NEXT_PASS (pass_expand);
954 NEXT_PASS (pass_rest_of_compilation);
956 struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
957 NEXT_PASS (pass_init_function);
958 NEXT_PASS (pass_jump);
959 NEXT_PASS (pass_rtl_eh);
960 NEXT_PASS (pass_initial_value_sets);
961 NEXT_PASS (pass_unshare_all_rtl);
962 NEXT_PASS (pass_instantiate_virtual_regs);
963 NEXT_PASS (pass_into_cfg_layout_mode);
964 NEXT_PASS (pass_jump2);
965 NEXT_PASS (pass_lower_subreg);
966 NEXT_PASS (pass_df_initialize_opt);
967 NEXT_PASS (pass_cse);
968 NEXT_PASS (pass_rtl_fwprop);
969 NEXT_PASS (pass_rtl_cprop);
970 NEXT_PASS (pass_rtl_pre);
971 NEXT_PASS (pass_rtl_hoist);
972 NEXT_PASS (pass_rtl_cprop);
973 NEXT_PASS (pass_rtl_store_motion);
974 NEXT_PASS (pass_cse_after_global_opts);
975 NEXT_PASS (pass_rtl_ifcvt);
976 NEXT_PASS (pass_reginfo_init);
977 /* Perform loop optimizations. It might be better to do them a bit
978 sooner, but we want the profile feedback to work more
979 efficiently. */
980 NEXT_PASS (pass_loop2);
982 struct opt_pass **p = &pass_loop2.pass.sub;
983 NEXT_PASS (pass_rtl_loop_init);
984 NEXT_PASS (pass_rtl_move_loop_invariants);
985 NEXT_PASS (pass_rtl_unswitch);
986 NEXT_PASS (pass_rtl_unroll_and_peel_loops);
987 NEXT_PASS (pass_rtl_doloop);
988 NEXT_PASS (pass_rtl_loop_done);
989 *p = NULL;
991 NEXT_PASS (pass_web);
992 NEXT_PASS (pass_rtl_cprop);
993 NEXT_PASS (pass_cse2);
994 NEXT_PASS (pass_rtl_dse1);
995 NEXT_PASS (pass_rtl_fwprop_addr);
996 NEXT_PASS (pass_inc_dec);
997 NEXT_PASS (pass_initialize_regs);
998 NEXT_PASS (pass_ud_rtl_dce);
999 NEXT_PASS (pass_combine);
1000 NEXT_PASS (pass_if_after_combine);
1001 NEXT_PASS (pass_partition_blocks);
1002 NEXT_PASS (pass_regmove);
1003 NEXT_PASS (pass_outof_cfg_layout_mode);
1004 NEXT_PASS (pass_split_all_insns);
1005 NEXT_PASS (pass_lower_subreg2);
1006 NEXT_PASS (pass_df_initialize_no_opt);
1007 NEXT_PASS (pass_stack_ptr_mod);
1008 NEXT_PASS (pass_mode_switching);
1009 NEXT_PASS (pass_match_asm_constraints);
1010 NEXT_PASS (pass_sms);
1011 NEXT_PASS (pass_sched);
1012 NEXT_PASS (pass_ira);
1013 NEXT_PASS (pass_postreload);
1015 struct opt_pass **p = &pass_postreload.pass.sub;
1016 NEXT_PASS (pass_postreload_cse);
1017 NEXT_PASS (pass_gcse2);
1018 NEXT_PASS (pass_split_after_reload);
1019 NEXT_PASS (pass_implicit_zee);
1020 NEXT_PASS (pass_branch_target_load_optimize1);
1021 NEXT_PASS (pass_thread_prologue_and_epilogue);
1022 NEXT_PASS (pass_rtl_dse2);
1023 NEXT_PASS (pass_stack_adjustments);
1024 NEXT_PASS (pass_peephole2);
1025 NEXT_PASS (pass_if_after_reload);
1026 NEXT_PASS (pass_regrename);
1027 NEXT_PASS (pass_cprop_hardreg);
1028 NEXT_PASS (pass_fast_rtl_dce);
1029 NEXT_PASS (pass_reorder_blocks);
1030 NEXT_PASS (pass_branch_target_load_optimize2);
1031 NEXT_PASS (pass_leaf_regs);
1032 NEXT_PASS (pass_split_before_sched2);
1033 NEXT_PASS (pass_sched2);
1034 NEXT_PASS (pass_stack_regs);
1036 struct opt_pass **p = &pass_stack_regs.pass.sub;
1037 NEXT_PASS (pass_split_before_regstack);
1038 NEXT_PASS (pass_stack_regs_run);
1040 NEXT_PASS (pass_compute_alignments);
1041 NEXT_PASS (pass_duplicate_computed_gotos);
1042 NEXT_PASS (pass_variable_tracking);
1043 NEXT_PASS (pass_free_cfg);
1044 NEXT_PASS (pass_machine_reorg);
1045 NEXT_PASS (pass_cleanup_barriers);
1046 NEXT_PASS (pass_delay_slots);
1047 NEXT_PASS (pass_split_for_shorten_branches);
1048 NEXT_PASS (pass_convert_to_eh_region_ranges);
1049 NEXT_PASS (pass_shorten_branches);
1050 NEXT_PASS (pass_set_nothrow_function_flags);
1051 NEXT_PASS (pass_final);
1053 NEXT_PASS (pass_df_finish);
1055 NEXT_PASS (pass_clean_state);
1056 *p = NULL;
1058 #undef NEXT_PASS
1060 /* Register the passes with the tree dump code. */
1061 register_dump_files (all_lowering_passes, PROP_gimple_any);
1062 register_dump_files (all_small_ipa_passes,
1063 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1064 | PROP_cfg);
1065 register_dump_files (all_regular_ipa_passes,
1066 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1067 | PROP_cfg);
1068 register_dump_files (all_lto_gen_passes,
1069 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1070 | PROP_cfg);
1071 register_dump_files (all_passes,
1072 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1073 | PROP_cfg);
1076 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1077 function CALLBACK for every function in the call graph. Otherwise,
1078 call CALLBACK on the current function. */
1080 static void
1081 do_per_function (void (*callback) (void *data), void *data)
1083 if (current_function_decl)
1084 callback (data);
1085 else
1087 struct cgraph_node *node;
1088 for (node = cgraph_nodes; node; node = node->next)
1089 if (node->analyzed && gimple_has_body_p (node->decl)
1090 && (!node->clone_of || node->decl != node->clone_of->decl))
1092 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1093 current_function_decl = node->decl;
1094 callback (data);
1095 if (!flag_wpa)
1097 free_dominance_info (CDI_DOMINATORS);
1098 free_dominance_info (CDI_POST_DOMINATORS);
1100 current_function_decl = NULL;
1101 pop_cfun ();
1102 ggc_collect ();
1107 /* Because inlining might remove no-longer reachable nodes, we need to
1108 keep the array visible to garbage collector to avoid reading collected
1109 out nodes. */
1110 static int nnodes;
1111 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1113 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1114 function CALLBACK for every function in the call graph. Otherwise,
1115 call CALLBACK on the current function.
1116 This function is global so that plugins can use it. */
1117 void
1118 do_per_function_toporder (void (*callback) (void *data), void *data)
1120 int i;
1122 if (current_function_decl)
1123 callback (data);
1124 else
1126 gcc_assert (!order);
1127 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1128 nnodes = cgraph_postorder (order);
1129 for (i = nnodes - 1; i >= 0; i--)
1130 order[i]->process = 1;
1131 for (i = nnodes - 1; i >= 0; i--)
1133 struct cgraph_node *node = order[i];
1135 /* Allow possibly removed nodes to be garbage collected. */
1136 order[i] = NULL;
1137 node->process = 0;
1138 if (node->analyzed)
1140 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1141 current_function_decl = node->decl;
1142 callback (data);
1143 free_dominance_info (CDI_DOMINATORS);
1144 free_dominance_info (CDI_POST_DOMINATORS);
1145 current_function_decl = NULL;
1146 pop_cfun ();
1147 ggc_collect ();
1151 ggc_free (order);
1152 order = NULL;
1153 nnodes = 0;
1156 /* Perform all TODO actions that ought to be done on each function. */
1158 static void
1159 execute_function_todo (void *data)
1161 unsigned int flags = (size_t)data;
1162 flags &= ~cfun->last_verified;
1163 if (!flags)
1164 return;
1166 /* Always cleanup the CFG before trying to update SSA. */
1167 if (flags & TODO_cleanup_cfg)
1169 bool cleanup = cleanup_tree_cfg ();
1171 if (cleanup && (cfun->curr_properties & PROP_ssa))
1172 flags |= TODO_remove_unused_locals;
1174 /* When cleanup_tree_cfg merges consecutive blocks, it may
1175 perform some simplistic propagation when removing single
1176 valued PHI nodes. This propagation may, in turn, cause the
1177 SSA form to become out-of-date (see PR 22037). So, even
1178 if the parent pass had not scheduled an SSA update, we may
1179 still need to do one. */
1180 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1181 flags |= TODO_update_ssa;
1184 if (flags & TODO_update_ssa_any)
1186 unsigned update_flags = flags & TODO_update_ssa_any;
1187 update_ssa (update_flags);
1188 cfun->last_verified &= ~TODO_verify_ssa;
1191 if (flags & TODO_rebuild_alias)
1193 execute_update_addresses_taken ();
1194 compute_may_aliases ();
1196 else if (optimize && (flags & TODO_update_address_taken))
1197 execute_update_addresses_taken ();
1199 if (flags & TODO_remove_unused_locals)
1200 remove_unused_locals ();
1202 if ((flags & TODO_dump_func) && dump_file && current_function_decl)
1204 if (cfun->curr_properties & PROP_trees)
1205 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1206 else
1208 if (dump_flags & TDF_SLIM)
1209 print_rtl_slim_with_bb (dump_file, get_insns (), dump_flags);
1210 else if ((cfun->curr_properties & PROP_cfg)
1211 && (dump_flags & TDF_BLOCKS))
1212 print_rtl_with_bb (dump_file, get_insns ());
1213 else
1214 print_rtl (dump_file, get_insns ());
1216 if ((cfun->curr_properties & PROP_cfg)
1217 && graph_dump_format != no_graph
1218 && (dump_flags & TDF_GRAPH))
1219 print_rtl_graph_with_bb (dump_file_name, get_insns ());
1222 /* Flush the file. If verification fails, we won't be able to
1223 close the file before aborting. */
1224 fflush (dump_file);
1227 if (flags & TODO_rebuild_frequencies)
1228 rebuild_frequencies ();
1230 /* If we've seen errors do not bother running any verifiers. */
1231 if (seen_error ())
1232 return;
1234 #if defined ENABLE_CHECKING
1235 if (flags & TODO_verify_ssa
1236 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1237 verify_ssa (true);
1238 if (flags & TODO_verify_flow)
1239 verify_flow_info ();
1240 if (flags & TODO_verify_stmts)
1241 verify_stmts ();
1242 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1243 verify_loop_closed_ssa (false);
1244 if (flags & TODO_verify_rtl_sharing)
1245 verify_rtl_sharing ();
1246 #endif
1248 cfun->last_verified = flags & TODO_verify_all;
1251 /* Perform all TODO actions. */
1252 static void
1253 execute_todo (unsigned int flags)
1255 #if defined ENABLE_CHECKING
1256 if (cfun
1257 && need_ssa_update_p (cfun))
1258 gcc_assert (flags & TODO_update_ssa_any);
1259 #endif
1261 timevar_push (TV_TODO);
1263 /* Inform the pass whether it is the first time it is run. */
1264 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1266 statistics_fini_pass ();
1268 do_per_function (execute_function_todo, (void *)(size_t) flags);
1270 /* Always remove functions just as before inlining: IPA passes might be
1271 interested to see bodies of extern inline functions that are not inlined
1272 to analyze side effects. The full removal is done just at the end
1273 of IPA pass queue. */
1274 if (flags & TODO_remove_functions)
1276 gcc_assert (!cfun);
1277 cgraph_remove_unreachable_nodes (true, dump_file);
1280 if ((flags & TODO_dump_cgraph) && dump_file && !current_function_decl)
1282 gcc_assert (!cfun);
1283 dump_cgraph (dump_file);
1284 /* Flush the file. If verification fails, we won't be able to
1285 close the file before aborting. */
1286 fflush (dump_file);
1289 if (flags & TODO_ggc_collect)
1290 ggc_collect ();
1292 /* Now that the dumping has been done, we can get rid of the optional
1293 df problems. */
1294 if (flags & TODO_df_finish)
1295 df_finish_pass ((flags & TODO_df_verify) != 0);
1297 timevar_pop (TV_TODO);
1300 /* Verify invariants that should hold between passes. This is a place
1301 to put simple sanity checks. */
1303 static void
1304 verify_interpass_invariants (void)
1306 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1309 /* Clear the last verified flag. */
1311 static void
1312 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1314 cfun->last_verified = 0;
1317 /* Helper function. Verify that the properties has been turn into the
1318 properties expected by the pass. */
1320 #ifdef ENABLE_CHECKING
1321 static void
1322 verify_curr_properties (void *data)
1324 unsigned int props = (size_t)data;
1325 gcc_assert ((cfun->curr_properties & props) == props);
1327 #endif
1329 /* Initialize pass dump file. */
1330 /* This is non-static so that the plugins can use it. */
1332 bool
1333 pass_init_dump_file (struct opt_pass *pass)
1335 /* If a dump file name is present, open it if enabled. */
1336 if (pass->static_pass_number != -1)
1338 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
1339 dump_file_name = get_dump_file_name (pass->static_pass_number);
1340 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
1341 if (dump_file && current_function_decl)
1343 const char *dname, *aname;
1344 struct cgraph_node *node = cgraph_node (current_function_decl);
1345 dname = lang_hooks.decl_printable_name (current_function_decl, 2);
1346 aname = (IDENTIFIER_POINTER
1347 (DECL_ASSEMBLER_NAME (current_function_decl)));
1348 fprintf (dump_file, "\n;; Function %s (%s)%s\n\n", dname, aname,
1349 node->frequency == NODE_FREQUENCY_HOT
1350 ? " (hot)"
1351 : node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
1352 ? " (unlikely executed)"
1353 : node->frequency == NODE_FREQUENCY_EXECUTED_ONCE
1354 ? " (executed once)"
1355 : "");
1357 return initializing_dump;
1359 else
1360 return false;
1363 /* Flush PASS dump file. */
1364 /* This is non-static so that plugins can use it. */
1366 void
1367 pass_fini_dump_file (struct opt_pass *pass)
1369 /* Flush and close dump file. */
1370 if (dump_file_name)
1372 free (CONST_CAST (char *, dump_file_name));
1373 dump_file_name = NULL;
1376 if (dump_file)
1378 dump_end (pass->static_pass_number, dump_file);
1379 dump_file = NULL;
1383 /* After executing the pass, apply expected changes to the function
1384 properties. */
1386 static void
1387 update_properties_after_pass (void *data)
1389 struct opt_pass *pass = (struct opt_pass *) data;
1390 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1391 & ~pass->properties_destroyed;
1394 /* Execute summary generation for all of the passes in IPA_PASS. */
1396 void
1397 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1399 while (ipa_pass)
1401 struct opt_pass *pass = &ipa_pass->pass;
1403 /* Execute all of the IPA_PASSes in the list. */
1404 if (ipa_pass->pass.type == IPA_PASS
1405 && (!pass->gate || pass->gate ())
1406 && ipa_pass->generate_summary)
1408 pass_init_dump_file (pass);
1410 /* If a timevar is present, start it. */
1411 if (pass->tv_id)
1412 timevar_push (pass->tv_id);
1414 ipa_pass->generate_summary ();
1416 /* Stop timevar. */
1417 if (pass->tv_id)
1418 timevar_pop (pass->tv_id);
1420 pass_fini_dump_file (pass);
1422 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
1426 /* Execute IPA_PASS function transform on NODE. */
1428 static void
1429 execute_one_ipa_transform_pass (struct cgraph_node *node,
1430 struct ipa_opt_pass_d *ipa_pass)
1432 struct opt_pass *pass = &ipa_pass->pass;
1433 unsigned int todo_after = 0;
1435 current_pass = pass;
1436 if (!ipa_pass->function_transform)
1437 return;
1439 /* Note that the folders should only create gimple expressions.
1440 This is a hack until the new folder is ready. */
1441 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1443 pass_init_dump_file (pass);
1445 /* Run pre-pass verification. */
1446 execute_todo (ipa_pass->function_transform_todo_flags_start);
1448 /* If a timevar is present, start it. */
1449 if (pass->tv_id != TV_NONE)
1450 timevar_push (pass->tv_id);
1452 /* Do it! */
1453 todo_after = ipa_pass->function_transform (node);
1455 /* Stop timevar. */
1456 if (pass->tv_id != TV_NONE)
1457 timevar_pop (pass->tv_id);
1459 /* Run post-pass cleanup and verification. */
1460 execute_todo (todo_after);
1461 verify_interpass_invariants ();
1463 pass_fini_dump_file (pass);
1465 current_pass = NULL;
1468 /* For the current function, execute all ipa transforms. */
1470 void
1471 execute_all_ipa_transforms (void)
1473 struct cgraph_node *node;
1474 if (!cfun)
1475 return;
1476 node = cgraph_node (current_function_decl);
1478 if (node->ipa_transforms_to_apply)
1480 unsigned int i;
1482 for (i = 0; i < VEC_length (ipa_opt_pass, node->ipa_transforms_to_apply);
1483 i++)
1484 execute_one_ipa_transform_pass (node,
1485 VEC_index (ipa_opt_pass,
1486 node->ipa_transforms_to_apply,
1487 i));
1488 VEC_free (ipa_opt_pass, heap, node->ipa_transforms_to_apply);
1489 node->ipa_transforms_to_apply = NULL;
1493 /* Execute PASS. */
1495 bool
1496 execute_one_pass (struct opt_pass *pass)
1498 bool initializing_dump;
1499 unsigned int todo_after = 0;
1501 bool gate_status;
1503 /* IPA passes are executed on whole program, so cfun should be NULL.
1504 Other passes need function context set. */
1505 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
1506 gcc_assert (!cfun && !current_function_decl);
1507 else
1508 gcc_assert (cfun && current_function_decl);
1510 current_pass = pass;
1512 /* Check whether gate check should be avoided.
1513 User controls the value of the gate through the parameter "gate_status". */
1514 gate_status = (pass->gate == NULL) ? true : pass->gate();
1516 /* Override gate with plugin. */
1517 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
1519 if (!gate_status)
1521 current_pass = NULL;
1522 return false;
1525 /* Pass execution event trigger: useful to identify passes being
1526 executed. */
1527 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
1529 if (!quiet_flag && !cfun)
1530 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
1532 /* Note that the folders should only create gimple expressions.
1533 This is a hack until the new folder is ready. */
1534 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1536 initializing_dump = pass_init_dump_file (pass);
1538 /* Run pre-pass verification. */
1539 execute_todo (pass->todo_flags_start);
1541 #ifdef ENABLE_CHECKING
1542 do_per_function (verify_curr_properties,
1543 (void *)(size_t)pass->properties_required);
1544 #endif
1546 /* If a timevar is present, start it. */
1547 if (pass->tv_id != TV_NONE)
1548 timevar_push (pass->tv_id);
1550 /* Do it! */
1551 if (pass->execute)
1553 todo_after = pass->execute ();
1554 do_per_function (clear_last_verified, NULL);
1557 /* Stop timevar. */
1558 if (pass->tv_id != TV_NONE)
1559 timevar_pop (pass->tv_id);
1561 do_per_function (update_properties_after_pass, pass);
1563 if (initializing_dump
1564 && dump_file
1565 && graph_dump_format != no_graph
1566 && cfun
1567 && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
1568 == (PROP_cfg | PROP_rtl))
1570 get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
1571 dump_flags |= TDF_GRAPH;
1572 clean_graph_dump_file (dump_file_name);
1575 /* Run post-pass cleanup and verification. */
1576 execute_todo (todo_after | pass->todo_flags_finish);
1577 verify_interpass_invariants ();
1578 if (pass->type == IPA_PASS)
1580 struct cgraph_node *node;
1581 for (node = cgraph_nodes; node; node = node->next)
1582 if (node->analyzed)
1583 VEC_safe_push (ipa_opt_pass, heap, node->ipa_transforms_to_apply,
1584 (struct ipa_opt_pass_d *)pass);
1587 if (!current_function_decl)
1588 cgraph_process_new_functions ();
1590 pass_fini_dump_file (pass);
1592 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
1593 gcc_assert (!(cfun->curr_properties & PROP_trees)
1594 || pass->type != RTL_PASS);
1596 current_pass = NULL;
1598 return true;
1601 void
1602 execute_pass_list (struct opt_pass *pass)
1606 gcc_assert (pass->type == GIMPLE_PASS
1607 || pass->type == RTL_PASS);
1608 if (execute_one_pass (pass) && pass->sub)
1609 execute_pass_list (pass->sub);
1610 pass = pass->next;
1612 while (pass);
1615 /* Same as execute_pass_list but assume that subpasses of IPA passes
1616 are local passes. If SET is not NULL, write out summaries of only
1617 those node in SET. */
1619 static void
1620 ipa_write_summaries_2 (struct opt_pass *pass, cgraph_node_set set,
1621 varpool_node_set vset,
1622 struct lto_out_decl_state *state)
1624 while (pass)
1626 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
1627 gcc_assert (!current_function_decl);
1628 gcc_assert (!cfun);
1629 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1630 if (pass->type == IPA_PASS
1631 && ipa_pass->write_summary
1632 && (!pass->gate || pass->gate ()))
1634 /* If a timevar is present, start it. */
1635 if (pass->tv_id)
1636 timevar_push (pass->tv_id);
1638 pass_init_dump_file (pass);
1640 ipa_pass->write_summary (set,vset);
1642 pass_fini_dump_file (pass);
1644 /* If a timevar is present, start it. */
1645 if (pass->tv_id)
1646 timevar_pop (pass->tv_id);
1649 if (pass->sub && pass->sub->type != GIMPLE_PASS)
1650 ipa_write_summaries_2 (pass->sub, set, vset, state);
1652 pass = pass->next;
1656 /* Helper function of ipa_write_summaries. Creates and destroys the
1657 decl state and calls ipa_write_summaries_2 for all passes that have
1658 summaries. SET is the set of nodes to be written. */
1660 static void
1661 ipa_write_summaries_1 (cgraph_node_set set, varpool_node_set vset)
1663 struct lto_out_decl_state *state = lto_new_out_decl_state ();
1664 compute_ltrans_boundary (state, set, vset);
1666 lto_push_out_decl_state (state);
1668 gcc_assert (!flag_wpa);
1669 ipa_write_summaries_2 (all_regular_ipa_passes, set, vset, state);
1670 ipa_write_summaries_2 (all_lto_gen_passes, set, vset, state);
1672 gcc_assert (lto_get_out_decl_state () == state);
1673 lto_pop_out_decl_state ();
1674 lto_delete_out_decl_state (state);
1677 /* Write out summaries for all the nodes in the callgraph. */
1679 void
1680 ipa_write_summaries (void)
1682 cgraph_node_set set;
1683 varpool_node_set vset;
1684 struct cgraph_node **order;
1685 struct varpool_node *vnode;
1686 int i, order_pos;
1688 if (!flag_generate_lto || seen_error ())
1689 return;
1691 set = cgraph_node_set_new ();
1693 /* Create the callgraph set in the same order used in
1694 cgraph_expand_all_functions. This mostly facilitates debugging,
1695 since it causes the gimple file to be processed in the same order
1696 as the source code. */
1697 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1698 order_pos = cgraph_postorder (order);
1699 gcc_assert (order_pos == cgraph_n_nodes);
1701 for (i = order_pos - 1; i >= 0; i--)
1703 struct cgraph_node *node = order[i];
1705 if (node->analyzed)
1707 /* When streaming out references to statements as part of some IPA
1708 pass summary, the statements need to have uids assigned and the
1709 following does that for all the IPA passes here. Naturally, this
1710 ordering then matches the one IPA-passes get in their stmt_fixup
1711 hooks. */
1713 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1714 renumber_gimple_stmt_uids ();
1715 pop_cfun ();
1717 if (node->analyzed)
1718 cgraph_node_set_add (set, node);
1720 vset = varpool_node_set_new ();
1722 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1723 if (vnode->needed && !vnode->alias)
1724 varpool_node_set_add (vset, vnode);
1726 ipa_write_summaries_1 (set, vset);
1728 free (order);
1729 ggc_free (set);
1730 ggc_free (vset);
1733 /* Same as execute_pass_list but assume that subpasses of IPA passes
1734 are local passes. If SET is not NULL, write out optimization summaries of
1735 only those node in SET. */
1737 static void
1738 ipa_write_optimization_summaries_1 (struct opt_pass *pass, cgraph_node_set set,
1739 varpool_node_set vset,
1740 struct lto_out_decl_state *state)
1742 while (pass)
1744 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
1745 gcc_assert (!current_function_decl);
1746 gcc_assert (!cfun);
1747 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1748 if (pass->type == IPA_PASS
1749 && ipa_pass->write_optimization_summary
1750 && (!pass->gate || pass->gate ()))
1752 /* If a timevar is present, start it. */
1753 if (pass->tv_id)
1754 timevar_push (pass->tv_id);
1756 pass_init_dump_file (pass);
1758 ipa_pass->write_optimization_summary (set, vset);
1760 pass_fini_dump_file (pass);
1762 /* If a timevar is present, start it. */
1763 if (pass->tv_id)
1764 timevar_pop (pass->tv_id);
1767 if (pass->sub && pass->sub->type != GIMPLE_PASS)
1768 ipa_write_optimization_summaries_1 (pass->sub, set, vset, state);
1770 pass = pass->next;
1774 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
1775 NULL, write out all summaries of all nodes. */
1777 void
1778 ipa_write_optimization_summaries (cgraph_node_set set, varpool_node_set vset)
1780 struct lto_out_decl_state *state = lto_new_out_decl_state ();
1781 cgraph_node_set_iterator csi;
1782 compute_ltrans_boundary (state, set, vset);
1784 lto_push_out_decl_state (state);
1785 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
1787 struct cgraph_node *node = csi_node (csi);
1788 /* When streaming out references to statements as part of some IPA
1789 pass summary, the statements need to have uids assigned.
1791 For functions newly born at WPA stage we need to initialize
1792 the uids here. */
1793 if (node->analyzed
1794 && gimple_has_body_p (node->decl))
1796 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1797 renumber_gimple_stmt_uids ();
1798 pop_cfun ();
1802 gcc_assert (flag_wpa);
1803 ipa_write_optimization_summaries_1 (all_regular_ipa_passes, set, vset, state);
1804 ipa_write_optimization_summaries_1 (all_lto_gen_passes, set, vset, state);
1806 gcc_assert (lto_get_out_decl_state () == state);
1807 lto_pop_out_decl_state ();
1808 lto_delete_out_decl_state (state);
1811 /* Same as execute_pass_list but assume that subpasses of IPA passes
1812 are local passes. */
1814 static void
1815 ipa_read_summaries_1 (struct opt_pass *pass)
1817 while (pass)
1819 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
1821 gcc_assert (!current_function_decl);
1822 gcc_assert (!cfun);
1823 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1825 if (pass->gate == NULL || pass->gate ())
1827 if (pass->type == IPA_PASS && ipa_pass->read_summary)
1829 /* If a timevar is present, start it. */
1830 if (pass->tv_id)
1831 timevar_push (pass->tv_id);
1833 pass_init_dump_file (pass);
1835 ipa_pass->read_summary ();
1837 pass_fini_dump_file (pass);
1839 /* Stop timevar. */
1840 if (pass->tv_id)
1841 timevar_pop (pass->tv_id);
1844 if (pass->sub && pass->sub->type != GIMPLE_PASS)
1845 ipa_read_summaries_1 (pass->sub);
1847 pass = pass->next;
1852 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
1854 void
1855 ipa_read_summaries (void)
1857 ipa_read_summaries_1 (all_regular_ipa_passes);
1858 ipa_read_summaries_1 (all_lto_gen_passes);
1861 /* Same as execute_pass_list but assume that subpasses of IPA passes
1862 are local passes. */
1864 static void
1865 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
1867 while (pass)
1869 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
1871 gcc_assert (!current_function_decl);
1872 gcc_assert (!cfun);
1873 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1875 if (pass->gate == NULL || pass->gate ())
1877 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
1879 /* If a timevar is present, start it. */
1880 if (pass->tv_id)
1881 timevar_push (pass->tv_id);
1883 pass_init_dump_file (pass);
1885 ipa_pass->read_optimization_summary ();
1887 pass_fini_dump_file (pass);
1889 /* Stop timevar. */
1890 if (pass->tv_id)
1891 timevar_pop (pass->tv_id);
1894 if (pass->sub && pass->sub->type != GIMPLE_PASS)
1895 ipa_read_optimization_summaries_1 (pass->sub);
1897 pass = pass->next;
1901 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
1903 void
1904 ipa_read_optimization_summaries (void)
1906 ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
1907 ipa_read_optimization_summaries_1 (all_lto_gen_passes);
1910 /* Same as execute_pass_list but assume that subpasses of IPA passes
1911 are local passes. */
1912 void
1913 execute_ipa_pass_list (struct opt_pass *pass)
1917 gcc_assert (!current_function_decl);
1918 gcc_assert (!cfun);
1919 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1920 if (execute_one_pass (pass) && pass->sub)
1922 if (pass->sub->type == GIMPLE_PASS)
1924 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
1925 do_per_function_toporder ((void (*)(void *))execute_pass_list,
1926 pass->sub);
1927 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
1929 else if (pass->sub->type == SIMPLE_IPA_PASS
1930 || pass->sub->type == IPA_PASS)
1931 execute_ipa_pass_list (pass->sub);
1932 else
1933 gcc_unreachable ();
1935 gcc_assert (!current_function_decl);
1936 cgraph_process_new_functions ();
1937 pass = pass->next;
1939 while (pass);
1942 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
1944 static void
1945 execute_ipa_stmt_fixups (struct opt_pass *pass,
1946 struct cgraph_node *node, gimple *stmts)
1948 while (pass)
1950 /* Execute all of the IPA_PASSes in the list. */
1951 if (pass->type == IPA_PASS
1952 && (!pass->gate || pass->gate ()))
1954 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
1956 if (ipa_pass->stmt_fixup)
1958 pass_init_dump_file (pass);
1959 /* If a timevar is present, start it. */
1960 if (pass->tv_id)
1961 timevar_push (pass->tv_id);
1963 ipa_pass->stmt_fixup (node, stmts);
1965 /* Stop timevar. */
1966 if (pass->tv_id)
1967 timevar_pop (pass->tv_id);
1968 pass_fini_dump_file (pass);
1970 if (pass->sub)
1971 execute_ipa_stmt_fixups (pass->sub, node, stmts);
1973 pass = pass->next;
1977 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
1979 void
1980 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
1982 execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
1986 extern void debug_properties (unsigned int);
1987 extern void dump_properties (FILE *, unsigned int);
1989 DEBUG_FUNCTION void
1990 dump_properties (FILE *dump, unsigned int props)
1992 fprintf (dump, "Properties:\n");
1993 if (props & PROP_gimple_any)
1994 fprintf (dump, "PROP_gimple_any\n");
1995 if (props & PROP_gimple_lcf)
1996 fprintf (dump, "PROP_gimple_lcf\n");
1997 if (props & PROP_gimple_leh)
1998 fprintf (dump, "PROP_gimple_leh\n");
1999 if (props & PROP_cfg)
2000 fprintf (dump, "PROP_cfg\n");
2001 if (props & PROP_referenced_vars)
2002 fprintf (dump, "PROP_referenced_vars\n");
2003 if (props & PROP_ssa)
2004 fprintf (dump, "PROP_ssa\n");
2005 if (props & PROP_no_crit_edges)
2006 fprintf (dump, "PROP_no_crit_edges\n");
2007 if (props & PROP_rtl)
2008 fprintf (dump, "PROP_rtl\n");
2009 if (props & PROP_gimple_lomp)
2010 fprintf (dump, "PROP_gimple_lomp\n");
2011 if (props & PROP_gimple_lcx)
2012 fprintf (dump, "PROP_gimple_lcx\n");
2013 if (props & PROP_cfglayout)
2014 fprintf (dump, "PROP_cfglayout\n");
2017 DEBUG_FUNCTION void
2018 debug_properties (unsigned int props)
2020 dump_properties (stderr, props);
2023 /* Called by local passes to see if function is called by already processed nodes.
2024 Because we process nodes in topological order, this means that function is
2025 in recursive cycle or we introduced new direct calls. */
2026 bool
2027 function_called_by_processed_nodes_p (void)
2029 struct cgraph_edge *e;
2030 for (e = cgraph_node (current_function_decl)->callers; e; e = e->next_caller)
2032 if (e->caller->decl == current_function_decl)
2033 continue;
2034 if (!e->caller->analyzed)
2035 continue;
2036 if (TREE_ASM_WRITTEN (e->caller->decl))
2037 continue;
2038 if (!e->caller->process && !e->caller->global.inlined_to)
2039 break;
2041 if (dump_file && e)
2043 fprintf (dump_file, "Already processed call to:\n");
2044 dump_cgraph_node (dump_file, e->caller);
2046 return e != NULL;
2049 #include "gt-passes.h"