From patchwork Tue Jul 9 15:28:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 1958516 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=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WJPys4mCdz1xrJ for ; Wed, 10 Jul 2024 01:28:37 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D4FF03843899 for ; Tue, 9 Jul 2024 15:28:35 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 15AD5386C599 for ; Tue, 9 Jul 2024 15:28:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 15AD5386C599 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 15AD5386C599 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720538892; cv=none; b=m6WeJMb4vOqQFb8cTOZNxnxeGI2QYItotVNkQribbCBpXKG5HHN0pLSRnVQUr0RkOEYUvgdW8aczWlv7BtvqkMxb1A9glImN0JIlbXCyrfurNzKgx3CHNQSGD7adyQBg66Nr1gnnh9Ok77fXwYAFs+Z6Sjw3Yb+Uytgauhz80XU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720538892; c=relaxed/simple; bh=cbrwXHULXe4J4llax24jhr5LuBOquHVDg/cdzQ8bg1o=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=uFFU9BugeffxcRR4b9UCVDuCLpxL8fK9bWchGBVa9hhJB+pCOof9JpQfsO70SerYa6yGrWNQWaT0djBsqswwN80jdlAV28+qhl6klXkaTuliwxXqvRHdL7gGZ/+EiPcpI1CFHsKdzdITdRjs3ljX9JZJwqouAlW0LybCHfudkT0= ARC-Authentication-Results: i=1; server2.sourceware.org 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 B20DE153B for ; Tue, 9 Jul 2024 08:28:34 -0700 (PDT) Received: from localhost (e121540-lin.manchester.arm.com [10.32.110.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 163633F766 for ; Tue, 9 Jul 2024 08:28:08 -0700 (PDT) From: Richard Sandiford To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: [PATCH 1/2] rtl-ssa: Add replace_nondebug_insn [PR115785] Date: Tue, 09 Jul 2024 16:28:07 +0100 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Spam-Status: No, score=-19.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org change_insns is used to change multiple instructions at once, so that the IR on return is valid & self-consistent. These changes can involve moving instructions, and the new position for one instruction might be expressed in terms of the old position of another instruction that is changing at the same time. change_insns therefore adds placeholder instructions to mark each new instruction position, then replaces each placeholder with the corresponding real instruction. This replacement was done in two steps: removing the old placeholder instruction and inserting the new real instruction. But it's more convenient for the upcoming fix for PR115785 if we do the operation as a single step. That should also be slightly more efficient, since e.g. no splay tree operations are needed. This operation happens purely on the rtl-ssa instruction chain. The placeholders are never represented in rtl. Bootstrapped & regression-tested on aarch64-linux-gnu and x86_64-linux-gnu. OK to install? Richard gcc/ PR rtl-optimization/115785 * rtl-ssa/functions.h (function_info::replace_nondebug_insn): Declare. * rtl-ssa/insns.h (insn_info::order_node::set_uid): New function. (insn_info::remove_note): Declare. * rtl-ssa/insns.cc (insn_info::remove_note): New function. (function_info::replace_nondebug_insn): Likewise. * rtl-ssa/changes.cc (function_info::change_insns): Use replace_nondebug_insn instead of remove_insn + add_insn. --- gcc/rtl-ssa/changes.cc | 5 +---- gcc/rtl-ssa/functions.h | 1 + gcc/rtl-ssa/insns.cc | 42 +++++++++++++++++++++++++++++++++++++++++ gcc/rtl-ssa/insns.h | 4 ++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/gcc/rtl-ssa/changes.cc b/gcc/rtl-ssa/changes.cc index bc80d7da829..6b6f7cd5d3a 100644 --- a/gcc/rtl-ssa/changes.cc +++ b/gcc/rtl-ssa/changes.cc @@ -874,14 +874,11 @@ function_info::change_insns (array_slice changes) } else { - // Remove the placeholder first so that we have a wider range of - // program points when inserting INSN. insn_info *after = placeholder->prev_any_insn (); if (!insn->is_temporary ()) remove_insn (insn); - remove_insn (placeholder); + replace_nondebug_insn (placeholder, insn); insn->set_bb (after->bb ()); - add_insn_after (insn, after); } } } diff --git a/gcc/rtl-ssa/functions.h b/gcc/rtl-ssa/functions.h index e2134621723..8be04f1aa96 100644 --- a/gcc/rtl-ssa/functions.h +++ b/gcc/rtl-ssa/functions.h @@ -274,6 +274,7 @@ private: insn_info::order_node *need_order_node (insn_info *); void add_insn_after (insn_info *, insn_info *); + void replace_nondebug_insn (insn_info *, insn_info *); void append_insn (insn_info *); void remove_insn (insn_info *); diff --git a/gcc/rtl-ssa/insns.cc b/gcc/rtl-ssa/insns.cc index 68365e323ec..7e26bfd978f 100644 --- a/gcc/rtl-ssa/insns.cc +++ b/gcc/rtl-ssa/insns.cc @@ -70,6 +70,16 @@ insn_info::add_note (insn_note *note) *ptr = note; } +// Remove NOTE from the instruction's notes. +void +insn_info::remove_note (insn_note *note) +{ + insn_note **ptr = &m_first_note; + while (*ptr != note) + ptr = &(*ptr)->m_next_note; + *ptr = note->m_next_note; +} + // Implement compare_with for the case in which this insn and OTHER // have the same program point. int @@ -346,6 +356,38 @@ function_info::add_insn_after (insn_info *insn, insn_info *after) } } +// Replace non-debug instruction OLD_INSN with non-debug instruction NEW_INSN. +// NEW_INSN is not currently linked. +void +function_info::replace_nondebug_insn (insn_info *old_insn, insn_info *new_insn) +{ + gcc_assert (!old_insn->is_debug_insn () + && !new_insn->is_debug_insn () + && !new_insn->has_insn_links ()); + + insn_info *prev = old_insn->prev_any_insn (); + insn_info *next_nondebug = old_insn->next_nondebug_insn (); + + // We should never remove the entry or exit block's instructions. + gcc_checking_assert (prev && next_nondebug); + + new_insn->copy_prev_from (old_insn); + new_insn->copy_next_from (old_insn); + + prev->set_next_any_insn (new_insn); + next_nondebug->set_prev_sametype_insn (new_insn); + + new_insn->set_point (old_insn->point ()); + if (insn_info::order_node *order = old_insn->get_order_node ()) + { + order->set_uid (new_insn->uid ()); + old_insn->remove_note (order); + new_insn->add_note (order); + } + + old_insn->clear_insn_links (); +} + // Remove INSN from the function's list of instructions. void function_info::remove_insn (insn_info *insn) diff --git a/gcc/rtl-ssa/insns.h b/gcc/rtl-ssa/insns.h index 1ba56abc2ca..80eae5eaa1e 100644 --- a/gcc/rtl-ssa/insns.h +++ b/gcc/rtl-ssa/insns.h @@ -330,6 +330,9 @@ private: // Return the uid of the instruction that this node describes. int uid () const { return m_data32; } + // Change the uid of the instruction that this node describes. + void set_uid (int uid) { m_data32 = uid; } + // The splay tree pointers. order_node *m_children[2]; order_node *m_parent; @@ -374,6 +377,7 @@ private: void set_bb (bb_info *bb) { m_bb = bb; } void add_note (insn_note *note); + void remove_note (insn_note *note); order_node *get_order_node () const; order_node *get_known_order_node () const;