10b57cec5SDimitry Andric //===-- ModuleUtils.cpp - Functions to manipulate Modules -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This family of functions perform manipulations on Modules.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
145ffd83dbSDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
15480093f4SDimitry Andric #include "llvm/Analysis/VectorUtils.h"
160b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
170b57cec5SDimitry Andric #include "llvm/IR/Function.h"
180b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
190b57cec5SDimitry Andric #include "llvm/IR/Module.h"
200b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
235ffd83dbSDimitry Andric #define DEBUG_TYPE "moduleutils"
245ffd83dbSDimitry Andric 
appendToGlobalArray(const char * Array,Module & M,Function * F,int Priority,Constant * Data)250b57cec5SDimitry Andric static void appendToGlobalArray(const char *Array, Module &M, Function *F,
260b57cec5SDimitry Andric                                 int Priority, Constant *Data) {
270b57cec5SDimitry Andric   IRBuilder<> IRB(M.getContext());
280b57cec5SDimitry Andric   FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric   // Get the current set of static global constructors and add the new ctor
310b57cec5SDimitry Andric   // to the list.
320b57cec5SDimitry Andric   SmallVector<Constant *, 16> CurrentCtors;
330b57cec5SDimitry Andric   StructType *EltTy = StructType::get(
340b57cec5SDimitry Andric       IRB.getInt32Ty(), PointerType::getUnqual(FnTy), IRB.getInt8PtrTy());
350b57cec5SDimitry Andric   if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
360b57cec5SDimitry Andric     if (Constant *Init = GVCtor->getInitializer()) {
370b57cec5SDimitry Andric       unsigned n = Init->getNumOperands();
380b57cec5SDimitry Andric       CurrentCtors.reserve(n + 1);
390b57cec5SDimitry Andric       for (unsigned i = 0; i != n; ++i)
400b57cec5SDimitry Andric         CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
410b57cec5SDimitry Andric     }
420b57cec5SDimitry Andric     GVCtor->eraseFromParent();
430b57cec5SDimitry Andric   }
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   // Build a 3 field global_ctor entry.  We don't take a comdat key.
460b57cec5SDimitry Andric   Constant *CSVals[3];
470b57cec5SDimitry Andric   CSVals[0] = IRB.getInt32(Priority);
480b57cec5SDimitry Andric   CSVals[1] = F;
490b57cec5SDimitry Andric   CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getInt8PtrTy())
500b57cec5SDimitry Andric                    : Constant::getNullValue(IRB.getInt8PtrTy());
510b57cec5SDimitry Andric   Constant *RuntimeCtorInit =
520b57cec5SDimitry Andric       ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   CurrentCtors.push_back(RuntimeCtorInit);
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   // Create a new initializer.
570b57cec5SDimitry Andric   ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
580b57cec5SDimitry Andric   Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   // Create the new global variable and replace all uses of
610b57cec5SDimitry Andric   // the old global variable with the new one.
620b57cec5SDimitry Andric   (void)new GlobalVariable(M, NewInit->getType(), false,
630b57cec5SDimitry Andric                            GlobalValue::AppendingLinkage, NewInit, Array);
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
appendToGlobalCtors(Module & M,Function * F,int Priority,Constant * Data)660b57cec5SDimitry Andric void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data) {
670b57cec5SDimitry Andric   appendToGlobalArray("llvm.global_ctors", M, F, Priority, Data);
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric 
appendToGlobalDtors(Module & M,Function * F,int Priority,Constant * Data)700b57cec5SDimitry Andric void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data) {
710b57cec5SDimitry Andric   appendToGlobalArray("llvm.global_dtors", M, F, Priority, Data);
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric 
appendToUsedList(Module & M,StringRef Name,ArrayRef<GlobalValue * > Values)740b57cec5SDimitry Andric static void appendToUsedList(Module &M, StringRef Name, ArrayRef<GlobalValue *> Values) {
750b57cec5SDimitry Andric   GlobalVariable *GV = M.getGlobalVariable(Name);
760b57cec5SDimitry Andric   SmallPtrSet<Constant *, 16> InitAsSet;
770b57cec5SDimitry Andric   SmallVector<Constant *, 16> Init;
780b57cec5SDimitry Andric   if (GV) {
79*5f7ddb14SDimitry Andric     if (GV->hasInitializer()) {
808bcb0991SDimitry Andric       auto *CA = cast<ConstantArray>(GV->getInitializer());
810b57cec5SDimitry Andric       for (auto &Op : CA->operands()) {
820b57cec5SDimitry Andric         Constant *C = cast_or_null<Constant>(Op);
830b57cec5SDimitry Andric         if (InitAsSet.insert(C).second)
840b57cec5SDimitry Andric           Init.push_back(C);
850b57cec5SDimitry Andric       }
86*5f7ddb14SDimitry Andric     }
870b57cec5SDimitry Andric     GV->eraseFromParent();
880b57cec5SDimitry Andric   }
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
910b57cec5SDimitry Andric   for (auto *V : Values) {
92*5f7ddb14SDimitry Andric     Constant *C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(V, Int8PtrTy);
930b57cec5SDimitry Andric     if (InitAsSet.insert(C).second)
940b57cec5SDimitry Andric       Init.push_back(C);
950b57cec5SDimitry Andric   }
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   if (Init.empty())
980b57cec5SDimitry Andric     return;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
1010b57cec5SDimitry Andric   GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
1020b57cec5SDimitry Andric                                 ConstantArray::get(ATy, Init), Name);
1030b57cec5SDimitry Andric   GV->setSection("llvm.metadata");
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric 
appendToUsed(Module & M,ArrayRef<GlobalValue * > Values)1060b57cec5SDimitry Andric void llvm::appendToUsed(Module &M, ArrayRef<GlobalValue *> Values) {
1070b57cec5SDimitry Andric   appendToUsedList(M, "llvm.used", Values);
1080b57cec5SDimitry Andric }
1090b57cec5SDimitry Andric 
appendToCompilerUsed(Module & M,ArrayRef<GlobalValue * > Values)1100b57cec5SDimitry Andric void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
1110b57cec5SDimitry Andric   appendToUsedList(M, "llvm.compiler.used", Values);
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric FunctionCallee
declareSanitizerInitFunction(Module & M,StringRef InitName,ArrayRef<Type * > InitArgTypes)1150b57cec5SDimitry Andric llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
1160b57cec5SDimitry Andric                                    ArrayRef<Type *> InitArgTypes) {
1170b57cec5SDimitry Andric   assert(!InitName.empty() && "Expected init function name");
1180b57cec5SDimitry Andric   return M.getOrInsertFunction(
1190b57cec5SDimitry Andric       InitName,
1200b57cec5SDimitry Andric       FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
1210b57cec5SDimitry Andric       AttributeList());
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric 
createSanitizerCtor(Module & M,StringRef CtorName)1245ffd83dbSDimitry Andric Function *llvm::createSanitizerCtor(Module &M, StringRef CtorName) {
125*5f7ddb14SDimitry Andric   Function *Ctor = Function::createWithDefaultAttr(
1265ffd83dbSDimitry Andric       FunctionType::get(Type::getVoidTy(M.getContext()), false),
127*5f7ddb14SDimitry Andric       GlobalValue::InternalLinkage, 0, CtorName, &M);
128*5f7ddb14SDimitry Andric   Ctor->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
1295ffd83dbSDimitry Andric   BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
1305ffd83dbSDimitry Andric   ReturnInst::Create(M.getContext(), CtorBB);
131*5f7ddb14SDimitry Andric   // Ensure Ctor cannot be discarded, even if in a comdat.
132*5f7ddb14SDimitry Andric   appendToUsed(M, {Ctor});
1335ffd83dbSDimitry Andric   return Ctor;
1345ffd83dbSDimitry Andric }
1355ffd83dbSDimitry Andric 
createSanitizerCtorAndInitFunctions(Module & M,StringRef CtorName,StringRef InitName,ArrayRef<Type * > InitArgTypes,ArrayRef<Value * > InitArgs,StringRef VersionCheckName)1360b57cec5SDimitry Andric std::pair<Function *, FunctionCallee> llvm::createSanitizerCtorAndInitFunctions(
1370b57cec5SDimitry Andric     Module &M, StringRef CtorName, StringRef InitName,
1380b57cec5SDimitry Andric     ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
1390b57cec5SDimitry Andric     StringRef VersionCheckName) {
1400b57cec5SDimitry Andric   assert(!InitName.empty() && "Expected init function name");
1410b57cec5SDimitry Andric   assert(InitArgs.size() == InitArgTypes.size() &&
1420b57cec5SDimitry Andric          "Sanitizer's init function expects different number of arguments");
1430b57cec5SDimitry Andric   FunctionCallee InitFunction =
1440b57cec5SDimitry Andric       declareSanitizerInitFunction(M, InitName, InitArgTypes);
1455ffd83dbSDimitry Andric   Function *Ctor = createSanitizerCtor(M, CtorName);
1465ffd83dbSDimitry Andric   IRBuilder<> IRB(Ctor->getEntryBlock().getTerminator());
1470b57cec5SDimitry Andric   IRB.CreateCall(InitFunction, InitArgs);
1480b57cec5SDimitry Andric   if (!VersionCheckName.empty()) {
1490b57cec5SDimitry Andric     FunctionCallee VersionCheckFunction = M.getOrInsertFunction(
1500b57cec5SDimitry Andric         VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
1510b57cec5SDimitry Andric         AttributeList());
1520b57cec5SDimitry Andric     IRB.CreateCall(VersionCheckFunction, {});
1530b57cec5SDimitry Andric   }
1540b57cec5SDimitry Andric   return std::make_pair(Ctor, InitFunction);
1550b57cec5SDimitry Andric }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric 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)1580b57cec5SDimitry Andric llvm::getOrCreateSanitizerCtorAndInitFunctions(
1590b57cec5SDimitry Andric     Module &M, StringRef CtorName, StringRef InitName,
1600b57cec5SDimitry Andric     ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
1610b57cec5SDimitry Andric     function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
1620b57cec5SDimitry Andric     StringRef VersionCheckName) {
1630b57cec5SDimitry Andric   assert(!CtorName.empty() && "Expected ctor function name");
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric   if (Function *Ctor = M.getFunction(CtorName))
1660b57cec5SDimitry Andric     // FIXME: Sink this logic into the module, similar to the handling of
1670b57cec5SDimitry Andric     // globals. This will make moving to a concurrent model much easier.
1680b57cec5SDimitry Andric     if (Ctor->arg_size() == 0 ||
1690b57cec5SDimitry Andric         Ctor->getReturnType() == Type::getVoidTy(M.getContext()))
1700b57cec5SDimitry Andric       return {Ctor, declareSanitizerInitFunction(M, InitName, InitArgTypes)};
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   Function *Ctor;
1730b57cec5SDimitry Andric   FunctionCallee InitFunction;
1740b57cec5SDimitry Andric   std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions(
1750b57cec5SDimitry Andric       M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName);
1760b57cec5SDimitry Andric   FunctionsCreatedCallback(Ctor, InitFunction);
1770b57cec5SDimitry Andric   return std::make_pair(Ctor, InitFunction);
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric 
filterDeadComdatFunctions(Module & M,SmallVectorImpl<Function * > & DeadComdatFunctions)1800b57cec5SDimitry Andric void llvm::filterDeadComdatFunctions(
1810b57cec5SDimitry Andric     Module &M, SmallVectorImpl<Function *> &DeadComdatFunctions) {
1820b57cec5SDimitry Andric   // Build a map from the comdat to the number of entries in that comdat we
1830b57cec5SDimitry Andric   // think are dead. If this fully covers the comdat group, then the entire
1840b57cec5SDimitry Andric   // group is dead. If we find another entry in the comdat group though, we'll
1850b57cec5SDimitry Andric   // have to preserve the whole group.
1860b57cec5SDimitry Andric   SmallDenseMap<Comdat *, int, 16> ComdatEntriesCovered;
1870b57cec5SDimitry Andric   for (Function *F : DeadComdatFunctions) {
1880b57cec5SDimitry Andric     Comdat *C = F->getComdat();
1890b57cec5SDimitry Andric     assert(C && "Expected all input GVs to be in a comdat!");
1900b57cec5SDimitry Andric     ComdatEntriesCovered[C] += 1;
1910b57cec5SDimitry Andric   }
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   auto CheckComdat = [&](Comdat &C) {
1940b57cec5SDimitry Andric     auto CI = ComdatEntriesCovered.find(&C);
1950b57cec5SDimitry Andric     if (CI == ComdatEntriesCovered.end())
1960b57cec5SDimitry Andric       return;
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric     // If this could have been covered by a dead entry, just subtract one to
1990b57cec5SDimitry Andric     // account for it.
2000b57cec5SDimitry Andric     if (CI->second > 0) {
2010b57cec5SDimitry Andric       CI->second -= 1;
2020b57cec5SDimitry Andric       return;
2030b57cec5SDimitry Andric     }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric     // If we've already accounted for all the entries that were dead, the
2060b57cec5SDimitry Andric     // entire comdat is alive so remove it from the map.
2070b57cec5SDimitry Andric     ComdatEntriesCovered.erase(CI);
2080b57cec5SDimitry Andric   };
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   auto CheckAllComdats = [&] {
2110b57cec5SDimitry Andric     for (Function &F : M.functions())
2120b57cec5SDimitry Andric       if (Comdat *C = F.getComdat()) {
2130b57cec5SDimitry Andric         CheckComdat(*C);
2140b57cec5SDimitry Andric         if (ComdatEntriesCovered.empty())
2150b57cec5SDimitry Andric           return;
2160b57cec5SDimitry Andric       }
2170b57cec5SDimitry Andric     for (GlobalVariable &GV : M.globals())
2180b57cec5SDimitry Andric       if (Comdat *C = GV.getComdat()) {
2190b57cec5SDimitry Andric         CheckComdat(*C);
2200b57cec5SDimitry Andric         if (ComdatEntriesCovered.empty())
2210b57cec5SDimitry Andric           return;
2220b57cec5SDimitry Andric       }
2230b57cec5SDimitry Andric     for (GlobalAlias &GA : M.aliases())
2240b57cec5SDimitry Andric       if (Comdat *C = GA.getComdat()) {
2250b57cec5SDimitry Andric         CheckComdat(*C);
2260b57cec5SDimitry Andric         if (ComdatEntriesCovered.empty())
2270b57cec5SDimitry Andric           return;
2280b57cec5SDimitry Andric       }
2290b57cec5SDimitry Andric   };
2300b57cec5SDimitry Andric   CheckAllComdats();
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric   if (ComdatEntriesCovered.empty()) {
2330b57cec5SDimitry Andric     DeadComdatFunctions.clear();
2340b57cec5SDimitry Andric     return;
2350b57cec5SDimitry Andric   }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   // Remove the entries that were not covering.
2380b57cec5SDimitry Andric   erase_if(DeadComdatFunctions, [&](GlobalValue *GV) {
2390b57cec5SDimitry Andric     return ComdatEntriesCovered.find(GV->getComdat()) ==
2400b57cec5SDimitry Andric            ComdatEntriesCovered.end();
2410b57cec5SDimitry Andric   });
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric 
getUniqueModuleId(Module * M)2440b57cec5SDimitry Andric std::string llvm::getUniqueModuleId(Module *M) {
2450b57cec5SDimitry Andric   MD5 Md5;
2460b57cec5SDimitry Andric   bool ExportsSymbols = false;
2470b57cec5SDimitry Andric   auto AddGlobal = [&](GlobalValue &GV) {
2480b57cec5SDimitry Andric     if (GV.isDeclaration() || GV.getName().startswith("llvm.") ||
2490b57cec5SDimitry Andric         !GV.hasExternalLinkage() || GV.hasComdat())
2500b57cec5SDimitry Andric       return;
2510b57cec5SDimitry Andric     ExportsSymbols = true;
2520b57cec5SDimitry Andric     Md5.update(GV.getName());
2530b57cec5SDimitry Andric     Md5.update(ArrayRef<uint8_t>{0});
2540b57cec5SDimitry Andric   };
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric   for (auto &F : *M)
2570b57cec5SDimitry Andric     AddGlobal(F);
2580b57cec5SDimitry Andric   for (auto &GV : M->globals())
2590b57cec5SDimitry Andric     AddGlobal(GV);
2600b57cec5SDimitry Andric   for (auto &GA : M->aliases())
2610b57cec5SDimitry Andric     AddGlobal(GA);
2620b57cec5SDimitry Andric   for (auto &IF : M->ifuncs())
2630b57cec5SDimitry Andric     AddGlobal(IF);
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric   if (!ExportsSymbols)
2660b57cec5SDimitry Andric     return "";
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   MD5::MD5Result R;
2690b57cec5SDimitry Andric   Md5.final(R);
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   SmallString<32> Str;
2720b57cec5SDimitry Andric   MD5::stringifyResult(R, Str);
273*5f7ddb14SDimitry Andric   return ("." + Str).str();
2740b57cec5SDimitry Andric }
275480093f4SDimitry Andric 
setVectorVariantNames(CallInst * CI,const SmallVector<std::string,8> & VariantMappings)276480093f4SDimitry Andric void VFABI::setVectorVariantNames(
277480093f4SDimitry Andric     CallInst *CI, const SmallVector<std::string, 8> &VariantMappings) {
278480093f4SDimitry Andric   if (VariantMappings.empty())
279480093f4SDimitry Andric     return;
280480093f4SDimitry Andric 
281480093f4SDimitry Andric   SmallString<256> Buffer;
282480093f4SDimitry Andric   llvm::raw_svector_ostream Out(Buffer);
283480093f4SDimitry Andric   for (const std::string &VariantMapping : VariantMappings)
284480093f4SDimitry Andric     Out << VariantMapping << ",";
285480093f4SDimitry Andric   // Get rid of the trailing ','.
286480093f4SDimitry Andric   assert(!Buffer.str().empty() && "Must have at least one char.");
287480093f4SDimitry Andric   Buffer.pop_back();
288480093f4SDimitry Andric 
289480093f4SDimitry Andric   Module *M = CI->getModule();
290480093f4SDimitry Andric #ifndef NDEBUG
291480093f4SDimitry Andric   for (const std::string &VariantMapping : VariantMappings) {
2925ffd83dbSDimitry Andric     LLVM_DEBUG(dbgs() << "VFABI: adding mapping '" << VariantMapping << "'\n");
2935ffd83dbSDimitry Andric     Optional<VFInfo> VI = VFABI::tryDemangleForVFABI(VariantMapping, *M);
2945ffd83dbSDimitry Andric     assert(VI.hasValue() && "Cannot add an invalid VFABI name.");
295480093f4SDimitry Andric     assert(M->getNamedValue(VI.getValue().VectorName) &&
296480093f4SDimitry Andric            "Cannot add variant to attribute: "
297480093f4SDimitry Andric            "vector function declaration is missing.");
298480093f4SDimitry Andric   }
299480093f4SDimitry Andric #endif
300480093f4SDimitry Andric   CI->addAttribute(
301480093f4SDimitry Andric       AttributeList::FunctionIndex,
302480093f4SDimitry Andric       Attribute::get(M->getContext(), MappingsAttrName, Buffer.str()));
303480093f4SDimitry Andric }
304