Skip to content

Commit

Permalink
Add support for "whole" stores
Browse files Browse the repository at this point in the history
  • Loading branch information
SingleAccretion committed Dec 16, 2022
1 parent 719146b commit db187d2
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10361,7 +10361,7 @@ PhaseStatus Compiler::optVNBasedDeadStoreRemoval()

LclVarDsc* varDsc = lvaGetDesc(lclNum);
unsigned defCount = varDsc->lvPerSsaData.GetCount();
if (defCount <= 2)
if (defCount <= 1)
{
continue;
}
Expand All @@ -10378,22 +10378,53 @@ PhaseStatus Compiler::optVNBasedDeadStoreRemoval()
JITDUMP("Considering [%06u] for removal...\n", dspTreeID(store));

GenTree* lhs = store->gtGetOp1();
if (!lhs->OperIs(GT_LCL_FLD) || ((lhs->gtFlags & GTF_VAR_USEASG) == 0) ||
(lhs->AsLclFld()->GetLclNum() != lclNum))
if (lhs->AsLclVarCommon()->GetLclNum() != lclNum)
{
JITDUMP(" -- no; composite definition\n");
continue;
}

ValueNum oldLclValue = varDsc->GetPerSsaData(defDsc->GetUseDefSsaNum())->m_vnPair.GetConservative();
ValueNum oldStoreValue =
vnStore->VNForLoad(VNK_Conservative, oldLclValue, lvaLclExactSize(lclNum), lhs->TypeGet(),
lhs->AsLclFld()->GetLclOffs(), lhs->AsLclFld()->GetSize());
ValueNum oldStoreValue;
if ((lhs->gtFlags & GTF_VAR_USEASG) == 0)
{
LclSsaVarDsc* lastDefDsc = varDsc->lvPerSsaData.GetSsaDefByIndex(defIndex - 1);
if (lastDefDsc->GetBlock() != defDsc->GetBlock())
{
JITDUMP(" -- no; last def not in the same block\n");
continue;
}

if ((lhs->gtFlags & GTF_VAR_EXPLICIT_INIT) != 0)
{
// Removing explicit inits is not profitable for primitives and not safe for structs.
JITDUMP(" -- no; 'explicit init'\n");
continue;
}

// CQ heuristic: avoid removing defs of enregisterable locals where this is likely to
// make them "must-init", extending live ranges. Here we assume the first SSA def was
// the implicit "live-in" one, which is not guaranteed, but very likely.
if ((defIndex == 1) && (varDsc->TypeGet() != TYP_STRUCT))
{
JITDUMP(" -- no; first explicit def of a non-STRUCT local\n", lclNum);
continue;
}

oldStoreValue = lastDefDsc->m_vnPair.GetConservative();
}
else
{
ValueNum oldLclValue = varDsc->GetPerSsaData(defDsc->GetUseDefSsaNum())->m_vnPair.GetConservative();
oldStoreValue =
vnStore->VNForLoad(VNK_Conservative, oldLclValue, lvaLclExactSize(lclNum), lhs->TypeGet(),
lhs->AsLclFld()->GetLclOffs(), lhs->AsLclFld()->GetSize());
}

GenTree* rhs = store->gtGetOp2();
ValueNum storeValue;
if (lhs->TypeIs(TYP_STRUCT) && rhs->IsIntegralConst(0))
{
storeValue = vnStore->VNForZeroObj(lhs->AsLclFld()->GetLayout());
storeValue = vnStore->VNForZeroObj(lhs->AsLclVarCommon()->GetLayout(this));
}
else
{
Expand All @@ -10419,6 +10450,10 @@ PhaseStatus Compiler::optVNBasedDeadStoreRemoval()

madeChanges = true;
}
else
{
JITDUMP(" -- no; not redundant\n");
}
}
}
}
Expand Down

0 comments on commit db187d2

Please sign in to comment.