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

match.pd: Fix MIN of counts fold for CLZ and CTZ

Corrects the fold condition for MIN of counts with CLZ/CTZ to handle vector types and signed comparisons.

This commit fixes an issue in match.pd concerning the MIN of counts fold for .CLZ and .CTZ operations. The fold, which simplifies min(.CLZ(x, V), .CLZ(y, V)) to .CLZ(x | y, V), had an incorrect condition that failed for vector types and misbehaved with signed values when V was set to the value-at-zero. The fix ensures V is compared as a signed value against the element precision, allowing the fold to work correctly for vector counts.

In Details

The match.pd pattern driver optimizes expressions. This change corrects the fold condition for min(clz(x), clz(y)) and min(ctz(x), ctz(y)). Previously, it incorrectly used TYPE_PRECISION and wi::geu_p which failed for vector types and allowed signed interpretation issues with the value-at-zero (-1). The fix compares V with element_precision as a signed value, resolving an ICE on AArch64 vectorization and a correctness issue with MIN_EXPR producing -1 instead of the expected 29 for CLZ(5, -1).

For Context
.CLZ
GCC's internal representation for the count leading zeros (CLZ) intrinsic. It calculates the number of zero bits preceding the most significant set bit in an integer.
.CTZ
GCC's internal representation for the count trailing zeros (CTZ) intrinsic. It calculates the number of zero bits following the least significant set bit in an integer.
value-at-zero
A special value used by some CLZ/CTZ implementations to represent the result when the input is zero. This can be -1 or other values depending on the target.
match.pd
The pattern-driven matching component of GCC's middle-end, responsible for simplifying and optimizing expressions by matching them against predefined patterns.
Filed Under: optimizationvectorizationclzctz