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