https://gcc.gnu.org/g:6c695e3e27d6447289e6faa93cf9f8aaec874d36

commit r17-6397-g6c695e3e27d6447289e6faa93cf9f8aaec874d36
Author: Jin Ma <[email protected]>
Date:   Wed Aug 12 18:57:44 2026 +0800

    RISC-V: Generalize LDPREINCREMENT macro-fusion recognition
    
    Allow LDPREINCREMENT to match non-word ADDI-type in-place address
    updates followed by scalar integer or floating-point loads.  Support
    register-plus-offset and LO_SUM addresses, self moves, and either
    load-destination relationship.
    
    Put the common pre/post-index load/store constraints in a
    parameterized helper for later fusion rules.  For stores, reject
    overlap between the updated base and any register used by the store
    data, including RV32 Zilsd register pairs.
    
    Assisted-by: qwenwork
    
    gcc/ChangeLog:
    
            * config/riscv/riscv-fusion.cc (riscv_fuse_indexed_mem_p): New
            function.  Check overlap with the complete store-data register 
range.
            (riscv_fuse_ldpreincrement): Use it and accept scalar integer or
            floating-point loads.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/riscv/fusion-preindex-load.c: New test.
            * gcc.target/riscv/fusion-preindex-load-rtl.c: Likewise.
    
    Signed-off-by: Jin Ma <[email protected]>

Diff:
---
 gcc/config/riscv/riscv-fusion.cc                   | 78 +++++++++++++++-------
 .../gcc.target/riscv/fusion-preindex-load-rtl.c    | 67 +++++++++++++++++++
 .../gcc.target/riscv/fusion-preindex-load.c        | 31 +++++++++
 3 files changed, 152 insertions(+), 24 deletions(-)

diff --git a/gcc/config/riscv/riscv-fusion.cc b/gcc/config/riscv/riscv-fusion.cc
index 61d3b185ba78..3841ce1d8bce 100644
--- a/gcc/config/riscv/riscv-fusion.cc
+++ b/gcc/config/riscv/riscv-fusion.cc
@@ -1067,6 +1067,47 @@ riscv_fuse_add_mem_p (rtx_insn *prev, rtx_insn *curr,
   return true;
 }
 
+/* Match an in-place ADDI-type update and a scalar load or store with the
+   same base register.  LOAD_P selects loads rather than stores, and
+   PREINDEX_P selects whether the update precedes the memory instruction.
+   The memory offset is independent of the update amount.  Loads may
+   overwrite the base register.  */
+
+static bool
+riscv_fuse_indexed_mem_p (rtx_insn *prev, rtx_insn *curr,
+                         bool load_p, bool preindex_p)
+{
+  rtx prev_set, curr_set;
+  if (!riscv_fuse_sets_p (prev, curr, &prev_set, &curr_set))
+    return false;
+
+  rtx_insn *update_insn = preindex_p ? prev : curr;
+  rtx_insn *mem_insn = preindex_p ? curr : prev;
+  rtx update_set = preindex_p ? prev_set : curr_set;
+  rtx update_dest = SET_DEST (update_set);
+  rtx update_base = NULL_RTX;
+  struct riscv_fusion_mem_info mem;
+
+  if (!riscv_insn_is_addi_type_p (update_insn, false, &update_base)
+      || update_base == NULL_RTX
+      || !riscv_fuse_mem_p (mem_insn, &mem)
+      || (load_p
+         ? mem.type == SCHED_FUSION_ST
+         : mem.type != SCHED_FUSION_ST)
+      || !(mem.addr.type == ADDRESS_REG
+          || mem.addr.type == ADDRESS_LO_SUM)
+      || riscv_regno (update_dest) == INVALID_REGNUM
+      || !riscv_fuse_same_reg_p (update_base, update_dest)
+      || !riscv_fuse_same_reg_p (mem.addr.reg, update_dest))
+    return false;
+
+  if (load_p)
+    return true;
+
+  /* A multi-register store must not overlap the updated base.  */
+  return !reg_overlap_mentioned_p (update_dest, mem.reg);
+}
+
 /* Check two parsed accesses in their proposed execution order against RULE.
    The caller selects enabled rules and checks the scheduling phase.  Pair
    formation must separately prove that moving the accesses is safe.  */
