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