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