Skip to content

Commit

Permalink
Fold obj.GetType to a constant (#87101)
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo authored Jun 5, 2023
1 parent 18c6495 commit 404aede
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17888,6 +17888,7 @@ CORINFO_CLASS_HANDLE Compiler::gtGetClassHandle(GenTree* tree, bool* pIsExact, b
{
// if we managed to get a class handle it's definitely not null
*pIsNonNull = true;
*pIsExact = true;
}
}
break;
Expand Down
17 changes: 17 additions & 0 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11393,6 +11393,23 @@ void Compiler::fgValueNumberIntrinsic(GenTree* tree)
else
{
assert(intrinsic->gtIntrinsicName == NI_System_Object_GetType);

// Try to fold obj.GetType() if we know the exact type of obj.
bool isExact = false;
bool isNonNull = false;
CORINFO_CLASS_HANDLE cls = gtGetClassHandle(tree->gtGetOp1(), &isExact, &isNonNull);
if ((cls != NO_CLASS_HANDLE) && isExact && isNonNull)
{
CORINFO_OBJECT_HANDLE typeObj = info.compCompHnd->getRuntimeTypePointer(cls);
if (typeObj != nullptr)
{
setMethodHasFrozenObjects();
ValueNum handleVN = vnStore->VNForHandle((ssize_t)typeObj, GTF_ICON_OBJ_HDL);
intrinsic->gtVNPair = vnStore->VNPWithExc(ValueNumPair(handleVN, handleVN), arg0VNPx);
return;
}
}

intrinsic->gtVNPair =
vnStore->VNPWithExc(vnStore->VNPairForFunc(intrinsic->TypeGet(), VNF_ObjGetType, arg0VNP), arg0VNPx);
}
Expand Down

0 comments on commit 404aede

Please sign in to comment.