Skip to content

Commit f455188

Browse files
authored
Value num refine phis (#104752)
VN doesn't always give PHI defs the best possible values (in particular if there are backedge PHI args). Revise VN to run intg a "loop-respecting" RPO where we don't visit any loop successors until all loop blocks have been visited. Once the loop is done, update the header PHI VNs since all PHI arg VNs are now known. Then look for equivalent PHI defs in copy prop and enable copy prop when two locals have the same values at the head of a loop. Addresses the regression case noted in #95645 (comment) where cross-block morph's copy prop plus loop bottom testing has created some unnecessary loop-carried values. Closes #95645.
1 parent 4f38f92 commit f455188

File tree

6 files changed

+449
-136
lines changed

6 files changed

+449
-136
lines changed

src/coreclr/jit/assertionprop.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,9 +1466,24 @@ AssertionIndex Compiler::optCreateAssertion(GenTree* op1,
14661466
assertion.op1.vn = optConservativeNormalVN(op1);
14671467
assertion.op1.lcl.ssaNum = op1->AsLclVarCommon()->GetSsaNum();
14681468

1469-
assert((assertion.op1.lcl.ssaNum == SsaConfig::RESERVED_SSA_NUM) ||
1470-
(assertion.op1.vn == vnStore->VNConservativeNormalValue(
1471-
lvaGetDesc(lclNum)->GetPerSsaData(assertion.op1.lcl.ssaNum)->m_vnPair)));
1469+
#ifdef DEBUG
1470+
1471+
// If we're ssa based, check that the VN is reasonable.
1472+
//
1473+
if (assertion.op1.lcl.ssaNum != SsaConfig::RESERVED_SSA_NUM)
1474+
{
1475+
LclSsaVarDsc* const ssaDsc = lvaGetDesc(lclNum)->GetPerSsaData(assertion.op1.lcl.ssaNum);
1476+
1477+
bool doesVNMatch = (assertion.op1.vn == vnStore->VNConservativeNormalValue(ssaDsc->m_vnPair));
1478+
1479+
if (!doesVNMatch && ssaDsc->m_updated)
1480+
{
1481+
doesVNMatch = (assertion.op1.vn == vnStore->VNConservativeNormalValue(ssaDsc->m_origVNPair));
1482+
}
1483+
1484+
assert(doesVNMatch);
1485+
}
1486+
#endif
14721487

14731488
ssize_t cnsValue = 0;
14741489
GenTreeFlags iconFlags = GTF_EMPTY;

src/coreclr/jit/compiler.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,13 @@ class LclSsaVarDsc
312312
}
313313

314314
ValueNumPair m_vnPair;
315+
316+
#ifdef DEBUG
317+
// True if this ssa def VN was updated
318+
bool m_updated = false;
319+
// Originally assigned VN
320+
ValueNumPair m_origVNPair;
321+
#endif
315322
};
316323

317324
// This class stores information associated with a memory SSA definition.
@@ -5759,9 +5766,15 @@ class Compiler
57595766

57605767
// Utility functions for fgValueNumber.
57615768

5769+
// Value number a block or blocks in a loop
5770+
void fgValueNumberBlocks(BasicBlock* block, BlockSet& visitedBlocks);
5771+
57625772
// Perform value-numbering for the trees in "blk".
57635773
void fgValueNumberBlock(BasicBlock* blk);
57645774

5775+
// Value number a phi definition
5776+
void fgValueNumberPhiDef(GenTreeLclVar* newSsaDef, BasicBlock* block, bool isUpdate = false);
5777+
57655778
// Requires that "entryBlock" is the header block of "loop" and that "loop" is the
57665779
// innermost loop of which "entryBlock" is the entry. Returns the value number that should be
57675780
// assumed for the memoryKind at the start "entryBlk".

