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