aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcc/ChangeLog13
-rw-r--r--gcc/Makefile.in7
-rw-r--r--gcc/timevar.def1
-rw-r--r--gcc/tree-cfg.c121
-rw-r--r--gcc/tree-flow.h1
-rw-r--r--gcc/tree-optimize.c1
-rw-r--r--gcc/tree-outof-ssa.c4
-rw-r--r--gcc/tree-pass.h1
-rw-r--r--gcc/tree-ssa-uncprop.c614
9 files changed, 636 insertions, 127 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 63ce38a..86e3f97 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,16 @@
+2005-04-12 Jeff Law <law@redhat.com>
+
+ * Makefile.in (OBJS-common): Add tree-ssa-uncprop.o.
+ (tree-ssa-uncprop.o): Add dependencies.
+ * tree-cfg.c (remove_useless_stmts_bb, remove_useless_stmts): Remove.
+ * tree-flow.h (remove_useless_stmts): Remove prototype.
+ * tree-outof-ssa.c (rewrite_out_of_ssa): Remove call to
+ remove_useless_stmts.
+ * timevar.def (TV_TREE_SSA_UNCPROP): New timevar.
+ * tree-optimize.c (init_tree_optimization_passes): Add uncprop pass.
+ * tree-pass.h (pass_uncprop): Declare.
+ * tree-ssa-uncprop.c: New file.
+
2005-04-12 James E. Wilson <wilson@specifixinc.com>
PR target/20670
diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index fc3ab24..b720a37 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -924,7 +924,7 @@ OBJS-common = \
tree-chrec.o tree-scalar-evolution.o tree-data-ref.o \
tree-cfg.o tree-dfa.o tree-eh.o tree-ssa.o tree-optimize.o tree-gimple.o \
gimplify.o tree-pretty-print.o tree-into-ssa.o \
- tree-outof-ssa.o tree-ssa-ccp.o tree-vn.o \
+ tree-outof-ssa.o tree-ssa-ccp.o tree-vn.o tree-ssa-uncprop.o \
tree-ssa-dce.o tree-ssa-copy.o tree-nrv.o tree-ssa-copyrename.o \
tree-ssa-pre.o tree-ssa-live.o tree-ssa-operands.o tree-ssa-alias.o \
tree-ssa-phiopt.o tree-ssa-forwprop.o tree-nested.o tree-ssa-dse.o \
@@ -1666,6 +1666,11 @@ tree-ssa-dom.o : tree-ssa-dom.c $(TREE_FLOW_H) $(CONFIG_H) $(SYSTEM_H) \
errors.h function.h $(TIMEVAR_H) $(TM_H) coretypes.h $(TREE_DUMP_H) \
$(BASIC_BLOCK_H) domwalk.h real.h tree-pass.h $(FLAGS_H) langhooks.h \
tree-ssa-propagate.h cfgloop.h
+tree-ssa-uncprop.o : tree-ssa-uncprop.c $(TREE_FLOW_H) $(CONFIG_H) $(SYSTEM_H) \
+ $(RTL_H) $(TREE_H) $(TM_P_H) $(EXPR_H) $(GGC_H) output.h diagnostic.h \
+ errors.h function.h $(TIMEVAR_H) $(TM_H) coretypes.h $(TREE_DUMP_H) \
+ $(BASIC_BLOCK_H) domwalk.h real.h tree-pass.h $(FLAGS_H) langhooks.h \
+ tree-ssa-propagate.h cfgloop.h
tree-ssa-threadupdate.o : tree-ssa-threadupdate.c $(TREE_FLOW_H) $(CONFIG_H) \
$(SYSTEM_H) $(RTL_H) $(TREE_H) $(TM_P_H) $(EXPR_H) $(GGC_H) output.h \
diagnostic.h errors.h function.h $(TM_H) coretypes.h $(TREE_DUMP_H) \
diff --git a/gcc/timevar.def b/gcc/timevar.def
index b8d9408..a90afea 100644
--- a/gcc/timevar.def
+++ b/gcc/timevar.def
@@ -103,6 +103,7 @@ DEFTIMEVAR (TV_TREE_LOOP_IVOPTS , "tree iv optimization")
DEFTIMEVAR (TV_TREE_LOOP_INIT , "tree loop init")
DEFTIMEVAR (TV_TREE_LOOP_FINI , "tree loop fini")
DEFTIMEVAR (TV_TREE_CH , "tree copy headers")
+DEFTIMEVAR (TV_TREE_SSA_UNCPROP , "tree SSA uncprop")
DEFTIMEVAR (TV_TREE_SSA_TO_NORMAL , "tree SSA to normal")
DEFTIMEVAR (TV_TREE_NRV , "tree NRV optimization")
DEFTIMEVAR (TV_TREE_COPY_RENAME , "tree rename SSA copies")
diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c
index a9a9fdc..a80285b 100644
--- a/gcc/tree-cfg.c
+++ b/gcc/tree-cfg.c
@@ -1896,127 +1896,6 @@ struct tree_opt_pass pass_remove_useless_stmts =
0 /* letter */
};
-
-/* Remove obviously useless statements in basic block BB. */
-
-static void
-cfg_remove_useless_stmts_bb (basic_block bb)
-{
- block_stmt_iterator bsi;
- tree stmt = NULL_TREE;
- tree cond, var = NULL_TREE, val = NULL_TREE;
- struct var_ann_d *ann;
-
- /* Check whether we come here from a condition, and if so, get the
- condition. */
- if (!single_pred_p (bb)
- || !(single_pred_edge (bb)->flags
- & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
- return;
-
- cond = COND_EXPR_COND (last_stmt (single_pred (bb)));
-
- if (TREE_CODE (cond) == VAR_DECL || TREE_CODE (cond) == PARM_DECL)
- {
- var = cond;
- val = (single_pred_edge (bb)->flags & EDGE_FALSE_VALUE
- ? boolean_false_node : boolean_true_node);
- }
- else if (TREE_CODE (cond) == TRUTH_NOT_EXPR
- && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
- || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL))
- {
- var = TREE_OPERAND (cond, 0);
- val = (single_pred_edge (bb)->flags & EDGE_FALSE_VALUE
- ? boolean_true_node : boolean_false_node);
- }
- else
- {
- if (single_pred_edge (bb)->flags & EDGE_FALSE_VALUE)
- cond = invert_truthvalue (cond);
- if (TREE_CODE (cond) == EQ_EXPR
- && (TREE_CODE (TREE_OPERAND (cond, 0)) == VAR_DECL
- || TREE_CODE (TREE_OPERAND (cond, 0)) == PARM_DECL)
- && (TREE_CODE (TREE_OPERAND (cond, 1)) == VAR_DECL
- || TREE_CODE (TREE_OPERAND (cond, 1)) == PARM_DECL
- || TREE_CONSTANT (TREE_OPERAND (cond, 1))))
- {
- var = TREE_OPERAND (cond, 0);
- val = TREE_OPERAND (cond, 1);
- }
- else
- return;
- }
-
- /* Only work for normal local variables. */
- ann = var_ann (var);
- if (!ann
- || ann->may_aliases
- || TREE_ADDRESSABLE (var))
- return;
-
- if (! TREE_CONSTANT (val))
- {
- ann = var_ann (val);
- if (!ann
- || ann->may_aliases
- || TREE_ADDRESSABLE (val))
- return;
- }
-
- /* Ignore floating point variables, since comparison behaves weird for
- them. */
- if (FLOAT_TYPE_P (TREE_TYPE (var)))
- return;
-
- for (bsi = bsi_start (bb); !bsi_end_p (bsi);)
- {
- stmt = bsi_stmt (bsi);
-
- /* If the THEN/ELSE clause merely assigns a value to a variable/parameter
- which is already known to contain that value, then remove the useless
- THEN/ELSE clause. */
- if (TREE_CODE (stmt) == MODIFY_EXPR
- && TREE_OPERAND (stmt, 0) == var
- && operand_equal_p (val, TREE_OPERAND (stmt, 1), 0))
- {
- bsi_remove (&bsi);
- continue;
- }
-
- /* Invalidate the var if we encounter something that could modify it.
- Likewise for the value it was previously set to. Note that we only
- consider values that are either a VAR_DECL or PARM_DECL so we
- can test for conflict very simply. */
- if (TREE_CODE (stmt) == ASM_EXPR
- || (TREE_CODE (stmt) == MODIFY_EXPR
- && (TREE_OPERAND (stmt, 0) == var
- || TREE_OPERAND (stmt, 0) == val)))
- return;
-
- bsi_next (&bsi);
- }
-}
-
-
-/* A CFG-aware version of remove_useless_stmts. */
-
-void
-cfg_remove_useless_stmts (void)
-{
- basic_block bb;
-
-#ifdef ENABLE_CHECKING
- verify_flow_info ();
-#endif
-
- FOR_EACH_BB (bb)
- {
- cfg_remove_useless_stmts_bb (bb);
- }
-}
-
-
/* Remove PHI nodes associated with basic block BB and all edges out of BB. */
static void
diff --git a/gcc/tree-flow.h b/gcc/tree-flow.h
index 961ddde..8cdadd1 100644
--- a/gcc/tree-flow.h
+++ b/gcc/tree-flow.h
@@ -522,7 +522,6 @@ extern tree last_stmt (basic_block);
extern tree *last_stmt_ptr (basic_block);
extern tree last_and_only_stmt (basic_block);
extern edge find_taken_edge (basic_block, tree);
-extern void cfg_remove_useless_stmts (void);
extern basic_block label_to_block_fn (struct function *, tree);
#define label_to_block(t) (label_to_block_fn (cfun, t))
extern void bsi_insert_on_edge (edge, tree);
diff --git a/gcc/tree-optimize.c b/gcc/tree-optimize.c
index 4884229..4d172c2 100644
--- a/gcc/tree-optimize.c
+++ b/gcc/tree-optimize.c
@@ -407,6 +407,7 @@ init_tree_optimization_passes (void)
NEXT_PASS (pass_phiopt);
NEXT_PASS (pass_tail_calls);
NEXT_PASS (pass_rename_ssa_copies);
+ NEXT_PASS (pass_uncprop);
NEXT_PASS (pass_del_ssa);
NEXT_PASS (pass_nrv);
NEXT_PASS (pass_remove_useless_vars);
diff --git a/gcc/tree-outof-ssa.c b/gcc/tree-outof-ssa.c
index ecbee58..4663abb 100644
--- a/gcc/tree-outof-ssa.c
+++ b/gcc/tree-outof-ssa.c
@@ -2508,10 +2508,6 @@ rewrite_out_of_ssa (void)
if (dump_file && (dump_flags & TDF_DETAILS))
dump_tree_cfg (dump_file, dump_flags & ~TDF_DETAILS);
- /* Do some cleanups which reduce the amount of data the
- tree->rtl expanders deal with. */
- cfg_remove_useless_stmts ();
-
/* Flush out flow graph and SSA data. */
delete_var_map (map);
diff --git a/gcc/tree-pass.h b/gcc/tree-pass.h
index 557a3e5..ac1ab92 100644
--- a/gcc/tree-pass.h
+++ b/gcc/tree-pass.h
@@ -212,5 +212,6 @@ extern struct tree_opt_pass pass_store_ccp;
extern struct tree_opt_pass pass_store_copy_prop;
extern struct tree_opt_pass pass_vrp;
extern struct tree_opt_pass pass_create_structure_vars;
+extern struct tree_opt_pass pass_uncprop;
#endif /* GCC_TREE_PASS_H */
diff --git a/gcc/tree-ssa-uncprop.c b/gcc/tree-ssa-uncprop.c
new file mode 100644
index 0000000..4400ee7
--- /dev/null
+++ b/gcc/tree-ssa-uncprop.c
@@ -0,0 +1,614 @@
+/* Routines for discovering and unpropagating edge equivalences.
+ Copyright (C) 2005 Free Software Foundation, Inc.
+
+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 "tree.h"
+#include "flags.h"
+#include "rtl.h"
+#include "tm_p.h"
+#include "ggc.h"
+#include "basic-block.h"
+#include "output.h"
+#include "errors.h"
+#include "expr.h"
+#include "function.h"
+#include "diagnostic.h"
+#include "timevar.h"
+#include "tree-dump.h"
+#include "tree-flow.h"
+#include "domwalk.h"
+#include "real.h"
+#include "tree-pass.h"
+#include "tree-ssa-propagate.h"
+#include "langhooks.h"
+
+/* The basic structure describing an equivalency created by traversing
+ an edge. Traversing the edge effectively means that we can assume
+ that we've seen an assignment LHS = RHS. */
+struct edge_equivalency
+{
+ tree rhs;
+ tree lhs;
+};
+
+/* This routine finds and records edge equivalences for every edge
+ in the CFG.
+
+ When complete, each edge that creates an equivalency will have an
+ EDGE_EQUIVALENCY structure hanging off the edge's AUX field.
+ The caller is responsible for freeing the AUX fields. */
+
+static void
+associate_equivalences_with_edges (void)
+{
+ basic_block bb;
+
+ /* Walk over each block. If the block ends with a control statement,
+ then it might create a useful equivalence. */
+ FOR_EACH_BB (bb)
+ {
+ block_stmt_iterator bsi = bsi_last (bb);
+ tree stmt;
+
+ /* If the block does not end with a COND_EXPR or SWITCH_EXPR
+ then there is nothing to do. */
+ if (bsi_end_p (bsi))
+ continue;
+
+ stmt = bsi_stmt (bsi);
+
+ if (!stmt)
+ continue;
+
+ /* A COND_EXPR may create an equivalency in a variety of different
+ ways. */
+ if (TREE_CODE (stmt) == COND_EXPR)
+ {
+ tree cond = COND_EXPR_COND (stmt);
+ edge true_edge;
+ edge false_edge;
+ struct edge_equivalency *equivalency;
+
+ extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
+
+ /* If the conditional is a single variable 'X', record 'X = 1'
+ for the true edge and 'X = 0' on the false edge. */
+ if (TREE_CODE (cond) == SSA_NAME)
+ {
+ equivalency = xmalloc (sizeof (struct edge_equivalency));
+ equivalency->rhs = constant_boolean_node (1, TREE_TYPE (cond));
+ equivalency->lhs = cond;
+ true_edge->aux = equivalency;
+
+ equivalency = xmalloc (sizeof (struct edge_equivalency));
+ equivalency->rhs = constant_boolean_node (0, TREE_TYPE (cond));
+ equivalency->lhs = cond;
+ false_edge->aux = equivalency;
+ }
+ /* Equality tests may create one or two equivalences. */
+ else if (TREE_CODE (cond) == EQ_EXPR || TREE_CODE (cond) == NE_EXPR)
+ {
+ tree op0 = TREE_OPERAND (cond, 0);
+ tree op1 = TREE_OPERAND (cond, 1);
+
+ /* Special case comparing booleans against a constant as we
+ know the value of OP0 on both arms of the branch. i.e., we
+ can record an equivalence for OP0 rather than COND. */
+ if (TREE_CODE (op0) == SSA_NAME
+ && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
+ && is_gimple_min_invariant (op1))
+ {
+ if (TREE_CODE (cond) == EQ_EXPR)
+ {
+ equivalency = xmalloc (sizeof (struct edge_equivalency));
+ equivalency->lhs = op0;
+ equivalency->rhs = (integer_zerop (op1)
+ ? boolean_false_node
+ : boolean_true_node);
+ true_edge->aux = equivalency;
+
+ equivalency = xmalloc (sizeof (struct edge_equivalency));
+ equivalency->lhs = op0;
+ equivalency->rhs = (integer_zerop (op1)
+ ? boolean_true_node
+ : boolean_false_node);
+ false_edge->aux = equivalency;
+ }
+ else
+ {
+ equivalency = xmalloc (sizeof (struct edge_equivalency));
+ equivalency->lhs = op0;
+ equivalency->rhs = (integer_zerop (op1)
+ ? boolean_true_node
+ : boolean_false_node);
+ true_edge->aux = equivalency;
+
+ equivalency = xmalloc (sizeof (struct edge_equivalency));
+ equivalency->lhs = op0;
+ equivalency->rhs = (integer_zerop (op1)
+ ? boolean_false_node
+ : boolean_true_node);
+ false_edge->aux = equivalency;
+ }
+ }
+
+ if (TREE_CODE (op0) == SSA_NAME
+ && (is_gimple_min_invariant (op1)
+ || TREE_CODE (op1) == SSA_NAME))
+ {
+ /* For IEEE, -0.0 == 0.0, so we don't necessarily know
+ the sign of a variable compared against zero. If
+ we're honoring signed zeros, then we cannot record
+ this value unless we know that the value is nonzero. */
+ if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
+ && (TREE_CODE (op1) != REAL_CST
+ || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (op1))))
+ continue;
+
+ equivalency = xmalloc (sizeof (struct edge_equivalency));
+ equivalency->lhs = op0;
+ equivalency->rhs = op1;
+ if (TREE_CODE (cond) == EQ_EXPR)
+ true_edge->aux = equivalency;
+ else
+ false_edge->aux = equivalency;
+
+ }
+ }
+
+ /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
+ }
+
+ /* For a SWITCH_EXPR, a case label which represents a single
+ value and which is the only case label which reaches the
+ target block creates an equivalence. */
+ if (TREE_CODE (stmt) == SWITCH_EXPR)
+ {
+ tree cond = SWITCH_COND (stmt);
+
+ if (TREE_CODE (cond) == SSA_NAME)
+ {
+ tree labels = SWITCH_LABELS (stmt);
+ int i, n_labels = TREE_VEC_LENGTH (labels);
+ tree *info = xcalloc (n_basic_blocks, sizeof (tree));
+
+ /* Walk over the case label vector. Record blocks
+ which are reached by a single case label which represents
+ a single value. */
+ for (i = 0; i < n_labels; i++)
+ {
+ tree label = TREE_VEC_ELT (labels, i);
+ basic_block bb = label_to_block (CASE_LABEL (label));
+
+
+ if (CASE_HIGH (label)
+ || !CASE_LOW (label)
+ || info[bb->index])
+ info[bb->index] = error_mark_node;
+ else
+ info[bb->index] = label;
+ }
+
+ /* Now walk over the blocks to determine which ones were
+ marked as being reached by a useful case label. */
+ for (i = 0; i < n_basic_blocks; i++)
+ {
+ tree node = info[i];
+
+ if (node != NULL
+ && node != error_mark_node)
+ {
+ tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
+ struct edge_equivalency *equivalency;
+
+ /* Record an equivalency on the edge from BB to basic
+ block I. */
+ equivalency = xmalloc (sizeof (struct edge_equivalency));
+ equivalency->rhs = x;
+ equivalency->lhs = cond;
+ find_edge (bb, BASIC_BLOCK (i))->aux = equivalency;
+ }
+ }
+ free (info);
+ }
+ }
+
+ }
+}
+
+
+/* Translating out of SSA sometimes requires inserting copies and
+ constant initializations on edges to eliminate PHI nodes.
+
+ In some cases those copies and constant initializations are
+ redundant because the target already has the value on the
+ RHS of the assignment.
+
+ We previously tried to catch these cases after translating
+ out of SSA form. However, that code often missed cases. Worse
+ yet, the cases it missed were also often missed by the RTL
+ optimizers. Thus the resulting code had redundant instructions.
+
+ This pass attempts to detect these situations before translating
+ out of SSA form.
+
+ The key concept that this pass is built upon is that these
+ redundant copies and constant initializations often occur
+ due to constant/copy propagating equivalences resulting from
+ COND_EXPRs and SWITCH_EXPRs.
+
+ We want to do those propagations as they can sometimes allow
+ the SSA optimziers to do a better job. However, in the cases
+ where such propagations do not result in further optimization,
+ we would like to "undo" the propagation to avoid the redundant
+ copies and constant initializations.
+
+ This pass works by first associating equivalences with edges in
+ the CFG. For example, the edge leading from a SWITCH_EXPR to
+ its associated CASE_LABEL will have an equivalency between
+ SWITCH_COND and the value in the case label.
+
+ Once we have found the edge equivalences, we proceed to walk
+ the CFG in dominator order. As we traverse edges we record
+ equivalences associated with those edges we traverse.
+
+ When we encounter a PHI node, we walk its arguments to see if we
+ have an equivalence for the PHI argument. If so, then we replace
+ the argument.
+
+ Equivalences are looked up based on their value (think of it as
+ the RHS of an assignment). A value may be an SSA_NAME or an
+ invariant. We may have several SSA_NAMEs with the same value,
+ so with each value we have a list of SSA_NAMEs that have the
+ same value. */
+
+/* As we enter each block we record the value for any edge equivalency
+ leading to this block. If no such edge equivalency exists, then we
+ record NULL. These equivalences are live until we leave the dominator
+ subtree rooted at the block where we record the equivalency. */
+static varray_type equiv_stack;
+
+/* Global hash table implementing a mapping from invariant values
+ to a list of SSA_NAMEs which have the same value. We might be
+ able to reuse tree-vn for this code. */
+static htab_t equiv;
+
+/* Main structure for recording equivalences into our hash table. */
+struct equiv_hash_elt
+{
+ /* The value/key of this entry. */
+ tree value;
+
+ /* List of SSA_NAMEs which have the same value/key. */
+ varray_type equivalences;
+};
+
+static void uncprop_initialize_block (struct dom_walk_data *, basic_block);
+static void uncprop_finalize_block (struct dom_walk_data *, basic_block);
+static void uncprop_into_successor_phis (struct dom_walk_data *, basic_block);
+
+/* Hashing and equality routines for the hash table. */
+
+static hashval_t
+equiv_hash (const void *p)
+{
+ tree value = ((struct equiv_hash_elt *)p)->value;
+ return iterative_hash_expr (value, 0);
+}
+
+static int
+equiv_eq (const void *p1, const void *p2)
+{
+ tree value1 = ((struct equiv_hash_elt *)p1)->value;
+ tree value2 = ((struct equiv_hash_elt *)p2)->value;
+
+ return operand_equal_p (value1, value2, 0);
+}
+
+/* Remove the most recently recorded equivalency for VALUE. */
+
+static void
+remove_equivalence (tree value)
+{
+ struct equiv_hash_elt equiv_hash_elt, *equiv_hash_elt_p;
+ void **slot;
+
+ equiv_hash_elt.value = value;
+ equiv_hash_elt.equivalences = NULL;
+
+ slot = htab_find_slot (equiv, &equiv_hash_elt, NO_INSERT);
+
+ equiv_hash_elt_p = (struct equiv_hash_elt *) *slot;
+ VARRAY_POP (equiv_hash_elt_p->equivalences);
+}
+
+/* Record EQUIVALENCE = VALUE into our hash table. */
+
+static void
+record_equiv (tree value, tree equivalence)
+{
+ struct equiv_hash_elt *equiv_hash_elt;
+ void **slot;
+
+ equiv_hash_elt = xmalloc (sizeof (struct equiv_hash_elt));
+ equiv_hash_elt->value = value;
+ equiv_hash_elt->equivalences = NULL;
+
+ slot = htab_find_slot (equiv, equiv_hash_elt, INSERT);
+
+ if (*slot == NULL)
+ *slot = (void *) equiv_hash_elt;
+ else
+ free (equiv_hash_elt);
+
+ equiv_hash_elt = (struct equiv_hash_elt *) *slot;
+
+ if (!equiv_hash_elt->equivalences)
+ VARRAY_TREE_INIT (equiv_hash_elt->equivalences, 10, "value equivs");
+ VARRAY_PUSH_TREE (equiv_hash_elt->equivalences, equivalence);
+}
+
+/* Main driver for un-cprop. */
+
+static void
+tree_ssa_uncprop (void)
+{
+ struct dom_walk_data walk_data;
+ basic_block bb;
+
+ associate_equivalences_with_edges ();
+
+ /* Create our global data structures. */
+ equiv = htab_create (1024, equiv_hash, equiv_eq, free);
+ VARRAY_TREE_INIT (equiv_stack, 2, "Block equiv stack");
+
+ /* We're going to do a dominator walk, so ensure that we have
+ dominance information. */
+ calculate_dominance_info (CDI_DOMINATORS);
+
+ /* Setup callbacks for the generic dominator tree walker. */
+ walk_data.walk_stmts_backward = false;
+ walk_data.dom_direction = CDI_DOMINATORS;
+ walk_data.initialize_block_local_data = NULL;
+ walk_data.before_dom_children_before_stmts = uncprop_initialize_block;
+ walk_data.before_dom_children_walk_stmts = NULL;
+ walk_data.before_dom_children_after_stmts = uncprop_into_successor_phis;
+ walk_data.after_dom_children_before_stmts = NULL;
+ walk_data.after_dom_children_walk_stmts = NULL;
+ walk_data.after_dom_children_after_stmts = uncprop_finalize_block;
+ walk_data.global_data = NULL;
+ walk_data.block_local_data_size = 0;
+ walk_data.interesting_blocks = NULL;
+
+ /* Now initialize the dominator walker. */
+ init_walk_dominator_tree (&walk_data);
+
+ /* Recursively walk the dominator tree undoing unprofitable
+ constant/copy propagations. */
+ walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
+
+ /* Finalize and clean up. */
+ fini_walk_dominator_tree (&walk_data);
+
+ /* EQUIV_STACK should already be empty at this point, so we just need
+ to empty elements out of the hash table and cleanup the AUX field
+ on the edges. */
+ htab_delete (equiv);
+ FOR_EACH_BB (bb)
+ {
+ edge e;
+ edge_iterator ei;
+
+ FOR_EACH_EDGE (e, ei, bb->succs)
+ {
+ if (e->aux)
+ {
+ free (e->aux);
+ e->aux = NULL;
+ }
+ }
+ }
+
+}
+
+
+/* We have finished processing the dominator children of BB, perform
+ any finalization actions in preparation for leaving this node in
+ the dominator tree. */
+
+static void
+uncprop_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+ basic_block bb ATTRIBUTE_UNUSED)
+{
+ tree value = VARRAY_TOP_TREE (equiv_stack);
+
+ /* Pop the topmost value off the equiv stack. */
+ VARRAY_POP (equiv_stack);
+
+ /* If that value was non-null, then pop the topmost equivalency off
+ its equivalency stack. */
+ if (value != NULL)
+ remove_equivalence (value);
+}
+
+/* Unpropagate values from PHI nodes in successor blocks of BB. */
+
+static void
+uncprop_into_successor_phis (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+ basic_block bb)
+{
+ edge e;
+ edge_iterator ei;
+
+ /* For each successor edge, first temporarily record any equivalence
+ on that edge. Then unpropagate values in any PHI nodes at the
+ destination of the edge. Then remove the temporary equivalence. */
+ FOR_EACH_EDGE (e, ei, bb->succs)
+ {
+ tree phi = phi_nodes (e->dest);
+
+ /* If there are no PHI nodes in this destination, then there is
+ no sense in recording any equivalences. */
+ if (!phi)
+ continue;
+
+ /* Record any equivalency associated with E. */
+ if (e->aux)
+ {
+ struct edge_equivalency *equiv = e->aux;
+ record_equiv (equiv->rhs, equiv->lhs);
+ }
+
+ /* Walk over the PHI nodes, unpropagating values. */
+ for ( ; phi; phi = PHI_CHAIN (phi))
+ {
+ /* Sigh. We'll have more efficient access to this one day. */
+ tree arg = PHI_ARG_DEF (phi, e->dest_idx);
+ struct equiv_hash_elt equiv_hash_elt;
+ void **slot;
+
+ /* If the argument is not an invariant, or refers to the same
+ underlying variable as the PHI result, then there's no
+ point in un-propagating the argument. */
+ if (!is_gimple_min_invariant (arg)
+ && SSA_NAME_VAR (arg) != SSA_NAME_VAR (PHI_RESULT (phi)))
+ continue;
+
+ /* Lookup this argument's value in the hash table. */
+ equiv_hash_elt.value = arg;
+ equiv_hash_elt.equivalences = NULL;
+ slot = htab_find_slot (equiv, &equiv_hash_elt, NO_INSERT);
+
+ if (slot)
+ {
+ struct equiv_hash_elt *elt = *slot;
+ int j;
+
+ /* Walk every equivalence with the same value. If we find
+ one with the same underlying variable as the PHI result,
+ then replace the value in the argument with its equivalent
+ SSA_NAME. Use the most recent equivlance as hopefully
+ that results in shortest lifetimes. */
+ for (j = VARRAY_ACTIVE_SIZE (elt->equivalences) - 1; j >= 0; j--)
+ {
+ tree equiv = VARRAY_TREE (elt->equivalences, j);
+
+ if (SSA_NAME_VAR (equiv) == SSA_NAME_VAR (PHI_RESULT (phi)))
+ {
+ SET_PHI_ARG_DEF (phi, e->dest_idx, equiv);
+ break;
+ }
+ }
+ }
+ }
+
+ /* If we had an equivalence associated with this edge, remove it. */
+ if (e->aux)
+ {
+ struct edge_equivalency *equiv = e->aux;
+ remove_equivalence (equiv->rhs);
+ }
+ }
+}
+
+/* Ignoring loop backedges, if BB has precisely one incoming edge then
+ return that edge. Otherwise return NULL. */
+static edge
+single_incoming_edge_ignoring_loop_edges (basic_block bb)
+{
+ edge retval = NULL;
+ edge e;
+ edge_iterator ei;
+
+ FOR_EACH_EDGE (e, ei, bb->preds)
+ {
+ /* A loop back edge can be identified by the destination of
+ the edge dominating the source of the edge. */
+ if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
+ continue;
+
+ /* If we have already seen a non-loop edge, then we must have
+ multiple incoming non-loop edges and thus we return NULL. */
+ if (retval)
+ return NULL;
+
+ /* This is the first non-loop incoming edge we have found. Record
+ it. */
+ retval = e;
+ }
+
+ return retval;
+}
+
+static void
+uncprop_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
+ basic_block bb)
+{
+ basic_block parent;
+ edge e;
+ bool recorded = false;
+
+ /* If this block is dominated by a single incoming edge and that edge
+ has an equivalency, then record the equivalency and push the
+ VALUE onto EQUIV_STACK. Else push a NULL entry on EQUIV_STACK. */
+ parent = get_immediate_dominator (CDI_DOMINATORS, bb);
+ if (parent)
+ {
+ e = single_incoming_edge_ignoring_loop_edges (bb);
+
+ if (e && e->src == parent && e->aux)
+ {
+ struct edge_equivalency *equiv = e->aux;
+
+ record_equiv (equiv->rhs, equiv->lhs);
+ VARRAY_PUSH_TREE (equiv_stack, equiv->rhs);
+ recorded = true;
+ }
+ }
+
+ if (!recorded)
+ VARRAY_PUSH_TREE (equiv_stack, NULL_TREE);
+}
+
+static bool
+gate_uncprop (void)
+{
+ return flag_tree_dom != 0;
+}
+
+struct tree_opt_pass pass_uncprop =
+{
+ "uncprop", /* name */
+ gate_uncprop, /* gate */
+ tree_ssa_uncprop, /* execute */
+ NULL, /* sub */
+ NULL, /* next */
+ 0, /* static_pass_number */
+ TV_TREE_SSA_UNCPROP, /* tv_id */
+ PROP_cfg | PROP_ssa, /* properties_required */
+ 0, /* properties_provided */
+ 0, /* properties_destroyed */
+ 0, /* todo_flags_start */
+ TODO_dump_func | TODO_verify_ssa, /* todo_flags_finish */
+ 0 /* letter */
+};