Free MDNodes when the LLVMContext is destroyed. Leak found by Valgrind.
[oota-llvm.git] / lib / VMCore / Metadata.cpp
index 822dbd9521d289d0ccbf8eb1384befffe6d7d682..faf83e6588e50af939442bde80e06f6ae35a078a 100644 (file)
@@ -28,7 +28,7 @@ using namespace llvm;
 //
 
 MDString::MDString(LLVMContext &C, StringRef S)
-  : MetadataBase(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
+  : Value(Type::getMetadataTy(C), Value::MDStringVal), Str(S) {}
 
 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
   LLVMContextImpl *pImpl = Context.pImpl;
@@ -93,7 +93,7 @@ static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
 
 MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
                bool isFunctionLocal)
-: MetadataBase(Type::getMetadataTy(C), Value::MDNodeVal) {
+: Value(Type::getMetadataTy(C), Value::MDNodeVal) {
   NumOperands = NumVals;
 
   if (isFunctionLocal)
@@ -110,8 +110,10 @@ MDNode::MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
 MDNode::~MDNode() {
   assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
          "Not being destroyed through destroy()?");
-  if (!isNotUniqued()) {
-    LLVMContextImpl *pImpl = getType()->getContext().pImpl;
+  LLVMContextImpl *pImpl = getType()->getContext().pImpl;
+  if (isNotUniqued()) {
+    pImpl->NonUniquedMDNodes.erase(this);
+  } else {
     pImpl->MDNodeSet.RemoveNode(this);
   }
 
@@ -122,26 +124,33 @@ MDNode::~MDNode() {
 }
 
 static const Function *getFunctionForValue(Value *V) {
+  assert(!isa<MDNode>(V) && "does not iterate over metadata operands");
   if (!V) return NULL;
   if (Instruction *I = dyn_cast<Instruction>(V))
     return I->getParent()->getParent();
-  if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) return BB->getParent();
-  if (Argument *A = dyn_cast<Argument>(V)) return A->getParent();
+  if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
+    return BB->getParent();
+  if (Argument *A = dyn_cast<Argument>(V))
+    return A->getParent();
   return NULL;
 }
 
 #ifndef NDEBUG
 static const Function *assertLocalFunction(const MDNode *N) {
-  if (!N->isFunctionLocal()) return NULL;
+  if (!N->isFunctionLocal()) return 0;
 
-  const Function *F = NULL, *NewF = NULL;
+  const Function *F = 0, *NewF = 0;
   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
     if (Value *V = N->getOperand(i)) {
-      if (MDNode *MD = dyn_cast<MDNode>(V)) NewF = assertLocalFunction(MD);
-      else NewF = getFunctionForValue(V);
+      if (MDNode *MD = dyn_cast<MDNode>(V))
+        NewF = assertLocalFunction(MD);
+      else
+        NewF = getFunctionForValue(V);
     }
-    if (F && NewF) assert(F == NewF && "inconsistent function-local metadata");
-    else if (!F) F = NewF;
+    if (F == 0)
+      F = NewF;
+    else 
+      assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
   }
   return F;
 }
@@ -159,10 +168,12 @@ const Function *MDNode::getFunction() const {
 
   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
     if (Value *V = getOperand(i)) {
-      if (MDNode *MD = dyn_cast<MDNode>(V))
-        if (const Function *F = MD->getFunction()) return F;
-      else
+      if (MDNode *MD = dyn_cast<MDNode>(V)) {
+        if (const Function *F = MD->getFunction())
+          return F;
+      } else {
         return getFunctionForValue(V);
+      }
     }
   }
   return NULL;
@@ -177,43 +188,50 @@ void MDNode::destroy() {
 }
 
 MDNode *MDNode::getMDNode(LLVMContext &Context, Value *const *Vals,
-                          unsigned NumVals, FunctionLocalness FL) {
+                          unsigned NumVals, FunctionLocalness FL,
+                          bool Insert) {
   LLVMContextImpl *pImpl = Context.pImpl;
   FoldingSetNodeID ID;
   for (unsigned i = 0; i != NumVals; ++i)
     ID.AddPointer(Vals[i]);
 
   void *InsertPoint;
-  MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
-  if (!N) {
-    bool isFunctionLocal = false;
-    switch (FL) {
-    case FL_Unknown:
-      for (unsigned i = 0; i != NumVals; ++i) {
-        Value *V = Vals[i];
-        if (!V) continue;
-        if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
-            (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
-          isFunctionLocal = true;
-          break;
-        }
+  MDNode *N = NULL;
+  
+  if ((N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)))
+    return N;
+    
+  if (!Insert)
+    return NULL;
+    
+  bool isFunctionLocal = false;
+  switch (FL) {
+  case FL_Unknown:
+    for (unsigned i = 0; i != NumVals; ++i) {
+      Value *V = Vals[i];
+      if (!V) continue;
+      if (isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
+          (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal())) {
+        isFunctionLocal = true;
+        break;
       }
-      break;
-    case FL_No:
-      isFunctionLocal = false;
-      break;
-    case FL_Yes:
-      isFunctionLocal = true;
-      break;
     }
+    break;
+  case FL_No:
+    isFunctionLocal = false;
+    break;
+  case FL_Yes:
+    isFunctionLocal = true;
+    break;
+  }
 
-    // Coallocate space for the node and Operands together, then placement new.
-    void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
-    N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
+  // Coallocate space for the node and Operands together, then placement new.
+  void *Ptr = malloc(sizeof(MDNode)+NumVals*sizeof(MDNodeOperand));
+  N = new (Ptr) MDNode(Context, Vals, NumVals, isFunctionLocal);
+
+  // InsertPoint will have been set by the FindNodeOrInsertPos call.
+  pImpl->MDNodeSet.InsertNode(N, InsertPoint);
 
-    // InsertPoint will have been set by the FindNodeOrInsertPos call.
-    pImpl->MDNodeSet.InsertNode(N, InsertPoint);
-  }
   return N;
 }
 
@@ -221,11 +239,16 @@ MDNode *MDNode::get(LLVMContext &Context, Value*const* Vals, unsigned NumVals) {
   return getMDNode(Context, Vals, NumVals, FL_Unknown);
 }
 
-MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value*const* Vals,
+MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context, Value *const *Vals,
                                       unsigned NumVals, bool isFunctionLocal) {
   return getMDNode(Context, Vals, NumVals, isFunctionLocal ? FL_Yes : FL_No);
 }
 
+MDNode *MDNode::getIfExists(LLVMContext &Context, Value *const *Vals,
+                            unsigned NumVals) {
+  return getMDNode(Context, Vals, NumVals, FL_Unknown, false);
+}
+
 /// getOperand - Return specified operand.
 Value *MDNode::getOperand(unsigned i) const {
   return *getOperandPtr(const_cast<MDNode*>(this), i);
@@ -236,6 +259,11 @@ void MDNode::Profile(FoldingSetNodeID &ID) const {
     ID.AddPointer(getOperand(i));
 }
 
+void MDNode::setIsNotUniqued() {
+  setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
+  LLVMContextImpl *pImpl = getType()->getContext().pImpl;
+  pImpl->NonUniquedMDNodes.insert(this);
+}
 
 // Replace value from this node's operand list.
 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {