Don't reverse LTGT/UNEQ when traps are honored
Prevents incorrect floating-point exception raising by not reversing signaling comparisons.
This commit corrects the behavior of reversing comparison codes in GCC’s middle-end, specifically for LTGT (less than or greater than) and UNEQ (not equal) operations when floating-point traps are enabled. LTGT is a signaling comparison that can raise FE_INVALID on NaN operands, while UNEQ is quiet. Unconditionally reversing LTGT into UNEQ (or vice versa) could incorrectly raise an exception. The fix ensures these reversals are avoided when floating-point exceptions are honored, and refactors the handling of NE comparisons to use !UNEQ instead of LTGT to avoid triggering signaling behavior for quiet comparisons.
In Details
The function reversed_comparison_code_parts in jump.cc previously reversed LTGT and UNEQ unconditionally. LTGT is a signaling comparison that raises FE_INVALID on NaN inputs, whereas UNEQ is a quiet comparison. When flag_trapping_math is set and NaNs are possible, reversing LTGT to UNEQ or vice-versa can lead to incorrect exception behavior. This patch modifies the function to return UNKNOWN for these specific comparisons under trapping conditions, mirroring invert_tree_comparison's behavior at the GIMPLE level. Additionally, emit_store_flag in expmed.cc is updated…
- middle-end
- The part of the compiler that performs optimizations and transformations on the intermediate representation of the code, between the front-end (parsing) and the back-end (code generation).
- LTGT
- A comparison condition code representing 'less than or greater than'. It is a signaling comparison in floating-point arithmetic, meaning it can trigger exceptions.
- UNEQ
- A comparison condition code representing 'not equal'. It is typically a quiet comparison in floating-point arithmetic.
- FE_INVALID
- A floating-point exception flag indicating an invalid operation, such as attempting to operate on a NaN (Not a Number).
- NaN
- Not a Number. A special floating-point value used to represent undefined or unrepresentable results, such as the result of 0/0.
- GIMPLE
- GCC's internal three-address code representation, used for optimization passes. It's a simplified intermediate representation derived from the compiler's parse tree.