16b3f41edSDimitry Andric //===- Module.cpp - Implement the Module class ----------------------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
94a16efa3SDimitry Andric // This file implements the Module class for the IR library.
10009b1c42SEd Schouten //
11009b1c42SEd Schouten //===----------------------------------------------------------------------===//
12009b1c42SEd Schouten
137ab83427SDimitry Andric #include "llvm/IR/Module.h"
14009b1c42SEd Schouten #include "SymbolTableListTraitsImpl.h"
154a16efa3SDimitry Andric #include "llvm/ADT/SmallString.h"
166b3f41edSDimitry Andric #include "llvm/ADT/SmallVector.h"
176b3f41edSDimitry Andric #include "llvm/ADT/StringMap.h"
186b3f41edSDimitry Andric #include "llvm/ADT/StringRef.h"
196b3f41edSDimitry Andric #include "llvm/ADT/Twine.h"
206b3f41edSDimitry Andric #include "llvm/IR/Attributes.h"
216b3f41edSDimitry Andric #include "llvm/IR/Comdat.h"
224a16efa3SDimitry Andric #include "llvm/IR/Constants.h"
236b3f41edSDimitry Andric #include "llvm/IR/DataLayout.h"
2401095a5dSDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
257ab83427SDimitry Andric #include "llvm/IR/DerivedTypes.h"
266b3f41edSDimitry Andric #include "llvm/IR/Function.h"
277ab83427SDimitry Andric #include "llvm/IR/GVMaterializer.h"
286b3f41edSDimitry Andric #include "llvm/IR/GlobalAlias.h"
296b3f41edSDimitry Andric #include "llvm/IR/GlobalIFunc.h"
306b3f41edSDimitry Andric #include "llvm/IR/GlobalValue.h"
316b3f41edSDimitry Andric #include "llvm/IR/GlobalVariable.h"
324a16efa3SDimitry Andric #include "llvm/IR/LLVMContext.h"
336b3f41edSDimitry Andric #include "llvm/IR/Metadata.h"
34cfca06d7SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h"
356b3f41edSDimitry Andric #include "llvm/IR/SymbolTableListTraits.h"
366b3f41edSDimitry Andric #include "llvm/IR/Type.h"
3767c32a98SDimitry Andric #include "llvm/IR/TypeFinder.h"
386b3f41edSDimitry Andric #include "llvm/IR/Value.h"
396b3f41edSDimitry Andric #include "llvm/IR/ValueSymbolTable.h"
406b3f41edSDimitry Andric #include "llvm/Support/Casting.h"
416b3f41edSDimitry Andric #include "llvm/Support/CodeGen.h"
42b915e9e0SDimitry Andric #include "llvm/Support/Error.h"
43b915e9e0SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
445ca98fd9SDimitry Andric #include "llvm/Support/Path.h"
455ca98fd9SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h"
46d8e91e46SDimitry Andric #include "llvm/Support/VersionTuple.h"
47009b1c42SEd Schouten #include <algorithm>
486b3f41edSDimitry Andric #include <cassert>
496b3f41edSDimitry Andric #include <cstdint>
506b3f41edSDimitry Andric #include <memory>
51e3b55780SDimitry Andric #include <optional>
526b3f41edSDimitry Andric #include <utility>
536b3f41edSDimitry Andric #include <vector>
54dd58ef01SDimitry Andric
55009b1c42SEd Schouten using namespace llvm;
56009b1c42SEd Schouten
57ac9a064cSDimitry Andric extern cl::opt<bool> UseNewDbgInfoFormat;
58ac9a064cSDimitry Andric
59009b1c42SEd Schouten //===----------------------------------------------------------------------===//
60009b1c42SEd Schouten // Methods to implement the globals and functions lists.
61009b1c42SEd Schouten //
62009b1c42SEd Schouten
63009b1c42SEd Schouten // Explicit instantiations of SymbolTableListTraits since some of the methods
64009b1c42SEd Schouten // are not in the public header file.
65dd58ef01SDimitry Andric template class llvm::SymbolTableListTraits<Function>;
66dd58ef01SDimitry Andric template class llvm::SymbolTableListTraits<GlobalVariable>;
67dd58ef01SDimitry Andric template class llvm::SymbolTableListTraits<GlobalAlias>;
6801095a5dSDimitry Andric template class llvm::SymbolTableListTraits<GlobalIFunc>;
69009b1c42SEd Schouten
70009b1c42SEd Schouten //===----------------------------------------------------------------------===//
71009b1c42SEd Schouten // Primitive Module methods.
72009b1c42SEd Schouten //
73009b1c42SEd Schouten
Module(StringRef MID,LLVMContext & C)74907da171SRoman Divacky Module::Module(StringRef MID, LLVMContext &C)
75344a3780SDimitry Andric : Context(C), ValSymTab(std::make_unique<ValueSymbolTable>(-1)),
76b1c73532SDimitry Andric ModuleID(std::string(MID)), SourceFileName(std::string(MID)), DL(""),
77ac9a064cSDimitry Andric IsNewDbgInfoFormat(UseNewDbgInfoFormat) {
78cf099d11SDimitry Andric Context.addModule(this);
79009b1c42SEd Schouten }
80009b1c42SEd Schouten
~Module()81009b1c42SEd Schouten Module::~Module() {
82cf099d11SDimitry Andric Context.removeModule(this);
83009b1c42SEd Schouten dropAllReferences();
84009b1c42SEd Schouten GlobalList.clear();
85009b1c42SEd Schouten FunctionList.clear();
86009b1c42SEd Schouten AliasList.clear();
8701095a5dSDimitry Andric IFuncList.clear();
88009b1c42SEd Schouten }
89009b1c42SEd Schouten
removeDebugIntrinsicDeclarations()90ac9a064cSDimitry Andric void Module::removeDebugIntrinsicDeclarations() {
91ac9a064cSDimitry Andric auto *DeclareIntrinsicFn =
92ac9a064cSDimitry Andric Intrinsic::getDeclaration(this, Intrinsic::dbg_declare);
93ac9a064cSDimitry Andric assert((!isMaterialized() || DeclareIntrinsicFn->hasZeroLiveUses()) &&
94ac9a064cSDimitry Andric "Debug declare intrinsic should have had uses removed.");
95ac9a064cSDimitry Andric DeclareIntrinsicFn->eraseFromParent();
96ac9a064cSDimitry Andric auto *ValueIntrinsicFn =
97ac9a064cSDimitry Andric Intrinsic::getDeclaration(this, Intrinsic::dbg_value);
98ac9a064cSDimitry Andric assert((!isMaterialized() || ValueIntrinsicFn->hasZeroLiveUses()) &&
99ac9a064cSDimitry Andric "Debug value intrinsic should have had uses removed.");
100ac9a064cSDimitry Andric ValueIntrinsicFn->eraseFromParent();
101ac9a064cSDimitry Andric auto *AssignIntrinsicFn =
102ac9a064cSDimitry Andric Intrinsic::getDeclaration(this, Intrinsic::dbg_assign);
103ac9a064cSDimitry Andric assert((!isMaterialized() || AssignIntrinsicFn->hasZeroLiveUses()) &&
104ac9a064cSDimitry Andric "Debug assign intrinsic should have had uses removed.");
105ac9a064cSDimitry Andric AssignIntrinsicFn->eraseFromParent();
106ac9a064cSDimitry Andric auto *LabelntrinsicFn = Intrinsic::getDeclaration(this, Intrinsic::dbg_label);
107ac9a064cSDimitry Andric assert((!isMaterialized() || LabelntrinsicFn->hasZeroLiveUses()) &&
108ac9a064cSDimitry Andric "Debug label intrinsic should have had uses removed.");
109ac9a064cSDimitry Andric LabelntrinsicFn->eraseFromParent();
110ac9a064cSDimitry Andric }
111ac9a064cSDimitry Andric
112cfca06d7SDimitry Andric std::unique_ptr<RandomNumberGenerator>
createRNG(const StringRef Name) const113cfca06d7SDimitry Andric Module::createRNG(const StringRef Name) const {
114cfca06d7SDimitry Andric SmallString<32> Salt(Name);
11567c32a98SDimitry Andric
11667c32a98SDimitry Andric // This RNG is guaranteed to produce the same random stream only
11767c32a98SDimitry Andric // when the Module ID and thus the input filename is the same. This
11867c32a98SDimitry Andric // might be problematic if the input filename extension changes
11967c32a98SDimitry Andric // (e.g. from .c to .bc or .ll).
12067c32a98SDimitry Andric //
12167c32a98SDimitry Andric // We could store this salt in NamedMetadata, but this would make
12267c32a98SDimitry Andric // the parameter non-const. This would unfortunately make this
12367c32a98SDimitry Andric // interface unusable by any Machine passes, since they only have a
12467c32a98SDimitry Andric // const reference to their IR Module. Alternatively we can always
12567c32a98SDimitry Andric // store salt metadata from the Module constructor.
12667c32a98SDimitry Andric Salt += sys::path::filename(getModuleIdentifier());
12767c32a98SDimitry Andric
128cfca06d7SDimitry Andric return std::unique_ptr<RandomNumberGenerator>(
129cfca06d7SDimitry Andric new RandomNumberGenerator(Salt));
13067c32a98SDimitry Andric }
13167c32a98SDimitry Andric
132009b1c42SEd Schouten /// getNamedValue - Return the first global value in the module with
133009b1c42SEd Schouten /// the specified name, of arbitrary type. This method returns null
134009b1c42SEd Schouten /// if a global with the specified name is not found.
getNamedValue(StringRef Name) const135907da171SRoman Divacky GlobalValue *Module::getNamedValue(StringRef Name) const {
136009b1c42SEd Schouten return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
137009b1c42SEd Schouten }
138009b1c42SEd Schouten
getNumNamedValues() const139c0981da4SDimitry Andric unsigned Module::getNumNamedValues() const {
140c0981da4SDimitry Andric return getValueSymbolTable().size();
141c0981da4SDimitry Andric }
142c0981da4SDimitry Andric
1431e7804dbSRoman Divacky /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
1441e7804dbSRoman Divacky /// This ID is uniqued across modules in the current LLVMContext.
getMDKindID(StringRef Name) const1451e7804dbSRoman Divacky unsigned Module::getMDKindID(StringRef Name) const {
1461e7804dbSRoman Divacky return Context.getMDKindID(Name);
1471e7804dbSRoman Divacky }
1481e7804dbSRoman Divacky
1491e7804dbSRoman Divacky /// getMDKindNames - Populate client supplied SmallVector with the name for
1501e7804dbSRoman Divacky /// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
1511e7804dbSRoman Divacky /// so it is filled in as an empty string.
getMDKindNames(SmallVectorImpl<StringRef> & Result) const1521e7804dbSRoman Divacky void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
1531e7804dbSRoman Divacky return Context.getMDKindNames(Result);
1541e7804dbSRoman Divacky }
1551e7804dbSRoman Divacky
getOperandBundleTags(SmallVectorImpl<StringRef> & Result) const156dd58ef01SDimitry Andric void Module::getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const {
157dd58ef01SDimitry Andric return Context.getOperandBundleTags(Result);
158dd58ef01SDimitry Andric }
1591e7804dbSRoman Divacky
160009b1c42SEd Schouten //===----------------------------------------------------------------------===//
161009b1c42SEd Schouten // Methods for easy access to the functions in the module.
162009b1c42SEd Schouten //
163009b1c42SEd Schouten
164009b1c42SEd Schouten // getOrInsertFunction - Look up the specified function in the module symbol
165009b1c42SEd Schouten // table. If it does not exist, add a prototype for the function and return
166009b1c42SEd Schouten // it. This is nice because it allows most passes to get away with not handling
167009b1c42SEd Schouten // the symbol table directly for this common task.
168009b1c42SEd Schouten //
getOrInsertFunction(StringRef Name,FunctionType * Ty,AttributeList AttributeList)169e6d15924SDimitry Andric FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty,
17071d5a254SDimitry Andric AttributeList AttributeList) {
171009b1c42SEd Schouten // See if we have a definition for the specified function already.
172009b1c42SEd Schouten GlobalValue *F = getNamedValue(Name);
1735ca98fd9SDimitry Andric if (!F) {
174009b1c42SEd Schouten // Nope, add it
175d8e91e46SDimitry Andric Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage,
176ac9a064cSDimitry Andric DL.getProgramAddressSpace(), Name, this);
177009b1c42SEd Schouten if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
178009b1c42SEd Schouten New->setAttributes(AttributeList);
179e6d15924SDimitry Andric return {Ty, New}; // Return the new prototype.
180009b1c42SEd Schouten }
181009b1c42SEd Schouten
182009b1c42SEd Schouten // Otherwise, we just found the existing function or a prototype.
183e6d15924SDimitry Andric return {Ty, F};
184009b1c42SEd Schouten }
185009b1c42SEd Schouten
getOrInsertFunction(StringRef Name,FunctionType * Ty)186e6d15924SDimitry Andric FunctionCallee Module::getOrInsertFunction(StringRef Name, FunctionType *Ty) {
18771d5a254SDimitry Andric return getOrInsertFunction(Name, Ty, AttributeList());
188009b1c42SEd Schouten }
189009b1c42SEd Schouten
190009b1c42SEd Schouten // getFunction - Look up the specified function in the module symbol table.
191009b1c42SEd Schouten // If it does not exist, return null.
192009b1c42SEd Schouten //
getFunction(StringRef Name) const193907da171SRoman Divacky Function *Module::getFunction(StringRef Name) const {
194009b1c42SEd Schouten return dyn_cast_or_null<Function>(getNamedValue(Name));
195009b1c42SEd Schouten }
196009b1c42SEd Schouten
197009b1c42SEd Schouten //===----------------------------------------------------------------------===//
198009b1c42SEd Schouten // Methods for easy access to the global variables in the module.
199009b1c42SEd Schouten //
200009b1c42SEd Schouten
201009b1c42SEd Schouten /// getGlobalVariable - Look up the specified global variable in the module
202009b1c42SEd Schouten /// symbol table. If it does not exist, return null. The type argument
203009b1c42SEd Schouten /// should be the underlying type of the global, i.e., it should not have
204009b1c42SEd Schouten /// the top-level PointerType, which represents the address of the global.
205009b1c42SEd Schouten /// If AllowLocal is set to true, this function will return types that
206009b1c42SEd Schouten /// have an local. By default, these types are not returned.
207009b1c42SEd Schouten ///
getGlobalVariable(StringRef Name,bool AllowLocal) const20871d5a254SDimitry Andric GlobalVariable *Module::getGlobalVariable(StringRef Name,
20971d5a254SDimitry Andric bool AllowLocal) const {
210009b1c42SEd Schouten if (GlobalVariable *Result =
211009b1c42SEd Schouten dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
212009b1c42SEd Schouten if (AllowLocal || !Result->hasLocalLinkage())
213009b1c42SEd Schouten return Result;
2145ca98fd9SDimitry Andric return nullptr;
215009b1c42SEd Schouten }
216009b1c42SEd Schouten
217009b1c42SEd Schouten /// getOrInsertGlobal - Look up the specified global in the module symbol table.
218009b1c42SEd Schouten /// 1. If it does not exist, add a declaration of the global and return it.
219009b1c42SEd Schouten /// 2. Else, the global exists but has the wrong type: return the function
220009b1c42SEd Schouten /// with a constantexpr cast to the right type.
221f8af5cf6SDimitry Andric /// 3. Finally, if the existing global is the correct declaration, return the
222009b1c42SEd Schouten /// existing global.
getOrInsertGlobal(StringRef Name,Type * Ty,function_ref<GlobalVariable * ()> CreateGlobalCallback)223d8e91e46SDimitry Andric Constant *Module::getOrInsertGlobal(
224d8e91e46SDimitry Andric StringRef Name, Type *Ty,
225d8e91e46SDimitry Andric function_ref<GlobalVariable *()> CreateGlobalCallback) {
226009b1c42SEd Schouten // See if we have a definition for the specified global already.
227009b1c42SEd Schouten GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
228d8e91e46SDimitry Andric if (!GV)
229d8e91e46SDimitry Andric GV = CreateGlobalCallback();
230d8e91e46SDimitry Andric assert(GV && "The CreateGlobalCallback is expected to create a global");
231009b1c42SEd Schouten
232009b1c42SEd Schouten // Otherwise, we just found the existing function or a prototype.
233009b1c42SEd Schouten return GV;
234009b1c42SEd Schouten }
235009b1c42SEd Schouten
236d8e91e46SDimitry Andric // Overload to construct a global variable using its constructor's defaults.
getOrInsertGlobal(StringRef Name,Type * Ty)237d8e91e46SDimitry Andric Constant *Module::getOrInsertGlobal(StringRef Name, Type *Ty) {
238d8e91e46SDimitry Andric return getOrInsertGlobal(Name, Ty, [&] {
239d8e91e46SDimitry Andric return new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
240d8e91e46SDimitry Andric nullptr, Name);
241d8e91e46SDimitry Andric });
242d8e91e46SDimitry Andric }
243d8e91e46SDimitry Andric
244009b1c42SEd Schouten //===----------------------------------------------------------------------===//
245009b1c42SEd Schouten // Methods for easy access to the global variables in the module.
246009b1c42SEd Schouten //
247009b1c42SEd Schouten
248009b1c42SEd Schouten // getNamedAlias - Look up the specified global in the module symbol table.
249009b1c42SEd Schouten // If it does not exist, return null.
250009b1c42SEd Schouten //
getNamedAlias(StringRef Name) const251907da171SRoman Divacky GlobalAlias *Module::getNamedAlias(StringRef Name) const {
252009b1c42SEd Schouten return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
253009b1c42SEd Schouten }
254009b1c42SEd Schouten
getNamedIFunc(StringRef Name) const25501095a5dSDimitry Andric GlobalIFunc *Module::getNamedIFunc(StringRef Name) const {
25601095a5dSDimitry Andric return dyn_cast_or_null<GlobalIFunc>(getNamedValue(Name));
25701095a5dSDimitry Andric }
25801095a5dSDimitry Andric
25959850d08SRoman Divacky /// getNamedMetadata - Return the first NamedMDNode in the module with the
26059850d08SRoman Divacky /// specified name. This method returns null if a NamedMDNode with the
26166e41e3cSRoman Divacky /// specified name is not found.
getNamedMetadata(const Twine & Name) const26266e41e3cSRoman Divacky NamedMDNode *Module::getNamedMetadata(const Twine &Name) const {
26366e41e3cSRoman Divacky SmallString<256> NameData;
26466e41e3cSRoman Divacky StringRef NameRef = Name.toStringRef(NameData);
265cfca06d7SDimitry Andric return NamedMDSymTab.lookup(NameRef);
26659850d08SRoman Divacky }
26759850d08SRoman Divacky
26859850d08SRoman Divacky /// getOrInsertNamedMetadata - Return the first named MDNode in the module
26959850d08SRoman Divacky /// with the specified name. This method returns a new NamedMDNode if a
27059850d08SRoman Divacky /// NamedMDNode with the specified name is not found.
getOrInsertNamedMetadata(StringRef Name)271907da171SRoman Divacky NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
272cfca06d7SDimitry Andric NamedMDNode *&NMD = NamedMDSymTab[Name];
273d39c594dSDimitry Andric if (!NMD) {
274d39c594dSDimitry Andric NMD = new NamedMDNode(Name);
275d39c594dSDimitry Andric NMD->setParent(this);
2767fa27ce4SDimitry Andric insertNamedMDNode(NMD);
277d39c594dSDimitry Andric }
27859850d08SRoman Divacky return NMD;
27959850d08SRoman Divacky }
28059850d08SRoman Divacky
28163faed5bSDimitry Andric /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
28263faed5bSDimitry Andric /// delete it.
eraseNamedMetadata(NamedMDNode * NMD)283d39c594dSDimitry Andric void Module::eraseNamedMetadata(NamedMDNode *NMD) {
284cfca06d7SDimitry Andric NamedMDSymTab.erase(NMD->getName());
2857fa27ce4SDimitry Andric eraseNamedMDNode(NMD);
286d39c594dSDimitry Andric }
287d39c594dSDimitry Andric
isValidModFlagBehavior(Metadata * MD,ModFlagBehavior & MFB)28867c32a98SDimitry Andric bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
2895a5ac124SDimitry Andric if (ConstantInt *Behavior = mdconst::dyn_extract_or_null<ConstantInt>(MD)) {
29067c32a98SDimitry Andric uint64_t Val = Behavior->getLimitedValue();
29167c32a98SDimitry Andric if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
29267c32a98SDimitry Andric MFB = static_cast<ModFlagBehavior>(Val);
29367c32a98SDimitry Andric return true;
29467c32a98SDimitry Andric }
29567c32a98SDimitry Andric }
29667c32a98SDimitry Andric return false;
29767c32a98SDimitry Andric }
29867c32a98SDimitry Andric
isValidModuleFlag(const MDNode & ModFlag,ModFlagBehavior & MFB,MDString * & Key,Metadata * & Val)299cfca06d7SDimitry Andric bool Module::isValidModuleFlag(const MDNode &ModFlag, ModFlagBehavior &MFB,
300cfca06d7SDimitry Andric MDString *&Key, Metadata *&Val) {
301cfca06d7SDimitry Andric if (ModFlag.getNumOperands() < 3)
302cfca06d7SDimitry Andric return false;
303cfca06d7SDimitry Andric if (!isValidModFlagBehavior(ModFlag.getOperand(0), MFB))
304cfca06d7SDimitry Andric return false;
305cfca06d7SDimitry Andric MDString *K = dyn_cast_or_null<MDString>(ModFlag.getOperand(1));
306cfca06d7SDimitry Andric if (!K)
307cfca06d7SDimitry Andric return false;
308cfca06d7SDimitry Andric Key = K;
309cfca06d7SDimitry Andric Val = ModFlag.getOperand(2);
310cfca06d7SDimitry Andric return true;
311cfca06d7SDimitry Andric }
312cfca06d7SDimitry Andric
31363faed5bSDimitry Andric /// getModuleFlagsMetadata - Returns the module flags in the provided vector.
31463faed5bSDimitry Andric void Module::
getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> & Flags) const31563faed5bSDimitry Andric getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
31663faed5bSDimitry Andric const NamedMDNode *ModFlags = getModuleFlagsMetadata();
31763faed5bSDimitry Andric if (!ModFlags) return;
31863faed5bSDimitry Andric
3195ca98fd9SDimitry Andric for (const MDNode *Flag : ModFlags->operands()) {
32067c32a98SDimitry Andric ModFlagBehavior MFB;
321cfca06d7SDimitry Andric MDString *Key = nullptr;
322cfca06d7SDimitry Andric Metadata *Val = nullptr;
323cfca06d7SDimitry Andric if (isValidModuleFlag(*Flag, MFB, Key, Val)) {
324f8af5cf6SDimitry Andric // Check the operands of the MDNode before accessing the operands.
325f8af5cf6SDimitry Andric // The verifier will actually catch these failures.
32667c32a98SDimitry Andric Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
32763faed5bSDimitry Andric }
32863faed5bSDimitry Andric }
329f8af5cf6SDimitry Andric }
330f8af5cf6SDimitry Andric
331f8af5cf6SDimitry Andric /// Return the corresponding value if Key appears in module flags, otherwise
332f8af5cf6SDimitry Andric /// return null.
getModuleFlag(StringRef Key) const33367c32a98SDimitry Andric Metadata *Module::getModuleFlag(StringRef Key) const {
334f8af5cf6SDimitry Andric SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
335f8af5cf6SDimitry Andric getModuleFlagsMetadata(ModuleFlags);
3365ca98fd9SDimitry Andric for (const ModuleFlagEntry &MFE : ModuleFlags) {
337f8af5cf6SDimitry Andric if (Key == MFE.Key->getString())
338f8af5cf6SDimitry Andric return MFE.Val;
339f8af5cf6SDimitry Andric }
3405ca98fd9SDimitry Andric return nullptr;
341f8af5cf6SDimitry Andric }
34263faed5bSDimitry Andric
34363faed5bSDimitry Andric /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
34463faed5bSDimitry Andric /// represents module-level flags. This method returns null if there are no
34563faed5bSDimitry Andric /// module-level flags.
getModuleFlagsMetadata() const34663faed5bSDimitry Andric NamedMDNode *Module::getModuleFlagsMetadata() const {
34763faed5bSDimitry Andric return getNamedMetadata("llvm.module.flags");
34863faed5bSDimitry Andric }
34963faed5bSDimitry Andric
35063faed5bSDimitry Andric /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
35163faed5bSDimitry Andric /// represents module-level flags. If module-level flags aren't found, it
35263faed5bSDimitry Andric /// creates the named metadata that contains them.
getOrInsertModuleFlagsMetadata()35363faed5bSDimitry Andric NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
35463faed5bSDimitry Andric return getOrInsertNamedMetadata("llvm.module.flags");
35563faed5bSDimitry Andric }
35663faed5bSDimitry Andric
35763faed5bSDimitry Andric /// addModuleFlag - Add a module-level flag to the module-level flags
35863faed5bSDimitry Andric /// metadata. It will create the module-level flags named metadata if it doesn't
35963faed5bSDimitry Andric /// already exist.
addModuleFlag(ModFlagBehavior Behavior,StringRef Key,Metadata * Val)36063faed5bSDimitry Andric void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
36167c32a98SDimitry Andric Metadata *Val) {
36263faed5bSDimitry Andric Type *Int32Ty = Type::getInt32Ty(Context);
36367c32a98SDimitry Andric Metadata *Ops[3] = {
36467c32a98SDimitry Andric ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
36567c32a98SDimitry Andric MDString::get(Context, Key), Val};
36663faed5bSDimitry Andric getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
36763faed5bSDimitry Andric }
addModuleFlag(ModFlagBehavior Behavior,StringRef Key,Constant * Val)36863faed5bSDimitry Andric void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
36967c32a98SDimitry Andric Constant *Val) {
37067c32a98SDimitry Andric addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
37167c32a98SDimitry Andric }
addModuleFlag(ModFlagBehavior Behavior,StringRef Key,uint32_t Val)37267c32a98SDimitry Andric void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
37363faed5bSDimitry Andric uint32_t Val) {
37463faed5bSDimitry Andric Type *Int32Ty = Type::getInt32Ty(Context);
37563faed5bSDimitry Andric addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
37663faed5bSDimitry Andric }
addModuleFlag(MDNode * Node)37763faed5bSDimitry Andric void Module::addModuleFlag(MDNode *Node) {
37863faed5bSDimitry Andric assert(Node->getNumOperands() == 3 &&
37963faed5bSDimitry Andric "Invalid number of operands for module flag!");
38067c32a98SDimitry Andric assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
38163faed5bSDimitry Andric isa<MDString>(Node->getOperand(1)) &&
38263faed5bSDimitry Andric "Invalid operand types for module flag!");
38363faed5bSDimitry Andric getOrInsertModuleFlagsMetadata()->addOperand(Node);
38463faed5bSDimitry Andric }
385009b1c42SEd Schouten
setModuleFlag(ModFlagBehavior Behavior,StringRef Key,Metadata * Val)386cfca06d7SDimitry Andric void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
387cfca06d7SDimitry Andric Metadata *Val) {
388cfca06d7SDimitry Andric NamedMDNode *ModFlags = getOrInsertModuleFlagsMetadata();
389cfca06d7SDimitry Andric // Replace the flag if it already exists.
390ac9a064cSDimitry Andric for (MDNode *Flag : ModFlags->operands()) {
391cfca06d7SDimitry Andric ModFlagBehavior MFB;
392cfca06d7SDimitry Andric MDString *K = nullptr;
393cfca06d7SDimitry Andric Metadata *V = nullptr;
394cfca06d7SDimitry Andric if (isValidModuleFlag(*Flag, MFB, K, V) && K->getString() == Key) {
395cfca06d7SDimitry Andric Flag->replaceOperandWith(2, Val);
396cfca06d7SDimitry Andric return;
397cfca06d7SDimitry Andric }
398cfca06d7SDimitry Andric }
399cfca06d7SDimitry Andric addModuleFlag(Behavior, Key, Val);
400cfca06d7SDimitry Andric }
setModuleFlag(ModFlagBehavior Behavior,StringRef Key,Constant * Val)401ac9a064cSDimitry Andric void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
402ac9a064cSDimitry Andric Constant *Val) {
403ac9a064cSDimitry Andric setModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
404ac9a064cSDimitry Andric }
setModuleFlag(ModFlagBehavior Behavior,StringRef Key,uint32_t Val)405ac9a064cSDimitry Andric void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
406ac9a064cSDimitry Andric uint32_t Val) {
407ac9a064cSDimitry Andric Type *Int32Ty = Type::getInt32Ty(Context);
408ac9a064cSDimitry Andric setModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
409ac9a064cSDimitry Andric }
410cfca06d7SDimitry Andric
setDataLayout(StringRef Desc)4115ca98fd9SDimitry Andric void Module::setDataLayout(StringRef Desc) {
4125ca98fd9SDimitry Andric DL.reset(Desc);
4135ca98fd9SDimitry Andric }
4145ca98fd9SDimitry Andric
setDataLayout(const DataLayout & Other)4155a5ac124SDimitry Andric void Module::setDataLayout(const DataLayout &Other) { DL = Other; }
4165ca98fd9SDimitry Andric
operator *() const41701095a5dSDimitry Andric DICompileUnit *Module::debug_compile_units_iterator::operator*() const {
41801095a5dSDimitry Andric return cast<DICompileUnit>(CUs->getOperand(Idx));
41901095a5dSDimitry Andric }
operator ->() const42001095a5dSDimitry Andric DICompileUnit *Module::debug_compile_units_iterator::operator->() const {
42101095a5dSDimitry Andric return cast<DICompileUnit>(CUs->getOperand(Idx));
42201095a5dSDimitry Andric }
42301095a5dSDimitry Andric
SkipNoDebugCUs()42401095a5dSDimitry Andric void Module::debug_compile_units_iterator::SkipNoDebugCUs() {
42501095a5dSDimitry Andric while (CUs && (Idx < CUs->getNumOperands()) &&
42601095a5dSDimitry Andric ((*this)->getEmissionKind() == DICompileUnit::NoDebug))
42701095a5dSDimitry Andric ++Idx;
42801095a5dSDimitry Andric }
42901095a5dSDimitry Andric
global_objects()430706b4fc4SDimitry Andric iterator_range<Module::global_object_iterator> Module::global_objects() {
431706b4fc4SDimitry Andric return concat<GlobalObject>(functions(), globals());
432706b4fc4SDimitry Andric }
433706b4fc4SDimitry Andric iterator_range<Module::const_global_object_iterator>
global_objects() const434706b4fc4SDimitry Andric Module::global_objects() const {
435706b4fc4SDimitry Andric return concat<const GlobalObject>(functions(), globals());
436706b4fc4SDimitry Andric }
437706b4fc4SDimitry Andric
global_values()438706b4fc4SDimitry Andric iterator_range<Module::global_value_iterator> Module::global_values() {
439706b4fc4SDimitry Andric return concat<GlobalValue>(functions(), globals(), aliases(), ifuncs());
440706b4fc4SDimitry Andric }
441706b4fc4SDimitry Andric iterator_range<Module::const_global_value_iterator>
global_values() const442706b4fc4SDimitry Andric Module::global_values() const {
443706b4fc4SDimitry Andric return concat<const GlobalValue>(functions(), globals(), aliases(), ifuncs());
444706b4fc4SDimitry Andric }
445706b4fc4SDimitry Andric
446009b1c42SEd Schouten //===----------------------------------------------------------------------===//
4476fe5c7aaSRoman Divacky // Methods to control the materialization of GlobalValues in the Module.
4486fe5c7aaSRoman Divacky //
setMaterializer(GVMaterializer * GVM)4496fe5c7aaSRoman Divacky void Module::setMaterializer(GVMaterializer *GVM) {
4506fe5c7aaSRoman Divacky assert(!Materializer &&
451dd58ef01SDimitry Andric "Module already has a GVMaterializer. Call materializeAll"
4526fe5c7aaSRoman Divacky " to clear it out before setting another one.");
4536fe5c7aaSRoman Divacky Materializer.reset(GVM);
4546fe5c7aaSRoman Divacky }
4556fe5c7aaSRoman Divacky
materialize(GlobalValue * GV)456b915e9e0SDimitry Andric Error Module::materialize(GlobalValue *GV) {
457f8af5cf6SDimitry Andric if (!Materializer)
458b915e9e0SDimitry Andric return Error::success();
459f8af5cf6SDimitry Andric
46067c32a98SDimitry Andric return Materializer->materialize(GV);
4616fe5c7aaSRoman Divacky }
4626fe5c7aaSRoman Divacky
materializeAll()463b915e9e0SDimitry Andric Error Module::materializeAll() {
4646fe5c7aaSRoman Divacky if (!Materializer)
465b915e9e0SDimitry Andric return Error::success();
466dd58ef01SDimitry Andric std::unique_ptr<GVMaterializer> M = std::move(Materializer);
467dd58ef01SDimitry Andric return M->materializeModule();
4686fe5c7aaSRoman Divacky }
4696fe5c7aaSRoman Divacky
materializeMetadata()470b915e9e0SDimitry Andric Error Module::materializeMetadata() {
4715a5ac124SDimitry Andric if (!Materializer)
472b915e9e0SDimitry Andric return Error::success();
4735a5ac124SDimitry Andric return Materializer->materializeMetadata();
4745a5ac124SDimitry Andric }
4755a5ac124SDimitry Andric
4766fe5c7aaSRoman Divacky //===----------------------------------------------------------------------===//
477009b1c42SEd Schouten // Other module related stuff.
478009b1c42SEd Schouten //
479009b1c42SEd Schouten
getIdentifiedStructTypes() const48067c32a98SDimitry Andric std::vector<StructType *> Module::getIdentifiedStructTypes() const {
48167c32a98SDimitry Andric // If we have a materializer, it is possible that some unread function
48267c32a98SDimitry Andric // uses a type that is currently not visible to a TypeFinder, so ask
48367c32a98SDimitry Andric // the materializer which types it created.
48467c32a98SDimitry Andric if (Materializer)
48567c32a98SDimitry Andric return Materializer->getIdentifiedStructTypes();
48667c32a98SDimitry Andric
48767c32a98SDimitry Andric std::vector<StructType *> Ret;
48867c32a98SDimitry Andric TypeFinder SrcStructTypes;
48967c32a98SDimitry Andric SrcStructTypes.run(*this, true);
49067c32a98SDimitry Andric Ret.assign(SrcStructTypes.begin(), SrcStructTypes.end());
49167c32a98SDimitry Andric return Ret;
49267c32a98SDimitry Andric }
493009b1c42SEd Schouten
getUniqueIntrinsicName(StringRef BaseName,Intrinsic::ID Id,const FunctionType * Proto)494344a3780SDimitry Andric std::string Module::getUniqueIntrinsicName(StringRef BaseName, Intrinsic::ID Id,
495344a3780SDimitry Andric const FunctionType *Proto) {
496344a3780SDimitry Andric auto Encode = [&BaseName](unsigned Suffix) {
497344a3780SDimitry Andric return (Twine(BaseName) + "." + Twine(Suffix)).str();
498344a3780SDimitry Andric };
499344a3780SDimitry Andric
500344a3780SDimitry Andric {
501344a3780SDimitry Andric // fast path - the prototype is already known
502344a3780SDimitry Andric auto UinItInserted = UniquedIntrinsicNames.insert({{Id, Proto}, 0});
503344a3780SDimitry Andric if (!UinItInserted.second)
504344a3780SDimitry Andric return Encode(UinItInserted.first->second);
505344a3780SDimitry Andric }
506344a3780SDimitry Andric
507344a3780SDimitry Andric // Not known yet. A new entry was created with index 0. Check if there already
508344a3780SDimitry Andric // exists a matching declaration, or select a new entry.
509344a3780SDimitry Andric
510344a3780SDimitry Andric // Start looking for names with the current known maximum count (or 0).
511344a3780SDimitry Andric auto NiidItInserted = CurrentIntrinsicIds.insert({BaseName, 0});
512344a3780SDimitry Andric unsigned Count = NiidItInserted.first->second;
513344a3780SDimitry Andric
514344a3780SDimitry Andric // This might be slow if a whole population of intrinsics already existed, but
515344a3780SDimitry Andric // we cache the values for later usage.
516344a3780SDimitry Andric std::string NewName;
517344a3780SDimitry Andric while (true) {
518344a3780SDimitry Andric NewName = Encode(Count);
519344a3780SDimitry Andric GlobalValue *F = getNamedValue(NewName);
520344a3780SDimitry Andric if (!F) {
521344a3780SDimitry Andric // Reserve this entry for the new proto
522344a3780SDimitry Andric UniquedIntrinsicNames[{Id, Proto}] = Count;
523344a3780SDimitry Andric break;
524344a3780SDimitry Andric }
525344a3780SDimitry Andric
526344a3780SDimitry Andric // A declaration with this name already exists. Remember it.
527344a3780SDimitry Andric FunctionType *FT = dyn_cast<FunctionType>(F->getValueType());
528344a3780SDimitry Andric auto UinItInserted = UniquedIntrinsicNames.insert({{Id, FT}, Count});
529344a3780SDimitry Andric if (FT == Proto) {
530344a3780SDimitry Andric // It was a declaration for our prototype. This entry was allocated in the
531344a3780SDimitry Andric // beginning. Update the count to match the existing declaration.
532344a3780SDimitry Andric UinItInserted.first->second = Count;
533344a3780SDimitry Andric break;
534344a3780SDimitry Andric }
535344a3780SDimitry Andric
536344a3780SDimitry Andric ++Count;
537344a3780SDimitry Andric }
538344a3780SDimitry Andric
539344a3780SDimitry Andric NiidItInserted.first->second = Count + 1;
540344a3780SDimitry Andric
541344a3780SDimitry Andric return NewName;
542344a3780SDimitry Andric }
543344a3780SDimitry Andric
544b61ab53cSDimitry Andric // dropAllReferences() - This function causes all the subelements to "let go"
545009b1c42SEd Schouten // of all references that they are maintaining. This allows one to 'delete' a
546009b1c42SEd Schouten // whole module at a time, even though there may be circular references... first
547009b1c42SEd Schouten // all references are dropped, and all use counts go to zero. Then everything
548009b1c42SEd Schouten // is deleted for real. Note that no operations are valid on an object that
549009b1c42SEd Schouten // has "dropped all references", except operator delete.
550009b1c42SEd Schouten //
dropAllReferences()551009b1c42SEd Schouten void Module::dropAllReferences() {
5525ca98fd9SDimitry Andric for (Function &F : *this)
5535ca98fd9SDimitry Andric F.dropAllReferences();
554009b1c42SEd Schouten
5555ca98fd9SDimitry Andric for (GlobalVariable &GV : globals())
5565ca98fd9SDimitry Andric GV.dropAllReferences();
557009b1c42SEd Schouten
5585ca98fd9SDimitry Andric for (GlobalAlias &GA : aliases())
5595ca98fd9SDimitry Andric GA.dropAllReferences();
56001095a5dSDimitry Andric
56101095a5dSDimitry Andric for (GlobalIFunc &GIF : ifuncs())
56201095a5dSDimitry Andric GIF.dropAllReferences();
5635ca98fd9SDimitry Andric }
5645ca98fd9SDimitry Andric
getNumberRegisterParameters() const56571d5a254SDimitry Andric unsigned Module::getNumberRegisterParameters() const {
56671d5a254SDimitry Andric auto *Val =
56771d5a254SDimitry Andric cast_or_null<ConstantAsMetadata>(getModuleFlag("NumRegisterParameters"));
56871d5a254SDimitry Andric if (!Val)
56971d5a254SDimitry Andric return 0;
57071d5a254SDimitry Andric return cast<ConstantInt>(Val->getValue())->getZExtValue();
57171d5a254SDimitry Andric }
57271d5a254SDimitry Andric
getDwarfVersion() const5735ca98fd9SDimitry Andric unsigned Module::getDwarfVersion() const {
57467c32a98SDimitry Andric auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
5755ca98fd9SDimitry Andric if (!Val)
576dd58ef01SDimitry Andric return 0;
577dd58ef01SDimitry Andric return cast<ConstantInt>(Val->getValue())->getZExtValue();
578dd58ef01SDimitry Andric }
579dd58ef01SDimitry Andric
isDwarf64() const580344a3780SDimitry Andric bool Module::isDwarf64() const {
581344a3780SDimitry Andric auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("DWARF64"));
582344a3780SDimitry Andric return Val && cast<ConstantInt>(Val->getValue())->isOne();
583344a3780SDimitry Andric }
584344a3780SDimitry Andric
getCodeViewFlag() const585dd58ef01SDimitry Andric unsigned Module::getCodeViewFlag() const {
586dd58ef01SDimitry Andric auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CodeView"));
587dd58ef01SDimitry Andric if (!Val)
588dd58ef01SDimitry Andric return 0;
58967c32a98SDimitry Andric return cast<ConstantInt>(Val->getValue())->getZExtValue();
5905ca98fd9SDimitry Andric }
5915ca98fd9SDimitry Andric
getInstructionCount() const592344a3780SDimitry Andric unsigned Module::getInstructionCount() const {
593eb11fae6SDimitry Andric unsigned NumInstrs = 0;
594344a3780SDimitry Andric for (const Function &F : FunctionList)
595eb11fae6SDimitry Andric NumInstrs += F.getInstructionCount();
596eb11fae6SDimitry Andric return NumInstrs;
597eb11fae6SDimitry Andric }
598eb11fae6SDimitry Andric
getOrInsertComdat(StringRef Name)5995ca98fd9SDimitry Andric Comdat *Module::getOrInsertComdat(StringRef Name) {
60067c32a98SDimitry Andric auto &Entry = *ComdatSymTab.insert(std::make_pair(Name, Comdat())).first;
6015ca98fd9SDimitry Andric Entry.second.Name = &Entry;
6025ca98fd9SDimitry Andric return &Entry.second;
603009b1c42SEd Schouten }
60467c32a98SDimitry Andric
getPICLevel() const60567c32a98SDimitry Andric PICLevel::Level Module::getPICLevel() const {
60667c32a98SDimitry Andric auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));
60767c32a98SDimitry Andric
608dd58ef01SDimitry Andric if (!Val)
60901095a5dSDimitry Andric return PICLevel::NotPIC;
61067c32a98SDimitry Andric
61167c32a98SDimitry Andric return static_cast<PICLevel::Level>(
61267c32a98SDimitry Andric cast<ConstantInt>(Val->getValue())->getZExtValue());
61367c32a98SDimitry Andric }
61467c32a98SDimitry Andric
setPICLevel(PICLevel::Level PL)61567c32a98SDimitry Andric void Module::setPICLevel(PICLevel::Level PL) {
616e3b55780SDimitry Andric // The merge result of a non-PIC object and a PIC object can only be reliably
617e3b55780SDimitry Andric // used as a non-PIC object, so use the Min merge behavior.
618e3b55780SDimitry Andric addModuleFlag(ModFlagBehavior::Min, "PIC Level", PL);
61967c32a98SDimitry Andric }
620dd58ef01SDimitry Andric
getPIELevel() const62101095a5dSDimitry Andric PIELevel::Level Module::getPIELevel() const {
62201095a5dSDimitry Andric auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIE Level"));
62301095a5dSDimitry Andric
62401095a5dSDimitry Andric if (!Val)
62501095a5dSDimitry Andric return PIELevel::Default;
62601095a5dSDimitry Andric
62701095a5dSDimitry Andric return static_cast<PIELevel::Level>(
62801095a5dSDimitry Andric cast<ConstantInt>(Val->getValue())->getZExtValue());
629dd58ef01SDimitry Andric }
630dd58ef01SDimitry Andric
setPIELevel(PIELevel::Level PL)63101095a5dSDimitry Andric void Module::setPIELevel(PIELevel::Level PL) {
632ab44ce3dSDimitry Andric addModuleFlag(ModFlagBehavior::Max, "PIE Level", PL);
63301095a5dSDimitry Andric }
63401095a5dSDimitry Andric
getCodeModel() const635e3b55780SDimitry Andric std::optional<CodeModel::Model> Module::getCodeModel() const {
636d8e91e46SDimitry Andric auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Code Model"));
637d8e91e46SDimitry Andric
638d8e91e46SDimitry Andric if (!Val)
639e3b55780SDimitry Andric return std::nullopt;
640d8e91e46SDimitry Andric
641d8e91e46SDimitry Andric return static_cast<CodeModel::Model>(
642d8e91e46SDimitry Andric cast<ConstantInt>(Val->getValue())->getZExtValue());
643d8e91e46SDimitry Andric }
644d8e91e46SDimitry Andric
setCodeModel(CodeModel::Model CL)645d8e91e46SDimitry Andric void Module::setCodeModel(CodeModel::Model CL) {
646d8e91e46SDimitry Andric // Linking object files with different code models is undefined behavior
647d8e91e46SDimitry Andric // because the compiler would have to generate additional code (to span
648d8e91e46SDimitry Andric // longer jumps) if a larger code model is used with a smaller one.
649d8e91e46SDimitry Andric // Therefore we will treat attempts to mix code models as an error.
650d8e91e46SDimitry Andric addModuleFlag(ModFlagBehavior::Error, "Code Model", CL);
651d8e91e46SDimitry Andric }
652d8e91e46SDimitry Andric
getLargeDataThreshold() const653b1c73532SDimitry Andric std::optional<uint64_t> Module::getLargeDataThreshold() const {
654b1c73532SDimitry Andric auto *Val =
655b1c73532SDimitry Andric cast_or_null<ConstantAsMetadata>(getModuleFlag("Large Data Threshold"));
656b1c73532SDimitry Andric
657b1c73532SDimitry Andric if (!Val)
658b1c73532SDimitry Andric return std::nullopt;
659b1c73532SDimitry Andric
660b1c73532SDimitry Andric return cast<ConstantInt>(Val->getValue())->getZExtValue();
661b1c73532SDimitry Andric }
662b1c73532SDimitry Andric
setLargeDataThreshold(uint64_t Threshold)663b1c73532SDimitry Andric void Module::setLargeDataThreshold(uint64_t Threshold) {
664b1c73532SDimitry Andric // Since the large data threshold goes along with the code model, the merge
665b1c73532SDimitry Andric // behavior is the same.
666b1c73532SDimitry Andric addModuleFlag(ModFlagBehavior::Error, "Large Data Threshold",
667b1c73532SDimitry Andric ConstantInt::get(Type::getInt64Ty(Context), Threshold));
668b1c73532SDimitry Andric }
669b1c73532SDimitry Andric
setProfileSummary(Metadata * M,ProfileSummary::Kind Kind)670e6d15924SDimitry Andric void Module::setProfileSummary(Metadata *M, ProfileSummary::Kind Kind) {
671e6d15924SDimitry Andric if (Kind == ProfileSummary::PSK_CSInstr)
672cfca06d7SDimitry Andric setModuleFlag(ModFlagBehavior::Error, "CSProfileSummary", M);
673e6d15924SDimitry Andric else
674cfca06d7SDimitry Andric setModuleFlag(ModFlagBehavior::Error, "ProfileSummary", M);
67501095a5dSDimitry Andric }
67601095a5dSDimitry Andric
getProfileSummary(bool IsCS) const677b60736ecSDimitry Andric Metadata *Module::getProfileSummary(bool IsCS) const {
678e6d15924SDimitry Andric return (IsCS ? getModuleFlag("CSProfileSummary")
679e6d15924SDimitry Andric : getModuleFlag("ProfileSummary"));
68001095a5dSDimitry Andric }
68101095a5dSDimitry Andric
getSemanticInterposition() const682cfca06d7SDimitry Andric bool Module::getSemanticInterposition() const {
683cfca06d7SDimitry Andric Metadata *MF = getModuleFlag("SemanticInterposition");
684cfca06d7SDimitry Andric
685cfca06d7SDimitry Andric auto *Val = cast_or_null<ConstantAsMetadata>(MF);
686cfca06d7SDimitry Andric if (!Val)
687cfca06d7SDimitry Andric return false;
688cfca06d7SDimitry Andric
689cfca06d7SDimitry Andric return cast<ConstantInt>(Val->getValue())->getZExtValue();
690cfca06d7SDimitry Andric }
691cfca06d7SDimitry Andric
setSemanticInterposition(bool SI)692cfca06d7SDimitry Andric void Module::setSemanticInterposition(bool SI) {
693cfca06d7SDimitry Andric addModuleFlag(ModFlagBehavior::Error, "SemanticInterposition", SI);
694cfca06d7SDimitry Andric }
695cfca06d7SDimitry Andric
setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB)696b915e9e0SDimitry Andric void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) {
697b915e9e0SDimitry Andric OwnedMemoryBuffer = std::move(MB);
698b915e9e0SDimitry Andric }
699b915e9e0SDimitry Andric
getRtLibUseGOT() const700eb11fae6SDimitry Andric bool Module::getRtLibUseGOT() const {
701eb11fae6SDimitry Andric auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("RtLibUseGOT"));
702eb11fae6SDimitry Andric return Val && (cast<ConstantInt>(Val->getValue())->getZExtValue() > 0);
703eb11fae6SDimitry Andric }
704eb11fae6SDimitry Andric
setRtLibUseGOT()705eb11fae6SDimitry Andric void Module::setRtLibUseGOT() {
706eb11fae6SDimitry Andric addModuleFlag(ModFlagBehavior::Max, "RtLibUseGOT", 1);
707eb11fae6SDimitry Andric }
708eb11fae6SDimitry Andric
getDirectAccessExternalData() const7097fa27ce4SDimitry Andric bool Module::getDirectAccessExternalData() const {
7107fa27ce4SDimitry Andric auto *Val = cast_or_null<ConstantAsMetadata>(
7117fa27ce4SDimitry Andric getModuleFlag("direct-access-external-data"));
7127fa27ce4SDimitry Andric if (Val)
7137fa27ce4SDimitry Andric return cast<ConstantInt>(Val->getValue())->getZExtValue() > 0;
7147fa27ce4SDimitry Andric return getPICLevel() == PICLevel::NotPIC;
7157fa27ce4SDimitry Andric }
7167fa27ce4SDimitry Andric
setDirectAccessExternalData(bool Value)7177fa27ce4SDimitry Andric void Module::setDirectAccessExternalData(bool Value) {
7187fa27ce4SDimitry Andric addModuleFlag(ModFlagBehavior::Max, "direct-access-external-data", Value);
7197fa27ce4SDimitry Andric }
7207fa27ce4SDimitry Andric
getUwtable() const721145449b1SDimitry Andric UWTableKind Module::getUwtable() const {
722145449b1SDimitry Andric if (auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("uwtable")))
723145449b1SDimitry Andric return UWTableKind(cast<ConstantInt>(Val->getValue())->getZExtValue());
724145449b1SDimitry Andric return UWTableKind::None;
725344a3780SDimitry Andric }
726344a3780SDimitry Andric
setUwtable(UWTableKind Kind)727145449b1SDimitry Andric void Module::setUwtable(UWTableKind Kind) {
728145449b1SDimitry Andric addModuleFlag(ModFlagBehavior::Max, "uwtable", uint32_t(Kind));
729145449b1SDimitry Andric }
730344a3780SDimitry Andric
getFramePointer() const731344a3780SDimitry Andric FramePointerKind Module::getFramePointer() const {
732344a3780SDimitry Andric auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("frame-pointer"));
733344a3780SDimitry Andric return static_cast<FramePointerKind>(
734344a3780SDimitry Andric Val ? cast<ConstantInt>(Val->getValue())->getZExtValue() : 0);
735344a3780SDimitry Andric }
736344a3780SDimitry Andric
setFramePointer(FramePointerKind Kind)737344a3780SDimitry Andric void Module::setFramePointer(FramePointerKind Kind) {
738344a3780SDimitry Andric addModuleFlag(ModFlagBehavior::Max, "frame-pointer", static_cast<int>(Kind));
739344a3780SDimitry Andric }
740344a3780SDimitry Andric
getStackProtectorGuard() const741344a3780SDimitry Andric StringRef Module::getStackProtectorGuard() const {
742344a3780SDimitry Andric Metadata *MD = getModuleFlag("stack-protector-guard");
743344a3780SDimitry Andric if (auto *MDS = dyn_cast_or_null<MDString>(MD))
744344a3780SDimitry Andric return MDS->getString();
745344a3780SDimitry Andric return {};
746344a3780SDimitry Andric }
747344a3780SDimitry Andric
setStackProtectorGuard(StringRef Kind)748344a3780SDimitry Andric void Module::setStackProtectorGuard(StringRef Kind) {
749344a3780SDimitry Andric MDString *ID = MDString::get(getContext(), Kind);
750344a3780SDimitry Andric addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard", ID);
751344a3780SDimitry Andric }
752344a3780SDimitry Andric
getStackProtectorGuardReg() const753344a3780SDimitry Andric StringRef Module::getStackProtectorGuardReg() const {
754344a3780SDimitry Andric Metadata *MD = getModuleFlag("stack-protector-guard-reg");
755344a3780SDimitry Andric if (auto *MDS = dyn_cast_or_null<MDString>(MD))
756344a3780SDimitry Andric return MDS->getString();
757344a3780SDimitry Andric return {};
758344a3780SDimitry Andric }
759344a3780SDimitry Andric
setStackProtectorGuardReg(StringRef Reg)760344a3780SDimitry Andric void Module::setStackProtectorGuardReg(StringRef Reg) {
761344a3780SDimitry Andric MDString *ID = MDString::get(getContext(), Reg);
762344a3780SDimitry Andric addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-reg", ID);
763344a3780SDimitry Andric }
764344a3780SDimitry Andric
getStackProtectorGuardSymbol() const7651f917f69SDimitry Andric StringRef Module::getStackProtectorGuardSymbol() const {
7661f917f69SDimitry Andric Metadata *MD = getModuleFlag("stack-protector-guard-symbol");
7671f917f69SDimitry Andric if (auto *MDS = dyn_cast_or_null<MDString>(MD))
7681f917f69SDimitry Andric return MDS->getString();
7691f917f69SDimitry Andric return {};
7701f917f69SDimitry Andric }
7711f917f69SDimitry Andric
setStackProtectorGuardSymbol(StringRef Symbol)7721f917f69SDimitry Andric void Module::setStackProtectorGuardSymbol(StringRef Symbol) {
7731f917f69SDimitry Andric MDString *ID = MDString::get(getContext(), Symbol);
7741f917f69SDimitry Andric addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-symbol", ID);
7751f917f69SDimitry Andric }
7761f917f69SDimitry Andric
getStackProtectorGuardOffset() const777344a3780SDimitry Andric int Module::getStackProtectorGuardOffset() const {
778344a3780SDimitry Andric Metadata *MD = getModuleFlag("stack-protector-guard-offset");
779344a3780SDimitry Andric if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
780344a3780SDimitry Andric return CI->getSExtValue();
781344a3780SDimitry Andric return INT_MAX;
782344a3780SDimitry Andric }
783344a3780SDimitry Andric
setStackProtectorGuardOffset(int Offset)784344a3780SDimitry Andric void Module::setStackProtectorGuardOffset(int Offset) {
785344a3780SDimitry Andric addModuleFlag(ModFlagBehavior::Error, "stack-protector-guard-offset", Offset);
786344a3780SDimitry Andric }
787344a3780SDimitry Andric
getOverrideStackAlignment() const788344a3780SDimitry Andric unsigned Module::getOverrideStackAlignment() const {
789344a3780SDimitry Andric Metadata *MD = getModuleFlag("override-stack-alignment");
790344a3780SDimitry Andric if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
791344a3780SDimitry Andric return CI->getZExtValue();
792344a3780SDimitry Andric return 0;
793344a3780SDimitry Andric }
794344a3780SDimitry Andric
getMaxTLSAlignment() const7957fa27ce4SDimitry Andric unsigned Module::getMaxTLSAlignment() const {
7967fa27ce4SDimitry Andric Metadata *MD = getModuleFlag("MaxTLSAlign");
7977fa27ce4SDimitry Andric if (auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(MD))
7987fa27ce4SDimitry Andric return CI->getZExtValue();
7997fa27ce4SDimitry Andric return 0;
8007fa27ce4SDimitry Andric }
8017fa27ce4SDimitry Andric
setOverrideStackAlignment(unsigned Align)802344a3780SDimitry Andric void Module::setOverrideStackAlignment(unsigned Align) {
803344a3780SDimitry Andric addModuleFlag(ModFlagBehavior::Error, "override-stack-alignment", Align);
804344a3780SDimitry Andric }
805344a3780SDimitry Andric
addSDKVersionMD(const VersionTuple & V,Module & M,StringRef Name)806145449b1SDimitry Andric static void addSDKVersionMD(const VersionTuple &V, Module &M, StringRef Name) {
807d8e91e46SDimitry Andric SmallVector<unsigned, 3> Entries;
808d8e91e46SDimitry Andric Entries.push_back(V.getMajor());
809d8e91e46SDimitry Andric if (auto Minor = V.getMinor()) {
810d8e91e46SDimitry Andric Entries.push_back(*Minor);
811d8e91e46SDimitry Andric if (auto Subminor = V.getSubminor())
812d8e91e46SDimitry Andric Entries.push_back(*Subminor);
813d8e91e46SDimitry Andric // Ignore the 'build' component as it can't be represented in the object
814d8e91e46SDimitry Andric // file.
815d8e91e46SDimitry Andric }
816145449b1SDimitry Andric M.addModuleFlag(Module::ModFlagBehavior::Warning, Name,
817145449b1SDimitry Andric ConstantDataArray::get(M.getContext(), Entries));
818145449b1SDimitry Andric }
819145449b1SDimitry Andric
setSDKVersion(const VersionTuple & V)820145449b1SDimitry Andric void Module::setSDKVersion(const VersionTuple &V) {
821145449b1SDimitry Andric addSDKVersionMD(V, *this, "SDK Version");
822d8e91e46SDimitry Andric }
823d8e91e46SDimitry Andric
getSDKVersionMD(Metadata * MD)82477fc4c14SDimitry Andric static VersionTuple getSDKVersionMD(Metadata *MD) {
82577fc4c14SDimitry Andric auto *CM = dyn_cast_or_null<ConstantAsMetadata>(MD);
826d8e91e46SDimitry Andric if (!CM)
827d8e91e46SDimitry Andric return {};
828d8e91e46SDimitry Andric auto *Arr = dyn_cast_or_null<ConstantDataArray>(CM->getValue());
829d8e91e46SDimitry Andric if (!Arr)
830d8e91e46SDimitry Andric return {};
831e3b55780SDimitry Andric auto getVersionComponent = [&](unsigned Index) -> std::optional<unsigned> {
832d8e91e46SDimitry Andric if (Index >= Arr->getNumElements())
833e3b55780SDimitry Andric return std::nullopt;
834d8e91e46SDimitry Andric return (unsigned)Arr->getElementAsInteger(Index);
835d8e91e46SDimitry Andric };
836d8e91e46SDimitry Andric auto Major = getVersionComponent(0);
837d8e91e46SDimitry Andric if (!Major)
838d8e91e46SDimitry Andric return {};
839d8e91e46SDimitry Andric VersionTuple Result = VersionTuple(*Major);
840d8e91e46SDimitry Andric if (auto Minor = getVersionComponent(1)) {
841d8e91e46SDimitry Andric Result = VersionTuple(*Major, *Minor);
842d8e91e46SDimitry Andric if (auto Subminor = getVersionComponent(2)) {
843d8e91e46SDimitry Andric Result = VersionTuple(*Major, *Minor, *Subminor);
844d8e91e46SDimitry Andric }
845d8e91e46SDimitry Andric }
846d8e91e46SDimitry Andric return Result;
847d8e91e46SDimitry Andric }
848d8e91e46SDimitry Andric
getSDKVersion() const84977fc4c14SDimitry Andric VersionTuple Module::getSDKVersion() const {
85077fc4c14SDimitry Andric return getSDKVersionMD(getModuleFlag("SDK Version"));
85177fc4c14SDimitry Andric }
85277fc4c14SDimitry Andric
collectUsedGlobalVariables(const Module & M,SmallVectorImpl<GlobalValue * > & Vec,bool CompilerUsed)85301095a5dSDimitry Andric GlobalVariable *llvm::collectUsedGlobalVariables(
854344a3780SDimitry Andric const Module &M, SmallVectorImpl<GlobalValue *> &Vec, bool CompilerUsed) {
85501095a5dSDimitry Andric const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
85601095a5dSDimitry Andric GlobalVariable *GV = M.getGlobalVariable(Name);
85701095a5dSDimitry Andric if (!GV || !GV->hasInitializer())
85801095a5dSDimitry Andric return GV;
85901095a5dSDimitry Andric
86001095a5dSDimitry Andric const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
86101095a5dSDimitry Andric for (Value *Op : Init->operands()) {
8621d5ae102SDimitry Andric GlobalValue *G = cast<GlobalValue>(Op->stripPointerCasts());
863344a3780SDimitry Andric Vec.push_back(G);
86401095a5dSDimitry Andric }
86501095a5dSDimitry Andric return GV;
866dd58ef01SDimitry Andric }
867cfca06d7SDimitry Andric
setPartialSampleProfileRatio(const ModuleSummaryIndex & Index)868cfca06d7SDimitry Andric void Module::setPartialSampleProfileRatio(const ModuleSummaryIndex &Index) {
869cfca06d7SDimitry Andric if (auto *SummaryMD = getProfileSummary(/*IsCS*/ false)) {
870cfca06d7SDimitry Andric std::unique_ptr<ProfileSummary> ProfileSummary(
871cfca06d7SDimitry Andric ProfileSummary::getFromMD(SummaryMD));
872cfca06d7SDimitry Andric if (ProfileSummary) {
873cfca06d7SDimitry Andric if (ProfileSummary->getKind() != ProfileSummary::PSK_Sample ||
874cfca06d7SDimitry Andric !ProfileSummary->isPartialProfile())
875cfca06d7SDimitry Andric return;
876cfca06d7SDimitry Andric uint64_t BlockCount = Index.getBlockCount();
877cfca06d7SDimitry Andric uint32_t NumCounts = ProfileSummary->getNumCounts();
878cfca06d7SDimitry Andric if (!NumCounts)
879cfca06d7SDimitry Andric return;
880cfca06d7SDimitry Andric double Ratio = (double)BlockCount / NumCounts;
881cfca06d7SDimitry Andric ProfileSummary->setPartialProfileRatio(Ratio);
882cfca06d7SDimitry Andric setProfileSummary(ProfileSummary->getMD(getContext()),
883cfca06d7SDimitry Andric ProfileSummary::PSK_Sample);
884cfca06d7SDimitry Andric }
885cfca06d7SDimitry Andric }
886cfca06d7SDimitry Andric }
88777fc4c14SDimitry Andric
getDarwinTargetVariantTriple() const88877fc4c14SDimitry Andric StringRef Module::getDarwinTargetVariantTriple() const {
88977fc4c14SDimitry Andric if (const auto *MD = getModuleFlag("darwin.target_variant.triple"))
89077fc4c14SDimitry Andric return cast<MDString>(MD)->getString();
89177fc4c14SDimitry Andric return "";
89277fc4c14SDimitry Andric }
89377fc4c14SDimitry Andric
setDarwinTargetVariantTriple(StringRef T)894145449b1SDimitry Andric void Module::setDarwinTargetVariantTriple(StringRef T) {
895ac9a064cSDimitry Andric addModuleFlag(ModFlagBehavior::Warning, "darwin.target_variant.triple",
896145449b1SDimitry Andric MDString::get(getContext(), T));
897145449b1SDimitry Andric }
898145449b1SDimitry Andric
getDarwinTargetVariantSDKVersion() const89977fc4c14SDimitry Andric VersionTuple Module::getDarwinTargetVariantSDKVersion() const {
90077fc4c14SDimitry Andric return getSDKVersionMD(getModuleFlag("darwin.target_variant.SDK Version"));
90177fc4c14SDimitry Andric }
902145449b1SDimitry Andric
setDarwinTargetVariantSDKVersion(VersionTuple Version)903145449b1SDimitry Andric void Module::setDarwinTargetVariantSDKVersion(VersionTuple Version) {
904145449b1SDimitry Andric addSDKVersionMD(Version, *this, "darwin.target_variant.SDK Version");
905145449b1SDimitry Andric }
906