Destroy allocated resources on exception.
[oota-llvm.git] / lib / VMCore / BasicBlock.cpp
index 89f9a27f8da8279bc0075b4df3ef0f707649e352..c15ce24ce6552c3a83cd51d26ba6b7a2ccc9b1ec 100644 (file)
@@ -186,7 +186,11 @@ void BasicBlock::removePredecessor(BasicBlock *Pred) {
 
       // 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
       }
 
@@ -196,7 +200,7 @@ void BasicBlock::removePredecessor(BasicBlock *Pred) {
   } 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);
   }
 }
@@ -213,12 +217,12 @@ void BasicBlock::removePredecessor(BasicBlock *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...
@@ -237,13 +241,12 @@ BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
   // 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);