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 | // |
Chandler Carruth | ef860a2 | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the Module class for the IR library. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 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" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Constants.h" |
| 21 | #include "llvm/IR/DerivedTypes.h" |
Chandler Carruth | d1163aa | 2014-03-06 03:50:29 +0000 | [diff] [blame] | 22 | #include "llvm/IR/GVMaterializer.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/InstrTypes.h" |
| 24 | #include "llvm/IR/LLVMContext.h" |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 25 | #include "llvm/IR/TypeFinder.h" |
Diego Novillo | 0915c04 | 2014-04-17 22:33:50 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Dwarf.h" |
JF Bastien | 144829d | 2014-06-25 15:21:42 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Path.h" |
| 28 | #include "llvm/Support/RandomNumberGenerator.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 30 | #include <cstdarg> |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 31 | #include <cstdlib> |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
Misha Brukman | 3bcead7 | 2004-04-21 18:27:56 +0000 | [diff] [blame] | 36 | // Methods to implement the globals and functions lists. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 37 | // |
| 38 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 39 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 40 | // are not in the public header file. |
Duncan P. N. Exon Smith | 37bf678 | 2015-10-07 20:05:10 +0000 | [diff] [blame] | 41 | template class llvm::SymbolTableListTraits<Function>; |
| 42 | template class llvm::SymbolTableListTraits<GlobalVariable>; |
| 43 | template class llvm::SymbolTableListTraits<GlobalAlias>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
| 46 | // Primitive Module methods. |
| 47 | // |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 48 | |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 49 | Module::Module(StringRef MID, LLVMContext &C) |
JF Bastien | e6acbdc | 2014-12-17 18:12:10 +0000 | [diff] [blame] | 50 | : Context(C), Materializer(), ModuleID(MID), DL("") { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 51 | ValSymTab = new ValueSymbolTable(); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 52 | NamedMDSymTab = new StringMap<NamedMDNode *>(); |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 53 | Context.addModule(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | Module::~Module() { |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 57 | Context.removeModule(this); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 58 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 59 | GlobalList.clear(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 60 | FunctionList.clear(); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 61 | AliasList.clear(); |
Devang Patel | 18dfdc9 | 2009-07-29 17:16:17 +0000 | [diff] [blame] | 62 | NamedMDList.clear(); |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 63 | delete ValSymTab; |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 64 | delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | } |
| 66 | |
JF Bastien | e6acbdc | 2014-12-17 18:12:10 +0000 | [diff] [blame] | 67 | RandomNumberGenerator *Module::createRNG(const Pass* P) const { |
| 68 | SmallString<32> Salt(P->getPassName()); |
| 69 | |
| 70 | // This RNG is guaranteed to produce the same random stream only |
| 71 | // when the Module ID and thus the input filename is the same. This |
| 72 | // might be problematic if the input filename extension changes |
| 73 | // (e.g. from .c to .bc or .ll). |
| 74 | // |
| 75 | // We could store this salt in NamedMetadata, but this would make |
| 76 | // the parameter non-const. This would unfortunately make this |
| 77 | // interface unusable by any Machine passes, since they only have a |
| 78 | // const reference to their IR Module. Alternatively we can always |
| 79 | // store salt metadata from the Module constructor. |
| 80 | Salt += sys::path::filename(getModuleIdentifier()); |
| 81 | |
| 82 | return new RandomNumberGenerator(Salt); |
| 83 | } |
| 84 | |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 85 | /// getNamedValue - Return the first global value in the module with |
| 86 | /// the specified name, of arbitrary type. This method returns null |
| 87 | /// if a global with the specified name is not found. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 88 | GlobalValue *Module::getNamedValue(StringRef Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 89 | return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name)); |
| 90 | } |
| 91 | |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 92 | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
| 93 | /// This ID is uniqued across modules in the current LLVMContext. |
| 94 | unsigned Module::getMDKindID(StringRef Name) const { |
| 95 | return Context.getMDKindID(Name); |
| 96 | } |
| 97 | |
| 98 | /// getMDKindNames - Populate client supplied SmallVector with the name for |
| 99 | /// custom metadata IDs registered in this LLVMContext. ID #0 is not used, |
| 100 | /// so it is filled in as an empty string. |
| 101 | void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const { |
| 102 | return Context.getMDKindNames(Result); |
| 103 | } |
| 104 | |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 105 | void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const { |
| 106 | return Context.getOperandBundleTags(Result); |
| 107 | } |
Chris Lattner | a056697 | 2009-12-29 09:01:33 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 109 | //===----------------------------------------------------------------------===// |
| 110 | // Methods for easy access to the functions in the module. |
| 111 | // |
| 112 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 113 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 114 | // table. If it does not exist, add a prototype for the function and return |
| 115 | // it. This is nice because it allows most passes to get away with not handling |
| 116 | // the symbol table directly for this common task. |
| 117 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 118 | Constant *Module::getOrInsertFunction(StringRef Name, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 119 | FunctionType *Ty, |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 120 | AttributeSet AttributeList) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 121 | // See if we have a definition for the specified function already. |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 122 | GlobalValue *F = getNamedValue(Name); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 123 | if (!F) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 124 | // Nope, add it |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 125 | Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 126 | if (!New->isIntrinsic()) // Intrinsics get attrs set on construction |
| 127 | New->setAttributes(AttributeList); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 128 | FunctionList.push_back(New); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 129 | return New; // Return the new prototype. |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 132 | // If the function exists but has the wrong type, return a bitcast to the |
| 133 | // right type. |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 134 | if (F->getType() != PointerType::getUnqual(Ty)) |
| 135 | return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 137 | // Otherwise, we just found the existing function or a prototype. |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 138 | return F; |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 141 | Constant *Module::getOrInsertFunction(StringRef Name, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 142 | FunctionType *Ty) { |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 143 | return getOrInsertFunction(Name, Ty, AttributeSet()); |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +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 it. |
| 148 | // This version of the method takes a null terminated list of function |
| 149 | // arguments, which makes it easier for clients to use. |
| 150 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 151 | Constant *Module::getOrInsertFunction(StringRef Name, |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 152 | AttributeSet AttributeList, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 153 | Type *RetTy, ...) { |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 154 | va_list Args; |
| 155 | va_start(Args, RetTy); |
| 156 | |
| 157 | // Build the list of argument types... |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 158 | std::vector<Type*> ArgTys; |
| 159 | while (Type *ArgTy = va_arg(Args, Type*)) |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 160 | ArgTys.push_back(ArgTy); |
| 161 | |
| 162 | va_end(Args); |
| 163 | |
| 164 | // Build the function type and chain to the other getOrInsertFunction... |
Owen Anderson | 785c56c | 2009-07-08 23:50:31 +0000 | [diff] [blame] | 165 | return getOrInsertFunction(Name, |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 166 | FunctionType::get(RetTy, ArgTys, false), |
Nick Lewycky | 3a0c106 | 2009-01-04 22:54:40 +0000 | [diff] [blame] | 167 | AttributeList); |
| 168 | } |
| 169 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 170 | Constant *Module::getOrInsertFunction(StringRef Name, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 171 | Type *RetTy, ...) { |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 172 | va_list Args; |
| 173 | va_start(Args, RetTy); |
| 174 | |
| 175 | // Build the list of argument types... |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 176 | std::vector<Type*> ArgTys; |
| 177 | while (Type *ArgTy = va_arg(Args, Type*)) |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 178 | ArgTys.push_back(ArgTy); |
| 179 | |
| 180 | va_end(Args); |
| 181 | |
| 182 | // Build the function type and chain to the other getOrInsertFunction... |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 183 | return getOrInsertFunction(Name, |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 184 | FunctionType::get(RetTy, ArgTys, false), |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 185 | AttributeSet()); |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 188 | // getFunction - Look up the specified function in the module symbol table. |
| 189 | // If it does not exist, return null. |
| 190 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 191 | Function *Module::getFunction(StringRef Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 192 | return dyn_cast_or_null<Function>(getNamedValue(Name)); |
Chris Lattner | e43649f | 2008-06-27 21:09:10 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 195 | //===----------------------------------------------------------------------===// |
| 196 | // Methods for easy access to the global variables in the module. |
| 197 | // |
| 198 | |
| 199 | /// getGlobalVariable - Look up the specified global variable in the module |
Chris Lattner | 7d4d93c | 2005-12-05 05:30:21 +0000 | [diff] [blame] | 200 | /// symbol table. If it does not exist, return null. The type argument |
| 201 | /// should be the underlying type of the global, i.e., it should not have |
| 202 | /// the top-level PointerType, which represents the address of the global. |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 203 | /// If AllowLocal is set to true, this function will return types that |
| 204 | /// have an local. By default, these types are not returned. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 205 | /// |
Rafael Espindola | ec2375f | 2013-07-25 02:50:08 +0000 | [diff] [blame] | 206 | GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) { |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 207 | if (GlobalVariable *Result = |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 208 | dyn_cast_or_null<GlobalVariable>(getNamedValue(Name))) |
| 209 | if (AllowLocal || !Result->hasLocalLinkage()) |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 210 | return Result; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 211 | return nullptr; |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Bill Wendling | f5f6f74 | 2008-11-05 23:42:27 +0000 | [diff] [blame] | 214 | /// getOrInsertGlobal - Look up the specified global in the module symbol table. |
| 215 | /// 1. If it does not exist, add a declaration of the global and return it. |
| 216 | /// 2. Else, the global exists but has the wrong type: return the function |
| 217 | /// with a constantexpr cast to the right type. |
Matt Arsenault | 5200fdf | 2013-09-28 01:08:00 +0000 | [diff] [blame] | 218 | /// 3. Finally, if the existing global is the correct declaration, return the |
Bill Wendling | f5f6f74 | 2008-11-05 23:42:27 +0000 | [diff] [blame] | 219 | /// existing global. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 220 | Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) { |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 221 | // See if we have a definition for the specified global already. |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 222 | GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 223 | if (!GV) { |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 224 | // Nope, add it |
| 225 | GlobalVariable *New = |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 226 | new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 227 | nullptr, Name); |
Owen Anderson | b17f329 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 228 | return New; // Return the new declaration. |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // If the variable exists but has the wrong type, return a bitcast to the |
| 232 | // right type. |
Matt Arsenault | 27e783e | 2013-09-30 21:23:03 +0000 | [diff] [blame] | 233 | Type *GVTy = GV->getType(); |
| 234 | PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace()); |
Matt Arsenault | a90a340 | 2013-09-30 23:31:50 +0000 | [diff] [blame] | 235 | if (GVTy != PTy) |
Matt Arsenault | 27e783e | 2013-09-30 21:23:03 +0000 | [diff] [blame] | 236 | return ConstantExpr::getBitCast(GV, PTy); |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 237 | |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 238 | // Otherwise, we just found the existing function or a prototype. |
| 239 | return GV; |
| 240 | } |
| 241 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 242 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 243 | // Methods for easy access to the global variables in the module. |
| 244 | // |
| 245 | |
| 246 | // getNamedAlias - Look up the specified global in the module symbol table. |
| 247 | // If it does not exist, return null. |
| 248 | // |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 249 | GlobalAlias *Module::getNamedAlias(StringRef Name) const { |
Daniel Dunbar | dcf8d3c | 2009-03-06 22:04:43 +0000 | [diff] [blame] | 250 | return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name)); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 253 | /// getNamedMetadata - Return the first NamedMDNode in the module with the |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 254 | /// specified name. This method returns null if a NamedMDNode with the |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 255 | /// specified name is not found. |
Devang Patel | b6e058d | 2010-06-22 01:19:38 +0000 | [diff] [blame] | 256 | NamedMDNode *Module::getNamedMetadata(const Twine &Name) const { |
Devang Patel | a6d20f4 | 2010-06-16 00:53:55 +0000 | [diff] [blame] | 257 | SmallString<256> NameData; |
| 258 | StringRef NameRef = Name.toStringRef(NameData); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 259 | return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef); |
Devang Patel | a6d20f4 | 2010-06-16 00:53:55 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Bill Wendling | e32c23a | 2012-04-23 00:23:33 +0000 | [diff] [blame] | 262 | /// getOrInsertNamedMetadata - Return the first named MDNode in the module |
| 263 | /// with the specified name. This method returns a new NamedMDNode if a |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 264 | /// NamedMDNode with the specified name is not found. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 265 | NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) { |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 266 | NamedMDNode *&NMD = |
| 267 | (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name]; |
| 268 | if (!NMD) { |
| 269 | NMD = new NamedMDNode(Name); |
| 270 | NMD->setParent(this); |
| 271 | NamedMDList.push_back(NMD); |
| 272 | } |
Devang Patel | 9825079 | 2009-07-30 23:59:04 +0000 | [diff] [blame] | 273 | return NMD; |
| 274 | } |
| 275 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 276 | /// eraseNamedMetadata - Remove the given NamedMDNode from this module and |
| 277 | /// delete it. |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 278 | void Module::eraseNamedMetadata(NamedMDNode *NMD) { |
| 279 | static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName()); |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 280 | NamedMDList.erase(NMD->getIterator()); |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 283 | bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) { |
David Majnemer | d7677e7 | 2015-02-11 09:13:06 +0000 | [diff] [blame] | 284 | if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) { |
Alexey Samsonov | af023ad | 2014-09-08 19:16:28 +0000 | [diff] [blame] | 285 | uint64_t Val = Behavior->getLimitedValue(); |
| 286 | if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) { |
| 287 | MFB = static_cast<ModFlagBehavior>(Val); |
| 288 | return true; |
| 289 | } |
| 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
Bill Wendling | 0294932 | 2012-02-15 22:34:20 +0000 | [diff] [blame] | 294 | /// getModuleFlagsMetadata - Returns the module flags in the provided vector. |
| 295 | void Module:: |
| 296 | getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const { |
| 297 | const NamedMDNode *ModFlags = getModuleFlagsMetadata(); |
| 298 | if (!ModFlags) return; |
| 299 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 300 | for (const MDNode *Flag : ModFlags->operands()) { |
Alexey Samsonov | af023ad | 2014-09-08 19:16:28 +0000 | [diff] [blame] | 301 | ModFlagBehavior MFB; |
| 302 | if (Flag->getNumOperands() >= 3 && |
| 303 | isValidModFlagBehavior(Flag->getOperand(0), MFB) && |
David Majnemer | d7677e7 | 2015-02-11 09:13:06 +0000 | [diff] [blame] | 304 | dyn_cast_or_null<MDString>(Flag->getOperand(1))) { |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 305 | // Check the operands of the MDNode before accessing the operands. |
| 306 | // The verifier will actually catch these failures. |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 307 | MDString *Key = cast<MDString>(Flag->getOperand(1)); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 308 | Metadata *Val = Flag->getOperand(2); |
Alexey Samsonov | af023ad | 2014-09-08 19:16:28 +0000 | [diff] [blame] | 309 | Flags.push_back(ModuleFlagEntry(MFB, Key, Val)); |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 310 | } |
Bill Wendling | 0294932 | 2012-02-15 22:34:20 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Manman Ren | 8bfde89 | 2013-07-16 23:21:16 +0000 | [diff] [blame] | 314 | /// Return the corresponding value if Key appears in module flags, otherwise |
| 315 | /// return null. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 316 | Metadata *Module::getModuleFlag(StringRef Key) const { |
Manman Ren | 8bfde89 | 2013-07-16 23:21:16 +0000 | [diff] [blame] | 317 | SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags; |
| 318 | getModuleFlagsMetadata(ModuleFlags); |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 319 | for (const ModuleFlagEntry &MFE : ModuleFlags) { |
Manman Ren | 8bfde89 | 2013-07-16 23:21:16 +0000 | [diff] [blame] | 320 | if (Key == MFE.Key->getString()) |
| 321 | return MFE.Val; |
| 322 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 323 | return nullptr; |
Manman Ren | 8bfde89 | 2013-07-16 23:21:16 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 326 | /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that |
| 327 | /// represents module-level flags. This method returns null if there are no |
| 328 | /// module-level flags. |
| 329 | NamedMDNode *Module::getModuleFlagsMetadata() const { |
| 330 | return getNamedMetadata("llvm.module.flags"); |
| 331 | } |
| 332 | |
| 333 | /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that |
| 334 | /// represents module-level flags. If module-level flags aren't found, it |
| 335 | /// creates the named metadata that contains them. |
| 336 | NamedMDNode *Module::getOrInsertModuleFlagsMetadata() { |
| 337 | return getOrInsertNamedMetadata("llvm.module.flags"); |
| 338 | } |
| 339 | |
| 340 | /// addModuleFlag - Add a module-level flag to the module-level flags |
| 341 | /// metadata. It will create the module-level flags named metadata if it doesn't |
| 342 | /// already exist. |
Bill Wendling | 89cc166 | 2012-02-16 10:28:10 +0000 | [diff] [blame] | 343 | void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 344 | Metadata *Val) { |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 345 | Type *Int32Ty = Type::getInt32Ty(Context); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 346 | Metadata *Ops[3] = { |
| 347 | ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)), |
| 348 | MDString::get(Context, Key), Val}; |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 349 | getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops)); |
| 350 | } |
Bill Wendling | 89cc166 | 2012-02-16 10:28:10 +0000 | [diff] [blame] | 351 | void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 352 | Constant *Val) { |
| 353 | addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val)); |
| 354 | } |
| 355 | void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key, |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 356 | uint32_t Val) { |
| 357 | Type *Int32Ty = Type::getInt32Ty(Context); |
| 358 | addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val)); |
| 359 | } |
| 360 | void Module::addModuleFlag(MDNode *Node) { |
| 361 | assert(Node->getNumOperands() == 3 && |
| 362 | "Invalid number of operands for module flag!"); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 363 | assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) && |
Bill Wendling | 66f0241 | 2012-02-11 11:38:06 +0000 | [diff] [blame] | 364 | isa<MDString>(Node->getOperand(1)) && |
| 365 | "Invalid operand types for module flag!"); |
| 366 | getOrInsertModuleFlagsMetadata()->addOperand(Node); |
| 367 | } |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 368 | |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 369 | void Module::setDataLayout(StringRef Desc) { |
Rafael Espindola | 248ac13 | 2014-02-25 22:23:04 +0000 | [diff] [blame] | 370 | DL.reset(Desc); |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 373 | void Module::setDataLayout(const DataLayout &Other) { DL = Other; } |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 374 | |
Mehdi Amini | 46a4355 | 2015-03-04 18:43:29 +0000 | [diff] [blame] | 375 | const DataLayout &Module::getDataLayout() const { return DL; } |
Rafael Espindola | f863ee2 | 2014-02-25 20:01:08 +0000 | [diff] [blame] | 376 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 377 | //===----------------------------------------------------------------------===// |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 378 | // Methods to control the materialization of GlobalValues in the Module. |
| 379 | // |
| 380 | void Module::setMaterializer(GVMaterializer *GVM) { |
| 381 | assert(!Materializer && |
| 382 | "Module already has a GVMaterializer. Call MaterializeAllPermanently" |
| 383 | " to clear it out before setting another one."); |
| 384 | Materializer.reset(GVM); |
| 385 | } |
| 386 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 387 | bool Module::isDematerializable(const GlobalValue *GV) const { |
| 388 | if (Materializer) |
| 389 | return Materializer->isDematerializable(GV); |
| 390 | return false; |
| 391 | } |
| 392 | |
Rafael Espindola | 5a52e6d | 2014-10-24 22:50:48 +0000 | [diff] [blame] | 393 | std::error_code Module::materialize(GlobalValue *GV) { |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 394 | if (!Materializer) |
Rafael Espindola | 5a52e6d | 2014-10-24 22:50:48 +0000 | [diff] [blame] | 395 | return std::error_code(); |
Rafael Espindola | 2b11ad4 | 2013-11-05 19:36:34 +0000 | [diff] [blame] | 396 | |
Rafael Espindola | 5a52e6d | 2014-10-24 22:50:48 +0000 | [diff] [blame] | 397 | return Materializer->materialize(GV); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Eric Christopher | 97cb565 | 2015-05-15 18:20:14 +0000 | [diff] [blame] | 400 | void Module::dematerialize(GlobalValue *GV) { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 401 | if (Materializer) |
Eric Christopher | 97cb565 | 2015-05-15 18:20:14 +0000 | [diff] [blame] | 402 | return Materializer->dematerialize(GV); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 405 | std::error_code Module::materializeAll() { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 406 | if (!Materializer) |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 407 | return std::error_code(); |
Eric Christopher | 97cb565 | 2015-05-15 18:20:14 +0000 | [diff] [blame] | 408 | return Materializer->materializeModule(this); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Rafael Espindola | d96d553 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 411 | std::error_code Module::materializeAllPermanently() { |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 412 | if (std::error_code EC = materializeAll()) |
Rafael Espindola | e9fab9b | 2014-01-14 23:51:27 +0000 | [diff] [blame] | 413 | return EC; |
| 414 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 415 | Materializer.reset(); |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 416 | return std::error_code(); |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Rafael Espindola | cba833a | 2015-03-13 21:54:20 +0000 | [diff] [blame] | 419 | std::error_code Module::materializeMetadata() { |
| 420 | if (!Materializer) |
| 421 | return std::error_code(); |
| 422 | return Materializer->materializeMetadata(); |
| 423 | } |
| 424 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 425 | //===----------------------------------------------------------------------===// |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 426 | // Other module related stuff. |
| 427 | // |
| 428 | |
Rafael Espindola | 2fa1e43 | 2014-12-03 07:18:23 +0000 | [diff] [blame] | 429 | std::vector<StructType *> Module::getIdentifiedStructTypes() const { |
| 430 | // If we have a materializer, it is possible that some unread function |
| 431 | // uses a type that is currently not visible to a TypeFinder, so ask |
| 432 | // the materializer which types it created. |
| 433 | if (Materializer) |
| 434 | return Materializer->getIdentifiedStructTypes(); |
| 435 | |
| 436 | std::vector<StructType *> Ret; |
| 437 | TypeFinder SrcStructTypes; |
| 438 | SrcStructTypes.run(*this, true); |
| 439 | Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end()); |
| 440 | return Ret; |
| 441 | } |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 442 | |
Eric Christopher | 7df0240 | 2012-04-16 23:54:31 +0000 | [diff] [blame] | 443 | // dropAllReferences() - This function causes all the subelements to "let go" |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 444 | // of all references that they are maintaining. This allows one to 'delete' a |
| 445 | // whole module at a time, even though there may be circular references... first |
| 446 | // 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] | 447 | // 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] | 448 | // has "dropped all references", except operator delete. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 449 | // |
| 450 | void Module::dropAllReferences() { |
David Majnemer | 3374910 | 2014-07-03 16:12:55 +0000 | [diff] [blame] | 451 | for (Function &F : *this) |
| 452 | F.dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 453 | |
David Majnemer | 3374910 | 2014-07-03 16:12:55 +0000 | [diff] [blame] | 454 | for (GlobalVariable &GV : globals()) |
| 455 | GV.dropAllReferences(); |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 456 | |
David Majnemer | 3374910 | 2014-07-03 16:12:55 +0000 | [diff] [blame] | 457 | for (GlobalAlias &GA : aliases()) |
| 458 | GA.dropAllReferences(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 459 | } |
Diego Novillo | 0915c04 | 2014-04-17 22:33:50 +0000 | [diff] [blame] | 460 | |
| 461 | unsigned Module::getDwarfVersion() const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 462 | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version")); |
Diego Novillo | 0915c04 | 2014-04-17 22:33:50 +0000 | [diff] [blame] | 463 | if (!Val) |
Reid Kleckner | 12d2c12 | 2015-08-05 22:26:20 +0000 | [diff] [blame] | 464 | return 0; |
| 465 | return cast<ConstantInt>(Val->getValue())->getZExtValue(); |
| 466 | } |
| 467 | |
| 468 | unsigned Module::getCodeViewFlag() const { |
| 469 | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView")); |
| 470 | if (!Val) |
| 471 | return 0; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 472 | return cast<ConstantInt>(Val->getValue())->getZExtValue(); |
Diego Novillo | 0915c04 | 2014-04-17 22:33:50 +0000 | [diff] [blame] | 473 | } |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 474 | |
| 475 | Comdat *Module::getOrInsertComdat(StringRef Name) { |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 476 | auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first; |
David Majnemer | dad0a64 | 2014-06-27 18:19:56 +0000 | [diff] [blame] | 477 | Entry.second.Name = &Entry; |
| 478 | return &Entry.second; |
| 479 | } |
Justin Hibbits | 771c132 | 2014-11-07 04:46:10 +0000 | [diff] [blame] | 480 | |
| 481 | PICLevel::Level Module::getPICLevel() const { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 482 | auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level")); |
Justin Hibbits | 771c132 | 2014-11-07 04:46:10 +0000 | [diff] [blame] | 483 | |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 484 | if (!Val) |
Justin Hibbits | 771c132 | 2014-11-07 04:46:10 +0000 | [diff] [blame] | 485 | return PICLevel::Default; |
| 486 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 487 | return static_cast<PICLevel::Level>( |
| 488 | cast<ConstantInt>(Val->getValue())->getZExtValue()); |
Justin Hibbits | 771c132 | 2014-11-07 04:46:10 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | void Module::setPICLevel(PICLevel::Level PL) { |
| 492 | addModuleFlag(ModFlagBehavior::Error, "PIC Level", PL); |
| 493 | } |
Easwaran Raman | ecb05e5 | 2015-12-03 20:57:37 +0000 | [diff] [blame] | 494 | |
| 495 | void Module::setMaximumFunctionCount(uint64_t Count) { |
| 496 | addModuleFlag(ModFlagBehavior::Error, "MaxFunctionCount", Count); |
| 497 | } |
| 498 | |
| 499 | Optional<uint64_t> Module::getMaximumFunctionCount() { |
| 500 | auto *Val = |
| 501 | cast_or_null<ConstantAsMetadata>(getModuleFlag("MaxFunctionCount")); |
| 502 | if (!Val) |
| 503 | return None; |
| 504 | return cast<ConstantInt>(Val->getValue())->getZExtValue(); |
| 505 | } |