OSDN Git Service

* tree-into-ssa (insert_phi_nodes_for): 'var' must be a DECL at
[pf3gnuchains/gcc-fork.git] / gcc / tree-into-ssa.c
index 09a73b2..c9e99ef 100644 (file)
@@ -1,12 +1,13 @@
 /* Rewrite a program in Normal form into SSA.
-   Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008
+   Free Software Foundation, Inc.
    Contributed by Diego Novillo <dnovillo@redhat.com>
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
 GCC is distributed in the hope that it will be useful,
@@ -15,9 +16,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to
-the Free Software Foundation, 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
 #include "system.h"
@@ -47,6 +47,7 @@ Boston, MA 02110-1301, USA.  */
 #include "domwalk.h"
 #include "ggc.h"
 #include "params.h"
+#include "vecprim.h"
 
 /* This file builds the SSA form for a function as described in:
    R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
@@ -54,9 +55,6 @@ Boston, MA 02110-1301, USA.  */
    Graph. ACM Transactions on Programming Languages and Systems,
    13(4):451-490, October 1991.  */
 
-/* True if the code is in ssa form.  */
-bool in_ssa_p;
-
 /* Structure to map a variable VAR to the set of blocks that contain
    definitions for VAR.  */
 struct def_blocks_d
@@ -90,25 +88,20 @@ static htab_t def_blocks;
    state after completing rewriting of a block and its dominator
    children.  Its elements have the following properties:
 
-   - An SSA_NAME indicates that the current definition of the
-     underlying variable should be set to the given SSA_NAME.
+   - An SSA_NAME (N) indicates that the current definition of the
+     underlying variable should be set to the given SSA_NAME.  If the
+     symbol associated with the SSA_NAME is not a GIMPLE register, the
+     next slot in the stack must be a _DECL node (SYM).  In this case,
+     the name N in the previous slot is the current reaching
+     definition for SYM.
 
    - A _DECL node indicates that the underlying variable has no
      current definition.
 
-   - A NULL node is used to mark the last node associated with the
-     current block.
-
-   - A NULL node at the top entry is used to mark the last node
+   - A NULL node at the top entry is used to mark the last slot
      associated with the current block.  */
 static VEC(tree,heap) *block_defs_stack;
 
-/* Basic block vectors used in this file ought to be allocated in the
-   heap.  We use pointer vector, because ints can be easily passed by
-   value.  */
-DEF_VEC_I(int);
-DEF_VEC_ALLOC_I(int,heap);
-
 /* Set of existing SSA names being replaced by update_ssa.  */
 static sbitmap old_ssa_names;
 
@@ -121,11 +114,30 @@ static sbitmap new_ssa_names;
    time.  */
 static bitmap syms_to_rename;
 
+/* Subset of SYMS_TO_RENAME.  Contains all the GIMPLE register symbols
+   that have been marked for renaming.  */
+static bitmap regs_to_rename;
+
+/* Subset of SYMS_TO_RENAME.  Contains all the memory symbols
+   that have been marked for renaming.  */
+static bitmap mem_syms_to_rename;
+
 /* Set of SSA names that have been marked to be released after they
    were registered in the replacement table.  They will be finally
    released after we finish updating the SSA web.  */
 static bitmap names_to_release;
 
+/* For each block, the PHI nodes that need to be rewritten are stored into
+   these vectors.  */
+typedef VEC(tree, heap) *tree_vec;
+DEF_VEC_P (tree_vec);
+DEF_VEC_ALLOC_P (tree_vec, heap);
+
+static VEC(tree_vec, heap) *phis_to_rewrite;
+
+/* The bitmap of non-NULL elements of PHIS_TO_REWRITE.  */
+static bitmap blocks_with_phis_to_rewrite;
+
 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES.  These sets need
    to grow as the callers to register_new_name_mapping will typically
    create new names on the fly.  FIXME.  Currently set to 1/3 to avoid
@@ -186,15 +198,30 @@ struct mark_def_sites_global_data
 /* Information stored for SSA names.  */
 struct ssa_name_info
 {
+  /* The current reaching definition replacing this SSA name.  */
+  tree current_def;
+
   /* This field indicates whether or not the variable may need PHI nodes.
      See the enum's definition for more detailed information about the
      states.  */
   ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
 
-  /* The actual definition of the ssa name.  */
-  tree current_def;
+  /* Age of this record (so that info_for_ssa_name table can be cleared
+     quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
+     are assumed to be null.  */
+  unsigned age;
 };
 
+/* The information associated with names.  */
+typedef struct ssa_name_info *ssa_name_info_p;
+DEF_VEC_P (ssa_name_info_p);
+DEF_VEC_ALLOC_P (ssa_name_info_p, heap);
+
+static VEC(ssa_name_info_p, heap) *info_for_ssa_name;
+static unsigned current_info_for_ssa_name_age;
+
+/* The set of blocks affected by update_ssa.  */
+static bitmap blocks_to_update;
 
 /* The main entry point to the SSA renamer (rewrite_blocks) may be
    called several times to do different, but related, tasks.
@@ -224,7 +251,7 @@ enum rewrite_mode {
    processed from those that only need to have their defs processed.
    Statements that define new SSA names only need to have their defs
    registered, but they don't need to have their uses renamed.  */
-#define REGISTER_DEFS_IN_THIS_STMT(T)  (T)->common.unsigned_flag
+#define REGISTER_DEFS_IN_THIS_STMT(T)  (T)->base.unsigned_flag
 
 
 /* Prototypes for debugging functions.  */
@@ -233,24 +260,61 @@ extern void debug_tree_ssa (void);
 extern void debug_def_blocks (void);
 extern void dump_tree_ssa_stats (FILE *);
 extern void debug_tree_ssa_stats (void);
-void dump_update_ssa (FILE *);
-void debug_update_ssa (void);
-void dump_names_replaced_by (FILE *, tree);
-void debug_names_replaced_by (tree);
+extern void dump_update_ssa (FILE *);
+extern void debug_update_ssa (void);
+extern void dump_names_replaced_by (FILE *, tree);
+extern void debug_names_replaced_by (tree);
+extern void dump_def_blocks (FILE *);
+extern void debug_def_blocks (void);
+extern void dump_defs_stack (FILE *, int);
+extern void debug_defs_stack (int);
+extern void dump_currdefs (FILE *);
+extern void debug_currdefs (void);
 
 /* Get the information associated with NAME.  */
 
-static inline struct ssa_name_info *
+static inline ssa_name_info_p
 get_ssa_name_ann (tree name)
 {
-  if (!SSA_NAME_AUX (name))
-    SSA_NAME_AUX (name) = xcalloc (1, sizeof (struct ssa_name_info));
+  unsigned ver = SSA_NAME_VERSION (name);
+  unsigned len = VEC_length (ssa_name_info_p, info_for_ssa_name);
+  struct ssa_name_info *info;
+
+  if (ver >= len)
+    {
+      unsigned new_len = num_ssa_names;
+
+      VEC_reserve (ssa_name_info_p, heap, info_for_ssa_name, new_len);
+      while (len++ < new_len)
+       {
+         struct ssa_name_info *info = XCNEW (struct ssa_name_info);
+         info->age = current_info_for_ssa_name_age;
+         VEC_quick_push (ssa_name_info_p, info_for_ssa_name, info);
+       }
+    }
+
+  info = VEC_index (ssa_name_info_p, info_for_ssa_name, ver);
+  if (info->age < current_info_for_ssa_name_age)
+    {
+      info->need_phi_state = 0;
+      info->current_def = NULL_TREE;
+      info->age = current_info_for_ssa_name_age;
+    }
+
+  return info;
+}
+
 
-  return (struct ssa_name_info *) SSA_NAME_AUX (name);
+/* Clears info for SSA names.  */
+
+static void
+clear_ssa_name_info (void)
+{
+  current_info_for_ssa_name_age++;
 }
 
 
-/* Gets phi_state field for VAR.  */
+/* Get phi_state field for VAR.  */
 
 static inline enum need_phi_state
 get_phi_state (tree var)
@@ -298,7 +362,7 @@ set_current_def (tree var, tree def)
 }
 
 