src/coreclr/jit/copyprop.cpp

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,25 @@ bool Compiler::optCopyProp(
161161
assert((tree->gtFlags & GTF_VAR_DEF) == 0);
162162
assert(tree->GetLclNum() == lclNum);
163163

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

172+
// See if this local is a candidate for phi dev equivalence checks
173+
//
174+
bool const varDefTreeIsPhiDef = (varDefTree != nullptr) && varDefTree->IsPhiDefn();
175+
bool varDefTreeIsPhiDefAtCycleEntry = false;
176+
177+
if (varDefTreeIsPhiDef)
178+
{
179+
FlowGraphNaturalLoop* const loop = m_blockToLoop->GetLoop(varDefBlock);
180+
varDefTreeIsPhiDefAtCycleEntry = (loop != nullptr) && (loop->GetHeader() == varDefBlock);
181+
}
182+
169183
for (LclNumToLiveDefsMap::Node* const iter : LclNumToLiveDefsMap::KeyValueIteration(curSsaName))
170184
{
171185
unsigned newLclNum = iter->GetKey();
@@ -190,7 +204,15 @@ bool Compiler::optCopyProp(
190204

191205
if (newLclDefVN != lclDefVN)
192206
{
193-
continue;
207+
bool arePhiDefsEquivalent =
208+
varDefTreeIsPhiDefAtCycleEntry && vnStore->AreVNsEquivalent(lclDefVN, newLclDefVN);
209+
if (!arePhiDefsEquivalent)
210+
{
211+
continue;
212+
}
213+
214+
JITDUMP("orig [%06u] copy [%06u] VNs proved equivalent\n", dspTreeID(tree),
215+
dspTreeID(newLclDef.GetDefNode()));
194216
}
195217

196218
// It may not be profitable to propagate a 'doNotEnregister' lclVar to an existing use of an
@@ -259,6 +281,24 @@ bool Compiler::optCopyProp(
259281

260282
tree->AsLclVarCommon()->SetLclNum(newLclNum);
261283
tree->AsLclVarCommon()->SetSsaNum(newSsaNum);
284+
285+
// Update VN to match, and propagate up through any enclosing commas.
286+
// (we could in principle try updating through other parents, but
287+
// we lack VN's context for memory, so can't get them all).
288+
//
289+
if (newLclDefVN != lclDefVN)
290+
{
291+
tree->SetVNs(newLclSsaDef->m_vnPair);
292+
GenTree* parent = tree->gtGetParent(nullptr);
293+
294+
while ((parent != nullptr) && parent->OperIs(GT_COMMA))
295+
{
296+
JITDUMP(" Updating COMMA parent VN [%06u]\n", dspTreeID(parent));
297+
ValueNumPair op1Xvnp = vnStore->VNPExceptionSet(parent->AsOp()->gtOp1->gtVNPair);
298+
parent->SetVNs(vnStore->VNPWithExc(parent->AsOp()->gtOp2->gtVNPair, op1Xvnp));
299+
parent = tree->gtGetParent(nullptr);
300+
}
301+
}
262302
gtUpdateSideEffects(stmt, tree);
263303
newLclSsaDef->AddUse(block);
264304

@@ -334,12 +374,6 @@ void Compiler::optCopyPropPushDef(GenTree* defNode, GenTreeLclVarCommon* lclNode
334374
else if (lclNode->HasSsaName())
335375
{
336376
unsigned ssaNum = lclNode->GetSsaNum();
337-
if ((defNode != nullptr) && defNode->IsPhiDefn())
338-
{
339-
// TODO-CQ: design better heuristics for propagation and remove this.
340-
ssaNum = SsaConfig::RESERVED_SSA_NUM;
341-
}
342-
343377
pushDef(lclNum, ssaNum);
344378
}
345379
}

src/coreclr/jit/redundantbranchopts.cpp

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,31 @@ bool Compiler::optJumpThreadCore(JumpThreadInfo& jti)
16281628
//
16291629
JITDUMP("Optimizing via jump threading\n");
16301630

1631+
bool setNoCseIn = false;
1632+
1633+
// If this is a phi-based threading, and the block we're bypassing has
1634+
// a memory phi, mark the successor blocks with BBF_NO_CSE_IN so we can
1635+
// block unsound CSE propagation.
1636+
//
1637+
if (jti.m_isPhiBased)
1638+
{
1639+
for (MemoryKind memoryKind : allMemoryKinds())
1640+
{
1641+
if ((memoryKind == ByrefExposed) && byrefStatesMatchGcHeapStates)
1642+
{
1643+
continue;
1644+
}
1645+
1646+
if (jti.m_block->bbMemorySsaPhiFunc[memoryKind] != nullptr)
1647+
{
1648+
JITDUMP(FMT_BB " has %s memory phi; will be marking blocks with BBF_NO_CSE_IN\n", jti.m_block->bbNum,
1649+
memoryKindNames[memoryKind]);
1650+
setNoCseIn = true;
1651+
break;
1652+
}
1653+
}
1654+
}
1655+
16311656
// Now reroute the flow from the predecessors.
16321657
// If this pred is in the set that will reuse block, do nothing.
16331658
// Else revise pred to branch directly to the appropriate successor of block.
@@ -1638,6 +1663,11 @@ bool Compiler::optJumpThreadCore(JumpThreadInfo& jti)
16381663
//
16391664
if (BlockSetOps::IsMember(this, jti.m_ambiguousPreds, predBlock->bbNum))
16401665
{
1666+
if (setNoCseIn && !jti.m_block->HasFlag(BBF_NO_CSE_IN))
1667+
{
1668+
JITDUMP(FMT_BB " => BBF_NO_CSE_IN\n", jti.m_block->bbNum);
1669+
jti.m_block->SetFlags(BBF_NO_CSE_IN);
1670+
}
16411671
continue;
16421672
}
16431673

@@ -1652,6 +1682,12 @@ bool Compiler::optJumpThreadCore(JumpThreadInfo& jti)
16521682
predBlock->bbNum, jti.m_block->bbNum, predBlock->bbNum, jti.m_trueTarget->bbNum);
16531683

16541684
fgReplaceJumpTarget(predBlock, jti.m_block, jti.m_trueTarget);
1685+
1686+
if (setNoCseIn && !jti.m_trueTarget->HasFlag(BBF_NO_CSE_IN))
1687+
{
1688+
JITDUMP(FMT_BB " => BBF_NO_CSE_IN\n", jti.m_trueTarget->bbNum);
1689+
jti.m_trueTarget->SetFlags(BBF_NO_CSE_IN);
1690+
}
16551691
}
16561692
else
16571693
{
@@ -1660,28 +1696,11 @@ bool Compiler::optJumpThreadCore(JumpThreadInfo& jti)
16601696
predBlock->bbNum, jti.m_block->bbNum, predBlock->bbNum, jti.m_falseTarget->bbNum);
16611697

16621698
fgReplaceJumpTarget(predBlock, jti.m_block, jti.m_falseTarget);
1663-
}
1664-
}
16651699

