From patchwork Tue Apr 18 13:39:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 1770192 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=uqtS/a6D; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Q14mP0Tq0z1yZy for ; Tue, 18 Apr 2023 23:40:05 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 012003858C74 for ; Tue, 18 Apr 2023 13:40:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 012003858C74 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681825203; bh=4W0/8I0Ww1PQfMggRb1DHBClWgvVOkvOz11YHeK3RaQ=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=uqtS/a6DYSPQ80IBo+XHPGPEnTnL6BG+izVk+Ld8QqEf816v9R0sVs1Ys1U7kOB03 DN9KOgKyxNV5KlP/AD/y2bWmJ8KVM6E8B/JNtIRLYRvc4Nm2CK5CbOT/ElmQMk4mMo GzOHZf9cT2+M1j29D11IeyzZlK0qHj6XChRsfheM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 0CC8C3858D32 for ; Tue, 18 Apr 2023 13:39:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0CC8C3858D32 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 33F17219D6 for ; Tue, 18 Apr 2023 13:39:42 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1FAC113581 for ; Tue, 18 Apr 2023 13:39:42 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id DsFcBp6dPmSiPgAAMHmgww (envelope-from ) for ; Tue, 18 Apr 2023 13:39:42 +0000 Date: Tue, 18 Apr 2023 15:39:41 +0200 (CEST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] RAII auto_mpfr and autp_mpz MIME-Version: 1.0 Message-Id: <20230418133942.1FAC113581@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The following adds two RAII classes, one for mpz_t and one for mpfr_t making object lifetime management easier. Both formerly require explicit initialization with {mpz,mpfr}_init and release with {mpz,mpfr}_clear. I've converted two example places (where lifetime is trivial). As discussed earlier and improved upon comments. Re-bootstrapped and tested on x86_64-unknown-linux-gnu, pushed to trunk. * system.h (class auto_mpz): New, * realmpfr.h (class auto_mpfr): Likewise. * fold-const-call.cc (do_mpfr_arg1): Use auto_mpfr. (do_mpfr_arg2): Likewise. * tree-ssa-loop-niter.cc (bound_difference): Use auto_mpz; --- gcc/fold-const-call.cc | 8 ++------ gcc/realmpfr.h | 20 ++++++++++++++++++++ gcc/system.h | 18 ++++++++++++++++++ gcc/tree-ssa-loop-niter.cc | 10 +--------- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/gcc/fold-const-call.cc b/gcc/fold-const-call.cc index 43819c1f984..fa0b287cc8a 100644 --- a/gcc/fold-const-call.cc +++ b/gcc/fold-const-call.cc @@ -130,14 +130,12 @@ do_mpfr_arg1 (real_value *result, int prec = format->p; mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN; - mpfr_t m; - mpfr_init2 (m, prec); + auto_mpfr m (prec); mpfr_from_real (m, arg, MPFR_RNDN); mpfr_clear_flags (); bool inexact = func (m, m, rnd); bool ok = do_mpfr_ckconv (result, m, inexact, format); - mpfr_clear (m); return ok; } @@ -224,14 +222,12 @@ do_mpfr_arg2 (real_value *result, int prec = format->p; mpfr_rnd_t rnd = format->round_towards_zero ? MPFR_RNDZ : MPFR_RNDN; - mpfr_t m; - mpfr_init2 (m, prec); + auto_mpfr m (prec); mpfr_from_real (m, arg1, MPFR_RNDN); mpfr_clear_flags (); bool inexact = func (m, arg0.to_shwi (), m, rnd); bool ok = do_mpfr_ckconv (result, m, inexact, format); - mpfr_clear (m); return ok; } diff --git a/gcc/realmpfr.h b/gcc/realmpfr.h index 5e032c05f25..3824e62da19 100644 --- a/gcc/realmpfr.h +++ b/gcc/realmpfr.h @@ -24,6 +24,26 @@ #include #include +class auto_mpfr +{ +public: + auto_mpfr () { mpfr_init (m_mpfr); } + explicit auto_mpfr (mpfr_prec_t prec) { mpfr_init2 (m_mpfr, prec); } + ~auto_mpfr () { mpfr_clear (m_mpfr); } + + operator mpfr_t& () { return m_mpfr; } + + auto_mpfr (const auto_mpfr &) = delete; + auto_mpfr &operator= (const auto_mpfr &) = delete; + + friend void mpfr_clear (auto_mpfr&) = delete; + friend void mpfr_init (auto_mpfr&) = delete; + friend void mpfr_init2 (auto_mpfr&, mpfr_prec_t) = delete; + +private: + mpfr_t m_mpfr; +}; + /* Convert between MPFR and REAL_VALUE_TYPE. The caller is responsible for initializing and clearing the MPFR parameter. */ diff --git a/gcc/system.h b/gcc/system.h index cf45db3f97e..71d8a040353 100644 --- a/gcc/system.h +++ b/gcc/system.h @@ -701,6 +701,24 @@ extern int vsnprintf (char *, size_t, const char *, va_list); /* Do not introduce a gmp.h dependency on the build system. */ #ifndef GENERATOR_FILE #include + +class auto_mpz +{ +public: + auto_mpz () { mpz_init (m_mpz); } + ~auto_mpz () { mpz_clear (m_mpz); } + + operator mpz_t& () { return m_mpz; } + + auto_mpz (const auto_mpz &) = delete; + auto_mpz &operator= (const auto_mpz &) = delete; + + friend void mpz_clear (auto_mpz&) = delete; + friend void mpz_init (auto_mpz&) = delete; + +private: + mpz_t m_mpz; +}; #endif /* Get libiberty declarations. */ diff --git a/gcc/tree-ssa-loop-niter.cc b/gcc/tree-ssa-loop-niter.cc index dc4c7a418f6..dcfba2fc7ae 100644 --- a/gcc/tree-ssa-loop-niter.cc +++ b/gcc/tree-ssa-loop-niter.cc @@ -722,7 +722,6 @@ bound_difference (class loop *loop, tree x, tree y, bounds *bnds) tree type = TREE_TYPE (x); tree varx, vary; mpz_t offx, offy; - mpz_t minx, maxx, miny, maxy; int cnt = 0; edge e; basic_block bb; @@ -754,19 +753,12 @@ bound_difference (class loop *loop, tree x, tree y, bounds *bnds) { /* Otherwise, use the value ranges to determine the initial estimates on below and up. */ - mpz_init (minx); - mpz_init (maxx); - mpz_init (miny); - mpz_init (maxy); + auto_mpz minx, maxx, miny, maxy; determine_value_range (loop, type, varx, offx, minx, maxx); determine_value_range (loop, type, vary, offy, miny, maxy); mpz_sub (bnds->below, minx, maxy); mpz_sub (bnds->up, maxx, miny); - mpz_clear (minx); - mpz_clear (maxx); - mpz_clear (miny); - mpz_clear (maxy); } /* If both X and Y are constants, we cannot get any more precise. */