along with GCC; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
+
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "errors.h"
#include "ggc.h"
#include "tree.h"
-
-/* These RTL headers are needed for basic-block.h. */
-#include "rtl.h"
-#include "tm_p.h"
-#include "hard-reg-set.h"
#include "basic-block.h"
#include "diagnostic.h"
#include "tree-inline.h"
#include "splay-tree.h"
#include "bitmap.h"
#include "langhooks.h"
+
/* TODO:
1. Implement load value numbering.
static tree find_leader (value_set_t, tree);
static void value_insert_into_set (value_set_t, tree);
static void insert_into_set (value_set_t, tree);
-static void add_to_value (tree, tree);
static value_set_t set_new (bool);
static bool is_undefined_value (tree);
-static bool expressions_equal_p (tree, tree);
static tree create_expression_by_pieces (basic_block, tree, tree);
/* We can add and remove elements and entries to and from sets
static alloc_pool binary_node_pool;
static alloc_pool unary_node_pool;
-/* The value table that maps expressions to values. */
-
-static htab_t value_table;
/* The phi_translate_table caches phi translations for a given
expression and predecessor. */
static htab_t phi_translate_table;
-/* Compare two expressions E1 and E2 and return true if they are
- equal. */
-
-static 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_CODE (e2)
- && (te1 == te2 || lang_hooks.types_compatible_p (te1, te2))
- && operand_equal_p (e1, e2, 0))
- return true;
- return false;
-}
-
-/* Map expressions to values. These are simple pairs of expressions
- and the values they represent. To find the value represented by
- an expression, we use a hash table where the elements are {e,v}
- pairs, and the expression is the key. */
-
-typedef struct val_expr_pair_d
-{
- tree v, e;
- hashval_t hashcode;
-} *val_expr_pair_t;
-
-
-/* Hash a {v,e} pair that is pointed to by P.
- The hashcode is cached in the val_expr_pair, so we just return
- that. */
-
-static hashval_t
-val_expr_pair_hash (const void *p)
-{
- const val_expr_pair_t ve = (val_expr_pair_t) p;
- return ve->hashcode;
-}
-
-
-/* Given two val_expr_pair_t's, return true if they represent the same
- expression, false otherwise.
- P1 and P2 should point to the val_expr_pair_t's to be compared. */
-
-static int
-val_expr_pair_expr_eq (const void *p1, const void *p2)
-{
- const val_expr_pair_t ve1 = (val_expr_pair_t) p1;
- const val_expr_pair_t ve2 = (val_expr_pair_t) p2;
-
- if (expressions_equal_p (ve1->e, ve2->e))
- return true;
-
- return false;
-}
-
-
-/* Get the value handle of EXPR. This is the only correct way to get
- the value handle for a "thing".
- Returns NULL if the value handle does not exist. */
-
-tree
-get_value_handle (tree expr)
-{
- /* We should never see these. */
- if (DECL_P (expr))
- abort ();
- else if (TREE_CODE (expr) == SSA_NAME)
- {
- return SSA_NAME_VALUE (expr);
- }
- else if (TREE_CODE_CLASS (TREE_CODE (expr)) == 'c')
- return expr;
- else if (EXPR_P (expr))
- {
- tree_ann_t ann = tree_ann (expr);
- if (ann)
- return ann->common.value_handle;
- return NULL;
- }
- abort ();
-}
-
-
-/* Set the value handle for expression E to value V */
-
-void
-set_value_handle (tree e, tree v)
-{
- if (DECL_P (e))
- abort ();
- else if (TREE_CODE (e) == SSA_NAME)
- SSA_NAME_VALUE (e) = v;
- else if (EXPR_P (e))
- get_tree_ann (e)->common.value_handle = v;
-}
-
/* A three tuple {e, pred, v} used to cache phi translations in the
phi_translate_table. */
struct expr_pred_trans_d ept;
ept.e = e;
ept.pred = pred;
- ept.hashcode = iterative_hash_expr (e, (unsigned long) pred);
+ ept.hashcode = vn_compute (e, (unsigned long) pred);
slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
NO_INSERT);
if (!slot)
new_pair->e = e;
new_pair->pred = pred;
new_pair->v = v;
- new_pair->hashcode = iterative_hash_expr (e, (unsigned long) pred);
+ new_pair->hashcode = vn_compute (e, (unsigned long) pred);
slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
new_pair->hashcode, INSERT);
if (*slot)
*slot = (void *) new_pair;
}
-/* Search in TABLE for an existing instance of expression E,
- and return its value, or NULL if none has been set. */
-
-static inline tree
-lookup (htab_t table, tree e)
-{
- void **slot;
- struct val_expr_pair_d vep = {NULL, NULL, 0};
- if (TREE_CODE_CLASS (TREE_CODE (e)) == 'c')
- return e;
- vep.e = e;
- vep.hashcode = iterative_hash_expr (e,0);
- slot = htab_find_slot_with_hash (table, &vep, vep.hashcode, NO_INSERT);
- if (!slot)
- return NULL_TREE;
- else
- return ((val_expr_pair_t) *slot)->v;
-}
-
/* Add expression E to the expression set of value V. */
-static inline void
+void
add_to_value (tree v, tree e)
{
- var_ann_t va;
/* For values representing non-CST nodes, but still function
invariant things we mark TREE_CONSTANT as true and set the tree
chain to the actual constant. This is because unlike values
TREE_CHAIN (v) = e;
return;
}
- va = var_ann (v);
- if (va->expr_set == NULL)
- va->expr_set = set_new (false);
- insert_into_set (va->expr_set, e);
-}
-
-/* Insert E into TABLE with value V, and add expression E to the value
- set for value V. */
-
-static inline void
-add (htab_t table, tree e, tree v)
-{
-
- void **slot;
- val_expr_pair_t new_pair = xmalloc (sizeof (struct val_expr_pair_d));
- new_pair->e = e;
- new_pair->v = v;
- new_pair->hashcode = iterative_hash_expr (e, 0);
- slot = htab_find_slot_with_hash (table, new_pair, new_pair->hashcode,
- INSERT);
- if (*slot)
- free (*slot);
- *slot = (void *) new_pair;
- set_value_handle (e, v);
-
- add_to_value (v, e);
-
-}
-
-/* A unique counter that is incremented every time we create a new
- value. */
-static int pre_uid;
-/* Create a new value handle for expression EXPR. */
-
-static tree
-create_new_value (tree expr)
-{
- tree a = create_tmp_var_raw (TREE_TYPE (expr), "value");
- create_var_ann (a);
- var_ann (a)->uid = pre_uid++;
+ if (VALUE_HANDLE_EXPR_SET (v) == NULL)
+ VALUE_HANDLE_EXPR_SET (v) = set_new (false);
- if (dump_file && (dump_flags & TDF_DETAILS))
- {
- fprintf (dump_file, "Created value ");
- print_generic_expr (dump_file, a, dump_flags);
- fprintf (dump_file, " for ");
- print_generic_expr (dump_file, expr, dump_flags);
- fprintf (dump_file, "\n");
- }
- return a;
+ insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
}
-/* Like lookup, but creates a new value for expression E if E doesn't
- already have a value.
- Return the existing/created value for E. */
-
-static inline tree
-lookup_or_add (htab_t table, tree e)
-{
- tree x = lookup (table, e);
- if (x == NULL_TREE)
- {
- tree v;
- v = create_new_value (e);
- add (table, e, v);
- x = v;
- }
- set_value_handle (e, x);
- return x;
-}
-
/* Return true if value V exists in the bitmap for SET. */
static inline bool
value_exists_in_set_bitmap (value_set_t set, tree v)
{
- if (TREE_CODE (v) != VAR_DECL)
- abort ();
-
if (!set->values)
return false;
- return bitmap_bit_p (set->values, get_var_ann (v)->uid);
+
+ return bitmap_bit_p (set->values, VALUE_HANDLE_ID (v));
}
+
/* Remove value V from the bitmap for SET. */
static void
value_remove_from_set_bitmap (value_set_t set, tree v)
{
- if (TREE_CODE (v) != VAR_DECL)
- abort ();
#ifdef ENABLE_CHECKING
if (!set->indexed)
abort ();
#endif
+
if (!set->values)
return;
- bitmap_clear_bit (set->values, get_var_ann (v)->uid);
+
+ bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
}
static inline void
value_insert_into_set_bitmap (value_set_t set, tree v)
{
- if (TREE_CODE (v) != VAR_DECL)
- abort ();
#ifdef ENABLE_CHECKING
if (!set->indexed)
abort ();
#endif
+
if (set->values == NULL)
{
set->values = BITMAP_GGC_ALLOC ();
bitmap_clear (set->values);
}
- bitmap_set_bit (set->values, get_var_ann (v)->uid);
+
+ bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
}
+
/* Create a new set. */
static value_set_t
{
value_set_node_t newnode = pool_alloc (value_set_node_pool);
tree val = get_value_handle (expr);
- if (DECL_P (expr))
- abort ();
if (val == NULL)
abort ();
{
tree val = get_value_handle (expr);
-
/* Constant and invariant values exist everywhere, and thus,
actually keeping them in the sets is pointless. */
if (TREE_CONSTANT (val))
}
/* Print out the expressions that have VAL to OUTFILE. */
+
void
print_value_expressions (FILE *outfile, tree val)
{
- var_ann_t va = var_ann (val);
- if (va && va->expr_set)
- print_value_set (outfile, va->expr_set,
- IDENTIFIER_POINTER (DECL_NAME (val)), 0);
+ if (VALUE_HANDLE_EXPR_SET (val))
+ {
+ char s[10];
+ sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
+ print_value_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
+ }
}
create_tree_ann (newexpr);
TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
- lookup_or_add (value_table, newexpr);
+ vn_lookup_or_add (newexpr);
expr = newexpr;
phi_trans_add (oldexpr, newexpr, pred);
}
memcpy (newexpr, expr, tree_size (expr));
create_tree_ann (newexpr);
TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
- lookup_or_add (value_table, newexpr);
+ vn_lookup_or_add (newexpr);
expr = newexpr;
phi_trans_add (oldexpr, newexpr, pred);
}
tree val;
if (is_undefined_value (PHI_ARG_DEF (phi, i)))
return NULL;
- val = lookup_or_add (value_table, PHI_ARG_DEF (phi, i));
+ val = vn_lookup_or_add (PHI_ARG_DEF (phi, i));
return PHI_ARG_DEF (phi, i);
}
}
fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
}
-/* Get the expressions represented by value VAL. */
-
-static value_set_t
-get_expr_set (tree val)
-{
- var_ann_t va = var_ann (val);
- return va->expr_set;
-}
-
/* Find a leader for an expression, or generate one using
create_expression_by_pieces if it's ANTIC but
not really . */
if (genop == NULL)
{
- genop = get_expr_set (expr)->head->expr;
+ genop = VALUE_HANDLE_EXPR_SET (expr)->head->expr;
if (TREE_CODE_CLASS (TREE_CODE (genop)) != '1'
&& TREE_CODE_CLASS (TREE_CODE (genop)) != '2')
abort ();
}
v = get_value_handle (expr);
- add (value_table, name, v);
+ vn_add (name, v);
insert_into_set (NEW_SETS (block), name);
value_insert_into_set (AVAIL_OUT (block), name);
if (dump_file && (dump_flags & TDF_DETAILS))
temp = create_tmp_var (type, "prephitmp");
add_referenced_tmp_var (temp);
temp = create_phi_node (temp, block);
- add (value_table, PHI_RESULT (temp), val);
+ vn_add (PHI_RESULT (temp), val);
#if 0
if (!set_contains_value (AVAIL_OUT (block), val))
fprintf (dump_file, "insert required %d iterations\n", num_iterations);
}
+
/* Return true if EXPR has no defining statement in this procedure,
*AND* isn't a live-on-entry parameter. */
+
static bool
is_undefined_value (tree expr)
{
-
#ifdef ENABLE_CHECKING
/* We should never be handed DECL's */
if (DECL_P (expr))
abort ();
#endif
+
if (TREE_CODE (expr) == SSA_NAME)
{
/* XXX: Is this the correct test? */
if (IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr)))
return true;
}
+
return false;
}
{
tree val;
tree def = default_def (param);
- val = lookup_or_add (value_table, def);
+ val = vn_lookup_or_add (def);
insert_into_set (TMP_GEN (block), def);
value_insert_into_set (AVAIL_OUT (block), def);
}
dom = get_immediate_dominator (CDI_DOMINATORS, block);
if (dom)
set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
+
for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
{
/* Ignore virtual PHIs until we can do PRE on expressions
if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
continue;
- lookup_or_add (value_table, PHI_RESULT (phi));
+ vn_lookup_or_add (PHI_RESULT (phi));
value_insert_into_set (AVAIL_OUT (block), PHI_RESULT (phi));
insert_into_set (PHI_GEN (block), PHI_RESULT (phi));
}
for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
{
tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
- lookup_or_add (value_table, def);
+ vn_lookup_or_add (def);
insert_into_set (TMP_GEN (block), def);
value_insert_into_set (AVAIL_OUT (block), def);
}
tree use = USE_OP (STMT_USE_OPS (stmt), j);
if (TREE_CODE (use) == SSA_NAME)
{
- lookup_or_add (value_table, use);
+ vn_lookup_or_add (use);
insert_into_set (TMP_GEN (block), use);
value_insert_into_set (AVAIL_OUT (block), use);
}
STRIP_USELESS_TYPE_CONVERSION (op1);
if (is_gimple_min_invariant (op1))
{
- add (value_table, op0, lookup_or_add (value_table, op1));
+ vn_add (op0, vn_lookup_or_add (op1));
insert_into_set (TMP_GEN (block), op0);
value_insert_into_set (AVAIL_OUT (block), op0);
}
tree newt;
bop1 = TREE_OPERAND (op1, 0);
bop2 = TREE_OPERAND (op1, 1);
- val1 = lookup_or_add (value_table, bop1);
- val2 = lookup_or_add (value_table, bop2);
+ val1 = vn_lookup_or_add (bop1);
+ val2 = vn_lookup_or_add (bop2);
newt = pool_alloc (binary_node_pool);
memcpy (newt, op1, tree_size (op1));
TREE_OPERAND (newt, 0) = val1;
TREE_OPERAND (newt, 1) = val2;
- val = lookup_or_add (value_table, newt);
- add (value_table, op0, val);
+ val = vn_lookup_or_add (newt);
+ vn_add (op0, val);
if (!is_undefined_value (bop1))
value_insert_into_set (EXP_GEN (block), bop1);
if (!is_undefined_value (bop2))
tree val, val1;
tree newt;
uop = TREE_OPERAND (op1, 0);
- val1 = lookup_or_add (value_table, uop);
+ val1 = vn_lookup_or_add (uop);
newt = pool_alloc (unary_node_pool);
memcpy (newt, op1, tree_size (op1));
TREE_OPERAND (newt, 0) = val1;
- val = lookup_or_add (value_table, newt);
- add (value_table, op0, val);
+ val = vn_lookup_or_add (newt);
+ vn_add (op0, val);
if (!is_undefined_value (uop))
value_insert_into_set (EXP_GEN (block), uop);
value_insert_into_set (EXP_GEN (block), newt);
}
else if (TREE_CODE (op1) == SSA_NAME)
{
- tree val = lookup_or_add (value_table, op1);
- add (value_table, op0, val);
+ tree val = vn_lookup_or_add (op1);
+ vn_add (op0, val);
if (!is_undefined_value (op1))
value_insert_into_set (EXP_GEN (block), op1);
insert_into_set (TMP_GEN (block), op0);
for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
{
tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
- lookup_or_add (value_table, def);
+ vn_lookup_or_add (def);
insert_into_set (TMP_GEN (block), def);
value_insert_into_set (AVAIL_OUT (block), def);
if (def != op0)
tree use = USE_OP (STMT_USE_OPS (stmt), j);
if (TREE_CODE (use) == SSA_NAME)
{
- lookup_or_add (value_table, use);
+ vn_lookup_or_add (use);
insert_into_set (TMP_GEN (block), use);
value_insert_into_set (AVAIL_OUT (block), use);
}
for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
{
tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
- lookup_or_add (value_table, def);
+ vn_lookup_or_add (def);
insert_into_set (TMP_GEN (block), def);
value_insert_into_set (AVAIL_OUT (block), def);
}
tree use = USE_OP (STMT_USE_OPS (stmt), j);
if (TREE_CODE (use) == SSA_NAME)
{
- lookup_or_add (value_table, use);
+ vn_lookup_or_add (use);
insert_into_set (TMP_GEN (block), use);
value_insert_into_set (AVAIL_OUT (block), use);
}
}
}
}
+
for (son = first_dom_son (CDI_DOMINATORS, block);
son;
son = next_dom_son (CDI_DOMINATORS, son))
compute_avail (son);
-
}
+
/* Eliminate fully redundant computations. */
static void
|| NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt))
|| stmt_ann (stmt)->has_volatile_ops)
continue;
+
/* Lookup the RHS of the expression, see if we have an
available computation for it. If so, replace the RHS with
the available computation. */
|| (TREE_CODE_CLASS (TREE_CODE (expr)) != '2'
&& TREE_CODE_CLASS (TREE_CODE (expr)) != '1'))
continue;
- sprime = find_leader (AVAIL_OUT (b),
- lookup (value_table, t));
+
+ sprime = find_leader (AVAIL_OUT (b), vn_lookup (t));
if (sprime
&& sprime != t
&& may_propagate_copy (sprime, TREE_OPERAND (stmt, 1)))
modify_stmt (stmt);
}
}
-
}
}
}
+
/* Main entry point to the SSA-PRE pass.
PHASE indicates which dump file from the DUMP_FILES array to use when
{
size_t tsize;
basic_block bb;
- pre_uid = num_referenced_vars;
memset (&pre_stats, 0, sizeof (pre_stats));
FOR_ALL_BB (bb)
{
phi_translate_table = htab_create (511, expr_pred_trans_hash,
expr_pred_trans_eq,
free);
- value_table = htab_create (511, val_expr_pair_hash,
- val_expr_pair_expr_eq, free);
value_set_pool = create_alloc_pool ("Value sets",
sizeof (struct value_set), 30);
value_set_node_pool = create_alloc_pool ("Value set nodes",
free_alloc_pool (value_set_node_pool);
free_alloc_pool (binary_node_pool);
free_alloc_pool (unary_node_pool);
- htab_delete (value_table);
htab_delete (phi_translate_table);
FOR_ALL_BB (bb)
--- /dev/null
+/* Value Numbering routines for tree expressions.
+ Copyright (C) 2001, 2002, 2003, 2004 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 2, 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 COPYING. If not, write to
+the Free Software Foundation, 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA. */
+
+#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"
+
+/* The value table that maps expressions to values. */
+static htab_t value_table;
+
+/* Map expressions to values. These are simple pairs of expressions
+ and the values they represent. To find the value represented by
+ an expression, we use a hash table where the elements are {e,v}
+ pairs, and the expression is the key. */
+typedef struct val_expr_pair_d
+{
+ tree v, e;
+ hashval_t hashcode;
+} *val_expr_pair_t;
+
+static void set_value_handle (tree e, tree v);
+
+
+/* Create and return a new value handle node of type TYPE. */
+
+static 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;
+}
+
+
+/* Given an expression or statement P, compute a hash value number using the
+ code of the expression and its real operands. */
+
+hashval_t
+vn_compute (tree expr, hashval_t val)
+{
+ val = iterative_hash_expr (expr, val);
+ return val;
+}
+
+
+/* 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_CODE (e2)
+ && (te1 == te2 || lang_hooks.types_compatible_p (te1, te2))
+ && operand_equal_p (e1, e2, 0))
+ return true;
+
+ return false;
+}
+
+
+/* Hash a {v,e} pair that is pointed to by P.
+ The hashcode is cached in the val_expr_pair, so we just return
+ that. */
+
+static hashval_t
+val_expr_pair_hash (const void *p)
+{
+ const val_expr_pair_t ve = (val_expr_pair_t) p;
+ return ve->hashcode;
+}
+
+
+/* Given two val_expr_pair_t's, return true if they represent the same
+ expression, false otherwise.
+ P1 and P2 should point to the val_expr_pair_t's to be compared. */
+
+static int
+val_expr_pair_expr_eq (const void *p1, const void *p2)
+{
+ const val_expr_pair_t ve1 = (val_expr_pair_t) p1;
+ const val_expr_pair_t ve2 = (val_expr_pair_t) p2;
+
+ if (expressions_equal_p (ve1->e, ve2->e))
+ return true;
+
+ return false;
+}
+
+
+/* Set the value handle for expression E to value V */
+
+static 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))
+ get_tree_ann (e)->common.value_handle = v;
+ else if (TREE_CODE_CLASS (TREE_CODE (e)) == 'c')
+ /* Do nothing. Constants are their own value handles. */
+ ;
+ else
+ abort ();
+}
+
+
+/* Insert E into VALUE_TABLE with value V, and add expression E to the
+ value set for value V. */
+
+void
+vn_add (tree e, tree v)
+{
+ void **slot;
+ val_expr_pair_t new_pair = xmalloc (sizeof (struct val_expr_pair_d));
+ new_pair->e = e;
+ new_pair->v = v;
+ new_pair->hashcode = vn_compute (e, 0);
+ slot = htab_find_slot_with_hash (value_table, new_pair, new_pair->hashcode,
+ INSERT);
+ if (*slot)
+ free (*slot);
+ *slot = (void *) new_pair;
+ set_value_handle (e, v);
+
+ add_to_value (v, e);
+}
+
+
+/* Search in VALUE_TABLE for an existing instance of expression E, and
+ return its value, or NULL if none has been set. */
+
+tree
+vn_lookup (tree e)
+{
+ void **slot;
+ struct val_expr_pair_d vep = {NULL, NULL, 0};
+
+ if (TREE_CODE_CLASS (TREE_CODE (e)) == 'c')
+ return e;
+ vep.e = e;
+ vep.hashcode = vn_compute (e, 0);
+ slot = htab_find_slot_with_hash (value_table, &vep, vep.hashcode, NO_INSERT);
+ if (!slot)
+ return NULL_TREE;
+ else
+ return ((val_expr_pair_t) *slot)->v;
+}
+
+
+/* Like vn_lookup, but creates a new value for expression E if E doesn't
+ already have a value. Return the existing/created value for E. */
+
+tree
+vn_lookup_or_add (tree e)
+{
+ tree x = vn_lookup (e);
+ if (x == NULL_TREE)
+ {
+ tree v = make_value_handle (TREE_TYPE (e));
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ {
+ fprintf (dump_file, "Created value ");
+ print_generic_expr (dump_file, v, dump_flags);
+ fprintf (dump_file, " for ");
+ print_generic_expr (dump_file, e, dump_flags);
+ fprintf (dump_file, "\n");
+ }
+
+ vn_add (e, v);
+ x = v;
+ }
+
+ set_value_handle (e, x);
+
+ return x;
+}
+
+
+/* Get the value handle of EXPR. This is the only correct way to get
+ the value handle for a "thing". If EXPR does not have a value
+ handle associated, it generates and returns a new one. */
+
+tree
+get_value_handle (tree expr)
+{
+ if (TREE_CODE (expr) == SSA_NAME)
+ return SSA_NAME_VALUE (expr);
+ else if (TREE_CODE_CLASS (TREE_CODE (expr)) == 'c')
+ return expr;
+ else if (EXPR_P (expr) || DECL_P (expr))
+ {
+ tree_ann_t ann = tree_ann (expr);
+ return ((ann) ? ann->common.value_handle : NULL_TREE);
+ }
+
+ abort ();
+}
+
+
+/* Initialize data structures used in value numbering. */
+
+void
+vn_init (void)
+{
+ value_table = htab_create (511, val_expr_pair_hash,
+ val_expr_pair_expr_eq, free);
+}
+
+
+/* Delete data used for value numbering. */
+
+void
+vn_delete (void)
+{
+ htab_delete (value_table);
+ value_table = NULL;
+}