From: "Stefan Kanthak" <stefan.kanthak@nexgo.de>
To: <gcc@gnu.org>
Subject: Another epic optimiser failure
Date: Sat, 27 May 2023 23:04:11 +0200 [thread overview]
Message-ID: <2A5397441B7D46C7928856D7AAF5836B@H270> (raw)
--- .c ---
int ispowerof2(unsigned long long argument) {
return __builtin_popcountll(argument) == 1;
}
--- EOF ---
GCC 13.3 gcc -m32 -march=alderlake -O3
gcc -m32 -march=sapphirerapids -O3
gcc -m32 -mpopcnt -mtune=sapphirerapids -O3
https://gcc.godbolt.org/z/cToYrrYPq
ispowerof2(unsigned long long):
xor eax, eax # superfluous
xor edx, edx # superfluous
popcnt eax, [esp+4]
popcnt edx, [esp+8]
add eax, edx
cmp eax, 1 -> dec eax
sete al
movzx eax, al # superfluous
ret
9 instructions in 28 bytes # 6 instructions in 20 bytes
OUCH: popcnt writes the WHOLE result register, there is ABSOLUTELY
no need to clear it beforehand nor to clear the higher 24 bits
afterwards!
JFTR: before GCC zealots write nonsense: see -march= or -mtune=
GCC 13.3 gcc -mpopcnt -mtune=barcelona -O3
https://gcc.godbolt.org/z/3Ks8vh7a6
ispowerof2(unsigned long long):
popcnt rdi, rdi -> popcnt rax, rdi
xor eax, eax # superfluous!
dec edi -> dec eax
sete al -> setz al
ret
GCC 13.3 gcc -m32 -mpopcnt -mtune=barcelona -O3
https://gcc.godbolt.org/z/s5s5KTGnv
ispowerof2(unsigned long long):
popcnt eax, [esp+4]
popcnt edx, [esp+8]
add eax, edx
dec eax
sete al
movzx eax, al # superfluous!
ret
Will GCC eventually generate properly optimised code instead of bloat?
Stefan
next reply other threads:[~2023-05-27 21:11 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-27 21:04 Stefan Kanthak [this message]
2023-05-27 21:20 ` Jakub Jelinek
2023-05-27 21:28 ` Stefan Kanthak
2023-05-27 21:42 ` Andrew Pinski
2023-05-27 22:00 ` Stefan Kanthak
2023-05-27 22:46 ` Jonathan Wakely
2023-05-28 6:28 ` Nicholas Vinson
2023-05-28 7:50 Julian Waters
2023-05-29 19:01 ` Dave Blanchard
2023-05-29 23:44 ` Nicholas Vinson
2023-05-30 4:04 ` Julian Waters
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=2A5397441B7D46C7928856D7AAF5836B@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).