On 10/22/26 5:44 PM, Jeff Law wrote:
> 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.

Sure.  I will post that as a new two-patch series:

1. Let the rule fire only with -fno-trapping-math or when NaNs are not
   honored, the same condition as the (ltgt @0 @0) rule next to it.
   This is the one to backport.  The rule came in with 86f5031c804
   ("i386: Optimize ordered and nonequal") for GCC 15, so only gcc-15
   and gcc-16 need it; gcc-14 does not have the rule.

2. Remove the rule on trunk, where combine_comparisons already gives
   LTGT with -fno-trapping-math and NE when NaNs are not honored.

Patch 2/2 of this series (the gimple-fold change for PR127786) does not
depend on this one, so I will repost it on its own.

Igor



________________________________
From: Andrea Pinski <[email protected]>
Sent: Thursday, October 22, 2026 8:19:58 PM
To: Jeff Law
Cc: Igor Shevlyakov; [email protected]; [email protected]
Subject: Re: [PATCH 1/2] match.pd: Remove the ORDERED & NE to LTGT fold 
[PR127785]

[EXTERNAL] Caution: This email originated from outside of the organization. Do 
not click links or open attachments unless you recognize the sender and know 
the content is safe.

On Thu, Oct 22, 2026 at 5:46 PM Jeff Law <[email protected]> wrote:
>
>
>
> 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.

This was also from Richard too.  I just spelled it out explicitly.

>
> 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