LLVM  10.0.0
ModuleUtils.cpp
Go to the documentation of this file.
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 
15 #include "llvm/IR/DerivedTypes.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/IRBuilder.h"
18 #include "llvm/IR/Module.h"
20 
21 using namespace llvm;
22 
23 static void appendToGlobalArray(const char *Array, Module &M, Function *F,
24  int Priority, Constant *Data) {
25  IRBuilder<> IRB(M.getContext());
26  FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
27 
28  // Get the current set of static global constructors and add the new ctor
29  // to the list.
30  SmallVector<Constant *, 16> CurrentCtors;
31  StructType *EltTy = StructType::get(
32  IRB.getInt32Ty(), PointerType::getUnqual(FnTy), IRB.getInt8PtrTy());
33  if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
34  if (Constant *Init = GVCtor->getInitializer()) {
35  unsigned n = Init->getNumOperands();
36  CurrentCtors.reserve(n + 1);
37  for (unsigned i = 0; i != n; ++i)
38  CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
39  }
40  GVCtor->eraseFromParent();
41  }
42 
43  // Build a 3 field global_ctor entry. We don't take a comdat key.
44  Constant *CSVals[3];
45  CSVals[0] = IRB.getInt32(Priority);
46  CSVals[1] = F;
47  CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getInt8PtrTy())
48  : Constant::getNullValue(IRB.getInt8PtrTy());
49  Constant *RuntimeCtorInit =
50  ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
51 
52  CurrentCtors.push_back(RuntimeCtorInit);
53 
54  // Create a new initializer.
55  ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
56  Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
57 
58  // Create the new global variable and replace all uses of
59  // the old global variable with the new one.
60  (void)new GlobalVariable(M, NewInit->getType(), false,
61  GlobalValue::AppendingLinkage, NewInit, Array);
62 }
63 
65  appendToGlobalArray("llvm.global_ctors", M, F, Priority, Data);
66 }
67 
69  appendToGlobalArray("llvm.global_dtors", M, F, Priority, Data);
70 }
71 
73  GlobalVariable *GV = M.getGlobalVariable(Name);
76  if (GV) {
77  auto *CA = cast<ConstantArray>(GV->getInitializer());
78  for (auto &Op : CA->operands()) {
79  Constant *C = cast_or_null<Constant>(Op);
80  if (InitAsSet.insert(C).second)
81  Init.push_back(C);
82  }
83  GV->eraseFromParent();
84  }
85 
86  Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
87  for (auto *V : Values) {
88  Constant *C = ConstantExpr::getBitCast(V, Int8PtrTy);
89  if (InitAsSet.insert(C).second)
90  Init.push_back(C);
91  }
92 
93  if (Init.empty())
94  return;
95 
96  ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
98  ConstantArray::get(ATy, Init), Name);
99  GV->setSection("llvm.metadata");
100 }
101 
103  appendToUsedList(M, "llvm.used", Values);
104 }
105 
107  appendToUsedList(M, "llvm.compiler.used", Values);
108 }
109 
112  ArrayRef<Type *> InitArgTypes) {
113  assert(!InitName.empty() && "Expected init function name");
114  return M.getOrInsertFunction(
115  InitName,
116  FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
117  AttributeList());
118 }
119 
120 std::pair<Function *, FunctionCallee> llvm::createSanitizerCtorAndInitFunctions(
121  Module &M, StringRef CtorName, StringRef InitName,
122  ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
123  StringRef VersionCheckName) {
124  assert(!InitName.empty() && "Expected init function name");
125  assert(InitArgs.size() == InitArgTypes.size() &&
126  "Sanitizer's init function expects different number of arguments");
127  FunctionCallee InitFunction =
128  declareSanitizerInitFunction(M, InitName, InitArgTypes);
129  Function *Ctor = Function::Create(
131  GlobalValue::InternalLinkage, CtorName, &M);
132  BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
133  IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
134  IRB.CreateCall(InitFunction, InitArgs);
135  if (!VersionCheckName.empty()) {
136  FunctionCallee VersionCheckFunction = M.getOrInsertFunction(
137  VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
138  AttributeList());
139  IRB.CreateCall(VersionCheckFunction, {});
140  }
141  return std::make_pair(Ctor, InitFunction);
142 }
143 
144 std::pair<Function *, FunctionCallee>
146  Module &M, StringRef CtorName, StringRef InitName,
147  ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
148  function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
149  StringRef VersionCheckName) {
150  assert(!CtorName.empty() && "Expected ctor function name");
151 
152  if (Function *Ctor = M.getFunction(CtorName))
153  // FIXME: Sink this logic into the module, similar to the handling of
154  // globals. This will make moving to a concurrent model much easier.
155  if (Ctor->arg_size() == 0 ||
156  Ctor->getReturnType() == Type::getVoidTy(M.getContext()))
157  return {Ctor, declareSanitizerInitFunction(M, InitName, InitArgTypes)};
158 
159  Function *Ctor;
160  FunctionCallee InitFunction;
161  std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions(
162  M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName);
163  FunctionsCreatedCallback(Ctor, InitFunction);
164  return std::make_pair(Ctor, InitFunction);
165 }
166 
168  assert(!Name.empty() && "Expected init function name");
169  if (Function *F = M.getFunction(Name)) {
170  if (F->arg_size() != 0 ||
171  F->getReturnType() != Type::getVoidTy(M.getContext())) {
172  std::string Err;
173  raw_string_ostream Stream(Err);
174  Stream << "Sanitizer interface function defined with wrong type: " << *F;
175  report_fatal_error(Err);
176  }
177  return F;
178  }
179  Function *F =
180  cast<Function>(M.getOrInsertFunction(Name, AttributeList(),
182  .getCallee());
183 
184  appendToGlobalCtors(M, F, 0);
185 
186  return F;
187 }
188 
190  Module &M, SmallVectorImpl<Function *> &DeadComdatFunctions) {
191  // Build a map from the comdat to the number of entries in that comdat we
192  // think are dead. If this fully covers the comdat group, then the entire
193  // group is dead. If we find another entry in the comdat group though, we'll
194  // have to preserve the whole group.
195  SmallDenseMap<Comdat *, int, 16> ComdatEntriesCovered;
196  for (Function *F : DeadComdatFunctions) {
197  Comdat *C = F->getComdat();
198  assert(C && "Expected all input GVs to be in a comdat!");
199  ComdatEntriesCovered[C] += 1;
200  }
201 
202  auto CheckComdat = [&](Comdat &C) {
203  auto CI = ComdatEntriesCovered.find(&C);
204  if (CI == ComdatEntriesCovered.end())
205  return;
206 
207  // If this could have been covered by a dead entry, just subtract one to
208  // account for it.
209  if (CI->second > 0) {
210  CI->second -= 1;
211  return;
212  }
213 
214  // If we've already accounted for all the entries that were dead, the
215  // entire comdat is alive so remove it from the map.
216  ComdatEntriesCovered.erase(CI);
217  };
218 
219  auto CheckAllComdats = [&] {
220  for (Function &F : M.functions())
221  if (Comdat *C = F.getComdat()) {
222  CheckComdat(*C);
223  if (ComdatEntriesCovered.empty())
224  return;
225  }
226  for (GlobalVariable &GV : M.globals())
227  if (Comdat *C = GV.getComdat()) {
228  CheckComdat(*C);
229  if (ComdatEntriesCovered.empty())
230  return;
231  }
232  for (GlobalAlias &GA : M.aliases())
233  if (Comdat *C = GA.getComdat()) {
234  CheckComdat(*C);
235  if (ComdatEntriesCovered.empty())
236  return;
237  }
238  };
239  CheckAllComdats();
240 
241  if (ComdatEntriesCovered.empty()) {
242  DeadComdatFunctions.clear();
243  return;
244  }
245 
246  // Remove the entries that were not covering.
247  erase_if(DeadComdatFunctions, [&](GlobalValue *GV) {
248  return ComdatEntriesCovered.find(GV->getComdat()) ==
249  ComdatEntriesCovered.end();
250  });
251 }
252 
254  MD5 Md5;
255  bool ExportsSymbols = false;
256  auto AddGlobal = [&](GlobalValue &GV) {
257  if (GV.isDeclaration() || GV.getName().startswith("llvm.") ||
258  !GV.hasExternalLinkage() || GV.hasComdat())
259  return;
260  ExportsSymbols = true;
261  Md5.update(GV.getName());
262  Md5.update(ArrayRef<uint8_t>{0});
263  };
264 
265  for (auto &F : *M)
266  AddGlobal(F);
267  for (auto &GV : M->globals())
268  AddGlobal(GV);
269  for (auto &GA : M->aliases())
270  AddGlobal(GA);
271  for (auto &IF : M->ifuncs())
272  AddGlobal(IF);
273 
274  if (!ExportsSymbols)
275  return "";
276 
277  MD5::MD5Result R;
278  Md5.final(R);
279 
280  SmallString<32> Str;
281  MD5::stringifyResult(R, Str);
282  return ("$" + Str).str();
283 }
284 
286  CallInst *CI, const SmallVector<std::string, 8> &VariantMappings) {
287  if (VariantMappings.empty())
288  return;
289 
290  SmallString<256> Buffer;
291  llvm::raw_svector_ostream Out(Buffer);
292  for (const std::string &VariantMapping : VariantMappings)
293  Out << VariantMapping << ",";
294  // Get rid of the trailing ','.
295  assert(!Buffer.str().empty() && "Must have at least one char.");
296  Buffer.pop_back();
297 
298  Module *M = CI->getModule();
299 #ifndef NDEBUG
300  for (const std::string &VariantMapping : VariantMappings) {
302  assert(VI.hasValue() && "Canno add an invalid VFABI name.");
304  "Cannot add variant to attribute: "
305  "vector function declaration is missing.");
306  }
307 #endif
308  CI->addAttribute(
310  Attribute::get(M->getContext(), MappingsAttrName, Buffer.str()));
311 }
uint64_t CallInst * C
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.compiler.used list.
void appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Same as appendToGlobalCtors(), but for global dtors.
Definition: ModuleUtils.cpp:68
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:54
Function * getOrCreateInitFunction(Module &M, StringRef Name)
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
static void stringifyResult(MD5Result &Result, SmallString< 32 > &Str)
Translates the bytes in Res to a hex string that is deposited into Str.
Definition: MD5.cpp:272
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:350
This class represents a function call, abstracting a target machine&#39;s calling convention.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:99
const GlobalVariable * getNamedGlobal(StringRef Name) const
Return the global variable in the module with the specified name, of arbitrary type.
Definition: Module.h:406
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:543
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
Definition: Module.h:391
F(f)
void addAttribute(unsigned i, Attribute::AttrKind Kind)
adds the attribute to the list of attributes.
Definition: InstrTypes.h:1401
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:1065
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:318
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, Instruction *InsertBefore=nullptr)
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:460
void update(ArrayRef< uint8_t > Data)
Updates the hash for the byte stream provided.
Definition: MD5.cpp:189
Class to represent struct types.
Definition: DerivedTypes.h:238
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:245
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
static StructType * get(LLVMContext &Context, ArrayRef< Type *> Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition: Type.cpp:346
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:266
Class to represent function types.
Definition: DerivedTypes.h:108
void appendToUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.used list.
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:255
Class to represent array types.
Definition: DerivedTypes.h:408
static constexpr char const * MappingsAttrName
Definition: VectorUtils.h:162
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.
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type. ...
Definition: Module.cpp:113
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1872
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:135
iterator_range< iterator > functions()
Definition: Module.h:610
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:158
This is an important base class in LLVM.
Definition: Constant.h:41
static void appendToGlobalArray(const char *Array, Module &M, Function *F, int Priority, Constant *Data)
Definition: ModuleUtils.cpp:23
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
void eraseFromParent()
eraseFromParent - This method unlinks &#39;this&#39; from the containing module and deletes it...
Definition: Globals.cpp:384
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:165
Optional< VFInfo > tryDemangleForVFABI(StringRef MangledName)
Function to contruct a VFInfo out of a mangled names in the following format:
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:301
static Constant * get(StructType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:1126
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:99
static void appendToUsedList(Module &M, StringRef Name, ArrayRef< GlobalValue *> Values)
Definition: ModuleUtils.cpp:72
std::string getUniqueModuleId(Module *M)
Produce a unique identifier for this module by taking the MD5 sum of the names of the module&#39;s strong...
size_t size() const
Definition: SmallVector.h:52
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:224
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1686
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:443
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
Definition: DerivedTypes.h:594
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
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.
Definition: ModuleUtils.cpp:64
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:143
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2&#39;s erase_if which is equivalent t...
Definition: STLExtras.h:1340
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:174
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:55
const Comdat * getComdat() const
Definition: Globals.cpp:175
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...
bool hasValue() const
Definition: Optional.h:259
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
Definition: MD5.h:41
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:587
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
static Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
Definition: Attributes.cpp:80
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:516
void final(MD5Result &Result)
Finishes off the hash and puts the result in result.
Definition: MD5.cpp:234
iterator_range< global_iterator > globals()
Definition: Module.h:588
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
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&#39;s init function from it.
StringRef VectorName
Definition: VectorUtils.h:121
void setVectorVariantNames(CallInst *CI, const SmallVector< std::string, 8 > &VariantMappings)
Overwrite the Vector Function ABI variants attribute with the names provide in VariantMappings.
iterator_range< alias_iterator > aliases()
Definition: Module.h:628