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