-/* Compute global livein information given the set of blockx where
+/* Compute global livein information given the set of blocks where
    an object is locally live at the start of the block (LIVEIN)
    and the set of blocks where the object is defined (DEF_BLOCKS).
 
@@ -317,9 +381,7 @@ compute_global_livein (bitmap livein, bitmap def_blocks)
     = (basic_block *) xmalloc (sizeof (basic_block) * (last_basic_block + 1));
 
   EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
-    {
-      *tos++ = BASIC_BLOCK (i);
-    }
+    *tos++ = BASIC_BLOCK (i);
 
   /* Iterate until the worklist is empty.  */
   while (tos != worklist)
@@ -351,6 +413,45 @@ compute_global_livein (bitmap livein, bitmap def_blocks)
 }
 
 
+/* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
+   all statements in basic block BB.  */
+
+static void
+initialize_flags_in_bb (basic_block bb)
+{
+  tree phi, stmt;
+  block_stmt_iterator bsi;
+
+  for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
+    {
+      REWRITE_THIS_STMT (phi) = 0;
+      REGISTER_DEFS_IN_THIS_STMT (phi) = 0;
+    }
+
+  for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
+    {
+      stmt = bsi_stmt (bsi);
+      /* We are going to use the operand cache API, such as
+        SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST.  The operand
+        cache for each statement should be up-to-date.  */
+      gcc_assert (!stmt_modified_p (stmt));
+      REWRITE_THIS_STMT (stmt) = 0;
+      REGISTER_DEFS_IN_THIS_STMT (stmt) = 0;
+    }
+}
+
+/* Mark block BB as interesting for update_ssa.  */
+
+static void
+mark_block_for_update (basic_block bb)
+{
+  gcc_assert (blocks_to_update != NULL);
+  if (bitmap_bit_p (blocks_to_update, bb->index))
+    return;
+  bitmap_set_bit (blocks_to_update, bb->index);
+  initialize_flags_in_bb (bb);
+}
+
 /* Return the set of blocks where variable VAR is defined and the blocks
    where VAR is live on entry (livein).  If no entry is found in
    DEF_BLOCKS, a new one is created and returned.  */
@@ -453,7 +554,6 @@ set_livein_block (tree var, basic_block bb)
 static inline bool
 symbol_marked_for_renaming (tree sym)
 {
-  gcc_assert (DECL_P (sym));
   return bitmap_bit_p (syms_to_rename, DECL_UID (sym));
 }
 
@@ -557,24 +657,18 @@ add_new_name_mapping (tree new, tree old)
   /* OLD and NEW must be different SSA names for the same symbol.  */
   gcc_assert (new != old && SSA_NAME_VAR (new) == SSA_NAME_VAR (old));
 
-  /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
-     caller may have created new names since the set was created.  */
-  if (new_ssa_names->n_bits <= num_ssa_names - 1)
-    {
-      unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
-      new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
-      old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
-    }
-
   /* If this mapping is for virtual names, we will need to update
-     virtual operands.  */
+     virtual operands.  If this is a mapping for .MEM, then we gather
+     the symbols associated with each name.  */
   if (!is_gimple_reg (new))
     {
       tree sym;
-      size_t uid;
 
       need_to_update_vops_p = true;
 
+      update_ssa_stats.num_virtual_mappings++;
+      update_ssa_stats.num_virtual_symbols++;
+
       /* Keep counts of virtual mappings and symbols to use in the
         virtual mapping heuristic.  If we have large numbers of
         virtual mappings for a relatively low number of symbols, it
@@ -582,13 +676,16 @@ add_new_name_mapping (tree new, tree old)
         Otherwise, the insertion of PHI nodes for each of the old
         names in these mappings will be very slow.  */
       sym = SSA_NAME_VAR (new);
-      uid = DECL_UID (sym);
-      update_ssa_stats.num_virtual_mappings++;
-      if (!bitmap_bit_p (update_ssa_stats.virtual_symbols, uid))
-       {
-         bitmap_set_bit (update_ssa_stats.virtual_symbols, uid);
-         update_ssa_stats.num_virtual_symbols++;
-       }
+      bitmap_set_bit (update_ssa_stats.virtual_symbols, DECL_UID (sym));
+    }
+
+  /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
+     caller may have created new names since the set was created.  */
+  if (new_ssa_names->n_bits <= num_ssa_names - 1)
+    {
+      unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
+      new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
+      old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
     }
 
   /* Update the REPL_TBL table.  */
@@ -626,28 +723,28 @@ add_new_name_mapping (tree new, tree old)
    we create.  */
 
 static void
