Skip to content

Type.IsValueType doesn't get folded if not followed by typeof(T) directly #86101

@hez2010

Description

@hez2010

Description

Repro:

class Program
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static nint Test<T>()
    {
        var type = typeof(T);
        var handle = type.TypeHandle;
        if (type.IsValueType)
        {
            return handle.Value;
        }
        return -1;
    }

    public static void Main()
    {
        var v1 = Test<int>(); ;
        var v2 = Test<string>();
        Console.WriteLine(v1 + v2);
    }
}

Codegen for Main:

; Method Program:Main()
G_M27646_IG01:  ;; offset=0000H
       push     rsi
       sub      rsp, 32
						;; size=5 bbWeight=1 PerfScore 1.25

G_M27646_IG02:  ;; offset=0005H
       mov      rcx, 0x1C980204A88      ; 'System.Int32'
       cmp      dword ptr [rcx], ecx
       call     [System.RuntimeType:IsValueTypeImpl():bool:this]
       test     eax, eax
       je       SHORT G_M27646_IG04
						;; size=22 bbWeight=1 PerfScore 7.50

G_M27646_IG03:  ;; offset=001BH
       mov      rsi, 0x7FFCA727E7E8
       jmp      SHORT G_M27646_IG05
						;; size=12 bbWeight=0.50 PerfScore 1.12

G_M27646_IG04:  ;; offset=0027H
       mov      rsi, -1
						;; size=10 bbWeight=0.50 PerfScore 0.12

G_M27646_IG05:  ;; offset=0031H
       mov      rcx, 0x1C9802000B8      ; 'System.String'
       cmp      dword ptr [rcx], ecx
       call     [System.RuntimeType:IsValueTypeImpl():bool:this]
       test     eax, eax
       je       SHORT G_M27646_IG07
						;; size=22 bbWeight=1 PerfScore 7.50

G_M27646_IG06:  ;; offset=0047H
       mov      rcx, 0x7FFCA7406F38
       jmp      SHORT G_M27646_IG08
						;; size=12 bbWeight=0.50 PerfScore 1.12

G_M27646_IG07:  ;; offset=0053H
       mov      rcx, -1
						;; size=10 bbWeight=0.50 PerfScore 0.12

G_M27646_IG08:  ;; offset=005DH
       add      rcx, rsi
       call     [System.Console:WriteLine(long)]
       nop      
						;; size=10 bbWeight=1 PerfScore 3.50

G_M27646_IG09:  ;; offset=0067H
       add      rsp, 32
       pop      rsi
       ret      
						;; size=6 bbWeight=1 PerfScore 1.75
; Total bytes of code: 109

But if I change the code to:

class Program
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static nint Test<T>()
    {
        var type = typeof(T);
        var handle = type.TypeHandle;
        if (typeof(T).IsValueType)
        {
            return handle.Value;
        }
        return -1;
    }

    public static void Main()
    {
        var v1 = Test<int>(); ;
        var v2 = Test<string>();
        Console.WriteLine(v1 + v2);
    }
}

The codegen will be expected:

; Method Program:Main()
G_M27646_IG01:  ;; offset=0000H
       sub      rsp, 40
						;; size=4 bbWeight=1 PerfScore 0.25

G_M27646_IG02:  ;; offset=0004H
       mov      rcx, 0x7FFCB20FE7E7
       call     [System.Console:WriteLine(long)]
       nop      
						;; size=17 bbWeight=1 PerfScore 3.50

G_M27646_IG03:  ;; offset=0015H
       add      rsp, 40
       ret      
						;; size=5 bbWeight=1 PerfScore 1.25
; Total bytes of code: 26

Metadata

Metadata

Assignees

Labels

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

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions