Chris Lattner | 2f7c963 | 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 | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 7 | #include "llvm/Module.h" |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 8 | #include "llvm/InstrTypes.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 9 | #include "llvm/Constants.h" |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 10 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 11 | #include "Support/STLExtras.h" |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 12 | #include "Support/LeakDetector.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 13 | #include "SymbolTableListTraitsImpl.h" |
| 14 | #include <algorithm> |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 15 | #include <map> |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 17 | Function *ilist_traits<Function>::createNode() { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 18 | FunctionType *FTy = |
| 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); |
| 23 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 24 | } |
| 25 | GlobalVariable *ilist_traits<GlobalVariable>::createNode() { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 26 | GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, false); |
| 27 | // This should not be garbage monitored. |
| 28 | LeakDetector::removeGarbageObject(Ret); |
| 29 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | iplist<Function> &ilist_traits<Function>::getList(Module *M) { |
| 33 | return M->getFunctionList(); |
| 34 | } |
| 35 | iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) { |
| 36 | return M->getGlobalList(); |
| 37 | } |
| 38 | |
| 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>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 44 | // Define the GlobalValueRefMap as a struct that wraps a map so that we don't |
| 45 | // have Module.h depend on <map> |
| 46 | // |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 47 | struct GlobalValueRefMap { |
| 48 | typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy; |
| 49 | typedef MapTy::iterator iterator; |
| 50 | std::map<GlobalValue*, ConstantPointerRef*> Map; |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 54 | Module::Module() { |
| 55 | FunctionList.setItemParent(this); |
| 56 | FunctionList.setParent(this); |
| 57 | GlobalList.setItemParent(this); |
| 58 | GlobalList.setParent(this); |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 59 | GVRefMap = 0; |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame^] | 60 | SymTab = new SymbolTable(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | Module::~Module() { |
| 64 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 65 | GlobalList.clear(); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 66 | GlobalList.setParent(0); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 67 | FunctionList.clear(); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 68 | FunctionList.setParent(0); |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 69 | delete SymTab; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 72 | // Module::dump() - Allow printing from debugger |
| 73 | void Module::dump() const { |
| 74 | print(std::cerr); |
| 75 | } |
| 76 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 77 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 78 | // table. If it does not exist, add a prototype for the function and return |
| 79 | // it. This is nice because it allows most passes to get away with not handling |
| 80 | // the symbol table directly for this common task. |
| 81 | // |
| 82 | Function *Module::getOrInsertFunction(const std::string &Name, |
| 83 | const FunctionType *Ty) { |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame^] | 84 | SymbolTable &SymTab = getSymbolTable(); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 85 | |
| 86 | // See if we have a definitions for the specified function already... |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame^] | 87 | if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) { |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 88 | return cast<Function>(V); // Yup, got it |
| 89 | } else { // Nope, add one |
| 90 | Function *New = new Function(Ty, false, Name); |
| 91 | FunctionList.push_back(New); |
| 92 | return New; // Return the new prototype... |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // getFunction - Look up the specified function in the module symbol table. |
| 97 | // If it does not exist, return null. |
| 98 | // |
| 99 | Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) { |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame^] | 100 | SymbolTable &SymTab = getSymbolTable(); |
| 101 | return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name)); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | 13ae72f | 2002-03-29 04:48:40 +0000 | [diff] [blame] | 104 | // addTypeName - Insert an entry in the symbol table mapping Str to Type. If |
| 105 | // there is already an entry for this name, true is returned and the symbol |
| 106 | // table is not modified. |
| 107 | // |
| 108 | bool Module::addTypeName(const std::string &Name, const Type *Ty) { |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame^] | 109 | SymbolTable &ST = getSymbolTable(); |
Chris Lattner | 13ae72f | 2002-03-29 04:48:40 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame^] | 111 | if (ST.lookup(Type::TypeTy, Name)) return true; // Already in symtab... |
Chris Lattner | 13ae72f | 2002-03-29 04:48:40 +0000 | [diff] [blame] | 112 | |
| 113 | // Not in symbol table? Set the name with the Symtab as an argument so the |
| 114 | // type knows what to update... |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame^] | 115 | ((Value*)Ty)->setName(Name, &ST); |
Chris Lattner | 13ae72f | 2002-03-29 04:48:40 +0000 | [diff] [blame] | 116 | |
| 117 | return false; |
| 118 | } |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 120 | /// getMainFunction - This function looks up main efficiently. This is such a |
| 121 | /// common case, that it is a method in Module. If main cannot be found, a |
| 122 | /// null pointer is returned. |
| 123 | /// |
| 124 | Function *Module::getMainFunction() { |
| 125 | std::vector<const Type*> Params; |
| 126 | |
| 127 | // int main(void)... |
| 128 | if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, |
| 129 | Params, false))) |
| 130 | return F; |
| 131 | |
| 132 | // void main(void)... |
| 133 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 134 | Params, false))) |
| 135 | return F; |
| 136 | |
| 137 | Params.push_back(Type::IntTy); |
| 138 | |
| 139 | // int main(int argc)... |
| 140 | if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, |
| 141 | Params, false))) |
| 142 | return F; |
| 143 | |
| 144 | // void main(int argc)... |
| 145 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 146 | Params, false))) |
| 147 | return F; |
| 148 | |
| 149 | for (unsigned i = 0; i != 2; ++i) { // Check argv and envp |
| 150 | Params.push_back(PointerType::get(PointerType::get(Type::SByteTy))); |
| 151 | |
| 152 | // int main(int argc, char **argv)... |
| 153 | if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, |
| 154 | Params, false))) |
| 155 | return F; |
| 156 | |
| 157 | // void main(int argc, char **argv)... |
| 158 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 159 | Params, false))) |
| 160 | return F; |
| 161 | } |
| 162 | |
Chris Lattner | b2e46c0 | 2002-11-19 18:41:44 +0000 | [diff] [blame] | 163 | // Ok, try to find main the hard way... |
| 164 | return getNamedFunction("main"); |
| 165 | } |
| 166 | |
| 167 | /// getNamedFunction - Return the first function in the module with the |
| 168 | /// specified name, of arbitrary type. This method returns null if a function |
| 169 | /// with the specified name is not found. |
| 170 | /// |
| 171 | Function *Module::getNamedFunction(const std::string &Name) { |
| 172 | // Loop over all of the functions, looking for the function desired |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 173 | for (iterator I = begin(), E = end(); I != E; ++I) |
Chris Lattner | b2e46c0 | 2002-11-19 18:41:44 +0000 | [diff] [blame] | 174 | if (I->getName() == Name) |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 175 | return I; |
Chris Lattner | b2e46c0 | 2002-11-19 18:41:44 +0000 | [diff] [blame] | 176 | return 0; // function not found... |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | |
| 180 | |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 181 | // getTypeName - If there is at least one entry in the symbol table for the |
| 182 | // specified type, return it. |
| 183 | // |
| 184 | std::string Module::getTypeName(const Type *Ty) { |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame^] | 185 | const SymbolTable &ST = getSymbolTable(); |
| 186 | if (ST.find(Type::TypeTy) == ST.end()) |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 187 | return ""; // No names for types... |
| 188 | |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame^] | 189 | SymbolTable::type_const_iterator TI = ST.type_begin(Type::TypeTy); |
| 190 | SymbolTable::type_const_iterator TE = ST.type_end(Type::TypeTy); |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 191 | |
| 192 | while (TI != TE && TI->second != (const Value*)Ty) |
| 193 | ++TI; |
| 194 | |
| 195 | if (TI != TE) // Must have found an entry! |
| 196 | return TI->first; |
| 197 | return ""; // Must not have found anything... |
| 198 | } |
| 199 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 200 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 201 | // dropAllReferences() - This function causes all the subelementss to "let go" |
| 202 | // of all references that they are maintaining. This allows one to 'delete' a |
| 203 | // whole module at a time, even though there may be circular references... first |
| 204 | // all references are dropped, and all use counts go to zero. Then everything |
| 205 | // is delete'd for real. Note that no operations are valid on an object that |
| 206 | // has "dropped all references", except operator delete. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 207 | // |
| 208 | void Module::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 209 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 210 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 212 | for(Module::giterator I = gbegin(), E = gend(); I != E; ++I) |
| 213 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 214 | |
| 215 | // If there are any GlobalVariable references still out there, nuke them now. |
| 216 | // Since all references are hereby dropped, nothing could possibly reference |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 217 | // them still. Note that destroying all of the constant pointer refs will |
| 218 | // eventually cause the GVRefMap field to be set to null (by |
| 219 | // destroyConstantPointerRef, below). |
| 220 | // |
| 221 | while (GVRefMap) |
| 222 | // Delete the ConstantPointerRef node... |
| 223 | GVRefMap->Map.begin()->second->destroyConstant(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 224 | } |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 226 | // Accessor for the underlying GlobalValRefMap... |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 227 | ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){ |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 228 | // Create ref map lazily on demand... |
| 229 | if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap(); |
| 230 | |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 231 | GlobalValueRefMap::iterator I = GVRefMap->Map.find(V); |
| 232 | if (I != GVRefMap->Map.end()) return I->second; |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 234 | ConstantPointerRef *Ref = new ConstantPointerRef(V); |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 235 | GVRefMap->Map[V] = Ref; |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 236 | return Ref; |
| 237 | } |
| 238 | |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 239 | void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) { |
| 240 | assert(GVRefMap && "No map allocated, but we have a CPR?"); |
| 241 | if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map... |
| 242 | assert(0 && "ConstantPointerRef not found in module CPR map!"); |
| 243 | |
| 244 | if (GVRefMap->Map.empty()) { // If the map is empty, delete it. |
| 245 | delete GVRefMap; |
| 246 | GVRefMap = 0; |
| 247 | } |
| 248 | } |
| 249 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 250 | void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) { |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 251 | GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV); |
| 252 | assert(I != GVRefMap->Map.end() && |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 253 | "mutateConstantPointerRef; OldGV not in table!"); |
| 254 | ConstantPointerRef *Ref = I->second; |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 255 | |
| 256 | // Remove the old entry... |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 257 | GVRefMap->Map.erase(I); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 258 | |
| 259 | // Insert the new entry... |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 260 | GVRefMap->Map.insert(std::make_pair(NewGV, Ref)); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 261 | } |