-mark_def_sites (struct dom_walk_data *walk_data,
-               basic_block bb,
+mark_def_sites (struct dom_walk_data *walk_data, basic_block bb,
                block_stmt_iterator bsi)
 {
-  struct mark_def_sites_global_data *gd =
-     (struct mark_def_sites_global_data *) walk_data->global_data;
-  bitmap kills = gd->kills;
+  struct mark_def_sites_global_data *gd;
+  bitmap kills;
   tree stmt, def;
   use_operand_p use_p;
-  def_operand_p def_p;
   ssa_op_iter iter;
 
   stmt = bsi_stmt (bsi);
   update_stmt_if_modified (stmt);
 
+  gd = (struct mark_def_sites_global_data *) walk_data->global_data;
+  kills = gd->kills;
+
+  gcc_assert (blocks_to_update == NULL);
   REGISTER_DEFS_IN_THIS_STMT (stmt) = 0;
   REWRITE_THIS_STMT (stmt) = 0;
 
   /* 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.  */
-  FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
-                           SSA_OP_USE | SSA_OP_VUSE | SSA_OP_VMUSTKILL)
+  FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
     {
       tree sym = USE_FROM_PTR (use_p);
       gcc_assert (DECL_P (sym));
@@ -656,23 +753,9 @@ mark_def_sites (struct dom_walk_data *walk_data,
       REWRITE_THIS_STMT (stmt) = 1;
     }
   
-  /* Note that virtual definitions are irrelevant for computing KILLS
-     because a V_MAY_DEF does not constitute a killing definition of the
-     variable.  However, the operand of a virtual definitions is a use
-     of the variable, so it may cause the variable to be considered
-     live-on-entry.  */
-  FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
-    {
-      tree sym = USE_FROM_PTR (use_p);
-      gcc_assert (DECL_P (sym));
-      set_livein_block (sym, bb);
-      set_def_block (sym, bb, false);
-      REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
-      REWRITE_THIS_STMT (stmt) = 1;
-    }
-
-  /* Now process the defs and must-defs made by this statement.  */
-  FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF | SSA_OP_VMUSTDEF)
+  /* Now process the defs.  Mark BB as the definition block and add
+     each def to the set of killed symbols.  */
+  FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
     {
       gcc_assert (DECL_P (def));
       set_def_block (def, bb, false);
@@ -686,67 +769,221 @@ mark_def_sites (struct dom_walk_data *walk_data,
     SET_BIT (gd->interesting_blocks, bb->index);
 }
 
+/* Structure used by prune_unused_phi_nodes to record bounds of the intervals
+   in the dfs numbering of the dominance tree.  */
 
-/* Given a set of blocks with variable definitions (DEF_BLOCKS),
-   return a bitmap with all the blocks in the iterated dominance
-   frontier of the blocks in DEF_BLOCKS.  DFS contains dominance
-   frontier information as returned by compute_dominance_frontiers.
-   
-   The resulting set of blocks are the potential sites where PHI nodes
-   are needed.  The caller is responsible from freeing the memory
-   allocated for the return value.  */
+struct dom_dfsnum
+{
+  /* Basic block whose index this entry corresponds to.  */
+  unsigned bb_index;
+
+  /* The dfs number of this node.  */
+  unsigned dfs_num;
+};
+
+/* Compares two entries of type struct dom_dfsnum by dfs_num field.  Callback
+   for qsort.  */
+
+static int
+cmp_dfsnum (const void *a, const void *b)
+{
+  const struct dom_dfsnum *const da = (const struct dom_dfsnum *) a;
+  const struct dom_dfsnum *const db = (const struct dom_dfsnum *) b;
+
+  return (int) da->dfs_num - (int) db->dfs_num;
+}
+
+/* Among the intervals starting at the N points specified in DEFS, find
+   the one that contains S, and return its bb_index.  */
+
+static unsigned
+find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
+{
+  unsigned f = 0, t = n, m;
+
+  while (t > f + 1)
+    {
+      m = (f + t) / 2;
+      if (defs[m].dfs_num <= s)
+       f = m;
+      else
+       t = m;
+    }
+
+  return defs[f].bb_index;
+}
+
+/* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
+   KILLS is a bitmap of blocks where the value is defined before any use.  */
 
-static bitmap
-find_idf (bitmap def_blocks, bitmap *dfs)
+static void
+prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
 {
+  VEC(int, heap) *worklist;
   bitmap_iterator bi;
-  unsigned bb_index;
-  VEC(int,heap) *work_stack;
-  bitmap phi_insertion_points;
-
-  work_stack = VEC_alloc (int, heap, n_basic_blocks);
-  phi_insertion_points = BITMAP_ALLOC (NULL);
-
-  /* Seed the work list with all the blocks in DEF_BLOCKS.  */
-  EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
-    /* We use VEC_quick_push here for speed.  This is safe because we
-       know that the number of definition blocks is no greater than
-       the number of basic blocks, which is the initial capacity of
-       WORK_STACK.  */
-    VEC_quick_push (int, work_stack, bb_index);
-
-  /* Pop a block off the worklist, add every block that appears in
-     the original block's DF that we have not already processed to
-     the worklist.  Iterate until the worklist is empty.   Blocks
-     which are added to the worklist are potential sites for
-     PHI nodes.  */
-  while (VEC_length (int, work_stack) > 0)
-    {
-      bb_index = VEC_pop (int, work_stack);
-
-      /* Since the registration of NEW -> OLD name mappings is done
-        separately from the call to update_ssa, when updating the SSA
-        form, the basic blocks where new and/or old names are defined
-        may have disappeared by CFG cleanup calls.  In this case,
-        we may pull a non-existing block from the work stack.  */
-      gcc_assert (bb_index < (unsigned) last_basic_block);
-
-      EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index], phi_insertion_points,
-                                     0, bb_index, bi)
+  unsigned i, b, p, u, top;
+  bitmap live_phis;
+  basic_block def_bb, use_bb;
+  edge e;
+  edge_iterator ei;
+  bitmap to_remove;
+  struct dom_dfsnum *defs;
+  unsigned n_defs, adef;
+
+  if (bitmap_empty_p (uses))
+    {
+      bitmap_clear (phis);
+      return;
+    }
+
+  /* The phi must dominate a use, or an argument of a live phi.  Also, we
+     do not create any phi nodes in def blocks, unless they are also livein.  */
+  to_remove = BITMAP_ALLOC (NULL);
+  bitmap_and_compl (to_remove, kills, uses);
+  bitmap_and_compl_into (phis, to_remove);
+  if (bitmap_empty_p (phis))
+    {
+      BITMAP_FREE (to_remove);
+      return;
+    }
+
+  /* We want to remove the unnecessary phi nodes, but we do not want to compute
+     liveness information, as that may be linear in the size of CFG, and if
+     there are lot of different variables to rewrite, this may lead to quadratic
+     behavior.
+
+     Instead, we basically emulate standard dce.  We put all uses to worklist,
+     then for each of them find the nearest def that dominates them.  If this
+     def is a phi node, we mark it live, and if it was not live before, we
+     add the predecessors of its basic block to the worklist.
+   
+     To quickly locate the nearest def that dominates use, we use dfs numbering
+     of the dominance tree (that is already available in order to speed up
+     queries).  For each def, we have the interval given by the dfs number on
+     entry to and on exit from the corresponding subtree in the dominance tree.
+     The nearest dominator for a given use is the smallest of these intervals
+     that contains entry and exit dfs numbers for the basic block with the use.
+     If we store the bounds for all the uses to an array and sort it, we can
+     locate the nearest dominating def in logarithmic time by binary search.*/
+  bitmap_ior (to_remove, kills, phis);
+  n_defs = bitmap_count_bits (to_remove);
+  defs = XNEWVEC (struct dom_dfsnum, 2 * n_defs + 1);
+  defs[0].bb_index = 1;
+  defs[0].dfs_num = 0;
+  adef = 1;
+  EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
+    {
+      def_bb = BASIC_BLOCK (i);
+      defs[adef].bb_index = i;
+      defs[adef].dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
+      defs[adef + 1].bb_index = i;
+      defs[adef + 1].dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
+      adef += 2;
+    }
+  BITMAP_FREE (to_remove);
+  gcc_assert (adef == 2 * n_defs + 1);
+  qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
+  gcc_assert (defs[0].bb_index == 1);
+
+  /* Now each DEFS entry contains the number of the basic block to that the
+     dfs number corresponds.  Change them to the number of basic block that
+     corresponds to the interval following the dfs number.  Also, for the
+     dfs_out numbers, increase the dfs number by one (so that it corresponds
+     to the start of the following interval, not to the end of the current
+     one).  We use WORKLIST as a stack.  */
+  worklist = VEC_alloc (int, heap, n_defs + 1);
+  VEC_quick_push (int, worklist, 1);
+  top = 1;
+  n_defs = 1;
+  for (i = 1; i < adef; i++)
+    {
+      b = defs[i].bb_index;
+      if (b == top)
+       {
+         /* This is a closing element.  Interval corresponding to the top
+            of the stack after removing it follows.  */
+         VEC_pop (int, worklist);
+         top = VEC_index (int, worklist, VEC_length (int, worklist) - 1);
+         defs[n_defs].bb_index = top;
+         defs[n_defs].dfs_num = defs[i].dfs_num + 1;
+       }
+      else
        {
-         /* Use a safe push because if there is a definition of VAR
-            in every basic block, then WORK_STACK may eventually have
-            more than N_BASIC_BLOCK entries.  */
-         VEC_safe_push (int, heap, work_stack, bb_index);
-         bitmap_set_bit (phi_insertion_points, bb_index);
+         /* Opening element.  Nothing to do, just push it to the stack and move
+            it to the correct position.  */
+         defs[n_defs].bb_index = defs[i].bb_index;
+         defs[n_defs].dfs_num = defs[i].dfs_num;
+         VEC_quick_push (int, worklist, b);
+         top = b;
        }
+
+      /* If this interval starts at the same point as the previous one, cancel
+        the previous one.  */
+      if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
+       defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
+      else
+       n_defs++;
     }
+  VEC_pop (int, worklist);
+  gcc_assert (VEC_empty (int, worklist));
 
-  VEC_free (int, heap, work_stack);
+  /* Now process the uses.  */
+  live_phis = BITMAP_ALLOC (NULL);
+  EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
+    {
+      VEC_safe_push (int, heap, worklist, i);
+    }
 
-  return phi_insertion_points;
-}
+  while (!VEC_empty (int, worklist))
+    {
+      b = VEC_pop (int, worklist);
+      if (b == ENTRY_BLOCK)
+       continue;
 
+      /* If there is a phi node in USE_BB, it is made live.  Otherwise,
+        find the def that dominates the immediate dominator of USE_BB
+        (the kill in USE_BB does not dominate the use).  */
+      if (bitmap_bit_p (phis, b))
+       p = b;
+      else
+       {
+         use_bb = get_immediate_dominator (CDI_DOMINATORS, BASIC_BLOCK (b));
+         p = find_dfsnum_interval (defs, n_defs,
+                                   bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
+         if (!bitmap_bit_p (phis, p))
+           continue;
+       }
+
+      /* If the phi node is already live, there is nothing to do.  */
+      if (bitmap_bit_p (live_phis, p))
+       continue;
+
+      /* Mark the phi as live, and add the new uses to the worklist.  */
+      bitmap_set_bit (live_phis, p);
+      def_bb = BASIC_BLOCK (p);
+      FOR_EACH_EDGE (e, ei, def_bb->preds)
+       {
+         u = e->src->index;
+         if (bitmap_bit_p (uses, u))
+           continue;
+
+         /* In case there is a kill directly in the use block, do not record
+            the use (this is also necessary for correctness, as we assume that
+            uses dominated by a def directly in their block have been filtered
+            out before).  */
+         if (bitmap_bit_p (kills, u))
+           continue;
+
+         bitmap_set_bit (uses, u);
+         VEC_safe_push (int, heap, worklist, u);
+       }
+    }
+
+  VEC_free (int, heap, worklist);
+  bitmap_copy (phis, live_phis);
+  BITMAP_FREE (live_phis);
+  free (defs);
+}
 
 /* Return the set of blocks where variable VAR is defined and the blocks
    where VAR is live on entry (livein).  Return NULL, if no entry is
@@ -766,7 +1003,7 @@ find_def_blocks_for (tree var)
 static inline tree
 get_default_def_for (tree sym)
 {
-  tree ddef = default_def (sym);
+  tree ddef = gimple_default_def (cfun, sym);
 
   if (ddef == NULL_TREE)
     {
@@ -778,12 +1015,41 @@ get_default_def_for (tree sym)
 }
 
 
+/* Marks phi node PHI in basic block BB for rewrite.  */
+
+static void
+mark_phi_for_rewrite (basic_block bb, tree phi)
+{
+  tree_vec phis;
+  unsigned i, idx = bb->index;
+
+  if (REWRITE_THIS_STMT (phi))
+    return;
+
+  REWRITE_THIS_STMT (phi) = 1;
+
+  if (!blocks_with_phis_to_rewrite)
+    return;
+
+  bitmap_set_bit (blocks_with_phis_to_rewrite, idx);
+  VEC_reserve (tree_vec, heap, phis_to_rewrite, last_basic_block + 1);
+  for (i = VEC_length (tree_vec, phis_to_rewrite); i <= idx; i++)
+    VEC_quick_push (tree_vec, phis_to_rewrite, NULL);
+
+  phis = VEC_index (tree_vec, phis_to_rewrite, idx);
+  if (!phis)
+    phis = VEC_alloc (tree, heap, 10);
+
+  VEC_safe_push (tree, heap, phis, phi);
+  VEC_replace (tree_vec, phis_to_rewrite, idx, phis);
+}
+
+
 /* Insert PHI nodes for variable VAR using the iterated dominance
    frontier given in PHI_INSERTION_POINTS.  If UPDATE_P is true, this
-   function assumes that the caller is incrementally updating the SSA
-   form, in which case (1) VAR is assumed to be an SSA name, (2) a new
-   SSA name is created for VAR's symbol, and, (3) all the arguments
-   for the newly created PHI node are set to VAR.
+   function assumes that the caller is incrementally updating the
+   existing SSA form, in which case VAR may be an SSA name instead of
+   a symbol.
 
    PHI_INSERTION_POINTS is updated to reflect nodes that already had a
    PHI node for VAR.  On exit, only the nodes that received a PHI node
@@ -805,17 +1071,20 @@ insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
   /* Remove the blocks where we already have PHI nodes for VAR.  */
   bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
 
-  /* 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);
+  /* Remove obviously useless phi nodes.  */
+  prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
+                         def_map->livein_blocks);
 
   /* And insert the PHI nodes.  */
