Skip to content

Redundant movsx in span indexing #116593

Open
@xtqqczze

Description

@xtqqczze

I expect M1 and M2 to have exactly the same codegen in the following code snippet, but M1 has a redundant sign-extension.

static int M1(ref byte reference, int length)
{
    ReadOnlySpan<byte> s = MemoryMarshal.CreateReadOnlySpan(ref reference, length);
    return s.Length > 0 ? (sbyte)s[0] : -1;
}

static int M2(ref byte reference, int length)
{
    ReadOnlySpan<byte> s = MemoryMarshal.CreateReadOnlySpan(ref reference, length);
    return s.Length > 0 ? (sbyte)MemoryMarshal.GetReference(s) : -1;
}
// coreclr trunk-20250612+15c514180982522386424c7bface57564ad6724d

C:M1(byref,int):int (FullOpts):
       test     esi, esi
       jle      SHORT G_M3362_IG05
       movzx    rax, byte  ptr [rdi]
       movsx    rax, al
       ret      
G_M3362_IG05:  ;; offset=0x000C
       mov      eax, -1
       ret      

C:M2(byref,int):int (FullOpts):
       test     esi, esi
       jle      SHORT G_M35265_IG05
       movsx    rax, byte  ptr [rdi]
       ret      
G_M35265_IG05:  ;; offset=0x0009
       mov      eax, -1
       ret      

https://csharp.godbolt.org/z/6P4W3PPGf

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