+/* Initialize internal data needed during renaming. */
+
+static void
+init_ssa_renamer (void)
+{
+ tree var;
+ referenced_var_iterator rvi;
+
+ cfun->gimple_df->in_ssa_p = false;
+
+ /* Allocate memory for the DEF_BLOCKS hash table. */
+ gcc_assert (def_blocks == NULL);
+ def_blocks = htab_create (num_referenced_vars, def_blocks_hash,
+ def_blocks_eq, def_blocks_free);
+
+ FOR_EACH_REFERENCED_VAR(var, rvi)
+ set_current_def (var, NULL_TREE);
+}
+
+
+/* Deallocate internal data structures used by the renamer. */
+
+static void
+fini_ssa_renamer (void)
+{
+ if (def_blocks)
+ {
+ htab_delete (def_blocks);
+ def_blocks = NULL;
+ }
+
+ cfun->gimple_df->in_ssa_p = true;
+}
+
+/* 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 4 are done using the dominator tree walker
+ (walk_dominator_tree). */
+
+static unsigned int
+rewrite_into_ssa (void)
+{
+ bitmap_head *dfs;
+ basic_block bb;
+
+ timevar_push (TV_TREE_SSA_OTHER);
+
+ /* Initialize operand data structures. */
+ init_ssa_operands ();
+
+ /* Initialize internal data needed by the renamer. */
+ init_ssa_renamer ();
+
+ /* 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 = XNEWVEC (bitmap_head, last_basic_block);
+ FOR_EACH_BB (bb)
+ bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
+
+ /* 1- Compute dominance frontiers. */
+ calculate_dominance_info (CDI_DOMINATORS);
+ compute_dominance_frontiers (dfs);
+
+ /* 2- Find and mark definition sites. */
+ mark_def_site_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);
+
+ /* Free allocated memory. */
+ FOR_EACH_BB (bb)
+ bitmap_clear (&dfs[bb->index]);
+ free (dfs);
+
+ sbitmap_free (interesting_blocks);
+
+ fini_ssa_renamer ();
+
+ timevar_pop (TV_TREE_SSA_OTHER);
+ return 0;
+}
+
+
+struct gimple_opt_pass pass_build_ssa =
+{
+ {
+ GIMPLE_PASS,
+ "ssa", /* name */
+ NULL, /* gate */
+ rewrite_into_ssa, /* execute */
+ NULL, /* sub */
+ NULL, /* next */
+ 0, /* static_pass_number */
+ TV_NONE, /* tv_id */
+ PROP_cfg | PROP_referenced_vars, /* properties_required */
+ PROP_ssa, /* properties_provided */
+ 0, /* properties_destroyed */
+ 0, /* todo_flags_start */
+ TODO_dump_func
+ | TODO_update_ssa_only_virtuals
+ | TODO_verify_ssa
+ | TODO_remove_unused_locals /* todo_flags_finish */
+ }
+};
+
+
+/* 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, gimple stmt, basic_block bb, bool insert_phi_p)
+{
+ gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
+ set_register_defs (stmt, true);
+
+ if (insert_phi_p)
+ {
+ bool is_phi_p = gimple_code (stmt) == GIMPLE_PHI;
+
+ 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. */
+
+static inline void
+mark_use_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p)
+{
+ basic_block def_bb = gimple_bb (stmt);
+
+ mark_block_for_update (def_bb);
+ mark_block_for_update (bb);
+
+ if (gimple_code (stmt) == GIMPLE_PHI)
+ mark_phi_for_rewrite (def_bb, stmt);
+ else
+ {
+ set_rewrite_uses (stmt, true);
+
+ if (is_gimple_debug (stmt))
+ return;
+ }
+
+ /* If VAR has not been defined in BB, then it is live-on-entry
+ to BB. Note that we cannot just use the block holding VAR's
+ definition because if VAR is one of the names in OLD_SSA_NAMES,
+ it will have several definitions (itself and all the names that
+ replace it). */
+ if (insert_phi_p)
+ {
+ struct def_blocks_d *db_p = get_def_blocks_for (var);
+ if (!bitmap_bit_p (db_p->def_blocks, bb->index))
+ set_livein_block (var, bb);
+ }
+}
+
+
+/* Do a dominator walk starting at BB processing statements that
+ reference symbols in SYMS_TO_RENAME. This is very similar to
+ mark_def_sites, but the scan handles statements whose operands may
+ already be SSA names.
+
+ If INSERT_PHI_P is true, mark those uses as live in the
+ corresponding block. This is later used by the PHI placement
+ algorithm to make PHI pruning decisions.
+
+ FIXME. Most of this would be unnecessary if we could associate a
+ symbol to all the SSA names that reference it. But that
+ sounds like it would be expensive to maintain. Still, it
+ would be interesting to see if it makes better sense to do
+ that. */
+
+static void
+prepare_block_for_update (basic_block bb, bool insert_phi_p)
+{
+ basic_block son;
+ gimple_stmt_iterator si;
+ edge e;
+ edge_iterator ei;
+
+ mark_block_for_update (bb);
+
+ /* Process PHI nodes marking interesting those that define or use
+ the symbols that we are interested in. */
+ for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
+ {
+ gimple phi = gsi_stmt (si);
+ tree lhs_sym, lhs = gimple_phi_result (phi);
+
+ lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
+
+ if (!symbol_marked_for_renaming (lhs_sym))
+ continue;
+
+ mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
+
+ /* Mark the uses in phi nodes as interesting. It would be more correct
+ to process the arguments of the phi nodes of the successor edges of
+ BB at the end of prepare_block_for_update, however, that turns out
+ to be significantly more expensive. Doing it here is conservatively
+ correct -- it may only cause us to believe a value to be live in a
+ block that also contains its definition, and thus insert a few more
+ phi nodes for it. */
+ FOR_EACH_EDGE (e, ei, bb->preds)
+ mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
+ }
+
+ /* Process the statements. */
+ for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
+ {
+ gimple stmt;
+ ssa_op_iter i;
+ use_operand_p use_p;
+ def_operand_p def_p;
+
+ stmt = gsi_stmt (si);
+
+ FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_ALL_USES)
+ {
+ tree use = USE_FROM_PTR (use_p);
+ tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
+ if (symbol_marked_for_renaming (sym))
+ mark_use_interesting (sym, stmt, bb, insert_phi_p);
+ }
+
+ FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_ALL_DEFS)
+ {
+ tree def = DEF_FROM_PTR (def_p);
+ tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
+ if (symbol_marked_for_renaming (sym))
+ mark_def_interesting (sym, stmt, bb, insert_phi_p);
+ }
+ }
+
+ /* Now visit all the blocks dominated by BB. */
+ for (son = first_dom_son (CDI_DOMINATORS, bb);
+ son;
+ son = next_dom_son (CDI_DOMINATORS, son))
+ prepare_block_for_update (son, insert_phi_p);
+}
+
+
+/* Helper for prepare_names_to_update. Mark all the use sites for
+ NAME as interesting. BLOCKS and INSERT_PHI_P are as in
+ prepare_names_to_update. */
+
+static void
+prepare_use_sites_for (tree name, bool insert_phi_p)
+{
+ use_operand_p use_p;
+ imm_use_iterator iter;
+
+ FOR_EACH_IMM_USE_FAST (use_p, iter, name)
+ {
+ gimple stmt = USE_STMT (use_p);
+ basic_block bb = gimple_bb (stmt);
+
+ if (gimple_code (stmt) == GIMPLE_PHI)
+ {
+ int ix = PHI_ARG_INDEX_FROM_USE (use_p);
+ edge e = gimple_phi_arg_edge (stmt, ix);
+ mark_use_interesting (name, stmt, e->src, insert_phi_p);
+ }
+ else
+ {
+ /* For regular statements, mark this as an interesting use
+ for NAME. */
+ mark_use_interesting (name, stmt, bb, insert_phi_p);
+ }
+ }
+}
+
+
+/* Helper for prepare_names_to_update. Mark the definition site for
+ NAME as interesting. BLOCKS and INSERT_PHI_P are as in
+ prepare_names_to_update. */
+
+static void
+prepare_def_site_for (tree name, bool insert_phi_p)
+{
+ gimple stmt;
+ basic_block bb;
+
+ gcc_assert (names_to_release == NULL
+ || !bitmap_bit_p (names_to_release, SSA_NAME_VERSION (name)));
+
+ stmt = SSA_NAME_DEF_STMT (name);
+ bb = gimple_bb (stmt);
+ if (bb)
+ {
+ gcc_assert (bb->index < last_basic_block);
+ mark_block_for_update (bb);
+ mark_def_interesting (name, stmt, bb, insert_phi_p);
+ }
+}
+
+
+/* Mark definition and use sites of names in NEW_SSA_NAMES and
+ OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
+ PHI nodes for newly created names. */
+
+static void
+prepare_names_to_update (bool insert_phi_p)
+{
+ unsigned i = 0;
+ bitmap_iterator bi;
+ sbitmap_iterator sbi;
+
+ /* If a name N from NEW_SSA_NAMES is also marked to be released,
+ remove it from NEW_SSA_NAMES so that we don't try to visit its
+ defining basic block (which most likely doesn't exist). Notice
+ that we cannot do the same with names in OLD_SSA_NAMES because we
+ want to replace existing instances. */
+ if (names_to_release)
+ EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
+ RESET_BIT (new_ssa_names, i);
+
+ /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
+ names may be considered to be live-in on blocks that contain
+ definitions for their replacements. */
+ EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
+ prepare_def_site_for (ssa_name (i), insert_phi_p);
+
+ /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
+ OLD_SSA_NAMES, but we have to ignore its definition site. */
+ EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
+ {
+ if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
+ prepare_def_site_for (ssa_name (i), insert_phi_p);
+ prepare_use_sites_for (ssa_name (i), insert_phi_p);
+ }
+}
+
+
+/* Dump all the names replaced by NAME to FILE. */
+
+void
+dump_names_replaced_by (FILE *file, tree name)
+{
+ unsigned i;
+ bitmap old_set;
+ bitmap_iterator bi;
+
+ print_generic_expr (file, name, 0);
+ fprintf (file, " -> { ");
+
+ old_set = names_replaced_by (name);
+ EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
+ {
+ print_generic_expr (file, ssa_name (i), 0);
+ fprintf (file, " ");
+ }
+
+ fprintf (file, "}\n");
+}
+
+
+/* Dump all the names replaced by NAME to stderr. */
+
+DEBUG_FUNCTION void
+debug_names_replaced_by (tree name)
+{
+ dump_names_replaced_by (stderr, name);
+}
+
+
+/* Dump SSA update information to FILE. */
+
+void
+dump_update_ssa (FILE *file)
+{
+ unsigned i = 0;
+ bitmap_iterator bi;
+
+ if (!need_ssa_update_p (cfun))
+ return;
+
+ if (new_ssa_names && sbitmap_first_set_bit (new_ssa_names) >= 0)
+ {
+ sbitmap_iterator sbi;
+
+ fprintf (file, "\nSSA replacement table\n");
+ fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
+ "O_1, ..., O_j\n\n");
+
+ EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
+ dump_names_replaced_by (file, ssa_name (i));
+
+ fprintf (file, "\n");
+ fprintf (file, "Number of virtual NEW -> OLD mappings: %7u\n",
+ update_ssa_stats.num_virtual_mappings);
+ fprintf (file, "Number of real NEW -> OLD mappings: %7u\n",
+ update_ssa_stats.num_total_mappings
+ - update_ssa_stats.num_virtual_mappings);
+ fprintf (file, "Number of total NEW -> OLD mappings: %7u\n",
+ update_ssa_stats.num_total_mappings);
+
+ fprintf (file, "\nNumber of virtual symbols: %u\n",
+ update_ssa_stats.num_virtual_symbols);
+ }
+
+ if (!bitmap_empty_p (SYMS_TO_RENAME (cfun)))
+ {
+ fprintf (file, "\n\nSymbols to be put in SSA form\n\n");
+ dump_decl_set (file, SYMS_TO_RENAME (cfun));
+ fprintf (file, "\n");
+ }
+
+ if (names_to_release && !bitmap_empty_p (names_to_release))
+ {
+ fprintf (file, "\n\nSSA names to release after updating the SSA web\n\n");
+ EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
+ {
+ print_generic_expr (file, ssa_name (i), 0);
+ fprintf (file, " ");
+ }
+ }
+
+ fprintf (file, "\n\n");
+}
+
+
+/* Dump SSA update information to stderr. */
+
+DEBUG_FUNCTION void
+debug_update_ssa (void)
+{
+ dump_update_ssa (stderr);
+}
+
+
+/* Initialize data structures used for incremental SSA updates. */
+
+static void
+init_update_ssa (struct function *fn)
+{
+ /* Reserve more space than the current number of names. The calls to
+ add_new_name_mapping are typically done after creating new SSA
+ names, so we'll need to reallocate these arrays. */
+ old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
+ sbitmap_zero (old_ssa_names);
+
+ new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
+ sbitmap_zero (new_ssa_names);
+
+ repl_tbl = htab_create (20, repl_map_hash, repl_map_eq, repl_map_free);
+ names_to_release = NULL;
+ memset (&update_ssa_stats, 0, sizeof (update_ssa_stats));
+ update_ssa_stats.virtual_symbols = BITMAP_ALLOC (NULL);
+ update_ssa_initialized_fn = fn;
+}
+
+
+/* Deallocate data structures used for incremental SSA updates. */
+
+void
+delete_update_ssa (void)
+{
+ unsigned i;
+ bitmap_iterator bi;
+
+ sbitmap_free (old_ssa_names);
+ old_ssa_names = NULL;
+
+ sbitmap_free (new_ssa_names);
+ new_ssa_names = NULL;
+
+ htab_delete (repl_tbl);
+ repl_tbl = NULL;