LLVM  9.0.0svn
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 
14 #include "llvm/IR/DerivedTypes.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/IRBuilder.h"
17 #include "llvm/IR/Module.h"
19 
20 using namespace llvm;
21 
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 
27  // Get the current set of static global constructors and add the new ctor
28  // to the list.
29  SmallVector<Constant *, 16> CurrentCtors;
30  StructType *EltTy = StructType::get(
31  IRB.getInt32Ty(), PointerType::getUnqual(FnTy), IRB.getInt8PtrTy());
32  if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
33  if (Constant *Init = GVCtor->getInitializer()) {
34  unsigned n = Init->getNumOperands();
35  CurrentCtors.reserve(n + 1);
36  for (unsigned i = 0; i != n; ++i)
37  CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
38  }
39  GVCtor->eraseFromParent();
40  }
41 
42  // Build a 3 field global_ctor entry. We don't take a comdat key.
43  Constant *CSVals[3];
44  CSVals[0] = IRB.getInt32(Priority);
45  CSVals[1] = F;
46  CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getInt8PtrTy())
47  : Constant::getNullValue(IRB.getInt8PtrTy());
48  Constant *RuntimeCtorInit =
49  ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
50 
51  CurrentCtors.push_back(RuntimeCtorInit);
52 
53  // Create a new initializer.
54  ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
55  Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
56 
57  // Create the new global variable and replace all uses of
58  // the old global variable with the new one.
59  (void)new GlobalVariable(M, NewInit->getType(), false,
60  GlobalValue::AppendingLinkage, NewInit, Array);
61 }
62 
64  appendToGlobalArray("llvm.global_ctors", M, F, Priority, Data);
65 }
66 
68  appendToGlobalArray("llvm.global_dtors", M, F, Priority, Data);
69 }
70 
72  GlobalVariable *GV = M.getGlobalVariable(Name);
75  if (GV) {
77  for (auto &Op : CA->operands()) {
78  Constant *C = cast_or_null<Constant>(Op);
79  if (InitAsSet.insert(C).second)
80  Init.push_back(C);
81  }
82  GV->eraseFromParent();
83  }
84 
85  Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
86  for (auto *V : Values) {
87  Constant *C = ConstantExpr::getBitCast(V, Int8PtrTy);
88  if (InitAsSet.insert(C).second)
89  Init.push_back(C);
90  }
91 
92  if (Init.empty())
93  return;
94 
95  ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
97  ConstantArray::get(ATy, Init), Name);
98  GV->setSection("llvm.metadata");
99 }
100 
102  appendToUsedList(M, "llvm.used", Values);
103 }
104 
106  appendToUsedList(M, "llvm.compiler.used", Values);
107 }
108 
111  ArrayRef<Type *> InitArgTypes) {
112  assert(!InitName.empty() && "Expected init function name");
113  return M.getOrInsertFunction(
114  InitName,
115  FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
116  AttributeList());
117 }
118 
119 std::pair<Function *, FunctionCallee> llvm::createSanitizerCtorAndInitFunctions(
120  Module &M, StringRef CtorName, StringRef InitName,
121  ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
122  StringRef VersionCheckName) {
123  assert(!InitName.empty() && "Expected init function name");
124  assert(InitArgs.size() == InitArgTypes.size() &&
125  "Sanitizer's init function expects different number of arguments");
126  FunctionCallee InitFunction =
127  declareSanitizerInitFunction(M, InitName, InitArgTypes);
128  Function *Ctor = Function::Create(
130  GlobalValue::InternalLinkage, CtorName, &M);
131  BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
132  IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
133  IRB.CreateCall(InitFunction, InitArgs);
134  if (!VersionCheckName.empty()) {
135  FunctionCallee VersionCheckFunction = M.getOrInsertFunction(
136  VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
137  AttributeList());
138  IRB.CreateCall(VersionCheckFunction, {});
139  }
140  return std::make_pair(Ctor, InitFunction);
141 }
142 
143 std::pair<Function *, FunctionCallee>
145  Module &M, StringRef CtorName, StringRef InitName,
146  ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
147  function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
148  StringRef VersionCheckName) {
149  assert(!CtorName.empty() && "Expected ctor function name");
150 
151  if (Function *Ctor = M.getFunction(CtorName))
152  // FIXME: Sink this logic into the module, similar to the handling of
153  // globals. This will make moving to a concurrent model much easier.
154  if (Ctor->arg_size() == 0 ||
155  Ctor->getReturnType() == Type::getVoidTy(M.getContext()))
156  return {Ctor, declareSanitizerInitFunction(M, InitName, InitArgTypes)};
157 
158  Function *Ctor;
159  FunctionCallee InitFunction;
160  std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions(
161  M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName);
162  FunctionsCreatedCallback(Ctor, InitFunction);
163  return std::make_pair(Ctor, InitFunction);
164 }
165 
167  assert(!Name.empty() && "Expected init function name");
168  if (Function *F = M.getFunction(Name)) {
169  if (F->arg_size() != 0 ||
170  F->getReturnType() != Type::getVoidTy(M.getContext())) {
171  std::string Err;
172  raw_string_ostream Stream(Err);
173  Stream << "Sanitizer interface function defined with wrong type: " << *F;
174  report_fatal_error(Err);
175  }
176  return F;
177  }
178  Function *F =
179  cast<Function>(M.getOrInsertFunction(Name, AttributeList(),
181  .getCallee());
182 
183  appendToGlobalCtors(M, F, 0);
184 
185  return F;
186 }
187 
189  Module &M, SmallVectorImpl<Function *> &DeadComdatFunctions) {
190  // Build a map from the comdat to the number of entries in that comdat we
191  // think are dead. If this fully covers the comdat group, then the entire
192  // group is dead. If we find another entry in the comdat group though, we'll
193  // have to preserve the whole group.
194  SmallDenseMap<Comdat *, int, 16> ComdatEntriesCovered;
195  for (Function *F : DeadComdatFunctions) {
196  Comdat *C = F->getComdat();
197  assert(C && "Expected all input GVs to be in a comdat!");
198  ComdatEntriesCovered[C] += 1;
199  }
200 
201  auto CheckComdat = [&](Comdat &C) {
202  auto CI = ComdatEntriesCovered.find(&C);
203  if (CI == ComdatEntriesCovered.end())
204  return;
205 
206  // If this could have been covered by a dead entry, just subtract one to
207  // account for it.
208  if (CI->second > 0) {
209  CI->second -= 1;
210  return;
211  }
212 
213  // If we've already accounted for all the entries that were dead, the
214  // entire comdat is alive so remove it from the map.
215  ComdatEntriesCovered.erase(CI);
216  };
217 
218  auto CheckAllComdats = [&] {
219  for (Function &F : M.functions())
220  if (Comdat *C = F.getComdat()) {
221  CheckComdat(*C);
222  if (ComdatEntriesCovered.empty())
223  return;
224  }
225  for (GlobalVariable &GV : M.globals())
226  if (Comdat *C = GV.getComdat()) {
227  CheckComdat(*C);
228  if (ComdatEntriesCovered.empty())
229  return;
230  }
231  for (GlobalAlias &GA : M.aliases())
232  if (Comdat *C = GA.getComdat()) {
233  CheckComdat(*C);
234  if (ComdatEntriesCovered.empty())
235  return;
236  }
237  };
238  CheckAllComdats();
239 
240  if (ComdatEntriesCovered.empty()) {
241  DeadComdatFunctions.clear();
242  return;
243  }
244 
245  // Remove the entries that were not covering.
246  erase_if(DeadComdatFunctions, [&](GlobalValue *GV) {
247  return ComdatEntriesCovered.find(GV->getComdat()) ==
248  ComdatEntriesCovered.end();
249  });
250 }
251 
253  MD5 Md5;
254  bool ExportsSymbols = false;
255  auto AddGlobal = [&](GlobalValue &GV) {
256  if (GV.isDeclaration() || GV.getName().startswith("llvm.") ||
257  !GV.hasExternalLinkage() || GV.hasComdat())
258  return;
259  ExportsSymbols = true;
260  Md5.update(GV.getName());
261  Md5.update(ArrayRef<uint8_t>{0});
262  };
263 
264  for (auto &F : *M)
265  AddGlobal(F);
266  for (auto &GV : M->globals())
267  AddGlobal(GV);
268  for (auto &GA : M->aliases())
269  AddGlobal(GA);
270  for (auto &IF : M->ifuncs())
271  AddGlobal(IF);
272 
273  if (!ExportsSymbols)
274  return "";
275 
276  MD5::MD5Result R;
277  Md5.final(R);
278 
279  SmallString<32> Str;
280  MD5::stringifyResult(R, Str);
281  return ("$" + Str).str();
282 }
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:67
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:139
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:65
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:164
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:344
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:116
const GlobalVariable * getNamedGlobal(StringRef Name) const
Return the global variable in the module with the specified name, of arbitrary type.
Definition: Module.h:405
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
Definition: Module.h:390
F(f)
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:992
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:274
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:450
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:232
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:742
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
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:341
Class to represent function types.
Definition: DerivedTypes.h:102
void appendToUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.used list.
Class to represent array types.
Definition: DerivedTypes.h:400
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.
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:1782
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:609
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:45
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
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:22
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:358
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:160
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:296
static Constant * get(StructType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:1053
op_range operands()
Definition: User.h:237
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:71
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:219
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1596
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
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:513
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
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:63
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:1384
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:174
const Comdat * getComdat() const
Definition: Globals.cpp:170
ConstantArray - Constant Array Declarations.
Definition: Constants.h:413
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
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:580
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:482
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:587
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
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.
iterator_range< alias_iterator > aliases()
Definition: Module.h:627