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