@@ -2456,25 +2456,28 @@ void ObjectAllocator::RewriteUses()
24562456 CallArg* const thisArg = call->gtArgs .GetThisArg ();
24572457 GenTree* const delegateThis = thisArg->GetNode ();
24582458
2459- if (delegateThis->OperIs (GT_LCL_VAR))
2459+ if (delegateThis->OperIs (GT_LCL_VAR, GT_LCL_ADDR ))
24602460 {
24612461 GenTreeLclVarCommon* const lcl = delegateThis->AsLclVarCommon ();
2462+ bool const isStackAllocatedDelegate =
2463+ delegateThis->OperIs (GT_LCL_ADDR) || m_allocator->DoesLclVarPointToStack (lcl->GetLclNum ());
24622464
2463- if (m_allocator-> DoesLclVarPointToStack (lcl-> GetLclNum ()) )
2465+ if (isStackAllocatedDelegate )
24642466 {
24652467 JITDUMP (" Expanding delegate invoke [%06u]\n " , m_compiler->dspTreeID (call));
2468+
24662469 // Expand the delgate invoke early, so that physical promotion has
24672470 // a chance to promote the delegate fields.
24682471 //
24692472 // Note the instance field may also be stack allocatable (someday)
24702473 //
2471- GenTree* const cloneThis = m_compiler->gtClone (lcl);
2474+ GenTree* const cloneThis = m_compiler->gtClone (lcl, /* complexOk */ true );
24722475 unsigned const instanceOffset = m_compiler->eeGetEEInfo ()->offsetOfDelegateInstance ;
24732476 GenTree* const newThisAddr =
24742477 m_compiler->gtNewOperNode (GT_ADD, TYP_I_IMPL, cloneThis,
24752478 m_compiler->gtNewIconNode (instanceOffset, TYP_I_IMPL));
24762479
2477- // For now assume the instance is heap...
2480+ // For now assume the instance field is on the heap...
24782481 //
24792482 GenTree* const newThis = m_compiler->gtNewIndir (TYP_REF, newThisAddr);
24802483 thisArg->SetEarlyNode (newThis);
0 commit comments