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