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