1249259Sdim//===-- Module.cpp - Implement the Module class ---------------------------===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10249259Sdim// This file implements the Module class for the IR library.
11249259Sdim//
12249259Sdim//===----------------------------------------------------------------------===//
13249259Sdim
14249259Sdim#include "llvm/IR/Module.h"
15249259Sdim#include "SymbolTableListTraitsImpl.h"
16249259Sdim#include "llvm/ADT/DenseSet.h"
17249259Sdim#include "llvm/ADT/STLExtras.h"
18249259Sdim#include "llvm/ADT/SmallString.h"
19249259Sdim#include "llvm/ADT/StringExtras.h"
20249259Sdim#include "llvm/IR/Constants.h"
21249259Sdim#include "llvm/IR/DerivedTypes.h"
22276479Sdim#include "llvm/IR/GVMaterializer.h"
23249259Sdim#include "llvm/IR/InstrTypes.h"
24249259Sdim#include "llvm/IR/LLVMContext.h"
25280031Sdim#include "llvm/IR/TypeFinder.h"
26276479Sdim#include "llvm/Support/Dwarf.h"
27276479Sdim#include "llvm/Support/Path.h"
28276479Sdim#include "llvm/Support/RandomNumberGenerator.h"
29249259Sdim#include <algorithm>
30249259Sdim#include <cstdarg>
31249259Sdim#include <cstdlib>
32296417Sdim
33249259Sdimusing namespace llvm;
34249259Sdim
35249259Sdim//===----------------------------------------------------------------------===//
36249259Sdim// Methods to implement the globals and functions lists.
37249259Sdim//
38249259Sdim
39249259Sdim// Explicit instantiations of SymbolTableListTraits since some of the methods
40249259Sdim// are not in the public header file.
41296417Sdimtemplate class llvm::SymbolTableListTraits<Function>;
42296417Sdimtemplate class llvm::SymbolTableListTraits<GlobalVariable>;
43296417Sdimtemplate class llvm::SymbolTableListTraits<GlobalAlias>;
44249259Sdim
45249259Sdim//===----------------------------------------------------------------------===//
46249259Sdim// Primitive Module methods.
47249259Sdim//
48249259Sdim
49276479SdimModule::Module(StringRef MID, LLVMContext &C)
50280031Sdim    : Context(C), Materializer(), ModuleID(MID), DL("") {
51249259Sdim  ValSymTab = new ValueSymbolTable();
52249259Sdim  NamedMDSymTab = new StringMap<NamedMDNode *>();
53249259Sdim  Context.addModule(this);
54249259Sdim}
55249259Sdim
56249259SdimModule::~Module() {
57249259Sdim  Context.removeModule(this);
58249259Sdim  dropAllReferences();
59249259Sdim  GlobalList.clear();
60249259Sdim  FunctionList.clear();
61249259Sdim  AliasList.clear();
62249259Sdim  NamedMDList.clear();
63249259Sdim  delete ValSymTab;
64249259Sdim  delete static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab);
65249259Sdim}
66249259Sdim
67280031SdimRandomNumberGenerator *Module::createRNG(const Pass* P) const {
68280031Sdim  SmallString<32> Salt(P->getPassName());
69280031Sdim
70280031Sdim  // This RNG is guaranteed to produce the same random stream only
71280031Sdim  // when the Module ID and thus the input filename is the same. This
72280031Sdim  // might be problematic if the input filename extension changes
73280031Sdim  // (e.g. from .c to .bc or .ll).
74280031Sdim  //
75280031Sdim  // We could store this salt in NamedMetadata, but this would make
76280031Sdim  // the parameter non-const. This would unfortunately make this
77280031Sdim  // interface unusable by any Machine passes, since they only have a
78280031Sdim  // const reference to their IR Module. Alternatively we can always
79280031Sdim  // store salt metadata from the Module constructor.
80280031Sdim  Salt += sys::path::filename(getModuleIdentifier());
81280031Sdim
82280031Sdim  return new RandomNumberGenerator(Salt);
83280031Sdim}
84280031Sdim
85249259Sdim/// getNamedValue - Return the first global value in the module with
86249259Sdim/// the specified name, of arbitrary type.  This method returns null
87249259Sdim/// if a global with the specified name is not found.
88249259SdimGlobalValue *Module::getNamedValue(StringRef Name) const {
89249259Sdim  return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
90249259Sdim}
91249259Sdim
92249259Sdim/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
93249259Sdim/// This ID is uniqued across modules in the current LLVMContext.
94249259Sdimunsigned Module::getMDKindID(StringRef Name) const {
95249259Sdim  return Context.getMDKindID(Name);
96249259Sdim}
97249259Sdim
98249259Sdim/// getMDKindNames - Populate client supplied SmallVector with the name for
99249259Sdim/// custom metadata IDs registered in this LLVMContext.   ID #0 is not used,
100249259Sdim/// so it is filled in as an empty string.
101249259Sdimvoid Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
102249259Sdim  return Context.getMDKindNames(Result);
103249259Sdim}
104249259Sdim
105296417Sdimvoid Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const {
106296417Sdim  return Context.getOperandBundleTags(Result);
107296417Sdim}
108249259Sdim
109249259Sdim//===----------------------------------------------------------------------===//
110249259Sdim// Methods for easy access to the functions in the module.
111249259Sdim//
112249259Sdim
113249259Sdim// getOrInsertFunction - Look up the specified function in the module symbol
114249259Sdim// table.  If it does not exist, add a prototype for the function and return
115249259Sdim// it.  This is nice because it allows most passes to get away with not handling
116249259Sdim// the symbol table directly for this common task.
117249259Sdim//
118249259SdimConstant *Module::getOrInsertFunction(StringRef Name,
119249259Sdim                                      FunctionType *Ty,
120249259Sdim                                      AttributeSet AttributeList) {
121249259Sdim  // See if we have a definition for the specified function already.
122249259Sdim  GlobalValue *F = getNamedValue(Name);
123276479Sdim  if (!F) {
124249259Sdim    // Nope, add it
125249259Sdim    Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
126249259Sdim    if (!New->isIntrinsic())       // Intrinsics get attrs set on construction
127249259Sdim      New->setAttributes(AttributeList);
128249259Sdim    FunctionList.push_back(New);
129249259Sdim    return New;                    // Return the new prototype.
130249259Sdim  }
131249259Sdim
132249259Sdim  // If the function exists but has the wrong type, return a bitcast to the
133249259Sdim  // right type.
134249259Sdim  if (F->getType() != PointerType::getUnqual(Ty))
135249259Sdim    return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
136249259Sdim
137249259Sdim  // Otherwise, we just found the existing function or a prototype.
138249259Sdim  return F;
139249259Sdim}
140249259Sdim
141249259SdimConstant *Module::getOrInsertFunction(StringRef Name,
142249259Sdim                                      FunctionType *Ty) {
143249259Sdim  return getOrInsertFunction(Name, Ty, AttributeSet());
144249259Sdim}
145249259Sdim
146249259Sdim// getOrInsertFunction - Look up the specified function in the module symbol
147249259Sdim// table.  If it does not exist, add a prototype for the function and return it.
148249259Sdim// This version of the method takes a null terminated list of function
149249259Sdim// arguments, which makes it easier for clients to use.
150249259Sdim//
151249259SdimConstant *Module::getOrInsertFunction(StringRef Name,
152249259Sdim                                      AttributeSet AttributeList,
153249259Sdim                                      Type *RetTy, ...) {
154249259Sdim  va_list Args;
155249259Sdim  va_start(Args, RetTy);
156249259Sdim
157249259Sdim  // Build the list of argument types...
158249259Sdim  std::vector<Type*> ArgTys;
159249259Sdim  while (Type *ArgTy = va_arg(Args, Type*))
160249259Sdim    ArgTys.push_back(ArgTy);
161249259Sdim
162249259Sdim  va_end(Args);
163249259Sdim
164249259Sdim  // Build the function type and chain to the other getOrInsertFunction...
165249259Sdim  return getOrInsertFunction(Name,
166249259Sdim                             FunctionType::get(RetTy, ArgTys, false),
167249259Sdim                             AttributeList);
168249259Sdim}
169249259Sdim
170249259SdimConstant *Module::getOrInsertFunction(StringRef Name,
171249259Sdim                                      Type *RetTy, ...) {
172249259Sdim  va_list Args;
173249259Sdim  va_start(Args, RetTy);
174249259Sdim
175249259Sdim  // Build the list of argument types...
176249259Sdim  std::vector<Type*> ArgTys;
177249259Sdim  while (Type *ArgTy = va_arg(Args, Type*))
178249259Sdim    ArgTys.push_back(ArgTy);
179249259Sdim
180249259Sdim  va_end(Args);
181249259Sdim
182249259Sdim  // Build the function type and chain to the other getOrInsertFunction...
183249259Sdim  return getOrInsertFunction(Name,
184249259Sdim                             FunctionType::get(RetTy, ArgTys, false),
185249259Sdim                             AttributeSet());
186249259Sdim}
187249259Sdim
188249259Sdim// getFunction - Look up the specified function in the module symbol table.
189249259Sdim// If it does not exist, return null.
190249259Sdim//
191249259SdimFunction *Module::getFunction(StringRef Name) const {
192249259Sdim  return dyn_cast_or_null<Function>(getNamedValue(Name));
193249259Sdim}
194249259Sdim
195249259Sdim//===----------------------------------------------------------------------===//
196249259Sdim// Methods for easy access to the global variables in the module.
197249259Sdim//
198249259Sdim
199249259Sdim/// getGlobalVariable - Look up the specified global variable in the module
200249259Sdim/// symbol table.  If it does not exist, return null.  The type argument
201249259Sdim/// should be the underlying type of the global, i.e., it should not have
202249259Sdim/// the top-level PointerType, which represents the address of the global.
203249259Sdim/// If AllowLocal is set to true, this function will return types that
204249259Sdim/// have an local. By default, these types are not returned.
205249259Sdim///
206261991SdimGlobalVariable *Module::getGlobalVariable(StringRef Name, bool AllowLocal) {
207249259Sdim  if (GlobalVariable *Result =
208249259Sdim      dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
209249259Sdim    if (AllowLocal || !Result->hasLocalLinkage())
210249259Sdim      return Result;
211276479Sdim  return nullptr;
212249259Sdim}
213249259Sdim
214249259Sdim/// getOrInsertGlobal - Look up the specified global in the module symbol table.
215249259Sdim///   1. If it does not exist, add a declaration of the global and return it.
216249259Sdim///   2. Else, the global exists but has the wrong type: return the function
217249259Sdim///      with a constantexpr cast to the right type.
218261991Sdim///   3. Finally, if the existing global is the correct declaration, return the
219249259Sdim///      existing global.
220249259SdimConstant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
221249259Sdim  // See if we have a definition for the specified global already.
222249259Sdim  GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
223276479Sdim  if (!GV) {
224249259Sdim    // Nope, add it
225249259Sdim    GlobalVariable *New =
226249259Sdim      new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
227276479Sdim                         nullptr, Name);
228249259Sdim     return New;                    // Return the new declaration.
229249259Sdim  }
230249259Sdim
231249259Sdim  // If the variable exists but has the wrong type, return a bitcast to the
232249259Sdim  // right type.
233261991Sdim  Type *GVTy = GV->getType();
234261991Sdim  PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
235261991Sdim  if (GVTy != PTy)
236261991Sdim    return ConstantExpr::getBitCast(GV, PTy);
237249259Sdim
238249259Sdim  // Otherwise, we just found the existing function or a prototype.
239249259Sdim  return GV;
240249259Sdim}
241249259Sdim
242249259Sdim//===----------------------------------------------------------------------===//
243249259Sdim// Methods for easy access to the global variables in the module.
244249259Sdim//
245249259Sdim
246249259Sdim// getNamedAlias - Look up the specified global in the module symbol table.
247249259Sdim// If it does not exist, return null.
248249259Sdim//
249249259SdimGlobalAlias *Module::getNamedAlias(StringRef Name) const {
250249259Sdim  return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
251249259Sdim}
252249259Sdim
253249259Sdim/// getNamedMetadata - Return the first NamedMDNode in the module with the
254249259Sdim/// specified name. This method returns null if a NamedMDNode with the
255249259Sdim/// specified name is not found.
256249259SdimNamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
257249259Sdim  SmallString<256> NameData;
258249259Sdim  StringRef NameRef = Name.toStringRef(NameData);
259249259Sdim  return static_cast<StringMap<NamedMDNode*> *>(NamedMDSymTab)->lookup(NameRef);
260249259Sdim}
261249259Sdim
262249259Sdim/// getOrInsertNamedMetadata - Return the first named MDNode in the module
263249259Sdim/// with the specified name. This method returns a new NamedMDNode if a
264249259Sdim/// NamedMDNode with the specified name is not found.
265249259SdimNamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
266249259Sdim  NamedMDNode *&NMD =
267249259Sdim    (*static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab))[Name];
268249259Sdim  if (!NMD) {
269249259Sdim    NMD = new NamedMDNode(Name);
270249259Sdim    NMD->setParent(this);
271249259Sdim    NamedMDList.push_back(NMD);
272249259Sdim  }
273249259Sdim  return NMD;
274249259Sdim}
275249259Sdim
276249259Sdim/// eraseNamedMetadata - Remove the given NamedMDNode from this module and
277249259Sdim/// delete it.
278249259Sdimvoid Module::eraseNamedMetadata(NamedMDNode *NMD) {
279249259Sdim  static_cast<StringMap<NamedMDNode *> *>(NamedMDSymTab)->erase(NMD->getName());
280296417Sdim  NamedMDList.erase(NMD->getIterator());
281249259Sdim}
282249259Sdim
283280031Sdimbool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
284288943Sdim  if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
285280031Sdim    uint64_t Val = Behavior->getLimitedValue();
286280031Sdim    if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
287280031Sdim      MFB = static_cast<ModFlagBehavior>(Val);
288280031Sdim      return true;
289280031Sdim    }
290280031Sdim  }
291280031Sdim  return false;
292280031Sdim}
293280031Sdim
294249259Sdim/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
295249259Sdimvoid Module::
296249259SdimgetModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
297249259Sdim  const NamedMDNode *ModFlags = getModuleFlagsMetadata();
298249259Sdim  if (!ModFlags) return;
299249259Sdim
300276479Sdim  for (const MDNode *Flag : ModFlags->operands()) {
301280031Sdim    ModFlagBehavior MFB;
302280031Sdim    if (Flag->getNumOperands() >= 3 &&
303280031Sdim        isValidModFlagBehavior(Flag->getOperand(0), MFB) &&
304288943Sdim        dyn_cast_or_null<MDString>(Flag->getOperand(1))) {
305261991Sdim      // Check the operands of the MDNode before accessing the operands.
306261991Sdim      // The verifier will actually catch these failures.
307261991Sdim      MDString *Key = cast<MDString>(Flag->getOperand(1));
308280031Sdim      Metadata *Val = Flag->getOperand(2);
309280031Sdim      Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
310261991Sdim    }
311249259Sdim  }
312249259Sdim}
313249259Sdim
314261991Sdim/// Return the corresponding value if Key appears in module flags, otherwise
315261991Sdim/// return null.
316280031SdimMetadata *Module::getModuleFlag(StringRef Key) const {
317261991Sdim  SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
318261991Sdim  getModuleFlagsMetadata(ModuleFlags);
319276479Sdim  for (const ModuleFlagEntry &MFE : ModuleFlags) {
320261991Sdim    if (Key == MFE.Key->getString())
321261991Sdim      return MFE.Val;
322261991Sdim  }
323276479Sdim  return nullptr;
324261991Sdim}
325261991Sdim
326249259Sdim/// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
327249259Sdim/// represents module-level flags. This method returns null if there are no
328249259Sdim/// module-level flags.
329249259SdimNamedMDNode *Module::getModuleFlagsMetadata() const {
330249259Sdim  return getNamedMetadata("llvm.module.flags");
331249259Sdim}
332249259Sdim
333249259Sdim/// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
334249259Sdim/// represents module-level flags. If module-level flags aren't found, it
335249259Sdim/// creates the named metadata that contains them.
336249259SdimNamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
337249259Sdim  return getOrInsertNamedMetadata("llvm.module.flags");
338249259Sdim}
339249259Sdim
340249259Sdim/// addModuleFlag - Add a module-level flag to the module-level flags
341249259Sdim/// metadata. It will create the module-level flags named metadata if it doesn't
342249259Sdim/// already exist.
343249259Sdimvoid Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
344280031Sdim                           Metadata *Val) {
345249259Sdim  Type *Int32Ty = Type::getInt32Ty(Context);
346280031Sdim  Metadata *Ops[3] = {
347280031Sdim      ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
348280031Sdim      MDString::get(Context, Key), Val};
349249259Sdim  getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
350249259Sdim}
351249259Sdimvoid Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
352280031Sdim                           Constant *Val) {
353280031Sdim  addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
354280031Sdim}
355280031Sdimvoid Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
356249259Sdim                           uint32_t Val) {
357249259Sdim  Type *Int32Ty = Type::getInt32Ty(Context);
358249259Sdim  addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
359249259Sdim}
360249259Sdimvoid Module::addModuleFlag(MDNode *Node) {
361249259Sdim  assert(Node->getNumOperands() == 3 &&
362249259Sdim         "Invalid number of operands for module flag!");
363280031Sdim  assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
364249259Sdim         isa<MDString>(Node->getOperand(1)) &&
365249259Sdim         "Invalid operand types for module flag!");
366249259Sdim  getOrInsertModuleFlagsMetadata()->addOperand(Node);
367249259Sdim}
368249259Sdim
369276479Sdimvoid Module::setDataLayout(StringRef Desc) {
370276479Sdim  DL.reset(Desc);
371276479Sdim}
372276479Sdim
373288943Sdimvoid Module::setDataLayout(const DataLayout &Other) { DL = Other; }
374276479Sdim
375288943Sdimconst DataLayout &Module::getDataLayout() const { return DL; }
376276479Sdim
377249259Sdim//===----------------------------------------------------------------------===//
378249259Sdim// Methods to control the materialization of GlobalValues in the Module.
379249259Sdim//
380249259Sdimvoid Module::setMaterializer(GVMaterializer *GVM) {
381249259Sdim  assert(!Materializer &&
382296417Sdim         "Module already has a GVMaterializer.  Call materializeAll"
383249259Sdim         " to clear it out before setting another one.");
384249259Sdim  Materializer.reset(GVM);
385249259Sdim}
386249259Sdim
387280031Sdimstd::error_code Module::materialize(GlobalValue *GV) {
388261991Sdim  if (!Materializer)
389280031Sdim    return std::error_code();
390261991Sdim
391280031Sdim  return Materializer->materialize(GV);
392249259Sdim}
393249259Sdim
394276479Sdimstd::error_code Module::materializeAll() {
395249259Sdim  if (!Materializer)
396276479Sdim    return std::error_code();
397296417Sdim  std::unique_ptr<GVMaterializer> M = std::move(Materializer);
398296417Sdim  return M->materializeModule();
399249259Sdim}
400249259Sdim
401288943Sdimstd::error_code Module::materializeMetadata() {
402288943Sdim  if (!Materializer)
403288943Sdim    return std::error_code();
404288943Sdim  return Materializer->materializeMetadata();
405288943Sdim}
406288943Sdim
407249259Sdim//===----------------------------------------------------------------------===//
408249259Sdim// Other module related stuff.
409249259Sdim//
410249259Sdim
411280031Sdimstd::vector<StructType *> Module::getIdentifiedStructTypes() const {
412280031Sdim  // If we have a materializer, it is possible that some unread function
413280031Sdim  // uses a type that is currently not visible to a TypeFinder, so ask
414280031Sdim  // the materializer which types it created.
415280031Sdim  if (Materializer)
416280031Sdim    return Materializer->getIdentifiedStructTypes();
417249259Sdim
418280031Sdim  std::vector<StructType *> Ret;
419280031Sdim  TypeFinder SrcStructTypes;
420280031Sdim  SrcStructTypes.run(*this, true);
421280031Sdim  Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
422280031Sdim  return Ret;
423280031Sdim}
424280031Sdim
425249259Sdim// dropAllReferences() - This function causes all the subelements to "let go"
426249259Sdim// of all references that they are maintaining.  This allows one to 'delete' a
427249259Sdim// whole module at a time, even though there may be circular references... first
428249259Sdim// all references are dropped, and all use counts go to zero.  Then everything
429249259Sdim// is deleted for real.  Note that no operations are valid on an object that
430249259Sdim// has "dropped all references", except operator delete.
431249259Sdim//
432249259Sdimvoid Module::dropAllReferences() {
433276479Sdim  for (Function &F : *this)
434276479Sdim    F.dropAllReferences();
435249259Sdim
436276479Sdim  for (GlobalVariable &GV : globals())
437276479Sdim    GV.dropAllReferences();
438249259Sdim
439276479Sdim  for (GlobalAlias &GA : aliases())
440276479Sdim    GA.dropAllReferences();
441249259Sdim}
442276479Sdim
443276479Sdimunsigned Module::getDwarfVersion() const {
444280031Sdim  auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
445276479Sdim  if (!Val)
446296417Sdim    return 0;
447280031Sdim  return cast<ConstantInt>(Val->getValue())->getZExtValue();
448276479Sdim}
449276479Sdim
450296417Sdimunsigned Module::getCodeViewFlag() const {
451296417Sdim  auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView"));
452296417Sdim  if (!Val)
453296417Sdim    return 0;
454296417Sdim  return cast<ConstantInt>(Val->getValue())->getZExtValue();
455296417Sdim}
456296417Sdim
457276479SdimComdat *Module::getOrInsertComdat(StringRef Name) {
458280031Sdim  auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
459276479Sdim  Entry.second.Name = &Entry;
460276479Sdim  return &Entry.second;
461276479Sdim}
462276479Sdim
463276479SdimPICLevel::Level Module::getPICLevel() const {
464280031Sdim  auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
465276479Sdim
466296417Sdim  if (!Val)
467276479Sdim    return PICLevel::Default;
468276479Sdim
469280031Sdim  return static_cast<PICLevel::Level>(
470280031Sdim      cast<ConstantInt>(Val->getValue())->getZExtValue());
471276479Sdim}
472276479Sdim
473276479Sdimvoid Module::setPICLevel(PICLevel::Level PL) {
474276479Sdim  addModuleFlag(ModFlagBehavior::Error, "PIC Level", PL);
475276479Sdim}
476296417Sdim
477296417Sdimvoid Module::setMaximumFunctionCount(uint64_t Count) {
478296417Sdim  addModuleFlag(ModFlagBehavior::Error, "MaxFunctionCount", Count);
479296417Sdim}
480296417Sdim
481296417SdimOptional<uint64_t> Module::getMaximumFunctionCount() {
482296417Sdim  auto *Val =
483296417Sdim      cast_or_null<ConstantAsMetadata>(getModuleFlag("MaxFunctionCount"));
484296417Sdim  if (!Val)
485296417Sdim    return None;
486296417Sdim  return cast<ConstantInt>(Val->getValue())->getZExtValue();
487296417Sdim}
488