blob: 43f42bcf87b5e2c5aa84513d51326548fdcb309e [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"
Chris Lattner113f4f42002-06-25 16:13:24 +000026#include <algorithm>
Chris Lattnerbd717d82003-08-31 00:19:28 +000027#include <cstdarg>
Owen Anderson9eb1a262006-05-18 02:10:31 +000028#include <cstdlib>
Chris Lattner189d19f2003-11-21 20:23:48 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Chris Lattner09bd1a02003-12-31 08:43:01 +000031//===----------------------------------------------------------------------===//
Misha Brukman3bcead72004-04-21 18:27:56 +000032// Methods to implement the globals and functions lists.
Chris Lattner09bd1a02003-12-31 08:43:01 +000033//
34
Chris Lattner113f4f42002-06-25 16:13:24 +000035// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnereef2fe72006-01-24 04:13:11 +000036// are not in the public header file.
John McCall086bb4e2009-12-19 00:55:12 +000037template class llvm::SymbolTableListTraits<Function, Module>;
Nick Lewycky2be81ac2011-08-13 01:04:44 +000038template class llvm::SymbolTableListTraits<GlobalVariable, Module>;
John McCall086bb4e2009-12-19 00:55:12 +000039template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000040
Chris Lattner09bd1a02003-12-31 08:43:01 +000041//===----------------------------------------------------------------------===//
42// Primitive Module methods.
43//
Chris Lattner446ad502001-10-13 06:58:40 +000044
Rafael Espindolaf863ee22014-02-25 20:01:08 +000045Module::Module(StringRef MID, LLVMContext &C)
Ahmed Charles56440fd2014-03-06 05:51:42 +000046 : Context(C), Materializer(), ModuleID(MID), DL("") {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000047 ValSymTab = new ValueSymbolTable();
Dan Gohman2637cc12010-07-21 23:38:33 +000048 NamedMDSymTab = new StringMap<NamedMDNode *>();
Owen Anderson8e89e412010-09-08 18:03:32 +000049 Context.addModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000050}
51
52Module::~Module() {
Owen Anderson8e89e412010-09-08 18:03:32 +000053 Context.removeModule(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000054 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000055 GlobalList.clear();
Chris Lattner113f4f42002-06-25 16:13:24 +000056 FunctionList.clear();
Anton Korobeynikova97b6942007-04-25 14:27:10 +000057 AliasList.clear();
Devang Patel18dfdc92009-07-29 17:16:17 +000058 NamedMDList.clear();
Reid Spencer32af9e82007-01-06 07:24:44 +000059 delete ValSymTab;
Dan Gohman2637cc12010-07-21 23:38:33 +000060 delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
Chris Lattner2f7c9632001-06-06 20:29:01 +000061}
62
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +000063/// getNamedValue - Return the first global value in the module with
64/// the specified name, of arbitrary type. This method returns null
65/// if a global with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000066GlobalValue *Module::getNamedValue(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +000067 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
68}
69
Chris Lattnera0566972009-12-29 09:01:33 +000070/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
71/// This ID is uniqued across modules in the current LLVMContext.
72unsigned Module::getMDKindID(StringRef Name) const {
73 return Context.getMDKindID(Name);
74}
75
76/// getMDKindNames - Populate client supplied SmallVector with the name for
77/// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
78/// so it is filled in as an empty string.
79void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
80 return Context.getMDKindNames(Result);
81}
82
83
Chris Lattner09bd1a02003-12-31 08:43:01 +000084//===----------------------------------------------------------------------===//
85// Methods for easy access to the functions in the module.
86//
87
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000088// getOrInsertFunction - Look up the specified function in the module symbol
89// table. If it does not exist, add a prototype for the function and return
90// it. This is nice because it allows most passes to get away with not handling
91// the symbol table directly for this common task.
92//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000093Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattner229907c2011-07-18 04:54:35 +000094 FunctionType *Ty,
Bill Wendlinge94d8432012-12-07 23:16:57 +000095 AttributeSet AttributeList) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000096 // See if we have a definition for the specified function already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +000097 GlobalValue *F = getNamedValue(Name);
Craig Topperc6207612014-04-09 06:08:46 +000098 if (!F) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000099 // Nope, add it
Gabor Greife9ecc682008-04-06 20:25:17 +0000100 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000101 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
102 New->setAttributes(AttributeList);
Chris Lattnera483b062002-03-29 03:44:18 +0000103 FunctionList.push_back(New);
Chris Lattner505c06b2007-01-07 08:09:25 +0000104 return New; // Return the new prototype.
Chris Lattnera483b062002-03-29 03:44:18 +0000105 }
Chris Lattner505c06b2007-01-07 08:09:25 +0000106
Chris Lattner505c06b2007-01-07 08:09:25 +0000107 // If the function exists but has the wrong type, return a bitcast to the
108 // right type.
Owen Anderson4056ca92009-07-29 22:17:13 +0000109 if (F->getType() != PointerType::getUnqual(Ty))
110 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000111
Chris Lattner505c06b2007-01-07 08:09:25 +0000112 // Otherwise, we just found the existing function or a prototype.
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000113 return F;
Chris Lattnera483b062002-03-29 03:44:18 +0000114}
115
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000116Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattner229907c2011-07-18 04:54:35 +0000117 FunctionType *Ty) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000118 return getOrInsertFunction(Name, Ty, AttributeSet());
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000119}
120
Chris Lattnerbd717d82003-08-31 00:19:28 +0000121// getOrInsertFunction - Look up the specified function in the module symbol
122// table. If it does not exist, add a prototype for the function and return it.
123// This version of the method takes a null terminated list of function
124// arguments, which makes it easier for clients to use.
125//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000126Constant *Module::getOrInsertFunction(StringRef Name,
Bill Wendlinge94d8432012-12-07 23:16:57 +0000127 AttributeSet AttributeList,
Chris Lattner229907c2011-07-18 04:54:35 +0000128 Type *RetTy, ...) {
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000129 va_list Args;
130 va_start(Args, RetTy);
131
132 // Build the list of argument types...
Jay Foadb804a2b2011-07-12 14:06:48 +0000133 std::vector<Type*> ArgTys;
134 while (Type *ArgTy = va_arg(Args, Type*))
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000135 ArgTys.push_back(ArgTy);
136
137 va_end(Args);
138
139 // Build the function type and chain to the other getOrInsertFunction...
Owen Anderson785c56c2009-07-08 23:50:31 +0000140 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000141 FunctionType::get(RetTy, ArgTys, false),
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000142 AttributeList);
143}
144
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000145Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattner229907c2011-07-18 04:54:35 +0000146 Type *RetTy, ...) {
Chris Lattnerbd717d82003-08-31 00:19:28 +0000147 va_list Args;
148 va_start(Args, RetTy);
149
150 // Build the list of argument types...
Jay Foadb804a2b2011-07-12 14:06:48 +0000151 std::vector<Type*> ArgTys;
152 while (Type *ArgTy = va_arg(Args, Type*))
Chris Lattnerbd717d82003-08-31 00:19:28 +0000153 ArgTys.push_back(ArgTy);
154
155 va_end(Args);
156
157 // Build the function type and chain to the other getOrInsertFunction...
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000158 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000159 FunctionType::get(RetTy, ArgTys, false),
Bill Wendlinge94d8432012-12-07 23:16:57 +0000160 AttributeSet());
Chris Lattnerbd717d82003-08-31 00:19:28 +0000161}
162
Chris Lattnera483b062002-03-29 03:44:18 +0000163// getFunction - Look up the specified function in the module symbol table.
164// If it does not exist, return null.
165//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000166Function *Module::getFunction(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000167 return dyn_cast_or_null<Function>(getNamedValue(Name));
Chris Lattnere43649f2008-06-27 21:09:10 +0000168}
169
Chris Lattner09bd1a02003-12-31 08:43:01 +0000170//===----------------------------------------------------------------------===//
171// Methods for easy access to the global variables in the module.
172//
173
174/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000175/// symbol table. If it does not exist, return null. The type argument
176/// should be the underlying type of the global, i.e., it should not have
177/// the top-level PointerType, which represents the address of the global.
Rafael Espindola6de96a12009-01-15 20:18:42 +0000178/// If AllowLocal is set to true, this function will return types that
179/// have an local. By default, these types are not returned.
Chris Lattner09bd1a02003-12-31 08:43:01 +0000180///
Rafael Espindolaec2375f2013-07-25 02:50:08 +0000181GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000182 if (GlobalVariable *Result =
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000183 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
184 if (AllowLocal || !Result->hasLocalLinkage())
Chris Lattner09bd1a02003-12-31 08:43:01 +0000185 return Result;
Craig Topperc6207612014-04-09 06:08:46 +0000186 return nullptr;
Chris Lattner09bd1a02003-12-31 08:43:01 +0000187}
188
Bill Wendlingf5f6f742008-11-05 23:42:27 +0000189/// getOrInsertGlobal - Look up the specified global in the module symbol table.
190/// 1. If it does not exist, add a declaration of the global and return it.
191/// 2. Else, the global exists but has the wrong type: return the function
192/// with a constantexpr cast to the right type.
Matt Arsenault5200fdf2013-09-28 01:08:00 +0000193/// 3. Finally, if the existing global is the correct declaration, return the
Bill Wendlingf5f6f742008-11-05 23:42:27 +0000194/// existing global.
Chris Lattner229907c2011-07-18 04:54:35 +0000195Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
Bill Wendling2f409562008-11-04 22:51:24 +0000196 // See if we have a definition for the specified global already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000197 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
Craig Topperc6207612014-04-09 06:08:46 +0000198 if (!GV) {
Bill Wendling2f409562008-11-04 22:51:24 +0000199 // Nope, add it
200 GlobalVariable *New =
Owen Andersonb17f3292009-07-08 19:03:57 +0000201 new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
Craig Topperc6207612014-04-09 06:08:46 +0000202 nullptr, Name);
Owen Andersonb17f3292009-07-08 19:03:57 +0000203 return New; // Return the new declaration.
Bill Wendling2f409562008-11-04 22:51:24 +0000204 }
205
206 // If the variable exists but has the wrong type, return a bitcast to the
207 // right type.
Matt Arsenault27e783e2013-09-30 21:23:03 +0000208 Type *GVTy = GV->getType();
209 PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
Matt Arsenaulta90a3402013-09-30 23:31:50 +0000210 if (GVTy != PTy)
Matt Arsenault27e783e2013-09-30 21:23:03 +0000211 return ConstantExpr::getBitCast(GV, PTy);
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000212
Bill Wendling2f409562008-11-04 22:51:24 +0000213 // Otherwise, we just found the existing function or a prototype.
214 return GV;
215}
216
Chris Lattner09bd1a02003-12-31 08:43:01 +0000217//===----------------------------------------------------------------------===//
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000218// Methods for easy access to the global variables in the module.
219//
220
221// getNamedAlias - Look up the specified global in the module symbol table.
222// If it does not exist, return null.
223//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000224GlobalAlias *Module::getNamedAlias(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000225 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000226}
227
Devang Patel98250792009-07-30 23:59:04 +0000228/// getNamedMetadata - Return the first NamedMDNode in the module with the
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000229/// specified name. This method returns null if a NamedMDNode with the
Bob Wilson45814342010-06-19 05:33:57 +0000230/// specified name is not found.
Devang Patelb6e058d2010-06-22 01:19:38 +0000231NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
Devang Patela6d20f42010-06-16 00:53:55 +0000232 SmallString<256> NameData;
233 StringRef NameRef = Name.toStringRef(NameData);
Dan Gohman2637cc12010-07-21 23:38:33 +0000234 return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
Devang Patela6d20f42010-06-16 00:53:55 +0000235}
236
Bill Wendlinge32c23a2012-04-23 00:23:33 +0000237/// getOrInsertNamedMetadata - Return the first named MDNode in the module
238/// with the specified name. This method returns a new NamedMDNode if a
Devang Patel98250792009-07-30 23:59:04 +0000239/// NamedMDNode with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000240NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
Dan Gohman2637cc12010-07-21 23:38:33 +0000241 NamedMDNode *&NMD =
242 (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
243 if (!NMD) {
244 NMD = new NamedMDNode(Name);
245 NMD->setParent(this);
246 NamedMDList.push_back(NMD);
247 }
Devang Patel98250792009-07-30 23:59:04 +0000248 return NMD;
249}
250
Bill Wendling66f02412012-02-11 11:38:06 +0000251/// eraseNamedMetadata - Remove the given NamedMDNode from this module and
252/// delete it.
Dan Gohman2637cc12010-07-21 23:38:33 +0000253void Module::eraseNamedMetadata(NamedMDNode *NMD) {
254 static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
255 NamedMDList.erase(NMD);
256}
257
Bill Wendling02949322012-02-15 22:34:20 +0000258/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
259void Module::
260getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
261 const NamedMDNode *ModFlags = getModuleFlagsMetadata();
262 if (!ModFlags) return;
263
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000264 for (const MDNode *Flag : ModFlags->operands()) {
Manman Ren8b4306c2013-12-02 21:29:56 +0000265 if (Flag->getNumOperands() >= 3 && isa<ConstantInt>(Flag->getOperand(0)) &&
266 isa<MDString>(Flag->getOperand(1))) {
267 // Check the operands of the MDNode before accessing the operands.
268 // The verifier will actually catch these failures.
269 ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0));
270 MDString *Key = cast<MDString>(Flag->getOperand(1));
271 Value *Val = Flag->getOperand(2);
272 Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()),
273 Key, Val));
274 }
Bill Wendling02949322012-02-15 22:34:20 +0000275 }
276}
277
Manman Ren8bfde892013-07-16 23:21:16 +0000278/// Return the corresponding value if Key appears in module flags, otherwise
279/// return null.
280Value *Module::getModuleFlag(StringRef Key) const {
281 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
282 getModuleFlagsMetadata(ModuleFlags);
Benjamin Kramer3ad5c962014-03-10 15:03:06 +0000283 for (const ModuleFlagEntry &MFE : ModuleFlags) {
Manman Ren8bfde892013-07-16 23:21:16 +0000284 if (Key == MFE.Key->getString())
285 return MFE.Val;
286 }
Craig Topperc6207612014-04-09 06:08:46 +0000287 return nullptr;
Manman Ren8bfde892013-07-16 23:21:16 +0000288}
289
Bill Wendling66f02412012-02-11 11:38:06 +0000290/// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
291/// represents module-level flags. This method returns null if there are no
292/// module-level flags.
293NamedMDNode *Module::getModuleFlagsMetadata() const {
294 return getNamedMetadata("llvm.module.flags");
295}
296
297/// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
298/// represents module-level flags. If module-level flags aren't found, it
299/// creates the named metadata that contains them.
300NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
301 return getOrInsertNamedMetadata("llvm.module.flags");
302}
303
304/// addModuleFlag - Add a module-level flag to the module-level flags
305/// metadata. It will create the module-level flags named metadata if it doesn't
306/// already exist.
Bill Wendling89cc1662012-02-16 10:28:10 +0000307void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Bill Wendling66f02412012-02-11 11:38:06 +0000308 Value *Val) {
309 Type *Int32Ty = Type::getInt32Ty(Context);
310 Value *Ops[3] = {
311 ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val
312 };
313 getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
314}
Bill Wendling89cc1662012-02-16 10:28:10 +0000315void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Bill Wendling66f02412012-02-11 11:38:06 +0000316 uint32_t Val) {
317 Type *Int32Ty = Type::getInt32Ty(Context);
318 addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
319}
320void Module::addModuleFlag(MDNode *Node) {
321 assert(Node->getNumOperands() == 3 &&
322 "Invalid number of operands for module flag!");
323 assert(isa<ConstantInt>(Node->getOperand(0)) &&
324 isa<MDString>(Node->getOperand(1)) &&
325 "Invalid operand types for module flag!");
326 getOrInsertModuleFlagsMetadata()->addOperand(Node);
327}
Chris Lattner10b7cb52002-04-13 18:58:33 +0000328
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000329void Module::setDataLayout(StringRef Desc) {
Rafael Espindola248ac132014-02-25 22:23:04 +0000330 DL.reset(Desc);
331
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000332 if (Desc.empty()) {
333 DataLayoutStr = "";
334 } else {
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000335 DataLayoutStr = DL.getStringRepresentation();
Rafael Espindola248ac132014-02-25 22:23:04 +0000336 // DataLayoutStr is now equivalent to Desc, but since the representation
337 // is not unique, they may not be identical.
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000338 }
339}
340
341void Module::setDataLayout(const DataLayout *Other) {
342 if (!Other) {
343 DataLayoutStr = "";
Rafael Espindola248ac132014-02-25 22:23:04 +0000344 DL.reset("");
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000345 } else {
346 DL = *Other;
347 DataLayoutStr = DL.getStringRepresentation();
348 }
349}
350
351const DataLayout *Module::getDataLayout() const {
352 if (DataLayoutStr.empty())
Craig Topperc6207612014-04-09 06:08:46 +0000353 return nullptr;
Rafael Espindolaf863ee22014-02-25 20:01:08 +0000354 return &DL;
355}
356
Chris Lattner09bd1a02003-12-31 08:43:01 +0000357//===----------------------------------------------------------------------===//
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000358// Methods to control the materialization of GlobalValues in the Module.
359//
360void Module::setMaterializer(GVMaterializer *GVM) {
361 assert(!Materializer &&
362 "Module already has a GVMaterializer. Call MaterializeAllPermanently"
363 " to clear it out before setting another one.");
364 Materializer.reset(GVM);
365}
366
367bool Module::isMaterializable(const GlobalValue *GV) const {
368 if (Materializer)
369 return Materializer->isMaterializable(GV);
370 return false;
371}
372
373bool Module::isDematerializable(const GlobalValue *GV) const {
374 if (Materializer)
375 return Materializer->isDematerializable(GV);
376 return false;
377}
378
379bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) {
Rafael Espindola2b11ad42013-11-05 19:36:34 +0000380 if (!Materializer)
381 return false;
382
383 error_code EC = Materializer->Materialize(GV);
384 if (!EC)
385 return false;
386 if (ErrInfo)
387 *ErrInfo = EC.message();
388 return true;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000389}
390
391void Module::Dematerialize(GlobalValue *GV) {
392 if (Materializer)
393 return Materializer->Dematerialize(GV);
394}
395
Rafael Espindola1d06f722014-01-14 23:02:01 +0000396error_code Module::materializeAll() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000397 if (!Materializer)
Rafael Espindola1d06f722014-01-14 23:02:01 +0000398 return error_code::success();
399 return Materializer->MaterializeModule(this);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000400}
401
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000402error_code Module::materializeAllPermanently() {
403 if (error_code EC = materializeAll())
404 return EC;
405
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000406 Materializer.reset();
Rafael Espindolae9fab9b2014-01-14 23:51:27 +0000407 return error_code::success();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000408}
409
410//===----------------------------------------------------------------------===//
Chris Lattner09bd1a02003-12-31 08:43:01 +0000411// Other module related stuff.
412//
413
414
Eric Christopher7df02402012-04-16 23:54:31 +0000415// dropAllReferences() - This function causes all the subelements to "let go"
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000416// of all references that they are maintaining. This allows one to 'delete' a
417// whole module at a time, even though there may be circular references... first
418// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000419// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000420// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000421//
422void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000423 for(Module::iterator I = begin(), E = end(); I != E; ++I)
424 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000425
Chris Lattner531f9e92005-03-15 04:54:21 +0000426 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000427 I->dropAllReferences();
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000428
429 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
430 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000431}