From patchwork Fri Oct 21 07:31:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 6542 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp544002wrr; Fri, 21 Oct 2022 00:33:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4KzX1WxvL+lA13qZ4KYvNtfRb5RY1jhelW7y4SiKv75SIOrivKk1wWenMxu8+SPkj1RGhz X-Received: by 2002:a05:6402:1e8d:b0:454:79a9:201f with SMTP id f13-20020a0564021e8d00b0045479a9201fmr16198949edf.176.1666337580774; Fri, 21 Oct 2022 00:33:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666337580; cv=none; d=google.com; s=arc-20160816; b=M49jNIiQlX6xXxVFQ3yAh9IXoGt+kXH0X7Nk1+NcsZovB9NqEt0/l54o0ygoWsNiQo WyKcphaGA0fkDXAdeLnr5sW5hTKSDuCME/uE2Xn6A85heCm/EyAempIixlj8nolDrRJk p2VvEFWoyxe78qyKRkr+fXPUovppkLdmNvr0AoFjOmCobYNHglom6eV38vbWoKDZLmRY YYKKm0Z+ovX5CO21UXzdms3bvnziNbBpa7aEjFgAUIF9U6T2aUQsPwN/Autgo0qyW7+p uDO20nncJpHVtfif0xzMWjlbYC2Ce6sNqKkIxFKReX072E60y2gDQxuZZMAJLCbMJuTG fXRQ== 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:organization:subject:to:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=v3ZEc7RLzaE6pWmnVx5GloCs2fe7Ef3i8tKQkeTsmVg=; b=UEruJNcuvWOl90qaKwEKph2XPucEjLFHR9AU1a9SB72UyhGKeaeEuod3GpBYHDPkCa Fk29hZxnJszbxxpKjSY+CdsoRUT50sT6Jzj0ZX53U1SbNoi7WQNlWp7byUIyZNdDlRWh BsVwKMMY2IKmtvu5eKBjyQEpQNesLxgkaTk5zleHttshfDkKtPFmpbTArG35DtZySr3D RfwjVlaSxZFy5zIKEkuct5NnZuBFmUpSmRUeIaMHXSH1sO2OlIYfw/wPmLEpQYUWAPqE muiWeYymagivuucreGmRu99vnL8Ec/i3898U+WxFIsXEaEfy8I+oXpxsEXKQOrYjAlrn 2JFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=HmogAR+t; 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 z102-20020a509e6f000000b0045cd7614e59si18639198ede.451.2022.10.21.00.33.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Oct 2022 00:33:00 -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=HmogAR+t; 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 B6850385AC1F for ; Fri, 21 Oct 2022 07:32:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B6850385AC1F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666337579; bh=v3ZEc7RLzaE6pWmnVx5GloCs2fe7Ef3i8tKQkeTsmVg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=HmogAR+tEPqU13TqglRBPPAXKXc8JJeKCQAQxMrMTHYnBThrFPE/6Nv5/4JXZxCcC /tuFS8GaHRnlnleGgGWQarb2d+zZ1aeir3c2eThx9UUxyh+CyMuG1RSlS41X2cHBlr UIdzRIofa7dmb07U84W20PoAYxxAZTto+n06IdRM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTPS id 9BA693857349 for ; Fri, 21 Oct 2022 07:31:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9BA693857349 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 7C54E116105; Fri, 21 Oct 2022 03:31:18 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 1Jx8UIn9WGYB; Fri, 21 Oct 2022 03:31:18 -0400 (EDT) Received: from free.home (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPS id F2493116100; Fri, 21 Oct 2022 03:31:17 -0400 (EDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id 29L7V9og964525 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 21 Oct 2022 04:31:10 -0300 To: gcc-patches@gcc.gnu.org Subject: [PATCH zero-call-used-regs] Add leafy mode for zero-call-used-regs Organization: Free thinker, does not speak for AdaCore Date: Fri, 21 Oct 2022 04:31:09 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_NONE, SPF_PASS, 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: Alexandre Oliva via Gcc-patches From: Alexandre Oliva Reply-To: Alexandre Oliva 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?1747281595101552514?= X-GMAIL-MSGID: =?utf-8?q?1747281595101552514?= Introduce 'leafy' to auto-select between 'used' and 'all' for leaf and nonleaf functions, respectively. Regstrapped on x86_64-linux-gnu. Ok to install? for gcc/ChangeLog * doc/extend.texi (zero-call-used-regs): Document leafy and variants thereof. * flag-types.h (zero_regs_flags): Add LEAFY_MODE, as well as LEAFY and variants. * function.cc (gen_call_ued_regs_seq): Set only_used for leaf functions in leafy mode. * opts.cc (zero_call_used_regs_opts): Add leafy and variants. for gcc/testsuite/ChangeLog * c-c++-common/zero-scratch-regs-leafy-1.c: New. * c-c++-common/zero-scratch-regs-leafy-2.c: New. * gcc.target/i386/zero-scratch-regs-leafy-1.c: New. * gcc.target/i386/zero-scratch-regs-leafy-2.c: New. --- gcc/doc/extend.texi | 22 ++++++++++++++++++-- gcc/flag-types.h | 5 +++++ gcc/function.cc | 3 +++ gcc/opts.cc | 4 ++++ .../c-c++-common/zero-scratch-regs-leafy-1.c | 15 ++++++++++++++ .../c-c++-common/zero-scratch-regs-leafy-2.c | 21 +++++++++++++++++++ .../gcc.target/i386/zero-scratch-regs-leafy-1.c | 12 +++++++++++ .../gcc.target/i386/zero-scratch-regs-leafy-2.c | 16 +++++++++++++++ 8 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-1.c create mode 100644 gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-2.c create mode 100644 gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-1.c create mode 100644 gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-2.c diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi index 04af0584d82cc..bf11956c467fb 100644 --- a/gcc/doc/extend.texi +++ b/gcc/doc/extend.texi @@ -4391,10 +4391,28 @@ zeros all call-used registers that pass arguments. @item all-gpr-arg zeros all call-used general purpose registers that pass arguments. + +@item leafy +Same as @samp{used} in a leaf function, and same as @samp{all} in a +nonleaf function. + +@item leafy-gpr +Same as @samp{used-gpr} in a leaf function, and same as @samp{all-gpr} +in a nonleaf function. + +@item leafy-arg +Same as @samp{used-arg} in a leaf function, and same as @samp{all-arg} +in a nonleaf function. + +@item leafy-gpr-arg +Same as @samp{used-gpr-arg} in a leaf function, and same as +@samp{all-gpr-arg} in a nonleaf function. + @end table -Of this list, @samp{used-arg}, @samp{used-gpr-arg}, @samp{all-arg}, -and @samp{all-gpr-arg} are mainly used for ROP mitigation. +Of this list, @samp{used-arg}, @samp{used-gpr-arg}, @samp{leafy-arg}, +@samp{leafy-gpr-arg}, @samp{all-arg}, and @samp{all-gpr-arg} are mainly +used for ROP mitigation. The default for the attribute is controlled by @option{-fzero-call-used-regs}. @end table diff --git a/gcc/flag-types.h b/gcc/flag-types.h index d2e751060ffce..b90c85167dcd4 100644 --- a/gcc/flag-types.h +++ b/gcc/flag-types.h @@ -338,6 +338,7 @@ namespace zero_regs_flags { const unsigned int ONLY_GPR = 1UL << 2; const unsigned int ONLY_ARG = 1UL << 3; const unsigned int ENABLED = 1UL << 4; + const unsigned int LEAFY_MODE = 1UL << 5; const unsigned int USED_GPR_ARG = ENABLED | ONLY_USED | ONLY_GPR | ONLY_ARG; const unsigned int USED_GPR = ENABLED | ONLY_USED | ONLY_GPR; const unsigned int USED_ARG = ENABLED | ONLY_USED | ONLY_ARG; @@ -346,6 +347,10 @@ namespace zero_regs_flags { const unsigned int ALL_GPR = ENABLED | ONLY_GPR; const unsigned int ALL_ARG = ENABLED | ONLY_ARG; const unsigned int ALL = ENABLED; + const unsigned int LEAFY_GPR_ARG = ENABLED | LEAFY_MODE | ONLY_GPR | ONLY_ARG; + const unsigned int LEAFY_GPR = ENABLED | LEAFY_MODE | ONLY_GPR; + const unsigned int LEAFY_ARG = ENABLED | LEAFY_MODE | ONLY_ARG; + const unsigned int LEAFY = ENABLED | LEAFY_MODE; } /* Settings of flag_incremental_link. */ diff --git a/gcc/function.cc b/gcc/function.cc index 6474a663b30b8..16582e698041a 100644 --- a/gcc/function.cc +++ b/gcc/function.cc @@ -5879,6 +5879,9 @@ gen_call_used_regs_seq (rtx_insn *ret, unsigned int zero_regs_type) only_used = zero_regs_type & ONLY_USED; only_arg = zero_regs_type & ONLY_ARG; + if ((zero_regs_type & LEAFY_MODE) && leaf_function_p ()) + only_used = true; + /* For each of the hard registers, we should zero it if: 1. it is a call-used register; and 2. it is not a fixed register; diff --git a/gcc/opts.cc b/gcc/opts.cc index ae079fcd20eea..39f6a1b278dc6 100644 --- a/gcc/opts.cc +++ b/gcc/opts.cc @@ -2099,6 +2099,10 @@ const struct zero_call_used_regs_opts_s zero_call_used_regs_opts[] = ZERO_CALL_USED_REGS_OPT (all-gpr, zero_regs_flags::ALL_GPR), ZERO_CALL_USED_REGS_OPT (all-arg, zero_regs_flags::ALL_ARG), ZERO_CALL_USED_REGS_OPT (all, zero_regs_flags::ALL), + ZERO_CALL_USED_REGS_OPT (leafy-gpr-arg, zero_regs_flags::LEAFY_GPR_ARG), + ZERO_CALL_USED_REGS_OPT (leafy-gpr, zero_regs_flags::LEAFY_GPR), + ZERO_CALL_USED_REGS_OPT (leafy-arg, zero_regs_flags::LEAFY_ARG), + ZERO_CALL_USED_REGS_OPT (leafy, zero_regs_flags::LEAFY), #undef ZERO_CALL_USED_REGS_OPT {NULL, 0U} }; diff --git a/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-1.c b/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-1.c new file mode 100644 index 0000000000000..c1a0c31ba1c37 --- /dev/null +++ b/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-1.c @@ -0,0 +1,15 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -fzero-call-used-regs=leafy" } */ + +volatile int result = 0; +int +__attribute__((noipa)) +foo (int x) +{ + return x; +} +int main() +{ + result = foo (2); + return 0; +} diff --git a/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-2.c b/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-2.c new file mode 100644 index 0000000000000..d450620c1fcfe --- /dev/null +++ b/gcc/testsuite/c-c++-common/zero-scratch-regs-leafy-2.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +#include +int result = 0; + +int +__attribute__((noipa)) +__attribute__ ((zero_call_used_regs("leafy"))) +foo1 (int x) +{ + return (x + 1); +} + +int +__attribute__((noipa)) +__attribute__ ((zero_call_used_regs("leafy"))) +foo2 (int x) +{ + return foo1 (x + 2); +} diff --git a/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-1.c b/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-1.c new file mode 100644 index 0000000000000..2277710c771b7 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fzero-call-used-regs=leafy -fno-stack-protector -fno-PIC" } */ + +void +foo (void) +{ +} + +/* { dg-final { scan-assembler-not "vzeroall" } } */ +/* { dg-final { scan-assembler-not "%xmm" } } */ +/* { dg-final { scan-assembler-not "xorl\[ \t\]+%" } } */ +/* { dg-final { scan-assembler-not "movl\[ \t\]+%" } } */ diff --git a/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-2.c b/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-2.c new file mode 100644 index 0000000000000..24b85c3dbb766 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/zero-scratch-regs-leafy-2.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fzero-call-used-regs=leafy-gpr -fno-stack-protector -fno-PIC" } */ + +extern int bar (int); + +void +foo (void) +{ + int x = bar (0); + if (x) + bar (1); +} + +/* { dg-final { scan-assembler "xorl\[ \t\]+%eax, %eax" } } */ +/* { dg-final { scan-assembler "xorl\[ \t\]+%edx, %edx" } } */ +/* { dg-final { scan-assembler "xorl\[ \t\]+%ecx, %ecx" } } */