Commit e9b5e15f by Richard Kenner

(fold, truth_andor): Don't apply distributive law when it would change evaluation order.

(fold, truth_andor): Don't apply distributive law when it would change
evaluation order.
Clean up code a bit to improve flow.

From-SVN: r6381
parent 212ac20c
......@@ -4121,23 +4121,15 @@ fold (expr)
return omit_one_operand (type, arg1, arg0);
truth_andor:
/* Check for the possibility of merging component references. If our
lhs is another similar operation, try to merge its rhs with our
rhs. Then try to merge our lhs and rhs. */
if (optimize)
{
if (TREE_CODE (arg0) == code)
{
tem = fold_truthop (code, type,
TREE_OPERAND (arg0, 1), arg1);
if (tem)
return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
}
/* We only do these simplifications if we are optimizing. */
if (!optimize)
return t;
/* Check for things like (A || B) && (A || C). We can convert
this to A || (B && C). Note that either operator can be any of
the four truth and/or operations and the transformation will
still be valid. */
/* Check for things like (A || B) && (A || C). We can convert this
to A || (B && C). Note that either operator can be any of the four
truth and/or operations and the transformation will still be
valid. Also note that we only care about order for the
ANDIF and ORIF operators. */
if (TREE_CODE (arg0) == TREE_CODE (arg1)
&& (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
|| TREE_CODE (arg0) == TRUTH_ORIF_EXPR
......@@ -4148,26 +4140,42 @@ fold (expr)
tree a01 = TREE_OPERAND (arg0, 1);
tree a10 = TREE_OPERAND (arg1, 0);
tree a11 = TREE_OPERAND (arg1, 1);
tree common = 0, op0, op1;
int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
|| TREE_CODE (arg0) == TRUTH_AND_EXPR)
&& (code == TRUTH_AND_EXPR
|| code == TRUTH_OR_EXPR));
if (operand_equal_p (a00, a10, 0))
common = a00, op0 = a01, op1 = a11;
else if (operand_equal_p (a00, a11, 0))
common = a00, op0 = a01, op1 = a10;
else if (operand_equal_p (a01, a10, 0))
common = a01, op0 = a00, op1 = a11;
else if (operand_equal_p (a01, a11, 0))
common = a01, op0 = a00, op1 = a10;
if (common)
return fold (build (TREE_CODE (arg0), type, common,
fold (build (code, type, op0, op1))));
return fold (build (TREE_CODE (arg0), type, a00,
fold (build (code, type, a01, a11))));
else if (commutative && operand_equal_p (a00, a11, 0))
return fold (build (TREE_CODE (arg0), type, a00,
fold (build (code, type, a01, a10))));
else if (commutative && operand_equal_p (a01, a10, 0))
return fold (build (TREE_CODE (arg0), type, a01,
fold (build (code, type, a00, a11))));
/* This case if tricky because we must either have commutative
operators or else A10 must not have side-effects. */
else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
&& operand_equal_p (a01, a11, 0))
return fold (build (TREE_CODE (arg0), type,
fold (build (code, type, a00, a10)),
a01));
}
tem = fold_truthop (code, type, arg0, arg1);
if (tem)
/* Check for the possibility of merging component references. If our
lhs is another similar operation, try to merge its rhs with our
rhs. Then try to merge our lhs and rhs. */
if (TREE_CODE (arg0) == code
&& 0 != (tem = fold_truthop (code, type,
TREE_OPERAND (arg0, 1), arg1)))
return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
return tem;
}
return t;
case TRUTH_ORIF_EXPR:
......
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