xref: /llvm-project-15.0.7/llvm/lib/IR/Module.cpp (revision db4ed0bd)
1ef860a24SChandler Carruth //===-- Module.cpp - Implement the Module class ---------------------------===//
2ef860a24SChandler Carruth //
3ef860a24SChandler Carruth //                     The LLVM Compiler Infrastructure
4ef860a24SChandler Carruth //
5ef860a24SChandler Carruth // This file is distributed under the University of Illinois Open Source
6ef860a24SChandler Carruth // License. See LICENSE.TXT for details.
7ef860a24SChandler Carruth //
8ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
9ef860a24SChandler Carruth //
10ef860a24SChandler Carruth // This file implements the Module class for the IR library.
11ef860a24SChandler Carruth //
12ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
13ef860a24SChandler Carruth 
149fb823bbSChandler Carruth #include "llvm/IR/Module.h"
15ef860a24SChandler Carruth #include "SymbolTableListTraitsImpl.h"
16ef860a24SChandler Carruth #include "llvm/ADT/DenseSet.h"
17ef860a24SChandler Carruth #include "llvm/ADT/STLExtras.h"
18ef860a24SChandler Carruth #include "llvm/ADT/SmallString.h"
19ef860a24SChandler Carruth #include "llvm/ADT/StringExtras.h"
209fb823bbSChandler Carruth #include "llvm/IR/Constants.h"
219fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.h"
22d1163aa5SChandler Carruth #include "llvm/IR/GVMaterializer.h"
239fb823bbSChandler Carruth #include "llvm/IR/InstrTypes.h"
249fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h"
254b6845c7SChandler Carruth #include "llvm/IR/LeakDetector.h"
260915c047SDiego Novillo #include "llvm/Support/Dwarf.h"
27ef860a24SChandler Carruth #include <algorithm>
28ef860a24SChandler Carruth #include <cstdarg>
29ef860a24SChandler Carruth #include <cstdlib>
30ef860a24SChandler Carruth using namespace llvm;
31ef860a24SChandler Carruth 
32ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
33ef860a24SChandler Carruth // Methods to implement the globals and functions lists.
34ef860a24SChandler Carruth //
35ef860a24SChandler Carruth 
36ef860a24SChandler Carruth // Explicit instantiations of SymbolTableListTraits since some of the methods
37ef860a24SChandler Carruth // are not in the public header file.
38ef860a24SChandler Carruth template class llvm::SymbolTableListTraits<Function, Module>;
39ef860a24SChandler Carruth template class llvm::SymbolTableListTraits<GlobalVariable, Module>;
40ef860a24SChandler Carruth template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
41ef860a24SChandler Carruth 
42ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
43ef860a24SChandler Carruth // Primitive Module methods.
44ef860a24SChandler Carruth //
45ef860a24SChandler Carruth 
46ef860a24SChandler Carruth Module::Module(StringRef MID, LLVMContext &C)
4756440fd8SAhmed Charles     : Context(C), Materializer(), ModuleID(MID), DL("") {
48ef860a24SChandler Carruth   ValSymTab = new ValueSymbolTable();
49ef860a24SChandler Carruth   NamedMDSymTab = new StringMap<NamedMDNode *>();
50ef860a24SChandler Carruth   Context.addModule(this);
51ef860a24SChandler Carruth }
52ef860a24SChandler Carruth 
53ef860a24SChandler Carruth Module::~Module() {
54ef860a24SChandler Carruth   Context.removeModule(this);
55ef860a24SChandler Carruth   dropAllReferences();
56ef860a24SChandler Carruth   GlobalList.clear();
57ef860a24SChandler Carruth   FunctionList.clear();
58ef860a24SChandler Carruth   AliasList.clear();
59ef860a24SChandler Carruth   NamedMDList.clear();
60ef860a24SChandler Carruth   delete ValSymTab;
61ef860a24SChandler Carruth   delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
62ef860a24SChandler Carruth }
63ef860a24SChandler Carruth 
64ef860a24SChandler Carruth /// getNamedValue - Return the first global value in the module with
65ef860a24SChandler Carruth /// the specified name, of arbitrary type.  This method returns null
66ef860a24SChandler Carruth /// if a global with the specified name is not found.
67ef860a24SChandler Carruth GlobalValue *Module::getNamedValue(StringRef Name) const {
68ef860a24SChandler Carruth   return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
69ef860a24SChandler Carruth }
70ef860a24SChandler Carruth 
71ef860a24SChandler Carruth /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
72ef860a24SChandler Carruth /// This ID is uniqued across modules in the current LLVMContext.
73ef860a24SChandler Carruth unsigned Module::getMDKindID(StringRef Name) const {
74ef860a24SChandler Carruth   return Context.getMDKindID(Name);
75ef860a24SChandler Carruth }
76ef860a24SChandler Carruth 
77ef860a24SChandler Carruth /// getMDKindNames - Populate client supplied SmallVector with the name for
78ef860a24SChandler Carruth /// custom metadata IDs registered in this LLVMContext.   ID #0 is not used,
79ef860a24SChandler Carruth /// so it is filled in as an empty string.
80ef860a24SChandler Carruth void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
81ef860a24SChandler Carruth   return Context.getMDKindNames(Result);
82ef860a24SChandler Carruth }
83ef860a24SChandler Carruth 
84ef860a24SChandler Carruth 
85ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
86ef860a24SChandler Carruth // Methods for easy access to the functions in the module.
87ef860a24SChandler Carruth //
88ef860a24SChandler Carruth 
89ef860a24SChandler Carruth // getOrInsertFunction - Look up the specified function in the module symbol
90ef860a24SChandler Carruth // table.  If it does not exist, add a prototype for the function and return
91ef860a24SChandler Carruth // it.  This is nice because it allows most passes to get away with not handling
92ef860a24SChandler Carruth // the symbol table directly for this common task.
93ef860a24SChandler Carruth //
94ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name,
95ef860a24SChandler Carruth                                       FunctionType *Ty,
96ef860a24SChandler Carruth                                       AttributeSet AttributeList) {
97ef860a24SChandler Carruth   // See if we have a definition for the specified function already.
98ef860a24SChandler Carruth   GlobalValue *F = getNamedValue(Name);
99c620761cSCraig Topper   if (!F) {
100ef860a24SChandler Carruth     // Nope, add it
101ef860a24SChandler Carruth     Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
102ef860a24SChandler Carruth     if (!New->isIntrinsic())       // Intrinsics get attrs set on construction
103ef860a24SChandler Carruth       New->setAttributes(AttributeList);
104ef860a24SChandler Carruth     FunctionList.push_back(New);
105ef860a24SChandler Carruth     return New;                    // Return the new prototype.
106ef860a24SChandler Carruth   }
107ef860a24SChandler Carruth 
108ef860a24SChandler Carruth   // If the function exists but has the wrong type, return a bitcast to the
109ef860a24SChandler Carruth   // right type.
110ef860a24SChandler Carruth   if (F->getType() != PointerType::getUnqual(Ty))
111ef860a24SChandler Carruth     return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
112ef860a24SChandler Carruth 
113ef860a24SChandler Carruth   // Otherwise, we just found the existing function or a prototype.
114ef860a24SChandler Carruth   return F;
115ef860a24SChandler Carruth }
116ef860a24SChandler Carruth 
117ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name,
118ef860a24SChandler Carruth                                       FunctionType *Ty) {
119ef860a24SChandler Carruth   return getOrInsertFunction(Name, Ty, AttributeSet());
120ef860a24SChandler Carruth }
121ef860a24SChandler Carruth 
122ef860a24SChandler Carruth // getOrInsertFunction - Look up the specified function in the module symbol
123ef860a24SChandler Carruth // table.  If it does not exist, add a prototype for the function and return it.
124ef860a24SChandler Carruth // This version of the method takes a null terminated list of function
125ef860a24SChandler Carruth // arguments, which makes it easier for clients to use.
126ef860a24SChandler Carruth //
127ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name,
128ef860a24SChandler Carruth                                       AttributeSet AttributeList,
129ef860a24SChandler Carruth                                       Type *RetTy, ...) {
130ef860a24SChandler Carruth   va_list Args;
131ef860a24SChandler Carruth   va_start(Args, RetTy);
132ef860a24SChandler Carruth 
133ef860a24SChandler Carruth   // Build the list of argument types...
134ef860a24SChandler Carruth   std::vector<Type*> ArgTys;
135ef860a24SChandler Carruth   while (Type *ArgTy = va_arg(Args, Type*))
136ef860a24SChandler Carruth     ArgTys.push_back(ArgTy);
137ef860a24SChandler Carruth 
138ef860a24SChandler Carruth   va_end(Args);
139ef860a24SChandler Carruth 
140ef860a24SChandler Carruth   // Build the function type and chain to the other getOrInsertFunction...
141ef860a24SChandler Carruth   return getOrInsertFunction(Name,
142ef860a24SChandler Carruth                              FunctionType::get(RetTy, ArgTys, false),
143ef860a24SChandler Carruth                              AttributeList);
144ef860a24SChandler Carruth }
145ef860a24SChandler Carruth 
146ef860a24SChandler Carruth Constant *Module::getOrInsertFunction(StringRef Name,
147ef860a24SChandler Carruth                                       Type *RetTy, ...) {
148ef860a24SChandler Carruth   va_list Args;
149ef860a24SChandler Carruth   va_start(Args, RetTy);
150ef860a24SChandler Carruth 
151ef860a24SChandler Carruth   // Build the list of argument types...
152ef860a24SChandler Carruth   std::vector<Type*> ArgTys;
153ef860a24SChandler Carruth   while (Type *ArgTy = va_arg(Args, Type*))
154ef860a24SChandler Carruth     ArgTys.push_back(ArgTy);
155ef860a24SChandler Carruth 
156ef860a24SChandler Carruth   va_end(Args);
157ef860a24SChandler Carruth 
158ef860a24SChandler Carruth   // Build the function type and chain to the other getOrInsertFunction...
159ef860a24SChandler Carruth   return getOrInsertFunction(Name,
160ef860a24SChandler Carruth                              FunctionType::get(RetTy, ArgTys, false),
161ef860a24SChandler Carruth                              AttributeSet());
162ef860a24SChandler Carruth }
163ef860a24SChandler Carruth 
164ef860a24SChandler Carruth // getFunction - Look up the specified function in the module symbol table.
165ef860a24SChandler Carruth // If it does not exist, return null.
166ef860a24SChandler Carruth //
167ef860a24SChandler Carruth Function *Module::getFunction(StringRef Name) const {
168ef860a24SChandler Carruth   return dyn_cast_or_null<Function>(getNamedValue(Name));
169ef860a24SChandler Carruth }
170ef860a24SChandler Carruth 
171ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
172ef860a24SChandler Carruth // Methods for easy access to the global variables in the module.
173ef860a24SChandler Carruth //
174ef860a24SChandler Carruth 
175ef860a24SChandler Carruth /// getGlobalVariable - Look up the specified global variable in the module
176ef860a24SChandler Carruth /// symbol table.  If it does not exist, return null.  The type argument
177ef860a24SChandler Carruth /// should be the underlying type of the global, i.e., it should not have
178ef860a24SChandler Carruth /// the top-level PointerType, which represents the address of the global.
179ef860a24SChandler Carruth /// If AllowLocal is set to true, this function will return types that
180ef860a24SChandler Carruth /// have an local. By default, these types are not returned.
181ef860a24SChandler Carruth ///
182ec2375fbSRafael Espindola GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
183ef860a24SChandler Carruth   if (GlobalVariable *Result =
184ef860a24SChandler Carruth       dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
185ef860a24SChandler Carruth     if (AllowLocal || !Result->hasLocalLinkage())
186ef860a24SChandler Carruth       return Result;
187c620761cSCraig Topper   return nullptr;
188ef860a24SChandler Carruth }
189ef860a24SChandler Carruth 
190ef860a24SChandler Carruth /// getOrInsertGlobal - Look up the specified global in the module symbol table.
191ef860a24SChandler Carruth ///   1. If it does not exist, add a declaration of the global and return it.
192ef860a24SChandler Carruth ///   2. Else, the global exists but has the wrong type: return the function
193ef860a24SChandler Carruth ///      with a constantexpr cast to the right type.
1945200fdf0SMatt Arsenault ///   3. Finally, if the existing global is the correct declaration, return the
195ef860a24SChandler Carruth ///      existing global.
196ef860a24SChandler Carruth Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
197ef860a24SChandler Carruth   // See if we have a definition for the specified global already.
198ef860a24SChandler Carruth   GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
199c620761cSCraig Topper   if (!GV) {
200ef860a24SChandler Carruth     // Nope, add it
201ef860a24SChandler Carruth     GlobalVariable *New =
202ef860a24SChandler Carruth       new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
203c620761cSCraig Topper                          nullptr, Name);
204ef860a24SChandler Carruth      return New;                    // Return the new declaration.
205ef860a24SChandler Carruth   }
206ef860a24SChandler Carruth 
207ef860a24SChandler Carruth   // If the variable exists but has the wrong type, return a bitcast to the
208ef860a24SChandler Carruth   // right type.
20927e783e9SMatt Arsenault   Type *GVTy = GV->getType();
21027e783e9SMatt Arsenault   PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
211a90a340fSMatt Arsenault   if (GVTy != PTy)
21227e783e9SMatt Arsenault     return ConstantExpr::getBitCast(GV, PTy);
213ef860a24SChandler Carruth 
214ef860a24SChandler Carruth   // Otherwise, we just found the existing function or a prototype.
215ef860a24SChandler Carruth   return GV;
216ef860a24SChandler Carruth }
217ef860a24SChandler Carruth 
218ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
219ef860a24SChandler Carruth // Methods for easy access to the global variables in the module.
220ef860a24SChandler Carruth //
221ef860a24SChandler Carruth 
222ef860a24SChandler Carruth // getNamedAlias - Look up the specified global in the module symbol table.
223ef860a24SChandler Carruth // If it does not exist, return null.
224ef860a24SChandler Carruth //
225ef860a24SChandler Carruth GlobalAlias *Module::getNamedAlias(StringRef Name) const {
226ef860a24SChandler Carruth   return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
227ef860a24SChandler Carruth }
228ef860a24SChandler Carruth 
229ef860a24SChandler Carruth /// getNamedMetadata - Return the first NamedMDNode in the module with the
230ef860a24SChandler Carruth /// specified name. This method returns null if a NamedMDNode with the
231ef860a24SChandler Carruth /// specified name is not found.
232ef860a24SChandler Carruth NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
233ef860a24SChandler Carruth   SmallString<256> NameData;
234ef860a24SChandler Carruth   StringRef NameRef = Name.toStringRef(NameData);
235ef860a24SChandler Carruth   return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
236ef860a24SChandler Carruth }
237ef860a24SChandler Carruth 
238ef860a24SChandler Carruth /// getOrInsertNamedMetadata - Return the first named MDNode in the module
239ef860a24SChandler Carruth /// with the specified name. This method returns a new NamedMDNode if a
240ef860a24SChandler Carruth /// NamedMDNode with the specified name is not found.
241ef860a24SChandler Carruth NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
242ef860a24SChandler Carruth   NamedMDNode *&NMD =
243ef860a24SChandler Carruth     (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
244ef860a24SChandler Carruth   if (!NMD) {
245ef860a24SChandler Carruth     NMD = new NamedMDNode(Name);
246ef860a24SChandler Carruth     NMD->setParent(this);
247ef860a24SChandler Carruth     NamedMDList.push_back(NMD);
248ef860a24SChandler Carruth   }
249ef860a24SChandler Carruth   return NMD;
250ef860a24SChandler Carruth }
251ef860a24SChandler Carruth 
252ef860a24SChandler Carruth /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
253ef860a24SChandler Carruth /// delete it.
254ef860a24SChandler Carruth void Module::eraseNamedMetadata(NamedMDNode *NMD) {
255ef860a24SChandler Carruth   static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
256ef860a24SChandler Carruth   NamedMDList.erase(NMD);
257ef860a24SChandler Carruth }
258ef860a24SChandler Carruth 
259ef860a24SChandler Carruth /// getModuleFlagsMetadata - Returns the module flags in the provided vector.
260ef860a24SChandler Carruth void Module::
261ef860a24SChandler Carruth getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
262ef860a24SChandler Carruth   const NamedMDNode *ModFlags = getModuleFlagsMetadata();
263ef860a24SChandler Carruth   if (!ModFlags) return;
264ef860a24SChandler Carruth 
2653ad5c962SBenjamin Kramer   for (const MDNode *Flag : ModFlags->operands()) {
2668b4306ceSManman Ren     if (Flag->getNumOperands() >= 3 && isa<ConstantInt>(Flag->getOperand(0)) &&
2678b4306ceSManman Ren         isa<MDString>(Flag->getOperand(1))) {
2688b4306ceSManman Ren       // Check the operands of the MDNode before accessing the operands.
2698b4306ceSManman Ren       // The verifier will actually catch these failures.
270ef860a24SChandler Carruth       ConstantInt *Behavior = cast<ConstantInt>(Flag->getOperand(0));
271ef860a24SChandler Carruth       MDString *Key = cast<MDString>(Flag->getOperand(1));
272ef860a24SChandler Carruth       Value *Val = Flag->getOperand(2);
273ef860a24SChandler Carruth       Flags.push_back(ModuleFlagEntry(ModFlagBehavior(Behavior->getZExtValue()),
274ef860a24SChandler Carruth                                       Key, Val));
275ef860a24SChandler Carruth     }
276ef860a24SChandler Carruth   }
2778b4306ceSManman Ren }
278ef860a24SChandler Carruth 
2798bfde891SManman Ren /// Return the corresponding value if Key appears in module flags, otherwise
2808bfde891SManman Ren /// return null.
2818bfde891SManman Ren Value *Module::getModuleFlag(StringRef Key) const {
2828bfde891SManman Ren   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
2838bfde891SManman Ren   getModuleFlagsMetadata(ModuleFlags);
2843ad5c962SBenjamin Kramer   for (const ModuleFlagEntry &MFE : ModuleFlags) {
2858bfde891SManman Ren     if (Key == MFE.Key->getString())
2868bfde891SManman Ren       return MFE.Val;
2878bfde891SManman Ren   }
288c620761cSCraig Topper   return nullptr;
2898bfde891SManman Ren }
2908bfde891SManman Ren 
291ef860a24SChandler Carruth /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
292ef860a24SChandler Carruth /// represents module-level flags. This method returns null if there are no
293ef860a24SChandler Carruth /// module-level flags.
294ef860a24SChandler Carruth NamedMDNode *Module::getModuleFlagsMetadata() const {
295ef860a24SChandler Carruth   return getNamedMetadata("llvm.module.flags");
296ef860a24SChandler Carruth }
297ef860a24SChandler Carruth 
298ef860a24SChandler Carruth /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
299ef860a24SChandler Carruth /// represents module-level flags. If module-level flags aren't found, it
300ef860a24SChandler Carruth /// creates the named metadata that contains them.
301ef860a24SChandler Carruth NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
302ef860a24SChandler Carruth   return getOrInsertNamedMetadata("llvm.module.flags");
303ef860a24SChandler Carruth }
304ef860a24SChandler Carruth 
305ef860a24SChandler Carruth /// addModuleFlag - Add a module-level flag to the module-level flags
306ef860a24SChandler Carruth /// metadata. It will create the module-level flags named metadata if it doesn't
307ef860a24SChandler Carruth /// already exist.
308ef860a24SChandler Carruth void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
309ef860a24SChandler Carruth                            Value *Val) {
310ef860a24SChandler Carruth   Type *Int32Ty = Type::getInt32Ty(Context);
311ef860a24SChandler Carruth   Value *Ops[3] = {
312ef860a24SChandler Carruth     ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val
313ef860a24SChandler Carruth   };
314ef860a24SChandler Carruth   getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
315ef860a24SChandler Carruth }
316ef860a24SChandler Carruth void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
317ef860a24SChandler Carruth                            uint32_t Val) {
318ef860a24SChandler Carruth   Type *Int32Ty = Type::getInt32Ty(Context);
319ef860a24SChandler Carruth   addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
320ef860a24SChandler Carruth }
321ef860a24SChandler Carruth void Module::addModuleFlag(MDNode *Node) {
322ef860a24SChandler Carruth   assert(Node->getNumOperands() == 3 &&
323ef860a24SChandler Carruth          "Invalid number of operands for module flag!");
324ef860a24SChandler Carruth   assert(isa<ConstantInt>(Node->getOperand(0)) &&
325ef860a24SChandler Carruth          isa<MDString>(Node->getOperand(1)) &&
326ef860a24SChandler Carruth          "Invalid operand types for module flag!");
327ef860a24SChandler Carruth   getOrInsertModuleFlagsMetadata()->addOperand(Node);
328ef860a24SChandler Carruth }
329ef860a24SChandler Carruth 
330f863ee29SRafael Espindola void Module::setDataLayout(StringRef Desc) {
331248ac139SRafael Espindola   DL.reset(Desc);
332248ac139SRafael Espindola 
333f863ee29SRafael Espindola   if (Desc.empty()) {
334f863ee29SRafael Espindola     DataLayoutStr = "";
335f863ee29SRafael Espindola   } else {
336f863ee29SRafael Espindola     DataLayoutStr = DL.getStringRepresentation();
337248ac139SRafael Espindola     // DataLayoutStr is now equivalent to Desc, but since the representation
338248ac139SRafael Espindola     // is not unique, they may not be identical.
339f863ee29SRafael Espindola   }
340f863ee29SRafael Espindola }
341f863ee29SRafael Espindola 
342f863ee29SRafael Espindola void Module::setDataLayout(const DataLayout *Other) {
343f863ee29SRafael Espindola   if (!Other) {
344f863ee29SRafael Espindola     DataLayoutStr = "";
345248ac139SRafael Espindola     DL.reset("");
346f863ee29SRafael Espindola   } else {
347f863ee29SRafael Espindola     DL = *Other;
348f863ee29SRafael Espindola     DataLayoutStr = DL.getStringRepresentation();
349f863ee29SRafael Espindola   }
350f863ee29SRafael Espindola }
351f863ee29SRafael Espindola 
352f863ee29SRafael Espindola const DataLayout *Module::getDataLayout() const {
353f863ee29SRafael Espindola   if (DataLayoutStr.empty())
354c620761cSCraig Topper     return nullptr;
355f863ee29SRafael Espindola   return &DL;
356f863ee29SRafael Espindola }
357f863ee29SRafael Espindola 
358ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
359ef860a24SChandler Carruth // Methods to control the materialization of GlobalValues in the Module.
360ef860a24SChandler Carruth //
361ef860a24SChandler Carruth void Module::setMaterializer(GVMaterializer *GVM) {
362ef860a24SChandler Carruth   assert(!Materializer &&
363ef860a24SChandler Carruth          "Module already has a GVMaterializer.  Call MaterializeAllPermanently"
364ef860a24SChandler Carruth          " to clear it out before setting another one.");
365ef860a24SChandler Carruth   Materializer.reset(GVM);
366ef860a24SChandler Carruth }
367ef860a24SChandler Carruth 
368ef860a24SChandler Carruth bool Module::isMaterializable(const GlobalValue *GV) const {
369ef860a24SChandler Carruth   if (Materializer)
370ef860a24SChandler Carruth     return Materializer->isMaterializable(GV);
371ef860a24SChandler Carruth   return false;
372ef860a24SChandler Carruth }
373ef860a24SChandler Carruth 
374ef860a24SChandler Carruth bool Module::isDematerializable(const GlobalValue *GV) const {
375ef860a24SChandler Carruth   if (Materializer)
376ef860a24SChandler Carruth     return Materializer->isDematerializable(GV);
377ef860a24SChandler Carruth   return false;
378ef860a24SChandler Carruth }
379ef860a24SChandler Carruth 
380ef860a24SChandler Carruth bool Module::Materialize(GlobalValue *GV, std::string *ErrInfo) {
3812b11ad4fSRafael Espindola   if (!Materializer)
382ef860a24SChandler Carruth     return false;
3832b11ad4fSRafael Espindola 
384*db4ed0bdSRafael Espindola   std::error_code EC = Materializer->Materialize(GV);
3852b11ad4fSRafael Espindola   if (!EC)
3862b11ad4fSRafael Espindola     return false;
3872b11ad4fSRafael Espindola   if (ErrInfo)
3882b11ad4fSRafael Espindola     *ErrInfo = EC.message();
3892b11ad4fSRafael Espindola   return true;
390ef860a24SChandler Carruth }
391ef860a24SChandler Carruth 
392ef860a24SChandler Carruth void Module::Dematerialize(GlobalValue *GV) {
393ef860a24SChandler Carruth   if (Materializer)
394ef860a24SChandler Carruth     return Materializer->Dematerialize(GV);
395ef860a24SChandler Carruth }
396ef860a24SChandler Carruth 
397*db4ed0bdSRafael Espindola std::error_code Module::materializeAll() {
398ef860a24SChandler Carruth   if (!Materializer)
399*db4ed0bdSRafael Espindola     return std::error_code();
4001d06f720SRafael Espindola   return Materializer->MaterializeModule(this);
4011d06f720SRafael Espindola }
4021d06f720SRafael Espindola 
403*db4ed0bdSRafael Espindola std::error_code Module::materializeAllPermanently() {
404*db4ed0bdSRafael Espindola   if (std::error_code EC = materializeAll())
405e9fab9b0SRafael Espindola     return EC;
406e9fab9b0SRafael Espindola 
407ef860a24SChandler Carruth   Materializer.reset();
408*db4ed0bdSRafael Espindola   return std::error_code();
409ef860a24SChandler Carruth }
410ef860a24SChandler Carruth 
411ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
412ef860a24SChandler Carruth // Other module related stuff.
413ef860a24SChandler Carruth //
414ef860a24SChandler Carruth 
415ef860a24SChandler Carruth 
416ef860a24SChandler Carruth // dropAllReferences() - This function causes all the subelements to "let go"
417ef860a24SChandler Carruth // of all references that they are maintaining.  This allows one to 'delete' a
418ef860a24SChandler Carruth // whole module at a time, even though there may be circular references... first
419ef860a24SChandler Carruth // all references are dropped, and all use counts go to zero.  Then everything
420ef860a24SChandler Carruth // is deleted for real.  Note that no operations are valid on an object that
421ef860a24SChandler Carruth // has "dropped all references", except operator delete.
422ef860a24SChandler Carruth //
423ef860a24SChandler Carruth void Module::dropAllReferences() {
424ef860a24SChandler Carruth   for(Module::iterator I = begin(), E = end(); I != E; ++I)
425ef860a24SChandler Carruth     I->dropAllReferences();
426ef860a24SChandler Carruth 
427ef860a24SChandler Carruth   for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
428ef860a24SChandler Carruth     I->dropAllReferences();
429ef860a24SChandler Carruth 
430ef860a24SChandler Carruth   for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
431ef860a24SChandler Carruth     I->dropAllReferences();
432ef860a24SChandler Carruth }
4330915c047SDiego Novillo 
4340915c047SDiego Novillo unsigned Module::getDwarfVersion() const {
4350915c047SDiego Novillo   Value *Val = getModuleFlag("Dwarf Version");
4360915c047SDiego Novillo   if (!Val)
4370915c047SDiego Novillo     return dwarf::DWARF_VERSION;
4380915c047SDiego Novillo   return cast<ConstantInt>(Val)->getZExtValue();
4390915c047SDiego Novillo }
440