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