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