Destroy allocated resources on exception.
[oota-llvm.git] / lib / VMCore / BasicBlock.cpp
index 39ffdea865bea5d98609608be384f537def61221..c15ce24ce6552c3a83cd51d26ba6b7a2ccc9b1ec 100644 (file)
@@ -1,4 +1,4 @@
-//===-- BasicBlock.cpp - Implement BasicBlock related functions --*- C++ -*--=//
+//===-- BasicBlock.cpp - Implement BasicBlock related methods -------------===//
 //
 // This file implements the BasicBlock class for the VMCore library.
 //
@@ -11,7 +11,7 @@
 #include "llvm/Constant.h"
 #include "llvm/iPHINode.h"
 #include "llvm/SymbolTable.h"
-#include "llvm/CodeGen/MachineInstr.h"
+#include "Support/LeakDetector.h"
 #include "SymbolTableListTraitsImpl.h"
 #include <algorithm>
 
 // instruction list.  This is not a real instruction.
 //
 struct DummyInst : public Instruction {
-  DummyInst() : Instruction(Type::VoidTy, NumOtherOps) {}
+  DummyInst() : Instruction(Type::VoidTy, OtherOpsEnd) {
+    // This should not be garbage monitored.
+    LeakDetector::removeGarbageObject(this);
+  }
 
-  virtual Instruction *clone() const { assert(0 && "Cannot clone EOL");abort();}
+  virtual Instruction *clone() const {
+    assert(0 && "Cannot clone EOL");abort();
+    return 0;
+  }
   virtual const char *getOpcodeName() const { return "*end-of-list-inst*"; }
 
   // Methods for support type inquiry through isa, cast, and dyn_cast...
   static inline bool classof(const DummyInst *) { return true; }
   static inline bool classof(const Instruction *I) {
-    return I->getOpcode() == NumOtherOps;
+    return I->getOpcode() == OtherOpsEnd;
   }
   static inline bool classof(const Value *V) {
     return isa<Instruction>(V) && classof(cast<Instruction>(V));
@@ -46,30 +52,64 @@ iplist<Instruction> &ilist_traits<Instruction>::getList(BasicBlock *BB) {
 template SymbolTableListTraits<Instruction, BasicBlock, Function>;
 
 
+// BasicBlock ctor - If the function parameter is specified, the basic block is
+// automatically inserted at the end of the function.
+//
 BasicBlock::BasicBlock(const std::string &name, Function *Parent)
-  : Value(Type::LabelTy, Value::BasicBlockVal, name),
-    machineInstrVec(new MachineCodeForBasicBlock) {
+  : Value(Type::LabelTy, Value::BasicBlockVal, name) {
   // Initialize the instlist...
   InstList.setItemParent(this);
 
+  // Make sure that we get added to a function
+  LeakDetector::addGarbageObject(this);
+
   if (Parent)
     Parent->getBasicBlockList().push_back(this);
 }
 
+/// BasicBlock ctor - If the InsertBefore parameter is specified, the basic
+/// block is automatically inserted right before the specified block.
+///
+BasicBlock::BasicBlock(const std::string &Name, BasicBlock *InsertBefore)
+  : Value(Type::LabelTy, Value::BasicBlockVal, Name) {
+  // Initialize the instlist...
+  InstList.setItemParent(this);
+
+  // Make sure that we get added to a function
+  LeakDetector::addGarbageObject(this);
+
+  if (InsertBefore) {
+    assert(InsertBefore->getParent() &&
+           "Cannot insert block before another block that is not embedded into"
+           " a function yet!");
+    InsertBefore->getParent()->getBasicBlockList().insert(InsertBefore, this);
+  }
+}
+
+
 BasicBlock::~BasicBlock() {
   dropAllReferences();
   InstList.clear();
-  delete machineInstrVec;
+}
+
+void BasicBlock::setParent(Function *parent) {
+  if (getParent())
+    LeakDetector::addGarbageObject(this);
+
+  InstList.setParent(parent);
+
+  if (getParent())
+    LeakDetector::removeGarbageObject(this);
 }
 
 // Specialize setName to take care of symbol table majik
 void BasicBlock::setName(const std::string &name, SymbolTable *ST) {
   Function *P;
-  assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
+  assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
         "Invalid symtab argument!");
-  if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
+  if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
   Value::setName(name);
-  if (P && hasName()) P->getSymbolTable()->insert(this);
+  if (P && hasName()) P->getSymbolTable().insert(this);
 }
 
 TerminatorInst *BasicBlock::getTerminator() {
@@ -143,21 +183,24 @@ void BasicBlock::removePredecessor(BasicBlock *Pred) {
     // Yup, loop through and nuke the PHI nodes
     while (PHINode *PN = dyn_cast<PHINode>(&front())) {
       PN->removeIncomingValue(Pred); // Remove the predecessor first...
-      
-      assert(PN->getNumIncomingValues() == max_idx-1 && 
-            "PHI node shouldn't have this many values!!!");
 
       // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
-      if (max_idx == 2)
-       PN->replaceAllUsesWith(PN->getOperand(0));
-      else // Otherwise there are no incoming values/edges, replace with dummy
-        PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
-      getInstList().pop_front();    // Remove the PHI node
+      if (max_idx == 2) {
+        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
+      }
+
+      // If the PHI node already only had one entry, it got deleted by
+      // removeIncomingValue.
     }
   } 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);
   }
 }
@@ -174,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...
@@ -198,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);