OSDN Git Service

* ada/gcc-interface/Make-lang.in, alias.c, attribs.c, auto-inc-dec.c,
[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, 2008, 2009, 2010
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 3, 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 COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 #include "df.h"
86 #include "predict.h"
87 #include "lto-streamer.h"
88 #include "plugin.h"
89
90 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
91 #include "dwarf2out.h"
92 #endif
93
94 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
95 #include "dbxout.h"
96 #endif
97
98 #ifdef SDB_DEBUGGING_INFO
99 #include "sdbout.h"
100 #endif
101
102 #ifdef XCOFF_DEBUGGING_INFO
103 #include "xcoffout.h"           /* Needed for external data
104                                    declarations for e.g. AIX 4.x.  */
105 #endif
106
107 /* This is used for debugging.  It allows the current pass to printed
108    from anywhere in compilation.
109    The variable current_pass is also used for statistics and plugins.  */
110 struct opt_pass *current_pass;
111
112 /* Call from anywhere to find out what pass this is.  Useful for
113    printing out debugging information deep inside an service
114    routine.  */
115 void
116 print_current_pass (FILE *file)
117 {
118   if (current_pass)
119     fprintf (file, "current pass = %s (%d)\n",
120              current_pass->name, current_pass->static_pass_number);
121   else
122     fprintf (file, "no current pass.\n");
123 }
124
125
126 /* Call from the debugger to get the current pass name.  */
127 void
128 debug_pass (void)
129 {
130   print_current_pass (stderr);
131 }
132
133
134
135 /* Global variables used to communicate with passes.  */
136 int dump_flags;
137 bool in_gimple_form;
138 bool first_pass_instance;
139
140
141 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
142    and TYPE_DECL nodes.
143
144    This does nothing for local (non-static) variables, unless the
145    variable is a register variable with DECL_ASSEMBLER_NAME set.  In
146    that case, or if the variable is not an automatic, it sets up the
147    RTL and outputs any assembler code (label definition, storage
148    allocation and initialization).
149
150    DECL is the declaration.  TOP_LEVEL is nonzero
151    if this declaration is not within a function.  */
152
153 void
154 rest_of_decl_compilation (tree decl,
155                           int top_level,
156                           int at_end)
157 {
158   /* We deferred calling assemble_alias so that we could collect
159      other attributes such as visibility.  Emit the alias now.  */
160   {
161     tree alias;
162     alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
163     if (alias)
164       {
165         alias = TREE_VALUE (TREE_VALUE (alias));
166         alias = get_identifier (TREE_STRING_POINTER (alias));
167         assemble_alias (decl, alias);
168       }
169   }
170
171   /* Can't defer this, because it needs to happen before any
172      later function definitions are processed.  */
173   if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
174     make_decl_rtl (decl);
175
176   /* Forward declarations for nested functions are not "external",
177      but we need to treat them as if they were.  */
178   if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
179       || TREE_CODE (decl) == FUNCTION_DECL)
180     {
181       timevar_push (TV_VARCONST);
182
183       /* Don't output anything when a tentative file-scope definition
184          is seen.  But at end of compilation, do output code for them.
185
186          We do output all variables and rely on
187          callgraph code to defer them except for forward declarations
188          (see gcc.c-torture/compile/920624-1.c) */
189       if ((at_end
190            || !DECL_DEFER_OUTPUT (decl)
191            || DECL_INITIAL (decl))
192           && !DECL_EXTERNAL (decl))
193         {
194           if (TREE_CODE (decl) != FUNCTION_DECL)
195             varpool_finalize_decl (decl);
196           else
197             assemble_variable (decl, top_level, at_end, 0);
198         }
199
200 #ifdef ASM_FINISH_DECLARE_OBJECT
201       if (decl == last_assemble_variable_decl)
202         {
203           ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
204                                      top_level, at_end);
205         }
206 #endif
207
208       timevar_pop (TV_VARCONST);
209     }
210   else if (TREE_CODE (decl) == TYPE_DECL
211            /* Like in rest_of_type_compilation, avoid confusing the debug
212               information machinery when there are errors.  */
213            && !(sorrycount || errorcount))
214     {
215       timevar_push (TV_SYMOUT);
216       debug_hooks->type_decl (decl, !top_level);
217       timevar_pop (TV_SYMOUT);
218     }
219
220   /* Let cgraph know about the existence of variables.  */
221   if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
222     varpool_node (decl);
223 }
224
225 /* Called after finishing a record, union or enumeral type.  */
226
227 void
228 rest_of_type_compilation (tree type, int toplev)
229 {
230   /* Avoid confusing the debug information machinery when there are
231      errors.  */
232   if (errorcount != 0 || sorrycount != 0)
233     return;
234
235   timevar_push (TV_SYMOUT);
236   debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
237   timevar_pop (TV_SYMOUT);
238 }
239
240 \f
241
242 void
243 finish_optimization_passes (void)
244 {
245   int i;
246   struct dump_file_info *dfi;
247   char *name;
248
249   timevar_push (TV_DUMP);
250   if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
251     {
252       dump_file = dump_begin (pass_profile.pass.static_pass_number, NULL);
253       end_branch_prob ();
254       if (dump_file)
255         dump_end (pass_profile.pass.static_pass_number, dump_file);
256     }
257
258   if (optimize > 0)
259     {
260       dump_file = dump_begin (pass_combine.pass.static_pass_number, NULL);
261       if (dump_file)
262         {
263           dump_combine_total_stats (dump_file);
264           dump_end (pass_combine.pass.static_pass_number, dump_file);
265         }
266     }
267
268   /* Do whatever is necessary to finish printing the graphs.  */
269   if (graph_dump_format != no_graph)
270     for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
271       if (dump_initialized_p (i)
272           && (dfi->flags & TDF_GRAPH) != 0
273           && (name = get_dump_file_name (i)) != NULL)
274         {
275           finish_graph_dump_file (name);
276           free (name);
277         }
278
279   timevar_pop (TV_DUMP);
280 }
281
282 static bool
283 gate_rest_of_compilation (void)
284 {
285   /* Early return if there were errors.  We can run afoul of our
286      consistency checks, and there's not really much point in fixing them.  */
287   return !(rtl_dump_and_exit || flag_syntax_only || errorcount || sorrycount);
288 }
289
290 struct gimple_opt_pass pass_rest_of_compilation =
291 {
292  {
293   GIMPLE_PASS,
294   "*rest_of_compilation",               /* name */
295   gate_rest_of_compilation,             /* gate */
296   NULL,                                 /* execute */
297   NULL,                                 /* sub */
298   NULL,                                 /* next */
299   0,                                    /* static_pass_number */
300   TV_REST_OF_COMPILATION,               /* tv_id */
301   PROP_rtl,                             /* properties_required */
302   0,                                    /* properties_provided */
303   0,                                    /* properties_destroyed */
304   0,                                    /* todo_flags_start */
305   TODO_ggc_collect                      /* todo_flags_finish */
306  }
307 };
308
309 static bool
310 gate_postreload (void)
311 {
312   return reload_completed;
313 }
314
315 struct rtl_opt_pass pass_postreload =
316 {
317  {
318   RTL_PASS,
319   "*all-postreload",                        /* name */
320   gate_postreload,                      /* gate */
321   NULL,                                 /* execute */
322   NULL,                                 /* sub */
323   NULL,                                 /* next */
324   0,                                    /* static_pass_number */
325   TV_NONE,                              /* tv_id */
326   PROP_rtl,                             /* properties_required */
327   0,                                    /* properties_provided */
328   0,                                    /* properties_destroyed */
329   0,                                    /* todo_flags_start */
330   TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
331  }
332 };
333
334
335
336 /* The root of the compilation pass tree, once constructed.  */
337 struct opt_pass *all_passes, *all_small_ipa_passes, *all_lowering_passes,
338   *all_regular_ipa_passes, *all_lto_gen_passes;
339
340 /* This is used by plugins, and should also be used in register_pass.  */
341 #define DEF_PASS_LIST(LIST) &LIST,
342 struct opt_pass **gcc_pass_lists[] = { GCC_PASS_LISTS NULL };
343 #undef DEF_PASS_LIST
344
345 /* A map from static pass id to optimization pass.  */
346 struct opt_pass **passes_by_id;
347 int passes_by_id_size;
348
349 /* Set the static pass number of pass PASS to ID and record that
350    in the mapping from static pass number to pass.  */
351
352 static void
353 set_pass_for_id (int id, struct opt_pass *pass)
354 {
355   pass->static_pass_number = id;
356   if (passes_by_id_size <= id)
357     {
358       passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
359       memset (passes_by_id + passes_by_id_size, 0,
360               (id + 1 - passes_by_id_size) * sizeof (void *));
361       passes_by_id_size = id + 1;
362     }
363   passes_by_id[id] = pass;
364 }
365
366 /* Return the pass with the static pass number ID.  */
367
368 struct opt_pass *
369 get_pass_for_id (int id)
370 {
371   if (id >= passes_by_id_size)
372     return NULL;
373   return passes_by_id[id];
374 }
375
376 /* Iterate over the pass tree allocating dump file numbers.  We want
377    to do this depth first, and independent of whether the pass is
378    enabled or not.  */
379
380 void
381 register_one_dump_file (struct opt_pass *pass)
382 {
383   char *dot_name, *flag_name, *glob_name;
384   const char *name, *prefix;
385   char num[10];
386   int flags, id;
387
388   /* See below in next_pass_1.  */
389   num[0] = '\0';
390   if (pass->static_pass_number != -1)
391     sprintf (num, "%d", ((int) pass->static_pass_number < 0
392                          ? 1 : pass->static_pass_number));
393
394   /* The name is both used to identify the pass for the purposes of plugins,
395      and to specify dump file name and option.
396      The latter two might want something short which is not quite unique; for
397      that reason, we may have a disambiguating prefix, followed by a space
398      to mark the start of the following dump file name / option string.  */
399   name = strchr (pass->name, ' ');
400   name = name ? name + 1 : pass->name;
401   dot_name = concat (".", name, num, NULL);
402   if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
403     prefix = "ipa-", flags = TDF_IPA;
404   else if (pass->type == GIMPLE_PASS)
405     prefix = "tree-", flags = TDF_TREE;
406   else
407     prefix = "rtl-", flags = TDF_RTL;
408
409   flag_name = concat (prefix, name, num, NULL);
410   glob_name = concat (prefix, name, NULL);
411   id = dump_register (dot_name, flag_name, glob_name, flags);
412   set_pass_for_id (id, pass);
413 }
414
415 /* Recursive worker function for register_dump_files.  */
416
417 static int
418 register_dump_files_1 (struct opt_pass *pass, int properties)
419 {
420   do
421     {
422       int new_properties = (properties | pass->properties_provided)
423                            & ~pass->properties_destroyed;
424
425       if (pass->name && pass->name[0] != '*')
426         register_one_dump_file (pass);
427
428       if (pass->sub)
429         new_properties = register_dump_files_1 (pass->sub, new_properties);
430
431       /* If we have a gate, combine the properties that we could have with
432          and without the pass being examined.  */
433       if (pass->gate)
434         properties &= new_properties;
435       else
436         properties = new_properties;
437
438       pass = pass->next;
439     }
440   while (pass);
441
442   return properties;
443 }
444
445 /* Register the dump files for the pipeline starting at PASS.
446    PROPERTIES reflects the properties that are guaranteed to be available at
447    the beginning of the pipeline.  */
448
449 static void
450 register_dump_files (struct opt_pass *pass,int properties)
451 {
452   pass->properties_required |= properties;
453   register_dump_files_1 (pass, properties);
454 }
455
456 /* Look at the static_pass_number and duplicate the pass
457    if it is already added to a list. */
458
459 static struct opt_pass *
460 make_pass_instance (struct opt_pass *pass, bool track_duplicates)
461 {
462   /* A nonzero static_pass_number indicates that the
463      pass is already in the list.  */
464   if (pass->static_pass_number)
465     {
466       struct opt_pass *new_pass;
467
468       if (pass->type == GIMPLE_PASS
469           || pass->type == RTL_PASS
470           || pass->type == SIMPLE_IPA_PASS)
471         {
472           new_pass = XNEW (struct opt_pass);
473           memcpy (new_pass, pass, sizeof (struct opt_pass));
474         }
475       else if (pass->type == IPA_PASS)
476         {
477           new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
478           memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
479         }
480       else
481         gcc_unreachable ();
482
483       new_pass->next = NULL;
484
485       new_pass->todo_flags_start &= ~TODO_mark_first_instance;
486
487       /* Indicate to register_dump_files that this pass has duplicates,
488          and so it should rename the dump file.  The first instance will
489          be -1, and be number of duplicates = -static_pass_number - 1.
490          Subsequent instances will be > 0 and just the duplicate number.  */
491       if ((pass->name && pass->name[0] != '*') || track_duplicates)
492         {
493           pass->static_pass_number -= 1;
494           new_pass->static_pass_number = -pass->static_pass_number;
495         }
496       return new_pass;
497     }
498   else
499     {
500       pass->todo_flags_start |= TODO_mark_first_instance;
501       pass->static_pass_number = -1;
502
503       invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
504     }
505   return pass;
506 }
507
508 /* Add a pass to the pass list. Duplicate the pass if it's already
509    in the list.  */
510
511 static struct opt_pass **
512 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
513 {
514   /* Every pass should have a name so that plugins can refer to them.  */
515   gcc_assert (pass->name != NULL);
516
517   *list = make_pass_instance (pass, false);
518
519   return &(*list)->next;
520 }
521
522 /* List node for an inserted pass instance. We need to keep track of all
523    the newly-added pass instances (with 'added_pass_nodes' defined below)
524    so that we can register their dump files after pass-positioning is finished.
525    Registering dumping files needs to be post-processed or the
526    static_pass_number of the opt_pass object would be modified and mess up
527    the dump file names of future pass instances to be added.  */
528
529 struct pass_list_node
530 {
531   struct opt_pass *pass;
532   struct pass_list_node *next;
533 };
534
535 static struct pass_list_node *added_pass_nodes = NULL;
536 static struct pass_list_node *prev_added_pass_node;
537
538 /* Insert the pass at the proper position. Return true if the pass
539    is successfully added.
540
541    NEW_PASS_INFO - new pass to be inserted
542    PASS_LIST - root of the pass list to insert the new pass to  */
543
544 static bool
545 position_pass (struct register_pass_info *new_pass_info,
546                struct opt_pass **pass_list)
547 {
548   struct opt_pass *pass = *pass_list, *prev_pass = NULL;
549   bool success = false;
550
551   for ( ; pass; prev_pass = pass, pass = pass->next)
552     {
553       /* Check if the current pass is of the same type as the new pass and
554          matches the name and the instance number of the reference pass.  */
555       if (pass->type == new_pass_info->pass->type
556           && pass->name
557           && !strcmp (pass->name, new_pass_info->reference_pass_name)
558           && ((new_pass_info->ref_pass_instance_number == 0)
559               || (new_pass_info->ref_pass_instance_number ==
560                   pass->static_pass_number)
561               || (new_pass_info->ref_pass_instance_number == 1
562                   && pass->todo_flags_start & TODO_mark_first_instance)))
563         {
564           struct opt_pass *new_pass;
565           struct pass_list_node *new_pass_node;
566
567           new_pass = make_pass_instance (new_pass_info->pass, true);
568
569           /* Insert the new pass instance based on the positioning op.  */
570           switch (new_pass_info->pos_op)
571             {
572               case PASS_POS_INSERT_AFTER:
573                 new_pass->next = pass->next;
574                 pass->next = new_pass;
575
576                 /* Skip newly inserted pass to avoid repeated
577                    insertions in the case where the new pass and the
578                    existing one have the same name.  */
579                 pass = new_pass;
580                 break;
581               case PASS_POS_INSERT_BEFORE:
582                 new_pass->next = pass;
583                 if (prev_pass)
584                   prev_pass->next = new_pass;
585                 else
586                   *pass_list = new_pass;
587                 break;
588               case PASS_POS_REPLACE:
589                 new_pass->next = pass->next;
590                 if (prev_pass)
591                   prev_pass->next = new_pass;
592                 else
593                   *pass_list = new_pass;
594                 new_pass->sub = pass->sub;
595                 new_pass->tv_id = pass->tv_id;
596                 pass = new_pass;
597                 break;
598               default:
599                 error ("Invalid pass positioning operation");
600                 return false;
601             }
602
603           /* Save the newly added pass (instance) in the added_pass_nodes
604              list so that we can register its dump file later. Note that
605              we cannot register the dump file now because doing so will modify
606              the static_pass_number of the opt_pass object and therefore
607              mess up the dump file name of future instances.  */
608           new_pass_node = XCNEW (struct pass_list_node);
609           new_pass_node->pass = new_pass;
610           if (!added_pass_nodes)
611             added_pass_nodes = new_pass_node;
612           else
613             prev_added_pass_node->next = new_pass_node;
614           prev_added_pass_node = new_pass_node;
615
616           success = true;
617         }
618
619       if (pass->sub && position_pass (new_pass_info, &pass->sub))
620         success = true;
621     }
622
623   return success;
624 }
625
626 /* Hooks a new pass into the pass lists.
627
628    PASS_INFO   - pass information that specifies the opt_pass object,
629                  reference pass, instance number, and how to position
630                  the pass  */
631
632 void
633 register_pass (struct register_pass_info *pass_info)
634 {
635   bool all_instances, success;
636
637   /* The checks below could fail in buggy plugins.  Existing GCC
638      passes should never fail these checks, so we mention plugin in
639      the messages.  */
640   if (!pass_info->pass)
641       fatal_error ("plugin cannot register a missing pass");
642
643   if (!pass_info->pass->name)
644       fatal_error ("plugin cannot register an unnamed pass");
645
646   if (!pass_info->reference_pass_name)
647       fatal_error
648         ("plugin cannot register pass %qs without reference pass name",
649          pass_info->pass->name);
650
651   /* Try to insert the new pass to the pass lists.  We need to check
652      all five lists as the reference pass could be in one (or all) of
653      them.  */
654   all_instances = pass_info->ref_pass_instance_number == 0;
655   success = position_pass (pass_info, &all_lowering_passes);
656   if (!success || all_instances)
657     success |= position_pass (pass_info, &all_small_ipa_passes);
658   if (!success || all_instances)
659     success |= position_pass (pass_info, &all_regular_ipa_passes);
660   if (!success || all_instances)
661     success |= position_pass (pass_info, &all_lto_gen_passes);
662   if (!success || all_instances)
663     success |= position_pass (pass_info, &all_passes);
664   if (!success)
665     fatal_error
666       ("pass %qs not found but is referenced by new pass %qs",
667        pass_info->reference_pass_name, pass_info->pass->name);
668
669   /* OK, we have successfully inserted the new pass. We need to register
670      the dump files for the newly added pass and its duplicates (if any).
671      Because the registration of plugin/backend passes happens after the
672      command-line options are parsed, the options that specify single
673      pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
674      passes. Therefore we currently can only enable dumping of
675      new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
676      are specified. While doing so, we also delete the pass_list_node
677      objects created during pass positioning.  */
678   while (added_pass_nodes)
679     {
680       struct pass_list_node *next_node = added_pass_nodes->next;
681       enum tree_dump_index tdi;
682       register_one_dump_file (added_pass_nodes->pass);
683       if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
684           || added_pass_nodes->pass->type == IPA_PASS)
685         tdi = TDI_ipa_all;
686       else if (added_pass_nodes->pass->type == GIMPLE_PASS)
687         tdi = TDI_tree_all;
688       else
689         tdi = TDI_rtl_all;
690       /* Check if dump-all flag is specified.  */
691       if (get_dump_file_info (tdi)->state)
692         get_dump_file_info (added_pass_nodes->pass->static_pass_number)
693             ->state = get_dump_file_info (tdi)->state;
694       XDELETE (added_pass_nodes);
695       added_pass_nodes = next_node;
696     }
697 }
698
699 /* Construct the pass tree.  The sequencing of passes is driven by
700    the cgraph routines:
701
702    cgraph_finalize_compilation_unit ()
703        for each node N in the cgraph
704            cgraph_analyze_function (N)
705                cgraph_lower_function (N) -> all_lowering_passes
706
707    If we are optimizing, cgraph_optimize is then invoked:
708
709    cgraph_optimize ()
710        ipa_passes ()                    -> all_small_ipa_passes
711        cgraph_expand_all_functions ()
712            for each node N in the cgraph
713                cgraph_expand_function (N)
714                   tree_rest_of_compilation (DECL (N))  -> all_passes
715 */
716
717 void
718 init_optimization_passes (void)
719 {
720   struct opt_pass **p;
721
722 #define NEXT_PASS(PASS)  (p = next_pass_1 (p, &((PASS).pass)))
723
724  /* All passes needed to lower the function into shape optimizers can
725     operate on.  These passes are always run first on the function, but
726     backend might produce already lowered functions that are not processed
727     by these passes.  */
728   p = &all_lowering_passes;
729   NEXT_PASS (pass_warn_unused_result);
730   NEXT_PASS (pass_diagnose_omp_blocks);
731   NEXT_PASS (pass_mudflap_1);
732   NEXT_PASS (pass_lower_omp);
733   NEXT_PASS (pass_lower_cf);
734   NEXT_PASS (pass_refactor_eh);
735   NEXT_PASS (pass_lower_eh);
736   NEXT_PASS (pass_build_cfg);
737   NEXT_PASS (pass_lower_vector);
738   NEXT_PASS (pass_warn_function_return);
739   NEXT_PASS (pass_build_cgraph_edges);
740   NEXT_PASS (pass_inline_parameters);
741   *p = NULL;
742
743   /* Interprocedural optimization passes.  */
744   p = &all_small_ipa_passes;
745   NEXT_PASS (pass_ipa_function_and_variable_visibility);
746   NEXT_PASS (pass_ipa_early_inline);
747     {
748       struct opt_pass **p = &pass_ipa_early_inline.pass.sub;
749       NEXT_PASS (pass_early_inline);
750       NEXT_PASS (pass_inline_parameters);
751       NEXT_PASS (pass_rebuild_cgraph_edges);
752     }
753   NEXT_PASS (pass_ipa_free_lang_data);
754   NEXT_PASS (pass_early_local_passes);
755     {
756       struct opt_pass **p = &pass_early_local_passes.pass.sub;
757       NEXT_PASS (pass_fixup_cfg);
758       NEXT_PASS (pass_tree_profile);
759       NEXT_PASS (pass_cleanup_cfg);
760       NEXT_PASS (pass_init_datastructures);
761       NEXT_PASS (pass_expand_omp);
762
763       NEXT_PASS (pass_referenced_vars);
764       NEXT_PASS (pass_build_ssa);
765       NEXT_PASS (pass_early_warn_uninitialized);
766       /* Note that it is not strictly necessary to schedule an early
767          inline pass here.  However, some test cases (e.g.,
768          g++.dg/other/p334435.C g++.dg/other/i386-1.C) expect extern
769          inline functions to be inlined even at -O0.  This does not
770          happen during the first early inline pass.  */
771       NEXT_PASS (pass_rebuild_cgraph_edges);
772       NEXT_PASS (pass_early_inline);
773       NEXT_PASS (pass_all_early_optimizations);
774         {
775           struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
776           NEXT_PASS (pass_remove_cgraph_callee_edges);
777           NEXT_PASS (pass_rename_ssa_copies);
778           NEXT_PASS (pass_ccp);
779           NEXT_PASS (pass_forwprop);
780           /* pass_build_ealias is a dummy pass that ensures that we
781              execute TODO_rebuild_alias at this point.  Re-building
782              alias information also rewrites no longer addressed
783              locals into SSA form if possible.  */
784           NEXT_PASS (pass_build_ealias);
785           NEXT_PASS (pass_sra_early);
786           NEXT_PASS (pass_copy_prop);
787           NEXT_PASS (pass_merge_phi);
788           NEXT_PASS (pass_cd_dce);
789           NEXT_PASS (pass_early_ipa_sra);
790           NEXT_PASS (pass_tail_recursion);
791           NEXT_PASS (pass_convert_switch);
792           NEXT_PASS (pass_cleanup_eh);
793           NEXT_PASS (pass_profile);
794           NEXT_PASS (pass_local_pure_const);
795         }
796       NEXT_PASS (pass_release_ssa_names);
797       NEXT_PASS (pass_rebuild_cgraph_edges);
798       NEXT_PASS (pass_inline_parameters);
799     }
800   NEXT_PASS (pass_ipa_increase_alignment);
801   NEXT_PASS (pass_ipa_matrix_reorg);
802   *p = NULL;
803
804   p = &all_regular_ipa_passes;
805   NEXT_PASS (pass_ipa_whole_program_visibility);
806   NEXT_PASS (pass_ipa_cp);
807   NEXT_PASS (pass_ipa_inline);
808   NEXT_PASS (pass_ipa_reference);
809   NEXT_PASS (pass_ipa_pure_const);
810   NEXT_PASS (pass_ipa_type_escape);
811   NEXT_PASS (pass_ipa_pta);
812   NEXT_PASS (pass_ipa_struct_reorg);
813   *p = NULL;
814
815   p = &all_lto_gen_passes;
816   NEXT_PASS (pass_ipa_lto_gimple_out);
817   NEXT_PASS (pass_ipa_lto_wpa_fixup);
818   NEXT_PASS (pass_ipa_lto_finish_out);  /* This must be the last LTO pass.  */
819   *p = NULL;
820
821   /* These passes are run after IPA passes on every function that is being
822      output to the assembler file.  */
823   p = &all_passes;
824   NEXT_PASS (pass_lower_eh_dispatch);
825   NEXT_PASS (pass_all_optimizations);
826     {
827       struct opt_pass **p = &pass_all_optimizations.pass.sub;
828       NEXT_PASS (pass_remove_cgraph_callee_edges);
829       /* Initial scalar cleanups before alias computation.
830          They ensure memory accesses are not indirect wherever possible.  */
831       NEXT_PASS (pass_strip_predict_hints);
832       NEXT_PASS (pass_update_address_taken);
833       NEXT_PASS (pass_rename_ssa_copies);
834       NEXT_PASS (pass_complete_unrolli);
835       NEXT_PASS (pass_ccp);
836       NEXT_PASS (pass_forwprop);
837       NEXT_PASS (pass_call_cdce);
838       /* pass_build_alias is a dummy pass that ensures that we
839          execute TODO_rebuild_alias at this point.  Re-building
840          alias information also rewrites no longer addressed
841          locals into SSA form if possible.  */
842       NEXT_PASS (pass_build_alias);
843       NEXT_PASS (pass_return_slot);
844       NEXT_PASS (pass_phiprop);
845       NEXT_PASS (pass_fre);
846       NEXT_PASS (pass_copy_prop);
847       NEXT_PASS (pass_merge_phi);
848       NEXT_PASS (pass_vrp);
849       NEXT_PASS (pass_dce);
850       NEXT_PASS (pass_cselim);
851       NEXT_PASS (pass_tree_ifcombine);
852       NEXT_PASS (pass_phiopt);
853       NEXT_PASS (pass_tail_recursion);
854       NEXT_PASS (pass_ch);
855       NEXT_PASS (pass_stdarg);
856       NEXT_PASS (pass_lower_complex);
857       NEXT_PASS (pass_sra);
858       NEXT_PASS (pass_rename_ssa_copies);
859       /* The dom pass will also resolve all __builtin_constant_p calls
860          that are still there to 0.  This has to be done after some
861          propagations have already run, but before some more dead code
862          is removed, and this place fits nicely.  Remember this when
863          trying to move or duplicate pass_dominator somewhere earlier.  */
864       NEXT_PASS (pass_dominator);
865       /* The only const/copy propagation opportunities left after
866          DOM should be due to degenerate PHI nodes.  So rather than
867          run the full propagators, run a specialized pass which
868          only examines PHIs to discover const/copy propagation
869          opportunities.  */
870       NEXT_PASS (pass_phi_only_cprop);
871       NEXT_PASS (pass_dse);
872       NEXT_PASS (pass_reassoc);
873       NEXT_PASS (pass_dce);
874       NEXT_PASS (pass_forwprop);
875       NEXT_PASS (pass_phiopt);
876       NEXT_PASS (pass_object_sizes);
877       NEXT_PASS (pass_ccp);
878       NEXT_PASS (pass_copy_prop);
879       NEXT_PASS (pass_cse_sincos);
880       NEXT_PASS (pass_optimize_bswap);
881       NEXT_PASS (pass_split_crit_edges);
882       NEXT_PASS (pass_pre);
883       NEXT_PASS (pass_sink_code);
884       NEXT_PASS (pass_tree_loop);
885         {
886           struct opt_pass **p = &pass_tree_loop.pass.sub;
887           NEXT_PASS (pass_tree_loop_init);
888           NEXT_PASS (pass_copy_prop);
889           NEXT_PASS (pass_dce_loop);
890           NEXT_PASS (pass_lim);
891           NEXT_PASS (pass_tree_unswitch);
892           NEXT_PASS (pass_scev_cprop);
893           NEXT_PASS (pass_record_bounds);
894           NEXT_PASS (pass_check_data_deps);
895           NEXT_PASS (pass_loop_distribution);
896           NEXT_PASS (pass_linear_transform);
897           NEXT_PASS (pass_graphite_transforms);
898             {
899               struct opt_pass **p = &pass_graphite_transforms.pass.sub;
900               NEXT_PASS (pass_copy_prop);
901               NEXT_PASS (pass_dce_loop);
902               NEXT_PASS (pass_lim);
903             }
904           NEXT_PASS (pass_iv_canon);
905           NEXT_PASS (pass_if_conversion);
906           NEXT_PASS (pass_vectorize);
907             {
908               struct opt_pass **p = &pass_vectorize.pass.sub;
909               NEXT_PASS (pass_lower_vector_ssa);
910               NEXT_PASS (pass_dce_loop);
911             }
912           NEXT_PASS (pass_predcom);
913           NEXT_PASS (pass_complete_unroll);
914           NEXT_PASS (pass_slp_vectorize);
915           NEXT_PASS (pass_parallelize_loops);
916           NEXT_PASS (pass_loop_prefetch);
917           NEXT_PASS (pass_iv_optimize);
918           NEXT_PASS (pass_tree_loop_done);
919         }
920       NEXT_PASS (pass_cse_reciprocals);
921       NEXT_PASS (pass_reassoc);
922       NEXT_PASS (pass_vrp);
923       NEXT_PASS (pass_dominator);
924       /* The only const/copy propagation opportunities left after
925          DOM should be due to degenerate PHI nodes.  So rather than
926          run the full propagators, run a specialized pass which
927          only examines PHIs to discover const/copy propagation
928          opportunities.  */
929       NEXT_PASS (pass_phi_only_cprop);
930       NEXT_PASS (pass_cd_dce);
931       NEXT_PASS (pass_tracer);
932
933       /* FIXME: If DCE is not run before checking for uninitialized uses,
934          we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
935          However, this also causes us to misdiagnose cases that should be
936          real warnings (e.g., testsuite/gcc.dg/pr18501.c).
937
938          To fix the false positives in uninit-5.c, we would have to
939          account for the predicates protecting the set and the use of each
940          variable.  Using a representation like Gated Single Assignment
941          may help.  */
942       NEXT_PASS (pass_late_warn_uninitialized);
943       NEXT_PASS (pass_dse);
944       NEXT_PASS (pass_forwprop);
945       NEXT_PASS (pass_phiopt);
946       NEXT_PASS (pass_fold_builtins);
947       NEXT_PASS (pass_tail_calls);
948       NEXT_PASS (pass_rename_ssa_copies);
949       NEXT_PASS (pass_uncprop);
950       NEXT_PASS (pass_local_pure_const);
951     }
952   NEXT_PASS (pass_lower_complex_O0);
953   NEXT_PASS (pass_cleanup_eh);
954   NEXT_PASS (pass_lower_resx);
955   NEXT_PASS (pass_nrv);
956   NEXT_PASS (pass_mudflap_2);
957   NEXT_PASS (pass_cleanup_cfg_post_optimizing);
958   NEXT_PASS (pass_warn_function_noreturn);
959
960   NEXT_PASS (pass_expand);
961
962   NEXT_PASS (pass_rest_of_compilation);
963     {
964       struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
965       NEXT_PASS (pass_init_function);
966       NEXT_PASS (pass_jump);
967       NEXT_PASS (pass_rtl_eh);
968       NEXT_PASS (pass_initial_value_sets);
969       NEXT_PASS (pass_unshare_all_rtl);
970       NEXT_PASS (pass_instantiate_virtual_regs);
971       NEXT_PASS (pass_into_cfg_layout_mode);
972       NEXT_PASS (pass_jump2);
973       NEXT_PASS (pass_lower_subreg);
974       NEXT_PASS (pass_df_initialize_opt);
975       NEXT_PASS (pass_cse);
976       NEXT_PASS (pass_rtl_fwprop);
977       NEXT_PASS (pass_rtl_cprop);
978       NEXT_PASS (pass_rtl_pre);
979       NEXT_PASS (pass_rtl_hoist);
980       NEXT_PASS (pass_rtl_cprop);
981       NEXT_PASS (pass_rtl_store_motion);
982       NEXT_PASS (pass_cse_after_global_opts);
983       NEXT_PASS (pass_rtl_ifcvt);
984       NEXT_PASS (pass_reginfo_init);
985       /* Perform loop optimizations.  It might be better to do them a bit
986          sooner, but we want the profile feedback to work more
987          efficiently.  */
988       NEXT_PASS (pass_loop2);
989         {
990           struct opt_pass **p = &pass_loop2.pass.sub;
991           NEXT_PASS (pass_rtl_loop_init);
992           NEXT_PASS (pass_rtl_move_loop_invariants);
993           NEXT_PASS (pass_rtl_unswitch);
994           NEXT_PASS (pass_rtl_unroll_and_peel_loops);
995           NEXT_PASS (pass_rtl_doloop);
996           NEXT_PASS (pass_rtl_loop_done);
997           *p = NULL;
998         }
999       NEXT_PASS (pass_web);
1000       NEXT_PASS (pass_rtl_cprop);
1001       NEXT_PASS (pass_cse2);
1002       NEXT_PASS (pass_rtl_dse1);
1003       NEXT_PASS (pass_rtl_fwprop_addr);
1004       NEXT_PASS (pass_inc_dec);
1005       NEXT_PASS (pass_initialize_regs);
1006       NEXT_PASS (pass_ud_rtl_dce);
1007       NEXT_PASS (pass_combine);
1008       NEXT_PASS (pass_if_after_combine);
1009       NEXT_PASS (pass_partition_blocks);
1010       NEXT_PASS (pass_regmove);
1011       NEXT_PASS (pass_outof_cfg_layout_mode);
1012       NEXT_PASS (pass_split_all_insns);
1013       NEXT_PASS (pass_lower_subreg2);
1014       NEXT_PASS (pass_df_initialize_no_opt);
1015       NEXT_PASS (pass_stack_ptr_mod);
1016       NEXT_PASS (pass_mode_switching);
1017       NEXT_PASS (pass_match_asm_constraints);
1018       NEXT_PASS (pass_sms);
1019       NEXT_PASS (pass_sched);
1020       NEXT_PASS (pass_ira);
1021       NEXT_PASS (pass_postreload);
1022         {
1023           struct opt_pass **p = &pass_postreload.pass.sub;
1024           NEXT_PASS (pass_postreload_cse);
1025           NEXT_PASS (pass_gcse2);
1026           NEXT_PASS (pass_split_after_reload);
1027           NEXT_PASS (pass_branch_target_load_optimize1);
1028           NEXT_PASS (pass_thread_prologue_and_epilogue);
1029           NEXT_PASS (pass_rtl_dse2);
1030           NEXT_PASS (pass_stack_adjustments);
1031           NEXT_PASS (pass_peephole2);
1032           NEXT_PASS (pass_if_after_reload);
1033           NEXT_PASS (pass_regrename);
1034           NEXT_PASS (pass_cprop_hardreg);
1035           NEXT_PASS (pass_fast_rtl_dce);
1036           NEXT_PASS (pass_reorder_blocks);
1037           NEXT_PASS (pass_branch_target_load_optimize2);
1038           NEXT_PASS (pass_leaf_regs);
1039           NEXT_PASS (pass_split_before_sched2);
1040           NEXT_PASS (pass_sched2);
1041           NEXT_PASS (pass_stack_regs);
1042             {
1043               struct opt_pass **p = &pass_stack_regs.pass.sub;
1044               NEXT_PASS (pass_split_before_regstack);
1045               NEXT_PASS (pass_stack_regs_run);
1046             }
1047           NEXT_PASS (pass_compute_alignments);
1048           NEXT_PASS (pass_duplicate_computed_gotos);
1049           NEXT_PASS (pass_variable_tracking);
1050           NEXT_PASS (pass_free_cfg);
1051           NEXT_PASS (pass_machine_reorg);
1052           NEXT_PASS (pass_cleanup_barriers);
1053           NEXT_PASS (pass_delay_slots);
1054           NEXT_PASS (pass_split_for_shorten_branches);
1055           NEXT_PASS (pass_convert_to_eh_region_ranges);
1056           NEXT_PASS (pass_shorten_branches);
1057           NEXT_PASS (pass_set_nothrow_function_flags);
1058           NEXT_PASS (pass_final);
1059         }
1060       NEXT_PASS (pass_df_finish);
1061     }
1062   NEXT_PASS (pass_clean_state);
1063   *p = NULL;
1064
1065 #undef NEXT_PASS
1066
1067   /* Register the passes with the tree dump code.  */
1068   register_dump_files (all_lowering_passes, PROP_gimple_any);
1069   register_dump_files (all_small_ipa_passes,
1070                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1071                        | PROP_cfg);
1072   register_dump_files (all_regular_ipa_passes,
1073                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1074                        | PROP_cfg);
1075   register_dump_files (all_lto_gen_passes,
1076                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1077                        | PROP_cfg);
1078   register_dump_files (all_passes,
1079                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1080                        | PROP_cfg);
1081 }
1082
1083 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1084    function CALLBACK for every function in the call graph.  Otherwise,
1085    call CALLBACK on the current function.  */
1086
1087 static void
1088 do_per_function (void (*callback) (void *data), void *data)
1089 {
1090   if (current_function_decl)
1091     callback (data);
1092   else
1093     {
1094       struct cgraph_node *node;
1095       for (node = cgraph_nodes; node; node = node->next)
1096         if (node->analyzed && gimple_has_body_p (node->decl)
1097             && (!node->clone_of || node->decl != node->clone_of->decl))
1098           {
1099             push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1100             current_function_decl = node->decl;
1101             callback (data);
1102             if (!flag_wpa)
1103               {
1104                 free_dominance_info (CDI_DOMINATORS);
1105                 free_dominance_info (CDI_POST_DOMINATORS);
1106               }
1107             current_function_decl = NULL;
1108             pop_cfun ();
1109             ggc_collect ();
1110           }
1111     }
1112 }
1113
1114 /* Because inlining might remove no-longer reachable nodes, we need to
1115    keep the array visible to garbage collector to avoid reading collected
1116    out nodes.  */
1117 static int nnodes;
1118 static GTY ((length ("nnodes"))) struct cgraph_node **order;
1119
1120 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1121    function CALLBACK for every function in the call graph.  Otherwise,
1122    call CALLBACK on the current function.
1123    This function is global so that plugins can use it.  */
1124 void
1125 do_per_function_toporder (void (*callback) (void *data), void *data)
1126 {
1127   int i;
1128
1129   if (current_function_decl)
1130     callback (data);
1131   else
1132     {
1133       gcc_assert (!order);
1134       order = GGC_NEWVEC (struct cgraph_node *, cgraph_n_nodes);
1135       nnodes = cgraph_postorder (order);
1136       for (i = nnodes - 1; i >= 0; i--)
1137         order[i]->process = 1;
1138       for (i = nnodes - 1; i >= 0; i--)
1139         {
1140           struct cgraph_node *node = order[i];
1141
1142           /* Allow possibly removed nodes to be garbage collected.  */
1143           order[i] = NULL;
1144           node->process = 0;
1145           if (node->analyzed)
1146             {
1147               push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1148               current_function_decl = node->decl;
1149               callback (data);
1150               free_dominance_info (CDI_DOMINATORS);
1151               free_dominance_info (CDI_POST_DOMINATORS);
1152               current_function_decl = NULL;
1153               pop_cfun ();
1154               ggc_collect ();
1155             }
1156         }
1157     }
1158   ggc_free (order);
1159   order = NULL;
1160   nnodes = 0;
1161 }
1162
1163 /* Perform all TODO actions that ought to be done on each function.  */
1164
1165 static void
1166 execute_function_todo (void *data)
1167 {
1168   unsigned int flags = (size_t)data;
1169   if (cfun->curr_properties & PROP_ssa)
1170     flags |= TODO_verify_ssa;
1171   flags &= ~cfun->last_verified;
1172   if (!flags)
1173     return;
1174
1175   statistics_fini_pass ();
1176
1177   /* Always cleanup the CFG before trying to update SSA.  */
1178   if (flags & TODO_cleanup_cfg)
1179     {
1180       bool cleanup = cleanup_tree_cfg ();
1181
1182       if (cleanup && (cfun->curr_properties & PROP_ssa))
1183         flags |= TODO_remove_unused_locals;
1184
1185       /* When cleanup_tree_cfg merges consecutive blocks, it may
1186          perform some simplistic propagation when removing single
1187          valued PHI nodes.  This propagation may, in turn, cause the
1188          SSA form to become out-of-date (see PR 22037).  So, even
1189          if the parent pass had not scheduled an SSA update, we may
1190          still need to do one.  */
1191       if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1192         flags |= TODO_update_ssa;
1193     }
1194
1195   if (flags & TODO_update_ssa_any)
1196     {
1197       unsigned update_flags = flags & TODO_update_ssa_any;
1198       update_ssa (update_flags);
1199       cfun->last_verified &= ~TODO_verify_ssa;
1200     }
1201
1202   if (flags & TODO_update_address_taken)
1203     execute_update_addresses_taken (true);
1204
1205   if (flags & TODO_rebuild_alias)
1206     {
1207       if (!(flags & TODO_update_address_taken))
1208         execute_update_addresses_taken (true);
1209       compute_may_aliases ();
1210     }
1211
1212   if (flags & TODO_remove_unused_locals)
1213     remove_unused_locals ();
1214
1215   if ((flags & TODO_dump_func) && dump_file && current_function_decl)
1216     {
1217       if (cfun->curr_properties & PROP_trees)
1218         dump_function_to_file (current_function_decl, dump_file, dump_flags);
1219       else
1220         {
1221           if (dump_flags & TDF_SLIM)
1222             print_rtl_slim_with_bb (dump_file, get_insns (), dump_flags);
1223           else if ((cfun->curr_properties & PROP_cfg)
1224                    && (dump_flags & TDF_BLOCKS))
1225             print_rtl_with_bb (dump_file, get_insns ());
1226           else
1227             print_rtl (dump_file, get_insns ());
1228
1229           if ((cfun->curr_properties & PROP_cfg)
1230               && graph_dump_format != no_graph
1231               && (dump_flags & TDF_GRAPH))
1232             print_rtl_graph_with_bb (dump_file_name, get_insns ());
1233         }
1234
1235       /* Flush the file.  If verification fails, we won't be able to
1236          close the file before aborting.  */
1237       fflush (dump_file);
1238     }
1239
1240   if (flags & TODO_rebuild_frequencies)
1241     {
1242       if (profile_status == PROFILE_GUESSED)
1243         {
1244           loop_optimizer_init (0);
1245           add_noreturn_fake_exit_edges ();
1246           mark_irreducible_loops ();
1247           connect_infinite_loops_to_exit ();
1248           estimate_bb_frequencies ();
1249           remove_fake_exit_edges ();
1250           loop_optimizer_finalize ();
1251         }
1252       else if (profile_status == PROFILE_READ)
1253         counts_to_freqs ();
1254       else
1255         gcc_unreachable ();
1256     }
1257
1258 #if defined ENABLE_CHECKING
1259   if (flags & TODO_verify_ssa)
1260     verify_ssa (true);
1261   if (flags & TODO_verify_flow)
1262     verify_flow_info ();
1263   if (flags & TODO_verify_stmts)
1264     verify_stmts ();
1265   if (flags & TODO_verify_loops)
1266     verify_loop_closed_ssa ();
1267   if (flags & TODO_verify_rtl_sharing)
1268     verify_rtl_sharing ();
1269 #endif
1270
1271   cfun->last_verified = flags & TODO_verify_all;
1272 }
1273
1274 /* Perform all TODO actions.  */
1275 static void
1276 execute_todo (unsigned int flags)
1277 {
1278 #if defined ENABLE_CHECKING
1279   if (cfun
1280       && need_ssa_update_p (cfun))
1281     gcc_assert (flags & TODO_update_ssa_any);
1282 #endif
1283
1284   /* Inform the pass whether it is the first time it is run.  */
1285   first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1286
1287   do_per_function (execute_function_todo, (void *)(size_t) flags);
1288
1289   /* Always remove functions just as before inlining: IPA passes might be
1290      interested to see bodies of extern inline functions that are not inlined
1291      to analyze side effects.  The full removal is done just at the end
1292      of IPA pass queue.  */
1293   if (flags & TODO_remove_functions)
1294     {
1295       gcc_assert (!cfun);
1296       cgraph_remove_unreachable_nodes (true, dump_file);
1297     }
1298
1299   if ((flags & TODO_dump_cgraph) && dump_file && !current_function_decl)
1300     {
1301       gcc_assert (!cfun);
1302       dump_cgraph (dump_file);
1303       /* Flush the file.  If verification fails, we won't be able to
1304          close the file before aborting.  */
1305       fflush (dump_file);
1306     }
1307
1308   if (flags & TODO_ggc_collect)
1309     ggc_collect ();
1310
1311   /* Now that the dumping has been done, we can get rid of the optional
1312      df problems.  */
1313   if (flags & TODO_df_finish)
1314     df_finish_pass ((flags & TODO_df_verify) != 0);
1315 }
1316
1317 /* Verify invariants that should hold between passes.  This is a place
1318    to put simple sanity checks.  */
1319
1320 static void
1321 verify_interpass_invariants (void)
1322 {
1323 #ifdef ENABLE_CHECKING
1324   gcc_assert (!fold_deferring_overflow_warnings_p ());
1325 #endif
1326 }
1327
1328 /* Clear the last verified flag.  */
1329
1330 static void
1331 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1332 {
1333   cfun->last_verified = 0;
1334 }
1335
1336 /* Helper function. Verify that the properties has been turn into the
1337    properties expected by the pass.  */
1338
1339 #ifdef ENABLE_CHECKING
1340 static void
1341 verify_curr_properties (void *data)
1342 {
1343   unsigned int props = (size_t)data;
1344   gcc_assert ((cfun->curr_properties & props) == props);
1345 }
1346 #endif
1347
1348 /* Initialize pass dump file.  */
1349 /* This is non-static so that the plugins can use it.  */
1350
1351 bool
1352 pass_init_dump_file (struct opt_pass *pass)
1353 {
1354   /* If a dump file name is present, open it if enabled.  */
1355   if (pass->static_pass_number != -1)
1356     {
1357       bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
1358       dump_file_name = get_dump_file_name (pass->static_pass_number);
1359       dump_file = dump_begin (pass->static_pass_number, &dump_flags);
1360       if (dump_file && current_function_decl)
1361         {
1362           const char *dname, *aname;
1363           dname = lang_hooks.decl_printable_name (current_function_decl, 2);
1364           aname = (IDENTIFIER_POINTER
1365                    (DECL_ASSEMBLER_NAME (current_function_decl)));
1366           fprintf (dump_file, "\n;; Function %s (%s)%s\n\n", dname, aname,
1367              cfun->function_frequency == FUNCTION_FREQUENCY_HOT
1368              ? " (hot)"
1369              : cfun->function_frequency == FUNCTION_FREQUENCY_UNLIKELY_EXECUTED
1370              ? " (unlikely executed)"
1371              : "");
1372         }
1373       return initializing_dump;
1374     }
1375   else
1376     return false;
1377 }
1378
1379 /* Flush PASS dump file.  */
1380 /* This is non-static so that plugins can use it.  */
1381
1382 void
1383 pass_fini_dump_file (struct opt_pass *pass)
1384 {
1385   /* Flush and close dump file.  */
1386   if (dump_file_name)
1387     {
1388       free (CONST_CAST (char *, dump_file_name));
1389       dump_file_name = NULL;
1390     }
1391
1392   if (dump_file)
1393     {
1394       dump_end (pass->static_pass_number, dump_file);
1395       dump_file = NULL;
1396     }
1397 }
1398
1399 /* After executing the pass, apply expected changes to the function
1400    properties. */
1401
1402 static void
1403 update_properties_after_pass (void *data)
1404 {
1405   struct opt_pass *pass = (struct opt_pass *) data;
1406   cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1407                            & ~pass->properties_destroyed;
1408 }
1409
1410 /* Execute summary generation for all of the passes in IPA_PASS.  */
1411
1412 void
1413 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1414 {
1415   while (ipa_pass)
1416     {
1417       struct opt_pass *pass = &ipa_pass->pass;
1418
1419       /* Execute all of the IPA_PASSes in the list.  */
1420       if (ipa_pass->pass.type == IPA_PASS
1421           && (!pass->gate || pass->gate ())
1422           && ipa_pass->generate_summary)
1423         {
1424           pass_init_dump_file (pass);
1425
1426           /* If a timevar is present, start it.  */
1427           if (pass->tv_id)
1428             timevar_push (pass->tv_id);
1429
1430           ipa_pass->generate_summary ();
1431
1432           /* Stop timevar.  */
1433           if (pass->tv_id)
1434             timevar_pop (pass->tv_id);
1435
1436           pass_fini_dump_file (pass);
1437         }
1438       ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
1439     }
1440 }
1441
1442 /* Execute IPA_PASS function transform on NODE.  */
1443
1444 static void
1445 execute_one_ipa_transform_pass (struct cgraph_node *node,
1446                                 struct ipa_opt_pass_d *ipa_pass)
1447 {
1448   struct opt_pass *pass = &ipa_pass->pass;
1449   unsigned int todo_after = 0;
1450
1451   current_pass = pass;
1452   if (!ipa_pass->function_transform)
1453     return;
1454
1455   /* Note that the folders should only create gimple expressions.
1456      This is a hack until the new folder is ready.  */
1457   in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1458
1459   pass_init_dump_file (pass);
1460
1461   /* Run pre-pass verification.  */
1462   execute_todo (ipa_pass->function_transform_todo_flags_start);
1463
1464   /* If a timevar is present, start it.  */
1465   if (pass->tv_id != TV_NONE)
1466     timevar_push (pass->tv_id);
1467
1468   /* Do it!  */
1469   todo_after = ipa_pass->function_transform (node);
1470
1471   /* Stop timevar.  */
1472   if (pass->tv_id != TV_NONE)
1473     timevar_pop (pass->tv_id);
1474
1475   /* Run post-pass cleanup and verification.  */
1476   execute_todo (todo_after);
1477   verify_interpass_invariants ();
1478
1479   pass_fini_dump_file (pass);
1480
1481   current_pass = NULL;
1482 }
1483
1484 /* For the current function, execute all ipa transforms. */
1485
1486 void
1487 execute_all_ipa_transforms (void)
1488 {
1489   struct cgraph_node *node;
1490   if (!cfun)
1491     return;
1492   node = cgraph_node (current_function_decl);
1493   if (node->ipa_transforms_to_apply)
1494     {
1495       unsigned int i;
1496
1497       for (i = 0; i < VEC_length (ipa_opt_pass, node->ipa_transforms_to_apply);
1498            i++)
1499         execute_one_ipa_transform_pass (node,
1500                                         VEC_index (ipa_opt_pass,
1501                                                    node->ipa_transforms_to_apply,
1502                                                    i));
1503       VEC_free (ipa_opt_pass, heap, node->ipa_transforms_to_apply);
1504       node->ipa_transforms_to_apply = NULL;
1505     }
1506 }
1507
1508 /* Execute PASS. */
1509
1510 bool
1511 execute_one_pass (struct opt_pass *pass)
1512 {
1513   bool initializing_dump;
1514   unsigned int todo_after = 0;
1515
1516   bool gate_status;
1517
1518   /* IPA passes are executed on whole program, so cfun should be NULL.
1519      Other passes need function context set.  */
1520   if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
1521     gcc_assert (!cfun && !current_function_decl);
1522   else
1523     gcc_assert (cfun && current_function_decl);
1524
1525   current_pass = pass;
1526
1527   /* Check whether gate check should be avoided.
1528      User controls the value of the gate through the parameter "gate_status". */
1529   gate_status = (pass->gate == NULL) ? true : pass->gate();
1530
1531   /* Override gate with plugin.  */
1532   invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
1533
1534   if (!gate_status)
1535     {
1536       current_pass = NULL;
1537       return false;
1538     }
1539
1540   /* Pass execution event trigger: useful to identify passes being
1541      executed.  */
1542   invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
1543
1544   if (!quiet_flag && !cfun)
1545     fprintf (stderr, " <%s>", pass->name ? pass->name : "");
1546
1547   /* Note that the folders should only create gimple expressions.
1548      This is a hack until the new folder is ready.  */
1549   in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1550
1551   initializing_dump = pass_init_dump_file (pass);
1552
1553   /* Run pre-pass verification.  */
1554   execute_todo (pass->todo_flags_start);
1555
1556 #ifdef ENABLE_CHECKING
1557   do_per_function (verify_curr_properties,
1558                    (void *)(size_t)pass->properties_required);
1559 #endif
1560
1561   /* If a timevar is present, start it.  */
1562   if (pass->tv_id != TV_NONE)
1563     timevar_push (pass->tv_id);
1564
1565   /* Do it!  */
1566   if (pass->execute)
1567     {
1568       todo_after = pass->execute ();
1569       do_per_function (clear_last_verified, NULL);
1570     }
1571
1572   /* Stop timevar.  */
1573   if (pass->tv_id != TV_NONE)
1574     timevar_pop (pass->tv_id);
1575
1576   do_per_function (update_properties_after_pass, pass);
1577
1578   if (initializing_dump
1579       && dump_file
1580       && graph_dump_format != no_graph
1581       && cfun
1582       && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
1583           == (PROP_cfg | PROP_rtl))
1584     {
1585       get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
1586       dump_flags |= TDF_GRAPH;
1587       clean_graph_dump_file (dump_file_name);
1588     }
1589
1590   /* Run post-pass cleanup and verification.  */
1591   execute_todo (todo_after | pass->todo_flags_finish);
1592   verify_interpass_invariants ();
1593   if (pass->type == IPA_PASS)
1594     {
1595       struct cgraph_node *node;
1596       for (node = cgraph_nodes; node; node = node->next)
1597         if (node->analyzed)
1598           VEC_safe_push (ipa_opt_pass, heap, node->ipa_transforms_to_apply,
1599                          (struct ipa_opt_pass_d *)pass);
1600     }
1601
1602   if (!current_function_decl)
1603     cgraph_process_new_functions ();
1604
1605   pass_fini_dump_file (pass);
1606
1607   if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
1608     gcc_assert (!(cfun->curr_properties & PROP_trees)
1609                 || pass->type != RTL_PASS);
1610
1611   current_pass = NULL;
1612
1613   return true;
1614 }
1615
1616 void
1617 execute_pass_list (struct opt_pass *pass)
1618 {
1619   do
1620     {
1621       gcc_assert (pass->type == GIMPLE_PASS
1622                   || pass->type == RTL_PASS);
1623       if (execute_one_pass (pass) && pass->sub)
1624         execute_pass_list (pass->sub);
1625       pass = pass->next;
1626     }
1627   while (pass);
1628 }
1629
1630 /* Same as execute_pass_list but assume that subpasses of IPA passes
1631    are local passes. If SET is not NULL, write out summaries of only
1632    those node in SET. */
1633
1634 static void
1635 ipa_write_summaries_2 (struct opt_pass *pass, cgraph_node_set set,
1636                        struct lto_out_decl_state *state)
1637 {
1638   while (pass)
1639     {
1640       struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
1641       gcc_assert (!current_function_decl);
1642       gcc_assert (!cfun);
1643       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1644       if (pass->type == IPA_PASS
1645           && ipa_pass->write_summary
1646           && (!pass->gate || pass->gate ()))
1647         {
1648           /* If a timevar is present, start it.  */
1649           if (pass->tv_id)
1650             timevar_push (pass->tv_id);
1651
1652           ipa_pass->write_summary (set);
1653
1654           /* If a timevar is present, start it.  */
1655           if (pass->tv_id)
1656             timevar_pop (pass->tv_id);
1657         }
1658
1659       if (pass->sub && pass->sub->type != GIMPLE_PASS)
1660         ipa_write_summaries_2 (pass->sub, set, state);
1661
1662       pass = pass->next;
1663     }
1664 }
1665
1666 /* Helper function of ipa_write_summaries. Creates and destroys the
1667    decl state and calls ipa_write_summaries_2 for all passes that have
1668    summaries.  SET is the set of nodes to be written.  */
1669
1670 static void
1671 ipa_write_summaries_1 (cgraph_node_set set)
1672 {
1673   struct lto_out_decl_state *state = lto_new_out_decl_state ();
1674   lto_push_out_decl_state (state);
1675
1676   if (!flag_wpa)
1677     ipa_write_summaries_2 (all_regular_ipa_passes, set, state);
1678   ipa_write_summaries_2 (all_lto_gen_passes, set, state);
1679
1680   gcc_assert (lto_get_out_decl_state () == state);
1681   lto_pop_out_decl_state ();
1682   lto_delete_out_decl_state (state);
1683 }
1684
1685 /* Write out summaries for all the nodes in the callgraph.  */
1686
1687 void
1688 ipa_write_summaries (void)
1689 {
1690   cgraph_node_set set;
1691   struct cgraph_node **order;
1692   int i, order_pos;
1693
1694   if (!flag_generate_lto || errorcount || sorrycount)
1695     return;
1696
1697   lto_new_extern_inline_states ();
1698   set = cgraph_node_set_new ();
1699
1700   /* Create the callgraph set in the same order used in
1701      cgraph_expand_all_functions.  This mostly facilitates debugging,
1702      since it causes the gimple file to be processed in the same order
1703      as the source code.  */
1704   order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1705   order_pos = cgraph_postorder (order);
1706   gcc_assert (order_pos == cgraph_n_nodes);
1707
1708   for (i = order_pos - 1; i >= 0; i--)
1709     {
1710       struct cgraph_node *node = order[i];
1711
1712       if (node->analyzed)
1713         {
1714           /* When streaming out references to statements as part of some IPA
1715              pass summary, the statements need to have uids assigned and the
1716              following does that for all the IPA passes here. Naturally, this
1717              ordering then matches the one IPA-passes get in their stmt_fixup
1718              hooks.  */
1719
1720           push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1721           renumber_gimple_stmt_uids ();
1722           pop_cfun ();
1723         }
1724       cgraph_node_set_add (set, node);
1725     }
1726
1727   ipa_write_summaries_1 (set);
1728   lto_delete_extern_inline_states ();
1729
1730   free (order);
1731   ggc_free (set);
1732 }
1733
1734
1735 /* Write all the summaries for the cgraph nodes in SET.  If SET is
1736    NULL, write out all summaries of all nodes. */
1737
1738 void
1739 ipa_write_summaries_of_cgraph_node_set (cgraph_node_set set)
1740 {
1741   if (flag_generate_lto && !(errorcount || sorrycount))
1742     ipa_write_summaries_1 (set);
1743 }
1744
1745 /* Same as execute_pass_list but assume that subpasses of IPA passes
1746    are local passes.  */
1747
1748 static void
1749 ipa_read_summaries_1 (struct opt_pass *pass)
1750 {
1751   while (pass)
1752     {
1753       struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
1754
1755       gcc_assert (!current_function_decl);
1756       gcc_assert (!cfun);
1757       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1758
1759       if (pass->gate == NULL || pass->gate ())
1760         {
1761           if (pass->type == IPA_PASS && ipa_pass->read_summary)
1762             {
1763               /* If a timevar is present, start it.  */
1764               if (pass->tv_id)
1765                 timevar_push (pass->tv_id);
1766
1767               ipa_pass->read_summary ();
1768
1769               /* Stop timevar.  */
1770               if (pass->tv_id)
1771                 timevar_pop (pass->tv_id);
1772             }
1773
1774           if (pass->sub && pass->sub->type != GIMPLE_PASS)
1775             ipa_read_summaries_1 (pass->sub);
1776         }
1777       pass = pass->next;
1778     }
1779 }
1780
1781
1782 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes.  */
1783
1784 void
1785 ipa_read_summaries (void)
1786 {
1787   if (!flag_ltrans)
1788     ipa_read_summaries_1 (all_regular_ipa_passes);
1789   ipa_read_summaries_1 (all_lto_gen_passes);
1790 }
1791
1792 /* Same as execute_pass_list but assume that subpasses of IPA passes
1793    are local passes.  */
1794 void
1795 execute_ipa_pass_list (struct opt_pass *pass)
1796 {
1797   do
1798     {
1799       gcc_assert (!current_function_decl);
1800       gcc_assert (!cfun);
1801       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1802       if (execute_one_pass (pass) && pass->sub)
1803         {
1804           if (pass->sub->type == GIMPLE_PASS)
1805             {
1806               invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
1807               do_per_function_toporder ((void (*)(void *))execute_pass_list,
1808                                         pass->sub);
1809               invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
1810             }
1811           else if (pass->sub->type == SIMPLE_IPA_PASS
1812                    || pass->sub->type == IPA_PASS)
1813             execute_ipa_pass_list (pass->sub);
1814           else
1815             gcc_unreachable ();
1816         }
1817       gcc_assert (!current_function_decl);
1818       cgraph_process_new_functions ();
1819       pass = pass->next;
1820     }
1821   while (pass);
1822 }
1823
1824 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS.  */
1825
1826 static void
1827 execute_ipa_stmt_fixups (struct opt_pass *pass,
1828                           struct cgraph_node *node, gimple *stmts)
1829 {
1830   while (pass)
1831     {
1832       /* Execute all of the IPA_PASSes in the list.  */
1833       if (pass->type == IPA_PASS
1834           && (!pass->gate || pass->gate ()))
1835         {
1836           struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
1837
1838           if (ipa_pass->stmt_fixup)
1839             {
1840               pass_init_dump_file (pass);
1841               /* If a timevar is present, start it.  */
1842               if (pass->tv_id)
1843                 timevar_push (pass->tv_id);
1844
1845               ipa_pass->stmt_fixup (node, stmts);
1846
1847               /* Stop timevar.  */
1848               if (pass->tv_id)
1849                 timevar_pop (pass->tv_id);
1850               pass_fini_dump_file (pass);
1851             }
1852           if (pass->sub)
1853             execute_ipa_stmt_fixups (pass->sub, node, stmts);
1854         }
1855       pass = pass->next;
1856     }
1857 }
1858
1859 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS.  */
1860
1861 void
1862 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
1863 {
1864   execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
1865 }
1866
1867
1868 extern void debug_properties (unsigned int);
1869 extern void dump_properties (FILE *, unsigned int);
1870
1871 void
1872 dump_properties (FILE *dump, unsigned int props)
1873 {
1874   fprintf (dump, "Properties:\n");
1875   if (props & PROP_gimple_any)
1876     fprintf (dump, "PROP_gimple_any\n");
1877   if (props & PROP_gimple_lcf)
1878     fprintf (dump, "PROP_gimple_lcf\n");
1879   if (props & PROP_gimple_leh)
1880     fprintf (dump, "PROP_gimple_leh\n");
1881   if (props & PROP_cfg)
1882     fprintf (dump, "PROP_cfg\n");
1883   if (props & PROP_referenced_vars)
1884     fprintf (dump, "PROP_referenced_vars\n");
1885   if (props & PROP_ssa)
1886     fprintf (dump, "PROP_ssa\n");
1887   if (props & PROP_no_crit_edges)
1888     fprintf (dump, "PROP_no_crit_edges\n");
1889   if (props & PROP_rtl)
1890     fprintf (dump, "PROP_rtl\n");
1891   if (props & PROP_gimple_lomp)
1892     fprintf (dump, "PROP_gimple_lomp\n");
1893   if (props & PROP_gimple_lcx)
1894     fprintf (dump, "PROP_gimple_lcx\n");
1895 }
1896
1897 void
1898 debug_properties (unsigned int props)
1899 {
1900   dump_properties (stderr, props);
1901 }
1902
1903 /* Called by local passes to see if function is called by already processed nodes.
1904    Because we process nodes in topological order, this means that function is
1905    in recursive cycle or we introduced new direct calls.  */
1906 bool
1907 function_called_by_processed_nodes_p (void)
1908 {
1909   struct cgraph_edge *e;
1910   for (e = cgraph_node (current_function_decl)->callers; e; e = e->next_caller)
1911     {
1912       if (e->caller->decl == current_function_decl)
1913         continue;
1914       if (!e->caller->analyzed)
1915         continue;
1916       if (TREE_ASM_WRITTEN (e->caller->decl))
1917         continue;
1918       if (!e->caller->process && !e->caller->global.inlined_to)
1919         break;
1920     }
1921   if (dump_file && e)
1922     {
1923       fprintf (dump_file, "Already processed call to:\n");
1924       dump_cgraph_node (dump_file, e->caller);
1925     }
1926   return e != NULL;
1927 }
1928
1929 #include "gt-passes.h"