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