Skip to content

JIT: Redundant bounds check for when loopUpperBound==arrayLength #114744

@BoyBaykiller

Description

@BoyBaykiller

Loop cloning generates a fast path without bounds check but this can be statically optimized without needing to keep the slow path: https://godbolt.org/z/3WeaEfWh3

static void Test(int count)
{
    int[] arrA = new int[count];
    float[] arrB = new float[count];
    for (int i = 0; i < count; i++)
    {
        arrA[i] = 0;     // bounds check
        arrB[i] = 0.0f;  // bounds check
    }
}
Program:Test(int) (FullOpts):
       push     rbp
       push     r15
       push     rbx
       lea      rbp, [rsp+0x10]
       mov      ebx, edi
       movsxd   rsi, ebx
       mov      rdi, 0x79E0784C8C88      ; int[]
       call     CORINFO_HELP_NEWARR_1_VC
       mov      r15, rax
       movsxd   rsi, ebx
       mov      rdi, 0x79E078711E90      ; float[]
       call     CORINFO_HELP_NEWARR_1_VC
       xor      ecx, ecx
       test     ebx, ebx
       jle      SHORT G_M29168_IG06
       cmp      dword ptr [r15+0x08], ebx
       jl       SHORT G_M29168_IG07
       cmp      dword ptr [rax+0x08], ebx
       jl       SHORT G_M29168_IG07
       mov      ecx, 16
       align    [0 bytes for IG04]
G_M29168_IG04:  ;; offset=0x0048
       xor      edx, edx
       mov      dword ptr [r15+rcx], edx
       mov      dword ptr [rax+rcx], edx
       add      rcx, 4
       dec      ebx
       jne      SHORT G_M29168_IG04
G_M29168_IG06:  ;; offset=0x0059
       pop      rbx
       pop      r15
       pop      rbp
       ret      
G_M29168_IG07:  ;; offset=0x005E
       cmp      ecx, dword ptr [r15+0x08]
       jae      SHORT G_M29168_IG09
       mov      edx, ecx
       xor      edi, edi
       mov      dword ptr [r15+4*rdx+0x10], edi
       cmp      ecx, dword ptr [rax+0x08]
       jae      SHORT G_M29168_IG09
       mov      edx, ecx
       mov      dword ptr [rax+4*rdx+0x10], edi
       inc      ecx
       cmp      ecx, ebx
       jl       SHORT G_M29168_IG07
       jmp      SHORT G_M29168_IG06
G_M29168_IG09:  ;; offset=0x0080
       call     CORINFO_HELP_RNGCHKFAIL
       int3     

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions