OSDN Git Service

PR c++/41183
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-dom.c
index 0660cc6..48f423b 100644 (file)
@@ -1,12 +1,13 @@
 /* SSA Dominator optimizations for trees
-   Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+   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, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
 #include "system.h"
@@ -29,8 +29,8 @@ Boston, MA 02111-1307, USA.  */
 #include "tm_p.h"
 #include "ggc.h"
 #include "basic-block.h"
+#include "cfgloop.h"
 #include "output.h"
-#include "errors.h"
 #include "expr.h"
 #include "function.h"
 #include "diagnostic.h"
@@ -40,10 +40,73 @@ Boston, MA 02111-1307, USA.  */
 #include "domwalk.h"
 #include "real.h"
 #include "tree-pass.h"
+#include "tree-ssa-propagate.h"
 #include "langhooks.h"
+#include "params.h"
 
 /* This file implements optimizations on the dominator tree.  */
 
+/* Representation of a "naked" right-hand-side expression, to be used
+   in recording available expressions in the expression hash table.  */
+
+enum expr_kind
+{
+  EXPR_SINGLE,
+  EXPR_UNARY,
+  EXPR_BINARY,
+  EXPR_CALL
+};
+
+struct hashable_expr
+{
+  tree type;
+  enum expr_kind kind;
+  union {
+    struct { tree rhs; } single;
+    struct { enum tree_code op;  tree opnd; } unary;
+    struct { enum tree_code op;  tree opnd0; tree opnd1; } binary;
+    struct { tree fn; bool pure; size_t nargs; tree *args; } call;
+  } ops;
+};
+
+/* Structure for recording known values of a conditional expression
+   at the exits from its block.  */
+
+struct cond_equivalence
+{
+  struct hashable_expr cond;
+  tree value;
+};
+
+/* Structure for recording edge equivalences as well as any pending
+   edge redirections during the dominator optimizer.
+
+   Computing and storing the edge equivalences instead of creating
+   them on-demand can save significant amounts of time, particularly
+   for pathological cases involving switch statements.
+
+   These structures live for a single iteration of the dominator
+   optimizer in the edge's AUX field.  At the end of an iteration we
+   free each of these structures and update the AUX field to point
+   to any requested redirection target (the code for updating the
+   CFG and SSA graph for edge redirection expects redirection edge
+   targets to be in the AUX field for each edge.  */
+
+struct edge_info
+{
+  /* If this edge creates a simple equivalence, the LHS and RHS of
+     the equivalence will be stored here.  */
+  tree lhs;
+  tree rhs;
+
+  /* Traversing an edge may also indicate one or more particular conditions
+     are true or false.  The number of recorded conditions can vary, but
+     can be determined by the condition's code.  So we have an array
+     and its maximum index rather than use a varray.  */
+  struct cond_equivalence *cond_equivalences;
+  unsigned int max_cond_equivalences;
+};
+
 /* Hash table with expressions made available during the renaming process.
    When an assignment of the form X_i = EXPR is found, the statement is
    stored in this table.  If the same expression EXPR is later found on the
@@ -53,16 +116,18 @@ Boston, MA 02111-1307, USA.  */
    in this table.  */
 static htab_t avail_exprs;
 
-/* Structure for entries in the expression hash table.
+/* Stack of available expressions in AVAIL_EXPRs.  Each block pushes any
+   expressions it enters into the hash table along with a marker entry
+   (null).  When we finish processing the block, we pop off entries and
+   remove the expressions from the global hash table until we hit the
+   marker.  */
+typedef struct expr_hash_elt * expr_hash_elt_t;
+DEF_VEC_P(expr_hash_elt_t);
+DEF_VEC_ALLOC_P(expr_hash_elt_t,heap);
+
+static VEC(expr_hash_elt_t,heap) *avail_exprs_stack;
 
-   This requires more memory for the hash table entries, but allows us
-   to avoid creating silly tree nodes and annotations for conditionals,
-   eliminates 2 global hash tables and two block local varrays.
-   
-   It also allows us to reduce the number of hash table lookups we
-   have to perform in lookup_avail_expr and finally it allows us to
-   significantly reduce the number of calls into the hashing routine
-   itself.  */
+/* Structure for entries in the expression hash table.  */
 
 struct expr_hash_elt
 {
@@ -70,28 +135,24 @@ struct expr_hash_elt
   tree lhs;
 
   /* The expression (rhs) we want to record.  */
-  tree rhs;
+  struct hashable_expr expr;
 
-  /* The annotation if this element corresponds to a statement.  */
-  stmt_ann_t ann;
+  /* The stmt pointer if this element corresponds to a statement.  */
+  gimple stmt;
 
-  /* The hash value for RHS/ann.  */
+  /* The hash value for RHS.  */
   hashval_t hash;
+
+  /* A unique stamp, typically the address of the hash
+     element itself, used in removing entries from the table.  */
+  struct expr_hash_elt *stamp;
 };
 
-/* Table of constant values and copies indexed by SSA name.  When the
-   renaming pass finds an assignment of a constant (X_i = C) or a copy
-   assignment from another SSA variable (X_i = Y_j), it creates a mapping
-   between X_i and the RHS in this table.  This mapping is used later on,
-   when renaming uses of X_i.  If an assignment to X_i is found in this
-   table, instead of using X_i, we use the RHS of the statement stored in
-   this table (thus performing very simplistic copy and constant
-   propagation).  */
-static varray_type const_and_copies;
+/* Stack of dest,src pairs that need to be restored during finalization.
 
-/* Bitmap of SSA_NAMEs known to have a nonzero value, even if we do not
-   know their exact value.  */
-static bitmap nonzero_vars;
+   A NULL entry is used to mark the end of pairs which need to be
+   restored during finalization of this block.  */
+static VEC(tree,heap) *const_and_copies_stack;
 
 /* Track whether or not we have changed the control flow graph.  */
 static bool cfg_altered;
@@ -106,310 +167,568 @@ struct opt_stats_d
   long num_stmts;
   long num_exprs_considered;
   long num_re;
+  long num_const_prop;
+  long num_copy_prop;
 };
 
-/* Value range propagation record.  Each time we encounter a conditional
-   of the form SSA_NAME COND CONST we create a new vrp_element to record
-   how the condition affects the possible values SSA_NAME may have.
+static struct opt_stats_d opt_stats;
 
-   Each record contains the condition tested (COND), and the the range of
-   values the variable may legitimately have if COND is true.  Note the
-   range of values may be a smaller range than COND specifies if we have
-   recorded other ranges for this variable.  Each record also contains the
-   block in which the range was recorded for invalidation purposes.
+/* Local functions.  */
+static void optimize_stmt (basic_block, gimple_stmt_iterator);
+static tree lookup_avail_expr (gimple, bool);
+static hashval_t avail_expr_hash (const void *);
+static hashval_t real_avail_expr_hash (const void *);
+static int avail_expr_eq (const void *, const void *);
+static void htab_statistics (FILE *, htab_t);
+static void record_cond (struct cond_equivalence *);
+static void record_const_or_copy (tree, tree);
+static void record_equality (tree, tree);
+static void record_equivalences_from_phis (basic_block);
+static void record_equivalences_from_incoming_edge (basic_block);
+static void eliminate_redundant_computations (gimple_stmt_iterator *);
+static void record_equivalences_from_stmt (gimple, int);
+static void dom_thread_across_edge (struct dom_walk_data *, edge);
+static void dom_opt_leave_block (struct dom_walk_data *, basic_block);
+static void dom_opt_enter_block (struct dom_walk_data *, basic_block);
+static void remove_local_expressions_from_table (void);
+static void restore_vars_to_original_value (void);
+static edge single_incoming_edge_ignoring_loop_edges (basic_block);
 
-   Note that the current known range is computed lazily.  This allows us
-   to avoid the overhead of computing ranges which are never queried.
 
-   When we encounter a conditional, we look for records which constrain
-   the SSA_NAME used in the condition.  In some cases those records allow
-   us to determine the condition's result at compile time.  In other cases
-   they may allow us to simplify the condition.
+/* Given a statement STMT, initialize the hash table element pointed to
+   by ELEMENT.  */
+
+static void
+initialize_hash_element (gimple stmt, tree lhs,
+                         struct expr_hash_elt *element)
+{
+  enum gimple_code code = gimple_code (stmt);
+  struct hashable_expr *expr = &element->expr;
+
+  if (code == GIMPLE_ASSIGN)
+    {
+      enum tree_code subcode = gimple_assign_rhs_code (stmt);
+
+      expr->type = NULL_TREE;
+
+      switch (get_gimple_rhs_class (subcode))
+        {
+        case GIMPLE_SINGLE_RHS:
+          expr->kind = EXPR_SINGLE;
+          expr->ops.single.rhs = gimple_assign_rhs1 (stmt);
+          break;
+        case GIMPLE_UNARY_RHS:
+          expr->kind = EXPR_UNARY;
+         expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
+          expr->ops.unary.op = subcode;
+          expr->ops.unary.opnd = gimple_assign_rhs1 (stmt);
+          break;
+        case GIMPLE_BINARY_RHS:
+          expr->kind = EXPR_BINARY;
+         expr->type = TREE_TYPE (gimple_assign_lhs (stmt));
+          expr->ops.binary.op = subcode;
+          expr->ops.binary.opnd0 = gimple_assign_rhs1 (stmt);
+          expr->ops.binary.opnd1 = gimple_assign_rhs2 (stmt);
+          break;
+        default:
+          gcc_unreachable ();
+        }
+    }
+  else if (code == GIMPLE_COND)
+    {
+      expr->type = boolean_type_node;
+      expr->kind = EXPR_BINARY;
+      expr->ops.binary.op = gimple_cond_code (stmt);
+      expr->ops.binary.opnd0 = gimple_cond_lhs (stmt);
+      expr->ops.binary.opnd1 = gimple_cond_rhs (stmt);
+    }
+  else if (code == GIMPLE_CALL)
+    {
+      size_t nargs = gimple_call_num_args (stmt);
+      size_t i;
 
-   We also use value ranges to do things like transform signed div/mod
-   operations into unsigned div/mod or to simplify ABS_EXPRs. 
+      gcc_assert (gimple_call_lhs (stmt));
 
-   Simple experiments have shown these optimizations to not be all that
-   useful on switch statements (much to my surprise).  So switch statement
-   optimizations are not performed.
+      expr->type = TREE_TYPE (gimple_call_lhs (stmt));
+      expr->kind = EXPR_CALL;
+      expr->ops.call.fn = gimple_call_fn (stmt);
 
-   Note carefully we do not propagate information through each statement
-   in the block.  ie, if we know variable X has a value defined of
-   [0, 25] and we encounter Y = X + 1, we do not track a value range
-   for Y (which would be [1, 26] if we cared).  Similarly we do not
-   constrain values as we encounter narrowing typecasts, etc.  */
+      if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
+        expr->ops.call.pure = true;
+      else
+        expr->ops.call.pure = false;
 
-struct vrp_element
+      expr->ops.call.nargs = nargs;
+      expr->ops.call.args = (tree *) xcalloc (nargs, sizeof (tree));
+      for (i = 0; i < nargs; i++)
+        expr->ops.call.args[i] = gimple_call_arg (stmt, i);
+    }
+  else if (code == GIMPLE_SWITCH)
+    {
+      expr->type = TREE_TYPE (gimple_switch_index (stmt));
+      expr->kind = EXPR_SINGLE;
+      expr->ops.single.rhs = gimple_switch_index (stmt);
+    }
+  else if (code == GIMPLE_GOTO)
+    {
+      expr->type = TREE_TYPE (gimple_goto_dest (stmt));
+      expr->kind = EXPR_SINGLE;
+      expr->ops.single.rhs = gimple_goto_dest (stmt);
+    }
+  else
+    gcc_unreachable ();
+
+  element->lhs = lhs;
+  element->stmt = stmt;
+  element->hash = avail_expr_hash (element);
+  element->stamp = element;
+}
+
+/* Given a conditional expression COND as a tree, initialize
+   a hashable_expr expression EXPR.  The conditional must be a
+   comparison or logical negation.  A constant or a variable is
+   not permitted.  */
+
+static void
+initialize_expr_from_cond (tree cond, struct hashable_expr *expr)
 {
-  /* The highest and lowest values the variable in COND may contain when
-     COND is true.  Note this may not necessarily be the same values
-     tested by COND if the same variable was used in earlier conditionals. 
+  expr->type = boolean_type_node;
 
-     Note this is computed lazily and thus can be NULL indicating that
-     the values have not been computed yet.  */
-  tree low;
-  tree high;
+  if (COMPARISON_CLASS_P (cond))
+    {
+      expr->kind = EXPR_BINARY;
+      expr->ops.binary.op = TREE_CODE (cond);
+      expr->ops.binary.opnd0 = TREE_OPERAND (cond, 0);
+      expr->ops.binary.opnd1 = TREE_OPERAND (cond, 1);
+    }
+  else if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
+    {
+      expr->kind = EXPR_UNARY;
+      expr->ops.unary.op = TRUTH_NOT_EXPR;
+      expr->ops.unary.opnd = TREE_OPERAND (cond, 0);
+    }
+  else
+    gcc_unreachable ();
+}
 
-  /* The actual conditional we recorded.  This is needed since we compute
-     ranges lazily.  */
-  tree cond;
+/* Given a hashable_expr expression EXPR and an LHS,
+   initialize the hash table element pointed to by ELEMENT.  */
 
-  /* The basic block where this record was created.  We use this to determine
-     when to remove records.  */
-  basic_block bb;
-};
+static void
+initialize_hash_element_from_expr (struct hashable_expr *expr,
+                                   tree lhs,
+                                   struct expr_hash_elt *element)
+{
+  element->expr = *expr;
+  element->lhs = lhs;
+  element->stmt = NULL;
+  element->hash = avail_expr_hash (element);
+  element->stamp = element;
+}
 
-static struct opt_stats_d opt_stats;
+/* Compare two hashable_expr structures for equivalence.
+   They are considered equivalent when the the expressions
+   they denote must necessarily be equal.  The logic is intended
+   to follow that of operand_equal_p in fold-const.c  */
+
+static bool
+hashable_expr_equal_p (const struct hashable_expr *expr0,
+                        const struct hashable_expr *expr1)
+{
+  tree type0 = expr0->type;
+  tree type1 = expr1->type;
+
+  /* If either type is NULL, there is nothing to check.  */
+  if ((type0 == NULL_TREE) ^ (type1 == NULL_TREE))
+    return false;
+
+  /* If both types don't have the same signedness, precision, and mode,
+     then we can't consider  them equal.  */
+  if (type0 != type1
+      && (TREE_CODE (type0) == ERROR_MARK
+         || TREE_CODE (type1) == ERROR_MARK
+         || TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
+         || TYPE_PRECISION (type0) != TYPE_PRECISION (type1)
+         || TYPE_MODE (type0) != TYPE_MODE (type1)))
+    return false;
+
+  if (expr0->kind != expr1->kind)
+    return false;
+
+  switch (expr0->kind)
+    {
+    case EXPR_SINGLE:
+      return operand_equal_p (expr0->ops.single.rhs,
+                              expr1->ops.single.rhs, 0);
+
+    case EXPR_UNARY:
+      if (expr0->ops.unary.op != expr1->ops.unary.op)
+        return false;
+
+      if ((CONVERT_EXPR_CODE_P (expr0->ops.unary.op)
+           || expr0->ops.unary.op == NON_LVALUE_EXPR)
+          && TYPE_UNSIGNED (expr0->type) != TYPE_UNSIGNED (expr1->type))
+        return false;
+
+      return operand_equal_p (expr0->ops.unary.opnd,
+                              expr1->ops.unary.opnd, 0);
 
-/* A virtual array holding value range records for the variable identified
-   by the index, SSA_VERSION.  */
-static varray_type vrp_data;
+    case EXPR_BINARY:
+      {
+        if (expr0->ops.binary.op != expr1->ops.binary.op)
+          return false;
+
+        if (operand_equal_p (expr0->ops.binary.opnd0,
+                             expr1->ops.binary.opnd0, 0)
+            && operand_equal_p (expr0->ops.binary.opnd1,
+                                expr1->ops.binary.opnd1, 0))
+          return true;
+
+        /* For commutative ops, allow the other order.  */
+        return (commutative_tree_code (expr0->ops.binary.op)
+                && operand_equal_p (expr0->ops.binary.opnd0,
+                                    expr1->ops.binary.opnd1, 0)
+                && operand_equal_p (expr0->ops.binary.opnd1,
+                                    expr1->ops.binary.opnd0, 0));
+      }
+
+    case EXPR_CALL:
+      {
+        size_t i;
+
+        /* If the calls are to different functions, then they
+           clearly cannot be equal.  */
+        if (! operand_equal_p (expr0->ops.call.fn,
+                               expr1->ops.call.fn, 0))
+          return false;
+
+        if (! expr0->ops.call.pure)
+          return false;
+
+        if (expr0->ops.call.nargs !=  expr1->ops.call.nargs)
+          return false;
+
+        for (i = 0; i < expr0->ops.call.nargs; i++)
+          if (! operand_equal_p (expr0->ops.call.args[i],
+                                 expr1->ops.call.args[i], 0))
+            return false;
+
+        return true;
+      }
+
+    default:
+      gcc_unreachable ();
+    }
+}
 
-/* Datastructure for block local data used during the dominator walk.  
-   We maintain a stack of these as we recursively walk down the
-   dominator tree.  */
+/* Compute a hash value for a hashable_expr value EXPR and a
+   previously accumulated hash value VAL.  If two hashable_expr
+   values compare equal with hashable_expr_equal_p, they must
+   hash to the same value, given an identical value of VAL.
+   The logic is intended to follow iterative_hash_expr in tree.c.  */
 
-struct dom_walk_block_data
+static hashval_t
+iterative_hash_hashable_expr (const struct hashable_expr *expr, hashval_t val)
 {
-  /* Array of all the expressions entered into the global expression
-     hash table by this block.  During finalization we use this array to
-     know what expressions to remove from the global expression hash
-     table.  */
-  varray_type avail_exprs;
-
-  /* Array of dest, src pairs that need to be restored during finalization
-     into the global const/copies table during finalization.  */
-  varray_type const_and_copies;
-
-  /* Similarly for the nonzero state of variables that needs to be
-     restored during finalization.  */
-  varray_type nonzero_vars;
-
-  /* Array of statements we need to rescan during finalization for newly
-     exposed variables.  */
-  varray_type stmts_to_rescan;
-
-  /* Array of variables which have their values constrained by operations
-     in this basic block.  We use this during finalization to know
-     which variables need their VRP data updated.  */
-  varray_type vrp_variables;
-
-  /* Array of tree pairs used to restore the global currdefs to its
-     original state after completing optimization of a block and its
-     dominator children.  */
-  varray_type block_defs;
-};
+  switch (expr->kind)
+    {
+    case EXPR_SINGLE:
+      val = iterative_hash_expr (expr->ops.single.rhs, val);
+      break;
+
+    case EXPR_UNARY:
+      val = iterative_hash_object (expr->ops.unary.op, val);
+
+      /* Make sure to include signedness in the hash computation.
+         Don't hash the type, that can lead to having nodes which
+         compare equal according to operand_equal_p, but which
+         have different hash codes.  */
+      if (CONVERT_EXPR_CODE_P (expr->ops.unary.op)
+          || expr->ops.unary.op == NON_LVALUE_EXPR)
+        val += TYPE_UNSIGNED (expr->type);
+
+      val = iterative_hash_expr (expr->ops.unary.opnd, val);
+      break;
+
+    case EXPR_BINARY:
+      val = iterative_hash_object (expr->ops.binary.op, val);
+      if (commutative_tree_code (expr->ops.binary.op))
+          val = iterative_hash_exprs_commutative (expr->ops.binary.opnd0,
+                                                  expr->ops.binary.opnd1, val);
+      else
+        {
+          val = iterative_hash_expr (expr->ops.binary.opnd0, val);
+          val = iterative_hash_expr (expr->ops.binary.opnd1, val);
+        }
+      break;
+
+    case EXPR_CALL:
+      {
+        size_t i;
+        enum tree_code code = CALL_EXPR;
 
-struct eq_expr_value
+        val = iterative_hash_object (code, val);
+        val = iterative_hash_expr (expr->ops.call.fn, val);
+        for (i = 0; i < expr->ops.call.nargs; i++)
+          val = iterative_hash_expr (expr->ops.call.args[i], val);
+      }
+      break;
+
+    default:
+      gcc_unreachable ();
+    }
+
+  return val;
+}
+
+/* Print a diagnostic dump of an expression hash table entry.  */
+
+static void
+print_expr_hash_elt (FILE * stream, const struct expr_hash_elt *element)
 {
-  tree src;
-  tree dst;
-};
+  if (element->stmt)
+    fprintf (stream, "STMT ");
+  else
+    fprintf (stream, "COND ");
 
-/* Local functions.  */
-static void optimize_stmt (struct dom_walk_data *, 
-                          basic_block bb,
-                          block_stmt_iterator);
-static inline tree get_value_for (tree, varray_type table);
-static inline void set_value_for (tree, tree, varray_type table);
-static tree lookup_avail_expr (tree, varray_type *, bool);
-static struct eq_expr_value get_eq_expr_value (tree, int, varray_type *,
-                                              basic_block, varray_type *);
-static hashval_t avail_expr_hash (const void *);
-static hashval_t real_avail_expr_hash (const void *);
-static int avail_expr_eq (const void *, const void *);
-static void htab_statistics (FILE *, htab_t);
-static void record_cond (tree, tree, varray_type *);
-static void record_dominating_conditions (tree, varray_type *);
-static void record_const_or_copy (tree, tree, varray_type *);
-static void record_equality (tree, tree, varray_type *);
-static tree update_rhs_and_lookup_avail_expr (tree, tree, varray_type *, bool);
-static tree simplify_rhs_and_lookup_avail_expr (struct dom_walk_data *,
-                                               tree, int);
-static tree simplify_cond_and_lookup_avail_expr (tree, varray_type *,
-                                                stmt_ann_t, int);
-static tree simplify_switch_and_lookup_avail_expr (tree, varray_type *, int);
-static tree find_equivalent_equality_comparison (tree);
-static void record_range (tree, basic_block, varray_type *);
-static bool extract_range_from_cond (tree, tree *, tree *, int *);
-static void record_equivalences_from_phis (struct dom_walk_data *, basic_block);
-static void record_equivalences_from_incoming_edge (struct dom_walk_data *,
-                                                   basic_block);
-static bool eliminate_redundant_computations (struct dom_walk_data *,
-                                             tree, stmt_ann_t);
-static void record_equivalences_from_stmt (tree, varray_type *, varray_type *,
-                                          int, stmt_ann_t);
-static void thread_across_edge (struct dom_walk_data *, edge);
-static void dom_opt_finalize_block (struct dom_walk_data *, basic_block);
-static void dom_opt_initialize_block_local_data (struct dom_walk_data *,
-                                                basic_block, bool);
-static void dom_opt_initialize_block (struct dom_walk_data *, basic_block);
-static void cprop_into_phis (struct dom_walk_data *, basic_block);
-static void remove_local_expressions_from_table (varray_type locals,
-                                                unsigned limit,
-                                                htab_t table);
-static void restore_vars_to_original_value (varray_type locals,
-                                           unsigned limit, 
-                                           varray_type table);
-static void restore_currdefs_to_original_value (varray_type locals,
-                                               unsigned limit);
-static void register_definitions_for_stmt (tree, varray_type *);
-static edge single_incoming_edge_ignoring_loop_edges (basic_block);
+  if (element->lhs)
+    {
+      print_generic_expr (stream, element->lhs, 0);
+      fprintf (stream, " = ");
+    }
 
