Skip to content

typeof(T).IsValueType isn't fully optimized for reference types T #32094

Closed
@GrabYourPitchforks

Description

@GrabYourPitchforks

Related: #1157

It looks like there are still some cases where typeof(T).IsValueType isn't fully optimized by the JIT. Repro:

static void Main(string[] args)
{
    while (true)
    {
        ReturnIsValueType<bool>();
        ReturnIsValueType<bool?>();
        ReturnIsValueType<string>();
    }
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static bool ReturnIsValueType<T>()
{
    return typeof(T).IsValueType;
}

For value types bool and bool? this is optimized as expected.

; for T = bool
00007ffd`51100bf0 b801000000      mov     eax,1
00007ffd`51100bf5 c3              ret

; for T = Nullable<bool>
00007ffd`51100c10 b801000000      mov     eax,1
00007ffd`51100c15 c3              ret

But for string there's still some throwaway work involving the passed type parameter and a stack spill before eventually returning the constant value false.

; for T = string
00007ffd`51100c30 50              push    rax
00007ffd`51100c31 48890c24        mov     qword ptr [rsp],rcx
00007ffd`51100c35 33c0            xor     eax,eax
00007ffd`51100c37 4883c408        add     rsp,8
00007ffd`51100c3b c3              ret

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions