-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[X86] Recognize Clang's <=>
code pattern
#60012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There are a few other formulations here that don't canonicalize to the same thing; maybe that should be looked at too: https://godbolt.org/z/7fcrczYeo |
(A s>> (BW - 1)) + (zext (A s> 0)) --> (A s>> (BW - 1)) | (zext (A != 0)) https://alive2.llvm.org/ce/z/V-nM8N This is not the form that we currently match as m_Signum(), but I'm not sure if one is better than the other, so there's a follow-up patch needed either way. For this patch, it should be better for analysis to use a not-null test and bitwise logic rather than >0 with add. Codegen doesn't seem significantly different on any targets that I looked at. Also note that none of these variants is shown in issue #60012 - those generally include at least one 'select', so that's likely where these patterns will end up.
@scottmcm Is this issue addressed? x86-64 assembly looks good:
|
Thanks, with LLVM 20 we saw this get picked up and do good things in Rust even before we did anything, and now with rust-lang/rust#133984 So that directly fixed this x86 issue, and we've also seen better optimization results around |
Today, this code https://cpp.godbolt.org/z/e8q15zzK1
gives this select chain
which compiles to some pretty verbose assembly:
There are other ways of doing this that are definitely better for size, and plausibly better for speed too https://graphics.stanford.edu/~seander/bithacks.html#CopyIntegerSign
For example, it could be
(I've filed this as x86-specific because the
select
s in LLVM-IR might be better for optimizations to use, as more obvious than the subtraction. The specific pattern that Clang emits for<=>
is most important, but it would be nice if it also recognized -- or canonicalized in IR -- the bunch of equivalent select chains.)The text was updated successfully, but these errors were encountered: