Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Module.cpp - Implement the Module class ---------------------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chandler Carruth | c2c50cd | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the Module class for the IR library. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Module.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "SymbolTableListTraitsImpl.h" |
| 16 | #include "llvm/ADT/DenseSet.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallString.h" |
| 19 | #include "llvm/ADT/StringExtras.h" |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 20 | #include "llvm/GVMaterializer.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Constants.h" |
| 22 | #include "llvm/IR/DerivedTypes.h" |
| 23 | #include "llvm/IR/InstrTypes.h" |
| 24 | #include "llvm/IR/LLVMContext.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Chris Lattner | 0ae8e87 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 27 | #include <cstdarg> |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 28 | #include <cstdlib> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Misha Brukman | af76cfb | 2004-04-21 18:27:56 +0000 | [diff] [blame] | 32 | // Methods to implement the globals and functions lists. |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 33 | // |
| 34 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 35 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
Chris Lattner | cc041ba | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 36 | // are not in the public header file. |
John McCall | c63ca0a | 2009-12-19 00:55:12 +0000 | [diff] [blame] | 37 | template class llvm::SymbolTableListTraits<Function, Module>; |
Nick Lewycky | 53b2b73 | 2011-08-13 01:04:44 +0000 | [diff] [blame] | 38 | template class llvm::SymbolTableListTraits<GlobalVariable, Module>; |
John McCall | c63ca0a | 2009-12-19 00:55:12 +0000 | [diff] [blame] | 39 | template class llvm::SymbolTableListTraits<GlobalAlias, Module>; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // Primitive Module methods. |
| 43 | // |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 44 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 45 | Module::Module(StringRef MID, LLVMContext& C) |
Dan Gohman | d98af0a | 2010-08-04 01:39:08 +0000 | [diff] [blame] | 46 | : Context(C), Materializer(NULL), ModuleID(MID) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 47 | ValSymTab = new ValueSymbolTable(); |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 48 | NamedMDSymTab = new StringMap<NamedMDNode *>(); |
Owen Anderson | 30268be | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 49 | Context.addModule(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | Module::~Module() { |
Owen Anderson | 30268be | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 53 | Context.removeModule(this); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 54 | dropAllReferences(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 55 | GlobalList.clear(); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 56 | FunctionList.clear(); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 57 | AliasList.clear(); |
Devang Patel | 28bc9d8 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 58 | NamedMDList.clear(); |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 59 | delete ValSymTab; |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 60 | delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 63 | /// Target endian information. |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 64 | Module::Endianness Module::getEndianness() const { |
Benjamin Kramer | d4f1959 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 65 | StringRef temp = DataLayout; |
Owen Anderson | ac4c75b | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 66 | Module::Endianness ret = AnyEndianness; |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 67 | |
Owen Anderson | ac4c75b | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 68 | while (!temp.empty()) { |
Chris Lattner | c30a38f | 2011-07-21 06:21:31 +0000 | [diff] [blame] | 69 | std::pair<StringRef, StringRef> P = getToken(temp, "-"); |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 70 | |
Chris Lattner | c30a38f | 2011-07-21 06:21:31 +0000 | [diff] [blame] | 71 | StringRef token = P.first; |
| 72 | temp = P.second; |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 73 | |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 74 | if (token[0] == 'e') { |
Owen Anderson | ac4c75b | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 75 | ret = LittleEndian; |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 76 | } else if (token[0] == 'E') { |
Owen Anderson | ac4c75b | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 77 | ret = BigEndian; |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 80 | |
Owen Anderson | ac4c75b | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 81 | return ret; |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | c30a38f | 2011-07-21 06:21:31 +0000 | [diff] [blame] | 84 | /// Target Pointer Size information. |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 85 | Module::PointerSize Module::getPointerSize() const { |
Benjamin Kramer | d4f1959 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 86 | StringRef temp = DataLayout; |
Owen Anderson | ac4c75b | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 87 | Module::PointerSize ret = AnyPointerSize; |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 88 | |
Owen Anderson | ac4c75b | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 89 | while (!temp.empty()) { |
Chris Lattner | c30a38f | 2011-07-21 06:21:31 +0000 | [diff] [blame] | 90 | std::pair<StringRef, StringRef> TmpP = getToken(temp, "-"); |
| 91 | temp = TmpP.second; |
| 92 | TmpP = getToken(TmpP.first, ":"); |
| 93 | StringRef token = TmpP.second, signalToken = TmpP.first; |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 94 | |
Benjamin Kramer | d4f1959 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 95 | if (signalToken[0] == 'p') { |
| 96 | int size = 0; |
| 97 | getToken(token, ":").first.getAsInteger(10, size); |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 98 | if (size == 32) |
Owen Anderson | ac4c75b | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 99 | ret = Pointer32; |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 100 | else if (size == 64) |
Owen Anderson | ac4c75b | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 101 | ret = Pointer64; |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 104 | |
Owen Anderson | ac4c75b | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 105 | return ret; |
Owen Anderson | 1d8b853 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Daniel Dunbar | f56ec64 | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 108 | /// getNamedValue - Return the first global value in the module with |
| 109 | /// the specified name, of arbitrary type. This method returns null |
| 110 | /// if a global with the specified name is not found. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 111 | GlobalValue *Module::getNamedValue(StringRef Name) const { |
Daniel Dunbar | f56ec64 | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 112 | return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name)); |
| 113 | } |
| 114 | |
Chris Lattner | 0811347 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 115 | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
| 116 | /// This ID is uniqued across modules in the current LLVMContext. |
| 117 | unsigned Module::getMDKindID(StringRef Name) const { |
| 118 | return Context.getMDKindID(Name); |
| 119 | } |
| 120 | |
| 121 | /// getMDKindNames - Populate client supplied SmallVector with the name for |
| 122 | /// custom metadata IDs registered in this LLVMContext. ID #0 is not used, |
| 123 | /// so it is filled in as an empty string. |
| 124 | void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const { |
| 125 | return Context.getMDKindNames(Result); |
| 126 | } |
| 127 | |
| 128 | |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 129 | //===----------------------------------------------------------------------===// |
| 130 | // Methods for easy access to the functions in the module. |
| 131 | // |
| 132 | |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 133 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 134 | // table. If it does not exist, add a prototype for the function and return |
| 135 | // it. This is nice because it allows most passes to get away with not handling |
| 136 | // the symbol table directly for this common task. |
| 137 | // |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 138 | Constant *Module::getOrInsertFunction(StringRef Name, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 139 | FunctionType *Ty, |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 140 | AttributeSet AttributeList) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 141 | // See if we have a definition for the specified function already. |
Daniel Dunbar | f56ec64 | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 142 | GlobalValue *F = getNamedValue(Name); |
Chris Lattner | 70d1305 | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 143 | if (F == 0) { |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 144 | // Nope, add it |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 145 | Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); |
Nick Lewycky | 1186bf1 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 146 | if (!New->isIntrinsic()) // Intrinsics get attrs set on construction |
| 147 | New->setAttributes(AttributeList); |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 148 | FunctionList.push_back(New); |
Chris Lattner | 70d1305 | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 149 | return New; // Return the new prototype. |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 150 | } |
Chris Lattner | 70d1305 | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 151 | |
| 152 | // Okay, the function exists. Does it have externally visible linkage? |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 153 | if (F->hasLocalLinkage()) { |
Chris Lattner | 48cd712 | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 154 | // Clear the function's name. |
| 155 | F->setName(""); |
Chris Lattner | 70d1305 | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 156 | // Retry, now there won't be a conflict. |
Chris Lattner | 48cd712 | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 157 | Constant *NewF = getOrInsertFunction(Name, Ty); |
Daniel Dunbar | 92ccf70 | 2009-07-25 06:02:13 +0000 | [diff] [blame] | 158 | F->setName(Name); |
Chris Lattner | 48cd712 | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 159 | return NewF; |
Chris Lattner | 70d1305 | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // If the function exists but has the wrong type, return a bitcast to the |
| 163 | // right type. |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 164 | if (F->getType() != PointerType::getUnqual(Ty)) |
| 165 | return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 70d1305 | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 167 | // Otherwise, we just found the existing function or a prototype. |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 168 | return F; |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 171 | Constant *Module::getOrInsertFunction(StringRef Name, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 172 | FunctionType *Ty) { |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 173 | return getOrInsertFunction(Name, Ty, AttributeSet()); |
Nick Lewycky | 1186bf1 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Chris Lattner | 0ae8e87 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 176 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 177 | // table. If it does not exist, add a prototype for the function and return it. |
| 178 | // This version of the method takes a null terminated list of function |
| 179 | // arguments, which makes it easier for clients to use. |
| 180 | // |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 181 | Constant *Module::getOrInsertFunction(StringRef Name, |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 182 | AttributeSet AttributeList, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 183 | Type *RetTy, ...) { |
Nick Lewycky | 1186bf1 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 184 | va_list Args; |
| 185 | va_start(Args, RetTy); |
| 186 | |
| 187 | // Build the list of argument types... |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 188 | std::vector<Type*> ArgTys; |
| 189 | while (Type *ArgTy = va_arg(Args, Type*)) |
Nick Lewycky | 1186bf1 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 190 | ArgTys.push_back(ArgTy); |
| 191 | |
| 192 | va_end(Args); |
| 193 | |
| 194 | // Build the function type and chain to the other getOrInsertFunction... |
Owen Anderson | c341f1c | 2009-07-08 23:50:31 +0000 | [diff] [blame] | 195 | return getOrInsertFunction(Name, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 196 | FunctionType::get(RetTy, ArgTys, false), |
Nick Lewycky | 1186bf1 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 197 | AttributeList); |
| 198 | } |
| 199 | |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 200 | Constant *Module::getOrInsertFunction(StringRef Name, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 201 | Type *RetTy, ...) { |
Chris Lattner | 0ae8e87 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 202 | va_list Args; |
| 203 | va_start(Args, RetTy); |
| 204 | |
| 205 | // Build the list of argument types... |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 206 | std::vector<Type*> ArgTys; |
| 207 | while (Type *ArgTy = va_arg(Args, Type*)) |
Chris Lattner | 0ae8e87 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 208 | ArgTys.push_back(ArgTy); |
| 209 | |
| 210 | va_end(Args); |
| 211 | |
| 212 | // Build the function type and chain to the other getOrInsertFunction... |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 213 | return getOrInsertFunction(Name, |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 214 | FunctionType::get(RetTy, ArgTys, false), |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 215 | AttributeSet()); |
Chris Lattner | 0ae8e87 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Chris Lattner | 6056c49 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 218 | // getFunction - Look up the specified function in the module symbol table. |
| 219 | // If it does not exist, return null. |
| 220 | // |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 221 | Function *Module::getFunction(StringRef Name) const { |
Daniel Dunbar | f56ec64 | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 222 | return dyn_cast_or_null<Function>(getNamedValue(Name)); |
Chris Lattner | bb0e248 | 2008-06-27 21:09:10 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 225 | //===----------------------------------------------------------------------===// |
| 226 | // Methods for easy access to the global variables in the module. |
| 227 | // |
| 228 | |
| 229 | /// getGlobalVariable - Look up the specified global variable in the module |
Chris Lattner | 3061467 | 2005-12-05 05:30:21 +0000 | [diff] [blame] | 230 | /// symbol table. If it does not exist, return null. The type argument |
| 231 | /// should be the underlying type of the global, i.e., it should not have |
| 232 | /// the top-level PointerType, which represents the address of the global. |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 233 | /// If AllowLocal is set to true, this function will return types that |
| 234 | /// have an local. By default, these types are not returned. |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 235 | /// |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 236 | GlobalVariable *Module::getGlobalVariable(StringRef Name, |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 237 | bool AllowLocal) const { |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 238 | if (GlobalVariable *Result = |
Daniel Dunbar | f56ec64 | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 239 | dyn_cast_or_null<GlobalVariable>(getNamedValue(Name))) |
| 240 | if (AllowLocal || !Result->hasLocalLinkage()) |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 241 | return Result; |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
Bill Wendling | b4b130f | 2008-11-05 23:42:27 +0000 | [diff] [blame] | 245 | /// getOrInsertGlobal - Look up the specified global in the module symbol table. |
| 246 | /// 1. If it does not exist, add a declaration of the global and return it. |
| 247 | /// 2. Else, the global exists but has the wrong type: return the function |
| 248 | /// with a constantexpr cast to the right type. |
| 249 | /// 3. Finally, if the existing global is the correct delclaration, return the |
| 250 | /// existing global. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 251 | Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { |
Bill Wendling | b7c2c12 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 252 | // See if we have a definition for the specified global already. |
Daniel Dunbar | f56ec64 | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 253 | GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)); |
Bill Wendling | b7c2c12 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 254 | if (GV == 0) { |
| 255 | // Nope, add it |
| 256 | GlobalVariable *New = |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 257 | new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, |
| 258 | 0, Name); |
| 259 | return New; // Return the new declaration. |
Bill Wendling | b7c2c12 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // If the variable exists but has the wrong type, return a bitcast to the |
| 263 | // right type. |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 264 | if (GV->getType() != PointerType::getUnqual(Ty)) |
| 265 | return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty)); |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 266 | |
Bill Wendling | b7c2c12 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 267 | // Otherwise, we just found the existing function or a prototype. |
| 268 | return GV; |
| 269 | } |
| 270 | |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 271 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 272 | // Methods for easy access to the global variables in the module. |
| 273 | // |
| 274 | |
| 275 | // getNamedAlias - Look up the specified global in the module symbol table. |
| 276 | // If it does not exist, return null. |
| 277 | // |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 278 | GlobalAlias *Module::getNamedAlias(StringRef Name) const { |
Daniel Dunbar | f56ec64 | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 279 | return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name)); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Devang Patel | 49fe6c9 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 282 | /// getNamedMetadata - Return the first NamedMDNode in the module with the |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 283 | /// specified name. This method returns null if a NamedMDNode with the |
Bob Wilson | 54eee52 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 284 | /// specified name is not found. |
Devang Patel | a762b09 | 2010-06-22 01:19:38 +0000 | [diff] [blame] | 285 | NamedMDNode *Module::getNamedMetadata(const Twine &Name) const { |
Devang Patel | 2f7d529 | 2010-06-16 00:53:55 +0000 | [diff] [blame] | 286 | SmallString<256> NameData; |
| 287 | StringRef NameRef = Name.toStringRef(NameData); |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 288 | return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef); |
Devang Patel | 2f7d529 | 2010-06-16 00:53:55 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Bill Wendling | 85d043d | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 291 | /// getOrInsertNamedMetadata - Return the first named MDNode in the module |
| 292 | /// with the specified name. This method returns a new NamedMDNode if a |
Devang Patel | 49fe6c9 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 293 | /// NamedMDNode with the specified name is not found. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 294 | NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 295 | NamedMDNode *&NMD = |
| 296 | (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name]; |
| 297 | if (!NMD) { |
| 298 | NMD = new NamedMDNode(Name); |
| 299 | NMD->setParent(this); |
| 300 | NamedMDList.push_back(NMD); |
| 301 | } |
Devang Patel | 49fe6c9 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 302 | return NMD; |
| 303 | } |
| 304 | |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 305 | /// eraseNamedMetadata - Remove the given NamedMDNode from this module and |
| 306 | /// delete it. |
Dan Gohman | 17aa92c | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 307 | void Module::eraseNamedMetadata(NamedMDNode *NMD) { |
| 308 | static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName()); |
| 309 | NamedMDList.erase(NMD); |
| 310 | } |
| 311 | |
Bill Wendling | f20f281 | 2012-02-15 22:34:20 +0000 | [diff] [blame] | 312 | /// getModuleFlagsMetadata - Returns the module flags in the provided vector. |
| 313 | void Module:: |
| 314 | getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const { |
| 315 | const NamedMDNode *ModFlags = getModuleFlagsMetadata(); |
| 316 | if (!ModFlags) return; |
| 317 | |
| 318 | for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) { |
| 319 | MDNode *Flag = ModFlags->getOperand(i); |
| 320 | ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0)); |
| 321 | MDString *Key = cast<MDString>(Flag->getOperand(1)); |
| 322 | Value *Val = Flag->getOperand(2); |
Bill Wendling | e6bd7a8 | 2012-02-16 10:28:10 +0000 | [diff] [blame] | 323 | Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()), |
Bill Wendling | 426f215 | 2012-02-15 23:27:50 +0000 | [diff] [blame] | 324 | Key, Val)); |
Bill Wendling | f20f281 | 2012-02-15 22:34:20 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Manman Ren | c8cfaa1 | 2013-07-16 23:21:16 +0000 | [diff] [blame] | 328 | /// Return the corresponding value if Key appears in module flags, otherwise |
| 329 | /// return null. |
| 330 | Value *Module::getModuleFlag(StringRef Key) const { |
| 331 | SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; |
| 332 | getModuleFlagsMetadata(ModuleFlags); |
| 333 | for (unsigned I = 0, E = ModuleFlags.size(); I < E; ++I) { |
| 334 | const ModuleFlagEntry &MFE = ModuleFlags[I]; |
| 335 | if (Key == MFE.Key->getString()) |
| 336 | return MFE.Val; |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 341 | /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that |
| 342 | /// represents module-level flags. This method returns null if there are no |
| 343 | /// module-level flags. |
| 344 | NamedMDNode *Module::getModuleFlagsMetadata() const { |
| 345 | return getNamedMetadata("llvm.module.flags"); |
| 346 | } |
| 347 | |
| 348 | /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that |
| 349 | /// represents module-level flags. If module-level flags aren't found, it |
| 350 | /// creates the named metadata that contains them. |
| 351 | NamedMDNode *Module::getOrInsertModuleFlagsMetadata() { |
| 352 | return getOrInsertNamedMetadata("llvm.module.flags"); |
| 353 | } |
| 354 | |
| 355 | /// addModuleFlag - Add a module-level flag to the module-level flags |
| 356 | /// metadata. It will create the module-level flags named metadata if it doesn't |
| 357 | /// already exist. |
Bill Wendling | e6bd7a8 | 2012-02-16 10:28:10 +0000 | [diff] [blame] | 358 | void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 359 | Value *Val) { |
| 360 | Type *Int32Ty = Type::getInt32Ty(Context); |
| 361 | Value *Ops[3] = { |
| 362 | ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val |
| 363 | }; |
| 364 | getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops)); |
| 365 | } |
Bill Wendling | e6bd7a8 | 2012-02-16 10:28:10 +0000 | [diff] [blame] | 366 | void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
Bill Wendling | d34cb1e | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 367 | uint32_t Val) { |
| 368 | Type *Int32Ty = Type::getInt32Ty(Context); |
| 369 | addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val)); |
| 370 | } |
| 371 | void Module::addModuleFlag(MDNode *Node) { |
| 372 | assert(Node->getNumOperands() == 3 && |
| 373 | "Invalid number of operands for module flag!"); |
| 374 | assert(isa<ConstantInt>(Node->getOperand(0)) && |
| 375 | isa<MDString>(Node->getOperand(1)) && |
| 376 | "Invalid operand types for module flag!"); |
| 377 | getOrInsertModuleFlagsMetadata()->addOperand(Node); |
| 378 | } |
Chris Lattner | f33fa6f | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 380 | //===----------------------------------------------------------------------===// |
Jeffrey Yasskin | f0356fe | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 381 | // Methods to control the materialization of GlobalValues in the Module. |
| 382 | // |
| 383 | void Module::setMaterializer(GVMaterializer *GVM) { |
| 384 | assert(!Materializer && |
| 385 | "Module already has a GVMaterializer. Call MaterializeAllPermanently" |
| 386 | " to clear it out before setting another one."); |
| 387 | Materializer.reset(GVM); |
| 388 | } |
| 389 | |
| 390 | bool Module::isMaterializable(const GlobalValue *GV) const { |
| 391 | if (Materializer) |
| 392 | return Materializer->isMaterializable(GV); |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | bool Module::isDematerializable(const GlobalValue *GV) const { |
| 397 | if (Materializer) |
| 398 | return Materializer->isDematerializable(GV); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) { |
| 403 | if (Materializer) |
| 404 | return Materializer->Materialize(GV, ErrInfo); |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | void Module::Dematerialize(GlobalValue *GV) { |
| 409 | if (Materializer) |
| 410 | return Materializer->Dematerialize(GV); |
| 411 | } |
| 412 | |
| 413 | bool Module::MaterializeAll(std::string *ErrInfo) { |
| 414 | if (!Materializer) |
| 415 | return false; |
| 416 | return Materializer->MaterializeModule(this, ErrInfo); |
| 417 | } |
| 418 | |
| 419 | bool Module::MaterializeAllPermanently(std::string *ErrInfo) { |
| 420 | if (MaterializeAll(ErrInfo)) |
| 421 | return true; |
| 422 | Materializer.reset(); |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6083782 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 427 | // Other module related stuff. |
| 428 | // |
| 429 | |
| 430 | |
Eric Christopher | cf84d86 | 2012-04-16 23:54:31 +0000 | [diff] [blame] | 431 | // dropAllReferences() - This function causes all the subelements to "let go" |
Chris Lattner | 9da0715 | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 432 | // of all references that they are maintaining. This allows one to 'delete' a |
| 433 | // whole module at a time, even though there may be circular references... first |
| 434 | // all references are dropped, and all use counts go to zero. Then everything |
Misha Brukman | 6b63452 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 435 | // is deleted for real. Note that no operations are valid on an object that |
Chris Lattner | 9da0715 | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 436 | // has "dropped all references", except operator delete. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 437 | // |
| 438 | void Module::dropAllReferences() { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 439 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 440 | I->dropAllReferences(); |
Chris Lattner | dd6dfbc | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 441 | |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 442 | for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 443 | I->dropAllReferences(); |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 444 | |
| 445 | for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I) |
| 446 | I->dropAllReferences(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 447 | } |