https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124955
--- Comment #5 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jeff Law <[email protected]>: https://gcc.gnu.org/g:046bc3484c90a25fb09c851d6afac13b790bb20c commit r17-412-g046bc3484c90a25fb09c851d6afac13b790bb20c Author: Jeff Law <[email protected]> Date: Fri May 8 11:40:29 2026 -0600 [V2][RISC-V][PR target/124955] Utilize slliw for some left shifted signed bitfield extractions Some functional change as was already posted, this time with a testcase. Given it's been in my tester and through the pre-commit CI system, I'm going forward now. -- So as the PR notes, this is an attempt to squeeze out some instructions from a hot part of leela, the random number generator in particular. typedef unsigned int uint32; uint32 random(uint32 s1) { const uint32 mask = 0xffffffff; s1 = (((s1 & 0xFFFFFFFEU) << 12) & mask); return s1; } Generates this RISC-V code: slli a5,a0,44 # 25 [c=4 l=4] ashldi3 srai a0,a5,44 # 26 [c=4 l=4] ashrdi3 andi a0,a0,-2 # 21 [c=4 l=4] *anddi3/1 slli a0,a0,12 # 22 [c=4 l=4] ashldi3 But this is an equivalent sequence: andi a0, a0, -2 slliw a0, a0, 12 The key is realizing that the the first two statements are just a sign extended bitfield of length 20. That ultimately gets shifted left 12 bits. 20+12 = 32, so we can at least conceptually use slliw (shift left sign extending result from SI to DI). The andi just turns off the low bit. Given a sign extracted bitfield starting at bit 0, of size N that is then left shifted by M where N+M == 32 is a natural slliw instruction. However, when I tried to recognize that and generate the slliw form I saw code quality regressions that didn't look particularly reasonable to try and fix. So we want to be more selective about recognizing that idiom. So we recognize it when we subsequently mask off some bits and the mask can be encoded via andi. This likely could be extended to other logical operations that don't ultimately affect the SI sign bit. PR target/124955 gcc/ * config/riscv/riscv.md (masked shifted bitfield extraction): New splitter to utilize slliw to eliminate the need for sign extnesion. gcc/testsuite/ * gcc.target/riscv/pr124955.c: New test
