public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/110008] New: early returns from functions result in suboptimal code
@ 2023-05-27 18:47 lh_mouse at 126 dot com
  2023-05-27 18:55 ` [Bug rtl-optimization/110008] shink wrapping could be improved pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: lh_mouse at 126 dot com @ 2023-05-27 18:47 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug rtl-optimization/110008] shink wrapping could be improved
  2023-05-27 18:47 [Bug rtl-optimization/110008] New: early returns from functions result in suboptimal code lh_mouse at 126 dot com
@ 2023-05-27 18:55 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-27 18:55 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
         Depends on|                            |106210
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2023-05-27
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. For aarch64, GCC used to do shink wrapping in GCC 5 for this
function but no longer does in GCC 6.

Shink wrapping is one of the fragile parts of GCC really.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106210
[Bug 106210] [10/11/12/13/14 Regression] missing shrink wrap for simple case
since r9-3594-g8d2d39587d941a40

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-05-27 18:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-27 18:47 [Bug rtl-optimization/110008] New: early returns from functions result in suboptimal code lh_mouse at 126 dot com
2023-05-27 18:55 ` [Bug rtl-optimization/110008] shink wrapping could be improved pinskia at gcc dot gnu.org

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).