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