Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize typeof(T).IsValueType #1157

Merged
merged 27 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5ef9ec3
Intrinsify typeof(T).IsValueType, IsClass, IsPrimitiveType
EgorBo Dec 26, 2019
ff69f86
Add tests
EgorBo Dec 26, 2019
ffbacad
Improve tests, fix copy-paste
EgorBo Dec 26, 2019
5868336
Improve tests, add Enum
EgorBo Dec 26, 2019
7bf4a12
Ignore GT_RUNTIMELOOKUP argument
EgorBo Dec 26, 2019
76a0312
Add IL tests (for ldtoken type& case)
EgorBo Dec 26, 2019
86e7842
fix formatting issues
EgorBo Dec 26, 2019
b71f1be
Handle Enum (is not primitive), improve tests, add more test cases
EgorBo Dec 26, 2019
5c8ff26
Add more test cases
EgorBo Dec 26, 2019
631a9ca
implement isEnum
EgorBo Dec 26, 2019
ac03c32
re-implement isEnum check
EgorBo Dec 26, 2019
3b37a90
formatting issues
EgorBo Dec 26, 2019
0926bbf
temp commit: disable optimization to check my tests
EgorBo Dec 26, 2019
b24213f
Simplify IL tests
EgorBo Dec 26, 2019
e36adb3
Fix copy-paste errors in tests, add more test cases.
EgorBo Dec 26, 2019
627e7a0
Use gtGetHelperArgClassHandle
EgorBo Dec 26, 2019
1fc0b71
Add tests for __Canon
EgorBo Dec 26, 2019
6b37368
Use canInlineTypeCheckWithObjectVTable() to detect __Canon
EgorBo Dec 27, 2019
3a53e71
fix formatting issues
EgorBo Dec 27, 2019
363d683
Add __Canon to well-known types
EgorBo Dec 27, 2019
fc0d723
Cleanup
EgorBo Dec 27, 2019
22a064a
Cleanup
EgorBo Dec 27, 2019
75d5c84
Cleanup
EgorBo Dec 27, 2019
a6f6f13
Remove IsClass and IsPrimitive
EgorBo Dec 27, 2019
b7e054f
Formatting
EgorBo Dec 27, 2019
5837874
Use asCorInfoType instead of getTypeForPrimitiveValueClass
EgorBo Dec 27, 2019
5bfe840
formatting
EgorBo Dec 27, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ CorJitResult __stdcall interceptor_ICJC::compileMethod(ICorJitInfo*
our_ICorJitInfo.getBuiltinClass(CLASSID_STRING);
our_ICorJitInfo.getBuiltinClass(CLASSID_ARGUMENT_HANDLE);
our_ICorJitInfo.getBuiltinClass(CLASSID_RUNTIME_TYPE);
our_ICorJitInfo.getBuiltinClass(CLASSID_ENUM);

#ifdef fatMC
// to build up a fat mc
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/src/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,7 @@ enum CorInfoClassId
CLASSID_STRING,
CLASSID_ARGUMENT_HANDLE,
CLASSID_RUNTIME_TYPE,
CLASSID_ENUM,
};

enum CorInfoInline
Expand Down
84 changes: 84 additions & 0 deletions src/coreclr/src/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4015,6 +4015,75 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis,
break;
}

