https://gcc.gnu.org/g:5342329f1d149e200902c1e44130d59762f4fb3b

commit r17-5839-g5342329f1d149e200902c1e44130d59762f4fb3b
Author: Igor Shevlyakov <[email protected]>
Date:   Tue Oct 6 17:23:50 2026 -0700

    simplify-rtx: Keep FP trap behavior when combining comparisons [PR127786]
    
    simplify_logical_relational_operation combines two comparisons of the
    same operands through a mask of the possible outcomes {<, >, ==,
    unordered}.  It ignores whether a comparison code raises an invalid
    operation exception for a quiet NaN (LT, LE, GT, GE and LTGT do; EQ,
    NE, ORDERED and the UN* codes do not), so under -ftrapping-math the
    result can add a trap that the source did not have, e.g. ORDERED & NE
    becomes LTGT, ORDERED & UNLT becomes LT and (eq (eq a b) (unlt a b))
    becomes GT, or drop one that it had, e.g. UNLT | GT becomes NE and
    LT ^ LE becomes EQ.
    
    combine_comparisons in fold-const.cc refuses such combinations when
    NaNs are honored and trapping math is on.  Add the same check here:
    in that case, refuse a result that raises the exception for a quiet NaN
    when neither input does, or that does not raise it when one of the
    inputs does.  The classification is the one combine_comparisons uses.
    A constant result never raises the exception, so a fold to true or
    false is also refused when one of the inputs would have raised it.
    
    Add selftests that evaluate the simplification of IOR and of the
    outer comparisons EQ, NE, LTU, LEU, GTU and GEU of every pair of the
    14 comparison codes against all possible outcomes, for CCmode, SImode
    and DFmode operands under each combination of -ffinite-math-only and
    -ftrapping-math, and check the trap property where it applies.  Also
    add an execution test that checks FE_INVALID for combinations that
    have to keep signaling or stay quiet.
    
    Assisted-by: Claude Opus 5.5
    
            PR middle-end/127786
    
    gcc/ChangeLog:
    
            * simplify-rtx.cc (comparison_mask_signals_p): New function.
            (simplify_context::simplify_logical_relational_operation): Don't
            change whether the result traps for a quiet NaN.
            (comparison_holds_p, comparison_signals_p, check_logical_comparison)
            (simplify_logical_comparison, test_logical_comparisons_in_mode)
            (logical_comparison_code, test_logical_comparisons): New functions.
            (test_scalar_ops): Call test_logical_comparisons.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/torture/cmp-of-cmp-fenv-3.c: New test.
    
    Signed-off-by: Igor Shevlyakov <[email protected]>

Diff:
---
 gcc/simplify-rtx.cc                              | 241 +++++++++++++++++++++++
 gcc/testsuite/gcc.dg/torture/cmp-of-cmp-fenv-3.c |  68 +++++++
 2 files changed, 309 insertions(+)

diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
index 6b9afd7039a1..3ad51cec7139 100644
--- a/gcc/simplify-rtx.cc
+++ b/gcc/simplify-rtx.cc
@@ -2660,6 +2660,20 @@ mask_to_comparison (int mask)
     }
 }
 
+/* Return true if the comparison with mask MASK (see comparison_to_mask)
+   raises an invalid-operation exception for a quiet NaN operand, as LT,
+   GT, LE, GE and LTGT do: they are false for unordered operands and
+   depend on their order.  EQ and ORDERED do not, and neither do the
+   constant results 0 and CMP_MASK_ALL.  */
+
+static bool
+comparison_mask_signals_p (int mask)
+{
+  return ((mask & CMP_BIT_UN) == 0
+         && (mask & (CMP_BIT_LT | CMP_BIT_GT)) != 0
+         && mask != CMP_MASK_ORDERED);
+}
+
 /* Canonicalize RES, a scalar const0_rtx/const_true_rtx to the right
    false/true value of comparison with MODE where comparison operands
    have CMP_MODE.  */
