https://gcc.gnu.org/g:59afbbd3cd6b4348e9badc972eb3f46c2cdd023d

commit r17-5945-g59afbbd3cd6b4348e9badc972eb3f46c2cdd023d
Author: Daniel Barboza <[email protected]>
Date:   Wed Oct 28 15:41:49 2026 -0300

    match.pd: (X ^ Y) & (X | Y) -> X ^ Y patterns [PR115547]
    
    Add a couple of match.pd patterns that simplifies a bit_and using a
    bit_xor and a bit_ior with the same operands:
    
    (X ^ Y) & (X | Y) -> X ^ Y
    
    For boolean values there is also a NE variant:
    
    (X NE Y) & (X | Y) -> X NE Y
    
    Also, for boolean values, we're also handling chained bit_iors:
    
    (X ^ Y)  & (Z | X | Y) -> (X ^ Y)
    (X NE Y) & (Z | X | Y) -> (X NE Y)
    
    The takeaway is that if we know that X != Y then X != Y & (X | Y | ...)
    is always going to be the same as X != Y.
    
    The remaining PR115547 patterns that uses "&&" generate chained ifconds
    that can't be handled via match.pd.  We'll try our luck handling those
    inside one of the passes at a later date.
    
    Bootstrapped and regression tested in x86_64.
    
            PR tree-optimization/115547
    
    gcc/ChangeLog:
    
            * match.pd(`(X ^ Y) & (X | Y)`): New pattern.
            (`(X NE Y) & (X | Y)`): New pattern.
            (`(X NE Y) & (Z | X | Y)`): New pattern.
            (`(X ^ Y) & (Z | X | Y)`): New pattern.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/pr115547.c: New test.

Diff:
---
 gcc/match.pd                             | 26 ++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr115547.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 70f956a692f1..fd698dd01a92 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -7233,6 +7233,32 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
               || (op == BIT_AND_EXPR && wi::eq_p (wi::bit_not (c2), c1))))
          (bit_xor @0 @1)))))
 
+/* PR115547: (X ^ Y) & (X | Y) -> (X ^ Y).  */
+(simplify
+ (bit_and:c (bit_xor@0 @X @Y) (bit_ior @X @Y))
+ @0)
+
+/* PR115547: (X NE Y) & (X | Y) -> (X NE Y)
+   Variant from above when X and Y are booleans/zero_one.  */
+(simplify
+ (bit_and:c (ne zero_one_valued_p@X zero_one_valued_p@Y)
+           (bit_ior @X @Y))
+ (ne @X @Y))
+
+/* PR115547:
+   (X ^ Y) & (Z | X | Y) -> (X ^ Y)
+   (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
  (convert (cond@0 @1 INTEGER_CST@2 INTEGER_CST@3))
  (if (INTEGRAL_TYPE_P (type)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr115547.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr115547.c
new file mode 100644
index 000000000000..3f8367d67f4c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr115547.c
@@ -0,0 +1,31 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+int fn6 (int a, int c) {
+    return (a ^ c) & (a | c);
+}
+
+_Bool fn6_alt (_Bool a, _Bool c) {
+    return (a != c) & (a | c);
+}
+
+_Bool fn2_alt (_Bool a, _Bool b, _Bool c) {
+    return (a ^ c) & (b | c | a);
+}
+
+_Bool fn2_alt2 (_Bool a, _Bool b, _Bool c) {
+    return (a ^ c) & (b | a | c);
+}
+
+_Bool fn2_alt3 (_Bool a, _Bool b, _Bool c) {
+    return (a != c) & (b | c | a);
+}
+
+_Bool fn2_alt4 (_Bool a, _Bool b, _Bool c) {
+    return (a != c) & (b | a | c);
+}
+
+/* { dg-final { scan-tree-dump-times " \\\^ " 3 "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" } } */

Reply via email to