OSDN Git Service

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