From patchwork Mon Mar 6 10:11:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 64548 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1756164wrd; Mon, 6 Mar 2023 02:12:07 -0800 (PST) X-Google-Smtp-Source: AK7set8P5HAa9j5gqi/rhPNb0YOhVNyqyaAPDryj6KV6WL+YD3bDPTPVx9XSuwptrOE928O0BVie X-Received: by 2002:a17:906:b10d:b0:8c0:6422:e0c2 with SMTP id u13-20020a170906b10d00b008c06422e0c2mr10529707ejy.22.1678097527203; Mon, 06 Mar 2023 02:12:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678097527; cv=none; d=google.com; s=arc-20160816; b=WV0FioLMUuPrfomhbmpsqBrY+tFQla4jVZhlMqldgg6nSNyedgorKH8vrpy72L6w5i TZz5AaiL2m3BjL2QG56N7KeC+ndC7bwycO0gcmE8o37Y1qE34JtTwGu4HGpFHebESraF /ND3ksVtPypXsu0leyPDMRl17859WtCxM6yAsl07RN/CKEM7reb8w2sgGX8gpAEMjqro 8U5gME41dXA/Ps/laHB29+JSTQg+zbiQ/qz9+MqOq3fFbCDTOFKYoYSYfxICQkcId8AP ARdB88P2IoZZjNhdVWdXk+iWW99naszX4nt8p+mRP232Jo1Ee5XWwE7Yj+gld2wGWjJq 0Awg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:message-id :mime-version:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=FNAX6+YOSoem9hsuByu8B50ptLGz66rHxZFOFR+hGCY=; b=XssV9GDRuO6nUXR8/pdJWx6K2JnwPRnEH/9ZdIWmXmPAiZ/JPubHDjJ45i8PdC4pZ8 qzBvCq7tU/DxqhZp3l2CJLyF77dapX4tnYg1EYw10CMP0+lYRDxFmnazzoXYwd6T/l5v He4uFYTXc/xBkUVotSydJ5K8S1DrEetYpgOqQZkEyD367Y1/2rodjEqmrRIsw98ZYust p6mPTKVjqxO5MnK5ofbpf4dA2h09T4ujN7scBNAHhdMyLQ/JL+j+CetqCjUnNnoCd1HA lOL31ZXvqiZEdRo2XQ/JGcVpkN4zoX1JmazLToxJWRF/Y+sXWPMOp8J97EJzl2KezJB/ 3IUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=hAi3SSGm; 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 f23-20020a170906825700b008e6bdf85401si2626921ejx.869.2023.03.06.02.12.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 02:12:07 -0800 (PST) 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=hAi3SSGm; 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 3FBC63857C55 for ; Mon, 6 Mar 2023 10:12:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3FBC63857C55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678097526; bh=FNAX6+YOSoem9hsuByu8B50ptLGz66rHxZFOFR+hGCY=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=hAi3SSGmh2dRX7k4QBUim7OdB5U6orbBUkJeefKE6mhUbC1+3Zi7IcMUtBZQfspqR JLEPZQWKqs1yxtYcNjnfxFHgjK6CILhNyq6D2IP8WIf6TplawtV5JjapKEBDf7lyf0 qvCyRgWtm7QFny95pcN85ddlIgMApA3RuMmlNXwk= 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 7FEDE3858D39 for ; Mon, 6 Mar 2023 10:11:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7FEDE3858D39 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-out2.suse.de (Postfix) with ESMTPS id 55D151FDDD; Mon, 6 Mar 2023 10:11:21 +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 3CFDA13A66; Mon, 6 Mar 2023 10:11:21 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id M3TaDUm8BWQrcgAAMHmgww (envelope-from ); Mon, 06 Mar 2023 10:11:21 +0000 Date: Mon, 6 Mar 2023 11:11:20 +0100 (CET) To: gcc-patches@gcc.gnu.org cc: Jakub Jelinek , jwakely@redhat.com Subject: [PATCH] [RFC] RAII auto_mpfr and autp_mpz MIME-Version: 1.0 Message-Id: <20230306101121.3CFDA13A66@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 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 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" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759612792656421891?= X-GMAIL-MSGID: =?utf-8?q?1759612792656421891?= 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). I've sofar only build cc1 with the change. Any comments? Thanks, Richard. * 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 | 15 +++++++++++++++ gcc/system.h | 14 ++++++++++++++ gcc/tree-ssa-loop-niter.cc | 10 +--------- 4 files changed, 32 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..2db2ecc94d4 100644 --- a/gcc/realmpfr.h +++ b/gcc/realmpfr.h @@ -24,6 +24,21 @@ #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; + +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 64cd5a49258..99f6c410481 100644 --- a/gcc/system.h +++ b/gcc/system.h @@ -701,6 +701,20 @@ 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; + +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. */