For --- int f(int);
int advance(int dz) { if (dz > 0) return (dz + dz) * dz; else return dz * f(dz); } --- Before r15-1619-g3b9b8d6cfdf593 advance(int): push rbx mov ebx, edi test edi, edi jle .L2 imul ebx, edi lea eax, [rbx+rbx] pop rbx ret .L2: call f(int) imul eax, ebx pop rbx ret After advance(int): test edi, edi jle .L2 imul edi, edi lea eax, [rdi+rdi] ret .L2: sub rsp, 24 mov DWORD PTR [rsp+12], edi call f(int) imul eax, DWORD PTR [rsp+12] add rsp, 24 ret There's no call in if branch, it's not optimal to push rbx at the entry of the function, it can be sinked to else branch. When "jle .L2" is not taken, it can save one push instruction. Update pr111673.c to verify that this optimization isn't turned off. PR rtl-optimization/111673 * gcc.target/i386/pr111673.c: Verify that PUSH/POP can be skipped. -- H.J.
From 6606ec5573e724295bdceb572ddc2813f021709f Mon Sep 17 00:00:00 2001 From: "H.J. Lu" <hjl.to...@gmail.com> Date: Fri, 7 Feb 2025 13:49:30 +0800 Subject: [PATCH] x86: Verify that PUSH/POP can be skipped For --- int f(int); int advance(int dz) { if (dz > 0) return (dz + dz) * dz; else return dz * f(dz); } --- Before r15-1619-g3b9b8d6cfdf593 advance(int): push rbx mov ebx, edi test edi, edi jle .L2 imul ebx, edi lea eax, [rbx+rbx] pop rbx ret .L2: call f(int) imul eax, ebx pop rbx ret After advance(int): test edi, edi jle .L2 imul edi, edi lea eax, [rdi+rdi] ret .L2: sub rsp, 24 mov DWORD PTR [rsp+12], edi call f(int) imul eax, DWORD PTR [rsp+12] add rsp, 24 ret There's no call in if branch, it's not optimal to push rbx at the entry of the function, it can be sinked to else branch. When "jle .L2" is not taken, it can save one push instruction. Update pr111673.c to verify that this optimization isn't turned off. PR rtl-optimization/111673 * gcc.target/i386/pr111673.c: Verify that PUSH/POP can be skipped. Signed-off-by: H.J. Lu <hjl.to...@gmail.com> --- gcc/testsuite/gcc.target/i386/pr111673.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gcc/testsuite/gcc.target/i386/pr111673.c b/gcc/testsuite/gcc.target/i386/pr111673.c index 8d8a5a764f0..b9ceacf7651 100644 --- a/gcc/testsuite/gcc.target/i386/pr111673.c +++ b/gcc/testsuite/gcc.target/i386/pr111673.c @@ -1,5 +1,19 @@ /* { dg-do compile { target { ! ia32 } } } */ /* { dg-options "-O2 -fdump-rtl-pro_and_epilogue" } */ +/* Keep labels and directives ('.cfi_startproc', '.cfi_endproc'). */ +/* { dg-final { check-function-bodies "**" "" "" { target "*-*-*" } {^\t?\.} } } */ + +/* +**advance: +**.LFB0: +** .cfi_startproc +** testl %edi, %edi +** jle .L2 +** imull %edi, %edi +** leal \(%rdi,%rdi\), %eax +** ret +**... +*/ /* Verify there is an early return without the prolog and shrink-wrap the function. */ -- 2.48.1