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