xref: /openbsd/gnu/gcc/gcc/passes.c (revision 404b540a)
1 /* Top level of GCC compilers (cc1, cc1plus, etc.)
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11 
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21 
22 /* This is the top level of cc1/c++.
23    It parses command args, opens files, invokes the various passes
24    in the proper order, and counts the time used by each.
25    Error messages and low-level interface to malloc also handled here.  */
26 
27 #include "config.h"
28 #undef FLOAT /* This is for hpux. They should change hpux.  */
29 #undef FFS  /* Some systems define this in param.h.  */
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include <signal.h>
34 
35 #ifdef HAVE_SYS_RESOURCE_H
36 # include <sys/resource.h>
37 #endif
38 
39 #ifdef HAVE_SYS_TIMES_H
40 # include <sys/times.h>
41 #endif
42 
43 #include "line-map.h"
44 #include "input.h"
45 #include "tree.h"
46 #include "rtl.h"
47 #include "tm_p.h"
48 #include "flags.h"
49 #include "insn-attr.h"
50 #include "insn-config.h"
51 #include "insn-flags.h"
52 #include "hard-reg-set.h"
53 #include "recog.h"
54 #include "output.h"
55 #include "except.h"
56 #include "function.h"
57 #include "toplev.h"
58 #include "expr.h"
59 #include "basic-block.h"
60 #include "intl.h"
61 #include "ggc.h"
62 #include "graph.h"
63 #include "regs.h"
64 #include "timevar.h"
65 #include "diagnostic.h"
66 #include "params.h"
67 #include "reload.h"
68 #include "dwarf2asm.h"
69 #include "integrate.h"
70 #include "real.h"
71 #include "debug.h"
72 #include "target.h"
73 #include "langhooks.h"
74 #include "cfglayout.h"
75 #include "cfgloop.h"
76 #include "hosthooks.h"
77 #include "cgraph.h"
78 #include "opts.h"
79 #include "coverage.h"
80 #include "value-prof.h"
81 #include "tree-inline.h"
82 #include "tree-flow.h"
83 #include "tree-pass.h"
84 #include "tree-dump.h"
85 
86 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
87 #include "dwarf2out.h"
88 #endif
89 
90 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
91 #include "dbxout.h"
92 #endif
93 
94 #ifdef SDB_DEBUGGING_INFO
95 #include "sdbout.h"
96 #endif
97 
98 #ifdef XCOFF_DEBUGGING_INFO
99 #include "xcoffout.h"		/* Needed for external data
100 				   declarations for e.g. AIX 4.x.  */
101 #endif
102 
103 /* Global variables used to communicate with passes.  */
104 int dump_flags;
105 bool in_gimple_form;
106 
107 
108 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
109    and TYPE_DECL nodes.
110 
111    This does nothing for local (non-static) variables, unless the
112    variable is a register variable with DECL_ASSEMBLER_NAME set.  In
113    that case, or if the variable is not an automatic, it sets up the
114    RTL and outputs any assembler code (label definition, storage
115    allocation and initialization).
116 
117    DECL is the declaration.  TOP_LEVEL is nonzero
118    if this declaration is not within a function.  */
119 
120 void
rest_of_decl_compilation(tree decl,int top_level,int at_end)121 rest_of_decl_compilation (tree decl,
122 			  int top_level,
123 			  int at_end)
124 {
125   /* We deferred calling assemble_alias so that we could collect
126      other attributes such as visibility.  Emit the alias now.  */
127   {
128     tree alias;
129     alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
130     if (alias)
131       {
132 	alias = TREE_VALUE (TREE_VALUE (alias));
133 	alias = get_identifier (TREE_STRING_POINTER (alias));
134 	assemble_alias (decl, alias);
135       }
136   }
137 
138   /* Can't defer this, because it needs to happen before any
139      later function definitions are processed.  */
140   if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
141     make_decl_rtl (decl);
142 
143   /* Forward declarations for nested functions are not "external",
144      but we need to treat them as if they were.  */
145   if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
146       || TREE_CODE (decl) == FUNCTION_DECL)
147     {
148       timevar_push (TV_VARCONST);
149 
150       /* Don't output anything when a tentative file-scope definition
151 	 is seen.  But at end of compilation, do output code for them.
152 
153 	 We do output all variables when unit-at-a-time is active and rely on
154 	 callgraph code to defer them except for forward declarations
155 	 (see gcc.c-torture/compile/920624-1.c) */
156       if ((at_end
157 	   || !DECL_DEFER_OUTPUT (decl)
158 	   || DECL_INITIAL (decl))
159 	  && !DECL_EXTERNAL (decl))
160 	{
161 	  if (TREE_CODE (decl) != FUNCTION_DECL)
162 	    cgraph_varpool_finalize_decl (decl);
163 	  else
164 	    assemble_variable (decl, top_level, at_end, 0);
165 	}
166 
167 #ifdef ASM_FINISH_DECLARE_OBJECT
168       if (decl == last_assemble_variable_decl)
169 	{
170 	  ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
171 				     top_level, at_end);
172 	}
173 #endif
174 
175       timevar_pop (TV_VARCONST);
176     }
177   else if (TREE_CODE (decl) == TYPE_DECL
178 	   /* Like in rest_of_type_compilation, avoid confusing the debug
179 	      information machinery when there are errors.  */
180 	   && !(sorrycount || errorcount))
181     {
182       timevar_push (TV_SYMOUT);
183       debug_hooks->type_decl (decl, !top_level);
184       timevar_pop (TV_SYMOUT);
185     }
186 
187   /* Let cgraph know about the existence of variables.  */
188   if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
189     cgraph_varpool_node (decl);
190 }
191 
192 /* Called after finishing a record, union or enumeral type.  */
193 
194 void
rest_of_type_compilation(tree type,int toplev)195 rest_of_type_compilation (tree type, int toplev)
196 {
197   /* Avoid confusing the debug information machinery when there are
198      errors.  */
199   if (errorcount != 0 || sorrycount != 0)
200     return;
201 
202   timevar_push (TV_SYMOUT);
203   debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
204   timevar_pop (TV_SYMOUT);
205 }
206 
207 
208 
209 void
finish_optimization_passes(void)210 finish_optimization_passes (void)
211 {
212   enum tree_dump_index i;
213   struct dump_file_info *dfi;
214   char *name;
215 
216   timevar_push (TV_DUMP);
217   if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
218     {
219       dump_file = dump_begin (pass_profile.static_pass_number, NULL);
220       end_branch_prob ();
221       if (dump_file)
222 	dump_end (pass_profile.static_pass_number, dump_file);
223     }
224 
225   if (optimize > 0)
226     {
227       dump_file = dump_begin (pass_combine.static_pass_number, NULL);
228       if (dump_file)
229 	{
230 	  dump_combine_total_stats (dump_file);
231           dump_end (pass_combine.static_pass_number, dump_file);
232 	}
233     }
234 
235   /* Do whatever is necessary to finish printing the graphs.  */
236   if (graph_dump_format != no_graph)
237     for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
238       if (dump_initialized_p (i)
239 	  && (dfi->flags & TDF_GRAPH) != 0
240 	  && (name = get_dump_file_name (i)) != NULL)
241 	{
242 	  finish_graph_dump_file (name);
243 	  free (name);
244 	}
245 
246   timevar_pop (TV_DUMP);
247 }
248 
249 static bool
gate_rest_of_compilation(void)250 gate_rest_of_compilation (void)
251 {
252   /* Early return if there were errors.  We can run afoul of our
253      consistency checks, and there's not really much point in fixing them.  */
254   return !(rtl_dump_and_exit || flag_syntax_only || errorcount || sorrycount);
255 }
256 
257 struct tree_opt_pass pass_rest_of_compilation =
258 {
259   NULL,                                 /* name */
260   gate_rest_of_compilation,             /* gate */
261   NULL,                                 /* execute */
262   NULL,                                 /* sub */
263   NULL,                                 /* next */
264   0,                                    /* static_pass_number */
265   TV_REST_OF_COMPILATION,               /* tv_id */
266   PROP_rtl,                             /* properties_required */
267   0,                                    /* properties_provided */
268   0,                                    /* properties_destroyed */
269   0,                                    /* todo_flags_start */
270   TODO_ggc_collect,                     /* todo_flags_finish */
271   0                                     /* letter */
272 };
273 
274 static bool
gate_postreload(void)275 gate_postreload (void)
276 {
277   return reload_completed;
278 }
279 
280 struct tree_opt_pass pass_postreload =
281 {
282   NULL,                                 /* name */
283   gate_postreload,                      /* gate */
284   NULL,                                 /* execute */
285   NULL,                                 /* sub */
286   NULL,                                 /* next */
287   0,                                    /* static_pass_number */
288   0,                                    /* tv_id */
289   PROP_rtl,                             /* properties_required */
290   0,                                    /* properties_provided */
291   0,                                    /* properties_destroyed */
292   0,                                    /* todo_flags_start */
293   TODO_ggc_collect,                     /* todo_flags_finish */
294   0					/* letter */
295 };
296 
297 
298 
299 /* The root of the compilation pass tree, once constructed.  */
300 struct tree_opt_pass *all_passes, *all_ipa_passes, *all_lowering_passes;
301 
302 /* Iterate over the pass tree allocating dump file numbers.  We want
303    to do this depth first, and independent of whether the pass is
304    enabled or not.  */
305 
306 static void
register_one_dump_file(struct tree_opt_pass * pass,bool ipa,int properties)307 register_one_dump_file (struct tree_opt_pass *pass, bool ipa, int properties)
308 {
309   char *dot_name, *flag_name, *glob_name;
310   const char *prefix;
311   char num[10];
312   int flags;
313 
314   /* See below in next_pass_1.  */
315   num[0] = '\0';
316   if (pass->static_pass_number != -1)
317     sprintf (num, "%d", ((int) pass->static_pass_number < 0
318 			 ? 1 : pass->static_pass_number));
319 
320   dot_name = concat (".", pass->name, num, NULL);
321   if (ipa)
322     prefix = "ipa-", flags = TDF_IPA;
323   else if (properties & PROP_trees)
324     prefix = "tree-", flags = TDF_TREE;
325   else
326     prefix = "rtl-", flags = TDF_RTL;
327 
328   flag_name = concat (prefix, pass->name, num, NULL);
329   glob_name = concat (prefix, pass->name, NULL);
330   pass->static_pass_number = dump_register (dot_name, flag_name, glob_name,
331                                             flags, pass->letter);
332 }
333 
334 /* Recursive worker function for register_dump_files.  */
335 
336 static int
register_dump_files_1(struct tree_opt_pass * pass,bool ipa,int properties)337 register_dump_files_1 (struct tree_opt_pass *pass, bool ipa, int properties)
338 {
339   do
340     {
341       int new_properties = (properties | pass->properties_provided)
342 			   & ~pass->properties_destroyed;
343 
344       if (pass->name)
345         register_one_dump_file (pass, ipa, new_properties);
346 
347       if (pass->sub)
348         new_properties = register_dump_files_1 (pass->sub, false,
349 						new_properties);
350 
351       /* If we have a gate, combine the properties that we could have with
352          and without the pass being examined.  */
353       if (pass->gate)
354         properties &= new_properties;
355       else
356         properties = new_properties;
357 
358       pass = pass->next;
359     }
360   while (pass);
361 
362   return properties;
363 }
364 
365 /* Register the dump files for the pipeline starting at PASS.  IPA is
366    true if the pass is inter-procedural, and PROPERTIES reflects the
367    properties that are guaranteed to be available at the beginning of
368    the pipeline.  */
369 
370 static void
register_dump_files(struct tree_opt_pass * pass,bool ipa,int properties)371 register_dump_files (struct tree_opt_pass *pass, bool ipa, int properties)
372 {
373   pass->properties_required |= properties;
374   pass->todo_flags_start |= TODO_set_props;
375   register_dump_files_1 (pass, ipa, properties);
376 }
377 
378 /* Add a pass to the pass list. Duplicate the pass if it's already
379    in the list.  */
380 
381 static struct tree_opt_pass **
next_pass_1(struct tree_opt_pass ** list,struct tree_opt_pass * pass)382 next_pass_1 (struct tree_opt_pass **list, struct tree_opt_pass *pass)
383 {
384   /* A nonzero static_pass_number indicates that the
385      pass is already in the list.  */
386   if (pass->static_pass_number)
387     {
388       struct tree_opt_pass *new;
389 
390       new = xmalloc (sizeof (*new));
391       memcpy (new, pass, sizeof (*new));
392 
393       /* Indicate to register_dump_files that this pass has duplicates,
394          and so it should rename the dump file.  The first instance will
395          be -1, and be number of duplicates = -static_pass_number - 1.
396          Subsequent instances will be > 0 and just the duplicate number.  */
397       if (pass->name)
398         {
399           pass->static_pass_number -= 1;
400           new->static_pass_number = -pass->static_pass_number;
401 	}
402 
403       *list = new;
404     }
405   else
406     {
407       pass->static_pass_number = -1;
408       *list = pass;
409     }
410 
411   return &(*list)->next;
412 
413 }
414 
415 /* Construct the pass tree.  The sequencing of passes is driven by
416    the cgraph routines:
417 
418    cgraph_finalize_compilation_unit ()
419        for each node N in the cgraph
420 	   cgraph_analyze_function (N)
421 	       cgraph_lower_function (N) -> all_lowering_passes
422 
423    If we are optimizing, cgraph_optimize is then invoked:
424 
425    cgraph_optimize ()
426        ipa_passes () 			-> all_ipa_passes
427        cgraph_expand_all_functions ()
428            for each node N in the cgraph
429 	       cgraph_expand_function (N)
430 	           cgraph_lower_function (N)	-> Now a NOP.
431 		   lang_hooks.callgraph.expand_function (DECL (N))
432 		   	tree_rest_of_compilation (DECL (N))  -> all_passes
433 */
434 
435 void
init_optimization_passes(void)436 init_optimization_passes (void)
437 {
438   struct tree_opt_pass **p;
439 
440 #define NEXT_PASS(PASS)  (p = next_pass_1 (p, &PASS))
441   /* Interprocedural optimization passes.  */
442   p = &all_ipa_passes;
443   NEXT_PASS (pass_early_ipa_inline);
444   NEXT_PASS (pass_early_local_passes);
445   NEXT_PASS (pass_ipa_cp);
446   NEXT_PASS (pass_ipa_inline);
447   NEXT_PASS (pass_ipa_reference);
448   NEXT_PASS (pass_ipa_pure_const);
449   NEXT_PASS (pass_ipa_type_escape);
450   NEXT_PASS (pass_ipa_pta);
451   *p = NULL;
452 
453   /* All passes needed to lower the function into shape optimizers can
454      operate on.  */
455   p = &all_lowering_passes;
456   NEXT_PASS (pass_remove_useless_stmts);
457   NEXT_PASS (pass_mudflap_1);
458   NEXT_PASS (pass_lower_omp);
459   NEXT_PASS (pass_lower_cf);
460   NEXT_PASS (pass_lower_eh);
461   NEXT_PASS (pass_build_cfg);
462   NEXT_PASS (pass_lower_complex_O0);
463   NEXT_PASS (pass_lower_vector);
464   NEXT_PASS (pass_warn_function_return);
465   NEXT_PASS (pass_early_tree_profile);
466   *p = NULL;
467 
468   p = &pass_early_local_passes.sub;
469   NEXT_PASS (pass_tree_profile);
470   NEXT_PASS (pass_cleanup_cfg);
471   NEXT_PASS (pass_rebuild_cgraph_edges);
472   *p = NULL;
473 
474   p = &all_passes;
475   NEXT_PASS (pass_fixup_cfg);
476   NEXT_PASS (pass_init_datastructures);
477   NEXT_PASS (pass_expand_omp);
478   NEXT_PASS (pass_all_optimizations);
479   NEXT_PASS (pass_warn_function_noreturn);
480   NEXT_PASS (pass_mudflap_2);
481   NEXT_PASS (pass_free_datastructures);
482   NEXT_PASS (pass_free_cfg_annotations);
483   NEXT_PASS (pass_expand);
484   NEXT_PASS (pass_rest_of_compilation);
485   NEXT_PASS (pass_clean_state);
486   *p = NULL;
487 
488   p = &pass_all_optimizations.sub;
489   NEXT_PASS (pass_referenced_vars);
490   NEXT_PASS (pass_reset_cc_flags);
491   NEXT_PASS (pass_create_structure_vars);
492   NEXT_PASS (pass_build_ssa);
493   NEXT_PASS (pass_may_alias);
494   NEXT_PASS (pass_return_slot);
495   NEXT_PASS (pass_rename_ssa_copies);
496   NEXT_PASS (pass_early_warn_uninitialized);
497 
498   /* Initial scalar cleanups.  */
499   NEXT_PASS (pass_ccp);
500   NEXT_PASS (pass_fre);
501   NEXT_PASS (pass_dce);
502   NEXT_PASS (pass_forwprop);
503   NEXT_PASS (pass_copy_prop);
504   NEXT_PASS (pass_merge_phi);
505   NEXT_PASS (pass_vrp);
506   NEXT_PASS (pass_dce);
507   NEXT_PASS (pass_dominator);
508 
509   /* The only const/copy propagation opportunities left after
510      DOM should be due to degenerate PHI nodes.  So rather than
511      run the full propagators, run a specialized pass which
512      only examines PHIs to discover const/copy propagation
513      opportunities.  */
514   NEXT_PASS (pass_phi_only_cprop);
515 
516   NEXT_PASS (pass_phiopt);
517   NEXT_PASS (pass_may_alias);
518   NEXT_PASS (pass_tail_recursion);
519   NEXT_PASS (pass_profile);
520   NEXT_PASS (pass_ch);
521   NEXT_PASS (pass_stdarg);
522   NEXT_PASS (pass_lower_complex);
523   NEXT_PASS (pass_sra);
524   /* FIXME: SRA may generate arbitrary gimple code, exposing new
525      aliased and call-clobbered variables.  As mentioned below,
526      pass_may_alias should be a TODO item.  */
527   NEXT_PASS (pass_may_alias);
528   NEXT_PASS (pass_rename_ssa_copies);
529   NEXT_PASS (pass_dominator);
530 
531   /* The only const/copy propagation opportunities left after
532      DOM should be due to degenerate PHI nodes.  So rather than
533      run the full propagators, run a specialized pass which
534      only examines PHIs to discover const/copy propagation
535      opportunities.  */
536   NEXT_PASS (pass_phi_only_cprop);
537 
538   NEXT_PASS (pass_reassoc);
539   NEXT_PASS (pass_dce);
540   NEXT_PASS (pass_dse);
541   NEXT_PASS (pass_may_alias);
542   NEXT_PASS (pass_forwprop);
543   NEXT_PASS (pass_phiopt);
544   NEXT_PASS (pass_object_sizes);
545   NEXT_PASS (pass_store_ccp);
546   NEXT_PASS (pass_store_copy_prop);
547   NEXT_PASS (pass_fold_builtins);
548   /* FIXME: May alias should a TODO but for 4.0.0,
549      we add may_alias right after fold builtins
550      which can create arbitrary GIMPLE.  */
551   NEXT_PASS (pass_may_alias);
552   NEXT_PASS (pass_split_crit_edges);
553   NEXT_PASS (pass_pre);
554   NEXT_PASS (pass_may_alias);
555   NEXT_PASS (pass_sink_code);
556   NEXT_PASS (pass_tree_loop);
557   NEXT_PASS (pass_cse_reciprocals);
558   NEXT_PASS (pass_reassoc);
559   NEXT_PASS (pass_vrp);
560   NEXT_PASS (pass_dominator);
561 
562   /* The only const/copy propagation opportunities left after
563      DOM should be due to degenerate PHI nodes.  So rather than
564      run the full propagators, run a specialized pass which
565      only examines PHIs to discover const/copy propagation
566      opportunities.  */
567   NEXT_PASS (pass_phi_only_cprop);
568 
569   NEXT_PASS (pass_cd_dce);
570 
571   /* FIXME: If DCE is not run before checking for uninitialized uses,
572      we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
573      However, this also causes us to misdiagnose cases that should be
574      real warnings (e.g., testsuite/gcc.dg/pr18501.c).
575 
576      To fix the false positives in uninit-5.c, we would have to
577      account for the predicates protecting the set and the use of each
578      variable.  Using a representation like Gated Single Assignment
579      may help.  */
580   NEXT_PASS (pass_late_warn_uninitialized);
581   NEXT_PASS (pass_dse);
582   NEXT_PASS (pass_forwprop);
583   NEXT_PASS (pass_phiopt);
584   NEXT_PASS (pass_tail_calls);
585   NEXT_PASS (pass_rename_ssa_copies);
586   NEXT_PASS (pass_uncprop);
587   NEXT_PASS (pass_del_ssa);
588   NEXT_PASS (pass_nrv);
589   NEXT_PASS (pass_mark_used_blocks);
590   NEXT_PASS (pass_cleanup_cfg_post_optimizing);
591   *p = NULL;
592 
593   p = &pass_tree_loop.sub;
594   NEXT_PASS (pass_tree_loop_init);
595   NEXT_PASS (pass_copy_prop);
596   NEXT_PASS (pass_lim);
597   NEXT_PASS (pass_tree_unswitch);
598   NEXT_PASS (pass_scev_cprop);
599   NEXT_PASS (pass_empty_loop);
600   NEXT_PASS (pass_record_bounds);
601   NEXT_PASS (pass_linear_transform);
602   NEXT_PASS (pass_iv_canon);
603   NEXT_PASS (pass_if_conversion);
604   NEXT_PASS (pass_vectorize);
605   /* NEXT_PASS (pass_may_alias) cannot be done again because the
606      vectorizer creates alias relations that are not supported by
607      pass_may_alias.  */
608   NEXT_PASS (pass_complete_unroll);
609   NEXT_PASS (pass_loop_prefetch);
610   NEXT_PASS (pass_iv_optimize);
611   NEXT_PASS (pass_tree_loop_done);
612   *p = NULL;
613 
614   p = &pass_vectorize.sub;
615   NEXT_PASS (pass_lower_vector_ssa);
616   NEXT_PASS (pass_dce_loop);
617   *p = NULL;
618 
619   p = &pass_loop2.sub;
620   NEXT_PASS (pass_rtl_loop_init);
621   NEXT_PASS (pass_rtl_move_loop_invariants);
622   NEXT_PASS (pass_rtl_unswitch);
623   NEXT_PASS (pass_rtl_unroll_and_peel_loops);
624   NEXT_PASS (pass_rtl_doloop);
625   NEXT_PASS (pass_rtl_loop_done);
626   *p = NULL;
627 
628   p = &pass_rest_of_compilation.sub;
629   NEXT_PASS (pass_init_function);
630   NEXT_PASS (pass_jump);
631   NEXT_PASS (pass_insn_locators_initialize);
632   NEXT_PASS (pass_rtl_eh);
633   NEXT_PASS (pass_initial_value_sets);
634   NEXT_PASS (pass_unshare_all_rtl);
635   NEXT_PASS (pass_instantiate_virtual_regs);
636   NEXT_PASS (pass_jump2);
637   NEXT_PASS (pass_cse);
638   NEXT_PASS (pass_gcse);
639   NEXT_PASS (pass_jump_bypass);
640   NEXT_PASS (pass_rtl_ifcvt);
641   NEXT_PASS (pass_tracer);
642   /* Perform loop optimizations.  It might be better to do them a bit
643      sooner, but we want the profile feedback to work more
644      efficiently.  */
645   NEXT_PASS (pass_loop2);
646   NEXT_PASS (pass_web);
647   NEXT_PASS (pass_cse2);
648   NEXT_PASS (pass_life);
649   NEXT_PASS (pass_combine);
650   NEXT_PASS (pass_if_after_combine);
651   NEXT_PASS (pass_partition_blocks);
652   NEXT_PASS (pass_regmove);
653   NEXT_PASS (pass_split_all_insns);
654   NEXT_PASS (pass_mode_switching);
655   NEXT_PASS (pass_see);
656   NEXT_PASS (pass_recompute_reg_usage);
657   NEXT_PASS (pass_sms);
658   NEXT_PASS (pass_sched);
659   NEXT_PASS (pass_local_alloc);
660   NEXT_PASS (pass_global_alloc);
661   NEXT_PASS (pass_postreload);
662   *p = NULL;
663 
664   p = &pass_postreload.sub;
665   NEXT_PASS (pass_postreload_cse);
666   NEXT_PASS (pass_gcse2);
667   NEXT_PASS (pass_flow2);
668   NEXT_PASS (pass_rtl_seqabstr);
669   NEXT_PASS (pass_stack_adjustments);
670   NEXT_PASS (pass_peephole2);
671   NEXT_PASS (pass_if_after_reload);
672   NEXT_PASS (pass_regrename);
673   NEXT_PASS (pass_reorder_blocks);
674   NEXT_PASS (pass_branch_target_load_optimize);
675   NEXT_PASS (pass_leaf_regs);
676   NEXT_PASS (pass_sched2);
677   NEXT_PASS (pass_split_before_regstack);
678   NEXT_PASS (pass_stack_regs);
679   NEXT_PASS (pass_compute_alignments);
680   NEXT_PASS (pass_duplicate_computed_gotos);
681   NEXT_PASS (pass_variable_tracking);
682   NEXT_PASS (pass_free_cfg);
683   NEXT_PASS (pass_machine_reorg);
684   NEXT_PASS (pass_purge_lineno_notes);
685   NEXT_PASS (pass_cleanup_barriers);
686   NEXT_PASS (pass_delay_slots);
687   NEXT_PASS (pass_split_for_shorten_branches);
688   NEXT_PASS (pass_convert_to_eh_region_ranges);
689   NEXT_PASS (pass_shorten_branches);
690   NEXT_PASS (pass_set_nothrow_function_flags);
691   NEXT_PASS (pass_final);
692   *p = NULL;
693 
694 #undef NEXT_PASS
695 
696   /* Register the passes with the tree dump code.  */
697   register_dump_files (all_ipa_passes, true,
698 		       PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
699 		       | PROP_cfg);
700   register_dump_files (all_lowering_passes, false, PROP_gimple_any);
701   register_dump_files (all_passes, false,
702 		       PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
703 		       | PROP_cfg);
704 }
705 
706 static unsigned int last_verified;
707 static unsigned int curr_properties;
708 
709 static void
execute_todo(unsigned int flags)710 execute_todo (unsigned int flags)
711 {
712 #if defined ENABLE_CHECKING
713   if (need_ssa_update_p ())
714     gcc_assert (flags & TODO_update_ssa_any);
715 #endif
716 
717   if (curr_properties & PROP_ssa)
718     flags |= TODO_verify_ssa;
719   flags &= ~last_verified;
720   if (!flags)
721     return;
722 
723   /* Always recalculate SMT usage before doing anything else.  */
724   if (flags & TODO_update_smt_usage)
725     recalculate_used_alone ();
726 
727   /* Always cleanup the CFG before trying to update SSA .  */
728   if (flags & TODO_cleanup_cfg)
729     {
730       /* CFG Cleanup can cause a constant to prop into an ARRAY_REF.  */
731       updating_used_alone = true;
732 
733       if (current_loops)
734 	cleanup_tree_cfg_loop ();
735       else
736 	cleanup_tree_cfg ();
737 
738       /* Update the used alone after cleanup cfg.  */
739       recalculate_used_alone ();
740 
741       /* When cleanup_tree_cfg merges consecutive blocks, it may
742 	 perform some simplistic propagation when removing single
743 	 valued PHI nodes.  This propagation may, in turn, cause the
744 	 SSA form to become out-of-date (see PR 22037).  So, even
745 	 if the parent pass had not scheduled an SSA update, we may
746 	 still need to do one.  */
747       if (!(flags & TODO_update_ssa_any) && need_ssa_update_p ())
748 	flags |= TODO_update_ssa;
749     }
750 
751   if (flags & TODO_update_ssa_any)
752     {
753       unsigned update_flags = flags & TODO_update_ssa_any;
754       update_ssa (update_flags);
755       last_verified &= ~TODO_verify_ssa;
756     }
757 
758   if (flags & TODO_remove_unused_locals)
759     remove_unused_locals ();
760 
761   if ((flags & TODO_dump_func)
762       && dump_file && current_function_decl)
763     {
764       if (curr_properties & PROP_trees)
765         dump_function_to_file (current_function_decl,
766                                dump_file, dump_flags);
767       else
768 	{
769 	  if (dump_flags & TDF_SLIM)
770 	    print_rtl_slim_with_bb (dump_file, get_insns (), dump_flags);
771 	  else if ((curr_properties & PROP_cfg) && (dump_flags & TDF_BLOCKS))
772 	    print_rtl_with_bb (dump_file, get_insns ());
773           else
774 	    print_rtl (dump_file, get_insns ());
775 
776 	  if (curr_properties & PROP_cfg
777 	      && graph_dump_format != no_graph
778 	      && (dump_flags & TDF_GRAPH))
779 	    print_rtl_graph_with_bb (dump_file_name, get_insns ());
780 	}
781 
782       /* Flush the file.  If verification fails, we won't be able to
783 	 close the file before aborting.  */
784       fflush (dump_file);
785     }
786   if ((flags & TODO_dump_cgraph)
787       && dump_file && !current_function_decl)
788     {
789       dump_cgraph (dump_file);
790       /* Flush the file.  If verification fails, we won't be able to
791 	 close the file before aborting.  */
792       fflush (dump_file);
793     }
794 
795   if (flags & TODO_ggc_collect)
796     {
797       ggc_collect ();
798     }
799 
800 #if defined ENABLE_CHECKING
801   if (flags & TODO_verify_ssa)
802     verify_ssa (true);
803   if (flags & TODO_verify_flow)
804     verify_flow_info ();
805   if (flags & TODO_verify_stmts)
806     verify_stmts ();
807   if (flags & TODO_verify_loops)
808     verify_loop_closed_ssa ();
809 #endif
810 
811   last_verified = flags & TODO_verify_all;
812 }
813 
814 /* Verify invariants that should hold between passes.  This is a place
815    to put simple sanity checks.  */
816 
817 static void
verify_interpass_invariants(void)818 verify_interpass_invariants (void)
819 {
820 #ifdef ENABLE_CHECKING
821   gcc_assert (!fold_deferring_overflow_warnings_p ());
822 #endif
823 }
824 
825 static bool
execute_one_pass(struct tree_opt_pass * pass)826 execute_one_pass (struct tree_opt_pass *pass)
827 {
828   bool initializing_dump;
829   unsigned int todo_after = 0;
830 
831   /* See if we're supposed to run this pass.  */
832   if (pass->gate && !pass->gate ())
833     return false;
834 
835   if (pass->todo_flags_start & TODO_set_props)
836     curr_properties = pass->properties_required;
837 
838   /* Note that the folders should only create gimple expressions.
839      This is a hack until the new folder is ready.  */
840   in_gimple_form = (curr_properties & PROP_trees) != 0;
841 
842   /* Run pre-pass verification.  */
843   execute_todo (pass->todo_flags_start);
844 
845   gcc_assert ((curr_properties & pass->properties_required)
846 	      == pass->properties_required);
847 
848   if (pass->properties_destroyed & PROP_smt_usage)
849     updating_used_alone = true;
850 
851   /* If a dump file name is present, open it if enabled.  */
852   if (pass->static_pass_number != -1)
853     {
854       initializing_dump = !dump_initialized_p (pass->static_pass_number);
855       dump_file_name = get_dump_file_name (pass->static_pass_number);
856       dump_file = dump_begin (pass->static_pass_number, &dump_flags);
857       if (dump_file && current_function_decl)
858 	{
859 	  const char *dname, *aname;
860 	  dname = lang_hooks.decl_printable_name (current_function_decl, 2);
861 	  aname = (IDENTIFIER_POINTER
862 		   (DECL_ASSEMBLER_NAME (current_function_decl)));
863 	  fprintf (dump_file, "\n;; Function %s (%s)%s\n\n", dname, aname,
864 	     cfun->function_frequency == FUNCTION_FREQUENCY_HOT
865 	     ? " (hot)"
866 	     : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
867 	     ? " (unlikely executed)"
868 	     : "");
869 	}
870     }
871   else
872     initializing_dump = false;
873 
874   /* If a timevar is present, start it.  */
875   if (pass->tv_id)
876     timevar_push (pass->tv_id);
877 
878   /* Do it!  */
879   if (pass->execute)
880     {
881       todo_after = pass->execute ();
882       last_verified = 0;
883     }
884 
885   /* Stop timevar.  */
886   if (pass->tv_id)
887     timevar_pop (pass->tv_id);
888 
889   curr_properties = (curr_properties | pass->properties_provided)
890 		    & ~pass->properties_destroyed;
891 
892   if (initializing_dump
893       && dump_file
894       && graph_dump_format != no_graph
895       && (curr_properties & (PROP_cfg | PROP_rtl)) == (PROP_cfg | PROP_rtl))
896     {
897       get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
898       dump_flags |= TDF_GRAPH;
899       clean_graph_dump_file (dump_file_name);
900     }
901 
902   /* Run post-pass cleanup and verification.  */
903   execute_todo (todo_after | pass->todo_flags_finish);
904   verify_interpass_invariants ();
905 
906   /* Flush and close dump file.  */
907   if (dump_file_name)
908     {
909       free ((char *) dump_file_name);
910       dump_file_name = NULL;
911     }
912   if (dump_file)
913     {
914       dump_end (pass->static_pass_number, dump_file);
915       dump_file = NULL;
916     }
917 
918   if (pass->properties_destroyed & PROP_smt_usage)
919     updating_used_alone = false;
920 
921   /* Reset in_gimple_form to not break non-unit-at-a-time mode.  */
922   in_gimple_form = false;
923 
924   return true;
925 }
926 
927 void
execute_pass_list(struct tree_opt_pass * pass)928 execute_pass_list (struct tree_opt_pass *pass)
929 {
930   do
931     {
932       if (execute_one_pass (pass) && pass->sub)
933         execute_pass_list (pass->sub);
934       pass = pass->next;
935     }
936   while (pass);
937 }
938 
939 /* Same as execute_pass_list but assume that subpasses of IPA passes
940    are local passes.  */
941 void
execute_ipa_pass_list(struct tree_opt_pass * pass)942 execute_ipa_pass_list (struct tree_opt_pass *pass)
943 {
944   do
945     {
946       if (execute_one_pass (pass) && pass->sub)
947 	{
948 	  struct cgraph_node *node;
949 	  for (node = cgraph_nodes; node; node = node->next)
950 	    if (node->analyzed)
951 	      {
952 		push_cfun (DECL_STRUCT_FUNCTION (node->decl));
953 		current_function_decl = node->decl;
954 		execute_pass_list (pass->sub);
955 		free_dominance_info (CDI_DOMINATORS);
956 		free_dominance_info (CDI_POST_DOMINATORS);
957 		current_function_decl = NULL;
958 		pop_cfun ();
959 		ggc_collect ();
960 	      }
961 	}
962       pass = pass->next;
963     }
964   while (pass);
965 }
966