From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2136) id ECD1B3858D32; Sun, 15 Jan 2023 15:43:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ECD1B3858D32 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673797400; bh=Q36oUW1gBFiT7rH3O3VgCILRR5EigOSGN3lIZ8hDtpc=; h=From:To:Subject:Date:From; b=YpGn1vIfZGPwxsFuvwPAtirpGAmMeWDD4uQwJEHnaCkhLc8l40y9eEHW3DcmmNzD7 vHjJ+tSTlr3UFdszGake6H9CakEi0yGRi321Fym16GZzBmLH4MN0VJqqmQjZQdxDMc eJpvqMhvSmR/2+EGJSaj9fOdH0KYY7FdmLOv2HZw= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Aldy Hernandez To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-5179] [PR107608] [range-ops] Avoid folding into INF when flag_trapping_math. X-Act-Checkin: gcc X-Git-Author: Aldy Hernandez X-Git-Refname: refs/heads/master X-Git-Oldrev: 5ba715ed1aaf24d4f07c0d250a0a9df03aa0acb2 X-Git-Newrev: 844190af178c210a6aff6b7eb4dd8c6a49210ff9 Message-Id: <20230115154320.ECD1B3858D32@sourceware.org> Date: Sun, 15 Jan 2023 15:43:20 +0000 (GMT) List-Id: https://gcc.gnu.org/g:844190af178c210a6aff6b7eb4dd8c6a49210ff9 commit r13-5179-g844190af178c210a6aff6b7eb4dd8c6a49210ff9 Author: Aldy Hernandez Date: Tue Jan 10 10:57:16 2023 +0100 [PR107608] [range-ops] Avoid folding into INF when flag_trapping_math. As discussed in the PR, for trapping math, do not fold overflowing operations into +-INF as doing so could elide a trap. There is a minor adjustment to known_isinf() where it was mistakenly returning true for an [infinity U NAN], whereas it should only return true when the range is exclusively +INF or -INF. This is benign, as there were no users of known_isinf up to now. Tested on x86-64 Linux. I also ran the glibc testsuite (git sources) on x86-64 and this patch fixes: -FAIL: math/test-double-lgamma -FAIL: math/test-double-log1p -FAIL: math/test-float-lgamma -FAIL: math/test-float-log1p -FAIL: math/test-float128-catan -FAIL: math/test-float128-catanh -FAIL: math/test-float128-lgamma -FAIL: math/test-float128-log -FAIL: math/test-float128-log1p -FAIL: math/test-float128-y0 -FAIL: math/test-float128-y1 -FAIL: math/test-float32-lgamma -FAIL: math/test-float32-log1p -FAIL: math/test-float32x-lgamma -FAIL: math/test-float32x-log1p -FAIL: math/test-float64-lgamma -FAIL: math/test-float64-log1p -FAIL: math/test-float64x-lgamma -FAIL: math/test-ldouble-lgamma PR tree-optimization/107608 gcc/ChangeLog: * range-op-float.cc (range_operator_float::fold_range): Avoid folding into INF when flag_trapping_math. * value-range.h (frange::known_isinf): Return false for possible NANs. Diff: --- gcc/range-op-float.cc | 21 +++++++++++++++++++++ gcc/value-range.h | 1 + 2 files changed, 22 insertions(+) diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc index 986a3896a4f..74ac4658378 100644 --- a/gcc/range-op-float.cc +++ b/gcc/range-op-float.cc @@ -91,6 +91,27 @@ range_operator_float::fold_range (frange &r, tree type, else r.clear_nan (); + // If the result has overflowed and flag_trapping_math, folding this + // operation could elide an overflow or division by zero exception. + // Avoid returning a singleton +-INF, to keep the propagators (DOM + // and substitute_and_fold_engine) from folding. See PR107608. + if (flag_trapping_math + && MODE_HAS_INFINITIES (TYPE_MODE (type)) + && r.known_isinf () && !op1.known_isinf () && !op2.known_isinf ()) + { + REAL_VALUE_TYPE inf = r.lower_bound (); + if (real_isneg (&inf)) + { + REAL_VALUE_TYPE min = real_min_representable (type); + r.set (type, inf, min); + } + else + { + REAL_VALUE_TYPE max = real_max_representable (type); + r.set (type, max, inf); + } + } + return true; } diff --git a/gcc/value-range.h b/gcc/value-range.h index ea50ed3e64a..f4ac73b499f 100644 --- a/gcc/value-range.h +++ b/gcc/value-range.h @@ -1300,6 +1300,7 @@ inline bool frange::known_isinf () const { return (m_kind == VR_RANGE + && !maybe_isnan () && real_identical (&m_min, &m_max) && real_isinf (&m_min)); }