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