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

Optimize JTRUE(cast_helper EQ/NE null) expansion #110272

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Nov 29, 2024

Slightly optimize cast expansion when the actual result is used inside "NE/EQ 0" ("is instance" pattern). This improvement is motivated by the comment https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-9/?commentid=21225#comment-21225

public bool Test()
{
    if (_obj is B)
        return true;
    return false;
}

Current codegen:

; Assembly listing for method Tests:Test():ubyte:this (Tier1)
G_M39947_IG01:
       sub      rsp, 40
G_M39947_IG02:
       mov      rdx, gword ptr [rcx+0x08]
       mov      rax, rdx
       test     rax, rax
       je       SHORT G_M39947_IG05
G_M39947_IG03:
       mov      rcx, 0x7FFC7909C970      ; Tests+C
       cmp      qword ptr [rax], rcx
       je       SHORT G_M39947_IG05
G_M39947_IG04:
       mov      rcx, 0x7FFC7909C7F8      ; Tests+B
       call     CORINFO_HELP_ISINSTANCEOFCLASS
G_M39947_IG05:
       test     rax, rax    ;; <-- we can duplicate this tail into return true/return false tails.
       setne    al
       movzx    rax, al
G_M39947_IG06:
       add      rsp, 40
       ret
; Total bytes of code 60

New codegen:

; Assembly listing for method Tests:Test():ubyte:this (Tier1)
G_M39947_IG01:
       sub      rsp, 40
G_M39947_IG02:
       mov      rdx, gword ptr [rcx+0x08]
       mov      rax, rdx
       test     rax, rax
       je       SHORT G_M39947_IG07
G_M39947_IG03:
       mov      rcx, 0x7FFC7909C970      ; Tests+C
       cmp      qword ptr [rax], rcx
       je       SHORT G_M39947_IG05
G_M39947_IG04:
       mov      rcx, 0x7FFC7909C7F8      ; Tests+B
       call     CORINFO_HELP_ISINSTANCEOFCLASS
       test     rax, rax
       je       SHORT G_M39947_IG07
G_M39947_IG05:
       mov      eax, 1
G_M39947_IG06:
       add      rsp, 40
       ret
G_M39947_IG07:
       xor      eax, eax
G_M39947_IG08:
       add      rsp, 40
       ret
; Total bytes of code 68

Diff: https://www.diffchecker.com/swl6knBa/
(slightly bigger size in this case due to duplicated epilogue, but no more extra "is zero" check)

PS: this PR needs #107499 to handle return obj is X; like patterns.

@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 Nov 29, 2024
Copy link
Contributor

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

@EgorBo

This comment was marked as resolved.

This comment was marked as resolved.

@EgorBo
Copy link
Member Author

EgorBo commented Nov 29, 2024

/azp run runtime-coreclr pgo

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo
Copy link
Member Author

EgorBo commented Nov 30, 2024

/azp run runtime-coreclr pgo

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant