[1/2] match.pd: Remove the ORDERED & NE to LTGT fold [PR127785]

Message ID gcc-upstream.upstream-bugfix-1015-pr127786.v1.20261009T165552Z.1@tachyum.com
State New
Headers
Series Keep FP exceptions when combining comparisons [PR127785, PR127786] |

Commit Message

Igor Shevlyakov Oct. 9, 2026, 4:55 p.m. UTC
  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 <andrew.pinski@oss.qualcomm.com>
Signed-off-by: Igor Shevlyakov <igor@tachyum.com>
---
 gcc/match.pd                                  |  3 -
 .../gcc.dg/opt-ordered-and-nonequal-1.c       |  4 +-
 .../gcc.dg/opt-ordered-and-nonequal-2.c       | 27 +++++++
 .../gcc.dg/torture/cmp-of-cmp-fenv.c          | 71 +++++++++++++++++++
 .../gcc.target/i386/avx10_2-compare-1b.c      |  2 +-
 gcc/testsuite/gcc.target/i386/opt-comi-1.c    |  2 +-
 6 files changed, 102 insertions(+), 7 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/opt-ordered-and-nonequal-2.c
 create mode 100644 gcc/testsuite/gcc.dg/torture/cmp-of-cmp-fenv.c
  

Comments

Jeff Law Oct. 23, 2026, 12:44 a.m. UTC | #1
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 <andrew.pinski@oss.qualcomm.com>
> Signed-off-by: Igor Shevlyakov <igor@tachyum.com>
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
  
Andrea Pinski Oct. 23, 2026, 3:19 a.m. UTC | #2
On Thu, Oct 22, 2026 at 5:46 PM Jeff Law <jeffrey.law@oss.qualcomm.com> 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 <andrew.pinski@oss.qualcomm.com>
> > Signed-off-by: Igor Shevlyakov <igor@tachyum.com>
> 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
  
Igor Shevlyakov Oct. 23, 2026, 3:31 a.m. UTC | #3
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 <andrew.pinski@oss.qualcomm.com>
Sent: Thursday, October 22, 2026 8:19:58 PM
To: Jeff Law
Cc: Igor Shevlyakov; gcc-patches@gcc.gnu.org; rguenther@suse.de
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 <jeffrey.law@oss.qualcomm.com> 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 <andrew.pinski@oss.qualcomm.com>
> > Signed-off-by: Igor Shevlyakov <igor@tachyum.com>
> 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
  

Patch

diff --git a/gcc/match.pd b/gcc/match.pd
index 648bda9bf11..ec575f49d57 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -8281,9 +8281,6 @@  DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (ltgt @0 @0)
  (if (!flag_trapping_math || !tree_expr_maybe_nan_p (@0))
   { constant_boolean_node (false, type); }))
-(simplify
- (bit_and:c (ordered @0 @1) (ne @0 @1))
- (ltgt @0 @1))
 
 /* x == ~x -> false */
 /* x != ~x -> true */
diff --git a/gcc/testsuite/gcc.dg/opt-ordered-and-nonequal-1.c b/gcc/testsuite/gcc.dg/opt-ordered-and-nonequal-1.c
index d61c3322214..de7d4324816 100644
--- a/gcc/testsuite/gcc.dg/opt-ordered-and-nonequal-1.c
+++ b/gcc/testsuite/gcc.dg/opt-ordered-and-nonequal-1.c
@@ -1,5 +1,5 @@ 
 /* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-forwprop1-details" } */
+/* { dg-options "-O2 -fno-trapping-math -fdump-tree-optimized" } */
 /* Make this work for default 'LOGICAL_OP_NON_SHORT_CIRCUIT == false' configurations:
    { dg-additional-options "--param logical-op-non-short-circuit=1" } */
 
@@ -48,4 +48,4 @@  int is_ordered_or_nonequal_sh_9 (float a, float b)
   return !((a == b) || __builtin_isunordered (b, a));
 }
 
