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