OSDN Git Service

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