Skip to content

Jit doesn't recognise ldstr as non-null #12011

@benaadams

Description

@benaadams
public ReadOnlySpan<char> Hello() => "Hello".AsSpan();
.method public hidebysig 
    instance valuetype [System.Memory]System.ReadOnlySpan`1<char> Hello () cil managed 
{
    // Method begins at RVA 0x2050
    // Code size 11 (0xb)
    .maxstack 8

    IL_0000: ldstr "Hello"
    IL_0005: call valuetype [System.Memory]System.ReadOnlySpan`1<char> [System.Memory]System.MemoryExtensions::AsSpan(string)
    IL_000a: ret
}
G_M23320_IG01:
       nop      

G_M23320_IG02:
       mov      rax, 0xD1FFAB1E
       mov      rax, gword ptr [rax]
       test     rax, rax               ; not needed
       jne      SHORT G_M23320_IG03    ; not needed
       xor      rcx, rcx               ; not needed
       xor      r8d, r8d               ; not needed
       jmp      SHORT G_M23320_IG04    ; not needed

G_M23320_IG03:
       mov      ecx, dword ptr [rax]   ; not needed   
       mov      rcx, rax               ; not needed             ?
       cmp      dword ptr [rcx], ecx   ; not needed
       add      rcx, 12                ; r8d, dword ptr [rax+8] ?
       mov      r8d, dword ptr [rax+8] ; add      rax, 12       ?

G_M23320_IG04:
       mov      bword ptr [rdx], rcx   ; bword ptr [rdx], rax   ?
       mov      dword ptr [rdx+8], r8d
       mov      rax, rdx

G_M23320_IG05:
       ret      

; Total bytes of code 56, prolog size 5 for method Program:Hello():struct:this

/cc @GrabYourPitchforks

/cc @KrzysztofCwalina as you mentioned in dotnet/roslyn#24621 (comment)

Sure, but it has nothing to do with ranges. We could do the same optimization for

ReadOnlySpan<char> x = "str"; 

category:cq
theme:basic-cq
skill-level:expert
cost:large

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions