OSDN Git Service

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