1169689Skan/* Top level of GCC compilers (cc1, cc1plus, etc.)
2169689Skan   Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3169689Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4169689Skan
5169689SkanThis file is part of GCC.
6169689Skan
7169689SkanGCC is free software; you can redistribute it and/or modify it under
8169689Skanthe terms of the GNU General Public License as published by the Free
9169689SkanSoftware Foundation; either version 2, or (at your option) any later
10169689Skanversion.
11169689Skan
12169689SkanGCC is distributed in the hope that it will be useful, but WITHOUT ANY
13169689SkanWARRANTY; without even the implied warranty of MERCHANTABILITY or
14169689SkanFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15169689Skanfor more details.
16169689Skan
17169689SkanYou should have received a copy of the GNU General Public License
18169689Skanalong with GCC; see the file COPYING.  If not, write to the Free
19169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20169689Skan02110-1301, USA.  */
21169689Skan
22169689Skan/* This is the top level of cc1/c++.
23169689Skan   It parses command args, opens files, invokes the various passes
24169689Skan   in the proper order, and counts the time used by each.
25169689Skan   Error messages and low-level interface to malloc also handled here.  */
26169689Skan
27169689Skan#include "config.h"
28169689Skan#undef FLOAT /* This is for hpux. They should change hpux.  */
29169689Skan#undef FFS  /* Some systems define this in param.h.  */
30169689Skan#include "system.h"
31169689Skan#include "coretypes.h"
32169689Skan#include "tm.h"
33169689Skan#include <signal.h>
34169689Skan
35169689Skan#ifdef HAVE_SYS_RESOURCE_H
36169689Skan# include <sys/resource.h>
37169689Skan#endif
38169689Skan
39169689Skan#ifdef HAVE_SYS_TIMES_H
40169689Skan# include <sys/times.h>
41169689Skan#endif
42169689Skan
43169689Skan#include "line-map.h"
44169689Skan#include "input.h"
45169689Skan#include "tree.h"
46169689Skan#include "rtl.h"
47169689Skan#include "tm_p.h"
48169689Skan#include "flags.h"
49169689Skan#include "insn-attr.h"
50169689Skan#include "insn-config.h"
51169689Skan#include "insn-flags.h"
52169689Skan#include "hard-reg-set.h"
53169689Skan#include "recog.h"
54169689Skan#include "output.h"
55169689Skan#include "except.h"
56169689Skan#include "function.h"
57169689Skan#include "toplev.h"
58169689Skan#include "expr.h"
59169689Skan#include "basic-block.h"
60169689Skan#include "intl.h"
61169689Skan#include "ggc.h"
62169689Skan#include "graph.h"
63169689Skan#include "regs.h"
64169689Skan#include "timevar.h"
65169689Skan#include "diagnostic.h"
66169689Skan#include "params.h"
67169689Skan#include "reload.h"
68169689Skan#include "dwarf2asm.h"
69169689Skan#include "integrate.h"
70169689Skan#include "real.h"
71169689Skan#include "debug.h"
72169689Skan#include "target.h"
73169689Skan#include "langhooks.h"
74169689Skan#include "cfglayout.h"
75169689Skan#include "cfgloop.h"
76169689Skan#include "hosthooks.h"
77169689Skan#include "cgraph.h"
78169689Skan#include "opts.h"
79169689Skan#include "coverage.h"
80169689Skan#include "value-prof.h"
81169689Skan#include "tree-inline.h"
82169689Skan#include "tree-flow.h"
83169689Skan#include "tree-pass.h"
84169689Skan#include "tree-dump.h"
85169689Skan
86169689Skan#if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
87169689Skan#include "dwarf2out.h"
88169689Skan#endif
89169689Skan
90169689Skan#if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
91169689Skan#include "dbxout.h"
92169689Skan#endif
93169689Skan
94169689Skan#ifdef SDB_DEBUGGING_INFO
95169689Skan#include "sdbout.h"
96169689Skan#endif
97169689Skan
98169689Skan#ifdef XCOFF_DEBUGGING_INFO
99169689Skan#include "xcoffout.h"		/* Needed for external data
100169689Skan				   declarations for e.g. AIX 4.x.  */
101169689Skan#endif
102169689Skan
103169689Skan/* Global variables used to communicate with passes.  */
104169689Skanint dump_flags;
105169689Skanbool in_gimple_form;
106169689Skan
107169689Skan
108169689Skan/* This is called from various places for FUNCTION_DECL, VAR_DECL,
109169689Skan   and TYPE_DECL nodes.
110169689Skan
111169689Skan   This does nothing for local (non-static) variables, unless the
112169689Skan   variable is a register variable with DECL_ASSEMBLER_NAME set.  In
113169689Skan   that case, or if the variable is not an automatic, it sets up the
114169689Skan   RTL and outputs any assembler code (label definition, storage
115169689Skan   allocation and initialization).
116169689Skan
117169689Skan   DECL is the declaration.  TOP_LEVEL is nonzero
118169689Skan   if this declaration is not within a function.  */
119169689Skan
120169689Skanvoid
121169689Skanrest_of_decl_compilation (tree decl,
122169689Skan			  int top_level,
123169689Skan			  int at_end)
124169689Skan{
125169689Skan  /* We deferred calling assemble_alias so that we could collect
126169689Skan     other attributes such as visibility.  Emit the alias now.  */
127169689Skan  {
128169689Skan    tree alias;
129169689Skan    alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
130169689Skan    if (alias)
131169689Skan      {
132169689Skan	alias = TREE_VALUE (TREE_VALUE (alias));
133169689Skan	alias = get_identifier (TREE_STRING_POINTER (alias));
134169689Skan	assemble_alias (decl, alias);
135169689Skan      }
136169689Skan  }
137169689Skan
138169689Skan  /* Can't defer this, because it needs to happen before any
139169689Skan     later function definitions are processed.  */
140169689Skan  if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
141169689Skan    make_decl_rtl (decl);
142169689Skan
143169689Skan  /* Forward declarations for nested functions are not "external",
144169689Skan     but we need to treat them as if they were.  */
145169689Skan  if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
146169689Skan      || TREE_CODE (decl) == FUNCTION_DECL)
147169689Skan    {
148169689Skan      timevar_push (TV_VARCONST);
149169689Skan
150169689Skan      /* Don't output anything when a tentative file-scope definition
151169689Skan	 is seen.  But at end of compilation, do output code for them.
152169689Skan
153169689Skan	 We do output all variables when unit-at-a-time is active and rely on
154169689Skan	 callgraph code to defer them except for forward declarations
155169689Skan	 (see gcc.c-torture/compile/920624-1.c) */
156169689Skan      if ((at_end
157169689Skan	   || !DECL_DEFER_OUTPUT (decl)
158169689Skan	   || DECL_INITIAL (decl))
159169689Skan	  && !DECL_EXTERNAL (decl))
160169689Skan	{
161169689Skan	  if (TREE_CODE (decl) != FUNCTION_DECL)
162169689Skan	    cgraph_varpool_finalize_decl (decl);
163169689Skan	  else
164169689Skan	    assemble_variable (decl, top_level, at_end, 0);
165169689Skan	}
166169689Skan
167169689Skan#ifdef ASM_FINISH_DECLARE_OBJECT
168169689Skan      if (decl == last_assemble_variable_decl)
169169689Skan	{
170169689Skan	  ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
171169689Skan				     top_level, at_end);
172169689Skan	}
173169689Skan#endif
174169689Skan
175169689Skan      timevar_pop (TV_VARCONST);
176169689Skan    }
177169689Skan  else if (TREE_CODE (decl) == TYPE_DECL
178169689Skan	   /* Like in rest_of_type_compilation, avoid confusing the debug
179169689Skan	      information machinery when there are errors.  */
180169689Skan	   && !(sorrycount || errorcount))
181169689Skan    {
182169689Skan      timevar_push (TV_SYMOUT);
183169689Skan      debug_hooks->type_decl (decl, !top_level);
184169689Skan      timevar_pop (TV_SYMOUT);
185169689Skan    }
186169689Skan
187169689Skan  /* Let cgraph know about the existence of variables.  */
188169689Skan  if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
189169689Skan    cgraph_varpool_node (decl);
190169689Skan}
191169689Skan
192169689Skan/* Called after finishing a record, union or enumeral type.  */
193169689Skan
194169689Skanvoid
195169689Skanrest_of_type_compilation (tree type, int toplev)
196169689Skan{
197169689Skan  /* Avoid confusing the debug information machinery when there are
198169689Skan     errors.  */
199169689Skan  if (errorcount != 0 || sorrycount != 0)
200169689Skan    return;
201169689Skan
202169689Skan  timevar_push (TV_SYMOUT);
203169689Skan  debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
204169689Skan  timevar_pop (TV_SYMOUT);
205169689Skan}
206169689Skan
207169689Skan
208169689Skan
209169689Skanvoid
210169689Skanfinish_optimization_passes (void)
211169689Skan{
212169689Skan  enum tree_dump_index i;
213169689Skan  struct dump_file_info *dfi;
214169689Skan  char *name;
215169689Skan
216169689Skan  timevar_push (TV_DUMP);
217169689Skan  if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
218169689Skan    {
219169689Skan      dump_file = dump_begin (pass_profile.static_pass_number, NULL);
220169689Skan      end_branch_prob ();
221169689Skan      if (dump_file)
222169689Skan	dump_end (pass_profile.static_pass_number, dump_file);
223169689Skan    }
224169689Skan
225169689Skan  if (optimize > 0)
226169689Skan    {
227169689Skan      dump_file = dump_begin (pass_combine.static_pass_number, NULL);
228169689Skan      if (dump_file)
229169689Skan	{
230169689Skan	  dump_combine_total_stats (dump_file);
231169689Skan          dump_end (pass_combine.static_pass_number, dump_file);
232169689Skan	}
233169689Skan    }
234169689Skan
235169689Skan  /* Do whatever is necessary to finish printing the graphs.  */
236169689Skan  if (graph_dump_format != no_graph)
237169689Skan    for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
238169689Skan      if (dump_initialized_p (i)
239169689Skan	  && (dfi->flags & TDF_GRAPH) != 0
240169689Skan	  && (name = get_dump_file_name (i)) != NULL)
241169689Skan	{
242169689Skan	  finish_graph_dump_file (name);
243169689Skan	  free (name);
244169689Skan	}
245169689Skan
246169689Skan  timevar_pop (TV_DUMP);
247169689Skan}
248169689Skan
249169689Skanstatic bool
250169689Skangate_rest_of_compilation (void)
251169689Skan{
252169689Skan  /* Early return if there were errors.  We can run afoul of our
253169689Skan     consistency checks, and there's not really much point in fixing them.  */
254169689Skan  return !(rtl_dump_and_exit || flag_syntax_only || errorcount || sorrycount);
255169689Skan}
256169689Skan
257169689Skanstruct tree_opt_pass pass_rest_of_compilation =
258169689Skan{
259169689Skan  NULL,                                 /* name */
260169689Skan  gate_rest_of_compilation,             /* gate */
261169689Skan  NULL,                                 /* execute */
262169689Skan  NULL,                                 /* sub */
263169689Skan  NULL,                                 /* next */
264169689Skan  0,                                    /* static_pass_number */
265169689Skan  TV_REST_OF_COMPILATION,               /* tv_id */
266169689Skan  PROP_rtl,                             /* properties_required */
267169689Skan  0,                                    /* properties_provided */
268169689Skan  0,                                    /* properties_destroyed */
269169689Skan  0,                                    /* todo_flags_start */
270169689Skan  TODO_ggc_collect,                     /* todo_flags_finish */
271169689Skan  0                                     /* letter */
272169689Skan};
273169689Skan
274169689Skanstatic bool
275169689Skangate_postreload (void)
276169689Skan{
277169689Skan  return reload_completed;
278169689Skan}
279169689Skan
280169689Skanstruct tree_opt_pass pass_postreload =
281169689Skan{
282169689Skan  NULL,                                 /* name */
283169689Skan  gate_postreload,                      /* gate */
284169689Skan  NULL,                                 /* execute */
285169689Skan  NULL,                                 /* sub */
286169689Skan  NULL,                                 /* next */
287169689Skan  0,                                    /* static_pass_number */
288169689Skan  0,                                    /* tv_id */
289169689Skan  PROP_rtl,                             /* properties_required */
290169689Skan  0,                                    /* properties_provided */
291169689Skan  0,                                    /* properties_destroyed */
292169689Skan  0,                                    /* todo_flags_start */
293169689Skan  TODO_ggc_collect,                     /* todo_flags_finish */
294169689Skan  0					/* letter */
295169689Skan};
296169689Skan
297169689Skan
298169689Skan
299169689Skan/* The root of the compilation pass tree, once constructed.  */
300169689Skanstruct tree_opt_pass *all_passes, *all_ipa_passes, *all_lowering_passes;
301169689Skan
302169689Skan/* Iterate over the pass tree allocating dump file numbers.  We want
303169689Skan   to do this depth first, and independent of whether the pass is
304169689Skan   enabled or not.  */
305169689Skan
306169689Skanstatic void
307169689Skanregister_one_dump_file (struct tree_opt_pass *pass, bool ipa, int properties)
308169689Skan{
309169689Skan  char *dot_name, *flag_name, *glob_name;
310169689Skan  const char *prefix;
311169689Skan  char num[10];
312169689Skan  int flags;
313169689Skan
314169689Skan  /* See below in next_pass_1.  */
315169689Skan  num[0] = '\0';
316169689Skan  if (pass->static_pass_number != -1)
317169689Skan    sprintf (num, "%d", ((int) pass->static_pass_number < 0
318169689Skan			 ? 1 : pass->static_pass_number));
319169689Skan
320169689Skan  dot_name = concat (".", pass->name, num, NULL);
321169689Skan  if (ipa)
322169689Skan    prefix = "ipa-", flags = TDF_IPA;
323169689Skan  else if (properties & PROP_trees)
324169689Skan    prefix = "tree-", flags = TDF_TREE;
325169689Skan  else
326169689Skan    prefix = "rtl-", flags = TDF_RTL;
327169689Skan
328169689Skan  flag_name = concat (prefix, pass->name, num, NULL);
329169689Skan  glob_name = concat (prefix, pass->name, NULL);
330169689Skan  pass->static_pass_number = dump_register (dot_name, flag_name, glob_name,
331169689Skan                                            flags, pass->letter);
332169689Skan}
333169689Skan
334169689Skan/* Recursive worker function for register_dump_files.  */
335169689Skan
336169689Skanstatic int
337169689Skanregister_dump_files_1 (struct tree_opt_pass *pass, bool ipa, int properties)
338169689Skan{
339169689Skan  do
340169689Skan    {
341169689Skan      int new_properties = (properties | pass->properties_provided)
342169689Skan			   & ~pass->properties_destroyed;
343169689Skan
344169689Skan      if (pass->name)
345169689Skan        register_one_dump_file (pass, ipa, new_properties);
346169689Skan
347169689Skan      if (pass->sub)
348169689Skan        new_properties = register_dump_files_1 (pass->sub, false,
349169689Skan						new_properties);
350169689Skan
351169689Skan      /* If we have a gate, combine the properties that we could have with
352169689Skan         and without the pass being examined.  */
353169689Skan      if (pass->gate)
354169689Skan        properties &= new_properties;
355169689Skan      else
356169689Skan        properties = new_properties;
357169689Skan
358169689Skan      pass = pass->next;
359169689Skan    }
360169689Skan  while (pass);
361169689Skan
362169689Skan  return properties;
363169689Skan}
364169689Skan
365169689Skan/* Register the dump files for the pipeline starting at PASS.  IPA is
366169689Skan   true if the pass is inter-procedural, and PROPERTIES reflects the
367169689Skan   properties that are guaranteed to be available at the beginning of
368169689Skan   the pipeline.  */
369169689Skan
370169689Skanstatic void
371169689Skanregister_dump_files (struct tree_opt_pass *pass, bool ipa, int properties)
372169689Skan{
373169689Skan  pass->properties_required |= properties;
374169689Skan  pass->todo_flags_start |= TODO_set_props;
375169689Skan  register_dump_files_1 (pass, ipa, properties);
376169689Skan}
377169689Skan
378169689Skan/* Add a pass to the pass list. Duplicate the pass if it's already
379169689Skan   in the list.  */
380169689Skan
381169689Skanstatic struct tree_opt_pass **
382169689Skannext_pass_1 (struct tree_opt_pass **list, struct tree_opt_pass *pass)
383169689Skan{
384169689Skan  /* A nonzero static_pass_number indicates that the
385169689Skan     pass is already in the list.  */
386169689Skan  if (pass->static_pass_number)
387169689Skan    {
388169689Skan      struct tree_opt_pass *new;
389169689Skan
390169689Skan      new = xmalloc (sizeof (*new));
391169689Skan      memcpy (new, pass, sizeof (*new));
392169689Skan
393169689Skan      /* Indicate to register_dump_files that this pass has duplicates,
394169689Skan         and so it should rename the dump file.  The first instance will
395169689Skan         be -1, and be number of duplicates = -static_pass_number - 1.
396169689Skan         Subsequent instances will be > 0 and just the duplicate number.  */
397169689Skan      if (pass->name)
398169689Skan        {
399169689Skan          pass->static_pass_number -= 1;
400169689Skan          new->static_pass_number = -pass->static_pass_number;
401169689Skan	}
402169689Skan
403169689Skan      *list = new;
404169689Skan    }
405169689Skan  else
406169689Skan    {
407169689Skan      pass->static_pass_number = -1;
408169689Skan      *list = pass;
409169689Skan    }
410169689Skan
411169689Skan  return &(*list)->next;
412169689Skan
413169689Skan}
414169689Skan
415169689Skan/* Construct the pass tree.  The sequencing of passes is driven by
416169689Skan   the cgraph routines:
417169689Skan
418169689Skan   cgraph_finalize_compilation_unit ()
419169689Skan       for each node N in the cgraph
420169689Skan	   cgraph_analyze_function (N)
421169689Skan	       cgraph_lower_function (N) -> all_lowering_passes
422169689Skan
423169689Skan   If we are optimizing, cgraph_optimize is then invoked:
424169689Skan
425169689Skan   cgraph_optimize ()
426169689Skan       ipa_passes () 			-> all_ipa_passes
427169689Skan       cgraph_expand_all_functions ()
428169689Skan           for each node N in the cgraph
429169689Skan	       cgraph_expand_function (N)
430169689Skan	           cgraph_lower_function (N)	-> Now a NOP.
431169689Skan		   lang_hooks.callgraph.expand_function (DECL (N))
432169689Skan		   	tree_rest_of_compilation (DECL (N))  -> all_passes
433169689Skan*/
434169689Skan
435169689Skanvoid
436169689Skaninit_optimization_passes (void)
437169689Skan{
438169689Skan  struct tree_opt_pass **p;
439169689Skan
440169689Skan#define NEXT_PASS(PASS)  (p = next_pass_1 (p, &PASS))
441169689Skan  /* Interprocedural optimization passes.  */
442169689Skan  p = &all_ipa_passes;
443169689Skan  NEXT_PASS (pass_early_ipa_inline);
444169689Skan  NEXT_PASS (pass_early_local_passes);
445169689Skan  NEXT_PASS (pass_ipa_cp);
446169689Skan  NEXT_PASS (pass_ipa_inline);
447169689Skan  NEXT_PASS (pass_ipa_reference);
448169689Skan  NEXT_PASS (pass_ipa_pure_const);
449169689Skan  NEXT_PASS (pass_ipa_type_escape);
450169689Skan  NEXT_PASS (pass_ipa_pta);
451169689Skan  *p = NULL;
452169689Skan
453169689Skan  /* All passes needed to lower the function into shape optimizers can
454169689Skan     operate on.  */
455169689Skan  p = &all_lowering_passes;
456169689Skan  NEXT_PASS (pass_remove_useless_stmts);
457169689Skan  NEXT_PASS (pass_mudflap_1);
458169689Skan  NEXT_PASS (pass_lower_omp);
459169689Skan  NEXT_PASS (pass_lower_cf);
460169689Skan  NEXT_PASS (pass_lower_eh);
461169689Skan  NEXT_PASS (pass_build_cfg);
462169689Skan  NEXT_PASS (pass_lower_complex_O0);
463169689Skan  NEXT_PASS (pass_lower_vector);
464169689Skan  NEXT_PASS (pass_warn_function_return);
465169689Skan  NEXT_PASS (pass_early_tree_profile);
466169689Skan  *p = NULL;
467169689Skan
468169689Skan  p = &pass_early_local_passes.sub;
469169689Skan  NEXT_PASS (pass_tree_profile);
470169689Skan  NEXT_PASS (pass_cleanup_cfg);
471169689Skan  NEXT_PASS (pass_rebuild_cgraph_edges);
472169689Skan  *p = NULL;
473169689Skan
474169689Skan  p = &all_passes;
475169689Skan  NEXT_PASS (pass_fixup_cfg);
476169689Skan  NEXT_PASS (pass_init_datastructures);
477169689Skan  NEXT_PASS (pass_expand_omp);
478169689Skan  NEXT_PASS (pass_all_optimizations);
479169689Skan  NEXT_PASS (pass_warn_function_noreturn);
480169689Skan  NEXT_PASS (pass_mudflap_2);
481169689Skan  NEXT_PASS (pass_free_datastructures);
482169689Skan  NEXT_PASS (pass_free_cfg_annotations);
483169689Skan  NEXT_PASS (pass_expand);
484169689Skan  NEXT_PASS (pass_rest_of_compilation);
485169689Skan  NEXT_PASS (pass_clean_state);
486169689Skan  *p = NULL;
487169689Skan
488169689Skan  p = &pass_all_optimizations.sub;
489169689Skan  NEXT_PASS (pass_referenced_vars);
490169689Skan  NEXT_PASS (pass_reset_cc_flags);
491169689Skan  NEXT_PASS (pass_create_structure_vars);
492169689Skan  NEXT_PASS (pass_build_ssa);
493169689Skan  NEXT_PASS (pass_may_alias);
494169689Skan  NEXT_PASS (pass_return_slot);
495169689Skan  NEXT_PASS (pass_rename_ssa_copies);
496169689Skan  NEXT_PASS (pass_early_warn_uninitialized);
497169689Skan
498169689Skan  /* Initial scalar cleanups.  */
499169689Skan  NEXT_PASS (pass_ccp);
500169689Skan  NEXT_PASS (pass_fre);
501169689Skan  NEXT_PASS (pass_dce);
502169689Skan  NEXT_PASS (pass_forwprop);
503169689Skan  NEXT_PASS (pass_copy_prop);
504169689Skan  NEXT_PASS (pass_merge_phi);
505169689Skan  NEXT_PASS (pass_vrp);
506169689Skan  NEXT_PASS (pass_dce);
507169689Skan  NEXT_PASS (pass_dominator);
508169689Skan
509169689Skan  /* The only const/copy propagation opportunities left after
510169689Skan     DOM should be due to degenerate PHI nodes.  So rather than
511169689Skan     run the full propagators, run a specialized pass which
512169689Skan     only examines PHIs to discover const/copy propagation
513169689Skan     opportunities.  */
514169689Skan  NEXT_PASS (pass_phi_only_cprop);
515169689Skan
516169689Skan  NEXT_PASS (pass_phiopt);
517169689Skan  NEXT_PASS (pass_may_alias);
518169689Skan  NEXT_PASS (pass_tail_recursion);
519169689Skan  NEXT_PASS (pass_profile);
520169689Skan  NEXT_PASS (pass_ch);
521169689Skan  NEXT_PASS (pass_stdarg);
522169689Skan  NEXT_PASS (pass_lower_complex);
523169689Skan  NEXT_PASS (pass_sra);
524169689Skan  /* FIXME: SRA may generate arbitrary gimple code, exposing new
525169689Skan     aliased and call-clobbered variables.  As mentioned below,
526169689Skan     pass_may_alias should be a TODO item.  */
527169689Skan  NEXT_PASS (pass_may_alias);
528169689Skan  NEXT_PASS (pass_rename_ssa_copies);
529169689Skan  NEXT_PASS (pass_dominator);
530169689Skan
531169689Skan  /* The only const/copy propagation opportunities left after
532169689Skan     DOM should be due to degenerate PHI nodes.  So rather than
533169689Skan     run the full propagators, run a specialized pass which
534169689Skan     only examines PHIs to discover const/copy propagation
535169689Skan     opportunities.  */
536169689Skan  NEXT_PASS (pass_phi_only_cprop);
537169689Skan
538169689Skan  NEXT_PASS (pass_reassoc);
539169689Skan  NEXT_PASS (pass_dce);
540169689Skan  NEXT_PASS (pass_dse);
541169689Skan  NEXT_PASS (pass_may_alias);
542169689Skan  NEXT_PASS (pass_forwprop);
543169689Skan  NEXT_PASS (pass_phiopt);
544169689Skan  NEXT_PASS (pass_object_sizes);
545169689Skan  NEXT_PASS (pass_store_ccp);
546169689Skan  NEXT_PASS (pass_store_copy_prop);
547169689Skan  NEXT_PASS (pass_fold_builtins);
548169689Skan  /* FIXME: May alias should a TODO but for 4.0.0,
549169689Skan     we add may_alias right after fold builtins
550169689Skan     which can create arbitrary GIMPLE.  */
551169689Skan  NEXT_PASS (pass_may_alias);
552169689Skan  NEXT_PASS (pass_split_crit_edges);
553169689Skan  NEXT_PASS (pass_pre);
554169689Skan  NEXT_PASS (pass_may_alias);
555169689Skan  NEXT_PASS (pass_sink_code);
556169689Skan  NEXT_PASS (pass_tree_loop);
557169689Skan  NEXT_PASS (pass_cse_reciprocals);
558169689Skan  NEXT_PASS (pass_reassoc);
559169689Skan  NEXT_PASS (pass_vrp);
560169689Skan  NEXT_PASS (pass_dominator);
561169689Skan
562169689Skan  /* The only const/copy propagation opportunities left after
563169689Skan     DOM should be due to degenerate PHI nodes.  So rather than
564169689Skan     run the full propagators, run a specialized pass which
565169689Skan     only examines PHIs to discover const/copy propagation
566169689Skan     opportunities.  */
567169689Skan  NEXT_PASS (pass_phi_only_cprop);
568169689Skan
569169689Skan  NEXT_PASS (pass_cd_dce);
570169689Skan
571169689Skan  /* FIXME: If DCE is not run before checking for uninitialized uses,
572169689Skan     we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
573169689Skan     However, this also causes us to misdiagnose cases that should be
574169689Skan     real warnings (e.g., testsuite/gcc.dg/pr18501.c).
575169689Skan
576169689Skan     To fix the false positives in uninit-5.c, we would have to
577169689Skan     account for the predicates protecting the set and the use of each
578169689Skan     variable.  Using a representation like Gated Single Assignment
579169689Skan     may help.  */
580169689Skan  NEXT_PASS (pass_late_warn_uninitialized);
581169689Skan  NEXT_PASS (pass_dse);
582169689Skan  NEXT_PASS (pass_forwprop);
583169689Skan  NEXT_PASS (pass_phiopt);
584169689Skan  NEXT_PASS (pass_tail_calls);
585169689Skan  NEXT_PASS (pass_rename_ssa_copies);
586169689Skan  NEXT_PASS (pass_uncprop);
587169689Skan  NEXT_PASS (pass_del_ssa);
588169689Skan  NEXT_PASS (pass_nrv);
589169689Skan  NEXT_PASS (pass_mark_used_blocks);
590169689Skan  NEXT_PASS (pass_cleanup_cfg_post_optimizing);
591169689Skan  *p = NULL;
592169689Skan
593169689Skan  p = &pass_tree_loop.sub;
594169689Skan  NEXT_PASS (pass_tree_loop_init);
595169689Skan  NEXT_PASS (pass_copy_prop);
596169689Skan  NEXT_PASS (pass_lim);
597169689Skan  NEXT_PASS (pass_tree_unswitch);
598169689Skan  NEXT_PASS (pass_scev_cprop);
599169689Skan  NEXT_PASS (pass_empty_loop);
600169689Skan  NEXT_PASS (pass_record_bounds);
601169689Skan  NEXT_PASS (pass_linear_transform);
602169689Skan  NEXT_PASS (pass_iv_canon);
603169689Skan  NEXT_PASS (pass_if_conversion);
604169689Skan  NEXT_PASS (pass_vectorize);
605169689Skan  /* NEXT_PASS (pass_may_alias) cannot be done again because the
606169689Skan     vectorizer creates alias relations that are not supported by
607169689Skan     pass_may_alias.  */
608169689Skan  NEXT_PASS (pass_complete_unroll);
609169689Skan  NEXT_PASS (pass_loop_prefetch);
610169689Skan  NEXT_PASS (pass_iv_optimize);
611169689Skan  NEXT_PASS (pass_tree_loop_done);
612169689Skan  *p = NULL;
613169689Skan
614169689Skan  p = &pass_vectorize.sub;
615169689Skan  NEXT_PASS (pass_lower_vector_ssa);
616169689Skan  NEXT_PASS (pass_dce_loop);
617169689Skan  *p = NULL;
618169689Skan
619169689Skan  p = &pass_loop2.sub;
620169689Skan  NEXT_PASS (pass_rtl_loop_init);
621169689Skan  NEXT_PASS (pass_rtl_move_loop_invariants);
622169689Skan  NEXT_PASS (pass_rtl_unswitch);
623169689Skan  NEXT_PASS (pass_rtl_unroll_and_peel_loops);
624169689Skan  NEXT_PASS (pass_rtl_doloop);
625169689Skan  NEXT_PASS (pass_rtl_loop_done);
626169689Skan  *p = NULL;
627169689Skan
628169689Skan  p = &pass_rest_of_compilation.sub;
629169689Skan  NEXT_PASS (pass_init_function);
630169689Skan  NEXT_PASS (pass_jump);
631169689Skan  NEXT_PASS (pass_insn_locators_initialize);
632169689Skan  NEXT_PASS (pass_rtl_eh);
633169689Skan  NEXT_PASS (pass_initial_value_sets);
634169689Skan  NEXT_PASS (pass_unshare_all_rtl);
635169689Skan  NEXT_PASS (pass_instantiate_virtual_regs);
636169689Skan  NEXT_PASS (pass_jump2);
637169689Skan  NEXT_PASS (pass_cse);
638169689Skan  NEXT_PASS (pass_gcse);
639169689Skan  NEXT_PASS (pass_jump_bypass);
640169689Skan  NEXT_PASS (pass_rtl_ifcvt);
641169689Skan  NEXT_PASS (pass_tracer);
642169689Skan  /* Perform loop optimizations.  It might be better to do them a bit
643169689Skan     sooner, but we want the profile feedback to work more
644169689Skan     efficiently.  */
645169689Skan  NEXT_PASS (pass_loop2);
646169689Skan  NEXT_PASS (pass_web);
647169689Skan  NEXT_PASS (pass_cse2);
648169689Skan  NEXT_PASS (pass_life);
649169689Skan  NEXT_PASS (pass_combine);
650169689Skan  NEXT_PASS (pass_if_after_combine);
651169689Skan  NEXT_PASS (pass_partition_blocks);
652169689Skan  NEXT_PASS (pass_regmove);
653169689Skan  NEXT_PASS (pass_split_all_insns);
654169689Skan  NEXT_PASS (pass_mode_switching);
655169689Skan  NEXT_PASS (pass_see);
656169689Skan  NEXT_PASS (pass_recompute_reg_usage);
657169689Skan  NEXT_PASS (pass_sms);
658169689Skan  NEXT_PASS (pass_sched);
659169689Skan  NEXT_PASS (pass_local_alloc);
660169689Skan  NEXT_PASS (pass_global_alloc);
661169689Skan  NEXT_PASS (pass_postreload);
662169689Skan  *p = NULL;
663169689Skan
664169689Skan  p = &pass_postreload.sub;
665169689Skan  NEXT_PASS (pass_postreload_cse);
666169689Skan  NEXT_PASS (pass_gcse2);
667169689Skan  NEXT_PASS (pass_flow2);
668169689Skan  NEXT_PASS (pass_rtl_seqabstr);
669169689Skan  NEXT_PASS (pass_stack_adjustments);
670169689Skan  NEXT_PASS (pass_peephole2);
671169689Skan  NEXT_PASS (pass_if_after_reload);
672169689Skan  NEXT_PASS (pass_regrename);
673169689Skan  NEXT_PASS (pass_reorder_blocks);
674169689Skan  NEXT_PASS (pass_branch_target_load_optimize);
675169689Skan  NEXT_PASS (pass_leaf_regs);
676169689Skan  NEXT_PASS (pass_sched2);
677169689Skan  NEXT_PASS (pass_split_before_regstack);
678169689Skan  NEXT_PASS (pass_stack_regs);
679169689Skan  NEXT_PASS (pass_compute_alignments);
680169689Skan  NEXT_PASS (pass_duplicate_computed_gotos);
681169689Skan  NEXT_PASS (pass_variable_tracking);
682169689Skan  NEXT_PASS (pass_free_cfg);
683169689Skan  NEXT_PASS (pass_machine_reorg);
684169689Skan  NEXT_PASS (pass_purge_lineno_notes);
685169689Skan  NEXT_PASS (pass_cleanup_barriers);
686169689Skan  NEXT_PASS (pass_delay_slots);
687169689Skan  NEXT_PASS (pass_split_for_shorten_branches);
688169689Skan  NEXT_PASS (pass_convert_to_eh_region_ranges);
689169689Skan  NEXT_PASS (pass_shorten_branches);
690169689Skan  NEXT_PASS (pass_set_nothrow_function_flags);
691169689Skan  NEXT_PASS (pass_final);
692169689Skan  *p = NULL;
693169689Skan
694169689Skan#undef NEXT_PASS
695169689Skan
696169689Skan  /* Register the passes with the tree dump code.  */
697169689Skan  register_dump_files (all_ipa_passes, true,
698169689Skan		       PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
699169689Skan		       | PROP_cfg);
700169689Skan  register_dump_files (all_lowering_passes, false, PROP_gimple_any);
701169689Skan  register_dump_files (all_passes, false,
702169689Skan		       PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
703169689Skan		       | PROP_cfg);
704169689Skan}
705169689Skan
706169689Skanstatic unsigned int last_verified;
707169689Skanstatic unsigned int curr_properties;
708169689Skan
709169689Skanstatic void
710169689Skanexecute_todo (unsigned int flags)
711169689Skan{
712169689Skan#if defined ENABLE_CHECKING
713169689Skan  if (need_ssa_update_p ())
714169689Skan    gcc_assert (flags & TODO_update_ssa_any);
715169689Skan#endif
716169689Skan
717169689Skan  if (curr_properties & PROP_ssa)
718169689Skan    flags |= TODO_verify_ssa;
719169689Skan  flags &= ~last_verified;
720169689Skan  if (!flags)
721169689Skan    return;
722169689Skan
723169689Skan  /* Always recalculate SMT usage before doing anything else.  */
724169689Skan  if (flags & TODO_update_smt_usage)
725169689Skan    recalculate_used_alone ();
726169689Skan
727169689Skan  /* Always cleanup the CFG before trying to update SSA .  */
728169689Skan  if (flags & TODO_cleanup_cfg)
729169689Skan    {
730169689Skan      /* CFG Cleanup can cause a constant to prop into an ARRAY_REF.  */
731169689Skan      updating_used_alone = true;
732169689Skan
733169689Skan      if (current_loops)
734169689Skan	cleanup_tree_cfg_loop ();
735169689Skan      else
736169689Skan	cleanup_tree_cfg ();
737169689Skan
738169689Skan      /* Update the used alone after cleanup cfg.  */
739169689Skan      recalculate_used_alone ();
740169689Skan
741169689Skan      /* When cleanup_tree_cfg merges consecutive blocks, it may
742169689Skan	 perform some simplistic propagation when removing single
743169689Skan	 valued PHI nodes.  This propagation may, in turn, cause the
744169689Skan	 SSA form to become out-of-date (see PR 22037).  So, even
745169689Skan	 if the parent pass had not scheduled an SSA update, we may
746169689Skan	 still need to do one.  */
747169689Skan      if (!(flags & TODO_update_ssa_any) && need_ssa_update_p ())
748169689Skan	flags |= TODO_update_ssa;
749169689Skan    }
750169689Skan
751169689Skan  if (flags & TODO_update_ssa_any)
752169689Skan    {
753169689Skan      unsigned update_flags = flags & TODO_update_ssa_any;
754169689Skan      update_ssa (update_flags);
755169689Skan      last_verified &= ~TODO_verify_ssa;
756169689Skan    }
757169689Skan
758169689Skan  if (flags & TODO_remove_unused_locals)
759169689Skan    remove_unused_locals ();
760169689Skan
761169689Skan  if ((flags & TODO_dump_func)
762169689Skan      && dump_file && current_function_decl)
763169689Skan    {
764169689Skan      if (curr_properties & PROP_trees)
765169689Skan        dump_function_to_file (current_function_decl,
766169689Skan                               dump_file, dump_flags);
767169689Skan      else
768169689Skan	{
769169689Skan	  if (dump_flags & TDF_SLIM)
770169689Skan	    print_rtl_slim_with_bb (dump_file, get_insns (), dump_flags);
771169689Skan	  else if ((curr_properties & PROP_cfg) && (dump_flags & TDF_BLOCKS))
772169689Skan	    print_rtl_with_bb (dump_file, get_insns ());
773169689Skan          else
774169689Skan	    print_rtl (dump_file, get_insns ());
775169689Skan
776169689Skan	  if (curr_properties & PROP_cfg
777169689Skan	      && graph_dump_format != no_graph
778169689Skan	      && (dump_flags & TDF_GRAPH))
779169689Skan	    print_rtl_graph_with_bb (dump_file_name, get_insns ());
780169689Skan	}
781169689Skan
782169689Skan      /* Flush the file.  If verification fails, we won't be able to
783169689Skan	 close the file before aborting.  */
784169689Skan      fflush (dump_file);
785169689Skan    }
786169689Skan  if ((flags & TODO_dump_cgraph)
787169689Skan      && dump_file && !current_function_decl)
788169689Skan    {
789169689Skan      dump_cgraph (dump_file);
790169689Skan      /* Flush the file.  If verification fails, we won't be able to
791169689Skan	 close the file before aborting.  */
792169689Skan      fflush (dump_file);
793169689Skan    }
794169689Skan
795169689Skan  if (flags & TODO_ggc_collect)
796169689Skan    {
797169689Skan      ggc_collect ();
798169689Skan    }
799169689Skan
800169689Skan#if defined ENABLE_CHECKING
801169689Skan  if (flags & TODO_verify_ssa)
802169689Skan    verify_ssa (true);
803169689Skan  if (flags & TODO_verify_flow)
804169689Skan    verify_flow_info ();
805169689Skan  if (flags & TODO_verify_stmts)
806169689Skan    verify_stmts ();
807169689Skan  if (flags & TODO_verify_loops)
808169689Skan    verify_loop_closed_ssa ();
809169689Skan#endif
810169689Skan
811169689Skan  last_verified = flags & TODO_verify_all;
812169689Skan}
813169689Skan
814169689Skan/* Verify invariants that should hold between passes.  This is a place
815169689Skan   to put simple sanity checks.  */
816169689Skan
817169689Skanstatic void
818169689Skanverify_interpass_invariants (void)
819169689Skan{
820169689Skan#ifdef ENABLE_CHECKING
821169689Skan  gcc_assert (!fold_deferring_overflow_warnings_p ());
822169689Skan#endif
823169689Skan}
824169689Skan
825169689Skanstatic bool
826169689Skanexecute_one_pass (struct tree_opt_pass *pass)
827169689Skan{
828169689Skan  bool initializing_dump;
829169689Skan  unsigned int todo_after = 0;
830169689Skan
831169689Skan  /* See if we're supposed to run this pass.  */
832169689Skan  if (pass->gate && !pass->gate ())
833169689Skan    return false;
834169689Skan
835169689Skan  if (pass->todo_flags_start & TODO_set_props)
836169689Skan    curr_properties = pass->properties_required;
837169689Skan
838169689Skan  /* Note that the folders should only create gimple expressions.
839169689Skan     This is a hack until the new folder is ready.  */
840169689Skan  in_gimple_form = (curr_properties & PROP_trees) != 0;
841169689Skan
842169689Skan  /* Run pre-pass verification.  */
843169689Skan  execute_todo (pass->todo_flags_start);
844169689Skan
845169689Skan  gcc_assert ((curr_properties & pass->properties_required)
846169689Skan	      == pass->properties_required);
847169689Skan
848169689Skan  if (pass->properties_destroyed & PROP_smt_usage)
849169689Skan    updating_used_alone = true;
850169689Skan
851169689Skan  /* If a dump file name is present, open it if enabled.  */
852169689Skan  if (pass->static_pass_number != -1)
853169689Skan    {
854169689Skan      initializing_dump = !dump_initialized_p (pass->static_pass_number);
855169689Skan      dump_file_name = get_dump_file_name (pass->static_pass_number);
856169689Skan      dump_file = dump_begin (pass->static_pass_number, &dump_flags);
857169689Skan      if (dump_file && current_function_decl)
858169689Skan	{
859169689Skan	  const char *dname, *aname;
860169689Skan	  dname = lang_hooks.decl_printable_name (current_function_decl, 2);
861169689Skan	  aname = (IDENTIFIER_POINTER
862169689Skan		   (DECL_ASSEMBLER_NAME (current_function_decl)));
863169689Skan	  fprintf (dump_file, "\n;; Function %s (%s)%s\n\n", dname, aname,
864169689Skan	     cfun->function_frequency == FUNCTION_FREQUENCY_HOT
865169689Skan	     ? " (hot)"
866169689Skan	     : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
867169689Skan	     ? " (unlikely executed)"
868169689Skan	     : "");
869169689Skan	}
870169689Skan    }
871169689Skan  else
872169689Skan    initializing_dump = false;
873169689Skan
874169689Skan  /* If a timevar is present, start it.  */
875169689Skan  if (pass->tv_id)
876169689Skan    timevar_push (pass->tv_id);
877169689Skan
878169689Skan  /* Do it!  */
879169689Skan  if (pass->execute)
880169689Skan    {
881169689Skan      todo_after = pass->execute ();
882169689Skan      last_verified = 0;
883169689Skan    }
884169689Skan
885169689Skan  /* Stop timevar.  */
886169689Skan  if (pass->tv_id)
887169689Skan    timevar_pop (pass->tv_id);
888169689Skan
889169689Skan  curr_properties = (curr_properties | pass->properties_provided)
890169689Skan		    & ~pass->properties_destroyed;
891169689Skan
892169689Skan  if (initializing_dump
893169689Skan      && dump_file
894169689Skan      && graph_dump_format != no_graph
895169689Skan      && (curr_properties & (PROP_cfg | PROP_rtl)) == (PROP_cfg | PROP_rtl))
896169689Skan    {
897169689Skan      get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
898169689Skan      dump_flags |= TDF_GRAPH;
899169689Skan      clean_graph_dump_file (dump_file_name);
900169689Skan    }
901169689Skan
902169689Skan  /* Run post-pass cleanup and verification.  */
903169689Skan  execute_todo (todo_after | pass->todo_flags_finish);
904169689Skan  verify_interpass_invariants ();
905169689Skan
906169689Skan  /* Flush and close dump file.  */
907169689Skan  if (dump_file_name)
908169689Skan    {
909169689Skan      free ((char *) dump_file_name);
910169689Skan      dump_file_name = NULL;
911169689Skan    }
912169689Skan  if (dump_file)
913169689Skan    {
914169689Skan      dump_end (pass->static_pass_number, dump_file);
915169689Skan      dump_file = NULL;
916169689Skan    }
917169689Skan
918169689Skan  if (pass->properties_destroyed & PROP_smt_usage)
919169689Skan    updating_used_alone = false;
920169689Skan
921169689Skan  /* Reset in_gimple_form to not break non-unit-at-a-time mode.  */
922169689Skan  in_gimple_form = false;
923169689Skan
924169689Skan  return true;
925169689Skan}
926169689Skan
927169689Skanvoid
928169689Skanexecute_pass_list (struct tree_opt_pass *pass)
929169689Skan{
930169689Skan  do
931169689Skan    {
932169689Skan      if (execute_one_pass (pass) && pass->sub)
933169689Skan        execute_pass_list (pass->sub);
934169689Skan      pass = pass->next;
935169689Skan    }
936169689Skan  while (pass);
937169689Skan}
938169689Skan
939169689Skan/* Same as execute_pass_list but assume that subpasses of IPA passes
940169689Skan   are local passes.  */
941169689Skanvoid
942169689Skanexecute_ipa_pass_list (struct tree_opt_pass *pass)
943169689Skan{
944169689Skan  do
945169689Skan    {
946169689Skan      if (execute_one_pass (pass) && pass->sub)
947169689Skan	{
948169689Skan	  struct cgraph_node *node;
949169689Skan	  for (node = cgraph_nodes; node; node = node->next)
950169689Skan	    if (node->analyzed)
951169689Skan	      {
952169689Skan		push_cfun (DECL_STRUCT_FUNCTION (node->decl));
953169689Skan		current_function_decl = node->decl;
954169689Skan		execute_pass_list (pass->sub);
955169689Skan		free_dominance_info (CDI_DOMINATORS);
956169689Skan		free_dominance_info (CDI_POST_DOMINATORS);
957169689Skan		current_function_decl = NULL;
958169689Skan		pop_cfun ();
959169689Skan		ggc_collect ();
960169689Skan	      }
961169689Skan	}
962169689Skan      pass = pass->next;
963169689Skan    }
964169689Skan  while (pass);
965169689Skan}
966