Skip to content

Redundant bound checks for arr[^N-1] after arr[^N] #124531

@EgorBo

Description

@EgorBo
public int DecodeRemaining(ReadOnlySpan<int> source)
    => source[^4] | source[^3] | source[^2] | source[^1];

currently generates:

; Method Program:DecodeRemaining(System.ReadOnlySpan`1[int]):int:this (FullOpts)
G_M18869_IG01:
       sub      rsp, 40
G_M18869_IG02:
       mov      rax, bword ptr [rdx]
       mov      ecx, dword ptr [rdx+0x08]
       lea      edx, [rcx-0x04]
       cmp      edx, ecx
       jae      SHORT G_M18869_IG04
       mov      edx, dword ptr [rax+4*rdx]
       lea      r8d, [rcx-0x03]
       cmp      r8d, ecx
       jae      SHORT G_M18869_IG04
       or       edx, dword ptr [rax+4*r8]
       lea      r8d, [rcx-0x02]
       cmp      r8d, ecx
       jae      SHORT G_M18869_IG04
       or       edx, dword ptr [rax+4*r8]
       dec      ecx
       or       edx, dword ptr [rax+4*rcx]
       mov      eax, edx
G_M18869_IG03:
       add      rsp, 40
       ret      
G_M18869_IG04:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 64

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions