* Expose new command line arg --debug-pass for gccas and llc debugging
[oota-llvm.git] / lib / VMCore / Module.cpp
index cec75bd3fb6e5db39c4da3dbda50b03f5760ac2d..606d3aef14226e8be991242cc16ce0df119908f6 100644 (file)
@@ -6,23 +6,37 @@
 
 #include "llvm/Module.h"
 #include "llvm/Method.h"
+#include "llvm/GlobalVariable.h"
 #include "llvm/BasicBlock.h"
 #include "llvm/InstrTypes.h"
 #include "llvm/ValueHolderImpl.h"
-#include "llvm/Tools/STLExtras.h"
+#include "llvm/Type.h"
+#include "llvm/ConstantVals.h"
+#include "Support/STLExtras.h"
+#include <map>
 
 // Instantiate Templates - This ugliness is the price we have to pay
 // for having a DefHolderImpl.h file seperate from DefHolder.h!  :(
 //
-template class ValueHolder<Method, Module>;
+template class ValueHolder<GlobalVariable, Module, Module>;
+template class ValueHolder<Method, Module, Module>;
+
+// Define the GlobalValueRefMap as a struct that wraps a map so that we don't
+// have Module.h depend on <map>
+//
+struct GlobalValueRefMap : public std::map<GlobalValue*, ConstantPointerRef*>{
+};
+
 
 Module::Module()
-  : SymTabValue(0/*TODO: REAL TYPE*/, Value::ModuleVal, ""),
-    MethodList(this, this) {
+  : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this),
+    GlobalList(this, this), MethodList(this, this), GVRefMap(0) {
 }
 
 Module::~Module() {
   dropAllReferences();
+  GlobalList.delete_all();
+  GlobalList.setParent(0);
   MethodList.delete_all();
   MethodList.setParent(0);
 }
@@ -37,14 +51,36 @@ Module::~Module() {
 // delete.
 //
 void Module::dropAllReferences() {
-  MethodListType::iterator MI = MethodList.begin();
-  for (; MI != MethodList.end(); ++MI)
-    (*MI)->dropAllReferences();
+  for_each(MethodList.begin(), MethodList.end(),
+          std::mem_fun(&Method::dropAllReferences));
+
+  for_each(GlobalList.begin(), GlobalList.end(),
+          std::mem_fun(&GlobalVariable::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->begin(), E = GVRefMap->end();
+        I != E; ++I) {
+      // Delete the ConstantPointerRef node...
+      I->second->destroyConstant();
+    }
+
+    // Since the table is empty, we can now delete it...
+    delete GVRefMap;
+  }
 }
 
 // reduceApply - Apply the specified function to all of the methods in this 
 // module.  The result values are or'd together and the result is returned.
 //
+bool Module::reduceApply(bool (*Func)(GlobalVariable*)) {
+  return reduce_apply_bool(gbegin(), gend(), Func);
+}
+bool Module::reduceApply(bool (*Func)(const GlobalVariable*)) const {
+  return reduce_apply_bool(gbegin(), gend(), Func);
+}
 bool Module::reduceApply(bool (*Func)(Method*)) {
   return reduce_apply_bool(begin(), end(), Func);
 }
@@ -52,3 +88,29 @@ bool Module::reduceApply(bool (*Func)(const Method*)) const {
   return reduce_apply_bool(begin(), end(), Func);
 }
 
+// Accessor for the underlying GlobalValRefMap...
+ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
+  // Create ref map lazily on demand...
+  if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
+
+  GlobalValueRefMap::iterator I = GVRefMap->find(V);
+  if (I != GVRefMap->end()) return I->second;
+
+  ConstantPointerRef *Ref = new ConstantPointerRef(V);
+  GVRefMap->insert(std::make_pair(V, Ref));
+
+  return Ref;
+}
+
+void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
+  GlobalValueRefMap::iterator I = GVRefMap->find(OldGV);
+  assert(I != GVRefMap->end() && 
+        "mutateConstantPointerRef; OldGV not in table!");
+  ConstantPointerRef *Ref = I->second;
+
+  // Remove the old entry...
+  GVRefMap->erase(I);
+
+  // Insert the new entry...
+  GVRefMap->insert(std::make_pair(NewGV, Ref));
+}