>From 6fd93ce7b4aa9dacf5c10c042f3681a41a50758f Mon Sep 17 00:00:00 2001
From: Kael Andrew Alonzo Franco <[email protected]>
Date: Tue, 28 Jul 2026 06:53:33 -0400
Subject: [PATCH] match: Combine patterns into five for.

This merges 10 simplify patterns so genmatch outputs
less C++ code.

Bootstrapped and regtested on x86_64-pc-linux-gnu.

gcc/ChangeLog:

        * match.pd: Combine patterns into five for.

Signed-off-by: Kael Andrew Franco <[email protected]>
---
 gcc/match.pd | 82 ++++++++++++++++++++++++----------------------------
 1 file changed, 37 insertions(+), 45 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 62d08093125..87275610fbe 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1655,15 +1655,13 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
        && (!wascmp || element_precision (type) == 1))
    @0)))

-/* ~(~a & b)  -->  a | ~b  */
-(simplify
- (bit_not (bit_and:cs (bit_not @0) @1))
- (bit_ior @0 (bit_not @1)))
-
-/* ~(~a | b) --> a & ~b */
-(simplify
- (bit_not (bit_ior:cs (bit_not @0) @1))
- (bit_and @0 (bit_not @1)))
+/* ~(~a & b) --> a | ~b
+   ~(~a | b) --> a & ~b  */
+(for iop (bit_and bit_ior)
+     res (bit_ior bit_and)
+ (simplify
+  (bit_not (iop:cs (bit_not @0) @1))
+  (res @0 (bit_not @1))))

 /* (a ^ b) & ((b ^ c) ^ a) --> (a ^ b) & ~c */
 (simplify
@@ -1928,27 +1926,27 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (bit_xor:c (bit_ior:c @0 @1) (bit_ior:c @0 (bit_not @1)))
  (bit_not @0))

-/* (x & y) | ~(x | y) -> ~(x ^ y) */
-(simplify
- (bit_ior:c (bit_and:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
- (bit_not (bit_xor @0 @1)))
+/* (x & y) | ~(x | y) -> ~(x ^ y)
+   (x ^ y) | ~(x | y) -> ~(x & y)  */
+(for lop (bit_and bit_xor)
+     res (bit_xor bit_and)
+ (simplify
+  (bit_ior:c (lop:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
+  (bit_not (res @0 @1))))

 /* (~x | y) ^ (x ^ y) -> x | ~y */
 (simplify
  (bit_xor:c (bit_ior:cs (bit_not @0) @1) (bit_xor:s @0 @1))
  (bit_ior @0 (bit_not @1)))

-/* (x ^ y) | ~(x | y) -> ~(x & y) */
-(simplify
- (bit_ior:c (bit_xor:s @0 @1) (bit_not:s (bit_ior:s @0 @1)))
- (bit_not (bit_and @0 @1)))
-
-/* (x & y) ^ (x | y) -> x ^ y */
-(simplify
- (bit_xor:c (bit_and @0 @1) (bit_ior @0 @1))
- (bit_xor @0 @1))
+/* (x & y) ^ (x | y) -> x ^ y
+   (x ^ y) ^ (x | y) -> x & y  */
+(for lop (bit_and bit_xor)
+     res (bit_xor bit_and)
+ (simplify
+  (bit_xor:c (lop @0 @1) (bit_ior @0 @1))
+  (res @0 @1)))

-/* (x ^ y) ^ (x | y) -> x & y */
 (simplify
  (bit_xor:c (bit_xor @0 @1) (bit_ior @0 @1))
  (bit_and @0 @1))
@@ -1966,34 +1964,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (plus:c (bit_and @0 @1) (bit_ior @0 @1))
  (plus @0 @1))

-/* (x + y) - (x | y) -> x & y */
-(simplify
- (minus (plus @0 @1) (bit_ior @0 @1))
- (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
-      && !TYPE_SATURATING (type))
-  (bit_and @0 @1)))
-
-/* (x + y) - (x & y) -> x | y */
-(simplify
- (minus (plus @0 @1) (bit_and @0 @1))
- (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
-      && !TYPE_SATURATING (type))
-  (bit_ior @0 @1)))
+/* (x + y) - (x | y) -> x & y
+   (x + y) - (x & y) -> x | y  */
+(for rop (bit_ior bit_and)
+     res (bit_and bit_ior)
+ (simplify
+  (minus (plus @0 @1) (rop @0 @1))
+  (if (!TYPE_OVERFLOW_SANITIZED (type) && !TYPE_OVERFLOW_TRAPS (type)
+       && !TYPE_SATURATING (type))
+   (res @0 @1))))

 /* (x | y) - y -> (x & ~y) */
 (simplify
  (minus (bit_ior:cs @0 @1) @1)
  (bit_and @0 (bit_not @1)))

-/* (x | y) - (x ^ y) -> x & y */
-(simplify
- (minus (bit_ior @0 @1) (bit_xor @0 @1))
- (bit_and @0 @1))
-
-/* (x | y) - (x & y) -> x ^ y */
-(simplify
- (minus (bit_ior @0 @1) (bit_and @0 @1))
- (bit_xor @0 @1))
+/* (x | y) - (x ^ y) -> x & y
+   (x | y) - (x & y) -> x ^ y  */
+(for rop (bit_xor bit_and)
+     res (bit_and bit_xor)
+ (simplify
+  (minus (bit_ior @0 @1) (rop @0 @1))
+  (res @0 @1)))

 /* (x | y) & ~(x & y) -> x ^ y */
 (simplify
-- 
2.55.0

Reply via email to