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

RISC-V Zbkb packh cost and instruction selection fixed.

Corrected RISC-V packh generation cost and instruction selection when Zbb and Zbkb extensions are enabled.

This commit addresses an issue where the packh instruction was not being generated correctly when both the Zbb and Zbkb RISC-V extensions were enabled. It corrects the cost model for masked byte shifts to accurately reflect a single instruction cost for packh under Zbkb, and refines instruction selection to prevent the generic shift-reverse pattern from incorrectly taking precedence over the dedicated packh pattern.

In Details

This patch updates config/riscv/riscv.cc and config/riscv/riscv.md to fix packh generation for the Zbkb extension. It modifies riscv_rtx_costs to assign a cost of one instruction to masked byte shifts when Zbkb is enabled, matching the cost of packh. Additionally, it adjusts the shift_reverse pattern to exclude masked byte shifts handled by packh when Zbkb is active, preventing the generic pattern from being selected erroneously. A test case enabling Zbb and Zbkb is also added to gcc.target/riscv/packh32.c.

For Context
RISC-V
An open-standard instruction set architecture (ISA) based on Reduced Instruction Set Computing principles.
Zbkb
A RISC-V extension that adds bit manipulation instructions, including byte packing and unpacking operations.
Zbb
A RISC-V extension providing basic bit manipulation instructions.
packh
A specific instruction, likely from the Zbkb extension, that packs multiple smaller data elements (e.g., bytes) into a larger one (e.g., a word).
Cost model
In compilers, a mechanism that estimates the performance cost (e.g., execution time, instruction count) of different code sequences or instructions, used by optimization passes.
Instruction selection
A compiler phase that maps intermediate representations of operations to specific machine instructions available on the target architecture.
Filed Under: gccrisc-voptimizationcompiler