From: Andrew Cooper <andrew.cooper3@citrix.com>
To: gcc@gcc.gnu.org
Subject: Builtin for consulting value analysis (better ffs() code gen)
Date: Thu, 14 Mar 2024 00:03:28 +0000 [thread overview]
Message-ID: <06d7af49-c4a9-43d5-a18f-266439c7f82d@citrix.com> (raw)
Hello,
I've come across an issue that I would have thought there would be a
builtin for, but perhaps that's just wishful thinking. I'd like to be
able to write something like this:
if (__builtin_expr_is_true(x > 0))
... // one thing
else
... // something else
This stems from trying to clean up the mess of bit operation helpers in Xen.
On x86, __builtin_ffs() doesn't have great code generation. This is a
consequence of the BSF instruction having miserable semantics, and the
builtin emits code with a branch or cmov to compensate for undefined
case of passing 0 in.
On x86_64 at least, Intel and AMD have made enough guarantees in writing
to allow a condition-less form:
mov $-1, %dst
bsf %src, %dst
add $1, %dst
which is good, but not great. It is common to have an __ffs() variant
which states that a src of 0 is undefined, and while this makes a
reasonable improvement to the code generation within loops, it's still
not great to rely on the programmer to get this right.
A common pattern to find is something like:
while (x) {
int b = ffs(x);
... // do something with x and b
where range analysis can know that x is nonzero. Indeed, the builtin
manages to spot this, and emits a condition-less form too.
However, doing this for a local implementation of ffs() doesn't work. With:
unsigned int my_ffs(unsigned int x)
{
int res;
if (x) {
asm ("bsf ..." : "=r" (res) : "rm" (x));
} else {
res = -1;
asm ("bsf ..." : "+r" (res) : "rm" (x));
}
return res + 1;
}
the while() example above really does get generated with ideal form.
However, in general code where the value of x is unknown, the entire
if/else chain is emitted, which is strictly worse than just emitting the
else case which is the safe catch-all.
I suppose that what I'm looking for is something a little like
__builtin_constant_p() which can either be used in a straight if(), or
in a __builtin_choose_expr().
Anyway - is there a way of doing this that I've managed to overlook?
~Andrew
next reply other threads:[~2024-03-14 0:03 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-14 0:03 Andrew Cooper [this message]
2024-03-14 6:04 ` Alexander Monakov
2024-03-14 11:30 ` Andrew Cooper
2024-03-14 12:03 ` Andreas Schwab
2024-03-14 15:33 ` Andrew Cooper
2024-03-21 8:15 ` LIU Hao
2024-03-14 11:02 ` Florian Weimer
2024-03-14 11:52 ` Andrew Cooper
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=06d7af49-c4a9-43d5-a18f-266439c7f82d@citrix.com \
--to=andrew.cooper3@citrix.com \
--cc=gcc@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).