From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id 588E53857BA9 for ; Mon, 20 Jun 2022 18:14:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 588E53857BA9 Received: by mail-pl1-x636.google.com with SMTP id a17so8459450pls.6 for ; Mon, 20 Jun 2022 11:14:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ANnbmSquGgHQ4TFvHmrucsoz5uchSYTSH/XWwgOVVQ0=; b=2A6rALsLXRG5V7RthMieAycrlrMr4jH/vASLO5UkGeHYOcbK93s6JD7tRBrrNsGqaQ PX+041Bo3fI5F1FtdRx/9kVDN12E2HcuNQCs6zobzo+echIgGag9PsPTJouYXWu9sB/g UZZ0rAQk3lv2iSgIK0ExBkUIPkoVWKQHTUquWpMHg3n0zDLYKnBdFnJNLEoatEOhaUgL 8EhI6Xunn9ZfWVEcMooA97M7Wv/vcpzjz78WjOGDV05P3mvHMzxBJ3Cpio1mfreJKuV5 930kquqJa4HeMUdLbA7jTsbBJBdbnmyn6z3SUWfUl9xHplFCFk5bJY6a21N9r2YsPm0a FoMQ== X-Gm-Message-State: AJIora/pjrPQwiwtK5c6z1zU8fSiHyf7mnJHGDJvYAs7b7XUtuDM/PHb NgVOzn9KRplxAwwhwTr9WDdiEAXG/eTGwoHLl5onTz/T X-Google-Smtp-Source: AGRyM1v0vs5Vq7KFn5JPE8F2fiKgbkdLe38Ql8cH0eLli6tE0C+suJlDmfLN4nAn1oQ4ez9FfflUG0eDRTG1Z+ZKtjI= X-Received: by 2002:a17:902:7088:b0:167:78c0:e05e with SMTP id z8-20020a170902708800b0016778c0e05emr24781958plk.149.1655748876008; Mon, 20 Jun 2022 11:14:36 -0700 (PDT) MIME-Version: 1.0 References: <20220510162505.2721901-1-hjl.tools@gmail.com> In-Reply-To: <20220510162505.2721901-1-hjl.tools@gmail.com> From: "H.J. Lu" Date: Mon, 20 Jun 2022 11:14:00 -0700 Message-ID: Subject: PING^1 [PATCH] x86: Skip ENDBR when emitting direct call/jmp to local function To: GCC Patches Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3024.4 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.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-List-Received-Date: Mon, 20 Jun 2022 18:14:39 -0000 On Tue, May 10, 2022 at 9:25 AM H.J. Lu wrote: > > Mark a function with SYMBOL_FLAG_FUNCTION_ENDBR when inserting ENDBR at > function entry. 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 86752a6516a..ad1de239bef 100644 > --- a/gcc/config/i386/i386.cc > +++ b/gcc/config/i386/i386.cc > @@ -13787,7 +13787,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 363082ba47b..7a6317fea57 100644 > --- a/gcc/config/i386/i386.h > +++ b/gcc/config/i386/i386.h > @@ -2792,6 +2792,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 } } */ > -- > 2.35.1 > PING. -- H.J.