https://gcc.gnu.org/g:8904680968822dd9416ec4878cd1320d04d43baf

commit r17-6402-g8904680968822dd9416ec4878cd1320d04d43baf
Author: Jin Ma <[email protected]>
Date:   Wed Aug 12 19:01:13 2026 +0800

    RISC-V: Add descending integer load/store pair fusion
    
    Recognize the descending-address counterpart of the integer
    load/store pair fusion.  Reuse the common pair matcher and select the
    reversed one-access-size offset relationship.
    Use RTL tests to supply descending pairs directly to sched2.
    
    Leave the fusion disabled by default and XFAIL its positive dump
    checks until a CPU enables it.
    
    Assisted-by: qwenwork
    
    gcc/ChangeLog:
    
            * config/riscv/riscv-fusion.cc (riscv_fusion_mem_rules): Add
            RISCV_FUSE_LDST_PAIR_DEC.
            (riscv_fuse_ldst_pair_dec): New function.
            (riscv_fusion_table): Add RISCV_FUSE_LDST_PAIR_DEC.
            * config/riscv/riscv-protos.h (enum riscv_fusion_pairs): Add
            RISCV_FUSE_LDST_PAIR_DEC.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/riscv/fusion-ldst-pair-dec.c: New test.
    
    Signed-off-by: Jin Ma <[email protected]>

Diff:
---
 gcc/config/riscv/riscv-fusion.cc                   |  26 +++++
 gcc/config/riscv/riscv-protos.h                    |   1 +
 .../gcc.target/riscv/fusion-ldst-pair-dec.c        | 112 +++++++++++++++++++++
 3 files changed, 139 insertions(+)

diff --git a/gcc/config/riscv/riscv-fusion.cc b/gcc/config/riscv/riscv-fusion.cc
index 89a044ffbe34..43ae80c52062 100644
--- a/gcc/config/riscv/riscv-fusion.cc
+++ b/gcc/config/riscv/riscv-fusion.cc
@@ -1016,6 +1016,9 @@ static const struct riscv_fusion_mem_rule 
riscv_fusion_mem_rules[] =
   { RISCV_FUSE_LDST_PAIR_INC, (1U << 4) | (1U << 8),
     SCHED_FUSION_LD | SCHED_FUSION_LD_SIGN_EXTEND | SCHED_FUSION_ST,
     false, false, RISCV_FUSION_INC, 1 },
+  { RISCV_FUSE_LDST_PAIR_DEC, (1U << 4) | (1U << 8),
+    SCHED_FUSION_LD | SCHED_FUSION_LD_SIGN_EXTEND | SCHED_FUSION_ST,
+    false, false, RISCV_FUSION_DEC, 1 },
   /* Allow 1-, 2-, 4- and 8-byte stores.  */
   { RISCV_FUSE_ALIGNED_STD, (1U << 1) | (1U << 2) | (1U << 4) | (1U << 8),
     SCHED_FUSION_ST, false, true, RISCV_FUSION_ANY, 2 }
@@ -1603,6 +1606,27 @@ riscv_fuse_ldst_pair_inc (rtx_insn *prev, rtx_insn *curr)
   return riscv_fuse_mem_pair_p (prev, curr, RISCV_FUSE_LDST_PAIR_INC);
 }
 
