ModuleUtils.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //===-- ModuleUtils.cpp - Functions to manipulate Modules -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This family of functions perform manipulations on Modules.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/Utils/ModuleUtils.h"
  13. #include "llvm/Analysis/TargetLibraryInfo.h"
  14. #include "llvm/Analysis/VectorUtils.h"
  15. #include "llvm/IR/DerivedTypes.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. #define DEBUG_TYPE "moduleutils"
  22. static void appendToGlobalArray(const char *Array, Module &M, Function *F,
  23. int Priority, Constant *Data) {
  24. IRBuilder<> IRB(M.getContext());
  25. FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
  26. // Get the current set of static global constructors and add the new ctor
  27. // to the list.
  28. SmallVector<Constant *, 16> CurrentCtors;
  29. StructType *EltTy = StructType::get(
  30. IRB.getInt32Ty(), PointerType::getUnqual(FnTy), IRB.getInt8PtrTy());
  31. if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
  32. if (Constant *Init = GVCtor->getInitializer()) {
  33. unsigned n = Init->getNumOperands();
  34. CurrentCtors.reserve(n + 1);
  35. for (unsigned i = 0; i != n; ++i)
  36. CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
  37. }
  38. GVCtor->eraseFromParent();
  39. }
  40. // Build a 3 field global_ctor entry. We don't take a comdat key.
  41. Constant *CSVals[3];
  42. CSVals[0] = IRB.getInt32(Priority);
  43. CSVals[1] = F;
  44. CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getInt8PtrTy())
  45. : Constant::getNullValue(IRB.getInt8PtrTy());
  46. Constant *RuntimeCtorInit =
  47. ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
  48. CurrentCtors.push_back(RuntimeCtorInit);
  49. // Create a new initializer.
  50. ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
  51. Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
  52. // Create the new global variable and replace all uses of
  53. // the old global variable with the new one.
  54. (void)new GlobalVariable(M, NewInit->getType(), false,
  55. GlobalValue::AppendingLinkage, NewInit, Array);
  56. }
  57. void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data) {
  58. appendToGlobalArray("llvm.global_ctors", M, F, Priority, Data);
  59. }
  60. void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data) {
  61. appendToGlobalArray("llvm.global_dtors", M, F, Priority, Data);
  62. }
  63. static void appendToUsedList(Module &M, StringRef Name, ArrayRef<GlobalValue *> Values) {
  64. GlobalVariable *GV = M.getGlobalVariable(Name);
  65. SmallPtrSet<Constant *, 16> InitAsSet;
  66. SmallVector<Constant *, 16> Init;
  67. if (GV) {
  68. if (GV->hasInitializer()) {
  69. auto *CA = cast<ConstantArray>(GV->getInitializer());
  70. for (auto &Op : CA->operands()) {
  71. Constant *C = cast_or_null<Constant>(Op);
  72. if (InitAsSet.insert(C).second)
  73. Init.push_back(C);
  74. }
  75. }
  76. GV->eraseFromParent();
  77. }
  78. Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
  79. for (auto *V : Values) {
  80. Constant *C = ConstantExpr::getPointerBitCastOrAddrSpaceCast(V, Int8PtrTy);
  81. if (InitAsSet.insert(C).second)
  82. Init.push_back(C);
  83. }
  84. if (Init.empty())
  85. return;
  86. ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
  87. GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
  88. ConstantArray::get(ATy, Init), Name);
  89. GV->setSection("llvm.metadata");
  90. }
  91. void llvm::appendToUsed(Module &M, ArrayRef<GlobalValue *> Values) {
  92. appendToUsedList(M, "llvm.used", Values);
  93. }
  94. void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
  95. appendToUsedList(M, "llvm.compiler.used", Values);
  96. }
  97. FunctionCallee
  98. llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
  99. ArrayRef<Type *> InitArgTypes) {
  100. assert(!InitName.empty() && "Expected init function name");
  101. return M.getOrInsertFunction(
  102. InitName,
  103. FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
  104. AttributeList());
  105. }
  106. Function *llvm::createSanitizerCtor(Module &M, StringRef CtorName) {
  107. Function *Ctor = Function::createWithDefaultAttr(
  108. FunctionType::get(Type::getVoidTy(M.getContext()), false),
  109. GlobalValue::InternalLinkage, 0, CtorName, &M);
  110. Ctor->addFnAttr(Attribute::NoUnwind);
  111. BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
  112. ReturnInst::Create(M.getContext(), CtorBB);
  113. // Ensure Ctor cannot be discarded, even if in a comdat.
  114. appendToUsed(M, {Ctor});
  115. return Ctor;
  116. }
  117. std::pair<Function *, FunctionCallee> llvm::createSanitizerCtorAndInitFunctions(
  118. Module &M, StringRef CtorName, StringRef InitName,
  119. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  120. StringRef VersionCheckName) {
  121. assert(!InitName.empty() && "Expected init function name");
  122. assert(InitArgs.size() == InitArgTypes.size() &&
  123. "Sanitizer's init function expects different number of arguments");
  124. FunctionCallee InitFunction =
  125. declareSanitizerInitFunction(M, InitName, InitArgTypes);
  126. Function *Ctor = createSanitizerCtor(M, CtorName);
  127. IRBuilder<> IRB(Ctor->getEntryBlock().getTerminator());
  128. IRB.CreateCall(InitFunction, InitArgs);
  129. if (!VersionCheckName.empty()) {
  130. FunctionCallee VersionCheckFunction = M.getOrInsertFunction(
  131. VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
  132. AttributeList());
  133. IRB.CreateCall(VersionCheckFunction, {});
  134. }
  135. return std::make_pair(Ctor, InitFunction);
  136. }
  137. std::pair<Function *, FunctionCallee>
  138. llvm::getOrCreateSanitizerCtorAndInitFunctions(
  139. Module &M, StringRef CtorName, StringRef InitName,
  140. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  141. function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
  142. StringRef VersionCheckName) {
  143. assert(!CtorName.empty() && "Expected ctor function name");
  144. if (Function *Ctor = M.getFunction(CtorName))
  145. // FIXME: Sink this logic into the module, similar to the handling of
  146. // globals. This will make moving to a concurrent model much easier.
  147. if (Ctor->arg_empty() ||
  148. Ctor->getReturnType() == Type::getVoidTy(M.getContext()))
  149. return {Ctor, declareSanitizerInitFunction(M, InitName, InitArgTypes)};
  150. Function *Ctor;
  151. FunctionCallee InitFunction;
  152. std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions(
  153. M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName);
  154. FunctionsCreatedCallback(Ctor, InitFunction);
  155. return std::make_pair(Ctor, InitFunction);
  156. }
  157. void llvm::filterDeadComdatFunctions(
  158. SmallVectorImpl<Function *> &DeadComdatFunctions) {
  159. SmallPtrSet<Function *, 32> MaybeDeadFunctions;
  160. SmallPtrSet<Comdat *, 32> MaybeDeadComdats;
  161. for (Function *F : DeadComdatFunctions) {
  162. MaybeDeadFunctions.insert(F);
  163. if (Comdat *C = F->getComdat())
  164. MaybeDeadComdats.insert(C);
  165. }
  166. // Find comdats for which all users are dead now.
  167. SmallPtrSet<Comdat *, 32> DeadComdats;
  168. for (Comdat *C : MaybeDeadComdats) {
  169. auto IsUserDead = [&](GlobalObject *GO) {
  170. auto *F = dyn_cast<Function>(GO);
  171. return F && MaybeDeadFunctions.contains(F);
  172. };
  173. if (all_of(C->getUsers(), IsUserDead))
  174. DeadComdats.insert(C);
  175. }
  176. // Only keep functions which have no comdat or a dead comdat.
  177. erase_if(DeadComdatFunctions, [&](Function *F) {
  178. Comdat *C = F->getComdat();
  179. return C && !DeadComdats.contains(C);
  180. });
  181. }
  182. std::string llvm::getUniqueModuleId(Module *M) {
  183. MD5 Md5;
  184. bool ExportsSymbols = false;
  185. auto AddGlobal = [&](GlobalValue &GV) {
  186. if (GV.isDeclaration() || GV.getName().startswith("llvm.") ||
  187. !GV.hasExternalLinkage() || GV.hasComdat())
  188. return;
  189. ExportsSymbols = true;
  190. Md5.update(GV.getName());
  191. Md5.update(ArrayRef<uint8_t>{0});
  192. };
  193. for (auto &F : *M)
  194. AddGlobal(F);
  195. for (auto &GV : M->globals())
  196. AddGlobal(GV);
  197. for (auto &GA : M->aliases())
  198. AddGlobal(GA);
  199. for (auto &IF : M->ifuncs())
  200. AddGlobal(IF);
  201. if (!ExportsSymbols)
  202. return "";
  203. MD5::MD5Result R;
  204. Md5.final(R);
  205. SmallString<32> Str;
  206. MD5::stringifyResult(R, Str);
  207. return ("." + Str).str();
  208. }
  209. void VFABI::setVectorVariantNames(
  210. CallInst *CI, const SmallVector<std::string, 8> &VariantMappings) {
  211. if (VariantMappings.empty())
  212. return;
  213. SmallString<256> Buffer;
  214. llvm::raw_svector_ostream Out(Buffer);
  215. for (const std::string &VariantMapping : VariantMappings)
  216. Out << VariantMapping << ",";
  217. // Get rid of the trailing ','.
  218. assert(!Buffer.str().empty() && "Must have at least one char.");
  219. Buffer.pop_back();
  220. Module *M = CI->getModule();
  221. #ifndef NDEBUG
  222. for (const std::string &VariantMapping : VariantMappings) {
  223. LLVM_DEBUG(dbgs() << "VFABI: adding mapping '" << VariantMapping << "'\n");
  224. Optional<VFInfo> VI = VFABI::tryDemangleForVFABI(VariantMapping, *M);
  225. assert(VI.hasValue() && "Cannot add an invalid VFABI name.");
  226. assert(M->getNamedValue(VI.getValue().VectorName) &&
  227. "Cannot add variant to attribute: "
  228. "vector function declaration is missing.");
  229. }
  230. #endif
  231. CI->addFnAttr(
  232. Attribute::get(M->getContext(), MappingsAttrName, Buffer.str()));
  233. }
  234. void llvm::embedBufferInModule(Module &M, MemoryBufferRef Buf,
  235. StringRef SectionName) {
  236. // Embed the buffer into the module.
  237. Constant *ModuleConstant = ConstantDataArray::get(
  238. M.getContext(), makeArrayRef(Buf.getBufferStart(), Buf.getBufferSize()));
  239. GlobalVariable *GV = new GlobalVariable(
  240. M, ModuleConstant->getType(), true, GlobalValue::PrivateLinkage,
  241. ModuleConstant, "llvm.embedded.object");
  242. GV->setSection(SectionName);
  243. appendToCompilerUsed(M, GV);
  244. }