Bounds checks for "a[i] followed by a[j] when j > i"

Open
#112,694 1 comment 0 reactions 1 assignee View on GitHub

@EgorBo is already working on this.

Since Feb 19, 2025.

Assessment

This issue has not been assessed yet.

Description

area-CodeGen-coreclr

Minimal repro for a case with a redundant bounds check

void Test(byte[] arr, int i, int j)
{
    if (j <= i && j >= 0) // hints
    {
        arr[i] = 0;
        arr[j] = 0; // redundant bounds check
    }
}

Current codegen:

; Assembly listing for method Bench:Test(ubyte[],int,int)
       sub      rsp, 40
       cmp      r8d, edx
       jg       SHORT G_M58188_IG04
       test     r8d, r8d
       jl       SHORT G_M58188_IG04
       mov      eax, dword ptr [rcx+0x08]
       cmp      edx, eax
       jae      SHORT G_M58188_IG05
       mov      edx, edx
       mov      byte  ptr [rcx+rdx+0x10], 0
       cmp      r8d, eax
       jae      SHORT G_M58188_IG05 ;; <---------------- redundant check
       mov      eax, r8d
       mov      byte  ptr [rcx+rax+0x10], 0
G_M58188_IG04:
       add      rsp, 40
       ret      
G_M58188_IG05:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code 52
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.