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