Commit 80c4ed35 by Andrew Pinski Committed by Andrew Pinski

re PR tree-optimization/17671 (PHI-OPT is not smart enough)

2005-03-06  Andrew Pinski  <pinskia@physics.uc.edu>

	PR tree-opt/17671
	* tree-ssa-phiopt.c (tree_ssa_phiopt): Rewrite so we base the
	bbs on the COND_EXPR instead of the PHI_NODEs.
	(candidate_bb_for_phi_optimization): Remove.
	(replace_phi_with_stmt): Rename to ...
	(replace_phi_edge_with_variable): this and change so that we
	replace the phi argument instead of removing the PHI.
	(conditional_replacement): Change so we deal with PHI with more
	than two arguments.
	(value_replacement): Likewise.
	(abs_replacement): Likewise.

2005-03-06  Andrew Pinski  <pinskia@physics.uc.edu>

	PR tree-opt/17671
	* gcc.dg/tree-ssa/phi-opt-[1-4].c: New tests.

From-SVN: r96067
parent 64022b5d
2005-03-06 Andrew Pinski <pinskia@physics.uc.edu>
PR tree-opt/17671
* tree-ssa-phiopt.c (tree_ssa_phiopt): Rewrite so we base the
bbs on the COND_EXPR instead of the PHI_NODEs.
(candidate_bb_for_phi_optimization): Remove.
(replace_phi_with_stmt): Rename to ...
(replace_phi_edge_with_variable): this and change so that we
replace the phi argument instead of removing the PHI.
(conditional_replacement): Change so we deal with PHI with more
than two arguments.
(value_replacement): Likewise.
(abs_replacement): Likewise.
2005-03-07 Aldy Hernandez <aldyh@redhat.com> 2005-03-07 Aldy Hernandez <aldyh@redhat.com>
* config/rs6000/rs6000-protos.h: Rename output_e500_flip_eq_bit to * config/rs6000/rs6000-protos.h: Rename output_e500_flip_eq_bit to
......
2005-03-06 Andrew Pinski <pinskia@physics.uc.edu>
PR tree-opt/17671
* gcc.dg/tree-ssa/phi-opt-[1-4].c: New tests.
2005-03-07 Mark Mitchell <mark@codesourcery.com> 2005-03-07 Mark Mitchell <mark@codesourcery.com>
* g++.dg/warn/Wnvdtor.C: New test. * g++.dg/warn/Wnvdtor.C: New test.
......
/* { dg-do compile } */
/* { dg-options "-O1 -fdump-tree-optimized" } */
int f(int a, int b, int c)
{
if (c == 0) goto temp;
if (a == 0)
return 0;
temp:
if (a == b)
return a;
return a;
}
/* There should be no ifs as the PHI arguments, we did not
optimize this before because PHI-OPT did not look at
PHIs which have more than two arguments. */
/* { dg-final { scan-tree-dump-times "if" 0 "optimized"} } */
/* { dg-do compile } */
/* { dg-options "-O1 -fdump-tree-optimized" } */
_Bool f1(_Bool a, _Bool b)
{
if (a)
{
if (b)
return 1;
else
return 0;
}
return 0;
}
/* There should be only one if, the outer one; the inner one
should have been changed to straight line code with the
value of b (except that we don't fold ! (b != 0) into b
which can be fixed in a different patch). */
/* { dg-final { scan-tree-dump-times "if" 1 "optimized"} } */
/* { dg-do compile } */
/* { dg-options "-O1 -fdump-tree-optimized" } */
int f(int a, int b)
{
if (a == 0)
return 0;
if (a != b)
return a;
return a;
}
/* There should be no ifs as the PHI arguments, we did not
optimize this before because PHI-OPT did not look at
PHIs which have more than two arguments. */
/* { dg-final { scan-tree-dump-times "if" 0 "optimized"} } */
/* { dg-do compile } */
/* { dg-options "-O1 -fdump-tree-optimized" } */
_Bool t();
_Bool t1();
_Bool f1()
{
return t() && t1();
}
/* There should be only one if, the outer one; the inner one
should have been changed to straight line code with the
value of b (except that we don't fold ! (b != 0) into b
which means that we miss a sib calling opportunity). */
/* { dg-final { scan-tree-dump-times "if " 1 "optimized"} } */
...@@ -37,14 +37,14 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA ...@@ -37,14 +37,14 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA
#include "langhooks.h" #include "langhooks.h"
static void tree_ssa_phiopt (void); static void tree_ssa_phiopt (void);
static bool conditional_replacement (basic_block, tree, tree, tree); static bool conditional_replacement (basic_block, basic_block, basic_block,
static bool value_replacement (basic_block, tree, tree, tree); edge, edge, tree, tree, tree);
static bool abs_replacement (basic_block, tree, tree, tree); static bool value_replacement (basic_block, basic_block, basic_block,
static void replace_phi_with_stmt (block_stmt_iterator, basic_block, edge, edge, tree, tree, tree);
basic_block, tree, tree); static bool abs_replacement (basic_block, basic_block, basic_block,
static bool candidate_bb_for_phi_optimization (basic_block, edge, edge, tree, tree, tree);
basic_block *, static void replace_phi_edge_with_variable (basic_block, basic_block, edge,
basic_block *); tree, tree);
/* This pass eliminates PHI nodes which can be trivially implemented as /* This pass eliminates PHI nodes which can be trivially implemented as
an assignment from a conditional expression. i.e. if we have something an assignment from a conditional expression. i.e. if we have something
...@@ -112,24 +112,85 @@ tree_ssa_phiopt (void) ...@@ -112,24 +112,85 @@ tree_ssa_phiopt (void)
basic_block bb; basic_block bb;
bool removed_phis = false; bool removed_phis = false;
/* Search every basic block for PHI nodes we may be able to optimize. */ /* Search every basic block for COND_EXPR we may be able to optimize in reverse
FOR_EACH_BB (bb) order so we can find more. */
FOR_EACH_BB_REVERSE (bb)
{ {
tree arg0, arg1, phi; tree cond_expr;
tree phi;
basic_block bb1, bb2;
edge e1, e2;
cond_expr = last_stmt (bb);
/* Check to see if the last statement is a COND_EXPR */
if (!cond_expr
|| TREE_CODE (cond_expr) != COND_EXPR)
continue;
e1 = EDGE_SUCC (bb, 0);
bb1 = e1->dest;
e2 = EDGE_SUCC (bb, 1);
bb2 = e2->dest;
/* We cannot do the optimization on abnormal edges. */
if ((e1->flags & EDGE_ABNORMAL) != 0
|| (e2->flags & EDGE_ABNORMAL) != 0)
continue;
/* If either bb1's succ or bb2 or bb2's succ is non NULL. */
if (EDGE_COUNT (bb1->succs) < 1
|| bb2 == NULL
|| EDGE_COUNT (bb2->succs) < 1)
continue;
/* Find the bb which is the fall through to the other. */
if (EDGE_SUCC (bb1, 0)->dest == bb2)
;
else if (EDGE_SUCC (bb2, 0)->dest == bb1)
{
basic_block bb_tmp = bb1;
edge e_tmp = e1;
bb1 = bb2;
bb2 = bb_tmp;
e1 = e2;
e2 = e_tmp;
}
else
continue;
e1 = EDGE_SUCC (bb1, 0);
/* Make sure that bb1 is just a fall through. */
if (EDGE_COUNT (bb1->succs) > 1
|| (e1->flags & EDGE_FALLTHRU) == 0)
continue;
/* Also make that bb1 only have one pred and it is bb. */
if (EDGE_COUNT (bb1->preds) > 1
|| EDGE_PRED (bb1, 0)->src != bb)
continue;
phi = phi_nodes (bb2);
/* We're searching for blocks with one PHI node which has two /* Check to make sure that there is only one PHI node.
arguments. */ TODO: we could do it with more than one iff the other PHI nodes
phi = phi_nodes (bb); have the same elements for these two edges. */
if (phi && PHI_CHAIN (phi) == NULL if (phi && PHI_CHAIN (phi) == NULL)
&& PHI_NUM_ARGS (phi) == 2)
{ {
arg0 = PHI_ARG_DEF (phi, 0); tree arg0 = NULL, arg1 = NULL;
arg1 = PHI_ARG_DEF (phi, 1); int i;
arg0 = PHI_ARG_DEF_TREE (phi, e1->dest_idx);
arg1 = PHI_ARG_DEF_TREE (phi, e2->dest_idx);
/* We know something is wrong if we cannot find the edges in the PHI
node. */
gcc_assert (arg0 != NULL && arg1 != NULL);
/* Do the replacement of conditional if it can be done. */ /* Do the replacement of conditional if it can be done. */
if (conditional_replacement (bb, phi, arg0, arg1) if (conditional_replacement (bb, bb1, bb2, e1, e2, phi, arg0, arg1)
|| value_replacement (bb, phi, arg0, arg1) || value_replacement (bb, bb1, bb2, e1, e2, phi, arg0, arg1)
|| abs_replacement (bb, phi, arg0, arg1)) || abs_replacement (bb, bb1, bb2, e1, e2, phi, arg0, arg1))
{ {
/* We have done the replacement so we need to rebuild the /* We have done the replacement so we need to rebuild the
cfg when this pass is complete. */ cfg when this pass is complete. */
...@@ -159,89 +220,21 @@ empty_block_p (basic_block bb) ...@@ -159,89 +220,21 @@ empty_block_p (basic_block bb)
return true; return true;
} }
/* BB is a basic block which has only one PHI node with precisely two /* Replace PHI node element whoes edge is E in block BB with variable NEW.
arguments. Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
Examine both of BB's predecessors to see if one ends with a
COND_EXPR and the other is a successor of the COND_EXPR. If so, then
we may be able to optimize PHI nodes at the start of BB.
If so, mark store the block with the COND_EXPR into COND_BLOCK_P
and the other block into OTHER_BLOCK_P and return true, otherwise
return false. */
static bool
candidate_bb_for_phi_optimization (basic_block bb,
basic_block *cond_block_p,
basic_block *other_block_p)
{
tree last0, last1;
basic_block cond_block, other_block;
/* One of the alternatives must come from a block ending with
a COND_EXPR. */
last0 = last_stmt (EDGE_PRED (bb, 0)->src);
last1 = last_stmt (EDGE_PRED (bb, 1)->src);
if (last0 && TREE_CODE (last0) == COND_EXPR)
{
cond_block = EDGE_PRED (bb, 0)->src;
other_block = EDGE_PRED (bb, 1)->src;
}
else if (last1 && TREE_CODE (last1) == COND_EXPR)
{
other_block = EDGE_PRED (bb, 0)->src;
cond_block = EDGE_PRED (bb, 1)->src;
}
else
return false;
/* COND_BLOCK must have precisely two successors. We indirectly
verify that those successors are BB and OTHER_BLOCK. */
if (EDGE_COUNT (cond_block->succs) != 2
|| (EDGE_SUCC (cond_block, 0)->flags & EDGE_ABNORMAL) != 0
|| (EDGE_SUCC (cond_block, 1)->flags & EDGE_ABNORMAL) != 0)
return false;
/* OTHER_BLOCK must have a single predecessor which is COND_BLOCK,
OTHER_BLOCK must have a single successor which is BB and
OTHER_BLOCK must have no PHI nodes. */
if (EDGE_COUNT (other_block->preds) != 1
|| EDGE_PRED (other_block, 0)->src != cond_block
|| EDGE_COUNT (other_block->succs) != 1
|| EDGE_SUCC (other_block, 0)->dest != bb
|| phi_nodes (other_block))
return false;
*cond_block_p = cond_block;
*other_block_p = other_block;
/* Everything looks OK. */
return true;
}
/* Replace PHI in block BB with statement NEW. NEW is inserted after
BSI. Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
is known to have two edges, one of which must reach BB). */ is known to have two edges, one of which must reach BB). */
static void static void
replace_phi_with_stmt (block_stmt_iterator bsi, basic_block bb, replace_phi_edge_with_variable (basic_block cond_block, basic_block bb,
basic_block cond_block, tree phi, tree new) edge e, tree phi, tree new)
{ {
basic_block block_to_remove; basic_block block_to_remove;
int i;
block_stmt_iterator bsi;
/* Change the PHI argument to new. */
PHI_ARG_DEF_TREE (phi, e->dest_idx) = new;
/* Insert our new statement at the head of our block. */
bsi_insert_after (&bsi, new, BSI_NEW_STMT);
/* Register our new statement as the defining statement for
the result. */
SSA_NAME_DEF_STMT (PHI_RESULT (phi)) = new;
/* Remove the now useless PHI node.
We do not want to use remove_phi_node since that releases the
SSA_NAME as well and the SSA_NAME is still being used. */
release_phi_node (phi);
bb_ann (bb)->phi_nodes = NULL;
/* Remove the empty basic block. */ /* Remove the empty basic block. */
if (EDGE_SUCC (cond_block, 0)->dest == bb) if (EDGE_SUCC (cond_block, 0)->dest == bb)
{ {
...@@ -278,16 +271,17 @@ replace_phi_with_stmt (block_stmt_iterator bsi, basic_block bb, ...@@ -278,16 +271,17 @@ replace_phi_with_stmt (block_stmt_iterator bsi, basic_block bb,
is argument 0 from PHI. Likewise for ARG1. */ is argument 0 from PHI. Likewise for ARG1. */
static bool static bool
conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1) conditional_replacement (basic_block cond_bb, basic_block middle_bb,
basic_block phi_bb, edge e0, edge e1, tree phi,
tree arg0, tree arg1)
{ {
tree result; tree result;
tree old_result = NULL; tree old_result = NULL;
basic_block other_block = NULL;
basic_block cond_block = NULL;
tree new, cond; tree new, cond;
block_stmt_iterator bsi; block_stmt_iterator bsi;
edge true_edge, false_edge; edge true_edge, false_edge;
tree new_var = NULL; tree new_var = NULL;
tree new_var1;
/* The PHI arguments have the constants 0 and 1, then convert /* The PHI arguments have the constants 0 and 1, then convert
it to the conditional. */ it to the conditional. */
...@@ -297,8 +291,7 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -297,8 +291,7 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
else else
return false; return false;
if (!candidate_bb_for_phi_optimization (bb, &cond_block, &other_block) if (!empty_block_p (middle_bb))
|| !empty_block_p (other_block))
return false; return false;
/* If the condition is not a naked SSA_NAME and its type does not /* If the condition is not a naked SSA_NAME and its type does not
...@@ -306,7 +299,7 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -306,7 +299,7 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
variable to optimize this case as it would likely create variable to optimize this case as it would likely create
non-gimple code when the condition was converted to the non-gimple code when the condition was converted to the
result's type. */ result's type. */
cond = COND_EXPR_COND (last_stmt (cond_block)); cond = COND_EXPR_COND (last_stmt (cond_bb));
result = PHI_RESULT (phi); result = PHI_RESULT (phi);
if (TREE_CODE (cond) != SSA_NAME if (TREE_CODE (cond) != SSA_NAME
&& !lang_hooks.types_compatible_p (TREE_TYPE (cond), TREE_TYPE (result))) && !lang_hooks.types_compatible_p (TREE_TYPE (cond), TREE_TYPE (result)))
...@@ -324,10 +317,12 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -324,10 +317,12 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
/* We need to know which is the true edge and which is the false /* We need to know which is the true edge and which is the false
edge so that we know when to invert the condition below. */ edge so that we know when to invert the condition below. */
extract_true_false_edges_from_block (cond_block, &true_edge, &false_edge); extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
/* Insert our new statement at the head of our block. */ /* Insert our new statement at the end of condtional block before the
bsi = bsi_after_labels (bb); COND_EXPR. */
bsi = bsi_last (cond_bb);
bsi_insert_before (&bsi, build_empty_stmt (), BSI_NEW_STMT);
if (old_result) if (old_result)
{ {
...@@ -342,6 +337,9 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -342,6 +337,9 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
new1 = build (MODIFY_EXPR, TREE_TYPE (old_result), new_var, new1); new1 = build (MODIFY_EXPR, TREE_TYPE (old_result), new_var, new1);
bsi_insert_after (&bsi, new1, BSI_NEW_STMT); bsi_insert_after (&bsi, new1, BSI_NEW_STMT);
} }
new_var1 = duplicate_ssa_name (PHI_RESULT (phi), NULL);
/* At this point we know we have a COND_EXPR with two successors. /* At this point we know we have a COND_EXPR with two successors.
One successor is BB, the other successor is an empty block which One successor is BB, the other successor is an empty block which
...@@ -360,13 +358,12 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -360,13 +358,12 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
false edge as the value zero. Note that those conditions are not false edge as the value zero. Note that those conditions are not
the same since only one of the outgoing edges from the COND_EXPR the same since only one of the outgoing edges from the COND_EXPR
will directly reach BB and thus be associated with an argument. */ will directly reach BB and thus be associated with an argument. */
if ((PHI_ARG_EDGE (phi, 0) == true_edge && integer_onep (arg0)) if ((e0 == true_edge && integer_onep (arg0))
|| (PHI_ARG_EDGE (phi, 0) == false_edge && integer_zerop (arg0)) || (e0 == false_edge && integer_zerop (arg0))
|| (PHI_ARG_EDGE (phi, 1) == true_edge && integer_onep (arg1)) || (e1 == true_edge && integer_onep (arg1))
|| (PHI_ARG_EDGE (phi, 1) == false_edge && integer_zerop (arg1))) || (e1 == false_edge && integer_zerop (arg1)))
{ {
new = build (MODIFY_EXPR, TREE_TYPE (PHI_RESULT (phi)), new = build (MODIFY_EXPR, TREE_TYPE (new_var1), new_var1, cond);
PHI_RESULT (phi), cond);
} }
else else
{ {
...@@ -376,7 +373,10 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -376,7 +373,10 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
/* If what we get back is a conditional expression, there is no /* If what we get back is a conditional expression, there is no
way that it can be gimple. */ way that it can be gimple. */
if (TREE_CODE (cond) == COND_EXPR) if (TREE_CODE (cond) == COND_EXPR)
return false; {
release_ssa_name (new_var1);
return false;
}
/* If what we get back is not gimple try to create it as gimple by /* If what we get back is not gimple try to create it as gimple by
using a temporary variable. */ using a temporary variable. */
...@@ -392,13 +392,19 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -392,13 +392,19 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
if (TREE_CODE (cond) == TRUTH_NOT_EXPR if (TREE_CODE (cond) == TRUTH_NOT_EXPR
&& !is_gimple_val (TREE_OPERAND (cond, 0))) && !is_gimple_val (TREE_OPERAND (cond, 0)))
return false; {
release_ssa_name (new_var1);
return false;
}
new = build (MODIFY_EXPR, TREE_TYPE (PHI_RESULT (phi)), new = build (MODIFY_EXPR, TREE_TYPE (new_var1), new_var1, cond);
PHI_RESULT (phi), cond);
} }
replace_phi_with_stmt (bsi, bb, cond_block, phi, new); bsi_insert_after (&bsi, new, BSI_NEW_STMT);
SSA_NAME_DEF_STMT (new_var1) = new;
replace_phi_edge_with_variable (cond_bb, phi_bb, e1, phi, new_var1);
/* Note that we optimized this PHI. */ /* Note that we optimized this PHI. */
return true; return true;
...@@ -411,12 +417,12 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -411,12 +417,12 @@ conditional_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
is argument 0 from the PHI. Likewise for ARG1. */ is argument 0 from the PHI. Likewise for ARG1. */
static bool static bool
value_replacement (basic_block bb, tree phi, tree arg0, tree arg1) value_replacement (basic_block cond_bb, basic_block middle_bb,
basic_block phi_bb, edge e0, edge e1, tree phi,
tree arg0, tree arg1)
{ {
tree result; tree result;
basic_block other_block = NULL; tree cond;
basic_block cond_block = NULL;
tree new, cond;
edge true_edge, false_edge; edge true_edge, false_edge;
/* If the type says honor signed zeros we cannot do this /* If the type says honor signed zeros we cannot do this
...@@ -424,11 +430,10 @@ value_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -424,11 +430,10 @@ value_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1)))) if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
return false; return false;
if (!candidate_bb_for_phi_optimization (bb, &cond_block, &other_block) if (!empty_block_p (middle_bb))
|| !empty_block_p (other_block))
return false; return false;
cond = COND_EXPR_COND (last_stmt (cond_block)); cond = COND_EXPR_COND (last_stmt (cond_bb));
result = PHI_RESULT (phi); result = PHI_RESULT (phi);
/* This transformation is only valid for equality comparisons. */ /* This transformation is only valid for equality comparisons. */
...@@ -437,7 +442,7 @@ value_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -437,7 +442,7 @@ value_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
/* We need to know which is the true edge and which is the false /* We need to know which is the true edge and which is the false
edge so that we know if have abs or negative abs. */ edge so that we know if have abs or negative abs. */
extract_true_false_edges_from_block (cond_block, &true_edge, &false_edge); extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
/* At this point we know we have a COND_EXPR with two successors. /* At this point we know we have a COND_EXPR with two successors.
One successor is BB, the other successor is an empty block which One successor is BB, the other successor is an empty block which
...@@ -467,20 +472,17 @@ value_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -467,20 +472,17 @@ value_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
OTHER_BLOCK). If that is the case, then we want the single outgoing OTHER_BLOCK). If that is the case, then we want the single outgoing
edge from OTHER_BLOCK which reaches BB and represents the desired edge from OTHER_BLOCK which reaches BB and represents the desired
path from COND_BLOCK. */ path from COND_BLOCK. */
if (e->dest == other_block) if (e->dest == middle_bb)
e = EDGE_SUCC (e->dest, 0); e = EDGE_SUCC (e->dest, 0);
/* Now we know the incoming edge to BB that has the argument for the /* Now we know the incoming edge to BB that has the argument for the
RHS of our new assignment statement. */ RHS of our new assignment statement. */
if (PHI_ARG_EDGE (phi, 0) == e) if (e0 == e)
arg = arg0; arg = arg0;
else else
arg = arg1; arg = arg1;
/* Build the new assignment. */ replace_phi_edge_with_variable (cond_bb, phi_bb, e1, phi, arg);
new = build (MODIFY_EXPR, TREE_TYPE (result), result, arg);
replace_phi_with_stmt (bsi_after_labels (bb), bb, cond_block, phi, new);
/* Note that we optimized this PHI. */ /* Note that we optimized this PHI. */
return true; return true;
...@@ -492,13 +494,14 @@ value_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -492,13 +494,14 @@ value_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
replacement. Return true if the replacement is done. Otherwise return replacement. Return true if the replacement is done. Otherwise return
false. false.
bb is the basic block where the replacement is going to be done on. arg0 bb is the basic block where the replacement is going to be done on. arg0
is argument 0 from the phi. Likewise for arg1. */ is argument 0 from the phi. Likewise for arg1. */
static bool static bool
abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1) abs_replacement (basic_block cond_bb, basic_block middle_bb,
basic_block phi_bb, edge e0 ATTRIBUTE_UNUSED, edge e1, tree phi,
tree arg0, tree arg1)
{ {
tree result; tree result;
basic_block other_block = NULL;
basic_block cond_block = NULL;
tree new, cond; tree new, cond;
block_stmt_iterator bsi; block_stmt_iterator bsi;
edge true_edge, false_edge; edge true_edge, false_edge;
...@@ -513,12 +516,9 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -513,12 +516,9 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1)))) if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
return false; return false;
if (!candidate_bb_for_phi_optimization (bb, &cond_block, &other_block))
return false;
/* OTHER_BLOCK must have only one executable statement which must have the /* OTHER_BLOCK must have only one executable statement which must have the
form arg0 = -arg1 or arg1 = -arg0. */ form arg0 = -arg1 or arg1 = -arg0. */
bsi = bsi_start (other_block); bsi = bsi_start (middle_bb);
while (!bsi_end_p (bsi)) while (!bsi_end_p (bsi))
{ {
tree stmt = bsi_stmt (bsi); tree stmt = bsi_stmt (bsi);
...@@ -570,7 +570,7 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -570,7 +570,7 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
if (assign == NULL) if (assign == NULL)
return false; return false;
cond = COND_EXPR_COND (last_stmt (cond_block)); cond = COND_EXPR_COND (last_stmt (cond_bb));
result = PHI_RESULT (phi); result = PHI_RESULT (phi);
/* Only relationals comparing arg[01] against zero are interesting. */ /* Only relationals comparing arg[01] against zero are interesting. */
...@@ -592,7 +592,7 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -592,7 +592,7 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
/* We need to know which is the true edge and which is the false /* We need to know which is the true edge and which is the false
edge so that we know if have abs or negative abs. */ edge so that we know if have abs or negative abs. */
extract_true_false_edges_from_block (cond_block, &true_edge, &false_edge); extract_true_false_edges_from_block (cond_bb, &true_edge, &false_edge);
/* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
will need to negate the result. Similarly for LT_EXPR/LE_EXPR if will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
...@@ -602,10 +602,12 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -602,10 +602,12 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
else else
e = false_edge; e = false_edge;
if (e->dest == other_block) if (e->dest == middle_bb)
negate = true; negate = true;
else else
negate = false; negate = false;
result = duplicate_ssa_name (result, NULL);
if (negate) if (negate)
lhs = make_rename_temp (TREE_TYPE (result), NULL); lhs = make_rename_temp (TREE_TYPE (result), NULL);
...@@ -616,7 +618,8 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -616,7 +618,8 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
new = build (MODIFY_EXPR, TREE_TYPE (lhs), new = build (MODIFY_EXPR, TREE_TYPE (lhs),
lhs, build1 (ABS_EXPR, TREE_TYPE (lhs), rhs)); lhs, build1 (ABS_EXPR, TREE_TYPE (lhs), rhs));
replace_phi_with_stmt (bsi_after_labels (bb), bb, cond_block, phi, new); bsi = bsi_last (cond_bb);
bsi_insert_before (&bsi, new, BSI_NEW_STMT);
if (negate) if (negate)
{ {
...@@ -624,18 +627,15 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1) ...@@ -624,18 +627,15 @@ abs_replacement (basic_block bb, tree phi, tree arg0, tree arg1)
/* Get the right BSI. We want to insert after the recently /* Get the right BSI. We want to insert after the recently
added ABS_EXPR statement (which we know is the first statement added ABS_EXPR statement (which we know is the first statement
in the block. */ in the block. */
bsi = bsi_start (bb);
bsi_next (&bsi);
new = build (MODIFY_EXPR, TREE_TYPE (result), new = build (MODIFY_EXPR, TREE_TYPE (result),
result, build1 (NEGATE_EXPR, TREE_TYPE (lhs), lhs)); result, build1 (NEGATE_EXPR, TREE_TYPE (lhs), lhs));
bsi_insert_after (&bsi, new, BSI_NEW_STMT); bsi_insert_after (&bsi, new, BSI_NEW_STMT);
/* Register the new statement as defining the temporary -- this is
normally done by replace_phi_with_stmt, but the link will be wrong
if we had to negate the resulting value. */
SSA_NAME_DEF_STMT (result) = new;
} }
SSA_NAME_DEF_STMT (result) = new;
replace_phi_edge_with_variable (cond_bb, phi_bb, e1, phi, result);
/* Note that we optimized this PHI. */ /* Note that we optimized this PHI. */
return true; return true;
...@@ -665,7 +665,7 @@ struct tree_opt_pass pass_phiopt = ...@@ -665,7 +665,7 @@ struct tree_opt_pass pass_phiopt =
0, /* todo_flags_start */ 0, /* todo_flags_start */
TODO_cleanup_cfg | TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */ TODO_cleanup_cfg | TODO_dump_func | TODO_ggc_collect /* todo_flags_finish */
| TODO_verify_ssa | TODO_rename_vars | TODO_verify_ssa | TODO_rename_vars
| TODO_verify_flow, | TODO_verify_flow | TODO_verify_stmts,
0 /* letter */ 0 /* letter */
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment