blob: 2acd9db210db52e43fc49249b3daa666f324d7e5 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Module.cpp - Implement the Module class ---------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chandler Carruthef860a22013-01-02 09:10:48 +000010// This file implements the Module class for the IR library.
Chris Lattner2f7c9632001-06-06 20:29:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#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 Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Constants.h"
21#include "llvm/IR/DerivedTypes.h"
Chandler Carruthd1163aa2014-03-06 03:50:29 +000022#include "llvm/IR/GVMaterializer.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/InstrTypes.h"
24#include "llvm/IR/LLVMContext.h"
Rafael Espindola2fa1e432014-12-03 07:18:23 +000025#include "llvm/IR/TypeFinder.h"
Diego Novillo0915c042014-04-17 22:33:50 +000026#include "llvm/Support/Dwarf.h"
JF Bastien144829d2014-06-25 15:21:42 +000027#include "llvm/Support/Path.h"
28#include "llvm/Support/RandomNumberGenerator.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000029#include <algorithm>
Chris Lattnerbd717d82003-08-31 00:19:28 +000030#include <cstdarg>
Owen Anderson9eb1a262006-05-18 02:10:31 +000031#include <cstdlib>
Hans Wennborg083ca9b2015-10-06 23:24:35 +000032
Chris Lattner189d19f2003-11-21 20:23:48 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chris Lattner09bd1a02003-12-31 08:43:01 +000035//===----------------------------------------------------------------------===//
Misha Brukman3bcead72004-04-21 18:27:56 +000036// Methods to implement the globals and functions lists.
Chris Lattner09bd1a02003-12-31 08:43:01 +000037//
38
Chris Lattner113f4f42002-06-25 16:13:24 +000039// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnereef2fe72006-01-24 04:13:11 +000040// are not in the public header file.
Duncan P. N. Exon Smith37bf6782015-10-07 20:05:10 +000041template class llvm::SymbolTableListTraits<Function>;
42template class llvm::SymbolTableListTraits<GlobalVariable>;
43template class llvm::SymbolTableListTraits<GlobalAlias>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000044
Chris Lattner09bd1a02003-12-31 08:43:01 +000045//===----------------------------------------------------------------------===//
46// Primitive Module methods.
47//
Chris Lattner446ad502001-10-13 06:58:40 +000048
Rafael Espindolaf863ee22014-02-25 20:01:08 +000049Module::Module(StringRef MID, LLVMContext &C)
JF Bastiene6acbdc2014-12-17 18:12:10 +000050 : Context(C), Materializer(), ModuleID(MID), DL("") {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000051 ValSymTab = new ValueSymbolTable();
Dan Gohman2637cc12010-07-21 23:38:33 +000052 NamedMDSymTab = new StringMap<NamedMDNode *>();
Owen Anderson8e89e412010-09-08 18:03:32 +000053 Context.addModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000054}
55
56Module::~Module() {
Owen Anderson8e89e412010-09-08 18:03:32 +000057 Context.removeModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000058 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000059 GlobalList.clear();
Chris Lattner113f4f42002-06-25 16:13:24 +000060 FunctionList.clear();
Anton Korobeynikova97b6942007-04-25 14:27:10 +000061 AliasList.clear();
Devang Patel18dfdc92009-07-29 17:16:17 +000062 NamedMDList.clear();
Reid Spencer32af9e82007-01-06 07:24:44 +000063 delete ValSymTab;
Dan Gohman2637cc12010-07-21 23:38:33 +000064 delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
Chris Lattner2f7c9632001-06-06 20:29:01 +000065}
66
JF Bastiene6acbdc2014-12-17 18:12:10 +000067RandomNumberGenerator *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 Dunbardcf8d3c2009-03-06 22:04:43 +000085/// 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 Dunbarad36e8a2009-11-06 10:58:06 +000088GlobalValue *Module::getNamedValue(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +000089 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
90}
91
Chris Lattnera0566972009-12-29 09:01:33 +000092/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
93/// This ID is uniqued across modules in the current LLVMContext.
94unsigned 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.
101void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
102 return Context.getMDKindNames(Result);
103}
104
Sanjoy Das9303c242015-09-24 19:14:18 +0000105void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const {
106 return Context.getOperandBundleTags(Result);
107}
Chris Lattnera0566972009-12-29 09:01:33 +0000108
Chris Lattner09bd1a02003-12-31 08:43:01 +0000109//===----------------------------------------------------------------------===//
110// Methods for easy access to the functions in the module.
111//
112
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000113// 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 Dunbarad36e8a2009-11-06 10:58:06 +0000118Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattner229907c2011-07-18 04:54:35 +0000119 FunctionType *Ty,
Bill Wendlinge94d8432012-12-07 23:16:57 +0000120 AttributeSet AttributeList) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000121 // See if we have a definition for the specified function already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000122 GlobalValue *F = getNamedValue(Name);
Craig Topperc6207612014-04-09 06:08:46 +0000123 if (!F) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000124 // Nope, add it
Gabor Greife9ecc682008-04-06 20:25:17 +0000125 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000126 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
127 New->setAttributes(AttributeList);
Chris Lattnera483b062002-03-29 03:44:18 +0000128 FunctionList.push_back(New);
Chris Lattner505c06b2007-01-07 08:09:25 +0000129 return New; // Return the new prototype.
Chris Lattnera483b062002-03-29 03:44:18 +0000130 }
Chris Lattner505c06b2007-01-07 08:09:25 +0000131
Chris Lattner505c06b2007-01-07 08:09:25 +0000132 // If the function exists but has the wrong type, return a bitcast to the
133 // right type.
Owen Anderson4056ca92009-07-29 22:17:13 +0000134 if (F->getType() != PointerType::getUnqual(Ty))
135 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000136
Chris Lattner505c06b2007-01-07 08:09:25 +0000137 // Otherwise, we just found the existing function or a prototype.
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000138 return F;
Chris Lattnera483b062002-03-29 03:44:18 +0000139}
140
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000141Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattner229907c2011-07-18 04:54:35 +0000142 FunctionType *Ty) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000143 return getOrInsertFunction(Name, Ty, AttributeSet());
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000144}
145
Chris Lattnerbd717d82003-08-31 00:19:28 +0000146// 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 Dunbarad36e8a2009-11-06 10:58:06 +0000151Constant *Module::getOrInsertFunction(StringRef Name,
Bill Wendlinge94d8432012-12-07 23:16:57 +0000152 AttributeSet AttributeList,
Chris Lattner229907c2011-07-18 04:54:35 +0000153 Type *RetTy, ...) {
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000154 va_list Args;
155 va_start(Args, RetTy);
156
157 // Build the list of argument types...
Jay Foadb804a2b2011-07-12 14:06:48 +0000158 std::vector<Type*> ArgTys;
159 while (Type *ArgTy = va_arg(Args, Type*))
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000160 ArgTys.push_back(ArgTy);
161
162 va_end(Args);
163
164 // Build the function type and chain to the other getOrInsertFunction...
Owen Anderson785c56c2009-07-08 23:50:31 +0000165 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000166 FunctionType::get(RetTy, ArgTys, false),
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000167 AttributeList);
168}
169
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000170Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattner229907c2011-07-18 04:54:35 +0000171 Type *RetTy, ...) {
Chris Lattnerbd717d82003-08-31 00:19:28 +0000172 va_list Args;
173 va_start(Args, RetTy);
174
175 // Build the list of argument types...
Jay Foadb804a2b2011-07-12 14:06:48 +0000176 std::vector<Type*> ArgTys;
177 while (Type *ArgTy = va_arg(Args, Type*))
Chris Lattnerbd717d82003-08-31 00:19:28 +0000178 ArgTys.push_back(ArgTy);
179
180 va_end(Args);
181
182 // Build the function type and chain to the other getOrInsertFunction...
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000183 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000184 FunctionType::get(RetTy, ArgTys, false),
Bill Wendlinge94d8432012-12-07 23:16:57 +0000185 AttributeSet());
Chris Lattnerbd717d82003-08-31 00:19:28 +0000186}
187
Chris Lattnera483b062002-03-29 03:44:18 +0000188// getFunction - Look up the specified function in the module symbol table.
189// If it does not exist, return null.
190//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000191Function *Module::getFunction(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000192 return dyn_cast_or_null<Function>(getNamedValue(Name));
Chris Lattnere43649f2008-06-27 21:09:10 +0000193}
194
Chris Lattner09bd1a02003-12-31 08:43:01 +0000195//===----------------------------------------------------------------------===//
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 Lattner7d4d93c2005-12-05 05:30:21 +0000200/// 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 Espindola6de96a12009-01-15 20:18:42 +0000203/// If AllowLocal is set to true, this function will return types that
204/// have an local. By default, these types are not returned.
Chris Lattner09bd1a02003-12-31 08:43:01 +0000205///
Rafael Espindolaec2375f2013-07-25 02:50:08 +0000206GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000207 if (GlobalVariable *Result =
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000208 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
209 if (AllowLocal || !Result->hasLocalLinkage())
Chris Lattner09bd1a02003-12-31 08:43:01 +0000210 return Result;
Craig Topperc6207612014-04-09 06:08:46 +0000211 return nullptr;
Chris Lattner09bd1a02003-12-31 08:43:01 +0000212}
213
Bill Wendlingf5f6f742008-11-05 23:42:27 +0000214/// 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 Arsenault5200fdf2013-09-28 01:08:00 +0000218/// 3. Finally, if the existing global is the correct declaration, return the
Bill Wendlingf5f6f742008-11-05 23:42:27 +0000219/// existing global.
Chris Lattner229907c2011-07-18 04:54:35 +0000220Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
Bill Wendling2f409562008-11-04 22:51:24 +0000221 // See if we have a definition for the specified global already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000222 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
Craig Topperc6207612014-04-09 06:08:46 +0000223 if (!GV) {
Bill Wendling2f409562008-11-04 22:51:24 +0000224 // Nope, add it
225 GlobalVariable *New =
Owen Andersonb17f3292009-07-08 19:03:57 +0000226 new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
Craig Topperc6207612014-04-09 06:08:46 +0000227 nullptr, Name);
Owen Andersonb17f3292009-07-08 19:03:57 +0000228 return New; // Return the new declaration.
Bill Wendling2f409562008-11-04 22:51:24 +0000229 }
230
231 // If the variable exists but has the wrong type, return a bitcast to the
232 // right type.
Matt Arsenault27e783e2013-09-30 21:23:03 +0000233 Type *GVTy = GV->getType();
234 PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
Matt Arsenaulta90a3402013-09-30 23:31:50 +0000235 if (GVTy != PTy)
Matt Arsenault27e783e2013-09-30 21:23:03 +0000236 return ConstantExpr::getBitCast(GV, PTy);
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000237
Bill Wendling2f409562008-11-04 22:51:24 +0000238 // Otherwise, we just found the existing function or a prototype.
239 return GV;
240}
241
Chris Lattner09bd1a02003-12-31 08:43:01 +0000242//===----------------------------------------------------------------------===//
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000243// 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 Dunbarad36e8a2009-11-06 10:58:06 +0000249GlobalAlias *Module::getNamedAlias(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000250 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000251}
252
Devang Patel98250792009-07-30 23:59:04 +0000253/// getNamedMetadata - Return the first NamedMDNode in the module with the
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000254/// specified name. This method returns null if a NamedMDNode with the
Bob Wilson45814342010-06-19 05:33:57 +0000255/// specified name is not found.
Devang Patelb6e058d2010-06-22 01:19:38 +0000256NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
Devang Patela6d20f42010-06-16 00:53:55 +0000257 SmallString<256> NameData;
258 StringRef NameRef = Name.toStringRef(NameData);
Dan Gohman2637cc12010-07-21 23:38:33 +0000259 return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
Devang Patela6d20f42010-06-16 00:53:55 +0000260}
261
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000262/// getOrInsertNamedMetadata - Return the first named MDNode in the module
263/// with the specified name. This method returns a new NamedMDNode if a
Devang Patel98250792009-07-30 23:59:04 +0000264/// NamedMDNode with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000265NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
Dan Gohman2637cc12010-07-21 23:38:33 +0000266 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 Patel98250792009-07-30 23:59:04 +0000273 return NMD;
274}
275
Bill Wendling66f02412012-02-11 11:38:06 +0000276/// eraseNamedMetadata - Remove the given NamedMDNode from this module and
277/// delete it.
Dan Gohman2637cc12010-07-21 23:38:33 +0000278void Module::eraseNamedMetadata(NamedMDNode *NMD) {
279 static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000280 NamedMDList.erase(NMD->getIterator());
Dan Gohman2637cc12010-07-21 23:38:33 +0000281}
282
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000283bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
David Majnemerd7677e72015-02-11 09:13:06 +0000284 if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
Alexey Samsonovaf023ad2014-09-08 19:16:28 +0000285 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 Wendling02949322012-02-15 22:34:20 +0000294/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
295void Module::
296getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
297 const NamedMDNode *ModFlags = getModuleFlagsMetadata();
298 if (!ModFlags) return;
299
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000300 for (const MDNode *Flag : ModFlags->operands()) {
Alexey Samsonovaf023ad2014-09-08 19:16:28 +0000301 ModFlagBehavior MFB;
302 if (Flag->getNumOperands() >= 3 &&
303 isValidModFlagBehavior(Flag->getOperand(0), MFB) &&
David Majnemerd7677e72015-02-11 09:13:06 +0000304 dyn_cast_or_null<MDString>(Flag->getOperand(1))) {
Manman Ren8b4306c2013-12-02 21:29:56 +0000305 // Check the operands of the MDNode before accessing the operands.
306 // The verifier will actually catch these failures.
Manman Ren8b4306c2013-12-02 21:29:56 +0000307 MDString *Key = cast<MDString>(Flag->getOperand(1));
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000308 Metadata *Val = Flag->getOperand(2);
Alexey Samsonovaf023ad2014-09-08 19:16:28 +0000309 Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
Manman Ren8b4306c2013-12-02 21:29:56 +0000310 }
Bill Wendling02949322012-02-15 22:34:20 +0000311 }
312}
313
Manman Ren8bfde892013-07-16 23:21:16 +0000314/// Return the corresponding value if Key appears in module flags, otherwise
315/// return null.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000316Metadata *Module::getModuleFlag(StringRef Key) const {
Manman Ren8bfde892013-07-16 23:21:16 +0000317 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
318 getModuleFlagsMetadata(ModuleFlags);
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000319 for (const ModuleFlagEntry &MFE : ModuleFlags) {
Manman Ren8bfde892013-07-16 23:21:16 +0000320 if (Key == MFE.Key->getString())
321 return MFE.Val;
322 }
Craig Topperc6207612014-04-09 06:08:46 +0000323 return nullptr;
Manman Ren8bfde892013-07-16 23:21:16 +0000324}
325
Bill Wendling66f02412012-02-11 11:38:06 +0000326/// 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.
329NamedMDNode *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.
336NamedMDNode *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 Wendling89cc1662012-02-16 10:28:10 +0000343void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000344 Metadata *Val) {
Bill Wendling66f02412012-02-11 11:38:06 +0000345 Type *Int32Ty = Type::getInt32Ty(Context);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000346 Metadata *Ops[3] = {
347 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
348 MDString::get(Context, Key), Val};
Bill Wendling66f02412012-02-11 11:38:06 +0000349 getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
350}
Bill Wendling89cc1662012-02-16 10:28:10 +0000351void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000352 Constant *Val) {
353 addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
354}
355void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Bill Wendling66f02412012-02-11 11:38:06 +0000356 uint32_t Val) {
357 Type *Int32Ty = Type::getInt32Ty(Context);
358 addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
359}
360void Module::addModuleFlag(MDNode *Node) {
361 assert(Node->getNumOperands() == 3 &&
362 "Invalid number of operands for module flag!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000363 assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
Bill Wendling66f02412012-02-11 11:38:06 +0000364 isa<MDString>(Node->getOperand(1)) &&
365 "Invalid operand types for module flag!");
366 getOrInsertModuleFlagsMetadata()->addOperand(Node);
367}
Chris Lattner10b7cb52002-04-13 18:58:33 +0000368
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000369void Module::setDataLayout(StringRef Desc) {
Rafael Espindola248ac132014-02-25 22:23:04 +0000370 DL.reset(Desc);
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000371}
372
Mehdi Amini46a43552015-03-04 18:43:29 +0000373void Module::setDataLayout(const DataLayout &Other) { DL = Other; }
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000374
Mehdi Amini46a43552015-03-04 18:43:29 +0000375const DataLayout &Module::getDataLayout() const { return DL; }
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000376
Chris Lattner09bd1a02003-12-31 08:43:01 +0000377//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000378// Methods to control the materialization of GlobalValues in the Module.
379//
380void 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 Yasskin091217b2010-01-27 20:34:15 +0000387bool Module::isDematerializable(const GlobalValue *GV) const {
388 if (Materializer)
389 return Materializer->isDematerializable(GV);
390 return false;
391}
392
Rafael Espindola5a52e6d2014-10-24 22:50:48 +0000393std::error_code Module::materialize(GlobalValue *GV) {
Rafael Espindola2b11ad42013-11-05 19:36:34 +0000394 if (!Materializer)
Rafael Espindola5a52e6d2014-10-24 22:50:48 +0000395 return std::error_code();
Rafael Espindola2b11ad42013-11-05 19:36:34 +0000396
Rafael Espindola5a52e6d2014-10-24 22:50:48 +0000397 return Materializer->materialize(GV);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000398}
399
Eric Christopher97cb5652015-05-15 18:20:14 +0000400void Module::dematerialize(GlobalValue *GV) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000401 if (Materializer)
Eric Christopher97cb5652015-05-15 18:20:14 +0000402 return Materializer->dematerialize(GV);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000403}
404
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000405std::error_code Module::materializeAll() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000406 if (!Materializer)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000407 return std::error_code();
Eric Christopher97cb5652015-05-15 18:20:14 +0000408 return Materializer->materializeModule(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000409}
410
Rafael Espindolad96d5532014-08-26 21:49:01 +0000411std::error_code Module::materializeAllPermanently() {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000412 if (std::error_code EC = materializeAll())
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000413 return EC;
414
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000415 Materializer.reset();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000416 return std::error_code();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000417}
418
Rafael Espindolacba833a2015-03-13 21:54:20 +0000419std::error_code Module::materializeMetadata() {
420 if (!Materializer)
421 return std::error_code();
422 return Materializer->materializeMetadata();
423}
424
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000425//===----------------------------------------------------------------------===//
Chris Lattner09bd1a02003-12-31 08:43:01 +0000426// Other module related stuff.
427//
428
Rafael Espindola2fa1e432014-12-03 07:18:23 +0000429std::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 Lattner09bd1a02003-12-31 08:43:01 +0000442
Eric Christopher7df02402012-04-16 23:54:31 +0000443// dropAllReferences() - This function causes all the subelements to "let go"
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000444// 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 Brukmanfa100532003-10-10 17:54:14 +0000447// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000448// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000449//
450void Module::dropAllReferences() {
David Majnemer33749102014-07-03 16:12:55 +0000451 for (Function &F : *this)
452 F.dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000453
David Majnemer33749102014-07-03 16:12:55 +0000454 for (GlobalVariable &GV : globals())
455 GV.dropAllReferences();
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000456
David Majnemer33749102014-07-03 16:12:55 +0000457 for (GlobalAlias &GA : aliases())
458 GA.dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000459}
Diego Novillo0915c042014-04-17 22:33:50 +0000460
461unsigned Module::getDwarfVersion() const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000462 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
Diego Novillo0915c042014-04-17 22:33:50 +0000463 if (!Val)
Reid Kleckner12d2c122015-08-05 22:26:20 +0000464 return 0;
465 return cast<ConstantInt>(Val->getValue())->getZExtValue();
466}
467
468unsigned Module::getCodeViewFlag() const {
469 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView"));
470 if (!Val)
471 return 0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000472 return cast<ConstantInt>(Val->getValue())->getZExtValue();
Diego Novillo0915c042014-04-17 22:33:50 +0000473}
David Majnemerdad0a642014-06-27 18:19:56 +0000474
475Comdat *Module::getOrInsertComdat(StringRef Name) {
David Blaikie5106ce72014-11-19 05:49:42 +0000476 auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
David Majnemerdad0a642014-06-27 18:19:56 +0000477 Entry.second.Name = &Entry;
478 return &Entry.second;
479}
Justin Hibbits771c1322014-11-07 04:46:10 +0000480
481PICLevel::Level Module::getPICLevel() const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000482 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
Justin Hibbits771c1322014-11-07 04:46:10 +0000483
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000484 if (!Val)
Justin Hibbits771c1322014-11-07 04:46:10 +0000485 return PICLevel::Default;
486
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000487 return static_cast<PICLevel::Level>(
488 cast<ConstantInt>(Val->getValue())->getZExtValue());
Justin Hibbits771c1322014-11-07 04:46:10 +0000489}
490
491void Module::setPICLevel(PICLevel::Level PL) {
492 addModuleFlag(ModFlagBehavior::Error, "PIC Level", PL);
493}
Easwaran Ramanecb05e52015-12-03 20:57:37 +0000494
495void Module::setMaximumFunctionCount(uint64_t Count) {
496 addModuleFlag(ModFlagBehavior::Error, "MaxFunctionCount", Count);
497}
498
499Optional<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}