PR jit/63854: Fix memory leaks within context/pass_manager/dump_manager
[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 true);
781 set_pass_for_id (id, pass);
782 full_name = concat (prefix, pass->name, num, NULL);
783 register_pass_name (pass, full_name);
784 free (CONST_CAST (char *, full_name));
785 }
786
787 /* Register the dump files for the pass_manager starting at PASS. */
788
789 void
790 pass_manager::register_dump_files (opt_pass *pass)
791 {
792 do
793 {
794 if (pass->name && pass->name[0] != '*')
795 register_one_dump_file (pass);
796
797 if (pass->sub)
798 register_dump_files (pass->sub);
799
800 pass = pass->next;
801 }
802 while (pass);
803 }
804
805 /* Helper for pass_registry hash table. */
806
807 struct pass_registry_hasher : default_hashmap_traits
808 {
809 static inline hashval_t hash (const char *);
810 static inline bool equal_keys (const char *, const char *);
811 };
812
813 /* Pass registry hash function. */
814
815 inline hashval_t
816 pass_registry_hasher::hash (const char *name)
817 {
818 return htab_hash_string (name);
819 }
820
821 /* Hash equal function */
822
823 inline bool
824 pass_registry_hasher::equal_keys (const char *s1, const char *s2)
825 {
826 return !strcmp (s1, s2);
827 }
828
829 static hash_map<const char *, opt_pass *, pass_registry_hasher>
830 *name_to_pass_map;
831
832 /* Register PASS with NAME. */
833
834 static void
835 register_pass_name (opt_pass *pass, const char *name)
836 {
837 if (!name_to_pass_map)
838 name_to_pass_map
839 = new hash_map<const char *, opt_pass *, pass_registry_hasher> (256);
840
841 if (name_to_pass_map->get (name))
842 return; /* Ignore plugin passes. */
843
844 const char *unique_name = xstrdup (name);
845 name_to_pass_map->put (unique_name, pass);
846 }
847
848 /* Map from pass id to canonicalized pass name. */
849
850 typedef const char *char_ptr;
851 static vec<char_ptr> pass_tab = vNULL;
852
853 /* Callback function for traversing NAME_TO_PASS_MAP. */
854
855 bool
856 passes_pass_traverse (const char *const &name, opt_pass *const &pass, void *)
857 {
858 gcc_assert (pass->static_pass_number > 0);
859 gcc_assert (pass_tab.exists ());
860
861 pass_tab[pass->static_pass_number] = name;
862
863 return 1;
864 }
865
866 /* The function traverses NAME_TO_PASS_MAP and creates a pass info
867 table for dumping purpose. */
868
869 static void
870 create_pass_tab (void)
871 {
872 if (!flag_dump_passes)
873 return;
874
875 pass_tab.safe_grow_cleared (g->get_passes ()->passes_by_id_size + 1);
876 name_to_pass_map->traverse <void *, passes_pass_traverse> (NULL);
877 }
878
879 static bool override_gate_status (opt_pass *, tree, bool);
880
881 /* Dump the instantiated name for PASS. IS_ON indicates if PASS
882 is turned on or not. */
883
884 static void
885 dump_one_pass (opt_pass *pass, int pass_indent)
886 {
887 int indent = 3 * pass_indent;
888 const char *pn;
889 bool is_on, is_really_on;
890
891 is_on = pass->gate (cfun);
892 is_really_on = override_gate_status (pass, current_function_decl, is_on);
893
894 if (pass->static_pass_number <= 0)
895 pn = pass->name;
896 else
897 pn = pass_tab[pass->static_pass_number];
898
899 fprintf (stderr, "%*s%-40s%*s:%s%s\n", indent, " ", pn,
900 (15 - indent < 0 ? 0 : 15 - indent), " ",
901 is_on ? " ON" : " OFF",
902 ((!is_on) == (!is_really_on) ? ""
903 : (is_really_on ? " (FORCED_ON)" : " (FORCED_OFF)")));
904 }
905
906 /* Dump pass list PASS with indentation INDENT. */
907
908 static void
909 dump_pass_list (opt_pass *pass, int indent)
910 {
911 do
912 {
913 dump_one_pass (pass, indent);
914 if (pass->sub)
915 dump_pass_list (pass->sub, indent + 1);
916 pass = pass->next;
917 }
918 while (pass);
919 }
920
921 /* Dump all optimization passes. */
922
923 void
924 dump_passes (void)
925 {
926 g->get_passes ()->dump_passes ();
927 }
928
929 void
930 pass_manager::dump_passes () const
931 {
932 struct cgraph_node *n, *node = NULL;
933
934 create_pass_tab ();
935
936 FOR_EACH_FUNCTION (n)
937 if (DECL_STRUCT_FUNCTION (n->decl))
938 {
939 node = n;
940 break;
941 }
942
943 if (!node)
944 return;
945
946 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
947
948 dump_pass_list (all_lowering_passes, 1);
949 dump_pass_list (all_small_ipa_passes, 1);
950 dump_pass_list (all_regular_ipa_passes, 1);
951 dump_pass_list (all_late_ipa_passes, 1);
952 dump_pass_list (all_passes, 1);
953
954 pop_cfun ();
955 }
956
957
958 /* Returns the pass with NAME. */
959
960 static opt_pass *
961 get_pass_by_name (const char *name)
962 {
963 opt_pass **p = name_to_pass_map->get (name);
964 if (p)
965 return *p;
966
967 return NULL;
968 }
969
970
971 /* Range [start, last]. */
972
973 struct uid_range
974 {
975 unsigned int start;
976 unsigned int last;
977 const char *assem_name;
978 struct uid_range *next;
979 };
980
981 typedef struct uid_range *uid_range_p;
982
983
984 static vec<uid_range_p>
985 enabled_pass_uid_range_tab = vNULL;
986 static vec<uid_range_p>
987 disabled_pass_uid_range_tab = vNULL;
988
989
990 /* Parse option string for -fdisable- and -fenable-
991 The syntax of the options:
992
993 -fenable-<pass_name>
994 -fdisable-<pass_name>
995
996 -fenable-<pass_name>=s1:e1,s2:e2,...
997 -fdisable-<pass_name>=s1:e1,s2:e2,...
998 */
999
1000 static void
1001 enable_disable_pass (const char *arg, bool is_enable)
1002 {
1003 opt_pass *pass;
1004 char *range_str, *phase_name;
1005 char *argstr = xstrdup (arg);
1006 vec<uid_range_p> *tab = 0;
1007
1008 range_str = strchr (argstr,'=');
1009 if (range_str)
1010 {
1011 *range_str = '\0';
1012 range_str++;
1013 }
1014
1015 phase_name = argstr;
1016 if (!*phase_name)
1017 {
1018 if (is_enable)
1019 error ("unrecognized option -fenable");
1020 else
1021 error ("unrecognized option -fdisable");
1022 free (argstr);
1023 return;
1024 }
1025 pass = get_pass_by_name (phase_name);
1026 if (!pass || pass->static_pass_number == -1)
1027 {
1028 if (is_enable)
1029 error ("unknown pass %s specified in -fenable", phase_name);
1030 else
1031 error ("unknown pass %s specified in -fdisable", phase_name);
1032 free (argstr);
1033 return;
1034 }
1035
1036 if (is_enable)
1037 tab = &enabled_pass_uid_range_tab;
1038 else
1039 tab = &disabled_pass_uid_range_tab;
1040
1041 if ((unsigned) pass->static_pass_number >= tab->length ())
1042 tab->safe_grow_cleared (pass->static_pass_number + 1);
1043
1044 if (!range_str)
1045 {
1046 uid_range_p slot;
1047 uid_range_p new_range = XCNEW (struct uid_range);
1048
1049 new_range->start = 0;
1050 new_range->last = (unsigned)-1;
1051
1052 slot = (*tab)[pass->static_pass_number];
1053 new_range->next = slot;
1054 (*tab)[pass->static_pass_number] = new_range;
1055 if (is_enable)
1056 inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
1057 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1058 else
1059 inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
1060 "of [%u, %u]", phase_name, new_range->start, new_range->last);
1061 }
1062 else
1063 {
1064 char *next_range = NULL;
1065 char *one_range = range_str;
1066 char *end_val = NULL;
1067
1068 do
1069 {
1070 uid_range_p slot;
1071 uid_range_p new_range;
1072 char *invalid = NULL;
1073 long start;
1074 char *func_name = NULL;
1075
1076 next_range = strchr (one_range, ',');
1077 if (next_range)
1078 {
1079 *next_range = '\0';
1080 next_range++;
1081 }
1082
1083 end_val = strchr (one_range, ':');
1084 if (end_val)
1085 {
1086 *end_val = '\0';
1087 end_val++;
1088 }
1089 start = strtol (one_range, &invalid, 10);
1090 if (*invalid || start < 0)
1091 {
1092 if (end_val || (one_range[0] >= '0'
1093 && one_range[0] <= '9'))
1094 {
1095 error ("Invalid range %s in option %s",
1096 one_range,
1097 is_enable ? "-fenable" : "-fdisable");
1098 free (argstr);
1099 return;
1100 }
1101 func_name = one_range;
1102 }
1103 if (!end_val)
1104 {
1105 new_range = XCNEW (struct uid_range);
1106 if (!func_name)
1107 {
1108 new_range->start = (unsigned) start;
1109 new_range->last = (unsigned) start;
1110 }
1111 else
1112 {
1113 new_range->start = (unsigned) -1;
1114 new_range->last = (unsigned) -1;
1115 new_range->assem_name = xstrdup (func_name);
1116 }
1117 }
1118 else
1119 {
1120 long last = strtol (end_val, &invalid, 10);
1121 if (*invalid || last < start)
1122 {
1123 error ("Invalid range %s in option %s",
1124 end_val,
1125 is_enable ? "-fenable" : "-fdisable");
1126 free (argstr);
1127 return;
1128 }
1129 new_range = XCNEW (struct uid_range);
1130 new_range->start = (unsigned) start;
1131 new_range->last = (unsigned) last;
1132 }
1133
1134 slot = (*tab)[pass->static_pass_number];
1135 new_range->next = slot;
1136 (*tab)[pass->static_pass_number] = new_range;
1137 if (is_enable)
1138 {
1139 if (new_range->assem_name)
1140 inform (UNKNOWN_LOCATION,
1141 "enable pass %s for function %s",
1142 phase_name, new_range->assem_name);
1143 else
1144 inform (UNKNOWN_LOCATION,
1145 "enable pass %s for functions in the range of [%u, %u]",
1146 phase_name, new_range->start, new_range->last);
1147 }
1148 else
1149 {
1150 if (new_range->assem_name)
1151 inform (UNKNOWN_LOCATION,
1152 "disable pass %s for function %s",
1153 phase_name, new_range->assem_name);
1154 else
1155 inform (UNKNOWN_LOCATION,
1156 "disable pass %s for functions in the range of [%u, %u]",
1157 phase_name, new_range->start, new_range->last);
1158 }
1159
1160 one_range = next_range;
1161 } while (next_range);
1162 }
1163
1164 free (argstr);
1165 }
1166
1167 /* Enable pass specified by ARG. */
1168
1169 void
1170 enable_pass (const char *arg)
1171 {
1172 enable_disable_pass (arg, true);
1173 }
1174
1175 /* Disable pass specified by ARG. */
1176
1177 void
1178 disable_pass (const char *arg)
1179 {
1180 enable_disable_pass (arg, false);
1181 }
1182
1183 /* Returns true if PASS is explicitly enabled/disabled for FUNC. */
1184
1185 static bool
1186 is_pass_explicitly_enabled_or_disabled (opt_pass *pass,
1187 tree func,
1188 vec<uid_range_p> tab)
1189 {
1190 uid_range_p slot, range;
1191 int cgraph_uid;
1192 const char *aname = NULL;
1193
1194 if (!tab.exists ()
1195 || (unsigned) pass->static_pass_number >= tab.length ()
1196 || pass->static_pass_number == -1)
1197 return false;
1198
1199 slot = tab[pass->static_pass_number];
1200 if (!slot)
1201 return false;
1202
1203 cgraph_uid = func ? cgraph_node::get (func)->uid : 0;
1204 if (func && DECL_ASSEMBLER_NAME_SET_P (func))
1205 aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
1206
1207 range = slot;
1208 while (range)
1209 {
1210 if ((unsigned) cgraph_uid >= range->start
1211 && (unsigned) cgraph_uid <= range->last)
1212 return true;
1213 if (range->assem_name && aname
1214 && !strcmp (range->assem_name, aname))
1215 return true;
1216 range = range->next;
1217 }
1218
1219 return false;
1220 }
1221
1222
1223 /* Update static_pass_number for passes (and the flag
1224 TODO_mark_first_instance).
1225
1226 Passes are constructed with static_pass_number preinitialized to 0
1227
1228 This field is used in two different ways: initially as instance numbers
1229 of their kind, and then as ids within the entire pass manager.
1230
1231 Within pass_manager::pass_manager:
1232
1233 * In add_pass_instance(), as called by next_pass_1 in
1234 NEXT_PASS in init_optimization_passes
1235
1236 * When the initial instance of a pass within a pass manager is seen,
1237 it is flagged, and its static_pass_number is set to -1
1238
1239 * On subsequent times that it is seen, the static pass number
1240 is decremented each time, so that if there are e.g. 4 dups,
1241 they have static_pass_number -4, 2, 3, 4 respectively (note
1242 how the initial one is negative and gives the count); these
1243 can be thought of as instance numbers of the specific pass
1244
1245 * Within the register_dump_files () traversal, set_pass_for_id()
1246 is called on each pass, using these instance numbers to create
1247 dumpfile switches, and then overwriting them with a pass id,
1248 which are global to the whole pass manager (based on
1249 (TDI_end + current value of extra_dump_files_in_use) ) */
1250
1251 static void
1252 add_pass_instance (opt_pass *new_pass, bool track_duplicates,
1253 opt_pass *initial_pass)
1254 {
1255 /* Are we dealing with the first pass of its kind, or a clone? */
1256 if (new_pass != initial_pass)
1257 {
1258 /* We're dealing with a clone. */
1259 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
1260
1261 /* Indicate to register_dump_files that this pass has duplicates,
1262 and so it should rename the dump file. The first instance will
1263 be -1, and be number of duplicates = -static_pass_number - 1.
1264 Subsequent instances will be > 0 and just the duplicate number. */
1265 if ((new_pass->name && new_pass->name[0] != '*') || track_duplicates)
1266 {
1267 initial_pass->static_pass_number -= 1;
1268 new_pass->static_pass_number = -initial_pass->static_pass_number;
1269 }
1270 }
1271 else
1272 {
1273 /* We're dealing with the first pass of its kind. */
1274 new_pass->todo_flags_start |= TODO_mark_first_instance;
1275 new_pass->static_pass_number = -1;
1276
1277 invoke_plugin_callbacks (PLUGIN_NEW_PASS, new_pass);
1278 }
1279 }
1280
1281 /* Add a pass to the pass list. Duplicate the pass if it's already
1282 in the list. */
1283
1284 static opt_pass **
1285 next_pass_1 (opt_pass **list, opt_pass *pass, opt_pass *initial_pass)
1286 {
1287 /* Every pass should have a name so that plugins can refer to them. */
1288 gcc_assert (pass->name != NULL);
1289
1290 add_pass_instance (pass, false, initial_pass);
1291 *list = pass;
1292
1293 return &(*list)->next;
1294 }
1295
1296 /* List node for an inserted pass instance. We need to keep track of all
1297 the newly-added pass instances (with 'added_pass_nodes' defined below)
1298 so that we can register their dump files after pass-positioning is finished.
1299 Registering dumping files needs to be post-processed or the
1300 static_pass_number of the opt_pass object would be modified and mess up
1301 the dump file names of future pass instances to be added. */
1302
1303 struct pass_list_node
1304 {
1305 opt_pass *pass;
1306 struct pass_list_node *next;
1307 };
1308
1309 static struct pass_list_node *added_pass_nodes = NULL;
1310 static struct pass_list_node *prev_added_pass_node;
1311
1312 /* Insert the pass at the proper position. Return true if the pass
1313 is successfully added.
1314
1315 NEW_PASS_INFO - new pass to be inserted
1316 PASS_LIST - root of the pass list to insert the new pass to */
1317
1318 static bool
1319 position_pass (struct register_pass_info *new_pass_info, opt_pass **pass_list)
1320 {
1321 opt_pass *pass = *pass_list, *prev_pass = NULL;
1322 bool success = false;
1323
1324 for ( ; pass; prev_pass = pass, pass = pass->next)
1325 {
1326 /* Check if the current pass is of the same type as the new pass and
1327 matches the name and the instance number of the reference pass. */
1328 if (pass->type == new_pass_info->pass->type
1329 && pass->name
1330 && !strcmp (pass->name, new_pass_info->reference_pass_name)
1331 && ((new_pass_info->ref_pass_instance_number == 0)
1332 || (new_pass_info->ref_pass_instance_number ==
1333 pass->static_pass_number)
1334 || (new_pass_info->ref_pass_instance_number == 1
1335 && pass->todo_flags_start & TODO_mark_first_instance)))
1336 {
1337 opt_pass *new_pass;
1338 struct pass_list_node *new_pass_node;
1339
1340 if (new_pass_info->ref_pass_instance_number == 0)
1341 {
1342 new_pass = new_pass_info->pass->clone ();
1343 add_pass_instance (new_pass, true, new_pass_info->pass);
1344 }
1345 else
1346 {
1347 new_pass = new_pass_info->pass;
1348 add_pass_instance (new_pass, true, new_pass);
1349 }
1350
1351 /* Insert the new pass instance based on the positioning op. */
1352 switch (new_pass_info->pos_op)
1353 {
1354 case PASS_POS_INSERT_AFTER:
1355 new_pass->next = pass->next;
1356 pass->next = new_pass;
1357
1358 /* Skip newly inserted pass to avoid repeated
1359 insertions in the case where the new pass and the
1360 existing one have the same name. */
1361 pass = new_pass;
1362 break;
1363 case PASS_POS_INSERT_BEFORE:
1364 new_pass->next = pass;
1365 if (prev_pass)
1366 prev_pass->next = new_pass;
1367 else
1368 *pass_list = new_pass;
1369 break;
1370 case PASS_POS_REPLACE:
1371 new_pass->next = pass->next;
1372 if (prev_pass)
1373 prev_pass->next = new_pass;
1374 else
1375 *pass_list = new_pass;
1376 new_pass->sub = pass->sub;
1377 new_pass->tv_id = pass->tv_id;
1378 pass = new_pass;
1379 break;
1380 default:
1381 error ("invalid pass positioning operation");
1382 return false;
1383 }
1384
1385 /* Save the newly added pass (instance) in the added_pass_nodes
1386 list so that we can register its dump file later. Note that
1387 we cannot register the dump file now because doing so will modify
1388 the static_pass_number of the opt_pass object and therefore
1389 mess up the dump file name of future instances. */
1390 new_pass_node = XCNEW (struct pass_list_node);
1391 new_pass_node->pass = new_pass;
1392 if (!added_pass_nodes)
1393 added_pass_nodes = new_pass_node;
1394 else
1395 prev_added_pass_node->next = new_pass_node;
1396 prev_added_pass_node = new_pass_node;
1397
1398 success = true;
1399 }
1400
1401 if (pass->sub && position_pass (new_pass_info, &pass->sub))
1402 success = true;
1403 }
1404
1405 return success;
1406 }
1407
1408 /* Hooks a new pass into the pass lists.
1409
1410 PASS_INFO - pass information that specifies the opt_pass object,
1411 reference pass, instance number, and how to position
1412 the pass */
1413
1414 void
1415 register_pass (struct register_pass_info *pass_info)
1416 {
1417 g->get_passes ()->register_pass (pass_info);
1418 }
1419
1420 void
1421 register_pass (opt_pass* pass, pass_positioning_ops pos,
1422 const char* ref_pass_name, int ref_pass_inst_number)
1423 {
1424 register_pass_info i;
1425 i.pass = pass;
1426 i.reference_pass_name = ref_pass_name;
1427 i.ref_pass_instance_number = ref_pass_inst_number;
1428 i.pos_op = pos;
1429
1430 g->get_passes ()->register_pass (&i);
1431 }
1432
1433 void
1434 pass_manager::register_pass (struct register_pass_info *pass_info)
1435 {
1436 bool all_instances, success;
1437 gcc::dump_manager *dumps = m_ctxt->get_dumps ();
1438
1439 /* The checks below could fail in buggy plugins. Existing GCC
1440 passes should never fail these checks, so we mention plugin in
1441 the messages. */
1442 if (!pass_info->pass)
1443 fatal_error ("plugin cannot register a missing pass");
1444
1445 if (!pass_info->pass->name)
1446 fatal_error ("plugin cannot register an unnamed pass");
1447
1448 if (!pass_info->reference_pass_name)
1449 fatal_error
1450 ("plugin cannot register pass %qs without reference pass name",
1451 pass_info->pass->name);
1452
1453 /* Try to insert the new pass to the pass lists. We need to check
1454 all five lists as the reference pass could be in one (or all) of
1455 them. */
1456 all_instances = pass_info->ref_pass_instance_number == 0;
1457 success = position_pass (pass_info, &all_lowering_passes);
1458 if (!success || all_instances)
1459 success |= position_pass (pass_info, &all_small_ipa_passes);
1460 if (!success || all_instances)
1461 success |= position_pass (pass_info, &all_regular_ipa_passes);
1462 if (!success || all_instances)
1463 success |= position_pass (pass_info, &all_late_ipa_passes);
1464 if (!success || all_instances)
1465 success |= position_pass (pass_info, &all_passes);
1466 if (!success)
1467 fatal_error
1468 ("pass %qs not found but is referenced by new pass %qs",
1469 pass_info->reference_pass_name, pass_info->pass->name);
1470
1471 /* OK, we have successfully inserted the new pass. We need to register
1472 the dump files for the newly added pass and its duplicates (if any).
1473 Because the registration of plugin/backend passes happens after the
1474 command-line options are parsed, the options that specify single
1475 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1476 passes. Therefore we currently can only enable dumping of
1477 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1478 are specified. While doing so, we also delete the pass_list_node
1479 objects created during pass positioning. */
1480 while (added_pass_nodes)
1481 {
1482 struct pass_list_node *next_node = added_pass_nodes->next;
1483 enum tree_dump_index tdi;
1484 register_one_dump_file (added_pass_nodes->pass);
1485 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1486 || added_pass_nodes->pass->type == IPA_PASS)
1487 tdi = TDI_ipa_all;
1488 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1489 tdi = TDI_tree_all;
1490 else
1491 tdi = TDI_rtl_all;
1492 /* Check if dump-all flag is specified. */
1493 if (dumps->get_dump_file_info (tdi)->pstate)
1494 dumps->get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1495 ->pstate = dumps->get_dump_file_info (tdi)->pstate;
1496 XDELETE (added_pass_nodes);
1497 added_pass_nodes = next_node;
1498 }
1499 }
1500
1501 /* Construct the pass tree. The sequencing of passes is driven by
1502 the cgraph routines:
1503
1504 finalize_compilation_unit ()
1505 for each node N in the cgraph
1506 cgraph_analyze_function (N)
1507 cgraph_lower_function (N) -> all_lowering_passes
1508
1509 If we are optimizing, compile is then invoked:
1510
1511 compile ()
1512 ipa_passes () -> all_small_ipa_passes
1513 -> Analysis of all_regular_ipa_passes
1514 * possible LTO streaming at copmilation time *
1515 -> Execution of all_regular_ipa_passes
1516 * possible LTO streaming at link time *
1517 -> all_late_ipa_passes
1518 expand_all_functions ()
1519 for each node N in the cgraph
1520 expand_function (N) -> Transformation of all_regular_ipa_passes
1521 -> all_passes
1522 */
1523
1524 void *
1525 pass_manager::operator new (size_t sz)
1526 {
1527 /* Ensure that all fields of the pass manager are zero-initialized. */
1528 return xcalloc (1, sz);
1529 }
1530
1531 void
1532 pass_manager::operator delete (void *ptr)
1533 {
1534 free (ptr);
1535 }
1536
1537 pass_manager::pass_manager (context *ctxt)
1538 : all_passes (NULL), all_small_ipa_passes (NULL), all_lowering_passes (NULL),
1539 all_regular_ipa_passes (NULL),
1540 all_late_ipa_passes (NULL), passes_by_id (NULL), passes_by_id_size (0),
1541 m_ctxt (ctxt)
1542 {
1543 opt_pass **p;
1544
1545 /* Initialize the pass_lists array. */
1546 #define DEF_PASS_LIST(LIST) pass_lists[PASS_LIST_NO_##LIST] = &LIST;
1547 GCC_PASS_LISTS
1548 #undef DEF_PASS_LIST
1549
1550 /* Build the tree of passes. */
1551
1552 #define INSERT_PASSES_AFTER(PASS) \
1553 p = &(PASS);
1554
1555 #define PUSH_INSERT_PASSES_WITHIN(PASS) \
1556 { \
1557 opt_pass **p = &(PASS ## _1)->sub;
1558
1559 #define POP_INSERT_PASSES() \
1560 }
1561
1562 #define NEXT_PASS(PASS, NUM) \
1563 do { \
1564 gcc_assert (NULL == PASS ## _ ## NUM); \
1565 if ((NUM) == 1) \
1566 PASS ## _1 = make_##PASS (m_ctxt); \
1567 else \
1568 { \
1569 gcc_assert (PASS ## _1); \
1570 PASS ## _ ## NUM = PASS ## _1->clone (); \
1571 } \
1572 p = next_pass_1 (p, PASS ## _ ## NUM, PASS ## _1); \
1573 } while (0)
1574
1575 #define TERMINATE_PASS_LIST() \
1576 *p = NULL;
1577
1578 #include "pass-instances.def"
1579
1580 #undef INSERT_PASSES_AFTER
1581 #undef PUSH_INSERT_PASSES_WITHIN
1582 #undef POP_INSERT_PASSES
1583 #undef NEXT_PASS
1584 #undef TERMINATE_PASS_LIST
1585
1586 /* Register the passes with the tree dump code. */
1587 register_dump_files (all_lowering_passes);
1588 register_dump_files (all_small_ipa_passes);
1589 register_dump_files (all_regular_ipa_passes);
1590 register_dump_files (all_late_ipa_passes);
1591 register_dump_files (all_passes);
1592 }
1593
1594 static void
1595 delete_pass_tree (opt_pass *pass)
1596 {
1597 while (pass)
1598 {
1599 /* Recurse into child passes. */
1600 delete_pass_tree (pass->sub);
1601
1602 opt_pass *next = pass->next;
1603
1604 /* Delete this pass. */
1605 delete pass;
1606
1607 /* Iterate onto sibling passes. */
1608 pass = next;
1609 }
1610 }
1611
1612 pass_manager::~pass_manager ()
1613 {
1614 XDELETEVEC (passes_by_id);
1615
1616 /* Call delete_pass_tree on each of the pass_lists. */
1617 #define DEF_PASS_LIST(LIST) \
1618 delete_pass_tree (*pass_lists[PASS_LIST_NO_##LIST]);
1619 GCC_PASS_LISTS
1620 #undef DEF_PASS_LIST
1621
1622 }
1623
1624 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1625 function CALLBACK for every function in the call graph. Otherwise,
1626 call CALLBACK on the current function. */
1627
1628 static void
1629 do_per_function (void (*callback) (function *, void *data), void *data)
1630 {
1631 if (current_function_decl)
1632 callback (cfun, data);
1633 else
1634 {
1635 struct cgraph_node *node;
1636 FOR_EACH_DEFINED_FUNCTION (node)
1637 if (node->analyzed && (gimple_has_body_p (node->decl) && !in_lto_p)
1638 && (!node->clone_of || node->decl != node->clone_of->decl))
1639 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1640 }
1641 }
1642
1643 /* Because inlining might remove no-longer reachable nodes, we need to
1644 keep the array visible to garbage collector to avoid reading collected
1645 out nodes. */
1646 static int nnodes;
1647 static GTY ((length ("nnodes"))) cgraph_node **order;
1648
1649 /* Hook called when NODE is removed and therefore should be
1650 excluded from order vector. DATA is an array of integers.
1651 DATA[0] holds max index it may be accessed by. For cgraph
1652 node DATA[node->uid + 1] holds index of this node in order
1653 vector. */
1654 static void
1655 remove_cgraph_node_from_order (cgraph_node *node, void *data)
1656 {
1657 int *order_idx = (int *)data;
1658
1659 if (node->uid >= order_idx[0])
1660 return;
1661
1662 int idx = order_idx[node->uid + 1];
1663 if (idx >= 0 && idx < nnodes && order[idx] == node)
1664 order[idx] = NULL;
1665 }
1666
1667 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1668 function CALLBACK for every function in the call graph. Otherwise,
1669 call CALLBACK on the current function.
1670 This function is global so that plugins can use it. */
1671 void
1672 do_per_function_toporder (void (*callback) (function *, void *data), void *data)
1673 {
1674 int i;
1675
1676 if (current_function_decl)
1677 callback (cfun, data);
1678 else
1679 {
1680 cgraph_node_hook_list *hook;
1681 int *order_idx;
1682 gcc_assert (!order);
1683 order = ggc_vec_alloc<cgraph_node *> (symtab->cgraph_count);
1684
1685 order_idx = XALLOCAVEC (int, symtab->cgraph_max_uid + 1);
1686 memset (order_idx + 1, -1, sizeof (int) * symtab->cgraph_max_uid);
1687 order_idx[0] = symtab->cgraph_max_uid;
1688
1689 nnodes = ipa_reverse_postorder (order);
1690 for (i = nnodes - 1; i >= 0; i--)
1691 {
1692 order[i]->process = 1;
1693 order_idx[order[i]->uid + 1] = i;
1694 }
1695 hook = symtab->add_cgraph_removal_hook (remove_cgraph_node_from_order,
1696 order_idx);
1697 for (i = nnodes - 1; i >= 0; i--)
1698 {
1699 /* Function could be inlined and removed as unreachable. */
1700 if (!order[i])
1701 continue;
1702
1703 struct cgraph_node *node = order[i];
1704
1705 /* Allow possibly removed nodes to be garbage collected. */
1706 order[i] = NULL;
1707 node->process = 0;
1708 if (node->has_gimple_body_p ())
1709 callback (DECL_STRUCT_FUNCTION (node->decl), data);
1710 }
1711 symtab->remove_cgraph_removal_hook (hook);
1712 }
1713 ggc_free (order);
1714 order = NULL;
1715 nnodes = 0;
1716 }
1717
1718 /* Helper function to perform function body dump. */
1719
1720 static void
1721 execute_function_dump (function *fn, void *data)
1722 {
1723 opt_pass *pass = (opt_pass *)data;
1724
1725 if (dump_file)
1726 {
1727 push_cfun (fn);
1728
1729 if (fn->curr_properties & PROP_trees)
1730 dump_function_to_file (fn->decl, dump_file, dump_flags);
1731 else
1732 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
1733
1734 /* Flush the file. If verification fails, we won't be able to
1735 close the file before aborting. */
1736 fflush (dump_file);
1737
1738 if ((fn->curr_properties & PROP_cfg)
1739 && (dump_flags & TDF_GRAPH))
1740 {
1741 if (!pass->graph_dump_initialized)
1742 {
1743 clean_graph_dump_file (dump_file_name);
1744 pass->graph_dump_initialized = true;
1745 }
1746 print_graph_cfg (dump_file_name, fn);
1747 }
1748
1749 pop_cfun ();
1750 }
1751 }
1752
1753 static struct profile_record *profile_record;
1754
1755 /* Do profile consistency book-keeping for the pass with static number INDEX.
1756 If SUBPASS is zero, we run _before_ the pass, and if SUBPASS is one, then
1757 we run _after_ the pass. RUN is true if the pass really runs, or FALSE
1758 if we are only book-keeping on passes that may have selectively disabled
1759 themselves on a given function. */
1760 static void
1761 check_profile_consistency (int index, int subpass, bool run)
1762 {
1763 pass_manager *passes = g->get_passes ();
1764 if (index == -1)
1765 return;
1766 if (!profile_record)
1767 profile_record = XCNEWVEC (struct profile_record,
1768 passes->passes_by_id_size);
1769 gcc_assert (index < passes->passes_by_id_size && index >= 0);
1770 gcc_assert (subpass < 2);
1771 profile_record[index].run |= run;
1772 account_profile_record (&profile_record[index], subpass);
1773 }
1774
1775 /* Output profile consistency. */
1776
1777 void
1778 dump_profile_report (void)
1779 {
1780 g->get_passes ()->dump_profile_report ();
1781 }
1782
1783 void
1784 pass_manager::dump_profile_report () const
1785 {
1786 int i, j;
1787 int last_freq_in = 0, last_count_in = 0, last_freq_out = 0, last_count_out = 0;
1788 gcov_type last_time = 0, last_size = 0;
1789 double rel_time_change, rel_size_change;
1790 int last_reported = 0;
1791
1792 if (!profile_record)
1793 return;
1794 fprintf (stderr, "\nProfile consistency report:\n\n");
1795 fprintf (stderr, "Pass name |mismatch in |mismated out|Overall\n");
1796 fprintf (stderr, " |freq count |freq count |size time\n");
1797
1798 for (i = 0; i < passes_by_id_size; i++)
1799 for (j = 0 ; j < 2; j++)
1800 if (profile_record[i].run)
1801 {
1802 if (last_time)
1803 rel_time_change = (profile_record[i].time[j]
1804 - (double)last_time) * 100 / (double)last_time;
1805 else
1806 rel_time_change = 0;
1807 if (last_size)
1808 rel_size_change = (profile_record[i].size[j]
1809 - (double)last_size) * 100 / (double)last_size;
1810 else
1811 rel_size_change = 0;
1812
1813 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in
1814 || profile_record[i].num_mismatched_freq_out[j] != last_freq_out
1815 || profile_record[i].num_mismatched_count_in[j] != last_count_in
1816 || profile_record[i].num_mismatched_count_out[j] != last_count_out
1817 || rel_time_change || rel_size_change)
1818 {
1819 last_reported = i;
1820 fprintf (stderr, "%-20s %s",
1821 passes_by_id [i]->name,
1822 j ? "(after TODO)" : " ");
1823 if (profile_record[i].num_mismatched_freq_in[j] != last_freq_in)
1824 fprintf (stderr, "| %+5i",
1825 profile_record[i].num_mismatched_freq_in[j]
1826 - last_freq_in);
1827 else
1828 fprintf (stderr, "| ");
1829 if (profile_record[i].num_mismatched_count_in[j] != last_count_in)
1830 fprintf (stderr, " %+5i",
1831 profile_record[i].num_mismatched_count_in[j]
1832 - last_count_in);
1833 else
1834 fprintf (stderr, " ");
1835 if (profile_record[i].num_mismatched_freq_out[j] != last_freq_out)
1836 fprintf (stderr, "| %+5i",
1837 profile_record[i].num_mismatched_freq_out[j]
1838 - last_freq_out);
1839 else
1840 fprintf (stderr, "| ");
1841 if (profile_record[i].num_mismatched_count_out[j] != last_count_out)
1842 fprintf (stderr, " %+5i",
1843 profile_record[i].num_mismatched_count_out[j]
1844 - last_count_out);
1845 else
1846 fprintf (stderr, " ");
1847
1848 /* Size/time units change across gimple and RTL. */
1849 if (i == pass_expand_1->static_pass_number)
1850 fprintf (stderr, "|----------");
1851 else
1852 {
1853 if (rel_size_change)
1854 fprintf (stderr, "| %+8.4f%%", rel_size_change);
1855 else
1856 fprintf (stderr, "| ");
1857 if (rel_time_change)
1858 fprintf (stderr, " %+8.4f%%", rel_time_change);
1859 }
1860 fprintf (stderr, "\n");
1861 last_freq_in = profile_record[i].num_mismatched_freq_in[j];
1862 last_freq_out = profile_record[i].num_mismatched_freq_out[j];
1863 last_count_in = profile_record[i].num_mismatched_count_in[j];
1864 last_count_out = profile_record[i].num_mismatched_count_out[j];
1865 }
1866 else if (j && last_reported != i)
1867 {
1868 last_reported = i;
1869 fprintf (stderr, "%-20s ------------| | |\n",
1870 passes_by_id [i]->name);
1871 }
1872 last_time = profile_record[i].time[j];
1873 last_size = profile_record[i].size[j];
1874 }
1875 }
1876
1877 /* Perform all TODO actions that ought to be done on each function. */
1878
1879 static void
1880 execute_function_todo (function *fn, void *data)
1881 {
1882 bool from_ipa_pass = (cfun == NULL);
1883 unsigned int flags = (size_t)data;
1884 flags &= ~fn->last_verified;
1885 if (!flags)
1886 return;
1887
1888 push_cfun (fn);
1889
1890 /* Always cleanup the CFG before trying to update SSA. */
1891 if (flags & TODO_cleanup_cfg)
1892 {
1893 cleanup_tree_cfg ();
1894
1895 /* When cleanup_tree_cfg merges consecutive blocks, it may
1896 perform some simplistic propagation when removing single
1897 valued PHI nodes. This propagation may, in turn, cause the
1898 SSA form to become out-of-date (see PR 22037). So, even
1899 if the parent pass had not scheduled an SSA update, we may
1900 still need to do one. */
1901 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1902 flags |= TODO_update_ssa;
1903 }
1904
1905 if (flags & TODO_update_ssa_any)
1906 {
1907 unsigned update_flags = flags & TODO_update_ssa_any;
1908 update_ssa (update_flags);
1909 }
1910
1911 if (flag_tree_pta && (flags & TODO_rebuild_alias))
1912 compute_may_aliases ();
1913
1914 if (optimize && (flags & TODO_update_address_taken))
1915 execute_update_addresses_taken ();
1916
1917 if (flags & TODO_remove_unused_locals)
1918 remove_unused_locals ();
1919
1920 if (flags & TODO_rebuild_frequencies)
1921 rebuild_frequencies ();
1922
1923 if (flags & TODO_rebuild_cgraph_edges)
1924 cgraph_edge::rebuild_edges ();
1925
1926 /* If we've seen errors do not bother running any verifiers. */
1927 if (!seen_error ())
1928 {
1929 #if defined ENABLE_CHECKING
1930 dom_state pre_verify_state = dom_info_state (fn, CDI_DOMINATORS);
1931 dom_state pre_verify_pstate = dom_info_state (fn, CDI_POST_DOMINATORS);
1932
1933 if (flags & TODO_verify_il)
1934 {
1935 if (cfun->curr_properties & PROP_trees)
1936 {
1937 if (cfun->curr_properties & PROP_cfg)
1938 /* IPA passes leave stmts to be fixed up, so make sure to
1939 not verify stmts really throw. */
1940 verify_gimple_in_cfg (cfun, !from_ipa_pass);
1941 else
1942 verify_gimple_in_seq (gimple_body (cfun->decl));
1943 }
1944 if (cfun->curr_properties & PROP_ssa)
1945 /* IPA passes leave stmts to be fixed up, so make sure to
1946 not verify SSA operands whose verifier will choke on that. */
1947 verify_ssa (true, !from_ipa_pass);
1948 /* IPA passes leave basic-blocks unsplit, so make sure to
1949 not trip on that. */
1950 if ((cfun->curr_properties & PROP_cfg)
1951 && !from_ipa_pass)
1952 verify_flow_info ();
1953 if (current_loops
1954 && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1955 verify_loop_closed_ssa (false);
1956 if (cfun->curr_properties & PROP_rtl)
1957 verify_rtl_sharing ();
1958 }
1959
1960 /* Make sure verifiers don't change dominator state. */
1961 gcc_assert (dom_info_state (fn, CDI_DOMINATORS) == pre_verify_state);
1962 gcc_assert (dom_info_state (fn, CDI_POST_DOMINATORS) == pre_verify_pstate);
1963 #endif
1964 }
1965
1966 fn->last_verified = flags & TODO_verify_all;
1967
1968 pop_cfun ();
1969
1970 /* For IPA passes make sure to release dominator info, it can be
1971 computed by non-verifying TODOs. */
1972 if (from_ipa_pass)
1973 {
1974 free_dominance_info (fn, CDI_DOMINATORS);
1975 free_dominance_info (fn, CDI_POST_DOMINATORS);
1976 }
1977 }
1978
1979 /* Perform all TODO actions. */
1980 static void
1981 execute_todo (unsigned int flags)
1982 {
1983 #if defined ENABLE_CHECKING
1984 if (cfun
1985 && need_ssa_update_p (cfun))
1986 gcc_assert (flags & TODO_update_ssa_any);
1987 #endif
1988
1989 timevar_push (TV_TODO);
1990
1991 /* Inform the pass whether it is the first time it is run. */
1992 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1993
1994 statistics_fini_pass ();
1995
1996 if (flags)
1997 do_per_function (execute_function_todo, (void *)(size_t) flags);
1998
1999 /* Always remove functions just as before inlining: IPA passes might be
2000 interested to see bodies of extern inline functions that are not inlined
2001 to analyze side effects. The full removal is done just at the end
2002 of IPA pass queue. */
2003 if (flags & TODO_remove_functions)
2004 {
2005 gcc_assert (!cfun);
2006 symtab->remove_unreachable_nodes (true, dump_file);
2007 }
2008
2009 if ((flags & TODO_dump_symtab) && dump_file && !current_function_decl)
2010 {
2011 gcc_assert (!cfun);
2012 symtab_node::dump_table (dump_file);
2013 /* Flush the file. If verification fails, we won't be able to
2014 close the file before aborting. */
2015 fflush (dump_file);
2016 }
2017
2018 /* Now that the dumping has been done, we can get rid of the optional
2019 df problems. */
2020 if (flags & TODO_df_finish)
2021 df_finish_pass ((flags & TODO_df_verify) != 0);
2022
2023 timevar_pop (TV_TODO);
2024 }
2025
2026 /* Verify invariants that should hold between passes. This is a place
2027 to put simple sanity checks. */
2028
2029 static void
2030 verify_interpass_invariants (void)
2031 {
2032 gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
2033 }
2034
2035 /* Clear the last verified flag. */
2036
2037 static void
2038 clear_last_verified (function *fn, void *data ATTRIBUTE_UNUSED)
2039 {
2040 fn->last_verified = 0;
2041 }
2042
2043 /* Helper function. Verify that the properties has been turn into the
2044 properties expected by the pass. */
2045
2046 #ifdef ENABLE_CHECKING
2047 static void
2048 verify_curr_properties (function *fn, void *data)
2049 {
2050 unsigned int props = (size_t)data;
2051 gcc_assert ((fn->curr_properties & props) == props);
2052 }
2053 #endif
2054
2055 /* Initialize pass dump file. */
2056 /* This is non-static so that the plugins can use it. */
2057
2058 bool
2059 pass_init_dump_file (opt_pass *pass)
2060 {
2061 /* If a dump file name is present, open it if enabled. */
2062 if (pass->static_pass_number != -1)
2063 {
2064 timevar_push (TV_DUMP);
2065 gcc::dump_manager *dumps = g->get_dumps ();
2066 bool initializing_dump =
2067 !dumps->dump_initialized_p (pass->static_pass_number);
2068 dump_file_name = dumps->get_dump_file_name (pass->static_pass_number);
2069 dumps->dump_start (pass->static_pass_number, &dump_flags);
2070 if (dump_file && current_function_decl)
2071 dump_function_header (dump_file, current_function_decl, dump_flags);
2072 if (initializing_dump
2073 && dump_file && (dump_flags & TDF_GRAPH)
2074 && cfun && (cfun->curr_properties & PROP_cfg))
2075 {
2076 clean_graph_dump_file (dump_file_name);
2077 pass->graph_dump_initialized = true;
2078 }
2079 timevar_pop (TV_DUMP);
2080 return initializing_dump;
2081 }
2082 else
2083 return false;
2084 }
2085
2086 /* Flush PASS dump file. */
2087 /* This is non-static so that plugins can use it. */
2088
2089 void
2090 pass_fini_dump_file (opt_pass *pass)
2091 {
2092 timevar_push (TV_DUMP);
2093
2094 /* Flush and close dump file. */
2095 if (dump_file_name)
2096 {
2097 free (CONST_CAST (char *, dump_file_name));
2098 dump_file_name = NULL;
2099 }
2100
2101 g->get_dumps ()->dump_finish (pass->static_pass_number);
2102 timevar_pop (TV_DUMP);
2103 }
2104
2105 /* After executing the pass, apply expected changes to the function
2106 properties. */
2107
2108 static void
2109 update_properties_after_pass (function *fn, void *data)
2110 {
2111 opt_pass *pass = (opt_pass *) data;
2112 fn->curr_properties = (fn->curr_properties | pass->properties_provided)
2113 & ~pass->properties_destroyed;
2114 }
2115
2116 /* Execute summary generation for all of the passes in IPA_PASS. */
2117
2118 void
2119 execute_ipa_summary_passes (ipa_opt_pass_d *ipa_pass)
2120 {
2121 while (ipa_pass)
2122 {
2123 opt_pass *pass = ipa_pass;
2124
2125 /* Execute all of the IPA_PASSes in the list. */
2126 if (ipa_pass->type == IPA_PASS
2127 && pass->gate (cfun)
2128 && ipa_pass->generate_summary)
2129 {
2130 pass_init_dump_file (pass);
2131
2132 /* If a timevar is present, start it. */
2133 if (pass->tv_id)
2134 timevar_push (pass->tv_id);
2135
2136 current_pass = pass;
2137 ipa_pass->generate_summary ();
2138
2139 /* Stop timevar. */
2140 if (pass->tv_id)
2141 timevar_pop (pass->tv_id);
2142
2143 pass_fini_dump_file (pass);
2144 }
2145 ipa_pass = (ipa_opt_pass_d *)ipa_pass->next;
2146 }
2147 }
2148
2149 /* Execute IPA_PASS function transform on NODE. */
2150
2151 static void
2152 execute_one_ipa_transform_pass (struct cgraph_node *node,
2153 ipa_opt_pass_d *ipa_pass)
2154 {
2155 opt_pass *pass = ipa_pass;
2156 unsigned int todo_after = 0;
2157
2158 current_pass = pass;
2159 if (!ipa_pass->function_transform)
2160 return;
2161
2162 /* Note that the folders should only create gimple expressions.
2163 This is a hack until the new folder is ready. */
2164 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2165
2166 pass_init_dump_file (pass);
2167
2168 /* Run pre-pass verification. */
2169 execute_todo (ipa_pass->function_transform_todo_flags_start);
2170
2171 /* If a timevar is present, start it. */
2172 if (pass->tv_id != TV_NONE)
2173 timevar_push (pass->tv_id);
2174
2175 /* Do it! */
2176 todo_after = ipa_pass->function_transform (node);
2177
2178 /* Stop timevar. */
2179 if (pass->tv_id != TV_NONE)
2180 timevar_pop (pass->tv_id);
2181
2182 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2183 check_profile_consistency (pass->static_pass_number, 0, true);
2184
2185 /* Run post-pass cleanup and verification. */
2186 execute_todo (todo_after);
2187 verify_interpass_invariants ();
2188 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2189 check_profile_consistency (pass->static_pass_number, 1, true);
2190
2191 if (dump_file)
2192 do_per_function (execute_function_dump, NULL);
2193 pass_fini_dump_file (pass);
2194
2195 current_pass = NULL;
2196
2197 /* Signal this is a suitable GC collection point. */
2198 if (!(todo_after & TODO_do_not_ggc_collect))
2199 ggc_collect ();
2200 }
2201
2202 /* For the current function, execute all ipa transforms. */
2203
2204 void
2205 execute_all_ipa_transforms (void)
2206 {
2207 struct cgraph_node *node;
2208 if (!cfun)
2209 return;
2210 node = cgraph_node::get (current_function_decl);
2211
2212 if (node->ipa_transforms_to_apply.exists ())
2213 {
2214 unsigned int i;
2215
2216 for (i = 0; i < node->ipa_transforms_to_apply.length (); i++)
2217 execute_one_ipa_transform_pass (node, node->ipa_transforms_to_apply[i]);
2218 node->ipa_transforms_to_apply.release ();
2219 }
2220 }
2221
2222 /* Check if PASS is explicitly disabled or enabled and return
2223 the gate status. FUNC is the function to be processed, and
2224 GATE_STATUS is the gate status determined by pass manager by
2225 default. */
2226
2227 static bool
2228 override_gate_status (opt_pass *pass, tree func, bool gate_status)
2229 {
2230 bool explicitly_enabled = false;
2231 bool explicitly_disabled = false;
2232
2233 explicitly_enabled
2234 = is_pass_explicitly_enabled_or_disabled (pass, func,
2235 enabled_pass_uid_range_tab);
2236 explicitly_disabled
2237 = is_pass_explicitly_enabled_or_disabled (pass, func,
2238 disabled_pass_uid_range_tab);
2239
2240 gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
2241
2242 return gate_status;
2243 }
2244
2245
2246 /* Execute PASS. */
2247
2248 bool
2249 execute_one_pass (opt_pass *pass)
2250 {
2251 unsigned int todo_after = 0;
2252
2253 bool gate_status;
2254
2255 /* IPA passes are executed on whole program, so cfun should be NULL.
2256 Other passes need function context set. */
2257 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
2258 gcc_assert (!cfun && !current_function_decl);
2259 else
2260 gcc_assert (cfun && current_function_decl);
2261
2262 current_pass = pass;
2263
2264 /* Check whether gate check should be avoided.
2265 User controls the value of the gate through the parameter "gate_status". */
2266 gate_status = pass->gate (cfun);
2267 gate_status = override_gate_status (pass, current_function_decl, gate_status);
2268
2269 /* Override gate with plugin. */
2270 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
2271
2272 if (!gate_status)
2273 {
2274 /* Run so passes selectively disabling themselves on a given function
2275 are not miscounted. */
2276 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2277 {
2278 check_profile_consistency (pass->static_pass_number, 0, false);
2279 check_profile_consistency (pass->static_pass_number, 1, false);
2280 }
2281 current_pass = NULL;
2282 return false;
2283 }
2284
2285 /* Pass execution event trigger: useful to identify passes being
2286 executed. */
2287 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
2288
2289 if (!quiet_flag && !cfun)
2290 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
2291
2292 /* Note that the folders should only create gimple expressions.
2293 This is a hack until the new folder is ready. */
2294 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
2295
2296 pass_init_dump_file (pass);
2297
2298 /* Run pre-pass verification. */
2299 execute_todo (pass->todo_flags_start);
2300
2301 #ifdef ENABLE_CHECKING
2302 do_per_function (verify_curr_properties,
2303 (void *)(size_t)pass->properties_required);
2304 #endif
2305
2306 /* If a timevar is present, start it. */
2307 if (pass->tv_id != TV_NONE)
2308 timevar_push (pass->tv_id);
2309
2310 /* Do it! */
2311 todo_after = pass->execute (cfun);
2312 do_per_function (clear_last_verified, NULL);
2313
2314 /* Stop timevar. */
2315 if (pass->tv_id != TV_NONE)
2316 timevar_pop (pass->tv_id);
2317
2318 do_per_function (update_properties_after_pass, pass);
2319
2320 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2321 check_profile_consistency (pass->static_pass_number, 0, true);
2322
2323 /* Run post-pass cleanup and verification. */
2324 execute_todo (todo_after | pass->todo_flags_finish | TODO_verify_il);
2325 if (profile_report && cfun && (cfun->curr_properties & PROP_cfg))
2326 check_profile_consistency (pass->static_pass_number, 1, true);
2327
2328 verify_interpass_invariants ();
2329 if (dump_file)
2330 do_per_function (execute_function_dump, pass);
2331 if (pass->type == IPA_PASS)
2332 {
2333 struct cgraph_node *node;
2334 FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
2335 node->ipa_transforms_to_apply.safe_push ((ipa_opt_pass_d *)pass);
2336 }
2337
2338 if (!current_function_decl)
2339 symtab->process_new_functions ();
2340
2341 pass_fini_dump_file (pass);
2342
2343 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
2344 gcc_assert (!(cfun->curr_properties & PROP_trees)
2345 || pass->type != RTL_PASS);
2346
2347 current_pass = NULL;
2348
2349 /* Signal this is a suitable GC collection point. */
2350 if (!((todo_after | pass->todo_flags_finish) & TODO_do_not_ggc_collect))
2351 ggc_collect ();
2352
2353 return true;
2354 }
2355
2356 static void
2357 execute_pass_list_1 (opt_pass *pass)
2358 {
2359 do
2360 {
2361 gcc_assert (pass->type == GIMPLE_PASS
2362 || pass->type == RTL_PASS);
2363 if (execute_one_pass (pass) && pass->sub)
2364 execute_pass_list_1 (pass->sub);
2365 pass = pass->next;
2366 }
2367 while (pass);
2368 }
2369
2370 void
2371 execute_pass_list (function *fn, opt_pass *pass)
2372 {
2373 push_cfun (fn);
2374 execute_pass_list_1 (pass);
2375 if (fn->cfg)
2376 {
2377 free_dominance_info (CDI_DOMINATORS);
2378 free_dominance_info (CDI_POST_DOMINATORS);
2379 }
2380 pop_cfun ();
2381 }
2382
2383 /* Write out all LTO data. */
2384 static void
2385 write_lto (void)
2386 {
2387 timevar_push (TV_IPA_LTO_GIMPLE_OUT);
2388 lto_output ();
2389 timevar_pop (TV_IPA_LTO_GIMPLE_OUT);
2390 timevar_push (TV_IPA_LTO_DECL_OUT);
2391 produce_asm_for_decls ();
2392 timevar_pop (TV_IPA_LTO_DECL_OUT);
2393 }
2394
2395 /* Same as execute_pass_list but assume that subpasses of IPA passes
2396 are local passes. If SET is not NULL, write out summaries of only
2397 those node in SET. */
2398
2399 static void
2400 ipa_write_summaries_2 (opt_pass *pass, struct lto_out_decl_state *state)
2401 {
2402 while (pass)
2403 {
2404 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2405 gcc_assert (!current_function_decl);
2406 gcc_assert (!cfun);
2407 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2408 if (pass->type == IPA_PASS
2409 && ipa_pass->write_summary
2410 && pass->gate (cfun))
2411 {
2412 /* If a timevar is present, start it. */
2413 if (pass->tv_id)
2414 timevar_push (pass->tv_id);
2415
2416 pass_init_dump_file (pass);
2417
2418 current_pass = pass;
2419 ipa_pass->write_summary ();
2420
2421 pass_fini_dump_file (pass);
2422
2423 /* If a timevar is present, start it. */
2424 if (pass->tv_id)
2425 timevar_pop (pass->tv_id);
2426 }
2427
2428 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2429 ipa_write_summaries_2 (pass->sub, state);
2430
2431 pass = pass->next;
2432 }
2433 }
2434
2435 /* Helper function of ipa_write_summaries. Creates and destroys the
2436 decl state and calls ipa_write_summaries_2 for all passes that have
2437 summaries. SET is the set of nodes to be written. */
2438
2439 static void
2440 ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
2441 {
2442 pass_manager *passes = g->get_passes ();
2443 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2444 state->symtab_node_encoder = encoder;
2445
2446 lto_push_out_decl_state (state);
2447
2448 gcc_assert (!flag_wpa);
2449 ipa_write_summaries_2 (passes->all_regular_ipa_passes, state);
2450
2451 write_lto ();
2452
2453 gcc_assert (lto_get_out_decl_state () == state);
2454 lto_pop_out_decl_state ();
2455 lto_delete_out_decl_state (state);
2456 }
2457
2458 /* Write out summaries for all the nodes in the callgraph. */
2459
2460 void
2461 ipa_write_summaries (bool offload_lto_mode)
2462 {
2463 lto_symtab_encoder_t encoder;
2464 int i, order_pos;
2465 varpool_node *vnode;
2466 struct cgraph_node *node;
2467 struct cgraph_node **order;
2468
2469 if (!flag_generate_lto || seen_error ())
2470 return;
2471
2472 select_what_to_stream (offload_lto_mode);
2473
2474 encoder = lto_symtab_encoder_new (false);
2475
2476 /* Create the callgraph set in the same order used in
2477 cgraph_expand_all_functions. This mostly facilitates debugging,
2478 since it causes the gimple file to be processed in the same order
2479 as the source code. */
2480 order = XCNEWVEC (struct cgraph_node *, symtab->cgraph_count);
2481 order_pos = ipa_reverse_postorder (order);
2482 gcc_assert (order_pos == symtab->cgraph_count);
2483
2484 for (i = order_pos - 1; i >= 0; i--)
2485 {
2486 struct cgraph_node *node = order[i];
2487
2488 if (node->has_gimple_body_p ())
2489 {
2490 /* When streaming out references to statements as part of some IPA
2491 pass summary, the statements need to have uids assigned and the
2492 following does that for all the IPA passes here. Naturally, this
2493 ordering then matches the one IPA-passes get in their stmt_fixup
2494 hooks. */
2495
2496 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2497 renumber_gimple_stmt_uids ();
2498 pop_cfun ();
2499 }
2500 if (node->definition && node->need_lto_streaming)
2501 lto_set_symtab_encoder_in_partition (encoder, node);
2502 }
2503
2504 FOR_EACH_DEFINED_FUNCTION (node)
2505 if (node->alias && node->need_lto_streaming)
2506 lto_set_symtab_encoder_in_partition (encoder, node);
2507 FOR_EACH_DEFINED_VARIABLE (vnode)
2508 if (vnode->need_lto_streaming)
2509 lto_set_symtab_encoder_in_partition (encoder, vnode);
2510
2511 ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
2512
2513 free (order);
2514 }
2515
2516 /* Same as execute_pass_list but assume that subpasses of IPA passes
2517 are local passes. If SET is not NULL, write out optimization summaries of
2518 only those node in SET. */
2519
2520 static void
2521 ipa_write_optimization_summaries_1 (opt_pass *pass,
2522 struct lto_out_decl_state *state)
2523 {
2524 while (pass)
2525 {
2526 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *)pass;
2527 gcc_assert (!current_function_decl);
2528 gcc_assert (!cfun);
2529 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2530 if (pass->type == IPA_PASS
2531 && ipa_pass->write_optimization_summary
2532 && pass->gate (cfun))
2533 {
2534 /* If a timevar is present, start it. */
2535 if (pass->tv_id)
2536 timevar_push (pass->tv_id);
2537
2538 pass_init_dump_file (pass);
2539
2540 current_pass = pass;
2541 ipa_pass->write_optimization_summary ();
2542
2543 pass_fini_dump_file (pass);
2544
2545 /* If a timevar is present, start it. */
2546 if (pass->tv_id)
2547 timevar_pop (pass->tv_id);
2548 }
2549
2550 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2551 ipa_write_optimization_summaries_1 (pass->sub, state);
2552
2553 pass = pass->next;
2554 }
2555 }
2556
2557 /* Write all the optimization summaries for the cgraph nodes in SET. If SET is
2558 NULL, write out all summaries of all nodes. */
2559
2560 void
2561 ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
2562 {
2563 struct lto_out_decl_state *state = lto_new_out_decl_state ();
2564 lto_symtab_encoder_iterator lsei;
2565 state->symtab_node_encoder = encoder;
2566
2567 lto_push_out_decl_state (state);
2568 for (lsei = lsei_start_function_in_partition (encoder);
2569 !lsei_end_p (lsei); lsei_next_function_in_partition (&lsei))
2570 {
2571 struct cgraph_node *node = lsei_cgraph_node (lsei);
2572 /* When streaming out references to statements as part of some IPA
2573 pass summary, the statements need to have uids assigned.
2574
2575 For functions newly born at WPA stage we need to initialize
2576 the uids here. */
2577 if (node->definition
2578 && gimple_has_body_p (node->decl))
2579 {
2580 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2581 renumber_gimple_stmt_uids ();
2582 pop_cfun ();
2583 }
2584 }
2585
2586 gcc_assert (flag_wpa);
2587 pass_manager *passes = g->get_passes ();
2588 ipa_write_optimization_summaries_1 (passes->all_regular_ipa_passes, state);
2589
2590 write_lto ();
2591
2592 gcc_assert (lto_get_out_decl_state () == state);
2593 lto_pop_out_decl_state ();
2594 lto_delete_out_decl_state (state);
2595 }
2596
2597 /* Same as execute_pass_list but assume that subpasses of IPA passes
2598 are local passes. */
2599
2600 static void
2601 ipa_read_summaries_1 (opt_pass *pass)
2602 {
2603 while (pass)
2604 {
2605 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2606
2607 gcc_assert (!current_function_decl);
2608 gcc_assert (!cfun);
2609 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2610
2611 if (pass->gate (cfun))
2612 {
2613 if (pass->type == IPA_PASS && ipa_pass->read_summary)
2614 {
2615 /* If a timevar is present, start it. */
2616 if (pass->tv_id)
2617 timevar_push (pass->tv_id);
2618
2619 pass_init_dump_file (pass);
2620
2621 current_pass = pass;
2622 ipa_pass->read_summary ();
2623
2624 pass_fini_dump_file (pass);
2625
2626 /* Stop timevar. */
2627 if (pass->tv_id)
2628 timevar_pop (pass->tv_id);
2629 }
2630
2631 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2632 ipa_read_summaries_1 (pass->sub);
2633 }
2634 pass = pass->next;
2635 }
2636 }
2637
2638
2639 /* Read all the summaries for all_regular_ipa_passes. */
2640
2641 void
2642 ipa_read_summaries (void)
2643 {
2644 pass_manager *passes = g->get_passes ();
2645 ipa_read_summaries_1 (passes->all_regular_ipa_passes);
2646 }
2647
2648 /* Same as execute_pass_list but assume that subpasses of IPA passes
2649 are local passes. */
2650
2651 static void
2652 ipa_read_optimization_summaries_1 (opt_pass *pass)
2653 {
2654 while (pass)
2655 {
2656 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2657
2658 gcc_assert (!current_function_decl);
2659 gcc_assert (!cfun);
2660 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2661
2662 if (pass->gate (cfun))
2663 {
2664 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2665 {
2666 /* If a timevar is present, start it. */
2667 if (pass->tv_id)
2668 timevar_push (pass->tv_id);
2669
2670 pass_init_dump_file (pass);
2671
2672 current_pass = pass;
2673 ipa_pass->read_optimization_summary ();
2674
2675 pass_fini_dump_file (pass);
2676
2677 /* Stop timevar. */
2678 if (pass->tv_id)
2679 timevar_pop (pass->tv_id);
2680 }
2681
2682 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2683 ipa_read_optimization_summaries_1 (pass->sub);
2684 }
2685 pass = pass->next;
2686 }
2687 }
2688
2689 /* Read all the summaries for all_regular_ipa_passes. */
2690
2691 void
2692 ipa_read_optimization_summaries (void)
2693 {
2694 pass_manager *passes = g->get_passes ();
2695 ipa_read_optimization_summaries_1 (passes->all_regular_ipa_passes);
2696 }
2697
2698 /* Same as execute_pass_list but assume that subpasses of IPA passes
2699 are local passes. */
2700 void
2701 execute_ipa_pass_list (opt_pass *pass)
2702 {
2703 do
2704 {
2705 gcc_assert (!current_function_decl);
2706 gcc_assert (!cfun);
2707 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2708 if (execute_one_pass (pass) && pass->sub)
2709 {
2710 if (pass->sub->type == GIMPLE_PASS)
2711 {
2712 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2713 do_per_function_toporder ((void (*)(function *, void *))
2714 execute_pass_list,
2715 pass->sub);
2716 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2717 }
2718 else if (pass->sub->type == SIMPLE_IPA_PASS
2719 || pass->sub->type == IPA_PASS)
2720 execute_ipa_pass_list (pass->sub);
2721 else
2722 gcc_unreachable ();
2723 }
2724 gcc_assert (!current_function_decl);
2725 symtab->process_new_functions ();
2726 pass = pass->next;
2727 }
2728 while (pass);
2729 }
2730
2731 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
2732
2733 static void
2734 execute_ipa_stmt_fixups (opt_pass *pass,
2735 struct cgraph_node *node, gimple *stmts)
2736 {
2737 while (pass)
2738 {
2739 /* Execute all of the IPA_PASSes in the list. */
2740 if (pass->type == IPA_PASS
2741 && pass->gate (cfun))
2742 {
2743 ipa_opt_pass_d *ipa_pass = (ipa_opt_pass_d *) pass;
2744
2745 if (ipa_pass->stmt_fixup)
2746 {
2747 pass_init_dump_file (pass);
2748 /* If a timevar is present, start it. */
2749 if (pass->tv_id)
2750 timevar_push (pass->tv_id);
2751
2752 current_pass = pass;
2753 ipa_pass->stmt_fixup (node, stmts);
2754
2755 /* Stop timevar. */
2756 if (pass->tv_id)
2757 timevar_pop (pass->tv_id);
2758 pass_fini_dump_file (pass);
2759 }
2760 if (pass->sub)
2761 execute_ipa_stmt_fixups (pass->sub, node, stmts);
2762 }
2763 pass = pass->next;
2764 }
2765 }
2766
2767 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
2768
2769 void
2770 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2771 {
2772 pass_manager *passes = g->get_passes ();
2773 execute_ipa_stmt_fixups (passes->all_regular_ipa_passes, node, stmts);
2774 }
2775
2776
2777 extern void debug_properties (unsigned int);
2778 extern void dump_properties (FILE *, unsigned int);
2779
2780 DEBUG_FUNCTION void
2781 dump_properties (FILE *dump, unsigned int props)
2782 {
2783 fprintf (dump, "Properties:\n");
2784 if (props & PROP_gimple_any)
2785 fprintf (dump, "PROP_gimple_any\n");
2786 if (props & PROP_gimple_lcf)
2787 fprintf (dump, "PROP_gimple_lcf\n");
2788 if (props & PROP_gimple_leh)
2789 fprintf (dump, "PROP_gimple_leh\n");
2790 if (props & PROP_cfg)
2791 fprintf (dump, "PROP_cfg\n");
2792 if (props & PROP_ssa)
2793 fprintf (dump, "PROP_ssa\n");
2794 if (props & PROP_no_crit_edges)
2795 fprintf (dump, "PROP_no_crit_edges\n");
2796 if (props & PROP_rtl)
2797 fprintf (dump, "PROP_rtl\n");
2798 if (props & PROP_gimple_lomp)
2799 fprintf (dump, "PROP_gimple_lomp\n");
2800 if (props & PROP_gimple_lcx)
2801 fprintf (dump, "PROP_gimple_lcx\n");
2802 if (props & PROP_gimple_lvec)
2803 fprintf (dump, "PROP_gimple_lvec\n");
2804 if (props & PROP_cfglayout)
2805 fprintf (dump, "PROP_cfglayout\n");
2806 }
2807
2808 DEBUG_FUNCTION void
2809 debug_properties (unsigned int props)
2810 {
2811 dump_properties (stderr, props);
2812 }
2813
2814 /* Called by local passes to see if function is called by already processed nodes.
2815 Because we process nodes in topological order, this means that function is
2816 in recursive cycle or we introduced new direct calls. */
2817 bool
2818 function_called_by_processed_nodes_p (void)
2819 {
2820 struct cgraph_edge *e;
2821 for (e = cgraph_node::get (current_function_decl)->callers;
2822 e;
2823 e = e->next_caller)
2824 {
2825 if (e->caller->decl == current_function_decl)
2826 continue;
2827 if (!e->caller->has_gimple_body_p ())
2828 continue;
2829 if (TREE_ASM_WRITTEN (e->caller->decl))
2830 continue;
2831 if (!e->caller->process && !e->caller->global.inlined_to)
2832 break;
2833 }
2834 if (dump_file && e)
2835 {
2836 fprintf (dump_file, "Already processed call to:\n");
2837 e->caller->dump (dump_file);
2838 }
2839 return e != NULL;
2840 }
2841
2842 #include "gt-passes.h"