606d3aef14226e8be991242cc16ce0df119908f6
[oota-llvm.git] / lib / VMCore / Module.cpp
1 //===-- Module.cpp - Implement the Module class ------------------*- C++ -*--=//
2 //
3 // This file implements the Module class for the VMCore library.
4 //
5 //===----------------------------------------------------------------------===//
6
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/Type.h"
14 #include "llvm/ConstantVals.h"
15 #include "Support/STLExtras.h"
16 #include <map>
17
18 // Instantiate Templates - This ugliness is the price we have to pay
19 // for having a DefHolderImpl.h file seperate from DefHolder.h!  :(
20 //
21 template class ValueHolder<GlobalVariable, Module, Module>;
22 template class ValueHolder<Method, Module, Module>;
23
24 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
25 // have Module.h depend on <map>
26 //
27 struct GlobalValueRefMap : public std::map<GlobalValue*, ConstantPointerRef*>{
28 };
29
30
31 Module::Module()
32   : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this),
33     GlobalList(this, this), MethodList(this, this), GVRefMap(0) {
34 }
35
36 Module::~Module() {
37   dropAllReferences();
38   GlobalList.delete_all();
39   GlobalList.setParent(0);
40   MethodList.delete_all();
41   MethodList.setParent(0);
42 }
43
44
45 // dropAllReferences() - This function causes all the subinstructions to "let
46 // go" of all references that they are maintaining.  This allows one to
47 // 'delete' a whole class at a time, even though there may be circular
48 // references... first all references are dropped, and all use counts go to
49 // zero.  Then everything is delete'd for real.  Note that no operations are
50 // valid on an object that has "dropped all references", except operator 
51 // delete.
52 //
53 void Module::dropAllReferences() {
54   for_each(MethodList.begin(), MethodList.end(),
55            std::mem_fun(&Method::dropAllReferences));
56
57   for_each(GlobalList.begin(), GlobalList.end(),
58            std::mem_fun(&GlobalVariable::dropAllReferences));
59
60   // If there are any GlobalVariable references still out there, nuke them now.
61   // Since all references are hereby dropped, nothing could possibly reference
62   // them still.
63   if (GVRefMap) {
64     for (GlobalValueRefMap::iterator I = GVRefMap->begin(), E = GVRefMap->end();
65          I != E; ++I) {
66       // Delete the ConstantPointerRef node...
67       I->second->destroyConstant();
68     }
69
70     // Since the table is empty, we can now delete it...
71     delete GVRefMap;
72   }
73 }
74
75 // reduceApply - Apply the specified function to all of the methods in this 
76 // module.  The result values are or'd together and the result is returned.
77 //
78 bool Module::reduceApply(bool (*Func)(GlobalVariable*)) {
79   return reduce_apply_bool(gbegin(), gend(), Func);
80 }
81 bool Module::reduceApply(bool (*Func)(const GlobalVariable*)) const {
82   return reduce_apply_bool(gbegin(), gend(), Func);
83 }
84 bool Module::reduceApply(bool (*Func)(Method*)) {
85   return reduce_apply_bool(begin(), end(), Func);
86 }
87 bool Module::reduceApply(bool (*Func)(const Method*)) const {
88   return reduce_apply_bool(begin(), end(), Func);
89 }
90
91 // Accessor for the underlying GlobalValRefMap...
92 ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
93   // Create ref map lazily on demand...
94   if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
95
96   GlobalValueRefMap::iterator I = GVRefMap->find(V);
97   if (I != GVRefMap->end()) return I->second;
98
99   ConstantPointerRef *Ref = new ConstantPointerRef(V);
100   GVRefMap->insert(std::make_pair(V, Ref));
101
102   return Ref;
103 }
104
105 void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
106   GlobalValueRefMap::iterator I = GVRefMap->find(OldGV);
107   assert(I != GVRefMap->end() && 
108          "mutateConstantPointerRef; OldGV not in table!");
109   ConstantPointerRef *Ref = I->second;
110
111   // Remove the old entry...
112   GVRefMap->erase(I);
113
114   // Insert the new entry...
115   GVRefMap->insert(std::make_pair(NewGV, Ref));
116 }