-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
JIT: Optimize bit-wise AND with a constant mask in combination with a left shift in a compare #111979
base: main
Are you sure you want to change the base?
JIT: Optimize bit-wise AND with a constant mask in combination with a left shift in a compare #111979
Conversation
…a left shift in a compare This optmizes the generated code when having a pattern like '(SomeConstant & (1 << value)) != 0' which was previously only optimized for '(variable & (1 << value)) != 0'. Fix dotnet#111554
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
@dotnet-policy-service agree |
GenTree* lshRight = cmp->gtGetOp2(); | ||
GenTree* lshLeft = cmp->gtGetOp1(); | ||
|
||
if (lsh->OperIs(GT_LSH) && varTypeIsIntOrI(lsh->TypeGet()) && lsh->gtGetOp1()->IsIntegralConst(1)) | ||
if (lshRight->OperIs(GT_LSH) && varTypeIsIntOrI(lshRight->TypeGet()) && lshRight->gtGetOp1()->IsIntegralConst(1)) | ||
{ | ||
cmp->SetOper(cmp->OperIs(GT_TEST_EQ) ? GT_BITTEST_EQ : GT_BITTEST_NE); | ||
cmp->AsOp()->gtOp2 = lsh->gtGetOp2(); | ||
cmp->AsOp()->gtOp2 = lshRight->gtGetOp2(); | ||
cmp->gtGetOp2()->ClearContained(); | ||
|
||
BlockRange().Remove(lsh->gtGetOp1()); | ||
BlockRange().Remove(lsh); | ||
BlockRange().Remove(lshRight->gtGetOp1()); | ||
BlockRange().Remove(lshRight); | ||
|
||
return cmp->gtNext; | ||
} | ||
else if (lshLeft->OperIs(GT_LSH) && varTypeIsIntOrI(lshLeft->TypeGet()) && lshLeft->gtGetOp1()->IsIntegralConst(1)) | ||
{ | ||
cmp->SetOper(cmp->OperIs(GT_TEST_EQ) ? GT_BITTEST_EQ : GT_BITTEST_NE); | ||
cmp->AsOp()->gtOp1 = cmp->AsOp()->gtOp2; | ||
cmp->AsOp()->gtOp2 = lshLeft->gtGetOp2(); | ||
cmp->gtGetOp2()->ClearContained(); | ||
|
||
BlockRange().Remove(lshLeft->gtGetOp1()); | ||
BlockRange().Remove(lshLeft); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the duplication be avoided?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could minimize it using a helper method. Can I just put the method in lower.h
with the body in the header like ReplaceWithLclVar
or differently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest maybe just doing something like:
GenTree** lsh = &cmp->AsOp()->gtOp1;
GenTree** op = &cmp->AsOp()->gtOp2;
if (!(*lsh)->OperIs(GT_LSH))
std::swap(lsh, op);
if ((*lsh)->OperIs(GT_LSH) && varTypeIsIntOrI(*lsh) && (*lsh)->gtGetOp1()->IsIntegralConst(1))
{
cmp->SetOper(cmp->OperIs(GT_TEST_EQ) ? GT_BITTEST_EQ : GT_BITTEST_NE);
*lsh = (*lsh)->gtGetOp2();
...
}
This resolves #111554.
C# code (example code from the issue):
x64 (before):
x64 (after):