-  EXECUTE_IF_AND_IN_BITMAP (phi_insertion_points, def_map->livein_blocks,
-                           0, bb_index, bi)
+  EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
     {
       bb = BASIC_BLOCK (bb_index);
+      if (update_p)
+       mark_block_for_update (bb);
+
+      phi = NULL_TREE;
 
-      if (update_p && TREE_CODE (var) == SSA_NAME)
+      if (TREE_CODE (var) == SSA_NAME)
        {
          /* If we are rewriting SSA names, create the LHS of the PHI
             node by duplicating VAR.  This is useful in the case of
@@ -824,7 +1093,9 @@ insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
          edge_iterator ei;
          tree new_lhs;
 
+         gcc_assert (update_p);
          phi = create_phi_node (var, bb);
+
          new_lhs = duplicate_ssa_name (var, phi);
          SET_PHI_RESULT (phi, new_lhs);
          add_new_name_mapping (new_lhs, var);
@@ -840,23 +1111,20 @@ insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
        }
       else
        {
-         tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
-         phi = create_phi_node (sym, bb);
+         gcc_assert (DECL_P (var));
+         phi = create_phi_node (var, bb);
        }
 
       /* Mark this PHI node as interesting for update_ssa.  */
       REGISTER_DEFS_IN_THIS_STMT (phi) = 1;
-      REWRITE_THIS_STMT (phi) = 1;
+      mark_phi_for_rewrite (bb, phi);
     }
 }
 
 
 /* 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).  */
+   the flowgraph.  */
 
 static void
 insert_phi_nodes (bitmap *dfs)
@@ -877,7 +1145,7 @@ insert_phi_nodes (bitmap *dfs)
 
       if (get_phi_state (var) != NEED_PHI_STATE_NO)
        {
-         idf = find_idf (def_map->def_blocks, dfs);
+         idf = compute_idf (def_map->def_blocks, dfs);
          insert_phi_nodes_for (var, idf, false);
          BITMAP_FREE (idf);
        }
@@ -887,14 +1155,12 @@ insert_phi_nodes (bitmap *dfs)
 }
 
 
-/* Register DEF (an SSA_NAME) to be a new definition for its underlying
-   variable (SSA_NAME_VAR (DEF)) and push VAR's current reaching definition
-   into the stack pointed to by BLOCK_DEFS_P.  */
+/* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
+   register DEF (an SSA_NAME) to be a new definition for SYM.  */
 
-void
-register_new_def (tree def, VEC(tree,heap) **block_defs_p)
+static void
+register_new_def (tree def, tree sym)
 {
-  tree var = SSA_NAME_VAR (def);
   tree currdef;
    
   /* If this variable is set in a single basic block and all uses are
@@ -905,23 +1171,31 @@ register_new_def (tree def, VEC(tree,heap) **block_defs_p)
      This is the same test to prune the set of variables which may
      need PHI nodes.  So we just use that information since it's already
      computed and available for us to use.  */
-  if (get_phi_state (var) == NEED_PHI_STATE_NO)
+  if (get_phi_state (sym) == NEED_PHI_STATE_NO)
     {
-      set_current_def (var, def);
+      set_current_def (sym, def);
       return;
     }
 
-  currdef = get_current_def (var);
+  currdef = get_current_def (sym);
+
+  /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
+     SSA_NAME_VAR is not necessarily SYM.  In this case, also push SYM
+     in the stack so that we know which symbol is being defined by
+     this SSA name when we unwind the stack.  */
+  if (currdef && !is_gimple_reg (sym))
+    VEC_safe_push (tree, heap, block_defs_stack, sym);
 
-  /* 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.  If there is no current
-     reaching definition, then just record the underlying _DECL node.  */
-  VEC_safe_push (tree, heap, *block_defs_p, currdef ? currdef : var);
+  /* Push the current reaching definition into BLOCK_DEFS_STACK.  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.  If there is no current reaching definition, then just
+     record the underlying _DECL node.  */
+  VEC_safe_push (tree, heap, block_defs_stack, currdef ? currdef : sym);
 
-  /* Set the current reaching definition for VAR to be DEF.  */
-  set_current_def (var, def);
+  /* Set the current reaching definition for SYM to be DEF.  */
+  set_current_def (sym, def);
 }
 
 
@@ -971,37 +1245,35 @@ rewrite_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
     {
       tree result = PHI_RESULT (phi);
-      register_new_def (result, &block_defs_stack);
+      gcc_assert (is_gimple_reg (result));
+      register_new_def (result, SSA_NAME_VAR (result));
     }
 }
 
 
 /* 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.  */
+   create a new SSA name to act as the zeroth definition for VAR.  */
 
 static tree
 get_reaching_def (tree var)
 {
-  tree currdef_var, avar;
+  tree currdef;
   
   /* Lookup the current reaching definition for VAR.  */
-  currdef_var = get_current_def (var);
+  currdef = 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)
+  if (currdef == NULL_TREE)
     {
-      avar = DECL_P (var) ? var : SSA_NAME_VAR (var);
-      currdef_var = get_default_def_for (avar);
-      set_current_def (var, currdef_var);
+      tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
+      currdef = get_default_def_for (sym);
+      set_current_def (var, currdef);
     }
 
   /* Return the current reaching definition for VAR, or the default
      definition, if we had to create one.  */
-  return currdef_var;
+  return currdef;
 }
 
 
@@ -1011,8 +1283,7 @@ get_reaching_def (tree var)
 
 static void
 rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
-             basic_block bb ATTRIBUTE_UNUSED,
-             block_stmt_iterator si)
+             basic_block bb ATTRIBUTE_UNUSED, block_stmt_iterator si)
 {
   tree stmt;
   use_operand_p use_p;
@@ -1023,6 +1294,7 @@ rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
 
   /* If mark_def_sites decided that we don't need to rewrite this
      statement, ignore it.  */
+  gcc_assert (blocks_to_update == NULL);
   if (!REWRITE_THIS_STMT (stmt) && !REGISTER_DEFS_IN_THIS_STMT (stmt))
     return;
 
@@ -1033,24 +1305,23 @@ rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
       fprintf (dump_file, "\n");
     }
 
-  /* Step 1.  Rewrite USES and VUSES in the statement.  */
+  /* Step 1.  Rewrite USES 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)
+    FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
       {
        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.  */
+  /* Step 2.  Register the statement's DEF operands.  */
   if (REGISTER_DEFS_IN_THIS_STMT (stmt))
-    FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
+    FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
       {
        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);
+       register_new_def (DEF_FROM_PTR (def_p), var);
       }
 }
 
@@ -1081,8 +1352,8 @@ rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
 }
 
 
-/* Called after visiting basic block BB.  Restore CURRDEFS to its
-   original value.  */
+/* Called after visiting all the statements in basic block BB and all
+   of its dominator children.  Restore CURRDEFS to its original value.  */
 
 static void
 rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
@@ -1097,17 +1368,25 @@ rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
       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)
        {
+         /* If we recorded an SSA_NAME, then make the SSA_NAME the
+            current definition of its underlying variable.  Note that
+            if the SSA_NAME is not for a GIMPLE register, the symbol
+            being defined is stored in the next slot in the stack.
+            This mechanism is needed because an SSA name for a
+            non-register symbol may be the definition for more than
+            one symbol (e.g., SFTs, aliased variables, etc).  */
          saved_def = tmp;
          var = SSA_NAME_VAR (saved_def);
+         if (!is_gimple_reg (var))
+           var = VEC_pop (tree, block_defs_stack);
        }
       else
        {
+         /* If we recorded anything else, it must have been a _DECL
+            node and its current reaching definition must have been
+            NULL.  */
          saved_def = NULL;
          var = tmp;
        }
@@ -1117,24 +1396,157 @@ rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
 }
 
 
+/* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE.  */
+
+void
+dump_decl_set (FILE *file, bitmap set)
+{
+  if (set)
+    {
+      bitmap_iterator bi;
+      unsigned i;
+
+      fprintf (file, "{ ");
+
+      EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
+       {
+         print_generic_expr (file, referenced_var (i), 0);
+         fprintf (file, " ");
+       }
+
+      fprintf (file, "}\n");
+    }
+  else
+    fprintf (file, "NIL\n");
+}
+
+
+/* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE.  */
+
+void
+debug_decl_set (bitmap set)
+{
+  dump_decl_set (stderr, set);
+}
+
+
+/* Dump the renaming stack (block_defs_stack) to FILE.  Traverse the
+   stack up to a maximum of N levels.  If N is -1, the whole stack is
+   dumped.  New levels are created when the dominator tree traversal
+   used for renaming enters a new sub-tree.  */
+
+void
+dump_defs_stack (FILE *file, int n)
+{
+  int i, j;
+
+  fprintf (file, "\n\nRenaming stack");
+  if (n > 0)
+    fprintf (file, " (up to %d levels)", n);
+  fprintf (file, "\n\n");
+
+  i = 1;
+  fprintf (file, "Level %d (current level)\n", i);
+  for (j = (int) VEC_length (tree, block_defs_stack) - 1; j >= 0; j--)
+    {
+      tree name, var;
+      
+      name = VEC_index (tree, block_defs_stack, j);
+      if (name == NULL_TREE)
+       {
+         i++;
+         if (n > 0 && i > n)
+           break;
+         fprintf (file, "\nLevel %d\n", i);
+         continue;
+       }
+
+      if (DECL_P (name))
+       {
+         var = name;
+         name = NULL_TREE;
+       }
+      else
+       {
+         var = SSA_NAME_VAR (name);
+         if (!is_gimple_reg (var))
+           {
+             j--;
+             var = VEC_index (tree, block_defs_stack, j);
+           }
+       }
+
+      fprintf (file, "    Previous CURRDEF (");
+      print_generic_expr (file, var, 0);
+      fprintf (file, ") = ");
+      if (name)
+       print_generic_expr (file, name, 0);
+      else
+       fprintf (file, "<NIL>");
+      fprintf (file, "\n");
+    }
+}
+
+
+/* Dump the renaming stack (block_defs_stack) to stderr.  Traverse the
+   stack up to a maximum of N levels.  If N is -1, the whole stack is
+   dumped.  New levels are created when the dominator tree traversal
+   used for renaming enters a new sub-tree.  */
+
+void
+debug_defs_stack (int n)
+{
+  dump_defs_stack (stderr, n);
+}
+
+
+/* Dump the current reaching definition of every symbol to FILE.  */
+
+void
+dump_currdefs (FILE *file)
+{
+  referenced_var_iterator i;
+  tree var;
+
+  fprintf (file, "\n\nCurrent reaching definitions\n\n");
+  FOR_EACH_REFERENCED_VAR (var, i)
+    if (syms_to_rename == NULL || bitmap_bit_p (syms_to_rename, DECL_UID (var)))
+      {
+       fprintf (file, "CURRDEF (");
+       print_generic_expr (file, var, 0);
+       fprintf (file, ") = ");
+       if (get_current_def (var))
+         print_generic_expr (file, get_current_def (var), 0);
+       else
+         fprintf (file, "<NIL>");
+       fprintf (file, "\n");
+      }
+}
+
+
+/* Dump the current reaching definition of every symbol to stderr.  */
+
+void
+debug_currdefs (void)
+{
+  dump_currdefs (stderr);
+}
+
+
 /* 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);
+  fprintf (file, "SSA renaming 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_def_blocks (file);
+  dump_defs_stack (file, -1);
+  dump_currdefs (file);
+  dump_tree_ssa_stats (file);
 }
 
 
@@ -1164,12 +1576,23 @@ htab_statistics (FILE *file, htab_t htab)
 void
 dump_tree_ssa_stats (FILE *file)
 {
-  fprintf (file, "\nHash table statistics:\n");
+  if (def_blocks || repl_tbl)
+    fprintf (file, "\nHash table statistics:\n");
+
+  if (def_blocks)
+    {
+      fprintf (file, "    def_blocks:   ");
+      htab_statistics (file, def_blocks);
+    }
 
-  fprintf (file, "    def_blocks: ");
-  htab_statistics (file, def_blocks);
+  if (repl_tbl)
+    {
+      fprintf (file, "    repl_tbl:     ");
+      htab_statistics (file, repl_tbl);
+    }
 
-  fprintf (file, "\n");
+  if (def_blocks || repl_tbl)
+    fprintf (file, "\n");
 }
 
 
@@ -1215,25 +1638,38 @@ def_blocks_free (void *p)
 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table.  */
 
 static int
-debug_def_blocks_r (void **slot, void *data ATTRIBUTE_UNUSED)
+debug_def_blocks_r (void **slot, void *data)
 {
+  FILE *file = (FILE *) data;
   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");
+  fprintf (file, "VAR: ");
+  print_generic_expr (file, db_p->var, dump_flags);
+  bitmap_print (file, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
+  bitmap_print (file, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}");
+  bitmap_print (file, db_p->phi_blocks, ", PHI_BLOCKS: { ", "}\n");
 
   return 1;
 }
 
 
+/* Dump the DEF_BLOCKS hash table on FILE.  */
+
+void
+dump_def_blocks (FILE *file)
+{
+  fprintf (file, "\n\nDefinition and live-in blocks:\n\n");
+  if (def_blocks)
+    htab_traverse (def_blocks, debug_def_blocks_r, file);
+}
+
+
 /* Dump the DEF_BLOCKS hash table on stderr.  */
 
 void
 debug_def_blocks (void)
 {
-  htab_traverse (def_blocks, debug_def_blocks_r, NULL);
+  dump_def_blocks (stderr);
 }
 
 
@@ -1244,7 +1680,7 @@ 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.
+  /* Push the current reaching definition into BLOCK_DEFS_STACK.
      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
@@ -1295,6 +1731,9 @@ rewrite_update_init_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
   /* Mark the unwind point for this block.  */
   VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
 
+  if (!bitmap_bit_p (blocks_to_update, bb->index))
+    return;
+
   /* Mark the LHS if any of the arguments flows through an abnormal
      edge.  */
   is_abnormal_phi = false;
@@ -1324,6 +1763,7 @@ rewrite_update_init_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
        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))
