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

Reorder RISC-V instructions for adding nontrivial constants to registers.

RISC-V code generation for `(plus (reg) (const_int))` is reordered to improve instruction scheduling and performance.

This commit reorders the instruction sequence generated by GCC for (plus (reg) (const_int)) on RISC-V when dealing with nontrivial constants. The previous sequence, often involving lui, addi, and add, could lead to suboptimal scheduling, especially when the result was used in memory references. The new order, typically lui, add, addi, improves instruction fusion and reduces execution cycles, yielding a performance gain of over 2% in benchmarks like x264.

In Details

On RISC-V, adding a large constant to a register often involves a lui (load upper immediate) followed by an addi (add immediate). The synthesize_add function in gcc/config/riscv/riscv.cc is responsible for generating this sequence. This patch reorders the instructions generated by synthesize_add when a 'good' synthesis (e.g., folding into a single instruction) isn't found. The new sequence prioritizes lui followed by add, then addi, which benefits instruction scheduling and folding, particularly for memory offsets, and avoids a recent regression.

For Context
RISC-V
An open-standard instruction set architecture (ISA) based on Reduced Instruction Set Computing principles.
lui
Load Upper Immediate. A RISC-V instruction that loads a 20-bit immediate value into the upper 20 bits of a register, zeroing the lower 12 bits.
addi
Add Immediate. A RISC-V instruction that adds a 12-bit immediate value to a register.
synthesize_add
A GCC internal function that generates machine instructions for performing an addition operation.
instruction scheduling
The process of reordering machine instructions to improve processor pipeline utilization and reduce execution time.
folding
An optimization technique where an operation with constant operands is performed at compile time, or where multiple operations are combined into a single, more efficient instruction.
Filed Under: riscvoptimizationperformanceinstruction-scheduling