Make sure not to count the PassManager wrapers
[oota-llvm.git] / lib / VMCore / Module.cpp
index ae3b34f93da65783f865d2a2d2de80d92026160d..4bd3a884b308674aecc7237dc3c4a6bb1b710652 100644 (file)
@@ -5,22 +5,28 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Module.h"
-#include "llvm/Function.h"
-#include "llvm/GlobalVariable.h"
 #include "llvm/InstrTypes.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "Support/STLExtras.h"
+#include "Support/LeakDetector.h"
 #include "SymbolTableListTraitsImpl.h"
 #include <algorithm>
 #include <map>
 
 Function *ilist_traits<Function>::createNode() {
-  return new Function(FunctionType::get(Type::VoidTy,std::vector<const Type*>(),
-                                        false), false);
+  FunctionType *FTy =
+    FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
+  Function *Ret = new Function(FTy, false);
+  // This should not be garbage monitored.
+  LeakDetector::removeGarbageObject(Ret);
+  return Ret;
 }
 GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
-  return new GlobalVariable(Type::IntTy, false, false);
+  GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, false);
+  // This should not be garbage monitored.
+  LeakDetector::removeGarbageObject(Ret);
+  return Ret;
 }
 
 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
@@ -63,6 +69,11 @@ Module::~Module() {
   delete SymTab;
 }
 
+// Module::dump() - Allow printing from debugger
+void Module::dump() const {
+  print(std::cerr);
+}
+
 SymbolTable *Module::getSymbolTableSure() {
   if (!SymTab) SymTab = new SymbolTable(0);
   return SymTab;
@@ -149,13 +160,12 @@ std::string Module::getTypeName(const Type *Ty) {
 }
 
 
-// dropAllReferences() - This function causes all the subinstructions to "let
-// go" of all references that they are maintaining.  This allows one to
-// 'delete' a whole class at a time, even though there may be circular
-// references... first all references are dropped, and all use counts go to
-// zero.  Then everything is delete'd for real.  Note that no operations are
-// valid on an object that has "dropped all references", except operator 
-// delete.
+// dropAllReferences() - This function causes all the subelementss to "let go"
+// of all references that they are maintaining.  This allows one to 'delete' a
+// whole module at a time, even though there may be circular references... first
+// all references are dropped, and all use counts go to zero.  Then everything
+// is delete'd for real.  Note that no operations are valid on an object that
+// has "dropped all references", except operator delete.
 //
 void Module::dropAllReferences() {
   for(Module::iterator I = begin(), E = end(); I != E; ++I)
@@ -166,17 +176,13 @@ void Module::dropAllReferences() {
 
   // If there are any GlobalVariable references still out there, nuke them now.
   // Since all references are hereby dropped, nothing could possibly reference
-  // them still.
-  if (GVRefMap) {
-    for (GlobalValueRefMap::iterator I = GVRefMap->Map.begin(),
-           E = GVRefMap->Map.end(); I != E; ++I) {
-      // Delete the ConstantPointerRef node...
-      I->second->destroyConstant();
-    }
-
-    // Since the table is empty, we can now delete it...
-    delete GVRefMap;
-  }
+  // them still.  Note that destroying all of the constant pointer refs will
+  // eventually cause the GVRefMap field to be set to null (by
+  // destroyConstantPointerRef, below).
+  //
+  while (GVRefMap)
+    // Delete the ConstantPointerRef node...  
+    GVRefMap->Map.begin()->second->destroyConstant();
 }
 
 // Accessor for the underlying GlobalValRefMap...
@@ -188,11 +194,21 @@ ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
   if (I != GVRefMap->Map.end()) return I->second;
 
   ConstantPointerRef *Ref = new ConstantPointerRef(V);
-  GVRefMap->Map.insert(std::make_pair(V, Ref));
-
+  GVRefMap->Map[V] = Ref;
   return Ref;
 }
 
+void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
+  assert(GVRefMap && "No map allocated, but we have a CPR?");
+  if (!GVRefMap->Map.erase(CPR->getValue()))  // Remove it from the map...
+    assert(0 && "ConstantPointerRef not found in module CPR map!");
+  
+  if (GVRefMap->Map.empty()) {   // If the map is empty, delete it.
+    delete GVRefMap;
+    GVRefMap = 0;
+  }
+}
+
 void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
   GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
   assert(I != GVRefMap->Map.end() &&