Optimize typeof(T).IsValueType by EgorBo · Pull Request #1157 · dotnet/runtime (original) (raw)
Fixes https://github.com/dotnet/coreclr/issues/23208
Also, contributes to https://github.com/dotnet/coreclr/issues/2591
Also, dotnet/aspnetcore@a42fff6
Replaces patterns like typeof(T).IsValueType
or var.GetType().IsValueType()
with constants (true/false).
E.g.:
class Case1 { public bool Foo() => typeof(T).IsValueType;
public bool Test1() => Foo<int>();
public bool Test2() => Foo<string>();
}
Current codegen:
; Method Case1:Test1():bool:this G_M15309_IG01: sub rsp, 40 G_M15309_IG02: mov rcx, 0xD1FFAB1E call CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE mov rcx, rax mov rax, qword ptr [(reloc)] cmp dword ptr [rcx], ecx G_M15309_IG03: add rsp, 40 rex.jmp rax ; Total bytes of code: 38
; Method Case1:Test2():bool:this G_M7114_IG01: sub rsp, 40 G_M7114_IG02: mov rcx, 0xD1FFAB1E call CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE mov rcx, rax mov rax, qword ptr [(reloc)] cmp dword ptr [rcx], ecx G_M7114_IG03: add rsp, 40 rex.jmp rax ; Total bytes of code: 38
New codegen:
; Method Case1:Test1():bool:this
G_M15308_IG02:
mov eax, 1
G_M15308_IG03:
ret
; Total bytes of code: 6
; Method Case1:Test2():bool:this
G_M7113_IG02:
xor eax, eax
G_M7113_IG03:
ret
; Total bytes of code: 3
This PR also implements Type.IsPrimitive
and Type.IsClass
. Probably can implement more for free, e.g. Type.IsArray
and Type.IsEnum
(this one is virtual)