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

Avoid additional local created for delegate invocations #63796

Merged
merged 3 commits into from
Jan 21, 2022
Merged
Changes from all commits
Commits
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
27 changes: 11 additions & 16 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3792,32 +3792,29 @@ GenTree* Lowering::LowerDelegateInvoke(GenTreeCall* call)

assert(thisArgNode != nullptr);
assert(thisArgNode->gtOper == GT_PUTARG_REG);
GenTree* originalThisExpr = thisArgNode->AsOp()->gtOp1;
GenTree* thisExpr = originalThisExpr;
GenTree* thisExpr = thisArgNode->AsOp()->gtOp1;

// We're going to use the 'this' expression multiple times, so make a local to copy it.

unsigned lclNum;

if (call->IsTailCallViaJitHelper() && originalThisExpr->IsLocal())
GenTree* base;
if (thisExpr->OperIs(GT_LCL_VAR))
{
// For ordering purposes for the special tailcall arguments on x86, we forced the
// 'this' pointer in this case to a local in Compiler::fgMorphTailCall().
// We could possibly use this case to remove copies for all architectures and non-tailcall
// calls by creating a new lcl var or lcl field reference, as is done in the
// LowerVirtualVtableCall() code.
assert(originalThisExpr->OperGet() == GT_LCL_VAR);
lclNum = originalThisExpr->AsLclVarCommon()->GetLclNum();
base = comp->gtNewLclvNode(thisExpr->AsLclVar()->GetLclNum(), thisExpr->TypeGet());
}
else if (thisExpr->OperIs(GT_LCL_FLD))
{
base = comp->gtNewLclFldNode(thisExpr->AsLclFld()->GetLclNum(), thisExpr->TypeGet(),
thisExpr->AsLclFld()->GetLclOffs());
}
else
{
unsigned delegateInvokeTmp = comp->lvaGrabTemp(true DEBUGARG("delegate invoke call"));
base = comp->gtNewLclvNode(delegateInvokeTmp, thisExpr->TypeGet());

LIR::Use thisExprUse(BlockRange(), &thisArgNode->AsOp()->gtOp1, thisArgNode);
ReplaceWithLclVar(thisExprUse, delegateInvokeTmp);

thisExpr = thisExprUse.Def(); // it's changed; reload it.
lclNum = delegateInvokeTmp;
}

// replace original expression feeding into thisPtr with
Expand All @@ -3836,8 +3833,6 @@ GenTree* Lowering::LowerDelegateInvoke(GenTreeCall* call)
// the control target is
// [originalThis + firstTgtOffs]

GenTree* base = new (comp, GT_LCL_VAR) GenTreeLclVar(GT_LCL_VAR, originalThisExpr->TypeGet(), lclNum);

unsigned targetOffs = comp->eeGetEEInfo()->offsetOfDelegateFirstTarget;
GenTree* result = new (comp, GT_LEA) GenTreeAddrMode(TYP_REF, base, nullptr, 0, targetOffs);
GenTree* callTarget = Ind(result);
Expand Down Expand Up @@ -4606,7 +4601,7 @@ GenTree* Lowering::LowerVirtualVtableCall(GenTreeCall* call)
// If what we are passing as the thisptr is not already a local, make a new local to place it in
// because we will be creating expressions based on it.
unsigned lclNum;
if (thisPtr->IsLocal())
if (thisPtr->OperIsLocal())
{
lclNum = thisPtr->AsLclVarCommon()->GetLclNum();
}
Expand Down