public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Stefan Kanthak" <stefan.kanthak@nexgo.de>
To: <gcc@gnu.org>
Subject: GCC plays "Shell Game", but looses track of the shell covering the nought
Date: Sat, 27 May 2023 18:23:12 +0200	[thread overview]
Message-ID: <501052832B6846AA8CF3F15C5C6CDA02@H270> (raw)

--- demo.c ---
int ispowerof2(unsigned long long argument) {
    return (argument != 0) && ((argument & argument - 1) == 0);
}
--- EOF ---

GCC 12.2    gcc -m32 -O3

https://gcc.godbolt.org/z/YWP4zb8jd
ispowerof2(unsigned long long):
        push    edi                        # three registers clobbered
        push    esi                        #  without need, and three
        push    ebx                        #   superfluous memory writes
        mov     edi, DWORD PTR [esp+20]    ->    mov    ecx, [esp+4]
        mov     esi, DWORD PTR [esp+16]    ->    mov    edx, [esp+8]
        mov     eax, edi                   ->    mov    eax, ecx
        or      eax, esi                   ->    or     eax, edx
        je      .L5                        ->    jz     .L0
        mov     eax, esi                   # superfluous
        mov     edx, edi                   # superfluous
        add     eax, -1                    ->    add    ecx, -1
        adc     edx, -1                    ->    adc    edx, -1
        mov     ecx, eax                   # shell game
        mov     eax, esi                   # shell game
        mov     ebx, edx                   # shell game
        mov     edx, edi                   # shell game
        and     eax, ecx                   ->    and    ecx, [esp+4]
        xor     ecx, ecx                   ->    xor    eax, eax
        and     edx, ebx                   ->    and    edx, [esp+8]
        pop     ebx                        # superfluous
        pop     esi                        # superfluous
        or      eax, edx                   ->    or     ecx, edx
        pop     edi                        # superfluous
        sete    cl                         ->    setz   al
        mov     eax, ecx                   # shell game
        ret                        .L0:    ->    ret
.L5:                                       # eax is 0 here!
        xor     ecx, ecx                   # superfluous
        pop     ebx                        # superfluous
        pop     esi                        # superfluous
        mov     eax, ecx                   # superfluous
        pop     edi                        # superfluous
        ret                                # superfluous

OUCH: 19 (in words: NINETEEN) superfluous instructions out of 32, despite -O3,
      3 registers clobbered without need and reason, resulting in
      3 superfluous memory writes

GCC 12.3 generates the same BRAINDEAD code!

GCC 13.3    gcc -m32 -O3

https://gcc.godbolt.org/z/Ge7q4Tv9M
ispowerof2(unsigned long long):
        push    esi                        # two registers clobbered without need,
        push    ebx                        #  two superfluous memory writes
        mov     ecx, DWORD PTR [esp+12]    ->    mov    ecx, [esp+4]
        mov     ebx, DWORD PTR [esp+16]    ->    mov    edx, [esp+8]
        mov     eax, ecx                   ->    mov    eax, ecx
        or      eax, ebx                   ->    or     eax, edx
        je      .L5                        ->    jz    .L0
                                                 xor    eax, eax
        mov     eax, ecx                   # superfluous
        mov     edx, ebx                   # superfluous
        add     eax, -1                    ->    add    ecx, -1
        adc     edx, -1                    ->    adc    edx, -1
        and     eax, ecx                   ->    and    ecx, [esp+4]
        and     edx, ebx                   ->    and    edx, [esp+8]
        pop     ebx                        # superfluous
        or      eax, edx                   ->    or     ecx, edx
        sete    al                         ->    setz   al
        movzx   eax, al                    # superfluous: see xor added above
        mov     esi, eax                   # shell game
        mov     eax, esi                   # shell game
        pop     esi                        # superfluous
        ret                       .L0:     ->    ret
.L5:                                       # eax is 0 here!
        xor     esi, esi                   # superfluous
        pop     ebx                        # superfluous
        mov     eax, esi                   # superfluous
        pop     esi                        # superfluous
        ret                                # superfluous

OUCH: 13 (in words: THIRTEEN) superfluous instructions out of 26, despite -O3,
      2 registers clobbered without need and reason, resulting in
      2 superfluous memory writes

It's e REAL shame how bad GCC's code generator is!

Stefan

             reply	other threads:[~2023-05-27 16:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-27 16:23 Stefan Kanthak [this message]
     [not found] ` <20230527113728.26edde9fb121c8c310413fbb@killthe.net>
2023-05-27 17:00   ` Stefan Kanthak

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=501052832B6846AA8CF3F15C5C6CDA02@H270 \
    --to=stefan.kanthak@nexgo.de \
    --cc=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).