diff --git a/src/coreclr/jit/lower.cpp b/src/coreclr/jit/lower.cpp index ae5a41c52e6827..197048b0f0f96b 100644 --- a/src/coreclr/jit/lower.cpp +++ b/src/coreclr/jit/lower.cpp @@ -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 @@ -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); @@ -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(); }