Free the constants that have no uses in ~LLVMContext.
This fixes leaks from LLVMContext in multithreaded apps.
Since constants are only deleted if they have no uses, it is safe to not delete
a Module on shutdown, as many single-threaded tools do.
Multithreaded apps should however delete the Module before destroying the
Context to ensure that there are no leaks (assuming they use a different context
for each thread).

llvm-svn: 80590
diff --git a/llvm/lib/VMCore/LLVMContextImpl.h b/llvm/lib/VMCore/LLVMContextImpl.h
index 4016c0a..2faf6ac 100644
--- a/llvm/lib/VMCore/LLVMContextImpl.h
+++ b/llvm/lib/VMCore/LLVMContextImpl.h
@@ -96,7 +96,6 @@
 class LLVMContextImpl {
 public:
   sys::SmartRWMutex<true> ConstantsLock;
-  
   typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 
                          DenseMapAPIntKeyInfo> IntMapTy;
   IntMapTy IntConstants;
@@ -196,6 +195,28 @@
     Int16Ty(C, 16),
     Int32Ty(C, 32),
     Int64Ty(C, 64) { }
+
+  ~LLVMContextImpl()
+  {
+    ExprConstants.freeConstants();
+    ArrayConstants.freeConstants();
+    StructConstants.freeConstants();
+    VectorConstants.freeConstants();
+
+    AggZeroConstants.freeConstants();
+    NullPtrConstants.freeConstants();
+    UndefValueConstants.freeConstants();
+    for (IntMapTy::iterator I=IntConstants.begin(), E=IntConstants.end(); 
+         I != E; ++I) {
+      if (I->second->use_empty())
+        delete I->second;
+    }
+    for (FPMapTy::iterator I=FPConstants.begin(), E=FPConstants.end(); 
+         I != E; ++I) {
+      if (I->second->use_empty())
+        delete I->second;
+    }
+  }
 };
 
 }