-/* Local version of fold that doesn't introduce cruft.  */
+  switch (element->expr.kind)
+    {
+      case EXPR_SINGLE:
+        print_generic_expr (stream, element->expr.ops.single.rhs, 0);
+        break;
+
+      case EXPR_UNARY:
+        fprintf (stream, "%s ", tree_code_name[element->expr.ops.unary.op]);
+        print_generic_expr (stream, element->expr.ops.unary.opnd, 0);
+        break;
+
+      case EXPR_BINARY:
+        print_generic_expr (stream, element->expr.ops.binary.opnd0, 0);
+        fprintf (stream, " %s ", tree_code_name[element->expr.ops.binary.op]);
+        print_generic_expr (stream, element->expr.ops.binary.opnd1, 0);
+        break;
+
+      case EXPR_CALL:
+        {
+          size_t i;
+          size_t nargs = element->expr.ops.call.nargs;
+
+          print_generic_expr (stream, element->expr.ops.call.fn, 0);
+          fprintf (stream, " (");
+          for (i = 0; i < nargs; i++)
+            {
+              print_generic_expr (stream, element->expr.ops.call.args[i], 0);
+              if (i + 1 < nargs)
+                fprintf (stream, ", ");
+            }
+          fprintf (stream, ")");
+        }
+        break;
+    }
+  fprintf (stream, "\n");
 
-static tree
-local_fold (tree t)
+  if (element->stmt)
+    {
+      fprintf (stream, "          ");
+      print_gimple_stmt (stream, element->stmt, 0, 0);
+    }
+}
+
+/* Delete an expr_hash_elt and reclaim its storage.  */
+
+static void
+free_expr_hash_elt (void *elt)
 {
-  t = fold (t);
+  struct expr_hash_elt *element = ((struct expr_hash_elt *)elt);
 
-  /* Strip away useless type conversions.  Both the NON_LVALUE_EXPR that
-     may have been added by fold, and "useless" type conversions that might
-     now be apparent due to propagation.  */
-  STRIP_USELESS_TYPE_CONVERSION (t);
+  if (element->expr.kind == EXPR_CALL)
+    free (element->expr.ops.call.args);
 
-  return t;
+  free (element);
 }
 
-/* Return the value associated with variable VAR in TABLE.  */
+/* Allocate an EDGE_INFO for edge E and attach it to E.
+   Return the new EDGE_INFO structure.  */
 
-static inline tree
-get_value_for (tree var, varray_type table)
+static struct edge_info *
+allocate_edge_info (edge e)
 {
-  return VARRAY_TREE (table, SSA_NAME_VERSION (var));
+  struct edge_info *edge_info;
+
+  edge_info = XCNEW (struct edge_info);
+
+  e->aux = edge_info;
+  return edge_info;
 }
 
-/* Associate VALUE to variable VAR in TABLE.  */
+/* Free all EDGE_INFO structures associated with edges in the CFG.
+   If a particular edge can be threaded, copy the redirection
+   target from the EDGE_INFO structure into the edge's AUX field
+   as required by code to update the CFG and SSA graph for
+   jump threading.  */
 
-static inline void
-set_value_for (tree var, tree value, varray_type table)
+static void
+free_all_edge_infos (void)
 {
-  VARRAY_TREE (table, SSA_NAME_VERSION (var)) = value;
+  basic_block bb;
+  edge_iterator ei;
+  edge e;
+
+  FOR_EACH_BB (bb)
+    {
+      FOR_EACH_EDGE (e, ei, bb->preds)
+        {
+        struct edge_info *edge_info = (struct edge_info *) e->aux;
+
+         if (edge_info)
+           {
+             if (edge_info->cond_equivalences)
+               free (edge_info->cond_equivalences);
+             free (edge_info);
+             e->aux = NULL;
+           }
+       }
+    }
 }
 
-/* Jump threading, redundancy elimination and const/copy propagation. 
+/* Jump threading, redundancy elimination and const/copy propagation.
 
    This pass may expose new symbols that need to be renamed into SSA.  For
    every new symbol exposed, its corresponding bit will be set in
    VARS_TO_RENAME.  */
 
-static void
+static unsigned int
 tree_ssa_dominator_optimize (void)
 {
   struct dom_walk_data walk_data;
-  unsigned int i;
 
-  for (i = 0; i < num_referenced_vars; i++)
-    var_ann (referenced_var (i))->current_def = NULL;
-
-  /* Mark loop edges so we avoid threading across loop boundaries.
-     This may result in transforming natural loop into irreducible
-     region.  */
-  mark_dfs_back_edges ();
+  memset (&opt_stats, 0, sizeof (opt_stats));
 
   /* Create our hash tables.  */
-  avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free);
-  VARRAY_TREE_INIT (const_and_copies, num_ssa_names, "const_and_copies");
-  nonzero_vars = BITMAP_XMALLOC ();
-  VARRAY_GENERIC_PTR_INIT (vrp_data, num_ssa_names, "vrp_data");
-  need_eh_cleanup = BITMAP_XMALLOC ();
+  avail_exprs = htab_create (1024, real_avail_expr_hash, avail_expr_eq, free_expr_hash_elt);
+  avail_exprs_stack = VEC_alloc (expr_hash_elt_t, heap, 20);
+  const_and_copies_stack = VEC_alloc (tree, heap, 20);
+  need_eh_cleanup = BITMAP_ALLOC (NULL);
 
   /* Setup callbacks for the generic dominator tree walker.  */
-  walk_data.walk_stmts_backward = false;
   walk_data.dom_direction = CDI_DOMINATORS;
-  walk_data.initialize_block_local_data = dom_opt_initialize_block_local_data;
-  walk_data.before_dom_children_before_stmts = dom_opt_initialize_block;
-  walk_data.before_dom_children_walk_stmts = optimize_stmt;
-  walk_data.before_dom_children_after_stmts = cprop_into_phis;
-  walk_data.after_dom_children_before_stmts = NULL;
-  walk_data.after_dom_children_walk_stmts = NULL;
-  walk_data.after_dom_children_after_stmts = dom_opt_finalize_block;
+  walk_data.initialize_block_local_data = NULL;
+  walk_data.before_dom_children = dom_opt_enter_block;
+  walk_data.after_dom_children = dom_opt_leave_block;
   /* Right now we only attach a dummy COND_EXPR to the global data pointer.
      When we attach more stuff we'll need to fill this out with a real
      structure.  */
   walk_data.global_data = NULL;
-  walk_data.block_local_data_size = sizeof (struct dom_walk_block_data);
+  walk_data.block_local_data_size = 0;
 
   /* Now initialize the dominator walker.  */
   init_walk_dominator_tree (&walk_data);
 
   calculate_dominance_info (CDI_DOMINATORS);
+  cfg_altered = false;
 
-  /* If we prove certain blocks are unreachable, then we want to
-     repeat the dominator optimization process as PHI nodes may
-     have turned into copies which allows better propagation of
-     values.  So we repeat until we do not identify any new unreachable
-     blocks.  */
-  do
-    {
-      /* Optimize the dominator tree.  */
-      cfg_altered = false;
-
-      /* Recursively walk the dominator tree optimizing statements.  */
-      walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
-
-      /* If we exposed any new variables, go ahead and put them into
-        SSA form now, before we handle jump threading.  This simplifies
-        interactions between rewriting of _DECL nodes into SSA form
-        and rewriting SSA_NAME nodes into SSA form after block
-        duplication and CFG manipulation.  */
-      if (bitmap_first_set_bit (vars_to_rename) >= 0)
-       {
-         rewrite_into_ssa (false);
-         bitmap_clear (vars_to_rename);
-       }
+  /* We need to know loop structures in order to avoid destroying them
+     in jump threading.  Note that we still can e.g. thread through loop
+     headers to an exit edge, or through loop header to the loop body, assuming
+     that we update the loop info.  */
+  loop_optimizer_init (LOOPS_HAVE_SIMPLE_LATCHES);
 
-      /* Thread jumps, creating duplicate blocks as needed.  */
-      cfg_altered = thread_through_all_blocks ();
+  /* Initialize the value-handle array.  */
+  threadedge_initialize_values ();
 
-      /* Removal of statements may make some EH edges dead.  Purge
-        such edges from the CFG as needed.  */
-      if (bitmap_first_set_bit (need_eh_cleanup) >= 0)
-       {
-         cfg_altered |= tree_purge_all_dead_eh_edges (need_eh_cleanup);
-         bitmap_zero (need_eh_cleanup);
-       }
+  /* We need accurate information regarding back edges in the CFG
+     for jump threading; this may include back edges that are not part of
+     a single loop.  */
+  mark_dfs_back_edges ();
+
+  /* Recursively walk the dominator tree optimizing statements.  */
+  walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
+
+  {
+    gimple_stmt_iterator gsi;
+    basic_block bb;
+    FOR_EACH_BB (bb)
+      {for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+         update_stmt_if_modified (gsi_stmt (gsi));
+      }
+  }
 
-      free_dominance_info (CDI_DOMINATORS);
-      cfg_altered = cleanup_tree_cfg ();
-      calculate_dominance_info (CDI_DOMINATORS);
+  /* If we exposed any new variables, go ahead and put them into
+     SSA form now, before we handle jump threading.  This simplifies
+     interactions between rewriting of _DECL nodes into SSA form
+     and rewriting SSA_NAME nodes into SSA form after block
+     duplication and CFG manipulation.  */
+  update_ssa (TODO_update_ssa);
 
-      rewrite_ssa_into_ssa ();
+  free_all_edge_infos ();
 
-      if (VARRAY_ACTIVE_SIZE (const_and_copies) <= num_ssa_names)
+  /* Thread jumps, creating duplicate blocks as needed.  */
+  cfg_altered |= thread_through_all_blocks (first_pass_instance);
+
+  if (cfg_altered)
+    free_dominance_info (CDI_DOMINATORS);
+
+  /* Removal of statements may make some EH edges dead.  Purge
+     such edges from the CFG as needed.  */
+  if (!bitmap_empty_p (need_eh_cleanup))
+    {
+      unsigned i;
+      bitmap_iterator bi;
+
+      /* Jump threading may have created forwarder blocks from blocks
+        needing EH cleanup; the new successor of these blocks, which
+        has inherited from the original block, needs the cleanup.  */
+      EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
        {
-         VARRAY_GROW (const_and_copies, num_ssa_names);
-         VARRAY_GROW (vrp_data, num_ssa_names);
+         basic_block bb = BASIC_BLOCK (i);
+         if (single_succ_p (bb) == 1
+             && (single_succ_edge (bb)->flags & EDGE_EH) == 0)
+           {
+             bitmap_clear_bit (need_eh_cleanup, i);
+             bitmap_set_bit (need_eh_cleanup, single_succ (bb)->index);
+           }
        }
 
-      /* Reinitialize the various tables.  */
-      bitmap_clear (nonzero_vars);
-      htab_empty (avail_exprs);
-      VARRAY_CLEAR (const_and_copies);
-      VARRAY_CLEAR (vrp_data);
-
-      for (i = 0; i < num_referenced_vars; i++)
-       var_ann (referenced_var (i))->current_def = NULL;
+      gimple_purge_all_dead_eh_edges (need_eh_cleanup);
+      bitmap_zero (need_eh_cleanup);
     }
-  while (cfg_altered);
+
+  statistics_counter_event (cfun, "Redundant expressions eliminated",
+                           opt_stats.num_re);
+  statistics_counter_event (cfun, "Constants propagated",
+                           opt_stats.num_const_prop);
+  statistics_counter_event (cfun, "Copies propagated",
+                           opt_stats.num_copy_prop);
 
   /* Debugging dumps.  */
   if (dump_file && (dump_flags & TDF_STATS))
     dump_dominator_optimization_stats (dump_file);
 
-  /* We emptied the hash table earlier, now delete it completely.  */
-  htab_delete (avail_exprs);
+  loop_optimizer_finalize ();
 
-  /* It is not necessary to clear CURRDEFS, REDIRECTION_EDGES, VRP_DATA,
-     CONST_AND_COPIES, and NONZERO_VARS as they all get cleared at the bottom
-     of the do-while loop above.  */
+  /* Delete our main hashtable.  */
+  htab_delete (avail_exprs);
 
   /* And finalize the dominator walker.  */
   fini_walk_dominator_tree (&walk_data);
 
-  /* Free nonzero_vars.   */
-  BITMAP_XFREE (nonzero_vars);
-  BITMAP_XFREE (need_eh_cleanup);
+  /* Free asserted bitmaps and stacks.  */
+  BITMAP_FREE (need_eh_cleanup);
+
+  VEC_free (expr_hash_elt_t, heap, avail_exprs_stack);
+  VEC_free (tree, heap, const_and_copies_stack);
+
+  /* Free the value-handle array.  */
+  threadedge_finalize_values ();
+  ssa_name_values = NULL;
+
+  return 0;
 }
 
 static bool
@@ -418,8 +737,10 @@ gate_dominator (void)
   return flag_tree_dom != 0;
 }
 
-struct tree_opt_pass pass_dominator = 
+struct gimple_opt_pass pass_dominator =
 {
+ {
+  GIMPLE_PASS,
   "dom",                               /* name */
   gate_dominator,                      /* gate */
   tree_ssa_dominator_optimize,         /* execute */
@@ -427,680 +748,193 @@ struct tree_opt_pass pass_dominator =
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
   TV_TREE_SSA_DOMINATOR_OPTS,          /* tv_id */
-  PROP_cfg | PROP_ssa | PROP_alias,    /* properties_required */
+  PROP_cfg | PROP_ssa,                 /* properties_required */
   0,                                   /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */
-  TODO_dump_func | TODO_rename_vars
+  TODO_dump_func
+    | TODO_update_ssa
+    | TODO_cleanup_cfg
     | TODO_verify_ssa                  /* todo_flags_finish */
+ }
 };
 
 
-/* We are exiting BB, see if the target block begins with a conditional
-   jump which has a known value when reached via BB.  */
+/* Given a conditional statement CONDSTMT, convert the
+   condition to a canonical form.  */
 
 static void
-thread_across_edge (struct dom_walk_data *walk_data, edge e)
+canonicalize_comparison (gimple condstmt)
 {
-  struct dom_walk_block_data *bd
-    = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
-  block_stmt_iterator bsi;
-  tree stmt = NULL;
-  tree phi;
-
-  /* Each PHI creates a temporary equivalence, record them.  */
-  for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
-    {
-      tree src = PHI_ARG_DEF_FROM_EDGE (phi, e);
-      tree dst = PHI_RESULT (phi);
-      record_const_or_copy (dst, src, &bd->const_and_copies);
-      register_new_def (dst, &bd->block_defs);
-    }
-
-  for (bsi = bsi_start (e->dest); ! bsi_end_p (bsi); bsi_next (&bsi))
-    {
-      tree lhs, cached_lhs;
-
-      stmt = bsi_stmt (bsi);
-
-      /* Ignore empty statements and labels.  */
-      if (IS_EMPTY_STMT (stmt) || TREE_CODE (stmt) == LABEL_EXPR)
-       continue;
-
-      /* If this is not a MODIFY_EXPR which sets an SSA_NAME to a new
-        value, then stop our search here.  Ideally when we stop a
-        search we stop on a COND_EXPR or SWITCH_EXPR.  */
-      if (TREE_CODE (stmt) != MODIFY_EXPR
-         || TREE_CODE (TREE_OPERAND (stmt, 0)) != SSA_NAME)
-       break;
-
-      /* At this point we have a statement which assigns an RHS to an
-        SSA_VAR on the LHS.  We want to prove that the RHS is already
-        available and that its value is held in the current definition
-        of the LHS -- meaning that this assignment is a NOP when
-        reached via edge E.  */
-      if (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME)
-       cached_lhs = TREE_OPERAND (stmt, 1);
-      else
-       cached_lhs = lookup_avail_expr (stmt, NULL, false);
-
-      lhs = TREE_OPERAND (stmt, 0);
-
-      /* This can happen if we thread around to the start of a loop.  */
-      if (lhs == cached_lhs)
-       break;
-
-      /* If we did not find RHS in the hash table, then try again after
-        temporarily const/copy propagating the operands.  */
-      if (!cached_lhs)
-       {
-         /* Copy the operands.  */
-         stmt_ann_t ann = stmt_ann (stmt);
-         use_optype uses = USE_OPS (ann);
-         vuse_optype vuses = VUSE_OPS (ann);
-         tree *uses_copy = xcalloc (NUM_USES (uses),  sizeof (tree));
-         tree *vuses_copy = xcalloc (NUM_VUSES (vuses), sizeof (tree));
-         unsigned int i;
-
-         /* Make a copy of the uses into USES_COPY, then cprop into
-            the use operands.  */
-         for (i = 0; i < NUM_USES (uses); i++)
-           {
-             tree tmp = NULL;
-
-             uses_copy[i] = USE_OP (uses, i);
-             if (TREE_CODE (USE_OP (uses, i)) == SSA_NAME)
-               tmp = get_value_for (USE_OP (uses, i), const_and_copies);
-             if (tmp)
-               SET_USE_OP (uses, i, tmp);
-           }
-
-         /* Similarly for virtual uses.  */
-         for (i = 0; i < NUM_VUSES (vuses); i++)
-           {
-             tree tmp = NULL;
+  tree op0;
+  tree op1;
+  enum tree_code code;
 
-             vuses_copy[i] = VUSE_OP (vuses, i);
-             if (TREE_CODE (VUSE_OP (vuses, i)) == SSA_NAME)
-               tmp = get_value_for (VUSE_OP (vuses, i), const_and_copies);
-             if (tmp)
-               SET_VUSE_OP (vuses, i, tmp);
-           }
+  gcc_assert (gimple_code (condstmt) == GIMPLE_COND);
 
-         /* Try to lookup the new expression.  */
-         cached_lhs = lookup_avail_expr (stmt, NULL, false);
+  op0 = gimple_cond_lhs (condstmt);
+  op1 = gimple_cond_rhs (condstmt);
 
-         /* Restore the statement's original uses/defs.  */
-         for (i = 0; i < NUM_USES (uses); i++)
-           SET_USE_OP (uses, i, uses_copy[i]);
+  code = gimple_cond_code (condstmt);
 
-         for (i = 0; i < NUM_VUSES (vuses); i++)
-           SET_VUSE_OP (vuses, i, vuses_copy[i]);
+  /* If it would be profitable to swap the operands, then do so to
+     canonicalize the statement, enabling better optimization.
 
-         free (uses_copy);
-         free (vuses_copy);
-
-         /* If we still did not find the expression in the hash table,
-            then we can not ignore this statement.  */
-         if (! cached_lhs)
-           break;
-       }
-
-      /* If the expression in the hash table was not assigned to an
-        SSA_NAME, then we can not ignore this statement.  */
-      if (TREE_CODE (cached_lhs) != SSA_NAME)
-       break;
-
-      /* If we have different underlying variables, then we can not
-        ignore this statement.  */
-      if (SSA_NAME_VAR (cached_lhs) != SSA_NAME_VAR (lhs))
-       break;
-
-      /* If CACHED_LHS does not represent the current value of the undering
-        variable in CACHED_LHS/LHS, then we can not ignore this statement.  */
-      if (var_ann (SSA_NAME_VAR (lhs))->current_def != cached_lhs)
-       break;
-
-      /* If we got here, then we can ignore this statement and continue
-        walking through the statements in the block looking for a threadable
-        COND_EXPR.
-
-        We want to record an equivalence lhs = cache_lhs so that if
-        the result of this statement is used later we can copy propagate
-        suitably.  */
-      record_const_or_copy (lhs, cached_lhs, &bd->const_and_copies);
-      register_new_def (lhs, &bd->block_defs);
-    }
-
-  /* If we stopped at a COND_EXPR or SWITCH_EXPR, then see if we know which
-     arm will be taken.  */
-  if (stmt
-      && (TREE_CODE (stmt) == COND_EXPR
-         || TREE_CODE (stmt) == SWITCH_EXPR))
+     By placing canonicalization of such expressions here we
+     transparently keep statements in canonical form, even
+     when the statement is modified.  */
+  if (tree_swap_operands_p (op0, op1, false))
     {
-      tree cond, cached_lhs;
-      edge e1;
-
-      /* Do not forward entry edges into the loop.  In the case loop
-        has multiple entry edges we may end up in constructing irreducible
-        region.  
-        ??? We may consider forwarding the edges in the case all incoming
-        edges forward to the same destination block.  */
-      if (!e->flags & EDGE_DFS_BACK)
-       {
-         for (e1 = e->dest->pred; e; e = e->pred_next)
-           if (e1->flags & EDGE_DFS_BACK)
-             break;
-         if (e1)
-           return;
-       }
-
-      /* Now temporarily cprop the operands and try to find the resulting
-        expression in the hash tables.  */
-      if (TREE_CODE (stmt) == COND_EXPR)
-       cond = COND_EXPR_COND (stmt);
-      else
-       cond = SWITCH_COND (stmt);
-
-      if (TREE_CODE_CLASS (TREE_CODE (cond)) == '<')
-       {
-         tree dummy_cond, op0, op1;
-         enum tree_code cond_code;
-
-         op0 = TREE_OPERAND (cond, 0);
-         op1 = TREE_OPERAND (cond, 1);
-         cond_code = TREE_CODE (cond);
-
-         /* Get the current value of both operands.  */
-         if (TREE_CODE (op0) == SSA_NAME)
-           {
-             tree tmp = get_value_for (op0, const_and_copies);
-             if (tmp)
-               op0 = tmp;
-           }
-
-         if (TREE_CODE (op1) == SSA_NAME)
-           {
-             tree tmp = get_value_for (op1, const_and_copies);
-             if (tmp)
-               op1 = tmp;
-           }
-
-         /* Stuff the operator and operands into our dummy conditional
-            expression, creating the dummy conditional if necessary.  */
-         dummy_cond = walk_data->global_data;
-         if (! dummy_cond)
-           {
-             dummy_cond = build (cond_code, boolean_type_node, op0, op1);
-             dummy_cond = build (COND_EXPR, void_type_node,
-                                 dummy_cond, NULL, NULL);
-             walk_data->global_data = dummy_cond;
-           }
-         else
-           {
-             TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), cond_code);
-             TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op0;
-             TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1) = op1;
-           }
-
-         /* If the conditional folds to an invariant, then we are done,
-            otherwise look it up in the hash tables.  */
-         cached_lhs = local_fold (COND_EXPR_COND (dummy_cond));
-         if (! is_gimple_min_invariant (cached_lhs))
-           cached_lhs = lookup_avail_expr (dummy_cond, NULL, false);
-         if (!cached_lhs || ! is_gimple_min_invariant (cached_lhs))
-           {
-             cached_lhs = simplify_cond_and_lookup_avail_expr (dummy_cond,
-                                                               NULL,
-                                                               NULL,
-                                                               false);
-           }
-       }
-      /* We can have conditionals which just test the state of a
-        variable rather than use a relational operator.  These are
-        simpler to handle.  */
-      else if (TREE_CODE (cond) == SSA_NAME)
-       {
-         cached_lhs = cond;
-         cached_lhs = get_value_for (cached_lhs, const_and_copies);
-         if (cached_lhs && ! is_gimple_min_invariant (cached_lhs))
-           cached_lhs = 0;
-       }
-      else
-       cached_lhs = lookup_avail_expr (stmt, NULL, false);
-
-      if (cached_lhs)
+      /* For relationals we need to swap the operands
+        and change the code.  */
+      if (code == LT_EXPR
+         || code == GT_EXPR
+         || code == LE_EXPR
+         || code == GE_EXPR)
        {
-         edge taken_edge = find_taken_edge (e->dest, cached_lhs);
-         basic_block dest = (taken_edge ? taken_edge->dest : NULL);
+          code = swap_tree_comparison (code);
 
-         if (dest == e->dest)
-           return;
+          gimple_cond_set_code (condstmt, code);
+          gimple_cond_set_lhs (condstmt, op1);
+          gimple_cond_set_rhs (condstmt, op0);
 
-         /* If we have a known destination for the conditional, then
-            we can perform this optimization, which saves at least one
-            conditional jump each time it applies since we get to
-            bypass the conditional at our original destination.   */
-         if (dest)
-           {
-             e->aux = taken_edge;
-             bb_ann (e->dest)->incoming_edge_threaded = true;
-           }
+          update_stmt (condstmt);
        }
     }
 }
 
