23 #define DEBUG_TYPE "moduleutils" 37 unsigned n =
Init->getNumOperands();
38 CurrentCtors.reserve(n + 1);
39 for (
unsigned i = 0; i != n; ++i)
40 CurrentCtors.push_back(cast<Constant>(
Init->getOperand(i)));
42 GVCtor->eraseFromParent();
47 CSVals[0] = IRB.getInt32(Priority);
54 CurrentCtors.push_back(RuntimeCtorInit);
80 for (
auto &
Op : CA->operands()) {
89 for (
auto *V : Values) {
101 GV->setSection(
"llvm.metadata");
115 assert(!InitName.
empty() &&
"Expected init function name");
116 return M.getOrInsertFunction(
135 assert(!InitName.
empty() &&
"Expected init function name");
137 "Sanitizer's init function expects different number of arguments");
143 if (!VersionCheckName.
empty()) {
147 IRB.CreateCall(VersionCheckFunction, {});
149 return std::make_pair(Ctor, InitFunction);
152 std::pair<Function *, FunctionCallee>
158 assert(!CtorName.
empty() &&
"Expected ctor function name");
160 if (
Function *Ctor = M.getFunction(CtorName))
163 if (Ctor->arg_size() == 0 ||
170 M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName);
171 FunctionsCreatedCallback(Ctor, InitFunction);
172 return std::make_pair(Ctor, InitFunction);
176 assert(!
Name.empty() &&
"Expected init function name");
178 if (
F->arg_size() != 0 ||
182 Stream <<
"Sanitizer interface function defined with wrong type: " << *
F;
204 for (
Function *
F : DeadComdatFunctions) {
206 assert(
C &&
"Expected all input GVs to be in a comdat!");
207 ComdatEntriesCovered[
C] += 1;
210 auto CheckComdat = [&](
Comdat &
C) {
211 auto CI = ComdatEntriesCovered.
find(&
C);
212 if (CI == ComdatEntriesCovered.
end())
217 if (CI->second > 0) {
224 ComdatEntriesCovered.
erase(CI);
227 auto CheckAllComdats = [&] {
231 if (ComdatEntriesCovered.
empty())
235 if (
Comdat *
C = GV.getComdat()) {
237 if (ComdatEntriesCovered.
empty())
241 if (
Comdat *
C = GA.getComdat()) {
243 if (ComdatEntriesCovered.
empty())
249 if (ComdatEntriesCovered.
empty()) {
250 DeadComdatFunctions.clear();
257 ComdatEntriesCovered.
end();
263 bool ExportsSymbols =
false;
265 if (GV.isDeclaration() || GV.getName().startswith(
"llvm.") ||
266 !GV.hasExternalLinkage() || GV.hasComdat())
268 ExportsSymbols =
true;
275 for (
auto &GV : M->globals())
277 for (
auto &GA : M->aliases())
279 for (
auto &
IF : M->ifuncs())
290 return (
"$" + Str).str();
295 if (VariantMappings.
empty())
300 for (
const std::string &VariantMapping : VariantMappings)
301 Out << VariantMapping <<
",";
308 for (
const std::string &VariantMapping : VariantMappings) {
309 LLVM_DEBUG(
dbgs() <<
"VFABI: adding mapping '" << VariantMapping <<
"'\n");
311 assert(
VI.hasValue() &&
"Cannot add an invalid VFABI name.");
312 assert(M->getNamedValue(
VI.getValue().VectorName) &&
313 "Cannot add variant to attribute: " 314 "vector function declaration is missing.");
void appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Same as appendToGlobalCtors(), but for global dtors.
Special purpose, only applies to global arrays.
Function * getOrCreateInitFunction(Module &M, StringRef Name)
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
std::pair< Function *, FunctionCallee > createSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, StringRef VersionCheckName=StringRef())
Creates sanitizer constructor function, and calls sanitizer's init function from it.
A Module instance is used to store all the information related to an LLVM module.
static void stringifyResult(MD5Result &Result, SmallString< 32 > &Str)
Translates the bytes in Res to a hex string that is deposited into Str.
LLVM_NODISCARD bool empty() const
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
unsigned getNumElements() const
Random access to the elements.
This class represents a function call, abstracting a target machine's calling convention.
An efficient, type-erasing, non-owning reference to a callable.
A raw_ostream that writes to an SmallVector or SmallString.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
void addAttribute(unsigned i, Attribute::AttrKind Kind)
adds the attribute to the list of attributes.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, Instruction *InsertBefore=nullptr)
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Class to represent struct types.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName, ArrayRef< Type * > InitArgTypes)
void filterDeadComdatFunctions(Module &M, SmallVectorImpl< Function * > &DeadComdatFunctions)
Filter out potentially dead comdat functions where other entries keep the entire comdat group alive.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
StringRef str() const
Explicit conversion to StringRef.
Class to represent function types.
Class to represent array types.
static constexpr char const * MappingsAttrName
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
iterator find(const_arg_type_t< KeyT > Val)
std::pair< Function *, FunctionCallee > getOrCreateSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, function_ref< void(Function *, FunctionCallee)> FunctionsCreatedCallback, StringRef VersionCheckName=StringRef())
Creates sanitizer constructor function lazily.
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
const BasicBlock & getEntryBlock() const
bool erase(const KeyT &Val)
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed.
size_t size() const
size - Get the array size.
This is an important base class in LLVM.
static void appendToGlobalArray(const char *Array, Module &M, Function *F, int Priority, Constant *Data)
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
static Type * getVoidTy(LLVMContext &C)
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
static Constant * get(StructType *T, ArrayRef< Constant * > V)
std::string getUniqueModuleId(Module *M)
Produce a unique identifier for this module by taking the MD5 sum of the names of the module's strong...
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Module.h This file contains the declarations for the Module class.
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
const Comdat * getComdat() const
static void appendToUsedList(Module &M, StringRef Name, ArrayRef< GlobalValue * > Values)
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Rename collisions when linking (static functions).
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
LLVM_NODISCARD bool empty() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Function * createSanitizerCtor(Module &M, StringRef CtorName)
Creates sanitizer constructor function.
void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
StringRef - Represent a constant reference to a string, i.e.
Optional< VFInfo > tryDemangleForVFABI(StringRef MangledName, const Module &M)
Function to construct a VFInfo out of a mangled names in the following format:
void setVectorVariantNames(CallInst *CI, const SmallVector< std::string, 8 > &VariantMappings)
Overwrite the Vector Function ABI variants attribute with the names provide in VariantMappings.