Don't fold wrapping signed (x + y) >> 1 to AVG_FLOOR
Prevents incorrect optimization of signed vector addition overflow into AVG_FLOOR.
This commit corrects a flaw in the match.pd patterns where signed vector addition results that wrap around were incorrectly folded into the IFN_AVG_FLOOR intrinsic. For signed types with defined overflow behavior, the previous logic required only one leading zero bit, which was insufficient to prevent incorrect results when addition overflowed. The fix now mandates two leading zero bits for signed types with defined overflow, ensuring accurate calculations and preventing misinterpretation of wrapped values.
In Details
The pattern merging (x + y) >> 1 into IFN_AVG_FLOOR(x, y) in match.pd relied on a check for a single leading zero bit in x and y to guarantee that x + y would not wrap. This is sufficient for unsigned types or signed types with undefined overflow. However, for signed types with defined overflow (e.g., -fwrapv), a two's complement addition can wrap from a large positive value to a negative one. This commit strengthens the check to require two leading zero bits for signed types with defined overflow, thus preventing the incorrect folding when overflow occurs and produces a negativ…
- match.pd
- A file containing pattern definitions for the GCC matcher, used to recognize and optimize specific instruction sequences or expressions.
- AVG_FLOOR
- An intrinsic function or pattern representing the average of two values, rounded down. In the context of vector operations, it implies a SIMD instruction that computes this average for multiple lanes.
- IFN_AVG_FLOOR
- Likely an internal GCC representation or intrinsic related to the AVG_FLOOR operation, possibly indicating a specific variant or context.
- -fwrapv
- A GCC compiler flag that specifies signed integer overflow is defined behavior and results in two's complement wrapping.