)
)
+/* `(a == CST) & a` can be simplified to `0` or `(a == CST)` depending
+ on the first bit of the CST. */
+(simplify
+ (bit_and:c (convert@2 (eq @0 INTEGER_CST@1)) (convert? @0))
+ (if ((wi::to_wide (@1) & 1) != 0)
+ @2
+ { build_zero_cst (type); }))
+
/* Optimize
# x_5 in range [cst1, cst2] where cst2 = cst1 + 1
x_5 ? cstN ? cst4 : cst3
--- /dev/null
+int
+foo (int a)
+{
+ int b = a == 0;
+ return (a & b);
+}
+
+#define function(vol,cst) \
+__attribute__((noipa)) \
+_Bool func_##cst##_##vol(vol int a) \
+{ \
+ vol int b = a == cst; \
+ return (a & b); \
+}
+
+#define funcdefs(cst) \
+function(,cst) \
+function(volatile,cst)
+
+#define funcs(f) \
+f(0) \
+f(1) \
+f(5)
+
+funcs(funcdefs)
+
+#define test(cst) \
+do { \
+ if(func_##cst##_(a) != func_##cst##_volatile(a))\
+ __builtin_abort(); \
+} while(0);
+int main(void)
+{
+ for(int a = -10; a <= 10; a++)
+ {
+ funcs(test)
+ }
+}
+
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/111431 */
+
+int
+foo (int a)
+{
+ int b = a == 2;
+ return (a & b);
+}
+
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/111431 */
+
+int
+foo (int a)
+{
+ int b = a == 1025;
+ return (a & b);
+}
+
+/* { dg-final { scan-tree-dump-not "return 0" "optimized" } } */
+/* { dg-final { scan-tree-dump-not " & " "optimized" } } */
+/* { dg-final { scan-tree-dump-times " == 1025;" 1 "optimized" } } */
return (a & !a) | (b & !b);
}
-/* As long as comparisons aren't boolified and casts from boolean-types
- aren't preserved, the folding of X & !X to zero fails. */
-/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
return (!a & a) | (b & !b);
}
-/* As long as comparisons aren't boolified and casts from boolean-types
- aren't preserved, the folding of X & !X to zero fails. */
-/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/111431 */
+
+int
+foo (int a)
+{
+ int b = !a;
+ return (a & b);
+}
+
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* PR tree-optimization/111431 */
+
+unsigned
+foo (int a)
+{
+ int b = !a;
+ return (a & b);
+}
+
+/* { dg-final { scan-tree-dump-times "return 0" 1 "optimized" } } */