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