OSDN Git Service

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