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

LoongArch: Fix missed optimization for bitwise OR with shift and extend

Corrects `bstrins_w_for_ior_ashift_and_extend` optimization by rewriting the pattern.

An optimization for bstrins_w_for_ior_ashift_and_extend on LoongArch was failing due to the target-independent code preferring plus over ior. Additionally, sign_extend could not be lowered through plus, affecting semantics. This commit rewrites the pattern as a define_insn instead of splitting it, adapts it for the ior to plus optimization, uses a ‘0’ constraint, and removes redundant conditions, fixing a test failure in bstrins-5.c.

In Details

This patch addresses a failure in gcc.target/loongarch/bstrins-5.c related to the bstrins_w_for_ior_ashift_and_extend optimization. The issue stems from two related problems: first, the target-independent optimizer's preference for plus over ior, which changes the pattern's recognizability; and second, the inability of sign_extend to be lowered through plus and associate with a subreg to form a sign_extract. The solution involves rewriting the optimization pattern in config/loongarch/loongarch.md as a define_insn rather than a split operation. This new definition adapts th…

For Context
LoongArch
An open-source RISC-based instruction set architecture (ISA) developed by Loongson Technology, primarily for high-performance computing and embedded systems.
define_insn
A macro in GCC's machine description files (.md) used to define a specific machine instruction pattern, its operands, and constraints.
RTL (Register Transfer Language)
An intermediate representation used in GCC that describes operations on registers and memory. It's a low-level language that is close to machine code but is still target-independent to some extent.
sign_extend
An operation that extends a smaller integer type to a larger one by replicating the most significant bit (the sign bit). This preserves the value's sign.
subreg
A part of a larger register that is treated as a separate operand. For example, the lower 32 bits of a 64-bit register might be accessed as a subreg.
Filed Under: loongarchoptimizationRTLbugfix