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

Fix sign test logic for masked narrowing conversions

Corrected how sign tests are handled after masked narrowing conversions to prevent incorrect optimization decisions.

An optimization bug caused incorrect sign tests for values that were masked and then narrowed. The compiler mistakenly considered bits beyond the narrowed type’s width when determining the sign bit, leading to faulty simplifications. The fix now correctly truncates the mask to the precision of the narrow type, ensuring sign tests accurately reflect the narrowed value and allowing more correct optimizations.

In Details

The fold_truth_andor_for_ifcombine pass incorrectly propagated sign bits when combining a truth mask with a subsequent narrowing conversion. Specifically, if a mask M was applied to a value X before it was narrowed (e.g., (short)(X & M)), the pass incorrectly used the sign bit of X based on M's original width, even if M's upper bits were discarded by the narrowing. This commit modifies decode_field_reference to truncate the mask to the target type's precision when such narrowing occurs, ensuring the sign bit is correctly computed based only on the relevant bits of the narrowed…

For Context
tree-optimization
A phase in GCC where optimizations are performed on the compiler's intermediate representation, known as 'tree'.
Sign Test
An operation that checks if a number is positive or negative, typically by examining its most significant bit.
Narrowing Conversion
A type conversion that reduces the size or precision of a value, potentially losing information (e.g., converting a 32-bit integer to an 8-bit integer).
Mask
A value used in bitwise operations to select or clear specific bits of another value.
ifcombine
A GCC optimization pass that attempts to combine conditional branches and logical operations for more efficient code.
Filed Under: optimizationtree-optimizationbugfixsign extension