Make opt_pass and gcc::pass_manager be GC-managed
[gcc.git] / gcc / passes.c
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 /* This is the top level of cc1/c++.
21 It parses command args, opens files, invokes the various passes
22 in the proper order, and counts the time used by each.
23 Error messages and low-level interface to malloc also handled here. */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "line-map.h"
30 #include "hash-table.h"
31 #include "input.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "insn-attr.h"
37 #include "insn-config.h"
38 #include "insn-flags.h"
39 #include "hard-reg-set.h"
40 #include "recog.h"
41 #include "output.h"
42 #include "except.h"
43 #include "function.h"
44 #include "toplev.h"
45 #include "expr.h"
46 #include "basic-block.h"
47 #include "intl.h"
48 #include "ggc.h"
49 #include "graph.h"
50 #include "regs.h"
51 #include "diagnostic-core.h"
52 #include "params.h"
53 #include "reload.h"
54 #include "debug.h"
55 #include "target.h"
56 #include "langhooks.h"
57 #include "cfgloop.h"
58 #include "hosthooks.h"
59 #include "cgraph.h"
60 #include "opts.h"
61 #include "coverage.h"
62 #include "value-prof.h"
63 #include "tree-inline.h"
64 #include "tree-flow.h"
65 #include "tree-pass.h"
66 #include "tree-dump.h"
67 #include "df.h"
68 #include "predict.h"
69 #include "lto-streamer.h"
70 #include "plugin.h"
71 #include "ipa-utils.h"
72 #include "tree-pretty-print.h" /* for dump_function_header */
73 #include "context.h"
74 #include "pass_manager.h"
75
76 using namespace gcc;
77
78 /* This is used for debugging. It allows the current pass to printed
79 from anywhere in compilation.
80 The variable current_pass is also used for statistics and plugins. */
81 struct opt_pass *current_pass;
82
83 static void register_pass_name (struct opt_pass *, const char *);
84
85 void*
86 opt_pass::operator new (size_t sz)
87 {
88 return ggc_internal_cleared_alloc_stat (sz MEM_STAT_INFO);
89 }
90
91 void opt_pass::gt_ggc_mx ()
92 {
93 ::gt_ggc_mx (ctxt_);
94 ::gt_ggc_mx (sub);
95
96 /* Avoid deep stack usage by iteratively walking the chain of "next"
97 passes, rather than recursing (analogous to the chain_next/chain_prev
98 GTY options). */
99
100 /* "this" has already been marked.
101 Mark a chain of as-yet-unmarked passes. */
102 opt_pass *limit = this->next;
103 while (ggc_test_and_set_mark (limit))
104 limit = limit->next;
105
106 /* "limit" is the first in the chain that wasn't just marked, either
107 because it is NULL, or because it was already marked.
108 Hence all of the passes in the half-open interval:
109 [this->next...limit)
110 have just been marked: visit them. */
111 for (opt_pass *iter = this->next; iter != limit; iter = iter->next)
112 iter->gt_ggc_mx ();
113 }
114
115 void opt_pass::gt_pch_nx ()
116 {
117 ::gt_pch_nx (ctxt_);
118 ::gt_pch_nx (sub);
119
120 /* Analogous to opt_pass::gt_ggc_mx. */
121 opt_pass *limit = this->next;
122 while (gt_pch_note_object (limit, limit, ::gt_pch_nx_with_op<opt_pass>))
123 limit = limit->next;
124
125 for (opt_pass *iter = this->next; iter != limit; iter = iter->next)
126 iter->gt_pch_nx ();
127
128 }
129
130 void opt_pass::gt_pch_nx_with_op (gt_pointer_operator op, void *cookie)
131 {
132 op (&(ctxt_), cookie);
133 op (&(sub), cookie);
134 op (&(next), cookie);
135 }
136
137 /* Most passes are single-instance (within their context) and thus don't
138 need to implement cloning, but passes that support multiple instances
139 *must* provide their own implementation of the clone method.
140
141 Handle this by providing a default implemenation, but make it a fatal
142 error to call it. */
143
144 opt_pass *
145 opt_pass::clone ()
146 {
147 internal_error ("pass %s does not support cloning", name);
148 }
149
150 bool
151 opt_pass::gate ()
152 {
153 return true;
154 }
155
156 unsigned int
157 opt_pass::execute ()
158 {
159 return 0;
160 }
161
162 opt_pass::opt_pass(const pass_data &data, context *ctxt)
163 : pass_data(data),
164 sub(NULL),
165 next(NULL),
166 static_pass_number(0),
167 ctxt_(ctxt)
168 {
169 }
170
171 void
172 pass_manager::gt_ggc_mx ()
173 {
174 /* We want to efficiently visit all pass objects without incurring deep
175 call chains that could blow the stack.
176
177 Although there are multiple fields referencing passes within the
178 pass_manager, *almost* all of the underlying pass instances are
179 referenced by the passes_by_id array.
180
181 Specifically, passes are in the passes_by_id array if they have
182 register_one_dump_file called on them, which requires them to have
183 a name, and for that name to *not* begin with "*". Currently
184 there are 25 passes with a "*" prefix and thus not in the array.
185
186 Each pass holds references to its sub and next, so visiting a pass
187 will potentially trigger a recursive traversal through these
188 neighbours - if these passes haven't been visited yet.
189
190 By walking the passes_by_id array *backwards*, in the common case
191 this leads to us walking the pass tree from the leaf passes first,
192 eventually reaching the trunk passes, and hence none of the calls
193 should recurse, given that at each point in the iteration pass->sub
194 and pass->next will already have been marked.
195
196 Having walked the array, we then walk the higher-level fields,
197 again in bottom-up order, which will ensure that we visit all
198 remaining passes. Most of the passes will have already been
199 visited, which should minimize further recursion. */
200 for (int i = passes_by_id_size ; i > 0; )
201 ::gt_ggc_mx (passes_by_id[--i]);
202
203 ::gt_ggc_mx (all_late_ipa_passes);
204 ::gt_ggc_mx (all_lto_gen_passes);
205 ::gt_ggc_mx (all_regular_ipa_passes);
206 ::gt_ggc_mx (all_lowering_passes);
207 ::gt_ggc_mx (all_small_ipa_passes);
208 ::gt_ggc_mx (all_passes);
209 }
210
211 void
212 pass_manager::gt_pch_nx ()
213 {
214 /* Analogous to pass_manager::gt_ggc_mx */
215 for (int i = passes_by_id_size ; i > 0; )
216 ::gt_pch_nx (passes_by_id[--i]);
217
218 ::gt_pch_nx (all_late_ipa_passes);
219 ::gt_pch_nx (all_lto_gen_passes);
220 ::gt_pch_nx (all_regular_ipa_passes);
221 ::gt_pch_nx (all_lowering_passes);
222 ::gt_pch_nx (all_small_ipa_passes);
223 ::gt_pch_nx (all_passes);
224 }
225
226 void
227 pass_manager::gt_pch_nx_with_op (gt_pointer_operator op, void *cookie)
228 {
229 /* Unlike the _mx and _nx hooks, we must visit *every* field, since
230 they must each be reconstructed when reading the data back from
231 disk. */
232 op (&(all_passes), cookie);
233 op (&(all_small_ipa_passes), cookie);
234 op (&(all_lowering_passes), cookie);
235 op (&(all_regular_ipa_passes), cookie);
236 op (&(all_lto_gen_passes), cookie);
237 op (&(all_late_ipa_passes), cookie);
238
239 for (int i = 0; i < passes_by_id_size; i++)
240 op (&(passes_by_id[i]), cookie);
241
242 #define INSERT_PASSES_AFTER(PASS)
243 #define PUSH_INSERT_PASSES_WITHIN(PASS)
244 #define POP_INSERT_PASSES()
245 #define NEXT_PASS(PASS, NUM) op (&(PASS ## _ ## NUM), cookie);
246 #define TERMINATE_PASS_LIST()
247
248 #include "pass-instances.def"
249
250 #undef INSERT_PASSES_AFTER
251 #undef PUSH_INSERT_PASSES_WITHIN
252 #undef POP_INSERT_PASSES
253 #undef NEXT_PASS
254 #undef TERMINATE_PASS_LIST
255
256 }
257
258 void
259 pass_manager::execute_early_local_passes ()
260 {
261 execute_pass_list (pass_early_local_passes_1->sub);
262 }
263
264 unsigned int
265 pass_manager::execute_pass_mode_switching ()
266 {
267 return pass_mode_switching_1->execute ();
268 }
269
270
271 /* Call from anywhere to find out what pass this is. Useful for
272 printing out debugging information deep inside an service
273 routine. */
274 void
275 print_current_pass (FILE *file)
276 {
277 if (current_pass)
278 fprintf (file, "current pass = %s (%d)\n",
279 current_pass->name, current_pass->static_pass_number);
280 else
281 fprintf (file, "no current pass.\n");
282 }
283
284
285 /* Call from the debugger to get the current pass name. */
286 DEBUG_FUNCTION void
287 debug_pass (void)
288 {
289 print_current_pass (stderr);
290 }
291
292
293
294 /* Global variables used to communicate with passes. */
295 bool in_gimple_form;
296 bool first_pass_instance;
297
298
299 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
300 and TYPE_DECL nodes.
301
302 This does nothing for local (non-static) variables, unless the
303 variable is a register variable with DECL_ASSEMBLER_NAME set. In
304 that case, or if the variable is not an automatic, it sets up the
305 RTL and outputs any assembler code (label definition, storage
306 allocation and initialization).
307
308 DECL is the declaration. TOP_LEVEL is nonzero
309 if this declaration is not within a function. */
310
311 void
312 rest_of_decl_compilation (tree decl,
313 int top_level,
314 int at_end)
315 {
316 /* We deferred calling assemble_alias so that we could collect
317 other attributes such as visibility. Emit the alias now. */
318 if (!in_lto_p)
319 {
320 tree alias;
321 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
322 if (alias)
323 {
324 alias = TREE_VALUE (TREE_VALUE (alias));
325 alias = get_identifier (TREE_STRING_POINTER (alias));
326 /* A quirk of the initial implementation of aliases required that the
327 user add "extern" to all of them. Which is silly, but now
328 historical. Do note that the symbol is in fact locally defined. */
329 DECL_EXTERNAL (decl) = 0;
330 TREE_STATIC (decl) = 1;
331 assemble_alias (decl, alias);
332 }
333 }
334
335 /* Can't defer this, because it needs to happen before any
336 later function definitions are processed. */
337 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
338 make_decl_rtl (decl);
339
340 /* Forward declarations for nested functions are not "external",
341 but we need to treat them as if they were. */
342 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
343 || TREE_CODE (decl) == FUNCTION_DECL)
344 {
345 timevar_push (TV_VARCONST);
346
347 /* Don't output anything when a tentative file-scope definition
348 is seen. But at end of compilation, do output code for them.
349
350 We do output all variables and rely on
351 callgraph code to defer them except for forward declarations
352 (see gcc.c-torture/compile/920624-1.c) */
353 if ((at_end
354 || !DECL_DEFER_OUTPUT (decl)
355 || DECL_INITIAL (decl))
356 && (TREE_CODE (decl) != VAR_DECL || !DECL_HAS_VALUE_EXPR_P (decl))
357 && !DECL_EXTERNAL (decl))
358 {
359 /* When reading LTO unit, we also read varpool, so do not
360 rebuild it. */
361 if (in_lto_p && !at_end)
362 ;
363 else if (TREE_CODE (decl) != FUNCTION_DECL)
364 varpool_finalize_decl (decl);
365 }
366
367 #ifdef ASM_FINISH_DECLARE_OBJECT
368 if (decl == last_assemble_variable_decl)
369 {
370 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
371 top_level, at_end);
372 }
373 #endif
374
375 timevar_pop (TV_VARCONST);
376 }
377 else if (TREE_CODE (decl) == TYPE_DECL
378 /* Like in rest_of_type_compilation, avoid confusing the debug
379 information machinery when there are errors. */
380 && !seen_error ())
381 {
382 timevar_push (TV_SYMOUT);
383 debug_hooks->type_decl (decl, !top_level);
384 timevar_pop (TV_SYMOUT);
385 }
386
387 /* Let cgraph know about the existence of variables. */
388 if (in_lto_p && !at_end)
389 ;
390 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
391 && TREE_STATIC (decl))
392 varpool_node_for_decl (decl);
393 }
394
395 /* Called after finishing a record, union or enumeral type. */
396
397 void
398 rest_of_type_compilation (tree type, int toplev)
399 {
400 /* Avoid confusing the debug information machinery when there are
401 errors. */
402 if (seen_error ())
403 return;
404
405 timevar_push (TV_SYMOUT);
406 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
407 timevar_pop (TV_SYMOUT);
408 }
409
410 \f
411
412 void
413 pass_manager::
414 finish_optimization_passes (void)
415 {
416 int i;
417 struct dump_file_info *dfi;
418 char *name;
419
420 timevar_push (TV_DUMP);
421 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
422 {
423 dump_start (pass_profile_1->static_pass_number, NULL);
424 end_branch_prob ();
425 dump_finish (pass_profile_1->static_pass_number);
426 }
427
428 if (optimize > 0)
429 {
430 dump_start (pass_profile_1->static_pass_number, NULL);
431 print_combine_total_stats ();
432 dump_finish (pass_profile_1->static_pass_number);
433 }
434
435 /* Do whatever is necessary to finish printing the graphs. */
436 for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
437 if (dump_initialized_p (i)
438 && (dfi->pflags & TDF_GRAPH) != 0
439 && (name = get_dump_file_name (i)) != NULL)
440 {
441 finish_graph_dump_file (name);
442 free (name);
443 }
444
445 timevar_pop (TV_DUMP);
446 }
447
448 static unsigned int
449 execute_all_early_local_passes (void)
450 {
451 /* Once this pass (and its sub-passes) are complete, all functions
452 will be in SSA form. Technically this state change is happening
453 a tad early, since the sub-passes have not yet run, but since
454 none of the sub-passes are IPA passes and do not create new
455 functions, this is ok. We're setting this value for the benefit
456 of IPA passes that follow. */
457 if (cgraph_state < CGRAPH_STATE_IPA_SSA)
458 cgraph_state = CGRAPH_STATE_IPA_SSA;
459 return 0;
460 }
461
462 /* Gate: execute, or not, all of the non-trivial optimizations. */
463
464 static bool
465 gate_all_early_local_passes (void)
466 {
467 /* Don't bother doing anything if the program has errors. */
468 return (!seen_error () && !in_lto_p);
469 }
470
471 namespace {
472
473 const pass_data pass_data_early_local_passes =
474 {
475 SIMPLE_IPA_PASS, /* type */
476 "early_local_cleanups", /* name */
477 OPTGROUP_NONE, /* optinfo_flags */
478 true, /* has_gate */
479 true, /* has_execute */
480 TV_EARLY_LOCAL, /* tv_id */
481 0, /* properties_required */
482 0, /* properties_provided */
483 0, /* properties_destroyed */
484 0, /* todo_flags_start */
485 TODO_remove_functions, /* todo_flags_finish */
486 };
487
488 class pass_early_local_passes : public simple_ipa_opt_pass
489 {
490 public:
491 pass_early_local_passes(gcc::context *ctxt)
492 : simple_ipa_opt_pass(pass_data_early_local_passes, ctxt)
493 {}
494
495 /* opt_pass methods: */
496 bool gate () { return gate_all_early_local_passes (); }
497 unsigned int execute () { return execute_all_early_local_passes (); }
498
499 }; // class pass_early_local_passes
500
501 } // anon namespace
502
503 simple_ipa_opt_pass *
504 make_pass_early_local_passes (gcc::context *ctxt)
505 {
506 return new pass_early_local_passes (ctxt);
507 }
508
509 /* Gate: execute, or not, all of the non-trivial optimizations. */
510
511 static bool
512 gate_all_early_optimizations (void)
513 {
514 return (optimize >= 1
515 /* Don't bother doing anything if the program has errors. */
516 && !seen_error ());
517 }
518
519 namespace {
520
521 const pass_data pass_data_all_early_optimizations =
522 {
523 GIMPLE_PASS, /* type */
524 "early_optimizations", /* name */
525 OPTGROUP_NONE, /* optinfo_flags */
526 true, /* has_gate */
527 false, /* has_execute */
528 TV_NONE, /* tv_id */
529 0, /* properties_required */
530 0, /* properties_provided */
531 0, /* properties_destroyed */
532 0, /* todo_flags_start */
533 0, /* todo_flags_finish */
534 };
535
536 class pass_all_early_optimizations : public gimple_opt_pass
537 {
538 public:
539 pass_all_early_optimizations(gcc::context *ctxt)
540 : gimple_opt_pass(pass_data_all_early_optimizations, ctxt)
541 {}
542
543 /* opt_pass methods: */
544 bool gate () { return gate_all_early_optimizations (); }
545
546 }; // class pass_all_early_optimizations
547
548 } // anon namespace
549
550 static gimple_opt_pass *
551 make_pass_all_early_optimizations (gcc::context *ctxt)
552 {
553 return new pass_all_early_optimizations (ctxt);
554 }
555
556 /* Gate: execute, or not, all of the non-trivial optimizations. */
557
558 static bool
559 gate_all_optimizations (void)
560 {
561 return optimize >= 1 && !optimize_debug;
562 }
563
564 namespace {
565
566 const pass_data pass_data_all_optimizations =
567 {
568 GIMPLE_PASS, /* type */
569 "*all_optimizations", /* name */
570 OPTGROUP_NONE, /* optinfo_flags */
571 true, /* has_gate */
572 false, /* has_execute */
573 TV_OPTIMIZE, /* tv_id */
574 0, /* properties_required */
575 0, /* properties_provided */
576 0, /* properties_destroyed */
577 0, /* todo_flags_start */
578 0, /* todo_flags_finish */
579 };
580
581 class pass_all_optimizations : public gimple_opt_pass
582 {
583 public:
584 pass_all_optimizations(gcc::context *ctxt)
585 : gimple_opt_pass(pass_data_all_optimizations, ctxt)
586 {}
587
588 /* opt_pass methods: */
589 bool gate () { return gate_all_optimizations (); }
590
591 }; // class pass_all_optimizations
592
593 } // anon namespace
594
595 static gimple_opt_pass *
596 make_pass_all_optimizations (gcc::context *ctxt)
597 {
598 return new pass_all_optimizations (ctxt);
599 }
600
601 /* Gate: execute, or not, all of the non-trivial optimizations. */
602
603 static bool
604 gate_all_optimizations_g (void)
605 {
606 return optimize >= 1 && optimize_debug;
607 }
608
609 namespace {
610
611 const pass_data pass_data_all_optimizations_g =
612 {
613 GIMPLE_PASS, /* type */
614 "*all_optimizations_g", /* name */
615 OPTGROUP_NONE, /* optinfo_flags */
616 true, /* has_gate */
617 false, /* has_execute */
618 TV_OPTIMIZE, /* tv_id */
619 0, /* properties_required */
620 0, /* properties_provided */
621 0, /* properties_destroyed */
622 0, /* todo_flags_start */
623 0, /* todo_flags_finish */
624 };
625
626 class pass_all_optimizations_g : public gimple_opt_pass
627 {
628 public:
629 pass_all_optimizations_g(gcc::context *ctxt)
630 : gimple_opt_pass(pass_data_all_optimizations_g, ctxt)
631 {}
632
633 /* opt_pass methods: */
634 bool gate () { return gate_all_optimizations_g (); }
635
636 }; // class pass_all_optimizations_g
637
638 } // anon namespace
639
640 static gimple_opt_pass *
641 make_pass_all_optimizations_g (gcc::context *ctxt)
642 {
643 return new pass_all_optimizations_g (ctxt);
644 }
645
646 static bool
647 gate_rest_of_compilation (void)
648 {
649 /* Early return if there were errors. We can run afoul of our
650 consistency checks, and there's not really much point in fixing them. */
651 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
652 }
653
654 namespace {
655
656 const pass_data pass_data_rest_of_compilation =
657 {
658 RTL_PASS, /* type */
659 "*rest_of_compilation", /* name */
660 OPTGROUP_NONE, /* optinfo_flags */
661 true, /* has_gate */
662 false, /* has_execute */
663 TV_REST_OF_COMPILATION, /* tv_id */
664 PROP_rtl, /* properties_required */
665 0, /* properties_provided */
666 0, /* properties_destroyed */
667 0, /* todo_flags_start */
668 0, /* todo_flags_finish */
669 };
670
671 class pass_rest_of_compilation : public rtl_opt_pass
672 {
673 public:
674 pass_rest_of_compilation(gcc::context *ctxt)
675 : rtl_opt_pass(pass_data_rest_of_compilation, ctxt)
676 {}
677
678 /* opt_pass methods: */
679 bool gate () { return gate_rest_of_compilation (); }
680
681 }; // class pass_rest_of_compilation
682
683 } // anon namespace
684
685 static rtl_opt_pass *
686 make_pass_rest_of_compilation (gcc::context *ctxt)
687 {
688 return new pass_rest_of_compilation (ctxt);
689 }
690
691 static bool
692 gate_postreload (void)
693 {
694 return reload_completed;
695 }
696
697 namespace {
698
699 const pass_data pass_data_postreload =
700 {
701 RTL_PASS, /* type */
702 "*all-postreload", /* name */
703 OPTGROUP_NONE, /* optinfo_flags */
704 true, /* has_gate */
705 false, /* has_execute */
706 TV_POSTRELOAD, /* tv_id */
707 PROP_rtl, /* properties_required */
708 0, /* properties_provided */
709 0, /* properties_destroyed */
710 0, /* todo_flags_start */
711 TODO_verify_rtl_sharing, /* todo_flags_finish */
712 };
713
714 class pass_postreload : public rtl_opt_pass
715 {
716 public:
717 pass_postreload(gcc::context *ctxt)
718 : rtl_opt_pass(pass_data_postreload, ctxt)
719 {}
720
721 /* opt_pass methods: */
722 bool gate () { return gate_postreload (); }
723
724 }; // class pass_postreload
725
726 } // anon namespace
727
728 static rtl_opt_pass *
729 make_pass_postreload (gcc::context *ctxt)
730 {
731 return new pass_postreload (ctxt);
732 }
733
734
735
736 /* Set the static pass number of pass PASS to ID and record that
737 in the mapping from static pass number to pass. */
738
739 void
740 pass_manager::
741 set_pass_for_id (int id, struct opt_pass *pass)
742 {
743 pass->static_pass_number = id;
744 if (passes_by_id_size <= id)
745 {
746 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
747 memset (passes_by_id + passes_by_id_size, 0,
748 (id + 1 - passes_by_id_size) * sizeof (void *));
749 passes_by_id_size = id + 1;
750 }
751 passes_by_id[id] = pass;
752 }
753
754 /* Return the pass with the static pass number ID. */
755
756 struct opt_pass *
757 pass_manager::get_pass_for_id (int id) const
758 {
759 if (id >= passes_by_id_size)
760 return NULL;
761 return passes_by_id[id];
762 }
763
764 /* Iterate over the pass tree allocating dump file numbers. We want
765 to do this depth first, and independent of whether the pass is
766 enabled or not. */
767
768 void
769 register_one_dump_file (struct opt_pass *pass)
770 {
771 g->get_passes ()->register_one_dump_file (pass);
772 }
773
774 void
775 pass_manager::register_one_dump_file (struct opt_pass *pass)
776 {
777 char *dot_name, *flag_name, *glob_name;
778 const char *name, *full_name, *prefix;
779 char num[10];
780 int flags, id;
781 int optgroup_flags = OPTGROUP_NONE;
782
783 /* See below in next_pass_1. */
784 num[0] = '\0';
785 if (pass->static_pass_number != -1)
786 sprintf (num, "%d", ((int) pass->static_pass_number < 0
787 ? 1 : pass->static_pass_number));
788
789 /* The name is both used to identify the pass for the purposes of plugins,
790 and to specify dump file name and option.
791 The latter two might want something short which is not quite unique; for
792 that reason, we may have a disambiguating prefix, followed by a space
793 to mark the start of the following dump file name / option string. */
794 name = strchr (pass->name, ' ');
795 name = name ? name + 1 : pass->name;
796 dot_name = concat (".", name, num, NULL);
797 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
798 {
799 prefix = "ipa-";
800 flags = TDF_IPA;
801 optgroup_flags |= OPTGROUP_IPA;
802 }
803 else if (pass->type == GIMPLE_PASS)
804 {
805 prefix = "tree-";
806 flags = TDF_TREE;
807 }
808 else
809 {
810 prefix = "rtl-";
811 flags = TDF_RTL;
812 }
813
814 flag_name = concat (prefix, name, num, NULL);
815 glob_name = concat (prefix, name, NULL);
816 optgroup_flags |= pass->optinfo_flags;
817 id = dump_register (dot_name, flag_name, glob_name, flags, optgroup_flags);
818 set_pass_for_id (id, pass);
819 full_name = concat (prefix, pass->name, num, NULL);
820 register_pass_name (pass, full_name);
821 free (CONST_CAST (char *, full_name));
822 }
823
824 /* Recursive worker function for register_dump_files. */
825
826 int
827 pass_manager::
828 register_dump_files_1 (struct opt_pass *pass, int properties)
829 {
830 do
831 {
832 int new_properties = (properties | pass->properties_provided)
833 & ~pass->properties_destroyed;
834
835 if (pass->name && pass->name[0] != '*')
836 register_one_dump_file (pass);
837
838 if (pass->sub)
839 new_properties = register_dump_files_1 (pass->sub, new_properties);
840
841 /* If we have a gate, combine the properties that we could have with
842 and without the pass being examined. */
843 if (pass->has_gate)
844 properties &= new_properties;
845 else
846 properties = new_properties;
847
848 pass = pass->next;
849 }
850 while (pass);
851
852 return properties;
853 }
854
855 /* Register the dump files for the pass_manager starting at PASS.
856 PROPERTIES reflects the properties that are guaranteed to be available at
857 the beginning of the pipeline. */
858
859 void
860 pass_manager::
861 register_dump_files (struct opt_pass *pass,int properties)
862 {
863 pass->properties_required |= properties;
864 register_dump_files_1 (pass, properties);
865 }
866
867 struct pass_registry
868 {
869 const char* unique_name;
870 struct opt_pass *pass;
871 };
872
873 /* Helper for pass_registry hash table. */
874
875 struct pass_registry_hasher : typed_noop_remove <pass_registry>
876 {
877 typedef pass_registry value_type;
878 typedef pass_registry compare_type;
879 static inline hashval_t hash (const value_type *);
880 static inline bool equal (const value_type *, const compare_type *);
881 };
882
883 /* Pass registry hash function. */
884
885 inline hashval_t
886 pass_registry_hasher::hash (const value_type *s)
887 {
888 return htab_hash_string (s->unique_name);
889 }
890
891 /* Hash equal function */
892
893 inline bool
894 pass_registry_hasher::equal (const value_type *s1, const compare_type *s2)
895 {
896 return !strcmp (s1->unique_name, s2->unique_name);
897 }
898
899 static hash_table <pass_registry_hasher> name_to_pass_map;
900
901 /* Register PASS with NAME. */
902
903 static void
904 register_pass_name (struct opt_pass *pass, const char *name)
905 {
906 struct pass_registry **slot;
907 struct pass_registry pr;
908
909 if (!name_to_pass_map.is_created ())
910 name_to_pass_map.create (256);
911
912 pr.unique_name = name;
913 slot = name_to_pass_map.find_slot (&pr, INSERT);
914 if (!*slot)
915 {
916 struct pass_registry *new_pr;
917
918 new_pr = XCNEW (struct pass_registry);
919 new_pr->unique_name = xstrdup (name);
920 new_pr->pass = pass;
921 *slot = new_pr;
922 }
923 else
924 return; /* Ignore plugin passes. */
925 }
926
927 /* Map from pass id to canonicalized pass name. */
928
929 typedef const char *char_ptr;
930 static vec<char_ptr> pass_tab = vNULL;
931
932 /* Callback function for traversing NAME_TO_PASS_MAP. */
933
934 int
935 passes_pass_traverse (pass_registry **p, void *data ATTRIBUTE_UNUSED)
936 {
937 struct opt_pass *pass = (*p)->pass;
938
939 gcc_assert (pass->static_pass_number > 0);
940 gcc_assert (pass_tab.exists ());
941
942 pass_tab[pass->static_pass_number] = (*p)->unique_name;
943
944 return 1;
945 }
946
947 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
948 table for dumping purpose. */
949
950 static void
951 create_pass_tab (void)
952 {
953 if (!flag_dump_passes)
954 return;
955
956 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
957 name_to_pass_map.traverse <void *, passes_pass_traverse> (NULL);
958 }
959
960 static bool override_gate_status (struct opt_pass *, tree, bool);
961
962 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
963 is turned on or not. */
964
965 static void
966 dump_one_pass (struct opt_pass *pass, int pass_indent)
967 {
968 int indent = 3 * pass_indent;
969 const char *pn;
970 bool is_on, is_really_on;
971
972 is_on = pass->has_gate ? pass->gate() : true;
973 is_really_on = override_gate_status (pass, current_function_decl, is_on);
974
975 if (pass->static_pass_number <= 0)
976 pn = pass->name;
977 else
978 pn = pass_tab[pass->static_pass_number];
979
980 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
981 (15 - indent < 0 ? 0 : 15 - indent), " ",
982 is_on ? " ON" : " OFF",
983 ((!is_on) == (!is_really_on) ? ""
984 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
985 }
986
987 /* Dump pass list PASS with indentation INDENT. */
988
989 static void
990 dump_pass_list (struct opt_pass *pass, int indent)
991 {
992 do
993 {
994 dump_one_pass (pass, indent);
995 if (pass->sub)
996 dump_pass_list (pass->sub, indent + 1);
997 pass = pass->next;
998 }
999 while (pass);
1000 }
1001
1002 /* Dump all optimization passes. */
1003
1004 void
1005 dump_passes (void)
1006 {
1007 g->get_passes ()->dump_passes ();
1008 }
1009
1010 void
1011 pass_manager::dump_passes () const
1012 {
1013 struct cgraph_node *n, *node = NULL;
1014
1015 create_pass_tab();
1016
1017 FOR_EACH_FUNCTION (n)
1018 if (DECL_STRUCT_FUNCTION (n->symbol.decl))
1019 {
1020 node = n;
1021 break;
1022 }
1023
1024 if (!node)
1025 return;
1026
1027 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1028
1029 dump_pass_list (all_lowering_passes, 1);
1030 dump_pass_list (all_small_ipa_passes, 1);
1031 dump_pass_list (all_regular_ipa_passes, 1);
1032 dump_pass_list (all_lto_gen_passes, 1);
1033 dump_pass_list (all_late_ipa_passes, 1);
1034 dump_pass_list (all_passes, 1);
1035
1036 pop_cfun ();
1037 }
1038
1039
1040 /* Returns the pass with NAME. */
1041
1042 static struct opt_pass *
1043 get_pass_by_name (const char *name)
1044 {
1045 struct pass_registry **slot, pr;
1046
1047 pr.unique_name = name;
1048 slot = name_to_pass_map.find_slot (&pr, NO_INSERT);
1049
1050 if (!slot || !*slot)
1051 return NULL;
1052
1053 return (*slot)->pass;
1054 }
1055
1056
1057 /* Range [start, last]. */
1058
1059 struct uid_range
1060 {
1061 unsigned int start;
1062 unsigned int last;
1063 const char *assem_name;
1064 struct uid_range *next;
1065 };
1066
1067 typedef struct uid_range *uid_range_p;
1068
1069
1070 static vec<uid_range_p>
1071 enabled_pass_uid_range_tab = vNULL;
1072 static vec<uid_range_p>
1073 disabled_pass_uid_range_tab = vNULL;
1074
1075
1076 /* Parse option string for -fdisable- and -fenable-
1077 The syntax of the options:
1078
1079 -fenable-<pass_name>
1080 -fdisable-<pass_name>
1081
1082 -fenable-<pass_name>=s1:e1,s2:e2,...
1083 -fdisable-<pass_name>=s1:e1,s2:e2,...
1084 */
1085
1086 static void
1087 enable_disable_pass (const char *arg, bool is_enable)
1088 {
1089 struct opt_pass *pass;
1090 char *range_str, *phase_name;
1091 char *argstr = xstrdup (arg);
1092 vec<uid_range_p> *tab = 0;
1093
1094 range_str = strchr (argstr,'=');
1095 if (range_str)
1096 {
1097 *range_str = '\0';
1098 range_str++;
1099 }
1100
1101 phase_name = argstr;
1102 if (!*phase_name)
1103 {
1104 if (is_enable)
1105 error ("unrecognized option -fenable");
1106 else
1107 error ("unrecognized option -fdisable");
1108 free (argstr);
1109 return;
1110 }
1111 pass = get_pass_by_name (phase_name);
1112 if (!pass || pass->static_pass_number == -1)
1113 {
1114 if (is_enable)
1115 error ("unknown pass %s specified in -fenable", phase_name);
1116 else
1117 error ("unknown pass %s specified in -fdisable", phase_name);
1118 free (argstr);
1119 return;
1120 }
1121
1122 if (is_enable)
1123 tab = &enabled_pass_uid_range_tab;
1124 else
1125 tab = &disabled_pass_uid_range_tab;
1126
1127 if ((unsigned) pass->static_pass_number >= tab->length ())
1128 tab->safe_grow_cleared (pass->static_pass_number + 1);
1129
1130 if (!range_str)
1131 {
1132 uid_range_p slot;
1133 uid_range_p new_range = XCNEW (struct uid_range);
1134
1135 new_range->start = 0;
1136 new_range->last = (unsigned)-1;
1137
1138 slot = (*tab)[pass->static_pass_number];
1139 new_range->next = slot;
1140 (*tab)[pass->static_pass_number] = new_range;
1141 if (is_enable)
1142 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1143 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1144 else
1145 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1146 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1147 }
1148 else
1149 {
1150 char *next_range = NULL;
1151 char *one_range = range_str;
1152 char *end_val = NULL;
1153
1154 do
1155 {
1156 uid_range_p slot;
1157 uid_range_p new_range;
1158 char *invalid = NULL;
1159 long start;
1160 char *func_name = NULL;
1161
1162 next_range = strchr (one_range, ',');
1163 if (next_range)
1164 {
1165 *next_range = '\0';
1166 next_range++;
1167 }
1168
1169 end_val = strchr (one_range, ':');
1170 if (end_val)
1171 {
1172 *end_val = '\0';
1173 end_val++;
1174 }
1175 start = strtol (one_range, &invalid, 10);
1176 if (*invalid || start < 0)
1177 {
1178 if (end_val || (one_range[0] >= '0'
1179 && one_range[0] <= '9'))
1180 {
1181 error ("Invalid range %s in option %s",
1182 one_range,
1183 is_enable ? "-fenable" : "-fdisable");
1184 free (argstr);
1185 return;
1186 }
1187 func_name = one_range;
1188 }
1189 if (!end_val)
1190 {
1191 new_range = XCNEW (struct uid_range);
1192 if (!func_name)
1193 {
1194 new_range->start = (unsigned) start;
1195 new_range->last = (unsigned) start;
1196 }
1197 else
1198 {
1199 new_range->start = (unsigned) -1;
1200 new_range->last = (unsigned) -1;
1201 new_range->assem_name = xstrdup (func_name);
1202 }
1203 }
1204 else
1205 {
1206 long last = strtol (end_val, &invalid, 10);
1207 if (*invalid || last < start)
1208 {
1209 error ("Invalid range %s in option %s",
1210 end_val,
1211 is_enable ? "-fenable" : "-fdisable");
1212 free (argstr);
1213 return;
1214 }
1215 new_range = XCNEW (struct uid_range);
1216 new_range->start = (unsigned) start;
1217 new_range->last = (unsigned) last;
1218 }
1219
1220 slot = (*tab)[pass->static_pass_number];
1221 new_range->next = slot;
1222 (*tab)[pass->static_pass_number] = new_range;
1223 if (is_enable)
1224 {
1225 if (new_range->assem_name)
1226 inform (UNKNOWN_LOCATION,
1227 "enable pass %s for function %s",
1228 phase_name, new_range->assem_name);
1229 else
1230 inform (UNKNOWN_LOCATION,
1231 "enable pass %s for functions in the range of [%u, %u]",
1232 phase_name, new_range->start, new_range->last);
1233 }
1234 else
1235 {
1236 if (new_range->assem_name)
1237 inform (UNKNOWN_LOCATION,
1238 "disable pass %s for function %s",
1239 phase_name, new_range->assem_name);
1240 else
1241 inform (UNKNOWN_LOCATION,
1242 "disable pass %s for functions in the range of [%u, %u]",
1243 phase_name, new_range->start, new_range->last);
1244 }
1245
1246 one_range = next_range;
1247 } while (next_range);
1248 }
1249
1250 free (argstr);
1251 }
1252
1253 /* Enable pass specified by ARG. */
1254
1255 void
1256 enable_pass (const char *arg)
1257 {
1258 enable_disable_pass (arg, true);
1259 }
1260
1261 /* Disable pass specified by ARG. */
1262
1263 void
1264 disable_pass (const char *arg)
1265 {
1266 enable_disable_pass (arg, false);
1267 }
1268
1269 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1270
1271 static bool
1272 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
1273 tree func,
1274 vec<uid_range_p> tab)
1275 {
1276 uid_range_p slot, range;
1277 int cgraph_uid;
1278 const char *aname = NULL;
1279
1280 if (!tab.exists ()
1281 || (unsigned) pass->static_pass_number >= tab.length ()
1282 || pass->static_pass_number == -1)
1283 return false;
1284
1285 slot = tab[pass->static_pass_number];
1286 if (!slot)
1287 return false;
1288
1289 cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
1290 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1291 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1292
1293 range = slot;
1294 while (range)
1295 {
1296 if ((unsigned) cgraph_uid >= range->start
1297 && (unsigned) cgraph_uid <= range->last)
1298 return true;
1299 if (range->assem_name && aname
1300 && !strcmp (range->assem_name, aname))
1301 return true;
1302 range = range->next;
1303 }
1304
1305 return false;
1306 }
1307
1308
1309 /* Update static_pass_number for passes (and the flag
1310 TODO_mark_first_instance).
1311
1312 Passes are constructed with static_pass_number preinitialized to 0
1313
1314 This field is used in two different ways: initially as instance numbers
1315 of their kind, and then as ids within the entire pass manager.
1316
1317 Within pass_manager::pass_manager:
1318
1319 * In add_pass_instance(), as called by next_pass_1 in
1320 NEXT_PASS in init_optimization_passes
1321
1322 * When the initial instance of a pass within a pass manager is seen,
1323 it is flagged, and its static_pass_number is set to -1
1324
1325 * On subsequent times that it is seen, the static pass number
1326 is decremented each time, so that if there are e.g. 4 dups,
1327 they have static_pass_number -4, 2, 3, 4 respectively (note
1328 how the initial one is negative and gives the count); these
1329 can be thought of as instance numbers of the specific pass
1330
1331 * Within the register_dump_files () traversal, set_pass_for_id()
1332 is called on each pass, using these instance numbers to create
1333 dumpfile switches, and then overwriting them with a pass id,
1334 which are global to the whole pass manager (based on
1335 (TDI_end + current value of extra_dump_files_in_use) ) */
1336
1337 static void
1338 add_pass_instance (struct opt_pass *new_pass, bool track_duplicates,
1339 opt_pass *initial_pass)
1340 {
1341 /* Are we dealing with the first pass of its kind, or a clone? */
1342 if (new_pass != initial_pass)
1343 {
1344 /* We're dealing with a clone. */
1345 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1346
1347 /* Indicate to register_dump_files that this pass has duplicates,
1348 and so it should rename the dump file. The first instance will
1349 be -1, and be number of duplicates = -static_pass_number - 1.
1350 Subsequent instances will be > 0 and just the duplicate number. */
1351 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1352 {
1353 initial_pass->static_pass_number -= 1;
1354 new_pass->static_pass_number = -initial_pass->static_pass_number;
1355 }
1356 }
1357 else
1358 {
1359 /* We're dealing with the first pass of its kind. */
1360 new_pass->todo_flags_start |= TODO_mark_first_instance;
1361 new_pass->static_pass_number = -1;
1362
1363 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1364 }
1365 }
1366
1367 /* Add a pass to the pass list. Duplicate the pass if it's already
1368 in the list. */
1369
1370 static struct opt_pass **
1371 next_pass_1 (struct opt_pass **list, struct opt_pass *pass,
1372 struct opt_pass *initial_pass)
1373 {
1374 /* Every pass should have a name so that plugins can refer to them. */
1375 gcc_assert (pass->name != NULL);
1376
1377 add_pass_instance (pass, false, initial_pass);
1378 *list = pass;
1379
1380 return &(*list)->next;
1381 }
1382
1383 /* List node for an inserted pass instance. We need to keep track of all
1384 the newly-added pass instances (with 'added_pass_nodes' defined below)
1385 so that we can register their dump files after pass-positioning is finished.
1386 Registering dumping files needs to be post-processed or the
1387 static_pass_number of the opt_pass object would be modified and mess up
1388 the dump file names of future pass instances to be added. */
1389
1390 struct pass_list_node
1391 {
1392 struct opt_pass *pass;
1393 struct pass_list_node *next;
1394 };
1395
1396 static struct pass_list_node *added_pass_nodes = NULL;
1397 static struct pass_list_node *prev_added_pass_node;
1398
1399 /* Insert the pass at the proper position. Return true if the pass
1400 is successfully added.
1401
1402 NEW_PASS_INFO - new pass to be inserted
1403 PASS_LIST - root of the pass list to insert the new pass to */
1404
1405 static bool
1406 position_pass (struct register_pass_info *new_pass_info,
1407 struct opt_pass **pass_list)
1408 {
1409 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
1410 bool success = false;
1411
1412 for ( ; pass; prev_pass = pass, pass = pass->next)
1413 {
1414 /* Check if the current pass is of the same type as the new pass and
1415 matches the name and the instance number of the reference pass. */
1416 if (pass->type == new_pass_info->pass->type
1417 && pass->name
1418 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1419 && ((new_pass_info->ref_pass_instance_number == 0)
1420 || (new_pass_info->ref_pass_instance_number ==
1421 pass->static_pass_number)
1422 || (new_pass_info->ref_pass_instance_number == 1
1423 && pass->todo_flags_start & TODO_mark_first_instance)))
1424 {
1425 struct opt_pass *new_pass;
1426 struct pass_list_node *new_pass_node;
1427
1428 if (new_pass_info->ref_pass_instance_number == 0)
1429 {
1430 new_pass = new_pass_info->pass->clone ();
1431 add_pass_instance (new_pass, true, new_pass_info->pass);
1432 }
1433 else
1434 {
1435 new_pass = new_pass_info->pass;
1436 add_pass_instance (new_pass, true, new_pass);
1437 }
1438
1439 /* Insert the new pass instance based on the positioning op. */
1440 switch (new_pass_info->pos_op)
1441 {
1442 case PASS_POS_INSERT_AFTER:
1443 new_pass->next = pass->next;
1444 pass->next = new_pass;
1445
1446 /* Skip newly inserted pass to avoid repeated
1447 insertions in the case where the new pass and the
1448 existing one have the same name. */
1449 pass = new_pass;
1450 break;
1451 case PASS_POS_INSERT_BEFORE:
1452 new_pass->next = pass;
1453 if (prev_pass)
1454 prev_pass->next = new_pass;
1455 else
1456 *pass_list = new_pass;
1457 break;
1458 case PASS_POS_REPLACE:
1459 new_pass->next = pass->next;
1460 if (prev_pass)
1461 prev_pass->next = new_pass;
1462 else
1463 *pass_list = new_pass;
1464 new_pass->sub = pass->sub;
1465 new_pass->tv_id = pass->tv_id;
1466 pass = new_pass;
1467 break;
1468 default:
1469 error ("invalid pass positioning operation");
1470 return false;
1471 }
1472
1473 /* Save the newly added pass (instance) in the added_pass_nodes
1474 list so that we can register its dump file later. Note that
1475 we cannot register the dump file now because doing so will modify
1476 the static_pass_number of the opt_pass object and therefore
1477 mess up the dump file name of future instances. */
1478 new_pass_node = XCNEW (struct pass_list_node);
1479 new_pass_node->pass = new_pass;
1480 if (!added_pass_nodes)
1481 added_pass_nodes = new_pass_node;
1482 else
1483 prev_added_pass_node->next = new_pass_node;
1484 prev_added_pass_node = new_pass_node;
1485
1486 success = true;
1487 }
1488
1489 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1490 success = true;
1491 }
1492
1493 return success;
1494 }
1495
1496 /* Hooks a new pass into the pass lists.
1497
1498 PASS_INFO - pass information that specifies the opt_pass object,
1499 reference pass, instance number, and how to position
1500 the pass */
1501
1502 void
1503 register_pass (struct register_pass_info *pass_info)
1504 {
1505 g->get_passes ()->register_pass (pass_info);
1506
1507 }
1508
1509 void
1510 pass_manager::register_pass (struct register_pass_info *pass_info)
1511 {
1512 bool all_instances, success;
1513
1514 /* The checks below could fail in buggy plugins. Existing GCC
1515 passes should never fail these checks, so we mention plugin in
1516 the messages. */
1517 if (!pass_info->pass)
1518 fatal_error ("plugin cannot register a missing pass");
1519
1520 if (!pass_info->pass->name)
1521 fatal_error ("plugin cannot register an unnamed pass");
1522
1523 if (!pass_info->reference_pass_name)
1524 fatal_error
1525 ("plugin cannot register pass %qs without reference pass name",
1526 pass_info->pass->name);
1527
1528 /* Try to insert the new pass to the pass lists. We need to check
1529 all five lists as the reference pass could be in one (or all) of
1530 them. */
1531 all_instances = pass_info->ref_pass_instance_number == 0;
1532 success = position_pass (pass_info, &all_lowering_passes);
1533 if (!success || all_instances)
1534 success |= position_pass (pass_info, &all_small_ipa_passes);
1535 if (!success || all_instances)
1536 success |= position_pass (pass_info, &all_regular_ipa_passes);
1537 if (!success || all_instances)
1538 success |= position_pass (pass_info, &all_lto_gen_passes);
1539 if (!success || all_instances)
1540 success |= position_pass (pass_info, &all_late_ipa_passes);
1541 if (!success || all_instances)
1542 success |= position_pass (pass_info, &all_passes);
1543 if (!success)
1544 fatal_error
1545 ("pass %qs not found but is referenced by new pass %qs",
1546 pass_info->reference_pass_name, pass_info->pass->name);
1547
1548 /* OK, we have successfully inserted the new pass. We need to register
1549 the dump files for the newly added pass and its duplicates (if any).
1550 Because the registration of plugin/backend passes happens after the
1551 command-line options are parsed, the options that specify single
1552 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1553 passes. Therefore we currently can only enable dumping of
1554 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1555 are specified. While doing so, we also delete the pass_list_node
1556 objects created during pass positioning. */
1557 while (added_pass_nodes)
1558 {
1559 struct pass_list_node *next_node = added_pass_nodes->next;
1560 enum tree_dump_index tdi;
1561 register_one_dump_file (added_pass_nodes->pass);
1562 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1563 || added_pass_nodes->pass->type == IPA_PASS)
1564 tdi = TDI_ipa_all;
1565 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1566 tdi = TDI_tree_all;
1567 else
1568 tdi = TDI_rtl_all;
1569 /* Check if dump-all flag is specified. */
1570 if (get_dump_file_info (tdi)->pstate)
1571 get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1572 ->pstate = get_dump_file_info (tdi)->pstate;
1573 XDELETE (added_pass_nodes);
1574 added_pass_nodes = next_node;
1575 }
1576 }
1577
1578 /* Construct the pass tree. The sequencing of passes is driven by
1579 the cgraph routines:
1580
1581 finalize_compilation_unit ()
1582 for each node N in the cgraph
1583 cgraph_analyze_function (N)
1584 cgraph_lower_function (N) -> all_lowering_passes
1585
1586 If we are optimizing, compile is then invoked:
1587
1588 compile ()
1589 ipa_passes () -> all_small_ipa_passes
1590 -> Analysis of all_regular_ipa_passes
1591 * possible LTO streaming at copmilation time *
1592 -> Execution of all_regular_ipa_passes
1593 * possible LTO streaming at link time *
1594 -> all_late_ipa_passes
1595 expand_all_functions ()
1596 for each node N in the cgraph
1597 expand_function (N) -> Transformation of all_regular_ipa_passes
1598 -> all_passes
1599 */
1600
1601 void *
1602 pass_manager::operator new (size_t sz)
1603 {
1604 /* Ensure that all fields of the pass manager are zero-initialized. */
1605 return ggc_internal_cleared_alloc_stat (sz MEM_STAT_INFO);
1606 }
1607
1608 pass_manager::pass_manager (context *ctxt)
1609 : all_passes(NULL), all_small_ipa_passes(NULL), all_lowering_passes(NULL),
1610 all_regular_ipa_passes(NULL), all_lto_gen_passes(NULL),
1611 all_late_ipa_passes(NULL), passes_by_id(NULL), passes_by_id_size(0),
1612 ctxt_(ctxt)
1613 {
1614 struct opt_pass **p;
1615
1616 /* Initialize the pass_lists array. */
1617 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1618 GCC_PASS_LISTS
1619 #undef DEF_PASS_LIST
1620
1621 /* Build the tree of passes. */
1622
1623 #define INSERT_PASSES_AFTER(PASS) \
1624 p = &(PASS);
1625
1626 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1627 { \
1628 struct opt_pass **p = &(PASS ## _1)->sub;
1629
1630 #define POP_INSERT_PASSES() \
1631 }
1632
1633 #define NEXT_PASS(PASS, NUM) \
1634 do { \
1635 gcc_assert (NULL == PASS ## _ ## NUM); \
1636 if ((NUM) == 1) \
1637 PASS ## _1 = make_##PASS (ctxt_); \
1638 else \
1639 { \
1640 gcc_assert (PASS ## _1); \
1641 PASS ## _ ## NUM = PASS ## _1->clone (); \
1642 } \
1643 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1644 } while (0)
1645
1646 #define TERMINATE_PASS_LIST() \
1647 *p = NULL;
1648
1649 #include "pass-instances.def"
1650
1651 #undef INSERT_PASSES_AFTER
1652 #undef PUSH_INSERT_PASSES_WITHIN
1653 #undef POP_INSERT_PASSES
1654 #undef NEXT_PASS
1655 #undef TERMINATE_PASS_LIST
1656
1657 /* Register the passes with the tree dump code. */
1658 register_dump_files (all_lowering_passes, PROP_gimple_any);
1659 register_dump_files (all_small_ipa_passes,
1660 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1661 | PROP_cfg);
1662 register_dump_files (all_regular_ipa_passes,
1663 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1664 | PROP_cfg);
1665 register_dump_files (all_lto_gen_passes,
1666 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1667 | PROP_cfg);
1668 register_dump_files (all_late_ipa_passes,
1669 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1670 | PROP_cfg);
1671 register_dump_files (all_passes,
1672 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1673 | PROP_cfg);
1674 }
1675
1676 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1677 function CALLBACK for every function in the call graph. Otherwise,
1678 call CALLBACK on the current function. */
1679
1680 static void
1681 do_per_function (void (*callback) (void *data), void *data)
1682 {
1683 if (current_function_decl)
1684 callback (data);
1685 else
1686 {
1687 struct cgraph_node *node;
1688 FOR_EACH_DEFINED_FUNCTION (node)
1689 if (node->symbol.analyzed && gimple_has_body_p (node->symbol.decl)
1690 && (!node->clone_of || node->symbol.decl != node->clone_of->symbol.decl))
1691 {
1692 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1693 callback (data);
1694 if (!flag_wpa)
1695 {
1696 free_dominance_info (CDI_DOMINATORS);
1697 free_dominance_info (CDI_POST_DOMINATORS);
1698 }
1699 pop_cfun ();
1700 ggc_collect ();
1701 }
1702 }
1703 }
1704
1705 /* Because inlining might remove no-longer reachable nodes, we need to
1706 keep the array visible to garbage collector to avoid reading collected
1707 out nodes. */
1708 static int nnodes;
1709 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1710
1711 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1712 function CALLBACK for every function in the call graph. Otherwise,
1713 call CALLBACK on the current function.
1714 This function is global so that plugins can use it. */
1715 void
1716 do_per_function_toporder (void (*callback) (void *data), void *data)
1717 {
1718 int i;
1719
1720 if (current_function_decl)
1721 callback (data);
1722 else
1723 {
1724 gcc_assert (!order);
1725 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1726 nnodes = ipa_reverse_postorder (order);
1727 for (i = nnodes - 1; i >= 0; i--)
1728 order[i]->process = 1;
1729 for (i = nnodes - 1; i >= 0; i--)
1730 {
1731 struct cgraph_node *node = order[i];
1732
1733 /* Allow possibly removed nodes to be garbage collected. */
1734 order[i] = NULL;
1735 node->process = 0;
1736 if (cgraph_function_with_gimple_body_p (node))
1737 {
1738 cgraph_get_body (node);
1739 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1740 callback (data);
1741 free_dominance_info (CDI_DOMINATORS);
1742 free_dominance_info (CDI_POST_DOMINATORS);
1743 pop_cfun ();
1744 ggc_collect ();
1745 }
1746 }
1747 }
1748 ggc_free (order);
1749 order = NULL;
1750 nnodes = 0;
1751 }
1752
1753 /* Helper function to perform function body dump. */
1754
1755 static void
1756 execute_function_dump (void *data ATTRIBUTE_UNUSED)
1757 {
1758 if (dump_file && current_function_decl)
1759 {
1760 if (cfun->curr_properties & PROP_trees)
1761 dump_function_to_file (current_function_decl, dump_file, dump_flags);
1762 else
1763 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1764
1765 /* Flush the file. If verification fails, we won't be able to
1766 close the file before aborting. */
1767 fflush (dump_file);
1768
1769 if ((cfun->curr_properties & PROP_cfg)
1770 && (dump_flags & TDF_GRAPH))
1771 print_graph_cfg (dump_file_name, cfun);
1772 }
1773 }
1774
1775 static struct profile_record *profile_record;
1776
1777 /* Do profile consistency book-keeping for the pass with static number INDEX.
1778 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1779 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1780 if we are only book-keeping on passes that may have selectively disabled
1781 themselves on a given function. */
1782 static void
1783 check_profile_consistency (int index, int subpass, bool run)
1784 {
1785 pass_manager *passes = g->get_passes ();
1786 if (index == -1)
1787 return;
1788 if (!profile_record)
1789 profile_record = XCNEWVEC (struct profile_record,
1790 passes->passes_by_id_size);
1791 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1792 gcc_assert (subpass < 2);
1793 profile_record[index].run |= run;
1794 account_profile_record (&profile_record[index], subpass);
1795 }
1796
1797 /* Output profile consistency. */
1798
1799 void
1800 dump_profile_report (void)
1801 {
1802 g->get_passes ()->dump_profile_report ();
1803 }
1804
1805 void
1806 pass_manager::dump_profile_report () const
1807 {
1808 int i, j;
1809 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1810 gcov_type last_time = 0, last_size = 0;
1811 double rel_time_change, rel_size_change;
1812 int last_reported = 0;
1813
1814 if (!profile_record)
1815 return;
1816 fprintf (stderr, "\nProfile consistency report:\n\n");
1817 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1818 fprintf (stderr, " |freq count |freq count |size time\n");
1819
1820 for (i = 0; i < passes_by_id_size; i++)
1821 for (j = 0 ; j < 2; j++)
1822 if (profile_record[i].run)
1823 {
1824 if (last_time)
1825 rel_time_change = (profile_record[i].time[j]
1826 - (double)last_time) * 100 / (double)last_time;
1827 else
1828 rel_time_change = 0;
1829 if (last_size)
1830 rel_size_change = (profile_record[i].size[j]
1831 - (double)last_size) * 100 / (double)last_size;
1832 else
1833 rel_size_change = 0;
1834
1835 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1836 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1837 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1838 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1839 || rel_time_change || rel_size_change)
1840 {
1841 last_reported = i;
1842 fprintf (stderr, "%-20s %s",
1843 passes_by_id [i]->name,
1844 j ? "(after TODO)" : " ");
1845 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1846 fprintf (stderr, "| %+5i",
1847 profile_record[i].num_mismatched_freq_in[j]
1848 - last_freq_in);
1849 else
1850 fprintf (stderr, "| ");
1851 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1852 fprintf (stderr, " %+5i",
1853 profile_record[i].num_mismatched_count_in[j]
1854 - last_count_in);
1855 else
1856 fprintf (stderr, " ");
1857 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1858 fprintf (stderr, "| %+5i",
1859 profile_record[i].num_mismatched_freq_out[j]
1860 - last_freq_out);
1861 else
1862 fprintf (stderr, "| ");
1863 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1864 fprintf (stderr, " %+5i",
1865 profile_record[i].num_mismatched_count_out[j]
1866 - last_count_out);
1867 else
1868 fprintf (stderr, " ");
1869
1870 /* Size/time units change across gimple and RTL. */
1871 if (i == pass_expand_1->static_pass_number)
1872 fprintf (stderr, "|----------");
1873 else
1874 {
1875 if (rel_size_change)
1876 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1877 else
1878 fprintf (stderr, "| ");
1879 if (rel_time_change)
1880 fprintf (stderr, " %+8.4f%%", rel_time_change);
1881 }
1882 fprintf (stderr, "\n");
1883 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1884 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1885 last_count_in = profile_record[i].num_mismatched_count_in[j];
1886 last_count_out = profile_record[i].num_mismatched_count_out[j];
1887 }
1888 else if (j && last_reported != i)
1889 {
1890 last_reported = i;
1891 fprintf (stderr, "%-20s ------------| | |\n",
1892 passes_by_id [i]->name);
1893 }
1894 last_time = profile_record[i].time[j];
1895 last_size = profile_record[i].size[j];
1896 }
1897 }
1898
1899 /* Perform all TODO actions that ought to be done on each function. */
1900
1901 static void
1902 execute_function_todo (void *data)
1903 {
1904 unsigned int flags = (size_t)data;
1905 flags &= ~cfun->last_verified;
1906 if (!flags)
1907 return;
1908
1909 /* Always cleanup the CFG before trying to update SSA. */
1910 if (flags & TODO_cleanup_cfg)
1911 {
1912 cleanup_tree_cfg ();
1913
1914 /* When cleanup_tree_cfg merges consecutive blocks, it may
1915 perform some simplistic propagation when removing single
1916 valued PHI nodes. This propagation may, in turn, cause the
1917 SSA form to become out-of-date (see PR 22037). So, even
1918 if the parent pass had not scheduled an SSA update, we may
1919 still need to do one. */
1920 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1921 flags |= TODO_update_ssa;
1922 }
1923
1924 if (flags & TODO_update_ssa_any)
1925 {
1926 unsigned update_flags = flags & TODO_update_ssa_any;
1927 update_ssa (update_flags);
1928 cfun->last_verified &= ~TODO_verify_ssa;
1929 }
1930
1931 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1932 compute_may_aliases ();
1933
1934 if (optimize && (flags & TODO_update_address_taken))
1935 execute_update_addresses_taken ();
1936
1937 if (flags & TODO_remove_unused_locals)
1938 remove_unused_locals ();
1939
1940 if (flags & TODO_rebuild_frequencies)
1941 rebuild_frequencies ();
1942
1943 if (flags & TODO_rebuild_cgraph_edges)
1944 rebuild_cgraph_edges ();
1945
1946 /* If we've seen errors do not bother running any verifiers. */
1947 if (seen_error ())
1948 return;
1949
1950 #if defined ENABLE_CHECKING
1951 if (flags & TODO_verify_ssa
1952 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1953 {
1954 verify_gimple_in_cfg (cfun);
1955 verify_ssa (true);
1956 }
1957 else if (flags & TODO_verify_stmts)
1958 verify_gimple_in_cfg (cfun);
1959 if (flags & TODO_verify_flow)
1960 verify_flow_info ();
1961 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1962 verify_loop_closed_ssa (false);
1963 if (flags & TODO_verify_rtl_sharing)
1964 verify_rtl_sharing ();
1965 #endif
1966
1967 cfun->last_verified = flags & TODO_verify_all;
1968 }
1969
1970 /* Perform all TODO actions. */
1971 static void
1972 execute_todo (unsigned int flags)
1973 {
1974 #if defined ENABLE_CHECKING
1975 if (cfun
1976 && need_ssa_update_p (cfun))
1977 gcc_assert (flags & TODO_update_ssa_any);
1978 #endif
1979
1980 timevar_push (TV_TODO);
1981
1982 /* Inform the pass whether it is the first time it is run. */
1983 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1984
1985 statistics_fini_pass ();
1986
1987 do_per_function (execute_function_todo, (void *)(size_t) flags);
1988
1989 /* Always remove functions just as before inlining: IPA passes might be
1990 interested to see bodies of extern inline functions that are not inlined
1991 to analyze side effects. The full removal is done just at the end
1992 of IPA pass queue. */
1993 if (flags & TODO_remove_functions)
1994 {
1995 gcc_assert (!cfun);
1996 symtab_remove_unreachable_nodes (true, dump_file);
1997 }
1998
1999 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2000 {
2001 gcc_assert (!cfun);
2002 dump_symtab (dump_file);
2003 /* Flush the file. If verification fails, we won't be able to
2004 close the file before aborting. */
2005 fflush (dump_file);
2006 }
2007
2008 /* Now that the dumping has been done, we can get rid of the optional
2009 df problems. */
2010 if (flags & TODO_df_finish)
2011 df_finish_pass ((flags & TODO_df_verify) != 0);
2012
2013 timevar_pop (TV_TODO);
2014 }
2015
2016 /* Verify invariants that should hold between passes. This is a place
2017 to put simple sanity checks. */
2018
2019 static void
2020 verify_interpass_invariants (void)
2021 {
2022 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2023 }
2024
2025 /* Clear the last verified flag. */
2026
2027 static void
2028 clear_last_verified (void *data ATTRIBUTE_UNUSED)
2029 {
2030 cfun->last_verified = 0;
2031 }
2032
2033 /* Helper function. Verify that the properties has been turn into the
2034 properties expected by the pass. */
2035
2036 #ifdef ENABLE_CHECKING
2037 static void
2038 verify_curr_properties (void *data)
2039 {
2040 unsigned int props = (size_t)data;
2041 gcc_assert ((cfun->curr_properties & props) == props);
2042 }
2043 #endif
2044
2045 /* Initialize pass dump file. */
2046 /* This is non-static so that the plugins can use it. */
2047
2048 bool
2049 pass_init_dump_file (struct opt_pass *pass)
2050 {
2051 /* If a dump file name is present, open it if enabled. */
2052 if (pass->static_pass_number != -1)
2053 {
2054 timevar_push (TV_DUMP);
2055 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
2056 dump_file_name = get_dump_file_name (pass->static_pass_number);
2057 dump_start (pass->static_pass_number, &dump_flags);
2058 if (dump_file && current_function_decl)
2059 dump_function_header (dump_file, current_function_decl, dump_flags);
2060 if (initializing_dump
2061 && dump_file && (dump_flags & TDF_GRAPH)
2062 && cfun && (cfun->curr_properties & PROP_cfg))
2063 clean_graph_dump_file (dump_file_name);
2064 timevar_pop (TV_DUMP);
2065 return initializing_dump;
2066 }
2067 else
2068 return false;
2069 }
2070
2071 /* Flush PASS dump file. */
2072 /* This is non-static so that plugins can use it. */
2073
2074 void
2075 pass_fini_dump_file (struct opt_pass *pass)
2076 {
2077 timevar_push (TV_DUMP);
2078
2079 /* Flush and close dump file. */
2080 if (dump_file_name)
2081 {
2082 free (CONST_CAST (char *, dump_file_name));
2083 dump_file_name = NULL;
2084 }
2085
2086 dump_finish (pass->static_pass_number);
2087 timevar_pop (TV_DUMP);
2088 }
2089
2090 /* After executing the pass, apply expected changes to the function
2091 properties. */
2092
2093 static void
2094 update_properties_after_pass (void *data)
2095 {
2096 struct opt_pass *pass = (struct opt_pass *) data;
2097 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
2098 & ~pass->properties_destroyed;
2099 }
2100
2101 /* Execute summary generation for all of the passes in IPA_PASS. */
2102
2103 void
2104 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
2105 {
2106 while (ipa_pass)
2107 {
2108 struct opt_pass *pass = ipa_pass;
2109
2110 /* Execute all of the IPA_PASSes in the list. */
2111 if (ipa_pass->type == IPA_PASS
2112 && ((!pass->has_gate) || pass->gate ())
2113 && ipa_pass->generate_summary)
2114 {
2115 pass_init_dump_file (pass);
2116
2117 /* If a timevar is present, start it. */
2118 if (pass->tv_id)
2119 timevar_push (pass->tv_id);
2120
2121 ipa_pass->generate_summary ();
2122
2123 /* Stop timevar. */
2124 if (pass->tv_id)
2125 timevar_pop (pass->tv_id);
2126
2127 pass_fini_dump_file (pass);
2128 }
2129 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->next;
2130 }
2131 }
2132
2133 /* Execute IPA_PASS function transform on NODE. */
2134
2135 static void
2136 execute_one_ipa_transform_pass (struct cgraph_node *node,
2137 struct ipa_opt_pass_d *ipa_pass)
2138 {
2139 struct opt_pass *pass = ipa_pass;
2140 unsigned int todo_after = 0;
2141
2142 current_pass = pass;
2143 if (!ipa_pass->function_transform)
2144 return;
2145
2146 /* Note that the folders should only create gimple expressions.
2147 This is a hack until the new folder is ready. */
2148 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2149
2150 pass_init_dump_file (pass);
2151
2152 /* Run pre-pass verification. */
2153 execute_todo (ipa_pass->function_transform_todo_flags_start);
2154
2155 /* If a timevar is present, start it. */
2156 if (pass->tv_id != TV_NONE)
2157 timevar_push (pass->tv_id);
2158
2159 /* Do it! */
2160 todo_after = ipa_pass->function_transform (node);
2161
2162 /* Stop timevar. */
2163 if (pass->tv_id != TV_NONE)
2164 timevar_pop (pass->tv_id);
2165
2166 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2167 check_profile_consistency (pass->static_pass_number, 0, true);
2168
2169 /* Run post-pass cleanup and verification. */
2170 execute_todo (todo_after);
2171 verify_interpass_invariants ();
2172 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2173 check_profile_consistency (pass->static_pass_number, 1, true);
2174
2175 do_per_function (execute_function_dump, NULL);
2176 pass_fini_dump_file (pass);
2177
2178 current_pass = NULL;
2179
2180 /* Signal this is a suitable GC collection point. */
2181 if (!(todo_after & TODO_do_not_ggc_collect))
2182 ggc_collect ();
2183 }
2184
2185 /* For the current function, execute all ipa transforms. */
2186
2187 void
2188 execute_all_ipa_transforms (void)
2189 {
2190 struct cgraph_node *node;
2191 if (!cfun)
2192 return;
2193 node = cgraph_get_node (current_function_decl);
2194
2195 if (node->ipa_transforms_to_apply.exists ())
2196 {
2197 unsigned int i;
2198
2199 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2200 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2201 node->ipa_transforms_to_apply.release ();
2202 }
2203 }
2204
2205 /* Callback for do_per_function to apply all IPA transforms. */
2206
2207 static void
2208 apply_ipa_transforms (void *data)
2209 {
2210 struct cgraph_node *node = cgraph_get_node (current_function_decl);
2211 if (!node->global.inlined_to && node->ipa_transforms_to_apply.exists ())
2212 {
2213 *(bool *)data = true;
2214 execute_all_ipa_transforms();
2215 rebuild_cgraph_edges ();
2216 }
2217 }
2218
2219 /* Check if PASS is explicitly disabled or enabled and return
2220 the gate status. FUNC is the function to be processed, and
2221 GATE_STATUS is the gate status determined by pass manager by
2222 default. */
2223
2224 static bool
2225 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
2226 {
2227 bool explicitly_enabled = false;
2228 bool explicitly_disabled = false;
2229
2230 explicitly_enabled
2231 = is_pass_explicitly_enabled_or_disabled (pass, func,
2232 enabled_pass_uid_range_tab);
2233 explicitly_disabled
2234 = is_pass_explicitly_enabled_or_disabled (pass, func,
2235 disabled_pass_uid_range_tab);
2236
2237 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2238
2239 return gate_status;
2240 }
2241
2242
2243 /* Execute PASS. */
2244
2245 bool
2246 execute_one_pass (struct opt_pass *pass)
2247 {
2248 unsigned int todo_after = 0;
2249
2250 bool gate_status;
2251
2252 /* IPA passes are executed on whole program, so cfun should be NULL.
2253 Other passes need function context set. */
2254 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2255 gcc_assert (!cfun && !current_function_decl);
2256 else
2257 gcc_assert (cfun && current_function_decl);
2258
2259 current_pass = pass;
2260
2261 /* Check whether gate check should be avoided.
2262 User controls the value of the gate through the parameter "gate_status". */
2263 gate_status = pass->has_gate ? pass->gate() : true;
2264 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2265
2266 /* Override gate with plugin. */
2267 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2268
2269 if (!gate_status)
2270 {
2271 /* Run so passes selectively disabling themselves on a given function
2272 are not miscounted. */
2273 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2274 {
2275 check_profile_consistency (pass->static_pass_number, 0, false);
2276 check_profile_consistency (pass->static_pass_number, 1, false);
2277 }
2278 current_pass = NULL;
2279 return false;
2280 }
2281
2282 /* Pass execution event trigger: useful to identify passes being
2283 executed. */
2284 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2285
2286 /* SIPLE IPA passes do not handle callgraphs with IPA transforms in it.
2287 Apply all trnasforms first. */
2288 if (pass->type == SIMPLE_IPA_PASS)
2289 {
2290 bool applied = false;
2291 do_per_function (apply_ipa_transforms, (void *)&applied);
2292 if (applied)
2293 symtab_remove_unreachable_nodes (true, dump_file);
2294 /* Restore current_pass. */
2295 current_pass = pass;
2296 }
2297
2298 if (!quiet_flag && !cfun)
2299 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2300
2301 /* Note that the folders should only create gimple expressions.
2302 This is a hack until the new folder is ready. */
2303 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2304
2305 pass_init_dump_file (pass);
2306
2307 /* Run pre-pass verification. */
2308 execute_todo (pass->todo_flags_start);
2309
2310 #ifdef ENABLE_CHECKING
2311 do_per_function (verify_curr_properties,
2312 (void *)(size_t)pass->properties_required);
2313 #endif
2314
2315 /* If a timevar is present, start it. */
2316 if (pass->tv_id != TV_NONE)
2317 timevar_push (pass->tv_id);
2318
2319 /* Do it! */
2320 if (pass->has_execute)
2321 {
2322 todo_after = pass->execute ();
2323 do_per_function (clear_last_verified, NULL);
2324 }
2325
2326 /* Stop timevar. */
2327 if (pass->tv_id != TV_NONE)
2328 timevar_pop (pass->tv_id);
2329
2330 do_per_function (update_properties_after_pass, pass);
2331
2332 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2333 check_profile_consistency (pass->static_pass_number, 0, true);
2334
2335 /* Run post-pass cleanup and verification. */
2336 execute_todo (todo_after | pass->todo_flags_finish);
2337 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2338 check_profile_consistency (pass->static_pass_number, 1, true);
2339
2340 verify_interpass_invariants ();
2341 do_per_function (execute_function_dump, NULL);
2342 if (pass->type == IPA_PASS)
2343 {
2344 struct cgraph_node *node;
2345 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2346 node->ipa_transforms_to_apply.safe_push ((struct ipa_opt_pass_d *)pass);
2347 }
2348
2349 if (!current_function_decl)
2350 cgraph_process_new_functions ();
2351
2352 pass_fini_dump_file (pass);
2353
2354 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2355 gcc_assert (!(cfun->curr_properties & PROP_trees)
2356 || pass->type != RTL_PASS);
2357
2358 current_pass = NULL;
2359
2360 /* Signal this is a suitable GC collection point. */
2361 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2362 ggc_collect ();
2363
2364 return true;
2365 }
2366
2367 void
2368 execute_pass_list (struct opt_pass *pass)
2369 {
2370 do
2371 {
2372 gcc_assert (pass->type == GIMPLE_PASS
2373 || pass->type == RTL_PASS);
2374 if (execute_one_pass (pass) && pass->sub)
2375 execute_pass_list (pass->sub);
2376 pass = pass->next;
2377 }
2378 while (pass);
2379 }
2380
2381 /* Same as execute_pass_list but assume that subpasses of IPA passes
2382 are local passes. If SET is not NULL, write out summaries of only
2383 those node in SET. */
2384
2385 static void
2386 ipa_write_summaries_2 (struct opt_pass *pass, struct lto_out_decl_state *state)
2387 {
2388 while (pass)
2389 {
2390 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2391 gcc_assert (!current_function_decl);
2392 gcc_assert (!cfun);
2393 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2394 if (pass->type == IPA_PASS
2395 && ipa_pass->write_summary
2396 && ((!pass->has_gate) || pass->gate ()))
2397 {
2398 /* If a timevar is present, start it. */
2399 if (pass->tv_id)
2400 timevar_push (pass->tv_id);
2401
2402 pass_init_dump_file (pass);
2403
2404 ipa_pass->write_summary ();
2405
2406 pass_fini_dump_file (pass);
2407
2408 /* If a timevar is present, start it. */
2409 if (pass->tv_id)
2410 timevar_pop (pass->tv_id);
2411 }
2412
2413 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2414 ipa_write_summaries_2 (pass->sub, state);
2415
2416 pass = pass->next;
2417 }
2418 }
2419
2420 /* Helper function of ipa_write_summaries. Creates and destroys the
2421 decl state and calls ipa_write_summaries_2 for all passes that have
2422 summaries. SET is the set of nodes to be written. */
2423
2424 static void
2425 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2426 {
2427 pass_manager *passes = g->get_passes ();
2428 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2429 state->symtab_node_encoder = encoder;
2430
2431 lto_push_out_decl_state (state);
2432
2433 gcc_assert (!flag_wpa);
2434 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2435 ipa_write_summaries_2 (passes->all_lto_gen_passes, state);
2436
2437 gcc_assert (lto_get_out_decl_state () == state);
2438 lto_pop_out_decl_state ();
2439 lto_delete_out_decl_state (state);
2440 }
2441
2442 /* Write out summaries for all the nodes in the callgraph. */
2443
2444 void
2445 ipa_write_summaries (void)
2446 {
2447 lto_symtab_encoder_t encoder;
2448 int i, order_pos;
2449 struct varpool_node *vnode;
2450 struct cgraph_node *node;
2451 struct cgraph_node **order;
2452
2453 if (!flag_generate_lto || seen_error ())
2454 return;
2455
2456 encoder = lto_symtab_encoder_new (false);
2457
2458 /* Create the callgraph set in the same order used in
2459 cgraph_expand_all_functions. This mostly facilitates debugging,
2460 since it causes the gimple file to be processed in the same order
2461 as the source code. */
2462 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2463 order_pos = ipa_reverse_postorder (order);
2464 gcc_assert (order_pos == cgraph_n_nodes);
2465
2466 for (i = order_pos - 1; i >= 0; i--)
2467 {
2468 struct cgraph_node *node = order[i];
2469
2470 if (cgraph_function_with_gimple_body_p (node))
2471 {
2472 /* When streaming out references to statements as part of some IPA
2473 pass summary, the statements need to have uids assigned and the
2474 following does that for all the IPA passes here. Naturally, this
2475 ordering then matches the one IPA-passes get in their stmt_fixup
2476 hooks. */
2477
2478 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2479 renumber_gimple_stmt_uids ();
2480 pop_cfun ();
2481 }
2482 if (node->symbol.definition)
2483 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
2484 }
2485
2486 FOR_EACH_DEFINED_FUNCTION (node)
2487 if (node->symbol.alias)
2488 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)node);
2489 FOR_EACH_DEFINED_VARIABLE (vnode)
2490 lto_set_symtab_encoder_in_partition (encoder, (symtab_node)vnode);
2491
2492 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2493
2494 free (order);
2495 }
2496
2497 /* Same as execute_pass_list but assume that subpasses of IPA passes
2498 are local passes. If SET is not NULL, write out optimization summaries of
2499 only those node in SET. */
2500
2501 static void
2502 ipa_write_optimization_summaries_1 (struct opt_pass *pass, struct lto_out_decl_state *state)
2503 {
2504 while (pass)
2505 {
2506 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2507 gcc_assert (!current_function_decl);
2508 gcc_assert (!cfun);
2509 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2510 if (pass->type == IPA_PASS
2511 && ipa_pass->write_optimization_summary
2512 && ((!pass->has_gate) || pass->gate ()))
2513 {
2514 /* If a timevar is present, start it. */
2515 if (pass->tv_id)
2516 timevar_push (pass->tv_id);
2517
2518 pass_init_dump_file (pass);
2519
2520 ipa_pass->write_optimization_summary ();
2521
2522 pass_fini_dump_file (pass);
2523
2524 /* If a timevar is present, start it. */
2525 if (pass->tv_id)
2526 timevar_pop (pass->tv_id);
2527 }
2528
2529 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2530 ipa_write_optimization_summaries_1 (pass->sub, state);
2531
2532 pass = pass->next;
2533 }
2534 }
2535
2536 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2537 NULL, write out all summaries of all nodes. */
2538
2539 void
2540 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2541 {
2542 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2543 lto_symtab_encoder_iterator lsei;
2544 state->symtab_node_encoder = encoder;
2545
2546 lto_push_out_decl_state (state);
2547 for (lsei = lsei_start_function_in_partition (encoder);
2548 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2549 {
2550 struct cgraph_node *node = lsei_cgraph_node (lsei);
2551 /* When streaming out references to statements as part of some IPA
2552 pass summary, the statements need to have uids assigned.
2553
2554 For functions newly born at WPA stage we need to initialize
2555 the uids here. */
2556 if (node->symbol.definition
2557 && gimple_has_body_p (node->symbol.decl))
2558 {
2559 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
2560 renumber_gimple_stmt_uids ();
2561 pop_cfun ();
2562 }
2563 }
2564
2565 gcc_assert (flag_wpa);
2566 pass_manager *passes = g->get_passes ();
2567 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2568 ipa_write_optimization_summaries_1 (passes->all_lto_gen_passes, state);
2569
2570 gcc_assert (lto_get_out_decl_state () == state);
2571 lto_pop_out_decl_state ();
2572 lto_delete_out_decl_state (state);
2573 }
2574
2575 /* Same as execute_pass_list but assume that subpasses of IPA passes
2576 are local passes. */
2577
2578 static void
2579 ipa_read_summaries_1 (struct opt_pass *pass)
2580 {
2581 while (pass)
2582 {
2583 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2584
2585 gcc_assert (!current_function_decl);
2586 gcc_assert (!cfun);
2587 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2588
2589 if ((!pass->has_gate) || pass->gate ())
2590 {
2591 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2592 {
2593 /* If a timevar is present, start it. */
2594 if (pass->tv_id)
2595 timevar_push (pass->tv_id);
2596
2597 pass_init_dump_file (pass);
2598
2599 ipa_pass->read_summary ();
2600
2601 pass_fini_dump_file (pass);
2602
2603 /* Stop timevar. */
2604 if (pass->tv_id)
2605 timevar_pop (pass->tv_id);
2606 }
2607
2608 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2609 ipa_read_summaries_1 (pass->sub);
2610 }
2611 pass = pass->next;
2612 }
2613 }
2614
2615
2616 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2617
2618 void
2619 ipa_read_summaries (void)
2620 {
2621 pass_manager *passes = g->get_passes ();
2622 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2623 ipa_read_summaries_1 (passes->all_lto_gen_passes);
2624 }
2625
2626 /* Same as execute_pass_list but assume that subpasses of IPA passes
2627 are local passes. */
2628
2629 static void
2630 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2631 {
2632 while (pass)
2633 {
2634 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2635
2636 gcc_assert (!current_function_decl);
2637 gcc_assert (!cfun);
2638 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2639
2640 if ((!pass->has_gate) || pass->gate ())
2641 {
2642 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2643 {
2644 /* If a timevar is present, start it. */
2645 if (pass->tv_id)
2646 timevar_push (pass->tv_id);
2647
2648 pass_init_dump_file (pass);
2649
2650 ipa_pass->read_optimization_summary ();
2651
2652 pass_fini_dump_file (pass);
2653
2654 /* Stop timevar. */
2655 if (pass->tv_id)
2656 timevar_pop (pass->tv_id);
2657 }
2658
2659 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2660 ipa_read_optimization_summaries_1 (pass->sub);
2661 }
2662 pass = pass->next;
2663 }
2664 }
2665
2666 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
2667
2668 void
2669 ipa_read_optimization_summaries (void)
2670 {
2671 pass_manager *passes = g->get_passes ();
2672 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2673 ipa_read_optimization_summaries_1 (passes->all_lto_gen_passes);
2674 }
2675
2676 /* Same as execute_pass_list but assume that subpasses of IPA passes
2677 are local passes. */
2678 void
2679 execute_ipa_pass_list (struct opt_pass *pass)
2680 {
2681 do
2682 {
2683 gcc_assert (!current_function_decl);
2684 gcc_assert (!cfun);
2685 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2686 if (execute_one_pass (pass) && pass->sub)
2687 {
2688 if (pass->sub->type == GIMPLE_PASS)
2689 {
2690 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2691 do_per_function_toporder ((void (*)(void *))execute_pass_list,
2692 pass->sub);
2693 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2694 }
2695 else if (pass->sub->type == SIMPLE_IPA_PASS
2696 || pass->sub->type == IPA_PASS)
2697 execute_ipa_pass_list (pass->sub);
2698 else
2699 gcc_unreachable ();
2700 }
2701 gcc_assert (!current_function_decl);
2702 cgraph_process_new_functions ();
2703 pass = pass->next;
2704 }
2705 while (pass);
2706 }
2707
2708 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2709
2710 static void
2711 execute_ipa_stmt_fixups (struct opt_pass *pass,
2712 struct cgraph_node *node, gimple *stmts)
2713 {
2714 while (pass)
2715 {
2716 /* Execute all of the IPA_PASSes in the list. */
2717 if (pass->type == IPA_PASS
2718 && ((!pass->has_gate) || pass->gate ()))
2719 {
2720 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2721
2722 if (ipa_pass->stmt_fixup)
2723 {
2724 pass_init_dump_file (pass);
2725 /* If a timevar is present, start it. */
2726 if (pass->tv_id)
2727 timevar_push (pass->tv_id);
2728
2729 ipa_pass->stmt_fixup (node, stmts);
2730
2731 /* Stop timevar. */
2732 if (pass->tv_id)
2733 timevar_pop (pass->tv_id);
2734 pass_fini_dump_file (pass);
2735 }
2736 if (pass->sub)
2737 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2738 }
2739 pass = pass->next;
2740 }
2741 }
2742
2743 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2744
2745 void
2746 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2747 {
2748 pass_manager *passes = g->get_passes ();
2749 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2750 }
2751
2752
2753 extern void debug_properties (unsigned int);
2754 extern void dump_properties (FILE *, unsigned int);
2755
2756 DEBUG_FUNCTION void
2757 dump_properties (FILE *dump, unsigned int props)
2758 {
2759 fprintf (dump, "Properties:\n");
2760 if (props & PROP_gimple_any)
2761 fprintf (dump, "PROP_gimple_any\n");
2762 if (props & PROP_gimple_lcf)
2763 fprintf (dump, "PROP_gimple_lcf\n");
2764 if (props & PROP_gimple_leh)
2765 fprintf (dump, "PROP_gimple_leh\n");
2766 if (props & PROP_cfg)
2767 fprintf (dump, "PROP_cfg\n");
2768 if (props & PROP_ssa)
2769 fprintf (dump, "PROP_ssa\n");
2770 if (props & PROP_no_crit_edges)
2771 fprintf (dump, "PROP_no_crit_edges\n");
2772 if (props & PROP_rtl)
2773 fprintf (dump, "PROP_rtl\n");
2774 if (props & PROP_gimple_lomp)
2775 fprintf (dump, "PROP_gimple_lomp\n");
2776 if (props & PROP_gimple_lcx)
2777 fprintf (dump, "PROP_gimple_lcx\n");
2778 if (props & PROP_gimple_lvec)
2779 fprintf (dump, "PROP_gimple_lvec\n");
2780 if (props & PROP_cfglayout)
2781 fprintf (dump, "PROP_cfglayout\n");
2782 }
2783
2784 DEBUG_FUNCTION void
2785 debug_properties (unsigned int props)
2786 {
2787 dump_properties (stderr, props);
2788 }
2789
2790 /* Called by local passes to see if function is called by already processed nodes.
2791 Because we process nodes in topological order, this means that function is
2792 in recursive cycle or we introduced new direct calls. */
2793 bool
2794 function_called_by_processed_nodes_p (void)
2795 {
2796 struct cgraph_edge *e;
2797 for (e = cgraph_get_node (current_function_decl)->callers;
2798 e;
2799 e = e->next_caller)
2800 {
2801 if (e->caller->symbol.decl == current_function_decl)
2802 continue;
2803 if (!cgraph_function_with_gimple_body_p (e->caller))
2804 continue;
2805 if (TREE_ASM_WRITTEN (e->caller->symbol.decl))
2806 continue;
2807 if (!e->caller->process && !e->caller->global.inlined_to)
2808 break;
2809 }
2810 if (dump_file && e)
2811 {
2812 fprintf (dump_file, "Already processed call to:\n");
2813 dump_cgraph_node (dump_file, e->caller);
2814 }
2815 return e != NULL;
2816 }
2817
2818 #include "gt-passes.h"