On 10/9/26 10:55 AM, Igor Shevlyakov wrote:
ORDERED and NE do not raise an invalid operation exception for a quiet
NaN, but LTGT does.  The (ordered & ne) -> ltgt rule added with
"i386: Optimize ordered and nonequal" has no trapping-math check, so
!__builtin_isunordered (a, b) & (a != b) raises FE_INVALID for a NaN
operand even at -O0, since the rule also fires in GENERIC.  On x86 the
result is a signaling comiss.

The rule is not needed: the generic (a CMP b) & (a CMP b) pattern
already combines these comparisons through combine_comparisons, which
gives LTGT with -fno-trapping-math, NE when NaNs are not honored, and
keeps the two comparisons apart under the default flags because LTGT
would trap where neither ORDERED nor NE does.  So remove it.  Under the
default flags x86 goes back to the quiet ucomiss/setp/setnp sequence
that the rule replaced.

The tests added with the rule check the fold itself, so they now pass
-fno-trapping-math, and opt-ordered-and-nonequal-1.c scans the
optimized dump instead of forwprop1.  The new
opt-ordered-and-nonequal-2.c checks that the default flags keep <> out,
and the new cmp-of-cmp-fenv.c checks at run time that combining two
comparisons of the same operands, the bitwise form fixed here included,
does not change whether FE_INVALID is raised for a quiet NaN.

        PR middle-end/127785

gcc/ChangeLog:

        * match.pd ((ordered @0 @1) & (ne @0 @1)): Remove.

gcc/testsuite/ChangeLog:

        * gcc.dg/opt-ordered-and-nonequal-1.c: Add -fno-trapping-math and
        scan the optimized dump.
        * gcc.dg/opt-ordered-and-nonequal-2.c: New test.
        * gcc.dg/torture/cmp-of-cmp-fenv.c: New test.
        * gcc.target/i386/opt-comi-1.c: Add -fno-trapping-math.
        * gcc.target/i386/avx10_2-compare-1b.c: Likewise.

Suggested-by: Andrea Pinski <[email protected]>
Signed-off-by: Igor Shevlyakov <[email protected]>
It sounds like Andrea has recommended that we have two patches for 127785.

First is to fix the pattern to be safe which we backport to gcc-14, gcc-15 and gcc-16.

Then a distinct patch to remove the pattern from the trunk as we have something better in place for the trunk.

jeff

Reply via email to