public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH]: Rewrite tree level PRE
@ 2008-07-06  4:48 Daniel Berlin
  2008-07-06  8:58 ` Ralf Wildenhues
  2008-07-06 15:23 ` Richard Guenther
  0 siblings, 2 replies; 16+ messages in thread
From: Daniel Berlin @ 2008-07-06  4:48 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 4747 bytes --]

This patch effectively rewrites tree PRE to share datastructures with
tree-vn, instead of copying statements and modifying trees (which
don't work so well in a tuples world :P).

As part of the rewrite, it also fixes a bunch of missed optimizations
because PRE will now move globals.
It also uses roughly half as much memory as before (since PRE no
longer copies statements but instead shares the vn structures with the
vn).
The fundamentals are the same as they were before, it simply no longer
does it's own value numbering.
Since PRE was the only user of VALUE_HANDLES, i have removed them as
part of this patch.
Since PRE was the only user of most of tree-vn.c, i removed it and
moved the 3 still-used functions to tree-ssa-sccvn.c
PRE is also now type correct, in that the insertions it creates should
have matched types, whereas before they were generally a mismash (it
would happily create phi nodes whose arguments had mixed types, etc).

This patch will cause some FRE failures due to a small bit of code i
disabled in tree-ssa-sccvn.c.  I have discussed this with richard
guenther and he has said he will fix it up, so the ssa-fre-* failures
are temporary.

Other than than PRE of globals, it should PRE all of the expressions
we used to, plus a few more in the way of loads.  I have verified this
by hand comparing the eliminations performed during a bootstrap of
gcc.
I have not performed more benchmarking than that, as the main goal was
a feature equivalent rewrite that would enable tuple conversion to
procede. The fact that it will PRE globals is simply a side effect of
sharing a representation with the VN.

The patch is large but 99% of it is the rewritten tree-ssa-pre.c.

Bootstrapped and regtested on i686-darwin (With the safe-ctype change
reverted) and i686-linux.
I will wait a few days for comments before committing it (All the code
outside of PRE that I am removing was added as part of PRE, but if
someone wants to object to it's removal, please let me know).


2008-07-05  Daniel Berlin  <dberlin@dberlin.org>
	
	Fix PR tree-optimization/23455
	Fix PR tree-optimization/35286
	Fix PR tree-optimization/35287
	* Makefile.in (OBJS-common): Remove tree-vn.o.
	tree-vn.o: Remove.
	* dbgcnt.def: Add treepre_insert debug counter.
	* gcc/tree-flow.h (add_to_value): Updated for other changes.
	(debug_value_expressions): Ditto.
	(print_value_expressions): Ditto.
	* tree-pretty-print.c (dump_generic_node): Updated for
	VALUE_HANDLE removal.
	* tree-ssa-dom.c (record_equality): Ditto.
	(cprop_operand): Ditto.
	(lookup_avail_expr): Ditto.
	* tree-ssa-threadedge.c
	(record_temporary_equivalences_from_stmts_at_dest): Ditto.
	(simplify_control_stmt_condition): Ditto.
	* tree.c (tree_code_size): Ditto.
	(tree_node_structure): Ditto.
	(iterative_hash_expr): Ditto.
	* tree.def: Ditto.
	* tree.h (VALUE_HANDLE_ID): Ditto.
	(VALUE_HANDLE_EXPR_SET): Ditto.
	(struct tree_value_handle): Ditto.
	(union tree_node): Ditto.
	* treestruct.def: Ditto.
	* tree-vn.c: Removed.
	* tree-ssa-pre.c: Rewritten entirely.
	* tree-ssa-sccvn.c (constant_to_value_id): New hashtable.
	(constant_value_ids): Ditto.
	(vn_nary_op_t): Moved to header.
	(vn_phi_t): Ditto.
	(vn_reference_op_t): Ditto
	(vn_reference_t): Ditto.
	(next_value_id): New variable.
	(VN_INFO): Add an assert.
	(vn_constant_eq): New function.
	(vn_constant_hash): Ditto.
	(get_or_alloc_constant_value_id): Ditto.
	(value_id_constant_p): Ditto.
	(vn_reference_compute_hash): De-staticify.
	(copy_reference_ops_from_ref): Don't use get_callee_fndecl.
	Disable some code with a FIXME.
	Remove VALUE_HANDLE use.
	(valueize_refs): Update opcode if it changes from ssa name to
	constant.
	(vn_reference_lookup_1): Add new argument.
	(vn_reference_lookup):  Ditto.
	(vn_reference_lookup_pieces): New function.
	(vn_reference_insert): Add return type. Modify to deal with value
	ids.
	(vn_reference_insert_pieces):  New function.
	(vn_nary_op_compute_hash): De-staticify.
	(vn_nary_op_eq): Ditto.
	(vn_nary_op_lookup_pieces): New function.
	(vn_nary_op_lookup): Add new argument.
	(vn_nary_op_insert_pieces): New function.
	(vn_nary_op_insert): Add return type. Modify to deal with value
	ids.
	(vn_phi_insert): Ditto.
	(visit_unary_op): Update for callee changes.
	(visit_binary_op): Ditto.
	(visit_reference_op_load): Ditto.
	(visit_reference_op_store): Ditto.
	(init_scc_vn): Init next_value_id, constant_to_value_id and constant_value_ids.
	(free_scc_vn): Free them.
	(set_hashtable_value_ids): New function.
	(run_scc_vn): Use it.
	(get_max_value_id): New function.
	(get_next_value_id): Ditto.
	(expressions_equal_p): Moved from tree-vn.c
	(sort_vuses): Ditto.
	(sort_vuses_heap): Ditto.
	* tree-ssa-sccvn.h: Structures moved from tree-ssa-sccvn.c (noted
	above).
	

--Dan

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: newpre.diff --]
[-- Type: text/x-diff; name=newpre.diff, Size: 173534 bytes --]

diff -r 2325330ce9ed gcc/Makefile.in
--- a/gcc/Makefile.in	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/Makefile.in	Sun Jul 06 00:27:17 2008 -0400
@@ -1222,7 +1222,6 @@
 	tree-vect-patterns.o \
 	tree-vect-transform.o \
 	tree-vectorizer.o \
-	tree-vn.o \
 	tree-vrp.o \
 	tree.o \
 	value-prof.o \
@@ -2115,9 +2114,6 @@
    alloc-pool.h $(BASIC_BLOCK_H) bitmap.h $(HASHTAB_H) $(TREE_GIMPLE_H) \
    $(TREE_INLINE_H) tree-iterator.h tree-ssa-propagate.h tree-ssa-sccvn.h \
    $(PARAMS_H)
-tree-vn.o : tree-vn.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(GGC_H) \
-   $(TREE_H) $(TREE_FLOW_H) $(HASHTAB_H) langhooks.h tree-pass.h \
-   $(TREE_DUMP_H) $(DIAGNOSTIC_H) tree-ssa-sccvn.h
 tree-vrp.o : tree-vrp.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(TREE_H) \
    $(TREE_FLOW_H) tree-pass.h $(TREE_DUMP_H) $(DIAGNOSTIC_H) $(GGC_H) \
    $(BASIC_BLOCK_H) tree-ssa-propagate.h $(FLAGS_H) $(TREE_DUMP_H) \
diff -r 2325330ce9ed gcc/dbgcnt.def
--- a/gcc/dbgcnt.def	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/dbgcnt.def	Sun Jul 06 00:27:17 2008 -0400
@@ -166,6 +166,7 @@
 DEBUG_COUNTER (local_alloc_for_sched)
 DEBUG_COUNTER (postreload_cse)
 DEBUG_COUNTER (pre_insn)
+DEBUG_COUNTER (treepre_insert)
 DEBUG_COUNTER (sched2_func)
 DEBUG_COUNTER (sched_block)
 DEBUG_COUNTER (sched_func)
@@ -174,4 +175,3 @@
 DEBUG_COUNTER (sms_sched_loop)
 DEBUG_COUNTER (split_for_sched2)
 DEBUG_COUNTER (tail_call)
-
diff -r 2325330ce9ed gcc/testsuite/gcc.c-torture/compile/20080704-1.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gcc/testsuite/gcc.c-torture/compile/20080704-1.c	Sun Jul 06 00:27:17 2008 -0400
@@ -0,0 +1,43 @@
+/* This code used to crash fold_convert due to PRE
+   wanting to fold_convert from a REA_TYPE to an INTEGER_TYPE.  */
+typedef unsigned int uint32_t;
+union double_union
+{
+  double d;
+  uint32_t i[2];
+};
+struct _Jv_reent
+{
+  int _errno;
+};
+_Jv_strtod_r (struct _Jv_reent *ptr, char **se)
+{
+  int bb2, sign;
+  double aadj, aadj1, adj;
+  unsigned long y, z;
+  union double_union rv, *bs = ((void *) 0), *delta = ((void *) 0);
+  {
+  ovfl:ptr->_errno = 34;
+    {
+      (((uint32_t) 0xfffffL) | ((uint32_t) 0x100000L) * (1024 + 1023 - 1));
+    }
+    if ((aadj = _Jv_ratio (delta, bs)) <= 2.)
+      {
+	{
+	  if (aadj < 2. / 2)
+	    aadj = 1. / 2;
+	  aadj1 = -aadj;
+	}
+      }
+    {
+      (rv.i[1]) -= 53 * ((uint32_t) 0x100000L);
+      adj = aadj1 * _Jv_ulp (rv.d);
+      rv.d += adj;
+      if (((rv.i[1]) & ((uint32_t) 0x7ff00000L)) >=
+	  ((uint32_t) 0x100000L) * (1024 + 1023 - 53))
+	{
+	  goto ovfl;
+	}
+    }
+  }
+}
diff -r 2325330ce9ed gcc/testsuite/gcc.dg/tree-ssa/loadpre24.c
--- a/gcc/testsuite/gcc.dg/tree-ssa/loadpre24.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/testsuite/gcc.dg/tree-ssa/loadpre24.c	Sun Jul 06 00:27:17 2008 -0400
@@ -18,7 +18,7 @@
   return d + e;
 }
 
-/* PRE of globals doesn't work.  */
+/* We will move the load of a out of the loop.  */
 
-/* { dg-final { scan-tree-dump-times "Eliminated: 1" 1 "pre" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "Eliminated: 1" 1 "pre" } } */
 /* { dg-final { cleanup-tree-dump "pre" } } */
diff -r 2325330ce9ed gcc/testsuite/gcc.dg/tree-ssa/pr23455.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr23455.c	Sun Jul 06 00:27:17 2008 -0400
@@ -0,0 +1,23 @@
+/* { dg-do compile } */ 
+/* { dg-options "-O2 -fdump-tree-pre-stats" } */
+unsigned long outcnt;
+extern void flush_outbuf(void);
+
+void
+bi_windup(unsigned int *outbuf, unsigned int bi_buf)
+{
+    unsigned long t1 = outcnt;
+    outbuf[t1] = bi_buf;
+
+    unsigned long t2 = outcnt;
+    if (t2 == 16384)
+      flush_outbuf();
+
+    unsigned long t3 = outcnt;
+    outbuf[t3] = bi_buf;
+}
+/* We should eliminate one load of outcnt, which will in turn let us eliminate
+   one multiply of outcnt which will in turn let us eliminate
+   one add involving outcnt and outbuf.  */
+/* { dg-final { scan-tree-dump-times "Eliminated: 3" 1 "pre"} } */
+/* { dg-final { cleanup-tree-dump "pre" } } */
diff -r 2325330ce9ed gcc/testsuite/gcc.dg/tree-ssa/pr35286.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr35286.c	Sun Jul 06 00:27:17 2008 -0400
@@ -0,0 +1,23 @@
+/* { dg-do compile } */ 
+/* { dg-options "-O2 -fdump-tree-pre-stats" } */
+int g2;
+struct A {
+    int a; int b;
+}g1;
+int foo(int a, int b)
+{
+  if (a > 0)
+    {
+      g1.a = a+ b;
+    }
+  else
+    g1.a = b;
+
+  g2 = a+b;
+
+  return g1.a;
+}
+/* We will eliminate the g1.a from the return statement as fully redundant,
+   and remove one calculation of a + b. */
+/* { dg-final { scan-tree-dump-times "Eliminated: 2" 1 "pre"} } */
+/* { dg-final { cleanup-tree-dump "pre" } } */
diff -r 2325330ce9ed gcc/testsuite/gcc.dg/tree-ssa/pr35287.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr35287.c	Sun Jul 06 00:27:17 2008 -0400
@@ -0,0 +1,15 @@
+/* { dg-do compile } */ 
+/* { dg-options "-O2 -fdump-tree-pre-stats" } */
+int *gp;
+int foo(int p)
+{
+  int t = 0;
+  if (p)
+    t = *gp + 1;
+
+  return (*gp + t);
+}
+
+/* We will eliminate one load of gp and one indirect load of *gp. */
+/* { dg-final { scan-tree-dump-times "Eliminated: 2" 1 "pre"} } */
+/* { dg-final { cleanup-tree-dump "pre" } } */
diff -r 2325330ce9ed gcc/tree-flow.h
--- a/gcc/tree-flow.h	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-flow.h	Sun Jul 06 00:27:17 2008 -0400
@@ -1124,9 +1124,10 @@
 extern bool maybe_clean_or_replace_eh_stmt (tree, tree);
 
 /* In tree-ssa-pre.c  */
-void add_to_value (tree, tree);
-void debug_value_expressions (tree);
-void print_value_expressions (FILE *, tree);
+struct pre_expr_d;
+void add_to_value (unsigned int, struct pre_expr_d *);
+void debug_value_expressions (unsigned int);
+void print_value_expressions (FILE *, unsigned int);
 
 
 /* In tree-vn.c  */
diff -r 2325330ce9ed gcc/tree-pretty-print.c
--- a/gcc/tree-pretty-print.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-pretty-print.c	Sun Jul 06 00:27:17 2008 -0400
@@ -952,16 +952,12 @@
 	pp_character (buffer, ')');
       pp_string (buffer, str);
       dump_generic_node (buffer, TREE_OPERAND (node, 1), spc, flags, false);
-
-      if (TREE_CODE (op0) != VALUE_HANDLE)
+      op0 = component_ref_field_offset (node);
+      if (op0 && TREE_CODE (op0) != INTEGER_CST)
 	{
-	  op0 = component_ref_field_offset (node);
-	  if (op0 && TREE_CODE (op0) != INTEGER_CST)
-	    {
-	      pp_string (buffer, "{off: ");
+	  pp_string (buffer, "{off: ");
 	      dump_generic_node (buffer, op0, spc, flags, false);
 	      pp_character (buffer, '}');
-	    }
 	}
       break;
 
@@ -1789,10 +1785,6 @@
       pp_string (buffer, ">");
       break;
 
-    case VALUE_HANDLE:
-      pp_printf (buffer, "VH.%d", VALUE_HANDLE_ID (node));
-      break;
-
     case ASSERT_EXPR:
       pp_string (buffer, "ASSERT_EXPR <");
       dump_generic_node (buffer, ASSERT_EXPR_VAR (node), spc, flags, false);
diff -r 2325330ce9ed gcc/tree-ssa-dom.c
--- a/gcc/tree-ssa-dom.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-ssa-dom.c	Sun Jul 06 00:27:17 2008 -0400
@@ -1163,7 +1163,7 @@
     prev_x = x, x = y, y = prev_x, prev_x = prev_y;
   else if (prev_x && is_gimple_min_invariant (prev_x))
     x = y, y = prev_x, prev_x = prev_y;
-  else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
+  else if (prev_y)
     y = prev_y;
 
   /* After the swapping, we must have one SSA_NAME.  */
@@ -1629,7 +1629,7 @@
      copy of some other variable, use the value or copy stored in
      CONST_AND_COPIES.  */
   val = SSA_NAME_VALUE (op);
-  if (val && val != op && TREE_CODE (val) != VALUE_HANDLE)
+  if (val && val != op)
     {
       tree op_type, val_type;
 
@@ -1969,7 +1969,7 @@
   if (TREE_CODE (lhs) == SSA_NAME)
     {
       temp = SSA_NAME_VALUE (lhs);
-      if (temp && TREE_CODE (temp) != VALUE_HANDLE)
+      if (temp)
 	lhs = temp;
     }
 
diff -r 2325330ce9ed gcc/tree-ssa-pre.c
--- a/gcc/tree-ssa-pre.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-ssa-pre.c	Sun Jul 06 00:27:17 2008 -0400
@@ -46,6 +46,7 @@
 #include "cfgloop.h"
 #include "tree-ssa-sccvn.h"
 #include "params.h"
+#include "dbgcnt.h"
 
 /* TODO:
 
@@ -116,155 +117,178 @@
 
 /* Representations of value numbers:
 
-   Value numbers are represented using the "value handle" approach.
-   This means that each SSA_NAME (and for other reasons to be
-   disclosed in a moment, expression nodes) has a value handle that
-   can be retrieved through get_value_handle.  This value handle *is*
-   the value number of the SSA_NAME.  You can pointer compare the
-   value handles for equivalence purposes.
-
-   For debugging reasons, the value handle is internally more than
-   just a number, it is a VALUE_HANDLE named "VH.x", where x is a
-   unique number for each value number in use.  This allows
-   expressions with SSA_NAMES replaced by value handles to still be
-   pretty printed in a sane way.  They simply print as "VH.3 *
-   VH.5", etc.
-
-   Expression nodes have value handles associated with them as a
-   cache.  Otherwise, we'd have to look them up again in the hash
-   table.  This makes significant difference (factor of two or more) on
-   some test cases.  They can be thrown away after the pass is
-   finished.  */
+   Value numbers are represented by a representative SSA_NAME.  We
+   will create fake SSA_NAME's in situations where we need a
+   representative but do not have one (because it is a complex
+   expression).  In order to facilitate storing the value numbers in
+   bitmaps, and keep the number of wasted SSA_NAME's down, we also
+   associate a value_id with each value number, and create full blown
+   ssa_name's only where we actually need them (IE in operands of
+   existing expressions).  
+
+   Theoretically you could replace all the value_id's with
+   SSA_NAME_VERSION, but this would allocate a large number of
+   SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
+   It would also require an additional indirection at each point we
+   use the value id.  */
 
 /* Representation of expressions on value numbers:
 
-   In some portions of this code, you will notice we allocate "fake"
-   analogues to the expression we are value numbering, and replace the
-   operands with the values of the expression.  Since we work on
-   values, and not just names, we canonicalize expressions to value
-   expressions for use in the ANTIC sets, the EXP_GEN set, etc.
-
-   This is theoretically unnecessary, it just saves a bunch of
-   repeated get_value_handle and find_leader calls in the remainder of
-   the code, trading off temporary memory usage for speed.  The tree
-   nodes aren't actually creating more garbage, since they are
-   allocated in a special pools which are thrown away at the end of
-   this pass.
-
-   All of this also means that if you print the EXP_GEN or ANTIC sets,
-   you will see "VH.5 + VH.7" in the set, instead of "a_55 +
-   b_66" or something.  The only thing that actually cares about
-   seeing the value leaders is phi translation, and it needs to be
-   able to find the leader for a value in an arbitrary block, so this
-   "value expression" form is perfect for it (otherwise you'd do
-   get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
-
+   Expressions consisting of  value numbers are represented the same
+   way as our VN internally represents them, with an additional
+   "pre_expr" wrapping around them in order to facilitate storing all
+   of the expressions in the same sets.  */
 
 /* Representation of sets:
 
-   There are currently two types of sets used, hopefully to be unified soon.
-   The AVAIL sets do not need to be sorted in any particular order,
-   and thus, are simply represented as two bitmaps, one that keeps
-   track of values present in the set, and one that keeps track of
-   expressions present in the set.
-
-   The other sets are represented as doubly linked lists kept in topological
-   order, with an optional supporting bitmap of values present in the
-   set.  The sets represent values, and the elements can be values or
-   expressions.  The elements can appear in different sets, but each
-   element can only appear once in each set.
-
-   Since each node in the set represents a value, we also want to be
-   able to map expression, set pairs to something that tells us
-   whether the value is present is a set.  We use a per-set bitmap for
-   that.  The value handles also point to a linked list of the
-   expressions they represent via a tree annotation.  This is mainly
-   useful only for debugging, since we don't do identity lookups.  */
+   The dataflow sets do not need to be sorted in any particular order
+   for the majority of their lifetime, are simply represented as two
+   bitmaps, one that keeps track of values present in the set, and one
+   that keeps track of expressions present in the set.
+
+   When we need them in topological order, we produce it on demand by
+   transforming the bitmap into an array and sorting it into topo
+   order.  */
+
+/* Type of expression, used to know which member of the PRE_EXPR union
+   is valid.  */
+
+enum pre_expr_kind
+{
+    NAME,
+    NARY,
+    REFERENCE,
+    CONSTANT
+};
+
+typedef union pre_expr_union_d
+{
+  tree name;
+  tree constant;
+  vn_nary_op_t nary;
+  vn_reference_t reference;
+} pre_expr_union;
+
+typedef struct pre_expr_d
+{
+  enum pre_expr_kind kind;
+  unsigned int id;
+  pre_expr_union u;
+} *pre_expr;
+
+#define PRE_EXPR_NAME(e) (e)->u.name
+#define PRE_EXPR_NARY(e) (e)->u.nary
+#define PRE_EXPR_REFERENCE(e) (e)->u.reference
+#define PRE_EXPR_CONSTANT(e) (e)->u.constant
+
+static int
+pre_expr_eq (const void *p1, const void *p2) 
+{
+  const struct pre_expr_d *e1 = (const struct pre_expr_d *) p1;
+  const struct pre_expr_d *e2 = (const struct pre_expr_d *) p2;
+  
+  if (e1->kind != e2->kind)
+    return false;
+  
+  switch (e1->kind) 
+    {
+    case CONSTANT:
+      return operand_equal_p (PRE_EXPR_CONSTANT (e1), PRE_EXPR_CONSTANT (e2),
+			      OEP_PURE_SAME);
+    case NAME:
+      return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
+    case NARY:
+      return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
+    case REFERENCE:
+      return vn_reference_eq (PRE_EXPR_REFERENCE (e1), PRE_EXPR_REFERENCE (e2));
+    default:
+      abort();
+    }
+}
+
+static hashval_t
+pre_expr_hash (const void *p1)
+{
+  const struct pre_expr_d *e = (const struct pre_expr_d *) p1;
+  switch (e->kind) 
+    {
+    case CONSTANT:
+      return iterative_hash_expr (PRE_EXPR_CONSTANT (e), 0);
+    case NAME:
+      return iterative_hash_expr (PRE_EXPR_NAME (e), 0);
+    case NARY:
+      return vn_nary_op_compute_hash (PRE_EXPR_NARY (e));
+    case REFERENCE:
+      return vn_reference_compute_hash (PRE_EXPR_REFERENCE (e));
+    default:
+      abort ();
+    }
+}
 
 
 /* Next global expression id number.  */
 static unsigned int next_expression_id;
 
-typedef VEC(tree, gc) *vuse_vec;
-DEF_VEC_P (vuse_vec);
-DEF_VEC_ALLOC_P (vuse_vec, heap);
-
-static VEC(vuse_vec, heap) *expression_vuses;
-
 /* Mapping from expression to id number we can use in bitmap sets.  */
-static VEC(tree, heap) *expressions;
-
+DEF_VEC_P (pre_expr);
+DEF_VEC_ALLOC_P (pre_expr, heap);
+static VEC(pre_expr, heap) *expressions;
+static htab_t expression_to_id;
+			   
 /* Allocate an expression id for EXPR.  */
 
 static inline unsigned int
-alloc_expression_id (tree expr)
-{
-  tree_ann_common_t ann;
-
-  ann = get_tree_common_ann (expr);
-
+alloc_expression_id (pre_expr expr)
+{
+  void **slot;
   /* Make sure we won't overflow. */
   gcc_assert (next_expression_id + 1 > next_expression_id);
-
-  ann->aux = XNEW (unsigned int);
-  * ((unsigned int *)ann->aux) = next_expression_id++;
-  VEC_safe_push (tree, heap, expressions, expr);
-  VEC_safe_push (vuse_vec, heap, expression_vuses, NULL);
+  expr->id = next_expression_id++;
+  VEC_safe_push (pre_expr, heap, expressions, expr);
+  slot = htab_find_slot (expression_to_id, expr, INSERT);
+  gcc_assert (!*slot);
+  *slot = expr;
   return next_expression_id - 1;
 }
 
 /* Return the expression id for tree EXPR.  */
 
 static inline unsigned int
-get_expression_id (tree expr)
-{
-  tree_ann_common_t ann = tree_common_ann (expr);
-  gcc_assert (ann);
-  gcc_assert (ann->aux);
-
-  return  *((unsigned int *)ann->aux);
+get_expression_id (const pre_expr expr)
+{
+  return expr->id;
+}
+
+static inline unsigned int
+lookup_expression_id (const pre_expr expr) 
+{
+  void **slot;
+
+  slot = htab_find_slot (expression_to_id, expr, NO_INSERT);
+  if (!slot)
+    return 0;
+  return ((pre_expr)*slot)->id;
 }
 
 /* Return the existing expression id for EXPR, or create one if one
    does not exist yet.  */
 
 static inline unsigned int
-get_or_alloc_expression_id (tree expr)
-{
-  tree_ann_common_t ann = tree_common_ann (expr);
-
-  if (ann == NULL || !ann->aux)
+get_or_alloc_expression_id (pre_expr expr)
+{
+  unsigned int id = lookup_expression_id (expr);
+  if (id == 0)
     return alloc_expression_id (expr);
-
-  return get_expression_id (expr);
+  return expr->id = id;
 }
 
 /* Return the expression that has expression id ID */
 
-static inline tree
+static inline pre_expr
 expression_for_id (unsigned int id)
 {
-  return VEC_index (tree, expressions, id);
-}
-
-/* Return the expression vuses for EXPR, if there are any.  */
-
-static inline vuse_vec
-get_expression_vuses (tree expr)
-{
-  unsigned int expr_id = get_or_alloc_expression_id (expr);
-  return VEC_index (vuse_vec, expression_vuses, expr_id);
-}
-
-/* Set the expression vuses for EXPR to VUSES.  */
-
-static inline void
-set_expression_vuses (tree expr, vuse_vec vuses)
-{
-  unsigned int expr_id = get_or_alloc_expression_id (expr);
-  VEC_replace (vuse_vec, expression_vuses, expr_id, vuses);
-}
-
+  return VEC_index (pre_expr, expressions, id);
+}
 
 /* Free the expression id field in all of our expressions,
    and then destroy the expressions array.  */
@@ -272,16 +296,31 @@
 static void
 clear_expression_ids (void)
 {
-  int i;
-  tree expr;
-
-  for (i = 0; VEC_iterate (tree, expressions, i, expr); i++)
-    {
-      free (tree_common_ann (expr)->aux);
-      tree_common_ann (expr)->aux = NULL;
-    }
-  VEC_free (tree, heap, expressions);
-  VEC_free (vuse_vec, heap, expression_vuses);
+  VEC_free (pre_expr, heap, expressions);
+}
+
+static alloc_pool pre_expr_pool;
+
+/* Given an SSA_NAME NAME, get or create a pre_expr to represent it.  */
+
+static pre_expr
+get_or_alloc_expr_for_name (tree name)
+{
+  pre_expr result = (pre_expr) pool_alloc (pre_expr_pool);
+  unsigned int result_id;
+
+  result->kind = NAME;
+  result->id = 0;
+  PRE_EXPR_NAME (result) = name;
+  result_id = lookup_expression_id (result);
+  if (result_id != 0)
+    {
+      pool_free (pre_expr_pool, result);
+      result = expression_for_id (result_id);
+      return result;
+    }
+  get_or_alloc_expression_id (result);
+  return result;
 }
 
 static bool in_fre = false;
@@ -295,7 +334,12 @@
 } *bitmap_set_t;
 
 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi)		\
-  EXECUTE_IF_SET_IN_BITMAP(set->expressions, 0, id, bi)
+  EXECUTE_IF_SET_IN_BITMAP((set)->expressions, 0, (id), (bi))
+
+/* Mapping from value id to expressions with that value_id.  */
+DEF_VEC_P (bitmap_set_t);
+DEF_VEC_ALLOC_P (bitmap_set_t, heap);
+static VEC(bitmap_set_t, heap) *value_expressions;
 
 /* Sets that we need to keep track of.  */
 typedef struct bb_bitmap_sets
@@ -347,6 +391,7 @@
 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
 
+      
 /* Maximal set of values, used to initialize the ANTIC problem, which
    is an intersection problem.  */
 static bitmap_set_t maximal_set;
@@ -376,31 +421,23 @@
 } pre_stats;
 
 static bool do_partial_partial;
-static tree bitmap_find_leader (bitmap_set_t, tree, tree);
-static void bitmap_value_insert_into_set (bitmap_set_t, tree);
-static void bitmap_value_replace_in_set (bitmap_set_t, tree);
+static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int , tree);
+static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
+static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
-static bool bitmap_set_contains_value (bitmap_set_t, tree);
-static void bitmap_insert_into_set (bitmap_set_t, tree);
+static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
+static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
+static void bitmap_insert_into_set_1 (bitmap_set_t, pre_expr, bool);
 static bitmap_set_t bitmap_set_new (void);
-static tree create_expression_by_pieces (basic_block, tree, tree, tree);
-static tree find_or_generate_expression (basic_block, tree, tree, tree);
+static tree create_expression_by_pieces (basic_block, pre_expr, tree, tree,
+					 tree);
+static tree find_or_generate_expression (basic_block, pre_expr, tree, tree);
 
 /* We can add and remove elements and entries to and from sets
    and hash tables, so we use alloc pools for them.  */
 
 static alloc_pool bitmap_set_pool;
-static alloc_pool binary_node_pool;
-static alloc_pool unary_node_pool;
-static alloc_pool reference_node_pool;
-static alloc_pool comparison_node_pool;
 static bitmap_obstack grand_bitmap_obstack;
-
-/* We can't use allocation pools to hold temporary CALL_EXPR objects, since
-   they are not of fixed size.  Instead, use an obstack.  */
-
-static struct obstack temp_call_expr_obstack;
-
 
 /* To avoid adding 300 temporary variables when we only need one, we
    only create one temporary variable, on demand, and build ssa names
@@ -428,16 +465,13 @@
 typedef struct expr_pred_trans_d
 {
   /* The expression.  */
-  tree e;
+  pre_expr e;
 
   /* The predecessor block along which we translated the expression.  */
   basic_block pred;
 
-  /* vuses associated with the expression.  */
-  VEC (tree, gc) *vuses;
-
   /* The value that resulted from the translation.  */
-  tree v;
+  pre_expr v;
 
   /* The hashcode for the expression, pred pair. This is cached for
      speed reasons.  */
@@ -464,50 +498,27 @@
   const_expr_pred_trans_t const ve2 = (const_expr_pred_trans_t) p2;
   basic_block b1 = ve1->pred;
   basic_block b2 = ve2->pred;
-  int i;
-  tree vuse1;
 
   /* If they are not translations for the same basic block, they can't
      be equal.  */
   if (b1 != b2)
     return false;
-
-
-  /* If they are for the same basic block, determine if the
-     expressions are equal.  */
-  if (!expressions_equal_p (ve1->e, ve2->e))
-    return false;
-
-  /* Make sure the vuses are equivalent.  */
-  if (ve1->vuses == ve2->vuses)
-    return true;
-
-  if (VEC_length (tree, ve1->vuses) != VEC_length (tree, ve2->vuses))
-    return false;
-
-  for (i = 0; VEC_iterate (tree, ve1->vuses, i, vuse1); i++)
-    {
-      if (VEC_index (tree, ve2->vuses, i) != vuse1)
-	return false;
-    }
-
-  return true;
+  return pre_expr_eq (ve1->e, ve2->e);
 }
 
 /* Search in the phi translation table for the translation of
-   expression E in basic block PRED with vuses VUSES.
+   expression E in basic block PRED. 
    Return the translated value, if found, NULL otherwise.  */
 
-static inline tree
-phi_trans_lookup (tree e, basic_block pred, VEC (tree, gc) *vuses)
+static inline pre_expr
+phi_trans_lookup (pre_expr e, basic_block pred)
 {
   void **slot;
   struct expr_pred_trans_d ept;
 
   ept.e = e;
   ept.pred = pred;
-  ept.vuses = vuses;
-  ept.hashcode = iterative_hash_expr (e, (unsigned long) pred);
+  ept.hashcode = pre_expr_hash (e);
   slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
 				   NO_INSERT);
   if (!slot)
@@ -517,19 +528,19 @@
 }
 
 
-/* Add the tuple mapping from {expression E, basic block PRED, vuses VUSES} to
+/* Add the tuple mapping from {expression E, basic block PRED} to
    value V, to the phi translation table.  */
 
 static inline void
-phi_trans_add (tree e, tree v, basic_block pred, VEC (tree, gc) *vuses)
+phi_trans_add (pre_expr e, pre_expr v, basic_block pred)
 {
   void **slot;
   expr_pred_trans_t new_pair = XNEW (struct expr_pred_trans_d);
   new_pair->e = e;
   new_pair->pred = pred;
-  new_pair->vuses = vuses;
   new_pair->v = v;
-  new_pair->hashcode = iterative_hash_expr (e, (unsigned long) pred);
+  new_pair->hashcode = pre_expr_hash (e);
+  
   slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
 				   new_pair->hashcode, INSERT);
   if (*slot)
@@ -538,29 +549,27 @@
 }
 
 
-/* Return true if V is a value expression that represents itself.
-   In our world, this is *only* non-value handles.  */
-
-static inline bool
-constant_expr_p (tree v)
-{
-  return TREE_CODE (v) != VALUE_HANDLE &&
-    (TREE_CODE (v) == FIELD_DECL || is_gimple_min_invariant (v));
-}
-
-/* Add expression E to the expression set of value V.  */
-
-void
-add_to_value (tree v, tree e)
-{
-  /* Constants have no expression sets.  */
-  if (constant_expr_p (v))
-    return;
-
-  if (VALUE_HANDLE_EXPR_SET (v) == NULL)
-    VALUE_HANDLE_EXPR_SET (v) = bitmap_set_new ();
-
-  bitmap_insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
+/* Add expression E to the expression set of value id V.  */
+
+void
+add_to_value (unsigned int v, pre_expr e)
+{
+  bitmap_set_t set;
+
+  if (v >= VEC_length (bitmap_set_t, value_expressions))
+    {
+      VEC_safe_grow_cleared (bitmap_set_t, heap, value_expressions,
+			     v + 1);
+    }
+    
+  set = VEC_index (bitmap_set_t, value_expressions, v);
+  if (!set)
+    {
+      set = bitmap_set_new ();
+      VEC_replace (bitmap_set_t, value_expressions, v, set);
+    }
+  
+  bitmap_insert_into_set_1 (set, e, true);
 }
 
 /* Create a new bitmap set and return it.  */
@@ -574,34 +583,59 @@
   return ret;
 }
 
+/* Return the value id for a PRE expression EXPR.  */
+
+static unsigned int
+get_expr_value_id (pre_expr expr)
+{
+  switch (expr->kind)
+    {
+    case CONSTANT:
+      return get_or_alloc_constant_value_id (PRE_EXPR_CONSTANT (expr));
+    case NAME:
+      return VN_INFO (PRE_EXPR_NAME (expr))->value_id;
+    case NARY:
+      return PRE_EXPR_NARY (expr)->value_id;
+    case REFERENCE:
+      return PRE_EXPR_REFERENCE (expr)->value_id;
+    default:
+      gcc_unreachable ();
+    }
+}
+
 /* Remove an expression EXPR from a bitmapped set.  */
 
 static void
-bitmap_remove_from_set (bitmap_set_t set, tree expr)
-{
-  tree val = get_value_handle (expr);
-
-  gcc_assert (val);
-  if (!constant_expr_p (val))
-    {
-      bitmap_clear_bit (set->values, VALUE_HANDLE_ID (val));
+bitmap_remove_from_set (bitmap_set_t set, pre_expr expr)
+{
+  unsigned int val  = get_expr_value_id (expr);
+  if (!value_id_constant_p (val)) 
+    {
+      bitmap_clear_bit (set->values, val);
       bitmap_clear_bit (set->expressions, get_expression_id (expr));
     }
 }
 
+static void
+bitmap_insert_into_set_1 (bitmap_set_t set, pre_expr expr,
+			  bool allow_constants)
+{
+  unsigned int val  = get_expr_value_id (expr);
+  if (allow_constants || !value_id_constant_p (val))
+    {
+      /* We specifically expect this and only this function to be able to
+	 insert constants into a set.  */
+      bitmap_set_bit (set->values, val);
+      bitmap_set_bit (set->expressions, get_or_alloc_expression_id (expr));
+    }
+}
+
 /* Insert an expression EXPR into a bitmapped set.  */
 
 static void
-bitmap_insert_into_set (bitmap_set_t set, tree expr)
-{
-  tree val = get_value_handle (expr);
-
-  gcc_assert (val);
-  if (!constant_expr_p (val))
-    {
-      bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
-      bitmap_set_bit (set->expressions, get_or_alloc_expression_id (expr));
-    }
+bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
+{
+  bitmap_insert_into_set_1 (set, expr, false);
 }
 
 /* Copy a bitmapped set ORIG, into bitmapped set DEST.  */
@@ -624,40 +658,31 @@
 
 
 /* A comparison function for use in qsort to top sort a bitmap set.  Simply
-   subtracts value handle ids, since they are created in topo-order.  */
+   subtracts value ids, since they are created in topo-order.  */
 
 static int
-vh_compare (const void *pa, const void *pb)
-{
-  const tree vha = get_value_handle (*((const tree *)pa));
-  const tree vhb = get_value_handle (*((const tree *)pb));
-
-  /* This can happen when we constify things.  */
-  if (constant_expr_p (vha))
-    {
-      if (constant_expr_p (vhb))
-	return -1;
-      return -1;
-    }
-  else if (constant_expr_p (vhb))
-    return 1;
-  return VALUE_HANDLE_ID (vha) - VALUE_HANDLE_ID (vhb);
+value_id_compare (const void *pa, const void *pb)
+{
+  const unsigned int vha = get_expr_value_id (*((const pre_expr *)pa));
+  const unsigned int vhb = get_expr_value_id (*((const pre_expr *)pb));
+
+  return vha - vhb;
 }
 
 /* Generate an topological-ordered array of bitmap set SET.  */
 
-static VEC(tree, heap) *
+static VEC(pre_expr, heap) *
 sorted_array_from_bitmap_set (bitmap_set_t set)
 {
   unsigned int i;
   bitmap_iterator bi;
-  VEC(tree, heap) *result = NULL;
+  VEC(pre_expr, heap) *result = NULL;
 
   FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
-    VEC_safe_push (tree, heap, result, expression_for_id (i));
-
-  qsort (VEC_address (tree, result), VEC_length (tree, result),
-	 sizeof (tree), vh_compare);
+    VEC_safe_push (pre_expr, heap, result, expression_for_id (i));
+
+  qsort (VEC_address (pre_expr, result), VEC_length (pre_expr, result),
+	 sizeof (pre_expr), value_id_compare);
 
   return result;
 }
@@ -678,9 +703,9 @@
       bitmap_copy (temp, dest->expressions);
       EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
 	{
-	  tree expr = expression_for_id (i);
-	  tree val = get_value_handle (expr);
-	  if (!bitmap_bit_p (dest->values, VALUE_HANDLE_ID (val)))
+	  pre_expr expr = expression_for_id (i);
+	  unsigned int value_id = get_expr_value_id (expr);
+	  if (!bitmap_bit_p (dest->values, value_id))
 	    bitmap_clear_bit (dest->expressions, i);
 	}
       BITMAP_FREE (temp);
@@ -701,9 +726,9 @@
 
   FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
     {
-      tree expr = expression_for_id (i);
-      tree val = get_value_handle (expr);
-      bitmap_set_bit (result->values, VALUE_HANDLE_ID (val));
+      pre_expr expr = expression_for_id (i);
+      unsigned int value_id = get_expr_value_id (expr);
+      bitmap_set_bit (result->values, value_id);
     }
 
   return result;
@@ -721,30 +746,30 @@
   bitmap_copy (temp, a->expressions);
   EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
     {
-      tree expr = expression_for_id (i);
-      if (bitmap_set_contains_value (b, get_value_handle (expr)))
+      pre_expr expr = expression_for_id (i);
+      if (bitmap_set_contains_value (b, get_expr_value_id (expr)))
 	bitmap_remove_from_set (a, expr);
     }
   BITMAP_FREE (temp);
 }
 
 
-/* Return true if bitmapped set SET contains the value VAL.  */
-
-static bool
-bitmap_set_contains_value (bitmap_set_t set, tree val)
-{
-  if (constant_expr_p (val))
+/* Return true if bitmapped set SET contains the value VALUE_ID.  */
+
+static bool
+bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
+{
+  if (value_id_constant_p (value_id))
     return true;
 
   if (!set || bitmap_empty_p (set->expressions))
     return false;
 
-  return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
+  return bitmap_bit_p (set->values, value_id);
 }
 
 static inline bool
-bitmap_set_contains_expr (bitmap_set_t set, tree expr)
+bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
 {
   return bitmap_bit_p (set->expressions, get_expression_id (expr));
 }
@@ -752,13 +777,14 @@
 /* Replace an instance of value LOOKFOR with expression EXPR in SET.  */
 
 static void
-bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
+bitmap_set_replace_value (bitmap_set_t set, unsigned int lookfor,
+			  const pre_expr expr)
 {
   bitmap_set_t exprset;
   unsigned int i;
   bitmap_iterator bi;
 
-  if (constant_expr_p (lookfor))
+  if (value_id_constant_p (lookfor))
     return;
 
   if (!bitmap_set_contains_value (set, lookfor))
@@ -773,7 +799,7 @@
      5-10x faster than walking the bitmap.  If this is somehow a
      significant lose for some cases, we can choose which set to walk
      based on the set size.  */
-  exprset = VALUE_HANDLE_EXPR_SET (lookfor);
+  exprset = VEC_index (bitmap_set_t, value_expressions, lookfor);
   FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
     {
       if (bitmap_bit_p (set->expressions, i))
@@ -797,9 +823,9 @@
    and add it otherwise.  */
 
 static void
-bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
-{
-  tree val = get_value_handle (expr);
+bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
+{
+  unsigned int val = get_expr_value_id (expr);
 
   if (bitmap_set_contains_value (set, val))
     bitmap_set_replace_value (set, val, expr);
@@ -811,17 +837,89 @@
    SET.  */
 
 static void
-bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
-{
-  tree val = get_value_handle (expr);
-
-  if (constant_expr_p (val))
+bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
+{
+  unsigned int val = get_expr_value_id (expr);
+
+  if (value_id_constant_p (val))
     return;
 
   if (!bitmap_set_contains_value (set, val))
     bitmap_insert_into_set (set, expr);
 }
 
+/* Print out EXPR to outfile.  */
+
+static void
+print_pre_expr (FILE *outfile, const pre_expr expr) 
+{
+  switch (expr->kind)
+    {
+    case CONSTANT:
+      print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr), 0);
+      break;
+    case NAME:
+      print_generic_expr (outfile, PRE_EXPR_NAME (expr), 0);
+      break;
+    case NARY:
+      {
+	unsigned int i;
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	fprintf (outfile, "{%s,", tree_code_name [nary->opcode]);
+	for (i = 0; i < nary->length; i++)
+	  {
+	    print_generic_expr (outfile, nary->op[i], 0);
+	    if (i != (unsigned) nary->length - 1)
+	      fprintf (outfile, ",");
+	  }
+	fprintf (outfile, "}");
+      }
+      break;
+      
+    case REFERENCE:
+      {
+	vn_reference_op_t vro;
+	unsigned int i;
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	fprintf (outfile, "{");
+	for (i = 0;
+	     VEC_iterate (vn_reference_op_s, ref->operands, i, vro); 
+	     i++)
+	  {
+	    if (vro->opcode != SSA_NAME
+		&& TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
+	      fprintf (outfile, "%s ", tree_code_name [vro->opcode]);
+	    if (vro->op0)
+	      {
+		if (vro->op1)
+		  fprintf (outfile, "<");
+		print_generic_expr (outfile, vro->op0, 0);
+		if (vro->op1)
+		  {
+		    fprintf (outfile, ",");
+		    print_generic_expr (outfile, vro->op1, 0);
+		  }
+		if (vro->op1)
+		  fprintf (outfile, ">");
+	      }
+	    if (i != VEC_length (vn_reference_op_s, ref->operands) - 1)
+	      fprintf (outfile, ",");
+	  }
+	fprintf (outfile, "}");
+      }
+      break;
+    }
+}
+void debug_pre_expr (pre_expr);
+
+/* Like print_pre_expr but always prints to stderr.  */
+void
+debug_pre_expr (pre_expr e)
+{
+  print_pre_expr (stderr, e);
+  fprintf (stderr, "\n");
+}
+
 /* Print out SET to OUTFILE.  */
 
 static void
@@ -837,16 +935,14 @@
 
       FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
 	{
-	  tree expr = expression_for_id (i);
+	  const pre_expr expr = expression_for_id (i);
 
 	  if (!first)
 	    fprintf (outfile, ", ");
 	  first = false;
-	  print_generic_expr (outfile, expr, 0);
-
-	  fprintf (outfile, " (");
-	  print_generic_expr (outfile, get_value_handle (expr), 0);
-	  fprintf (outfile, ") ");
+	  print_pre_expr (outfile, expr);
+
+	  fprintf (outfile, " (%04d)", get_expr_value_id (expr));
 	}
     }
   fprintf (outfile, " }\n");
@@ -863,42 +959,141 @@
 /* Print out the expressions that have VAL to OUTFILE.  */
 
 void
-print_value_expressions (FILE *outfile, tree val)
-{
-  if (VALUE_HANDLE_EXPR_SET (val))
+print_value_expressions (FILE *outfile, unsigned int val)
+{
+  bitmap_set_t set = VEC_index (bitmap_set_t, value_expressions, val);
+  if (set)
     {
       char s[10];
-      sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
-      print_bitmap_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
-    }
-}
-
-
-void
-debug_value_expressions (tree val)
+      sprintf (s, "%04d", val);
+      print_bitmap_set (outfile, set, s, 0);
+    }
+}
+
+
+void
+debug_value_expressions (unsigned int val)
 {
   print_value_expressions (stderr, val);
+}
+
+/* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
+   represent it.  */
+
+static pre_expr
+get_or_alloc_expr_for_constant (tree constant)
+{
+  unsigned int result_id;
+  unsigned int value_id;
+  pre_expr newexpr = (pre_expr) pool_alloc (pre_expr_pool);
+  newexpr->kind = CONSTANT;
+  PRE_EXPR_CONSTANT (newexpr) = constant;
+  result_id = lookup_expression_id (newexpr);
+  if (result_id != 0)
+    {
+      pool_free (pre_expr_pool, newexpr);
+      newexpr = expression_for_id (result_id);
+      return newexpr;
+    }
+  value_id = get_or_alloc_constant_value_id (constant);
+  get_or_alloc_expression_id (newexpr);
+  add_to_value (value_id, newexpr);
+  return newexpr;
+}
+
+/* Given a value id V, find the actual tree representing the constant
+   value if there is one, and return it. Return NULL if we can't find
+   a constant.  */
+
+static tree 
+get_constant_for_value_id (unsigned int v)
+{
+  if (value_id_constant_p (v))
+    {
+      unsigned int i;
+      bitmap_iterator bi;
+      bitmap_set_t exprset = VEC_index (bitmap_set_t, value_expressions, v);
+      
+      FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
+	{
+	  pre_expr expr = expression_for_id (i);
+	  if (expr->kind == CONSTANT)
+	    return PRE_EXPR_CONSTANT (expr);
+	}
+    }
+  return NULL;
+}
+
+/* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
+   Currently only supports constants and SSA_NAMES.  */
+static pre_expr
+get_or_alloc_expr_for (tree t)
+{
+  if (TREE_CODE (t) == SSA_NAME)
+    return get_or_alloc_expr_for_name (t);
+  else if (is_gimple_min_invariant (t))
+    return get_or_alloc_expr_for_constant (t);
+  return NULL;
 }
 
 /* Return the folded version of T if T, when folded, is a gimple
    min_invariant.  Otherwise, return T.  */
 
-static tree
-fully_constant_expression (tree t)
-{
-  tree folded;
-  folded = fold (t);
-  if (folded && is_gimple_min_invariant (folded))
-    return folded;
-  return t;
-}
-
-/* Make a temporary copy of a CALL_EXPR object NODE.  */
-
-static tree
-temp_copy_call_expr (tree node)
-{
-  return (tree) obstack_copy (&temp_call_expr_obstack, node, tree_size (node));
+static pre_expr
+fully_constant_expression (pre_expr e)
+{
+  switch (e->kind) 
+    {
+    case CONSTANT:
+      return e;
+    case NARY:
+      {
+	vn_nary_op_t nary = PRE_EXPR_NARY (e);
+	switch (TREE_CODE_CLASS (nary->opcode))
+	  {
+	  case tcc_binary:
+	    {
+	      /* We have to go from trees to pre exprs to value ids to
+		 constants.  */
+	      tree naryop0 = nary->op[0];
+	      tree naryop1 = nary->op[1];
+	      pre_expr rep0 = get_or_alloc_expr_for (naryop0);
+	      pre_expr rep1 = get_or_alloc_expr_for (naryop1);
+	      unsigned int vrep0 = get_expr_value_id (rep0);
+	      unsigned int vrep1 = get_expr_value_id (rep1);	      
+	      tree const0 = get_constant_for_value_id (vrep0);
+	      tree const1 = get_constant_for_value_id (vrep1);
+	      tree result = NULL;
+	      if (const0 && const1)
+		result = fold_binary (nary->opcode, nary->type, const0,
+				      const1);
+	      if (result && is_gimple_min_invariant (result))
+		return get_or_alloc_expr_for_constant (result);
+	      return e;
+	    }
+	  case tcc_unary:
+	    {
+	    /* We have to go from trees to pre exprs to value ids to
+	       constants.  */
+	      tree naryop0 = nary->op[0];
+	      pre_expr rep0 = get_or_alloc_expr_for (naryop0);
+	      unsigned int vrep0 = get_expr_value_id (rep0);
+	      tree const0 = get_constant_for_value_id (vrep0);
+	      tree result = NULL;
+	      if (const0)
+		result = fold_unary (nary->opcode, nary->type, const0);
+	      if (result && is_gimple_min_invariant (result))
+		return get_or_alloc_expr_for_constant (result);
+	      return e;
+	    }
+	  default:
+	    return e;
+	  }
+      }
+    default:
+      return e;
+    }
+  return e;
 }
 
 /* Translate the vuses in the VUSES vector backwards through phi nodes
@@ -949,44 +1144,155 @@
    SET2.  This is used to avoid making a set consisting of the union
    of PA_IN and ANTIC_IN during insert.  */
 
-static inline tree
-find_leader_in_sets (tree expr, bitmap_set_t set1, bitmap_set_t set2)
-{
-  tree result;
-
-  result = bitmap_find_leader (set1, expr, NULL_TREE);
+static inline pre_expr
+find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2)
+{
+  pre_expr result;
+
+  result = bitmap_find_leader (set1, val, NULL_TREE);
   if (!result && set2)
-    result = bitmap_find_leader (set2, expr, NULL_TREE);
+    result = bitmap_find_leader (set2, val, NULL_TREE);
   return result;
 }
 
+/* Get the tree type for our PRE expression e.  */
+
+static tree
+get_expr_type (const pre_expr e) 
+{
+  switch (e->kind) 
+    {
+    case NAME:
+      return TREE_TYPE (PRE_EXPR_NAME (e));
+    case CONSTANT:
+      return TREE_TYPE (PRE_EXPR_CONSTANT (e));
+    case REFERENCE:
+      {
+	vn_reference_op_t vro;
+
+	gcc_assert (PRE_EXPR_REFERENCE (e)->operands);
+	vro = VEC_index (vn_reference_op_s,
+			 PRE_EXPR_REFERENCE (e)->operands,
+			 0);
+	/* We don't store type along with COMPONENT_REF because it is
+	   always the same as FIELD_DECL's type.  */
+	if (!vro->type)
+	  {
+	    gcc_assert (vro->opcode == COMPONENT_REF);
+	    return TREE_TYPE (vro->op0);
+	  }
+	return vro->type;
+      }
+
+    case NARY:
+      return PRE_EXPR_NARY (e)->type;
+    }
+  gcc_unreachable();
+}
+
+/* Get a representative SSA_NAME for a given expression.
+   Since all of our sub-expressions are treated as values, we require
+   them to be SSA_NAME's for simplicity.
+   Prior versions of GVNPRE used to use "value handles" here, so that
+   an expression would be VH.11 + VH.10 instead of d_3 + e_6.  In
+   either case, the operands are really values (IE we do not expect
+   them to be usable without finding leaders).  */
+
+static tree
+get_representative_for (const pre_expr e) 
+{
+  tree exprtype;
+  tree name;
+  unsigned int value_id = get_expr_value_id (e);
+
+  switch (e->kind)
+    {
+    case NAME:
+      return PRE_EXPR_NAME (e);
+    case CONSTANT:
+    case NARY:      
+    case REFERENCE:
+      {
+	/* Go through all of the expressions representing this value
+	   and pick out an SSA_NAME.  */
+	unsigned int i;
+	bitmap_iterator bi;
+	bitmap_set_t exprs = VEC_index (bitmap_set_t, value_expressions,
+					value_id);
+	FOR_EACH_EXPR_ID_IN_SET (exprs, i, bi)
+	  {
+	    pre_expr rep = expression_for_id (i);
+	    if (rep->kind == NAME)
+	      return PRE_EXPR_NAME (rep);
+	  }
+      }
+      break;
+    }
+  /* If we reached here we couldn't find an SSA_NAME.  This can
+     happen when we've discovered a value that has never appeared in
+     the program as set to an SSA_NAME, most likely as the result of
+     phi translation.  */
+  if (dump_file)
+    {
+      fprintf (dump_file,
+	       "Could not find SSA_NAME representative for expression:");
+      print_pre_expr (dump_file, e);
+      fprintf (dump_file, "\n");
+    }
+  
+  exprtype = get_expr_type (e);
+  
+  /* Build and insert the assignment of the end result to the temporary
+     that we will return.  */
+  if (!pretemp || exprtype != TREE_TYPE (pretemp))
+    {
+      pretemp = create_tmp_var (exprtype, "pretmp");
+      get_var_ann (pretemp);
+    }
+
+  name = make_ssa_name (pretemp, build_empty_stmt ());
+  VN_INFO_GET (name)->value_id = value_id;
+  if (e->kind == CONSTANT)
+    VN_INFO (name)->valnum = PRE_EXPR_CONSTANT (e);
+  else
+    VN_INFO (name)->valnum = name;
+
+  add_to_value (value_id, get_or_alloc_expr_for_name (name));
+  if (dump_file)
+    {
+      fprintf (dump_file, "Created SSA_NAME representative ");
+      print_generic_expr (dump_file, name, 0);
+      fprintf (dump_file, " for expression:");
+      print_pre_expr (dump_file, e);
+      fprintf (dump_file, "\n");
+    }
+
+  return name;  
+}
+
+	    
+
+      
 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
    the phis in PRED.  SEEN is a bitmap saying which expression we have
    translated since we started translation of the toplevel expression.
    Return NULL if we can't find a leader for each part of the
    translated expression.  */
 
-static tree
-phi_translate_1 (tree expr, bitmap_set_t set1, bitmap_set_t set2,
+static pre_expr
+phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
 		 basic_block pred, basic_block phiblock, bitmap seen)
 {
-  tree phitrans = NULL;
-  tree oldexpr = expr;
-
-  if (expr == NULL)
+  pre_expr oldexpr = expr;
+  pre_expr phitrans;
+  
+  if (!expr)
     return NULL;
-
-  if (constant_expr_p (expr))
+  
+  if (value_id_constant_p (get_expr_value_id (expr)))
     return expr;
-
-  /* Phi translations of a given expression don't change.  */
-  if (EXPR_P (expr) || GIMPLE_STMT_P (expr))
-    {
-      phitrans = phi_trans_lookup (expr, pred, get_expression_vuses (expr));
-    }
-  else
-    phitrans = phi_trans_lookup (expr, pred, NULL);
-
+  
+  phitrans = phi_trans_lookup (expr, pred);
   if (phitrans)
     return phitrans;
 
@@ -1000,317 +1306,223 @@
       bitmap_set_bit (seen, expr_id);
     }
 
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_expression:
-      return NULL;
-
-    case tcc_vl_exp:
-      {
-	if (TREE_CODE (expr) != CALL_EXPR)
-	  return NULL;
-	else
-	  {
-	    tree oldfn = CALL_EXPR_FN (expr);
-	    tree oldsc = CALL_EXPR_STATIC_CHAIN (expr);
-	    tree newfn, newsc = NULL;
-	    tree newexpr = NULL_TREE;
-	    bool invariantarg = false;
-	    int i, nargs;
-	    VEC (tree, gc) *vuses = get_expression_vuses (expr);
-	    VEC (tree, gc) *tvuses;
-
-	    newfn = phi_translate_1 (find_leader_in_sets (oldfn, set1, set2),
-				     set1, set2, pred, phiblock, seen);
-	    if (newfn == NULL)
-	      return NULL;
-	    if (newfn != oldfn)
-	      {
-		newexpr = temp_copy_call_expr (expr);
-		CALL_EXPR_FN (newexpr) = get_value_handle (newfn);
-	      }
-	    if (oldsc)
-	      {
-		newsc = phi_translate_1 (find_leader_in_sets (oldsc, set1, set2),
-					 set1, set2, pred, phiblock, seen);
-		if (newsc == NULL)
+  switch (expr->kind)
+    {
+      /* Constants contain no values that need translation.  */
+    case CONSTANT:
+      return expr;
+      
+    case NARY:
+      {
+	unsigned int i;
+	bool changed = false;	
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	struct vn_nary_op_s newnary;
+	/* The NARY structure is only guaranteed to have been
+	   allocated to the nary->length operands.  */
+	memcpy (&newnary, nary, (sizeof (struct vn_nary_op_s)
+				 - sizeof (tree) * (4 - nary->length)));
+	
+	for (i = 0; i < newnary.length; i++)
+	  {
+	    if (TREE_CODE (newnary.op[i]) != SSA_NAME)
+	      continue;
+	    else
+	      {
+		unsigned int op_val_id = VN_INFO (newnary.op[i])->value_id;
+		pre_expr leader = find_leader_in_sets (op_val_id, set1, set2);
+		pre_expr result = phi_translate_1 (leader, set1, set2,
+						   pred, phiblock, seen);
+		if (result && result != leader)
+		  {
+		    tree name = get_representative_for (result);
+		    if (!name)
+		      return NULL;
+		    newnary.op[i] = name;
+		  }
+		else if (!result)
 		  return NULL;
-		if (newsc != oldsc)
-		  {
-		    if (!newexpr)
-		      newexpr = temp_copy_call_expr (expr);
-		    CALL_EXPR_STATIC_CHAIN (newexpr) = get_value_handle (newsc);
-		  }
-	      }
-
-	    /* phi translate the argument list piece by piece.  */
-	    nargs = call_expr_nargs (expr);
-	    for (i = 0; i < nargs; i++)
-	      {
-		tree oldval = CALL_EXPR_ARG (expr, i);
-		tree newval;
-		if (oldval)
-		  {
-		    /* This may seem like a weird place for this
-		       check, but it's actually the easiest place to
-		       do it.  We can't do it lower on in the
-		       recursion because it's valid for pieces of a
-		       component ref to be of AGGREGATE_TYPE, as long
-		       as the outermost one is not.
-		       To avoid *that* case, we have a check for
-		       AGGREGATE_TYPE_P in insert_aux.  However, that
-		       check will *not* catch this case because here
-		       it occurs in the argument list.  */
-		    if (AGGREGATE_TYPE_P (TREE_TYPE (oldval)))
-		      return NULL;
-		    oldval = find_leader_in_sets (oldval, set1, set2);
-		    newval = phi_translate_1 (oldval, set1, set2, pred,
-					    phiblock, seen);
-		    if (newval == NULL)
-		      return NULL;
-		    if (newval != oldval)
-		      {
-			invariantarg |= is_gimple_min_invariant (newval);
-			if (!newexpr)
-			  newexpr = temp_copy_call_expr (expr);
-			CALL_EXPR_ARG (newexpr, i) = get_value_handle (newval);
-		      }
-		  }
-	      }
-
-	    /* In case of new invariant args we might try to fold the call
-	       again.  */
-	    if (invariantarg && !newsc)
-	      {
-		tree tmp1 = build_call_array (TREE_TYPE (expr),
-					      newfn, call_expr_nargs (newexpr),
-					      CALL_EXPR_ARGP (newexpr));
-		tree tmp2 = fold (tmp1);
-		if (tmp2 != tmp1)
-		  {
-		    STRIP_TYPE_NOPS (tmp2);
-		    if (is_gimple_min_invariant (tmp2))
-		      return tmp2;
-		  }
-	      }
-
-	    tvuses = translate_vuses_through_block (vuses, phiblock, pred);
-	    if (vuses != tvuses && ! newexpr)
-	      newexpr = temp_copy_call_expr (expr);
-
-	    if (newexpr)
-	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add_with_vuses (newexpr, tvuses);
-		expr = newexpr;
-		set_expression_vuses (newexpr, tvuses);
-	      }
-	    phi_trans_add (oldexpr, expr, pred, tvuses);
-	  }
-      }
-      return expr;
-
-    case tcc_declaration:
-      {
-	VEC (tree, gc) * oldvuses = NULL;
-	VEC (tree, gc) * newvuses = NULL;
-
-	oldvuses = get_expression_vuses (expr);
-	if (oldvuses)
-	  newvuses = translate_vuses_through_block (oldvuses, phiblock,
-						    pred);
-
-	if (oldvuses != newvuses)
-	  {
-	    vn_lookup_or_add_with_vuses (expr, newvuses);
-	    set_expression_vuses (expr, newvuses);
-	  }
-	phi_trans_add (oldexpr, expr, pred, newvuses);
-      }
-      return expr;
-
-    case tcc_reference:
-      {
-	tree oldop0 = TREE_OPERAND (expr, 0);
-	tree oldop1 = NULL;
-	tree newop0;
-	tree newop1 = NULL;
-	tree oldop2 = NULL;
-	tree newop2 = NULL;
-	tree oldop3 = NULL;
-	tree newop3 = NULL;
-	tree newexpr;
-	VEC (tree, gc) * oldvuses = NULL;
-	VEC (tree, gc) * newvuses = NULL;
-
-	if (TREE_CODE (expr) != INDIRECT_REF
-	    && TREE_CODE (expr) != COMPONENT_REF
-	    && TREE_CODE (expr) != ARRAY_REF)
-	  return NULL;
-
-	oldop0 = find_leader_in_sets (oldop0, set1, set2);
-	newop0 = phi_translate_1 (oldop0, set1, set2, pred, phiblock, seen);
-	if (newop0 == NULL)
-	  return NULL;
-
-	if (TREE_CODE (expr) == ARRAY_REF)
-	  {
-	    oldop1 = TREE_OPERAND (expr, 1);
-	    oldop1 = find_leader_in_sets (oldop1, set1, set2);
-	    newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
-
-	    if (newop1 == NULL)
-	      return NULL;
-
-	    oldop2 = TREE_OPERAND (expr, 2);
-	    if (oldop2)
-	      {
-		oldop2 = find_leader_in_sets (oldop2, set1, set2);
-		newop2 = phi_translate_1 (oldop2, set1, set2, pred, phiblock, seen);
-
-		if (newop2 == NULL)
+		
+		changed |= newnary.op[i] != nary->op[i];
+	      }
+	  }
+	if (changed)
+	  {
+	    pre_expr constant;
+	    
+	    tree result = vn_nary_op_lookup_pieces (newnary.length,
+						    newnary.opcode,
+						    newnary.type,
+						    newnary.op[0],
+						    newnary.op[1],
+						    newnary.op[2],
+						    newnary.op[3],
+						    &nary);
+	    unsigned int new_val_id;
+	    
+	    expr = (pre_expr) pool_alloc (pre_expr_pool);
+	    expr->kind = NARY;
+	    expr->id = 0;
+	    if (result && is_gimple_min_invariant (result))
+	      return get_or_alloc_expr_for_constant (result);
+
+
+	    if (nary)
+	      {
+		PRE_EXPR_NARY (expr) = nary;
+		constant = fully_constant_expression (expr);
+		if (constant != expr)
+		  return constant;
+
+		new_val_id = nary->value_id;
+		get_or_alloc_expression_id (expr);
+	      }
+	    else
+	      {
+		new_val_id = get_next_value_id ();
+		VEC_safe_grow_cleared (bitmap_set_t, heap,
+				       value_expressions,
+				       get_max_value_id() + 1);
+		nary = vn_nary_op_insert_pieces (newnary.length,
+						 newnary.opcode,
+						 newnary.type,
+						 newnary.op[0],
+						 newnary.op[1],
+						 newnary.op[2],
+						 newnary.op[3],
+						 result, new_val_id);
+		PRE_EXPR_NARY (expr) = nary;
+		constant = fully_constant_expression (expr);
+		if (constant != expr)
+		  return constant;
+		get_or_alloc_expression_id (expr);
+		add_to_value (new_val_id, expr);
+	      }
+	  }
+	phi_trans_add (oldexpr, expr, pred);
+	return expr;
+      }
+      break;
+    case REFERENCE:
+      {
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	VEC (vn_reference_op_s, heap) *operands = ref->operands;
+	VEC (tree, gc) *vuses = ref->vuses;
+	VEC (tree, gc) *newvuses = vuses;	
+	VEC (vn_reference_op_s, heap) *newoperands = NULL;
+	bool changed = false;
+	unsigned int i;
+	vn_reference_op_t operand;
+	vn_reference_t newref;
+
+	for (i = 0; VEC_iterate (vn_reference_op_s, operands, i, operand); i++)
+	  {
+	    pre_expr opresult;
+	    pre_expr leader;
+	    tree oldop0 = operand->op0;
+	    tree oldop1 = operand->op1;
+	    tree op0 = oldop0;
+	    tree op1 = oldop1;
+	    tree type = operand->type;
+	    vn_reference_op_s newop = *operand;
+
+	    if (op0 && TREE_CODE (op0) == SSA_NAME)
+	      {
+		unsigned int op_val_id = VN_INFO (op0)->value_id;
+		leader = find_leader_in_sets (op_val_id, set1, set2);
+		opresult = phi_translate_1 (leader, set1, set2,
+					    pred, phiblock, seen);
+		if (opresult && opresult != leader)
+		  {
+		    tree name = get_representative_for (opresult);
+		    if (!name)
+		      return NULL;		    
+		    op0 = name;
+		  }
+		else if (!opresult)
 		  return NULL;
 	      }
-	    oldop3 = TREE_OPERAND (expr, 3);
-	    if (oldop3)
-	      {
-		oldop3 = find_leader_in_sets (oldop3, set1, set2);
-		newop3 = phi_translate_1 (oldop3, set1, set2, pred, phiblock, seen);
-
-		if (newop3 == NULL)
+	    changed |= op0 != oldop0;
+	    
+	    if (op1 && TREE_CODE (op1) == SSA_NAME)
+	      {
+		unsigned int op_val_id = VN_INFO (op1)->value_id;
+		leader = find_leader_in_sets (op_val_id, set1, set2);
+		opresult = phi_translate_1 (leader, set1, set2,
+					    pred, phiblock, seen);
+		if (opresult && opresult != leader)
+		  {
+		    tree name = get_representative_for (opresult);
+		    if (!name)
+		      return NULL;		    
+		    op1 = name;
+		  }
+		else if (!opresult)
 		  return NULL;
 	      }
-	  }
-
-	oldvuses = get_expression_vuses (expr);
-	if (oldvuses)
-	  newvuses = translate_vuses_through_block (oldvuses, phiblock,
-						    pred);
-
-	if (newop0 != oldop0 || newvuses != oldvuses
-	    || newop1 != oldop1
-	    || newop2 != oldop2
-	    || newop3 != oldop3)
-	  {
-	    tree t;
-
-	    newexpr = (tree) pool_alloc (reference_node_pool);
-	    memcpy (newexpr, expr, tree_size (expr));
-	    TREE_OPERAND (newexpr, 0) = get_value_handle (newop0);
-	    if (TREE_CODE (expr) == ARRAY_REF)
-	      {
-		TREE_OPERAND (newexpr, 1) = get_value_handle (newop1);
-		if (newop2)
-		  TREE_OPERAND (newexpr, 2) = get_value_handle (newop2);
-		if (newop3)
-		  TREE_OPERAND (newexpr, 3) = get_value_handle (newop3);
-	      }
-
-	    t = fully_constant_expression (newexpr);
-
-	    if (t != newexpr)
-	      {
-		pool_free (reference_node_pool, newexpr);
-		newexpr = t;
+	    changed |= op1 != oldop1;
+	    if (!newoperands)
+	      newoperands = VEC_copy (vn_reference_op_s, heap, operands);
+	    /* We may have changed from an SSA_NAME to a constant */
+	    if (newop.opcode == SSA_NAME && TREE_CODE (op0) != SSA_NAME)
+	      newop.opcode = TREE_CODE (op0);
+	    newop.type = type;
+	    newop.op0 = op0;
+	    newop.op1 = op1;
+	    VEC_replace (vn_reference_op_s, newoperands, i, &newop);
+	  }
+	
+	newvuses = translate_vuses_through_block (vuses, phiblock, pred);
+	changed |= newvuses != vuses;
+
+	if (changed)
+	  {
+	    tree result = vn_reference_lookup_pieces (newvuses,
+						      newoperands,
+						      &newref);
+	    unsigned int new_val_id;
+
+	    if (newref)
+	      VEC_free (vn_reference_op_s, heap, newoperands);
+	    
+	    if (result && is_gimple_min_invariant (result))
+	      return get_or_alloc_expr_for_constant (result);
+
+	    expr = (pre_expr) pool_alloc (pre_expr_pool);
+	    expr->kind = REFERENCE;
+	    expr->id = 0;
+
+	    if (newref)
+	      {
+		PRE_EXPR_REFERENCE (expr) = newref;
+		new_val_id = newref->value_id;
+		get_or_alloc_expression_id (expr);
 	      }
 	    else
 	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add_with_vuses (newexpr, newvuses);
-		set_expression_vuses (newexpr, newvuses);
-	      }
-	    expr = newexpr;
-	  }
-	phi_trans_add (oldexpr, expr, pred, newvuses);
-      }
-      return expr;
-      break;
-
-    case tcc_binary:
-    case tcc_comparison:
-      {
-	tree oldop1 = TREE_OPERAND (expr, 0);
-	tree oldval1 = oldop1;
-	tree oldop2 = TREE_OPERAND (expr, 1);
-	tree oldval2 = oldop2;
-	tree newop1;
-	tree newop2;
-	tree newexpr;
-
-	oldop1 = find_leader_in_sets (oldop1, set1, set2);
-	newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
-	if (newop1 == NULL)
-	  return NULL;
-
-	oldop2 = find_leader_in_sets (oldop2, set1, set2);
-	newop2 = phi_translate_1 (oldop2, set1, set2, pred, phiblock, seen);
-	if (newop2 == NULL)
-	  return NULL;
-	if (newop1 != oldop1 || newop2 != oldop2)
-	  {
-	    tree t;
-	    newexpr = (tree) pool_alloc (binary_node_pool);
-	    memcpy (newexpr, expr, tree_size (expr));
-	    TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldval1 : get_value_handle (newop1);
-	    TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldval2 : get_value_handle (newop2);
-	    t = fully_constant_expression (newexpr);
-	    if (t != newexpr)
-	      {
-		pool_free (binary_node_pool, newexpr);
-		newexpr = t;
-	      }
-	    else
-	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add (newexpr);
-	      }
-	    expr = newexpr;
-	  }
-	phi_trans_add (oldexpr, expr, pred, NULL);
-      }
-      return expr;
-
-    case tcc_unary:
-      {
-	tree oldop1 = TREE_OPERAND (expr, 0);
-	tree newop1;
-	tree newexpr;
-
-	oldop1 = find_leader_in_sets (oldop1, set1, set2);
-	newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
-	if (newop1 == NULL)
-	  return NULL;
-	if (newop1 != oldop1)
-	  {
-	    tree t;
-	    newexpr = (tree) pool_alloc (unary_node_pool);
-	    memcpy (newexpr, expr, tree_size (expr));
-	    TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
-	    t = fully_constant_expression (newexpr);
-	    if (t != newexpr)
-	      {
-		pool_free (unary_node_pool, newexpr);
-		newexpr = t;
-	      }
-	    else
-	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add (newexpr);
-	      }
-	    expr = newexpr;
-	  }
-	phi_trans_add (oldexpr, expr, pred, NULL);
-      }
-      return expr;
-
-    case tcc_exceptional:
+		new_val_id = get_next_value_id ();
+		VEC_safe_grow_cleared (bitmap_set_t, heap, value_expressions,
+				       get_max_value_id() + 1);
+		newref = vn_reference_insert_pieces (newvuses,
+						     newoperands, 
+						     result, new_val_id);
+		PRE_EXPR_REFERENCE (expr) = newref;
+		get_or_alloc_expression_id (expr);
+		add_to_value (new_val_id, expr);
+	      }    
+	  }
+	phi_trans_add (oldexpr, expr, pred);
+	return expr;
+      }
+      break;
+    case NAME:
       {
 	tree phi = NULL;
 	edge e;
 	tree def_stmt;
-	gcc_assert (TREE_CODE (expr) == SSA_NAME);
-
-	def_stmt = SSA_NAME_DEF_STMT (expr);
+	tree name = PRE_EXPR_NAME (expr);
+
+	def_stmt = SSA_NAME_DEF_STMT (name);
 	if (TREE_CODE (def_stmt) == PHI_NODE
 	    && bb_for_stmt (def_stmt) == phiblock)
 	  phi = def_stmt;
@@ -1320,18 +1532,18 @@
 	e = find_edge (pred, bb_for_stmt (phi));
 	if (e)
 	  {
-	    tree val;
 	    tree def = PHI_ARG_DEF (phi, e->dest_idx);
-
+	    pre_expr newexpr;
+
+	    /* Handle constant. */
 	    if (is_gimple_min_invariant (def))
-	      return def;
+	      return get_or_alloc_expr_for_constant (def);
 
 	    if (TREE_CODE (def) == SSA_NAME && ssa_undefined_value_p (def))
 	      return NULL;
 
-	    val = get_value_handle (def);
-	    gcc_assert (val);
-	    return def;
+	    newexpr = get_or_alloc_expr_for_name (def);
+	    return newexpr;
 	  }
       }
       return expr;
@@ -1346,8 +1558,8 @@
    Return NULL if we can't find a leader for each part of the
    translated expression.  */
 
-static tree
-phi_translate (tree expr, bitmap_set_t set1, bitmap_set_t set2,
+static pre_expr
+phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
 	       basic_block pred, basic_block phiblock)
 {
   bitmap_clear (seen_during_translate);
@@ -1355,7 +1567,7 @@
 			  seen_during_translate);
 }
 
-/* For each expression in SET, translate the value handles through phi nodes
+/* For each expression in SET, translate the values through phi nodes
    in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
    expressions in DEST.  */
 
@@ -1363,8 +1575,8 @@
 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
 		   basic_block phiblock)
 {
-  VEC (tree, heap) *exprs;
-  tree expr;
+  VEC (pre_expr, heap) *exprs;
+  pre_expr expr;
   int i;
 
   if (!phi_nodes (phiblock))
@@ -1374,23 +1586,22 @@
     }
 
   exprs = sorted_array_from_bitmap_set (set);
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
-    {
-      tree translated;
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
+    {
+      pre_expr translated;
       translated = phi_translate (expr, set, NULL, pred, phiblock);
 
       /* Don't add constants or empty translations to the cache, since
 	 we won't look them up that way, or use the result, anyway.  */
-      if (translated && !is_gimple_min_invariant (translated))
-	{
-	  phi_trans_add (expr, translated, pred,
-			 get_expression_vuses (translated));
+      if (translated && !value_id_constant_p (get_expr_value_id (translated)))
+	{
+	  phi_trans_add (expr, translated, pred);
 	}
 
       if (translated != NULL)
 	bitmap_value_insert_into_set (dest, translated);
     }
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
 }
 
 /* Find the leader for a value (i.e., the name representing that
@@ -1398,15 +1609,22 @@
    makes sure the defining statement for the leader dominates it.
    Return NULL if no leader is found.  */
 
-static tree
-bitmap_find_leader (bitmap_set_t set, tree val, tree stmt)
-{
-  if (val == NULL)
-    return NULL;
-
-  if (constant_expr_p (val))
-    return val;
-
+static pre_expr
+bitmap_find_leader (bitmap_set_t set, unsigned int val, tree stmt)
+{
+  if (value_id_constant_p (val))
+    {
+      unsigned int i;
+      bitmap_iterator bi;
+      bitmap_set_t exprset = VEC_index (bitmap_set_t, value_expressions, val);
+
+      FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
+	{
+	  pre_expr expr = expression_for_id (i);
+	  if (expr->kind == CONSTANT)
+	    return expr;
+	}
+    }
   if (bitmap_set_contains_value (set, val))
     {
       /* Rather than walk the entire bitmap of expressions, and see
@@ -1422,15 +1640,17 @@
 	 choose which set to walk based on which set is smaller.  */
       unsigned int i;
       bitmap_iterator bi;
-      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
+      bitmap_set_t exprset = VEC_index (bitmap_set_t, value_expressions, val);
 
       EXECUTE_IF_AND_IN_BITMAP (exprset->expressions,
 				set->expressions, 0, i, bi)
 	{
-	  tree val = expression_for_id (i);
+	  pre_expr val = expression_for_id (i);
+	  /* At the point where stmt is not null, there should always
+	     be an SSA_NAME first in the list of expressions.  */
 	  if (stmt)
 	    {
-	      tree def_stmt = SSA_NAME_DEF_STMT (val);
+	      tree def_stmt = SSA_NAME_DEF_STMT (PRE_EXPR_NAME (val));
 	      if (TREE_CODE (def_stmt) != PHI_NODE
 		  && bb_for_stmt (def_stmt) == bb_for_stmt (stmt)
 		  && stmt_ann (def_stmt)->uid >= stmt_ann (stmt)->uid)
@@ -1450,11 +1670,11 @@
    ANTIC_IN set already.  */
 
 static bool
-value_dies_in_block_x (tree expr, basic_block block)
+value_dies_in_block_x (pre_expr expr, basic_block block)
 {
   int i;
   tree vuse;
-  VEC (tree, gc) *vuses = get_expression_vuses (expr);
+  VEC (tree, gc) *vuses = PRE_EXPR_REFERENCE (expr)->vuses;
 
   /* Conservatively, a value dies if it's vuses are defined in this
      block, unless they come from phi nodes (which are merge operations,
@@ -1472,120 +1692,100 @@
   return false;
 }
 
+
+#define union_contains_value(SET1, SET2, VAL)			\
+  (bitmap_set_contains_value ((SET1), (VAL))			\
+   || ((SET2) && bitmap_set_contains_value ((SET2), (VAL))))
+
+/* Determine if vn_reference_op_t VRO is legal in SET1 U SET2.
+ */
+static bool
+vro_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2,
+		   vn_reference_op_t vro) 
+{
+  if (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME)
+    {
+      struct pre_expr_d temp;
+      temp.kind = NAME;
+      temp.id = 0;
+      PRE_EXPR_NAME (&temp) = vro->op0;
+      temp.id = lookup_expression_id (&temp);
+      if (temp.id == 0)
+	return false;
+      if (!union_contains_value (set1, set2,
+				 get_expr_value_id (&temp)))
+	return false;
+    }
+  if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
+    {
+      struct pre_expr_d temp;
+      temp.kind = NAME;
+      temp.id = 0;
+      PRE_EXPR_NAME (&temp) = vro->op1;
+      temp.id = lookup_expression_id (&temp);
+      if (temp.id == 0)
+	return false;
+      if (!union_contains_value (set1, set2,
+				 get_expr_value_id (&temp)))
+	return false;
+    }
+  return true;
+}
+
 /* Determine if the expression EXPR is valid in SET1 U SET2.
    ONLY SET2 CAN BE NULL.
    This means that we have a leader for each part of the expression
    (if it consists of values), or the expression is an SSA_NAME.
    For loads/calls, we also see if the vuses are killed in this block.
-
-   NB: We never should run into a case where we have SSA_NAME +
-   SSA_NAME or SSA_NAME + value.  The sets valid_in_sets is called on,
-   the ANTIC sets, will only ever have SSA_NAME's or value expressions
-   (IE VALUE1 + VALUE2, *VALUE1, VALUE1 < VALUE2)  */
-
-#define union_contains_value(SET1, SET2, VAL)			\
-  (bitmap_set_contains_value ((SET1), (VAL))			\
-   || ((SET2) && bitmap_set_contains_value ((SET2), (VAL))))
-
-static bool
-valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree expr,
+*/
+
+static bool
+valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr,
 	       basic_block block)
 {
- switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_binary:
-    case tcc_comparison:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	tree op2 = TREE_OPERAND (expr, 1);
-
-	return union_contains_value (set1, set2, op1)
-	  && union_contains_value (set1, set2, op2);
-      }
-
-    case tcc_unary:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	return union_contains_value (set1, set2, op1);
-      }
-
-    case tcc_expression:
-      return false;
-
-    case tcc_vl_exp:
-      {
-	if (TREE_CODE (expr) == CALL_EXPR)
-	  {
-	    tree fn = CALL_EXPR_FN (expr);
-	    tree sc = CALL_EXPR_STATIC_CHAIN (expr);
-	    tree arg;
-	    call_expr_arg_iterator iter;
-
-	    /* Check the non-argument operands first.  */
-	    if (!union_contains_value (set1, set2, fn)
-		|| (sc && !union_contains_value (set1, set2, sc)))
+  switch (expr->kind)
+    {
+    case NAME:
+      return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
+    case NARY:
+      {
+	unsigned int i;
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	for (i = 0; i < nary->length; i++)
+	  {
+	    if (TREE_CODE (nary->op[i]) == SSA_NAME)
+	      {
+		struct pre_expr_d temp;
+		temp.kind = NAME;
+		temp.id = 0;
+		PRE_EXPR_NAME (&temp) = nary->op[i];
+		temp.id = lookup_expression_id (&temp);
+		if (temp.id == 0)
+		  return false;
+		if (!union_contains_value (set1, set2,
+					   get_expr_value_id (&temp)))
+		  return false;
+	      }
+	  }
+	return true;
+      }
+      break;
+    case REFERENCE: 
+      {
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	vn_reference_op_t vro;
+	unsigned int i;
+	
+	for (i = 0; VEC_iterate (vn_reference_op_s, ref->operands, i, vro); i++)
+	  {
+	    if (!vro_valid_in_sets (set1, set2, vro))
 	      return false;
-
-	    /* Now check the operands.  */
-	    FOR_EACH_CALL_EXPR_ARG (arg, iter, expr)
-	      {
-		if (!union_contains_value (set1, set2, arg))
-		  return false;
-	      }
-	    return !value_dies_in_block_x (expr, block);
-	  }
-	return false;
-      }
-
-    case tcc_reference:
-      {
-	if (TREE_CODE (expr) == INDIRECT_REF
-	    || TREE_CODE (expr) == COMPONENT_REF
-	    || TREE_CODE (expr) == ARRAY_REF)
-	  {
-	    tree op0 = TREE_OPERAND (expr, 0);
-	    gcc_assert (is_gimple_min_invariant (op0)
-			|| TREE_CODE (op0) == VALUE_HANDLE);
-	    if (!union_contains_value (set1, set2, op0))
-	      return false;
-	    if (TREE_CODE (expr) == ARRAY_REF)
-	      {
-		tree op1 = TREE_OPERAND (expr, 1);
-		tree op2 = TREE_OPERAND (expr, 2);
-		tree op3 = TREE_OPERAND (expr, 3);
-		gcc_assert (is_gimple_min_invariant (op1)
-			    || TREE_CODE (op1) == VALUE_HANDLE);
-		if (!union_contains_value (set1, set2, op1))
-		  return false;
-		gcc_assert (!op2 || is_gimple_min_invariant (op2)
-			    || TREE_CODE (op2) == VALUE_HANDLE);
-		if (op2
-		    && !union_contains_value (set1, set2, op2))
-		  return false;
-		gcc_assert (!op3 || is_gimple_min_invariant (op3)
-			    || TREE_CODE (op3) == VALUE_HANDLE);
-		if (op3
-		    && !union_contains_value (set1, set2, op3))
-		  return false;
-	    }
-	    return !value_dies_in_block_x (expr, block);
-	  }
-      }
-      return false;
-
-    case tcc_exceptional:
-      {
-	gcc_assert (TREE_CODE (expr) == SSA_NAME);
-	return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
-      }
-
-    case tcc_declaration:
-      return !value_dies_in_block_x (expr, block);
-
-    default:
-      /* No other cases should be encountered.  */
+	  }
+	return !value_dies_in_block_x (expr, block);
+      }
+    default:
       gcc_unreachable ();
-   }
+    }
 }
 
 /* Clean the set of expressions that are no longer valid in SET1 or
@@ -1597,16 +1797,16 @@
 static void
 dependent_clean (bitmap_set_t set1, bitmap_set_t set2, basic_block block)
 {
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set1);
-  tree expr;
-  int i;
-
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (set1);
+  pre_expr expr;
+  int i;
+
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
     {
       if (!valid_in_sets (set1, set2, expr, block))
 	bitmap_remove_from_set (set1, expr);
     }
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
 }
 
 /* Clean the set of expressions that are no longer valid in SET.  This
@@ -1616,16 +1816,16 @@
 static void
 clean (bitmap_set_t set, basic_block block)
 {
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set);
-  tree expr;
-  int i;
-
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (set);
+  pre_expr expr;
+  int i;
+
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
     {
       if (!valid_in_sets (set, NULL, expr, block))
 	bitmap_remove_from_set (set, expr);
     }
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
 }
 
 static sbitmap has_abnormal_preds;
@@ -1967,73 +2167,6 @@
   return changed;
 }
 
-/* Initialize data structures used for ANTIC and AVAIL.  */
-
-static void
-init_antic (void)
-{
-  basic_block bb;
-
-  next_expression_id = 0;
-  expressions = NULL;
-  expression_vuses = NULL;
-
-  postorder = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
-  post_order_compute (postorder, false, false);
-
-  bitmap_obstack_initialize (&grand_bitmap_obstack);
-  obstack_init (&temp_call_expr_obstack);
-  seen_during_translate = BITMAP_ALLOC (&grand_bitmap_obstack);
-
-  bitmap_set_pool = create_alloc_pool ("Bitmap sets",
-					sizeof (struct bitmap_set), 30);
-  binary_node_pool = create_alloc_pool ("Binary tree nodes",
-					tree_code_size (PLUS_EXPR), 30);
-  unary_node_pool = create_alloc_pool ("Unary tree nodes",
-				       tree_code_size (NEGATE_EXPR), 30);
-  reference_node_pool = create_alloc_pool ("Reference tree nodes",
-					   tree_code_size (ARRAY_REF), 30);
-  comparison_node_pool = create_alloc_pool ("Comparison tree nodes",
-					    tree_code_size (EQ_EXPR), 30);
-
-  phi_translate_table = htab_create (5110, expr_pred_trans_hash,
-				     expr_pred_trans_eq, free);
-  maximal_set = in_fre ? NULL : bitmap_set_new ();
-
-  FOR_ALL_BB (bb)
-    {
-      bb->aux = xcalloc (1, sizeof (struct bb_bitmap_sets));
-      EXP_GEN (bb) = bitmap_set_new ();
-      PHI_GEN (bb) = bitmap_set_new ();
-      TMP_GEN (bb) = bitmap_set_new ();
-      AVAIL_OUT (bb) = bitmap_set_new ();
-    }
-}
-
-/* Deinitialize data structures used for ANTIC and AVAIL.  */
-
-static void
-fini_antic (void)
-{
-  basic_block bb;
-
-  if (maximal_set)
-    bitmap_set_free (maximal_set);
-  free (postorder);
-  bitmap_obstack_release (&grand_bitmap_obstack);
-  free_alloc_pool (bitmap_set_pool);
-  free_alloc_pool (binary_node_pool);
-  free_alloc_pool (reference_node_pool);
-  free_alloc_pool (unary_node_pool);
-  free_alloc_pool (comparison_node_pool);
-
-  FOR_ALL_BB (bb)
-    {
-      free (bb->aux);
-      bb->aux = NULL;
-    }
-}
-
 /* Compute ANTIC and partial ANTIC sets.  */
 
 static void
@@ -2159,22 +2292,6 @@
 {
   return TREE_CODE (op) == FILTER_EXPR || TREE_CODE (op) == EXC_PTR_EXPR;
 }
-
-/* Return true if OP is a tree which we can perform value numbering
-   on.  */
-
-static bool
-can_value_number_operation (tree op)
-{
-  return (UNARY_CLASS_P (op)
-	  && !is_exception_related (TREE_OPERAND (op, 0)))
-    || BINARY_CLASS_P (op)
-    || COMPARISON_CLASS_P (op)
-    || REFERENCE_CLASS_P (op)
-    || (TREE_CODE (op) == CALL_EXPR
-	&& can_value_number_call (op));
-}
-
 
 /* Return true if OP is a tree which we can perform PRE on
    on.  This may not match the operations we can value number, but in
@@ -2218,90 +2335,178 @@
    are doing.
 */
 static tree
-create_component_ref_by_pieces (basic_block block, tree expr, tree stmts,
-				tree domstmt)
-{
-  tree genop = expr;
-  tree folded;
-
-  if (TREE_CODE (genop) == VALUE_HANDLE)
-    {
-      tree found = bitmap_find_leader (AVAIL_OUT (block), expr, domstmt);
-      if (found)
-	return found;
-    }
-
-  if (TREE_CODE (genop) == VALUE_HANDLE)
-    {
-      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
-      unsigned int firstbit = bitmap_first_set_bit (exprset->expressions);
-      genop = expression_for_id (firstbit);
-    }
-
-  switch TREE_CODE (genop)
-    {
+create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
+				unsigned int operand,
+				tree stmts,
+				tree domstmt,
+				bool in_call)
+{
+  vn_reference_op_t currop = VEC_index (vn_reference_op_s, ref->operands,
+					operand);
+  tree genop;
+  switch (currop->opcode) 
+    {
+    case CALL_EXPR:
+      {
+	tree folded;
+	unsigned int i;
+	vn_reference_op_t declop = VEC_index (vn_reference_op_s,
+					      ref->operands, 1);
+	unsigned int nargs = VEC_length (vn_reference_op_s, ref->operands) - 2;
+	tree *args = XNEWVEC (tree, nargs);
+
+	for (i = 0; i < nargs; i++)
+	  {
+	    args[i] = create_component_ref_by_pieces (block, ref,
+						      operand + 2 + i, stmts,
+						      domstmt, true);
+	  }
+	folded = build_call_array (currop->type, declop->op0, nargs, args);
+	free (args);
+	return folded;
+      }
+      break;
+    case REALPART_EXPR:
+    case IMAGPART_EXPR:
+    case VIEW_CONVERT_EXPR:
+      {
+	tree folded;
+	tree genop0 = create_component_ref_by_pieces (block, ref,
+						      operand + 1,
+						      stmts, domstmt,
+						      in_call);
+	if (!genop0)
+	  return NULL_TREE;
+	folded = fold_build1 (currop->opcode, currop->type,
+			      genop0);
+	return folded;
+      }
+      break;
+    case ALIGN_INDIRECT_REF:
+    case MISALIGNED_INDIRECT_REF:
+    case INDIRECT_REF:
+      {
+	/* Inside a CALL_EXPR op0 is the actual indirect_ref.  */
+	if (in_call)
+	  {
+	    tree folded;
+	    pre_expr op0expr = get_or_alloc_expr_for (TREE_OPERAND (currop->op0, 0));
+	    tree genop0 = find_or_generate_expression (block, op0expr, stmts, domstmt);
+	    if (!genop0)
+	      return NULL_TREE;
+	    folded = fold_build1 (currop->opcode, currop->type,
+				  genop0);
+	    return folded;
+	  }
+	else
+	  {
+
+	    tree folded;
+	    tree genop1 = create_component_ref_by_pieces (block, ref, operand + 1,
+		stmts, domstmt,
+		in_call);
+	    if (!genop1)
+	      return NULL_TREE;
+	    genop1 = fold_convert (build_pointer_type (currop->type),
+		genop1);
+
+	    folded = fold_build1 (currop->opcode, currop->type,
+		genop1);
+	    return folded;
+	  }
+      }
+      break;
+    case BIT_FIELD_REF:
+      {
+	tree folded;
+	tree genop0 = create_component_ref_by_pieces (block, ref, operand + 1,
+						      stmts, domstmt,
+						      in_call);
+	pre_expr op1expr = get_or_alloc_expr_for (currop->op0);
+	pre_expr op2expr = get_or_alloc_expr_for (currop->op1);
+	tree genop1;
+	tree genop2;
+	
+	if (!genop0)
+	  return NULL_TREE;
+	genop1 = find_or_generate_expression (block, op1expr, stmts, domstmt);
+	if (!genop1)
+	  return NULL_TREE;
+	genop2 = find_or_generate_expression (block, op2expr, stmts, domstmt);
+	if (!genop2)
+	  return NULL_TREE;
+	folded = fold_build3 (BIT_FIELD_REF, currop->type, genop0, genop1,
+			      genop2);
+	return folded;
+      }
+      
+      /* For array ref vn_reference_op's, operand 1 of the array ref
+	 is op0 of the reference op and operand 3 of the array ref is
+	 op1.  */
+    case ARRAY_RANGE_REF:
     case ARRAY_REF:
       {
-	tree op0;
-	tree op1, op2, op3;
-	op0 = create_component_ref_by_pieces (block,
-					      TREE_OPERAND (genop, 0),
-					      stmts, domstmt);
-	op1 = TREE_OPERAND (genop, 1);
-	if (TREE_CODE (op1) == VALUE_HANDLE)
-	  op1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	op2 = TREE_OPERAND (genop, 2);
-	if (op2 && TREE_CODE (op2) == VALUE_HANDLE)
-	  op2 = find_or_generate_expression (block, op2, stmts, domstmt);
-	op3 = TREE_OPERAND (genop, 3);
-	if (op3 && TREE_CODE (op3) == VALUE_HANDLE)
-	  op3 = find_or_generate_expression (block, op3, stmts, domstmt);
-	if (!op0 || !op1)
-	  return NULL_TREE;
-	folded = build4 (ARRAY_REF, TREE_TYPE (genop), op0, op1,
-			      op2, op3);
-	return folded;
-      }
+	vn_reference_op_t op0expr;
+	tree genop0;
+	tree genop1 = currop->op0;
+	pre_expr op1expr;
+	tree genop3;
+	op0expr = VEC_index (vn_reference_op_s, ref->operands, operand + 1);
+	genop0 = create_component_ref_by_pieces (block, ref, operand + 1,
+						 stmts, domstmt,
+						 in_call);
+	if (!genop0)
+	  return NULL_TREE;
+	op1expr = get_or_alloc_expr_for (genop1);
+	genop1 = find_or_generate_expression (block, op1expr, stmts, domstmt);
+	if (!genop1)
+	  return NULL_TREE;
+	genop3 = currop->op1;
+	return build4 (currop->opcode, currop->type, genop0, genop1,
+		       NULL_TREE, genop3);
+      }		      
     case COMPONENT_REF:
       {
 	tree op0;
 	tree op1;
-	op0 = create_component_ref_by_pieces (block,
-					      TREE_OPERAND (genop, 0),
-					      stmts, domstmt);
+	op0 = create_component_ref_by_pieces (block, ref, operand + 1,
+					      stmts, domstmt, in_call);
 	if (!op0)
 	  return NULL_TREE;
 	/* op1 should be a FIELD_DECL, which are represented by
 	   themselves.  */
-	op1 = TREE_OPERAND (genop, 1);
-	folded = fold_build3 (COMPONENT_REF, TREE_TYPE (genop), op0, op1,
-			      NULL_TREE);
-	return folded;
-      }
-      break;
-    case INDIRECT_REF:
-      {
-	tree op1 = TREE_OPERAND (genop, 0);
-	tree genop1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	if (!genop1)
-	  return NULL_TREE;
-
-	folded = fold_build1 (TREE_CODE (genop), TREE_TYPE (genop),
-			      genop1);
-	return folded;
-      }
-      break;
+	op1 = currop->op0;
+	return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1,
+			    NULL_TREE);
+      }
+      break;
+    case SSA_NAME:
+      {
+	pre_expr op0expr = get_or_alloc_expr_for (currop->op0);
+	genop = find_or_generate_expression (block, op0expr, stmts, domstmt);
+	return genop;
+      }
+    case STRING_CST:
+    case INTEGER_CST:
+    case COMPLEX_CST:
+    case VECTOR_CST:
+    case REAL_CST:
+    case CONSTRUCTOR:
     case VAR_DECL:
     case PARM_DECL:
+    case CONST_DECL:
     case RESULT_DECL:
-    case SSA_NAME:
-    case STRING_CST:
-      return genop;
+    case FUNCTION_DECL:
+      /* For ADDR_EXPR in a CALL_EXPR, op0 is actually the entire
+	 ADDR_EXPR, not just it's operand.  */
+    case ADDR_EXPR:
+      if (currop->opcode == ADDR_EXPR)
+	gcc_assert (currop->op0 != NULL);
+      return currop->op0;
+      
     default:
       gcc_unreachable ();
-    }
-
-  return NULL_TREE;
+    } 
 }
 
 /* Find a leader for an expression, or generate one using
@@ -2318,31 +2523,40 @@
    on failure.  */
 
 static tree
-find_or_generate_expression (basic_block block, tree expr, tree stmts,
+find_or_generate_expression (basic_block block, pre_expr expr, tree stmts,
 			     tree domstmt)
 {
-  tree genop = bitmap_find_leader (AVAIL_OUT (block), expr, domstmt);
-
+  pre_expr leader = bitmap_find_leader (AVAIL_OUT (block),
+					get_expr_value_id (expr), domstmt);
+  tree genop = NULL;
+  if (leader)
+    {
+      if (leader->kind == NAME)
+	genop = PRE_EXPR_NAME (leader);
+      else if (leader->kind == CONSTANT)
+	genop = PRE_EXPR_CONSTANT (leader);
+    }
+  
   /* If it's still NULL, it must be a complex expression, so generate
      it recursively.  */
   if (genop == NULL)
     {
-      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
+      bitmap_set_t exprset;
+      unsigned int lookfor = get_expr_value_id (expr);
       bool handled = false;
       bitmap_iterator bi;
       unsigned int i;
 
-      /* We will hit cases where we have SSA_NAME's in exprset before
-	 other operations, because we may have come up with the SCCVN
-	 value before getting to the RHS of the expression.  */
+      exprset = VEC_index (bitmap_set_t, value_expressions, lookfor);
       FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
 	{
-	  genop = expression_for_id (i);
-	  if (can_PRE_operation (genop))
+	  pre_expr temp = expression_for_id (i);
+	  if (temp->kind != NAME)
 	    {
 	      handled = true;
-	      genop = create_expression_by_pieces (block, genop, stmts,
-						   domstmt);
+	      genop = create_expression_by_pieces (block, temp, stmts,
+						   domstmt,
+						   get_expr_type (expr));
 	      break;
 	    }
 	}
@@ -2355,6 +2569,7 @@
 }
 
 #define NECESSARY(stmt)		stmt->base.asm_written_flag
+
 /* Create an expression in pieces, so that we can handle very complex
    expressions that may be ANTIC, but not necessary GIMPLE.
    BLOCK is the basic block the expression will be inserted into,
@@ -2374,109 +2589,77 @@
    can return NULL_TREE to signal failure.  */
 
 static tree
-create_expression_by_pieces (basic_block block, tree expr, tree stmts,
-			     tree domstmt)
+create_expression_by_pieces (basic_block block, pre_expr expr, tree stmts,
+			     tree domstmt,
+			     tree type)
 {
   tree temp, name;
   tree folded, forced_stmts, newexpr;
-  tree v;
+  unsigned int value_id;
   tree_stmt_iterator tsi;
-
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_vl_exp:
-      {
-	tree fn, sc;
-	tree genfn;
-	int i, nargs;
-	tree *buffer;
-
-	gcc_assert (TREE_CODE (expr) == CALL_EXPR);
-
-	fn = CALL_EXPR_FN (expr);
-	sc = CALL_EXPR_STATIC_CHAIN (expr);
-
-	genfn = find_or_generate_expression (block, fn, stmts, domstmt);
-	if (!genfn)
-	  return NULL_TREE;
-
-	nargs = call_expr_nargs (expr);
-	buffer = (tree*) alloca (nargs * sizeof (tree));
-
-	for (i = 0; i < nargs; i++)
-	  {
-	    tree arg = CALL_EXPR_ARG (expr, i);
-	    buffer[i] = find_or_generate_expression (block, arg, stmts,
-						     domstmt);
-	    if (!buffer[i])
-	      return NULL_TREE;
-	  }
-
-	folded = build_call_array (TREE_TYPE (expr), genfn, nargs, buffer);
-	if (sc)
-	  {
-	    CALL_EXPR_STATIC_CHAIN (folded) =
-	      find_or_generate_expression (block, sc, stmts, domstmt);
-	    if (!CALL_EXPR_STATIC_CHAIN (folded))
-	      return NULL_TREE;
-	  }
-	folded = fold (folded);
-	break;
-      }
-      break;
-    case tcc_reference:
-      {
-	if (TREE_CODE (expr) == COMPONENT_REF
-	    || TREE_CODE (expr) == ARRAY_REF)
-	  {
-	    folded = create_component_ref_by_pieces (block, expr, stmts,
-						     domstmt);
-	    if (!folded)
-	      return NULL_TREE;
-	  }
-	else
-	  {
-	    tree op1 = TREE_OPERAND (expr, 0);
-	    tree genop1 = find_or_generate_expression (block, op1, stmts,
-						       domstmt);
-	    if (!genop1)
-	      return NULL_TREE;
-
-	    folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
-				  genop1);
-	  }
-	break;
-      }
-
-    case tcc_binary:
-    case tcc_comparison:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	tree op2 = TREE_OPERAND (expr, 1);
-	tree genop1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	tree genop2 = find_or_generate_expression (block, op2, stmts, domstmt);
-	if (!genop1 || !genop2)
-	  return NULL_TREE;
-	folded = fold_build2 (TREE_CODE (expr), TREE_TYPE (expr),
-			      genop1, genop2);
-	break;
-      }
-
-    case tcc_unary:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	tree genop1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	if (!genop1)
-	  return NULL_TREE;
-	folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
-			      genop1);
-	break;
-      }
-
-    default:
-      gcc_unreachable ();
-    }
-
+  tree exprtype = type ? type : get_expr_type (expr);
+  pre_expr nameexpr;
+  
+  switch (expr->kind)
+    {
+      /* We may hit the NAME/CONSTANT case if we have to convert types
+	 that value numbering saw through.  */
+    case NAME:
+      folded = PRE_EXPR_NAME (expr);
+      break;
+    case CONSTANT:
+      folded = PRE_EXPR_CONSTANT (expr);
+      break;
+    case REFERENCE:
+      {
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	folded = create_component_ref_by_pieces (block, ref, 0, stmts,
+						 domstmt, false);
+      }
+      break;
+    case NARY:
+      {
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	switch (nary->length) 
+	  {
+	  case 2:
+	    {
+	      pre_expr op1 = get_or_alloc_expr_for (nary->op[0]);
+	      pre_expr op2 = get_or_alloc_expr_for (nary->op[1]);
+	      tree genop1 = find_or_generate_expression (block, op1,
+							 stmts, domstmt);
+	      tree genop2 = find_or_generate_expression (block, op2,
+							 stmts, domstmt);
+	      if (!genop1 || !genop2)
+		return NULL_TREE;
+	      /* Ensure op2 is a sizetype for POINTER_PLUS_EXPR.  It
+		 may be a constant with the wrong type.  */
+	      if (nary->opcode == POINTER_PLUS_EXPR)
+		genop2 = fold_convert (sizetype, genop2);
+	      folded = fold_build2 (nary->opcode, nary->type,
+				    genop1, genop2);
+	    }
+	    break;
+	  case 1:
+	    {
+	      pre_expr op1 = get_or_alloc_expr_for (nary->op[0]);
+	      tree genop1 = find_or_generate_expression (block, op1,
+							 stmts, domstmt);
+	      if (!genop1)
+		return NULL_TREE;
+	      folded = fold_build1 (nary->opcode, nary->type,
+				    genop1);
+	    }
+	    break;
+	  default:
+	    return NULL_TREE;
+	  }
+      }
+      break;
+    default:
+      return NULL_TREE;
+    }
+  folded = fold_convert (exprtype, folded);
   /* Force the generated expression to be a sequence of GIMPLE
      statements.
      We have to call unshare_expr because force_gimple_operand may
@@ -2493,14 +2676,18 @@
 	{
 	  tree stmt = tsi_stmt (tsi);
 	  tree forcedname = GIMPLE_STMT_OPERAND (stmt, 0);
-	  tree forcedexpr = GIMPLE_STMT_OPERAND (stmt, 1);
-	  tree val = vn_lookup_or_add (forcedexpr);
-
+	  pre_expr nameexpr;
+	  
 	  VEC_safe_push (tree, heap, inserted_exprs, stmt);
-	  VN_INFO_GET (forcedname)->valnum = forcedname;
-	  vn_add (forcedname, val);
-	  bitmap_value_replace_in_set (NEW_SETS (block), forcedname);
-	  bitmap_value_replace_in_set (AVAIL_OUT (block), forcedname);
+	  if (TREE_CODE (forcedname) == SSA_NAME)
+	    {
+	      VN_INFO_GET (forcedname)->valnum = forcedname;
+	      VN_INFO (forcedname)->value_id = get_next_value_id ();
+	      nameexpr = get_or_alloc_expr_for_name (forcedname);
+	      add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
+	      bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
+	      bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
+	    }
 	  mark_symbols_for_renaming (stmt);
 	}
       tsi = tsi_last (stmts);
@@ -2509,17 +2696,17 @@
 
   /* Build and insert the assignment of the end result to the temporary
      that we will return.  */
-  if (!pretemp || TREE_TYPE (expr) != TREE_TYPE (pretemp))
-    {
-      pretemp = create_tmp_var (TREE_TYPE (expr), "pretmp");
+  if (!pretemp || exprtype != TREE_TYPE (pretemp))
+    {
+      pretemp = create_tmp_var (exprtype, "pretmp");
       get_var_ann (pretemp);
     }
 
   temp = pretemp;
   add_referenced_var (temp);
 
-  if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE
-      || TREE_CODE (TREE_TYPE (expr)) == VECTOR_TYPE)
+  if (TREE_CODE (exprtype) == COMPLEX_TYPE
+      || TREE_CODE (exprtype) == VECTOR_TYPE)
     DECL_GIMPLE_REG_P (temp) = 1;
 
   newexpr = build_gimple_modify_stmt (temp, newexpr);
@@ -2534,18 +2721,19 @@
   /* All the symbols in NEWEXPR should be put into SSA form.  */
   mark_symbols_for_renaming (newexpr);
 
-  /* Add a value handle to the temporary.
+  /* Add a value number to the temporary.
      The value may already exist in either NEW_SETS, or AVAIL_OUT, because
      we are creating the expression by pieces, and this particular piece of
      the expression may have been represented.  There is no harm in replacing
      here.  */
-  v = get_value_handle (expr);
-  vn_add (name, v);
   VN_INFO_GET (name)->valnum = name;
-  get_or_alloc_expression_id (name);
+  value_id = get_expr_value_id (expr);
+  VN_INFO (name)->value_id = value_id;
+  nameexpr = get_or_alloc_expr_for_name (name);
+  add_to_value (value_id, nameexpr);
   if (!in_fre)
-    bitmap_value_replace_in_set (NEW_SETS (block), name);
-  bitmap_value_replace_in_set (AVAIL_OUT (block), name);
+    bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
+  bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
 
   pre_stats.insertions++;
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -2558,39 +2746,38 @@
   return name;
 }
 
+
 /* Insert the to-be-made-available values of expression EXPRNUM for each
    predecessor, stored in AVAIL, into the predecessors of BLOCK, and
-   merge the result with a phi node, given the same value handle as
+   merge the result with a phi node, given the same value number as
    NODE.  Return true if we have inserted new stuff.  */
 
 static bool
 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
-			    tree *avail)
-{
-  tree expr = expression_for_id (exprnum);
-  tree val = get_value_handle (expr);
+			    pre_expr *avail)
+{
+  pre_expr expr = expression_for_id (exprnum);
+  pre_expr newphi;
+  unsigned int val = get_expr_value_id (expr);
   edge pred;
   bool insertions = false;
   bool nophi = false;
   basic_block bprime;
-  tree eprime;
+  pre_expr eprime;
   edge_iterator ei;
-  tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
+  tree type = get_expr_type (expr);
   tree temp;
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
       fprintf (dump_file, "Found partial redundancy for expression ");
-      print_generic_expr (dump_file, expr, 0);
-      fprintf (dump_file, " (");
-      print_generic_expr (dump_file, val, 0);
-      fprintf (dump_file, ")");
-      fprintf (dump_file, "\n");
+      print_pre_expr (dump_file, expr);
+      fprintf (dump_file, " (%04d)\n", val);
     }
 
   /* Make sure we aren't creating an induction variable.  */
   if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2
-      && TREE_CODE_CLASS (TREE_CODE (expr)) != tcc_reference )
+      && expr->kind != REFERENCE)
     {
       bool firstinsideloop = false;
       bool secondinsideloop = false;
@@ -2616,15 +2803,110 @@
       bprime = pred->src;
       eprime = avail[bprime->index];
 
-      if (can_PRE_operation (eprime))
+      if (eprime->kind != NAME && eprime->kind != CONSTANT)
 	{
 	  builtexpr = create_expression_by_pieces (bprime,
 						   eprime,
-						   stmts, NULL_TREE);
+						   stmts, NULL_TREE,
+						   type);
 	  gcc_assert (!(pred->flags & EDGE_ABNORMAL));
 	  bsi_insert_on_edge (pred, stmts);
-	  avail[bprime->index] = builtexpr;
+	  avail[bprime->index] = get_or_alloc_expr_for_name (builtexpr);
 	  insertions = true;
+	}
+      else if (eprime->kind == CONSTANT) 
+	{
+	  /* Constants may not have the right type, fold_convert
+	     should give us back a constant with the right type.
+	  */
+	  tree constant = PRE_EXPR_CONSTANT (eprime);
+	  if (TREE_TYPE (constant) != type)
+	    {
+	      tree builtexpr = fold_convert (type, constant);
+	      if (is_gimple_min_invariant (builtexpr))
+		{
+		  PRE_EXPR_CONSTANT (eprime) = builtexpr;
+		}
+	      else
+		{
+		  tree forcedexpr = force_gimple_operand (builtexpr,
+							  &stmts, true,
+							  NULL);
+		  if (is_gimple_min_invariant (forcedexpr))
+		    {
+		      PRE_EXPR_CONSTANT (eprime) = forcedexpr;
+		    }
+		  else
+		    {
+		      if (forcedexpr != builtexpr)
+			{
+			  VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime);
+			  VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime);
+			}
+		      if (stmts)
+			{
+			  tree_stmt_iterator tsi;
+			  tsi = tsi_start (stmts);
+			  for (; !tsi_end_p (tsi); tsi_next (&tsi))
+			    {
+			      tree stmt = tsi_stmt (tsi);
+			      tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
+			      VEC_safe_push (tree, heap, inserted_exprs, stmt);
+			      NECESSARY (lhs) = 0;
+			    }
+			  bsi_insert_on_edge (pred, stmts);
+			}
+		      NECESSARY (forcedexpr) = 0;
+		      avail[bprime->index] = get_or_alloc_expr_for_name (forcedexpr);
+		    }
+		}
+	    }  
+	}
+      else if (eprime->kind == NAME)
+	{
+	  /* We may have to do a conversion because our value
+	     numbering can look through types in certain cases, but
+	     our IL requires all operands of a phi node have the same
+	     type.  */
+	  tree name = PRE_EXPR_NAME (eprime);
+	  if (TREE_TYPE (name) != type)
+	    {
+	      tree builtexpr;
+	      tree forcedexpr;
+	      /* When eliminating casts through unions, 
+		 we sometimes want to convert a real to an integer,
+		 which fold_convert will ICE on  */
+	      if (fold_convertible_p (type, name))
+		builtexpr = fold_convert (type, name);
+	      else
+		builtexpr = convert (type, name);
+	      
+	      forcedexpr = force_gimple_operand (builtexpr,
+						 &stmts, true,
+						 NULL);
+
+	      if (forcedexpr != name)
+		{
+		  VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum;
+		  VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id;
+		}
+	      
+	      if (stmts)
+		{
+		  tree_stmt_iterator tsi;
+		  tsi = tsi_start (stmts);
+		  for (; !tsi_end_p (tsi); tsi_next (&tsi))
+		    {
+		      tree stmt = tsi_stmt (tsi);
+		      tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
+		      VEC_safe_push (tree, heap, inserted_exprs, stmt);
+		      NECESSARY (lhs) = 0;
+		    }
+		  bsi_insert_on_edge (pred, stmts);
+		}
+	      NECESSARY (forcedexpr) = 0;
+	      avail[bprime->index] = get_or_alloc_expr_for_name (forcedexpr);
+	    }
 	}
     }
   /* If we didn't want a phi node, and we made insertions, we still have
@@ -2646,7 +2928,6 @@
   temp = prephitemp;
   add_referenced_var (temp);
 
-
   if (TREE_CODE (type) == COMPLEX_TYPE
       || TREE_CODE (type) == VECTOR_TYPE)
     DECL_GIMPLE_REG_P (temp) = 1;
@@ -2654,12 +2935,21 @@
 
   NECESSARY (temp) = 0;
   VN_INFO_GET (PHI_RESULT (temp))->valnum = PHI_RESULT (temp);
-
+  VN_INFO (PHI_RESULT (temp))->value_id = val;
   VEC_safe_push (tree, heap, inserted_exprs, temp);
   FOR_EACH_EDGE (pred, ei, block->preds)
-    add_phi_arg (temp, avail[pred->src->index], pred);
-
-  vn_add (PHI_RESULT (temp), val);
+    {
+      pre_expr ae = avail[pred->src->index];
+      gcc_assert (get_expr_type (ae) == type
+		  || useless_type_conversion_p (type, get_expr_type (ae)));
+      if (ae->kind == CONSTANT)
+	add_phi_arg (temp, PRE_EXPR_CONSTANT (ae), pred);
+      else
+	add_phi_arg (temp, PRE_EXPR_NAME (avail[pred->src->index]), pred);
+    }
+  
+  newphi = get_or_alloc_expr_for_name (PHI_RESULT (temp));
+  add_to_value (val, newphi);
 
   /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
      this insertion, since we test for the existence of this value in PHI_GEN
@@ -2675,12 +2965,11 @@
      AVAIL_OUT, and would have been skipped due to the full redundancy check.
   */
 
-  bitmap_insert_into_set (PHI_GEN (block),
-			  PHI_RESULT (temp));
+  bitmap_insert_into_set (PHI_GEN (block), newphi);
   bitmap_value_replace_in_set (AVAIL_OUT (block),
-			       PHI_RESULT (temp));
+			       newphi);
   bitmap_insert_into_set (NEW_SETS (block),
-			  PHI_RESULT (temp));
+			  newphi);
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
@@ -2716,26 +3005,26 @@
 do_regular_insertion (basic_block block, basic_block dom)
 {
   bool new_stuff = false;
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
-  tree expr;
-  int i;
-
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
-    {
-      if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
-	{
-	  tree *avail;
-	  tree val;
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
+  pre_expr expr;
+  int i;
+
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
+    {
+      if (expr->kind != NAME)
+	{
+	  pre_expr *avail;
+	  unsigned int val;
 	  bool by_some = false;
 	  bool cant_insert = false;
 	  bool all_same = true;
-	  tree first_s = NULL;
+	  pre_expr first_s = NULL;
 	  edge pred;
 	  basic_block bprime;
-	  tree eprime = NULL_TREE;
+	  pre_expr eprime = NULL;
 	  edge_iterator ei;
 
-	  val = get_value_handle (expr);
+	  val = get_expr_value_id (expr);
 	  if (bitmap_set_contains_value (PHI_GEN (block), val))
 	    continue;
 	  if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
@@ -2745,11 +3034,11 @@
 	      continue;
 	    }
 
-	  avail = XCNEWVEC (tree, last_basic_block);
+	  avail = XCNEWVEC (pre_expr, last_basic_block);
 	  FOR_EACH_EDGE (pred, ei, block->preds)
 	    {
-	      tree vprime;
-	      tree edoubleprime;
+	      unsigned int vprime;
+	      pre_expr edoubleprime;
 
 	      /* This can happen in the very weird case
 		 that our fake infinite loop edges have caused a
@@ -2779,8 +3068,7 @@
 		}
 
 	      eprime = fully_constant_expression (eprime);
-	      vprime = get_value_handle (eprime);
-	      gcc_assert (vprime);
+	      vprime = get_expr_value_id (eprime);
 	      edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
 						 vprime, NULL_TREE);
 	      if (edoubleprime == NULL)
@@ -2794,8 +3082,7 @@
 		  by_some = true;
 		  if (first_s == NULL)
 		    first_s = edoubleprime;
-		  else if (!operand_equal_p (first_s, edoubleprime,
-					     0))
+		  else if (!pre_expr_eq (first_s, edoubleprime))
 		    all_same = false;
 		}
 	    }
@@ -2813,19 +3100,26 @@
 	     an invariant, then the PHI has the same value on all
 	     edges.  Note this.  */
 	  else if (!cant_insert && all_same && eprime
-		   && is_gimple_min_invariant (eprime)
-		   && !is_gimple_min_invariant (val))
+		   && eprime->kind == CONSTANT
+		   && !value_id_constant_p (val))
 	    {
 	      unsigned int j;
 	      bitmap_iterator bi;
-
-	      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
+	      bitmap_set_t exprset = VEC_index (bitmap_set_t,
+						value_expressions, val);
+
+	      unsigned int new_val = get_expr_value_id (eprime);
 	      FOR_EACH_EXPR_ID_IN_SET (exprset, j, bi)
 		{
-		  tree expr = expression_for_id (j);
-		  if (TREE_CODE (expr) == SSA_NAME)
-		    {
-		      vn_add (expr, eprime);
+		  pre_expr expr = expression_for_id (j);
+
+		  if (expr->kind == NAME)
+		    {
+		      vn_ssa_aux_t info = VN_INFO (PRE_EXPR_NAME (expr));
+		      /* Just reset the value id and valnum so it is
+			 the same as the constant we have discovered.  */
+		      info->valnum = PRE_EXPR_CONSTANT (eprime);
+		      info->value_id = new_val;
 		      pre_stats.constified++;
 		    }
 		}
@@ -2834,7 +3128,7 @@
 	}
     }
 
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
   return new_stuff;
 }
 
@@ -2850,34 +3144,34 @@
 do_partial_partial_insertion (basic_block block, basic_block dom)
 {
   bool new_stuff = false;
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (PA_IN (block));
-  tree expr;
-  int i;
-
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
-    {
-      if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
-	{
-	  tree *avail;
-	  tree val;
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (PA_IN (block));
+  pre_expr expr;
+  int i;
+
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
+    {
+      if (expr->kind != NAME)
+	{
+	  pre_expr *avail;
+	  unsigned int val;
 	  bool by_all = true;
 	  bool cant_insert = false;
 	  edge pred;
 	  basic_block bprime;
-	  tree eprime = NULL_TREE;
+	  pre_expr eprime = NULL;
 	  edge_iterator ei;
 
-	  val = get_value_handle (expr);
+	  val = get_expr_value_id (expr);
 	  if (bitmap_set_contains_value (PHI_GEN (block), val))
 	    continue;
 	  if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
 	    continue;
 
-	  avail = XCNEWVEC (tree, last_basic_block);
+	  avail = XCNEWVEC (pre_expr, last_basic_block);
 	  FOR_EACH_EDGE (pred, ei, block->preds)
 	    {
-	      tree vprime;
-	      tree edoubleprime;
+	      unsigned int vprime;
+	      pre_expr edoubleprime;
 
 	      /* This can happen in the very weird case
 		 that our fake infinite loop edges have caused a
@@ -2908,8 +3202,7 @@
 		}
 
 	      eprime = fully_constant_expression (eprime);
-	      vprime = get_value_handle (eprime);
-	      gcc_assert (vprime);
+	      vprime = get_expr_value_id (eprime);
 	      edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
 						 vprime, NULL_TREE);
 	      if (edoubleprime == NULL)
@@ -2926,7 +3219,7 @@
 	     already existing along every predecessor, and
 	     it's defined by some predecessor, it is
 	     partially redundant.  */
-	  if (!cant_insert && by_all)
+	  if (!cant_insert && by_all && dbg_cnt (treepre_insert))
 	    {
 	      pre_stats.pa_insert++;
 	      if (insert_into_preds_of_block (block, get_expression_id (expr),
@@ -2937,7 +3230,7 @@
 	}
     }
 
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
   return new_stuff;
 }
 
@@ -2964,7 +3257,7 @@
 		 to replace it with.  */
 	      FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
 		{
-		  tree expr = expression_for_id (i);
+		  pre_expr expr = expression_for_id (i);
 		  bitmap_value_replace_in_set (NEW_SETS (block), expr);
 		  bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
 		}
@@ -3019,162 +3312,16 @@
 {
   if (!in_fre)
     {
+      pre_expr result;
       if (TREE_CODE (op) == SSA_NAME && ssa_undefined_value_p (op))
 	return;
-      bitmap_value_insert_into_set (EXP_GEN (block), op);
+      result = get_or_alloc_expr_for_name (op);
+      bitmap_value_insert_into_set (EXP_GEN (block), result);
       if (TREE_CODE (op) != SSA_NAME
 	  || TREE_CODE (SSA_NAME_DEF_STMT (op)) != PHI_NODE)
-	bitmap_value_insert_into_set (maximal_set, op);
-    }
-}
-
-
-/* Given an SSA variable VAR and an expression EXPR, compute the value
-   number for EXPR and create a value handle (VAL) for it.  If VAR and
-   EXPR are not the same, associate VAL with VAR.  Finally, add VAR to
-   S1 and its value handle to S2, and to the maximal set if
-   ADD_TO_MAXIMAL is true.
-
-   VUSES represent the virtual use operands associated with EXPR (if
-   any).  */
-
-static inline void
-add_to_sets (tree var, tree expr, VEC(tree, gc) *vuses, bitmap_set_t s1,
-	     bitmap_set_t s2)
-{
-  tree val;
-  val = vn_lookup_or_add_with_vuses (expr, vuses);
-
-  /* VAR and EXPR may be the same when processing statements for which
-     we are not computing value numbers (e.g., non-assignments, or
-     statements that make aliased stores).  In those cases, we are
-     only interested in making VAR available as its own value.  */
-  if (var != expr)
-    vn_add (var, val);
-
-  if (s1)
-    bitmap_insert_into_set (s1, var);
-
-  bitmap_value_insert_into_set (s2, var);
-}
-
-/* Find existing value expression that is the same as T,
-   and return it if it exists.  */
-
-static inline tree
-find_existing_value_expr (tree t, VEC (tree, gc) *vuses)
-{
-  bitmap_iterator bi;
-  unsigned int bii;
-  tree vh;
-  bitmap_set_t exprset;
-
-  if (REFERENCE_CLASS_P (t) || TREE_CODE (t) == CALL_EXPR || DECL_P (t))
-    vh = vn_lookup_with_vuses (t, vuses);
-  else
-    vh = vn_lookup (t);
-
-  if (!vh)
-    return NULL;
-  exprset = VALUE_HANDLE_EXPR_SET (vh);
-  FOR_EACH_EXPR_ID_IN_SET (exprset, bii, bi)
-    {
-      tree efi = expression_for_id (bii);
-      if (expressions_equal_p (t, efi))
-	return efi;
-    }
-  return NULL;
-}
-
-/* Given a unary or binary expression EXPR, create and return a new
-   expression with the same structure as EXPR but with its operands
-   replaced with the value handles of each of the operands of EXPR.
-
-   VUSES represent the virtual use operands associated with EXPR (if
-   any).  Insert EXPR's operands into the EXP_GEN set for BLOCK.
-
-   If CHECK_AVAIL is true, checks availability of each operand in
-   BLOCKs AVAIL_OUT set.  */
-
-static inline tree
-create_value_expr_from (tree expr, basic_block block, VEC (tree, gc) *vuses,
-			bool check_avail)
-{
-  int i;
-  enum tree_code code = TREE_CODE (expr);
-  tree vexpr;
-  alloc_pool pool = NULL;
-  tree efi;
-
-  gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
-	      || TREE_CODE_CLASS (code) == tcc_binary
-	      || TREE_CODE_CLASS (code) == tcc_comparison
-	      || TREE_CODE_CLASS (code) == tcc_reference
-	      || TREE_CODE_CLASS (code) == tcc_expression
-	      || TREE_CODE_CLASS (code) == tcc_vl_exp
-	      || TREE_CODE_CLASS (code) == tcc_exceptional
-	      || TREE_CODE_CLASS (code) == tcc_declaration);
-
-  if (TREE_CODE_CLASS (code) == tcc_unary)
-    pool = unary_node_pool;
-  else if (TREE_CODE_CLASS (code) == tcc_reference)
-    pool = reference_node_pool;
-  else if (TREE_CODE_CLASS (code) == tcc_binary)
-    pool = binary_node_pool;
-  else if (TREE_CODE_CLASS (code) == tcc_comparison)
-    pool = comparison_node_pool;
-  else
-    gcc_assert (code == CALL_EXPR);
-
-  if (code == CALL_EXPR)
-    vexpr = temp_copy_call_expr (expr);
-  else
-    {
-      vexpr = (tree) pool_alloc (pool);
-      memcpy (vexpr, expr, tree_size (expr));
-    }
-
-  for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
-    {
-      tree val = NULL_TREE;
-      tree op;
-
-      op = TREE_OPERAND (expr, i);
-      if (op == NULL_TREE)
-	continue;
-
-      /* Recursively value-numberize reference ops and tree lists.  */
-      if (REFERENCE_CLASS_P (op))
-	{
-	  tree tempop = create_value_expr_from (op, block, vuses, check_avail);
-	  op = tempop ? tempop : op;
-	  val = vn_lookup_or_add_with_vuses (op, vuses);
-	  set_expression_vuses (op, vuses);
-	}
-      else
-	{
-	  val = vn_lookup_or_add (op);
-	}
-      if (TREE_CODE (op) != TREE_LIST)
-	add_to_exp_gen (block, op);
-
-      if (TREE_CODE (val) == VALUE_HANDLE)
-	TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
-
-      TREE_OPERAND (vexpr, i) = val;
-
-      if (check_avail
-	  && TREE_CODE (val) == VALUE_HANDLE
-	  && !bitmap_set_contains_value (AVAIL_OUT (block), val))
-	return NULL_TREE;
-    }
-  efi = find_existing_value_expr (vexpr, vuses);
-  if (efi)
-    return efi;
-  get_or_alloc_expression_id (vexpr);
-  return vexpr;
-}
-
+	bitmap_value_insert_into_set (maximal_set, result);
+    }
+}
 
 /* For each real store operation of the form
    *a = <value> that we see, create a corresponding fake store of the
@@ -3287,57 +3434,7 @@
     }
 }
 
-/* Given an SSA_NAME, see if SCCVN has a value number for it, and if
-   so, return the value handle for this value number, creating it if
-   necessary.
-   Return NULL if SCCVN has no info for us.  */
-
-static tree
-get_sccvn_value (tree name)
-{
-  if (TREE_CODE (name) == SSA_NAME
-      && VN_INFO (name)->valnum != name
-      && VN_INFO (name)->valnum != VN_TOP)
-    {
-      tree val = VN_INFO (name)->valnum;
-      bool is_invariant = is_gimple_min_invariant (val);
-      tree valvh = !is_invariant ? get_value_handle (val) : NULL_TREE;
-
-      /* We may end up with situations where SCCVN has chosen a
-	 representative for the equivalence set that we have not
-	 visited yet.  In this case, just create the value handle for
-	 it.  */
-      if (!valvh && !is_invariant)
-	{
-	  /* We lookup with the LHS, so do not use vn_lookup_or_add_with_stmt
-	     here, as that will result in useless reference lookups.  */
-	  valvh = vn_lookup_or_add (val);
-	}
-
-      if (dump_file && (dump_flags & TDF_DETAILS))
-	{
-	  fprintf (dump_file, "SCCVN says ");
-	  print_generic_expr (dump_file, name, 0);
-	  fprintf (dump_file, " value numbers to ");
-	  if (valvh && !is_invariant)
-	    {
-	      print_generic_expr (dump_file, val, 0);
-	      fprintf (dump_file, " (");
-	      print_generic_expr (dump_file, valvh, 0);
-	      fprintf (dump_file, ")\n");
-	    }
-	  else
-	    print_generic_stmt (dump_file, val, 0);
-	}
-      if (valvh)
-	return valvh;
-      else
-	return val;
-    }
-  return NULL_TREE;
-}
-
-/* Create value handles for PHI in BLOCK.  */
+/* Create value ids for PHI in BLOCK.  */
 
 static void
 make_values_for_phi (tree phi, basic_block block)
@@ -3347,122 +3444,11 @@
      actual computations.  */
   if (is_gimple_reg (result))
     {
-      tree sccvnval = get_sccvn_value (result);
-      if (sccvnval)
-	{
-	  vn_add (result, sccvnval);
-	  bitmap_insert_into_set (PHI_GEN (block), result);
-	  bitmap_value_insert_into_set (AVAIL_OUT (block), result);
-	}
-      else
-	add_to_sets (result, result, NULL,
-		     PHI_GEN (block), AVAIL_OUT (block));
-    }
-}
-
-/* Create value handles for STMT in BLOCK.  Return true if we handled
-   the statement.  */
-
-static bool
-make_values_for_stmt (tree stmt, basic_block block)
-{
-
-  tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
-  tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
-  tree valvh = NULL_TREE;
-  tree lhsval;
-  VEC (tree, gc) *vuses = NULL;
-
-  valvh = get_sccvn_value (lhs);
-
-  if (valvh)
-    {
-      vn_add (lhs, valvh);
-      bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-      /* Shortcut for FRE. We have no need to create value expressions,
-	 just want to know what values are available where.  */
-      if (in_fre)
-	return true;
-
-    }
-  else if (in_fre)
-    {
-      /* For FRE, if SCCVN didn't find anything, we aren't going to
-	 either, so just make up a new value number if necessary and
-	 call it a day */
-      if (get_value_handle (lhs) == NULL)
-	vn_lookup_or_add (lhs);
-      bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-      return true;
-    }
-
-  lhsval = valvh ? valvh : get_value_handle (lhs);
-  vuses = copy_vuses_from_stmt (stmt);
-  STRIP_USELESS_TYPE_CONVERSION (rhs);
-  if (can_value_number_operation (rhs)
-      && (!lhsval || !is_gimple_min_invariant (lhsval))
-      && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
-    {
-      /* For value numberable operation, create a
-	 duplicate expression with the operands replaced
-	 with the value handles of the original RHS.  */
-      tree newt = create_value_expr_from (rhs, block, vuses, false);
-      if (newt)
-	{
-	  set_expression_vuses (newt, vuses);
-	  /* If we already have a value number for the LHS, reuse
-	     it rather than creating a new one.  */
-	  if (lhsval)
-	    {
-	      set_value_handle (newt, lhsval);
-	      if (!is_gimple_min_invariant (lhsval))
-		add_to_value (lhsval, newt);
-	    }
-	  else
-	    {
-	      tree val = vn_lookup_or_add_with_vuses (newt, vuses);
-	      vn_add (lhs, val);
-	    }
-
-	  add_to_exp_gen (block, newt);
-	}
-
-      bitmap_insert_into_set (TMP_GEN (block), lhs);
-      bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-      return true;
-    }
-  else if ((TREE_CODE (rhs) == SSA_NAME
-	    && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
-	   || is_gimple_min_invariant (rhs)
-	   || TREE_CODE (rhs) == ADDR_EXPR
-	   || DECL_P (rhs))
-    {
-
-      if (lhsval)
-	{
-	  set_expression_vuses (rhs, vuses);
-	  set_value_handle (rhs, lhsval);
-	  if (!is_gimple_min_invariant (lhsval))
-	    add_to_value (lhsval, rhs);
-	  bitmap_insert_into_set (TMP_GEN (block), lhs);
-	  bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-	}
-      else
-	{
-	  /* Compute a value number for the RHS of the statement
-	     and add its value to the AVAIL_OUT set for the block.
-	     Add the LHS to TMP_GEN.  */
-	  set_expression_vuses (rhs, vuses);
-	  add_to_sets (lhs, rhs, vuses, TMP_GEN (block),
-		       AVAIL_OUT (block));
-	}
-      /* None of the rest of these can be PRE'd.  */
-      if (TREE_CODE (rhs) == SSA_NAME && !ssa_undefined_value_p (rhs))
-	add_to_exp_gen (block, rhs);
-      return true;
-    }
-  return false;
-
+      pre_expr e = get_or_alloc_expr_for_name (result);
+      add_to_value (get_expr_value_id (e), e);
+      bitmap_insert_into_set (PHI_GEN (block), e);
+      bitmap_value_insert_into_set (AVAIL_OUT (block), e);
+    }
 }
 
 /* Compute the AVAIL set for all basic blocks.
@@ -3478,11 +3464,12 @@
 static void
 compute_avail (void)
 {
+
   basic_block block, son;
   basic_block *worklist;
   size_t sp = 0;
   tree param;
-
+  
   /* For arguments with default definitions, we pretend they are
      defined in the entry block.  */
   for (param = DECL_ARGUMENTS (current_function_decl);
@@ -3492,14 +3479,15 @@
       if (gimple_default_def (cfun, param) != NULL)
 	{
 	  tree def = gimple_default_def (cfun, param);
-
-	  vn_lookup_or_add (def);
+	  pre_expr e = get_or_alloc_expr_for_name (def);
+
+	  add_to_value (get_expr_value_id (e), e);	  
 	  if (!in_fre)
 	    {
-	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
-	      bitmap_value_insert_into_set (maximal_set, def);
-	    }
-	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
+	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), e);
+	      bitmap_value_insert_into_set (maximal_set, e);
+	    }
+	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), e);
 	}
     }
 
@@ -3510,14 +3498,15 @@
       if (gimple_default_def (cfun, param) != NULL)
 	{
 	  tree def = gimple_default_def (cfun, param);
-
-	  vn_lookup_or_add (def);
+	  pre_expr e = get_or_alloc_expr_for_name (def);
+
+	  add_to_value (get_expr_value_id (e), e);
 	  if (!in_fre)
 	    {
-	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
-	      bitmap_value_insert_into_set (maximal_set, def);
-	    }
-	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
+	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), e);
+	      bitmap_value_insert_into_set (maximal_set, e);
+	    }
+	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), e);
 	}
     }
 
@@ -3565,67 +3554,119 @@
 
 	  set_gimple_stmt_uid (stmt, stmt_uid++);
 
-	  /* For regular value numbering, we are only interested in
-	     assignments of the form X_i = EXPR, where EXPR represents
-	     an "interesting" computation, it has no volatile operands
-	     and X_i doesn't flow through an abnormal edge.  */
-	  if (TREE_CODE (stmt) == RETURN_EXPR
-	      && !ann->has_volatile_ops)
-	    {
-	      tree realstmt = stmt;
-	      tree lhs;
-	      tree rhs;
-
-	      stmt = TREE_OPERAND (stmt, 0);
-	      if (stmt && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
-		{
-		  lhs = GIMPLE_STMT_OPERAND (stmt, 0);
-		  rhs = GIMPLE_STMT_OPERAND (stmt, 1);
-		  if (TREE_CODE (lhs) == SSA_NAME
-		      && is_gimple_min_invariant (VN_INFO (lhs)->valnum))
-		    {
-		      if (dump_file && (dump_flags & TDF_DETAILS))
-			{
-			  fprintf (dump_file, "SCCVN says ");
-			  print_generic_expr (dump_file, lhs, 0);
-			  fprintf (dump_file, " value numbers to ");
-			  print_generic_stmt (dump_file, VN_INFO (lhs)->valnum,
-					      0);
-			}
-		      vn_add (lhs, VN_INFO (lhs)->valnum);
-		      continue;
-		    }
-
-		  if (TREE_CODE (rhs) == SSA_NAME)
-		    add_to_exp_gen (block, rhs);
-
-		  FOR_EACH_SSA_TREE_OPERAND (op, realstmt, iter, SSA_OP_DEF)
-		    add_to_sets (op, op, NULL, TMP_GEN (block),
-				 AVAIL_OUT (block));
-		}
+	  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
+	    {
+	      pre_expr e = get_or_alloc_expr_for_name (op);
+
+	      add_to_value (get_expr_value_id (e), e);
+	      if (!in_fre)
+		{
+		  bitmap_insert_into_set (TMP_GEN (block), e);
+		  bitmap_value_insert_into_set (maximal_set, e);
+		}
+	      bitmap_value_insert_into_set (AVAIL_OUT (block), e);
+	    }
+
+	  switch (TREE_CODE (stmt)) 
+	    {
+	    case RETURN_EXPR:
+	      if (!ann->has_volatile_ops)
+		FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
+		  add_to_exp_gen (block, op);
 	      continue;
-	    }
-
-	  else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
-		   && !ann->has_volatile_ops
-		   && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
-		   && !tree_could_throw_p (stmt))
-	    {
-	      if (make_values_for_stmt (stmt, block))
+	    case GIMPLE_MODIFY_STMT:
+	      {
+		tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
+		if (!ann->has_volatile_ops
+		    && !tree_could_throw_p (stmt))
+		  {
+		    pre_expr result = NULL;
+		    switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
+		      {
+		      case tcc_unary:
+			if (is_exception_related (rhs))
+			  continue;
+		      case tcc_binary:
+			{
+			  vn_nary_op_t nary;
+			  unsigned int i;
+			  
+			  vn_nary_op_lookup (rhs, &nary);
+
+			  if (!nary)
+			    continue;
+
+			  for (i = 0; i < nary->length; i++)
+			    if (TREE_CODE (nary->op[i]) == SSA_NAME)
+			      add_to_exp_gen (block, nary->op[i]);
+			  
+			  result = (pre_expr) pool_alloc (pre_expr_pool);
+			  result->kind = NARY;
+			  result->id = 0;
+			  PRE_EXPR_NARY (result) = nary;
+			}
+			break;
+		      case tcc_vl_exp:
+			if (!can_value_number_call (rhs))
+			  continue;
+
+		      case tcc_declaration:
+		      case tcc_reference:
+			{
+			  vn_reference_t ref;
+			  unsigned int i;
+			  vn_reference_op_t vro;
+
+			  vn_reference_lookup (rhs,
+					       shared_vuses_from_stmt (stmt),
+					       true, &ref);
+			  if (!ref)
+			    continue;
+
+			  for (i = 0; VEC_iterate (vn_reference_op_s,
+						   ref->operands, i,
+						   vro); i++)
+			    {
+			      if (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME)
+				add_to_exp_gen (block, vro->op0);
+			      if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
+				add_to_exp_gen (block, vro->op1);
+			    }
+			  result = (pre_expr) pool_alloc (pre_expr_pool);
+			  result->kind = REFERENCE;
+			  result->id = 0;
+			  PRE_EXPR_REFERENCE (result) = ref;
+			}
+			break;
+		      default:
+			{
+			  /* For any other statement that we don't
+			     recognize, simply add all referenced
+			     SSA_NAMEs to EXP_GEN.  */
+			  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
+			    add_to_exp_gen (block, op);
+			  continue;
+			}
+		      }
+		    get_or_alloc_expression_id (result);
+		    add_to_value (get_expr_value_id (result), result);
+		    if (!in_fre)
+		      {
+			bitmap_value_insert_into_set (EXP_GEN (block),
+						      result);
+			bitmap_value_insert_into_set (maximal_set, result);
+		      }  
+
+		  }
 		continue;
-	    }
-
-	  /* For any other statement that we don't recognize, simply
-	     make the names generated by the statement available in
-	     AVAIL_OUT and TMP_GEN.  */
-	  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
-	    add_to_sets (op, op, NULL, TMP_GEN (block), AVAIL_OUT (block));
-
-	  /* Also add all referenced SSA_NAMEs to EXP_GEN.  */
-	  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
-	    add_to_exp_gen (block, op);
-	}
-
+	      }
+	    default:
+	      break;
+	      
+	    }
+	  
+
+	}
       /* Put the dominator children of BLOCK on the worklist of blocks
 	 to compute available sets for.  */
       for (son = first_dom_son (CDI_DOMINATORS, block);
@@ -3648,18 +3689,21 @@
   basic_block bb = bb_for_stmt (stmt);
   block_stmt_iterator bsi;
   tree expr, stmts;
+  pre_expr e;
 
   /* First create a value expression from the expression we want
      to insert and associate it with the value handle for SSA_VN.  */
-  expr = create_value_expr_from (VN_INFO (ssa_vn)->expr, bb, NULL, true);
-  if (expr == NULL_TREE)
+  
+  /* TODO: Handle complex expressions.  */
+  e = get_or_alloc_expr_for (VN_INFO (ssa_vn)->expr);
+  if (e == NULL)
     return NULL_TREE;
-  set_value_handle (expr, get_value_handle (ssa_vn));
-
-  /* Then use create_expression_by_pieces to generate a valid
+
+/* Then use create_expression_by_pieces to generate a valid
      expression to insert at this point of the IL stream.  */
   stmts = alloc_stmt_list ();
-  expr = create_expression_by_pieces (bb, expr, stmts, stmt);
+  expr = create_expression_by_pieces (bb, e, stmts, stmt,
+				      NULL);
   if (expr == NULL_TREE)
     return NULL_TREE;
   bsi = bsi_for_stmt (stmt);
@@ -3695,24 +3739,56 @@
 	    {
 	      tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
 	      tree *rhs_p = &GIMPLE_STMT_OPERAND (stmt, 1);
-	      tree sprime;
-
-	      sprime = bitmap_find_leader (AVAIL_OUT (b),
-					   get_value_handle (lhs), NULL_TREE);
-
+	      tree sprime = NULL;
+	      pre_expr lhsexpr = get_or_alloc_expr_for_name (lhs);
+	      pre_expr sprimeexpr;
+
+	      sprimeexpr = bitmap_find_leader (AVAIL_OUT (b),
+					       get_expr_value_id (lhsexpr),
+					       NULL_TREE);
+
+	      if (sprimeexpr)
+		{
+		  if (sprimeexpr->kind == CONSTANT)
+		    sprime = PRE_EXPR_CONSTANT (sprimeexpr);
+		  else if (sprimeexpr->kind == NAME)
+		    sprime = PRE_EXPR_NAME (sprimeexpr);
+		  else
+		    gcc_unreachable ();
+		}
+	      /* If there is no existing leader but SCCVN knows this
+		 value is constant, use that constant.  */
+	      if (!sprime && is_gimple_min_invariant (VN_INFO (lhs)->valnum))
+		{
+		  sprime = VN_INFO (lhs)->valnum;
+
+		  if (dump_file && (dump_flags & TDF_DETAILS))
+		    {
+		      fprintf (dump_file, "Replaced ");
+		      print_generic_expr (dump_file, *rhs_p, 0);
+		      fprintf (dump_file, " with ");
+		      print_generic_expr (dump_file, sprime, 0);
+		      fprintf (dump_file, " in ");
+		      print_generic_stmt (dump_file, stmt, 0);
+		    }
+		  pre_stats.eliminations++;
+		  propagate_tree_value (rhs_p, sprime);
+		  update_stmt (stmt);
+		  continue;
+		}
+	      
 	      /* If there is no existing usable leader but SCCVN thinks
 		 it has an expression it wants to use as replacement,
 		 insert that.  */
-	      if (!sprime
-		  || sprime == lhs)
+	      if (!sprime || sprime == lhs)
 		{
 		  tree val = VN_INFO (lhs)->valnum;
 		  if (val != VN_TOP
+		      && TREE_CODE (val) == SSA_NAME
 		      && VN_INFO (val)->needs_insertion
 		      && can_PRE_operation (VN_INFO (val)->expr))
 		    sprime = do_SCCVN_insertion (stmt, val);
 		}
-
 	      if (sprime
 		  && sprime != lhs
 		  && (TREE_CODE (*rhs_p) != SSA_NAME
@@ -3921,6 +3997,15 @@
 static void
 init_pre (bool do_fre)
 {
+  basic_block bb;
+
+  next_expression_id = 1;
+  expressions = NULL;
+  VEC_safe_push (pre_expr, heap, expressions, NULL);
+  value_expressions = VEC_alloc (bitmap_set_t, heap, get_max_value_id () + 1);
+  VEC_safe_grow_cleared (bitmap_set_t, heap, value_expressions,
+			 get_max_value_id() + 1);
+  
   in_fre = do_fre;
 
   inserted_exprs = NULL;
@@ -3929,16 +4014,38 @@
   storetemp = NULL_TREE;
   prephitemp = NULL_TREE;
 
-  if (!do_fre)
-    loop_optimizer_init (LOOPS_NORMAL);
-
   connect_infinite_loops_to_exit ();
   memset (&pre_stats, 0, sizeof (pre_stats));
 
+
+  postorder = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
+  post_order_compute (postorder, false, false);
+
+  FOR_ALL_BB (bb)
+    bb->aux = XCNEWVEC (struct bb_bitmap_sets, 1);
+
   calculate_dominance_info (CDI_POST_DOMINATORS);
   calculate_dominance_info (CDI_DOMINATORS);
 
-  init_antic ();
+  bitmap_obstack_initialize (&grand_bitmap_obstack);
+  phi_translate_table = htab_create (5110, expr_pred_trans_hash,
+				     expr_pred_trans_eq, free);
+  expression_to_id = htab_create (num_ssa_names * 3,
+				  pre_expr_hash,
+				  pre_expr_eq, NULL);
+  seen_during_translate = BITMAP_ALLOC (&grand_bitmap_obstack);
+  bitmap_set_pool = create_alloc_pool ("Bitmap sets",
+				       sizeof (struct bitmap_set), 30);
+  pre_expr_pool = create_alloc_pool ("pre_expr nodes",
+				     sizeof (struct pre_expr_d), 30);
+  FOR_ALL_BB (bb)
+    {
+      EXP_GEN (bb) = bitmap_set_new ();
+      PHI_GEN (bb) = bitmap_set_new ();
+      TMP_GEN (bb) = bitmap_set_new ();
+      AVAIL_OUT (bb) = bitmap_set_new ();
+    }
+  maximal_set = in_fre ? NULL : bitmap_set_new ();
 
   need_eh_cleanup = BITMAP_ALLOC (NULL);
 }
@@ -3949,14 +4056,25 @@
 static void
 fini_pre (void)
 {
-  unsigned int i;
-
+  basic_block bb;
+  unsigned int i;
+
+  free (postorder);
+  VEC_free (bitmap_set_t, heap, value_expressions);
   VEC_free (tree, heap, inserted_exprs);
   VEC_free (tree, heap, need_creation);
+  bitmap_obstack_release (&grand_bitmap_obstack);
+  free_alloc_pool (bitmap_set_pool);
+  free_alloc_pool (pre_expr_pool);
   htab_delete (phi_translate_table);
+  htab_delete (expression_to_id);
   remove_fake_exit_edges ();
-
-  fini_antic ();
+  
+  FOR_ALL_BB (bb)
+    {
+      free (bb->aux);
+      bb->aux = NULL;
+    }
 
   free_dominance_info (CDI_POST_DOMINATORS);
 
@@ -3977,9 +4095,6 @@
       if (!name)
 	continue;
 
-      if (SSA_NAME_VALUE (name)
-	  && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
-	SSA_NAME_VALUE (name) = NULL;
     }
   if (current_loops != NULL)
     loop_optimizer_finalize ();
@@ -3994,12 +4109,14 @@
   unsigned int todo = 0;
 
   do_partial_partial = optimize > 2;
-  init_pre (do_fre);
-
+
+  /* This has to happen before SCCVN runs because
+     loop_optimizer_init may create new phis, etc.  */
   if (!do_fre)
+    loop_optimizer_init (LOOPS_NORMAL);
+  if (0 && !do_fre)
     insert_fake_stores ();
 
-  /* Collect and value number expressions computed in each basic block.  */
   if (!run_scc_vn (do_fre))
     {
       if (!do_fre)
@@ -4007,7 +4124,10 @@
       fini_pre ();
       return 0;
     }
-  switch_to_PRE_table ();
+  init_pre (do_fre);
+
+
+  /* Collect and value number expressions computed in each basic block.  */
   compute_avail ();
 
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -4032,16 +4152,6 @@
   if (!do_fre && n_basic_blocks < 4000)
     {
       compute_antic ();
-  if (dump_file && (dump_flags & TDF_DETAILS))
-    {
-      basic_block bb;
-
-      FOR_ALL_BB (bb)
-	{
-	  print_bitmap_set (dump_file, ANTIC_IN (bb), "antic_in", bb->index);
-	}
-    }
-
       insert ();
     }
 
@@ -4060,7 +4170,8 @@
   if (!do_fre)
     {
       remove_dead_inserted_code ();
-      realify_fake_stores ();
+      if (0)
+	realify_fake_stores ();
     }
 
   fini_pre ();
diff -r 2325330ce9ed gcc/tree-ssa-sccvn.c
--- a/gcc/tree-ssa-sccvn.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-ssa-sccvn.c	Sun Jul 06 00:27:17 2008 -0400
@@ -115,72 +115,9 @@
   alloc_pool references_pool;
 } *vn_tables_t;
 
-/* Nary operations in the hashtable consist of length operands, an
-   opcode, and a type.  Result is the value number of the operation,
-   and hashcode is stored to avoid having to calculate it
-   repeatedly.  */
+static htab_t constant_to_value_id;
+static bitmap constant_value_ids;
 
-typedef struct vn_nary_op_s
-{
-  ENUM_BITFIELD(tree_code) opcode : 16;
-  unsigned length : 16;
-  hashval_t hashcode;
-  tree result;
-  tree type;
-  tree op[4];
-} *vn_nary_op_t;
-typedef const struct vn_nary_op_s *const_vn_nary_op_t;
-
-/* Phi nodes in the hashtable consist of their non-VN_TOP phi
-   arguments, and the basic block the phi is in. Result is the value
-   number of the operation, and hashcode is stored to avoid having to
-   calculate it repeatedly.  Phi nodes not in the same block are never
-   considered equivalent.  */
-
-typedef struct vn_phi_s
-{
-  VEC (tree, heap) *phiargs;
-  basic_block block;
-  hashval_t hashcode;
-  tree result;
-} *vn_phi_t;
-typedef const struct vn_phi_s *const_vn_phi_t;
-
-/* Reference operands only exist in reference operations structures.
-   They consist of an opcode, type, and some number of operands.  For
-   a given opcode, some, all, or none of the operands may be used.
-   The operands are there to store the information that makes up the
-   portion of the addressing calculation that opcode performs.  */
-
-typedef struct vn_reference_op_struct
-{
-  enum tree_code opcode;
-  tree type;
-  tree op0;
-  tree op1;
-} vn_reference_op_s;
-typedef vn_reference_op_s *vn_reference_op_t;
-typedef const vn_reference_op_s *const_vn_reference_op_t;
-
-DEF_VEC_O(vn_reference_op_s);
-DEF_VEC_ALLOC_O(vn_reference_op_s, heap);
-
-/* A reference operation in the hashtable is representation as a
-   collection of vuses, representing the memory state at the time of
-   the operation, and a collection of operands that make up the
-   addressing calculation.  If two vn_reference_t's have the same set
-   of operands, they access the same memory location. We also store
-   the resulting value number, and the hashcode.  The vuses are
-   always stored in order sorted by ssa name version.  */
-
-typedef struct vn_reference_s
-{
-  VEC (tree, gc) *vuses;
-  VEC (vn_reference_op_s, heap) *operands;
-  hashval_t hashcode;
-  tree result;
-} *vn_reference_t;
-typedef const struct vn_reference_s *const_vn_reference_t;
 
 /* Valid hashtables storing information we have proven to be
    correct.  */
@@ -215,6 +152,10 @@
 
 tree VN_TOP;
 
+/* Unique counter for our value ids.  */
+
+static unsigned int next_value_id;
+
 /* Next DFS number and the stack for strongly connected component
    detection. */
 
@@ -239,8 +180,10 @@
 vn_ssa_aux_t
 VN_INFO (tree name)
 {
-  return VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
-		    SSA_NAME_VERSION (name));
+  vn_ssa_aux_t res = VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
+				SSA_NAME_VERSION (name));
+  gcc_assert (res);
+  return res;
 }
 
 /* Set the value numbering info for a given SSA name to a given
@@ -288,6 +231,58 @@
 {
   vn_reference_t vr = (vn_reference_t) vp;
   VEC_free (vn_reference_op_s, heap, vr->operands);
+}
+
+/* Hash table equality function for vn_constant_t.  */
+
+static int
+vn_constant_eq (const void *p1, const void *p2)
+{
+  const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
+  const struct vn_constant_s *vc2 = (const struct vn_constant_s *) p2;
+
+  return expressions_equal_p (vc1->constant, vc2->constant);
+}
+
+/* Hash table hash function for vn_constant_t.  */
+   
+static hashval_t
+vn_constant_hash (const void *p1)
+{
+  const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
+  return vc1->hashcode;
+}
+
+/* Lookup a value id for CONSTANT, and if it does not exist, create a
+   new one and return it.  If it does exist, return it.  */
+
+unsigned int
+get_or_alloc_constant_value_id (tree constant)
+{
+  void **slot;
+  vn_constant_t vc = XNEW (struct vn_constant_s);
+  
+  vc->hashcode = iterative_hash_expr (constant, 0);
+  vc->constant = constant;
+  slot = htab_find_slot_with_hash (constant_to_value_id, vc,
+				   vc->hashcode, INSERT);  
+  if (*slot)
+    {
+      free (vc);
+      return ((vn_constant_t)*slot)->value_id;
+    }
+  vc->value_id = get_next_value_id ();
+  *slot = vc;
+  bitmap_set_bit (constant_value_ids, vc->value_id);
+  return vc->value_id;
+}
+
+/* Return true if V is a value id for a constant.  */
+
+bool
+value_id_constant_p (unsigned int v)
+{
+  return bitmap_bit_p (constant_value_ids, v);  
 }
 
 /* Compare two reference operands P1 and P2 for equality.  Return true if
@@ -324,7 +319,7 @@
 
 /* Compute a hash for the reference operation VR1 and return it.  */
 
-static inline hashval_t
+hashval_t
 vn_reference_compute_hash (const vn_reference_t vr1)
 {
   hashval_t result = 0;
@@ -343,7 +338,7 @@
 /* Return true if reference operations P1 and P2 are equivalent.  This
    means they have the same set of operands and vuses.  */
 
-static int
+int
 vn_reference_eq (const void *p1, const void *p2)
 {
   tree v;
@@ -484,10 +479,18 @@
       temp.type = TREE_TYPE (ref);
       temp.opcode = CALL_EXPR;
       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
+      
+      /* We make no attempt to simplify the called function because
+      the typical &FUNCTION_DECL form is also used in function pointer
+      cases that become constant.  If we simplify the original to
+      FUNCTION_DECL but not the function pointer case (which can
+      happen because we have no fold functions that operate on
+      vn_reference_t), we will claim they are not equivalent.
 
-      callfn = get_callee_fndecl (ref);
-      if (!callfn)
-	callfn = CALL_EXPR_FN (ref);
+      An example of this behavior can be see if CALL_EXPR_FN below is
+      replaced with get_callee_fndecl and gcc.dg/tree-ssa/ssa-pre-13.c
+      is compiled.  */
+      callfn = CALL_EXPR_FN (ref);
       temp.type = TREE_TYPE (callfn);
       temp.opcode = TREE_CODE (callfn);
       temp.op0 = callfn;
@@ -554,6 +557,7 @@
 	     a matching type is not necessary and a mismatching type
 	     is always a spurious difference.  */
 	  temp.type = NULL_TREE;
+#if FIXME
 	  /* If this is a reference to a union member, record the union
 	     member size as operand.  Do so only if we are doing
 	     expression insertion (during FRE), as PRE currently gets
@@ -564,6 +568,7 @@
 	      && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1))))
 	    temp.op0 = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)));
 	  else
+#endif
 	    /* Record field as operand.  */
 	    temp.op0 = TREE_OPERAND (ref, 1);
 	  break;
@@ -579,7 +584,6 @@
 	case VECTOR_CST:
 	case REAL_CST:
 	case CONSTRUCTOR:
-	case VALUE_HANDLE:
 	case VAR_DECL:
 	case PARM_DECL:
 	case CONST_DECL:
@@ -653,7 +657,14 @@
     {
       if (vro->opcode == SSA_NAME
 	  || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
-	vro->op0 = SSA_VAL (vro->op0);
+	{
+	  vro->op0 = SSA_VAL (vro->op0);
+	  /* If it transforms from an SSA_NAME to a constant, update
+	     the opcode.  */
+	  if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
+	    vro->opcode = TREE_CODE (vro->op0);
+	}
+      
     }
 
   return orig;
@@ -733,10 +744,11 @@
 
 /* Lookup a SCCVN reference operation VR in the current hash table.
    Returns the resulting value number if it exists in the hash table,
-   NULL_TREE otherwise.  */
+   NULL_TREE otherwise.  VNRESULT will be filled in with the actual
+   vn_reference_t stored in the hashtable if something is found.  */
 
 static tree
-vn_reference_lookup_1 (vn_reference_t vr)
+vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
 {
   void **slot;
   hashval_t hash;
@@ -748,25 +760,58 @@
     slot = htab_find_slot_with_hash (valid_info->references, vr,
 				     hash, NO_INSERT);
   if (slot)
-    return ((vn_reference_t)*slot)->result;
-
+    {
+      if (vnresult)
+	*vnresult = (vn_reference_t)*slot;
+      return ((vn_reference_t)*slot)->result;
+    }
+  
   return NULL_TREE;
 }
 
-/* Lookup OP in the current hash table, and return the resulting
-   value number if it exists in the hash table.  Return NULL_TREE if
-   it does not exist in the hash table. */
+
+/* Lookup a reference operation by it's parts, in the current hash table.
+   Returns the resulting value number if it exists in the hash table,
+   NULL_TREE otherwise.  VNRESULT will be filled in with the actual
+   vn_reference_t stored in the hashtable if something is found.  */
 
 tree
-vn_reference_lookup (tree op, VEC (tree, gc) *vuses, bool maywalk)
+vn_reference_lookup_pieces (VEC (tree, gc) *vuses,
+			    VEC (vn_reference_op_s, heap) *operands,
+			    vn_reference_t *vnresult) 
+{
+  struct vn_reference_s vr1;
+  tree result;
+  if (vnresult)
+    *vnresult = NULL;
+  
+  vr1.vuses = valueize_vuses (vuses);
+  vr1.operands = valueize_refs (operands);
+  vr1.hashcode = vn_reference_compute_hash (&vr1);
+  result = vn_reference_lookup_1 (&vr1, vnresult);
+
+  return result;
+}
+
+/* Lookup OP in the current hash table, and return the resulting value
+   number if it exists in the hash table.  Return NULL_TREE if it does
+   not exist in the hash table or if the result field of the structure
+   was NULL..  VNRESULT will be filled in with the vn_reference_t
+   stored in the hashtable if one exists.  */
+
+tree
+vn_reference_lookup (tree op, VEC (tree, gc) *vuses, bool maywalk,
+		     vn_reference_t *vnresult)
 {
   struct vn_reference_s vr1;
   tree result, def_stmt;
+  if (vnresult)
+    *vnresult = NULL;
 
   vr1.vuses = valueize_vuses (vuses);
   vr1.operands = valueize_refs (shared_reference_ops_from_ref (op));
   vr1.hashcode = vn_reference_compute_hash (&vr1);
-  result = vn_reference_lookup_1 (&vr1);
+  result = vn_reference_lookup_1 (&vr1, vnresult);
 
   /* If there is a single defining statement for all virtual uses, we can
      use that, following virtual use-def chains.  */
@@ -786,23 +831,27 @@
       vdefs_to_vec (def_stmt, &vuses);
       vr1.vuses = valueize_vuses (vuses);
       vr1.hashcode = vn_reference_compute_hash (&vr1);
-      result = vn_reference_lookup_1 (&vr1);
+      result = vn_reference_lookup_1 (&vr1, vnresult);
     }
 
   return result;
 }
 
+
 /* Insert OP into the current hash table with a value number of
-   RESULT.  */
+   RESULT, and return the resulting reference structure we created.  */
 
-void
+vn_reference_t
 vn_reference_insert (tree op, tree result, VEC (tree, gc) *vuses)
 {
   void **slot;
   vn_reference_t vr1;
 
   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
-
+  if (TREE_CODE (result) == SSA_NAME)
+    vr1->value_id = VN_INFO (result)->value_id;
+  else
+    vr1->value_id = get_or_alloc_constant_value_id (result);
   vr1->vuses = valueize_vuses (vuses);
   vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
   vr1->hashcode = vn_reference_compute_hash (vr1);
@@ -824,11 +873,48 @@
     free_reference (*slot);
 
   *slot = vr1;
+  return vr1;
+}
+
+/* Insert a reference by it's pieces into the current hash table with
+   a value number of RESULT.  Return the resulting reference
+   structure we created.  */
+
+vn_reference_t
+vn_reference_insert_pieces (VEC (tree, gc) *vuses,
+			    VEC (vn_reference_op_s, heap) *operands,
+			    tree result, unsigned int value_id)
+
+{
+  void **slot;
+  vn_reference_t vr1;
+
+  vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
+  vr1->value_id =  value_id;
+  vr1->vuses = valueize_vuses (vuses);
+  vr1->operands = valueize_refs (operands);
+  vr1->hashcode = vn_reference_compute_hash (vr1);
+  if (result && TREE_CODE (result) == SSA_NAME)
+    result = SSA_VAL (result);
+  vr1->result = result;
+
+  slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
+				   INSERT);
+  
+  /* At this point we should have all the things inserted that we have
+  seen before, and we should never try inserting something that
+  already exists.  */
+  gcc_assert (!*slot);
+  if (*slot)
+    free_reference (*slot);
+
+  *slot = vr1;
+  return vr1;
 }
 
 /* Compute and return the hash value for nary operation VBO1.  */
 
-static inline hashval_t
+inline hashval_t
 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
 {
   hashval_t hash = 0;
@@ -865,7 +951,7 @@
 /* Compare nary operations P1 and P2 and return true if they are
    equivalent.  */
 
-static int
+int
 vn_nary_op_eq (const void *p1, const void *p2)
 {
   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
@@ -883,17 +969,56 @@
   return true;
 }
 
-/* Lookup OP in the current hash table, and return the resulting
-   value number if it exists in the hash table.  Return NULL_TREE if
-   it does not exist in the hash table. */
+/* Lookup a n-ary operation by its pieces and return the resulting value
+   number if it exists in the hash table.  Return NULL_TREE if it does
+   not exist in the hash table or if the result field of the operation
+   is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
+   if it exists.  */
 
 tree
-vn_nary_op_lookup (tree op)
+vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
+			  tree type, tree op0, tree op1, tree op2,
+			  tree op3, vn_nary_op_t *vnresult) 
+{
+  void **slot;
+  struct vn_nary_op_s vno1;
+  if (vnresult)
+    *vnresult = NULL;
+  vno1.opcode = code;
+  vno1.length = length;
+  vno1.type = type;
+  vno1.op[0] = op0;
+  vno1.op[1] = op1;
+  vno1.op[2] = op2;
+  vno1.op[3] = op3;
+  vno1.hashcode = vn_nary_op_compute_hash (&vno1);
+  slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
+				   NO_INSERT);
+  if (!slot && current_info == optimistic_info)
+    slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
+				     NO_INSERT);
+  if (!slot)
+    return NULL_TREE;
+  if (vnresult)
+    *vnresult = (vn_nary_op_t)*slot;
+  return ((vn_nary_op_t)*slot)->result;
+}
+
+/* Lookup OP in the current hash table, and return the resulting value
+   number if it exists in the hash table.  Return NULL_TREE if it does
+   not exist in the hash table or if the result field of the operation
+   is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
+   if it exists.  */
+
+tree
+vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
 {
   void **slot;
   struct vn_nary_op_s vno1;
   unsigned i;
 
+  if (vnresult)
+    *vnresult = NULL;
   vno1.opcode = TREE_CODE (op);
   vno1.length = TREE_CODE_LENGTH (TREE_CODE (op));
   vno1.type = TREE_TYPE (op);
@@ -907,13 +1032,56 @@
 				     NO_INSERT);
   if (!slot)
     return NULL_TREE;
+  if (vnresult)
+    *vnresult = (vn_nary_op_t)*slot;
   return ((vn_nary_op_t)*slot)->result;
 }
 
+/* Insert a n-ary operation into the current hash table using it's
+   pieces.  Return the vn_nary_op_t structure we created and put in
+   the hashtable.  */
+
+vn_nary_op_t
+vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
+			  tree type, tree op0,
+			  tree op1, tree op2, tree op3,
+			  tree result,
+			  unsigned int value_id) 
+{
+  void **slot;
+  vn_nary_op_t vno1;
+
+  vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
+				       (sizeof (struct vn_nary_op_s)
+					- sizeof (tree) * (4 - length)));
+  vno1->value_id = value_id;
+  vno1->opcode = code;
+  vno1->length = length;
+  vno1->type = type;
+  if (length >= 1)
+    vno1->op[0] = op0;
+  if (length >= 2)
+    vno1->op[1] = op1;
+  if (length >= 3)
+    vno1->op[2] = op2;
+  if (length >= 4)
+    vno1->op[3] = op3;
+  vno1->result = result;
+  vno1->hashcode = vn_nary_op_compute_hash (vno1);
+  slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
+				   INSERT);
+  gcc_assert (!*slot);
+
+  *slot = vno1;
+  return vno1;
+  
+}
+
 /* Insert OP into the current hash table with a value number of
-   RESULT.  */
+   RESULT.  Return the vn_nary_op_t structure we created and put in
+   the hashtable.  */
 
-void
+vn_nary_op_t
 vn_nary_op_insert (tree op, tree result)
 {
   unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
@@ -924,6 +1092,7 @@
   vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
 			(sizeof (struct vn_nary_op_s)
 			 - sizeof (tree) * (4 - length)));
+  vno1->value_id = VN_INFO (result)->value_id;
   vno1->opcode = TREE_CODE (op);
   vno1->length = length;
   vno1->type = TREE_TYPE (op);
@@ -936,6 +1105,7 @@
   gcc_assert (!*slot);
 
   *slot = vno1;
+  return vno1;
 }
 
 /* Compute a hashcode for PHI operation VP1 and return it.  */
@@ -1034,7 +1204,7 @@
 /* Insert PHI into the current hash table with a value number of
    RESULT.  */
 
-static void
+static vn_phi_t
 vn_phi_insert (tree phi, tree result)
 {
   void **slot;
@@ -1049,6 +1219,7 @@
       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
       VEC_safe_push (tree, heap, args, def);
     }
+  vp1->value_id = VN_INFO (result)->value_id;
   vp1->phiargs = args;
   vp1->block = bb_for_stmt (phi);
   vp1->result = result;
@@ -1060,6 +1231,7 @@
   /* Because we iterate over phi operations more than once, it's
      possible the slot might already exist here, hence no assert.*/
   *slot = vp1;
+  return vp1;
 }
 
 
@@ -1168,7 +1340,7 @@
 visit_unary_op (tree lhs, tree op)
 {
   bool changed = false;
-  tree result = vn_nary_op_lookup (op);
+  tree result = vn_nary_op_lookup (op, NULL);
 
   if (result)
     {
@@ -1190,7 +1362,7 @@
 visit_binary_op (tree lhs, tree op)
 {
   bool changed = false;
-  tree result = vn_nary_op_lookup (op);
+  tree result = vn_nary_op_lookup (op, NULL);
 
   if (result)
     {
@@ -1212,7 +1384,8 @@
 visit_reference_op_load (tree lhs, tree op, tree stmt)
 {
   bool changed = false;
-  tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt), true);
+  tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt), true,
+				     NULL);
 
   /* We handle type-punning through unions by value-numbering based
      on offset and size of the access.  Be prepared to handle a
@@ -1236,7 +1409,7 @@
       result = val;
       if (!is_gimple_min_invariant (val)
 	  && TREE_CODE (val) != SSA_NAME)
-	result = vn_nary_op_lookup (val);
+	result = vn_nary_op_lookup (val, NULL);
       /* If the expression is not yet available, value-number lhs to
 	 a new SSA_NAME we create.  */
       if (!result && may_insert)
@@ -1319,7 +1492,8 @@
      Otherwise, the vdefs for the store are used when inserting into
      the table, since the store generates a new memory state.  */
 
-  result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt), false);
+  result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt), false,
+				NULL);
 
   if (result)
     {
@@ -2146,12 +2320,18 @@
 
   calculate_dominance_info (CDI_DOMINATORS);
   sccstack = NULL;
+  constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
+				  free);
+  
+  constant_value_ids = BITMAP_ALLOC (NULL);
+  
   next_dfs_num = 1;
-
+  next_value_id = 1;
+  
   vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
   /* VEC_alloc doesn't actually grow it to the right size, it just
      preallocates the space to do so.  */
-  VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
+  VEC_safe_grow_cleared (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
   gcc_obstack_init (&vn_ssa_aux_obstack);
 
   shared_lookup_phiargs = NULL;
@@ -2180,6 +2360,7 @@
 	{
 	  VN_INFO_GET (name)->valnum = VN_TOP;
 	  VN_INFO (name)->expr = name;
+	  VN_INFO (name)->value_id = 0;
 	}
     }
 
@@ -2206,6 +2387,8 @@
 {
   size_t i;
 
+  htab_delete (constant_to_value_id);
+  BITMAP_FREE (constant_value_ids);
   VEC_free (tree, heap, shared_lookup_phiargs);
   VEC_free (tree, gc, shared_lookup_vops);
   VEC_free (vn_reference_op_s, heap, shared_lookup_references);
@@ -2215,8 +2398,7 @@
     {
       tree name = ssa_name (i);
       if (name
-	  && SSA_NAME_VALUE (name)
-	  && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
+	  && SSA_NAME_VALUE (name))
 	SSA_NAME_VALUE (name) = NULL;
       if (name
 	  && VN_INFO (name)->needs_insertion)
@@ -2237,6 +2419,91 @@
     }
 }
 
+/* Set the value ids in the valid/optimistic hash tables.  */
+
+static void
+set_hashtable_value_ids (void)
+{
+  htab_iterator hi;
+  vn_nary_op_t vno;
+  vn_reference_t vr;
+  vn_phi_t vp;
+  
+  /* Now set the value ids of the things we had put in the hash
+     table.  */
+
+  FOR_EACH_HTAB_ELEMENT (valid_info->nary,
+			 vno, vn_nary_op_t, hi) 
+    {
+      if (vno->result)
+	{
+	  if (TREE_CODE (vno->result) == SSA_NAME)
+	    vno->value_id = VN_INFO (vno->result)->value_id;
+	  else if (is_gimple_min_invariant (vno->result))
+	    vno->value_id = get_or_alloc_constant_value_id (vno->result);
+	}
+    }
+
+  FOR_EACH_HTAB_ELEMENT (optimistic_info->nary,
+			 vno, vn_nary_op_t, hi) 
+    {
+      if (vno->result)
+	{
+	  if (TREE_CODE (vno->result) == SSA_NAME)
+	    vno->value_id = VN_INFO (vno->result)->value_id;
+	  else if (is_gimple_min_invariant (vno->result))
+	    vno->value_id = get_or_alloc_constant_value_id (vno->result);
+	}
+    }
+
+  FOR_EACH_HTAB_ELEMENT (valid_info->phis,
+			 vp, vn_phi_t, hi) 
+    {
+      if (vp->result)
+	{
+	  if (TREE_CODE (vp->result) == SSA_NAME)
+	    vp->value_id = VN_INFO (vp->result)->value_id;
+	  else if (is_gimple_min_invariant (vp->result))
+	    vp->value_id = get_or_alloc_constant_value_id (vp->result);
+	}
+    }
+  FOR_EACH_HTAB_ELEMENT (optimistic_info->phis,
+			 vp, vn_phi_t, hi) 
+    {
+      if (vp->result)
+	{
+	  if (TREE_CODE (vp->result) == SSA_NAME)
+	    vp->value_id = VN_INFO (vp->result)->value_id;
+	  else if (is_gimple_min_invariant (vp->result))
+	    vp->value_id = get_or_alloc_constant_value_id (vp->result);
+	}
+    }
+
+
+  FOR_EACH_HTAB_ELEMENT (valid_info->references,
+			 vr, vn_reference_t, hi) 
+    {
+      if (vr->result)
+	{
+	  if (TREE_CODE (vr->result) == SSA_NAME)
+	    vr->value_id = VN_INFO (vr->result)->value_id;
+	  else if (is_gimple_min_invariant (vr->result))
+	    vr->value_id = get_or_alloc_constant_value_id (vr->result);
+	}
+    }
+  FOR_EACH_HTAB_ELEMENT (optimistic_info->references,
+			 vr, vn_reference_t, hi) 
+    {
+      if (vr->result)
+	{
+	  if (TREE_CODE (vr->result) == SSA_NAME)
+	    vr->value_id = VN_INFO (vr->result)->value_id;
+	  else if (is_gimple_min_invariant (vr->result))
+	    vr->value_id = get_or_alloc_constant_value_id (vr->result);
+	}
+    }
+}
+
 /* Do SCCVN.  Returns true if it finished, false if we bailed out
    due to resource constraints.  */
 
@@ -2245,7 +2512,8 @@
 {
   size_t i;
   tree param;
-
+  bool changed = true;
+  
   may_insert = may_insert_arg;
 
   init_scc_vn ();
@@ -2276,6 +2544,45 @@
 	  }
     }
 
+  /* Initialize the value ids.  */
+      
+  for (i = 1; i < num_ssa_names; ++i)
+    {
+      tree name = ssa_name (i);
+      vn_ssa_aux_t info;
+      if (!name)
+	continue;
+      info = VN_INFO (name);
+      if (info->valnum == name)
+	info->value_id = get_next_value_id ();
+      else if (is_gimple_min_invariant (info->valnum))
+	info->value_id = get_or_alloc_constant_value_id (info->valnum);
+    }
+  
+  /* Propagate until they stop changing.  */
+  while (changed)
+    {
+      changed = false;
+      for (i = 1; i < num_ssa_names; ++i)
+	{
+	  tree name = ssa_name (i);
+	  vn_ssa_aux_t info;
+	  if (!name)
+	    continue;
+	  info = VN_INFO (name);
+	  if (TREE_CODE (info->valnum) == SSA_NAME
+	      && info->valnum != name
+	      && TREE_CODE (info->valnum) == SSA_NAME
+	      && info->value_id != VN_INFO (info->valnum)->value_id)
+	    {
+	      changed = true;
+	      info->value_id = VN_INFO (info->valnum)->value_id;
+	    }
+	}
+    }
+  
+  set_hashtable_value_ids ();
+  
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
       fprintf (dump_file, "Value numbers:\n");
@@ -2300,3 +2607,83 @@
   may_insert = false;
   return true;
 }
+
+/* Return the maximum value id we have ever seen.  */
+
+unsigned int
+get_max_value_id (void) 
+{
+  return next_value_id;
+}
+
+/* Return the next unique value id.  */
+
+unsigned int
+get_next_value_id (void)
+{
+  return next_value_id++;
+}
+
+
+/* Compare two expressions E1 and E2 and return true if they are
+   equal.  */
+
+bool
+expressions_equal_p (tree e1, tree e2)
+{
+  tree te1, te2;
+
+  if (e1 == e2)
+    return true;
+
+  te1 = TREE_TYPE (e1);
+  te2 = TREE_TYPE (e2);
+
+  if (TREE_CODE (e1) == TREE_LIST && TREE_CODE (e2) == TREE_LIST)
+    {
+      tree lop1 = e1;
+      tree lop2 = e2;
+      for (lop1 = e1, lop2 = e2;
+	   lop1 || lop2;
+	   lop1 = TREE_CHAIN (lop1), lop2 = TREE_CHAIN (lop2))
+	{
+	  if (!lop1 || !lop2)
+	    return false;
+	  if (!expressions_equal_p (TREE_VALUE (lop1), TREE_VALUE (lop2)))
+	    return false;
+	}
+      return true;
+
+    }
+  else if (TREE_CODE (e1) == TREE_CODE (e2)
+	   && operand_equal_p (e1, e2, OEP_PURE_SAME))
+    return true;
+
+  return false;
+}
+
+/* Sort the VUSE array so that we can do equality comparisons
+   quicker on two vuse vecs.  */
+
+void
+sort_vuses (VEC (tree,gc) *vuses)
+{
+  if (VEC_length (tree, vuses) > 1)
+    qsort (VEC_address (tree, vuses),
+	   VEC_length (tree, vuses),
+	   sizeof (tree),
+	   operand_build_cmp);
+}
+
+/* Sort the VUSE array so that we can do equality comparisons
+   quicker on two vuse vecs.  */
+
+void
+sort_vuses_heap (VEC (tree,heap) *vuses)
+{
+  if (VEC_length (tree, vuses) > 1)
+    qsort (VEC_address (tree, vuses),
+	   VEC_length (tree, vuses),
+	   sizeof (tree),
+	   operand_build_cmp);
+}
diff -r 2325330ce9ed gcc/tree-ssa-sccvn.h
--- a/gcc/tree-ssa-sccvn.h	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-ssa-sccvn.h	Sun Jul 06 00:27:17 2008 -0400
@@ -24,12 +24,95 @@
 /* TOP of the VN lattice.  */
 extern tree VN_TOP;
 
+/* N-ary operations in the hashtable consist of length operands, an
+   opcode, and a type.  Result is the value number of the operation,
+   and hashcode is stored to avoid having to calculate it
+   repeatedly.  */
+
+typedef struct vn_nary_op_s
+{
+  /* Unique identify that all expressions with the same value have. */
+  unsigned int value_id;
+  ENUM_BITFIELD(tree_code) opcode : 16;
+  unsigned length : 16;
+  hashval_t hashcode;
+  tree result;
+  tree type;
+  tree op[4];
+} *vn_nary_op_t;
+typedef const struct vn_nary_op_s *const_vn_nary_op_t;
+
+/* Phi nodes in the hashtable consist of their non-VN_TOP phi
+   arguments, and the basic block the phi is in. Result is the value
+   number of the operation, and hashcode is stored to avoid having to
+   calculate it repeatedly.  Phi nodes not in the same block are never
+   considered equivalent.  */
+
+typedef struct vn_phi_s
+{
+  /* Unique identifier that all expressions with the same value have. */
+  unsigned int value_id;
+  hashval_t hashcode;
+  VEC (tree, heap) *phiargs;
+  basic_block block;
+  tree result;
+} *vn_phi_t;
+typedef const struct vn_phi_s *const_vn_phi_t;
+
+/* Reference operands only exist in reference operations structures.
+   They consist of an opcode, type, and some number of operands.  For
+   a given opcode, some, all, or none of the operands may be used.
+   The operands are there to store the information that makes up the
+   portion of the addressing calculation that opcode performs.  */
+
+typedef struct vn_reference_op_struct
+{
+  enum tree_code opcode;
+  tree type;
+  tree op0;
+  tree op1;
+} vn_reference_op_s;
+typedef vn_reference_op_s *vn_reference_op_t;
+typedef const vn_reference_op_s *const_vn_reference_op_t;
+
+DEF_VEC_O(vn_reference_op_s);
+DEF_VEC_ALLOC_O(vn_reference_op_s, heap);
+
+/* A reference operation in the hashtable is representation as a
+   collection of vuses, representing the memory state at the time of
+   the operation, and a collection of operands that make up the
+   addressing calculation.  If two vn_reference_t's have the same set
+   of operands, they access the same memory location. We also store
+   the resulting value number, and the hashcode.  The vuses are
+   always stored in order sorted by ssa name version.  */
+
+typedef struct vn_reference_s
+{
+  /* Unique identifier that all expressions with the same value have. */
+  unsigned int value_id;
+  hashval_t hashcode;
+  VEC (tree, gc) *vuses;
+  VEC (vn_reference_op_s, heap) *operands;
+  tree result;
+} *vn_reference_t;
+typedef const struct vn_reference_s *const_vn_reference_t;
+
+typedef struct vn_constant_s
+{
+  unsigned int value_id;
+  hashval_t hashcode;
+  tree constant;
+} *vn_constant_t;
+  
 typedef struct vn_ssa_aux
 {
   /* Value number. This may be an SSA name or a constant.  */
   tree valnum;
   /* Representative expression, if not a direct constant. */
   tree expr;
+
+  /* Unique identifier that all expressions with the same value have. */
+  unsigned int value_id;
 
   /* SCC information.  */
   unsigned int dfsnum;
@@ -57,12 +140,31 @@
 bool run_scc_vn (bool);
 void free_scc_vn (void);
 void switch_to_PRE_table (void);
-tree vn_nary_op_lookup (tree);
-void vn_nary_op_insert (tree, tree);
-tree vn_reference_lookup (tree, VEC (tree, gc) *, bool);
-void vn_reference_insert (tree, tree, VEC (tree, gc) *);
+tree vn_nary_op_lookup (tree, vn_nary_op_t *);
+tree vn_nary_op_lookup_pieces (unsigned int, enum tree_code,
+			       tree, tree, tree, tree, tree,
+			       vn_nary_op_t *);
+vn_nary_op_t vn_nary_op_insert (tree, tree);
+vn_nary_op_t vn_nary_op_insert_pieces (unsigned int, enum tree_code,
+				       tree, tree, tree, tree,
+				       tree, tree, unsigned int);
+tree vn_reference_lookup_pieces (VEC (tree, gc) *,
+				 VEC (vn_reference_op_s, heap) *,
+				 vn_reference_t *);
+tree vn_reference_lookup (tree, VEC (tree, gc) *, bool, vn_reference_t *);
+vn_reference_t vn_reference_insert (tree, tree, VEC (tree, gc) *);
+vn_reference_t vn_reference_insert_pieces (VEC (tree, gc) *,
+					   VEC (vn_reference_op_s, heap) *,
+					   tree, unsigned int);
+
+hashval_t vn_nary_op_compute_hash (const vn_nary_op_t);
+int vn_nary_op_eq (const void *, const void *);
+hashval_t vn_reference_compute_hash (const vn_reference_t);
+int vn_reference_eq (const void *, const void *);
+unsigned int get_max_value_id (void);
+unsigned int get_next_value_id (void);
+unsigned int get_or_alloc_constant_value_id (tree);
+bool value_id_constant_p (unsigned int);
 VEC (tree, gc) *shared_vuses_from_stmt (tree);
 VEC (tree, gc) *copy_vuses_from_stmt (tree);
-
-
 #endif /* TREE_SSA_SCCVN_H  */
diff -r 2325330ce9ed gcc/tree-ssa-threadedge.c
--- a/gcc/tree-ssa-threadedge.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-ssa-threadedge.c	Sun Jul 06 00:27:17 2008 -0400
@@ -313,7 +313,7 @@
 	      copy[i++] = use;
 	      if (TREE_CODE (use) == SSA_NAME)
 		tmp = SSA_NAME_VALUE (use);
-	      if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
+	      if (tmp)
 		SET_USE (use_p, tmp);
 	    }
 
@@ -407,14 +407,14 @@
       if (TREE_CODE (op0) == SSA_NAME)
 	{
           tree tmp = SSA_NAME_VALUE (op0);
-	  if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
+	  if (tmp)
 	    op0 = tmp;
 	}
 
       if (TREE_CODE (op1) == SSA_NAME)
 	{
 	  tree tmp = SSA_NAME_VALUE (op1);
-	  if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
+	  if (tmp)
 	    op1 = tmp;
 	}
 
diff -r 2325330ce9ed gcc/tree-vn.c
--- a/gcc/tree-vn.c	Sun Jul 06 00:16:30 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,406 +0,0 @@
-/* Value Numbering routines for tree expressions.
-   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008 Free Software
-   Foundation, Inc.
-   Contributed by Daniel Berlin <dan@dberlin.org>, Steven Bosscher
-   <stevenb@suse.de> and 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 3, or (at your option)
-any later version.
-
-GCC is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-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 COPYING3.  If not see
-<http://www.gnu.org/licenses/>.  */
-
-#include "config.h"
-#include "system.h"
-#include "coretypes.h"
-#include "tm.h"
-#include "ggc.h"
-#include "tree.h"
-#include "tree-flow.h"
-#include "hashtab.h"
-#include "langhooks.h"
-#include "tree-pass.h"
-#include "tree-dump.h"
-#include "diagnostic.h"
-#include "tree-ssa-sccvn.h"
-
-/* Most of this is PRE specific.  The real grunt work is done in
-   tree-ssa-sccvn.c.  This is where the lookup and insertion
-   functions, etc, can be found.  */
-
-/* Create and return a new value handle node of type TYPE.  */
-
-tree
-make_value_handle (tree type)
-{
-  static unsigned int id = 0;
-  tree vh;
-
-  vh = build0 (VALUE_HANDLE, type);
-  VALUE_HANDLE_ID (vh) = id++;
-  return vh;
-}
-
-/* Compare two expressions E1 and E2 and return true if they are
-   equal.  */
-
-bool
-expressions_equal_p (tree e1, tree e2)
-{
-  tree te1, te2;
-
-  if (e1 == e2)
-    return true;
-
-  te1 = TREE_TYPE (e1);
-  te2 = TREE_TYPE (e2);
-
-  if (TREE_CODE (e1) == TREE_LIST && TREE_CODE (e2) == TREE_LIST)
-    {
-      tree lop1 = e1;
-      tree lop2 = e2;
-      for (lop1 = e1, lop2 = e2;
-	   lop1 || lop2;
-	   lop1 = TREE_CHAIN (lop1), lop2 = TREE_CHAIN (lop2))
-	{
-	  if (!lop1 || !lop2)
-	    return false;
-	  if (!expressions_equal_p (TREE_VALUE (lop1), TREE_VALUE (lop2)))
-	    return false;
-	}
-      return true;
-
-    }
-  else if (TREE_CODE (e1) == TREE_CODE (e2)
-	   && operand_equal_p (e1, e2, OEP_PURE_SAME))
-    return true;
-
-  return false;
-}
-
-/* Set the value handle for expression E to value V.  */
-
-void
-set_value_handle (tree e, tree v)
-{
-  if (TREE_CODE (e) == SSA_NAME)
-    SSA_NAME_VALUE (e) = v;
-  else if (EXPR_P (e) || DECL_P (e) || TREE_CODE (e) == TREE_LIST
-	   || GIMPLE_STMT_P (e)
-	   || TREE_CODE (e) == CONSTRUCTOR)
-    get_tree_common_ann (e)->value_handle = v;
-  else
-    /* Do nothing.  Constants are their own value handles.  */
-    gcc_assert (is_gimple_min_invariant (e));
-}
-
-/* Print out the "Created value <x> for <Y>" statement to the
-   dump_file.
-   This is factored because both versions of lookup use it, and it
-   obscures the real work going on in those functions.  */
-
-static void
-print_creation_to_file (tree v, tree expr, VEC (tree, gc) *vuses)
-{
-  fprintf (dump_file, "Created value ");
-  print_generic_expr (dump_file, v, dump_flags);
-  fprintf (dump_file, " for ");
-  print_generic_expr (dump_file, expr, dump_flags);
-
-  if (vuses && VEC_length (tree, vuses) != 0)
-    {
-      size_t i;
-      tree vuse;
-
-      fprintf (dump_file, " vuses: (");
-      for (i = 0; VEC_iterate (tree, vuses, i, vuse); i++)
-	{
-	  print_generic_expr (dump_file, vuse, dump_flags);
-	  if (VEC_length (tree, vuses) - 1 != i)
-	    fprintf (dump_file, ",");
-	}
-      fprintf (dump_file, ")");
-    }
-  fprintf (dump_file, "\n");
-}
-
-/* Sort the VUSE array so that we can do equality comparisons
-   quicker on two vuse vecs.  */
-
-void
-sort_vuses (VEC (tree,gc) *vuses)
-{
-  if (VEC_length (tree, vuses) > 1)
-    qsort (VEC_address (tree, vuses),
-	   VEC_length (tree, vuses),
-	   sizeof (tree),
-	   operand_build_cmp);
-}
-
-/* Sort the VUSE array so that we can do equality comparisons
-   quicker on two vuse vecs.  */
-
-void
-sort_vuses_heap (VEC (tree,heap) *vuses)
-{
-  if (VEC_length (tree, vuses) > 1)
-    qsort (VEC_address (tree, vuses),
-	   VEC_length (tree, vuses),
-	   sizeof (tree),
-	   operand_build_cmp);
-}
-
-/* Insert EXPR into VALUE_TABLE with value VAL, and add expression
-   EXPR to the value set for value VAL.  */
-
-void
-vn_add (tree expr, tree val)
-{
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_comparison:
-    case tcc_binary:
-      vn_nary_op_insert (expr, val);
-      break;
-    case tcc_unary:
-      vn_nary_op_insert (expr, val);
-      break;
-      /* In the case of array-refs of constants, for example, we can
-	 end up with no vuses.  */
-    case tcc_reference:
-      vn_reference_insert (expr, val, NULL);
-      break;
-      /* The *only* time CALL_EXPR should appear here is
-	 when it has no vuses.  */
-    case tcc_vl_exp:
-    case tcc_exceptional:
-    case tcc_expression:
-    case tcc_declaration:
-      if (TREE_CODE (expr) == CALL_EXPR || DECL_P (expr))
-	{
-	  vn_reference_insert (expr, val, NULL);
-	  break;
-	}
-      else if (TREE_CODE (expr) == SSA_NAME)
-	{
-	  SSA_NAME_VALUE (expr) = val;
-	  break;
-	}
-      switch (TREE_CODE (expr))
-	{
-	case ADDR_EXPR:
-	case TRUTH_AND_EXPR:
-	case TRUTH_OR_EXPR:
-	case TRUTH_XOR_EXPR:
-	case TRUTH_NOT_EXPR:
-	  vn_nary_op_insert (expr, val);
-	    break;
-	default:
-	  gcc_unreachable ();
-	}
-      break;
-    default:
-      gcc_unreachable ();
-    }
-  set_value_handle (expr, val);
-  if (TREE_CODE (val) == VALUE_HANDLE)
-    add_to_value (val, expr);
-}
-
-/* Insert EXPR into the value numbering tables with value VAL, and
-   add expression EXPR to the value set for value VAL.  VUSES
-   represents the virtual use operands associated with EXPR.  It is
-   used when computing a hash value for EXPR.  */
-
-void
-vn_add_with_vuses (tree expr, tree val, VEC (tree, gc) *vuses)
-{
-  if (!vuses)
-    {
-      vn_add (expr, val);
-      return;
-    }
-  vn_reference_insert (expr, val, vuses);
-
-  set_value_handle (expr, val);
-  if (TREE_CODE (val) == VALUE_HANDLE)
-    add_to_value (val, expr);
-}
-
-/* Lookup EXPR in the value numbering tables and return the result, if
-   we have one.  */
-
-tree
-vn_lookup (tree expr)
-{
-  /* Constants are their own value.  */
-  if (is_gimple_min_invariant (expr) || TREE_CODE (expr) == FIELD_DECL)
-    return expr;
-
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_comparison:
-    case tcc_binary:
-      return vn_nary_op_lookup (expr);
-    case tcc_unary:
-      return vn_nary_op_lookup (expr);
-      break;
-      /* In the case of array-refs of constants, for example, we can
-	 end up with no vuses.  */
-    case tcc_reference:
-      return vn_reference_lookup (expr, NULL, false);
-      break;
-      /* It is possible to have CALL_EXPR with no vuses for things
-	 like "cos", and these will fall into vn_lookup.   */
-    case tcc_vl_exp:
-    case tcc_exceptional:
-    case tcc_expression:
-    case tcc_declaration:
-      if (TREE_CODE (expr) == CALL_EXPR || DECL_P (expr))
-	return vn_reference_lookup (expr, NULL, false);
-      else if (TREE_CODE (expr) == SSA_NAME)
-	return SSA_NAME_VALUE (expr);
-      switch (TREE_CODE (expr))
-	{
-	case ADDR_EXPR:
-	case TRUTH_AND_EXPR:
-	case TRUTH_OR_EXPR:
-	case TRUTH_XOR_EXPR:
-	case TRUTH_NOT_EXPR:
-	  return vn_nary_op_lookup (expr);
-	default:
-	  gcc_unreachable ();
-	}
-      break;
-    default:
-      gcc_unreachable ();
-    }
-  return NULL;
-}
-
-/* Search in the value numbering tables for an existing instance of
-   expression EXPR,  and return its value, or NULL if none has been set.  STMT
-   represents the stmt associated with EXPR.  It is used when computing the
-   hash value for EXPR for reference operations.  */
-
-tree
-vn_lookup_with_stmt (tree expr, tree stmt)
-{
-  if (stmt == NULL)
-    return vn_lookup (expr);
-
-  /* Constants are their own value.  */
-  if (is_gimple_min_invariant (expr) || TREE_CODE (expr) == FIELD_DECL)
-    return expr;
-
-  return vn_lookup_with_vuses (expr, shared_vuses_from_stmt (stmt));
-}
-
-/* Search in VALUE_TABLE for an existing instance of expression EXPR,
-   and return its value, or NULL if none has been set.  VUSES is the
-   list of virtual use operands associated with EXPR.  It is used when
-   computing the hash value for EXPR.  */
-
-tree
-vn_lookup_with_vuses (tree expr, VEC (tree, gc) *vuses)
-{
-  if (!vuses || !VEC_length (tree, vuses))
-    return vn_lookup (expr);
-
-  if (is_gimple_min_invariant (expr) || TREE_CODE (expr) == FIELD_DECL)
-    return expr;
-
-  /* We may not walk the use-def chains here as the alias oracle cannot
-     properly deal with VALUE_HANDLE tree nodes we feed it here.  */
-  return vn_reference_lookup (expr, vuses, false);
-}
-
-static tree
-create_value_handle_for_expr (tree expr, VEC(tree, gc) *vuses)
-{
-  tree v;
-
-  v = make_value_handle (TREE_TYPE (expr));
-
-  if (dump_file && (dump_flags & TDF_DETAILS))
-    print_creation_to_file (v, expr, vuses);
-  return v;
-}
-
-/* Like vn_lookup, but creates a new value for the operation if one
-   does not exist.  */
-
-tree
-vn_lookup_or_add (tree expr)
-{
-  tree v = vn_lookup (expr);
-
-  if (v == NULL_TREE)
-    {
-      v = create_value_handle_for_expr (expr, NULL);
-      vn_add (expr, v);
-    }
-  else
-    set_value_handle (expr, v);
-
-  return v;
-}
-
-/* Like vn_lookup, but handles reference operations as well by using
-   STMT to get the set of vuses.  */
-
-tree
-vn_lookup_or_add_with_stmt (tree expr, tree stmt)
-{
-  tree v;
-  if (!stmt)
-    return vn_lookup_or_add (expr);
-
-  v = vn_lookup_with_stmt (expr, stmt);
-  if (v == NULL_TREE)
-    {
-      VEC (tree, gc) *vuses = copy_vuses_from_stmt (stmt);
-      v = create_value_handle_for_expr (expr, vuses);
-      vn_add_with_vuses (expr, v, vuses);
-    }
-  else
-    set_value_handle (expr, v);
-
-  return v;
-}
-
-/* Like vn_lookup, but creates a new value for expression EXPR, if
-   EXPR doesn't already have a value.  Return the existing/created
-   value for EXPR.  STMT represents the stmt associated with EXPR.  It is used
-   when computing the hash value for EXPR.  */
-
-tree
-vn_lookup_or_add_with_vuses (tree expr, VEC (tree, gc) *vuses)
-{
-  tree v;
-
-  if (!vuses || VEC_length (tree, vuses) == 0)
-    return vn_lookup_or_add (expr);
-
-  v = vn_lookup_with_vuses (expr, vuses);
-  if (v == NULL_TREE)
-    {
-      v = create_value_handle_for_expr (expr, vuses);
-      vn_add_with_vuses (expr, v, vuses);
-    }
-  else
-    set_value_handle (expr, v);
-
-  return v;
-}
-
diff -r 2325330ce9ed gcc/tree.c
--- a/gcc/tree.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree.c	Sun Jul 06 00:27:17 2008 -0400
@@ -462,7 +462,6 @@
 
 	case STATEMENT_LIST:	return sizeof (struct tree_statement_list);
 	case BLOCK:		return sizeof (struct tree_block);
-	case VALUE_HANDLE:	return sizeof (struct tree_value_handle);
 	case CONSTRUCTOR:	return sizeof (struct tree_constructor);
 
 	default:
@@ -2387,7 +2386,6 @@
     case BLOCK:			return TS_BLOCK;
     case CONSTRUCTOR:		return TS_CONSTRUCTOR;
     case TREE_BINFO:		return TS_BINFO;
-    case VALUE_HANDLE:		return TS_VALUE_HANDLE;
     case OMP_CLAUSE:		return TS_OMP_CLAUSE;
 
     default:
@@ -5358,7 +5356,6 @@
       return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
 
     case SSA_NAME:
-    case VALUE_HANDLE:
       /* we can just compare by pointer.  */
       return iterative_hash_pointer (t, val);
 
diff -r 2325330ce9ed gcc/tree.def
--- a/gcc/tree.def	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree.def	Sun Jul 06 00:27:17 2008 -0400
@@ -925,12 +925,6 @@
    Use the interface in tree-iterator.h to access this node.  */
 DEFTREECODE (STATEMENT_LIST, "statement_list", tcc_exceptional, 0)
 
-/* Value handles.  Artificial nodes to represent expressions in
-   partial redundancy elimination (tree-ssa-pre.c).  These nodes are
-   used for expression canonicalization.  If two expressions compute
-   the same value, they will be assigned the same value handle.  */
-DEFTREECODE (VALUE_HANDLE, "value_handle", tcc_exceptional, 0)
-
 /* Predicate assertion.  Artificial expression generated by the optimizers
    to keep track of predicate values.  This expression may only appear on
    the RHS of assignments.
diff -r 2325330ce9ed gcc/tree.h
--- a/gcc/tree.h	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree.h	Sun Jul 06 00:27:17 2008 -0400
@@ -3492,26 +3492,6 @@
   struct tree_statement_list_node *tail;
 };
 
-#define VALUE_HANDLE_ID(NODE)		\
-  (VALUE_HANDLE_CHECK (NODE)->value_handle.id)
-
-#define VALUE_HANDLE_EXPR_SET(NODE)	\
-  (VALUE_HANDLE_CHECK (NODE)->value_handle.expr_set)
-
-/* Defined and used in tree-ssa-pre.c.  */
-
-struct tree_value_handle GTY(())
-{
-  struct tree_common common;
-
-  /* The set of expressions represented by this handle.  */
-  struct bitmap_set * GTY ((skip)) expr_set;
-
-  /* Unique ID for this value handle.  IDs are handed out in a
-     conveniently dense form starting at 0, so that we can make
-     bitmaps of value handles.  */
-  unsigned int id;
-};
 \f
 /* Define the overall contents of a tree node.
    It may be any of the structures declared above
@@ -3552,7 +3532,6 @@
   struct tree_binfo GTY ((tag ("TS_BINFO"))) binfo;
   struct tree_statement_list GTY ((tag ("TS_STATEMENT_LIST"))) stmt_list;
   struct gimple_stmt GTY ((tag ("TS_GIMPLE_STATEMENT"))) gstmt;
-  struct tree_value_handle GTY ((tag ("TS_VALUE_HANDLE"))) value_handle;
   struct tree_constructor GTY ((tag ("TS_CONSTRUCTOR"))) constructor;
   struct tree_memory_tag GTY ((tag ("TS_MEMORY_TAG"))) mtag;
   struct tree_omp_clause GTY ((tag ("TS_OMP_CLAUSE"))) omp_clause;
diff -r 2325330ce9ed gcc/treestruct.def
--- a/gcc/treestruct.def	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/treestruct.def	Sun Jul 06 00:27:17 2008 -0400
@@ -59,7 +59,6 @@
 DEFTREESTRUCT(TS_BINFO, "binfo")
 DEFTREESTRUCT(TS_STATEMENT_LIST, "statement list")
 DEFTREESTRUCT(TS_GIMPLE_STATEMENT, "gimple statement")
-DEFTREESTRUCT(TS_VALUE_HANDLE, "value handle")
 DEFTREESTRUCT(TS_CONSTRUCTOR, "constructor")
 DEFTREESTRUCT(TS_MEMORY_TAG, "memory tag")
 DEFTREESTRUCT(TS_OMP_CLAUSE, "omp clause")

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-06  4:48 [PATCH]: Rewrite tree level PRE Daniel Berlin
@ 2008-07-06  8:58 ` Ralf Wildenhues
  2008-07-06 19:24   ` Daniel Berlin
  2008-07-06 15:23 ` Richard Guenther
  1 sibling, 1 reply; 16+ messages in thread
From: Ralf Wildenhues @ 2008-07-06  8:58 UTC (permalink / raw)
  To: Daniel Berlin; +Cc: gcc-patches

Hello Daniel,

* Daniel Berlin wrote on Sun, Jul 06, 2008 at 06:33:10AM CEST:
> 	* Makefile.in (OBJS-common): Remove tree-vn.o.
> 	tree-vn.o: Remove.

> 	* tree-ssa-pre.c: Rewritten entirely.

> --- a/gcc/Makefile.in	Sun Jul 06 00:16:30 2008 +0000
> +++ b/gcc/Makefile.in	Sun Jul 06 00:27:17 2008 -0400

Needs $(DBGCNT_H) added to tree-ssa-pre.o prerequisites.

> --- a/gcc/tree-ssa-pre.c	Sun Jul 06 00:16:30 2008 +0000
> +++ b/gcc/tree-ssa-pre.c	Sun Jul 06 00:27:17 2008 -0400
> @@ -46,6 +46,7 @@
>  #include "cfgloop.h"
>  #include "tree-ssa-sccvn.h"
>  #include "params.h"
> +#include "dbgcnt.h"

Cheers,
Ralf

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-06  4:48 [PATCH]: Rewrite tree level PRE Daniel Berlin
  2008-07-06  8:58 ` Ralf Wildenhues
@ 2008-07-06 15:23 ` Richard Guenther
  2008-07-06 19:19   ` Daniel Berlin
  2008-07-11 13:12   ` [PATCH]: Rewrite tree level PRE Eric Botcazou
  1 sibling, 2 replies; 16+ messages in thread
From: Richard Guenther @ 2008-07-06 15:23 UTC (permalink / raw)
  To: Daniel Berlin; +Cc: gcc-patches

On Sun, Jul 6, 2008 at 6:33 AM, Daniel Berlin <dberlin@dberlin.org> wrote:
> This patch effectively rewrites tree PRE to share datastructures with
> tree-vn, instead of copying statements and modifying trees (which
> don't work so well in a tuples world :P).
>
> As part of the rewrite, it also fixes a bunch of missed optimizations
> because PRE will now move globals.
> It also uses roughly half as much memory as before (since PRE no
> longer copies statements but instead shares the vn structures with the
> vn).
> The fundamentals are the same as they were before, it simply no longer
> does it's own value numbering.
> Since PRE was the only user of VALUE_HANDLES, i have removed them as
> part of this patch.
> Since PRE was the only user of most of tree-vn.c, i removed it and
> moved the 3 still-used functions to tree-ssa-sccvn.c
> PRE is also now type correct, in that the insertions it creates should
> have matched types, whereas before they were generally a mismash (it
> would happily create phi nodes whose arguments had mixed types, etc).
>
> This patch will cause some FRE failures due to a small bit of code i
> disabled in tree-ssa-sccvn.c.  I have discussed this with richard
> guenther and he has said he will fix it up, so the ssa-fre-* failures
> are temporary.
>
> Other than than PRE of globals, it should PRE all of the expressions
> we used to, plus a few more in the way of loads.  I have verified this
> by hand comparing the eliminations performed during a bootstrap of
> gcc.
> I have not performed more benchmarking than that, as the main goal was
> a feature equivalent rewrite that would enable tuple conversion to
> procede. The fact that it will PRE globals is simply a side effect of
> sharing a representation with the VN.
>
> The patch is large but 99% of it is the rewritten tree-ssa-pre.c.
>
> Bootstrapped and regtested on i686-darwin (With the safe-ctype change
> reverted) and i686-linux.
> I will wait a few days for comments before committing it (All the code
> outside of PRE that I am removing was added as part of PRE, but if
> someone wants to object to it's removal, please let me know).

This triggers a latent issue in SCCVN (it breaks bootstrap on x86_64 with Ada).

copy_reference_ops_from_ref for ARRAY_REF also needs to record
TREE_OPERAND (ref, 2) (likewise foro COMPONENT_REF).

Without that we ICE during gimplifying of

(*pretmp.7645_139)[pretmp.7649_143]{lb: (long int) <PLACEHOLDER_EXPR
struct types__text_buffer___XUP>.P_BOUNDS->LB0 sz: 1}

#1  0x00000000009be6ab in gimplify_expr (expr_p=0x2b0b398ed8f0,
    pre_p=0x7fff721f58d8, post_p=0x7fff721f5088,
    gimple_test_f=0xb7cfe4 <is_gimple_min_lval>, fallback=fb_either)
    at ../../trunk/gcc/gimplify.c:6321
...
#10 0x00000000009c16b0 in force_gimple_operand (expr=0x2b0b398ed820,
    stmts=0x7fff721f58d8, simple=0 '\0', var=0x0)
    at ../../trunk/gcc/gimplify.c:6825
#11 0x0000000000cc9859 in create_expression_by_pieces (block=0x2b0b39c5c480,
    expr=0x1ecd1c8, stmts=0x2b0b39914360, domstmt=0x0, type=0x2b0b38ea9480)
---Type <return> to continue, or q <return> to quit---
    at ../../trunk/gcc/tree-ssa-pre.c:2667

we obviously cannot use the PLACEHOLDER_EXPR, but need to use the
gimplified value that was stored in the ARRAY_EXPRs operand 3.

So, the number of ops in vn_reference_op_struct needs to increase by one.

To reproduce

./xgcc -B. -B/usr/local/x86_64-unknown-linux-gnu/bin/ -c -g -O2
-gnatpg -gnata -gnatwns -nostdinc -I- -I. -Iada -I../../trunk/gcc/ada
../../trunk/gcc/ada/ali.adb -o ada/ali.o

in the stage1 gcc/ dir.

Richard.

> 2008-07-05  Daniel Berlin  <dberlin@dberlin.org>
>
>        Fix PR tree-optimization/23455
>        Fix PR tree-optimization/35286
>        Fix PR tree-optimization/35287
>        * Makefile.in (OBJS-common): Remove tree-vn.o.
>        tree-vn.o: Remove.
>        * dbgcnt.def: Add treepre_insert debug counter.
>        * gcc/tree-flow.h (add_to_value): Updated for other changes.
>        (debug_value_expressions): Ditto.
>        (print_value_expressions): Ditto.
>        * tree-pretty-print.c (dump_generic_node): Updated for
>        VALUE_HANDLE removal.
>        * tree-ssa-dom.c (record_equality): Ditto.
>        (cprop_operand): Ditto.
>        (lookup_avail_expr): Ditto.
>        * tree-ssa-threadedge.c
>        (record_temporary_equivalences_from_stmts_at_dest): Ditto.
>        (simplify_control_stmt_condition): Ditto.
>        * tree.c (tree_code_size): Ditto.
>        (tree_node_structure): Ditto.
>        (iterative_hash_expr): Ditto.
>        * tree.def: Ditto.
>        * tree.h (VALUE_HANDLE_ID): Ditto.
>        (VALUE_HANDLE_EXPR_SET): Ditto.
>        (struct tree_value_handle): Ditto.
>        (union tree_node): Ditto.
>        * treestruct.def: Ditto.
>        * tree-vn.c: Removed.
>        * tree-ssa-pre.c: Rewritten entirely.
>        * tree-ssa-sccvn.c (constant_to_value_id): New hashtable.
>        (constant_value_ids): Ditto.
>        (vn_nary_op_t): Moved to header.
>        (vn_phi_t): Ditto.
>        (vn_reference_op_t): Ditto
>        (vn_reference_t): Ditto.
>        (next_value_id): New variable.
>        (VN_INFO): Add an assert.
>        (vn_constant_eq): New function.
>        (vn_constant_hash): Ditto.
>        (get_or_alloc_constant_value_id): Ditto.
>        (value_id_constant_p): Ditto.
>        (vn_reference_compute_hash): De-staticify.
>        (copy_reference_ops_from_ref): Don't use get_callee_fndecl.
>        Disable some code with a FIXME.
>        Remove VALUE_HANDLE use.
>        (valueize_refs): Update opcode if it changes from ssa name to
>        constant.
>        (vn_reference_lookup_1): Add new argument.
>        (vn_reference_lookup):  Ditto.
>        (vn_reference_lookup_pieces): New function.
>        (vn_reference_insert): Add return type. Modify to deal with value
>        ids.
>        (vn_reference_insert_pieces):  New function.
>        (vn_nary_op_compute_hash): De-staticify.
>        (vn_nary_op_eq): Ditto.
>        (vn_nary_op_lookup_pieces): New function.
>        (vn_nary_op_lookup): Add new argument.
>        (vn_nary_op_insert_pieces): New function.
>        (vn_nary_op_insert): Add return type. Modify to deal with value
>        ids.
>        (vn_phi_insert): Ditto.
>        (visit_unary_op): Update for callee changes.
>        (visit_binary_op): Ditto.
>        (visit_reference_op_load): Ditto.
>        (visit_reference_op_store): Ditto.
>        (init_scc_vn): Init next_value_id, constant_to_value_id and constant_value_ids.
>        (free_scc_vn): Free them.
>        (set_hashtable_value_ids): New function.
>        (run_scc_vn): Use it.
>        (get_max_value_id): New function.
>        (get_next_value_id): Ditto.
>        (expressions_equal_p): Moved from tree-vn.c
>        (sort_vuses): Ditto.
>        (sort_vuses_heap): Ditto.
>        * tree-ssa-sccvn.h: Structures moved from tree-ssa-sccvn.c (noted
>        above).
>
>
> --Dan
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-06 15:23 ` Richard Guenther
@ 2008-07-06 19:19   ` Daniel Berlin
  2008-07-06 22:12     ` Daniel Berlin
  2008-07-11 13:12   ` [PATCH]: Rewrite tree level PRE Eric Botcazou
  1 sibling, 1 reply; 16+ messages in thread
From: Daniel Berlin @ 2008-07-06 19:19 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc-patches

On Sun, Jul 6, 2008 at 10:32 AM, Richard Guenther
<richard.guenther@gmail.com> wrote:
> On Sun, Jul 6, 2008 at 6:33 AM, Daniel Berlin <dberlin@dberlin.org> wrote:
>> This patch effectively rewrites tree PRE to share datastructures with
>> tree-vn, instead of copying statements and modifying trees (which
>> don't work so well in a tuples world :P).
>>
>> As part of the rewrite, it also fixes a bunch of missed optimizations
>> because PRE will now move globals.
>> It also uses roughly half as much memory as before (since PRE no
>> longer copies statements but instead shares the vn structures with the
>> vn).
>> The fundamentals are the same as they were before, it simply no longer
>> does it's own value numbering.
>> Since PRE was the only user of VALUE_HANDLES, i have removed them as
>> part of this patch.
>> Since PRE was the only user of most of tree-vn.c, i removed it and
>> moved the 3 still-used functions to tree-ssa-sccvn.c
>> PRE is also now type correct, in that the insertions it creates should
>> have matched types, whereas before they were generally a mismash (it
>> would happily create phi nodes whose arguments had mixed types, etc).
>>
>> This patch will cause some FRE failures due to a small bit of code i
>> disabled in tree-ssa-sccvn.c.  I have discussed this with richard
>> guenther and he has said he will fix it up, so the ssa-fre-* failures
>> are temporary.
>>
>> Other than than PRE of globals, it should PRE all of the expressions
>> we used to, plus a few more in the way of loads.  I have verified this
>> by hand comparing the eliminations performed during a bootstrap of
>> gcc.
>> I have not performed more benchmarking than that, as the main goal was
>> a feature equivalent rewrite that would enable tuple conversion to
>> procede. The fact that it will PRE globals is simply a side effect of
>> sharing a representation with the VN.
>>
>> The patch is large but 99% of it is the rewritten tree-ssa-pre.c.
>>
>> Bootstrapped and regtested on i686-darwin (With the safe-ctype change
>> reverted) and i686-linux.
>> I will wait a few days for comments before committing it (All the code
>> outside of PRE that I am removing was added as part of PRE, but if
>> someone wants to object to it's removal, please let me know).
>
> This triggers a latent issue in SCCVN (it breaks bootstrap on x86_64 with Ada).
>

I don't have an ada compiler on my x86_64. If i send you an updated
patch, can you test it for me with ada?

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-06  8:58 ` Ralf Wildenhues
@ 2008-07-06 19:24   ` Daniel Berlin
  0 siblings, 0 replies; 16+ messages in thread
From: Daniel Berlin @ 2008-07-06 19:24 UTC (permalink / raw)
  To: Ralf Wildenhues, Daniel Berlin, gcc-patches

Done, thanks

On Sun, Jul 6, 2008 at 3:56 AM, Ralf Wildenhues <Ralf.Wildenhues@gmx.de> wrote:
> Hello Daniel,
>
> * Daniel Berlin wrote on Sun, Jul 06, 2008 at 06:33:10AM CEST:
>>       * Makefile.in (OBJS-common): Remove tree-vn.o.
>>       tree-vn.o: Remove.
>
>>       * tree-ssa-pre.c: Rewritten entirely.
>
>> --- a/gcc/Makefile.in Sun Jul 06 00:16:30 2008 +0000
>> +++ b/gcc/Makefile.in Sun Jul 06 00:27:17 2008 -0400
>
> Needs $(DBGCNT_H) added to tree-ssa-pre.o prerequisites.
>
>> --- a/gcc/tree-ssa-pre.c      Sun Jul 06 00:16:30 2008 +0000
>> +++ b/gcc/tree-ssa-pre.c      Sun Jul 06 00:27:17 2008 -0400
>> @@ -46,6 +46,7 @@
>>  #include "cfgloop.h"
>>  #include "tree-ssa-sccvn.h"
>>  #include "params.h"
>> +#include "dbgcnt.h"
>
> Cheers,
> Ralf
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-06 19:19   ` Daniel Berlin
@ 2008-07-06 22:12     ` Daniel Berlin
  2008-07-07 16:49       ` Daniel Berlin
  0 siblings, 1 reply; 16+ messages in thread
From: Daniel Berlin @ 2008-07-06 22:12 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 2662 bytes --]

I got it going on my home machine.
The attached fixes the problem.


On Sun, Jul 6, 2008 at 1:46 PM, Daniel Berlin <dberlin@dberlin.org> wrote:
> On Sun, Jul 6, 2008 at 10:32 AM, Richard Guenther
> <richard.guenther@gmail.com> wrote:
>> On Sun, Jul 6, 2008 at 6:33 AM, Daniel Berlin <dberlin@dberlin.org> wrote:
>>> This patch effectively rewrites tree PRE to share datastructures with
>>> tree-vn, instead of copying statements and modifying trees (which
>>> don't work so well in a tuples world :P).
>>>
>>> As part of the rewrite, it also fixes a bunch of missed optimizations
>>> because PRE will now move globals.
>>> It also uses roughly half as much memory as before (since PRE no
>>> longer copies statements but instead shares the vn structures with the
>>> vn).
>>> The fundamentals are the same as they were before, it simply no longer
>>> does it's own value numbering.
>>> Since PRE was the only user of VALUE_HANDLES, i have removed them as
>>> part of this patch.
>>> Since PRE was the only user of most of tree-vn.c, i removed it and
>>> moved the 3 still-used functions to tree-ssa-sccvn.c
>>> PRE is also now type correct, in that the insertions it creates should
>>> have matched types, whereas before they were generally a mismash (it
>>> would happily create phi nodes whose arguments had mixed types, etc).
>>>
>>> This patch will cause some FRE failures due to a small bit of code i
>>> disabled in tree-ssa-sccvn.c.  I have discussed this with richard
>>> guenther and he has said he will fix it up, so the ssa-fre-* failures
>>> are temporary.
>>>
>>> Other than than PRE of globals, it should PRE all of the expressions
>>> we used to, plus a few more in the way of loads.  I have verified this
>>> by hand comparing the eliminations performed during a bootstrap of
>>> gcc.
>>> I have not performed more benchmarking than that, as the main goal was
>>> a feature equivalent rewrite that would enable tuple conversion to
>>> procede. The fact that it will PRE globals is simply a side effect of
>>> sharing a representation with the VN.
>>>
>>> The patch is large but 99% of it is the rewritten tree-ssa-pre.c.
>>>
>>> Bootstrapped and regtested on i686-darwin (With the safe-ctype change
>>> reverted) and i686-linux.
>>> I will wait a few days for comments before committing it (All the code
>>> outside of PRE that I am removing was added as part of PRE, but if
>>> someone wants to object to it's removal, please let me know).
>>
>> This triggers a latent issue in SCCVN (it breaks bootstrap on x86_64 with Ada).
>>
>
> I don't have an ada compiler on my x86_64. If i send you an updated
> patch, can you test it for me with ada?
>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: newpre.diff --]
[-- Type: text/x-diff; name=newpre.diff, Size: 179707 bytes --]

diff -r 2325330ce9ed gcc/ChangeLog
--- a/gcc/ChangeLog	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/ChangeLog	Sun Jul 06 15:28:19 2008 -0400
@@ -1,3 +1,81 @@
+2008-07-05  Daniel Berlin  <dberlin@dberlin.org>
+	
+	Fix PR tree-optimization/23455
+	Fix PR tree-optimization/35286
+	Fix PR tree-optimization/35287
+	* Makefile.in (OBJS-common): Remove tree-vn.o.
+	tree-vn.o: Remove.
+	* dbgcnt.def: Add treepre_insert debug counter.
+	* gcc/tree-flow.h (add_to_value): Updated for other changes.
+	(debug_value_expressions): Ditto.
+	(print_value_expressions): Ditto.
+	* tree-pretty-print.c (dump_generic_node): Updated for
+	VALUE_HANDLE removal.
+	* tree-ssa-dom.c (record_equality): Ditto.
+	(cprop_operand): Ditto.
+	(lookup_avail_expr): Ditto.
+	* tree-ssa-threadedge.c
+	(record_temporary_equivalences_from_stmts_at_dest): Ditto.
+	(simplify_control_stmt_condition): Ditto.
+	* tree.c (tree_code_size): Ditto.
+	(tree_node_structure): Ditto.
+	(iterative_hash_expr): Ditto.
+	* tree.def: Ditto.
+	* tree.h (VALUE_HANDLE_ID): Ditto.
+	(VALUE_HANDLE_EXPR_SET): Ditto.
+	(struct tree_value_handle): Ditto.
+	(union tree_node): Ditto.
+	* treestruct.def: Ditto.
+	* tree-vn.c: Removed.
+	* tree-ssa-pre.c: Rewritten entirely.
+	* tree-ssa-sccvn.c (constant_to_value_id): New hashtable.
+	(constant_value_ids): Ditto.
+	(vn_nary_op_t): Moved to header.
+	(vn_phi_t): Ditto.
+	(vn_reference_op_t): Ditto
+	(vn_reference_t): Ditto.
+	(next_value_id): New variable.
+	(VN_INFO): Add an assert.
+	(vn_constant_eq): New function.
+	(vn_constant_hash): Ditto.
+	(get_or_alloc_constant_value_id): Ditto.
+	(value_id_constant_p): Ditto.
+	(vn_reference_compute_hash): De-staticify.
+	(copy_reference_ops_from_ref): Don't use get_callee_fndecl.
+	Disable some code with a FIXME.
+	Remove VALUE_HANDLE use.
+	(valueize_refs): Update opcode if it changes from ssa name to
+	constant.
+	(vn_reference_lookup_1): Add new argument.
+	(vn_reference_lookup):  Ditto.
+	(vn_reference_lookup_pieces): New function.
+	(vn_reference_insert): Add return type. Modify to deal with value
+	ids.
+	(vn_reference_insert_pieces):  New function.
+	(vn_nary_op_compute_hash): De-staticify.
+	(vn_nary_op_eq): Ditto.
+	(vn_nary_op_lookup_pieces): New function.
+	(vn_nary_op_lookup): Add new argument.  
+	(vn_nary_op_insert_pieces): New function.
+	(vn_nary_op_insert): Add return type. Modify to deal with value
+	ids.
+	(vn_phi_insert): Ditto.
+	(visit_unary_op): Update for callee changes.
+	(visit_binary_op): Ditto.
+	(visit_reference_op_load): Ditto.
+	(visit_reference_op_store): Ditto.
+	(init_scc_vn): Init next_value_id, constant_to_value_id and constant_value_ids.
+	(free_scc_vn): Free them.
+	(set_hashtable_value_ids): New function.
+	(run_scc_vn): Use it.
+	(get_max_value_id): New function.
+	(get_next_value_id): Ditto.
+	(expressions_equal_p): Moved from tree-vn.c
+	(sort_vuses): Ditto.
+	(sort_vuses_heap): Ditto.
+	* tree-ssa-sccvn.h: Structures moved from tree-ssa-sccvn.c (noted
+	above).
+	
 2008-07-05  Andrew Pinski  <andrew_pinski@playstation.sony.com>
 
 	* fold-const.c (fold_convert_const): Treat OFFSET_TYPE the same as
diff -r 2325330ce9ed gcc/Makefile.in
--- a/gcc/Makefile.in	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/Makefile.in	Sun Jul 06 15:28:19 2008 -0400
@@ -1222,7 +1222,6 @@
 	tree-vect-patterns.o \
 	tree-vect-transform.o \
 	tree-vectorizer.o \
-	tree-vn.o \
 	tree-vrp.o \
 	tree.o \
 	value-prof.o \
@@ -2108,16 +2107,14 @@
    $(SYSTEM_H) $(TREE_H) $(GGC_H) $(DIAGNOSTIC_H) $(TIMEVAR_H) \
    $(TM_H) coretypes.h $(TREE_DUMP_H) tree-pass.h $(FLAGS_H) $(CFGLOOP_H) \
    alloc-pool.h $(BASIC_BLOCK_H) bitmap.h $(HASHTAB_H) $(TREE_GIMPLE_H) \
-   $(TREE_INLINE_H) tree-iterator.h tree-ssa-sccvn.h $(PARAMS_H)
+   $(TREE_INLINE_H) tree-iterator.h tree-ssa-sccvn.h $(PARAMS_H) \
+   $(DBGCNT_H)
 tree-ssa-sccvn.o : tree-ssa-sccvn.c $(TREE_FLOW_H) $(CONFIG_H) \
    $(SYSTEM_H) $(TREE_H) $(GGC_H) $(DIAGNOSTIC_H) $(TIMEVAR_H) \
    $(TM_H) coretypes.h $(TREE_DUMP_H) tree-pass.h $(FLAGS_H) $(CFGLOOP_H) \
    alloc-pool.h $(BASIC_BLOCK_H) bitmap.h $(HASHTAB_H) $(TREE_GIMPLE_H) \
    $(TREE_INLINE_H) tree-iterator.h tree-ssa-propagate.h tree-ssa-sccvn.h \
    $(PARAMS_H)
-tree-vn.o : tree-vn.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(GGC_H) \
-   $(TREE_H) $(TREE_FLOW_H) $(HASHTAB_H) langhooks.h tree-pass.h \
-   $(TREE_DUMP_H) $(DIAGNOSTIC_H) tree-ssa-sccvn.h
 tree-vrp.o : tree-vrp.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(TREE_H) \
    $(TREE_FLOW_H) tree-pass.h $(TREE_DUMP_H) $(DIAGNOSTIC_H) $(GGC_H) \
    $(BASIC_BLOCK_H) tree-ssa-propagate.h $(FLAGS_H) $(TREE_DUMP_H) \
diff -r 2325330ce9ed gcc/dbgcnt.def
--- a/gcc/dbgcnt.def	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/dbgcnt.def	Sun Jul 06 15:28:19 2008 -0400
@@ -166,6 +166,7 @@
 DEBUG_COUNTER (local_alloc_for_sched)
 DEBUG_COUNTER (postreload_cse)
 DEBUG_COUNTER (pre_insn)
+DEBUG_COUNTER (treepre_insert)
 DEBUG_COUNTER (sched2_func)
 DEBUG_COUNTER (sched_block)
 DEBUG_COUNTER (sched_func)
@@ -174,4 +175,3 @@
 DEBUG_COUNTER (sms_sched_loop)
 DEBUG_COUNTER (split_for_sched2)
 DEBUG_COUNTER (tail_call)
-
diff -r 2325330ce9ed gcc/testsuite/gcc.c-torture/compile/20080704-1.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gcc/testsuite/gcc.c-torture/compile/20080704-1.c	Sun Jul 06 15:28:19 2008 -0400
@@ -0,0 +1,43 @@
+/* This code used to crash fold_convert due to PRE
+   wanting to fold_convert from a REA_TYPE to an INTEGER_TYPE.  */
+typedef unsigned int uint32_t;
+union double_union
+{
+  double d;
+  uint32_t i[2];
+};
+struct _Jv_reent
+{
+  int _errno;
+};
+_Jv_strtod_r (struct _Jv_reent *ptr, char **se)
+{
+  int bb2, sign;
+  double aadj, aadj1, adj;
+  unsigned long y, z;
+  union double_union rv, *bs = ((void *) 0), *delta = ((void *) 0);
+  {
+  ovfl:ptr->_errno = 34;
+    {
+      (((uint32_t) 0xfffffL) | ((uint32_t) 0x100000L) * (1024 + 1023 - 1));
+    }
+    if ((aadj = _Jv_ratio (delta, bs)) <= 2.)
+      {
+	{
+	  if (aadj < 2. / 2)
+	    aadj = 1. / 2;
+	  aadj1 = -aadj;
+	}
+      }
+    {
+      (rv.i[1]) -= 53 * ((uint32_t) 0x100000L);
+      adj = aadj1 * _Jv_ulp (rv.d);
+      rv.d += adj;
+      if (((rv.i[1]) & ((uint32_t) 0x7ff00000L)) >=
+	  ((uint32_t) 0x100000L) * (1024 + 1023 - 53))
+	{
+	  goto ovfl;
+	}
+    }
+  }
+}
diff -r 2325330ce9ed gcc/testsuite/gcc.dg/tree-ssa/loadpre24.c
--- a/gcc/testsuite/gcc.dg/tree-ssa/loadpre24.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/testsuite/gcc.dg/tree-ssa/loadpre24.c	Sun Jul 06 15:28:19 2008 -0400
@@ -18,7 +18,7 @@
   return d + e;
 }
 
-/* PRE of globals doesn't work.  */
+/* We will move the load of a out of the loop.  */
 
-/* { dg-final { scan-tree-dump-times "Eliminated: 1" 1 "pre" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "Eliminated: 1" 1 "pre" } } */
 /* { dg-final { cleanup-tree-dump "pre" } } */
diff -r 2325330ce9ed gcc/testsuite/gcc.dg/tree-ssa/pr23455.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr23455.c	Sun Jul 06 15:28:19 2008 -0400
@@ -0,0 +1,23 @@
+/* { dg-do compile } */ 
+/* { dg-options "-O2 -fdump-tree-pre-stats" } */
+unsigned long outcnt;
+extern void flush_outbuf(void);
+
+void
+bi_windup(unsigned int *outbuf, unsigned int bi_buf)
+{
+    unsigned long t1 = outcnt;
+    outbuf[t1] = bi_buf;
+
+    unsigned long t2 = outcnt;
+    if (t2 == 16384)
+      flush_outbuf();
+
+    unsigned long t3 = outcnt;
+    outbuf[t3] = bi_buf;
+}
+/* We should eliminate one load of outcnt, which will in turn let us eliminate
+   one multiply of outcnt which will in turn let us eliminate
+   one add involving outcnt and outbuf.  */
+/* { dg-final { scan-tree-dump-times "Eliminated: 3" 1 "pre"} } */
+/* { dg-final { cleanup-tree-dump "pre" } } */
diff -r 2325330ce9ed gcc/testsuite/gcc.dg/tree-ssa/pr35286.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr35286.c	Sun Jul 06 15:28:19 2008 -0400
@@ -0,0 +1,23 @@
+/* { dg-do compile } */ 
+/* { dg-options "-O2 -fdump-tree-pre-stats" } */
+int g2;
+struct A {
+    int a; int b;
+}g1;
+int foo(int a, int b)
+{
+  if (a > 0)
+    {
+      g1.a = a+ b;
+    }
+  else
+    g1.a = b;
+
+  g2 = a+b;
+
+  return g1.a;
+}
+/* We will eliminate the g1.a from the return statement as fully redundant,
+   and remove one calculation of a + b. */
+/* { dg-final { scan-tree-dump-times "Eliminated: 2" 1 "pre"} } */
+/* { dg-final { cleanup-tree-dump "pre" } } */
diff -r 2325330ce9ed gcc/testsuite/gcc.dg/tree-ssa/pr35287.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr35287.c	Sun Jul 06 15:28:19 2008 -0400
@@ -0,0 +1,15 @@
+/* { dg-do compile } */ 
+/* { dg-options "-O2 -fdump-tree-pre-stats" } */
+int *gp;
+int foo(int p)
+{
+  int t = 0;
+  if (p)
+    t = *gp + 1;
+
+  return (*gp + t);
+}
+
+/* We will eliminate one load of gp and one indirect load of *gp. */
+/* { dg-final { scan-tree-dump-times "Eliminated: 2" 1 "pre"} } */
+/* { dg-final { cleanup-tree-dump "pre" } } */
diff -r 2325330ce9ed gcc/tree-flow.h
--- a/gcc/tree-flow.h	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-flow.h	Sun Jul 06 15:28:19 2008 -0400
@@ -1124,9 +1124,10 @@
 extern bool maybe_clean_or_replace_eh_stmt (tree, tree);
 
 /* In tree-ssa-pre.c  */
-void add_to_value (tree, tree);
-void debug_value_expressions (tree);
-void print_value_expressions (FILE *, tree);
+struct pre_expr_d;
+void add_to_value (unsigned int, struct pre_expr_d *);
+void debug_value_expressions (unsigned int);
+void print_value_expressions (FILE *, unsigned int);
 
 
 /* In tree-vn.c  */
diff -r 2325330ce9ed gcc/tree-pretty-print.c
--- a/gcc/tree-pretty-print.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-pretty-print.c	Sun Jul 06 15:28:19 2008 -0400
@@ -952,16 +952,12 @@
 	pp_character (buffer, ')');
       pp_string (buffer, str);
       dump_generic_node (buffer, TREE_OPERAND (node, 1), spc, flags, false);
-
-      if (TREE_CODE (op0) != VALUE_HANDLE)
+      op0 = component_ref_field_offset (node);
+      if (op0 && TREE_CODE (op0) != INTEGER_CST)
 	{
-	  op0 = component_ref_field_offset (node);
-	  if (op0 && TREE_CODE (op0) != INTEGER_CST)
-	    {
-	      pp_string (buffer, "{off: ");
+	  pp_string (buffer, "{off: ");
 	      dump_generic_node (buffer, op0, spc, flags, false);
 	      pp_character (buffer, '}');
-	    }
 	}
       break;
 
@@ -1789,10 +1785,6 @@
       pp_string (buffer, ">");
       break;
 
-    case VALUE_HANDLE:
-      pp_printf (buffer, "VH.%d", VALUE_HANDLE_ID (node));
-      break;
-
     case ASSERT_EXPR:
       pp_string (buffer, "ASSERT_EXPR <");
       dump_generic_node (buffer, ASSERT_EXPR_VAR (node), spc, flags, false);
diff -r 2325330ce9ed gcc/tree-ssa-dom.c
--- a/gcc/tree-ssa-dom.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-ssa-dom.c	Sun Jul 06 15:28:19 2008 -0400
@@ -1163,7 +1163,7 @@
     prev_x = x, x = y, y = prev_x, prev_x = prev_y;
   else if (prev_x && is_gimple_min_invariant (prev_x))
     x = y, y = prev_x, prev_x = prev_y;
-  else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
+  else if (prev_y)
     y = prev_y;
 
   /* After the swapping, we must have one SSA_NAME.  */
@@ -1629,7 +1629,7 @@
      copy of some other variable, use the value or copy stored in
      CONST_AND_COPIES.  */
   val = SSA_NAME_VALUE (op);
-  if (val && val != op && TREE_CODE (val) != VALUE_HANDLE)
+  if (val && val != op)
     {
       tree op_type, val_type;
 
@@ -1969,7 +1969,7 @@
   if (TREE_CODE (lhs) == SSA_NAME)
     {
       temp = SSA_NAME_VALUE (lhs);
-      if (temp && TREE_CODE (temp) != VALUE_HANDLE)
+      if (temp)
 	lhs = temp;
     }
 
diff -r 2325330ce9ed gcc/tree-ssa-pre.c
--- a/gcc/tree-ssa-pre.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-ssa-pre.c	Sun Jul 06 15:28:19 2008 -0400
@@ -46,6 +46,7 @@
 #include "cfgloop.h"
 #include "tree-ssa-sccvn.h"
 #include "params.h"
+#include "dbgcnt.h"
 
 /* TODO:
 
@@ -116,155 +117,178 @@
 
 /* Representations of value numbers:
 
-   Value numbers are represented using the "value handle" approach.
-   This means that each SSA_NAME (and for other reasons to be
-   disclosed in a moment, expression nodes) has a value handle that
-   can be retrieved through get_value_handle.  This value handle *is*
-   the value number of the SSA_NAME.  You can pointer compare the
-   value handles for equivalence purposes.
-
-   For debugging reasons, the value handle is internally more than
-   just a number, it is a VALUE_HANDLE named "VH.x", where x is a
-   unique number for each value number in use.  This allows
-   expressions with SSA_NAMES replaced by value handles to still be
-   pretty printed in a sane way.  They simply print as "VH.3 *
-   VH.5", etc.
-
-   Expression nodes have value handles associated with them as a
-   cache.  Otherwise, we'd have to look them up again in the hash
-   table.  This makes significant difference (factor of two or more) on
-   some test cases.  They can be thrown away after the pass is
-   finished.  */
+   Value numbers are represented by a representative SSA_NAME.  We
+   will create fake SSA_NAME's in situations where we need a
+   representative but do not have one (because it is a complex
+   expression).  In order to facilitate storing the value numbers in
+   bitmaps, and keep the number of wasted SSA_NAME's down, we also
+   associate a value_id with each value number, and create full blown
+   ssa_name's only where we actually need them (IE in operands of
+   existing expressions).  
+
+   Theoretically you could replace all the value_id's with
+   SSA_NAME_VERSION, but this would allocate a large number of
+   SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
+   It would also require an additional indirection at each point we
+   use the value id.  */
 
 /* Representation of expressions on value numbers:
 
-   In some portions of this code, you will notice we allocate "fake"
-   analogues to the expression we are value numbering, and replace the
-   operands with the values of the expression.  Since we work on
-   values, and not just names, we canonicalize expressions to value
-   expressions for use in the ANTIC sets, the EXP_GEN set, etc.
-
-   This is theoretically unnecessary, it just saves a bunch of
-   repeated get_value_handle and find_leader calls in the remainder of
-   the code, trading off temporary memory usage for speed.  The tree
-   nodes aren't actually creating more garbage, since they are
-   allocated in a special pools which are thrown away at the end of
-   this pass.
-
-   All of this also means that if you print the EXP_GEN or ANTIC sets,
-   you will see "VH.5 + VH.7" in the set, instead of "a_55 +
-   b_66" or something.  The only thing that actually cares about
-   seeing the value leaders is phi translation, and it needs to be
-   able to find the leader for a value in an arbitrary block, so this
-   "value expression" form is perfect for it (otherwise you'd do
-   get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
-
+   Expressions consisting of  value numbers are represented the same
+   way as our VN internally represents them, with an additional
+   "pre_expr" wrapping around them in order to facilitate storing all
+   of the expressions in the same sets.  */
 
 /* Representation of sets:
 
-   There are currently two types of sets used, hopefully to be unified soon.
-   The AVAIL sets do not need to be sorted in any particular order,
-   and thus, are simply represented as two bitmaps, one that keeps
-   track of values present in the set, and one that keeps track of
-   expressions present in the set.
-
-   The other sets are represented as doubly linked lists kept in topological
-   order, with an optional supporting bitmap of values present in the
-   set.  The sets represent values, and the elements can be values or
-   expressions.  The elements can appear in different sets, but each
-   element can only appear once in each set.
-
-   Since each node in the set represents a value, we also want to be
-   able to map expression, set pairs to something that tells us
-   whether the value is present is a set.  We use a per-set bitmap for
-   that.  The value handles also point to a linked list of the
-   expressions they represent via a tree annotation.  This is mainly
-   useful only for debugging, since we don't do identity lookups.  */
+   The dataflow sets do not need to be sorted in any particular order
+   for the majority of their lifetime, are simply represented as two
+   bitmaps, one that keeps track of values present in the set, and one
+   that keeps track of expressions present in the set.
+
+   When we need them in topological order, we produce it on demand by
+   transforming the bitmap into an array and sorting it into topo
+   order.  */
+
+/* Type of expression, used to know which member of the PRE_EXPR union
+   is valid.  */
+
+enum pre_expr_kind
+{
+    NAME,
+    NARY,
+    REFERENCE,
+    CONSTANT
+};
+
+typedef union pre_expr_union_d
+{
+  tree name;
+  tree constant;
+  vn_nary_op_t nary;
+  vn_reference_t reference;
+} pre_expr_union;
+
+typedef struct pre_expr_d
+{
+  enum pre_expr_kind kind;
+  unsigned int id;
+  pre_expr_union u;
+} *pre_expr;
+
+#define PRE_EXPR_NAME(e) (e)->u.name
+#define PRE_EXPR_NARY(e) (e)->u.nary
+#define PRE_EXPR_REFERENCE(e) (e)->u.reference
+#define PRE_EXPR_CONSTANT(e) (e)->u.constant
+
+static int
+pre_expr_eq (const void *p1, const void *p2) 
+{
+  const struct pre_expr_d *e1 = (const struct pre_expr_d *) p1;
+  const struct pre_expr_d *e2 = (const struct pre_expr_d *) p2;
+  
+  if (e1->kind != e2->kind)
+    return false;
+  
+  switch (e1->kind) 
+    {
+    case CONSTANT:
+      return operand_equal_p (PRE_EXPR_CONSTANT (e1), PRE_EXPR_CONSTANT (e2),
+			      OEP_PURE_SAME);
+    case NAME:
+      return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
+    case NARY:
+      return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
+    case REFERENCE:
+      return vn_reference_eq (PRE_EXPR_REFERENCE (e1), PRE_EXPR_REFERENCE (e2));
+    default:
+      abort();
+    }
+}
+
+static hashval_t
+pre_expr_hash (const void *p1)
+{
+  const struct pre_expr_d *e = (const struct pre_expr_d *) p1;
+  switch (e->kind) 
+    {
+    case CONSTANT:
+      return iterative_hash_expr (PRE_EXPR_CONSTANT (e), 0);
+    case NAME:
+      return iterative_hash_expr (PRE_EXPR_NAME (e), 0);
+    case NARY:
+      return vn_nary_op_compute_hash (PRE_EXPR_NARY (e));
+    case REFERENCE:
+      return vn_reference_compute_hash (PRE_EXPR_REFERENCE (e));
+    default:
+      abort ();
+    }
+}
 
 
 /* Next global expression id number.  */
 static unsigned int next_expression_id;
 
-typedef VEC(tree, gc) *vuse_vec;
-DEF_VEC_P (vuse_vec);
-DEF_VEC_ALLOC_P (vuse_vec, heap);
-
-static VEC(vuse_vec, heap) *expression_vuses;
-
 /* Mapping from expression to id number we can use in bitmap sets.  */
-static VEC(tree, heap) *expressions;
-
+DEF_VEC_P (pre_expr);
+DEF_VEC_ALLOC_P (pre_expr, heap);
+static VEC(pre_expr, heap) *expressions;
+static htab_t expression_to_id;
+			   
 /* Allocate an expression id for EXPR.  */
 
 static inline unsigned int
-alloc_expression_id (tree expr)
-{
-  tree_ann_common_t ann;
-
-  ann = get_tree_common_ann (expr);
-
+alloc_expression_id (pre_expr expr)
+{
+  void **slot;
   /* Make sure we won't overflow. */
   gcc_assert (next_expression_id + 1 > next_expression_id);
-
-  ann->aux = XNEW (unsigned int);
-  * ((unsigned int *)ann->aux) = next_expression_id++;
-  VEC_safe_push (tree, heap, expressions, expr);
-  VEC_safe_push (vuse_vec, heap, expression_vuses, NULL);
+  expr->id = next_expression_id++;
+  VEC_safe_push (pre_expr, heap, expressions, expr);
+  slot = htab_find_slot (expression_to_id, expr, INSERT);
+  gcc_assert (!*slot);
+  *slot = expr;
   return next_expression_id - 1;
 }
 
 /* Return the expression id for tree EXPR.  */
 
 static inline unsigned int
-get_expression_id (tree expr)
-{
-  tree_ann_common_t ann = tree_common_ann (expr);
-  gcc_assert (ann);
-  gcc_assert (ann->aux);
-
-  return  *((unsigned int *)ann->aux);
+get_expression_id (const pre_expr expr)
+{
+  return expr->id;
+}
+
+static inline unsigned int
+lookup_expression_id (const pre_expr expr) 
+{
+  void **slot;
+
+  slot = htab_find_slot (expression_to_id, expr, NO_INSERT);
+  if (!slot)
+    return 0;
+  return ((pre_expr)*slot)->id;
 }
 
 /* Return the existing expression id for EXPR, or create one if one
    does not exist yet.  */
 
 static inline unsigned int
-get_or_alloc_expression_id (tree expr)
-{
-  tree_ann_common_t ann = tree_common_ann (expr);
-
-  if (ann == NULL || !ann->aux)
+get_or_alloc_expression_id (pre_expr expr)
+{
+  unsigned int id = lookup_expression_id (expr);
+  if (id == 0)
     return alloc_expression_id (expr);
-
-  return get_expression_id (expr);
+  return expr->id = id;
 }
 
 /* Return the expression that has expression id ID */
 
-static inline tree
+static inline pre_expr
 expression_for_id (unsigned int id)
 {
-  return VEC_index (tree, expressions, id);
-}
-
-/* Return the expression vuses for EXPR, if there are any.  */
-
-static inline vuse_vec
-get_expression_vuses (tree expr)
-{
-  unsigned int expr_id = get_or_alloc_expression_id (expr);
-  return VEC_index (vuse_vec, expression_vuses, expr_id);
-}
-
-/* Set the expression vuses for EXPR to VUSES.  */
-
-static inline void
-set_expression_vuses (tree expr, vuse_vec vuses)
-{
-  unsigned int expr_id = get_or_alloc_expression_id (expr);
-  VEC_replace (vuse_vec, expression_vuses, expr_id, vuses);
-}
-
+  return VEC_index (pre_expr, expressions, id);
+}
 
 /* Free the expression id field in all of our expressions,
    and then destroy the expressions array.  */
@@ -272,16 +296,31 @@
 static void
 clear_expression_ids (void)
 {
-  int i;
-  tree expr;
-
-  for (i = 0; VEC_iterate (tree, expressions, i, expr); i++)
-    {
-      free (tree_common_ann (expr)->aux);
-      tree_common_ann (expr)->aux = NULL;
-    }
-  VEC_free (tree, heap, expressions);
-  VEC_free (vuse_vec, heap, expression_vuses);
+  VEC_free (pre_expr, heap, expressions);
+}
+
+static alloc_pool pre_expr_pool;
+
+/* Given an SSA_NAME NAME, get or create a pre_expr to represent it.  */
+
+static pre_expr
+get_or_alloc_expr_for_name (tree name)
+{
+  pre_expr result = (pre_expr) pool_alloc (pre_expr_pool);
+  unsigned int result_id;
+
+  result->kind = NAME;
+  result->id = 0;
+  PRE_EXPR_NAME (result) = name;
+  result_id = lookup_expression_id (result);
+  if (result_id != 0)
+    {
+      pool_free (pre_expr_pool, result);
+      result = expression_for_id (result_id);
+      return result;
+    }
+  get_or_alloc_expression_id (result);
+  return result;
 }
 
 static bool in_fre = false;
@@ -295,7 +334,12 @@
 } *bitmap_set_t;
 
 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi)		\
-  EXECUTE_IF_SET_IN_BITMAP(set->expressions, 0, id, bi)
+  EXECUTE_IF_SET_IN_BITMAP((set)->expressions, 0, (id), (bi))
+
+/* Mapping from value id to expressions with that value_id.  */
+DEF_VEC_P (bitmap_set_t);
+DEF_VEC_ALLOC_P (bitmap_set_t, heap);
+static VEC(bitmap_set_t, heap) *value_expressions;
 
 /* Sets that we need to keep track of.  */
 typedef struct bb_bitmap_sets
@@ -347,6 +391,7 @@
 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
 
+      
 /* Maximal set of values, used to initialize the ANTIC problem, which
    is an intersection problem.  */
 static bitmap_set_t maximal_set;
@@ -376,31 +421,23 @@
 } pre_stats;
 
 static bool do_partial_partial;
-static tree bitmap_find_leader (bitmap_set_t, tree, tree);
-static void bitmap_value_insert_into_set (bitmap_set_t, tree);
-static void bitmap_value_replace_in_set (bitmap_set_t, tree);
+static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int , tree);
+static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
+static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
-static bool bitmap_set_contains_value (bitmap_set_t, tree);
-static void bitmap_insert_into_set (bitmap_set_t, tree);
+static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
+static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
+static void bitmap_insert_into_set_1 (bitmap_set_t, pre_expr, bool);
 static bitmap_set_t bitmap_set_new (void);
-static tree create_expression_by_pieces (basic_block, tree, tree, tree);
-static tree find_or_generate_expression (basic_block, tree, tree, tree);
+static tree create_expression_by_pieces (basic_block, pre_expr, tree, tree,
+					 tree);
+static tree find_or_generate_expression (basic_block, pre_expr, tree, tree);
 
 /* We can add and remove elements and entries to and from sets
    and hash tables, so we use alloc pools for them.  */
 
 static alloc_pool bitmap_set_pool;
-static alloc_pool binary_node_pool;
-static alloc_pool unary_node_pool;
-static alloc_pool reference_node_pool;
-static alloc_pool comparison_node_pool;
 static bitmap_obstack grand_bitmap_obstack;
-
-/* We can't use allocation pools to hold temporary CALL_EXPR objects, since
-   they are not of fixed size.  Instead, use an obstack.  */
-
-static struct obstack temp_call_expr_obstack;
-
 
 /* To avoid adding 300 temporary variables when we only need one, we
    only create one temporary variable, on demand, and build ssa names
@@ -428,16 +465,13 @@
 typedef struct expr_pred_trans_d
 {
   /* The expression.  */
-  tree e;
+  pre_expr e;
 
   /* The predecessor block along which we translated the expression.  */
   basic_block pred;
 
-  /* vuses associated with the expression.  */
-  VEC (tree, gc) *vuses;
-
   /* The value that resulted from the translation.  */
-  tree v;
+  pre_expr v;
 
   /* The hashcode for the expression, pred pair. This is cached for
      speed reasons.  */
@@ -464,50 +498,27 @@
   const_expr_pred_trans_t const ve2 = (const_expr_pred_trans_t) p2;
   basic_block b1 = ve1->pred;
   basic_block b2 = ve2->pred;
-  int i;
-  tree vuse1;
 
   /* If they are not translations for the same basic block, they can't
      be equal.  */
   if (b1 != b2)
     return false;
-
-
-  /* If they are for the same basic block, determine if the
-     expressions are equal.  */
-  if (!expressions_equal_p (ve1->e, ve2->e))
-    return false;
-
-  /* Make sure the vuses are equivalent.  */
-  if (ve1->vuses == ve2->vuses)
-    return true;
-
-  if (VEC_length (tree, ve1->vuses) != VEC_length (tree, ve2->vuses))
-    return false;
-
-  for (i = 0; VEC_iterate (tree, ve1->vuses, i, vuse1); i++)
-    {
-      if (VEC_index (tree, ve2->vuses, i) != vuse1)
-	return false;
-    }
-
-  return true;
+  return pre_expr_eq (ve1->e, ve2->e);
 }
 
 /* Search in the phi translation table for the translation of
-   expression E in basic block PRED with vuses VUSES.
+   expression E in basic block PRED. 
    Return the translated value, if found, NULL otherwise.  */
 
-static inline tree
-phi_trans_lookup (tree e, basic_block pred, VEC (tree, gc) *vuses)
+static inline pre_expr
+phi_trans_lookup (pre_expr e, basic_block pred)
 {
   void **slot;
   struct expr_pred_trans_d ept;
 
   ept.e = e;
   ept.pred = pred;
-  ept.vuses = vuses;
-  ept.hashcode = iterative_hash_expr (e, (unsigned long) pred);
+  ept.hashcode = pre_expr_hash (e);
   slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
 				   NO_INSERT);
   if (!slot)
@@ -517,19 +528,19 @@
 }
 
 
-/* Add the tuple mapping from {expression E, basic block PRED, vuses VUSES} to
+/* Add the tuple mapping from {expression E, basic block PRED} to
    value V, to the phi translation table.  */
 
 static inline void
-phi_trans_add (tree e, tree v, basic_block pred, VEC (tree, gc) *vuses)
+phi_trans_add (pre_expr e, pre_expr v, basic_block pred)
 {
   void **slot;
   expr_pred_trans_t new_pair = XNEW (struct expr_pred_trans_d);
   new_pair->e = e;
   new_pair->pred = pred;
-  new_pair->vuses = vuses;
   new_pair->v = v;
-  new_pair->hashcode = iterative_hash_expr (e, (unsigned long) pred);
+  new_pair->hashcode = pre_expr_hash (e);
+  
   slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
 				   new_pair->hashcode, INSERT);
   if (*slot)
@@ -538,29 +549,27 @@
 }
 
 
-/* Return true if V is a value expression that represents itself.
-   In our world, this is *only* non-value handles.  */
-
-static inline bool
-constant_expr_p (tree v)
-{
-  return TREE_CODE (v) != VALUE_HANDLE &&
-    (TREE_CODE (v) == FIELD_DECL || is_gimple_min_invariant (v));
-}
-
-/* Add expression E to the expression set of value V.  */
-
-void
-add_to_value (tree v, tree e)
-{
-  /* Constants have no expression sets.  */
-  if (constant_expr_p (v))
-    return;
-
-  if (VALUE_HANDLE_EXPR_SET (v) == NULL)
-    VALUE_HANDLE_EXPR_SET (v) = bitmap_set_new ();
-
-  bitmap_insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
+/* Add expression E to the expression set of value id V.  */
+
+void
+add_to_value (unsigned int v, pre_expr e)
+{
+  bitmap_set_t set;
+
+  if (v >= VEC_length (bitmap_set_t, value_expressions))
+    {
+      VEC_safe_grow_cleared (bitmap_set_t, heap, value_expressions,
+			     v + 1);
+    }
+    
+  set = VEC_index (bitmap_set_t, value_expressions, v);
+  if (!set)
+    {
+      set = bitmap_set_new ();
+      VEC_replace (bitmap_set_t, value_expressions, v, set);
+    }
+  
+  bitmap_insert_into_set_1 (set, e, true);
 }
 
 /* Create a new bitmap set and return it.  */
@@ -574,34 +583,59 @@
   return ret;
 }
 
+/* Return the value id for a PRE expression EXPR.  */
+
+static unsigned int
+get_expr_value_id (pre_expr expr)
+{
+  switch (expr->kind)
+    {
+    case CONSTANT:
+      return get_or_alloc_constant_value_id (PRE_EXPR_CONSTANT (expr));
+    case NAME:
+      return VN_INFO (PRE_EXPR_NAME (expr))->value_id;
+    case NARY:
+      return PRE_EXPR_NARY (expr)->value_id;
+    case REFERENCE:
+      return PRE_EXPR_REFERENCE (expr)->value_id;
+    default:
+      gcc_unreachable ();
+    }
+}
+
 /* Remove an expression EXPR from a bitmapped set.  */
 
 static void
-bitmap_remove_from_set (bitmap_set_t set, tree expr)
-{
-  tree val = get_value_handle (expr);
-
-  gcc_assert (val);
-  if (!constant_expr_p (val))
-    {
-      bitmap_clear_bit (set->values, VALUE_HANDLE_ID (val));
+bitmap_remove_from_set (bitmap_set_t set, pre_expr expr)
+{
+  unsigned int val  = get_expr_value_id (expr);
+  if (!value_id_constant_p (val)) 
+    {
+      bitmap_clear_bit (set->values, val);
       bitmap_clear_bit (set->expressions, get_expression_id (expr));
     }
 }
 
+static void
+bitmap_insert_into_set_1 (bitmap_set_t set, pre_expr expr,
+			  bool allow_constants)
+{
+  unsigned int val  = get_expr_value_id (expr);
+  if (allow_constants || !value_id_constant_p (val))
+    {
+      /* We specifically expect this and only this function to be able to
+	 insert constants into a set.  */
+      bitmap_set_bit (set->values, val);
+      bitmap_set_bit (set->expressions, get_or_alloc_expression_id (expr));
+    }
+}
+
 /* Insert an expression EXPR into a bitmapped set.  */
 
 static void
-bitmap_insert_into_set (bitmap_set_t set, tree expr)
-{
-  tree val = get_value_handle (expr);
-
-  gcc_assert (val);
-  if (!constant_expr_p (val))
-    {
-      bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
-      bitmap_set_bit (set->expressions, get_or_alloc_expression_id (expr));
-    }
+bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
+{
+  bitmap_insert_into_set_1 (set, expr, false);
 }
 
 /* Copy a bitmapped set ORIG, into bitmapped set DEST.  */
@@ -624,40 +658,32 @@
 
 
 /* A comparison function for use in qsort to top sort a bitmap set.  Simply
-   subtracts value handle ids, since they are created in topo-order.  */
+   subtracts value ids, since they are created with leaves before
+   their parent users (IE topological order).  */
 
 static int
-vh_compare (const void *pa, const void *pb)
-{
-  const tree vha = get_value_handle (*((const tree *)pa));
-  const tree vhb = get_value_handle (*((const tree *)pb));
-
-  /* This can happen when we constify things.  */
-  if (constant_expr_p (vha))
-    {
-      if (constant_expr_p (vhb))
-	return -1;
-      return -1;
-    }
-  else if (constant_expr_p (vhb))
-    return 1;
-  return VALUE_HANDLE_ID (vha) - VALUE_HANDLE_ID (vhb);
+value_id_compare (const void *pa, const void *pb)
+{
+  const unsigned int vha = get_expr_value_id (*((const pre_expr *)pa));
+  const unsigned int vhb = get_expr_value_id (*((const pre_expr *)pb));
+
+  return vha - vhb;
 }
 
 /* Generate an topological-ordered array of bitmap set SET.  */
 
-static VEC(tree, heap) *
+static VEC(pre_expr, heap) *
 sorted_array_from_bitmap_set (bitmap_set_t set)
 {
   unsigned int i;
   bitmap_iterator bi;
-  VEC(tree, heap) *result = NULL;
+  VEC(pre_expr, heap) *result = NULL;
 
   FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
-    VEC_safe_push (tree, heap, result, expression_for_id (i));
-
-  qsort (VEC_address (tree, result), VEC_length (tree, result),
-	 sizeof (tree), vh_compare);
+    VEC_safe_push (pre_expr, heap, result, expression_for_id (i));
+
+  qsort (VEC_address (pre_expr, result), VEC_length (pre_expr, result),
+	 sizeof (pre_expr), value_id_compare);
 
   return result;
 }
@@ -678,9 +704,9 @@
       bitmap_copy (temp, dest->expressions);
       EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
 	{
-	  tree expr = expression_for_id (i);
-	  tree val = get_value_handle (expr);
-	  if (!bitmap_bit_p (dest->values, VALUE_HANDLE_ID (val)))
+	  pre_expr expr = expression_for_id (i);
+	  unsigned int value_id = get_expr_value_id (expr);
+	  if (!bitmap_bit_p (dest->values, value_id))
 	    bitmap_clear_bit (dest->expressions, i);
 	}
       BITMAP_FREE (temp);
@@ -701,9 +727,9 @@
 
   FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
     {
-      tree expr = expression_for_id (i);
-      tree val = get_value_handle (expr);
-      bitmap_set_bit (result->values, VALUE_HANDLE_ID (val));
+      pre_expr expr = expression_for_id (i);
+      unsigned int value_id = get_expr_value_id (expr);
+      bitmap_set_bit (result->values, value_id);
     }
 
   return result;
@@ -721,30 +747,30 @@
   bitmap_copy (temp, a->expressions);
   EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
     {
-      tree expr = expression_for_id (i);
-      if (bitmap_set_contains_value (b, get_value_handle (expr)))
+      pre_expr expr = expression_for_id (i);
+      if (bitmap_set_contains_value (b, get_expr_value_id (expr)))
 	bitmap_remove_from_set (a, expr);
     }
   BITMAP_FREE (temp);
 }
 
 
-/* Return true if bitmapped set SET contains the value VAL.  */
-
-static bool
-bitmap_set_contains_value (bitmap_set_t set, tree val)
-{
-  if (constant_expr_p (val))
+/* Return true if bitmapped set SET contains the value VALUE_ID.  */
+
+static bool
+bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
+{
+  if (value_id_constant_p (value_id))
     return true;
 
   if (!set || bitmap_empty_p (set->expressions))
     return false;
 
-  return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
+  return bitmap_bit_p (set->values, value_id);
 }
 
 static inline bool
-bitmap_set_contains_expr (bitmap_set_t set, tree expr)
+bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
 {
   return bitmap_bit_p (set->expressions, get_expression_id (expr));
 }
@@ -752,13 +778,14 @@
 /* Replace an instance of value LOOKFOR with expression EXPR in SET.  */
 
 static void
-bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
+bitmap_set_replace_value (bitmap_set_t set, unsigned int lookfor,
+			  const pre_expr expr)
 {
   bitmap_set_t exprset;
   unsigned int i;
   bitmap_iterator bi;
 
-  if (constant_expr_p (lookfor))
+  if (value_id_constant_p (lookfor))
     return;
 
   if (!bitmap_set_contains_value (set, lookfor))
@@ -773,7 +800,7 @@
      5-10x faster than walking the bitmap.  If this is somehow a
      significant lose for some cases, we can choose which set to walk
      based on the set size.  */
-  exprset = VALUE_HANDLE_EXPR_SET (lookfor);
+  exprset = VEC_index (bitmap_set_t, value_expressions, lookfor);
   FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
     {
       if (bitmap_bit_p (set->expressions, i))
@@ -797,9 +824,9 @@
    and add it otherwise.  */
 
 static void
-bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
-{
-  tree val = get_value_handle (expr);
+bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
+{
+  unsigned int val = get_expr_value_id (expr);
 
   if (bitmap_set_contains_value (set, val))
     bitmap_set_replace_value (set, val, expr);
@@ -811,17 +838,89 @@
    SET.  */
 
 static void
-bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
-{
-  tree val = get_value_handle (expr);
-
-  if (constant_expr_p (val))
+bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
+{
+  unsigned int val = get_expr_value_id (expr);
+
+  if (value_id_constant_p (val))
     return;
 
   if (!bitmap_set_contains_value (set, val))
     bitmap_insert_into_set (set, expr);
 }
 
+/* Print out EXPR to outfile.  */
+
+static void
+print_pre_expr (FILE *outfile, const pre_expr expr) 
+{
+  switch (expr->kind)
+    {
+    case CONSTANT:
+      print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr), 0);
+      break;
+    case NAME:
+      print_generic_expr (outfile, PRE_EXPR_NAME (expr), 0);
+      break;
+    case NARY:
+      {
+	unsigned int i;
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	fprintf (outfile, "{%s,", tree_code_name [nary->opcode]);
+	for (i = 0; i < nary->length; i++)
+	  {
+	    print_generic_expr (outfile, nary->op[i], 0);
+	    if (i != (unsigned) nary->length - 1)
+	      fprintf (outfile, ",");
+	  }
+	fprintf (outfile, "}");
+      }
+      break;
+      
+    case REFERENCE:
+      {
+	vn_reference_op_t vro;
+	unsigned int i;
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	fprintf (outfile, "{");
+	for (i = 0;
+	     VEC_iterate (vn_reference_op_s, ref->operands, i, vro); 
+	     i++)
+	  {
+	    if (vro->opcode != SSA_NAME
+		&& TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
+	      fprintf (outfile, "%s ", tree_code_name [vro->opcode]);
+	    if (vro->op0)
+	      {
+		if (vro->op1)
+		  fprintf (outfile, "<");
+		print_generic_expr (outfile, vro->op0, 0);
+		if (vro->op1)
+		  {
+		    fprintf (outfile, ",");
+		    print_generic_expr (outfile, vro->op1, 0);
+		  }
+		if (vro->op1)
+		  fprintf (outfile, ">");
+	      }
+	    if (i != VEC_length (vn_reference_op_s, ref->operands) - 1)
+	      fprintf (outfile, ",");
+	  }
+	fprintf (outfile, "}");
+      }
+      break;
+    }
+}
+void debug_pre_expr (pre_expr);
+
+/* Like print_pre_expr but always prints to stderr.  */
+void
+debug_pre_expr (pre_expr e)
+{
+  print_pre_expr (stderr, e);
+  fprintf (stderr, "\n");
+}
+
 /* Print out SET to OUTFILE.  */
 
 static void
@@ -837,16 +936,14 @@
 
       FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
 	{
-	  tree expr = expression_for_id (i);
+	  const pre_expr expr = expression_for_id (i);
 
 	  if (!first)
 	    fprintf (outfile, ", ");
 	  first = false;
-	  print_generic_expr (outfile, expr, 0);
-
-	  fprintf (outfile, " (");
-	  print_generic_expr (outfile, get_value_handle (expr), 0);
-	  fprintf (outfile, ") ");
+	  print_pre_expr (outfile, expr);
+
+	  fprintf (outfile, " (%04d)", get_expr_value_id (expr));
 	}
     }
   fprintf (outfile, " }\n");
@@ -863,42 +960,141 @@
 /* Print out the expressions that have VAL to OUTFILE.  */
 
 void
-print_value_expressions (FILE *outfile, tree val)
-{
-  if (VALUE_HANDLE_EXPR_SET (val))
+print_value_expressions (FILE *outfile, unsigned int val)
+{
+  bitmap_set_t set = VEC_index (bitmap_set_t, value_expressions, val);
+  if (set)
     {
       char s[10];
-      sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
-      print_bitmap_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
-    }
-}
-
-
-void
-debug_value_expressions (tree val)
+      sprintf (s, "%04d", val);
+      print_bitmap_set (outfile, set, s, 0);
+    }
+}
+
+
+void
+debug_value_expressions (unsigned int val)
 {
   print_value_expressions (stderr, val);
+}
+
+/* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
+   represent it.  */
+
+static pre_expr
+get_or_alloc_expr_for_constant (tree constant)
+{
+  unsigned int result_id;
+  unsigned int value_id;
+  pre_expr newexpr = (pre_expr) pool_alloc (pre_expr_pool);
+  newexpr->kind = CONSTANT;
+  PRE_EXPR_CONSTANT (newexpr) = constant;
+  result_id = lookup_expression_id (newexpr);
+  if (result_id != 0)
+    {
+      pool_free (pre_expr_pool, newexpr);
+      newexpr = expression_for_id (result_id);
+      return newexpr;
+    }
+  value_id = get_or_alloc_constant_value_id (constant);
+  get_or_alloc_expression_id (newexpr);
+  add_to_value (value_id, newexpr);
+  return newexpr;
+}
+
+/* Given a value id V, find the actual tree representing the constant
+   value if there is one, and return it. Return NULL if we can't find
+   a constant.  */
+
+static tree 
+get_constant_for_value_id (unsigned int v)
+{
+  if (value_id_constant_p (v))
+    {
+      unsigned int i;
+      bitmap_iterator bi;
+      bitmap_set_t exprset = VEC_index (bitmap_set_t, value_expressions, v);
+      
+      FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
+	{
+	  pre_expr expr = expression_for_id (i);
+	  if (expr->kind == CONSTANT)
+	    return PRE_EXPR_CONSTANT (expr);
+	}
+    }
+  return NULL;
+}
+
+/* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
+   Currently only supports constants and SSA_NAMES.  */
+static pre_expr
+get_or_alloc_expr_for (tree t)
+{
+  if (TREE_CODE (t) == SSA_NAME)
+    return get_or_alloc_expr_for_name (t);
+  else if (is_gimple_min_invariant (t))
+    return get_or_alloc_expr_for_constant (t);
+  return NULL;
 }
 
 /* Return the folded version of T if T, when folded, is a gimple
    min_invariant.  Otherwise, return T.  */
 
-static tree
-fully_constant_expression (tree t)
-{
-  tree folded;
-  folded = fold (t);
-  if (folded && is_gimple_min_invariant (folded))
-    return folded;
-  return t;
-}
-
-/* Make a temporary copy of a CALL_EXPR object NODE.  */
-
-static tree
-temp_copy_call_expr (tree node)
-{
-  return (tree) obstack_copy (&temp_call_expr_obstack, node, tree_size (node));
+static pre_expr
+fully_constant_expression (pre_expr e)
+{
+  switch (e->kind) 
+    {
+    case CONSTANT:
+      return e;
+    case NARY:
+      {
+	vn_nary_op_t nary = PRE_EXPR_NARY (e);
+	switch (TREE_CODE_CLASS (nary->opcode))
+	  {
+	  case tcc_binary:
+	    {
+	      /* We have to go from trees to pre exprs to value ids to
+		 constants.  */
+	      tree naryop0 = nary->op[0];
+	      tree naryop1 = nary->op[1];
+	      pre_expr rep0 = get_or_alloc_expr_for (naryop0);
+	      pre_expr rep1 = get_or_alloc_expr_for (naryop1);
+	      unsigned int vrep0 = get_expr_value_id (rep0);
+	      unsigned int vrep1 = get_expr_value_id (rep1);	      
+	      tree const0 = get_constant_for_value_id (vrep0);
+	      tree const1 = get_constant_for_value_id (vrep1);
+	      tree result = NULL;
+	      if (const0 && const1)
+		result = fold_binary (nary->opcode, nary->type, const0,
+				      const1);
+	      if (result && is_gimple_min_invariant (result))
+		return get_or_alloc_expr_for_constant (result);
+	      return e;
+	    }
+	  case tcc_unary:
+	    {
+	    /* We have to go from trees to pre exprs to value ids to
+	       constants.  */
+	      tree naryop0 = nary->op[0];
+	      pre_expr rep0 = get_or_alloc_expr_for (naryop0);
+	      unsigned int vrep0 = get_expr_value_id (rep0);
+	      tree const0 = get_constant_for_value_id (vrep0);
+	      tree result = NULL;
+	      if (const0)
+		result = fold_unary (nary->opcode, nary->type, const0);
+	      if (result && is_gimple_min_invariant (result))
+		return get_or_alloc_expr_for_constant (result);
+	      return e;
+	    }
+	  default:
+	    return e;
+	  }
+      }
+    default:
+      return e;
+    }
+  return e;
 }
 
 /* Translate the vuses in the VUSES vector backwards through phi nodes
@@ -949,44 +1145,155 @@
    SET2.  This is used to avoid making a set consisting of the union
    of PA_IN and ANTIC_IN during insert.  */
 
-static inline tree
-find_leader_in_sets (tree expr, bitmap_set_t set1, bitmap_set_t set2)
-{
-  tree result;
-
-  result = bitmap_find_leader (set1, expr, NULL_TREE);
+static inline pre_expr
+find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2)
+{
+  pre_expr result;
+
+  result = bitmap_find_leader (set1, val, NULL_TREE);
   if (!result && set2)
-    result = bitmap_find_leader (set2, expr, NULL_TREE);
+    result = bitmap_find_leader (set2, val, NULL_TREE);
   return result;
 }
 
+/* Get the tree type for our PRE expression e.  */
+
+static tree
+get_expr_type (const pre_expr e) 
+{
+  switch (e->kind) 
+    {
+    case NAME:
+      return TREE_TYPE (PRE_EXPR_NAME (e));
+    case CONSTANT:
+      return TREE_TYPE (PRE_EXPR_CONSTANT (e));
+    case REFERENCE:
+      {
+	vn_reference_op_t vro;
+
+	gcc_assert (PRE_EXPR_REFERENCE (e)->operands);
+	vro = VEC_index (vn_reference_op_s,
+			 PRE_EXPR_REFERENCE (e)->operands,
+			 0);
+	/* We don't store type along with COMPONENT_REF because it is
+	   always the same as FIELD_DECL's type.  */
+	if (!vro->type)
+	  {
+	    gcc_assert (vro->opcode == COMPONENT_REF);
+	    return TREE_TYPE (vro->op0);
+	  }
+	return vro->type;
+      }
+
+    case NARY:
+      return PRE_EXPR_NARY (e)->type;
+    }
+  gcc_unreachable();
+}
+
+/* Get a representative SSA_NAME for a given expression.
+   Since all of our sub-expressions are treated as values, we require
+   them to be SSA_NAME's for simplicity.
+   Prior versions of GVNPRE used to use "value handles" here, so that
+   an expression would be VH.11 + VH.10 instead of d_3 + e_6.  In
+   either case, the operands are really values (IE we do not expect
+   them to be usable without finding leaders).  */
+
+static tree
+get_representative_for (const pre_expr e) 
+{
+  tree exprtype;
+  tree name;
+  unsigned int value_id = get_expr_value_id (e);
+
+  switch (e->kind)
+    {
+    case NAME:
+      return PRE_EXPR_NAME (e);
+    case CONSTANT:
+    case NARY:      
+    case REFERENCE:
+      {
+	/* Go through all of the expressions representing this value
+	   and pick out an SSA_NAME.  */
+	unsigned int i;
+	bitmap_iterator bi;
+	bitmap_set_t exprs = VEC_index (bitmap_set_t, value_expressions,
+					value_id);
+	FOR_EACH_EXPR_ID_IN_SET (exprs, i, bi)
+	  {
+	    pre_expr rep = expression_for_id (i);
+	    if (rep->kind == NAME)
+	      return PRE_EXPR_NAME (rep);
+	  }
+      }
+      break;
+    }
+  /* If we reached here we couldn't find an SSA_NAME.  This can
+     happen when we've discovered a value that has never appeared in
+     the program as set to an SSA_NAME, most likely as the result of
+     phi translation.  */
+  if (dump_file)
+    {
+      fprintf (dump_file,
+	       "Could not find SSA_NAME representative for expression:");
+      print_pre_expr (dump_file, e);
+      fprintf (dump_file, "\n");
+    }
+  
+  exprtype = get_expr_type (e);
+  
+  /* Build and insert the assignment of the end result to the temporary
+     that we will return.  */
+  if (!pretemp || exprtype != TREE_TYPE (pretemp))
+    {
+      pretemp = create_tmp_var (exprtype, "pretmp");
+      get_var_ann (pretemp);
+    }
+
+  name = make_ssa_name (pretemp, build_empty_stmt ());
+  VN_INFO_GET (name)->value_id = value_id;
+  if (e->kind == CONSTANT)
+    VN_INFO (name)->valnum = PRE_EXPR_CONSTANT (e);
+  else
+    VN_INFO (name)->valnum = name;
+
+  add_to_value (value_id, get_or_alloc_expr_for_name (name));
+  if (dump_file)
+    {
+      fprintf (dump_file, "Created SSA_NAME representative ");
+      print_generic_expr (dump_file, name, 0);
+      fprintf (dump_file, " for expression:");
+      print_pre_expr (dump_file, e);
+      fprintf (dump_file, "\n");
+    }
+
+  return name;  
+}
+
+	    
+
+      
 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
    the phis in PRED.  SEEN is a bitmap saying which expression we have
    translated since we started translation of the toplevel expression.
    Return NULL if we can't find a leader for each part of the
    translated expression.  */
 
-static tree
-phi_translate_1 (tree expr, bitmap_set_t set1, bitmap_set_t set2,
+static pre_expr
+phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
 		 basic_block pred, basic_block phiblock, bitmap seen)
 {
-  tree phitrans = NULL;
-  tree oldexpr = expr;
-
-  if (expr == NULL)
+  pre_expr oldexpr = expr;
+  pre_expr phitrans;
+  
+  if (!expr)
     return NULL;
-
-  if (constant_expr_p (expr))
+  
+  if (value_id_constant_p (get_expr_value_id (expr)))
     return expr;
-
-  /* Phi translations of a given expression don't change.  */
-  if (EXPR_P (expr) || GIMPLE_STMT_P (expr))
-    {
-      phitrans = phi_trans_lookup (expr, pred, get_expression_vuses (expr));
-    }
-  else
-    phitrans = phi_trans_lookup (expr, pred, NULL);
-
+  
+  phitrans = phi_trans_lookup (expr, pred);
   if (phitrans)
     return phitrans;
 
@@ -1000,317 +1307,223 @@
       bitmap_set_bit (seen, expr_id);
     }
 
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_expression:
-      return NULL;
-
-    case tcc_vl_exp:
-      {
-	if (TREE_CODE (expr) != CALL_EXPR)
-	  return NULL;
-	else
-	  {
-	    tree oldfn = CALL_EXPR_FN (expr);
-	    tree oldsc = CALL_EXPR_STATIC_CHAIN (expr);
-	    tree newfn, newsc = NULL;
-	    tree newexpr = NULL_TREE;
-	    bool invariantarg = false;
-	    int i, nargs;
-	    VEC (tree, gc) *vuses = get_expression_vuses (expr);
-	    VEC (tree, gc) *tvuses;
-
-	    newfn = phi_translate_1 (find_leader_in_sets (oldfn, set1, set2),
-				     set1, set2, pred, phiblock, seen);
-	    if (newfn == NULL)
-	      return NULL;
-	    if (newfn != oldfn)
-	      {
-		newexpr = temp_copy_call_expr (expr);
-		CALL_EXPR_FN (newexpr) = get_value_handle (newfn);
-	      }
-	    if (oldsc)
-	      {
-		newsc = phi_translate_1 (find_leader_in_sets (oldsc, set1, set2),
-					 set1, set2, pred, phiblock, seen);
-		if (newsc == NULL)
+  switch (expr->kind)
+    {
+      /* Constants contain no values that need translation.  */
+    case CONSTANT:
+      return expr;
+      
+    case NARY:
+      {
+	unsigned int i;
+	bool changed = false;	
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	struct vn_nary_op_s newnary;
+	/* The NARY structure is only guaranteed to have been
+	   allocated to the nary->length operands.  */
+	memcpy (&newnary, nary, (sizeof (struct vn_nary_op_s)
+				 - sizeof (tree) * (4 - nary->length)));
+	
+	for (i = 0; i < newnary.length; i++)
+	  {
+	    if (TREE_CODE (newnary.op[i]) != SSA_NAME)
+	      continue;
+	    else
+	      {
+		unsigned int op_val_id = VN_INFO (newnary.op[i])->value_id;
+		pre_expr leader = find_leader_in_sets (op_val_id, set1, set2);
+		pre_expr result = phi_translate_1 (leader, set1, set2,
+						   pred, phiblock, seen);
+		if (result && result != leader)
+		  {
+		    tree name = get_representative_for (result);
+		    if (!name)
+		      return NULL;
+		    newnary.op[i] = name;
+		  }
+		else if (!result)
 		  return NULL;
-		if (newsc != oldsc)
-		  {
-		    if (!newexpr)
-		      newexpr = temp_copy_call_expr (expr);
-		    CALL_EXPR_STATIC_CHAIN (newexpr) = get_value_handle (newsc);
-		  }
-	      }
-
-	    /* phi translate the argument list piece by piece.  */
-	    nargs = call_expr_nargs (expr);
-	    for (i = 0; i < nargs; i++)
-	      {
-		tree oldval = CALL_EXPR_ARG (expr, i);
-		tree newval;
-		if (oldval)
-		  {
-		    /* This may seem like a weird place for this
-		       check, but it's actually the easiest place to
-		       do it.  We can't do it lower on in the
-		       recursion because it's valid for pieces of a
-		       component ref to be of AGGREGATE_TYPE, as long
-		       as the outermost one is not.
-		       To avoid *that* case, we have a check for
-		       AGGREGATE_TYPE_P in insert_aux.  However, that
-		       check will *not* catch this case because here
-		       it occurs in the argument list.  */
-		    if (AGGREGATE_TYPE_P (TREE_TYPE (oldval)))
-		      return NULL;
-		    oldval = find_leader_in_sets (oldval, set1, set2);
-		    newval = phi_translate_1 (oldval, set1, set2, pred,
-					    phiblock, seen);
-		    if (newval == NULL)
-		      return NULL;
-		    if (newval != oldval)
-		      {
-			invariantarg |= is_gimple_min_invariant (newval);
-			if (!newexpr)
-			  newexpr = temp_copy_call_expr (expr);
-			CALL_EXPR_ARG (newexpr, i) = get_value_handle (newval);
-		      }
-		  }
-	      }
-
-	    /* In case of new invariant args we might try to fold the call
-	       again.  */
-	    if (invariantarg && !newsc)
-	      {
-		tree tmp1 = build_call_array (TREE_TYPE (expr),
-					      newfn, call_expr_nargs (newexpr),
-					      CALL_EXPR_ARGP (newexpr));
-		tree tmp2 = fold (tmp1);
-		if (tmp2 != tmp1)
-		  {
-		    STRIP_TYPE_NOPS (tmp2);
-		    if (is_gimple_min_invariant (tmp2))
-		      return tmp2;
-		  }
-	      }
-
-	    tvuses = translate_vuses_through_block (vuses, phiblock, pred);
-	    if (vuses != tvuses && ! newexpr)
-	      newexpr = temp_copy_call_expr (expr);
-
-	    if (newexpr)
-	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add_with_vuses (newexpr, tvuses);
-		expr = newexpr;
-		set_expression_vuses (newexpr, tvuses);
-	      }
-	    phi_trans_add (oldexpr, expr, pred, tvuses);
-	  }
-      }
-      return expr;
-
-    case tcc_declaration:
-      {
-	VEC (tree, gc) * oldvuses = NULL;
-	VEC (tree, gc) * newvuses = NULL;
-
-	oldvuses = get_expression_vuses (expr);
-	if (oldvuses)
-	  newvuses = translate_vuses_through_block (oldvuses, phiblock,
-						    pred);
-
-	if (oldvuses != newvuses)
-	  {
-	    vn_lookup_or_add_with_vuses (expr, newvuses);
-	    set_expression_vuses (expr, newvuses);
-	  }
-	phi_trans_add (oldexpr, expr, pred, newvuses);
-      }
-      return expr;
-
-    case tcc_reference:
-      {
-	tree oldop0 = TREE_OPERAND (expr, 0);
-	tree oldop1 = NULL;
-	tree newop0;
-	tree newop1 = NULL;
-	tree oldop2 = NULL;
-	tree newop2 = NULL;
-	tree oldop3 = NULL;
-	tree newop3 = NULL;
-	tree newexpr;
-	VEC (tree, gc) * oldvuses = NULL;
-	VEC (tree, gc) * newvuses = NULL;
-
-	if (TREE_CODE (expr) != INDIRECT_REF
-	    && TREE_CODE (expr) != COMPONENT_REF
-	    && TREE_CODE (expr) != ARRAY_REF)
-	  return NULL;
-
-	oldop0 = find_leader_in_sets (oldop0, set1, set2);
-	newop0 = phi_translate_1 (oldop0, set1, set2, pred, phiblock, seen);
-	if (newop0 == NULL)
-	  return NULL;
-
-	if (TREE_CODE (expr) == ARRAY_REF)
-	  {
-	    oldop1 = TREE_OPERAND (expr, 1);
-	    oldop1 = find_leader_in_sets (oldop1, set1, set2);
-	    newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
-
-	    if (newop1 == NULL)
-	      return NULL;
-
-	    oldop2 = TREE_OPERAND (expr, 2);
-	    if (oldop2)
-	      {
-		oldop2 = find_leader_in_sets (oldop2, set1, set2);
-		newop2 = phi_translate_1 (oldop2, set1, set2, pred, phiblock, seen);
-
-		if (newop2 == NULL)
+		
+		changed |= newnary.op[i] != nary->op[i];
+	      }
+	  }
+	if (changed)
+	  {
+	    pre_expr constant;
+	    
+	    tree result = vn_nary_op_lookup_pieces (newnary.length,
+						    newnary.opcode,
+						    newnary.type,
+						    newnary.op[0],
+						    newnary.op[1],
+						    newnary.op[2],
+						    newnary.op[3],
+						    &nary);
+	    unsigned int new_val_id;
+	    
+	    expr = (pre_expr) pool_alloc (pre_expr_pool);
+	    expr->kind = NARY;
+	    expr->id = 0;
+	    if (result && is_gimple_min_invariant (result))
+	      return get_or_alloc_expr_for_constant (result);
+
+
+	    if (nary)
+	      {
+		PRE_EXPR_NARY (expr) = nary;
+		constant = fully_constant_expression (expr);
+		if (constant != expr)
+		  return constant;
+
+		new_val_id = nary->value_id;
+		get_or_alloc_expression_id (expr);
+	      }
+	    else
+	      {
+		new_val_id = get_next_value_id ();
+		VEC_safe_grow_cleared (bitmap_set_t, heap,
+				       value_expressions,
+				       get_max_value_id() + 1);
+		nary = vn_nary_op_insert_pieces (newnary.length,
+						 newnary.opcode,
+						 newnary.type,
+						 newnary.op[0],
+						 newnary.op[1],
+						 newnary.op[2],
+						 newnary.op[3],
+						 result, new_val_id);
+		PRE_EXPR_NARY (expr) = nary;
+		constant = fully_constant_expression (expr);
+		if (constant != expr)
+		  return constant;
+		get_or_alloc_expression_id (expr);
+		add_to_value (new_val_id, expr);
+	      }
+	  }
+	phi_trans_add (oldexpr, expr, pred);
+	return expr;
+      }
+      break;
+    case REFERENCE:
+      {
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	VEC (vn_reference_op_s, heap) *operands = ref->operands;
+	VEC (tree, gc) *vuses = ref->vuses;
+	VEC (tree, gc) *newvuses = vuses;	
+	VEC (vn_reference_op_s, heap) *newoperands = NULL;
+	bool changed = false;
+	unsigned int i;
+	vn_reference_op_t operand;
+	vn_reference_t newref;
+
+	for (i = 0; VEC_iterate (vn_reference_op_s, operands, i, operand); i++)
+	  {
+	    pre_expr opresult;
+	    pre_expr leader;
+	    tree oldop0 = operand->op0;
+	    tree oldop1 = operand->op1;
+	    tree op0 = oldop0;
+	    tree op1 = oldop1;
+	    tree type = operand->type;
+	    vn_reference_op_s newop = *operand;
+
+	    if (op0 && TREE_CODE (op0) == SSA_NAME)
+	      {
+		unsigned int op_val_id = VN_INFO (op0)->value_id;
+		leader = find_leader_in_sets (op_val_id, set1, set2);
+		opresult = phi_translate_1 (leader, set1, set2,
+					    pred, phiblock, seen);
+		if (opresult && opresult != leader)
+		  {
+		    tree name = get_representative_for (opresult);
+		    if (!name)
+		      return NULL;		    
+		    op0 = name;
+		  }
+		else if (!opresult)
 		  return NULL;
 	      }
-	    oldop3 = TREE_OPERAND (expr, 3);
-	    if (oldop3)
-	      {
-		oldop3 = find_leader_in_sets (oldop3, set1, set2);
-		newop3 = phi_translate_1 (oldop3, set1, set2, pred, phiblock, seen);
-
-		if (newop3 == NULL)
+	    changed |= op0 != oldop0;
+	    
+	    if (op1 && TREE_CODE (op1) == SSA_NAME)
+	      {
+		unsigned int op_val_id = VN_INFO (op1)->value_id;
+		leader = find_leader_in_sets (op_val_id, set1, set2);
+		opresult = phi_translate_1 (leader, set1, set2,
+					    pred, phiblock, seen);
+		if (opresult && opresult != leader)
+		  {
+		    tree name = get_representative_for (opresult);
+		    if (!name)
+		      return NULL;		    
+		    op1 = name;
+		  }
+		else if (!opresult)
 		  return NULL;
 	      }
-	  }
-
-	oldvuses = get_expression_vuses (expr);
-	if (oldvuses)
-	  newvuses = translate_vuses_through_block (oldvuses, phiblock,
-						    pred);
-
-	if (newop0 != oldop0 || newvuses != oldvuses
-	    || newop1 != oldop1
-	    || newop2 != oldop2
-	    || newop3 != oldop3)
-	  {
-	    tree t;
-
-	    newexpr = (tree) pool_alloc (reference_node_pool);
-	    memcpy (newexpr, expr, tree_size (expr));
-	    TREE_OPERAND (newexpr, 0) = get_value_handle (newop0);
-	    if (TREE_CODE (expr) == ARRAY_REF)
-	      {
-		TREE_OPERAND (newexpr, 1) = get_value_handle (newop1);
-		if (newop2)
-		  TREE_OPERAND (newexpr, 2) = get_value_handle (newop2);
-		if (newop3)
-		  TREE_OPERAND (newexpr, 3) = get_value_handle (newop3);
-	      }
-
-	    t = fully_constant_expression (newexpr);
-
-	    if (t != newexpr)
-	      {
-		pool_free (reference_node_pool, newexpr);
-		newexpr = t;
+	    changed |= op1 != oldop1;
+	    if (!newoperands)
+	      newoperands = VEC_copy (vn_reference_op_s, heap, operands);
+	    /* We may have changed from an SSA_NAME to a constant */
+	    if (newop.opcode == SSA_NAME && TREE_CODE (op0) != SSA_NAME)
+	      newop.opcode = TREE_CODE (op0);
+	    newop.type = type;
+	    newop.op0 = op0;
+	    newop.op1 = op1;
+	    VEC_replace (vn_reference_op_s, newoperands, i, &newop);
+	  }
+	
+	newvuses = translate_vuses_through_block (vuses, phiblock, pred);
+	changed |= newvuses != vuses;
+
+	if (changed)
+	  {
+	    tree result = vn_reference_lookup_pieces (newvuses,
+						      newoperands,
+						      &newref);
+	    unsigned int new_val_id;
+
+	    if (newref)
+	      VEC_free (vn_reference_op_s, heap, newoperands);
+	    
+	    if (result && is_gimple_min_invariant (result))
+	      return get_or_alloc_expr_for_constant (result);
+
+	    expr = (pre_expr) pool_alloc (pre_expr_pool);
+	    expr->kind = REFERENCE;
+	    expr->id = 0;
+
+	    if (newref)
+	      {
+		PRE_EXPR_REFERENCE (expr) = newref;
+		new_val_id = newref->value_id;
+		get_or_alloc_expression_id (expr);
 	      }
 	    else
 	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add_with_vuses (newexpr, newvuses);
-		set_expression_vuses (newexpr, newvuses);
-	      }
-	    expr = newexpr;
-	  }
-	phi_trans_add (oldexpr, expr, pred, newvuses);
-      }
-      return expr;
-      break;
-
-    case tcc_binary:
-    case tcc_comparison:
-      {
-	tree oldop1 = TREE_OPERAND (expr, 0);
-	tree oldval1 = oldop1;
-	tree oldop2 = TREE_OPERAND (expr, 1);
-	tree oldval2 = oldop2;
-	tree newop1;
-	tree newop2;
-	tree newexpr;
-
-	oldop1 = find_leader_in_sets (oldop1, set1, set2);
-	newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
-	if (newop1 == NULL)
-	  return NULL;
-
-	oldop2 = find_leader_in_sets (oldop2, set1, set2);
-	newop2 = phi_translate_1 (oldop2, set1, set2, pred, phiblock, seen);
-	if (newop2 == NULL)
-	  return NULL;
-	if (newop1 != oldop1 || newop2 != oldop2)
-	  {
-	    tree t;
-	    newexpr = (tree) pool_alloc (binary_node_pool);
-	    memcpy (newexpr, expr, tree_size (expr));
-	    TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldval1 : get_value_handle (newop1);
-	    TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldval2 : get_value_handle (newop2);
-	    t = fully_constant_expression (newexpr);
-	    if (t != newexpr)
-	      {
-		pool_free (binary_node_pool, newexpr);
-		newexpr = t;
-	      }
-	    else
-	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add (newexpr);
-	      }
-	    expr = newexpr;
-	  }
-	phi_trans_add (oldexpr, expr, pred, NULL);
-      }
-      return expr;
-
-    case tcc_unary:
-      {
-	tree oldop1 = TREE_OPERAND (expr, 0);
-	tree newop1;
-	tree newexpr;
-
-	oldop1 = find_leader_in_sets (oldop1, set1, set2);
-	newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
-	if (newop1 == NULL)
-	  return NULL;
-	if (newop1 != oldop1)
-	  {
-	    tree t;
-	    newexpr = (tree) pool_alloc (unary_node_pool);
-	    memcpy (newexpr, expr, tree_size (expr));
-	    TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
-	    t = fully_constant_expression (newexpr);
-	    if (t != newexpr)
-	      {
-		pool_free (unary_node_pool, newexpr);
-		newexpr = t;
-	      }
-	    else
-	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add (newexpr);
-	      }
-	    expr = newexpr;
-	  }
-	phi_trans_add (oldexpr, expr, pred, NULL);
-      }
-      return expr;
-
-    case tcc_exceptional:
+		new_val_id = get_next_value_id ();
+		VEC_safe_grow_cleared (bitmap_set_t, heap, value_expressions,
+				       get_max_value_id() + 1);
+		newref = vn_reference_insert_pieces (newvuses,
+						     newoperands, 
+						     result, new_val_id);
+		PRE_EXPR_REFERENCE (expr) = newref;
+		get_or_alloc_expression_id (expr);
+		add_to_value (new_val_id, expr);
+	      }    
+	  }
+	phi_trans_add (oldexpr, expr, pred);
+	return expr;
+      }
+      break;
+    case NAME:
       {
 	tree phi = NULL;
 	edge e;
 	tree def_stmt;
-	gcc_assert (TREE_CODE (expr) == SSA_NAME);
-
-	def_stmt = SSA_NAME_DEF_STMT (expr);
+	tree name = PRE_EXPR_NAME (expr);
+
+	def_stmt = SSA_NAME_DEF_STMT (name);
 	if (TREE_CODE (def_stmt) == PHI_NODE
 	    && bb_for_stmt (def_stmt) == phiblock)
 	  phi = def_stmt;
@@ -1320,18 +1533,18 @@
 	e = find_edge (pred, bb_for_stmt (phi));
 	if (e)
 	  {
-	    tree val;
 	    tree def = PHI_ARG_DEF (phi, e->dest_idx);
-
+	    pre_expr newexpr;
+
+	    /* Handle constant. */
 	    if (is_gimple_min_invariant (def))
-	      return def;
+	      return get_or_alloc_expr_for_constant (def);
 
 	    if (TREE_CODE (def) == SSA_NAME && ssa_undefined_value_p (def))
 	      return NULL;
 
-	    val = get_value_handle (def);
-	    gcc_assert (val);
-	    return def;
+	    newexpr = get_or_alloc_expr_for_name (def);
+	    return newexpr;
 	  }
       }
       return expr;
@@ -1346,8 +1559,8 @@
    Return NULL if we can't find a leader for each part of the
    translated expression.  */
 
-static tree
-phi_translate (tree expr, bitmap_set_t set1, bitmap_set_t set2,
+static pre_expr
+phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
 	       basic_block pred, basic_block phiblock)
 {
   bitmap_clear (seen_during_translate);
@@ -1355,7 +1568,7 @@
 			  seen_during_translate);
 }
 
-/* For each expression in SET, translate the value handles through phi nodes
+/* For each expression in SET, translate the values through phi nodes
    in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
    expressions in DEST.  */
 
@@ -1363,8 +1576,8 @@
 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
 		   basic_block phiblock)
 {
-  VEC (tree, heap) *exprs;
-  tree expr;
+  VEC (pre_expr, heap) *exprs;
+  pre_expr expr;
   int i;
 
   if (!phi_nodes (phiblock))
@@ -1374,23 +1587,22 @@
     }
 
   exprs = sorted_array_from_bitmap_set (set);
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
-    {
-      tree translated;
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
+    {
+      pre_expr translated;
       translated = phi_translate (expr, set, NULL, pred, phiblock);
 
       /* Don't add constants or empty translations to the cache, since
 	 we won't look them up that way, or use the result, anyway.  */
-      if (translated && !is_gimple_min_invariant (translated))
-	{
-	  phi_trans_add (expr, translated, pred,
-			 get_expression_vuses (translated));
+      if (translated && !value_id_constant_p (get_expr_value_id (translated)))
+	{
+	  phi_trans_add (expr, translated, pred);
 	}
 
       if (translated != NULL)
 	bitmap_value_insert_into_set (dest, translated);
     }
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
 }
 
 /* Find the leader for a value (i.e., the name representing that
@@ -1398,15 +1610,22 @@
    makes sure the defining statement for the leader dominates it.
    Return NULL if no leader is found.  */
 
-static tree
-bitmap_find_leader (bitmap_set_t set, tree val, tree stmt)
-{
-  if (val == NULL)
-    return NULL;
-
-  if (constant_expr_p (val))
-    return val;
-
+static pre_expr
+bitmap_find_leader (bitmap_set_t set, unsigned int val, tree stmt)
+{
+  if (value_id_constant_p (val))
+    {
+      unsigned int i;
+      bitmap_iterator bi;
+      bitmap_set_t exprset = VEC_index (bitmap_set_t, value_expressions, val);
+
+      FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
+	{
+	  pre_expr expr = expression_for_id (i);
+	  if (expr->kind == CONSTANT)
+	    return expr;
+	}
+    }
   if (bitmap_set_contains_value (set, val))
     {
       /* Rather than walk the entire bitmap of expressions, and see
@@ -1422,15 +1641,17 @@
 	 choose which set to walk based on which set is smaller.  */
       unsigned int i;
       bitmap_iterator bi;
-      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
+      bitmap_set_t exprset = VEC_index (bitmap_set_t, value_expressions, val);
 
       EXECUTE_IF_AND_IN_BITMAP (exprset->expressions,
 				set->expressions, 0, i, bi)
 	{
-	  tree val = expression_for_id (i);
+	  pre_expr val = expression_for_id (i);
+	  /* At the point where stmt is not null, there should always
+	     be an SSA_NAME first in the list of expressions.  */
 	  if (stmt)
 	    {
-	      tree def_stmt = SSA_NAME_DEF_STMT (val);
+	      tree def_stmt = SSA_NAME_DEF_STMT (PRE_EXPR_NAME (val));
 	      if (TREE_CODE (def_stmt) != PHI_NODE
 		  && bb_for_stmt (def_stmt) == bb_for_stmt (stmt)
 		  && stmt_ann (def_stmt)->uid >= stmt_ann (stmt)->uid)
@@ -1450,11 +1671,11 @@
    ANTIC_IN set already.  */
 
 static bool
-value_dies_in_block_x (tree expr, basic_block block)
+value_dies_in_block_x (pre_expr expr, basic_block block)
 {
   int i;
   tree vuse;
-  VEC (tree, gc) *vuses = get_expression_vuses (expr);
+  VEC (tree, gc) *vuses = PRE_EXPR_REFERENCE (expr)->vuses;
 
   /* Conservatively, a value dies if it's vuses are defined in this
      block, unless they come from phi nodes (which are merge operations,
@@ -1472,120 +1693,100 @@
   return false;
 }
 
+
+#define union_contains_value(SET1, SET2, VAL)			\
+  (bitmap_set_contains_value ((SET1), (VAL))			\
+   || ((SET2) && bitmap_set_contains_value ((SET2), (VAL))))
+
+/* Determine if vn_reference_op_t VRO is legal in SET1 U SET2.
+ */
+static bool
+vro_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2,
+		   vn_reference_op_t vro) 
+{
+  if (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME)
+    {
+      struct pre_expr_d temp;
+      temp.kind = NAME;
+      temp.id = 0;
+      PRE_EXPR_NAME (&temp) = vro->op0;
+      temp.id = lookup_expression_id (&temp);
+      if (temp.id == 0)
+	return false;
+      if (!union_contains_value (set1, set2,
+				 get_expr_value_id (&temp)))
+	return false;
+    }
+  if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
+    {
+      struct pre_expr_d temp;
+      temp.kind = NAME;
+      temp.id = 0;
+      PRE_EXPR_NAME (&temp) = vro->op1;
+      temp.id = lookup_expression_id (&temp);
+      if (temp.id == 0)
+	return false;
+      if (!union_contains_value (set1, set2,
+				 get_expr_value_id (&temp)))
+	return false;
+    }
+  return true;
+}
+
 /* Determine if the expression EXPR is valid in SET1 U SET2.
    ONLY SET2 CAN BE NULL.
    This means that we have a leader for each part of the expression
    (if it consists of values), or the expression is an SSA_NAME.
    For loads/calls, we also see if the vuses are killed in this block.
-
-   NB: We never should run into a case where we have SSA_NAME +
-   SSA_NAME or SSA_NAME + value.  The sets valid_in_sets is called on,
-   the ANTIC sets, will only ever have SSA_NAME's or value expressions
-   (IE VALUE1 + VALUE2, *VALUE1, VALUE1 < VALUE2)  */
-
-#define union_contains_value(SET1, SET2, VAL)			\
-  (bitmap_set_contains_value ((SET1), (VAL))			\
-   || ((SET2) && bitmap_set_contains_value ((SET2), (VAL))))
-
-static bool
-valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree expr,
+*/
+
+static bool
+valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr,
 	       basic_block block)
 {
- switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_binary:
-    case tcc_comparison:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	tree op2 = TREE_OPERAND (expr, 1);
-
-	return union_contains_value (set1, set2, op1)
-	  && union_contains_value (set1, set2, op2);
-      }
-
-    case tcc_unary:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	return union_contains_value (set1, set2, op1);
-      }
-
-    case tcc_expression:
-      return false;
-
-    case tcc_vl_exp:
-      {
-	if (TREE_CODE (expr) == CALL_EXPR)
-	  {
-	    tree fn = CALL_EXPR_FN (expr);
-	    tree sc = CALL_EXPR_STATIC_CHAIN (expr);
-	    tree arg;
-	    call_expr_arg_iterator iter;
-
-	    /* Check the non-argument operands first.  */
-	    if (!union_contains_value (set1, set2, fn)
-		|| (sc && !union_contains_value (set1, set2, sc)))
+  switch (expr->kind)
+    {
+    case NAME:
+      return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
+    case NARY:
+      {
+	unsigned int i;
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	for (i = 0; i < nary->length; i++)
+	  {
+	    if (TREE_CODE (nary->op[i]) == SSA_NAME)
+	      {
+		struct pre_expr_d temp;
+		temp.kind = NAME;
+		temp.id = 0;
+		PRE_EXPR_NAME (&temp) = nary->op[i];
+		temp.id = lookup_expression_id (&temp);
+		if (temp.id == 0)
+		  return false;
+		if (!union_contains_value (set1, set2,
+					   get_expr_value_id (&temp)))
+		  return false;
+	      }
+	  }
+	return true;
+      }
+      break;
+    case REFERENCE: 
+      {
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	vn_reference_op_t vro;
+	unsigned int i;
+	
+	for (i = 0; VEC_iterate (vn_reference_op_s, ref->operands, i, vro); i++)
+	  {
+	    if (!vro_valid_in_sets (set1, set2, vro))
 	      return false;
-
-	    /* Now check the operands.  */
-	    FOR_EACH_CALL_EXPR_ARG (arg, iter, expr)
-	      {
-		if (!union_contains_value (set1, set2, arg))
-		  return false;
-	      }
-	    return !value_dies_in_block_x (expr, block);
-	  }
-	return false;
-      }
-
-    case tcc_reference:
-      {
-	if (TREE_CODE (expr) == INDIRECT_REF
-	    || TREE_CODE (expr) == COMPONENT_REF
-	    || TREE_CODE (expr) == ARRAY_REF)
-	  {
-	    tree op0 = TREE_OPERAND (expr, 0);
-	    gcc_assert (is_gimple_min_invariant (op0)
-			|| TREE_CODE (op0) == VALUE_HANDLE);
-	    if (!union_contains_value (set1, set2, op0))
-	      return false;
-	    if (TREE_CODE (expr) == ARRAY_REF)
-	      {
-		tree op1 = TREE_OPERAND (expr, 1);
-		tree op2 = TREE_OPERAND (expr, 2);
-		tree op3 = TREE_OPERAND (expr, 3);
-		gcc_assert (is_gimple_min_invariant (op1)
-			    || TREE_CODE (op1) == VALUE_HANDLE);
-		if (!union_contains_value (set1, set2, op1))
-		  return false;
-		gcc_assert (!op2 || is_gimple_min_invariant (op2)
-			    || TREE_CODE (op2) == VALUE_HANDLE);
-		if (op2
-		    && !union_contains_value (set1, set2, op2))
-		  return false;
-		gcc_assert (!op3 || is_gimple_min_invariant (op3)
-			    || TREE_CODE (op3) == VALUE_HANDLE);
-		if (op3
-		    && !union_contains_value (set1, set2, op3))
-		  return false;
-	    }
-	    return !value_dies_in_block_x (expr, block);
-	  }
-      }
-      return false;
-
-    case tcc_exceptional:
-      {
-	gcc_assert (TREE_CODE (expr) == SSA_NAME);
-	return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
-      }
-
-    case tcc_declaration:
-      return !value_dies_in_block_x (expr, block);
-
-    default:
-      /* No other cases should be encountered.  */
+	  }
+	return !value_dies_in_block_x (expr, block);
+      }
+    default:
       gcc_unreachable ();
-   }
+    }
 }
 
 /* Clean the set of expressions that are no longer valid in SET1 or
@@ -1597,16 +1798,16 @@
 static void
 dependent_clean (bitmap_set_t set1, bitmap_set_t set2, basic_block block)
 {
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set1);
-  tree expr;
-  int i;
-
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (set1);
+  pre_expr expr;
+  int i;
+
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
     {
       if (!valid_in_sets (set1, set2, expr, block))
 	bitmap_remove_from_set (set1, expr);
     }
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
 }
 
 /* Clean the set of expressions that are no longer valid in SET.  This
@@ -1616,16 +1817,16 @@
 static void
 clean (bitmap_set_t set, basic_block block)
 {
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set);
-  tree expr;
-  int i;
-
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (set);
+  pre_expr expr;
+  int i;
+
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
     {
       if (!valid_in_sets (set, NULL, expr, block))
 	bitmap_remove_from_set (set, expr);
     }
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
 }
 
 static sbitmap has_abnormal_preds;
@@ -1967,73 +2168,6 @@
   return changed;
 }
 
-/* Initialize data structures used for ANTIC and AVAIL.  */
-
-static void
-init_antic (void)
-{
-  basic_block bb;
-
-  next_expression_id = 0;
-  expressions = NULL;
-  expression_vuses = NULL;
-
-  postorder = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
-  post_order_compute (postorder, false, false);
-
-  bitmap_obstack_initialize (&grand_bitmap_obstack);
-  obstack_init (&temp_call_expr_obstack);
-  seen_during_translate = BITMAP_ALLOC (&grand_bitmap_obstack);
-
-  bitmap_set_pool = create_alloc_pool ("Bitmap sets",
-					sizeof (struct bitmap_set), 30);
-  binary_node_pool = create_alloc_pool ("Binary tree nodes",
-					tree_code_size (PLUS_EXPR), 30);
-  unary_node_pool = create_alloc_pool ("Unary tree nodes",
-				       tree_code_size (NEGATE_EXPR), 30);
-  reference_node_pool = create_alloc_pool ("Reference tree nodes",
-					   tree_code_size (ARRAY_REF), 30);
-  comparison_node_pool = create_alloc_pool ("Comparison tree nodes",
-					    tree_code_size (EQ_EXPR), 30);
-
-  phi_translate_table = htab_create (5110, expr_pred_trans_hash,
-				     expr_pred_trans_eq, free);
-  maximal_set = in_fre ? NULL : bitmap_set_new ();
-
-  FOR_ALL_BB (bb)
-    {
-      bb->aux = xcalloc (1, sizeof (struct bb_bitmap_sets));
-      EXP_GEN (bb) = bitmap_set_new ();
-      PHI_GEN (bb) = bitmap_set_new ();
-      TMP_GEN (bb) = bitmap_set_new ();
-      AVAIL_OUT (bb) = bitmap_set_new ();
-    }
-}
-
-/* Deinitialize data structures used for ANTIC and AVAIL.  */
-
-static void
-fini_antic (void)
-{
-  basic_block bb;
-
-  if (maximal_set)
-    bitmap_set_free (maximal_set);
-  free (postorder);
-  bitmap_obstack_release (&grand_bitmap_obstack);
-  free_alloc_pool (bitmap_set_pool);
-  free_alloc_pool (binary_node_pool);
-  free_alloc_pool (reference_node_pool);
-  free_alloc_pool (unary_node_pool);
-  free_alloc_pool (comparison_node_pool);
-
-  FOR_ALL_BB (bb)
-    {
-      free (bb->aux);
-      bb->aux = NULL;
-    }
-}
-
 /* Compute ANTIC and partial ANTIC sets.  */
 
 static void
@@ -2159,22 +2293,6 @@
 {
   return TREE_CODE (op) == FILTER_EXPR || TREE_CODE (op) == EXC_PTR_EXPR;
 }
-
-/* Return true if OP is a tree which we can perform value numbering
-   on.  */
-
-static bool
-can_value_number_operation (tree op)
-{
-  return (UNARY_CLASS_P (op)
-	  && !is_exception_related (TREE_OPERAND (op, 0)))
-    || BINARY_CLASS_P (op)
-    || COMPARISON_CLASS_P (op)
-    || REFERENCE_CLASS_P (op)
-    || (TREE_CODE (op) == CALL_EXPR
-	&& can_value_number_call (op));
-}
-
 
 /* Return true if OP is a tree which we can perform PRE on
    on.  This may not match the operations we can value number, but in
@@ -2218,90 +2336,203 @@
    are doing.
 */
 static tree
-create_component_ref_by_pieces (basic_block block, tree expr, tree stmts,
-				tree domstmt)
-{
-  tree genop = expr;
-  tree folded;
-
-  if (TREE_CODE (genop) == VALUE_HANDLE)
-    {
-      tree found = bitmap_find_leader (AVAIL_OUT (block), expr, domstmt);
-      if (found)
-	return found;
-    }
-
-  if (TREE_CODE (genop) == VALUE_HANDLE)
-    {
-      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
-      unsigned int firstbit = bitmap_first_set_bit (exprset->expressions);
-      genop = expression_for_id (firstbit);
-    }
-
-  switch TREE_CODE (genop)
-    {
+create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
+				unsigned int operand,
+				tree stmts,
+				tree domstmt,
+				bool in_call)
+{
+  vn_reference_op_t currop = VEC_index (vn_reference_op_s, ref->operands,
+					operand);
+  tree genop;
+  switch (currop->opcode) 
+    {
+    case CALL_EXPR:
+      {
+	tree folded;
+	unsigned int i;
+	vn_reference_op_t declop = VEC_index (vn_reference_op_s,
+					      ref->operands, 1);
+	unsigned int nargs = VEC_length (vn_reference_op_s, ref->operands) - 2;
+	tree *args = XNEWVEC (tree, nargs);
+
+	for (i = 0; i < nargs; i++)
+	  {
+	    args[i] = create_component_ref_by_pieces (block, ref,
+						      operand + 2 + i, stmts,
+						      domstmt, true);
+	  }
+	folded = build_call_array (currop->type, declop->op0, nargs, args);
+	free (args);
+	return folded;
+      }
+      break;
+    case REALPART_EXPR:
+    case IMAGPART_EXPR:
+    case VIEW_CONVERT_EXPR:
+      {
+	tree folded;
+	tree genop0 = create_component_ref_by_pieces (block, ref,
+						      operand + 1,
+						      stmts, domstmt,
+						      in_call);
+	if (!genop0)
+	  return NULL_TREE;
+	folded = fold_build1 (currop->opcode, currop->type,
+			      genop0);
+	return folded;
+      }
+      break;
+    case ALIGN_INDIRECT_REF:
+    case MISALIGNED_INDIRECT_REF:
+    case INDIRECT_REF:
+      {
+	/* Inside a CALL_EXPR op0 is the actual indirect_ref.  */
+	if (in_call)
+	  {
+	    tree folded;
+	    tree op0 = TREE_OPERAND (currop->op0, 0);
+	    pre_expr op0expr = get_or_alloc_expr_for (op0);
+	    tree genop0 = find_or_generate_expression (block, op0expr, stmts,
+						       domstmt);
+	    if (!genop0)
+	      return NULL_TREE;
+	    folded = fold_build1 (currop->opcode, currop->type,
+				  genop0);
+	    return folded;
+	  }
+	else
+	  {
+
+	    tree folded;
+	    tree genop1 = create_component_ref_by_pieces (block, ref,
+							  operand + 1,
+							  stmts, domstmt,
+							  in_call);
+	    if (!genop1)
+	      return NULL_TREE;
+	    genop1 = fold_convert (build_pointer_type (currop->type),
+		genop1);
+
+	    folded = fold_build1 (currop->opcode, currop->type,
+		genop1);
+	    return folded;
+	  }
+      }
+      break;
+    case BIT_FIELD_REF:
+      {
+	tree folded;
+	tree genop0 = create_component_ref_by_pieces (block, ref, operand + 1,
+						      stmts, domstmt,
+						      in_call);
+	pre_expr op1expr = get_or_alloc_expr_for (currop->op0);
+	pre_expr op2expr = get_or_alloc_expr_for (currop->op1);
+	tree genop1;
+	tree genop2;
+	
+	if (!genop0)
+	  return NULL_TREE;
+	genop1 = find_or_generate_expression (block, op1expr, stmts, domstmt);
+	if (!genop1)
+	  return NULL_TREE;
+	genop2 = find_or_generate_expression (block, op2expr, stmts, domstmt);
+	if (!genop2)
+	  return NULL_TREE;
+	folded = fold_build3 (BIT_FIELD_REF, currop->type, genop0, genop1,
+			      genop2);
+	return folded;
+      }
+      
+      /* For array ref vn_reference_op's, operand 1 of the array ref
+	 is op0 of the reference op and operand 3 of the array ref is
+	 op1.  */
+    case ARRAY_RANGE_REF:
     case ARRAY_REF:
       {
-	tree op0;
-	tree op1, op2, op3;
-	op0 = create_component_ref_by_pieces (block,
-					      TREE_OPERAND (genop, 0),
-					      stmts, domstmt);
-	op1 = TREE_OPERAND (genop, 1);
-	if (TREE_CODE (op1) == VALUE_HANDLE)
-	  op1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	op2 = TREE_OPERAND (genop, 2);
-	if (op2 && TREE_CODE (op2) == VALUE_HANDLE)
-	  op2 = find_or_generate_expression (block, op2, stmts, domstmt);
-	op3 = TREE_OPERAND (genop, 3);
-	if (op3 && TREE_CODE (op3) == VALUE_HANDLE)
-	  op3 = find_or_generate_expression (block, op3, stmts, domstmt);
-	if (!op0 || !op1)
-	  return NULL_TREE;
-	folded = build4 (ARRAY_REF, TREE_TYPE (genop), op0, op1,
-			      op2, op3);
-	return folded;
-      }
+	vn_reference_op_t op0expr;
+	tree genop0;
+	tree genop1 = currop->op0;
+	pre_expr op1expr;
+	tree genop2 = currop->op1;
+	pre_expr op2expr;
+	tree genop3;
+	op0expr = VEC_index (vn_reference_op_s, ref->operands, operand + 1);
+	genop0 = create_component_ref_by_pieces (block, ref, operand + 1,
+						 stmts, domstmt,
+						 in_call);
+	if (!genop0)
+	  return NULL_TREE;
+	op1expr = get_or_alloc_expr_for (genop1);
+	genop1 = find_or_generate_expression (block, op1expr, stmts, domstmt);
+	if (!genop1)
+	  return NULL_TREE;
+	if (genop2) 
+	  {
+	    op2expr = get_or_alloc_expr_for (genop2);
+	    genop2 = find_or_generate_expression (block, op2expr, stmts,
+						  domstmt);
+	    if (!genop2)
+	      return NULL_TREE;
+	  }
+
+	genop3 = currop->op2;
+	return build4 (currop->opcode, currop->type, genop0, genop1,
+		       genop2, genop3);
+      }		      
     case COMPONENT_REF:
       {
 	tree op0;
 	tree op1;
-	op0 = create_component_ref_by_pieces (block,
-					      TREE_OPERAND (genop, 0),
-					      stmts, domstmt);
+	tree genop2 = currop->op1;
+	pre_expr op2expr;
+	op0 = create_component_ref_by_pieces (block, ref, operand + 1,
+					      stmts, domstmt, in_call);
 	if (!op0)
 	  return NULL_TREE;
 	/* op1 should be a FIELD_DECL, which are represented by
 	   themselves.  */
-	op1 = TREE_OPERAND (genop, 1);
-	folded = fold_build3 (COMPONENT_REF, TREE_TYPE (genop), op0, op1,
-			      NULL_TREE);
-	return folded;
-      }
-      break;
-    case INDIRECT_REF:
-      {
-	tree op1 = TREE_OPERAND (genop, 0);
-	tree genop1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	if (!genop1)
-	  return NULL_TREE;
-
-	folded = fold_build1 (TREE_CODE (genop), TREE_TYPE (genop),
-			      genop1);
-	return folded;
-      }
-      break;
+	op1 = currop->op0;
+	if (genop2) 
+	  {
+	    op2expr = get_or_alloc_expr_for (genop2);
+	    genop2 = find_or_generate_expression (block, op2expr, stmts,
+						  domstmt);
+	    if (!genop2)
+	      return NULL_TREE;
+	  }
+	
+	return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1,
+			    genop2);
+      }
+      break;
+    case SSA_NAME:
+      {
+	pre_expr op0expr = get_or_alloc_expr_for (currop->op0);
+	genop = find_or_generate_expression (block, op0expr, stmts, domstmt);
+	return genop;
+      }
+    case STRING_CST:
+    case INTEGER_CST:
+    case COMPLEX_CST:
+    case VECTOR_CST:
+    case REAL_CST:
+    case CONSTRUCTOR:
     case VAR_DECL:
     case PARM_DECL:
+    case CONST_DECL:
     case RESULT_DECL:
-    case SSA_NAME:
-    case STRING_CST:
-      return genop;
+    case FUNCTION_DECL:
+      /* For ADDR_EXPR in a CALL_EXPR, op0 is actually the entire
+	 ADDR_EXPR, not just it's operand.  */
+    case ADDR_EXPR:
+      if (currop->opcode == ADDR_EXPR)
+	gcc_assert (currop->op0 != NULL);
+      return currop->op0;
+      
     default:
       gcc_unreachable ();
-    }
-
-  return NULL_TREE;
+    } 
 }
 
 /* Find a leader for an expression, or generate one using
@@ -2318,31 +2549,40 @@
    on failure.  */
 
 static tree
-find_or_generate_expression (basic_block block, tree expr, tree stmts,
+find_or_generate_expression (basic_block block, pre_expr expr, tree stmts,
 			     tree domstmt)
 {
-  tree genop = bitmap_find_leader (AVAIL_OUT (block), expr, domstmt);
-
+  pre_expr leader = bitmap_find_leader (AVAIL_OUT (block),
+					get_expr_value_id (expr), domstmt);
+  tree genop = NULL;
+  if (leader)
+    {
+      if (leader->kind == NAME)
+	genop = PRE_EXPR_NAME (leader);
+      else if (leader->kind == CONSTANT)
+	genop = PRE_EXPR_CONSTANT (leader);
+    }
+  
   /* If it's still NULL, it must be a complex expression, so generate
      it recursively.  */
   if (genop == NULL)
     {
-      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
+      bitmap_set_t exprset;
+      unsigned int lookfor = get_expr_value_id (expr);
       bool handled = false;
       bitmap_iterator bi;
       unsigned int i;
 
-      /* We will hit cases where we have SSA_NAME's in exprset before
-	 other operations, because we may have come up with the SCCVN
-	 value before getting to the RHS of the expression.  */
+      exprset = VEC_index (bitmap_set_t, value_expressions, lookfor);
       FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
 	{
-	  genop = expression_for_id (i);
-	  if (can_PRE_operation (genop))
+	  pre_expr temp = expression_for_id (i);
+	  if (temp->kind != NAME)
 	    {
 	      handled = true;
-	      genop = create_expression_by_pieces (block, genop, stmts,
-						   domstmt);
+	      genop = create_expression_by_pieces (block, temp, stmts,
+						   domstmt,
+						   get_expr_type (expr));
 	      break;
 	    }
 	}
@@ -2355,6 +2595,7 @@
 }
 
 #define NECESSARY(stmt)		stmt->base.asm_written_flag
+
 /* Create an expression in pieces, so that we can handle very complex
    expressions that may be ANTIC, but not necessary GIMPLE.
    BLOCK is the basic block the expression will be inserted into,
@@ -2374,109 +2615,77 @@
    can return NULL_TREE to signal failure.  */
 
 static tree
-create_expression_by_pieces (basic_block block, tree expr, tree stmts,
-			     tree domstmt)
+create_expression_by_pieces (basic_block block, pre_expr expr, tree stmts,
+			     tree domstmt,
+			     tree type)
 {
   tree temp, name;
   tree folded, forced_stmts, newexpr;
-  tree v;
+  unsigned int value_id;
   tree_stmt_iterator tsi;
-
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_vl_exp:
-      {
-	tree fn, sc;
-	tree genfn;
-	int i, nargs;
-	tree *buffer;
-
-	gcc_assert (TREE_CODE (expr) == CALL_EXPR);
-
-	fn = CALL_EXPR_FN (expr);
-	sc = CALL_EXPR_STATIC_CHAIN (expr);
-
-	genfn = find_or_generate_expression (block, fn, stmts, domstmt);
-	if (!genfn)
-	  return NULL_TREE;
-
-	nargs = call_expr_nargs (expr);
-	buffer = (tree*) alloca (nargs * sizeof (tree));
-
-	for (i = 0; i < nargs; i++)
-	  {
-	    tree arg = CALL_EXPR_ARG (expr, i);
-	    buffer[i] = find_or_generate_expression (block, arg, stmts,
-						     domstmt);
-	    if (!buffer[i])
-	      return NULL_TREE;
-	  }
-
-	folded = build_call_array (TREE_TYPE (expr), genfn, nargs, buffer);
-	if (sc)
-	  {
-	    CALL_EXPR_STATIC_CHAIN (folded) =
-	      find_or_generate_expression (block, sc, stmts, domstmt);
-	    if (!CALL_EXPR_STATIC_CHAIN (folded))
-	      return NULL_TREE;
-	  }
-	folded = fold (folded);
-	break;
-      }
-      break;
-    case tcc_reference:
-      {
-	if (TREE_CODE (expr) == COMPONENT_REF
-	    || TREE_CODE (expr) == ARRAY_REF)
-	  {
-	    folded = create_component_ref_by_pieces (block, expr, stmts,
-						     domstmt);
-	    if (!folded)
-	      return NULL_TREE;
-	  }
-	else
-	  {
-	    tree op1 = TREE_OPERAND (expr, 0);
-	    tree genop1 = find_or_generate_expression (block, op1, stmts,
-						       domstmt);
-	    if (!genop1)
-	      return NULL_TREE;
-
-	    folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
-				  genop1);
-	  }
-	break;
-      }
-
-    case tcc_binary:
-    case tcc_comparison:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	tree op2 = TREE_OPERAND (expr, 1);
-	tree genop1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	tree genop2 = find_or_generate_expression (block, op2, stmts, domstmt);
-	if (!genop1 || !genop2)
-	  return NULL_TREE;
-	folded = fold_build2 (TREE_CODE (expr), TREE_TYPE (expr),
-			      genop1, genop2);
-	break;
-      }
-
-    case tcc_unary:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	tree genop1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	if (!genop1)
-	  return NULL_TREE;
-	folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
-			      genop1);
-	break;
-      }
-
-    default:
-      gcc_unreachable ();
-    }
-
+  tree exprtype = type ? type : get_expr_type (expr);
+  pre_expr nameexpr;
+  
+  switch (expr->kind)
+    {
+      /* We may hit the NAME/CONSTANT case if we have to convert types
+	 that value numbering saw through.  */
+    case NAME:
+      folded = PRE_EXPR_NAME (expr);
+      break;
+    case CONSTANT:
+      folded = PRE_EXPR_CONSTANT (expr);
+      break;
+    case REFERENCE:
+      {
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	folded = create_component_ref_by_pieces (block, ref, 0, stmts,
+						 domstmt, false);
+      }
+      break;
+    case NARY:
+      {
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	switch (nary->length) 
+	  {
+	  case 2:
+	    {
+	      pre_expr op1 = get_or_alloc_expr_for (nary->op[0]);
+	      pre_expr op2 = get_or_alloc_expr_for (nary->op[1]);
+	      tree genop1 = find_or_generate_expression (block, op1,
+							 stmts, domstmt);
+	      tree genop2 = find_or_generate_expression (block, op2,
+							 stmts, domstmt);
+	      if (!genop1 || !genop2)
+		return NULL_TREE;
+	      /* Ensure op2 is a sizetype for POINTER_PLUS_EXPR.  It
+		 may be a constant with the wrong type.  */
+	      if (nary->opcode == POINTER_PLUS_EXPR)
+		genop2 = fold_convert (sizetype, genop2);
+	      folded = fold_build2 (nary->opcode, nary->type,
+				    genop1, genop2);
+	    }
+	    break;
+	  case 1:
+	    {
+	      pre_expr op1 = get_or_alloc_expr_for (nary->op[0]);
+	      tree genop1 = find_or_generate_expression (block, op1,
+							 stmts, domstmt);
+	      if (!genop1)
+		return NULL_TREE;
+	      folded = fold_build1 (nary->opcode, nary->type,
+				    genop1);
+	    }
+	    break;
+	  default:
+	    return NULL_TREE;
+	  }
+      }
+      break;
+    default:
+      return NULL_TREE;
+    }
+  folded = fold_convert (exprtype, folded);
   /* Force the generated expression to be a sequence of GIMPLE
      statements.
      We have to call unshare_expr because force_gimple_operand may
@@ -2493,14 +2702,18 @@
 	{
 	  tree stmt = tsi_stmt (tsi);
 	  tree forcedname = GIMPLE_STMT_OPERAND (stmt, 0);
-	  tree forcedexpr = GIMPLE_STMT_OPERAND (stmt, 1);
-	  tree val = vn_lookup_or_add (forcedexpr);
-
+	  pre_expr nameexpr;
+	  
 	  VEC_safe_push (tree, heap, inserted_exprs, stmt);
-	  VN_INFO_GET (forcedname)->valnum = forcedname;
-	  vn_add (forcedname, val);
-	  bitmap_value_replace_in_set (NEW_SETS (block), forcedname);
-	  bitmap_value_replace_in_set (AVAIL_OUT (block), forcedname);
+	  if (TREE_CODE (forcedname) == SSA_NAME)
+	    {
+	      VN_INFO_GET (forcedname)->valnum = forcedname;
+	      VN_INFO (forcedname)->value_id = get_next_value_id ();
+	      nameexpr = get_or_alloc_expr_for_name (forcedname);
+	      add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
+	      bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
+	      bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
+	    }
 	  mark_symbols_for_renaming (stmt);
 	}
       tsi = tsi_last (stmts);
@@ -2509,17 +2722,17 @@
 
   /* Build and insert the assignment of the end result to the temporary
      that we will return.  */
-  if (!pretemp || TREE_TYPE (expr) != TREE_TYPE (pretemp))
-    {
-      pretemp = create_tmp_var (TREE_TYPE (expr), "pretmp");
+  if (!pretemp || exprtype != TREE_TYPE (pretemp))
+    {
+      pretemp = create_tmp_var (exprtype, "pretmp");
       get_var_ann (pretemp);
     }
 
   temp = pretemp;
   add_referenced_var (temp);
 
-  if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE
-      || TREE_CODE (TREE_TYPE (expr)) == VECTOR_TYPE)
+  if (TREE_CODE (exprtype) == COMPLEX_TYPE
+      || TREE_CODE (exprtype) == VECTOR_TYPE)
     DECL_GIMPLE_REG_P (temp) = 1;
 
   newexpr = build_gimple_modify_stmt (temp, newexpr);
@@ -2534,18 +2747,19 @@
   /* All the symbols in NEWEXPR should be put into SSA form.  */
   mark_symbols_for_renaming (newexpr);
 
-  /* Add a value handle to the temporary.
+  /* Add a value number to the temporary.
      The value may already exist in either NEW_SETS, or AVAIL_OUT, because
      we are creating the expression by pieces, and this particular piece of
      the expression may have been represented.  There is no harm in replacing
      here.  */
-  v = get_value_handle (expr);
-  vn_add (name, v);
   VN_INFO_GET (name)->valnum = name;
-  get_or_alloc_expression_id (name);
+  value_id = get_expr_value_id (expr);
+  VN_INFO (name)->value_id = value_id;
+  nameexpr = get_or_alloc_expr_for_name (name);
+  add_to_value (value_id, nameexpr);
   if (!in_fre)
-    bitmap_value_replace_in_set (NEW_SETS (block), name);
-  bitmap_value_replace_in_set (AVAIL_OUT (block), name);
+    bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
+  bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
 
   pre_stats.insertions++;
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -2558,39 +2772,38 @@
   return name;
 }
 
+
 /* Insert the to-be-made-available values of expression EXPRNUM for each
    predecessor, stored in AVAIL, into the predecessors of BLOCK, and
-   merge the result with a phi node, given the same value handle as
+   merge the result with a phi node, given the same value number as
    NODE.  Return true if we have inserted new stuff.  */
 
 static bool
 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
-			    tree *avail)
-{
-  tree expr = expression_for_id (exprnum);
-  tree val = get_value_handle (expr);
+			    pre_expr *avail)
+{
+  pre_expr expr = expression_for_id (exprnum);
+  pre_expr newphi;
+  unsigned int val = get_expr_value_id (expr);
   edge pred;
   bool insertions = false;
   bool nophi = false;
   basic_block bprime;
-  tree eprime;
+  pre_expr eprime;
   edge_iterator ei;
-  tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
+  tree type = get_expr_type (expr);
   tree temp;
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
       fprintf (dump_file, "Found partial redundancy for expression ");
-      print_generic_expr (dump_file, expr, 0);
-      fprintf (dump_file, " (");
-      print_generic_expr (dump_file, val, 0);
-      fprintf (dump_file, ")");
-      fprintf (dump_file, "\n");
+      print_pre_expr (dump_file, expr);
+      fprintf (dump_file, " (%04d)\n", val);
     }
 
   /* Make sure we aren't creating an induction variable.  */
   if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2
-      && TREE_CODE_CLASS (TREE_CODE (expr)) != tcc_reference )
+      && expr->kind != REFERENCE)
     {
       bool firstinsideloop = false;
       bool secondinsideloop = false;
@@ -2616,15 +2829,110 @@
       bprime = pred->src;
       eprime = avail[bprime->index];
 
-      if (can_PRE_operation (eprime))
+      if (eprime->kind != NAME && eprime->kind != CONSTANT)
 	{
 	  builtexpr = create_expression_by_pieces (bprime,
 						   eprime,
-						   stmts, NULL_TREE);
+						   stmts, NULL_TREE,
+						   type);
 	  gcc_assert (!(pred->flags & EDGE_ABNORMAL));
 	  bsi_insert_on_edge (pred, stmts);
-	  avail[bprime->index] = builtexpr;
+	  avail[bprime->index] = get_or_alloc_expr_for_name (builtexpr);
 	  insertions = true;
+	}
+      else if (eprime->kind == CONSTANT) 
+	{
+	  /* Constants may not have the right type, fold_convert
+	     should give us back a constant with the right type.
+	  */
+	  tree constant = PRE_EXPR_CONSTANT (eprime);
+	  if (TREE_TYPE (constant) != type)
+	    {
+	      tree builtexpr = fold_convert (type, constant);
+	      if (is_gimple_min_invariant (builtexpr))
+		{
+		  PRE_EXPR_CONSTANT (eprime) = builtexpr;
+		}
+	      else
+		{
+		  tree forcedexpr = force_gimple_operand (builtexpr,
+							  &stmts, true,
+							  NULL);
+		  if (is_gimple_min_invariant (forcedexpr))
+		    {
+		      PRE_EXPR_CONSTANT (eprime) = forcedexpr;
+		    }
+		  else
+		    {
+		      if (forcedexpr != builtexpr)
+			{
+			  VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime);
+			  VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime);
+			}
+		      if (stmts)
+			{
+			  tree_stmt_iterator tsi;
+			  tsi = tsi_start (stmts);
+			  for (; !tsi_end_p (tsi); tsi_next (&tsi))
+			    {
+			      tree stmt = tsi_stmt (tsi);
+			      tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
+			      VEC_safe_push (tree, heap, inserted_exprs, stmt);
+			      NECESSARY (lhs) = 0;
+			    }
+			  bsi_insert_on_edge (pred, stmts);
+			}
+		      NECESSARY (forcedexpr) = 0;
+		      avail[bprime->index] = get_or_alloc_expr_for_name (forcedexpr);
+		    }
+		}
+	    }  
+	}
+      else if (eprime->kind == NAME)
+	{
+	  /* We may have to do a conversion because our value
+	     numbering can look through types in certain cases, but
+	     our IL requires all operands of a phi node have the same
+	     type.  */
+	  tree name = PRE_EXPR_NAME (eprime);
+	  if (TREE_TYPE (name) != type)
+	    {
+	      tree builtexpr;
+	      tree forcedexpr;
+	      /* When eliminating casts through unions, 
+		 we sometimes want to convert a real to an integer,
+		 which fold_convert will ICE on  */
+	      if (fold_convertible_p (type, name))
+		builtexpr = fold_convert (type, name);
+	      else
+		builtexpr = convert (type, name);
+	      
+	      forcedexpr = force_gimple_operand (builtexpr,
+						 &stmts, true,
+						 NULL);
+
+	      if (forcedexpr != name)
+		{
+		  VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum;
+		  VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id;
+		}
+	      
+	      if (stmts)
+		{
+		  tree_stmt_iterator tsi;
+		  tsi = tsi_start (stmts);
+		  for (; !tsi_end_p (tsi); tsi_next (&tsi))
+		    {
+		      tree stmt = tsi_stmt (tsi);
+		      tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
+		      VEC_safe_push (tree, heap, inserted_exprs, stmt);
+		      NECESSARY (lhs) = 0;
+		    }
+		  bsi_insert_on_edge (pred, stmts);
+		}
+	      NECESSARY (forcedexpr) = 0;
+	      avail[bprime->index] = get_or_alloc_expr_for_name (forcedexpr);
+	    }
 	}
     }
   /* If we didn't want a phi node, and we made insertions, we still have
@@ -2646,7 +2954,6 @@
   temp = prephitemp;
   add_referenced_var (temp);
 
-
   if (TREE_CODE (type) == COMPLEX_TYPE
       || TREE_CODE (type) == VECTOR_TYPE)
     DECL_GIMPLE_REG_P (temp) = 1;
@@ -2654,12 +2961,21 @@
 
   NECESSARY (temp) = 0;
   VN_INFO_GET (PHI_RESULT (temp))->valnum = PHI_RESULT (temp);
-
+  VN_INFO (PHI_RESULT (temp))->value_id = val;
   VEC_safe_push (tree, heap, inserted_exprs, temp);
   FOR_EACH_EDGE (pred, ei, block->preds)
-    add_phi_arg (temp, avail[pred->src->index], pred);
-
-  vn_add (PHI_RESULT (temp), val);
+    {
+      pre_expr ae = avail[pred->src->index];
+      gcc_assert (get_expr_type (ae) == type
+		  || useless_type_conversion_p (type, get_expr_type (ae)));
+      if (ae->kind == CONSTANT)
+	add_phi_arg (temp, PRE_EXPR_CONSTANT (ae), pred);
+      else
+	add_phi_arg (temp, PRE_EXPR_NAME (avail[pred->src->index]), pred);
+    }
+  
+  newphi = get_or_alloc_expr_for_name (PHI_RESULT (temp));
+  add_to_value (val, newphi);
 
   /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
      this insertion, since we test for the existence of this value in PHI_GEN
@@ -2675,12 +2991,11 @@
      AVAIL_OUT, and would have been skipped due to the full redundancy check.
   */
 
-  bitmap_insert_into_set (PHI_GEN (block),
-			  PHI_RESULT (temp));
+  bitmap_insert_into_set (PHI_GEN (block), newphi);
   bitmap_value_replace_in_set (AVAIL_OUT (block),
-			       PHI_RESULT (temp));
+			       newphi);
   bitmap_insert_into_set (NEW_SETS (block),
-			  PHI_RESULT (temp));
+			  newphi);
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
@@ -2716,26 +3031,26 @@
 do_regular_insertion (basic_block block, basic_block dom)
 {
   bool new_stuff = false;
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
-  tree expr;
-  int i;
-
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
-    {
-      if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
-	{
-	  tree *avail;
-	  tree val;
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
+  pre_expr expr;
+  int i;
+
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
+    {
+      if (expr->kind != NAME)
+	{
+	  pre_expr *avail;
+	  unsigned int val;
 	  bool by_some = false;
 	  bool cant_insert = false;
 	  bool all_same = true;
-	  tree first_s = NULL;
+	  pre_expr first_s = NULL;
 	  edge pred;
 	  basic_block bprime;
-	  tree eprime = NULL_TREE;
+	  pre_expr eprime = NULL;
 	  edge_iterator ei;
 
-	  val = get_value_handle (expr);
+	  val = get_expr_value_id (expr);
 	  if (bitmap_set_contains_value (PHI_GEN (block), val))
 	    continue;
 	  if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
@@ -2745,11 +3060,11 @@
 	      continue;
 	    }
 
-	  avail = XCNEWVEC (tree, last_basic_block);
+	  avail = XCNEWVEC (pre_expr, last_basic_block);
 	  FOR_EACH_EDGE (pred, ei, block->preds)
 	    {
-	      tree vprime;
-	      tree edoubleprime;
+	      unsigned int vprime;
+	      pre_expr edoubleprime;
 
 	      /* This can happen in the very weird case
 		 that our fake infinite loop edges have caused a
@@ -2779,8 +3094,7 @@
 		}
 
 	      eprime = fully_constant_expression (eprime);
-	      vprime = get_value_handle (eprime);
-	      gcc_assert (vprime);
+	      vprime = get_expr_value_id (eprime);
 	      edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
 						 vprime, NULL_TREE);
 	      if (edoubleprime == NULL)
@@ -2794,8 +3108,7 @@
 		  by_some = true;
 		  if (first_s == NULL)
 		    first_s = edoubleprime;
-		  else if (!operand_equal_p (first_s, edoubleprime,
-					     0))
+		  else if (!pre_expr_eq (first_s, edoubleprime))
 		    all_same = false;
 		}
 	    }
@@ -2813,19 +3126,26 @@
 	     an invariant, then the PHI has the same value on all
 	     edges.  Note this.  */
 	  else if (!cant_insert && all_same && eprime
-		   && is_gimple_min_invariant (eprime)
-		   && !is_gimple_min_invariant (val))
+		   && eprime->kind == CONSTANT
+		   && !value_id_constant_p (val))
 	    {
 	      unsigned int j;
 	      bitmap_iterator bi;
-
-	      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
+	      bitmap_set_t exprset = VEC_index (bitmap_set_t,
+						value_expressions, val);
+
+	      unsigned int new_val = get_expr_value_id (eprime);
 	      FOR_EACH_EXPR_ID_IN_SET (exprset, j, bi)
 		{
-		  tree expr = expression_for_id (j);
-		  if (TREE_CODE (expr) == SSA_NAME)
-		    {
-		      vn_add (expr, eprime);
+		  pre_expr expr = expression_for_id (j);
+
+		  if (expr->kind == NAME)
+		    {
+		      vn_ssa_aux_t info = VN_INFO (PRE_EXPR_NAME (expr));
+		      /* Just reset the value id and valnum so it is
+			 the same as the constant we have discovered.  */
+		      info->valnum = PRE_EXPR_CONSTANT (eprime);
+		      info->value_id = new_val;
 		      pre_stats.constified++;
 		    }
 		}
@@ -2834,7 +3154,7 @@
 	}
     }
 
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
   return new_stuff;
 }
 
@@ -2850,34 +3170,34 @@
 do_partial_partial_insertion (basic_block block, basic_block dom)
 {
   bool new_stuff = false;
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (PA_IN (block));
-  tree expr;
-  int i;
-
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
-    {
-      if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
-	{
-	  tree *avail;
-	  tree val;
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (PA_IN (block));
+  pre_expr expr;
+  int i;
+
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
+    {
+      if (expr->kind != NAME)
+	{
+	  pre_expr *avail;
+	  unsigned int val;
 	  bool by_all = true;
 	  bool cant_insert = false;
 	  edge pred;
 	  basic_block bprime;
-	  tree eprime = NULL_TREE;
+	  pre_expr eprime = NULL;
 	  edge_iterator ei;
 
-	  val = get_value_handle (expr);
+	  val = get_expr_value_id (expr);
 	  if (bitmap_set_contains_value (PHI_GEN (block), val))
 	    continue;
 	  if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
 	    continue;
 
-	  avail = XCNEWVEC (tree, last_basic_block);
+	  avail = XCNEWVEC (pre_expr, last_basic_block);
 	  FOR_EACH_EDGE (pred, ei, block->preds)
 	    {
-	      tree vprime;
-	      tree edoubleprime;
+	      unsigned int vprime;
+	      pre_expr edoubleprime;
 
 	      /* This can happen in the very weird case
 		 that our fake infinite loop edges have caused a
@@ -2908,8 +3228,7 @@
 		}
 
 	      eprime = fully_constant_expression (eprime);
-	      vprime = get_value_handle (eprime);
-	      gcc_assert (vprime);
+	      vprime = get_expr_value_id (eprime);
 	      edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
 						 vprime, NULL_TREE);
 	      if (edoubleprime == NULL)
@@ -2926,7 +3245,7 @@
 	     already existing along every predecessor, and
 	     it's defined by some predecessor, it is
 	     partially redundant.  */
-	  if (!cant_insert && by_all)
+	  if (!cant_insert && by_all && dbg_cnt (treepre_insert))
 	    {
 	      pre_stats.pa_insert++;
 	      if (insert_into_preds_of_block (block, get_expression_id (expr),
@@ -2937,7 +3256,7 @@
 	}
     }
 
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
   return new_stuff;
 }
 
@@ -2964,7 +3283,7 @@
 		 to replace it with.  */
 	      FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
 		{
-		  tree expr = expression_for_id (i);
+		  pre_expr expr = expression_for_id (i);
 		  bitmap_value_replace_in_set (NEW_SETS (block), expr);
 		  bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
 		}
@@ -3019,162 +3338,16 @@
 {
   if (!in_fre)
     {
+      pre_expr result;
       if (TREE_CODE (op) == SSA_NAME && ssa_undefined_value_p (op))
 	return;
-      bitmap_value_insert_into_set (EXP_GEN (block), op);
+      result = get_or_alloc_expr_for_name (op);
+      bitmap_value_insert_into_set (EXP_GEN (block), result);
       if (TREE_CODE (op) != SSA_NAME
 	  || TREE_CODE (SSA_NAME_DEF_STMT (op)) != PHI_NODE)
-	bitmap_value_insert_into_set (maximal_set, op);
-    }
-}
-
-
-/* Given an SSA variable VAR and an expression EXPR, compute the value
-   number for EXPR and create a value handle (VAL) for it.  If VAR and
-   EXPR are not the same, associate VAL with VAR.  Finally, add VAR to
-   S1 and its value handle to S2, and to the maximal set if
-   ADD_TO_MAXIMAL is true.
-
-   VUSES represent the virtual use operands associated with EXPR (if
-   any).  */
-
-static inline void
-add_to_sets (tree var, tree expr, VEC(tree, gc) *vuses, bitmap_set_t s1,
-	     bitmap_set_t s2)
-{
-  tree val;
-  val = vn_lookup_or_add_with_vuses (expr, vuses);
-
-  /* VAR and EXPR may be the same when processing statements for which
-     we are not computing value numbers (e.g., non-assignments, or
-     statements that make aliased stores).  In those cases, we are
-     only interested in making VAR available as its own value.  */
-  if (var != expr)
-    vn_add (var, val);
-
-  if (s1)
-    bitmap_insert_into_set (s1, var);
-
-  bitmap_value_insert_into_set (s2, var);
-}
-
-/* Find existing value expression that is the same as T,
-   and return it if it exists.  */
-
-static inline tree
-find_existing_value_expr (tree t, VEC (tree, gc) *vuses)
-{
-  bitmap_iterator bi;
-  unsigned int bii;
-  tree vh;
-  bitmap_set_t exprset;
-
-  if (REFERENCE_CLASS_P (t) || TREE_CODE (t) == CALL_EXPR || DECL_P (t))
-    vh = vn_lookup_with_vuses (t, vuses);
-  else
-    vh = vn_lookup (t);
-
-  if (!vh)
-    return NULL;
-  exprset = VALUE_HANDLE_EXPR_SET (vh);
-  FOR_EACH_EXPR_ID_IN_SET (exprset, bii, bi)
-    {
-      tree efi = expression_for_id (bii);
-      if (expressions_equal_p (t, efi))
-	return efi;
-    }
-  return NULL;
-}
-
-/* Given a unary or binary expression EXPR, create and return a new
-   expression with the same structure as EXPR but with its operands
-   replaced with the value handles of each of the operands of EXPR.
-
-   VUSES represent the virtual use operands associated with EXPR (if
-   any).  Insert EXPR's operands into the EXP_GEN set for BLOCK.
-
-   If CHECK_AVAIL is true, checks availability of each operand in
-   BLOCKs AVAIL_OUT set.  */
-
-static inline tree
-create_value_expr_from (tree expr, basic_block block, VEC (tree, gc) *vuses,
-			bool check_avail)
-{
-  int i;
-  enum tree_code code = TREE_CODE (expr);
-  tree vexpr;
-  alloc_pool pool = NULL;
-  tree efi;
-
-  gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
-	      || TREE_CODE_CLASS (code) == tcc_binary
-	      || TREE_CODE_CLASS (code) == tcc_comparison
-	      || TREE_CODE_CLASS (code) == tcc_reference
-	      || TREE_CODE_CLASS (code) == tcc_expression
-	      || TREE_CODE_CLASS (code) == tcc_vl_exp
-	      || TREE_CODE_CLASS (code) == tcc_exceptional
-	      || TREE_CODE_CLASS (code) == tcc_declaration);
-
-  if (TREE_CODE_CLASS (code) == tcc_unary)
-    pool = unary_node_pool;
-  else if (TREE_CODE_CLASS (code) == tcc_reference)
-    pool = reference_node_pool;
-  else if (TREE_CODE_CLASS (code) == tcc_binary)
-    pool = binary_node_pool;
-  else if (TREE_CODE_CLASS (code) == tcc_comparison)
-    pool = comparison_node_pool;
-  else
-    gcc_assert (code == CALL_EXPR);
-
-  if (code == CALL_EXPR)
-    vexpr = temp_copy_call_expr (expr);
-  else
-    {
-      vexpr = (tree) pool_alloc (pool);
-      memcpy (vexpr, expr, tree_size (expr));
-    }
-
-  for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
-    {
-      tree val = NULL_TREE;
-      tree op;
-
-      op = TREE_OPERAND (expr, i);
-      if (op == NULL_TREE)
-	continue;
-
-      /* Recursively value-numberize reference ops and tree lists.  */
-      if (REFERENCE_CLASS_P (op))
-	{
-	  tree tempop = create_value_expr_from (op, block, vuses, check_avail);
-	  op = tempop ? tempop : op;
-	  val = vn_lookup_or_add_with_vuses (op, vuses);
-	  set_expression_vuses (op, vuses);
-	}
-      else
-	{
-	  val = vn_lookup_or_add (op);
-	}
-      if (TREE_CODE (op) != TREE_LIST)
-	add_to_exp_gen (block, op);
-
-      if (TREE_CODE (val) == VALUE_HANDLE)
-	TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
-
-      TREE_OPERAND (vexpr, i) = val;
-
-      if (check_avail
-	  && TREE_CODE (val) == VALUE_HANDLE
-	  && !bitmap_set_contains_value (AVAIL_OUT (block), val))
-	return NULL_TREE;
-    }
-  efi = find_existing_value_expr (vexpr, vuses);
-  if (efi)
-    return efi;
-  get_or_alloc_expression_id (vexpr);
-  return vexpr;
-}
-
+	bitmap_value_insert_into_set (maximal_set, result);
+    }
+}
 
 /* For each real store operation of the form
    *a = <value> that we see, create a corresponding fake store of the
@@ -3287,57 +3460,7 @@
     }
 }
 
-/* Given an SSA_NAME, see if SCCVN has a value number for it, and if
-   so, return the value handle for this value number, creating it if
-   necessary.
-   Return NULL if SCCVN has no info for us.  */
-
-static tree
-get_sccvn_value (tree name)
-{
-  if (TREE_CODE (name) == SSA_NAME
-      && VN_INFO (name)->valnum != name
-      && VN_INFO (name)->valnum != VN_TOP)
-    {
-      tree val = VN_INFO (name)->valnum;
-      bool is_invariant = is_gimple_min_invariant (val);
-      tree valvh = !is_invariant ? get_value_handle (val) : NULL_TREE;
-
-      /* We may end up with situations where SCCVN has chosen a
-	 representative for the equivalence set that we have not
-	 visited yet.  In this case, just create the value handle for
-	 it.  */
-      if (!valvh && !is_invariant)
-	{
-	  /* We lookup with the LHS, so do not use vn_lookup_or_add_with_stmt
-	     here, as that will result in useless reference lookups.  */
-	  valvh = vn_lookup_or_add (val);
-	}
-
-      if (dump_file && (dump_flags & TDF_DETAILS))
-	{
-	  fprintf (dump_file, "SCCVN says ");
-	  print_generic_expr (dump_file, name, 0);
-	  fprintf (dump_file, " value numbers to ");
-	  if (valvh && !is_invariant)
-	    {
-	      print_generic_expr (dump_file, val, 0);
-	      fprintf (dump_file, " (");
-	      print_generic_expr (dump_file, valvh, 0);
-	      fprintf (dump_file, ")\n");
-	    }
-	  else
-	    print_generic_stmt (dump_file, val, 0);
-	}
-      if (valvh)
-	return valvh;
-      else
-	return val;
-    }
-  return NULL_TREE;
-}
-
-/* Create value handles for PHI in BLOCK.  */
+/* Create value ids for PHI in BLOCK.  */
 
 static void
 make_values_for_phi (tree phi, basic_block block)
@@ -3347,122 +3470,11 @@
      actual computations.  */
   if (is_gimple_reg (result))
     {
-      tree sccvnval = get_sccvn_value (result);
-      if (sccvnval)
-	{
-	  vn_add (result, sccvnval);
-	  bitmap_insert_into_set (PHI_GEN (block), result);
-	  bitmap_value_insert_into_set (AVAIL_OUT (block), result);
-	}
-      else
-	add_to_sets (result, result, NULL,
-		     PHI_GEN (block), AVAIL_OUT (block));
-    }
-}
-
-/* Create value handles for STMT in BLOCK.  Return true if we handled
-   the statement.  */
-
-static bool
-make_values_for_stmt (tree stmt, basic_block block)
-{
-
-  tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
-  tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
-  tree valvh = NULL_TREE;
-  tree lhsval;
-  VEC (tree, gc) *vuses = NULL;
-
-  valvh = get_sccvn_value (lhs);
-
-  if (valvh)
-    {
-      vn_add (lhs, valvh);
-      bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-      /* Shortcut for FRE. We have no need to create value expressions,
-	 just want to know what values are available where.  */
-      if (in_fre)
-	return true;
-
-    }
-  else if (in_fre)
-    {
-      /* For FRE, if SCCVN didn't find anything, we aren't going to
-	 either, so just make up a new value number if necessary and
-	 call it a day */
-      if (get_value_handle (lhs) == NULL)
-	vn_lookup_or_add (lhs);
-      bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-      return true;
-    }
-
-  lhsval = valvh ? valvh : get_value_handle (lhs);
-  vuses = copy_vuses_from_stmt (stmt);
-  STRIP_USELESS_TYPE_CONVERSION (rhs);
-  if (can_value_number_operation (rhs)
-      && (!lhsval || !is_gimple_min_invariant (lhsval))
-      && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
-    {
-      /* For value numberable operation, create a
-	 duplicate expression with the operands replaced
-	 with the value handles of the original RHS.  */
-      tree newt = create_value_expr_from (rhs, block, vuses, false);
-      if (newt)
-	{
-	  set_expression_vuses (newt, vuses);
-	  /* If we already have a value number for the LHS, reuse
-	     it rather than creating a new one.  */
-	  if (lhsval)
-	    {
-	      set_value_handle (newt, lhsval);
-	      if (!is_gimple_min_invariant (lhsval))
-		add_to_value (lhsval, newt);
-	    }
-	  else
-	    {
-	      tree val = vn_lookup_or_add_with_vuses (newt, vuses);
-	      vn_add (lhs, val);
-	    }
-
-	  add_to_exp_gen (block, newt);
-	}
-
-      bitmap_insert_into_set (TMP_GEN (block), lhs);
-      bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-      return true;
-    }
-  else if ((TREE_CODE (rhs) == SSA_NAME
-	    && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
-	   || is_gimple_min_invariant (rhs)
-	   || TREE_CODE (rhs) == ADDR_EXPR
-	   || DECL_P (rhs))
-    {
-
-      if (lhsval)
-	{
-	  set_expression_vuses (rhs, vuses);
-	  set_value_handle (rhs, lhsval);
-	  if (!is_gimple_min_invariant (lhsval))
-	    add_to_value (lhsval, rhs);
-	  bitmap_insert_into_set (TMP_GEN (block), lhs);
-	  bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-	}
-      else
-	{
-	  /* Compute a value number for the RHS of the statement
-	     and add its value to the AVAIL_OUT set for the block.
-	     Add the LHS to TMP_GEN.  */
-	  set_expression_vuses (rhs, vuses);
-	  add_to_sets (lhs, rhs, vuses, TMP_GEN (block),
-		       AVAIL_OUT (block));
-	}
-      /* None of the rest of these can be PRE'd.  */
-      if (TREE_CODE (rhs) == SSA_NAME && !ssa_undefined_value_p (rhs))
-	add_to_exp_gen (block, rhs);
-      return true;
-    }
-  return false;
-
+      pre_expr e = get_or_alloc_expr_for_name (result);
+      add_to_value (get_expr_value_id (e), e);
+      bitmap_insert_into_set (PHI_GEN (block), e);
+      bitmap_value_insert_into_set (AVAIL_OUT (block), e);
+    }
 }
 
 /* Compute the AVAIL set for all basic blocks.
@@ -3478,11 +3490,12 @@
 static void
 compute_avail (void)
 {
+
   basic_block block, son;
   basic_block *worklist;
   size_t sp = 0;
   tree param;
-
+  
   /* For arguments with default definitions, we pretend they are
      defined in the entry block.  */
   for (param = DECL_ARGUMENTS (current_function_decl);
@@ -3492,14 +3505,15 @@
       if (gimple_default_def (cfun, param) != NULL)
 	{
 	  tree def = gimple_default_def (cfun, param);
-
-	  vn_lookup_or_add (def);
+	  pre_expr e = get_or_alloc_expr_for_name (def);
+
+	  add_to_value (get_expr_value_id (e), e);	  
 	  if (!in_fre)
 	    {
-	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
-	      bitmap_value_insert_into_set (maximal_set, def);
-	    }
-	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
+	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), e);
+	      bitmap_value_insert_into_set (maximal_set, e);
+	    }
+	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), e);
 	}
     }
 
@@ -3510,14 +3524,15 @@
       if (gimple_default_def (cfun, param) != NULL)
 	{
 	  tree def = gimple_default_def (cfun, param);
-
-	  vn_lookup_or_add (def);
+	  pre_expr e = get_or_alloc_expr_for_name (def);
+
+	  add_to_value (get_expr_value_id (e), e);
 	  if (!in_fre)
 	    {
-	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
-	      bitmap_value_insert_into_set (maximal_set, def);
-	    }
-	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
+	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), e);
+	      bitmap_value_insert_into_set (maximal_set, e);
+	    }
+	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), e);
 	}
     }
 
@@ -3565,67 +3580,119 @@
 
 	  set_gimple_stmt_uid (stmt, stmt_uid++);
 
-	  /* For regular value numbering, we are only interested in
-	     assignments of the form X_i = EXPR, where EXPR represents
-	     an "interesting" computation, it has no volatile operands
-	     and X_i doesn't flow through an abnormal edge.  */
-	  if (TREE_CODE (stmt) == RETURN_EXPR
-	      && !ann->has_volatile_ops)
-	    {
-	      tree realstmt = stmt;
-	      tree lhs;
-	      tree rhs;
-
-	      stmt = TREE_OPERAND (stmt, 0);
-	      if (stmt && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
-		{
-		  lhs = GIMPLE_STMT_OPERAND (stmt, 0);
-		  rhs = GIMPLE_STMT_OPERAND (stmt, 1);
-		  if (TREE_CODE (lhs) == SSA_NAME
-		      && is_gimple_min_invariant (VN_INFO (lhs)->valnum))
-		    {
-		      if (dump_file && (dump_flags & TDF_DETAILS))
-			{
-			  fprintf (dump_file, "SCCVN says ");
-			  print_generic_expr (dump_file, lhs, 0);
-			  fprintf (dump_file, " value numbers to ");
-			  print_generic_stmt (dump_file, VN_INFO (lhs)->valnum,
-					      0);
-			}
-		      vn_add (lhs, VN_INFO (lhs)->valnum);
-		      continue;
-		    }
-
-		  if (TREE_CODE (rhs) == SSA_NAME)
-		    add_to_exp_gen (block, rhs);
-
-		  FOR_EACH_SSA_TREE_OPERAND (op, realstmt, iter, SSA_OP_DEF)
-		    add_to_sets (op, op, NULL, TMP_GEN (block),
-				 AVAIL_OUT (block));
-		}
+	  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
+	    {
+	      pre_expr e = get_or_alloc_expr_for_name (op);
+
+	      add_to_value (get_expr_value_id (e), e);
+	      if (!in_fre)
+		{
+		  bitmap_insert_into_set (TMP_GEN (block), e);
+		  bitmap_value_insert_into_set (maximal_set, e);
+		}
+	      bitmap_value_insert_into_set (AVAIL_OUT (block), e);
+	    }
+
+	  switch (TREE_CODE (stmt)) 
+	    {
+	    case RETURN_EXPR:
+	      if (!ann->has_volatile_ops)
+		FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
+		  add_to_exp_gen (block, op);
 	      continue;
-	    }
-
-	  else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
-		   && !ann->has_volatile_ops
-		   && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
-		   && !tree_could_throw_p (stmt))
-	    {
-	      if (make_values_for_stmt (stmt, block))
+	    case GIMPLE_MODIFY_STMT:
+	      {
+		tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
+		if (!ann->has_volatile_ops
+		    && !tree_could_throw_p (stmt))
+		  {
+		    pre_expr result = NULL;
+		    switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
+		      {
+		      case tcc_unary:
+			if (is_exception_related (rhs))
+			  continue;
+		      case tcc_binary:
+			{
+			  vn_nary_op_t nary;
+			  unsigned int i;
+			  
+			  vn_nary_op_lookup (rhs, &nary);
+
+			  if (!nary)
+			    continue;
+
+			  for (i = 0; i < nary->length; i++)
+			    if (TREE_CODE (nary->op[i]) == SSA_NAME)
+			      add_to_exp_gen (block, nary->op[i]);
+			  
+			  result = (pre_expr) pool_alloc (pre_expr_pool);
+			  result->kind = NARY;
+			  result->id = 0;
+			  PRE_EXPR_NARY (result) = nary;
+			}
+			break;
+		      case tcc_vl_exp:
+			if (!can_value_number_call (rhs))
+			  continue;
+
+		      case tcc_declaration:
+		      case tcc_reference:
+			{
+			  vn_reference_t ref;
+			  unsigned int i;
+			  vn_reference_op_t vro;
+
+			  vn_reference_lookup (rhs,
+					       shared_vuses_from_stmt (stmt),
+					       true, &ref);
+			  if (!ref)
+			    continue;
+
+			  for (i = 0; VEC_iterate (vn_reference_op_s,
+						   ref->operands, i,
+						   vro); i++)
+			    {
+			      if (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME)
+				add_to_exp_gen (block, vro->op0);
+			      if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
+				add_to_exp_gen (block, vro->op1);
+			    }
+			  result = (pre_expr) pool_alloc (pre_expr_pool);
+			  result->kind = REFERENCE;
+			  result->id = 0;
+			  PRE_EXPR_REFERENCE (result) = ref;
+			}
+			break;
+		      default:
+			{
+			  /* For any other statement that we don't
+			     recognize, simply add all referenced
+			     SSA_NAMEs to EXP_GEN.  */
+			  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
+			    add_to_exp_gen (block, op);
+			  continue;
+			}
+		      }
+		    get_or_alloc_expression_id (result);
+		    add_to_value (get_expr_value_id (result), result);
+		    if (!in_fre)
+		      {
+			bitmap_value_insert_into_set (EXP_GEN (block),
+						      result);
+			bitmap_value_insert_into_set (maximal_set, result);
+		      }  
+
+		  }
 		continue;
-	    }
-
-	  /* For any other statement that we don't recognize, simply
-	     make the names generated by the statement available in
-	     AVAIL_OUT and TMP_GEN.  */
-	  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
-	    add_to_sets (op, op, NULL, TMP_GEN (block), AVAIL_OUT (block));
-
-	  /* Also add all referenced SSA_NAMEs to EXP_GEN.  */
-	  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
-	    add_to_exp_gen (block, op);
-	}
-
+	      }
+	    default:
+	      break;
+	      
+	    }
+	  
+
+	}
       /* Put the dominator children of BLOCK on the worklist of blocks
 	 to compute available sets for.  */
       for (son = first_dom_son (CDI_DOMINATORS, block);
@@ -3648,18 +3715,21 @@
   basic_block bb = bb_for_stmt (stmt);
   block_stmt_iterator bsi;
   tree expr, stmts;
+  pre_expr e;
 
   /* First create a value expression from the expression we want
      to insert and associate it with the value handle for SSA_VN.  */
-  expr = create_value_expr_from (VN_INFO (ssa_vn)->expr, bb, NULL, true);
-  if (expr == NULL_TREE)
+  
+  /* TODO: Handle complex expressions.  */
+  e = get_or_alloc_expr_for (VN_INFO (ssa_vn)->expr);
+  if (e == NULL)
     return NULL_TREE;
-  set_value_handle (expr, get_value_handle (ssa_vn));
-
-  /* Then use create_expression_by_pieces to generate a valid
+
+/* Then use create_expression_by_pieces to generate a valid
      expression to insert at this point of the IL stream.  */
   stmts = alloc_stmt_list ();
-  expr = create_expression_by_pieces (bb, expr, stmts, stmt);
+  expr = create_expression_by_pieces (bb, e, stmts, stmt,
+				      NULL);
   if (expr == NULL_TREE)
     return NULL_TREE;
   bsi = bsi_for_stmt (stmt);
@@ -3695,24 +3765,56 @@
 	    {
 	      tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
 	      tree *rhs_p = &GIMPLE_STMT_OPERAND (stmt, 1);
-	      tree sprime;
-
-	      sprime = bitmap_find_leader (AVAIL_OUT (b),
-					   get_value_handle (lhs), NULL_TREE);
-
+	      tree sprime = NULL;
+	      pre_expr lhsexpr = get_or_alloc_expr_for_name (lhs);
+	      pre_expr sprimeexpr;
+
+	      sprimeexpr = bitmap_find_leader (AVAIL_OUT (b),
+					       get_expr_value_id (lhsexpr),
+					       NULL_TREE);
+
+	      if (sprimeexpr)
+		{
+		  if (sprimeexpr->kind == CONSTANT)
+		    sprime = PRE_EXPR_CONSTANT (sprimeexpr);
+		  else if (sprimeexpr->kind == NAME)
+		    sprime = PRE_EXPR_NAME (sprimeexpr);
+		  else
+		    gcc_unreachable ();
+		}
+	      /* If there is no existing leader but SCCVN knows this
+		 value is constant, use that constant.  */
+	      if (!sprime && is_gimple_min_invariant (VN_INFO (lhs)->valnum))
+		{
+		  sprime = VN_INFO (lhs)->valnum;
+
+		  if (dump_file && (dump_flags & TDF_DETAILS))
+		    {
+		      fprintf (dump_file, "Replaced ");
+		      print_generic_expr (dump_file, *rhs_p, 0);
+		      fprintf (dump_file, " with ");
+		      print_generic_expr (dump_file, sprime, 0);
+		      fprintf (dump_file, " in ");
+		      print_generic_stmt (dump_file, stmt, 0);
+		    }
+		  pre_stats.eliminations++;
+		  propagate_tree_value (rhs_p, sprime);
+		  update_stmt (stmt);
+		  continue;
+		}
+	      
 	      /* If there is no existing usable leader but SCCVN thinks
 		 it has an expression it wants to use as replacement,
 		 insert that.  */
-	      if (!sprime
-		  || sprime == lhs)
+	      if (!sprime || sprime == lhs)
 		{
 		  tree val = VN_INFO (lhs)->valnum;
 		  if (val != VN_TOP
+		      && TREE_CODE (val) == SSA_NAME
 		      && VN_INFO (val)->needs_insertion
 		      && can_PRE_operation (VN_INFO (val)->expr))
 		    sprime = do_SCCVN_insertion (stmt, val);
 		}
-
 	      if (sprime
 		  && sprime != lhs
 		  && (TREE_CODE (*rhs_p) != SSA_NAME
@@ -3921,6 +4023,15 @@
 static void
 init_pre (bool do_fre)
 {
+  basic_block bb;
+
+  next_expression_id = 1;
+  expressions = NULL;
+  VEC_safe_push (pre_expr, heap, expressions, NULL);
+  value_expressions = VEC_alloc (bitmap_set_t, heap, get_max_value_id () + 1);
+  VEC_safe_grow_cleared (bitmap_set_t, heap, value_expressions,
+			 get_max_value_id() + 1);
+  
   in_fre = do_fre;
 
   inserted_exprs = NULL;
@@ -3929,85 +4040,108 @@
   storetemp = NULL_TREE;
   prephitemp = NULL_TREE;
 
+  connect_infinite_loops_to_exit ();
+  memset (&pre_stats, 0, sizeof (pre_stats));
+
+
+  postorder = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
+  post_order_compute (postorder, false, false);
+
+  FOR_ALL_BB (bb)
+    bb->aux = XCNEWVEC (struct bb_bitmap_sets, 1);
+
+  calculate_dominance_info (CDI_POST_DOMINATORS);
+  calculate_dominance_info (CDI_DOMINATORS);
+
+  bitmap_obstack_initialize (&grand_bitmap_obstack);
+  phi_translate_table = htab_create (5110, expr_pred_trans_hash,
+				     expr_pred_trans_eq, free);
+  expression_to_id = htab_create (num_ssa_names * 3,
+				  pre_expr_hash,
+				  pre_expr_eq, NULL);
+  seen_during_translate = BITMAP_ALLOC (&grand_bitmap_obstack);
+  bitmap_set_pool = create_alloc_pool ("Bitmap sets",
+				       sizeof (struct bitmap_set), 30);
+  pre_expr_pool = create_alloc_pool ("pre_expr nodes",
+				     sizeof (struct pre_expr_d), 30);
+  FOR_ALL_BB (bb)
+    {
+      EXP_GEN (bb) = bitmap_set_new ();
+      PHI_GEN (bb) = bitmap_set_new ();
+      TMP_GEN (bb) = bitmap_set_new ();
+      AVAIL_OUT (bb) = bitmap_set_new ();
+    }
+  maximal_set = in_fre ? NULL : bitmap_set_new ();
+
+  need_eh_cleanup = BITMAP_ALLOC (NULL);
+}
+
+
+/* Deallocate data structures used by PRE.  */
+
+static void
+fini_pre (void)
+{
+  basic_block bb;
+
+  free (postorder);
+  VEC_free (bitmap_set_t, heap, value_expressions);
+  VEC_free (tree, heap, inserted_exprs);
+  VEC_free (tree, heap, need_creation);
+  bitmap_obstack_release (&grand_bitmap_obstack);
+  free_alloc_pool (bitmap_set_pool);
+  free_alloc_pool (pre_expr_pool);
+  htab_delete (phi_translate_table);
+  htab_delete (expression_to_id);
+  remove_fake_exit_edges ();
+  
+  FOR_ALL_BB (bb)
+    {
+      free (bb->aux);
+      bb->aux = NULL;
+    }
+
+  free_dominance_info (CDI_POST_DOMINATORS);
+
+  if (!bitmap_empty_p (need_eh_cleanup))
+    {
+      tree_purge_all_dead_eh_edges (need_eh_cleanup);
+      cleanup_tree_cfg ();
+    }
+
+  BITMAP_FREE (need_eh_cleanup);
+
+  if (current_loops != NULL)
+    loop_optimizer_finalize ();
+}
+
+/* Main entry point to the SSA-PRE pass.  DO_FRE is true if the caller
+   only wants to do full redundancy elimination.  */
+
+static unsigned int
+execute_pre (bool do_fre)
+{
+  unsigned int todo = 0;
+
+  do_partial_partial = optimize > 2;
+
+  /* This has to happen before SCCVN runs because
+     loop_optimizer_init may create new phis, etc.  */
   if (!do_fre)
     loop_optimizer_init (LOOPS_NORMAL);
-
-  connect_infinite_loops_to_exit ();
-  memset (&pre_stats, 0, sizeof (pre_stats));
-
-  calculate_dominance_info (CDI_POST_DOMINATORS);
-  calculate_dominance_info (CDI_DOMINATORS);
-
-  init_antic ();
-
-  need_eh_cleanup = BITMAP_ALLOC (NULL);
-}
-
-
-/* Deallocate data structures used by PRE.  */
-
-static void
-fini_pre (void)
-{
-  unsigned int i;
-
-  VEC_free (tree, heap, inserted_exprs);
-  VEC_free (tree, heap, need_creation);
-  htab_delete (phi_translate_table);
-  remove_fake_exit_edges ();
-
-  fini_antic ();
-
-  free_dominance_info (CDI_POST_DOMINATORS);
-
-  if (!bitmap_empty_p (need_eh_cleanup))
-    {
-      tree_purge_all_dead_eh_edges (need_eh_cleanup);
-      cleanup_tree_cfg ();
-    }
-
-  BITMAP_FREE (need_eh_cleanup);
-
-  /* Wipe out pointers to VALUE_HANDLEs.  In the not terribly distant
-     future we will want them to be persistent though.  */
-  for (i = 0; i < num_ssa_names; i++)
-    {
-      tree name = ssa_name (i);
-
-      if (!name)
-	continue;
-
-      if (SSA_NAME_VALUE (name)
-	  && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
-	SSA_NAME_VALUE (name) = NULL;
-    }
-  if (current_loops != NULL)
-    loop_optimizer_finalize ();
-}
-
-/* Main entry point to the SSA-PRE pass.  DO_FRE is true if the caller
-   only wants to do full redundancy elimination.  */
-
-static unsigned int
-execute_pre (bool do_fre)
-{
-  unsigned int todo = 0;
-
-  do_partial_partial = optimize > 2;
-  init_pre (do_fre);
-
-  if (!do_fre)
+  if (0 && !do_fre)
     insert_fake_stores ();
 
-  /* Collect and value number expressions computed in each basic block.  */
   if (!run_scc_vn (do_fre))
     {
       if (!do_fre)
 	remove_dead_inserted_code ();
-      fini_pre ();
       return 0;
     }
-  switch_to_PRE_table ();
+  init_pre (do_fre);
+
+
+  /* Collect and value number expressions computed in each basic block.  */
   compute_avail ();
 
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -4032,16 +4166,6 @@
   if (!do_fre && n_basic_blocks < 4000)
     {
       compute_antic ();
-  if (dump_file && (dump_flags & TDF_DETAILS))
-    {
-      basic_block bb;
-
-      FOR_ALL_BB (bb)
-	{
-	  print_bitmap_set (dump_file, ANTIC_IN (bb), "antic_in", bb->index);
-	}
-    }
-
       insert ();
     }
 
@@ -4060,7 +4184,8 @@
   if (!do_fre)
     {
       remove_dead_inserted_code ();
-      realify_fake_stores ();
+      if (0)
+	realify_fake_stores ();
     }
 
   fini_pre ();
diff -r 2325330ce9ed gcc/tree-ssa-sccvn.c
--- a/gcc/tree-ssa-sccvn.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-ssa-sccvn.c	Sun Jul 06 15:28:19 2008 -0400
@@ -115,72 +115,9 @@
   alloc_pool references_pool;
 } *vn_tables_t;
 
-/* Nary operations in the hashtable consist of length operands, an
-   opcode, and a type.  Result is the value number of the operation,
-   and hashcode is stored to avoid having to calculate it
-   repeatedly.  */
+static htab_t constant_to_value_id;
+static bitmap constant_value_ids;
 
-typedef struct vn_nary_op_s
-{
-  ENUM_BITFIELD(tree_code) opcode : 16;
-  unsigned length : 16;
-  hashval_t hashcode;
-  tree result;
-  tree type;
-  tree op[4];
-} *vn_nary_op_t;
-typedef const struct vn_nary_op_s *const_vn_nary_op_t;
-
-/* Phi nodes in the hashtable consist of their non-VN_TOP phi
-   arguments, and the basic block the phi is in. Result is the value
-   number of the operation, and hashcode is stored to avoid having to
-   calculate it repeatedly.  Phi nodes not in the same block are never
-   considered equivalent.  */
-
-typedef struct vn_phi_s
-{
-  VEC (tree, heap) *phiargs;
-  basic_block block;
-  hashval_t hashcode;
-  tree result;
-} *vn_phi_t;
-typedef const struct vn_phi_s *const_vn_phi_t;
-
-/* Reference operands only exist in reference operations structures.
-   They consist of an opcode, type, and some number of operands.  For
-   a given opcode, some, all, or none of the operands may be used.
-   The operands are there to store the information that makes up the
-   portion of the addressing calculation that opcode performs.  */
-
-typedef struct vn_reference_op_struct
-{
-  enum tree_code opcode;
-  tree type;
-  tree op0;
-  tree op1;
-} vn_reference_op_s;
-typedef vn_reference_op_s *vn_reference_op_t;
-typedef const vn_reference_op_s *const_vn_reference_op_t;
-
-DEF_VEC_O(vn_reference_op_s);
-DEF_VEC_ALLOC_O(vn_reference_op_s, heap);
-
-/* A reference operation in the hashtable is representation as a
-   collection of vuses, representing the memory state at the time of
-   the operation, and a collection of operands that make up the
-   addressing calculation.  If two vn_reference_t's have the same set
-   of operands, they access the same memory location. We also store
-   the resulting value number, and the hashcode.  The vuses are
-   always stored in order sorted by ssa name version.  */
-
-typedef struct vn_reference_s
-{
-  VEC (tree, gc) *vuses;
-  VEC (vn_reference_op_s, heap) *operands;
-  hashval_t hashcode;
-  tree result;
-} *vn_reference_t;
-typedef const struct vn_reference_s *const_vn_reference_t;
 
 /* Valid hashtables storing information we have proven to be
    correct.  */
@@ -215,6 +152,10 @@
 
 tree VN_TOP;
 
+/* Unique counter for our value ids.  */
+
+static unsigned int next_value_id;
+
 /* Next DFS number and the stack for strongly connected component
    detection. */
 
@@ -239,8 +180,10 @@
 vn_ssa_aux_t
 VN_INFO (tree name)
 {
-  return VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
-		    SSA_NAME_VERSION (name));
+  vn_ssa_aux_t res = VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
+				SSA_NAME_VERSION (name));
+  gcc_assert (res);
+  return res;
 }
 
 /* Set the value numbering info for a given SSA name to a given
@@ -288,6 +231,58 @@
 {
   vn_reference_t vr = (vn_reference_t) vp;
   VEC_free (vn_reference_op_s, heap, vr->operands);
+}
+
+/* Hash table equality function for vn_constant_t.  */
+
+static int
+vn_constant_eq (const void *p1, const void *p2)
+{
+  const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
+  const struct vn_constant_s *vc2 = (const struct vn_constant_s *) p2;
+
+  return expressions_equal_p (vc1->constant, vc2->constant);
+}
+
+/* Hash table hash function for vn_constant_t.  */
+   
+static hashval_t
+vn_constant_hash (const void *p1)
+{
+  const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
+  return vc1->hashcode;
+}
+
+/* Lookup a value id for CONSTANT, and if it does not exist, create a
+   new one and return it.  If it does exist, return it.  */
+
+unsigned int
+get_or_alloc_constant_value_id (tree constant)
+{
+  void **slot;
+  vn_constant_t vc = XNEW (struct vn_constant_s);
+  
+  vc->hashcode = iterative_hash_expr (constant, 0);
+  vc->constant = constant;
+  slot = htab_find_slot_with_hash (constant_to_value_id, vc,
+				   vc->hashcode, INSERT);  
+  if (*slot)
+    {
+      free (vc);
+      return ((vn_constant_t)*slot)->value_id;
+    }
+  vc->value_id = get_next_value_id ();
+  *slot = vc;
+  bitmap_set_bit (constant_value_ids, vc->value_id);
+  return vc->value_id;
+}
+
+/* Return true if V is a value id for a constant.  */
+
+bool
+value_id_constant_p (unsigned int v)
+{
+  return bitmap_bit_p (constant_value_ids, v);  
 }
 
 /* Compare two reference operands P1 and P2 for equality.  Return true if
@@ -324,7 +319,7 @@
 
 /* Compute a hash for the reference operation VR1 and return it.  */
 
-static inline hashval_t
+hashval_t
 vn_reference_compute_hash (const vn_reference_t vr1)
 {
   hashval_t result = 0;
@@ -343,7 +338,7 @@
 /* Return true if reference operations P1 and P2 are equivalent.  This
    means they have the same set of operands and vuses.  */
 
-static int
+int
 vn_reference_eq (const void *p1, const void *p2)
 {
   tree v;
@@ -484,10 +479,18 @@
       temp.type = TREE_TYPE (ref);
       temp.opcode = CALL_EXPR;
       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
+      
+      /* We make no attempt to simplify the called function because
+      the typical &FUNCTION_DECL form is also used in function pointer
+      cases that become constant.  If we simplify the original to
+      FUNCTION_DECL but not the function pointer case (which can
+      happen because we have no fold functions that operate on
+      vn_reference_t), we will claim they are not equivalent.
 
-      callfn = get_callee_fndecl (ref);
-      if (!callfn)
-	callfn = CALL_EXPR_FN (ref);
+      An example of this behavior can be see if CALL_EXPR_FN below is
+      replaced with get_callee_fndecl and gcc.dg/tree-ssa/ssa-pre-13.c
+      is compiled.  */
+      callfn = CALL_EXPR_FN (ref);
       temp.type = TREE_TYPE (callfn);
       temp.opcode = TREE_CODE (callfn);
       temp.op0 = callfn;
@@ -554,6 +557,7 @@
 	     a matching type is not necessary and a mismatching type
 	     is always a spurious difference.  */
 	  temp.type = NULL_TREE;
+#if FIXME
 	  /* If this is a reference to a union member, record the union
 	     member size as operand.  Do so only if we are doing
 	     expression insertion (during FRE), as PRE currently gets
@@ -564,14 +568,17 @@
 	      && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1))))
 	    temp.op0 = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)));
 	  else
+#endif
 	    /* Record field as operand.  */
 	    temp.op0 = TREE_OPERAND (ref, 1);
+	    temp.op1 = TREE_OPERAND (ref, 2);	  
 	  break;
 	case ARRAY_RANGE_REF:
 	case ARRAY_REF:
 	  /* Record index as operand.  */
 	  temp.op0 = TREE_OPERAND (ref, 1);
-	  temp.op1 = TREE_OPERAND (ref, 3);
+	  temp.op1 = TREE_OPERAND (ref, 2);
+	  temp.op2 = TREE_OPERAND (ref, 3);
 	  break;
 	case STRING_CST:
 	case INTEGER_CST:
@@ -579,7 +586,6 @@
 	case VECTOR_CST:
 	case REAL_CST:
 	case CONSTRUCTOR:
-	case VALUE_HANDLE:
 	case VAR_DECL:
 	case PARM_DECL:
 	case CONST_DECL:
@@ -653,7 +659,14 @@
     {
       if (vro->opcode == SSA_NAME
 	  || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
-	vro->op0 = SSA_VAL (vro->op0);
+	{
+	  vro->op0 = SSA_VAL (vro->op0);
+	  /* If it transforms from an SSA_NAME to a constant, update
+	     the opcode.  */
+	  if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
+	    vro->opcode = TREE_CODE (vro->op0);
+	}
+      
     }
 
   return orig;
@@ -733,10 +746,11 @@
 
 /* Lookup a SCCVN reference operation VR in the current hash table.
    Returns the resulting value number if it exists in the hash table,
-   NULL_TREE otherwise.  */
+   NULL_TREE otherwise.  VNRESULT will be filled in with the actual
+   vn_reference_t stored in the hashtable if something is found.  */
 
 static tree
-vn_reference_lookup_1 (vn_reference_t vr)
+vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
 {
   void **slot;
   hashval_t hash;
@@ -748,25 +762,58 @@
     slot = htab_find_slot_with_hash (valid_info->references, vr,
 				     hash, NO_INSERT);
   if (slot)
-    return ((vn_reference_t)*slot)->result;
-
+    {
+      if (vnresult)
+	*vnresult = (vn_reference_t)*slot;
+      return ((vn_reference_t)*slot)->result;
+    }
+  
   return NULL_TREE;
 }
 
-/* Lookup OP in the current hash table, and return the resulting
-   value number if it exists in the hash table.  Return NULL_TREE if
-   it does not exist in the hash table. */
+
+/* Lookup a reference operation by it's parts, in the current hash table.
+   Returns the resulting value number if it exists in the hash table,
+   NULL_TREE otherwise.  VNRESULT will be filled in with the actual
+   vn_reference_t stored in the hashtable if something is found.  */
 
 tree
-vn_reference_lookup (tree op, VEC (tree, gc) *vuses, bool maywalk)
+vn_reference_lookup_pieces (VEC (tree, gc) *vuses,
+			    VEC (vn_reference_op_s, heap) *operands,
+			    vn_reference_t *vnresult) 
+{
+  struct vn_reference_s vr1;
+  tree result;
+  if (vnresult)
+    *vnresult = NULL;
+  
+  vr1.vuses = valueize_vuses (vuses);
+  vr1.operands = valueize_refs (operands);
+  vr1.hashcode = vn_reference_compute_hash (&vr1);
+  result = vn_reference_lookup_1 (&vr1, vnresult);
+
+  return result;
+}
+
+/* Lookup OP in the current hash table, and return the resulting value
+   number if it exists in the hash table.  Return NULL_TREE if it does
+   not exist in the hash table or if the result field of the structure
+   was NULL..  VNRESULT will be filled in with the vn_reference_t
+   stored in the hashtable if one exists.  */
+
+tree
+vn_reference_lookup (tree op, VEC (tree, gc) *vuses, bool maywalk,
+		     vn_reference_t *vnresult)
 {
   struct vn_reference_s vr1;
   tree result, def_stmt;
+  if (vnresult)
+    *vnresult = NULL;
 
   vr1.vuses = valueize_vuses (vuses);
   vr1.operands = valueize_refs (shared_reference_ops_from_ref (op));
   vr1.hashcode = vn_reference_compute_hash (&vr1);
-  result = vn_reference_lookup_1 (&vr1);
+  result = vn_reference_lookup_1 (&vr1, vnresult);
 
   /* If there is a single defining statement for all virtual uses, we can
      use that, following virtual use-def chains.  */
@@ -786,23 +833,27 @@
       vdefs_to_vec (def_stmt, &vuses);
       vr1.vuses = valueize_vuses (vuses);
       vr1.hashcode = vn_reference_compute_hash (&vr1);
-      result = vn_reference_lookup_1 (&vr1);
+      result = vn_reference_lookup_1 (&vr1, vnresult);
     }
 
   return result;
 }
 
+
 /* Insert OP into the current hash table with a value number of
-   RESULT.  */
+   RESULT, and return the resulting reference structure we created.  */
 
-void
+vn_reference_t
 vn_reference_insert (tree op, tree result, VEC (tree, gc) *vuses)
 {
   void **slot;
   vn_reference_t vr1;
 
   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
-
+  if (TREE_CODE (result) == SSA_NAME)
+    vr1->value_id = VN_INFO (result)->value_id;
+  else
+    vr1->value_id = get_or_alloc_constant_value_id (result);
   vr1->vuses = valueize_vuses (vuses);
   vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
   vr1->hashcode = vn_reference_compute_hash (vr1);
@@ -824,11 +875,48 @@
     free_reference (*slot);
 
   *slot = vr1;
+  return vr1;
+}
+
+/* Insert a reference by it's pieces into the current hash table with
+   a value number of RESULT.  Return the resulting reference
+   structure we created.  */
+
+vn_reference_t
+vn_reference_insert_pieces (VEC (tree, gc) *vuses,
+			    VEC (vn_reference_op_s, heap) *operands,
+			    tree result, unsigned int value_id)
+
+{
+  void **slot;
+  vn_reference_t vr1;
+
+  vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
+  vr1->value_id =  value_id;
+  vr1->vuses = valueize_vuses (vuses);
+  vr1->operands = valueize_refs (operands);
+  vr1->hashcode = vn_reference_compute_hash (vr1);
+  if (result && TREE_CODE (result) == SSA_NAME)
+    result = SSA_VAL (result);
+  vr1->result = result;
+
+  slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
+				   INSERT);
+  
+  /* At this point we should have all the things inserted that we have
+  seen before, and we should never try inserting something that
+  already exists.  */
+  gcc_assert (!*slot);
+  if (*slot)
+    free_reference (*slot);
+
+  *slot = vr1;
+  return vr1;
 }
 
 /* Compute and return the hash value for nary operation VBO1.  */
 
-static inline hashval_t
+inline hashval_t
 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
 {
   hashval_t hash = 0;
@@ -865,7 +953,7 @@
 /* Compare nary operations P1 and P2 and return true if they are
    equivalent.  */
 
-static int
+int
 vn_nary_op_eq (const void *p1, const void *p2)
 {
   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
@@ -883,17 +971,56 @@
   return true;
 }
 
-/* Lookup OP in the current hash table, and return the resulting
-   value number if it exists in the hash table.  Return NULL_TREE if
-   it does not exist in the hash table. */
+/* Lookup a n-ary operation by its pieces and return the resulting value
+   number if it exists in the hash table.  Return NULL_TREE if it does
+   not exist in the hash table or if the result field of the operation
+   is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
+   if it exists.  */
 
 tree
-vn_nary_op_lookup (tree op)
+vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
+			  tree type, tree op0, tree op1, tree op2,
+			  tree op3, vn_nary_op_t *vnresult) 
+{
+  void **slot;
+  struct vn_nary_op_s vno1;
+  if (vnresult)
+    *vnresult = NULL;
+  vno1.opcode = code;
+  vno1.length = length;
+  vno1.type = type;
+  vno1.op[0] = op0;
+  vno1.op[1] = op1;
+  vno1.op[2] = op2;
+  vno1.op[3] = op3;
+  vno1.hashcode = vn_nary_op_compute_hash (&vno1);
+  slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
+				   NO_INSERT);
+  if (!slot && current_info == optimistic_info)
+    slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
+				     NO_INSERT);
+  if (!slot)
+    return NULL_TREE;
+  if (vnresult)
+    *vnresult = (vn_nary_op_t)*slot;
+  return ((vn_nary_op_t)*slot)->result;
+}
+
+/* Lookup OP in the current hash table, and return the resulting value
+   number if it exists in the hash table.  Return NULL_TREE if it does
+   not exist in the hash table or if the result field of the operation
+   is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
+   if it exists.  */
+
+tree
+vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
 {
   void **slot;
   struct vn_nary_op_s vno1;
   unsigned i;
 
+  if (vnresult)
+    *vnresult = NULL;
   vno1.opcode = TREE_CODE (op);
   vno1.length = TREE_CODE_LENGTH (TREE_CODE (op));
   vno1.type = TREE_TYPE (op);
@@ -907,13 +1034,56 @@
 				     NO_INSERT);
   if (!slot)
     return NULL_TREE;
+  if (vnresult)
+    *vnresult = (vn_nary_op_t)*slot;
   return ((vn_nary_op_t)*slot)->result;
 }
 
+/* Insert a n-ary operation into the current hash table using it's
+   pieces.  Return the vn_nary_op_t structure we created and put in
+   the hashtable.  */
+
+vn_nary_op_t
+vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
+			  tree type, tree op0,
+			  tree op1, tree op2, tree op3,
+			  tree result,
+			  unsigned int value_id) 
+{
+  void **slot;
+  vn_nary_op_t vno1;
+
+  vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
+				       (sizeof (struct vn_nary_op_s)
+					- sizeof (tree) * (4 - length)));
+  vno1->value_id = value_id;
+  vno1->opcode = code;
+  vno1->length = length;
+  vno1->type = type;
+  if (length >= 1)
+    vno1->op[0] = op0;
+  if (length >= 2)
+    vno1->op[1] = op1;
+  if (length >= 3)
+    vno1->op[2] = op2;
+  if (length >= 4)
+    vno1->op[3] = op3;
+  vno1->result = result;
+  vno1->hashcode = vn_nary_op_compute_hash (vno1);
+  slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
+				   INSERT);
+  gcc_assert (!*slot);
+
+  *slot = vno1;
+  return vno1;
+  
+}
+
 /* Insert OP into the current hash table with a value number of
-   RESULT.  */
+   RESULT.  Return the vn_nary_op_t structure we created and put in
+   the hashtable.  */
 
-void
+vn_nary_op_t
 vn_nary_op_insert (tree op, tree result)
 {
   unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
@@ -924,6 +1094,7 @@
   vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
 			(sizeof (struct vn_nary_op_s)
 			 - sizeof (tree) * (4 - length)));
+  vno1->value_id = VN_INFO (result)->value_id;
   vno1->opcode = TREE_CODE (op);
   vno1->length = length;
   vno1->type = TREE_TYPE (op);
@@ -936,6 +1107,7 @@
   gcc_assert (!*slot);
 
   *slot = vno1;
+  return vno1;
 }
 
 /* Compute a hashcode for PHI operation VP1 and return it.  */
@@ -1034,7 +1206,7 @@
 /* Insert PHI into the current hash table with a value number of
    RESULT.  */
 
-static void
+static vn_phi_t
 vn_phi_insert (tree phi, tree result)
 {
   void **slot;
@@ -1049,6 +1221,7 @@
       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
       VEC_safe_push (tree, heap, args, def);
     }
+  vp1->value_id = VN_INFO (result)->value_id;
   vp1->phiargs = args;
   vp1->block = bb_for_stmt (phi);
   vp1->result = result;
@@ -1060,6 +1233,7 @@
   /* Because we iterate over phi operations more than once, it's
      possible the slot might already exist here, hence no assert.*/
   *slot = vp1;
+  return vp1;
 }
 
 
@@ -1168,7 +1342,7 @@
 visit_unary_op (tree lhs, tree op)
 {
   bool changed = false;
-  tree result = vn_nary_op_lookup (op);
+  tree result = vn_nary_op_lookup (op, NULL);
 
   if (result)
     {
@@ -1190,7 +1364,7 @@
 visit_binary_op (tree lhs, tree op)
 {
   bool changed = false;
-  tree result = vn_nary_op_lookup (op);
+  tree result = vn_nary_op_lookup (op, NULL);
 
   if (result)
     {
@@ -1212,7 +1386,8 @@
 visit_reference_op_load (tree lhs, tree op, tree stmt)
 {
   bool changed = false;
-  tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt), true);
+  tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt), true,
+				     NULL);
 
   /* We handle type-punning through unions by value-numbering based
      on offset and size of the access.  Be prepared to handle a
@@ -1236,7 +1411,7 @@
       result = val;
       if (!is_gimple_min_invariant (val)
 	  && TREE_CODE (val) != SSA_NAME)
-	result = vn_nary_op_lookup (val);
+	result = vn_nary_op_lookup (val, NULL);
       /* If the expression is not yet available, value-number lhs to
 	 a new SSA_NAME we create.  */
       if (!result && may_insert)
@@ -1319,7 +1494,8 @@
      Otherwise, the vdefs for the store are used when inserting into
      the table, since the store generates a new memory state.  */
 
-  result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt), false);
+  result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt), false,
+				NULL);
 
   if (result)
     {
@@ -2146,12 +2322,18 @@
 
   calculate_dominance_info (CDI_DOMINATORS);
   sccstack = NULL;
+  constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
+				  free);
+  
+  constant_value_ids = BITMAP_ALLOC (NULL);
+  
   next_dfs_num = 1;
-
+  next_value_id = 1;
+  
   vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
   /* VEC_alloc doesn't actually grow it to the right size, it just
      preallocates the space to do so.  */
-  VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
+  VEC_safe_grow_cleared (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
   gcc_obstack_init (&vn_ssa_aux_obstack);
 
   shared_lookup_phiargs = NULL;
@@ -2180,6 +2362,7 @@
 	{
 	  VN_INFO_GET (name)->valnum = VN_TOP;
 	  VN_INFO (name)->expr = name;
+	  VN_INFO (name)->value_id = 0;
 	}
     }
 
@@ -2206,6 +2389,8 @@
 {
   size_t i;
 
+  htab_delete (constant_to_value_id);
+  BITMAP_FREE (constant_value_ids);
   VEC_free (tree, heap, shared_lookup_phiargs);
   VEC_free (tree, gc, shared_lookup_vops);
   VEC_free (vn_reference_op_s, heap, shared_lookup_references);
@@ -2215,8 +2400,7 @@
     {
       tree name = ssa_name (i);
       if (name
-	  && SSA_NAME_VALUE (name)
-	  && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
+	  && SSA_NAME_VALUE (name))
 	SSA_NAME_VALUE (name) = NULL;
       if (name
 	  && VN_INFO (name)->needs_insertion)
@@ -2237,6 +2421,91 @@
     }
 }
 
+/* Set the value ids in the valid/optimistic hash tables.  */
+
+static void
+set_hashtable_value_ids (void)
+{
+  htab_iterator hi;
+  vn_nary_op_t vno;
+  vn_reference_t vr;
+  vn_phi_t vp;
+  
+  /* Now set the value ids of the things we had put in the hash
+     table.  */
+
+  FOR_EACH_HTAB_ELEMENT (valid_info->nary,
+			 vno, vn_nary_op_t, hi) 
+    {
+      if (vno->result)
+	{
+	  if (TREE_CODE (vno->result) == SSA_NAME)
+	    vno->value_id = VN_INFO (vno->result)->value_id;
+	  else if (is_gimple_min_invariant (vno->result))
+	    vno->value_id = get_or_alloc_constant_value_id (vno->result);
+	}
+    }
+
+  FOR_EACH_HTAB_ELEMENT (optimistic_info->nary,
+			 vno, vn_nary_op_t, hi) 
+    {
+      if (vno->result)
+	{
+	  if (TREE_CODE (vno->result) == SSA_NAME)
+	    vno->value_id = VN_INFO (vno->result)->value_id;
+	  else if (is_gimple_min_invariant (vno->result))
+	    vno->value_id = get_or_alloc_constant_value_id (vno->result);
+	}
+    }
+
+  FOR_EACH_HTAB_ELEMENT (valid_info->phis,
+			 vp, vn_phi_t, hi) 
+    {
+      if (vp->result)
+	{
+	  if (TREE_CODE (vp->result) == SSA_NAME)
+	    vp->value_id = VN_INFO (vp->result)->value_id;
+	  else if (is_gimple_min_invariant (vp->result))
+	    vp->value_id = get_or_alloc_constant_value_id (vp->result);
+	}
+    }
+  FOR_EACH_HTAB_ELEMENT (optimistic_info->phis,
+			 vp, vn_phi_t, hi) 
+    {
+      if (vp->result)
+	{
+	  if (TREE_CODE (vp->result) == SSA_NAME)
+	    vp->value_id = VN_INFO (vp->result)->value_id;
+	  else if (is_gimple_min_invariant (vp->result))
+	    vp->value_id = get_or_alloc_constant_value_id (vp->result);
+	}
+    }
+
+
+  FOR_EACH_HTAB_ELEMENT (valid_info->references,
+			 vr, vn_reference_t, hi) 
+    {
+      if (vr->result)
+	{
+	  if (TREE_CODE (vr->result) == SSA_NAME)
+	    vr->value_id = VN_INFO (vr->result)->value_id;
+	  else if (is_gimple_min_invariant (vr->result))
+	    vr->value_id = get_or_alloc_constant_value_id (vr->result);
+	}
+    }
+  FOR_EACH_HTAB_ELEMENT (optimistic_info->references,
+			 vr, vn_reference_t, hi) 
+    {
+      if (vr->result)
+	{
+	  if (TREE_CODE (vr->result) == SSA_NAME)
+	    vr->value_id = VN_INFO (vr->result)->value_id;
+	  else if (is_gimple_min_invariant (vr->result))
+	    vr->value_id = get_or_alloc_constant_value_id (vr->result);
+	}
+    }
+}
+
 /* Do SCCVN.  Returns true if it finished, false if we bailed out
    due to resource constraints.  */
 
@@ -2245,7 +2514,8 @@
 {
   size_t i;
   tree param;
-
+  bool changed = true;
+  
   may_insert = may_insert_arg;
 
   init_scc_vn ();
@@ -2276,6 +2546,45 @@
 	  }
     }
 
+  /* Initialize the value ids.  */
+      
+  for (i = 1; i < num_ssa_names; ++i)
+    {
+      tree name = ssa_name (i);
+      vn_ssa_aux_t info;
+      if (!name)
+	continue;
+      info = VN_INFO (name);
+      if (info->valnum == name)
+	info->value_id = get_next_value_id ();
+      else if (is_gimple_min_invariant (info->valnum))
+	info->value_id = get_or_alloc_constant_value_id (info->valnum);
+    }
+  
+  /* Propagate until they stop changing.  */
+  while (changed)
+    {
+      changed = false;
+      for (i = 1; i < num_ssa_names; ++i)
+	{
+	  tree name = ssa_name (i);
+	  vn_ssa_aux_t info;
+	  if (!name)
+	    continue;
+	  info = VN_INFO (name);
+	  if (TREE_CODE (info->valnum) == SSA_NAME
+	      && info->valnum != name
+	      && TREE_CODE (info->valnum) == SSA_NAME
+	      && info->value_id != VN_INFO (info->valnum)->value_id)
+	    {
+	      changed = true;
+	      info->value_id = VN_INFO (info->valnum)->value_id;
+	    }
+	}
+    }
+  
+  set_hashtable_value_ids ();
+  
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
       fprintf (dump_file, "Value numbers:\n");
@@ -2300,3 +2609,83 @@
   may_insert = false;
   return true;
 }
+
+/* Return the maximum value id we have ever seen.  */
+
+unsigned int
+get_max_value_id (void) 
+{
+  return next_value_id;
+}
+
+/* Return the next unique value id.  */
+
+unsigned int
+get_next_value_id (void)
+{
+  return next_value_id++;
+}
+
+
+/* Compare two expressions E1 and E2 and return true if they are
+   equal.  */
+
+bool
+expressions_equal_p (tree e1, tree e2)
+{
+  tree te1, te2;
+
+  if (e1 == e2)
+    return true;
+
+  te1 = TREE_TYPE (e1);
+  te2 = TREE_TYPE (e2);
+
+  if (TREE_CODE (e1) == TREE_LIST && TREE_CODE (e2) == TREE_LIST)
+    {
+      tree lop1 = e1;
+      tree lop2 = e2;
+      for (lop1 = e1, lop2 = e2;
+	   lop1 || lop2;
+	   lop1 = TREE_CHAIN (lop1), lop2 = TREE_CHAIN (lop2))
+	{
+	  if (!lop1 || !lop2)
+	    return false;
+	  if (!expressions_equal_p (TREE_VALUE (lop1), TREE_VALUE (lop2)))
+	    return false;
+	}
+      return true;
+
+    }
+  else if (TREE_CODE (e1) == TREE_CODE (e2)
+	   && operand_equal_p (e1, e2, OEP_PURE_SAME))
+    return true;
+
+  return false;
+}
+
+/* Sort the VUSE array so that we can do equality comparisons
+   quicker on two vuse vecs.  */
+
+void
+sort_vuses (VEC (tree,gc) *vuses)
+{
+  if (VEC_length (tree, vuses) > 1)
+    qsort (VEC_address (tree, vuses),
+	   VEC_length (tree, vuses),
+	   sizeof (tree),
+	   operand_build_cmp);
+}
+
+/* Sort the VUSE array so that we can do equality comparisons
+   quicker on two vuse vecs.  */
+
+void
+sort_vuses_heap (VEC (tree,heap) *vuses)
+{
+  if (VEC_length (tree, vuses) > 1)
+    qsort (VEC_address (tree, vuses),
+	   VEC_length (tree, vuses),
+	   sizeof (tree),
+	   operand_build_cmp);
+}
diff -r 2325330ce9ed gcc/tree-ssa-sccvn.h
--- a/gcc/tree-ssa-sccvn.h	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-ssa-sccvn.h	Sun Jul 06 15:28:19 2008 -0400
@@ -24,12 +24,96 @@
 /* TOP of the VN lattice.  */
 extern tree VN_TOP;
 
+/* N-ary operations in the hashtable consist of length operands, an
+   opcode, and a type.  Result is the value number of the operation,
+   and hashcode is stored to avoid having to calculate it
+   repeatedly.  */
+
+typedef struct vn_nary_op_s
+{
+  /* Unique identify that all expressions with the same value have. */
+  unsigned int value_id;
+  ENUM_BITFIELD(tree_code) opcode : 16;
+  unsigned length : 16;
+  hashval_t hashcode;
+  tree result;
+  tree type;
+  tree op[4];
+} *vn_nary_op_t;
+typedef const struct vn_nary_op_s *const_vn_nary_op_t;
+
+/* Phi nodes in the hashtable consist of their non-VN_TOP phi
+   arguments, and the basic block the phi is in. Result is the value
+   number of the operation, and hashcode is stored to avoid having to
+   calculate it repeatedly.  Phi nodes not in the same block are never
+   considered equivalent.  */
+
+typedef struct vn_phi_s
+{
+  /* Unique identifier that all expressions with the same value have. */
+  unsigned int value_id;
+  hashval_t hashcode;
+  VEC (tree, heap) *phiargs;
+  basic_block block;
+  tree result;
+} *vn_phi_t;
+typedef const struct vn_phi_s *const_vn_phi_t;
+
+/* Reference operands only exist in reference operations structures.
+   They consist of an opcode, type, and some number of operands.  For
+   a given opcode, some, all, or none of the operands may be used.
+   The operands are there to store the information that makes up the
+   portion of the addressing calculation that opcode performs.  */
+
+typedef struct vn_reference_op_struct
+{
+  enum tree_code opcode;
+  tree type;
+  tree op0;
+  tree op1;
+  tree op2;
+} vn_reference_op_s;
+typedef vn_reference_op_s *vn_reference_op_t;
+typedef const vn_reference_op_s *const_vn_reference_op_t;
+
+DEF_VEC_O(vn_reference_op_s);
+DEF_VEC_ALLOC_O(vn_reference_op_s, heap);
+
+/* A reference operation in the hashtable is representation as a
+   collection of vuses, representing the memory state at the time of
+   the operation, and a collection of operands that make up the
+   addressing calculation.  If two vn_reference_t's have the same set
+   of operands, they access the same memory location. We also store
+   the resulting value number, and the hashcode.  The vuses are
+   always stored in order sorted by ssa name version.  */
+
+typedef struct vn_reference_s
+{
+  /* Unique identifier that all expressions with the same value have. */
+  unsigned int value_id;
+  hashval_t hashcode;
+  VEC (tree, gc) *vuses;
+  VEC (vn_reference_op_s, heap) *operands;
+  tree result;
+} *vn_reference_t;
+typedef const struct vn_reference_s *const_vn_reference_t;
+
+typedef struct vn_constant_s
+{
+  unsigned int value_id;
+  hashval_t hashcode;
+  tree constant;
+} *vn_constant_t;
+  
 typedef struct vn_ssa_aux
 {
   /* Value number. This may be an SSA name or a constant.  */
   tree valnum;
   /* Representative expression, if not a direct constant. */
   tree expr;
+
+  /* Unique identifier that all expressions with the same value have. */
+  unsigned int value_id;
 
   /* SCC information.  */
   unsigned int dfsnum;
@@ -57,12 +141,31 @@
 bool run_scc_vn (bool);
 void free_scc_vn (void);
 void switch_to_PRE_table (void);
-tree vn_nary_op_lookup (tree);
-void vn_nary_op_insert (tree, tree);
-tree vn_reference_lookup (tree, VEC (tree, gc) *, bool);
-void vn_reference_insert (tree, tree, VEC (tree, gc) *);
+tree vn_nary_op_lookup (tree, vn_nary_op_t *);
+tree vn_nary_op_lookup_pieces (unsigned int, enum tree_code,
+			       tree, tree, tree, tree, tree,
+			       vn_nary_op_t *);
+vn_nary_op_t vn_nary_op_insert (tree, tree);
+vn_nary_op_t vn_nary_op_insert_pieces (unsigned int, enum tree_code,
+				       tree, tree, tree, tree,
+				       tree, tree, unsigned int);
+tree vn_reference_lookup_pieces (VEC (tree, gc) *,
+				 VEC (vn_reference_op_s, heap) *,
+				 vn_reference_t *);
+tree vn_reference_lookup (tree, VEC (tree, gc) *, bool, vn_reference_t *);
+vn_reference_t vn_reference_insert (tree, tree, VEC (tree, gc) *);
+vn_reference_t vn_reference_insert_pieces (VEC (tree, gc) *,
+					   VEC (vn_reference_op_s, heap) *,
+					   tree, unsigned int);
+
+hashval_t vn_nary_op_compute_hash (const vn_nary_op_t);
+int vn_nary_op_eq (const void *, const void *);
+hashval_t vn_reference_compute_hash (const vn_reference_t);
+int vn_reference_eq (const void *, const void *);
+unsigned int get_max_value_id (void);
+unsigned int get_next_value_id (void);
+unsigned int get_or_alloc_constant_value_id (tree);
+bool value_id_constant_p (unsigned int);
 VEC (tree, gc) *shared_vuses_from_stmt (tree);
 VEC (tree, gc) *copy_vuses_from_stmt (tree);
-
-
 #endif /* TREE_SSA_SCCVN_H  */
diff -r 2325330ce9ed gcc/tree-ssa-threadedge.c
--- a/gcc/tree-ssa-threadedge.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree-ssa-threadedge.c	Sun Jul 06 15:28:19 2008 -0400
@@ -313,7 +313,7 @@
 	      copy[i++] = use;
 	      if (TREE_CODE (use) == SSA_NAME)
 		tmp = SSA_NAME_VALUE (use);
-	      if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
+	      if (tmp)
 		SET_USE (use_p, tmp);
 	    }
 
@@ -407,14 +407,14 @@
       if (TREE_CODE (op0) == SSA_NAME)
 	{
           tree tmp = SSA_NAME_VALUE (op0);
-	  if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
+	  if (tmp)
 	    op0 = tmp;
 	}
 
       if (TREE_CODE (op1) == SSA_NAME)
 	{
 	  tree tmp = SSA_NAME_VALUE (op1);
-	  if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
+	  if (tmp)
 	    op1 = tmp;
 	}
 
diff -r 2325330ce9ed gcc/tree-vn.c
--- a/gcc/tree-vn.c	Sun Jul 06 00:16:30 2008 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,406 +0,0 @@
-/* Value Numbering routines for tree expressions.
-   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008 Free Software
-   Foundation, Inc.
-   Contributed by Daniel Berlin <dan@dberlin.org>, Steven Bosscher
-   <stevenb@suse.de> and 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 3, or (at your option)
-any later version.
-
-GCC is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-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 COPYING3.  If not see
-<http://www.gnu.org/licenses/>.  */
-
-#include "config.h"
-#include "system.h"
-#include "coretypes.h"
-#include "tm.h"
-#include "ggc.h"
-#include "tree.h"
-#include "tree-flow.h"
-#include "hashtab.h"
-#include "langhooks.h"
-#include "tree-pass.h"
-#include "tree-dump.h"
-#include "diagnostic.h"
-#include "tree-ssa-sccvn.h"
-
-/* Most of this is PRE specific.  The real grunt work is done in
-   tree-ssa-sccvn.c.  This is where the lookup and insertion
-   functions, etc, can be found.  */
-
-/* Create and return a new value handle node of type TYPE.  */
-
-tree
-make_value_handle (tree type)
-{
-  static unsigned int id = 0;
-  tree vh;
-
-  vh = build0 (VALUE_HANDLE, type);
-  VALUE_HANDLE_ID (vh) = id++;
-  return vh;
-}
-
-/* Compare two expressions E1 and E2 and return true if they are
-   equal.  */
-
-bool
-expressions_equal_p (tree e1, tree e2)
-{
-  tree te1, te2;
-
-  if (e1 == e2)
-    return true;
-
-  te1 = TREE_TYPE (e1);
-  te2 = TREE_TYPE (e2);
-
-  if (TREE_CODE (e1) == TREE_LIST && TREE_CODE (e2) == TREE_LIST)
-    {
-      tree lop1 = e1;
-      tree lop2 = e2;
-      for (lop1 = e1, lop2 = e2;
-	   lop1 || lop2;
-	   lop1 = TREE_CHAIN (lop1), lop2 = TREE_CHAIN (lop2))
-	{
-	  if (!lop1 || !lop2)
-	    return false;
-	  if (!expressions_equal_p (TREE_VALUE (lop1), TREE_VALUE (lop2)))
-	    return false;
-	}
-      return true;
-
-    }
-  else if (TREE_CODE (e1) == TREE_CODE (e2)
-	   && operand_equal_p (e1, e2, OEP_PURE_SAME))
-    return true;
-
-  return false;
-}
-
-/* Set the value handle for expression E to value V.  */
-
-void
-set_value_handle (tree e, tree v)
-{
-  if (TREE_CODE (e) == SSA_NAME)
-    SSA_NAME_VALUE (e) = v;
-  else if (EXPR_P (e) || DECL_P (e) || TREE_CODE (e) == TREE_LIST
-	   || GIMPLE_STMT_P (e)
-	   || TREE_CODE (e) == CONSTRUCTOR)
-    get_tree_common_ann (e)->value_handle = v;
-  else
-    /* Do nothing.  Constants are their own value handles.  */
-    gcc_assert (is_gimple_min_invariant (e));
-}
-
-/* Print out the "Created value <x> for <Y>" statement to the
-   dump_file.
-   This is factored because both versions of lookup use it, and it
-   obscures the real work going on in those functions.  */
-
-static void
-print_creation_to_file (tree v, tree expr, VEC (tree, gc) *vuses)
-{
-  fprintf (dump_file, "Created value ");
-  print_generic_expr (dump_file, v, dump_flags);
-  fprintf (dump_file, " for ");
-  print_generic_expr (dump_file, expr, dump_flags);
-
-  if (vuses && VEC_length (tree, vuses) != 0)
-    {
-      size_t i;
-      tree vuse;
-
-      fprintf (dump_file, " vuses: (");
-      for (i = 0; VEC_iterate (tree, vuses, i, vuse); i++)
-	{
-	  print_generic_expr (dump_file, vuse, dump_flags);
-	  if (VEC_length (tree, vuses) - 1 != i)
-	    fprintf (dump_file, ",");
-	}
-      fprintf (dump_file, ")");
-    }
-  fprintf (dump_file, "\n");
-}
-
-/* Sort the VUSE array so that we can do equality comparisons
-   quicker on two vuse vecs.  */
-
-void
-sort_vuses (VEC (tree,gc) *vuses)
-{
-  if (VEC_length (tree, vuses) > 1)
-    qsort (VEC_address (tree, vuses),
-	   VEC_length (tree, vuses),
-	   sizeof (tree),
-	   operand_build_cmp);
-}
-
-/* Sort the VUSE array so that we can do equality comparisons
-   quicker on two vuse vecs.  */
-
-void
-sort_vuses_heap (VEC (tree,heap) *vuses)
-{
-  if (VEC_length (tree, vuses) > 1)
-    qsort (VEC_address (tree, vuses),
-	   VEC_length (tree, vuses),
-	   sizeof (tree),
-	   operand_build_cmp);
-}
-
-/* Insert EXPR into VALUE_TABLE with value VAL, and add expression
-   EXPR to the value set for value VAL.  */
-
-void
-vn_add (tree expr, tree val)
-{
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_comparison:
-    case tcc_binary:
-      vn_nary_op_insert (expr, val);
-      break;
-    case tcc_unary:
-      vn_nary_op_insert (expr, val);
-      break;
-      /* In the case of array-refs of constants, for example, we can
-	 end up with no vuses.  */
-    case tcc_reference:
-      vn_reference_insert (expr, val, NULL);
-      break;
-      /* The *only* time CALL_EXPR should appear here is
-	 when it has no vuses.  */
-    case tcc_vl_exp:
-    case tcc_exceptional:
-    case tcc_expression:
-    case tcc_declaration:
-      if (TREE_CODE (expr) == CALL_EXPR || DECL_P (expr))
-	{
-	  vn_reference_insert (expr, val, NULL);
-	  break;
-	}
-      else if (TREE_CODE (expr) == SSA_NAME)
-	{
-	  SSA_NAME_VALUE (expr) = val;
-	  break;
-	}
-      switch (TREE_CODE (expr))
-	{
-	case ADDR_EXPR:
-	case TRUTH_AND_EXPR:
-	case TRUTH_OR_EXPR:
-	case TRUTH_XOR_EXPR:
-	case TRUTH_NOT_EXPR:
-	  vn_nary_op_insert (expr, val);
-	    break;
-	default:
-	  gcc_unreachable ();
-	}
-      break;
-    default:
-      gcc_unreachable ();
-    }
-  set_value_handle (expr, val);
-  if (TREE_CODE (val) == VALUE_HANDLE)
-    add_to_value (val, expr);
-}
-
-/* Insert EXPR into the value numbering tables with value VAL, and
-   add expression EXPR to the value set for value VAL.  VUSES
-   represents the virtual use operands associated with EXPR.  It is
-   used when computing a hash value for EXPR.  */
-
-void
-vn_add_with_vuses (tree expr, tree val, VEC (tree, gc) *vuses)
-{
-  if (!vuses)
-    {
-      vn_add (expr, val);
-      return;
-    }
-  vn_reference_insert (expr, val, vuses);
-
-  set_value_handle (expr, val);
-  if (TREE_CODE (val) == VALUE_HANDLE)
-    add_to_value (val, expr);
-}
-
-/* Lookup EXPR in the value numbering tables and return the result, if
-   we have one.  */
-
-tree
-vn_lookup (tree expr)
-{
-  /* Constants are their own value.  */
-  if (is_gimple_min_invariant (expr) || TREE_CODE (expr) == FIELD_DECL)
-    return expr;
-
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_comparison:
-    case tcc_binary:
-      return vn_nary_op_lookup (expr);
-    case tcc_unary:
-      return vn_nary_op_lookup (expr);
-      break;
-      /* In the case of array-refs of constants, for example, we can
-	 end up with no vuses.  */
-    case tcc_reference:
-      return vn_reference_lookup (expr, NULL, false);
-      break;
-      /* It is possible to have CALL_EXPR with no vuses for things
-	 like "cos", and these will fall into vn_lookup.   */
-    case tcc_vl_exp:
-    case tcc_exceptional:
-    case tcc_expression:
-    case tcc_declaration:
-      if (TREE_CODE (expr) == CALL_EXPR || DECL_P (expr))
-	return vn_reference_lookup (expr, NULL, false);
-      else if (TREE_CODE (expr) == SSA_NAME)
-	return SSA_NAME_VALUE (expr);
-      switch (TREE_CODE (expr))
-	{
-	case ADDR_EXPR:
-	case TRUTH_AND_EXPR:
-	case TRUTH_OR_EXPR:
-	case TRUTH_XOR_EXPR:
-	case TRUTH_NOT_EXPR:
-	  return vn_nary_op_lookup (expr);
-	default:
-	  gcc_unreachable ();
-	}
-      break;
-    default:
-      gcc_unreachable ();
-    }
-  return NULL;
-}
-
-/* Search in the value numbering tables for an existing instance of
-   expression EXPR,  and return its value, or NULL if none has been set.  STMT
-   represents the stmt associated with EXPR.  It is used when computing the
-   hash value for EXPR for reference operations.  */
-
-tree
-vn_lookup_with_stmt (tree expr, tree stmt)
-{
-  if (stmt == NULL)
-    return vn_lookup (expr);
-
-  /* Constants are their own value.  */
-  if (is_gimple_min_invariant (expr) || TREE_CODE (expr) == FIELD_DECL)
-    return expr;
-
-  return vn_lookup_with_vuses (expr, shared_vuses_from_stmt (stmt));
-}
-
-/* Search in VALUE_TABLE for an existing instance of expression EXPR,
-   and return its value, or NULL if none has been set.  VUSES is the
-   list of virtual use operands associated with EXPR.  It is used when
-   computing the hash value for EXPR.  */
-
-tree
-vn_lookup_with_vuses (tree expr, VEC (tree, gc) *vuses)
-{
-  if (!vuses || !VEC_length (tree, vuses))
-    return vn_lookup (expr);
-
-  if (is_gimple_min_invariant (expr) || TREE_CODE (expr) == FIELD_DECL)
-    return expr;
-
-  /* We may not walk the use-def chains here as the alias oracle cannot
-     properly deal with VALUE_HANDLE tree nodes we feed it here.  */
-  return vn_reference_lookup (expr, vuses, false);
-}
-
-static tree
-create_value_handle_for_expr (tree expr, VEC(tree, gc) *vuses)
-{
-  tree v;
-
-  v = make_value_handle (TREE_TYPE (expr));
-
-  if (dump_file && (dump_flags & TDF_DETAILS))
-    print_creation_to_file (v, expr, vuses);
-  return v;
-}
-
-/* Like vn_lookup, but creates a new value for the operation if one
-   does not exist.  */
-
-tree
-vn_lookup_or_add (tree expr)
-{
-  tree v = vn_lookup (expr);
-
-  if (v == NULL_TREE)
-    {
-      v = create_value_handle_for_expr (expr, NULL);
-      vn_add (expr, v);
-    }
-  else
-    set_value_handle (expr, v);
-
-  return v;
-}
-
-/* Like vn_lookup, but handles reference operations as well by using
-   STMT to get the set of vuses.  */
-
-tree
-vn_lookup_or_add_with_stmt (tree expr, tree stmt)
-{
-  tree v;
-  if (!stmt)
-    return vn_lookup_or_add (expr);
-
-  v = vn_lookup_with_stmt (expr, stmt);
-  if (v == NULL_TREE)
-    {
-      VEC (tree, gc) *vuses = copy_vuses_from_stmt (stmt);
-      v = create_value_handle_for_expr (expr, vuses);
-      vn_add_with_vuses (expr, v, vuses);
-    }
-  else
-    set_value_handle (expr, v);
-
-  return v;
-}
-
-/* Like vn_lookup, but creates a new value for expression EXPR, if
-   EXPR doesn't already have a value.  Return the existing/created
-   value for EXPR.  STMT represents the stmt associated with EXPR.  It is used
-   when computing the hash value for EXPR.  */
-
-tree
-vn_lookup_or_add_with_vuses (tree expr, VEC (tree, gc) *vuses)
-{
-  tree v;
-
-  if (!vuses || VEC_length (tree, vuses) == 0)
-    return vn_lookup_or_add (expr);
-
-  v = vn_lookup_with_vuses (expr, vuses);
-  if (v == NULL_TREE)
-    {
-      v = create_value_handle_for_expr (expr, vuses);
-      vn_add_with_vuses (expr, v, vuses);
-    }
-  else
-    set_value_handle (expr, v);
-
-  return v;
-}
-
diff -r 2325330ce9ed gcc/tree.c
--- a/gcc/tree.c	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree.c	Sun Jul 06 15:28:19 2008 -0400
@@ -462,7 +462,6 @@
 
 	case STATEMENT_LIST:	return sizeof (struct tree_statement_list);
 	case BLOCK:		return sizeof (struct tree_block);
-	case VALUE_HANDLE:	return sizeof (struct tree_value_handle);
 	case CONSTRUCTOR:	return sizeof (struct tree_constructor);
 
 	default:
@@ -2387,7 +2386,6 @@
     case BLOCK:			return TS_BLOCK;
     case CONSTRUCTOR:		return TS_CONSTRUCTOR;
     case TREE_BINFO:		return TS_BINFO;
-    case VALUE_HANDLE:		return TS_VALUE_HANDLE;
     case OMP_CLAUSE:		return TS_OMP_CLAUSE;
 
     default:
@@ -5358,7 +5356,6 @@
       return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
 
     case SSA_NAME:
-    case VALUE_HANDLE:
       /* we can just compare by pointer.  */
       return iterative_hash_pointer (t, val);
 
diff -r 2325330ce9ed gcc/tree.def
--- a/gcc/tree.def	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree.def	Sun Jul 06 15:28:19 2008 -0400
@@ -925,12 +925,6 @@
    Use the interface in tree-iterator.h to access this node.  */
 DEFTREECODE (STATEMENT_LIST, "statement_list", tcc_exceptional, 0)
 
-/* Value handles.  Artificial nodes to represent expressions in
-   partial redundancy elimination (tree-ssa-pre.c).  These nodes are
-   used for expression canonicalization.  If two expressions compute
-   the same value, they will be assigned the same value handle.  */
-DEFTREECODE (VALUE_HANDLE, "value_handle", tcc_exceptional, 0)
-
 /* Predicate assertion.  Artificial expression generated by the optimizers
    to keep track of predicate values.  This expression may only appear on
    the RHS of assignments.
diff -r 2325330ce9ed gcc/tree.h
--- a/gcc/tree.h	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/tree.h	Sun Jul 06 15:28:19 2008 -0400
@@ -3492,26 +3492,6 @@
   struct tree_statement_list_node *tail;
 };
 
-#define VALUE_HANDLE_ID(NODE)		\
-  (VALUE_HANDLE_CHECK (NODE)->value_handle.id)
-
-#define VALUE_HANDLE_EXPR_SET(NODE)	\
-  (VALUE_HANDLE_CHECK (NODE)->value_handle.expr_set)
-
-/* Defined and used in tree-ssa-pre.c.  */
-
-struct tree_value_handle GTY(())
-{
-  struct tree_common common;
-
-  /* The set of expressions represented by this handle.  */
-  struct bitmap_set * GTY ((skip)) expr_set;
-
-  /* Unique ID for this value handle.  IDs are handed out in a
-     conveniently dense form starting at 0, so that we can make
-     bitmaps of value handles.  */
-  unsigned int id;
-};
 \f
 /* Define the overall contents of a tree node.
    It may be any of the structures declared above
@@ -3552,7 +3532,6 @@
   struct tree_binfo GTY ((tag ("TS_BINFO"))) binfo;
   struct tree_statement_list GTY ((tag ("TS_STATEMENT_LIST"))) stmt_list;
   struct gimple_stmt GTY ((tag ("TS_GIMPLE_STATEMENT"))) gstmt;
-  struct tree_value_handle GTY ((tag ("TS_VALUE_HANDLE"))) value_handle;
   struct tree_constructor GTY ((tag ("TS_CONSTRUCTOR"))) constructor;
   struct tree_memory_tag GTY ((tag ("TS_MEMORY_TAG"))) mtag;
   struct tree_omp_clause GTY ((tag ("TS_OMP_CLAUSE"))) omp_clause;
diff -r 2325330ce9ed gcc/treestruct.def
--- a/gcc/treestruct.def	Sun Jul 06 00:16:30 2008 +0000
+++ b/gcc/treestruct.def	Sun Jul 06 15:28:19 2008 -0400
@@ -59,7 +59,6 @@
 DEFTREESTRUCT(TS_BINFO, "binfo")
 DEFTREESTRUCT(TS_STATEMENT_LIST, "statement list")
 DEFTREESTRUCT(TS_GIMPLE_STATEMENT, "gimple statement")
-DEFTREESTRUCT(TS_VALUE_HANDLE, "value handle")
 DEFTREESTRUCT(TS_CONSTRUCTOR, "constructor")
 DEFTREESTRUCT(TS_MEMORY_TAG, "memory tag")
 DEFTREESTRUCT(TS_OMP_CLAUSE, "omp clause")

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-06 22:12     ` Daniel Berlin
@ 2008-07-07 16:49       ` Daniel Berlin
  2008-07-08 16:42         ` Daniel Berlin
  0 siblings, 1 reply; 16+ messages in thread
From: Daniel Berlin @ 2008-07-07 16:49 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 3049 bytes --]

I've attached the latest, and what i plan on committing, which also
fixes a speed issue Richard found in interpert.ii from libjava.

The differences between thisand the last one is that
iterative_hash_hashval_t is made externally visible in tree.c.



On Sun, Jul 6, 2008 at 3:30 PM, Daniel Berlin <dberlin@dberlin.org> wrote:
> I got it going on my home machine.
> The attached fixes the problem.
>
>
> On Sun, Jul 6, 2008 at 1:46 PM, Daniel Berlin <dberlin@dberlin.org> wrote:
>> On Sun, Jul 6, 2008 at 10:32 AM, Richard Guenther
>> <richard.guenther@gmail.com> wrote:
>>> On Sun, Jul 6, 2008 at 6:33 AM, Daniel Berlin <dberlin@dberlin.org> wrote:
>>>> This patch effectively rewrites tree PRE to share datastructures with
>>>> tree-vn, instead of copying statements and modifying trees (which
>>>> don't work so well in a tuples world :P).
>>>>
>>>> As part of the rewrite, it also fixes a bunch of missed optimizations
>>>> because PRE will now move globals.
>>>> It also uses roughly half as much memory as before (since PRE no
>>>> longer copies statements but instead shares the vn structures with the
>>>> vn).
>>>> The fundamentals are the same as they were before, it simply no longer
>>>> does it's own value numbering.
>>>> Since PRE was the only user of VALUE_HANDLES, i have removed them as
>>>> part of this patch.
>>>> Since PRE was the only user of most of tree-vn.c, i removed it and
>>>> moved the 3 still-used functions to tree-ssa-sccvn.c
>>>> PRE is also now type correct, in that the insertions it creates should
>>>> have matched types, whereas before they were generally a mismash (it
>>>> would happily create phi nodes whose arguments had mixed types, etc).
>>>>
>>>> This patch will cause some FRE failures due to a small bit of code i
>>>> disabled in tree-ssa-sccvn.c.  I have discussed this with richard
>>>> guenther and he has said he will fix it up, so the ssa-fre-* failures
>>>> are temporary.
>>>>
>>>> Other than than PRE of globals, it should PRE all of the expressions
>>>> we used to, plus a few more in the way of loads.  I have verified this
>>>> by hand comparing the eliminations performed during a bootstrap of
>>>> gcc.
>>>> I have not performed more benchmarking than that, as the main goal was
>>>> a feature equivalent rewrite that would enable tuple conversion to
>>>> procede. The fact that it will PRE globals is simply a side effect of
>>>> sharing a representation with the VN.
>>>>
>>>> The patch is large but 99% of it is the rewritten tree-ssa-pre.c.
>>>>
>>>> Bootstrapped and regtested on i686-darwin (With the safe-ctype change
>>>> reverted) and i686-linux.
>>>> I will wait a few days for comments before committing it (All the code
>>>> outside of PRE that I am removing was added as part of PRE, but if
>>>> someone wants to object to it's removal, please let me know).
>>>
>>> This triggers a latent issue in SCCVN (it breaks bootstrap on x86_64 with Ada).
>>>
>>
>> I don't have an ada compiler on my x86_64. If i send you an updated
>> patch, can you test it for me with ada?
>>
>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: newpre.diff --]
[-- Type: text/x-diff; name=newpre.diff, Size: 181325 bytes --]

Index: gcc/dbgcnt.def
===================================================================
--- gcc/dbgcnt.def	(revision 137479)
+++ gcc/dbgcnt.def	(working copy)
@@ -166,6 +166,7 @@ DEBUG_COUNTER (jump_bypass)
 DEBUG_COUNTER (local_alloc_for_sched)
 DEBUG_COUNTER (postreload_cse)
 DEBUG_COUNTER (pre_insn)
+DEBUG_COUNTER (treepre_insert)
 DEBUG_COUNTER (sched2_func)
 DEBUG_COUNTER (sched_block)
 DEBUG_COUNTER (sched_func)
@@ -174,4 +175,3 @@ DEBUG_COUNTER (sched_region)
 DEBUG_COUNTER (sms_sched_loop)
 DEBUG_COUNTER (split_for_sched2)
 DEBUG_COUNTER (tail_call)
-
Index: gcc/tree-pretty-print.c
===================================================================
--- gcc/tree-pretty-print.c	(revision 137479)
+++ gcc/tree-pretty-print.c	(working copy)
@@ -952,16 +952,12 @@ dump_generic_node (pretty_printer *buffe
 	pp_character (buffer, ')');
       pp_string (buffer, str);
       dump_generic_node (buffer, TREE_OPERAND (node, 1), spc, flags, false);
-
-      if (TREE_CODE (op0) != VALUE_HANDLE)
+      op0 = component_ref_field_offset (node);
+      if (op0 && TREE_CODE (op0) != INTEGER_CST)
 	{
-	  op0 = component_ref_field_offset (node);
-	  if (op0 && TREE_CODE (op0) != INTEGER_CST)
-	    {
-	      pp_string (buffer, "{off: ");
+	  pp_string (buffer, "{off: ");
 	      dump_generic_node (buffer, op0, spc, flags, false);
 	      pp_character (buffer, '}');
-	    }
 	}
       break;
 
@@ -1789,10 +1785,6 @@ dump_generic_node (pretty_printer *buffe
       pp_string (buffer, ">");
       break;
 
-    case VALUE_HANDLE:
-      pp_printf (buffer, "VH.%d", VALUE_HANDLE_ID (node));
-      break;
-
     case ASSERT_EXPR:
       pp_string (buffer, "ASSERT_EXPR <");
       dump_generic_node (buffer, ASSERT_EXPR_VAR (node), spc, flags, false);
Index: gcc/tree.c
===================================================================
--- gcc/tree.c	(revision 137479)
+++ gcc/tree.c	(working copy)
@@ -462,7 +462,6 @@ tree_code_size (enum tree_code code)
 
 	case STATEMENT_LIST:	return sizeof (struct tree_statement_list);
 	case BLOCK:		return sizeof (struct tree_block);
-	case VALUE_HANDLE:	return sizeof (struct tree_value_handle);
 	case CONSTRUCTOR:	return sizeof (struct tree_constructor);
 
 	default:
@@ -2387,7 +2386,6 @@ tree_node_structure (const_tree t)
     case BLOCK:			return TS_BLOCK;
     case CONSTRUCTOR:		return TS_CONSTRUCTOR;
     case TREE_BINFO:		return TS_BINFO;
-    case VALUE_HANDLE:		return TS_VALUE_HANDLE;
     case OMP_CLAUSE:		return TS_OMP_CLAUSE;
 
     default:
@@ -3639,7 +3637,7 @@ build_decl_attribute_variant (tree ddecl
 
 
 /* Produce good hash value combining VAL and VAL2.  */
-static inline hashval_t
+hashval_t
 iterative_hash_hashval_t (hashval_t val, hashval_t val2)
 {
   /* the golden ratio; an arbitrary value.  */
@@ -5358,7 +5356,6 @@ iterative_hash_expr (const_tree t, hashv
       return iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
 
     case SSA_NAME:
-    case VALUE_HANDLE:
       /* we can just compare by pointer.  */
       return iterative_hash_pointer (t, val);
 
Index: gcc/tree.h
===================================================================
--- gcc/tree.h	(revision 137479)
+++ gcc/tree.h	(working copy)
@@ -3492,26 +3492,6 @@ struct tree_statement_list
   struct tree_statement_list_node *tail;
 };
 
-#define VALUE_HANDLE_ID(NODE)		\
-  (VALUE_HANDLE_CHECK (NODE)->value_handle.id)
-
-#define VALUE_HANDLE_EXPR_SET(NODE)	\
-  (VALUE_HANDLE_CHECK (NODE)->value_handle.expr_set)
-
-/* Defined and used in tree-ssa-pre.c.  */
-
-struct tree_value_handle GTY(())
-{
-  struct tree_common common;
-
-  /* The set of expressions represented by this handle.  */
-  struct bitmap_set * GTY ((skip)) expr_set;
-
-  /* Unique ID for this value handle.  IDs are handed out in a
-     conveniently dense form starting at 0, so that we can make
-     bitmaps of value handles.  */
-  unsigned int id;
-};
 \f
 /* Define the overall contents of a tree node.
    It may be any of the structures declared above
@@ -3552,7 +3532,6 @@ union tree_node GTY ((ptr_alias (union l
   struct tree_binfo GTY ((tag ("TS_BINFO"))) binfo;
   struct tree_statement_list GTY ((tag ("TS_STATEMENT_LIST"))) stmt_list;
   struct gimple_stmt GTY ((tag ("TS_GIMPLE_STATEMENT"))) gstmt;
-  struct tree_value_handle GTY ((tag ("TS_VALUE_HANDLE"))) value_handle;
   struct tree_constructor GTY ((tag ("TS_CONSTRUCTOR"))) constructor;
   struct tree_memory_tag GTY ((tag ("TS_MEMORY_TAG"))) mtag;
   struct tree_omp_clause GTY ((tag ("TS_OMP_CLAUSE"))) omp_clause;
@@ -4964,6 +4943,7 @@ extern int tree_log2 (const_tree);
 extern int tree_floor_log2 (const_tree);
 extern int simple_cst_equal (const_tree, const_tree);
 extern hashval_t iterative_hash_expr (const_tree, hashval_t);
+extern hashval_t iterative_hash_hashval_t (hashval_t, hashval_t);
 extern int compare_tree_int (const_tree, unsigned HOST_WIDE_INT);
 extern int type_list_equal (const_tree, const_tree);
 extern int chain_member (const_tree, const_tree);
Index: gcc/tree-ssa-sccvn.c
===================================================================
--- gcc/tree-ssa-sccvn.c	(revision 137479)
+++ gcc/tree-ssa-sccvn.c	(working copy)
@@ -115,72 +115,9 @@ typedef struct vn_tables_s
   alloc_pool references_pool;
 } *vn_tables_t;
 
-/* Nary operations in the hashtable consist of length operands, an
-   opcode, and a type.  Result is the value number of the operation,
-   and hashcode is stored to avoid having to calculate it
-   repeatedly.  */
-
-typedef struct vn_nary_op_s
-{
-  ENUM_BITFIELD(tree_code) opcode : 16;
-  unsigned length : 16;
-  hashval_t hashcode;
-  tree result;
-  tree type;
-  tree op[4];
-} *vn_nary_op_t;
-typedef const struct vn_nary_op_s *const_vn_nary_op_t;
-
-/* Phi nodes in the hashtable consist of their non-VN_TOP phi
-   arguments, and the basic block the phi is in. Result is the value
-   number of the operation, and hashcode is stored to avoid having to
-   calculate it repeatedly.  Phi nodes not in the same block are never
-   considered equivalent.  */
-
-typedef struct vn_phi_s
-{
-  VEC (tree, heap) *phiargs;
-  basic_block block;
-  hashval_t hashcode;
-  tree result;
-} *vn_phi_t;
-typedef const struct vn_phi_s *const_vn_phi_t;
+static htab_t constant_to_value_id;
+static bitmap constant_value_ids;
 
-/* Reference operands only exist in reference operations structures.
-   They consist of an opcode, type, and some number of operands.  For
-   a given opcode, some, all, or none of the operands may be used.
-   The operands are there to store the information that makes up the
-   portion of the addressing calculation that opcode performs.  */
-
-typedef struct vn_reference_op_struct
-{
-  enum tree_code opcode;
-  tree type;
-  tree op0;
-  tree op1;
-} vn_reference_op_s;
-typedef vn_reference_op_s *vn_reference_op_t;
-typedef const vn_reference_op_s *const_vn_reference_op_t;
-
-DEF_VEC_O(vn_reference_op_s);
-DEF_VEC_ALLOC_O(vn_reference_op_s, heap);
-
-/* A reference operation in the hashtable is representation as a
-   collection of vuses, representing the memory state at the time of
-   the operation, and a collection of operands that make up the
-   addressing calculation.  If two vn_reference_t's have the same set
-   of operands, they access the same memory location. We also store
-   the resulting value number, and the hashcode.  The vuses are
-   always stored in order sorted by ssa name version.  */
-
-typedef struct vn_reference_s
-{
-  VEC (tree, gc) *vuses;
-  VEC (vn_reference_op_s, heap) *operands;
-  hashval_t hashcode;
-  tree result;
-} *vn_reference_t;
-typedef const struct vn_reference_s *const_vn_reference_t;
 
 /* Valid hashtables storing information we have proven to be
    correct.  */
@@ -215,6 +152,10 @@ static int *rpo_numbers;
 
 tree VN_TOP;
 
+/* Unique counter for our value ids.  */
+
+static unsigned int next_value_id;
+
 /* Next DFS number and the stack for strongly connected component
    detection. */
 
@@ -239,8 +180,10 @@ static struct obstack vn_ssa_aux_obstack
 vn_ssa_aux_t
 VN_INFO (tree name)
 {
-  return VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
-		    SSA_NAME_VERSION (name));
+  vn_ssa_aux_t res = VEC_index (vn_ssa_aux_t, vn_ssa_aux_table,
+				SSA_NAME_VERSION (name));
+  gcc_assert (res);
+  return res;
 }
 
 /* Set the value numbering info for a given SSA name to a given
@@ -290,6 +233,58 @@ free_reference (void *vp)
   VEC_free (vn_reference_op_s, heap, vr->operands);
 }
 
+/* Hash table equality function for vn_constant_t.  */
+
+static int
+vn_constant_eq (const void *p1, const void *p2)
+{
+  const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
+  const struct vn_constant_s *vc2 = (const struct vn_constant_s *) p2;
+
+  return expressions_equal_p (vc1->constant, vc2->constant);
+}
+
+/* Hash table hash function for vn_constant_t.  */
+   
+static hashval_t
+vn_constant_hash (const void *p1)
+{
+  const struct vn_constant_s *vc1 = (const struct vn_constant_s *) p1;
+  return vc1->hashcode;
+}
+
+/* Lookup a value id for CONSTANT, and if it does not exist, create a
+   new one and return it.  If it does exist, return it.  */
+
+unsigned int
+get_or_alloc_constant_value_id (tree constant)
+{
+  void **slot;
+  vn_constant_t vc = XNEW (struct vn_constant_s);
+  
+  vc->hashcode = iterative_hash_expr (constant, 0);
+  vc->constant = constant;
+  slot = htab_find_slot_with_hash (constant_to_value_id, vc,
+				   vc->hashcode, INSERT);  
+  if (*slot)
+    {
+      free (vc);
+      return ((vn_constant_t)*slot)->value_id;
+    }
+  vc->value_id = get_next_value_id ();
+  *slot = vc;
+  bitmap_set_bit (constant_value_ids, vc->value_id);
+  return vc->value_id;
+}
+
+/* Return true if V is a value id for a constant.  */
+
+bool
+value_id_constant_p (unsigned int v)
+{
+  return bitmap_bit_p (constant_value_ids, v);  
+}
+
 /* Compare two reference operands P1 and P2 for equality.  Return true if
    they are equal, and false otherwise.  */
 
@@ -324,7 +319,7 @@ vn_reference_hash (const void *p1)
 
 /* Compute a hash for the reference operation VR1 and return it.  */
 
-static inline hashval_t
+hashval_t
 vn_reference_compute_hash (const vn_reference_t vr1)
 {
   hashval_t result = 0;
@@ -343,7 +338,7 @@ vn_reference_compute_hash (const vn_refe
 /* Return true if reference operations P1 and P2 are equivalent.  This
    means they have the same set of operands and vuses.  */
 
-static int
+int
 vn_reference_eq (const void *p1, const void *p2)
 {
   tree v;
@@ -484,10 +479,18 @@ copy_reference_ops_from_ref (tree ref, V
       temp.type = TREE_TYPE (ref);
       temp.opcode = CALL_EXPR;
       VEC_safe_push (vn_reference_op_s, heap, *result, &temp);
-
-      callfn = get_callee_fndecl (ref);
-      if (!callfn)
-	callfn = CALL_EXPR_FN (ref);
+      
+      /* We make no attempt to simplify the called function because
+      the typical &FUNCTION_DECL form is also used in function pointer
+      cases that become constant.  If we simplify the original to
+      FUNCTION_DECL but not the function pointer case (which can
+      happen because we have no fold functions that operate on
+      vn_reference_t), we will claim they are not equivalent.
+
+      An example of this behavior can be see if CALL_EXPR_FN below is
+      replaced with get_callee_fndecl and gcc.dg/tree-ssa/ssa-pre-13.c
+      is compiled.  */
+      callfn = CALL_EXPR_FN (ref);
       temp.type = TREE_TYPE (callfn);
       temp.opcode = TREE_CODE (callfn);
       temp.op0 = callfn;
@@ -554,6 +557,7 @@ copy_reference_ops_from_ref (tree ref, V
 	     a matching type is not necessary and a mismatching type
 	     is always a spurious difference.  */
 	  temp.type = NULL_TREE;
+#if FIXME
 	  /* If this is a reference to a union member, record the union
 	     member size as operand.  Do so only if we are doing
 	     expression insertion (during FRE), as PRE currently gets
@@ -564,14 +568,17 @@ copy_reference_ops_from_ref (tree ref, V
 	      && integer_zerop (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (ref, 1))))
 	    temp.op0 = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)));
 	  else
+#endif
 	    /* Record field as operand.  */
 	    temp.op0 = TREE_OPERAND (ref, 1);
+	    temp.op1 = TREE_OPERAND (ref, 2);	  
 	  break;
 	case ARRAY_RANGE_REF:
 	case ARRAY_REF:
 	  /* Record index as operand.  */
 	  temp.op0 = TREE_OPERAND (ref, 1);
-	  temp.op1 = TREE_OPERAND (ref, 3);
+	  temp.op1 = TREE_OPERAND (ref, 2);
+	  temp.op2 = TREE_OPERAND (ref, 3);
 	  break;
 	case STRING_CST:
 	case INTEGER_CST:
@@ -579,7 +586,6 @@ copy_reference_ops_from_ref (tree ref, V
 	case VECTOR_CST:
 	case REAL_CST:
 	case CONSTRUCTOR:
-	case VALUE_HANDLE:
 	case VAR_DECL:
 	case PARM_DECL:
 	case CONST_DECL:
@@ -653,7 +659,14 @@ valueize_refs (VEC (vn_reference_op_s, h
     {
       if (vro->opcode == SSA_NAME
 	  || (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME))
-	vro->op0 = SSA_VAL (vro->op0);
+	{
+	  vro->op0 = SSA_VAL (vro->op0);
+	  /* If it transforms from an SSA_NAME to a constant, update
+	     the opcode.  */
+	  if (TREE_CODE (vro->op0) != SSA_NAME && vro->opcode == SSA_NAME)
+	    vro->opcode = TREE_CODE (vro->op0);
+	}
+      
     }
 
   return orig;
@@ -733,10 +746,11 @@ cont:
 
 /* Lookup a SCCVN reference operation VR in the current hash table.
    Returns the resulting value number if it exists in the hash table,
-   NULL_TREE otherwise.  */
+   NULL_TREE otherwise.  VNRESULT will be filled in with the actual
+   vn_reference_t stored in the hashtable if something is found.  */
 
 static tree
-vn_reference_lookup_1 (vn_reference_t vr)
+vn_reference_lookup_1 (vn_reference_t vr, vn_reference_t *vnresult)
 {
   void **slot;
   hashval_t hash;
@@ -748,25 +762,58 @@ vn_reference_lookup_1 (vn_reference_t vr
     slot = htab_find_slot_with_hash (valid_info->references, vr,
 				     hash, NO_INSERT);
   if (slot)
-    return ((vn_reference_t)*slot)->result;
-
+    {
+      if (vnresult)
+	*vnresult = (vn_reference_t)*slot;
+      return ((vn_reference_t)*slot)->result;
+    }
+  
   return NULL_TREE;
 }
 
-/* Lookup OP in the current hash table, and return the resulting
-   value number if it exists in the hash table.  Return NULL_TREE if
-   it does not exist in the hash table. */
+
+/* Lookup a reference operation by it's parts, in the current hash table.
+   Returns the resulting value number if it exists in the hash table,
+   NULL_TREE otherwise.  VNRESULT will be filled in with the actual
+   vn_reference_t stored in the hashtable if something is found.  */
 
 tree
-vn_reference_lookup (tree op, VEC (tree, gc) *vuses, bool maywalk)
+vn_reference_lookup_pieces (VEC (tree, gc) *vuses,
+			    VEC (vn_reference_op_s, heap) *operands,
+			    vn_reference_t *vnresult) 
+{
+  struct vn_reference_s vr1;
+  tree result;
+  if (vnresult)
+    *vnresult = NULL;
+  
+  vr1.vuses = valueize_vuses (vuses);
+  vr1.operands = valueize_refs (operands);
+  vr1.hashcode = vn_reference_compute_hash (&vr1);
+  result = vn_reference_lookup_1 (&vr1, vnresult);
+
+  return result;
+}
+
+/* Lookup OP in the current hash table, and return the resulting value
+   number if it exists in the hash table.  Return NULL_TREE if it does
+   not exist in the hash table or if the result field of the structure
+   was NULL..  VNRESULT will be filled in with the vn_reference_t
+   stored in the hashtable if one exists.  */
+
+tree
+vn_reference_lookup (tree op, VEC (tree, gc) *vuses, bool maywalk,
+		     vn_reference_t *vnresult)
 {
   struct vn_reference_s vr1;
   tree result, def_stmt;
+  if (vnresult)
+    *vnresult = NULL;
 
   vr1.vuses = valueize_vuses (vuses);
   vr1.operands = valueize_refs (shared_reference_ops_from_ref (op));
   vr1.hashcode = vn_reference_compute_hash (&vr1);
-  result = vn_reference_lookup_1 (&vr1);
+  result = vn_reference_lookup_1 (&vr1, vnresult);
 
   /* If there is a single defining statement for all virtual uses, we can
      use that, following virtual use-def chains.  */
@@ -786,23 +833,27 @@ vn_reference_lookup (tree op, VEC (tree,
       vdefs_to_vec (def_stmt, &vuses);
       vr1.vuses = valueize_vuses (vuses);
       vr1.hashcode = vn_reference_compute_hash (&vr1);
-      result = vn_reference_lookup_1 (&vr1);
+      result = vn_reference_lookup_1 (&vr1, vnresult);
     }
 
   return result;
 }
 
+
 /* Insert OP into the current hash table with a value number of
-   RESULT.  */
+   RESULT, and return the resulting reference structure we created.  */
 
-void
+vn_reference_t
 vn_reference_insert (tree op, tree result, VEC (tree, gc) *vuses)
 {
   void **slot;
   vn_reference_t vr1;
 
   vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
-
+  if (TREE_CODE (result) == SSA_NAME)
+    vr1->value_id = VN_INFO (result)->value_id;
+  else
+    vr1->value_id = get_or_alloc_constant_value_id (result);
   vr1->vuses = valueize_vuses (vuses);
   vr1->operands = valueize_refs (create_reference_ops_from_ref (op));
   vr1->hashcode = vn_reference_compute_hash (vr1);
@@ -824,11 +875,48 @@ vn_reference_insert (tree op, tree resul
     free_reference (*slot);
 
   *slot = vr1;
+  return vr1;
+}
+
+/* Insert a reference by it's pieces into the current hash table with
+   a value number of RESULT.  Return the resulting reference
+   structure we created.  */
+
+vn_reference_t
+vn_reference_insert_pieces (VEC (tree, gc) *vuses,
+			    VEC (vn_reference_op_s, heap) *operands,
+			    tree result, unsigned int value_id)
+
+{
+  void **slot;
+  vn_reference_t vr1;
+
+  vr1 = (vn_reference_t) pool_alloc (current_info->references_pool);
+  vr1->value_id =  value_id;
+  vr1->vuses = valueize_vuses (vuses);
+  vr1->operands = valueize_refs (operands);
+  vr1->hashcode = vn_reference_compute_hash (vr1);
+  if (result && TREE_CODE (result) == SSA_NAME)
+    result = SSA_VAL (result);
+  vr1->result = result;
+
+  slot = htab_find_slot_with_hash (current_info->references, vr1, vr1->hashcode,
+				   INSERT);
+  
+  /* At this point we should have all the things inserted that we have
+  seen before, and we should never try inserting something that
+  already exists.  */
+  gcc_assert (!*slot);
+  if (*slot)
+    free_reference (*slot);
+
+  *slot = vr1;
+  return vr1;
 }
 
 /* Compute and return the hash value for nary operation VBO1.  */
 
-static inline hashval_t
+inline hashval_t
 vn_nary_op_compute_hash (const vn_nary_op_t vno1)
 {
   hashval_t hash = 0;
@@ -865,7 +953,7 @@ vn_nary_op_hash (const void *p1)
 /* Compare nary operations P1 and P2 and return true if they are
    equivalent.  */
 
-static int
+int
 vn_nary_op_eq (const void *p1, const void *p2)
 {
   const_vn_nary_op_t const vno1 = (const_vn_nary_op_t) p1;
@@ -883,17 +971,56 @@ vn_nary_op_eq (const void *p1, const voi
   return true;
 }
 
-/* Lookup OP in the current hash table, and return the resulting
-   value number if it exists in the hash table.  Return NULL_TREE if
-   it does not exist in the hash table. */
+/* Lookup a n-ary operation by its pieces and return the resulting value
+   number if it exists in the hash table.  Return NULL_TREE if it does
+   not exist in the hash table or if the result field of the operation
+   is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
+   if it exists.  */
 
 tree
-vn_nary_op_lookup (tree op)
+vn_nary_op_lookup_pieces (unsigned int length, enum tree_code code,
+			  tree type, tree op0, tree op1, tree op2,
+			  tree op3, vn_nary_op_t *vnresult) 
+{
+  void **slot;
+  struct vn_nary_op_s vno1;
+  if (vnresult)
+    *vnresult = NULL;
+  vno1.opcode = code;
+  vno1.length = length;
+  vno1.type = type;
+  vno1.op[0] = op0;
+  vno1.op[1] = op1;
+  vno1.op[2] = op2;
+  vno1.op[3] = op3;
+  vno1.hashcode = vn_nary_op_compute_hash (&vno1);
+  slot = htab_find_slot_with_hash (current_info->nary, &vno1, vno1.hashcode,
+				   NO_INSERT);
+  if (!slot && current_info == optimistic_info)
+    slot = htab_find_slot_with_hash (valid_info->nary, &vno1, vno1.hashcode,
+				     NO_INSERT);
+  if (!slot)
+    return NULL_TREE;
+  if (vnresult)
+    *vnresult = (vn_nary_op_t)*slot;
+  return ((vn_nary_op_t)*slot)->result;
+}
+
+/* Lookup OP in the current hash table, and return the resulting value
+   number if it exists in the hash table.  Return NULL_TREE if it does
+   not exist in the hash table or if the result field of the operation
+   is NULL. VNRESULT will contain the vn_nary_op_t from the hashtable
+   if it exists.  */
+
+tree
+vn_nary_op_lookup (tree op, vn_nary_op_t *vnresult)
 {
   void **slot;
   struct vn_nary_op_s vno1;
   unsigned i;
 
+  if (vnresult)
+    *vnresult = NULL;
   vno1.opcode = TREE_CODE (op);
   vno1.length = TREE_CODE_LENGTH (TREE_CODE (op));
   vno1.type = TREE_TYPE (op);
@@ -907,13 +1034,56 @@ vn_nary_op_lookup (tree op)
 				     NO_INSERT);
   if (!slot)
     return NULL_TREE;
+  if (vnresult)
+    *vnresult = (vn_nary_op_t)*slot;
   return ((vn_nary_op_t)*slot)->result;
 }
 
+/* Insert a n-ary operation into the current hash table using it's
+   pieces.  Return the vn_nary_op_t structure we created and put in
+   the hashtable.  */
+
+vn_nary_op_t
+vn_nary_op_insert_pieces (unsigned int length, enum tree_code code,
+			  tree type, tree op0,
+			  tree op1, tree op2, tree op3,
+			  tree result,
+			  unsigned int value_id) 
+{
+  void **slot;
+  vn_nary_op_t vno1;
+
+  vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
+				       (sizeof (struct vn_nary_op_s)
+					- sizeof (tree) * (4 - length)));
+  vno1->value_id = value_id;
+  vno1->opcode = code;
+  vno1->length = length;
+  vno1->type = type;
+  if (length >= 1)
+    vno1->op[0] = op0;
+  if (length >= 2)
+    vno1->op[1] = op1;
+  if (length >= 3)
+    vno1->op[2] = op2;
+  if (length >= 4)
+    vno1->op[3] = op3;
+  vno1->result = result;
+  vno1->hashcode = vn_nary_op_compute_hash (vno1);
+  slot = htab_find_slot_with_hash (current_info->nary, vno1, vno1->hashcode,
+				   INSERT);
+  gcc_assert (!*slot);
+
+  *slot = vno1;
+  return vno1;
+  
+}
+
 /* Insert OP into the current hash table with a value number of
-   RESULT.  */
+   RESULT.  Return the vn_nary_op_t structure we created and put in
+   the hashtable.  */
 
-void
+vn_nary_op_t
 vn_nary_op_insert (tree op, tree result)
 {
   unsigned length = TREE_CODE_LENGTH (TREE_CODE (op));
@@ -924,6 +1094,7 @@ vn_nary_op_insert (tree op, tree result)
   vno1 = (vn_nary_op_t) obstack_alloc (&current_info->nary_obstack,
 			(sizeof (struct vn_nary_op_s)
 			 - sizeof (tree) * (4 - length)));
+  vno1->value_id = VN_INFO (result)->value_id;
   vno1->opcode = TREE_CODE (op);
   vno1->length = length;
   vno1->type = TREE_TYPE (op);
@@ -936,6 +1107,7 @@ vn_nary_op_insert (tree op, tree result)
   gcc_assert (!*slot);
 
   *slot = vno1;
+  return vno1;
 }
 
 /* Compute a hashcode for PHI operation VP1 and return it.  */
@@ -1034,7 +1206,7 @@ vn_phi_lookup (tree phi)
 /* Insert PHI into the current hash table with a value number of
    RESULT.  */
 
-static void
+static vn_phi_t
 vn_phi_insert (tree phi, tree result)
 {
   void **slot;
@@ -1049,6 +1221,7 @@ vn_phi_insert (tree phi, tree result)
       def = TREE_CODE (def) == SSA_NAME ? SSA_VAL (def) : def;
       VEC_safe_push (tree, heap, args, def);
     }
+  vp1->value_id = VN_INFO (result)->value_id;
   vp1->phiargs = args;
   vp1->block = bb_for_stmt (phi);
   vp1->result = result;
@@ -1060,6 +1233,7 @@ vn_phi_insert (tree phi, tree result)
   /* Because we iterate over phi operations more than once, it's
      possible the slot might already exist here, hence no assert.*/
   *slot = vp1;
+  return vp1;
 }
 
 
@@ -1168,7 +1342,7 @@ static bool
 visit_unary_op (tree lhs, tree op)
 {
   bool changed = false;
-  tree result = vn_nary_op_lookup (op);
+  tree result = vn_nary_op_lookup (op, NULL);
 
   if (result)
     {
@@ -1190,7 +1364,7 @@ static bool
 visit_binary_op (tree lhs, tree op)
 {
   bool changed = false;
-  tree result = vn_nary_op_lookup (op);
+  tree result = vn_nary_op_lookup (op, NULL);
 
   if (result)
     {
@@ -1212,7 +1386,8 @@ static bool
 visit_reference_op_load (tree lhs, tree op, tree stmt)
 {
   bool changed = false;
-  tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt), true);
+  tree result = vn_reference_lookup (op, shared_vuses_from_stmt (stmt), true,
+				     NULL);
 
   /* We handle type-punning through unions by value-numbering based
      on offset and size of the access.  Be prepared to handle a
@@ -1236,7 +1411,7 @@ visit_reference_op_load (tree lhs, tree 
       result = val;
       if (!is_gimple_min_invariant (val)
 	  && TREE_CODE (val) != SSA_NAME)
-	result = vn_nary_op_lookup (val);
+	result = vn_nary_op_lookup (val, NULL);
       /* If the expression is not yet available, value-number lhs to
 	 a new SSA_NAME we create.  */
       if (!result && may_insert)
@@ -1319,7 +1494,8 @@ visit_reference_op_store (tree lhs, tree
      Otherwise, the vdefs for the store are used when inserting into
      the table, since the store generates a new memory state.  */
 
-  result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt), false);
+  result = vn_reference_lookup (lhs, shared_vuses_from_stmt (stmt), false,
+				NULL);
 
   if (result)
     {
@@ -2146,12 +2322,18 @@ init_scc_vn (void)
 
   calculate_dominance_info (CDI_DOMINATORS);
   sccstack = NULL;
+  constant_to_value_id = htab_create (23, vn_constant_hash, vn_constant_eq,
+				  free);
+  
+  constant_value_ids = BITMAP_ALLOC (NULL);
+  
   next_dfs_num = 1;
-
+  next_value_id = 1;
+  
   vn_ssa_aux_table = VEC_alloc (vn_ssa_aux_t, heap, num_ssa_names + 1);
   /* VEC_alloc doesn't actually grow it to the right size, it just
      preallocates the space to do so.  */
-  VEC_safe_grow (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
+  VEC_safe_grow_cleared (vn_ssa_aux_t, heap, vn_ssa_aux_table, num_ssa_names + 1);
   gcc_obstack_init (&vn_ssa_aux_obstack);
 
   shared_lookup_phiargs = NULL;
@@ -2180,6 +2362,7 @@ init_scc_vn (void)
 	{
 	  VN_INFO_GET (name)->valnum = VN_TOP;
 	  VN_INFO (name)->expr = name;
+	  VN_INFO (name)->value_id = 0;
 	}
     }
 
@@ -2206,6 +2389,8 @@ free_scc_vn (void)
 {
   size_t i;
 
+  htab_delete (constant_to_value_id);
+  BITMAP_FREE (constant_value_ids);
   VEC_free (tree, heap, shared_lookup_phiargs);
   VEC_free (tree, gc, shared_lookup_vops);
   VEC_free (vn_reference_op_s, heap, shared_lookup_references);
@@ -2215,8 +2400,7 @@ free_scc_vn (void)
     {
       tree name = ssa_name (i);
       if (name
-	  && SSA_NAME_VALUE (name)
-	  && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
+	  && SSA_NAME_VALUE (name))
 	SSA_NAME_VALUE (name) = NULL;
       if (name
 	  && VN_INFO (name)->needs_insertion)
@@ -2237,6 +2421,91 @@ free_scc_vn (void)
     }
 }
 
+/* Set the value ids in the valid/optimistic hash tables.  */
+
+static void
+set_hashtable_value_ids (void)
+{
+  htab_iterator hi;
+  vn_nary_op_t vno;
+  vn_reference_t vr;
+  vn_phi_t vp;
+  
+  /* Now set the value ids of the things we had put in the hash
+     table.  */
+
+  FOR_EACH_HTAB_ELEMENT (valid_info->nary,
+			 vno, vn_nary_op_t, hi) 
+    {
+      if (vno->result)
+	{
+	  if (TREE_CODE (vno->result) == SSA_NAME)
+	    vno->value_id = VN_INFO (vno->result)->value_id;
+	  else if (is_gimple_min_invariant (vno->result))
+	    vno->value_id = get_or_alloc_constant_value_id (vno->result);
+	}
+    }
+
+  FOR_EACH_HTAB_ELEMENT (optimistic_info->nary,
+			 vno, vn_nary_op_t, hi) 
+    {
+      if (vno->result)
+	{
+	  if (TREE_CODE (vno->result) == SSA_NAME)
+	    vno->value_id = VN_INFO (vno->result)->value_id;
+	  else if (is_gimple_min_invariant (vno->result))
+	    vno->value_id = get_or_alloc_constant_value_id (vno->result);
+	}
+    }
+
+  FOR_EACH_HTAB_ELEMENT (valid_info->phis,
+			 vp, vn_phi_t, hi) 
+    {
+      if (vp->result)
+	{
+	  if (TREE_CODE (vp->result) == SSA_NAME)
+	    vp->value_id = VN_INFO (vp->result)->value_id;
+	  else if (is_gimple_min_invariant (vp->result))
+	    vp->value_id = get_or_alloc_constant_value_id (vp->result);
+	}
+    }
+  FOR_EACH_HTAB_ELEMENT (optimistic_info->phis,
+			 vp, vn_phi_t, hi) 
+    {
+      if (vp->result)
+	{
+	  if (TREE_CODE (vp->result) == SSA_NAME)
+	    vp->value_id = VN_INFO (vp->result)->value_id;
+	  else if (is_gimple_min_invariant (vp->result))
+	    vp->value_id = get_or_alloc_constant_value_id (vp->result);
+	}
+    }
+
+
+  FOR_EACH_HTAB_ELEMENT (valid_info->references,
+			 vr, vn_reference_t, hi) 
+    {
+      if (vr->result)
+	{
+	  if (TREE_CODE (vr->result) == SSA_NAME)
+	    vr->value_id = VN_INFO (vr->result)->value_id;
+	  else if (is_gimple_min_invariant (vr->result))
+	    vr->value_id = get_or_alloc_constant_value_id (vr->result);
+	}
+    }
+  FOR_EACH_HTAB_ELEMENT (optimistic_info->references,
+			 vr, vn_reference_t, hi) 
+    {
+      if (vr->result)
+	{
+	  if (TREE_CODE (vr->result) == SSA_NAME)
+	    vr->value_id = VN_INFO (vr->result)->value_id;
+	  else if (is_gimple_min_invariant (vr->result))
+	    vr->value_id = get_or_alloc_constant_value_id (vr->result);
+	}
+    }
+}
+
 /* Do SCCVN.  Returns true if it finished, false if we bailed out
    due to resource constraints.  */
 
@@ -2245,7 +2514,8 @@ run_scc_vn (bool may_insert_arg)
 {
   size_t i;
   tree param;
-
+  bool changed = true;
+  
   may_insert = may_insert_arg;
 
   init_scc_vn ();
@@ -2276,6 +2546,45 @@ run_scc_vn (bool may_insert_arg)
 	  }
     }
 
+  /* Initialize the value ids.  */
+      
+  for (i = 1; i < num_ssa_names; ++i)
+    {
+      tree name = ssa_name (i);
+      vn_ssa_aux_t info;
+      if (!name)
+	continue;
+      info = VN_INFO (name);
+      if (info->valnum == name)
+	info->value_id = get_next_value_id ();
+      else if (is_gimple_min_invariant (info->valnum))
+	info->value_id = get_or_alloc_constant_value_id (info->valnum);
+    }
+  
+  /* Propagate until they stop changing.  */
+  while (changed)
+    {
+      changed = false;
+      for (i = 1; i < num_ssa_names; ++i)
+	{
+	  tree name = ssa_name (i);
+	  vn_ssa_aux_t info;
+	  if (!name)
+	    continue;
+	  info = VN_INFO (name);
+	  if (TREE_CODE (info->valnum) == SSA_NAME
+	      && info->valnum != name
+	      && TREE_CODE (info->valnum) == SSA_NAME
+	      && info->value_id != VN_INFO (info->valnum)->value_id)
+	    {
+	      changed = true;
+	      info->value_id = VN_INFO (info->valnum)->value_id;
+	    }
+	}
+    }
+  
+  set_hashtable_value_ids ();
+  
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
       fprintf (dump_file, "Value numbers:\n");
@@ -2300,3 +2609,83 @@ run_scc_vn (bool may_insert_arg)
   may_insert = false;
   return true;
 }
+
+/* Return the maximum value id we have ever seen.  */
+
+unsigned int
+get_max_value_id (void) 
+{
+  return next_value_id;
+}
+
+/* Return the next unique value id.  */
+
+unsigned int
+get_next_value_id (void)
+{
+  return next_value_id++;
+}
+
+
+/* Compare two expressions E1 and E2 and return true if they are
+   equal.  */
+
+bool
+expressions_equal_p (tree e1, tree e2)
+{
+  tree te1, te2;
+
+  if (e1 == e2)
+    return true;
+
+  te1 = TREE_TYPE (e1);
+  te2 = TREE_TYPE (e2);
+
+  if (TREE_CODE (e1) == TREE_LIST && TREE_CODE (e2) == TREE_LIST)
+    {
+      tree lop1 = e1;
+      tree lop2 = e2;
+      for (lop1 = e1, lop2 = e2;
+	   lop1 || lop2;
+	   lop1 = TREE_CHAIN (lop1), lop2 = TREE_CHAIN (lop2))
+	{
+	  if (!lop1 || !lop2)
+	    return false;
+	  if (!expressions_equal_p (TREE_VALUE (lop1), TREE_VALUE (lop2)))
+	    return false;
+	}
+      return true;
+
+    }
+  else if (TREE_CODE (e1) == TREE_CODE (e2)
+	   && operand_equal_p (e1, e2, OEP_PURE_SAME))
+    return true;
+
+  return false;
+}
+
+/* Sort the VUSE array so that we can do equality comparisons
+   quicker on two vuse vecs.  */
+
+void
+sort_vuses (VEC (tree,gc) *vuses)
+{
+  if (VEC_length (tree, vuses) > 1)
+    qsort (VEC_address (tree, vuses),
+	   VEC_length (tree, vuses),
+	   sizeof (tree),
+	   operand_build_cmp);
+}
+
+/* Sort the VUSE array so that we can do equality comparisons
+   quicker on two vuse vecs.  */
+
+void
+sort_vuses_heap (VEC (tree,heap) *vuses)
+{
+  if (VEC_length (tree, vuses) > 1)
+    qsort (VEC_address (tree, vuses),
+	   VEC_length (tree, vuses),
+	   sizeof (tree),
+	   operand_build_cmp);
+}
Index: gcc/tree-ssa-sccvn.h
===================================================================
--- gcc/tree-ssa-sccvn.h	(revision 137479)
+++ gcc/tree-ssa-sccvn.h	(working copy)
@@ -24,6 +24,87 @@
 /* TOP of the VN lattice.  */
 extern tree VN_TOP;
 
+/* N-ary operations in the hashtable consist of length operands, an
+   opcode, and a type.  Result is the value number of the operation,
+   and hashcode is stored to avoid having to calculate it
+   repeatedly.  */
+
+typedef struct vn_nary_op_s
+{
+  /* Unique identify that all expressions with the same value have. */
+  unsigned int value_id;
+  ENUM_BITFIELD(tree_code) opcode : 16;
+  unsigned length : 16;
+  hashval_t hashcode;
+  tree result;
+  tree type;
+  tree op[4];
+} *vn_nary_op_t;
+typedef const struct vn_nary_op_s *const_vn_nary_op_t;
+
+/* Phi nodes in the hashtable consist of their non-VN_TOP phi
+   arguments, and the basic block the phi is in. Result is the value
+   number of the operation, and hashcode is stored to avoid having to
+   calculate it repeatedly.  Phi nodes not in the same block are never
+   considered equivalent.  */
+
+typedef struct vn_phi_s
+{
+  /* Unique identifier that all expressions with the same value have. */
+  unsigned int value_id;
+  hashval_t hashcode;
+  VEC (tree, heap) *phiargs;
+  basic_block block;
+  tree result;
+} *vn_phi_t;
+typedef const struct vn_phi_s *const_vn_phi_t;
+
+/* Reference operands only exist in reference operations structures.
+   They consist of an opcode, type, and some number of operands.  For
+   a given opcode, some, all, or none of the operands may be used.
+   The operands are there to store the information that makes up the
+   portion of the addressing calculation that opcode performs.  */
+
+typedef struct vn_reference_op_struct
+{
+  enum tree_code opcode;
+  tree type;
+  tree op0;
+  tree op1;
+  tree op2;
+} vn_reference_op_s;
+typedef vn_reference_op_s *vn_reference_op_t;
+typedef const vn_reference_op_s *const_vn_reference_op_t;
+
+DEF_VEC_O(vn_reference_op_s);
+DEF_VEC_ALLOC_O(vn_reference_op_s, heap);
+
+/* A reference operation in the hashtable is representation as a
+   collection of vuses, representing the memory state at the time of
+   the operation, and a collection of operands that make up the
+   addressing calculation.  If two vn_reference_t's have the same set
+   of operands, they access the same memory location. We also store
+   the resulting value number, and the hashcode.  The vuses are
+   always stored in order sorted by ssa name version.  */
+
+typedef struct vn_reference_s
+{
+  /* Unique identifier that all expressions with the same value have. */
+  unsigned int value_id;
+  hashval_t hashcode;
+  VEC (tree, gc) *vuses;
+  VEC (vn_reference_op_s, heap) *operands;
+  tree result;
+} *vn_reference_t;
+typedef const struct vn_reference_s *const_vn_reference_t;
+
+typedef struct vn_constant_s
+{
+  unsigned int value_id;
+  hashval_t hashcode;
+  tree constant;
+} *vn_constant_t;
+  
 typedef struct vn_ssa_aux
 {
   /* Value number. This may be an SSA name or a constant.  */
@@ -31,6 +112,9 @@ typedef struct vn_ssa_aux
   /* Representative expression, if not a direct constant. */
   tree expr;
 
+  /* Unique identifier that all expressions with the same value have. */
+  unsigned int value_id;
+
   /* SCC information.  */
   unsigned int dfsnum;
   unsigned int low;
@@ -57,12 +141,31 @@ extern vn_ssa_aux_t VN_INFO_GET (tree);
 bool run_scc_vn (bool);
 void free_scc_vn (void);
 void switch_to_PRE_table (void);
-tree vn_nary_op_lookup (tree);
-void vn_nary_op_insert (tree, tree);
-tree vn_reference_lookup (tree, VEC (tree, gc) *, bool);
-void vn_reference_insert (tree, tree, VEC (tree, gc) *);
+tree vn_nary_op_lookup (tree, vn_nary_op_t *);
+tree vn_nary_op_lookup_pieces (unsigned int, enum tree_code,
+			       tree, tree, tree, tree, tree,
+			       vn_nary_op_t *);
+vn_nary_op_t vn_nary_op_insert (tree, tree);
+vn_nary_op_t vn_nary_op_insert_pieces (unsigned int, enum tree_code,
+				       tree, tree, tree, tree,
+				       tree, tree, unsigned int);
+tree vn_reference_lookup_pieces (VEC (tree, gc) *,
+				 VEC (vn_reference_op_s, heap) *,
+				 vn_reference_t *);
+tree vn_reference_lookup (tree, VEC (tree, gc) *, bool, vn_reference_t *);
+vn_reference_t vn_reference_insert (tree, tree, VEC (tree, gc) *);
+vn_reference_t vn_reference_insert_pieces (VEC (tree, gc) *,
+					   VEC (vn_reference_op_s, heap) *,
+					   tree, unsigned int);
+
+hashval_t vn_nary_op_compute_hash (const vn_nary_op_t);
+int vn_nary_op_eq (const void *, const void *);
+hashval_t vn_reference_compute_hash (const vn_reference_t);
+int vn_reference_eq (const void *, const void *);
+unsigned int get_max_value_id (void);
+unsigned int get_next_value_id (void);
+unsigned int get_or_alloc_constant_value_id (tree);
+bool value_id_constant_p (unsigned int);
 VEC (tree, gc) *shared_vuses_from_stmt (tree);
 VEC (tree, gc) *copy_vuses_from_stmt (tree);
-
-
 #endif /* TREE_SSA_SCCVN_H  */
Index: gcc/testsuite/gcc.c-torture/compile/20080704-1.c
===================================================================
--- gcc/testsuite/gcc.c-torture/compile/20080704-1.c	(revision 0)
+++ gcc/testsuite/gcc.c-torture/compile/20080704-1.c	(revision 0)
@@ -0,0 +1,43 @@
+/* This code used to crash fold_convert due to PRE
+   wanting to fold_convert from a REA_TYPE to an INTEGER_TYPE.  */
+typedef unsigned int uint32_t;
+union double_union
+{
+  double d;
+  uint32_t i[2];
+};
+struct _Jv_reent
+{
+  int _errno;
+};
+_Jv_strtod_r (struct _Jv_reent *ptr, char **se)
+{
+  int bb2, sign;
+  double aadj, aadj1, adj;
+  unsigned long y, z;
+  union double_union rv, *bs = ((void *) 0), *delta = ((void *) 0);
+  {
+  ovfl:ptr->_errno = 34;
+    {
+      (((uint32_t) 0xfffffL) | ((uint32_t) 0x100000L) * (1024 + 1023 - 1));
+    }
+    if ((aadj = _Jv_ratio (delta, bs)) <= 2.)
+      {
+	{
+	  if (aadj < 2. / 2)
+	    aadj = 1. / 2;
+	  aadj1 = -aadj;
+	}
+      }
+    {
+      (rv.i[1]) -= 53 * ((uint32_t) 0x100000L);
+      adj = aadj1 * _Jv_ulp (rv.d);
+      rv.d += adj;
+      if (((rv.i[1]) & ((uint32_t) 0x7ff00000L)) >=
+	  ((uint32_t) 0x100000L) * (1024 + 1023 - 53))
+	{
+	  goto ovfl;
+	}
+    }
+  }
+}
Index: gcc/testsuite/gcc.dg/tree-ssa/pr23455.c
===================================================================
--- gcc/testsuite/gcc.dg/tree-ssa/pr23455.c	(revision 0)
+++ gcc/testsuite/gcc.dg/tree-ssa/pr23455.c	(revision 0)
@@ -0,0 +1,23 @@
+/* { dg-do compile } */ 
+/* { dg-options "-O2 -fdump-tree-pre-stats" } */
+unsigned long outcnt;
+extern void flush_outbuf(void);
+
+void
+bi_windup(unsigned int *outbuf, unsigned int bi_buf)
+{
+    unsigned long t1 = outcnt;
+    outbuf[t1] = bi_buf;
+
+    unsigned long t2 = outcnt;
+    if (t2 == 16384)
+      flush_outbuf();
+
+    unsigned long t3 = outcnt;
+    outbuf[t3] = bi_buf;
+}
+/* We should eliminate one load of outcnt, which will in turn let us eliminate
+   one multiply of outcnt which will in turn let us eliminate
+   one add involving outcnt and outbuf.  */
+/* { dg-final { scan-tree-dump-times "Eliminated: 3" 1 "pre"} } */
+/* { dg-final { cleanup-tree-dump "pre" } } */
Index: gcc/testsuite/gcc.dg/tree-ssa/pr35286.c
===================================================================
--- gcc/testsuite/gcc.dg/tree-ssa/pr35286.c	(revision 0)
+++ gcc/testsuite/gcc.dg/tree-ssa/pr35286.c	(revision 0)
@@ -0,0 +1,23 @@
+/* { dg-do compile } */ 
+/* { dg-options "-O2 -fdump-tree-pre-stats" } */
+int g2;
+struct A {
+    int a; int b;
+}g1;
+int foo(int a, int b)
+{
+  if (a > 0)
+    {
+      g1.a = a+ b;
+    }
+  else
+    g1.a = b;
+
+  g2 = a+b;
+
+  return g1.a;
+}
+/* We will eliminate the g1.a from the return statement as fully redundant,
+   and remove one calculation of a + b. */
+/* { dg-final { scan-tree-dump-times "Eliminated: 2" 1 "pre"} } */
+/* { dg-final { cleanup-tree-dump "pre" } } */
Index: gcc/testsuite/gcc.dg/tree-ssa/pr35287.c
===================================================================
--- gcc/testsuite/gcc.dg/tree-ssa/pr35287.c	(revision 0)
+++ gcc/testsuite/gcc.dg/tree-ssa/pr35287.c	(revision 0)
@@ -0,0 +1,15 @@
+/* { dg-do compile } */ 
+/* { dg-options "-O2 -fdump-tree-pre-stats" } */
+int *gp;
+int foo(int p)
+{
+  int t = 0;
+  if (p)
+    t = *gp + 1;
+
+  return (*gp + t);
+}
+
+/* We will eliminate one load of gp and one indirect load of *gp. */
+/* { dg-final { scan-tree-dump-times "Eliminated: 2" 1 "pre"} } */
+/* { dg-final { cleanup-tree-dump "pre" } } */
Index: gcc/testsuite/gcc.dg/tree-ssa/loadpre24.c
===================================================================
--- gcc/testsuite/gcc.dg/tree-ssa/loadpre24.c	(revision 137479)
+++ gcc/testsuite/gcc.dg/tree-ssa/loadpre24.c	(working copy)
@@ -18,7 +18,7 @@ int foo(int argc)
   return d + e;
 }
 
-/* PRE of globals doesn't work.  */
+/* We will move the load of a out of the loop.  */
 
-/* { dg-final { scan-tree-dump-times "Eliminated: 1" 1 "pre" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "Eliminated: 1" 1 "pre" } } */
 /* { dg-final { cleanup-tree-dump "pre" } } */
Index: gcc/tree-ssa-dom.c
===================================================================
--- gcc/tree-ssa-dom.c	(revision 137479)
+++ gcc/tree-ssa-dom.c	(working copy)
@@ -1163,7 +1163,7 @@ record_equality (tree x, tree y)
     prev_x = x, x = y, y = prev_x, prev_x = prev_y;
   else if (prev_x && is_gimple_min_invariant (prev_x))
     x = y, y = prev_x, prev_x = prev_y;
-  else if (prev_y && TREE_CODE (prev_y) != VALUE_HANDLE)
+  else if (prev_y)
     y = prev_y;
 
   /* After the swapping, we must have one SSA_NAME.  */
@@ -1629,7 +1629,7 @@ cprop_operand (tree stmt, use_operand_p 
      copy of some other variable, use the value or copy stored in
      CONST_AND_COPIES.  */
   val = SSA_NAME_VALUE (op);
-  if (val && val != op && TREE_CODE (val) != VALUE_HANDLE)
+  if (val && val != op)
     {
       tree op_type, val_type;
 
@@ -1969,7 +1969,7 @@ lookup_avail_expr (tree stmt, bool inser
   if (TREE_CODE (lhs) == SSA_NAME)
     {
       temp = SSA_NAME_VALUE (lhs);
-      if (temp && TREE_CODE (temp) != VALUE_HANDLE)
+      if (temp)
 	lhs = temp;
     }
 
Index: gcc/treestruct.def
===================================================================
--- gcc/treestruct.def	(revision 137479)
+++ gcc/treestruct.def	(working copy)
@@ -59,7 +59,6 @@ DEFTREESTRUCT(TS_BLOCK, "block")
 DEFTREESTRUCT(TS_BINFO, "binfo")
 DEFTREESTRUCT(TS_STATEMENT_LIST, "statement list")
 DEFTREESTRUCT(TS_GIMPLE_STATEMENT, "gimple statement")
-DEFTREESTRUCT(TS_VALUE_HANDLE, "value handle")
 DEFTREESTRUCT(TS_CONSTRUCTOR, "constructor")
 DEFTREESTRUCT(TS_MEMORY_TAG, "memory tag")
 DEFTREESTRUCT(TS_OMP_CLAUSE, "omp clause")
Index: gcc/tree-vn.c
===================================================================
--- gcc/tree-vn.c	(revision 137479)
+++ gcc/tree-vn.c	(working copy)
@@ -1,406 +0,0 @@
-/* Value Numbering routines for tree expressions.
-   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008 Free Software
-   Foundation, Inc.
-   Contributed by Daniel Berlin <dan@dberlin.org>, Steven Bosscher
-   <stevenb@suse.de> and 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 3, or (at your option)
-any later version.
-
-GCC is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-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 COPYING3.  If not see
-<http://www.gnu.org/licenses/>.  */
-
-#include "config.h"
-#include "system.h"
-#include "coretypes.h"
-#include "tm.h"
-#include "ggc.h"
-#include "tree.h"
-#include "tree-flow.h"
-#include "hashtab.h"
-#include "langhooks.h"
-#include "tree-pass.h"
-#include "tree-dump.h"
-#include "diagnostic.h"
-#include "tree-ssa-sccvn.h"
-
-/* Most of this is PRE specific.  The real grunt work is done in
-   tree-ssa-sccvn.c.  This is where the lookup and insertion
-   functions, etc, can be found.  */
-
-/* Create and return a new value handle node of type TYPE.  */
-
-tree
-make_value_handle (tree type)
-{
-  static unsigned int id = 0;
-  tree vh;
-
-  vh = build0 (VALUE_HANDLE, type);
-  VALUE_HANDLE_ID (vh) = id++;
-  return vh;
-}
-
-/* Compare two expressions E1 and E2 and return true if they are
-   equal.  */
-
-bool
-expressions_equal_p (tree e1, tree e2)
-{
-  tree te1, te2;
-
-  if (e1 == e2)
-    return true;
-
-  te1 = TREE_TYPE (e1);
-  te2 = TREE_TYPE (e2);
-
-  if (TREE_CODE (e1) == TREE_LIST && TREE_CODE (e2) == TREE_LIST)
-    {
-      tree lop1 = e1;
-      tree lop2 = e2;
-      for (lop1 = e1, lop2 = e2;
-	   lop1 || lop2;
-	   lop1 = TREE_CHAIN (lop1), lop2 = TREE_CHAIN (lop2))
-	{
-	  if (!lop1 || !lop2)
-	    return false;
-	  if (!expressions_equal_p (TREE_VALUE (lop1), TREE_VALUE (lop2)))
-	    return false;
-	}
-      return true;
-
-    }
-  else if (TREE_CODE (e1) == TREE_CODE (e2)
-	   && operand_equal_p (e1, e2, OEP_PURE_SAME))
-    return true;
-
-  return false;
-}
-
-/* Set the value handle for expression E to value V.  */
-
-void
-set_value_handle (tree e, tree v)
-{
-  if (TREE_CODE (e) == SSA_NAME)
-    SSA_NAME_VALUE (e) = v;
-  else if (EXPR_P (e) || DECL_P (e) || TREE_CODE (e) == TREE_LIST
-	   || GIMPLE_STMT_P (e)
-	   || TREE_CODE (e) == CONSTRUCTOR)
-    get_tree_common_ann (e)->value_handle = v;
-  else
-    /* Do nothing.  Constants are their own value handles.  */
-    gcc_assert (is_gimple_min_invariant (e));
-}
-
-/* Print out the "Created value <x> for <Y>" statement to the
-   dump_file.
-   This is factored because both versions of lookup use it, and it
-   obscures the real work going on in those functions.  */
-
-static void
-print_creation_to_file (tree v, tree expr, VEC (tree, gc) *vuses)
-{
-  fprintf (dump_file, "Created value ");
-  print_generic_expr (dump_file, v, dump_flags);
-  fprintf (dump_file, " for ");
-  print_generic_expr (dump_file, expr, dump_flags);
-
-  if (vuses && VEC_length (tree, vuses) != 0)
-    {
-      size_t i;
-      tree vuse;
-
-      fprintf (dump_file, " vuses: (");
-      for (i = 0; VEC_iterate (tree, vuses, i, vuse); i++)
-	{
-	  print_generic_expr (dump_file, vuse, dump_flags);
-	  if (VEC_length (tree, vuses) - 1 != i)
-	    fprintf (dump_file, ",");
-	}
-      fprintf (dump_file, ")");
-    }
-  fprintf (dump_file, "\n");
-}
-
-/* Sort the VUSE array so that we can do equality comparisons
-   quicker on two vuse vecs.  */
-
-void
-sort_vuses (VEC (tree,gc) *vuses)
-{
-  if (VEC_length (tree, vuses) > 1)
-    qsort (VEC_address (tree, vuses),
-	   VEC_length (tree, vuses),
-	   sizeof (tree),
-	   operand_build_cmp);
-}
-
-/* Sort the VUSE array so that we can do equality comparisons
-   quicker on two vuse vecs.  */
-
-void
-sort_vuses_heap (VEC (tree,heap) *vuses)
-{
-  if (VEC_length (tree, vuses) > 1)
-    qsort (VEC_address (tree, vuses),
-	   VEC_length (tree, vuses),
-	   sizeof (tree),
-	   operand_build_cmp);
-}
-
-/* Insert EXPR into VALUE_TABLE with value VAL, and add expression
-   EXPR to the value set for value VAL.  */
-
-void
-vn_add (tree expr, tree val)
-{
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_comparison:
-    case tcc_binary:
-      vn_nary_op_insert (expr, val);
-      break;
-    case tcc_unary:
-      vn_nary_op_insert (expr, val);
-      break;
-      /* In the case of array-refs of constants, for example, we can
-	 end up with no vuses.  */
-    case tcc_reference:
-      vn_reference_insert (expr, val, NULL);
-      break;
-      /* The *only* time CALL_EXPR should appear here is
-	 when it has no vuses.  */
-    case tcc_vl_exp:
-    case tcc_exceptional:
-    case tcc_expression:
-    case tcc_declaration:
-      if (TREE_CODE (expr) == CALL_EXPR || DECL_P (expr))
-	{
-	  vn_reference_insert (expr, val, NULL);
-	  break;
-	}
-      else if (TREE_CODE (expr) == SSA_NAME)
-	{
-	  SSA_NAME_VALUE (expr) = val;
-	  break;
-	}
-      switch (TREE_CODE (expr))
-	{
-	case ADDR_EXPR:
-	case TRUTH_AND_EXPR:
-	case TRUTH_OR_EXPR:
-	case TRUTH_XOR_EXPR:
-	case TRUTH_NOT_EXPR:
-	  vn_nary_op_insert (expr, val);
-	    break;
-	default:
-	  gcc_unreachable ();
-	}
-      break;
-    default:
-      gcc_unreachable ();
-    }
-  set_value_handle (expr, val);
-  if (TREE_CODE (val) == VALUE_HANDLE)
-    add_to_value (val, expr);
-}
-
-/* Insert EXPR into the value numbering tables with value VAL, and
-   add expression EXPR to the value set for value VAL.  VUSES
-   represents the virtual use operands associated with EXPR.  It is
-   used when computing a hash value for EXPR.  */
-
-void
-vn_add_with_vuses (tree expr, tree val, VEC (tree, gc) *vuses)
-{
-  if (!vuses)
-    {
-      vn_add (expr, val);
-      return;
-    }
-  vn_reference_insert (expr, val, vuses);
-
-  set_value_handle (expr, val);
-  if (TREE_CODE (val) == VALUE_HANDLE)
-    add_to_value (val, expr);
-}
-
-/* Lookup EXPR in the value numbering tables and return the result, if
-   we have one.  */
-
-tree
-vn_lookup (tree expr)
-{
-  /* Constants are their own value.  */
-  if (is_gimple_min_invariant (expr) || TREE_CODE (expr) == FIELD_DECL)
-    return expr;
-
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_comparison:
-    case tcc_binary:
-      return vn_nary_op_lookup (expr);
-    case tcc_unary:
-      return vn_nary_op_lookup (expr);
-      break;
-      /* In the case of array-refs of constants, for example, we can
-	 end up with no vuses.  */
-    case tcc_reference:
-      return vn_reference_lookup (expr, NULL, false);
-      break;
-      /* It is possible to have CALL_EXPR with no vuses for things
-	 like "cos", and these will fall into vn_lookup.   */
-    case tcc_vl_exp:
-    case tcc_exceptional:
-    case tcc_expression:
-    case tcc_declaration:
-      if (TREE_CODE (expr) == CALL_EXPR || DECL_P (expr))
-	return vn_reference_lookup (expr, NULL, false);
-      else if (TREE_CODE (expr) == SSA_NAME)
-	return SSA_NAME_VALUE (expr);
-      switch (TREE_CODE (expr))
-	{
-	case ADDR_EXPR:
-	case TRUTH_AND_EXPR:
-	case TRUTH_OR_EXPR:
-	case TRUTH_XOR_EXPR:
-	case TRUTH_NOT_EXPR:
-	  return vn_nary_op_lookup (expr);
-	default:
-	  gcc_unreachable ();
-	}
-      break;
-    default:
-      gcc_unreachable ();
-    }
-  return NULL;
-}
-
-/* Search in the value numbering tables for an existing instance of
-   expression EXPR,  and return its value, or NULL if none has been set.  STMT
-   represents the stmt associated with EXPR.  It is used when computing the
-   hash value for EXPR for reference operations.  */
-
-tree
-vn_lookup_with_stmt (tree expr, tree stmt)
-{
-  if (stmt == NULL)
-    return vn_lookup (expr);
-
-  /* Constants are their own value.  */
-  if (is_gimple_min_invariant (expr) || TREE_CODE (expr) == FIELD_DECL)
-    return expr;
-
-  return vn_lookup_with_vuses (expr, shared_vuses_from_stmt (stmt));
-}
-
-/* Search in VALUE_TABLE for an existing instance of expression EXPR,
-   and return its value, or NULL if none has been set.  VUSES is the
-   list of virtual use operands associated with EXPR.  It is used when
-   computing the hash value for EXPR.  */
-
-tree
-vn_lookup_with_vuses (tree expr, VEC (tree, gc) *vuses)
-{
-  if (!vuses || !VEC_length (tree, vuses))
-    return vn_lookup (expr);
-
-  if (is_gimple_min_invariant (expr) || TREE_CODE (expr) == FIELD_DECL)
-    return expr;
-
-  /* We may not walk the use-def chains here as the alias oracle cannot
-     properly deal with VALUE_HANDLE tree nodes we feed it here.  */
-  return vn_reference_lookup (expr, vuses, false);
-}
-
-static tree
-create_value_handle_for_expr (tree expr, VEC(tree, gc) *vuses)
-{
-  tree v;
-
-  v = make_value_handle (TREE_TYPE (expr));
-
-  if (dump_file && (dump_flags & TDF_DETAILS))
-    print_creation_to_file (v, expr, vuses);
-  return v;
-}
-
-/* Like vn_lookup, but creates a new value for the operation if one
-   does not exist.  */
-
-tree
-vn_lookup_or_add (tree expr)
-{
-  tree v = vn_lookup (expr);
-
-  if (v == NULL_TREE)
-    {
-      v = create_value_handle_for_expr (expr, NULL);
-      vn_add (expr, v);
-    }
-  else
-    set_value_handle (expr, v);
-
-  return v;
-}
-
-/* Like vn_lookup, but handles reference operations as well by using
-   STMT to get the set of vuses.  */
-
-tree
-vn_lookup_or_add_with_stmt (tree expr, tree stmt)
-{
-  tree v;
-  if (!stmt)
-    return vn_lookup_or_add (expr);
-
-  v = vn_lookup_with_stmt (expr, stmt);
-  if (v == NULL_TREE)
-    {
-      VEC (tree, gc) *vuses = copy_vuses_from_stmt (stmt);
-      v = create_value_handle_for_expr (expr, vuses);
-      vn_add_with_vuses (expr, v, vuses);
-    }
-  else
-    set_value_handle (expr, v);
-
-  return v;
-}
-
-/* Like vn_lookup, but creates a new value for expression EXPR, if
-   EXPR doesn't already have a value.  Return the existing/created
-   value for EXPR.  STMT represents the stmt associated with EXPR.  It is used
-   when computing the hash value for EXPR.  */
-
-tree
-vn_lookup_or_add_with_vuses (tree expr, VEC (tree, gc) *vuses)
-{
-  tree v;
-
-  if (!vuses || VEC_length (tree, vuses) == 0)
-    return vn_lookup_or_add (expr);
-
-  v = vn_lookup_with_vuses (expr, vuses);
-  if (v == NULL_TREE)
-    {
-      v = create_value_handle_for_expr (expr, vuses);
-      vn_add_with_vuses (expr, v, vuses);
-    }
-  else
-    set_value_handle (expr, v);
-
-  return v;
-}
-
Index: gcc/tree.def
===================================================================
--- gcc/tree.def	(revision 137479)
+++ gcc/tree.def	(working copy)
@@ -925,12 +925,6 @@ DEFTREECODE (POLYNOMIAL_CHREC, "polynomi
    Use the interface in tree-iterator.h to access this node.  */
 DEFTREECODE (STATEMENT_LIST, "statement_list", tcc_exceptional, 0)
 
-/* Value handles.  Artificial nodes to represent expressions in
-   partial redundancy elimination (tree-ssa-pre.c).  These nodes are
-   used for expression canonicalization.  If two expressions compute
-   the same value, they will be assigned the same value handle.  */
-DEFTREECODE (VALUE_HANDLE, "value_handle", tcc_exceptional, 0)
-
 /* Predicate assertion.  Artificial expression generated by the optimizers
    to keep track of predicate values.  This expression may only appear on
    the RHS of assignments.
Index: gcc/tree-ssa-pre.c
===================================================================
--- gcc/tree-ssa-pre.c	(revision 137479)
+++ gcc/tree-ssa-pre.c	(working copy)
@@ -46,6 +46,7 @@ along with GCC; see the file COPYING3.  
 #include "cfgloop.h"
 #include "tree-ssa-sccvn.h"
 #include "params.h"
+#include "dbgcnt.h"
 
 /* TODO:
 
@@ -116,172 +117,210 @@ along with GCC; see the file COPYING3.  
 
 /* Representations of value numbers:
 
-   Value numbers are represented using the "value handle" approach.
-   This means that each SSA_NAME (and for other reasons to be
-   disclosed in a moment, expression nodes) has a value handle that
-   can be retrieved through get_value_handle.  This value handle *is*
-   the value number of the SSA_NAME.  You can pointer compare the
-   value handles for equivalence purposes.
-
-   For debugging reasons, the value handle is internally more than
-   just a number, it is a VALUE_HANDLE named "VH.x", where x is a
-   unique number for each value number in use.  This allows
-   expressions with SSA_NAMES replaced by value handles to still be
-   pretty printed in a sane way.  They simply print as "VH.3 *
-   VH.5", etc.
-
-   Expression nodes have value handles associated with them as a
-   cache.  Otherwise, we'd have to look them up again in the hash
-   table.  This makes significant difference (factor of two or more) on
-   some test cases.  They can be thrown away after the pass is
-   finished.  */
+   Value numbers are represented by a representative SSA_NAME.  We
+   will create fake SSA_NAME's in situations where we need a
+   representative but do not have one (because it is a complex
+   expression).  In order to facilitate storing the value numbers in
+   bitmaps, and keep the number of wasted SSA_NAME's down, we also
+   associate a value_id with each value number, and create full blown
+   ssa_name's only where we actually need them (IE in operands of
+   existing expressions).  
+
+   Theoretically you could replace all the value_id's with
+   SSA_NAME_VERSION, but this would allocate a large number of
+   SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
+   It would also require an additional indirection at each point we
+   use the value id.  */
 
 /* Representation of expressions on value numbers:
 
-   In some portions of this code, you will notice we allocate "fake"
-   analogues to the expression we are value numbering, and replace the
-   operands with the values of the expression.  Since we work on
-   values, and not just names, we canonicalize expressions to value
-   expressions for use in the ANTIC sets, the EXP_GEN set, etc.
-
-   This is theoretically unnecessary, it just saves a bunch of
-   repeated get_value_handle and find_leader calls in the remainder of
-   the code, trading off temporary memory usage for speed.  The tree
-   nodes aren't actually creating more garbage, since they are
-   allocated in a special pools which are thrown away at the end of
-   this pass.
-
-   All of this also means that if you print the EXP_GEN or ANTIC sets,
-   you will see "VH.5 + VH.7" in the set, instead of "a_55 +
-   b_66" or something.  The only thing that actually cares about
-   seeing the value leaders is phi translation, and it needs to be
-   able to find the leader for a value in an arbitrary block, so this
-   "value expression" form is perfect for it (otherwise you'd do
-   get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
-
+   Expressions consisting of  value numbers are represented the same
+   way as our VN internally represents them, with an additional
+   "pre_expr" wrapping around them in order to facilitate storing all
+   of the expressions in the same sets.  */
 
 /* Representation of sets:
 
-   There are currently two types of sets used, hopefully to be unified soon.
-   The AVAIL sets do not need to be sorted in any particular order,
-   and thus, are simply represented as two bitmaps, one that keeps
-   track of values present in the set, and one that keeps track of
-   expressions present in the set.
-
-   The other sets are represented as doubly linked lists kept in topological
-   order, with an optional supporting bitmap of values present in the
-   set.  The sets represent values, and the elements can be values or
-   expressions.  The elements can appear in different sets, but each
-   element can only appear once in each set.
-
-   Since each node in the set represents a value, we also want to be
-   able to map expression, set pairs to something that tells us
-   whether the value is present is a set.  We use a per-set bitmap for
-   that.  The value handles also point to a linked list of the
-   expressions they represent via a tree annotation.  This is mainly
-   useful only for debugging, since we don't do identity lookups.  */
+   The dataflow sets do not need to be sorted in any particular order
+   for the majority of their lifetime, are simply represented as two
+   bitmaps, one that keeps track of values present in the set, and one
+   that keeps track of expressions present in the set.
+
+   When we need them in topological order, we produce it on demand by
+   transforming the bitmap into an array and sorting it into topo
+   order.  */
+
+/* Type of expression, used to know which member of the PRE_EXPR union
+   is valid.  */
+
+enum pre_expr_kind
+{
+    NAME,
+    NARY,
+    REFERENCE,
+    CONSTANT
+};
 
+typedef union pre_expr_union_d
+{
+  tree name;
+  tree constant;
+  vn_nary_op_t nary;
+  vn_reference_t reference;
+} pre_expr_union;
+
+typedef struct pre_expr_d
+{
+  enum pre_expr_kind kind;
+  unsigned int id;
+  pre_expr_union u;
+} *pre_expr;
+
+#define PRE_EXPR_NAME(e) (e)->u.name
+#define PRE_EXPR_NARY(e) (e)->u.nary
+#define PRE_EXPR_REFERENCE(e) (e)->u.reference
+#define PRE_EXPR_CONSTANT(e) (e)->u.constant
 
-/* Next global expression id number.  */
-static unsigned int next_expression_id;
+static int
+pre_expr_eq (const void *p1, const void *p2) 
+{
+  const struct pre_expr_d *e1 = (const struct pre_expr_d *) p1;
+  const struct pre_expr_d *e2 = (const struct pre_expr_d *) p2;
+  
+  if (e1->kind != e2->kind)
+    return false;
+  
+  switch (e1->kind) 
+    {
+    case CONSTANT:
+      return operand_equal_p (PRE_EXPR_CONSTANT (e1), PRE_EXPR_CONSTANT (e2),
+			      OEP_PURE_SAME);
+    case NAME:
+      return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
+    case NARY:
+      return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
+    case REFERENCE:
+      return vn_reference_eq (PRE_EXPR_REFERENCE (e1), PRE_EXPR_REFERENCE (e2));
+    default:
+      abort();
+    }
+}
+
+static hashval_t
+pre_expr_hash (const void *p1)
+{
+  const struct pre_expr_d *e = (const struct pre_expr_d *) p1;
+  switch (e->kind) 
+    {
+    case CONSTANT:
+      return iterative_hash_expr (PRE_EXPR_CONSTANT (e), 0);
+    case NAME:
+      return iterative_hash_expr (PRE_EXPR_NAME (e), 0);
+    case NARY:
+      return vn_nary_op_compute_hash (PRE_EXPR_NARY (e));
+    case REFERENCE:
+      return vn_reference_compute_hash (PRE_EXPR_REFERENCE (e));
+    default:
+      abort ();
+    }
+}
 
-typedef VEC(tree, gc) *vuse_vec;
-DEF_VEC_P (vuse_vec);
-DEF_VEC_ALLOC_P (vuse_vec, heap);
 
-static VEC(vuse_vec, heap) *expression_vuses;
+/* Next global expression id number.  */
+static unsigned int next_expression_id;
 
 /* Mapping from expression to id number we can use in bitmap sets.  */
-static VEC(tree, heap) *expressions;
-
+DEF_VEC_P (pre_expr);
+DEF_VEC_ALLOC_P (pre_expr, heap);
+static VEC(pre_expr, heap) *expressions;
+static htab_t expression_to_id;
+			   
 /* Allocate an expression id for EXPR.  */
 
 static inline unsigned int
-alloc_expression_id (tree expr)
+alloc_expression_id (pre_expr expr)
 {
-  tree_ann_common_t ann;
-
-  ann = get_tree_common_ann (expr);
-
+  void **slot;
   /* Make sure we won't overflow. */
   gcc_assert (next_expression_id + 1 > next_expression_id);
-
-  ann->aux = XNEW (unsigned int);
-  * ((unsigned int *)ann->aux) = next_expression_id++;
-  VEC_safe_push (tree, heap, expressions, expr);
-  VEC_safe_push (vuse_vec, heap, expression_vuses, NULL);
+  expr->id = next_expression_id++;
+  VEC_safe_push (pre_expr, heap, expressions, expr);
+  slot = htab_find_slot (expression_to_id, expr, INSERT);
+  gcc_assert (!*slot);
+  *slot = expr;
   return next_expression_id - 1;
 }
 
 /* Return the expression id for tree EXPR.  */
 
 static inline unsigned int
-get_expression_id (tree expr)
+get_expression_id (const pre_expr expr)
+{
+  return expr->id;
+}
+
+static inline unsigned int
+lookup_expression_id (const pre_expr expr) 
 {
-  tree_ann_common_t ann = tree_common_ann (expr);
-  gcc_assert (ann);
-  gcc_assert (ann->aux);
+  void **slot;
 
-  return  *((unsigned int *)ann->aux);
+  slot = htab_find_slot (expression_to_id, expr, NO_INSERT);
+  if (!slot)
+    return 0;
+  return ((pre_expr)*slot)->id;
 }
 
 /* Return the existing expression id for EXPR, or create one if one
    does not exist yet.  */
 
 static inline unsigned int
-get_or_alloc_expression_id (tree expr)
+get_or_alloc_expression_id (pre_expr expr)
 {
-  tree_ann_common_t ann = tree_common_ann (expr);
-
-  if (ann == NULL || !ann->aux)
+  unsigned int id = lookup_expression_id (expr);
+  if (id == 0)
     return alloc_expression_id (expr);
-
-  return get_expression_id (expr);
+  return expr->id = id;
 }
 
 /* Return the expression that has expression id ID */
 
-static inline tree
+static inline pre_expr
 expression_for_id (unsigned int id)
 {
-  return VEC_index (tree, expressions, id);
-}
-
-/* Return the expression vuses for EXPR, if there are any.  */
-
-static inline vuse_vec
-get_expression_vuses (tree expr)
-{
-  unsigned int expr_id = get_or_alloc_expression_id (expr);
-  return VEC_index (vuse_vec, expression_vuses, expr_id);
+  return VEC_index (pre_expr, expressions, id);
 }
 
-/* Set the expression vuses for EXPR to VUSES.  */
+/* Free the expression id field in all of our expressions,
+   and then destroy the expressions array.  */
 
-static inline void
-set_expression_vuses (tree expr, vuse_vec vuses)
+static void
+clear_expression_ids (void)
 {
-  unsigned int expr_id = get_or_alloc_expression_id (expr);
-  VEC_replace (vuse_vec, expression_vuses, expr_id, vuses);
+  VEC_free (pre_expr, heap, expressions);
 }
 
+static alloc_pool pre_expr_pool;
 
-/* Free the expression id field in all of our expressions,
-   and then destroy the expressions array.  */
+/* Given an SSA_NAME NAME, get or create a pre_expr to represent it.  */
 
-static void
-clear_expression_ids (void)
+static pre_expr
+get_or_alloc_expr_for_name (tree name)
 {
-  int i;
-  tree expr;
+  pre_expr result = (pre_expr) pool_alloc (pre_expr_pool);
+  unsigned int result_id;
 
-  for (i = 0; VEC_iterate (tree, expressions, i, expr); i++)
+  result->kind = NAME;
+  result->id = 0;
+  PRE_EXPR_NAME (result) = name;
+  result_id = lookup_expression_id (result);
+  if (result_id != 0)
     {
-      free (tree_common_ann (expr)->aux);
-      tree_common_ann (expr)->aux = NULL;
+      pool_free (pre_expr_pool, result);
+      result = expression_for_id (result_id);
+      return result;
     }
-  VEC_free (tree, heap, expressions);
-  VEC_free (vuse_vec, heap, expression_vuses);
+  get_or_alloc_expression_id (result);
+  return result;
 }
 
 static bool in_fre = false;
@@ -295,7 +334,12 @@ typedef struct bitmap_set
 } *bitmap_set_t;
 
 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi)		\
-  EXECUTE_IF_SET_IN_BITMAP(set->expressions, 0, id, bi)
+  EXECUTE_IF_SET_IN_BITMAP((set)->expressions, 0, (id), (bi))
+
+/* Mapping from value id to expressions with that value_id.  */
+DEF_VEC_P (bitmap_set_t);
+DEF_VEC_ALLOC_P (bitmap_set_t, heap);
+static VEC(bitmap_set_t, heap) *value_expressions;
 
 /* Sets that we need to keep track of.  */
 typedef struct bb_bitmap_sets
@@ -347,6 +391,7 @@ typedef struct bb_bitmap_sets
 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
 
+      
 /* Maximal set of values, used to initialize the ANTIC problem, which
    is an intersection problem.  */
 static bitmap_set_t maximal_set;
@@ -376,32 +421,24 @@ static struct
 } pre_stats;
 
 static bool do_partial_partial;
-static tree bitmap_find_leader (bitmap_set_t, tree, tree);
-static void bitmap_value_insert_into_set (bitmap_set_t, tree);
-static void bitmap_value_replace_in_set (bitmap_set_t, tree);
+static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int , tree);
+static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
+static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
-static bool bitmap_set_contains_value (bitmap_set_t, tree);
-static void bitmap_insert_into_set (bitmap_set_t, tree);
+static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
+static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
+static void bitmap_insert_into_set_1 (bitmap_set_t, pre_expr, bool);
 static bitmap_set_t bitmap_set_new (void);
-static tree create_expression_by_pieces (basic_block, tree, tree, tree);
-static tree find_or_generate_expression (basic_block, tree, tree, tree);
+static tree create_expression_by_pieces (basic_block, pre_expr, tree, tree,
+					 tree);
+static tree find_or_generate_expression (basic_block, pre_expr, tree, tree);
 
 /* We can add and remove elements and entries to and from sets
    and hash tables, so we use alloc pools for them.  */
 
 static alloc_pool bitmap_set_pool;
-static alloc_pool binary_node_pool;
-static alloc_pool unary_node_pool;
-static alloc_pool reference_node_pool;
-static alloc_pool comparison_node_pool;
 static bitmap_obstack grand_bitmap_obstack;
 
-/* We can't use allocation pools to hold temporary CALL_EXPR objects, since
-   they are not of fixed size.  Instead, use an obstack.  */
-
-static struct obstack temp_call_expr_obstack;
-
-
 /* To avoid adding 300 temporary variables when we only need one, we
    only create one temporary variable, on demand, and build ssa names
    off that.  We do have to change the variable if the types don't
@@ -428,16 +465,13 @@ static htab_t phi_translate_table;
 typedef struct expr_pred_trans_d
 {
   /* The expression.  */
-  tree e;
+  pre_expr e;
 
   /* The predecessor block along which we translated the expression.  */
   basic_block pred;
 
-  /* vuses associated with the expression.  */
-  VEC (tree, gc) *vuses;
-
   /* The value that resulted from the translation.  */
-  tree v;
+  pre_expr v;
 
   /* The hashcode for the expression, pred pair. This is cached for
      speed reasons.  */
@@ -464,50 +498,27 @@ expr_pred_trans_eq (const void *p1, cons
   const_expr_pred_trans_t const ve2 = (const_expr_pred_trans_t) p2;
   basic_block b1 = ve1->pred;
   basic_block b2 = ve2->pred;
-  int i;
-  tree vuse1;
 
   /* If they are not translations for the same basic block, they can't
      be equal.  */
   if (b1 != b2)
     return false;
-
-
-  /* If they are for the same basic block, determine if the
-     expressions are equal.  */
-  if (!expressions_equal_p (ve1->e, ve2->e))
-    return false;
-
-  /* Make sure the vuses are equivalent.  */
-  if (ve1->vuses == ve2->vuses)
-    return true;
-
-  if (VEC_length (tree, ve1->vuses) != VEC_length (tree, ve2->vuses))
-    return false;
-
-  for (i = 0; VEC_iterate (tree, ve1->vuses, i, vuse1); i++)
-    {
-      if (VEC_index (tree, ve2->vuses, i) != vuse1)
-	return false;
-    }
-
-  return true;
+  return pre_expr_eq (ve1->e, ve2->e);
 }
 
 /* Search in the phi translation table for the translation of
-   expression E in basic block PRED with vuses VUSES.
+   expression E in basic block PRED. 
    Return the translated value, if found, NULL otherwise.  */
 
-static inline tree
-phi_trans_lookup (tree e, basic_block pred, VEC (tree, gc) *vuses)
+static inline pre_expr
+phi_trans_lookup (pre_expr e, basic_block pred)
 {
   void **slot;
   struct expr_pred_trans_d ept;
 
   ept.e = e;
   ept.pred = pred;
-  ept.vuses = vuses;
-  ept.hashcode = iterative_hash_expr (e, (unsigned long) pred);
+  ept.hashcode = iterative_hash_hashval_t (pre_expr_hash (e), pred->index);
   slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
 				   NO_INSERT);
   if (!slot)
@@ -517,19 +528,20 @@ phi_trans_lookup (tree e, basic_block pr
 }
 
 
-/* Add the tuple mapping from {expression E, basic block PRED, vuses VUSES} to
+/* Add the tuple mapping from {expression E, basic block PRED} to
    value V, to the phi translation table.  */
 
 static inline void
-phi_trans_add (tree e, tree v, basic_block pred, VEC (tree, gc) *vuses)
+phi_trans_add (pre_expr e, pre_expr v, basic_block pred)
 {
   void **slot;
   expr_pred_trans_t new_pair = XNEW (struct expr_pred_trans_d);
   new_pair->e = e;
   new_pair->pred = pred;
-  new_pair->vuses = vuses;
   new_pair->v = v;
-  new_pair->hashcode = iterative_hash_expr (e, (unsigned long) pred);
+  new_pair->hashcode = iterative_hash_hashval_t (pre_expr_hash (e), 
+						 pred->index);
+  
   slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
 				   new_pair->hashcode, INSERT);
   if (*slot)
@@ -538,29 +550,27 @@ phi_trans_add (tree e, tree v, basic_blo
 }
 
 
-/* Return true if V is a value expression that represents itself.
-   In our world, this is *only* non-value handles.  */
-
-static inline bool
-constant_expr_p (tree v)
-{
-  return TREE_CODE (v) != VALUE_HANDLE &&
-    (TREE_CODE (v) == FIELD_DECL || is_gimple_min_invariant (v));
-}
-
-/* Add expression E to the expression set of value V.  */
+/* Add expression E to the expression set of value id V.  */
 
 void
-add_to_value (tree v, tree e)
+add_to_value (unsigned int v, pre_expr e)
 {
-  /* Constants have no expression sets.  */
-  if (constant_expr_p (v))
-    return;
-
-  if (VALUE_HANDLE_EXPR_SET (v) == NULL)
-    VALUE_HANDLE_EXPR_SET (v) = bitmap_set_new ();
+  bitmap_set_t set;
 
-  bitmap_insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
+  if (v >= VEC_length (bitmap_set_t, value_expressions))
+    {
+      VEC_safe_grow_cleared (bitmap_set_t, heap, value_expressions,
+			     v + 1);
+    }
+    
+  set = VEC_index (bitmap_set_t, value_expressions, v);
+  if (!set)
+    {
+      set = bitmap_set_new ();
+      VEC_replace (bitmap_set_t, value_expressions, v, set);
+    }
+  
+  bitmap_insert_into_set_1 (set, e, true);
 }
 
 /* Create a new bitmap set and return it.  */
@@ -574,36 +584,61 @@ bitmap_set_new (void)
   return ret;
 }
 
+/* Return the value id for a PRE expression EXPR.  */
+
+static unsigned int
+get_expr_value_id (pre_expr expr)
+{
+  switch (expr->kind)
+    {
+    case CONSTANT:
+      return get_or_alloc_constant_value_id (PRE_EXPR_CONSTANT (expr));
+    case NAME:
+      return VN_INFO (PRE_EXPR_NAME (expr))->value_id;
+    case NARY:
+      return PRE_EXPR_NARY (expr)->value_id;
+    case REFERENCE:
+      return PRE_EXPR_REFERENCE (expr)->value_id;
+    default:
+      gcc_unreachable ();
+    }
+}
+
 /* Remove an expression EXPR from a bitmapped set.  */
 
 static void
-bitmap_remove_from_set (bitmap_set_t set, tree expr)
+bitmap_remove_from_set (bitmap_set_t set, pre_expr expr)
 {
-  tree val = get_value_handle (expr);
-
-  gcc_assert (val);
-  if (!constant_expr_p (val))
+  unsigned int val  = get_expr_value_id (expr);
+  if (!value_id_constant_p (val)) 
     {
-      bitmap_clear_bit (set->values, VALUE_HANDLE_ID (val));
+      bitmap_clear_bit (set->values, val);
       bitmap_clear_bit (set->expressions, get_expression_id (expr));
     }
 }
 
-/* Insert an expression EXPR into a bitmapped set.  */
-
 static void
-bitmap_insert_into_set (bitmap_set_t set, tree expr)
+bitmap_insert_into_set_1 (bitmap_set_t set, pre_expr expr,
+			  bool allow_constants)
 {
-  tree val = get_value_handle (expr);
-
-  gcc_assert (val);
-  if (!constant_expr_p (val))
+  unsigned int val  = get_expr_value_id (expr);
+  if (allow_constants || !value_id_constant_p (val))
     {
-      bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
+      /* We specifically expect this and only this function to be able to
+	 insert constants into a set.  */
+      bitmap_set_bit (set->values, val);
       bitmap_set_bit (set->expressions, get_or_alloc_expression_id (expr));
     }
 }
 
+/* Insert an expression EXPR into a bitmapped set.  */
+
+static void
+bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
+{
+  bitmap_insert_into_set_1 (set, expr, false);
+}
+
 /* Copy a bitmapped set ORIG, into bitmapped set DEST.  */
 
 static void
@@ -624,40 +659,32 @@ bitmap_set_free (bitmap_set_t set)
 
 
 /* A comparison function for use in qsort to top sort a bitmap set.  Simply
-   subtracts value handle ids, since they are created in topo-order.  */
+   subtracts value ids, since they are created with leaves before
+   their parent users (IE topological order).  */
 
 static int
-vh_compare (const void *pa, const void *pb)
+value_id_compare (const void *pa, const void *pb)
 {
-  const tree vha = get_value_handle (*((const tree *)pa));
-  const tree vhb = get_value_handle (*((const tree *)pb));
+  const unsigned int vha = get_expr_value_id (*((const pre_expr *)pa));
+  const unsigned int vhb = get_expr_value_id (*((const pre_expr *)pb));
 
-  /* This can happen when we constify things.  */
-  if (constant_expr_p (vha))
-    {
-      if (constant_expr_p (vhb))
-	return -1;
-      return -1;
-    }
-  else if (constant_expr_p (vhb))
-    return 1;
-  return VALUE_HANDLE_ID (vha) - VALUE_HANDLE_ID (vhb);
+  return vha - vhb;
 }
 
 /* Generate an topological-ordered array of bitmap set SET.  */
 
-static VEC(tree, heap) *
+static VEC(pre_expr, heap) *
 sorted_array_from_bitmap_set (bitmap_set_t set)
 {
   unsigned int i;
   bitmap_iterator bi;
-  VEC(tree, heap) *result = NULL;
+  VEC(pre_expr, heap) *result = NULL;
 
   FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
-    VEC_safe_push (tree, heap, result, expression_for_id (i));
+    VEC_safe_push (pre_expr, heap, result, expression_for_id (i));
 
-  qsort (VEC_address (tree, result), VEC_length (tree, result),
-	 sizeof (tree), vh_compare);
+  qsort (VEC_address (pre_expr, result), VEC_length (pre_expr, result),
+	 sizeof (pre_expr), value_id_compare);
 
   return result;
 }
@@ -678,9 +705,9 @@ bitmap_set_and (bitmap_set_t dest, bitma
       bitmap_copy (temp, dest->expressions);
       EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
 	{
-	  tree expr = expression_for_id (i);
-	  tree val = get_value_handle (expr);
-	  if (!bitmap_bit_p (dest->values, VALUE_HANDLE_ID (val)))
+	  pre_expr expr = expression_for_id (i);
+	  unsigned int value_id = get_expr_value_id (expr);
+	  if (!bitmap_bit_p (dest->values, value_id))
 	    bitmap_clear_bit (dest->expressions, i);
 	}
       BITMAP_FREE (temp);
@@ -701,9 +728,9 @@ bitmap_set_subtract (bitmap_set_t dest, 
 
   FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
     {
-      tree expr = expression_for_id (i);
-      tree val = get_value_handle (expr);
-      bitmap_set_bit (result->values, VALUE_HANDLE_ID (val));
+      pre_expr expr = expression_for_id (i);
+      unsigned int value_id = get_expr_value_id (expr);
+      bitmap_set_bit (result->values, value_id);
     }
 
   return result;
@@ -721,30 +748,30 @@ bitmap_set_subtract_values (bitmap_set_t
   bitmap_copy (temp, a->expressions);
   EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
     {
-      tree expr = expression_for_id (i);
-      if (bitmap_set_contains_value (b, get_value_handle (expr)))
+      pre_expr expr = expression_for_id (i);
+      if (bitmap_set_contains_value (b, get_expr_value_id (expr)))
 	bitmap_remove_from_set (a, expr);
     }
   BITMAP_FREE (temp);
 }
 
 
-/* Return true if bitmapped set SET contains the value VAL.  */
+/* Return true if bitmapped set SET contains the value VALUE_ID.  */
 
 static bool
-bitmap_set_contains_value (bitmap_set_t set, tree val)
+bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
 {
-  if (constant_expr_p (val))
+  if (value_id_constant_p (value_id))
     return true;
 
   if (!set || bitmap_empty_p (set->expressions))
     return false;
 
-  return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
+  return bitmap_bit_p (set->values, value_id);
 }
 
 static inline bool
-bitmap_set_contains_expr (bitmap_set_t set, tree expr)
+bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
 {
   return bitmap_bit_p (set->expressions, get_expression_id (expr));
 }
@@ -752,13 +779,14 @@ bitmap_set_contains_expr (bitmap_set_t s
 /* Replace an instance of value LOOKFOR with expression EXPR in SET.  */
 
 static void
-bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
+bitmap_set_replace_value (bitmap_set_t set, unsigned int lookfor,
+			  const pre_expr expr)
 {
   bitmap_set_t exprset;
   unsigned int i;
   bitmap_iterator bi;
 
-  if (constant_expr_p (lookfor))
+  if (value_id_constant_p (lookfor))
     return;
 
   if (!bitmap_set_contains_value (set, lookfor))
@@ -773,7 +801,7 @@ bitmap_set_replace_value (bitmap_set_t s
      5-10x faster than walking the bitmap.  If this is somehow a
      significant lose for some cases, we can choose which set to walk
      based on the set size.  */
-  exprset = VALUE_HANDLE_EXPR_SET (lookfor);
+  exprset = VEC_index (bitmap_set_t, value_expressions, lookfor);
   FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
     {
       if (bitmap_bit_p (set->expressions, i))
@@ -797,9 +825,9 @@ bitmap_set_equal (bitmap_set_t a, bitmap
    and add it otherwise.  */
 
 static void
-bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
+bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
 {
-  tree val = get_value_handle (expr);
+  unsigned int val = get_expr_value_id (expr);
 
   if (bitmap_set_contains_value (set, val))
     bitmap_set_replace_value (set, val, expr);
@@ -811,17 +839,89 @@ bitmap_value_replace_in_set (bitmap_set_
    SET.  */
 
 static void
-bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
+bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
 {
-  tree val = get_value_handle (expr);
+  unsigned int val = get_expr_value_id (expr);
 
-  if (constant_expr_p (val))
+  if (value_id_constant_p (val))
     return;
 
   if (!bitmap_set_contains_value (set, val))
     bitmap_insert_into_set (set, expr);
 }
 
+/* Print out EXPR to outfile.  */
+
+static void
+print_pre_expr (FILE *outfile, const pre_expr expr) 
+{
+  switch (expr->kind)
+    {
+    case CONSTANT:
+      print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr), 0);
+      break;
+    case NAME:
+      print_generic_expr (outfile, PRE_EXPR_NAME (expr), 0);
+      break;
+    case NARY:
+      {
+	unsigned int i;
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	fprintf (outfile, "{%s,", tree_code_name [nary->opcode]);
+	for (i = 0; i < nary->length; i++)
+	  {
+	    print_generic_expr (outfile, nary->op[i], 0);
+	    if (i != (unsigned) nary->length - 1)
+	      fprintf (outfile, ",");
+	  }
+	fprintf (outfile, "}");
+      }
+      break;
+      
+    case REFERENCE:
+      {
+	vn_reference_op_t vro;
+	unsigned int i;
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	fprintf (outfile, "{");
+	for (i = 0;
+	     VEC_iterate (vn_reference_op_s, ref->operands, i, vro); 
+	     i++)
+	  {
+	    if (vro->opcode != SSA_NAME
+		&& TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
+	      fprintf (outfile, "%s ", tree_code_name [vro->opcode]);
+	    if (vro->op0)
+	      {
+		if (vro->op1)
+		  fprintf (outfile, "<");
+		print_generic_expr (outfile, vro->op0, 0);
+		if (vro->op1)
+		  {
+		    fprintf (outfile, ",");
+		    print_generic_expr (outfile, vro->op1, 0);
+		  }
+		if (vro->op1)
+		  fprintf (outfile, ">");
+	      }
+	    if (i != VEC_length (vn_reference_op_s, ref->operands) - 1)
+	      fprintf (outfile, ",");
+	  }
+	fprintf (outfile, "}");
+      }
+      break;
+    }
+}
+void debug_pre_expr (pre_expr);
+
+/* Like print_pre_expr but always prints to stderr.  */
+void
+debug_pre_expr (pre_expr e)
+{
+  print_pre_expr (stderr, e);
+  fprintf (stderr, "\n");
+}
+
 /* Print out SET to OUTFILE.  */
 
 static void
@@ -837,16 +937,14 @@ print_bitmap_set (FILE *outfile, bitmap_
 
       FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
 	{
-	  tree expr = expression_for_id (i);
+	  const pre_expr expr = expression_for_id (i);
 
 	  if (!first)
 	    fprintf (outfile, ", ");
 	  first = false;
-	  print_generic_expr (outfile, expr, 0);
+	  print_pre_expr (outfile, expr);
 
-	  fprintf (outfile, " (");
-	  print_generic_expr (outfile, get_value_handle (expr), 0);
-	  fprintf (outfile, ") ");
+	  fprintf (outfile, " (%04d)", get_expr_value_id (expr));
 	}
     }
   fprintf (outfile, " }\n");
@@ -863,42 +961,141 @@ debug_bitmap_set (bitmap_set_t set)
 /* Print out the expressions that have VAL to OUTFILE.  */
 
 void
-print_value_expressions (FILE *outfile, tree val)
+print_value_expressions (FILE *outfile, unsigned int val)
 {
-  if (VALUE_HANDLE_EXPR_SET (val))
+  bitmap_set_t set = VEC_index (bitmap_set_t, value_expressions, val);
+  if (set)
     {
       char s[10];
-      sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
-      print_bitmap_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
+      sprintf (s, "%04d", val);
+      print_bitmap_set (outfile, set, s, 0);
     }
 }
 
 
 void
-debug_value_expressions (tree val)
+debug_value_expressions (unsigned int val)
 {
   print_value_expressions (stderr, val);
 }
 
-/* Return the folded version of T if T, when folded, is a gimple
-   min_invariant.  Otherwise, return T.  */
+/* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
+   represent it.  */
 
-static tree
-fully_constant_expression (tree t)
+static pre_expr
+get_or_alloc_expr_for_constant (tree constant)
+{
+  unsigned int result_id;
+  unsigned int value_id;
+  pre_expr newexpr = (pre_expr) pool_alloc (pre_expr_pool);
+  newexpr->kind = CONSTANT;
+  PRE_EXPR_CONSTANT (newexpr) = constant;
+  result_id = lookup_expression_id (newexpr);
+  if (result_id != 0)
+    {
+      pool_free (pre_expr_pool, newexpr);
+      newexpr = expression_for_id (result_id);
+      return newexpr;
+    }
+  value_id = get_or_alloc_constant_value_id (constant);
+  get_or_alloc_expression_id (newexpr);
+  add_to_value (value_id, newexpr);
+  return newexpr;
+}
+
+/* Given a value id V, find the actual tree representing the constant
+   value if there is one, and return it. Return NULL if we can't find
+   a constant.  */
+
+static tree 
+get_constant_for_value_id (unsigned int v)
 {
-  tree folded;
-  folded = fold (t);
-  if (folded && is_gimple_min_invariant (folded))
-    return folded;
-  return t;
+  if (value_id_constant_p (v))
+    {
+      unsigned int i;
+      bitmap_iterator bi;
+      bitmap_set_t exprset = VEC_index (bitmap_set_t, value_expressions, v);
+      
+      FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
+	{
+	  pre_expr expr = expression_for_id (i);
+	  if (expr->kind == CONSTANT)
+	    return PRE_EXPR_CONSTANT (expr);
+	}
+    }
+  return NULL;
 }
 
-/* Make a temporary copy of a CALL_EXPR object NODE.  */
+/* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
+   Currently only supports constants and SSA_NAMES.  */
+static pre_expr
+get_or_alloc_expr_for (tree t)
+{
+  if (TREE_CODE (t) == SSA_NAME)
+    return get_or_alloc_expr_for_name (t);
+  else if (is_gimple_min_invariant (t))
+    return get_or_alloc_expr_for_constant (t);
+  return NULL;
+}
 
-static tree
-temp_copy_call_expr (tree node)
+/* Return the folded version of T if T, when folded, is a gimple
+   min_invariant.  Otherwise, return T.  */
+
+static pre_expr
+fully_constant_expression (pre_expr e)
 {
-  return (tree) obstack_copy (&temp_call_expr_obstack, node, tree_size (node));
+  switch (e->kind) 
+    {
+    case CONSTANT:
+      return e;
+    case NARY:
+      {
+	vn_nary_op_t nary = PRE_EXPR_NARY (e);
+	switch (TREE_CODE_CLASS (nary->opcode))
+	  {
+	  case tcc_binary:
+	    {
+	      /* We have to go from trees to pre exprs to value ids to
+		 constants.  */
+	      tree naryop0 = nary->op[0];
+	      tree naryop1 = nary->op[1];
+	      pre_expr rep0 = get_or_alloc_expr_for (naryop0);
+	      pre_expr rep1 = get_or_alloc_expr_for (naryop1);
+	      unsigned int vrep0 = get_expr_value_id (rep0);
+	      unsigned int vrep1 = get_expr_value_id (rep1);	      
+	      tree const0 = get_constant_for_value_id (vrep0);
+	      tree const1 = get_constant_for_value_id (vrep1);
+	      tree result = NULL;
+	      if (const0 && const1)
+		result = fold_binary (nary->opcode, nary->type, const0,
+				      const1);
+	      if (result && is_gimple_min_invariant (result))
+		return get_or_alloc_expr_for_constant (result);
+	      return e;
+	    }
+	  case tcc_unary:
+	    {
+	    /* We have to go from trees to pre exprs to value ids to
+	       constants.  */
+	      tree naryop0 = nary->op[0];
+	      pre_expr rep0 = get_or_alloc_expr_for (naryop0);
+	      unsigned int vrep0 = get_expr_value_id (rep0);
+	      tree const0 = get_constant_for_value_id (vrep0);
+	      tree result = NULL;
+	      if (const0)
+		result = fold_unary (nary->opcode, nary->type, const0);
+	      if (result && is_gimple_min_invariant (result))
+		return get_or_alloc_expr_for_constant (result);
+	      return e;
+	    }
+	  default:
+	    return e;
+	  }
+      }
+    default:
+      return e;
+    }
+  return e;
 }
 
 /* Translate the vuses in the VUSES vector backwards through phi nodes
@@ -949,44 +1146,155 @@ translate_vuses_through_block (VEC (tree
    SET2.  This is used to avoid making a set consisting of the union
    of PA_IN and ANTIC_IN during insert.  */
 
-static inline tree
-find_leader_in_sets (tree expr, bitmap_set_t set1, bitmap_set_t set2)
+static inline pre_expr
+find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2)
 {
-  tree result;
+  pre_expr result;
 
-  result = bitmap_find_leader (set1, expr, NULL_TREE);
+  result = bitmap_find_leader (set1, val, NULL_TREE);
   if (!result && set2)
-    result = bitmap_find_leader (set2, expr, NULL_TREE);
+    result = bitmap_find_leader (set2, val, NULL_TREE);
   return result;
 }
 
-/* Translate EXPR using phis in PHIBLOCK, so that it has the values of
-   the phis in PRED.  SEEN is a bitmap saying which expression we have
-   translated since we started translation of the toplevel expression.
-   Return NULL if we can't find a leader for each part of the
-   translated expression.  */
+/* Get the tree type for our PRE expression e.  */
 
 static tree
-phi_translate_1 (tree expr, bitmap_set_t set1, bitmap_set_t set2,
-		 basic_block pred, basic_block phiblock, bitmap seen)
+get_expr_type (const pre_expr e) 
 {
-  tree phitrans = NULL;
-  tree oldexpr = expr;
+  switch (e->kind) 
+    {
+    case NAME:
+      return TREE_TYPE (PRE_EXPR_NAME (e));
+    case CONSTANT:
+      return TREE_TYPE (PRE_EXPR_CONSTANT (e));
+    case REFERENCE:
+      {
+	vn_reference_op_t vro;
 
-  if (expr == NULL)
-    return NULL;
+	gcc_assert (PRE_EXPR_REFERENCE (e)->operands);
+	vro = VEC_index (vn_reference_op_s,
+			 PRE_EXPR_REFERENCE (e)->operands,
+			 0);
+	/* We don't store type along with COMPONENT_REF because it is
+	   always the same as FIELD_DECL's type.  */
+	if (!vro->type)
+	  {
+	    gcc_assert (vro->opcode == COMPONENT_REF);
+	    return TREE_TYPE (vro->op0);
+	  }
+	return vro->type;
+      }
 
-  if (constant_expr_p (expr))
-    return expr;
+    case NARY:
+      return PRE_EXPR_NARY (e)->type;
+    }
+  gcc_unreachable();
+}
 
-  /* Phi translations of a given expression don't change.  */
-  if (EXPR_P (expr) || GIMPLE_STMT_P (expr))
+/* Get a representative SSA_NAME for a given expression.
+   Since all of our sub-expressions are treated as values, we require
+   them to be SSA_NAME's for simplicity.
+   Prior versions of GVNPRE used to use "value handles" here, so that
+   an expression would be VH.11 + VH.10 instead of d_3 + e_6.  In
+   either case, the operands are really values (IE we do not expect
+   them to be usable without finding leaders).  */
+
+static tree
+get_representative_for (const pre_expr e) 
+{
+  tree exprtype;
+  tree name;
+  unsigned int value_id = get_expr_value_id (e);
+
+  switch (e->kind)
+    {
+    case NAME:
+      return PRE_EXPR_NAME (e);
+    case CONSTANT:
+    case NARY:      
+    case REFERENCE:
+      {
+	/* Go through all of the expressions representing this value
+	   and pick out an SSA_NAME.  */
+	unsigned int i;
+	bitmap_iterator bi;
+	bitmap_set_t exprs = VEC_index (bitmap_set_t, value_expressions,
+					value_id);
+	FOR_EACH_EXPR_ID_IN_SET (exprs, i, bi)
+	  {
+	    pre_expr rep = expression_for_id (i);
+	    if (rep->kind == NAME)
+	      return PRE_EXPR_NAME (rep);
+	  }
+      }
+      break;
+    }
+  /* If we reached here we couldn't find an SSA_NAME.  This can
+     happen when we've discovered a value that has never appeared in
+     the program as set to an SSA_NAME, most likely as the result of
+     phi translation.  */
+  if (dump_file)
+    {
+      fprintf (dump_file,
+	       "Could not find SSA_NAME representative for expression:");
+      print_pre_expr (dump_file, e);
+      fprintf (dump_file, "\n");
+    }
+  
+  exprtype = get_expr_type (e);
+  
+  /* Build and insert the assignment of the end result to the temporary
+     that we will return.  */
+  if (!pretemp || exprtype != TREE_TYPE (pretemp))
     {
-      phitrans = phi_trans_lookup (expr, pred, get_expression_vuses (expr));
+      pretemp = create_tmp_var (exprtype, "pretmp");
+      get_var_ann (pretemp);
     }
+
+  name = make_ssa_name (pretemp, build_empty_stmt ());
+  VN_INFO_GET (name)->value_id = value_id;
+  if (e->kind == CONSTANT)
+    VN_INFO (name)->valnum = PRE_EXPR_CONSTANT (e);
   else
-    phitrans = phi_trans_lookup (expr, pred, NULL);
+    VN_INFO (name)->valnum = name;
+
+  add_to_value (value_id, get_or_alloc_expr_for_name (name));
+  if (dump_file)
+    {
+      fprintf (dump_file, "Created SSA_NAME representative ");
+      print_generic_expr (dump_file, name, 0);
+      fprintf (dump_file, " for expression:");
+      print_pre_expr (dump_file, e);
+      fprintf (dump_file, "\n");
+    }
+
+  return name;  
+}
 
+	    
+
+      
+/* Translate EXPR using phis in PHIBLOCK, so that it has the values of
+   the phis in PRED.  SEEN is a bitmap saying which expression we have
+   translated since we started translation of the toplevel expression.
+   Return NULL if we can't find a leader for each part of the
+   translated expression.  */
+
+static pre_expr
+phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
+		 basic_block pred, basic_block phiblock, bitmap seen)
+{
+  pre_expr oldexpr = expr;
+  pre_expr phitrans;
+  
+  if (!expr)
+    return NULL;
+  
+  if (value_id_constant_p (get_expr_value_id (expr)))
+    return expr;
+  
+  phitrans = phi_trans_lookup (expr, pred);
   if (phitrans)
     return phitrans;
 
@@ -1000,317 +1308,223 @@ phi_translate_1 (tree expr, bitmap_set_t
       bitmap_set_bit (seen, expr_id);
     }
 
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
+  switch (expr->kind)
     {
-    case tcc_expression:
-      return NULL;
-
-    case tcc_vl_exp:
+      /* Constants contain no values that need translation.  */
+    case CONSTANT:
+      return expr;
+      
+    case NARY:
       {
-	if (TREE_CODE (expr) != CALL_EXPR)
-	  return NULL;
-	else
+	unsigned int i;
+	bool changed = false;	
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	struct vn_nary_op_s newnary;
+	/* The NARY structure is only guaranteed to have been
+	   allocated to the nary->length operands.  */
+	memcpy (&newnary, nary, (sizeof (struct vn_nary_op_s)
+				 - sizeof (tree) * (4 - nary->length)));
+	
+	for (i = 0; i < newnary.length; i++)
 	  {
-	    tree oldfn = CALL_EXPR_FN (expr);
-	    tree oldsc = CALL_EXPR_STATIC_CHAIN (expr);
-	    tree newfn, newsc = NULL;
-	    tree newexpr = NULL_TREE;
-	    bool invariantarg = false;
-	    int i, nargs;
-	    VEC (tree, gc) *vuses = get_expression_vuses (expr);
-	    VEC (tree, gc) *tvuses;
-
-	    newfn = phi_translate_1 (find_leader_in_sets (oldfn, set1, set2),
-				     set1, set2, pred, phiblock, seen);
-	    if (newfn == NULL)
-	      return NULL;
-	    if (newfn != oldfn)
-	      {
-		newexpr = temp_copy_call_expr (expr);
-		CALL_EXPR_FN (newexpr) = get_value_handle (newfn);
-	      }
-	    if (oldsc)
-	      {
-		newsc = phi_translate_1 (find_leader_in_sets (oldsc, set1, set2),
-					 set1, set2, pred, phiblock, seen);
-		if (newsc == NULL)
-		  return NULL;
-		if (newsc != oldsc)
-		  {
-		    if (!newexpr)
-		      newexpr = temp_copy_call_expr (expr);
-		    CALL_EXPR_STATIC_CHAIN (newexpr) = get_value_handle (newsc);
-		  }
-	      }
-
-	    /* phi translate the argument list piece by piece.  */
-	    nargs = call_expr_nargs (expr);
-	    for (i = 0; i < nargs; i++)
+	    if (TREE_CODE (newnary.op[i]) != SSA_NAME)
+	      continue;
+	    else
 	      {
-		tree oldval = CALL_EXPR_ARG (expr, i);
-		tree newval;
-		if (oldval)
+		unsigned int op_val_id = VN_INFO (newnary.op[i])->value_id;
+		pre_expr leader = find_leader_in_sets (op_val_id, set1, set2);
+		pre_expr result = phi_translate_1 (leader, set1, set2,
+						   pred, phiblock, seen);
+		if (result && result != leader)
 		  {
-		    /* This may seem like a weird place for this
-		       check, but it's actually the easiest place to
-		       do it.  We can't do it lower on in the
-		       recursion because it's valid for pieces of a
-		       component ref to be of AGGREGATE_TYPE, as long
-		       as the outermost one is not.
-		       To avoid *that* case, we have a check for
-		       AGGREGATE_TYPE_P in insert_aux.  However, that
-		       check will *not* catch this case because here
-		       it occurs in the argument list.  */
-		    if (AGGREGATE_TYPE_P (TREE_TYPE (oldval)))
-		      return NULL;
-		    oldval = find_leader_in_sets (oldval, set1, set2);
-		    newval = phi_translate_1 (oldval, set1, set2, pred,
-					    phiblock, seen);
-		    if (newval == NULL)
+		    tree name = get_representative_for (result);
+		    if (!name)
 		      return NULL;
-		    if (newval != oldval)
-		      {
-			invariantarg |= is_gimple_min_invariant (newval);
-			if (!newexpr)
-			  newexpr = temp_copy_call_expr (expr);
-			CALL_EXPR_ARG (newexpr, i) = get_value_handle (newval);
-		      }
-		  }
-	      }
-
-	    /* In case of new invariant args we might try to fold the call
-	       again.  */
-	    if (invariantarg && !newsc)
-	      {
-		tree tmp1 = build_call_array (TREE_TYPE (expr),
-					      newfn, call_expr_nargs (newexpr),
-					      CALL_EXPR_ARGP (newexpr));
-		tree tmp2 = fold (tmp1);
-		if (tmp2 != tmp1)
-		  {
-		    STRIP_TYPE_NOPS (tmp2);
-		    if (is_gimple_min_invariant (tmp2))
-		      return tmp2;
+		    newnary.op[i] = name;
 		  }
-	      }
-
-	    tvuses = translate_vuses_through_block (vuses, phiblock, pred);
-	    if (vuses != tvuses && ! newexpr)
-	      newexpr = temp_copy_call_expr (expr);
-
-	    if (newexpr)
-	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add_with_vuses (newexpr, tvuses);
-		expr = newexpr;
-		set_expression_vuses (newexpr, tvuses);
-	      }
-	    phi_trans_add (oldexpr, expr, pred, tvuses);
-	  }
-      }
-      return expr;
-
-    case tcc_declaration:
-      {
-	VEC (tree, gc) * oldvuses = NULL;
-	VEC (tree, gc) * newvuses = NULL;
-
-	oldvuses = get_expression_vuses (expr);
-	if (oldvuses)
-	  newvuses = translate_vuses_through_block (oldvuses, phiblock,
-						    pred);
-
-	if (oldvuses != newvuses)
-	  {
-	    vn_lookup_or_add_with_vuses (expr, newvuses);
-	    set_expression_vuses (expr, newvuses);
-	  }
-	phi_trans_add (oldexpr, expr, pred, newvuses);
-      }
-      return expr;
-
-    case tcc_reference:
-      {
-	tree oldop0 = TREE_OPERAND (expr, 0);
-	tree oldop1 = NULL;
-	tree newop0;
-	tree newop1 = NULL;
-	tree oldop2 = NULL;
-	tree newop2 = NULL;
-	tree oldop3 = NULL;
-	tree newop3 = NULL;
-	tree newexpr;
-	VEC (tree, gc) * oldvuses = NULL;
-	VEC (tree, gc) * newvuses = NULL;
-
-	if (TREE_CODE (expr) != INDIRECT_REF
-	    && TREE_CODE (expr) != COMPONENT_REF
-	    && TREE_CODE (expr) != ARRAY_REF)
-	  return NULL;
-
-	oldop0 = find_leader_in_sets (oldop0, set1, set2);
-	newop0 = phi_translate_1 (oldop0, set1, set2, pred, phiblock, seen);
-	if (newop0 == NULL)
-	  return NULL;
-
-	if (TREE_CODE (expr) == ARRAY_REF)
-	  {
-	    oldop1 = TREE_OPERAND (expr, 1);
-	    oldop1 = find_leader_in_sets (oldop1, set1, set2);
-	    newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
-
-	    if (newop1 == NULL)
-	      return NULL;
-
-	    oldop2 = TREE_OPERAND (expr, 2);
-	    if (oldop2)
-	      {
-		oldop2 = find_leader_in_sets (oldop2, set1, set2);
-		newop2 = phi_translate_1 (oldop2, set1, set2, pred, phiblock, seen);
-
-		if (newop2 == NULL)
-		  return NULL;
-	      }
-	    oldop3 = TREE_OPERAND (expr, 3);
-	    if (oldop3)
-	      {
-		oldop3 = find_leader_in_sets (oldop3, set1, set2);
-		newop3 = phi_translate_1 (oldop3, set1, set2, pred, phiblock, seen);
-
-		if (newop3 == NULL)
+		else if (!result)
 		  return NULL;
+		
+		changed |= newnary.op[i] != nary->op[i];
 	      }
 	  }
-
-	oldvuses = get_expression_vuses (expr);
-	if (oldvuses)
-	  newvuses = translate_vuses_through_block (oldvuses, phiblock,
-						    pred);
-
-	if (newop0 != oldop0 || newvuses != oldvuses
-	    || newop1 != oldop1
-	    || newop2 != oldop2
-	    || newop3 != oldop3)
+	if (changed)
 	  {
-	    tree t;
+	    pre_expr constant;
+	    
+	    tree result = vn_nary_op_lookup_pieces (newnary.length,
+						    newnary.opcode,
+						    newnary.type,
+						    newnary.op[0],
+						    newnary.op[1],
+						    newnary.op[2],
+						    newnary.op[3],
+						    &nary);
+	    unsigned int new_val_id;
+	    
+	    expr = (pre_expr) pool_alloc (pre_expr_pool);
+	    expr->kind = NARY;
+	    expr->id = 0;
+	    if (result && is_gimple_min_invariant (result))
+	      return get_or_alloc_expr_for_constant (result);
 
-	    newexpr = (tree) pool_alloc (reference_node_pool);
-	    memcpy (newexpr, expr, tree_size (expr));
-	    TREE_OPERAND (newexpr, 0) = get_value_handle (newop0);
-	    if (TREE_CODE (expr) == ARRAY_REF)
-	      {
-		TREE_OPERAND (newexpr, 1) = get_value_handle (newop1);
-		if (newop2)
-		  TREE_OPERAND (newexpr, 2) = get_value_handle (newop2);
-		if (newop3)
-		  TREE_OPERAND (newexpr, 3) = get_value_handle (newop3);
-	      }
 
-	    t = fully_constant_expression (newexpr);
-
-	    if (t != newexpr)
+	    if (nary)
 	      {
-		pool_free (reference_node_pool, newexpr);
-		newexpr = t;
+		PRE_EXPR_NARY (expr) = nary;
+		constant = fully_constant_expression (expr);
+		if (constant != expr)
+		  return constant;
+
+		new_val_id = nary->value_id;
+		get_or_alloc_expression_id (expr);
 	      }
 	    else
 	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add_with_vuses (newexpr, newvuses);
-		set_expression_vuses (newexpr, newvuses);
+		new_val_id = get_next_value_id ();
+		VEC_safe_grow_cleared (bitmap_set_t, heap,
+				       value_expressions,
+				       get_max_value_id() + 1);
+		nary = vn_nary_op_insert_pieces (newnary.length,
+						 newnary.opcode,
+						 newnary.type,
+						 newnary.op[0],
+						 newnary.op[1],
+						 newnary.op[2],
+						 newnary.op[3],
+						 result, new_val_id);
+		PRE_EXPR_NARY (expr) = nary;
+		constant = fully_constant_expression (expr);
+		if (constant != expr)
+		  return constant;
+		get_or_alloc_expression_id (expr);
+		add_to_value (new_val_id, expr);
 	      }
-	    expr = newexpr;
 	  }
-	phi_trans_add (oldexpr, expr, pred, newvuses);
+	phi_trans_add (oldexpr, expr, pred);
+	return expr;
       }
-      return expr;
       break;
-
-    case tcc_binary:
-    case tcc_comparison:
+    case REFERENCE:
       {
-	tree oldop1 = TREE_OPERAND (expr, 0);
-	tree oldval1 = oldop1;
-	tree oldop2 = TREE_OPERAND (expr, 1);
-	tree oldval2 = oldop2;
-	tree newop1;
-	tree newop2;
-	tree newexpr;
-
-	oldop1 = find_leader_in_sets (oldop1, set1, set2);
-	newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
-	if (newop1 == NULL)
-	  return NULL;
-
-	oldop2 = find_leader_in_sets (oldop2, set1, set2);
-	newop2 = phi_translate_1 (oldop2, set1, set2, pred, phiblock, seen);
-	if (newop2 == NULL)
-	  return NULL;
-	if (newop1 != oldop1 || newop2 != oldop2)
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	VEC (vn_reference_op_s, heap) *operands = ref->operands;
+	VEC (tree, gc) *vuses = ref->vuses;
+	VEC (tree, gc) *newvuses = vuses;	
+	VEC (vn_reference_op_s, heap) *newoperands = NULL;
+	bool changed = false;
+	unsigned int i;
+	vn_reference_op_t operand;
+	vn_reference_t newref;
+
+	for (i = 0; VEC_iterate (vn_reference_op_s, operands, i, operand); i++)
 	  {
-	    tree t;
-	    newexpr = (tree) pool_alloc (binary_node_pool);
-	    memcpy (newexpr, expr, tree_size (expr));
-	    TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldval1 : get_value_handle (newop1);
-	    TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldval2 : get_value_handle (newop2);
-	    t = fully_constant_expression (newexpr);
-	    if (t != newexpr)
+	    pre_expr opresult;
+	    pre_expr leader;
+	    tree oldop0 = operand->op0;
+	    tree oldop1 = operand->op1;
+	    tree op0 = oldop0;
+	    tree op1 = oldop1;
+	    tree type = operand->type;
+	    vn_reference_op_s newop = *operand;
+
+	    if (op0 && TREE_CODE (op0) == SSA_NAME)
 	      {
-		pool_free (binary_node_pool, newexpr);
-		newexpr = t;
+		unsigned int op_val_id = VN_INFO (op0)->value_id;
+		leader = find_leader_in_sets (op_val_id, set1, set2);
+		opresult = phi_translate_1 (leader, set1, set2,
+					    pred, phiblock, seen);
+		if (opresult && opresult != leader)
+		  {
+		    tree name = get_representative_for (opresult);
+		    if (!name)
+		      return NULL;		    
+		    op0 = name;
+		  }
+		else if (!opresult)
+		  return NULL;
 	      }
-	    else
+	    changed |= op0 != oldop0;
+	    
+	    if (op1 && TREE_CODE (op1) == SSA_NAME)
 	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add (newexpr);
+		unsigned int op_val_id = VN_INFO (op1)->value_id;
+		leader = find_leader_in_sets (op_val_id, set1, set2);
+		opresult = phi_translate_1 (leader, set1, set2,
+					    pred, phiblock, seen);
+		if (opresult && opresult != leader)
+		  {
+		    tree name = get_representative_for (opresult);
+		    if (!name)
+		      return NULL;		    
+		    op1 = name;
+		  }
+		else if (!opresult)
+		  return NULL;
 	      }
-	    expr = newexpr;
+	    changed |= op1 != oldop1;
+	    if (!newoperands)
+	      newoperands = VEC_copy (vn_reference_op_s, heap, operands);
+	    /* We may have changed from an SSA_NAME to a constant */
+	    if (newop.opcode == SSA_NAME && TREE_CODE (op0) != SSA_NAME)
+	      newop.opcode = TREE_CODE (op0);
+	    newop.type = type;
+	    newop.op0 = op0;
+	    newop.op1 = op1;
+	    VEC_replace (vn_reference_op_s, newoperands, i, &newop);
 	  }
-	phi_trans_add (oldexpr, expr, pred, NULL);
-      }
-      return expr;
+	
+	newvuses = translate_vuses_through_block (vuses, phiblock, pred);
+	changed |= newvuses != vuses;
 
-    case tcc_unary:
-      {
-	tree oldop1 = TREE_OPERAND (expr, 0);
-	tree newop1;
-	tree newexpr;
-
-	oldop1 = find_leader_in_sets (oldop1, set1, set2);
-	newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
-	if (newop1 == NULL)
-	  return NULL;
-	if (newop1 != oldop1)
+	if (changed)
 	  {
-	    tree t;
-	    newexpr = (tree) pool_alloc (unary_node_pool);
-	    memcpy (newexpr, expr, tree_size (expr));
-	    TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
-	    t = fully_constant_expression (newexpr);
-	    if (t != newexpr)
+	    tree result = vn_reference_lookup_pieces (newvuses,
+						      newoperands,
+						      &newref);
+	    unsigned int new_val_id;
+
+	    if (newref)
+	      VEC_free (vn_reference_op_s, heap, newoperands);
+	    
+	    if (result && is_gimple_min_invariant (result))
+	      return get_or_alloc_expr_for_constant (result);
+
+	    expr = (pre_expr) pool_alloc (pre_expr_pool);
+	    expr->kind = REFERENCE;
+	    expr->id = 0;
+
+	    if (newref)
 	      {
-		pool_free (unary_node_pool, newexpr);
-		newexpr = t;
+		PRE_EXPR_REFERENCE (expr) = newref;
+		new_val_id = newref->value_id;
+		get_or_alloc_expression_id (expr);
 	      }
 	    else
 	      {
-		newexpr->base.ann = NULL;
-		vn_lookup_or_add (newexpr);
-	      }
-	    expr = newexpr;
+		new_val_id = get_next_value_id ();
+		VEC_safe_grow_cleared (bitmap_set_t, heap, value_expressions,
+				       get_max_value_id() + 1);
+		newref = vn_reference_insert_pieces (newvuses,
+						     newoperands, 
+						     result, new_val_id);
+		PRE_EXPR_REFERENCE (expr) = newref;
+		get_or_alloc_expression_id (expr);
+		add_to_value (new_val_id, expr);
+	      }    
 	  }
-	phi_trans_add (oldexpr, expr, pred, NULL);
+	phi_trans_add (oldexpr, expr, pred);
+	return expr;
       }
-      return expr;
-
-    case tcc_exceptional:
+      break;
+    case NAME:
       {
 	tree phi = NULL;
 	edge e;
 	tree def_stmt;
-	gcc_assert (TREE_CODE (expr) == SSA_NAME);
+	tree name = PRE_EXPR_NAME (expr);
 
-	def_stmt = SSA_NAME_DEF_STMT (expr);
+	def_stmt = SSA_NAME_DEF_STMT (name);
 	if (TREE_CODE (def_stmt) == PHI_NODE
 	    && bb_for_stmt (def_stmt) == phiblock)
 	  phi = def_stmt;
@@ -1320,18 +1534,18 @@ phi_translate_1 (tree expr, bitmap_set_t
 	e = find_edge (pred, bb_for_stmt (phi));
 	if (e)
 	  {
-	    tree val;
 	    tree def = PHI_ARG_DEF (phi, e->dest_idx);
+	    pre_expr newexpr;
 
+	    /* Handle constant. */
 	    if (is_gimple_min_invariant (def))
-	      return def;
+	      return get_or_alloc_expr_for_constant (def);
 
 	    if (TREE_CODE (def) == SSA_NAME && ssa_undefined_value_p (def))
 	      return NULL;
 
-	    val = get_value_handle (def);
-	    gcc_assert (val);
-	    return def;
+	    newexpr = get_or_alloc_expr_for_name (def);
+	    return newexpr;
 	  }
       }
       return expr;
@@ -1346,8 +1560,8 @@ phi_translate_1 (tree expr, bitmap_set_t
    Return NULL if we can't find a leader for each part of the
    translated expression.  */
 
-static tree
-phi_translate (tree expr, bitmap_set_t set1, bitmap_set_t set2,
+static pre_expr
+phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
 	       basic_block pred, basic_block phiblock)
 {
   bitmap_clear (seen_during_translate);
@@ -1355,7 +1569,7 @@ phi_translate (tree expr, bitmap_set_t s
 			  seen_during_translate);
 }
 
-/* For each expression in SET, translate the value handles through phi nodes
+/* For each expression in SET, translate the values through phi nodes
    in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
    expressions in DEST.  */
 
@@ -1363,8 +1577,8 @@ static void
 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
 		   basic_block phiblock)
 {
-  VEC (tree, heap) *exprs;
-  tree expr;
+  VEC (pre_expr, heap) *exprs;
+  pre_expr expr;
   int i;
 
   if (!phi_nodes (phiblock))
@@ -1374,23 +1588,20 @@ phi_translate_set (bitmap_set_t dest, bi
     }
 
   exprs = sorted_array_from_bitmap_set (set);
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
     {
-      tree translated;
+      pre_expr translated;
       translated = phi_translate (expr, set, NULL, pred, phiblock);
 
       /* Don't add constants or empty translations to the cache, since
 	 we won't look them up that way, or use the result, anyway.  */
-      if (translated && !is_gimple_min_invariant (translated))
-	{
-	  phi_trans_add (expr, translated, pred,
-			 get_expression_vuses (translated));
-	}
+      if (translated)
+	phi_trans_add (expr, translated, pred);
 
       if (translated != NULL)
 	bitmap_value_insert_into_set (dest, translated);
     }
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
 }
 
 /* Find the leader for a value (i.e., the name representing that
@@ -1398,15 +1609,22 @@ phi_translate_set (bitmap_set_t dest, bi
    makes sure the defining statement for the leader dominates it.
    Return NULL if no leader is found.  */
 
-static tree
-bitmap_find_leader (bitmap_set_t set, tree val, tree stmt)
+static pre_expr
+bitmap_find_leader (bitmap_set_t set, unsigned int val, tree stmt)
 {
-  if (val == NULL)
-    return NULL;
-
-  if (constant_expr_p (val))
-    return val;
+  if (value_id_constant_p (val))
+    {
+      unsigned int i;
+      bitmap_iterator bi;
+      bitmap_set_t exprset = VEC_index (bitmap_set_t, value_expressions, val);
 
+      FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
+	{
+	  pre_expr expr = expression_for_id (i);
+	  if (expr->kind == CONSTANT)
+	    return expr;
+	}
+    }
   if (bitmap_set_contains_value (set, val))
     {
       /* Rather than walk the entire bitmap of expressions, and see
@@ -1422,15 +1640,17 @@ bitmap_find_leader (bitmap_set_t set, tr
 	 choose which set to walk based on which set is smaller.  */
       unsigned int i;
       bitmap_iterator bi;
-      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
+      bitmap_set_t exprset = VEC_index (bitmap_set_t, value_expressions, val);
 
       EXECUTE_IF_AND_IN_BITMAP (exprset->expressions,
 				set->expressions, 0, i, bi)
 	{
-	  tree val = expression_for_id (i);
+	  pre_expr val = expression_for_id (i);
+	  /* At the point where stmt is not null, there should always
+	     be an SSA_NAME first in the list of expressions.  */
 	  if (stmt)
 	    {
-	      tree def_stmt = SSA_NAME_DEF_STMT (val);
+	      tree def_stmt = SSA_NAME_DEF_STMT (PRE_EXPR_NAME (val));
 	      if (TREE_CODE (def_stmt) != PHI_NODE
 		  && bb_for_stmt (def_stmt) == bb_for_stmt (stmt)
 		  && stmt_ann (def_stmt)->uid >= stmt_ann (stmt)->uid)
@@ -1450,11 +1670,11 @@ bitmap_find_leader (bitmap_set_t set, tr
    ANTIC_IN set already.  */
 
 static bool
-value_dies_in_block_x (tree expr, basic_block block)
+value_dies_in_block_x (pre_expr expr, basic_block block)
 {
   int i;
   tree vuse;
-  VEC (tree, gc) *vuses = get_expression_vuses (expr);
+  VEC (tree, gc) *vuses = PRE_EXPR_REFERENCE (expr)->vuses;
 
   /* Conservatively, a value dies if it's vuses are defined in this
      block, unless they come from phi nodes (which are merge operations,
@@ -1472,120 +1692,100 @@ value_dies_in_block_x (tree expr, basic_
   return false;
 }
 
-/* Determine if the expression EXPR is valid in SET1 U SET2.
-   ONLY SET2 CAN BE NULL.
-   This means that we have a leader for each part of the expression
-   (if it consists of values), or the expression is an SSA_NAME.
-   For loads/calls, we also see if the vuses are killed in this block.
-
-   NB: We never should run into a case where we have SSA_NAME +
-   SSA_NAME or SSA_NAME + value.  The sets valid_in_sets is called on,
-   the ANTIC sets, will only ever have SSA_NAME's or value expressions
-   (IE VALUE1 + VALUE2, *VALUE1, VALUE1 < VALUE2)  */
 
 #define union_contains_value(SET1, SET2, VAL)			\
   (bitmap_set_contains_value ((SET1), (VAL))			\
    || ((SET2) && bitmap_set_contains_value ((SET2), (VAL))))
 
+/* Determine if vn_reference_op_t VRO is legal in SET1 U SET2.
+ */
 static bool
-valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree expr,
-	       basic_block block)
+vro_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2,
+		   vn_reference_op_t vro) 
 {
- switch (TREE_CODE_CLASS (TREE_CODE (expr)))
+  if (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME)
     {
-    case tcc_binary:
-    case tcc_comparison:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	tree op2 = TREE_OPERAND (expr, 1);
-
-	return union_contains_value (set1, set2, op1)
-	  && union_contains_value (set1, set2, op2);
-      }
-
-    case tcc_unary:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	return union_contains_value (set1, set2, op1);
-      }
+      struct pre_expr_d temp;
+      temp.kind = NAME;
+      temp.id = 0;
+      PRE_EXPR_NAME (&temp) = vro->op0;
+      temp.id = lookup_expression_id (&temp);
+      if (temp.id == 0)
+	return false;
+      if (!union_contains_value (set1, set2,
+				 get_expr_value_id (&temp)))
+	return false;
+    }
+  if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
+    {
+      struct pre_expr_d temp;
+      temp.kind = NAME;
+      temp.id = 0;
+      PRE_EXPR_NAME (&temp) = vro->op1;
+      temp.id = lookup_expression_id (&temp);
+      if (temp.id == 0)
+	return false;
+      if (!union_contains_value (set1, set2,
+				 get_expr_value_id (&temp)))
+	return false;
+    }
+  return true;
+}
 
-    case tcc_expression:
-      return false;
+/* Determine if the expression EXPR is valid in SET1 U SET2.
+   ONLY SET2 CAN BE NULL.
+   This means that we have a leader for each part of the expression
+   (if it consists of values), or the expression is an SSA_NAME.
+   For loads/calls, we also see if the vuses are killed in this block.
+*/
 
-    case tcc_vl_exp:
+static bool
+valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr,
+	       basic_block block)
+{
+  switch (expr->kind)
+    {
+    case NAME:
+      return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
+    case NARY:
       {
-	if (TREE_CODE (expr) == CALL_EXPR)
+	unsigned int i;
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	for (i = 0; i < nary->length; i++)
 	  {
-	    tree fn = CALL_EXPR_FN (expr);
-	    tree sc = CALL_EXPR_STATIC_CHAIN (expr);
-	    tree arg;
-	    call_expr_arg_iterator iter;
-
-	    /* Check the non-argument operands first.  */
-	    if (!union_contains_value (set1, set2, fn)
-		|| (sc && !union_contains_value (set1, set2, sc)))
-	      return false;
-
-	    /* Now check the operands.  */
-	    FOR_EACH_CALL_EXPR_ARG (arg, iter, expr)
+	    if (TREE_CODE (nary->op[i]) == SSA_NAME)
 	      {
-		if (!union_contains_value (set1, set2, arg))
+		struct pre_expr_d temp;
+		temp.kind = NAME;
+		temp.id = 0;
+		PRE_EXPR_NAME (&temp) = nary->op[i];
+		temp.id = lookup_expression_id (&temp);
+		if (temp.id == 0)
+		  return false;
+		if (!union_contains_value (set1, set2,
+					   get_expr_value_id (&temp)))
 		  return false;
 	      }
-	    return !value_dies_in_block_x (expr, block);
 	  }
-	return false;
+	return true;
       }
-
-    case tcc_reference:
+      break;
+    case REFERENCE: 
       {
-	if (TREE_CODE (expr) == INDIRECT_REF
-	    || TREE_CODE (expr) == COMPONENT_REF
-	    || TREE_CODE (expr) == ARRAY_REF)
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	vn_reference_op_t vro;
+	unsigned int i;
+	
+	for (i = 0; VEC_iterate (vn_reference_op_s, ref->operands, i, vro); i++)
 	  {
-	    tree op0 = TREE_OPERAND (expr, 0);
-	    gcc_assert (is_gimple_min_invariant (op0)
-			|| TREE_CODE (op0) == VALUE_HANDLE);
-	    if (!union_contains_value (set1, set2, op0))
+	    if (!vro_valid_in_sets (set1, set2, vro))
 	      return false;
-	    if (TREE_CODE (expr) == ARRAY_REF)
-	      {
-		tree op1 = TREE_OPERAND (expr, 1);
-		tree op2 = TREE_OPERAND (expr, 2);
-		tree op3 = TREE_OPERAND (expr, 3);
-		gcc_assert (is_gimple_min_invariant (op1)
-			    || TREE_CODE (op1) == VALUE_HANDLE);
-		if (!union_contains_value (set1, set2, op1))
-		  return false;
-		gcc_assert (!op2 || is_gimple_min_invariant (op2)
-			    || TREE_CODE (op2) == VALUE_HANDLE);
-		if (op2
-		    && !union_contains_value (set1, set2, op2))
-		  return false;
-		gcc_assert (!op3 || is_gimple_min_invariant (op3)
-			    || TREE_CODE (op3) == VALUE_HANDLE);
-		if (op3
-		    && !union_contains_value (set1, set2, op3))
-		  return false;
-	    }
-	    return !value_dies_in_block_x (expr, block);
 	  }
+	return !value_dies_in_block_x (expr, block);
       }
-      return false;
-
-    case tcc_exceptional:
-      {
-	gcc_assert (TREE_CODE (expr) == SSA_NAME);
-	return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
-      }
-
-    case tcc_declaration:
-      return !value_dies_in_block_x (expr, block);
-
     default:
-      /* No other cases should be encountered.  */
       gcc_unreachable ();
-   }
+    }
 }
 
 /* Clean the set of expressions that are no longer valid in SET1 or
@@ -1597,16 +1797,16 @@ valid_in_sets (bitmap_set_t set1, bitmap
 static void
 dependent_clean (bitmap_set_t set1, bitmap_set_t set2, basic_block block)
 {
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set1);
-  tree expr;
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (set1);
+  pre_expr expr;
   int i;
 
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
     {
       if (!valid_in_sets (set1, set2, expr, block))
 	bitmap_remove_from_set (set1, expr);
     }
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
 }
 
 /* Clean the set of expressions that are no longer valid in SET.  This
@@ -1616,16 +1816,16 @@ dependent_clean (bitmap_set_t set1, bitm
 static void
 clean (bitmap_set_t set, basic_block block)
 {
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set);
-  tree expr;
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (set);
+  pre_expr expr;
   int i;
 
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
     {
       if (!valid_in_sets (set, NULL, expr, block))
 	bitmap_remove_from_set (set, expr);
     }
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
 }
 
 static sbitmap has_abnormal_preds;
@@ -1967,73 +2167,6 @@ compute_partial_antic_aux (basic_block b
   return changed;
 }
 
-/* Initialize data structures used for ANTIC and AVAIL.  */
-
-static void
-init_antic (void)
-{
-  basic_block bb;
-
-  next_expression_id = 0;
-  expressions = NULL;
-  expression_vuses = NULL;
-
-  postorder = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
-  post_order_compute (postorder, false, false);
-
-  bitmap_obstack_initialize (&grand_bitmap_obstack);
-  obstack_init (&temp_call_expr_obstack);
-  seen_during_translate = BITMAP_ALLOC (&grand_bitmap_obstack);
-
-  bitmap_set_pool = create_alloc_pool ("Bitmap sets",
-					sizeof (struct bitmap_set), 30);
-  binary_node_pool = create_alloc_pool ("Binary tree nodes",
-					tree_code_size (PLUS_EXPR), 30);
-  unary_node_pool = create_alloc_pool ("Unary tree nodes",
-				       tree_code_size (NEGATE_EXPR), 30);
-  reference_node_pool = create_alloc_pool ("Reference tree nodes",
-					   tree_code_size (ARRAY_REF), 30);
-  comparison_node_pool = create_alloc_pool ("Comparison tree nodes",
-					    tree_code_size (EQ_EXPR), 30);
-
-  phi_translate_table = htab_create (5110, expr_pred_trans_hash,
-				     expr_pred_trans_eq, free);
-  maximal_set = in_fre ? NULL : bitmap_set_new ();
-
-  FOR_ALL_BB (bb)
-    {
-      bb->aux = xcalloc (1, sizeof (struct bb_bitmap_sets));
-      EXP_GEN (bb) = bitmap_set_new ();
-      PHI_GEN (bb) = bitmap_set_new ();
-      TMP_GEN (bb) = bitmap_set_new ();
-      AVAIL_OUT (bb) = bitmap_set_new ();
-    }
-}
-
-/* Deinitialize data structures used for ANTIC and AVAIL.  */
-
-static void
-fini_antic (void)
-{
-  basic_block bb;
-
-  if (maximal_set)
-    bitmap_set_free (maximal_set);
-  free (postorder);
-  bitmap_obstack_release (&grand_bitmap_obstack);
-  free_alloc_pool (bitmap_set_pool);
-  free_alloc_pool (binary_node_pool);
-  free_alloc_pool (reference_node_pool);
-  free_alloc_pool (unary_node_pool);
-  free_alloc_pool (comparison_node_pool);
-
-  FOR_ALL_BB (bb)
-    {
-      free (bb->aux);
-      bb->aux = NULL;
-    }
-}
-
 /* Compute ANTIC and partial ANTIC sets.  */
 
 static void
@@ -2160,22 +2293,6 @@ is_exception_related (tree op)
   return TREE_CODE (op) == FILTER_EXPR || TREE_CODE (op) == EXC_PTR_EXPR;
 }
 
-/* Return true if OP is a tree which we can perform value numbering
-   on.  */
-
-static bool
-can_value_number_operation (tree op)
-{
-  return (UNARY_CLASS_P (op)
-	  && !is_exception_related (TREE_OPERAND (op, 0)))
-    || BINARY_CLASS_P (op)
-    || COMPARISON_CLASS_P (op)
-    || REFERENCE_CLASS_P (op)
-    || (TREE_CODE (op) == CALL_EXPR
-	&& can_value_number_call (op));
-}
-
-
 /* Return true if OP is a tree which we can perform PRE on
    on.  This may not match the operations we can value number, but in
    a perfect world would.  */
@@ -2218,90 +2335,203 @@ static VEC(tree, heap) *need_creation;
    are doing.
 */
 static tree
-create_component_ref_by_pieces (basic_block block, tree expr, tree stmts,
-				tree domstmt)
-{
-  tree genop = expr;
-  tree folded;
-
-  if (TREE_CODE (genop) == VALUE_HANDLE)
+create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
+				unsigned int operand,
+				tree stmts,
+				tree domstmt,
+				bool in_call)
+{
+  vn_reference_op_t currop = VEC_index (vn_reference_op_s, ref->operands,
+					operand);
+  tree genop;
+  switch (currop->opcode) 
     {
-      tree found = bitmap_find_leader (AVAIL_OUT (block), expr, domstmt);
-      if (found)
-	return found;
-    }
+    case CALL_EXPR:
+      {
+	tree folded;
+	unsigned int i;
+	vn_reference_op_t declop = VEC_index (vn_reference_op_s,
+					      ref->operands, 1);
+	unsigned int nargs = VEC_length (vn_reference_op_s, ref->operands) - 2;
+	tree *args = XNEWVEC (tree, nargs);
 
-  if (TREE_CODE (genop) == VALUE_HANDLE)
-    {
-      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
-      unsigned int firstbit = bitmap_first_set_bit (exprset->expressions);
-      genop = expression_for_id (firstbit);
-    }
+	for (i = 0; i < nargs; i++)
+	  {
+	    args[i] = create_component_ref_by_pieces (block, ref,
+						      operand + 2 + i, stmts,
+						      domstmt, true);
+	  }
+	folded = build_call_array (currop->type, declop->op0, nargs, args);
+	free (args);
+	return folded;
+      }
+      break;
+    case REALPART_EXPR:
+    case IMAGPART_EXPR:
+    case VIEW_CONVERT_EXPR:
+      {
+	tree folded;
+	tree genop0 = create_component_ref_by_pieces (block, ref,
+						      operand + 1,
+						      stmts, domstmt,
+						      in_call);
+	if (!genop0)
+	  return NULL_TREE;
+	folded = fold_build1 (currop->opcode, currop->type,
+			      genop0);
+	return folded;
+      }
+      break;
+    case ALIGN_INDIRECT_REF:
+    case MISALIGNED_INDIRECT_REF:
+    case INDIRECT_REF:
+      {
+	/* Inside a CALL_EXPR op0 is the actual indirect_ref.  */
+	if (in_call)
+	  {
+	    tree folded;
+	    tree op0 = TREE_OPERAND (currop->op0, 0);
+	    pre_expr op0expr = get_or_alloc_expr_for (op0);
+	    tree genop0 = find_or_generate_expression (block, op0expr, stmts,
+						       domstmt);
+	    if (!genop0)
+	      return NULL_TREE;
+	    folded = fold_build1 (currop->opcode, currop->type,
+				  genop0);
+	    return folded;
+	  }
+	else
+	  {
 
-  switch TREE_CODE (genop)
-    {
-    case ARRAY_REF:
+	    tree folded;
+	    tree genop1 = create_component_ref_by_pieces (block, ref,
+							  operand + 1,
+							  stmts, domstmt,
+							  in_call);
+	    if (!genop1)
+	      return NULL_TREE;
+	    genop1 = fold_convert (build_pointer_type (currop->type),
+		genop1);
+
+	    folded = fold_build1 (currop->opcode, currop->type,
+		genop1);
+	    return folded;
+	  }
+      }
+      break;
+    case BIT_FIELD_REF:
       {
-	tree op0;
-	tree op1, op2, op3;
-	op0 = create_component_ref_by_pieces (block,
-					      TREE_OPERAND (genop, 0),
-					      stmts, domstmt);
-	op1 = TREE_OPERAND (genop, 1);
-	if (TREE_CODE (op1) == VALUE_HANDLE)
-	  op1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	op2 = TREE_OPERAND (genop, 2);
-	if (op2 && TREE_CODE (op2) == VALUE_HANDLE)
-	  op2 = find_or_generate_expression (block, op2, stmts, domstmt);
-	op3 = TREE_OPERAND (genop, 3);
-	if (op3 && TREE_CODE (op3) == VALUE_HANDLE)
-	  op3 = find_or_generate_expression (block, op3, stmts, domstmt);
-	if (!op0 || !op1)
+	tree folded;
+	tree genop0 = create_component_ref_by_pieces (block, ref, operand + 1,
+						      stmts, domstmt,
+						      in_call);
+	pre_expr op1expr = get_or_alloc_expr_for (currop->op0);
+	pre_expr op2expr = get_or_alloc_expr_for (currop->op1);
+	tree genop1;
+	tree genop2;
+	
+	if (!genop0)
 	  return NULL_TREE;
-	folded = build4 (ARRAY_REF, TREE_TYPE (genop), op0, op1,
-			      op2, op3);
+	genop1 = find_or_generate_expression (block, op1expr, stmts, domstmt);
+	if (!genop1)
+	  return NULL_TREE;
+	genop2 = find_or_generate_expression (block, op2expr, stmts, domstmt);
+	if (!genop2)
+	  return NULL_TREE;
+	folded = fold_build3 (BIT_FIELD_REF, currop->type, genop0, genop1,
+			      genop2);
 	return folded;
       }
+      
+      /* For array ref vn_reference_op's, operand 1 of the array ref
+	 is op0 of the reference op and operand 3 of the array ref is
+	 op1.  */
+    case ARRAY_RANGE_REF:
+    case ARRAY_REF:
+      {
+	vn_reference_op_t op0expr;
+	tree genop0;
+	tree genop1 = currop->op0;
+	pre_expr op1expr;
+	tree genop2 = currop->op1;
+	pre_expr op2expr;
+	tree genop3;
+	op0expr = VEC_index (vn_reference_op_s, ref->operands, operand + 1);
+	genop0 = create_component_ref_by_pieces (block, ref, operand + 1,
+						 stmts, domstmt,
+						 in_call);
+	if (!genop0)
+	  return NULL_TREE;
+	op1expr = get_or_alloc_expr_for (genop1);
+	genop1 = find_or_generate_expression (block, op1expr, stmts, domstmt);
+	if (!genop1)
+	  return NULL_TREE;
+	if (genop2) 
+	  {
+	    op2expr = get_or_alloc_expr_for (genop2);
+	    genop2 = find_or_generate_expression (block, op2expr, stmts,
+						  domstmt);
+	    if (!genop2)
+	      return NULL_TREE;
+	  }
+
+	genop3 = currop->op2;
+	return build4 (currop->opcode, currop->type, genop0, genop1,
+		       genop2, genop3);
+      }		      
     case COMPONENT_REF:
       {
 	tree op0;
 	tree op1;
-	op0 = create_component_ref_by_pieces (block,
-					      TREE_OPERAND (genop, 0),
-					      stmts, domstmt);
+	tree genop2 = currop->op1;
+	pre_expr op2expr;
+	op0 = create_component_ref_by_pieces (block, ref, operand + 1,
+					      stmts, domstmt, in_call);
 	if (!op0)
 	  return NULL_TREE;
 	/* op1 should be a FIELD_DECL, which are represented by
 	   themselves.  */
-	op1 = TREE_OPERAND (genop, 1);
-	folded = fold_build3 (COMPONENT_REF, TREE_TYPE (genop), op0, op1,
-			      NULL_TREE);
-	return folded;
+	op1 = currop->op0;
+	if (genop2) 
+	  {
+	    op2expr = get_or_alloc_expr_for (genop2);
+	    genop2 = find_or_generate_expression (block, op2expr, stmts,
+						  domstmt);
+	    if (!genop2)
+	      return NULL_TREE;
+	  }
+	
+	return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1,
+			    genop2);
       }
       break;
-    case INDIRECT_REF:
+    case SSA_NAME:
       {
-	tree op1 = TREE_OPERAND (genop, 0);
-	tree genop1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	if (!genop1)
-	  return NULL_TREE;
-
-	folded = fold_build1 (TREE_CODE (genop), TREE_TYPE (genop),
-			      genop1);
-	return folded;
+	pre_expr op0expr = get_or_alloc_expr_for (currop->op0);
+	genop = find_or_generate_expression (block, op0expr, stmts, domstmt);
+	return genop;
       }
-      break;
+    case STRING_CST:
+    case INTEGER_CST:
+    case COMPLEX_CST:
+    case VECTOR_CST:
+    case REAL_CST:
+    case CONSTRUCTOR:
     case VAR_DECL:
     case PARM_DECL:
+    case CONST_DECL:
     case RESULT_DECL:
-    case SSA_NAME:
-    case STRING_CST:
-      return genop;
+    case FUNCTION_DECL:
+      /* For ADDR_EXPR in a CALL_EXPR, op0 is actually the entire
+	 ADDR_EXPR, not just it's operand.  */
+    case ADDR_EXPR:
+      if (currop->opcode == ADDR_EXPR)
+	gcc_assert (currop->op0 != NULL);
+      return currop->op0;
+      
     default:
       gcc_unreachable ();
-    }
-
-  return NULL_TREE;
+    } 
 }
 
 /* Find a leader for an expression, or generate one using
@@ -2318,31 +2548,40 @@ create_component_ref_by_pieces (basic_bl
    on failure.  */
 
 static tree
-find_or_generate_expression (basic_block block, tree expr, tree stmts,
+find_or_generate_expression (basic_block block, pre_expr expr, tree stmts,
 			     tree domstmt)
 {
-  tree genop = bitmap_find_leader (AVAIL_OUT (block), expr, domstmt);
-
+  pre_expr leader = bitmap_find_leader (AVAIL_OUT (block),
+					get_expr_value_id (expr), domstmt);
+  tree genop = NULL;
+  if (leader)
+    {
+      if (leader->kind == NAME)
+	genop = PRE_EXPR_NAME (leader);
+      else if (leader->kind == CONSTANT)
+	genop = PRE_EXPR_CONSTANT (leader);
+    }
+  
   /* If it's still NULL, it must be a complex expression, so generate
      it recursively.  */
   if (genop == NULL)
     {
-      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
+      bitmap_set_t exprset;
+      unsigned int lookfor = get_expr_value_id (expr);
       bool handled = false;
       bitmap_iterator bi;
       unsigned int i;
 
-      /* We will hit cases where we have SSA_NAME's in exprset before
-	 other operations, because we may have come up with the SCCVN
-	 value before getting to the RHS of the expression.  */
+      exprset = VEC_index (bitmap_set_t, value_expressions, lookfor);
       FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
 	{
-	  genop = expression_for_id (i);
-	  if (can_PRE_operation (genop))
+	  pre_expr temp = expression_for_id (i);
+	  if (temp->kind != NAME)
 	    {
 	      handled = true;
-	      genop = create_expression_by_pieces (block, genop, stmts,
-						   domstmt);
+	      genop = create_expression_by_pieces (block, temp, stmts,
+						   domstmt,
+						   get_expr_type (expr));
 	      break;
 	    }
 	}
@@ -2355,6 +2594,7 @@ find_or_generate_expression (basic_block
 }
 
 #define NECESSARY(stmt)		stmt->base.asm_written_flag
+
 /* Create an expression in pieces, so that we can handle very complex
    expressions that may be ANTIC, but not necessary GIMPLE.
    BLOCK is the basic block the expression will be inserted into,
@@ -2374,109 +2614,77 @@ find_or_generate_expression (basic_block
    can return NULL_TREE to signal failure.  */
 
 static tree
-create_expression_by_pieces (basic_block block, tree expr, tree stmts,
-			     tree domstmt)
+create_expression_by_pieces (basic_block block, pre_expr expr, tree stmts,
+			     tree domstmt,
+			     tree type)
 {
   tree temp, name;
   tree folded, forced_stmts, newexpr;
-  tree v;
+  unsigned int value_id;
   tree_stmt_iterator tsi;
-
-  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
-    {
-    case tcc_vl_exp:
+  tree exprtype = type ? type : get_expr_type (expr);
+  pre_expr nameexpr;
+  
+  switch (expr->kind)
+    {
+      /* We may hit the NAME/CONSTANT case if we have to convert types
+	 that value numbering saw through.  */
+    case NAME:
+      folded = PRE_EXPR_NAME (expr);
+      break;
+    case CONSTANT:
+      folded = PRE_EXPR_CONSTANT (expr);
+      break;
+    case REFERENCE:
       {
-	tree fn, sc;
-	tree genfn;
-	int i, nargs;
-	tree *buffer;
-
-	gcc_assert (TREE_CODE (expr) == CALL_EXPR);
-
-	fn = CALL_EXPR_FN (expr);
-	sc = CALL_EXPR_STATIC_CHAIN (expr);
-
-	genfn = find_or_generate_expression (block, fn, stmts, domstmt);
-	if (!genfn)
-	  return NULL_TREE;
-
-	nargs = call_expr_nargs (expr);
-	buffer = (tree*) alloca (nargs * sizeof (tree));
-
-	for (i = 0; i < nargs; i++)
-	  {
-	    tree arg = CALL_EXPR_ARG (expr, i);
-	    buffer[i] = find_or_generate_expression (block, arg, stmts,
-						     domstmt);
-	    if (!buffer[i])
-	      return NULL_TREE;
-	  }
-
-	folded = build_call_array (TREE_TYPE (expr), genfn, nargs, buffer);
-	if (sc)
-	  {
-	    CALL_EXPR_STATIC_CHAIN (folded) =
-	      find_or_generate_expression (block, sc, stmts, domstmt);
-	    if (!CALL_EXPR_STATIC_CHAIN (folded))
-	      return NULL_TREE;
-	  }
-	folded = fold (folded);
-	break;
+	vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
+	folded = create_component_ref_by_pieces (block, ref, 0, stmts,
+						 domstmt, false);
       }
       break;
-    case tcc_reference:
+    case NARY:
       {
-	if (TREE_CODE (expr) == COMPONENT_REF
-	    || TREE_CODE (expr) == ARRAY_REF)
-	  {
-	    folded = create_component_ref_by_pieces (block, expr, stmts,
-						     domstmt);
-	    if (!folded)
-	      return NULL_TREE;
-	  }
-	else
+	vn_nary_op_t nary = PRE_EXPR_NARY (expr);
+	switch (nary->length) 
 	  {
-	    tree op1 = TREE_OPERAND (expr, 0);
-	    tree genop1 = find_or_generate_expression (block, op1, stmts,
-						       domstmt);
-	    if (!genop1)
-	      return NULL_TREE;
-
-	    folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
-				  genop1);
+	  case 2:
+	    {
+	      pre_expr op1 = get_or_alloc_expr_for (nary->op[0]);
+	      pre_expr op2 = get_or_alloc_expr_for (nary->op[1]);
+	      tree genop1 = find_or_generate_expression (block, op1,
+							 stmts, domstmt);
+	      tree genop2 = find_or_generate_expression (block, op2,
+							 stmts, domstmt);
+	      if (!genop1 || !genop2)
+		return NULL_TREE;
+	      /* Ensure op2 is a sizetype for POINTER_PLUS_EXPR.  It
+		 may be a constant with the wrong type.  */
+	      if (nary->opcode == POINTER_PLUS_EXPR)
+		genop2 = fold_convert (sizetype, genop2);
+	      folded = fold_build2 (nary->opcode, nary->type,
+				    genop1, genop2);
+	    }
+	    break;
+	  case 1:
+	    {
+	      pre_expr op1 = get_or_alloc_expr_for (nary->op[0]);
+	      tree genop1 = find_or_generate_expression (block, op1,
+							 stmts, domstmt);
+	      if (!genop1)
+		return NULL_TREE;
+	      folded = fold_build1 (nary->opcode, nary->type,
+				    genop1);
+	    }
+	    break;
+	  default:
+	    return NULL_TREE;
 	  }
-	break;
-      }
-
-    case tcc_binary:
-    case tcc_comparison:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	tree op2 = TREE_OPERAND (expr, 1);
-	tree genop1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	tree genop2 = find_or_generate_expression (block, op2, stmts, domstmt);
-	if (!genop1 || !genop2)
-	  return NULL_TREE;
-	folded = fold_build2 (TREE_CODE (expr), TREE_TYPE (expr),
-			      genop1, genop2);
-	break;
-      }
-
-    case tcc_unary:
-      {
-	tree op1 = TREE_OPERAND (expr, 0);
-	tree genop1 = find_or_generate_expression (block, op1, stmts, domstmt);
-	if (!genop1)
-	  return NULL_TREE;
-	folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
-			      genop1);
-	break;
       }
-
+      break;
     default:
-      gcc_unreachable ();
+      return NULL_TREE;
     }
-
+  folded = fold_convert (exprtype, folded);
   /* Force the generated expression to be a sequence of GIMPLE
      statements.
      We have to call unshare_expr because force_gimple_operand may
@@ -2493,14 +2701,18 @@ create_expression_by_pieces (basic_block
 	{
 	  tree stmt = tsi_stmt (tsi);
 	  tree forcedname = GIMPLE_STMT_OPERAND (stmt, 0);
-	  tree forcedexpr = GIMPLE_STMT_OPERAND (stmt, 1);
-	  tree val = vn_lookup_or_add (forcedexpr);
-
+	  pre_expr nameexpr;
+	  
 	  VEC_safe_push (tree, heap, inserted_exprs, stmt);
-	  VN_INFO_GET (forcedname)->valnum = forcedname;
-	  vn_add (forcedname, val);
-	  bitmap_value_replace_in_set (NEW_SETS (block), forcedname);
-	  bitmap_value_replace_in_set (AVAIL_OUT (block), forcedname);
+	  if (TREE_CODE (forcedname) == SSA_NAME)
+	    {
+	      VN_INFO_GET (forcedname)->valnum = forcedname;
+	      VN_INFO (forcedname)->value_id = get_next_value_id ();
+	      nameexpr = get_or_alloc_expr_for_name (forcedname);
+	      add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
+	      bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
+	      bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
+	    }
 	  mark_symbols_for_renaming (stmt);
 	}
       tsi = tsi_last (stmts);
@@ -2509,17 +2721,17 @@ create_expression_by_pieces (basic_block
 
   /* Build and insert the assignment of the end result to the temporary
      that we will return.  */
-  if (!pretemp || TREE_TYPE (expr) != TREE_TYPE (pretemp))
+  if (!pretemp || exprtype != TREE_TYPE (pretemp))
     {
-      pretemp = create_tmp_var (TREE_TYPE (expr), "pretmp");
+      pretemp = create_tmp_var (exprtype, "pretmp");
       get_var_ann (pretemp);
     }
 
   temp = pretemp;
   add_referenced_var (temp);
 
-  if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE
-      || TREE_CODE (TREE_TYPE (expr)) == VECTOR_TYPE)
+  if (TREE_CODE (exprtype) == COMPLEX_TYPE
+      || TREE_CODE (exprtype) == VECTOR_TYPE)
     DECL_GIMPLE_REG_P (temp) = 1;
 
   newexpr = build_gimple_modify_stmt (temp, newexpr);
@@ -2534,18 +2746,19 @@ create_expression_by_pieces (basic_block
   /* All the symbols in NEWEXPR should be put into SSA form.  */
   mark_symbols_for_renaming (newexpr);
 
-  /* Add a value handle to the temporary.
+  /* Add a value number to the temporary.
      The value may already exist in either NEW_SETS, or AVAIL_OUT, because
      we are creating the expression by pieces, and this particular piece of
      the expression may have been represented.  There is no harm in replacing
      here.  */
-  v = get_value_handle (expr);
-  vn_add (name, v);
   VN_INFO_GET (name)->valnum = name;
-  get_or_alloc_expression_id (name);
+  value_id = get_expr_value_id (expr);
+  VN_INFO (name)->value_id = value_id;
+  nameexpr = get_or_alloc_expr_for_name (name);
+  add_to_value (value_id, nameexpr);
   if (!in_fre)
-    bitmap_value_replace_in_set (NEW_SETS (block), name);
-  bitmap_value_replace_in_set (AVAIL_OUT (block), name);
+    bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
+  bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
 
   pre_stats.insertions++;
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -2558,39 +2771,38 @@ create_expression_by_pieces (basic_block
   return name;
 }
 
+
 /* Insert the to-be-made-available values of expression EXPRNUM for each
    predecessor, stored in AVAIL, into the predecessors of BLOCK, and
-   merge the result with a phi node, given the same value handle as
+   merge the result with a phi node, given the same value number as
    NODE.  Return true if we have inserted new stuff.  */
 
 static bool
 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
-			    tree *avail)
+			    pre_expr *avail)
 {
-  tree expr = expression_for_id (exprnum);
-  tree val = get_value_handle (expr);
+  pre_expr expr = expression_for_id (exprnum);
+  pre_expr newphi;
+  unsigned int val = get_expr_value_id (expr);
   edge pred;
   bool insertions = false;
   bool nophi = false;
   basic_block bprime;
-  tree eprime;
+  pre_expr eprime;
   edge_iterator ei;
-  tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
+  tree type = get_expr_type (expr);
   tree temp;
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
       fprintf (dump_file, "Found partial redundancy for expression ");
-      print_generic_expr (dump_file, expr, 0);
-      fprintf (dump_file, " (");
-      print_generic_expr (dump_file, val, 0);
-      fprintf (dump_file, ")");
-      fprintf (dump_file, "\n");
+      print_pre_expr (dump_file, expr);
+      fprintf (dump_file, " (%04d)\n", val);
     }
 
   /* Make sure we aren't creating an induction variable.  */
   if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2
-      && TREE_CODE_CLASS (TREE_CODE (expr)) != tcc_reference )
+      && expr->kind != REFERENCE)
     {
       bool firstinsideloop = false;
       bool secondinsideloop = false;
@@ -2616,16 +2828,111 @@ insert_into_preds_of_block (basic_block 
       bprime = pred->src;
       eprime = avail[bprime->index];
 
-      if (can_PRE_operation (eprime))
+      if (eprime->kind != NAME && eprime->kind != CONSTANT)
 	{
 	  builtexpr = create_expression_by_pieces (bprime,
 						   eprime,
-						   stmts, NULL_TREE);
+						   stmts, NULL_TREE,
+						   type);
 	  gcc_assert (!(pred->flags & EDGE_ABNORMAL));
 	  bsi_insert_on_edge (pred, stmts);
-	  avail[bprime->index] = builtexpr;
+	  avail[bprime->index] = get_or_alloc_expr_for_name (builtexpr);
 	  insertions = true;
 	}
+      else if (eprime->kind == CONSTANT) 
+	{
+	  /* Constants may not have the right type, fold_convert
+	     should give us back a constant with the right type.
+	  */
+	  tree constant = PRE_EXPR_CONSTANT (eprime);
+	  if (TREE_TYPE (constant) != type)
+	    {
+	      tree builtexpr = fold_convert (type, constant);
+	      if (is_gimple_min_invariant (builtexpr))
+		{
+		  PRE_EXPR_CONSTANT (eprime) = builtexpr;
+		}
+	      else
+		{
+		  tree forcedexpr = force_gimple_operand (builtexpr,
+							  &stmts, true,
+							  NULL);
+		  if (is_gimple_min_invariant (forcedexpr))
+		    {
+		      PRE_EXPR_CONSTANT (eprime) = forcedexpr;
+		    }
+		  else
+		    {
+		      if (forcedexpr != builtexpr)
+			{
+			  VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime);
+			  VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime);
+			}
+		      if (stmts)
+			{
+			  tree_stmt_iterator tsi;
+			  tsi = tsi_start (stmts);
+			  for (; !tsi_end_p (tsi); tsi_next (&tsi))
+			    {
+			      tree stmt = tsi_stmt (tsi);
+			      tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
+			      VEC_safe_push (tree, heap, inserted_exprs, stmt);
+			      NECESSARY (lhs) = 0;
+			    }
+			  bsi_insert_on_edge (pred, stmts);
+			}
+		      NECESSARY (forcedexpr) = 0;
+		      avail[bprime->index] = get_or_alloc_expr_for_name (forcedexpr);
+		    }
+		}
+	    }  
+	}
+      else if (eprime->kind == NAME)
+	{
+	  /* We may have to do a conversion because our value
+	     numbering can look through types in certain cases, but
+	     our IL requires all operands of a phi node have the same
+	     type.  */
+	  tree name = PRE_EXPR_NAME (eprime);
+	  if (TREE_TYPE (name) != type)
+	    {
+	      tree builtexpr;
+	      tree forcedexpr;
+	      /* When eliminating casts through unions, 
+		 we sometimes want to convert a real to an integer,
+		 which fold_convert will ICE on  */
+	      if (fold_convertible_p (type, name))
+		builtexpr = fold_convert (type, name);
+	      else
+		builtexpr = convert (type, name);
+	      
+	      forcedexpr = force_gimple_operand (builtexpr,
+						 &stmts, true,
+						 NULL);
+
+	      if (forcedexpr != name)
+		{
+		  VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum;
+		  VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id;
+		}
+	      
+	      if (stmts)
+		{
+		  tree_stmt_iterator tsi;
+		  tsi = tsi_start (stmts);
+		  for (; !tsi_end_p (tsi); tsi_next (&tsi))
+		    {
+		      tree stmt = tsi_stmt (tsi);
+		      tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
+		      VEC_safe_push (tree, heap, inserted_exprs, stmt);
+		      NECESSARY (lhs) = 0;
+		    }
+		  bsi_insert_on_edge (pred, stmts);
+		}
+	      NECESSARY (forcedexpr) = 0;
+	      avail[bprime->index] = get_or_alloc_expr_for_name (forcedexpr);
+	    }
+	}
     }
   /* If we didn't want a phi node, and we made insertions, we still have
      inserted new stuff, and thus return true.  If we didn't want a phi node,
@@ -2646,7 +2953,6 @@ insert_into_preds_of_block (basic_block 
   temp = prephitemp;
   add_referenced_var (temp);
 
-
   if (TREE_CODE (type) == COMPLEX_TYPE
       || TREE_CODE (type) == VECTOR_TYPE)
     DECL_GIMPLE_REG_P (temp) = 1;
@@ -2654,12 +2960,21 @@ insert_into_preds_of_block (basic_block 
 
   NECESSARY (temp) = 0;
   VN_INFO_GET (PHI_RESULT (temp))->valnum = PHI_RESULT (temp);
-
+  VN_INFO (PHI_RESULT (temp))->value_id = val;
   VEC_safe_push (tree, heap, inserted_exprs, temp);
   FOR_EACH_EDGE (pred, ei, block->preds)
-    add_phi_arg (temp, avail[pred->src->index], pred);
-
-  vn_add (PHI_RESULT (temp), val);
+    {
+      pre_expr ae = avail[pred->src->index];
+      gcc_assert (get_expr_type (ae) == type
+		  || useless_type_conversion_p (type, get_expr_type (ae)));
+      if (ae->kind == CONSTANT)
+	add_phi_arg (temp, PRE_EXPR_CONSTANT (ae), pred);
+      else
+	add_phi_arg (temp, PRE_EXPR_NAME (avail[pred->src->index]), pred);
+    }
+  
+  newphi = get_or_alloc_expr_for_name (PHI_RESULT (temp));
+  add_to_value (val, newphi);
 
   /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
      this insertion, since we test for the existence of this value in PHI_GEN
@@ -2675,12 +2990,11 @@ insert_into_preds_of_block (basic_block 
      AVAIL_OUT, and would have been skipped due to the full redundancy check.
   */
 
-  bitmap_insert_into_set (PHI_GEN (block),
-			  PHI_RESULT (temp));
+  bitmap_insert_into_set (PHI_GEN (block), newphi);
   bitmap_value_replace_in_set (AVAIL_OUT (block),
-			       PHI_RESULT (temp));
+			       newphi);
   bitmap_insert_into_set (NEW_SETS (block),
-			  PHI_RESULT (temp));
+			  newphi);
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
@@ -2716,26 +3030,26 @@ static bool
 do_regular_insertion (basic_block block, basic_block dom)
 {
   bool new_stuff = false;
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
-  tree expr;
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
+  pre_expr expr;
   int i;
 
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
     {
-      if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
+      if (expr->kind != NAME)
 	{
-	  tree *avail;
-	  tree val;
+	  pre_expr *avail;
+	  unsigned int val;
 	  bool by_some = false;
 	  bool cant_insert = false;
 	  bool all_same = true;
-	  tree first_s = NULL;
+	  pre_expr first_s = NULL;
 	  edge pred;
 	  basic_block bprime;
-	  tree eprime = NULL_TREE;
+	  pre_expr eprime = NULL;
 	  edge_iterator ei;
 
-	  val = get_value_handle (expr);
+	  val = get_expr_value_id (expr);
 	  if (bitmap_set_contains_value (PHI_GEN (block), val))
 	    continue;
 	  if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
@@ -2745,11 +3059,11 @@ do_regular_insertion (basic_block block,
 	      continue;
 	    }
 
-	  avail = XCNEWVEC (tree, last_basic_block);
+	  avail = XCNEWVEC (pre_expr, last_basic_block);
 	  FOR_EACH_EDGE (pred, ei, block->preds)
 	    {
-	      tree vprime;
-	      tree edoubleprime;
+	      unsigned int vprime;
+	      pre_expr edoubleprime;
 
 	      /* This can happen in the very weird case
 		 that our fake infinite loop edges have caused a
@@ -2779,8 +3093,7 @@ do_regular_insertion (basic_block block,
 		}
 
 	      eprime = fully_constant_expression (eprime);
-	      vprime = get_value_handle (eprime);
-	      gcc_assert (vprime);
+	      vprime = get_expr_value_id (eprime);
 	      edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
 						 vprime, NULL_TREE);
 	      if (edoubleprime == NULL)
@@ -2794,8 +3107,7 @@ do_regular_insertion (basic_block block,
 		  by_some = true;
 		  if (first_s == NULL)
 		    first_s = edoubleprime;
-		  else if (!operand_equal_p (first_s, edoubleprime,
-					     0))
+		  else if (!pre_expr_eq (first_s, edoubleprime))
 		    all_same = false;
 		}
 	    }
@@ -2813,19 +3125,26 @@ do_regular_insertion (basic_block block,
 	     an invariant, then the PHI has the same value on all
 	     edges.  Note this.  */
 	  else if (!cant_insert && all_same && eprime
-		   && is_gimple_min_invariant (eprime)
-		   && !is_gimple_min_invariant (val))
+		   && eprime->kind == CONSTANT
+		   && !value_id_constant_p (val))
 	    {
 	      unsigned int j;
 	      bitmap_iterator bi;
+	      bitmap_set_t exprset = VEC_index (bitmap_set_t,
+						value_expressions, val);
 
-	      bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
+	      unsigned int new_val = get_expr_value_id (eprime);
 	      FOR_EACH_EXPR_ID_IN_SET (exprset, j, bi)
 		{
-		  tree expr = expression_for_id (j);
-		  if (TREE_CODE (expr) == SSA_NAME)
+		  pre_expr expr = expression_for_id (j);
+
+		  if (expr->kind == NAME)
 		    {
-		      vn_add (expr, eprime);
+		      vn_ssa_aux_t info = VN_INFO (PRE_EXPR_NAME (expr));
+		      /* Just reset the value id and valnum so it is
+			 the same as the constant we have discovered.  */
+		      info->valnum = PRE_EXPR_CONSTANT (eprime);
+		      info->value_id = new_val;
 		      pre_stats.constified++;
 		    }
 		}
@@ -2834,7 +3153,7 @@ do_regular_insertion (basic_block block,
 	}
     }
 
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
   return new_stuff;
 }
 
@@ -2850,34 +3169,34 @@ static bool
 do_partial_partial_insertion (basic_block block, basic_block dom)
 {
   bool new_stuff = false;
-  VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (PA_IN (block));
-  tree expr;
+  VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (PA_IN (block));
+  pre_expr expr;
   int i;
 
-  for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
+  for (i = 0; VEC_iterate (pre_expr, exprs, i, expr); i++)
     {
-      if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
+      if (expr->kind != NAME)
 	{
-	  tree *avail;
-	  tree val;
+	  pre_expr *avail;
+	  unsigned int val;
 	  bool by_all = true;
 	  bool cant_insert = false;
 	  edge pred;
 	  basic_block bprime;
-	  tree eprime = NULL_TREE;
+	  pre_expr eprime = NULL;
 	  edge_iterator ei;
 
-	  val = get_value_handle (expr);
+	  val = get_expr_value_id (expr);
 	  if (bitmap_set_contains_value (PHI_GEN (block), val))
 	    continue;
 	  if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
 	    continue;
 
-	  avail = XCNEWVEC (tree, last_basic_block);
+	  avail = XCNEWVEC (pre_expr, last_basic_block);
 	  FOR_EACH_EDGE (pred, ei, block->preds)
 	    {
-	      tree vprime;
-	      tree edoubleprime;
+	      unsigned int vprime;
+	      pre_expr edoubleprime;
 
 	      /* This can happen in the very weird case
 		 that our fake infinite loop edges have caused a
@@ -2908,8 +3227,7 @@ do_partial_partial_insertion (basic_bloc
 		}
 
 	      eprime = fully_constant_expression (eprime);
-	      vprime = get_value_handle (eprime);
-	      gcc_assert (vprime);
+	      vprime = get_expr_value_id (eprime);
 	      edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
 						 vprime, NULL_TREE);
 	      if (edoubleprime == NULL)
@@ -2926,7 +3244,7 @@ do_partial_partial_insertion (basic_bloc
 	     already existing along every predecessor, and
 	     it's defined by some predecessor, it is
 	     partially redundant.  */
-	  if (!cant_insert && by_all)
+	  if (!cant_insert && by_all && dbg_cnt (treepre_insert))
 	    {
 	      pre_stats.pa_insert++;
 	      if (insert_into_preds_of_block (block, get_expression_id (expr),
@@ -2937,7 +3255,7 @@ do_partial_partial_insertion (basic_bloc
 	}
     }
 
-  VEC_free (tree, heap, exprs);
+  VEC_free (pre_expr, heap, exprs);
   return new_stuff;
 }
 
@@ -2964,7 +3282,7 @@ insert_aux (basic_block block)
 		 to replace it with.  */
 	      FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
 		{
-		  tree expr = expression_for_id (i);
+		  pre_expr expr = expression_for_id (i);
 		  bitmap_value_replace_in_set (NEW_SETS (block), expr);
 		  bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
 		}
@@ -3019,163 +3337,17 @@ add_to_exp_gen (basic_block block, tree 
 {
   if (!in_fre)
     {
+      pre_expr result;
       if (TREE_CODE (op) == SSA_NAME && ssa_undefined_value_p (op))
 	return;
-      bitmap_value_insert_into_set (EXP_GEN (block), op);
+      result = get_or_alloc_expr_for_name (op);
+      bitmap_value_insert_into_set (EXP_GEN (block), result);
       if (TREE_CODE (op) != SSA_NAME
 	  || TREE_CODE (SSA_NAME_DEF_STMT (op)) != PHI_NODE)
-	bitmap_value_insert_into_set (maximal_set, op);
-    }
-}
-
-
-/* Given an SSA variable VAR and an expression EXPR, compute the value
-   number for EXPR and create a value handle (VAL) for it.  If VAR and
-   EXPR are not the same, associate VAL with VAR.  Finally, add VAR to
-   S1 and its value handle to S2, and to the maximal set if
-   ADD_TO_MAXIMAL is true.
-
-   VUSES represent the virtual use operands associated with EXPR (if
-   any).  */
-
-static inline void
-add_to_sets (tree var, tree expr, VEC(tree, gc) *vuses, bitmap_set_t s1,
-	     bitmap_set_t s2)
-{
-  tree val;
-  val = vn_lookup_or_add_with_vuses (expr, vuses);
-
-  /* VAR and EXPR may be the same when processing statements for which
-     we are not computing value numbers (e.g., non-assignments, or
-     statements that make aliased stores).  In those cases, we are
-     only interested in making VAR available as its own value.  */
-  if (var != expr)
-    vn_add (var, val);
-
-  if (s1)
-    bitmap_insert_into_set (s1, var);
-
-  bitmap_value_insert_into_set (s2, var);
-}
-
-/* Find existing value expression that is the same as T,
-   and return it if it exists.  */
-
-static inline tree
-find_existing_value_expr (tree t, VEC (tree, gc) *vuses)
-{
-  bitmap_iterator bi;
-  unsigned int bii;
-  tree vh;
-  bitmap_set_t exprset;
-
-  if (REFERENCE_CLASS_P (t) || TREE_CODE (t) == CALL_EXPR || DECL_P (t))
-    vh = vn_lookup_with_vuses (t, vuses);
-  else
-    vh = vn_lookup (t);
-
-  if (!vh)
-    return NULL;
-  exprset = VALUE_HANDLE_EXPR_SET (vh);
-  FOR_EACH_EXPR_ID_IN_SET (exprset, bii, bi)
-    {
-      tree efi = expression_for_id (bii);
-      if (expressions_equal_p (t, efi))
-	return efi;
-    }
-  return NULL;
-}
-
-/* Given a unary or binary expression EXPR, create and return a new
-   expression with the same structure as EXPR but with its operands
-   replaced with the value handles of each of the operands of EXPR.
-
-   VUSES represent the virtual use operands associated with EXPR (if
-   any).  Insert EXPR's operands into the EXP_GEN set for BLOCK.
-
-   If CHECK_AVAIL is true, checks availability of each operand in
-   BLOCKs AVAIL_OUT set.  */
-
-static inline tree
-create_value_expr_from (tree expr, basic_block block, VEC (tree, gc) *vuses,
-			bool check_avail)
-{
-  int i;
-  enum tree_code code = TREE_CODE (expr);
-  tree vexpr;
-  alloc_pool pool = NULL;
-  tree efi;
-
-  gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
-	      || TREE_CODE_CLASS (code) == tcc_binary
-	      || TREE_CODE_CLASS (code) == tcc_comparison
-	      || TREE_CODE_CLASS (code) == tcc_reference
-	      || TREE_CODE_CLASS (code) == tcc_expression
-	      || TREE_CODE_CLASS (code) == tcc_vl_exp
-	      || TREE_CODE_CLASS (code) == tcc_exceptional
-	      || TREE_CODE_CLASS (code) == tcc_declaration);
-
-  if (TREE_CODE_CLASS (code) == tcc_unary)
-    pool = unary_node_pool;
-  else if (TREE_CODE_CLASS (code) == tcc_reference)
-    pool = reference_node_pool;
-  else if (TREE_CODE_CLASS (code) == tcc_binary)
-    pool = binary_node_pool;
-  else if (TREE_CODE_CLASS (code) == tcc_comparison)
-    pool = comparison_node_pool;
-  else
-    gcc_assert (code == CALL_EXPR);
-
-  if (code == CALL_EXPR)
-    vexpr = temp_copy_call_expr (expr);
-  else
-    {
-      vexpr = (tree) pool_alloc (pool);
-      memcpy (vexpr, expr, tree_size (expr));
-    }
-
-  for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
-    {
-      tree val = NULL_TREE;
-      tree op;
-
-      op = TREE_OPERAND (expr, i);
-      if (op == NULL_TREE)
-	continue;
-
-      /* Recursively value-numberize reference ops and tree lists.  */
-      if (REFERENCE_CLASS_P (op))
-	{
-	  tree tempop = create_value_expr_from (op, block, vuses, check_avail);
-	  op = tempop ? tempop : op;
-	  val = vn_lookup_or_add_with_vuses (op, vuses);
-	  set_expression_vuses (op, vuses);
-	}
-      else
-	{
-	  val = vn_lookup_or_add (op);
-	}
-      if (TREE_CODE (op) != TREE_LIST)
-	add_to_exp_gen (block, op);
-
-      if (TREE_CODE (val) == VALUE_HANDLE)
-	TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
-
-      TREE_OPERAND (vexpr, i) = val;
-
-      if (check_avail
-	  && TREE_CODE (val) == VALUE_HANDLE
-	  && !bitmap_set_contains_value (AVAIL_OUT (block), val))
-	return NULL_TREE;
+	bitmap_value_insert_into_set (maximal_set, result);
     }
-  efi = find_existing_value_expr (vexpr, vuses);
-  if (efi)
-    return efi;
-  get_or_alloc_expression_id (vexpr);
-  return vexpr;
 }
 
-
 /* For each real store operation of the form
    *a = <value> that we see, create a corresponding fake store of the
    form storetmp_<version> = *a.
@@ -3287,57 +3459,7 @@ realify_fake_stores (void)
     }
 }
 
-/* Given an SSA_NAME, see if SCCVN has a value number for it, and if
-   so, return the value handle for this value number, creating it if
-   necessary.
-   Return NULL if SCCVN has no info for us.  */
-
-static tree
-get_sccvn_value (tree name)
-{
-  if (TREE_CODE (name) == SSA_NAME
-      && VN_INFO (name)->valnum != name
-      && VN_INFO (name)->valnum != VN_TOP)
-    {
-      tree val = VN_INFO (name)->valnum;
-      bool is_invariant = is_gimple_min_invariant (val);
-      tree valvh = !is_invariant ? get_value_handle (val) : NULL_TREE;
-
-      /* We may end up with situations where SCCVN has chosen a
-	 representative for the equivalence set that we have not
-	 visited yet.  In this case, just create the value handle for
-	 it.  */
-      if (!valvh && !is_invariant)
-	{
-	  /* We lookup with the LHS, so do not use vn_lookup_or_add_with_stmt
-	     here, as that will result in useless reference lookups.  */
-	  valvh = vn_lookup_or_add (val);
-	}
-
-      if (dump_file && (dump_flags & TDF_DETAILS))
-	{
-	  fprintf (dump_file, "SCCVN says ");
-	  print_generic_expr (dump_file, name, 0);
-	  fprintf (dump_file, " value numbers to ");
-	  if (valvh && !is_invariant)
-	    {
-	      print_generic_expr (dump_file, val, 0);
-	      fprintf (dump_file, " (");
-	      print_generic_expr (dump_file, valvh, 0);
-	      fprintf (dump_file, ")\n");
-	    }
-	  else
-	    print_generic_stmt (dump_file, val, 0);
-	}
-      if (valvh)
-	return valvh;
-      else
-	return val;
-    }
-  return NULL_TREE;
-}
-
-/* Create value handles for PHI in BLOCK.  */
+/* Create value ids for PHI in BLOCK.  */
 
 static void
 make_values_for_phi (tree phi, basic_block block)
@@ -3347,122 +3469,11 @@ make_values_for_phi (tree phi, basic_blo
      actual computations.  */
   if (is_gimple_reg (result))
     {
-      tree sccvnval = get_sccvn_value (result);
-      if (sccvnval)
-	{
-	  vn_add (result, sccvnval);
-	  bitmap_insert_into_set (PHI_GEN (block), result);
-	  bitmap_value_insert_into_set (AVAIL_OUT (block), result);
-	}
-      else
-	add_to_sets (result, result, NULL,
-		     PHI_GEN (block), AVAIL_OUT (block));
-    }
-}
-
-/* Create value handles for STMT in BLOCK.  Return true if we handled
-   the statement.  */
-
-static bool
-make_values_for_stmt (tree stmt, basic_block block)
-{
-
-  tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
-  tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
-  tree valvh = NULL_TREE;
-  tree lhsval;
-  VEC (tree, gc) *vuses = NULL;
-
-  valvh = get_sccvn_value (lhs);
-
-  if (valvh)
-    {
-      vn_add (lhs, valvh);
-      bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-      /* Shortcut for FRE. We have no need to create value expressions,
-	 just want to know what values are available where.  */
-      if (in_fre)
-	return true;
-
-    }
-  else if (in_fre)
-    {
-      /* For FRE, if SCCVN didn't find anything, we aren't going to
-	 either, so just make up a new value number if necessary and
-	 call it a day */
-      if (get_value_handle (lhs) == NULL)
-	vn_lookup_or_add (lhs);
-      bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-      return true;
-    }
-
-  lhsval = valvh ? valvh : get_value_handle (lhs);
-  vuses = copy_vuses_from_stmt (stmt);
-  STRIP_USELESS_TYPE_CONVERSION (rhs);
-  if (can_value_number_operation (rhs)
-      && (!lhsval || !is_gimple_min_invariant (lhsval))
-      && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
-    {
-      /* For value numberable operation, create a
-	 duplicate expression with the operands replaced
-	 with the value handles of the original RHS.  */
-      tree newt = create_value_expr_from (rhs, block, vuses, false);
-      if (newt)
-	{
-	  set_expression_vuses (newt, vuses);
-	  /* If we already have a value number for the LHS, reuse
-	     it rather than creating a new one.  */
-	  if (lhsval)
-	    {
-	      set_value_handle (newt, lhsval);
-	      if (!is_gimple_min_invariant (lhsval))
-		add_to_value (lhsval, newt);
-	    }
-	  else
-	    {
-	      tree val = vn_lookup_or_add_with_vuses (newt, vuses);
-	      vn_add (lhs, val);
-	    }
-
-	  add_to_exp_gen (block, newt);
-	}
-
-      bitmap_insert_into_set (TMP_GEN (block), lhs);
-      bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-      return true;
-    }
-  else if ((TREE_CODE (rhs) == SSA_NAME
-	    && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
-	   || is_gimple_min_invariant (rhs)
-	   || TREE_CODE (rhs) == ADDR_EXPR
-	   || DECL_P (rhs))
-    {
-
-      if (lhsval)
-	{
-	  set_expression_vuses (rhs, vuses);
-	  set_value_handle (rhs, lhsval);
-	  if (!is_gimple_min_invariant (lhsval))
-	    add_to_value (lhsval, rhs);
-	  bitmap_insert_into_set (TMP_GEN (block), lhs);
-	  bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
-	}
-      else
-	{
-	  /* Compute a value number for the RHS of the statement
-	     and add its value to the AVAIL_OUT set for the block.
-	     Add the LHS to TMP_GEN.  */
-	  set_expression_vuses (rhs, vuses);
-	  add_to_sets (lhs, rhs, vuses, TMP_GEN (block),
-		       AVAIL_OUT (block));
-	}
-      /* None of the rest of these can be PRE'd.  */
-      if (TREE_CODE (rhs) == SSA_NAME && !ssa_undefined_value_p (rhs))
-	add_to_exp_gen (block, rhs);
-      return true;
+      pre_expr e = get_or_alloc_expr_for_name (result);
+      add_to_value (get_expr_value_id (e), e);
+      bitmap_insert_into_set (PHI_GEN (block), e);
+      bitmap_value_insert_into_set (AVAIL_OUT (block), e);
     }
-  return false;
-
 }
 
 /* Compute the AVAIL set for all basic blocks.
@@ -3478,11 +3489,12 @@ make_values_for_stmt (tree stmt, basic_b
 static void
 compute_avail (void)
 {
+
   basic_block block, son;
   basic_block *worklist;
   size_t sp = 0;
   tree param;
-
+  
   /* For arguments with default definitions, we pretend they are
      defined in the entry block.  */
   for (param = DECL_ARGUMENTS (current_function_decl);
@@ -3492,14 +3504,15 @@ compute_avail (void)
       if (gimple_default_def (cfun, param) != NULL)
 	{
 	  tree def = gimple_default_def (cfun, param);
+	  pre_expr e = get_or_alloc_expr_for_name (def);
 
-	  vn_lookup_or_add (def);
+	  add_to_value (get_expr_value_id (e), e);	  
 	  if (!in_fre)
 	    {
-	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
-	      bitmap_value_insert_into_set (maximal_set, def);
+	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), e);
+	      bitmap_value_insert_into_set (maximal_set, e);
 	    }
-	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
+	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), e);
 	}
     }
 
@@ -3510,14 +3523,15 @@ compute_avail (void)
       if (gimple_default_def (cfun, param) != NULL)
 	{
 	  tree def = gimple_default_def (cfun, param);
+	  pre_expr e = get_or_alloc_expr_for_name (def);
 
-	  vn_lookup_or_add (def);
+	  add_to_value (get_expr_value_id (e), e);
 	  if (!in_fre)
 	    {
-	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
-	      bitmap_value_insert_into_set (maximal_set, def);
+	      bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), e);
+	      bitmap_value_insert_into_set (maximal_set, e);
 	    }
-	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
+	  bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), e);
 	}
     }
 
@@ -3565,67 +3579,119 @@ compute_avail (void)
 
 	  set_gimple_stmt_uid (stmt, stmt_uid++);
 
-	  /* For regular value numbering, we are only interested in
-	     assignments of the form X_i = EXPR, where EXPR represents
-	     an "interesting" computation, it has no volatile operands
-	     and X_i doesn't flow through an abnormal edge.  */
-	  if (TREE_CODE (stmt) == RETURN_EXPR
-	      && !ann->has_volatile_ops)
-	    {
-	      tree realstmt = stmt;
-	      tree lhs;
-	      tree rhs;
-
-	      stmt = TREE_OPERAND (stmt, 0);
-	      if (stmt && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
-		{
-		  lhs = GIMPLE_STMT_OPERAND (stmt, 0);
-		  rhs = GIMPLE_STMT_OPERAND (stmt, 1);
-		  if (TREE_CODE (lhs) == SSA_NAME
-		      && is_gimple_min_invariant (VN_INFO (lhs)->valnum))
-		    {
-		      if (dump_file && (dump_flags & TDF_DETAILS))
-			{
-			  fprintf (dump_file, "SCCVN says ");
-			  print_generic_expr (dump_file, lhs, 0);
-			  fprintf (dump_file, " value numbers to ");
-			  print_generic_stmt (dump_file, VN_INFO (lhs)->valnum,
-					      0);
-			}
-		      vn_add (lhs, VN_INFO (lhs)->valnum);
-		      continue;
-		    }
-
-		  if (TREE_CODE (rhs) == SSA_NAME)
-		    add_to_exp_gen (block, rhs);
+	  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
+	    {
+	      pre_expr e = get_or_alloc_expr_for_name (op);
 
-		  FOR_EACH_SSA_TREE_OPERAND (op, realstmt, iter, SSA_OP_DEF)
-		    add_to_sets (op, op, NULL, TMP_GEN (block),
-				 AVAIL_OUT (block));
+	      add_to_value (get_expr_value_id (e), e);
+	      if (!in_fre)
+		{
+		  bitmap_insert_into_set (TMP_GEN (block), e);
+		  bitmap_value_insert_into_set (maximal_set, e);
 		}
-	      continue;
+	      bitmap_value_insert_into_set (AVAIL_OUT (block), e);
 	    }
 
-	  else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
-		   && !ann->has_volatile_ops
-		   && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
-		   && !tree_could_throw_p (stmt))
+	  switch (TREE_CODE (stmt)) 
 	    {
-	      if (make_values_for_stmt (stmt, block))
+	    case RETURN_EXPR:
+	      if (!ann->has_volatile_ops)
+		FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
+		  add_to_exp_gen (block, op);
+	      continue;
+	    case GIMPLE_MODIFY_STMT:
+	      {
+		tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
+		if (!ann->has_volatile_ops
+		    && !tree_could_throw_p (stmt))
+		  {
+		    pre_expr result = NULL;
+		    switch (TREE_CODE_CLASS (TREE_CODE (rhs)))
+		      {
+		      case tcc_unary:
+			if (is_exception_related (rhs))
+			  continue;
+		      case tcc_binary:
+			{
+			  vn_nary_op_t nary;
+			  unsigned int i;
+			  
+			  vn_nary_op_lookup (rhs, &nary);
+
+			  if (!nary)
+			    continue;
+
+			  for (i = 0; i < nary->length; i++)
+			    if (TREE_CODE (nary->op[i]) == SSA_NAME)
+			      add_to_exp_gen (block, nary->op[i]);
+			  
+			  result = (pre_expr) pool_alloc (pre_expr_pool);
+			  result->kind = NARY;
+			  result->id = 0;
+			  PRE_EXPR_NARY (result) = nary;
+			}
+			break;
+		      case tcc_vl_exp:
+			if (!can_value_number_call (rhs))
+			  continue;
+
+		      case tcc_declaration:
+		      case tcc_reference:
+			{
+			  vn_reference_t ref;
+			  unsigned int i;
+			  vn_reference_op_t vro;
+
+			  vn_reference_lookup (rhs,
+					       shared_vuses_from_stmt (stmt),
+					       true, &ref);
+			  if (!ref)
+			    continue;
+
+			  for (i = 0; VEC_iterate (vn_reference_op_s,
+						   ref->operands, i,
+						   vro); i++)
+			    {
+			      if (vro->op0 && TREE_CODE (vro->op0) == SSA_NAME)
+				add_to_exp_gen (block, vro->op0);
+			      if (vro->op1 && TREE_CODE (vro->op1) == SSA_NAME)
+				add_to_exp_gen (block, vro->op1);
+			    }
+			  result = (pre_expr) pool_alloc (pre_expr_pool);
+			  result->kind = REFERENCE;
+			  result->id = 0;
+			  PRE_EXPR_REFERENCE (result) = ref;
+			}
+			break;
+		      default:
+			{
+			  /* For any other statement that we don't
+			     recognize, simply add all referenced
+			     SSA_NAMEs to EXP_GEN.  */
+			  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
+			    add_to_exp_gen (block, op);
+			  continue;
+			}
+		      }
+		    get_or_alloc_expression_id (result);
+		    add_to_value (get_expr_value_id (result), result);
+		    if (!in_fre)
+		      {
+			bitmap_value_insert_into_set (EXP_GEN (block),
+						      result);
+			bitmap_value_insert_into_set (maximal_set, result);
+		      }  
+
+		  }
 		continue;
+	      }
+	    default:
+	      break;
+	      
 	    }
+	  
 
-	  /* For any other statement that we don't recognize, simply
-	     make the names generated by the statement available in
-	     AVAIL_OUT and TMP_GEN.  */
-	  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
-	    add_to_sets (op, op, NULL, TMP_GEN (block), AVAIL_OUT (block));
-
-	  /* Also add all referenced SSA_NAMEs to EXP_GEN.  */
-	  FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
-	    add_to_exp_gen (block, op);
 	}
-
       /* Put the dominator children of BLOCK on the worklist of blocks
 	 to compute available sets for.  */
       for (son = first_dom_son (CDI_DOMINATORS, block);
@@ -3648,18 +3714,21 @@ do_SCCVN_insertion (tree stmt, tree ssa_
   basic_block bb = bb_for_stmt (stmt);
   block_stmt_iterator bsi;
   tree expr, stmts;
+  pre_expr e;
 
   /* First create a value expression from the expression we want
      to insert and associate it with the value handle for SSA_VN.  */
-  expr = create_value_expr_from (VN_INFO (ssa_vn)->expr, bb, NULL, true);
-  if (expr == NULL_TREE)
+  
+  /* TODO: Handle complex expressions.  */
+  e = get_or_alloc_expr_for (VN_INFO (ssa_vn)->expr);
+  if (e == NULL)
     return NULL_TREE;
-  set_value_handle (expr, get_value_handle (ssa_vn));
 
-  /* Then use create_expression_by_pieces to generate a valid
+/* Then use create_expression_by_pieces to generate a valid
      expression to insert at this point of the IL stream.  */
   stmts = alloc_stmt_list ();
-  expr = create_expression_by_pieces (bb, expr, stmts, stmt);
+  expr = create_expression_by_pieces (bb, e, stmts, stmt,
+				      NULL);
   if (expr == NULL_TREE)
     return NULL_TREE;
   bsi = bsi_for_stmt (stmt);
@@ -3695,24 +3764,56 @@ eliminate (void)
 	    {
 	      tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
 	      tree *rhs_p = &GIMPLE_STMT_OPERAND (stmt, 1);
-	      tree sprime;
+	      tree sprime = NULL;
+	      pre_expr lhsexpr = get_or_alloc_expr_for_name (lhs);
+	      pre_expr sprimeexpr;
+
+	      sprimeexpr = bitmap_find_leader (AVAIL_OUT (b),
+					       get_expr_value_id (lhsexpr),
+					       NULL_TREE);
 
-	      sprime = bitmap_find_leader (AVAIL_OUT (b),
-					   get_value_handle (lhs), NULL_TREE);
+	      if (sprimeexpr)
+		{
+		  if (sprimeexpr->kind == CONSTANT)
+		    sprime = PRE_EXPR_CONSTANT (sprimeexpr);
+		  else if (sprimeexpr->kind == NAME)
+		    sprime = PRE_EXPR_NAME (sprimeexpr);
+		  else
+		    gcc_unreachable ();
+		}
+	      /* If there is no existing leader but SCCVN knows this
+		 value is constant, use that constant.  */
+	      if (!sprime && is_gimple_min_invariant (VN_INFO (lhs)->valnum))
+		{
+		  sprime = VN_INFO (lhs)->valnum;
 
+		  if (dump_file && (dump_flags & TDF_DETAILS))
+		    {
+		      fprintf (dump_file, "Replaced ");
+		      print_generic_expr (dump_file, *rhs_p, 0);
+		      fprintf (dump_file, " with ");
+		      print_generic_expr (dump_file, sprime, 0);
+		      fprintf (dump_file, " in ");
+		      print_generic_stmt (dump_file, stmt, 0);
+		    }
+		  pre_stats.eliminations++;
+		  propagate_tree_value (rhs_p, sprime);
+		  update_stmt (stmt);
+		  continue;
+		}
+	      
 	      /* If there is no existing usable leader but SCCVN thinks
 		 it has an expression it wants to use as replacement,
 		 insert that.  */
-	      if (!sprime
-		  || sprime == lhs)
+	      if (!sprime || sprime == lhs)
 		{
 		  tree val = VN_INFO (lhs)->valnum;
 		  if (val != VN_TOP
+		      && TREE_CODE (val) == SSA_NAME
 		      && VN_INFO (val)->needs_insertion
 		      && can_PRE_operation (VN_INFO (val)->expr))
 		    sprime = do_SCCVN_insertion (stmt, val);
 		}
-
 	      if (sprime
 		  && sprime != lhs
 		  && (TREE_CODE (*rhs_p) != SSA_NAME
@@ -3921,6 +4022,15 @@ remove_dead_inserted_code (void)
 static void
 init_pre (bool do_fre)
 {
+  basic_block bb;
+
+  next_expression_id = 1;
+  expressions = NULL;
+  VEC_safe_push (pre_expr, heap, expressions, NULL);
+  value_expressions = VEC_alloc (bitmap_set_t, heap, get_max_value_id () + 1);
+  VEC_safe_grow_cleared (bitmap_set_t, heap, value_expressions,
+			 get_max_value_id() + 1);
+  
   in_fre = do_fre;
 
   inserted_exprs = NULL;
@@ -3929,16 +4039,38 @@ init_pre (bool do_fre)
   storetemp = NULL_TREE;
   prephitemp = NULL_TREE;
 
-  if (!do_fre)
-    loop_optimizer_init (LOOPS_NORMAL);
-
   connect_infinite_loops_to_exit ();
   memset (&pre_stats, 0, sizeof (pre_stats));
 
+
+  postorder = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
+  post_order_compute (postorder, false, false);
+
+  FOR_ALL_BB (bb)
+    bb->aux = XCNEWVEC (struct bb_bitmap_sets, 1);
+
   calculate_dominance_info (CDI_POST_DOMINATORS);
   calculate_dominance_info (CDI_DOMINATORS);
 
-  init_antic ();
+  bitmap_obstack_initialize (&grand_bitmap_obstack);
+  phi_translate_table = htab_create (5110, expr_pred_trans_hash,
+				     expr_pred_trans_eq, free);
+  expression_to_id = htab_create (num_ssa_names * 3,
+				  pre_expr_hash,
+				  pre_expr_eq, NULL);
+  seen_during_translate = BITMAP_ALLOC (&grand_bitmap_obstack);
+  bitmap_set_pool = create_alloc_pool ("Bitmap sets",
+				       sizeof (struct bitmap_set), 30);
+  pre_expr_pool = create_alloc_pool ("pre_expr nodes",
+				     sizeof (struct pre_expr_d), 30);
+  FOR_ALL_BB (bb)
+    {
+      EXP_GEN (bb) = bitmap_set_new ();
+      PHI_GEN (bb) = bitmap_set_new ();
+      TMP_GEN (bb) = bitmap_set_new ();
+      AVAIL_OUT (bb) = bitmap_set_new ();
+    }
+  maximal_set = in_fre ? NULL : bitmap_set_new ();
 
   need_eh_cleanup = BITMAP_ALLOC (NULL);
 }
@@ -3949,14 +4081,24 @@ init_pre (bool do_fre)
 static void
 fini_pre (void)
 {
-  unsigned int i;
+  basic_block bb;
 
+  free (postorder);
+  VEC_free (bitmap_set_t, heap, value_expressions);
   VEC_free (tree, heap, inserted_exprs);
   VEC_free (tree, heap, need_creation);
+  bitmap_obstack_release (&grand_bitmap_obstack);
+  free_alloc_pool (bitmap_set_pool);
+  free_alloc_pool (pre_expr_pool);
   htab_delete (phi_translate_table);
+  htab_delete (expression_to_id);
   remove_fake_exit_edges ();
-
-  fini_antic ();
+  
+  FOR_ALL_BB (bb)
+    {
+      free (bb->aux);
+      bb->aux = NULL;
+    }
 
   free_dominance_info (CDI_POST_DOMINATORS);
 
@@ -3968,19 +4110,6 @@ fini_pre (void)
 
   BITMAP_FREE (need_eh_cleanup);
 
-  /* Wipe out pointers to VALUE_HANDLEs.  In the not terribly distant
-     future we will want them to be persistent though.  */
-  for (i = 0; i < num_ssa_names; i++)
-    {
-      tree name = ssa_name (i);
-
-      if (!name)
-	continue;
-
-      if (SSA_NAME_VALUE (name)
-	  && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
-	SSA_NAME_VALUE (name) = NULL;
-    }
   if (current_loops != NULL)
     loop_optimizer_finalize ();
 }
@@ -3994,20 +4123,24 @@ execute_pre (bool do_fre)
   unsigned int todo = 0;
 
   do_partial_partial = optimize > 2;
-  init_pre (do_fre);
 
+  /* This has to happen before SCCVN runs because
+     loop_optimizer_init may create new phis, etc.  */
   if (!do_fre)
+    loop_optimizer_init (LOOPS_NORMAL);
+  if (0 && !do_fre)
     insert_fake_stores ();
 
-  /* Collect and value number expressions computed in each basic block.  */
   if (!run_scc_vn (do_fre))
     {
       if (!do_fre)
 	remove_dead_inserted_code ();
-      fini_pre ();
       return 0;
     }
-  switch_to_PRE_table ();
+  init_pre (do_fre);
+
+
+  /* Collect and value number expressions computed in each basic block.  */
   compute_avail ();
 
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -4032,16 +4165,6 @@ execute_pre (bool do_fre)
   if (!do_fre && n_basic_blocks < 4000)
     {
       compute_antic ();
-  if (dump_file && (dump_flags & TDF_DETAILS))
-    {
-      basic_block bb;
-
-      FOR_ALL_BB (bb)
-	{
-	  print_bitmap_set (dump_file, ANTIC_IN (bb), "antic_in", bb->index);
-	}
-    }
-
       insert ();
     }
 
@@ -4060,7 +4183,8 @@ execute_pre (bool do_fre)
   if (!do_fre)
     {
       remove_dead_inserted_code ();
-      realify_fake_stores ();
+      if (0)
+	realify_fake_stores ();
     }
 
   fini_pre ();
Index: gcc/tree-flow.h
===================================================================
--- gcc/tree-flow.h	(revision 137479)
+++ gcc/tree-flow.h	(working copy)
@@ -1124,9 +1124,10 @@ extern bool remove_stmt_from_eh_region (
 extern bool maybe_clean_or_replace_eh_stmt (tree, tree);
 
 /* In tree-ssa-pre.c  */
-void add_to_value (tree, tree);
-void debug_value_expressions (tree);
-void print_value_expressions (FILE *, tree);
+struct pre_expr_d;
+void add_to_value (unsigned int, struct pre_expr_d *);
+void debug_value_expressions (unsigned int);
+void print_value_expressions (FILE *, unsigned int);
 
 
 /* In tree-vn.c  */
Index: gcc/Makefile.in
===================================================================
--- gcc/Makefile.in	(revision 137479)
+++ gcc/Makefile.in	(working copy)
@@ -1222,7 +1222,6 @@ OBJS-common = \
 	tree-vect-patterns.o \
 	tree-vect-transform.o \
 	tree-vectorizer.o \
-	tree-vn.o \
 	tree-vrp.o \
 	tree.o \
 	value-prof.o \
@@ -2108,16 +2107,14 @@ tree-ssa-pre.o : tree-ssa-pre.c $(TREE_F
    $(SYSTEM_H) $(TREE_H) $(GGC_H) $(DIAGNOSTIC_H) $(TIMEVAR_H) \
    $(TM_H) coretypes.h $(TREE_DUMP_H) tree-pass.h $(FLAGS_H) $(CFGLOOP_H) \
    alloc-pool.h $(BASIC_BLOCK_H) bitmap.h $(HASHTAB_H) $(TREE_GIMPLE_H) \
-   $(TREE_INLINE_H) tree-iterator.h tree-ssa-sccvn.h $(PARAMS_H)
+   $(TREE_INLINE_H) tree-iterator.h tree-ssa-sccvn.h $(PARAMS_H) \
+   $(DBGCNT_H)
 tree-ssa-sccvn.o : tree-ssa-sccvn.c $(TREE_FLOW_H) $(CONFIG_H) \
    $(SYSTEM_H) $(TREE_H) $(GGC_H) $(DIAGNOSTIC_H) $(TIMEVAR_H) \
    $(TM_H) coretypes.h $(TREE_DUMP_H) tree-pass.h $(FLAGS_H) $(CFGLOOP_H) \
    alloc-pool.h $(BASIC_BLOCK_H) bitmap.h $(HASHTAB_H) $(TREE_GIMPLE_H) \
    $(TREE_INLINE_H) tree-iterator.h tree-ssa-propagate.h tree-ssa-sccvn.h \
    $(PARAMS_H)
-tree-vn.o : tree-vn.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(GGC_H) \
-   $(TREE_H) $(TREE_FLOW_H) $(HASHTAB_H) langhooks.h tree-pass.h \
-   $(TREE_DUMP_H) $(DIAGNOSTIC_H) tree-ssa-sccvn.h
 tree-vrp.o : tree-vrp.c $(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H) $(TREE_H) \
    $(TREE_FLOW_H) tree-pass.h $(TREE_DUMP_H) $(DIAGNOSTIC_H) $(GGC_H) \
    $(BASIC_BLOCK_H) tree-ssa-propagate.h $(FLAGS_H) $(TREE_DUMP_H) \
Index: gcc/tree-ssa-threadedge.c
===================================================================
--- gcc/tree-ssa-threadedge.c	(revision 137479)
+++ gcc/tree-ssa-threadedge.c	(working copy)
@@ -313,7 +313,7 @@ record_temporary_equivalences_from_stmts
 	      copy[i++] = use;
 	      if (TREE_CODE (use) == SSA_NAME)
 		tmp = SSA_NAME_VALUE (use);
-	      if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
+	      if (tmp)
 		SET_USE (use_p, tmp);
 	    }
 
@@ -407,14 +407,14 @@ simplify_control_stmt_condition (edge e,
       if (TREE_CODE (op0) == SSA_NAME)
 	{
           tree tmp = SSA_NAME_VALUE (op0);
-	  if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
+	  if (tmp)
 	    op0 = tmp;
 	}
 
       if (TREE_CODE (op1) == SSA_NAME)
 	{
 	  tree tmp = SSA_NAME_VALUE (op1);
-	  if (tmp && TREE_CODE (tmp) != VALUE_HANDLE)
+	  if (tmp)
 	    op1 = tmp;
 	}
 

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-07 16:49       ` Daniel Berlin
@ 2008-07-08 16:42         ` Daniel Berlin
  2008-07-09  2:10           ` ICE caused by "[PATCH]: Rewrite tree level PRE" Hans-Peter Nilsson
  0 siblings, 1 reply; 16+ messages in thread
From: Daniel Berlin @ 2008-07-08 16:42 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc-patches

This is now committed.
Please remember this is going to cause a few ssa-fre-* failures, and
one ssa-pre failure for a short time. :)



On Mon, Jul 7, 2008 at 11:51 AM, Daniel Berlin <dberlin@dberlin.org> wrote:
> I've attached the latest, and what i plan on committing, which also
> fixes a speed issue Richard found in interpert.ii from libjava.
>
> The differences between thisand the last one is that
> iterative_hash_hashval_t is made externally visible in tree.c.
>
>
>
> On Sun, Jul 6, 2008 at 3:30 PM, Daniel Berlin <dberlin@dberlin.org> wrote:
>> I got it going on my home machine.
>> The attached fixes the problem.
>>
>>
>> On Sun, Jul 6, 2008 at 1:46 PM, Daniel Berlin <dberlin@dberlin.org> wrote:
>>> On Sun, Jul 6, 2008 at 10:32 AM, Richard Guenther
>>> <richard.guenther@gmail.com> wrote:
>>>> On Sun, Jul 6, 2008 at 6:33 AM, Daniel Berlin <dberlin@dberlin.org> wrote:
>>>>> This patch effectively rewrites tree PRE to share datastructures with
>>>>> tree-vn, instead of copying statements and modifying trees (which
>>>>> don't work so well in a tuples world :P).
>>>>>
>>>>> As part of the rewrite, it also fixes a bunch of missed optimizations
>>>>> because PRE will now move globals.
>>>>> It also uses roughly half as much memory as before (since PRE no
>>>>> longer copies statements but instead shares the vn structures with the
>>>>> vn).
>>>>> The fundamentals are the same as they were before, it simply no longer
>>>>> does it's own value numbering.
>>>>> Since PRE was the only user of VALUE_HANDLES, i have removed them as
>>>>> part of this patch.
>>>>> Since PRE was the only user of most of tree-vn.c, i removed it and
>>>>> moved the 3 still-used functions to tree-ssa-sccvn.c
>>>>> PRE is also now type correct, in that the insertions it creates should
>>>>> have matched types, whereas before they were generally a mismash (it
>>>>> would happily create phi nodes whose arguments had mixed types, etc).
>>>>>
>>>>> This patch will cause some FRE failures due to a small bit of code i
>>>>> disabled in tree-ssa-sccvn.c.  I have discussed this with richard
>>>>> guenther and he has said he will fix it up, so the ssa-fre-* failures
>>>>> are temporary.
>>>>>
>>>>> Other than than PRE of globals, it should PRE all of the expressions
>>>>> we used to, plus a few more in the way of loads.  I have verified this
>>>>> by hand comparing the eliminations performed during a bootstrap of
>>>>> gcc.
>>>>> I have not performed more benchmarking than that, as the main goal was
>>>>> a feature equivalent rewrite that would enable tuple conversion to
>>>>> procede. The fact that it will PRE globals is simply a side effect of
>>>>> sharing a representation with the VN.
>>>>>
>>>>> The patch is large but 99% of it is the rewritten tree-ssa-pre.c.
>>>>>
>>>>> Bootstrapped and regtested on i686-darwin (With the safe-ctype change
>>>>> reverted) and i686-linux.
>>>>> I will wait a few days for comments before committing it (All the code
>>>>> outside of PRE that I am removing was added as part of PRE, but if
>>>>> someone wants to object to it's removal, please let me know).
>>>>
>>>> This triggers a latent issue in SCCVN (it breaks bootstrap on x86_64 with Ada).
>>>>
>>>
>>> I don't have an ada compiler on my x86_64. If i send you an updated
>>> patch, can you test it for me with ada?
>>>
>>
>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* ICE caused by "[PATCH]: Rewrite tree level PRE"
  2008-07-08 16:42         ` Daniel Berlin
@ 2008-07-09  2:10           ` Hans-Peter Nilsson
  2008-07-09  8:21             ` Daniel Berlin
  2008-07-09 17:35             ` Daniel Berlin
  0 siblings, 2 replies; 16+ messages in thread
From: Hans-Peter Nilsson @ 2008-07-09  2:10 UTC (permalink / raw)
  To: dberlin; +Cc: gcc-patches

> Date: Tue, 8 Jul 2008 12:07:40 -0400
> From: "Daniel Berlin" <dberlin@dberlin.org>

> This is now committed.
> Please remember this is going to cause a few ssa-fre-* failures, and
> one ssa-pre failure for a short time. :)

I assume you refer to the commit r137631 being the cause of these:

FAIL: gcc.dg/tree-ssa/ssa-fre-13.c scan-tree-dump fre "Inserted .* &a"
FAIL: gcc.dg/tree-ssa/ssa-fre-13.c scan-tree-dump fre "Replaced tmp1_.\(D\)->data"
FAIL: gcc.dg/tree-ssa/ssa-fre-14.c scan-tree-dump fre "Inserted .* &a"
FAIL: gcc.dg/tree-ssa/ssa-fre-14.c scan-tree-dump fre "Replaced tmp1.data"
FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump-times fre "Inserted pretmp" 1
FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump-times fre "Replaced a.u.f with pretmp" 3
FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump-times fre "Replaced a.u.k with j" 1
FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump fre "= VIEW_CONVERT_EXPR<float>\(j_"
FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump optimized "return j"
FAIL: gcc.dg/tree-ssa/ssa-fre-8.c scan-tree-dump-times fre "Replaced u.f with pretmp" 2
FAIL: gcc.dg/tree-ssa/ssa-fre-8.c scan-tree-dump-times fre "Inserted pretmp" 2
FAIL: gcc.dg/tree-ssa/ssa-fre-9.c scan-tree-dump-times fre "Eliminated: 1" 2
FAIL: gcc.dg/tree-ssa/ssa-fre-9.c scan-tree-dump-times fre "Insertions: 1" 2
FAIL: gcc.dg/tree-ssa/ssa-pre-15.c scan-tree-dump optimized "= 0;"

but, for at least cris-axis-elf, it also caused this:

Running /tmp/hpautotest-gcc1/gcc/gcc/testsuite/gfortran.fortran-torture/compile/compile.exp ...
FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O2   (internal compiler error)
FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O2 -fomit-frame-pointer -finline-functions   (internal compiler error)
FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O2 -fomit-frame-pointer -finline-functions -funroll-loops   (internal compiler error)
FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O3 -g   (internal compiler error)

in gfortran.log:
x/gcc/testsuite/gfortran.fortran-torture/compile/pr32663.f: In function 'dimoid':
x/gcc/testsuite/gfortran.fortran-torture/compile/pr32663.f:1: internal compiler error: Segmentation fault

brgds, H-P

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: ICE caused by "[PATCH]: Rewrite tree level PRE"
  2008-07-09  2:10           ` ICE caused by "[PATCH]: Rewrite tree level PRE" Hans-Peter Nilsson
@ 2008-07-09  8:21             ` Daniel Berlin
  2008-07-09  9:07               ` Richard Guenther
  2008-07-09 17:35             ` Daniel Berlin
  1 sibling, 1 reply; 16+ messages in thread
From: Daniel Berlin @ 2008-07-09  8:21 UTC (permalink / raw)
  To: Hans-Peter Nilsson; +Cc: gcc-patches

On Tue, Jul 8, 2008 at 9:26 PM, Hans-Peter Nilsson
<hans-peter.nilsson@axis.com> wrote:
>> Date: Tue, 8 Jul 2008 12:07:40 -0400
>> From: "Daniel Berlin" <dberlin@dberlin.org>
>
>> This is now committed.
>> Please remember this is going to cause a few ssa-fre-* failures, and
>> one ssa-pre failure for a short time. :)
>
> I assume you refer to the commit r137631 being the cause of these:
>
> FAIL: gcc.dg/tree-ssa/ssa-fre-13.c scan-tree-dump fre "Inserted .* &a"
> FAIL: gcc.dg/tree-ssa/ssa-fre-13.c scan-tree-dump fre "Replaced tmp1_.\(D\)->data"
> FAIL: gcc.dg/tree-ssa/ssa-fre-14.c scan-tree-dump fre "Inserted .* &a"
> FAIL: gcc.dg/tree-ssa/ssa-fre-14.c scan-tree-dump fre "Replaced tmp1.data"
> FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump-times fre "Inserted pretmp" 1
> FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump-times fre "Replaced a.u.f with pretmp" 3
> FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump-times fre "Replaced a.u.k with j" 1
> FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump fre "= VIEW_CONVERT_EXPR<float>\(j_"
> FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump optimized "return j"
> FAIL: gcc.dg/tree-ssa/ssa-fre-8.c scan-tree-dump-times fre "Replaced u.f with pretmp" 2
> FAIL: gcc.dg/tree-ssa/ssa-fre-8.c scan-tree-dump-times fre "Inserted pretmp" 2
> FAIL: gcc.dg/tree-ssa/ssa-fre-9.c scan-tree-dump-times fre "Eliminated: 1" 2
> FAIL: gcc.dg/tree-ssa/ssa-fre-9.c scan-tree-dump-times fre "Insertions: 1" 2
> FAIL: gcc.dg/tree-ssa/ssa-pre-15.c scan-tree-dump optimized "= 0;"
>

Yes.


> but, for at least cris-axis-elf, it also caused this:
>
> Running /tmp/hpautotest-gcc1/gcc/gcc/testsuite/gfortran.fortran-torture/compile/compile.exp ...
> FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O2   (internal compiler error)
> FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O2 -fomit-frame-pointer -finline-functions   (internal compiler error)
> FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O2 -fomit-frame-pointer -finline-functions -funroll-loops   (internal compiler error)
> FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O3 -g   (internal compiler error)
>
> in gfortran.log:
> x/gcc/testsuite/gfortran.fortran-torture/compile/pr32663.f: In function 'dimoid':
> x/gcc/testsuite/gfortran.fortran-torture/compile/pr32663.f:1: internal compiler error: Segmentation fault
>
> brgds, H-P
>


Fixing this now, i've got a patch in testing.
Thanks,
Dan

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: ICE caused by "[PATCH]: Rewrite tree level PRE"
  2008-07-09  8:21             ` Daniel Berlin
@ 2008-07-09  9:07               ` Richard Guenther
  0 siblings, 0 replies; 16+ messages in thread
From: Richard Guenther @ 2008-07-09  9:07 UTC (permalink / raw)
  To: Daniel Berlin; +Cc: Hans-Peter Nilsson, gcc-patches

On Wed, Jul 9, 2008 at 4:10 AM, Daniel Berlin <dberlin@dberlin.org> wrote:
> On Tue, Jul 8, 2008 at 9:26 PM, Hans-Peter Nilsson
> <hans-peter.nilsson@axis.com> wrote:
>>> Date: Tue, 8 Jul 2008 12:07:40 -0400
>>> From: "Daniel Berlin" <dberlin@dberlin.org>
>>
>>> This is now committed.
>>> Please remember this is going to cause a few ssa-fre-* failures, and
>>> one ssa-pre failure for a short time. :)
>>
>> I assume you refer to the commit r137631 being the cause of these:
>>
>> FAIL: gcc.dg/tree-ssa/ssa-fre-13.c scan-tree-dump fre "Inserted .* &a"
>> FAIL: gcc.dg/tree-ssa/ssa-fre-13.c scan-tree-dump fre "Replaced tmp1_.\(D\)->data"
>> FAIL: gcc.dg/tree-ssa/ssa-fre-14.c scan-tree-dump fre "Inserted .* &a"
>> FAIL: gcc.dg/tree-ssa/ssa-fre-14.c scan-tree-dump fre "Replaced tmp1.data"
>> FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump-times fre "Inserted pretmp" 1
>> FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump-times fre "Replaced a.u.f with pretmp" 3
>> FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump-times fre "Replaced a.u.k with j" 1
>> FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump fre "= VIEW_CONVERT_EXPR<float>\(j_"
>> FAIL: gcc.dg/tree-ssa/ssa-fre-7.c scan-tree-dump optimized "return j"
>> FAIL: gcc.dg/tree-ssa/ssa-fre-8.c scan-tree-dump-times fre "Replaced u.f with pretmp" 2
>> FAIL: gcc.dg/tree-ssa/ssa-fre-8.c scan-tree-dump-times fre "Inserted pretmp" 2
>> FAIL: gcc.dg/tree-ssa/ssa-fre-9.c scan-tree-dump-times fre "Eliminated: 1" 2
>> FAIL: gcc.dg/tree-ssa/ssa-fre-9.c scan-tree-dump-times fre "Insertions: 1" 2
>> FAIL: gcc.dg/tree-ssa/ssa-pre-15.c scan-tree-dump optimized "= 0;"
>>
>
> Yes.

Just to explain, these are the testcases for the VIEW_CONVERT_EXPR
insertion trick.  I volunteered to fix these and will do so, but not before
the tuples conversion of PRE.

Richard.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: ICE caused by "[PATCH]: Rewrite tree level PRE"
  2008-07-09  2:10           ` ICE caused by "[PATCH]: Rewrite tree level PRE" Hans-Peter Nilsson
  2008-07-09  8:21             ` Daniel Berlin
@ 2008-07-09 17:35             ` Daniel Berlin
  1 sibling, 0 replies; 16+ messages in thread
From: Daniel Berlin @ 2008-07-09 17:35 UTC (permalink / raw)
  To: Hans-Peter Nilsson; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1025 bytes --]

The attached patch fixes the fortran failure.
Bootstrapped and regtested on i686-linux-gnu

Committed to mainline

2008-07-09  Daniel Berlin  <dberlin@dberlin.org>

        * tree-ssa-pre.c (phi_translate_1): Update placement of
        add_to_value calls.


> Running /tmp/hpautotest-gcc1/gcc/gcc/testsuite/gfortran.fortran-torture/compile/compile.exp ...
> FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O2   (internal compiler error)
> FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O2 -fomit-frame-pointer -finline-functions   (internal compiler error)
> FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O2 -fomit-frame-pointer -finline-functions -funroll-loops   (internal compiler error)
> FAIL: gfortran.fortran-torture/compile/pr32663.f,  -O3 -g   (internal compiler error)
>
> in gfortran.log:
> x/gcc/testsuite/gfortran.fortran-torture/compile/pr32663.f: In function 'dimoid':
> x/gcc/testsuite/gfortran.fortran-torture/compile/pr32663.f:1: internal compiler error: Segmentation fault
>
> brgds, H-P
>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: fixfortran.diff --]
[-- Type: text/x-diff; name=fixfortran.diff, Size: 772 bytes --]

Index: tree-ssa-pre.c
===================================================================
--- tree-ssa-pre.c	(revision 137631)
+++ tree-ssa-pre.c	(working copy)
@@ -1399,8 +1399,8 @@ phi_translate_1 (pre_expr expr, bitmap_s
 		if (constant != expr)
 		  return constant;
 		get_or_alloc_expression_id (expr);
-		add_to_value (new_val_id, expr);
 	      }
+	    add_to_value (new_val_id, expr);
 	  }
 	phi_trans_add (oldexpr, expr, pred);
 	return expr;
@@ -1532,8 +1532,8 @@ phi_translate_1 (pre_expr expr, bitmap_s
 						     result, new_val_id);
 		PRE_EXPR_REFERENCE (expr) = newref;
 		get_or_alloc_expression_id (expr);
-		add_to_value (new_val_id, expr);
 	      }
+	    add_to_value (new_val_id, expr);
 	  }
 	phi_trans_add (oldexpr, expr, pred);
 	return expr;

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-06 15:23 ` Richard Guenther
  2008-07-06 19:19   ` Daniel Berlin
@ 2008-07-11 13:12   ` Eric Botcazou
  2008-07-11 14:01     ` Richard Guenther
  2008-07-11 18:22     ` Daniel Berlin
  1 sibling, 2 replies; 16+ messages in thread
From: Eric Botcazou @ 2008-07-11 13:12 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc-patches, Daniel Berlin

[-- Attachment #1: Type: text/plain, Size: 708 bytes --]

> This triggers a latent issue in SCCVN (it breaks bootstrap on x86_64 with
> Ada).
>
> copy_reference_ops_from_ref for ARRAY_REF also needs to record
> TREE_OPERAND (ref, 2) (likewise foro COMPONENT_REF).

Yes, we just ran into the problem with our 4.3-based compiler (but the 
testcase doesn't fail with the FSF 4.3 compiler).  We need something like


2008-07-11  Eric Botcazou  <ebotcazou@adacore.com>

	* tree-ssa-pre.c (create_component_ref_by_pieces) <COMPONENT_REF>:
	Deal with operand #2 as well.
	* tree-ssa-sccvn.c (copy_reference_ops_from_ref) <COMPONENT_REF>:
	Likewise.
	<ARRAY_REF>: Likewise.


to overcome the problem.  Should I install it on the 4.3 branch after testing?

-- 
Eric Botcazou

[-- Attachment #2: p.diff --]
[-- Type: text/x-diff, Size: 1456 bytes --]

Index: tree-ssa-pre.c
===================================================================
--- tree-ssa-pre.c	(revision 137455)
+++ tree-ssa-pre.c	(working copy)
@@ -2185,15 +2185,18 @@ create_component_ref_by_pieces (basic_bl
     case COMPONENT_REF:
       {
 	tree op0;
-	tree op1;
+	tree op1, op2;
 	op0 = create_component_ref_by_pieces (block,
 					      TREE_OPERAND (genop, 0),
 					      stmts);
 	/* op1 should be a FIELD_DECL, which are represented by
 	   themselves.  */
 	op1 = TREE_OPERAND (genop, 1);
+	op2 = TREE_OPERAND (genop, 2);
+	if (op2 && TREE_CODE (op2) == VALUE_HANDLE)
+	  op2 = find_or_generate_expression (block, op2, stmts);
 	folded = fold_build3 (COMPONENT_REF, TREE_TYPE (genop), op0, op1,
-			      NULL_TREE);
+			      op2);
 	return folded;
       }
       break;
Index: tree-ssa-sccvn.c
===================================================================
--- tree-ssa-sccvn.c	(revision 137455)
+++ tree-ssa-sccvn.c	(working copy)
@@ -540,12 +540,14 @@ copy_reference_ops_from_ref (tree ref, V
 	case COMPONENT_REF:
 	  /* Record field as operand.  */
 	  temp.op0 = TREE_OPERAND (ref, 1);
+	  temp.op1 = TREE_OPERAND (ref, 2);
 	  break;
 	case ARRAY_RANGE_REF:
 	case ARRAY_REF:
 	  /* Record index as operand.  */
 	  temp.op0 = TREE_OPERAND (ref, 1);
-	  temp.op1 = TREE_OPERAND (ref, 3);
+	  temp.op1 = TREE_OPERAND (ref, 2);
+	  temp.op2 = TREE_OPERAND (ref, 3);
 	  break;
 	case STRING_CST:
 	case INTEGER_CST:

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-11 13:12   ` [PATCH]: Rewrite tree level PRE Eric Botcazou
@ 2008-07-11 14:01     ` Richard Guenther
  2008-07-11 18:22     ` Daniel Berlin
  1 sibling, 0 replies; 16+ messages in thread
From: Richard Guenther @ 2008-07-11 14:01 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: gcc-patches, Daniel Berlin

On Fri, Jul 11, 2008 at 1:20 PM, Eric Botcazou <ebotcazou@adacore.com> wrote:
>> This triggers a latent issue in SCCVN (it breaks bootstrap on x86_64 with
>> Ada).
>>
>> copy_reference_ops_from_ref for ARRAY_REF also needs to record
>> TREE_OPERAND (ref, 2) (likewise foro COMPONENT_REF).
>
> Yes, we just ran into the problem with our 4.3-based compiler (but the
> testcase doesn't fail with the FSF 4.3 compiler).  We need something like
>
>
> 2008-07-11  Eric Botcazou  <ebotcazou@adacore.com>
>
>        * tree-ssa-pre.c (create_component_ref_by_pieces) <COMPONENT_REF>:
>        Deal with operand #2 as well.
>        * tree-ssa-sccvn.c (copy_reference_ops_from_ref) <COMPONENT_REF>:
>        Likewise.
>        <ARRAY_REF>: Likewise.
>
>
> to overcome the problem.  Should I install it on the 4.3 branch after testing?

Yes.  Bonus points if you have a testcase that triggers on the branch.

Thanks,
Richard.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-11 13:12   ` [PATCH]: Rewrite tree level PRE Eric Botcazou
  2008-07-11 14:01     ` Richard Guenther
@ 2008-07-11 18:22     ` Daniel Berlin
  2008-07-11 18:40       ` Eric Botcazou
  1 sibling, 1 reply; 16+ messages in thread
From: Daniel Berlin @ 2008-07-11 18:22 UTC (permalink / raw)
  To: Eric Botcazou; +Cc: Richard Guenther, gcc-patches

On Fri, Jul 11, 2008 at 7:20 AM, Eric Botcazou <ebotcazou@adacore.com> wrote:
>> This triggers a latent issue in SCCVN (it breaks bootstrap on x86_64 with
>> Ada).
>>
>> copy_reference_ops_from_ref for ARRAY_REF also needs to record
>> TREE_OPERAND (ref, 2) (likewise foro COMPONENT_REF).
>
> Yes, we just ran into the problem with our 4.3-based compiler (but the
> testcase doesn't fail with the FSF 4.3 compiler).  We need something like
>
>
> 2008-07-11  Eric Botcazou  <ebotcazou@adacore.com>
>
>        * tree-ssa-pre.c (create_component_ref_by_pieces) <COMPONENT_REF>:
>        Deal with operand #2 as well.
>        * tree-ssa-sccvn.c (copy_reference_ops_from_ref) <COMPONENT_REF>:
>        Likewise.
>        <ARRAY_REF>: Likewise.
>
>
> to overcome the problem.  Should I install it on the 4.3 branch after testing?

At least on the trunk, vn_reference_op_t's op2 wasn't being compared
(it didn't exist).
I assume op2 exists and is being compared by the vn_reference_op_eq
function on the branch?
If so, this is fine.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH]: Rewrite tree level PRE
  2008-07-11 18:22     ` Daniel Berlin
@ 2008-07-11 18:40       ` Eric Botcazou
  0 siblings, 0 replies; 16+ messages in thread
From: Eric Botcazou @ 2008-07-11 18:40 UTC (permalink / raw)
  To: Daniel Berlin; +Cc: Richard Guenther, gcc-patches

> I assume op2 exists and is being compared by the vn_reference_op_eq
> function on the branch?

Yup.

/* Compare two reference operands P1 and P2 for equality.  return true if
   they are equal, and false otherwise.  */

static int
vn_reference_op_eq (const void *p1, const void *p2)
{
  const_vn_reference_op_t const vro1 = (const_vn_reference_op_t) p1;
  const_vn_reference_op_t const vro2 = (const_vn_reference_op_t) p2;
  return vro1->opcode == vro2->opcode
    && vro1->type == vro2->type
    && expressions_equal_p (vro1->op0, vro2->op0)
    && expressions_equal_p (vro1->op1, vro2->op1)
    && expressions_equal_p (vro1->op2, vro2->op2);
}

> If so, this is fine.

Thanks.  Testing was successful so I'm going to install it.

-- 
Eric Botcazou

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2008-07-11 17:20 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-06  4:48 [PATCH]: Rewrite tree level PRE Daniel Berlin
2008-07-06  8:58 ` Ralf Wildenhues
2008-07-06 19:24   ` Daniel Berlin
2008-07-06 15:23 ` Richard Guenther
2008-07-06 19:19   ` Daniel Berlin
2008-07-06 22:12     ` Daniel Berlin
2008-07-07 16:49       ` Daniel Berlin
2008-07-08 16:42         ` Daniel Berlin
2008-07-09  2:10           ` ICE caused by "[PATCH]: Rewrite tree level PRE" Hans-Peter Nilsson
2008-07-09  8:21             ` Daniel Berlin
2008-07-09  9:07               ` Richard Guenther
2008-07-09 17:35             ` Daniel Berlin
2008-07-11 13:12   ` [PATCH]: Rewrite tree level PRE Eric Botcazou
2008-07-11 14:01     ` Richard Guenther
2008-07-11 18:22     ` Daniel Berlin
2008-07-11 18:40       ` Eric Botcazou

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).