match.pd: Adjust ORDERED & NE to LTGT fold for NaN handling
Comparison folding rule for ORDERED & NE now correctly handles NaNs and trapping math, preventing unexpected exceptions.
The pattern matching rule in match.pd that folds ORDERED and NE comparisons to LTGT has been adjusted to correctly handle floating-point NaNs and trapping math. Previously, this rule could raise an invalid operation exception even with non-trapping math due to how it interacted with GENERIC code. The fix restricts the fold to cases where trapping math is disabled or NaNs are not honored, aligning with the behavior of related rules and ensuring correct comparison semantics.
In Details
This commit modifies a pattern matching rule in match.pd that folds (ordered @0 @1) & (ne @0 @1) into an LTGT comparison. The original rule lacked a check for trapping math and applied at the GENERIC level, leading to incorrect floating-point exception behavior (FE_INVALID) with quiet NaNs, as LTGT can trap while ORDERED and NE do not. The change adds a condition to only apply the fold when -fno-trapping-math is in effect or when NaNs are not honored, mirroring the behavior of adjacent LTGT rules and restoring the correct sequence of floating-point comparisons on x86 architectures.
- match.pd
- A file in GCC containing pattern matching rules used to optimize the GENERIC intermediate representation.
- ORDERED
- A comparison predicate that is true if neither operand is NaN (Not a Number).
- NE
- Not Equal comparison operator.
- LTGT
- A combined Less Than and Greater Than condition, often used as a shorthand for certain comparison outcomes.
- GENERIC
- The first compiler intermediate representation (IR) in GCC, representing code in a tree-like structure before further optimizations and target-specific lowering.
- NaN
- Not a Number. A special floating-point value used to represent undefined or unrepresentable results, such as from dividing by zero or taking the square root of a negative number.
- FE_INVALID
- A floating-point exception flag indicating an invalid operation, such as dividing zero by zero or taking the square root of a negative number.
- trapping math
- A mode of floating-point arithmetic where certain operations (like division by zero or operations involving NaNs) raise an exception rather than producing a special result (like infinity or NaN).