]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/passes.c
backport: As described in http://gcc.gnu.org/ml/gcc/2012-08/msg00015.html...
[thirdparty/gcc.git] / gcc / passes.c
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.
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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. */
26
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 */
74
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;
79
80 static void register_pass_name (struct opt_pass *, const char *);
81
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)
87 {
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");
93 }
94
95
96 /* Call from the debugger to get the current pass name. */
97 DEBUG_FUNCTION void
98 debug_pass (void)
99 {
100 print_current_pass (stderr);
101 }
102
103
104
105 /* Global variables used to communicate with passes. */
106 int dump_flags;
107 bool in_gimple_form;
108 bool first_pass_instance;
109
110
111 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
112 and TYPE_DECL nodes.
113
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).
119
120 DECL is the declaration. TOP_LEVEL is nonzero
121 if this declaration is not within a function. */
122
123 void
124 rest_of_decl_compilation (tree decl,
125 int top_level,
126 int at_end)
127 {
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)
131 {
132 tree alias;
133 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
134 if (alias)
135 {
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);
144 }
145 }
146
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);
151
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)
156 {
157 timevar_push (TV_VARCONST);
158
159 /* Don't output anything when a tentative file-scope definition
160 is seen. But at end of compilation, do output code for them.
161
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))
170 {
171 /* When reading LTO unit, we also read varpool, so do not
172 rebuild it. */
173 if (in_lto_p && !at_end)
174 ;
175 else if (TREE_CODE (decl) != FUNCTION_DECL)
176 varpool_finalize_decl (decl);
177 }
178
179 #ifdef ASM_FINISH_DECLARE_OBJECT
180 if (decl == last_assemble_variable_decl)
181 {
182 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
183 top_level, at_end);
184 }
185 #endif
186
187 timevar_pop (TV_VARCONST);
188 }
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 ())
193 {
194 timevar_push (TV_SYMOUT);
195 debug_hooks->type_decl (decl, !top_level);
196 timevar_pop (TV_SYMOUT);
197 }
198
199 /* Let cgraph know about the existence of variables. */
200 if (in_lto_p && !at_end)
201 ;
202 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
203 && TREE_STATIC (decl))
204 varpool_node (decl);
205 }
206
207 /* Called after finishing a record, union or enumeral type. */
208
209 void
210 rest_of_type_compilation (tree type, int toplev)
211 {
212 /* Avoid confusing the debug information machinery when there are
213 errors. */
214 if (seen_error ())
215 return;
216
217 timevar_push (TV_SYMOUT);
218 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
219 timevar_pop (TV_SYMOUT);
220 }
221
222 \f
223
224 void
225 finish_optimization_passes (void)
226 {
227 int i;
228 struct dump_file_info *dfi;
229 char *name;
230
231 timevar_push (TV_DUMP);
232 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
233 {
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);
238 }
239
240 if (optimize > 0)
241 {
242 dump_file = dump_begin (pass_combine.pass.static_pass_number, NULL);
243 if (dump_file)
244 {
245 dump_combine_total_stats (dump_file);
246 dump_end (pass_combine.pass.static_pass_number, dump_file);
247 }
248 }
249
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)
256 {
257 finish_graph_dump_file (name);
258 free (name);
259 }
260
261 timevar_pop (TV_DUMP);
262 }
263
264 static unsigned int
265 execute_all_early_local_passes (void)
266 {
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;
276 }
277
278 /* Gate: execute, or not, all of the non-trivial optimizations. */
279
280 static bool
281 gate_all_early_local_passes (void)
282 {
283 /* Don't bother doing anything if the program has errors. */
284 return (!seen_error () && !in_lto_p);
285 }
286
287 struct simple_ipa_opt_pass pass_early_local_passes =
288 {
289 {
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 */
303 }
304 };
305
306 /* Gate: execute, or not, all of the non-trivial optimizations. */
307
308 static bool
309 gate_all_early_optimizations (void)
310 {
311 return (optimize >= 1
312 /* Don't bother doing anything if the program has errors. */
313 && !seen_error ());
314 }
315
316 static struct gimple_opt_pass pass_all_early_optimizations =
317 {
318 {
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 */
332 }
333 };
334
335 /* Gate: execute, or not, all of the non-trivial optimizations. */
336
337 static bool
338 gate_all_optimizations (void)
339 {
340 return (optimize >= 1
341 /* Don't bother doing anything if the program has errors.
342 We have to pass down the queue if we already went into SSA */
343 && (!seen_error () || gimple_in_ssa_p (cfun)));
344 }
345
346 static struct gimple_opt_pass pass_all_optimizations =
347 {
348 {
349 GIMPLE_PASS,
350 "*all_optimizations", /* name */
351 gate_all_optimizations, /* gate */
352 NULL, /* execute */
353 NULL, /* sub */
354 NULL, /* next */
355 0, /* static_pass_number */
356 TV_OPTIMIZE, /* tv_id */
357 0, /* properties_required */
358 0, /* properties_provided */
359 0, /* properties_destroyed */
360 0, /* todo_flags_start */
361 0 /* todo_flags_finish */
362 }
363 };
364
365 static bool
366 gate_rest_of_compilation (void)
367 {
368 /* Early return if there were errors. We can run afoul of our
369 consistency checks, and there's not really much point in fixing them. */
370 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
371 }
372
373 static struct rtl_opt_pass pass_rest_of_compilation =
374 {
375 {
376 RTL_PASS,
377 "*rest_of_compilation", /* name */
378 gate_rest_of_compilation, /* gate */
379 NULL, /* execute */
380 NULL, /* sub */
381 NULL, /* next */
382 0, /* static_pass_number */
383 TV_REST_OF_COMPILATION, /* tv_id */
384 PROP_rtl, /* properties_required */
385 0, /* properties_provided */
386 0, /* properties_destroyed */
387 0, /* todo_flags_start */
388 TODO_ggc_collect /* todo_flags_finish */
389 }
390 };
391
392 static bool
393 gate_postreload (void)
394 {
395 return reload_completed;
396 }
397
398 static struct rtl_opt_pass pass_postreload =
399 {
400 {
401 RTL_PASS,
402 "*all-postreload", /* name */
403 gate_postreload, /* gate */
404 NULL, /* execute */
405 NULL, /* sub */
406 NULL, /* next */
407 0, /* static_pass_number */
408 TV_POSTRELOAD, /* tv_id */
409 PROP_rtl, /* properties_required */
410 0, /* properties_provided */
411 0, /* properties_destroyed */
412 0, /* todo_flags_start */
413 TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
414 }
415 };
416
417
418
419 /* The root of the compilation pass tree, once constructed. */
420 struct opt_pass *all_passes, *all_small_ipa_passes, *all_lowering_passes,
421 *all_regular_ipa_passes, *all_late_ipa_passes, *all_lto_gen_passes;
422
423 /* This is used by plugins, and should also be used in register_pass. */
424 #define DEF_PASS_LIST(LIST) &LIST,
425 struct opt_pass **gcc_pass_lists[] = { GCC_PASS_LISTS NULL };
426 #undef DEF_PASS_LIST
427
428 /* A map from static pass id to optimization pass. */
429 struct opt_pass **passes_by_id;
430 int passes_by_id_size;
431
432 /* Set the static pass number of pass PASS to ID and record that
433 in the mapping from static pass number to pass. */
434
435 static void
436 set_pass_for_id (int id, struct opt_pass *pass)
437 {
438 pass->static_pass_number = id;
439 if (passes_by_id_size <= id)
440 {
441 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
442 memset (passes_by_id + passes_by_id_size, 0,
443 (id + 1 - passes_by_id_size) * sizeof (void *));
444 passes_by_id_size = id + 1;
445 }
446 passes_by_id[id] = pass;
447 }
448
449 /* Return the pass with the static pass number ID. */
450
451 struct opt_pass *
452 get_pass_for_id (int id)
453 {
454 if (id >= passes_by_id_size)
455 return NULL;
456 return passes_by_id[id];
457 }
458
459 /* Iterate over the pass tree allocating dump file numbers. We want
460 to do this depth first, and independent of whether the pass is
461 enabled or not. */
462
463 void
464 register_one_dump_file (struct opt_pass *pass)
465 {
466 char *dot_name, *flag_name, *glob_name;
467 const char *name, *full_name, *prefix;
468 char num[10];
469 int flags, id;
470
471 /* See below in next_pass_1. */
472 num[0] = '\0';
473 if (pass->static_pass_number != -1)
474 sprintf (num, "%d", ((int) pass->static_pass_number < 0
475 ? 1 : pass->static_pass_number));
476
477 /* The name is both used to identify the pass for the purposes of plugins,
478 and to specify dump file name and option.
479 The latter two might want something short which is not quite unique; for
480 that reason, we may have a disambiguating prefix, followed by a space
481 to mark the start of the following dump file name / option string. */
482 name = strchr (pass->name, ' ');
483 name = name ? name + 1 : pass->name;
484 dot_name = concat (".", name, num, NULL);
485 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
486 prefix = "ipa-", flags = TDF_IPA;
487 else if (pass->type == GIMPLE_PASS)
488 prefix = "tree-", flags = TDF_TREE;
489 else
490 prefix = "rtl-", flags = TDF_RTL;
491
492 flag_name = concat (prefix, name, num, NULL);
493 glob_name = concat (prefix, name, NULL);
494 id = dump_register (dot_name, flag_name, glob_name, flags);
495 set_pass_for_id (id, pass);
496 full_name = concat (prefix, pass->name, num, NULL);
497 register_pass_name (pass, full_name);
498 free (CONST_CAST (char *, full_name));
499 }
500
501 /* Recursive worker function for register_dump_files. */
502
503 static int
504 register_dump_files_1 (struct opt_pass *pass, int properties)
505 {
506 do
507 {
508 int new_properties = (properties | pass->properties_provided)
509 & ~pass->properties_destroyed;
510
511 if (pass->name && pass->name[0] != '*')
512 register_one_dump_file (pass);
513
514 if (pass->sub)
515 new_properties = register_dump_files_1 (pass->sub, new_properties);
516
517 /* If we have a gate, combine the properties that we could have with
518 and without the pass being examined. */
519 if (pass->gate)
520 properties &= new_properties;
521 else
522 properties = new_properties;
523
524 pass = pass->next;
525 }
526 while (pass);
527
528 return properties;
529 }
530
531 /* Register the dump files for the pipeline starting at PASS.
532 PROPERTIES reflects the properties that are guaranteed to be available at
533 the beginning of the pipeline. */
534
535 static void
536 register_dump_files (struct opt_pass *pass,int properties)
537 {
538 pass->properties_required |= properties;
539 register_dump_files_1 (pass, properties);
540 }
541
542 struct pass_registry
543 {
544 const char* unique_name;
545 struct opt_pass *pass;
546 };
547
548 /* Pass registry hash function. */
549
550 static hashval_t
551 passr_hash (const void *p)
552 {
553 const struct pass_registry *const s = (const struct pass_registry *const) p;
554 return htab_hash_string (s->unique_name);
555 }
556
557 /* Hash equal function */
558
559 static int
560 passr_eq (const void *p1, const void *p2)
561 {
562 const struct pass_registry *const s1 = (const struct pass_registry *const) p1;
563 const struct pass_registry *const s2 = (const struct pass_registry *const) p2;
564
565 return !strcmp (s1->unique_name, s2->unique_name);
566 }
567
568 static htab_t name_to_pass_map = NULL;
569
570 /* Register PASS with NAME. */
571
572 static void
573 register_pass_name (struct opt_pass *pass, const char *name)
574 {
575 struct pass_registry **slot;
576 struct pass_registry pr;
577
578 if (!name_to_pass_map)
579 name_to_pass_map = htab_create (256, passr_hash, passr_eq, NULL);
580
581 pr.unique_name = name;
582 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map, &pr, INSERT);
583 if (!*slot)
584 {
585 struct pass_registry *new_pr;
586
587 new_pr = XCNEW (struct pass_registry);
588 new_pr->unique_name = xstrdup (name);
589 new_pr->pass = pass;
590 *slot = new_pr;
591 }
592 else
593 return; /* Ignore plugin passes. */
594 }
595
596 /* Map from pass id to canonicalized pass name. */
597
598 typedef const char *char_ptr;
599 DEF_VEC_P(char_ptr);
600 DEF_VEC_ALLOC_P(char_ptr, heap);
601 static VEC(char_ptr, heap) *pass_tab = NULL;
602
603 /* Callback function for traversing NAME_TO_PASS_MAP. */
604
605 static int
606 pass_traverse (void **slot, void *data ATTRIBUTE_UNUSED)
607 {
608 struct pass_registry **p = (struct pass_registry **)slot;
609 struct opt_pass *pass = (*p)->pass;
610
611 gcc_assert (pass->static_pass_number > 0);
612 gcc_assert (pass_tab);
613
614 VEC_replace (char_ptr, pass_tab, pass->static_pass_number,
615 (*p)->unique_name);
616
617 return 1;
618 }
619
620 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
621 table for dumping purpose. */
622
623 static void
624 create_pass_tab (void)
625 {
626 if (!flag_dump_passes)
627 return;
628
629 VEC_safe_grow_cleared (char_ptr, heap,
630 pass_tab, passes_by_id_size + 1);
631 htab_traverse (name_to_pass_map, pass_traverse, NULL);
632 }
633
634 static bool override_gate_status (struct opt_pass *, tree, bool);
635
636 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
637 is turned on or not. */
638
639 static void
640 dump_one_pass (struct opt_pass *pass, int pass_indent)
641 {
642 int indent = 3 * pass_indent;
643 const char *pn;
644 bool is_on, is_really_on;
645
646 is_on = (pass->gate == NULL) ? true : pass->gate();
647 is_really_on = override_gate_status (pass, current_function_decl, is_on);
648
649 if (pass->static_pass_number <= 0)
650 pn = pass->name;
651 else
652 pn = VEC_index (char_ptr, pass_tab, pass->static_pass_number);
653
654 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
655 (15 - indent < 0 ? 0 : 15 - indent), " ",
656 is_on ? " ON" : " OFF",
657 ((!is_on) == (!is_really_on) ? ""
658 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
659 }
660
661 /* Dump pass list PASS with indentation INDENT. */
662
663 static void
664 dump_pass_list (struct opt_pass *pass, int indent)
665 {
666 do
667 {
668 dump_one_pass (pass, indent);
669 if (pass->sub)
670 dump_pass_list (pass->sub, indent + 1);
671 pass = pass->next;
672 }
673 while (pass);
674 }
675
676 /* Dump all optimization passes. */
677
678 void
679 dump_passes (void)
680 {
681 struct cgraph_node *n, *node = NULL;
682 tree save_fndecl = current_function_decl;
683
684 create_pass_tab();
685
686 FOR_EACH_DEFINED_FUNCTION (n)
687 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
688 {
689 node = n;
690 break;
691 }
692
693 if (!node)
694 return;
695
696 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
697 current_function_decl = node->symbol.decl;
698
699 dump_pass_list (all_lowering_passes, 1);
700 dump_pass_list (all_small_ipa_passes, 1);
701 dump_pass_list (all_regular_ipa_passes, 1);
702 dump_pass_list (all_lto_gen_passes, 1);
703 dump_pass_list (all_late_ipa_passes, 1);
704 dump_pass_list (all_passes, 1);
705
706 pop_cfun ();
707 current_function_decl = save_fndecl;
708 }
709
710
711 /* Returns the pass with NAME. */
712
713 static struct opt_pass *
714 get_pass_by_name (const char *name)
715 {
716 struct pass_registry **slot, pr;
717
718 pr.unique_name = name;
719 slot = (struct pass_registry **) htab_find_slot (name_to_pass_map,
720 &pr, NO_INSERT);
721
722 if (!slot || !*slot)
723 return NULL;
724
725 return (*slot)->pass;
726 }
727
728
729 /* Range [start, last]. */
730
731 struct uid_range
732 {
733 unsigned int start;
734 unsigned int last;
735 const char *assem_name;
736 struct uid_range *next;
737 };
738
739 typedef struct uid_range *uid_range_p;
740
741 DEF_VEC_P(uid_range_p);
742 DEF_VEC_ALLOC_P(uid_range_p, heap);
743
744 static VEC(uid_range_p, heap) *enabled_pass_uid_range_tab = NULL;
745 static VEC(uid_range_p, heap) *disabled_pass_uid_range_tab = NULL;
746
747
748 /* Parse option string for -fdisable- and -fenable-
749 The syntax of the options:
750
751 -fenable-<pass_name>
752 -fdisable-<pass_name>
753
754 -fenable-<pass_name>=s1:e1,s2:e2,...
755 -fdisable-<pass_name>=s1:e1,s2:e2,...
756 */
757
758 static void
759 enable_disable_pass (const char *arg, bool is_enable)
760 {
761 struct opt_pass *pass;
762 char *range_str, *phase_name;
763 char *argstr = xstrdup (arg);
764 VEC(uid_range_p, heap) **tab = 0;
765
766 range_str = strchr (argstr,'=');
767 if (range_str)
768 {
769 *range_str = '\0';
770 range_str++;
771 }
772
773 phase_name = argstr;
774 if (!*phase_name)
775 {
776 if (is_enable)
777 error ("unrecognized option -fenable");
778 else
779 error ("unrecognized option -fdisable");
780 free (argstr);
781 return;
782 }
783 pass = get_pass_by_name (phase_name);
784 if (!pass || pass->static_pass_number == -1)
785 {
786 if (is_enable)
787 error ("unknown pass %s specified in -fenable", phase_name);
788 else
789 error ("unknown pass %s specified in -fdisable", phase_name);
790 free (argstr);
791 return;
792 }
793
794 if (is_enable)
795 tab = &enabled_pass_uid_range_tab;
796 else
797 tab = &disabled_pass_uid_range_tab;
798
799 if ((unsigned) pass->static_pass_number >= VEC_length (uid_range_p, *tab))
800 VEC_safe_grow_cleared (uid_range_p, heap,
801 *tab, pass->static_pass_number + 1);
802
803 if (!range_str)
804 {
805 uid_range_p slot;
806 uid_range_p new_range = XCNEW (struct uid_range);
807
808 new_range->start = 0;
809 new_range->last = (unsigned)-1;
810
811 slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
812 new_range->next = slot;
813 VEC_replace (uid_range_p, *tab, pass->static_pass_number,
814 new_range);
815 if (is_enable)
816 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
817 "of [%u, %u]", phase_name, new_range->start, new_range->last);
818 else
819 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
820 "of [%u, %u]", phase_name, new_range->start, new_range->last);
821 }
822 else
823 {
824 char *next_range = NULL;
825 char *one_range = range_str;
826 char *end_val = NULL;
827
828 do
829 {
830 uid_range_p slot;
831 uid_range_p new_range;
832 char *invalid = NULL;
833 long start;
834 char *func_name = NULL;
835
836 next_range = strchr (one_range, ',');
837 if (next_range)
838 {
839 *next_range = '\0';
840 next_range++;
841 }
842
843 end_val = strchr (one_range, ':');
844 if (end_val)
845 {
846 *end_val = '\0';
847 end_val++;
848 }
849 start = strtol (one_range, &invalid, 10);
850 if (*invalid || start < 0)
851 {
852 if (end_val || (one_range[0] >= '0'
853 && one_range[0] <= '9'))
854 {
855 error ("Invalid range %s in option %s",
856 one_range,
857 is_enable ? "-fenable" : "-fdisable");
858 free (argstr);
859 return;
860 }
861 func_name = one_range;
862 }
863 if (!end_val)
864 {
865 new_range = XCNEW (struct uid_range);
866 if (!func_name)
867 {
868 new_range->start = (unsigned) start;
869 new_range->last = (unsigned) start;
870 }
871 else
872 {
873 new_range->start = (unsigned) -1;
874 new_range->last = (unsigned) -1;
875 new_range->assem_name = xstrdup (func_name);
876 }
877 }
878 else
879 {
880 long last = strtol (end_val, &invalid, 10);
881 if (*invalid || last < start)
882 {
883 error ("Invalid range %s in option %s",
884 end_val,
885 is_enable ? "-fenable" : "-fdisable");
886 free (argstr);
887 return;
888 }
889 new_range = XCNEW (struct uid_range);
890 new_range->start = (unsigned) start;
891 new_range->last = (unsigned) last;
892 }
893
894 slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
895 new_range->next = slot;
896 VEC_replace (uid_range_p, *tab, pass->static_pass_number,
897 new_range);
898 if (is_enable)
899 {
900 if (new_range->assem_name)
901 inform (UNKNOWN_LOCATION,
902 "enable pass %s for function %s",
903 phase_name, new_range->assem_name);
904 else
905 inform (UNKNOWN_LOCATION,
906 "enable pass %s for functions in the range of [%u, %u]",
907 phase_name, new_range->start, new_range->last);
908 }
909 else
910 {
911 if (new_range->assem_name)
912 inform (UNKNOWN_LOCATION,
913 "disable pass %s for function %s",
914 phase_name, new_range->assem_name);
915 else
916 inform (UNKNOWN_LOCATION,
917 "disable pass %s for functions in the range of [%u, %u]",
918 phase_name, new_range->start, new_range->last);
919 }
920
921 one_range = next_range;
922 } while (next_range);
923 }
924
925 free (argstr);
926 }
927
928 /* Enable pass specified by ARG. */
929
930 void
931 enable_pass (const char *arg)
932 {
933 enable_disable_pass (arg, true);
934 }
935
936 /* Disable pass specified by ARG. */
937
938 void
939 disable_pass (const char *arg)
940 {
941 enable_disable_pass (arg, false);
942 }
943
944 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
945
946 static bool
947 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
948 tree func,
949 VEC(uid_range_p, heap) *tab)
950 {
951 uid_range_p slot, range;
952 int cgraph_uid;
953 const char *aname = NULL;
954
955 if (!tab
956 || (unsigned) pass->static_pass_number >= VEC_length (uid_range_p, tab)
957 || pass->static_pass_number == -1)
958 return false;
959
960 slot = VEC_index (uid_range_p, tab, pass->static_pass_number);
961 if (!slot)
962 return false;
963
964 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
965 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
966 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
967
968 range = slot;
969 while (range)
970 {
971 if ((unsigned) cgraph_uid >= range->start
972 && (unsigned) cgraph_uid <= range->last)
973 return true;
974 if (range->assem_name && aname
975 && !strcmp (range->assem_name, aname))
976 return true;
977 range = range->next;
978 }
979
980 return false;
981 }
982
983 /* Look at the static_pass_number and duplicate the pass
984 if it is already added to a list. */
985
986 static struct opt_pass *
987 make_pass_instance (struct opt_pass *pass, bool track_duplicates)
988 {
989 /* A nonzero static_pass_number indicates that the
990 pass is already in the list. */
991 if (pass->static_pass_number)
992 {
993 struct opt_pass *new_pass;
994
995 if (pass->type == GIMPLE_PASS
996 || pass->type == RTL_PASS
997 || pass->type == SIMPLE_IPA_PASS)
998 {
999 new_pass = XNEW (struct opt_pass);
1000 memcpy (new_pass, pass, sizeof (struct opt_pass));
1001 }
1002 else if (pass->type == IPA_PASS)
1003 {
1004 new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
1005 memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
1006 }
1007 else
1008 gcc_unreachable ();
1009
1010 new_pass->next = NULL;
1011
1012 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1013
1014 /* Indicate to register_dump_files that this pass has duplicates,
1015 and so it should rename the dump file. The first instance will
1016 be -1, and be number of duplicates = -static_pass_number - 1.
1017 Subsequent instances will be > 0 and just the duplicate number. */
1018 if ((pass->name && pass->name[0] != '*') || track_duplicates)
1019 {
1020 pass->static_pass_number -= 1;
1021 new_pass->static_pass_number = -pass->static_pass_number;
1022 }
1023 return new_pass;
1024 }
1025 else
1026 {
1027 pass->todo_flags_start |= TODO_mark_first_instance;
1028 pass->static_pass_number = -1;
1029
1030 invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
1031 }
1032 return pass;
1033 }
1034
1035 /* Add a pass to the pass list. Duplicate the pass if it's already
1036 in the list. */
1037
1038 static struct opt_pass **
1039 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
1040 {
1041 /* Every pass should have a name so that plugins can refer to them. */
1042 gcc_assert (pass->name != NULL);
1043
1044 *list = make_pass_instance (pass, false);
1045
1046 return &(*list)->next;
1047 }
1048
1049 /* List node for an inserted pass instance. We need to keep track of all
1050 the newly-added pass instances (with 'added_pass_nodes' defined below)
1051 so that we can register their dump files after pass-positioning is finished.
1052 Registering dumping files needs to be post-processed or the
1053 static_pass_number of the opt_pass object would be modified and mess up
1054 the dump file names of future pass instances to be added. */
1055
1056 struct pass_list_node
1057 {
1058 struct opt_pass *pass;
1059 struct pass_list_node *next;
1060 };
1061
1062 static struct pass_list_node *added_pass_nodes = NULL;
1063 static struct pass_list_node *prev_added_pass_node;
1064
1065 /* Insert the pass at the proper position. Return true if the pass
1066 is successfully added.
1067
1068 NEW_PASS_INFO - new pass to be inserted
1069 PASS_LIST - root of the pass list to insert the new pass to */
1070
1071 static bool
1072 position_pass (struct register_pass_info *new_pass_info,
1073 struct opt_pass **pass_list)
1074 {
1075 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1076 bool success = false;
1077
1078 for ( ; pass; prev_pass = pass, pass = pass->next)
1079 {
1080 /* Check if the current pass is of the same type as the new pass and
1081 matches the name and the instance number of the reference pass. */
1082 if (pass->type == new_pass_info->pass->type
1083 && pass->name
1084 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1085 && ((new_pass_info->ref_pass_instance_number == 0)
1086 || (new_pass_info->ref_pass_instance_number ==
1087 pass->static_pass_number)
1088 || (new_pass_info->ref_pass_instance_number == 1
1089 && pass->todo_flags_start & TODO_mark_first_instance)))
1090 {
1091 struct opt_pass *new_pass;
1092 struct pass_list_node *new_pass_node;
1093
1094 new_pass = make_pass_instance (new_pass_info->pass, true);
1095
1096 /* Insert the new pass instance based on the positioning op. */
1097 switch (new_pass_info->pos_op)
1098 {
1099 case PASS_POS_INSERT_AFTER:
1100 new_pass->next = pass->next;
1101 pass->next = new_pass;
1102
1103 /* Skip newly inserted pass to avoid repeated
1104 insertions in the case where the new pass and the
1105 existing one have the same name. */
1106 pass = new_pass;
1107 break;
1108 case PASS_POS_INSERT_BEFORE:
1109 new_pass->next = pass;
1110 if (prev_pass)
1111 prev_pass->next = new_pass;
1112 else
1113 *pass_list = new_pass;
1114 break;
1115 case PASS_POS_REPLACE:
1116 new_pass->next = pass->next;
1117 if (prev_pass)
1118 prev_pass->next = new_pass;
1119 else
1120 *pass_list = new_pass;
1121 new_pass->sub = pass->sub;
1122 new_pass->tv_id = pass->tv_id;
1123 pass = new_pass;
1124 break;
1125 default:
1126 error ("invalid pass positioning operation");
1127 return false;
1128 }
1129
1130 /* Save the newly added pass (instance) in the added_pass_nodes
1131 list so that we can register its dump file later. Note that
1132 we cannot register the dump file now because doing so will modify
1133 the static_pass_number of the opt_pass object and therefore
1134 mess up the dump file name of future instances. */
1135 new_pass_node = XCNEW (struct pass_list_node);
1136 new_pass_node->pass = new_pass;
1137 if (!added_pass_nodes)
1138 added_pass_nodes = new_pass_node;
1139 else
1140 prev_added_pass_node->next = new_pass_node;
1141 prev_added_pass_node = new_pass_node;
1142
1143 success = true;
1144 }
1145
1146 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1147 success = true;
1148 }
1149
1150 return success;
1151 }
1152
1153 /* Hooks a new pass into the pass lists.
1154
1155 PASS_INFO - pass information that specifies the opt_pass object,
1156 reference pass, instance number, and how to position
1157 the pass */
1158
1159 void
1160 register_pass (struct register_pass_info *pass_info)
1161 {
1162 bool all_instances, success;
1163
1164 /* The checks below could fail in buggy plugins. Existing GCC
1165 passes should never fail these checks, so we mention plugin in
1166 the messages. */
1167 if (!pass_info->pass)
1168 fatal_error ("plugin cannot register a missing pass");
1169
1170 if (!pass_info->pass->name)
1171 fatal_error ("plugin cannot register an unnamed pass");
1172
1173 if (!pass_info->reference_pass_name)
1174 fatal_error
1175 ("plugin cannot register pass %qs without reference pass name",
1176 pass_info->pass->name);
1177
1178 /* Try to insert the new pass to the pass lists. We need to check
1179 all five lists as the reference pass could be in one (or all) of
1180 them. */
1181 all_instances = pass_info->ref_pass_instance_number == 0;
1182 success = position_pass (pass_info, &all_lowering_passes);
1183 if (!success || all_instances)
1184 success |= position_pass (pass_info, &all_small_ipa_passes);
1185 if (!success || all_instances)
1186 success |= position_pass (pass_info, &all_regular_ipa_passes);
1187 if (!success || all_instances)
1188 success |= position_pass (pass_info, &all_lto_gen_passes);
1189 if (!success || all_instances)
1190 success |= position_pass (pass_info, &all_late_ipa_passes);
1191 if (!success || all_instances)
1192 success |= position_pass (pass_info, &all_passes);
1193 if (!success)
1194 fatal_error
1195 ("pass %qs not found but is referenced by new pass %qs",
1196 pass_info->reference_pass_name, pass_info->pass->name);
1197
1198 /* OK, we have successfully inserted the new pass. We need to register
1199 the dump files for the newly added pass and its duplicates (if any).
1200 Because the registration of plugin/backend passes happens after the
1201 command-line options are parsed, the options that specify single
1202 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1203 passes. Therefore we currently can only enable dumping of
1204 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1205 are specified. While doing so, we also delete the pass_list_node
1206 objects created during pass positioning. */
1207 while (added_pass_nodes)
1208 {
1209 struct pass_list_node *next_node = added_pass_nodes->next;
1210 enum tree_dump_index tdi;
1211 register_one_dump_file (added_pass_nodes->pass);
1212 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1213 || added_pass_nodes->pass->type == IPA_PASS)
1214 tdi = TDI_ipa_all;
1215 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1216 tdi = TDI_tree_all;
1217 else
1218 tdi = TDI_rtl_all;
1219 /* Check if dump-all flag is specified. */
1220 if (get_dump_file_info (tdi)->state)
1221 get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1222 ->state = get_dump_file_info (tdi)->state;
1223 XDELETE (added_pass_nodes);
1224 added_pass_nodes = next_node;
1225 }
1226 }
1227
1228 /* Construct the pass tree. The sequencing of passes is driven by
1229 the cgraph routines:
1230
1231 finalize_compilation_unit ()
1232 for each node N in the cgraph
1233 cgraph_analyze_function (N)
1234 cgraph_lower_function (N) -> all_lowering_passes
1235
1236 If we are optimizing, compile is then invoked:
1237
1238 compile ()
1239 ipa_passes () -> all_small_ipa_passes
1240 -> Analysis of all_regular_ipa_passes
1241 * possible LTO streaming at copmilation time *
1242 -> Execution of all_regular_ipa_passes
1243 * possible LTO streaming at link time *
1244 -> all_late_ipa_passes
1245 expand_all_functions ()
1246 for each node N in the cgraph
1247 expand_function (N) -> Transformation of all_regular_ipa_passes
1248 -> all_passes
1249 */
1250
1251 void
1252 init_optimization_passes (void)
1253 {
1254 struct opt_pass **p;
1255
1256 #define NEXT_PASS(PASS) (p = next_pass_1 (p, &((PASS).pass)))
1257
1258 /* All passes needed to lower the function into shape optimizers can
1259 operate on. These passes are always run first on the function, but
1260 backend might produce already lowered functions that are not processed
1261 by these passes. */
1262 p = &all_lowering_passes;
1263 NEXT_PASS (pass_warn_unused_result);
1264 NEXT_PASS (pass_diagnose_omp_blocks);
1265 NEXT_PASS (pass_diagnose_tm_blocks);
1266 NEXT_PASS (pass_mudflap_1);
1267 NEXT_PASS (pass_lower_omp);
1268 NEXT_PASS (pass_lower_cf);
1269 NEXT_PASS (pass_lower_tm);
1270 NEXT_PASS (pass_refactor_eh);
1271 NEXT_PASS (pass_lower_eh);
1272 NEXT_PASS (pass_build_cfg);
1273 NEXT_PASS (pass_warn_function_return);
1274 NEXT_PASS (pass_build_cgraph_edges);
1275 *p = NULL;
1276
1277 /* Interprocedural optimization passes. */
1278 p = &all_small_ipa_passes;
1279 NEXT_PASS (pass_ipa_free_lang_data);
1280 NEXT_PASS (pass_ipa_function_and_variable_visibility);
1281 NEXT_PASS (pass_early_local_passes);
1282 {
1283 struct opt_pass **p = &pass_early_local_passes.pass.sub;
1284 NEXT_PASS (pass_fixup_cfg);
1285 NEXT_PASS (pass_init_datastructures);
1286 NEXT_PASS (pass_expand_omp);
1287
1288 NEXT_PASS (pass_build_ssa);
1289 NEXT_PASS (pass_lower_vector);
1290 NEXT_PASS (pass_early_warn_uninitialized);
1291 NEXT_PASS (pass_rebuild_cgraph_edges);
1292 NEXT_PASS (pass_inline_parameters);
1293 NEXT_PASS (pass_early_inline);
1294 NEXT_PASS (pass_all_early_optimizations);
1295 {
1296 struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
1297 NEXT_PASS (pass_remove_cgraph_callee_edges);
1298 NEXT_PASS (pass_rename_ssa_copies);
1299 NEXT_PASS (pass_ccp);
1300 NEXT_PASS (pass_forwprop);
1301 /* pass_build_ealias is a dummy pass that ensures that we
1302 execute TODO_rebuild_alias at this point. Re-building
1303 alias information also rewrites no longer addressed
1304 locals into SSA form if possible. */
1305 NEXT_PASS (pass_build_ealias);
1306 NEXT_PASS (pass_sra_early);
1307 NEXT_PASS (pass_fre);
1308 NEXT_PASS (pass_copy_prop);
1309 NEXT_PASS (pass_merge_phi);
1310 NEXT_PASS (pass_cd_dce);
1311 NEXT_PASS (pass_early_ipa_sra);
1312 NEXT_PASS (pass_tail_recursion);
1313 NEXT_PASS (pass_convert_switch);
1314 NEXT_PASS (pass_cleanup_eh);
1315 NEXT_PASS (pass_profile);
1316 NEXT_PASS (pass_local_pure_const);
1317 /* Split functions creates parts that are not run through
1318 early optimizations again. It is thus good idea to do this
1319 late. */
1320 NEXT_PASS (pass_split_functions);
1321 }
1322 NEXT_PASS (pass_release_ssa_names);
1323 NEXT_PASS (pass_rebuild_cgraph_edges);
1324 NEXT_PASS (pass_inline_parameters);
1325 }
1326 NEXT_PASS (pass_ipa_free_inline_summary);
1327 NEXT_PASS (pass_ipa_tree_profile);
1328 {
1329 struct opt_pass **p = &pass_ipa_tree_profile.pass.sub;
1330 NEXT_PASS (pass_feedback_split_functions);
1331 }
1332 NEXT_PASS (pass_ipa_increase_alignment);
1333 NEXT_PASS (pass_ipa_tm);
1334 NEXT_PASS (pass_ipa_lower_emutls);
1335 *p = NULL;
1336
1337 p = &all_regular_ipa_passes;
1338 NEXT_PASS (pass_ipa_whole_program_visibility);
1339 NEXT_PASS (pass_ipa_profile);
1340 NEXT_PASS (pass_ipa_cp);
1341 NEXT_PASS (pass_ipa_cdtor_merge);
1342 NEXT_PASS (pass_ipa_inline);
1343 NEXT_PASS (pass_ipa_pure_const);
1344 NEXT_PASS (pass_ipa_reference);
1345 *p = NULL;
1346
1347 p = &all_lto_gen_passes;
1348 NEXT_PASS (pass_ipa_lto_gimple_out);
1349 NEXT_PASS (pass_ipa_lto_finish_out); /* This must be the last LTO pass. */
1350 *p = NULL;
1351
1352 /* Simple IPA passes executed after the regular passes. In WHOPR mode the
1353 passes are executed after partitioning and thus see just parts of the
1354 compiled unit. */
1355 p = &all_late_ipa_passes;
1356 NEXT_PASS (pass_ipa_pta);
1357 *p = NULL;
1358
1359 /* These passes are run after IPA passes on every function that is being
1360 output to the assembler file. */
1361 p = &all_passes;
1362 NEXT_PASS (pass_fixup_cfg);
1363 NEXT_PASS (pass_lower_eh_dispatch);
1364 NEXT_PASS (pass_all_optimizations);
1365 {
1366 struct opt_pass **p = &pass_all_optimizations.pass.sub;
1367 NEXT_PASS (pass_remove_cgraph_callee_edges);
1368 /* Initial scalar cleanups before alias computation.
1369 They ensure memory accesses are not indirect wherever possible. */
1370 NEXT_PASS (pass_strip_predict_hints);
1371 NEXT_PASS (pass_rename_ssa_copies);
1372 NEXT_PASS (pass_complete_unrolli);
1373 NEXT_PASS (pass_ccp);
1374 NEXT_PASS (pass_forwprop);
1375 /* pass_build_alias is a dummy pass that ensures that we
1376 execute TODO_rebuild_alias at this point. Re-building
1377 alias information also rewrites no longer addressed
1378 locals into SSA form if possible. */
1379 NEXT_PASS (pass_build_alias);
1380 NEXT_PASS (pass_return_slot);
1381 NEXT_PASS (pass_phiprop);
1382 NEXT_PASS (pass_fre);
1383 NEXT_PASS (pass_copy_prop);
1384 NEXT_PASS (pass_merge_phi);
1385 NEXT_PASS (pass_vrp);
1386 NEXT_PASS (pass_dce);
1387 NEXT_PASS (pass_call_cdce);
1388 NEXT_PASS (pass_cselim);
1389 NEXT_PASS (pass_tree_ifcombine);
1390 NEXT_PASS (pass_phiopt);
1391 NEXT_PASS (pass_tail_recursion);
1392 NEXT_PASS (pass_ch);
1393 NEXT_PASS (pass_stdarg);
1394 NEXT_PASS (pass_lower_complex);
1395 NEXT_PASS (pass_sra);
1396 NEXT_PASS (pass_rename_ssa_copies);
1397 /* The dom pass will also resolve all __builtin_constant_p calls
1398 that are still there to 0. This has to be done after some
1399 propagations have already run, but before some more dead code
1400 is removed, and this place fits nicely. Remember this when
1401 trying to move or duplicate pass_dominator somewhere earlier. */
1402 NEXT_PASS (pass_dominator);
1403 /* The only const/copy propagation opportunities left after
1404 DOM should be due to degenerate PHI nodes. So rather than
1405 run the full propagators, run a specialized pass which
1406 only examines PHIs to discover const/copy propagation
1407 opportunities. */
1408 NEXT_PASS (pass_phi_only_cprop);
1409 NEXT_PASS (pass_dse);
1410 NEXT_PASS (pass_reassoc);
1411 NEXT_PASS (pass_dce);
1412 NEXT_PASS (pass_forwprop);
1413 NEXT_PASS (pass_phiopt);
1414 NEXT_PASS (pass_object_sizes);
1415 NEXT_PASS (pass_strlen);
1416 NEXT_PASS (pass_ccp);
1417 NEXT_PASS (pass_copy_prop);
1418 NEXT_PASS (pass_cse_sincos);
1419 NEXT_PASS (pass_optimize_bswap);
1420 NEXT_PASS (pass_split_crit_edges);
1421 NEXT_PASS (pass_pre);
1422 NEXT_PASS (pass_sink_code);
1423 NEXT_PASS (pass_tree_loop);
1424 {
1425 struct opt_pass **p = &pass_tree_loop.pass.sub;
1426 NEXT_PASS (pass_tree_loop_init);
1427 NEXT_PASS (pass_lim);
1428 NEXT_PASS (pass_copy_prop);
1429 NEXT_PASS (pass_dce_loop);
1430 NEXT_PASS (pass_tree_unswitch);
1431 NEXT_PASS (pass_scev_cprop);
1432 NEXT_PASS (pass_record_bounds);
1433 NEXT_PASS (pass_check_data_deps);
1434 NEXT_PASS (pass_loop_distribution);
1435 NEXT_PASS (pass_copy_prop);
1436 NEXT_PASS (pass_graphite);
1437 {
1438 struct opt_pass **p = &pass_graphite.pass.sub;
1439 NEXT_PASS (pass_graphite_transforms);
1440 NEXT_PASS (pass_lim);
1441 NEXT_PASS (pass_copy_prop);
1442 NEXT_PASS (pass_dce_loop);
1443 }
1444 NEXT_PASS (pass_iv_canon);
1445 NEXT_PASS (pass_if_conversion);
1446 NEXT_PASS (pass_vectorize);
1447 {
1448 struct opt_pass **p = &pass_vectorize.pass.sub;
1449 NEXT_PASS (pass_dce_loop);
1450 }
1451 NEXT_PASS (pass_predcom);
1452 NEXT_PASS (pass_complete_unroll);
1453 NEXT_PASS (pass_slp_vectorize);
1454 NEXT_PASS (pass_parallelize_loops);
1455 NEXT_PASS (pass_loop_prefetch);
1456 NEXT_PASS (pass_iv_optimize);
1457 NEXT_PASS (pass_lim);
1458 NEXT_PASS (pass_tree_loop_done);
1459 }
1460 NEXT_PASS (pass_lower_vector_ssa);
1461 NEXT_PASS (pass_cse_reciprocals);
1462 NEXT_PASS (pass_reassoc);
1463 NEXT_PASS (pass_vrp);
1464 NEXT_PASS (pass_strength_reduction);
1465 NEXT_PASS (pass_dominator);
1466 /* The only const/copy propagation opportunities left after
1467 DOM should be due to degenerate PHI nodes. So rather than
1468 run the full propagators, run a specialized pass which
1469 only examines PHIs to discover const/copy propagation
1470 opportunities. */
1471 NEXT_PASS (pass_phi_only_cprop);
1472 NEXT_PASS (pass_cd_dce);
1473 NEXT_PASS (pass_tracer);
1474
1475 /* FIXME: If DCE is not run before checking for uninitialized uses,
1476 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
1477 However, this also causes us to misdiagnose cases that should be
1478 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
1479
1480 To fix the false positives in uninit-5.c, we would have to
1481 account for the predicates protecting the set and the use of each
1482 variable. Using a representation like Gated Single Assignment
1483 may help. */
1484 NEXT_PASS (pass_late_warn_uninitialized);
1485 NEXT_PASS (pass_dse);
1486 NEXT_PASS (pass_forwprop);
1487 NEXT_PASS (pass_phiopt);
1488 NEXT_PASS (pass_fold_builtins);
1489 NEXT_PASS (pass_optimize_widening_mul);
1490 NEXT_PASS (pass_tail_calls);
1491 NEXT_PASS (pass_rename_ssa_copies);
1492 NEXT_PASS (pass_uncprop);
1493 NEXT_PASS (pass_local_pure_const);
1494 }
1495 NEXT_PASS (pass_tm_init);
1496 {
1497 struct opt_pass **p = &pass_tm_init.pass.sub;
1498 NEXT_PASS (pass_tm_mark);
1499 NEXT_PASS (pass_tm_memopt);
1500 NEXT_PASS (pass_tm_edges);
1501 }
1502 NEXT_PASS (pass_lower_complex_O0);
1503 NEXT_PASS (pass_cleanup_eh);
1504 NEXT_PASS (pass_lower_resx);
1505 NEXT_PASS (pass_nrv);
1506 NEXT_PASS (pass_mudflap_2);
1507 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
1508 NEXT_PASS (pass_warn_function_noreturn);
1509
1510 NEXT_PASS (pass_expand);
1511
1512 NEXT_PASS (pass_rest_of_compilation);
1513 {
1514 struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
1515 NEXT_PASS (pass_instantiate_virtual_regs);
1516 NEXT_PASS (pass_into_cfg_layout_mode);
1517 NEXT_PASS (pass_jump);
1518 NEXT_PASS (pass_lower_subreg);
1519 NEXT_PASS (pass_df_initialize_opt);
1520 NEXT_PASS (pass_cse);
1521 NEXT_PASS (pass_rtl_fwprop);
1522 NEXT_PASS (pass_rtl_cprop);
1523 NEXT_PASS (pass_rtl_pre);
1524 NEXT_PASS (pass_rtl_hoist);
1525 NEXT_PASS (pass_rtl_cprop);
1526 NEXT_PASS (pass_rtl_store_motion);
1527 NEXT_PASS (pass_cse_after_global_opts);
1528 NEXT_PASS (pass_rtl_ifcvt);
1529 NEXT_PASS (pass_reginfo_init);
1530 /* Perform loop optimizations. It might be better to do them a bit
1531 sooner, but we want the profile feedback to work more
1532 efficiently. */
1533 NEXT_PASS (pass_loop2);
1534 {
1535 struct opt_pass **p = &pass_loop2.pass.sub;
1536 NEXT_PASS (pass_rtl_loop_init);
1537 NEXT_PASS (pass_rtl_move_loop_invariants);
1538 NEXT_PASS (pass_rtl_unswitch);
1539 NEXT_PASS (pass_rtl_unroll_and_peel_loops);
1540 NEXT_PASS (pass_rtl_doloop);
1541 NEXT_PASS (pass_rtl_loop_done);
1542 *p = NULL;
1543 }
1544 NEXT_PASS (pass_web);
1545 NEXT_PASS (pass_rtl_cprop);
1546 NEXT_PASS (pass_cse2);
1547 NEXT_PASS (pass_rtl_dse1);
1548 NEXT_PASS (pass_rtl_fwprop_addr);
1549 NEXT_PASS (pass_inc_dec);
1550 NEXT_PASS (pass_initialize_regs);
1551 NEXT_PASS (pass_ud_rtl_dce);
1552 NEXT_PASS (pass_combine);
1553 NEXT_PASS (pass_if_after_combine);
1554 NEXT_PASS (pass_partition_blocks);
1555 NEXT_PASS (pass_regmove);
1556 NEXT_PASS (pass_outof_cfg_layout_mode);
1557 NEXT_PASS (pass_split_all_insns);
1558 NEXT_PASS (pass_lower_subreg2);
1559 NEXT_PASS (pass_df_initialize_no_opt);
1560 NEXT_PASS (pass_stack_ptr_mod);
1561 NEXT_PASS (pass_mode_switching);
1562 NEXT_PASS (pass_match_asm_constraints);
1563 NEXT_PASS (pass_sms);
1564 NEXT_PASS (pass_sched);
1565 NEXT_PASS (pass_ira);
1566 NEXT_PASS (pass_reload);
1567 NEXT_PASS (pass_postreload);
1568 {
1569 struct opt_pass **p = &pass_postreload.pass.sub;
1570 NEXT_PASS (pass_postreload_cse);
1571 NEXT_PASS (pass_gcse2);
1572 NEXT_PASS (pass_split_after_reload);
1573 NEXT_PASS (pass_ree);
1574 NEXT_PASS (pass_compare_elim_after_reload);
1575 NEXT_PASS (pass_branch_target_load_optimize1);
1576 NEXT_PASS (pass_thread_prologue_and_epilogue);
1577 NEXT_PASS (pass_rtl_dse2);
1578 NEXT_PASS (pass_stack_adjustments);
1579 NEXT_PASS (pass_jump2);
1580 NEXT_PASS (pass_peephole2);
1581 NEXT_PASS (pass_if_after_reload);
1582 NEXT_PASS (pass_regrename);
1583 NEXT_PASS (pass_cprop_hardreg);
1584 NEXT_PASS (pass_fast_rtl_dce);
1585 NEXT_PASS (pass_reorder_blocks);
1586 NEXT_PASS (pass_branch_target_load_optimize2);
1587 NEXT_PASS (pass_leaf_regs);
1588 NEXT_PASS (pass_split_before_sched2);
1589 NEXT_PASS (pass_sched2);
1590 NEXT_PASS (pass_stack_regs);
1591 {
1592 struct opt_pass **p = &pass_stack_regs.pass.sub;
1593 NEXT_PASS (pass_split_before_regstack);
1594 NEXT_PASS (pass_stack_regs_run);
1595 }
1596 NEXT_PASS (pass_compute_alignments);
1597 NEXT_PASS (pass_duplicate_computed_gotos);
1598 NEXT_PASS (pass_variable_tracking);
1599 NEXT_PASS (pass_free_cfg);
1600 NEXT_PASS (pass_machine_reorg);
1601 NEXT_PASS (pass_cleanup_barriers);
1602 NEXT_PASS (pass_delay_slots);
1603 NEXT_PASS (pass_split_for_shorten_branches);
1604 NEXT_PASS (pass_convert_to_eh_region_ranges);
1605 NEXT_PASS (pass_shorten_branches);
1606 NEXT_PASS (pass_set_nothrow_function_flags);
1607 NEXT_PASS (pass_dwarf2_frame);
1608 NEXT_PASS (pass_final);
1609 }
1610 NEXT_PASS (pass_df_finish);
1611 }
1612 NEXT_PASS (pass_clean_state);
1613 *p = NULL;
1614
1615 #undef NEXT_PASS
1616
1617 /* Register the passes with the tree dump code. */
1618 register_dump_files (all_lowering_passes, PROP_gimple_any);
1619 register_dump_files (all_small_ipa_passes,
1620 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1621 | PROP_cfg);
1622 register_dump_files (all_regular_ipa_passes,
1623 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1624 | PROP_cfg);
1625 register_dump_files (all_lto_gen_passes,
1626 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1627 | PROP_cfg);
1628 register_dump_files (all_late_ipa_passes,
1629 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1630 | PROP_cfg);
1631 register_dump_files (all_passes,
1632 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1633 | PROP_cfg);
1634 }
1635
1636 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1637 function CALLBACK for every function in the call graph. Otherwise,
1638 call CALLBACK on the current function. */
1639
1640 static void
1641 do_per_function (void (*callback) (void *data), void *data)
1642 {
1643 if (current_function_decl)
1644 callback (data);
1645 else
1646 {
1647 struct cgraph_node *node;
1648 FOR_EACH_DEFINED_FUNCTION (node)
1649 if (gimple_has_body_p (node->symbol.decl)
1650 && (!node->clone_of || node->symbol.decl != node->clone_of->symbol.decl))
1651 {
1652 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1653 current_function_decl = node->symbol.decl;
1654 callback (data);
1655 if (!flag_wpa)
1656 {
1657 free_dominance_info (CDI_DOMINATORS);
1658 free_dominance_info (CDI_POST_DOMINATORS);
1659 }
1660 current_function_decl = NULL;
1661 pop_cfun ();
1662 ggc_collect ();
1663 }
1664 }
1665 }
1666
1667 /* Because inlining might remove no-longer reachable nodes, we need to
1668 keep the array visible to garbage collector to avoid reading collected
1669 out nodes. */
1670 static int nnodes;
1671 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1672
1673 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1674 function CALLBACK for every function in the call graph. Otherwise,
1675 call CALLBACK on the current function.
1676 This function is global so that plugins can use it. */
1677 void
1678 do_per_function_toporder (void (*callback) (void *data), void *data)
1679 {
1680 int i;
1681
1682 if (current_function_decl)
1683 callback (data);
1684 else
1685 {
1686 gcc_assert (!order);
1687 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1688 nnodes = ipa_reverse_postorder (order);
1689 for (i = nnodes - 1; i >= 0; i--)
1690 order[i]->process = 1;
1691 for (i = nnodes - 1; i >= 0; i--)
1692 {
1693 struct cgraph_node *node = order[i];
1694
1695 /* Allow possibly removed nodes to be garbage collected. */
1696 order[i] = NULL;
1697 node->process = 0;
1698 if (cgraph_function_with_gimple_body_p (node))
1699 {
1700 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1701 current_function_decl = node->symbol.decl;
1702 callback (data);
1703 free_dominance_info (CDI_DOMINATORS);
1704 free_dominance_info (CDI_POST_DOMINATORS);
1705 current_function_decl = NULL;
1706 pop_cfun ();
1707 ggc_collect ();
1708 }
1709 }
1710 }
1711 ggc_free (order);
1712 order = NULL;
1713 nnodes = 0;
1714 }
1715
1716 /* Helper function to perform function body dump. */
1717
1718 static void
1719 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1720 {
1721 if (dump_file && current_function_decl)
1722 {
1723 if (cfun->curr_properties & PROP_trees)
1724 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1725 else
1726 {
1727 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1728
1729 if ((cfun->curr_properties & PROP_cfg)
1730 && graph_dump_format != no_graph
1731 && (dump_flags & TDF_GRAPH))
1732 print_rtl_graph_with_bb (dump_file_name, get_insns ());
1733 }
1734
1735 /* Flush the file. If verification fails, we won't be able to
1736 close the file before aborting. */
1737 fflush (dump_file);
1738 }
1739 }
1740
1741 /* Perform all TODO actions that ought to be done on each function. */
1742
1743 static void
1744 execute_function_todo (void *data)
1745 {
1746 unsigned int flags = (size_t)data;
1747 flags &= ~cfun->last_verified;
1748 if (!flags)
1749 return;
1750
1751 /* Always cleanup the CFG before trying to update SSA. */
1752 if (flags & TODO_cleanup_cfg)
1753 {
1754 bool cleanup = cleanup_tree_cfg ();
1755
1756 if (cleanup && (cfun->curr_properties & PROP_ssa))
1757 flags |= TODO_remove_unused_locals;
1758
1759 /* When cleanup_tree_cfg merges consecutive blocks, it may
1760 perform some simplistic propagation when removing single
1761 valued PHI nodes. This propagation may, in turn, cause the
1762 SSA form to become out-of-date (see PR 22037). So, even
1763 if the parent pass had not scheduled an SSA update, we may
1764 still need to do one. */
1765 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1766 flags |= TODO_update_ssa;
1767 }
1768
1769 if (flags & TODO_update_ssa_any)
1770 {
1771 unsigned update_flags = flags & TODO_update_ssa_any;
1772 update_ssa (update_flags);
1773 cfun->last_verified &= ~TODO_verify_ssa;
1774 }
1775
1776 if (flags & TODO_rebuild_alias)
1777 {
1778 execute_update_addresses_taken ();
1779 compute_may_aliases ();
1780 }
1781 else if (optimize && (flags & TODO_update_address_taken))
1782 execute_update_addresses_taken ();
1783
1784 if (flags & TODO_remove_unused_locals)
1785 remove_unused_locals ();
1786
1787 if (flags & TODO_rebuild_frequencies)
1788 rebuild_frequencies ();
1789
1790 if (flags & TODO_rebuild_cgraph_edges)
1791 rebuild_cgraph_edges ();
1792
1793 /* If we've seen errors do not bother running any verifiers. */
1794 if (seen_error ())
1795 return;
1796
1797 #if defined ENABLE_CHECKING
1798 if (flags & TODO_verify_ssa
1799 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1800 {
1801 verify_gimple_in_cfg (cfun);
1802 verify_ssa (true);
1803 }
1804 else if (flags & TODO_verify_stmts)
1805 verify_gimple_in_cfg (cfun);
1806 if (flags & TODO_verify_flow)
1807 verify_flow_info ();
1808 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1809 verify_loop_closed_ssa (false);
1810 if (flags & TODO_verify_rtl_sharing)
1811 verify_rtl_sharing ();
1812 #endif
1813
1814 cfun->last_verified = flags & TODO_verify_all;
1815 }
1816
1817 /* Perform all TODO actions. */
1818 static void
1819 execute_todo (unsigned int flags)
1820 {
1821 #if defined ENABLE_CHECKING
1822 if (cfun
1823 && need_ssa_update_p (cfun))
1824 gcc_assert (flags & TODO_update_ssa_any);
1825 #endif
1826
1827 timevar_push (TV_TODO);
1828
1829 /* Inform the pass whether it is the first time it is run. */
1830 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1831
1832 statistics_fini_pass ();
1833
1834 do_per_function (execute_function_todo, (void *)(size_t) flags);
1835
1836 /* Always remove functions just as before inlining: IPA passes might be
1837 interested to see bodies of extern inline functions that are not inlined
1838 to analyze side effects. The full removal is done just at the end
1839 of IPA pass queue. */
1840 if (flags & TODO_remove_functions)
1841 {
1842 gcc_assert (!cfun);
1843 symtab_remove_unreachable_nodes (true, dump_file);
1844 }
1845
1846 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
1847 {
1848 gcc_assert (!cfun);
1849 dump_symtab (dump_file);
1850 /* Flush the file. If verification fails, we won't be able to
1851 close the file before aborting. */
1852 fflush (dump_file);
1853 }
1854
1855 if (flags & TODO_ggc_collect)
1856 ggc_collect ();
1857
1858 /* Now that the dumping has been done, we can get rid of the optional
1859 df problems. */
1860 if (flags & TODO_df_finish)
1861 df_finish_pass ((flags & TODO_df_verify) != 0);
1862
1863 timevar_pop (TV_TODO);
1864 }
1865
1866 /* Verify invariants that should hold between passes. This is a place
1867 to put simple sanity checks. */
1868
1869 static void
1870 verify_interpass_invariants (void)
1871 {
1872 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1873 }
1874
1875 /* Clear the last verified flag. */
1876
1877 static void
1878 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1879 {
1880 cfun->last_verified = 0;
1881 }
1882
1883 /* Helper function. Verify that the properties has been turn into the
1884 properties expected by the pass. */
1885
1886 #ifdef ENABLE_CHECKING
1887 static void
1888 verify_curr_properties (void *data)
1889 {
1890 unsigned int props = (size_t)data;
1891 gcc_assert ((cfun->curr_properties & props) == props);
1892 }
1893 #endif
1894
1895 /* Initialize pass dump file. */
1896 /* This is non-static so that the plugins can use it. */
1897
1898 bool
1899 pass_init_dump_file (struct opt_pass *pass)
1900 {
1901 /* If a dump file name is present, open it if enabled. */
1902 if (pass->static_pass_number != -1)
1903 {
1904 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
1905 dump_file_name = get_dump_file_name (pass->static_pass_number);
1906 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
1907 if (dump_file && current_function_decl)
1908 dump_function_header (dump_file, current_function_decl, dump_flags);
1909 return initializing_dump;
1910 }
1911 else
1912 return false;
1913 }
1914
1915 /* Flush PASS dump file. */
1916 /* This is non-static so that plugins can use it. */
1917
1918 void
1919 pass_fini_dump_file (struct opt_pass *pass)
1920 {
1921 /* Flush and close dump file. */
1922 if (dump_file_name)
1923 {
1924 free (CONST_CAST (char *, dump_file_name));
1925 dump_file_name = NULL;
1926 }
1927
1928 if (dump_file)
1929 {
1930 dump_end (pass->static_pass_number, dump_file);
1931 dump_file = NULL;
1932 }
1933 }
1934
1935 /* After executing the pass, apply expected changes to the function
1936 properties. */
1937
1938 static void
1939 update_properties_after_pass (void *data)
1940 {
1941 struct opt_pass *pass = (struct opt_pass *) data;
1942 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1943 & ~pass->properties_destroyed;
1944 }
1945
1946 /* Execute summary generation for all of the passes in IPA_PASS. */
1947
1948 void
1949 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1950 {
1951 while (ipa_pass)
1952 {
1953 struct opt_pass *pass = &ipa_pass->pass;
1954
1955 /* Execute all of the IPA_PASSes in the list. */
1956 if (ipa_pass->pass.type == IPA_PASS
1957 && (!pass->gate || pass->gate ())
1958 && ipa_pass->generate_summary)
1959 {
1960 pass_init_dump_file (pass);
1961
1962 /* If a timevar is present, start it. */
1963 if (pass->tv_id)
1964 timevar_push (pass->tv_id);
1965
1966 ipa_pass->generate_summary ();
1967
1968 /* Stop timevar. */
1969 if (pass->tv_id)
1970 timevar_pop (pass->tv_id);
1971
1972 pass_fini_dump_file (pass);
1973 }
1974 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
1975 }
1976 }
1977
1978 /* Execute IPA_PASS function transform on NODE. */
1979
1980 static void
1981 execute_one_ipa_transform_pass (struct cgraph_node *node,
1982 struct ipa_opt_pass_d *ipa_pass)
1983 {
1984 struct opt_pass *pass = &ipa_pass->pass;
1985 unsigned int todo_after = 0;
1986
1987 current_pass = pass;
1988 if (!ipa_pass->function_transform)
1989 return;
1990
1991 /* Note that the folders should only create gimple expressions.
1992 This is a hack until the new folder is ready. */
1993 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1994
1995 pass_init_dump_file (pass);
1996
1997 /* Run pre-pass verification. */
1998 execute_todo (ipa_pass->function_transform_todo_flags_start);
1999
2000 /* If a timevar is present, start it. */
2001 if (pass->tv_id != TV_NONE)
2002 timevar_push (pass->tv_id);
2003
2004 /* Do it! */
2005 todo_after = ipa_pass->function_transform (node);
2006
2007 /* Stop timevar. */
2008 if (pass->tv_id != TV_NONE)
2009 timevar_pop (pass->tv_id);
2010
2011 /* Run post-pass cleanup and verification. */
2012 execute_todo (todo_after);
2013 verify_interpass_invariants ();
2014
2015 do_per_function (execute_function_dump, NULL);
2016 pass_fini_dump_file (pass);
2017
2018 current_pass = NULL;
2019 }
2020
2021 /* For the current function, execute all ipa transforms. */
2022
2023 void
2024 execute_all_ipa_transforms (void)
2025 {
2026 struct cgraph_node *node;
2027 if (!cfun)
2028 return;
2029 node = cgraph_get_node (current_function_decl);
2030
2031 if (node->ipa_transforms_to_apply)
2032 {
2033 unsigned int i;
2034
2035 for (i = 0; i < VEC_length (ipa_opt_pass, node->ipa_transforms_to_apply);
2036 i++)
2037 execute_one_ipa_transform_pass (node,
2038 VEC_index (ipa_opt_pass,
2039 node->ipa_transforms_to_apply,
2040 i));
2041 VEC_free (ipa_opt_pass, heap, node->ipa_transforms_to_apply);
2042 node->ipa_transforms_to_apply = NULL;
2043 }
2044 }
2045
2046 /* Callback for do_per_function to apply all IPA transforms. */
2047
2048 static void
2049 apply_ipa_transforms (void *data)
2050 {
2051 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2052 if (!node->global.inlined_to && node->ipa_transforms_to_apply)
2053 {
2054 *(bool *)data = true;
2055 execute_all_ipa_transforms();
2056 rebuild_cgraph_edges ();
2057 }
2058 }
2059
2060 /* Check if PASS is explicitly disabled or enabled and return
2061 the gate status. FUNC is the function to be processed, and
2062 GATE_STATUS is the gate status determined by pass manager by
2063 default. */
2064
2065 static bool
2066 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2067 {
2068 bool explicitly_enabled = false;
2069 bool explicitly_disabled = false;
2070
2071 explicitly_enabled
2072 = is_pass_explicitly_enabled_or_disabled (pass, func,
2073 enabled_pass_uid_range_tab);
2074 explicitly_disabled
2075 = is_pass_explicitly_enabled_or_disabled (pass, func,
2076 disabled_pass_uid_range_tab);
2077
2078 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2079
2080 return gate_status;
2081 }
2082
2083
2084 /* Execute PASS. */
2085
2086 bool
2087 execute_one_pass (struct opt_pass *pass)
2088 {
2089 bool initializing_dump;
2090 unsigned int todo_after = 0;
2091
2092 bool gate_status;
2093
2094 /* IPA passes are executed on whole program, so cfun should be NULL.
2095 Other passes need function context set. */
2096 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2097 gcc_assert (!cfun && !current_function_decl);
2098 else
2099 gcc_assert (cfun && current_function_decl);
2100
2101 current_pass = pass;
2102
2103 /* Check whether gate check should be avoided.
2104 User controls the value of the gate through the parameter "gate_status". */
2105 gate_status = (pass->gate == NULL) ? true : pass->gate();
2106 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2107
2108 /* Override gate with plugin. */
2109 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2110
2111 if (!gate_status)
2112 {
2113 current_pass = NULL;
2114 return false;
2115 }
2116
2117 /* Pass execution event trigger: useful to identify passes being
2118 executed. */
2119 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2120
2121 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2122 Apply all trnasforms first. */
2123 if (pass->type == SIMPLE_IPA_PASS)
2124 {
2125 bool applied = false;
2126 do_per_function (apply_ipa_transforms, (void *)&applied);
2127 if (applied)
2128 symtab_remove_unreachable_nodes (true, dump_file);
2129 /* Restore current_pass. */
2130 current_pass = pass;
2131 }
2132
2133 if (!quiet_flag && !cfun)
2134 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2135
2136 /* Note that the folders should only create gimple expressions.
2137 This is a hack until the new folder is ready. */
2138 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2139
2140 initializing_dump = pass_init_dump_file (pass);
2141
2142 /* Run pre-pass verification. */
2143 execute_todo (pass->todo_flags_start);
2144
2145 #ifdef ENABLE_CHECKING
2146 do_per_function (verify_curr_properties,
2147 (void *)(size_t)pass->properties_required);
2148 #endif
2149
2150 /* If a timevar is present, start it. */
2151 if (pass->tv_id != TV_NONE)
2152 timevar_push (pass->tv_id);
2153
2154 /* Do it! */
2155 if (pass->execute)
2156 {
2157 todo_after = pass->execute ();
2158 do_per_function (clear_last_verified, NULL);
2159 }
2160
2161 /* Stop timevar. */
2162 if (pass->tv_id != TV_NONE)
2163 timevar_pop (pass->tv_id);
2164
2165 do_per_function (update_properties_after_pass, pass);
2166
2167 if (initializing_dump
2168 && dump_file
2169 && graph_dump_format != no_graph
2170 && cfun
2171 && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
2172 == (PROP_cfg | PROP_rtl))
2173 {
2174 get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
2175 dump_flags |= TDF_GRAPH;
2176 clean_graph_dump_file (dump_file_name);
2177 }
2178
2179 /* Run post-pass cleanup and verification. */
2180 execute_todo (todo_after | pass->todo_flags_finish);
2181 verify_interpass_invariants ();
2182 do_per_function (execute_function_dump, NULL);
2183 if (pass->type == IPA_PASS)
2184 {
2185 struct cgraph_node *node;
2186 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2187 VEC_safe_push (ipa_opt_pass, heap, node->ipa_transforms_to_apply,
2188 (struct ipa_opt_pass_d *)pass);
2189 }
2190
2191 if (!current_function_decl)
2192 cgraph_process_new_functions ();
2193
2194 pass_fini_dump_file (pass);
2195
2196 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2197 gcc_assert (!(cfun->curr_properties & PROP_trees)
2198 || pass->type != RTL_PASS);
2199
2200 current_pass = NULL;
2201
2202 return true;
2203 }
2204
2205 void
2206 execute_pass_list (struct opt_pass *pass)
2207 {
2208 do
2209 {
2210 gcc_assert (pass->type == GIMPLE_PASS
2211 || pass->type == RTL_PASS);
2212 if (execute_one_pass (pass) && pass->sub)
2213 execute_pass_list (pass->sub);
2214 pass = pass->next;
2215 }
2216 while (pass);
2217 }
2218
2219 /* Same as execute_pass_list but assume that subpasses of IPA passes
2220 are local passes. If SET is not NULL, write out summaries of only
2221 those node in SET. */
2222
2223 static void
2224 ipa_write_summaries_2 (struct opt_pass *pass, struct lto_out_decl_state *state)
2225 {
2226 while (pass)
2227 {
2228 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2229 gcc_assert (!current_function_decl);
2230 gcc_assert (!cfun);
2231 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2232 if (pass->type == IPA_PASS
2233 && ipa_pass->write_summary
2234 && (!pass->gate || pass->gate ()))
2235 {
2236 /* If a timevar is present, start it. */
2237 if (pass->tv_id)
2238 timevar_push (pass->tv_id);
2239
2240 pass_init_dump_file (pass);
2241
2242 ipa_pass->write_summary ();
2243
2244 pass_fini_dump_file (pass);
2245
2246 /* If a timevar is present, start it. */
2247 if (pass->tv_id)
2248 timevar_pop (pass->tv_id);
2249 }
2250
2251 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2252 ipa_write_summaries_2 (pass->sub, state);
2253
2254 pass = pass->next;
2255 }
2256 }
2257
2258 /* Helper function of ipa_write_summaries. Creates and destroys the
2259 decl state and calls ipa_write_summaries_2 for all passes that have
2260 summaries. SET is the set of nodes to be written. */
2261
2262 static void
2263 ipa_write_summaries_1 (cgraph_node_set set, varpool_node_set vset)
2264 {
2265 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2266 compute_ltrans_boundary (state, set, vset);
2267
2268 lto_push_out_decl_state (state);
2269
2270 gcc_assert (!flag_wpa);
2271 ipa_write_summaries_2 (all_regular_ipa_passes, state);
2272 ipa_write_summaries_2 (all_lto_gen_passes, state);
2273
2274 gcc_assert (lto_get_out_decl_state () == state);
2275 lto_pop_out_decl_state ();
2276 lto_delete_out_decl_state (state);
2277 }
2278
2279 /* Write out summaries for all the nodes in the callgraph. */
2280
2281 void
2282 ipa_write_summaries (void)
2283 {
2284 cgraph_node_set set;
2285 varpool_node_set vset;
2286 struct cgraph_node **order;
2287 struct varpool_node *vnode;
2288 int i, order_pos;
2289
2290 if (!flag_generate_lto || seen_error ())
2291 return;
2292
2293 set = cgraph_node_set_new ();
2294
2295 /* Create the callgraph set in the same order used in
2296 cgraph_expand_all_functions. This mostly facilitates debugging,
2297 since it causes the gimple file to be processed in the same order
2298 as the source code. */
2299 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2300 order_pos = ipa_reverse_postorder (order);
2301 gcc_assert (order_pos == cgraph_n_nodes);
2302
2303 for (i = order_pos - 1; i >= 0; i--)
2304 {
2305 struct cgraph_node *node = order[i];
2306
2307 if (cgraph_function_with_gimple_body_p (node))
2308 {
2309 /* When streaming out references to statements as part of some IPA
2310 pass summary, the statements need to have uids assigned and the
2311 following does that for all the IPA passes here. Naturally, this
2312 ordering then matches the one IPA-passes get in their stmt_fixup
2313 hooks. */
2314
2315 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2316 renumber_gimple_stmt_uids ();
2317 pop_cfun ();
2318 }
2319 if (node->analyzed)
2320 cgraph_node_set_add (set, node);
2321 }
2322 vset = varpool_node_set_new ();
2323
2324 FOR_EACH_DEFINED_VARIABLE (vnode)
2325 if ((!vnode->alias || vnode->alias_of))
2326 varpool_node_set_add (vset, vnode);
2327
2328 ipa_write_summaries_1 (set, vset);
2329
2330 free (order);
2331 free_cgraph_node_set (set);
2332 free_varpool_node_set (vset);
2333 }
2334
2335 /* Same as execute_pass_list but assume that subpasses of IPA passes
2336 are local passes. If SET is not NULL, write out optimization summaries of
2337 only those node in SET. */
2338
2339 static void
2340 ipa_write_optimization_summaries_1 (struct opt_pass *pass, struct lto_out_decl_state *state)
2341 {
2342 while (pass)
2343 {
2344 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2345 gcc_assert (!current_function_decl);
2346 gcc_assert (!cfun);
2347 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2348 if (pass->type == IPA_PASS
2349 && ipa_pass->write_optimization_summary
2350 && (!pass->gate || pass->gate ()))
2351 {
2352 /* If a timevar is present, start it. */
2353 if (pass->tv_id)
2354 timevar_push (pass->tv_id);
2355
2356 pass_init_dump_file (pass);
2357
2358 ipa_pass->write_optimization_summary ();
2359
2360 pass_fini_dump_file (pass);
2361
2362 /* If a timevar is present, start it. */
2363 if (pass->tv_id)
2364 timevar_pop (pass->tv_id);
2365 }
2366
2367 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2368 ipa_write_optimization_summaries_1 (pass->sub, state);
2369
2370 pass = pass->next;
2371 }
2372 }
2373
2374 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2375 NULL, write out all summaries of all nodes. */
2376
2377 void
2378 ipa_write_optimization_summaries (cgraph_node_set set, varpool_node_set vset)
2379 {
2380 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2381 cgraph_node_set_iterator csi;
2382 compute_ltrans_boundary (state, set, vset);
2383
2384 lto_push_out_decl_state (state);
2385 for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2386 {
2387 struct cgraph_node *node = csi_node (csi);
2388 /* When streaming out references to statements as part of some IPA
2389 pass summary, the statements need to have uids assigned.
2390
2391 For functions newly born at WPA stage we need to initialize
2392 the uids here. */
2393 if (node->analyzed
2394 && gimple_has_body_p (node->symbol.decl))
2395 {
2396 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2397 renumber_gimple_stmt_uids ();
2398 pop_cfun ();
2399 }
2400 }
2401
2402 gcc_assert (flag_wpa);
2403 ipa_write_optimization_summaries_1 (all_regular_ipa_passes, state);
2404 ipa_write_optimization_summaries_1 (all_lto_gen_passes, state);
2405
2406 gcc_assert (lto_get_out_decl_state () == state);
2407 lto_pop_out_decl_state ();
2408 lto_delete_out_decl_state (state);
2409 }
2410
2411 /* Same as execute_pass_list but assume that subpasses of IPA passes
2412 are local passes. */
2413
2414 static void
2415 ipa_read_summaries_1 (struct opt_pass *pass)
2416 {
2417 while (pass)
2418 {
2419 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2420
2421 gcc_assert (!current_function_decl);
2422 gcc_assert (!cfun);
2423 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2424
2425 if (pass->gate == NULL || pass->gate ())
2426 {
2427 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2428 {
2429 /* If a timevar is present, start it. */
2430 if (pass->tv_id)
2431 timevar_push (pass->tv_id);
2432
2433 pass_init_dump_file (pass);
2434
2435 ipa_pass->read_summary ();
2436
2437 pass_fini_dump_file (pass);
2438
2439 /* Stop timevar. */
2440 if (pass->tv_id)
2441 timevar_pop (pass->tv_id);
2442 }
2443
2444 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2445 ipa_read_summaries_1 (pass->sub);
2446 }
2447 pass = pass->next;
2448 }
2449 }
2450
2451
2452 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2453
2454 void
2455 ipa_read_summaries (void)
2456 {
2457 ipa_read_summaries_1 (all_regular_ipa_passes);
2458 ipa_read_summaries_1 (all_lto_gen_passes);
2459 }
2460
2461 /* Same as execute_pass_list but assume that subpasses of IPA passes
2462 are local passes. */
2463
2464 static void
2465 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2466 {
2467 while (pass)
2468 {
2469 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2470
2471 gcc_assert (!current_function_decl);
2472 gcc_assert (!cfun);
2473 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2474
2475 if (pass->gate == NULL || pass->gate ())
2476 {
2477 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2478 {
2479 /* If a timevar is present, start it. */
2480 if (pass->tv_id)
2481 timevar_push (pass->tv_id);
2482
2483 pass_init_dump_file (pass);
2484
2485 ipa_pass->read_optimization_summary ();
2486
2487 pass_fini_dump_file (pass);
2488
2489 /* Stop timevar. */
2490 if (pass->tv_id)
2491 timevar_pop (pass->tv_id);
2492 }
2493
2494 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2495 ipa_read_optimization_summaries_1 (pass->sub);
2496 }
2497 pass = pass->next;
2498 }
2499 }
2500
2501 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2502
2503 void
2504 ipa_read_optimization_summaries (void)
2505 {
2506 ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
2507 ipa_read_optimization_summaries_1 (all_lto_gen_passes);
2508 }
2509
2510 /* Same as execute_pass_list but assume that subpasses of IPA passes
2511 are local passes. */
2512 void
2513 execute_ipa_pass_list (struct opt_pass *pass)
2514 {
2515 do
2516 {
2517 gcc_assert (!current_function_decl);
2518 gcc_assert (!cfun);
2519 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2520 if (execute_one_pass (pass) && pass->sub)
2521 {
2522 if (pass->sub->type == GIMPLE_PASS)
2523 {
2524 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2525 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2526 pass->sub);
2527 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2528 }
2529 else if (pass->sub->type == SIMPLE_IPA_PASS
2530 || pass->sub->type == IPA_PASS)
2531 execute_ipa_pass_list (pass->sub);
2532 else
2533 gcc_unreachable ();
2534 }
2535 gcc_assert (!current_function_decl);
2536 cgraph_process_new_functions ();
2537 pass = pass->next;
2538 }
2539 while (pass);
2540 }
2541
2542 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2543
2544 static void
2545 execute_ipa_stmt_fixups (struct opt_pass *pass,
2546 struct cgraph_node *node, gimple *stmts)
2547 {
2548 while (pass)
2549 {
2550 /* Execute all of the IPA_PASSes in the list. */
2551 if (pass->type == IPA_PASS
2552 && (!pass->gate || pass->gate ()))
2553 {
2554 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2555
2556 if (ipa_pass->stmt_fixup)
2557 {
2558 pass_init_dump_file (pass);
2559 /* If a timevar is present, start it. */
2560 if (pass->tv_id)
2561 timevar_push (pass->tv_id);
2562
2563 ipa_pass->stmt_fixup (node, stmts);
2564
2565 /* Stop timevar. */
2566 if (pass->tv_id)
2567 timevar_pop (pass->tv_id);
2568 pass_fini_dump_file (pass);
2569 }
2570 if (pass->sub)
2571 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2572 }
2573 pass = pass->next;
2574 }
2575 }
2576
2577 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2578
2579 void
2580 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2581 {
2582 execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
2583 }
2584
2585
2586 extern void debug_properties (unsigned int);
2587 extern void dump_properties (FILE *, unsigned int);
2588
2589 DEBUG_FUNCTION void
2590 dump_properties (FILE *dump, unsigned int props)
2591 {
2592 fprintf (dump, "Properties:\n");
2593 if (props & PROP_gimple_any)
2594 fprintf (dump, "PROP_gimple_any\n");
2595 if (props & PROP_gimple_lcf)
2596 fprintf (dump, "PROP_gimple_lcf\n");
2597 if (props & PROP_gimple_leh)
2598 fprintf (dump, "PROP_gimple_leh\n");
2599 if (props & PROP_cfg)
2600 fprintf (dump, "PROP_cfg\n");
2601 if (props & PROP_ssa)
2602 fprintf (dump, "PROP_ssa\n");
2603 if (props & PROP_no_crit_edges)
2604 fprintf (dump, "PROP_no_crit_edges\n");
2605 if (props & PROP_rtl)
2606 fprintf (dump, "PROP_rtl\n");
2607 if (props & PROP_gimple_lomp)
2608 fprintf (dump, "PROP_gimple_lomp\n");
2609 if (props & PROP_gimple_lcx)
2610 fprintf (dump, "PROP_gimple_lcx\n");
2611 if (props & PROP_cfglayout)
2612 fprintf (dump, "PROP_cfglayout\n");
2613 }
2614
2615 DEBUG_FUNCTION void
2616 debug_properties (unsigned int props)
2617 {
2618 dump_properties (stderr, props);
2619 }
2620
2621 /* Called by local passes to see if function is called by already processed nodes.
2622 Because we process nodes in topological order, this means that function is
2623 in recursive cycle or we introduced new direct calls. */
2624 bool
2625 function_called_by_processed_nodes_p (void)
2626 {
2627 struct cgraph_edge *e;
2628 for (e = cgraph_get_node (current_function_decl)->callers;
2629 e;
2630 e = e->next_caller)
2631 {
2632 if (e->caller->symbol.decl == current_function_decl)
2633 continue;
2634 if (!cgraph_function_with_gimple_body_p (e->caller))
2635 continue;
2636 if (TREE_ASM_WRITTEN (e->caller->symbol.decl))
2637 continue;
2638 if (!e->caller->process && !e->caller->global.inlined_to)
2639 break;
2640 }
2641 if (dump_file && e)
2642 {
2643 fprintf (dump_file, "Already processed call to:\n");
2644 dump_cgraph_node (dump_file, e->caller);
2645 }
2646 return e != NULL;
2647 }
2648
2649 #include "gt-passes.h"