https://gcc.gnu.org/g:ae820c0e4c2e13177bdb5d811c9a76f6fa955248
commit r17-6025-gae820c0e4c2e13177bdb5d811c9a76f6fa955248 Author: Jeff Law <[email protected]> Date: Sat Nov 7 22:21:26 2026 -0700 [RISC-V] Drop unnecessary shifting when upper bits are not read after bitfield extraction Another small improvement to x264 discovered by LLM review of execution block data. In this case we've got a shift pair that creates a bit field of 32 or more bits. However, subsequent insns (PLUS in this case) never read beyond the 32nd bit. Knowing those upper bits are do-not-cares we can simplify the bitfield extraction and eliminate one of the shifts. This can almost certainly be extended to cover other ops that have widening forms if they show up in practice. ext-dce doesn't directly help here, though perhaps it could be taught to use its data for the same kind of optimization. Given I'm not at all a fan of ext-dce implementation, I'm not inclined to add more functionality to its current implementation. This is in the noise for x264, but given the analysis has been done and the pattern was trivial and reasonably generic I'm going ahead with it on the hopes that it matters somewhere else ;-) Regression tested on riscv32-elf and riscv64-elf. Bootstrapped and regression tested on the c920. K3 is in flight. K1 is queued. Assisted-By: Claude for investigation and testcase. gcc/ * config/riscv/riscv.md (bitfield extraction splitter): New splitter for cases when high bits of a bitfield extraction are never read. gcc/testsuite/ * gcc.target/riscv/shift-shift-8.c: New test. Diff: --- gcc/config/riscv/riscv.md | 19 +++++++++++ gcc/testsuite/gcc.target/riscv/shift-shift-8.c | 45 ++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index f715906c2467..1fabe9260e44 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -3336,6 +3336,25 @@ operands[6] = GEN_INT (leading); }) +;; The low 32 bits of a zero_extract of at least 32 bits are the low 32 bits +;; of a right shift, which addiw consumes directly. This arises when a shift +;; left followed by a logical shift right feeds a 32-bit add. Narrower fields +;; do not qualify as the bits above the field must read as zero. +(define_split + [(set (match_operand:DI 0 "register_operand") + (sign_extend:DI + (plus:SI + (subreg:SI + (zero_extract:DI (match_operand:DI 1 "register_operand") + (match_operand 2 "const_int_operand") + (match_operand 3 "const_int_operand")) 0) + (match_operand:SI 4 "arith_operand")))) + (clobber (match_operand:DI 5 "register_operand"))] + "TARGET_64BIT && INTVAL (operands[2]) >= 32 && INTVAL (operands[3]) >= 1" + [(set (match_dup 5) (lshiftrt:DI (match_dup 1) (match_dup 3))) + (set (match_dup 0) + (sign_extend:DI (plus:SI (subreg:SI (match_dup 5) 0) (match_dup 4))))]) + ;; Handle logical AND feeding an equality test against zero where an operand ;; to the AND is a constant requiring synthesis. Because we only care about ;; zero/nonzero state after the AND, we may be able to shift both operands diff --git a/gcc/testsuite/gcc.target/riscv/shift-shift-8.c b/gcc/testsuite/gcc.target/riscv/shift-shift-8.c new file mode 100644 index 000000000000..66ba7d91f423 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/shift-shift-8.c @@ -0,0 +1,45 @@ +/* { dg-do compile { target rv64 } } */ +/* { dg-options "-O2 -march=rv64gc -mabi=lp64d" } */ + +/* These all have field extractions where the field is 32 or more bits, but + any bits beyond the SI sign bit are never used. Knowing those upper bits + are never used, we can simplify the shift sequences a bit. */ + +int +f32 (unsigned long s) +{ + return (int) ((s << 24) >> 32) + 47; +} + +int +f33 (unsigned long s) +{ + return (int) ((s << 24) >> 31) + 47; +} + +int +f40 (unsigned long s) +{ + return (int) ((s << 16) >> 24) + 47; +} + +int +f48 (unsigned long s) +{ + return (int) ((s << 8) >> 16) + 47; +} + +int +f62 (unsigned long s) +{ + return (int) ((s << 1) >> 2) + 47; +} + +int +freg (unsigned long s, int y) +{ + return (int) ((s << 24) >> 32) + y; +} + +/* { dg-final { scan-assembler-times "\tsrli\t" 6 } } */ +/* { dg-final { scan-assembler-not "\tslli\t" } } */
