From patchwork Thu Sep 22 16:49:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aldy Hernandez X-Patchwork-Id: 1378 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp292281wrt; Thu, 22 Sep 2022 09:50:05 -0700 (PDT) X-Google-Smtp-Source: AMsMyM77nXnBKd1ZasFgK7z1Btmsq5qZgrcFiJwZY8k+cB1URoiGpXNiC4Nv3+JNt9EQ2lOMq9H7 X-Received: by 2002:a17:907:6094:b0:781:229a:2e8b with SMTP id ht20-20020a170907609400b00781229a2e8bmr3536613ejc.99.1663865405643; Thu, 22 Sep 2022 09:50:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663865405; cv=none; d=google.com; s=arc-20160816; b=rJCntKzBVEDLtyOg3LpajPqPBYz7PxAUv7J6tnAEuJT9VrFNKaoQ402QuuxDVMi+x7 GwZ61BjuT5PSSn7ZzdINyz6oepctCCi8S6KCTL6agvfE3k/E7TFe36v9jYiFdywQAf6b UAUDzoPBLdlVPi3zDULLKoiyp9fp9pCfFfkBg8KWazKuOS7IzVTwp3CXOsKWhF6zTplT FCP6mKzIsYhVHQCQLDESqZrCQpUDl+JyIP9Gd814zKy9JLxJuYJtD7/3Gk9R9YGp7/bW FD4JEYTO0eFGRH2Q6ka1nQBlU4b2rvsFEjyFG3b9+1pu3doIggvkJk0XNeM4kcBIfSTz NeWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=mMdZjXsTIwMiUzjkJSOehlDbYKlEFdNGz0rNRn+SKBk=; b=WUjVFSuHoPcWQiE0sB1umLYQXsNIJHRwygk8CDMWomQm4K3XrxTbhTv2N7hkOY0zOw 4cg4B7oJXFwOBhOFIu7oz7wXLE+/Xly2btisejaVWwQA8Tuy56JYmm20MnZSioKypiQK TZQ3Tim9TeNg3UQeoFkpIVPQT7wmYq8jjmtyNbnbVQaIEZ0GTt2aY0AeDRb6g3TBdpIv xtDhwkoVB3/zHC7MeY6WA9iL9EPPOrsP6Syz6rdE1hCV4zTP0qFr8K+EaKcxcs0xTQEl b73GzxrgSc8SXFCEit05xszXmUxDuCMiW8+LONlw5rVpz5apGStpwzyG7Zq+OpcwCI/c +X2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Mg681nnO; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id z7-20020a05640235c700b0043dfc949d31si6214092edc.25.2022.09.22.09.50.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 Sep 2022 09:50:05 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Mg681nnO; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 64AB8385803D for ; Thu, 22 Sep 2022 16:50:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 64AB8385803D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663865404; bh=mMdZjXsTIwMiUzjkJSOehlDbYKlEFdNGz0rNRn+SKBk=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=Mg681nnOrANFS+HbPnRAyX0IHb1rexhcGOq035rLvkI3Nqgdo/gt3BatInoMFuiPE czlHzDy1I7KF3h21MB3H1OL5l0swyRLCJkeGaHi9YUFLwUa2pw9llqiFMb171kOiJw +DhcL1bJ5kraAjlOfQGMoR5jp3IQVcUIWvh0uvKc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id D45DE3858D38 for ; Thu, 22 Sep 2022 16:49:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D45DE3858D38 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-510-HNpWK5ydNguqqAiSOmmdIA-1; Thu, 22 Sep 2022 12:49:18 -0400 X-MC-Unique: HNpWK5ydNguqqAiSOmmdIA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 23116800186; Thu, 22 Sep 2022 16:49:18 +0000 (UTC) Received: from abulafia.quesejoda.com (unknown [10.39.192.141]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D07D340C6EC2; Thu, 22 Sep 2022 16:49:17 +0000 (UTC) Received: from abulafia.quesejoda.com (localhost [127.0.0.1]) by abulafia.quesejoda.com (8.17.1/8.17.1) with ESMTPS id 28MGnGDr2566234 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 22 Sep 2022 18:49:16 +0200 Received: (from aldyh@localhost) by abulafia.quesejoda.com (8.17.1/8.17.1/Submit) id 28MGnGsv2566233; Thu, 22 Sep 2022 18:49:16 +0200 To: GCC patches Subject: [PATCH] frange: drop endpoints to min/max representable numbers for -ffinite-math-only. Date: Thu, 22 Sep 2022 18:49:11 +0200 Message-Id: <20220922164911.2566143-2-aldyh@redhat.com> In-Reply-To: <20220922164911.2566143-1-aldyh@redhat.com> References: <20220922164911.2566143-1-aldyh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Aldy Hernandez via Gcc-patches From: Aldy Hernandez Reply-To: Aldy Hernandez Cc: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1744689331654556510?= X-GMAIL-MSGID: =?utf-8?q?1744689331654556510?= 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; + } + // 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))) { 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); + } 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);