tree-optimization/127876 - wrong code with conditional float reduction
Fixes incorrect code generation for conditional floating-point reductions involving vectorization.
This commit corrects erroneous code generation for conditional floating-point reductions of the form r = <cond> ? r : CST when vectorization is applied. Previously, the compiler would reduce a mixed vector of initial constant values and the constant reduction value, performing a final MAX or MIN reduction. This approach failed when the initial and reduced values compared equal, and also did not correctly handle special floating-point values like -0.0 or NaNs. The fix uses explicit Less Than (LT) or Greater Than (GT) checks to ensure correctness across these cases.
In Details
Fixes a bug in tree-vect-loop.cc:vectorizable_reduction where conditional reductions of the form r = <cond> ? r : CST were vectorized incorrectly. The previous implementation performed a reduction on a vector mixed with the initial constant value and the reduction constant, followed by a MAX/MIN operation. This logic is flawed when the initial value equals the reduction constant or when dealing with floating-point special values like -0.0 or NaN. The corrected code now uses explicit LT/GT comparisons to establish the condition for the reduction, ensuring correctness for all scalar a…
- tree-optimization
- A phase in the GCC compiler where code is represented and optimized using an intermediate representation called GENERIC/GIMPLE, which is tree-like.
- vectorization
- The process of transforming code to use SIMD instructions, allowing a single instruction to operate on multiple data elements simultaneously.
- reduction
- An operation that combines elements of a sequence into a single value, such as sum, product, minimum, or maximum.
- conditional reduction
- A reduction operation where the operation is only applied if a certain condition is met, or where the initial value is conditionally chosen.
- floating-point
- A data type representing real numbers, with limitations in precision and special values like infinity and NaN (Not a Number).
- NaN
- Not a Number. A special floating-point value indicating an undefined or unrepresentable result, often resulting from operations like 0/0.