Free all Constants in ~LLVMConstantImpl. We avoid assertion failures
authorJeffrey Yasskin <jyasskin@google.com>
Mon, 22 Mar 2010 05:23:37 +0000 (05:23 +0000)
committerJeffrey Yasskin <jyasskin@google.com>
Mon, 22 Mar 2010 05:23:37 +0000 (05:23 +0000)
by dropping all references from all constants that can use other
constants before trying to destroy any of them.

I also had to free bugpoint's Module in ~BugDriver().

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@99160 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/ConstantsContext.h
lib/VMCore/LLVMContextImpl.cpp
tools/bugpoint/BugDriver.cpp
tools/bugpoint/BugDriver.h

index 560e6c4974ff26ffb5feaff2f1f746342b3e1fd7..2f2fac53f062e93e6c589372a61fcec649d39ed3 100644 (file)
@@ -600,8 +600,8 @@ public:
   void freeConstants() {
     for (typename MapTy::iterator I=Map.begin(), E=Map.end();
          I != E; ++I) {
-      if (I->second->use_empty())
-        delete I->second;
+      // Asserts that use_empty().
+      delete I->second;
     }
   }
     
index 176ccf18e4eaf7ed759ea68ff438bf58ca3d365f..b4553ddc1b9f4f899bfbab2252894b9db0cc68b0 100644 (file)
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "LLVMContextImpl.h"
+#include <algorithm>
 
 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
   : TheTrueVal(0), TheFalseVal(0),
@@ -34,10 +35,32 @@ LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
   OpaqueTypes.insert(AlwaysOpaqueTy);
 }
 
+namespace {
+struct DropReferences {
+  // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
+  // is a Constant*.
+  template<typename PairT>
+  void operator()(const PairT &P) {
+    P.second->dropAllReferences();
+  }
+};
+}
+
 LLVMContextImpl::~LLVMContextImpl() {
+  std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
+                DropReferences());
+  std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
+                DropReferences());
+  std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
+                DropReferences());
+  std::for_each(UnionConstants.map_begin(), UnionConstants.map_end(),
+                DropReferences());
+  std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
+                DropReferences());
   ExprConstants.freeConstants();
   ArrayConstants.freeConstants();
   StructConstants.freeConstants();
+  UnionConstants.freeConstants();
   VectorConstants.freeConstants();
   AggZeroConstants.freeConstants();
   NullPtrConstants.freeConstants();
@@ -45,13 +68,11 @@ LLVMContextImpl::~LLVMContextImpl() {
   InlineAsms.freeConstants();
   for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end(); 
        I != E; ++I) {
-    if (I->second->use_empty())
-      delete I->second;
+    delete I->second;
   }
   for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end(); 
        I != E; ++I) {
-    if (I->second->use_empty())
-      delete I->second;
+    delete I->second;
   }
   AlwaysOpaqueTy->dropRef();
   for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end();
index 813c96cc795deb81fcdb9fa6824bd365b901f608..024d8e219fa30e2d1f0082a99acdc77fefc341dd 100644 (file)
@@ -75,6 +75,10 @@ BugDriver::BugDriver(const char *toolname, bool as_child, bool find_bugs,
     run_as_child(as_child), run_find_bugs(find_bugs), Timeout(timeout), 
     MemoryLimit(memlimit), UseValgrind(use_valgrind) {}
 
+BugDriver::~BugDriver() {
+  delete Program;
+}
+
 
 /// ParseInputFile - Given a bitcode or assembly input filename, parse and
 /// return it, or return null if not possible.
index 0a10a61c2159186c5d2b969bae3bb6239ddad0cc..abc249868de25018fd3a58f91f621c5a1dac4129 100644 (file)
@@ -65,6 +65,7 @@ public:
   BugDriver(const char *toolname, bool as_child, bool find_bugs,
             unsigned timeout, unsigned memlimit, bool use_valgrind,
             LLVMContext& ctxt);
+  ~BugDriver();
 
   const char *getToolName() const { return ToolName; }