Improve saturating truncation sequences for RISC-V when Zbb is enabled
Optimizes saturating truncation for RISC-V by using the Zbb extension's `umin` instruction.
This commit enhances RISC-V code generation for saturating unsigned truncation operations by utilizing the umin instruction from the Zbb extension. When truncating a larger integer type to a smaller one with saturation, the compiler now loads the maximum value of the target type (e.g., 255 for QI) and performs a umin operation with the input. This approach is simpler and more efficient than previous methods, reducing instruction count for operations like DI->QI truncation, as demonstrated in tests for the x264 benchmark.
In Details
This change optimizes saturating unsigned truncation on RISC-V when the Zbb extension is enabled. The riscv_expand_ustrunc function in riscv.cc is modified to generate a sequence using umin for saturation. Instead of complex conditional logic or register-to-register moves, it loads the maximum value of the destination type into a register and then performs a umin with the source value. This transformation is applied to DI->{SI,HI,QI} and SI->{HI,QI} truncations. The optimization is performed in the expander rather than combine or isel for simplicity and effectiveness, as findi…
- RISC-V
- An open-standard instruction set architecture (ISA) based on reduced instruction set computing principles, known for its modularity and extensibility.
- Zbb extension
- A standard extension for the RISC-V architecture that adds bit manipulation instructions, including unsigned minimum (
umin). - saturating truncation
- An operation that converts a value from a larger data type to a smaller one, clamping the result to the maximum or minimum representable value of the smaller type if the original value exceeds those bounds.
- DI
- Double Integer, typically a 64-bit integer type in GCC.
- QI
- Quad Integer, typically an 8-bit integer type in GCC.
- umin
- Unsigned Minimum, an instruction that returns the smaller of two unsigned integer operands.