diff options
author | Andrew Pinski <pinskia@gmail.com> | 2023-10-10 12:45:56 -0700 |
---|---|---|
committer | Andrew Pinski <pinskia@gmail.com> | 2023-10-11 09:45:51 -0700 |
commit | e8d418df3dc609f27487deece796d4aa69004b8c (patch) | |
tree | 80b4a620828e3e420688d03e3e6fea9ff4acf1e7 /gcc | |
parent | acfca27eaf4960069f7a49039f1407b956669ec1 (diff) | |
download | gcc-e8d418df3dc609f27487deece796d4aa69004b8c.zip gcc-e8d418df3dc609f27487deece796d4aa69004b8c.tar.gz gcc-e8d418df3dc609f27487deece796d4aa69004b8c.tar.bz2 |
MATCH: [PR111282] Simplify `a & (b ^ ~a)` to `a & b`
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.
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/match.pd | 20 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/and-1.c | 6 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/tree-ssa/bitops-6.c | 33 |
3 files changed, 56 insertions, 3 deletions
diff --git a/gcc/match.pd b/gcc/match.pd index 49740d1..26b05c1 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 276c2b9..27d3890 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 0000000..e6ab2fd --- /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" } } */ + |