Changes to make GlobalValueRefMap not derive from std::map.
[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/Function.h"
9 #include "llvm/GlobalVariable.h"
10 #include "llvm/InstrTypes.h"
11 #include "llvm/Constants.h"
12 #include "llvm/DerivedTypes.h"
13 #include "Support/STLExtras.h"
14 #include "SymbolTableListTraitsImpl.h"
15 #include <algorithm>
16 #include <map>
17
18 Function *ilist_traits<Function>::createNode() {
19   return new Function(FunctionType::get(Type::VoidTy,std::vector<const Type*>(),
20                                         false), false);
21 }
22 GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
23   return new GlobalVariable(Type::IntTy, false, false);
24 }
25
26 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
27   return M->getFunctionList();
28 }
29 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
30   return M->getGlobalList();
31 }
32
33 // Explicit instantiations of SymbolTableListTraits since some of the methods
34 // are not in the public header file...
35 template SymbolTableListTraits<GlobalVariable, Module, Module>;
36 template SymbolTableListTraits<Function, Module, Module>;
37
38 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
39 // have Module.h depend on <map>
40 //
41 struct GlobalValueRefMap {
42   typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
43   typedef MapTy::iterator iterator;
44   std::map<GlobalValue*, ConstantPointerRef*> Map;
45 };
46
47
48 Module::Module() {
49   FunctionList.setItemParent(this);
50   FunctionList.setParent(this);
51   GlobalList.setItemParent(this);
52   GlobalList.setParent(this);
53   GVRefMap = 0;
54   SymTab = 0;
55 }
56
57 Module::~Module() {
58   dropAllReferences();
59   GlobalList.clear();
60   GlobalList.setParent(0);
61   FunctionList.clear();
62   FunctionList.setParent(0);
63   delete SymTab;
64 }
65
66 SymbolTable *Module::getSymbolTableSure() {
67   if (!SymTab) SymTab = new SymbolTable(0);
68   return SymTab;
69 }
70
71 // hasSymbolTable() - Returns true if there is a symbol table allocated to
72 // this object AND if there is at least one name in it!
73 //
74 bool Module::hasSymbolTable() const {
75   if (!SymTab) return false;
76
77   for (SymbolTable::const_iterator I = SymTab->begin(), E = SymTab->end();
78        I != E; ++I)
79     if (I->second.begin() != I->second.end())
80       return true;                                // Found nonempty type plane!
81   
82   return false;
83 }
84
85
86 // getOrInsertFunction - Look up the specified function in the module symbol
87 // table.  If it does not exist, add a prototype for the function and return
88 // it.  This is nice because it allows most passes to get away with not handling
89 // the symbol table directly for this common task.
90 //
91 Function *Module::getOrInsertFunction(const std::string &Name,
92                                       const FunctionType *Ty) {
93   SymbolTable *SymTab = getSymbolTableSure();
94
95   // See if we have a definitions for the specified function already...
96   if (Value *V = SymTab->lookup(PointerType::get(Ty), Name)) {
97     return cast<Function>(V);      // Yup, got it
98   } else {                         // Nope, add one
99     Function *New = new Function(Ty, false, Name);
100     FunctionList.push_back(New);
101     return New;                    // Return the new prototype...
102   }
103 }
104
105 // getFunction - Look up the specified function in the module symbol table.
106 // If it does not exist, return null.
107 //
108 Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
109   SymbolTable *SymTab = getSymbolTable();
110   if (SymTab == 0) return 0;  // No symtab, no symbols...
111
112   return cast_or_null<Function>(SymTab->lookup(PointerType::get(Ty), Name));
113 }
114
115 // addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
116 // there is already an entry for this name, true is returned and the symbol
117 // table is not modified.
118 //
119 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
120   SymbolTable *ST = getSymbolTableSure();
121
122   if (ST->lookup(Type::TypeTy, Name)) return true;  // Already in symtab...
123   
124   // Not in symbol table?  Set the name with the Symtab as an argument so the
125   // type knows what to update...
126   ((Value*)Ty)->setName(Name, ST);
127
128   return false;
129 }
130
131 // getTypeName - If there is at least one entry in the symbol table for the
132 // specified type, return it.
133 //
134 std::string Module::getTypeName(const Type *Ty) {
135   const SymbolTable *ST = getSymbolTable();
136   if (ST == 0) return "";  // No symbol table, must not have an entry...
137   if (ST->find(Type::TypeTy) == ST->end())
138     return ""; // No names for types...
139
140   SymbolTable::type_const_iterator TI = ST->type_begin(Type::TypeTy);
141   SymbolTable::type_const_iterator TE = ST->type_end(Type::TypeTy);
142
143   while (TI != TE && TI->second != (const Value*)Ty)
144     ++TI;
145
146   if (TI != TE)  // Must have found an entry!
147     return TI->first;
148   return "";     // Must not have found anything...
149 }
150
151
152 // dropAllReferences() - This function causes all the subinstructions to "let
153 // go" of all references that they are maintaining.  This allows one to
154 // 'delete' a whole class at a time, even though there may be circular
155 // references... first all references are dropped, and all use counts go to
156 // zero.  Then everything is delete'd for real.  Note that no operations are
157 // valid on an object that has "dropped all references", except operator 
158 // delete.
159 //
160 void Module::dropAllReferences() {
161   for(Module::iterator I = begin(), E = end(); I != E; ++I)
162     I->dropAllReferences();
163
164   for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
165     I->dropAllReferences();
166
167   // If there are any GlobalVariable references still out there, nuke them now.
168   // Since all references are hereby dropped, nothing could possibly reference
169   // them still.
170   if (GVRefMap) {
171     for (GlobalValueRefMap::iterator I = GVRefMap->Map.begin(),
172            E = GVRefMap->Map.end(); I != E; ++I) {
173       // Delete the ConstantPointerRef node...
174       I->second->destroyConstant();
175     }
176
177     // Since the table is empty, we can now delete it...
178     delete GVRefMap;
179   }
180 }
181
182 // Accessor for the underlying GlobalValRefMap...
183 ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
184   // Create ref map lazily on demand...
185   if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
186
187   GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
188   if (I != GVRefMap->Map.end()) return I->second;
189
190   ConstantPointerRef *Ref = new ConstantPointerRef(V);
191   GVRefMap->Map.insert(std::make_pair(V, Ref));
192
193   return Ref;
194 }
195
196 void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
197   GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
198   assert(I != GVRefMap->Map.end() && 
199          "mutateConstantPointerRef; OldGV not in table!");
200   ConstantPointerRef *Ref = I->second;
201
202   // Remove the old entry...
203   GVRefMap->Map.erase(I);
204
205   // Insert the new entry...
206   GVRefMap->Map.insert(std::make_pair(NewGV, Ref));
207 }