https://gcc.gnu.org/g:73225095de4c3b873841aea8a73cc14d2f067b72
commit r17-5900-g73225095de4c3b873841aea8a73cc14d2f067b72 Author: Jeff Law <[email protected]> Date: Tue Nov 3 09:01:31 2026 -0700 [RISC-V] Improve saturating truncation sequences for RISC-V when Zbb is enabled In addition to machine description scanning for bugs, I've also got LLMs scurrying around spec data looking for inefficient code generation issues on RISC-V. This is a fix for one such finding for x264. It saves a bit over 600m instructions x264 input #1 on RISC-V. So it's not huge, but it's not trivial either. We're interested in optimizing the unsigned truncation with saturation path. So if we're truncating with saturation from DI to QI we're going to end up with a value 0..255. We can load up the largest value in the range of the output mode into a register (255 in this case), then issue a umin instruction. If the other input was 0..255, then we get its value, otherwise we get 255. This naturally relies of the Zbb extension. Tackling in combine wasn't really feasible as we often need to see too many instructions to make the transformation. I could have tackled in isel with something conditional on a umin opcode. But fixing the RISC-V expander was just too easy :-) An implementation note. For DI->SI we sign extend the inputs to match LOAD_EXTEND_OP in the hope that lets us combine the extension with the memory load. Otherwise we zero extend. Either is valid. It's a heuristic. I'm sure it's not going to be optimal somewhere. The test verifies the full set of useful sat-trunc cases. DI->{SI,HI,QI} and SI->{HI,QI}. It checks that the optimization fires and there's run tests as well. Every case improves by at least 1 instruction. Assisted-By: Claude for discovery and testcase generation. Tested on riscv32-elf and riscv64-elf. Bootstrap & regression tested on K3 and c920. Pushing to the trunk. gcc/ * config/riscv/riscv.cc (riscv_expand_ustrunc): Generate umin based sequence when Zbb is enabled. gcc/testsuite * gcc.target/riscv/ustrunc-zbb-1.c: New test. * gcc.target/riscv/ustrunc-zbb-run.c: New test. * gcc.target/riscv/ustrunc-zbb.h: New common file for ustrunc tests. Diff: --- gcc/config/riscv/riscv.cc | 34 +++++++++++----- gcc/testsuite/gcc.target/riscv/ustrunc-zbb-1.c | 13 ++++++ gcc/testsuite/gcc.target/riscv/ustrunc-zbb-run.c | 50 ++++++++++++++++++++++++ gcc/testsuite/gcc.target/riscv/ustrunc-zbb.h | 37 ++++++++++++++++++ 4 files changed, 125 insertions(+), 9 deletions(-) diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 7aca11d4daac..c827aee918ab 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -15111,23 +15111,39 @@ riscv_expand_ustrunc (rtx dest, rtx src) gcc_assert (precision < 64); uint64_t max = ((uint64_t)1u << precision) - 1u; - rtx xmode_src = riscv_extend_to_xmode_reg (src, GET_MODE (src), ZERO_EXTEND); rtx xmode_dest = gen_reg_rtx (Xmode); - rtx xmode_lt = gen_reg_rtx (Xmode); /* Step-1: max = half truncated max */ emit_move_insn (xmode_max, gen_int_mode (max, Xmode)); - /* Step-2: lt = src < max */ - riscv_emit_binary (LTU, xmode_lt, xmode_src, xmode_max); + /* When possible, emit UMIN based sequence. It's inherently smaller and more + and by being smaller may allow combine to work better as a bonus. */ + if (TARGET_ZBB) + { + machine_mode smode = GET_MODE (src); + rtx xmode_src + = riscv_extend_to_xmode_reg (src, smode, + (smode == SImode + ? SIGN_EXTEND + : ZERO_EXTEND)); + riscv_emit_binary (UMIN, xmode_dest, xmode_src, xmode_max); + } + else + { + rtx xmode_src = riscv_extend_to_xmode_reg (src, GET_MODE (src), ZERO_EXTEND); + rtx xmode_lt = gen_reg_rtx (Xmode); + + /* Step-2: lt = src < max */ + riscv_emit_binary (LTU, xmode_lt, xmode_src, xmode_max); - /* Step-3: lt = lt - 1 */ - riscv_emit_binary (PLUS, xmode_lt, xmode_lt, CONSTM1_RTX (Xmode)); + /* Step-3: lt = lt - 1 */ + riscv_emit_binary (PLUS, xmode_lt, xmode_lt, CONSTM1_RTX (Xmode)); - /* Step-4: xmode_dest = lt | src */ - riscv_emit_binary (IOR, xmode_dest, xmode_lt, xmode_src); + /* Step-4: xmode_dest = lt | src */ + riscv_emit_binary (IOR, xmode_dest, xmode_lt, xmode_src); + } - /* Step-5: dest = xmode_dest */ + /* Final: dest = xmode_dest */ emit_move_insn (dest, gen_lowpart (mode, xmode_dest)); } diff --git a/gcc/testsuite/gcc.target/riscv/ustrunc-zbb-1.c b/gcc/testsuite/gcc.target/riscv/ustrunc-zbb-1.c new file mode 100644 index 000000000000..3175889415c6 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/ustrunc-zbb-1.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zbb -mabi=lp64d -O2 -fdump-tree-optimized" { target { rv64 } } } */ +/* { dg-options "-march=rv32gc_zbb -mabi=ilp32d -O2 -fdump-tree-optimized" { target { rv32 } } } */ + +#include "ustrunc-zbb.h" + +USTRUNC_PAIRS (DEF_SAT) + +/* { dg-final { scan-tree-dump-times ".SAT_TRUNC " 6 "optimized" { target { rv64 } } } } */ +/* { dg-final { scan-tree-dump-times ".SAT_TRUNC " 3 "optimized" { target { rv32 } } } } */ +/* { dg-final { scan-assembler-times "\\mminu\\M" 6 { target { rv64 } } } } */ +/* { dg-final { scan-assembler-times "\\mminu\\M" 3 { target { rv32 } } } } */ +/* { dg-final { scan-assembler-not "\\msltu\\M|\\msltiu\\M|\\mneg\\M|\\mor\\M" } } */ diff --git a/gcc/testsuite/gcc.target/riscv/ustrunc-zbb-run.c b/gcc/testsuite/gcc.target/riscv/ustrunc-zbb-run.c new file mode 100644 index 000000000000..a8abce5fa950 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/ustrunc-zbb-run.c @@ -0,0 +1,50 @@ +/* { dg-do run { target { rv32 || rv64 } } } */ +/* { dg-options "-march=rv64gc_zbb -mabi=lp64d -O2" { target { rv64 } } } */ +/* { dg-options "-march=rv32gc_zbb -mabi=ilp32d -O2" { target { rv32 } } } */ + +#include "ustrunc-zbb.h" + +USTRUNC_PAIRS (DEF_SAT) +USTRUNC_PAIRS (DEF_SAT_TRUNC) + +/* Reference. The volatile load keeps the compiler from recognizing the + comparison as a saturation. */ +uint64_t __attribute__((noinline)) +ref (uint64_t x, uint64_t max) +{ + volatile uint64_t m = max; + return x > m ? m : x; +} + +static const uint64_t vals[] = { + 0, 1, 2, + 0x7e, 0x7f, 0x80, 0x81, 0xfe, 0xff, 0x100, 0x101, + 0x7ffe, 0x7fff, 0x8000, 0x8001, 0xfffe, 0xffff, 0x10000, 0x10001, + 0x7ffffffe, 0x7fffffff, 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff, + 0x100000000, 0x100000001, 0x1000000ff, 0x1ffffffff, + 0x7fffffffffffffff, 0x8000000000000000, 0x8000000000000001, + 0xfffffffffffffffe, 0xffffffffffffffff, + 0xffffffff80000000, 0xffffffff7fffffff, 0xffffffffffffff00, + 0xffffffff00000100, 0x12345678abcdef01 +}; + +#define CHECK(DST, SRC) \ + { \ + SRC s = (SRC)v; \ + DST e = (DST)ref ((uint64_t)s, (uint64_t)(DST)(-1)); \ + if (sat_##SRC##_##DST (s) != e) \ + __builtin_abort (); \ + if (sat_trunc_##SRC##_##DST (v) != e) \ + __builtin_abort (); \ + } + +int +main (void) +{ + for (unsigned i = 0; i < sizeof (vals) / sizeof (vals[0]); i++) + { + uint64_t v = vals[i]; + USTRUNC_PAIRS (CHECK) + } + return 0; +} diff --git a/gcc/testsuite/gcc.target/riscv/ustrunc-zbb.h b/gcc/testsuite/gcc.target/riscv/ustrunc-zbb.h new file mode 100644 index 000000000000..b333f32c4552 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/ustrunc-zbb.h @@ -0,0 +1,37 @@ +#include <stdint-gcc.h> + +/* Every source/destination mode pair accepted by the ustrunc expanders. + The HI/SI/DI sources are uint16_t/uint32_t/uint64_t; DI exists only on + RV64. */ +#if __riscv_xlen == 64 +#define USTRUNC_PAIRS(X) \ + X (uint8_t, uint16_t) \ + X (uint16_t, uint32_t) \ + X (uint8_t, uint32_t) \ + X (uint32_t, uint64_t) \ + X (uint16_t, uint64_t) \ + X (uint8_t, uint64_t) +#else +#define USTRUNC_PAIRS(X) \ + X (uint8_t, uint16_t) \ + X (uint16_t, uint32_t) \ + X (uint8_t, uint32_t) +#endif + +/* The source is passed as its own type. */ +#define DEF_SAT(DST, SRC) \ +DST __attribute__((noinline)) \ +sat_##SRC##_##DST (SRC x) \ +{ \ + return x > (SRC)(DST)(-1) ? (DST)(-1) : (DST)x; \ +} + +/* The source is built by truncating a 64-bit value, so the upper bits of its + register are not guaranteed to be an extension of the value. */ +#define DEF_SAT_TRUNC(DST, SRC) \ +DST __attribute__((noinline)) \ +sat_trunc_##SRC##_##DST (uint64_t y) \ +{ \ + SRC x = (SRC)y; \ + return x > (SRC)(DST)(-1) ? (DST)(-1) : (DST)x; \ +}
