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