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