ModuleUtils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. auto *CA = cast<ConstantArray>(GV->getInitializer());
  69. for (auto &Op : CA->operands()) {
  70. Constant *C = cast_or_null<Constant>(Op);
  71. if (InitAsSet.insert(C).second)
  72. Init.push_back(C);
  73. }
  74. GV->eraseFromParent();
  75. }
  76. Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
  77. for (auto *V : Values) {
  78. Constant *C = ConstantExpr::getBitCast(V, Int8PtrTy);
  79. if (InitAsSet.insert(C).second)
  80. Init.push_back(C);
  81. }
  82. if (Init.empty())
  83. return;
  84. ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
  85. GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
  86. ConstantArray::get(ATy, Init), Name);
  87. GV->setSection("llvm.metadata");
  88. }
  89. void llvm::appendToUsed(Module &M, ArrayRef<GlobalValue *> Values) {
  90. appendToUsedList(M, "llvm.used", Values);
  91. }
  92. void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
  93. appendToUsedList(M, "llvm.compiler.used", Values);
  94. }
  95. FunctionCallee
  96. llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
  97. ArrayRef<Type *> InitArgTypes) {
  98. assert(!InitName.empty() && "Expected init function name");
  99. return M.getOrInsertFunction(
  100. InitName,
  101. FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
  102. AttributeList());
  103. }
  104. Function *llvm::createSanitizerCtor(Module &M, StringRef CtorName) {
  105. Function *Ctor = Function::Create(
  106. FunctionType::get(Type::getVoidTy(M.getContext()), false),
  107. GlobalValue::InternalLinkage, CtorName, &M);
  108. BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
  109. ReturnInst::Create(M.getContext(), CtorBB);
  110. return Ctor;
  111. }
  112. std::pair<Function *, FunctionCallee> llvm::createSanitizerCtorAndInitFunctions(
  113. Module &M, StringRef CtorName, StringRef InitName,
  114. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  115. StringRef VersionCheckName) {
  116. assert(!InitName.empty() && "Expected init function name");
  117. assert(InitArgs.size() == InitArgTypes.size() &&
  118. "Sanitizer's init function expects different number of arguments");
  119. FunctionCallee InitFunction =
  120. declareSanitizerInitFunction(M, InitName, InitArgTypes);
  121. Function *Ctor = createSanitizerCtor(M, CtorName);
  122. IRBuilder<> IRB(Ctor->getEntryBlock().getTerminator());
  123. IRB.CreateCall(InitFunction, InitArgs);
  124. if (!VersionCheckName.empty()) {
  125. FunctionCallee VersionCheckFunction = M.getOrInsertFunction(
  126. VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
  127. AttributeList());
  128. IRB.CreateCall(VersionCheckFunction, {});
  129. }
  130. return std::make_pair(Ctor, InitFunction);
  131. }
  132. std::pair<Function *, FunctionCallee>
  133. llvm::getOrCreateSanitizerCtorAndInitFunctions(
  134. Module &M, StringRef CtorName, StringRef InitName,
  135. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  136. function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
  137. StringRef VersionCheckName) {
  138. assert(!CtorName.empty() && "Expected ctor function name");
  139. if (Function *Ctor = M.getFunction(CtorName))
  140. // FIXME: Sink this logic into the module, similar to the handling of
  141. // globals. This will make moving to a concurrent model much easier.
  142. if (Ctor->arg_size() == 0 ||
  143. Ctor->getReturnType() == Type::getVoidTy(M.getContext()))
  144. return {Ctor, declareSanitizerInitFunction(M, InitName, InitArgTypes)};
  145. Function *Ctor;
  146. FunctionCallee InitFunction;
  147. std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions(
  148. M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName);
  149. FunctionsCreatedCallback(Ctor, InitFunction);
  150. return std::make_pair(Ctor, InitFunction);
  151. }
  152. Function *llvm::getOrCreateInitFunction(Module &M, StringRef Name) {
  153. assert(!Name.empty() && "Expected init function name");
  154. if (Function *F = M.getFunction(Name)) {
  155. if (F->arg_size() != 0 ||
  156. F->getReturnType() != Type::getVoidTy(M.getContext())) {
  157. std::string Err;
  158. raw_string_ostream Stream(Err);
  159. Stream << "Sanitizer interface function defined with wrong type: " << *F;
  160. report_fatal_error(Err);
  161. }
  162. return F;
  163. }
  164. Function *F =
  165. cast<Function>(M.getOrInsertFunction(Name, AttributeList(),
  166. Type::getVoidTy(M.getContext()))
  167. .getCallee());
  168. appendToGlobalCtors(M, F, 0);
  169. return F;
  170. }
  171. void llvm::filterDeadComdatFunctions(
  172. Module &M, SmallVectorImpl<Function *> &DeadComdatFunctions) {
  173. // Build a map from the comdat to the number of entries in that comdat we
  174. // think are dead. If this fully covers the comdat group, then the entire
  175. // group is dead. If we find another entry in the comdat group though, we'll
  176. // have to preserve the whole group.
  177. SmallDenseMap<Comdat *, int, 16> ComdatEntriesCovered;
  178. for (Function *F : DeadComdatFunctions) {
  179. Comdat *C = F->getComdat();
  180. assert(C && "Expected all input GVs to be in a comdat!");
  181. ComdatEntriesCovered[C] += 1;
  182. }
  183. auto CheckComdat = [&](Comdat &C) {
  184. auto CI = ComdatEntriesCovered.find(&C);
  185. if (CI == ComdatEntriesCovered.end())
  186. return;
  187. // If this could have been covered by a dead entry, just subtract one to
  188. // account for it.
  189. if (CI->second > 0) {
  190. CI->second -= 1;
  191. return;
  192. }
  193. // If we've already accounted for all the entries that were dead, the
  194. // entire comdat is alive so remove it from the map.
  195. ComdatEntriesCovered.erase(CI);
  196. };
  197. auto CheckAllComdats = [&] {
  198. for (Function &F : M.functions())
  199. if (Comdat *C = F.getComdat()) {
  200. CheckComdat(*C);
  201. if (ComdatEntriesCovered.empty())
  202. return;
  203. }
  204. for (GlobalVariable &GV : M.globals())
  205. if (Comdat *C = GV.getComdat()) {
  206. CheckComdat(*C);
  207. if (ComdatEntriesCovered.empty())
  208. return;
  209. }
  210. for (GlobalAlias &GA : M.aliases())
  211. if (Comdat *C = GA.getComdat()) {
  212. CheckComdat(*C);
  213. if (ComdatEntriesCovered.empty())
  214. return;
  215. }
  216. };
  217. CheckAllComdats();
  218. if (ComdatEntriesCovered.empty()) {
  219. DeadComdatFunctions.clear();
  220. return;
  221. }
  222. // Remove the entries that were not covering.
  223. erase_if(DeadComdatFunctions, [&](GlobalValue *GV) {
  224. return ComdatEntriesCovered.find(GV->getComdat()) ==
  225. ComdatEntriesCovered.end();
  226. });
  227. }
  228. std::string llvm::getUniqueModuleId(Module *M) {
  229. MD5 Md5;
  230. bool ExportsSymbols = false;
  231. auto AddGlobal = [&](GlobalValue &GV) {
  232. if (GV.isDeclaration() || GV.getName().startswith("llvm.") ||
  233. !GV.hasExternalLinkage() || GV.hasComdat())
  234. return;
  235. ExportsSymbols = true;
  236. Md5.update(GV.getName());
  237. Md5.update(ArrayRef<uint8_t>{0});
  238. };
  239. for (auto &F : *M)
  240. AddGlobal(F);
  241. for (auto &GV : M->globals())
  242. AddGlobal(GV);
  243. for (auto &GA : M->aliases())
  244. AddGlobal(GA);
  245. for (auto &IF : M->ifuncs())
  246. AddGlobal(IF);
  247. if (!ExportsSymbols)
  248. return "";
  249. MD5::MD5Result R;
  250. Md5.final(R);
  251. SmallString<32> Str;
  252. MD5::stringifyResult(R, Str);
  253. return ("$" + Str).str();
  254. }
  255. void VFABI::setVectorVariantNames(
  256. CallInst *CI, const SmallVector<std::string, 8> &VariantMappings) {
  257. if (VariantMappings.empty())
  258. return;
  259. SmallString<256> Buffer;
  260. llvm::raw_svector_ostream Out(Buffer);
  261. for (const std::string &VariantMapping : VariantMappings)
  262. Out << VariantMapping << ",";
  263. // Get rid of the trailing ','.
  264. assert(!Buffer.str().empty() && "Must have at least one char.");
  265. Buffer.pop_back();
  266. Module *M = CI->getModule();
  267. #ifndef NDEBUG
  268. for (const std::string &VariantMapping : VariantMappings) {
  269. LLVM_DEBUG(dbgs() << "VFABI: adding mapping '" << VariantMapping << "'\n");
  270. Optional<VFInfo> VI = VFABI::tryDemangleForVFABI(VariantMapping, *M);
  271. assert(VI.hasValue() && "Cannot add an invalid VFABI name.");
  272. assert(M->getNamedValue(VI.getValue().VectorName) &&
  273. "Cannot add variant to attribute: "
  274. "vector function declaration is missing.");
  275. }
  276. #endif
  277. CI->addAttribute(
  278. AttributeList::FunctionIndex,
  279. Attribute::get(M->getContext(), MappingsAttrName, Buffer.str()));
  280. }