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