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