mirror of
https://gcc.gnu.org/git/gcc.git
synced 2024-12-06 02:04:03 +08:00
fold-const.c (fold_binary): Fold (~X & C) eq/ne 0 as (X & C) ne/eq 0, where C is a single bit, i.e.
* fold-const.c (fold_binary) <EQ_EXPR>: Fold (~X & C) eq/ne 0 as (X & C) ne/eq 0, where C is a single bit, i.e. a power of two. Fold both "((X & C) ^ C) eq/ne 0" and "((X ^ C) & C) eq/ne 0" as (X & C) ne/eq 0. * gcc.dg/fold-eqandnot-1.c: New test case. From-SVN: r111471
This commit is contained in:
parent
53f1b560db
commit
5881ad5d9d
@ -1,3 +1,10 @@
|
||||
2006-02-26 Roger Sayle <roger@eyesopen.com>
|
||||
|
||||
* fold-const.c (fold_binary) <EQ_EXPR>: Fold (~X & C) eq/ne 0 as
|
||||
(X & C) ne/eq 0, where C is a single bit, i.e. a power of two.
|
||||
Fold both "((X & C) ^ C) eq/ne 0" and "((X ^ C) & C) eq/ne 0"
|
||||
as (X & C) ne/eq 0.
|
||||
|
||||
2006-02-26 Roger Sayle <roger@eyesopen.com>
|
||||
|
||||
PR middle-end/19983
|
||||
|
@ -9868,6 +9868,50 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
|
||||
fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
|
||||
TREE_OPERAND (arg0, 1), arg1));
|
||||
|
||||
/* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
|
||||
(X & C) == 0 when C is a single bit. */
|
||||
if (TREE_CODE (arg0) == BIT_AND_EXPR
|
||||
&& TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
|
||||
&& integer_zerop (arg1)
|
||||
&& integer_pow2p (TREE_OPERAND (arg0, 1)))
|
||||
{
|
||||
tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
|
||||
TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
|
||||
TREE_OPERAND (arg0, 1));
|
||||
return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
|
||||
type, tem, arg1);
|
||||
}
|
||||
|
||||
/* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
|
||||
constant C is a power of two, i.e. a single bit. */
|
||||
if (TREE_CODE (arg0) == BIT_XOR_EXPR
|
||||
&& TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
|
||||
&& integer_zerop (arg1)
|
||||
&& integer_pow2p (TREE_OPERAND (arg0, 1))
|
||||
&& operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
|
||||
TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
|
||||
{
|
||||
tree arg00 = TREE_OPERAND (arg0, 0);
|
||||
return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
|
||||
arg00, build_int_cst (TREE_TYPE (arg00), 0));
|
||||
}
|
||||
|
||||
/* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
|
||||
when is C is a power of two, i.e. a single bit. */
|
||||
if (TREE_CODE (arg0) == BIT_AND_EXPR
|
||||
&& TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
|
||||
&& integer_zerop (arg1)
|
||||
&& integer_pow2p (TREE_OPERAND (arg0, 1))
|
||||
&& operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
|
||||
TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
|
||||
{
|
||||
tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
|
||||
tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
|
||||
arg000, TREE_OPERAND (arg0, 1));
|
||||
return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
|
||||
tem, build_int_cst (TREE_TYPE (tem), 0));
|
||||
}
|
||||
|
||||
if (integer_zerop (arg1)
|
||||
&& tree_expr_nonzero_p (arg0))
|
||||
{
|
||||
|
@ -1,3 +1,7 @@
|
||||
2006-02-26 Roger Sayle <roger@eyesopen.com>
|
||||
|
||||
* gcc.dg/fold-eqandnot-1.c: New test case.
|
||||
|
||||
2006-02-26 Roger Sayle <roger@eyesopen.com>
|
||||
|
||||
PR middle-end/19983
|
||||
|
33
gcc/testsuite/gcc.dg/fold-eqandnot-1.c
Normal file
33
gcc/testsuite/gcc.dg/fold-eqandnot-1.c
Normal file
@ -0,0 +1,33 @@
|
||||
/* { dg-do compile } */
|
||||
/* { dg-options "-O2 -fdump-tree-original" } */
|
||||
int test1(int a)
|
||||
{
|
||||
return !(a & 4);
|
||||
}
|
||||
|
||||
int test2(int b)
|
||||
{
|
||||
return (b & 4) == 0;
|
||||
}
|
||||
|
||||
int test3(int c)
|
||||
{
|
||||
return ((c & 4) ^ 4) != 0;
|
||||
}
|
||||
|
||||
int test4(int d)
|
||||
{
|
||||
return ((d ^ 4) & 4) != 0;
|
||||
}
|
||||
|
||||
int test5(int e)
|
||||
{
|
||||
return (~e & 4) != 0;
|
||||
}
|
||||
|
||||
/* { dg-final { scan-tree-dump-times "\\(a \& 4\\) == 0" 1 "original" } } */
|
||||
/* { dg-final { scan-tree-dump-times "\\(b \& 4\\) == 0" 1 "original" } } */
|
||||
/* { dg-final { scan-tree-dump-times "\\(c \& 4\\) == 0" 1 "original" } } */
|
||||
/* { dg-final { scan-tree-dump-times "\\(d \& 4\\) == 0" 1 "original" } } */
|
||||
/* { dg-final { scan-tree-dump-times "\\(e \& 4\\) == 0" 1 "original" } } */
|
||||
/* { dg-final { cleanup-tree-dump "original" } } */
|
Loading…
Reference in New Issue
Block a user