Skip to content

Conversation

Lotendan
Copy link

Extend the "skip-test" optimization to all instructions that modify the same flags as "TEST" x86 instruction

Originally wanted to only cover the "POPCNT" case, but actually we can go further than that and cover more instructions.

Fixes #118811

Using the following code:

static void M1(int value)
{
    if (int.PopCount(value) > 0) 
        throw null!;
}

This fix reduces this assembly:

popcnt   edi, edi
test     edi, edi
jg       SHORT G_M9581_IG04

to this assembly:

popcnt    edi, edi
jg       SHORT G_M000_IG04

Note: this is not exactly as was intended in the issue, where a jne was expected. But that should be good enough.
Note2: this does not affect tnzcnt or lzcnt instructions as these instructions do not modify the flags.

same flags as "TEST" x86 instruction

Originally wanted to only cover the "POPCNT" case, but actually we can
go further than that

Fix dotnet#118811
@github-actions github-actions bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Oct 19, 2025
@Lotendan
Copy link
Author

@dotnet-policy-service agree

// "TEST" instruction:
// - Resets OF and CF flags to zero
// - Sets SF, ZF, PF flags from the result
// - Leaves AF undefined
Copy link
Author

Choose a reason for hiding this comment

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

Unsure here whether it makes sense to add a condition on AF flag as well

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.

Redundant test after PopCount

1 participant