goto bit_rotate;
case BIT_AND_EXPR:
- /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
- if (TREE_CODE (arg0) == BIT_XOR_EXPR
- && INTEGRAL_TYPE_P (type)
- && integer_onep (TREE_OPERAND (arg0, 1))
- && integer_onep (arg1))
- {
- tree tem2;
- tem = TREE_OPERAND (arg0, 0);
- tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
- tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
- tem, tem2);
- return fold_build2_loc (loc, EQ_EXPR, type, tem2,
- build_zero_cst (TREE_TYPE (tem)));
- }
- /* Fold ~X & 1 as (X & 1) == 0. */
- if (TREE_CODE (arg0) == BIT_NOT_EXPR
- && INTEGRAL_TYPE_P (type)
- && integer_onep (arg1))
- {
- tree tem2;
- tem = TREE_OPERAND (arg0, 0);
- tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
- tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
- tem, tem2);
- return fold_build2_loc (loc, EQ_EXPR, type, tem2,
- build_zero_cst (TREE_TYPE (tem)));
- }
/* Fold !X & 1 as X == 0. */
if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
&& integer_onep (arg1))
(if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
(rcmp @0 @1))))
+/* (type)([0,1]@a != 0) -> (type)a
+ (type)([0,1]@a == 1) -> (type)a
+ (type)([0,1]@a == 0) -> a ^ 1
+ (type)([0,1]@a != 1) -> a ^ 1. */
+(for eqne (eq ne)
+ (simplify
+ (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
+ (if ((integer_zerop (@1) || integer_onep (@1)))
+ (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
+ (convert @0)
+ /* Only do this if the types match as (type)(a == 0) is
+ canonical form normally, while `a ^ 1` is canonical when
+ there is no type change. */
+ (if (types_match (type, TREE_TYPE (@0)))
+ (bit_xor @0 { build_one_cst (type); } ))))))
+
/* We can't reassociate at all for saturating types. */
(if (!TYPE_SATURATING (type))
+++ /dev/null
-/* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-original" } */
-int test1(int a)
-{
- return !(a & 1);
-}
-
-int test2(int b)
-{
- return (b & 1) == 0;
-}
-
-int test3(int c)
-{
- return (c & 1) ^ 1;
-}
-
-int test4(int d)
-{
- return (d ^ 1) & 1;
-}
-
-int test5(int e)
-{
- return ~e & 1;
-}
-
-/* { dg-final { scan-tree-dump-times "\\(a \& 1\\) == 0" 1 "original" } } */
-/* { dg-final { scan-tree-dump-times "\\(b \& 1\\) == 0" 1 "original" } } */
-/* { dg-final { scan-tree-dump-times "\\(c \& 1\\) == 0" 1 "original" } } */
-/* { dg-final { scan-tree-dump-times "\\(d \& 1\\) == 0" 1 "original" } } */
-/* { dg-final { scan-tree-dump-times "\\(e \& 1\\) == 0" 1 "original" } } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+int f(int a)
+{
+ int b = (a & 1)!=0;
+ return b;
+}
+
+/* This should be optimized to just return (a & 1); */
+/* { dg-final { scan-tree-dump-not " == " "optimized"} } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+int f(int a)
+{
+ int b = a & 1;
+ int c = b == 0;
+ return c;
+}
+
+/* This should be optimized to just return `(a&1) ^ 1` or `(~a) & 1`. */
+/* { dg-final { scan-tree-dump-not " == " "optimized"} } */
+/* { dg-final { scan-tree-dump-times "~a" 1 "optimized"} } */
+/* { dg-final { scan-tree-dump-times " & 1" 1 "optimized"} } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+int f(int a)
+{
+ int b = a & 1;
+ int c = b == 0;
+ int d = ~a;
+ int e = d & 1;
+ return c == e;
+}
+
+/* This should be optimized to just `return 1` */
+/* { dg-final { scan-tree-dump-not " == " "optimized"} } */
+/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized"} } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-phiopt" } */
+double
+foo() {
+ long n3 = 3450000, xtra = 7270;
+ long i,ix;
+ long j;
+ double Check;
+
+ /* Section 3, Conditional jumps */
+ j = 0;
+ {
+ for (ix=0; ix<xtra; ix++)
+ {
+ for(i=0; i<n3; i++)
+ {
+ if(j==1) j = 2;
+ else j = 3;
+ if(j>2) j = 0;
+ else j = 1;
+ if(j<1) j = 1;
+ else j = 0;
+ }
+ }
+ }
+ Check = Check + (double)j;
+ return Check;
+}
+
+/* the above if statements in loop should be optimized to just `j ^ 1`
+ and should not be (type)(j != 1). */
+/* { dg-final { scan-tree-dump-not " != 1" "phiopt2"} } */
+/* { dg-final { scan-tree-dump-times " \\^ 1" 1 "phiopt2"} } */
+
/* { dg-final { scan-tree-dump-times "Replaced .bufferstep_\[0-9\]+. with constant .1." 1 "dom3"} } */
/* And some assignments ought to fold down to constants. */
-/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 1;" 1 "dom3"} } */
-/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 0;" 1 "dom3"} } */
+/* { dg-final { scan-tree-dump-times "Folded to: (?:bufferstep)?_\[0-9\]+ = 1;" 1 "dom3"} } */
+/* { dg-final { scan-tree-dump-times "Folded to: (?:bufferstep)?_\[0-9\]+ = 0;" 1 "dom3"} } */
/* The XOR operations should have been optimized to constants. */
/* { dg-final { scan-tree-dump-not "bit_xor" "dom3"} } */
return (((ptr)[(bit)/(32 - 0)] & (((mp_limb_t) 1L) << ((bit)%(32 - 0)))) != 0);
}
-/* { dg-final { scan-assembler "bt\[ql\]" } } */
-/* { dg-final { scan-assembler "setc" } } */
+/* 32bit produces:
+ btl %eax, %edx
+ setc %al
+ movzbl %al, %eax
+ */
+/* { dg-final { scan-assembler "bt\[ql\]" { target { ! lp64 } } } } */
+/* { dg-final { scan-assembler "setc" { target { ! lp64 } } } } */
+
+/* 64bit produces:
+ shrq %cl, %rax
+ andl $1, %eax
+ */
+/* { dg-final { scan-assembler-times "shrq" 2 { target { lp64 } } } } */
+/* { dg-final { scan-assembler-times "andl" 2 { target { lp64 } } } } */