Skip to content

Commit

Permalink
RyuJIT: Don't emit cast helpers for (T)array.Clone() (#45311)
Browse files Browse the repository at this point in the history
Note that Array.Clone()'s return type is the same as the type of its first operand.

This allows optimization of casts on the result of calls to Clone().
  • Loading branch information
EgorBo authored Dec 17, 2020
1 parent f8a83c8 commit acd4855
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17757,6 +17757,14 @@ CORINFO_CLASS_HANDLE Compiler::gtGetClassHandle(GenTree* tree, bool* pIsExact, b
case GT_CALL:
{
GenTreeCall* call = tree->AsCall();
if (call->gtFlags & CORINFO_FLG_JIT_INTRINSIC)
{
if (lookupNamedIntrinsic(call->gtCallMethHnd) == NI_System_Array_Clone)
{
objClass = gtGetClassHandle(call->gtCallThisArg->GetNode(), pIsExact, pIsNonNull);
break;
}
}
if (call->IsInlineCandidate())
{
// For inline candidates, we've already cached the return
Expand Down
7 changes: 7 additions & 0 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4809,6 +4809,13 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method)
result = NI_System_GC_KeepAlive;
}
}
else if (strcmp(className, "Array") == 0)
{
if (strcmp(methodName, "Clone") == 0)
{
result = NI_System_Array_Clone;
}
}
else if (strcmp(className, "Type") == 0)
{
if (strcmp(methodName, "get_IsValueType") == 0)
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/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_Array_Clone,

// These are used by HWIntrinsics but are defined more generally
// to allow dead code optimization and handle the recursion case
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ void IList.RemoveAt(int index)

// Make a new array which is a shallow copy of the original array.
//
[Intrinsic]
public object Clone()
{
return MemberwiseClone();
Expand Down

0 comments on commit acd4855

Please sign in to comment.