During the review of the first PR115547 patterns [1] Jeff mentioned that
this zero_one pattern:

(X ^ Y) & (Z | X | Y) -> X ^ Y

can be also applied for non-zero_one values.  He's correct.  In the
original patch this pattern was mixed with the NE variant, which is
zero_one restricted, and we ended up missing out on this one.

Split the existing (X NE|BIT_XOR Y) pattern in two: one (X ^ Y) pattern
for all values and another (X NE Y) pattern that is zero_one restricted.

Bootstrapped and regression tested in x86_64.

[1] https://gcc.gnu.org/pipermail/gcc-patches/2026-November/737058.html

gcc/ChangeLog:

        * match.pd(`(X ^ Y) & (Z | X | Y)`): New pattern.
        (`(X NE Y) & (Z | X | Y)`): New pattern.

gcc/testsuite/ChangeLog:

        * gcc.dg/tree-ssa/pr115547.c: add "(X ^ Y) & (Z | X | Y)"
        non-zero_one testcases.
---
 gcc/match.pd                             | 20 +++++++++++---------
 gcc/testsuite/gcc.dg/tree-ssa/pr115547.c | 10 +++++++++-
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 3e206704fc6..ab30fa99aa5 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -7278,18 +7278,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (ne @X @Y))
 
 /* PR115547:
-   (X ^ Y) & (Z | X | Y) -> (X ^ Y)
+   (X ^ Y) & (Z | X | Y) -> (X ^ Y).  */
+(simplify
+ (bit_and:c (bit_xor:c@0 @X @Y) (bit_ior:c @Y (bit_ior:c @Z @X)))
+  @0)
+
+/* PR115547:
    (X NE Y) & (Z | X | Y) -> (X NE Y)
 
    For X, Y and Z being booleans/zero_one values.  */
-(for op (bit_xor ne)
- (simplify
-  (bit_and:c
-    (op:c@0 zero_one_valued_p@X zero_one_valued_p@Y)
-    (bit_ior:c @Y (bit_ior:c zero_one_valued_p@Z @X)))
-   (if (op == BIT_XOR_EXPR)
-    @0
-   (ne @X @Y))))
+(simplify
+ (bit_and:c
+   (ne:c@0 zero_one_valued_p@X zero_one_valued_p@Y)
+   (bit_ior:c @Y (bit_ior:c zero_one_valued_p@Z @X)))
+  (ne @X @Y))
 
 (simplify
  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr115547.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr115547.c
index 3f8367d67f4..1f9a13dd72d 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr115547.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr115547.c
@@ -25,7 +25,15 @@ _Bool fn2_alt4 (_Bool a, _Bool b, _Bool c) {
     return (a != c) & (b | a | c);
 }
 
-/* { dg-final { scan-tree-dump-times " \\\^ " 3 "optimized" } } */
+int fn2_alt5 (int a, int b, int c) {
+    return (a ^ c) & (b | c | a);
+}
+
+int fn2_alt6 (int a, int b, int c) {
+    return (a ^ c) & (b | a | c);
+}
+
+/* { dg-final { scan-tree-dump-times " \\\^ " 5 "optimized" } } */
 /* { dg-final { scan-tree-dump-times " != " 3 "optimized" } } */
 /* { dg-final { scan-tree-dump-times " \\\| " 0 "optimized" } } */
 /* { dg-final { scan-tree-dump-times " & " 0 "optimized" } } */
-- 
2.43.0

Reply via email to