-
-/* Initialize the local stacks.
-     
-   AVAIL_EXPRS stores all the expressions made available in this block.
-
-   CONST_AND_COPIES stores var/value pairs to restore at the end of this
-   block.
-
-   NONZERO_VARS stores the vars which have a nonzero value made in this
-   block.
-
-   STMTS_TO_RESCAN is a list of statements we will rescan for operands.
-
-   VRP_VARIABLES is the list of variables which have had their values
-   constrained by an operation in this block.
-
-   These stacks are cleared in the finalization routine run for each
-   block.  */
-
-static void
-dom_opt_initialize_block_local_data (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
-                                    basic_block bb ATTRIBUTE_UNUSED,
-                                    bool recycled ATTRIBUTE_UNUSED)
-{
-#ifdef ENABLE_CHECKING
-  struct dom_walk_block_data *bd
-    = (struct dom_walk_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
-
-  /* We get cleared memory from the allocator, so if the memory is not
-     cleared, then we are re-using a previously allocated entry.  In
-     that case, we can also re-use the underlying virtual arrays.  Just
-     make sure we clear them before using them!  */
-  if (recycled)
-    {
-      if (bd->avail_exprs && VARRAY_ACTIVE_SIZE (bd->avail_exprs) > 0)
-       abort ();
-      if (bd->const_and_copies && VARRAY_ACTIVE_SIZE (bd->const_and_copies) > 0)
-       abort ();
-      if (bd->nonzero_vars && VARRAY_ACTIVE_SIZE (bd->nonzero_vars) > 0)
-       abort ();
-      if (bd->stmts_to_rescan && VARRAY_ACTIVE_SIZE (bd->stmts_to_rescan) > 0)
-       abort ();
-      if (bd->vrp_variables && VARRAY_ACTIVE_SIZE (bd->vrp_variables) > 0)
-       abort ();
-      if (bd->block_defs && VARRAY_ACTIVE_SIZE (bd->block_defs) > 0)
-       abort ();
-    }
-#endif
-}
-
 /* Initialize local stacks for this optimizer and record equivalences
    upon entry to BB.  Equivalences can come from the edge traversed to
    reach BB or they may come from PHI nodes at the start of BB.  */
 
-static void
-dom_opt_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
-{
-  if (dump_file && (dump_flags & TDF_DETAILS))
-    fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
-
-  record_equivalences_from_incoming_edge (walk_data, bb);
-
-  /* PHI nodes can create equivalences too.  */
-  record_equivalences_from_phis (walk_data, bb);
-}
-
-/* Given an expression EXPR (a relational expression or a statement), 
-   initialize the hash table element pointed by by ELEMENT.  */
-
-static void
-initialize_hash_element (tree expr, tree lhs, struct expr_hash_elt *element)
-{
-  /* Hash table elements may be based on conditional expressions or statements.
-
-     For the former case, we have no annotation and we want to hash the
-     conditional expression.  In the latter case we have an annotation and
-     we want to record the expression the statement evaluates.  */
-  if (TREE_CODE_CLASS (TREE_CODE (expr)) == '<'
-      || TREE_CODE (expr) == TRUTH_NOT_EXPR)
-    {
-      element->ann = NULL;
-      element->rhs = expr;
-    }
-  else if (TREE_CODE (expr) == COND_EXPR)
-    {
-      element->ann = stmt_ann (expr);
-      element->rhs = COND_EXPR_COND (expr);
-    }
-  else if (TREE_CODE (expr) == SWITCH_EXPR)
-    {
-      element->ann = stmt_ann (expr);
-      element->rhs = SWITCH_COND (expr);
-    }
-  else if (TREE_CODE (expr) == RETURN_EXPR && TREE_OPERAND (expr, 0))
-    {
-      element->ann = stmt_ann (expr);
-      element->rhs = TREE_OPERAND (TREE_OPERAND (expr, 0), 1);
-    }
-  else
-    {
-      element->ann = stmt_ann (expr);
-      element->rhs = TREE_OPERAND (expr, 1);
-    }
-
-  element->lhs = lhs;
-  element->hash = avail_expr_hash (element);
-}
-
 /* Remove all the expressions in LOCALS from TABLE, stopping when there are
    LIMIT entries left in LOCALs.  */
 
 static void
-remove_local_expressions_from_table (varray_type locals,
-                                    unsigned limit,
-                                    htab_t table)
+remove_local_expressions_from_table (void)
 {
-  if (! locals)
-    return;
-
   /* Remove all the expressions made available in this block.  */
-  while (VARRAY_ACTIVE_SIZE (locals) > limit)
+  while (VEC_length (expr_hash_elt_t, avail_exprs_stack) > 0)
     {
-      struct expr_hash_elt element;
-      tree expr = VARRAY_TOP_TREE (locals);
-      VARRAY_POP (locals);
-
-      initialize_hash_element (expr, NULL, &element);
-      htab_remove_elt_with_hash (table, &element, element.hash);
-    }
-}
+      expr_hash_elt_t victim = VEC_pop (expr_hash_elt_t, avail_exprs_stack);
+      void **slot;
 
-/* Use the SSA_NAMES in LOCALS to restore TABLE to its original
-   state, stopping when there are LIMIT entries left in LOCALs.  */
-
-static void
-restore_nonzero_vars_to_original_value (varray_type locals,
-                                       unsigned limit,
-                                       bitmap table)
-{
-  if (!locals)
-    return;
+      if (victim == NULL)
+       break;
 
-  while (VARRAY_ACTIVE_SIZE (locals) > limit)
-    {
-      tree name = VARRAY_TOP_TREE (locals);
-      VARRAY_POP (locals);
-      bitmap_clear_bit (table, SSA_NAME_VERSION (name));
+      /* This must precede the actual removal from the hash table,
+         as ELEMENT and the table entry may share a call argument
+         vector which will be freed during removal.  */
+      if (dump_file && (dump_flags & TDF_DETAILS))
+        {
+          fprintf (dump_file, "<<<< ");
+          print_expr_hash_elt (dump_file, victim);
+        }
+
+      slot = htab_find_slot_with_hash (avail_exprs,
+                                      victim, victim->hash, NO_INSERT);
+      gcc_assert (slot && *slot == (void *) victim);
+      htab_clear_slot (avail_exprs, slot);
     }
 }
 
-/* Use the source/dest pairs in LOCALS to restore TABLE to its original
-   state, stopping when there are LIMIT entries left in LOCALs.  */
+/* Use the source/dest pairs in CONST_AND_COPIES_STACK to restore
+   CONST_AND_COPIES to its original state, stopping when we hit a
+   NULL marker.  */
 
 static void
-restore_vars_to_original_value (varray_type locals,
-                               unsigned limit,
-                               varray_type table)
+restore_vars_to_original_value (void)
 {
-  if (! locals)
-    return;
-
-  while (VARRAY_ACTIVE_SIZE (locals) > limit)
+  while (VEC_length (tree, const_and_copies_stack) > 0)
     {
       tree prev_value, dest;
 
-      prev_value = VARRAY_TOP_TREE (locals);
-      VARRAY_POP (locals);
-      dest = VARRAY_TOP_TREE (locals);
-      VARRAY_POP (locals);
+      dest = VEC_pop (tree, const_and_copies_stack);
 
-      set_value_for (dest, prev_value, table);
-    }
-}
-
-/* Similar to restore_vars_to_original_value, except that it restores 
-   CURRDEFS to its original value.  */
-static void
-restore_currdefs_to_original_value (varray_type locals, unsigned limit)
-{
-  if (!locals)
-    return;
-
-  /* Restore CURRDEFS to its original state.  */
-  while (VARRAY_ACTIVE_SIZE (locals) > limit)
-    {
-      tree tmp = VARRAY_TOP_TREE (locals);
-      tree saved_def, var;
-
-      VARRAY_POP (locals);
+      if (dest == NULL)
+       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
+      if (dump_file && (dump_flags & TDF_DETAILS))
        {
-         saved_def = NULL;
-         var = tmp;
+         fprintf (dump_file, "<<<< COPY ");
+         print_generic_expr (dump_file, dest, 0);
+         fprintf (dump_file, " = ");
+         print_generic_expr (dump_file, SSA_NAME_VALUE (dest), 0);
+         fprintf (dump_file, "\n");
        }
-                                                                                
-      var_ann (var)->current_def = saved_def;
+
+      prev_value = VEC_pop (tree, const_and_copies_stack);
+      set_ssa_name_value (dest, prev_value);
     }
 }
 
-/* We have finished processing the dominator children of BB, perform
-   any finalization actions in preparation for leaving this node in
-   the dominator tree.  */
-
-static void
-dom_opt_finalize_block (struct dom_walk_data *walk_data, basic_block bb)
+/* A trivial wrapper so that we can present the generic jump
+   threading code with a simple API for simplifying statements.  */
+static tree
+simplify_stmt_for_jump_threading (gimple stmt,
+                                 gimple within_stmt ATTRIBUTE_UNUSED)
 {
-  struct dom_walk_block_data *bd
-    = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
-  tree last;
-
-  /* If we are at a leaf node in the dominator graph, see if we can thread
-     the edge from BB through its successor.
-
-     Do this before we remove entries from our equivalence tables.  */
-  if (bb->succ
-      && ! bb->succ->succ_next
-      && (bb->succ->flags & EDGE_ABNORMAL) == 0
-      && (get_immediate_dominator (CDI_DOMINATORS, bb->succ->dest) != bb
-         || phi_nodes (bb->succ->dest)))
-       
-    {
-      thread_across_edge (walk_data, bb->succ);
-    }
-  else if ((last = last_stmt (bb))
-          && TREE_CODE (last) == COND_EXPR
-          && (TREE_CODE_CLASS (TREE_CODE (COND_EXPR_COND (last))) == '<'
-              || TREE_CODE (COND_EXPR_COND (last)) == SSA_NAME)
-          && bb->succ
-          && (bb->succ->flags & EDGE_ABNORMAL) == 0
-          && bb->succ->succ_next
-          && (bb->succ->succ_next->flags & EDGE_ABNORMAL) == 0
-          && ! bb->succ->succ_next->succ_next)
-    {
-      edge true_edge, false_edge;
-      tree cond, inverted = NULL;
-      enum tree_code cond_code;
-
-      extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
-
-      cond = COND_EXPR_COND (last);
-      cond_code = TREE_CODE (cond);
-
-      if (TREE_CODE_CLASS (cond_code) == '<')
-       inverted = invert_truthvalue (cond);
-
-      /* If the THEN arm is the end of a dominator tree or has PHI nodes,
-        then try to thread through its edge.  */
-      if (get_immediate_dominator (CDI_DOMINATORS, true_edge->dest) != bb
-         || phi_nodes (true_edge->dest))
-       {
-         unsigned avail_expr_limit;
-         unsigned const_and_copies_limit;
-         unsigned currdefs_limit;
-
-         avail_expr_limit
-           = bd->avail_exprs ? VARRAY_ACTIVE_SIZE (bd->avail_exprs) : 0;
-         const_and_copies_limit
-           = bd->const_and_copies ? VARRAY_ACTIVE_SIZE (bd->const_and_copies)
-                                  : 0;
-         currdefs_limit
-           = bd->block_defs ? VARRAY_ACTIVE_SIZE (bd->block_defs) : 0;
-
-         /* Record any equivalences created by following this edge.  */
-         if (TREE_CODE_CLASS (cond_code) == '<')
-           {
-             record_cond (cond, boolean_true_node, &bd->avail_exprs);
-             record_dominating_conditions (cond, &bd->avail_exprs);
-             record_cond (inverted, boolean_false_node, &bd->avail_exprs);
-           }
-         else if (cond_code == SSA_NAME)
-           record_const_or_copy (cond, boolean_true_node,
-                                 &bd->const_and_copies);
-
-         /* Now thread the edge.  */
-         thread_across_edge (walk_data, true_edge);
-
-         /* And restore the various tables to their state before
-            we threaded this edge.  */
-         remove_local_expressions_from_table (bd->avail_exprs,
-                                              avail_expr_limit,
-                                              avail_exprs);
-         restore_vars_to_original_value (bd->const_and_copies,
-                                         const_and_copies_limit,
-                                         const_and_copies);
-         restore_currdefs_to_original_value (bd->block_defs, currdefs_limit);
-       }
-
-      /* Similarly for the ELSE arm.  */
-      if (get_immediate_dominator (CDI_DOMINATORS, false_edge->dest) != bb
-         || phi_nodes (false_edge->dest))
-       {
-         /* Record any equivalences created by following this edge.  */
-         if (TREE_CODE_CLASS (cond_code) == '<')
-           {
-             record_cond (cond, boolean_false_node, &bd->avail_exprs);
-             record_cond (inverted, boolean_true_node, &bd->avail_exprs);
-             record_dominating_conditions (inverted, &bd->avail_exprs);
-           }
-         else if (cond_code == SSA_NAME)
-           record_const_or_copy (cond, boolean_false_node,
-                                 &bd->const_and_copies);
-
-         thread_across_edge (walk_data, false_edge);
-
-         /* No need to remove local expressions from our tables
-            or restore vars to their original value as that will
-            be done immediately below.  */
-       }
-    }
-
-  remove_local_expressions_from_table (bd->avail_exprs, 0, avail_exprs);
-  restore_nonzero_vars_to_original_value (bd->nonzero_vars, 0, nonzero_vars);
-  restore_vars_to_original_value (bd->const_and_copies, 0, const_and_copies);
-  restore_currdefs_to_original_value (bd->block_defs, 0);
-
-  /* Remove VRP records associated with this basic block.  They are no
-     longer valid.
-
-     To be efficient, we note which variables have had their values
-     constrained in this block.  So walk over each variable in the
-     VRP_VARIABLEs array.  */
-  while (bd->vrp_variables && VARRAY_ACTIVE_SIZE (bd->vrp_variables) > 0)
-    {
-      tree var = VARRAY_TOP_TREE (bd->vrp_variables);
-
-      /* Each variable has a stack of value range records.  We want to
-        invalidate those associated with our basic block.  So we walk
-        the array backwards popping off records associated with our
-        block.  Once we hit a record not associated with our block
-        we are done.  */
-      varray_type var_vrp_records = VARRAY_GENERIC_PTR (vrp_data,
-                                                       SSA_NAME_VERSION (var));
-
-      while (VARRAY_ACTIVE_SIZE (var_vrp_records) > 0)
-       {
-         struct vrp_element *element
-           = (struct vrp_element *)VARRAY_TOP_GENERIC_PTR (var_vrp_records);
-
-         if (element->bb != bb)
-           break;
-  
-         VARRAY_POP (var_vrp_records);
-       }
+  return lookup_avail_expr (stmt, false);
+}
 
-      VARRAY_POP (bd->vrp_variables);
-    }
+/* Wrapper for common code to attempt to thread an edge.  For example,
+   it handles lazily building the dummy condition and the bookkeeping
+   when jump threading is successful.  */
 
-  /* Re-scan operands in all statements that may have had new symbols
-     exposed.  */
-  while (bd->stmts_to_rescan && VARRAY_ACTIVE_SIZE (bd->stmts_to_rescan) > 0)
-    {
-      tree stmt = VARRAY_TOP_TREE (bd->stmts_to_rescan);
-      VARRAY_POP (bd->stmts_to_rescan);
-      mark_new_vars_to_rename (stmt, vars_to_rename);
-    }
+static void
+dom_thread_across_edge (struct dom_walk_data *walk_data, edge e)
+{
+  if (! walk_data->global_data)
+  {
+    gimple dummy_cond =
+        gimple_build_cond (NE_EXPR,
+                           integer_zero_node, integer_zero_node,
+                           NULL, NULL);
+    walk_data->global_data = dummy_cond;
+  }
+
+  thread_across_edge ((gimple) walk_data->global_data, e, false,
+                     &const_and_copies_stack,
+                     simplify_stmt_for_jump_threading);
 }
 
 /* PHI nodes can create equivalences too.
 
    Ignoring any alternatives which are the same as the result, if
    all the alternatives are equal, then the PHI node creates an
-   equivalence.
-
-   Additionally, if all the PHI alternatives are known to have a nonzero
-   value, then the result of this PHI is known to have a nonzero value,
-   even if we do not know its exact value.  */
+   equivalence.  */
 
 static void
-record_equivalences_from_phis (struct dom_walk_data *walk_data, basic_block bb)
+record_equivalences_from_phis (basic_block bb)
 {
-  struct dom_walk_block_data *bd
-    = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
-  tree phi;
+  gimple_stmt_iterator gsi;
 
-  for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
+  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     {
-      tree lhs = PHI_RESULT (phi);
+      gimple phi = gsi_stmt (gsi);
+
+      tree lhs = gimple_phi_result (phi);
       tree rhs = NULL;
-      int i;
+      size_t i;
 
-      for (i = 0; i < PHI_NUM_ARGS (phi); i++)
+      for (i = 0; i < gimple_phi_num_args (phi); i++)
        {
-         tree t = PHI_ARG_DEF (phi, i);
+         tree t = gimple_phi_arg_def (phi, i);
 
-         if (TREE_CODE (t) == SSA_NAME || is_gimple_min_invariant (t))
-           {
-             /* Ignore alternatives which are the same as our LHS.  */
-             if (operand_equal_p (lhs, t, 0))
-               continue;
-
-             /* If we have not processed an alternative yet, then set
-                RHS to this alternative.  */
-             if (rhs == NULL)
-               rhs = t;
-             /* If we have processed an alternative (stored in RHS), then
-                see if it is equal to this one.  If it isn't, then stop
-                the search.  */
-             else if (! operand_equal_p (rhs, t, 0))
-               break;
-           }
-         else
+         /* Ignore alternatives which are the same as our LHS.  Since
+            LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
+            can simply compare pointers.  */
+         if (lhs == t)
+           continue;
+
+         /* If we have not processed an alternative yet, then set
+            RHS to this alternative.  */
+         if (rhs == NULL)
+           rhs = t;
+         /* If we have processed an alternative (stored in RHS), then
+            see if it is equal to this one.  If it isn't, then stop
+            the search.  */
+         else if (! operand_equal_for_phi_arg_p (rhs, t))
            break;
        }
 
@@ -1115,22 +949,8 @@ record_equivalences_from_phis (struct dom_walk_data *walk_data, basic_block bb)
         this, since this is a true assignment and not an equivalence
         inferred from a comparison.  All uses of this ssa name are dominated
         by this assignment, so unwinding just costs time and space.  */
-      if (i == PHI_NUM_ARGS (phi)
-         && may_propagate_copy (lhs, rhs))
-       set_value_for (lhs, rhs, const_and_copies);
-
-      /* Now see if we know anything about the nonzero property for the
-        result of this PHI.  */
-      for (i = 0; i < PHI_NUM_ARGS (phi); i++)
-       {
-         if (!PHI_ARG_NONZERO (phi, i))
-           break;
-       }
-
-      if (i == PHI_NUM_ARGS (phi))
-       bitmap_set_bit (nonzero_vars, SSA_NAME_VERSION (PHI_RESULT (phi)));
-
-      register_new_def (lhs, &bd->block_defs);
+      if (i == gimple_phi_num_args (phi) && may_propagate_copy (lhs, rhs))
+       set_ssa_name_value (lhs, rhs);
     }
 }
 
@@ -1141,8 +961,9 @@ single_incoming_edge_ignoring_loop_edges (basic_block bb)
 {
   edge retval = NULL;
   edge e;
+  edge_iterator ei;
 
-  for (e = bb->pred; e; e = e->pred_next)
+  FOR_EACH_EDGE (e, ei, bb->preds)
     {
       /* A loop back edge can be identified by the destination of
         the edge dominating the source of the edge.  */
@@ -1166,115 +987,41 @@ single_incoming_edge_ignoring_loop_edges (basic_block bb)
    has more than one incoming edge, then no equivalence is created.  */
 
 static void
-record_equivalences_from_incoming_edge (struct dom_walk_data *walk_data,
-                                       basic_block bb)
+record_equivalences_from_incoming_edge (basic_block bb)
 {
-  int edge_flags;
+  edge e;
   basic_block parent;
-  struct eq_expr_value eq_expr_value;
-  tree parent_block_last_stmt = NULL;
-  struct dom_walk_block_data *bd
-    = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
+  struct edge_info *edge_info;
 
-  /* If our parent block ended with a control statment, then we may be
+  /* If our parent block ended with a control statement, then we may be
      able to record some equivalences based on which outgoing edge from
      the parent was followed.  */
   parent = get_immediate_dominator (CDI_DOMINATORS, bb);
-  if (parent)
-    {
-      parent_block_last_stmt = last_stmt (parent);
-      if (parent_block_last_stmt && !is_ctrl_stmt (parent_block_last_stmt))
-       parent_block_last_stmt = NULL;
-    }
 
-  eq_expr_value.src = NULL;
-  eq_expr_value.dst = NULL;
+  e = single_incoming_edge_ignoring_loop_edges (bb);
 
-  /* If we have a single predecessor (ignoring loop backedges), then extract
-     EDGE_FLAGS from the single incoming edge.  Otherwise just return as
-     there is nothing to do.  */
-  if (bb->pred
-      && parent_block_last_stmt)
+  /* If we had a single incoming edge from our parent block, then enter
+     any data associated with the edge into our tables.  */
+  if (e && e->src == parent)
     {
-      edge e = single_incoming_edge_ignoring_loop_edges (bb);
-      if (e && bb_for_stmt (parent_block_last_stmt) == e->src)
-       edge_flags = e->flags;
-      else
-       return;
-    }
-  else
-    return;
+      unsigned int i;
 
-  /* If our parent block ended in a COND_EXPR, add any equivalences
-     created by the COND_EXPR to the hash table and initialize
-     EQ_EXPR_VALUE appropriately.
-
-     EQ_EXPR_VALUE is an assignment expression created when BB's immediate
-     dominator ends in a COND_EXPR statement whose predicate is of the form
-     'VAR == VALUE', where VALUE may be another variable or a constant.
-     This is used to propagate VALUE on the THEN_CLAUSE of that
-     conditional. This assignment is inserted in CONST_AND_COPIES so that
-     the copy and constant propagator can find more propagation
-     opportunities.  */
-  if (TREE_CODE (parent_block_last_stmt) == COND_EXPR
-      && (edge_flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
-    eq_expr_value = get_eq_expr_value (parent_block_last_stmt,
-                                      (edge_flags & EDGE_TRUE_VALUE) != 0,
-                                      &bd->avail_exprs,
-                                      bb,
-                                      &bd->vrp_variables);
-  /* Similarly when the parent block ended in a SWITCH_EXPR.
-     We can only know the value of the switch's condition if the dominator
-     parent is also the only predecessor of this block.  */
-  else if (bb->pred->src == parent
-          && TREE_CODE (parent_block_last_stmt) == SWITCH_EXPR)
-    {
-      tree switch_cond = SWITCH_COND (parent_block_last_stmt);
+      edge_info = (struct edge_info *) e->aux;
 
-      /* If the switch's condition is an SSA variable, then we may
-        know its value at each of the case labels.  */
-      if (TREE_CODE (switch_cond) == SSA_NAME)
+      if (edge_info)
        {
-         tree switch_vec = SWITCH_LABELS (parent_block_last_stmt);
-         size_t i, n = TREE_VEC_LENGTH (switch_vec);
-         int case_count = 0;
-         tree match_case = NULL_TREE;
-
-         /* Search the case labels for those whose destination is
-            the current basic block.  */
-         for (i = 0; i < n; ++i)
-           {
-             tree elt = TREE_VEC_ELT (switch_vec, i);
-             if (label_to_block (CASE_LABEL (elt)) == bb)
-               {
-                 if (++case_count > 1 || CASE_HIGH (elt))
-                   break;
-                 match_case = elt;
-               }
-           }
+         tree lhs = edge_info->lhs;
+         tree rhs = edge_info->rhs;
+         struct cond_equivalence *cond_equivalences = edge_info->cond_equivalences;
 
-         /* If we encountered precisely one CASE_LABEL_EXPR and it
-            was not the default case, or a case range, then we know
-            the exact value of SWITCH_COND which caused us to get to
-            this block.  Record that equivalence in EQ_EXPR_VALUE.  */
-         if (case_count == 1
-             && match_case
-             && CASE_LOW (match_case)
-             && !CASE_HIGH (match_case))
-           {
-             eq_expr_value.dst = switch_cond;
-             eq_expr_value.src = fold_convert (TREE_TYPE (switch_cond),
-                                               CASE_LOW (match_case));
-           }
+         if (lhs)
+           record_equality (lhs, rhs);
+
+         if (cond_equivalences)
+            for (i = 0; i < edge_info->max_cond_equivalences; i++)
+              record_cond (&cond_equivalences[i]);
        }
     }
-
-  /* If EQ_EXPR_VALUE (VAR == VALUE) is given, register the VALUE as a
-     new value for VAR, so that occurrences of VAR can be replaced with
-     VALUE while re-writing the THEN arm of a COND_EXPR.  */
-  if (eq_expr_value.src && eq_expr_value.dst)
-    record_equality (eq_expr_value.dst, eq_expr_value.src,
-                    &bd->const_and_copies);
 }
 
 /* Dump SSA statistics on FILE.  */
@@ -1282,21 +1029,11 @@ record_equivalences_from_incoming_edge (struct dom_walk_data *walk_data,
 void
 dump_dominator_optimization_stats (FILE *file)
 {
-  long n_exprs;
-
   fprintf (file, "Total number of statements:                   %6ld\n\n",
           opt_stats.num_stmts);
   fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
            opt_stats.num_exprs_considered);
 
-  n_exprs = opt_stats.num_exprs_considered;
-  if (n_exprs == 0)
-    n_exprs = 1;
-
-  fprintf (file, "    Redundant expressions eliminated:         %6ld (%.0f%%)\n",
-          opt_stats.num_re, PERCENT (opt_stats.num_re,
-                                     n_exprs));
-
   fprintf (file, "\nHash table statistics:\n");
 
   fprintf (file, "    avail_exprs: ");
@@ -1324,278 +1061,294 @@ htab_statistics (FILE *file, htab_t htab)
           htab_collisions (htab));
 }
 
-/* Record the fact that VAR has a nonzero value, though we may not know
-   its exact value.  Note that if VAR is already known to have a nonzero
-   value, then we do nothing.  */
-
-static void
-record_var_is_nonzero (tree var, varray_type *block_nonzero_vars_p)
-{
-  int indx = SSA_NAME_VERSION (var);
-
-  if (bitmap_bit_p (nonzero_vars, indx))
-    return;
-
-  /* Mark it in the global table.  */
-  bitmap_set_bit (nonzero_vars, indx);
-
-  /* Record this SSA_NAME so that we can reset the global table
-     when we leave this block.  */
-  if (! *block_nonzero_vars_p)
-    VARRAY_TREE_INIT (*block_nonzero_vars_p, 2, "block_nonzero_vars");
-  VARRAY_PUSH_TREE (*block_nonzero_vars_p, var);
-}
 
-/* Enter a statement into the true/false expression hash table indicating
-   that the condition COND has the value VALUE.  */
+/* Enter condition equivalence into the expression hash table.
+   This indicates that a conditional expression has a known
+   boolean value.  */
 
 static void
-record_cond (tree cond, tree value, varray_type *block_avail_exprs_p)
+record_cond (struct cond_equivalence *p)
 {
-  struct expr_hash_elt *element = xmalloc (sizeof (struct expr_hash_elt));
+  struct expr_hash_elt *element = XCNEW (struct expr_hash_elt);
   void **slot;
 
-  initialize_hash_element (cond, value, element);
+  initialize_hash_element_from_expr (&p->cond, p->value, element);
 
   slot = htab_find_slot_with_hash (avail_exprs, (void *)element,
-                                  element->hash, true);
+                                  element->hash, INSERT);
   if (*slot == NULL)
     {
       *slot = (void *) element;
-      if (! *block_avail_exprs_p)
-       VARRAY_TREE_INIT (*block_avail_exprs_p, 20, "block_avail_exprs");
-      VARRAY_PUSH_TREE (*block_avail_exprs_p, cond);
+
+      if (dump_file && (dump_flags & TDF_DETAILS))
+        {
+          fprintf (dump_file, "1>>> ");
+          print_expr_hash_elt (dump_file, element);
+        }
+
+      VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, element);
     }
   else
     free (element);
 }
 
-/* COND is a condition which is known to be true.   Record variants of
-   COND which must also be true.
+/* Build a cond_equivalence record indicating that the comparison
+   CODE holds between operands OP0 and OP1.  */
+
+static void
+build_and_record_new_cond (enum tree_code code,
+                           tree op0, tree op1,
+                           struct cond_equivalence *p)
+{
+  struct hashable_expr *cond = &p->cond;
+
+  gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
+
+  cond->type = boolean_type_node;
+  cond->kind = EXPR_BINARY;
+  cond->ops.binary.op = code;
+  cond->ops.binary.opnd0 = op0;
+  cond->ops.binary.opnd1 = op1;
+
+  p->value = boolean_true_node;
+}
+
+/* Record that COND is true and INVERTED is false into the edge information
+   structure.  Also record that any conditions dominated by COND are true
+   as well.
 
    For example, if a < b is true, then a <= b must also be true.  */
 
 static void
-record_dominating_conditions (tree cond, varray_type *block_avail_exprs_p)
+record_conditions (struct edge_info *edge_info, tree cond, tree inverted)
 {
+  tree op0, op1;
+
+  if (!COMPARISON_CLASS_P (cond))
+    return;
+
+  op0 = TREE_OPERAND (cond, 0);
+  op1 = TREE_OPERAND (cond, 1);
+
   switch (TREE_CODE (cond))
     {
     case LT_EXPR:
-      record_cond (build2 (LE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (ORDERED_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (NE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (LTGT_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      break;
-
     case GT_EXPR:
-      record_cond (build2 (GE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (ORDERED_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (NE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (LTGT_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
+      if (FLOAT_TYPE_P (TREE_TYPE (op0)))
+       {
+         edge_info->max_cond_equivalences = 6;
+         edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 6);
+         build_and_record_new_cond (ORDERED_EXPR, op0, op1,
+                                    &edge_info->cond_equivalences[4]);
+         build_and_record_new_cond (LTGT_EXPR, op0, op1,
+                                    &edge_info->cond_equivalences[5]);
+       }
+      else
+        {
+          edge_info->max_cond_equivalences = 4;
+         edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 4);
+       }
+
+      build_and_record_new_cond ((TREE_CODE (cond) == LT_EXPR
+                                 ? LE_EXPR : GE_EXPR),
+                                op0, op1, &edge_info->cond_equivalences[2]);
+      build_and_record_new_cond (NE_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[3]);
       break;
 
     case GE_EXPR:
     case LE_EXPR:
-      record_cond (build2 (ORDERED_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
+      if (FLOAT_TYPE_P (TREE_TYPE (op0)))
+       {
+         edge_info->max_cond_equivalences = 3;
+         edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 3);
+         build_and_record_new_cond (ORDERED_EXPR, op0, op1,
+                                    &edge_info->cond_equivalences[2]);
+       }
+      else
+       {
+         edge_info->max_cond_equivalences = 2;
+         edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 2);
+       }
       break;
 
     case EQ_EXPR:
-      record_cond (build2 (ORDERED_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (LE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (GE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
+      if (FLOAT_TYPE_P (TREE_TYPE (op0)))
+       {
+         edge_info->max_cond_equivalences = 5;
+         edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 5);
+         build_and_record_new_cond (ORDERED_EXPR, op0, op1,
+                                    &edge_info->cond_equivalences[4]);
+       }
+      else
+       {
+         edge_info->max_cond_equivalences = 4;
+         edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 4);
+       }
+      build_and_record_new_cond (LE_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[2]);
+      build_and_record_new_cond (GE_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[3]);
       break;
 
     case UNORDERED_EXPR:
-      record_cond (build2 (NE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (UNLE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (UNGE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (UNEQ_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (UNLT_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (UNGT_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
+      edge_info->max_cond_equivalences = 8;
+      edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 8);
+      build_and_record_new_cond (NE_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[2]);
+      build_and_record_new_cond (UNLE_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[3]);
+      build_and_record_new_cond (UNGE_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[4]);
+      build_and_record_new_cond (UNEQ_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[5]);
+      build_and_record_new_cond (UNLT_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[6]);
+      build_and_record_new_cond (UNGT_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[7]);
       break;
 
     case UNLT_EXPR:
-      record_cond (build2 (UNLE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (NE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      break;
-
     case UNGT_EXPR:
-      record_cond (build2 (UNGE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (NE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
+      edge_info->max_cond_equivalences = 4;
+      edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 4);
+      build_and_record_new_cond ((TREE_CODE (cond) == UNLT_EXPR
+                                 ? UNLE_EXPR : UNGE_EXPR),
+                                op0, op1, &edge_info->cond_equivalences[2]);
+      build_and_record_new_cond (NE_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[3]);
       break;
 
     case UNEQ_EXPR:
-      record_cond (build2 (UNLE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (UNGE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
+      edge_info->max_cond_equivalences = 4;
+      edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 4);
+      build_and_record_new_cond (UNLE_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[2]);
+      build_and_record_new_cond (UNGE_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[3]);
       break;
 
     case LTGT_EXPR:
-      record_cond (build2 (NE_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
-      record_cond (build2 (ORDERED_EXPR, boolean_type_node,
-                          TREE_OPERAND (cond, 0),
-                          TREE_OPERAND (cond, 1)),
-                  boolean_true_node,
-                  block_avail_exprs_p);
+      edge_info->max_cond_equivalences = 4;
+      edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 4);
+      build_and_record_new_cond (NE_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[2]);
+      build_and_record_new_cond (ORDERED_EXPR, op0, op1,
+                                &edge_info->cond_equivalences[3]);
+      break;
 
     default:
+      edge_info->max_cond_equivalences = 2;
+      edge_info->cond_equivalences = XNEWVEC (struct cond_equivalence, 2);
       break;
     }
+
+  /* Now store the original true and false conditions into the first
+     two slots.  */
+  initialize_expr_from_cond (cond, &edge_info->cond_equivalences[0].cond);
+  edge_info->cond_equivalences[0].value = boolean_true_node;
+
+  /* It is possible for INVERTED to be the negation of a comparison,
+     and not a valid RHS or GIMPLE_COND condition.  This happens because
+     invert_truthvalue may return such an expression when asked to invert
+     a floating-point comparison.  These comparisons are not assumed to
+     obey the trichotomy law.  */
+  initialize_expr_from_cond (inverted, &edge_info->cond_equivalences[1].cond);
+  edge_info->cond_equivalences[1].value = boolean_false_node;
 }
 
 /* A helper function for record_const_or_copy and record_equality.
    Do the work of recording the value and undo info.  */
 
 static void
-record_const_or_copy_1 (tree x, tree y, tree prev_x,
-                       varray_type *block_const_and_copies_p)
+record_const_or_copy_1 (tree x, tree y, tree prev_x)
+{
+  set_ssa_name_value (x, y);
+
+  if (dump_file && (dump_flags & TDF_DETAILS))
+    {
+      fprintf (dump_file, "0>>> COPY ");
+      print_generic_expr (dump_file, x, 0);
+      fprintf (dump_file, " = ");
+      print_generic_expr (dump_file, y, 0);
+      fprintf (dump_file, "\n");
+    }
+
+  VEC_reserve (tree, heap, const_and_copies_stack, 2);
+  VEC_quick_push (tree, const_and_copies_stack, prev_x);
+  VEC_quick_push (tree, const_and_copies_stack, x);
+}
+
+/* Return the loop depth of the basic block of the defining statement of X.
+   This number should not be treated as absolutely correct because the loop
+   information may not be completely up-to-date when dom runs.  However, it
+   will be relatively correct, and as more passes are taught to keep loop info
+   up to date, the result will become more and more accurate.  */
+
+int
+loop_depth_of_name (tree x)
 {
-  set_value_for (x, y, const_and_copies);
+  gimple defstmt;
+  basic_block defbb;
+
+  /* If it's not an SSA_NAME, we have no clue where the definition is.  */
+  if (TREE_CODE (x) != SSA_NAME)
+    return 0;
+
+  /* Otherwise return the loop depth of the defining statement's bb.
+     Note that there may not actually be a bb for this statement, if the
+     ssa_name is live on entry.  */
+  defstmt = SSA_NAME_DEF_STMT (x);
+  defbb = gimple_bb (defstmt);
+  if (!defbb)
+    return 0;
 
-  if (!*block_const_and_copies_p)
-    VARRAY_TREE_INIT (*block_const_and_copies_p, 2, "block_const_and_copies");
-  VARRAY_PUSH_TREE (*block_const_and_copies_p, x);
-  VARRAY_PUSH_TREE (*block_const_and_copies_p, prev_x);
+  return defbb->loop_depth;
 }
 
 /* Record that X is equal to Y in const_and_copies.  Record undo
-   information in the block-local varray.  */
+   information in the block-local vector.  */
 
 static void
-record_const_or_copy (tree x, tree y, varray_type *block_const_and_copies_p)
+record_const_or_copy (tree x, tree y)
 {
-  tree prev_x = get_value_for (x, const_and_copies);
+  tree prev_x = SSA_NAME_VALUE (x);
+
+  gcc_assert (TREE_CODE (x) == SSA_NAME);
 
   if (TREE_CODE (y) == SSA_NAME)
     {
-      tree tmp = get_value_for (y, const_and_copies);
+      tree tmp = SSA_NAME_VALUE (y);
       if (tmp)
        y = tmp;
     }
 
-  record_const_or_copy_1 (x, y, prev_x, block_const_and_copies_p);
+  record_const_or_copy_1 (x, y, prev_x);
 }
 
 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
    This constrains the cases in which we may treat this as assignment.  */
 
 static void
-record_equality (tree x, tree y, varray_type *block_const_and_copies_p)
+record_equality (tree x, tree y)
 {
   tree prev_x = NULL, prev_y = NULL;
 
   if (TREE_CODE (x) == SSA_NAME)
-    prev_x = get_value_for (x, const_and_copies);
+    prev_x = SSA_NAME_VALUE (x);
   if (TREE_CODE (y) == SSA_NAME)
-    prev_y = get_value_for (y, const_and_copies);
+    prev_y = SSA_NAME_VALUE (y);
 
-  /* If one of the previous values is invariant, then use that.
+  /* If one of the previous values is invariant, or invariant in more loops
+     (by depth), then use that.
      Otherwise it doesn't matter which value we choose, just so
      long as we canonicalize on one value.  */
-  if (TREE_INVARIANT (y))
+  if (is_gimple_min_invariant (y))
     ;
-  else if (TREE_INVARIANT (x))
+  else if (is_gimple_min_invariant (x)
+          || (loop_depth_of_name (x) <= loop_depth_of_name (y)))
     prev_x = x, x = y, y = prev_x, prev_x = prev_y;
-  else if (prev_x && TREE_INVARIANT (prev_x))
+  else if (prev_x && is_gimple_min_invariant (prev_x))
     x = y, y = prev_x, prev_x = prev_y;
   else if (prev_y)
     y = prev_y;
@@ -1613,750 +1366,403 @@ record_equality (tree x, tree y, varray_type *block_const_and_copies_p)
          || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (y))))
     return;
 
-  record_const_or_copy_1 (x, y, prev_x, block_const_and_copies_p);
+  record_const_or_copy_1 (x, y, prev_x);
 }
 
-/* STMT is a MODIFY_EXPR for which we were unable to find RHS in the
-   hash tables.  Try to simplify the RHS using whatever equivalences
-   we may have recorded.
+/* Returns true when STMT is a simple iv increment.  It detects the
+   following situation:
 
-   If we are able to simplify the RHS, then lookup the simplified form in
-   the hash table and return the result.  Otherwise return NULL.  */
+   i_1 = phi (..., i_2)
+   i_2 = i_1 +/- ...  */
 
-static tree
-simplify_rhs_and_lookup_avail_expr (struct dom_walk_data *walk_data,
-                                   tree stmt, int insert)
+static bool
+simple_iv_increment_p (gimple stmt)
 {
-  tree rhs = TREE_OPERAND (stmt, 1);
-  enum tree_code rhs_code = TREE_CODE (rhs);
-  tree result = NULL;
-  struct dom_walk_block_data *bd
-    = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
-
-  /* If we have lhs = ~x, look and see if we earlier had x = ~y.
-     In which case we can change this statement to be lhs = y.
-     Which can then be copy propagated. 
-
-     Similarly for negation.  */
-  if ((rhs_code == BIT_NOT_EXPR || rhs_code == NEGATE_EXPR)
-      && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME)
-    {
-      /* Get the definition statement for our RHS.  */
-      tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
-
-      /* See if the RHS_DEF_STMT has the same form as our statement.  */
-      if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR
-         && TREE_CODE (TREE_OPERAND (rhs_def_stmt, 1)) == rhs_code)
-       {
-         tree rhs_def_operand;
-
-         rhs_def_operand = TREE_OPERAND (TREE_OPERAND (rhs_def_stmt, 1), 0);
-
-         /* Verify that RHS_DEF_OPERAND is a suitable SSA variable.  */
-         if (TREE_CODE (rhs_def_operand) == SSA_NAME
-             && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs_def_operand))
-           result = update_rhs_and_lookup_avail_expr (stmt,
-                                                      rhs_def_operand,
-                                                      &bd->avail_exprs,
-                                                      insert);
-       }
-    }
+  tree lhs, preinc;
+  gimple phi;
+  size_t i;
 
-  /* If we have z = (x OP C1), see if we earlier had x = y OP C2.
-     If OP is associative, create and fold (y OP C2) OP C1 which
-     should result in (y OP C3), use that as the RHS for the
-     assignment.  Add minus to this, as we handle it specially below.  */
-  if ((associative_tree_code (rhs_code) || rhs_code == MINUS_EXPR)
-      && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
-      && is_gimple_min_invariant (TREE_OPERAND (rhs, 1)))
-    {
-      tree rhs_def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
+  if (gimple_code (stmt) != GIMPLE_ASSIGN)
+    return false;
 
-      /* See if the RHS_DEF_STMT has the same form as our statement.  */
-      if (TREE_CODE (rhs_def_stmt) == MODIFY_EXPR)
-       {
-         tree rhs_def_rhs = TREE_OPERAND (rhs_def_stmt, 1);
-         enum tree_code rhs_def_code = TREE_CODE (rhs_def_rhs);
+  lhs = gimple_assign_lhs (stmt);
+  if (TREE_CODE (lhs) != SSA_NAME)
+    return false;
 
-         if (rhs_code == rhs_def_code
-             || (rhs_code == PLUS_EXPR && rhs_def_code == MINUS_EXPR)
-             || (rhs_code == MINUS_EXPR && rhs_def_code == PLUS_EXPR))
-           {
-             tree def_stmt_op0 = TREE_OPERAND (rhs_def_rhs, 0);
-             tree def_stmt_op1 = TREE_OPERAND (rhs_def_rhs, 1);
+  if (gimple_assign_rhs_code (stmt) != PLUS_EXPR
+      && gimple_assign_rhs_code (stmt) != MINUS_EXPR)
+    return false;
 
-             if (TREE_CODE (def_stmt_op0) == SSA_NAME
-                 && ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def_stmt_op0)
-                 && is_gimple_min_invariant (def_stmt_op1))
-               {
-                 tree outer_const = TREE_OPERAND (rhs, 1);
-                 tree type = TREE_TYPE (TREE_OPERAND (stmt, 0));
-                 tree t;
-
-                 /* If we care about correct floating point results, then
-                    don't fold x + c1 - c2.  Note that we need to take both
-                    the codes and the signs to figure this out.  */
-                 if (FLOAT_TYPE_P (type)
-                     && !flag_unsafe_math_optimizations
-                     && (rhs_def_code == PLUS_EXPR
-                         || rhs_def_code == MINUS_EXPR))
-                   {
-                     bool neg = false;
+  preinc = gimple_assign_rhs1 (stmt);
 
-                     neg ^= (rhs_code == MINUS_EXPR);
-                     neg ^= (rhs_def_code == MINUS_EXPR);
-                     neg ^= real_isneg (TREE_REAL_CST_PTR (outer_const));
-                     neg ^= real_isneg (TREE_REAL_CST_PTR (def_stmt_op1));
+  if (TREE_CODE (preinc) != SSA_NAME)
+    return false;
 
-                     if (neg)
-                       goto dont_fold_assoc;
-                   }
+  phi = SSA_NAME_DEF_STMT (preinc);
+  if (gimple_code (phi) != GIMPLE_PHI)
+    return false;
 
-                 /* Ho hum.  So fold will only operate on the outermost
-                    thingy that we give it, so we have to build the new
-                    expression in two pieces.  This requires that we handle
-                    combinations of plus and minus.  */
-                 if (rhs_def_code != rhs_code)
-                   {
-                     if (rhs_def_code == MINUS_EXPR)
-                       t = build (MINUS_EXPR, type, outer_const, def_stmt_op1);
-                     else
-                       t = build (MINUS_EXPR, type, def_stmt_op1, outer_const);
-                     rhs_code = PLUS_EXPR;
-                   }
-                 else if (rhs_def_code == MINUS_EXPR)
-                   t = build (PLUS_EXPR, type, def_stmt_op1, outer_const);
-                 else
-                   t = build (rhs_def_code, type, def_stmt_op1, outer_const);
-                 t = local_fold (t);
-                 t = build (rhs_code, type, def_stmt_op0, t);
-                 t = local_fold (t);
-
-                 /* If the result is a suitable looking gimple expression,
-                    then use it instead of the original for STMT.  */
-                 if (TREE_CODE (t) == SSA_NAME
-                     || (TREE_CODE_CLASS (TREE_CODE (t)) == '1'
-                         && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME)
-                     || ((TREE_CODE_CLASS (TREE_CODE (t)) == '2'
-                          || TREE_CODE_CLASS (TREE_CODE (t)) == '<')
-                         && TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
-                         && is_gimple_val (TREE_OPERAND (t, 1))))
-                   result = update_rhs_and_lookup_avail_expr
-                     (stmt, t, &bd->avail_exprs, insert);
-               }
-           }
-       }
- dont_fold_assoc:;
-    }
+  for (i = 0; i < gimple_phi_num_args (phi); i++)
+    if (gimple_phi_arg_def (phi, i) == lhs)
+      return true;
 
-  /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
-     and BIT_AND_EXPR respectively if the first operand is greater
-     than zero and the second operand is an exact power of two.  */
-  if ((rhs_code == TRUNC_DIV_EXPR || rhs_code == TRUNC_MOD_EXPR)
-      && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))
-      && integer_pow2p (TREE_OPERAND (rhs, 1)))
-    {
-      tree val;
-      tree op = TREE_OPERAND (rhs, 0);
+  return false;
+}
 
-      if (TYPE_UNSIGNED (TREE_TYPE (op)))
-       {
-         val = integer_one_node;
-       }
-      else
-       {
-         tree dummy_cond = walk_data->global_data;
+/* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
+   known value for that SSA_NAME (or NULL if no value is known).
 
-         if (! dummy_cond)
-           {
-             dummy_cond = build (GT_EXPR, boolean_type_node,
-                                 op, integer_zero_node);
-             dummy_cond = build (COND_EXPR, void_type_node,
-                                 dummy_cond, NULL, NULL);
-             walk_data->global_data = dummy_cond;
-           }
-          else
-           {
-             TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), GT_EXPR);
-             TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
-             TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
-               = integer_zero_node;
-           }
-         val = simplify_cond_and_lookup_avail_expr (dummy_cond,
-                                                    &bd->avail_exprs,
-                                                    NULL, false);
-       }
+   Propagate values from CONST_AND_COPIES into the PHI nodes of the
+   successors of BB.  */
 
-      if (val && integer_onep (val))
-       {
-         tree t;
-         tree op0 = TREE_OPERAND (rhs, 0);
-         tree op1 = TREE_OPERAND (rhs, 1);
-
-         if (rhs_code == TRUNC_DIV_EXPR)
-           t = build (RSHIFT_EXPR, TREE_TYPE (op0), op0,
-                      build_int_cst (NULL_TREE, tree_log2 (op1)));
-         else
-           t = build (BIT_AND_EXPR, TREE_TYPE (op0), op0,
-                      local_fold (build (MINUS_EXPR, TREE_TYPE (op1),
-                                         op1, integer_one_node)));
-
-         result = update_rhs_and_lookup_avail_expr (stmt, t,
-                                                    &bd->avail_exprs, insert);
-       }
-    }
+static void
+cprop_into_successor_phis (basic_block bb)
+{
+  edge e;
+  edge_iterator ei;
 
-  /* Transform ABS (X) into X or -X as appropriate.  */
-  if (rhs_code == ABS_EXPR
-      && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0))))
+  FOR_EACH_EDGE (e, ei, bb->succs)
     {
-      tree val;
-      tree op = TREE_OPERAND (rhs, 0);
-      tree type = TREE_TYPE (op);
-
-      if (TYPE_UNSIGNED (type))
-       {
-         val = integer_zero_node;
-       }
-      else
-       {
-         tree dummy_cond = walk_data->global_data;
-
-         if (! dummy_cond)
-           {
-             dummy_cond = build (LE_EXPR, boolean_type_node,
-                                 op, integer_zero_node);
-             dummy_cond = build (COND_EXPR, void_type_node,
-                                 dummy_cond, NULL, NULL);
-             walk_data->global_data = dummy_cond;
-           }
-         else
-           {
-             TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), LE_EXPR);
-             TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
-             TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
-               = fold_convert (type, integer_zero_node);
-           }
-         val = simplify_cond_and_lookup_avail_expr (dummy_cond,
-                                                    &bd->avail_exprs,
-                                                    NULL, false);
-
-         if (!val)
-           {
-             TREE_SET_CODE (TREE_OPERAND (dummy_cond, 0), GE_EXPR);
-             TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 0) = op;
-             TREE_OPERAND (TREE_OPERAND (dummy_cond, 0), 1)
-               = fold_convert (type, integer_zero_node);
+      int indx;
+      gimple_stmt_iterator gsi;
 
-             val = simplify_cond_and_lookup_avail_expr (dummy_cond,
-                                                        &bd->avail_exprs,
-                                                        NULL, false);
+      /* If this is an abnormal edge, then we do not want to copy propagate
+        into the PHI alternative associated with this edge.  */
+      if (e->flags & EDGE_ABNORMAL)
+       continue;
 
-             if (val)
-               {
-                 if (integer_zerop (val))
-                   val = integer_one_node;
-                 else if (integer_onep (val))
-                   val = integer_zero_node;
-               }
-           }
-       }
+      gsi = gsi_start_phis (e->dest);
+      if (gsi_end_p (gsi))
+       continue;
 
-      if (val
-         && (integer_onep (val) || integer_zerop (val)))
+      indx = e->dest_idx;
+      for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
        {
-         tree t;
+         tree new_val;
+         use_operand_p orig_p;
+         tree orig_val;
+          gimple phi = gsi_stmt (gsi);
 
-         if (integer_onep (val))
-           t = build1 (NEGATE_EXPR, TREE_TYPE (op), op);
-         else
-           t = op;
+         /* The alternative may be associated with a constant, so verify
+            it is an SSA_NAME before doing anything with it.  */
+         orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
+         orig_val = get_use_from_ptr (orig_p);
+         if (TREE_CODE (orig_val) != SSA_NAME)
+           continue;
 
-         result = update_rhs_and_lookup_avail_expr (stmt, t,
-                                                    &bd->avail_exprs, insert);
+         /* If we have *ORIG_P in our constant/copy table, then replace
+            ORIG_P with its value in our constant/copy table.  */
+         new_val = SSA_NAME_VALUE (orig_val);
+         if (new_val
+             && new_val != orig_val
+             && (TREE_CODE (new_val) == SSA_NAME
+                 || is_gimple_min_invariant (new_val))
+             && may_propagate_copy (orig_val, new_val))
+           propagate_value (orig_p, new_val);
        }
     }
-
-  /* Optimize *"foo" into 'f'.  This is done here rather than
-     in fold to avoid problems with stuff like &*"foo".  */
-  if (TREE_CODE (rhs) == INDIRECT_REF || TREE_CODE (rhs) == ARRAY_REF)
-    {
-      tree t = fold_read_from_constant_string (rhs);
-
-      if (t)
-        result = update_rhs_and_lookup_avail_expr (stmt, t,
-                                                  &bd->avail_exprs, insert);
-    }
-
-  return result;
 }
 
-/* COND is a condition of the form:
-
-     x == const or x != const
-
-   Look back to x's defining statement and see if x is defined as
-
-     x = (type) y;
-
-   If const is unchanged if we convert it to type, then we can build
-   the equivalent expression:
-
-
-      y == const or y != const
-
-   Which may allow further optimizations.
+/* We have finished optimizing BB, record any information implied by
+   taking a specific outgoing edge from BB.  */
 
-   Return the equivalent comparison or NULL if no such equivalent comparison
-   was found.  */
-
-static tree
-find_equivalent_equality_comparison (tree cond)
+static void
+record_edge_info (basic_block bb)
 {
-  tree op0 = TREE_OPERAND (cond, 0);
-  tree op1 = TREE_OPERAND (cond, 1);
-  tree def_stmt = SSA_NAME_DEF_STMT (op0);
+  gimple_stmt_iterator gsi = gsi_last_bb (bb);
+  struct edge_info *edge_info;
 
-  /* OP0 might have been a parameter, so first make sure it
-     was defined by a MODIFY_EXPR.  */
-  if (def_stmt && TREE_CODE (def_stmt) == MODIFY_EXPR)
+  if (! gsi_end_p (gsi))
     {
-      tree def_rhs = TREE_OPERAND (def_stmt, 1);
+      gimple stmt = gsi_stmt (gsi);
+      location_t loc = gimple_location (stmt);
 
-      /* Now make sure the RHS of the MODIFY_EXPR is a typecast.  */
-      if ((TREE_CODE (def_rhs) == NOP_EXPR
-          || TREE_CODE (def_rhs) == CONVERT_EXPR)
-         && TREE_CODE (TREE_OPERAND (def_rhs, 0)) == SSA_NAME)
+      if (gimple_code (stmt) == GIMPLE_SWITCH)
        {
-         tree def_rhs_inner = TREE_OPERAND (def_rhs, 0);
-         tree def_rhs_inner_type = TREE_TYPE (def_rhs_inner);
-         tree new;
-
-         if (TYPE_PRECISION (def_rhs_inner_type)
-             > TYPE_PRECISION (TREE_TYPE (def_rhs)))
-           return NULL;
-
-         /* What we want to prove is that if we convert OP1 to
-            the type of the object inside the NOP_EXPR that the
-            result is still equivalent to SRC. 
-
-            If that is true, the build and return new equivalent
-            condition which uses the source of the typecast and the
-            new constant (which has only changed its type).  */
-         new = build1 (TREE_CODE (def_rhs), def_rhs_inner_type, op1);
-         new = local_fold (new);
-         if (is_gimple_val (new) && tree_int_cst_equal (new, op1))
-           return build (TREE_CODE (cond), TREE_TYPE (cond),
-                         def_rhs_inner, new);
-       }
-    }
-  return NULL;
-}
-
-/* STMT is a COND_EXPR for which we could not trivially determine its
-   result.  This routine attempts to find equivalent forms of the
-   condition which we may be able to optimize better.  It also 
-   uses simple value range propagation to optimize conditionals.  */
-
-static tree
-simplify_cond_and_lookup_avail_expr (tree stmt,
-                                    varray_type *block_avail_exprs_p,
-                                    stmt_ann_t ann,
-                                    int insert)
-{
-  tree cond = COND_EXPR_COND (stmt);
-
-  if (TREE_CODE_CLASS (TREE_CODE (cond)) == '<')
-    {
-      tree op0 = TREE_OPERAND (cond, 0);
-      tree op1 = TREE_OPERAND (cond, 1);
+         tree index = gimple_switch_index (stmt);
 
-      if (TREE_CODE (op0) == SSA_NAME && is_gimple_min_invariant (op1))
-       {
-         int limit;
-         tree low, high, cond_low, cond_high;
-         int lowequal, highequal, swapped, no_overlap, subset, cond_inverted;
-         varray_type vrp_records;
-         struct vrp_element *element;
-
-         /* First see if we have test of an SSA_NAME against a constant
-            where the SSA_NAME is defined by an earlier typecast which
-            is irrelevant when performing tests against the given
-            constant.  */
-         if (TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
+         if (TREE_CODE (index) == SSA_NAME)
            {
-             tree new_cond = find_equivalent_equality_comparison (cond);
+             int i;
+              int n_labels = gimple_switch_num_labels (stmt);
+             tree *info = XCNEWVEC (tree, last_basic_block);
+             edge e;
+             edge_iterator ei;
 
-             if (new_cond)
+             for (i = 0; i < n_labels; i++)
                {
-                 /* Update the statement to use the new equivalent
-                    condition.  */
-                 COND_EXPR_COND (stmt) = new_cond;
-
-                 /* If this is not a real stmt, ann will be NULL and we
-                    avoid processing the operands.  */
-                 if (ann)
-                   modify_stmt (stmt);
-
-                 /* Lookup the condition and return its known value if it
-                    exists.  */
-                 new_cond = lookup_avail_expr (stmt, block_avail_exprs_p,
-                                               insert);
-                 if (new_cond)
-                   return new_cond;
-
-                 /* The operands have changed, so update op0 and op1.  */
-                 op0 = TREE_OPERAND (cond, 0);
-                 op1 = TREE_OPERAND (cond, 1);
+                 tree label = gimple_switch_label (stmt, i);
+                 basic_block target_bb = label_to_block (CASE_LABEL (label));
+                 if (CASE_HIGH (label)
+                     || !CASE_LOW (label)
+                     || info[target_bb->index])
+                   info[target_bb->index] = error_mark_node;
+                 else
+                   info[target_bb->index] = label;
                }
-           }
 
-         /* Consult the value range records for this variable (if they exist)
-            to see if we can eliminate or simplify this conditional. 
-
-            Note two tests are necessary to determine no records exist.
-            First we have to see if the virtual array exists, if it 
-            exists, then we have to check its active size. 
-
-            Also note the vast majority of conditionals are not testing
-            a variable which has had its range constrained by an earlier
-            conditional.  So this filter avoids a lot of unnecessary work.  */
-         vrp_records = VARRAY_GENERIC_PTR (vrp_data, SSA_NAME_VERSION (op0));
-         if (vrp_records == NULL)
-           return NULL;
-
-         limit = VARRAY_ACTIVE_SIZE (vrp_records);
-
-         /* If we have no value range records for this variable, or we are
-            unable to extract a range for this condition, then there is
-            nothing to do.  */
-         if (limit == 0
-             || ! extract_range_from_cond (cond, &cond_high,
-                                           &cond_low, &cond_inverted))
-           return NULL;
-
-         /* We really want to avoid unnecessary computations of range
-            info.  So all ranges are computed lazily; this avoids a
-            lot of unnecessary work.  ie, we record the conditional,
-            but do not process how it constrains the variable's 
-            potential values until we know that processing the condition
-            could be helpful.
-
-            However, we do not want to have to walk a potentially long
-            list of ranges, nor do we want to compute a variable's
-            range more than once for a given path.
-
-            Luckily, each time we encounter a conditional that can not
-            be otherwise optimized we will end up here and we will
-            compute the necessary range information for the variable
-            used in this condition.
-
-            Thus you can conclude that there will never be more than one
-            conditional associated with a variable which has not been
-            processed.  So we never need to merge more than one new
-            conditional into the current range. 
-
-            These properties also help us avoid unnecessary work.  */
-          element
-            = (struct vrp_element *)VARRAY_GENERIC_PTR (vrp_records, limit - 1);
-
-         if (element->high && element->low)
-           {
-             /* The last element has been processed, so there is no range
-                merging to do, we can simply use the high/low values
-                recorded in the last element.  */
-             low = element->low;
-             high = element->high;
-           }
-         else
-           {
-             tree tmp_high, tmp_low;
-             int dummy;
-
-             /* The last element has not been processed.  Process it now.  */
-             extract_range_from_cond (element->cond, &tmp_high,
-                                      &tmp_low, &dummy);
-         
-             /* If this is the only element, then no merging is necessary, 
-                the high/low values from extract_range_from_cond are all
-                we need.  */
-             if (limit == 1)
-               {
-                 low = tmp_low;
-                 high = tmp_high;
-               }
-             else
+             FOR_EACH_EDGE (e, ei, bb->succs)
                {
-                 /* Get the high/low value from the previous element.  */
-                 struct vrp_element *prev
-                   = (struct vrp_element *)VARRAY_GENERIC_PTR (vrp_records,
-                                                               limit - 2);
-                 low = prev->low;
-                 high = prev->high;
-
-                 /* Merge in this element's range with the range from the
-                    previous element.
-
-                    The low value for the merged range is the maximum of
-                    the previous low value and the low value of this record.
-
-                    Similarly the high value for the merged range is the
-                    minimum of the previous high value and the high value of
-                    this record.  */
-                 low = (tree_int_cst_compare (low, tmp_low) == 1
-                        ? low : tmp_low);
-                 high = (tree_int_cst_compare (high, tmp_high) == -1
-                         ? high : tmp_high);
-               }
-
-             /* And record the computed range.  */
-             element->low = low;
-             element->high = high;
-
-           }
-
-         /* After we have constrained this variable's potential values,
-            we try to determine the result of the given conditional.
-
-            To simplify later tests, first determine if the current
-            low value is the same low value as the conditional.
-            Similarly for the current high value and the high value
-            for the conditional.  */
-         lowequal = tree_int_cst_equal (low, cond_low);
-         highequal = tree_int_cst_equal (high, cond_high);
-
-         if (lowequal && highequal)
-           return (cond_inverted ? boolean_false_node : boolean_true_node);
-
-         /* To simplify the overlap/subset tests below we may want
-            to swap the two ranges so that the larger of the two
-            ranges occurs "first".  */
-         swapped = 0;
-         if (tree_int_cst_compare (low, cond_low) == 1
-             || (lowequal 
-                 && tree_int_cst_compare (cond_high, high) == 1))
-           {
-             tree temp;
-
-             swapped = 1;
-             temp = low;
-             low = cond_low;
-             cond_low = temp;
-             temp = high;
-             high = cond_high;
-             cond_high = temp;
-           }
+                 basic_block target_bb = e->dest;
+                 tree label = info[target_bb->index];
 
-         /* Now determine if there is no overlap in the ranges
-            or if the second range is a subset of the first range.  */
-         no_overlap = tree_int_cst_lt (high, cond_low);
-         subset = tree_int_cst_compare (cond_high, high) != 1;
-
-         /* If there was no overlap in the ranges, then this conditional
-            always has a false value (unless we had to invert this
-            conditional, in which case it always has a true value).  */
-         if (no_overlap)
-           return (cond_inverted ? boolean_true_node : boolean_false_node);
-
-         /* If the current range is a subset of the condition's range,
-            then this conditional always has a true value (unless we
-            had to invert this conditional, in which case it always
-            has a true value).  */
-         if (subset && swapped)
-           return (cond_inverted ? boolean_false_node : boolean_true_node);
-
-         /* We were unable to determine the result of the conditional.
-            However, we may be able to simplify the conditional.  First
-            merge the ranges in the same manner as range merging above.  */
-         low = tree_int_cst_compare (low, cond_low) == 1 ? low : cond_low;
-         high = tree_int_cst_compare (high, cond_high) == -1 ? high : cond_high;
-         
-         /* If the range has converged to a single point, then turn this
-            into an equality comparison.  */
-         if (TREE_CODE (cond) != EQ_EXPR
-             && TREE_CODE (cond) != NE_EXPR
-             && tree_int_cst_equal (low, high))
-           {
-             TREE_SET_CODE (cond, EQ_EXPR);
-             TREE_OPERAND (cond, 1) = high;
+                 if (label != NULL && label != error_mark_node)
+                   {
+                     tree x = fold_convert_loc (loc, TREE_TYPE (index),
+                                                CASE_LOW (label));
+                     edge_info = allocate_edge_info (e);
+                     edge_info->lhs = index;
+                     edge_info->rhs = x;
+                   }
+               }
+             free (info);
            }
        }
+
+      /* A COND_EXPR may create equivalences too.  */
+      if (gimple_code (stmt) == GIMPLE_COND)
+       {
+         edge true_edge;
+         edge false_edge;
+
+          tree op0 = gimple_cond_lhs (stmt);
+          tree op1 = gimple_cond_rhs (stmt);
+          enum tree_code code = gimple_cond_code (stmt);
+
+         extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
+
+          /* Special case comparing booleans against a constant as we
+             know the value of OP0 on both arms of the branch.  i.e., we
+             can record an equivalence for OP0 rather than COND.  */
+          if ((code == EQ_EXPR || code == NE_EXPR)
+              && TREE_CODE (op0) == SSA_NAME
+              && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
+              && is_gimple_min_invariant (op1))
+            {
+              if (code == EQ_EXPR)
+                {
+                  edge_info = allocate_edge_info (true_edge);
+                  edge_info->lhs = op0;
+                  edge_info->rhs = (integer_zerop (op1)
+                                    ? boolean_false_node
+                                    : boolean_true_node);
+
+                  edge_info = allocate_edge_info (false_edge);
+                  edge_info->lhs = op0;
+                  edge_info->rhs = (integer_zerop (op1)
+                                    ? boolean_true_node
+                                    : boolean_false_node);
+                }
+              else
+                {
+                  edge_info = allocate_edge_info (true_edge);
+                  edge_info->lhs = op0;
+                  edge_info->rhs = (integer_zerop (op1)
+                                    ? boolean_true_node
+                                    : boolean_false_node);
+
+                  edge_info = allocate_edge_info (false_edge);
+                  edge_info->lhs = op0;
+                  edge_info->rhs = (integer_zerop (op1)
+                                    ? boolean_false_node
+                                    : boolean_true_node);
+                }
+            }
+          else if (is_gimple_min_invariant (op0)
+                   && (TREE_CODE (op1) == SSA_NAME
+                       || is_gimple_min_invariant (op1)))
+            {
+              tree cond = build2 (code, boolean_type_node, op0, op1);
+              tree inverted = invert_truthvalue_loc (loc, cond);
+              struct edge_info *edge_info;
+
+              edge_info = allocate_edge_info (true_edge);
+              record_conditions (edge_info, cond, inverted);
+
+              if (code == EQ_EXPR)
+                {
+                  edge_info->lhs = op1;
+                  edge_info->rhs = op0;
+                }
+
+              edge_info = allocate_edge_info (false_edge);
+              record_conditions (edge_info, inverted, cond);
+
+              if (code == NE_EXPR)
+                {
+                  edge_info->lhs = op1;
+                  edge_info->rhs = op0;
+                }
+            }
+
+          else if (TREE_CODE (op0) == SSA_NAME
+                   && (is_gimple_min_invariant (op1)
+                       || TREE_CODE (op1) == SSA_NAME))
+            {
+              tree cond = build2 (code, boolean_type_node, op0, op1);
+              tree inverted = invert_truthvalue_loc (loc, cond);
+              struct edge_info *edge_info;
+
+              edge_info = allocate_edge_info (true_edge);
+              record_conditions (edge_info, cond, inverted);
+
+              if (code == EQ_EXPR)
+                {
+                  edge_info->lhs = op0;
+                  edge_info->rhs = op1;
+                }
+
+              edge_info = allocate_edge_info (false_edge);
+              record_conditions (edge_info, inverted, cond);
+
+              if (TREE_CODE (cond) == NE_EXPR)
+                {
+                  edge_info->lhs = op0;
+                  edge_info->rhs = op1;
+                }
+            }
+        }
+
+      /* ??? TRUTH_NOT_EXPR can create an equivalence too.  */
     }
-  return 0;
 }
 
-/* STMT is a SWITCH_EXPR for which we could not trivially determine its
-   result.  This routine attempts to find equivalent forms of the
-   condition which we may be able to optimize better.  */
-
-static tree
-simplify_switch_and_lookup_avail_expr (tree stmt,
-                                      varray_type *block_avail_exprs_p,
-                                      int insert)
+static void
+dom_opt_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+                    basic_block bb)
 {
-  tree cond = SWITCH_COND (stmt);
-  tree def, to, ti;
-
-  /* The optimization that we really care about is removing unnecessary
-     casts.  That will let us do much better in propagating the inferred
-     constant at the switch target.  */
-  if (TREE_CODE (cond) == SSA_NAME)
-    {
-      def = SSA_NAME_DEF_STMT (cond);
-      if (TREE_CODE (def) == MODIFY_EXPR)
-       {
-         def = TREE_OPERAND (def, 1);
-         if (TREE_CODE (def) == NOP_EXPR)
-           {
-             int need_precision;
-             bool fail;
-
-             def = TREE_OPERAND (def, 0);
-
-#ifdef ENABLE_CHECKING
-             /* ??? Why was Jeff testing this?  We are gimple...  */
-             if (!is_gimple_val (def))
-               abort ();
-#endif
+  gimple_stmt_iterator gsi;
 
-             to = TREE_TYPE (cond);
-             ti = TREE_TYPE (def);
+  if (dump_file && (dump_flags & TDF_DETAILS))
+    fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
 
-             /* If we have an extension that preserves value, then we
-                can copy the source value into the switch.  */
+  /* Push a marker on the stacks of local information so that we know how
+     far to unwind when we finalize this block.  */
+  VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, NULL);
+  VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
 
-             need_precision = TYPE_PRECISION (ti);
-             fail = false;
-             if (TYPE_UNSIGNED (to) && !TYPE_UNSIGNED (ti))
-               fail = true;
-             else if (!TYPE_UNSIGNED (to) && TYPE_UNSIGNED (ti))
-               need_precision += 1;
-             if (TYPE_PRECISION (to) < need_precision)
-               fail = true;
+  record_equivalences_from_incoming_edge (bb);
 
-             if (!fail)
-               {
-                 SWITCH_COND (stmt) = def;
-                 modify_stmt (stmt);
+  /* PHI nodes can create equivalences too.  */
+  record_equivalences_from_phis (bb);
 
-                 return lookup_avail_expr (stmt, block_avail_exprs_p, insert);
-               }
-           }
-       }
-    }
+  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+    optimize_stmt (bb, gsi);
 
-  return 0;
+  /* Now prepare to process dominated blocks.  */
+  record_edge_info (bb);
+  cprop_into_successor_phis (bb);
 }
 
-
-/* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
-   known value for that SSA_NAME (or NULL if no value is known).  
-
-   NONZERO_VARS is the set SSA_NAMES known to have a nonzero value,
-   even if we don't know their precise value.
-
-   Propagate values from CONST_AND_COPIES and NONZERO_VARS into the PHI
-   nodes of the successors of BB.  */
+/* We have finished processing the dominator children of BB, perform
+   any finalization actions in preparation for leaving this node in
+   the dominator tree.  */
 
 static void
-cprop_into_successor_phis (basic_block bb,
-                          varray_type const_and_copies,
-                          bitmap nonzero_vars)
+dom_opt_leave_block (struct dom_walk_data *walk_data, basic_block bb)
 {
-  edge e;
-
-  /* This can get rather expensive if the implementation is naive in
-     how it finds the phi alternative associated with a particular edge.  */
-  for (e = bb->succ; e; e = e->succ_next)
+  gimple last;
+
+  /* If we have an outgoing edge to a block with multiple incoming and
+     outgoing edges, then we may be able to thread the edge, i.e., we
+     may be able to statically determine which of the outgoing edges
+     will be traversed when the incoming edge from BB is traversed.  */
+  if (single_succ_p (bb)
+      && (single_succ_edge (bb)->flags & EDGE_ABNORMAL) == 0
+      && potentially_threadable_block (single_succ (bb)))
     {
-      tree phi;
-      int phi_num_args;
-      int hint;
-
-      /* If this is an abnormal edge, then we do not want to copy propagate
-        into the PHI alternative associated with this edge.  */
-      if (e->flags & EDGE_ABNORMAL)
-       continue;
+      dom_thread_across_edge (walk_data, single_succ_edge (bb));
+    }
+  else if ((last = last_stmt (bb))
+          && gimple_code (last) == GIMPLE_COND
+          && EDGE_COUNT (bb->succs) == 2
+          && (EDGE_SUCC (bb, 0)->flags & EDGE_ABNORMAL) == 0
+          && (EDGE_SUCC (bb, 1)->flags & EDGE_ABNORMAL) == 0)
+    {
+      edge true_edge, false_edge;
 
-      phi = phi_nodes (e->dest);
-      if (! phi)
-       continue;
+      extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
 
-      /* There is no guarantee that for any two PHI nodes in a block that
-        the phi alternative associated with a particular edge will be
-        at the same index in the phi alternative array.
-
-        However, it is very likely they will be the same.  So we keep
-        track of the index of the alternative where we found the edge in
-        the previous phi node and check that index first in the next
-        phi node.  If that hint fails, then we actually search all
-        the entries.  */
-      phi_num_args = PHI_NUM_ARGS (phi);
-      hint = phi_num_args;
-      for ( ; phi; phi = PHI_CHAIN (phi))
+      /* Only try to thread the edge if it reaches a target block with
+        more than one predecessor and more than one successor.  */
+      if (potentially_threadable_block (true_edge->dest))
        {
-         int i;
-         tree new;
-         use_operand_p orig_p;
-         tree orig;
+         struct edge_info *edge_info;
+         unsigned int i;
 
-         /* If the hint is valid (!= phi_num_args), see if it points
-            us to the desired phi alternative.  */
-         if (hint != phi_num_args && PHI_ARG_EDGE (phi, hint) == e)
-           ;
-         else
+         /* Push a marker onto the available expression stack so that we
+            unwind any expressions related to the TRUE arm before processing
+            the false arm below.  */
+          VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, NULL);
+         VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
+
+         edge_info = (struct edge_info *) true_edge->aux;
+
+         /* If we have info associated with this edge, record it into
+            our equivalence tables.  */
+         if (edge_info)
            {
-             /* The hint was either invalid or did not point to the
-                correct phi alternative.  Search all the alternatives
-                for the correct one.  Update the hint.  */
-             for (i = 0; i < phi_num_args; i++)
-               if (PHI_ARG_EDGE (phi, i) == e)
-                 break;
-             hint = i;
+             struct cond_equivalence *cond_equivalences = edge_info->cond_equivalences;
+             tree lhs = edge_info->lhs;
+             tree rhs = edge_info->rhs;
+
+             /* If we have a simple NAME = VALUE equivalence, record it.  */
+             if (lhs && TREE_CODE (lhs) == SSA_NAME)
+               record_const_or_copy (lhs, rhs);
+
+             /* If we have 0 = COND or 1 = COND equivalences, record them
+                into our expression hash tables.  */
+             if (cond_equivalences)
+               for (i = 0; i < edge_info->max_cond_equivalences; i++)
+                  record_cond (&cond_equivalences[i]);
            }
 
-#ifdef ENABLE_CHECKING
-         /* If we did not find the proper alternative, then something is
-            horribly wrong.  */
-         if (hint == phi_num_args)
-           abort ();
-#endif
+         dom_thread_across_edge (walk_data, true_edge);
 
-         /* The alternative may be associated with a constant, so verify
-            it is an SSA_NAME before doing anything with it.  */
-         orig_p = PHI_ARG_DEF_PTR (phi, hint);
-         orig = USE_FROM_PTR (orig_p);
-         if (TREE_CODE (orig) != SSA_NAME)
-           continue;
+         /* And restore the various tables to their state before
+            we threaded this edge.  */
+         remove_local_expressions_from_table ();
+       }
+
+      /* Similarly for the ELSE arm.  */
+      if (potentially_threadable_block (false_edge->dest))
+       {
+         struct edge_info *edge_info;
+         unsigned int i;
 
-         /* If the alternative is known to have a nonzero value, record
-            that fact in the PHI node itself for future use.  */
-         if (bitmap_bit_p (nonzero_vars, SSA_NAME_VERSION (orig)))
-           PHI_ARG_NONZERO (phi, hint) = true;
+         VEC_safe_push (tree, heap, const_and_copies_stack, NULL_TREE);
+         edge_info = (struct edge_info *) false_edge->aux;
 
-         /* If we have *ORIG_P in our constant/copy table, then replace
-            ORIG_P with its value in our constant/copy table.  */
-         new = VARRAY_TREE (const_and_copies, SSA_NAME_VERSION (orig));
-         if (new
-             && (TREE_CODE (new) == SSA_NAME
-                 || is_gimple_min_invariant (new))
-             && may_propagate_copy (orig, new))
+         /* If we have info associated with this edge, record it into
+            our equivalence tables.  */
+         if (edge_info)
            {
-             propagate_value (orig_p, new);
+             struct cond_equivalence *cond_equivalences = edge_info->cond_equivalences;
+             tree lhs = edge_info->lhs;
+             tree rhs = edge_info->rhs;
+
+             /* If we have a simple NAME = VALUE equivalence, record it.  */
+             if (lhs && TREE_CODE (lhs) == SSA_NAME)
+               record_const_or_copy (lhs, rhs);
+
+             /* If we have 0 = COND or 1 = COND equivalences, record them
+                into our expression hash tables.  */
+             if (cond_equivalences)
+               for (i = 0; i < edge_info->max_cond_equivalences; i++)
+                  record_cond (&cond_equivalences[i]);
            }
-       }
-    }
-}
 
+         /* Now thread the edge.  */
+         dom_thread_across_edge (walk_data, false_edge);
 
-/* Propagate known constants/copies into PHI nodes of BB's successor
-   blocks.  */
+         /* No need to remove local expressions from our tables
+            or restore vars to their original value as that will
+            be done immediately below.  */
+       }
+    }
 
-static void
-cprop_into_phis (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
-                basic_block bb)
-{
-  cprop_into_successor_phis (bb, const_and_copies, nonzero_vars);
+  remove_local_expressions_from_table ();
+  restore_vars_to_original_value ();
 }
 
 /* Search for redundant computations in STMT.  If any are found, then
@@ -2365,124 +1771,116 @@ cprop_into_phis (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
    If safe, record this expression into the available expression hash
    table.  */
 
-static bool
-eliminate_redundant_computations (struct dom_walk_data *walk_data,
-                                 tree stmt, stmt_ann_t ann)
+static void
+eliminate_redundant_computations (gimple_stmt_iterator* gsi)
 {
-  v_may_def_optype v_may_defs = V_MAY_DEF_OPS (ann);
-  tree *expr_p, def = NULL_TREE;
-  bool insert = true;
+  tree expr_type;
   tree cached_lhs;
-  bool retval = false;
-  struct dom_walk_block_data *bd
-    = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
+  bool insert = true;
+  bool assigns_var_p = false;
 
-  if (TREE_CODE (stmt) == MODIFY_EXPR)
-    def = TREE_OPERAND (stmt, 0);
+  gimple stmt = gsi_stmt (*gsi);
+
+  tree def = gimple_get_lhs (stmt);
 
   /* Certain expressions on the RHS can be optimized away, but can not
-     themselves be entered into the hash tables.   */
-  if (ann->makes_aliased_stores
-      || ! def
+     themselves be entered into the hash tables.  */
+  if (! def
       || TREE_CODE (def) != SSA_NAME
       || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
-      || NUM_V_MAY_DEFS (v_may_defs) != 0)
+      || gimple_vdef (stmt)
+      /* Do not record equivalences for increments of ivs.  This would create
+        overlapping live ranges for a very questionable gain.  */
+      || simple_iv_increment_p (stmt))
     insert = false;
 
   /* Check if the expression has been computed before.  */
-  cached_lhs = lookup_avail_expr (stmt, &bd->avail_exprs, insert);
-
-  /* If this is an assignment and the RHS was not in the hash table,
-     then try to simplify the RHS and lookup the new RHS in the
-     hash table.  */
-  if (! cached_lhs && TREE_CODE (stmt) == MODIFY_EXPR)
-    cached_lhs = simplify_rhs_and_lookup_avail_expr (walk_data,
-                                                    stmt,
-                                                    insert);
-  /* Similarly if this is a COND_EXPR and we did not find its
-     expression in the hash table, simplify the condition and
-     try again.  */
-  else if (! cached_lhs && TREE_CODE (stmt) == COND_EXPR)
-    cached_lhs = simplify_cond_and_lookup_avail_expr (stmt,
-                                                     &bd->avail_exprs,
-                                                     ann,
-                                                     insert);
-  /* Similarly for a SWITCH_EXPR.  */
-  else if (!cached_lhs && TREE_CODE (stmt) == SWITCH_EXPR)
-    cached_lhs = simplify_switch_and_lookup_avail_expr (stmt,
-                                                       &bd->avail_exprs,
-                                                       insert);
+  cached_lhs = lookup_avail_expr (stmt, insert);
 
   opt_stats.num_exprs_considered++;
 
-  /* Get a pointer to the expression we are trying to optimize.  */
-  if (TREE_CODE (stmt) == COND_EXPR)
-    expr_p = &COND_EXPR_COND (stmt);
-  else if (TREE_CODE (stmt) == SWITCH_EXPR)
-    expr_p = &SWITCH_COND (stmt);
-  else if (TREE_CODE (stmt) == RETURN_EXPR && TREE_OPERAND (stmt, 0))
-    expr_p = &TREE_OPERAND (TREE_OPERAND (stmt, 0), 1);
+  /* Get the type of the expression we are trying to optimize.  */
+  if (is_gimple_assign (stmt))
+    {
+      expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
+      assigns_var_p = true;
+    }
+  else if (gimple_code (stmt) == GIMPLE_COND)
+    expr_type = boolean_type_node;
+  else if (is_gimple_call (stmt))
+    {
+      gcc_assert (gimple_call_lhs (stmt));
+      expr_type = TREE_TYPE (gimple_call_lhs (stmt));
+      assigns_var_p = true;
+    }
+  else if (gimple_code (stmt) == GIMPLE_SWITCH)
+    expr_type = TREE_TYPE (gimple_switch_index (stmt));
   else
-    expr_p = &TREE_OPERAND (stmt, 1);
+    gcc_unreachable ();
+
+  if (!cached_lhs)
+    return;
 
   /* It is safe to ignore types here since we have already done
      type checking in the hashing and equality routines.  In fact
      type checking here merely gets in the way of constant
      propagation.  Also, make sure that it is safe to propagate
-     CACHED_LHS into *EXPR_P.  */
-  if (cached_lhs
-      && (TREE_CODE (cached_lhs) != SSA_NAME
-         || may_propagate_copy (*expr_p, cached_lhs)))
-    {
+     CACHED_LHS into the expression in STMT.  */
+  if ((TREE_CODE (cached_lhs) != SSA_NAME
+       && (assigns_var_p
+           || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
+      || may_propagate_copy_into_stmt (stmt, cached_lhs))
+  {
+#if defined ENABLE_CHECKING
+      gcc_assert (TREE_CODE (cached_lhs) == SSA_NAME
+                 || is_gimple_min_invariant (cached_lhs));
+#endif
+
       if (dump_file && (dump_flags & TDF_DETAILS))
        {
          fprintf (dump_file, "  Replaced redundant expr '");
-         print_generic_expr (dump_file, *expr_p, dump_flags);
+         print_gimple_expr (dump_file, stmt, 0, dump_flags);
          fprintf (dump_file, "' with '");
          print_generic_expr (dump_file, cached_lhs, dump_flags);
-          fprintf (dump_file, "'\n");
+          fprintf (dump_file, "'\n");
        }
 
       opt_stats.num_re++;
 
-#if defined ENABLE_CHECKING
-      if (TREE_CODE (cached_lhs) != SSA_NAME
-         && !is_gimple_min_invariant (cached_lhs))
-       abort ();
-#endif
+      if (assigns_var_p
+         && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
+       cached_lhs = fold_convert (expr_type, cached_lhs);
 
-      if (TREE_CODE (cached_lhs) == ADDR_EXPR
-         || (POINTER_TYPE_P (TREE_TYPE (*expr_p))
-             && is_gimple_min_invariant (cached_lhs)))
-       retval = true;
+      propagate_tree_value_into_stmt (gsi, cached_lhs);
 
-      propagate_tree_value (expr_p, cached_lhs);
-      modify_stmt (stmt);
-    }
-  return retval;
+      /* Since it is always necessary to mark the result as modified,
+         perhaps we should move this into propagate_tree_value_into_stmt
+         itself.  */
+      gimple_set_modified (gsi_stmt (*gsi), true);
+  }
 }
 
-/* STMT, a MODIFY_EXPR, may create certain equivalences, in either
+/* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
    the available expressions table or the const_and_copies table.
    Detect and record those equivalences.  */
+/* We handle only very simple copy equivalences here.  The heavy
+   lifing is done by eliminate_redundant_computations.  */
 
 static void
-record_equivalences_from_stmt (tree stmt,
-                              varray_type *block_avail_exprs_p,
-                              varray_type *block_nonzero_vars_p,
-                              int may_optimize_p,
-                              stmt_ann_t ann)
+record_equivalences_from_stmt (gimple stmt, int may_optimize_p)
 {
-  tree lhs = TREE_OPERAND (stmt, 0);
-  enum tree_code lhs_code = TREE_CODE (lhs);
-  int i;
+  tree lhs;
+  enum tree_code lhs_code;
 
-  if (lhs_code == SSA_NAME)
-    {
-      tree rhs = TREE_OPERAND (stmt, 1);
+  gcc_assert (is_gimple_assign (stmt));
+
+  lhs = gimple_assign_lhs (stmt);
+  lhs_code = TREE_CODE (lhs);
 
-      /* Strip away any useless type conversions.  */
-      STRIP_USELESS_TYPE_CONVERSION (rhs);
+  if (lhs_code == SSA_NAME
+      && gimple_assign_single_p (stmt))
+    {
+      tree rhs = gimple_assign_rhs1 (stmt);
 
       /* If the RHS of the assignment is a constant or another variable that
         may be propagated, register it in the CONST_AND_COPIES table.  We
@@ -2493,172 +1891,119 @@ record_equivalences_from_stmt (tree stmt,
       if (may_optimize_p
          && (TREE_CODE (rhs) == SSA_NAME
              || is_gimple_min_invariant (rhs)))
-       set_value_for (lhs, rhs, const_and_copies);
-
-      /* alloca never returns zero and the address of a non-weak symbol
-        is never zero.  NOP_EXPRs and CONVERT_EXPRs can be completely
-        stripped as they do not affect this equivalence.  */
-      while (TREE_CODE (rhs) == NOP_EXPR
-            || TREE_CODE (rhs) == CONVERT_EXPR)
-        rhs = TREE_OPERAND (rhs, 0);
-
-      if (alloca_call_p (rhs)
-          || (TREE_CODE (rhs) == ADDR_EXPR
-             && DECL_P (TREE_OPERAND (rhs, 0))
-             && ! DECL_WEAK (TREE_OPERAND (rhs, 0))))
-       record_var_is_nonzero (lhs, block_nonzero_vars_p);
-
-      /* IOR of any value with a nonzero value will result in a nonzero
-        value.  Even if we do not know the exact result recording that
-        the result is nonzero is worth the effort.  */
-      if (TREE_CODE (rhs) == BIT_IOR_EXPR
-         && integer_nonzerop (TREE_OPERAND (rhs, 1)))
-       record_var_is_nonzero (lhs, block_nonzero_vars_p);
-    }
-
-  /* Look at both sides for pointer dereferences.  If we find one, then
-     the pointer must be nonnull and we can enter that equivalence into
-     the hash tables.  */
-  if (flag_delete_null_pointer_checks)
-    for (i = 0; i < 2; i++)
       {
-       tree t = TREE_OPERAND (stmt, i);
-
-       /* Strip away any COMPONENT_REFs.  */
-       while (TREE_CODE (t) == COMPONENT_REF)
-         t = TREE_OPERAND (t, 0);
-
-       /* Now see if this is a pointer dereference.  */
-       if (TREE_CODE (t) == INDIRECT_REF)
-          {
-           tree op = TREE_OPERAND (t, 0);
-
-           /* If the pointer is a SSA variable, then enter new
-              equivalences into the hash table.  */
-           while (TREE_CODE (op) == SSA_NAME)
-             {
-               tree def = SSA_NAME_DEF_STMT (op);
-
-               record_var_is_nonzero (op, block_nonzero_vars_p);
-
-               /* And walk up the USE-DEF chains noting other SSA_NAMEs
-                  which are known to have a nonzero value.  */
-               if (def
-                   && TREE_CODE (def) == MODIFY_EXPR
-                   && TREE_CODE (TREE_OPERAND (def, 1)) == NOP_EXPR)
-                 op = TREE_OPERAND (TREE_OPERAND (def, 1), 0);
-               else
-                 break;
-             }
+       if (dump_file && (dump_flags & TDF_DETAILS))
+         {
+           fprintf (dump_file, "==== ASGN ");
+           print_generic_expr (dump_file, lhs, 0);
+           fprintf (dump_file, " = ");
+           print_generic_expr (dump_file, rhs, 0);
+           fprintf (dump_file, "\n");
          }
+
+       set_ssa_name_value (lhs, rhs);
       }
+    }
 
   /* A memory store, even an aliased store, creates a useful
      equivalence.  By exchanging the LHS and RHS, creating suitable
      vops and recording the result in the available expression table,
      we may be able to expose more redundant loads.  */
-  if (!ann->has_volatile_ops
-      && (TREE_CODE (TREE_OPERAND (stmt, 1)) == SSA_NAME
-         || is_gimple_min_invariant (TREE_OPERAND (stmt, 1)))
+  if (!gimple_has_volatile_ops (stmt)
+      && gimple_references_memory_p (stmt)
+      && gimple_assign_single_p (stmt)
+      && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
+         || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
       && !is_gimple_reg (lhs))
     {
-      tree rhs = TREE_OPERAND (stmt, 1);
-      tree new;
-
-      /* FIXME: If the LHS of the assignment is a bitfield and the RHS
-         is a constant, we need to adjust the constant to fit into the
-         type of the LHS.  If the LHS is a bitfield and the RHS is not
-        a constant, then we can not record any equivalences for this
-        statement since we would need to represent the widening or
-        narrowing of RHS.  This fixes gcc.c-torture/execute/921016-1.c
-        and should not be necessary if GCC represented bitfields
-        properly.  */
-      if (lhs_code == COMPONENT_REF
-         && DECL_BIT_FIELD (TREE_OPERAND (lhs, 1)))
-       {
-         if (TREE_CONSTANT (rhs))
-           rhs = widen_bitfield (rhs, TREE_OPERAND (lhs, 1), lhs);
-         else
-           rhs = NULL;
-
-         /* If the value overflowed, then we can not use this equivalence.  */
-         if (rhs && ! is_gimple_min_invariant (rhs))
-           rhs = NULL;
-       }
-
-      if (rhs)
-       {
-         /* Build a new statement with the RHS and LHS exchanged.  */
-         new = build (MODIFY_EXPR, TREE_TYPE (stmt), rhs, lhs);
+      tree rhs = gimple_assign_rhs1 (stmt);
+      gimple new_stmt;
+
+      /* Build a new statement with the RHS and LHS exchanged.  */
+      if (TREE_CODE (rhs) == SSA_NAME)
+        {
+          /* NOTE tuples.  The call to gimple_build_assign below replaced
+             a call to build_gimple_modify_stmt, which did not set the
+             SSA_NAME_DEF_STMT on the LHS of the assignment.  Doing so
+             may cause an SSA validation failure, as the LHS may be a
+             default-initialized name and should have no definition.  I'm
+             a bit dubious of this, as the artificial statement that we
+             generate here may in fact be ill-formed, but it is simply
+             used as an internal device in this pass, and never becomes
+             part of the CFG.  */
+          gimple defstmt = SSA_NAME_DEF_STMT (rhs);
+          new_stmt = gimple_build_assign (rhs, lhs);
+          SSA_NAME_DEF_STMT (rhs) = defstmt;
+        }
+      else
+        new_stmt = gimple_build_assign (rhs, lhs);
 
-         create_ssa_artficial_load_stmt (&(ann->operands), new);
+      gimple_set_vuse (new_stmt, gimple_vdef (stmt));
 
-         /* Finally enter the statement into the available expression
-            table.  */
-         lookup_avail_expr (new, block_avail_exprs_p, true);
-       }
+      /* Finally enter the statement into the available expression
+        table.  */
+      lookup_avail_expr (new_stmt, true);
     }
 }
 
 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
    CONST_AND_COPIES.  */
 
-static bool
-cprop_operand (tree stmt, use_operand_p op_p, varray_type const_and_copies)
+static void
+cprop_operand (gimple stmt, use_operand_p op_p)
 {
-  bool may_have_exposed_new_symbols = false;
   tree val;
   tree op = USE_FROM_PTR (op_p);
 
   /* If the operand has a known constant value or it is known to be a
      copy of some other variable, use the value or copy stored in
      CONST_AND_COPIES.  */
-  val = VARRAY_TREE (const_and_copies, SSA_NAME_VERSION (op));
-  if (val)
+  val = SSA_NAME_VALUE (op);
+  if (val && val != op)
     {
-      tree op_type, val_type;
-
       /* Do not change the base variable in the virtual operand
         tables.  That would make it impossible to reconstruct
         the renamed virtual operand if we later modify this
         statement.  Also only allow the new value to be an SSA_NAME
-        for propagation into virtual operands.  */
-      if (!is_gimple_reg (op)
-         && (get_virtual_var (val) != get_virtual_var (op)
-             || TREE_CODE (val) != SSA_NAME))
-       return false;
-
-      /* Get the toplevel type of each operand.  */
-      op_type = TREE_TYPE (op);
-      val_type = TREE_TYPE (val);
-
-      /* While both types are pointers, get the type of the object
-        pointed to.  */
-      while (POINTER_TYPE_P (op_type) && POINTER_TYPE_P (val_type))
-       {
-         op_type = TREE_TYPE (op_type);
-         val_type = TREE_TYPE (val_type);
-       }
+        for propagation into virtual operands.  */
+      if (!is_gimple_reg (op)
+         && (TREE_CODE (val) != SSA_NAME
+             || is_gimple_reg (val)
+             || get_virtual_var (val) != get_virtual_var (op)))
+       return;
 
-      /* Make sure underlying types match before propagating a constant by
-        converting the constant to the proper type.  Note that convert may
-        return a non-gimple expression, in which case we ignore this
-        propagation opportunity.  */
-      if (TREE_CODE (val) != SSA_NAME)
-       {
-         if (!lang_hooks.types_compatible_p (op_type, val_type))
-           {
-             val = fold_convert (TREE_TYPE (op), val);
-             if (!is_gimple_min_invariant (val))
-               return false;
-           }
-       }
+      /* Do not replace hard register operands in asm statements.  */
+      if (gimple_code (stmt) == GIMPLE_ASM
+         && !may_propagate_copy_into_asm (op))
+       return;
 
       /* Certain operands are not allowed to be copy propagated due
         to their interaction with exception handling and some GCC
         extensions.  */
-      else if (!may_propagate_copy (op, val))
-       return false;
+      if (!may_propagate_copy (op, val))
+       return;
+
+      /* Do not propagate addresses that point to volatiles into memory
+        stmts without volatile operands.  */
+      if (POINTER_TYPE_P (TREE_TYPE (val))
+         && TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (val)))
+         && gimple_has_mem_ops (stmt)
+         && !gimple_has_volatile_ops (stmt))
+       return;
+
+      /* Do not propagate copies if the propagated value is at a deeper loop
+        depth than the propagatee.  Otherwise, this may move loop variant
+        variables outside of their loops and prevent coalescing
+        opportunities.  If the value was loop invariant, it will be hoisted
+        by LICM and exposed for copy propagation.  */
+      if (loop_depth_of_name (val) > loop_depth_of_name (op))
+       return;
+
+      /* Do not propagate copies into simple IV increment statements.
+         See PR23821 for how this can disturb IV analysis.  */
+      if (TREE_CODE (val) != INTEGER_CST
+         && simple_iv_increment_p (stmt))
+       return;
 
       /* Dump details.  */
       if (dump_file && (dump_flags & TDF_DETAILS))
@@ -2671,49 +2016,41 @@ cprop_operand (tree stmt, use_operand_p op_p, varray_type const_and_copies)
          fprintf (dump_file, "'\n");
        }
 
-      /* If VAL is an ADDR_EXPR or a constant of pointer type, note
-        that we may have exposed a new symbol for SSA renaming.  */
-      if (TREE_CODE (val) == ADDR_EXPR
-         || (POINTER_TYPE_P (TREE_TYPE (op))
-             && is_gimple_min_invariant (val)))
-       may_have_exposed_new_symbols = true;
+      if (TREE_CODE (val) != SSA_NAME)
+       opt_stats.num_const_prop++;
+      else
+       opt_stats.num_copy_prop++;
 
       propagate_value (op_p, val);
 
       /* And note that we modified this statement.  This is now
         safe, even if we changed virtual operands since we will
         rescan the statement and rewrite its operands again.  */
-      modify_stmt (stmt);
+      gimple_set_modified (stmt, true);
     }
-  return may_have_exposed_new_symbols;
 }
 
 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
-   known value for that SSA_NAME (or NULL if no value is known).  
+   known value for that SSA_NAME (or NULL if no value is known).
 
    Propagate values from CONST_AND_COPIES into the uses, vuses and
-   v_may_def_ops of STMT.  */
+   vdef_ops of STMT.  */
 
-static bool
-cprop_into_stmt (tree stmt, varray_type const_and_copies)
+static void
+cprop_into_stmt (gimple stmt)
 {
-  bool may_have_exposed_new_symbols = false;
   use_operand_p op_p;
   ssa_op_iter iter;
 
   FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_ALL_USES)
     {
       if (TREE_CODE (USE_FROM_PTR (op_p)) == SSA_NAME)
-       may_have_exposed_new_symbols
-         |= cprop_operand (stmt, op_p, const_and_copies);
+       cprop_operand (stmt, op_p);
     }
-
-  return may_have_exposed_new_symbols;
 }
 
+/* Optimize the statement pointed to by iterator SI.
 
-/* Optimize the statement pointed by iterator SI.
-   
    We try to perform some simplistic global redundancy elimination and
    constant propagation:
 
@@ -2728,90 +2065,109 @@ cprop_into_stmt (tree stmt, varray_type const_and_copies)
       the variable in the LHS in the CONST_AND_COPIES table.  */
 
 static void
-optimize_stmt (struct dom_walk_data *walk_data, basic_block bb,
-              block_stmt_iterator si)
+optimize_stmt (basic_block bb, gimple_stmt_iterator si)
 {
-  stmt_ann_t ann;
-  tree stmt;
+  gimple stmt, old_stmt;
   bool may_optimize_p;
-  bool may_have_exposed_new_symbols = false;
-  struct dom_walk_block_data *bd
-    = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
+  bool modified_p = false;
+
+  old_stmt = stmt = gsi_stmt (si);
 
-  stmt = bsi_stmt (si);
+  if (gimple_code (stmt) == GIMPLE_COND)
+    canonicalize_comparison (stmt);
 
-  get_stmt_operands (stmt);
-  ann = stmt_ann (stmt);
+  update_stmt_if_modified (stmt);
   opt_stats.num_stmts++;
-  may_have_exposed_new_symbols = false;
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
       fprintf (dump_file, "Optimizing statement ");
-      print_generic_stmt (dump_file, stmt, TDF_SLIM);
+      print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
     }
 
-  /* Const/copy propagate into USES, VUSES and the RHS of V_MAY_DEFs.  */
-  may_have_exposed_new_symbols = cprop_into_stmt (stmt, const_and_copies);
+  /* Const/copy propagate into USES, VUSES and the RHS of VDEFs.  */
+  cprop_into_stmt (stmt);
 
   /* If the statement has been modified with constant replacements,
      fold its RHS before checking for redundant computations.  */
-  if (ann->modified)
+  if (gimple_modified_p (stmt))
     {
+      tree rhs = NULL;
+
       /* Try to fold the statement making sure that STMT is kept
         up to date.  */
-      if (fold_stmt (bsi_stmt_ptr (si)))
+      if (fold_stmt (&si))
        {
-         stmt = bsi_stmt (si);
-         ann = stmt_ann (stmt);
+         stmt = gsi_stmt (si);
+         gimple_set_modified (stmt, true);
 
          if (dump_file && (dump_flags & TDF_DETAILS))
            {
              fprintf (dump_file, "  Folded to: ");
-             print_generic_stmt (dump_file, stmt, TDF_SLIM);
+             print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
            }
        }
 
-      /* Constant/copy propagation above may change the set of 
-        virtual operands associated with this statement.  Folding
-        may remove the need for some virtual operands.
-
-        Indicate we will need to rescan and rewrite the statement.  */
-      may_have_exposed_new_symbols = true;
+      /* We only need to consider cases that can yield a gimple operand.  */
+      if (gimple_assign_single_p (stmt))
+        rhs = gimple_assign_rhs1 (stmt);
+      else if (gimple_code (stmt) == GIMPLE_GOTO)
+        rhs = gimple_goto_dest (stmt);
+      else if (gimple_code (stmt) == GIMPLE_SWITCH)
+        /* This should never be an ADDR_EXPR.  */
+        rhs = gimple_switch_index (stmt);
+
+      if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
+        recompute_tree_invariant_for_addr_expr (rhs);
+
+      /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
+        even if fold_stmt updated the stmt already and thus cleared
+        gimple_modified_p flag on it.  */
+      modified_p = true;
     }
 
   /* Check for redundant computations.  Do this optimization only
      for assignments that have no volatile ops and conditionals.  */
-  may_optimize_p = (!ann->has_volatile_ops
-                   && ((TREE_CODE (stmt) == RETURN_EXPR
-                        && TREE_OPERAND (stmt, 0)
-                        && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR
-                        && ! (TREE_SIDE_EFFECTS
-                              (TREE_OPERAND (TREE_OPERAND (stmt, 0), 1))))
-                       || (TREE_CODE (stmt) == MODIFY_EXPR
-                           && ! TREE_SIDE_EFFECTS (TREE_OPERAND (stmt, 1)))
-                       || TREE_CODE (stmt) == COND_EXPR
-                       || TREE_CODE (stmt) == SWITCH_EXPR));
+  may_optimize_p = (!gimple_has_volatile_ops (stmt)
+                    && ((is_gimple_assign (stmt)
+                         && !gimple_rhs_has_side_effects (stmt))
+                        || (is_gimple_call (stmt)
+                            && gimple_call_lhs (stmt) != NULL_TREE
+                            && !gimple_rhs_has_side_effects (stmt))
+                        || gimple_code (stmt) == GIMPLE_COND
+                        || gimple_code (stmt) == GIMPLE_SWITCH));
 
   if (may_optimize_p)
-    may_have_exposed_new_symbols
-      |= eliminate_redundant_computations (walk_data, stmt, ann);
+    {
+      if (gimple_code (stmt) == GIMPLE_CALL)
+       {
+         /* Resolve __builtin_constant_p.  If it hasn't been
+            folded to integer_one_node by now, it's fairly
+            certain that the value simply isn't constant.  */
+         tree callee = gimple_call_fndecl (stmt);
+         if (callee
+             && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL
+             && DECL_FUNCTION_CODE (callee) == BUILT_IN_CONSTANT_P)
+           {
+             propagate_tree_value_into_stmt (&si, integer_zero_node);
+             stmt = gsi_stmt (si);
+           }
+       }
 
-  /* Record any additional equivalences created by this statement.  */
-  if (TREE_CODE (stmt) == MODIFY_EXPR)
-    record_equivalences_from_stmt (stmt,
-                                  &bd->avail_exprs,
-                                  &bd->nonzero_vars,
-                                  may_optimize_p,
-                                  ann);
+      update_stmt_if_modified (stmt);
+      eliminate_redundant_computations (&si);
+      stmt = gsi_stmt (si);
+    }
 
-  register_definitions_for_stmt (stmt, &bd->block_defs);
+  /* Record any additional equivalences created by this statement.  */
+  if (is_gimple_assign (stmt))
+    record_equivalences_from_stmt (stmt, may_optimize_p);
 
   /* If STMT is a COND_EXPR and it was modified, then we may know
      where it goes.  If that is the case, then mark the CFG as altered.
 
      This will cause us to later call remove_unreachable_blocks and
-     cleanup_tree_cfg when it is safe to do so.  It is not safe to 
+     cleanup_tree_cfg when it is safe to do so.  It is not safe to
      clean things up here since removal of edges and such can trigger
      the removal of PHI nodes, which in turn can release SSA_NAMEs to
      the manager.
@@ -2832,152 +2188,71 @@ optimize_stmt (struct dom_walk_data *walk_data, basic_block bb,
 
      Ultimately I suspect we're going to need to change the interface
      into the SSA_NAME manager.  */
-
-  if (ann->modified)
+  if (gimple_modified_p (stmt) || modified_p)
     {
       tree val = NULL;
 
-      if (TREE_CODE (stmt) == COND_EXPR)
-       val = COND_EXPR_COND (stmt);
-      else if (TREE_CODE (stmt) == SWITCH_EXPR)
-       val = SWITCH_COND (stmt);
+      update_stmt_if_modified (stmt);
+
+      if (gimple_code (stmt) == GIMPLE_COND)
+        val = fold_binary_loc (gimple_location (stmt),
+                          gimple_cond_code (stmt), boolean_type_node,
+                           gimple_cond_lhs (stmt),  gimple_cond_rhs (stmt));
+      else if (gimple_code (stmt) == GIMPLE_SWITCH)
+       val = gimple_switch_index (stmt);
 
       if (val && TREE_CODE (val) == INTEGER_CST && find_taken_edge (bb, val))
        cfg_altered = true;
 
       /* If we simplified a statement in such a way as to be shown that it
         cannot trap, update the eh information and the cfg to match.  */
-      if (maybe_clean_eh_stmt (stmt))
+      if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
        {
          bitmap_set_bit (need_eh_cleanup, bb->index);
          if (dump_file && (dump_flags & TDF_DETAILS))
            fprintf (dump_file, "  Flagged to clear EH edges.\n");
        }
     }
-
-  if (may_have_exposed_new_symbols)
-    {
-      if (! bd->stmts_to_rescan)
-       VARRAY_TREE_INIT (bd->stmts_to_rescan, 20, "stmts_to_rescan");
-      VARRAY_PUSH_TREE (bd->stmts_to_rescan, bsi_stmt (si));
-    }
-}
-
-/* Replace the RHS of STMT with NEW_RHS.  If RHS can be found in the
-   available expression hashtable, then return the LHS from the hash
-   table.
-
-   If INSERT is true, then we also update the available expression
-   hash table to account for the changes made to STMT.  */
-
-static tree
-update_rhs_and_lookup_avail_expr (tree stmt, tree new_rhs, 
-                                 varray_type *block_avail_exprs_p,
-                                 bool insert)
-{
-  tree cached_lhs = NULL;
-
-  /* Remove the old entry from the hash table.  */
-  if (insert)
-    {
-      struct expr_hash_elt element;
-
-      initialize_hash_element (stmt, NULL, &element);
-      htab_remove_elt_with_hash (avail_exprs, &element, element.hash);
-    }
-
-  /* Now update the RHS of the assignment.  */
-  TREE_OPERAND (stmt, 1) = new_rhs;
-
-  /* Now lookup the updated statement in the hash table.  */
-  cached_lhs = lookup_avail_expr (stmt, block_avail_exprs_p, insert);
-
-  /* We have now called lookup_avail_expr twice with two different
-     versions of this same statement, once in optimize_stmt, once here.
-
-     We know the call in optimize_stmt did not find an existing entry
-     in the hash table, so a new entry was created.  At the same time
-     this statement was pushed onto the BLOCK_AVAIL_EXPRS varray. 
-
-     If this call failed to find an existing entry on the hash table,
-     then the new version of this statement was entered into the
-     hash table.  And this statement was pushed onto BLOCK_AVAIL_EXPR
-     for the second time.  So there are two copies on BLOCK_AVAIL_EXPRs
-
-     If this call succeeded, we still have one copy of this statement
-     on the BLOCK_AVAIL_EXPRs varray.
-
-     For both cases, we need to pop the most recent entry off the
-     BLOCK_AVAIL_EXPRs varray.  For the case where we never found this
-     statement in the hash tables, that will leave precisely one
-     copy of this statement on BLOCK_AVAIL_EXPRs.  For the case where
-     we found a copy of this statement in the second hash table lookup
-     we want _no_ copies of this statement in BLOCK_AVAIL_EXPRs.  */
-  if (insert)
-    VARRAY_POP (*block_avail_exprs_p);
-
-  /* And make sure we record the fact that we modified this
-     statement.  */
-  modify_stmt (stmt);
-
-  return cached_lhs;
 }
 
-/* Search for an existing instance of STMT in the AVAIL_EXPRS table.  If
-   found, return its LHS. Otherwise insert STMT in the table and return
-   NULL_TREE.
-
-   Also, when an expression is first inserted in the AVAIL_EXPRS table, it
-   is also added to the stack pointed by BLOCK_AVAIL_EXPRS_P, so that they
-   can be removed when we finish processing this block and its children.
+/* Search for an existing instance of STMT in the AVAIL_EXPRS table.
+   If found, return its LHS. Otherwise insert STMT in the table and
+   return NULL_TREE.
 
-   NOTE: This function assumes that STMT is a MODIFY_EXPR node that
-   contains no CALL_EXPR on its RHS and makes no volatile nor
-   aliased references.  */
+   Also, when an expression is first inserted in the  table, it is also
+   is also added to AVAIL_EXPRS_STACK, so that it can be removed when
+   we finish processing this block and its children.  */
 
 static tree
-lookup_avail_expr (tree stmt, varray_type *block_avail_exprs_p, bool insert)
+lookup_avail_expr (gimple stmt, bool insert)
 {
   void **slot;
   tree lhs;
   tree temp;
-  struct expr_hash_elt *element = xcalloc (sizeof (struct expr_hash_elt), 1);
+  struct expr_hash_elt *element = XNEW (struct expr_hash_elt);
 
-  lhs = TREE_CODE (stmt) == MODIFY_EXPR ? TREE_OPERAND (stmt, 0) : NULL;
+  /* Get LHS of assignment or call, else NULL_TREE.  */
+  lhs = gimple_get_lhs (stmt);
 
   initialize_hash_element (stmt, lhs, element);
 
+  if (dump_file && (dump_flags & TDF_DETAILS))
+    {
+      fprintf (dump_file, "LKUP ");
+      print_expr_hash_elt (dump_file, element);
+    }
+
   /* Don't bother remembering constant assignments and copy operations.
      Constants and copy operations are handled by the constant/copy propagator
      in optimize_stmt.  */
-  if (TREE_CODE (element->rhs) == SSA_NAME
-      || is_gimple_min_invariant (element->rhs))
+  if (element->expr.kind == EXPR_SINGLE
+      && (TREE_CODE (element->expr.ops.single.rhs) == SSA_NAME
+          || is_gimple_min_invariant (element->expr.ops.single.rhs)))
     {
       free (element);
       return NULL_TREE;
     }
 
-  /* If this is an equality test against zero, see if we have recorded a
-     nonzero value for the variable in question.  */
-  if ((TREE_CODE (element->rhs) == EQ_EXPR
-       || TREE_CODE  (element->rhs) == NE_EXPR)
-      && TREE_CODE (TREE_OPERAND (element->rhs, 0)) == SSA_NAME
-      && integer_zerop (TREE_OPERAND (element->rhs, 1)))
-    {
-      int indx = SSA_NAME_VERSION (TREE_OPERAND (element->rhs, 0));
-
-      if (bitmap_bit_p (nonzero_vars, indx))
-       {
-         tree t = element->rhs;
-         free (element);
-
-         if (TREE_CODE (t) == EQ_EXPR)
-           return boolean_false_node;
-         else
-           return boolean_true_node;
-       }
-    }
-
   /* Finally try to find the expression in the main expression hash table.  */
   slot = htab_find_slot_with_hash (avail_exprs, element, element->hash,
                                   (insert ? INSERT : NO_INSERT));
@@ -2990,9 +2265,14 @@ lookup_avail_expr (tree stmt, varray_type *block_avail_exprs_p, bool insert)
   if (*slot == NULL)
     {
       *slot = (void *) element;
-      if (! *block_avail_exprs_p)
-        VARRAY_TREE_INIT (*block_avail_exprs_p, 20, "block_avail_exprs");
-      VARRAY_PUSH_TREE (*block_avail_exprs_p, stmt ? stmt : element->rhs);
+
+      if (dump_file && (dump_flags & TDF_DETAILS))
+        {
+          fprintf (dump_file, "2>>> ");
+          print_expr_hash_elt (dump_file, element);
+        }
+
+      VEC_safe_push (expr_hash_elt_t, heap, avail_exprs_stack, element);
       return NULL_TREE;
     }
 
@@ -3004,361 +2284,610 @@ lookup_avail_expr (tree stmt, varray_type *block_avail_exprs_p, bool insert)
      use the value from the const_and_copies table.  */
   if (TREE_CODE (lhs) == SSA_NAME)
     {
-      temp = get_value_for (lhs, const_and_copies);
+      temp = SSA_NAME_VALUE (lhs);
       if (temp)
        lhs = temp;
     }
 
   free (element);
+
+  if (dump_file && (dump_flags & TDF_DETAILS))
+    {
+      fprintf (dump_file, "FIND: ");
+      print_generic_expr (dump_file, lhs, 0);
+      fprintf (dump_file, "\n");
+    }
+
   return lhs;
 }
 
-/* Given a condition COND, record into HI_P, LO_P and INVERTED_P the
-   range of values that result in the conditional having a true value.
-
-   Return true if we are successful in extracting a range from COND and
-   false if we are unsuccessful.  */
+/* Hashing and equality functions for AVAIL_EXPRS.  We compute a value number
+   for expressions using the code of the expression and the SSA numbers of
+   its operands.  */
 
-static bool
-extract_range_from_cond (tree cond, tree *hi_p, tree *lo_p, int *inverted_p)
+static hashval_t
+avail_expr_hash (const void *p)
 {
-  tree op1 = TREE_OPERAND (cond, 1);
-  tree high, low, type;
-  int inverted;
-  
-  /* Experiments have shown that it's rarely, if ever useful to
-     record ranges for enumerations.  Presumably this is due to
-     the fact that they're rarely used directly.  They are typically
-     cast into an integer type and used that way.  */
-  if (TREE_CODE (TREE_TYPE (op1)) != INTEGER_TYPE)
-    return 0;
+  gimple stmt = ((const struct expr_hash_elt *)p)->stmt;
+  const struct hashable_expr *expr = &((const struct expr_hash_elt *)p)->expr;
+  tree vuse;
+  hashval_t val = 0;
 
-  type = TREE_TYPE (op1);
+  val = iterative_hash_hashable_expr (expr, val);
 
-  switch (TREE_CODE (cond))
-    {
-    case EQ_EXPR:
-      high = low = op1;
-      inverted = 0;
-      break;
+  /* If the hash table entry is not associated with a statement, then we
+     can just hash the expression and not worry about virtual operands
+     and such.  */
+  if (!stmt)
+    return val;
 
-    case NE_EXPR:
-      high = low = op1;
-      inverted = 1;
-      break;
+  /* Add the SSA version numbers of the vuse operand.  This is important
+     because compound variables like arrays are not renamed in the
+     operands.  Rather, the rename is done on the virtual variable
+     representing all the elements of the array.  */
+  if ((vuse = gimple_vuse (stmt)))
+    val = iterative_hash_expr (vuse, val);
 
-    case GE_EXPR:
-      low = op1;
-      high = TYPE_MAX_VALUE (type);
-      inverted = 0;
-      break;
+  return val;
+}
 
-    case GT_EXPR:
-      low = int_const_binop (PLUS_EXPR, op1, integer_one_node, 1);
-      high = TYPE_MAX_VALUE (type);
-      inverted = 0;
-      break;
+static hashval_t
+real_avail_expr_hash (const void *p)
+{
+  return ((const struct expr_hash_elt *)p)->hash;
+}
 
-    case LE_EXPR:
-      high = op1;
-      low = TYPE_MIN_VALUE (type);
-      inverted = 0;
-      break;
+static int
+avail_expr_eq (const void *p1, const void *p2)
+{
+  gimple stmt1 = ((const struct expr_hash_elt *)p1)->stmt;
+  const struct hashable_expr *expr1 = &((const struct expr_hash_elt *)p1)->expr;
+  const struct expr_hash_elt *stamp1 = ((const struct expr_hash_elt *)p1)->stamp;
+  gimple stmt2 = ((const struct expr_hash_elt *)p2)->stmt;
+  const struct hashable_expr *expr2 = &((const struct expr_hash_elt *)p2)->expr;
+  const struct expr_hash_elt *stamp2 = ((const struct expr_hash_elt *)p2)->stamp;
+
+  /* This case should apply only when removing entries from the table.  */
+  if (stamp1 == stamp2)
+    return true;
 
-    case LT_EXPR:
-      high = int_const_binop (MINUS_EXPR, op1, integer_one_node, 1);
-      low = TYPE_MIN_VALUE (type);
-      inverted = 0;
-      break;
+  /* FIXME tuples:
+     We add stmts to a hash table and them modify them. To detect the case
+     that we modify a stmt and then search for it, we assume that the hash
+     is always modified by that change.
+     We have to fully check why this doesn't happen on trunk or rewrite
+     this in a more  reliable (and easier to understand) way. */
+  if (((const struct expr_hash_elt *)p1)->hash
+      != ((const struct expr_hash_elt *)p2)->hash)
+    return false;
 
-    default:
-      return 0;
+  /* In case of a collision, both RHS have to be identical and have the
+     same VUSE operands.  */
+  if (hashable_expr_equal_p (expr1, expr2)
+      && types_compatible_p (expr1->type, expr2->type))
+    {
+      /* Note that STMT1 and/or STMT2 may be NULL.  */
+      return ((stmt1 ? gimple_vuse (stmt1) : NULL_TREE)
+             == (stmt2 ? gimple_vuse (stmt2) : NULL_TREE));
     }
 
-  *hi_p = high;
-  *lo_p = low;
-  *inverted_p = inverted;
-  return 1;
+  return false;
 }
 
-/* Record a range created by COND for basic block BB.  */
+/* PHI-ONLY copy and constant propagation.  This pass is meant to clean
+   up degenerate PHIs created by or exposed by jump threading.  */
 
-static void
-record_range (tree cond, basic_block bb, varray_type *vrp_variables_p)
+/* Given PHI, return its RHS if the PHI is a degenerate, otherwise return
+   NULL.  */
+
+tree
+degenerate_phi_result (gimple phi)
 {
-  /* We explicitly ignore NE_EXPRs.  They rarely allow for meaningful
-     range optimizations and significantly complicate the implementation.  */
-  if (TREE_CODE_CLASS (TREE_CODE (cond)) == '<'
-      && TREE_CODE (cond) != NE_EXPR
-      && TREE_CODE (TREE_TYPE (TREE_OPERAND (cond, 1))) == INTEGER_TYPE)
+  tree lhs = gimple_phi_result (phi);
+  tree val = NULL;
+  size_t i;
+
+  /* Ignoring arguments which are the same as LHS, if all the remaining
+     arguments are the same, then the PHI is a degenerate and has the
+     value of that common argument.  */
+  for (i = 0; i < gimple_phi_num_args (phi); i++)
     {
-      struct vrp_element *element = ggc_alloc (sizeof (struct vrp_element));
-      int ssa_version = SSA_NAME_VERSION (TREE_OPERAND (cond, 0));
+      tree arg = gimple_phi_arg_def (phi, i);
+
+      if (arg == lhs)
+       continue;
+      else if (!arg)
+       break;
+      else if (!val)
+       val = arg;
+      else if (arg == val)
+       continue;
+      /* We bring in some of operand_equal_p not only to speed things
+        up, but also to avoid crashing when dereferencing the type of
+        a released SSA name.  */
+      else if (TREE_CODE (val) != TREE_CODE (arg)
+              || TREE_CODE (val) == SSA_NAME
+              || !operand_equal_p (arg, val, 0))
+       break;
+    }
+  return (i == gimple_phi_num_args (phi) ? val : NULL);
+}
 
-      varray_type *vrp_records_p
-       = (varray_type *)&VARRAY_GENERIC_PTR (vrp_data, ssa_version);
+/* Given a statement STMT, which is either a PHI node or an assignment,
+   remove it from the IL.  */
 
-      element->low = NULL;
-      element->high = NULL;
-      element->cond = cond;
-      element->bb = bb;
+static void
+remove_stmt_or_phi (gimple stmt)
+{
+  gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
 
-      if (*vrp_records_p == NULL)
-       {
-         VARRAY_GENERIC_PTR_INIT (*vrp_records_p, 2, "vrp records");
-         VARRAY_GENERIC_PTR (vrp_data, ssa_version) = *vrp_records_p;
-       }
-      
-      VARRAY_PUSH_GENERIC_PTR (*vrp_records_p, element);
-      if (! *vrp_variables_p)
-       VARRAY_TREE_INIT (*vrp_variables_p, 2, "vrp_variables");
-      VARRAY_PUSH_TREE (*vrp_variables_p, TREE_OPERAND (cond, 0));
+  if (gimple_code (stmt) == GIMPLE_PHI)
+    remove_phi_node (&gsi, true);
+  else
+    {
+      gsi_remove (&gsi, true);
+      release_defs (stmt);
     }
 }
 
-/* Given a conditional statement IF_STMT, return the assignment 'X = Y'
-   known to be true depending on which arm of IF_STMT is taken.
-
-   Not all conditional statements will result in a useful assignment.
-   Return NULL_TREE in that case.
+/* Given a statement STMT, which is either a PHI node or an assignment,
+   return the "rhs" of the node, in the case of a non-degenerate
+   phi, NULL is returned.  */
 
-   Also enter into the available expression table statements of
-   the form:
+static tree
+get_rhs_or_phi_arg (gimple stmt)
+{
+  if (gimple_code (stmt) == GIMPLE_PHI)
+    return degenerate_phi_result (stmt);
+  else if (gimple_assign_single_p (stmt))
+    return gimple_assign_rhs1 (stmt);
+  else
+    gcc_unreachable ();
+}
 
-     TRUE ARM          FALSE ARM
-     1 = cond          1 = cond'
-     0 = cond'         0 = cond
 
-   This allows us to lookup the condition in a dominated block and
-   get back a constant indicating if the condition is true.  */
+/* Given a statement STMT, which is either a PHI node or an assignment,
+   return the "lhs" of the node.  */
 
-static struct eq_expr_value
-get_eq_expr_value (tree if_stmt,
-                  int true_arm,
-                  varray_type *block_avail_exprs_p,
-                  basic_block bb,
-                  varray_type *vrp_variables_p)
+static tree
+get_lhs_or_phi_result (gimple stmt)
 {
-  tree cond;
-  struct eq_expr_value retval;
+  if (gimple_code (stmt) == GIMPLE_PHI)
+    return gimple_phi_result (stmt);
+  else if (is_gimple_assign (stmt))
+    return gimple_assign_lhs (stmt);
+  else
+    gcc_unreachable ();
+}
 
-  cond = COND_EXPR_COND (if_stmt);
-  retval.src = NULL;
-  retval.dst = NULL;
+/* Propagate RHS into all uses of LHS (when possible).
 
-  /* If the conditional is a single variable 'X', return 'X = 1' for
-     the true arm and 'X = 0' on the false arm.   */
-  if (TREE_CODE (cond) == SSA_NAME)
-    {
-      retval.dst = cond;
-      retval.src = constant_boolean_node (true_arm, TREE_TYPE (cond));
-      return retval;
-    }
+   RHS and LHS are derived from STMT, which is passed in solely so
+   that we can remove it if propagation is successful.
+
+   When propagating into a PHI node or into a statement which turns
+   into a trivial copy or constant initialization, set the
+   appropriate bit in INTERESTING_NAMEs so that we will visit those
+   nodes as well in an effort to pick up secondary optimization
+   opportunities.  */
 
-  /* If we have a comparison expression, then record its result into
-     the available expression table.  */
-  if (TREE_CODE_CLASS (TREE_CODE (cond)) == '<')
+static void
+propagate_rhs_into_lhs (gimple stmt, tree lhs, tree rhs, bitmap interesting_names)
+{
+  /* First verify that propagation is valid and isn't going to move a
+     loop variant variable outside its loop.  */
+  if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs)
+      && (TREE_CODE (rhs) != SSA_NAME
+         || ! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
+      && may_propagate_copy (lhs, rhs)
+      && loop_depth_of_name (lhs) >= loop_depth_of_name (rhs))
     {
-      tree op0 = TREE_OPERAND (cond, 0);
-      tree op1 = TREE_OPERAND (cond, 1);
-
-      /* Special case comparing booleans against a constant as we know
-        the value of OP0 on both arms of the branch.  ie, we can record
-        an equivalence for OP0 rather than COND.  */
-      if ((TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
-         && TREE_CODE (op0) == SSA_NAME
-         && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
-         && is_gimple_min_invariant (op1))
+      use_operand_p use_p;
+      imm_use_iterator iter;
+      gimple use_stmt;
+      bool all = true;
+
+      /* Dump details.  */
+      if (dump_file && (dump_flags & TDF_DETAILS))
+       {
+         fprintf (dump_file, "  Replacing '");
+         print_generic_expr (dump_file, lhs, dump_flags);
+         fprintf (dump_file, "' with %s '",
+                  (TREE_CODE (rhs) != SSA_NAME ? "constant" : "variable"));
+                  print_generic_expr (dump_file, rhs, dump_flags);
+         fprintf (dump_file, "'\n");
+       }
+
+      /* Walk over every use of LHS and try to replace the use with RHS.
+        At this point the only reason why such a propagation would not
+        be successful would be if the use occurs in an ASM_EXPR.  */
+      FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
        {
-         if ((TREE_CODE (cond) == EQ_EXPR && true_arm)
-             || (TREE_CODE (cond) == NE_EXPR && ! true_arm))
+         /* Leave debug stmts alone.  If we succeed in propagating
+            all non-debug uses, we'll drop the DEF, and propagation
+            into debug stmts will occur then.  */
+         if (gimple_debug_bind_p (use_stmt))
+           continue;
+
+         /* It's not always safe to propagate into an ASM_EXPR.  */
+         if (gimple_code (use_stmt) == GIMPLE_ASM
+              && ! may_propagate_copy_into_asm (lhs))
            {
-             retval.src = op1;
+             all = false;
+             continue;
            }
-         else
+
+         /* Dump details.  */
+         if (dump_file && (dump_flags & TDF_DETAILS))
            {
-             if (integer_zerop (op1))
-               retval.src = boolean_true_node;
-             else
-               retval.src = boolean_false_node;
+             fprintf (dump_file, "    Original statement:");
+             print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
            }
-         retval.dst = op0;
-         return retval;
-       }
 
-      if (TREE_CODE (op0) == SSA_NAME
-         && (is_gimple_min_invariant (op1) || TREE_CODE (op1) == SSA_NAME))
-       {
-         tree inverted = invert_truthvalue (cond);
+         /* Propagate the RHS into this use of the LHS.  */
+         FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
+           propagate_value (use_p, rhs);
 
-         /* When we find an available expression in the hash table, we replace
-            the expression with the LHS of the statement in the hash table.
+         /* Special cases to avoid useless calls into the folding
+            routines, operand scanning, etc.
 
-            So, we want to build statements such as "1 = <condition>" on the
-            true arm and "0 = <condition>" on the false arm.  That way if we
-            find the expression in the table, we will replace it with its
-            known constant value.  Also insert inversions of the result and
-            condition into the hash table.  */
-         if (true_arm)
-           {
-             record_cond (cond, boolean_true_node, block_avail_exprs_p);
-             record_dominating_conditions (cond, block_avail_exprs_p);
-             record_cond (inverted, boolean_false_node, block_avail_exprs_p);
+            First, propagation into a PHI may cause the PHI to become
+            a degenerate, so mark the PHI as interesting.  No other
+            actions are necessary.
 
-             if (TREE_CONSTANT (op1))
-               record_range (cond, bb, vrp_variables_p);
+            Second, if we're propagating a virtual operand and the
+            propagation does not change the underlying _DECL node for
+            the virtual operand, then no further actions are necessary.  */
+         if (gimple_code (use_stmt) == GIMPLE_PHI
+             || (! is_gimple_reg (lhs)
+                 && TREE_CODE (rhs) == SSA_NAME
+                 && SSA_NAME_VAR (lhs) == SSA_NAME_VAR (rhs)))
+           {
+             /* Dump details.  */
+             if (dump_file && (dump_flags & TDF_DETAILS))
+               {
+                 fprintf (dump_file, "    Updated statement:");
+                 print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
+               }
 
-               /* If the conditional is of the form 'X == Y', return 'X = Y'
-                  for the true arm.  */
-             if (TREE_CODE (cond) == EQ_EXPR)
+             /* Propagation into a PHI may expose new degenerate PHIs,
+                so mark the result of the PHI as interesting.  */
+             if (gimple_code (use_stmt) == GIMPLE_PHI)
                {
-                 retval.dst = op0;
-                 retval.src = op1;
-                 return retval;
+                 tree result = get_lhs_or_phi_result (use_stmt);
+                 bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
                }
+
+             continue;
+           }
+
+         /* From this point onward we are propagating into a
+            real statement.  Folding may (or may not) be possible,
+            we may expose new operands, expose dead EH edges,
+            etc.  */
+          /* NOTE tuples. In the tuples world, fold_stmt_inplace
+             cannot fold a call that simplifies to a constant,
+             because the GIMPLE_CALL must be replaced by a
+             GIMPLE_ASSIGN, and there is no way to effect such a
+             transformation in-place.  We might want to consider
+             using the more general fold_stmt here.  */
+         fold_stmt_inplace (use_stmt);
+
+         /* Sometimes propagation can expose new operands to the
+            renamer.  */
+         update_stmt (use_stmt);
+
+         /* Dump details.  */
+         if (dump_file && (dump_flags & TDF_DETAILS))
+           {
+             fprintf (dump_file, "    Updated statement:");
+             print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
            }
-         else
+
+         /* If we replaced a variable index with a constant, then
+            we would need to update the invariant flag for ADDR_EXPRs.  */
+          if (gimple_assign_single_p (use_stmt)
+              && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ADDR_EXPR)
+           recompute_tree_invariant_for_addr_expr
+                (gimple_assign_rhs1 (use_stmt));
+
+         /* If we cleaned up EH information from the statement,
+            mark its containing block as needing EH cleanups.  */
+         if (maybe_clean_or_replace_eh_stmt (use_stmt, use_stmt))
            {
+             bitmap_set_bit (need_eh_cleanup, gimple_bb (use_stmt)->index);
+             if (dump_file && (dump_flags & TDF_DETAILS))
+               fprintf (dump_file, "  Flagged to clear EH edges.\n");
+           }
 
-             record_cond (inverted, boolean_true_node, block_avail_exprs_p);
-             record_dominating_conditions (inverted, block_avail_exprs_p);
-             record_cond (cond, boolean_false_node, block_avail_exprs_p);
+         /* Propagation may expose new trivial copy/constant propagation
+            opportunities.  */
+          if (gimple_assign_single_p (use_stmt)
+              && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
+              && (TREE_CODE (gimple_assign_rhs1 (use_stmt)) == SSA_NAME
+                  || is_gimple_min_invariant (gimple_assign_rhs1 (use_stmt))))
+            {
+             tree result = get_lhs_or_phi_result (use_stmt);
+             bitmap_set_bit (interesting_names, SSA_NAME_VERSION (result));
+           }
 
-             if (TREE_CONSTANT (op1))
-               record_range (inverted, bb, vrp_variables_p);
+         /* Propagation into these nodes may make certain edges in
+            the CFG unexecutable.  We want to identify them as PHI nodes
+            at the destination of those unexecutable edges may become
+            degenerates.  */
+         else if (gimple_code (use_stmt) == GIMPLE_COND
+                  || gimple_code (use_stmt) == GIMPLE_SWITCH
+                  || gimple_code (use_stmt) == GIMPLE_GOTO)
+            {
+             tree val;
+
+             if (gimple_code (use_stmt) == GIMPLE_COND)
+                val = fold_binary_loc (gimple_location (use_stmt),
+                                  gimple_cond_code (use_stmt),
+                                   boolean_type_node,
+                                   gimple_cond_lhs (use_stmt),
+                                   gimple_cond_rhs (use_stmt));
+              else if (gimple_code (use_stmt) == GIMPLE_SWITCH)
+               val = gimple_switch_index (use_stmt);
+             else
+               val = gimple_goto_dest  (use_stmt);
 
-               /* If the conditional is of the form 'X != Y', return 'X = Y'
-                  for the false arm.  */
-             if (TREE_CODE (cond) == NE_EXPR)
+             if (val && is_gimple_min_invariant (val))
                {
-                 retval.dst = op0;
-                 retval.src = op1;
-                 return retval;
-               }
+                 basic_block bb = gimple_bb (use_stmt);
+                 edge te = find_taken_edge (bb, val);
+                 edge_iterator ei;
+                 edge e;
+                 gimple_stmt_iterator gsi, psi;
+
+                 /* Remove all outgoing edges except TE.  */
+                 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei));)
+                   {
+                     if (e != te)
+                       {
+                         /* Mark all the PHI nodes at the destination of
+                            the unexecutable edge as interesting.  */
+                          for (psi = gsi_start_phis (e->dest);
+                               !gsi_end_p (psi);
+                               gsi_next (&psi))
+                            {
+                              gimple phi = gsi_stmt (psi);
+
+                             tree result = gimple_phi_result (phi);
+                             int version = SSA_NAME_VERSION (result);
+
+                             bitmap_set_bit (interesting_names, version);
+                           }
+
+                         te->probability += e->probability;
+
+                         te->count += e->count;
+                         remove_edge (e);
+                         cfg_altered = true;
+                       }
+                     else
+                       ei_next (&ei);
+                   }
+
+                 gsi = gsi_last_bb (gimple_bb (use_stmt));
+                 gsi_remove (&gsi, true);
+
+                 /* And fixup the flags on the single remaining edge.  */
+                 te->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
+                 te->flags &= ~EDGE_ABNORMAL;
+                 te->flags |= EDGE_FALLTHRU;
+                 if (te->probability > REG_BR_PROB_BASE)
+                   te->probability = REG_BR_PROB_BASE;
+               }
            }
        }
-    }
 
-  return retval;
+      /* Ensure there is nothing else to do. */
+      gcc_assert (!all || has_zero_uses (lhs));
+
+      /* If we were able to propagate away all uses of LHS, then
+        we can remove STMT.  */
+      if (all)
+       remove_stmt_or_phi (stmt);
+    }
 }
 
-/* Hashing and equality functions for AVAIL_EXPRS.  The table stores
-   MODIFY_EXPR statements.  We compute a value number for expressions using
-   the code of the expression and the SSA numbers of its operands.  */
+/* STMT is either a PHI node (potentially a degenerate PHI node) or
+   a statement that is a trivial copy or constant initialization.
 
-static hashval_t
-avail_expr_hash (const void *p)
-{
-  stmt_ann_t ann = ((struct expr_hash_elt *)p)->ann;
-  tree rhs = ((struct expr_hash_elt *)p)->rhs;
-  hashval_t val = 0;
-  size_t i;
-  vuse_optype vuses;
+   Attempt to eliminate T by propagating its RHS into all uses of
+   its LHS.  This may in turn set new bits in INTERESTING_NAMES
+   for nodes we want to revisit later.
 
-  /* iterative_hash_expr knows how to deal with any expression and
-     deals with commutative operators as well, so just use it instead
-     of duplicating such complexities here.  */
-  val = iterative_hash_expr (rhs, val);
+   All exit paths should clear INTERESTING_NAMES for the result
+   of STMT.  */
 
-  /* If the hash table entry is not associated with a statement, then we
-     can just hash the expression and not worry about virtual operands
-     and such.  */
-  if (!ann)
-    return val;
+static void
+eliminate_const_or_copy (gimple stmt, bitmap interesting_names)
+{
+  tree lhs = get_lhs_or_phi_result (stmt);
+  tree rhs;
+  int version = SSA_NAME_VERSION (lhs);
+
+  /* If the LHS of this statement or PHI has no uses, then we can
+     just eliminate it.  This can occur if, for example, the PHI
+     was created by block duplication due to threading and its only
+     use was in the conditional at the end of the block which was
+     deleted.  */
+  if (has_zero_uses (lhs))
+    {
+      bitmap_clear_bit (interesting_names, version);
+      remove_stmt_or_phi (stmt);
+      return;
+    }
 
-  /* Add the SSA version numbers of every vuse operand.  This is important
-     because compound variables like arrays are not renamed in the
-     operands.  Rather, the rename is done on the virtual variable
-     representing all the elements of the array.  */
-  vuses = VUSE_OPS (ann);
-  for (i = 0; i < NUM_VUSES (vuses); i++)
-    val = iterative_hash_expr (VUSE_OP (vuses, i), val);
+  /* Get the RHS of the assignment or PHI node if the PHI is a
+     degenerate.  */
+  rhs = get_rhs_or_phi_arg (stmt);
+  if (!rhs)
+    {
+      bitmap_clear_bit (interesting_names, version);
+      return;
+    }
 
-  return val;
-}
+  propagate_rhs_into_lhs (stmt, lhs, rhs, interesting_names);
 
-static hashval_t
-real_avail_expr_hash (const void *p)
-{
-  return ((const struct expr_hash_elt *)p)->hash;
+  /* Note that STMT may well have been deleted by now, so do
+     not access it, instead use the saved version # to clear
+     T's entry in the worklist.  */
+  bitmap_clear_bit (interesting_names, version);
 }
 
-static int
-avail_expr_eq (const void *p1, const void *p2)
-{
-  stmt_ann_t ann1 = ((struct expr_hash_elt *)p1)->ann;
-  tree rhs1 = ((struct expr_hash_elt *)p1)->rhs;
-  stmt_ann_t ann2 = ((struct expr_hash_elt *)p2)->ann;
-  tree rhs2 = ((struct expr_hash_elt *)p2)->rhs;
+/* The first phase in degenerate PHI elimination.
 
-  /* If they are the same physical expression, return true.  */
-  if (rhs1 == rhs2 && ann1 == ann2)
-    return true;
+   Eliminate the degenerate PHIs in BB, then recurse on the
+   dominator children of BB.  */
 
-  /* If their codes are not equal, then quit now.  */
-  if (TREE_CODE (rhs1) != TREE_CODE (rhs2))
-    return false;
+static void
+eliminate_degenerate_phis_1 (basic_block bb, bitmap interesting_names)
+{
+  gimple_stmt_iterator gsi;
+  basic_block son;
 
-  /* In case of a collision, both RHS have to be identical and have the
-     same VUSE operands.  */
-  if ((TREE_TYPE (rhs1) == TREE_TYPE (rhs2)
-       || lang_hooks.types_compatible_p (TREE_TYPE (rhs1), TREE_TYPE (rhs2)))
-      && operand_equal_p (rhs1, rhs2, OEP_PURE_SAME))
+  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     {
-      vuse_optype ops1 = NULL;
-      vuse_optype ops2 = NULL;
-      size_t num_ops1 = 0;
-      size_t num_ops2 = 0;
-      size_t i;
+      gimple phi = gsi_stmt (gsi);
 
-      if (ann1)
-       {
-         ops1 = VUSE_OPS (ann1);
-         num_ops1 = NUM_VUSES (ops1);
-       }
+      eliminate_const_or_copy (phi, interesting_names);
+    }
 
-      if (ann2)
-       {
-         ops2 = VUSE_OPS (ann2);
-         num_ops2 = NUM_VUSES (ops2);
-       }
+  /* Recurse into the dominator children of BB.  */
+  for (son = first_dom_son (CDI_DOMINATORS, bb);
+       son;
+       son = next_dom_son (CDI_DOMINATORS, son))
+    eliminate_degenerate_phis_1 (son, interesting_names);
+}
 
-      /* If the number of virtual uses is different, then we consider
-        them not equal.  */
-      if (num_ops1 != num_ops2)
-       return false;
 
-      for (i = 0; i < num_ops1; i++)
-       if (VUSE_OP (ops1, i) != VUSE_OP (ops2, i))
-         return false;
+/* A very simple pass to eliminate degenerate PHI nodes from the
+   IL.  This is meant to be fast enough to be able to be run several
+   times in the optimization pipeline.
 
-#ifdef ENABLE_CHECKING
-      if (((struct expr_hash_elt *)p1)->hash
-         != ((struct expr_hash_elt *)p2)->hash)
-       abort ();
-#endif
-      return true;
-    }
+   Certain optimizations, particularly those which duplicate blocks
+   or remove edges from the CFG can create or expose PHIs which are
+   trivial copies or constant initializations.
 
-  return false;
-}
+   While we could pick up these optimizations in DOM or with the
+   combination of copy-prop and CCP, those solutions are far too
+   heavy-weight for our needs.
 
-/* Given STMT and a pointer to the block local definitions BLOCK_DEFS_P,
-   register register all objects set by this statement into BLOCK_DEFS_P
-   and CURRDEFS.  */
+   This implementation has two phases so that we can efficiently
+   eliminate the first order degenerate PHIs and second order
+   degenerate PHIs.
 
-static void
-register_definitions_for_stmt (tree stmt, varray_type *block_defs_p)
+   The first phase performs a dominator walk to identify and eliminate
+   the vast majority of the degenerate PHIs.  When a degenerate PHI
+   is identified and eliminated any affected statements or PHIs
+   are put on a worklist.
+
+   The second phase eliminates degenerate PHIs and trivial copies
+   or constant initializations using the worklist.  This is how we
+   pick up the secondary optimization opportunities with minimal
+   cost.  */
+
+static unsigned int
+eliminate_degenerate_phis (void)
 {
-  tree def;
-  ssa_op_iter iter;
+  bitmap interesting_names;
+  bitmap interesting_names1;
+
+  /* Bitmap of blocks which need EH information updated.  We can not
+     update it on-the-fly as doing so invalidates the dominator tree.  */
+  need_eh_cleanup = BITMAP_ALLOC (NULL);
 
-  FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
+  /* INTERESTING_NAMES is effectively our worklist, indexed by
+     SSA_NAME_VERSION.
+
+     A set bit indicates that the statement or PHI node which
+     defines the SSA_NAME should be (re)examined to determine if
+     it has become a degenerate PHI or trivial const/copy propagation
+     opportunity.
+
+     Experiments have show we generally get better compilation
+     time behavior with bitmaps rather than sbitmaps.  */
+  interesting_names = BITMAP_ALLOC (NULL);
+  interesting_names1 = BITMAP_ALLOC (NULL);
+
+  calculate_dominance_info (CDI_DOMINATORS);
+  cfg_altered = false;
+
+  /* First phase.  Eliminate degenerate PHIs via a dominator
+     walk of the CFG.
+
+     Experiments have indicated that we generally get better
+     compile-time behavior by visiting blocks in the first
+     phase in dominator order.  Presumably this is because walking
+     in dominator order leaves fewer PHIs for later examination
+     by the worklist phase.  */
+  eliminate_degenerate_phis_1 (ENTRY_BLOCK_PTR, interesting_names);
+
+  /* Second phase.  Eliminate second order degenerate PHIs as well
+     as trivial copies or constant initializations identified by
+     the first phase or this phase.  Basically we keep iterating
+     until our set of INTERESTING_NAMEs is empty.   */
+  while (!bitmap_empty_p (interesting_names))
     {
+      unsigned int i;
+      bitmap_iterator bi;
+
+      /* EXECUTE_IF_SET_IN_BITMAP does not like its bitmap
+        changed during the loop.  Copy it to another bitmap and
+        use that.  */
+      bitmap_copy (interesting_names1, interesting_names);
+
+      EXECUTE_IF_SET_IN_BITMAP (interesting_names1, 0, i, bi)
+       {
+         tree name = ssa_name (i);
+
+         /* Ignore SSA_NAMEs that have been released because
+            their defining statement was deleted (unreachable).  */
+         if (name)
+           eliminate_const_or_copy (SSA_NAME_DEF_STMT (ssa_name (i)),
+                                    interesting_names);
+       }
+    }
 
-      /* FIXME: We shouldn't be registering new defs if the variable
-        doesn't need to be renamed.  */
-      register_new_def (def, block_defs_p);
+  if (cfg_altered)
+    free_dominance_info (CDI_DOMINATORS);
+
+  /* Propagation of const and copies may make some EH edges dead.  Purge
+     such edges from the CFG as needed.  */
+  if (!bitmap_empty_p (need_eh_cleanup))
+    {
+      gimple_purge_all_dead_eh_edges (need_eh_cleanup);
+      BITMAP_FREE (need_eh_cleanup);
     }
+
+  BITMAP_FREE (interesting_names);
+  BITMAP_FREE (interesting_names1);
+  return 0;
 }
 
+struct gimple_opt_pass pass_phi_only_cprop =
+{
+ {
+  GIMPLE_PASS,
+  "phicprop",                           /* name */
+  gate_dominator,                       /* gate */
+  eliminate_degenerate_phis,            /* execute */
+  NULL,                                 /* sub */
+  NULL,                                 /* next */
+  0,                                    /* static_pass_number */
+  TV_TREE_PHI_CPROP,                    /* tv_id */
+  PROP_cfg | PROP_ssa,                 /* properties_required */
+  0,                                    /* properties_provided */
+  0,                                   /* properties_destroyed */
+  0,                                    /* todo_flags_start */
+  TODO_cleanup_cfg
+    | TODO_dump_func
+    | TODO_ggc_collect
+    | TODO_verify_ssa
+    | TODO_verify_stmts
+    | TODO_update_ssa                  /* todo_flags_finish */
+ }
+};