From patchwork Sat Jan 20 14:27:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 189742 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp1643614dyb; Sat, 20 Jan 2024 06:29:45 -0800 (PST) X-Google-Smtp-Source: AGHT+IEspHFoIeBYvMxe0uOYU7kdbX6U4WsvfAlHMupwFE+xpJc1eEWCUktWyhJFgmLoYGe5TOx3 X-Received: by 2002:a05:6214:2506:b0:67f:c498:2db9 with SMTP id gf6-20020a056214250600b0067fc4982db9mr2149544qvb.106.1705760985512; Sat, 20 Jan 2024 06:29:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705760985; cv=pass; d=google.com; s=arc-20160816; b=vuiBuAOu2QHOavsJIWhZWUx7kTd5vbREwG24xzfFKpP4hU4ylUc15trfT6G/skcUgE RyUuOneHnxK8Tf8cqesnKS4huwNEKO0a0vAtzDlVrdSOwYK3J1NUPbzZ7nFKnaEu8u1o 5Wd/zS3Mbuq5DNEAHcPw9pR48FVxgvcUnKzBXL9XsoDZ0USUijMvxTKRpq3GJyPD8R65 XykyI/OYqs7vl366Y2Pi1KXE0dnQQGR+k+4rZgP0oYnbCJEXWlpCG3Bqy/N3Ajt187wM qVvacui6fDSkTXifa8sOljBkdTReACVCyCAYOTMS6fgjYenC3McjI2P1Nd8vp8GzyfCk HUDw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=4U5Wz8a1fPs71GIfINplqtprSQ8++frLuNcDJiW/h/c=; fh=DkbU5qKJ1pwscM07d9qZ8pO3cz62HhAUS2BaFSQ0JHM=; b=UHFGSCQi6Cts3CbPGEBYA43tcRZ75oPyQAKsV6Ni8o+3jwDDNswtol+k4Q18X+r+lk A5iqf48f1JhUFKlmVYBXHY7PRGU2S2sN13wX3tVIpeSS+8JuiCTpTP6pFZPYarrxST9d Az+lpAikc8utG4b2HNVyqJ3qJQB7xUULCk1F2z7gz2DLu5gWtdLMpPBbJHWgiGZ5M9Vy 2GE3a3adoQVSY1Qo+9k2XKsHEHMJsIRtoCOiMz+hqruiy1HbQ+DlomYTtdaAWGe7hJ3F 1U9jF3BTCzDHIhrq/fm1kLF4TwY7N1LwYm1OW/lGzeyL6mD/hd7IabUAXO4xCwk+IDwg TrGg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=lriiDnJ5; arc=pass (i=1); 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d4-20020a0cb2c4000000b006816c0b1f82si1587807qvf.446.2024.01.20.06.29.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jan 2024 06:29:45 -0800 (PST) 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=@gmail.com header.s=20230601 header.b=lriiDnJ5; arc=pass (i=1); 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=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 65FBB38582BF for ; Sat, 20 Jan 2024 14:29:44 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id B3B923858C2A for ; Sat, 20 Jan 2024 14:27:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B3B923858C2A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B3B923858C2A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705760879; cv=none; b=YdyBeL8UKazlBAs0q8FswD4akZh6uYuVNKuJawFxHMaMpGgy2QddZjDdoMeBs+WuhUaZeswktgZYjGtwNWcyW4eLGNUWJBzEHkarXg2L/HBb1ajv+ZqueAuuq1evcg0l6FmYFmgtzm7fcWv1EaTn9yp1/8SzVveGrYBWgulXLyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705760879; c=relaxed/simple; bh=vgaFccf0sJPH5wUV6v2h+6b56rhD95lIaKtBlyaBfCU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=M1Lia1nQ2DAxJOyHzi7yhKRTsIeGzAURk30NWSob2KUN9uK38g96h0mcD5lLQfb9Lne++vA7JD99cfsw1+mvAyG3MC0EwL2GAM+TnaolcjvIWTjhikorD3R4E1NcRH5Juw5ntVGeZJMLEar8+jcJ0nXfSek6leVySMt1Mg+JeyU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-6dbd07916d0so325707b3a.0 for ; Sat, 20 Jan 2024 06:27:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705760875; x=1706365675; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4U5Wz8a1fPs71GIfINplqtprSQ8++frLuNcDJiW/h/c=; b=lriiDnJ531kVWAgQA955Yq/TiRlD85zd6baQ8FGM4wH7tQTzB1QQ+53xNyYccEGBUL ruZdmFAHfxSiW46YO9dpiLkY2tGp/WNDdoTJpz9Jr0sCd4tUrGD+45ckMnYudcxdDWjv 3nUK20Pak7IMPja9dEeJgbAo3wn5UKOyMIOU6ryxuL4Ch9bIbZcVYNXVk1BwvMDM5p+9 +idx3FQWjFS1xd4kGiEXj9uQtcXlnWxQmlPtIL6mB5geABDNdCbRlv+JjsPsNxr1+sh1 a5OAheZ6vm0mD4CTTcNuzUmhkpaTflcjs98/lBQsXkO2EyQDuIVahoOV7bi4u4IGrdxB kfMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705760875; x=1706365675; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4U5Wz8a1fPs71GIfINplqtprSQ8++frLuNcDJiW/h/c=; b=CqEp+0JILap+W7SvJtPUlZvfdqdb3OhICCD+wrEB26rW149NtKZk58PJgEWl5Cz1d4 +2anTr/ycck4H/32Byq+32u5+8zPPEZvr8LnIIvLYFtaEDQSK9qL8S/G2KfVgDeOZWTu eFgjrB/pvNtlEBMcjvbTIq8tUuWlzcJYXRdbIbokIUb5JjNKkAFo7rBpTe+IMszKZHs6 daOsLNj44VBIbXd37qsZscDx/3dEuVBEjaz9dMa2nb4h7zz0z6iLZZNDQWUsmXmFhJaC imYGya69NgEptWjHaVtfLCkXTqn9YDI7g5RW2S/pj1RXqLC+39ozqcOypWTB1OLTNduB VM6Q== X-Gm-Message-State: AOJu0YyvUdFxptIzc6mvIH9WJ1VgwDS3dCglpX6oqSyQAgWxnK94pJfo 8NidG0Jf9NUdavjL05g2QMR5pjea7cavvlVnF7karAq3u3POjYUd3kRlprQ7 X-Received: by 2002:a05:6a00:408d:b0:6d9:9613:cb9e with SMTP id bw13-20020a056a00408d00b006d99613cb9emr2190361pfb.29.1705760874908; Sat, 20 Jan 2024 06:27:54 -0800 (PST) Received: from gnu-cfl-3.localdomain ([172.56.168.9]) by smtp.gmail.com with ESMTPSA id k4-20020aa78204000000b006d0a29ad0aasm6841491pfi.5.2024.01.20.06.27.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jan 2024 06:27:54 -0800 (PST) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id EDCBB7403AC; Sat, 20 Jan 2024 06:27:52 -0800 (PST) From: "H.J. Lu" To: gcc-patches@gcc.gnu.org Cc: ubizjak@gmail.com, hongtao.liu@intel.com, jh@suse.cz Subject: [PATCH 2/2] x86: Don't save callee-saved registers in noreturn functions Date: Sat, 20 Jan 2024 06:27:52 -0800 Message-ID: <20240120142752.1387725-3-hjl.tools@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240120142752.1387725-1-hjl.tools@gmail.com> References: <20240120142752.1387725-1-hjl.tools@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-3024.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788620030777895559 X-GMAIL-MSGID: 1788620030777895559 There is no need to save callee-saved registers in noreturn functions if they don't throw nor support exceptions. We can treat them the same as functions with no_callee_saved_registers attribute. Adjust stack-check-17.c for noreturn function which no longer saves any registers. With this change, __libc_start_main in glibc 2.39, which is a noreturn function, is changed from __libc_start_main: endbr64 push %r15 push %r14 mov %rcx,%r14 push %r13 push %r12 push %rbp mov %esi,%ebp push %rbx mov %rdx,%rbx sub $0x28,%rsp mov %rdi,(%rsp) mov %fs:0x28,%rax mov %rax,0x18(%rsp) xor %eax,%eax test %r9,%r9 to __libc_start_main: endbr64 sub $0x28,%rsp mov %esi,%ebp mov %rdx,%rbx mov %rcx,%r14 mov %rdi,(%rsp) mov %fs:0x28,%rax mov %rax,0x18(%rsp) xor %eax,%eax test %r9,%r9 In Linux kernel 6.7.0 on x86-64, do_exit is changed from do_exit: endbr64 call push %r15 push %r14 push %r13 push %r12 mov %rdi,%r12 push %rbp push %rbx mov %gs:0x0,%rbx sub $0x28,%rsp mov %gs:0x28,%rax mov %rax,0x20(%rsp) xor %eax,%eax call *0x0(%rip) # test $0x2,%ah je to do_exit: endbr64 call sub $0x28,%rsp mov %rdi,%r12 mov %gs:0x28,%rax mov %rax,0x20(%rsp) xor %eax,%eax mov %gs:0x0,%rbx call *0x0(%rip) # test $0x2,%ah je I compared GCC master branch bootstrap and test times on a slow machine with 6.6 Linux kernels compiled with the original GCC 13 and the GCC 13 with the backported patch. The performance data isn't precise since the measurements were done on different days with different GCC sources under different 6.6 kernel versions. GCC master branch build time in seconds: before after improvement 30043.75user 30013.16user 0% 1274.85system 1243.72system 2.4% GCC master branch test time in seconds (new tests added): before after improvement 216035.90user 216547.51user 0 27365.51system 26658.54system 2.6% gcc/ PR target/38534 * config/i386/i386-options.cc (ix86_set_func_type): Don't save and restore callee saved registers for a noreturn function with nothrow or compiled with -fno-exceptions. gcc/testsuite/ PR target/38534 * gcc.target/i386/pr38534-1.c: New file. * gcc.target/i386/pr38534-2.c: Likewise. * gcc.target/i386/pr38534-3.c: Likewise. * gcc.target/i386/pr38534-4.c: Likewise. * gcc.target/i386/stack-check-17.c: Updated. --- gcc/config/i386/i386-options.cc | 16 ++++++++++-- gcc/testsuite/gcc.target/i386/pr38534-1.c | 26 +++++++++++++++++++ gcc/testsuite/gcc.target/i386/pr38534-2.c | 18 +++++++++++++ gcc/testsuite/gcc.target/i386/pr38534-3.c | 19 ++++++++++++++ gcc/testsuite/gcc.target/i386/pr38534-4.c | 18 +++++++++++++ .../gcc.target/i386/stack-check-17.c | 19 +++++--------- 6 files changed, 102 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/gcc.target/i386/pr38534-1.c create mode 100644 gcc/testsuite/gcc.target/i386/pr38534-2.c create mode 100644 gcc/testsuite/gcc.target/i386/pr38534-3.c create mode 100644 gcc/testsuite/gcc.target/i386/pr38534-4.c diff --git a/gcc/config/i386/i386-options.cc b/gcc/config/i386/i386-options.cc index 0cdea30599e..f965568947c 100644 --- a/gcc/config/i386/i386-options.cc +++ b/gcc/config/i386/i386-options.cc @@ -3371,9 +3371,21 @@ ix86_simd_clone_adjust (struct cgraph_node *node) static void ix86_set_func_type (tree fndecl) { + /* No need to save and restore callee-saved registers for a noreturn + function with nothrow or compiled with -fno-exceptions. + + NB: Don't use TREE_THIS_VOLATILE to check if this is a noreturn + function. The local-pure-const pass turns an interrupt function + into a noreturn function by setting TREE_THIS_VOLATILE. Normally + the local-pure-const pass is run after ix86_set_func_type is called. + When the local-pure-const pass is enabled for LTO, the interrupt + function is marked as noreturn in the IR output, which leads the + incompatible attribute error in LTO1. */ bool has_no_callee_saved_registers - = lookup_attribute ("no_callee_saved_registers", - TYPE_ATTRIBUTES (TREE_TYPE (fndecl))); + = (((TREE_NOTHROW (fndecl) || !flag_exceptions) + && lookup_attribute ("noreturn", DECL_ATTRIBUTES (fndecl))) + || lookup_attribute ("no_callee_saved_registers", + TYPE_ATTRIBUTES (TREE_TYPE (fndecl)))); if (cfun->machine->func_type == TYPE_UNKNOWN) { diff --git a/gcc/testsuite/gcc.target/i386/pr38534-1.c b/gcc/testsuite/gcc.target/i386/pr38534-1.c new file mode 100644 index 00000000000..9297959e759 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr38534-1.c @@ -0,0 +1,26 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mtune-ctrl=^prologue_using_move,^epilogue_using_move" } */ + +#define ARRAY_SIZE 256 + +extern int array[ARRAY_SIZE][ARRAY_SIZE][ARRAY_SIZE]; +extern int value (int, int, int) +#ifndef __x86_64__ +__attribute__ ((regparm(3))) +#endif +; + +void +__attribute__((noreturn)) +no_return_to_caller (void) +{ + unsigned i, j, k; + for (i = ARRAY_SIZE; i > 0; --i) + for (j = ARRAY_SIZE; j > 0; --j) + for (k = ARRAY_SIZE; k > 0; --k) + array[i - 1][j - 1][k - 1] = value (i, j, k); + while (1); +} + +/* { dg-final { scan-assembler-not "push" } } */ +/* { dg-final { scan-assembler-not "pop" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr38534-2.c b/gcc/testsuite/gcc.target/i386/pr38534-2.c new file mode 100644 index 00000000000..1fb01363273 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr38534-2.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mtune-ctrl=^prologue_using_move,^epilogue_using_move" } */ + +extern void bar (void) __attribute__ ((no_callee_saved_registers)); +extern void fn (void) __attribute__ ((noreturn)); + +__attribute__ ((noreturn)) +void +foo (void) +{ + bar (); + fn (); +} + +/* { dg-final { scan-assembler-not "push" } } */ +/* { dg-final { scan-assembler-not "pop" } } */ +/* { dg-final { scan-assembler-not "jmp\[\\t \]+_?bar" } } */ +/* { dg-final { scan-assembler "call\[\\t \]+_?bar" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr38534-3.c b/gcc/testsuite/gcc.target/i386/pr38534-3.c new file mode 100644 index 00000000000..87fc35f3fe9 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr38534-3.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mtune-ctrl=^prologue_using_move,^epilogue_using_move" } */ + +typedef void (*fn_t) (void) __attribute__ ((no_callee_saved_registers)); +extern fn_t bar; +extern void fn (void) __attribute__ ((noreturn)); + +__attribute__ ((noreturn)) +void +foo (void) +{ + bar (); + fn (); +} + +/* { dg-final { scan-assembler-not "push" } } */ +/* { dg-final { scan-assembler-not "pop" } } */ +/* { dg-final { scan-assembler-not "jmp" } } */ +/* { dg-final { scan-assembler "call\[\\t \]+" } } */ diff --git a/gcc/testsuite/gcc.target/i386/pr38534-4.c b/gcc/testsuite/gcc.target/i386/pr38534-4.c new file mode 100644 index 00000000000..561ebeef194 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr38534-4.c @@ -0,0 +1,18 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mtune-ctrl=^prologue_using_move,^epilogue_using_move" } */ + +typedef void (*fn_t) (void) __attribute__ ((no_callee_saved_registers)); +extern void fn (void) __attribute__ ((noreturn)); + +__attribute__ ((noreturn)) +void +foo (fn_t bar) +{ + bar (); + fn (); +} + +/* { dg-final { scan-assembler-not "push" } } */ +/* { dg-final { scan-assembler-not "pop" } } */ +/* { dg-final { scan-assembler-not "jmp" } } */ +/* { dg-final { scan-assembler "call\[\\t \]+" } } */ diff --git a/gcc/testsuite/gcc.target/i386/stack-check-17.c b/gcc/testsuite/gcc.target/i386/stack-check-17.c index b3e41cb3d25..061484e1319 100644 --- a/gcc/testsuite/gcc.target/i386/stack-check-17.c +++ b/gcc/testsuite/gcc.target/i386/stack-check-17.c @@ -23,19 +23,14 @@ f3 (void) /* Verify no explicit probes. */ /* { dg-final { scan-assembler-not "or\[ql\]" } } */ -/* We also want to verify we did not use a push/pop sequence - to probe *sp as the callee register saves are sufficient - to probe *sp. - - y0/y1 are live across the call and thus must be allocated +/* y0/y1 are live across the call and thus must be allocated into either a stack slot or callee saved register. The former would be rather dumb. So assume it does not happen. - So search for two/four pushes for the callee register saves/argument pushes - (plus one for the PIC register if needed on ia32) and no pops (since the - function has no reachable epilogue). */ -/* { dg-final { scan-assembler-times "push\[ql\]" 2 { target { ! ia32 } } } } */ -/* { dg-final { scan-assembler-times "push\[ql\]" 4 { target { ia32 && nonpic } } } } */ -/* { dg-final { scan-assembler-times "push\[ql\]" 5 { target { ia32 && { ! nonpic } } } } } */ -/* { dg-final { scan-assembler-not "pop" } } */ + So search for a push/pop sequence for stack probe and 2 argument + pushes on ia32. There is no need to save and restore the PIC + register on ia32 for a noreturn function. */ +/* { dg-final { scan-assembler-times "push\[ql\]" 1 { target { ! ia32 } } } } */ +/* { dg-final { scan-assembler-times "push\[ql\]" 3 { target ia32 } } } */ +/* { dg-final { scan-assembler-times "pop" 1 } } */