public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "lh_mouse at 126 dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/110008] New: early returns from functions result in suboptimal code
Date: Sat, 27 May 2023 18:47:04 +0000	[thread overview]
Message-ID: <bug-110008-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110008

            Bug ID: 110008
           Summary: early returns from functions result in suboptimal code
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---

https://gcc.godbolt.org/z/94Wf3Worq
```
int complex_one(int, int);

int
test(int a, int b, int c)
  {
    if(__builtin_expect(a, 0) == 0)
      return 0;

    int r = complex_one(a, b);
    r += complex_one(r, c);
    return r + a + b;
  }
```

GCC:
```
test:
        push    rdi
        push    rsi
        push    rbx
        sub     rsp, 32
        mov     ebx, ecx
        mov     esi, edx
        test    ecx, ecx
        jne     .L7
        mov     eax, ebx
        add     rsp, 32
        pop     rbx
        pop     rsi
        pop     rdi
        ret
.L7:
        mov     DWORD PTR 80[rsp], r8d
        call    complex_one
        mov     edx, DWORD PTR 80[rsp]
        mov     ecx, eax
        mov     edi, eax
        call    complex_one
        add     edi, eax
        add     ebx, edi
        add     ebx, esi
        mov     eax, ebx
        add     rsp, 32
        pop     rbx
        pop     rsi
        pop     rdi
        ret
```

Clang:
```
test:                                   # @test
        xor     eax, eax
        test    edi, edi
        jne     .LBB0_1
        ret
.LBB0_1:
        push    rbp
        push    r15
        push    r14
        push    rbx
        push    rax
        mov     r14d, edx
        mov     ebx, esi
        mov     ebp, edi
        call    complex_one@PLT
        mov     r15d, eax
        mov     edi, eax
        mov     esi, r14d
        call    complex_one@PLT
        add     ebx, ebp
        add     ebx, r15d
        add     ebx, eax
        mov     eax, ebx
        add     rsp, 8
        pop     rbx
        pop     r14
        pop     r15
        pop     rbp
        ret
```

There are two issues in this code: The first one is that GCC uses apparently
more space for temporary variables than Clang. The other is that when `a`
equals zero, Clang skips the normal function prologue which pushes a lot of
registers onto the stack, but GCC performs the check after it, in which case
both the prologue and epilogue get executed for nothing.

             reply	other threads:[~2023-05-27 18:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-27 18:47 lh_mouse at 126 dot com [this message]
2023-05-27 18:55 ` [Bug rtl-optimization/110008] shink wrapping could be improved pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110008-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).