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

Add optimization "X & 1 == 1" to "X & 1" (#61412) #62818

Merged
merged 13 commits into from
Mar 21, 2022
19 changes: 19 additions & 0 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2925,6 +2925,25 @@ GenTree* Lowering::OptimizeConstCompare(GenTree* cmp)

if (op2Value != 0)
{
// Optimizes (X & 1) == 1 to (X & 1)
// The compiler requires jumps to have relop operands, so we do not fold that case.
LIR::Use cmpUse;
if ((op2Value == 1) && cmp->OperIs(GT_EQ))
{
if (andOp2->IsIntegralConst(1) && (genActualType(op1) == cmp->TypeGet()) &&
BlockRange().TryGetUse(cmp, &cmpUse) && !cmpUse.User()->OperIs(GT_JTRUE))
{
GenTree* next = cmp->gtNext;

cmpUse.ReplaceWith(op1);

BlockRange().Remove(cmp->gtGetOp2());
BlockRange().Remove(cmp);

return next;
}
}

//
// If we don't have a 0 compare we can get one by transforming ((x AND mask) EQ|NE mask)
// into ((x AND mask) NE|EQ 0) when mask is a single bit.
Expand Down