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