77 Context.addModule(
this);
81 assert(&Context == &
Other.Context &&
"Module must be in the same Context");
85 ModuleID = std::move(
Other.ModuleID);
86 SourceFileName = std::move(
Other.SourceFileName);
87 IsNewDbgInfoFormat = std::move(
Other.IsNewDbgInfoFormat);
90 GlobalList.splice(GlobalList.begin(),
Other.GlobalList);
93 FunctionList.splice(FunctionList.begin(),
Other.FunctionList);
96 AliasList.splice(AliasList.begin(),
Other.AliasList);
99 IFuncList.splice(IFuncList.begin(),
Other.IFuncList);
102 NamedMDList.splice(NamedMDList.begin(),
Other.NamedMDList);
103 GlobalScopeAsm = std::move(
Other.GlobalScopeAsm);
104 OwnedMemoryBuffer = std::move(
Other.OwnedMemoryBuffer);
105 Materializer = std::move(
Other.Materializer);
106 TargetTriple = std::move(
Other.TargetTriple);
108 CurrentIntrinsicIds = std::move(
Other.CurrentIntrinsicIds);
109 UniquedIntrinsicNames = std::move(
Other.UniquedIntrinsicNames);
110 ModuleFlags = std::move(
Other.ModuleFlags);
111 Context.addModule(
this);
116 Context.removeModule(
this);
119 FunctionList.clear();
124void Module::removeDebugIntrinsicDeclarations() {
125 auto *DeclareIntrinsicFn =
126 Intrinsic::getOrInsertDeclaration(
this, Intrinsic::dbg_declare);
127 assert((!isMaterialized() || DeclareIntrinsicFn->hasZeroLiveUses()) &&
128 "Debug declare intrinsic should have had uses removed.");
129 DeclareIntrinsicFn->eraseFromParent();
130 auto *ValueIntrinsicFn =
131 Intrinsic::getOrInsertDeclaration(
this, Intrinsic::dbg_value);
132 assert((!isMaterialized() || ValueIntrinsicFn->hasZeroLiveUses()) &&
133 "Debug value intrinsic should have had uses removed.");
134 ValueIntrinsicFn->eraseFromParent();
135 auto *AssignIntrinsicFn =
136 Intrinsic::getOrInsertDeclaration(
this, Intrinsic::dbg_assign);
137 assert((!isMaterialized() || AssignIntrinsicFn->hasZeroLiveUses()) &&
138 "Debug assign intrinsic should have had uses removed.");
139 AssignIntrinsicFn->eraseFromParent();
140 auto *LabelntrinsicFn =
141 Intrinsic::getOrInsertDeclaration(
this, Intrinsic::dbg_label);
142 assert((!isMaterialized() || LabelntrinsicFn->hasZeroLiveUses()) &&
143 "Debug label intrinsic should have had uses removed.");
144 LabelntrinsicFn->eraseFromParent();
147std::unique_ptr<RandomNumberGenerator>
161 Salt += sys::path::filename(getModuleIdentifier());
163 return std::unique_ptr<RandomNumberGenerator>(
171 return cast_or_null<GlobalValue>(getValueSymbolTable().
lookup(
Name));
174unsigned Module::getNumNamedValues()
const {
175 return getValueSymbolTable().size();
181 return Context.getMDKindID(
Name);
188 return Context.getMDKindNames(Result);
192 return Context.getOperandBundleTags(Result);
210 Function *
New = Function::Create(Ty, GlobalVariable::ExternalLinkage,
211 DL.getProgramAddressSpace(),
Name,
this);
212 if (!
New->isIntrinsic())
229 return dyn_cast_or_null<Function>(getNamedValue(
Name));
244 bool AllowLocal)
const {
246 dyn_cast_or_null<GlobalVariable>(getNamedValue(
Name)))
247 if (AllowLocal || !
Result->hasLocalLinkage())
264 GV = CreateGlobalCallback();
265 assert(GV &&
"The CreateGlobalCallback is expected to create a global");
274 return new GlobalVariable(*
this, Ty,
false, GlobalVariable::ExternalLinkage,
287 return dyn_cast_or_null<GlobalAlias>(getNamedValue(
Name));
291 return dyn_cast_or_null<GlobalIFunc>(getNamedValue(
Name));
298 return NamedMDSymTab.lookup(
Name);
308 NMD->setParent(
this);
309 insertNamedMDNode(NMD);
310 if (
Name ==
"llvm.module.flags")
319 NamedMDSymTab.erase(NMD->
getName());
320 if (NMD == ModuleFlags)
321 ModuleFlags =
nullptr;
322 eraseNamedMDNode(NMD);
325bool Module::isValidModFlagBehavior(
Metadata *MD, ModFlagBehavior &MFB) {
326 if (
ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
327 uint64_t Val = Behavior->getLimitedValue();
328 if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
329 MFB =
static_cast<ModFlagBehavior
>(Val);
339 const NamedMDNode *ModFlags = getModuleFlagsMetadata();
340 if (!ModFlags)
return;
344 auto *MFBConstant = mdconst::extract<ConstantInt>(
Flag->getOperand(0));
345 auto MFB =
static_cast<ModFlagBehavior
>(MFBConstant->getLimitedValue());
348 Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
355 const NamedMDNode *ModFlags = getModuleFlagsMetadata();
359 if (Key == cast<MDString>(
Flag->getOperand(1))->getString())
360 return Flag->getOperand(2);
368NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
371 return getOrInsertNamedMetadata(
"llvm.module.flags");
377void Module::addModuleFlag(ModFlagBehavior Behavior,
StringRef Key,
379 Type *Int32Ty = Type::getInt32Ty(Context);
381 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
382 MDString::get(Context, Key), Val};
383 getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
385void Module::addModuleFlag(ModFlagBehavior Behavior,
StringRef Key,
387 addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
389void Module::addModuleFlag(ModFlagBehavior Behavior,
StringRef Key,
391 Type *Int32Ty = Type::getInt32Ty(Context);
392 addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
396 "Invalid number of operands for module flag!");
397 assert(mdconst::hasa<ConstantInt>(
Node->getOperand(0)) &&
398 isa<MDString>(
Node->getOperand(1)) &&
399 "Invalid operand types for module flag!");
400 getOrInsertModuleFlagsMetadata()->addOperand(
Node);
403void Module::setModuleFlag(ModFlagBehavior Behavior,
StringRef Key,
405 NamedMDNode *ModFlags = getOrInsertModuleFlagsMetadata();
408 if (cast<MDString>(
Flag->getOperand(1))->getString() == Key) {
409 Flag->replaceOperandWith(2, Val);
413 addModuleFlag(Behavior, Key, Val);
415void Module::setModuleFlag(ModFlagBehavior Behavior,
StringRef Key,
417 setModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
419void Module::setModuleFlag(ModFlagBehavior Behavior,
StringRef Key,
421 Type *Int32Ty = Type::getInt32Ty(Context);
422 setModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
429DICompileUnit *Module::debug_compile_units_iterator::operator*()
const {
430 return cast<DICompileUnit>(CUs->getOperand(
Idx));
432DICompileUnit *Module::debug_compile_units_iterator::operator->()
const {
433 return cast<DICompileUnit>(CUs->getOperand(
Idx));
436void Module::debug_compile_units_iterator::SkipNoDebugCUs() {
437 while (CUs && (Idx < CUs->getNumOperands()) &&
438 ((*this)->getEmissionKind() == DICompileUnit::NoDebug))
446Module::global_objects()
const {
454Module::global_values()
const {
455 return concat<const GlobalValue>(
functions(),
globals(), aliases(), ifuncs());
463 "Module already has a GVMaterializer. Call materializeAll"
464 " to clear it out before setting another one.");
465 Materializer.reset(GVM);
470 return Error::success();
472 return Materializer->materialize(GV);
475Error Module::materializeAll() {
477 return Error::success();
478 std::unique_ptr<GVMaterializer>
M = std::move(Materializer);
479 return M->materializeModule();
482Error Module::materializeMetadata() {
484 return Error::success();
485 return Materializer->materializeMetadata();
492std::vector<StructType *> Module::getIdentifiedStructTypes()
const {
497 return Materializer->getIdentifiedStructTypes();
499 std::vector<StructType *>
Ret;
501 SrcStructTypes.
run(*
this,
true);
502 Ret.assign(SrcStructTypes.
begin(), SrcStructTypes.
end());
508 auto Encode = [&BaseName](
unsigned Suffix) {
509 return (
Twine(BaseName) +
"." +
Twine(Suffix)).str();
514 auto UinItInserted = UniquedIntrinsicNames.insert({{
Id, Proto}, 0});
515 if (!UinItInserted.second)
516 return Encode(UinItInserted.first->second);
523 auto NiidItInserted = CurrentIntrinsicIds.insert({BaseName, 0});
524 unsigned Count = NiidItInserted.first->second;
530 NewName = Encode(Count);
534 UniquedIntrinsicNames[{
Id, Proto}] = Count;
539 FunctionType *FT = dyn_cast<FunctionType>(
F->getValueType());
540 auto UinItInserted = UniquedIntrinsicNames.insert({{
Id, FT}, Count});
544 UinItInserted.first->second = Count;
551 NiidItInserted.first->second = Count + 1;
563void Module::dropAllReferences() {
565 F.dropAllReferences();
571 GA.dropAllReferences();
574 GIF.dropAllReferences();
577unsigned Module::getNumberRegisterParameters()
const {
579 cast_or_null<ConstantAsMetadata>(getModuleFlag(
"NumRegisterParameters"));
582 return cast<ConstantInt>(Val->getValue())->getZExtValue();
585unsigned Module::getDwarfVersion()
const {
586 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag(
"Dwarf Version"));
589 return cast<ConstantInt>(Val->getValue())->getZExtValue();
592bool Module::isDwarf64()
const {
593 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag(
"DWARF64"));
594 return Val && cast<ConstantInt>(Val->getValue())->isOne();
597unsigned Module::getCodeViewFlag()
const {
598 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag(
"CodeView"));
601 return cast<ConstantInt>(Val->getValue())->getZExtValue();
604unsigned Module::getInstructionCount()
const {
605 unsigned NumInstrs = 0;
607 NumInstrs +=
F.getInstructionCount();
612 auto &
Entry = *ComdatSymTab.insert(std::make_pair(
Name,
Comdat())).first;
614 return &
Entry.second;
618 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag(
"PIC Level"));
621 return PICLevel::NotPIC;
624 cast<ConstantInt>(Val->getValue())->getZExtValue());
630 addModuleFlag(ModFlagBehavior::Min,
"PIC Level", PL);
634 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag(
"PIE Level"));
637 return PIELevel::Default;
640 cast<ConstantInt>(Val->getValue())->getZExtValue());
644 addModuleFlag(ModFlagBehavior::Max,
"PIE Level", PL);
647std::optional<CodeModel::Model> Module::getCodeModel()
const {
648 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag(
"Code Model"));
654 cast<ConstantInt>(Val->getValue())->getZExtValue());
662 addModuleFlag(ModFlagBehavior::Error,
"Code Model", CL);
665std::optional<uint64_t> Module::getLargeDataThreshold()
const {
667 cast_or_null<ConstantAsMetadata>(getModuleFlag(
"Large Data Threshold"));
672 return cast<ConstantInt>(Val->getValue())->getZExtValue();
675void Module::setLargeDataThreshold(
uint64_t Threshold) {
678 addModuleFlag(ModFlagBehavior::Error,
"Large Data Threshold",
679 ConstantInt::get(Type::getInt64Ty(Context), Threshold));
683 if (Kind == ProfileSummary::PSK_CSInstr)
684 setModuleFlag(ModFlagBehavior::Error,
"CSProfileSummary", M);
686 setModuleFlag(ModFlagBehavior::Error,
"ProfileSummary", M);
689Metadata *Module::getProfileSummary(
bool IsCS)
const {
690 return (IsCS ? getModuleFlag(
"CSProfileSummary")
691 : getModuleFlag(
"ProfileSummary"));
694bool Module::getSemanticInterposition()
const {
695 Metadata *MF = getModuleFlag(
"SemanticInterposition");
697 auto *Val = cast_or_null<ConstantAsMetadata>(MF);
701 return cast<ConstantInt>(Val->getValue())->getZExtValue();
704void Module::setSemanticInterposition(
bool SI) {
705 addModuleFlag(ModFlagBehavior::Error,
"SemanticInterposition",
SI);
708void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) {
709 OwnedMemoryBuffer = std::move(MB);
712bool Module::getRtLibUseGOT()
const {
713 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag(
"RtLibUseGOT"));
714 return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0);
717void Module::setRtLibUseGOT() {
718 addModuleFlag(ModFlagBehavior::Max,
"RtLibUseGOT", 1);
721bool Module::getDirectAccessExternalData()
const {
722 auto *Val = cast_or_null<ConstantAsMetadata>(
723 getModuleFlag(
"direct-access-external-data"));
725 return cast<ConstantInt>(Val->getValue())->getZExtValue() > 0;
726 return getPICLevel() == PICLevel::NotPIC;
729void Module::setDirectAccessExternalData(
bool Value) {
730 addModuleFlag(ModFlagBehavior::Max,
"direct-access-external-data",
Value);
734 if (
auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag(
"uwtable")))
735 return UWTableKind(cast<ConstantInt>(Val->getValue())->getZExtValue());
736 return UWTableKind::None;
740 addModuleFlag(ModFlagBehavior::Max,
"uwtable",
uint32_t(Kind));
744 auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag(
"frame-pointer"));
746 Val ? cast<ConstantInt>(Val->getValue())->getZExtValue() : 0);
750 addModuleFlag(ModFlagBehavior::Max,
"frame-pointer",
static_cast<int>(Kind));
753StringRef Module::getStackProtectorGuard()
const {
754 Metadata *MD = getModuleFlag(
"stack-protector-guard");
755 if (
auto *MDS = dyn_cast_or_null<MDString>(MD))
756 return MDS->getString();
760void Module::setStackProtectorGuard(
StringRef Kind) {
761 MDString *
ID = MDString::get(getContext(), Kind);
762 addModuleFlag(ModFlagBehavior::Error,
"stack-protector-guard",
ID);
765StringRef Module::getStackProtectorGuardReg()
const {
766 Metadata *MD = getModuleFlag(
"stack-protector-guard-reg");
767 if (
auto *MDS = dyn_cast_or_null<MDString>(MD))
768 return MDS->getString();
774 addModuleFlag(ModFlagBehavior::Error,
"stack-protector-guard-reg",
ID);
777StringRef Module::getStackProtectorGuardSymbol()
const {
778 Metadata *MD = getModuleFlag(
"stack-protector-guard-symbol");
779 if (
auto *MDS = dyn_cast_or_null<MDString>(MD))
780 return MDS->getString();
784void Module::setStackProtectorGuardSymbol(
StringRef Symbol) {
785 MDString *
ID = MDString::get(getContext(), Symbol);
786 addModuleFlag(ModFlagBehavior::Error,
"stack-protector-guard-symbol",
ID);
789int Module::getStackProtectorGuardOffset()
const {
790 Metadata *MD = getModuleFlag(
"stack-protector-guard-offset");
791 if (
auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
792 return CI->getSExtValue();
796void Module::setStackProtectorGuardOffset(
int Offset) {
797 addModuleFlag(ModFlagBehavior::Error,
"stack-protector-guard-offset",
Offset);
800unsigned Module::getOverrideStackAlignment()
const {
801 Metadata *MD = getModuleFlag(
"override-stack-alignment");
802 if (
auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
803 return CI->getZExtValue();
807unsigned Module::getMaxTLSAlignment()
const {
808 Metadata *MD = getModuleFlag(
"MaxTLSAlign");
809 if (
auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
810 return CI->getZExtValue();
814void Module::setOverrideStackAlignment(
unsigned Align) {
815 addModuleFlag(ModFlagBehavior::Error,
"override-stack-alignment",
Align);
820 Entries.push_back(
V.getMajor());
821 if (
auto Minor =
V.getMinor()) {
822 Entries.push_back(*Minor);
823 if (
auto Subminor =
V.getSubminor())
824 Entries.push_back(*Subminor);
828 M.addModuleFlag(Module::ModFlagBehavior::Warning,
Name,
829 ConstantDataArray::get(
M.getContext(), Entries));
837 auto *CM = dyn_cast_or_null<ConstantAsMetadata>(MD);
840 auto *Arr = dyn_cast_or_null<ConstantDataArray>(CM->getValue());
843 auto getVersionComponent = [&](
unsigned Index) -> std::optional<unsigned> {
844 if (
Index >= Arr->getNumElements())
846 return (
unsigned)Arr->getElementAsInteger(
Index);
848 auto Major = getVersionComponent(0);
852 if (
auto Minor = getVersionComponent(1)) {
854 if (
auto Subminor = getVersionComponent(2)) {
867 const char *
Name = CompilerUsed ?
"llvm.compiler.used" :
"llvm.used";
881 if (
auto *SummaryMD = getProfileSummary(
false)) {
883 ProfileSummary::getFromMD(SummaryMD));
892 double Ratio = (double)BlockCount / NumCounts;
895 ProfileSummary::PSK_Sample);
900StringRef Module::getDarwinTargetVariantTriple()
const {
901 if (
const auto *MD = getModuleFlag(
"darwin.target_variant.triple"))
902 return cast<MDString>(MD)->getString();
906void Module::setDarwinTargetVariantTriple(
StringRef T) {
907 addModuleFlag(ModFlagBehavior::Warning,
"darwin.target_variant.triple",
908 MDString::get(getContext(),
T));
911VersionTuple Module::getDarwinTargetVariantSDKVersion()
const {
912 return getSDKVersionMD(getModuleFlag(
"darwin.target_variant.SDK Version"));
915void Module::setDarwinTargetVariantSDKVersion(
VersionTuple Version) {
This file defines the StringMap class.
Lower uses of LDS variables from non kernel functions
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::optional< std::vector< StOtherPiece > > Other
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
Module.h This file contains the declarations for the Module class.
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
static Constant * getOrInsertGlobal(Module &M, StringRef Name, Type *Ty)
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach)
llvm::cl::opt< bool > UseNewDbgInfoFormat
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static VersionTuple getSDKVersionMD(Metadata *MD)
static void addSDKVersionMD(const VersionTuple &V, Module &M, StringRef Name)
This file defines the SmallString class.
This file defines the SmallVector class.
Defines the llvm::VersionTuple class, which represents a version in the form major[....
ConstantArray - Constant Array Declarations.
This is the shared class of boolean and integer constants.
This is an important base class in LLVM.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Lightweight error class with error context and mandatory checking.
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool hasInitializer() const
Definitions have initializers, declarations don't.
This is an important class for using LLVM in a threaded context.
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
StringRef getName() const
iterator_range< op_iterator > operands()
void setPartialProfileRatio(double R)
Metadata * getMD(LLVMContext &Context, bool AddPartialField=true, bool AddPartialProfileRatioField=true)
Return summary information as metadata.
uint32_t getNumCounts() const
bool isPartialProfile() const
A random number generator.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
TypeFinder - Walk over a module, identifying all of the types that are used by the module.
void run(const Module &M, bool onlyNamed)
The instances of the Type class are immutable: once they are created, they are never changed.
void dropAllReferences()
Drop all references to operands.
This class provides a symbol table of name/value pairs.
LLVM Value Representation.
Represents a version number in the form major[.minor[.subminor[.build]]].
An efficient, type-erasing, non-owning reference to a callable.
A range adaptor for a pair of iterators.
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
This is an optimization pass for GlobalISel generic memory operations.
GlobalVariable * collectUsedGlobalVariables(const Module &M, SmallVectorImpl< GlobalValue * > &Vec, bool CompilerUsed)
Given "llvm.used" or "llvm.compiler.used" as a global name, collect the initializer elements of that ...
Implement std::hash so that hash_code can be used in STL containers.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Description of the encoding of one expression Op.