xref: /minix/external/bsd/llvm/dist/llvm/lib/IR/Module.cpp (revision 0a6a1f1d)
1f4a2713aSLionel Sambuc //===-- Module.cpp - Implement the Module class ---------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the Module class for the IR library.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "llvm/IR/Module.h"
15f4a2713aSLionel Sambuc #include "SymbolTableListTraitsImpl.h"
16f4a2713aSLionel Sambuc #include "llvm/ADT/DenseSet.h"
17f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h"
18f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
20f4a2713aSLionel Sambuc #include "llvm/IR/Constants.h"
21f4a2713aSLionel Sambuc #include "llvm/IR/DerivedTypes.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/IR/GVMaterializer.h"
23f4a2713aSLionel Sambuc #include "llvm/IR/InstrTypes.h"
24f4a2713aSLionel Sambuc #include "llvm/IR/LLVMContext.h"
25*0a6a1f1dSLionel Sambuc #include "llvm/IR/TypeFinder.h"
26*0a6a1f1dSLionel Sambuc #include "llvm/Support/Dwarf.h"
27*0a6a1f1dSLionel Sambuc #include "llvm/Support/Path.h"
28*0a6a1f1dSLionel Sambuc #include "llvm/Support/RandomNumberGenerator.h"
29f4a2713aSLionel Sambuc #include <algorithm>
30f4a2713aSLionel Sambuc #include <cstdarg>
31f4a2713aSLionel Sambuc #include <cstdlib>
32f4a2713aSLionel Sambuc using namespace llvm;
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
35f4a2713aSLionel Sambuc // Methods to implement the globals and functions lists.
36f4a2713aSLionel Sambuc //
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc // Explicit instantiations of SymbolTableListTraits since some of the methods
39f4a2713aSLionel Sambuc // are not in the public header file.
40f4a2713aSLionel Sambuc template class llvm::SymbolTableListTraits<Function, Module>;
41f4a2713aSLionel Sambuc template class llvm::SymbolTableListTraits<GlobalVariable, Module>;
42f4a2713aSLionel Sambuc template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
45f4a2713aSLionel Sambuc // Primitive Module methods.
46f4a2713aSLionel Sambuc //
47f4a2713aSLionel Sambuc 
Module(StringRef MID,LLVMContext & C)48f4a2713aSLionel Sambuc Module::Module(StringRef MID, LLVMContext &C)
49*0a6a1f1dSLionel Sambuc     : Context(C), Materializer(), ModuleID(MID), DL("") {
50f4a2713aSLionel Sambuc   ValSymTab = new ValueSymbolTable();
51f4a2713aSLionel Sambuc   NamedMDSymTab = new StringMap<NamedMDNode *>();
52f4a2713aSLionel Sambuc   Context.addModule(this);
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc 
~Module()55f4a2713aSLionel Sambuc Module::~Module() {
56f4a2713aSLionel Sambuc   Context.removeModule(this);
57f4a2713aSLionel Sambuc   dropAllReferences();
58f4a2713aSLionel Sambuc   GlobalList.clear();
59f4a2713aSLionel Sambuc   FunctionList.clear();
60f4a2713aSLionel Sambuc   AliasList.clear();
61f4a2713aSLionel Sambuc   NamedMDList.clear();
62f4a2713aSLionel Sambuc   delete ValSymTab;
63f4a2713aSLionel Sambuc   delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
64f4a2713aSLionel Sambuc }
65f4a2713aSLionel Sambuc 
createRNG(const Pass * P) const66*0a6a1f1dSLionel Sambuc RandomNumberGenerator *Module::createRNG(const Pass* P) const {
67*0a6a1f1dSLionel Sambuc   SmallString<32> Salt(P->getPassName());
68f4a2713aSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc   // This RNG is guaranteed to produce the same random stream only
70*0a6a1f1dSLionel Sambuc   // when the Module ID and thus the input filename is the same. This
71*0a6a1f1dSLionel Sambuc   // might be problematic if the input filename extension changes
72*0a6a1f1dSLionel Sambuc   // (e.g. from .c to .bc or .ll).
73*0a6a1f1dSLionel Sambuc   //
74*0a6a1f1dSLionel Sambuc   // We could store this salt in NamedMetadata, but this would make
75*0a6a1f1dSLionel Sambuc   // the parameter non-const. This would unfortunately make this
76*0a6a1f1dSLionel Sambuc   // interface unusable by any Machine passes, since they only have a
77*0a6a1f1dSLionel Sambuc   // const reference to their IR Module. Alternatively we can always
78*0a6a1f1dSLionel Sambuc   // store salt metadata from the Module constructor.
79*0a6a1f1dSLionel Sambuc   Salt += sys::path::filename(getModuleIdentifier());
80f4a2713aSLionel Sambuc 
81*0a6a1f1dSLionel Sambuc   return new RandomNumberGenerator(Salt);
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc /// getNamedValue - Return the first global value in the module with
86f4a2713aSLionel Sambuc /// the specified name, of arbitrary type.  This method returns null
87f4a2713aSLionel Sambuc /// if a global with the specified name is not found.
getNamedValue(StringRef Name) const88f4a2713aSLionel Sambuc GlobalValue *Module::getNamedValue(StringRef Name) const {
89f4a2713aSLionel Sambuc   return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
93f4a2713aSLionel Sambuc /// This ID is uniqued across modules in the current LLVMContext.
getMDKindID(StringRef Name) const94f4a2713aSLionel Sambuc unsigned Module::getMDKindID(StringRef Name) const {
95f4a2713aSLionel Sambuc   return Context.getMDKindID(Name);
96f4a2713aSLionel Sambuc }
97f4a2713aSLionel Sambuc 
98f4a2713aSLionel Sambuc /// getMDKindNames - Populate client supplied SmallVector with the name for
99f4a2713aSLionel Sambuc /// custom metadata IDs registered in this LLVMContext.   ID #0 is not used,
100f4a2713aSLionel Sambuc /// so it is filled in as an empty string.
getMDKindNames(SmallVectorImpl<StringRef> & Result) const101f4a2713aSLionel Sambuc void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
102f4a2713aSLionel Sambuc   return Context.getMDKindNames(Result);
103f4a2713aSLionel Sambuc }
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
107f4a2713aSLionel Sambuc // Methods for easy access to the functions in the module.
108f4a2713aSLionel Sambuc //
109f4a2713aSLionel Sambuc 
110f4a2713aSLionel Sambuc // getOrInsertFunction - Look up the specified function in the module symbol
111f4a2713aSLionel Sambuc // table.  If it does not exist, add a prototype for the function and return
112f4a2713aSLionel Sambuc // it.  This is nice because it allows most passes to get away with not handling
113f4a2713aSLionel Sambuc // the symbol table directly for this common task.
114f4a2713aSLionel Sambuc //
getOrInsertFunction(StringRef Name,FunctionType * Ty,AttributeSet AttributeList)115f4a2713aSLionel Sambuc Constant *Module::getOrInsertFunction(StringRef Name,
116f4a2713aSLionel Sambuc                                       FunctionType *Ty,
117f4a2713aSLionel Sambuc                                       AttributeSet AttributeList) {
118f4a2713aSLionel Sambuc   // See if we have a definition for the specified function already.
119f4a2713aSLionel Sambuc   GlobalValue *F = getNamedValue(Name);
120*0a6a1f1dSLionel Sambuc   if (!F) {
121f4a2713aSLionel Sambuc     // Nope, add it
122f4a2713aSLionel Sambuc     Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
123f4a2713aSLionel Sambuc     if (!New->isIntrinsic())       // Intrinsics get attrs set on construction
124f4a2713aSLionel Sambuc       New->setAttributes(AttributeList);
125f4a2713aSLionel Sambuc     FunctionList.push_back(New);
126f4a2713aSLionel Sambuc     return New;                    // Return the new prototype.
127f4a2713aSLionel Sambuc   }
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc   // If the function exists but has the wrong type, return a bitcast to the
130f4a2713aSLionel Sambuc   // right type.
131f4a2713aSLionel Sambuc   if (F->getType() != PointerType::getUnqual(Ty))
132f4a2713aSLionel Sambuc     return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc   // Otherwise, we just found the existing function or a prototype.
135f4a2713aSLionel Sambuc   return F;
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc 
getOrInsertFunction(StringRef Name,FunctionType * Ty)138f4a2713aSLionel Sambuc Constant *Module::getOrInsertFunction(StringRef Name,
139f4a2713aSLionel Sambuc                                       FunctionType *Ty) {
140f4a2713aSLionel Sambuc   return getOrInsertFunction(Name, Ty, AttributeSet());
141f4a2713aSLionel Sambuc }
142f4a2713aSLionel Sambuc 
143f4a2713aSLionel Sambuc // getOrInsertFunction - Look up the specified function in the module symbol
144f4a2713aSLionel Sambuc // table.  If it does not exist, add a prototype for the function and return it.
145f4a2713aSLionel Sambuc // This version of the method takes a null terminated list of function
146f4a2713aSLionel Sambuc // arguments, which makes it easier for clients to use.
147f4a2713aSLionel Sambuc //
getOrInsertFunction(StringRef Name,AttributeSet AttributeList,Type * RetTy,...)148f4a2713aSLionel Sambuc Constant *Module::getOrInsertFunction(StringRef Name,
149f4a2713aSLionel Sambuc                                       AttributeSet AttributeList,
150f4a2713aSLionel Sambuc                                       Type *RetTy, ...) {
151f4a2713aSLionel Sambuc   va_list Args;
152f4a2713aSLionel Sambuc   va_start(Args, RetTy);
153f4a2713aSLionel Sambuc 
154f4a2713aSLionel Sambuc   // Build the list of argument types...
155f4a2713aSLionel Sambuc   std::vector<Type*> ArgTys;
156f4a2713aSLionel Sambuc   while (Type *ArgTy = va_arg(Args, Type*))
157f4a2713aSLionel Sambuc     ArgTys.push_back(ArgTy);
158f4a2713aSLionel Sambuc 
159f4a2713aSLionel Sambuc   va_end(Args);
160f4a2713aSLionel Sambuc 
161f4a2713aSLionel Sambuc   // Build the function type and chain to the other getOrInsertFunction...
162f4a2713aSLionel Sambuc   return getOrInsertFunction(Name,
163f4a2713aSLionel Sambuc                              FunctionType::get(RetTy, ArgTys, false),
164f4a2713aSLionel Sambuc                              AttributeList);
165f4a2713aSLionel Sambuc }
166f4a2713aSLionel Sambuc 
getOrInsertFunction(StringRef Name,Type * RetTy,...)167f4a2713aSLionel Sambuc Constant *Module::getOrInsertFunction(StringRef Name,
168f4a2713aSLionel Sambuc                                       Type *RetTy, ...) {
169f4a2713aSLionel Sambuc   va_list Args;
170f4a2713aSLionel Sambuc   va_start(Args, RetTy);
171f4a2713aSLionel Sambuc 
172f4a2713aSLionel Sambuc   // Build the list of argument types...
173f4a2713aSLionel Sambuc   std::vector<Type*> ArgTys;
174f4a2713aSLionel Sambuc   while (Type *ArgTy = va_arg(Args, Type*))
175f4a2713aSLionel Sambuc     ArgTys.push_back(ArgTy);
176f4a2713aSLionel Sambuc 
177f4a2713aSLionel Sambuc   va_end(Args);
178f4a2713aSLionel Sambuc 
179f4a2713aSLionel Sambuc   // Build the function type and chain to the other getOrInsertFunction...
180f4a2713aSLionel Sambuc   return getOrInsertFunction(Name,
181f4a2713aSLionel Sambuc                              FunctionType::get(RetTy, ArgTys, false),
182f4a2713aSLionel Sambuc                              AttributeSet());
183f4a2713aSLionel Sambuc }
184f4a2713aSLionel Sambuc 
185f4a2713aSLionel Sambuc // getFunction - Look up the specified function in the module symbol table.
186f4a2713aSLionel Sambuc // If it does not exist, return null.
187f4a2713aSLionel Sambuc //
getFunction(StringRef Name) const188f4a2713aSLionel Sambuc Function *Module::getFunction(StringRef Name) const {
189f4a2713aSLionel Sambuc   return dyn_cast_or_null<Function>(getNamedValue(Name));
190f4a2713aSLionel Sambuc }
191f4a2713aSLionel Sambuc 
192f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
193f4a2713aSLionel Sambuc // Methods for easy access to the global variables in the module.
194f4a2713aSLionel Sambuc //
195f4a2713aSLionel Sambuc 
196f4a2713aSLionel Sambuc /// getGlobalVariable - Look up the specified global variable in the module
197f4a2713aSLionel Sambuc /// symbol table.  If it does not exist, return null.  The type argument
198f4a2713aSLionel Sambuc /// should be the underlying type of the global, i.e., it should not have
199f4a2713aSLionel Sambuc /// the top-level PointerType, which represents the address of the global.
200f4a2713aSLionel Sambuc /// If AllowLocal is set to true, this function will return types that
201f4a2713aSLionel Sambuc /// have an local. By default, these types are not returned.
202f4a2713aSLionel Sambuc ///
getGlobalVariable(StringRef Name,bool AllowLocal)203f4a2713aSLionel Sambuc GlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
204f4a2713aSLionel Sambuc   if (GlobalVariable *Result =
205f4a2713aSLionel Sambuc       dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
206f4a2713aSLionel Sambuc     if (AllowLocal || !Result->hasLocalLinkage())
207f4a2713aSLionel Sambuc       return Result;
208*0a6a1f1dSLionel Sambuc   return nullptr;
209f4a2713aSLionel Sambuc }
210f4a2713aSLionel Sambuc 
211f4a2713aSLionel Sambuc /// getOrInsertGlobal - Look up the specified global in the module symbol table.
212f4a2713aSLionel Sambuc ///   1. If it does not exist, add a declaration of the global and return it.
213f4a2713aSLionel Sambuc ///   2. Else, the global exists but has the wrong type: return the function
214f4a2713aSLionel Sambuc ///      with a constantexpr cast to the right type.
215f4a2713aSLionel Sambuc ///   3. Finally, if the existing global is the correct declaration, return the
216f4a2713aSLionel Sambuc ///      existing global.
getOrInsertGlobal(StringRef Name,Type * Ty)217f4a2713aSLionel Sambuc Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
218f4a2713aSLionel Sambuc   // See if we have a definition for the specified global already.
219f4a2713aSLionel Sambuc   GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
220*0a6a1f1dSLionel Sambuc   if (!GV) {
221f4a2713aSLionel Sambuc     // Nope, add it
222f4a2713aSLionel Sambuc     GlobalVariable *New =
223f4a2713aSLionel Sambuc       new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
224*0a6a1f1dSLionel Sambuc                          nullptr, Name);
225f4a2713aSLionel Sambuc      return New;                    // Return the new declaration.
226f4a2713aSLionel Sambuc   }
227f4a2713aSLionel Sambuc 
228f4a2713aSLionel Sambuc   // If the variable exists but has the wrong type, return a bitcast to the
229f4a2713aSLionel Sambuc   // right type.
230f4a2713aSLionel Sambuc   Type *GVTy = GV->getType();
231f4a2713aSLionel Sambuc   PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
232f4a2713aSLionel Sambuc   if (GVTy != PTy)
233f4a2713aSLionel Sambuc     return ConstantExpr::getBitCast(GV, PTy);
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc   // Otherwise, we just found the existing function or a prototype.
236f4a2713aSLionel Sambuc   return GV;
237f4a2713aSLionel Sambuc }
238f4a2713aSLionel Sambuc 
239f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
240f4a2713aSLionel Sambuc // Methods for easy access to the global variables in the module.
241f4a2713aSLionel Sambuc //
242f4a2713aSLionel Sambuc 
243f4a2713aSLionel Sambuc // getNamedAlias - Look up the specified global in the module symbol table.
244f4a2713aSLionel Sambuc // If it does not exist, return null.
245f4a2713aSLionel Sambuc //
getNamedAlias(StringRef Name) const246f4a2713aSLionel Sambuc GlobalAlias *Module::getNamedAlias(StringRef Name) const {
247f4a2713aSLionel Sambuc   return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
248f4a2713aSLionel Sambuc }
249f4a2713aSLionel Sambuc 
250f4a2713aSLionel Sambuc /// getNamedMetadata - Return the first NamedMDNode in the module with the
251f4a2713aSLionel Sambuc /// specified name. This method returns null if a NamedMDNode with the
252f4a2713aSLionel Sambuc /// specified name is not found.
getNamedMetadata(const Twine & Name) const253f4a2713aSLionel Sambuc NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
254f4a2713aSLionel Sambuc   SmallString<256> NameData;
255f4a2713aSLionel Sambuc   StringRef NameRef = Name.toStringRef(NameData);
256f4a2713aSLionel Sambuc   return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
257f4a2713aSLionel Sambuc }
258f4a2713aSLionel Sambuc 
259f4a2713aSLionel Sambuc /// getOrInsertNamedMetadata - Return the first named MDNode in the module
260f4a2713aSLionel Sambuc /// with the specified name. This method returns a new NamedMDNode if a
261f4a2713aSLionel Sambuc /// NamedMDNode with the specified name is not found.
getOrInsertNamedMetadata(StringRef Name)262f4a2713aSLionel Sambuc NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
263f4a2713aSLionel Sambuc   NamedMDNode *&NMD =
264f4a2713aSLionel Sambuc     (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
265f4a2713aSLionel Sambuc   if (!NMD) {
266f4a2713aSLionel Sambuc     NMD = new NamedMDNode(Name);
267f4a2713aSLionel Sambuc     NMD->setParent(this);
268f4a2713aSLionel Sambuc     NamedMDList.push_back(NMD);
269f4a2713aSLionel Sambuc   }
270f4a2713aSLionel Sambuc   return NMD;
271f4a2713aSLionel Sambuc }
272f4a2713aSLionel Sambuc 
273f4a2713aSLionel Sambuc /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
274f4a2713aSLionel Sambuc /// delete it.
eraseNamedMetadata(NamedMDNode * NMD)275f4a2713aSLionel Sambuc void Module::eraseNamedMetadata(NamedMDNode *NMD) {
276f4a2713aSLionel Sambuc   static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
277f4a2713aSLionel Sambuc   NamedMDList.erase(NMD);
278f4a2713aSLionel Sambuc }
279f4a2713aSLionel Sambuc 
isValidModFlagBehavior(Metadata * MD,ModFlagBehavior & MFB)280*0a6a1f1dSLionel Sambuc bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
281*0a6a1f1dSLionel Sambuc   if (ConstantInt *Behavior = mdconst::dyn_extract<ConstantInt>(MD)) {
282*0a6a1f1dSLionel Sambuc     uint64_t Val = Behavior->getLimitedValue();
283*0a6a1f1dSLionel Sambuc     if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
284*0a6a1f1dSLionel Sambuc       MFB = static_cast<ModFlagBehavior>(Val);
285*0a6a1f1dSLionel Sambuc       return true;
286*0a6a1f1dSLionel Sambuc     }
287*0a6a1f1dSLionel Sambuc   }
288*0a6a1f1dSLionel Sambuc   return false;
289*0a6a1f1dSLionel Sambuc }
290*0a6a1f1dSLionel Sambuc 
291f4a2713aSLionel Sambuc /// getModuleFlagsMetadata - Returns the module flags in the provided vector.
292f4a2713aSLionel Sambuc void Module::
getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> & Flags) const293f4a2713aSLionel Sambuc getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
294f4a2713aSLionel Sambuc   const NamedMDNode *ModFlags = getModuleFlagsMetadata();
295f4a2713aSLionel Sambuc   if (!ModFlags) return;
296f4a2713aSLionel Sambuc 
297*0a6a1f1dSLionel Sambuc   for (const MDNode *Flag : ModFlags->operands()) {
298*0a6a1f1dSLionel Sambuc     ModFlagBehavior MFB;
299*0a6a1f1dSLionel Sambuc     if (Flag->getNumOperands() >= 3 &&
300*0a6a1f1dSLionel Sambuc         isValidModFlagBehavior(Flag->getOperand(0), MFB) &&
301*0a6a1f1dSLionel Sambuc         isa<MDString>(Flag->getOperand(1))) {
302*0a6a1f1dSLionel Sambuc       // Check the operands of the MDNode before accessing the operands.
303*0a6a1f1dSLionel Sambuc       // The verifier will actually catch these failures.
304f4a2713aSLionel Sambuc       MDString *Key = cast<MDString>(Flag->getOperand(1));
305*0a6a1f1dSLionel Sambuc       Metadata *Val = Flag->getOperand(2);
306*0a6a1f1dSLionel Sambuc       Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
307*0a6a1f1dSLionel Sambuc     }
308f4a2713aSLionel Sambuc   }
309f4a2713aSLionel Sambuc }
310f4a2713aSLionel Sambuc 
311f4a2713aSLionel Sambuc /// Return the corresponding value if Key appears in module flags, otherwise
312f4a2713aSLionel Sambuc /// return null.
getModuleFlag(StringRef Key) const313*0a6a1f1dSLionel Sambuc Metadata *Module::getModuleFlag(StringRef Key) const {
314f4a2713aSLionel Sambuc   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
315f4a2713aSLionel Sambuc   getModuleFlagsMetadata(ModuleFlags);
316*0a6a1f1dSLionel Sambuc   for (const ModuleFlagEntry &MFE : ModuleFlags) {
317f4a2713aSLionel Sambuc     if (Key == MFE.Key->getString())
318f4a2713aSLionel Sambuc       return MFE.Val;
319f4a2713aSLionel Sambuc   }
320*0a6a1f1dSLionel Sambuc   return nullptr;
321f4a2713aSLionel Sambuc }
322f4a2713aSLionel Sambuc 
323f4a2713aSLionel Sambuc /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
324f4a2713aSLionel Sambuc /// represents module-level flags. This method returns null if there are no
325f4a2713aSLionel Sambuc /// module-level flags.
getModuleFlagsMetadata() const326f4a2713aSLionel Sambuc NamedMDNode *Module::getModuleFlagsMetadata() const {
327f4a2713aSLionel Sambuc   return getNamedMetadata("llvm.module.flags");
328f4a2713aSLionel Sambuc }
329f4a2713aSLionel Sambuc 
330f4a2713aSLionel Sambuc /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
331f4a2713aSLionel Sambuc /// represents module-level flags. If module-level flags aren't found, it
332f4a2713aSLionel Sambuc /// creates the named metadata that contains them.
getOrInsertModuleFlagsMetadata()333f4a2713aSLionel Sambuc NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
334f4a2713aSLionel Sambuc   return getOrInsertNamedMetadata("llvm.module.flags");
335f4a2713aSLionel Sambuc }
336f4a2713aSLionel Sambuc 
337f4a2713aSLionel Sambuc /// addModuleFlag - Add a module-level flag to the module-level flags
338f4a2713aSLionel Sambuc /// metadata. It will create the module-level flags named metadata if it doesn't
339f4a2713aSLionel Sambuc /// already exist.
addModuleFlag(ModFlagBehavior Behavior,StringRef Key,Metadata * Val)340f4a2713aSLionel Sambuc void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
341*0a6a1f1dSLionel Sambuc                            Metadata *Val) {
342f4a2713aSLionel Sambuc   Type *Int32Ty = Type::getInt32Ty(Context);
343*0a6a1f1dSLionel Sambuc   Metadata *Ops[3] = {
344*0a6a1f1dSLionel Sambuc       ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
345*0a6a1f1dSLionel Sambuc       MDString::get(Context, Key), Val};
346f4a2713aSLionel Sambuc   getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
347f4a2713aSLionel Sambuc }
addModuleFlag(ModFlagBehavior Behavior,StringRef Key,Constant * Val)348f4a2713aSLionel Sambuc void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
349*0a6a1f1dSLionel Sambuc                            Constant *Val) {
350*0a6a1f1dSLionel Sambuc   addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
351*0a6a1f1dSLionel Sambuc }
addModuleFlag(ModFlagBehavior Behavior,StringRef Key,uint32_t Val)352*0a6a1f1dSLionel Sambuc void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
353f4a2713aSLionel Sambuc                            uint32_t Val) {
354f4a2713aSLionel Sambuc   Type *Int32Ty = Type::getInt32Ty(Context);
355f4a2713aSLionel Sambuc   addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
356f4a2713aSLionel Sambuc }
addModuleFlag(MDNode * Node)357f4a2713aSLionel Sambuc void Module::addModuleFlag(MDNode *Node) {
358f4a2713aSLionel Sambuc   assert(Node->getNumOperands() == 3 &&
359f4a2713aSLionel Sambuc          "Invalid number of operands for module flag!");
360*0a6a1f1dSLionel Sambuc   assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
361f4a2713aSLionel Sambuc          isa<MDString>(Node->getOperand(1)) &&
362f4a2713aSLionel Sambuc          "Invalid operand types for module flag!");
363f4a2713aSLionel Sambuc   getOrInsertModuleFlagsMetadata()->addOperand(Node);
364f4a2713aSLionel Sambuc }
365f4a2713aSLionel Sambuc 
setDataLayout(StringRef Desc)366*0a6a1f1dSLionel Sambuc void Module::setDataLayout(StringRef Desc) {
367*0a6a1f1dSLionel Sambuc   DL.reset(Desc);
368*0a6a1f1dSLionel Sambuc 
369*0a6a1f1dSLionel Sambuc   if (Desc.empty()) {
370*0a6a1f1dSLionel Sambuc     DataLayoutStr = "";
371*0a6a1f1dSLionel Sambuc   } else {
372*0a6a1f1dSLionel Sambuc     DataLayoutStr = DL.getStringRepresentation();
373*0a6a1f1dSLionel Sambuc     // DataLayoutStr is now equivalent to Desc, but since the representation
374*0a6a1f1dSLionel Sambuc     // is not unique, they may not be identical.
375*0a6a1f1dSLionel Sambuc   }
376*0a6a1f1dSLionel Sambuc }
377*0a6a1f1dSLionel Sambuc 
setDataLayout(const DataLayout * Other)378*0a6a1f1dSLionel Sambuc void Module::setDataLayout(const DataLayout *Other) {
379*0a6a1f1dSLionel Sambuc   if (!Other) {
380*0a6a1f1dSLionel Sambuc     DataLayoutStr = "";
381*0a6a1f1dSLionel Sambuc     DL.reset("");
382*0a6a1f1dSLionel Sambuc   } else {
383*0a6a1f1dSLionel Sambuc     DL = *Other;
384*0a6a1f1dSLionel Sambuc     DataLayoutStr = DL.getStringRepresentation();
385*0a6a1f1dSLionel Sambuc   }
386*0a6a1f1dSLionel Sambuc }
387*0a6a1f1dSLionel Sambuc 
getDataLayout() const388*0a6a1f1dSLionel Sambuc const DataLayout *Module::getDataLayout() const {
389*0a6a1f1dSLionel Sambuc   if (DataLayoutStr.empty())
390*0a6a1f1dSLionel Sambuc     return nullptr;
391*0a6a1f1dSLionel Sambuc   return &DL;
392*0a6a1f1dSLionel Sambuc }
393*0a6a1f1dSLionel Sambuc 
394f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
395f4a2713aSLionel Sambuc // Methods to control the materialization of GlobalValues in the Module.
396f4a2713aSLionel Sambuc //
setMaterializer(GVMaterializer * GVM)397f4a2713aSLionel Sambuc void Module::setMaterializer(GVMaterializer *GVM) {
398f4a2713aSLionel Sambuc   assert(!Materializer &&
399f4a2713aSLionel Sambuc          "Module already has a GVMaterializer.  Call MaterializeAllPermanently"
400f4a2713aSLionel Sambuc          " to clear it out before setting another one.");
401f4a2713aSLionel Sambuc   Materializer.reset(GVM);
402f4a2713aSLionel Sambuc }
403f4a2713aSLionel Sambuc 
isDematerializable(const GlobalValue * GV) const404f4a2713aSLionel Sambuc bool Module::isDematerializable(const GlobalValue *GV) const {
405f4a2713aSLionel Sambuc   if (Materializer)
406f4a2713aSLionel Sambuc     return Materializer->isDematerializable(GV);
407f4a2713aSLionel Sambuc   return false;
408f4a2713aSLionel Sambuc }
409f4a2713aSLionel Sambuc 
materialize(GlobalValue * GV)410*0a6a1f1dSLionel Sambuc std::error_code Module::materialize(GlobalValue *GV) {
411f4a2713aSLionel Sambuc   if (!Materializer)
412*0a6a1f1dSLionel Sambuc     return std::error_code();
413f4a2713aSLionel Sambuc 
414*0a6a1f1dSLionel Sambuc   return Materializer->materialize(GV);
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc 
Dematerialize(GlobalValue * GV)417f4a2713aSLionel Sambuc void Module::Dematerialize(GlobalValue *GV) {
418f4a2713aSLionel Sambuc   if (Materializer)
419f4a2713aSLionel Sambuc     return Materializer->Dematerialize(GV);
420f4a2713aSLionel Sambuc }
421f4a2713aSLionel Sambuc 
materializeAll()422*0a6a1f1dSLionel Sambuc std::error_code Module::materializeAll() {
423f4a2713aSLionel Sambuc   if (!Materializer)
424*0a6a1f1dSLionel Sambuc     return std::error_code();
425*0a6a1f1dSLionel Sambuc   return Materializer->MaterializeModule(this);
426f4a2713aSLionel Sambuc }
427f4a2713aSLionel Sambuc 
materializeAllPermanently()428*0a6a1f1dSLionel Sambuc std::error_code Module::materializeAllPermanently() {
429*0a6a1f1dSLionel Sambuc   if (std::error_code EC = materializeAll())
430*0a6a1f1dSLionel Sambuc     return EC;
431*0a6a1f1dSLionel Sambuc 
432f4a2713aSLionel Sambuc   Materializer.reset();
433*0a6a1f1dSLionel Sambuc   return std::error_code();
434f4a2713aSLionel Sambuc }
435f4a2713aSLionel Sambuc 
436f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
437f4a2713aSLionel Sambuc // Other module related stuff.
438f4a2713aSLionel Sambuc //
439f4a2713aSLionel Sambuc 
getIdentifiedStructTypes() const440*0a6a1f1dSLionel Sambuc std::vector<StructType *> Module::getIdentifiedStructTypes() const {
441*0a6a1f1dSLionel Sambuc   // If we have a materializer, it is possible that some unread function
442*0a6a1f1dSLionel Sambuc   // uses a type that is currently not visible to a TypeFinder, so ask
443*0a6a1f1dSLionel Sambuc   // the materializer which types it created.
444*0a6a1f1dSLionel Sambuc   if (Materializer)
445*0a6a1f1dSLionel Sambuc     return Materializer->getIdentifiedStructTypes();
446*0a6a1f1dSLionel Sambuc 
447*0a6a1f1dSLionel Sambuc   std::vector<StructType *> Ret;
448*0a6a1f1dSLionel Sambuc   TypeFinder SrcStructTypes;
449*0a6a1f1dSLionel Sambuc   SrcStructTypes.run(*this, true);
450*0a6a1f1dSLionel Sambuc   Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
451*0a6a1f1dSLionel Sambuc   return Ret;
452*0a6a1f1dSLionel Sambuc }
453f4a2713aSLionel Sambuc 
454f4a2713aSLionel Sambuc // dropAllReferences() - This function causes all the subelements to "let go"
455f4a2713aSLionel Sambuc // of all references that they are maintaining.  This allows one to 'delete' a
456f4a2713aSLionel Sambuc // whole module at a time, even though there may be circular references... first
457f4a2713aSLionel Sambuc // all references are dropped, and all use counts go to zero.  Then everything
458f4a2713aSLionel Sambuc // is deleted for real.  Note that no operations are valid on an object that
459f4a2713aSLionel Sambuc // has "dropped all references", except operator delete.
460f4a2713aSLionel Sambuc //
dropAllReferences()461f4a2713aSLionel Sambuc void Module::dropAllReferences() {
462*0a6a1f1dSLionel Sambuc   for (Function &F : *this)
463*0a6a1f1dSLionel Sambuc     F.dropAllReferences();
464f4a2713aSLionel Sambuc 
465*0a6a1f1dSLionel Sambuc   for (GlobalVariable &GV : globals())
466*0a6a1f1dSLionel Sambuc     GV.dropAllReferences();
467f4a2713aSLionel Sambuc 
468*0a6a1f1dSLionel Sambuc   for (GlobalAlias &GA : aliases())
469*0a6a1f1dSLionel Sambuc     GA.dropAllReferences();
470*0a6a1f1dSLionel Sambuc }
471*0a6a1f1dSLionel Sambuc 
getDwarfVersion() const472*0a6a1f1dSLionel Sambuc unsigned Module::getDwarfVersion() const {
473*0a6a1f1dSLionel Sambuc   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
474*0a6a1f1dSLionel Sambuc   if (!Val)
475*0a6a1f1dSLionel Sambuc     return dwarf::DWARF_VERSION;
476*0a6a1f1dSLionel Sambuc   return cast<ConstantInt>(Val->getValue())->getZExtValue();
477*0a6a1f1dSLionel Sambuc }
478*0a6a1f1dSLionel Sambuc 
getOrInsertComdat(StringRef Name)479*0a6a1f1dSLionel Sambuc Comdat *Module::getOrInsertComdat(StringRef Name) {
480*0a6a1f1dSLionel Sambuc   auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
481*0a6a1f1dSLionel Sambuc   Entry.second.Name = &Entry;
482*0a6a1f1dSLionel Sambuc   return &Entry.second;
483*0a6a1f1dSLionel Sambuc }
484*0a6a1f1dSLionel Sambuc 
getPICLevel() const485*0a6a1f1dSLionel Sambuc PICLevel::Level Module::getPICLevel() const {
486*0a6a1f1dSLionel Sambuc   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
487*0a6a1f1dSLionel Sambuc 
488*0a6a1f1dSLionel Sambuc   if (Val == NULL)
489*0a6a1f1dSLionel Sambuc     return PICLevel::Default;
490*0a6a1f1dSLionel Sambuc 
491*0a6a1f1dSLionel Sambuc   return static_cast<PICLevel::Level>(
492*0a6a1f1dSLionel Sambuc       cast<ConstantInt>(Val->getValue())->getZExtValue());
493*0a6a1f1dSLionel Sambuc }
494*0a6a1f1dSLionel Sambuc 
setPICLevel(PICLevel::Level PL)495*0a6a1f1dSLionel Sambuc void Module::setPICLevel(PICLevel::Level PL) {
496*0a6a1f1dSLionel Sambuc   addModuleFlag(ModFlagBehavior::Error, "PIC Level", PL);
497f4a2713aSLionel Sambuc }
498