Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Module.cpp - Implement the Module class ---------------------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the Module class for the VMCore library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 15 | #include "llvm/InstrTypes.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5de2204 | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 18 | #include "Support/STLExtras.h" |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 19 | #include "Support/LeakDetector.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 20 | #include "SymbolTableListTraitsImpl.h" |
| 21 | #include <algorithm> |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 22 | #include <cstdarg> |
Reid Spencer | 8baf8e2 | 2004-07-04 11:55:37 +0000 | [diff] [blame] | 23 | #include <iostream> |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 24 | #include <map> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
Misha Brukman | 3bcead7 | 2004-04-21 18:27:56 +0000 | [diff] [blame] | 28 | // Methods to implement the globals and functions lists. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 29 | // |
| 30 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 31 | Function *ilist_traits<Function>::createNode() { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 32 | FunctionType *FTy = |
| 33 | FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false); |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 34 | Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 35 | // This should not be garbage monitored. |
| 36 | LeakDetector::removeGarbageObject(Ret); |
| 37 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 38 | } |
| 39 | GlobalVariable *ilist_traits<GlobalVariable>::createNode() { |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 40 | GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false, |
| 41 | GlobalValue::ExternalLinkage); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 42 | // This should not be garbage monitored. |
| 43 | LeakDetector::removeGarbageObject(Ret); |
| 44 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | iplist<Function> &ilist_traits<Function>::getList(Module *M) { |
| 48 | return M->getFunctionList(); |
| 49 | } |
| 50 | iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) { |
| 51 | return M->getGlobalList(); |
| 52 | } |
| 53 | |
| 54 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
| 55 | // are not in the public header file... |
Chris Lattner | 41baa98 | 2003-11-05 05:15:42 +0000 | [diff] [blame] | 56 | template class SymbolTableListTraits<GlobalVariable, Module, Module>; |
| 57 | template class SymbolTableListTraits<Function, Module, Module>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 59 | // Define the GlobalValueRefMap as a struct that wraps a map so that we don't |
| 60 | // have Module.h depend on <map> |
| 61 | // |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 62 | namespace llvm { |
| 63 | struct GlobalValueRefMap { |
| 64 | typedef std::map<GlobalValue*, ConstantPointerRef*> MapTy; |
| 65 | typedef MapTy::iterator iterator; |
| 66 | std::map<GlobalValue*, ConstantPointerRef*> Map; |
| 67 | }; |
| 68 | } |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 70 | //===----------------------------------------------------------------------===// |
| 71 | // Primitive Module methods. |
| 72 | // |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 73 | |
Chris Lattner | c3f6e00 | 2003-04-22 18:02:04 +0000 | [diff] [blame] | 74 | Module::Module(const std::string &MID) |
Chris Lattner | 8068e0c | 2003-08-24 13:48:48 +0000 | [diff] [blame] | 75 | : ModuleID(MID), Endian(AnyEndianness), PtrSize(AnyPointerSize) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 76 | FunctionList.setItemParent(this); |
| 77 | FunctionList.setParent(this); |
| 78 | GlobalList.setItemParent(this); |
| 79 | GlobalList.setParent(this); |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 80 | GVRefMap = 0; |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 81 | SymTab = new SymbolTable(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | Module::~Module() { |
| 85 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 86 | GlobalList.clear(); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 87 | GlobalList.setParent(0); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 88 | FunctionList.clear(); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 89 | FunctionList.setParent(0); |
Chris Lattner | 2c8ff63 | 2002-04-28 04:51:51 +0000 | [diff] [blame] | 90 | delete SymTab; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 93 | // Module::dump() - Allow printing from debugger |
| 94 | void Module::dump() const { |
| 95 | print(std::cerr); |
| 96 | } |
| 97 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 98 | //===----------------------------------------------------------------------===// |
| 99 | // Methods for easy access to the functions in the module. |
| 100 | // |
| 101 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 102 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 103 | // table. If it does not exist, add a prototype for the function and return |
| 104 | // it. This is nice because it allows most passes to get away with not handling |
| 105 | // the symbol table directly for this common task. |
| 106 | // |
| 107 | Function *Module::getOrInsertFunction(const std::string &Name, |
| 108 | const FunctionType *Ty) { |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 109 | SymbolTable &SymTab = getSymbolTable(); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 110 | |
| 111 | // See if we have a definitions for the specified function already... |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 112 | if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) { |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 113 | return cast<Function>(V); // Yup, got it |
| 114 | } else { // Nope, add one |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 115 | Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 116 | FunctionList.push_back(New); |
| 117 | return New; // Return the new prototype... |
| 118 | } |
| 119 | } |
| 120 | |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 121 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 122 | // table. If it does not exist, add a prototype for the function and return it. |
| 123 | // This version of the method takes a null terminated list of function |
| 124 | // arguments, which makes it easier for clients to use. |
| 125 | // |
| 126 | Function *Module::getOrInsertFunction(const std::string &Name, |
| 127 | const Type *RetTy, ...) { |
| 128 | va_list Args; |
| 129 | va_start(Args, RetTy); |
| 130 | |
| 131 | // Build the list of argument types... |
| 132 | std::vector<const Type*> ArgTys; |
| 133 | while (const Type *ArgTy = va_arg(Args, const Type*)) |
| 134 | ArgTys.push_back(ArgTy); |
| 135 | |
| 136 | va_end(Args); |
| 137 | |
| 138 | // Build the function type and chain to the other getOrInsertFunction... |
| 139 | return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false)); |
| 140 | } |
| 141 | |
| 142 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 143 | // getFunction - Look up the specified function in the module symbol table. |
| 144 | // If it does not exist, return null. |
| 145 | // |
| 146 | Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) { |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 147 | SymbolTable &SymTab = getSymbolTable(); |
| 148 | return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name)); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 152 | /// getMainFunction - This function looks up main efficiently. This is such a |
| 153 | /// common case, that it is a method in Module. If main cannot be found, a |
| 154 | /// null pointer is returned. |
| 155 | /// |
| 156 | Function *Module::getMainFunction() { |
| 157 | std::vector<const Type*> Params; |
| 158 | |
| 159 | // int main(void)... |
| 160 | if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, |
| 161 | Params, false))) |
| 162 | return F; |
| 163 | |
| 164 | // void main(void)... |
| 165 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 166 | Params, false))) |
| 167 | return F; |
| 168 | |
| 169 | Params.push_back(Type::IntTy); |
| 170 | |
| 171 | // int main(int argc)... |
| 172 | if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, |
| 173 | Params, false))) |
| 174 | return F; |
| 175 | |
| 176 | // void main(int argc)... |
| 177 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 178 | Params, false))) |
| 179 | return F; |
| 180 | |
| 181 | for (unsigned i = 0; i != 2; ++i) { // Check argv and envp |
| 182 | Params.push_back(PointerType::get(PointerType::get(Type::SByteTy))); |
| 183 | |
| 184 | // int main(int argc, char **argv)... |
| 185 | if (Function *F = getFunction("main", FunctionType::get(Type::IntTy, |
| 186 | Params, false))) |
| 187 | return F; |
| 188 | |
| 189 | // void main(int argc, char **argv)... |
| 190 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 191 | Params, false))) |
| 192 | return F; |
| 193 | } |
| 194 | |
Chris Lattner | b2e46c0 | 2002-11-19 18:41:44 +0000 | [diff] [blame] | 195 | // Ok, try to find main the hard way... |
| 196 | return getNamedFunction("main"); |
| 197 | } |
| 198 | |
| 199 | /// getNamedFunction - Return the first function in the module with the |
| 200 | /// specified name, of arbitrary type. This method returns null if a function |
| 201 | /// with the specified name is not found. |
| 202 | /// |
| 203 | Function *Module::getNamedFunction(const std::string &Name) { |
| 204 | // Loop over all of the functions, looking for the function desired |
Chris Lattner | 4b4dacd | 2003-07-23 20:21:30 +0000 | [diff] [blame] | 205 | Function *Found = 0; |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 206 | for (iterator I = begin(), E = end(); I != E; ++I) |
Chris Lattner | b2e46c0 | 2002-11-19 18:41:44 +0000 | [diff] [blame] | 207 | if (I->getName() == Name) |
Chris Lattner | 4b4dacd | 2003-07-23 20:21:30 +0000 | [diff] [blame] | 208 | if (I->isExternal()) |
| 209 | Found = I; |
| 210 | else |
| 211 | return I; |
| 212 | return Found; // Non-external function not found... |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
| 216 | // Methods for easy access to the global variables in the module. |
| 217 | // |
| 218 | |
| 219 | /// getGlobalVariable - Look up the specified global variable in the module |
| 220 | /// symbol table. If it does not exist, return null. Note that this only |
| 221 | /// returns a global variable if it does not have internal linkage. The type |
| 222 | /// argument should be the underlying type of the global, ie, it should not |
| 223 | /// have the top-level PointerType, which represents the address of the |
| 224 | /// global. |
| 225 | /// |
| 226 | GlobalVariable *Module::getGlobalVariable(const std::string &Name, |
| 227 | const Type *Ty) { |
| 228 | if (Value *V = getSymbolTable().lookup(PointerType::get(Ty), Name)) { |
| 229 | GlobalVariable *Result = cast<GlobalVariable>(V); |
| 230 | if (!Result->hasInternalLinkage()) |
| 231 | return Result; |
| 232 | } |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | |
| 237 | |
| 238 | //===----------------------------------------------------------------------===// |
| 239 | // Methods for easy access to the types in the module. |
| 240 | // |
| 241 | |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 242 | |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 243 | // addTypeName - Insert an entry in the symbol table mapping Str to Type. If |
| 244 | // there is already an entry for this name, true is returned and the symbol |
| 245 | // table is not modified. |
| 246 | // |
| 247 | bool Module::addTypeName(const std::string &Name, const Type *Ty) { |
| 248 | SymbolTable &ST = getSymbolTable(); |
| 249 | |
Reid Spencer | abb6f00 | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 250 | if (ST.lookupType(Name)) return true; // Already in symtab... |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 251 | |
| 252 | // Not in symbol table? Set the name with the Symtab as an argument so the |
| 253 | // type knows what to update... |
Reid Spencer | f9776c3 | 2004-07-10 16:37:42 +0000 | [diff] [blame^] | 254 | ST.insert(Name, Ty); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 255 | |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | /// getTypeByName - Return the type with the specified name in this module, or |
| 260 | /// null if there is none by that name. |
| 261 | const Type *Module::getTypeByName(const std::string &Name) const { |
| 262 | const SymbolTable &ST = getSymbolTable(); |
Reid Spencer | abb6f00 | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 263 | return cast_or_null<Type>(ST.lookupType(Name)); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 264 | } |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 266 | // getTypeName - If there is at least one entry in the symbol table for the |
| 267 | // specified type, return it. |
| 268 | // |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 269 | std::string Module::getTypeName(const Type *Ty) const { |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 270 | const SymbolTable &ST = getSymbolTable(); |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 271 | |
Reid Spencer | abb6f00 | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 272 | SymbolTable::type_const_iterator TI = ST.type_begin(); |
| 273 | SymbolTable::type_const_iterator TE = ST.type_end(); |
| 274 | if ( TI == TE ) return ""; // No names for types |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 275 | |
Reid Spencer | abb6f00 | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 276 | while (TI != TE && TI->second != Ty) |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 277 | ++TI; |
| 278 | |
| 279 | if (TI != TE) // Must have found an entry! |
| 280 | return TI->first; |
| 281 | return ""; // Must not have found anything... |
| 282 | } |
| 283 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 285 | //===----------------------------------------------------------------------===// |
| 286 | // Other module related stuff. |
| 287 | // |
| 288 | |
| 289 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 290 | // dropAllReferences() - This function causes all the subelementss to "let go" |
| 291 | // of all references that they are maintaining. This allows one to 'delete' a |
| 292 | // whole module at a time, even though there may be circular references... first |
| 293 | // all references are dropped, and all use counts go to zero. Then everything |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 294 | // is deleted for real. Note that no operations are valid on an object that |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 295 | // has "dropped all references", except operator delete. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 296 | // |
| 297 | void Module::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 298 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 299 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 301 | for(Module::giterator I = gbegin(), E = gend(); I != E; ++I) |
| 302 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 303 | |
| 304 | // If there are any GlobalVariable references still out there, nuke them now. |
| 305 | // Since all references are hereby dropped, nothing could possibly reference |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 306 | // them still. Note that destroying all of the constant pointer refs will |
| 307 | // eventually cause the GVRefMap field to be set to null (by |
| 308 | // destroyConstantPointerRef, below). |
| 309 | // |
| 310 | while (GVRefMap) |
| 311 | // Delete the ConstantPointerRef node... |
| 312 | GVRefMap->Map.begin()->second->destroyConstant(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 313 | } |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 315 | // Accessor for the underlying GlobalValRefMap... |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 316 | ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){ |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 317 | // Create ref map lazily on demand... |
| 318 | if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap(); |
| 319 | |
Chris Lattner | 7d9a14d | 2002-08-12 20:23:29 +0000 | [diff] [blame] | 320 | GlobalValueRefMap::iterator I = GVRefMap->Map.find(V); |
| 321 | if (I != GVRefMap->Map.end()) return I->second; |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 323 | ConstantPointerRef *Ref = new ConstantPointerRef(V); |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 324 | GVRefMap->Map[V] = Ref; |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 325 | return Ref; |
| 326 | } |
| 327 | |
Chris Lattner | 0c6e0b9 | 2002-08-18 00:40:04 +0000 | [diff] [blame] | 328 | void Module::destroyConstantPointerRef(ConstantPointerRef *CPR) { |
| 329 | assert(GVRefMap && "No map allocated, but we have a CPR?"); |
| 330 | if (!GVRefMap->Map.erase(CPR->getValue())) // Remove it from the map... |
| 331 | assert(0 && "ConstantPointerRef not found in module CPR map!"); |
| 332 | |
| 333 | if (GVRefMap->Map.empty()) { // If the map is empty, delete it. |
| 334 | delete GVRefMap; |
| 335 | GVRefMap = 0; |
| 336 | } |
| 337 | } |