Skip to content

Commit

Permalink
Drop cast helper call for (TTo)obj.MemberwiseClone()
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo committed Nov 29, 2020
1 parent 15f7a79 commit 43282cd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public partial class Object
// object. This is always a shallow copy of the instance. The method is protected
// so that other object may only call this method on themselves. It is intended to
// support the ICloneable interface.
[Intrinsic]
protected unsafe object MemberwiseClone()
{
object clone = RuntimeHelpers.AllocateUninitializedClone(this);
Expand Down
7 changes: 7 additions & 0 deletions src/coreclr/src/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4789,6 +4789,13 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
result = NI_System_Type_IsAssignableTo;
}
}
else if (strcmp(className, "Object") == 0)
{
if (strcmp(methodName, "MemberwiseClone") == 0)
{
result = NI_System_Object_MemberwiseClone;
}
}
}
else if (strcmp(namespaceName, "System.Threading") == 0)
{
Expand Down
24 changes: 24 additions & 0 deletions src/coreclr/src/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9062,6 +9062,30 @@ GenTree* Compiler::fgMorphCall(GenTreeCall* call)
call->gtCallArgs->SetNode(dest);
}

if (call->gtCallMethHnd == eeFindHelper(CORINFO_HELP_CHKCASTARRAY)) // not only array?
{
GenTree* arg0 = call->gtCallArgs->GetNode();
GenTree* arg1 = call->gtCallArgs->GetNext()->GetNode();

if (arg0->OperIs(GT_CNS_INT) && arg1->IsCall() &&
(lookupNamedIntrinsic(arg1->AsCall()->gtCallMethHnd) == NI_System_Object_MemberwiseClone))
{
CORINFO_CLASS_HANDLE castToHandle = (CORINFO_CLASS_HANDLE)arg0->AsIntCon()->IconValue();

bool isExact = false;
bool isNonNull = false;
CORINFO_CLASS_HANDLE castFromHandle =
gtGetClassHandle(arg1->AsCall()->gtCallThisArg->GetNode(), &isExact, &isNonNull);

if (castToHandle != NO_CLASS_HANDLE &&
castFromHandle != NO_CLASS_HANDLE &&
info.compCompHnd->compareTypesForCast(castFromHandle, castToHandle) == TypeCompareState::Must) // or canCast() ?
{
return fgMorphTree(arg1);
}
}
}

/* Process the "normal" argument list */
call = fgMorphArgs(call);
noway_assert(call->gtOper == GT_CALL);
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/src/jit/namedintrinsiclist.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ enum NamedIntrinsic : unsigned short
NI_System_Type_get_IsValueType,
NI_System_Type_IsAssignableFrom,
NI_System_Type_IsAssignableTo,
NI_System_Object_MemberwiseClone,

// These are used by HWIntrinsics but are defined more generally
// to allow dead code optimization and handle the recursion case
Expand Down

0 comments on commit 43282cd

Please sign in to comment.