https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127786

--- Comment #7 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Igor Shevlyakov
<[email protected]>:

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

Reply via email to