GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
gcc Performance Win

Improve select across A/A OP C where C is 2^n

GCC opts for a more specialized instruction sequence for conditional adds on loongarch.

This commit enhances GCC’s ability to generate conditional zero-based sequences for operations where one operand is a constant power of two. This often results in better code than generalized conditional moves. The change specifically addresses a regression on loongarch by favoring a specialized instruction sequence for conditional adds, ensuring better performance characteristics for that architecture. The optimization is applicable to various arithmetic and logical operations where zero is a neutral operand.

In Details

This change refactors the conditional select optimization within the GCC backend. It targets arithmetic and logical operations of the form A OP C where C is a 2^N constant. Previously, if-conversion could generate conditional moves. This commit improves the generation of sequences that exploit the pattern select(A, A OP 2^N) by transforming it into A OP select(0, 2^N), which can be further optimized. The specific instruction sequence generated now accounts for architectural preferences, such as the loongarch's preference for sequences that avoid conditional moves when a power-of-two c…

For Context
if-conversion
A compiler optimization that replaces conditional branches with conditional instructions, potentially improving instruction-level parallelism.
conditional select
An instruction that chooses between two source operands based on a condition, often referred to as a ternary operation (e.g., condition ? value_if_true : value_if_false).
loongarch
A family of instruction set architectures (ISAs) developed in China, known for its support of various computing needs from embedded systems to high-performance computing.
power of two (2^N)
An integer that can be expressed as 2 raised to the power of some non-negative integer N. These constants have special properties in binary representation and computing.
Filed Under: optimizationloongarchcompiler