Destroy MDNodes gracefully while deleting llvm context.
authorDevang Patel <dpatel@apple.com>
Thu, 18 Feb 2010 20:53:16 +0000 (20:53 +0000)
committerDevang Patel <dpatel@apple.com>
Thu, 18 Feb 2010 20:53:16 +0000 (20:53 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@96609 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Metadata.h
lib/VMCore/LLVMContextImpl.h
lib/VMCore/Metadata.cpp

index 4e459bf7bf5c113d04ab2de4c8f165f8c9ea2f9e..0369f5e80ca136d7dd6b088909ec2ad0968b2716 100644 (file)
@@ -75,6 +75,7 @@ class MDNode : public Value, public FoldingSetNode {
   MDNode(const MDNode &);                // DO NOT IMPLEMENT
   void operator=(const MDNode &);        // DO NOT IMPLEMENT
   friend class MDNodeOperand;
+  friend class LLVMContextImpl;
 
   /// NumOperands - This many 'MDNodeOperand' items are co-allocated onto the
   /// end of this MDNode.
@@ -106,6 +107,9 @@ class MDNode : public Value, public FoldingSetNode {
   // Replace each instance of F from the operand list of this node with T.
   void replaceOperand(MDNodeOperand *Op, Value *NewVal);
   ~MDNode();
+    // replaceAllOperandsWithNull - This is used while destroying llvm context to 
+  // gracefully delete all nodes. This method replaces all operands with null.
+  void replaceAllOperandsWithNull();
 
 protected:
   explicit MDNode(LLVMContext &C, Value *const *Vals, unsigned NumVals,
index 62491d838fbea789d2753192be435d025251e970..85bbe4ac9b0099d57f8a97ac670843d0f79d6802 100644 (file)
@@ -229,13 +229,23 @@ public:
       if (I->second->use_empty())
         delete I->second;
     }
-    MDNodeSet.clear();
     AlwaysOpaqueTy->dropRef();
     for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end();
         I != E; ++I) {
       (*I)->AbstractTypeUsers.clear();
       delete *I;
     }
+    // Destroy MDNode operands first.
+    for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
+         I != E;) {
+      MDNode *N = &(*I);
+      ++I;
+      N->replaceAllOperandsWithNull();
+    }
+    while (!MDNodeSet.empty()) {
+      MDNode *N = &(*MDNodeSet.begin());
+      N->destroy();
+    }
   }
 };
 
index 07a5f3c539882ca8b7e98414b307f073b193952d..a08c45480b6d44dbdaec4a7cc3de15b5ab006ba0 100644 (file)
@@ -257,6 +257,13 @@ void MDNode::Profile(FoldingSetNodeID &ID) const {
     ID.AddPointer(getOperand(i));
 }
 
+// replaceAllOperandsWithNull - This is used while destroying llvm context to 
+// gracefully delete all nodes. This method replaces all operands with null.
+void MDNode::replaceAllOperandsWithNull() {
+  for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
+       Op != E; ++Op)
+    replaceOperand(Op, 0);
+}
 
 // Replace value from this node's operand list.
 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {