From patchwork Fri Nov 17 06:36:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yujie X-Patchwork-Id: 1864938 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 4SWnJW3RCvz1yS6 for ; Fri, 17 Nov 2023 17:37:31 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8F56D3858D33 for ; Fri, 17 Nov 2023 06:37:28 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id 70C4D3858D20 for ; Fri, 17 Nov 2023 06:37:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 70C4D3858D20 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 70C4D3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:470:142:3::10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700203037; cv=none; b=CpgkS1+5R/yob+CxPwft29PoAt7pBJPg5wwVEhCHhQELuBoL3wvONkkE84Z2pIKY4oDs4CFSx03KnH3A9+qZpEuknN3Wi1t+YJphbpUupQjXomQVbi19WCM98nScokTR7QIaGAzywoZZUl+uDM4+1TnyFrktV2Mk/thw10+2lls= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700203037; c=relaxed/simple; bh=d9drhaMPke3twWL9VW5XO5J2kieJRmHiLTo6xQIUbZo=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=BAo5fjgFzdCYxDTtcfRxX7XEX+QZ8cNTwEgE8CWjG55nG8d5RBo0bCWzeQ3Rv6qn4XYKW0/Xr94aaid/LWdsMTGC1K0FmJupLTyuv29hnlLHENLr66UcBr70YovDp00cK6T/60m0i9t4qp5Rx4K+S4F7nlTXig9ijdDJuakY/P4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r3sTY-0008FC-9j for gcc-patches@gcc.gnu.org; Fri, 17 Nov 2023 01:37:14 -0500 Received: from loongson.cn (unknown [114.243.104.185]) by gateway (Coremail) with SMTP id _____8DxRvERCldlrL46AA--.50421S3; Fri, 17 Nov 2023 14:37:06 +0800 (CST) Received: from localhost.localdomain (unknown [114.243.104.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Dx3t4QCldlQQxFAA--.7827S2; Fri, 17 Nov 2023 14:37:04 +0800 (CST) From: Yang Yujie To: gcc-patches@gcc.gnu.org Cc: chenglulu@loongson.cn, xuchenghua@loongson.cn, Yang Yujie Subject: [PATCH] LoongArch: Fix eh_return epilogue for normal returns. Date: Fri, 17 Nov 2023 14:36:40 +0800 Message-ID: <20231117063640.1242505-1-yangyujie@loongson.cn> X-Mailer: git-send-email 2.42.1 MIME-Version: 1.0 X-CM-TRANSID: AQAAf8Dx3t4QCldlQQxFAA--.7827S2 X-CM-SenderInfo: 51dqw5pxmlvq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I8E87Iv6x kF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07UE-erUUUUU= Received-SPF: pass client-ip=114.242.206.163; envelope-from=yangyujie@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, SPF_FAIL, SPF_HELO_PASS, 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 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 gcc/ChangeLog: * config/loongarch/loongarch.cc: Do not restore the saved eh_return data registers ($r4-$r7) for a normal return of a function that calls __builtin_eh_return elsewhere. * config/loongarch/loongarch-protos.h: Same. * config/loongarch/loongarch.md: Same. --- gcc/config/loongarch/loongarch-protos.h | 2 +- gcc/config/loongarch/loongarch.cc | 41 ++++++++++++++++--------- gcc/config/loongarch/loongarch.md | 18 +++++++++-- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/gcc/config/loongarch/loongarch-protos.h b/gcc/config/loongarch/loongarch-protos.h index cb8fc36b086..af20b5d7132 100644 --- a/gcc/config/loongarch/loongarch-protos.h +++ b/gcc/config/loongarch/loongarch-protos.h @@ -60,7 +60,7 @@ enum loongarch_symbol_type { extern rtx loongarch_emit_move (rtx, rtx); extern HOST_WIDE_INT loongarch_initial_elimination_offset (int, int); extern void loongarch_expand_prologue (void); -extern void loongarch_expand_epilogue (bool); +extern void loongarch_expand_epilogue (int); extern bool loongarch_can_use_return_insn (void); extern bool loongarch_symbolic_constant_p (rtx, enum loongarch_symbol_type *); diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc index 738911661d7..7f60a4367d3 100644 --- a/gcc/config/loongarch/loongarch.cc +++ b/gcc/config/loongarch/loongarch.cc @@ -1011,20 +1011,30 @@ loongarch_save_restore_reg (machine_mode mode, int regno, HOST_WIDE_INT offset, static void loongarch_for_each_saved_reg (HOST_WIDE_INT sp_offset, - loongarch_save_restore_fn fn) + loongarch_save_restore_fn fn, + bool skip_eh_data_regs_p) { HOST_WIDE_INT offset; /* Save the link register and s-registers. */ offset = cfun->machine->frame.gp_sp_offset - sp_offset; for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++) - if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST)) - { - if (!cfun->machine->reg_is_wrapped_separately[regno]) - loongarch_save_restore_reg (word_mode, regno, offset, fn); + { + /* Special care needs to be taken for $r4-$r7 (EH_RETURN_DATA_REGNO) + when returning normally from a function that calls __builtin_eh_return. + In this case, these registers are saved but should not be restored, + or the return value may be clobbered. */ - offset -= UNITS_PER_WORD; - } + if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST)) + { + if (!(cfun->machine->reg_is_wrapped_separately[regno] + || (skip_eh_data_regs_p + && GP_ARG_FIRST <= regno && regno < GP_ARG_FIRST + 4))) + loongarch_save_restore_reg (word_mode, regno, offset, fn); + + offset -= UNITS_PER_WORD; + } + } /* This loop must iterate over the same space as its companion in loongarch_compute_frame_info. */ @@ -1293,7 +1303,7 @@ loongarch_expand_prologue (void) GEN_INT (-step1)); RTX_FRAME_RELATED_P (emit_insn (insn)) = 1; size -= step1; - loongarch_for_each_saved_reg (size, loongarch_save_reg); + loongarch_for_each_saved_reg (size, loongarch_save_reg, false); } /* Set up the frame pointer, if we're using one. */ @@ -1378,11 +1388,11 @@ loongarch_can_use_return_insn (void) return reload_completed && cfun->machine->frame.total_size == 0; } -/* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P - says which. */ +/* Expand function epilogue for the following insn patterns: + "epilogue" (style == 0) / "sibcall_epilogue" (1) / "eh_return" (2). */ void -loongarch_expand_epilogue (bool sibcall_p) +loongarch_expand_epilogue (int style) { /* Split the frame into two. STEP1 is the amount of stack we should deallocate before restoring the registers. STEP2 is the amount we @@ -1399,7 +1409,8 @@ loongarch_expand_epilogue (bool sibcall_p) bool need_barrier_p = (get_frame_size () + cfun->machine->frame.arg_pointer_offset) != 0; - if (!sibcall_p && loongarch_can_use_return_insn ()) + /* Handle simple returns. */ + if (style == 0 && loongarch_can_use_return_insn ()) { emit_jump_insn (gen_return ()); return; @@ -1475,7 +1486,8 @@ loongarch_expand_epilogue (bool sibcall_p) /* Restore the registers. */ loongarch_for_each_saved_reg (frame->total_size - step2, - loongarch_restore_reg); + loongarch_restore_reg, + crtl->calls_eh_return && style != 2); if (need_barrier_p) loongarch_emit_stack_tie (); @@ -1500,7 +1512,8 @@ loongarch_expand_epilogue (bool sibcall_p) emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx, EH_RETURN_STACKADJ_RTX)); - if (!sibcall_p) + /* Emit return unless doing sibcall. */ + if (style != 1) emit_jump_insn (gen_simple_return_internal (ra)); } diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md index 22814a3679c..f388026d50c 100644 --- a/gcc/config/loongarch/loongarch.md +++ b/gcc/config/loongarch/loongarch.md @@ -3156,7 +3156,7 @@ (define_expand "epilogue" [(const_int 2)] "" { - loongarch_expand_epilogue (false); + loongarch_expand_epilogue (0); DONE; }) @@ -3164,7 +3164,7 @@ (define_expand "sibcall_epilogue" [(const_int 2)] "" { - loongarch_expand_epilogue (true); + loongarch_expand_epilogue (1); DONE; }) @@ -3221,6 +3221,20 @@ (define_expand "eh_return" emit_insn (gen_eh_set_ra_di (operands[0])); else emit_insn (gen_eh_set_ra_si (operands[0])); + + emit_jump_insn (gen_eh_return_internal ()); + emit_barrier (); + DONE; +}) + +(define_insn_and_split "eh_return_internal" + [(eh_return)] + "" + "#" + "epilogue_completed" + [(const_int 0)] +{ + loongarch_expand_epilogue (2); DONE; })