Skip to content

JIT: Optimize bit-wise and operation and compare with same constant value #111933

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

Conversation

varelen
Copy link
Contributor

@varelen varelen commented Jan 28, 2025

This pull request resolves #101000 and is based on the feedback that was given on the original PR #103868. Thanks for the previous work, it was a really great way to get started.

The code emitted for:

void Foo(int x)
{
    if ((x & 0xC000000) == 0xC000000)
        Console.WriteLine();
}

x64 (before):

and     edi, 0xC000000
cmp     edi, 0xC000000
jne     SHORT G_M000_IG04

x64 (after):

not      edi
and      edi, 0xC000000
jne      SHORT G_M000_IG04

ARM64 (before):

and	w0, w0, #0xC000000
mov	w1, #0xC000000
cmp	w0, w1
bne	G_M29168_IG04

ARM64 (after):

mvn	w0, w0
and	w0, w0, #0xC000000
cbnz	w0, G_M29168_IG04

…alue

This change optimizes bit-wise and with compare patterns like '(x & <constant>) == <constant>'

Fix dotnet#101000
@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jan 28, 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 28, 2025
@varelen varelen marked this pull request as ready for review February 4, 2025 17:19
@jakobbotsch jakobbotsch reopened this Feb 24, 2025
@jakobbotsch
Copy link
Member

Thanks for the contribution! I closed/reopened this to kick off a fresh run of the CI.

cc @dotnet/jit-contrib

@@ -4346,6 +4346,16 @@ GenTree* Lowering::OptimizeConstCompare(GenTree* cmp)
}
#endif
}
else if (andOp2->IsIntegralConst() && GenTree::Compare(andOp2, op2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you leave a comment of what this transformation does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a comment, hope it's okay.

@EgorBo
Copy link
Member

EgorBo commented Feb 25, 2025

/azp run Fuzzlyn

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@JulieLeeMSFT
Copy link
Member

@EgorBo, please review this community PR again.

@JulieLeeMSFT
Copy link
Member

@BruceForstall, please review this community PR.

@JulieLeeMSFT JulieLeeMSFT removed the request for review from EgorBo April 14, 2025 15:54
@BruceForstall
Copy link
Contributor

/azp run Fuzzlyn, runtime-coreclr outerloop

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@BruceForstall
Copy link
Contributor

/ba-g unrelated failures

@BruceForstall BruceForstall merged commit a37502b into dotnet:main Apr 23, 2025
163 of 168 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators May 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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: Optimize "x & cns == cns" pattern
5 participants