Skip to content

Unnecessary conditional check #115091

Open
Open
@rameel

Description

@rameel

There is an unnecessary check generated when slicing a span with s.Slice(0, s.Length - 1).

godbolt

Repro:

public static int Test(ReadOnlySpan<char> s)
{
    while (s.Length != 0)
    {
        if (s[^1] != ' ')
            break;

        s = s.Slice(0, s.Length - 1); // unnecessary check generated
    }
    
    return s.Length;
}
Sample:Test(System.ReadOnlySpan`1[ushort]):int (FullOpts):
       push     rbp
       mov      rbp, rsp
G_M44937_IG02:  ;; offset=0x0004
       test     esi, esi
       je       SHORT G_M44937_IG04
       lea      eax, [rsi-0x01]
       mov      ecx, eax
       cmp      word  ptr [rdi+2*rcx], 32
       jne      SHORT G_M44937_IG04
       cmp      eax, esi             ; unnecessary check
       ja       SHORT G_M44937_IG06  ;
       mov      esi, eax
       jmp      SHORT G_M44937_IG02
G_M44937_IG04:  ;; offset=0x001C
       mov      eax, esi
       pop      rbp
       ret      
G_M44937_IG06:  ;; offset=0x0020
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions