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 E695B3858C41 for ; Tue, 5 Dec 2023 09:37:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E695B3858C41 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 E695B3858C41 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=1701769031; cv=none; b=pbvxiBSjIuMOBK3Y8QreuBBw6SKSlb+jCwd8oFrIRi01PFvh9LiWRTDrrMCuxogxE4VNc4q057+t8nftih8t1FciKuUDhN9y6HyjsB9EQcRMoKBr0upbmp5aD168WQu0QESx3PYigB/nLxKTghNviWDI6ajr7ZMvYbnhvdlbGiw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701769031; c=relaxed/simple; bh=IlFclPlvSJ9bBhb7p1QIg5lovrJWUetoSXnPpbtaBps=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=ZRFcuPhMLRAuQj6OXdRyW6vm3sKmvjZl+2pyEcyp4AjRjiOtX6CmcfK+DXcMO8gAgqFvfoQCTRFjm7gziIq1YZsYTe1MlbexFmyOosEuz7VB+idSA3CJT8QgheUCw/WnMfSQMJjlPOTsHtJ+GRNum9FRR+qOQpj15avzcnMcrjw= 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 3916C139F for ; Tue, 5 Dec 2023 01:37:56 -0800 (PST) Received: from localhost (e121540-lin.manchester.arm.com [10.32.110.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 439843F6C4 for ; Tue, 5 Dec 2023 01:37:09 -0800 (PST) From: Richard Sandiford 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: Tue, 05 Dec 2023 09:37:08 +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=-22.2 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_NONE,KAM_DMARC_STATUS,KAM_LAZY_DOMAIN_SECURITY,KAM_SHORT,SPF_HELO_NONE,SPF_NONE,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 List-Id: Jeff approved this patch last year (thanks!): https://gcc.gnu.org/pipermail/gcc-patches/2022-November/606362.html I ended up not pushing it then because the things that used it didn't go in. Now pushed after retesting on aarch64-linux-gnu. --- 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). gcc/ * doc/tm.texi.in: Add TARGET_EMIT_EPILOGUE_FOR_SIBCALL. * doc/tm.texi: 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 +---------- gcc/doc/tm.texi | 8 ++++++++ gcc/doc/tm.texi.in | 2 ++ gcc/function.cc | 15 +++++++++++++-- gcc/target.def | 9 +++++++++ 8 files changed, 43 insertions(+), 18 deletions(-) diff --git a/gcc/calls.cc b/gcc/calls.cc index 9edb5831611..cc4210cd456 100644 --- a/gcc/calls.cc +++ b/gcc/calls.cc @@ -2502,7 +2502,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 d2718cc87b3..b0b7d33714d 100644 --- a/gcc/config/aarch64/aarch64-protos.h +++ b/gcc/config/aarch64/aarch64-protos.h @@ -888,7 +888,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 fca64daf2a0..c864f4c0f6f 100644 --- a/gcc/config/aarch64/aarch64.cc +++ b/gcc/config/aarch64/aarch64.cc @@ -7971,7 +7971,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) { aarch64_frame &frame = cfun->machine->frame; poly_int64 initial_adjust = frame.initial_adjust; @@ -8102,7 +8102,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) { /* If the EH_RETURN_TAKEN_RTX flag is set then we need to unwind the stack and jump to the handler, otherwise @@ -8137,7 +8137,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 (aarch_ra_sign_key) { @@ -8155,7 +8155,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); } @@ -26427,6 +26427,9 @@ aarch64_libgcc_floating_mode_supported_p #undef TARGET_CONST_ANCHOR #define TARGET_CONST_ANCHOR 0x1000000 +#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 4a3af6df7e7..202190c2cbf 100644 --- a/gcc/config/aarch64/aarch64.md +++ b/gcc/config/aarch64/aarch64.md @@ -863,16 +863,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/tm.texi b/gcc/doc/tm.texi index 6709c42a48f..bc8ee04d7d5 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -11898,6 +11898,14 @@ the hook might return true if the prologue and epilogue need to switch between instruction sets. @end deftypefn +@deftypefn {Target Hook} void TARGET_EMIT_EPILOGUE_FOR_SIBCALL (rtx_call_insn *@var{call}) +If defined, this hook emits an epilogue sequence for sibling (tail) +call instruction @var{call}. Another way of providing epilogues +for sibling calls is to define the @code{sibcall_epilogue} instruction +pattern; the main advantage of this hook over the pattern is that it +has access to the call instruction. +@end deftypefn + @deftypefn {Target Hook} void TARGET_MACHINE_DEPENDENT_REORG (void) If non-null, this hook performs a target-specific pass over the instruction stream. The compiler will run it at all optimization levels, diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index d1d7cfafdca..5f636abf9fc 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -7786,6 +7786,8 @@ to by @var{ce_info}. @hook TARGET_USE_LATE_PROLOGUE_EPILOGUE +@hook TARGET_EMIT_EPILOGUE_FOR_SIBCALL + @hook TARGET_MACHINE_DEPENDENT_REORG @hook TARGET_INIT_BUILTINS diff --git a/gcc/function.cc b/gcc/function.cc index 704930160c3..89841787ff8 100644 --- a/gcc/function.cc +++ b/gcc/function.cc @@ -6208,7 +6208,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); @@ -6268,7 +6278,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 04715028460..427fe526b5a 100644 --- a/gcc/target.def +++ b/gcc/target.def @@ -4174,6 +4174,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 @var{call}. Another way of providing epilogues\n\ +for sibling calls is to define the @code{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 -- 2.25.1