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