未验证 提交 4544bddc 编写于 作者: E Egor Bogatov 提交者: GitHub

NativeAOT: devirtualize isinst/castclass for monomorphic cases (#80831)

上级 f429780c
......@@ -5716,7 +5716,30 @@ GenTree* Compiler::impCastClassOrIsInstToTree(
// Check legality only if an inline expansion is desirable.
if (shouldExpandInline)
{
if (isCastClass)
CORINFO_CLASS_HANDLE actualImplCls = NO_CLASS_HANDLE;
if (this->IsTargetAbi(CORINFO_NATIVEAOT_ABI) &&
((helper == CORINFO_HELP_ISINSTANCEOFINTERFACE) || (helper == CORINFO_HELP_CHKCASTINTERFACE)) &&
(info.compCompHnd->getExactClasses(pResolvedToken->hClass, 1, &actualImplCls) == 1) &&
(actualImplCls != NO_CLASS_HANDLE) && impIsClassExact(actualImplCls))
{
// if an interface has a single implementation on NativeAOT where we won't load new types,
// we can assume that our object is always of that implementation's type, e.g.:
//
// var case1 = obj is IMyInterface;
// var case2 = (IMyInterface)obj;
//
// can be optimized to:
//
// var case1 = o is not null && o.GetType() == typeof(MyInterfaceImpl);
// var case2 = (o is null || o.GetType() == typeof(MyInterfaceImpl)) ? o : HELPER_CALL(o);
//
canExpandInline = true;
exactCls = actualImplCls;
JITDUMP("'%s' interface has a single implementation - '%s', using that to inline isinst/castclass.",
eeGetClassName(pResolvedToken->hClass), eeGetClassName(actualImplCls));
}
else if (isCastClass)
{
// Jit can only inline expand CHKCASTCLASS and CHKCASTARRAY helpers.
canExpandInline = (helper == CORINFO_HELP_CHKCASTCLASS) || (helper == CORINFO_HELP_CHKCASTARRAY);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册