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, GlobalValue::ExternalLinkage);
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,
27 GlobalValue::ExternalLinkage);
28 // This should not be garbage monitored.
29 LeakDetector::removeGarbageObject(Ret);
33 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
34 return M->getFunctionList();
36 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
37 return M->getGlobalList();
40 // Explicit instantiations of SymbolTableListTraits since some of the methods
41 // are not in the public header file...
42 template SymbolTableListTraits<GlobalVariable, Module, Module>;
43 template SymbolTableListTraits<Function, Module, Module>;
45 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
46 // have Module.h depend on <map>
48 struct GlobalValueRefMap {
49 typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy;
50 typedef MapTy::iterator iterator;
51 std::map<GlobalValue*, ConstantPointerRef*> Map;
55 Module::Module(const std::string &MID)
56 : ModuleID(MID), Endian(BigEndian), PtrSize(Pointer64) {
57 FunctionList.setItemParent(this);
58 FunctionList.setParent(this);
59 GlobalList.setItemParent(this);
60 GlobalList.setParent(this);
62 SymTab = new SymbolTable();
68 GlobalList.setParent(0);
70 FunctionList.setParent(0);
74 // Module::dump() - Allow printing from debugger
75 void Module::dump() const {
79 // getOrInsertFunction - Look up the specified function in the module symbol
80 // table. If it does not exist, add a prototype for the function and return
81 // it. This is nice because it allows most passes to get away with not handling
82 // the symbol table directly for this common task.
84 Function *Module::getOrInsertFunction(const std::string &Name,
85 const FunctionType *Ty) {
86 SymbolTable &SymTab = getSymbolTable();
88 // See if we have a definitions for the specified function already...
89 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
90 return cast<Function>(V); // Yup, got it
91 } else { // Nope, add one
92 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
93 FunctionList.push_back(New);
94 return New; // Return the new prototype...
98 // getFunction - Look up the specified function in the module symbol table.
99 // If it does not exist, return null.
101 Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
102 SymbolTable &SymTab = getSymbolTable();
103 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
106 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
107 // there is already an entry for this name, true is returned and the symbol
108 // table is not modified.
110 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
111 SymbolTable &ST = getSymbolTable();
113 if (ST.lookup(Type::TypeTy, Name)) return true; // Already in symtab...
115 // Not in symbol table? Set the name with the Symtab as an argument so the
116 // type knows what to update...
117 ((Value*)Ty)->setName(Name, &ST);
122 /// getMainFunction - This function looks up main efficiently. This is such a
123 /// common case, that it is a method in Module. If main cannot be found, a
124 /// null pointer is returned.
126 Function *Module::getMainFunction() {
127 std::vector<const Type*> Params;
130 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
134 // void main(void)...
135 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
139 Params.push_back(Type::IntTy);
141 // int main(int argc)...
142 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
146 // void main(int argc)...
147 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
151 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
152 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
154 // int main(int argc, char **argv)...
155 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
159 // void main(int argc, char **argv)...
160 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
165 // Ok, try to find main the hard way...
166 return getNamedFunction("main");
169 /// getNamedFunction - Return the first function in the module with the
170 /// specified name, of arbitrary type. This method returns null if a function
171 /// with the specified name is not found.
173 Function *Module::getNamedFunction(const std::string &Name) {
174 // Loop over all of the functions, looking for the function desired
175 for (iterator I = begin(), E = end(); I != E; ++I)
176 if (I->getName() == Name)
178 return 0; // function not found...
183 // getTypeName - If there is at least one entry in the symbol table for the
184 // specified type, return it.
186 std::string Module::getTypeName(const Type *Ty) {
187 const SymbolTable &ST = getSymbolTable();
188 if (ST.find(Type::TypeTy) == ST.end())
189 return ""; // No names for types...
191 SymbolTable::type_const_iterator TI = ST.type_begin(Type::TypeTy);
192 SymbolTable::type_const_iterator TE = ST.type_end(Type::TypeTy);
194 while (TI != TE && TI->second != (const Value*)Ty)
197 if (TI != TE) // Must have found an entry!
199 return ""; // Must not have found anything...
203 // dropAllReferences() - This function causes all the subelementss to "let go"
204 // of all references that they are maintaining. This allows one to 'delete' a
205 // whole module at a time, even though there may be circular references... first
206 // all references are dropped, and all use counts go to zero. Then everything
207 // is delete'd for real. Note that no operations are valid on an object that
208 // has "dropped all references", except operator delete.
210 void Module::dropAllReferences() {
211 for(Module::iterator I = begin(), E = end(); I != E; ++I)
212 I->dropAllReferences();
214 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
215 I->dropAllReferences();
217 // If there are any GlobalVariable references still out there, nuke them now.
218 // Since all references are hereby dropped, nothing could possibly reference
219 // them still. Note that destroying all of the constant pointer refs will
220 // eventually cause the GVRefMap field to be set to null (by
221 // destroyConstantPointerRef, below).
224 // Delete the ConstantPointerRef node...
225 GVRefMap->Map.begin()->second->destroyConstant();
228 // Accessor for the underlying GlobalValRefMap...
229 ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
230 // Create ref map lazily on demand...
231 if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
233 GlobalValueRefMap::iterator I = GVRefMap->Map.find(V);
234 if (I != GVRefMap->Map.end()) return I->second;
236 ConstantPointerRef *Ref = new ConstantPointerRef(V);
237 GVRefMap->Map[V] = Ref;
241 void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) {
242 assert(GVRefMap && "No map allocated, but we have a CPR?");
243 if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map...
244 assert(0 && "ConstantPointerRef not found in module CPR map!");
246 if (GVRefMap->Map.empty()) { // If the map is empty, delete it.
252 void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
253 assert(OldGV != NewGV && "Cannot mutate to the same global!");
254 GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV);
255 assert(I != GVRefMap->Map.end() &&
256 "mutateConstantPointerRef; OldGV not in table!");
257 ConstantPointerRef *Ref = I->second;
259 // Remove the old entry...
260 GVRefMap->Map.erase(I);
262 // Check to see if a CPR already exists for NewGV
263 I = GVRefMap->Map.lower_bound(NewGV);
265 if (I == GVRefMap->Map.end() || I->first != NewGV) {
266 // Insert the new entry...
267 GVRefMap->Map.insert(I, std::make_pair(NewGV, Ref));
269 // Otherwise, an entry already exists for the current global value.
270 // Completely replace the old CPR with the existing one...
271 Ref->replaceAllUsesWith(I->second);