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] Optimization for CMP REG, 0xFFFFFFFF(FFFFFFFF) -> INC REG #52523

Closed

Conversation

DarkBullNull
Copy link
Contributor

@DarkBullNull DarkBullNull commented May 9, 2021

Optimization for (example):

bool Test(uint x)
{
    return x == uint.MaxValue; // or ulong.MaxValue
}

If in REG 0xFFFFFFFF, then instead of CMP => INC REG, thus the ZF flag changes in 1, and SETcc set byte in returned REG
If there are any pitfalls here, please explain to me.
Before:

G_M21953_IG02:
       cmp      edx, -1 ; or cmp edx, 0xFFFFFFFF
       sete     al
       movzx    rax, al
G_M21953_IG03:
       ret 

After:

G_M21953_IG02:
       inc      edx
       sete     al
       movzx    rax, al
G_M21953_IG03:
       ret 

SPMI:
https://gist.github.com/DarkBullNull/1ea1218c1260b4ddb232ac2e07cf8c01

@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 May 9, 2021
@DarkBullNull DarkBullNull changed the title [JIT] Optimization for CMP REG, 0xFFFFFFFF(FFFFFFFF) -> INC [JIT] Optimization for CMP REG, 0xFFFFFFFF(FFFFFFFF) -> INC REG May 9, 2021
// "INC REG" instead of "CMP REG, 0xFFFFFFFF" (or just - if "CMP REG, -1")
if (op1->isUsedFromReg() && op2->IsIntegralConst(-1) && targetReg != REG_NA)
{
emit->emitIns_R(INS_inc, emitTypeSize(tree), op1->GetRegNum());
Copy link
Member

Choose a reason for hiding this comment

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

Not sure I understand this optimization - doesn't it modify op1 reg?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, good point! My big mistake. Yes, it changes the logic of the program by modify op1 value. Thank you.

@GrabYourPitchforks
Copy link
Member

Per Agner (see https://www.agner.org/optimize/microarchitecture.pdf, Sec. 12.6, Table 12.1), this might suppress macro-op fusion?

@DarkBullNull DarkBullNull deleted the jit-optimization-cmp-to-inc branch May 10, 2021 11:51
@karelz karelz added this to the 6.0.0 milestone May 20, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jun 19, 2021
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants