aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorRoger Sayle <roger@eyesopen.com>2006-02-26 15:36:52 +0000
committerRoger Sayle <sayle@gcc.gnu.org>2006-02-26 15:36:52 +0000
commita861485caf6698fe171b1d80a78f3f73091615eb (patch)
tree54fbe907a418470a9df428739064819f26fcfdfd /gcc
parentcd683f6da56b08d6cde14e25cd453ea0cc88521f (diff)
downloadgcc-a861485caf6698fe171b1d80a78f3f73091615eb.zip
gcc-a861485caf6698fe171b1d80a78f3f73091615eb.tar.gz
gcc-a861485caf6698fe171b1d80a78f3f73091615eb.tar.bz2
re PR tree-optimization/21137 (Convert (a >> 2) & 1 != 0 into a & 4 != 0)
2006-02-26 Roger Sayle <roger@eyesopen.com> James A. Morrison <phython@gcc.gnu.org> PR middle-end/21137 * fold-const.c (fold_binary) <EQ_EXPR>: Fold ((X>>C1)&C2) eq/ne 0, when C2 is a power of two, as either (X&(C2<<C1)) eq/ne 0 if the new constant C2<<C1, or as (X<0) or (X,false) depending upon the signedness of the shift operation. * gcc.dg/fold-eqandshift-1.c: New test case. Co-Authored-By: James A. Morrison <phython@gcc.gnu.org> From-SVN: r111453
Diffstat (limited to 'gcc')
-rw-r--r--gcc/ChangeLog9
-rw-r--r--gcc/fold-const.c46
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/gcc.dg/fold-eqandshift-1.c46
4 files changed, 106 insertions, 0 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 84c278d..ead872d 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,12 @@
+2006-02-26 Roger Sayle <roger@eyesopen.com>
+ James A. Morrison <phython@gcc.gnu.org>
+
+ PR middle-end/21137
+ * fold-const.c (fold_binary) <EQ_EXPR>: Fold ((X>>C1)&C2) eq/ne 0,
+ when C2 is a power of two, as either (X&(C2<<C1)) eq/ne 0 if the
+ new constant C2<<C1, or as (X<0) or (X,false) depending upon the
+ signedness of the shift operation.
+
2006-02-26 Dorit Nuzman <dorit@il.ibm.com>
PR tree-optimization/26359
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index 3c3852b..7eb4d91 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -9653,6 +9653,52 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
fold_convert (newtype, arg1));
}
+ /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
+ C1 is a valid shift constant, and C2 is a power of two, i.e.
+ a single bit. */
+ if (TREE_CODE (arg0) == BIT_AND_EXPR
+ && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
+ && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
+ == INTEGER_CST
+ && integer_pow2p (TREE_OPERAND (arg0, 1))
+ && integer_zerop (arg1))
+ {
+ tree itype = TREE_TYPE (arg0);
+ unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
+ tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
+
+ /* Check for a valid shift count. */
+ if (TREE_INT_CST_HIGH (arg001) == 0
+ && TREE_INT_CST_LOW (arg001) < prec)
+ {
+ tree arg01 = TREE_OPERAND (arg0, 1);
+ tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
+ unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
+ /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
+ can be rewritten as (X & (C2 << C1)) != 0. */
+ if ((log2 + TREE_INT_CST_LOW (arg01)) < prec)
+ {
+ tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
+ tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
+ return fold_build2 (code, type, tem, arg1);
+ }
+ /* Otherwise, for signed (arithmetic) shifts,
+ ((X >> C1) & C2) != 0 is rewritten as X < 0, and
+ ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
+ else if (!TYPE_UNSIGNED (itype))
+ return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
+ arg000, build_int_cst (itype, 0));
+ /* Otherwise, of unsigned (logical) shifts,
+ ((X >> C1) & C2) != 0 is rewritten as (X,false), and
+ ((X >> C1) & C2) == 0 is rewritten as (X,true). */
+ else
+ return omit_one_operand (type,
+ code == EQ_EXPR ? integer_one_node
+ : integer_zero_node,
+ arg000);
+ }
+ }
+
/* If this is an NE comparison of zero with an AND of one, remove the
comparison since the AND will give the correct value. */
if (code == NE_EXPR
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 3040961..deb3000 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2006-02-26 Roger Sayle <roger@eyesopen.com>
+
+ PR middle-end/21137
+ * gcc.dg/fold-eqandshift-1.c: New test case.
+
2006-02-26 Dorit Nuzman <dorit@il.ibm.com>
PR tree-optimization/25125
diff --git a/gcc/testsuite/gcc.dg/fold-eqandshift-1.c b/gcc/testsuite/gcc.dg/fold-eqandshift-1.c
new file mode 100644
index 0000000..6de7116
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fold-eqandshift-1.c
@@ -0,0 +1,46 @@
+/* PR middle-end/21137 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+extern void foo();
+
+void test1(int a)
+{
+ if ((a >> 3) & 1)
+ foo ();
+}
+
+void test2(int b)
+{
+ if ((b >> 3) & 4)
+ foo ();
+}
+
+int test3(int c)
+{
+ return (c >> 3) & 1;
+}
+
+int test4(int d)
+{
+ return (d >> 3) & 4;
+}
+
+#if 0
+void test5(int e)
+{
+ if ((e >> 31) & 64)
+ foo();
+}
+
+void test6(unsigned int f)
+{
+ if ((f >> 31) & 64)
+ foo();
+}
+#endif
+
+/* { dg-final { scan-tree-dump-times "\\(a \& 8\\) != 0" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "\\(b \& 32\\) != 0" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "c >> 3 \& 1" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "d >> 3 \& 4" 1 "original" } } */
+/* { dg-final { cleanup-tree-dump "original" } } */