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" |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseSet.h" |
Devang Patel | a6d20f4 | 2010-06-16 00:53:55 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallString.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 24 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 25 | #include "SymbolTableListTraitsImpl.h" |
| 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(); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 63 | NamedMDSymTab = new StringMap<NamedMDNode *>(); |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 64 | Context.addModule(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | Module::~Module() { |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 68 | Context.removeModule(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 69 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 70 | GlobalList.clear(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 71 | FunctionList.clear(); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 72 | AliasList.clear(); |
Reid Spencer | a0b05b3 | 2004-07-25 18:08:57 +0000 | [diff] [blame] | 73 | LibraryList.clear(); |
Devang Patel | 18dfdc9 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 74 | NamedMDList.clear(); |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 75 | delete ValSymTab; |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 76 | delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 79 | /// Target endian information. |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 80 | Module::Endianness Module::getEndianness() const { |
Benjamin Kramer | c6fe3c3 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 81 | StringRef temp = DataLayout; |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 82 | Module::Endianness ret = AnyEndianness; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 83 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 84 | while (!temp.empty()) { |
Benjamin Kramer | c6fe3c3 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 85 | StringRef token = DataLayout; |
Chris Lattner | ae12e35 | 2010-03-23 21:48:41 +0000 | [diff] [blame] | 86 | tie(token, temp) = getToken(temp, "-"); |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 87 | |
| 88 | if (token[0] == 'e') { |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 89 | ret = LittleEndian; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 90 | } else if (token[0] == 'E') { |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 91 | ret = BigEndian; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 95 | return ret; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 98 | /// Target Pointer Size information... |
| 99 | Module::PointerSize Module::getPointerSize() const { |
Benjamin Kramer | c6fe3c3 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 100 | StringRef temp = DataLayout; |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 101 | Module::PointerSize ret = AnyPointerSize; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 102 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 103 | while (!temp.empty()) { |
Benjamin Kramer | c6fe3c3 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 104 | StringRef token, signalToken; |
| 105 | tie(token, temp) = getToken(temp, "-"); |
| 106 | tie(signalToken, token) = getToken(token, ":"); |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 107 | |
Benjamin Kramer | c6fe3c3 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 108 | if (signalToken[0] == 'p') { |
| 109 | int size = 0; |
| 110 | getToken(token, ":").first.getAsInteger(10, size); |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 111 | if (size == 32) |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 112 | ret = Pointer32; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 113 | else if (size == 64) |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 114 | ret = Pointer64; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 118 | return ret; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 121 | /// getNamedValue - Return the first global value in the module with |
| 122 | /// the specified name, of arbitrary type. This method returns null |
| 123 | /// if a global with the specified name is not found. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 124 | GlobalValue *Module::getNamedValue(StringRef Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 125 | return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name)); |
| 126 | } |
| 127 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 128 | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
| 129 | /// This ID is uniqued across modules in the current LLVMContext. |
| 130 | unsigned Module::getMDKindID(StringRef Name) const { |
| 131 | return Context.getMDKindID(Name); |
| 132 | } |
| 133 | |
| 134 | /// getMDKindNames - Populate client supplied SmallVector with the name for |
| 135 | /// custom metadata IDs registered in this LLVMContext. ID #0 is not used, |
| 136 | /// so it is filled in as an empty string. |
| 137 | void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const { |
| 138 | return Context.getMDKindNames(Result); |
| 139 | } |
| 140 | |
| 141 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 142 | //===----------------------------------------------------------------------===// |
| 143 | // Methods for easy access to the functions in the module. |
| 144 | // |
| 145 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 146 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 147 | // table. If it does not exist, add a prototype for the function and return |
| 148 | // it. This is nice because it allows most passes to get away with not handling |
| 149 | // the symbol table directly for this common task. |
| 150 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 151 | Constant *Module::getOrInsertFunction(StringRef Name, |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 152 | const FunctionType *Ty, |
| 153 | AttrListPtr AttributeList) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 154 | // See if we have a definition for the specified function already. |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 155 | GlobalValue *F = getNamedValue(Name); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 156 | if (F == 0) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 157 | // Nope, add it |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 158 | Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 159 | if (!New->isIntrinsic()) // Intrinsics get attrs set on construction |
| 160 | New->setAttributes(AttributeList); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 161 | FunctionList.push_back(New); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 162 | return New; // Return the new prototype. |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 163 | } |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 164 | |
| 165 | // Okay, the function exists. Does it have externally visible linkage? |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 166 | if (F->hasLocalLinkage()) { |
Chris Lattner | 42e983e | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 167 | // Clear the function's name. |
| 168 | F->setName(""); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 169 | // Retry, now there won't be a conflict. |
Chris Lattner | 42e983e | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 170 | Constant *NewF = getOrInsertFunction(Name, Ty); |
Daniel Dunbar | d43b86d | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 171 | F->setName(Name); |
Chris Lattner | 42e983e | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 172 | return NewF; |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // If the function exists but has the wrong type, return a bitcast to the |
| 176 | // right type. |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 177 | if (F->getType() != PointerType::getUnqual(Ty)) |
| 178 | return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 179 | |
| 180 | // Otherwise, we just found the existing function or a prototype. |
| 181 | return F; |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 184 | Constant *Module::getOrInsertTargetIntrinsic(StringRef Name, |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 185 | const FunctionType *Ty, |
| 186 | AttrListPtr AttributeList) { |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 187 | // See if we have a definition for the specified function already. |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 188 | GlobalValue *F = getNamedValue(Name); |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 189 | if (F == 0) { |
| 190 | // Nope, add it |
| 191 | Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); |
| 192 | New->setAttributes(AttributeList); |
| 193 | FunctionList.push_back(New); |
| 194 | return New; // Return the new prototype. |
| 195 | } |
| 196 | |
| 197 | // Otherwise, we just found the existing function or a prototype. |
| 198 | return F; |
| 199 | } |
| 200 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 201 | Constant *Module::getOrInsertFunction(StringRef Name, |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 202 | const FunctionType *Ty) { |
| 203 | AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0); |
| 204 | return getOrInsertFunction(Name, Ty, AttributeList); |
| 205 | } |
| 206 | |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 207 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 208 | // table. If it does not exist, add a prototype for the function and return it. |
| 209 | // This version of the method takes a null terminated list of function |
| 210 | // arguments, which makes it easier for clients to use. |
| 211 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 212 | Constant *Module::getOrInsertFunction(StringRef Name, |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 213 | AttrListPtr AttributeList, |
| 214 | const Type *RetTy, ...) { |
| 215 | va_list Args; |
| 216 | va_start(Args, RetTy); |
| 217 | |
| 218 | // Build the list of argument types... |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 219 | std::vector<Type*> ArgTys; |
| 220 | while (Type *ArgTy = va_arg(Args, Type*)) |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 221 | ArgTys.push_back(ArgTy); |
| 222 | |
| 223 | va_end(Args); |
| 224 | |
| 225 | // Build the function type and chain to the other getOrInsertFunction... |
Owen Anderson | 785c56c | 2009-07-08 23:50:31 +0000 | [diff] [blame] | 226 | return getOrInsertFunction(Name, |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 227 | FunctionType::get(RetTy, ArgTys, false), |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 228 | AttributeList); |
| 229 | } |
| 230 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 231 | Constant *Module::getOrInsertFunction(StringRef Name, |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 232 | const Type *RetTy, ...) { |
| 233 | va_list Args; |
| 234 | va_start(Args, RetTy); |
| 235 | |
| 236 | // Build the list of argument types... |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 237 | std::vector<Type*> ArgTys; |
| 238 | while (Type *ArgTy = va_arg(Args, Type*)) |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 239 | ArgTys.push_back(ArgTy); |
| 240 | |
| 241 | va_end(Args); |
| 242 | |
| 243 | // Build the function type and chain to the other getOrInsertFunction... |
Owen Anderson | 785c56c | 2009-07-08 23:50:31 +0000 | [diff] [blame] | 244 | return getOrInsertFunction(Name, |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 245 | FunctionType::get(RetTy, ArgTys, false), |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 246 | AttrListPtr::get((AttributeWithIndex *)0, 0)); |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 249 | // getFunction - Look up the specified function in the module symbol table. |
| 250 | // If it does not exist, return null. |
| 251 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 252 | Function *Module::getFunction(StringRef Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 253 | return dyn_cast_or_null<Function>(getNamedValue(Name)); |
Chris Lattner | e43649f | 2008-06-27 21:09:10 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 256 | //===----------------------------------------------------------------------===// |
| 257 | // Methods for easy access to the global variables in the module. |
| 258 | // |
| 259 | |
| 260 | /// getGlobalVariable - Look up the specified global variable in the module |
Chris Lattner | 7d4d93c | 2005-12-05 05:30:21 +0000 | [diff] [blame] | 261 | /// symbol table. If it does not exist, return null. The type argument |
| 262 | /// should be the underlying type of the global, i.e., it should not have |
| 263 | /// the top-level PointerType, which represents the address of the global. |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 264 | /// If AllowLocal is set to true, this function will return types that |
| 265 | /// have an local. By default, these types are not returned. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 266 | /// |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 267 | GlobalVariable *Module::getGlobalVariable(StringRef Name, |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 268 | bool AllowLocal) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 269 | if (GlobalVariable *Result = |
| 270 | dyn_cast_or_null<GlobalVariable>(getNamedValue(Name))) |
| 271 | if (AllowLocal || !Result->hasLocalLinkage()) |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 272 | return Result; |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
Bill Wendling | f5f6f74 | 2008-11-05 23:42:27 +0000 | [diff] [blame] | 276 | /// getOrInsertGlobal - Look up the specified global in the module symbol table. |
| 277 | /// 1. If it does not exist, add a declaration of the global and return it. |
| 278 | /// 2. Else, the global exists but has the wrong type: return the function |
| 279 | /// with a constantexpr cast to the right type. |
| 280 | /// 3. Finally, if the existing global is the correct delclaration, return the |
| 281 | /// existing global. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 282 | Constant *Module::getOrInsertGlobal(StringRef Name, const Type *Ty) { |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 283 | // See if we have a definition for the specified global already. |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 284 | GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)); |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 285 | if (GV == 0) { |
| 286 | // Nope, add it |
| 287 | GlobalVariable *New = |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 288 | new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, |
| 289 | 0, Name); |
| 290 | return New; // Return the new declaration. |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // If the variable exists but has the wrong type, return a bitcast to the |
| 294 | // right type. |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 295 | if (GV->getType() != PointerType::getUnqual(Ty)) |
| 296 | return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty)); |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 297 | |
| 298 | // Otherwise, we just found the existing function or a prototype. |
| 299 | return GV; |
| 300 | } |
| 301 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 302 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 303 | // Methods for easy access to the global variables in the module. |
| 304 | // |
| 305 | |
| 306 | // getNamedAlias - Look up the specified global in the module symbol table. |
| 307 | // If it does not exist, return null. |
| 308 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 309 | GlobalAlias *Module::getNamedAlias(StringRef Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 310 | return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name)); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 313 | /// getNamedMetadata - Return the first NamedMDNode in the module with the |
| 314 | /// specified name. This method returns null if a NamedMDNode with the |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 315 | /// specified name is not found. |
Devang Patel | b6e058d | 2010-06-22 01:19:38 +0000 | [diff] [blame] | 316 | NamedMDNode *Module::getNamedMetadata(const Twine &Name) const { |
Devang Patel | a6d20f4 | 2010-06-16 00:53:55 +0000 | [diff] [blame] | 317 | SmallString<256> NameData; |
| 318 | StringRef NameRef = Name.toStringRef(NameData); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 319 | return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef); |
Devang Patel | a6d20f4 | 2010-06-16 00:53:55 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 322 | /// getOrInsertNamedMetadata - Return the first named MDNode in the module |
| 323 | /// with the specified name. This method returns a new NamedMDNode if a |
| 324 | /// NamedMDNode with the specified name is not found. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 325 | NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 326 | NamedMDNode *&NMD = |
| 327 | (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name]; |
| 328 | if (!NMD) { |
| 329 | NMD = new NamedMDNode(Name); |
| 330 | NMD->setParent(this); |
| 331 | NamedMDList.push_back(NMD); |
| 332 | } |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 333 | return NMD; |
| 334 | } |
| 335 | |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 336 | void Module::eraseNamedMetadata(NamedMDNode *NMD) { |
| 337 | static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName()); |
| 338 | NamedMDList.erase(NMD); |
| 339 | } |
| 340 | |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 342 | //===----------------------------------------------------------------------===// |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 343 | // Methods to control the materialization of GlobalValues in the Module. |
| 344 | // |
| 345 | void Module::setMaterializer(GVMaterializer *GVM) { |
| 346 | assert(!Materializer && |
| 347 | "Module already has a GVMaterializer. Call MaterializeAllPermanently" |
| 348 | " to clear it out before setting another one."); |
| 349 | Materializer.reset(GVM); |
| 350 | } |
| 351 | |
| 352 | bool Module::isMaterializable(const GlobalValue *GV) const { |
| 353 | if (Materializer) |
| 354 | return Materializer->isMaterializable(GV); |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | bool Module::isDematerializable(const GlobalValue *GV) const { |
| 359 | if (Materializer) |
| 360 | return Materializer->isDematerializable(GV); |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) { |
| 365 | if (Materializer) |
| 366 | return Materializer->Materialize(GV, ErrInfo); |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | void Module::Dematerialize(GlobalValue *GV) { |
| 371 | if (Materializer) |
| 372 | return Materializer->Dematerialize(GV); |
| 373 | } |
| 374 | |
| 375 | bool Module::MaterializeAll(std::string *ErrInfo) { |
| 376 | if (!Materializer) |
| 377 | return false; |
| 378 | return Materializer->MaterializeModule(this, ErrInfo); |
| 379 | } |
| 380 | |
| 381 | bool Module::MaterializeAllPermanently(std::string *ErrInfo) { |
| 382 | if (MaterializeAll(ErrInfo)) |
| 383 | return true; |
| 384 | Materializer.reset(); |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | //===----------------------------------------------------------------------===// |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 389 | // Other module related stuff. |
| 390 | // |
| 391 | |
| 392 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 393 | // dropAllReferences() - This function causes all the subelementss to "let go" |
| 394 | // of all references that they are maintaining. This allows one to 'delete' a |
| 395 | // whole module at a time, even though there may be circular references... first |
| 396 | // 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] | 397 | // 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] | 398 | // has "dropped all references", except operator delete. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 399 | // |
| 400 | void Module::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 401 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 402 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 404 | 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] | 405 | I->dropAllReferences(); |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 406 | |
| 407 | for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I) |
| 408 | I->dropAllReferences(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 409 | } |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 410 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 411 | void Module::addLibrary(StringRef Lib) { |
Reid Spencer | 3f4e6e8 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 412 | for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I) |
| 413 | if (*I == Lib) |
| 414 | return; |
| 415 | LibraryList.push_back(Lib); |
| 416 | } |
| 417 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 418 | void Module::removeLibrary(StringRef Lib) { |
Reid Spencer | 3f4e6e8 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 419 | LibraryListType::iterator I = LibraryList.begin(); |
| 420 | LibraryListType::iterator E = LibraryList.end(); |
| 421 | for (;I != E; ++I) |
| 422 | if (*I == Lib) { |
| 423 | LibraryList.erase(I); |
| 424 | return; |
| 425 | } |
| 426 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 427 | |
| 428 | //===----------------------------------------------------------------------===// |
| 429 | // Type finding functionality. |
| 430 | //===----------------------------------------------------------------------===// |
| 431 | |
| 432 | namespace { |
| 433 | /// TypeFinder - Walk over a module, identifying all of the types that are |
| 434 | /// used by the module. |
| 435 | class TypeFinder { |
| 436 | // To avoid walking constant expressions multiple times and other IR |
| 437 | // objects, we keep several helper maps. |
| 438 | DenseSet<const Value*> VisitedConstants; |
| 439 | DenseSet<const Type*> VisitedTypes; |
| 440 | |
| 441 | std::vector<StructType*> &StructTypes; |
| 442 | public: |
| 443 | TypeFinder(std::vector<StructType*> &structTypes) |
| 444 | : StructTypes(structTypes) {} |
| 445 | |
| 446 | void run(const Module &M) { |
| 447 | // Get types from global variables. |
| 448 | for (Module::const_global_iterator I = M.global_begin(), |
| 449 | E = M.global_end(); I != E; ++I) { |
| 450 | incorporateType(I->getType()); |
| 451 | if (I->hasInitializer()) |
| 452 | incorporateValue(I->getInitializer()); |
| 453 | } |
| 454 | |
| 455 | // Get types from aliases. |
| 456 | for (Module::const_alias_iterator I = M.alias_begin(), |
| 457 | E = M.alias_end(); I != E; ++I) { |
| 458 | incorporateType(I->getType()); |
| 459 | if (const Value *Aliasee = I->getAliasee()) |
| 460 | incorporateValue(Aliasee); |
| 461 | } |
| 462 | |
| 463 | SmallVector<std::pair<unsigned, MDNode*>, 4> MDForInst; |
| 464 | |
| 465 | // Get types from functions. |
| 466 | for (Module::const_iterator FI = M.begin(), E = M.end(); FI != E; ++FI) { |
| 467 | incorporateType(FI->getType()); |
| 468 | |
| 469 | for (Function::const_iterator BB = FI->begin(), E = FI->end(); |
| 470 | BB != E;++BB) |
| 471 | for (BasicBlock::const_iterator II = BB->begin(), |
| 472 | E = BB->end(); II != E; ++II) { |
| 473 | const Instruction &I = *II; |
| 474 | // Incorporate the type of the instruction and all its operands. |
| 475 | incorporateType(I.getType()); |
| 476 | for (User::const_op_iterator OI = I.op_begin(), OE = I.op_end(); |
| 477 | OI != OE; ++OI) |
| 478 | incorporateValue(*OI); |
| 479 | |
| 480 | // Incorporate types hiding in metadata. |
Chris Lattner | 4cc3bcd | 2011-07-14 18:58:40 +0000 | [diff] [blame^] | 481 | I.getAllMetadataOtherThanDebugLoc(MDForInst); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 482 | for (unsigned i = 0, e = MDForInst.size(); i != e; ++i) |
| 483 | incorporateMDNode(MDForInst[i].second); |
| 484 | MDForInst.clear(); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | for (Module::const_named_metadata_iterator I = M.named_metadata_begin(), |
| 489 | E = M.named_metadata_end(); I != E; ++I) { |
| 490 | const NamedMDNode *NMD = I; |
| 491 | for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) |
| 492 | incorporateMDNode(NMD->getOperand(i)); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | private: |
| 497 | void incorporateType(Type *Ty) { |
| 498 | // Check to see if we're already visited this type. |
| 499 | if (!VisitedTypes.insert(Ty).second) |
| 500 | return; |
| 501 | |
| 502 | // If this is a structure or opaque type, add a name for the type. |
| 503 | if (StructType *STy = dyn_cast<StructType>(Ty)) |
| 504 | StructTypes.push_back(STy); |
| 505 | |
| 506 | // Recursively walk all contained types. |
| 507 | for (Type::subtype_iterator I = Ty->subtype_begin(), |
| 508 | E = Ty->subtype_end(); I != E; ++I) |
| 509 | incorporateType(*I); |
| 510 | } |
| 511 | |
| 512 | /// incorporateValue - This method is used to walk operand lists finding |
| 513 | /// types hiding in constant expressions and other operands that won't be |
| 514 | /// walked in other ways. GlobalValues, basic blocks, instructions, and |
| 515 | /// inst operands are all explicitly enumerated. |
| 516 | void incorporateValue(const Value *V) { |
| 517 | if (const MDNode *M = dyn_cast<MDNode>(V)) |
| 518 | return incorporateMDNode(M); |
| 519 | if (!isa<Constant>(V) || isa<GlobalValue>(V)) return; |
| 520 | |
| 521 | // Already visited? |
| 522 | if (!VisitedConstants.insert(V).second) |
| 523 | return; |
| 524 | |
| 525 | // Check this type. |
| 526 | incorporateType(V->getType()); |
| 527 | |
| 528 | // Look in operands for types. |
| 529 | const User *U = cast<User>(V); |
| 530 | for (Constant::const_op_iterator I = U->op_begin(), |
| 531 | E = U->op_end(); I != E;++I) |
| 532 | incorporateValue(*I); |
| 533 | } |
| 534 | |
| 535 | void incorporateMDNode(const MDNode *V) { |
| 536 | |
| 537 | // Already visited? |
| 538 | if (!VisitedConstants.insert(V).second) |
| 539 | return; |
| 540 | |
| 541 | // Look in operands for types. |
| 542 | for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) |
| 543 | if (Value *Op = V->getOperand(i)) |
| 544 | incorporateValue(Op); |
| 545 | } |
| 546 | }; |
| 547 | } // end anonymous namespace |
| 548 | |
| 549 | void Module::findUsedStructTypes(std::vector<StructType*> &StructTypes) const { |
| 550 | TypeFinder(StructTypes).run(*this); |
| 551 | } |
| 552 | |
| 553 | |