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 | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 12 | #include "SymbolTableListTraitsImpl.h" |
| 13 | #include <algorithm> |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 14 | #include <map> |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 16 | Function *ilist_traits<Function>::createNode() { |
| 17 | return new Function(FunctionType::get(Type::VoidTy,std::vector<const Type*>(), |
| 18 | false), false); |
| 19 | } |
| 20 | GlobalVariable *ilist_traits<GlobalVariable>::createNode() { |
| 21 | return new GlobalVariable(Type::IntTy, false, false); |
| 22 | } |
| 23 | |
| 24 | iplist<Function> &ilist_traits<Function>::getList(Module *M) { |
| 25 | return M->getFunctionList(); |
| 26 | } |
| 27 | iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) { |
| 28 | return M->getGlobalList(); |
| 29 | } |
| 30 | |
| 31 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 32 | // are not in the public header file... |
| 33 | template SymbolTableListTraits<GlobalVariable, Module, Module>; |
| 34 | template SymbolTableListTraits<Function, Module, Module>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 36 | // Define the GlobalValueRefMap as a struct that wraps a map so that we don't |
| 37 | // have Module.h depend on <map> |
| 38 | // |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 39 | struct GlobalValueRefMap { |
| 40 | typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy; |
| 41 | typedef MapTy::iterator iterator; |
| 42 | std::map<GlobalValue*, ConstantPointerRef*> Map; |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 46 | Module::Module() { |
| 47 | FunctionList.setItemParent(this); |
| 48 | FunctionList.setParent(this); |
| 49 | GlobalList.setItemParent(this); |
| 50 | GlobalList.setParent(this); |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 51 | GVRefMap = 0; |
| 52 | SymTab = 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | Module::~Module() { |
| 56 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 57 | GlobalList.clear(); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 58 | GlobalList.setParent(0); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 59 | FunctionList.clear(); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 60 | FunctionList.setParent(0); |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 61 | delete SymTab; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame^] | 64 | // Module::dump() - Allow printing from debugger |
| 65 | void Module::dump() const { |
| 66 | print(std::cerr); |
| 67 | } |
| 68 | |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 69 | SymbolTable *Module::getSymbolTableSure() { |
| 70 | if (!SymTab) SymTab = new SymbolTable(0); |
| 71 | return SymTab; |
| 72 | } |
| 73 | |
| 74 | // hasSymbolTable() - Returns true if there is a symbol table allocated to |
| 75 | // this object AND if there is at least one name in it! |
| 76 | // |
| 77 | bool Module::hasSymbolTable() const { |
| 78 | if (!SymTab) return false; |
| 79 | |
| 80 | for (SymbolTable::const_iterator I = SymTab->begin(), E = SymTab->end(); |
| 81 | I != E; ++I) |
| 82 | if (I->second.begin() != I->second.end()) |
| 83 | return true; // Found nonempty type plane! |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 89 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 90 | // table. If it does not exist, add a prototype for the function and return |
| 91 | // it. This is nice because it allows most passes to get away with not handling |
| 92 | // the symbol table directly for this common task. |
| 93 | // |
| 94 | Function *Module::getOrInsertFunction(const std::string &Name, |
| 95 | const FunctionType *Ty) { |
| 96 | SymbolTable *SymTab = getSymbolTableSure(); |
| 97 | |
| 98 | // See if we have a definitions for the specified function already... |
| 99 | if (Value *V = SymTab->lookup(PointerType::get(Ty), Name)) { |
| 100 | return cast<Function>(V); // Yup, got it |
| 101 | } else { // Nope, add one |
| 102 | Function *New = new Function(Ty, false, Name); |
| 103 | FunctionList.push_back(New); |
| 104 | return New; // Return the new prototype... |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // getFunction - Look up the specified function in the module symbol table. |
| 109 | // If it does not exist, return null. |
| 110 | // |
| 111 | Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) { |
| 112 | SymbolTable *SymTab = getSymbolTable(); |
| 113 | if (SymTab == 0) return 0; // No symtab, no symbols... |
| 114 | |
| 115 | return cast_or_null<Function>(SymTab->lookup(PointerType::get(Ty), Name)); |
| 116 | } |
| 117 | |
Chris Lattner | 13ae72f | 2002-03-29 04:48:40 +0000 | [diff] [blame] | 118 | // addTypeName - Insert an entry in the symbol table mapping Str to Type. If |
| 119 | // there is already an entry for this name, true is returned and the symbol |
| 120 | // table is not modified. |
| 121 | // |
| 122 | bool Module::addTypeName(const std::string &Name, const Type *Ty) { |
| 123 | SymbolTable *ST = getSymbolTableSure(); |
| 124 | |
| 125 | if (ST->lookup(Type::TypeTy, Name)) return true; // Already in symtab... |
| 126 | |
| 127 | // Not in symbol table? Set the name with the Symtab as an argument so the |
| 128 | // type knows what to update... |
| 129 | ((Value*)Ty)->setName(Name, ST); |
| 130 | |
| 131 | return false; |
| 132 | } |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 134 | // getTypeName - If there is at least one entry in the symbol table for the |
| 135 | // specified type, return it. |
| 136 | // |
| 137 | std::string Module::getTypeName(const Type *Ty) { |
| 138 | const SymbolTable *ST = getSymbolTable(); |
| 139 | if (ST == 0) return ""; // No symbol table, must not have an entry... |
| 140 | if (ST->find(Type::TypeTy) == ST->end()) |
| 141 | return ""; // No names for types... |
| 142 | |
| 143 | SymbolTable::type_const_iterator TI = ST->type_begin(Type::TypeTy); |
| 144 | SymbolTable::type_const_iterator TE = ST->type_end(Type::TypeTy); |
| 145 | |
| 146 | while (TI != TE && TI->second != (const Value*)Ty) |
| 147 | ++TI; |
| 148 | |
| 149 | if (TI != TE) // Must have found an entry! |
| 150 | return TI->first; |
| 151 | return ""; // Must not have found anything... |
| 152 | } |
| 153 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 154 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame^] | 155 | // dropAllReferences() - This function causes all the subelementss to "let go" |
| 156 | // of all references that they are maintaining. This allows one to 'delete' a |
| 157 | // whole module at a time, even though there may be circular references... first |
| 158 | // all references are dropped, and all use counts go to zero. Then everything |
| 159 | // is delete'd for real. Note that no operations are valid on an object that |
| 160 | // has "dropped all references", except operator delete. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 161 | // |
| 162 | void Module::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 163 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 164 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 166 | for(Module::giterator I = gbegin(), E = gend(); I != E; ++I) |
| 167 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 168 | |
| 169 | // If there are any GlobalVariable references still out there, nuke them now. |
| 170 | // Since all references are hereby dropped, nothing could possibly reference |
| 171 | // them still. |
| 172 | if (GVRefMap) { |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 173 | for (GlobalValueRefMap::iterator I = GVRefMap->Map.begin(), |
| 174 | E = GVRefMap->Map.end(); I != E; ++I) { |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 175 | // Delete the ConstantPointerRef node... |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 176 | I->second->destroyConstant(); |
| 177 | } |
| 178 | |
| 179 | // Since the table is empty, we can now delete it... |
| 180 | delete GVRefMap; |
| 181 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 182 | } |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 184 | // Accessor for the underlying GlobalValRefMap... |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 185 | ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){ |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 186 | // Create ref map lazily on demand... |
| 187 | if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap(); |
| 188 | |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 189 | GlobalValueRefMap::iterator I = GVRefMap->Map.find(V); |
| 190 | if (I != GVRefMap->Map.end()) return I->second; |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 192 | ConstantPointerRef *Ref = new ConstantPointerRef(V); |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 193 | GVRefMap->Map.insert(std::make_pair(V, Ref)); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 194 | |
| 195 | return Ref; |
| 196 | } |
| 197 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 198 | void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) { |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 199 | GlobalValueRefMap::iterator I = GVRefMap->Map.find(OldGV); |
| 200 | assert(I != GVRefMap->Map.end() && |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 201 | "mutateConstantPointerRef; OldGV not in table!"); |
| 202 | ConstantPointerRef *Ref = I->second; |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 203 | |
| 204 | // Remove the old entry... |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 205 | GVRefMap->Map.erase(I); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 206 | |
| 207 | // Insert the new entry... |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 208 | GVRefMap->Map.insert(std::make_pair(NewGV, Ref)); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 209 | } |