CTZ compare fold corrected for values at zero
A bug in the pattern matching for CTZ (Count Trailing Zeros) comparisons involving zero has been fixed, preventing miscompilation on certain architectures.
This commit fixes a bug in GCC’s match.pd file related to folding CTZ (Count Trailing Zeros) comparisons. The fold incorrectly applied when the value at zero for CTZ was less than the comparison constant, leading to incorrect results and potential miscompilation on targets like GCN and certain RS6000 configurations. The fix ensures the fold only applies when the value at zero is greater than or equal to the comparison constant, resolving issues reported in PR95527.
In Details
The match.pd pattern for folding .CTZ (x, V) >= C into (x & ((1 << C) - 1)) == 0 has been corrected. This fold is applicable when V < 0 or V >= prec. The issue arises when x = 0: the original fold yields true, but .CTZ(0, V) is only guaranteed to be greater than or equal to C if V >= C. The corrected pattern now requires V >= C for the fold to be applied, preventing miscompilation on targets like GCN and RS6000 (when TARGET_CTZ and TARGET_POPCNTD are not defined), which use -1 as the value at zero for CTZ.
- match.pd
- A file containing patterns used by GCC's backend to match intermediate representation code to target-specific instructions. 'pd' likely stands for 'pattern description'.
- CTZ
- Count Trailing Zeros. An instruction or function that counts the number of zero bits from the least significant bit up to the first set bit.
- fold
- In compiler optimization, 'folding' refers to replacing an expression with a simpler equivalent constant value or a more efficient pattern.
- V
- In this context, likely refers to the second argument of the
.CTZintrinsic, which specifies the precision or the number of bits to consider. - prec
- Precision. Refers to the number of bits in the data type being operated on (e.g., 32 or 64).
- GCN
- Graphics Core Next. AMD's architecture for their Graphics Processing Units (GPUs).
- RS6000
- A family of POWER-based workstations and servers originally developed by IBM. GCC has backends for generating code for this architecture.