Skip to content

JIT: remove bound check for negated ranges #96123

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

Merged
merged 2 commits into from
Dec 20, 2023
Merged

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Dec 18, 2023

Closes #96046

byte Test(int a) => rva[8 - (a & 7)];

static ReadOnlySpan<byte> rva => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

Was:

; Method Program:Test(int):ubyte:this (FullOpts)
       sub      rsp, 40
       and      ecx, 7
       mov      eax, ecx
       neg      eax
       add      eax, 8
       cmp      eax, 10
       jae      SHORT G_M949_IG04
       mov      rcx, 0x1735CBE29D0
       movzx    rax, byte  ptr [rax+rcx]
       add      rsp, 40
       ret
G_M949_IG04:
       call     CORINFO_HELP_RNGCHKFAIL
       int3
; Total bytes of code 44

Now:

; Method Program:Test(int):ubyte:this (FullOpts)
       sub      rsp, 40
       and      edx, 7
       mov      eax, edx
       neg      eax
       add      eax, 8
       mov      rcx, 0x228C6572A20
       movzx    rax, byte  ptr [rax+rcx]
       add      rsp, 40
       ret      
; Total bytes of code: 33
@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 Dec 18, 2023
@ghost ghost assigned EgorBo Dec 18, 2023
@ghost
Copy link

ghost commented Dec 18, 2023

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

Issue Details

Closes #96046

byte Test(int a) => rva[8 - (a & 7)];

static ReadOnlySpan<byte> rva => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

Was:

; Method Program:Test(int):ubyte:this (FullOpts)
       sub      rsp, 40
       and      ecx, 7
       mov      eax, ecx
       neg      eax
       add      eax, 8
       cmp      eax, 10
       jae      SHORT G_M949_IG04
       mov      rcx, 0x1735CBE29D0
       movzx    rax, byte  ptr [rax+rcx]
       add      rsp, 40
       ret
G_M949_IG04:
       call     CORINFO_HELP_RNGCHKFAIL
       int3
; Total bytes of code 44

Now:

; Method Program:Test(int):ubyte:this (FullOpts)
       sub      rsp, 40
       and      edx, 7
       mov      eax, edx
       neg      eax
       add      eax, 8
       mov      rcx, 0x228C6572A20
       movzx    rax, byte  ptr [rax+rcx]
       add      rsp, 40
       ret      
; Total bytes of code: 33
Author: EgorBo
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -
@EgorBo
Copy link
Member Author

EgorBo commented Dec 18, 2023

@dotnet/jit-contrib PTAL, small change, diffs aren't too big, but it closes #96046

Comment on lines 558 to 559
result.lLimit = Limit(Limit::keConstant, -range.UpperLimit().GetConstant());
result.uLimit = Limit(Limit::keConstant, -range.LowerLimit().GetConstant());
Copy link
Member

Choose a reason for hiding this comment

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

Can either the existing upper or lower limit be INT_MIN, in which case -INT_MIN == INT_MIN?

Copy link
Member Author

@EgorBo EgorBo Dec 19, 2023

Choose a reason for hiding this comment

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

I wasn't able to repro such a case, but added a check just in case 👍

@EgorBo EgorBo merged commit 152463d into dotnet:main Dec 20, 2023
@EgorBo EgorBo deleted the neg-bound-checks branch December 20, 2023 12:42
@github-actions github-actions bot locked and limited conversation to collaborators Jan 20, 2024
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
2 participants