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