+ /* Mark the unwind point for this block. */
+ VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
+
+ /* 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 = PHI_CHAIN (phi))
+ {
+ tree result = PHI_RESULT (phi);
+ register_new_def (result, &block_defs_stack);
+ }
+}
+
+
+/* Return the current definition for variable VAR. If none is found,
+ create a new SSA name to act as the zeroth definition for VAR. If VAR
+ is call clobbered and there exists a more recent definition of
+ GLOBAL_VAR, return the definition for GLOBAL_VAR. This means that VAR
+ has been clobbered by a function call since its last assignment. */
+
+static tree
+get_reaching_def (tree var)
+{
+ tree currdef_var, avar;
+
+ /* Lookup the current reaching definition for VAR. */
+ currdef_var = get_current_def (var);
+
+ /* If there is no reaching definition for VAR, create and register a
+ default definition for it (if needed). */
+ if (currdef_var == NULL_TREE)
+ {
+ avar = DECL_P (var) ? var : SSA_NAME_VAR (var);
+ currdef_var = get_default_def_for (avar);
+ set_current_def (var, currdef_var);
+ }
+
+ /* Return the current reaching definition for VAR, or the default
+ definition, if we had to create one. */
+ return currdef_var;
+}
+
+
+/* SSA Rewriting Step 2. Rewrite every variable used in each statement in
+ the block with its immediate reaching definitions. Update the current
+ definition of a variable when a new real or virtual definition is found. */
+
+static void
+rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+ basic_block bb ATTRIBUTE_UNUSED,
+ block_stmt_iterator si)
+{
+ tree stmt;
+ use_operand_p use_p;
+ def_operand_p def_p;
+ ssa_op_iter iter;
+
+ stmt = bsi_stmt (si);
+
+ /* If mark_def_sites decided that we don't need to rewrite this
+ statement, ignore it. */
+ if (!REWRITE_THIS_STMT (stmt) && !REGISTER_DEFS_IN_THIS_STMT (stmt))
+ return;
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ {
+ fprintf (dump_file, "Renaming statement ");
+ print_generic_stmt (dump_file, stmt, TDF_SLIM);
+ fprintf (dump_file, "\n");
+ }
+
+ /* Step 1. Rewrite USES and VUSES in the statement. */
+ if (REWRITE_THIS_STMT (stmt))
+ FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
+ SSA_OP_ALL_USES|SSA_OP_ALL_KILLS)
+ {
+ tree var = USE_FROM_PTR (use_p);
+ gcc_assert (DECL_P (var));
+ SET_USE (use_p, get_reaching_def (var));
+ }
+
+ /* Step 2. Register the statement's DEF and VDEF operands. */
+ if (REGISTER_DEFS_IN_THIS_STMT (stmt))
+ FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
+ {
+ tree var = DEF_FROM_PTR (def_p);
+ gcc_assert (DECL_P (var));
+ SET_DEF (def_p, make_ssa_name (var, stmt));
+ register_new_def (DEF_FROM_PTR (def_p), &block_defs_stack);
+ }
+}
+
+
+/* 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
+ current reaching definition for the variable and the edge through which
+ that definition is reaching the PHI node. */
+
+static void
+rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+ basic_block bb)
+{
+ edge e;
+ edge_iterator ei;
+
+ FOR_EACH_EDGE (e, ei, bb->succs)
+ {
+ tree phi;
+
+ for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
+ {
+ tree currdef;
+ currdef = get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi)));
+ add_phi_arg (phi, currdef, e);
+ }
+ }
+}
+
+
+/* Called after visiting basic block BB. Restore CURRDEFS to its
+ original value. */
+
+static void
+rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+ basic_block bb ATTRIBUTE_UNUSED)
+{
+ /* Restore CURRDEFS to its original state. */
+ while (VEC_length (tree, block_defs_stack) > 0)
+ {
+ tree tmp = VEC_pop (tree, block_defs_stack);
+ tree saved_def, var;
+
+ if (tmp == NULL_TREE)
+ break;
+
+ /* If we recorded an SSA_NAME, then make the SSA_NAME the current
+ definition of its underlying variable. If we recorded anything
+ else, it must have been an _DECL node and its current reaching
+ definition must have been NULL. */
+ if (TREE_CODE (tmp) == SSA_NAME)
+ {
+ saved_def = tmp;
+ var = SSA_NAME_VAR (saved_def);
+ }
+ else
+ {
+ saved_def = NULL;
+ var = tmp;
+ }
+
+ set_current_def (var, saved_def);
+ }
+}
+
+
+/* Dump SSA information to FILE. */
+
+void
+dump_tree_ssa (FILE *file)
+{
+ basic_block bb;
+ const char *funcname
+ = lang_hooks.decl_printable_name (current_function_decl, 2);
+
+ fprintf (file, "SSA information for %s\n\n", funcname);
+
+ FOR_EACH_BB (bb)
+ {
+ dump_bb (bb, file, 0);
+ fputs (" ", file);
+ print_generic_stmt (file, phi_nodes (bb), dump_flags);
+ fputs ("\n\n", file);
+ }
+}
+
+
+/* Dump SSA information to stderr. */
+
+void
+debug_tree_ssa (void)
+{
+ dump_tree_ssa (stderr);
+}
+
+
+/* Dump statistics for the hash table HTAB. */
+
+static void
+htab_statistics (FILE *file, htab_t htab)
+{
+ fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
+ (long) htab_size (htab),
+ (long) htab_elements (htab),
+ htab_collisions (htab));
+}
+
+
+/* Dump SSA statistics on FILE. */
+
+void
+dump_tree_ssa_stats (FILE *file)
+{
+ fprintf (file, "\nHash table statistics:\n");
+
+ fprintf (file, " def_blocks: ");
+ htab_statistics (file, def_blocks);
+
+ fprintf (file, "\n");
+}
+
+
+/* Dump SSA statistics on stderr. */
+
+void
+debug_tree_ssa_stats (void)
+{
+ dump_tree_ssa_stats (stderr);
+}
+
+
+/* Hashing and equality functions for DEF_BLOCKS. */
+
+static hashval_t
+def_blocks_hash (const void *p)
+{
+ return htab_hash_pointer
+ ((const void *)((const struct def_blocks_d *)p)->var);
+}
+
+static int
+def_blocks_eq (const void *p1, const void *p2)
+{
+ return ((const struct def_blocks_d *)p1)->var
+ == ((const struct def_blocks_d *)p2)->var;
+}
+
+
+/* Free memory allocated by one entry in DEF_BLOCKS. */
+
+static void
+def_blocks_free (void *p)
+{
+ struct def_blocks_d *entry = p;
+ BITMAP_FREE (entry->def_blocks);
+ BITMAP_FREE (entry->phi_blocks);
+ BITMAP_FREE (entry->livein_blocks);
+ free (entry);
+}
+
+
+/* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
+
+static int
+debug_def_blocks_r (void **slot, void *data ATTRIBUTE_UNUSED)
+{
+ struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
+
+ fprintf (stderr, "VAR: ");
+ print_generic_expr (stderr, db_p->var, dump_flags);
+ bitmap_print (stderr, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
+ bitmap_print (stderr, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}\n");
+
+ return 1;
+}
+
+
+/* Dump the DEF_BLOCKS hash table on stderr. */
+
+void
+debug_def_blocks (void)
+{
+ htab_traverse (def_blocks, debug_def_blocks_r, NULL);
+}
+
+
+/* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
+
+static inline void
+register_new_update_single (tree new_name, tree old_name)
+{
+ tree currdef = get_current_def (old_name);
+
+ /* Push the current reaching definition into *BLOCK_DEFS_P.
+ This stack is later used by the dominator tree callbacks to
+ restore the reaching definitions for all the variables
+ defined in the block after a recursive visit to all its
+ immediately dominated blocks. */
+ VEC_reserve (tree, heap, block_defs_stack, 2);
+ VEC_quick_push (tree, block_defs_stack, currdef);
+ VEC_quick_push (tree, block_defs_stack, old_name);
+
+ /* Set the current reaching definition for OLD_NAME to be
+ NEW_NAME. */
+ set_current_def (old_name, new_name);
+}
+
+
+/* Register NEW_NAME to be the new reaching definition for all the
+ names in OLD_NAMES. Used by the incremental SSA update routines to
+ replace old SSA names with new ones. */
+
+static inline void
+register_new_update_set (tree new_name, bitmap old_names)
+{
+ bitmap_iterator bi;
+ unsigned i;
+
+ EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
+ register_new_update_single (new_name, ssa_name (i));
+}
+
+
+/* Initialization of block data structures for the incremental SSA
+ update pass. Create a block local stack of reaching definitions
+ for new SSA names produced in this block (BLOCK_DEFS). Register
+ new definitions for every PHI node in the block. */
+
+static void
+rewrite_update_init_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+ basic_block bb)
+{
+ edge e;
+ edge_iterator ei;
+ tree phi;
+ bool is_abnormal_phi;
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "\n\nRegistering new PHI nodes in block #%d\n\n",
+ bb->index);
+
+ /* Mark the unwind point for this block. */
+ VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
+
+ /* Mark the LHS if any of the arguments flows through an abnormal
+ edge. */
+ is_abnormal_phi = false;
+ FOR_EACH_EDGE (e, ei, bb->preds)
+ if (e->flags & EDGE_ABNORMAL)
+ {
+ is_abnormal_phi = true;
+ break;
+ }
+
+ /* If any of the PHI nodes is a replacement for a name in
+ OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
+ register it as a new definition for its corresponding name. Also
+ register definitions for names whose underlying symbols are
+ marked for renaming. */
+ for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
+ {
+ tree lhs, lhs_sym;
+
+ if (!REGISTER_DEFS_IN_THIS_STMT (phi))
+ continue;
+
+ lhs = PHI_RESULT (phi);
+ lhs_sym = SSA_NAME_VAR (lhs);
+
+ if (symbol_marked_for_renaming (lhs_sym))
+ register_new_update_single (lhs, lhs_sym);
+ else
+ {
+ /* If LHS is a new name, register a new definition for all
+ the names replaced by LHS. */
+ if (is_new_name (lhs))
+ register_new_update_set (lhs, names_replaced_by (lhs));
+
+ /* If LHS is an OLD name, register it as a new definition
+ for itself. */
+ if (is_old_name (lhs))
+ register_new_update_single (lhs, lhs);
+ }
+
+ if (is_abnormal_phi)
+ SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
+ }
+}
+
+
+/* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
+ the current reaching definition of every name re-written in BB to
+ the original reaching definition before visiting BB. This
+ unwinding must be done in the opposite order to what is done in
+ register_new_update_set. */
+
+static void
+rewrite_update_fini_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+ basic_block bb ATTRIBUTE_UNUSED)
+{
+ while (VEC_length (tree, block_defs_stack) > 0)
+ {
+ tree var = VEC_pop (tree, block_defs_stack);
+ tree saved_def;
+
+ /* NULL indicates the unwind stop point for this block (see
+ rewrite_update_init_block). */
+ if (var == NULL)
+ return;
+
+ saved_def = VEC_pop (tree, block_defs_stack);
+ set_current_def (var, saved_def);
+ }
+}
+
+
+/* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
+ it is a symbol marked for renaming, replace it with USE_P's current
+ reaching definition. */
+
+static inline void
+maybe_replace_use (use_operand_p use_p)
+{
+ tree rdef = NULL_TREE;
+ tree use = USE_FROM_PTR (use_p);
+ tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
+
+ if (symbol_marked_for_renaming (sym))
+ rdef = get_reaching_def (sym);
+ else if (is_old_name (use))
+ rdef = get_reaching_def (use);
+
+ if (rdef && rdef != use)
+ SET_USE (use_p, rdef);
+}
+
+
+/* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
+ or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
+ register it as the current definition for the names replaced by
+ DEF_P. */
+
+static inline void
+maybe_register_def (def_operand_p def_p, tree stmt)
+{
+ tree def = DEF_FROM_PTR (def_p);
+ tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
+
+ /* If DEF is a naked symbol that needs renaming, create a
+ new name for it. */
+ if (symbol_marked_for_renaming (sym))
+ {
+ if (DECL_P (def))
+ {
+ def = make_ssa_name (def, stmt);
+ SET_DEF (def_p, def);
+ }
+
+ register_new_update_single (def, sym);
+ }
+ else
+ {
+ /* If DEF is a new name, register it as a new definition
+ for all the names replaced by DEF. */
+ if (is_new_name (def))
+ register_new_update_set (def, names_replaced_by (def));
+
+ /* If DEF is an old name, register DEF as a new
+ definition for itself. */
+ if (is_old_name (def))
+ register_new_update_single (def, def);
+ }
+}
+
+
+/* Update every variable used in the statement pointed-to by SI. The
+ statement is assumed to be in SSA form already. Names in
+ OLD_SSA_NAMES used by SI will be updated to their current reaching
+ definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
+ will be registered as a new definition for their corresponding name
+ in OLD_SSA_NAMES. */
+
+static void
+rewrite_update_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+ basic_block bb ATTRIBUTE_UNUSED,
+ block_stmt_iterator si)
+{
+ stmt_ann_t ann;
+ tree stmt;
+ use_operand_p use_p;
+ def_operand_p def_p;
+ ssa_op_iter iter;
+
+ stmt = bsi_stmt (si);
+ ann = stmt_ann (stmt);
+
+ /* Only update marked statements. */
+ if (!REWRITE_THIS_STMT (stmt) && !REGISTER_DEFS_IN_THIS_STMT (stmt))
+ return;
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ {
+ fprintf (dump_file, "Updating SSA information for statement ");
+ print_generic_stmt (dump_file, stmt, TDF_SLIM);
+ fprintf (dump_file, "\n");
+ }
+
+ /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
+ symbol is marked for renaming. */
+ if (REWRITE_THIS_STMT (stmt))
+ {
+ FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
+ maybe_replace_use (use_p);
+
+ if (need_to_update_vops_p)
+ FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
+ SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_KILLS)
+ maybe_replace_use (use_p);
+ }
+
+ /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
+ Also register definitions for names whose underlying symbol is
+ marked for renaming. */
+ if (REGISTER_DEFS_IN_THIS_STMT (stmt))
+ {
+ FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
+ maybe_register_def (def_p, stmt);
+
+ if (need_to_update_vops_p)
+ FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_VIRTUAL_DEFS)
+ maybe_register_def (def_p, stmt);
+ }
+}
+
+
+/* Replace the operand pointed by USE_P with USE's current reaching
+ definition. */
+
+static inline void
+replace_use (use_operand_p use_p, tree use)
+{
+ tree rdef = get_reaching_def (use);
+ if (rdef != use)
+ SET_USE (use_p, rdef);
+}
+
+
+/* Visit all the successor blocks of BB looking for PHI nodes. For
+ every PHI node found, check if any of its arguments is in
+ OLD_SSA_NAMES. If so, and if the argument has a current reaching
+ definition, replace it. */
+
+static void
+rewrite_update_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+ basic_block bb)
+{
+ edge e;
+ edge_iterator ei;
+
+ FOR_EACH_EDGE (e, ei, bb->succs)
+ {
+ tree phi;
+
+ for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
+ {
+ tree arg;
+ use_operand_p arg_p;
+
+ /* Skip PHI nodes that are not marked for rewrite. */
+ if (!REWRITE_THIS_STMT (phi))
+ continue;
+
+ arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
+ arg = USE_FROM_PTR (arg_p);
+
+ if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
+ continue;
+
+ if (arg == NULL_TREE)
+ {
+ /* When updating a PHI node for a recently introduced
+ symbol we may find NULL arguments. That's why we
+ take the symbol from the LHS of the PHI node. */
+ replace_use (arg_p, SSA_NAME_VAR (PHI_RESULT (phi)));
+ }
+ else
+ {
+ tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
+
+ if (symbol_marked_for_renaming (sym))
+ replace_use (arg_p, sym);
+ else if (is_old_name (arg))
+ replace_use (arg_p, arg);
+ }
+
+ if (e->flags & EDGE_ABNORMAL)
+ SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
+ }
+ }
+}
+
+
+/* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
+ form.
+
+ ENTRY indicates the block where to start. Every block dominated by
+ ENTRY will be rewritten.
+
+ WHAT indicates what actions will be taken by the renamer (see enum
+ rewrite_mode).
+
+ BLOCKS are the set of interesting blocks for the dominator walker
+ to process. If this set is NULL, then all the nodes dominated
+ by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
+ are not present in BLOCKS are ignored. */
+
+static void
+rewrite_blocks (basic_block entry, enum rewrite_mode what, sbitmap blocks)
+{
+ struct dom_walk_data walk_data;
+
+ /* Rewrite all the basic blocks in the program. */
+ timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
+
+ /* Setup callbacks for the generic dominator tree walker. */
+ memset (&walk_data, 0, sizeof (walk_data));
+
+ walk_data.dom_direction = CDI_DOMINATORS;
+ walk_data.interesting_blocks = blocks;
+
+ if (what == REWRITE_UPDATE)
+ walk_data.before_dom_children_before_stmts = rewrite_update_init_block;
+ else
+ walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
+
+ if (what == REWRITE_ALL)
+ walk_data.before_dom_children_walk_stmts = rewrite_stmt;
+ else if (what == REWRITE_UPDATE)
+ walk_data.before_dom_children_walk_stmts = rewrite_update_stmt;
+ else
+ gcc_unreachable ();
+
+ if (what == REWRITE_ALL)
+ walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
+ else if (what == REWRITE_UPDATE)
+ walk_data.before_dom_children_after_stmts = rewrite_update_phi_arguments;
+ else
+ gcc_unreachable ();
+
+ if (what == REWRITE_ALL)
+ walk_data.after_dom_children_after_stmts = rewrite_finalize_block;
+ else if (what == REWRITE_UPDATE)
+ walk_data.after_dom_children_after_stmts = rewrite_update_fini_block;
+ else
+ gcc_unreachable ();
+
+ block_defs_stack = VEC_alloc (tree, heap, 10);
+
+ /* 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);
+
+ /* Finalize the dominator walker. */
+ fini_walk_dominator_tree (&walk_data);
+
+ /* Debugging dumps. */
+ if (dump_file && (dump_flags & TDF_STATS))
+ {
+ dump_dfa_stats (dump_file);
+ if (def_blocks)
+ dump_tree_ssa_stats (dump_file);
+ }
+
+ if (def_blocks)
+ {
+ htab_delete (def_blocks);
+ def_blocks = NULL;
+ }
+
+ VEC_free (tree, heap, block_defs_stack);
+
+ timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
+}
+
+
+/* Block initialization routine for mark_def_sites. Clear the
+ KILLS bitmap at the start of each block. */
+
+static void
+mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
+ basic_block bb ATTRIBUTE_UNUSED)
+{
+ struct mark_def_sites_global_data *gd = walk_data->global_data;
+ bitmap kills = gd->kills;
+ bitmap_clear (kills);
+}
+
+
+/* Mark the definition site blocks for each variable, so that we know
+ where the variable is actually live.
+
+ INTERESTING_BLOCKS will be filled in with all the blocks that
+ should be processed by the renamer. It is assumed to be
+ initialized and zeroed by the caller. */
+
+static void
+mark_def_site_blocks (sbitmap interesting_blocks)
+{
+ size_t i;
+ struct dom_walk_data walk_data;
+ struct mark_def_sites_global_data mark_def_sites_global_data;
+
+ /* Allocate memory for the DEF_BLOCKS hash table. */
+ def_blocks = htab_create (VEC_length (tree, referenced_vars),
+ def_blocks_hash, def_blocks_eq, def_blocks_free);
+
+ for (i = 0; i < num_referenced_vars; i++)
+ set_current_def (referenced_var (i), NULL_TREE);
+
+ /* 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 = mark_def_sites_initialize_block;
+ walk_data.before_dom_children_walk_stmts = mark_def_sites;
+ walk_data.before_dom_children_after_stmts = NULL;
+ walk_data.after_dom_children_before_stmts = NULL;
+ walk_data.after_dom_children_walk_stmts = NULL;
+ walk_data.after_dom_children_after_stmts = NULL;
+ walk_data.interesting_blocks = NULL;
+
+ /* Notice that this bitmap is indexed using variable UIDs, so it must be
+ large enough to accommodate all the variables referenced in the
+ function, not just the ones we are renaming. */
+ mark_def_sites_global_data.kills = BITMAP_ALLOC (NULL);
+
+ /* Create the set of interesting blocks that will be filled by
+ mark_def_sites. */
+ mark_def_sites_global_data.interesting_blocks = interesting_blocks;
+ 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. */
+ BITMAP_FREE (mark_def_sites_global_data.kills);
+}
+
+
+/* Main entry point into the SSA builder. The renaming process
+ proceeds in four main phases:
+
+ 1- Compute dominance frontier and immediate dominators, needed to
+ insert PHI nodes and rename the function in dominator tree
+ order.
+
+ 2- Find and mark all the blocks that define variables
+ (mark_def_site_blocks).
+
+ 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
+
+ 4- Rename all the blocks (rewrite_blocks) and statements in the program.
+
+ Steps 3 and 5 are done using the dominator tree walker
+ (walk_dominator_tree). */
+
+static void
+rewrite_into_ssa (void)
+{
+ bitmap *dfs;
+ basic_block bb;
+ sbitmap interesting_blocks;
+
+ timevar_push (TV_TREE_SSA_OTHER);
+
+ /* Initialize operand data structures. */
+ init_ssa_operands ();
+
+ /* Initialize the set of interesting blocks. The callback
+ mark_def_sites will add to this set those blocks that the renamer
+ should process. */
+ interesting_blocks = sbitmap_alloc (last_basic_block);
+ sbitmap_zero (interesting_blocks);
+
+ /* Initialize dominance frontier. */
+ dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
+ FOR_EACH_BB (bb)
+ dfs[bb->index] = BITMAP_ALLOC (NULL);
+
+ /* 1- Compute dominance frontiers. */
+ calculate_dominance_info (CDI_DOMINATORS);
+ compute_dominance_frontiers (dfs);
+
+ /* 2- Find and mark definition sites. */
+ mark_def_site_blocks (interesting_blocks);
+
+ /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
+ insert_phi_nodes (dfs);
+
+ /* 4- Rename all the blocks. */
+ rewrite_blocks (ENTRY_BLOCK_PTR, REWRITE_ALL, interesting_blocks);
+
+ /* Free allocated memory. */
+ FOR_EACH_BB (bb)
+ BITMAP_FREE (dfs[bb->index]);
+ free (dfs);
+ sbitmap_free (interesting_blocks);
+
+ timevar_pop (TV_TREE_SSA_OTHER);
+ in_ssa_p = true;
+}
+
+
+struct tree_opt_pass pass_build_ssa =
+{
+ "ssa", /* name */
+ NULL, /* gate */
+ rewrite_into_ssa, /* execute */
+ NULL, /* sub */
+ NULL, /* next */
+ 0, /* static_pass_number */
+ 0, /* tv_id */
+ PROP_cfg | PROP_referenced_vars, /* properties_required */
+ PROP_ssa, /* properties_provided */
+ 0, /* properties_destroyed */
+ 0, /* todo_flags_start */
+ TODO_dump_func | TODO_verify_ssa, /* todo_flags_finish */
+ 0 /* letter */
+};
+
+
+/* Mark the definition of VAR at STMT and BB as interesting for the
+ renamer. BLOCKS is the set of blocks that need updating. */
+
+static void
+mark_def_interesting (tree var, tree stmt, basic_block bb, bitmap blocks,
+ bool insert_phi_p)
+{
+ REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
+ bitmap_set_bit (blocks, bb->index);
+
+ if (insert_phi_p)
+ {
+ bool is_phi_p = TREE_CODE (stmt) == PHI_NODE;
+
+ set_def_block (var, bb, is_phi_p);
+
+ /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
+ site for both itself and all the old names replaced by it. */
+ if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
+ {
+ bitmap_iterator bi;
+ unsigned i;
+ bitmap set = names_replaced_by (var);
+ if (set)
+ EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
+ set_def_block (ssa_name (i), bb, is_phi_p);
+ }
+ }
+}
+
+
+/* Mark the use of VAR at STMT and BB as interesting for the
+ renamer. INSERT_PHI_P is true if we are going to insert new PHI
+ nodes. BLOCKS is the set of blocks that need updating. */