public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-4561] MATCH: [PR111282] Simplify `a & (b ^ ~a)` to `a & b`
@ 2023-10-11 16:46 Andrew Pinski
  0 siblings, 0 replies; only message in thread
From: Andrew Pinski @ 2023-10-11 16:46 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:e8d418df3dc609f27487deece796d4aa69004b8c

commit r14-4561-ge8d418df3dc609f27487deece796d4aa69004b8c
Author: Andrew Pinski <pinskia@gmail.com>
Date:   Tue Oct 10 12:45:56 2023 -0700

    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.

Diff:
---
 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(-)

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" } } */
+

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-10-11 16:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-11 16:46 [gcc r14-4561] MATCH: [PR111282] Simplify `a & (b ^ ~a)` to `a & b` Andrew Pinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).