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