Privatize SSA variables into gimple_df.
[official-gcc.git] / gcc / tree-into-ssa.c
blob3fb2c53181741a5e894131cc14f3e6681ae1deb8
1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "langhooks.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "output.h"
34 #include "expr.h"
35 #include "function.h"
36 #include "diagnostic.h"
37 #include "bitmap.h"
38 #include "tree-flow.h"
39 #include "tree-gimple.h"
40 #include "tree-inline.h"
41 #include "varray.h"
42 #include "timevar.h"
43 #include "hashtab.h"
44 #include "tree-dump.h"
45 #include "tree-pass.h"
46 #include "cfgloop.h"
47 #include "domwalk.h"
48 #include "ggc.h"
49 #include "params.h"
50 #include "vecprim.h"
52 /* This file builds the SSA form for a function as described in:
53 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
54 Computing Static Single Assignment Form and the Control Dependence
55 Graph. ACM Transactions on Programming Languages and Systems,
56 13(4):451-490, October 1991. */
58 /* Structure to map a variable VAR to the set of blocks that contain
59 definitions for VAR. */
60 struct def_blocks_d
62 /* The variable. */
63 tree var;
65 /* Blocks that contain definitions of VAR. Bit I will be set if the
66 Ith block contains a definition of VAR. */
67 bitmap def_blocks;
69 /* Blocks that contain a PHI node for VAR. */
70 bitmap phi_blocks;
72 /* Blocks where VAR is live-on-entry. Similar semantics as
73 DEF_BLOCKS. */
74 bitmap livein_blocks;
78 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
79 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
80 basic blocks where VAR is defined (assigned a new value). It also
81 contains a bitmap of all the blocks where VAR is live-on-entry
82 (i.e., there is a use of VAR in block B without a preceding
83 definition in B). The live-on-entry information is used when
84 computing PHI pruning heuristics. */
85 static htab_t def_blocks;
87 /* Stack of trees used to restore the global currdefs to its original
88 state after completing rewriting of a block and its dominator
89 children. Its elements have the following properties:
91 - An SSA_NAME indicates that the current definition of the
92 underlying variable should be set to the given SSA_NAME.
94 - A _DECL node indicates that the underlying variable has no
95 current definition.
97 - A NULL node is used to mark the last node associated with the
98 current block.
100 - A NULL node at the top entry is used to mark the last node
101 associated with the current block. */
102 static VEC(tree,heap) *block_defs_stack;
104 /* Set of existing SSA names being replaced by update_ssa. */
105 static sbitmap old_ssa_names;
107 /* Set of new SSA names being added by update_ssa. Note that both
108 NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
109 the operations done on them are presence tests. */
110 static sbitmap new_ssa_names;
112 /* Symbols whose SSA form needs to be updated or created for the first
113 time. */
114 static bitmap syms_to_rename;
116 /* Set of SSA names that have been marked to be released after they
117 were registered in the replacement table. They will be finally
118 released after we finish updating the SSA web. */
119 static bitmap names_to_release;
121 /* For each block, the phi nodes that need to be rewritten are stored into
122 these vectors. */
124 typedef VEC(tree, heap) *tree_vec;
125 DEF_VEC_P (tree_vec);
126 DEF_VEC_ALLOC_P (tree_vec, heap);
128 static VEC(tree_vec, heap) *phis_to_rewrite;
130 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
132 static bitmap blocks_with_phis_to_rewrite;
134 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
135 to grow as the callers to register_new_name_mapping will typically
136 create new names on the fly. FIXME. Currently set to 1/3 to avoid
137 frequent reallocations but still need to find a reasonable growth
138 strategy. */
139 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
141 /* Tuple used to represent replacement mappings. */
142 struct repl_map_d
144 tree name;
145 bitmap set;
148 /* NEW -> OLD_SET replacement table. If we are replacing several
149 existing SSA names O_1, O_2, ..., O_j with a new name N_i,
150 then REPL_TBL[N_i] = { O_1, O_2, ..., O_j }. */
151 static htab_t repl_tbl;
153 /* true if register_new_name_mapping needs to initialize the data
154 structures needed by update_ssa. */
155 static bool need_to_initialize_update_ssa_p = true;
157 /* true if update_ssa needs to update virtual operands. */
158 static bool need_to_update_vops_p = false;
160 /* Statistics kept by update_ssa to use in the virtual mapping
161 heuristic. If the number of virtual mappings is beyond certain
162 threshold, the updater will switch from using the mappings into
163 renaming the virtual symbols from scratch. In some cases, the
164 large number of name mappings for virtual names causes significant
165 slowdowns in the PHI insertion code. */
166 struct update_ssa_stats_d
168 unsigned num_virtual_mappings;
169 unsigned num_total_mappings;
170 bitmap virtual_symbols;
171 unsigned num_virtual_symbols;
173 static struct update_ssa_stats_d update_ssa_stats;
175 /* Global data to attach to the main dominator walk structure. */
176 struct mark_def_sites_global_data
178 /* This bitmap contains the variables which are set before they
179 are used in a basic block. */
180 bitmap kills;
182 /* Bitmap of names to rename. */
183 sbitmap names_to_rename;
185 /* Set of blocks that mark_def_sites deems interesting for the
186 renamer to process. */
187 sbitmap interesting_blocks;
191 /* Information stored for SSA names. */
192 struct ssa_name_info
194 /* The actual definition of the ssa name. */
195 tree current_def;
197 /* This field indicates whether or not the variable may need PHI nodes.
198 See the enum's definition for more detailed information about the
199 states. */
200 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
202 /* Age of this record (so that info_for_ssa_name table can be cleared
203 quicky); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
204 are assumed to be null. */
205 unsigned age;
208 /* The information associated with names. */
209 typedef struct ssa_name_info *ssa_name_info_p;
210 DEF_VEC_P (ssa_name_info_p);
211 DEF_VEC_ALLOC_P (ssa_name_info_p, heap);
213 static VEC(ssa_name_info_p, heap) *info_for_ssa_name;
214 static unsigned current_info_for_ssa_name_age;
216 /* The set of blocks affected by update_ssa. */
218 static bitmap blocks_to_update;
220 /* The main entry point to the SSA renamer (rewrite_blocks) may be
221 called several times to do different, but related, tasks.
222 Initially, we need it to rename the whole program into SSA form.
223 At other times, we may need it to only rename into SSA newly
224 exposed symbols. Finally, we can also call it to incrementally fix
225 an already built SSA web. */
226 enum rewrite_mode {
227 /* Convert the whole function into SSA form. */
228 REWRITE_ALL,
230 /* Incrementally update the SSA web by replacing existing SSA
231 names with new ones. See update_ssa for details. */
232 REWRITE_UPDATE
236 /* Use TREE_VISITED to keep track of which statements we want to
237 rename. When renaming a subset of the variables, not all
238 statements will be processed. This is decided in mark_def_sites. */
239 #define REWRITE_THIS_STMT(T) TREE_VISITED (T)
241 /* Use the unsigned flag to keep track of which statements we want to
242 visit when marking new definition sites. This is slightly
243 different than REWRITE_THIS_STMT: it's used by update_ssa to
244 distinguish statements that need to have both uses and defs
245 processed from those that only need to have their defs processed.
246 Statements that define new SSA names only need to have their defs
247 registered, but they don't need to have their uses renamed. */
248 #define REGISTER_DEFS_IN_THIS_STMT(T) (T)->common.unsigned_flag
251 /* Prototypes for debugging functions. */
252 extern void dump_tree_ssa (FILE *);
253 extern void debug_tree_ssa (void);
254 extern void debug_def_blocks (void);
255 extern void dump_tree_ssa_stats (FILE *);
256 extern void debug_tree_ssa_stats (void);
257 void dump_update_ssa (FILE *);
258 void debug_update_ssa (void);
259 void dump_names_replaced_by (FILE *, tree);
260 void debug_names_replaced_by (tree);
262 /* Get the information associated with NAME. */
264 static inline struct ssa_name_info *
265 get_ssa_name_ann (tree name)
267 unsigned ver = SSA_NAME_VERSION (name);
268 unsigned len = VEC_length (ssa_name_info_p, info_for_ssa_name);
269 struct ssa_name_info *info;
271 if (ver >= len)
273 unsigned new_len = num_ssa_names;
275 VEC_reserve (ssa_name_info_p, heap, info_for_ssa_name, new_len);
276 while (len++ < new_len)
278 struct ssa_name_info *info = XCNEW (struct ssa_name_info);
279 info->age = current_info_for_ssa_name_age;
280 VEC_quick_push (ssa_name_info_p, info_for_ssa_name, info);
284 info = VEC_index (ssa_name_info_p, info_for_ssa_name, ver);
285 if (info->age < current_info_for_ssa_name_age)
287 info->need_phi_state = 0;
288 info->current_def = NULL_TREE;
289 info->age = current_info_for_ssa_name_age;
292 return info;
295 /* Clears info for ssa names. */
297 static void
298 clear_ssa_name_info (void)
300 current_info_for_ssa_name_age++;
303 /* Gets phi_state field for VAR. */
305 static inline enum need_phi_state
306 get_phi_state (tree var)
308 if (TREE_CODE (var) == SSA_NAME)
309 return get_ssa_name_ann (var)->need_phi_state;
310 else
311 return var_ann (var)->need_phi_state;
315 /* Sets phi_state field for VAR to STATE. */
317 static inline void
318 set_phi_state (tree var, enum need_phi_state state)
320 if (TREE_CODE (var) == SSA_NAME)
321 get_ssa_name_ann (var)->need_phi_state = state;
322 else
323 var_ann (var)->need_phi_state = state;
327 /* Return the current definition for VAR. */
329 tree
330 get_current_def (tree var)
332 if (TREE_CODE (var) == SSA_NAME)
333 return get_ssa_name_ann (var)->current_def;
334 else
335 return var_ann (var)->current_def;
339 /* Sets current definition of VAR to DEF. */
341 void
342 set_current_def (tree var, tree def)
344 if (TREE_CODE (var) == SSA_NAME)
345 get_ssa_name_ann (var)->current_def = def;
346 else
347 var_ann (var)->current_def = def;
351 /* Compute global livein information given the set of blockx where
352 an object is locally live at the start of the block (LIVEIN)
353 and the set of blocks where the object is defined (DEF_BLOCKS).
355 Note: This routine augments the existing local livein information
356 to include global livein (i.e., it modifies the underlying bitmap
357 for LIVEIN). */
359 void
360 compute_global_livein (bitmap livein, bitmap def_blocks)
362 basic_block bb, *worklist, *tos;
363 unsigned i;
364 bitmap_iterator bi;
366 tos = worklist
367 = (basic_block *) xmalloc (sizeof (basic_block) * (last_basic_block + 1));
369 EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
371 *tos++ = BASIC_BLOCK (i);
374 /* Iterate until the worklist is empty. */
375 while (tos != worklist)
377 edge e;
378 edge_iterator ei;
380 /* Pull a block off the worklist. */
381 bb = *--tos;
383 /* For each predecessor block. */
384 FOR_EACH_EDGE (e, ei, bb->preds)
386 basic_block pred = e->src;
387 int pred_index = pred->index;
389 /* None of this is necessary for the entry block. */
390 if (pred != ENTRY_BLOCK_PTR
391 && ! bitmap_bit_p (livein, pred_index)
392 && ! bitmap_bit_p (def_blocks, pred_index))
394 *tos++ = pred;
395 bitmap_set_bit (livein, pred_index);
400 free (worklist);
404 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
405 all statements in basic block BB. */
407 static void
408 initialize_flags_in_bb (basic_block bb)
410 tree phi, stmt;
411 block_stmt_iterator bsi;
413 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
415 REWRITE_THIS_STMT (phi) = 0;
416 REGISTER_DEFS_IN_THIS_STMT (phi) = 0;
419 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
421 stmt = bsi_stmt (bsi);
422 /* We are going to use the operand cache API, such as
423 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
424 cache for each statement should be up-to-date. */
425 gcc_assert (!stmt_modified_p (stmt));
426 REWRITE_THIS_STMT (stmt) = 0;
427 REGISTER_DEFS_IN_THIS_STMT (stmt) = 0;
431 /* Mark block BB as interesting for update_ssa. */
433 static void
434 mark_block_for_update (basic_block bb)
436 gcc_assert (blocks_to_update != NULL);
437 if (bitmap_bit_p (blocks_to_update, bb->index))
438 return;
439 bitmap_set_bit (blocks_to_update, bb->index);
440 initialize_flags_in_bb (bb);
443 /* Return the set of blocks where variable VAR is defined and the blocks
444 where VAR is live on entry (livein). If no entry is found in
445 DEF_BLOCKS, a new one is created and returned. */
447 static inline struct def_blocks_d *
448 get_def_blocks_for (tree var)
450 struct def_blocks_d db, *db_p;
451 void **slot;
453 db.var = var;
454 slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
455 if (*slot == NULL)
457 db_p = XNEW (struct def_blocks_d);
458 db_p->var = var;
459 db_p->def_blocks = BITMAP_ALLOC (NULL);
460 db_p->phi_blocks = BITMAP_ALLOC (NULL);
461 db_p->livein_blocks = BITMAP_ALLOC (NULL);
462 *slot = (void *) db_p;
464 else
465 db_p = (struct def_blocks_d *) *slot;
467 return db_p;
471 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
472 VAR is defined by a PHI node. */
474 static void
475 set_def_block (tree var, basic_block bb, bool phi_p)
477 struct def_blocks_d *db_p;
478 enum need_phi_state state;
480 state = get_phi_state (var);
481 db_p = get_def_blocks_for (var);
483 /* Set the bit corresponding to the block where VAR is defined. */
484 bitmap_set_bit (db_p->def_blocks, bb->index);
485 if (phi_p)
486 bitmap_set_bit (db_p->phi_blocks, bb->index);
488 /* Keep track of whether or not we may need to insert PHI nodes.
490 If we are in the UNKNOWN state, then this is the first definition
491 of VAR. Additionally, we have not seen any uses of VAR yet, so
492 we do not need a PHI node for this variable at this time (i.e.,
493 transition to NEED_PHI_STATE_NO).
495 If we are in any other state, then we either have multiple definitions
496 of this variable occurring in different blocks or we saw a use of the
497 variable which was not dominated by the block containing the
498 definition(s). In this case we may need a PHI node, so enter
499 state NEED_PHI_STATE_MAYBE. */
500 if (state == NEED_PHI_STATE_UNKNOWN)
501 set_phi_state (var, NEED_PHI_STATE_NO);
502 else
503 set_phi_state (var, NEED_PHI_STATE_MAYBE);
507 /* Mark block BB as having VAR live at the entry to BB. */
509 static void
510 set_livein_block (tree var, basic_block bb)
512 struct def_blocks_d *db_p;
513 enum need_phi_state state = get_phi_state (var);
515 db_p = get_def_blocks_for (var);
517 /* Set the bit corresponding to the block where VAR is live in. */
518 bitmap_set_bit (db_p->livein_blocks, bb->index);
520 /* Keep track of whether or not we may need to insert PHI nodes.
522 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
523 by the single block containing the definition(s) of this variable. If
524 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
525 NEED_PHI_STATE_MAYBE. */
526 if (state == NEED_PHI_STATE_NO)
528 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
530 if (def_block_index == -1
531 || ! dominated_by_p (CDI_DOMINATORS, bb,
532 BASIC_BLOCK (def_block_index)))
533 set_phi_state (var, NEED_PHI_STATE_MAYBE);
535 else
536 set_phi_state (var, NEED_PHI_STATE_MAYBE);
540 /* Return true if symbol SYM is marked for renaming. */
542 static inline bool
543 symbol_marked_for_renaming (tree sym)
545 gcc_assert (DECL_P (sym));
546 return bitmap_bit_p (syms_to_rename, DECL_UID (sym));
550 /* Return true if NAME is in OLD_SSA_NAMES. */
552 static inline bool
553 is_old_name (tree name)
555 unsigned ver = SSA_NAME_VERSION (name);
556 return ver < new_ssa_names->n_bits && TEST_BIT (old_ssa_names, ver);
560 /* Return true if NAME is in NEW_SSA_NAMES. */
562 static inline bool
563 is_new_name (tree name)
565 unsigned ver = SSA_NAME_VERSION (name);
566 return ver < new_ssa_names->n_bits && TEST_BIT (new_ssa_names, ver);
570 /* Hashing and equality functions for REPL_TBL. */
572 static hashval_t
573 repl_map_hash (const void *p)
575 return htab_hash_pointer ((const void *)((const struct repl_map_d *)p)->name);
578 static int
579 repl_map_eq (const void *p1, const void *p2)
581 return ((const struct repl_map_d *)p1)->name
582 == ((const struct repl_map_d *)p2)->name;
585 static void
586 repl_map_free (void *p)
588 BITMAP_FREE (((struct repl_map_d *)p)->set);
589 free (p);
593 /* Return the names replaced by NEW (i.e., REPL_TBL[NEW].SET). */
595 static inline bitmap
596 names_replaced_by (tree new)
598 struct repl_map_d m;
599 void **slot;
601 m.name = new;
602 slot = htab_find_slot (repl_tbl, (void *) &m, NO_INSERT);
604 /* If N was not registered in the replacement table, return NULL. */
605 if (slot == NULL || *slot == NULL)
606 return NULL;
608 return ((struct repl_map_d *) *slot)->set;
612 /* Add OLD to REPL_TBL[NEW].SET. */
614 static inline void
615 add_to_repl_tbl (tree new, tree old)
617 struct repl_map_d m, *mp;
618 void **slot;
620 m.name = new;
621 slot = htab_find_slot (repl_tbl, (void *) &m, INSERT);
622 if (*slot == NULL)
624 mp = XNEW (struct repl_map_d);
625 mp->name = new;
626 mp->set = BITMAP_ALLOC (NULL);
627 *slot = (void *) mp;
629 else
630 mp = (struct repl_map_d *) *slot;
632 bitmap_set_bit (mp->set, SSA_NAME_VERSION (old));
636 /* Add a new mapping NEW -> OLD REPL_TBL. Every entry N_i in REPL_TBL
637 represents the set of names O_1 ... O_j replaced by N_i. This is
638 used by update_ssa and its helpers to introduce new SSA names in an
639 already formed SSA web. */
641 static void
642 add_new_name_mapping (tree new, tree old)
644 timevar_push (TV_TREE_SSA_INCREMENTAL);
646 /* OLD and NEW must be different SSA names for the same symbol. */
647 gcc_assert (new != old && SSA_NAME_VAR (new) == SSA_NAME_VAR (old));
649 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
650 caller may have created new names since the set was created. */
651 if (new_ssa_names->n_bits <= num_ssa_names - 1)
653 unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
654 new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
655 old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
658 /* If this mapping is for virtual names, we will need to update
659 virtual operands. */
660 if (!is_gimple_reg (new))
662 tree sym;
663 size_t uid;
665 need_to_update_vops_p = true;
667 /* Keep counts of virtual mappings and symbols to use in the
668 virtual mapping heuristic. If we have large numbers of
669 virtual mappings for a relatively low number of symbols, it
670 will make more sense to rename the symbols from scratch.
671 Otherwise, the insertion of PHI nodes for each of the old
672 names in these mappings will be very slow. */
673 sym = SSA_NAME_VAR (new);
674 uid = DECL_UID (sym);
675 update_ssa_stats.num_virtual_mappings++;
676 if (!bitmap_bit_p (update_ssa_stats.virtual_symbols, uid))
678 bitmap_set_bit (update_ssa_stats.virtual_symbols, uid);
679 update_ssa_stats.num_virtual_symbols++;
683 /* Update the REPL_TBL table. */
684 add_to_repl_tbl (new, old);
686 /* If OLD had already been registered as a new name, then all the
687 names that OLD replaces should also be replaced by NEW. */
688 if (is_new_name (old))
689 bitmap_ior_into (names_replaced_by (new), names_replaced_by (old));
691 /* Register NEW and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
692 respectively. */
693 SET_BIT (new_ssa_names, SSA_NAME_VERSION (new));
694 SET_BIT (old_ssa_names, SSA_NAME_VERSION (old));
696 /* Update mapping counter to use in the virtual mapping heuristic. */
697 update_ssa_stats.num_total_mappings++;
699 timevar_pop (TV_TREE_SSA_INCREMENTAL);
703 /* Call back for walk_dominator_tree used to collect definition sites
704 for every variable in the function. For every statement S in block
707 1- Variables defined by S in the DEFS of S are marked in the bitmap
708 WALK_DATA->GLOBAL_DATA->KILLS.
710 2- If S uses a variable VAR and there is no preceding kill of VAR,
711 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
713 This information is used to determine which variables are live
714 across block boundaries to reduce the number of PHI nodes
715 we create. */
717 static void
718 mark_def_sites (struct dom_walk_data *walk_data,
719 basic_block bb,
720 block_stmt_iterator bsi)
722 struct mark_def_sites_global_data *gd =
723 (struct mark_def_sites_global_data *) walk_data->global_data;
724 bitmap kills = gd->kills;
725 tree stmt, def;
726 use_operand_p use_p;
727 def_operand_p def_p;
728 ssa_op_iter iter;
730 stmt = bsi_stmt (bsi);
731 update_stmt_if_modified (stmt);
733 gcc_assert (blocks_to_update == NULL);
734 REGISTER_DEFS_IN_THIS_STMT (stmt) = 0;
735 REWRITE_THIS_STMT (stmt) = 0;
737 /* If a variable is used before being set, then the variable is live
738 across a block boundary, so mark it live-on-entry to BB. */
739 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
740 SSA_OP_USE | SSA_OP_VUSE | SSA_OP_VMUSTKILL)
742 tree sym = USE_FROM_PTR (use_p);
743 gcc_assert (DECL_P (sym));
744 if (!bitmap_bit_p (kills, DECL_UID (sym)))
745 set_livein_block (sym, bb);
746 REWRITE_THIS_STMT (stmt) = 1;
749 /* Note that virtual definitions are irrelevant for computing KILLS
750 because a V_MAY_DEF does not constitute a killing definition of the
751 variable. However, the operand of a virtual definitions is a use
752 of the variable, so it may cause the variable to be considered
753 live-on-entry. */
754 FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
756 tree sym = USE_FROM_PTR (use_p);
757 gcc_assert (DECL_P (sym));
758 set_livein_block (sym, bb);
759 set_def_block (sym, bb, false);
760 REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
761 REWRITE_THIS_STMT (stmt) = 1;
764 /* Now process the defs and must-defs made by this statement. */
765 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF | SSA_OP_VMUSTDEF)
767 gcc_assert (DECL_P (def));
768 set_def_block (def, bb, false);
769 bitmap_set_bit (kills, DECL_UID (def));
770 REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
773 /* If we found the statement interesting then also mark the block BB
774 as interesting. */
775 if (REWRITE_THIS_STMT (stmt) || REGISTER_DEFS_IN_THIS_STMT (stmt))
776 SET_BIT (gd->interesting_blocks, bb->index);
779 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
780 in the dfs numbering of the dominance tree. */
782 struct dom_dfsnum
784 /* Basic block whose index this entry corresponds to. */
785 unsigned bb_index;
787 /* The dfs number of this node. */
788 unsigned dfs_num;
791 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
792 for qsort. */
794 static int
795 cmp_dfsnum (const void *a, const void *b)
797 const struct dom_dfsnum *da = a;
798 const struct dom_dfsnum *db = b;
800 return (int) da->dfs_num - (int) db->dfs_num;
803 /* Among the intervals starting at the N points specified in DEFS, find
804 the one that contains S, and return its bb_index. */
806 static unsigned
807 find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
809 unsigned f = 0, t = n, m;
811 while (t > f + 1)
813 m = (f + t) / 2;
814 if (defs[m].dfs_num <= s)
815 f = m;
816 else
817 t = m;
820 return defs[f].bb_index;
823 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
824 KILLS is a bitmap of blocks where the value is defined before any use. */
826 static void
827 prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
829 VEC(int, heap) *worklist;
830 bitmap_iterator bi;
831 unsigned i, b, p, u, top;
832 bitmap live_phis;
833 basic_block def_bb, use_bb;
834 edge e;
835 edge_iterator ei;
836 bitmap to_remove;
837 struct dom_dfsnum *defs;
838 unsigned n_defs, adef;
840 if (bitmap_empty_p (uses))
842 bitmap_clear (phis);
843 return;
846 /* The phi must dominate a use, or an argument of a live phi. Also, we
847 do not create any phi nodes in def blocks, unless they are also livein. */
848 to_remove = BITMAP_ALLOC (NULL);
849 bitmap_and_compl (to_remove, kills, uses);
850 bitmap_and_compl_into (phis, to_remove);
851 if (bitmap_empty_p (phis))
853 BITMAP_FREE (to_remove);
854 return;
857 /* We want to remove the unnecessary phi nodes, but we do not want to compute
858 liveness information, as that may be linear in the size of CFG, and if
859 there are lot of different variables to rewrite, this may lead to quadratic
860 behavior.
862 Instead, we basically emulate standard dce. We put all uses to worklist,
863 then for each of them find the nearest def that dominates them. If this
864 def is a phi node, we mark it live, and if it was not live before, we
865 add the predecessors of its basic block to the worklist.
867 To quickly locate the nearest def that dominates use, we use dfs numbering
868 of the dominance tree (that is already available in order to speed up
869 queries). For each def, we have the interval given by the dfs number on
870 entry to and on exit from the corresponding subtree in the dominance tree.
871 The nearest dominator for a given use is the smallest of these intervals
872 that contains entry and exit dfs numbers for the basic block with the use.
873 If we store the bounds for all the uses to an array and sort it, we can
874 locate the nearest dominating def in logarithmic time by binary search.*/
875 bitmap_ior (to_remove, kills, phis);
876 n_defs = bitmap_count_bits (to_remove);
877 defs = XNEWVEC (struct dom_dfsnum, 2 * n_defs + 1);
878 defs[0].bb_index = 1;
879 defs[0].dfs_num = 0;
880 adef = 1;
881 EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
883 def_bb = BASIC_BLOCK (i);
884 defs[adef].bb_index = i;
885 defs[adef].dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
886 defs[adef + 1].bb_index = i;
887 defs[adef + 1].dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
888 adef += 2;
890 BITMAP_FREE (to_remove);
891 gcc_assert (adef == 2 * n_defs + 1);
892 qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
893 gcc_assert (defs[0].bb_index == 1);
895 /* Now each DEFS entry contains the number of the basic block to that the
896 dfs number corresponds. Change them to the number of basic block that
897 corresponds to the interval following the dfs number. Also, for the
898 dfs_out numbers, increase the dfs number by one (so that it corresponds
899 to the start of the following interval, not to the end of the current
900 one). We use WORKLIST as a stack. */
901 worklist = VEC_alloc (int, heap, n_defs + 1);
902 VEC_quick_push (int, worklist, 1);
903 top = 1;
904 n_defs = 1;
905 for (i = 1; i < adef; i++)
907 b = defs[i].bb_index;
908 if (b == top)
910 /* This is a closing element. Interval corresponding to the top
911 of the stack after removing it follows. */
912 VEC_pop (int, worklist);
913 top = VEC_index (int, worklist, VEC_length (int, worklist) - 1);
914 defs[n_defs].bb_index = top;
915 defs[n_defs].dfs_num = defs[i].dfs_num + 1;
917 else
919 /* Opening element. Nothing to do, just push it to the stack and move
920 it to the correct position. */
921 defs[n_defs].bb_index = defs[i].bb_index;
922 defs[n_defs].dfs_num = defs[i].dfs_num;
923 VEC_quick_push (int, worklist, b);
924 top = b;
927 /* If this interval starts at the same point as the previous one, cancel
928 the previous one. */
929 if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
930 defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
931 else
932 n_defs++;
934 VEC_pop (int, worklist);
935 gcc_assert (VEC_empty (int, worklist));
937 /* Now process the uses. */
938 live_phis = BITMAP_ALLOC (NULL);
939 EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
941 VEC_safe_push (int, heap, worklist, i);
944 while (!VEC_empty (int, worklist))
946 b = VEC_pop (int, worklist);
947 if (b == ENTRY_BLOCK)
948 continue;
950 /* If there is a phi node in USE_BB, it is made live. Otherwise,
951 find the def that dominates the immediate dominator of USE_BB
952 (the kill in USE_BB does not dominate the use). */
953 if (bitmap_bit_p (phis, b))
954 p = b;
955 else
957 use_bb = get_immediate_dominator (CDI_DOMINATORS, BASIC_BLOCK (b));
958 p = find_dfsnum_interval (defs, n_defs,
959 bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
960 if (!bitmap_bit_p (phis, p))
961 continue;
964 /* If the phi node is already live, there is nothing to do. */
965 if (bitmap_bit_p (live_phis, p))
966 continue;
968 /* Mark the phi as live, and add the new uses to the worklist. */
969 bitmap_set_bit (live_phis, p);
970 def_bb = BASIC_BLOCK (p);
971 FOR_EACH_EDGE (e, ei, def_bb->preds)
973 u = e->src->index;
974 if (bitmap_bit_p (uses, u))
975 continue;
977 /* In case there is a kill directly in the use block, do not record
978 the use (this is also necessary for correctness, as we assume that
979 uses dominated by a def directly in their block have been filtered
980 out before). */
981 if (bitmap_bit_p (kills, u))
982 continue;
984 bitmap_set_bit (uses, u);
985 VEC_safe_push (int, heap, worklist, u);
989 VEC_free (int, heap, worklist);
990 bitmap_copy (phis, live_phis);
991 BITMAP_FREE (live_phis);
992 free (defs);
995 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
996 return a bitmap with all the blocks in the iterated dominance
997 frontier of the blocks in DEF_BLOCKS. DFS contains dominance
998 frontier information as returned by compute_dominance_frontiers.
1000 The resulting set of blocks are the potential sites where PHI nodes
1001 are needed. The caller is responsible from freeing the memory
1002 allocated for the return value. */
1004 static bitmap
1005 find_idf (bitmap def_blocks, bitmap *dfs)
1007 bitmap_iterator bi;
1008 unsigned bb_index;
1009 VEC(int,heap) *work_stack;
1010 bitmap phi_insertion_points;
1012 work_stack = VEC_alloc (int, heap, n_basic_blocks);
1013 phi_insertion_points = BITMAP_ALLOC (NULL);
1015 /* Seed the work list with all the blocks in DEF_BLOCKS. */
1016 EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
1017 /* We use VEC_quick_push here for speed. This is safe because we
1018 know that the number of definition blocks is no greater than
1019 the number of basic blocks, which is the initial capacity of
1020 WORK_STACK. */
1021 VEC_quick_push (int, work_stack, bb_index);
1023 /* Pop a block off the worklist, add every block that appears in
1024 the original block's DF that we have not already processed to
1025 the worklist. Iterate until the worklist is empty. Blocks
1026 which are added to the worklist are potential sites for
1027 PHI nodes. */
1028 while (VEC_length (int, work_stack) > 0)
1030 bb_index = VEC_pop (int, work_stack);
1032 /* Since the registration of NEW -> OLD name mappings is done
1033 separately from the call to update_ssa, when updating the SSA
1034 form, the basic blocks where new and/or old names are defined
1035 may have disappeared by CFG cleanup calls. In this case,
1036 we may pull a non-existing block from the work stack. */
1037 gcc_assert (bb_index < (unsigned) last_basic_block);
1039 EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index], phi_insertion_points,
1040 0, bb_index, bi)
1042 /* Use a safe push because if there is a definition of VAR
1043 in every basic block, then WORK_STACK may eventually have
1044 more than N_BASIC_BLOCK entries. */
1045 VEC_safe_push (int, heap, work_stack, bb_index);
1046 bitmap_set_bit (phi_insertion_points, bb_index);
1050 VEC_free (int, heap, work_stack);
1052 return phi_insertion_points;
1056 /* Return the set of blocks where variable VAR is defined and the blocks
1057 where VAR is live on entry (livein). Return NULL, if no entry is
1058 found in DEF_BLOCKS. */
1060 static inline struct def_blocks_d *
1061 find_def_blocks_for (tree var)
1063 struct def_blocks_d dm;
1064 dm.var = var;
1065 return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1069 /* Retrieve or create a default definition for symbol SYM. */
1071 static inline tree
1072 get_default_def_for (tree sym)
1074 tree ddef = gimple_default_def (cfun, sym);
1076 if (ddef == NULL_TREE)
1078 ddef = make_ssa_name (sym, build_empty_stmt ());
1079 set_default_def (sym, ddef);
1082 return ddef;
1086 /* Marks phi node PHI in basic block BB for rewrite. */
1088 static void
1089 mark_phi_for_rewrite (basic_block bb, tree phi)
1091 tree_vec phis;
1092 unsigned i, idx = bb->index;
1094 if (REWRITE_THIS_STMT (phi))
1095 return;
1096 REWRITE_THIS_STMT (phi) = 1;
1098 if (!blocks_with_phis_to_rewrite)
1099 return;
1101 bitmap_set_bit (blocks_with_phis_to_rewrite, idx);
1102 VEC_reserve (tree_vec, heap, phis_to_rewrite, last_basic_block + 1);
1103 for (i = VEC_length (tree_vec, phis_to_rewrite); i <= idx; i++)
1104 VEC_quick_push (tree_vec, phis_to_rewrite, NULL);
1106 phis = VEC_index (tree_vec, phis_to_rewrite, idx);
1107 if (!phis)
1108 phis = VEC_alloc (tree, heap, 10);
1110 VEC_safe_push (tree, heap, phis, phi);
1111 VEC_replace (tree_vec, phis_to_rewrite, idx, phis);
1114 /* Insert PHI nodes for variable VAR using the iterated dominance
1115 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
1116 function assumes that the caller is incrementally updating the SSA
1117 form, in which case (1) VAR is assumed to be an SSA name, (2) a new
1118 SSA name is created for VAR's symbol, and, (3) all the arguments
1119 for the newly created PHI node are set to VAR.
1121 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1122 PHI node for VAR. On exit, only the nodes that received a PHI node
1123 for VAR will be present in PHI_INSERTION_POINTS. */
1125 static void
1126 insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
1128 unsigned bb_index;
1129 edge e;
1130 tree phi;
1131 basic_block bb;
1132 bitmap_iterator bi;
1133 struct def_blocks_d *def_map;
1135 def_map = find_def_blocks_for (var);
1136 gcc_assert (def_map);
1138 /* Remove the blocks where we already have PHI nodes for VAR. */
1139 bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
1141 /* Remove obviously useless phi nodes. */
1142 prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
1143 def_map->livein_blocks);
1145 /* And insert the PHI nodes. */
1146 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
1148 bb = BASIC_BLOCK (bb_index);
1149 if (update_p)
1150 mark_block_for_update (bb);
1152 if (update_p && TREE_CODE (var) == SSA_NAME)
1154 /* If we are rewriting SSA names, create the LHS of the PHI
1155 node by duplicating VAR. This is useful in the case of
1156 pointers, to also duplicate pointer attributes (alias
1157 information, in particular). */
1158 edge_iterator ei;
1159 tree new_lhs;
1161 phi = create_phi_node (var, bb);
1162 new_lhs = duplicate_ssa_name (var, phi);
1163 SET_PHI_RESULT (phi, new_lhs);
1164 add_new_name_mapping (new_lhs, var);
1166 /* Add VAR to every argument slot of PHI. We need VAR in
1167 every argument so that rewrite_update_phi_arguments knows
1168 which name is this PHI node replacing. If VAR is a
1169 symbol marked for renaming, this is not necessary, the
1170 renamer will use the symbol on the LHS to get its
1171 reaching definition. */
1172 FOR_EACH_EDGE (e, ei, bb->preds)
1173 add_phi_arg (phi, var, e);
1175 else
1177 tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
1178 phi = create_phi_node (sym, bb);
1181 /* Mark this PHI node as interesting for update_ssa. */
1182 REGISTER_DEFS_IN_THIS_STMT (phi) = 1;
1183 mark_phi_for_rewrite (bb, phi);
1188 /* Insert PHI nodes at the dominance frontier of blocks with variable
1189 definitions. DFS contains the dominance frontier information for
1190 the flowgraph. PHI nodes will only be inserted at the dominance
1191 frontier of definition blocks for variables whose NEED_PHI_STATE
1192 annotation is marked as ``maybe'' or ``unknown'' (computed by
1193 mark_def_sites). */
1195 static void
1196 insert_phi_nodes (bitmap *dfs)
1198 referenced_var_iterator rvi;
1199 tree var;
1201 timevar_push (TV_TREE_INSERT_PHI_NODES);
1203 FOR_EACH_REFERENCED_VAR (var, rvi)
1205 struct def_blocks_d *def_map;
1206 bitmap idf;
1208 def_map = find_def_blocks_for (var);
1209 if (def_map == NULL)
1210 continue;
1212 if (get_phi_state (var) != NEED_PHI_STATE_NO)
1214 idf = find_idf (def_map->def_blocks, dfs);
1215 insert_phi_nodes_for (var, idf, false);
1216 BITMAP_FREE (idf);
1220 timevar_pop (TV_TREE_INSERT_PHI_NODES);
1224 /* Register DEF (an SSA_NAME) to be a new definition for its underlying
1225 variable (SSA_NAME_VAR (DEF)) and push VAR's current reaching definition
1226 into the stack pointed to by BLOCK_DEFS_P. */
1228 void
1229 register_new_def (tree def, VEC(tree,heap) **block_defs_p)
1231 tree var = SSA_NAME_VAR (def);
1232 tree currdef;
1234 /* If this variable is set in a single basic block and all uses are
1235 dominated by the set(s) in that single basic block, then there is
1236 no reason to record anything for this variable in the block local
1237 definition stacks. Doing so just wastes time and memory.
1239 This is the same test to prune the set of variables which may
1240 need PHI nodes. So we just use that information since it's already
1241 computed and available for us to use. */
1242 if (get_phi_state (var) == NEED_PHI_STATE_NO)
1244 set_current_def (var, def);
1245 return;
1248 currdef = get_current_def (var);
1250 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
1251 later used by the dominator tree callbacks to restore the reaching
1252 definitions for all the variables defined in the block after a recursive
1253 visit to all its immediately dominated blocks. If there is no current
1254 reaching definition, then just record the underlying _DECL node. */
1255 VEC_safe_push (tree, heap, *block_defs_p, currdef ? currdef : var);
1257 /* Set the current reaching definition for VAR to be DEF. */
1258 set_current_def (var, def);
1262 /* Perform a depth-first traversal of the dominator tree looking for
1263 variables to rename. BB is the block where to start searching.
1264 Renaming is a five step process:
1266 1- Every definition made by PHI nodes at the start of the blocks is
1267 registered as the current definition for the corresponding variable.
1269 2- Every statement in BB is rewritten. USE and VUSE operands are
1270 rewritten with their corresponding reaching definition. DEF and
1271 VDEF targets are registered as new definitions.
1273 3- All the PHI nodes in successor blocks of BB are visited. The
1274 argument corresponding to BB is replaced with its current reaching
1275 definition.
1277 4- Recursively rewrite every dominator child block of BB.
1279 5- Restore (in reverse order) the current reaching definition for every
1280 new definition introduced in this block. This is done so that when
1281 we return from the recursive call, all the current reaching
1282 definitions are restored to the names that were valid in the
1283 dominator parent of BB. */
1285 /* SSA Rewriting Step 1. Initialization, create a block local stack
1286 of reaching definitions for new SSA names produced in this block
1287 (BLOCK_DEFS). Register new definitions for every PHI node in the
1288 block. */
1290 static void
1291 rewrite_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1292 basic_block bb)
1294 tree phi;
1296 if (dump_file && (dump_flags & TDF_DETAILS))
1297 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
1299 /* Mark the unwind point for this block. */
1300 VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
1302 /* Step 1. Register new definitions for every PHI node in the block.
1303 Conceptually, all the PHI nodes are executed in parallel and each PHI
1304 node introduces a new version for the associated variable. */
1305 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1307 tree result = PHI_RESULT (phi);
1308 register_new_def (result, &block_defs_stack);
1313 /* Return the current definition for variable VAR. If none is found,
1314 create a new SSA name to act as the zeroth definition for VAR. If VAR
1315 is call clobbered and there exists a more recent definition of
1316 GLOBAL_VAR, return the definition for GLOBAL_VAR. This means that VAR
1317 has been clobbered by a function call since its last assignment. */
1319 static tree
1320 get_reaching_def (tree var)
1322 tree currdef_var, avar;
1324 /* Lookup the current reaching definition for VAR. */
1325 currdef_var = get_current_def (var);
1327 /* If there is no reaching definition for VAR, create and register a
1328 default definition for it (if needed). */
1329 if (currdef_var == NULL_TREE)
1331 avar = DECL_P (var) ? var : SSA_NAME_VAR (var);
1332 currdef_var = get_default_def_for (avar);
1333 set_current_def (var, currdef_var);
1336 /* Return the current reaching definition for VAR, or the default
1337 definition, if we had to create one. */
1338 return currdef_var;
1342 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1343 the block with its immediate reaching definitions. Update the current
1344 definition of a variable when a new real or virtual definition is found. */
1346 static void
1347 rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1348 basic_block bb ATTRIBUTE_UNUSED,
1349 block_stmt_iterator si)
1351 tree stmt;
1352 use_operand_p use_p;
1353 def_operand_p def_p;
1354 ssa_op_iter iter;
1356 stmt = bsi_stmt (si);
1358 /* If mark_def_sites decided that we don't need to rewrite this
1359 statement, ignore it. */
1360 gcc_assert (blocks_to_update == NULL);
1361 if (!REWRITE_THIS_STMT (stmt) && !REGISTER_DEFS_IN_THIS_STMT (stmt))
1362 return;
1364 if (dump_file && (dump_flags & TDF_DETAILS))
1366 fprintf (dump_file, "Renaming statement ");
1367 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1368 fprintf (dump_file, "\n");
1371 /* Step 1. Rewrite USES and VUSES in the statement. */
1372 if (REWRITE_THIS_STMT (stmt))
1373 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
1374 SSA_OP_ALL_USES|SSA_OP_ALL_KILLS)
1376 tree var = USE_FROM_PTR (use_p);
1377 gcc_assert (DECL_P (var));
1378 SET_USE (use_p, get_reaching_def (var));
1381 /* Step 2. Register the statement's DEF and VDEF operands. */
1382 if (REGISTER_DEFS_IN_THIS_STMT (stmt))
1383 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1385 tree var = DEF_FROM_PTR (def_p);
1386 gcc_assert (DECL_P (var));
1387 SET_DEF (def_p, make_ssa_name (var, stmt));
1388 register_new_def (DEF_FROM_PTR (def_p), &block_defs_stack);
1393 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1394 PHI nodes. For every PHI node found, add a new argument containing the
1395 current reaching definition for the variable and the edge through which
1396 that definition is reaching the PHI node. */
1398 static void
1399 rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1400 basic_block bb)
1402 edge e;
1403 edge_iterator ei;
1405 FOR_EACH_EDGE (e, ei, bb->succs)
1407 tree phi;
1409 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
1411 tree currdef;
1412 currdef = get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi)));
1413 add_phi_arg (phi, currdef, e);
1419 /* Called after visiting basic block BB. Restore CURRDEFS to its
1420 original value. */
1422 static void
1423 rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1424 basic_block bb ATTRIBUTE_UNUSED)
1426 /* Restore CURRDEFS to its original state. */
1427 while (VEC_length (tree, block_defs_stack) > 0)
1429 tree tmp = VEC_pop (tree, block_defs_stack);
1430 tree saved_def, var;
1432 if (tmp == NULL_TREE)
1433 break;
1435 /* If we recorded an SSA_NAME, then make the SSA_NAME the current
1436 definition of its underlying variable. If we recorded anything
1437 else, it must have been an _DECL node and its current reaching
1438 definition must have been NULL. */
1439 if (TREE_CODE (tmp) == SSA_NAME)
1441 saved_def = tmp;
1442 var = SSA_NAME_VAR (saved_def);
1444 else
1446 saved_def = NULL;
1447 var = tmp;
1450 set_current_def (var, saved_def);
1455 /* Dump SSA information to FILE. */
1457 void
1458 dump_tree_ssa (FILE *file)
1460 basic_block bb;
1461 const char *funcname
1462 = lang_hooks.decl_printable_name (current_function_decl, 2);
1464 fprintf (file, "SSA information for %s\n\n", funcname);
1466 FOR_EACH_BB (bb)
1468 dump_bb (bb, file, 0);
1469 fputs (" ", file);
1470 print_generic_stmt (file, phi_nodes (bb), dump_flags);
1471 fputs ("\n\n", file);
1476 /* Dump SSA information to stderr. */
1478 void
1479 debug_tree_ssa (void)
1481 dump_tree_ssa (stderr);
1485 /* Dump statistics for the hash table HTAB. */
1487 static void
1488 htab_statistics (FILE *file, htab_t htab)
1490 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1491 (long) htab_size (htab),
1492 (long) htab_elements (htab),
1493 htab_collisions (htab));
1497 /* Dump SSA statistics on FILE. */
1499 void
1500 dump_tree_ssa_stats (FILE *file)
1502 fprintf (file, "\nHash table statistics:\n");
1504 fprintf (file, " def_blocks: ");
1505 htab_statistics (file, def_blocks);
1507 fprintf (file, "\n");
1511 /* Dump SSA statistics on stderr. */
1513 void
1514 debug_tree_ssa_stats (void)
1516 dump_tree_ssa_stats (stderr);
1520 /* Hashing and equality functions for DEF_BLOCKS. */
1522 static hashval_t
1523 def_blocks_hash (const void *p)
1525 return htab_hash_pointer
1526 ((const void *)((const struct def_blocks_d *)p)->var);
1529 static int
1530 def_blocks_eq (const void *p1, const void *p2)
1532 return ((const struct def_blocks_d *)p1)->var
1533 == ((const struct def_blocks_d *)p2)->var;
1537 /* Free memory allocated by one entry in DEF_BLOCKS. */
1539 static void
1540 def_blocks_free (void *p)
1542 struct def_blocks_d *entry = (struct def_blocks_d *) p;
1543 BITMAP_FREE (entry->def_blocks);
1544 BITMAP_FREE (entry->phi_blocks);
1545 BITMAP_FREE (entry->livein_blocks);
1546 free (entry);
1550 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1552 static int
1553 debug_def_blocks_r (void **slot, void *data ATTRIBUTE_UNUSED)
1555 struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1557 fprintf (stderr, "VAR: ");
1558 print_generic_expr (stderr, db_p->var, dump_flags);
1559 bitmap_print (stderr, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
1560 bitmap_print (stderr, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}\n");
1562 return 1;
1566 /* Dump the DEF_BLOCKS hash table on stderr. */
1568 void
1569 debug_def_blocks (void)
1571 htab_traverse (def_blocks, debug_def_blocks_r, NULL);
1575 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1577 static inline void
1578 register_new_update_single (tree new_name, tree old_name)
1580 tree currdef = get_current_def (old_name);
1582 /* Push the current reaching definition into *BLOCK_DEFS_P.
1583 This stack is later used by the dominator tree callbacks to
1584 restore the reaching definitions for all the variables
1585 defined in the block after a recursive visit to all its
1586 immediately dominated blocks. */
1587 VEC_reserve (tree, heap, block_defs_stack, 2);
1588 VEC_quick_push (tree, block_defs_stack, currdef);
1589 VEC_quick_push (tree, block_defs_stack, old_name);
1591 /* Set the current reaching definition for OLD_NAME to be
1592 NEW_NAME. */
1593 set_current_def (old_name, new_name);
1597 /* Register NEW_NAME to be the new reaching definition for all the
1598 names in OLD_NAMES. Used by the incremental SSA update routines to
1599 replace old SSA names with new ones. */
1601 static inline void
1602 register_new_update_set (tree new_name, bitmap old_names)
1604 bitmap_iterator bi;
1605 unsigned i;
1607 EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
1608 register_new_update_single (new_name, ssa_name (i));
1612 /* Initialization of block data structures for the incremental SSA
1613 update pass. Create a block local stack of reaching definitions
1614 for new SSA names produced in this block (BLOCK_DEFS). Register
1615 new definitions for every PHI node in the block. */
1617 static void
1618 rewrite_update_init_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1619 basic_block bb)
1621 edge e;
1622 edge_iterator ei;
1623 tree phi;
1624 bool is_abnormal_phi;
1626 if (dump_file && (dump_flags & TDF_DETAILS))
1627 fprintf (dump_file, "\n\nRegistering new PHI nodes in block #%d\n\n",
1628 bb->index);
1630 /* Mark the unwind point for this block. */
1631 VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
1633 if (!bitmap_bit_p (blocks_to_update, bb->index))
1634 return;
1636 /* Mark the LHS if any of the arguments flows through an abnormal
1637 edge. */
1638 is_abnormal_phi = false;
1639 FOR_EACH_EDGE (e, ei, bb->preds)
1640 if (e->flags & EDGE_ABNORMAL)
1642 is_abnormal_phi = true;
1643 break;
1646 /* If any of the PHI nodes is a replacement for a name in
1647 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
1648 register it as a new definition for its corresponding name. Also
1649 register definitions for names whose underlying symbols are
1650 marked for renaming. */
1652 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1654 tree lhs, lhs_sym;
1656 if (!REGISTER_DEFS_IN_THIS_STMT (phi))
1657 continue;
1659 lhs = PHI_RESULT (phi);
1660 lhs_sym = SSA_NAME_VAR (lhs);
1662 if (symbol_marked_for_renaming (lhs_sym))
1663 register_new_update_single (lhs, lhs_sym);
1664 else
1666 /* If LHS is a new name, register a new definition for all
1667 the names replaced by LHS. */
1668 if (is_new_name (lhs))
1669 register_new_update_set (lhs, names_replaced_by (lhs));
1671 /* If LHS is an OLD name, register it as a new definition
1672 for itself. */
1673 if (is_old_name (lhs))
1674 register_new_update_single (lhs, lhs);
1677 if (is_abnormal_phi)
1678 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
1683 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
1684 the current reaching definition of every name re-written in BB to
1685 the original reaching definition before visiting BB. This
1686 unwinding must be done in the opposite order to what is done in
1687 register_new_update_set. */
1689 static void
1690 rewrite_update_fini_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1691 basic_block bb ATTRIBUTE_UNUSED)
1693 while (VEC_length (tree, block_defs_stack) > 0)
1695 tree var = VEC_pop (tree, block_defs_stack);
1696 tree saved_def;
1698 /* NULL indicates the unwind stop point for this block (see
1699 rewrite_update_init_block). */
1700 if (var == NULL)
1701 return;
1703 saved_def = VEC_pop (tree, block_defs_stack);
1704 set_current_def (var, saved_def);
1709 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1710 it is a symbol marked for renaming, replace it with USE_P's current
1711 reaching definition. */
1713 static inline void
1714 maybe_replace_use (use_operand_p use_p)
1716 tree rdef = NULL_TREE;
1717 tree use = USE_FROM_PTR (use_p);
1718 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1720 if (symbol_marked_for_renaming (sym))
1721 rdef = get_reaching_def (sym);
1722 else if (is_old_name (use))
1723 rdef = get_reaching_def (use);
1725 if (rdef && rdef != use)
1726 SET_USE (use_p, rdef);
1730 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1731 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1732 register it as the current definition for the names replaced by
1733 DEF_P. */
1735 static inline void
1736 maybe_register_def (def_operand_p def_p, tree stmt)
1738 tree def = DEF_FROM_PTR (def_p);
1739 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
1741 /* If DEF is a naked symbol that needs renaming, create a
1742 new name for it. */
1743 if (symbol_marked_for_renaming (sym))
1745 if (DECL_P (def))
1747 def = make_ssa_name (def, stmt);
1748 SET_DEF (def_p, def);
1751 register_new_update_single (def, sym);
1753 else
1755 /* If DEF is a new name, register it as a new definition
1756 for all the names replaced by DEF. */
1757 if (is_new_name (def))
1758 register_new_update_set (def, names_replaced_by (def));
1760 /* If DEF is an old name, register DEF as a new
1761 definition for itself. */
1762 if (is_old_name (def))
1763 register_new_update_single (def, def);
1768 /* Update every variable used in the statement pointed-to by SI. The
1769 statement is assumed to be in SSA form already. Names in
1770 OLD_SSA_NAMES used by SI will be updated to their current reaching
1771 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1772 will be registered as a new definition for their corresponding name
1773 in OLD_SSA_NAMES. */
1775 static void
1776 rewrite_update_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1777 basic_block bb ATTRIBUTE_UNUSED,
1778 block_stmt_iterator si)
1780 stmt_ann_t ann;
1781 tree stmt;
1782 use_operand_p use_p;
1783 def_operand_p def_p;
1784 ssa_op_iter iter;
1786 stmt = bsi_stmt (si);
1787 ann = stmt_ann (stmt);
1789 gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
1791 /* Only update marked statements. */
1792 if (!REWRITE_THIS_STMT (stmt) && !REGISTER_DEFS_IN_THIS_STMT (stmt))
1793 return;
1795 if (dump_file && (dump_flags & TDF_DETAILS))
1797 fprintf (dump_file, "Updating SSA information for statement ");
1798 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1799 fprintf (dump_file, "\n");
1802 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1803 symbol is marked for renaming. */
1804 if (REWRITE_THIS_STMT (stmt))
1806 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1807 maybe_replace_use (use_p);
1809 if (need_to_update_vops_p)
1810 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
1811 SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_KILLS)
1812 maybe_replace_use (use_p);
1815 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
1816 Also register definitions for names whose underlying symbol is
1817 marked for renaming. */
1818 if (REGISTER_DEFS_IN_THIS_STMT (stmt))
1820 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1821 maybe_register_def (def_p, stmt);
1823 if (need_to_update_vops_p)
1824 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_VIRTUAL_DEFS)
1825 maybe_register_def (def_p, stmt);
1830 /* Replace the operand pointed to by USE_P with USE's current reaching
1831 definition. */
1833 static inline void
1834 replace_use (use_operand_p use_p, tree use)
1836 tree rdef = get_reaching_def (use);
1837 if (rdef != use)
1838 SET_USE (use_p, rdef);
1842 /* Visit all the successor blocks of BB looking for PHI nodes. For
1843 every PHI node found, check if any of its arguments is in
1844 OLD_SSA_NAMES. If so, and if the argument has a current reaching
1845 definition, replace it. */
1847 static void
1848 rewrite_update_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1849 basic_block bb)
1851 edge e;
1852 edge_iterator ei;
1853 unsigned i;
1855 FOR_EACH_EDGE (e, ei, bb->succs)
1857 tree phi;
1858 tree_vec phis;
1860 if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
1861 continue;
1863 phis = VEC_index (tree_vec, phis_to_rewrite, e->dest->index);
1864 for (i = 0; VEC_iterate (tree, phis, i, phi); i++)
1866 tree arg;
1867 use_operand_p arg_p;
1869 gcc_assert (REWRITE_THIS_STMT (phi));
1871 arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
1872 arg = USE_FROM_PTR (arg_p);
1874 if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
1875 continue;
1877 if (arg == NULL_TREE)
1879 /* When updating a PHI node for a recently introduced
1880 symbol we may find NULL arguments. That's why we
1881 take the symbol from the LHS of the PHI node. */
1882 replace_use (arg_p, SSA_NAME_VAR (PHI_RESULT (phi)));
1884 else
1886 tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
1888 if (symbol_marked_for_renaming (sym))
1889 replace_use (arg_p, sym);
1890 else if (is_old_name (arg))
1891 replace_use (arg_p, arg);
1894 if (e->flags & EDGE_ABNORMAL)
1895 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
1901 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
1902 form.
1904 ENTRY indicates the block where to start. Every block dominated by
1905 ENTRY will be rewritten.
1907 WHAT indicates what actions will be taken by the renamer (see enum
1908 rewrite_mode).
1910 BLOCKS are the set of interesting blocks for the dominator walker
1911 to process. If this set is NULL, then all the nodes dominated
1912 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
1913 are not present in BLOCKS are ignored. */
1915 static void
1916 rewrite_blocks (basic_block entry, enum rewrite_mode what, sbitmap blocks)
1918 struct dom_walk_data walk_data;
1920 /* Rewrite all the basic blocks in the program. */
1921 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1923 /* Setup callbacks for the generic dominator tree walker. */
1924 memset (&walk_data, 0, sizeof (walk_data));
1926 walk_data.dom_direction = CDI_DOMINATORS;
1927 walk_data.interesting_blocks = blocks;
1929 if (what == REWRITE_UPDATE)
1930 walk_data.before_dom_children_before_stmts = rewrite_update_init_block;
1931 else
1932 walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
1934 if (what == REWRITE_ALL)
1935 walk_data.before_dom_children_walk_stmts = rewrite_stmt;
1936 else if (what == REWRITE_UPDATE)
1937 walk_data.before_dom_children_walk_stmts = rewrite_update_stmt;
1938 else
1939 gcc_unreachable ();
1941 if (what == REWRITE_ALL)
1942 walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
1943 else if (what == REWRITE_UPDATE)
1944 walk_data.before_dom_children_after_stmts = rewrite_update_phi_arguments;
1945 else
1946 gcc_unreachable ();
1948 if (what == REWRITE_ALL)
1949 walk_data.after_dom_children_after_stmts = rewrite_finalize_block;
1950 else if (what == REWRITE_UPDATE)
1951 walk_data.after_dom_children_after_stmts = rewrite_update_fini_block;
1952 else
1953 gcc_unreachable ();
1955 block_defs_stack = VEC_alloc (tree, heap, 10);
1957 /* Initialize the dominator walker. */
1958 init_walk_dominator_tree (&walk_data);
1960 /* Recursively walk the dominator tree rewriting each statement in
1961 each basic block. */
1962 walk_dominator_tree (&walk_data, entry);
1964 /* Finalize the dominator walker. */
1965 fini_walk_dominator_tree (&walk_data);
1967 /* Debugging dumps. */
1968 if (dump_file && (dump_flags & TDF_STATS))
1970 dump_dfa_stats (dump_file);
1971 if (def_blocks)
1972 dump_tree_ssa_stats (dump_file);
1975 if (def_blocks)
1977 htab_delete (def_blocks);
1978 def_blocks = NULL;
1981 VEC_free (tree, heap, block_defs_stack);
1983 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1987 /* Block initialization routine for mark_def_sites. Clear the
1988 KILLS bitmap at the start of each block. */
1990 static void
1991 mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
1992 basic_block bb ATTRIBUTE_UNUSED)
1994 struct mark_def_sites_global_data *gd =
1995 (struct mark_def_sites_global_data *) walk_data->global_data;
1996 bitmap kills = gd->kills;
1997 bitmap_clear (kills);
2001 /* Mark the definition site blocks for each variable, so that we know
2002 where the variable is actually live.
2004 INTERESTING_BLOCKS will be filled in with all the blocks that
2005 should be processed by the renamer. It is assumed to be
2006 initialized and zeroed by the caller. */
2008 static void
2009 mark_def_site_blocks (sbitmap interesting_blocks)
2011 struct dom_walk_data walk_data;
2012 struct mark_def_sites_global_data mark_def_sites_global_data;
2013 referenced_var_iterator rvi;
2014 tree var;
2016 /* Allocate memory for the DEF_BLOCKS hash table. */
2017 def_blocks = htab_create (num_referenced_vars,
2018 def_blocks_hash, def_blocks_eq, def_blocks_free);
2019 FOR_EACH_REFERENCED_VAR(var, rvi)
2020 set_current_def (var, NULL_TREE);
2022 /* Setup callbacks for the generic dominator tree walker to find and
2023 mark definition sites. */
2024 walk_data.walk_stmts_backward = false;
2025 walk_data.dom_direction = CDI_DOMINATORS;
2026 walk_data.initialize_block_local_data = NULL;
2027 walk_data.before_dom_children_before_stmts = mark_def_sites_initialize_block;
2028 walk_data.before_dom_children_walk_stmts = mark_def_sites;
2029 walk_data.before_dom_children_after_stmts = NULL;
2030 walk_data.after_dom_children_before_stmts = NULL;
2031 walk_data.after_dom_children_walk_stmts = NULL;
2032 walk_data.after_dom_children_after_stmts = NULL;
2033 walk_data.interesting_blocks = NULL;
2035 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2036 large enough to accommodate all the variables referenced in the
2037 function, not just the ones we are renaming. */
2038 mark_def_sites_global_data.kills = BITMAP_ALLOC (NULL);
2040 /* Create the set of interesting blocks that will be filled by
2041 mark_def_sites. */
2042 mark_def_sites_global_data.interesting_blocks = interesting_blocks;
2043 walk_data.global_data = &mark_def_sites_global_data;
2045 /* We do not have any local data. */
2046 walk_data.block_local_data_size = 0;
2048 /* Initialize the dominator walker. */
2049 init_walk_dominator_tree (&walk_data);
2051 /* Recursively walk the dominator tree. */
2052 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
2054 /* Finalize the dominator walker. */
2055 fini_walk_dominator_tree (&walk_data);
2057 /* We no longer need this bitmap, clear and free it. */
2058 BITMAP_FREE (mark_def_sites_global_data.kills);
2062 /* Main entry point into the SSA builder. The renaming process
2063 proceeds in four main phases:
2065 1- Compute dominance frontier and immediate dominators, needed to
2066 insert PHI nodes and rename the function in dominator tree
2067 order.
2069 2- Find and mark all the blocks that define variables
2070 (mark_def_site_blocks).
2072 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2074 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2076 Steps 3 and 4 are done using the dominator tree walker
2077 (walk_dominator_tree). */
2079 static unsigned int
2080 rewrite_into_ssa (void)
2082 bitmap *dfs;
2083 basic_block bb;
2084 sbitmap interesting_blocks;
2086 timevar_push (TV_TREE_SSA_OTHER);
2088 /* Initialize operand data structures. */
2089 init_ssa_operands ();
2091 /* Initialize the set of interesting blocks. The callback
2092 mark_def_sites will add to this set those blocks that the renamer
2093 should process. */
2094 interesting_blocks = sbitmap_alloc (last_basic_block);
2095 sbitmap_zero (interesting_blocks);
2097 /* Initialize dominance frontier. */
2098 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap));
2099 FOR_EACH_BB (bb)
2100 dfs[bb->index] = BITMAP_ALLOC (NULL);
2102 /* 1- Compute dominance frontiers. */
2103 calculate_dominance_info (CDI_DOMINATORS);
2104 compute_dominance_frontiers (dfs);
2106 /* 2- Find and mark definition sites. */
2107 mark_def_site_blocks (interesting_blocks);
2109 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2110 insert_phi_nodes (dfs);
2112 /* 4- Rename all the blocks. */
2113 rewrite_blocks (ENTRY_BLOCK_PTR, REWRITE_ALL, interesting_blocks);
2115 /* Free allocated memory. */
2116 FOR_EACH_BB (bb)
2117 BITMAP_FREE (dfs[bb->index]);
2118 free (dfs);
2119 sbitmap_free (interesting_blocks);
2121 timevar_pop (TV_TREE_SSA_OTHER);
2122 cfun->gimple_df->in_ssa_p = true;
2123 return 0;
2127 struct tree_opt_pass pass_build_ssa =
2129 "ssa", /* name */
2130 NULL, /* gate */
2131 rewrite_into_ssa, /* execute */
2132 NULL, /* sub */
2133 NULL, /* next */
2134 0, /* static_pass_number */
2135 0, /* tv_id */
2136 PROP_cfg | PROP_referenced_vars, /* properties_required */
2137 PROP_ssa, /* properties_provided */
2138 0, /* properties_destroyed */
2139 0, /* todo_flags_start */
2140 TODO_dump_func
2141 | TODO_verify_ssa
2142 | TODO_remove_unused_locals, /* todo_flags_finish */
2143 0 /* letter */
2147 /* Mark the definition of VAR at STMT and BB as interesting for the
2148 renamer. BLOCKS is the set of blocks that need updating. */
2150 static void
2151 mark_def_interesting (tree var, tree stmt, basic_block bb, bool insert_phi_p)
2153 gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
2154 REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
2156 if (insert_phi_p)
2158 bool is_phi_p = TREE_CODE (stmt) == PHI_NODE;
2160 set_def_block (var, bb, is_phi_p);
2162 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2163 site for both itself and all the old names replaced by it. */
2164 if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
2166 bitmap_iterator bi;
2167 unsigned i;
2168 bitmap set = names_replaced_by (var);
2169 if (set)
2170 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2171 set_def_block (ssa_name (i), bb, is_phi_p);
2177 /* Mark the use of VAR at STMT and BB as interesting for the
2178 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2179 nodes. */
2181 static inline void
2182 mark_use_interesting (tree var, tree stmt, basic_block bb, bool insert_phi_p)
2184 basic_block def_bb = bb_for_stmt (stmt);
2186 mark_block_for_update (def_bb);
2187 mark_block_for_update (bb);
2189 if (TREE_CODE (stmt) == PHI_NODE)
2190 mark_phi_for_rewrite (def_bb, stmt);
2191 else
2192 REWRITE_THIS_STMT (stmt) = 1;
2194 /* If VAR has not been defined in BB, then it is live-on-entry
2195 to BB. Note that we cannot just use the block holding VAR's
2196 definition because if VAR is one of the names in OLD_SSA_NAMES,
2197 it will have several definitions (itself and all the names that
2198 replace it). */
2199 if (insert_phi_p)
2201 struct def_blocks_d *db_p = get_def_blocks_for (var);
2202 if (!bitmap_bit_p (db_p->def_blocks, bb->index))
2203 set_livein_block (var, bb);
2208 /* Do a dominator walk starting at BB processing statements that
2209 reference symbols in SYMS_TO_RENAME. This is very similar to
2210 mark_def_sites, but the scan handles statements whose operands may
2211 already be SSA names.
2213 If INSERT_PHI_P is true, mark those uses as live in the
2214 corresponding block. This is later used by the PHI placement
2215 algorithm to make PHI pruning decisions. */
2217 static void
2218 prepare_block_for_update (basic_block bb, bool insert_phi_p)
2220 basic_block son;
2221 block_stmt_iterator si;
2222 tree phi;
2223 edge e;
2224 edge_iterator ei;
2226 mark_block_for_update (bb);
2228 /* Process PHI nodes marking interesting those that define or use
2229 the symbols that we are interested in. */
2230 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2232 tree lhs_sym, lhs = PHI_RESULT (phi);
2234 lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
2236 if (!symbol_marked_for_renaming (lhs_sym))
2237 continue;
2238 mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
2240 /* Mark the uses in phi nodes as interesting. It would be more correct
2241 to process the arguments of the phi nodes of the successor edges of
2242 BB at the end of prepare_block_for_update, however, that turns out
2243 to be significantly more expensive. Doing it here is conservatively
2244 correct -- it may only cause us to believe a value to be live in a
2245 block that also contains its definition, and thus insert a few more
2246 phi nodes for it. */
2247 FOR_EACH_EDGE (e, ei, bb->preds)
2249 mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
2253 /* Process the statements. */
2254 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2256 tree stmt;
2257 ssa_op_iter i;
2258 use_operand_p use_p;
2259 def_operand_p def_p;
2261 stmt = bsi_stmt (si);
2263 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
2265 tree use = USE_FROM_PTR (use_p);
2266 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
2267 if (symbol_marked_for_renaming (sym))
2268 mark_use_interesting (use, stmt, bb, insert_phi_p);
2271 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_DEF)
2273 tree def = DEF_FROM_PTR (def_p);
2274 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
2276 if (symbol_marked_for_renaming (sym))
2277 mark_def_interesting (def, stmt, bb, insert_phi_p);
2280 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_VIRTUAL_DEFS)
2282 tree def = DEF_FROM_PTR (def_p);
2283 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
2285 if (symbol_marked_for_renaming (sym))
2287 mark_use_interesting (sym, stmt, bb, insert_phi_p);
2288 mark_def_interesting (sym, stmt, bb, insert_phi_p);
2292 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_VUSE)
2294 tree use = USE_FROM_PTR (use_p);
2295 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
2297 if (symbol_marked_for_renaming (sym))
2298 mark_use_interesting (sym, stmt, bb, insert_phi_p);
2302 /* Now visit all the blocks dominated by BB. */
2303 for (son = first_dom_son (CDI_DOMINATORS, bb);
2304 son;
2305 son = next_dom_son (CDI_DOMINATORS, son))
2306 prepare_block_for_update (son, insert_phi_p);
2310 /* Helper for prepare_names_to_update. Mark all the use sites for
2311 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2312 prepare_names_to_update. */
2314 static void
2315 prepare_use_sites_for (tree name, bool insert_phi_p)
2317 use_operand_p use_p;
2318 imm_use_iterator iter;
2320 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
2322 tree stmt = USE_STMT (use_p);
2323 basic_block bb = bb_for_stmt (stmt);
2325 if (TREE_CODE (stmt) == PHI_NODE)
2327 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
2328 edge e = PHI_ARG_EDGE (stmt, ix);
2329 mark_use_interesting (name, stmt, e->src, insert_phi_p);
2331 else
2333 /* For regular statements, mark this as an interesting use
2334 for NAME. */
2335 mark_use_interesting (name, stmt, bb, insert_phi_p);
2341 /* Helper for prepare_names_to_update. Mark the definition site for
2342 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2343 prepare_names_to_update. */
2345 static void
2346 prepare_def_site_for (tree name, bool insert_phi_p)
2348 tree stmt;
2349 basic_block bb;
2351 gcc_assert (names_to_release == NULL
2352 || !bitmap_bit_p (names_to_release, SSA_NAME_VERSION (name)));
2354 stmt = SSA_NAME_DEF_STMT (name);
2355 bb = bb_for_stmt (stmt);
2356 if (bb)
2358 gcc_assert (bb->index < last_basic_block);
2359 mark_block_for_update (bb);
2360 mark_def_interesting (name, stmt, bb, insert_phi_p);
2365 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2366 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2367 PHI nodes for newly created names. */
2369 static void
2370 prepare_names_to_update (bool insert_phi_p)
2372 unsigned i = 0;
2373 bitmap_iterator bi;
2374 sbitmap_iterator sbi;
2376 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2377 remove it from NEW_SSA_NAMES so that we don't try to visit its
2378 defining basic block (which most likely doesn't exist). Notice
2379 that we cannot do the same with names in OLD_SSA_NAMES because we
2380 want to replace existing instances. */
2381 if (names_to_release)
2382 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2383 RESET_BIT (new_ssa_names, i);
2385 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2386 names may be considered to be live-in on blocks that contain
2387 definitions for their replacements. */
2388 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2389 prepare_def_site_for (ssa_name (i), insert_phi_p);
2391 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2392 OLD_SSA_NAMES, but we have to ignore its definition site. */
2393 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2395 if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
2396 prepare_def_site_for (ssa_name (i), insert_phi_p);
2397 prepare_use_sites_for (ssa_name (i), insert_phi_p);
2402 /* Dump all the names replaced by NAME to FILE. */
2404 void
2405 dump_names_replaced_by (FILE *file, tree name)
2407 unsigned i;
2408 bitmap old_set;
2409 bitmap_iterator bi;
2411 print_generic_expr (file, name, 0);
2412 fprintf (file, " -> { ");
2414 old_set = names_replaced_by (name);
2415 EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
2417 print_generic_expr (file, ssa_name (i), 0);
2418 fprintf (file, " ");
2421 fprintf (file, "}\n");
2425 /* Dump all the names replaced by NAME to stderr. */
2427 void
2428 debug_names_replaced_by (tree name)
2430 dump_names_replaced_by (stderr, name);
2434 /* Dump SSA update information to FILE. */
2436 void
2437 dump_update_ssa (FILE *file)
2439 unsigned i = 0;
2440 bitmap_iterator bi;
2442 if (!need_ssa_update_p ())
2443 return;
2445 if (new_ssa_names && sbitmap_first_set_bit (new_ssa_names) >= 0)
2447 sbitmap_iterator sbi;
2449 fprintf (file, "\nSSA replacement table\n");
2450 fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
2451 "O_1, ..., O_j\n\n");
2453 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2454 dump_names_replaced_by (file, ssa_name (i));
2456 fprintf (file, "\n");
2457 fprintf (file, "Number of virtual NEW -> OLD mappings: %7u\n",
2458 update_ssa_stats.num_virtual_mappings);
2459 fprintf (file, "Number of real NEW -> OLD mappings: %7u\n",
2460 update_ssa_stats.num_total_mappings
2461 - update_ssa_stats.num_virtual_mappings);
2462 fprintf (file, "Number of total NEW -> OLD mappings: %7u\n",
2463 update_ssa_stats.num_total_mappings);
2465 fprintf (file, "\nNumber of virtual symbols: %u\n",
2466 update_ssa_stats.num_virtual_symbols);
2469 if (syms_to_rename && !bitmap_empty_p (syms_to_rename))
2471 fprintf (file, "\n\nSymbols to be put in SSA form\n\n");
2472 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
2474 print_generic_expr (file, referenced_var (i), 0);
2475 fprintf (file, " ");
2479 if (names_to_release && !bitmap_empty_p (names_to_release))
2481 fprintf (file, "\n\nSSA names to release after updating the SSA web\n\n");
2482 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2484 print_generic_expr (file, ssa_name (i), 0);
2485 fprintf (file, " ");
2489 fprintf (file, "\n\n");
2493 /* Dump SSA update information to stderr. */
2495 void
2496 debug_update_ssa (void)
2498 dump_update_ssa (stderr);
2502 /* Initialize data structures used for incremental SSA updates. */
2504 static void
2505 init_update_ssa (void)
2507 /* Reserve more space than the current number of names. The calls to
2508 add_new_name_mapping are typically done after creating new SSA
2509 names, so we'll need to reallocate these arrays. */
2510 old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2511 sbitmap_zero (old_ssa_names);
2513 new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2514 sbitmap_zero (new_ssa_names);
2516 repl_tbl = htab_create (20, repl_map_hash, repl_map_eq, repl_map_free);
2517 need_to_initialize_update_ssa_p = false;
2518 need_to_update_vops_p = false;
2519 syms_to_rename = BITMAP_ALLOC (NULL);
2520 names_to_release = NULL;
2521 memset (&update_ssa_stats, 0, sizeof (update_ssa_stats));
2522 update_ssa_stats.virtual_symbols = BITMAP_ALLOC (NULL);
2526 /* Deallocate data structures used for incremental SSA updates. */
2528 void
2529 delete_update_ssa (void)
2531 unsigned i;
2532 bitmap_iterator bi;
2534 sbitmap_free (old_ssa_names);
2535 old_ssa_names = NULL;
2537 sbitmap_free (new_ssa_names);
2538 new_ssa_names = NULL;
2540 htab_delete (repl_tbl);
2541 repl_tbl = NULL;
2543 need_to_initialize_update_ssa_p = true;
2544 need_to_update_vops_p = false;
2545 BITMAP_FREE (syms_to_rename);
2546 BITMAP_FREE (update_ssa_stats.virtual_symbols);
2548 if (names_to_release)
2550 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2551 release_ssa_name (ssa_name (i));
2552 BITMAP_FREE (names_to_release);
2555 clear_ssa_name_info ();
2559 /* Create a new name for OLD_NAME in statement STMT and replace the
2560 operand pointed to by DEF_P with the newly created name. Return
2561 the new name and register the replacement mapping <NEW, OLD> in
2562 update_ssa's tables. */
2564 tree
2565 create_new_def_for (tree old_name, tree stmt, def_operand_p def)
2567 tree new_name = duplicate_ssa_name (old_name, stmt);
2569 SET_DEF (def, new_name);
2571 if (TREE_CODE (stmt) == PHI_NODE)
2573 edge e;
2574 edge_iterator ei;
2575 basic_block bb = bb_for_stmt (stmt);
2577 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2578 FOR_EACH_EDGE (e, ei, bb->preds)
2579 if (e->flags & EDGE_ABNORMAL)
2581 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
2582 break;
2586 register_new_name_mapping (new_name, old_name);
2588 /* For the benefit of passes that will be updating the SSA form on
2589 their own, set the current reaching definition of OLD_NAME to be
2590 NEW_NAME. */
2591 set_current_def (old_name, new_name);
2593 return new_name;
2597 /* Register name NEW to be a replacement for name OLD. This function
2598 must be called for every replacement that should be performed by
2599 update_ssa. */
2601 void
2602 register_new_name_mapping (tree new, tree old)
2604 if (need_to_initialize_update_ssa_p)
2605 init_update_ssa ();
2607 add_new_name_mapping (new, old);
2611 /* Register symbol SYM to be renamed by update_ssa. */
2613 void
2614 mark_sym_for_renaming (tree sym)
2616 if (need_to_initialize_update_ssa_p)
2617 init_update_ssa ();
2619 bitmap_set_bit (syms_to_rename, DECL_UID (sym));
2621 if (!is_gimple_reg (sym))
2622 need_to_update_vops_p = true;
2626 /* Register all the symbols in SET to be renamed by update_ssa. */
2628 void
2629 mark_set_for_renaming (bitmap set)
2631 bitmap_iterator bi;
2632 unsigned i;
2634 if (bitmap_empty_p (set))
2635 return;
2637 if (need_to_initialize_update_ssa_p)
2638 init_update_ssa ();
2640 bitmap_ior_into (syms_to_rename, set);
2642 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2643 if (!is_gimple_reg (referenced_var (i)))
2645 need_to_update_vops_p = true;
2646 break;
2651 /* Return true if there is any work to be done by update_ssa. */
2653 bool
2654 need_ssa_update_p (void)
2656 return syms_to_rename || old_ssa_names || new_ssa_names;
2659 /* Return true if SSA name mappings have been registered for SSA updating. */
2661 bool
2662 name_mappings_registered_p (void)
2664 return repl_tbl && htab_elements (repl_tbl) > 0;
2667 /* Return true if name N has been registered in the replacement table. */
2669 bool
2670 name_registered_for_update_p (tree n)
2672 if (!need_ssa_update_p ())
2673 return false;
2675 return is_new_name (n)
2676 || is_old_name (n)
2677 || symbol_marked_for_renaming (SSA_NAME_VAR (n));
2681 /* Return the set of all the SSA names marked to be replaced. */
2683 bitmap
2684 ssa_names_to_replace (void)
2686 unsigned i = 0;
2687 bitmap ret;
2688 sbitmap_iterator sbi;
2690 ret = BITMAP_ALLOC (NULL);
2691 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2692 bitmap_set_bit (ret, i);
2694 return ret;
2698 /* Mark NAME to be released after update_ssa has finished. */
2700 void
2701 release_ssa_name_after_update_ssa (tree name)
2703 gcc_assert (!need_to_initialize_update_ssa_p);
2705 if (names_to_release == NULL)
2706 names_to_release = BITMAP_ALLOC (NULL);
2708 bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name));
2712 /* Insert new PHI nodes to replace VAR. DFS contains dominance
2713 frontier information. BLOCKS is the set of blocks to be updated.
2715 This is slightly different than the regular PHI insertion
2716 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
2717 real names (i.e., GIMPLE registers) are inserted:
2719 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
2720 nodes inside the region affected by the block that defines VAR
2721 and the blocks that define all its replacements. All these
2722 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
2724 First, we compute the entry point to the region (ENTRY). This is
2725 given by the nearest common dominator to all the definition
2726 blocks. When computing the iterated dominance frontier (IDF), any
2727 block not strictly dominated by ENTRY is ignored.
2729 We then call the standard PHI insertion algorithm with the pruned
2730 IDF.
2732 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
2733 names is not pruned. PHI nodes are inserted at every IDF block. */
2735 static void
2736 insert_updated_phi_nodes_for (tree var, bitmap *dfs, bitmap blocks,
2737 unsigned update_flags)
2739 basic_block entry;
2740 struct def_blocks_d *db;
2741 bitmap idf, pruned_idf;
2742 bitmap_iterator bi;
2743 unsigned i;
2745 #if defined ENABLE_CHECKING
2746 if (TREE_CODE (var) == SSA_NAME)
2747 gcc_assert (is_old_name (var));
2748 else
2749 gcc_assert (symbol_marked_for_renaming (var));
2750 #endif
2752 /* Get all the definition sites for VAR. */
2753 db = find_def_blocks_for (var);
2755 /* No need to do anything if there were no definitions to VAR. */
2756 if (db == NULL || bitmap_empty_p (db->def_blocks))
2757 return;
2759 /* Compute the initial iterated dominance frontier. */
2760 idf = find_idf (db->def_blocks, dfs);
2761 pruned_idf = BITMAP_ALLOC (NULL);
2763 if (TREE_CODE (var) == SSA_NAME)
2765 if (update_flags == TODO_update_ssa)
2767 /* If doing regular SSA updates for GIMPLE registers, we are
2768 only interested in IDF blocks dominated by the nearest
2769 common dominator of all the definition blocks. */
2770 entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
2771 db->def_blocks);
2773 if (entry != ENTRY_BLOCK_PTR)
2774 EXECUTE_IF_SET_IN_BITMAP (idf, 0, i, bi)
2775 if (BASIC_BLOCK (i) != entry
2776 && dominated_by_p (CDI_DOMINATORS, BASIC_BLOCK (i), entry))
2777 bitmap_set_bit (pruned_idf, i);
2779 else
2781 /* Otherwise, do not prune the IDF for VAR. */
2782 gcc_assert (update_flags == TODO_update_ssa_full_phi);
2783 bitmap_copy (pruned_idf, idf);
2786 else
2788 /* Otherwise, VAR is a symbol that needs to be put into SSA form
2789 for the first time, so we need to compute the full IDF for
2790 it. */
2791 bitmap_copy (pruned_idf, idf);
2794 if (!bitmap_empty_p (pruned_idf))
2796 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
2797 are included in the region to be updated. The feeding blocks
2798 are important to guarantee that the PHI arguments are renamed
2799 properly. */
2800 bitmap_ior_into (blocks, pruned_idf);
2801 EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
2803 edge e;
2804 edge_iterator ei;
2805 basic_block bb = BASIC_BLOCK (i);
2807 FOR_EACH_EDGE (e, ei, bb->preds)
2808 if (e->src->index >= 0)
2809 bitmap_set_bit (blocks, e->src->index);
2812 insert_phi_nodes_for (var, pruned_idf, true);
2815 BITMAP_FREE (pruned_idf);
2816 BITMAP_FREE (idf);
2820 /* Heuristic to determine whether SSA name mappings for virtual names
2821 should be discarded and their symbols rewritten from scratch. When
2822 there is a large number of mappings for virtual names, the
2823 insertion of PHI nodes for the old names in the mappings takes
2824 considerable more time than if we inserted PHI nodes for the
2825 symbols instead.
2827 Currently the heuristic takes these stats into account:
2829 - Number of mappings for virtual SSA names.
2830 - Number of distinct virtual symbols involved in those mappings.
2832 If the number of virtual mappings is much larger than the number of
2833 virtual symbols, then it will be faster to compute PHI insertion
2834 spots for the symbols. Even if this involves traversing the whole
2835 CFG, which is what happens when symbols are renamed from scratch. */
2837 static bool
2838 switch_virtuals_to_full_rewrite_p (void)
2840 if (update_ssa_stats.num_virtual_mappings < (unsigned) MIN_VIRTUAL_MAPPINGS)
2841 return false;
2843 if (update_ssa_stats.num_virtual_mappings
2844 > (unsigned) VIRTUAL_MAPPINGS_TO_SYMS_RATIO
2845 * update_ssa_stats.num_virtual_symbols)
2846 return true;
2848 return false;
2852 /* Remove every virtual mapping and mark all the affected virtual
2853 symbols for renaming. */
2855 static void
2856 switch_virtuals_to_full_rewrite (void)
2858 unsigned i = 0;
2859 sbitmap_iterator sbi;
2861 if (dump_file)
2863 fprintf (dump_file, "\nEnabled virtual name mapping heuristic.\n");
2864 fprintf (dump_file, "\tNumber of virtual mappings: %7u\n",
2865 update_ssa_stats.num_virtual_mappings);
2866 fprintf (dump_file, "\tNumber of unique virtual symbols: %7u\n",
2867 update_ssa_stats.num_virtual_symbols);
2868 fprintf (dump_file, "Updating FUD-chains from top of CFG will be "
2869 "faster than processing\nthe name mappings.\n\n");
2872 /* Remove all virtual names from NEW_SSA_NAMES and OLD_SSA_NAMES.
2873 Note that it is not really necessary to remove the mappings from
2874 REPL_TBL, that would only waste time. */
2875 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2876 if (!is_gimple_reg (ssa_name (i)))
2877 RESET_BIT (new_ssa_names, i);
2879 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2880 if (!is_gimple_reg (ssa_name (i)))
2881 RESET_BIT (old_ssa_names, i);
2883 bitmap_ior_into (syms_to_rename, update_ssa_stats.virtual_symbols);
2887 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
2888 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
2890 1- The names in OLD_SSA_NAMES dominated by the definitions of
2891 NEW_SSA_NAMES are all re-written to be reached by the
2892 appropriate definition from NEW_SSA_NAMES.
2894 2- If needed, new PHI nodes are added to the iterated dominance
2895 frontier of the blocks where each of NEW_SSA_NAMES are defined.
2897 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
2898 calling register_new_name_mapping for every pair of names that the
2899 caller wants to replace.
2901 The caller identifies the new names that have been inserted and the
2902 names that need to be replaced by calling register_new_name_mapping
2903 for every pair <NEW, OLD>. Note that the function assumes that the
2904 new names have already been inserted in the IL.
2906 For instance, given the following code:
2908 1 L0:
2909 2 x_1 = PHI (0, x_5)
2910 3 if (x_1 < 10)
2911 4 if (x_1 > 7)
2912 5 y_2 = 0
2913 6 else
2914 7 y_3 = x_1 + x_7
2915 8 endif
2916 9 x_5 = x_1 + 1
2917 10 goto L0;
2918 11 endif
2920 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
2922 1 L0:
2923 2 x_1 = PHI (0, x_5)
2924 3 if (x_1 < 10)
2925 4 x_10 = ...
2926 5 if (x_1 > 7)
2927 6 y_2 = 0
2928 7 else
2929 8 x_11 = ...
2930 9 y_3 = x_1 + x_7
2931 10 endif
2932 11 x_5 = x_1 + 1
2933 12 goto L0;
2934 13 endif
2936 We want to replace all the uses of x_1 with the new definitions of
2937 x_10 and x_11. Note that the only uses that should be replaced are
2938 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
2939 *not* be replaced (this is why we cannot just mark symbol 'x' for
2940 renaming).
2942 Additionally, we may need to insert a PHI node at line 11 because
2943 that is a merge point for x_10 and x_11. So the use of x_1 at line
2944 11 will be replaced with the new PHI node. The insertion of PHI
2945 nodes is optional. They are not strictly necessary to preserve the
2946 SSA form, and depending on what the caller inserted, they may not
2947 even be useful for the optimizers. UPDATE_FLAGS controls various
2948 aspects of how update_ssa operates, see the documentation for
2949 TODO_update_ssa*. */
2951 void
2952 update_ssa (unsigned update_flags)
2954 basic_block bb, start_bb;
2955 bitmap_iterator bi;
2956 unsigned i = 0;
2957 sbitmap tmp;
2958 bool insert_phi_p;
2959 sbitmap_iterator sbi;
2961 if (!need_ssa_update_p ())
2962 return;
2964 timevar_push (TV_TREE_SSA_INCREMENTAL);
2966 blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
2967 if (!phis_to_rewrite)
2968 phis_to_rewrite = VEC_alloc (tree_vec, heap, last_basic_block);
2969 blocks_to_update = BITMAP_ALLOC (NULL);
2971 /* Ensure that the dominance information is up-to-date. */
2972 calculate_dominance_info (CDI_DOMINATORS);
2974 /* Only one update flag should be set. */
2975 gcc_assert (update_flags == TODO_update_ssa
2976 || update_flags == TODO_update_ssa_no_phi
2977 || update_flags == TODO_update_ssa_full_phi
2978 || update_flags == TODO_update_ssa_only_virtuals);
2980 /* If we only need to update virtuals, remove all the mappings for
2981 real names before proceeding. The caller is responsible for
2982 having dealt with the name mappings before calling update_ssa. */
2983 if (update_flags == TODO_update_ssa_only_virtuals)
2985 sbitmap_zero (old_ssa_names);
2986 sbitmap_zero (new_ssa_names);
2987 htab_empty (repl_tbl);
2990 insert_phi_p = (update_flags != TODO_update_ssa_no_phi);
2992 if (insert_phi_p)
2994 /* If the caller requested PHI nodes to be added, initialize
2995 live-in information data structures (DEF_BLOCKS). */
2997 /* For each SSA name N, the DEF_BLOCKS table describes where the
2998 name is defined, which blocks have PHI nodes for N, and which
2999 blocks have uses of N (i.e., N is live-on-entry in those
3000 blocks). */
3001 def_blocks = htab_create (num_ssa_names, def_blocks_hash,
3002 def_blocks_eq, def_blocks_free);
3004 else
3006 def_blocks = NULL;
3009 /* Heuristic to avoid massive slow downs when the replacement
3010 mappings include lots of virtual names. */
3011 if (insert_phi_p && switch_virtuals_to_full_rewrite_p ())
3012 switch_virtuals_to_full_rewrite ();
3014 /* If there are names defined in the replacement table, prepare
3015 definition and use sites for all the names in NEW_SSA_NAMES and
3016 OLD_SSA_NAMES. */
3017 if (sbitmap_first_set_bit (new_ssa_names) >= 0)
3019 prepare_names_to_update (insert_phi_p);
3021 /* If all the names in NEW_SSA_NAMES had been marked for
3022 removal, and there are no symbols to rename, then there's
3023 nothing else to do. */
3024 if (sbitmap_first_set_bit (new_ssa_names) < 0
3025 && bitmap_empty_p (syms_to_rename))
3026 goto done;
3029 /* Next, determine the block at which to start the renaming process. */
3030 if (!bitmap_empty_p (syms_to_rename))
3032 /* If we have to rename some symbols from scratch, we need to
3033 start the process at the root of the CFG. FIXME, it should
3034 be possible to determine the nearest block that had a
3035 definition for each of the symbols that are marked for
3036 updating. For now this seems more work than it's worth. */
3037 start_bb = ENTRY_BLOCK_PTR;
3039 /* Traverse the CFG looking for definitions and uses of symbols
3040 in SYMS_TO_RENAME. Mark interesting blocks and statements
3041 and set local live-in information for the PHI placement
3042 heuristics. */
3043 prepare_block_for_update (start_bb, insert_phi_p);
3045 else
3047 /* Otherwise, the entry block to the region is the nearest
3048 common dominator for the blocks in BLOCKS. */
3049 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3050 blocks_to_update);
3053 /* If requested, insert PHI nodes at the iterated dominance frontier
3054 of every block, creating new definitions for names in OLD_SSA_NAMES
3055 and for symbols in SYMS_TO_RENAME. */
3056 if (insert_phi_p)
3058 bitmap *dfs;
3060 /* If the caller requested PHI nodes to be added, compute
3061 dominance frontiers. */
3062 dfs = XNEWVEC (bitmap, last_basic_block);
3063 FOR_EACH_BB (bb)
3064 dfs[bb->index] = BITMAP_ALLOC (NULL);
3065 compute_dominance_frontiers (dfs);
3067 if (sbitmap_first_set_bit (old_ssa_names) >= 0)
3069 sbitmap_iterator sbi;
3071 /* insert_update_phi_nodes_for will call add_new_name_mapping
3072 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3073 will grow while we are traversing it (but it will not
3074 gain any new members). Copy OLD_SSA_NAMES to a temporary
3075 for traversal. */
3076 sbitmap tmp = sbitmap_alloc (old_ssa_names->n_bits);
3077 sbitmap_copy (tmp, old_ssa_names);
3078 EXECUTE_IF_SET_IN_SBITMAP (tmp, 0, i, sbi)
3079 insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks_to_update,
3080 update_flags);
3081 sbitmap_free (tmp);
3084 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
3085 insert_updated_phi_nodes_for (referenced_var (i), dfs,
3086 blocks_to_update, update_flags);
3088 FOR_EACH_BB (bb)
3089 BITMAP_FREE (dfs[bb->index]);
3090 free (dfs);
3092 /* Insertion of PHI nodes may have added blocks to the region.
3093 We need to re-compute START_BB to include the newly added
3094 blocks. */
3095 if (start_bb != ENTRY_BLOCK_PTR)
3096 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3097 blocks_to_update);
3100 /* Reset the current definition for name and symbol before renaming
3101 the sub-graph. */
3102 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
3103 set_current_def (ssa_name (i), NULL_TREE);
3105 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
3106 set_current_def (referenced_var (i), NULL_TREE);
3108 /* Now start the renaming process at START_BB. */
3109 tmp = sbitmap_alloc (last_basic_block);
3110 sbitmap_zero (tmp);
3111 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3112 SET_BIT (tmp, i);
3114 rewrite_blocks (start_bb, REWRITE_UPDATE, tmp);
3116 sbitmap_free (tmp);
3118 /* Debugging dumps. */
3119 if (dump_file)
3121 int c;
3122 unsigned i;
3124 dump_update_ssa (dump_file);
3126 fprintf (dump_file, "Incremental SSA update started at block: %d\n\n",
3127 start_bb->index);
3129 c = 0;
3130 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3131 c++;
3132 fprintf (dump_file, "Number of blocks in CFG: %d\n", last_basic_block);
3133 fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n\n",
3134 c, PERCENT (c, last_basic_block));
3136 if (dump_flags & TDF_DETAILS)
3138 fprintf (dump_file, "Affected blocks: ");
3139 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3140 fprintf (dump_file, "%u ", i);
3141 fprintf (dump_file, "\n");
3144 fprintf (dump_file, "\n\n");
3147 /* Free allocated memory. */
3148 done:
3149 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi)
3151 tree_vec phis = VEC_index (tree_vec, phis_to_rewrite, i);
3153 VEC_free (tree, heap, phis);
3154 VEC_replace (tree_vec, phis_to_rewrite, i, NULL);
3156 BITMAP_FREE (blocks_with_phis_to_rewrite);
3157 BITMAP_FREE (blocks_to_update);
3158 delete_update_ssa ();
3160 timevar_pop (TV_TREE_SSA_INCREMENTAL);