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

commit r17-6631-gaae842ede5d7b575bbdc86ac309b1adcb28fca0e
Author: Jeff Law <[email protected]>
Date:   Tue Nov 24 07:03:39 2026 -0700

    [PR rtl/optimization/128767] Simplify (and (xor ...)) sequence to (and (not 
...)) form when possible
    
    This is one piece of the pr128767 puzzle.
    
    The core issue (as outlined in the PR) is RISC-V has a minimalist set of scc
    insns, often forcing the port to invert the result of one form via seq/xor 
#1
    to derive another.
    
    Naturally this is an RTL issue since the code looks peachy in gimple.
    
    In this case we have  (and (xor A 1) B) where we know that B is a 0/1 value.
    That means we don't really care about anything other than the least 
significant
    bit of A and we're just inverting the lowest bit.  We can safely use andn 
which
    allows us to squash out the seq/xor inversion step.
    
    So we recognize in simplify-rtx and generate the andn form.  If the target
    doesn't match that, then obviously combine will give up and we're no worse 
than
    we were before.
    
    This saves about 160 million instructions in x264; it's general enough that 
I
    could easily see it firing outside those aliasing check paths, so I won't 
claim
    it's all improvements in the aliasing testing code.
    
    The one adjusted test for aarch64 just swaps the order of instructions.
    
    Bootstrapped and regression tested on x86_64 and aarch64.  Regression 
tested on
    the cross targets as well.  The larger kit has been bootstrapped and 
regression
    tested on the K1, K3 and C920 platforms as well.
    
    Pushing to the trunk.
    
            PR rtl-optimization/128767
    gcc
            * simplify-rtx.cc (simplify_context::simplify_binary_operation_1):
            Simplify (and (xor A 1) B) to (and not (A) B) when B is a 0/1 value.
    
    gcc/testsuite
    
            * gcc.target/riscv/zbb-andn-xor1-1.c: New test.
            * gcc.target/riscv/zbb-andn-xor1-2.c: New test.
            * gcc.target/riscv/zbb-andn-xor1-3.c: New test.
            * gcc.target/riscv/zbb-andn-xor1-4.c: New test.
            * gcc.target/aarch64/bic-1.c: Adjust expected output.

Diff:
---
 gcc/simplify-rtx.cc                              | 19 ++++++++
 gcc/testsuite/gcc.target/aarch64/bic-1.c         |  4 +-
 gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-1.c | 56 ++++++++++++++++++++++++
 gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-2.c | 15 +++++++
 gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-3.c | 49 +++++++++++++++++++++
 gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-4.c | 16 +++++++
 6 files changed, 157 insertions(+), 2 deletions(-)

diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
index fb3a83bd3aef..213853a2f414 100644
--- a/gcc/simplify-rtx.cc
+++ b/gcc/simplify-rtx.cc
@@ -4485,6 +4485,25 @@ simplify_context::simplify_binary_operation_1 (rtx_code 
code,
          && GET_MODE_CLASS (mode) != MODE_CC)
        return CONST0_RTX (mode);
 
+      /* Convert (and (xor A 1) B) to (and (not A) B) when B is known to be
+        0 or 1.  Only bit 0 of the XOR matters, and the NOT may be folded
+        into an and-not instruction.  */
+      if (is_a <scalar_int_mode> (mode, &int_mode)
+         && HWI_COMPUTABLE_MODE_P (int_mode))
+       for (int i = 0; i < 2; i++)
+         {
+           rtx x = i ? op1 : op0;
+           rtx y = i ? op0 : op1;
+           if (GET_CODE (x) == XOR
+               && XEXP (x, 1) == CONST1_RTX (mode)
+               && (nonzero_bits (y, int_mode) & ~HOST_WIDE_INT_1U) == 0)
+             return simplify_gen_binary (AND, mode,
+                                         simplify_gen_unary (NOT, mode,
+                                                             XEXP (x, 0),
+                                                             mode),
+                                         y);
+         }
+
       /* Convert (and (plus (A - 1)) (neg A)) to 0.  */
       if (match_plus_neg_pattern (op0, op1, mode))
        return CONST0_RTX (mode);
diff --git a/gcc/testsuite/gcc.target/aarch64/bic-1.c 
b/gcc/testsuite/gcc.target/aarch64/bic-1.c
index 65e1514755ff..2ad815023d33 100644
--- a/gcc/testsuite/gcc.target/aarch64/bic-1.c
+++ b/gcc/testsuite/gcc.target/aarch64/bic-1.c
@@ -30,8 +30,8 @@ unsigned func2(bool a, bool b)
 
 /*
 **func3:
-**     bic     w([0-9]+), w1, w0
-**     and     w0, w\1, 1
+**     and     w([0-9]+), w1, 1
+**     bic     w0, w\1, w0
 **      ret
 */
 bool func3(bool a, unsigned char b)