@@ -1399,8 +1839,8 @@ 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 DEF is a naked symbol that needs renaming, create a new
+     name for it.  */
   if (symbol_marked_for_renaming (sym))
     {
       if (DECL_P (def))
@@ -1447,6 +1887,8 @@ rewrite_update_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
   stmt = bsi_stmt (si);
   ann = stmt_ann (stmt);
 
+  gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
+
   /* Only update marked statements.  */
   if (!REWRITE_THIS_STMT (stmt) && !REGISTER_DEFS_IN_THIS_STMT (stmt))
     return;
@@ -1466,8 +1908,7 @@ rewrite_update_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
        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)
+       FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_VIRTUAL_USES)
          maybe_replace_use (use_p);
     }
 
@@ -1486,18 +1927,6 @@ rewrite_update_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
 }
 
 
-/* Replace the operand pointed to 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
@@ -1509,19 +1938,23 @@ rewrite_update_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
 {
   edge e;
   edge_iterator ei;
+  unsigned i;
 
   FOR_EACH_EDGE (e, ei, bb->succs)
     {
       tree phi;
+      tree_vec phis;
 
-      for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
+      if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
+       continue;
+     
+      phis = VEC_index (tree_vec, phis_to_rewrite, e->dest->index);
+      for (i = 0; VEC_iterate (tree, phis, i, phi); i++)
        {
-         tree arg;
+         tree arg, lhs_sym;
          use_operand_p arg_p;
 
-         /* Skip PHI nodes that are not marked for rewrite.  */
-         if (!REWRITE_THIS_STMT (phi))
-           continue;
+         gcc_assert (REWRITE_THIS_STMT (phi));
 
          arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
          arg = USE_FROM_PTR (arg_p);
@@ -1529,21 +1962,23 @@ rewrite_update_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
          if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
            continue;
 
+         lhs_sym = SSA_NAME_VAR (PHI_RESULT (phi));
+
          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)));
+             SET_USE (arg_p, get_reaching_def (lhs_sym));
            }
          else
            {
              tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
 
              if (symbol_marked_for_renaming (sym))
-               replace_use (arg_p, sym);
+               SET_USE (arg_p, get_reaching_def (sym));
              else if (is_old_name (arg))
-               replace_use (arg_p, arg);
+               SET_USE (arg_p, get_reaching_def (arg));
            }
 
          if (e->flags & EDGE_ABNORMAL)
@@ -1581,10 +2016,10 @@ rewrite_blocks (basic_block entry, enum rewrite_mode what, sbitmap blocks)
   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
+  if (what == REWRITE_ALL)
     walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
+  else
+    walk_data.before_dom_children_before_stmts = rewrite_update_init_block;
 
   if (what == REWRITE_ALL)
     walk_data.before_dom_children_walk_stmts = rewrite_stmt;
@@ -1626,12 +2061,6 @@ rewrite_blocks (basic_block entry, enum rewrite_mode what, sbitmap blocks)
       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);
 
@@ -1646,10 +2075,9 @@ 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 =
-     (struct mark_def_sites_global_data *) walk_data->global_data;
-  bitmap kills = gd->kills;
-  bitmap_clear (kills);
+  struct mark_def_sites_global_data *gd;
+  gd = (struct mark_def_sites_global_data *) walk_data->global_data;
+  bitmap_clear (gd->kills);
 }
 
 
@@ -1665,14 +2093,6 @@ mark_def_site_blocks (sbitmap interesting_blocks)
 {
   struct dom_walk_data walk_data;
   struct mark_def_sites_global_data mark_def_sites_global_data;
-  referenced_var_iterator rvi;
-  tree var;
-
-  /* Allocate memory for the DEF_BLOCKS hash table.  */
-  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);
 
   /* Setup callbacks for the generic dominator tree walker to find and
      mark definition sites.  */
@@ -1714,6 +2134,41 @@ mark_def_site_blocks (sbitmap interesting_blocks)
 }
 
 
+/* 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:
 
@@ -1731,7 +2186,7 @@ mark_def_site_blocks (sbitmap interesting_blocks)
    Steps 3 and 4 are done using the dominator tree walker
    (walk_dominator_tree).  */
 
-static void
+static unsigned int
 rewrite_into_ssa (void)
 {
   bitmap *dfs;
@@ -1743,6 +2198,9 @@ rewrite_into_ssa (void)
   /* 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.  */
@@ -1750,7 +2208,7 @@ rewrite_into_ssa (void)
   sbitmap_zero (interesting_blocks);
 
   /* Initialize dominance frontier.  */
-  dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap));
+  dfs = XNEWVEC (bitmap, last_basic_block);
   FOR_EACH_BB (bb)
     dfs[bb->index] = BITMAP_ALLOC (NULL);
 
@@ -1773,13 +2231,17 @@ rewrite_into_ssa (void)
   free (dfs);
   sbitmap_free (interesting_blocks);
 
+  fini_ssa_renamer ();
+
   timevar_pop (TV_TREE_SSA_OTHER);
-  in_ssa_p = true;
+  return 0;
 }
 
 
-struct tree_opt_pass pass_build_ssa = 
+struct gimple_opt_pass pass_build_ssa = 
 {
+ {
+  GIMPLE_PASS,
   "ssa",                               /* name */
   NULL,                                        /* gate */
   rewrite_into_ssa,                    /* execute */
@@ -1793,8 +2255,8 @@ struct tree_opt_pass pass_build_ssa =
   0,                                   /* todo_flags_start */
   TODO_dump_func
     | TODO_verify_ssa
-    | TODO_remove_unused_locals,       /* todo_flags_finish */
-  0                                    /* letter */
+    | TODO_remove_unused_locals                /* todo_flags_finish */
+ }
 };
 
 
@@ -1802,11 +2264,10 @@ struct tree_opt_pass pass_build_ssa =
    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)
+mark_def_interesting (tree var, tree stmt, basic_block bb, bool insert_phi_p)
 {
+  gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
   REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
-  bitmap_set_bit (blocks, bb->index);
 
   if (insert_phi_p)
     {
@@ -1831,14 +2292,20 @@ mark_def_interesting (tree var, tree stmt, basic_block bb, bitmap blocks,
 
 /* 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.  */
+   nodes.  */
 
 static inline void
-mark_use_interesting (tree var, tree stmt, basic_block bb, bitmap blocks,
-                     bool insert_phi_p)
+mark_use_interesting (tree var, tree stmt, basic_block bb, bool insert_phi_p)
 {
-  REWRITE_THIS_STMT (stmt) = 1;
-  bitmap_set_bit (blocks, bb->index);
+  basic_block def_bb = bb_for_stmt (stmt);
+
+  mark_block_for_update (def_bb);
+  mark_block_for_update (bb);
+
+  if (TREE_CODE (stmt) == PHI_NODE)
+    mark_phi_for_rewrite (def_bb, stmt);
+  else
+    REWRITE_THIS_STMT (stmt) = 1;
 
   /* 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
@@ -1857,19 +2324,28 @@ mark_use_interesting (tree var, tree stmt, basic_block bb, bitmap blocks,
 /* 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.  Blocks that contain defs or uses of symbols
-   in SYMS_TO_RENAME are added to BLOCKS.
+   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.  */
+   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, bitmap blocks, bool insert_phi_p)
+prepare_block_for_update (basic_block bb, bool insert_phi_p)
 {
   basic_block son;
   block_stmt_iterator si;
   tree phi;
+  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.  */
@@ -1879,10 +2355,20 @@ prepare_block_for_update (basic_block bb, bitmap blocks, bool insert_phi_p)
 
       lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
 
-      if (symbol_marked_for_renaming (lhs_sym))
+      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, bb, blocks, insert_phi_p);
-         mark_def_interesting (lhs_sym, phi, bb, blocks, insert_phi_p);
+         mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
        }
     }
 
@@ -1896,50 +2382,28 @@ prepare_block_for_update (basic_block bb, bitmap blocks, bool insert_phi_p)
       
       stmt = bsi_stmt (si);
 
-      FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
+      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 (use, stmt, bb, blocks, insert_phi_p);
+           mark_use_interesting (sym, stmt, bb, insert_phi_p);
        }
 
-      FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_DEF)
+      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 (def, stmt, bb, blocks, insert_phi_p);
-       }
-
-      FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_VIRTUAL_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_use_interesting (sym, stmt, bb, blocks, insert_phi_p);
-             mark_def_interesting (sym, stmt, bb, blocks, insert_phi_p);
-           }
-       }
-
-      FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_VUSE)
-       {
-         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, blocks, insert_phi_p);
+           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, blocks, insert_phi_p);
+       son;
+       son = next_dom_son (CDI_DOMINATORS, son))
+    prepare_block_for_update (son, insert_phi_p);
 }
 
 