-/* { dg-final { scan-tree-dump-times "gimple_simplified to\[^\n\r]*<>" 9 "forwprop1" } } */
+/* { dg-final { scan-tree-dump-times "<>" 9 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/opt-ordered-and-nonequal-2.c b/gcc/testsuite/gcc.dg/opt-ordered-and-nonequal-2.c
new file mode 100644
index 00000000000..d1fbae9d5e7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/opt-ordered-and-nonequal-2.c
@@ -0,0 +1,27 @@ 
+/* ORDERED and NE do not raise FE_INVALID for a quiet NaN but LTGT does,
+   so without -fno-trapping-math they must not be combined.  */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-add-options ieee } */
+
+int is_ordered_and_nonequal_1 (double a, double b)
+{
+  return !__builtin_isunordered (a, b) & (a != b);
+}
+
+int is_ordered_and_nonequal_2 (double a, double b)
+{
+  return (b != a) & !__builtin_isunordered (a, b);
+}
+
+int is_ordered_and_nonequal_3 (double a, double b)
+{
+  return !__builtin_isunordered (a, b) && (a != b);
+}
+
+int is_ordered_or_nonequal_4 (double a, double b)
+{
+  return !(__builtin_isunordered (a, b) || (a == b));
+}
+
+/* { dg-final { scan-tree-dump-not "<>" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/torture/cmp-of-cmp-fenv.c b/gcc/testsuite/gcc.dg/torture/cmp-of-cmp-fenv.c
new file mode 100644
index 00000000000..157a3d1af64
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/cmp-of-cmp-fenv.c
@@ -0,0 +1,71 @@ 
+/* Combining two comparisons of the same operands must not change whether
+   FE_INVALID is raised for a quiet NaN.  */
+/* { dg-do run } */
+/* { dg-add-options ieee } */
+/* { dg-require-effective-target fenv_exceptions_double } */
+
+#include <fenv.h>
+
+#define NOIPA __attribute__ ((noipa))
+
+/* Signaling | signaling: LTGT, still signals.  */
+NOIPA int lt_or_gt (double a, double b) { return (a < b) | (a > b); }
+/* Signaling | signaling: always true for ordered operands.  */
+NOIPA int lt_or_ge (double a, double b) { return (a < b) | (a >= b); }
+/* Signaling ^ signaling: GT, still signals.  */
+NOIPA int le_xor_ge (double a, double b) { return (a <= b) ^ (a >= b); }
+/* Quiet & quiet: LT would signal.  */
+NOIPA int ord_and_unlt (double a, double b)
+{ return !__builtin_isunordered (a, b) & !__builtin_isgreaterequal (a, b); }
+/* Quiet && quiet: LT would signal.  */
+NOIPA int ord_andif_unlt (double a, double b)
+{ return !__builtin_isunordered (a, b) && !__builtin_isgreaterequal (a, b); }
+/* Quiet == quiet: GT would signal.  */
+NOIPA int eq_eq_unlt (double a, double b)
+{ return (a == b) == !__builtin_isgreaterequal (a, b); }
+/* Quiet | quiet: UNEQ, still quiet.  */
+NOIPA int uno_or_eq (double a, double b)
+{ return __builtin_isunordered (a, b) | (a == b); }
+/* Quiet || signaling: > is not evaluated for a NaN, so NE is fine.  */
+NOIPA int unlt_orif_gt (double a, double b)
+{ return !__builtin_isgreaterequal (a, b) || (a > b); }
+/* Quiet & quiet: LTGT would signal.  */
+NOIPA int ord_and_ne (double a, double b)
+{ return !__builtin_isunordered (a, b) & (a != b); }
+
+static const struct
+{
+  int (*fn) (double, double);
+  int signals;
+  /* Results for (NaN, 1), (0, 1), (1, 1) and (2, 1).  */
+  int res[4];
+} tests[] = {
+  { lt_or_gt,       1, { 0, 1, 0, 1 } },
+  { lt_or_ge,       1, { 0, 1, 1, 1 } },
+  { le_xor_ge,      1, { 0, 1, 0, 1 } },
+  { ord_and_unlt,   0, { 0, 1, 0, 0 } },
+  { ord_andif_unlt, 0, { 0, 1, 0, 0 } },
+  { eq_eq_unlt,     0, { 0, 0, 0, 1 } },
+  { uno_or_eq,      0, { 1, 0, 1, 0 } },
+  { unlt_orif_gt,   0, { 1, 1, 0, 1 } },
+  { ord_and_ne,     0, { 0, 1, 0, 1 } },
+};
+
+int
+main (void)
+{
+  static const double as[4] = { __builtin_nan (""), 0.0, 1.0, 2.0 };
+
+  for (unsigned i = 0; i < sizeof (tests) / sizeof (tests[0]); i++)
+    for (unsigned j = 0; j < 4; j++)
+      {
+	feclearexcept (FE_INVALID);
+	int r = tests[i].fn (as[j], 1.0);
+	int invalid = fetestexcept (FE_INVALID) != 0;
+	if (r != tests[i].res[j])
+	  __builtin_abort ();
+	if (invalid != (j == 0 && tests[i].signals))
+	  __builtin_abort ();
+      }
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.target/i386/avx10_2-compare-1b.c b/gcc/testsuite/gcc.target/i386/avx10_2-compare-1b.c
index cc7f8205f5f..73457c9096a 100644
--- a/gcc/testsuite/gcc.target/i386/avx10_2-compare-1b.c
+++ b/gcc/testsuite/gcc.target/i386/avx10_2-compare-1b.c
@@ -1,5 +1,5 @@ 
 /* { dg-do compile } */
-/* { dg-options "-O2 -march=x86-64-v3 -mavx10.2 -mfpmath=sse" } */
+/* { dg-options "-O2 -fno-trapping-math -march=x86-64-v3 -mavx10.2 -mfpmath=sse" } */
 /* { dg-final { scan-assembler-times "comi" 6 } } */
 /* { dg-final { scan-assembler-times "comx" 12 } } */
 
diff --git a/gcc/testsuite/gcc.target/i386/opt-comi-1.c b/gcc/testsuite/gcc.target/i386/opt-comi-1.c
index a2c5219d98d..715847faf33 100644
--- a/gcc/testsuite/gcc.target/i386/opt-comi-1.c
+++ b/gcc/testsuite/gcc.target/i386/opt-comi-1.c
@@ -1,5 +1,5 @@ 
 /* { dg-do compile } */
-/* { dg-options "-O2 -mfpmath=sse -msse2" } */
+/* { dg-options "-O2 -fno-trapping-math -mfpmath=sse -msse2" } */
 /* { dg-additional-options "-fno-unwind-tables -fno-asynchronous-unwind-tables" { target *-*-darwin* } } */
 /* { dg-final { scan-assembler-times "comiss" 9 } } */
 /* { dg-final { scan-assembler-times "set" 9 } } */