@@ -2614,14 +2614,16 @@ void Compiler::lvaSetVarLiveInOutOfHandler(unsigned varNum)
26142614 {
26152615 noway_assert (lvaTable[i].lvIsStructField );
26162616 lvaTable[i].lvLiveInOutOfHndlr = 1 ;
2617- if (!lvaEnregEHVars)
2617+ // For now, only enregister an EH Var if it is a single def and whose refCnt > 1.
2618+ if (!lvaEnregEHVars || !lvaTable[i].lvEhWriteThruCandidate || lvaTable[i].lvRefCnt () <= 1 )
26182619 {
26192620 lvaSetVarDoNotEnregister (i DEBUGARG (DNER_LiveInOutOfHandler));
26202621 }
26212622 }
26222623 }
26232624
2624- if (!lvaEnregEHVars)
2625+ // For now, only enregister an EH Var if it is a single def and whose refCnt > 1.
2626+ if (!lvaEnregEHVars || !varDsc->lvEhWriteThruCandidate || varDsc->lvRefCnt () <= 1 )
26252627 {
26262628 lvaSetVarDoNotEnregister (varNum DEBUGARG (DNER_LiveInOutOfHandler));
26272629 }
@@ -4040,7 +4042,7 @@ void Compiler::lvaMarkLclRefs(GenTree* tree, BasicBlock* block, Statement* stmt,
40404042
40414043 /* Record if the variable has a single def or not */
40424044
4043- if (!varDsc->lvDisqualify ) // If this variable is already disqualified we can skip this
4045+ if (!varDsc->lvDisqualify ) // If this variable is already disqualified, we can skip this
40444046 {
40454047 if (tree->gtFlags & GTF_VAR_DEF ) // Is this is a def of our variable
40464048 {
@@ -4075,6 +4077,34 @@ void Compiler::lvaMarkLclRefs(GenTree* tree, BasicBlock* block, Statement* stmt,
40754077 BlockSetOps::AddElemD (this , varDsc->lvRefBlks , block->bbNum );
40764078 }
40774079 }
4080+
4081+ if (!varDsc->lvDisqualifyForEhWriteThru ) // If this EH var already disqualified, we can skip this
4082+ {
4083+ if (tree->gtFlags & GTF_VAR_DEF ) // Is this is a def of our variable
4084+ {
4085+ bool bbInALoop = (block->bbFlags & BBF_BACKWARD_JUMP ) != 0 ;
4086+ bool bbIsReturn = block->bbJumpKind == BBJ_RETURN ;
4087+ bool needsExplicitZeroInit = fgVarNeedsExplicitZeroInit (lclNum, bbInALoop, bbIsReturn);
4088+
4089+ if (varDsc->lvEhWriteThruCandidate || needsExplicitZeroInit)
4090+ {
4091+ varDsc->lvEhWriteThruCandidate = false ;
4092+ varDsc->lvDisqualifyForEhWriteThru = true ;
4093+ }
4094+ else
4095+ {
4096+ #if FEATURE_PARTIAL_SIMD_CALLEE_SAVE
4097+ // TODO-CQ: If the varType needs partial callee save, conservatively do not enregister
4098+ // such variable. In future, need to enable enregisteration for such variables.
4099+ if (!varTypeNeedsPartialCalleeSave (varDsc->lvType ))
4100+ #endif
4101+ {
4102+ varDsc->lvEhWriteThruCandidate = true ;
4103+ }
4104+ }
4105+ }
4106+ }
4107+
40784108#endif // ASSERTION_PROP
40794109
40804110 bool allowStructs = false ;
@@ -4178,6 +4208,8 @@ void Compiler::lvaMarkLocalVars(BasicBlock* block, bool isRecompute)
41784208
41794209 Compiler::fgWalkResult PreOrderVisit (GenTree** use, GenTree* user)
41804210 {
4211+ // TODO: Stop passing isRecompute once we are sure that this assert is never hit.
4212+ assert (!m_isRecompute);
41814213 m_compiler->lvaMarkLclRefs (*use, m_block, m_stmt, m_isRecompute);
41824214 return WALK_CONTINUE ;
41834215 }
@@ -4437,7 +4469,13 @@ void Compiler::lvaComputeRefCounts(bool isRecompute, bool setSlotNumbers)
44374469
44384470 // Set initial value for lvSingleDef for explicit and implicit
44394471 // argument locals as they are "defined" on entry.
4440- varDsc->lvSingleDef = varDsc->lvIsParam ;
4472+ // However, if we are just recomputing the ref counts, retain the value
4473+ // that was set by past phases.
4474+ if (!isRecompute)
4475+ {
4476+ varDsc->lvSingleDef = varDsc->lvIsParam ;
4477+ varDsc->lvEhWriteThruCandidate = varDsc->lvIsParam ;
4478+ }
44414479 }
44424480
44434481 // Remember current state of generic context use, and prepare
@@ -7194,7 +7232,7 @@ void Compiler::lvaDumpFrameLocation(unsigned lclNum)
71947232 baseReg = EBPbased ? REG_FPBASE : REG_SPBASE ;
71957233#endif
71967234
7197- printf (" [%2s%1s0x%02X ] " , getRegName (baseReg), (offset < 0 ? " -" : " +" ), (offset < 0 ? -offset : offset));
7235+ printf (" [%2s%1s%02XH ] " , getRegName (baseReg), (offset < 0 ? " -" : " +" ), (offset < 0 ? -offset : offset));
71987236}
71997237
72007238/* ****************************************************************************
0 commit comments