@@ -1948,7 +2412,7 @@ prepare_block_for_update (basic_block bb, bitmap blocks, bool insert_phi_p)
    prepare_names_to_update.  */
 
 static void
-prepare_use_sites_for (tree name, bitmap blocks, bool insert_phi_p)
+prepare_use_sites_for (tree name, bool insert_phi_p)
 {
   use_operand_p use_p;
   imm_use_iterator iter;
@@ -1960,44 +2424,15 @@ prepare_use_sites_for (tree name, bitmap blocks, bool insert_phi_p)
 
       if (TREE_CODE (stmt) == PHI_NODE)
        {
-         /* Mark this use of NAME interesting for the renamer.
-            Notice that we explicitly call mark_use_interesting with
-            INSERT_PHI_P == false.
-
-            This is to avoid marking NAME as live-in in this block
-            BB. If we were to mark NAME live-in to BB, then NAME
-            would be considered live-in through ALL incoming edges to
-            BB which is not what we want.  Since we are updating the
-            SSA form for NAME, we don't really know what other names
-            of NAME are coming in through other edges into BB.
-
-            If we considered NAME live-in at BB, then the PHI
-            placement algorithm may try to insert PHI nodes in blocks
-            that are not only unnecessary but also the renamer would
-            not know how to fill in.  */
-         mark_use_interesting (name, stmt, bb, blocks, false);
-
-         /* As discussed above, we only want to mark NAME live-in
-            through the edge corresponding to its slot inside the PHI
-            argument list.  So, we look for the block BB1 where NAME
-            is flowing through.  If BB1 does not contain a definition
-            of NAME, then consider NAME live-in at BB1.  */
-         if (insert_phi_p)
-           {
-             int ix = PHI_ARG_INDEX_FROM_USE (use_p);
-             edge e = PHI_ARG_EDGE (stmt, ix);
-             basic_block bb1 = e->src;
-             struct def_blocks_d *db = get_def_blocks_for (name);
-
-             if (!bitmap_bit_p (db->def_blocks, bb1->index))
-               set_livein_block (name, bb1);
-           }
+         int ix = PHI_ARG_INDEX_FROM_USE (use_p);
+         edge e = 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, blocks, insert_phi_p);
+         mark_use_interesting (name, stmt, bb, insert_phi_p);
        }
     }
 }
@@ -2008,7 +2443,7 @@ prepare_use_sites_for (tree name, bitmap blocks, bool insert_phi_p)
    prepare_names_to_update.  */
 
 static void
-prepare_def_site_for (tree name, bitmap blocks, bool insert_phi_p)
+prepare_def_site_for (tree name, bool insert_phi_p)
 {
   tree stmt;
   basic_block bb;
@@ -2021,18 +2456,18 @@ prepare_def_site_for (tree name, bitmap blocks, bool insert_phi_p)
   if (bb)
     {
       gcc_assert (bb->index < last_basic_block);
-      mark_def_interesting (name, stmt, bb, blocks, insert_phi_p);
+      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.  Add each definition block to BLOCKS.  INSERT_PHI_P
-   is true if the caller wants to insert PHI nodes for newly created
-   names.  */
+   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 (bitmap blocks, bool insert_phi_p)
+prepare_names_to_update (bool insert_phi_p)
 {
   unsigned i = 0;
   bitmap_iterator bi;
@@ -2051,15 +2486,15 @@ prepare_names_to_update (bitmap blocks, bool insert_phi_p)
      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), blocks, insert_phi_p);
+    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), blocks, insert_phi_p);
-      prepare_use_sites_for (ssa_name (i), blocks, insert_phi_p);
+       prepare_def_site_for (ssa_name (i), insert_phi_p);
+      prepare_use_sites_for (ssa_name (i), insert_phi_p);
     }
 }
 
@@ -2134,11 +2569,7 @@ dump_update_ssa (FILE *file)
   if (syms_to_rename && !bitmap_empty_p (syms_to_rename))
     {
       fprintf (file, "\n\nSymbols to be put in SSA form\n\n");
-      EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
-       {
-         print_generic_expr (file, referenced_var (i), 0);
-         fprintf (file, " ");
-       }
+      dump_decl_set (file, syms_to_rename);
     }
 
   if (names_to_release && !bitmap_empty_p (names_to_release))
@@ -2182,6 +2613,8 @@ init_update_ssa (void)
   need_to_initialize_update_ssa_p = false;
   need_to_update_vops_p = false;
   syms_to_rename = BITMAP_ALLOC (NULL);
+  regs_to_rename = BITMAP_ALLOC (NULL);
+  mem_syms_to_rename = BITMAP_ALLOC (NULL);
   names_to_release = NULL;
   memset (&update_ssa_stats, 0, sizeof (update_ssa_stats));
   update_ssa_stats.virtual_symbols = BITMAP_ALLOC (NULL);
@@ -2208,6 +2641,8 @@ delete_update_ssa (void)
   need_to_initialize_update_ssa_p = true;
   need_to_update_vops_p = false;
   BITMAP_FREE (syms_to_rename);
+  BITMAP_FREE (regs_to_rename);
+  BITMAP_FREE (mem_syms_to_rename);
   BITMAP_FREE (update_ssa_stats.virtual_symbols);
 
   if (names_to_release)
@@ -2217,16 +2652,21 @@ delete_update_ssa (void)
       BITMAP_FREE (names_to_release);
     }
 
-  for (i = 1; i < num_ssa_names; i++)
-    {
-      tree n = ssa_name (i);
+  clear_ssa_name_info ();
 
-      if (n)
-       {
-         free (SSA_NAME_AUX (n));
-         SSA_NAME_AUX (n) = NULL;
-       }
-    }
+  fini_ssa_renamer ();
+
+  if (blocks_with_phis_to_rewrite)
+    EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi)
+      {
+       tree_vec phis = VEC_index (tree_vec, phis_to_rewrite, i);
+
+       VEC_free (tree, heap, phis);
+       VEC_replace (tree_vec, phis_to_rewrite, i, NULL);
+      }
+
+  BITMAP_FREE (blocks_with_phis_to_rewrite);
+  BITMAP_FREE (blocks_to_update);
 }
 
 
@@ -2293,7 +2733,11 @@ mark_sym_for_renaming (tree sym)
   bitmap_set_bit (syms_to_rename, DECL_UID (sym));
 
   if (!is_gimple_reg (sym))
-    need_to_update_vops_p = true;
+    {
+      need_to_update_vops_p = true;
+      if (memory_partition (sym))
+       bitmap_set_bit (syms_to_rename, DECL_UID (memory_partition (sym)));
+    }
 }
 
 
@@ -2305,20 +2749,14 @@ mark_set_for_renaming (bitmap set)
   bitmap_iterator bi;
   unsigned i;
 
-  if (bitmap_empty_p (set))
+  if (set == NULL || bitmap_empty_p (set))
     return;
 
   if (need_to_initialize_update_ssa_p)
     init_update_ssa ();
 
-  bitmap_ior_into (syms_to_rename, set);
-
   EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
-    if (!is_gimple_reg (referenced_var (i)))
-      {
-       need_to_update_vops_p = true;
-       break;
-      }
+    mark_sym_for_renaming (referenced_var (i));
 }
 
 
@@ -2330,6 +2768,13 @@ need_ssa_update_p (void)
   return syms_to_rename || old_ssa_names || new_ssa_names;
 }
 
+/* Return true if SSA name mappings have been registered for SSA updating.  */
+
+bool
+name_mappings_registered_p (void)
+{
+  return repl_tbl && htab_elements (repl_tbl) > 0;
+}
 
 /* Return true if name N has been registered in the replacement table.  */
 
