GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc/riscv Performance Win

RISC-V: Use `bseti` over `li+add` for setting bits and improve instruction splitting.

RISC-V compiler replaces `li+add` with `bseti` for setting bits and improves instruction splitting to eliminate sign extensions.

This commit enhances RISC-V code generation by replacing sequences of lui (load immediate) and addi (add immediate) with bseti when setting a single bit. This is achieved by adjusting the arithmetic operation expander to handle single-bit constants, leading to more efficient instruction sequences. Additionally, a new, generic instruction splitter is introduced to intelligently handle complex expression transformations, specifically eliminating unnecessary sign extensions that previously degraded code quality.

In Details

This change modifies the RISC-V backend's instruction selection and RTL expansion. It specifically targets the scenario where a constant like 0x8000 needs to be added to a register. If the target bit is clear in the existing value, bseti (set bit immediate) can be used instead of synthesizing the constant with lui and then adding. This requires enhancing the arithmetic operation expander in riscv.md to better recognize and utilize single-bit constants for bitwise OR operations. The second part of the patch introduces a new splitter to handle complex expression trees, such as `sign_ext…

For Context
bseti
An instruction on some architectures (like RISC-V) that sets a specific bit in a register to 1.
lui
Load Upper Immediate. An instruction that loads a 16-bit immediate value into the upper 16 bits of a register, typically used to synthesize larger constants.
addi
Add Immediate. An instruction that adds an immediate value to a register.
RTL
Register Transfer Language. An intermediate representation used by GCC to describe operations on registers.
splitter
In GCC's instruction selection, a pattern or function that breaks down a complex intermediate representation (like RTL) into smaller, matchable RTL components for instruction generation.
sign extend
An operation that expands a smaller signed integer to a larger integer type by replicating the sign bit (the most significant bit) to the new higher-order bits.
Filed Under: risc-voptimizationcompiler