Skip to content
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

Failure to optimize popcount of a single bit #47952

Closed
GabrielRavier opened this issue Dec 27, 2020 · 3 comments
Closed

Failure to optimize popcount of a single bit #47952

GabrielRavier opened this issue Dec 27, 2020 · 3 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla

Comments

@GabrielRavier
Copy link
Contributor

Bugzilla Link 48608
Resolution FIXED
Resolved on Dec 28, 2020 13:38
Version trunk
OS Linux
CC @RKSimon,@rotateright
Fixed by commit(s) 236c452

Extended Description

int f(unsigned int a)
{
return __builtin_popcount(a >> (CHAR_BIT * sizeof(a) - 1));
}

This can be optimized to return (a >> (__CHAR_BIT__ * sizeof(a) - 1));. This transformation is done by GCC, but not by LLVM.

See also comparison here : https://godbolt.org/z/1Kz5qf

@GabrielRavier
Copy link
Contributor Author

assigned to @rotateright

@rotateright
Copy link
Contributor

We miss both of these:

define i32 @​pop_signbit(i32 %x) {
%b = lshr i32 %x, 31
%r = tail call i32 @​llvm.ctpop.i32(i32 %b), !range !​0
ret i32 %r
}

define i32 @​pop_lowbit(i32 %x) {
%b = and i32 %x, 1
%r = tail call i32 @​llvm.ctpop.i32(i32 %b), !range !​0
ret i32 %r
}

We might be able to rely on the range metadata. I'll take a look.

@rotateright
Copy link
Contributor

Metadata doesn't provide what we need. I used a ValueTracking call instead.
Should be fixed with:
https://reviews.llvm.org/rG236c4524a7cd

It looks like gcc still does more, so please open a new bug if more popcount optimizations are expected/needed.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla
Projects
None yet
Development

No branches or pull requests

2 participants