On Wed, Oct 11, 2023 at 2:46 AM Andrew Pinski <pins...@gmail.com> wrote: > > While `a & (b ^ ~a)` is optimized to `a & b` on the rtl level, > it is always good to optimize this at the gimple level and allows > us to match a few extra things including where a is a comparison. > > Note I had to update/change the testcase and-1.c to avoid matching > this case as we can match -2 and 1 as bitwise inversions.
OK. > PR tree-optimization/111282 > > gcc/ChangeLog: > > * match.pd (`a & ~(a ^ b)`, `a & (a == b)`, > `a & ((~a) ^ b)`): New patterns. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/and-1.c: Update testcase to avoid > matching `~1 & (a ^ 1)` simplification. > * gcc.dg/tree-ssa/bitops-6.c: New test. > --- > gcc/match.pd | 20 ++++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/and-1.c | 6 ++--- > gcc/testsuite/gcc.dg/tree-ssa/bitops-6.c | 33 ++++++++++++++++++++++++ > 3 files changed, 56 insertions(+), 3 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bitops-6.c > > diff --git a/gcc/match.pd b/gcc/match.pd > index 49740d189a7..26b05c157c1 100644 > --- a/gcc/match.pd > +++ b/gcc/match.pd > @@ -1358,6 +1358,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > && (!wascmp || element_precision (type) == 1)) > (bit_ior @0 (bit_not @2))))) > > +/* a & ~(a ^ b) --> a & b */ > +(simplify > + (bit_and:c @0 (bit_not (bit_xor:c @0 @1))) > + (bit_and @0 @1)) > + > +/* a & (a == b) --> a & b (boolean version of the above). */ > +(simplify > + (bit_and:c @0 (nop_convert? (eq:c @0 @1))) > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) > + && TYPE_PRECISION (TREE_TYPE (@0)) == 1) > + (bit_and @0 @1))) > + > +/* a & ((~a) ^ b) --> a & b (alt version of the above 2) */ > +(simplify > + (bit_and:c @0 (bit_xor:c @1 @2)) > + (with { bool wascmp; } > + (if (bitwise_inverted_equal_p (@0, @1, wascmp) > + && (!wascmp || element_precision (type) == 1)) > + (bit_and @0 @2)))) > + > /* (a | b) | (a &^ b) --> a | b */ > (for op (bit_and bit_xor) > (simplify > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/and-1.c > b/gcc/testsuite/gcc.dg/tree-ssa/and-1.c > index 276c2b9bd8a..27d38907eea 100644 > --- a/gcc/testsuite/gcc.dg/tree-ssa/and-1.c > +++ b/gcc/testsuite/gcc.dg/tree-ssa/and-1.c > @@ -2,10 +2,10 @@ > /* { dg-options "-O -fdump-tree-optimized-raw" } */ > > int f(int in) { > - in = in | 3; > - in = in ^ 1; > + in = in | 7; > + in = in ^ 3; > in = (in & ~(unsigned long)1); > return in; > } > > -/* { dg-final { scan-tree-dump-not "bit_and_expr" "optimized" } } */ > +/* { dg-final { scan-tree-dump-not "bit_and_expr, " "optimized" } } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bitops-6.c > b/gcc/testsuite/gcc.dg/tree-ssa/bitops-6.c > new file mode 100644 > index 00000000000..e6ab2fd6c71 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/bitops-6.c > @@ -0,0 +1,33 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */ > +/* PR tree-optimization/111282 */ > + > + > +int f(int a, int b) > +{ > + return a & (b ^ ~a); // a & b > +} > + > +_Bool fb(_Bool x, _Bool y) > +{ > + return x & (y ^ !x); // x & y > +} > + > +int fa(int w, int z) > +{ > + return (~w) & (w ^ z); // ~w & z > +} > + > +int fcmp(int x, int y) > +{ > + _Bool a = x == 2; > + _Bool b = y == 1; > + return a & (b ^ !a); // (x == 2) & (y == 1) > +} > + > +/* { dg-final { scan-tree-dump-not "bit_xor_expr, " "optimized" } } */ > +/* { dg-final { scan-tree-dump-times "bit_and_expr, " 4 "optimized" } } */ > +/* { dg-final { scan-tree-dump-times "bit_not_expr, " 1 "optimized" } } */ > +/* { dg-final { scan-tree-dump-not "ne_expr, " "optimized" } } */ > +/* { dg-final { scan-tree-dump-times "eq_expr, " 2 "optimized" } } */ > + > -- > 2.39.3 >