From patchwork Wed Apr 19 09:36:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 85277 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp250257vqo; Wed, 19 Apr 2023 02:37:25 -0700 (PDT) X-Google-Smtp-Source: AKy350Z9RvrPfusAN1J2GWZZxEdDF/RYacuyl6Q+RE7KkCptbBEBd+QBUCcaj66PO44atCSEiYG/ X-Received: by 2002:a17:906:a93:b0:94f:9cd:464b with SMTP id y19-20020a1709060a9300b0094f09cd464bmr14036945ejf.15.1681897045068; Wed, 19 Apr 2023 02:37:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681897045; cv=none; d=google.com; s=arc-20160816; b=xv0c8jXgk7a9uv3Wx3a2gNbmMBwOcM0pMljHMQHu32YkohHKHwEU150iDrE1SV5FN4 FWzmVdlw/tJVLWwQesrjWtUHM5QfL9VRlzUIgCYekX1r90OoC22+DWWeIsNQo8lHqhDV ngNTc0vettKbHSMyCaPI1WFAz/qAA8qpVjClcvNt8okQ8mlGF+xQzxeC/3cYU0hxSqTV hK/me5fIHicMRKdDO+iVPNOnPqrLeGhzscR6jdk0g7R/ZV9mM0McTXmz5f+gGEgCcO8b 7KGK1qo3vNGya1LfuBjsil9H8wDu4LX2y9gOV5sOkQYP4mKyfJnUpAE7IwAnLv59n70w VHnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=message-id:sender:errors-to:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:user-agent:subject:cc:to:date:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=w+sEn6NEWqvs/JIVRHBRyHi5U5VxAbe4lWIkwtfmHyc=; b=sHD1ZK8Sf6qoNzVN1DT6TWoVgTriN8JWvj5KNwtvFfNuHqHzPsYskHpMe+aaB+O2E0 kw8M0WAGjLc4+5H14Tvjlv3JQSK26gWCb9MPFMAKw+Lik2sMoRYUtmCSPgWdfK8zQ1sK cy5+wlV1uM3rUp2UfhPjnuBURTz22jBpvpo94e9diNVeU3KX3L++6xiKqTZ9X9DCIP+Q QqmebccANI+bjhvq9JuIQqwQSdtELPLbzJazSK6/M5hzkMWu6iv3bHPQOvG8SEMfH097 6lLTCciTsliJMAeOP5ZCX75GPqeTt75fO3Fgn8vm4WAUE1VFCuoB6VqkZXHbjY/Unw0F 28IA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=TqzzcGBq; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id u26-20020a056402065a00b00504b71ec367si6785446edx.130.2023.04.19.02.37.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Apr 2023 02:37:25 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=TqzzcGBq; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 F218A385771D for ; Wed, 19 Apr 2023 09:37:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F218A385771D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681897043; bh=w+sEn6NEWqvs/JIVRHBRyHi5U5VxAbe4lWIkwtfmHyc=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=TqzzcGBqVYKAU7prPaFOB2gcumimSJxIiRyNi+mO1/qlWjGz0chZgIwZiRIua6YI5 kySV+SIR22A2UfM90FnqRIOrkWU3CTWry7dndjnCpyJpOWEkZZfcwQzWguNS7XB7tE ObAIB5H5DS+qL3ModGi/bJUEltIbFoLlk1vnQo6s= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id F01F33858D1E for ; Wed, 19 Apr 2023 09:36:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F01F33858D1E Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 1D7D91FD8A; Wed, 19 Apr 2023 09:36:36 +0000 (UTC) Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 105982C141; Wed, 19 Apr 2023 09:36:35 +0000 (UTC) Date: Wed, 19 Apr 2023 09:36:35 +0000 (UTC) To: gcc-patches@gcc.gnu.org cc: jwakely@redhat.com Subject: [PATCH] Transform more gmp/mpfr uses to use RAII User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, MISSING_MID, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" Message-Id: <20230419093722.F218A385771D@sourceware.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1763596876009537537?= X-GMAIL-MSGID: =?utf-8?q?1763596876009537537?= The following picks up the coccinelle generated patch from Bernhard, leaving out the fortran frontend parts and fixing up the rest. In particular both gmp.h and mpfr.h contain macros like #define mpfr_inf_p(_x) ((_x)->_mpfr_exp == __MPFR_EXP_INF) for which I add operator-> overloads to the auto_* classes. Bootstrapped and tested on x86_64-unknown-linux-gnu. CCed Jonathan just to make sure I'm not doing anything stupid C++ wise here. * system.h (auto_mpz::operator->()): New. * realmpfr.h (auto_mpfr::operator->()): New. * builtins.cc (do_mpfr_lgamma_r): Use auto_mpfr. * real.cc (real_from_string): Likewise. (dconst_e_ptr): Likewise. (dconst_sqrt2_ptr): Likewise. * tree-ssa-loop-niter.cc (refine_value_range_using_guard): Use auto_mpz. (bound_difference_of_offsetted_base): Likewise. (number_of_iterations_ne): Likewise. (number_of_iterations_lt_to_ne): Likewise. * ubsan.cc: Include realmpfr.h. (ubsan_instrument_float_cast): Use auto_mpfr. --- gcc/builtins.cc | 4 +--- gcc/real.cc | 22 +++++----------------- gcc/realmpfr.h | 1 + gcc/system.h | 1 + gcc/tree-ssa-loop-niter.cc | 29 ++++++++--------------------- gcc/ubsan.cc | 9 ++++----- 6 files changed, 20 insertions(+), 46 deletions(-) diff --git a/gcc/builtins.cc b/gcc/builtins.cc index 1bfdc598eec..80b8b89d98b 100644 --- a/gcc/builtins.cc +++ b/gcc/builtins.cc @@ -11084,15 +11084,13 @@ do_mpfr_lgamma_r (tree arg, tree arg_sg, tree type) const int prec = fmt->p; const mpfr_rnd_t rnd = fmt->round_towards_zero? MPFR_RNDZ : MPFR_RNDN; int inexact, sg; - mpfr_t m; tree result_lg; - mpfr_init2 (m, prec); + auto_mpfr m (prec); mpfr_from_real (m, ra, MPFR_RNDN); mpfr_clear_flags (); inexact = mpfr_lgamma (m, &sg, m, rnd); result_lg = do_mpfr_ckconv (m, type, inexact); - mpfr_clear (m); if (result_lg) { tree result_sg; diff --git a/gcc/real.cc b/gcc/real.cc index 126695bf2e2..cf164e5e945 100644 --- a/gcc/real.cc +++ b/gcc/real.cc @@ -2131,7 +2131,6 @@ real_from_string (REAL_VALUE_TYPE *r, const char *str) { /* Decimal floating point. */ const char *cstr = str; - mpfr_t m; bool inexact; while (*cstr == '0') @@ -2148,21 +2147,15 @@ real_from_string (REAL_VALUE_TYPE *r, const char *str) goto is_a_zero; /* Nonzero value, possibly overflowing or underflowing. */ - mpfr_init2 (m, SIGNIFICAND_BITS); + auto_mpfr m (SIGNIFICAND_BITS); inexact = mpfr_strtofr (m, str, NULL, 10, MPFR_RNDZ); /* The result should never be a NaN, and because the rounding is toward zero should never be an infinity. */ gcc_assert (!mpfr_nan_p (m) && !mpfr_inf_p (m)); if (mpfr_zero_p (m) || mpfr_get_exp (m) < -MAX_EXP + 4) - { - mpfr_clear (m); - goto underflow; - } + goto underflow; else if (mpfr_get_exp (m) > MAX_EXP - 4) - { - mpfr_clear (m); - goto overflow; - } + goto overflow; else { real_from_mpfr (r, m, NULL_TREE, MPFR_RNDZ); @@ -2173,7 +2166,6 @@ real_from_string (REAL_VALUE_TYPE *r, const char *str) gcc_assert (r->cl == rvc_normal); /* Set a sticky bit if mpfr_strtofr was inexact. */ r->sig[0] |= inexact; - mpfr_clear (m); } } @@ -2474,12 +2466,10 @@ dconst_e_ptr (void) These constants need to be given to at least 160 bits precision. */ if (value.cl == rvc_zero) { - mpfr_t m; - mpfr_init2 (m, SIGNIFICAND_BITS); + auto_mpfr m (SIGNIFICAND_BITS); mpfr_set_ui (m, 1, MPFR_RNDN); mpfr_exp (m, m, MPFR_RNDN); real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN); - mpfr_clear (m); } return &value; @@ -2517,11 +2507,9 @@ dconst_sqrt2_ptr (void) These constants need to be given to at least 160 bits precision. */ if (value.cl == rvc_zero) { - mpfr_t m; - mpfr_init2 (m, SIGNIFICAND_BITS); + auto_mpfr m (SIGNIFICAND_BITS); mpfr_sqrt_ui (m, 2, MPFR_RNDN); real_from_mpfr (&value, m, NULL_TREE, MPFR_RNDN); - mpfr_clear (m); } return &value; } diff --git a/gcc/realmpfr.h b/gcc/realmpfr.h index 3824e62da19..a2b1bf6a2db 100644 --- a/gcc/realmpfr.h +++ b/gcc/realmpfr.h @@ -32,6 +32,7 @@ public: ~auto_mpfr () { mpfr_clear (m_mpfr); } operator mpfr_t& () { return m_mpfr; } + mpfr_ptr operator-> () { return m_mpfr; } auto_mpfr (const auto_mpfr &) = delete; auto_mpfr &operator= (const auto_mpfr &) = delete; diff --git a/gcc/system.h b/gcc/system.h index 65d514d3c01..c67bc42863f 100644 --- a/gcc/system.h +++ b/gcc/system.h @@ -709,6 +709,7 @@ public: ~auto_mpz () { mpz_clear (m_mpz); } operator mpz_t& () { return m_mpz; } + mpz_ptr operator-> () { return m_mpz; } auto_mpz (const auto_mpz &) = delete; auto_mpz &operator= (const auto_mpz &) = delete; diff --git a/gcc/tree-ssa-loop-niter.cc b/gcc/tree-ssa-loop-niter.cc index dcfba2fc7ae..adf9937b88a 100644 --- a/gcc/tree-ssa-loop-niter.cc +++ b/gcc/tree-ssa-loop-niter.cc @@ -159,17 +159,13 @@ refine_value_range_using_guard (tree type, tree var, if (operand_equal_p (var, c0, 0)) { - mpz_t valc1; - /* Case of comparing VAR with its below/up bounds. */ - mpz_init (valc1); + auto_mpz valc1; wi::to_mpz (wi::to_wide (c1), valc1, TYPE_SIGN (type)); if (mpz_cmp (valc1, below) == 0) cmp = GT_EXPR; if (mpz_cmp (valc1, up) == 0) cmp = LT_EXPR; - - mpz_clear (valc1); } else { @@ -506,7 +502,6 @@ bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y, { int rel = mpz_cmp (x, y); bool may_wrap = !nowrap_type_p (type); - mpz_t m; /* If X == Y, then the expressions are always equal. If X > Y, there are the following possibilities: @@ -529,7 +524,7 @@ bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y, return; } - mpz_init (m); + auto_mpz m; wi::to_mpz (wi::minus_one (TYPE_PRECISION (type)), m, UNSIGNED); mpz_add_ui (m, m, 1); mpz_sub (bnds->up, x, y); @@ -542,8 +537,6 @@ bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y, else mpz_add (bnds->up, bnds->up, m); } - - mpz_clear (m); } /* From condition C0 CMP C1 derives information regarding the @@ -975,7 +968,6 @@ number_of_iterations_ne (class loop *loop, tree type, affine_iv *iv, { tree niter_type = unsigned_type_for (type); tree s, c, d, bits, assumption, tmp, bound; - mpz_t max; niter->control = *iv; niter->bound = final; @@ -1003,12 +995,11 @@ number_of_iterations_ne (class loop *loop, tree type, affine_iv *iv, fold_convert (niter_type, iv->base)); } - mpz_init (max); + auto_mpz max; number_of_iterations_ne_max (max, iv->no_overflow, c, s, bnds, exit_must_be_taken); niter->max = widest_int::from (wi::from_mpz (niter_type, max, false), TYPE_SIGN (niter_type)); - mpz_clear (max); /* Compute no-overflow information for the control iv. This can be proven when below two conditions are satisfied: @@ -1155,9 +1146,8 @@ number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1, tree niter_type = TREE_TYPE (step); tree mod = fold_build2 (FLOOR_MOD_EXPR, niter_type, *delta, step); tree tmod; - mpz_t mmod; tree assumption = boolean_true_node, bound, noloop; - bool ret = false, fv_comp_no_overflow; + bool fv_comp_no_overflow; tree type1 = type; if (POINTER_TYPE_P (type)) type1 = sizetype; @@ -1168,7 +1158,7 @@ number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1, mod = fold_build2 (MINUS_EXPR, niter_type, step, mod); tmod = fold_convert (type1, mod); - mpz_init (mmod); + auto_mpz mmod; wi::to_mpz (wi::to_wide (mod), mmod, UNSIGNED); mpz_neg (mmod, mmod); @@ -1200,7 +1190,7 @@ number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1, assumption = fold_build2 (LE_EXPR, boolean_type_node, iv1->base, bound); if (integer_zerop (assumption)) - goto end; + return false; } if (mpz_cmp (mmod, bnds->below) < 0) noloop = boolean_false_node; @@ -1226,7 +1216,7 @@ number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1, assumption = fold_build2 (GE_EXPR, boolean_type_node, iv0->base, bound); if (integer_zerop (assumption)) - goto end; + return false; } if (mpz_cmp (mmod, bnds->below) < 0) noloop = boolean_false_node; @@ -1254,10 +1244,7 @@ number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1, bounds_add (bnds, wi::to_widest (mod), type); *delta = fold_build2 (PLUS_EXPR, niter_type, *delta, mod); - ret = true; -end: - mpz_clear (mmod); - return ret; + return true; } /* Add assertions to NITER that ensure that the control variable of the loop diff --git a/gcc/ubsan.cc b/gcc/ubsan.cc index 08c1127c8bd..e6ffea3ff8b 100644 --- a/gcc/ubsan.cc +++ b/gcc/ubsan.cc @@ -49,6 +49,7 @@ along with GCC; see the file COPYING3. If not see #include "tree-cfg.h" #include "gimple-fold.h" #include "varasm.h" +#include "realmpfr.h" /* Map from a tree to a VAR_DECL tree. */ @@ -1877,16 +1878,15 @@ ubsan_instrument_float_cast (location_t loc, tree type, tree expr) /* For _Decimal128 up to 34 decimal digits, - sign, dot, e, exponent. */ char buf[64]; - mpfr_t m; int p = REAL_MODE_FORMAT (mode)->p; REAL_VALUE_TYPE maxval, minval; /* Use mpfr_snprintf rounding to compute the smallest representable decimal number greater or equal than 1 << (prec - !uns_p). */ - mpfr_init2 (m, prec + 2); + auto_mpfr m (prec + 2); mpfr_set_ui_2exp (m, 1, prec - !uns_p, MPFR_RNDN); - mpfr_snprintf (buf, sizeof buf, "%.*RUe", p - 1, m); + mpfr_snprintf (buf, sizeof buf, "%.*RUe", p - 1, (mpfr_srcptr) m); decimal_real_from_string (&maxval, buf); max = build_real (expr_type, maxval); @@ -1900,11 +1900,10 @@ ubsan_instrument_float_cast (location_t loc, tree type, tree expr) (-1 << (prec - 1)) - 1. */ mpfr_set_si_2exp (m, -1, prec - 1, MPFR_RNDN); mpfr_sub_ui (m, m, 1, MPFR_RNDN); - mpfr_snprintf (buf, sizeof buf, "%.*RDe", p - 1, m); + mpfr_snprintf (buf, sizeof buf, "%.*RDe", p - 1, (mpfr_srcptr) m); decimal_real_from_string (&minval, buf); min = build_real (expr_type, minval); } - mpfr_clear (m); } else return NULL_TREE;