]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-into-ssa.c
2015-06-04 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / tree-into-ssa.c
CommitLineData
4ee9c684 1/* Rewrite a program in Normal form into SSA.
d353bf18 2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
4ee9c684 3 Contributed by Diego Novillo <dnovillo@redhat.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
8c4c00c1 9the Free Software Foundation; either version 3, or (at your option)
4ee9c684 10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
8c4c00c1 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
4ee9c684 20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
b20a8bb4 25#include "hash-set.h"
b20a8bb4 26#include "vec.h"
b20a8bb4 27#include "input.h"
28#include "alias.h"
29#include "symtab.h"
b20a8bb4 30#include "inchash.h"
4ee9c684 31#include "tree.h"
b20a8bb4 32#include "fold-const.h"
4ee9c684 33#include "flags.h"
4ee9c684 34#include "tm_p.h"
35#include "langhooks.h"
94ea8568 36#include "predict.h"
a3020f2f 37#include "hard-reg-set.h"
4ee9c684 38#include "function.h"
94ea8568 39#include "dominance.h"
40#include "cfg.h"
41#include "cfganal.h"
42#include "basic-block.h"
ce084dfc 43#include "gimple-pretty-print.h"
bc61cadb 44#include "hash-table.h"
45#include "tree-ssa-alias.h"
46#include "internal-fn.h"
47#include "gimple-expr.h"
48#include "is-a.h"
75a70cf9 49#include "gimple.h"
dcf1a1ec 50#include "gimple-iterator.h"
073c1fd5 51#include "gimple-ssa.h"
52#include "tree-cfg.h"
53#include "tree-phinodes.h"
54#include "ssa-iterators.h"
9ed99284 55#include "stringpool.h"
073c1fd5 56#include "tree-ssanames.h"
57#include "tree-into-ssa.h"
d53441c8 58#include "hashtab.h"
59#include "rtl.h"
60#include "statistics.h"
d53441c8 61#include "insn-config.h"
62#include "expmed.h"
63#include "dojump.h"
64#include "explow.h"
65#include "calls.h"
66#include "emit-rtl.h"
67#include "varasm.h"
68#include "stmt.h"
9ed99284 69#include "expr.h"
073c1fd5 70#include "tree-dfa.h"
71#include "tree-ssa.h"
4ee9c684 72#include "tree-inline.h"
4ee9c684 73#include "tree-pass.h"
74#include "cfgloop.h"
75#include "domwalk.h"
095dcfa3 76#include "params.h"
b4fa1427 77#include "diagnostic-core.h"
4ee9c684 78
e797f49f 79#define PERCENT(x,y) ((float)(x) * 100.0 / (float)(y))
75a70cf9 80
4ee9c684 81/* This file builds the SSA form for a function as described in:
82 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
83 Computing Static Single Assignment Form and the Control Dependence
84 Graph. ACM Transactions on Programming Languages and Systems,
85 13(4):451-490, October 1991. */
86
4ee9c684 87/* Structure to map a variable VAR to the set of blocks that contain
88 definitions for VAR. */
89struct def_blocks_d
90{
4ee9c684 91 /* Blocks that contain definitions of VAR. Bit I will be set if the
92 Ith block contains a definition of VAR. */
93 bitmap def_blocks;
94
e551bd1a 95 /* Blocks that contain a PHI node for VAR. */
f3d9a16c 96 bitmap phi_blocks;
97
4ee9c684 98 /* Blocks where VAR is live-on-entry. Similar semantics as
99 DEF_BLOCKS. */
100 bitmap livein_blocks;
101};
102
c86a4051 103typedef struct def_blocks_d *def_blocks_p;
104
4ee9c684 105
dd2d357d 106/* Stack of trees used to restore the global currdefs to its original
88dbf20f 107 state after completing rewriting of a block and its dominator
108 children. Its elements have the following properties:
dd2d357d 109
4fb5e5ca 110 - An SSA_NAME (N) indicates that the current definition of the
111 underlying variable should be set to the given SSA_NAME. If the
112 symbol associated with the SSA_NAME is not a GIMPLE register, the
113 next slot in the stack must be a _DECL node (SYM). In this case,
114 the name N in the previous slot is the current reaching
115 definition for SYM.
dd2d357d 116
88dbf20f 117 - A _DECL node indicates that the underlying variable has no
118 current definition.
e551bd1a 119
4fb5e5ca 120 - A NULL node at the top entry is used to mark the last slot
88dbf20f 121 associated with the current block. */
f1f41a6c 122static vec<tree> block_defs_stack;
486b57c7 123
75a70cf9 124
88dbf20f 125/* Set of existing SSA names being replaced by update_ssa. */
126static sbitmap old_ssa_names;
127
128/* Set of new SSA names being added by update_ssa. Note that both
129 NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
130 the operations done on them are presence tests. */
131static sbitmap new_ssa_names;
132
47c1894f 133static sbitmap interesting_blocks;
75a70cf9 134
88dbf20f 135/* Set of SSA names that have been marked to be released after they
136 were registered in the replacement table. They will be finally
137 released after we finish updating the SSA web. */
138static bitmap names_to_release;
139
f1f41a6c 140/* vec of vec of PHIs to rewrite in a basic block. Element I corresponds
2670407e 141 the to basic block with index I. Allocated once per compilation, *not*
142 released between different functions. */
1a91d914 143static vec< vec<gphi *> > phis_to_rewrite;
70630fcd 144
145/* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
70630fcd 146static bitmap blocks_with_phis_to_rewrite;
147
88dbf20f 148/* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
0dc262e0 149 to grow as the callers to create_new_def_for will create new names on
150 the fly.
151 FIXME. Currently set to 1/3 to avoid frequent reallocations but still
152 need to find a reasonable growth strategy. */
88dbf20f 153#define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
154
88dbf20f 155
dd277d48 156/* The function the SSA updating data structures have been initialized for.
0dc262e0 157 NULL if they need to be initialized by create_new_def_for. */
dd277d48 158static struct function *update_ssa_initialized_fn = NULL;
88dbf20f 159
4ee9c684 160/* Global data to attach to the main dominator walk structure. */
161struct mark_def_sites_global_data
162{
88dbf20f 163 /* This bitmap contains the variables which are set before they
164 are used in a basic block. */
ecddc799 165 bitmap kills;
4ee9c684 166};
167
8f6fa493 168/* It is advantageous to avoid things like life analysis for variables which
169 do not need PHI nodes. This enum describes whether or not a particular
170 variable may need a PHI node. */
171
172enum need_phi_state {
173 /* This is the default. If we are still in this state after finding
174 all the definition and use sites, then we will assume the variable
175 needs PHI nodes. This is probably an overly conservative assumption. */
176 NEED_PHI_STATE_UNKNOWN,
177
178 /* This state indicates that we have seen one or more sets of the
179 variable in a single basic block and that the sets dominate all
180 uses seen so far. If after finding all definition and use sites
181 we are still in this state, then the variable does not need any
182 PHI nodes. */
183 NEED_PHI_STATE_NO,
184
185 /* This state indicates that we have either seen multiple definitions of
186 the variable in multiple blocks, or that we encountered a use in a
187 block that was not dominated by the block containing the set(s) of
188 this variable. This variable is assumed to need PHI nodes. */
189 NEED_PHI_STATE_MAYBE
190};
191
74105c0d 192/* Information stored for both SSA names and decls. */
193struct common_info_d
22d6c74e 194{
22d6c74e 195 /* This field indicates whether or not the variable may need PHI nodes.
196 See the enum's definition for more detailed information about the
197 states. */
198 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
199
74105c0d 200 /* The current reaching definition replacing this var. */
22d6c74e 201 tree current_def;
202
74105c0d 203 /* Definitions for this var. */
22d6c74e 204 struct def_blocks_d def_blocks;
205};
206
74105c0d 207/* The information associated with decls and SSA names. */
208typedef struct common_info_d *common_info_p;
209
210/* Information stored for decls. */
211struct var_info_d
212{
213 /* The variable. */
214 tree var;
215
216 /* Information stored for both SSA names and decls. */
217 struct common_info_d info;
218};
219
22d6c74e 220/* The information associated with decls. */
221typedef struct var_info_d *var_info_p;
222
22d6c74e 223
3e871d4d 224/* VAR_INFOS hashtable helpers. */
225
226struct var_info_hasher : typed_free_remove <var_info_d>
227{
2933f7af 228 typedef var_info_d *value_type;
229 typedef var_info_d *compare_type;
2933f7af 230 static inline hashval_t hash (const value_type &);
231 static inline bool equal (const value_type &, const compare_type &);
3e871d4d 232};
233
234inline hashval_t
2933f7af 235var_info_hasher::hash (const value_type &p)
3e871d4d 236{
237 return DECL_UID (p->var);
238}
239
240inline bool
2933f7af 241var_info_hasher::equal (const value_type &p1, const compare_type &p2)
3e871d4d 242{
243 return p1->var == p2->var;
244}
245
246
22d6c74e 247/* Each entry in VAR_INFOS contains an element of type STRUCT
248 VAR_INFO_D. */
c1f445d2 249static hash_table<var_info_hasher> *var_infos;
22d6c74e 250
251
88dbf20f 252/* Information stored for SSA names. */
f3d9a16c 253struct ssa_name_info
254{
5eeafc2d 255 /* Age of this record (so that info_for_ssa_name table can be cleared
256 quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
257 are assumed to be null. */
258 unsigned age;
2920559d 259
5eeafc2d 260 /* Replacement mappings, allocated from update_ssa_obstack. */
261 bitmap repl_set;
22d6c74e 262
74105c0d 263 /* Information stored for both SSA names and decls. */
264 struct common_info_d info;
f3d9a16c 265};
4ee9c684 266
2920559d 267/* The information associated with names. */
268typedef struct ssa_name_info *ssa_name_info_p;
2920559d 269
f1f41a6c 270static vec<ssa_name_info_p> info_for_ssa_name;
2920559d 271static unsigned current_info_for_ssa_name_age;
272
5eeafc2d 273static bitmap_obstack update_ssa_obstack;
274
2920559d 275/* The set of blocks affected by update_ssa. */
2920559d 276static bitmap blocks_to_update;
4ee9c684 277
88dbf20f 278/* The main entry point to the SSA renamer (rewrite_blocks) may be
279 called several times to do different, but related, tasks.
280 Initially, we need it to rename the whole program into SSA form.
281 At other times, we may need it to only rename into SSA newly
282 exposed symbols. Finally, we can also call it to incrementally fix
283 an already built SSA web. */
284enum rewrite_mode {
285 /* Convert the whole function into SSA form. */
286 REWRITE_ALL,
287
288 /* Incrementally update the SSA web by replacing existing SSA
289 names with new ones. See update_ssa for details. */
290 REWRITE_UPDATE
291};
292
e70e8b13 293/* The set of symbols we ought to re-write into SSA form in update_ssa. */
294static bitmap symbols_to_rename_set;
f1f41a6c 295static vec<tree> symbols_to_rename;
e70e8b13 296
297/* Mark SYM for renaming. */
298
299static void
300mark_for_renaming (tree sym)
301{
302 if (!symbols_to_rename_set)
303 symbols_to_rename_set = BITMAP_ALLOC (NULL);
304 if (bitmap_set_bit (symbols_to_rename_set, DECL_UID (sym)))
f1f41a6c 305 symbols_to_rename.safe_push (sym);
e70e8b13 306}
307
308/* Return true if SYM is marked for renaming. */
309
310static bool
311marked_for_renaming (tree sym)
312{
ec11736b 313 if (!symbols_to_rename_set || sym == NULL_TREE)
e70e8b13 314 return false;
315 return bitmap_bit_p (symbols_to_rename_set, DECL_UID (sym));
316}
317
318
75a70cf9 319/* Return true if STMT needs to be rewritten. When renaming a subset
320 of the variables, not all statements will be processed. This is
321 decided in mark_def_sites. */
322
323static inline bool
324rewrite_uses_p (gimple stmt)
325{
326 return gimple_visited_p (stmt);
327}
328
329
330/* Set the rewrite marker on STMT to the value given by REWRITE_P. */
331
332static inline void
333set_rewrite_uses (gimple stmt, bool rewrite_p)
334{
335 gimple_set_visited (stmt, rewrite_p);
336}
337
338
339/* Return true if the DEFs created by statement STMT should be
340 registered when marking new definition sites. This is slightly
341 different than rewrite_uses_p: it's used by update_ssa to
342 distinguish statements that need to have both uses and defs
343 processed from those that only need to have their defs processed.
344 Statements that define new SSA names only need to have their defs
345 registered, but they don't need to have their uses renamed. */
346
347static inline bool
348register_defs_p (gimple stmt)
349{
350 return gimple_plf (stmt, GF_PLF_1) != 0;
351}
352
353
354/* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */
355
356static inline void
357set_register_defs (gimple stmt, bool register_defs_p)
358{
359 gimple_set_plf (stmt, GF_PLF_1, register_defs_p);
360}
361
362
f3d9a16c 363/* Get the information associated with NAME. */
364
4fb5e5ca 365static inline ssa_name_info_p
f3d9a16c 366get_ssa_name_ann (tree name)
367{
2920559d 368 unsigned ver = SSA_NAME_VERSION (name);
f1f41a6c 369 unsigned len = info_for_ssa_name.length ();
2920559d 370 struct ssa_name_info *info;
371
83b709f2 372 /* Re-allocate the vector at most once per update/into-SSA. */
2920559d 373 if (ver >= len)
f1f41a6c 374 info_for_ssa_name.safe_grow_cleared (num_ssa_names);
83b709f2 375
376 /* But allocate infos lazily. */
f1f41a6c 377 info = info_for_ssa_name[ver];
83b709f2 378 if (!info)
2920559d 379 {
83b709f2 380 info = XCNEW (struct ssa_name_info);
381 info->age = current_info_for_ssa_name_age;
382 info->info.need_phi_state = NEED_PHI_STATE_UNKNOWN;
f1f41a6c 383 info_for_ssa_name[ver] = info;
2920559d 384 }
385
2920559d 386 if (info->age < current_info_for_ssa_name_age)
387 {
2920559d 388 info->age = current_info_for_ssa_name_age;
74105c0d 389 info->repl_set = NULL;
390 info->info.need_phi_state = NEED_PHI_STATE_UNKNOWN;
391 info->info.current_def = NULL_TREE;
392 info->info.def_blocks.def_blocks = NULL;
393 info->info.def_blocks.phi_blocks = NULL;
394 info->info.def_blocks.livein_blocks = NULL;
2920559d 395 }
f3d9a16c 396
2920559d 397 return info;
f3d9a16c 398}
399
22d6c74e 400/* Return and allocate the auxiliar information for DECL. */
401
402static inline var_info_p
403get_var_info (tree decl)
404{
405 struct var_info_d vi;
3e871d4d 406 var_info_d **slot;
22d6c74e 407 vi.var = decl;
c1f445d2 408 slot = var_infos->find_slot_with_hash (&vi, DECL_UID (decl), INSERT);
22d6c74e 409 if (*slot == NULL)
410 {
411 var_info_p v = XCNEW (struct var_info_d);
412 v->var = decl;
3e871d4d 413 *slot = v;
22d6c74e 414 return v;
415 }
3e871d4d 416 return *slot;
22d6c74e 417}
418
4fb5e5ca 419
420/* Clears info for SSA names. */
2920559d 421
422static void
423clear_ssa_name_info (void)
424{
425 current_info_for_ssa_name_age++;
5eeafc2d 426
427 /* If current_info_for_ssa_name_age wraps we use stale information.
428 Asser that this does not happen. */
429 gcc_assert (current_info_for_ssa_name_age != 0);
2920559d 430}
e551bd1a 431
4fb5e5ca 432
74105c0d 433/* Get access to the auxiliar information stored per SSA name or decl. */
f3d9a16c 434
74105c0d 435static inline common_info_p
436get_common_info (tree var)
f3d9a16c 437{
438 if (TREE_CODE (var) == SSA_NAME)
74105c0d 439 return &get_ssa_name_ann (var)->info;
f3d9a16c 440 else
74105c0d 441 return &get_var_info (var)->info;
f3d9a16c 442}
443
e551bd1a 444
f3d9a16c 445/* Return the current definition for VAR. */
446
095dcfa3 447tree
f3d9a16c 448get_current_def (tree var)
449{
74105c0d 450 return get_common_info (var)->current_def;
f3d9a16c 451}
452
e551bd1a 453
f3d9a16c 454/* Sets current definition of VAR to DEF. */
455
095dcfa3 456void
f3d9a16c 457set_current_def (tree var, tree def)
458{
74105c0d 459 get_common_info (var)->current_def = def;
f3d9a16c 460}
461
2920559d 462/* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
463 all statements in basic block BB. */
464
465static void
466initialize_flags_in_bb (basic_block bb)
467{
75a70cf9 468 gimple stmt;
469 gimple_stmt_iterator gsi;
2920559d 470
75a70cf9 471 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2920559d 472 {
75a70cf9 473 gimple phi = gsi_stmt (gsi);
474 set_rewrite_uses (phi, false);
475 set_register_defs (phi, false);
2920559d 476 }
477
75a70cf9 478 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2920559d 479 {
75a70cf9 480 stmt = gsi_stmt (gsi);
481
2920559d 482 /* We are going to use the operand cache API, such as
483 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
484 cache for each statement should be up-to-date. */
88536563 485 gcc_checking_assert (!gimple_modified_p (stmt));
75a70cf9 486 set_rewrite_uses (stmt, false);
487 set_register_defs (stmt, false);
2920559d 488 }
489}
490
491/* Mark block BB as interesting for update_ssa. */
492
493static void
494mark_block_for_update (basic_block bb)
495{
88536563 496 gcc_checking_assert (blocks_to_update != NULL);
39192870 497 if (!bitmap_set_bit (blocks_to_update, bb->index))
2920559d 498 return;
2920559d 499 initialize_flags_in_bb (bb);
500}
501
e551bd1a 502/* Return the set of blocks where variable VAR is defined and the blocks
503 where VAR is live on entry (livein). If no entry is found in
504 DEF_BLOCKS, a new one is created and returned. */
4ee9c684 505
e551bd1a 506static inline struct def_blocks_d *
74105c0d 507get_def_blocks_for (common_info_p info)
4ee9c684 508{
74105c0d 509 struct def_blocks_d *db_p = &info->def_blocks;
22d6c74e 510 if (!db_p->def_blocks)
4ee9c684 511 {
22d6c74e 512 db_p->def_blocks = BITMAP_ALLOC (&update_ssa_obstack);
513 db_p->phi_blocks = BITMAP_ALLOC (&update_ssa_obstack);
514 db_p->livein_blocks = BITMAP_ALLOC (&update_ssa_obstack);
2cf24776 515 }
516
e551bd1a 517 return db_p;
4ee9c684 518}
519
6cf80e5b 520
f3d9a16c 521/* Mark block BB as the definition site for variable VAR. PHI_P is true if
88dbf20f 522 VAR is defined by a PHI node. */
4ee9c684 523
524static void
88dbf20f 525set_def_block (tree var, basic_block bb, bool phi_p)
4ee9c684 526{
527 struct def_blocks_d *db_p;
74105c0d 528 common_info_p info;
ce898113 529
74105c0d 530 info = get_common_info (var);
531 db_p = get_def_blocks_for (info);
4ee9c684 532
533 /* Set the bit corresponding to the block where VAR is defined. */
534 bitmap_set_bit (db_p->def_blocks, bb->index);
f3d9a16c 535 if (phi_p)
536 bitmap_set_bit (db_p->phi_blocks, bb->index);
4ee9c684 537
e551bd1a 538 /* Keep track of whether or not we may need to insert PHI nodes.
4ee9c684 539
540 If we are in the UNKNOWN state, then this is the first definition
541 of VAR. Additionally, we have not seen any uses of VAR yet, so
e551bd1a 542 we do not need a PHI node for this variable at this time (i.e.,
4ee9c684 543 transition to NEED_PHI_STATE_NO).
544
545 If we are in any other state, then we either have multiple definitions
546 of this variable occurring in different blocks or we saw a use of the
547 variable which was not dominated by the block containing the
548 definition(s). In this case we may need a PHI node, so enter
549 state NEED_PHI_STATE_MAYBE. */
74105c0d 550 if (info->need_phi_state == NEED_PHI_STATE_UNKNOWN)
551 info->need_phi_state = NEED_PHI_STATE_NO;
4ee9c684 552 else
74105c0d 553 info->need_phi_state = NEED_PHI_STATE_MAYBE;
4ee9c684 554}
555
556
557/* Mark block BB as having VAR live at the entry to BB. */
558
559static void
560set_livein_block (tree var, basic_block bb)
561{
74105c0d 562 common_info_p info;
4ee9c684 563 struct def_blocks_d *db_p;
4ee9c684 564
74105c0d 565 info = get_common_info (var);
566 db_p = get_def_blocks_for (info);
4ee9c684 567
568 /* Set the bit corresponding to the block where VAR is live in. */
569 bitmap_set_bit (db_p->livein_blocks, bb->index);
570
e551bd1a 571 /* Keep track of whether or not we may need to insert PHI nodes.
4ee9c684 572
573 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
574 by the single block containing the definition(s) of this variable. If
575 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
576 NEED_PHI_STATE_MAYBE. */
74105c0d 577 if (info->need_phi_state == NEED_PHI_STATE_NO)
4ee9c684 578 {
579 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
580
581 if (def_block_index == -1
582 || ! dominated_by_p (CDI_DOMINATORS, bb,
f5a6b05f 583 BASIC_BLOCK_FOR_FN (cfun, def_block_index)))
74105c0d 584 info->need_phi_state = NEED_PHI_STATE_MAYBE;
4ee9c684 585 }
586 else
74105c0d 587 info->need_phi_state = NEED_PHI_STATE_MAYBE;
4ee9c684 588}
589
590
88dbf20f 591/* Return true if NAME is in OLD_SSA_NAMES. */
4ee9c684 592
88dbf20f 593static inline bool
594is_old_name (tree name)
595{
095dcfa3 596 unsigned ver = SSA_NAME_VERSION (name);
100be96d 597 if (!old_ssa_names)
dd277d48 598 return false;
100be96d 599 return (ver < SBITMAP_SIZE (old_ssa_names)
08b7917c 600 && bitmap_bit_p (old_ssa_names, ver));
88dbf20f 601}
602
603
604/* Return true if NAME is in NEW_SSA_NAMES. */
ce898113 605
88dbf20f 606static inline bool
607is_new_name (tree name)
608{
095dcfa3 609 unsigned ver = SSA_NAME_VERSION (name);
dd277d48 610 if (!new_ssa_names)
611 return false;
156093aa 612 return (ver < SBITMAP_SIZE (new_ssa_names)
08b7917c 613 && bitmap_bit_p (new_ssa_names, ver));
4ee9c684 614}
615
e551bd1a 616
f4e36c33 617/* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */
88dbf20f 618
619static inline bitmap
f4e36c33 620names_replaced_by (tree new_tree)
88dbf20f 621{
5eeafc2d 622 return get_ssa_name_ann (new_tree)->repl_set;
88dbf20f 623}
624
625
f4e36c33 626/* Add OLD to REPL_TBL[NEW_TREE].SET. */
88dbf20f 627
628static inline void
f4e36c33 629add_to_repl_tbl (tree new_tree, tree old)
88dbf20f 630{
5eeafc2d 631 bitmap *set = &get_ssa_name_ann (new_tree)->repl_set;
632 if (!*set)
633 *set = BITMAP_ALLOC (&update_ssa_obstack);
634 bitmap_set_bit (*set, SSA_NAME_VERSION (old));
88dbf20f 635}
636
637
f4e36c33 638/* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL
88dbf20f 639 represents the set of names O_1 ... O_j replaced by N_i. This is
640 used by update_ssa and its helpers to introduce new SSA names in an
641 already formed SSA web. */
642
643static void
f4e36c33 644add_new_name_mapping (tree new_tree, tree old)
88dbf20f 645{
f4e36c33 646 /* OLD and NEW_TREE must be different SSA names for the same symbol. */
88536563 647 gcc_checking_assert (new_tree != old
648 && SSA_NAME_VAR (new_tree) == SSA_NAME_VAR (old));
095dcfa3 649
4fb5e5ca 650 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
651 caller may have created new names since the set was created. */
156093aa 652 if (SBITMAP_SIZE (new_ssa_names) <= num_ssa_names - 1)
4fb5e5ca 653 {
654 unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
655 new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
656 old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
88dbf20f 657 }
658
88dbf20f 659 /* Update the REPL_TBL table. */
f4e36c33 660 add_to_repl_tbl (new_tree, old);
56004dc5 661
88dbf20f 662 /* If OLD had already been registered as a new name, then all the
f4e36c33 663 names that OLD replaces should also be replaced by NEW_TREE. */
88dbf20f 664 if (is_new_name (old))
f4e36c33 665 bitmap_ior_into (names_replaced_by (new_tree), names_replaced_by (old));
88dbf20f 666
f4e36c33 667 /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
88dbf20f 668 respectively. */
08b7917c 669 bitmap_set_bit (new_ssa_names, SSA_NAME_VERSION (new_tree));
670 bitmap_set_bit (old_ssa_names, SSA_NAME_VERSION (old));
56004dc5 671}
4ee9c684 672
4ee9c684 673
e551bd1a 674/* Call back for walk_dominator_tree used to collect definition sites
675 for every variable in the function. For every statement S in block
676 BB:
4ee9c684 677
b66731e8 678 1- Variables defined by S in the DEFS of S are marked in the bitmap
6bf320fb 679 KILLS.
4ee9c684 680
e551bd1a 681 2- If S uses a variable VAR and there is no preceding kill of VAR,
5cdd0289 682 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
4ee9c684 683
e551bd1a 684 This information is used to determine which variables are live
685 across block boundaries to reduce the number of PHI nodes
686 we create. */
4ee9c684 687
e551bd1a 688static void
6bf320fb 689mark_def_sites (basic_block bb, gimple stmt, bitmap kills)
e551bd1a 690{
75a70cf9 691 tree def;
e551bd1a 692 use_operand_p use_p;
e551bd1a 693 ssa_op_iter iter;
694
75a70cf9 695 /* Since this is the first time that we rewrite the program into SSA
696 form, force an operand scan on every statement. */
75a70cf9 697 update_stmt (stmt);
e551bd1a 698
88536563 699 gcc_checking_assert (blocks_to_update == NULL);
75a70cf9 700 set_register_defs (stmt, false);
701 set_rewrite_uses (stmt, false);
e551bd1a 702
9845d120 703 if (is_gimple_debug (stmt))
841424cc 704 {
705 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
706 {
707 tree sym = USE_FROM_PTR (use_p);
88536563 708 gcc_checking_assert (DECL_P (sym));
841424cc 709 set_rewrite_uses (stmt, true);
710 }
711 if (rewrite_uses_p (stmt))
08b7917c 712 bitmap_set_bit (interesting_blocks, bb->index);
841424cc 713 return;
714 }
9845d120 715
e551bd1a 716 /* If a variable is used before being set, then the variable is live
717 across a block boundary, so mark it live-on-entry to BB. */
5aabc5f2 718 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
e551bd1a 719 {
88dbf20f 720 tree sym = USE_FROM_PTR (use_p);
88536563 721 gcc_checking_assert (DECL_P (sym));
a55dc2cd 722 if (!bitmap_bit_p (kills, DECL_UID (sym)))
88dbf20f 723 set_livein_block (sym, bb);
75a70cf9 724 set_rewrite_uses (stmt, true);
e551bd1a 725 }
48e1416a 726
4fb5e5ca 727 /* Now process the defs. Mark BB as the definition block and add
728 each def to the set of killed symbols. */
5aabc5f2 729 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
e551bd1a 730 {
88536563 731 gcc_checking_assert (DECL_P (def));
88dbf20f 732 set_def_block (def, bb, false);
a55dc2cd 733 bitmap_set_bit (kills, DECL_UID (def));
75a70cf9 734 set_register_defs (stmt, true);
e551bd1a 735 }
88dbf20f 736
737 /* If we found the statement interesting then also mark the block BB
738 as interesting. */
75a70cf9 739 if (rewrite_uses_p (stmt) || register_defs_p (stmt))
08b7917c 740 bitmap_set_bit (interesting_blocks, bb->index);
e551bd1a 741}
742
f50cf978 743/* Structure used by prune_unused_phi_nodes to record bounds of the intervals
744 in the dfs numbering of the dominance tree. */
745
746struct dom_dfsnum
747{
748 /* Basic block whose index this entry corresponds to. */
749 unsigned bb_index;
750
751 /* The dfs number of this node. */
752 unsigned dfs_num;
753};
754
755/* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
756 for qsort. */
757
758static int
759cmp_dfsnum (const void *a, const void *b)
760{
45ba1503 761 const struct dom_dfsnum *const da = (const struct dom_dfsnum *) a;
762 const struct dom_dfsnum *const db = (const struct dom_dfsnum *) b;
f50cf978 763
764 return (int) da->dfs_num - (int) db->dfs_num;
765}
766
767/* Among the intervals starting at the N points specified in DEFS, find
768 the one that contains S, and return its bb_index. */
769
770static unsigned
771find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
772{
773 unsigned f = 0, t = n, m;
774
775 while (t > f + 1)
776 {
777 m = (f + t) / 2;
778 if (defs[m].dfs_num <= s)
779 f = m;
780 else
781 t = m;
782 }
783
784 return defs[f].bb_index;
785}
786
787/* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
788 KILLS is a bitmap of blocks where the value is defined before any use. */
789
790static void
791prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
792{
f50cf978 793 bitmap_iterator bi;
794 unsigned i, b, p, u, top;
795 bitmap live_phis;
796 basic_block def_bb, use_bb;
797 edge e;
798 edge_iterator ei;
799 bitmap to_remove;
800 struct dom_dfsnum *defs;
801 unsigned n_defs, adef;
802
803 if (bitmap_empty_p (uses))
804 {
805 bitmap_clear (phis);
806 return;
807 }
808
809 /* The phi must dominate a use, or an argument of a live phi. Also, we
810 do not create any phi nodes in def blocks, unless they are also livein. */
811 to_remove = BITMAP_ALLOC (NULL);
812 bitmap_and_compl (to_remove, kills, uses);
813 bitmap_and_compl_into (phis, to_remove);
814 if (bitmap_empty_p (phis))
815 {
816 BITMAP_FREE (to_remove);
817 return;
818 }
819
820 /* We want to remove the unnecessary phi nodes, but we do not want to compute
821 liveness information, as that may be linear in the size of CFG, and if
822 there are lot of different variables to rewrite, this may lead to quadratic
823 behavior.
824
825 Instead, we basically emulate standard dce. We put all uses to worklist,
826 then for each of them find the nearest def that dominates them. If this
827 def is a phi node, we mark it live, and if it was not live before, we
828 add the predecessors of its basic block to the worklist.
48e1416a 829
f50cf978 830 To quickly locate the nearest def that dominates use, we use dfs numbering
831 of the dominance tree (that is already available in order to speed up
832 queries). For each def, we have the interval given by the dfs number on
833 entry to and on exit from the corresponding subtree in the dominance tree.
834 The nearest dominator for a given use is the smallest of these intervals
835 that contains entry and exit dfs numbers for the basic block with the use.
836 If we store the bounds for all the uses to an array and sort it, we can
837 locate the nearest dominating def in logarithmic time by binary search.*/
838 bitmap_ior (to_remove, kills, phis);
839 n_defs = bitmap_count_bits (to_remove);
840 defs = XNEWVEC (struct dom_dfsnum, 2 * n_defs + 1);
841 defs[0].bb_index = 1;
842 defs[0].dfs_num = 0;
843 adef = 1;
844 EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
845 {
f5a6b05f 846 def_bb = BASIC_BLOCK_FOR_FN (cfun, i);
f50cf978 847 defs[adef].bb_index = i;
848 defs[adef].dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
849 defs[adef + 1].bb_index = i;
850 defs[adef + 1].dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
851 adef += 2;
852 }
853 BITMAP_FREE (to_remove);
854 gcc_assert (adef == 2 * n_defs + 1);
855 qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
856 gcc_assert (defs[0].bb_index == 1);
857
858 /* Now each DEFS entry contains the number of the basic block to that the
859 dfs number corresponds. Change them to the number of basic block that
860 corresponds to the interval following the dfs number. Also, for the
861 dfs_out numbers, increase the dfs number by one (so that it corresponds
862 to the start of the following interval, not to the end of the current
863 one). We use WORKLIST as a stack. */
c2078b80 864 auto_vec<int> worklist (n_defs + 1);
f1f41a6c 865 worklist.quick_push (1);
f50cf978 866 top = 1;
867 n_defs = 1;
868 for (i = 1; i < adef; i++)
869 {
870 b = defs[i].bb_index;
871 if (b == top)
872 {
873 /* This is a closing element. Interval corresponding to the top
874 of the stack after removing it follows. */
f1f41a6c 875 worklist.pop ();
876 top = worklist[worklist.length () - 1];
f50cf978 877 defs[n_defs].bb_index = top;
878 defs[n_defs].dfs_num = defs[i].dfs_num + 1;
879 }
880 else
881 {
882 /* Opening element. Nothing to do, just push it to the stack and move
883 it to the correct position. */
884 defs[n_defs].bb_index = defs[i].bb_index;
885 defs[n_defs].dfs_num = defs[i].dfs_num;
f1f41a6c 886 worklist.quick_push (b);
f50cf978 887 top = b;
888 }
889
890 /* If this interval starts at the same point as the previous one, cancel
891 the previous one. */
892 if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
893 defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
894 else
895 n_defs++;
896 }
f1f41a6c 897 worklist.pop ();
898 gcc_assert (worklist.is_empty ());
f50cf978 899
900 /* Now process the uses. */
901 live_phis = BITMAP_ALLOC (NULL);
902 EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
903 {
f1f41a6c 904 worklist.safe_push (i);
f50cf978 905 }
906
f1f41a6c 907 while (!worklist.is_empty ())
f50cf978 908 {
f1f41a6c 909 b = worklist.pop ();
f50cf978 910 if (b == ENTRY_BLOCK)
911 continue;
912
913 /* If there is a phi node in USE_BB, it is made live. Otherwise,
914 find the def that dominates the immediate dominator of USE_BB
915 (the kill in USE_BB does not dominate the use). */
916 if (bitmap_bit_p (phis, b))
917 p = b;
918 else
919 {
f5a6b05f 920 use_bb = get_immediate_dominator (CDI_DOMINATORS,
921 BASIC_BLOCK_FOR_FN (cfun, b));
f50cf978 922 p = find_dfsnum_interval (defs, n_defs,
923 bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
924 if (!bitmap_bit_p (phis, p))
925 continue;
926 }
927
928 /* If the phi node is already live, there is nothing to do. */
6ef9bbe0 929 if (!bitmap_set_bit (live_phis, p))
f50cf978 930 continue;
931
6ef9bbe0 932 /* Add the new uses to the worklist. */
f5a6b05f 933 def_bb = BASIC_BLOCK_FOR_FN (cfun, p);
f50cf978 934 FOR_EACH_EDGE (e, ei, def_bb->preds)
935 {
936 u = e->src->index;
937 if (bitmap_bit_p (uses, u))
938 continue;
939
94858f65 940 /* In case there is a kill directly in the use block, do not record
941 the use (this is also necessary for correctness, as we assume that
942 uses dominated by a def directly in their block have been filtered
943 out before). */
944 if (bitmap_bit_p (kills, u))
945 continue;
946
f50cf978 947 bitmap_set_bit (uses, u);
f1f41a6c 948 worklist.safe_push (u);
f50cf978 949 }
950 }
951
f50cf978 952 bitmap_copy (phis, live_phis);
953 BITMAP_FREE (live_phis);
954 free (defs);
955}
e551bd1a 956
e551bd1a 957/* Return the set of blocks where variable VAR is defined and the blocks
958 where VAR is live on entry (livein). Return NULL, if no entry is
959 found in DEF_BLOCKS. */
960
961static inline struct def_blocks_d *
962find_def_blocks_for (tree var)
963{
74105c0d 964 def_blocks_p p = &get_common_info (var)->def_blocks;
22d6c74e 965 if (!p->def_blocks)
966 return NULL;
967 return p;
e551bd1a 968}
969
970
70630fcd 971/* Marks phi node PHI in basic block BB for rewrite. */
972
973static void
1a91d914 974mark_phi_for_rewrite (basic_block bb, gphi *phi)
70630fcd 975{
1a91d914 976 vec<gphi *> phis;
2670407e 977 unsigned n, idx = bb->index;
70630fcd 978
75a70cf9 979 if (rewrite_uses_p (phi))
70630fcd 980 return;
4fb5e5ca 981
75a70cf9 982 set_rewrite_uses (phi, true);
70630fcd 983
984 if (!blocks_with_phis_to_rewrite)
985 return;
986
987 bitmap_set_bit (blocks_with_phis_to_rewrite, idx);
2670407e 988
fe672ac0 989 n = (unsigned) last_basic_block_for_fn (cfun) + 1;
f1f41a6c 990 if (phis_to_rewrite.length () < n)
991 phis_to_rewrite.safe_grow_cleared (n);
70630fcd 992
f1f41a6c 993 phis = phis_to_rewrite[idx];
994 phis.reserve (10);
70630fcd 995
f1f41a6c 996 phis.safe_push (phi);
997 phis_to_rewrite[idx] = phis;
70630fcd 998}
999
e551bd1a 1000/* Insert PHI nodes for variable VAR using the iterated dominance
88dbf20f 1001 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
4fb5e5ca 1002 function assumes that the caller is incrementally updating the
1003 existing SSA form, in which case VAR may be an SSA name instead of
1004 a symbol.
88dbf20f 1005
1006 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1007 PHI node for VAR. On exit, only the nodes that received a PHI node
1008 for VAR will be present in PHI_INSERTION_POINTS. */
e551bd1a 1009
1010static void
88dbf20f 1011insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
e551bd1a 1012{
1013 unsigned bb_index;
1014 edge e;
1a91d914 1015 gphi *phi;
e551bd1a 1016 basic_block bb;
1017 bitmap_iterator bi;
88536563 1018 struct def_blocks_d *def_map = find_def_blocks_for (var);
e551bd1a 1019
1020 /* Remove the blocks where we already have PHI nodes for VAR. */
1021 bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
1022
f50cf978 1023 /* Remove obviously useless phi nodes. */
1024 prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
1025 def_map->livein_blocks);
e551bd1a 1026
1027 /* And insert the PHI nodes. */
f50cf978 1028 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
e551bd1a 1029 {
f5a6b05f 1030 bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2920559d 1031 if (update_p)
1032 mark_block_for_update (bb);
e551bd1a 1033
487c79c3 1034 if (dump_file && (dump_flags & TDF_DETAILS))
1035 {
1036 fprintf (dump_file, "creating PHI node in block #%d for ", bb_index);
1037 print_generic_expr (dump_file, var, TDF_SLIM);
1038 fprintf (dump_file, "\n");
1039 }
75a70cf9 1040 phi = NULL;
4fb5e5ca 1041
1042 if (TREE_CODE (var) == SSA_NAME)
e551bd1a 1043 {
095dcfa3 1044 /* If we are rewriting SSA names, create the LHS of the PHI
1045 node by duplicating VAR. This is useful in the case of
1046 pointers, to also duplicate pointer attributes (alias
1047 information, in particular). */
e551bd1a 1048 edge_iterator ei;
095dcfa3 1049 tree new_lhs;
88dbf20f 1050
88536563 1051 gcc_checking_assert (update_p);
9c06f260 1052 new_lhs = duplicate_ssa_name (var, NULL);
1053 phi = create_phi_node (new_lhs, bb);
095dcfa3 1054 add_new_name_mapping (new_lhs, var);
88dbf20f 1055
1056 /* Add VAR to every argument slot of PHI. We need VAR in
1057 every argument so that rewrite_update_phi_arguments knows
1058 which name is this PHI node replacing. If VAR is a
1059 symbol marked for renaming, this is not necessary, the
1060 renamer will use the symbol on the LHS to get its
1061 reaching definition. */
e551bd1a 1062 FOR_EACH_EDGE (e, ei, bb->preds)
60d535d2 1063 add_phi_arg (phi, var, e, UNKNOWN_LOCATION);
e551bd1a 1064 }
095dcfa3 1065 else
1066 {
9845d120 1067 tree tracked_var;
cd610ee6 1068
88536563 1069 gcc_checking_assert (DECL_P (var));
25ae9ba2 1070 phi = create_phi_node (var, bb);
cd610ee6 1071
1072 tracked_var = target_for_debug_bind (var);
1073 if (tracked_var)
9845d120 1074 {
1075 gimple note = gimple_build_debug_bind (tracked_var,
1076 PHI_RESULT (phi),
1077 phi);
1078 gimple_stmt_iterator si = gsi_after_labels (bb);
1079 gsi_insert_before (&si, note, GSI_SAME_STMT);
1080 }
095dcfa3 1081 }
88dbf20f 1082
1083 /* Mark this PHI node as interesting for update_ssa. */
75a70cf9 1084 set_register_defs (phi, true);
70630fcd 1085 mark_phi_for_rewrite (bb, phi);
e551bd1a 1086 }
1087}
1088
22d6c74e 1089/* Sort var_infos after DECL_UID of their var. */
c86a4051 1090
1091static int
22d6c74e 1092insert_phi_nodes_compare_var_infos (const void *a, const void *b)
c86a4051 1093{
22d6c74e 1094 const struct var_info_d *defa = *(struct var_info_d * const *)a;
1095 const struct var_info_d *defb = *(struct var_info_d * const *)b;
c86a4051 1096 if (DECL_UID (defa->var) < DECL_UID (defb->var))
1097 return -1;
1098 else
1099 return 1;
1100}
e551bd1a 1101
e551bd1a 1102/* Insert PHI nodes at the dominance frontier of blocks with variable
1103 definitions. DFS contains the dominance frontier information for
4fb5e5ca 1104 the flowgraph. */
e551bd1a 1105
1106static void
8a2980be 1107insert_phi_nodes (bitmap_head *dfs)
e551bd1a 1108{
c1f445d2 1109 hash_table<var_info_hasher>::iterator hi;
c86a4051 1110 unsigned i;
22d6c74e 1111 var_info_p info;
e551bd1a 1112
1113 timevar_push (TV_TREE_INSERT_PHI_NODES);
48e1416a 1114
c1f445d2 1115 auto_vec<var_info_p> vars (var_infos->elements ());
1116 FOR_EACH_HASH_TABLE_ELEMENT (*var_infos, info, var_info_p, hi)
74105c0d 1117 if (info->info.need_phi_state != NEED_PHI_STATE_NO)
f1f41a6c 1118 vars.quick_push (info);
c86a4051 1119
409a375f 1120 /* Do two stages to avoid code generation differences for UID
1121 differences but no UID ordering differences. */
f1f41a6c 1122 vars.qsort (insert_phi_nodes_compare_var_infos);
409a375f 1123
f1f41a6c 1124 FOR_EACH_VEC_ELT (vars, i, info)
f3d9a16c 1125 {
74105c0d 1126 bitmap idf = compute_idf (info->info.def_blocks.def_blocks, dfs);
22d6c74e 1127 insert_phi_nodes_for (info->var, idf, false);
409a375f 1128 BITMAP_FREE (idf);
1129 }
1130
4ee9c684 1131 timevar_pop (TV_TREE_INSERT_PHI_NODES);
1132}
1133
1134
4fb5e5ca 1135/* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1136 register DEF (an SSA_NAME) to be a new definition for SYM. */
e551bd1a 1137
de6ed584 1138static void
4fb5e5ca 1139register_new_def (tree def, tree sym)
e551bd1a 1140{
74105c0d 1141 common_info_p info = get_common_info (sym);
e551bd1a 1142 tree currdef;
48e1416a 1143
e551bd1a 1144 /* If this variable is set in a single basic block and all uses are
1145 dominated by the set(s) in that single basic block, then there is
1146 no reason to record anything for this variable in the block local
1147 definition stacks. Doing so just wastes time and memory.
1148
1149 This is the same test to prune the set of variables which may
1150 need PHI nodes. So we just use that information since it's already
1151 computed and available for us to use. */
74105c0d 1152 if (info->need_phi_state == NEED_PHI_STATE_NO)
e551bd1a 1153 {
74105c0d 1154 info->current_def = def;
e551bd1a 1155 return;
1156 }
1157
74105c0d 1158 currdef = info->current_def;
e551bd1a 1159
4fb5e5ca 1160 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1161 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1162 in the stack so that we know which symbol is being defined by
1163 this SSA name when we unwind the stack. */
1164 if (currdef && !is_gimple_reg (sym))
f1f41a6c 1165 block_defs_stack.safe_push (sym);
e551bd1a 1166
4fb5e5ca 1167 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1168 stack is later used by the dominator tree callbacks to restore
1169 the reaching definitions for all the variables defined in the
1170 block after a recursive visit to all its immediately dominated
1171 blocks. If there is no current reaching definition, then just
1172 record the underlying _DECL node. */
f1f41a6c 1173 block_defs_stack.safe_push (currdef ? currdef : sym);
4fb5e5ca 1174
1175 /* Set the current reaching definition for SYM to be DEF. */
74105c0d 1176 info->current_def = def;
e551bd1a 1177}
1178
1179
4ee9c684 1180/* Perform a depth-first traversal of the dominator tree looking for
1181 variables to rename. BB is the block where to start searching.
1182 Renaming is a five step process:
1183
1184 1- Every definition made by PHI nodes at the start of the blocks is
1185 registered as the current definition for the corresponding variable.
1186
1187 2- Every statement in BB is rewritten. USE and VUSE operands are
1188 rewritten with their corresponding reaching definition. DEF and
1189 VDEF targets are registered as new definitions.
48e1416a 1190
4ee9c684 1191 3- All the PHI nodes in successor blocks of BB are visited. The
1192 argument corresponding to BB is replaced with its current reaching
1193 definition.
1194
1195 4- Recursively rewrite every dominator child block of BB.
1196
1197 5- Restore (in reverse order) the current reaching definition for every
1198 new definition introduced in this block. This is done so that when
1199 we return from the recursive call, all the current reaching
1200 definitions are restored to the names that were valid in the
1201 dominator parent of BB. */
1202
e551bd1a 1203/* Return the current definition for variable VAR. If none is found,
4fb5e5ca 1204 create a new SSA name to act as the zeroth definition for VAR. */
f3d9a16c 1205
e551bd1a 1206static tree
1207get_reaching_def (tree var)
1208{
74105c0d 1209 common_info_p info = get_common_info (var);
4fb5e5ca 1210 tree currdef;
48e1416a 1211
e551bd1a 1212 /* Lookup the current reaching definition for VAR. */
74105c0d 1213 currdef = info->current_def;
f3d9a16c 1214
e551bd1a 1215 /* If there is no reaching definition for VAR, create and register a
1216 default definition for it (if needed). */
4fb5e5ca 1217 if (currdef == NULL_TREE)
e551bd1a 1218 {
4fb5e5ca 1219 tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
c6dfe037 1220 currdef = get_or_create_ssa_default_def (cfun, sym);
e551bd1a 1221 }
1222
1223 /* Return the current reaching definition for VAR, or the default
1224 definition, if we had to create one. */
4fb5e5ca 1225 return currdef;
e551bd1a 1226}
1227
1228
841424cc 1229/* Helper function for rewrite_stmt. Rewrite uses in a debug stmt. */
1230
1231static void
1232rewrite_debug_stmt_uses (gimple stmt)
1233{
1234 use_operand_p use_p;
1235 ssa_op_iter iter;
1236 bool update = false;
1237
1238 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1239 {
4ae5778c 1240 tree var = USE_FROM_PTR (use_p), def;
74105c0d 1241 common_info_p info = get_common_info (var);
88536563 1242 gcc_checking_assert (DECL_P (var));
74105c0d 1243 def = info->current_def;
4ae5778c 1244 if (!def)
841424cc 1245 {
34154e27 1246 if (TREE_CODE (var) == PARM_DECL
1247 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
841424cc 1248 {
1249 gimple_stmt_iterator gsi
34154e27 1250 =
1251 gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
841424cc 1252 int lim;
1253 /* Search a few source bind stmts at the start of first bb to
1254 see if a DEBUG_EXPR_DECL can't be reused. */
1255 for (lim = 32;
1256 !gsi_end_p (gsi) && lim > 0;
1257 gsi_next (&gsi), lim--)
1258 {
1259 gimple gstmt = gsi_stmt (gsi);
1260 if (!gimple_debug_source_bind_p (gstmt))
1261 break;
1262 if (gimple_debug_source_bind_get_value (gstmt) == var)
1263 {
1264 def = gimple_debug_source_bind_get_var (gstmt);
1265 if (TREE_CODE (def) == DEBUG_EXPR_DECL)
1266 break;
1267 else
1268 def = NULL_TREE;
1269 }
1270 }
1271 /* If not, add a new source bind stmt. */
1272 if (def == NULL_TREE)
1273 {
1274 gimple def_temp;
1275 def = make_node (DEBUG_EXPR_DECL);
1276 def_temp = gimple_build_debug_source_bind (def, var, NULL);
1277 DECL_ARTIFICIAL (def) = 1;
1278 TREE_TYPE (def) = TREE_TYPE (var);
1279 DECL_MODE (def) = DECL_MODE (var);
34154e27 1280 gsi =
1281 gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
841424cc 1282 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
1283 }
1284 update = true;
1285 }
1286 }
1287 else
824651eb 1288 {
74105c0d 1289 /* Check if info->current_def can be trusted. */
4ae5778c 1290 basic_block bb = gimple_bb (stmt);
1291 basic_block def_bb
1292 = SSA_NAME_IS_DEFAULT_DEF (def)
1293 ? NULL : gimple_bb (SSA_NAME_DEF_STMT (def));
1294
1295 /* If definition is in current bb, it is fine. */
1296 if (bb == def_bb)
1297 ;
1298 /* If definition bb doesn't dominate the current bb,
1299 it can't be used. */
1300 else if (def_bb && !dominated_by_p (CDI_DOMINATORS, bb, def_bb))
1301 def = NULL;
1302 /* If there is just one definition and dominates the current
1303 bb, it is fine. */
74105c0d 1304 else if (info->need_phi_state == NEED_PHI_STATE_NO)
4ae5778c 1305 ;
1306 else
824651eb 1307 {
74105c0d 1308 struct def_blocks_d *db_p = get_def_blocks_for (info);
824651eb 1309
4ae5778c 1310 /* If there are some non-debug uses in the current bb,
1311 it is fine. */
1312 if (bitmap_bit_p (db_p->livein_blocks, bb->index))
824651eb 1313 ;
4ae5778c 1314 /* Otherwise give up for now. */
824651eb 1315 else
4ae5778c 1316 def = NULL;
824651eb 1317 }
1318 }
841424cc 1319 if (def == NULL)
1320 {
1321 gimple_debug_bind_reset_value (stmt);
1322 update_stmt (stmt);
1323 return;
1324 }
1325 SET_USE (use_p, def);
1326 }
1327 if (update)
1328 update_stmt (stmt);
1329}
1330
e551bd1a 1331/* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1332 the block with its immediate reaching definitions. Update the current
1333 definition of a variable when a new real or virtual definition is found. */
f3d9a16c 1334
1335static void
eb54054d 1336rewrite_stmt (gimple_stmt_iterator *si)
f3d9a16c 1337{
e551bd1a 1338 use_operand_p use_p;
1339 def_operand_p def_p;
1340 ssa_op_iter iter;
eb54054d 1341 gimple stmt = gsi_stmt (*si);
e551bd1a 1342
e551bd1a 1343 /* If mark_def_sites decided that we don't need to rewrite this
1344 statement, ignore it. */
2920559d 1345 gcc_assert (blocks_to_update == NULL);
75a70cf9 1346 if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
e551bd1a 1347 return;
f3d9a16c 1348
1349 if (dump_file && (dump_flags & TDF_DETAILS))
e551bd1a 1350 {
1351 fprintf (dump_file, "Renaming statement ");
75a70cf9 1352 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
e551bd1a 1353 fprintf (dump_file, "\n");
1354 }
f3d9a16c 1355
4fb5e5ca 1356 /* Step 1. Rewrite USES in the statement. */
75a70cf9 1357 if (rewrite_uses_p (stmt))
841424cc 1358 {
1359 if (is_gimple_debug (stmt))
1360 rewrite_debug_stmt_uses (stmt);
1361 else
5aabc5f2 1362 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
841424cc 1363 {
1364 tree var = USE_FROM_PTR (use_p);
88536563 1365 gcc_checking_assert (DECL_P (var));
841424cc 1366 SET_USE (use_p, get_reaching_def (var));
1367 }
1368 }
f3d9a16c 1369
4fb5e5ca 1370 /* Step 2. Register the statement's DEF operands. */
75a70cf9 1371 if (register_defs_p (stmt))
5aabc5f2 1372 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
88dbf20f 1373 {
1374 tree var = DEF_FROM_PTR (def_p);
eb54054d 1375 tree name;
9845d120 1376 tree tracked_var;
eb54054d 1377
88536563 1378 gcc_checking_assert (DECL_P (var));
eb54054d 1379
1380 if (gimple_clobber_p (stmt)
1381 && is_gimple_reg (var))
1382 {
1383 /* If we rewrite a DECL into SSA form then drop its
1384 clobber stmts and replace uses with a new default def. */
88536563 1385 gcc_checking_assert (TREE_CODE (var) == VAR_DECL
1386 && !gimple_vdef (stmt));
eb54054d 1387 gsi_replace (si, gimple_build_nop (), true);
1388 register_new_def (get_or_create_ssa_default_def (cfun, var), var);
1389 break;
1390 }
1391
1392 name = make_ssa_name (var, stmt);
9845d120 1393 SET_DEF (def_p, name);
4fb5e5ca 1394 register_new_def (DEF_FROM_PTR (def_p), var);
9845d120 1395
1396 tracked_var = target_for_debug_bind (var);
1397 if (tracked_var)
1398 {
1399 gimple note = gimple_build_debug_bind (tracked_var, name, stmt);
eb54054d 1400 gsi_insert_after (si, note, GSI_SAME_STMT);
9845d120 1401 }
88dbf20f 1402 }
f3d9a16c 1403}
4ee9c684 1404
e551bd1a 1405
4ee9c684 1406/* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1407 PHI nodes. For every PHI node found, add a new argument containing the
1408 current reaching definition for the variable and the edge through which
dac49aa5 1409 that definition is reaching the PHI node. */
4ee9c684 1410
1411static void
6bf320fb 1412rewrite_add_phi_arguments (basic_block bb)
4ee9c684 1413{
1414 edge e;
cd665a06 1415 edge_iterator ei;
4ee9c684 1416
cd665a06 1417 FOR_EACH_EDGE (e, ei, bb->succs)
4ee9c684 1418 {
1a91d914 1419 gphi *phi;
1420 gphi_iterator gsi;
4ee9c684 1421
75a70cf9 1422 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
1423 gsi_next (&gsi))
4ee9c684 1424 {
e60062ea 1425 tree currdef, res;
1426 location_t loc;
efbcb6de 1427
1a91d914 1428 phi = gsi.phi ();
e60062ea 1429 res = gimple_phi_result (phi);
1430 currdef = get_reaching_def (SSA_NAME_VAR (res));
1431 /* Virtual operand PHI args do not need a location. */
1432 if (virtual_operand_p (res))
1433 loc = UNKNOWN_LOCATION;
1434 else
1435 loc = gimple_location (SSA_NAME_DEF_STMT (currdef));
1436 add_phi_arg (phi, currdef, e, loc);
4ee9c684 1437 }
1438 }
1439}
1440
54c91640 1441class rewrite_dom_walker : public dom_walker
1442{
1443public:
1444 rewrite_dom_walker (cdi_direction direction) : dom_walker (direction) {}
1445
1446 virtual void before_dom_children (basic_block);
1447 virtual void after_dom_children (basic_block);
1448};
1449
6bf320fb 1450/* SSA Rewriting Step 1. Initialization, create a block local stack
1451 of reaching definitions for new SSA names produced in this block
1452 (BLOCK_DEFS). Register new definitions for every PHI node in the
1453 block. */
1454
54c91640 1455void
1456rewrite_dom_walker::before_dom_children (basic_block bb)
6bf320fb 1457{
6bf320fb 1458 if (dump_file && (dump_flags & TDF_DETAILS))
1459 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
1460
1461 /* Mark the unwind point for this block. */
f1f41a6c 1462 block_defs_stack.safe_push (NULL_TREE);
6bf320fb 1463
1464 /* Step 1. Register new definitions for every PHI node in the block.
1465 Conceptually, all the PHI nodes are executed in parallel and each PHI
1466 node introduces a new version for the associated variable. */
1a91d914 1467 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1468 gsi_next (&gsi))
6bf320fb 1469 {
5aabc5f2 1470 tree result = gimple_phi_result (gsi_stmt (gsi));
6bf320fb 1471 register_new_def (result, SSA_NAME_VAR (result));
1472 }
1473
1474 /* Step 2. Rewrite every variable used in each statement in the block
1475 with its immediate reaching definitions. Update the current definition
1476 of a variable when a new real or virtual definition is found. */
08b7917c 1477 if (bitmap_bit_p (interesting_blocks, bb->index))
1a91d914 1478 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1479 gsi_next (&gsi))
eb54054d 1480 rewrite_stmt (&gsi);
6bf320fb 1481
1482 /* Step 3. Visit all the successor blocks of BB looking for PHI nodes.
1483 For every PHI node found, add a new argument containing the current
1484 reaching definition for the variable and the edge through which that
1485 definition is reaching the PHI node. */
1486 rewrite_add_phi_arguments (bb);
1487}
1488
1489
e551bd1a 1490
4fb5e5ca 1491/* Called after visiting all the statements in basic block BB and all
1492 of its dominator children. Restore CURRDEFS to its original value. */
4ee9c684 1493
54c91640 1494void
1495rewrite_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
4ee9c684 1496{
dd2d357d 1497 /* Restore CURRDEFS to its original state. */
f1f41a6c 1498 while (block_defs_stack.length () > 0)
4ee9c684 1499 {
f1f41a6c 1500 tree tmp = block_defs_stack.pop ();
4ee9c684 1501 tree saved_def, var;
1502
dd2d357d 1503 if (tmp == NULL_TREE)
1504 break;
1505
4ee9c684 1506 if (TREE_CODE (tmp) == SSA_NAME)
1507 {
4fb5e5ca 1508 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1509 current definition of its underlying variable. Note that
1510 if the SSA_NAME is not for a GIMPLE register, the symbol
1511 being defined is stored in the next slot in the stack.
1512 This mechanism is needed because an SSA name for a
1513 non-register symbol may be the definition for more than
1514 one symbol (e.g., SFTs, aliased variables, etc). */
4ee9c684 1515 saved_def = tmp;
1516 var = SSA_NAME_VAR (saved_def);
4fb5e5ca 1517 if (!is_gimple_reg (var))
f1f41a6c 1518 var = block_defs_stack.pop ();
4ee9c684 1519 }
1520 else
1521 {
4fb5e5ca 1522 /* If we recorded anything else, it must have been a _DECL
1523 node and its current reaching definition must have been
1524 NULL. */
4ee9c684 1525 saved_def = NULL;
1526 var = tmp;
1527 }
48e1416a 1528
74105c0d 1529 get_common_info (var)->current_def = saved_def;
4ee9c684 1530 }
1531}
1532
1533
4fb5e5ca 1534/* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1535
4b987fac 1536DEBUG_FUNCTION void
4fb5e5ca 1537debug_decl_set (bitmap set)
1538{
1539 dump_decl_set (stderr, set);
dd277d48 1540 fprintf (stderr, "\n");
4fb5e5ca 1541}
1542
1543
1544/* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1545 stack up to a maximum of N levels. If N is -1, the whole stack is
1546 dumped. New levels are created when the dominator tree traversal
1547 used for renaming enters a new sub-tree. */
1548
1549void
1550dump_defs_stack (FILE *file, int n)
1551{
1552 int i, j;
1553
1554 fprintf (file, "\n\nRenaming stack");
1555 if (n > 0)
1556 fprintf (file, " (up to %d levels)", n);
1557 fprintf (file, "\n\n");
1558
1559 i = 1;
1560 fprintf (file, "Level %d (current level)\n", i);
f1f41a6c 1561 for (j = (int) block_defs_stack.length () - 1; j >= 0; j--)
4fb5e5ca 1562 {
1563 tree name, var;
48e1416a 1564
f1f41a6c 1565 name = block_defs_stack[j];
4fb5e5ca 1566 if (name == NULL_TREE)
1567 {
1568 i++;
1569 if (n > 0 && i > n)
1570 break;
1571 fprintf (file, "\nLevel %d\n", i);
1572 continue;
1573 }
1574
1575 if (DECL_P (name))
1576 {
1577 var = name;
1578 name = NULL_TREE;
1579 }
1580 else
1581 {
1582 var = SSA_NAME_VAR (name);
1583 if (!is_gimple_reg (var))
1584 {
1585 j--;
f1f41a6c 1586 var = block_defs_stack[j];
4fb5e5ca 1587 }
1588 }
1589
1590 fprintf (file, " Previous CURRDEF (");
1591 print_generic_expr (file, var, 0);
1592 fprintf (file, ") = ");
1593 if (name)
1594 print_generic_expr (file, name, 0);
1595 else
1596 fprintf (file, "<NIL>");
1597 fprintf (file, "\n");
1598 }
1599}
1600
1601
1602/* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1603 stack up to a maximum of N levels. If N is -1, the whole stack is
1604 dumped. New levels are created when the dominator tree traversal
1605 used for renaming enters a new sub-tree. */
1606
4b987fac 1607DEBUG_FUNCTION void
4fb5e5ca 1608debug_defs_stack (int n)
1609{
1610 dump_defs_stack (stderr, n);
1611}
1612
1613
1614/* Dump the current reaching definition of every symbol to FILE. */
1615
1616void
1617dump_currdefs (FILE *file)
1618{
e70e8b13 1619 unsigned i;
4fb5e5ca 1620 tree var;
1621
f1f41a6c 1622 if (symbols_to_rename.is_empty ())
e70e8b13 1623 return;
1624
4fb5e5ca 1625 fprintf (file, "\n\nCurrent reaching definitions\n\n");
f1f41a6c 1626 FOR_EACH_VEC_ELT (symbols_to_rename, i, var)
e70e8b13 1627 {
74105c0d 1628 common_info_p info = get_common_info (var);
e70e8b13 1629 fprintf (file, "CURRDEF (");
1630 print_generic_expr (file, var, 0);
1631 fprintf (file, ") = ");
74105c0d 1632 if (info->current_def)
1633 print_generic_expr (file, info->current_def, 0);
e70e8b13 1634 else
1635 fprintf (file, "<NIL>");
1636 fprintf (file, "\n");
1637 }
4fb5e5ca 1638}
1639
1640
1641/* Dump the current reaching definition of every symbol to stderr. */
1642
4b987fac 1643DEBUG_FUNCTION void
4fb5e5ca 1644debug_currdefs (void)
1645{
1646 dump_currdefs (stderr);
1647}
1648
1649
4ee9c684 1650/* Dump SSA information to FILE. */
1651
1652void
1653dump_tree_ssa (FILE *file)
1654{
4ee9c684 1655 const char *funcname
5135beeb 1656 = lang_hooks.decl_printable_name (current_function_decl, 2);
4ee9c684 1657
4fb5e5ca 1658 fprintf (file, "SSA renaming information for %s\n\n", funcname);
4ee9c684 1659
22d6c74e 1660 dump_var_infos (file);
4fb5e5ca 1661 dump_defs_stack (file, -1);
1662 dump_currdefs (file);
1663 dump_tree_ssa_stats (file);
4ee9c684 1664}
1665
1666
1667/* Dump SSA information to stderr. */
1668
4b987fac 1669DEBUG_FUNCTION void
4ee9c684 1670debug_tree_ssa (void)
1671{
1672 dump_tree_ssa (stderr);
1673}
1674
1675
e551bd1a 1676/* Dump statistics for the hash table HTAB. */
1677
1678static void
c1f445d2 1679htab_statistics (FILE *file, const hash_table<var_info_hasher> &htab)
e551bd1a 1680{
1681 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
3e871d4d 1682 (long) htab.size (),
1683 (long) htab.elements (),
1684 htab.collisions ());
e551bd1a 1685}
1686
1687
4ee9c684 1688/* Dump SSA statistics on FILE. */
1689
1690void
1691dump_tree_ssa_stats (FILE *file)
1692{
c1f445d2 1693 if (var_infos)
4fb5e5ca 1694 {
22d6c74e 1695 fprintf (file, "\nHash table statistics:\n");
1696 fprintf (file, " var_infos: ");
c1f445d2 1697 htab_statistics (file, *var_infos);
22d6c74e 1698 fprintf (file, "\n");
4fb5e5ca 1699 }
4ee9c684 1700}
1701
1702
1703/* Dump SSA statistics on stderr. */
1704
4b987fac 1705DEBUG_FUNCTION void
4ee9c684 1706debug_tree_ssa_stats (void)
1707{
1708 dump_tree_ssa_stats (stderr);
1709}
1710
1711
22d6c74e 1712/* Callback for htab_traverse to dump the VAR_INFOS hash table. */
4ee9c684 1713
3e871d4d 1714int
1715debug_var_infos_r (var_info_d **slot, FILE *file)
e551bd1a 1716{
3e871d4d 1717 struct var_info_d *info = *slot;
48e1416a 1718
4fb5e5ca 1719 fprintf (file, "VAR: ");
74105c0d 1720 print_generic_expr (file, info->var, dump_flags);
1721 bitmap_print (file, info->info.def_blocks.def_blocks,
1722 ", DEF_BLOCKS: { ", "}");
1723 bitmap_print (file, info->info.def_blocks.livein_blocks,
1724 ", LIVEIN_BLOCKS: { ", "}");
1725 bitmap_print (file, info->info.def_blocks.phi_blocks,
1726 ", PHI_BLOCKS: { ", "}\n");
4ee9c684 1727
e551bd1a 1728 return 1;
1729}
4ee9c684 1730
4ee9c684 1731
22d6c74e 1732/* Dump the VAR_INFOS hash table on FILE. */
4fb5e5ca 1733
1734void
22d6c74e 1735dump_var_infos (FILE *file)
4fb5e5ca 1736{
1737 fprintf (file, "\n\nDefinition and live-in blocks:\n\n");
c1f445d2 1738 if (var_infos)
1739 var_infos->traverse <FILE *, debug_var_infos_r> (file);
4fb5e5ca 1740}
1741
1742
22d6c74e 1743/* Dump the VAR_INFOS hash table on stderr. */
4ee9c684 1744
4b987fac 1745DEBUG_FUNCTION void
22d6c74e 1746debug_var_infos (void)
e551bd1a 1747{
22d6c74e 1748 dump_var_infos (stderr);
e551bd1a 1749}
4ee9c684 1750
f3d9a16c 1751
88dbf20f 1752/* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
4ee9c684 1753
88dbf20f 1754static inline void
1755register_new_update_single (tree new_name, tree old_name)
1756{
74105c0d 1757 common_info_p info = get_common_info (old_name);
1758 tree currdef = info->current_def;
f3d9a16c 1759
4fb5e5ca 1760 /* Push the current reaching definition into BLOCK_DEFS_STACK.
88dbf20f 1761 This stack is later used by the dominator tree callbacks to
1762 restore the reaching definitions for all the variables
1763 defined in the block after a recursive visit to all its
1764 immediately dominated blocks. */
f1f41a6c 1765 block_defs_stack.reserve (2);
1766 block_defs_stack.quick_push (currdef);
1767 block_defs_stack.quick_push (old_name);
f3d9a16c 1768
88dbf20f 1769 /* Set the current reaching definition for OLD_NAME to be
1770 NEW_NAME. */
74105c0d 1771 info->current_def = new_name;
88dbf20f 1772}
e551bd1a 1773
4ee9c684 1774
88dbf20f 1775/* Register NEW_NAME to be the new reaching definition for all the
1776 names in OLD_NAMES. Used by the incremental SSA update routines to
1777 replace old SSA names with new ones. */
e551bd1a 1778
88dbf20f 1779static inline void
1780register_new_update_set (tree new_name, bitmap old_names)
1781{
1782 bitmap_iterator bi;
1783 unsigned i;
e551bd1a 1784
88dbf20f 1785 EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
1786 register_new_update_single (new_name, ssa_name (i));
4ee9c684 1787}
1788
e551bd1a 1789
88dbf20f 1790
095dcfa3 1791/* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1792 it is a symbol marked for renaming, replace it with USE_P's current
1793 reaching definition. */
1794
1795static inline void
1796maybe_replace_use (use_operand_p use_p)
1797{
1798 tree rdef = NULL_TREE;
1799 tree use = USE_FROM_PTR (use_p);
1800 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1801
e70e8b13 1802 if (marked_for_renaming (sym))
095dcfa3 1803 rdef = get_reaching_def (sym);
1804 else if (is_old_name (use))
1805 rdef = get_reaching_def (use);
1806
1807 if (rdef && rdef != use)
1808 SET_USE (use_p, rdef);
1809}
1810
1811
9845d120 1812/* Same as maybe_replace_use, but without introducing default stmts,
1813 returning false to indicate a need to do so. */
1814
1815static inline bool
1816maybe_replace_use_in_debug_stmt (use_operand_p use_p)
1817{
1818 tree rdef = NULL_TREE;
1819 tree use = USE_FROM_PTR (use_p);
1820 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1821
e70e8b13 1822 if (marked_for_renaming (sym))
74105c0d 1823 rdef = get_var_info (sym)->info.current_def;
9845d120 1824 else if (is_old_name (use))
1825 {
74105c0d 1826 rdef = get_ssa_name_ann (use)->info.current_def;
9845d120 1827 /* We can't assume that, if there's no current definition, the
1828 default one should be used. It could be the case that we've
1829 rearranged blocks so that the earlier definition no longer
1830 dominates the use. */
1831 if (!rdef && SSA_NAME_IS_DEFAULT_DEF (use))
1832 rdef = use;
1833 }
1834 else
1835 rdef = use;
1836
1837 if (rdef && rdef != use)
1838 SET_USE (use_p, rdef);
1839
1840 return rdef != NULL_TREE;
1841}
1842
1843
095dcfa3 1844/* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1845 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1846 register it as the current definition for the names replaced by
4d8d655b 1847 DEF_P. Returns whether the statement should be removed. */
095dcfa3 1848
4d8d655b 1849static inline bool
cd610ee6 1850maybe_register_def (def_operand_p def_p, gimple stmt,
1851 gimple_stmt_iterator gsi)
095dcfa3 1852{
1853 tree def = DEF_FROM_PTR (def_p);
1854 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
4d8d655b 1855 bool to_delete = false;
095dcfa3 1856
4fb5e5ca 1857 /* If DEF is a naked symbol that needs renaming, create a new
1858 name for it. */
e70e8b13 1859 if (marked_for_renaming (sym))
095dcfa3 1860 {
1861 if (DECL_P (def))
1862 {
4d8d655b 1863 if (gimple_clobber_p (stmt) && is_gimple_reg (sym))
1864 {
1865 gcc_checking_assert (TREE_CODE (sym) == VAR_DECL);
1866 /* Replace clobber stmts with a default def. This new use of a
1867 default definition may make it look like SSA_NAMEs have
1868 conflicting lifetimes, so we need special code to let them
1869 coalesce properly. */
1870 to_delete = true;
1871 def = get_or_create_ssa_default_def (cfun, sym);
1872 }
1873 else
1874 def = make_ssa_name (def, stmt);
095dcfa3 1875 SET_DEF (def_p, def);
cd610ee6 1876
4d8d655b 1877 tree tracked_var = target_for_debug_bind (sym);
cd610ee6 1878 if (tracked_var)
1879 {
1880 gimple note = gimple_build_debug_bind (tracked_var, def, stmt);
8d7f27c3 1881 /* If stmt ends the bb, insert the debug stmt on the single
1882 non-EH edge from the stmt. */
1883 if (gsi_one_before_end_p (gsi) && stmt_ends_bb_p (stmt))
1884 {
1885 basic_block bb = gsi_bb (gsi);
1886 edge_iterator ei;
1887 edge e, ef = NULL;
1888 FOR_EACH_EDGE (e, ei, bb->succs)
1889 if (!(e->flags & EDGE_EH))
1890 {
88536563 1891 gcc_checking_assert (!ef);
8d7f27c3 1892 ef = e;
1893 }
36a2709b 1894 /* If there are other predecessors to ef->dest, then
1895 there must be PHI nodes for the modified
1896 variable, and therefore there will be debug bind
1897 stmts after the PHI nodes. The debug bind notes
1898 we'd insert would force the creation of a new
1899 block (diverging codegen) and be redundant with
1900 the post-PHI bind stmts, so don't add them.
1901
1902 As for the exit edge, there wouldn't be redundant
1903 bind stmts, but there wouldn't be a PC to bind
1904 them to either, so avoid diverging the CFG. */
1905 if (ef && single_pred_p (ef->dest)
34154e27 1906 && ef->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
36a2709b 1907 {
1908 /* If there were PHI nodes in the node, we'd
1909 have to make sure the value we're binding
1910 doesn't need rewriting. But there shouldn't
1911 be PHI nodes in a single-predecessor block,
1912 so we just add the note. */
1913 gsi_insert_on_edge_immediate (ef, note);
1914 }
8d7f27c3 1915 }
1916 else
1917 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
cd610ee6 1918 }
095dcfa3 1919 }
1920
1921 register_new_update_single (def, sym);
1922 }
1923 else
1924 {
1925 /* If DEF is a new name, register it as a new definition
1926 for all the names replaced by DEF. */
1927 if (is_new_name (def))
1928 register_new_update_set (def, names_replaced_by (def));
1929
1930 /* If DEF is an old name, register DEF as a new
1931 definition for itself. */
1932 if (is_old_name (def))
1933 register_new_update_single (def, def);
1934 }
4d8d655b 1935
1936 return to_delete;
095dcfa3 1937}
1938
1939
88dbf20f 1940/* Update every variable used in the statement pointed-to by SI. The
1941 statement is assumed to be in SSA form already. Names in
1942 OLD_SSA_NAMES used by SI will be updated to their current reaching
1943 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1944 will be registered as a new definition for their corresponding name
4d8d655b 1945 in OLD_SSA_NAMES. Returns whether STMT should be removed. */
88dbf20f 1946
4d8d655b 1947static bool
cd610ee6 1948rewrite_update_stmt (gimple stmt, gimple_stmt_iterator gsi)
88dbf20f 1949{
88dbf20f 1950 use_operand_p use_p;
1951 def_operand_p def_p;
1952 ssa_op_iter iter;
1953
88dbf20f 1954 /* Only update marked statements. */
75a70cf9 1955 if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
4d8d655b 1956 return false;
88dbf20f 1957
1958 if (dump_file && (dump_flags & TDF_DETAILS))
1959 {
1960 fprintf (dump_file, "Updating SSA information for statement ");
75a70cf9 1961 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
88dbf20f 1962 }
1963
1964 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1965 symbol is marked for renaming. */
75a70cf9 1966 if (rewrite_uses_p (stmt))
9845d120 1967 {
1968 if (is_gimple_debug (stmt))
1969 {
1970 bool failed = false;
1971
1972 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1973 if (!maybe_replace_use_in_debug_stmt (use_p))
1974 {
1975 failed = true;
1976 break;
1977 }
1978
1979 if (failed)
1980 {
1981 /* DOM sometimes threads jumps in such a way that a
1982 debug stmt ends up referencing a SSA variable that no
1983 longer dominates the debug stmt, but such that all
1984 incoming definitions refer to the same definition in
1985 an earlier dominator. We could try to recover that
1986 definition somehow, but this will have to do for now.
1987
1988 Introducing a default definition, which is what
1989 maybe_replace_use() would do in such cases, may
1990 modify code generation, for the otherwise-unused
1991 default definition would never go away, modifying SSA
1992 version numbers all over. */
1993 gimple_debug_bind_reset_value (stmt);
1994 update_stmt (stmt);
1995 }
1996 }
1997 else
1998 {
1999 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
2000 maybe_replace_use (use_p);
2001 }
2002 }
88dbf20f 2003
2004 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
2005 Also register definitions for names whose underlying symbol is
2006 marked for renaming. */
4d8d655b 2007 bool to_delete = false;
75a70cf9 2008 if (register_defs_p (stmt))
dd277d48 2009 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
4d8d655b 2010 to_delete |= maybe_register_def (def_p, stmt, gsi);
2011
2012 return to_delete;
88dbf20f 2013}
2014
2015
2016/* Visit all the successor blocks of BB looking for PHI nodes. For
2017 every PHI node found, check if any of its arguments is in
2018 OLD_SSA_NAMES. If so, and if the argument has a current reaching
2019 definition, replace it. */
2020
2021static void
6bf320fb 2022rewrite_update_phi_arguments (basic_block bb)
88dbf20f 2023{
2024 edge e;
2025 edge_iterator ei;
70630fcd 2026 unsigned i;
88dbf20f 2027
2028 FOR_EACH_EDGE (e, ei, bb->succs)
2029 {
1a91d914 2030 gphi *phi;
2031 vec<gphi *> phis;
88dbf20f 2032
70630fcd 2033 if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
2034 continue;
48e1416a 2035
f1f41a6c 2036 phis = phis_to_rewrite[e->dest->index];
2037 FOR_EACH_VEC_ELT (phis, i, phi)
88dbf20f 2038 {
efbcb6de 2039 tree arg, lhs_sym, reaching_def = NULL;
88dbf20f 2040 use_operand_p arg_p;
2041
88536563 2042 gcc_checking_assert (rewrite_uses_p (phi));
88dbf20f 2043
2044 arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
2045 arg = USE_FROM_PTR (arg_p);
2046
2047 if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
2048 continue;
2049
75a70cf9 2050 lhs_sym = SSA_NAME_VAR (gimple_phi_result (phi));
4fb5e5ca 2051
88dbf20f 2052 if (arg == NULL_TREE)
2053 {
2054 /* When updating a PHI node for a recently introduced
2055 symbol we may find NULL arguments. That's why we
2056 take the symbol from the LHS of the PHI node. */
efbcb6de 2057 reaching_def = get_reaching_def (lhs_sym);
2058
88dbf20f 2059 }
2060 else
2061 {
2062 tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
2063
e70e8b13 2064 if (marked_for_renaming (sym))
efbcb6de 2065 reaching_def = get_reaching_def (sym);
88dbf20f 2066 else if (is_old_name (arg))
efbcb6de 2067 reaching_def = get_reaching_def (arg);
2068 }
2069
2070 /* Update the argument if there is a reaching def. */
2071 if (reaching_def)
2072 {
efbcb6de 2073 source_location locus;
2074 int arg_i = PHI_ARG_INDEX_FROM_USE (arg_p);
2075
2076 SET_USE (arg_p, reaching_def);
efbcb6de 2077
e60062ea 2078 /* Virtual operands do not need a location. */
2079 if (virtual_operand_p (reaching_def))
2080 locus = UNKNOWN_LOCATION;
efbcb6de 2081 else
e60062ea 2082 {
2083 gimple stmt = SSA_NAME_DEF_STMT (reaching_def);
1a91d914 2084 gphi *other_phi = dyn_cast <gphi *> (stmt);
e60062ea 2085
2086 /* Single element PHI nodes behave like copies, so get the
2087 location from the phi argument. */
1a91d914 2088 if (other_phi
2089 && gimple_phi_num_args (other_phi) == 1)
2090 locus = gimple_phi_arg_location (other_phi, 0);
e60062ea 2091 else
2092 locus = gimple_location (stmt);
2093 }
efbcb6de 2094
2095 gimple_phi_arg_set_location (phi, arg_i, locus);
88dbf20f 2096 }
2097
efbcb6de 2098
88dbf20f 2099 if (e->flags & EDGE_ABNORMAL)
2100 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
2101 }
2102 }
2103}
2104
54c91640 2105class rewrite_update_dom_walker : public dom_walker
2106{
2107public:
2108 rewrite_update_dom_walker (cdi_direction direction) : dom_walker (direction) {}
2109
2110 virtual void before_dom_children (basic_block);
2111 virtual void after_dom_children (basic_block);
2112};
88dbf20f 2113
6bf320fb 2114/* Initialization of block data structures for the incremental SSA
2115 update pass. Create a block local stack of reaching definitions
2116 for new SSA names produced in this block (BLOCK_DEFS). Register
2117 new definitions for every PHI node in the block. */
2118
54c91640 2119void
2120rewrite_update_dom_walker::before_dom_children (basic_block bb)
6bf320fb 2121{
6bf320fb 2122 bool is_abnormal_phi;
6bf320fb 2123
2124 if (dump_file && (dump_flags & TDF_DETAILS))
0ffbb1b6 2125 fprintf (dump_file, "Registering new PHI nodes in block #%d\n",
6bf320fb 2126 bb->index);
2127
2128 /* Mark the unwind point for this block. */
f1f41a6c 2129 block_defs_stack.safe_push (NULL_TREE);
6bf320fb 2130
2131 if (!bitmap_bit_p (blocks_to_update, bb->index))
2132 return;
2133
2134 /* Mark the LHS if any of the arguments flows through an abnormal
2135 edge. */
f1047120 2136 is_abnormal_phi = bb_has_abnormal_pred (bb);
6bf320fb 2137
2138 /* If any of the PHI nodes is a replacement for a name in
2139 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
2140 register it as a new definition for its corresponding name. Also
2141 register definitions for names whose underlying symbols are
2142 marked for renaming. */
1a91d914 2143 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2144 gsi_next (&gsi))
6bf320fb 2145 {
2146 tree lhs, lhs_sym;
1a91d914 2147 gphi *phi = gsi.phi ();
6bf320fb 2148
2149 if (!register_defs_p (phi))
2150 continue;
48e1416a 2151
6bf320fb 2152 lhs = gimple_phi_result (phi);
2153 lhs_sym = SSA_NAME_VAR (lhs);
2154
e70e8b13 2155 if (marked_for_renaming (lhs_sym))
6bf320fb 2156 register_new_update_single (lhs, lhs_sym);
2157 else
2158 {
2159
2160 /* If LHS is a new name, register a new definition for all
2161 the names replaced by LHS. */
2162 if (is_new_name (lhs))
2163 register_new_update_set (lhs, names_replaced_by (lhs));
48e1416a 2164
6bf320fb 2165 /* If LHS is an OLD name, register it as a new definition
2166 for itself. */
2167 if (is_old_name (lhs))
2168 register_new_update_single (lhs, lhs);
2169 }
2170
2171 if (is_abnormal_phi)
2172 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
2173 }
2174
2175 /* Step 2. Rewrite every variable used in each statement in the block. */
08b7917c 2176 if (bitmap_bit_p (interesting_blocks, bb->index))
cd610ee6 2177 {
88536563 2178 gcc_checking_assert (bitmap_bit_p (blocks_to_update, bb->index));
1a91d914 2179 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
4d8d655b 2180 if (rewrite_update_stmt (gsi_stmt (gsi), gsi))
2181 gsi_remove (&gsi, true);
2182 else
2183 gsi_next (&gsi);
cd610ee6 2184 }
6bf320fb 2185
2186 /* Step 3. Update PHI nodes. */
2187 rewrite_update_phi_arguments (bb);
2188}
2189
2190/* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
2191 the current reaching definition of every name re-written in BB to
2192 the original reaching definition before visiting BB. This
2193 unwinding must be done in the opposite order to what is done in
2194 register_new_update_set. */
2195
54c91640 2196void
2197rewrite_update_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
6bf320fb 2198{
f1f41a6c 2199 while (block_defs_stack.length () > 0)
6bf320fb 2200 {
f1f41a6c 2201 tree var = block_defs_stack.pop ();
6bf320fb 2202 tree saved_def;
48e1416a 2203
6bf320fb 2204 /* NULL indicates the unwind stop point for this block (see
2205 rewrite_update_enter_block). */
2206 if (var == NULL)
2207 return;
2208
f1f41a6c 2209 saved_def = block_defs_stack.pop ();
74105c0d 2210 get_common_info (var)->current_def = saved_def;
6bf320fb 2211 }
2212}
2213
2214
88dbf20f 2215/* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
48e1416a 2216 form.
88dbf20f 2217
2218 ENTRY indicates the block where to start. Every block dominated by
2219 ENTRY will be rewritten.
2220
2221 WHAT indicates what actions will be taken by the renamer (see enum
2222 rewrite_mode).
2223
2224 BLOCKS are the set of interesting blocks for the dominator walker
2225 to process. If this set is NULL, then all the nodes dominated
2226 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2227 are not present in BLOCKS are ignored. */
2228
2229static void
6bf320fb 2230rewrite_blocks (basic_block entry, enum rewrite_mode what)
88dbf20f 2231{
88dbf20f 2232 /* Rewrite all the basic blocks in the program. */
2233 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
2234
54c91640 2235 block_defs_stack.create (10);
88dbf20f 2236
54c91640 2237 /* Recursively walk the dominator tree rewriting each statement in
2238 each basic block. */
88dbf20f 2239 if (what == REWRITE_ALL)
54c91640 2240 rewrite_dom_walker (CDI_DOMINATORS).walk (entry);
88dbf20f 2241 else if (what == REWRITE_UPDATE)
54c91640 2242 rewrite_update_dom_walker (CDI_DOMINATORS).walk (entry);
88dbf20f 2243 else
2244 gcc_unreachable ();
2245
88dbf20f 2246 /* Debugging dumps. */
2247 if (dump_file && (dump_flags & TDF_STATS))
2248 {
2249 dump_dfa_stats (dump_file);
c1f445d2 2250 if (var_infos)
88dbf20f 2251 dump_tree_ssa_stats (dump_file);
2252 }
48e1416a 2253
f1f41a6c 2254 block_defs_stack.release ();
88dbf20f 2255
2256 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
2257}
2258
54c91640 2259class mark_def_dom_walker : public dom_walker
e551bd1a 2260{
54c91640 2261public:
2262 mark_def_dom_walker (cdi_direction direction);
2263 ~mark_def_dom_walker ();
f3d9a16c 2264
54c91640 2265 virtual void before_dom_children (basic_block);
f3d9a16c 2266
54c91640 2267private:
e551bd1a 2268 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2269 large enough to accommodate all the variables referenced in the
2270 function, not just the ones we are renaming. */
ae84f584 2271 bitmap m_kills;
54c91640 2272};
4ee9c684 2273
54c91640 2274mark_def_dom_walker::mark_def_dom_walker (cdi_direction direction)
ae84f584 2275 : dom_walker (direction), m_kills (BITMAP_ALLOC (NULL))
54c91640 2276{
2277}
e551bd1a 2278
54c91640 2279mark_def_dom_walker::~mark_def_dom_walker ()
2280{
ae84f584 2281 BITMAP_FREE (m_kills);
54c91640 2282}
e551bd1a 2283
54c91640 2284/* Block processing routine for mark_def_sites. Clear the KILLS bitmap
2285 at the start of each block, and call mark_def_sites for each statement. */
e551bd1a 2286
54c91640 2287void
2288mark_def_dom_walker::before_dom_children (basic_block bb)
2289{
2290 gimple_stmt_iterator gsi;
e551bd1a 2291
ae84f584 2292 bitmap_clear (m_kills);
54c91640 2293 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
ae84f584 2294 mark_def_sites (bb, gsi_stmt (gsi), m_kills);
4ee9c684 2295}
2296
4fb5e5ca 2297/* Initialize internal data needed during renaming. */
2298
2299static void
2300init_ssa_renamer (void)
2301{
4fb5e5ca 2302 cfun->gimple_df->in_ssa_p = false;
2303
2304 /* Allocate memory for the DEF_BLOCKS hash table. */
c1f445d2 2305 gcc_assert (!var_infos);
2306 var_infos = new hash_table<var_info_hasher>
2307 (vec_safe_length (cfun->local_decls));
4fb5e5ca 2308
22d6c74e 2309 bitmap_obstack_initialize (&update_ssa_obstack);
4fb5e5ca 2310}
2311
2312
2313/* Deallocate internal data structures used by the renamer. */
2314
2315static void
2316fini_ssa_renamer (void)
2317{
c1f445d2 2318 delete var_infos;
2319 var_infos = NULL;
4fb5e5ca 2320
22d6c74e 2321 bitmap_obstack_release (&update_ssa_obstack);
2322
e70e8b13 2323 cfun->gimple_df->ssa_renaming_needed = 0;
2324 cfun->gimple_df->rename_vops = 0;
4fb5e5ca 2325 cfun->gimple_df->in_ssa_p = true;
2326}
2327
e551bd1a 2328/* Main entry point into the SSA builder. The renaming process
88dbf20f 2329 proceeds in four main phases:
4ee9c684 2330
88dbf20f 2331 1- Compute dominance frontier and immediate dominators, needed to
2332 insert PHI nodes and rename the function in dominator tree
2333 order.
4ee9c684 2334
54c91640 2335 2- Find and mark all the blocks that define variables.
4ee9c684 2336
88dbf20f 2337 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
4ee9c684 2338
88dbf20f 2339 4- Rename all the blocks (rewrite_blocks) and statements in the program.
4ee9c684 2340
21bdfe89 2341 Steps 3 and 4 are done using the dominator tree walker
88dbf20f 2342 (walk_dominator_tree). */
e551bd1a 2343
65b0537f 2344namespace {
2345
2346const pass_data pass_data_build_ssa =
2347{
2348 GIMPLE_PASS, /* type */
2349 "ssa", /* name */
2350 OPTGROUP_NONE, /* optinfo_flags */
65b0537f 2351 TV_TREE_SSA_OTHER, /* tv_id */
2352 PROP_cfg, /* properties_required */
2353 PROP_ssa, /* properties_provided */
2354 0, /* properties_destroyed */
2355 0, /* todo_flags_start */
8b88439e 2356 TODO_remove_unused_locals, /* todo_flags_finish */
65b0537f 2357};
2358
2359class pass_build_ssa : public gimple_opt_pass
2360{
2361public:
2362 pass_build_ssa (gcc::context *ctxt)
2363 : gimple_opt_pass (pass_data_build_ssa, ctxt)
2364 {}
2365
2366 /* opt_pass methods: */
2367 virtual bool gate (function *fun)
2368 {
2369 /* Do nothing for funcions that was produced already in SSA form. */
2370 return !(fun->curr_properties & PROP_ssa);
2371 }
2372
2373 virtual unsigned int execute (function *);
2374
2375}; // class pass_build_ssa
2376
2377unsigned int
2378pass_build_ssa::execute (function *fun)
4ee9c684 2379{
8a2980be 2380 bitmap_head *dfs;
e551bd1a 2381 basic_block bb;
ec11736b 2382 unsigned i;
48e1416a 2383
88dbf20f 2384 /* Initialize operand data structures. */
65b0537f 2385 init_ssa_operands (fun);
4ee9c684 2386
4fb5e5ca 2387 /* Initialize internal data needed by the renamer. */
2388 init_ssa_renamer ();
2389
88dbf20f 2390 /* Initialize the set of interesting blocks. The callback
2391 mark_def_sites will add to this set those blocks that the renamer
2392 should process. */
65b0537f 2393 interesting_blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
53c5d9d4 2394 bitmap_clear (interesting_blocks);
4ee9c684 2395
71797bf6 2396 /* Initialize dominance frontier. */
65b0537f 2397 dfs = XNEWVEC (bitmap_head, last_basic_block_for_fn (fun));
2398 FOR_EACH_BB_FN (bb, fun)
8a2980be 2399 bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
4ee9c684 2400
88dbf20f 2401 /* 1- Compute dominance frontiers. */
2402 calculate_dominance_info (CDI_DOMINATORS);
e551bd1a 2403 compute_dominance_frontiers (dfs);
4ee9c684 2404
88dbf20f 2405 /* 2- Find and mark definition sites. */
65b0537f 2406 mark_def_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
88dbf20f 2407
2408 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
095dcfa3 2409 insert_phi_nodes (dfs);
4ee9c684 2410
88dbf20f 2411 /* 4- Rename all the blocks. */
65b0537f 2412 rewrite_blocks (ENTRY_BLOCK_PTR_FOR_FN (fun), REWRITE_ALL);
4ee9c684 2413
e551bd1a 2414 /* Free allocated memory. */
65b0537f 2415 FOR_EACH_BB_FN (bb, fun)
8a2980be 2416 bitmap_clear (&dfs[bb->index]);
e551bd1a 2417 free (dfs);
4ee9c684 2418
acf11b62 2419 sbitmap_free (interesting_blocks);
2420
4fb5e5ca 2421 fini_ssa_renamer ();
2422
ec11736b 2423 /* Try to get rid of all gimplifier generated temporaries by making
2424 its SSA names anonymous. This way we can garbage collect them
2425 all after removing unused locals which we do in our TODO. */
2426 for (i = 1; i < num_ssa_names; ++i)
2427 {
2428 tree decl, name = ssa_name (i);
2429 if (!name
2430 || SSA_NAME_IS_DEFAULT_DEF (name))
2431 continue;
2432 decl = SSA_NAME_VAR (name);
2433 if (decl
2434 && TREE_CODE (decl) == VAR_DECL
2435 && !VAR_DECL_IS_VIRTUAL_OPERAND (decl)
382c2352 2436 && DECL_IGNORED_P (decl))
2437 SET_SSA_NAME_VAR_OR_IDENTIFIER (name, DECL_NAME (decl));
ec11736b 2438 }
2439
2a1990e9 2440 return 0;
4ee9c684 2441}
2442
cbe8bda8 2443} // anon namespace
2444
2445gimple_opt_pass *
2446make_pass_build_ssa (gcc::context *ctxt)
2447{
2448 return new pass_build_ssa (ctxt);
2449}
2450
4ee9c684 2451
88dbf20f 2452/* Mark the definition of VAR at STMT and BB as interesting for the
2453 renamer. BLOCKS is the set of blocks that need updating. */
4ee9c684 2454
88dbf20f 2455static void
75a70cf9 2456mark_def_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p)
4ee9c684 2457{
88536563 2458 gcc_checking_assert (bitmap_bit_p (blocks_to_update, bb->index));
75a70cf9 2459 set_register_defs (stmt, true);
88dbf20f 2460
2461 if (insert_phi_p)
2462 {
75a70cf9 2463 bool is_phi_p = gimple_code (stmt) == GIMPLE_PHI;
88dbf20f 2464
88dbf20f 2465 set_def_block (var, bb, is_phi_p);
2466
2467 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2468 site for both itself and all the old names replaced by it. */
2469 if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
2470 {
2471 bitmap_iterator bi;
2472 unsigned i;
2473 bitmap set = names_replaced_by (var);
2474 if (set)
2475 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2476 set_def_block (ssa_name (i), bb, is_phi_p);
2477 }
2478 }
2479}
2480
2481
2482/* Mark the use of VAR at STMT and BB as interesting for the
2483 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2920559d 2484 nodes. */
88dbf20f 2485
2486static inline void
75a70cf9 2487mark_use_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p)
88dbf20f 2488{
75a70cf9 2489 basic_block def_bb = gimple_bb (stmt);
70630fcd 2490
2920559d 2491 mark_block_for_update (def_bb);
2492 mark_block_for_update (bb);
2493
75a70cf9 2494 if (gimple_code (stmt) == GIMPLE_PHI)
1a91d914 2495 mark_phi_for_rewrite (def_bb, as_a <gphi *> (stmt));
70630fcd 2496 else
9845d120 2497 {
2498 set_rewrite_uses (stmt, true);
2499
2500 if (is_gimple_debug (stmt))
2501 return;
2502 }
88dbf20f 2503
2504 /* If VAR has not been defined in BB, then it is live-on-entry
2505 to BB. Note that we cannot just use the block holding VAR's
2506 definition because if VAR is one of the names in OLD_SSA_NAMES,
2507 it will have several definitions (itself and all the names that
2508 replace it). */
2509 if (insert_phi_p)
2510 {
74105c0d 2511 struct def_blocks_d *db_p = get_def_blocks_for (get_common_info (var));
88dbf20f 2512 if (!bitmap_bit_p (db_p->def_blocks, bb->index))
2513 set_livein_block (var, bb);
2514 }
2515}
2516
2517
88dbf20f 2518/* Do a dominator walk starting at BB processing statements that
e70e8b13 2519 reference symbols in SSA operands. This is very similar to
095dcfa3 2520 mark_def_sites, but the scan handles statements whose operands may
2920559d 2521 already be SSA names.
88dbf20f 2522
095dcfa3 2523 If INSERT_PHI_P is true, mark those uses as live in the
2524 corresponding block. This is later used by the PHI placement
4fb5e5ca 2525 algorithm to make PHI pruning decisions.
2526
2527 FIXME. Most of this would be unnecessary if we could associate a
2528 symbol to all the SSA names that reference it. But that
2529 sounds like it would be expensive to maintain. Still, it
2530 would be interesting to see if it makes better sense to do
2531 that. */
88dbf20f 2532
2533static void
2920559d 2534prepare_block_for_update (basic_block bb, bool insert_phi_p)
88dbf20f 2535{
2536 basic_block son;
f50cf978 2537 edge e;
2538 edge_iterator ei;
88dbf20f 2539
2920559d 2540 mark_block_for_update (bb);
2541
88dbf20f 2542 /* Process PHI nodes marking interesting those that define or use
095dcfa3 2543 the symbols that we are interested in. */
1a91d914 2544 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
2545 gsi_next (&si))
88dbf20f 2546 {
1a91d914 2547 gphi *phi = si.phi ();
75a70cf9 2548 tree lhs_sym, lhs = gimple_phi_result (phi);
88dbf20f 2549
e70e8b13 2550 if (TREE_CODE (lhs) == SSA_NAME
0087edc6 2551 && (! virtual_operand_p (lhs)
2552 || ! cfun->gimple_df->rename_vops))
f50cf978 2553 continue;
75a70cf9 2554
0087edc6 2555 lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
e70e8b13 2556 mark_for_renaming (lhs_sym);
f50cf978 2557 mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
2558
2559 /* Mark the uses in phi nodes as interesting. It would be more correct
2560 to process the arguments of the phi nodes of the successor edges of
2561 BB at the end of prepare_block_for_update, however, that turns out
2562 to be significantly more expensive. Doing it here is conservatively
2563 correct -- it may only cause us to believe a value to be live in a
2564 block that also contains its definition, and thus insert a few more
2565 phi nodes for it. */
2566 FOR_EACH_EDGE (e, ei, bb->preds)
75a70cf9 2567 mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
88dbf20f 2568 }
2569
2570 /* Process the statements. */
1a91d914 2571 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
2572 gsi_next (&si))
88dbf20f 2573 {
75a70cf9 2574 gimple stmt;
88dbf20f 2575 ssa_op_iter i;
2576 use_operand_p use_p;
2577 def_operand_p def_p;
48e1416a 2578
75a70cf9 2579 stmt = gsi_stmt (si);
88dbf20f 2580
e70e8b13 2581 if (cfun->gimple_df->rename_vops
2582 && gimple_vuse (stmt))
88dbf20f 2583 {
e70e8b13 2584 tree use = gimple_vuse (stmt);
88dbf20f 2585 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
e70e8b13 2586 mark_for_renaming (sym);
2587 mark_use_interesting (sym, stmt, bb, insert_phi_p);
88dbf20f 2588 }
2589
e70e8b13 2590 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
88dbf20f 2591 {
e70e8b13 2592 tree use = USE_FROM_PTR (use_p);
2593 if (!DECL_P (use))
2594 continue;
2595 mark_for_renaming (use);
2596 mark_use_interesting (use, stmt, bb, insert_phi_p);
2597 }
2598
2599 if (cfun->gimple_df->rename_vops
2600 && gimple_vdef (stmt))
2601 {
2602 tree def = gimple_vdef (stmt);
88dbf20f 2603 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
e70e8b13 2604 mark_for_renaming (sym);
2605 mark_def_interesting (sym, stmt, bb, insert_phi_p);
2606 }
2607
2608 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_DEF)
2609 {
2610 tree def = DEF_FROM_PTR (def_p);
2611 if (!DECL_P (def))
2612 continue;
2613 mark_for_renaming (def);
2614 mark_def_interesting (def, stmt, bb, insert_phi_p);
88dbf20f 2615 }
2616 }
2617
2618 /* Now visit all the blocks dominated by BB. */
095dcfa3 2619 for (son = first_dom_son (CDI_DOMINATORS, bb);
4fb5e5ca 2620 son;
2621 son = next_dom_son (CDI_DOMINATORS, son))
2920559d 2622 prepare_block_for_update (son, insert_phi_p);
095dcfa3 2623}
2624
2625
2626/* Helper for prepare_names_to_update. Mark all the use sites for
2627 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2628 prepare_names_to_update. */
2629
2630static void
2920559d 2631prepare_use_sites_for (tree name, bool insert_phi_p)
095dcfa3 2632{
2633 use_operand_p use_p;
2634 imm_use_iterator iter;
2635
2636 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
2637 {
75a70cf9 2638 gimple stmt = USE_STMT (use_p);
2639 basic_block bb = gimple_bb (stmt);
095dcfa3 2640
75a70cf9 2641 if (gimple_code (stmt) == GIMPLE_PHI)
095dcfa3 2642 {
f50cf978 2643 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
1a91d914 2644 edge e = gimple_phi_arg_edge (as_a <gphi *> (stmt), ix);
f50cf978 2645 mark_use_interesting (name, stmt, e->src, insert_phi_p);
095dcfa3 2646 }
2647 else
2648 {
2649 /* For regular statements, mark this as an interesting use
2650 for NAME. */
2920559d 2651 mark_use_interesting (name, stmt, bb, insert_phi_p);
095dcfa3 2652 }
2653 }
88dbf20f 2654}
2655
2656
095dcfa3 2657/* Helper for prepare_names_to_update. Mark the definition site for
2658 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2659 prepare_names_to_update. */
88dbf20f 2660
2661static void
2920559d 2662prepare_def_site_for (tree name, bool insert_phi_p)
88dbf20f 2663{
75a70cf9 2664 gimple stmt;
88dbf20f 2665 basic_block bb;
2666
88536563 2667 gcc_checking_assert (names_to_release == NULL
2668 || !bitmap_bit_p (names_to_release,
2669 SSA_NAME_VERSION (name)));
88dbf20f 2670
2671 stmt = SSA_NAME_DEF_STMT (name);
75a70cf9 2672 bb = gimple_bb (stmt);
88dbf20f 2673 if (bb)
2674 {
fe672ac0 2675 gcc_checking_assert (bb->index < last_basic_block_for_fn (cfun));
2920559d 2676 mark_block_for_update (bb);
2677 mark_def_interesting (name, stmt, bb, insert_phi_p);
88dbf20f 2678 }
2679}
2680
2681
095dcfa3 2682/* Mark definition and use sites of names in NEW_SSA_NAMES and
2920559d 2683 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2684 PHI nodes for newly created names. */
88dbf20f 2685
2686static void
2920559d 2687prepare_names_to_update (bool insert_phi_p)
88dbf20f 2688{
86c1585a 2689 unsigned i = 0;
88dbf20f 2690 bitmap_iterator bi;
3e790786 2691 sbitmap_iterator sbi;
88dbf20f 2692
2693 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2694 remove it from NEW_SSA_NAMES so that we don't try to visit its
2695 defining basic block (which most likely doesn't exist). Notice
2696 that we cannot do the same with names in OLD_SSA_NAMES because we
2697 want to replace existing instances. */
2698 if (names_to_release)
2699 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
08b7917c 2700 bitmap_clear_bit (new_ssa_names, i);
88dbf20f 2701
095dcfa3 2702 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2703 names may be considered to be live-in on blocks that contain
2704 definitions for their replacements. */
0d211963 2705 EXECUTE_IF_SET_IN_BITMAP (new_ssa_names, 0, i, sbi)
2920559d 2706 prepare_def_site_for (ssa_name (i), insert_phi_p);
095dcfa3 2707
88dbf20f 2708 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2709 OLD_SSA_NAMES, but we have to ignore its definition site. */
0d211963 2710 EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi)
095dcfa3 2711 {
2712 if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
2920559d 2713 prepare_def_site_for (ssa_name (i), insert_phi_p);
2714 prepare_use_sites_for (ssa_name (i), insert_phi_p);
3e790786 2715 }
88dbf20f 2716}
2717
2718
2719/* Dump all the names replaced by NAME to FILE. */
2720
2721void
2722dump_names_replaced_by (FILE *file, tree name)
2723{
2724 unsigned i;
2725 bitmap old_set;
2726 bitmap_iterator bi;
2727
2728 print_generic_expr (file, name, 0);
2729 fprintf (file, " -> { ");
2730
2731 old_set = names_replaced_by (name);
2732 EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
2733 {
2734 print_generic_expr (file, ssa_name (i), 0);
2735 fprintf (file, " ");
2736 }
2737
2738 fprintf (file, "}\n");
2739}
2740
2741
2742/* Dump all the names replaced by NAME to stderr. */
2743
4b987fac 2744DEBUG_FUNCTION void
88dbf20f 2745debug_names_replaced_by (tree name)
2746{
2747 dump_names_replaced_by (stderr, name);
2748}
2749
2750
095dcfa3 2751/* Dump SSA update information to FILE. */
88dbf20f 2752
2753void
095dcfa3 2754dump_update_ssa (FILE *file)
88dbf20f 2755{
86c1585a 2756 unsigned i = 0;
88dbf20f 2757 bitmap_iterator bi;
2758
dd277d48 2759 if (!need_ssa_update_p (cfun))
88dbf20f 2760 return;
2761
53c5d9d4 2762 if (new_ssa_names && bitmap_first_set_bit (new_ssa_names) >= 0)
88dbf20f 2763 {
3e790786 2764 sbitmap_iterator sbi;
2765
88dbf20f 2766 fprintf (file, "\nSSA replacement table\n");
2767 fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
2768 "O_1, ..., O_j\n\n");
2769
0d211963 2770 EXECUTE_IF_SET_IN_BITMAP (new_ssa_names, 0, i, sbi)
3e790786 2771 dump_names_replaced_by (file, ssa_name (i));
88dbf20f 2772 }
2773
e70e8b13 2774 if (symbols_to_rename_set && !bitmap_empty_p (symbols_to_rename_set))
88dbf20f 2775 {
0ffbb1b6 2776 fprintf (file, "\nSymbols to be put in SSA form\n");
e70e8b13 2777 dump_decl_set (file, symbols_to_rename_set);
dd277d48 2778 fprintf (file, "\n");
88dbf20f 2779 }
2780
88dbf20f 2781 if (names_to_release && !bitmap_empty_p (names_to_release))
2782 {
0ffbb1b6 2783 fprintf (file, "\nSSA names to release after updating the SSA web\n\n");
88dbf20f 2784 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2785 {
2786 print_generic_expr (file, ssa_name (i), 0);
2787 fprintf (file, " ");
2788 }
0ffbb1b6 2789 fprintf (file, "\n");
88dbf20f 2790 }
88dbf20f 2791}
2792
2793
095dcfa3 2794/* Dump SSA update information to stderr. */
88dbf20f 2795
4b987fac 2796DEBUG_FUNCTION void
095dcfa3 2797debug_update_ssa (void)
88dbf20f 2798{
095dcfa3 2799 dump_update_ssa (stderr);
88dbf20f 2800}
2801
2802
2803/* Initialize data structures used for incremental SSA updates. */
2804
2805static void
dd277d48 2806init_update_ssa (struct function *fn)
88dbf20f 2807{
095dcfa3 2808 /* Reserve more space than the current number of names. The calls to
88dbf20f 2809 add_new_name_mapping are typically done after creating new SSA
2810 names, so we'll need to reallocate these arrays. */
2811 old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
53c5d9d4 2812 bitmap_clear (old_ssa_names);
88dbf20f 2813
2814 new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
53c5d9d4 2815 bitmap_clear (new_ssa_names);
88dbf20f 2816
5eeafc2d 2817 bitmap_obstack_initialize (&update_ssa_obstack);
2818
88dbf20f 2819 names_to_release = NULL;
dd277d48 2820 update_ssa_initialized_fn = fn;
88dbf20f 2821}
2822
2823
2824/* Deallocate data structures used for incremental SSA updates. */
2825
095dcfa3 2826void
88dbf20f 2827delete_update_ssa (void)
2828{
2829 unsigned i;
2830 bitmap_iterator bi;
2831
2832 sbitmap_free (old_ssa_names);
2833 old_ssa_names = NULL;
2834
2835 sbitmap_free (new_ssa_names);
2836 new_ssa_names = NULL;
2837
e70e8b13 2838 BITMAP_FREE (symbols_to_rename_set);
2839 symbols_to_rename_set = NULL;
f1f41a6c 2840 symbols_to_rename.release ();
88dbf20f 2841
2842 if (names_to_release)
2843 {
2844 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2845 release_ssa_name (ssa_name (i));
2846 BITMAP_FREE (names_to_release);
2847 }
2848
2920559d 2849 clear_ssa_name_info ();
4fb5e5ca 2850
2851 fini_ssa_renamer ();
2852
2853 if (blocks_with_phis_to_rewrite)
2854 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi)
2855 {
1a91d914 2856 vec<gphi *> phis = phis_to_rewrite[i];
f1f41a6c 2857 phis.release ();
2858 phis_to_rewrite[i].create (0);
4fb5e5ca 2859 }
2860
2861 BITMAP_FREE (blocks_with_phis_to_rewrite);
2862 BITMAP_FREE (blocks_to_update);
5eeafc2d 2863
dd277d48 2864 update_ssa_initialized_fn = NULL;
88dbf20f 2865}
2866
2867
2868/* Create a new name for OLD_NAME in statement STMT and replace the
0dc262e0 2869 operand pointed to by DEF_P with the newly created name. If DEF_P
2870 is NULL then STMT should be a GIMPLE assignment.
2871 Return the new name and register the replacement mapping <NEW, OLD> in
88dbf20f 2872 update_ssa's tables. */
2873
2874tree
75a70cf9 2875create_new_def_for (tree old_name, gimple stmt, def_operand_p def)
88dbf20f 2876{
0dc262e0 2877 tree new_name;
88dbf20f 2878
0dc262e0 2879 timevar_push (TV_TREE_SSA_INCREMENTAL);
2880
2881 if (!update_ssa_initialized_fn)
2882 init_update_ssa (cfun);
2883
2884 gcc_assert (update_ssa_initialized_fn == cfun);
2885
2886 new_name = duplicate_ssa_name (old_name, stmt);
2887 if (def)
2888 SET_DEF (def, new_name);
2889 else
2890 gimple_assign_set_lhs (stmt, new_name);
88dbf20f 2891
75a70cf9 2892 if (gimple_code (stmt) == GIMPLE_PHI)
88dbf20f 2893 {
75a70cf9 2894 basic_block bb = gimple_bb (stmt);
88dbf20f 2895
2896 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
f1047120 2897 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = bb_has_abnormal_pred (bb);
88dbf20f 2898 }
2899
0dc262e0 2900 add_new_name_mapping (new_name, old_name);
88dbf20f 2901
2902 /* For the benefit of passes that will be updating the SSA form on
2903 their own, set the current reaching definition of OLD_NAME to be
2904 NEW_NAME. */
74105c0d 2905 get_ssa_name_ann (old_name)->info.current_def = new_name;
88dbf20f 2906
0dc262e0 2907 timevar_pop (TV_TREE_SSA_INCREMENTAL);
88dbf20f 2908
0dc262e0 2909 return new_name;
88dbf20f 2910}
2911
2912
278611f2 2913/* Mark virtual operands of FN for renaming by update_ssa. */
88dbf20f 2914
2915void
278611f2 2916mark_virtual_operands_for_renaming (struct function *fn)
88dbf20f 2917{
278611f2 2918 fn->gimple_df->ssa_renaming_needed = 1;
2919 fn->gimple_df->rename_vops = 1;
88dbf20f 2920}
2921
ba4d2b2f 2922/* Replace all uses of NAME by underlying variable and mark it
2923 for renaming. This assumes the defining statement of NAME is
2924 going to be removed. */
2925
2926void
2927mark_virtual_operand_for_renaming (tree name)
2928{
2929 tree name_var = SSA_NAME_VAR (name);
2930 bool used = false;
2931 imm_use_iterator iter;
2932 use_operand_p use_p;
2933 gimple stmt;
2934
2935 gcc_assert (VAR_DECL_IS_VIRTUAL_OPERAND (name_var));
2936 FOR_EACH_IMM_USE_STMT (stmt, iter, name)
2937 {
2938 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
2939 SET_USE (use_p, name_var);
2940 used = true;
2941 }
2942 if (used)
2943 mark_virtual_operands_for_renaming (cfun);
2944}
2945
2946/* Replace all uses of the virtual PHI result by its underlying variable
2947 and mark it for renaming. This assumes the PHI node is going to be
2948 removed. */
2949
2950void
1a91d914 2951mark_virtual_phi_result_for_renaming (gphi *phi)
ba4d2b2f 2952{
2953 if (dump_file && (dump_flags & TDF_DETAILS))
2954 {
2955 fprintf (dump_file, "Marking result for renaming : ");
2956 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
2957 fprintf (dump_file, "\n");
2958 }
2959
2960 mark_virtual_operand_for_renaming (gimple_phi_result (phi));
2961}
88dbf20f 2962
dd277d48 2963/* Return true if there is any work to be done by update_ssa
2964 for function FN. */
88dbf20f 2965
2966bool
dd277d48 2967need_ssa_update_p (struct function *fn)
88dbf20f 2968{
dd277d48 2969 gcc_assert (fn != NULL);
2970 return (update_ssa_initialized_fn == fn
e70e8b13 2971 || (fn->gimple_df && fn->gimple_df->ssa_renaming_needed));
88dbf20f 2972}
2973
88dbf20f 2974/* Return true if name N has been registered in the replacement table. */
2975
2976bool
75a70cf9 2977name_registered_for_update_p (tree n ATTRIBUTE_UNUSED)
88dbf20f 2978{
dd277d48 2979 if (!update_ssa_initialized_fn)
88dbf20f 2980 return false;
2981
dd277d48 2982 gcc_assert (update_ssa_initialized_fn == cfun);
2983
2984 return is_new_name (n) || is_old_name (n);
88dbf20f 2985}
2986
2987
88dbf20f 2988/* Mark NAME to be released after update_ssa has finished. */
2989
2990void
2991release_ssa_name_after_update_ssa (tree name)
2992{
dd277d48 2993 gcc_assert (cfun && update_ssa_initialized_fn == cfun);
88dbf20f 2994
2995 if (names_to_release == NULL)
2996 names_to_release = BITMAP_ALLOC (NULL);
2997
2998 bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name));
2999}
3000
3001
3002/* Insert new PHI nodes to replace VAR. DFS contains dominance
3003 frontier information. BLOCKS is the set of blocks to be updated.
3004
3005 This is slightly different than the regular PHI insertion
3006 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
3007 real names (i.e., GIMPLE registers) are inserted:
48e1416a 3008
88dbf20f 3009 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
3010 nodes inside the region affected by the block that defines VAR
3011 and the blocks that define all its replacements. All these
095dcfa3 3012 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
88dbf20f 3013
3014 First, we compute the entry point to the region (ENTRY). This is
3015 given by the nearest common dominator to all the definition
3016 blocks. When computing the iterated dominance frontier (IDF), any
3017 block not strictly dominated by ENTRY is ignored.
3018
3019 We then call the standard PHI insertion algorithm with the pruned
3020 IDF.
3021
3022 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
3023 names is not pruned. PHI nodes are inserted at every IDF block. */
3024
3025static void
8a2980be 3026insert_updated_phi_nodes_for (tree var, bitmap_head *dfs, bitmap blocks,
88dbf20f 3027 unsigned update_flags)
3028{
3029 basic_block entry;
3030 struct def_blocks_d *db;
3031 bitmap idf, pruned_idf;
3032 bitmap_iterator bi;
3033 unsigned i;
3034
88dbf20f 3035 if (TREE_CODE (var) == SSA_NAME)
1b4345f7 3036 gcc_checking_assert (is_old_name (var));
88dbf20f 3037 else
e70e8b13 3038 gcc_checking_assert (marked_for_renaming (var));
88dbf20f 3039
3040 /* Get all the definition sites for VAR. */
3041 db = find_def_blocks_for (var);
3042
3043 /* No need to do anything if there were no definitions to VAR. */
3044 if (db == NULL || bitmap_empty_p (db->def_blocks))
3045 return;
3046
3047 /* Compute the initial iterated dominance frontier. */
4fb5e5ca 3048 idf = compute_idf (db->def_blocks, dfs);
88dbf20f 3049 pruned_idf = BITMAP_ALLOC (NULL);
3050
3051 if (TREE_CODE (var) == SSA_NAME)
3052 {
3053 if (update_flags == TODO_update_ssa)
3054 {
3055 /* If doing regular SSA updates for GIMPLE registers, we are
3056 only interested in IDF blocks dominated by the nearest
3057 common dominator of all the definition blocks. */
3058 entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
3059 db->def_blocks);
34154e27 3060 if (entry != ENTRY_BLOCK_PTR_FOR_FN (cfun))
88dbf20f 3061 EXECUTE_IF_SET_IN_BITMAP (idf, 0, i, bi)
f5a6b05f 3062 if (BASIC_BLOCK_FOR_FN (cfun, i) != entry
3063 && dominated_by_p (CDI_DOMINATORS,
3064 BASIC_BLOCK_FOR_FN (cfun, i), entry))
88dbf20f 3065 bitmap_set_bit (pruned_idf, i);
3066 }
3067 else
3068 {
3069 /* Otherwise, do not prune the IDF for VAR. */
88536563 3070 gcc_checking_assert (update_flags == TODO_update_ssa_full_phi);
88dbf20f 3071 bitmap_copy (pruned_idf, idf);
3072 }
3073 }
3074 else
3075 {
3076 /* Otherwise, VAR is a symbol that needs to be put into SSA form
3077 for the first time, so we need to compute the full IDF for
3078 it. */
3079 bitmap_copy (pruned_idf, idf);
88dbf20f 3080 }
3081
3082 if (!bitmap_empty_p (pruned_idf))
3083 {
3084 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
3085 are included in the region to be updated. The feeding blocks
3086 are important to guarantee that the PHI arguments are renamed
3087 properly. */
4fb5e5ca 3088
3089 /* FIXME, this is not needed if we are updating symbols. We are
3090 already starting at the ENTRY block anyway. */
88dbf20f 3091 bitmap_ior_into (blocks, pruned_idf);
3092 EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
3093 {
3094 edge e;
3095 edge_iterator ei;
f5a6b05f 3096 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
88dbf20f 3097
3098 FOR_EACH_EDGE (e, ei, bb->preds)
3099 if (e->src->index >= 0)
3100 bitmap_set_bit (blocks, e->src->index);
3101 }
3102
3103 insert_phi_nodes_for (var, pruned_idf, true);
3104 }
3105
3106 BITMAP_FREE (pruned_idf);
3107 BITMAP_FREE (idf);
3108}
3109
487c79c3 3110/* Sort symbols_to_rename after their DECL_UID. */
3111
3112static int
3113insert_updated_phi_nodes_compare_uids (const void *a, const void *b)
3114{
3115 const_tree syma = *(const const_tree *)a;
3116 const_tree symb = *(const const_tree *)b;
3117 if (DECL_UID (syma) == DECL_UID (symb))
3118 return 0;
3119 return DECL_UID (syma) < DECL_UID (symb) ? -1 : 1;
3120}
88dbf20f 3121
3122/* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3123 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3124
3125 1- The names in OLD_SSA_NAMES dominated by the definitions of
3126 NEW_SSA_NAMES are all re-written to be reached by the
3127 appropriate definition from NEW_SSA_NAMES.
3128
3129 2- If needed, new PHI nodes are added to the iterated dominance
3130 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3131
3132 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
0dc262e0 3133 calling create_new_def_for to create new defs for names that the
88dbf20f 3134 caller wants to replace.
3135
0dc262e0 3136 The caller cretaes the new names to be inserted and the names that need
3137 to be replaced by calling create_new_def_for for each old definition
3138 to be replaced. Note that the function assumes that the
3139 new defining statement has already been inserted in the IL.
88dbf20f 3140
3141 For instance, given the following code:
3142
3143 1 L0:
3144 2 x_1 = PHI (0, x_5)
3145 3 if (x_1 < 10)
3146 4 if (x_1 > 7)
3147 5 y_2 = 0
3148 6 else
3149 7 y_3 = x_1 + x_7
3150 8 endif
3151 9 x_5 = x_1 + 1
3152 10 goto L0;
3153 11 endif
3154
3155 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3156
3157 1 L0:
3158 2 x_1 = PHI (0, x_5)
3159 3 if (x_1 < 10)
3160 4 x_10 = ...
3161 5 if (x_1 > 7)
3162 6 y_2 = 0
3163 7 else
3164 8 x_11 = ...
3165 9 y_3 = x_1 + x_7
3166 10 endif
3167 11 x_5 = x_1 + 1
3168 12 goto L0;
3169 13 endif
3170
3171 We want to replace all the uses of x_1 with the new definitions of
3172 x_10 and x_11. Note that the only uses that should be replaced are
3173 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3174 *not* be replaced (this is why we cannot just mark symbol 'x' for
3175 renaming).
3176
3177 Additionally, we may need to insert a PHI node at line 11 because
3178 that is a merge point for x_10 and x_11. So the use of x_1 at line
3179 11 will be replaced with the new PHI node. The insertion of PHI
3180 nodes is optional. They are not strictly necessary to preserve the
3181 SSA form, and depending on what the caller inserted, they may not
3182 even be useful for the optimizers. UPDATE_FLAGS controls various
3183 aspects of how update_ssa operates, see the documentation for
3184 TODO_update_ssa*. */
3185
3186void
3187update_ssa (unsigned update_flags)
3188{
88dbf20f 3189 basic_block bb, start_bb;
3190 bitmap_iterator bi;
86c1585a 3191 unsigned i = 0;
88dbf20f 3192 bool insert_phi_p;
3e790786 3193 sbitmap_iterator sbi;
e70e8b13 3194 tree sym;
88dbf20f 3195
2670407e 3196 /* Only one update flag should be set. */
3197 gcc_assert (update_flags == TODO_update_ssa
3198 || update_flags == TODO_update_ssa_no_phi
3199 || update_flags == TODO_update_ssa_full_phi
3200 || update_flags == TODO_update_ssa_only_virtuals);
3201
dd277d48 3202 if (!need_ssa_update_p (cfun))
88dbf20f 3203 return;
3204
9e8385f4 3205#ifdef ENABLE_CHECKING
3206 timevar_push (TV_TREE_STMT_VERIFY);
3207
3208 bool err = false;
3209
3210 FOR_EACH_BB_FN (bb, cfun)
3211 {
3212 gimple_stmt_iterator gsi;
3213 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3214 {
3215 gimple stmt = gsi_stmt (gsi);
3216
3217 ssa_op_iter i;
3218 use_operand_p use_p;
3219 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_ALL_USES)
3220 {
3221 tree use = USE_FROM_PTR (use_p);
3222 if (TREE_CODE (use) != SSA_NAME)
3223 continue;
3224
3225 if (SSA_NAME_IN_FREE_LIST (use))
3226 {
3227 error ("statement uses released SSA name:");
3228 debug_gimple_stmt (stmt);
3229 fprintf (stderr, "The use of ");
3230 print_generic_expr (stderr, use, 0);
3231 fprintf (stderr," should have been replaced\n");
3232 err = true;
3233 }
3234 }
3235 }
3236 }
3237
3238 if (err)
3239 internal_error ("cannot update SSA form");
3240
3241 timevar_pop (TV_TREE_STMT_VERIFY);
3242#endif
3243
88dbf20f 3244 timevar_push (TV_TREE_SSA_INCREMENTAL);
3245
0ffbb1b6 3246 if (dump_file && (dump_flags & TDF_DETAILS))
3247 fprintf (dump_file, "\nUpdating SSA:\n");
3248
dd277d48 3249 if (!update_ssa_initialized_fn)
3250 init_update_ssa (cfun);
2670407e 3251 else if (update_flags == TODO_update_ssa_only_virtuals)
3252 {
3253 /* If we only need to update virtuals, remove all the mappings for
3254 real names before proceeding. The caller is responsible for
3255 having dealt with the name mappings before calling update_ssa. */
53c5d9d4 3256 bitmap_clear (old_ssa_names);
3257 bitmap_clear (new_ssa_names);
2670407e 3258 }
3259
dd277d48 3260 gcc_assert (update_ssa_initialized_fn == cfun);
3261
70630fcd 3262 blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
f1f41a6c 3263 if (!phis_to_rewrite.exists ())
fe672ac0 3264 phis_to_rewrite.create (last_basic_block_for_fn (cfun) + 1);
2920559d 3265 blocks_to_update = BITMAP_ALLOC (NULL);
70630fcd 3266
88dbf20f 3267 /* Ensure that the dominance information is up-to-date. */
3268 calculate_dominance_info (CDI_DOMINATORS);
3269
095dcfa3 3270 insert_phi_p = (update_flags != TODO_update_ssa_no_phi);
88dbf20f 3271
095dcfa3 3272 /* If there are names defined in the replacement table, prepare
3273 definition and use sites for all the names in NEW_SSA_NAMES and
3274 OLD_SSA_NAMES. */
53c5d9d4 3275 if (bitmap_first_set_bit (new_ssa_names) >= 0)
095dcfa3 3276 {
2920559d 3277 prepare_names_to_update (insert_phi_p);
095dcfa3 3278
3279 /* If all the names in NEW_SSA_NAMES had been marked for
3280 removal, and there are no symbols to rename, then there's
3281 nothing else to do. */
53c5d9d4 3282 if (bitmap_first_set_bit (new_ssa_names) < 0
e70e8b13 3283 && !cfun->gimple_df->ssa_renaming_needed)
095dcfa3 3284 goto done;
3285 }
3286
3287 /* Next, determine the block at which to start the renaming process. */
e70e8b13 3288 if (cfun->gimple_df->ssa_renaming_needed)
095dcfa3 3289 {
22d6c74e 3290 /* If we rename bare symbols initialize the mapping to
3291 auxiliar info we need to keep track of. */
c1f445d2 3292 var_infos = new hash_table<var_info_hasher> (47);
22d6c74e 3293
095dcfa3 3294 /* If we have to rename some symbols from scratch, we need to
3295 start the process at the root of the CFG. FIXME, it should
3296 be possible to determine the nearest block that had a
3297 definition for each of the symbols that are marked for
3298 updating. For now this seems more work than it's worth. */
34154e27 3299 start_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
095dcfa3 3300
4fb5e5ca 3301 /* Traverse the CFG looking for existing definitions and uses of
e70e8b13 3302 symbols in SSA operands. Mark interesting blocks and
4fb5e5ca 3303 statements and set local live-in information for the PHI
3304 placement heuristics. */
2920559d 3305 prepare_block_for_update (start_bb, insert_phi_p);
f8aa507d 3306
3307#ifdef ENABLE_CHECKING
3308 for (i = 1; i < num_ssa_names; ++i)
3309 {
3310 tree name = ssa_name (i);
3311 if (!name
3312 || virtual_operand_p (name))
3313 continue;
3314
3315 /* For all but virtual operands, which do not have SSA names
3316 with overlapping life ranges, ensure that symbols marked
3317 for renaming do not have existing SSA names associated with
3318 them as we do not re-write them out-of-SSA before going
3319 into SSA for the remaining symbol uses. */
3320 if (marked_for_renaming (SSA_NAME_VAR (name)))
3321 {
3322 fprintf (stderr, "Existing SSA name for symbol marked for "
3323 "renaming: ");
3324 print_generic_expr (stderr, name, TDF_SLIM);
3325 fprintf (stderr, "\n");
3326 internal_error ("SSA corruption");
3327 }
3328 }
3329#endif
88dbf20f 3330 }
3331 else
095dcfa3 3332 {
3333 /* Otherwise, the entry block to the region is the nearest
3334 common dominator for the blocks in BLOCKS. */
2920559d 3335 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3336 blocks_to_update);
095dcfa3 3337 }
88dbf20f 3338
3339 /* If requested, insert PHI nodes at the iterated dominance frontier
095dcfa3 3340 of every block, creating new definitions for names in OLD_SSA_NAMES
e70e8b13 3341 and for symbols found. */
88dbf20f 3342 if (insert_phi_p)
3343 {
8a2980be 3344 bitmap_head *dfs;
66d00abf 3345
3346 /* If the caller requested PHI nodes to be added, compute
3347 dominance frontiers. */
fe672ac0 3348 dfs = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
fc00614f 3349 FOR_EACH_BB_FN (bb, cfun)
8a2980be 3350 bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
66d00abf 3351 compute_dominance_frontiers (dfs);
3352
53c5d9d4 3353 if (bitmap_first_set_bit (old_ssa_names) >= 0)
88dbf20f 3354 {
3e790786 3355 sbitmap_iterator sbi;
3356
095dcfa3 3357 /* insert_update_phi_nodes_for will call add_new_name_mapping
3358 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3359 will grow while we are traversing it (but it will not
3360 gain any new members). Copy OLD_SSA_NAMES to a temporary
3361 for traversal. */
156093aa 3362 sbitmap tmp = sbitmap_alloc (SBITMAP_SIZE (old_ssa_names));
53c5d9d4 3363 bitmap_copy (tmp, old_ssa_names);
0d211963 3364 EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, sbi)
2920559d 3365 insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks_to_update,
3e790786 3366 update_flags);
88dbf20f 3367 sbitmap_free (tmp);
3368 }
3369
487c79c3 3370 symbols_to_rename.qsort (insert_updated_phi_nodes_compare_uids);
f1f41a6c 3371 FOR_EACH_VEC_ELT (symbols_to_rename, i, sym)
e70e8b13 3372 insert_updated_phi_nodes_for (sym, dfs, blocks_to_update,
4fb5e5ca 3373 update_flags);
88dbf20f 3374
fc00614f 3375 FOR_EACH_BB_FN (bb, cfun)
8a2980be 3376 bitmap_clear (&dfs[bb->index]);
66d00abf 3377 free (dfs);
3378
88dbf20f 3379 /* Insertion of PHI nodes may have added blocks to the region.
3380 We need to re-compute START_BB to include the newly added
3381 blocks. */
34154e27 3382 if (start_bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
2920559d 3383 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3384 blocks_to_update);
88dbf20f 3385 }
3386
3387 /* Reset the current definition for name and symbol before renaming
3388 the sub-graph. */
0d211963 3389 EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi)
74105c0d 3390 get_ssa_name_ann (ssa_name (i))->info.current_def = NULL_TREE;
88dbf20f 3391
f1f41a6c 3392 FOR_EACH_VEC_ELT (symbols_to_rename, i, sym)
74105c0d 3393 get_var_info (sym)->info.current_def = NULL_TREE;
88dbf20f 3394
3395 /* Now start the renaming process at START_BB. */
fe672ac0 3396 interesting_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
53c5d9d4 3397 bitmap_clear (interesting_blocks);
2920559d 3398 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
08b7917c 3399 bitmap_set_bit (interesting_blocks, i);
88dbf20f 3400
6bf320fb 3401 rewrite_blocks (start_bb, REWRITE_UPDATE);
88dbf20f 3402
6bf320fb 3403 sbitmap_free (interesting_blocks);
88dbf20f 3404
3405 /* Debugging dumps. */
3406 if (dump_file)
3407 {
3408 int c;
3409 unsigned i;
3410
095dcfa3 3411 dump_update_ssa (dump_file);
88dbf20f 3412
0ffbb1b6 3413 fprintf (dump_file, "Incremental SSA update started at block: %d\n",
88dbf20f 3414 start_bb->index);
3415
3416 c = 0;
2920559d 3417 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
88dbf20f 3418 c++;
fe672ac0 3419 fprintf (dump_file, "Number of blocks in CFG: %d\n",
3420 last_basic_block_for_fn (cfun));
0ffbb1b6 3421 fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n",
fe672ac0 3422 c, PERCENT (c, last_basic_block_for_fn (cfun)));
88dbf20f 3423
3424 if (dump_flags & TDF_DETAILS)
3425 {
7ef97146 3426 fprintf (dump_file, "Affected blocks:");
2920559d 3427 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
7ef97146 3428 fprintf (dump_file, " %u", i);
88dbf20f 3429 fprintf (dump_file, "\n");
3430 }
3431
3432 fprintf (dump_file, "\n\n");
3433 }
3434
3435 /* Free allocated memory. */
095dcfa3 3436done:
88dbf20f 3437 delete_update_ssa ();
3438
3439 timevar_pop (TV_TREE_SSA_INCREMENTAL);
3440}