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