diff --git a/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-1.c 
b/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-1.c
new file mode 100644
index 000000000000..986e277eac7d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-1.c
@@ -0,0 +1,56 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gc_zbb -mabi=lp64d" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */
+
+/* RISC-V has no sge/sgeu, so the inverse of a comparison is computed as a
+   set-on-less-than followed by xori 1.  When that value is ANDed with
+   another comparison result, which is known to be 0 or 1, the xori is
+   equivalent to a NOT and the pair folds into a single andn.  */
+
+int
+f_lt (long a, long b, long c, long d)
+{
+  return (a < b) & !(c < d);
+}
+
+int
+f_gt (long a, long b, long c, long d)
+{
+  return (a < b) & !(c > d);
+}
+
+int
+f_ltu (unsigned long a, unsigned long b, unsigned long c, unsigned long d)
+{
+  return (a < b) & !(c < d);
+}
+
+int
+f_gtu (unsigned long a, unsigned long b, unsigned long c, unsigned long d)
+{
+  return (a < b) & !(c > d);
+}
+
+/* The inverted comparison as the first operand.  */
+int
+f_swapped (long a, long b, long c, long d)
+{
+  return !(c < d) & (a < b);
+}
+
+/* 32-bit operands.  */
+int
+f_si (int a, int b, int c, int d)
+{
+  return (a < b) & !(c < d);
+}
+
+/* The other operand is itself an AND of comparisons.  */
+int
+f_and (long a, long b, long c, long d, long e, long f)
+{
+  return ((a < b) & (e < f)) & !(c < d);
+}
+
+/* { dg-final { scan-assembler-times "\tandn\t" 7 } } */
+/* { dg-final { scan-assembler-not "\txori\t" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-2.c 
b/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-2.c
new file mode 100644
index 000000000000..95928487226e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-2.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv64gc_zbb -mabi=lp64d" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */
+
+/* The other operand is not known to be 0 or 1, so the xori cannot be
+   treated as a NOT: (x ^ 1) & b clears bit 0 of b when x is 1, whereas
+   ~x & b would keep the upper bits of b.  */
+
+long
+f (long a, long b, long c, long d)
+{
+  return (long) !(c < d) & a;
+}
+
+/* { dg-final { scan-assembler-not "\tandn\t" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-3.c 
b/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-3.c
new file mode 100644
index 000000000000..1f0e8465215d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-3.c
@@ -0,0 +1,49 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv32gc_zbb -mabi=ilp32d" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */
+
+/* RV32 version of zbb-andn-xor1-1.c.  The inverse of a comparison is
+   computed as a set-on-less-than followed by xori 1.  When that value is
+   ANDed with another comparison result, which is known to be 0 or 1, the
+   xori is equivalent to a NOT and the pair folds into a single andn.  */
+
+int
+f_lt (int a, int b, int c, int d)
+{
+  return (a < b) & !(c < d);
+}
+
+int
+f_gt (int a, int b, int c, int d)
+{
+  return (a < b) & !(c > d);
+}
+
+int
+f_ltu (unsigned a, unsigned b, unsigned c, unsigned d)
+{
+  return (a < b) & !(c < d);
+}
+
+int
+f_gtu (unsigned a, unsigned b, unsigned c, unsigned d)
+{
+  return (a < b) & !(c > d);
+}
+
+/* The inverted comparison as the first operand.  */
+int
+f_swapped (int a, int b, int c, int d)
+{
+  return !(c < d) & (a < b);
+}
+
+/* The other operand is itself an AND of comparisons.  */
+int
+f_and (int a, int b, int c, int d, int e, int f)
+{
+  return ((a < b) & (e < f)) & !(c < d);
+}
+
+/* { dg-final { scan-assembler-times "\tandn\t" 6 } } */
+/* { dg-final { scan-assembler-not "\txori\t" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-4.c 
b/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-4.c
new file mode 100644
index 000000000000..3b8beb5d2dc9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zbb-andn-xor1-4.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv32gc_zbb -mabi=ilp32d" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */
+
+/* RV32 version of zbb-andn-xor1-2.c.  The other operand is not known to be
+   0 or 1, so the xori cannot be treated as a NOT: (x ^ 1) & b clears bit 0
+   of b when x is 1, whereas ~x & b would keep the upper bits of b.  */
+
+int
+f (int a, int c)
+{
+  return (c ^ 1) & a;
+}
+
+/* { dg-final { scan-assembler-not "\tandn\t" } } */
+/* { dg-final { scan-assembler "\txori\t" } } */

Reply via email to