LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 48984 - Failure to optimize the value of low bits of result of left shift of constant where those bits are already unset
Summary: Failure to optimize the value of low bits of result of left shift of constant...
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Scalar Optimizations (show other bugs)
Version: trunk
Hardware: PC Linux
: P enhancement
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-02-01 00:30 PST by Gabriel Ravier
Modified: 2021-02-10 05:40 PST (History)
4 users (show)

See Also:
Fixed By Commit(s): 0be0a1237cb9


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gabriel Ravier 2021-02-01 00:30:31 PST
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
Comment 1 Gabriel Ravier 2021-02-01 00:37:24 PST
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).
Comment 2 Gabriel Ravier 2021-02-01 00:37:55 PST
PPS: I messed up and typed `a == b` where I meant `a == 3` in the previous message.
Comment 4 Sanjay Patel 2021-02-03 09:55:49 PST
This would catch the example in the description (and similar):
https://reviews.llvm.org/D95959
Comment 5 Sanjay Patel 2021-02-10 05:40:12 PST
Should be fixed after:
https://reviews.llvm.org/rG0be0a1237cb9