KEMBAR78
Eliminate bound checks for "arr[arr.Length - cns]" by EgorBo · Pull Request #84213 · dotnet/runtime · GitHub
Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Apr 1, 2023

Motivation: #84210 (comment)

void Test1(int[] arr)
{
    if (arr.Length == 0 || arr[arr.Length - 1] == 0)
        Console.WriteLine();
}

void Test2(int[] arr)
{
    if (arr.Length < 10)
        return;
    
    if (arr[^10] == 0)
        Console.WriteLine();
}

Previous codegen:

; Method Test1(int[]):this
       sub      rsp, 40
       mov      eax, dword ptr [rdx+08H]
       test     eax, eax
       je       SHORT G_M32023_IG04
       lea      ecx, [rax-01H]
       cmp      ecx, eax
       jae      SHORT G_M32023_IG06
       mov      eax, ecx
       cmp      dword ptr [rdx+4*rax+10H], 0
       jne      SHORT G_M32023_IG05
G_M32023_IG04:
       add      rsp, 40
       tail.jmp [System.Console:WriteLine()]
G_M32023_IG05:
       add      rsp, 40
       ret      
G_M32023_IG06:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 48


; Method Test2(int[]):this
       sub      rsp, 40
       mov      eax, dword ptr [rdx+08H]
       cmp      eax, 10
       jge      SHORT G_M54292_IG04
       add      rsp, 40
       ret      
G_M54292_IG04:
       lea      ecx, [rax-0AH]
       cmp      ecx, eax
       jae      SHORT G_M54292_IG07
       mov      eax, ecx
       cmp      dword ptr [rdx+4*rax+10H], 0
       jne      SHORT G_M54292_IG06
       add      rsp, 40
       tail.jmp [System.Console:WriteLine()]
G_M54292_IG06:
       add      rsp, 40
       ret      
G_M54292_IG07:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 54

New codegen:

; Method Test1(int[]):this
       mov      eax, dword ptr [rdx+08H]
       test     eax, eax
       je       SHORT G_M32023_IG04
       dec      eax
       cmp      dword ptr [rdx+4*rax+10H], 0
       jne      SHORT G_M32023_IG05
G_M32023_IG04:
       tail.jmp [System.Console:WriteLine()]
G_M32023_IG05:
       ret      
; Total bytes of code: 23


; Method Test2(int[]):this
       mov      eax, dword ptr [rdx+08H]
       cmp      eax, 10
       jge      SHORT G_M54292_IG04
       ret      
G_M54292_IG04:
       add      eax, -10
       cmp      dword ptr [rdx+4*rax+10H], 0
       jne      SHORT G_M54292_IG06
       tail.jmp [System.Console:WriteLine()]
G_M54292_IG06:
       ret      
; Total bytes of code: 26

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 1, 2023
@ghost ghost assigned EgorBo Apr 1, 2023
@ghost
Copy link

ghost commented Apr 1, 2023

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

Issue Details
void Test(int[] arr)
{
    if (arr.Length == 0 || arr[arr.Length - 1] == 0)
    {
        Console.WriteLine();
    }
}

Was:

; Method Prog:Test(int[]):this
       sub      rsp, 40
       mov      eax, dword ptr [rdx+08H]
       test     eax, eax
       je       SHORT G_M2580_IG04
       lea      ecx, [rax-01H]
       cmp      ecx, eax
       jae      SHORT G_M2580_IG07
       mov      eax, ecx
       cmp      dword ptr [rdx+4*rax+10H], 0
       jne      SHORT G_M2580_IG05
G_M2580_IG04:
       call     [System.Console:WriteLine()]
G_M2580_IG05:
       nop      
       add      rsp, 40
       ret      
G_M2580_IG07:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 45

New:

; Method Prog:Test(int[]):this
       sub      rsp, 40
       mov      eax, dword ptr [rdx+08H]
       test     eax, eax
       je       SHORT G_M2580_IG04
       dec      eax
       cmp      dword ptr [rdx+4*rax+10H], 0
       jne      SHORT G_M2580_IG05
G_M2580_IG04:
       call     [System.Console:WriteLine()]
G_M2580_IG05:
       nop   
       add      rsp, 40
       ret      
; Total bytes of code: 32
Author: EgorBo
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo
Copy link
Member Author

EgorBo commented Apr 1, 2023

@AndyAyersMS @jakobbotsch @dotnet/jit-contrib PTAL

@EgorBo
Copy link
Member Author

EgorBo commented Apr 1, 2023

Diffs, no TP impact.

Copy link
Member

@AndyAyersMS AndyAyersMS left a comment

Choose a reason for hiding this comment

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

LGTM.

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.

2 participants