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

JIT: Optimize bit-wise AND with a constant mask in combination with a left shift in a compare #111979

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

varelen
Copy link

@varelen varelen commented Jan 29, 2025

This resolves #111554.

C# code (example code from the issue):

[MethodImpl(MethodImplOptions.NoInlining)]
public static bool CheckLetterOrDigitConst(int uc)
    => (LetterOrDigitCategories & (1 << uc)) != 0;

x64 (before):

mov      eax, 1
shlx     eax, eax, ecx
test     eax, 287
setne    al
movzx    rax, al

x64 (after):

mov      eax, 287
bt       eax, edi
setb     al
movzx    rax, al

…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
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jan 29, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 29, 2025
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@varelen
Copy link
Author

varelen commented Jan 29, 2025

@dotnet-policy-service agree

Comment on lines +4309 to +4331
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);
Copy link
Member

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?

Copy link
Author

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?

Copy link
Member

@jakobbotsch jakobbotsch Jan 30, 2025

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();
  ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JIT does not use BT instruction for constant mask
2 participants