// If the PHI _HAD_ two uses, replace PHI node with its now *single* value
if (max_idx == 2) {
- PN->replaceAllUsesWith(PN->getOperand(0));
+ if (PN->getOperand(0) != PN)
+ PN->replaceAllUsesWith(PN->getOperand(0));
+ else
+ // We are left with an infinite loop with no entries: kill the PHI.
+ PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
getInstList().pop_front(); // Remove the PHI node
}
} else {
// Okay, now we know that we need to remove predecessor #pred_idx from all
// PHI nodes. Iterate over each PHI node fixing them up
- for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(&*II); ++II)
+ for (iterator II = begin(); PHINode *PN = dyn_cast<PHINode>(II); ++II)
PN->removeIncomingValue(Pred);
}
}
// cause a degenerate basic block to be formed, having a terminator inside of
// the basic block).
//
-BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
+BasicBlock *BasicBlock::splitBasicBlock(iterator I, const std::string &BBName) {
assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
assert(I != InstList.end() &&
"Trying to get me to create degenerate basic block!");
- BasicBlock *New = new BasicBlock("", getParent());
+ BasicBlock *New = new BasicBlock(BBName, getParent());
// Go from the end of the basic block through to the iterator pointer, moving
// to the new basic block...
// successors. If there were PHI nodes in the successors, then they need to
// know that incoming branches will be from New, not from Old.
//
- for (BasicBlock::succ_iterator I = succ_begin(New), E = succ_end(New);
- I != E; ++I) {
+ for (succ_iterator I = succ_begin(New), E = succ_end(New); I != E; ++I) {
// Loop over any phi nodes in the basic block, updating the BB field of
// incoming values...
BasicBlock *Successor = *I;
for (BasicBlock::iterator II = Successor->begin();
- PHINode *PN = dyn_cast<PHINode>(&*II); ++II) {
+ PHINode *PN = dyn_cast<PHINode>(II); ++II) {
int IDX = PN->getBasicBlockIndex(this);
while (IDX != -1) {
PN->setIncomingBlock((unsigned)IDX, New);