[PATCH 0/2] Keep FP exceptions when combining comparisons [PR127785, PR127786]

Igor Shevlyakov igor@tachyum.com
Fri Oct 9 16:55:52 GMT 2026


Under the default -ftrapping-math, combining two comparisons of the same
operands must not change whether FE_INVALID is raised for a quiet NaN.
Two places get this wrong; both show on x86_64 and have been wrong since
at least GCC 16.

1/2 removes the match.pd rule (ordered & ne) -> ltgt, which turns two
quiet comparisons into a signaling one (PR127785, even at -O0).  As
Andrea suggested in the PR, the generic combine_comparisons pattern
already handles this case and gets the trapping check right.

2/2 makes and_comparisons_1/or_comparisons_1 treat both comparisons as
evaluated (TRUTH_AND_EXPR/TRUTH_OR_EXPR), which is what reassoc and
if-conversion combine; reassoc currently drops the FE_INVALID of one of
them (PR127786).  ifcombine, the one short-circuiting caller, is not
affected after r17-4921.

The new gcc.dg/torture/cmp-of-cmp-fenv.c checks the exceptions at run
time for a set of combined comparisons, including the PR testcases.

Bootstrapped and regtested on x86_64-pc-linux-gnu (-m32/-m64), no new regressions.

Igor Shevlyakov (2):
  match.pd: Remove the ORDERED & NE to LTGT fold [PR127785]
  gimple-fold: Combine two comparisons as both evaluated [PR127786]

 gcc/gimple-fold.cc                            | 21 +++--
 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          | 91 +++++++++++++++++++
 .../gcc.dg/tree-ssa/fp-trapping-cmp-8.c       | 16 ++++
 .../gcc.target/i386/avx10_2-compare-1b.c      |  2 +-
 gcc/testsuite/gcc.target/i386/opt-comi-1.c    |  2 +-
 8 files changed, 153 insertions(+), 13 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
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fp-trapping-cmp-8.c

-- 
2.55.0




More information about the Gcc-patches mailing list