Skip to content

Eliminate bound checks for "arr[arr.Length - cns]" - #84213

Merged
EgorBo merged 6 commits into
dotnet:mainfrom
EgorBo:bound-checks-arr-add-cns
Apr 2, 2023
Merged

Eliminate bound checks for "arr[arr.Length - cns]"#84213
EgorBo merged 6 commits into
dotnet:mainfrom
EgorBo:bound-checks-arr-add-cns

Conversation

@EgorBo

@EgorBoEgorBo commented Apr 1, 2023

Copy link
Copy Markdown
Member

Motivation: #84210 (comment)

voidTest1(int[]arr){if(arr.Length==0||arr[arr.Length-1]==0)Console.WriteLine();}voidTest2(int[]arr){if(arr.Length<10)return;if(arr[^10]==0)Console.WriteLine();}

Previous codegen:

; Method Test1(int[]):thissubrsp,40moveax, dword ptr [rdx+08H]testeax,eaxje SHORT G_M32023_IG04leaecx,[rax-01H]cmpecx,eaxjae SHORT G_M32023_IG06moveax,ecxcmp dword ptr [rdx+4*rax+10H],0jne SHORT G_M32023_IG05G_M32023_IG04:addrsp,40 tail.jmp[System.Console:WriteLine()]G_M32023_IG05:addrsp,40retG_M32023_IG06:call CORINFO_HELP_RNGCHKFAILint3; Total bytes of code: 48; Method Test2(int[]):thissubrsp,40moveax, dword ptr [rdx+08H]cmpeax,10jge SHORT G_M54292_IG04addrsp,40retG_M54292_IG04:leaecx,[rax-0AH]cmpecx,eaxjae SHORT G_M54292_IG07moveax,ecxcmp dword ptr [rdx+4*rax+10H],0jne SHORT G_M54292_IG06addrsp,40 tail.jmp[System.Console:WriteLine()]G_M54292_IG06:addrsp,40retG_M54292_IG07:call CORINFO_HELP_RNGCHKFAILint3; Total bytes of code: 54

New codegen:

; Method Test1(int[]):thismoveax, dword ptr [rdx+08H]testeax,eaxje SHORT G_M32023_IG04deceaxcmp dword ptr [rdx+4*rax+10H],0jne SHORT G_M32023_IG05G_M32023_IG04: tail.jmp[System.Console:WriteLine()]G_M32023_IG05:ret; Total bytes of code: 23; Method Test2(int[]):thismoveax, dword ptr [rdx+08H]cmpeax,10jge SHORT G_M54292_IG04retG_M54292_IG04:addeax,-10cmp dword ptr [rdx+4*rax+10H],0jne SHORT G_M54292_IG06 tail.jmp[System.Console:WriteLine()]G_M54292_IG06:ret; Total bytes of code: 26

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

ghost commented Apr 1, 2023

Copy link
Copy Markdown

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

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

Was:

; Method Prog:Test(int[]):thissubrsp,40moveax, dword ptr [rdx+08H]testeax,eaxje SHORT G_M2580_IG04leaecx,[rax-01H]cmpecx,eaxjae SHORT G_M2580_IG07moveax,ecxcmp dword ptr [rdx+4*rax+10H],0jne SHORT G_M2580_IG05G_M2580_IG04:call[System.Console:WriteLine()]G_M2580_IG05:nopaddrsp,40retG_M2580_IG07:call CORINFO_HELP_RNGCHKFAILint3; Total bytes of code: 45

New:

; Method Prog:Test(int[]):thissubrsp,40moveax, dword ptr [rdx+08H]testeax,eaxje SHORT G_M2580_IG04deceaxcmp dword ptr [rdx+4*rax+10H],0jne SHORT G_M2580_IG05G_M2580_IG04:call[System.Console:WriteLine()]G_M2580_IG05:nopaddrsp,40ret; Total bytes of code: 32
Author:EgorBo
Assignees:-
Labels:

area-CodeGen-coreclr

Milestone:-

@EgorBo

Copy link
Copy Markdown
MemberAuthor

@AndyAyersMS@jakobbotsch @dotnet/jit-contrib PTAL

@EgorBo

Copy link
Copy Markdown
MemberAuthor

Diffs, no TP impact.

@AndyAyersMSAndyAyersMS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

@EgorBo
EgorBo merged commit 443bf51 into dotnet:mainApr 2, 2023
@EgorBo
EgorBo deleted the bound-checks-arr-add-cns branch April 2, 2023 15:55
@EgorBoEgorBo mentioned this pull request Apr 4, 2023
@ghostghost locked as resolved and limited conversation to collaborators May 2, 2023
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR 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

@EgorBo@AndyAyersMS