https://gcc.gnu.org/g:b9b7ba1475201499aae35b1aaf7a797d5a9c4d6f
commit r17-6016-gb9b7ba1475201499aae35b1aaf7a797d5a9c4d6f Author: Jeff Law <[email protected]> Date: Sat Nov 7 08:09:06 2026 -0700 [RISC-V] Add splitter to enable narrowing load in a load->shift->store sequence I'm not terribly happy with this case. The basic idea here is we're seeing a load into a wider mode like SI, a right shift by 8 positions, then a narrow store of that result (HI). If the target supports fast unaligned access, then that's just a HI load followed by a HI store. Obviously this generalizes and appears to be a run-of-the-mill 3->2 splitter. Combine is able to simplify the sequence from the wide load, shift, narrow store to just a narrow load + narrow store. It takes care of adjusting the address, mode switches, etc. So the matching rtl is just: (set (mem) (mem)). That's both the beauty and worry I have. It's great that combine was able to see through the sequence and boil it down to its essence, but at that the same time that leaves us with a splitter that matches much more generally than I would have preferred. Checking volatile on the source operand is critical since the load itself gets narrowed. I couldn't convince myself that the same couldn't (in theory) happen with the destination operand if it were being stored back to a location and only changed a subset of bytes from a prior known value. So while it's probably not strictly necessary today, better safe than sorry. Another 13M (drop in the bucket) dynamic instructions gone. Bootstrapped and regression tested on K3 and c920, K1 in flight. Also regression tested on riscv64-elf and riscv32-elf. Pushing to the trunk. gcc/ * config/riscv/riscv.md (load->store splitter): New splitter for capturing narrowing loads in load -> shift -> narrow store sequence. gcc/testsuite * gcc.target/riscv/load-no-shift.c: New test. Diff: --- gcc/config/riscv/riscv.md | 23 +++++++++++++ gcc/testsuite/gcc.target/riscv/load-no-shift.c | 47 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index bc8e7106e52f..e6572f629abb 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -5515,6 +5515,29 @@ } ) +;; We want to capture things like loading a 32 bit object, right shifting +;; it by 8 bits and storing it as a 16 bit object. Naively that would +;; generate the 32-bit load, right shift and 16-bit store. But if we can +;; handle unaligned accesses, then we can adjust the address of the load +;; and narrow it to 16 bits. +;; +;; Combine has actually done all the hard work to adjust the source and +;; and destination operands. So we just need to load from memory into +;; the appropriately sized temporary and store from that temporary into +;; memory. +(define_split + [(set (match_operand:SUBX 0 "memory_operand") + (match_operand:SUBX 1 "memory_operand"))] + "(!MEM_VOLATILE_P (operands[0]) + && !MEM_VOLATILE_P (operands[1]) + && (<MODE>mode == QImode || !riscv_slow_unaligned_access_p))" + [(set (match_dup 2) (match_dup 1)) + (set (match_dup 0) (match_dup 2))] +{ + operands[2] = gen_reg_rtx (<MODE>mode); +}) + + (include "bitmanip.md") (include "crypto.md") (include "sync.md") diff --git a/gcc/testsuite/gcc.target/riscv/load-no-shift.c b/gcc/testsuite/gcc.target/riscv/load-no-shift.c new file mode 100644 index 000000000000..5ee077146eb0 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/load-no-shift.c @@ -0,0 +1,47 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mtune=generic-ooo" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" } } */ + +/* Only a narrow field of the loaded word is stored, so a load of the store's + width at the adjusted offset is enough. */ + +void +f_qi (unsigned char *d, const unsigned int *s) +{ + *d = *s >> 8; +} + +void +f_hi_8 (unsigned short *d, const unsigned int *s) +{ + *d = *s >> 8; +} + +void +f_hi_16 (unsigned short *d, const unsigned int *s) +{ + *d = *s >> 16; +} + +#if __riscv_xlen == 64 +void +f_si_8 (unsigned int *d, const unsigned long *s) +{ + *d = *s >> 8; +} + +void +f_si_16 (unsigned int *d, const unsigned long *s) +{ + *d = *s >> 16; +} +#endif + +/* Each function must be a single load, a single store and a return. */ +/* { dg-final { scan-assembler-times "\tl\[bhw\]u?\t" 3 { target { rv32 } } } } */ +/* { dg-final { scan-assembler-times "\ts\[bhw\]\t" 3 { target { rv32 } } } } */ +/* { dg-final { scan-assembler-times "\tret\n" 3 { target { rv32 } } } } */ +/* { dg-final { scan-assembler-times "\tl\[bhw\]u?\t" 5 { target { rv64 } } } } */ +/* { dg-final { scan-assembler-times "\ts\[bhw\]\t" 5 { target { rv64 } } } } */ +/* { dg-final { scan-assembler-times "\tret\n" 5 { target { rv64 } } } } */ +/* { dg-final { scan-assembler-not "\n\t(?!l\[bhw\]u?\t|s\[bhw\]\t|ret\n)\[a-z\]" } } */
