GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
match

Match: Rejects NARROW_CLIP saturating truncation at minimum value for correctness.

Pattern matching for unsigned integer narrow clip saturating truncation now rejects cases involving the minimum value to avoid incorrect results.

This commit adjusts the unsigned_integer_narrow_clip pattern in GCC’s matcher to reject cases where the operand is known to be the minimum value of its type. The original pattern could incorrectly rewrite a program when the minimum value was present, leading to a stored value of NT_MAX instead of the expected 0 on certain targets. The updated pattern now requires the minimum value not to occur, preserving correctness and resolving PR126981.

In Details

The unsigned_integer_narrow_clip pattern in match-sat-alu.pd incorrectly rewrites (UT) X > (NT) -1 ? (-X) >> (PREC (X) - 1) : X to SAT_U_TRUNC (MAX (0, X)). This rewrite introduces an error when X is the minimum value of its type, as the negation on the unsigned type is its own inverse, leading to a different result after the shift compared to the MAX(0, X) simplification. This commit adds a guard to the pattern to reject matching when the minimum value is known to occur, thus fixing PR126981.

For Context
Match
Refers to the pattern matching component of GCC, used for recognizing and transforming specific code constructs during optimization passes.
unsigned integer narrow clip saturating truncation
An arithmetic operation that truncates an unsigned integer to a narrower type, clamping the result to the maximum value of the target type if overflow occurs, with a specific handling for negation and shifting.
NT_MAX
Represents the maximum value for a non-trap (NT) integer type.
GIMPLE
GCC's internal representation for intermediate code, used after parsing and before optimization and code generation. It's a three-address code form.
optab
A table within GCC that stores machine-specific operations and their corresponding instruction patterns.
Filed Under: matchoptimizationbugfixinteger arithmeticsaturating arithmetic