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