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/InstrTypes.h"
9 #include "llvm/Constants.h"
10 #include "llvm/DerivedTypes.h"
11 #include "Support/STLExtras.h"
12 #include "Support/LeakDetector.h"
13 #include "SymbolTableListTraitsImpl.h"
17 Function *ilist_traits<Function>::createNode() {
19 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
20 Function *Ret = new Function(FTy, false);
21 // This should not be garbage monitored.
22 LeakDetector::removeGarbageObject(Ret);
25 GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
26 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, false);
27 // This should not be garbage monitored.
28 LeakDetector::removeGarbageObject(Ret);
32 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
33 return M->getFunctionList();
35 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
36 return M->getGlobalList();
39 // Explicit instantiations of SymbolTableListTraits since some of the methods
40 // are not in the public header file...
41 template SymbolTableListTraits<GlobalVariable, Module, Module>;
42 template SymbolTableListTraits<Function, Module, Module>;
44 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
45 // have Module.h depend on <map>
47 struct GlobalValueRefMap {
48 typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
49 typedef MapTy::iterator iterator;
50 std::map<GlobalValue*, ConstantPointerRef*> Map;
55 FunctionList.setItemParent(this);
56 FunctionList.setParent(this);
57 GlobalList.setItemParent(this);
58 GlobalList.setParent(this);
66 GlobalList.setParent(0);
68 FunctionList.setParent(0);
72 // Module::dump() - Allow printing from debugger
73 void Module::dump() const {
77 SymbolTable *Module::getSymbolTableSure() {
78 if (!SymTab) SymTab = new SymbolTable();
82 // hasSymbolTable() - Returns true if there is a symbol table allocated to
83 // this object AND if there is at least one name in it!
85 bool Module::hasSymbolTable() const {
86 if (!SymTab) return false;
88 for (SymbolTable::const_iterator I = SymTab->begin(), E = SymTab->end();
90 if (I->second.begin() != I->second.end())
91 return true; // Found nonempty type plane!
97 // getOrInsertFunction - Look up the specified function in the module symbol
98 // table. If it does not exist, add a prototype for the function and return
99 // it. This is nice because it allows most passes to get away with not handling
100 // the symbol table directly for this common task.
102 Function *Module::getOrInsertFunction(const std::string &Name,
103 const FunctionType *Ty) {
104 SymbolTable *SymTab = getSymbolTableSure();
106 // See if we have a definitions for the specified function already...
107 if (Value *V = SymTab->lookup(PointerType::get(Ty), Name)) {
108 return cast<Function>(V); // Yup, got it
109 } else { // Nope, add one
110 Function *New = new Function(Ty, false, Name);
111 FunctionList.push_back(New);
112 return New; // Return the new prototype...
116 // getFunction - Look up the specified function in the module symbol table.
117 // If it does not exist, return null.
119 Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
120 SymbolTable *SymTab = getSymbolTable();
121 if (SymTab == 0) return 0; // No symtab, no symbols...
123 return cast_or_null<Function>(SymTab->lookup(PointerType::get(Ty), Name));
126 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
127 // there is already an entry for this name, true is returned and the symbol
128 // table is not modified.
130 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
131 SymbolTable *ST = getSymbolTableSure();
133 if (ST->lookup(Type::TypeTy, Name)) return true; // Already in symtab...
135 // Not in symbol table? Set the name with the Symtab as an argument so the
136 // type knows what to update...
137 ((Value*)Ty)->setName(Name, ST);
142 /// getMainFunction - This function looks up main efficiently. This is such a
143 /// common case, that it is a method in Module. If main cannot be found, a
144 /// null pointer is returned.
146 Function *Module::getMainFunction() {
147 std::vector<const Type*> Params;
150 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
154 // void main(void)...
155 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
159 Params.push_back(Type::IntTy);
161 // int main(int argc)...
162 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
166 // void main(int argc)...
167 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
171 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
172 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
174 // int main(int argc, char **argv)...
175 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
179 // void main(int argc, char **argv)...
180 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
185 // Ok, try to find main the hard way...
186 return getNamedFunction("main");
189 /// getNamedFunction - Return the first function in the module with the
190 /// specified name, of arbitrary type. This method returns null if a function
191 /// with the specified name is not found.
193 Function *Module::getNamedFunction(const std::string &Name) {
194 // Loop over all of the functions, looking for the function desired
195 for (iterator I = begin(), E = end(); I != E; ++I)
196 if (I->getName() == Name)
198 return 0; // function not found...
203 // getTypeName - If there is at least one entry in the symbol table for the
204 // specified type, return it.
206 std::string Module::getTypeName(const Type *Ty) {
207 const SymbolTable *ST = getSymbolTable();
208 if (ST == 0) return ""; // No symbol table, must not have an entry...
209 if (ST->find(Type::TypeTy) == ST->end())
210 return ""; // No names for types...
212 SymbolTable::type_const_iterator TI = ST->type_begin(Type::TypeTy);
213 SymbolTable::type_const_iterator TE = ST->type_end(Type::TypeTy);
215 while (TI != TE && TI->second != (const Value*)Ty)
218 if (TI != TE) // Must have found an entry!
220 return ""; // Must not have found anything...
224 // dropAllReferences() - This function causes all the subelementss to "let go"
225 // of all references that they are maintaining. This allows one to 'delete' a
226 // whole module at a time, even though there may be circular references... first
227 // all references are dropped, and all use counts go to zero. Then everything
228 // is delete'd for real. Note that no operations are valid on an object that
229 // has "dropped all references", except operator delete.
231 void Module::dropAllReferences() {
232 for(Module::iterator I = begin(), E = end(); I != E; ++I)
233 I->dropAllReferences();
235 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
236 I->dropAllReferences();
238 // If there are any GlobalVariable references still out there, nuke them now.
239 // Since all references are hereby dropped, nothing could possibly reference
240 // them still. Note that destroying all of the constant pointer refs will
241 // eventually cause the GVRefMap field to be set to null (by
242 // destroyConstantPointerRef, below).
245 // Delete the ConstantPointerRef node...
246 GVRefMap->Map.begin()->second->destroyConstant();
249 // Accessor for the underlying GlobalValRefMap...
250 ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
251 // Create ref map lazily on demand...
252 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
254 GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
255 if (I != GVRefMap->Map.end()) return I->second;
257 ConstantPointerRef *Ref = new ConstantPointerRef(V);
258 GVRefMap->Map[V] = Ref;
262 void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
263 assert(GVRefMap && "No map allocated, but we have a CPR?");
264 if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map...
265 assert(0 && "ConstantPointerRef not found in module CPR map!");
267 if (GVRefMap->Map.empty()) { // If the map is empty, delete it.
273 void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
274 GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
275 assert(I != GVRefMap->Map.end() &&
276 "mutateConstantPointerRef; OldGV not in table!");
277 ConstantPointerRef *Ref = I->second;
279 // Remove the old entry...
280 GVRefMap->Map.erase(I);
282 // Insert the new entry...
283 GVRefMap->Map.insert(std::make_pair(NewGV, Ref));