aboutsummaryrefslogtreecommitdiff
path: root/gcc/tree-into-ssa.c
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/tree-into-ssa.c')
-rw-r--r--gcc/tree-into-ssa.c718
1 files changed, 652 insertions, 66 deletions
diff --git a/gcc/tree-into-ssa.c b/gcc/tree-into-ssa.c
index d8949de2b2a..7c21b46c38e 100644
--- a/gcc/tree-into-ssa.c
+++ b/gcc/tree-into-ssa.c
@@ -66,6 +66,9 @@ struct def_blocks_d
Ith block contains a definition of VAR. */
bitmap def_blocks;
+ /* Blocks that contain a phi node for VAR. */
+ bitmap phi_blocks;
+
/* Blocks where VAR is live-on-entry. Similar semantics as
DEF_BLOCKS. */
bitmap livein_blocks;
@@ -88,6 +91,12 @@ struct mark_def_sites_global_data
solely to avoid the overhead of allocating and deallocating
the bitmap. */
sbitmap kills;
+
+ /* Bitmap of names to rename. */
+ sbitmap names_to_rename;
+
+ /* Array of ssa names. */
+ tree *ssa_names;
};
/* Table to store the current reaching definition for every variable in
@@ -100,7 +109,6 @@ struct rewrite_block_data
varray_type block_defs;
};
-
/* Local functions. */
static void rewrite_finalize_block (struct dom_walk_data *, basic_block);
static void rewrite_initialize_block_local_data (struct dom_walk_data *,
@@ -111,15 +119,14 @@ static void mark_def_sites (struct dom_walk_data *walk_data,
basic_block bb, block_stmt_iterator);
static void mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
basic_block bb);
-static void compute_global_livein (bitmap, bitmap);
-static void set_def_block (tree, basic_block);
+static void set_def_block (tree, basic_block, bool);
static void set_livein_block (tree, basic_block);
static bool prepare_operand_for_rename (tree *op_p, size_t *uid_p);
-static void insert_phi_nodes (bitmap *);
+static void insert_phi_nodes (bitmap *, bitmap, tree *);
static void rewrite_stmt (struct dom_walk_data *, basic_block,
block_stmt_iterator);
static inline void rewrite_operand (tree *);
-static void insert_phi_nodes_for (tree, bitmap *);
+static void insert_phi_nodes_for (tree, bitmap *, varray_type *);
static tree get_reaching_def (tree);
static tree get_value_for (tree, varray_type);
static void set_value_for (tree, tree, varray_type);
@@ -131,12 +138,37 @@ static inline struct def_blocks_d *get_def_blocks_for (tree);
static inline struct def_blocks_d *find_def_blocks_for (tree);
static void htab_statistics (FILE *, htab_t);
+/* Gets phi_state field for VAR. */
+
+static inline enum need_phi_state
+get_phi_state (tree var)
+{
+ if (TREE_CODE (var) == SSA_NAME)
+ return get_ssa_name_ann (var)->need_phi_state;
+ else
+ return var_ann (var)->need_phi_state;
+}
+
+/* Sets phi_state field for VAR to STATE. */
+
+static inline void
+set_phi_state (tree var, enum need_phi_state state)
+{
+ if (TREE_CODE (var) == SSA_NAME)
+ get_ssa_name_ann (var)->need_phi_state = state;
+ else
+ var_ann (var)->need_phi_state = state;
+}
+
/* Return the value associated with variable VAR in TABLE. */
static inline tree
get_value_for (tree var, varray_type table)
{
- return VARRAY_TREE (table, var_ann (var)->uid);
+ if (TREE_CODE (var) == SSA_NAME)
+ return VARRAY_TREE (table, SSA_NAME_VERSION (var));
+ else
+ return VARRAY_TREE (table, var_ann (var)->uid);
}
@@ -145,7 +177,10 @@ get_value_for (tree var, varray_type table)
static inline void
set_value_for (tree var, tree value, varray_type table)
{
- VARRAY_TREE (table, var_ann (var)->uid) = value;
+ if (TREE_CODE (var) == SSA_NAME)
+ VARRAY_TREE (table, SSA_NAME_VERSION (var)) = value;
+ else
+ VARRAY_TREE (table, var_ann (var)->uid) = value;
}
@@ -157,20 +192,18 @@ set_value_for (tree var, tree value, varray_type table)
to include global livein (i.e., it modifies the underlying bitmap
for LIVEIN). */
-static void
+void
compute_global_livein (bitmap livein, bitmap def_blocks)
{
basic_block bb, *worklist, *tos;
+ int index;
tos = worklist
- = (basic_block *) xmalloc (sizeof (basic_block) * (last_basic_block + 1));
+ = (basic_block *) xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
/* Initialize the worklist. */
- FOR_EACH_BB (bb)
- {
- if (bitmap_bit_p (livein, bb->index))
- *tos++ = bb;
- }
+ EXECUTE_IF_SET_IN_BITMAP (livein, 0, index,
+ *tos++ = BASIC_BLOCK (index));
/* Iterate until the worklist is empty. */
while (tos != worklist)
@@ -214,6 +247,65 @@ mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
sbitmap_zero (kills);
}
+/* Block initialization routine for mark_def_sites. Clear the
+ KILLS bitmap at the start of each block. */
+
+static void
+ssa_mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
+ basic_block bb)
+{
+ struct mark_def_sites_global_data *gd = walk_data->global_data;
+ sbitmap kills = gd->kills;
+ tree phi, def;
+ unsigned def_uid;
+
+ sbitmap_zero (kills);
+
+ for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
+ {
+ def = PHI_RESULT (phi);
+ def_uid = SSA_NAME_VERSION (def);
+
+ if (!TEST_BIT (gd->names_to_rename, def_uid))
+ continue;
+
+ gd->ssa_names[def_uid] = def;
+
+ set_def_block (def, bb, true);
+ SET_BIT (kills, def_uid);
+ }
+}
+
+/* Marks ssa names used as arguments of phis at the end of BB. */
+
+static void
+ssa_mark_phi_uses (struct dom_walk_data *walk_data, basic_block bb)
+{
+ struct mark_def_sites_global_data *gd = walk_data->global_data;
+ sbitmap kills = gd->kills;
+ edge e;
+ tree phi, use;
+ unsigned uid;
+
+ for (e = bb->succ; e; e = e->succ_next)
+ {
+ if (e->dest == EXIT_BLOCK_PTR)
+ continue;
+
+ for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
+ {
+ use = phi_element_for_edge (phi, e)->def;
+ if (TREE_CODE (use) != SSA_NAME)
+ continue;
+
+ uid = SSA_NAME_VERSION (use);
+
+ if (TEST_BIT (gd->names_to_rename, uid)
+ && !TEST_BIT (kills, uid))
+ set_livein_block (use, bb);
+ }
+ }
+}
/* Call back for walk_dominator_tree used to collect definition sites
for every variable in the function. For every statement S in block
@@ -291,7 +383,7 @@ mark_def_sites (struct dom_walk_data *walk_data,
if (!TEST_BIT (kills, uid))
set_livein_block (VDEF_OP (vdefs, i), bb);
- set_def_block (VDEF_RESULT (vdefs, i), bb);
+ set_def_block (VDEF_RESULT (vdefs, i), bb, false);
}
}
@@ -304,25 +396,113 @@ mark_def_sites (struct dom_walk_data *walk_data,
if (prepare_operand_for_rename (def_p, &uid))
{
- set_def_block (*def_p, bb);
+ set_def_block (*def_p, bb, false);
SET_BIT (kills, uid);
}
}
}
+/* Ditto, but works over ssa names. */
-/* Mark block BB as the definition site for variable VAR. */
+static void
+ssa_mark_def_sites (struct dom_walk_data *walk_data,
+ basic_block bb,
+ block_stmt_iterator bsi)
+{
+ struct mark_def_sites_global_data *gd = walk_data->global_data;
+ sbitmap kills = gd->kills;
+ vdef_optype vdefs;
+ vuse_optype vuses;
+ def_optype defs;
+ use_optype uses;
+ size_t i, uid, def_uid;
+ tree stmt, use, def;
+ stmt_ann_t ann;
+
+ /* Mark all the blocks that have definitions for each variable in the
+ names_to_rename bitmap. */
+ stmt = bsi_stmt (bsi);
+ get_stmt_operands (stmt);
+ ann = stmt_ann (stmt);
+
+ /* If a variable is used before being set, then the variable is live
+ across a block boundary, so mark it live-on-entry to BB. */
+ uses = USE_OPS (ann);
+ for (i = 0; i < NUM_USES (uses); i++)
+ {
+ use = USE_OP (uses, i);
+ uid = SSA_NAME_VERSION (use);
+
+ if (TEST_BIT (gd->names_to_rename, uid)
+ && !TEST_BIT (kills, uid))
+ set_livein_block (use, bb);
+ }
+
+ /* Similarly for virtual uses. */
+ vuses = VUSE_OPS (ann);
+ for (i = 0; i < NUM_VUSES (vuses); i++)
+ {
+ use = VUSE_OP (vuses, i);
+ uid = SSA_NAME_VERSION (use);
+
+ if (TEST_BIT (gd->names_to_rename, uid)
+ && !TEST_BIT (kills, uid))
+ set_livein_block (use, bb);
+ }
+
+ vdefs = VDEF_OPS (ann);
+ for (i = 0; i < NUM_VDEFS (vdefs); i++)
+ {
+ def = VDEF_RESULT (vdefs, i);
+ def_uid = SSA_NAME_VERSION (def);
+
+ use = VDEF_OP (vdefs, i);
+ uid = SSA_NAME_VERSION (use);
+
+ if (TEST_BIT (gd->names_to_rename, uid)
+ && !TEST_BIT (kills, uid))
+ set_livein_block (use, bb);
+
+ if (TEST_BIT (gd->names_to_rename, def_uid))
+ {
+ gd->ssa_names[def_uid] = def;
+ set_def_block (def, bb, false);
+ SET_BIT (kills, def_uid);
+ }
+ }
+
+ /* Now process the definition made by this statement. Mark the
+ variables in KILLS. */
+ defs = DEF_OPS (ann);
+ for (i = 0; i < NUM_DEFS (defs); i++)
+ {
+ def = DEF_OP (defs, i);
+ def_uid = SSA_NAME_VERSION (def);
+
+ if (TEST_BIT (gd->names_to_rename, def_uid))
+ {
+ gd->ssa_names[def_uid] = def;
+ set_def_block (def, bb, false);
+ SET_BIT (kills, def_uid);
+ }
+ }
+}
+
+/* Mark block BB as the definition site for variable VAR. PHI_P is true if
+ VAR is defined by a phi node. */
static void
-set_def_block (tree var, basic_block bb)
+set_def_block (tree var, basic_block bb, bool phi_p)
{
struct def_blocks_d *db_p;
- enum need_phi_state state = var_ann (var)->need_phi_state;
+ enum need_phi_state state = get_phi_state (var);
db_p = get_def_blocks_for (var);
/* Set the bit corresponding to the block where VAR is defined. */
bitmap_set_bit (db_p->def_blocks, bb->index);
+ if (phi_p)
+ bitmap_set_bit (db_p->phi_blocks, bb->index);
/* Keep track of whether or not we may need to insert phi nodes.
@@ -337,9 +517,9 @@ set_def_block (tree var, basic_block bb)
definition(s). In this case we may need a PHI node, so enter
state NEED_PHI_STATE_MAYBE. */
if (state == NEED_PHI_STATE_UNKNOWN)
- var_ann (var)->need_phi_state = NEED_PHI_STATE_NO;
+ set_phi_state (var, NEED_PHI_STATE_NO);
else
- var_ann (var)->need_phi_state = NEED_PHI_STATE_MAYBE;
+ set_phi_state (var, NEED_PHI_STATE_MAYBE);
}
@@ -349,7 +529,7 @@ static void
set_livein_block (tree var, basic_block bb)
{
struct def_blocks_d *db_p;
- enum need_phi_state state = var_ann (var)->need_phi_state;
+ enum need_phi_state state = get_phi_state (var);
db_p = get_def_blocks_for (var);
@@ -369,10 +549,10 @@ set_livein_block (tree var, basic_block bb)
if (def_block_index == -1
|| ! dominated_by_p (CDI_DOMINATORS, bb,
BASIC_BLOCK (def_block_index)))
- var_ann (var)->need_phi_state = NEED_PHI_STATE_MAYBE;
+ set_phi_state (var, NEED_PHI_STATE_MAYBE);
}
else
- var_ann (var)->need_phi_state = NEED_PHI_STATE_MAYBE;
+ set_phi_state (var, NEED_PHI_STATE_MAYBE);
}
@@ -405,41 +585,57 @@ prepare_operand_for_rename (tree *op_p, size_t *uid_p)
/* Helper for insert_phi_nodes. If VAR needs PHI nodes, insert them
- at the dominance frontier (DFS) of blocks defining VAR. */
+ at the dominance frontier (DFS) of blocks defining VAR.
+ WORK_STACK is the varray used to implement the worklist of basic
+ blocks. */
static inline
-void insert_phi_nodes_1 (tree var, bitmap *dfs)
+void insert_phi_nodes_1 (tree var, bitmap *dfs, varray_type *work_stack)
{
- var_ann_t ann = var_ann (var);
- if (ann->need_phi_state != NEED_PHI_STATE_NO)
- insert_phi_nodes_for (var, dfs);
+ if (get_phi_state (var) != NEED_PHI_STATE_NO)
+ insert_phi_nodes_for (var, dfs, work_stack);
}
-
/* Insert PHI nodes at the dominance frontier of blocks with variable
definitions. DFS contains the dominance frontier information for
the flowgraph. PHI nodes will only be inserted at the dominance
frontier of definition blocks for variables whose NEED_PHI_STATE
annotation is marked as ``maybe'' or ``unknown'' (computed by
- mark_def_sites). */
+ mark_def_sites). If NAMES_TO_RENAME is not NULL, do the same but
+ for ssa name rewriting. SSA_NAMES then maps versions to ssa names. */
static void
-insert_phi_nodes (bitmap *dfs)
+insert_phi_nodes (bitmap *dfs, bitmap names_to_rename, tree *ssa_names)
{
size_t i;
+ varray_type work_stack;
timevar_push (TV_TREE_INSERT_PHI_NODES);
+ /* Array WORK_STACK is a stack of CFG blocks. Each block that contains
+ an assignment or PHI node will be pushed to this stack. */
+ VARRAY_GENERIC_PTR_NOGC_INIT (work_stack, last_basic_block, "work_stack");
+
/* Iterate over all variables in VARS_TO_RENAME. For each variable, add
to the work list all the blocks that have a definition for the
variable. PHI nodes will be added to the dominance frontier blocks of
each definition block. */
- if (vars_to_rename)
+ if (names_to_rename)
+ {
+ EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i,
+ {
+ if (ssa_names[i])
+ insert_phi_nodes_1 (ssa_names[i], dfs, &work_stack);
+ });
+ }
+ else if (vars_to_rename)
EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i,
- insert_phi_nodes_1 (referenced_var (i), dfs));
+ insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack));
else
for (i = 0; i < num_referenced_vars; i++)
- insert_phi_nodes_1 (referenced_var (i), dfs);
+ insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack);
+
+ VARRAY_FREE (work_stack);
timevar_pop (TV_TREE_INSERT_PHI_NODES);
}
@@ -519,6 +715,66 @@ rewrite_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
}
}
+/* Creates a duplicate of a ssa name NAME defined in statement STMT. */
+
+tree
+duplicate_ssa_name (tree name, tree stmt)
+{
+ tree new_name = make_ssa_name (SSA_NAME_VAR (name), stmt);
+ ssa_name_ann_t old_ann = get_ssa_name_ann (name);
+ ssa_name_ann_t ann = get_ssa_name_ann (new_name);
+
+ *ann = *old_ann;
+ if (old_ann->pt_vars)
+ {
+ ann->pt_vars = BITMAP_GGC_ALLOC ();
+ bitmap_copy (ann->pt_vars, old_ann->pt_vars);
+ }
+
+ return new_name;
+}
+
+/* Ditto, for rewriting ssa names. */
+
+static void
+ssa_rewrite_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
+{
+ tree phi, new_name;
+ struct rewrite_block_data *bd
+ = (struct rewrite_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
+ sbitmap names_to_rename = walk_data->global_data;
+ edge e;
+ bool abnormal_phi;
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
+
+ for (e = bb->pred; e; e = e->pred_next)
+ if (e->flags & EDGE_ABNORMAL)
+ break;
+ abnormal_phi = (e != NULL);
+
+ /* Step 1. Register new definitions for every PHI node in the block.
+ Conceptually, all the PHI nodes are executed in parallel and each PHI
+ node introduces a new version for the associated variable. */
+ for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
+ {
+ tree result = PHI_RESULT (phi);
+
+ if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (result)))
+ {
+ new_name = duplicate_ssa_name (result, phi);
+ PHI_RESULT (phi) = new_name;
+
+ if (abnormal_phi)
+ SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
+ }
+ else
+ new_name = result;
+
+ register_new_def (result, new_name, &bd->block_defs, currdefs);
+ }
+}
/* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
PHI nodes. For every PHI node found, add a new argument containing the
@@ -551,6 +807,38 @@ rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
}
}
+/* Ditto, for ssa name rewriting. */
+
+static void
+ssa_rewrite_phi_arguments (struct dom_walk_data *walk_data, basic_block bb)
+{
+ edge e;
+ sbitmap names_to_rename = walk_data->global_data;
+ tree *op;
+
+ for (e = bb->succ; e; e = e->succ_next)
+ {
+ tree phi;
+
+ if (e->dest == EXIT_BLOCK_PTR)
+ continue;
+
+ for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
+ {
+ op = &phi_element_for_edge (phi, e)->def;
+ if (TREE_CODE (*op) != SSA_NAME)
+ continue;
+
+ if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (*op)))
+ continue;
+
+ *op = get_reaching_def (*op);
+ if (e->flags & EDGE_ABNORMAL)
+ SSA_NAME_OCCURS_IN_ABNORMAL_PHI (*op) = 1;
+ }
+ }
+}
+
/* SSA Rewriting Step 5. Restore the current reaching definition for each
variable referenced in the block (in reverse order). */
@@ -583,6 +871,29 @@ rewrite_finalize_block (struct dom_walk_data *walk_data,
}
}
+/* Ditto, for rewriting ssa names. */
+
+static void
+ssa_rewrite_finalize_block (struct dom_walk_data *walk_data,
+ basic_block bb ATTRIBUTE_UNUSED)
+{
+ struct rewrite_block_data *bd
+ = (struct rewrite_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
+
+ /* Step 5. Restore the current reaching definition for each variable
+ referenced in the block (in reverse order). */
+ while (bd->block_defs && VARRAY_ACTIVE_SIZE (bd->block_defs) > 0)
+ {
+ tree var;
+ tree saved_def = VARRAY_TOP_TREE (bd->block_defs);
+ VARRAY_POP (bd->block_defs);
+
+ var = VARRAY_TOP_TREE (bd->block_defs);
+ VARRAY_POP (bd->block_defs);
+
+ set_value_for (var, saved_def, currdefs);
+ }
+}
/* Dump SSA information to FILE. */
@@ -650,29 +961,28 @@ htab_statistics (FILE *file, htab_t htab)
/* Insert PHI nodes for variable VAR using the dominance frontier
- information given in DFS. */
+ information given in DFS. WORK_STACK is the varray used to
+ implement the worklist of basic blocks. */
static void
-insert_phi_nodes_for (tree var, bitmap *dfs)
+insert_phi_nodes_for (tree var, bitmap *dfs, varray_type *work_stack)
{
struct def_blocks_d *def_map;
bitmap phi_insertion_points;
int bb_index;
- varray_type work_stack;
+ edge e;
+ tree phi;
+ basic_block bb;
def_map = find_def_blocks_for (var);
if (def_map == NULL)
return;
- /* Array WORK_STACK is a stack of CFG blocks. Each block that contains
- an assignment or PHI node will be pushed to this stack. */
- VARRAY_BB_INIT (work_stack, last_basic_block, "work_stack");
-
phi_insertion_points = BITMAP_XMALLOC ();
EXECUTE_IF_SET_IN_BITMAP (def_map->def_blocks, 0, bb_index,
{
- VARRAY_PUSH_BB (work_stack, BASIC_BLOCK (bb_index));
+ VARRAY_PUSH_GENERIC_PTR_NOGC (*work_stack, BASIC_BLOCK (bb_index));
});
/* Pop a block off the worklist, add every block that appears in
@@ -687,13 +997,14 @@ insert_phi_nodes_for (tree var, bitmap *dfs)
determine if fully pruned or semi pruned SSA form was appropriate.
We now always use fully pruned SSA form. */
- while (VARRAY_ACTIVE_SIZE (work_stack) > 0)
+ while (VARRAY_ACTIVE_SIZE (*work_stack) > 0)
{
- basic_block bb = VARRAY_TOP_BB (work_stack);
- int bb_index = bb->index;
int dfs_index;
- VARRAY_POP (work_stack);
+ bb = VARRAY_TOP_GENERIC_PTR_NOGC (*work_stack);
+ bb_index = bb->index;
+
+ VARRAY_POP (*work_stack);
EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index],
phi_insertion_points,
@@ -701,11 +1012,15 @@ insert_phi_nodes_for (tree var, bitmap *dfs)
{
basic_block bb = BASIC_BLOCK (dfs_index);
- VARRAY_PUSH_BB (work_stack, bb);
+ VARRAY_PUSH_GENERIC_PTR_NOGC (*work_stack, bb);
bitmap_set_bit (phi_insertion_points, dfs_index);
});
}
+ /* Remove the blocks where we already have the phis. */
+ bitmap_operation (phi_insertion_points, phi_insertion_points,
+ def_map->phi_blocks, BITMAP_AND_COMPL);
+
/* Now compute global livein for this variable. Note this modifies
def_map->livein_blocks. */
compute_global_livein (def_map->livein_blocks, def_map->def_blocks);
@@ -713,11 +1028,22 @@ insert_phi_nodes_for (tree var, bitmap *dfs)
/* And insert the PHI nodes. */
EXECUTE_IF_AND_IN_BITMAP (phi_insertion_points, def_map->livein_blocks,
0, bb_index,
- {
- create_phi_node (var, BASIC_BLOCK (bb_index));
- });
+ do
+ {
+ bb = BASIC_BLOCK (bb_index);
- BITMAP_FREE (phi_insertion_points);
+ phi = create_phi_node (var, bb);
+
+ /* If we are rewriting ssa names, add also the phi arguments. */
+ if (TREE_CODE (var) == SSA_NAME)
+ {
+ for (e = bb->pred; e; e = e->pred_next)
+ add_phi_arg (&phi, var, e);
+ }
+ }
+ while (0));
+
+ BITMAP_XFREE (phi_insertion_points);
}
/* SSA Rewriting Step 2. Rewrite every variable used in each statement in
@@ -800,6 +1126,93 @@ rewrite_stmt (struct dom_walk_data *walk_data,
}
}
+/* Ditto, for rewriting ssa names. */
+
+static void
+ssa_rewrite_stmt (struct dom_walk_data *walk_data,
+ basic_block bb ATTRIBUTE_UNUSED,
+ block_stmt_iterator si)
+{
+ size_t i;
+ stmt_ann_t ann;
+ tree stmt, var, *use_p;
+ vuse_optype vuses;
+ vdef_optype vdefs;
+ def_optype defs;
+ use_optype uses;
+ struct rewrite_block_data *bd;
+ sbitmap names_to_rename = walk_data->global_data;
+
+ bd = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
+
+ stmt = bsi_stmt (si);
+ ann = stmt_ann (stmt);
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ {
+ fprintf (dump_file, "Renaming statement ");
+ print_generic_stmt (dump_file, stmt, TDF_SLIM);
+ fprintf (dump_file, "\n");
+ }
+
+#if defined ENABLE_CHECKING
+ /* We have just scanned the code for operands. No statement should
+ be modified. */
+ if (ann->modified)
+ abort ();
+#endif
+
+ defs = DEF_OPS (ann);
+ uses = USE_OPS (ann);
+ vuses = VUSE_OPS (ann);
+ vdefs = VDEF_OPS (ann);
+
+ /* Step 1. Rewrite USES and VUSES in the statement. */
+ for (i = 0; i < NUM_USES (uses); i++)
+ {
+ use_p = USE_OP_PTR (uses, i);
+ if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (*use_p)))
+ *use_p = get_reaching_def (*use_p);
+ }
+
+ /* Rewrite virtual uses in the statement. */
+ for (i = 0; i < NUM_VUSES (vuses); i++)
+ {
+ use_p = VUSE_OP_PTR (vuses, i);
+ if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (*use_p)))
+ *use_p = get_reaching_def (*use_p);
+ }
+
+ /* Step 2. Register the statement's DEF and VDEF operands. */
+ for (i = 0; i < NUM_DEFS (defs); i++)
+ {
+ tree *def_p = DEF_OP_PTR (defs, i);
+ var = *def_p;
+
+ if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (*def_p)))
+ continue;
+
+ *def_p = duplicate_ssa_name (var, stmt);
+ register_new_def (var, *def_p, &bd->block_defs, currdefs);
+ }
+
+ /* Register new virtual definitions made by the statement. */
+ for (i = 0; i < NUM_VDEFS (vdefs); i++)
+ {
+ tree *def_p = VDEF_RESULT_PTR (vdefs, i);
+ var = *def_p;
+
+ use_p = VDEF_OP_PTR (vdefs, i);
+ if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (*use_p)))
+ *use_p = get_reaching_def (*use_p);
+
+ if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (*def_p)))
+ continue;
+
+ *def_p = duplicate_ssa_name (var, stmt);
+ register_new_def (var, *def_p, &bd->block_defs, currdefs);
+ }
+}
/* Replace the operand pointed by OP_P with its immediate reaching
definition. */
@@ -811,7 +1224,6 @@ rewrite_operand (tree *op_p)
*op_p = get_reaching_def (*op_p);
}
-
/* Register DEF to be a new definition for variable VAR and push VAR's
current reaching definition into the stack pointed by BLOCK_DEFS_P.
IS_REAL_OPERAND is true when DEF is a real definition. */
@@ -828,7 +1240,8 @@ register_new_def (tree var, tree def,
/* If the current reaching definition is NULL, push the variable itself
so that the dominator tree callbacks know what variable is associated
with this NULL reaching def when unwinding the *BLOCK_DEFS_P stack. */
- if (currdef == NULL_TREE)
+ if (currdef == NULL_TREE
+ || TREE_CODE (var) == SSA_NAME)
VARRAY_PUSH_TREE (*block_defs_p, var);
/* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
@@ -851,7 +1264,7 @@ register_new_def (tree var, tree def,
static tree
get_reaching_def (tree var)
{
- tree default_d, currdef_var;
+ tree default_d, currdef_var, avar;
/* Lookup the current reaching definition for VAR. */
default_d = NULL_TREE;
@@ -861,11 +1274,16 @@ get_reaching_def (tree var)
default definition for it (if needed). */
if (currdef_var == NULL_TREE)
{
- default_d = default_def (var);
+ if (TREE_CODE (var) == SSA_NAME)
+ avar = SSA_NAME_VAR (var);
+ else
+ avar = var;
+
+ default_d = default_def (avar);
if (default_d == NULL_TREE)
{
- default_d = make_ssa_name (var, build_empty_stmt ());
- set_default_def (var, default_d);
+ default_d = make_ssa_name (avar, build_empty_stmt ());
+ set_default_def (avar, default_d);
}
set_value_for (var, default_d, currdefs);
}
@@ -898,8 +1316,9 @@ static void
def_blocks_free (void *p)
{
struct def_blocks_d *entry = p;
- BITMAP_FREE (entry->def_blocks);
- BITMAP_FREE (entry->livein_blocks);
+ BITMAP_XFREE (entry->def_blocks);
+ BITMAP_XFREE (entry->phi_blocks);
+ BITMAP_XFREE (entry->livein_blocks);
free (entry);
}
@@ -965,6 +1384,7 @@ get_def_blocks_for (tree var)
db_p = xmalloc (sizeof (*db_p));
db_p->var = var;
db_p->def_blocks = BITMAP_XMALLOC ();
+ db_p->phi_blocks = BITMAP_XMALLOC ();
db_p->livein_blocks = BITMAP_XMALLOC ();
*slot = (void *) db_p;
}
@@ -996,24 +1416,40 @@ get_def_blocks_for (tree var)
(rewrite_stmt).
Steps 3 and 5 are done using the dominator tree walker
- (walk_dominator_tree). */
+ (walk_dominator_tree).
+
+ ALL is true if all variables should be renamed (otherwise just those
+ mentioned in vars_to_rename are taken into account). */
void
-rewrite_into_ssa (void)
+rewrite_into_ssa (bool all)
{
bitmap *dfs;
basic_block bb;
struct dom_walk_data walk_data;
struct mark_def_sites_global_data mark_def_sites_global_data;
+ bitmap old_vars_to_rename = vars_to_rename;
timevar_push (TV_TREE_SSA_OTHER);
- /* Initialize the array of variables to rename. */
- if (vars_to_rename != NULL)
+ if (all)
+ vars_to_rename = NULL;
+ else
{
size_t i;
bool rename_name_tags_p;
+ /* Initialize the array of variables to rename. */
+
+ if (vars_to_rename == NULL)
+ abort ();
+
+ if (bitmap_first_set_bit (vars_to_rename) < 0)
+ {
+ timevar_pop (TV_TREE_SSA_OTHER);
+ return;
+ }
+
/* If any of the variables in VARS_TO_RENAME is a pointer, we need to
invalidate all the name memory tags associated with the variables
that we are about to rename. FIXME: Currently we just invalidate
@@ -1102,7 +1538,7 @@ rewrite_into_ssa (void)
sbitmap_free (mark_def_sites_global_data.kills);
/* Insert PHI nodes at dominance frontiers of definition blocks. */
- insert_phi_nodes (dfs);
+ insert_phi_nodes (dfs, NULL, NULL);
/* Rewrite all the basic blocks in the program. */
timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
@@ -1147,14 +1583,164 @@ rewrite_into_ssa (void)
htab_delete (def_blocks);
VARRAY_CLEAR (currdefs);
+ vars_to_rename = old_vars_to_rename;
timevar_pop (TV_TREE_SSA_OTHER);
}
+/* The ssa names in NAMES_TO_RENAME may have more than one definition;
+ add phi nodes and rewrite them to fix this. */
+
+void
+rewrite_ssa_into_ssa (bitmap names_to_rename)
+{
+ bitmap *dfs;
+ basic_block bb;
+ struct dom_walk_data walk_data;
+ struct mark_def_sites_global_data mark_def_sites_global_data;
+ tree *ssa_names;
+ unsigned i;
+ sbitmap snames_to_rename;
+
+ if (bitmap_first_set_bit (names_to_rename) < 0)
+ return;
+
+ timevar_push (TV_TREE_SSA_OTHER);
+
+ /* Allocate memory for the DEF_BLOCKS hash table. */
+ def_blocks = htab_create (highest_ssa_version,
+ def_blocks_hash, def_blocks_eq, def_blocks_free);
+
+ VARRAY_TREE_INIT (currdefs, highest_ssa_version, "currdefs");
+
+ ssa_names = xcalloc (highest_ssa_version, sizeof (tree));
+
+ /* Initialize dominance frontier and immediate dominator bitmaps.
+ Also count the number of predecessors for each block. Doing so
+ can save significant time during PHI insertion for large graphs. */
+ dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
+ FOR_EACH_BB (bb)
+ {
+ edge e;
+ int count = 0;
+
+ for (e = bb->pred; e; e = e->pred_next)
+ count++;
+
+ bb_ann (bb)->num_preds = count;
+ dfs[bb->index] = BITMAP_XMALLOC ();
+ }
+
+ /* Ensure that the dominance information is OK. */
+ calculate_dominance_info (CDI_DOMINATORS);
+
+ /* Compute dominance frontiers. */
+ compute_dominance_frontiers (dfs);
+
+ /* Setup callbacks for the generic dominator tree walker to find and
+ mark definition sites. */
+ walk_data.walk_stmts_backward = false;
+ walk_data.dom_direction = CDI_DOMINATORS;
+ walk_data.initialize_block_local_data = NULL;
+ walk_data.before_dom_children_before_stmts
+ = ssa_mark_def_sites_initialize_block;
+ walk_data.before_dom_children_walk_stmts = ssa_mark_def_sites;
+ walk_data.before_dom_children_after_stmts = ssa_mark_phi_uses;
+ walk_data.after_dom_children_before_stmts = NULL;
+ walk_data.after_dom_children_walk_stmts = NULL;
+ walk_data.after_dom_children_after_stmts = NULL;
+
+ snames_to_rename = sbitmap_alloc (highest_ssa_version);
+ sbitmap_zero (snames_to_rename);
+ EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i,
+ SET_BIT (snames_to_rename, i));
+
+ mark_def_sites_global_data.kills = sbitmap_alloc (highest_ssa_version);
+ mark_def_sites_global_data.names_to_rename = snames_to_rename;
+ mark_def_sites_global_data.ssa_names = ssa_names;
+ walk_data.global_data = &mark_def_sites_global_data;
+
+ /* We do not have any local data. */
+ walk_data.block_local_data_size = 0;
+
+ /* Initialize the dominator walker. */
+ init_walk_dominator_tree (&walk_data);
+
+ /* Recursively walk the dominator tree. */
+ walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
+
+ /* Finalize the dominator walker. */
+ fini_walk_dominator_tree (&walk_data);
+
+ /* We no longer need this bitmap, clear and free it. */
+ sbitmap_free (mark_def_sites_global_data.kills);
+
+ /* Insert PHI nodes at dominance frontiers of definition blocks. */
+ insert_phi_nodes (dfs, names_to_rename, ssa_names);
+
+ /* Rewrite all the basic blocks in the program. */
+ timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
+
+ /* Setup callbacks for the generic dominator tree walker. */
+ walk_data.walk_stmts_backward = false;
+ walk_data.dom_direction = CDI_DOMINATORS;
+ walk_data.initialize_block_local_data
+ = rewrite_initialize_block_local_data;
+ walk_data.before_dom_children_before_stmts = ssa_rewrite_initialize_block;
+ walk_data.before_dom_children_walk_stmts = ssa_rewrite_stmt;
+ walk_data.before_dom_children_after_stmts = ssa_rewrite_phi_arguments;
+ walk_data.after_dom_children_before_stmts = NULL;
+ walk_data.after_dom_children_walk_stmts = NULL;
+ walk_data.after_dom_children_after_stmts = ssa_rewrite_finalize_block;
+ walk_data.global_data = snames_to_rename;
+ walk_data.block_local_data_size = sizeof (struct rewrite_block_data);
+
+ /* Initialize the dominator walker. */
+ init_walk_dominator_tree (&walk_data);
+
+ /* Recursively walk the dominator tree rewriting each statement in
+ each basic block. */
+ walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
+
+ /* Finalize the dominator walker. */
+ fini_walk_dominator_tree (&walk_data);
+
+ sbitmap_free (snames_to_rename);
+
+ timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
+
+ /* Debugging dumps. */
+ if (dump_file && (dump_flags & TDF_STATS))
+ {
+ dump_dfa_stats (dump_file);
+ dump_tree_ssa_stats (dump_file);
+ }
+
+ /* Free allocated memory. */
+ FOR_EACH_BB (bb)
+ BITMAP_XFREE (dfs[bb->index]);
+ free (dfs);
+
+ htab_delete (def_blocks);
+ VARRAY_CLEAR (currdefs);
+
+ free (ssa_names);
+
+ timevar_pop (TV_TREE_SSA_OTHER);
+}
+
+/* Rewrites all variables into ssa. */
+
+static void
+rewrite_all_into_ssa (void)
+{
+ rewrite_into_ssa (true);
+}
+
struct tree_opt_pass pass_build_ssa =
{
"ssa", /* name */
NULL, /* gate */
- rewrite_into_ssa, /* execute */
+ rewrite_all_into_ssa, /* execute */
NULL, /* sub */
NULL, /* next */
0, /* static_pass_number */