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