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