Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Module.cpp - Implement the Module class ---------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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" |
Owen Anderson | 6773d38 | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 22 | #include "SymbolTableListTraitsImpl.h" |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 23 | #include "llvm/TypeSymbolTable.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 25 | #include <cstdarg> |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 26 | #include <cstdlib> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Misha Brukman | 3bcead7 | 2004-04-21 18:27:56 +0000 | [diff] [blame] | 30 | // Methods to implement the globals and functions lists. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 31 | // |
| 32 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 33 | GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() { |
Chris Lattner | 46b5c64 | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 34 | GlobalVariable *Ret = new GlobalVariable(Type::getInt32Ty(getGlobalContext()), |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 35 | false, GlobalValue::ExternalLinkage); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 36 | // This should not be garbage monitored. |
| 37 | LeakDetector::removeGarbageObject(Ret); |
| 38 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 39 | } |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 40 | GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 41 | GlobalAlias *Ret = new GlobalAlias(Type::getInt32Ty(getGlobalContext()), |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 42 | GlobalValue::ExternalLinkage); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 43 | // This should not be garbage monitored. |
| 44 | LeakDetector::removeGarbageObject(Ret); |
| 45 | return Ret; |
| 46 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 48 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 49 | // are not in the public header file. |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 50 | template class SymbolTableListTraits<GlobalVariable, Module>; |
| 51 | template class SymbolTableListTraits<Function, Module>; |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 52 | template class SymbolTableListTraits<GlobalAlias, Module>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | // Primitive Module methods. |
| 56 | // |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 57 | |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 58 | Module::Module(const StringRef &MID, LLVMContext& C) |
Owen Anderson | 6773d38 | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 59 | : Context(C), ModuleID(MID), DataLayout("") { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 60 | ValSymTab = new ValueSymbolTable(); |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 61 | TypeSymTab = new TypeSymbolTable(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | Module::~Module() { |
| 65 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 66 | GlobalList.clear(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 67 | FunctionList.clear(); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 68 | AliasList.clear(); |
Reid Spencer | a0b05b3 | 2004-07-25 18:08:57 +0000 | [diff] [blame] | 69 | LibraryList.clear(); |
Devang Patel | 18dfdc9 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 70 | NamedMDList.clear(); |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 71 | delete ValSymTab; |
| 72 | delete TypeSymTab; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 75 | /// Target endian information... |
| 76 | Module::Endianness Module::getEndianness() const { |
| 77 | std::string temp = DataLayout; |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 78 | Module::Endianness ret = AnyEndianness; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 79 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 80 | while (!temp.empty()) { |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 81 | std::string token = getToken(temp, "-"); |
| 82 | |
| 83 | if (token[0] == 'e') { |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 84 | ret = LittleEndian; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 85 | } else if (token[0] == 'E') { |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 86 | ret = BigEndian; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 90 | return ret; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 93 | /// Target Pointer Size information... |
| 94 | Module::PointerSize Module::getPointerSize() const { |
| 95 | std::string temp = DataLayout; |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 96 | Module::PointerSize ret = AnyPointerSize; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 97 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 98 | while (!temp.empty()) { |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 99 | std::string token = getToken(temp, "-"); |
| 100 | char signal = getToken(token, ":")[0]; |
| 101 | |
| 102 | if (signal == 'p') { |
| 103 | int size = atoi(getToken(token, ":").c_str()); |
| 104 | if (size == 32) |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 105 | ret = Pointer32; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 106 | else if (size == 64) |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 107 | ret = Pointer64; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 111 | return ret; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 114 | /// getNamedValue - Return the first global value in the module with |
| 115 | /// the specified name, of arbitrary type. This method returns null |
| 116 | /// if a global with the specified name is not found. |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 117 | GlobalValue *Module::getNamedValue(const StringRef &Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 118 | return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name)); |
| 119 | } |
| 120 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 121 | //===----------------------------------------------------------------------===// |
| 122 | // Methods for easy access to the functions in the module. |
| 123 | // |
| 124 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 125 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 126 | // table. If it does not exist, add a prototype for the function and return |
| 127 | // it. This is nice because it allows most passes to get away with not handling |
| 128 | // the symbol table directly for this common task. |
| 129 | // |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 130 | Constant *Module::getOrInsertFunction(const StringRef &Name, |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 131 | const FunctionType *Ty, |
| 132 | AttrListPtr AttributeList) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 133 | // See if we have a definition for the specified function already. |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 134 | GlobalValue *F = getNamedValue(Name); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 135 | if (F == 0) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 136 | // Nope, add it |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 137 | Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 138 | if (!New->isIntrinsic()) // Intrinsics get attrs set on construction |
| 139 | New->setAttributes(AttributeList); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 140 | FunctionList.push_back(New); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 141 | return New; // Return the new prototype. |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 143 | |
| 144 | // Okay, the function exists. Does it have externally visible linkage? |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 145 | if (F->hasLocalLinkage()) { |
Chris Lattner | 42e983e | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 146 | // Clear the function's name. |
| 147 | F->setName(""); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 148 | // Retry, now there won't be a conflict. |
Chris Lattner | 42e983e | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 149 | Constant *NewF = getOrInsertFunction(Name, Ty); |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 150 | F->setName(Name); |
Chris Lattner | 42e983e | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 151 | return NewF; |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // If the function exists but has the wrong type, return a bitcast to the |
| 155 | // right type. |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 156 | if (F->getType() != PointerType::getUnqual(Ty)) |
| 157 | return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 158 | |
| 159 | // Otherwise, we just found the existing function or a prototype. |
| 160 | return F; |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 163 | Constant *Module::getOrInsertTargetIntrinsic(const StringRef &Name, |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 164 | const FunctionType *Ty, |
| 165 | AttrListPtr AttributeList) { |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 166 | // See if we have a definition for the specified function already. |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 167 | GlobalValue *F = getNamedValue(Name); |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 168 | if (F == 0) { |
| 169 | // Nope, add it |
| 170 | Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); |
| 171 | New->setAttributes(AttributeList); |
| 172 | FunctionList.push_back(New); |
| 173 | return New; // Return the new prototype. |
| 174 | } |
| 175 | |
| 176 | // Otherwise, we just found the existing function or a prototype. |
| 177 | return F; |
| 178 | } |
| 179 | |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 180 | Constant *Module::getOrInsertFunction(const StringRef &Name, |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 181 | const FunctionType *Ty) { |
| 182 | AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0); |
| 183 | return getOrInsertFunction(Name, Ty, AttributeList); |
| 184 | } |
| 185 | |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 186 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 187 | // table. If it does not exist, add a prototype for the function and return it. |
| 188 | // This version of the method takes a null terminated list of function |
| 189 | // arguments, which makes it easier for clients to use. |
| 190 | // |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 191 | Constant *Module::getOrInsertFunction(const StringRef &Name, |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 192 | AttrListPtr AttributeList, |
| 193 | const Type *RetTy, ...) { |
| 194 | va_list Args; |
| 195 | va_start(Args, RetTy); |
| 196 | |
| 197 | // Build the list of argument types... |
| 198 | std::vector<const Type*> ArgTys; |
| 199 | while (const Type *ArgTy = va_arg(Args, const Type*)) |
| 200 | ArgTys.push_back(ArgTy); |
| 201 | |
| 202 | va_end(Args); |
| 203 | |
| 204 | // Build the function type and chain to the other getOrInsertFunction... |
Owen Anderson | 785c56c | 2009-07-08 23:50:31 +0000 | [diff] [blame] | 205 | return getOrInsertFunction(Name, |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 206 | FunctionType::get(RetTy, ArgTys, false), |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 207 | AttributeList); |
| 208 | } |
| 209 | |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 210 | Constant *Module::getOrInsertFunction(const StringRef &Name, |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 211 | const Type *RetTy, ...) { |
| 212 | va_list Args; |
| 213 | va_start(Args, RetTy); |
| 214 | |
| 215 | // Build the list of argument types... |
| 216 | std::vector<const Type*> ArgTys; |
| 217 | while (const Type *ArgTy = va_arg(Args, const Type*)) |
| 218 | ArgTys.push_back(ArgTy); |
| 219 | |
| 220 | va_end(Args); |
| 221 | |
| 222 | // Build the function type and chain to the other getOrInsertFunction... |
Owen Anderson | 785c56c | 2009-07-08 23:50:31 +0000 | [diff] [blame] | 223 | return getOrInsertFunction(Name, |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 224 | FunctionType::get(RetTy, ArgTys, false), |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 225 | AttrListPtr::get((AttributeWithIndex *)0, 0)); |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 228 | // getFunction - Look up the specified function in the module symbol table. |
| 229 | // If it does not exist, return null. |
| 230 | // |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 231 | Function *Module::getFunction(const StringRef &Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 232 | return dyn_cast_or_null<Function>(getNamedValue(Name)); |
Chris Lattner | e43649f | 2008-06-27 21:09:10 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 235 | //===----------------------------------------------------------------------===// |
| 236 | // Methods for easy access to the global variables in the module. |
| 237 | // |
| 238 | |
| 239 | /// getGlobalVariable - Look up the specified global variable in the module |
Chris Lattner | 7d4d93c | 2005-12-05 05:30:21 +0000 | [diff] [blame] | 240 | /// symbol table. If it does not exist, return null. The type argument |
| 241 | /// should be the underlying type of the global, i.e., it should not have |
| 242 | /// the top-level PointerType, which represents the address of the global. |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 243 | /// If AllowLocal is set to true, this function will return types that |
| 244 | /// have an local. By default, these types are not returned. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 245 | /// |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 246 | GlobalVariable *Module::getGlobalVariable(const StringRef &Name, |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 247 | bool AllowLocal) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 248 | if (GlobalVariable *Result = |
| 249 | dyn_cast_or_null<GlobalVariable>(getNamedValue(Name))) |
| 250 | if (AllowLocal || !Result->hasLocalLinkage()) |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 251 | return Result; |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 252 | return 0; |
| 253 | } |
| 254 | |
Bill Wendling | f5f6f74 | 2008-11-05 23:42:27 +0000 | [diff] [blame] | 255 | /// getOrInsertGlobal - Look up the specified global in the module symbol table. |
| 256 | /// 1. If it does not exist, add a declaration of the global and return it. |
| 257 | /// 2. Else, the global exists but has the wrong type: return the function |
| 258 | /// with a constantexpr cast to the right type. |
| 259 | /// 3. Finally, if the existing global is the correct delclaration, return the |
| 260 | /// existing global. |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 261 | Constant *Module::getOrInsertGlobal(const StringRef &Name, const Type *Ty) { |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 262 | // See if we have a definition for the specified global already. |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 263 | GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)); |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 264 | if (GV == 0) { |
| 265 | // Nope, add it |
| 266 | GlobalVariable *New = |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 267 | new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, |
| 268 | 0, Name); |
| 269 | return New; // Return the new declaration. |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // If the variable exists but has the wrong type, return a bitcast to the |
| 273 | // right type. |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 274 | if (GV->getType() != PointerType::getUnqual(Ty)) |
| 275 | return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty)); |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 276 | |
| 277 | // Otherwise, we just found the existing function or a prototype. |
| 278 | return GV; |
| 279 | } |
| 280 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 281 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 282 | // Methods for easy access to the global variables in the module. |
| 283 | // |
| 284 | |
| 285 | // getNamedAlias - Look up the specified global in the module symbol table. |
| 286 | // If it does not exist, return null. |
| 287 | // |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 288 | GlobalAlias *Module::getNamedAlias(const StringRef &Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 289 | return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name)); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 292 | /// getNamedMetadata - Return the first NamedMDNode in the module with the |
| 293 | /// specified name. This method returns null if a NamedMDNode with the |
| 294 | //// specified name is not found. |
Devang Patel | 18dfdc9 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 295 | NamedMDNode *Module::getNamedMetadata(const StringRef &Name) const { |
Devang Patel | 3f4ab29 | 2009-07-30 18:46:29 +0000 | [diff] [blame] | 296 | return dyn_cast_or_null<NamedMDNode>(getValueSymbolTable().lookup(Name)); |
Devang Patel | 18dfdc9 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 299 | /// getOrInsertNamedMetadata - Return the first named MDNode in the module |
| 300 | /// with the specified name. This method returns a new NamedMDNode if a |
| 301 | /// NamedMDNode with the specified name is not found. |
| 302 | NamedMDNode *Module::getOrInsertNamedMetadata(const StringRef &Name) { |
| 303 | NamedMDNode *NMD = |
| 304 | dyn_cast_or_null<NamedMDNode>(getValueSymbolTable().lookup(Name)); |
| 305 | if (!NMD) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 306 | NMD = NamedMDNode::Create(getContext(), Name, NULL, 0, this); |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 307 | return NMD; |
| 308 | } |
| 309 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 310 | //===----------------------------------------------------------------------===// |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 311 | // Methods for easy access to the types in the module. |
| 312 | // |
| 313 | |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 314 | |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 315 | // addTypeName - Insert an entry in the symbol table mapping Str to Type. If |
| 316 | // there is already an entry for this name, true is returned and the symbol |
| 317 | // table is not modified. |
| 318 | // |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 319 | bool Module::addTypeName(const StringRef &Name, const Type *Ty) { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 320 | TypeSymbolTable &ST = getTypeSymbolTable(); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 321 | |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 322 | if (ST.lookup(Name)) return true; // Already in symtab... |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 323 | |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 324 | // Not in symbol table? Set the name with the Symtab as an argument so the |
| 325 | // type knows what to update... |
Reid Spencer | f9776c3 | 2004-07-10 16:37:42 +0000 | [diff] [blame] | 326 | ST.insert(Name, Ty); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 327 | |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | /// getTypeByName - Return the type with the specified name in this module, or |
| 332 | /// null if there is none by that name. |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 333 | const Type *Module::getTypeByName(const StringRef &Name) const { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 334 | const TypeSymbolTable &ST = getTypeSymbolTable(); |
| 335 | return cast_or_null<Type>(ST.lookup(Name)); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 336 | } |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 338 | // getTypeName - If there is at least one entry in the symbol table for the |
| 339 | // specified type, return it. |
| 340 | // |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 341 | std::string Module::getTypeName(const Type *Ty) const { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 342 | const TypeSymbolTable &ST = getTypeSymbolTable(); |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 343 | |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 344 | TypeSymbolTable::const_iterator TI = ST.begin(); |
| 345 | TypeSymbolTable::const_iterator TE = ST.end(); |
Reid Spencer | abb6f00 | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 346 | if ( TI == TE ) return ""; // No names for types |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 347 | |
Reid Spencer | abb6f00 | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 348 | while (TI != TE && TI->second != Ty) |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 349 | ++TI; |
| 350 | |
| 351 | if (TI != TE) // Must have found an entry! |
| 352 | return TI->first; |
| 353 | return ""; // Must not have found anything... |
| 354 | } |
| 355 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 356 | //===----------------------------------------------------------------------===// |
| 357 | // Other module related stuff. |
| 358 | // |
| 359 | |
| 360 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 361 | // dropAllReferences() - This function causes all the subelementss to "let go" |
| 362 | // of all references that they are maintaining. This allows one to 'delete' a |
| 363 | // whole module at a time, even though there may be circular references... first |
| 364 | // 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] | 365 | // 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] | 366 | // has "dropped all references", except operator delete. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 367 | // |
| 368 | void Module::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 369 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 370 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 372 | for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I) |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 373 | I->dropAllReferences(); |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 374 | |
| 375 | for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I) |
| 376 | I->dropAllReferences(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 377 | } |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 378 | |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 379 | void Module::addLibrary(const StringRef& Lib) { |
Reid Spencer | 3f4e6e8 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 380 | for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I) |
| 381 | if (*I == Lib) |
| 382 | return; |
| 383 | LibraryList.push_back(Lib); |
| 384 | } |
| 385 | |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 386 | void Module::removeLibrary(const StringRef& Lib) { |
Reid Spencer | 3f4e6e8 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 387 | LibraryListType::iterator I = LibraryList.begin(); |
| 388 | LibraryListType::iterator E = LibraryList.end(); |
| 389 | for (;I != E; ++I) |
| 390 | if (*I == Lib) { |
| 391 | LibraryList.erase(I); |
| 392 | return; |
| 393 | } |
| 394 | } |