Skip to content

Commit

Permalink
JIT: Change VN's representation for phi definitions (dotnet#105198)
Browse files Browse the repository at this point in the history
Replace `VNF_PhiDef` and `VNF_MemoryPhiDef` by new explicit
representations that represent all phi args directly.
  • Loading branch information
jakobbotsch authored Jul 23, 2024
1 parent 98b165d commit 7d86f88
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 253 deletions.
31 changes: 15 additions & 16 deletions src/coreclr/jit/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5162,24 +5162,13 @@ bool Compiler::optVNIsLoopInvariant(ValueNum vn, FlowGraphNaturalLoop* loop, VNS
return previousRes;
}

bool res = true;
VNFuncApp funcApp;
bool res = true;
VNFuncApp funcApp;
VNPhiDef phiDef;
VNMemoryPhiDef memoryPhiDef;
if (vnStore->GetVNFunc(vn, &funcApp))
{
if (funcApp.m_func == VNF_PhiDef)
{
// Is the definition within the loop? If so, is not loop-invariant.
unsigned lclNum = funcApp.m_args[0];
unsigned ssaNum = funcApp.m_args[1];
LclSsaVarDsc* ssaDef = lvaTable[lclNum].GetPerSsaData(ssaNum);
res = !loop->ContainsBlock(ssaDef->GetBlock());
}
else if (funcApp.m_func == VNF_PhiMemoryDef)
{
BasicBlock* defnBlk = reinterpret_cast<BasicBlock*>(vnStore->ConstantValue<ssize_t>(funcApp.m_args[0]));
res = !loop->ContainsBlock(defnBlk);
}
else if (funcApp.m_func == VNF_MemOpaque)
if (funcApp.m_func == VNF_MemOpaque)
{
const unsigned loopIndex = funcApp.m_args[0];

Expand Down Expand Up @@ -5239,6 +5228,16 @@ bool Compiler::optVNIsLoopInvariant(ValueNum vn, FlowGraphNaturalLoop* loop, VNS
}
}
}
else if (vnStore->GetPhiDef(vn, &phiDef))
{
// Is the definition within the loop? If so, is not loop-invariant.
LclSsaVarDsc* ssaDef = lvaTable[phiDef.LclNum].GetPerSsaData(phiDef.SsaDef);
res = !loop->ContainsBlock(ssaDef->GetBlock());
}
else if (vnStore->GetMemoryPhiDef(vn, &memoryPhiDef))
{
res = !loop->ContainsBlock(memoryPhiDef.Block);
}

loopVnInvariantCache->Set(vn, res);
return res;
Expand Down
14 changes: 6 additions & 8 deletions src/coreclr/jit/redundantbranchopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1398,8 +1398,8 @@ bool Compiler::optJumpThreadPhi(BasicBlock* block, GenTree* tree, ValueNum treeN
for (int i = 0; i < 2; i++)
{
const ValueNum phiDefVN = treeNormVNFuncApp.m_args[i];
VNFuncApp phiDefFuncApp;
if (!vnStore->GetVNFunc(phiDefVN, &phiDefFuncApp) || (phiDefFuncApp.m_func != VNF_PhiDef))
VNPhiDef phiDef;
if (!vnStore->GetPhiDef(phiDefVN, &phiDef))
{
// This input is not a phi def. If it's a func app it might depend on
// transitively on a phi def; consider a general search utility.
Expand All @@ -1409,12 +1409,10 @@ bool Compiler::optJumpThreadPhi(BasicBlock* block, GenTree* tree, ValueNum treeN

// The PhiDef args tell us which local and which SSA def of that local.
//
assert(phiDefFuncApp.m_arity == 3);
const unsigned lclNum = unsigned(phiDefFuncApp.m_args[0]);
const unsigned ssaDefNum = unsigned(phiDefFuncApp.m_args[1]);
const ValueNum phiVN = ValueNum(phiDefFuncApp.m_args[2]);
JITDUMP("... JT-PHI [interestingVN] in " FMT_BB " relop %s operand VN is PhiDef for V%02u:%u " FMT_VN "\n",
block->bbNum, i == 0 ? "first" : "second", lclNum, ssaDefNum, phiVN);
const unsigned lclNum = phiDef.LclNum;
const unsigned ssaDefNum = phiDef.SsaDef;
JITDUMP("... JT-PHI [interestingVN] in " FMT_BB " relop %s operand VN is PhiDef for V%02u\n", block->bbNum,
i == 0 ? "first" : "second", lclNum, ssaDefNum);
if (!foundPhiDef)
{
DISPTREE(tree);
Expand Down
Loading

0 comments on commit 7d86f88

Please sign in to comment.