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 | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 8 | #include "llvm/Function.h" |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 9 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 10 | #include "llvm/InstrTypes.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 11 | #include "llvm/Constants.h" |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 12 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 13 | #include "Support/STLExtras.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 14 | #include "SymbolTableListTraitsImpl.h" |
| 15 | #include <algorithm> |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 16 | #include <map> |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 18 | Function *ilist_traits<Function>::createNode() { |
| 19 | return new Function(FunctionType::get(Type::VoidTy,std::vector<const Type*>(), |
| 20 | false), false); |
| 21 | } |
| 22 | GlobalVariable *ilist_traits<GlobalVariable>::createNode() { |
| 23 | return new GlobalVariable(Type::IntTy, false, false); |
| 24 | } |
| 25 | |
| 26 | iplist<Function> &ilist_traits<Function>::getList(Module *M) { |
| 27 | return M->getFunctionList(); |
| 28 | } |
| 29 | iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) { |
| 30 | return M->getGlobalList(); |
| 31 | } |
| 32 | |
| 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>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 38 | // Define the GlobalValueRefMap as a struct that wraps a map so that we don't |
| 39 | // have Module.h depend on <map> |
| 40 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 41 | struct GlobalValueRefMap : public std::map<GlobalValue*, ConstantPointerRef*>{ |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 45 | Module::Module() { |
| 46 | FunctionList.setItemParent(this); |
| 47 | FunctionList.setParent(this); |
| 48 | GlobalList.setItemParent(this); |
| 49 | GlobalList.setParent(this); |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 50 | GVRefMap = 0; |
| 51 | SymTab = 0; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | Module::~Module() { |
| 55 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 56 | GlobalList.clear(); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 57 | GlobalList.setParent(0); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 58 | FunctionList.clear(); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 59 | FunctionList.setParent(0); |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 60 | delete SymTab; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 63 | SymbolTable *Module::getSymbolTableSure() { |
| 64 | if (!SymTab) SymTab = new SymbolTable(0); |
| 65 | return SymTab; |
| 66 | } |
| 67 | |
| 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! |
| 70 | // |
| 71 | bool Module::hasSymbolTable() const { |
| 72 | if (!SymTab) return false; |
| 73 | |
| 74 | for (SymbolTable::const_iterator I = SymTab->begin(), E = SymTab->end(); |
| 75 | I != E; ++I) |
| 76 | if (I->second.begin() != I->second.end()) |
| 77 | return true; // Found nonempty type plane! |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 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. |
| 87 | // |
| 88 | Function *Module::getOrInsertFunction(const std::string &Name, |
| 89 | const FunctionType *Ty) { |
| 90 | SymbolTable *SymTab = getSymbolTableSure(); |
| 91 | |
| 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... |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // getFunction - Look up the specified function in the module symbol table. |
| 103 | // If it does not exist, return null. |
| 104 | // |
| 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... |
| 108 | |
| 109 | return cast_or_null<Function>(SymTab->lookup(PointerType::get(Ty), Name)); |
| 110 | } |
| 111 | |
Chris Lattner | 13ae72f | 2002-03-29 04:48:40 +0000 | [diff] [blame] | 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. |
| 115 | // |
| 116 | bool Module::addTypeName(const std::string &Name, const Type *Ty) { |
| 117 | SymbolTable *ST = getSymbolTableSure(); |
| 118 | |
| 119 | if (ST->lookup(Type::TypeTy, Name)) return true; // Already in symtab... |
| 120 | |
| 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); |
| 124 | |
| 125 | return false; |
| 126 | } |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 128 | // getTypeName - If there is at least one entry in the symbol table for the |
| 129 | // specified type, return it. |
| 130 | // |
| 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... |
| 136 | |
| 137 | SymbolTable::type_const_iterator TI = ST->type_begin(Type::TypeTy); |
| 138 | SymbolTable::type_const_iterator TE = ST->type_end(Type::TypeTy); |
| 139 | |
| 140 | while (TI != TE && TI->second != (const Value*)Ty) |
| 141 | ++TI; |
| 142 | |
| 143 | if (TI != TE) // Must have found an entry! |
| 144 | return TI->first; |
| 145 | return ""; // Must not have found anything... |
| 146 | } |
| 147 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 148 | |
| 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 |
| 155 | // delete. |
| 156 | // |
| 157 | void Module::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 158 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 159 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame^] | 161 | for(Module::giterator I = gbegin(), E = gend(); I != E; ++I) |
| 162 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 163 | |
| 164 | // If there are any GlobalVariable references still out there, nuke them now. |
| 165 | // Since all references are hereby dropped, nothing could possibly reference |
| 166 | // them still. |
| 167 | if (GVRefMap) { |
| 168 | for (GlobalValueRefMap::iterator I = GVRefMap->begin(), E = GVRefMap->end(); |
| 169 | I != E; ++I) { |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 170 | // Delete the ConstantPointerRef node... |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 171 | I->second->destroyConstant(); |
| 172 | } |
| 173 | |
| 174 | // Since the table is empty, we can now delete it... |
| 175 | delete GVRefMap; |
| 176 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 177 | } |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 179 | // Accessor for the underlying GlobalValRefMap... |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 180 | ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){ |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 181 | // Create ref map lazily on demand... |
| 182 | if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap(); |
| 183 | |
| 184 | GlobalValueRefMap::iterator I = GVRefMap->find(V); |
| 185 | if (I != GVRefMap->end()) return I->second; |
| 186 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 187 | ConstantPointerRef *Ref = new ConstantPointerRef(V); |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 188 | GVRefMap->insert(std::make_pair(V, Ref)); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 189 | |
| 190 | return Ref; |
| 191 | } |
| 192 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 193 | void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) { |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 194 | GlobalValueRefMap::iterator I = GVRefMap->find(OldGV); |
| 195 | assert(I != GVRefMap->end() && |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 196 | "mutateConstantPointerRef; OldGV not in table!"); |
| 197 | ConstantPointerRef *Ref = I->second; |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 198 | |
| 199 | // Remove the old entry... |
| 200 | GVRefMap->erase(I); |
| 201 | |
| 202 | // Insert the new entry... |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 203 | GVRefMap->insert(std::make_pair(NewGV, Ref)); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 204 | } |