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