@@ -2772,6 +2786,15 @@ simplify_context::simplify_logical_relational_operation 
(rtx_code code,
   else
     return 0;
 
+  /* With trapping math, the result must signal for a quiet NaN exactly
+     when OP0 or OP1 does.  Inverting OP0 does not change that.  */
+  if (HONOR_NANS (cmp_mode)
+      && flag_trapping_math
+      && ((comparison_mask_signals_p (comparison_to_mask (code0))
+          || comparison_mask_signals_p (comparison_to_mask (code1)))
+         != comparison_mask_signals_p (mask)))
+    return 0;
+
   if (mask == all)
     return relational_result (mode, GET_MODE (op0), const_true_rtx);
 
@@ -9580,6 +9603,223 @@ test_comparisons (machine_mode mode0, machine_mode 
mode1, machine_mode mode2)
        }
 }
 
+/* Return true if comparison CODE holds when its operands compare as
+   OUTCOME, one of CMP_BIT_LT, CMP_BIT_GT, CMP_BIT_EQ and CMP_BIT_UN.  */
+
+static bool
+comparison_holds_p (rtx_code code, int outcome)
+{
+  int mask = unsigned_comparison_to_mask (code);
+  if (mask == 0 || code == NE)
+    mask = comparison_to_mask (code);
+  return mask & outcome;
+}
+
+/* Return true if X is a comparison that raises an invalid-operation
+   exception for a quiet NaN operand.  */
+
+static bool
+comparison_signals_p (rtx x)
+{
+  return (COMPARISON_P (x)
+         && comparison_mask_signals_p (comparison_to_mask (GET_CODE (x))));
+}
+
+/* Check RES, a simplification of (CODE OP0 OP1) where OP0 and OP1 are
+   comparisons between REG0 and REG1, against the value of (CODE OP0 OP1)
+   for every possible outcome of comparing REG0 and REG1.  CODE is IOR or
+   a comparison of the results of OP0 and OP1.  NANS_P says whether REG0
+   and REG1 can be unordered, TRAPS_P whether the result must signal
+   exactly when OP0 or OP1 does.  */
+
+static void
+check_logical_comparison (rtx res, rtx_code code, machine_mode mode,
+                         rtx op0, rtx op1, rtx reg0, rtx reg1,
+                         bool nans_p, bool traps_p)
+{
+  if (res == NULL_RTX)
+    return;
+
+  ASSERT_TRUE (CONSTANT_P (res)
+              || (COMPARISON_P (res)
+                  && GET_MODE (res) == mode
+                  && ((XEXP (res, 0) == reg0 && XEXP (res, 1) == reg1)
+                      || (XEXP (res, 0) == reg1 && XEXP (res, 1) == reg0))));
+
+  /* Generic folds of (CODE X X) are allowed to drop a trap.  */
+  if (traps_p && GET_CODE (op0) != GET_CODE (op1))
+    ASSERT_EQ (comparison_signals_p (op0) || comparison_signals_p (op1),
+              comparison_signals_p (res));
+
+  static const int outcomes[] = {
+    CMP_BIT_LT, CMP_BIT_GT, CMP_BIT_EQ, CMP_BIT_UN
+  };
+  for (int outcome : outcomes)
+    {
+      if (outcome == CMP_BIT_UN && !nans_p)
+       continue;
+
+      rtx val0 = (comparison_holds_p (GET_CODE (op0), outcome)
+                 ? const_true_rtx : const0_rtx);
+      rtx val1 = (comparison_holds_p (GET_CODE (op1), outcome)
+                 ? const_true_rtx : const0_rtx);
+      rtx expected;
+      if (code == IOR)
+       expected = simplify_const_binary_operation (code, mode, val0, val1);
+      else
+       expected = simplify_const_relational_operation (code, mode,
+                                                        val0, val1);
+
+      rtx folded = res;
+      if (COMPARISON_P (res))
+       {
+         /* Swapping the operands exchanges < and >.  */
+         int swapped = outcome;
+         if (XEXP (res, 0) == reg1 && outcome == CMP_BIT_LT)
+           swapped = CMP_BIT_GT;
+         else if (XEXP (res, 0) == reg1 && outcome == CMP_BIT_GT)
+           swapped = CMP_BIT_LT;
+         folded = (comparison_holds_p (GET_CODE (res), swapped)
+                   ? const_true_rtx : const0_rtx);
+       }
+      ASSERT_RTX_EQ (expected, folded);
+    }
+}
+
+/* Simplify (CODE (CODE0 REG0 REG1) (CODE1 REG0 REG1)), with all results
+   in SImode.  CODE is IOR or a comparison; the latter combines the inner
+   comparisons through AND, IOR or XOR, inverting the first one for all
+   but NE (for example, (ltu X Y) is (and (not X) Y)).  */
+
+static rtx
+simplify_logical_comparison (rtx_code code, rtx_code code0, rtx_code code1,
+                            rtx reg0, rtx reg1)
+{
+  rtx op0 = gen_rtx_fmt_ee (code0, SImode, reg0, reg1);
+  rtx op1 = gen_rtx_fmt_ee (code1, SImode, reg0, reg1);
+  if (code == IOR)
+    return simplify_binary_operation (code, SImode, op0, op1);
+  return simplify_relational_operation (code, SImode, SImode, op0, op1);
+}
+
+/* Test the simplification of IOR and comparisons of two comparisons
+   between operands of mode OP_MODE against an evaluation of them, for
+   all pairs of comparison codes.  */
+
+static void
+test_logical_comparisons_in_mode (machine_mode op_mode)
+{
+  rtx reg0 = make_test_reg (op_mode);
+  rtx reg1 = (GET_MODE_CLASS (op_mode) == MODE_CC
+             ? const0_rtx : make_test_reg (op_mode));
+
+  static const rtx_code codes[] = {
+    EQ, NE, LT, LE, GT, GE, LTGT, ORDERED,
+    UNORDERED, UNEQ, UNLT, UNLE, UNGT, UNGE
+  };
+  static const rtx_code outer_codes[] = { IOR, EQ, NE, LTU, LEU, GTU, GEU };
+
+  bool nans_p = (GET_MODE_CLASS (op_mode) == MODE_CC
+                || HONOR_NANS (op_mode));
+  bool traps_p = HONOR_NANS (op_mode) && flag_trapping_math;
+
+  for (auto code0 : codes)
+    for (auto code1 : codes)
+      for (auto code : outer_codes)
+       {
+         rtx res = simplify_logical_comparison (code, code0, code1,
+                                                reg0, reg1);
+         check_logical_comparison (res, code, SImode,
+                                   gen_rtx_fmt_ee (code0, SImode, reg0, reg1),
+                                   gen_rtx_fmt_ee (code1, SImode, reg0, reg1),
+                                   reg0, reg1, nans_p, traps_p);
+         /* Only a constant result can be expressed for a CC mode, other
+            than by the generic (ior X X) -> X.  */
+         if (GET_MODE_CLASS (op_mode) == MODE_CC
+             && code == IOR
+             && code0 != code1)
+           ASSERT_FALSE (res && COMPARISON_P (res));
+       }
+}
+
+/* Return the code of simplify_logical_comparison's result, or UNKNOWN
+   if there is none.  A constant result has code CONST_INT.  */
+
+static rtx_code
+logical_comparison_code (rtx_code code, rtx_code code0, rtx_code code1,
+                        rtx reg0, rtx reg1)
+{
+  rtx res = simplify_logical_comparison (code, code0, code1, reg0, reg1);
+  return res ? GET_CODE (res) : UNKNOWN;
+}
+
+/* Test combinations of CC-mode, integer and floating-point comparisons,
+   the latter under the various floating-point options.  */
+
+static void
+test_logical_comparisons ()
+{
+  int saved_trapping_math = flag_trapping_math;
+  int saved_finite_math_only = flag_finite_math_only;
+
+  test_logical_comparisons_in_mode (CCmode);
+
+  for (int finite = 0; finite <= 1; ++finite)
+    for (int trapping = 0; trapping <= 1; ++trapping)
+      {
+       flag_finite_math_only = finite;
+       flag_trapping_math = trapping;
+       test_logical_comparisons_in_mode (SImode);
+       test_logical_comparisons_in_mode (DFmode);
+      }
+
+  rtx a = make_test_reg (DFmode);
+  rtx b = make_test_reg (DFmode);
+  rtx cc = make_test_reg (CCmode);
+
+  /* All outcomes are possible for a CC mode: x < 0 || x >= 0 is not
+     always true.  */
+  ASSERT_EQ (logical_comparison_code (IOR, LT, GE, cc, const0_rtx),
+            UNKNOWN);
+  ASSERT_EQ (logical_comparison_code (IOR, EQ, NE, cc, const0_rtx),
+            CONST_INT);
+
+  if (MODE_HAS_NANS (DFmode))
+    {
+      flag_finite_math_only = 0;
+      flag_trapping_math = 1;
+      /* These would add a trap: ORDERED & NE -> LTGT, ORDERED & UNLT -> LT.  
*/
+      ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, NE, a, b), UNKNOWN);
+      ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, UNLT, a, b),
+                UNKNOWN);
+      /* These would drop one: UNLT | GT -> NE, LT ^ LE -> EQ,
+        !UNGE & GT -> false.  */
+      ASSERT_EQ (logical_comparison_code (IOR, UNLT, GT, a, b), UNKNOWN);
+      ASSERT_EQ (logical_comparison_code (NE, LT, LE, a, b), UNKNOWN);
+      ASSERT_EQ (logical_comparison_code (LTU, UNGE, GT, a, b), UNKNOWN);
+      /* These do not.  */
+      ASSERT_EQ (logical_comparison_code (IOR, LT, GT, a, b), LTGT);
+      ASSERT_EQ (logical_comparison_code (IOR, LT, EQ, a, b), LE);
+      ASSERT_EQ (logical_comparison_code (IOR, UNORDERED, EQ, a, b), UNEQ);
+      ASSERT_EQ (logical_comparison_code (LTU, UNLT, UNLE, a, b), EQ);
+      ASSERT_EQ (logical_comparison_code (LTU, LT, GE, a, b), GE);
+
+      flag_trapping_math = 0;
+      ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, NE, a, b), LTGT);
+      ASSERT_EQ (logical_comparison_code (LTU, UNORDERED, UNLT, a, b), LT);
+      ASSERT_EQ (logical_comparison_code (IOR, UNLT, GT, a, b), NE);
+      ASSERT_EQ (logical_comparison_code (NE, LT, LE, a, b), EQ);
+      ASSERT_EQ (logical_comparison_code (LTU, UNGE, GT, a, b), CONST_INT);
+    }
+
+  flag_finite_math_only = 1;
+  flag_trapping_math = 1;
+  ASSERT_EQ (logical_comparison_code (IOR, LT, GT, a, b), NE);
+  ASSERT_EQ (logical_comparison_code (IOR, LT, GE, a, b), CONST_INT);
+
+  flag_trapping_math = saved_trapping_math;
+  flag_finite_math_only = saved_finite_math_only;
+}
 
 /* Verify some simplifications involving scalar expressions.  */
 
@@ -9606,6 +9846,7 @@ test_scalar_ops ()
   test_scalar_int_ext_ops2 (DImode, SImode, HImode);
 
   test_comparisons (QImode, HImode, SImode);
+  test_logical_comparisons ();
 }
 
 /* Test vector simplifications involving VEC_DUPLICATE in which the
diff --git a/gcc/testsuite/gcc.dg/torture/cmp-of-cmp-fenv-3.c 
b/gcc/testsuite/gcc.dg/torture/cmp-of-cmp-fenv-3.c
new file mode 100644
index 000000000000..4bb3a3dd6799
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/cmp-of-cmp-fenv-3.c
@@ -0,0 +1,68 @@
+/* PR middle-end/127786 */
+/* 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: LTGT, 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); }
+
+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 } },
+};
+
+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;
+}

Reply via email to