+/* Check for RISCV_FUSE_LDST_PAIR_DEC fusion.
+   prev/curr (one of the following pairs):
+     prev (lw/ld) == (set (reg rd1) (mem (rs1, offset1)))
+     curr (lw/ld) == (set (reg rd2) (mem (rs1, offset2)))
+
+     prev (sw/sd) == (set (mem (rs1, offset1)) (reg rs2))
+     curr (sw/sd) == (set (mem (rs1, offset2)) (reg rs3))
+
+   Constraints:
+     access size is 4 or 8 bytes
+     offset1 - offset2 equals the access size
+     offsets are aligned to the access size
+     loads are not zero-extending
+     for loads, rd1 != rd2 and rd1 != rs1.  */
+
+static bool
+riscv_fuse_ldst_pair_dec (rtx_insn *prev, rtx_insn *curr)
+{
+  return riscv_fuse_mem_pair_p (prev, curr, RISCV_FUSE_LDST_PAIR_DEC);
+}
+
 /* Check for RISCV_FUSE_BFEXT fusion.
    prev (slli) == (set (reg rd1)
                       (ashift (reg rs1) (const_int shamt1)))
@@ -1953,6 +1977,8 @@ static const struct riscv_fusion_entry 
riscv_fusion_table[] =
     riscv_fuse_aligned_std, "RISCV_FUSE_ALIGNED_STD" },
   { RISCV_FUSE_LDST_PAIR_INC,
     riscv_fuse_ldst_pair_inc, "RISCV_FUSE_LDST_PAIR_INC" },
+  { RISCV_FUSE_LDST_PAIR_DEC,
+    riscv_fuse_ldst_pair_dec, "RISCV_FUSE_LDST_PAIR_DEC" },
   { RISCV_FUSE_BFEXT,
     riscv_fuse_bfext, "RISCV_FUSE_BFEXT" },
   { RISCV_FUSE_SLLI_SRLI,
diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h
index 7f08eff61032..680dd0b28719 100644
--- a/gcc/config/riscv/riscv-protos.h
+++ b/gcc/config/riscv/riscv-protos.h
@@ -880,6 +880,7 @@ enum riscv_fusion_pairs
   RISCV_FUSE_POSTINDEX_LD = HOST_WIDE_INT_1U << 21,
   RISCV_FUSE_POSTINDEX_ST = HOST_WIDE_INT_1U << 22,
   RISCV_FUSE_LDST_PAIR_INC = HOST_WIDE_INT_1U << 23,
+  RISCV_FUSE_LDST_PAIR_DEC = HOST_WIDE_INT_1U << 24,
 };
 
 extern bool riscv_macro_fusion_p (void);
diff --git a/gcc/testsuite/gcc.target/riscv/fusion-ldst-pair-dec.c 
b/gcc/testsuite/gcc.target/riscv/fusion-ldst-pair-dec.c
new file mode 100644
index 000000000000..844666099fd7
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/fusion-ldst-pair-dec.c
@@ -0,0 +1,112 @@
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt -O2 
-fdump-rtl-sched2-details" } */
+/* { dg-final { scan-rtl-dump-times "RISCV_FUSE_LDST_PAIR_DEC" 4 "sched2" { 
xfail *-*-* } } } */
+
+/* Present descending pairs directly to sched2 so that earlier passes do
+   not choose the memory order.  Each function has exactly one pair.  */
+long __RTL (startwith ("sched2"))
+test_ld_pair_dec (long *p)
+{
+(function "test_ld_pair_dec"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (reg:DI a1)
+                    (mem:DI
+                      (plus:DI (reg:DI a0)
+                               (const_int 8)) [0 S8 A64])))
+      (cinsn 4 (set (reg:DI a0)
+                    (mem:DI (reg:DI a0) [0 S8 A64])))
+      (cinsn 5 (set (reg:DI a0)
+                    (plus:DI (reg:DI a1) (reg:DI a0))))
+      (cinsn 6 (use (reg/i:DI a0)))
+      (cjump_insn 7 (simple_return))
+      (edge-to exit)
+    ) ;; block 2
+    (cbarrier 8)
+  ) ;; insn-chain
+  (crtl
+    (return_rtx (reg/i:DI a0))
+  ) ;; crtl
+) ;; function "test_ld_pair_dec"
+}
+
+long __RTL (startwith ("sched2"))
+test_lw_pair_dec (int *p)
+{
+(function "test_lw_pair_dec"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (reg:DI a1)
+                    (sign_extend:DI
+                      (mem:SI
+                        (plus:DI (reg:DI a0)
+                                 (const_int 4)) [0 S4 A32]))))
+      (cinsn 4 (set (reg:DI a0)
+                    (sign_extend:DI
+                      (mem:SI (reg:DI a0) [0 S4 A32]))))
+      (cinsn 5 (set (reg:DI a0)
+                    (plus:DI (reg:DI a1) (reg:DI a0))))
+      (cinsn 6 (use (reg/i:DI a0)))
+      (cjump_insn 7 (simple_return))
+      (edge-to exit)
+    ) ;; block 2
+    (cbarrier 8)
+  ) ;; insn-chain
+  (crtl
+    (return_rtx (reg/i:DI a0))
+  ) ;; crtl
+) ;; function "test_lw_pair_dec"
+}
+
+void __RTL (startwith ("sched2"))
+test_sd_pair_dec (long *p, long a, long b)
+{
+(function "test_sd_pair_dec"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (mem:DI
+                      (plus:DI (reg:DI a0)
+                               (const_int 8)) [0 S8 A64])
+                    (reg:DI a2)))
+      (cinsn 4 (set (mem:DI (reg:DI a0) [0 S8 A64])
+                    (reg:DI a1)))
+      (cjump_insn 5 (simple_return))
+      (edge-to exit)
+    ) ;; block 2
+    (cbarrier 6)
+  ) ;; insn-chain
+) ;; function "test_sd_pair_dec"
+}
+
+void __RTL (startwith ("sched2"))
+test_sw_pair_dec (int *p, int a, int b)
+{
+(function "test_sw_pair_dec"
+  (insn-chain
+    (block 2
+      (edge-from entry (flags "FALLTHRU"))
+      (cnote 1 [bb 2] NOTE_INSN_BASIC_BLOCK)
+      (cnote 2 NOTE_INSN_FUNCTION_BEG)
+      (cinsn 3 (set (mem:SI
+                      (plus:DI (reg:DI a0)
+                               (const_int 4)) [0 S4 A32])
+                    (reg:SI a2)))
+      (cinsn 4 (set (mem:SI (reg:DI a0) [0 S4 A32])
+                    (reg:SI a1)))
+      (cjump_insn 5 (simple_return))
+      (edge-to exit)
+    ) ;; block 2
+    (cbarrier 6)
+  ) ;; insn-chain
+) ;; function "test_sw_pair_dec"
+}

Reply via email to