From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id D492E3A1B40C for ; Fri, 13 Nov 2020 08:20:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D492E3A1B40C Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8B4C1142F for ; Fri, 13 Nov 2020 00:20:36 -0800 (PST) Received: from localhost (e121540-lin.manchester.arm.com [10.32.98.126]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 344703F718 for ; Fri, 13 Nov 2020 00:20:36 -0800 (PST) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: [18/23] recog: Add an RAII class for undoing insn changes References: Date: Fri, 13 Nov 2020 08:20:34 +0000 In-Reply-To: (Richard Sandiford's message of "Fri, 13 Nov 2020 08:10:54 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Fri, 13 Nov 2020 08:20:38 -0000 When using validate_change to make a group of changes, you have to remember to cancel them if something goes wrong. This patch adds an RAII class to make that easier. See the comments in the patch for details and examples. gcc/ * recog.h (insn_change_watermark): New class. --- gcc/recog.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/gcc/recog.h b/gcc/recog.h index d6af2aa66d9..b8de43b95bb 100644 --- a/gcc/recog.h +++ b/gcc/recog.h @@ -503,6 +503,57 @@ alternative_mask get_preferred_alternatives (rtx_insn *, basic_block); bool check_bool_attrs (rtx_insn *); void recog_init (); + +/* This RAII class can help to undo tentative insn changes on failure. + When an object of the class goes out of scope, it undoes all group + changes that have been made via the validate_change machinery and + not yet confirmed via confirm_change_group. + + For example: + + insn_change_watermark watermark; + validate_change (..., true); // A + ... + if (test) + // Undoes change A. + return false; + ... + validate_change (..., true); // B + ... + if (test) + // Undoes changes A and B. + return false; + ... + confirm_change_group (); + + Code that wants to avoid this behavior can use keep (): + + insn_change_watermark watermark; + validate_change (..., true); // A + ... + if (test) + // Undoes change A. + return false; + ... + watermark.keep (); + validate_change (..., true); // B + ... + if (test) + // Undoes change B, but not A. + return false; + ... + confirm_change_group (); */ +class insn_change_watermark +{ +public: + insn_change_watermark () : m_old_num_changes (num_validated_changes ()) {} + ~insn_change_watermark () { cancel_changes (m_old_num_changes); } + void keep () { m_old_num_changes = num_validated_changes (); } + +private: + int m_old_num_changes; +}; + #endif #endif /* GCC_RECOG_H */ -- 2.17.1