case NI_System_Type_get_IsClass:
case NI_System_Type_get_IsPrimitive:
case NI_System_Type_get_IsValueType:
{
// Optimize things like
//
// ldtoken [Type]
// call Type.GetTypeFromHandle (which is replaced with CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE)
// call Type.IsValueType (also IsClass and IsPrimitive)
//
// to `true` or `false`
// e.g. `typeof(int).IsValueType` => `true`
if (impStackTop().val->IsCall())
{
GenTreeCall* call = impStackTop().val->AsCall();
if (call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE))
{
GenTree* arg = call->Args().begin()->GetNode();
if (!arg->IsIntegralConst())
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
{
// e.g. GT_RUNTIMELOOKUP
break;
}
GenTreeIntCon* handle = arg->AsIntCon();
auto hClass = reinterpret_cast<CORINFO_CLASS_HANDLE>(handle->IconValue());
DWORD classAttr = info.compCompHnd->getClassAttribs(hClass);
if (classAttr & CORINFO_FLG_SHAREDINST)
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
{
// we have shared type instance
break;
}
BOOL isValueType = eeIsValueClass(hClass);
CorInfoType cit = info.compCompHnd->getTypeForPrimitiveValueClass(hClass);
if (ni == NI_System_Type_get_IsPrimitive)
{
if (isValueType)
{
bool isEnum = info.compCompHnd->getBuiltinClass(CLASSID_ENUM) ==
info.compCompHnd->getParentType(hClass);
retNode = gtNewIconNode(
(cit >= CORINFO_TYPE_BOOL) && (cit <= CORINFO_TYPE_DOUBLE) && !isEnum ? 1 : 0);
}
else
{
retNode = gtNewIconNode(0);
}
}
else if (ni == NI_System_Type_get_IsClass)
{
BOOL isInterface = info.compCompHnd->getClassAttribs(hClass) & CORINFO_FLG_INTERFACE;
jkotas marked this conversation as resolved.
Show resolved Hide resolved
retNode = // typeof(int*).IsClass has to be true (CORINFO_TYPE_PTR)
gtNewIconNode((cit == CORINFO_TYPE_PTR) || (!isValueType && !isInterface) ? 1 : 0);

}
else if (ni == NI_System_Type_get_IsValueType)
{
retNode = gtNewIconNode((isValueType && cit != CORINFO_TYPE_PTR) ? 1 : 0);
}
else
{
assert(false);
}
// drop CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE call
impPopStack();
}
}
break;
}

#ifdef FEATURE_HW_INTRINSICS
case NI_System_Math_FusedMultiplyAdd:
case NI_System_MathF_FusedMultiplyAdd:
Expand Down Expand Up @@ -4306,6 +4375,21 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
result = NI_System_GC_KeepAlive;
}
}
else if (strcmp(className, "notType") == 0) // CI test
{
if (strcmp(methodName, "get_IsValueType") == 0)
{
result = NI_System_Type_get_IsValueType;
}
else if (strcmp(methodName, "get_IsClass") == 0)
{
result = NI_System_Type_get_IsClass;
}
else if (strcmp(methodName, "get_IsPrimitive") == 0)
{
result = NI_System_Type_get_IsPrimitive;
}
}
}
#if defined(_TARGET_XARCH_) // We currently only support BSWAP on x86
else if (strcmp(namespaceName, "System.Buffers.Binary") == 0)
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/src/jit/namedintrinsiclist.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ enum NamedIntrinsic : unsigned short
NI_System_Collections_Generic_EqualityComparer_get_Default,
NI_System_Buffers_Binary_BinaryPrimitives_ReverseEndianness,
NI_System_GC_KeepAlive,
NI_System_Type_get_IsValueType,
NI_System_Type_get_IsClass,
NI_System_Type_get_IsPrimitive,

#ifdef FEATURE_HW_INTRINSICS
NI_IsSupported_True,
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,9 @@ private CorInfoInitClassResult initClass(CORINFO_FIELD_STRUCT_* field, CORINFO_M
TypeDesc typeOfRuntimeType = _compilation.GetTypeOfRuntimeType();
return typeOfRuntimeType != null ? ObjectToHandle(typeOfRuntimeType) : null;

case CorInfoClassId.CLASSID_ENUM:
return ObjectToHandle(_compilation.TypeSystemContext.GetWellKnownType(WellKnownType.Enum));

default:
throw new NotImplementedException();
}
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/src/tools/Common/JitInterface/CorInfoTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ public enum CorInfoClassId
CLASSID_STRING,
CLASSID_ARGUMENT_HANDLE,
CLASSID_RUNTIME_TYPE,
CLASSID_ENUM,
}
public enum CorInfoInline
{
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/src/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4310,6 +4310,9 @@ CORINFO_CLASS_HANDLE CEEInfo::getBuiltinClass(CorInfoClassId classId)
case CLASSID_RUNTIME_TYPE:
result = CORINFO_CLASS_HANDLE(g_pRuntimeTypeClass);
break;
case CLASSID_ENUM:
result = CORINFO_CLASS_HANDLE(g_pEnumClass);
break;
default:
_ASSERTE(!"NYI: unknown classId");
break;
Expand Down
Loading