LLVM  8.0.1
ModuleUtils.cpp
Go to the documentation of this file.
1 //===-- ModuleUtils.cpp - Functions to manipulate Modules -----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This family of functions perform manipulations on Modules.
11 //
12 //===----------------------------------------------------------------------===//
13 
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;
32  if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
33  ArrayType *ATy = cast<ArrayType>(GVCtor->getValueType());
34  StructType *OldEltTy = cast<StructType>(ATy->getElementType());
35  // Upgrade a 2-field global array type to the new 3-field format if needed.
36  if (Data && OldEltTy->getNumElements() < 3)
37  EltTy = StructType::get(IRB.getInt32Ty(), PointerType::getUnqual(FnTy),
38  IRB.getInt8PtrTy());
39  else
40  EltTy = OldEltTy;
41  if (Constant *Init = GVCtor->getInitializer()) {
42  unsigned n = Init->getNumOperands();
43  CurrentCtors.reserve(n + 1);
44  for (unsigned i = 0; i != n; ++i) {
45  auto Ctor = cast<Constant>(Init->getOperand(i));
46  if (EltTy != OldEltTy)
47  Ctor =
48  ConstantStruct::get(EltTy, Ctor->getAggregateElement((unsigned)0),
49  Ctor->getAggregateElement(1),
50  Constant::getNullValue(IRB.getInt8PtrTy()));
51  CurrentCtors.push_back(Ctor);
52  }
53  }
54  GVCtor->eraseFromParent();
55  } else {
56  // Use the new three-field struct if there isn't one already.
57  EltTy = StructType::get(IRB.getInt32Ty(), PointerType::getUnqual(FnTy),
58  IRB.getInt8PtrTy());
59  }
60 
61  // Build a 2 or 3 field global_ctor entry. We don't take a comdat key.
62  Constant *CSVals[3];
63  CSVals[0] = IRB.getInt32(Priority);
64  CSVals[1] = F;
65  // FIXME: Drop support for the two element form in LLVM 4.0.
66  if (EltTy->getNumElements() >= 3)
67  CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getInt8PtrTy())
68  : Constant::getNullValue(IRB.getInt8PtrTy());
69  Constant *RuntimeCtorInit =
70  ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
71 
72  CurrentCtors.push_back(RuntimeCtorInit);
73 
74  // Create a new initializer.
75  ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
76  Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
77 
78  // Create the new global variable and replace all uses of
79  // the old global variable with the new one.
80  (void)new GlobalVariable(M, NewInit->getType(), false,
81  GlobalValue::AppendingLinkage, NewInit, Array);
82 }
83 
85  appendToGlobalArray("llvm.global_ctors", M, F, Priority, Data);
86 }
87 
89  appendToGlobalArray("llvm.global_dtors", M, F, Priority, Data);
90 }
91 
93  GlobalVariable *GV = M.getGlobalVariable(Name);
96  if (GV) {
98  for (auto &Op : CA->operands()) {
99  Constant *C = cast_or_null<Constant>(Op);
100  if (InitAsSet.insert(C).second)
101  Init.push_back(C);
102  }
103  GV->eraseFromParent();
104  }
105 
106  Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
107  for (auto *V : Values) {
108  Constant *C = ConstantExpr::getBitCast(V, Int8PtrTy);
109  if (InitAsSet.insert(C).second)
110  Init.push_back(C);
111  }
112 
113  if (Init.empty())
114  return;
115 
116  ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
117  GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
118  ConstantArray::get(ATy, Init), Name);
119  GV->setSection("llvm.metadata");
120 }
121 
123  appendToUsedList(M, "llvm.used", Values);
124 }
125 
127  appendToUsedList(M, "llvm.compiler.used", Values);
128 }
129 
131  if (isa<Function>(FuncOrBitcast))
132  return cast<Function>(FuncOrBitcast);
133  FuncOrBitcast->print(errs());
134  errs() << '\n';
135  std::string Err;
136  raw_string_ostream Stream(Err);
137  Stream << "Sanitizer interface function redefined: " << *FuncOrBitcast;
138  report_fatal_error(Err);
139 }
140 
142  ArrayRef<Type *> InitArgTypes) {
143  assert(!InitName.empty() && "Expected init function name");
145  InitName,
146  FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
147  AttributeList()));
148  F->setLinkage(Function::ExternalLinkage);
149  return F;
150 }
151 
152 std::pair<Function *, Function *> llvm::createSanitizerCtorAndInitFunctions(
153  Module &M, StringRef CtorName, StringRef InitName,
154  ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
155  StringRef VersionCheckName) {
156  assert(!InitName.empty() && "Expected init function name");
157  assert(InitArgs.size() == InitArgTypes.size() &&
158  "Sanitizer's init function expects different number of arguments");
159  Function *InitFunction =
160  declareSanitizerInitFunction(M, InitName, InitArgTypes);
161  Function *Ctor = Function::Create(
163  GlobalValue::InternalLinkage, CtorName, &M);
164  BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
165  IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
166  IRB.CreateCall(InitFunction, InitArgs);
167  if (!VersionCheckName.empty()) {
168  Function *VersionCheckFunction =
170  VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
171  AttributeList()));
172  IRB.CreateCall(VersionCheckFunction, {});
173  }
174  return std::make_pair(Ctor, InitFunction);
175 }
176 
177 std::pair<Function *, Function *>
179  Module &M, StringRef CtorName, StringRef InitName,
180  ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
181  function_ref<void(Function *, Function *)> FunctionsCreatedCallback,
182  StringRef VersionCheckName) {
183  assert(!CtorName.empty() && "Expected ctor function name");
184 
185  if (Function *Ctor = M.getFunction(CtorName))
186  // FIXME: Sink this logic into the module, similar to the handling of
187  // globals. This will make moving to a concurrent model much easier.
188  if (Ctor->arg_size() == 0 ||
189  Ctor->getReturnType() == Type::getVoidTy(M.getContext()))
190  return {Ctor, declareSanitizerInitFunction(M, InitName, InitArgTypes)};
191 
192  Function *Ctor, *InitFunction;
193  std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions(
194  M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName);
195  FunctionsCreatedCallback(Ctor, InitFunction);
196  return std::make_pair(Ctor, InitFunction);
197 }
198 
200  assert(!Name.empty() && "Expected init function name");
201  if (Function *F = M.getFunction(Name)) {
202  if (F->arg_size() != 0 ||
203  F->getReturnType() != Type::getVoidTy(M.getContext())) {
204  std::string Err;
205  raw_string_ostream Stream(Err);
206  Stream << "Sanitizer interface function defined with wrong type: " << *F;
207  report_fatal_error(Err);
208  }
209  return F;
210  }
212  Name, AttributeList(), Type::getVoidTy(M.getContext())));
214 
215  appendToGlobalCtors(M, F, 0);
216 
217  return F;
218 }
219 
221  Module &M, SmallVectorImpl<Function *> &DeadComdatFunctions) {
222  // Build a map from the comdat to the number of entries in that comdat we
223  // think are dead. If this fully covers the comdat group, then the entire
224  // group is dead. If we find another entry in the comdat group though, we'll
225  // have to preserve the whole group.
226  SmallDenseMap<Comdat *, int, 16> ComdatEntriesCovered;
227  for (Function *F : DeadComdatFunctions) {
228  Comdat *C = F->getComdat();
229  assert(C && "Expected all input GVs to be in a comdat!");
230  ComdatEntriesCovered[C] += 1;
231  }
232 
233  auto CheckComdat = [&](Comdat &C) {
234  auto CI = ComdatEntriesCovered.find(&C);
235  if (CI == ComdatEntriesCovered.end())
236  return;
237 
238  // If this could have been covered by a dead entry, just subtract one to
239  // account for it.
240  if (CI->second > 0) {
241  CI->second -= 1;
242  return;
243  }
244 
245  // If we've already accounted for all the entries that were dead, the
246  // entire comdat is alive so remove it from the map.
247  ComdatEntriesCovered.erase(CI);
248  };
249 
250  auto CheckAllComdats = [&] {
251  for (Function &F : M.functions())
252  if (Comdat *C = F.getComdat()) {
253  CheckComdat(*C);
254  if (ComdatEntriesCovered.empty())
255  return;
256  }
257  for (GlobalVariable &GV : M.globals())
258  if (Comdat *C = GV.getComdat()) {
259  CheckComdat(*C);
260  if (ComdatEntriesCovered.empty())
261  return;
262  }
263  for (GlobalAlias &GA : M.aliases())
264  if (Comdat *C = GA.getComdat()) {
265  CheckComdat(*C);
266  if (ComdatEntriesCovered.empty())
267  return;
268  }
269  };
270  CheckAllComdats();
271 
272  if (ComdatEntriesCovered.empty()) {
273  DeadComdatFunctions.clear();
274  return;
275  }
276 
277  // Remove the entries that were not covering.
278  erase_if(DeadComdatFunctions, [&](GlobalValue *GV) {
279  return ComdatEntriesCovered.find(GV->getComdat()) ==
280  ComdatEntriesCovered.end();
281  });
282 }
283 
285  MD5 Md5;
286  bool ExportsSymbols = false;
287  auto AddGlobal = [&](GlobalValue &GV) {
288  if (GV.isDeclaration() || GV.getName().startswith("llvm.") ||
289  !GV.hasExternalLinkage() || GV.hasComdat())
290  return;
291  ExportsSymbols = true;
292  Md5.update(GV.getName());
293  Md5.update(ArrayRef<uint8_t>{0});
294  };
295 
296  for (auto &F : *M)
297  AddGlobal(F);
298  for (auto &GV : M->globals())
299  AddGlobal(GV);
300  for (auto &GA : M->aliases())
301  AddGlobal(GA);
302  for (auto &IF : M->ifuncs())
303  AddGlobal(IF);
304 
305  if (!ExportsSymbols)
306  return "";
307 
308  MD5::MD5Result R;
309  Md5.final(R);
310 
311  SmallString<32> Str;
312  MD5::stringifyResult(R, Str);
313  return ("$" + Str).str();
314 }
uint64_t CallInst * C
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.compiler.used list.
Function * declareSanitizerInitFunction(Module &M, StringRef InitName, ArrayRef< Type *> InitArgTypes)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Same as appendToGlobalCtors(), but for global dtors.
Definition: ModuleUtils.cpp:88
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:55
Function * checkSanitizerInterfaceFunction(Constant *FuncOrBitcast)
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:24
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:144
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
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:313
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:117
const GlobalVariable * getNamedGlobal(StringRef Name) const
Return the global variable in the module with the specified name, of arbitrary type.
Definition: Module.h:402
Externally visible function.
Definition: GlobalValue.h:49
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
Definition: Module.h:387
F(f)
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:983
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:265
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, Instruction *InsertBefore=nullptr)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
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:201
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:42
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
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:342
Class to represent function types.
Definition: DerivedTypes.h:103
void appendToUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.used list.
Class to represent array types.
Definition: DerivedTypes.h:369
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1773
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:136
iterator_range< iterator > functions()
Definition: Module.h:606
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
std::pair< Function *, Function * > 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.
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
This is an important base class in LLVM.
Definition: Constant.h:42
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:371
void eraseFromParent()
eraseFromParent - This method unlinks &#39;this&#39; from the containing module and deletes it...
Definition: Globals.cpp:359
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:161
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:4148
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:297
static Constant * get(StructType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:1044
op_range operands()
Definition: User.h:238
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
static void appendToUsedList(Module &M, StringRef Name, ArrayRef< GlobalValue *> Values)
Definition: ModuleUtils.cpp:92
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:53
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:220
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1587
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
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:482
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
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:84
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:445
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:1330
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:176
const Comdat * getComdat() const
Definition: Globals.cpp:171
ConstantArray - Constant Array Declarations.
Definition: Constants.h:414
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:56
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:581
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:323
Rename collisions when linking (static functions).
Definition: GlobalValue.h:56
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:483
Type * getElementType() const
Definition: DerivedTypes.h:360
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:584
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
std::pair< Function *, Function * > getOrCreateSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type *> InitArgTypes, ArrayRef< Value *> InitArgs, function_ref< void(Function *, Function *)> FunctionsCreatedCallback, StringRef VersionCheckName=StringRef())
Creates sanitizer constructor function lazily.
iterator_range< alias_iterator > aliases()
Definition: Module.h:624