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