@@ -1285,36 +1326,25 @@ riscv_fuse_expanded_ld (rtx_insn *prev, rtx_insn *curr)
 }
 
 /* Check for RISCV_FUSE_LDPREINCREMENT fusion.
-   prev (addi) == (set (reg rd1)
-                      (plus (reg rd1) (const_int offset)))
-   curr (load) == (set (reg rd2) (mem (reg rd1)))
+   prev (one of the following):
+     (addi) == (set (reg rd1) (plus (reg rd1) (const_int imm12)))
+     (self-mv) == (set (reg rd1) (reg rd1))
+     (addi) == (set (reg rd1) (lo_sum (reg rd1) symbol1))
+   curr (one of the following):
+     (load) == (set (reg rd2) (mem addr))
+     (load) == (set (reg rd2) (any_extend (mem addr)))
+     (fpload) == (set (reg frd) (mem addr))
+   addr (one of the following):
+     (rd1, offset)
+     (lo_sum (reg rd1) symbol2)
 
    Constraints:
-     rd1 == rd2.  */
+     the ADDI-type instruction is not a word form.  */
 
 static bool
 riscv_fuse_ldpreincrement (rtx_insn *prev, rtx_insn *curr)
 {
-  rtx prev_set = single_set (prev);
-  rtx curr_set = single_set (curr);
-  if (!prev_set || !curr_set || any_condjump_p (curr))
-    return false;
-
-  if (!riscv_fuse_same_dest_p (prev_set, curr_set))
-    return false;
-
-  if (MEM_P (SET_SRC (curr_set))
-      && SCALAR_INT_MODE_P (GET_MODE (SET_DEST (curr_set)))
-      && REG_P (XEXP (SET_SRC (curr_set), 0))
-      && REGNO (XEXP (SET_SRC (curr_set), 0)) == REGNO (SET_DEST (prev_set))
-      && GET_CODE (SET_SRC (prev_set)) == PLUS
-      && REG_P (XEXP (SET_SRC (prev_set), 0))
-      && CONST_INT_P (XEXP (SET_SRC (prev_set), 1))
-      && riscv_fuse_same_reg_p (XEXP (SET_SRC (prev_set), 0),
-                               SET_DEST (prev_set)))
-    return true;
-
-  return false;
+  return riscv_fuse_indexed_mem_p (prev, curr, true, true);
 }
 
 /* Check for RISCV_FUSE_LUI_ADDI fusion.
diff --git a/gcc/testsuite/gcc.target/riscv/fusion-preindex-load-rtl.c 
b/gcc/testsuite/gcc.target/riscv/fusion-preindex-load-rtl.c
new file mode 100644
index 000000000000..e7d6900aa8d3
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/fusion-preindex-load-rtl.c
@@ -0,0 +1,67 @@
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mexplicit-relocs -O2 
-mtune=xt-c9501fdvt -fdump-rtl-sched2-details" } */
+/* { dg-final { scan-rtl-dump-times "RISCV_FUSE_LDPREINCREMENT" 2 "sched2" { 
xfail *-*-* } } } */
+
+extern double preindex_symbol;
+
+/* The load may write the address-update destination.  */
+long __RTL (startwith ("sched2"))
+test_preindex_same_destination (void)
+{
+(function "test_preindex_same_destination"
+  (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 a0)
+                    (plus:DI (reg:DI a0) (const_int 1))))
+      (cinsn 4 (set (reg:DI a0)
+                    (zero_extend:DI
+                      (mem:QI (reg:DI a0) [0 S1 A8]))))
+      (cinsn 5 (use (reg/i:DI a0)))
+      (cjump_insn 6 (simple_return))
+      (edge-to exit)
+    ) ;; block 2
+    (cbarrier 7)
+  ) ;; insn-chain
+  (crtl
+    (return_rtx (reg/i:DI a0))
+  ) ;; crtl
+) ;; function "test_preindex_same_destination"
+}
+
+/* Both the update and memory address use LO_SUM.  The load intentionally
+   applies the low relocation again after the update.  */
+long __RTL (startwith ("sched2"))
+test_preindex_lo_sum_fload (void)
+{
+(function "test_preindex_lo_sum_fload"
+  (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 a0)
+                    (lo_sum:DI
+                      (reg:DI a0)
+                      (symbol_ref:DI ("preindex_symbol")))))
+      (cinsn 4 (set (reg:DF fa0)
+                    (mem:DF
+                      (lo_sum:DI
+                        (reg:DI a0)
+                        (symbol_ref:DI ("preindex_symbol")))
+                      [0 S8 A64])))
+      (cinsn 5 (use (reg:DI a0)))
+      (cinsn 6 (use (reg:DF fa0)))
+      (cjump_insn 7 (simple_return))
+      (edge-to exit)
+    ) ;; block 2
+    (cbarrier 8)
+  ) ;; insn-chain
+  (crtl
+    (return_rtx (reg/i:DI a0))
+  ) ;; crtl
+) ;; function "test_preindex_lo_sum_fload"
+}
diff --git a/gcc/testsuite/gcc.target/riscv/fusion-preindex-load.c 
b/gcc/testsuite/gcc.target/riscv/fusion-preindex-load.c
new file mode 100644
index 000000000000..6fd83652d55b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/fusion-preindex-load.c
@@ -0,0 +1,31 @@
+/* { dg-do compile { target { rv64 } } } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-funroll-loops" 
"-flto" } } */
+/* { dg-options "-march=rv64gc -mabi=lp64d -mtune=xt-c9501fdvt -O2 
-fdump-rtl-sched2-details" } */
+/* No tune enables this fusion pair yet.  */
+/* { dg-final { scan-rtl-dump-times "RISCV_FUSE_LDPREINCREMENT" 2 "sched2" { 
xfail *-*-* } } } */
+
+typedef signed char int8_t;
+
+int8_t
+test_preindex_lb (int8_t *p, int n)
+{
+  int8_t sum = 0;
+  for (int i = 0; i < n; ++i)
+    {
+      p += 2;
+      sum += *p;
+    }
+  return sum;
+}
+
+double
+test_preindex_fld (double *p, int n)
+{
+  double sum = 0;
+  for (int i = 0; i < n; ++i)
+    {
+      p += 2;
+      sum += *p;
+    }
+  return sum;
+}

Reply via email to