https://gcc.gnu.org/g:44b0b38f5cf848d73a461cb16029ba71bd44276f
commit r17-6396-g44b0b38f5cf848d73a461cb16029ba71bd44276f Author: Jin Ma <[email protected]> Date: Wed Aug 12 18:57:03 2026 +0800 RISC-V: Add SRLI_ADD macro-fusion recognition Recognize SRLI/SRLIW followed by a matching-width ADD-type operation when the effective right shift amount is in the range 0 to 3 and the required producer-consumer dependency holds. Reuse the common shift classifier and the existing ADD classifier's wordness output. SH1ADD, SH2ADD and SH3ADD instead correspond to left shifts by 1, 2 and 3 followed by ADD. 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_fuse_srli_add): New function. (riscv_fusion_table): Add RISCV_FUSE_SRLI_ADD. * config/riscv/riscv-protos.h (enum riscv_fusion_pairs): Add RISCV_FUSE_SRLI_ADD. gcc/testsuite/ChangeLog: * gcc.target/riscv/fusion-srli-add.c: New test. * gcc.target/riscv/fusion-srli-add-zero-rtl.c: Likewise. * gcc.target/riscv/fusion-word-positive-rtl-srli-add.c: Likewise. Signed-off-by: Jin Ma <[email protected]> Diff: --- gcc/config/riscv/riscv-fusion.cc | 42 ++++++++++++++ gcc/config/riscv/riscv-protos.h | 1 + .../gcc.target/riscv/fusion-srli-add-zero-rtl.c | 52 +++++++++++++++++ gcc/testsuite/gcc.target/riscv/fusion-srli-add.c | 38 ++++++++++++ .../riscv/fusion-word-positive-rtl-srli-add.c | 67 ++++++++++++++++++++++ 5 files changed, 200 insertions(+) diff --git a/gcc/config/riscv/riscv-fusion.cc b/gcc/config/riscv/riscv-fusion.cc index 21e64042f525..61d3b185ba78 100644 --- a/gcc/config/riscv/riscv-fusion.cc +++ b/gcc/config/riscv/riscv-fusion.cc @@ -1517,6 +1517,46 @@ riscv_fuse_slli_srli (rtx_insn *prev, rtx_insn *curr) return riscv_fuse_shift_pair_p (prev, curr, true, false); } +/* Check for RISCV_FUSE_SRLI_ADD fusion. + prev (srli/srliw) == (set (reg rd1) (lshiftrt (reg rs1) + (const_int shamt))) + curr (one of the following): + (add) == (set (reg rd2) (plus (reg rd1) (reg rs2))) + (addw) == (set (reg rd2) + (sign_extend (plus (reg rd1) (reg rs2)))) + (add.uw) == (set (reg rd2) (plus (zero_extend (reg rd1)) + (reg rs2))) + + Constraints: + rd1 == rd2 + both instructions are word forms or both are non-word forms + the effective right shift amount is in the range 0 to 3. + + SH1ADD, SH2ADD and SH3ADD instead add a value shifted left by 1, 2 or 3 + bits, scaling an index for 2-, 4- or 8-byte elements. They correspond + to SLLI + ADD, not this SRLI + ADD fusion. */ + +static bool +riscv_fuse_srli_add (rtx_insn *prev, rtx_insn *curr) +{ + rtx prev_set, curr_set; + if (!riscv_fuse_sets_p (prev, curr, &prev_set, &curr_set) + || get_attr_type (prev) != TYPE_SHIFT) + return false; + + bool shift_word_p, add_word_p; + HOST_WIDE_INT shift_amount; + if (!riscv_set_is_shift_type_p (prev_set, LSHIFTRT, &shift_word_p, + &shift_amount) + || !riscv_insn_is_add_type_p (curr, &add_word_p) + || shift_word_p != add_word_p + || !riscv_fuse_same_dest_p (prev_set, curr_set, true)) + return false; + + unsigned int mask = shift_word_p ? 0x1f : (TARGET_64BIT ? 0x3f : 0x1f); + return (shift_amount & mask) <= 3; +} + /* Check for RISCV_FUSE_B_ALUI fusion. prev/curr (one of the following pairs): prev (orc.b) == (set (reg rd1) @@ -1787,6 +1827,8 @@ static const struct riscv_fusion_entry riscv_fusion_table[] = riscv_fuse_bfext, "RISCV_FUSE_BFEXT" }, { RISCV_FUSE_SLLI_SRLI, riscv_fuse_slli_srli, "RISCV_FUSE_SLLI_SRLI" }, + { RISCV_FUSE_SRLI_ADD, + riscv_fuse_srli_add, "RISCV_FUSE_SRLI_ADD" }, { RISCV_FUSE_B_ALUI, riscv_fuse_b_alui, "RISCV_FUSE_B_ALUI" }, { RISCV_FUSE_SUB_SEQZ, diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h index 58b437dad152..be9bd04b1ede 100644 --- a/gcc/config/riscv/riscv-protos.h +++ b/gcc/config/riscv/riscv-protos.h @@ -875,6 +875,7 @@ enum riscv_fusion_pairs RISCV_FUSE_ANDI_ADD = HOST_WIDE_INT_1U << 16, RISCV_FUSE_LOGIC_LOGIC = HOST_WIDE_INT_1U << 17, RISCV_FUSE_SLLI_SRLI = HOST_WIDE_INT_1U << 18, + RISCV_FUSE_SRLI_ADD = HOST_WIDE_INT_1U << 19, }; extern bool riscv_macro_fusion_p (void); diff --git a/gcc/testsuite/gcc.target/riscv/fusion-srli-add-zero-rtl.c b/gcc/testsuite/gcc.target/riscv/fusion-srli-add-zero-rtl.c new file mode 100644 index 000000000000..05cc3de9c4e6 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/fusion-srli-add-zero-rtl.c @@ -0,0 +1,52 @@ +/* Preserve zero shifts through sched2 to check the lower bound. */ +/* { 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" } */ +/* No tune enables these fusion pairs yet. */ +/* { dg-final { scan-rtl-dump-times {(?s);; Function test_srli(?:_add|w_addw)_zero (?:(?!;; Function).)*RISCV_FUSE_SRLI_ADD} 2 "sched2" { xfail *-*-* } } } */ + +long __RTL (startwith ("sched2")) +test_srli_add_zero (void) +{ +(function "test_srli_add_zero" + (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) + (lshiftrt:DI (reg:DI a1) + (const_int 0)))) + (cinsn 4 (set (reg:DI a0) + (plus:DI (reg:DI a0) + (reg:DI a2)))) + (cinsn 5 (use (reg/i:DI a0))) + (cjump_insn 6 (simple_return)) + (edge-to exit)) + (cbarrier 7)) + (crtl (return_rtx (reg/i:DI a0)))) +} + +long __RTL (startwith ("sched2")) +test_srliw_addw_zero (void) +{ +(function "test_srliw_addw_zero" + (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) + (sign_extend:DI + (lshiftrt:SI (reg:SI a1) + (const_int 0))))) + (cinsn 4 (set (reg:DI a0) + (sign_extend:DI + (plus:SI (reg:SI a0) + (reg:SI a2))))) + (cinsn 5 (use (reg/i:DI a0))) + (cjump_insn 6 (simple_return)) + (edge-to exit)) + (cbarrier 7)) + (crtl (return_rtx (reg/i:DI a0)))) +} diff --git a/gcc/testsuite/gcc.target/riscv/fusion-srli-add.c b/gcc/testsuite/gcc.target/riscv/fusion-srli-add.c new file mode 100644 index 000000000000..4d7bf16de719 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/fusion-srli-add.c @@ -0,0 +1,38 @@ +/* { dg-do compile { target { rv64 } } } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-O3" "-O[sgz]" "-flto" } } */ +/* { dg-options "-march=rv64gc_zba -mabi=lp64d -mtune=xt-c9501fdvt -O2 -fdump-rtl-sched2-details" } */ +/* No tune enables these fusion pairs yet. */ +/* { dg-final { scan-rtl-dump-times {(?s);; Function test_srli(?:_add|w_addw|_adduw)_[123] (?:(?!;; Function).)*RISCV_FUSE_SRLI_ADD} 9 "sched2" { xfail *-*-* } } } */ +/* { dg-final { scan-rtl-dump-not {(?s);; Function test_srli(?:_add|w_addw|_adduw)_(?:4|31) (?:(?!;; Function).)*RISCV_FUSE_SRLI_ADD} "sched2" } } */ + +typedef long int64_t; +typedef unsigned long uint64_t; +typedef int int32_t; +typedef unsigned int uint32_t; + +/* Check each accepted ADD form. Shift amounts 1, 2 and 3 should fuse; + 4 and 31 should not. A fixed RTL test covers shifts by zero, which C + optimization would remove. */ +#define TEST_SRLI_ADD(SHIFT) \ + int64_t \ + test_srli_add_##SHIFT (uint64_t a, int64_t b) \ + { \ + return (int64_t) (a >> SHIFT) + b; \ + } \ + int32_t \ + test_srliw_addw_##SHIFT (uint32_t a, int32_t b) \ + { \ + return (int32_t) (a >> SHIFT) + b; \ + } \ + uint64_t \ + test_srli_adduw_##SHIFT (uint64_t a, uint64_t b) \ + { \ + return (uint64_t) (uint32_t) (a >> SHIFT) + b; \ + } + +TEST_SRLI_ADD (1) +TEST_SRLI_ADD (2) +TEST_SRLI_ADD (3) +TEST_SRLI_ADD (4) +TEST_SRLI_ADD (31) + diff --git a/gcc/testsuite/gcc.target/riscv/fusion-word-positive-rtl-srli-add.c b/gcc/testsuite/gcc.target/riscv/fusion-word-positive-rtl-srli-add.c new file mode 100644 index 000000000000..a05e4e83752f --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/fusion-word-positive-rtl-srli-add.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 -mtune=xt-c9501fdvt -O2 -fdump-rtl-sched2-details" } */ +/* { dg-final { scan-rtl-dump-times "RISCV_FUSE_SRLI_ADD" 2 "sched2" { xfail *-*-* } } } */ + +/* Raw SImode srli followed by a same-source addw should fuse. */ +long __RTL (startwith ("sched2")) +test_raw_srliw_extended_addw (void) +{ +(function "test_raw_srliw_extended_addw" + (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:SI a0) + (lshiftrt:SI (reg:SI a1) + (const_int 2)))) + (cinsn 4 (set (reg:DI a0) + (sign_extend:DI + (plus:SI (reg:SI a0) + (reg:SI a0))))) + (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) + ) ;; return_rtx + ) ;; crtl +) ;; function "test_raw_srliw_extended_addw" +} +/* Zero-extended srliw followed by addw expressed via truncate. */ +long __RTL (startwith ("sched2")) +test_lowpart_srliw_addw (void) +{ +(function "test_lowpart_srliw_addw" + (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) + (zero_extend:DI + (lshiftrt:SI (reg:SI a1) + (const_int 2))))) + (cinsn 4 (set (reg:DI a0) + (sign_extend:DI + (truncate:SI + (plus:DI (reg:DI a0) + (reg:DI a2)))))) + (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) + ) ;; return_rtx + ) ;; crtl +) ;; function "test_lowpart_srliw_addw" +}
