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