OSDN Git Service

Revert r174848,174849
[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 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "line-map.h"
32 #include "input.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "insn-attr.h"
38 #include "insn-config.h"
39 #include "insn-flags.h"
40 #include "hard-reg-set.h"
41 #include "recog.h"
42 #include "output.h"
43 #include "except.h"
44 #include "function.h"
45 #include "toplev.h"
46 #include "expr.h"
47 #include "basic-block.h"
48 #include "intl.h"
49 #include "ggc.h"
50 #include "graph.h"
51 #include "regs.h"
52 #include "timevar.h"
53 #include "diagnostic-core.h"
54 #include "params.h"
55 #include "reload.h"
56 #include "dwarf2asm.h"
57 #include "integrate.h"
58 #include "debug.h"
59 #include "target.h"
60 #include "langhooks.h"
61 #include "cfglayout.h"
62 #include "cfgloop.h"
63 #include "hosthooks.h"
64 #include "cgraph.h"
65 #include "opts.h"
66 #include "coverage.h"
67 #include "value-prof.h"
68 #include "tree-inline.h"
69 #include "tree-flow.h"
70 #include "tree-pass.h"
71 #include "tree-dump.h"
72 #include "df.h"
73 #include "predict.h"
74 #include "lto-streamer.h"
75 #include "plugin.h"
76 #include "ipa-utils.h"
77 #include "tree-pretty-print.h"
78
79 #if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
80 #include "dwarf2out.h"
81 #endif
82
83 #if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
84 #include "dbxout.h"
85 #endif
86
87 #ifdef SDB_DEBUGGING_INFO
88 #include "sdbout.h"
89 #endif
90
91 #ifdef XCOFF_DEBUGGING_INFO
92 #include "xcoffout.h"           /* Needed for external data
93                                    declarations for e.g. AIX 4.x.  */
94 #endif
95
96 /* This is used for debugging.  It allows the current pass to printed
97    from anywhere in compilation.
98    The variable current_pass is also used for statistics and plugins.  */
99 struct opt_pass *current_pass;
100
101 static void register_pass_name (struct opt_pass *, const char *);
102
103 /* Call from anywhere to find out what pass this is.  Useful for
104    printing out debugging information deep inside an service
105    routine.  */
106 void
107 print_current_pass (FILE *file)
108 {
109   if (current_pass)
110     fprintf (file, "current pass = %s (%d)\n",
111              current_pass->name, current_pass->static_pass_number);
112   else
113     fprintf (file, "no current pass.\n");
114 }
115
116
117 /* Call from the debugger to get the current pass name.  */
118 DEBUG_FUNCTION void
119 debug_pass (void)
120 {
121   print_current_pass (stderr);
122 }
123
124
125
126 /* Global variables used to communicate with passes.  */
127 int dump_flags;
128 bool in_gimple_form;
129 bool first_pass_instance;
130
131
132 /* This is called from various places for FUNCTION_DECL, VAR_DECL,
133    and TYPE_DECL nodes.
134
135    This does nothing for local (non-static) variables, unless the
136    variable is a register variable with DECL_ASSEMBLER_NAME set.  In
137    that case, or if the variable is not an automatic, it sets up the
138    RTL and outputs any assembler code (label definition, storage
139    allocation and initialization).
140
141    DECL is the declaration.  TOP_LEVEL is nonzero
142    if this declaration is not within a function.  */
143
144 void
145 rest_of_decl_compilation (tree decl,
146                           int top_level,
147                           int at_end)
148 {
149   /* We deferred calling assemble_alias so that we could collect
150      other attributes such as visibility.  Emit the alias now.  */
151   if (!in_lto_p)
152   {
153     tree alias;
154     alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
155     if (alias)
156       {
157         alias = TREE_VALUE (TREE_VALUE (alias));
158         alias = get_identifier (TREE_STRING_POINTER (alias));
159         assemble_alias (decl, alias);
160       }
161   }
162
163   /* Can't defer this, because it needs to happen before any
164      later function definitions are processed.  */
165   if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
166     make_decl_rtl (decl);
167
168   /* Forward declarations for nested functions are not "external",
169      but we need to treat them as if they were.  */
170   if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
171       || TREE_CODE (decl) == FUNCTION_DECL)
172     {
173       timevar_push (TV_VARCONST);
174
175       /* Don't output anything when a tentative file-scope definition
176          is seen.  But at end of compilation, do output code for them.
177
178          We do output all variables and rely on
179          callgraph code to defer them except for forward declarations
180          (see gcc.c-torture/compile/920624-1.c) */
181       if ((at_end
182            || !DECL_DEFER_OUTPUT (decl)
183            || DECL_INITIAL (decl))
184           && !DECL_EXTERNAL (decl))
185         {
186           /* When reading LTO unit, we also read varpool, so do not
187              rebuild it.  */
188           if (in_lto_p && !at_end)
189             ;
190           else if (TREE_CODE (decl) != FUNCTION_DECL)
191             varpool_finalize_decl (decl);
192         }
193
194 #ifdef ASM_FINISH_DECLARE_OBJECT
195       if (decl == last_assemble_variable_decl)
196         {
197           ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
198                                      top_level, at_end);
199         }
200 #endif
201
202       timevar_pop (TV_VARCONST);
203     }
204   else if (TREE_CODE (decl) == TYPE_DECL
205            /* Like in rest_of_type_compilation, avoid confusing the debug
206               information machinery when there are errors.  */
207            && !seen_error ())
208     {
209       timevar_push (TV_SYMOUT);
210       debug_hooks->type_decl (decl, !top_level);
211       timevar_pop (TV_SYMOUT);
212     }
213
214   /* Let cgraph know about the existence of variables.  */
215   if (in_lto_p && !at_end)
216     ;
217   else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl)
218            && TREE_STATIC (decl))
219     varpool_node (decl);
220 }
221
222 /* Called after finishing a record, union or enumeral type.  */
223
224 void
225 rest_of_type_compilation (tree type, int toplev)
226 {
227   /* Avoid confusing the debug information machinery when there are
228      errors.  */
229   if (seen_error ())
230     return;
231
232   timevar_push (TV_SYMOUT);
233   debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
234   timevar_pop (TV_SYMOUT);
235 }
236
237 \f
238
239 void
240 finish_optimization_passes (void)
241 {
242   int i;
243   struct dump_file_info *dfi;
244   char *name;
245
246   timevar_push (TV_DUMP);
247   if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
248     {
249       dump_file = dump_begin (pass_profile.pass.static_pass_number, NULL);
250       end_branch_prob ();
251       if (dump_file)
252         dump_end (pass_profile.pass.static_pass_number, dump_file);
253     }
254
255   if (optimize > 0)
256     {
257       dump_file = dump_begin (pass_combine.pass.static_pass_number, NULL);
258       if (dump_file)
259         {
260           dump_combine_total_stats (dump_file);
261           dump_end (pass_combine.pass.static_pass_number, dump_file);
262         }
263     }
264
265   /* Do whatever is necessary to finish printing the graphs.  */
266   if (graph_dump_format != no_graph)
267     for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
268       if (dump_initialized_p (i)
269           && (dfi->flags & TDF_GRAPH) != 0
270           && (name = get_dump_file_name (i)) != NULL)
271         {
272           finish_graph_dump_file (name);
273           free (name);
274         }
275
276   timevar_pop (TV_DUMP);
277 }
278
279 static bool
280 gate_rest_of_compilation (void)
281 {
282   /* Early return if there were errors.  We can run afoul of our
283      consistency checks, and there's not really much point in fixing them.  */
284   return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
285 }
286
287 struct gimple_opt_pass pass_rest_of_compilation =
288 {
289  {
290   GIMPLE_PASS,
291   "*rest_of_compilation",               /* name */
292   gate_rest_of_compilation,             /* gate */
293   NULL,                                 /* execute */
294   NULL,                                 /* sub */
295   NULL,                                 /* next */
296   0,                                    /* static_pass_number */
297   TV_REST_OF_COMPILATION,               /* tv_id */
298   PROP_rtl,                             /* properties_required */
299   0,                                    /* properties_provided */
300   0,                                    /* properties_destroyed */
301   0,                                    /* todo_flags_start */
302   TODO_ggc_collect                      /* todo_flags_finish */
303  }
304 };
305
306 static bool
307 gate_postreload (void)
308 {
309   return reload_completed;
310 }
311
312 struct rtl_opt_pass pass_postreload =
313 {
314  {
315   RTL_PASS,
316   "*all-postreload",                        /* name */
317   gate_postreload,                      /* gate */
318   NULL,                                 /* execute */
319   NULL,                                 /* sub */
320   NULL,                                 /* next */
321   0,                                    /* static_pass_number */
322   TV_POSTRELOAD,                        /* tv_id */
323   PROP_rtl,                             /* properties_required */
324   0,                                    /* properties_provided */
325   0,                                    /* properties_destroyed */
326   0,                                    /* todo_flags_start */
327   TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
328  }
329 };
330
331
332
333 /* The root of the compilation pass tree, once constructed.  */
334 struct opt_pass *all_passes, *all_small_ipa_passes, *all_lowering_passes,
335   *all_regular_ipa_passes, *all_lto_gen_passes;
336
337 /* This is used by plugins, and should also be used in register_pass.  */
338 #define DEF_PASS_LIST(LIST) &LIST,
339 struct opt_pass **gcc_pass_lists[] = { GCC_PASS_LISTS NULL };
340 #undef DEF_PASS_LIST
341
342 /* A map from static pass id to optimization pass.  */
343 struct opt_pass **passes_by_id;
344 int passes_by_id_size;
345
346 /* Set the static pass number of pass PASS to ID and record that
347    in the mapping from static pass number to pass.  */
348
349 static void
350 set_pass_for_id (int id, struct opt_pass *pass)
351 {
352   pass->static_pass_number = id;
353   if (passes_by_id_size <= id)
354     {
355       passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
356       memset (passes_by_id + passes_by_id_size, 0,
357               (id + 1 - passes_by_id_size) * sizeof (void *));
358       passes_by_id_size = id + 1;
359     }
360   passes_by_id[id] = pass;
361 }
362
363 /* Return the pass with the static pass number ID.  */
364
365 struct opt_pass *
366 get_pass_for_id (int id)
367 {
368   if (id >= passes_by_id_size)
369     return NULL;
370   return passes_by_id[id];
371 }
372
373 /* Iterate over the pass tree allocating dump file numbers.  We want
374    to do this depth first, and independent of whether the pass is
375    enabled or not.  */
376
377 void
378 register_one_dump_file (struct opt_pass *pass)
379 {
380   char *dot_name, *flag_name, *glob_name;
381   const char *name, *full_name, *prefix;
382   char num[10];
383   int flags, id;
384
385   /* See below in next_pass_1.  */
386   num[0] = '\0';
387   if (pass->static_pass_number != -1)
388     sprintf (num, "%d", ((int) pass->static_pass_number < 0
389                          ? 1 : pass->static_pass_number));
390
391   /* The name is both used to identify the pass for the purposes of plugins,
392      and to specify dump file name and option.
393      The latter two might want something short which is not quite unique; for
394      that reason, we may have a disambiguating prefix, followed by a space
395      to mark the start of the following dump file name / option string.  */
396   name = strchr (pass->name, ' ');
397   name = name ? name + 1 : pass->name;
398   dot_name = concat (".", name, num, NULL);
399   if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
400     prefix = "ipa-", flags = TDF_IPA;
401   else if (pass->type == GIMPLE_PASS)
402     prefix = "tree-", flags = TDF_TREE;
403   else
404     prefix = "rtl-", flags = TDF_RTL;
405
406   flag_name = concat (prefix, name, num, NULL);
407   glob_name = concat (prefix, name, NULL);
408   id = dump_register (dot_name, flag_name, glob_name, flags);
409   set_pass_for_id (id, pass);
410   full_name = concat (prefix, pass->name, num, NULL);
411   register_pass_name (pass, full_name);
412 }
413
414 /* Recursive worker function for register_dump_files.  */
415
416 static int
417 register_dump_files_1 (struct opt_pass *pass, int properties)
418 {
419   do
420     {
421       int new_properties = (properties | pass->properties_provided)
422                            & ~pass->properties_destroyed;
423
424       if (pass->name && pass->name[0] != '*')
425         register_one_dump_file (pass);
426
427       if (pass->sub)
428         new_properties = register_dump_files_1 (pass->sub, new_properties);
429
430       /* If we have a gate, combine the properties that we could have with
431          and without the pass being examined.  */
432       if (pass->gate)
433         properties &= new_properties;
434       else
435         properties = new_properties;
436
437       pass = pass->next;
438     }
439   while (pass);
440
441   return properties;
442 }
443
444 /* Register the dump files for the pipeline starting at PASS.
445    PROPERTIES reflects the properties that are guaranteed to be available at
446    the beginning of the pipeline.  */
447
448 static void
449 register_dump_files (struct opt_pass *pass,int properties)
450 {
451   pass->properties_required |= properties;
452   register_dump_files_1 (pass, properties);
453 }
454
455 struct pass_registry
456 {
457   const char* unique_name;
458   struct opt_pass *pass;
459 };
460
461 /* Pass registry hash function.  */
462
463 static hashval_t
464 passr_hash (const void *p)
465 {
466   const struct pass_registry *const s = (const struct pass_registry *const) p;
467   return htab_hash_string (s->unique_name);
468 }
469
470 /* Hash equal function  */
471
472 static int
473 passr_eq (const void *p1, const void *p2)
474 {
475   const struct pass_registry *const s1 = (const struct pass_registry *const) p1;
476   const struct pass_registry *const s2 = (const struct pass_registry *const) p2;
477
478   return !strcmp (s1->unique_name, s2->unique_name);
479 }
480
481 static htab_t pass_name_tab = NULL;
482
483 /* Register PASS with NAME.  */
484
485 static void
486 register_pass_name (struct opt_pass *pass, const char *name)
487 {
488   struct pass_registry **slot;
489   struct pass_registry pr;
490
491   if (!pass_name_tab)
492     pass_name_tab = htab_create (256, passr_hash, passr_eq, NULL);
493
494   pr.unique_name = name;
495   slot = (struct pass_registry **) htab_find_slot (pass_name_tab, &pr, INSERT);
496   if (!*slot)
497     {
498       struct pass_registry *new_pr;
499
500       new_pr = XCNEW (struct pass_registry);
501       new_pr->unique_name = xstrdup (name);
502       new_pr->pass = pass;
503       *slot = new_pr;
504     }
505   else
506     return; /* Ignore plugin passes.  */
507 }
508
509 /* Returns the pass with NAME.  */
510
511 static struct opt_pass *
512 get_pass_by_name (const char *name)
513 {
514   struct pass_registry **slot, pr;
515
516   gcc_assert (pass_name_tab);
517   pr.unique_name = name;
518   slot = (struct pass_registry **) htab_find_slot (pass_name_tab,
519                                                    &pr, NO_INSERT);
520
521   if (!slot || !*slot)
522     return NULL;
523
524   return (*slot)->pass;
525 }
526
527
528 /* Range [start, last].  */
529
530 struct uid_range
531 {
532   unsigned int start;
533   unsigned int last;
534   const char *assem_name;
535   struct uid_range *next;
536 };
537
538 typedef struct uid_range *uid_range_p;
539
540 DEF_VEC_P(uid_range_p);
541 DEF_VEC_ALLOC_P(uid_range_p, heap);
542
543 static VEC(uid_range_p, heap) *enabled_pass_uid_range_tab = NULL;
544 static VEC(uid_range_p, heap) *disabled_pass_uid_range_tab = NULL;
545
546
547 /* Parse option string for -fdisable- and -fenable-
548    The syntax of the options:
549
550    -fenable-<pass_name>
551    -fdisable-<pass_name>
552
553    -fenable-<pass_name>=s1:e1,s2:e2,...
554    -fdisable-<pass_name>=s1:e1,s2:e2,...
555 */
556
557 static void
558 enable_disable_pass (const char *arg, bool is_enable)
559 {
560   struct opt_pass *pass;
561   char *range_str, *phase_name;
562   char *argstr = xstrdup (arg);
563   VEC(uid_range_p, heap) **tab = 0;
564
565   range_str = strchr (argstr,'=');
566   if (range_str)
567     {
568       *range_str = '\0';
569       range_str++;
570     }
571
572   phase_name = argstr;
573   if (!*phase_name)
574     {
575       if (is_enable)
576         error ("unrecognized option -fenable");
577       else
578         error ("unrecognized option -fdisable");
579       free (argstr);
580       return;
581     }
582   pass = get_pass_by_name (phase_name);
583   if (!pass || pass->static_pass_number == -1)
584     {
585       if (is_enable)
586         error ("unknown pass %s specified in -fenable", phase_name);
587       else
588         error ("unknown pass %s specified in -fdisble", phase_name);
589       free (argstr);
590       return;
591     }
592
593   if (is_enable)
594     tab = &enabled_pass_uid_range_tab;
595   else
596     tab = &disabled_pass_uid_range_tab;
597
598   if ((unsigned) pass->static_pass_number >= VEC_length (uid_range_p, *tab))
599     VEC_safe_grow_cleared (uid_range_p, heap,
600                            *tab, pass->static_pass_number + 1);
601
602   if (!range_str)
603     {
604       uid_range_p slot;
605       uid_range_p new_range = XCNEW (struct uid_range);
606
607       new_range->start = 0;
608       new_range->last = (unsigned)-1;
609
610       slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
611       new_range->next = slot;
612       VEC_replace (uid_range_p, *tab, pass->static_pass_number,
613                    new_range);
614       if (is_enable)
615         inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
616                 "of [%u, %u]", phase_name, new_range->start, new_range->last);
617       else
618         inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
619                 "of [%u, %u]", phase_name, new_range->start, new_range->last);
620     }
621   else
622     {
623       char *next_range = NULL;
624       char *one_range = range_str;
625       char *end_val = NULL;
626
627       do
628         {
629           uid_range_p slot;
630           uid_range_p new_range;
631           char *invalid = NULL;
632           long start;
633           char *func_name = NULL;
634
635           next_range = strchr (one_range, ',');
636           if (next_range)
637             {
638               *next_range = '\0';
639               next_range++;
640             }
641
642           end_val = strchr (one_range, ':');
643           if (end_val)
644             {
645               *end_val = '\0';
646               end_val++;
647             }
648           start = strtol (one_range, &invalid, 10);
649           if (*invalid || start < 0)
650             {
651               if (end_val || (one_range[0] >= '0'
652                               && one_range[0] <= '9'))
653                 {
654                   error ("Invalid range %s in option %s",
655                          one_range,
656                          is_enable ? "-fenable" : "-fdisable");
657                   free (argstr);
658                   return;
659                 }
660               func_name = one_range;
661             }
662           if (!end_val)
663             {
664               new_range = XCNEW (struct uid_range);
665               if (!func_name)
666                 {
667                   new_range->start = (unsigned) start;
668                   new_range->last = (unsigned) start;
669                 }
670               else
671                 {
672                   new_range->start = (unsigned) -1;
673                   new_range->last = (unsigned) -1;
674                   new_range->assem_name = xstrdup (func_name);
675                 }
676             }
677           else
678             {
679               long last = strtol (end_val, &invalid, 10);
680               if (*invalid || last < start)
681                 {
682                   error ("Invalid range %s in option %s",
683                          end_val,
684                          is_enable ? "-fenable" : "-fdisable");
685                   free (argstr);
686                   return;
687                 }
688               new_range = XCNEW (struct uid_range);
689               new_range->start = (unsigned) start;
690               new_range->last = (unsigned) last;
691             }
692
693           slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
694           new_range->next = slot;
695           VEC_replace (uid_range_p, *tab, pass->static_pass_number,
696                        new_range);
697           if (is_enable)
698             {
699               if (new_range->assem_name)
700                 inform (UNKNOWN_LOCATION,
701                         "enable pass %s for function %s",
702                         phase_name, new_range->assem_name);
703               else
704                 inform (UNKNOWN_LOCATION,
705                         "enable pass %s for functions in the range of [%u, %u]",
706                         phase_name, new_range->start, new_range->last);
707             }
708           else
709             {
710               if (new_range->assem_name)
711                 inform (UNKNOWN_LOCATION,
712                         "disable pass %s for function %s",
713                         phase_name, new_range->assem_name);
714               else
715                 inform (UNKNOWN_LOCATION,
716                         "disable pass %s for functions in the range of [%u, %u]",
717                         phase_name, new_range->start, new_range->last);
718             }
719
720           one_range = next_range;
721         } while (next_range);
722     }
723
724   free (argstr);
725 }
726
727 /* Enable pass specified by ARG.  */
728
729 void
730 enable_pass (const char *arg)
731 {
732   enable_disable_pass (arg, true);
733 }
734
735 /* Disable pass specified by ARG.  */
736
737 void
738 disable_pass (const char *arg)
739 {
740   enable_disable_pass (arg, false);
741 }
742
743 /* Returns true if PASS is explicitly enabled/disabled for FUNC.  */
744
745 static bool
746 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
747                                         tree func,
748                                         VEC(uid_range_p, heap) *tab)
749 {
750   uid_range_p slot, range;
751   int cgraph_uid;
752   const char *aname = NULL;
753
754   if (!tab
755       || (unsigned) pass->static_pass_number >= VEC_length (uid_range_p, tab)
756       || pass->static_pass_number == -1)
757     return false;
758
759   slot = VEC_index (uid_range_p, tab, pass->static_pass_number);
760   if (!slot)
761     return false;
762
763   cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
764   if (func && DECL_ASSEMBLER_NAME_SET_P (func))
765     aname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func));
766
767   range = slot;
768   while (range)
769     {
770       if ((unsigned) cgraph_uid >= range->start
771           && (unsigned) cgraph_uid <= range->last)
772         return true;
773       if (range->assem_name && aname
774           && !strcmp (range->assem_name, aname))
775         return true;
776       range = range->next;
777     }
778
779   return false;
780 }
781
782 /* Look at the static_pass_number and duplicate the pass
783    if it is already added to a list. */
784
785 static struct opt_pass *
786 make_pass_instance (struct opt_pass *pass, bool track_duplicates)
787 {
788   /* A nonzero static_pass_number indicates that the
789      pass is already in the list.  */
790   if (pass->static_pass_number)
791     {
792       struct opt_pass *new_pass;
793
794       if (pass->type == GIMPLE_PASS
795           || pass->type == RTL_PASS
796           || pass->type == SIMPLE_IPA_PASS)
797         {
798           new_pass = XNEW (struct opt_pass);
799           memcpy (new_pass, pass, sizeof (struct opt_pass));
800         }
801       else if (pass->type == IPA_PASS)
802         {
803           new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
804           memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
805         }
806       else
807         gcc_unreachable ();
808
809       new_pass->next = NULL;
810
811       new_pass->todo_flags_start &= ~TODO_mark_first_instance;
812
813       /* Indicate to register_dump_files that this pass has duplicates,
814          and so it should rename the dump file.  The first instance will
815          be -1, and be number of duplicates = -static_pass_number - 1.
816          Subsequent instances will be > 0 and just the duplicate number.  */
817       if ((pass->name && pass->name[0] != '*') || track_duplicates)
818         {
819           pass->static_pass_number -= 1;
820           new_pass->static_pass_number = -pass->static_pass_number;
821         }
822       return new_pass;
823     }
824   else
825     {
826       pass->todo_flags_start |= TODO_mark_first_instance;
827       pass->static_pass_number = -1;
828
829       invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
830     }
831   return pass;
832 }
833
834 /* Add a pass to the pass list. Duplicate the pass if it's already
835    in the list.  */
836
837 static struct opt_pass **
838 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
839 {
840   /* Every pass should have a name so that plugins can refer to them.  */
841   gcc_assert (pass->name != NULL);
842
843   *list = make_pass_instance (pass, false);
844
845   return &(*list)->next;
846 }
847
848 /* List node for an inserted pass instance. We need to keep track of all
849    the newly-added pass instances (with 'added_pass_nodes' defined below)
850    so that we can register their dump files after pass-positioning is finished.
851    Registering dumping files needs to be post-processed or the
852    static_pass_number of the opt_pass object would be modified and mess up
853    the dump file names of future pass instances to be added.  */
854
855 struct pass_list_node
856 {
857   struct opt_pass *pass;
858   struct pass_list_node *next;
859 };
860
861 static struct pass_list_node *added_pass_nodes = NULL;
862 static struct pass_list_node *prev_added_pass_node;
863
864 /* Insert the pass at the proper position. Return true if the pass
865    is successfully added.
866
867    NEW_PASS_INFO - new pass to be inserted
868    PASS_LIST - root of the pass list to insert the new pass to  */
869
870 static bool
871 position_pass (struct register_pass_info *new_pass_info,
872                struct opt_pass **pass_list)
873 {
874   struct opt_pass *pass = *pass_list, *prev_pass = NULL;
875   bool success = false;
876
877   for ( ; pass; prev_pass = pass, pass = pass->next)
878     {
879       /* Check if the current pass is of the same type as the new pass and
880          matches the name and the instance number of the reference pass.  */
881       if (pass->type == new_pass_info->pass->type
882           && pass->name
883           && !strcmp (pass->name, new_pass_info->reference_pass_name)
884           && ((new_pass_info->ref_pass_instance_number == 0)
885               || (new_pass_info->ref_pass_instance_number ==
886                   pass->static_pass_number)
887               || (new_pass_info->ref_pass_instance_number == 1
888                   && pass->todo_flags_start & TODO_mark_first_instance)))
889         {
890           struct opt_pass *new_pass;
891           struct pass_list_node *new_pass_node;
892
893           new_pass = make_pass_instance (new_pass_info->pass, true);
894
895           /* Insert the new pass instance based on the positioning op.  */
896           switch (new_pass_info->pos_op)
897             {
898               case PASS_POS_INSERT_AFTER:
899                 new_pass->next = pass->next;
900                 pass->next = new_pass;
901
902                 /* Skip newly inserted pass to avoid repeated
903                    insertions in the case where the new pass and the
904                    existing one have the same name.  */
905                 pass = new_pass;
906                 break;
907               case PASS_POS_INSERT_BEFORE:
908                 new_pass->next = pass;
909                 if (prev_pass)
910                   prev_pass->next = new_pass;
911                 else
912                   *pass_list = new_pass;
913                 break;
914               case PASS_POS_REPLACE:
915                 new_pass->next = pass->next;
916                 if (prev_pass)
917                   prev_pass->next = new_pass;
918                 else
919                   *pass_list = new_pass;
920                 new_pass->sub = pass->sub;
921                 new_pass->tv_id = pass->tv_id;
922                 pass = new_pass;
923                 break;
924               default:
925                 error ("invalid pass positioning operation");
926                 return false;
927             }
928
929           /* Save the newly added pass (instance) in the added_pass_nodes
930              list so that we can register its dump file later. Note that
931              we cannot register the dump file now because doing so will modify
932              the static_pass_number of the opt_pass object and therefore
933              mess up the dump file name of future instances.  */
934           new_pass_node = XCNEW (struct pass_list_node);
935           new_pass_node->pass = new_pass;
936           if (!added_pass_nodes)
937             added_pass_nodes = new_pass_node;
938           else
939             prev_added_pass_node->next = new_pass_node;
940           prev_added_pass_node = new_pass_node;
941
942           success = true;
943         }
944
945       if (pass->sub && position_pass (new_pass_info, &pass->sub))
946         success = true;
947     }
948
949   return success;
950 }
951
952 /* Hooks a new pass into the pass lists.
953
954    PASS_INFO   - pass information that specifies the opt_pass object,
955                  reference pass, instance number, and how to position
956                  the pass  */
957
958 void
959 register_pass (struct register_pass_info *pass_info)
960 {
961   bool all_instances, success;
962
963   /* The checks below could fail in buggy plugins.  Existing GCC
964      passes should never fail these checks, so we mention plugin in
965      the messages.  */
966   if (!pass_info->pass)
967       fatal_error ("plugin cannot register a missing pass");
968
969   if (!pass_info->pass->name)
970       fatal_error ("plugin cannot register an unnamed pass");
971
972   if (!pass_info->reference_pass_name)
973       fatal_error
974         ("plugin cannot register pass %qs without reference pass name",
975          pass_info->pass->name);
976
977   /* Try to insert the new pass to the pass lists.  We need to check
978      all five lists as the reference pass could be in one (or all) of
979      them.  */
980   all_instances = pass_info->ref_pass_instance_number == 0;
981   success = position_pass (pass_info, &all_lowering_passes);
982   if (!success || all_instances)
983     success |= position_pass (pass_info, &all_small_ipa_passes);
984   if (!success || all_instances)
985     success |= position_pass (pass_info, &all_regular_ipa_passes);
986   if (!success || all_instances)
987     success |= position_pass (pass_info, &all_lto_gen_passes);
988   if (!success || all_instances)
989     success |= position_pass (pass_info, &all_passes);
990   if (!success)
991     fatal_error
992       ("pass %qs not found but is referenced by new pass %qs",
993        pass_info->reference_pass_name, pass_info->pass->name);
994
995   /* OK, we have successfully inserted the new pass. We need to register
996      the dump files for the newly added pass and its duplicates (if any).
997      Because the registration of plugin/backend passes happens after the
998      command-line options are parsed, the options that specify single
999      pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
1000      passes. Therefore we currently can only enable dumping of
1001      new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
1002      are specified. While doing so, we also delete the pass_list_node
1003      objects created during pass positioning.  */
1004   while (added_pass_nodes)
1005     {
1006       struct pass_list_node *next_node = added_pass_nodes->next;
1007       enum tree_dump_index tdi;
1008       register_one_dump_file (added_pass_nodes->pass);
1009       if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
1010           || added_pass_nodes->pass->type == IPA_PASS)
1011         tdi = TDI_ipa_all;
1012       else if (added_pass_nodes->pass->type == GIMPLE_PASS)
1013         tdi = TDI_tree_all;
1014       else
1015         tdi = TDI_rtl_all;
1016       /* Check if dump-all flag is specified.  */
1017       if (get_dump_file_info (tdi)->state)
1018         get_dump_file_info (added_pass_nodes->pass->static_pass_number)
1019             ->state = get_dump_file_info (tdi)->state;
1020       XDELETE (added_pass_nodes);
1021       added_pass_nodes = next_node;
1022     }
1023 }
1024
1025 /* Construct the pass tree.  The sequencing of passes is driven by
1026    the cgraph routines:
1027
1028    cgraph_finalize_compilation_unit ()
1029        for each node N in the cgraph
1030            cgraph_analyze_function (N)
1031                cgraph_lower_function (N) -> all_lowering_passes
1032
1033    If we are optimizing, cgraph_optimize is then invoked:
1034
1035    cgraph_optimize ()
1036        ipa_passes ()                    -> all_small_ipa_passes
1037        cgraph_expand_all_functions ()
1038            for each node N in the cgraph
1039                cgraph_expand_function (N)
1040                   tree_rest_of_compilation (DECL (N))  -> all_passes
1041 */
1042
1043 void
1044 init_optimization_passes (void)
1045 {
1046   struct opt_pass **p;
1047
1048 #define NEXT_PASS(PASS)  (p = next_pass_1 (p, &((PASS).pass)))
1049
1050  /* All passes needed to lower the function into shape optimizers can
1051     operate on.  These passes are always run first on the function, but
1052     backend might produce already lowered functions that are not processed
1053     by these passes.  */
1054   p = &all_lowering_passes;
1055   NEXT_PASS (pass_warn_unused_result);
1056   NEXT_PASS (pass_diagnose_omp_blocks);
1057   NEXT_PASS (pass_mudflap_1);
1058   NEXT_PASS (pass_lower_omp);
1059   NEXT_PASS (pass_lower_cf);
1060   NEXT_PASS (pass_refactor_eh);
1061   NEXT_PASS (pass_lower_eh);
1062   NEXT_PASS (pass_build_cfg);
1063   NEXT_PASS (pass_warn_function_return);
1064   NEXT_PASS (pass_build_cgraph_edges);
1065   *p = NULL;
1066
1067   /* Interprocedural optimization passes.  */
1068   p = &all_small_ipa_passes;
1069   NEXT_PASS (pass_ipa_free_lang_data);
1070   NEXT_PASS (pass_ipa_function_and_variable_visibility);
1071   NEXT_PASS (pass_early_local_passes);
1072     {
1073       struct opt_pass **p = &pass_early_local_passes.pass.sub;
1074       NEXT_PASS (pass_fixup_cfg);
1075       NEXT_PASS (pass_init_datastructures);
1076       NEXT_PASS (pass_expand_omp);
1077
1078       NEXT_PASS (pass_referenced_vars);
1079       NEXT_PASS (pass_build_ssa);
1080       NEXT_PASS (pass_lower_vector);
1081       NEXT_PASS (pass_early_warn_uninitialized);
1082       NEXT_PASS (pass_rebuild_cgraph_edges);
1083       NEXT_PASS (pass_inline_parameters);
1084       NEXT_PASS (pass_early_inline);
1085       NEXT_PASS (pass_all_early_optimizations);
1086         {
1087           struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
1088           NEXT_PASS (pass_remove_cgraph_callee_edges);
1089           NEXT_PASS (pass_rename_ssa_copies);
1090           NEXT_PASS (pass_ccp);
1091           NEXT_PASS (pass_forwprop);
1092           /* pass_build_ealias is a dummy pass that ensures that we
1093              execute TODO_rebuild_alias at this point.  Re-building
1094              alias information also rewrites no longer addressed
1095              locals into SSA form if possible.  */
1096           NEXT_PASS (pass_build_ealias);
1097           NEXT_PASS (pass_sra_early);
1098           NEXT_PASS (pass_fre);
1099           NEXT_PASS (pass_copy_prop);
1100           NEXT_PASS (pass_merge_phi);
1101           NEXT_PASS (pass_cd_dce);
1102           NEXT_PASS (pass_early_ipa_sra);
1103           NEXT_PASS (pass_tail_recursion);
1104           NEXT_PASS (pass_convert_switch);
1105           NEXT_PASS (pass_cleanup_eh);
1106           NEXT_PASS (pass_profile);
1107           NEXT_PASS (pass_local_pure_const);
1108           /* Split functions creates parts that are not run through
1109              early optimizations again.  It is thus good idea to do this
1110              late.  */
1111           NEXT_PASS (pass_split_functions);
1112         }
1113       NEXT_PASS (pass_release_ssa_names);
1114       NEXT_PASS (pass_rebuild_cgraph_edges);
1115       NEXT_PASS (pass_inline_parameters);
1116     }
1117   NEXT_PASS (pass_ipa_tree_profile);
1118     {
1119       struct opt_pass **p = &pass_ipa_tree_profile.pass.sub;
1120       NEXT_PASS (pass_feedback_split_functions);
1121     }
1122   NEXT_PASS (pass_ipa_increase_alignment);
1123   NEXT_PASS (pass_ipa_matrix_reorg);
1124   NEXT_PASS (pass_ipa_lower_emutls);
1125   *p = NULL;
1126
1127   p = &all_regular_ipa_passes;
1128   NEXT_PASS (pass_ipa_whole_program_visibility);
1129   NEXT_PASS (pass_ipa_profile);
1130   NEXT_PASS (pass_ipa_cp);
1131   NEXT_PASS (pass_ipa_cdtor_merge);
1132   NEXT_PASS (pass_ipa_inline);
1133   NEXT_PASS (pass_ipa_pure_const);
1134   NEXT_PASS (pass_ipa_reference);
1135   NEXT_PASS (pass_ipa_pta);
1136   *p = NULL;
1137
1138   p = &all_lto_gen_passes;
1139   NEXT_PASS (pass_ipa_lto_gimple_out);
1140   NEXT_PASS (pass_ipa_lto_finish_out);  /* This must be the last LTO pass.  */
1141   *p = NULL;
1142
1143   /* These passes are run after IPA passes on every function that is being
1144      output to the assembler file.  */
1145   p = &all_passes;
1146   NEXT_PASS (pass_lower_eh_dispatch);
1147   NEXT_PASS (pass_all_optimizations);
1148     {
1149       struct opt_pass **p = &pass_all_optimizations.pass.sub;
1150       NEXT_PASS (pass_remove_cgraph_callee_edges);
1151       /* Initial scalar cleanups before alias computation.
1152          They ensure memory accesses are not indirect wherever possible.  */
1153       NEXT_PASS (pass_strip_predict_hints);
1154       NEXT_PASS (pass_rename_ssa_copies);
1155       NEXT_PASS (pass_complete_unrolli);
1156       NEXT_PASS (pass_ccp);
1157       NEXT_PASS (pass_forwprop);
1158       NEXT_PASS (pass_call_cdce);
1159       /* pass_build_alias is a dummy pass that ensures that we
1160          execute TODO_rebuild_alias at this point.  Re-building
1161          alias information also rewrites no longer addressed
1162          locals into SSA form if possible.  */
1163       NEXT_PASS (pass_build_alias);
1164       NEXT_PASS (pass_return_slot);
1165       NEXT_PASS (pass_phiprop);
1166       NEXT_PASS (pass_fre);
1167       NEXT_PASS (pass_copy_prop);
1168       NEXT_PASS (pass_merge_phi);
1169       NEXT_PASS (pass_vrp);
1170       NEXT_PASS (pass_dce);
1171       NEXT_PASS (pass_cselim);
1172       NEXT_PASS (pass_tree_ifcombine);
1173       NEXT_PASS (pass_phiopt);
1174       NEXT_PASS (pass_tail_recursion);
1175       NEXT_PASS (pass_ch);
1176       NEXT_PASS (pass_stdarg);
1177       NEXT_PASS (pass_lower_complex);
1178       NEXT_PASS (pass_sra);
1179       NEXT_PASS (pass_rename_ssa_copies);
1180       /* The dom pass will also resolve all __builtin_constant_p calls
1181          that are still there to 0.  This has to be done after some
1182          propagations have already run, but before some more dead code
1183          is removed, and this place fits nicely.  Remember this when
1184          trying to move or duplicate pass_dominator somewhere earlier.  */
1185       NEXT_PASS (pass_dominator);
1186       /* The only const/copy propagation opportunities left after
1187          DOM should be due to degenerate PHI nodes.  So rather than
1188          run the full propagators, run a specialized pass which
1189          only examines PHIs to discover const/copy propagation
1190          opportunities.  */
1191       NEXT_PASS (pass_phi_only_cprop);
1192       NEXT_PASS (pass_dse);
1193       NEXT_PASS (pass_reassoc);
1194       NEXT_PASS (pass_dce);
1195       NEXT_PASS (pass_forwprop);
1196       NEXT_PASS (pass_phiopt);
1197       NEXT_PASS (pass_object_sizes);
1198       NEXT_PASS (pass_ccp);
1199       NEXT_PASS (pass_copy_prop);
1200       NEXT_PASS (pass_cse_sincos);
1201       NEXT_PASS (pass_optimize_bswap);
1202       NEXT_PASS (pass_split_crit_edges);
1203       NEXT_PASS (pass_pre);
1204       NEXT_PASS (pass_sink_code);
1205       NEXT_PASS (pass_tree_loop);
1206         {
1207           struct opt_pass **p = &pass_tree_loop.pass.sub;
1208           NEXT_PASS (pass_tree_loop_init);
1209           NEXT_PASS (pass_lim);
1210           NEXT_PASS (pass_copy_prop);
1211           NEXT_PASS (pass_dce_loop);
1212           NEXT_PASS (pass_tree_unswitch);
1213           NEXT_PASS (pass_scev_cprop);
1214           NEXT_PASS (pass_record_bounds);
1215           NEXT_PASS (pass_check_data_deps);
1216           NEXT_PASS (pass_loop_distribution);
1217           NEXT_PASS (pass_copy_prop);
1218           NEXT_PASS (pass_graphite);
1219             {
1220               struct opt_pass **p = &pass_graphite.pass.sub;
1221               NEXT_PASS (pass_graphite_transforms);
1222               NEXT_PASS (pass_lim);
1223               NEXT_PASS (pass_copy_prop);
1224               NEXT_PASS (pass_dce_loop);
1225             }
1226           NEXT_PASS (pass_iv_canon);
1227           NEXT_PASS (pass_if_conversion);
1228           NEXT_PASS (pass_vectorize);
1229             {
1230               struct opt_pass **p = &pass_vectorize.pass.sub;
1231               NEXT_PASS (pass_lower_vector_ssa);
1232               NEXT_PASS (pass_dce_loop);
1233             }
1234           NEXT_PASS (pass_predcom);
1235           NEXT_PASS (pass_complete_unroll);
1236           NEXT_PASS (pass_slp_vectorize);
1237           NEXT_PASS (pass_parallelize_loops);
1238           NEXT_PASS (pass_loop_prefetch);
1239           NEXT_PASS (pass_iv_optimize);
1240           NEXT_PASS (pass_tree_loop_done);
1241         }
1242       NEXT_PASS (pass_cse_reciprocals);
1243       NEXT_PASS (pass_reassoc);
1244       NEXT_PASS (pass_vrp);
1245       NEXT_PASS (pass_dominator);
1246       /* The only const/copy propagation opportunities left after
1247          DOM should be due to degenerate PHI nodes.  So rather than
1248          run the full propagators, run a specialized pass which
1249          only examines PHIs to discover const/copy propagation
1250          opportunities.  */
1251       NEXT_PASS (pass_phi_only_cprop);
1252       NEXT_PASS (pass_cd_dce);
1253       NEXT_PASS (pass_tracer);
1254
1255       /* FIXME: If DCE is not run before checking for uninitialized uses,
1256          we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
1257          However, this also causes us to misdiagnose cases that should be
1258          real warnings (e.g., testsuite/gcc.dg/pr18501.c).
1259
1260          To fix the false positives in uninit-5.c, we would have to
1261          account for the predicates protecting the set and the use of each
1262          variable.  Using a representation like Gated Single Assignment
1263          may help.  */
1264       NEXT_PASS (pass_late_warn_uninitialized);
1265       NEXT_PASS (pass_dse);
1266       NEXT_PASS (pass_forwprop);
1267       NEXT_PASS (pass_phiopt);
1268       NEXT_PASS (pass_fold_builtins);
1269       NEXT_PASS (pass_optimize_widening_mul);
1270       NEXT_PASS (pass_tail_calls);
1271       NEXT_PASS (pass_rename_ssa_copies);
1272       NEXT_PASS (pass_uncprop);
1273       NEXT_PASS (pass_local_pure_const);
1274     }
1275   NEXT_PASS (pass_lower_complex_O0);
1276   NEXT_PASS (pass_cleanup_eh);
1277   NEXT_PASS (pass_lower_resx);
1278   NEXT_PASS (pass_nrv);
1279   NEXT_PASS (pass_mudflap_2);
1280   NEXT_PASS (pass_cleanup_cfg_post_optimizing);
1281   NEXT_PASS (pass_warn_function_noreturn);
1282
1283   NEXT_PASS (pass_expand);
1284
1285   NEXT_PASS (pass_rest_of_compilation);
1286     {
1287       struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
1288       NEXT_PASS (pass_init_function);
1289       NEXT_PASS (pass_jump);
1290       NEXT_PASS (pass_rtl_eh);
1291       NEXT_PASS (pass_initial_value_sets);
1292       NEXT_PASS (pass_unshare_all_rtl);
1293       NEXT_PASS (pass_instantiate_virtual_regs);
1294       NEXT_PASS (pass_into_cfg_layout_mode);
1295       NEXT_PASS (pass_jump2);
1296       NEXT_PASS (pass_lower_subreg);
1297       NEXT_PASS (pass_df_initialize_opt);
1298       NEXT_PASS (pass_cse);
1299       NEXT_PASS (pass_rtl_fwprop);
1300       NEXT_PASS (pass_rtl_cprop);
1301       NEXT_PASS (pass_rtl_pre);
1302       NEXT_PASS (pass_rtl_hoist);
1303       NEXT_PASS (pass_rtl_cprop);
1304       NEXT_PASS (pass_rtl_store_motion);
1305       NEXT_PASS (pass_cse_after_global_opts);
1306       NEXT_PASS (pass_rtl_ifcvt);
1307       NEXT_PASS (pass_reginfo_init);
1308       /* Perform loop optimizations.  It might be better to do them a bit
1309          sooner, but we want the profile feedback to work more
1310          efficiently.  */
1311       NEXT_PASS (pass_loop2);
1312         {
1313           struct opt_pass **p = &pass_loop2.pass.sub;
1314           NEXT_PASS (pass_rtl_loop_init);
1315           NEXT_PASS (pass_rtl_move_loop_invariants);
1316           NEXT_PASS (pass_rtl_unswitch);
1317           NEXT_PASS (pass_rtl_unroll_and_peel_loops);
1318           NEXT_PASS (pass_rtl_doloop);
1319           NEXT_PASS (pass_rtl_loop_done);
1320           *p = NULL;
1321         }
1322       NEXT_PASS (pass_web);
1323       NEXT_PASS (pass_rtl_cprop);
1324       NEXT_PASS (pass_cse2);
1325       NEXT_PASS (pass_rtl_dse1);
1326       NEXT_PASS (pass_rtl_fwprop_addr);
1327       NEXT_PASS (pass_inc_dec);
1328       NEXT_PASS (pass_initialize_regs);
1329       NEXT_PASS (pass_ud_rtl_dce);
1330       NEXT_PASS (pass_combine);
1331       NEXT_PASS (pass_if_after_combine);
1332       NEXT_PASS (pass_partition_blocks);
1333       NEXT_PASS (pass_regmove);
1334       NEXT_PASS (pass_outof_cfg_layout_mode);
1335       NEXT_PASS (pass_split_all_insns);
1336       NEXT_PASS (pass_lower_subreg2);
1337       NEXT_PASS (pass_df_initialize_no_opt);
1338       NEXT_PASS (pass_stack_ptr_mod);
1339       NEXT_PASS (pass_mode_switching);
1340       NEXT_PASS (pass_match_asm_constraints);
1341       NEXT_PASS (pass_sms);
1342       NEXT_PASS (pass_sched);
1343       NEXT_PASS (pass_ira);
1344       NEXT_PASS (pass_postreload);
1345         {
1346           struct opt_pass **p = &pass_postreload.pass.sub;
1347           NEXT_PASS (pass_postreload_cse);
1348           NEXT_PASS (pass_gcse2);
1349           NEXT_PASS (pass_split_after_reload);
1350           NEXT_PASS (pass_implicit_zee);
1351           NEXT_PASS (pass_compare_elim_after_reload);
1352           NEXT_PASS (pass_branch_target_load_optimize1);
1353           NEXT_PASS (pass_thread_prologue_and_epilogue);
1354           NEXT_PASS (pass_rtl_dse2);
1355           NEXT_PASS (pass_stack_adjustments);
1356           NEXT_PASS (pass_peephole2);
1357           NEXT_PASS (pass_if_after_reload);
1358           NEXT_PASS (pass_regrename);
1359           NEXT_PASS (pass_cprop_hardreg);
1360           NEXT_PASS (pass_fast_rtl_dce);
1361           NEXT_PASS (pass_reorder_blocks);
1362           NEXT_PASS (pass_branch_target_load_optimize2);
1363           NEXT_PASS (pass_leaf_regs);
1364           NEXT_PASS (pass_split_before_sched2);
1365           NEXT_PASS (pass_sched2);
1366           NEXT_PASS (pass_stack_regs);
1367             {
1368               struct opt_pass **p = &pass_stack_regs.pass.sub;
1369               NEXT_PASS (pass_split_before_regstack);
1370               NEXT_PASS (pass_stack_regs_run);
1371             }
1372           NEXT_PASS (pass_compute_alignments);
1373           NEXT_PASS (pass_duplicate_computed_gotos);
1374           NEXT_PASS (pass_variable_tracking);
1375           NEXT_PASS (pass_free_cfg);
1376           NEXT_PASS (pass_machine_reorg);
1377           NEXT_PASS (pass_cleanup_barriers);
1378           NEXT_PASS (pass_delay_slots);
1379           NEXT_PASS (pass_split_for_shorten_branches);
1380           NEXT_PASS (pass_convert_to_eh_region_ranges);
1381           NEXT_PASS (pass_shorten_branches);
1382           NEXT_PASS (pass_set_nothrow_function_flags);
1383           NEXT_PASS (pass_final);
1384         }
1385       NEXT_PASS (pass_df_finish);
1386     }
1387   NEXT_PASS (pass_clean_state);
1388   *p = NULL;
1389
1390 #undef NEXT_PASS
1391
1392   /* Register the passes with the tree dump code.  */
1393   register_dump_files (all_lowering_passes, PROP_gimple_any);
1394   register_dump_files (all_small_ipa_passes,
1395                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1396                        | PROP_cfg);
1397   register_dump_files (all_regular_ipa_passes,
1398                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1399                        | PROP_cfg);
1400   register_dump_files (all_lto_gen_passes,
1401                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1402                        | PROP_cfg);
1403   register_dump_files (all_passes,
1404                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1405                        | PROP_cfg);
1406 }
1407
1408 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1409    function CALLBACK for every function in the call graph.  Otherwise,
1410    call CALLBACK on the current function.  */
1411
1412 static void
1413 do_per_function (void (*callback) (void *data), void *data)
1414 {
1415   if (current_function_decl)
1416     callback (data);
1417   else
1418     {
1419       struct cgraph_node *node;
1420       for (node = cgraph_nodes; node; node = node->next)
1421         if (node->analyzed && gimple_has_body_p (node->decl)
1422             && (!node->clone_of || node->decl != node->clone_of->decl))
1423           {
1424             push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1425             current_function_decl = node->decl;
1426             callback (data);
1427             if (!flag_wpa)
1428               {
1429                 free_dominance_info (CDI_DOMINATORS);
1430                 free_dominance_info (CDI_POST_DOMINATORS);
1431               }
1432             current_function_decl = NULL;
1433             pop_cfun ();
1434             ggc_collect ();
1435           }
1436     }
1437 }
1438
1439 /* Because inlining might remove no-longer reachable nodes, we need to
1440    keep the array visible to garbage collector to avoid reading collected
1441    out nodes.  */
1442 static int nnodes;
1443 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1444
1445 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1446    function CALLBACK for every function in the call graph.  Otherwise,
1447    call CALLBACK on the current function.
1448    This function is global so that plugins can use it.  */
1449 void
1450 do_per_function_toporder (void (*callback) (void *data), void *data)
1451 {
1452   int i;
1453
1454   if (current_function_decl)
1455     callback (data);
1456   else
1457     {
1458       gcc_assert (!order);
1459       order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1460       nnodes = ipa_reverse_postorder (order);
1461       for (i = nnodes - 1; i >= 0; i--)
1462         order[i]->process = 1;
1463       for (i = nnodes - 1; i >= 0; i--)
1464         {
1465           struct cgraph_node *node = order[i];
1466
1467           /* Allow possibly removed nodes to be garbage collected.  */
1468           order[i] = NULL;
1469           node->process = 0;
1470           if (cgraph_function_with_gimple_body_p (node))
1471             {
1472               push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1473               current_function_decl = node->decl;
1474               callback (data);
1475               free_dominance_info (CDI_DOMINATORS);
1476               free_dominance_info (CDI_POST_DOMINATORS);
1477               current_function_decl = NULL;
1478               pop_cfun ();
1479               ggc_collect ();
1480             }
1481         }
1482     }
1483   ggc_free (order);
1484   order = NULL;
1485   nnodes = 0;
1486 }
1487
1488 /* Perform all TODO actions that ought to be done on each function.  */
1489
1490 static void
1491 execute_function_todo (void *data)
1492 {
1493   unsigned int flags = (size_t)data;
1494   flags &= ~cfun->last_verified;
1495   if (!flags)
1496     return;
1497
1498   /* Always cleanup the CFG before trying to update SSA.  */
1499   if (flags & TODO_cleanup_cfg)
1500     {
1501       bool cleanup = cleanup_tree_cfg ();
1502
1503       if (cleanup && (cfun->curr_properties & PROP_ssa))
1504         flags |= TODO_remove_unused_locals;
1505
1506       /* When cleanup_tree_cfg merges consecutive blocks, it may
1507          perform some simplistic propagation when removing single
1508          valued PHI nodes.  This propagation may, in turn, cause the
1509          SSA form to become out-of-date (see PR 22037).  So, even
1510          if the parent pass had not scheduled an SSA update, we may
1511          still need to do one.  */
1512       if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1513         flags |= TODO_update_ssa;
1514     }
1515
1516   if (flags & TODO_update_ssa_any)
1517     {
1518       unsigned update_flags = flags & TODO_update_ssa_any;
1519       update_ssa (update_flags);
1520       cfun->last_verified &= ~TODO_verify_ssa;
1521     }
1522
1523   if (flags & TODO_rebuild_alias)
1524     {
1525       execute_update_addresses_taken ();
1526       compute_may_aliases ();
1527     }
1528   else if (optimize && (flags & TODO_update_address_taken))
1529     execute_update_addresses_taken ();
1530
1531   if (flags & TODO_remove_unused_locals)
1532     remove_unused_locals ();
1533
1534   if ((flags & TODO_dump_func) && dump_file && current_function_decl)
1535     {
1536       if (cfun->curr_properties & PROP_trees)
1537         dump_function_to_file (current_function_decl, dump_file, dump_flags);
1538       else
1539         {
1540           if (dump_flags & TDF_SLIM)
1541             print_rtl_slim_with_bb (dump_file, get_insns (), dump_flags);
1542           else if ((cfun->curr_properties & PROP_cfg)
1543                    && (dump_flags & TDF_BLOCKS))
1544             print_rtl_with_bb (dump_file, get_insns ());
1545           else
1546             print_rtl (dump_file, get_insns ());
1547
1548           if ((cfun->curr_properties & PROP_cfg)
1549               && graph_dump_format != no_graph
1550               && (dump_flags & TDF_GRAPH))
1551             print_rtl_graph_with_bb (dump_file_name, get_insns ());
1552         }
1553
1554       /* Flush the file.  If verification fails, we won't be able to
1555          close the file before aborting.  */
1556       fflush (dump_file);
1557     }
1558
1559   if (flags & TODO_rebuild_frequencies)
1560     rebuild_frequencies ();
1561
1562   if (flags & TODO_rebuild_cgraph_edges)
1563     rebuild_cgraph_edges ();
1564
1565   /* If we've seen errors do not bother running any verifiers.  */
1566   if (seen_error ())
1567     return;
1568
1569 #if defined ENABLE_CHECKING
1570   if (flags & TODO_verify_ssa
1571       || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1572     verify_ssa (true);
1573   if (flags & TODO_verify_flow)
1574     verify_flow_info ();
1575   if (flags & TODO_verify_stmts)
1576     verify_gimple_in_cfg (cfun);
1577   if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1578     verify_loop_closed_ssa (false);
1579   if (flags & TODO_verify_rtl_sharing)
1580     verify_rtl_sharing ();
1581 #endif
1582
1583   cfun->last_verified = flags & TODO_verify_all;
1584 }
1585
1586 /* Perform all TODO actions.  */
1587 static void
1588 execute_todo (unsigned int flags)
1589 {
1590 #if defined ENABLE_CHECKING
1591   if (cfun
1592       && need_ssa_update_p (cfun))
1593     gcc_assert (flags & TODO_update_ssa_any);
1594 #endif
1595
1596   timevar_push (TV_TODO);
1597
1598   /* Inform the pass whether it is the first time it is run.  */
1599   first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1600
1601   statistics_fini_pass ();
1602
1603   do_per_function (execute_function_todo, (void *)(size_t) flags);
1604
1605   /* Always remove functions just as before inlining: IPA passes might be
1606      interested to see bodies of extern inline functions that are not inlined
1607      to analyze side effects.  The full removal is done just at the end
1608      of IPA pass queue.  */
1609   if (flags & TODO_remove_functions)
1610     {
1611       gcc_assert (!cfun);
1612       cgraph_remove_unreachable_nodes (true, dump_file);
1613     }
1614
1615   if ((flags & TODO_dump_cgraph) && dump_file && !current_function_decl)
1616     {
1617       gcc_assert (!cfun);
1618       dump_cgraph (dump_file);
1619       /* Flush the file.  If verification fails, we won't be able to
1620          close the file before aborting.  */
1621       fflush (dump_file);
1622     }
1623
1624   if (flags & TODO_ggc_collect)
1625     ggc_collect ();
1626
1627   /* Now that the dumping has been done, we can get rid of the optional
1628      df problems.  */
1629   if (flags & TODO_df_finish)
1630     df_finish_pass ((flags & TODO_df_verify) != 0);
1631
1632   timevar_pop (TV_TODO);
1633 }
1634
1635 /* Verify invariants that should hold between passes.  This is a place
1636    to put simple sanity checks.  */
1637
1638 static void
1639 verify_interpass_invariants (void)
1640 {
1641   gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1642 }
1643
1644 /* Clear the last verified flag.  */
1645
1646 static void
1647 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1648 {
1649   cfun->last_verified = 0;
1650 }
1651
1652 /* Helper function. Verify that the properties has been turn into the
1653    properties expected by the pass.  */
1654
1655 #ifdef ENABLE_CHECKING
1656 static void
1657 verify_curr_properties (void *data)
1658 {
1659   unsigned int props = (size_t)data;
1660   gcc_assert ((cfun->curr_properties & props) == props);
1661 }
1662 #endif
1663
1664 /* Initialize pass dump file.  */
1665 /* This is non-static so that the plugins can use it.  */
1666
1667 bool
1668 pass_init_dump_file (struct opt_pass *pass)
1669 {
1670   /* If a dump file name is present, open it if enabled.  */
1671   if (pass->static_pass_number != -1)
1672     {
1673       bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
1674       dump_file_name = get_dump_file_name (pass->static_pass_number);
1675       dump_file = dump_begin (pass->static_pass_number, &dump_flags);
1676       if (dump_file && current_function_decl)
1677         dump_function_header (dump_file, current_function_decl, dump_flags);
1678       return initializing_dump;
1679     }
1680   else
1681     return false;
1682 }
1683
1684 /* Flush PASS dump file.  */
1685 /* This is non-static so that plugins can use it.  */
1686
1687 void
1688 pass_fini_dump_file (struct opt_pass *pass)
1689 {
1690   /* Flush and close dump file.  */
1691   if (dump_file_name)
1692     {
1693       free (CONST_CAST (char *, dump_file_name));
1694       dump_file_name = NULL;
1695     }
1696
1697   if (dump_file)
1698     {
1699       dump_end (pass->static_pass_number, dump_file);
1700       dump_file = NULL;
1701     }
1702 }
1703
1704 /* After executing the pass, apply expected changes to the function
1705    properties. */
1706
1707 static void
1708 update_properties_after_pass (void *data)
1709 {
1710   struct opt_pass *pass = (struct opt_pass *) data;
1711   cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1712                            & ~pass->properties_destroyed;
1713 }
1714
1715 /* Execute summary generation for all of the passes in IPA_PASS.  */
1716
1717 void
1718 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1719 {
1720   while (ipa_pass)
1721     {
1722       struct opt_pass *pass = &ipa_pass->pass;
1723
1724       /* Execute all of the IPA_PASSes in the list.  */
1725       if (ipa_pass->pass.type == IPA_PASS
1726           && (!pass->gate || pass->gate ())
1727           && ipa_pass->generate_summary)
1728         {
1729           pass_init_dump_file (pass);
1730
1731           /* If a timevar is present, start it.  */
1732           if (pass->tv_id)
1733             timevar_push (pass->tv_id);
1734
1735           ipa_pass->generate_summary ();
1736
1737           /* Stop timevar.  */
1738           if (pass->tv_id)
1739             timevar_pop (pass->tv_id);
1740
1741           pass_fini_dump_file (pass);
1742         }
1743       ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
1744     }
1745 }
1746
1747 /* Execute IPA_PASS function transform on NODE.  */
1748
1749 static void
1750 execute_one_ipa_transform_pass (struct cgraph_node *node,
1751                                 struct ipa_opt_pass_d *ipa_pass)
1752 {
1753   struct opt_pass *pass = &ipa_pass->pass;
1754   unsigned int todo_after = 0;
1755
1756   current_pass = pass;
1757   if (!ipa_pass->function_transform)
1758     return;
1759
1760   /* Note that the folders should only create gimple expressions.
1761      This is a hack until the new folder is ready.  */
1762   in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1763
1764   pass_init_dump_file (pass);
1765
1766   /* Run pre-pass verification.  */
1767   execute_todo (ipa_pass->function_transform_todo_flags_start);
1768
1769   /* If a timevar is present, start it.  */
1770   if (pass->tv_id != TV_NONE)
1771     timevar_push (pass->tv_id);
1772
1773   /* Do it!  */
1774   todo_after = ipa_pass->function_transform (node);
1775
1776   /* Stop timevar.  */
1777   if (pass->tv_id != TV_NONE)
1778     timevar_pop (pass->tv_id);
1779
1780   /* Run post-pass cleanup and verification.  */
1781   execute_todo (todo_after);
1782   verify_interpass_invariants ();
1783
1784   pass_fini_dump_file (pass);
1785
1786   current_pass = NULL;
1787 }
1788
1789 /* For the current function, execute all ipa transforms. */
1790
1791 void
1792 execute_all_ipa_transforms (void)
1793 {
1794   struct cgraph_node *node;
1795   if (!cfun)
1796     return;
1797   node = cgraph_get_node (current_function_decl);
1798
1799   if (node->ipa_transforms_to_apply)
1800     {
1801       unsigned int i;
1802
1803       for (i = 0; i < VEC_length (ipa_opt_pass, node->ipa_transforms_to_apply);
1804            i++)
1805         execute_one_ipa_transform_pass (node,
1806                                         VEC_index (ipa_opt_pass,
1807                                                    node->ipa_transforms_to_apply,
1808                                                    i));
1809       VEC_free (ipa_opt_pass, heap, node->ipa_transforms_to_apply);
1810       node->ipa_transforms_to_apply = NULL;
1811     }
1812 }
1813
1814 /* Check if PASS is explicitly disabled or enabled and return
1815    the gate status.  FUNC is the function to be processed, and
1816    GATE_STATUS is the gate status determined by pass manager by
1817    default.  */
1818
1819 static bool
1820 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
1821 {
1822   bool explicitly_enabled = false;
1823   bool explicitly_disabled = false;
1824
1825   explicitly_enabled
1826    = is_pass_explicitly_enabled_or_disabled (pass, func,
1827                                              enabled_pass_uid_range_tab);
1828   explicitly_disabled
1829    = is_pass_explicitly_enabled_or_disabled (pass, func,
1830                                              disabled_pass_uid_range_tab);
1831
1832   gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
1833
1834   return gate_status;
1835 }
1836
1837
1838 /* Execute PASS. */
1839
1840 bool
1841 execute_one_pass (struct opt_pass *pass)
1842 {
1843   bool initializing_dump;
1844   unsigned int todo_after = 0;
1845
1846   bool gate_status;
1847
1848   /* IPA passes are executed on whole program, so cfun should be NULL.
1849      Other passes need function context set.  */
1850   if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
1851     gcc_assert (!cfun && !current_function_decl);
1852   else
1853     gcc_assert (cfun && current_function_decl);
1854
1855   current_pass = pass;
1856
1857   /* Check whether gate check should be avoided.
1858      User controls the value of the gate through the parameter "gate_status". */
1859   gate_status = (pass->gate == NULL) ? true : pass->gate();
1860   gate_status = override_gate_status (pass, current_function_decl, gate_status);
1861
1862   /* Override gate with plugin.  */
1863   invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
1864
1865   if (!gate_status)
1866     {
1867       current_pass = NULL;
1868       return false;
1869     }
1870
1871   /* Pass execution event trigger: useful to identify passes being
1872      executed.  */
1873   invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
1874
1875   if (!quiet_flag && !cfun)
1876     fprintf (stderr, " <%s>", pass->name ? pass->name : "");
1877
1878   /* Note that the folders should only create gimple expressions.
1879      This is a hack until the new folder is ready.  */
1880   in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1881
1882   initializing_dump = pass_init_dump_file (pass);
1883
1884   /* Run pre-pass verification.  */
1885   execute_todo (pass->todo_flags_start);
1886
1887 #ifdef ENABLE_CHECKING
1888   do_per_function (verify_curr_properties,
1889                    (void *)(size_t)pass->properties_required);
1890 #endif
1891
1892   /* If a timevar is present, start it.  */
1893   if (pass->tv_id != TV_NONE)
1894     timevar_push (pass->tv_id);
1895
1896   /* Do it!  */
1897   if (pass->execute)
1898     {
1899       todo_after = pass->execute ();
1900       do_per_function (clear_last_verified, NULL);
1901     }
1902
1903   /* Stop timevar.  */
1904   if (pass->tv_id != TV_NONE)
1905     timevar_pop (pass->tv_id);
1906
1907   do_per_function (update_properties_after_pass, pass);
1908
1909   if (initializing_dump
1910       && dump_file
1911       && graph_dump_format != no_graph
1912       && cfun
1913       && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
1914           == (PROP_cfg | PROP_rtl))
1915     {
1916       get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
1917       dump_flags |= TDF_GRAPH;
1918       clean_graph_dump_file (dump_file_name);
1919     }
1920
1921   /* Run post-pass cleanup and verification.  */
1922   execute_todo (todo_after | pass->todo_flags_finish);
1923   verify_interpass_invariants ();
1924   if (pass->type == IPA_PASS)
1925     {
1926       struct cgraph_node *node;
1927       FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1928         VEC_safe_push (ipa_opt_pass, heap, node->ipa_transforms_to_apply,
1929                        (struct ipa_opt_pass_d *)pass);
1930     }
1931
1932   if (!current_function_decl)
1933     cgraph_process_new_functions ();
1934
1935   pass_fini_dump_file (pass);
1936
1937   if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
1938     gcc_assert (!(cfun->curr_properties & PROP_trees)
1939                 || pass->type != RTL_PASS);
1940
1941   current_pass = NULL;
1942
1943   return true;
1944 }
1945
1946 void
1947 execute_pass_list (struct opt_pass *pass)
1948 {
1949   do
1950     {
1951       gcc_assert (pass->type == GIMPLE_PASS
1952                   || pass->type == RTL_PASS);
1953       if (execute_one_pass (pass) && pass->sub)
1954         execute_pass_list (pass->sub);
1955       pass = pass->next;
1956     }
1957   while (pass);
1958 }
1959
1960 /* Same as execute_pass_list but assume that subpasses of IPA passes
1961    are local passes. If SET is not NULL, write out summaries of only
1962    those node in SET. */
1963
1964 static void
1965 ipa_write_summaries_2 (struct opt_pass *pass, cgraph_node_set set,
1966                        varpool_node_set vset,
1967                        struct lto_out_decl_state *state)
1968 {
1969   while (pass)
1970     {
1971       struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
1972       gcc_assert (!current_function_decl);
1973       gcc_assert (!cfun);
1974       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1975       if (pass->type == IPA_PASS
1976           && ipa_pass->write_summary
1977           && (!pass->gate || pass->gate ()))
1978         {
1979           /* If a timevar is present, start it.  */
1980           if (pass->tv_id)
1981             timevar_push (pass->tv_id);
1982
1983           pass_init_dump_file (pass);
1984
1985           ipa_pass->write_summary (set,vset);
1986
1987           pass_fini_dump_file (pass);
1988
1989           /* If a timevar is present, start it.  */
1990           if (pass->tv_id)
1991             timevar_pop (pass->tv_id);
1992         }
1993
1994       if (pass->sub && pass->sub->type != GIMPLE_PASS)
1995         ipa_write_summaries_2 (pass->sub, set, vset, state);
1996
1997       pass = pass->next;
1998     }
1999 }
2000
2001 /* Helper function of ipa_write_summaries. Creates and destroys the
2002    decl state and calls ipa_write_summaries_2 for all passes that have
2003    summaries.  SET is the set of nodes to be written.  */
2004
2005 static void
2006 ipa_write_summaries_1 (cgraph_node_set set, varpool_node_set vset)
2007 {
2008   struct lto_out_decl_state *state = lto_new_out_decl_state ();
2009   compute_ltrans_boundary (state, set, vset);
2010
2011   lto_push_out_decl_state (state);
2012
2013   gcc_assert (!flag_wpa);
2014   ipa_write_summaries_2 (all_regular_ipa_passes, set, vset, state);
2015   ipa_write_summaries_2 (all_lto_gen_passes, set, vset, state);
2016
2017   gcc_assert (lto_get_out_decl_state () == state);
2018   lto_pop_out_decl_state ();
2019   lto_delete_out_decl_state (state);
2020 }
2021
2022 /* Write out summaries for all the nodes in the callgraph.  */
2023
2024 void
2025 ipa_write_summaries (void)
2026 {
2027   cgraph_node_set set;
2028   varpool_node_set vset;
2029   struct cgraph_node **order;
2030   struct varpool_node *vnode;
2031   int i, order_pos;
2032
2033   if (!flag_generate_lto || seen_error ())
2034     return;
2035
2036   set = cgraph_node_set_new ();
2037
2038   /* Create the callgraph set in the same order used in
2039      cgraph_expand_all_functions.  This mostly facilitates debugging,
2040      since it causes the gimple file to be processed in the same order
2041      as the source code.  */
2042   order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2043   order_pos = ipa_reverse_postorder (order);
2044   gcc_assert (order_pos == cgraph_n_nodes);
2045
2046   for (i = order_pos - 1; i >= 0; i--)
2047     {
2048       struct cgraph_node *node = order[i];
2049
2050       if (cgraph_function_with_gimple_body_p (node))
2051         {
2052           /* When streaming out references to statements as part of some IPA
2053              pass summary, the statements need to have uids assigned and the
2054              following does that for all the IPA passes here. Naturally, this
2055              ordering then matches the one IPA-passes get in their stmt_fixup
2056              hooks.  */
2057
2058           push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2059           renumber_gimple_stmt_uids ();
2060           pop_cfun ();
2061         }
2062       if (node->analyzed)
2063         cgraph_node_set_add (set, node);
2064     }
2065   vset = varpool_node_set_new ();
2066
2067   for (vnode = varpool_nodes; vnode; vnode = vnode->next)
2068     if (vnode->needed && !vnode->alias)
2069       varpool_node_set_add (vset, vnode);
2070
2071   ipa_write_summaries_1 (set, vset);
2072
2073   free (order);
2074   free_cgraph_node_set (set);
2075   free_varpool_node_set (vset);
2076 }
2077
2078 /* Same as execute_pass_list but assume that subpasses of IPA passes
2079    are local passes. If SET is not NULL, write out optimization summaries of
2080    only those node in SET. */
2081
2082 static void
2083 ipa_write_optimization_summaries_1 (struct opt_pass *pass, cgraph_node_set set,
2084                        varpool_node_set vset,
2085                        struct lto_out_decl_state *state)
2086 {
2087   while (pass)
2088     {
2089       struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2090       gcc_assert (!current_function_decl);
2091       gcc_assert (!cfun);
2092       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2093       if (pass->type == IPA_PASS
2094           && ipa_pass->write_optimization_summary
2095           && (!pass->gate || pass->gate ()))
2096         {
2097           /* If a timevar is present, start it.  */
2098           if (pass->tv_id)
2099             timevar_push (pass->tv_id);
2100
2101           pass_init_dump_file (pass);
2102
2103           ipa_pass->write_optimization_summary (set, vset);
2104
2105           pass_fini_dump_file (pass);
2106
2107           /* If a timevar is present, start it.  */
2108           if (pass->tv_id)
2109             timevar_pop (pass->tv_id);
2110         }
2111
2112       if (pass->sub && pass->sub->type != GIMPLE_PASS)
2113         ipa_write_optimization_summaries_1 (pass->sub, set, vset, state);
2114
2115       pass = pass->next;
2116     }
2117 }
2118
2119 /* Write all the optimization summaries for the cgraph nodes in SET.  If SET is
2120    NULL, write out all summaries of all nodes. */
2121
2122 void
2123 ipa_write_optimization_summaries (cgraph_node_set set, varpool_node_set vset)
2124 {
2125   struct lto_out_decl_state *state = lto_new_out_decl_state ();
2126   cgraph_node_set_iterator csi;
2127   compute_ltrans_boundary (state, set, vset);
2128
2129   lto_push_out_decl_state (state);
2130   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2131     {
2132       struct cgraph_node *node = csi_node (csi);
2133       /* When streaming out references to statements as part of some IPA
2134          pass summary, the statements need to have uids assigned.
2135
2136          For functions newly born at WPA stage we need to initialize
2137          the uids here.  */
2138       if (node->analyzed
2139           && gimple_has_body_p (node->decl))
2140         {
2141           push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2142           renumber_gimple_stmt_uids ();
2143           pop_cfun ();
2144         }
2145     }
2146
2147   gcc_assert (flag_wpa);
2148   ipa_write_optimization_summaries_1 (all_regular_ipa_passes, set, vset, state);
2149   ipa_write_optimization_summaries_1 (all_lto_gen_passes, set, vset, state);
2150
2151   gcc_assert (lto_get_out_decl_state () == state);
2152   lto_pop_out_decl_state ();
2153   lto_delete_out_decl_state (state);
2154 }
2155
2156 /* Same as execute_pass_list but assume that subpasses of IPA passes
2157    are local passes.  */
2158
2159 static void
2160 ipa_read_summaries_1 (struct opt_pass *pass)
2161 {
2162   while (pass)
2163     {
2164       struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2165
2166       gcc_assert (!current_function_decl);
2167       gcc_assert (!cfun);
2168       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2169
2170       if (pass->gate == NULL || pass->gate ())
2171         {
2172           if (pass->type == IPA_PASS && ipa_pass->read_summary)
2173             {
2174               /* If a timevar is present, start it.  */
2175               if (pass->tv_id)
2176                 timevar_push (pass->tv_id);
2177
2178               pass_init_dump_file (pass);
2179
2180               ipa_pass->read_summary ();
2181
2182               pass_fini_dump_file (pass);
2183
2184               /* Stop timevar.  */
2185               if (pass->tv_id)
2186                 timevar_pop (pass->tv_id);
2187             }
2188
2189           if (pass->sub && pass->sub->type != GIMPLE_PASS)
2190             ipa_read_summaries_1 (pass->sub);
2191         }
2192       pass = pass->next;
2193     }
2194 }
2195
2196
2197 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes.  */
2198
2199 void
2200 ipa_read_summaries (void)
2201 {
2202   ipa_read_summaries_1 (all_regular_ipa_passes);
2203   ipa_read_summaries_1 (all_lto_gen_passes);
2204 }
2205
2206 /* Same as execute_pass_list but assume that subpasses of IPA passes
2207    are local passes.  */
2208
2209 static void
2210 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2211 {
2212   while (pass)
2213     {
2214       struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2215
2216       gcc_assert (!current_function_decl);
2217       gcc_assert (!cfun);
2218       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2219
2220       if (pass->gate == NULL || pass->gate ())
2221         {
2222           if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2223             {
2224               /* If a timevar is present, start it.  */
2225               if (pass->tv_id)
2226                 timevar_push (pass->tv_id);
2227
2228               pass_init_dump_file (pass);
2229
2230               ipa_pass->read_optimization_summary ();
2231
2232               pass_fini_dump_file (pass);
2233
2234               /* Stop timevar.  */
2235               if (pass->tv_id)
2236                 timevar_pop (pass->tv_id);
2237             }
2238
2239           if (pass->sub && pass->sub->type != GIMPLE_PASS)
2240             ipa_read_optimization_summaries_1 (pass->sub);
2241         }
2242       pass = pass->next;
2243     }
2244 }
2245
2246 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes.  */
2247
2248 void
2249 ipa_read_optimization_summaries (void)
2250 {
2251   ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
2252   ipa_read_optimization_summaries_1 (all_lto_gen_passes);
2253 }
2254
2255 /* Same as execute_pass_list but assume that subpasses of IPA passes
2256    are local passes.  */
2257 void
2258 execute_ipa_pass_list (struct opt_pass *pass)
2259 {
2260   do
2261     {
2262       gcc_assert (!current_function_decl);
2263       gcc_assert (!cfun);
2264       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2265       if (execute_one_pass (pass) && pass->sub)
2266         {
2267           if (pass->sub->type == GIMPLE_PASS)
2268             {
2269               invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2270               do_per_function_toporder ((void (*)(void *))execute_pass_list,
2271                                         pass->sub);
2272               invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2273             }
2274           else if (pass->sub->type == SIMPLE_IPA_PASS
2275                    || pass->sub->type == IPA_PASS)
2276             execute_ipa_pass_list (pass->sub);
2277           else
2278             gcc_unreachable ();
2279         }
2280       gcc_assert (!current_function_decl);
2281       cgraph_process_new_functions ();
2282       pass = pass->next;
2283     }
2284   while (pass);
2285 }
2286
2287 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS.  */
2288
2289 static void
2290 execute_ipa_stmt_fixups (struct opt_pass *pass,
2291                           struct cgraph_node *node, gimple *stmts)
2292 {
2293   while (pass)
2294     {
2295       /* Execute all of the IPA_PASSes in the list.  */
2296       if (pass->type == IPA_PASS
2297           && (!pass->gate || pass->gate ()))
2298         {
2299           struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2300
2301           if (ipa_pass->stmt_fixup)
2302             {
2303               pass_init_dump_file (pass);
2304               /* If a timevar is present, start it.  */
2305               if (pass->tv_id)
2306                 timevar_push (pass->tv_id);
2307
2308               ipa_pass->stmt_fixup (node, stmts);
2309
2310               /* Stop timevar.  */
2311               if (pass->tv_id)
2312                 timevar_pop (pass->tv_id);
2313               pass_fini_dump_file (pass);
2314             }
2315           if (pass->sub)
2316             execute_ipa_stmt_fixups (pass->sub, node, stmts);
2317         }
2318       pass = pass->next;
2319     }
2320 }
2321
2322 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS.  */
2323
2324 void
2325 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2326 {
2327   execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
2328 }
2329
2330
2331 extern void debug_properties (unsigned int);
2332 extern void dump_properties (FILE *, unsigned int);
2333
2334 DEBUG_FUNCTION void
2335 dump_properties (FILE *dump, unsigned int props)
2336 {
2337   fprintf (dump, "Properties:\n");
2338   if (props & PROP_gimple_any)
2339     fprintf (dump, "PROP_gimple_any\n");
2340   if (props & PROP_gimple_lcf)
2341     fprintf (dump, "PROP_gimple_lcf\n");
2342   if (props & PROP_gimple_leh)
2343     fprintf (dump, "PROP_gimple_leh\n");
2344   if (props & PROP_cfg)
2345     fprintf (dump, "PROP_cfg\n");
2346   if (props & PROP_referenced_vars)
2347     fprintf (dump, "PROP_referenced_vars\n");
2348   if (props & PROP_ssa)
2349     fprintf (dump, "PROP_ssa\n");
2350   if (props & PROP_no_crit_edges)
2351     fprintf (dump, "PROP_no_crit_edges\n");
2352   if (props & PROP_rtl)
2353     fprintf (dump, "PROP_rtl\n");
2354   if (props & PROP_gimple_lomp)
2355     fprintf (dump, "PROP_gimple_lomp\n");
2356   if (props & PROP_gimple_lcx)
2357     fprintf (dump, "PROP_gimple_lcx\n");
2358   if (props & PROP_cfglayout)
2359     fprintf (dump, "PROP_cfglayout\n");
2360 }
2361
2362 DEBUG_FUNCTION void
2363 debug_properties (unsigned int props)
2364 {
2365   dump_properties (stderr, props);
2366 }
2367
2368 /* Called by local passes to see if function is called by already processed nodes.
2369    Because we process nodes in topological order, this means that function is
2370    in recursive cycle or we introduced new direct calls.  */
2371 bool
2372 function_called_by_processed_nodes_p (void)
2373 {
2374   struct cgraph_edge *e;
2375   for (e = cgraph_get_node (current_function_decl)->callers;
2376        e;
2377        e = e->next_caller)
2378     {
2379       if (e->caller->decl == current_function_decl)
2380         continue;
2381       if (!cgraph_function_with_gimple_body_p (e->caller))
2382         continue;
2383       if (TREE_ASM_WRITTEN (e->caller->decl))
2384         continue;
2385       if (!e->caller->process && !e->caller->global.inlined_to)
2386         break;
2387     }
2388   if (dump_file && e)
2389     {
2390       fprintf (dump_file, "Already processed call to:\n");
2391       dump_cgraph_node (dump_file, e->caller);
2392     }
2393   return e != NULL;
2394 }
2395
2396 #include "gt-passes.h"