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

Value num refine phis #104752

Merged
merged 8 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 18 additions & 3 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1466,9 +1466,24 @@ AssertionIndex Compiler::optCreateAssertion(GenTree* op1,
assertion.op1.vn = optConservativeNormalVN(op1);
assertion.op1.lcl.ssaNum = op1->AsLclVarCommon()->GetSsaNum();

assert((assertion.op1.lcl.ssaNum == SsaConfig::RESERVED_SSA_NUM) ||
(assertion.op1.vn == vnStore->VNConservativeNormalValue(
lvaGetDesc(lclNum)->GetPerSsaData(assertion.op1.lcl.ssaNum)->m_vnPair)));
#ifdef DEBUG

// If we're ssa based, check that the VN is reasonable.
//
if (assertion.op1.lcl.ssaNum != SsaConfig::RESERVED_SSA_NUM)
{
LclSsaVarDsc* const ssaDsc = lvaGetDesc(lclNum)->GetPerSsaData(assertion.op1.lcl.ssaNum);

bool doesVNMatch = (assertion.op1.vn == vnStore->VNConservativeNormalValue(ssaDsc->m_vnPair));

if (!doesVNMatch && ssaDsc->m_updated)
{
doesVNMatch = (assertion.op1.vn == vnStore->VNConservativeNormalValue(ssaDsc->m_origVNPair));
}

assert(doesVNMatch);
}
#endif

ssize_t cnsValue = 0;
GenTreeFlags iconFlags = GTF_EMPTY;
Expand Down
13 changes: 13 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ class LclSsaVarDsc
}

ValueNumPair m_vnPair;

#ifdef DEBUG
// True if this ssa def VN was updated
bool m_updated = false;
// Originally assigned VN
ValueNumPair m_origVNPair;
#endif
};

// This class stores information associated with a memory SSA definition.
Expand Down Expand Up @@ -5701,9 +5708,15 @@ class Compiler

// Utility functions for fgValueNumber.

// Value number a block or blocks in a loop
void fgValueNumberBlocks(BasicBlock* block, BlockSet& visitedBlocks);

// Perform value-numbering for the trees in "blk".
void fgValueNumberBlock(BasicBlock* blk);

// Value number a phi definition
void fgValueNumberPhiDef(GenTreeLclVar* newSsaDef, BasicBlock* block, bool isUpdate = false);

// Requires that "entryBlock" is the header block of "loop" and that "loop" is the
// innermost loop of which "entryBlock" is the entry. Returns the value number that should be
// assumed for the memoryKind at the start "entryBlk".
Expand Down
36 changes: 26 additions & 10 deletions src/coreclr/jit/copyprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,25 @@ bool Compiler::optCopyProp(
assert((tree->gtFlags & GTF_VAR_DEF) == 0);
assert(tree->GetLclNum() == lclNum);

bool madeChanges = false;
LclVarDsc* varDsc = lvaGetDesc(lclNum);
ValueNum lclDefVN = varDsc->GetPerSsaData(tree->GetSsaNum())->m_vnPair.GetConservative();
bool madeChanges = false;
LclVarDsc* const varDsc = lvaGetDesc(lclNum);
LclSsaVarDsc* const varSsaDsc = varDsc->GetPerSsaData(tree->GetSsaNum());
GenTree* const varDefTree = varSsaDsc->GetDefNode();
BasicBlock* const varDefBlock = varSsaDsc->GetBlock();
ValueNum const lclDefVN = varSsaDsc->m_vnPair.GetConservative();
assert(lclDefVN != ValueNumStore::NoVN);

// See if this local is a candidate for phi dev equivalence checks
//
bool const varDefTreeIsPhiDef = (varDefTree != nullptr) && varDefTree->IsPhiDefn();
bool varDefTreeIsPhiDefAtCycleEntry = false;

if (varDefTreeIsPhiDef)
{
FlowGraphNaturalLoop* const loop = m_blockToLoop->GetLoop(varDefBlock);
varDefTreeIsPhiDefAtCycleEntry = (loop != nullptr) && (loop->GetHeader() == varDefBlock);
}

for (LclNumToLiveDefsMap::Node* const iter : LclNumToLiveDefsMap::KeyValueIteration(curSsaName))
{
unsigned newLclNum = iter->GetKey();
Expand All @@ -190,7 +204,15 @@ bool Compiler::optCopyProp(

if (newLclDefVN != lclDefVN)
{
continue;
bool arePhiDefsEquivalent =
varDefTreeIsPhiDefAtCycleEntry && vnStore->AreVNsEquivalent(lclDefVN, newLclDefVN);
if (!arePhiDefsEquivalent)
{
continue;
}

JITDUMP("orig [%06] copy [%06u] VNs proved equivalent\n", dspTreeID(tree),
dspTreeID(newLclDef.GetDefNode()));
}

// It may not be profitable to propagate a 'doNotEnregister' lclVar to an existing use of an
Expand Down Expand Up @@ -334,12 +356,6 @@ void Compiler::optCopyPropPushDef(GenTree* defNode, GenTreeLclVarCommon* lclNode
else if (lclNode->HasSsaName())
{
unsigned ssaNum = lclNode->GetSsaNum();
if ((defNode != nullptr) && defNode->IsPhiDefn())
{
// TODO-CQ: design better heuristics for propagation and remove this.
ssaNum = SsaConfig::RESERVED_SSA_NUM;
}

pushDef(lclNum, ssaNum);
}
}
Expand Down
Loading
Loading