Rewrite another SSAUpdater function to avoid recursion.

llvm-svn: 100147
This commit is contained in:
Bob Wilson 2010-04-01 23:05:58 +00:00
parent 5342ddaadf
commit 37b73d9d3e
2 changed files with 50 additions and 36 deletions

View File

@ -108,8 +108,8 @@ private:
void FindPHIPlacement(BasicBlock *BB, BBInfo *Info, bool &Changed, void FindPHIPlacement(BasicBlock *BB, BBInfo *Info, bool &Changed,
unsigned Counter); unsigned Counter);
void FindAvailableVal(BasicBlock *BB, BBInfo *Info, unsigned Counter); void FindAvailableVal(BasicBlock *BB, BBInfo *Info, unsigned Counter);
void FindExistingPHI(BasicBlock *BB, BBInfo *Info); void FindExistingPHI(BasicBlock *BB);
bool CheckIfPHIMatches(BasicBlock *BB, BBInfo *Info, Value *Val); bool CheckIfPHIMatches(PHINode *PHI);
void RecordMatchingPHI(PHINode *PHI); void RecordMatchingPHI(PHINode *PHI);
void ClearPHITags(PHINode *PHI); void ClearPHITags(PHINode *PHI);

View File

@ -366,7 +366,7 @@ void SSAUpdater::FindAvailableVal(BasicBlock *BB, BBInfo *Info,
PHINode *NewPHI = 0; PHINode *NewPHI = 0;
if (Info->DefBB == BB) { if (Info->DefBB == BB) {
// Look for an existing PHI. // Look for an existing PHI.
FindExistingPHI(BB, Info); FindExistingPHI(BB);
if (!Info->AvailableVal) { if (!Info->AvailableVal) {
NewPHI = PHINode::Create(PrototypeValue->getType(), NewPHI = PHINode::Create(PrototypeValue->getType(),
PrototypeValue->getName(), &BB->front()); PrototypeValue->getName(), &BB->front());
@ -401,11 +401,11 @@ void SSAUpdater::FindAvailableVal(BasicBlock *BB, BBInfo *Info,
/// FindExistingPHI - Look through the PHI nodes in a block to see if any of /// FindExistingPHI - Look through the PHI nodes in a block to see if any of
/// them match what is needed. /// them match what is needed.
void SSAUpdater::FindExistingPHI(BasicBlock *BB, BBInfo *Info) { void SSAUpdater::FindExistingPHI(BasicBlock *BB) {
PHINode *SomePHI; PHINode *SomePHI;
for (BasicBlock::iterator It = BB->begin(); for (BasicBlock::iterator It = BB->begin();
(SomePHI = dyn_cast<PHINode>(It)); ++It) { (SomePHI = dyn_cast<PHINode>(It)); ++It) {
if (CheckIfPHIMatches(BB, Info, SomePHI)) { if (CheckIfPHIMatches(SomePHI)) {
RecordMatchingPHI(SomePHI); RecordMatchingPHI(SomePHI);
break; break;
} }
@ -413,40 +413,54 @@ void SSAUpdater::FindExistingPHI(BasicBlock *BB, BBInfo *Info) {
} }
} }
/// CheckIfPHIMatches - Check if Val is a PHI node in block BB that matches /// CheckIfPHIMatches - Check if a PHI node matches the placement and values
/// the placement and values in the BBMap. /// in the BBMap.
bool SSAUpdater::CheckIfPHIMatches(BasicBlock *BB, BBInfo *Info, Value *Val) { bool SSAUpdater::CheckIfPHIMatches(PHINode *PHI) {
if (Info->AvailableVal)
return Val == Info->AvailableVal;
// Check if Val is a PHI in this block.
PHINode *PHI = dyn_cast<PHINode>(Val);
if (!PHI || PHI->getParent() != BB)
return false;
// If this block has already been visited, check if this PHI matches.
if (Info->PHITag)
return PHI == Info->PHITag;
Info->PHITag = PHI;
bool IsMatch = true;
// Iterate through the predecessors.
BBMapTy *BBMap = getBBMap(BM); BBMapTy *BBMap = getBBMap(BM);
for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) { SmallVector<PHINode*, 20> WorkList;
BasicBlock *Pred = PHI->getIncomingBlock(i); WorkList.push_back(PHI);
Value *IncomingVal = PHI->getIncomingValue(i);
BBInfo *PredInfo = (*BBMap)[Pred]; // Mark that the block containing this PHI has been visited.
// Skip to the nearest preceding definition. (*BBMap)[PHI->getParent()]->PHITag = PHI;
if (PredInfo->DefBB != Pred) {
Pred = PredInfo->DefBB; while (!WorkList.empty()) {
PredInfo = (*BBMap)[Pred]; PHI = WorkList.pop_back_val();
}
if (!CheckIfPHIMatches(Pred, PredInfo, IncomingVal)) { // Iterate through the PHI's incoming values.
IsMatch = false; for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) {
break; Value *IncomingVal = PHI->getIncomingValue(i);
BasicBlock *Pred = PHI->getIncomingBlock(i);
BBInfo *PredInfo = (*BBMap)[Pred];
// Skip to the nearest preceding definition.
if (PredInfo->DefBB != Pred) {
Pred = PredInfo->DefBB;
PredInfo = (*BBMap)[Pred];
}
// Check if it matches the expected value.
if (PredInfo->AvailableVal) {
if (IncomingVal == PredInfo->AvailableVal)
continue;
return false;
}
// Check if the value is a PHI in the correct block.
PHINode *IncomingPHIVal = dyn_cast<PHINode>(IncomingVal);
if (!IncomingPHIVal || IncomingPHIVal->getParent() != Pred)
return false;
// If this block has already been visited, check if this PHI matches.
if (PredInfo->PHITag) {
if (IncomingPHIVal == PredInfo->PHITag)
continue;
return false;
}
PredInfo->PHITag = IncomingPHIVal;
WorkList.push_back(IncomingPHIVal);
} }
} }
return IsMatch; return true;
} }
/// RecordMatchingPHI - For a PHI node that matches, record it and its input /// RecordMatchingPHI - For a PHI node that matches, record it and its input