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;
31  if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
32  ArrayType *ATy = cast<ArrayType>(GVCtor->getValueType());
33  StructType *OldEltTy = cast<StructType>(ATy->getElementType());
34  // Upgrade a 2-field global array type to the new 3-field format if needed.
35  if (Data && OldEltTy->getNumElements() < 3)
36  EltTy = StructType::get(IRB.getInt32Ty(), PointerType::getUnqual(FnTy),
37  IRB.getInt8PtrTy());
38  else
39  EltTy = OldEltTy;
40  if (Constant *Init = GVCtor->getInitializer()) {
41  unsigned n = Init->getNumOperands();
42  CurrentCtors.reserve(n + 1);
43  for (unsigned i = 0; i != n; ++i) {
44  auto Ctor = cast<Constant>(Init->getOperand(i));
45  if (EltTy != OldEltTy)
46  Ctor =
47  ConstantStruct::get(EltTy, Ctor->getAggregateElement((unsigned)0),
48  Ctor->getAggregateElement(1),
49  Constant::getNullValue(IRB.getInt8PtrTy()));
50  CurrentCtors.push_back(Ctor);
51  }
52  }
53  GVCtor->eraseFromParent();
54  } else {
55  // Use the new three-field struct if there isn't one already.
56  EltTy = StructType::get(IRB.getInt32Ty(), PointerType::getUnqual(FnTy),
57  IRB.getInt8PtrTy());
58  }
59 
60  // Build a 2 or 3 field global_ctor entry. We don't take a comdat key.
61  Constant *CSVals[3];
62  CSVals[0] = IRB.getInt32(Priority);
63  CSVals[1] = F;
64  // FIXME: Drop support for the two element form in LLVM 4.0.
65  if (EltTy->getNumElements() >= 3)
66  CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getInt8PtrTy())
67  : Constant::getNullValue(IRB.getInt8PtrTy());
68  Constant *RuntimeCtorInit =
69  ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
70 
71  CurrentCtors.push_back(RuntimeCtorInit);
72 
73  // Create a new initializer.
74  ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
75  Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
76 
77  // Create the new global variable and replace all uses of
78  // the old global variable with the new one.
79  (void)new GlobalVariable(M, NewInit->getType(), false,
80  GlobalValue::AppendingLinkage, NewInit, Array);
81 }
82 
84  appendToGlobalArray("llvm.global_ctors", M, F, Priority, Data);
85 }
86 
88  appendToGlobalArray("llvm.global_dtors", M, F, Priority, Data);
89 }
90 
92  GlobalVariable *GV = M.getGlobalVariable(Name);
95  if (GV) {
97  for (auto &Op : CA->operands()) {
98  Constant *C = cast_or_null<Constant>(Op);
99  if (InitAsSet.insert(C).second)
100  Init.push_back(C);
101  }
102  GV->eraseFromParent();
103  }
104 
105  Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
106  for (auto *V : Values) {
107  Constant *C = ConstantExpr::getBitCast(V, Int8PtrTy);
108  if (InitAsSet.insert(C).second)
109  Init.push_back(C);
110  }
111 
112  if (Init.empty())
113  return;
114 
115  ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
116  GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
117  ConstantArray::get(ATy, Init), Name);
118  GV->setSection("llvm.metadata");
119 }
120 
122  appendToUsedList(M, "llvm.used", Values);
123 }
124 
126  appendToUsedList(M, "llvm.compiler.used", Values);
127 }
128 
130  if (isa<Function>(FuncOrBitcast))
131  return cast<Function>(FuncOrBitcast);
132  FuncOrBitcast->print(errs());
133  errs() << '\n';
134  std::string Err;
135  raw_string_ostream Stream(Err);
136  Stream << "Sanitizer interface function redefined: " << *FuncOrBitcast;
137  report_fatal_error(Err);
138 }
139 
141  ArrayRef<Type *> InitArgTypes) {
142  assert(!InitName.empty() && "Expected init function name");
144  InitName,
145  FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
146  AttributeList()));
147  F->setLinkage(Function::ExternalLinkage);
148  return F;
149 }
150 
151 std::pair<Function *, Function *> llvm::createSanitizerCtorAndInitFunctions(
152  Module &M, StringRef CtorName, StringRef InitName,
153  ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
154  StringRef VersionCheckName) {
155  assert(!InitName.empty() && "Expected init function name");
156  assert(InitArgs.size() == InitArgTypes.size() &&
157  "Sanitizer's init function expects different number of arguments");
158  Function *InitFunction =
159  declareSanitizerInitFunction(M, InitName, InitArgTypes);
160  Function *Ctor = Function::Create(
162  GlobalValue::InternalLinkage, CtorName, &M);
163  BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
164  IRBuilder<> IRB(ReturnInst::Create(M.getContext(), CtorBB));
165  IRB.CreateCall(InitFunction, InitArgs);
166  if (!VersionCheckName.empty()) {
167  Function *VersionCheckFunction =
169  VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
170  AttributeList()));
171  IRB.CreateCall(VersionCheckFunction, {});
172  }
173  return std::make_pair(Ctor, InitFunction);
174 }
175 
176 std::pair<Function *, Function *>
178  Module &M, StringRef CtorName, StringRef InitName,
179  ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
180  function_ref<void(Function *, Function *)> FunctionsCreatedCallback,
181  StringRef VersionCheckName) {
182  assert(!CtorName.empty() && "Expected ctor function name");
183 
184  if (Function *Ctor = M.getFunction(CtorName))
185  // FIXME: Sink this logic into the module, similar to the handling of
186  // globals. This will make moving to a concurrent model much easier.
187  if (Ctor->arg_size() == 0 ||
188  Ctor->getReturnType() == Type::getVoidTy(M.getContext()))
189  return {Ctor, declareSanitizerInitFunction(M, InitName, InitArgTypes)};
190 
191  Function *Ctor, *InitFunction;
192  std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions(
193  M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName);
194  FunctionsCreatedCallback(Ctor, InitFunction);
195  return std::make_pair(Ctor, InitFunction);
196 }
197 
199  assert(!Name.empty() && "Expected init function name");
200  if (Function *F = M.getFunction(Name)) {
201  if (F->arg_size() != 0 ||
202  F->getReturnType() != Type::getVoidTy(M.getContext())) {
203  std::string Err;
204  raw_string_ostream Stream(Err);
205  Stream << "Sanitizer interface function defined with wrong type: " << *F;
206  report_fatal_error(Err);
207  }
208  return F;
209  }
211  Name, AttributeList(), Type::getVoidTy(M.getContext())));
213 
214  appendToGlobalCtors(M, F, 0);
215 
216  return F;
217 }
218 
220  Module &M, SmallVectorImpl<Function *> &DeadComdatFunctions) {
221  // Build a map from the comdat to the number of entries in that comdat we
222  // think are dead. If this fully covers the comdat group, then the entire
223  // group is dead. If we find another entry in the comdat group though, we'll
224  // have to preserve the whole group.
225  SmallDenseMap<Comdat *, int, 16> ComdatEntriesCovered;
226  for (Function *F : DeadComdatFunctions) {
227  Comdat *C = F->getComdat();
228  assert(C && "Expected all input GVs to be in a comdat!");
229  ComdatEntriesCovered[C] += 1;
230  }
231 
232  auto CheckComdat = [&](Comdat &C) {
233  auto CI = ComdatEntriesCovered.find(&C);
234  if (CI == ComdatEntriesCovered.end())
235  return;
236 
237  // If this could have been covered by a dead entry, just subtract one to
238  // account for it.
239  if (CI->second > 0) {
240  CI->second -= 1;
241  return;
242  }
243 
244  // If we've already accounted for all the entries that were dead, the
245  // entire comdat is alive so remove it from the map.
246  ComdatEntriesCovered.erase(CI);
247  };
248 
249  auto CheckAllComdats = [&] {
250  for (Function &F : M.functions())
251  if (Comdat *C = F.getComdat()) {
252  CheckComdat(*C);
253  if (ComdatEntriesCovered.empty())
254  return;
255  }
256  for (GlobalVariable &GV : M.globals())
257  if (Comdat *C = GV.getComdat()) {
258  CheckComdat(*C);
259  if (ComdatEntriesCovered.empty())
260  return;
261  }
262  for (GlobalAlias &GA : M.aliases())
263  if (Comdat *C = GA.getComdat()) {
264  CheckComdat(*C);
265  if (ComdatEntriesCovered.empty())
266  return;
267  }
268  };
269  CheckAllComdats();
270 
271  if (ComdatEntriesCovered.empty()) {
272  DeadComdatFunctions.clear();
273  return;
274  }
275 
276  // Remove the entries that were not covering.
277  erase_if(DeadComdatFunctions, [&](GlobalValue *GV) {
278  return ComdatEntriesCovered.find(GV->getComdat()) ==
279  ComdatEntriesCovered.end();
280  });
281 }
282 
284  MD5 Md5;
285  bool ExportsSymbols = false;
286  auto AddGlobal = [&](GlobalValue &GV) {
287  if (GV.isDeclaration() || GV.getName().startswith("llvm.") ||
288  !GV.hasExternalLinkage() || GV.hasComdat())
289  return;
290  ExportsSymbols = true;
291  Md5.update(GV.getName());
292  Md5.update(ArrayRef<uint8_t>{0});
293  };
294 
295  for (auto &F : *M)
296  AddGlobal(F);
297  for (auto &GV : M->globals())
298  AddGlobal(GV);
299  for (auto &GA : M->aliases())
300  AddGlobal(GA);
301  for (auto &IF : M->ifuncs())
302  AddGlobal(IF);
303 
304  if (!ExportsSymbols)
305  return "";
306 
307  MD5::MD5Result R;
308  Md5.final(R);
309 
310  SmallString<32> Str;
311  MD5::stringifyResult(R, Str);
312  return ("$" + Str).str();
313 }
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:87
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:54
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:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:143
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
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:312
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:401
Externally visible function.
Definition: GlobalValue.h:48
GlobalVariable * getGlobalVariable(StringRef Name) const
Look up the specified global variable in the module symbol table.
Definition: Module.h:386
F(f)
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:982
static Constant * getNullValue(Type *Ty)
Constructor to create a &#39;0&#39; constant of arbitrary type.
Definition: Constants.cpp:264
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: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:200
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:243
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
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:368
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:132
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:1772
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:605
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
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: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
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:4147
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:1043
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:91
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:1586
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:481
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:846
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:83
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:444
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:1329
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:175
const Comdat * getComdat() const
Definition: Globals.cpp:170
ConstantArray - Constant Array Declarations.
Definition: Constants.h:413
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:322
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
Type * getElementType() const
Definition: DerivedTypes.h:359
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:583
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
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:623