From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id E2F903858D37; Fri, 28 Apr 2023 07:12:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E2F903858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682665959; bh=bSOs+K0BGlr9Taj91Rv689nRxShF2U1EJIyygwuSgac=; h=From:To:Subject:Date:From; b=C/4h8ZjL+IyOQ4+JX8MvWlJwjyq3GPqZauLGYwxanZ9E3dsBhJc30GB6PPwMqQGRd KCzRO8wGNHJAQef4S/9wHUsjveB+tBdOnh8ulI4O8H0Ud9c4Ppg9qPDhFkTPVVfrk5 IwPhdGhhUGtITT5lzltJO5a33a7aIODF6oVaJmw0= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-318] gimple-range-op: Handle sqrt (basic bounds only) X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: 9ffddbfc738a941ca3d32187a0e3f2616c7dd522 X-Git-Newrev: ba39d2be0bab29cfb68cdb845b98353d11442244 Message-Id: <20230428071239.E2F903858D37@sourceware.org> Date: Fri, 28 Apr 2023 07:12:39 +0000 (GMT) List-Id: https://gcc.gnu.org/g:ba39d2be0bab29cfb68cdb845b98353d11442244 commit r14-318-gba39d2be0bab29cfb68cdb845b98353d11442244 Author: Jakub Jelinek Date: Fri Apr 28 09:10:37 2023 +0200 gimple-range-op: Handle sqrt (basic bounds only) The following patch adds sqrt support (but similarly to sincos, only dumb basic ranges only). Will improve this incrementally and sin/cos as well. 2023-04-28 Jakub Jelinek * gimple-range-op.cc (class cfn_sqrt): New type. (op_cfn_sqrt): New variable. (gimple_range_op_handler::maybe_builtin_call): Handle CASE_CFN_SQRT{,_FN}. * gcc.dg/tree-ssa/range-sqrt.c: New test. * gfortran.dg/ieee/ieee_6.f90: Make x volatile to avoid ranger optimizing sqrt (-1) call away because it is only used in test for whether it returns NaN. Diff: --- gcc/gimple-range-op.cc | 84 ++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/range-sqrt.c | 41 +++++++++++++++ gcc/testsuite/gfortran.dg/ieee/ieee_6.f90 | 2 +- 3 files changed, 126 insertions(+), 1 deletion(-) diff --git a/gcc/gimple-range-op.cc b/gcc/gimple-range-op.cc index b66a4a0360b..6fa26f5d3a2 100644 --- a/gcc/gimple-range-op.cc +++ b/gcc/gimple-range-op.cc @@ -401,6 +401,83 @@ public: } } op_cfn_copysign; +class cfn_sqrt : public range_operator_float +{ +public: + using range_operator_float::fold_range; + using range_operator_float::op1_range; + virtual bool fold_range (frange &r, tree type, + const frange &lh, const frange &, + relation_trio) const final override + { + if (lh.undefined_p ()) + return false; + if (lh.known_isnan () || real_less (&lh.upper_bound (), &dconstm0)) + { + r.set_nan (type); + return true; + } + unsigned bulps + = targetm.libm_function_max_error (CFN_SQRT, TYPE_MODE (type), true); + if (bulps == ~0U) + r.set_varying (type); + else if (bulps == 0) + r.set (type, dconstm0, dconstinf); + else + { + REAL_VALUE_TYPE boundmin = dconstm0; + while (bulps--) + frange_nextafter (TYPE_MODE (type), boundmin, dconstninf); + r.set (type, boundmin, dconstinf); + } + if (!lh.maybe_isnan () && !real_less (&lh.lower_bound (), &dconst0)) + r.clear_nan (); + return true; + } + virtual bool op1_range (frange &r, tree type, + const frange &lhs, const frange &, + relation_trio) const final override + { + if (lhs.undefined_p ()) + return false; + + // A known NAN means the input is [-INF,-0.) U +-NAN. + if (lhs.known_isnan ()) + { + known_nan: + REAL_VALUE_TYPE ub = dconstm0; + frange_nextafter (TYPE_MODE (type), ub, dconstninf); + r.set (type, dconstninf, ub); + // No r.flush_denormals_to_zero (); here - it is a reverse op. + return true; + } + + // Results outside of [-0.0, +Inf] are impossible. + const REAL_VALUE_TYPE &ub = lhs.upper_bound (); + if (real_less (&ub, &dconstm0)) + { + if (!lhs.maybe_isnan ()) + r.set_undefined (); + else + // If lhs could be NAN and finite result is impossible, + // the range is like lhs.known_isnan () above. + goto known_nan; + return true; + } + + if (!lhs.maybe_isnan ()) + { + // If NAN is not valid result, the input cannot include either + // a NAN nor values smaller than -0. + r.set (type, dconstm0, dconstinf, nan_state (false, false)); + return true; + } + + r.set_varying (type); + return true; + } +} op_cfn_sqrt; + class cfn_sincos : public range_operator_float { public: @@ -962,6 +1039,13 @@ gimple_range_op_handler::maybe_builtin_call () m_valid = true; break; + CASE_CFN_SQRT: + CASE_CFN_SQRT_FN: + m_op1 = gimple_call_arg (call, 0); + m_float = &op_cfn_sqrt; + m_valid = true; + break; + CASE_CFN_SIN: CASE_CFN_SIN_FN: m_op1 = gimple_call_arg (call, 0); diff --git a/gcc/testsuite/gcc.dg/tree-ssa/range-sqrt.c b/gcc/testsuite/gcc.dg/tree-ssa/range-sqrt.c new file mode 100644 index 00000000000..d2a2626add3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/range-sqrt.c @@ -0,0 +1,41 @@ +// { dg-do compile } +// { dg-options "-O2 -fdump-tree-evrp -fno-thread-jumps" } + +#include + +void use (double); +void link_error (); + +void +foo (double x) +{ + if (__builtin_isnan (x)) + __builtin_unreachable (); + x = sqrt (x); + if (x < -0.0) + link_error (); + use (x); +} + +void +bar (double x) +{ + if (!__builtin_isnan (sqrt (x))) + { + if (__builtin_isnan (x)) + link_error (); + if (x < -0.0) + link_error (); + } +} + +void +stool (double x) +{ + double res1 = sqrt (x); + double res2 = __builtin_sqrt (x); + if (res1 < -0.0 || res2 < -0.0) + link_error (); +} + +// { dg-final { scan-tree-dump-not "link_error" "evrp" { target { { *-*-linux* } && { glibc } } } } } diff --git a/gcc/testsuite/gfortran.dg/ieee/ieee_6.f90 b/gcc/testsuite/gfortran.dg/ieee/ieee_6.f90 index 1af7ed395b0..fd637ebbe38 100644 --- a/gcc/testsuite/gfortran.dg/ieee/ieee_6.f90 +++ b/gcc/testsuite/gfortran.dg/ieee/ieee_6.f90 @@ -12,7 +12,7 @@ type(ieee_status_type) :: s1, s2 logical :: flags(5), halt(5), haltworks type(ieee_round_type) :: mode - real :: x + real, volatile :: x ! Test IEEE_GET_STATUS and IEEE_SET_STATUS