This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[Committed] PR middle-end/23673: Fold (X^Y) == 0 as X == Y
- From: Roger Sayle <roger at eyesopen dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sat, 25 Feb 2006 15:27:04 -0700 (MST)
- Subject: [Committed] PR middle-end/23673: Fold (X^Y) == 0 as X == Y
The following pair of patches resolve PR middle-end/23673, by implementing
a number of transformations of equality/inequality comparisons against an
XOR operand, at both the tree and RTL levels.
Both patches have been tested independently on x86_64-unknown-linux-gnu
with a full "make bootstrap", all default languages, and regression
tested with a top-level "make -k check" with no new failures.
Committed to mainline as revisions 111442 and 111443.
2006-02-25 Roger Sayle <roger@eyesopen.com>
PR middle-end/23673
* fold-const.c (fold_binary) <EQ_EXPR>: Fold (X^Y) == 0 as X == Y
and (X^Y) != 0 as X != Y. Fold (X^Y) == Y as X == 0, and some
symmetry related transformations. Fold (X^C1) == C2 as
X == (C1^C2).
* gcc.dg/fold-eqxor-1.c: New test case.
* gcc.dg/fold-eqxor-2.c: Likewise.
* gcc.dg/fold-eqxor-3.c: Likewise.
Index: fold-const.c
===================================================================
*** fold-const.c (revision 111423)
--- fold-const.c (working copy)
*************** fold_binary (enum tree_code code, tree t
*** 9767,9772 ****
--- 9767,9798 ----
}
}
+ /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
+ if (integer_zerop (arg1)
+ && TREE_CODE (arg0) == BIT_XOR_EXPR)
+ return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
+ TREE_OPERAND (arg0, 1));
+
+ /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
+ if (TREE_CODE (arg0) == BIT_XOR_EXPR
+ && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
+ return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
+ build_int_cst (TREE_TYPE (arg1), 0));
+ /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
+ if (TREE_CODE (arg0) == BIT_XOR_EXPR
+ && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
+ && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
+ return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
+ build_int_cst (TREE_TYPE (arg1), 0));
+
+ /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
+ if (TREE_CODE (arg0) == BIT_XOR_EXPR
+ && TREE_CODE (arg1) == INTEGER_CST
+ && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
+ return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
+ fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
+ TREE_OPERAND (arg0, 1), arg1));
+
if (integer_zerop (arg1)
&& tree_expr_nonzero_p (arg0))
{
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-original" } */
int test1(int a, int b)
{
return (a ^ b) == 0;
}
int test2(int c, int d)
{
return (c ^ d) != 0;
}
unsigned int test3(unsigned int e, unsigned int f)
{
return (e ^ f) == 0;
}
unsigned int test4(unsigned int g, unsigned int h)
{
return (g ^ h) != 0;
}
/* { dg-final { scan-tree-dump-times "a == b" 1 "original" } } */
/* { dg-final { scan-tree-dump-times "c != d" 1 "original" } } */
/* { dg-final { scan-tree-dump-times "e == f" 1 "original" } } */
/* { dg-final { scan-tree-dump-times "g != h" 1 "original" } } */
/* { dg-final { cleanup-tree-dump "original" } } */
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-original" } */
int test1(int a, int b)
{
return (a ^ b) == a;
}
int test2(int c, int d)
{
return (c ^ d) != c;
}
int test3(int e, int f)
{
return (e ^ f) == f;
}
int test4(int g, int h)
{
return (g ^ h) != h;
}
/* { dg-final { scan-tree-dump-times "b == 0" 1 "original" } } */
/* { dg-final { scan-tree-dump-times "d != 0" 1 "original" } } */
/* { dg-final { scan-tree-dump-times "e == 0" 1 "original" } } */
/* { dg-final { scan-tree-dump-times "g != 0" 1 "original" } } */
/* { dg-final { cleanup-tree-dump "original" } } */
/* { dg-do compile } */
/* { dg-options "-O2 -fdump-tree-original" } */
int test1(int a, int b)
{
return (a ^ 2) == 2;
}
int test2(int c, int d)
{
return (c ^ 4) != 4;
}
int test3(int e, int f)
{
return (e ^ 2) == 6;
}
int test4(int g, int h)
{
return (g ^ 6) != 4;
}
/* { dg-final { scan-tree-dump-times "a == 0" 1 "original" } } */
/* { dg-final { scan-tree-dump-times "c != 0" 1 "original" } } */
/* { dg-final { scan-tree-dump-times "e == 4" 1 "original" } } */
/* { dg-final { scan-tree-dump-times "g != 2" 1 "original" } } */
/* { dg-final { cleanup-tree-dump "original" } } */
2006-02-25 Roger Sayle <roger@eyesopen.com>
* simplify-rtx.c (simplify_relational_operation_1): Simplify
(X^Y) == 0 as X == Y and (X^Y) != 0 as X != Y. Simplify (X^Y) == Y
as X == 0, and some symmetry related transformations.
Simplify (X^C1) == C2 as X == (C1^C2). Split long comment line.
Index: simplify-rtx.c
===================================================================
*** simplify-rtx.c (revision 111423)
--- simplify-rtx.c (working copy)
*************** simplify_relational_operation_1 (enum rt
*** 3512,3518 ****
{
if (INTVAL (op1) == 0 && COMPARISON_P (op0))
{
! /* If op0 is a comparison, extract the comparison arguments form it. */
if (code == NE)
{
if (GET_MODE (op0) == mode)
--- 3512,3519 ----
{
if (INTVAL (op1) == 0 && COMPARISON_P (op0))
{
! /* If op0 is a comparison, extract the comparison arguments
! from it. */
if (code == NE)
{
if (GET_MODE (op0) == mode)
*************** simplify_relational_operation_1 (enum rt
*** 3561,3566 ****
--- 3562,3598 ----
? simplify_gen_unary (ZERO_EXTEND, mode, op0, cmp_mode)
: lowpart_subreg (mode, op0, cmp_mode);
+ /* (eq/ne (xor x y) 0) simplifies to (eq/ne x y). */
+ if ((code == EQ || code == NE)
+ && op1 == const0_rtx
+ && op0code == XOR)
+ return simplify_gen_relational (code, mode, cmp_mode,
+ XEXP (op0, 0), XEXP (op0, 1));
+
+ /* (eq/ne (xor x y) x) simplifies to (eq/ne x 0). */
+ if ((code == EQ || code == NE)
+ && op0code == XOR
+ && rtx_equal_p (XEXP (op0, 0), op1)
+ && !side_effects_p (XEXP (op0, 1)))
+ return simplify_gen_relational (code, mode, cmp_mode, op1, const0_rtx);
+ /* Likewise (eq/ne (xor x y) y) simplifies to (eq/ne y 0). */
+ if ((code == EQ || code == NE)
+ && op0code == XOR
+ && rtx_equal_p (XEXP (op0, 1), op1)
+ && !side_effects_p (XEXP (op0, 0)))
+ return simplify_gen_relational (code, mode, cmp_mode, op1, const0_rtx);
+
+ /* (eq/ne (xor x C1) C2) simplifies to (eq/ne x (C1^C2)). */
+ if ((code == EQ || code == NE)
+ && op0code == XOR
+ && (GET_CODE (op1) == CONST_INT
+ || GET_CODE (op1) == CONST_DOUBLE)
+ && (GET_CODE (XEXP (op0, 1)) == CONST_INT
+ || GET_CODE (XEXP (op0, 1)) == CONST_DOUBLE))
+ return simplify_gen_relational (code, mode, cmp_mode, XEXP (op0, 0),
+ simplify_gen_binary (XOR, cmp_mode,
+ XEXP (op0, 1), op1));
+
return NULL_RTX;
}
Roger
--