xref: /llvm-project/llvm/lib/IR/Module.cpp (revision 00821fed09969305b0003d3313c44d1e761a7131)
1 //===- Module.cpp - Implement the Module class ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Module class for the IR library.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/IR/Module.h"
14 #include "SymbolTableListTraitsImpl.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/Comdat.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/DebugInfoMetadata.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/GVMaterializer.h"
28 #include "llvm/IR/GlobalAlias.h"
29 #include "llvm/IR/GlobalIFunc.h"
30 #include "llvm/IR/GlobalValue.h"
31 #include "llvm/IR/GlobalVariable.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/Metadata.h"
34 #include "llvm/IR/ModuleSummaryIndex.h"
35 #include "llvm/IR/SymbolTableListTraits.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/IR/TypeFinder.h"
38 #include "llvm/IR/Value.h"
39 #include "llvm/IR/ValueSymbolTable.h"
40 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/CodeGen.h"
42 #include "llvm/Support/Error.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/Path.h"
45 #include "llvm/Support/RandomNumberGenerator.h"
46 #include "llvm/Support/VersionTuple.h"
47 #include <algorithm>
48 #include <cassert>
49 #include <cstdint>
50 #include <memory>
51 #include <optional>
52 #include <utility>
53 #include <vector>
54 
55 using namespace llvm;
56 
57 //===----------------------------------------------------------------------===//
58 // Methods to implement the globals and functions lists.
59 //
60 
61 // Explicit instantiations of SymbolTableListTraits since some of the methods
62 // are not in the public header file.
63 template class llvm::SymbolTableListTraits<Function>;
64 template class llvm::SymbolTableListTraits<GlobalVariable>;
65 template class llvm::SymbolTableListTraits<GlobalAlias>;
66 template class llvm::SymbolTableListTraits<GlobalIFunc>;
67 
68 //===----------------------------------------------------------------------===//
69 // Primitive Module methods.
70 //
71 
72 Module::Module(StringRef MID, LLVMContext &C)
73     : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>(-1)),
74       ModuleID(std::string(MID)), SourceFileName(std::string(MID)), DL(""),
75       IsNewDbgInfoFormat(false) {
76   Context.addModule(this);
77 }
78 
79 Module::~Module() {
80   Context.removeModule(this);
81   dropAllReferences();
82   GlobalList.clear();
83   FunctionList.clear();
84   AliasList.clear();
85   IFuncList.clear();
86 }
87 
88 void Module::removeDebugIntrinsicDeclarations() {
89   auto *DeclareIntrinsicFn =
90       Intrinsic::getDeclaration(this, Intrinsic::dbg_declare);
91   assert((!isMaterialized() || DeclareIntrinsicFn->hasZeroLiveUses()) &&
92          "Debug declare intrinsic should have had uses removed.");
93   DeclareIntrinsicFn->eraseFromParent();
94   auto *ValueIntrinsicFn =
95       Intrinsic::getDeclaration(this, Intrinsic::dbg_value);
96   assert((!isMaterialized() || ValueIntrinsicFn->hasZeroLiveUses()) &&
97          "Debug value intrinsic should have had uses removed.");
98   ValueIntrinsicFn->eraseFromParent();
99   auto *AssignIntrinsicFn =
100       Intrinsic::getDeclaration(this, Intrinsic::dbg_assign);
101   assert((!isMaterialized() || AssignIntrinsicFn->hasZeroLiveUses()) &&
102          "Debug assign intrinsic should have had uses removed.");
103   AssignIntrinsicFn->eraseFromParent();
104   auto *LabelntrinsicFn = Intrinsic::getDeclaration(this, Intrinsic::dbg_label);
105   assert((!isMaterialized() || LabelntrinsicFn->hasZeroLiveUses()) &&
106          "Debug label intrinsic should have had uses removed.");
107   LabelntrinsicFn->eraseFromParent();
108 }
109 
110 std::unique_ptr<RandomNumberGenerator>
111 Module::createRNG(const StringRef Name) const {
112   SmallString<32> Salt(Name);
113 
114   // This RNG is guaranteed to produce the same random stream only
115   // when the Module ID and thus the input filename is the same. This
116   // might be problematic if the input filename extension changes
117   // (e.g. from .c to .bc or .ll).
118   //
119   // We could store this salt in NamedMetadata, but this would make
120   // the parameter non-const. This would unfortunately make this
121   // interface unusable by any Machine passes, since they only have a
122   // const reference to their IR Module. Alternatively we can always
123   // store salt metadata from the Module constructor.
124   Salt += sys::path::filename(getModuleIdentifier());
125 
126   return std::unique_ptr<RandomNumberGenerator>(
127       new RandomNumberGenerator(Salt));
128 }
129 
130 /// getNamedValue - Return the first global value in the module with
131 /// the specified name, of arbitrary type.  This method returns null
132 /// if a global with the specified name is not found.
133 GlobalValue *Module::getNamedValue(StringRef Name) const {
134   return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
135 }
136 
137 unsigned Module::getNumNamedValues() const {
138   return getValueSymbolTable().size();
139 }
140 
141 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
142 /// This ID is uniqued across modules in the current LLVMContext.
143 unsigned Module::getMDKindID(StringRef Name) const {
144   return Context.getMDKindID(Name);
145 }
146 
147 /// getMDKindNames - Populate client supplied SmallVector with the name for
148 /// custom metadata IDs registered in this LLVMContext.   ID #0 is not used,
149 /// so it is filled in as an empty string.
150 void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
151   return Context.getMDKindNames(Result);
152 }
153 
154 void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const {
155   return Context.getOperandBundleTags(Result);
156 }
157 
158 //===----------------------------------------------------------------------===//
159 // Methods for easy access to the functions in the module.
160 //
161 
162 // getOrInsertFunction - Look up the specified function in the module symbol
163 // table.  If it does not exist, add a prototype for the function and return
164 // it.  This is nice because it allows most passes to get away with not handling
165 // the symbol table directly for this common task.
166 //
167 FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty,
168                                            AttributeList AttributeList) {
169   // See if we have a definition for the specified function already.
170   GlobalValue *F = getNamedValue(Name);
171   if (!F) {
172     // Nope, add it
173     Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage,
174                                      DL.getProgramAddressSpace(), Name, this);
175     if (!New->isIntrinsic())       // Intrinsics get attrs set on construction
176       New->setAttributes(AttributeList);
177     return {Ty, New}; // Return the new prototype.
178   }
179 
180   // Otherwise, we just found the existing function or a prototype.
181   return {Ty, F};
182 }
183 
184 FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) {
185   return getOrInsertFunction(Name, Ty, AttributeList());
186 }
187 
188 // getFunction - Look up the specified function in the module symbol table.
189 // If it does not exist, return null.
190 //
191 Function *Module::getFunction(StringRef Name) const {
192   return dyn_cast_or_null<Function>(getNamedValue(Name));
193 }
194 
195 //===----------------------------------------------------------------------===//
196 // Methods for easy access to the global variables in the module.
197 //
198 
199 /// getGlobalVariable - Look up the specified global variable in the module
200 /// symbol table.  If it does not exist, return null.  The type argument
201 /// should be the underlying type of the global, i.e., it should not have
202 /// the top-level PointerType, which represents the address of the global.
203 /// If AllowLocal is set to true, this function will return types that
204 /// have an local. By default, these types are not returned.
205 ///
206 GlobalVariable *Module::getGlobalVariable(StringRef Name,
207                                           bool AllowLocal) const {
208   if (GlobalVariable *Result =
209       dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
210     if (AllowLocal || !Result->hasLocalLinkage())
211       return Result;
212   return nullptr;
213 }
214 
215 /// getOrInsertGlobal - Look up the specified global in the module symbol table.
216 ///   1. If it does not exist, add a declaration of the global and return it.
217 ///   2. Else, the global exists but has the wrong type: return the function
218 ///      with a constantexpr cast to the right type.
219 ///   3. Finally, if the existing global is the correct declaration, return the
220 ///      existing global.
221 Constant *Module::getOrInsertGlobal(
222     StringRef Name, Type *Ty,
223     function_ref<GlobalVariable *()> CreateGlobalCallback) {
224   // See if we have a definition for the specified global already.
225   GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
226   if (!GV)
227     GV = CreateGlobalCallback();
228   assert(GV && "The CreateGlobalCallback is expected to create a global");
229 
230   // Otherwise, we just found the existing function or a prototype.
231   return GV;
232 }
233 
234 // Overload to construct a global variable using its constructor's defaults.
235 Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
236   return getOrInsertGlobal(Name, Ty, [&] {
237     return new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
238                               nullptr, Name);
239   });
240 }
241 
242 //===----------------------------------------------------------------------===//
243 // Methods for easy access to the global variables in the module.
244 //
245 
246 // getNamedAlias - Look up the specified global in the module symbol table.
247 // If it does not exist, return null.
248 //
249 GlobalAlias *Module::getNamedAlias(StringRef Name) const {
250   return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
251 }
252 
253 GlobalIFunc *Module::getNamedIFunc(StringRef Name) const {
254   return dyn_cast_or_null<GlobalIFunc>(getNamedValue(Name));
255 }
256 
257 /// getNamedMetadata - Return the first NamedMDNode in the module with the
258 /// specified name. This method returns null if a NamedMDNode with the
259 /// specified name is not found.
260 NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
261   SmallString<256> NameData;
262   StringRef NameRef = Name.toStringRef(NameData);
263   return NamedMDSymTab.lookup(NameRef);
264 }
265 
266 /// getOrInsertNamedMetadata - Return the first named MDNode in the module
267 /// with the specified name. This method returns a new NamedMDNode if a
268 /// NamedMDNode with the specified name is not found.
269 NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
270   NamedMDNode *&NMD = NamedMDSymTab[Name];
271   if (!NMD) {
272     NMD = new NamedMDNode(Name);
273     NMD->setParent(this);
274     insertNamedMDNode(NMD);
275   }
276   return NMD;
277 }
278 
279 /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
280 /// delete it.
281 void Module::eraseNamedMetadata(NamedMDNode *NMD) {
282   NamedMDSymTab.erase(NMD->getName());
283   eraseNamedMDNode(NMD);
284 }
285 
286 bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
287   if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
288     uint64_t Val = Behavior->getLimitedValue();
289     if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
290       MFB = static_cast<ModFlagBehavior>(Val);
291       return true;
292     }
293   }
294   return false;
295 }
296 
297 bool Module::isValidModuleFlag(const MDNode &ModFlag, ModFlagBehavior &MFB,
298                                MDString *&Key, Metadata *&Val) {
299   if (ModFlag.getNumOperands() < 3)
300     return false;
301   if (!isValidModFlagBehavior(ModFlag.getOperand(0), MFB))
302     return false;
303   MDString *K = dyn_cast_or_null<MDString>(ModFlag.getOperand(1));
304   if (!K)
305     return false;
306   Key = K;
307   Val = ModFlag.getOperand(2);
308   return true;
309 }
310 
311 /// getModuleFlagsMetadata - Returns the module flags in the provided vector.
312 void Module::
313 getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
314   const NamedMDNode *ModFlags = getModuleFlagsMetadata();
315   if (!ModFlags) return;
316 
317   for (const MDNode *Flag : ModFlags->operands()) {
318     ModFlagBehavior MFB;
319     MDString *Key = nullptr;
320     Metadata *Val = nullptr;
321     if (isValidModuleFlag(*Flag, MFB, Key, Val)) {
322       // Check the operands of the MDNode before accessing the operands.
323       // The verifier will actually catch these failures.
324       Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
325     }
326   }
327 }
328 
329 /// Return the corresponding value if Key appears in module flags, otherwise
330 /// return null.
331 Metadata *Module::getModuleFlag(StringRef Key) const {
332   SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
333   getModuleFlagsMetadata(ModuleFlags);
334   for (const ModuleFlagEntry &MFE : ModuleFlags) {
335     if (Key == MFE.Key->getString())
336       return MFE.Val;
337   }
338   return nullptr;
339 }
340 
341 /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
342 /// represents module-level flags. This method returns null if there are no
343 /// module-level flags.
344 NamedMDNode *Module::getModuleFlagsMetadata() const {
345   return getNamedMetadata("llvm.module.flags");
346 }
347 
348 /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
349 /// represents module-level flags. If module-level flags aren't found, it
350 /// creates the named metadata that contains them.
351 NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
352   return getOrInsertNamedMetadata("llvm.module.flags");
353 }
354 
355 /// addModuleFlag - Add a module-level flag to the module-level flags
356 /// metadata. It will create the module-level flags named metadata if it doesn't
357 /// already exist.
358 void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
359                            Metadata *Val) {
360   Type *Int32Ty = Type::getInt32Ty(Context);
361   Metadata *Ops[3] = {
362       ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
363       MDString::get(Context, Key), Val};
364   getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
365 }
366 void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
367                            Constant *Val) {
368   addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
369 }
370 void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
371                            uint32_t Val) {
372   Type *Int32Ty = Type::getInt32Ty(Context);
373   addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
374 }
375 void Module::addModuleFlag(MDNode *Node) {
376   assert(Node->getNumOperands() == 3 &&
377          "Invalid number of operands for module flag!");
378   assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
379          isa<MDString>(Node->getOperand(1)) &&
380          "Invalid operand types for module flag!");
381   getOrInsertModuleFlagsMetadata()->addOperand(Node);
382 }
383 
384 void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
385                            Metadata *Val) {
386   NamedMDNode *ModFlags = getOrInsertModuleFlagsMetadata();
387   // Replace the flag if it already exists.
388   for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
389     MDNode *Flag = ModFlags->getOperand(I);
390     ModFlagBehavior MFB;
391     MDString *K = nullptr;
392     Metadata *V = nullptr;
393     if (isValidModuleFlag(*Flag, MFB, K, V) && K->getString() == Key) {
394       Flag->replaceOperandWith(2, Val);
395       return;
396     }
397   }
398   addModuleFlag(Behavior, Key, Val);
399 }
400 
401 void Module::setDataLayout(StringRef Desc) {
402   DL.reset(Desc);
403 }
404 
405 void Module::setDataLayout(const DataLayout &Other) { DL = Other; }
406 
407 DICompileUnit *Module::debug_compile_units_iterator::operator*() const {
408   return cast<DICompileUnit>(CUs->getOperand(Idx));
409 }
410 DICompileUnit *Module::debug_compile_units_iterator::operator->() const {
411   return cast<DICompileUnit>(CUs->getOperand(Idx));
412 }
413 
414 void Module::debug_compile_units_iterator::SkipNoDebugCUs() {
415   while (CUs && (Idx < CUs->getNumOperands()) &&
416          ((*this)->getEmissionKind() == DICompileUnit::NoDebug))
417     ++Idx;
418 }
419 
420 iterator_range<Module::global_object_iterator> Module::global_objects() {
421   return concat<GlobalObject>(functions(), globals());
422 }
423 iterator_range<Module::const_global_object_iterator>
424 Module::global_objects() const {
425   return concat<const GlobalObject>(functions(), globals());
426 }
427 
428 iterator_range<Module::global_value_iterator> Module::global_values() {
429   return concat<GlobalValue>(functions(), globals(), aliases(), ifuncs());
430 }
431 iterator_range<Module::const_global_value_iterator>
432 Module::global_values() const {
433   return concat<const GlobalValue>(functions(), globals(), aliases(), ifuncs());
434 }
435 
436 //===----------------------------------------------------------------------===//
437 // Methods to control the materialization of GlobalValues in the Module.
438 //
439 void Module::setMaterializer(GVMaterializer *GVM) {
440   assert(!Materializer &&
441          "Module already has a GVMaterializer.  Call materializeAll"
442          " to clear it out before setting another one.");
443   Materializer.reset(GVM);
444 }
445 
446 Error Module::materialize(GlobalValue *GV) {
447   if (!Materializer)
448     return Error::success();
449 
450   return Materializer->materialize(GV);
451 }
452 
453 Error Module::materializeAll() {
454   if (!Materializer)
455     return Error::success();
456   std::unique_ptr<GVMaterializer> M = std::move(Materializer);
457   return M->materializeModule();
458 }
459 
460 Error Module::materializeMetadata() {
461   if (!Materializer)
462     return Error::success();
463   return Materializer->materializeMetadata();
464 }
465 
466 //===----------------------------------------------------------------------===//
467 // Other module related stuff.
468 //
469 
470 std::vector<StructType *> Module::getIdentifiedStructTypes() const {
471   // If we have a materializer, it is possible that some unread function
472   // uses a type that is currently not visible to a TypeFinder, so ask
473   // the materializer which types it created.
474   if (Materializer)
475     return Materializer->getIdentifiedStructTypes();
476 
477   std::vector<StructType *> Ret;
478   TypeFinder SrcStructTypes;
479   SrcStructTypes.run(*this, true);
480   Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
481   return Ret;
482 }
483 
484 std::string Module::getUniqueIntrinsicName(StringRef BaseName, Intrinsic::ID Id,
485                                            const FunctionType *Proto) {
486   auto Encode = [&BaseName](unsigned Suffix) {
487     return (Twine(BaseName) + "." + Twine(Suffix)).str();
488   };
489 
490   {
491     // fast path - the prototype is already known
492     auto UinItInserted = UniquedIntrinsicNames.insert({{Id, Proto}, 0});
493     if (!UinItInserted.second)
494       return Encode(UinItInserted.first->second);
495   }
496 
497   // Not known yet. A new entry was created with index 0. Check if there already
498   // exists a matching declaration, or select a new entry.
499 
500   // Start looking for names with the current known maximum count (or 0).
501   auto NiidItInserted = CurrentIntrinsicIds.insert({BaseName, 0});
502   unsigned Count = NiidItInserted.first->second;
503 
504   // This might be slow if a whole population of intrinsics already existed, but
505   // we cache the values for later usage.
506   std::string NewName;
507   while (true) {
508     NewName = Encode(Count);
509     GlobalValue *F = getNamedValue(NewName);
510     if (!F) {
511       // Reserve this entry for the new proto
512       UniquedIntrinsicNames[{Id, Proto}] = Count;
513       break;
514     }
515 
516     // A declaration with this name already exists. Remember it.
517     FunctionType *FT = dyn_cast<FunctionType>(F->getValueType());
518     auto UinItInserted = UniquedIntrinsicNames.insert({{Id, FT}, Count});
519     if (FT == Proto) {
520       // It was a declaration for our prototype. This entry was allocated in the
521       // beginning. Update the count to match the existing declaration.
522       UinItInserted.first->second = Count;
523       break;
524     }
525 
526     ++Count;
527   }
528 
529   NiidItInserted.first->second = Count + 1;
530 
531   return NewName;
532 }
533 
534 // dropAllReferences() - This function causes all the subelements to "let go"
535 // of all references that they are maintaining.  This allows one to 'delete' a
536 // whole module at a time, even though there may be circular references... first
537 // all references are dropped, and all use counts go to zero.  Then everything
538 // is deleted for real.  Note that no operations are valid on an object that
539 // has "dropped all references", except operator delete.
540 //
541 void Module::dropAllReferences() {
542   for (Function &F : *this)
543     F.dropAllReferences();
544 
545   for (GlobalVariable &GV : globals())
546     GV.dropAllReferences();
547 
548   for (GlobalAlias &GA : aliases())
549     GA.dropAllReferences();
550 
551   for (GlobalIFunc &GIF : ifuncs())
552     GIF.dropAllReferences();
553 }
554 
555 unsigned Module::getNumberRegisterParameters() const {
556   auto *Val =
557       cast_or_null<ConstantAsMetadata>(getModuleFlag("NumRegisterParameters"));
558   if (!Val)
559     return 0;
560   return cast<ConstantInt>(Val->getValue())->getZExtValue();
561 }
562 
563 unsigned Module::getDwarfVersion() const {
564   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
565   if (!Val)
566     return 0;
567   return cast<ConstantInt>(Val->getValue())->getZExtValue();
568 }
569 
570 bool Module::isDwarf64() const {
571   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("DWARF64"));
572   return Val && cast<ConstantInt>(Val->getValue())->isOne();
573 }
574 
575 unsigned Module::getCodeViewFlag() const {
576   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView"));
577   if (!Val)
578     return 0;
579   return cast<ConstantInt>(Val->getValue())->getZExtValue();
580 }
581 
582 unsigned Module::getInstructionCount() const {
583   unsigned NumInstrs = 0;
584   for (const Function &F : FunctionList)
585     NumInstrs += F.getInstructionCount();
586   return NumInstrs;
587 }
588 
589 Comdat *Module::getOrInsertComdat(StringRef Name) {
590   auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
591   Entry.second.Name = &Entry;
592   return &Entry.second;
593 }
594 
595 PICLevel::Level Module::getPICLevel() const {
596   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
597 
598   if (!Val)
599     return PICLevel::NotPIC;
600 
601   return static_cast<PICLevel::Level>(
602       cast<ConstantInt>(Val->getValue())->getZExtValue());
603 }
604 
605 void Module::setPICLevel(PICLevel::Level PL) {
606   // The merge result of a non-PIC object and a PIC object can only be reliably
607   // used as a non-PIC object, so use the Min merge behavior.
608   addModuleFlag(ModFlagBehavior::Min, "PIC Level", PL);
609 }
610 
611 PIELevel::Level Module::getPIELevel() const {
612   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIE Level"));
613 
614   if (!Val)
615     return PIELevel::Default;
616 
617   return static_cast<PIELevel::Level>(
618       cast<ConstantInt>(Val->getValue())->getZExtValue());
619 }
620 
621 void Module::setPIELevel(PIELevel::Level PL) {
622   addModuleFlag(ModFlagBehavior::Max, "PIE Level", PL);
623 }
624 
625 std::optional<CodeModel::Model> Module::getCodeModel() const {
626   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Code Model"));
627 
628   if (!Val)
629     return std::nullopt;
630 
631   return static_cast<CodeModel::Model>(
632       cast<ConstantInt>(Val->getValue())->getZExtValue());
633 }
634 
635 void Module::setCodeModel(CodeModel::Model CL) {
636   // Linking object files with different code models is undefined behavior
637   // because the compiler would have to generate additional code (to span
638   // longer jumps) if a larger code model is used with a smaller one.
639   // Therefore we will treat attempts to mix code models as an error.
640   addModuleFlag(ModFlagBehavior::Error, "Code Model", CL);
641 }
642 
643 std::optional<uint64_t> Module::getLargeDataThreshold() const {
644   auto *Val =
645       cast_or_null<ConstantAsMetadata>(getModuleFlag("Large Data Threshold"));
646 
647   if (!Val)
648     return std::nullopt;
649 
650   return cast<ConstantInt>(Val->getValue())->getZExtValue();
651 }
652 
653 void Module::setLargeDataThreshold(uint64_t Threshold) {
654   // Since the large data threshold goes along with the code model, the merge
655   // behavior is the same.
656   addModuleFlag(ModFlagBehavior::Error, "Large Data Threshold",
657                 ConstantInt::get(Type::getInt64Ty(Context), Threshold));
658 }
659 
660 void Module::setProfileSummary(Metadata *M, ProfileSummary::Kind Kind) {
661   if (Kind == ProfileSummary::PSK_CSInstr)
662     setModuleFlag(ModFlagBehavior::Error, "CSProfileSummary", M);
663   else
664     setModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M);
665 }
666 
667 Metadata *Module::getProfileSummary(bool IsCS) const {
668   return (IsCS ? getModuleFlag("CSProfileSummary")
669                : getModuleFlag("ProfileSummary"));
670 }
671 
672 bool Module::getSemanticInterposition() const {
673   Metadata *MF = getModuleFlag("SemanticInterposition");
674 
675   auto *Val = cast_or_null<ConstantAsMetadata>(MF);
676   if (!Val)
677     return false;
678 
679   return cast<ConstantInt>(Val->getValue())->getZExtValue();
680 }
681 
682 void Module::setSemanticInterposition(bool SI) {
683   addModuleFlag(ModFlagBehavior::Error, "SemanticInterposition", SI);
684 }
685 
686 void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) {
687   OwnedMemoryBuffer = std::move(MB);
688 }
689 
690 bool Module::getRtLibUseGOT() const {
691   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("RtLibUseGOT"));
692   return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0);
693 }
694 
695 void Module::setRtLibUseGOT() {
696   addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1);
697 }
698 
699 bool Module::getDirectAccessExternalData() const {
700   auto *Val = cast_or_null<ConstantAsMetadata>(
701       getModuleFlag("direct-access-external-data"));
702   if (Val)
703     return cast<ConstantInt>(Val->getValue())->getZExtValue() > 0;
704   return getPICLevel() == PICLevel::NotPIC;
705 }
706 
707 void Module::setDirectAccessExternalData(bool Value) {
708   addModuleFlag(ModFlagBehavior::Max, "direct-access-external-data", Value);
709 }
710 
711 UWTableKind Module::getUwtable() const {
712   if (auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("uwtable")))
713     return UWTableKind(cast<ConstantInt>(Val->getValue())->getZExtValue());
714   return UWTableKind::None;
715 }
716 
717 void Module::setUwtable(UWTableKind Kind) {
718   addModuleFlag(ModFlagBehavior::Max, "uwtable", uint32_t(Kind));
719 }
720 
721 FramePointerKind Module::getFramePointer() const {
722   auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("frame-pointer"));
723   return static_cast<FramePointerKind>(
724       Val ? cast<ConstantInt>(Val->getValue())->getZExtValue() : 0);
725 }
726 
727 void Module::setFramePointer(FramePointerKind Kind) {
728   addModuleFlag(ModFlagBehavior::Max, "frame-pointer", static_cast<int>(Kind));
729 }
730 
731 StringRef Module::getStackProtectorGuard() const {
732   Metadata *MD = getModuleFlag("stack-protector-guard");
733   if (auto *MDS = dyn_cast_or_null<MDString>(MD))
734     return MDS->getString();
735   return {};
736 }
737 
738 void Module::setStackProtectorGuard(StringRef Kind) {
739   MDString *ID = MDString::get(getContext(), Kind);
740   addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard", ID);
741 }
742 
743 StringRef Module::getStackProtectorGuardReg() const {
744   Metadata *MD = getModuleFlag("stack-protector-guard-reg");
745   if (auto *MDS = dyn_cast_or_null<MDString>(MD))
746     return MDS->getString();
747   return {};
748 }
749 
750 void Module::setStackProtectorGuardReg(StringRef Reg) {
751   MDString *ID = MDString::get(getContext(), Reg);
752   addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-reg", ID);
753 }
754 
755 StringRef Module::getStackProtectorGuardSymbol() const {
756   Metadata *MD = getModuleFlag("stack-protector-guard-symbol");
757   if (auto *MDS = dyn_cast_or_null<MDString>(MD))
758     return MDS->getString();
759   return {};
760 }
761 
762 void Module::setStackProtectorGuardSymbol(StringRef Symbol) {
763   MDString *ID = MDString::get(getContext(), Symbol);
764   addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-symbol", ID);
765 }
766 
767 int Module::getStackProtectorGuardOffset() const {
768   Metadata *MD = getModuleFlag("stack-protector-guard-offset");
769   if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
770     return CI->getSExtValue();
771   return INT_MAX;
772 }
773 
774 void Module::setStackProtectorGuardOffset(int Offset) {
775   addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-offset", Offset);
776 }
777 
778 unsigned Module::getOverrideStackAlignment() const {
779   Metadata *MD = getModuleFlag("override-stack-alignment");
780   if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
781     return CI->getZExtValue();
782   return 0;
783 }
784 
785 unsigned Module::getMaxTLSAlignment() const {
786   Metadata *MD = getModuleFlag("MaxTLSAlign");
787   if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
788     return CI->getZExtValue();
789   return 0;
790 }
791 
792 void Module::setOverrideStackAlignment(unsigned Align) {
793   addModuleFlag(ModFlagBehavior::Error, "override-stack-alignment", Align);
794 }
795 
796 static void addSDKVersionMD(const VersionTuple &V, Module &M, StringRef Name) {
797   SmallVector<unsigned, 3> Entries;
798   Entries.push_back(V.getMajor());
799   if (auto Minor = V.getMinor()) {
800     Entries.push_back(*Minor);
801     if (auto Subminor = V.getSubminor())
802       Entries.push_back(*Subminor);
803     // Ignore the 'build' component as it can't be represented in the object
804     // file.
805   }
806   M.addModuleFlag(Module::ModFlagBehavior::Warning, Name,
807                   ConstantDataArray::get(M.getContext(), Entries));
808 }
809 
810 void Module::setSDKVersion(const VersionTuple &V) {
811   addSDKVersionMD(V, *this, "SDK Version");
812 }
813 
814 static VersionTuple getSDKVersionMD(Metadata *MD) {
815   auto *CM = dyn_cast_or_null<ConstantAsMetadata>(MD);
816   if (!CM)
817     return {};
818   auto *Arr = dyn_cast_or_null<ConstantDataArray>(CM->getValue());
819   if (!Arr)
820     return {};
821   auto getVersionComponent = [&](unsigned Index) -> std::optional<unsigned> {
822     if (Index >= Arr->getNumElements())
823       return std::nullopt;
824     return (unsigned)Arr->getElementAsInteger(Index);
825   };
826   auto Major = getVersionComponent(0);
827   if (!Major)
828     return {};
829   VersionTuple Result = VersionTuple(*Major);
830   if (auto Minor = getVersionComponent(1)) {
831     Result = VersionTuple(*Major, *Minor);
832     if (auto Subminor = getVersionComponent(2)) {
833       Result = VersionTuple(*Major, *Minor, *Subminor);
834     }
835   }
836   return Result;
837 }
838 
839 VersionTuple Module::getSDKVersion() const {
840   return getSDKVersionMD(getModuleFlag("SDK Version"));
841 }
842 
843 GlobalVariable *llvm::collectUsedGlobalVariables(
844     const Module &M, SmallVectorImpl<GlobalValue *> &Vec, bool CompilerUsed) {
845   const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
846   GlobalVariable *GV = M.getGlobalVariable(Name);
847   if (!GV || !GV->hasInitializer())
848     return GV;
849 
850   const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
851   for (Value *Op : Init->operands()) {
852     GlobalValue *G = cast<GlobalValue>(Op->stripPointerCasts());
853     Vec.push_back(G);
854   }
855   return GV;
856 }
857 
858 void Module::setPartialSampleProfileRatio(const ModuleSummaryIndex &Index) {
859   if (auto *SummaryMD = getProfileSummary(/*IsCS*/ false)) {
860     std::unique_ptr<ProfileSummary> ProfileSummary(
861         ProfileSummary::getFromMD(SummaryMD));
862     if (ProfileSummary) {
863       if (ProfileSummary->getKind() != ProfileSummary::PSK_Sample ||
864           !ProfileSummary->isPartialProfile())
865         return;
866       uint64_t BlockCount = Index.getBlockCount();
867       uint32_t NumCounts = ProfileSummary->getNumCounts();
868       if (!NumCounts)
869         return;
870       double Ratio = (double)BlockCount / NumCounts;
871       ProfileSummary->setPartialProfileRatio(Ratio);
872       setProfileSummary(ProfileSummary->getMD(getContext()),
873                         ProfileSummary::PSK_Sample);
874     }
875   }
876 }
877 
878 StringRef Module::getDarwinTargetVariantTriple() const {
879   if (const auto *MD = getModuleFlag("darwin.target_variant.triple"))
880     return cast<MDString>(MD)->getString();
881   return "";
882 }
883 
884 void Module::setDarwinTargetVariantTriple(StringRef T) {
885   addModuleFlag(ModFlagBehavior::Override, "darwin.target_variant.triple",
886                 MDString::get(getContext(), T));
887 }
888 
889 VersionTuple Module::getDarwinTargetVariantSDKVersion() const {
890   return getSDKVersionMD(getModuleFlag("darwin.target_variant.SDK Version"));
891 }
892 
893 void Module::setDarwinTargetVariantSDKVersion(VersionTuple Version) {
894   addSDKVersionMD(Version, *this, "darwin.target_variant.SDK Version");
895 }
896