]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match.pd: x&c ?(x op c):(x|c) -> x^c [PR122615, PR122616]
authorDaniel Barboza <dbarboza@ventanamicro.com>
Fri, 28 Nov 2025 01:03:18 +0000 (22:03 -0300)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Fri, 5 Dec 2025 03:32:30 +0000 (19:32 -0800)
Add a single pattern to reduce these patterns to "x ^ c":

x & c ? (x - c) | (x | c)
x & c ? (x & ~c) | (x | c)

As long as "c" has a single bit set.

        PR tree-optimization/122615
        PR tree-optimization/122616

gcc/ChangeLog:

* match.pd (`x & c ? (x - c) | (x | c)`): New pattern.
(`x & c ? (x & ~c) | (x | c)`): Likewise.

gcc/testsuite/ChangeLog:

* gcc.dg/torture/pr122615.c: New test.
* gcc.dg/torture/pr122616.c: Likewise.

Co-authored-by: Jeff Law <jlaw@ventanamicro.com>
Signed-off-by: Daniel Barboza <dbarboza@ventanamicro.com>
gcc/match.pd
gcc/testsuite/gcc.dg/torture/pr122615.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/torture/pr122616.c [new file with mode: 0644]

index f164ec59100812c1c6aecd1007891c377077632e..bf410a75f5f858d236f5ab933f34ef7500997e6d 100644 (file)
@@ -6617,6 +6617,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        (convert (minus { arg; } (convert:type1 @0))))))))))
 #endif
 
+/* X & C1 ? (X + -C1) : (X | C1) -> X ^ C1
+   X & C1 ? (X & ~C1) : (X | C1) -> X ^ C1
+   when C1 has a single bit set.  */
+(for op (plus bit_and)
+ (simplify
+  (cond (ne (bit_and @0 INTEGER_CST@1) integer_zerop)
+            (op @0 INTEGER_CST@2) (bit_ior @0 @1))
+   (with {
+     auto c1 = wi::to_wide (@1);
+     auto c2 = wi::to_wide (@2);
+    }
+    (if (wi::popcount (c1) == 1
+         && ((op == PLUS_EXPR && wi::eq_p (wi::neg (c2), c1))
+              || (op == BIT_AND_EXPR && wi::eq_p (wi::bit_not (c2), c1))))
+         (bit_xor @0 @1)))))
+
 (simplify
  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
  (if (INTEGRAL_TYPE_P (type)
diff --git a/gcc/testsuite/gcc.dg/torture/pr122615.c b/gcc/testsuite/gcc.dg/torture/pr122615.c
new file mode 100644 (file)
index 0000000..9f4f3c4
--- /dev/null
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-original" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" } { "" } } */
+
+int f1 (int x)
+{
+  return x & 1 ? (x & ~1) : (x | 1);
+}
+
+int f2 (int x)
+{
+  return x & 2 ? (x & ~2) : (x | 2);
+}
+
+int f3 (int x)
+{
+  return x & 3 ? (x & ~3) : (x | 3);
+}
+
+/* { dg-final { scan-tree-dump-times "x \\^ 1" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "x \\^ 2" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "x \\^ 3" 0 "original" } } */
diff --git a/gcc/testsuite/gcc.dg/torture/pr122616.c b/gcc/testsuite/gcc.dg/torture/pr122616.c
new file mode 100644 (file)
index 0000000..77d364e
--- /dev/null
@@ -0,0 +1,22 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-original" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" } { "" } } */
+
+int f1 (int x)
+{
+  return x & 1 ? (x - 1) : (x | 1);
+}
+
+int f2 (int x)
+{
+  return x & 2 ? (x - 2) : (x | 2);
+}
+
+int f3 (int x)
+{
+  return x & 3 ? (x - 3) : (x | 3);
+}
+
+/* { dg-final { scan-tree-dump-times "x \\^ 1" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "x \\^ 2" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "x \\^ 3" 0 "original" } } */