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