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