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

commit r17-6411-gace34007589579c27cbe0b40575466d351bd049d
Author: Jiawei <[email protected]>
Date:   Tue Nov 17 15:56:59 2026 +0800

    RISC-V: Preserve masks when complementing shift counts
    
    Keep the five- or six-bit mask when rewriting a shift count as a
    complement. Dropping it can produce wrong code when combine later
    selects an XLEN-wide Zbs instruction for a word mask.
    
    Preserve the masks in both the native and word splitters. Existing
    masked shift patterns still emit the same instructions. Add a runtime
    test for narrow counts derived from 31 - y and check that PR123838
    still needs no separate masking instruction.
    
    gcc/ChangeLog:
    
            * config/riscv/riscv.md: Preserve count masks in the native and
            word shift-complement splitters.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/riscv/pr123838.c: Check for no extra andi.
            * gcc.target/riscv/shift-count-complement-1.c: New test.

Diff:
---
 gcc/config/riscv/riscv.md                          | 19 +++++---
 gcc/testsuite/gcc.target/riscv/pr123838.c          |  1 +
 .../gcc.target/riscv/shift-count-complement-1.c    | 54 ++++++++++++++++++++++
 3 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
index 4135b60be6cb..f9128bbdc383 100644
--- a/gcc/config/riscv/riscv.md
+++ b/gcc/config/riscv/riscv.md
@@ -5299,6 +5299,8 @@
 ;; that -x = ~x + 1 which is equivalent to (-1 - x) = ~x.  When shifting only
 ;; low bits of X matter (5 for SI, 6 for DI).  So 31/63 are equivalent to -1
 ;; for SI/DI shifts.
+;; Keep the mask in RTL, since later combinations can select instructions
+;; with a different count width.  Masked shift patterns omit it in assembly.
 ;;
 ;; Strangely, even for rv64, the shift computation is done in SI, presumably
 ;; something narrowed the arithmetic prior to gimple->rtl expansion.
@@ -5311,11 +5313,13 @@
                               (match_operand:SI 3 "register_operand")) 0)))
     (clobber (match_operand:X 4 "register_operand"))]
   ""
-  [(set (match_dup 4) (not:X (match_dup 6)))
-   (set (match_dup 0) (any_shift_rotate:X (match_dup 1) (match_dup 5)))]
+  [(set (match_dup 4) (not:X (match_dup 5)))
+   (set (match_dup 0)
+       (any_shift_rotate:X
+         (match_dup 1)
+         (subreg:QI (and:X (match_dup 4) (match_dup 2)) 0)))]
  {
-   operands[5] = gen_lowpart (QImode, operands[4]);
-   operands[6] = gen_lowpart (word_mode, operands[3]);
+   operands[5] = gen_lowpart (word_mode, operands[3]);
  })
 
 ;; This is the same thing as the prior pattern, but for 32 bit shifts on rv64.
@@ -5330,11 +5334,12 @@
   "TARGET_64BIT"
   [(set (match_dup 3) (not:DI (match_dup 2)))
    (set (match_dup 0)
-       (sign_extend:DI (any_shift_rotate:SI (match_dup 1)
-                                            (match_dup 4))))]
+       (sign_extend:DI
+         (any_shift_rotate:SI
+           (match_dup 1)
+           (subreg:QI (and:DI (match_dup 3) (const_int 31)) 0))))]
  {
    operands[2] = gen_lowpart (DImode, operands[2]);
-   operands[4] = gen_lowpart (QImode, operands[3]);
  })
 
 ;; This is similar using a shift triplet to implement a logical AND when
diff --git a/gcc/testsuite/gcc.target/riscv/pr123838.c 
b/gcc/testsuite/gcc.target/riscv/pr123838.c
index 58a8ea6ea0bb..e4be9c4f27a4 100644
--- a/gcc/testsuite/gcc.target/riscv/pr123838.c
+++ b/gcc/testsuite/gcc.target/riscv/pr123838.c
@@ -12,4 +12,5 @@ F(LSHIFT, <<, long)
 /* { dg-final { scan-assembler-not "li\t" } } */
 /* { dg-final { scan-assembler-not "sub\t" } } */
 /* { dg-final { scan-assembler-not "subw\t" } } */
+/* { dg-final { scan-assembler-not "andi\t" } } */
 
diff --git a/gcc/testsuite/gcc.target/riscv/shift-count-complement-1.c 
b/gcc/testsuite/gcc.target/riscv/shift-count-complement-1.c
new file mode 100644
index 000000000000..b3bc9f6f0431
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/shift-count-complement-1.c
@@ -0,0 +1,54 @@
+/* { dg-do run { target { rv64 && riscv_b_ok } } } */
+/* { dg-options "-O2 -march=rv64gc_zbs -mabi=lp64d" } */
+
+/* The narrow count lets the 31 - y splitter run before combining the
+   shifted mask with a Zbs operation.  */
+#define OPERATOR(NAME, OP) \
+__attribute__ ((noipa)) unsigned long \
+NAME (unsigned long x, unsigned int y) \
+{ \
+  unsigned char n = 31 - y; \
+  return x OP (unsigned long) (1U << n); \
+}
+
+OPERATOR (set, |)
+OPERATOR (invert, ^)
+OPERATOR (clear, & ~)
+
+__attribute__ ((noipa)) unsigned long
+mask (unsigned int y)
+{
+  unsigned char n = 31 - y;
+  return (unsigned long) (1U << n);
+}
+
+__attribute__ ((noipa)) long
+signed_mask (unsigned int y)
+{
+  unsigned char n = 31 - y;
+  return (int) (1U << n);
+}
+
+int
+main (void)
+{
+  unsigned long values[] = { 0, ~0UL, 0xaaaaaaaa55555555UL };
+
+  for (unsigned int y = 0; y < 32; ++y)
+    {
+      unsigned long m = 0x80000000UL >> y;
+      long sm = y == 0 ? -0x80000000L : (long) m;
+      if (mask (y) != m || signed_mask (y) != sm)
+       __builtin_abort ();
+
+      for (unsigned int i = 0; i < 3; ++i)
+       {
+         unsigned long x = values[i];
+         if (set (x, y) != (x | m)
+             || invert (x, y) != (x ^ m)
+             || clear (x, y) != (x & ~m))
+           __builtin_abort ();
+       }
+    }
+  return 0;
+}

Reply via email to