https://gcc.gnu.org/g:abe009e4a16c0f099f6641892552267b7da0a920
commit r17-6401-gabe009e4a16c0f099f6641892552267b7da0a920 Author: Jin Ma <[email protected]> Date: Wed Aug 12 19:00:31 2026 +0800 RISC-V: Add ascending integer load/store pair fusion Recognize adjacent 4-byte or 8-byte integer loads or stores whose addresses share a base and increase by one access size. Add a rule to the shared memory-pair table, requiring access-size-aligned offsets, disjoint load results and preservation of the common base after the first load. The shared matcher checks full register ranges, including RV32 Zilsd loads. Test same and different subregister bases before allocation on RV32. Leave the new 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_INC. (riscv_fuse_ldst_pair_inc): New function. (riscv_fusion_table): Add RISCV_FUSE_LDST_PAIR_INC. * config/riscv/riscv-protos.h (enum riscv_fusion_pairs): Add RISCV_FUSE_LDST_PAIR_INC. gcc/testsuite/ChangeLog: * gcc.target/riscv/fusion-ldst-pair-inc.c: New test. * gcc.target/riscv/fusion-memory-rtl-shapes-ldst-pair.c: Likewise. * gcc.target/riscv/fusion-memory-rtl-shapes-ldst-pair-unaligned.c: Likewise. * gcc.target/riscv/fusion-pair-subreg-prera-negative.c: Likewise. * gcc.target/riscv/fusion-pair-subreg-prera-positive.c: Likewise. 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-inc.c | 33 +++++++++++ .../fusion-memory-rtl-shapes-ldst-pair-unaligned.c | 64 ++++++++++++++++++++ .../riscv/fusion-memory-rtl-shapes-ldst-pair.c | 38 ++++++++++++ .../riscv/fusion-pair-subreg-prera-negative.c | 68 ++++++++++++++++++++++ .../riscv/fusion-pair-subreg-prera-positive.c | 68 ++++++++++++++++++++++ 7 files changed, 298 insertions(+) diff --git a/gcc/config/riscv/riscv-fusion.cc b/gcc/config/riscv/riscv-fusion.cc index e4bc6710b6dc..89a044ffbe34 100644 --- a/gcc/config/riscv/riscv-fusion.cc +++ b/gcc/config/riscv/riscv-fusion.cc @@ -1013,6 +1013,9 @@ struct riscv_fusion_mem_rule 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 }, /* 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 } @@ -1579,6 +1582,27 @@ riscv_fuse_aligned_std (rtx_insn *prev, rtx_insn *curr) return riscv_fuse_mem_pair_p (prev, curr, RISCV_FUSE_ALIGNED_STD); } +/* Check for RISCV_FUSE_LDST_PAIR_INC 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 + offset2 - offset1 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_inc (rtx_insn *prev, rtx_insn *curr) +{ + return riscv_fuse_mem_pair_p (prev, curr, RISCV_FUSE_LDST_PAIR_INC); +} + /* Check for RISCV_FUSE_BFEXT fusion. prev (slli) == (set (reg rd1) (ashift (reg rs1) (const_int shamt1))) @@ -1927,6 +1951,8 @@ static const struct riscv_fusion_entry riscv_fusion_table[] = riscv_fuse_auipc_ld, "RISCV_FUSE_AUIPC_LD" }, { RISCV_FUSE_ALIGNED_STD, 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_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 5accdfa02683..7f08eff61032 100644 --- a/gcc/config/riscv/riscv-protos.h +++ b/gcc/config/riscv/riscv-protos.h @@ -879,6 +879,7 @@ enum riscv_fusion_pairs RISCV_FUSE_PREINDEX_ST = HOST_WIDE_INT_1U << 20, 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, }; extern bool riscv_macro_fusion_p (void); diff --git a/gcc/testsuite/gcc.target/riscv/fusion-ldst-pair-inc.c b/gcc/testsuite/gcc.target/riscv/fusion-ldst-pair-inc.c new file mode 100644 index 000000000000..44ad262bf17d --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/fusion-ldst-pair-inc.c @@ -0,0 +1,33 @@ +/* { 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_INC" 4 "sched2" { xfail *-*-* } } } */ + +typedef int int32_t; +typedef long int64_t; + +int64_t +test_ld_pair_inc (int64_t *p) +{ + return p[0] + p[1]; +} + +int64_t +test_lw_pair_inc (int32_t *p) +{ + return (int64_t) p[0] + p[1]; +} + +void +test_sd_pair_inc (int64_t *p, int64_t a, int64_t b) +{ + p[0] = a; + p[1] = b; +} + +void +test_sw_pair_inc (int32_t *p, int32_t a, int32_t b) +{ + p[0] = a; + p[1] = b; +} diff --git a/gcc/testsuite/gcc.target/riscv/fusion-memory-rtl-shapes-ldst-pair-unaligned.c b/gcc/testsuite/gcc.target/riscv/fusion-memory-rtl-shapes-ldst-pair-unaligned.c new file mode 100644 index 000000000000..e6a43e705e45 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/fusion-memory-rtl-shapes-ldst-pair-unaligned.c @@ -0,0 +1,64 @@ +/* { dg-do compile { target { rv64 } } } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } } */ +/* { dg-options "-march=rv64gc -mabi=lp64d -O2 -mtune=xt-c9501fdvt -fdump-rtl-sched2-details" } */ +/* { dg-final { scan-rtl-dump-not "RISCV_FUSE_LDST_PAIR_INC" "sched2" } } */ +/* { dg-final { scan-rtl-dump-not "RISCV_FUSE_LDST_PAIR_DEC" "sched2" } } */ + +/* Reject unaligned loads and stores, covering both widths and directions. */ +long __RTL (startwith ("sched2")) +test_unaligned_load_store_pairs (void) +{ +(function "test_unaligned_load_store_pairs" + (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 2)) [0 S4 A16])))) + (cinsn 4 (set (reg:DI a2) + (sign_extend:DI + (mem:SI + (plus:DI (reg:DI a0) + (const_int 6)) [0 S4 A16])))) + (cinsn 5 (set (reg:DI a3) + (mem:DI + (plus:DI (reg:DI a0) + (const_int 12)) [0 S8 A32]))) + (cinsn 6 (set (reg:DI a4) + (mem:DI + (plus:DI (reg:DI a0) + (const_int 4)) [0 S8 A32]))) + (cinsn 7 (set (mem:SI + (plus:DI (reg:DI a0) + (const_int 18)) [0 S4 A16]) + (reg:SI a5))) + (cinsn 8 (set (mem:SI + (plus:DI (reg:DI a0) + (const_int 22)) [0 S4 A16]) + (reg:SI a6))) + (cinsn 9 (set (mem:DI + (plus:DI (reg:DI a0) + (const_int 44)) [0 S8 A32]) + (reg:DI a5))) + (cinsn 10 (set (mem:DI + (plus:DI (reg:DI a0) + (const_int 36)) [0 S8 A32]) + (reg:DI a6))) + (cinsn 11 (use (reg:DI a1))) + (cinsn 12 (use (reg:DI a2))) + (cinsn 13 (use (reg:DI a3))) + (cinsn 14 (use (reg:DI a4))) + (cjump_insn 15 (simple_return)) + (edge-to exit) + ) ;; block 2 + (cbarrier 16) + ) ;; insn-chain + (crtl + (return_rtx (reg/i:DI a0)) + ) ;; crtl +) ;; function "test_unaligned_load_store_pairs" +} diff --git a/gcc/testsuite/gcc.target/riscv/fusion-memory-rtl-shapes-ldst-pair.c b/gcc/testsuite/gcc.target/riscv/fusion-memory-rtl-shapes-ldst-pair.c new file mode 100644 index 000000000000..87b61f2239cf --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/fusion-memory-rtl-shapes-ldst-pair.c @@ -0,0 +1,38 @@ +/* { dg-do compile { target { rv64 } } } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } } */ +/* { dg-options "-march=rv64gc -mabi=lp64d -O2 -mtune=xt-c9501fdvt -fdump-rtl-sched2-details" } */ +/* { dg-final { scan-rtl-dump-times "RISCV_FUSE_LDST_PAIR_INC" 1 "sched2" { xfail *-*-* } } } */ + +/* The second load destination may be the shared base register. Offsets + aligned to the access size need not be aligned to twice that size. */ +long __RTL (startwith ("sched2")) +test_load_pair_inc_second_destination_is_base (void) +{ +(function "test_load_pair_inc_second_destination_is_base" + (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 + (plus:DI (reg:DI a0) + (const_int 16)) [0 S8 A64]))) + (cinsn 5 (use (reg:DI a1))) + (cinsn 6 (use (reg: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_load_pair_inc_second_destination_is_base" +} + +/* { dg-final { scan-rtl-dump-not "RISCV_FUSE_LDST_PAIR_DEC" "sched2" } } */ diff --git a/gcc/testsuite/gcc.target/riscv/fusion-pair-subreg-prera-negative.c b/gcc/testsuite/gcc.target/riscv/fusion-pair-subreg-prera-negative.c new file mode 100644 index 000000000000..27e87979c598 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/fusion-pair-subreg-prera-negative.c @@ -0,0 +1,68 @@ +/* { dg-do compile { target { rv32 } } } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } } */ +/* { dg-options "-march=rv32gc -mabi=ilp32d -O2 -mtune=xt-c9501fdvt -fdump-rtl-sched1-details -fdump-rtl-sched2-details" } */ +/* { dg-final { scan-rtl-dump-not "RISCV_FUSE_LDST_PAIR_(INC|DEC)" "sched1" } } */ +/* { dg-final { scan-rtl-dump-not "RISCV_FUSE_LDST_PAIR_(INC|DEC)" "sched2" } } */ + +/* Opposite halves of a DI pseudo are different RV32 address registers. */ +int __RTL (startwith ("sched1")) +test_pair_low_high (void) +{ +(function "test_pair_low_high" + (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 <0>) (reg:DI a0))) + (cinsn 4 (set (reg:SI <1>) + (mem:SI + (subreg:SI (reg:DI <0>) 0) [0 S4 A32]))) + (cinsn 5 (set (reg:SI <2>) + (mem:SI + (plus:SI (subreg:SI (reg:DI <0>) 4) + (const_int 4)) [0 S4 A32]))) + (cinsn 6 (set (reg:SI a0) + (plus:SI (reg:SI <1>) (reg:SI <2>)))) + (cinsn 7 (use (reg/i:SI a0))) + (cjump_insn 8 (simple_return)) + (edge-to exit) + ) ;; block 2 + (cbarrier 9) + ) ;; insn-chain + (crtl + (return_rtx (reg/i:SI a0)) + ) ;; crtl +) ;; function "test_pair_low_high" +} + +int __RTL (startwith ("sched1")) +test_pair_high_low (void) +{ +(function "test_pair_high_low" + (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 <0>) (reg:DI a0))) + (cinsn 4 (set (reg:SI <1>) + (mem:SI + (subreg:SI (reg:DI <0>) 4) [0 S4 A32]))) + (cinsn 5 (set (reg:SI <2>) + (mem:SI + (plus:SI (subreg:SI (reg:DI <0>) 0) + (const_int 4)) [0 S4 A32]))) + (cinsn 6 (set (reg:SI a0) + (plus:SI (reg:SI <1>) (reg:SI <2>)))) + (cinsn 7 (use (reg/i:SI a0))) + (cjump_insn 8 (simple_return)) + (edge-to exit) + ) ;; block 2 + (cbarrier 9) + ) ;; insn-chain + (crtl + (return_rtx (reg/i:SI a0)) + ) ;; crtl +) ;; function "test_pair_high_low" +} diff --git a/gcc/testsuite/gcc.target/riscv/fusion-pair-subreg-prera-positive.c b/gcc/testsuite/gcc.target/riscv/fusion-pair-subreg-prera-positive.c new file mode 100644 index 000000000000..611101b22c23 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/fusion-pair-subreg-prera-positive.c @@ -0,0 +1,68 @@ +/* { dg-do compile { target { rv32 } } } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } } */ +/* { dg-options "-march=rv32gc -mabi=ilp32d -O2 -mtune=xt-c9501fdvt -fdump-rtl-sched1-details -fdump-rtl-sched2-details" } */ +/* { dg-final { scan-rtl-dump-times "RISCV_FUSE_LDST_PAIR_INC" 2 "sched1" { xfail *-*-* } } } */ +/* { dg-final { scan-rtl-dump-times "RISCV_FUSE_LDST_PAIR_INC" 2 "sched2" { xfail *-*-* } } } */ + +/* Matching low or high parts of the same pseudo remain valid bases. */ +int __RTL (startwith ("sched1")) +test_pair_same_lowpart (void) +{ +(function "test_pair_same_lowpart" + (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 <0>) (reg:DI a0))) + (cinsn 4 (set (reg:SI <1>) + (mem:SI + (subreg:SI (reg:DI <0>) 0) [0 S4 A32]))) + (cinsn 5 (set (reg:SI <2>) + (mem:SI + (plus:SI (subreg:SI (reg:DI <0>) 0) + (const_int 4)) [0 S4 A32]))) + (cinsn 6 (set (reg:SI a0) + (plus:SI (reg:SI <1>) (reg:SI <2>)))) + (cinsn 7 (use (reg/i:SI a0))) + (cjump_insn 8 (simple_return)) + (edge-to exit) + ) ;; block 2 + (cbarrier 9) + ) ;; insn-chain + (crtl + (return_rtx (reg/i:SI a0)) + ) ;; crtl +) ;; function "test_pair_same_lowpart" +} + +int __RTL (startwith ("sched1")) +test_pair_same_highpart (void) +{ +(function "test_pair_same_highpart" + (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 <0>) (reg:DI a0))) + (cinsn 4 (set (reg:SI <1>) + (mem:SI + (subreg:SI (reg:DI <0>) 4) [0 S4 A32]))) + (cinsn 5 (set (reg:SI <2>) + (mem:SI + (plus:SI (subreg:SI (reg:DI <0>) 4) + (const_int 4)) [0 S4 A32]))) + (cinsn 6 (set (reg:SI a0) + (plus:SI (reg:SI <1>) (reg:SI <2>)))) + (cinsn 7 (use (reg/i:SI a0))) + (cjump_insn 8 (simple_return)) + (edge-to exit) + ) ;; block 2 + (cbarrier 9) + ) ;; insn-chain + (crtl + (return_rtx (reg/i:SI a0)) + ) ;; crtl +) ;; function "test_pair_same_highpart" +}
