From patchwork Fri Nov 11 16:22:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 18905 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp839677wru; Fri, 11 Nov 2022 08:22:47 -0800 (PST) X-Google-Smtp-Source: AA0mqf7nHiXjrMObPDMq8dc4VUa/owXR2ylJG/2rEMOIzM5dP+RdVxQFbgn//1cOTKamoBr5FD2m X-Received: by 2002:a50:9352:0:b0:461:7a4f:6392 with SMTP id n18-20020a509352000000b004617a4f6392mr2167205eda.107.1668183767703; Fri, 11 Nov 2022 08:22:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668183767; cv=none; d=google.com; s=arc-20160816; b=CJZemWnlOnZsKqrr20bp5zC1bxqnJNiD2ZTaVSq+wGAK/jnwkQwsURogGFaM/VgiL8 GMYl9RSzaipRReQHFRDcXYsgP+T3JtqL5w3iMEqTcwnNDVmEuTMVwTApn0whnOX0fF9d ZLTViaGILHm10rUJazfeK5gu7a+hRlt9Dwg27ocHrcgHoS44+1578yKq8ysXrHneLqO6 2fB/ovyFprlHcWlvssGyMz1Oo9ZVd3M0WbLndrHTMPK12ZMzDMwQW7waIiMUwKubcYW1 wcv8z5Ah88FXR8x5ACUP9dmKxTzNwHIE7Pl9kcuZZ71MOwu2PlIqhSCbP1CyKZ6+XW92 sijg== 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:mime-version :user-agent:message-id:date:subject:mail-followup-to:to:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=wr3YLsWyOEey4oR0sQc1Z49oLyYxHCfN3rtO3w2n1mc=; b=QiRuV6aOdSFbzbpiYHfiIjS6yn2EtJtRm+HPgBNU3kmttn2BDP1ckjSnQQc4XFMgeB yKPleVTM2PwwDZVwmxYEtl4xGEeJ6Q5xdnLUOxbEt7+r6lt0oLxJHGxMJV0i5nn6BYu8 7RFHY9U0WACSH+pL9SuEx11SLcY3vn9KCc+I6ZIu2RZL9j3qwAYnYXDbne2xXBhUA0Bn 6tpXnyoUE8RKi/nKaDs0XQQupNbSkCKssCSUCXE6PUgPHNOFPekXzkRx4tfbELkCjh5A laZHg2dfXTk/Ogt7A4XC96T0XIqZnMxbTNN+SV5LUqItsjZkzIsoZmVpr0WAGD0EEJ6Q H9lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="sZRig/an"; 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 cs14-20020a170906dc8e00b00715867834e3si2671006ejc.506.2022.11.11.08.22.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 08:22:47 -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="sZRig/an"; 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 AC7253858431 for ; Fri, 11 Nov 2022 16:22:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AC7253858431 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668183766; bh=wr3YLsWyOEey4oR0sQc1Z49oLyYxHCfN3rtO3w2n1mc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=sZRig/an0S7ZLsCC8JQy2g3oY85DWd+Pr5kANnPI2b+/RTXHMvfpgnr7AlzxmUu9K GU9MpbN+kAjBCqniPwujANrEJEk85n2lj2eO3jv5E6e5GmQIkuTr0Mm8kBxH8Nuufi CXDJ3UVrKevvZC11ofTdxcMIolqTiGOAx0Sm4nfA= 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 2B8353858434 for ; Fri, 11 Nov 2022 16:22:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2B8353858434 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 0F83D1FB for ; Fri, 11 Nov 2022 08:22:09 -0800 (PST) Received: from localhost (e121540-lin.manchester.arm.com [10.32.98.62]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7BBC03F73D for ; Fri, 11 Nov 2022 08:22:02 -0800 (PST) To: gcc-patches@gcc.gnu.org Mail-Followup-To: gcc-patches@gcc.gnu.org, richard.sandiford@arm.com Subject: [PATCH] Add a target hook for sibcall epilogues Date: Fri, 11 Nov 2022 16:22:01 +0000 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 X-Spam-Status: No, score=-42.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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Richard Sandiford via Gcc-patches From: Richard Sandiford Reply-To: Richard Sandiford 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?1749217462529087168?= X-GMAIL-MSGID: =?utf-8?q?1749217462529087168?= Epilogues for sibling calls are generated using the sibcall_epilogue pattern. One disadvantage of this approach is that the target doesn't know which call the epilogue is for, even though the code that generates the pattern has the call to hand. Although call instructions are currently rtxes, and so could be passed as an operand to the pattern, the main point of introducing rtx_insn was to move towards separating the rtx and insn types (a good thing IMO). There also isn't an existing practice of passing genuine instructions (as opposed to labels) to instruction patterns. This patch therefore adds a hook that can be defined as an alternative to sibcall_epilogue. The advantage is that it can be passed the call; the disadvantage is that it can't use .md conveniences like generating instructions from textual patterns (although most epilogues are too complex to benefit much from that anyway). Tested on aarch64-linux-gnu and x86_64-linux-gnu. OK to install? Richard gcc/ * doc/gccint/target-macros/miscellaneous-parameters.rst: Add TARGET_EMIT_EPILOGUE_FOR_SIBCALL. * doc/gccint/target-macros/tm.rst.in: Regenerate. * target.def (emit_epilogue_for_sibcall): New hook. * calls.cc (can_implement_as_sibling_call_p): Use it. * function.cc (thread_prologue_and_epilogue_insns): Likewise. (reposition_prologue_and_epilogue_notes): Likewise. * config/aarch64/aarch64-protos.h (aarch64_expand_epilogue): Take an rtx_call_insn * rather than a bool. * config/aarch64/aarch64.cc (aarch64_expand_epilogue): Likewise. (TARGET_EMIT_EPILOGUE_FOR_SIBCALL): Define. * config/aarch64/aarch64.md (epilogue): Update call. (sibcall_epilogue): Delete. --- gcc/calls.cc | 3 ++- gcc/config/aarch64/aarch64-protos.h | 2 +- gcc/config/aarch64/aarch64.cc | 11 +++++++---- gcc/config/aarch64/aarch64.md | 11 +---------- .../target-macros/miscellaneous-parameters.rst | 5 +++++ gcc/doc/gccint/target-macros/tm.rst.in | 11 +++++++++++ gcc/function.cc | 15 +++++++++++++-- gcc/target.def | 9 +++++++++ 8 files changed, 49 insertions(+), 18 deletions(-) diff --git a/gcc/calls.cc b/gcc/calls.cc index 6dd6f73e978..51b664f1b4d 100644 --- a/gcc/calls.cc +++ b/gcc/calls.cc @@ -2493,7 +2493,8 @@ can_implement_as_sibling_call_p (tree exp, tree addr, const args_size &args_size) { - if (!targetm.have_sibcall_epilogue ()) + if (!targetm.have_sibcall_epilogue () + && !targetm.emit_epilogue_for_sibcall) { maybe_complain_about_tail_call (exp, diff --git a/gcc/config/aarch64/aarch64-protos.h b/gcc/config/aarch64/aarch64-protos.h index 238820581c5..3d81c223b01 100644 --- a/gcc/config/aarch64/aarch64-protos.h +++ b/gcc/config/aarch64/aarch64-protos.h @@ -886,7 +886,7 @@ const char * aarch64_gen_far_branch (rtx *, int, const char *, const char *); const char * aarch64_output_probe_stack_range (rtx, rtx); const char * aarch64_output_probe_sve_stack_clash (rtx, rtx, rtx, rtx); void aarch64_err_no_fpadvsimd (machine_mode); -void aarch64_expand_epilogue (bool); +void aarch64_expand_epilogue (rtx_call_insn *); rtx aarch64_ptrue_all (unsigned int); opt_machine_mode aarch64_ptrue_all_mode (rtx); rtx aarch64_convert_sve_data_to_pred (rtx, machine_mode, rtx); diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc index d1f979ebcf8..41a2181a7d3 100644 --- a/gcc/config/aarch64/aarch64.cc +++ b/gcc/config/aarch64/aarch64.cc @@ -10003,7 +10003,7 @@ aarch64_use_return_insn_p (void) from a deallocated stack, and we optimize the unwind records by emitting them all together if possible. */ void -aarch64_expand_epilogue (bool for_sibcall) +aarch64_expand_epilogue (rtx_call_insn *sibcall) { poly_int64 initial_adjust = cfun->machine->frame.initial_adjust; HOST_WIDE_INT callee_adjust = cfun->machine->frame.callee_adjust; @@ -10151,7 +10151,7 @@ aarch64_expand_epilogue (bool for_sibcall) explicitly authenticate. */ if (aarch64_return_address_signing_enabled () - && (for_sibcall || !TARGET_ARMV8_3)) + && (sibcall || !TARGET_ARMV8_3)) { switch (aarch64_ra_sign_key) { @@ -10169,7 +10169,7 @@ aarch64_expand_epilogue (bool for_sibcall) } /* Stack adjustment for exception handler. */ - if (crtl->calls_eh_return && !for_sibcall) + if (crtl->calls_eh_return && !sibcall) { /* We need to unwind the stack by the offset computed by EH_RETURN_STACKADJ_RTX. We have already reset the CFA @@ -10180,7 +10180,7 @@ aarch64_expand_epilogue (bool for_sibcall) } emit_use (gen_rtx_REG (DImode, LR_REGNUM)); - if (!for_sibcall) + if (!sibcall) emit_jump_insn (ret_rtx); } @@ -27906,6 +27906,9 @@ aarch64_libgcc_floating_mode_supported_p #undef TARGET_HAVE_SHADOW_CALL_STACK #define TARGET_HAVE_SHADOW_CALL_STACK true +#undef TARGET_EMIT_EPILOGUE_FOR_SIBCALL +#define TARGET_EMIT_EPILOGUE_FOR_SIBCALL aarch64_expand_epilogue + struct gcc_target targetm = TARGET_INITIALIZER; #include "gt-aarch64.h" diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md index f2e3d905dbb..c4581cb823f 100644 --- a/gcc/config/aarch64/aarch64.md +++ b/gcc/config/aarch64/aarch64.md @@ -868,16 +868,7 @@ (define_expand "epilogue" [(clobber (const_int 0))] "" " - aarch64_expand_epilogue (false); - DONE; - " -) - -(define_expand "sibcall_epilogue" - [(clobber (const_int 0))] - "" - " - aarch64_expand_epilogue (true); + aarch64_expand_epilogue (nullptr); DONE; " ) diff --git a/gcc/doc/gccint/target-macros/miscellaneous-parameters.rst b/gcc/doc/gccint/target-macros/miscellaneous-parameters.rst index b48f91d3fd2..29d6b87b3c3 100644 --- a/gcc/doc/gccint/target-macros/miscellaneous-parameters.rst +++ b/gcc/doc/gccint/target-macros/miscellaneous-parameters.rst @@ -556,6 +556,11 @@ Here are several miscellaneous parameters. :end-before: [TARGET_USE_LATE_PROLOGUE_EPILOGUE] +.. include:: tm.rst.in + :start-after: [TARGET_EMIT_EPILOGUE_FOR_SIBCALL] + :end-before: [TARGET_EMIT_EPILOGUE_FOR_SIBCALL] + + .. include:: tm.rst.in :start-after: [TARGET_MACHINE_DEPENDENT_REORG] :end-before: [TARGET_MACHINE_DEPENDENT_REORG] diff --git a/gcc/doc/gccint/target-macros/tm.rst.in b/gcc/doc/gccint/target-macros/tm.rst.in index 2e789f8723d..a19fa38b811 100644 --- a/gcc/doc/gccint/target-macros/tm.rst.in +++ b/gcc/doc/gccint/target-macros/tm.rst.in @@ -3724,6 +3724,17 @@ [TARGET_USE_LATE_PROLOGUE_EPILOGUE] +[TARGET_EMIT_EPILOGUE_FOR_SIBCALL] +.. function:: void TARGET_EMIT_EPILOGUE_FOR_SIBCALL (rtx_call_insn *call) + + If defined, this hook emits an epilogue sequence for sibling (tail) + call instruction :samp:`call`. Another way of providing epilogues + for sibling calls is to define the ``sibcall_epilogue`` instruction + pattern; the main advantage of this hook over the pattern is that it + has access to the call instruction. + +[TARGET_EMIT_EPILOGUE_FOR_SIBCALL] + [TARGET_MACHINE_DEPENDENT_REORG] .. function:: void TARGET_MACHINE_DEPENDENT_REORG (void) diff --git a/gcc/function.cc b/gcc/function.cc index 3b1ab5d09e5..ecd656da332 100644 --- a/gcc/function.cc +++ b/gcc/function.cc @@ -6224,7 +6224,17 @@ thread_prologue_and_epilogue_insns (void) if (!(CALL_P (insn) && SIBLING_CALL_P (insn))) continue; - if (rtx_insn *ep_seq = targetm.gen_sibcall_epilogue ()) + rtx_insn *ep_seq; + if (targetm.emit_epilogue_for_sibcall) + { + start_sequence (); + targetm.emit_epilogue_for_sibcall (as_a (insn)); + ep_seq = get_insns (); + end_sequence (); + } + else + ep_seq = targetm.gen_sibcall_epilogue (); + if (ep_seq) { start_sequence (); emit_note (NOTE_INSN_EPILOGUE_BEG); @@ -6284,7 +6294,8 @@ reposition_prologue_and_epilogue_notes (void) { if (!targetm.have_prologue () && !targetm.have_epilogue () - && !targetm.have_sibcall_epilogue ()) + && !targetm.have_sibcall_epilogue () + && !targetm.emit_epilogue_for_sibcall) return; /* Since the hash table is created on demand, the fact that it is diff --git a/gcc/target.def b/gcc/target.def index 8b8aef982e8..15fd939200b 100644 --- a/gcc/target.def +++ b/gcc/target.def @@ -4083,6 +4083,15 @@ between instruction sets.", bool, (), hook_bool_void_false) +DEFHOOK +(emit_epilogue_for_sibcall, + "If defined, this hook emits an epilogue sequence for sibling (tail)\n\ +call instruction :samp:`call`. Another way of providing epilogues\n\ +for sibling calls is to define the ``sibcall_epilogue`` instruction\n\ +pattern; the main advantage of this hook over the pattern is that it\n\ +has access to the call instruction.", + void, (rtx_call_insn *call), NULL) + /* Do machine-dependent code transformations. Called just before delayed-branch scheduling. */ DEFHOOK