Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redundant bounds check #104890

Open
Tracked by #109677
rameel opened this issue Jul 15, 2024 · 2 comments
Open
Tracked by #109677

Redundant bounds check #104890

rameel opened this issue Jul 15, 2024 · 2 comments
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Milestone

Comments

@rameel
Copy link

rameel commented Jul 15, 2024

Unnecessary bounds check in the following snippet

static int Test(ReadOnlySpan<char> value)
{
    var i = 0;
    for (; i < value.Length; i++)
        if (value[i] != '/')
            break;

    value = value.Slice(i);

    foreach (var c in value)
    { }

    return 0;
}

Codegen: sharplab, (godbolt)

G_M000_IG01:                ;; offset=0x0000
       sub      rsp, 40

G_M000_IG02:                ;; offset=0x0004
       mov      rax, bword ptr [rcx]
       mov      ecx, dword ptr [rcx+0x08]
       xor      edx, edx
       xor      r8d, r8d
       test     ecx, ecx
       jle      SHORT G_M000_IG05
       align    [13 bytes for IG03]

G_M000_IG03:                ;; offset=0x0020
       cmp      word  ptr [rax+2*r8], 47
       jne      SHORT G_M000_IG04
       inc      r8d
       cmp      r8d, ecx
       jl       SHORT G_M000_IG03

G_M000_IG04:                ;; offset=0x0030
       mov      edx, r8d

G_M000_IG05:                ;; offset=0x0033
       cmp      edx, ecx
       ja       SHORT G_M000_IG09
       sub      ecx, edx
       xor      eax, eax
       test     ecx, ecx
       jle      SHORT G_M000_IG07
       align    [1 bytes for IG06]

G_M000_IG06:                ;; offset=0x0040
       cmp      eax, ecx
       jae      SHORT G_M000_IG10
       inc      eax
       cmp      eax, ecx
       jl       SHORT G_M000_IG06

G_M000_IG07:                ;; offset=0x004A
       xor      eax, eax

G_M000_IG08:                ;; offset=0x004C
       add      rsp, 40
       ret      

G_M000_IG09:                ;; offset=0x0051
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     

G_M000_IG10:                ;; offset=0x0058
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 94
@rameel rameel added the tenet-performance Performance related issue label Jul 15, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jul 15, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jul 15, 2024
Copy link
Contributor

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

@rameel rameel changed the title Redundant bound check Redundant bounds check Jul 15, 2024
@EgorBo EgorBo added this to the Future milestone Jul 15, 2024
@EgorBo EgorBo removed the untriaged New issue has not been triaged by the area owner label Jul 15, 2024
@EgorBo
Copy link
Member

EgorBo commented Mar 25, 2025

CORINFO_HELP_RNGCHKFAIL is now removed with #113862, ThrowArgumentOutOfRangeException is still there

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

2 participants