@@ -2424,7 +2869,7 @@ insert_updated_phi_nodes_for (tree var, bitmap *dfs, bitmap blocks,
     return;
 
   /* Compute the initial iterated dominance frontier.  */
-  idf = find_idf (db->def_blocks, dfs);
+  idf = compute_idf (db->def_blocks, dfs);
   pruned_idf = BITMAP_ALLOC (NULL);
 
   if (TREE_CODE (var) == SSA_NAME)
@@ -2436,7 +2881,6 @@ insert_updated_phi_nodes_for (tree var, bitmap *dfs, bitmap blocks,
             common dominator of all the definition blocks.  */
          entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
                                                    db->def_blocks);
-
          if (entry != ENTRY_BLOCK_PTR)
            EXECUTE_IF_SET_IN_BITMAP (idf, 0, i, bi)
              if (BASIC_BLOCK (i) != entry
@@ -2464,6 +2908,9 @@ insert_updated_phi_nodes_for (tree var, bitmap *dfs, bitmap blocks,
         are included in the region to be updated.  The feeding blocks
         are important to guarantee that the PHI arguments are renamed
         properly.  */
+
+      /* FIXME, this is not needed if we are updating symbols.  We are
+        already starting at the ENTRY block anyway.  */
       bitmap_ior_into (blocks, pruned_idf);
       EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
        {
@@ -2547,7 +2994,7 @@ switch_virtuals_to_full_rewrite (void)
     if (!is_gimple_reg (ssa_name (i)))
       RESET_BIT (old_ssa_names, i);
 
-  bitmap_ior_into (syms_to_rename, update_ssa_stats.virtual_symbols);
+  mark_set_for_renaming (update_ssa_stats.virtual_symbols);
 }
 
 
@@ -2618,7 +3065,6 @@ switch_virtuals_to_full_rewrite (void)
 void
 update_ssa (unsigned update_flags)
 {
-  bitmap blocks;
   basic_block bb, start_bb;
   bitmap_iterator bi;
   unsigned i = 0;
@@ -2631,6 +3077,11 @@ update_ssa (unsigned update_flags)
 
   timevar_push (TV_TREE_SSA_INCREMENTAL);
 
+  blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
+  if (!phis_to_rewrite)
+    phis_to_rewrite = VEC_alloc (tree_vec, heap, last_basic_block);
+  blocks_to_update = BITMAP_ALLOC (NULL);
+
   /* Ensure that the dominance information is up-to-date.  */
   calculate_dominance_info (CDI_DOMINATORS);
 
@@ -2669,44 +3120,46 @@ update_ssa (unsigned update_flags)
       def_blocks = NULL;
     }
 
-  blocks = BITMAP_ALLOC (NULL);
+  /* Heuristic to avoid massive slow downs when the replacement
+     mappings include lots of virtual names.  */
+  if (insert_phi_p && switch_virtuals_to_full_rewrite_p ())
+    switch_virtuals_to_full_rewrite ();
 
-  /* Clear the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags
-     for every statement and PHI node.  */
-  FOR_EACH_BB (bb)
+  /* If there are symbols to rename, identify those symbols that are
+     GIMPLE registers into the set REGS_TO_RENAME and those that are
+     memory symbols into the set MEM_SYMS_TO_RENAME.  */
+  if (!bitmap_empty_p (syms_to_rename))
     {
-      block_stmt_iterator si;
-      tree phi;
+      unsigned i;
+      bitmap_iterator bi;
 
-      for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
+      EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
        {
-         REWRITE_THIS_STMT (phi) = 0;
-         REGISTER_DEFS_IN_THIS_STMT (phi) = 0;
+         tree sym = referenced_var (i);
+         if (is_gimple_reg (sym))
+           bitmap_set_bit (regs_to_rename, i);
+         else
+           {
+             /* Memory partitioning information may have been
+                computed after the symbol was marked for renaming,
+                if SYM is inside a partition also mark the partition
+                for renaming.  */
+             tree mpt = memory_partition (sym);
+             if (mpt)
+               bitmap_set_bit (syms_to_rename, DECL_UID (mpt));
+           }
        }
 
-      for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
-       {
-         tree stmt = bsi_stmt (si);
-         /* We are going to use the operand cache API, such as
-            SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST.  The operand
-            cache for each statement should be up-to-date.  */
-         gcc_assert (!stmt_modified_p (stmt));
-         REWRITE_THIS_STMT (stmt) = 0;
-         REGISTER_DEFS_IN_THIS_STMT (stmt) = 0;
-       }
+      /* Memory symbols are those not in REGS_TO_RENAME.  */
+      bitmap_and_compl (mem_syms_to_rename, syms_to_rename, regs_to_rename);
     }
 
-  /* Heuristic to avoid massive slow downs when the replacement
-     mappings include lots of virtual names.  */
-  if (insert_phi_p && switch_virtuals_to_full_rewrite_p ())
-    switch_virtuals_to_full_rewrite ();
-
   /* If there are names defined in the replacement table, prepare
      definition and use sites for all the names in NEW_SSA_NAMES and
      OLD_SSA_NAMES.  */
   if (sbitmap_first_set_bit (new_ssa_names) >= 0)
     {
-      prepare_names_to_update (blocks, insert_phi_p);
+      prepare_names_to_update (insert_phi_p);
 
       /* If all the names in NEW_SSA_NAMES had been marked for
         removal, and there are no symbols to rename, then there's
@@ -2726,17 +3179,18 @@ update_ssa (unsigned update_flags)
         updating.  For now this seems more work than it's worth.  */
       start_bb = ENTRY_BLOCK_PTR;
 
-      /* Traverse the CFG looking for definitions and uses of symbols
-        in SYMS_TO_RENAME.  Mark interesting blocks and statements
-        and set local live-in information for the PHI placement
-        heuristics.  */
-      prepare_block_for_update (start_bb, blocks, insert_phi_p);
+      /* Traverse the CFG looking for existing definitions and uses of
+        symbols in SYMS_TO_RENAME.  Mark interesting blocks and
+        statements and set local live-in information for the PHI
+        placement heuristics.  */
+      prepare_block_for_update (start_bb, insert_phi_p);
     }
   else
     {
       /* Otherwise, the entry block to the region is the nearest
         common dominator for the blocks in BLOCKS.  */
-      start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS, blocks);
+      start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
+                                                  blocks_to_update);
     }
 
   /* If requested, insert PHI nodes at the iterated dominance frontier
@@ -2765,14 +3219,14 @@ update_ssa (unsigned update_flags)
          sbitmap tmp = sbitmap_alloc (old_ssa_names->n_bits);
          sbitmap_copy (tmp, old_ssa_names);
          EXECUTE_IF_SET_IN_SBITMAP (tmp, 0, i, sbi)
-           insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks,
+           insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks_to_update,
                                          update_flags);
          sbitmap_free (tmp);
        }
 
       EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
-       insert_updated_phi_nodes_for (referenced_var (i), dfs, blocks,
-                                     update_flags);
+       insert_updated_phi_nodes_for (referenced_var (i), dfs, blocks_to_update,
+                                     update_flags);
 
       FOR_EACH_BB (bb)
        BITMAP_FREE (dfs[bb->index]);
@@ -2782,7 +3236,8 @@ update_ssa (unsigned update_flags)
         We need to re-compute START_BB to include the newly added
         blocks.  */
       if (start_bb != ENTRY_BLOCK_PTR)
-       start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS, blocks);
+       start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
+                                                    blocks_to_update);
     }
 
   /* Reset the current definition for name and symbol before renaming
@@ -2796,7 +3251,7 @@ update_ssa (unsigned update_flags)
   /* Now start the renaming process at START_BB.  */
   tmp = sbitmap_alloc (last_basic_block);
   sbitmap_zero (tmp);
-  EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
+  EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
     SET_BIT (tmp, i);
 
   rewrite_blocks (start_bb, REWRITE_UPDATE, tmp);
@@ -2815,7 +3270,7 @@ update_ssa (unsigned update_flags)
               start_bb->index);
 
       c = 0;
-      EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
+      EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
        c++;
       fprintf (dump_file, "Number of blocks in CFG: %d\n", last_basic_block);
       fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n\n",
@@ -2824,7 +3279,7 @@ update_ssa (unsigned update_flags)
       if (dump_flags & TDF_DETAILS)
        {
          fprintf (dump_file, "Affected blocks: ");
-         EXECUTE_IF_SET_IN_BITMAP (blocks, 0, i, bi)
+         EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
            fprintf (dump_file, "%u ", i);
          fprintf (dump_file, "\n");
        }
@@ -2834,7 +3289,6 @@ update_ssa (unsigned update_flags)
 
   /* Free allocated memory.  */
 done:
-  BITMAP_FREE (blocks);
   delete_update_ssa ();
 
   timevar_pop (TV_TREE_SSA_INCREMENTAL);