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 : public std::map<GlobalValue*, ConstantPointerRef*>{
46 FunctionList.setItemParent(this);
47 FunctionList.setParent(this);
48 GlobalList.setItemParent(this);
49 GlobalList.setParent(this);
57 GlobalList.setParent(0);
59 FunctionList.setParent(0);
63 SymbolTable *Module::getSymbolTableSure() {
64 if (!SymTab) SymTab = new SymbolTable(0);
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!
71 bool Module::hasSymbolTable() const {
72 if (!SymTab) return false;
74 for (SymbolTable::const_iterator I = SymTab->begin(), E = SymTab->end();
76 if (I->second.begin() != I->second.end())
77 return true; // Found nonempty type plane!
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.
88 Function *Module::getOrInsertFunction(const std::string &Name,
89 const FunctionType *Ty) {
90 SymbolTable *SymTab = getSymbolTableSure();
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...
102 // getFunction - Look up the specified function in the module symbol table.
103 // If it does not exist, return null.
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...
109 return cast_or_null<Function>(SymTab->lookup(PointerType::get(Ty), Name));
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.
116 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
117 SymbolTable *ST = getSymbolTableSure();
119 if (ST->lookup(Type::TypeTy, Name)) return true; // Already in symtab...
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);
128 // getTypeName - If there is at least one entry in the symbol table for the
129 // specified type, return it.
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...
137 SymbolTable::type_const_iterator TI = ST->type_begin(Type::TypeTy);
138 SymbolTable::type_const_iterator TE = ST->type_end(Type::TypeTy);
140 while (TI != TE && TI->second != (const Value*)Ty)
143 if (TI != TE) // Must have found an entry!
145 return ""; // Must not have found anything...
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
157 void Module::dropAllReferences() {
158 for(Module::iterator I = begin(), E = end(); I != E; ++I)
159 I->dropAllReferences();
161 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
162 I->dropAllReferences();
164 // If there are any GlobalVariable references still out there, nuke them now.
165 // Since all references are hereby dropped, nothing could possibly reference
168 for (GlobalValueRefMap::iterator I = GVRefMap->begin(), E = GVRefMap->end();
170 // Delete the ConstantPointerRef node...
171 I->second->destroyConstant();
174 // Since the table is empty, we can now delete it...
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();
184 GlobalValueRefMap::iterator I = GVRefMap->find(V);
185 if (I != GVRefMap->end()) return I->second;
187 ConstantPointerRef *Ref = new ConstantPointerRef(V);
188 GVRefMap->insert(std::make_pair(V, Ref));
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;
199 // Remove the old entry...
202 // Insert the new entry...
203 GVRefMap->insert(std::make_pair(NewGV, Ref));