1666-
// If this is a phi-based threading, and the block we're bypassing has
1667-
// a memory phi, mark the block with BBF_NO_CSE_IN so we can block CSE propagation
1668-
// into the block.
1669-
//
1670-
if (jti.m_isPhiBased)
1671-
{
1672-
for (MemoryKind memoryKind : allMemoryKinds())
1673-
{
1674-
if ((memoryKind == ByrefExposed) && byrefStatesMatchGcHeapStates)
1675-
{
1676-
continue;
1677-
}
1678-
1679-
if (jti.m_block->bbMemorySsaPhiFunc[memoryKind] != nullptr)
1700+
if (setNoCseIn && !jti.m_falseTarget->HasFlag(BBF_NO_CSE_IN))
16801701
{
1681-
JITDUMP(FMT_BB " has %s memory phi; marking as BBF_NO_CSE_IN\n", jti.m_block->bbNum,
1682-
memoryKindNames[memoryKind]);
1683-
jti.m_block->SetFlags(BBF_NO_CSE_IN);
1684-
break;
1702+
JITDUMP(FMT_BB " => BBF_NO_CSE_IN\n", jti.m_falseTarget->bbNum);
1703+
jti.m_falseTarget->SetFlags(BBF_NO_CSE_IN);
16851704
}
16861705
}
16871706
}

0 commit comments

Comments
 (0)