GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
risc-v

RISC-V: Fix mis-compile in bitmanip.md for registers with high bits set.

RISC-V bitmanip code generation now correctly handles registers where upper bits are set.

This commit addresses a mis-compilation issue on RISC-V targets related to bit manipulation instructions. The problem occurred when a define_split pattern was used on a register where bits beyond the lower 32 were non-zero, leading to incorrect code generation. By conditioning the split on nonzero_bits, the compiler now uses the optimized pattern only when appropriate, and falls back to a correct, albeit potentially less optimized, sequence otherwise. A new test case specifically triggers this bug.

In Details

Addresses PR target/121268 by modifying gcc/config/riscv/bitmanip.md. The define_split for (x & -x) - 1 to (x - 1) & ~x is now conditioned on nonzero_bits for the extended SI variant. This ensures that the split only occurs when the upper 32 bits of the register are zero, preventing incorrect code generation when those bits are set. An execution test is added to cover the scenario where the bug manifests.

For Context
define_split
An instruction pattern in GCC's machine description files that defines how a complex instruction can be broken down into simpler ones by the instruction scheduler.
SI
Single Integer, typically a 32-bit integer register in many architectures.
DI
Double Integer, typically a 64-bit integer register.
nonzero_bits
A condition used in GCC's instruction patterns to check if specific bits in an operand are non-zero.
RISC-V
A free and open instruction set architecture (ISA) based on reduced instruction set computer principles.
Filed Under: risc-vcompiler bugcode generation