OSDN Git Service

Better function header dump
[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   struct uid_range *next;
535 };
536
537 typedef struct uid_range *uid_range_p;
538
539 DEF_VEC_P(uid_range_p);
540 DEF_VEC_ALLOC_P(uid_range_p, heap);
541
542 static VEC(uid_range_p, heap) *enabled_pass_uid_range_tab = NULL;
543 static VEC(uid_range_p, heap) *disabled_pass_uid_range_tab = NULL;
544
545 /* Parse option string for -fdisable- and -fenable-
546    The syntax of the options:
547
548    -fenable-<pass_name>
549    -fdisable-<pass_name>
550
551    -fenable-<pass_name>=s1:e1,s2:e2,...
552    -fdisable-<pass_name>=s1:e1,s2:e2,...
553 */
554
555 static void
556 enable_disable_pass (const char *arg, bool is_enable)
557 {
558   struct opt_pass *pass;
559   char *range_str, *phase_name;
560   char *argstr = xstrdup (arg);
561   VEC(uid_range_p, heap) **tab = 0;
562
563   range_str = strchr (argstr,'=');
564   if (range_str)
565     {
566       *range_str = '\0';
567       range_str++;
568     }
569
570   phase_name = argstr;
571   if (!*phase_name)
572     {
573       if (is_enable)
574         error ("unrecognized option -fenable");
575       else
576         error ("unrecognized option -fdisable");
577       free (argstr);
578       return;
579     }
580   pass = get_pass_by_name (phase_name);
581   if (!pass || pass->static_pass_number == -1)
582     {
583       if (is_enable)
584         error ("unknown pass %s specified in -fenable", phase_name);
585       else
586         error ("unknown pass %s specified in -fdisble", phase_name);
587       free (argstr);
588       return;
589     }
590
591   if (is_enable)
592     tab = &enabled_pass_uid_range_tab;
593   else
594     tab = &disabled_pass_uid_range_tab;
595
596   if ((unsigned) pass->static_pass_number >= VEC_length (uid_range_p, *tab))
597     VEC_safe_grow_cleared (uid_range_p, heap,
598                            *tab, pass->static_pass_number + 1);
599
600   if (!range_str)
601     {
602       uid_range_p slot;
603       uid_range_p new_range = XCNEW (struct uid_range);
604
605       new_range->start = 0;
606       new_range->last = (unsigned)-1;
607
608       slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
609       new_range->next = slot;
610       VEC_replace (uid_range_p, *tab, pass->static_pass_number,
611                    new_range);
612       if (is_enable)
613         inform (UNKNOWN_LOCATION, "enable pass %s for functions in the range "
614                 "of [%u, %u]", phase_name, new_range->start, new_range->last);
615       else
616         inform (UNKNOWN_LOCATION, "disable pass %s for functions in the range "
617                 "of [%u, %u]", phase_name, new_range->start, new_range->last);
618     }
619   else
620     {
621       char *next_range = NULL;
622       char *one_range = range_str;
623       char *end_val = NULL;
624
625       do
626         {
627           uid_range_p slot;
628           uid_range_p new_range;
629           char *invalid = NULL;
630           long start;
631
632           next_range = strchr (one_range, ',');
633           if (next_range)
634             {
635               *next_range = '\0';
636               next_range++;
637             }
638
639           end_val = strchr (one_range, ':');
640           if (end_val)
641             {
642               *end_val = '\0';
643               end_val++;
644             }
645           start = strtol (one_range, &invalid, 10);
646           if (*invalid || start < 0)
647             {
648               error ("Invalid range %s in option %s",
649                      one_range,
650                      is_enable ? "-fenable" : "-fdisable");
651               free (argstr);
652               return;
653             }
654           if (!end_val)
655             {
656               new_range = XCNEW (struct uid_range);
657               new_range->start = (unsigned) start;
658               new_range->last = (unsigned) start;
659             }
660           else
661             {
662               long last = strtol (end_val, &invalid, 10);
663               if (*invalid || last < start)
664                 {
665                   error ("Invalid range %s in option %s",
666                          end_val,
667                          is_enable ? "-fenable" : "-fdisable");
668                   free (argstr);
669                   return;
670                 }
671               new_range = XCNEW (struct uid_range);
672               new_range->start = (unsigned) start;
673               new_range->last = (unsigned) last;
674             }
675
676           slot = VEC_index (uid_range_p, *tab, pass->static_pass_number);
677           new_range->next = slot;
678           VEC_replace (uid_range_p, *tab, pass->static_pass_number,
679                        new_range);
680
681           if (is_enable)
682             inform (UNKNOWN_LOCATION,
683                     "enable pass %s for functions in the range of [%u, %u]",
684                     phase_name, new_range->start, new_range->last);
685           else
686             inform (UNKNOWN_LOCATION,
687                     "disable pass %s for functions in the range of [%u, %u]",
688                     phase_name, new_range->start, new_range->last);
689
690           one_range = next_range;
691         } while (next_range);
692     }
693
694   free (argstr);
695 }
696
697 /* Enable pass specified by ARG.  */
698
699 void
700 enable_pass (const char *arg)
701 {
702   enable_disable_pass (arg, true);
703 }
704
705 /* Disable pass specified by ARG.  */
706
707 void
708 disable_pass (const char *arg)
709 {
710   enable_disable_pass (arg, false);
711 }
712
713 /* Returns true if PASS is explicitly enabled/disabled for FUNC.  */
714
715 static bool
716 is_pass_explicitly_enabled_or_disabled (struct opt_pass *pass,
717                                         tree func,
718                                         VEC(uid_range_p, heap) *tab)
719 {
720   uid_range_p slot, range;
721   int cgraph_uid;
722
723   if (!tab
724       || (unsigned) pass->static_pass_number >= VEC_length (uid_range_p, tab)
725       || pass->static_pass_number == -1)
726     return false;
727
728   slot = VEC_index (uid_range_p, tab, pass->static_pass_number);
729   if (!slot)
730     return false;
731
732   cgraph_uid = func ? cgraph_get_node (func)->uid : 0;
733
734   range = slot;
735   while (range)
736     {
737       if ((unsigned) cgraph_uid >= range->start
738           && (unsigned) cgraph_uid <= range->last)
739         return true;
740       range = range->next;
741     }
742
743   return false;
744 }
745
746 /* Look at the static_pass_number and duplicate the pass
747    if it is already added to a list. */
748
749 static struct opt_pass *
750 make_pass_instance (struct opt_pass *pass, bool track_duplicates)
751 {
752   /* A nonzero static_pass_number indicates that the
753      pass is already in the list.  */
754   if (pass->static_pass_number)
755     {
756       struct opt_pass *new_pass;
757
758       if (pass->type == GIMPLE_PASS
759           || pass->type == RTL_PASS
760           || pass->type == SIMPLE_IPA_PASS)
761         {
762           new_pass = XNEW (struct opt_pass);
763           memcpy (new_pass, pass, sizeof (struct opt_pass));
764         }
765       else if (pass->type == IPA_PASS)
766         {
767           new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
768           memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
769         }
770       else
771         gcc_unreachable ();
772
773       new_pass->next = NULL;
774
775       new_pass->todo_flags_start &= ~TODO_mark_first_instance;
776
777       /* Indicate to register_dump_files that this pass has duplicates,
778          and so it should rename the dump file.  The first instance will
779          be -1, and be number of duplicates = -static_pass_number - 1.
780          Subsequent instances will be > 0 and just the duplicate number.  */
781       if ((pass->name && pass->name[0] != '*') || track_duplicates)
782         {
783           pass->static_pass_number -= 1;
784           new_pass->static_pass_number = -pass->static_pass_number;
785         }
786       return new_pass;
787     }
788   else
789     {
790       pass->todo_flags_start |= TODO_mark_first_instance;
791       pass->static_pass_number = -1;
792
793       invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
794     }
795   return pass;
796 }
797
798 /* Add a pass to the pass list. Duplicate the pass if it's already
799    in the list.  */
800
801 static struct opt_pass **
802 next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
803 {
804   /* Every pass should have a name so that plugins can refer to them.  */
805   gcc_assert (pass->name != NULL);
806
807   *list = make_pass_instance (pass, false);
808
809   return &(*list)->next;
810 }
811
812 /* List node for an inserted pass instance. We need to keep track of all
813    the newly-added pass instances (with 'added_pass_nodes' defined below)
814    so that we can register their dump files after pass-positioning is finished.
815    Registering dumping files needs to be post-processed or the
816    static_pass_number of the opt_pass object would be modified and mess up
817    the dump file names of future pass instances to be added.  */
818
819 struct pass_list_node
820 {
821   struct opt_pass *pass;
822   struct pass_list_node *next;
823 };
824
825 static struct pass_list_node *added_pass_nodes = NULL;
826 static struct pass_list_node *prev_added_pass_node;
827
828 /* Insert the pass at the proper position. Return true if the pass
829    is successfully added.
830
831    NEW_PASS_INFO - new pass to be inserted
832    PASS_LIST - root of the pass list to insert the new pass to  */
833
834 static bool
835 position_pass (struct register_pass_info *new_pass_info,
836                struct opt_pass **pass_list)
837 {
838   struct opt_pass *pass = *pass_list, *prev_pass = NULL;
839   bool success = false;
840
841   for ( ; pass; prev_pass = pass, pass = pass->next)
842     {
843       /* Check if the current pass is of the same type as the new pass and
844          matches the name and the instance number of the reference pass.  */
845       if (pass->type == new_pass_info->pass->type
846           && pass->name
847           && !strcmp (pass->name, new_pass_info->reference_pass_name)
848           && ((new_pass_info->ref_pass_instance_number == 0)
849               || (new_pass_info->ref_pass_instance_number ==
850                   pass->static_pass_number)
851               || (new_pass_info->ref_pass_instance_number == 1
852                   && pass->todo_flags_start & TODO_mark_first_instance)))
853         {
854           struct opt_pass *new_pass;
855           struct pass_list_node *new_pass_node;
856
857           new_pass = make_pass_instance (new_pass_info->pass, true);
858
859           /* Insert the new pass instance based on the positioning op.  */
860           switch (new_pass_info->pos_op)
861             {
862               case PASS_POS_INSERT_AFTER:
863                 new_pass->next = pass->next;
864                 pass->next = new_pass;
865
866                 /* Skip newly inserted pass to avoid repeated
867                    insertions in the case where the new pass and the
868                    existing one have the same name.  */
869                 pass = new_pass;
870                 break;
871               case PASS_POS_INSERT_BEFORE:
872                 new_pass->next = pass;
873                 if (prev_pass)
874                   prev_pass->next = new_pass;
875                 else
876                   *pass_list = new_pass;
877                 break;
878               case PASS_POS_REPLACE:
879                 new_pass->next = pass->next;
880                 if (prev_pass)
881                   prev_pass->next = new_pass;
882                 else
883                   *pass_list = new_pass;
884                 new_pass->sub = pass->sub;
885                 new_pass->tv_id = pass->tv_id;
886                 pass = new_pass;
887                 break;
888               default:
889                 error ("invalid pass positioning operation");
890                 return false;
891             }
892
893           /* Save the newly added pass (instance) in the added_pass_nodes
894              list so that we can register its dump file later. Note that
895              we cannot register the dump file now because doing so will modify
896              the static_pass_number of the opt_pass object and therefore
897              mess up the dump file name of future instances.  */
898           new_pass_node = XCNEW (struct pass_list_node);
899           new_pass_node->pass = new_pass;
900           if (!added_pass_nodes)
901             added_pass_nodes = new_pass_node;
902           else
903             prev_added_pass_node->next = new_pass_node;
904           prev_added_pass_node = new_pass_node;
905
906           success = true;
907         }
908
909       if (pass->sub && position_pass (new_pass_info, &pass->sub))
910         success = true;
911     }
912
913   return success;
914 }
915
916 /* Hooks a new pass into the pass lists.
917
918    PASS_INFO   - pass information that specifies the opt_pass object,
919                  reference pass, instance number, and how to position
920                  the pass  */
921
922 void
923 register_pass (struct register_pass_info *pass_info)
924 {
925   bool all_instances, success;
926
927   /* The checks below could fail in buggy plugins.  Existing GCC
928      passes should never fail these checks, so we mention plugin in
929      the messages.  */
930   if (!pass_info->pass)
931       fatal_error ("plugin cannot register a missing pass");
932
933   if (!pass_info->pass->name)
934       fatal_error ("plugin cannot register an unnamed pass");
935
936   if (!pass_info->reference_pass_name)
937       fatal_error
938         ("plugin cannot register pass %qs without reference pass name",
939          pass_info->pass->name);
940
941   /* Try to insert the new pass to the pass lists.  We need to check
942      all five lists as the reference pass could be in one (or all) of
943      them.  */
944   all_instances = pass_info->ref_pass_instance_number == 0;
945   success = position_pass (pass_info, &all_lowering_passes);
946   if (!success || all_instances)
947     success |= position_pass (pass_info, &all_small_ipa_passes);
948   if (!success || all_instances)
949     success |= position_pass (pass_info, &all_regular_ipa_passes);
950   if (!success || all_instances)
951     success |= position_pass (pass_info, &all_lto_gen_passes);
952   if (!success || all_instances)
953     success |= position_pass (pass_info, &all_passes);
954   if (!success)
955     fatal_error
956       ("pass %qs not found but is referenced by new pass %qs",
957        pass_info->reference_pass_name, pass_info->pass->name);
958
959   /* OK, we have successfully inserted the new pass. We need to register
960      the dump files for the newly added pass and its duplicates (if any).
961      Because the registration of plugin/backend passes happens after the
962      command-line options are parsed, the options that specify single
963      pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
964      passes. Therefore we currently can only enable dumping of
965      new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
966      are specified. While doing so, we also delete the pass_list_node
967      objects created during pass positioning.  */
968   while (added_pass_nodes)
969     {
970       struct pass_list_node *next_node = added_pass_nodes->next;
971       enum tree_dump_index tdi;
972       register_one_dump_file (added_pass_nodes->pass);
973       if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
974           || added_pass_nodes->pass->type == IPA_PASS)
975         tdi = TDI_ipa_all;
976       else if (added_pass_nodes->pass->type == GIMPLE_PASS)
977         tdi = TDI_tree_all;
978       else
979         tdi = TDI_rtl_all;
980       /* Check if dump-all flag is specified.  */
981       if (get_dump_file_info (tdi)->state)
982         get_dump_file_info (added_pass_nodes->pass->static_pass_number)
983             ->state = get_dump_file_info (tdi)->state;
984       XDELETE (added_pass_nodes);
985       added_pass_nodes = next_node;
986     }
987 }
988
989 /* Construct the pass tree.  The sequencing of passes is driven by
990    the cgraph routines:
991
992    cgraph_finalize_compilation_unit ()
993        for each node N in the cgraph
994            cgraph_analyze_function (N)
995                cgraph_lower_function (N) -> all_lowering_passes
996
997    If we are optimizing, cgraph_optimize is then invoked:
998
999    cgraph_optimize ()
1000        ipa_passes ()                    -> all_small_ipa_passes
1001        cgraph_expand_all_functions ()
1002            for each node N in the cgraph
1003                cgraph_expand_function (N)
1004                   tree_rest_of_compilation (DECL (N))  -> all_passes
1005 */
1006
1007 void
1008 init_optimization_passes (void)
1009 {
1010   struct opt_pass **p;
1011
1012 #define NEXT_PASS(PASS)  (p = next_pass_1 (p, &((PASS).pass)))
1013
1014  /* All passes needed to lower the function into shape optimizers can
1015     operate on.  These passes are always run first on the function, but
1016     backend might produce already lowered functions that are not processed
1017     by these passes.  */
1018   p = &all_lowering_passes;
1019   NEXT_PASS (pass_warn_unused_result);
1020   NEXT_PASS (pass_diagnose_omp_blocks);
1021   NEXT_PASS (pass_mudflap_1);
1022   NEXT_PASS (pass_lower_omp);
1023   NEXT_PASS (pass_lower_cf);
1024   NEXT_PASS (pass_refactor_eh);
1025   NEXT_PASS (pass_lower_eh);
1026   NEXT_PASS (pass_build_cfg);
1027   NEXT_PASS (pass_warn_function_return);
1028   NEXT_PASS (pass_build_cgraph_edges);
1029   *p = NULL;
1030
1031   /* Interprocedural optimization passes.  */
1032   p = &all_small_ipa_passes;
1033   NEXT_PASS (pass_ipa_free_lang_data);
1034   NEXT_PASS (pass_ipa_function_and_variable_visibility);
1035   NEXT_PASS (pass_early_local_passes);
1036     {
1037       struct opt_pass **p = &pass_early_local_passes.pass.sub;
1038       NEXT_PASS (pass_fixup_cfg);
1039       NEXT_PASS (pass_init_datastructures);
1040       NEXT_PASS (pass_expand_omp);
1041
1042       NEXT_PASS (pass_referenced_vars);
1043       NEXT_PASS (pass_build_ssa);
1044       NEXT_PASS (pass_lower_vector);
1045       NEXT_PASS (pass_early_warn_uninitialized);
1046       NEXT_PASS (pass_rebuild_cgraph_edges);
1047       NEXT_PASS (pass_inline_parameters);
1048       NEXT_PASS (pass_early_inline);
1049       NEXT_PASS (pass_all_early_optimizations);
1050         {
1051           struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
1052           NEXT_PASS (pass_remove_cgraph_callee_edges);
1053           NEXT_PASS (pass_rename_ssa_copies);
1054           NEXT_PASS (pass_ccp);
1055           NEXT_PASS (pass_forwprop);
1056           /* pass_build_ealias is a dummy pass that ensures that we
1057              execute TODO_rebuild_alias at this point.  Re-building
1058              alias information also rewrites no longer addressed
1059              locals into SSA form if possible.  */
1060           NEXT_PASS (pass_build_ealias);
1061           NEXT_PASS (pass_sra_early);
1062           NEXT_PASS (pass_fre);
1063           NEXT_PASS (pass_copy_prop);
1064           NEXT_PASS (pass_merge_phi);
1065           NEXT_PASS (pass_cd_dce);
1066           NEXT_PASS (pass_early_ipa_sra);
1067           NEXT_PASS (pass_tail_recursion);
1068           NEXT_PASS (pass_convert_switch);
1069           NEXT_PASS (pass_cleanup_eh);
1070           NEXT_PASS (pass_profile);
1071           NEXT_PASS (pass_local_pure_const);
1072           /* Split functions creates parts that are not run through
1073              early optimizations again.  It is thus good idea to do this
1074              late.  */
1075           NEXT_PASS (pass_split_functions);
1076         }
1077       NEXT_PASS (pass_release_ssa_names);
1078       NEXT_PASS (pass_rebuild_cgraph_edges);
1079       NEXT_PASS (pass_inline_parameters);
1080     }
1081   NEXT_PASS (pass_ipa_tree_profile);
1082     {
1083       struct opt_pass **p = &pass_ipa_tree_profile.pass.sub;
1084       NEXT_PASS (pass_feedback_split_functions);
1085     }
1086   NEXT_PASS (pass_ipa_increase_alignment);
1087   NEXT_PASS (pass_ipa_matrix_reorg);
1088   NEXT_PASS (pass_ipa_lower_emutls);
1089   *p = NULL;
1090
1091   p = &all_regular_ipa_passes;
1092   NEXT_PASS (pass_ipa_whole_program_visibility);
1093   NEXT_PASS (pass_ipa_profile);
1094   NEXT_PASS (pass_ipa_cp);
1095   NEXT_PASS (pass_ipa_cdtor_merge);
1096   NEXT_PASS (pass_ipa_inline);
1097   NEXT_PASS (pass_ipa_pure_const);
1098   NEXT_PASS (pass_ipa_reference);
1099   NEXT_PASS (pass_ipa_pta);
1100   *p = NULL;
1101
1102   p = &all_lto_gen_passes;
1103   NEXT_PASS (pass_ipa_lto_gimple_out);
1104   NEXT_PASS (pass_ipa_lto_finish_out);  /* This must be the last LTO pass.  */
1105   *p = NULL;
1106
1107   /* These passes are run after IPA passes on every function that is being
1108      output to the assembler file.  */
1109   p = &all_passes;
1110   NEXT_PASS (pass_lower_eh_dispatch);
1111   NEXT_PASS (pass_all_optimizations);
1112     {
1113       struct opt_pass **p = &pass_all_optimizations.pass.sub;
1114       NEXT_PASS (pass_remove_cgraph_callee_edges);
1115       /* Initial scalar cleanups before alias computation.
1116          They ensure memory accesses are not indirect wherever possible.  */
1117       NEXT_PASS (pass_strip_predict_hints);
1118       NEXT_PASS (pass_rename_ssa_copies);
1119       NEXT_PASS (pass_complete_unrolli);
1120       NEXT_PASS (pass_ccp);
1121       NEXT_PASS (pass_forwprop);
1122       NEXT_PASS (pass_call_cdce);
1123       /* pass_build_alias is a dummy pass that ensures that we
1124          execute TODO_rebuild_alias at this point.  Re-building
1125          alias information also rewrites no longer addressed
1126          locals into SSA form if possible.  */
1127       NEXT_PASS (pass_build_alias);
1128       NEXT_PASS (pass_return_slot);
1129       NEXT_PASS (pass_phiprop);
1130       NEXT_PASS (pass_fre);
1131       NEXT_PASS (pass_copy_prop);
1132       NEXT_PASS (pass_merge_phi);
1133       NEXT_PASS (pass_vrp);
1134       NEXT_PASS (pass_dce);
1135       NEXT_PASS (pass_cselim);
1136       NEXT_PASS (pass_tree_ifcombine);
1137       NEXT_PASS (pass_phiopt);
1138       NEXT_PASS (pass_tail_recursion);
1139       NEXT_PASS (pass_ch);
1140       NEXT_PASS (pass_stdarg);
1141       NEXT_PASS (pass_lower_complex);
1142       NEXT_PASS (pass_sra);
1143       NEXT_PASS (pass_rename_ssa_copies);
1144       /* The dom pass will also resolve all __builtin_constant_p calls
1145          that are still there to 0.  This has to be done after some
1146          propagations have already run, but before some more dead code
1147          is removed, and this place fits nicely.  Remember this when
1148          trying to move or duplicate pass_dominator somewhere earlier.  */
1149       NEXT_PASS (pass_dominator);
1150       /* The only const/copy propagation opportunities left after
1151          DOM should be due to degenerate PHI nodes.  So rather than
1152          run the full propagators, run a specialized pass which
1153          only examines PHIs to discover const/copy propagation
1154          opportunities.  */
1155       NEXT_PASS (pass_phi_only_cprop);
1156       NEXT_PASS (pass_dse);
1157       NEXT_PASS (pass_reassoc);
1158       NEXT_PASS (pass_dce);
1159       NEXT_PASS (pass_forwprop);
1160       NEXT_PASS (pass_phiopt);
1161       NEXT_PASS (pass_object_sizes);
1162       NEXT_PASS (pass_ccp);
1163       NEXT_PASS (pass_copy_prop);
1164       NEXT_PASS (pass_cse_sincos);
1165       NEXT_PASS (pass_optimize_bswap);
1166       NEXT_PASS (pass_split_crit_edges);
1167       NEXT_PASS (pass_pre);
1168       NEXT_PASS (pass_sink_code);
1169       NEXT_PASS (pass_tree_loop);
1170         {
1171           struct opt_pass **p = &pass_tree_loop.pass.sub;
1172           NEXT_PASS (pass_tree_loop_init);
1173           NEXT_PASS (pass_lim);
1174           NEXT_PASS (pass_copy_prop);
1175           NEXT_PASS (pass_dce_loop);
1176           NEXT_PASS (pass_tree_unswitch);
1177           NEXT_PASS (pass_scev_cprop);
1178           NEXT_PASS (pass_record_bounds);
1179           NEXT_PASS (pass_check_data_deps);
1180           NEXT_PASS (pass_loop_distribution);
1181           NEXT_PASS (pass_copy_prop);
1182           NEXT_PASS (pass_graphite);
1183             {
1184               struct opt_pass **p = &pass_graphite.pass.sub;
1185               NEXT_PASS (pass_graphite_transforms);
1186               NEXT_PASS (pass_lim);
1187               NEXT_PASS (pass_copy_prop);
1188               NEXT_PASS (pass_dce_loop);
1189             }
1190           NEXT_PASS (pass_iv_canon);
1191           NEXT_PASS (pass_if_conversion);
1192           NEXT_PASS (pass_vectorize);
1193             {
1194               struct opt_pass **p = &pass_vectorize.pass.sub;
1195               NEXT_PASS (pass_lower_vector_ssa);
1196               NEXT_PASS (pass_dce_loop);
1197             }
1198           NEXT_PASS (pass_predcom);
1199           NEXT_PASS (pass_complete_unroll);
1200           NEXT_PASS (pass_slp_vectorize);
1201           NEXT_PASS (pass_parallelize_loops);
1202           NEXT_PASS (pass_loop_prefetch);
1203           NEXT_PASS (pass_iv_optimize);
1204           NEXT_PASS (pass_tree_loop_done);
1205         }
1206       NEXT_PASS (pass_cse_reciprocals);
1207       NEXT_PASS (pass_reassoc);
1208       NEXT_PASS (pass_vrp);
1209       NEXT_PASS (pass_dominator);
1210       /* The only const/copy propagation opportunities left after
1211          DOM should be due to degenerate PHI nodes.  So rather than
1212          run the full propagators, run a specialized pass which
1213          only examines PHIs to discover const/copy propagation
1214          opportunities.  */
1215       NEXT_PASS (pass_phi_only_cprop);
1216       NEXT_PASS (pass_cd_dce);
1217       NEXT_PASS (pass_tracer);
1218
1219       /* FIXME: If DCE is not run before checking for uninitialized uses,
1220          we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
1221          However, this also causes us to misdiagnose cases that should be
1222          real warnings (e.g., testsuite/gcc.dg/pr18501.c).
1223
1224          To fix the false positives in uninit-5.c, we would have to
1225          account for the predicates protecting the set and the use of each
1226          variable.  Using a representation like Gated Single Assignment
1227          may help.  */
1228       NEXT_PASS (pass_late_warn_uninitialized);
1229       NEXT_PASS (pass_dse);
1230       NEXT_PASS (pass_forwprop);
1231       NEXT_PASS (pass_phiopt);
1232       NEXT_PASS (pass_fold_builtins);
1233       NEXT_PASS (pass_optimize_widening_mul);
1234       NEXT_PASS (pass_tail_calls);
1235       NEXT_PASS (pass_rename_ssa_copies);
1236       NEXT_PASS (pass_uncprop);
1237       NEXT_PASS (pass_local_pure_const);
1238     }
1239   NEXT_PASS (pass_lower_complex_O0);
1240   NEXT_PASS (pass_cleanup_eh);
1241   NEXT_PASS (pass_lower_resx);
1242   NEXT_PASS (pass_nrv);
1243   NEXT_PASS (pass_mudflap_2);
1244   NEXT_PASS (pass_cleanup_cfg_post_optimizing);
1245   NEXT_PASS (pass_warn_function_noreturn);
1246
1247   NEXT_PASS (pass_expand);
1248
1249   NEXT_PASS (pass_rest_of_compilation);
1250     {
1251       struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
1252       NEXT_PASS (pass_init_function);
1253       NEXT_PASS (pass_jump);
1254       NEXT_PASS (pass_rtl_eh);
1255       NEXT_PASS (pass_initial_value_sets);
1256       NEXT_PASS (pass_unshare_all_rtl);
1257       NEXT_PASS (pass_instantiate_virtual_regs);
1258       NEXT_PASS (pass_into_cfg_layout_mode);
1259       NEXT_PASS (pass_jump2);
1260       NEXT_PASS (pass_lower_subreg);
1261       NEXT_PASS (pass_df_initialize_opt);
1262       NEXT_PASS (pass_cse);
1263       NEXT_PASS (pass_rtl_fwprop);
1264       NEXT_PASS (pass_rtl_cprop);
1265       NEXT_PASS (pass_rtl_pre);
1266       NEXT_PASS (pass_rtl_hoist);
1267       NEXT_PASS (pass_rtl_cprop);
1268       NEXT_PASS (pass_rtl_store_motion);
1269       NEXT_PASS (pass_cse_after_global_opts);
1270       NEXT_PASS (pass_rtl_ifcvt);
1271       NEXT_PASS (pass_reginfo_init);
1272       /* Perform loop optimizations.  It might be better to do them a bit
1273          sooner, but we want the profile feedback to work more
1274          efficiently.  */
1275       NEXT_PASS (pass_loop2);
1276         {
1277           struct opt_pass **p = &pass_loop2.pass.sub;
1278           NEXT_PASS (pass_rtl_loop_init);
1279           NEXT_PASS (pass_rtl_move_loop_invariants);
1280           NEXT_PASS (pass_rtl_unswitch);
1281           NEXT_PASS (pass_rtl_unroll_and_peel_loops);
1282           NEXT_PASS (pass_rtl_doloop);
1283           NEXT_PASS (pass_rtl_loop_done);
1284           *p = NULL;
1285         }
1286       NEXT_PASS (pass_web);
1287       NEXT_PASS (pass_rtl_cprop);
1288       NEXT_PASS (pass_cse2);
1289       NEXT_PASS (pass_rtl_dse1);
1290       NEXT_PASS (pass_rtl_fwprop_addr);
1291       NEXT_PASS (pass_inc_dec);
1292       NEXT_PASS (pass_initialize_regs);
1293       NEXT_PASS (pass_ud_rtl_dce);
1294       NEXT_PASS (pass_combine);
1295       NEXT_PASS (pass_if_after_combine);
1296       NEXT_PASS (pass_partition_blocks);
1297       NEXT_PASS (pass_regmove);
1298       NEXT_PASS (pass_outof_cfg_layout_mode);
1299       NEXT_PASS (pass_split_all_insns);
1300       NEXT_PASS (pass_lower_subreg2);
1301       NEXT_PASS (pass_df_initialize_no_opt);
1302       NEXT_PASS (pass_stack_ptr_mod);
1303       NEXT_PASS (pass_mode_switching);
1304       NEXT_PASS (pass_match_asm_constraints);
1305       NEXT_PASS (pass_sms);
1306       NEXT_PASS (pass_sched);
1307       NEXT_PASS (pass_ira);
1308       NEXT_PASS (pass_postreload);
1309         {
1310           struct opt_pass **p = &pass_postreload.pass.sub;
1311           NEXT_PASS (pass_postreload_cse);
1312           NEXT_PASS (pass_gcse2);
1313           NEXT_PASS (pass_split_after_reload);
1314           NEXT_PASS (pass_implicit_zee);
1315           NEXT_PASS (pass_compare_elim_after_reload);
1316           NEXT_PASS (pass_branch_target_load_optimize1);
1317           NEXT_PASS (pass_thread_prologue_and_epilogue);
1318           NEXT_PASS (pass_rtl_dse2);
1319           NEXT_PASS (pass_stack_adjustments);
1320           NEXT_PASS (pass_peephole2);
1321           NEXT_PASS (pass_if_after_reload);
1322           NEXT_PASS (pass_regrename);
1323           NEXT_PASS (pass_cprop_hardreg);
1324           NEXT_PASS (pass_fast_rtl_dce);
1325           NEXT_PASS (pass_reorder_blocks);
1326           NEXT_PASS (pass_branch_target_load_optimize2);
1327           NEXT_PASS (pass_leaf_regs);
1328           NEXT_PASS (pass_split_before_sched2);
1329           NEXT_PASS (pass_sched2);
1330           NEXT_PASS (pass_stack_regs);
1331             {
1332               struct opt_pass **p = &pass_stack_regs.pass.sub;
1333               NEXT_PASS (pass_split_before_regstack);
1334               NEXT_PASS (pass_stack_regs_run);
1335             }
1336           NEXT_PASS (pass_compute_alignments);
1337           NEXT_PASS (pass_duplicate_computed_gotos);
1338           NEXT_PASS (pass_variable_tracking);
1339           NEXT_PASS (pass_free_cfg);
1340           NEXT_PASS (pass_machine_reorg);
1341           NEXT_PASS (pass_cleanup_barriers);
1342           NEXT_PASS (pass_delay_slots);
1343           NEXT_PASS (pass_split_for_shorten_branches);
1344           NEXT_PASS (pass_convert_to_eh_region_ranges);
1345           NEXT_PASS (pass_shorten_branches);
1346           NEXT_PASS (pass_set_nothrow_function_flags);
1347           NEXT_PASS (pass_final);
1348         }
1349       NEXT_PASS (pass_df_finish);
1350     }
1351   NEXT_PASS (pass_clean_state);
1352   *p = NULL;
1353
1354 #undef NEXT_PASS
1355
1356   /* Register the passes with the tree dump code.  */
1357   register_dump_files (all_lowering_passes, PROP_gimple_any);
1358   register_dump_files (all_small_ipa_passes,
1359                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1360                        | PROP_cfg);
1361   register_dump_files (all_regular_ipa_passes,
1362                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1363                        | PROP_cfg);
1364   register_dump_files (all_lto_gen_passes,
1365                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1366                        | PROP_cfg);
1367   register_dump_files (all_passes,
1368                        PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1369                        | PROP_cfg);
1370 }
1371
1372 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1373    function CALLBACK for every function in the call graph.  Otherwise,
1374    call CALLBACK on the current function.  */
1375
1376 static void
1377 do_per_function (void (*callback) (void *data), void *data)
1378 {
1379   if (current_function_decl)
1380     callback (data);
1381   else
1382     {
1383       struct cgraph_node *node;
1384       for (node = cgraph_nodes; node; node = node->next)
1385         if (node->analyzed && gimple_has_body_p (node->decl)
1386             && (!node->clone_of || node->decl != node->clone_of->decl))
1387           {
1388             push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1389             current_function_decl = node->decl;
1390             callback (data);
1391             if (!flag_wpa)
1392               {
1393                 free_dominance_info (CDI_DOMINATORS);
1394                 free_dominance_info (CDI_POST_DOMINATORS);
1395               }
1396             current_function_decl = NULL;
1397             pop_cfun ();
1398             ggc_collect ();
1399           }
1400     }
1401 }
1402
1403 /* Because inlining might remove no-longer reachable nodes, we need to
1404    keep the array visible to garbage collector to avoid reading collected
1405    out nodes.  */
1406 static int nnodes;
1407 static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
1408
1409 /* If we are in IPA mode (i.e., current_function_decl is NULL), call
1410    function CALLBACK for every function in the call graph.  Otherwise,
1411    call CALLBACK on the current function.
1412    This function is global so that plugins can use it.  */
1413 void
1414 do_per_function_toporder (void (*callback) (void *data), void *data)
1415 {
1416   int i;
1417
1418   if (current_function_decl)
1419     callback (data);
1420   else
1421     {
1422       gcc_assert (!order);
1423       order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
1424       nnodes = ipa_reverse_postorder (order);
1425       for (i = nnodes - 1; i >= 0; i--)
1426         order[i]->process = 1;
1427       for (i = nnodes - 1; i >= 0; i--)
1428         {
1429           struct cgraph_node *node = order[i];
1430
1431           /* Allow possibly removed nodes to be garbage collected.  */
1432           order[i] = NULL;
1433           node->process = 0;
1434           if (cgraph_function_with_gimple_body_p (node))
1435             {
1436               push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1437               current_function_decl = node->decl;
1438               callback (data);
1439               free_dominance_info (CDI_DOMINATORS);
1440               free_dominance_info (CDI_POST_DOMINATORS);
1441               current_function_decl = NULL;
1442               pop_cfun ();
1443               ggc_collect ();
1444             }
1445         }
1446     }
1447   ggc_free (order);
1448   order = NULL;
1449   nnodes = 0;
1450 }
1451
1452 /* Perform all TODO actions that ought to be done on each function.  */
1453
1454 static void
1455 execute_function_todo (void *data)
1456 {
1457   unsigned int flags = (size_t)data;
1458   flags &= ~cfun->last_verified;
1459   if (!flags)
1460     return;
1461
1462   /* Always cleanup the CFG before trying to update SSA.  */
1463   if (flags & TODO_cleanup_cfg)
1464     {
1465       bool cleanup = cleanup_tree_cfg ();
1466
1467       if (cleanup && (cfun->curr_properties & PROP_ssa))
1468         flags |= TODO_remove_unused_locals;
1469
1470       /* When cleanup_tree_cfg merges consecutive blocks, it may
1471          perform some simplistic propagation when removing single
1472          valued PHI nodes.  This propagation may, in turn, cause the
1473          SSA form to become out-of-date (see PR 22037).  So, even
1474          if the parent pass had not scheduled an SSA update, we may
1475          still need to do one.  */
1476       if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
1477         flags |= TODO_update_ssa;
1478     }
1479
1480   if (flags & TODO_update_ssa_any)
1481     {
1482       unsigned update_flags = flags & TODO_update_ssa_any;
1483       update_ssa (update_flags);
1484       cfun->last_verified &= ~TODO_verify_ssa;
1485     }
1486
1487   if (flags & TODO_rebuild_alias)
1488     {
1489       execute_update_addresses_taken ();
1490       compute_may_aliases ();
1491     }
1492   else if (optimize && (flags & TODO_update_address_taken))
1493     execute_update_addresses_taken ();
1494
1495   if (flags & TODO_remove_unused_locals)
1496     remove_unused_locals ();
1497
1498   if ((flags & TODO_dump_func) && dump_file && current_function_decl)
1499     {
1500       if (cfun->curr_properties & PROP_trees)
1501         dump_function_to_file (current_function_decl, dump_file, dump_flags);
1502       else
1503         {
1504           if (dump_flags & TDF_SLIM)
1505             print_rtl_slim_with_bb (dump_file, get_insns (), dump_flags);
1506           else if ((cfun->curr_properties & PROP_cfg)
1507                    && (dump_flags & TDF_BLOCKS))
1508             print_rtl_with_bb (dump_file, get_insns ());
1509           else
1510             print_rtl (dump_file, get_insns ());
1511
1512           if ((cfun->curr_properties & PROP_cfg)
1513               && graph_dump_format != no_graph
1514               && (dump_flags & TDF_GRAPH))
1515             print_rtl_graph_with_bb (dump_file_name, get_insns ());
1516         }
1517
1518       /* Flush the file.  If verification fails, we won't be able to
1519          close the file before aborting.  */
1520       fflush (dump_file);
1521     }
1522
1523   if (flags & TODO_rebuild_frequencies)
1524     rebuild_frequencies ();
1525
1526   if (flags & TODO_rebuild_cgraph_edges)
1527     rebuild_cgraph_edges ();
1528
1529   /* If we've seen errors do not bother running any verifiers.  */
1530   if (seen_error ())
1531     return;
1532
1533 #if defined ENABLE_CHECKING
1534   if (flags & TODO_verify_ssa
1535       || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
1536     verify_ssa (true);
1537   if (flags & TODO_verify_flow)
1538     verify_flow_info ();
1539   if (flags & TODO_verify_stmts)
1540     verify_gimple_in_cfg (cfun);
1541   if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
1542     verify_loop_closed_ssa (false);
1543   if (flags & TODO_verify_rtl_sharing)
1544     verify_rtl_sharing ();
1545 #endif
1546
1547   cfun->last_verified = flags & TODO_verify_all;
1548 }
1549
1550 /* Perform all TODO actions.  */
1551 static void
1552 execute_todo (unsigned int flags)
1553 {
1554 #if defined ENABLE_CHECKING
1555   if (cfun
1556       && need_ssa_update_p (cfun))
1557     gcc_assert (flags & TODO_update_ssa_any);
1558 #endif
1559
1560   timevar_push (TV_TODO);
1561
1562   /* Inform the pass whether it is the first time it is run.  */
1563   first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1564
1565   statistics_fini_pass ();
1566
1567   do_per_function (execute_function_todo, (void *)(size_t) flags);
1568
1569   /* Always remove functions just as before inlining: IPA passes might be
1570      interested to see bodies of extern inline functions that are not inlined
1571      to analyze side effects.  The full removal is done just at the end
1572      of IPA pass queue.  */
1573   if (flags & TODO_remove_functions)
1574     {
1575       gcc_assert (!cfun);
1576       cgraph_remove_unreachable_nodes (true, dump_file);
1577     }
1578
1579   if ((flags & TODO_dump_cgraph) && dump_file && !current_function_decl)
1580     {
1581       gcc_assert (!cfun);
1582       dump_cgraph (dump_file);
1583       /* Flush the file.  If verification fails, we won't be able to
1584          close the file before aborting.  */
1585       fflush (dump_file);
1586     }
1587
1588   if (flags & TODO_ggc_collect)
1589     ggc_collect ();
1590
1591   /* Now that the dumping has been done, we can get rid of the optional
1592      df problems.  */
1593   if (flags & TODO_df_finish)
1594     df_finish_pass ((flags & TODO_df_verify) != 0);
1595
1596   timevar_pop (TV_TODO);
1597 }
1598
1599 /* Verify invariants that should hold between passes.  This is a place
1600    to put simple sanity checks.  */
1601
1602 static void
1603 verify_interpass_invariants (void)
1604 {
1605   gcc_checking_assert (!fold_deferring_overflow_warnings_p ());
1606 }
1607
1608 /* Clear the last verified flag.  */
1609
1610 static void
1611 clear_last_verified (void *data ATTRIBUTE_UNUSED)
1612 {
1613   cfun->last_verified = 0;
1614 }
1615
1616 /* Helper function. Verify that the properties has been turn into the
1617    properties expected by the pass.  */
1618
1619 #ifdef ENABLE_CHECKING
1620 static void
1621 verify_curr_properties (void *data)
1622 {
1623   unsigned int props = (size_t)data;
1624   gcc_assert ((cfun->curr_properties & props) == props);
1625 }
1626 #endif
1627
1628 /* Initialize pass dump file.  */
1629 /* This is non-static so that the plugins can use it.  */
1630
1631 bool
1632 pass_init_dump_file (struct opt_pass *pass)
1633 {
1634   /* If a dump file name is present, open it if enabled.  */
1635   if (pass->static_pass_number != -1)
1636     {
1637       bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
1638       dump_file_name = get_dump_file_name (pass->static_pass_number);
1639       dump_file = dump_begin (pass->static_pass_number, &dump_flags);
1640       if (dump_file && current_function_decl)
1641         dump_function_header (dump_file, current_function_decl);
1642       return initializing_dump;
1643     }
1644   else
1645     return false;
1646 }
1647
1648 /* Flush PASS dump file.  */
1649 /* This is non-static so that plugins can use it.  */
1650
1651 void
1652 pass_fini_dump_file (struct opt_pass *pass)
1653 {
1654   /* Flush and close dump file.  */
1655   if (dump_file_name)
1656     {
1657       free (CONST_CAST (char *, dump_file_name));
1658       dump_file_name = NULL;
1659     }
1660
1661   if (dump_file)
1662     {
1663       dump_end (pass->static_pass_number, dump_file);
1664       dump_file = NULL;
1665     }
1666 }
1667
1668 /* After executing the pass, apply expected changes to the function
1669    properties. */
1670
1671 static void
1672 update_properties_after_pass (void *data)
1673 {
1674   struct opt_pass *pass = (struct opt_pass *) data;
1675   cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1676                            & ~pass->properties_destroyed;
1677 }
1678
1679 /* Execute summary generation for all of the passes in IPA_PASS.  */
1680
1681 void
1682 execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
1683 {
1684   while (ipa_pass)
1685     {
1686       struct opt_pass *pass = &ipa_pass->pass;
1687
1688       /* Execute all of the IPA_PASSes in the list.  */
1689       if (ipa_pass->pass.type == IPA_PASS
1690           && (!pass->gate || pass->gate ())
1691           && ipa_pass->generate_summary)
1692         {
1693           pass_init_dump_file (pass);
1694
1695           /* If a timevar is present, start it.  */
1696           if (pass->tv_id)
1697             timevar_push (pass->tv_id);
1698
1699           ipa_pass->generate_summary ();
1700
1701           /* Stop timevar.  */
1702           if (pass->tv_id)
1703             timevar_pop (pass->tv_id);
1704
1705           pass_fini_dump_file (pass);
1706         }
1707       ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
1708     }
1709 }
1710
1711 /* Execute IPA_PASS function transform on NODE.  */
1712
1713 static void
1714 execute_one_ipa_transform_pass (struct cgraph_node *node,
1715                                 struct ipa_opt_pass_d *ipa_pass)
1716 {
1717   struct opt_pass *pass = &ipa_pass->pass;
1718   unsigned int todo_after = 0;
1719
1720   current_pass = pass;
1721   if (!ipa_pass->function_transform)
1722     return;
1723
1724   /* Note that the folders should only create gimple expressions.
1725      This is a hack until the new folder is ready.  */
1726   in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1727
1728   pass_init_dump_file (pass);
1729
1730   /* Run pre-pass verification.  */
1731   execute_todo (ipa_pass->function_transform_todo_flags_start);
1732
1733   /* If a timevar is present, start it.  */
1734   if (pass->tv_id != TV_NONE)
1735     timevar_push (pass->tv_id);
1736
1737   /* Do it!  */
1738   todo_after = ipa_pass->function_transform (node);
1739
1740   /* Stop timevar.  */
1741   if (pass->tv_id != TV_NONE)
1742     timevar_pop (pass->tv_id);
1743
1744   /* Run post-pass cleanup and verification.  */
1745   execute_todo (todo_after);
1746   verify_interpass_invariants ();
1747
1748   pass_fini_dump_file (pass);
1749
1750   current_pass = NULL;
1751 }
1752
1753 /* For the current function, execute all ipa transforms. */
1754
1755 void
1756 execute_all_ipa_transforms (void)
1757 {
1758   struct cgraph_node *node;
1759   if (!cfun)
1760     return;
1761   node = cgraph_get_node (current_function_decl);
1762
1763   if (node->ipa_transforms_to_apply)
1764     {
1765       unsigned int i;
1766
1767       for (i = 0; i < VEC_length (ipa_opt_pass, node->ipa_transforms_to_apply);
1768            i++)
1769         execute_one_ipa_transform_pass (node,
1770                                         VEC_index (ipa_opt_pass,
1771                                                    node->ipa_transforms_to_apply,
1772                                                    i));
1773       VEC_free (ipa_opt_pass, heap, node->ipa_transforms_to_apply);
1774       node->ipa_transforms_to_apply = NULL;
1775     }
1776 }
1777
1778 /* Check if PASS is explicitly disabled or enabled and return
1779    the gate status.  FUNC is the function to be processed, and
1780    GATE_STATUS is the gate status determined by pass manager by
1781    default.  */
1782
1783 static bool
1784 override_gate_status (struct opt_pass *pass, tree func, bool gate_status)
1785 {
1786   bool explicitly_enabled = false;
1787   bool explicitly_disabled = false;
1788
1789   explicitly_enabled
1790    = is_pass_explicitly_enabled_or_disabled (pass, func,
1791                                              enabled_pass_uid_range_tab);
1792   explicitly_disabled
1793    = is_pass_explicitly_enabled_or_disabled (pass, func,
1794                                              disabled_pass_uid_range_tab);
1795
1796   gate_status = !explicitly_disabled && (gate_status || explicitly_enabled);
1797
1798   return gate_status;
1799 }
1800
1801
1802 /* Execute PASS. */
1803
1804 bool
1805 execute_one_pass (struct opt_pass *pass)
1806 {
1807   bool initializing_dump;
1808   unsigned int todo_after = 0;
1809
1810   bool gate_status;
1811
1812   /* IPA passes are executed on whole program, so cfun should be NULL.
1813      Other passes need function context set.  */
1814   if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
1815     gcc_assert (!cfun && !current_function_decl);
1816   else
1817     gcc_assert (cfun && current_function_decl);
1818
1819   current_pass = pass;
1820
1821   /* Check whether gate check should be avoided.
1822      User controls the value of the gate through the parameter "gate_status". */
1823   gate_status = (pass->gate == NULL) ? true : pass->gate();
1824   gate_status = override_gate_status (pass, current_function_decl, gate_status);
1825
1826   /* Override gate with plugin.  */
1827   invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
1828
1829   if (!gate_status)
1830     {
1831       current_pass = NULL;
1832       return false;
1833     }
1834
1835   /* Pass execution event trigger: useful to identify passes being
1836      executed.  */
1837   invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
1838
1839   if (!quiet_flag && !cfun)
1840     fprintf (stderr, " <%s>", pass->name ? pass->name : "");
1841
1842   /* Note that the folders should only create gimple expressions.
1843      This is a hack until the new folder is ready.  */
1844   in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1845
1846   initializing_dump = pass_init_dump_file (pass);
1847
1848   /* Run pre-pass verification.  */
1849   execute_todo (pass->todo_flags_start);
1850
1851 #ifdef ENABLE_CHECKING
1852   do_per_function (verify_curr_properties,
1853                    (void *)(size_t)pass->properties_required);
1854 #endif
1855
1856   /* If a timevar is present, start it.  */
1857   if (pass->tv_id != TV_NONE)
1858     timevar_push (pass->tv_id);
1859
1860   /* Do it!  */
1861   if (pass->execute)
1862     {
1863       todo_after = pass->execute ();
1864       do_per_function (clear_last_verified, NULL);
1865     }
1866
1867   /* Stop timevar.  */
1868   if (pass->tv_id != TV_NONE)
1869     timevar_pop (pass->tv_id);
1870
1871   do_per_function (update_properties_after_pass, pass);
1872
1873   if (initializing_dump
1874       && dump_file
1875       && graph_dump_format != no_graph
1876       && cfun
1877       && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
1878           == (PROP_cfg | PROP_rtl))
1879     {
1880       get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
1881       dump_flags |= TDF_GRAPH;
1882       clean_graph_dump_file (dump_file_name);
1883     }
1884
1885   /* Run post-pass cleanup and verification.  */
1886   execute_todo (todo_after | pass->todo_flags_finish);
1887   verify_interpass_invariants ();
1888   if (pass->type == IPA_PASS)
1889     {
1890       struct cgraph_node *node;
1891       FOR_EACH_FUNCTION_WITH_GIMPLE_BODY (node)
1892         VEC_safe_push (ipa_opt_pass, heap, node->ipa_transforms_to_apply,
1893                        (struct ipa_opt_pass_d *)pass);
1894     }
1895
1896   if (!current_function_decl)
1897     cgraph_process_new_functions ();
1898
1899   pass_fini_dump_file (pass);
1900
1901   if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
1902     gcc_assert (!(cfun->curr_properties & PROP_trees)
1903                 || pass->type != RTL_PASS);
1904
1905   current_pass = NULL;
1906
1907   return true;
1908 }
1909
1910 void
1911 execute_pass_list (struct opt_pass *pass)
1912 {
1913   do
1914     {
1915       gcc_assert (pass->type == GIMPLE_PASS
1916                   || pass->type == RTL_PASS);
1917       if (execute_one_pass (pass) && pass->sub)
1918         execute_pass_list (pass->sub);
1919       pass = pass->next;
1920     }
1921   while (pass);
1922 }
1923
1924 /* Same as execute_pass_list but assume that subpasses of IPA passes
1925    are local passes. If SET is not NULL, write out summaries of only
1926    those node in SET. */
1927
1928 static void
1929 ipa_write_summaries_2 (struct opt_pass *pass, cgraph_node_set set,
1930                        varpool_node_set vset,
1931                        struct lto_out_decl_state *state)
1932 {
1933   while (pass)
1934     {
1935       struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
1936       gcc_assert (!current_function_decl);
1937       gcc_assert (!cfun);
1938       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1939       if (pass->type == IPA_PASS
1940           && ipa_pass->write_summary
1941           && (!pass->gate || pass->gate ()))
1942         {
1943           /* If a timevar is present, start it.  */
1944           if (pass->tv_id)
1945             timevar_push (pass->tv_id);
1946
1947           pass_init_dump_file (pass);
1948
1949           ipa_pass->write_summary (set,vset);
1950
1951           pass_fini_dump_file (pass);
1952
1953           /* If a timevar is present, start it.  */
1954           if (pass->tv_id)
1955             timevar_pop (pass->tv_id);
1956         }
1957
1958       if (pass->sub && pass->sub->type != GIMPLE_PASS)
1959         ipa_write_summaries_2 (pass->sub, set, vset, state);
1960
1961       pass = pass->next;
1962     }
1963 }
1964
1965 /* Helper function of ipa_write_summaries. Creates and destroys the
1966    decl state and calls ipa_write_summaries_2 for all passes that have
1967    summaries.  SET is the set of nodes to be written.  */
1968
1969 static void
1970 ipa_write_summaries_1 (cgraph_node_set set, varpool_node_set vset)
1971 {
1972   struct lto_out_decl_state *state = lto_new_out_decl_state ();
1973   compute_ltrans_boundary (state, set, vset);
1974
1975   lto_push_out_decl_state (state);
1976
1977   gcc_assert (!flag_wpa);
1978   ipa_write_summaries_2 (all_regular_ipa_passes, set, vset, state);
1979   ipa_write_summaries_2 (all_lto_gen_passes, set, vset, state);
1980
1981   gcc_assert (lto_get_out_decl_state () == state);
1982   lto_pop_out_decl_state ();
1983   lto_delete_out_decl_state (state);
1984 }
1985
1986 /* Write out summaries for all the nodes in the callgraph.  */
1987
1988 void
1989 ipa_write_summaries (void)
1990 {
1991   cgraph_node_set set;
1992   varpool_node_set vset;
1993   struct cgraph_node **order;
1994   struct varpool_node *vnode;
1995   int i, order_pos;
1996
1997   if (!flag_generate_lto || seen_error ())
1998     return;
1999
2000   set = cgraph_node_set_new ();
2001
2002   /* Create the callgraph set in the same order used in
2003      cgraph_expand_all_functions.  This mostly facilitates debugging,
2004      since it causes the gimple file to be processed in the same order
2005      as the source code.  */
2006   order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
2007   order_pos = ipa_reverse_postorder (order);
2008   gcc_assert (order_pos == cgraph_n_nodes);
2009
2010   for (i = order_pos - 1; i >= 0; i--)
2011     {
2012       struct cgraph_node *node = order[i];
2013
2014       if (cgraph_function_with_gimple_body_p (node))
2015         {
2016           /* When streaming out references to statements as part of some IPA
2017              pass summary, the statements need to have uids assigned and the
2018              following does that for all the IPA passes here. Naturally, this
2019              ordering then matches the one IPA-passes get in their stmt_fixup
2020              hooks.  */
2021
2022           push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2023           renumber_gimple_stmt_uids ();
2024           pop_cfun ();
2025         }
2026       if (node->analyzed)
2027         cgraph_node_set_add (set, node);
2028     }
2029   vset = varpool_node_set_new ();
2030
2031   for (vnode = varpool_nodes; vnode; vnode = vnode->next)
2032     if (vnode->needed && !vnode->alias)
2033       varpool_node_set_add (vset, vnode);
2034
2035   ipa_write_summaries_1 (set, vset);
2036
2037   free (order);
2038   free_cgraph_node_set (set);
2039   free_varpool_node_set (vset);
2040 }
2041
2042 /* Same as execute_pass_list but assume that subpasses of IPA passes
2043    are local passes. If SET is not NULL, write out optimization summaries of
2044    only those node in SET. */
2045
2046 static void
2047 ipa_write_optimization_summaries_1 (struct opt_pass *pass, cgraph_node_set set,
2048                        varpool_node_set vset,
2049                        struct lto_out_decl_state *state)
2050 {
2051   while (pass)
2052     {
2053       struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
2054       gcc_assert (!current_function_decl);
2055       gcc_assert (!cfun);
2056       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2057       if (pass->type == IPA_PASS
2058           && ipa_pass->write_optimization_summary
2059           && (!pass->gate || pass->gate ()))
2060         {
2061           /* If a timevar is present, start it.  */
2062           if (pass->tv_id)
2063             timevar_push (pass->tv_id);
2064
2065           pass_init_dump_file (pass);
2066
2067           ipa_pass->write_optimization_summary (set, vset);
2068
2069           pass_fini_dump_file (pass);
2070
2071           /* If a timevar is present, start it.  */
2072           if (pass->tv_id)
2073             timevar_pop (pass->tv_id);
2074         }
2075
2076       if (pass->sub && pass->sub->type != GIMPLE_PASS)
2077         ipa_write_optimization_summaries_1 (pass->sub, set, vset, state);
2078
2079       pass = pass->next;
2080     }
2081 }
2082
2083 /* Write all the optimization summaries for the cgraph nodes in SET.  If SET is
2084    NULL, write out all summaries of all nodes. */
2085
2086 void
2087 ipa_write_optimization_summaries (cgraph_node_set set, varpool_node_set vset)
2088 {
2089   struct lto_out_decl_state *state = lto_new_out_decl_state ();
2090   cgraph_node_set_iterator csi;
2091   compute_ltrans_boundary (state, set, vset);
2092
2093   lto_push_out_decl_state (state);
2094   for (csi = csi_start (set); !csi_end_p (csi); csi_next (&csi))
2095     {
2096       struct cgraph_node *node = csi_node (csi);
2097       /* When streaming out references to statements as part of some IPA
2098          pass summary, the statements need to have uids assigned.
2099
2100          For functions newly born at WPA stage we need to initialize
2101          the uids here.  */
2102       if (node->analyzed
2103           && gimple_has_body_p (node->decl))
2104         {
2105           push_cfun (DECL_STRUCT_FUNCTION (node->decl));
2106           renumber_gimple_stmt_uids ();
2107           pop_cfun ();
2108         }
2109     }
2110
2111   gcc_assert (flag_wpa);
2112   ipa_write_optimization_summaries_1 (all_regular_ipa_passes, set, vset, state);
2113   ipa_write_optimization_summaries_1 (all_lto_gen_passes, set, vset, state);
2114
2115   gcc_assert (lto_get_out_decl_state () == state);
2116   lto_pop_out_decl_state ();
2117   lto_delete_out_decl_state (state);
2118 }
2119
2120 /* Same as execute_pass_list but assume that subpasses of IPA passes
2121    are local passes.  */
2122
2123 static void
2124 ipa_read_summaries_1 (struct opt_pass *pass)
2125 {
2126   while (pass)
2127     {
2128       struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2129
2130       gcc_assert (!current_function_decl);
2131       gcc_assert (!cfun);
2132       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2133
2134       if (pass->gate == NULL || pass->gate ())
2135         {
2136           if (pass->type == IPA_PASS && ipa_pass->read_summary)
2137             {
2138               /* If a timevar is present, start it.  */
2139               if (pass->tv_id)
2140                 timevar_push (pass->tv_id);
2141
2142               pass_init_dump_file (pass);
2143
2144               ipa_pass->read_summary ();
2145
2146               pass_fini_dump_file (pass);
2147
2148               /* Stop timevar.  */
2149               if (pass->tv_id)
2150                 timevar_pop (pass->tv_id);
2151             }
2152
2153           if (pass->sub && pass->sub->type != GIMPLE_PASS)
2154             ipa_read_summaries_1 (pass->sub);
2155         }
2156       pass = pass->next;
2157     }
2158 }
2159
2160
2161 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes.  */
2162
2163 void
2164 ipa_read_summaries (void)
2165 {
2166   ipa_read_summaries_1 (all_regular_ipa_passes);
2167   ipa_read_summaries_1 (all_lto_gen_passes);
2168 }
2169
2170 /* Same as execute_pass_list but assume that subpasses of IPA passes
2171    are local passes.  */
2172
2173 static void
2174 ipa_read_optimization_summaries_1 (struct opt_pass *pass)
2175 {
2176   while (pass)
2177     {
2178       struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2179
2180       gcc_assert (!current_function_decl);
2181       gcc_assert (!cfun);
2182       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2183
2184       if (pass->gate == NULL || pass->gate ())
2185         {
2186           if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
2187             {
2188               /* If a timevar is present, start it.  */
2189               if (pass->tv_id)
2190                 timevar_push (pass->tv_id);
2191
2192               pass_init_dump_file (pass);
2193
2194               ipa_pass->read_optimization_summary ();
2195
2196               pass_fini_dump_file (pass);
2197
2198               /* Stop timevar.  */
2199               if (pass->tv_id)
2200                 timevar_pop (pass->tv_id);
2201             }
2202
2203           if (pass->sub && pass->sub->type != GIMPLE_PASS)
2204             ipa_read_optimization_summaries_1 (pass->sub);
2205         }
2206       pass = pass->next;
2207     }
2208 }
2209
2210 /* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes.  */
2211
2212 void
2213 ipa_read_optimization_summaries (void)
2214 {
2215   ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
2216   ipa_read_optimization_summaries_1 (all_lto_gen_passes);
2217 }
2218
2219 /* Same as execute_pass_list but assume that subpasses of IPA passes
2220    are local passes.  */
2221 void
2222 execute_ipa_pass_list (struct opt_pass *pass)
2223 {
2224   do
2225     {
2226       gcc_assert (!current_function_decl);
2227       gcc_assert (!cfun);
2228       gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
2229       if (execute_one_pass (pass) && pass->sub)
2230         {
2231           if (pass->sub->type == GIMPLE_PASS)
2232             {
2233               invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
2234               do_per_function_toporder ((void (*)(void *))execute_pass_list,
2235                                         pass->sub);
2236               invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
2237             }
2238           else if (pass->sub->type == SIMPLE_IPA_PASS
2239                    || pass->sub->type == IPA_PASS)
2240             execute_ipa_pass_list (pass->sub);
2241           else
2242             gcc_unreachable ();
2243         }
2244       gcc_assert (!current_function_decl);
2245       cgraph_process_new_functions ();
2246       pass = pass->next;
2247     }
2248   while (pass);
2249 }
2250
2251 /* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS.  */
2252
2253 static void
2254 execute_ipa_stmt_fixups (struct opt_pass *pass,
2255                           struct cgraph_node *node, gimple *stmts)
2256 {
2257   while (pass)
2258     {
2259       /* Execute all of the IPA_PASSes in the list.  */
2260       if (pass->type == IPA_PASS
2261           && (!pass->gate || pass->gate ()))
2262         {
2263           struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
2264
2265           if (ipa_pass->stmt_fixup)
2266             {
2267               pass_init_dump_file (pass);
2268               /* If a timevar is present, start it.  */
2269               if (pass->tv_id)
2270                 timevar_push (pass->tv_id);
2271
2272               ipa_pass->stmt_fixup (node, stmts);
2273
2274               /* Stop timevar.  */
2275               if (pass->tv_id)
2276                 timevar_pop (pass->tv_id);
2277               pass_fini_dump_file (pass);
2278             }
2279           if (pass->sub)
2280             execute_ipa_stmt_fixups (pass->sub, node, stmts);
2281         }
2282       pass = pass->next;
2283     }
2284 }
2285
2286 /* Execute stmt fixup hooks of all IPA passes for NODE and STMTS.  */
2287
2288 void
2289 execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
2290 {
2291   execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
2292 }
2293
2294
2295 extern void debug_properties (unsigned int);
2296 extern void dump_properties (FILE *, unsigned int);
2297
2298 DEBUG_FUNCTION void
2299 dump_properties (FILE *dump, unsigned int props)
2300 {
2301   fprintf (dump, "Properties:\n");
2302   if (props & PROP_gimple_any)
2303     fprintf (dump, "PROP_gimple_any\n");
2304   if (props & PROP_gimple_lcf)
2305     fprintf (dump, "PROP_gimple_lcf\n");
2306   if (props & PROP_gimple_leh)
2307     fprintf (dump, "PROP_gimple_leh\n");
2308   if (props & PROP_cfg)
2309     fprintf (dump, "PROP_cfg\n");
2310   if (props & PROP_referenced_vars)
2311     fprintf (dump, "PROP_referenced_vars\n");
2312   if (props & PROP_ssa)
2313     fprintf (dump, "PROP_ssa\n");
2314   if (props & PROP_no_crit_edges)
2315     fprintf (dump, "PROP_no_crit_edges\n");
2316   if (props & PROP_rtl)
2317     fprintf (dump, "PROP_rtl\n");
2318   if (props & PROP_gimple_lomp)
2319     fprintf (dump, "PROP_gimple_lomp\n");
2320   if (props & PROP_gimple_lcx)
2321     fprintf (dump, "PROP_gimple_lcx\n");
2322   if (props & PROP_cfglayout)
2323     fprintf (dump, "PROP_cfglayout\n");
2324 }
2325
2326 DEBUG_FUNCTION void
2327 debug_properties (unsigned int props)
2328 {
2329   dump_properties (stderr, props);
2330 }
2331
2332 /* Called by local passes to see if function is called by already processed nodes.
2333    Because we process nodes in topological order, this means that function is
2334    in recursive cycle or we introduced new direct calls.  */
2335 bool
2336 function_called_by_processed_nodes_p (void)
2337 {
2338   struct cgraph_edge *e;
2339   for (e = cgraph_get_node (current_function_decl)->callers;
2340        e;
2341        e = e->next_caller)
2342     {
2343       if (e->caller->decl == current_function_decl)
2344         continue;
2345       if (!cgraph_function_with_gimple_body_p (e->caller))
2346         continue;
2347       if (TREE_ASM_WRITTEN (e->caller->decl))
2348         continue;
2349       if (!e->caller->process && !e->caller->global.inlined_to)
2350         break;
2351     }
2352   if (dump_file && e)
2353     {
2354       fprintf (dump_file, "Already processed call to:\n");
2355       dump_cgraph_node (dump_file, e->caller);
2356     }
2357   return e != NULL;
2358 }
2359
2360 #include "gt-passes.h"