blob: 14e534b8b1bbafea8be71058d116673cda8e5762 [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"
Chandler Carruth4b6845c2014-03-04 12:46:06 +000025#include "llvm/IR/LeakDetector.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>
Chris Lattner189d19f2003-11-21 20:23:48 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattner09bd1a02003-12-31 08:43:01 +000034//===----------------------------------------------------------------------===//
Misha Brukman3bcead72004-04-21 18:27:56 +000035// Methods to implement the globals and functions lists.
Chris Lattner09bd1a02003-12-31 08:43:01 +000036//
37
Chris Lattner113f4f42002-06-25 16:13:24 +000038// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnereef2fe72006-01-24 04:13:11 +000039// are not in the public header file.
John McCall086bb4e2009-12-19 00:55:12 +000040template class llvm::SymbolTableListTraits<Function, Module>;
Nick Lewycky2be81ac2011-08-13 01:04:44 +000041template class llvm::SymbolTableListTraits<GlobalVariable, Module>;
John McCall086bb4e2009-12-19 00:55:12 +000042template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000043
Chris Lattner09bd1a02003-12-31 08:43:01 +000044//===----------------------------------------------------------------------===//
45// Primitive Module methods.
46//
Chris Lattner446ad502001-10-13 06:58:40 +000047
Rafael Espindolaf863ee22014-02-25 20:01:08 +000048Module::Module(StringRef MID, LLVMContext &C)
JF Bastien144829d2014-06-25 15:21:42 +000049 : Context(C), Materializer(), ModuleID(MID), RNG(nullptr), DL("") {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000050 ValSymTab = new ValueSymbolTable();
Dan Gohman2637cc12010-07-21 23:38:33 +000051 NamedMDSymTab = new StringMap<NamedMDNode *>();
Owen Anderson8e89e412010-09-08 18:03:32 +000052 Context.addModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000053}
54
55Module::~Module() {
Owen Anderson8e89e412010-09-08 18:03:32 +000056 Context.removeModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000057 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000058 GlobalList.clear();
Chris Lattner113f4f42002-06-25 16:13:24 +000059 FunctionList.clear();
Anton Korobeynikova97b6942007-04-25 14:27:10 +000060 AliasList.clear();
Devang Patel18dfdc92009-07-29 17:16:17 +000061 NamedMDList.clear();
Reid Spencer32af9e82007-01-06 07:24:44 +000062 delete ValSymTab;
Dan Gohman2637cc12010-07-21 23:38:33 +000063 delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
JF Bastien144829d2014-06-25 15:21:42 +000064 delete RNG;
Chris Lattner2f7c9632001-06-06 20:29:01 +000065}
66
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +000067/// getNamedValue - Return the first global value in the module with
68/// the specified name, of arbitrary type. This method returns null
69/// if a global with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000070GlobalValue *Module::getNamedValue(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +000071 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
72}
73
Chris Lattnera0566972009-12-29 09:01:33 +000074/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
75/// This ID is uniqued across modules in the current LLVMContext.
76unsigned Module::getMDKindID(StringRef Name) const {
77 return Context.getMDKindID(Name);
78}
79
80/// getMDKindNames - Populate client supplied SmallVector with the name for
81/// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
82/// so it is filled in as an empty string.
83void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
84 return Context.getMDKindNames(Result);
85}
86
87
Chris Lattner09bd1a02003-12-31 08:43:01 +000088//===----------------------------------------------------------------------===//
89// Methods for easy access to the functions in the module.
90//
91
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000092// getOrInsertFunction - Look up the specified function in the module symbol
93// table. If it does not exist, add a prototype for the function and return
94// it. This is nice because it allows most passes to get away with not handling
95// the symbol table directly for this common task.
96//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000097Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattner229907c2011-07-18 04:54:35 +000098 FunctionType *Ty,
Bill Wendlinge94d8432012-12-07 23:16:57 +000099 AttributeSet AttributeList) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000100 // See if we have a definition for the specified function already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000101 GlobalValue *F = getNamedValue(Name);
Craig Topperc6207612014-04-09 06:08:46 +0000102 if (!F) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000103 // Nope, add it
Gabor Greife9ecc682008-04-06 20:25:17 +0000104 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000105 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
106 New->setAttributes(AttributeList);
Chris Lattnera483b062002-03-29 03:44:18 +0000107 FunctionList.push_back(New);
Chris Lattner505c06b2007-01-07 08:09:25 +0000108 return New; // Return the new prototype.
Chris Lattnera483b062002-03-29 03:44:18 +0000109 }
Chris Lattner505c06b2007-01-07 08:09:25 +0000110
Chris Lattner505c06b2007-01-07 08:09:25 +0000111 // If the function exists but has the wrong type, return a bitcast to the
112 // right type.
Owen Anderson4056ca92009-07-29 22:17:13 +0000113 if (F->getType() != PointerType::getUnqual(Ty))
114 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000115
Chris Lattner505c06b2007-01-07 08:09:25 +0000116 // Otherwise, we just found the existing function or a prototype.
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000117 return F;
Chris Lattnera483b062002-03-29 03:44:18 +0000118}
119
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000120Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattner229907c2011-07-18 04:54:35 +0000121 FunctionType *Ty) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000122 return getOrInsertFunction(Name, Ty, AttributeSet());
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000123}
124
Chris Lattnerbd717d82003-08-31 00:19:28 +0000125// getOrInsertFunction - Look up the specified function in the module symbol
126// table. If it does not exist, add a prototype for the function and return it.
127// This version of the method takes a null terminated list of function
128// arguments, which makes it easier for clients to use.
129//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000130Constant *Module::getOrInsertFunction(StringRef Name,
Bill Wendlinge94d8432012-12-07 23:16:57 +0000131 AttributeSet AttributeList,
Chris Lattner229907c2011-07-18 04:54:35 +0000132 Type *RetTy, ...) {
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000133 va_list Args;
134 va_start(Args, RetTy);
135
136 // Build the list of argument types...
Jay Foadb804a2b2011-07-12 14:06:48 +0000137 std::vector<Type*> ArgTys;
138 while (Type *ArgTy = va_arg(Args, Type*))
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000139 ArgTys.push_back(ArgTy);
140
141 va_end(Args);
142
143 // Build the function type and chain to the other getOrInsertFunction...
Owen Anderson785c56c2009-07-08 23:50:31 +0000144 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000145 FunctionType::get(RetTy, ArgTys, false),
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000146 AttributeList);
147}
148
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000149Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattner229907c2011-07-18 04:54:35 +0000150 Type *RetTy, ...) {
Chris Lattnerbd717d82003-08-31 00:19:28 +0000151 va_list Args;
152 va_start(Args, RetTy);
153
154 // Build the list of argument types...
Jay Foadb804a2b2011-07-12 14:06:48 +0000155 std::vector<Type*> ArgTys;
156 while (Type *ArgTy = va_arg(Args, Type*))
Chris Lattnerbd717d82003-08-31 00:19:28 +0000157 ArgTys.push_back(ArgTy);
158
159 va_end(Args);
160
161 // Build the function type and chain to the other getOrInsertFunction...
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000162 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000163 FunctionType::get(RetTy, ArgTys, false),
Bill Wendlinge94d8432012-12-07 23:16:57 +0000164 AttributeSet());
Chris Lattnerbd717d82003-08-31 00:19:28 +0000165}
166
Chris Lattnera483b062002-03-29 03:44:18 +0000167// getFunction - Look up the specified function in the module symbol table.
168// If it does not exist, return null.
169//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000170Function *Module::getFunction(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000171 return dyn_cast_or_null<Function>(getNamedValue(Name));
Chris Lattnere43649f2008-06-27 21:09:10 +0000172}
173
Chris Lattner09bd1a02003-12-31 08:43:01 +0000174//===----------------------------------------------------------------------===//
175// Methods for easy access to the global variables in the module.
176//
177
178/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000179/// symbol table. If it does not exist, return null. The type argument
180/// should be the underlying type of the global, i.e., it should not have
181/// the top-level PointerType, which represents the address of the global.
Rafael Espindola6de96a12009-01-15 20:18:42 +0000182/// If AllowLocal is set to true, this function will return types that
183/// have an local. By default, these types are not returned.
Chris Lattner09bd1a02003-12-31 08:43:01 +0000184///
Rafael Espindolaec2375f2013-07-25 02:50:08 +0000185GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000186 if (GlobalVariable *Result =
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000187 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
188 if (AllowLocal || !Result->hasLocalLinkage())
Chris Lattner09bd1a02003-12-31 08:43:01 +0000189 return Result;
Craig Topperc6207612014-04-09 06:08:46 +0000190 return nullptr;
Chris Lattner09bd1a02003-12-31 08:43:01 +0000191}
192
Bill Wendlingf5f6f742008-11-05 23:42:27 +0000193/// getOrInsertGlobal - Look up the specified global in the module symbol table.
194/// 1. If it does not exist, add a declaration of the global and return it.
195/// 2. Else, the global exists but has the wrong type: return the function
196/// with a constantexpr cast to the right type.
Matt Arsenault5200fdf2013-09-28 01:08:00 +0000197/// 3. Finally, if the existing global is the correct declaration, return the
Bill Wendlingf5f6f742008-11-05 23:42:27 +0000198/// existing global.
Chris Lattner229907c2011-07-18 04:54:35 +0000199Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
Bill Wendling2f409562008-11-04 22:51:24 +0000200 // See if we have a definition for the specified global already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000201 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
Craig Topperc6207612014-04-09 06:08:46 +0000202 if (!GV) {
Bill Wendling2f409562008-11-04 22:51:24 +0000203 // Nope, add it
204 GlobalVariable *New =
Owen Andersonb17f3292009-07-08 19:03:57 +0000205 new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
Craig Topperc6207612014-04-09 06:08:46 +0000206 nullptr, Name);
Owen Andersonb17f3292009-07-08 19:03:57 +0000207 return New; // Return the new declaration.
Bill Wendling2f409562008-11-04 22:51:24 +0000208 }
209
210 // If the variable exists but has the wrong type, return a bitcast to the
211 // right type.
Matt Arsenault27e783e2013-09-30 21:23:03 +0000212 Type *GVTy = GV->getType();
213 PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
Matt Arsenaulta90a3402013-09-30 23:31:50 +0000214 if (GVTy != PTy)
Matt Arsenault27e783e2013-09-30 21:23:03 +0000215 return ConstantExpr::getBitCast(GV, PTy);
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000216
Bill Wendling2f409562008-11-04 22:51:24 +0000217 // Otherwise, we just found the existing function or a prototype.
218 return GV;
219}
220
Chris Lattner09bd1a02003-12-31 08:43:01 +0000221//===----------------------------------------------------------------------===//
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000222// Methods for easy access to the global variables in the module.
223//
224
225// getNamedAlias - Look up the specified global in the module symbol table.
226// If it does not exist, return null.
227//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000228GlobalAlias *Module::getNamedAlias(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000229 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000230}
231
Devang Patel98250792009-07-30 23:59:04 +0000232/// getNamedMetadata - Return the first NamedMDNode in the module with the
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000233/// specified name. This method returns null if a NamedMDNode with the
Bob Wilson45814342010-06-19 05:33:57 +0000234/// specified name is not found.
Devang Patelb6e058d2010-06-22 01:19:38 +0000235NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
Devang Patela6d20f42010-06-16 00:53:55 +0000236 SmallString<256> NameData;
237 StringRef NameRef = Name.toStringRef(NameData);
Dan Gohman2637cc12010-07-21 23:38:33 +0000238 return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
Devang Patela6d20f42010-06-16 00:53:55 +0000239}
240
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000241/// getOrInsertNamedMetadata - Return the first named MDNode in the module
242/// with the specified name. This method returns a new NamedMDNode if a
Devang Patel98250792009-07-30 23:59:04 +0000243/// NamedMDNode with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000244NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
Dan Gohman2637cc12010-07-21 23:38:33 +0000245 NamedMDNode *&NMD =
246 (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
247 if (!NMD) {
248 NMD = new NamedMDNode(Name);
249 NMD->setParent(this);
250 NamedMDList.push_back(NMD);
251 }
Devang Patel98250792009-07-30 23:59:04 +0000252 return NMD;
253}
254
Bill Wendling66f02412012-02-11 11:38:06 +0000255/// eraseNamedMetadata - Remove the given NamedMDNode from this module and
256/// delete it.
Dan Gohman2637cc12010-07-21 23:38:33 +0000257void Module::eraseNamedMetadata(NamedMDNode *NMD) {
258 static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
259 NamedMDList.erase(NMD);
260}
261
Alexey Samsonovaf023ad2014-09-08 19:16:28 +0000262bool Module::isValidModFlagBehavior(Value *V, ModFlagBehavior &MFB) {
263 if (ConstantInt *Behavior = dyn_cast<ConstantInt>(V)) {
264 uint64_t Val = Behavior->getLimitedValue();
265 if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
266 MFB = static_cast<ModFlagBehavior>(Val);
267 return true;
268 }
269 }
270 return false;
271}
272
Bill Wendling02949322012-02-15 22:34:20 +0000273/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
274void Module::
275getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
276 const NamedMDNode *ModFlags = getModuleFlagsMetadata();
277 if (!ModFlags) return;
278
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000279 for (const MDNode *Flag : ModFlags->operands()) {
Alexey Samsonovaf023ad2014-09-08 19:16:28 +0000280 ModFlagBehavior MFB;
281 if (Flag->getNumOperands() >= 3 &&
282 isValidModFlagBehavior(Flag->getOperand(0), MFB) &&
Manman Ren8b4306c2013-12-02 21:29:56 +0000283 isa<MDString>(Flag->getOperand(1))) {
284 // Check the operands of the MDNode before accessing the operands.
285 // The verifier will actually catch these failures.
Manman Ren8b4306c2013-12-02 21:29:56 +0000286 MDString *Key = cast<MDString>(Flag->getOperand(1));
287 Value *Val = Flag->getOperand(2);
Alexey Samsonovaf023ad2014-09-08 19:16:28 +0000288 Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
Manman Ren8b4306c2013-12-02 21:29:56 +0000289 }
Bill Wendling02949322012-02-15 22:34:20 +0000290 }
291}
292
Manman Ren8bfde892013-07-16 23:21:16 +0000293/// Return the corresponding value if Key appears in module flags, otherwise
294/// return null.
295Value *Module::getModuleFlag(StringRef Key) const {
296 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
297 getModuleFlagsMetadata(ModuleFlags);
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000298 for (const ModuleFlagEntry &MFE : ModuleFlags) {
Manman Ren8bfde892013-07-16 23:21:16 +0000299 if (Key == MFE.Key->getString())
300 return MFE.Val;
301 }
Craig Topperc6207612014-04-09 06:08:46 +0000302 return nullptr;
Manman Ren8bfde892013-07-16 23:21:16 +0000303}
304
Bill Wendling66f02412012-02-11 11:38:06 +0000305/// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
306/// represents module-level flags. This method returns null if there are no
307/// module-level flags.
308NamedMDNode *Module::getModuleFlagsMetadata() const {
309 return getNamedMetadata("llvm.module.flags");
310}
311
312/// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
313/// represents module-level flags. If module-level flags aren't found, it
314/// creates the named metadata that contains them.
315NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
316 return getOrInsertNamedMetadata("llvm.module.flags");
317}
318
319/// addModuleFlag - Add a module-level flag to the module-level flags
320/// metadata. It will create the module-level flags named metadata if it doesn't
321/// already exist.
Bill Wendling89cc1662012-02-16 10:28:10 +0000322void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Bill Wendling66f02412012-02-11 11:38:06 +0000323 Value *Val) {
324 Type *Int32Ty = Type::getInt32Ty(Context);
325 Value *Ops[3] = {
326 ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val
327 };
328 getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
329}
Bill Wendling89cc1662012-02-16 10:28:10 +0000330void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Bill Wendling66f02412012-02-11 11:38:06 +0000331 uint32_t Val) {
332 Type *Int32Ty = Type::getInt32Ty(Context);
333 addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
334}
335void Module::addModuleFlag(MDNode *Node) {
336 assert(Node->getNumOperands() == 3 &&
337 "Invalid number of operands for module flag!");
338 assert(isa<ConstantInt>(Node->getOperand(0)) &&
339 isa<MDString>(Node->getOperand(1)) &&
340 "Invalid operand types for module flag!");
341 getOrInsertModuleFlagsMetadata()->addOperand(Node);
342}
Chris Lattner10b7cb52002-04-13 18:58:33 +0000343
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000344void Module::setDataLayout(StringRef Desc) {
Rafael Espindola248ac132014-02-25 22:23:04 +0000345 DL.reset(Desc);
346
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000347 if (Desc.empty()) {
348 DataLayoutStr = "";
349 } else {
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000350 DataLayoutStr = DL.getStringRepresentation();
Rafael Espindola248ac132014-02-25 22:23:04 +0000351 // DataLayoutStr is now equivalent to Desc, but since the representation
352 // is not unique, they may not be identical.
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000353 }
354}
355
356void Module::setDataLayout(const DataLayout *Other) {
357 if (!Other) {
358 DataLayoutStr = "";
Rafael Espindola248ac132014-02-25 22:23:04 +0000359 DL.reset("");
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000360 } else {
361 DL = *Other;
362 DataLayoutStr = DL.getStringRepresentation();
363 }
364}
365
366const DataLayout *Module::getDataLayout() const {
367 if (DataLayoutStr.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000368 return nullptr;
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000369 return &DL;
370}
371
JF Bastien144829d2014-06-25 15:21:42 +0000372// We want reproducible builds, but ModuleID may be a full path so we just use
373// the filename to salt the RNG (although it is not guaranteed to be unique).
374RandomNumberGenerator &Module::getRNG() const {
375 if (RNG == nullptr) {
376 StringRef Salt = sys::path::filename(ModuleID);
377 RNG = new RandomNumberGenerator(Salt);
378 }
379 return *RNG;
380}
381
Chris Lattner09bd1a02003-12-31 08:43:01 +0000382//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000383// Methods to control the materialization of GlobalValues in the Module.
384//
385void Module::setMaterializer(GVMaterializer *GVM) {
386 assert(!Materializer &&
387 "Module already has a GVMaterializer. Call MaterializeAllPermanently"
388 " to clear it out before setting another one.");
389 Materializer.reset(GVM);
390}
391
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000392bool Module::isDematerializable(const GlobalValue *GV) const {
393 if (Materializer)
394 return Materializer->isDematerializable(GV);
395 return false;
396}
397
Rafael Espindola5a52e6d2014-10-24 22:50:48 +0000398std::error_code Module::materialize(GlobalValue *GV) {
Rafael Espindola2b11ad42013-11-05 19:36:34 +0000399 if (!Materializer)
Rafael Espindola5a52e6d2014-10-24 22:50:48 +0000400 return std::error_code();
Rafael Espindola2b11ad42013-11-05 19:36:34 +0000401
Rafael Espindola5a52e6d2014-10-24 22:50:48 +0000402 return Materializer->materialize(GV);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000403}
404
405void Module::Dematerialize(GlobalValue *GV) {
406 if (Materializer)
407 return Materializer->Dematerialize(GV);
408}
409
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000410std::error_code Module::materializeAll() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000411 if (!Materializer)
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000412 return std::error_code();
Rafael Espindola1d06f722014-01-14 23:02:01 +0000413 return Materializer->MaterializeModule(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000414}
415
Rafael Espindolad96d5532014-08-26 21:49:01 +0000416std::error_code Module::materializeAllPermanently() {
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000417 if (std::error_code EC = materializeAll())
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000418 return EC;
419
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000420 Materializer.reset();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000421 return std::error_code();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000422}
423
424//===----------------------------------------------------------------------===//
Chris Lattner09bd1a02003-12-31 08:43:01 +0000425// Other module related stuff.
426//
427
428
Eric Christopher7df02402012-04-16 23:54:31 +0000429// dropAllReferences() - This function causes all the subelements to "let go"
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000430// of all references that they are maintaining. This allows one to 'delete' a
431// whole module at a time, even though there may be circular references... first
432// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000433// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000434// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000435//
436void Module::dropAllReferences() {
David Majnemer33749102014-07-03 16:12:55 +0000437 for (Function &F : *this)
438 F.dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000439
David Majnemer33749102014-07-03 16:12:55 +0000440 for (GlobalVariable &GV : globals())
441 GV.dropAllReferences();
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000442
David Majnemer33749102014-07-03 16:12:55 +0000443 for (GlobalAlias &GA : aliases())
444 GA.dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000445}
Diego Novillo0915c042014-04-17 22:33:50 +0000446
447unsigned Module::getDwarfVersion() const {
448 Value *Val = getModuleFlag("Dwarf Version");
449 if (!Val)
450 return dwarf::DWARF_VERSION;
451 return cast<ConstantInt>(Val)->getZExtValue();
452}
David Majnemerdad0a642014-06-27 18:19:56 +0000453
454Comdat *Module::getOrInsertComdat(StringRef Name) {
David Blaikie5106ce72014-11-19 05:49:42 +0000455 auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
David Majnemerdad0a642014-06-27 18:19:56 +0000456 Entry.second.Name = &Entry;
457 return &Entry.second;
458}
Justin Hibbits771c1322014-11-07 04:46:10 +0000459
460PICLevel::Level Module::getPICLevel() const {
461 Value *Val = getModuleFlag("PIC Level");
462
463 if (Val == NULL)
464 return PICLevel::Default;
465
466 return static_cast<PICLevel::Level>(cast<ConstantInt>(Val)->getZExtValue());
467}
468
469void Module::setPICLevel(PICLevel::Level PL) {
470 addModuleFlag(ModFlagBehavior::Error, "PIC Level", PL);
471}