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