Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Module.cpp - Implement the Module class ------------------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the Module class for the VMCore library. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 7 | #include "llvm/Module.h" |
Chris Lattner | 28bf86a | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 8 | #include "llvm/InstrTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 9 | #include "llvm/Constants.h" |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 10 | #include "llvm/DerivedTypes.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 11 | #include "Support/STLExtras.h" |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 12 | #include "Support/LeakDetector.h" |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 13 | #include "SymbolTableListTraitsImpl.h" |
| 14 | #include <algorithm> |
Chris Lattner | 0ae8e87 | 2003-08-31 00:19:28 +0000 | [diff] [blame^] | 15 | #include <cstdarg> |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 16 | #include <map> |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 18 | Function *ilist_traits<Function>::createNode() { |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 19 | FunctionType *FTy = |
| 20 | FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false); |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 21 | Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage); |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 22 | // This should not be garbage monitored. |
| 23 | LeakDetector::removeGarbageObject(Ret); |
| 24 | return Ret; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 25 | } |
| 26 | GlobalVariable *ilist_traits<GlobalVariable>::createNode() { |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 27 | GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, |
| 28 | GlobalValue::ExternalLinkage); |
Chris Lattner | d1e693f | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 29 | // This should not be garbage monitored. |
| 30 | LeakDetector::removeGarbageObject(Ret); |
| 31 | return Ret; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | iplist<Function> &ilist_traits<Function>::getList(Module *M) { |
| 35 | return M->getFunctionList(); |
| 36 | } |
| 37 | iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) { |
| 38 | return M->getGlobalList(); |
| 39 | } |
| 40 | |
| 41 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 42 | // are not in the public header file... |
| 43 | template SymbolTableListTraits<GlobalVariable, Module, Module>; |
| 44 | template SymbolTableListTraits<Function, Module, Module>; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 45 | |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 46 | // Define the GlobalValueRefMap as a struct that wraps a map so that we don't |
| 47 | // have Module.h depend on <map> |
| 48 | // |
Chris Lattner | 4228b5a | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 49 | struct GlobalValueRefMap { |
| 50 | typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy; |
| 51 | typedef MapTy::iterator iterator; |
| 52 | std::map<GlobalValue*, ConstantPointerRef*> Map; |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | |
Chris Lattner | 678c6a0 | 2003-04-22 18:02:04 +0000 | [diff] [blame] | 56 | Module::Module(const std::string &MID) |
Chris Lattner | eb5d3a1 | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 57 | : ModuleID(MID), Endian(AnyEndianness), PtrSize(AnyPointerSize) { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 58 | FunctionList.setItemParent(this); |
| 59 | FunctionList.setParent(this); |
| 60 | GlobalList.setItemParent(this); |
| 61 | GlobalList.setParent(this); |
Chris Lattner | 11aa477 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 62 | GVRefMap = 0; |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 63 | SymTab = new SymbolTable(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | Module::~Module() { |
| 67 | dropAllReferences(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 68 | GlobalList.clear(); |
Chris Lattner | 70cc339 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 69 | GlobalList.setParent(0); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 70 | FunctionList.clear(); |
Chris Lattner | 79df7c0 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 71 | FunctionList.setParent(0); |
Chris Lattner | 11aa477 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 72 | delete SymTab; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | 9da0715 | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 75 | // Module::dump() - Allow printing from debugger |
| 76 | void Module::dump() const { |
| 77 | print(std::cerr); |
| 78 | } |
| 79 | |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 80 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 81 | // table. If it does not exist, add a prototype for the function and return |
| 82 | // it. This is nice because it allows most passes to get away with not handling |
| 83 | // the symbol table directly for this common task. |
| 84 | // |
| 85 | Function *Module::getOrInsertFunction(const std::string &Name, |
| 86 | const FunctionType *Ty) { |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 87 | SymbolTable &SymTab = getSymbolTable(); |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 88 | |
| 89 | // See if we have a definitions for the specified function already... |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 90 | if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) { |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 91 | return cast<Function>(V); // Yup, got it |
| 92 | } else { // Nope, add one |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 93 | Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name); |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 94 | FunctionList.push_back(New); |
| 95 | return New; // Return the new prototype... |
| 96 | } |
| 97 | } |
| 98 | |
Chris Lattner | 0ae8e87 | 2003-08-31 00:19:28 +0000 | [diff] [blame^] | 99 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 100 | // table. If it does not exist, add a prototype for the function and return it. |
| 101 | // This version of the method takes a null terminated list of function |
| 102 | // arguments, which makes it easier for clients to use. |
| 103 | // |
| 104 | Function *Module::getOrInsertFunction(const std::string &Name, |
| 105 | const Type *RetTy, ...) { |
| 106 | va_list Args; |
| 107 | va_start(Args, RetTy); |
| 108 | |
| 109 | // Build the list of argument types... |
| 110 | std::vector<const Type*> ArgTys; |
| 111 | while (const Type *ArgTy = va_arg(Args, const Type*)) |
| 112 | ArgTys.push_back(ArgTy); |
| 113 | |
| 114 | va_end(Args); |
| 115 | |
| 116 | // Build the function type and chain to the other getOrInsertFunction... |
| 117 | return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false)); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 122 | // getFunction - Look up the specified function in the module symbol table. |
| 123 | // If it does not exist, return null. |
| 124 | // |
| 125 | Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) { |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 126 | SymbolTable &SymTab = getSymbolTable(); |
| 127 | return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name)); |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Chris Lattner | 68e5ed8 | 2002-03-29 04:48:40 +0000 | [diff] [blame] | 130 | // addTypeName - Insert an entry in the symbol table mapping Str to Type. If |
| 131 | // there is already an entry for this name, true is returned and the symbol |
| 132 | // table is not modified. |
| 133 | // |
| 134 | bool Module::addTypeName(const std::string &Name, const Type *Ty) { |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 135 | SymbolTable &ST = getSymbolTable(); |
Chris Lattner | 68e5ed8 | 2002-03-29 04:48:40 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 137 | if (ST.lookup(Type::TypeTy, Name)) return true; // Already in symtab... |
Chris Lattner | 68e5ed8 | 2002-03-29 04:48:40 +0000 | [diff] [blame] | 138 | |
| 139 | // Not in symbol table? Set the name with the Symtab as an argument so the |
| 140 | // type knows what to update... |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 141 | ((Value*)Ty)->setName(Name, &ST); |
Chris Lattner | 68e5ed8 | 2002-03-29 04:48:40 +0000 | [diff] [blame] | 142 | |
| 143 | return false; |
| 144 | } |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 145 | |
Chris Lattner | a9a93f3 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 146 | /// getMainFunction - This function looks up main efficiently. This is such a |
| 147 | /// common case, that it is a method in Module. If main cannot be found, a |
| 148 | /// null pointer is returned. |
| 149 | /// |
| 150 | Function *Module::getMainFunction() { |
| 151 | std::vector<const Type*> Params; |
| 152 | |
| 153 | // int main(void)... |
| 154 | if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, |
| 155 | Params, false))) |
| 156 | return F; |
| 157 | |
| 158 | // void main(void)... |
| 159 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 160 | Params, false))) |
| 161 | return F; |
| 162 | |
| 163 | Params.push_back(Type::IntTy); |
| 164 | |
| 165 | // int main(int argc)... |
| 166 | if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, |
| 167 | Params, false))) |
| 168 | return F; |
| 169 | |
| 170 | // void main(int argc)... |
| 171 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 172 | Params, false))) |
| 173 | return F; |
| 174 | |
| 175 | for (unsigned i = 0; i != 2; ++i) { // Check argv and envp |
| 176 | Params.push_back(PointerType::get(PointerType::get(Type::SByteTy))); |
| 177 | |
| 178 | // int main(int argc, char **argv)... |
| 179 | if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, |
| 180 | Params, false))) |
| 181 | return F; |
| 182 | |
| 183 | // void main(int argc, char **argv)... |
| 184 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 185 | Params, false))) |
| 186 | return F; |
| 187 | } |
| 188 | |
Chris Lattner | 9a641b4 | 2002-11-19 18:41:44 +0000 | [diff] [blame] | 189 | // Ok, try to find main the hard way... |
| 190 | return getNamedFunction("main"); |
| 191 | } |
| 192 | |
| 193 | /// getNamedFunction - Return the first function in the module with the |
| 194 | /// specified name, of arbitrary type. This method returns null if a function |
| 195 | /// with the specified name is not found. |
| 196 | /// |
| 197 | Function *Module::getNamedFunction(const std::string &Name) { |
| 198 | // Loop over all of the functions, looking for the function desired |
Chris Lattner | b6ede8a | 2003-07-23 20:21:30 +0000 | [diff] [blame] | 199 | Function *Found = 0; |
Chris Lattner | a9a93f3 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 200 | for (iterator I = begin(), E = end(); I != E; ++I) |
Chris Lattner | 9a641b4 | 2002-11-19 18:41:44 +0000 | [diff] [blame] | 201 | if (I->getName() == Name) |
Chris Lattner | b6ede8a | 2003-07-23 20:21:30 +0000 | [diff] [blame] | 202 | if (I->isExternal()) |
| 203 | Found = I; |
| 204 | else |
| 205 | return I; |
| 206 | return Found; // Non-external function not found... |
Chris Lattner | a9a93f3 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
| 210 | |
Chris Lattner | f33fa6f | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 211 | // getTypeName - If there is at least one entry in the symbol table for the |
| 212 | // specified type, return it. |
| 213 | // |
| 214 | std::string Module::getTypeName(const Type *Ty) { |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 215 | const SymbolTable &ST = getSymbolTable(); |
| 216 | if (ST.find(Type::TypeTy) == ST.end()) |
Chris Lattner | f33fa6f | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 217 | return ""; // No names for types... |
| 218 | |
Chris Lattner | 6e6026b | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 219 | SymbolTable::type_const_iterator TI = ST.type_begin(Type::TypeTy); |
| 220 | SymbolTable::type_const_iterator TE = ST.type_end(Type::TypeTy); |
Chris Lattner | f33fa6f | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 221 | |
| 222 | while (TI != TE && TI->second != (const Value*)Ty) |
| 223 | ++TI; |
| 224 | |
| 225 | if (TI != TE) // Must have found an entry! |
| 226 | return TI->first; |
| 227 | return ""; // Must not have found anything... |
| 228 | } |
| 229 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 9da0715 | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 231 | // dropAllReferences() - This function causes all the subelementss to "let go" |
| 232 | // of all references that they are maintaining. This allows one to 'delete' a |
| 233 | // whole module at a time, even though there may be circular references... first |
| 234 | // all references are dropped, and all use counts go to zero. Then everything |
| 235 | // is delete'd for real. Note that no operations are valid on an object that |
| 236 | // has "dropped all references", except operator delete. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 237 | // |
| 238 | void Module::dropAllReferences() { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 239 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 240 | I->dropAllReferences(); |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 242 | for(Module::giterator I = gbegin(), E = gend(); I != E; ++I) |
| 243 | I->dropAllReferences(); |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 244 | |
| 245 | // If there are any GlobalVariable references still out there, nuke them now. |
| 246 | // Since all references are hereby dropped, nothing could possibly reference |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 247 | // them still. Note that destroying all of the constant pointer refs will |
| 248 | // eventually cause the GVRefMap field to be set to null (by |
| 249 | // destroyConstantPointerRef, below). |
| 250 | // |
| 251 | while (GVRefMap) |
| 252 | // Delete the ConstantPointerRef node... |
| 253 | GVRefMap->Map.begin()->second->destroyConstant(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 254 | } |
Chris Lattner | 28bf86a | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 255 | |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 256 | // Accessor for the underlying GlobalValRefMap... |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 257 | ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){ |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 258 | // Create ref map lazily on demand... |
| 259 | if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap(); |
| 260 | |
Chris Lattner | 4228b5a | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 261 | GlobalValueRefMap::iterator I = GVRefMap->Map.find(V); |
| 262 | if (I != GVRefMap->Map.end()) return I->second; |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 263 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 264 | ConstantPointerRef *Ref = new ConstantPointerRef(V); |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 265 | GVRefMap->Map[V] = Ref; |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 266 | return Ref; |
| 267 | } |
| 268 | |
Chris Lattner | 41661fd | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 269 | void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) { |
| 270 | assert(GVRefMap && "No map allocated, but we have a CPR?"); |
| 271 | if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map... |
| 272 | assert(0 && "ConstantPointerRef not found in module CPR map!"); |
| 273 | |
| 274 | if (GVRefMap->Map.empty()) { // If the map is empty, delete it. |
| 275 | delete GVRefMap; |
| 276 | GVRefMap = 0; |
| 277 | } |
| 278 | } |
| 279 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 280 | void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) { |
Chris Lattner | 608f4b0 | 2003-05-15 19:37:21 +0000 | [diff] [blame] | 281 | assert(OldGV != NewGV && "Cannot mutate to the same global!"); |
Chris Lattner | 4228b5a | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 282 | GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV); |
| 283 | assert(I != GVRefMap->Map.end() && |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 284 | "mutateConstantPointerRef; OldGV not in table!"); |
| 285 | ConstantPointerRef *Ref = I->second; |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 286 | |
| 287 | // Remove the old entry... |
Chris Lattner | 4228b5a | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 288 | GVRefMap->Map.erase(I); |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 608f4b0 | 2003-05-15 19:37:21 +0000 | [diff] [blame] | 290 | // Check to see if a CPR already exists for NewGV |
| 291 | I = GVRefMap->Map.lower_bound(NewGV); |
| 292 | |
| 293 | if (I == GVRefMap->Map.end() || I->first != NewGV) { |
| 294 | // Insert the new entry... |
| 295 | GVRefMap->Map.insert(I, std::make_pair(NewGV, Ref)); |
| 296 | } else { |
| 297 | // Otherwise, an entry already exists for the current global value. |
| 298 | // Completely replace the old CPR with the existing one... |
| 299 | Ref->replaceAllUsesWith(I->second); |
| 300 | delete Ref; |
| 301 | } |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 302 | } |