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