From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by sourceware.org (Postfix) with ESMTPS id B345A3858C52 for ; Fri, 23 Sep 2022 07:51:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B345A3858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ej1-x631.google.com with SMTP id r18so26117539eja.11 for ; Fri, 23 Sep 2022 00:51:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=RDBH8fGwbxu7WNlkxsXBUVICcEA7o55uC8rE1ej9RJ4=; b=G4NxyVmlwlZ2g4YNd4Afl3Sub8vLALwzKo20fBb/p4kOYVpCNgIOrsFbF0c50Sa+f3 ew4rRR2CBNEziLs4gTTu7+61om0bMT6/Sve6lj0oPa/GxC3c2peUr+aQKhdRLMLpuCHR VWNaPntrtDlC2lSl1cg72mh6X6I1dpTg5YWxdKdf5NWje/Evp2oWWSkNnLjLB8i9hwBN a5wGw97ZymjafcwzBO1jSJbNRdGyIPWEO0BE3YIAupFB1Ik+VY2lr9TaWiDND1oho1iD brVU6LMPscOJYSkaFtQq1RaEkbGWDN8nC8lhMItQAfOCQj5Cm6eSZBYma5tOtOutidz0 Tzbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=RDBH8fGwbxu7WNlkxsXBUVICcEA7o55uC8rE1ej9RJ4=; b=aTAyx2LKWoCzHb3j1N1YeR46REu3ui96ota4iwLxtDaKPbxmRazhheCb4JvVswxUqu +Hrmdrcf/mlp5bkCH5bk03911WSdZRBAqtSv9GB+Or91mMi+nDjjZPREpLOUs7Tj9dMa P7Oz1yJhY6OJJKUGwOhg2Drb/azNZvsKTuLdUOMqbTv7yaz3qCI3dL6etq9Oi//Y72Ux t0SbqdnIo5Gw2+FSL3pHhZ6KVrocE0N8YfKtLoC6qsuk5xR21DLdcz1+v5dOLH6RsY45 Eq7zGJo2KczVFylgP3l7qM+1DFHWEH95yBdDvhYQT0FnmGil0sDzhJ5HaC6Amg80O4rY /dUQ== X-Gm-Message-State: ACrzQf2Ijk80xZbtiXHoGQuesHoCv5mhpgr1ZmWidrfI/abrYd5CBWZ1 J28yoj8kTrWyJ8qZ338Ayjw4zpfTJk+1cGjfwQM= X-Google-Smtp-Source: AMsMyM6odgcgPJxfRjFv6K5oc5MVR7yLcWlqPMXCuKIkehAns1JIHJ96/Gg/rj4k2qmt3MLMImSxHwKI/i3XRM39jx4= X-Received: by 2002:a17:906:8251:b0:781:8016:2dc9 with SMTP id f17-20020a170906825100b0078180162dc9mr6171980ejx.488.1663919486184; Fri, 23 Sep 2022 00:51:26 -0700 (PDT) MIME-Version: 1.0 References: <20220922164911.2566143-1-aldyh@redhat.com> <20220922164911.2566143-2-aldyh@redhat.com> In-Reply-To: From: Richard Biener Date: Fri, 23 Sep 2022 09:51:13 +0200 Message-ID: Subject: Re: [PATCH] frange: drop endpoints to min/max representable numbers for -ffinite-math-only. To: Aldy Hernandez Cc: GCC patches , Jakub Jelinek , Andrew MacLeod Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Sep 23, 2022 at 9:21 AM Aldy Hernandez wrote: > > Ughhh, my bad. I had reworked this as soon as Jakub said we couldn't > cache the min/max in TYPE_MIN/MAX_VALUE, but forgot to send it. And > yes...the incessant wrapping was very annoying. It's all fixed. > > Let me know what you think. Much better. Thanks, Richard. > Aldy > > On Fri, Sep 23, 2022 at 9:04 AM Richard Biener > wrote: > > > > On Thu, Sep 22, 2022 at 6:49 PM Aldy Hernandez wrote: > > > > > > Similarly to how we drop NANs to UNDEFINED when -ffinite-math-only, I > > > think we can drop the numbers outside of the min/max representable > > > numbers to the representable number. > > > > > > This means the endpoings to VR_VARYING for -ffinite-math-only can now > > > be the min/max representable, instead of -INF and +INF. > > > > > > Saturating in the setter means that the upcoming implementation for > > > binary operators no longer have to worry about doing the right > > > thing for -ffinite-math-only. If the range goes outside the limits, > > > it'll get chopped down. > > > > > > How does this look? > > > > > > Tested on x86-64 Linux. > > > > > > gcc/ChangeLog: > > > > > > * range-op-float.cc (build_le): Use vrp_val_*. > > > (build_lt): Same. > > > (build_ge): Same. > > > (build_gt): Same. > > > * value-range.cc (frange::set): Chop ranges outside of the > > > representable numbers for -ffinite-math-only. > > > (frange::normalize_kind): Use vrp_val*. > > > (frange::verify_range): Same. > > > (frange::set_nonnegative): Same. > > > (range_tests_floats): Remove tests that depend on -INF and +INF. > > > * value-range.h (real_max_representable): Add prototype. > > > (real_min_representable): Same. > > > (vrp_val_max): Set max representable number for > > > -ffinite-math-only. > > > (vrp_val_min): Same but for min. > > > (frange::set_varying): Use vrp_val*. > > > --- > > > gcc/range-op-float.cc | 12 +++++++---- > > > gcc/value-range.cc | 46 ++++++++++++++++++++----------------------- > > > gcc/value-range.h | 30 ++++++++++++++++++++++------ > > > 3 files changed, 53 insertions(+), 35 deletions(-) > > > > > > diff --git a/gcc/range-op-float.cc b/gcc/range-op-float.cc > > > index 2bd3dc9253f..15ba19c2deb 100644 > > > --- a/gcc/range-op-float.cc > > > +++ b/gcc/range-op-float.cc > > > @@ -232,7 +232,8 @@ build_le (frange &r, tree type, const frange &val) > > > { > > > gcc_checking_assert (!val.known_isnan ()); > > > > > > - r.set (type, dconstninf, val.upper_bound ()); > > > + REAL_VALUE_TYPE ninf = *TREE_REAL_CST_PTR (vrp_val_min (type)); > > > + r.set (type, ninf, val.upper_bound ()); > > > > > > // Add both zeros if there's the possibility of zero equality. > > > frange_add_zeros (r, type); > > > @@ -257,7 +258,8 @@ build_lt (frange &r, tree type, const frange &val) > > > return false; > > > } > > > // We only support closed intervals. > > > - r.set (type, dconstninf, val.upper_bound ()); > > > + REAL_VALUE_TYPE ninf = *TREE_REAL_CST_PTR (vrp_val_min (type)); > > > + r.set (type, ninf, val.upper_bound ()); > > > return true; > > > } > > > > > > @@ -268,7 +270,8 @@ build_ge (frange &r, tree type, const frange &val) > > > { > > > gcc_checking_assert (!val.known_isnan ()); > > > > > > - r.set (type, val.lower_bound (), dconstinf); > > > + REAL_VALUE_TYPE inf = *TREE_REAL_CST_PTR (vrp_val_max (type)); > > > + r.set (type, val.lower_bound (), inf); > > > > > > // Add both zeros if there's the possibility of zero equality. > > > frange_add_zeros (r, type); > > > @@ -294,7 +297,8 @@ build_gt (frange &r, tree type, const frange &val) > > > } > > > > > > // We only support closed intervals. > > > - r.set (type, val.lower_bound (), dconstinf); > > > + REAL_VALUE_TYPE inf = *TREE_REAL_CST_PTR (vrp_val_max (type)); > > > + r.set (type, val.lower_bound (), inf); > > > return true; > > > } > > > > > > diff --git a/gcc/value-range.cc b/gcc/value-range.cc > > > index 7e8028eced2..e57d60e1bac 100644 > > > --- a/gcc/value-range.cc > > > +++ b/gcc/value-range.cc > > > @@ -338,6 +338,18 @@ frange::set (tree min, tree max, value_range_kind kind) > > > m_neg_nan = false; > > > } > > > > > > + // For -ffinite-math-only we can drop ranges outside the > > > + // representable numbers to min/max for the type. > > > + if (flag_finite_math_only) > > > + { > > > + REAL_VALUE_TYPE min_repr = *TREE_REAL_CST_PTR (vrp_val_min (m_type)); > > > + REAL_VALUE_TYPE max_repr = *TREE_REAL_CST_PTR (vrp_val_max (m_type)); > > > + if (real_less (&m_min, &min_repr)) > > > + m_min = min_repr; > > > + if (real_less (&max_repr, &m_max)) > > > + m_max = max_repr; > > > > I think you want to re-formulate that in terms of real_isinf() and > > change those to the max representable values. > > > > > + } > > > + > > > // Check for swapped ranges. > > > gcc_checking_assert (tree_compare (LE_EXPR, min, max)); > > > > > > @@ -371,8 +383,8 @@ bool > > > frange::normalize_kind () > > > { > > > if (m_kind == VR_RANGE > > > - && real_isinf (&m_min, 1) > > > - && real_isinf (&m_max, 0)) > > > + && vrp_val_is_min (build_real (m_type, m_min)) > > > + && vrp_val_is_max (build_real (m_type, m_max))) > > > > I think this suggests that keeping +-Inf might be the better choice. > > At least you want to change vrp_val_is_min/max to take a REAL_VALUE_TYPE > > to avoid building four tree nodes for just this compare? > > > > > { > > > if (m_pos_nan && m_neg_nan) > > > { > > > @@ -385,8 +397,8 @@ frange::normalize_kind () > > > if (!m_pos_nan || !m_neg_nan) > > > { > > > m_kind = VR_RANGE; > > > - m_min = dconstninf; > > > - m_max = dconstinf; > > > + m_min = *TREE_REAL_CST_PTR (vrp_val_min (m_type)); > > > + m_max = *TREE_REAL_CST_PTR (vrp_val_max (m_type)); > > > return true; > > > } > > > } > > > @@ -706,8 +718,8 @@ frange::verify_range () > > > case VR_VARYING: > > > gcc_checking_assert (m_type); > > > gcc_checking_assert (m_pos_nan && m_neg_nan); > > > - gcc_checking_assert (real_isinf (&m_min, 1)); > > > - gcc_checking_assert (real_isinf (&m_max, 0)); > > > + gcc_checking_assert (vrp_val_is_min (build_real (m_type, m_min))); > > > + gcc_checking_assert (vrp_val_is_max (build_real (m_type, m_max))); > > > return; > > > case VR_RANGE: > > > gcc_checking_assert (m_type); > > > @@ -732,7 +744,8 @@ frange::verify_range () > > > // If all the properties are clear, we better not span the entire > > > // domain, because that would make us varying. > > > if (m_pos_nan && m_neg_nan) > > > - gcc_checking_assert (!real_isinf (&m_min, 1) || !real_isinf (&m_max, 0)); > > > + gcc_checking_assert (!vrp_val_is_min (build_real (m_type, m_min)) > > > + || !vrp_val_is_max (build_real (m_type, m_max))); > > > } > > > > > > // We can't do much with nonzeros yet. > > > @@ -779,7 +792,7 @@ frange::zero_p () const > > > void > > > frange::set_nonnegative (tree type) > > > { > > > - set (type, dconst0, dconstinf); > > > + set (type, dconst0, *TREE_REAL_CST_PTR (vrp_val_max (type))); > > > > > > // Set +NAN as the only possibility. > > > if (HONOR_NANS (type)) > > > @@ -3886,23 +3899,6 @@ range_tests_floats () > > > r0.clear_nan (); > > > ASSERT_FALSE (r0.varying_p ()); > > > > > > - // The endpoints of a VARYING are +-INF. > > > - r0.set_varying (float_type_node); > > > - ASSERT_TRUE (real_identical (&r0.lower_bound (), &dconstninf)); > > > - ASSERT_TRUE (real_identical (&r0.upper_bound (), &dconstinf)); > > > - > > > - // The maximum representable range for a type is still a subset of VARYING. > > > - REAL_VALUE_TYPE q, r; > > > - real_min_representable (&q, float_type_node); > > > - real_max_representable (&r, float_type_node); > > > - r0 = frange (float_type_node, q, r); > > > - // r0 is not a varying, because it does not include -INF/+INF. > > > - ASSERT_FALSE (r0.varying_p ()); > > > - // The upper bound of r0 must be less than +INF. > > > - ASSERT_TRUE (real_less (&r0.upper_bound (), &dconstinf)); > > > - // The lower bound of r0 must be greater than -INF. > > > - ASSERT_TRUE (real_less (&dconstninf, &r0.lower_bound ())); > > > - > > > // For most architectures, where float and double are different > > > // sizes, having the same endpoints does not necessarily mean the > > > // ranges are equal. > > > diff --git a/gcc/value-range.h b/gcc/value-range.h > > > index 3668b331187..74879b76b40 100644 > > > --- a/gcc/value-range.h > > > +++ b/gcc/value-range.h > > > @@ -593,6 +593,8 @@ extern void dump_value_range (FILE *, const vrange *); > > > extern bool vrp_val_is_min (const_tree); > > > extern bool vrp_val_is_max (const_tree); > > > extern bool vrp_operand_equal_p (const_tree, const_tree); > > > +inline void real_max_representable (REAL_VALUE_TYPE *r, const_tree type); > > > +inline void real_min_representable (REAL_VALUE_TYPE *r, const_tree type); > > > > > > inline value_range_kind > > > vrange::kind () const > > > @@ -1009,7 +1011,15 @@ vrp_val_max (const_tree type) > > > return wide_int_to_tree (const_cast (type), max); > > > } > > > if (frange::supports_p (type)) > > > - return build_real (const_cast (type), dconstinf); > > > + { > > > + if (flag_finite_math_only) > > > + { > > > + REAL_VALUE_TYPE r; > > > + real_max_representable (&r, type); > > > + return build_real (const_cast (type), r); > > > + } > > > + return build_real (const_cast (type), dconstinf); > > > > Overall there's too many places affected IMHO. The above also shows > > we're building trees a lot - note that REAL_CST are _not_ shared, so > > each time you call build_real a new REAL_CST tree node is allocated > > (and later GCed if no longer used). > > > > I thought vrp_val_min/max are legacy by now ... > > > > That said, using trees for the range representation might not be the > > very best thing to do, it's unnecessarily wrapping things. > > > > > + } > > > return NULL_TREE; > > > } > > > > > > @@ -1023,7 +1033,15 @@ vrp_val_min (const_tree type) > > > if (POINTER_TYPE_P (type)) > > > return build_zero_cst (const_cast (type)); > > > if (frange::supports_p (type)) > > > - return build_real (const_cast (type), dconstninf); > > > + { > > > + if (flag_finite_math_only) > > > + { > > > + REAL_VALUE_TYPE r; > > > + real_min_representable (&r, type); > > > + return build_real (const_cast (type), r); > > > + } > > > + return build_real (const_cast (type), dconstninf); > > > + } > > > return NULL_TREE; > > > } > > > > > > @@ -1073,8 +1091,8 @@ frange::set_varying (tree type) > > > { > > > m_kind = VR_VARYING; > > > m_type = type; > > > - m_min = dconstninf; > > > - m_max = dconstinf; > > > + m_min = *TREE_REAL_CST_PTR (vrp_val_min (type)); > > > + m_max = *TREE_REAL_CST_PTR (vrp_val_max (type)); > > > m_pos_nan = true; > > > m_neg_nan = true; > > > } > > > @@ -1133,7 +1151,7 @@ frange::clear_nan () > > > // Set R to maximum representable value for TYPE. > > > > > > inline void > > > -real_max_representable (REAL_VALUE_TYPE *r, tree type) > > > +real_max_representable (REAL_VALUE_TYPE *r, const_tree type) > > > { > > > char buf[128]; > > > get_max_float (REAL_MODE_FORMAT (TYPE_MODE (type)), > > > @@ -1145,7 +1163,7 @@ real_max_representable (REAL_VALUE_TYPE *r, tree type) > > > // Set R to minimum representable value for TYPE. > > > > > > inline void > > > -real_min_representable (REAL_VALUE_TYPE *r, tree type) > > > +real_min_representable (REAL_VALUE_TYPE *r, const_tree type) > > > { > > > real_max_representable (r, type); > > > *r = real_value_negate (r); > > > -- > > > 2.37.1 > > > > >