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

RISC-V: Combine zero-extended word masks with Zbs operations

RISC-V Zbs instructions now better handle zero-extended word masks.

This commit improves the RISC-V backend’s handling of bit manipulation operations by combining zero-extended word-sized bit masks with 64-bit values using Zbs instructions. It introduces new patterns for bset, binv, and bclr that directly match canonical complemented forms with Zbs alone. This approach optimizes the generation of code involving bitmasks, potentially leading to more efficient bit manipulation operations on the target architecture.

In Details

This patch introduces new machine patterns for the RISC-V Zbs (Bit Manipulation) extension to handle cases where a SImode (32-bit) mask, zero-extended from a word, is combined with a DImode (64-bit) value. Previously, these combinations might have involved shifts and masks that were less efficient. The new patterns (*<bit_optab>di_zext_si, *bclrdi_zext_si, *bclrdi_zext_si_canonical) allow the compiler to directly use Zbs instructions like bset, binv, and bclr when operating on a 64-bit destination with a 32-bit zero-extended mask, simplifying the generated code.

For Context
RISC-V
An open-standard Instruction Set Architecture (ISA) based on Reduced Instruction Set Computing principles. GCC supports various RISC-V extensions for different architectures.
Zbs
A RISC-V extension that provides bit manipulation instructions, including bit set, bit clear, and bit invert operations.
zero-extended
A type of type conversion where a value of a smaller data type is expanded to a larger data type by padding the most significant bits with zeros. This ensures that the original value is preserved without introducing sign extension artifacts.
word mask
A bitmask that is the size of a 'word' on a particular architecture. In a 64-bit architecture, a word is typically 32 bits, so a 'word mask' here refers to a 32-bit mask.
SImode
GCC's internal representation for a single integer, typically 32 bits.
DImode
GCC's internal representation for a double integer, typically 64 bits.
Filed Under: risc-vbit manipulationoptimization