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

Fix sign-extension bug when widening inner mask in decode_field_reference

Corrects sign extension for masked bitfields to prevent miscompilation.

A bug in decode_field_reference incorrectly zero-extended inner bitwise AND masks, leading to incorrect sign extension behavior when masks were widened. This patch ensures that the sign of the inner mask is correctly considered, preventing miscompilation of conditions involving sign extension. A new test case verifies the fix.

In Details

The decode_field_reference GIMPLE-fold function mishandled sign extension when a BIT_AND_EXPR mask of narrower precision was combined with a wider-precision mask from an outer condition. It unconditionally used UNSIGNED for zero-extension, even when the inner mask was signed and its sign bit was set. This incorrectly zeroed out sign-extension bits, causing the outer condition's sign-bit test to fail. The fix uses TYPE_SIGN of the mask operand to correctly extend and_mask to the target precision prec_in, preserving the sign bit when necessary.

For Context
BIT_AND_EXPR
GCC's internal representation for a bitwise AND operation within the GIMPLE intermediate language.
GIMPLE
GCC's main intermediate representation (IR), a three-address code form that simplifies many optimizations and transformations.
sign-extension
The process of converting a signed number to a larger data type by duplicating the sign bit. This preserves the number's value.
Filed Under: optimizationbugfixtree-optimization