From patchwork Fri Jun 2 07:07:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 102334 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp843352vqr; Fri, 2 Jun 2023 00:08:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7jDFPMV89Ev0LPy95ph/eC2YoK8lXby6IjG1ei9SuQqxSiIZRPGaeFwVC+Zhmi92l4n0G0 X-Received: by 2002:a17:907:25c2:b0:973:8cb7:4d81 with SMTP id ae2-20020a17090725c200b009738cb74d81mr11064125ejc.49.1685689718121; Fri, 02 Jun 2023 00:08:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685689718; cv=none; d=google.com; s=arc-20160816; b=XZaGou8PQjzEOU0TKRai2uC9cWPGpKCaSNo3MUO90t50Koyf4UhzsIJApEskNq0vkW 3cGjlr4yN7uINaYQDRrfymv2GqVfuTpJiGEP8FWR+kvPp1G7qA3oALrvcCn8txY2f+tw Bhs+lZnfhJtR97xHuTWI4fKFzbQ6wcwTFppoqQcsFYn/icPfrw/jzUltnbnUzkY23clI BbklXoYK1F8oexRnecS5vD0/JH0fFkpS3P1q0uekplFNiK3xb7pGGegmlwNs6Q1iRcPa AGcZo2vSQv9InTLX2lFhc4OUau3nSSfSQb0Nl9aIaZP5ddTF3b42aXlIWvY5+IkXg3b5 SAkg== 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 :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=EpSGaohx9Oh3lI8p9rwvgdAHvqzaPmvNwHKX1aW7W7c=; b=fFGHRliIrE0lvYa/2IDdZqniwl7WNWDP8esOFLLXb7bHFZyApVCSAdPgUElPaU54r6 6x7zyy6Ac2TN7zcla3sK3HcNcLnCRhhBR+sQDFUUd8eIlTvxzDc3Pgn/PzHtefTjMAHA DcCB4AJ1DH55WMQQ18gDxI/02mOJVb85AEnw/z/9d/qbWM1ZcG7FdKMZMQbtfZ57Wxv5 ElPOimlexSVbmVQZw7+gq1QPCXp/67uzym6KlhpKjjhsiHliqnPZLsp6W37gcijWqgGM FyA3ob/CM4DQxPnswGfflDI3D4zQqmORL84ESoOSimVFgspwOCmatEcw3FXexu6cQjO2 3jfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=fVQfsDqQ; 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 gb35-20020a170907962300b0096f803afe77si525853ejc.95.2023.06.02.00.08.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 00:08:38 -0700 (PDT) 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=fVQfsDqQ; 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 CE0623856632 for ; Fri, 2 Jun 2023 07:08:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CE0623856632 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685689708; bh=EpSGaohx9Oh3lI8p9rwvgdAHvqzaPmvNwHKX1aW7W7c=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=fVQfsDqQskSgUJAHuct1xa+1JrqKcREzuTG1gjP4pXqVbNa57CBOuy6q84VX+LSlU W+VCWJc1+hZTeDPHMDcg2flYl342RtOeu93aC4uQcybqjSpaBodNEZYqEYLDIwQdG5 SFAuICIyM8JHouLOr1+zQEyUbMpOjbrsMYz4zCa0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by sourceware.org (Postfix) with ESMTPS id CD6A83858C53 for ; Fri, 2 Jun 2023 07:07:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CD6A83858C53 X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="384073229" X-IronPort-AV: E=Sophos;i="6.00,212,1681196400"; d="scan'208";a="384073229" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jun 2023 00:07:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10728"; a="737419708" X-IronPort-AV: E=Sophos;i="6.00,212,1681196400"; d="scan'208";a="737419708" Received: from shvmail02.sh.intel.com ([10.239.244.9]) by orsmga008.jf.intel.com with ESMTP; 02 Jun 2023 00:07:40 -0700 Received: from yanzhang-dev.sh.intel.com (yanzhang-dev.sh.intel.com [10.239.159.126]) by shvmail02.sh.intel.com (Postfix) with ESMTP id EFE6310083C6; Fri, 2 Jun 2023 15:07:39 +0800 (CST) To: gcc-patches@gcc.gnu.org Cc: juzhe.zhong@rivai.ai, kito.cheng@sifive.com, pan2.li@intel.com, yanzhang.wang@intel.com Subject: [PATCH] RISCV: Add -m(no)-omit-leaf-frame-pointer support. Date: Fri, 2 Jun 2023 15:07:26 +0800 Message-Id: <20230602070726.3807539-1-yanzhang.wang@intel.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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 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: "yanzhang.wang--- via Gcc-patches" From: "Li, Pan2 via Gcc-patches" Reply-To: yanzhang.wang@intel.com 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?1767573782016561928?= X-GMAIL-MSGID: =?utf-8?q?1767573782016561928?= From: Yanzhang Wang gcc/ChangeLog: * config/riscv/riscv.cc (riscv_save_reg_p): Save ra for leaf when enabling -mno-omit-leaf-frame-pointer (riscv_option_override): Override omit-frame-pointer. (riscv_frame_pointer_required): Save s0 for non-leaf function (TARGET_FRAME_POINTER_REQUIRED): Override defination * config/riscv/riscv.opt: Add option support. gcc/testsuite/ChangeLog: * gcc.target/riscv/omit-frame-pointer-1.c: New test. * gcc.target/riscv/omit-frame-pointer-2.c: New test. * gcc.target/riscv/omit-frame-pointer-3.c: New test. * gcc.target/riscv/omit-frame-pointer-4.c: New test. * gcc.target/riscv/omit-frame-pointer-test.c: New test. Signed-off-by: Yanzhang Wang Signed-off-by: Yanzhang Wang --- gcc/config/riscv/riscv.cc | 31 ++++++++++++++++++- gcc/config/riscv/riscv.opt | 4 +++ .../gcc.target/riscv/omit-frame-pointer-1.c | 7 +++++ .../gcc.target/riscv/omit-frame-pointer-2.c | 7 +++++ .../gcc.target/riscv/omit-frame-pointer-3.c | 7 +++++ .../gcc.target/riscv/omit-frame-pointer-4.c | 7 +++++ .../riscv/omit-frame-pointer-test.c | 13 ++++++++ 7 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/riscv/omit-frame-pointer-1.c create mode 100644 gcc/testsuite/gcc.target/riscv/omit-frame-pointer-2.c create mode 100644 gcc/testsuite/gcc.target/riscv/omit-frame-pointer-3.c create mode 100644 gcc/testsuite/gcc.target/riscv/omit-frame-pointer-4.c create mode 100644 gcc/testsuite/gcc.target/riscv/omit-frame-pointer-test.c diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 5d2550871c7..e02f9cb50a4 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -408,6 +408,10 @@ static const struct riscv_tune_info riscv_tune_info_table[] = { #include "riscv-cores.def" }; +/* Global variable to distinguish whether we should save and restore s0/fp for + function. */ +static bool riscv_save_frame_pointer; + void riscv_frame_info::reset(void) { total_size = 0; @@ -4744,7 +4748,11 @@ riscv_save_reg_p (unsigned int regno) if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed) return true; - if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return) + /* Need not to use ra for leaf when frame pointer is turned off by option + whatever the omit-leaf-frame's value. */ + bool keep_leaf_ra = frame_pointer_needed && crtl->is_leaf + && !TARGET_OMIT_LEAF_FRAME_POINTER; + if (regno == RETURN_ADDR_REGNUM && (crtl->calls_eh_return || keep_leaf_ra)) return true; /* If this is an interrupt handler, then must save extra registers. */ @@ -6287,6 +6295,15 @@ riscv_option_override (void) if (flag_pic) riscv_cmodel = CM_PIC; + riscv_save_frame_pointer = false; + if (TARGET_OMIT_LEAF_FRAME_POINTER_P (global_options.x_target_flags)) + { + if (!global_options.x_flag_omit_frame_pointer) + riscv_save_frame_pointer = true; + + global_options.x_flag_omit_frame_pointer = 1; + } + /* We get better code with explicit relocs for CM_MEDLOW, but worse code for the others (for now). Pick the best default. */ if ((target_flags_explicit & MASK_EXPLICIT_RELOCS) == 0) @@ -7158,6 +7175,15 @@ riscv_zero_call_used_regs (HARD_REG_SET need_zeroed_hardregs) & ~zeroed_hardregs); } +static bool +riscv_frame_pointer_required (void) +{ + if (riscv_save_frame_pointer && !crtl->is_leaf) + return true; + + return false; +} + /* Initialize the GCC target structure. */ #undef TARGET_ASM_ALIGNED_HI_OP #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t" @@ -7412,6 +7438,9 @@ riscv_zero_call_used_regs (HARD_REG_SET need_zeroed_hardregs) #undef TARGET_ZERO_CALL_USED_REGS #define TARGET_ZERO_CALL_USED_REGS riscv_zero_call_used_regs +#undef TARGET_FRAME_POINTER_REQUIRED +#define TARGET_FRAME_POINTER_REQUIRED riscv_frame_pointer_required + struct gcc_target targetm = TARGET_INITIALIZER; #include "gt-riscv.h" diff --git a/gcc/config/riscv/riscv.opt b/gcc/config/riscv/riscv.opt index ff1dd4ddd4f..c3e2e7c1da4 100644 --- a/gcc/config/riscv/riscv.opt +++ b/gcc/config/riscv/riscv.opt @@ -138,6 +138,10 @@ Enable the CSR checking for the ISA-dependent CRS and the read-only CSR. The ISA-dependent CSR are only valid when the specific ISA is set. The read-only CSR can not be written by the CSR instructions. +momit-leaf-frame-pointer +Target Mask (OMIT_LEAF_FRAME_POINTER) Save +Omit the frame pointer in leaf functions. + Mask(64BIT) Mask(MUL) diff --git a/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-1.c b/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-1.c new file mode 100644 index 00000000000..c96123ea702 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-1.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc -mabi=lp64 -O2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fno-inline" } */ + +#include "omit-frame-pointer-test.c" + +/* { dg-final { scan-assembler-times "sd\tra,\[0-9\]+\\(sp\\)" 2 } } */ +/* { dg-final { scan-assembler-times "sd\ts0,\[0-9\]+\\(sp\\)" 2 } } */ diff --git a/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-2.c b/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-2.c new file mode 100644 index 00000000000..067148c6a58 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-2.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc -mabi=lp64 -O2 -fno-omit-frame-pointer -momit-leaf-frame-pointer -fno-inline" } */ + +#include "omit-frame-pointer-test.c" + +/* { dg-final { scan-assembler-times "sd\tra,\[0-9\]+\\(sp\\)" 1 } } */ +/* { dg-final { scan-assembler-times "sd\ts0,\[0-9\]+\\(sp\\)" 1 } } */ diff --git a/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-3.c b/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-3.c new file mode 100644 index 00000000000..b4d7d6f4f0d --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-3.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc -mabi=lp64 -O2 -fomit-frame-pointer -mno-omit-leaf-frame-pointer -fno-inline" } */ + +#include "omit-frame-pointer-test.c" + +/* { dg-final { scan-assembler-times "sd\tra,\[0-9\]+\\(sp\\)" 1 } } */ +/* { dg-final { scan-assembler-not "sd\ts0,\[0-9\]+\\(sp\\)"} } */ diff --git a/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-4.c b/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-4.c new file mode 100644 index 00000000000..5a5b540ef4e --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-4.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc -mabi=lp64 -O2 -fomit-frame-pointer -momit-leaf-frame-pointer -fno-inline" } */ + +#include "omit-frame-pointer-test.c" + +/* { dg-final { scan-assembler-times "sd\tra,\[0-9\]+\\(sp\\)" 1 } } */ +/* { dg-final { scan-assembler-not "sd\ts0,\[0-9\]+\\(sp\\)"} } */ diff --git a/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-test.c b/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-test.c new file mode 100644 index 00000000000..cf19f001e29 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/omit-frame-pointer-test.c @@ -0,0 +1,13 @@ +int inc(int n) +{ + return n + 1; +} + + +int bar(void) +{ + int n = 100; + n = inc(n); + n = inc(n) + 100; + return n; +}