Skip to content

Redundant bounds check #121259

Closed
Closed

Description

@EgorBo

The following snippet produces a redundant bounds check:

void Test(ReadOnlySpan<char> name)
{
    int length = name.Length;
    if (length == 0)
    {
        return;
    }
    if (length == 1)
    {
        if (name[0] == ':')
        {
            return;
        }
    }
    else if (name[0] == ':' && name[1] != ':')
    {
        return;
    }
}

Codegen:

; Method Program:Test(System.ReadOnlySpan`1[char]):this (FullOpts)
       sub      rsp, 40
       mov      rax, bword ptr [rdx]
       mov      ecx, dword ptr [rdx+0x08]
       test     ecx, ecx
       je       SHORT G_M56625_IG05
       cmp      ecx, 1
       je       SHORT G_M56625_IG04
       cmp      word  ptr [rax], 58
       jne      SHORT G_M56625_IG05
       cmp      ecx, 1
       jbe      SHORT G_M56625_IG06
       jmp      SHORT G_M56625_IG05
G_M56625_IG04:
       cmp      word  ptr [rax], ax
G_M56625_IG05:
       add      rsp, 40
       ret      
G_M56625_IG06:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 46

It's actually a simplified version of:

if (name is [] or [':'] or [':', not ':', ..])
{
}

Activity

  1. added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Nov 1, 2025
  2. added and removed
    untriagedNew issue has not been triaged by the area owner
    on Nov 1, 2025
  3. added this to the Future milestone on Nov 1, 2025
  4. dotnet-policy-service commented on Nov 1, 2025

    @dotnet-policy-service
    Contributor

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

  5. modified the milestones: Future, 11.0.0 on Nov 3, 2025
  6. self-assigned this
    on Nov 3, 2025
  7. locked and limited conversation to collaborators on Dec 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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