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
55using 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.
63template class llvm::SymbolTableListTraits<Function>;
64template class llvm::SymbolTableListTraits<GlobalVariable>;
65template class llvm::SymbolTableListTraits<GlobalAlias>;
66template class llvm::SymbolTableListTraits<GlobalIFunc>;
67
68//===----------------------------------------------------------------------===//
69// Primitive Module methods.
70//
71
72Module::Module(StringRef MID, LLVMContext &C)
73 : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>(args: -1)),
74 ModuleID(std::string(MID)), SourceFileName(std::string(MID)), DL(""),
75 IsNewDbgInfoFormat(false) {
76 Context.addModule(this);
77}
78
79Module::~Module() {
80 Context.removeModule(this);
81 dropAllReferences();
82 GlobalList.clear();
83 FunctionList.clear();
84 AliasList.clear();
85 IFuncList.clear();
86}
87
88void 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
110std::unique_ptr<RandomNumberGenerator>
111Module::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(path: 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.
133GlobalValue *Module::getNamedValue(StringRef Name) const {
134 return cast_or_null<GlobalValue>(Val: getValueSymbolTable().lookup(Name));
135}
136
137unsigned 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.
143unsigned 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.
150void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
151 return Context.getMDKindNames(Result);
152}
153
154void 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//
167FunctionCallee 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, Linkage: GlobalVariable::ExternalLinkage,
174 AddrSpace: DL.getProgramAddressSpace(), N: Name, M: 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
184FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) {
185 return getOrInsertFunction(Name, Ty, AttributeList: AttributeList());
186}
187
188// getFunction - Look up the specified function in the module symbol table.
189// If it does not exist, return null.
190//
191Function *Module::getFunction(StringRef Name) const {
192 return dyn_cast_or_null<Function>(Val: 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///
206GlobalVariable *Module::getGlobalVariable(StringRef Name,
207 bool AllowLocal) const {
208 if (GlobalVariable *Result =
209 dyn_cast_or_null<GlobalVariable>(Val: 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.
221Constant *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>(Val: 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.
235Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
236 return getOrInsertGlobal(Name, Ty, CreateGlobalCallback: [&] {
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//
249GlobalAlias *Module::getNamedAlias(StringRef Name) const {
250 return dyn_cast_or_null<GlobalAlias>(Val: getNamedValue(Name));
251}
252
253GlobalIFunc *Module::getNamedIFunc(StringRef Name) const {
254 return dyn_cast_or_null<GlobalIFunc>(Val: 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.
260NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
261 SmallString<256> NameData;
262 StringRef NameRef = Name.toStringRef(Out&: NameData);
263 return NamedMDSymTab.lookup(Key: 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.
269NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
270 NamedMDNode *&NMD = NamedMDSymTab[Name];
271 if (!NMD) {
272 NMD = new NamedMDNode(Name);
273 NMD->setParent(this);
274 insertNamedMDNode(MDNode: NMD);
275 }
276 return NMD;
277}
278
279/// eraseNamedMetadata - Remove the given NamedMDNode from this module and
280/// delete it.
281void Module::eraseNamedMetadata(NamedMDNode *NMD) {
282 NamedMDSymTab.erase(Key: NMD->getName());
283 eraseNamedMDNode(MDNode: NMD);
284}
285
286bool 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
297bool Module::isValidModuleFlag(const MDNode &ModFlag, ModFlagBehavior &MFB,
298 MDString *&Key, Metadata *&Val) {
299 if (ModFlag.getNumOperands() < 3)
300 return false;
301 if (!isValidModFlagBehavior(MD: ModFlag.getOperand(I: 0), MFB))
302 return false;
303 MDString *K = dyn_cast_or_null<MDString>(Val: ModFlag.getOperand(I: 1));
304 if (!K)
305 return false;
306 Key = K;
307 Val = ModFlag.getOperand(I: 2);
308 return true;
309}
310
311/// getModuleFlagsMetadata - Returns the module flags in the provided vector.
312void Module::
313getModuleFlagsMetadata(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(ModFlag: *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(Elt: ModuleFlagEntry(MFB, Key, Val));
325 }
326 }
327}
328
329/// Return the corresponding value if Key appears in module flags, otherwise
330/// return null.
331Metadata *Module::getModuleFlag(StringRef Key) const {
332 SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
333 getModuleFlagsMetadata(Flags&: 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.
344NamedMDNode *Module::getModuleFlagsMetadata() const {
345 return getNamedMetadata(Name: "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.
351NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
352 return getOrInsertNamedMetadata(Name: "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.
358void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
359 Metadata *Val) {
360 Type *Int32Ty = Type::getInt32Ty(C&: Context);
361 Metadata *Ops[3] = {
362 ConstantAsMetadata::get(C: ConstantInt::get(Ty: Int32Ty, V: Behavior)),
363 MDString::get(Context, Str: Key), Val};
364 getOrInsertModuleFlagsMetadata()->addOperand(M: MDNode::get(Context, MDs: Ops));
365}
366void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
367 Constant *Val) {
368 addModuleFlag(Behavior, Key, Val: ConstantAsMetadata::get(C: Val));
369}
370void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
371 uint32_t Val) {
372 Type *Int32Ty = Type::getInt32Ty(C&: Context);
373 addModuleFlag(Behavior, Key, Val: ConstantInt::get(Ty: Int32Ty, V: Val));
374}
375void 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(M: Node);
382}
383
384void 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: I);
390 ModFlagBehavior MFB;
391 MDString *K = nullptr;
392 Metadata *V = nullptr;
393 if (isValidModuleFlag(ModFlag: *Flag, MFB, Key&: K, Val&: V) && K->getString() == Key) {
394 Flag->replaceOperandWith(I: 2, New: Val);
395 return;
396 }
397 }
398 addModuleFlag(Behavior, Key, Val);
399}
400
401void Module::setDataLayout(StringRef Desc) {
402 DL.reset(LayoutDescription: Desc);
403}
404
405void Module::setDataLayout(const DataLayout &Other) { DL = Other; }
406
407DICompileUnit *Module::debug_compile_units_iterator::operator*() const {
408 return cast<DICompileUnit>(Val: CUs->getOperand(i: Idx));
409}
410DICompileUnit *Module::debug_compile_units_iterator::operator->() const {
411 return cast<DICompileUnit>(Val: CUs->getOperand(i: Idx));
412}
413
414void Module::debug_compile_units_iterator::SkipNoDebugCUs() {
415 while (CUs && (Idx < CUs->getNumOperands()) &&
416 ((*this)->getEmissionKind() == DICompileUnit::NoDebug))
417 ++Idx;
418}
419
420iterator_range<Module::global_object_iterator> Module::global_objects() {
421 return concat<GlobalObject>(Ranges: functions(), Ranges: globals());
422}
423iterator_range<Module::const_global_object_iterator>
424Module::global_objects() const {
425 return concat<const GlobalObject>(Ranges: functions(), Ranges: globals());
426}
427
428iterator_range<Module::global_value_iterator> Module::global_values() {
429 return concat<GlobalValue>(Ranges: functions(), Ranges: globals(), Ranges: aliases(), Ranges: ifuncs());
430}
431iterator_range<Module::const_global_value_iterator>
432Module::global_values() const {
433 return concat<const GlobalValue>(Ranges: functions(), Ranges: globals(), Ranges: aliases(), Ranges: ifuncs());
434}
435
436//===----------------------------------------------------------------------===//
437// Methods to control the materialization of GlobalValues in the Module.
438//
439void 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(p: GVM);
444}
445
446Error Module::materialize(GlobalValue *GV) {
447 if (!Materializer)
448 return Error::success();
449
450 return Materializer->materialize(GV);
451}
452
453Error Module::materializeAll() {
454 if (!Materializer)
455 return Error::success();
456 std::unique_ptr<GVMaterializer> M = std::move(Materializer);
457 return M->materializeModule();
458}
459
460Error Module::materializeMetadata() {
461 if (!Materializer)
462 return Error::success();
463 return Materializer->materializeMetadata();
464}
465
466//===----------------------------------------------------------------------===//
467// Other module related stuff.
468//
469
470std::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(M: *this, onlyNamed: true);
480 Ret.assign(first: SrcStructTypes.begin(), last: SrcStructTypes.end());
481 return Ret;
482}
483
484std::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(KV: {{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(KV: {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(Name: 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>(Val: F->getValueType());
518 auto UinItInserted = UniquedIntrinsicNames.insert(KV: {{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//
541void 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
555unsigned Module::getNumberRegisterParameters() const {
556 auto *Val =
557 cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "NumRegisterParameters"));
558 if (!Val)
559 return 0;
560 return cast<ConstantInt>(Val: Val->getValue())->getZExtValue();
561}
562
563unsigned Module::getDwarfVersion() const {
564 auto *Val = cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "Dwarf Version"));
565 if (!Val)
566 return 0;
567 return cast<ConstantInt>(Val: Val->getValue())->getZExtValue();
568}
569
570bool Module::isDwarf64() const {
571 auto *Val = cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "DWARF64"));
572 return Val && cast<ConstantInt>(Val: Val->getValue())->isOne();
573}
574
575unsigned Module::getCodeViewFlag() const {
576 auto *Val = cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "CodeView"));
577 if (!Val)
578 return 0;
579 return cast<ConstantInt>(Val: Val->getValue())->getZExtValue();
580}
581
582unsigned Module::getInstructionCount() const {
583 unsigned NumInstrs = 0;
584 for (const Function &F : FunctionList)
585 NumInstrs += F.getInstructionCount();
586 return NumInstrs;
587}
588
589Comdat *Module::getOrInsertComdat(StringRef Name) {
590 auto &Entry = *ComdatSymTab.insert(KV: std::make_pair(x&: Name, y: Comdat())).first;
591 Entry.second.Name = &Entry;
592 return &Entry.second;
593}
594
595PICLevel::Level Module::getPICLevel() const {
596 auto *Val = cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "PIC Level"));
597
598 if (!Val)
599 return PICLevel::NotPIC;
600
601 return static_cast<PICLevel::Level>(
602 cast<ConstantInt>(Val: Val->getValue())->getZExtValue());
603}
604
605void 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(Behavior: ModFlagBehavior::Min, Key: "PIC Level", Val: PL);
609}
610
611PIELevel::Level Module::getPIELevel() const {
612 auto *Val = cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "PIE Level"));
613
614 if (!Val)
615 return PIELevel::Default;
616
617 return static_cast<PIELevel::Level>(
618 cast<ConstantInt>(Val: Val->getValue())->getZExtValue());
619}
620
621void Module::setPIELevel(PIELevel::Level PL) {
622 addModuleFlag(Behavior: ModFlagBehavior::Max, Key: "PIE Level", Val: PL);
623}
624
625std::optional<CodeModel::Model> Module::getCodeModel() const {
626 auto *Val = cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "Code Model"));
627
628 if (!Val)
629 return std::nullopt;
630
631 return static_cast<CodeModel::Model>(
632 cast<ConstantInt>(Val: Val->getValue())->getZExtValue());
633}
634
635void 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(Behavior: ModFlagBehavior::Error, Key: "Code Model", Val: CL);
641}
642
643std::optional<uint64_t> Module::getLargeDataThreshold() const {
644 auto *Val =
645 cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "Large Data Threshold"));
646
647 if (!Val)
648 return std::nullopt;
649
650 return cast<ConstantInt>(Val: Val->getValue())->getZExtValue();
651}
652
653void 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(Behavior: ModFlagBehavior::Error, Key: "Large Data Threshold",
657 Val: ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: Threshold));
658}
659
660void Module::setProfileSummary(Metadata *M, ProfileSummary::Kind Kind) {
661 if (Kind == ProfileSummary::PSK_CSInstr)
662 setModuleFlag(Behavior: ModFlagBehavior::Error, Key: "CSProfileSummary", Val: M);
663 else
664 setModuleFlag(Behavior: ModFlagBehavior::Error, Key: "ProfileSummary", Val: M);
665}
666
667Metadata *Module::getProfileSummary(bool IsCS) const {
668 return (IsCS ? getModuleFlag(Key: "CSProfileSummary")
669 : getModuleFlag(Key: "ProfileSummary"));
670}
671
672bool Module::getSemanticInterposition() const {
673 Metadata *MF = getModuleFlag(Key: "SemanticInterposition");
674
675 auto *Val = cast_or_null<ConstantAsMetadata>(Val: MF);
676 if (!Val)
677 return false;
678
679 return cast<ConstantInt>(Val: Val->getValue())->getZExtValue();
680}
681
682void Module::setSemanticInterposition(bool SI) {
683 addModuleFlag(Behavior: ModFlagBehavior::Error, Key: "SemanticInterposition", Val: SI);
684}
685
686void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) {
687 OwnedMemoryBuffer = std::move(MB);
688}
689
690bool Module::getRtLibUseGOT() const {
691 auto *Val = cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "RtLibUseGOT"));
692 return Val && (cast<ConstantInt>(Val: Val->getValue())->getZExtValue() > 0);
693}
694
695void Module::setRtLibUseGOT() {
696 addModuleFlag(Behavior: ModFlagBehavior::Max, Key: "RtLibUseGOT", Val: 1);
697}
698
699bool Module::getDirectAccessExternalData() const {
700 auto *Val = cast_or_null<ConstantAsMetadata>(
701 Val: getModuleFlag(Key: "direct-access-external-data"));
702 if (Val)
703 return cast<ConstantInt>(Val: Val->getValue())->getZExtValue() > 0;
704 return getPICLevel() == PICLevel::NotPIC;
705}
706
707void Module::setDirectAccessExternalData(bool Value) {
708 addModuleFlag(Behavior: ModFlagBehavior::Max, Key: "direct-access-external-data", Val: Value);
709}
710
711UWTableKind Module::getUwtable() const {
712 if (auto *Val = cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "uwtable")))
713 return UWTableKind(cast<ConstantInt>(Val: Val->getValue())->getZExtValue());
714 return UWTableKind::None;
715}
716
717void Module::setUwtable(UWTableKind Kind) {
718 addModuleFlag(Behavior: ModFlagBehavior::Max, Key: "uwtable", Val: uint32_t(Kind));
719}
720
721FramePointerKind Module::getFramePointer() const {
722 auto *Val = cast_or_null<ConstantAsMetadata>(Val: getModuleFlag(Key: "frame-pointer"));
723 return static_cast<FramePointerKind>(
724 Val ? cast<ConstantInt>(Val: Val->getValue())->getZExtValue() : 0);
725}
726
727void Module::setFramePointer(FramePointerKind Kind) {
728 addModuleFlag(Behavior: ModFlagBehavior::Max, Key: "frame-pointer", Val: static_cast<int>(Kind));
729}
730
731StringRef Module::getStackProtectorGuard() const {
732 Metadata *MD = getModuleFlag(Key: "stack-protector-guard");
733 if (auto *MDS = dyn_cast_or_null<MDString>(Val: MD))
734 return MDS->getString();
735 return {};
736}
737
738void Module::setStackProtectorGuard(StringRef Kind) {
739 MDString *ID = MDString::get(Context&: getContext(), Str: Kind);
740 addModuleFlag(Behavior: ModFlagBehavior::Error, Key: "stack-protector-guard", Val: ID);
741}
742
743StringRef Module::getStackProtectorGuardReg() const {
744 Metadata *MD = getModuleFlag(Key: "stack-protector-guard-reg");
745 if (auto *MDS = dyn_cast_or_null<MDString>(Val: MD))
746 return MDS->getString();
747 return {};
748}
749
750void Module::setStackProtectorGuardReg(StringRef Reg) {
751 MDString *ID = MDString::get(Context&: getContext(), Str: Reg);
752 addModuleFlag(Behavior: ModFlagBehavior::Error, Key: "stack-protector-guard-reg", Val: ID);
753}
754
755StringRef Module::getStackProtectorGuardSymbol() const {
756 Metadata *MD = getModuleFlag(Key: "stack-protector-guard-symbol");
757 if (auto *MDS = dyn_cast_or_null<MDString>(Val: MD))
758 return MDS->getString();
759 return {};
760}
761
762void Module::setStackProtectorGuardSymbol(StringRef Symbol) {
763 MDString *ID = MDString::get(Context&: getContext(), Str: Symbol);
764 addModuleFlag(Behavior: ModFlagBehavior::Error, Key: "stack-protector-guard-symbol", Val: ID);
765}
766
767int Module::getStackProtectorGuardOffset() const {
768 Metadata *MD = getModuleFlag(Key: "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
774void Module::setStackProtectorGuardOffset(int Offset) {
775 addModuleFlag(Behavior: ModFlagBehavior::Error, Key: "stack-protector-guard-offset", Val: Offset);
776}
777
778unsigned Module::getOverrideStackAlignment() const {
779 Metadata *MD = getModuleFlag(Key: "override-stack-alignment");
780 if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
781 return CI->getZExtValue();
782 return 0;
783}
784
785unsigned Module::getMaxTLSAlignment() const {
786 Metadata *MD = getModuleFlag(Key: "MaxTLSAlign");
787 if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
788 return CI->getZExtValue();
789 return 0;
790}
791
792void Module::setOverrideStackAlignment(unsigned Align) {
793 addModuleFlag(Behavior: ModFlagBehavior::Error, Key: "override-stack-alignment", Val: Align);
794}
795
796static void addSDKVersionMD(const VersionTuple &V, Module &M, StringRef Name) {
797 SmallVector<unsigned, 3> Entries;
798 Entries.push_back(Elt: V.getMajor());
799 if (auto Minor = V.getMinor()) {
800 Entries.push_back(Elt: *Minor);
801 if (auto Subminor = V.getSubminor())
802 Entries.push_back(Elt: *Subminor);
803 // Ignore the 'build' component as it can't be represented in the object
804 // file.
805 }
806 M.addModuleFlag(Behavior: Module::ModFlagBehavior::Warning, Key: Name,
807 Val: ConstantDataArray::get(Context&: M.getContext(), Elts&: Entries));
808}
809
810void Module::setSDKVersion(const VersionTuple &V) {
811 addSDKVersionMD(V, M&: *this, Name: "SDK Version");
812}
813
814static VersionTuple getSDKVersionMD(Metadata *MD) {
815 auto *CM = dyn_cast_or_null<ConstantAsMetadata>(Val: MD);
816 if (!CM)
817 return {};
818 auto *Arr = dyn_cast_or_null<ConstantDataArray>(Val: 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(i: 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
839VersionTuple Module::getSDKVersion() const {
840 return getSDKVersionMD(MD: getModuleFlag(Key: "SDK Version"));
841}
842
843GlobalVariable *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>(Val: GV->getInitializer());
851 for (Value *Op : Init->operands()) {
852 GlobalValue *G = cast<GlobalValue>(Val: Op->stripPointerCasts());
853 Vec.push_back(Elt: G);
854 }
855 return GV;
856}
857
858void Module::setPartialSampleProfileRatio(const ModuleSummaryIndex &Index) {
859 if (auto *SummaryMD = getProfileSummary(/*IsCS*/ false)) {
860 std::unique_ptr<ProfileSummary> ProfileSummary(
861 ProfileSummary::getFromMD(MD: 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(M: ProfileSummary->getMD(Context&: getContext()),
873 Kind: ProfileSummary::PSK_Sample);
874 }
875 }
876}
877
878StringRef Module::getDarwinTargetVariantTriple() const {
879 if (const auto *MD = getModuleFlag(Key: "darwin.target_variant.triple"))
880 return cast<MDString>(Val: MD)->getString();
881 return "";
882}
883
884void Module::setDarwinTargetVariantTriple(StringRef T) {
885 addModuleFlag(Behavior: ModFlagBehavior::Override, Key: "darwin.target_variant.triple",
886 Val: MDString::get(Context&: getContext(), Str: T));
887}
888
889VersionTuple Module::getDarwinTargetVariantSDKVersion() const {
890 return getSDKVersionMD(MD: getModuleFlag(Key: "darwin.target_variant.SDK Version"));
891}
892
893void Module::setDarwinTargetVariantSDKVersion(VersionTuple Version) {
894 addSDKVersionMD(V: Version, M&: *this, Name: "darwin.target_variant.SDK Version");
895}
896

source code of llvm/lib/IR/Module.cpp