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 the value of low bits of result of left shift of constant where those bits are already unset #48328

Closed
GabrielRavier opened this issue Feb 1, 2021 · 5 comments
Labels
bugzilla Issues migrated from bugzilla

Comments

@GabrielRavier
Copy link
Contributor

Bugzilla Link 48984
Resolution FIXED
Resolved on Feb 10, 2021 05:40
Version trunk
OS Linux
CC @RKSimon,@nikic,@rotateright
Fixed by commit(s) 0be0a12

Extended Description

int f(int a)
{
return ((2 << a) & 1) == 0;
}

This can be optimized to return 1;. This transformation is done by GCC, but by LLVM.

Godbolt comparison here: https://godbolt.org/z/dTfsbx
alive2 check here: https://alive2.llvm.org/ce/z/U3DMgJ

@GabrielRavier
Copy link
Contributor Author

PS: This can also be optimized in cases where the low bits aren't necessarily unset, e.g.

int f(int a)
{
return ((2 << a) & 16) != 0;
}

Can be optimized to return a == b; (done by GCC, not by LLVM).

@GabrielRavier
Copy link
Contributor Author

PPS: I messed up and typed a == b where I meant a == 3 in the previous message.

@nikic
Copy link
Contributor

nikic commented Feb 1, 2021

This is presumably caused by the early exit at

if (MaxShiftAmtIsOutOfRange) {
.

@rotateright
Copy link
Contributor

This would catch the example in the description (and similar):
https://reviews.llvm.org/D95959

@rotateright
Copy link
Contributor

Should be fixed after:
https://reviews.llvm.org/rG0be0a1237cb9

@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

3 participants