1 //===-- Module.cpp - Implement the Module class ------------------*- C++ -*--=//
3 // This file implements the Module class for the VMCore library.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Module.h"
8 #include "llvm/Method.h"
9 #include "llvm/GlobalVariable.h"
10 #include "llvm/BasicBlock.h"
11 #include "llvm/InstrTypes.h"
12 #include "llvm/ValueHolderImpl.h"
13 #include "llvm/Support/STLExtras.h"
14 #include "llvm/Type.h"
17 // Instantiate Templates - This ugliness is the price we have to pay
18 // for having a DefHolderImpl.h file seperate from DefHolder.h! :(
20 template class ValueHolder<GlobalVariable, Module, Module>;
21 template class ValueHolder<Method, Module, Module>;
23 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
24 // have Module.h depend on <map>
26 struct GlobalValueRefMap : public map<GlobalValue*, ConstPoolPointerRef*>{
31 : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this),
32 GlobalList(this, this), MethodList(this, this), GVRefMap(0) {
37 GlobalList.delete_all();
38 GlobalList.setParent(0);
39 MethodList.delete_all();
40 MethodList.setParent(0);
44 // dropAllReferences() - This function causes all the subinstructions to "let
45 // go" of all references that they are maintaining. This allows one to
46 // 'delete' a whole class at a time, even though there may be circular
47 // references... first all references are dropped, and all use counts go to
48 // zero. Then everything is delete'd for real. Note that no operations are
49 // valid on an object that has "dropped all references", except operator
52 void Module::dropAllReferences() {
53 for_each(MethodList.begin(), MethodList.end(),
54 std::mem_fun(&Method::dropAllReferences));
56 for_each(GlobalList.begin(), GlobalList.end(),
57 std::mem_fun(&GlobalVariable::dropAllReferences));
59 // If there are any GlobalVariable references still out there, nuke them now.
60 // Since all references are hereby dropped, nothing could possibly reference
63 for (GlobalValueRefMap::iterator I = GVRefMap->begin(), E = GVRefMap->end();
65 // Delete the ConstPoolPointerRef node...
66 I->second->destroyConstant();
69 // Since the table is empty, we can now delete it...
74 // reduceApply - Apply the specified function to all of the methods in this
75 // module. The result values are or'd together and the result is returned.
77 bool Module::reduceApply(bool (*Func)(GlobalVariable*)) {
78 return reduce_apply_bool(gbegin(), gend(), Func);
80 bool Module::reduceApply(bool (*Func)(const GlobalVariable*)) const {
81 return reduce_apply_bool(gbegin(), gend(), Func);
83 bool Module::reduceApply(bool (*Func)(Method*)) {
84 return reduce_apply_bool(begin(), end(), Func);
86 bool Module::reduceApply(bool (*Func)(const Method*)) const {
87 return reduce_apply_bool(begin(), end(), Func);
90 // Accessor for the underlying GlobalValRefMap...
91 ConstPoolPointerRef *Module::getConstPoolPointerRef(GlobalValue *V){
92 // Create ref map lazily on demand...
93 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
95 GlobalValueRefMap::iterator I = GVRefMap->find(V);
96 if (I != GVRefMap->end()) return I->second;
98 ConstPoolPointerRef *Ref = new ConstPoolPointerRef(V);
99 GVRefMap->insert(make_pair(V, Ref));
104 void Module::mutateConstPoolPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
105 GlobalValueRefMap::iterator I = GVRefMap->find(OldGV);
106 assert(I != GVRefMap->end() &&
107 "mutateConstPoolPointerRef; OldGV not in table!");
108 ConstPoolPointerRef *Ref = I->second;
110 // Remove the old entry...
113 // Insert the new entry...
114 GVRefMap->insert(make_pair(NewGV, Ref));