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