]> gcc.gnu.org Git - gcc.git/blame - gcc/passes.c
utils.c (init_gnat_to_gnu): Use typed GC allocation.
[gcc.git] / gcc / passes.c
CommitLineData
f6db1481
RH
1/* Top level of GCC compilers (cc1, cc1plus, etc.)
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
c75c517d 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
e53a16e7 4 Free Software Foundation, Inc.
f6db1481
RH
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
f6db1481
RH
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
f6db1481
RH
21
22/* This is the top level of cc1/c++.
23 It parses command args, opens files, invokes the various passes
24 in the proper order, and counts the time used by each.
25 Error messages and low-level interface to malloc also handled here. */
26
27#include "config.h"
f6db1481
RH
28#include "system.h"
29#include "coretypes.h"
30#include "tm.h"
31#include <signal.h>
32
33#ifdef HAVE_SYS_RESOURCE_H
34# include <sys/resource.h>
35#endif
36
37#ifdef HAVE_SYS_TIMES_H
38# include <sys/times.h>
39#endif
40
41#include "line-map.h"
42#include "input.h"
43#include "tree.h"
44#include "rtl.h"
45#include "tm_p.h"
46#include "flags.h"
47#include "insn-attr.h"
48#include "insn-config.h"
49#include "insn-flags.h"
50#include "hard-reg-set.h"
51#include "recog.h"
52#include "output.h"
53#include "except.h"
54#include "function.h"
55#include "toplev.h"
56#include "expr.h"
57#include "basic-block.h"
58#include "intl.h"
59#include "ggc.h"
60#include "graph.h"
f6db1481
RH
61#include "regs.h"
62#include "timevar.h"
1da2ed5f 63#include "diagnostic-core.h"
f6db1481
RH
64#include "params.h"
65#include "reload.h"
66#include "dwarf2asm.h"
67#include "integrate.h"
f6db1481
RH
68#include "debug.h"
69#include "target.h"
70#include "langhooks.h"
71#include "cfglayout.h"
72#include "cfgloop.h"
73#include "hosthooks.h"
74#include "cgraph.h"
75#include "opts.h"
76#include "coverage.h"
77#include "value-prof.h"
ef330312
PB
78#include "tree-inline.h"
79#include "tree-flow.h"
2f8e398b 80#include "tree-pass.h"
9f8628ba 81#include "tree-dump.h"
6fb5fa3c 82#include "df.h"
45a80bb9 83#include "predict.h"
d7f09764 84#include "lto-streamer.h"
090fa0ab 85#include "plugin.h"
f6db1481
RH
86
87#if defined (DWARF2_UNWIND_INFO) || defined (DWARF2_DEBUGGING_INFO)
88#include "dwarf2out.h"
89#endif
90
97b0ade3 91#if defined (DBX_DEBUGGING_INFO) || defined (XCOFF_DEBUGGING_INFO)
f6db1481
RH
92#include "dbxout.h"
93#endif
94
95#ifdef SDB_DEBUGGING_INFO
96#include "sdbout.h"
97#endif
98
99#ifdef XCOFF_DEBUGGING_INFO
100#include "xcoffout.h" /* Needed for external data
101 declarations for e.g. AIX 4.x. */
102#endif
103
6fb5fa3c 104/* This is used for debugging. It allows the current pass to printed
090fa0ab
GF
105 from anywhere in compilation.
106 The variable current_pass is also used for statistics and plugins. */
8ddbbcae 107struct opt_pass *current_pass;
6fb5fa3c
DB
108
109/* Call from anywhere to find out what pass this is. Useful for
110 printing out debugging information deep inside an service
111 routine. */
112void
113print_current_pass (FILE *file)
114{
115 if (current_pass)
b8698a0f 116 fprintf (file, "current pass = %s (%d)\n",
6fb5fa3c
DB
117 current_pass->name, current_pass->static_pass_number);
118 else
119 fprintf (file, "no current pass.\n");
120}
121
122
123/* Call from the debugger to get the current pass name. */
24e47c76 124DEBUG_FUNCTION void
6fb5fa3c
DB
125debug_pass (void)
126{
127 print_current_pass (stderr);
b8698a0f 128}
6fb5fa3c
DB
129
130
131
ef330312
PB
132/* Global variables used to communicate with passes. */
133int dump_flags;
134bool in_gimple_form;
b02b9b53 135bool first_pass_instance;
f6db1481 136
f6db1481
RH
137
138/* This is called from various places for FUNCTION_DECL, VAR_DECL,
139 and TYPE_DECL nodes.
140
141 This does nothing for local (non-static) variables, unless the
0e6df31e
GK
142 variable is a register variable with DECL_ASSEMBLER_NAME set. In
143 that case, or if the variable is not an automatic, it sets up the
144 RTL and outputs any assembler code (label definition, storage
145 allocation and initialization).
f6db1481 146
0e6df31e 147 DECL is the declaration. TOP_LEVEL is nonzero
f6db1481
RH
148 if this declaration is not within a function. */
149
150void
151rest_of_decl_compilation (tree decl,
f6db1481
RH
152 int top_level,
153 int at_end)
154{
155 /* We deferred calling assemble_alias so that we could collect
156 other attributes such as visibility. Emit the alias now. */
157 {
158 tree alias;
159 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
160 if (alias)
161 {
162 alias = TREE_VALUE (TREE_VALUE (alias));
163 alias = get_identifier (TREE_STRING_POINTER (alias));
164 assemble_alias (decl, alias);
165 }
166 }
167
0e6df31e
GK
168 /* Can't defer this, because it needs to happen before any
169 later function definitions are processed. */
820cc88f 170 if (DECL_ASSEMBLER_NAME_SET_P (decl) && DECL_REGISTER (decl))
0e6df31e
GK
171 make_decl_rtl (decl);
172
f6db1481
RH
173 /* Forward declarations for nested functions are not "external",
174 but we need to treat them as if they were. */
175 if (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
176 || TREE_CODE (decl) == FUNCTION_DECL)
177 {
178 timevar_push (TV_VARCONST);
179
f6db1481
RH
180 /* Don't output anything when a tentative file-scope definition
181 is seen. But at end of compilation, do output code for them.
182
7e8b322a 183 We do output all variables and rely on
f6db1481
RH
184 callgraph code to defer them except for forward declarations
185 (see gcc.c-torture/compile/920624-1.c) */
186 if ((at_end
187 || !DECL_DEFER_OUTPUT (decl)
cd9c7bd2 188 || DECL_INITIAL (decl))
f6db1481
RH
189 && !DECL_EXTERNAL (decl))
190 {
2942c502
JH
191 /* When reading LTO unit, we also read varpool, so do not
192 rebuild it. */
193 if (in_lto_p && !at_end)
194 ;
195 else if (TREE_CODE (decl) != FUNCTION_DECL)
8a4a83ed 196 varpool_finalize_decl (decl);
f6db1481
RH
197 else
198 assemble_variable (decl, top_level, at_end, 0);
199 }
200
201#ifdef ASM_FINISH_DECLARE_OBJECT
202 if (decl == last_assemble_variable_decl)
203 {
204 ASM_FINISH_DECLARE_OBJECT (asm_out_file, decl,
205 top_level, at_end);
206 }
207#endif
208
209 timevar_pop (TV_VARCONST);
210 }
ef11c839
SB
211 else if (TREE_CODE (decl) == TYPE_DECL
212 /* Like in rest_of_type_compilation, avoid confusing the debug
213 information machinery when there are errors. */
1da2ed5f 214 && !seen_error ())
f6db1481
RH
215 {
216 timevar_push (TV_SYMOUT);
217 debug_hooks->type_decl (decl, !top_level);
218 timevar_pop (TV_SYMOUT);
219 }
e4d5432a 220
aabcd309 221 /* Let cgraph know about the existence of variables. */
2942c502
JH
222 if (in_lto_p && !at_end)
223 ;
224 else if (TREE_CODE (decl) == VAR_DECL && !DECL_EXTERNAL (decl))
8a4a83ed 225 varpool_node (decl);
f6db1481
RH
226}
227
228/* Called after finishing a record, union or enumeral type. */
229
230void
231rest_of_type_compilation (tree type, int toplev)
232{
233 /* Avoid confusing the debug information machinery when there are
234 errors. */
1da2ed5f 235 if (seen_error ())
f6db1481
RH
236 return;
237
238 timevar_push (TV_SYMOUT);
239 debug_hooks->type_decl (TYPE_STUB_DECL (type), !toplev);
240 timevar_pop (TV_SYMOUT);
241}
242
ef330312 243\f
f6db1481 244
ef330312
PB
245void
246finish_optimization_passes (void)
f6db1481 247{
09639a83 248 int i;
ef330312
PB
249 struct dump_file_info *dfi;
250 char *name;
f6db1481 251
ef330312
PB
252 timevar_push (TV_DUMP);
253 if (profile_arc_flag || flag_test_coverage || flag_branch_probabilities)
f6db1481 254 {
8ddbbcae 255 dump_file = dump_begin (pass_profile.pass.static_pass_number, NULL);
ef330312
PB
256 end_branch_prob ();
257 if (dump_file)
8ddbbcae 258 dump_end (pass_profile.pass.static_pass_number, dump_file);
f6db1481 259 }
f6db1481 260
ef330312 261 if (optimize > 0)
f6db1481 262 {
8ddbbcae 263 dump_file = dump_begin (pass_combine.pass.static_pass_number, NULL);
ef330312 264 if (dump_file)
f6db1481 265 {
ef330312 266 dump_combine_total_stats (dump_file);
8ddbbcae 267 dump_end (pass_combine.pass.static_pass_number, dump_file);
f6db1481
RH
268 }
269 }
270
ef330312
PB
271 /* Do whatever is necessary to finish printing the graphs. */
272 if (graph_dump_format != no_graph)
273 for (i = TDI_end; (dfi = get_dump_file_info (i)) != NULL; ++i)
274 if (dump_initialized_p (i)
5e34206b 275 && (dfi->flags & TDF_GRAPH) != 0
ef330312 276 && (name = get_dump_file_name (i)) != NULL)
5e34206b
JJ
277 {
278 finish_graph_dump_file (name);
279 free (name);
280 }
f6db1481 281
ef330312 282 timevar_pop (TV_DUMP);
f6db1481 283}
f6db1481 284
ef330312
PB
285static bool
286gate_rest_of_compilation (void)
f6db1481 287{
ef330312
PB
288 /* Early return if there were errors. We can run afoul of our
289 consistency checks, and there's not really much point in fixing them. */
1da2ed5f 290 return !(rtl_dump_and_exit || flag_syntax_only || seen_error ());
f6db1481
RH
291}
292
8ddbbcae 293struct gimple_opt_pass pass_rest_of_compilation =
f6db1481 294{
8ddbbcae
JH
295 {
296 GIMPLE_PASS,
cf400ddb 297 "*rest_of_compilation", /* name */
ef330312
PB
298 gate_rest_of_compilation, /* gate */
299 NULL, /* execute */
300 NULL, /* sub */
301 NULL, /* next */
302 0, /* static_pass_number */
303 TV_REST_OF_COMPILATION, /* tv_id */
304 PROP_rtl, /* properties_required */
305 0, /* properties_provided */
306 0, /* properties_destroyed */
307 0, /* todo_flags_start */
8ddbbcae
JH
308 TODO_ggc_collect /* todo_flags_finish */
309 }
ef330312 310};
f6db1481 311
ef330312
PB
312static bool
313gate_postreload (void)
314{
315 return reload_completed;
f6db1481
RH
316}
317
8ddbbcae 318struct rtl_opt_pass pass_postreload =
f6db1481 319{
8ddbbcae
JH
320 {
321 RTL_PASS,
e0a42b0f 322 "*all-postreload", /* name */
ef330312
PB
323 gate_postreload, /* gate */
324 NULL, /* execute */
325 NULL, /* sub */
326 NULL, /* next */
327 0, /* static_pass_number */
7072a650 328 TV_NONE, /* tv_id */
ef330312
PB
329 PROP_rtl, /* properties_required */
330 0, /* properties_provided */
331 0, /* properties_destroyed */
332 0, /* todo_flags_start */
8ddbbcae
JH
333 TODO_ggc_collect | TODO_verify_rtl_sharing /* todo_flags_finish */
334 }
ef330312 335};
f6db1481 336
97b0ade3 337
f6db1481 338
ef330312 339/* The root of the compilation pass tree, once constructed. */
d7f09764
DN
340struct opt_pass *all_passes, *all_small_ipa_passes, *all_lowering_passes,
341 *all_regular_ipa_passes, *all_lto_gen_passes;
f6db1481 342
2a5e9d16
JR
343/* This is used by plugins, and should also be used in register_pass. */
344#define DEF_PASS_LIST(LIST) &LIST,
345struct opt_pass **gcc_pass_lists[] = { GCC_PASS_LISTS NULL };
346#undef DEF_PASS_LIST
347
9fe0cb7d
RG
348/* A map from static pass id to optimization pass. */
349struct opt_pass **passes_by_id;
350int passes_by_id_size;
351
352/* Set the static pass number of pass PASS to ID and record that
353 in the mapping from static pass number to pass. */
354
355static void
356set_pass_for_id (int id, struct opt_pass *pass)
357{
358 pass->static_pass_number = id;
359 if (passes_by_id_size <= id)
360 {
d3bfe4de 361 passes_by_id = XRESIZEVEC (struct opt_pass *, passes_by_id, id + 1);
9fe0cb7d
RG
362 memset (passes_by_id + passes_by_id_size, 0,
363 (id + 1 - passes_by_id_size) * sizeof (void *));
364 passes_by_id_size = id + 1;
365 }
366 passes_by_id[id] = pass;
367}
368
369/* Return the pass with the static pass number ID. */
370
371struct opt_pass *
372get_pass_for_id (int id)
373{
374 if (id >= passes_by_id_size)
375 return NULL;
376 return passes_by_id[id];
377}
378
ef330312
PB
379/* Iterate over the pass tree allocating dump file numbers. We want
380 to do this depth first, and independent of whether the pass is
381 enabled or not. */
f6db1481 382
68a607d8 383void
9e016eba 384register_one_dump_file (struct opt_pass *pass)
ef330312
PB
385{
386 char *dot_name, *flag_name, *glob_name;
e0a42b0f 387 const char *name, *prefix;
ef330312 388 char num[10];
9fe0cb7d 389 int flags, id;
f6db1481 390
ef330312
PB
391 /* See below in next_pass_1. */
392 num[0] = '\0';
393 if (pass->static_pass_number != -1)
394 sprintf (num, "%d", ((int) pass->static_pass_number < 0
395 ? 1 : pass->static_pass_number));
f6db1481 396
e0a42b0f
ZC
397 /* The name is both used to identify the pass for the purposes of plugins,
398 and to specify dump file name and option.
399 The latter two might want something short which is not quite unique; for
400 that reason, we may have a disambiguating prefix, followed by a space
401 to mark the start of the following dump file name / option string. */
402 name = strchr (pass->name, ' ');
403 name = name ? name + 1 : pass->name;
404 dot_name = concat (".", name, num, NULL);
17653c00 405 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
bbbe4e7b 406 prefix = "ipa-", flags = TDF_IPA;
9e016eba 407 else if (pass->type == GIMPLE_PASS)
bbbe4e7b 408 prefix = "tree-", flags = TDF_TREE;
f6db1481 409 else
bbbe4e7b
PB
410 prefix = "rtl-", flags = TDF_RTL;
411
e0a42b0f
ZC
412 flag_name = concat (prefix, name, num, NULL);
413 glob_name = concat (prefix, name, NULL);
9fe0cb7d
RG
414 id = dump_register (dot_name, flag_name, glob_name, flags);
415 set_pass_for_id (id, pass);
97b0ade3
PB
416}
417
bbbe4e7b
PB
418/* Recursive worker function for register_dump_files. */
419
b8698a0f 420static int
9e016eba 421register_dump_files_1 (struct opt_pass *pass, int properties)
97b0ade3 422{
ef330312
PB
423 do
424 {
bbbe4e7b
PB
425 int new_properties = (properties | pass->properties_provided)
426 & ~pass->properties_destroyed;
f6db1481 427
8e352cd3 428 if (pass->name && pass->name[0] != '*')
9e016eba 429 register_one_dump_file (pass);
f6db1481 430
ef330312 431 if (pass->sub)
9e016eba 432 new_properties = register_dump_files_1 (pass->sub, new_properties);
f6db1481 433
ef330312
PB
434 /* If we have a gate, combine the properties that we could have with
435 and without the pass being examined. */
436 if (pass->gate)
437 properties &= new_properties;
438 else
439 properties = new_properties;
f6db1481 440
ef330312 441 pass = pass->next;
f6db1481 442 }
ef330312 443 while (pass);
f6db1481 444
ef330312 445 return properties;
f6db1481 446}
f9957958 447
b8698a0f 448/* Register the dump files for the pipeline starting at PASS.
9e016eba
JH
449 PROPERTIES reflects the properties that are guaranteed to be available at
450 the beginning of the pipeline. */
bbbe4e7b 451
b8698a0f 452static void
9e016eba 453register_dump_files (struct opt_pass *pass,int properties)
bbbe4e7b
PB
454{
455 pass->properties_required |= properties;
9e016eba 456 register_dump_files_1 (pass, properties);
bbbe4e7b
PB
457}
458
b80b0fd9
ST
459/* Look at the static_pass_number and duplicate the pass
460 if it is already added to a list. */
f9957958 461
b80b0fd9
ST
462static struct opt_pass *
463make_pass_instance (struct opt_pass *pass, bool track_duplicates)
f6db1481 464{
ef330312
PB
465 /* A nonzero static_pass_number indicates that the
466 pass is already in the list. */
467 if (pass->static_pass_number)
468 {
82d6e6fc 469 struct opt_pass *new_pass;
f6db1481 470
63a00e0d
DS
471 if (pass->type == GIMPLE_PASS
472 || pass->type == RTL_PASS
473 || pass->type == SIMPLE_IPA_PASS)
474 {
475 new_pass = XNEW (struct opt_pass);
476 memcpy (new_pass, pass, sizeof (struct opt_pass));
477 }
478 else if (pass->type == IPA_PASS)
479 {
480 new_pass = (struct opt_pass *)XNEW (struct ipa_opt_pass_d);
481 memcpy (new_pass, pass, sizeof (struct ipa_opt_pass_d));
482 }
483 else
484 gcc_unreachable ();
485
82d6e6fc 486 new_pass->next = NULL;
f6db1481 487
82d6e6fc 488 new_pass->todo_flags_start &= ~TODO_mark_first_instance;
b02b9b53 489
ef330312
PB
490 /* Indicate to register_dump_files that this pass has duplicates,
491 and so it should rename the dump file. The first instance will
492 be -1, and be number of duplicates = -static_pass_number - 1.
493 Subsequent instances will be > 0 and just the duplicate number. */
e0a42b0f 494 if ((pass->name && pass->name[0] != '*') || track_duplicates)
ef330312
PB
495 {
496 pass->static_pass_number -= 1;
82d6e6fc 497 new_pass->static_pass_number = -pass->static_pass_number;
ef330312 498 }
b80b0fd9 499 return new_pass;
ef330312
PB
500 }
501 else
502 {
b02b9b53 503 pass->todo_flags_start |= TODO_mark_first_instance;
ef330312 504 pass->static_pass_number = -1;
090fa0ab
GF
505
506 invoke_plugin_callbacks (PLUGIN_NEW_PASS, pass);
b8698a0f
L
507 }
508 return pass;
b80b0fd9
ST
509}
510
511/* Add a pass to the pass list. Duplicate the pass if it's already
512 in the list. */
513
514static struct opt_pass **
515next_pass_1 (struct opt_pass **list, struct opt_pass *pass)
516{
e0a42b0f
ZC
517 /* Every pass should have a name so that plugins can refer to them. */
518 gcc_assert (pass->name != NULL);
519
b80b0fd9 520 *list = make_pass_instance (pass, false);
b8698a0f 521
ef330312 522 return &(*list)->next;
f6db1481
RH
523}
524
b80b0fd9
ST
525/* List node for an inserted pass instance. We need to keep track of all
526 the newly-added pass instances (with 'added_pass_nodes' defined below)
527 so that we can register their dump files after pass-positioning is finished.
528 Registering dumping files needs to be post-processed or the
529 static_pass_number of the opt_pass object would be modified and mess up
530 the dump file names of future pass instances to be added. */
531
532struct pass_list_node
533{
534 struct opt_pass *pass;
535 struct pass_list_node *next;
536};
537
538static struct pass_list_node *added_pass_nodes = NULL;
539static struct pass_list_node *prev_added_pass_node;
540
b8698a0f 541/* Insert the pass at the proper position. Return true if the pass
b80b0fd9
ST
542 is successfully added.
543
544 NEW_PASS_INFO - new pass to be inserted
545 PASS_LIST - root of the pass list to insert the new pass to */
546
547static bool
548position_pass (struct register_pass_info *new_pass_info,
549 struct opt_pass **pass_list)
550{
551 struct opt_pass *pass = *pass_list, *prev_pass = NULL;
552 bool success = false;
553
554 for ( ; pass; prev_pass = pass, pass = pass->next)
555 {
556 /* Check if the current pass is of the same type as the new pass and
557 matches the name and the instance number of the reference pass. */
558 if (pass->type == new_pass_info->pass->type
559 && pass->name
560 && !strcmp (pass->name, new_pass_info->reference_pass_name)
561 && ((new_pass_info->ref_pass_instance_number == 0)
562 || (new_pass_info->ref_pass_instance_number ==
563 pass->static_pass_number)
564 || (new_pass_info->ref_pass_instance_number == 1
565 && pass->todo_flags_start & TODO_mark_first_instance)))
566 {
567 struct opt_pass *new_pass;
568 struct pass_list_node *new_pass_node;
569
570 new_pass = make_pass_instance (new_pass_info->pass, true);
b8698a0f 571
b80b0fd9
ST
572 /* Insert the new pass instance based on the positioning op. */
573 switch (new_pass_info->pos_op)
574 {
575 case PASS_POS_INSERT_AFTER:
576 new_pass->next = pass->next;
577 pass->next = new_pass;
578
579 /* Skip newly inserted pass to avoid repeated
580 insertions in the case where the new pass and the
581 existing one have the same name. */
b8698a0f 582 pass = new_pass;
b80b0fd9
ST
583 break;
584 case PASS_POS_INSERT_BEFORE:
585 new_pass->next = pass;
586 if (prev_pass)
587 prev_pass->next = new_pass;
588 else
589 *pass_list = new_pass;
590 break;
591 case PASS_POS_REPLACE:
592 new_pass->next = pass->next;
593 if (prev_pass)
594 prev_pass->next = new_pass;
595 else
596 *pass_list = new_pass;
597 new_pass->sub = pass->sub;
598 new_pass->tv_id = pass->tv_id;
599 pass = new_pass;
600 break;
601 default:
602 error ("Invalid pass positioning operation");
603 return false;
604 }
605
606 /* Save the newly added pass (instance) in the added_pass_nodes
607 list so that we can register its dump file later. Note that
608 we cannot register the dump file now because doing so will modify
609 the static_pass_number of the opt_pass object and therefore
610 mess up the dump file name of future instances. */
611 new_pass_node = XCNEW (struct pass_list_node);
612 new_pass_node->pass = new_pass;
613 if (!added_pass_nodes)
614 added_pass_nodes = new_pass_node;
615 else
616 prev_added_pass_node->next = new_pass_node;
617 prev_added_pass_node = new_pass_node;
618
619 success = true;
620 }
621
622 if (pass->sub && position_pass (new_pass_info, &pass->sub))
623 success = true;
624 }
625
626 return success;
627}
628
629/* Hooks a new pass into the pass lists.
630
631 PASS_INFO - pass information that specifies the opt_pass object,
632 reference pass, instance number, and how to position
633 the pass */
634
635void
636register_pass (struct register_pass_info *pass_info)
637{
669887fc
DS
638 bool all_instances, success;
639
b9e467a2
BS
640 /* The checks below could fail in buggy plugins. Existing GCC
641 passes should never fail these checks, so we mention plugin in
642 the messages. */
b80b0fd9 643 if (!pass_info->pass)
b9e467a2
BS
644 fatal_error ("plugin cannot register a missing pass");
645
646 if (!pass_info->pass->name)
647 fatal_error ("plugin cannot register an unnamed pass");
b80b0fd9
ST
648
649 if (!pass_info->reference_pass_name)
b9e467a2
BS
650 fatal_error
651 ("plugin cannot register pass %qs without reference pass name",
652 pass_info->pass->name);
b80b0fd9 653
b9e467a2 654 /* Try to insert the new pass to the pass lists. We need to check
669887fc 655 all five lists as the reference pass could be in one (or all) of
b9e467a2 656 them. */
669887fc
DS
657 all_instances = pass_info->ref_pass_instance_number == 0;
658 success = position_pass (pass_info, &all_lowering_passes);
659 if (!success || all_instances)
660 success |= position_pass (pass_info, &all_small_ipa_passes);
661 if (!success || all_instances)
662 success |= position_pass (pass_info, &all_regular_ipa_passes);
663 if (!success || all_instances)
664 success |= position_pass (pass_info, &all_lto_gen_passes);
665 if (!success || all_instances)
666 success |= position_pass (pass_info, &all_passes);
667 if (!success)
b9e467a2
BS
668 fatal_error
669 ("pass %qs not found but is referenced by new pass %qs",
670 pass_info->reference_pass_name, pass_info->pass->name);
669887fc
DS
671
672 /* OK, we have successfully inserted the new pass. We need to register
673 the dump files for the newly added pass and its duplicates (if any).
674 Because the registration of plugin/backend passes happens after the
675 command-line options are parsed, the options that specify single
676 pass dumping (e.g. -fdump-tree-PASSNAME) cannot be used for new
677 passes. Therefore we currently can only enable dumping of
678 new passes when the 'dump-all' flags (e.g. -fdump-tree-all)
679 are specified. While doing so, we also delete the pass_list_node
680 objects created during pass positioning. */
681 while (added_pass_nodes)
b80b0fd9 682 {
669887fc
DS
683 struct pass_list_node *next_node = added_pass_nodes->next;
684 enum tree_dump_index tdi;
685 register_one_dump_file (added_pass_nodes->pass);
686 if (added_pass_nodes->pass->type == SIMPLE_IPA_PASS
687 || added_pass_nodes->pass->type == IPA_PASS)
688 tdi = TDI_ipa_all;
689 else if (added_pass_nodes->pass->type == GIMPLE_PASS)
690 tdi = TDI_tree_all;
691 else
692 tdi = TDI_rtl_all;
693 /* Check if dump-all flag is specified. */
694 if (get_dump_file_info (tdi)->state)
695 get_dump_file_info (added_pass_nodes->pass->static_pass_number)
696 ->state = get_dump_file_info (tdi)->state;
697 XDELETE (added_pass_nodes);
698 added_pass_nodes = next_node;
b80b0fd9
ST
699 }
700}
6fb5fa3c 701
dd97d271
DN
702/* Construct the pass tree. The sequencing of passes is driven by
703 the cgraph routines:
704
705 cgraph_finalize_compilation_unit ()
706 for each node N in the cgraph
707 cgraph_analyze_function (N)
708 cgraph_lower_function (N) -> all_lowering_passes
709
710 If we are optimizing, cgraph_optimize is then invoked:
711
712 cgraph_optimize ()
d7f09764 713 ipa_passes () -> all_small_ipa_passes
dd97d271
DN
714 cgraph_expand_all_functions ()
715 for each node N in the cgraph
716 cgraph_expand_function (N)
e89d6010 717 tree_rest_of_compilation (DECL (N)) -> all_passes
dd97d271 718*/
97b0ade3 719
ef330312
PB
720void
721init_optimization_passes (void)
722{
8ddbbcae 723 struct opt_pass **p;
ef330312 724
8ddbbcae 725#define NEXT_PASS(PASS) (p = next_pass_1 (p, &((PASS).pass)))
873aa8f5 726
f1bd2543
JH
727 /* All passes needed to lower the function into shape optimizers can
728 operate on. These passes are always run first on the function, but
729 backend might produce already lowered functions that are not processed
730 by these passes. */
ef330312 731 p = &all_lowering_passes;
a406865a
RG
732 NEXT_PASS (pass_warn_unused_result);
733 NEXT_PASS (pass_diagnose_omp_blocks);
ef330312 734 NEXT_PASS (pass_mudflap_1);
953ff289 735 NEXT_PASS (pass_lower_omp);
ef330312 736 NEXT_PASS (pass_lower_cf);
a24549d4 737 NEXT_PASS (pass_refactor_eh);
ef330312
PB
738 NEXT_PASS (pass_lower_eh);
739 NEXT_PASS (pass_build_cfg);
ef330312
PB
740 NEXT_PASS (pass_lower_vector);
741 NEXT_PASS (pass_warn_function_return);
2dee695b 742 NEXT_PASS (pass_build_cgraph_edges);
873aa8f5 743 NEXT_PASS (pass_inline_parameters);
ef330312
PB
744 *p = NULL;
745
7e8b322a 746 /* Interprocedural optimization passes. */
d7f09764 747 p = &all_small_ipa_passes;
b6050cb7 748 NEXT_PASS (pass_ipa_free_lang_data);
f1bd2543
JH
749 NEXT_PASS (pass_ipa_function_and_variable_visibility);
750 NEXT_PASS (pass_ipa_early_inline);
751 {
8ddbbcae 752 struct opt_pass **p = &pass_ipa_early_inline.pass.sub;
f1bd2543
JH
753 NEXT_PASS (pass_early_inline);
754 NEXT_PASS (pass_inline_parameters);
755 NEXT_PASS (pass_rebuild_cgraph_edges);
756 }
757 NEXT_PASS (pass_early_local_passes);
758 {
8ddbbcae 759 struct opt_pass **p = &pass_early_local_passes.pass.sub;
6f1873a1 760 NEXT_PASS (pass_fixup_cfg);
f1bd2543
JH
761 NEXT_PASS (pass_tree_profile);
762 NEXT_PASS (pass_cleanup_cfg);
763 NEXT_PASS (pass_init_datastructures);
764 NEXT_PASS (pass_expand_omp);
c72321c9
JH
765
766 NEXT_PASS (pass_referenced_vars);
c72321c9 767 NEXT_PASS (pass_build_ssa);
2dc74010 768 NEXT_PASS (pass_early_warn_uninitialized);
d7f09764
DN
769 /* Note that it is not strictly necessary to schedule an early
770 inline pass here. However, some test cases (e.g.,
771 g++.dg/other/p334435.C g++.dg/other/i386-1.C) expect extern
772 inline functions to be inlined even at -O0. This does not
773 happen during the first early inline pass. */
774 NEXT_PASS (pass_rebuild_cgraph_edges);
775 NEXT_PASS (pass_early_inline);
f1bd2543
JH
776 NEXT_PASS (pass_all_early_optimizations);
777 {
8ddbbcae 778 struct opt_pass **p = &pass_all_early_optimizations.pass.sub;
133f9369 779 NEXT_PASS (pass_remove_cgraph_callee_edges);
f1bd2543
JH
780 NEXT_PASS (pass_rename_ssa_copies);
781 NEXT_PASS (pass_ccp);
782 NEXT_PASS (pass_forwprop);
6b8ed145
RG
783 /* pass_build_ealias is a dummy pass that ensures that we
784 execute TODO_rebuild_alias at this point. Re-building
785 alias information also rewrites no longer addressed
786 locals into SSA form if possible. */
787 NEXT_PASS (pass_build_ealias);
029f45bd 788 NEXT_PASS (pass_sra_early);
f1bd2543
JH
789 NEXT_PASS (pass_copy_prop);
790 NEXT_PASS (pass_merge_phi);
11b08ee9 791 NEXT_PASS (pass_cd_dce);
07ffa034 792 NEXT_PASS (pass_early_ipa_sra);
f1bd2543 793 NEXT_PASS (pass_tail_recursion);
b6e99746 794 NEXT_PASS (pass_convert_switch);
a8da523f 795 NEXT_PASS (pass_cleanup_eh);
45a80bb9 796 NEXT_PASS (pass_profile);
33977f81 797 NEXT_PASS (pass_local_pure_const);
f1bd2543 798 }
c72321c9 799 NEXT_PASS (pass_release_ssa_names);
f1bd2543 800 NEXT_PASS (pass_rebuild_cgraph_edges);
b91bc349 801 NEXT_PASS (pass_inline_parameters);
f1bd2543
JH
802 }
803 NEXT_PASS (pass_ipa_increase_alignment);
43d861a5 804 NEXT_PASS (pass_ipa_matrix_reorg);
d7f09764
DN
805 *p = NULL;
806
807 p = &all_regular_ipa_passes;
b20996ff 808 NEXT_PASS (pass_ipa_whole_program_visibility);
e65bb9be 809 NEXT_PASS (pass_ipa_profile);
f1bd2543
JH
810 NEXT_PASS (pass_ipa_cp);
811 NEXT_PASS (pass_ipa_inline);
b8698a0f 812 NEXT_PASS (pass_ipa_pure_const);
514f01ad 813 NEXT_PASS (pass_ipa_reference);
f1bd2543
JH
814 NEXT_PASS (pass_ipa_type_escape);
815 NEXT_PASS (pass_ipa_pta);
d7f09764
DN
816 NEXT_PASS (pass_ipa_struct_reorg);
817 *p = NULL;
818
819 p = &all_lto_gen_passes;
820 NEXT_PASS (pass_ipa_lto_gimple_out);
d7f09764 821 NEXT_PASS (pass_ipa_lto_finish_out); /* This must be the last LTO pass. */
7a388ee4
JH
822 *p = NULL;
823
b3f7d793
DN
824 /* These passes are run after IPA passes on every function that is being
825 output to the assembler file. */
ef330312 826 p = &all_passes;
1d65f45c 827 NEXT_PASS (pass_lower_eh_dispatch);
ef330312 828 NEXT_PASS (pass_all_optimizations);
f1bd2543 829 {
8ddbbcae 830 struct opt_pass **p = &pass_all_optimizations.pass.sub;
133f9369 831 NEXT_PASS (pass_remove_cgraph_callee_edges);
11b08ee9
RG
832 /* Initial scalar cleanups before alias computation.
833 They ensure memory accesses are not indirect wherever possible. */
7299cb99 834 NEXT_PASS (pass_strip_predict_hints);
11b08ee9 835 NEXT_PASS (pass_update_address_taken);
f1bd2543 836 NEXT_PASS (pass_rename_ssa_copies);
d6e840ee 837 NEXT_PASS (pass_complete_unrolli);
f1bd2543 838 NEXT_PASS (pass_ccp);
e2081a1d 839 NEXT_PASS (pass_forwprop);
11b08ee9
RG
840 NEXT_PASS (pass_call_cdce);
841 /* pass_build_alias is a dummy pass that ensures that we
842 execute TODO_rebuild_alias at this point. Re-building
843 alias information also rewrites no longer addressed
844 locals into SSA form if possible. */
845 NEXT_PASS (pass_build_alias);
846 NEXT_PASS (pass_return_slot);
3b48ccbc 847 NEXT_PASS (pass_phiprop);
f1bd2543 848 NEXT_PASS (pass_fre);
f1bd2543
JH
849 NEXT_PASS (pass_copy_prop);
850 NEXT_PASS (pass_merge_phi);
851 NEXT_PASS (pass_vrp);
852 NEXT_PASS (pass_dce);
a5828d1e 853 NEXT_PASS (pass_cselim);
18d08014 854 NEXT_PASS (pass_tree_ifcombine);
f1bd2543 855 NEXT_PASS (pass_phiopt);
f1bd2543 856 NEXT_PASS (pass_tail_recursion);
f1bd2543
JH
857 NEXT_PASS (pass_ch);
858 NEXT_PASS (pass_stdarg);
859 NEXT_PASS (pass_lower_complex);
860 NEXT_PASS (pass_sra);
f1bd2543 861 NEXT_PASS (pass_rename_ssa_copies);
44e10129
MM
862 /* The dom pass will also resolve all __builtin_constant_p calls
863 that are still there to 0. This has to be done after some
864 propagations have already run, but before some more dead code
865 is removed, and this place fits nicely. Remember this when
866 trying to move or duplicate pass_dominator somewhere earlier. */
f1bd2543 867 NEXT_PASS (pass_dominator);
f1bd2543
JH
868 /* The only const/copy propagation opportunities left after
869 DOM should be due to degenerate PHI nodes. So rather than
870 run the full propagators, run a specialized pass which
871 only examines PHIs to discover const/copy propagation
872 opportunities. */
873 NEXT_PASS (pass_phi_only_cprop);
25c6036a 874 NEXT_PASS (pass_dse);
f1bd2543
JH
875 NEXT_PASS (pass_reassoc);
876 NEXT_PASS (pass_dce);
f1bd2543
JH
877 NEXT_PASS (pass_forwprop);
878 NEXT_PASS (pass_phiopt);
879 NEXT_PASS (pass_object_sizes);
a4b8a65f 880 NEXT_PASS (pass_ccp);
324d2217 881 NEXT_PASS (pass_copy_prop);
f1bd2543 882 NEXT_PASS (pass_cse_sincos);
03bd2f1a 883 NEXT_PASS (pass_optimize_bswap);
f1bd2543
JH
884 NEXT_PASS (pass_split_crit_edges);
885 NEXT_PASS (pass_pre);
f1bd2543
JH
886 NEXT_PASS (pass_sink_code);
887 NEXT_PASS (pass_tree_loop);
888 {
8ddbbcae 889 struct opt_pass **p = &pass_tree_loop.pass.sub;
f1bd2543 890 NEXT_PASS (pass_tree_loop_init);
c86a3947 891 NEXT_PASS (pass_lim);
f1bd2543 892 NEXT_PASS (pass_copy_prop);
bbc8a8dc 893 NEXT_PASS (pass_dce_loop);
f1bd2543
JH
894 NEXT_PASS (pass_tree_unswitch);
895 NEXT_PASS (pass_scev_cprop);
f1bd2543 896 NEXT_PASS (pass_record_bounds);
3d8864c0 897 NEXT_PASS (pass_check_data_deps);
dea61d92 898 NEXT_PASS (pass_loop_distribution);
f1bd2543 899 NEXT_PASS (pass_linear_transform);
f8bf9252 900 NEXT_PASS (pass_graphite_transforms);
204b560f
SP
901 {
902 struct opt_pass **p = &pass_graphite_transforms.pass.sub;
caaf41d0 903 NEXT_PASS (pass_copy_prop);
204b560f
SP
904 NEXT_PASS (pass_dce_loop);
905 NEXT_PASS (pass_lim);
906 }
f1bd2543
JH
907 NEXT_PASS (pass_iv_canon);
908 NEXT_PASS (pass_if_conversion);
909 NEXT_PASS (pass_vectorize);
910 {
8ddbbcae 911 struct opt_pass **p = &pass_vectorize.pass.sub;
f1bd2543
JH
912 NEXT_PASS (pass_lower_vector_ssa);
913 NEXT_PASS (pass_dce_loop);
914 }
6a753d5f 915 NEXT_PASS (pass_predcom);
f1bd2543 916 NEXT_PASS (pass_complete_unroll);
a70d6342 917 NEXT_PASS (pass_slp_vectorize);
5f40b3cb 918 NEXT_PASS (pass_parallelize_loops);
f1bd2543
JH
919 NEXT_PASS (pass_loop_prefetch);
920 NEXT_PASS (pass_iv_optimize);
921 NEXT_PASS (pass_tree_loop_done);
922 }
923 NEXT_PASS (pass_cse_reciprocals);
924 NEXT_PASS (pass_reassoc);
925 NEXT_PASS (pass_vrp);
926 NEXT_PASS (pass_dominator);
f1bd2543
JH
927 /* The only const/copy propagation opportunities left after
928 DOM should be due to degenerate PHI nodes. So rather than
929 run the full propagators, run a specialized pass which
930 only examines PHIs to discover const/copy propagation
931 opportunities. */
932 NEXT_PASS (pass_phi_only_cprop);
f1bd2543 933 NEXT_PASS (pass_cd_dce);
232abc3f 934 NEXT_PASS (pass_tracer);
f1bd2543
JH
935
936 /* FIXME: If DCE is not run before checking for uninitialized uses,
937 we may get false warnings (e.g., testsuite/gcc.dg/uninit-5.c).
938 However, this also causes us to misdiagnose cases that should be
939 real warnings (e.g., testsuite/gcc.dg/pr18501.c).
b8698a0f 940
f1bd2543
JH
941 To fix the false positives in uninit-5.c, we would have to
942 account for the predicates protecting the set and the use of each
943 variable. Using a representation like Gated Single Assignment
944 may help. */
945 NEXT_PASS (pass_late_warn_uninitialized);
946 NEXT_PASS (pass_dse);
947 NEXT_PASS (pass_forwprop);
948 NEXT_PASS (pass_phiopt);
44e10129 949 NEXT_PASS (pass_fold_builtins);
5b58b39b 950 NEXT_PASS (pass_optimize_widening_mul);
f1bd2543
JH
951 NEXT_PASS (pass_tail_calls);
952 NEXT_PASS (pass_rename_ssa_copies);
953 NEXT_PASS (pass_uncprop);
33977f81 954 NEXT_PASS (pass_local_pure_const);
f1bd2543 955 }
688a482d 956 NEXT_PASS (pass_lower_complex_O0);
6d07ad98 957 NEXT_PASS (pass_cleanup_eh);
1d65f45c 958 NEXT_PASS (pass_lower_resx);
c72321c9 959 NEXT_PASS (pass_nrv);
4e3825db 960 NEXT_PASS (pass_mudflap_2);
c72321c9 961 NEXT_PASS (pass_cleanup_cfg_post_optimizing);
ef330312 962 NEXT_PASS (pass_warn_function_noreturn);
726a989a 963
ef330312 964 NEXT_PASS (pass_expand);
4e3825db 965
ef330312 966 NEXT_PASS (pass_rest_of_compilation);
f1bd2543 967 {
8ddbbcae 968 struct opt_pass **p = &pass_rest_of_compilation.pass.sub;
f1bd2543
JH
969 NEXT_PASS (pass_init_function);
970 NEXT_PASS (pass_jump);
f1bd2543
JH
971 NEXT_PASS (pass_rtl_eh);
972 NEXT_PASS (pass_initial_value_sets);
973 NEXT_PASS (pass_unshare_all_rtl);
974 NEXT_PASS (pass_instantiate_virtual_regs);
dacb3cda 975 NEXT_PASS (pass_into_cfg_layout_mode);
f1bd2543
JH
976 NEXT_PASS (pass_jump2);
977 NEXT_PASS (pass_lower_subreg);
6fb5fa3c 978 NEXT_PASS (pass_df_initialize_opt);
f1bd2543
JH
979 NEXT_PASS (pass_cse);
980 NEXT_PASS (pass_rtl_fwprop);
5f39ad47
SB
981 NEXT_PASS (pass_rtl_cprop);
982 NEXT_PASS (pass_rtl_pre);
983 NEXT_PASS (pass_rtl_hoist);
984 NEXT_PASS (pass_rtl_cprop);
985 NEXT_PASS (pass_rtl_store_motion);
986 NEXT_PASS (pass_cse_after_global_opts);
f1bd2543 987 NEXT_PASS (pass_rtl_ifcvt);
1833192f 988 NEXT_PASS (pass_reginfo_init);
f1bd2543
JH
989 /* Perform loop optimizations. It might be better to do them a bit
990 sooner, but we want the profile feedback to work more
991 efficiently. */
992 NEXT_PASS (pass_loop2);
993 {
8ddbbcae 994 struct opt_pass **p = &pass_loop2.pass.sub;
f1bd2543
JH
995 NEXT_PASS (pass_rtl_loop_init);
996 NEXT_PASS (pass_rtl_move_loop_invariants);
997 NEXT_PASS (pass_rtl_unswitch);
998 NEXT_PASS (pass_rtl_unroll_and_peel_loops);
999 NEXT_PASS (pass_rtl_doloop);
1000 NEXT_PASS (pass_rtl_loop_done);
1001 *p = NULL;
1002 }
1003 NEXT_PASS (pass_web);
5f39ad47 1004 NEXT_PASS (pass_rtl_cprop);
f1bd2543 1005 NEXT_PASS (pass_cse2);
6fb5fa3c 1006 NEXT_PASS (pass_rtl_dse1);
f1bd2543 1007 NEXT_PASS (pass_rtl_fwprop_addr);
6fb5fa3c
DB
1008 NEXT_PASS (pass_inc_dec);
1009 NEXT_PASS (pass_initialize_regs);
6fb5fa3c 1010 NEXT_PASS (pass_ud_rtl_dce);
f1bd2543
JH
1011 NEXT_PASS (pass_combine);
1012 NEXT_PASS (pass_if_after_combine);
1013 NEXT_PASS (pass_partition_blocks);
1014 NEXT_PASS (pass_regmove);
d25aa7ab 1015 NEXT_PASS (pass_outof_cfg_layout_mode);
f1bd2543
JH
1016 NEXT_PASS (pass_split_all_insns);
1017 NEXT_PASS (pass_lower_subreg2);
6fb5fa3c
DB
1018 NEXT_PASS (pass_df_initialize_no_opt);
1019 NEXT_PASS (pass_stack_ptr_mod);
f1bd2543 1020 NEXT_PASS (pass_mode_switching);
d8d72314 1021 NEXT_PASS (pass_match_asm_constraints);
f1bd2543 1022 NEXT_PASS (pass_sms);
ce18efcb 1023 NEXT_PASS (pass_sched);
058e97ec 1024 NEXT_PASS (pass_ira);
f1bd2543
JH
1025 NEXT_PASS (pass_postreload);
1026 {
8ddbbcae 1027 struct opt_pass **p = &pass_postreload.pass.sub;
f1bd2543
JH
1028 NEXT_PASS (pass_postreload_cse);
1029 NEXT_PASS (pass_gcse2);
6fb5fa3c 1030 NEXT_PASS (pass_split_after_reload);
87a0ebfd 1031 NEXT_PASS (pass_implicit_zee);
6fb5fa3c
DB
1032 NEXT_PASS (pass_branch_target_load_optimize1);
1033 NEXT_PASS (pass_thread_prologue_and_epilogue);
1034 NEXT_PASS (pass_rtl_dse2);
f1bd2543
JH
1035 NEXT_PASS (pass_stack_adjustments);
1036 NEXT_PASS (pass_peephole2);
1037 NEXT_PASS (pass_if_after_reload);
1038 NEXT_PASS (pass_regrename);
6fb5fa3c
DB
1039 NEXT_PASS (pass_cprop_hardreg);
1040 NEXT_PASS (pass_fast_rtl_dce);
f1bd2543 1041 NEXT_PASS (pass_reorder_blocks);
6fb5fa3c 1042 NEXT_PASS (pass_branch_target_load_optimize2);
f1bd2543 1043 NEXT_PASS (pass_leaf_regs);
6fb5fa3c 1044 NEXT_PASS (pass_split_before_sched2);
f1bd2543 1045 NEXT_PASS (pass_sched2);
f1bd2543 1046 NEXT_PASS (pass_stack_regs);
6fb5fa3c 1047 {
8ddbbcae 1048 struct opt_pass **p = &pass_stack_regs.pass.sub;
6fb5fa3c
DB
1049 NEXT_PASS (pass_split_before_regstack);
1050 NEXT_PASS (pass_stack_regs_run);
1051 }
f1bd2543
JH
1052 NEXT_PASS (pass_compute_alignments);
1053 NEXT_PASS (pass_duplicate_computed_gotos);
1054 NEXT_PASS (pass_variable_tracking);
1055 NEXT_PASS (pass_free_cfg);
1056 NEXT_PASS (pass_machine_reorg);
1057 NEXT_PASS (pass_cleanup_barriers);
1058 NEXT_PASS (pass_delay_slots);
1059 NEXT_PASS (pass_split_for_shorten_branches);
1060 NEXT_PASS (pass_convert_to_eh_region_ranges);
1061 NEXT_PASS (pass_shorten_branches);
1062 NEXT_PASS (pass_set_nothrow_function_flags);
1063 NEXT_PASS (pass_final);
1064 }
0b738568 1065 NEXT_PASS (pass_df_finish);
f1bd2543 1066 }
ef330312
PB
1067 NEXT_PASS (pass_clean_state);
1068 *p = NULL;
1069
ef330312
PB
1070#undef NEXT_PASS
1071
1072 /* Register the passes with the tree dump code. */
9e016eba 1073 register_dump_files (all_lowering_passes, PROP_gimple_any);
b8698a0f 1074 register_dump_files (all_small_ipa_passes,
d7f09764
DN
1075 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1076 | PROP_cfg);
b8698a0f 1077 register_dump_files (all_regular_ipa_passes,
d7f09764
DN
1078 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1079 | PROP_cfg);
b8698a0f 1080 register_dump_files (all_lto_gen_passes,
bbbe4e7b
PB
1081 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1082 | PROP_cfg);
b8698a0f 1083 register_dump_files (all_passes,
bbbe4e7b
PB
1084 PROP_gimple_any | PROP_gimple_lcf | PROP_gimple_leh
1085 | PROP_cfg);
ef330312
PB
1086}
1087
a5093353
JH
1088/* If we are in IPA mode (i.e., current_function_decl is NULL), call
1089 function CALLBACK for every function in the call graph. Otherwise,
b8698a0f 1090 call CALLBACK on the current function. */
bbbe4e7b 1091
ef330312 1092static void
a5093353 1093do_per_function (void (*callback) (void *data), void *data)
ef330312 1094{
a5093353
JH
1095 if (current_function_decl)
1096 callback (data);
1097 else
1098 {
1099 struct cgraph_node *node;
1100 for (node = cgraph_nodes; node; node = node->next)
21ecdec5
JH
1101 if (node->analyzed && gimple_has_body_p (node->decl)
1102 && (!node->clone_of || node->decl != node->clone_of->decl))
a5093353
JH
1103 {
1104 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1105 current_function_decl = node->decl;
1106 callback (data);
d7f09764
DN
1107 if (!flag_wpa)
1108 {
1109 free_dominance_info (CDI_DOMINATORS);
1110 free_dominance_info (CDI_POST_DOMINATORS);
1111 }
a5093353
JH
1112 current_function_decl = NULL;
1113 pop_cfun ();
1114 ggc_collect ();
1115 }
1116 }
1117}
1118
873aa8f5
JH
1119/* Because inlining might remove no-longer reachable nodes, we need to
1120 keep the array visible to garbage collector to avoid reading collected
1121 out nodes. */
1122static int nnodes;
a9429e29 1123static GTY ((length ("nnodes"))) cgraph_node_ptr *order;
873aa8f5
JH
1124
1125/* If we are in IPA mode (i.e., current_function_decl is NULL), call
1126 function CALLBACK for every function in the call graph. Otherwise,
090fa0ab
GF
1127 call CALLBACK on the current function.
1128 This function is global so that plugins can use it. */
1129void
873aa8f5
JH
1130do_per_function_toporder (void (*callback) (void *data), void *data)
1131{
1132 int i;
1133
1134 if (current_function_decl)
1135 callback (data);
1136 else
1137 {
1138 gcc_assert (!order);
a9429e29 1139 order = ggc_alloc_vec_cgraph_node_ptr (cgraph_n_nodes);
873aa8f5 1140 nnodes = cgraph_postorder (order);
257eb6e3
JH
1141 for (i = nnodes - 1; i >= 0; i--)
1142 order[i]->process = 1;
873aa8f5
JH
1143 for (i = nnodes - 1; i >= 0; i--)
1144 {
1145 struct cgraph_node *node = order[i];
1146
1147 /* Allow possibly removed nodes to be garbage collected. */
1148 order[i] = NULL;
257eb6e3 1149 node->process = 0;
b20996ff 1150 if (node->analyzed)
873aa8f5
JH
1151 {
1152 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1153 current_function_decl = node->decl;
1154 callback (data);
1155 free_dominance_info (CDI_DOMINATORS);
1156 free_dominance_info (CDI_POST_DOMINATORS);
1157 current_function_decl = NULL;
1158 pop_cfun ();
1159 ggc_collect ();
1160 }
1161 }
1162 }
1163 ggc_free (order);
1164 order = NULL;
1165 nnodes = 0;
1166}
1167
a5093353 1168/* Perform all TODO actions that ought to be done on each function. */
ef330312 1169
a5093353
JH
1170static void
1171execute_function_todo (void *data)
1172{
1173 unsigned int flags = (size_t)data;
a5093353 1174 flags &= ~cfun->last_verified;
bbbe4e7b
PB
1175 if (!flags)
1176 return;
9fe0cb7d
RG
1177
1178 statistics_fini_pass ();
1179
1994bfea 1180 /* Always cleanup the CFG before trying to update SSA. */
ef330312
PB
1181 if (flags & TODO_cleanup_cfg)
1182 {
592c303d 1183 bool cleanup = cleanup_tree_cfg ();
c4f548b8 1184
1994bfea
JH
1185 if (cleanup && (cfun->curr_properties & PROP_ssa))
1186 flags |= TODO_remove_unused_locals;
b8698a0f 1187
c4f548b8
DN
1188 /* When cleanup_tree_cfg merges consecutive blocks, it may
1189 perform some simplistic propagation when removing single
1190 valued PHI nodes. This propagation may, in turn, cause the
1191 SSA form to become out-of-date (see PR 22037). So, even
1192 if the parent pass had not scheduled an SSA update, we may
1193 still need to do one. */
5006671f 1194 if (!(flags & TODO_update_ssa_any) && need_ssa_update_p (cfun))
c4f548b8 1195 flags |= TODO_update_ssa;
ef330312 1196 }
f6db1481 1197
563cb6be
DN
1198 if (flags & TODO_update_ssa_any)
1199 {
1200 unsigned update_flags = flags & TODO_update_ssa_any;
1201 update_ssa (update_flags);
a5093353 1202 cfun->last_verified &= ~TODO_verify_ssa;
563cb6be 1203 }
b8698a0f 1204
5006671f
RG
1205 if (flags & TODO_update_address_taken)
1206 execute_update_addresses_taken (true);
1207
7b0e48fb
DB
1208 if (flags & TODO_rebuild_alias)
1209 {
5006671f
RG
1210 if (!(flags & TODO_update_address_taken))
1211 execute_update_addresses_taken (true);
7b0e48fb 1212 compute_may_aliases ();
7b0e48fb 1213 }
b8698a0f 1214
3f519b35
RG
1215 if (flags & TODO_remove_unused_locals)
1216 remove_unused_locals ();
1217
726a989a 1218 if ((flags & TODO_dump_func) && dump_file && current_function_decl)
ef330312 1219 {
a5093353 1220 if (cfun->curr_properties & PROP_trees)
726a989a 1221 dump_function_to_file (current_function_decl, dump_file, dump_flags);
a68e7e6c 1222 else
5e34206b 1223 {
a68e7e6c
PB
1224 if (dump_flags & TDF_SLIM)
1225 print_rtl_slim_with_bb (dump_file, get_insns (), dump_flags);
a5093353
JH
1226 else if ((cfun->curr_properties & PROP_cfg)
1227 && (dump_flags & TDF_BLOCKS))
a68e7e6c
PB
1228 print_rtl_with_bb (dump_file, get_insns ());
1229 else
1230 print_rtl (dump_file, get_insns ());
1231
726a989a 1232 if ((cfun->curr_properties & PROP_cfg)
a68e7e6c 1233 && graph_dump_format != no_graph
5e34206b
JJ
1234 && (dump_flags & TDF_GRAPH))
1235 print_rtl_graph_with_bb (dump_file_name, get_insns ());
1236 }
ef330312
PB
1237
1238 /* Flush the file. If verification fails, we won't be able to
1239 close the file before aborting. */
1240 fflush (dump_file);
1241 }
a5093353 1242
45a80bb9
JH
1243 if (flags & TODO_rebuild_frequencies)
1244 {
1245 if (profile_status == PROFILE_GUESSED)
1246 {
1247 loop_optimizer_init (0);
1248 add_noreturn_fake_exit_edges ();
1249 mark_irreducible_loops ();
1250 connect_infinite_loops_to_exit ();
1251 estimate_bb_frequencies ();
1252 remove_fake_exit_edges ();
1253 loop_optimizer_finalize ();
1254 }
1255 else if (profile_status == PROFILE_READ)
1256 counts_to_freqs ();
1257 else
1258 gcc_unreachable ();
1259 }
1260
a5093353 1261#if defined ENABLE_CHECKING
a3b9e73c
SP
1262 if (flags & TODO_verify_ssa
1263 || (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA)))
a5093353
JH
1264 verify_ssa (true);
1265 if (flags & TODO_verify_flow)
1266 verify_flow_info ();
1267 if (flags & TODO_verify_stmts)
1268 verify_stmts ();
98b6e9dd 1269 if (current_loops && loops_state_satisfies_p (LOOP_CLOSED_SSA))
a3b9e73c 1270 verify_loop_closed_ssa (false);
a36b8a1e
JH
1271 if (flags & TODO_verify_rtl_sharing)
1272 verify_rtl_sharing ();
a5093353
JH
1273#endif
1274
1275 cfun->last_verified = flags & TODO_verify_all;
1276}
1277
1278/* Perform all TODO actions. */
1279static void
1280execute_todo (unsigned int flags)
1281{
1282#if defined ENABLE_CHECKING
5006671f
RG
1283 if (cfun
1284 && need_ssa_update_p (cfun))
a5093353
JH
1285 gcc_assert (flags & TODO_update_ssa_any);
1286#endif
1287
b02b9b53
ZD
1288 /* Inform the pass whether it is the first time it is run. */
1289 first_pass_instance = (flags & TODO_mark_first_instance) != 0;
1290
a5093353
JH
1291 do_per_function (execute_function_todo, (void *)(size_t) flags);
1292
f4b3ca72
JH
1293 /* Always remove functions just as before inlining: IPA passes might be
1294 interested to see bodies of extern inline functions that are not inlined
1295 to analyze side effects. The full removal is done just at the end
1296 of IPA pass queue. */
1297 if (flags & TODO_remove_functions)
a12f79f5
JH
1298 {
1299 gcc_assert (!cfun);
1300 cgraph_remove_unreachable_nodes (true, dump_file);
1301 }
f4b3ca72 1302
726a989a 1303 if ((flags & TODO_dump_cgraph) && dump_file && !current_function_decl)
ef330312 1304 {
a12f79f5 1305 gcc_assert (!cfun);
ef330312
PB
1306 dump_cgraph (dump_file);
1307 /* Flush the file. If verification fails, we won't be able to
1308 close the file before aborting. */
1309 fflush (dump_file);
1310 }
f6db1481 1311
ef330312 1312 if (flags & TODO_ggc_collect)
726a989a 1313 ggc_collect ();
6fb5fa3c 1314
b8698a0f 1315 /* Now that the dumping has been done, we can get rid of the optional
6fb5fa3c
DB
1316 df problems. */
1317 if (flags & TODO_df_finish)
0d475361 1318 df_finish_pass ((flags & TODO_df_verify) != 0);
a5093353 1319}
97b0ade3 1320
6ac01510
ILT
1321/* Verify invariants that should hold between passes. This is a place
1322 to put simple sanity checks. */
1323
1324static void
1325verify_interpass_invariants (void)
1326{
1327#ifdef ENABLE_CHECKING
1328 gcc_assert (!fold_deferring_overflow_warnings_p ());
1329#endif
1330}
1331
a5093353
JH
1332/* Clear the last verified flag. */
1333
1334static void
1335clear_last_verified (void *data ATTRIBUTE_UNUSED)
1336{
1337 cfun->last_verified = 0;
1338}
1339
1340/* Helper function. Verify that the properties has been turn into the
1341 properties expected by the pass. */
bbbe4e7b 1342
582fd9ce 1343#ifdef ENABLE_CHECKING
a5093353
JH
1344static void
1345verify_curr_properties (void *data)
1346{
1347 unsigned int props = (size_t)data;
1348 gcc_assert ((cfun->curr_properties & props) == props);
1349}
582fd9ce 1350#endif
a5093353 1351
17653c00 1352/* Initialize pass dump file. */
090fa0ab 1353/* This is non-static so that the plugins can use it. */
17653c00 1354
090fa0ab 1355bool
17653c00
JH
1356pass_init_dump_file (struct opt_pass *pass)
1357{
1358 /* If a dump file name is present, open it if enabled. */
1359 if (pass->static_pass_number != -1)
1360 {
1361 bool initializing_dump = !dump_initialized_p (pass->static_pass_number);
1362 dump_file_name = get_dump_file_name (pass->static_pass_number);
1363 dump_file = dump_begin (pass->static_pass_number, &dump_flags);
1364 if (dump_file && current_function_decl)
1365 {
1366 const char *dname, *aname;
5fefcf92 1367 struct cgraph_node *node = cgraph_node (current_function_decl);
17653c00
JH
1368 dname = lang_hooks.decl_printable_name (current_function_decl, 2);
1369 aname = (IDENTIFIER_POINTER
1370 (DECL_ASSEMBLER_NAME (current_function_decl)));
3716f233 1371 fprintf (dump_file, "\n;; Function %s (%s)%s\n\n", dname, aname,
5fefcf92 1372 node->frequency == NODE_FREQUENCY_HOT
17653c00 1373 ? " (hot)"
5fefcf92 1374 : node->frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED
17653c00 1375 ? " (unlikely executed)"
5fefcf92
JH
1376 : node->frequency == NODE_FREQUENCY_EXECUTED_ONCE
1377 ? " (executed once)"
17653c00
JH
1378 : "");
1379 }
1380 return initializing_dump;
1381 }
1382 else
1383 return false;
1384}
1385
1386/* Flush PASS dump file. */
090fa0ab 1387/* This is non-static so that plugins can use it. */
17653c00 1388
090fa0ab 1389void
17653c00
JH
1390pass_fini_dump_file (struct opt_pass *pass)
1391{
1392 /* Flush and close dump file. */
1393 if (dump_file_name)
1394 {
1395 free (CONST_CAST (char *, dump_file_name));
1396 dump_file_name = NULL;
1397 }
1398
1399 if (dump_file)
1400 {
1401 dump_end (pass->static_pass_number, dump_file);
1402 dump_file = NULL;
1403 }
1404}
1405
a5093353
JH
1406/* After executing the pass, apply expected changes to the function
1407 properties. */
17653c00 1408
a5093353
JH
1409static void
1410update_properties_after_pass (void *data)
1411{
d3bfe4de 1412 struct opt_pass *pass = (struct opt_pass *) data;
a5093353
JH
1413 cfun->curr_properties = (cfun->curr_properties | pass->properties_provided)
1414 & ~pass->properties_destroyed;
f6db1481
RH
1415}
1416
1920df6c 1417/* Execute summary generation for all of the passes in IPA_PASS. */
17653c00 1418
d7f09764 1419void
7e5487a2 1420execute_ipa_summary_passes (struct ipa_opt_pass_d *ipa_pass)
17653c00 1421{
1920df6c 1422 while (ipa_pass)
17653c00
JH
1423 {
1424 struct opt_pass *pass = &ipa_pass->pass;
1920df6c
KZ
1425
1426 /* Execute all of the IPA_PASSes in the list. */
b8698a0f 1427 if (ipa_pass->pass.type == IPA_PASS
d7f09764
DN
1428 && (!pass->gate || pass->gate ())
1429 && ipa_pass->generate_summary)
17653c00
JH
1430 {
1431 pass_init_dump_file (pass);
d7f09764
DN
1432
1433 /* If a timevar is present, start it. */
1434 if (pass->tv_id)
1435 timevar_push (pass->tv_id);
1436
1920df6c 1437 ipa_pass->generate_summary ();
d7f09764
DN
1438
1439 /* Stop timevar. */
1440 if (pass->tv_id)
1441 timevar_pop (pass->tv_id);
1442
17653c00
JH
1443 pass_fini_dump_file (pass);
1444 }
7e5487a2 1445 ipa_pass = (struct ipa_opt_pass_d *)ipa_pass->pass.next;
17653c00
JH
1446 }
1447}
1448
1449/* Execute IPA_PASS function transform on NODE. */
1450
1451static void
1452execute_one_ipa_transform_pass (struct cgraph_node *node,
7e5487a2 1453 struct ipa_opt_pass_d *ipa_pass)
17653c00
JH
1454{
1455 struct opt_pass *pass = &ipa_pass->pass;
1456 unsigned int todo_after = 0;
1457
1458 current_pass = pass;
1459 if (!ipa_pass->function_transform)
1460 return;
1461
1462 /* Note that the folders should only create gimple expressions.
1463 This is a hack until the new folder is ready. */
1464 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
1465
1466 pass_init_dump_file (pass);
1467
1468 /* Run pre-pass verification. */
a12f79f5 1469 execute_todo (ipa_pass->function_transform_todo_flags_start);
17653c00
JH
1470
1471 /* If a timevar is present, start it. */
7072a650 1472 if (pass->tv_id != TV_NONE)
17653c00
JH
1473 timevar_push (pass->tv_id);
1474
1475 /* Do it! */
1476 todo_after = ipa_pass->function_transform (node);
1477
1478 /* Stop timevar. */
7072a650 1479 if (pass->tv_id != TV_NONE)
17653c00
JH
1480 timevar_pop (pass->tv_id);
1481
1482 /* Run post-pass cleanup and verification. */
1483 execute_todo (todo_after);
1484 verify_interpass_invariants ();
1485
1486 pass_fini_dump_file (pass);
1487
1488 current_pass = NULL;
17653c00
JH
1489}
1490
d7f09764 1491/* For the current function, execute all ipa transforms. */
9e016eba 1492
d7f09764
DN
1493void
1494execute_all_ipa_transforms (void)
1495{
0e3776db
JH
1496 struct cgraph_node *node;
1497 if (!cfun)
1498 return;
1499 node = cgraph_node (current_function_decl);
bc58d7e1 1500
0e3776db 1501 if (node->ipa_transforms_to_apply)
17653c00
JH
1502 {
1503 unsigned int i;
17653c00 1504
0e3776db 1505 for (i = 0; i < VEC_length (ipa_opt_pass, node->ipa_transforms_to_apply);
17653c00
JH
1506 i++)
1507 execute_one_ipa_transform_pass (node,
d7f09764 1508 VEC_index (ipa_opt_pass,
0e3776db 1509 node->ipa_transforms_to_apply,
17653c00 1510 i));
0e3776db
JH
1511 VEC_free (ipa_opt_pass, heap, node->ipa_transforms_to_apply);
1512 node->ipa_transforms_to_apply = NULL;
17653c00 1513 }
d7f09764
DN
1514}
1515
1516/* Execute PASS. */
1517
090fa0ab 1518bool
d7f09764
DN
1519execute_one_pass (struct opt_pass *pass)
1520{
1521 bool initializing_dump;
1522 unsigned int todo_after = 0;
1523
090fa0ab
GF
1524 bool gate_status;
1525
d7f09764
DN
1526 /* IPA passes are executed on whole program, so cfun should be NULL.
1527 Other passes need function context set. */
1528 if (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS)
1529 gcc_assert (!cfun && !current_function_decl);
1530 else
1531 gcc_assert (cfun && current_function_decl);
17653c00 1532
6fb5fa3c 1533 current_pass = pass;
726a989a 1534
090fa0ab
GF
1535 /* Check whether gate check should be avoided.
1536 User controls the value of the gate through the parameter "gate_status". */
1537 gate_status = (pass->gate == NULL) ? true : pass->gate();
1538
1539 /* Override gate with plugin. */
1540 invoke_plugin_callbacks (PLUGIN_OVERRIDE_GATE, &gate_status);
1541
1542 if (!gate_status)
1543 {
1544 current_pass = NULL;
1545 return false;
1546 }
1547
1548 /* Pass execution event trigger: useful to identify passes being
1549 executed. */
1550 invoke_plugin_callbacks (PLUGIN_PASS_EXECUTION, pass);
f6db1481 1551
b9cafe60 1552 if (!quiet_flag && !cfun)
873aa8f5
JH
1553 fprintf (stderr, " <%s>", pass->name ? pass->name : "");
1554
ef330312
PB
1555 /* Note that the folders should only create gimple expressions.
1556 This is a hack until the new folder is ready. */
a5093353 1557 in_gimple_form = (cfun && (cfun->curr_properties & PROP_trees)) != 0;
f6db1481 1558
5006671f
RG
1559 initializing_dump = pass_init_dump_file (pass);
1560
ef330312 1561 /* Run pre-pass verification. */
bbbe4e7b
PB
1562 execute_todo (pass->todo_flags_start);
1563
a5093353
JH
1564#ifdef ENABLE_CHECKING
1565 do_per_function (verify_curr_properties,
1566 (void *)(size_t)pass->properties_required);
1567#endif
f6db1481 1568
ef330312 1569 /* If a timevar is present, start it. */
7072a650 1570 if (pass->tv_id != TV_NONE)
ef330312 1571 timevar_push (pass->tv_id);
f6db1481 1572
ef330312
PB
1573 /* Do it! */
1574 if (pass->execute)
bbbe4e7b 1575 {
c2924966 1576 todo_after = pass->execute ();
a5093353 1577 do_per_function (clear_last_verified, NULL);
bbbe4e7b 1578 }
f6db1481 1579
ef330312 1580 /* Stop timevar. */
7072a650 1581 if (pass->tv_id != TV_NONE)
ef330312 1582 timevar_pop (pass->tv_id);
f6db1481 1583
a5093353 1584 do_per_function (update_properties_after_pass, pass);
bbbe4e7b
PB
1585
1586 if (initializing_dump
1587 && dump_file
1588 && graph_dump_format != no_graph
6f1fe305 1589 && cfun
a5093353
JH
1590 && (cfun->curr_properties & (PROP_cfg | PROP_rtl))
1591 == (PROP_cfg | PROP_rtl))
bbbe4e7b
PB
1592 {
1593 get_dump_file_info (pass->static_pass_number)->flags |= TDF_GRAPH;
1594 dump_flags |= TDF_GRAPH;
1595 clean_graph_dump_file (dump_file_name);
1596 }
1597
ef330312 1598 /* Run post-pass cleanup and verification. */
c2924966 1599 execute_todo (todo_after | pass->todo_flags_finish);
6ac01510 1600 verify_interpass_invariants ();
17653c00 1601 if (pass->type == IPA_PASS)
0e3776db
JH
1602 {
1603 struct cgraph_node *node;
1604 for (node = cgraph_nodes; node; node = node->next)
1605 if (node->analyzed)
1606 VEC_safe_push (ipa_opt_pass, heap, node->ipa_transforms_to_apply,
1607 (struct ipa_opt_pass_d *)pass);
1608 }
f6db1481 1609
f45e0ad1
JH
1610 if (!current_function_decl)
1611 cgraph_process_new_functions ();
1612
17653c00 1613 pass_fini_dump_file (pass);
f6db1481 1614
17653c00 1615 if (pass->type != SIMPLE_IPA_PASS && pass->type != IPA_PASS)
e3b5732b
JH
1616 gcc_assert (!(cfun->curr_properties & PROP_trees)
1617 || pass->type != RTL_PASS);
1618
6fb5fa3c 1619 current_pass = NULL;
91851351 1620
ef330312 1621 return true;
f6db1481
RH
1622}
1623
ef330312 1624void
8ddbbcae 1625execute_pass_list (struct opt_pass *pass)
f6db1481 1626{
ef330312 1627 do
f6db1481 1628 {
9e016eba
JH
1629 gcc_assert (pass->type == GIMPLE_PASS
1630 || pass->type == RTL_PASS);
ef330312
PB
1631 if (execute_one_pass (pass) && pass->sub)
1632 execute_pass_list (pass->sub);
1633 pass = pass->next;
f6db1481 1634 }
ef330312 1635 while (pass);
f6db1481
RH
1636}
1637
ef330312 1638/* Same as execute_pass_list but assume that subpasses of IPA passes
d7f09764
DN
1639 are local passes. If SET is not NULL, write out summaries of only
1640 those node in SET. */
1641
1642static void
1643ipa_write_summaries_2 (struct opt_pass *pass, cgraph_node_set set,
2942c502 1644 varpool_node_set vset,
d7f09764
DN
1645 struct lto_out_decl_state *state)
1646{
1647 while (pass)
1648 {
1649 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
1650 gcc_assert (!current_function_decl);
1651 gcc_assert (!cfun);
1652 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1653 if (pass->type == IPA_PASS
1654 && ipa_pass->write_summary
1655 && (!pass->gate || pass->gate ()))
1656 {
1657 /* If a timevar is present, start it. */
1658 if (pass->tv_id)
1659 timevar_push (pass->tv_id);
1660
f59292da
JH
1661 pass_init_dump_file (pass);
1662
2942c502 1663 ipa_pass->write_summary (set,vset);
d7f09764 1664
f59292da
JH
1665 pass_fini_dump_file (pass);
1666
d7f09764
DN
1667 /* If a timevar is present, start it. */
1668 if (pass->tv_id)
1669 timevar_pop (pass->tv_id);
1670 }
1671
1672 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2942c502 1673 ipa_write_summaries_2 (pass->sub, set, vset, state);
d7f09764
DN
1674
1675 pass = pass->next;
1676 }
1677}
1678
1679/* Helper function of ipa_write_summaries. Creates and destroys the
1680 decl state and calls ipa_write_summaries_2 for all passes that have
1681 summaries. SET is the set of nodes to be written. */
1682
1683static void
2942c502 1684ipa_write_summaries_1 (cgraph_node_set set, varpool_node_set vset)
d7f09764
DN
1685{
1686 struct lto_out_decl_state *state = lto_new_out_decl_state ();
f3380641 1687 compute_ltrans_boundary (state, set, vset);
5c4f225f 1688
d7f09764
DN
1689 lto_push_out_decl_state (state);
1690
e792884f 1691 gcc_assert (!flag_wpa);
2942c502
JH
1692 ipa_write_summaries_2 (all_regular_ipa_passes, set, vset, state);
1693 ipa_write_summaries_2 (all_lto_gen_passes, set, vset, state);
d7f09764
DN
1694
1695 gcc_assert (lto_get_out_decl_state () == state);
1696 lto_pop_out_decl_state ();
1697 lto_delete_out_decl_state (state);
1698}
1699
1700/* Write out summaries for all the nodes in the callgraph. */
1701
ef330312 1702void
d7f09764 1703ipa_write_summaries (void)
f6db1481 1704{
d7f09764 1705 cgraph_node_set set;
2942c502 1706 varpool_node_set vset;
d7f09764 1707 struct cgraph_node **order;
2942c502 1708 struct varpool_node *vnode;
d7f09764 1709 int i, order_pos;
b8698a0f 1710
1da2ed5f 1711 if (!flag_generate_lto || seen_error ())
d7f09764
DN
1712 return;
1713
d7f09764
DN
1714 set = cgraph_node_set_new ();
1715
1716 /* Create the callgraph set in the same order used in
1717 cgraph_expand_all_functions. This mostly facilitates debugging,
1718 since it causes the gimple file to be processed in the same order
1719 as the source code. */
1720 order = XCNEWVEC (struct cgraph_node *, cgraph_n_nodes);
1721 order_pos = cgraph_postorder (order);
1722 gcc_assert (order_pos == cgraph_n_nodes);
1723
1724 for (i = order_pos - 1; i >= 0; i--)
8b220502
MJ
1725 {
1726 struct cgraph_node *node = order[i];
1727
1728 if (node->analyzed)
1729 {
1730 /* When streaming out references to statements as part of some IPA
1731 pass summary, the statements need to have uids assigned and the
1732 following does that for all the IPA passes here. Naturally, this
1733 ordering then matches the one IPA-passes get in their stmt_fixup
1734 hooks. */
1735
1736 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1737 renumber_gimple_stmt_uids ();
1738 pop_cfun ();
1739 }
9b3cf76a 1740 if (node->analyzed)
2942c502 1741 cgraph_node_set_add (set, node);
8b220502 1742 }
2942c502 1743 vset = varpool_node_set_new ();
d7f09764 1744
2942c502
JH
1745 for (vnode = varpool_nodes; vnode; vnode = vnode->next)
1746 if (vnode->needed && !vnode->alias)
1747 varpool_node_set_add (vset, vnode);
1748
1749 ipa_write_summaries_1 (set, vset);
d7f09764
DN
1750
1751 free (order);
1752 ggc_free (set);
2942c502 1753 ggc_free (vset);
d7f09764
DN
1754}
1755
e792884f
JH
1756/* Same as execute_pass_list but assume that subpasses of IPA passes
1757 are local passes. If SET is not NULL, write out optimization summaries of
1758 only those node in SET. */
d7f09764 1759
e792884f
JH
1760static void
1761ipa_write_optimization_summaries_1 (struct opt_pass *pass, cgraph_node_set set,
2942c502 1762 varpool_node_set vset,
e792884f
JH
1763 struct lto_out_decl_state *state)
1764{
1765 while (pass)
1766 {
1767 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *)pass;
1768 gcc_assert (!current_function_decl);
1769 gcc_assert (!cfun);
1770 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1771 if (pass->type == IPA_PASS
1772 && ipa_pass->write_optimization_summary
1773 && (!pass->gate || pass->gate ()))
1774 {
1775 /* If a timevar is present, start it. */
1776 if (pass->tv_id)
1777 timevar_push (pass->tv_id);
1778
f59292da
JH
1779 pass_init_dump_file (pass);
1780
2942c502 1781 ipa_pass->write_optimization_summary (set, vset);
e792884f 1782
f59292da
JH
1783 pass_fini_dump_file (pass);
1784
e792884f
JH
1785 /* If a timevar is present, start it. */
1786 if (pass->tv_id)
1787 timevar_pop (pass->tv_id);
1788 }
1789
1790 if (pass->sub && pass->sub->type != GIMPLE_PASS)
2942c502 1791 ipa_write_optimization_summaries_1 (pass->sub, set, vset, state);
e792884f
JH
1792
1793 pass = pass->next;
1794 }
1795}
1796
1797/* Write all the optimization summaries for the cgraph nodes in SET. If SET is
d7f09764
DN
1798 NULL, write out all summaries of all nodes. */
1799
1800void
2942c502 1801ipa_write_optimization_summaries (cgraph_node_set set, varpool_node_set vset)
d7f09764 1802{
e792884f 1803 struct lto_out_decl_state *state = lto_new_out_decl_state ();
f3380641
JH
1804 compute_ltrans_boundary (state, set, vset);
1805
e792884f
JH
1806 lto_push_out_decl_state (state);
1807
1808 gcc_assert (flag_wpa);
2942c502
JH
1809 ipa_write_optimization_summaries_1 (all_regular_ipa_passes, set, vset, state);
1810 ipa_write_optimization_summaries_1 (all_lto_gen_passes, set, vset, state);
e792884f
JH
1811
1812 gcc_assert (lto_get_out_decl_state () == state);
1813 lto_pop_out_decl_state ();
1814 lto_delete_out_decl_state (state);
d7f09764
DN
1815}
1816
1817/* Same as execute_pass_list but assume that subpasses of IPA passes
1818 are local passes. */
1819
1820static void
1821ipa_read_summaries_1 (struct opt_pass *pass)
1822{
1823 while (pass)
f6db1481 1824 {
d7f09764
DN
1825 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
1826
a5093353
JH
1827 gcc_assert (!current_function_decl);
1828 gcc_assert (!cfun);
17653c00 1829 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
d7f09764
DN
1830
1831 if (pass->gate == NULL || pass->gate ())
17653c00 1832 {
d7f09764 1833 if (pass->type == IPA_PASS && ipa_pass->read_summary)
17653c00 1834 {
d7f09764
DN
1835 /* If a timevar is present, start it. */
1836 if (pass->tv_id)
1837 timevar_push (pass->tv_id);
1838
f59292da
JH
1839 pass_init_dump_file (pass);
1840
d7f09764
DN
1841 ipa_pass->read_summary ();
1842
f59292da
JH
1843 pass_fini_dump_file (pass);
1844
d7f09764
DN
1845 /* Stop timevar. */
1846 if (pass->tv_id)
1847 timevar_pop (pass->tv_id);
17653c00 1848 }
d7f09764
DN
1849
1850 if (pass->sub && pass->sub->type != GIMPLE_PASS)
1851 ipa_read_summaries_1 (pass->sub);
17653c00 1852 }
d7f09764
DN
1853 pass = pass->next;
1854 }
1855}
1856
1857
1858/* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
1859
1860void
1861ipa_read_summaries (void)
1862{
e792884f 1863 ipa_read_summaries_1 (all_regular_ipa_passes);
d7f09764
DN
1864 ipa_read_summaries_1 (all_lto_gen_passes);
1865}
1866
e792884f
JH
1867/* Same as execute_pass_list but assume that subpasses of IPA passes
1868 are local passes. */
1869
1870static void
1871ipa_read_optimization_summaries_1 (struct opt_pass *pass)
1872{
1873 while (pass)
1874 {
1875 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
1876
1877 gcc_assert (!current_function_decl);
1878 gcc_assert (!cfun);
1879 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
1880
1881 if (pass->gate == NULL || pass->gate ())
1882 {
1883 if (pass->type == IPA_PASS && ipa_pass->read_optimization_summary)
1884 {
1885 /* If a timevar is present, start it. */
1886 if (pass->tv_id)
1887 timevar_push (pass->tv_id);
1888
f59292da
JH
1889 pass_init_dump_file (pass);
1890
e792884f
JH
1891 ipa_pass->read_optimization_summary ();
1892
f59292da
JH
1893 pass_fini_dump_file (pass);
1894
e792884f
JH
1895 /* Stop timevar. */
1896 if (pass->tv_id)
1897 timevar_pop (pass->tv_id);
1898 }
1899
1900 if (pass->sub && pass->sub->type != GIMPLE_PASS)
1901 ipa_read_optimization_summaries_1 (pass->sub);
1902 }
1903 pass = pass->next;
1904 }
1905}
1906
1907/* Read all the summaries for all_regular_ipa_passes and all_lto_gen_passes. */
1908
1909void
1910ipa_read_optimization_summaries (void)
1911{
1912 ipa_read_optimization_summaries_1 (all_regular_ipa_passes);
1913 ipa_read_optimization_summaries_1 (all_lto_gen_passes);
1914}
1915
d7f09764
DN
1916/* Same as execute_pass_list but assume that subpasses of IPA passes
1917 are local passes. */
1918void
1919execute_ipa_pass_list (struct opt_pass *pass)
1920{
1921 do
1922 {
1923 gcc_assert (!current_function_decl);
1924 gcc_assert (!cfun);
1925 gcc_assert (pass->type == SIMPLE_IPA_PASS || pass->type == IPA_PASS);
ef330312 1926 if (execute_one_pass (pass) && pass->sub)
9e016eba
JH
1927 {
1928 if (pass->sub->type == GIMPLE_PASS)
090fa0ab
GF
1929 {
1930 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_START, NULL);
1931 do_per_function_toporder ((void (*)(void *))execute_pass_list,
1932 pass->sub);
1933 invoke_plugin_callbacks (PLUGIN_EARLY_GIMPLE_PASSES_END, NULL);
1934 }
17653c00
JH
1935 else if (pass->sub->type == SIMPLE_IPA_PASS
1936 || pass->sub->type == IPA_PASS)
9e016eba
JH
1937 execute_ipa_pass_list (pass->sub);
1938 else
1939 gcc_unreachable ();
1940 }
d7f09764
DN
1941 gcc_assert (!current_function_decl);
1942 cgraph_process_new_functions ();
ef330312 1943 pass = pass->next;
f6db1481 1944 }
ef330312 1945 while (pass);
97b0ade3 1946}
9fe0cb7d 1947
2c5721d9
MJ
1948/* Execute stmt fixup hooks of all passes in PASS for NODE and STMTS. */
1949
1950static void
1951execute_ipa_stmt_fixups (struct opt_pass *pass,
1952 struct cgraph_node *node, gimple *stmts)
1953{
1954 while (pass)
1955 {
1956 /* Execute all of the IPA_PASSes in the list. */
1957 if (pass->type == IPA_PASS
1958 && (!pass->gate || pass->gate ()))
1959 {
1960 struct ipa_opt_pass_d *ipa_pass = (struct ipa_opt_pass_d *) pass;
1961
1962 if (ipa_pass->stmt_fixup)
1963 {
1964 pass_init_dump_file (pass);
1965 /* If a timevar is present, start it. */
1966 if (pass->tv_id)
1967 timevar_push (pass->tv_id);
1968
1969 ipa_pass->stmt_fixup (node, stmts);
1970
1971 /* Stop timevar. */
1972 if (pass->tv_id)
1973 timevar_pop (pass->tv_id);
1974 pass_fini_dump_file (pass);
1975 }
1976 if (pass->sub)
1977 execute_ipa_stmt_fixups (pass->sub, node, stmts);
1978 }
1979 pass = pass->next;
1980 }
1981}
1982
1983/* Execute stmt fixup hooks of all IPA passes for NODE and STMTS. */
1984
1985void
1986execute_all_ipa_stmt_fixups (struct cgraph_node *node, gimple *stmts)
1987{
1988 execute_ipa_stmt_fixups (all_regular_ipa_passes, node, stmts);
1989}
1990
1991
d7f09764
DN
1992extern void debug_properties (unsigned int);
1993extern void dump_properties (FILE *, unsigned int);
1994
24e47c76 1995DEBUG_FUNCTION void
d7f09764
DN
1996dump_properties (FILE *dump, unsigned int props)
1997{
1998 fprintf (dump, "Properties:\n");
1999 if (props & PROP_gimple_any)
2000 fprintf (dump, "PROP_gimple_any\n");
2001 if (props & PROP_gimple_lcf)
2002 fprintf (dump, "PROP_gimple_lcf\n");
2003 if (props & PROP_gimple_leh)
2004 fprintf (dump, "PROP_gimple_leh\n");
2005 if (props & PROP_cfg)
2006 fprintf (dump, "PROP_cfg\n");
2007 if (props & PROP_referenced_vars)
2008 fprintf (dump, "PROP_referenced_vars\n");
2009 if (props & PROP_ssa)
2010 fprintf (dump, "PROP_ssa\n");
2011 if (props & PROP_no_crit_edges)
2012 fprintf (dump, "PROP_no_crit_edges\n");
2013 if (props & PROP_rtl)
2014 fprintf (dump, "PROP_rtl\n");
2015 if (props & PROP_gimple_lomp)
2016 fprintf (dump, "PROP_gimple_lomp\n");
688a482d
RG
2017 if (props & PROP_gimple_lcx)
2018 fprintf (dump, "PROP_gimple_lcx\n");
24e47c76
JH
2019 if (props & PROP_cfglayout)
2020 fprintf (dump, "PROP_cfglayout\n");
d7f09764
DN
2021}
2022
24e47c76 2023DEBUG_FUNCTION void
d7f09764
DN
2024debug_properties (unsigned int props)
2025{
2026 dump_properties (stderr, props);
2027}
2028
33977f81
JH
2029/* Called by local passes to see if function is called by already processed nodes.
2030 Because we process nodes in topological order, this means that function is
2031 in recursive cycle or we introduced new direct calls. */
2032bool
2033function_called_by_processed_nodes_p (void)
2034{
2035 struct cgraph_edge *e;
2036 for (e = cgraph_node (current_function_decl)->callers; e; e = e->next_caller)
2037 {
2038 if (e->caller->decl == current_function_decl)
2039 continue;
b20996ff 2040 if (!e->caller->analyzed)
33977f81
JH
2041 continue;
2042 if (TREE_ASM_WRITTEN (e->caller->decl))
2043 continue;
2044 if (!e->caller->process && !e->caller->global.inlined_to)
2045 break;
2046 }
2047 if (dump_file && e)
2048 {
2049 fprintf (dump_file, "Already processed call to:\n");
2050 dump_cgraph_node (dump_file, e->caller);
2051 }
2052 return e != NULL;
2053}
2054
873aa8f5 2055#include "gt-passes.h"
This page took 2.735654 seconds and 5 git commands to generate.