i386: Implement TARGET_UNSPEC_MAY_TRAP_P for trapping SSE/AVX/x87 UNSPECs
Correctly flags trapping behavior for i386 SSE/AVX/x87 instructions under -ftrapping-math.
The i386 backend now correctly implements TARGET_UNSPEC_MAY_TRAP_P, which determines if an intrinsic operation might trap. Previously, the default implementation caused false negatives for certain SSE, AVX, and x87 operations, leading to incorrect hoisting of instructions that could trigger floating-point exceptions on NaN inputs when -ftrapping-math was enabled. This fix ensures these operations are not speculated or reordered inappropriately, preventing spurious floating-point exceptions.
In Details
The i386 backend lacked a specific implementation for TARGET_UNSPEC_MAY_TRAP_P. The default default_unspec_may_trap_p incorrectly classified certain floating-point UNSPECs (like UNSPEC_FIX_NOTRUNC and UNSPEC_IEEE_MIN/MAX in vector modes) as non-trapping. This commit adds ix86_unspec_may_trap_p to correctly identify trapping behavior, especially crucial when -ftrapping-math is enabled, preventing incorrect instruction hoisting and potential spurious FE_INVALID exceptions.
- TARGET_UNSPEC_MAY_TRAP_P
- A GCC target hook that indicates whether a given
UNSPECorUNSPEC_VOLATILEoperation can potentially trap or raise a floating-point exception. - UNSPEC
- An intermediate representation (IR) in GCC used to represent machine-specific operations or intrinsics that do not map directly to standard GCC tree nodes.
- -ftrapping-math
- A GCC command-line option that modifies floating-point exception handling, causing operations that might raise exceptions (like division by zero or operations on NaNs) to be treated more conservatively to avoid undefined behavior or spurious exceptions.
- FE_INVALID
- A floating-point exception flag indicating an invalid operation, such as taking the square root of a negative number or dividing zero by zero.