Make better use of the PHINode API.

Change various bits of code to make better use of the existing PHINode
API, to insulate them from forthcoming changes in how PHINodes store
their operands.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@133434 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jay Foad 2011-06-20 14:18:48 +00:00
parent 691c05bb29
commit c137120bb0
7 changed files with 34 additions and 21 deletions

View File

@ -1079,12 +1079,16 @@ static void WriteInstruction(const Instruction &I, unsigned InstID,
AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
break;
case Instruction::PHI:
case Instruction::PHI: {
const PHINode &PN = cast<PHINode>(I);
Code = bitc::FUNC_CODE_INST_PHI;
Vals.push_back(VE.getTypeID(I.getType()));
for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
Vals.push_back(VE.getValueID(I.getOperand(i)));
Vals.push_back(VE.getTypeID(PN.getType()));
for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Vals.push_back(VE.getValueID(PN.getIncomingValue(i)));
Vals.push_back(VE.getValueID(PN.getIncomingBlock(i)));
}
break;
}
case Instruction::Alloca:
Code = bitc::FUNC_CODE_INST_ALLOCA;

View File

@ -1353,9 +1353,10 @@ void CppWriter::printInstruction(const Instruction *I,
printEscapedString(phi->getName());
Out << "\", " << bbname << ");";
nl(Out);
for (unsigned i = 0; i < phi->getNumOperands(); i+=2) {
for (unsigned i = 0; i < phi->getNumIncomingValues(); ++i) {
Out << iName << "->addIncoming("
<< opNames[i] << ", " << opNames[i+1] << ");";
<< opNames[PHINode::getOperandNumForIncomingValue(i)] << ", "
<< opNames[PHINode::getOperandNumForIncomingBlock(i)] << ");";
nl(Out);
}
break;

View File

@ -1190,8 +1190,10 @@ static Value *ConstructSSAForLoadSet(LoadInst *LI,
// escaping uses to any values that are operands to these PHIs.
for (unsigned i = 0, e = NewPHIs.size(); i != e; ++i) {
PHINode *P = NewPHIs[i];
for (unsigned ii = 0, ee = P->getNumIncomingValues(); ii != ee; ++ii)
AA->addEscapingUse(P->getOperandUse(2*ii));
for (unsigned ii = 0, ee = P->getNumIncomingValues(); ii != ee; ++ii) {
unsigned jj = PHINode::getOperandNumForIncomingValue(ii);
AA->addEscapingUse(P->getOperandUse(jj));
}
}
}
@ -2149,8 +2151,11 @@ bool GVN::performPRE(Function &F) {
// Because we have added a PHI-use of the pointer value, it has now
// "escaped" from alias analysis' perspective. We need to inform
// AA of this.
for (unsigned ii = 0, ee = Phi->getNumIncomingValues(); ii != ee; ++ii)
VN.getAliasAnalysis()->addEscapingUse(Phi->getOperandUse(2*ii));
for (unsigned ii = 0, ee = Phi->getNumIncomingValues(); ii != ee;
++ii) {
unsigned jj = PHINode::getOperandNumForIncomingValue(ii);
VN.getAliasAnalysis()->addEscapingUse(Phi->getOperandUse(jj));
}
if (MD)
MD->invalidateCachedPointerInfo(Phi);

View File

@ -220,7 +220,7 @@ bool LoopRotate::rotateLoop(Loop *L) {
// For PHI nodes, the value available in OldPreHeader is just the
// incoming value from OldPreHeader.
for (; PHINode *PN = dyn_cast<PHINode>(I); ++I)
ValueMap[PN] = PN->getIncomingValue(PN->getBasicBlockIndex(OrigPreheader));
ValueMap[PN] = PN->getIncomingValueForBlock(OrigPreheader);
// For the rest of the instructions, either hoist to the OrigPreheader if
// possible or create a clone in the OldPreHeader if not.

View File

@ -1905,16 +1905,16 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
writeOperand(I.getOperand(i), true);
}
Out << ']';
} else if (isa<PHINode>(I)) {
} else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
Out << ' ';
TypePrinter.print(I.getType(), Out);
Out << ' ';
for (unsigned op = 0, Eop = I.getNumOperands(); op < Eop; op += 2) {
for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
if (op) Out << ", ";
Out << "[ ";
writeOperand(I.getOperand(op ), false); Out << ", ";
writeOperand(I.getOperand(op+1), false); Out << " ]";
writeOperand(PN->getIncomingValue(op), false); Out << ", ";
writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
}
} else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
Out << ' ';

View File

@ -227,8 +227,8 @@ void BasicBlock::removePredecessor(BasicBlock *Pred,
// If the PHI _HAD_ two uses, replace PHI node with its now *single* value
if (max_idx == 2) {
if (PN->getOperand(0) != PN)
PN->replaceAllUsesWith(PN->getOperand(0));
if (PN->getIncomingValue(0) != PN)
PN->replaceAllUsesWith(PN->getIncomingValue(0));
else
// We are left with an infinite loop with no entries: kill the PHI.
PN->replaceAllUsesWith(UndefValue::get(PN->getType()));

View File

@ -1482,8 +1482,10 @@ void Verifier::visitInstruction(Instruction &I) {
// PHI nodes differ from other nodes because they actually "use" the
// value in the predecessor basic blocks they correspond to.
BasicBlock *UseBlock = BB;
if (isa<PHINode>(I))
UseBlock = dyn_cast<BasicBlock>(I.getOperand(i+1));
if (PHINode *PN = dyn_cast<PHINode>(&I)) {
unsigned j = PHINode::getIncomingValueNumForOperand(i);
UseBlock = PN->getIncomingBlock(j);
}
Assert2(UseBlock, "Invoke operand is PHI node with bad incoming-BB",
Op, &I);
@ -1515,10 +1517,11 @@ void Verifier::visitInstruction(Instruction &I) {
return;
}
}
} else if (isa<PHINode>(I)) {
} else if (PHINode *PN = dyn_cast<PHINode>(&I)) {
// PHI nodes are more difficult than other nodes because they actually
// "use" the value in the predecessor basic blocks they correspond to.
BasicBlock *PredBB = dyn_cast<BasicBlock>(I.getOperand(i+1));
unsigned j = PHINode::getIncomingValueNumForOperand(i);
BasicBlock *PredBB = PN->getIncomingBlock(j);
Assert2(PredBB && (DT->dominates(OpBlock, PredBB) ||
!DT->isReachableFromEntry(PredBB)),
"Instruction does not dominate all uses!", Op, &I);