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/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"
18 Function *ilist_traits<Function>::createNode() {
19 return new Function(FunctionType::get(Type::VoidTy,std::vector<const Type*>(),
22 GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
23 return new GlobalVariable(Type::IntTy, false, false);
26 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
27 return M->getFunctionList();
29 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
30 return M->getGlobalList();
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>;
38 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
39 // have Module.h depend on <map>
41 struct GlobalValueRefMap {
42 typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
43 typedef MapTy::iterator iterator;
44 std::map<GlobalValue*, ConstantPointerRef*> Map;
49 FunctionList.setItemParent(this);
50 FunctionList.setParent(this);
51 GlobalList.setItemParent(this);
52 GlobalList.setParent(this);
60 GlobalList.setParent(0);
62 FunctionList.setParent(0);
66 SymbolTable *Module::getSymbolTableSure() {
67 if (!SymTab) SymTab = new SymbolTable(0);
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!
74 bool Module::hasSymbolTable() const {
75 if (!SymTab) return false;
77 for (SymbolTable::const_iterator I = SymTab->begin(), E = SymTab->end();
79 if (I->second.begin() != I->second.end())
80 return true; // Found nonempty type plane!
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.
91 Function *Module::getOrInsertFunction(const std::string &Name,
92 const FunctionType *Ty) {
93 SymbolTable *SymTab = getSymbolTableSure();
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...
105 // getFunction - Look up the specified function in the module symbol table.
106 // If it does not exist, return null.
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...
112 return cast_or_null<Function>(SymTab->lookup(PointerType::get(Ty), Name));
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.
119 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
120 SymbolTable *ST = getSymbolTableSure();
122 if (ST->lookup(Type::TypeTy, Name)) return true; // Already in symtab...
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);
131 // getTypeName - If there is at least one entry in the symbol table for the
132 // specified type, return it.
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...
140 SymbolTable::type_const_iterator TI = ST->type_begin(Type::TypeTy);
141 SymbolTable::type_const_iterator TE = ST->type_end(Type::TypeTy);
143 while (TI != TE && TI->second != (const Value*)Ty)
146 if (TI != TE) // Must have found an entry!
148 return ""; // Must not have found anything...
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
160 void Module::dropAllReferences() {
161 for(Module::iterator I = begin(), E = end(); I != E; ++I)
162 I->dropAllReferences();
164 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
165 I->dropAllReferences();
167 // If there are any GlobalVariable references still out there, nuke them now.
168 // Since all references are hereby dropped, nothing could possibly reference
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();
177 // Since the table is empty, we can now delete it...
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();
187 GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
188 if (I != GVRefMap->Map.end()) return I->second;
190 ConstantPointerRef *Ref = new ConstantPointerRef(V);
191 GVRefMap->Map.insert(std::make_pair(V, Ref));
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;
202 // Remove the old entry...
203 GVRefMap->Map.erase(I);
205 // Insert the new entry...
206 GVRefMap->Map.insert(std::make_pair(NewGV, Ref));