From patchwork Wed Feb 23 22:35:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1596929 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=efPlvhxg; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4K3rVn6Z4rz9sG7 for ; Thu, 24 Feb 2022 09:36:33 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ADCC0385800A for ; Wed, 23 Feb 2022 22:36:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ADCC0385800A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1645655791; bh=QMU2AL4M9YzU77QACGEeB3FzXIn1DkxjHZCqGwiR7Uk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=efPlvhxgt6Sp4yry/8FodBZnEZfWVfy68LvkpuDjHlsthQQfsf7FLZLEr0yqNB3PU f0FxXNyj3XzvlPdgtft297YAwqgotcoOXYToVHZDlpnTIxe5XZSEe/QMsCZPgMcZ0P S+mCcTXiZdIeW2EiGhD38nP38RBxErKHNmlSDdh0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by sourceware.org (Postfix) with ESMTPS id 8468F385840F for ; Wed, 23 Feb 2022 22:35:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8468F385840F Received: by mail-pj1-x1034.google.com with SMTP id v4so322498pjh.2 for ; Wed, 23 Feb 2022 14:35:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=QMU2AL4M9YzU77QACGEeB3FzXIn1DkxjHZCqGwiR7Uk=; b=1o3U3l0LDuq49v6M9q0UHTSdmNSbFNmp+yHqcxrRlPgtA0aWXXYf5jth/s0imqoBJ2 WoEPW/FDeDfzb20jPEdQ8jl62r2tydE28H5/ZGVbBLOdP07htO/kugOuLt1a0tNllwHn 8gzFGWU89EKZxoMCBbQUskg29MbGAd7+TcssQgoK32IqUcrLW+XKoVLdvtOr38rof0Ho FZEURfuF6r3dJeN2ts0PE27JRhn85p/P/rIqM02FNN3HHE0YiRj5jr02kcdWHt3z2lDz tJTxyyCH1p634UN9kHtTWF5R2kuklxJpQ9JnUwhIuYfZXKLk4VaSO2s7plh4uhJh2J77 7y5A== X-Gm-Message-State: AOAM530iP7HPth13SqWvxh8rubdeTGQhrKEJxILZ3/qqZBglgLBE7eIt oX5tvo98pUaErfcy27UK5SQZYE2/H/E= X-Google-Smtp-Source: ABdhPJzvooodzGyhAOLzKB/r0pUyeEfcvlhlfS/RwDY3uab+JAn4KnHSRedWebq/m4uUu53T/FjtTw== X-Received: by 2002:a17:903:1c2:b0:14d:b46d:8c80 with SMTP id e2-20020a17090301c200b0014db46d8c80mr1802282plh.173.1645655707095; Wed, 23 Feb 2022 14:35:07 -0800 (PST) Received: from gnu-tgl-3.localdomain ([172.58.35.88]) by smtp.gmail.com with ESMTPSA id g63sm554844pfb.65.2022.02.23.14.35.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Feb 2022 14:35:06 -0800 (PST) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 6206AC03BF for ; Wed, 23 Feb 2022 14:35:05 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] x86: Skip ENDBR when emitting direct call/jmp to local function Date: Wed, 23 Feb 2022 14:35:05 -0800 Message-Id: <20220223223505.624645-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3028.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: "H.J. Lu via Gcc-patches" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Skip the 4-byte ENDBR when emitting a direct call/jmp to a local function with ENDBR at function entry. This has been tested on Linux kernel. gcc/ PR target/102953 * config/i386/i386-features.cc (rest_of_insert_endbr_and_patchable_area): Set SYMBOL_FLAG_FUNCTION_ENDBR when inserting ENDBR. * config/i386/i386.cc (ix86_print_operand): Skip the 4-byte ENDBR when calling the local function with ENDBR at function entry. * config/i386/i386.h (SYMBOL_FLAG_FUNCTION_ENDBR): New. (SYMBOL_FLAG_FUNCTION_ENDBR_P): Likewise. gcc/testsuite/ PR target/102953 * gcc.target/i386/pr102953-1.c: New test. * gcc.target/i386/pr102953-2.c: Likewise. --- gcc/config/i386/i386-features.cc | 2 ++ gcc/config/i386/i386.cc | 11 +++++++- gcc/config/i386/i386.h | 5 ++++ gcc/testsuite/gcc.target/i386/pr102953-1.c | 25 ++++++++++++++++++ gcc/testsuite/gcc.target/i386/pr102953-2.c | 30 ++++++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr102953-1.c create mode 100644 gcc/testsuite/gcc.target/i386/pr102953-2.c diff --git a/gcc/config/i386/i386-features.cc b/gcc/config/i386/i386-features.cc index 6fe41c3c24f..3ca1131ed59 100644 --- a/gcc/config/i386/i386-features.cc +++ b/gcc/config/i386/i386-features.cc @@ -1979,6 +1979,8 @@ rest_of_insert_endbr_and_patchable_area (bool need_endbr, || (TARGET_DLLIMPORT_DECL_ATTRIBUTES && DECL_DLLIMPORT_P (cfun->decl)))) { + rtx symbol = XEXP (DECL_RTL (cfun->decl), 0); + SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_FUNCTION_ENDBR; if (crtl->profile && flag_fentry) { /* Queue ENDBR insertion to x86_function_profiler. diff --git a/gcc/config/i386/i386.cc b/gcc/config/i386/i386.cc index b2bf90576d5..33777f942f2 100644 --- a/gcc/config/i386/i386.cc +++ b/gcc/config/i386/i386.cc @@ -13810,7 +13810,16 @@ ix86_print_operand (FILE *file, rtx x, int code) else if (flag_pic || MACHOPIC_INDIRECT) output_pic_addr_const (file, x, code); else - output_addr_const (file, x); + { + /* Skip ENDBR when emitting a direct call/jmp to a local + function with ENDBR at function entry. */ + if (code == 'P' + && GET_CODE (x) == SYMBOL_REF + && SYMBOL_REF_LOCAL_P (x) + && SYMBOL_FLAG_FUNCTION_ENDBR_P (x)) + x = gen_rtx_PLUS (Pmode, x, GEN_INT (4)); + output_addr_const (file, x); + } } } diff --git a/gcc/config/i386/i386.h b/gcc/config/i386/i386.h index f41e0908250..e3e50e1ebbb 100644 --- a/gcc/config/i386/i386.h +++ b/gcc/config/i386/i386.h @@ -2786,6 +2786,11 @@ extern GTY(()) tree ms_va_list_type_node; #define SYMBOL_REF_STUBVAR_P(X) \ ((SYMBOL_REF_FLAGS (X) & SYMBOL_FLAG_STUBVAR) != 0) +/* Flag to mark a function with ENDBR at entry. */ +#define SYMBOL_FLAG_FUNCTION_ENDBR (SYMBOL_FLAG_MACH_DEP << 5) +#define SYMBOL_FLAG_FUNCTION_ENDBR_P(X) \ + ((SYMBOL_REF_FLAGS (X) & SYMBOL_FLAG_FUNCTION_ENDBR) != 0) + extern void debug_ready_dispatch (void); extern void debug_dispatch_window (int); diff --git a/gcc/testsuite/gcc.target/i386/pr102953-1.c b/gcc/testsuite/gcc.target/i386/pr102953-1.c new file mode 100644 index 00000000000..2afad391baf --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr102953-1.c @@ -0,0 +1,25 @@ +/* { dg-do compile { target { ! *-*-darwin* } } } */ +/* { dg-options "-O2 -fno-pic -fplt -fcf-protection" } */ + +extern int func (int); + +extern int i; + +__attribute__ ((noclone, noinline, noipa)) +static int +bar (int x) +{ + if (x == 0) + return x; + return bar (x - 1) + func (x); +} + +void * +foo (void) +{ + i = bar (2); + return bar; +} + +/* { dg-final { scan-assembler-times {call\t_?bar\+4\M} 2 } } */ +/* { dg-final { scan-assembler-times {call\t_?func\M} 1 } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr102953-2.c b/gcc/testsuite/gcc.target/i386/pr102953-2.c new file mode 100644 index 00000000000..5b8d517f4f2 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr102953-2.c @@ -0,0 +1,30 @@ +/* { dg-do compile { target { ! *-*-darwin* } } } */ +/* { dg-options "-O2 -fno-pic -fplt -fcf-protection" } */ + +static int bar (int x); +extern int func (int); + +int +foo (int i) +{ + return bar (i); +} + +void * +bar_p (void) +{ + return bar; +} + +__attribute__ ((noclone, noinline, noipa)) +static int +bar (int x) +{ + if (x == 0) + return x; + return bar (x - 1) + func (x); +} + +/* { dg-final { scan-assembler-times {call\t_?bar\+4\M} 1 } } */ +/* { dg-final { scan-assembler-times {jmp\t_?bar\+4\M} 1 } } */ +/* { dg-final { scan-assembler-times {call\t_?func\M} 1 } } */