diff --git a/gcc/match.pd b/gcc/match.pd index 87edf0e75c3d3f2edb736ae5cc54246c433c7a97..a05d4f07ab5b6f89fe09312765277df30aa8d8f2 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5156,6 +5156,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) ) ) +/* `(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 diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c b/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c new file mode 100644 index 0000000000000000000000000000000000000000..a96dbadf2b5c06eb64d710ab3c31ff45ff216e11 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr111431-1.c @@ -0,0 +1,39 @@ +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) + } +} + diff --git a/gcc/testsuite/gcc.dg/binop-andeq1.c b/gcc/testsuite/gcc.dg/binop-andeq1.c new file mode 100644 index 0000000000000000000000000000000000000000..2a92b8f95df8b2bcc544ee69d5958e743163dc2c --- /dev/null +++ b/gcc/testsuite/gcc.dg/binop-andeq1.c @@ -0,0 +1,12 @@ +/* { 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" } } */ diff --git a/gcc/testsuite/gcc.dg/binop-andeq2.c b/gcc/testsuite/gcc.dg/binop-andeq2.c new file mode 100644 index 0000000000000000000000000000000000000000..895262fc17e6aedbcd5eb1ea14864085b8da05e3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/binop-andeq2.c @@ -0,0 +1,14 @@ +/* { 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" } } */ diff --git a/gcc/testsuite/gcc.dg/binop-notand1a.c b/gcc/testsuite/gcc.dg/binop-notand1a.c index c7e932b26387e6037d3e4af8849be5cfa868ed55..d94685eb4ceb17bc6e9cd094f781443045dde26e 100644 --- a/gcc/testsuite/gcc.dg/binop-notand1a.c +++ b/gcc/testsuite/gcc.dg/binop-notand1a.c @@ -7,6 +7,4 @@ foo (char a, unsigned short b) 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" } } */ diff --git a/gcc/testsuite/gcc.dg/binop-notand4a.c b/gcc/testsuite/gcc.dg/binop-notand4a.c index dce6a5c7eb55fd0aea4aa47717336e88357cc567..bd9c7cce638d34b87263d34b4cd06672a62be16b 100644 --- a/gcc/testsuite/gcc.dg/binop-notand4a.c +++ b/gcc/testsuite/gcc.dg/binop-notand4a.c @@ -7,6 +7,4 @@ foo (unsigned char a, _Bool b) 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" } } */ diff --git a/gcc/testsuite/gcc.dg/binop-notand7.c b/gcc/testsuite/gcc.dg/binop-notand7.c new file mode 100644 index 0000000000000000000000000000000000000000..c2bb6a0449d3d8817fb63716ed31cbdbad1ac4a0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/binop-notand7.c @@ -0,0 +1,12 @@ +/* { 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" } } */ diff --git a/gcc/testsuite/gcc.dg/binop-notand7a.c b/gcc/testsuite/gcc.dg/binop-notand7a.c new file mode 100644 index 0000000000000000000000000000000000000000..dd50916da6162db12ef5883f236f137502a0b610 --- /dev/null +++ b/gcc/testsuite/gcc.dg/binop-notand7a.c @@ -0,0 +1,12 @@ +/* { 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" } } */