LLVM  8.0.0svn
EfficiencySanitizer.cpp
Go to the documentation of this file.
1 //===-- EfficiencySanitizer.cpp - performance tuner -----------------------===//
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 file is a part of EfficiencySanitizer, a family of performance tuners
11 // that detects multiple performance issues via separate sub-tools.
12 //
13 // The instrumentation phase is straightforward:
14 // - Take action on every memory access: either inlined instrumentation,
15 // or Inserted calls to our run-time library.
16 // - Optimizations may apply to avoid instrumenting some of the accesses.
17 // - Turn mem{set,cpy,move} instrinsics into library calls.
18 // The rest is handled by the run-time library.
19 //===----------------------------------------------------------------------===//
20 
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/IRBuilder.h"
29 #include "llvm/IR/IntrinsicInst.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/IR/Type.h"
33 #include "llvm/Support/Debug.h"
38 
39 using namespace llvm;
40 
41 #define DEBUG_TYPE "esan"
42 
43 // The tool type must be just one of these ClTool* options, as the tools
44 // cannot be combined due to shadow memory constraints.
45 static cl::opt<bool>
46  ClToolCacheFrag("esan-cache-frag", cl::init(false),
47  cl::desc("Detect data cache fragmentation"), cl::Hidden);
48 static cl::opt<bool>
49  ClToolWorkingSet("esan-working-set", cl::init(false),
50  cl::desc("Measure the working set size"), cl::Hidden);
51 // Each new tool will get its own opt flag here.
52 // These are converted to EfficiencySanitizerOptions for use
53 // in the code.
54 
56  "esan-instrument-loads-and-stores", cl::init(true),
57  cl::desc("Instrument loads and stores"), cl::Hidden);
59  "esan-instrument-memintrinsics", cl::init(true),
60  cl::desc("Instrument memintrinsics (memset/memcpy/memmove)"), cl::Hidden);
62  "esan-instrument-fastpath", cl::init(true),
63  cl::desc("Instrument fastpath"), cl::Hidden);
65  "esan-aux-field-info", cl::init(true),
66  cl::desc("Generate binary with auxiliary struct field information"),
67  cl::Hidden);
68 
69 // Experiments show that the performance difference can be 2x or more,
70 // and accuracy loss is typically negligible, so we turn this on by default.
72  "esan-assume-intra-cache-line", cl::init(true),
73  cl::desc("Assume each memory access touches just one cache line, for "
74  "better performance but with a potential loss of accuracy."),
75  cl::Hidden);
76 
77 STATISTIC(NumInstrumentedLoads, "Number of instrumented loads");
78 STATISTIC(NumInstrumentedStores, "Number of instrumented stores");
79 STATISTIC(NumFastpaths, "Number of instrumented fastpaths");
80 STATISTIC(NumAccessesWithIrregularSize,
81  "Number of accesses with a size outside our targeted callout sizes");
82 STATISTIC(NumIgnoredStructs, "Number of ignored structs");
83 STATISTIC(NumIgnoredGEPs, "Number of ignored GEP instructions");
84 STATISTIC(NumInstrumentedGEPs, "Number of instrumented GEP instructions");
85 STATISTIC(NumAssumedIntraCacheLine,
86  "Number of accesses assumed to be intra-cache-line");
87 
88 static const uint64_t EsanCtorAndDtorPriority = 0;
89 static const char *const EsanModuleCtorName = "esan.module_ctor";
90 static const char *const EsanModuleDtorName = "esan.module_dtor";
91 static const char *const EsanInitName = "__esan_init";
92 static const char *const EsanExitName = "__esan_exit";
93 
94 // We need to specify the tool to the runtime earlier than
95 // the ctor is called in some cases, so we set a global variable.
96 static const char *const EsanWhichToolName = "__esan_which_tool";
97 
98 // We must keep these Shadow* constants consistent with the esan runtime.
99 // FIXME: Try to place these shadow constants, the names of the __esan_*
100 // interface functions, and the ToolType enum into a header shared between
101 // llvm and compiler-rt.
103  uint64_t ShadowMask;
104  uint64_t ShadowOffs[3];
105 };
106 
108  0x00000fffffffffffull,
109  {
110  0x0000130000000000ull, 0x0000220000000000ull, 0x0000440000000000ull,
111  }};
112 
114  0x0fffffffffull,
115  {
116  0x1300000000ull, 0x2200000000ull, 0x4400000000ull,
117  }};
118 
119 // This array is indexed by the ToolType enum.
120 static const int ShadowScale[] = {
121  0, // ESAN_None.
122  2, // ESAN_CacheFrag: 4B:1B, so 4 to 1 == >>2.
123  6, // ESAN_WorkingSet: 64B:1B, so 64 to 1 == >>6.
124 };
125 
126 // MaxStructCounterNameSize is a soft size limit to avoid insanely long
127 // names for those extremely large structs.
128 static const unsigned MaxStructCounterNameSize = 512;
129 
130 namespace {
131 
133 OverrideOptionsFromCL(EfficiencySanitizerOptions Options) {
134  if (ClToolCacheFrag)
136  else if (ClToolWorkingSet)
138 
139  // Direct opt invocation with no params will have the default ESAN_None.
140  // We run the default tool in that case.
143 
144  return Options;
145 }
146 
147 /// EfficiencySanitizer: instrument each module to find performance issues.
148 class EfficiencySanitizer : public ModulePass {
149 public:
150  EfficiencySanitizer(
152  : ModulePass(ID), Options(OverrideOptionsFromCL(Opts)) {}
153  StringRef getPassName() const override;
154  void getAnalysisUsage(AnalysisUsage &AU) const override;
155  bool runOnModule(Module &M) override;
156  static char ID;
157 
158 private:
159  bool initOnModule(Module &M);
160  void initializeCallbacks(Module &M);
161  bool shouldIgnoreStructType(StructType *StructTy);
162  void createStructCounterName(
164  void createCacheFragAuxGV(
165  Module &M, const DataLayout &DL, StructType *StructTy,
167  GlobalVariable *createCacheFragInfoGV(Module &M, const DataLayout &DL,
168  Constant *UnitName);
169  Constant *createEsanInitToolInfoArg(Module &M, const DataLayout &DL);
170  void createDestructor(Module &M, Constant *ToolInfoArg);
171  bool runOnFunction(Function &F, Module &M);
172  bool instrumentLoadOrStore(Instruction *I, const DataLayout &DL);
173  bool instrumentMemIntrinsic(MemIntrinsic *MI);
174  bool instrumentGetElementPtr(Instruction *I, Module &M);
175  bool insertCounterUpdate(Instruction *I, StructType *StructTy,
176  unsigned CounterIdx);
177  unsigned getFieldCounterIdx(StructType *StructTy) {
178  return 0;
179  }
180  unsigned getArrayCounterIdx(StructType *StructTy) {
181  return StructTy->getNumElements();
182  }
183  unsigned getStructCounterSize(StructType *StructTy) {
184  // The struct counter array includes:
185  // - one counter for each struct field,
186  // - one counter for the struct access within an array.
187  return (StructTy->getNumElements()/*field*/ + 1/*array*/);
188  }
189  bool shouldIgnoreMemoryAccess(Instruction *I);
190  int getMemoryAccessFuncIndex(Value *Addr, const DataLayout &DL);
191  Value *appToShadow(Value *Shadow, IRBuilder<> &IRB);
192  bool instrumentFastpath(Instruction *I, const DataLayout &DL, bool IsStore,
193  Value *Addr, unsigned Alignment);
194  // Each tool has its own fastpath routine:
195  bool instrumentFastpathCacheFrag(Instruction *I, const DataLayout &DL,
196  Value *Addr, unsigned Alignment);
197  bool instrumentFastpathWorkingSet(Instruction *I, const DataLayout &DL,
198  Value *Addr, unsigned Alignment);
199 
201  LLVMContext *Ctx;
202  Type *IntptrTy;
203  // Our slowpath involves callouts to the runtime library.
204  // Access sizes are powers of two: 1, 2, 4, 8, 16.
205  static const size_t NumberOfAccessSizes = 5;
206  Function *EsanAlignedLoad[NumberOfAccessSizes];
207  Function *EsanAlignedStore[NumberOfAccessSizes];
208  Function *EsanUnalignedLoad[NumberOfAccessSizes];
209  Function *EsanUnalignedStore[NumberOfAccessSizes];
210  // For irregular sizes of any alignment:
211  Function *EsanUnalignedLoadN, *EsanUnalignedStoreN;
212  Function *MemmoveFn, *MemcpyFn, *MemsetFn;
213  Function *EsanCtorFunction;
214  Function *EsanDtorFunction;
215  // Remember the counter variable for each struct type to avoid
216  // recomputing the variable name later during instrumentation.
217  std::map<Type *, GlobalVariable *> StructTyMap;
218  ShadowMemoryParams ShadowParams;
219 };
220 } // namespace
221 
222 char EfficiencySanitizer::ID = 0;
224  EfficiencySanitizer, "esan",
225  "EfficiencySanitizer: finds performance issues.", false, false)
228  EfficiencySanitizer, "esan",
229  "EfficiencySanitizer: finds performance issues.", false, false)
230 
231 StringRef EfficiencySanitizer::getPassName() const {
232  return "EfficiencySanitizer";
233 }
234 
235 void EfficiencySanitizer::getAnalysisUsage(AnalysisUsage &AU) const {
237 }
238 
239 ModulePass *
241  return new EfficiencySanitizer(Options);
242 }
243 
244 void EfficiencySanitizer::initializeCallbacks(Module &M) {
245  IRBuilder<> IRB(M.getContext());
246  // Initialize the callbacks.
247  for (size_t Idx = 0; Idx < NumberOfAccessSizes; ++Idx) {
248  const unsigned ByteSize = 1U << Idx;
249  std::string ByteSizeStr = utostr(ByteSize);
250  // We'll inline the most common (i.e., aligned and frequent sizes)
251  // load + store instrumentation: these callouts are for the slowpath.
252  SmallString<32> AlignedLoadName("__esan_aligned_load" + ByteSizeStr);
253  EsanAlignedLoad[Idx] =
255  AlignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
256  SmallString<32> AlignedStoreName("__esan_aligned_store" + ByteSizeStr);
257  EsanAlignedStore[Idx] =
259  AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
260  SmallString<32> UnalignedLoadName("__esan_unaligned_load" + ByteSizeStr);
261  EsanUnalignedLoad[Idx] =
263  UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
264  SmallString<32> UnalignedStoreName("__esan_unaligned_store" + ByteSizeStr);
265  EsanUnalignedStore[Idx] =
267  UnalignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
268  }
269  EsanUnalignedLoadN = checkSanitizerInterfaceFunction(
270  M.getOrInsertFunction("__esan_unaligned_loadN", IRB.getVoidTy(),
271  IRB.getInt8PtrTy(), IntptrTy));
272  EsanUnalignedStoreN = checkSanitizerInterfaceFunction(
273  M.getOrInsertFunction("__esan_unaligned_storeN", IRB.getVoidTy(),
274  IRB.getInt8PtrTy(), IntptrTy));
276  M.getOrInsertFunction("memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
277  IRB.getInt8PtrTy(), IntptrTy));
279  M.getOrInsertFunction("memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
280  IRB.getInt8PtrTy(), IntptrTy));
282  M.getOrInsertFunction("memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
283  IRB.getInt32Ty(), IntptrTy));
284 }
285 
286 bool EfficiencySanitizer::shouldIgnoreStructType(StructType *StructTy) {
287  if (StructTy == nullptr || StructTy->isOpaque() /* no struct body */)
288  return true;
289  return false;
290 }
291 
292 void EfficiencySanitizer::createStructCounterName(
294  // Append NumFields and field type ids to avoid struct conflicts
295  // with the same name but different fields.
296  if (StructTy->hasName())
297  NameStr += StructTy->getName();
298  else
299  NameStr += "struct.anon";
300  // We allow the actual size of the StructCounterName to be larger than
301  // MaxStructCounterNameSize and append $NumFields and at least one
302  // field type id.
303  // Append $NumFields.
304  NameStr += "$";
305  Twine(StructTy->getNumElements()).toVector(NameStr);
306  // Append struct field type ids in the reverse order.
307  for (int i = StructTy->getNumElements() - 1; i >= 0; --i) {
308  NameStr += "$";
309  Twine(StructTy->getElementType(i)->getTypeID()).toVector(NameStr);
310  if (NameStr.size() >= MaxStructCounterNameSize)
311  break;
312  }
313  if (StructTy->isLiteral()) {
314  // End with $ for literal struct.
315  NameStr += "$";
316  }
317 }
318 
319 // Create global variables with auxiliary information (e.g., struct field size,
320 // offset, and type name) for better user report.
321 void EfficiencySanitizer::createCacheFragAuxGV(
322  Module &M, const DataLayout &DL, StructType *StructTy,
324  GlobalVariable *&Size) {
325  auto *Int8PtrTy = Type::getInt8PtrTy(*Ctx);
326  auto *Int32Ty = Type::getInt32Ty(*Ctx);
327  // FieldTypeName.
328  auto *TypeNameArrayTy = ArrayType::get(Int8PtrTy, StructTy->getNumElements());
329  TypeName = new GlobalVariable(M, TypeNameArrayTy, true,
331  SmallVector<Constant *, 16> TypeNameVec;
332  // FieldOffset.
333  auto *OffsetArrayTy = ArrayType::get(Int32Ty, StructTy->getNumElements());
334  Offset = new GlobalVariable(M, OffsetArrayTy, true,
336  SmallVector<Constant *, 16> OffsetVec;
337  // FieldSize
338  auto *SizeArrayTy = ArrayType::get(Int32Ty, StructTy->getNumElements());
339  Size = new GlobalVariable(M, SizeArrayTy, true,
342  for (unsigned i = 0; i < StructTy->getNumElements(); ++i) {
343  Type *Ty = StructTy->getElementType(i);
344  std::string Str;
345  raw_string_ostream StrOS(Str);
346  Ty->print(StrOS);
347  TypeNameVec.push_back(
349  createPrivateGlobalForString(M, StrOS.str(), true),
350  Int8PtrTy));
351  OffsetVec.push_back(
353  DL.getStructLayout(StructTy)->getElementOffset(i)));
355  DL.getTypeAllocSize(Ty)));
356  }
357  TypeName->setInitializer(ConstantArray::get(TypeNameArrayTy, TypeNameVec));
358  Offset->setInitializer(ConstantArray::get(OffsetArrayTy, OffsetVec));
359  Size->setInitializer(ConstantArray::get(SizeArrayTy, SizeVec));
360 }
361 
362 // Create the global variable for the cache-fragmentation tool.
363 GlobalVariable *EfficiencySanitizer::createCacheFragInfoGV(
364  Module &M, const DataLayout &DL, Constant *UnitName) {
366 
367  auto *Int8PtrTy = Type::getInt8PtrTy(*Ctx);
368  auto *Int8PtrPtrTy = Int8PtrTy->getPointerTo();
369  auto *Int32Ty = Type::getInt32Ty(*Ctx);
370  auto *Int32PtrTy = Type::getInt32PtrTy(*Ctx);
371  auto *Int64Ty = Type::getInt64Ty(*Ctx);
372  auto *Int64PtrTy = Type::getInt64PtrTy(*Ctx);
373  // This structure should be kept consistent with the StructInfo struct
374  // in the runtime library.
375  // struct StructInfo {
376  // const char *StructName;
377  // u32 Size;
378  // u32 NumFields;
379  // u32 *FieldOffset; // auxiliary struct field info.
380  // u32 *FieldSize; // auxiliary struct field info.
381  // const char **FieldTypeName; // auxiliary struct field info.
382  // u64 *FieldCounters;
383  // u64 *ArrayCounter;
384  // };
385  auto *StructInfoTy =
386  StructType::get(Int8PtrTy, Int32Ty, Int32Ty, Int32PtrTy, Int32PtrTy,
387  Int8PtrPtrTy, Int64PtrTy, Int64PtrTy);
388  auto *StructInfoPtrTy = StructInfoTy->getPointerTo();
389  // This structure should be kept consistent with the CacheFragInfo struct
390  // in the runtime library.
391  // struct CacheFragInfo {
392  // const char *UnitName;
393  // u32 NumStructs;
394  // StructInfo *Structs;
395  // };
396  auto *CacheFragInfoTy = StructType::get(Int8PtrTy, Int32Ty, StructInfoPtrTy);
397 
398  std::vector<StructType *> Vec = M.getIdentifiedStructTypes();
399  unsigned NumStructs = 0;
400  SmallVector<Constant *, 16> Initializers;
401 
402  for (auto &StructTy : Vec) {
403  if (shouldIgnoreStructType(StructTy)) {
404  ++NumIgnoredStructs;
405  continue;
406  }
407  ++NumStructs;
408 
409  // StructName.
411  createStructCounterName(StructTy, CounterNameStr);
412  GlobalVariable *StructCounterName = createPrivateGlobalForString(
413  M, CounterNameStr, /*AllowMerging*/true);
414 
415  // Counters.
416  // We create the counter array with StructCounterName and weak linkage
417  // so that the structs with the same name and layout from different
418  // compilation units will be merged into one.
419  auto *CounterArrayTy = ArrayType::get(Int64Ty,
420  getStructCounterSize(StructTy));
421  GlobalVariable *Counters =
422  new GlobalVariable(M, CounterArrayTy, false,
424  ConstantAggregateZero::get(CounterArrayTy),
425  CounterNameStr);
426 
427  // Remember the counter variable for each struct type.
428  StructTyMap.insert(std::pair<Type *, GlobalVariable *>(StructTy, Counters));
429 
430  // We pass the field type name array, offset array, and size array to
431  // the runtime for better reporting.
432  GlobalVariable *TypeName = nullptr, *Offset = nullptr, *Size = nullptr;
433  if (ClAuxFieldInfo)
434  createCacheFragAuxGV(M, DL, StructTy, TypeName, Offset, Size);
435 
436  Constant *FieldCounterIdx[2];
437  FieldCounterIdx[0] = ConstantInt::get(Int32Ty, 0);
438  FieldCounterIdx[1] = ConstantInt::get(Int32Ty,
439  getFieldCounterIdx(StructTy));
440  Constant *ArrayCounterIdx[2];
441  ArrayCounterIdx[0] = ConstantInt::get(Int32Ty, 0);
442  ArrayCounterIdx[1] = ConstantInt::get(Int32Ty,
443  getArrayCounterIdx(StructTy));
444  Initializers.push_back(ConstantStruct::get(
445  StructInfoTy,
446  ConstantExpr::getPointerCast(StructCounterName, Int8PtrTy),
448  DL.getStructLayout(StructTy)->getSizeInBytes()),
450  Offset == nullptr ? ConstantPointerNull::get(Int32PtrTy)
451  : ConstantExpr::getPointerCast(Offset, Int32PtrTy),
452  Size == nullptr ? ConstantPointerNull::get(Int32PtrTy)
453  : ConstantExpr::getPointerCast(Size, Int32PtrTy),
454  TypeName == nullptr
455  ? ConstantPointerNull::get(Int8PtrPtrTy)
456  : ConstantExpr::getPointerCast(TypeName, Int8PtrPtrTy),
457  ConstantExpr::getGetElementPtr(CounterArrayTy, Counters,
458  FieldCounterIdx),
459  ConstantExpr::getGetElementPtr(CounterArrayTy, Counters,
460  ArrayCounterIdx)));
461  }
462  // Structs.
463  Constant *StructInfo;
464  if (NumStructs == 0) {
465  StructInfo = ConstantPointerNull::get(StructInfoPtrTy);
466  } else {
467  auto *StructInfoArrayTy = ArrayType::get(StructInfoTy, NumStructs);
468  StructInfo = ConstantExpr::getPointerCast(
469  new GlobalVariable(M, StructInfoArrayTy, false,
471  ConstantArray::get(StructInfoArrayTy, Initializers)),
472  StructInfoPtrTy);
473  }
474 
475  auto *CacheFragInfoGV = new GlobalVariable(
476  M, CacheFragInfoTy, true, GlobalVariable::InternalLinkage,
477  ConstantStruct::get(CacheFragInfoTy, UnitName,
478  ConstantInt::get(Int32Ty, NumStructs), StructInfo));
479  return CacheFragInfoGV;
480 }
481 
482 // Create the tool-specific argument passed to EsanInit and EsanExit.
483 Constant *EfficiencySanitizer::createEsanInitToolInfoArg(Module &M,
484  const DataLayout &DL) {
485  // This structure contains tool-specific information about each compilation
486  // unit (module) and is passed to the runtime library.
487  GlobalVariable *ToolInfoGV = nullptr;
488 
489  auto *Int8PtrTy = Type::getInt8PtrTy(*Ctx);
490  // Compilation unit name.
491  auto *UnitName = ConstantExpr::getPointerCast(
493  Int8PtrTy);
494 
495  // Create the tool-specific variable.
496  if (Options.ToolType == EfficiencySanitizerOptions::ESAN_CacheFrag)
497  ToolInfoGV = createCacheFragInfoGV(M, DL, UnitName);
498 
499  if (ToolInfoGV != nullptr)
500  return ConstantExpr::getPointerCast(ToolInfoGV, Int8PtrTy);
501 
502  // Create the null pointer if no tool-specific variable created.
503  return ConstantPointerNull::get(Int8PtrTy);
504 }
505 
506 void EfficiencySanitizer::createDestructor(Module &M, Constant *ToolInfoArg) {
507  PointerType *Int8PtrTy = Type::getInt8PtrTy(*Ctx);
508  EsanDtorFunction = Function::Create(FunctionType::get(Type::getVoidTy(*Ctx),
509  false),
511  EsanModuleDtorName, &M);
512  ReturnInst::Create(*Ctx, BasicBlock::Create(*Ctx, "", EsanDtorFunction));
513  IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator());
515  M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(),
516  Int8PtrTy));
517  EsanExit->setLinkage(Function::ExternalLinkage);
518  IRB_Dtor.CreateCall(EsanExit, {ToolInfoArg});
519  appendToGlobalDtors(M, EsanDtorFunction, EsanCtorAndDtorPriority);
520 }
521 
522 bool EfficiencySanitizer::initOnModule(Module &M) {
523 
524  Triple TargetTriple(M.getTargetTriple());
525  if (TargetTriple.isMIPS64())
526  ShadowParams = ShadowParams40;
527  else
528  ShadowParams = ShadowParams47;
529 
530  Ctx = &M.getContext();
531  const DataLayout &DL = M.getDataLayout();
532  IRBuilder<> IRB(M.getContext());
533  IntegerType *OrdTy = IRB.getInt32Ty();
534  PointerType *Int8PtrTy = Type::getInt8PtrTy(*Ctx);
535  IntptrTy = DL.getIntPtrType(M.getContext());
536  // Create the variable passed to EsanInit and EsanExit.
537  Constant *ToolInfoArg = createEsanInitToolInfoArg(M, DL);
538  // Constructor
539  // We specify the tool type both in the EsanWhichToolName global
540  // and as an arg to the init routine as a sanity check.
541  std::tie(EsanCtorFunction, std::ignore) = createSanitizerCtorAndInitFunctions(
542  M, EsanModuleCtorName, EsanInitName, /*InitArgTypes=*/{OrdTy, Int8PtrTy},
543  /*InitArgs=*/{
544  ConstantInt::get(OrdTy, static_cast<int>(Options.ToolType)),
545  ToolInfoArg});
546  appendToGlobalCtors(M, EsanCtorFunction, EsanCtorAndDtorPriority);
547 
548  createDestructor(M, ToolInfoArg);
549 
550  new GlobalVariable(M, OrdTy, true,
552  ConstantInt::get(OrdTy,
553  static_cast<int>(Options.ToolType)),
555 
556  return true;
557 }
558 
559 Value *EfficiencySanitizer::appToShadow(Value *Shadow, IRBuilder<> &IRB) {
560  // Shadow = ((App & Mask) + Offs) >> Scale
561  Shadow = IRB.CreateAnd(Shadow, ConstantInt::get(IntptrTy, ShadowParams.ShadowMask));
562  uint64_t Offs;
563  int Scale = ShadowScale[Options.ToolType];
564  if (Scale <= 2)
565  Offs = ShadowParams.ShadowOffs[Scale];
566  else
567  Offs = ShadowParams.ShadowOffs[0] << Scale;
568  Shadow = IRB.CreateAdd(Shadow, ConstantInt::get(IntptrTy, Offs));
569  if (Scale > 0)
570  Shadow = IRB.CreateLShr(Shadow, Scale);
571  return Shadow;
572 }
573 
574 bool EfficiencySanitizer::shouldIgnoreMemoryAccess(Instruction *I) {
575  if (Options.ToolType == EfficiencySanitizerOptions::ESAN_CacheFrag) {
576  // We'd like to know about cache fragmentation in vtable accesses and
577  // constant data references, so we do not currently ignore anything.
578  return false;
579  } else if (Options.ToolType == EfficiencySanitizerOptions::ESAN_WorkingSet) {
580  // TODO: the instrumentation disturbs the data layout on the stack, so we
581  // may want to add an option to ignore stack references (if we can
582  // distinguish them) to reduce overhead.
583  }
584  // TODO(bruening): future tools will be returning true for some cases.
585  return false;
586 }
587 
588 bool EfficiencySanitizer::runOnModule(Module &M) {
589  bool Res = initOnModule(M);
590  initializeCallbacks(M);
591  for (auto &F : M) {
592  Res |= runOnFunction(F, M);
593  }
594  return Res;
595 }
596 
598  // This is required to prevent instrumenting the call to __esan_init from
599  // within the module constructor.
600  if (&F == EsanCtorFunction)
601  return false;
602  SmallVector<Instruction *, 8> LoadsAndStores;
603  SmallVector<Instruction *, 8> MemIntrinCalls;
604  SmallVector<Instruction *, 8> GetElementPtrs;
605  bool Res = false;
606  const DataLayout &DL = M.getDataLayout();
607  const TargetLibraryInfo *TLI =
608  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
609 
610  for (auto &BB : F) {
611  for (auto &Inst : BB) {
612  if ((isa<LoadInst>(Inst) || isa<StoreInst>(Inst) ||
613  isa<AtomicRMWInst>(Inst) || isa<AtomicCmpXchgInst>(Inst)) &&
614  !shouldIgnoreMemoryAccess(&Inst))
615  LoadsAndStores.push_back(&Inst);
616  else if (isa<MemIntrinsic>(Inst))
617  MemIntrinCalls.push_back(&Inst);
618  else if (isa<GetElementPtrInst>(Inst))
619  GetElementPtrs.push_back(&Inst);
620  else if (CallInst *CI = dyn_cast<CallInst>(&Inst))
622  }
623  }
624 
626  for (auto Inst : LoadsAndStores) {
627  Res |= instrumentLoadOrStore(Inst, DL);
628  }
629  }
630 
632  for (auto Inst : MemIntrinCalls) {
633  Res |= instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
634  }
635  }
636 
637  if (Options.ToolType == EfficiencySanitizerOptions::ESAN_CacheFrag) {
638  for (auto Inst : GetElementPtrs) {
639  Res |= instrumentGetElementPtr(Inst, M);
640  }
641  }
642 
643  return Res;
644 }
645 
646 bool EfficiencySanitizer::instrumentLoadOrStore(Instruction *I,
647  const DataLayout &DL) {
648  IRBuilder<> IRB(I);
649  bool IsStore;
650  Value *Addr;
651  unsigned Alignment;
652  if (LoadInst *Load = dyn_cast<LoadInst>(I)) {
653  IsStore = false;
654  Alignment = Load->getAlignment();
655  Addr = Load->getPointerOperand();
656  } else if (StoreInst *Store = dyn_cast<StoreInst>(I)) {
657  IsStore = true;
658  Alignment = Store->getAlignment();
659  Addr = Store->getPointerOperand();
660  } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
661  IsStore = true;
662  Alignment = 0;
663  Addr = RMW->getPointerOperand();
664  } else if (AtomicCmpXchgInst *Xchg = dyn_cast<AtomicCmpXchgInst>(I)) {
665  IsStore = true;
666  Alignment = 0;
667  Addr = Xchg->getPointerOperand();
668  } else
669  llvm_unreachable("Unsupported mem access type");
670 
671  Type *OrigTy = cast<PointerType>(Addr->getType())->getElementType();
672  const uint32_t TypeSizeBytes = DL.getTypeStoreSizeInBits(OrigTy) / 8;
673  Value *OnAccessFunc = nullptr;
674 
675  // Convert 0 to the default alignment.
676  if (Alignment == 0)
677  Alignment = DL.getPrefTypeAlignment(OrigTy);
678 
679  if (IsStore)
680  NumInstrumentedStores++;
681  else
682  NumInstrumentedLoads++;
683  int Idx = getMemoryAccessFuncIndex(Addr, DL);
684  if (Idx < 0) {
685  OnAccessFunc = IsStore ? EsanUnalignedStoreN : EsanUnalignedLoadN;
686  IRB.CreateCall(OnAccessFunc,
687  {IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
688  ConstantInt::get(IntptrTy, TypeSizeBytes)});
689  } else {
690  if (ClInstrumentFastpath &&
691  instrumentFastpath(I, DL, IsStore, Addr, Alignment)) {
692  NumFastpaths++;
693  return true;
694  }
695  if (Alignment == 0 || (Alignment % TypeSizeBytes) == 0)
696  OnAccessFunc = IsStore ? EsanAlignedStore[Idx] : EsanAlignedLoad[Idx];
697  else
698  OnAccessFunc = IsStore ? EsanUnalignedStore[Idx] : EsanUnalignedLoad[Idx];
699  IRB.CreateCall(OnAccessFunc,
700  IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()));
701  }
702  return true;
703 }
704 
705 // It's simplest to replace the memset/memmove/memcpy intrinsics with
706 // calls that the runtime library intercepts.
707 // Our pass is late enough that calls should not turn back into intrinsics.
708 bool EfficiencySanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
709  IRBuilder<> IRB(MI);
710  bool Res = false;
711  if (isa<MemSetInst>(MI)) {
712  IRB.CreateCall(
713  MemsetFn,
714  {IRB.CreatePointerCast(MI->getArgOperand(0), IRB.getInt8PtrTy()),
715  IRB.CreateIntCast(MI->getArgOperand(1), IRB.getInt32Ty(), false),
716  IRB.CreateIntCast(MI->getArgOperand(2), IntptrTy, false)});
717  MI->eraseFromParent();
718  Res = true;
719  } else if (isa<MemTransferInst>(MI)) {
720  IRB.CreateCall(
721  isa<MemCpyInst>(MI) ? MemcpyFn : MemmoveFn,
722  {IRB.CreatePointerCast(MI->getArgOperand(0), IRB.getInt8PtrTy()),
723  IRB.CreatePointerCast(MI->getArgOperand(1), IRB.getInt8PtrTy()),
724  IRB.CreateIntCast(MI->getArgOperand(2), IntptrTy, false)});
725  MI->eraseFromParent();
726  Res = true;
727  } else
728  llvm_unreachable("Unsupported mem intrinsic type");
729  return Res;
730 }
731 
732 bool EfficiencySanitizer::instrumentGetElementPtr(Instruction *I, Module &M) {
734  bool Res = false;
735  if (GepInst == nullptr || GepInst->getNumIndices() == 1) {
736  ++NumIgnoredGEPs;
737  return false;
738  }
739  Type *SourceTy = GepInst->getSourceElementType();
740  StructType *StructTy = nullptr;
741  ConstantInt *Idx;
742  // Check if GEP calculates address from a struct array.
743  if (isa<StructType>(SourceTy)) {
744  StructTy = cast<StructType>(SourceTy);
745  Idx = dyn_cast<ConstantInt>(GepInst->getOperand(1));
746  if ((Idx == nullptr || Idx->getSExtValue() != 0) &&
747  !shouldIgnoreStructType(StructTy) && StructTyMap.count(StructTy) != 0)
748  Res |= insertCounterUpdate(I, StructTy, getArrayCounterIdx(StructTy));
749  }
750  // Iterate all (except the first and the last) idx within each GEP instruction
751  // for possible nested struct field address calculation.
752  for (unsigned i = 1; i < GepInst->getNumIndices(); ++i) {
753  SmallVector<Value *, 8> IdxVec(GepInst->idx_begin(),
754  GepInst->idx_begin() + i);
755  Type *Ty = GetElementPtrInst::getIndexedType(SourceTy, IdxVec);
756  unsigned CounterIdx = 0;
757  if (isa<ArrayType>(Ty)) {
758  ArrayType *ArrayTy = cast<ArrayType>(Ty);
759  StructTy = dyn_cast<StructType>(ArrayTy->getElementType());
760  if (shouldIgnoreStructType(StructTy) || StructTyMap.count(StructTy) == 0)
761  continue;
762  // The last counter for struct array access.
763  CounterIdx = getArrayCounterIdx(StructTy);
764  } else if (isa<StructType>(Ty)) {
765  StructTy = cast<StructType>(Ty);
766  if (shouldIgnoreStructType(StructTy) || StructTyMap.count(StructTy) == 0)
767  continue;
768  // Get the StructTy's subfield index.
769  Idx = cast<ConstantInt>(GepInst->getOperand(i+1));
770  assert(Idx->getSExtValue() >= 0 &&
771  Idx->getSExtValue() < StructTy->getNumElements());
772  CounterIdx = getFieldCounterIdx(StructTy) + Idx->getSExtValue();
773  }
774  Res |= insertCounterUpdate(I, StructTy, CounterIdx);
775  }
776  if (Res)
777  ++NumInstrumentedGEPs;
778  else
779  ++NumIgnoredGEPs;
780  return Res;
781 }
782 
783 bool EfficiencySanitizer::insertCounterUpdate(Instruction *I,
784  StructType *StructTy,
785  unsigned CounterIdx) {
786  GlobalVariable *CounterArray = StructTyMap[StructTy];
787  if (CounterArray == nullptr)
788  return false;
789  IRBuilder<> IRB(I);
790  Constant *Indices[2];
791  // Xref http://llvm.org/docs/LangRef.html#i-getelementptr and
792  // http://llvm.org/docs/GetElementPtr.html.
793  // The first index of the GEP instruction steps through the first operand,
794  // i.e., the array itself.
795  Indices[0] = ConstantInt::get(IRB.getInt32Ty(), 0);
796  // The second index is the index within the array.
797  Indices[1] = ConstantInt::get(IRB.getInt32Ty(), CounterIdx);
798  Constant *Counter =
800  ArrayType::get(IRB.getInt64Ty(), getStructCounterSize(StructTy)),
801  CounterArray, Indices);
802  Value *Load = IRB.CreateLoad(Counter);
803  IRB.CreateStore(IRB.CreateAdd(Load, ConstantInt::get(IRB.getInt64Ty(), 1)),
804  Counter);
805  return true;
806 }
807 
808 int EfficiencySanitizer::getMemoryAccessFuncIndex(Value *Addr,
809  const DataLayout &DL) {
810  Type *OrigPtrTy = Addr->getType();
811  Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
812  assert(OrigTy->isSized());
813  // The size is always a multiple of 8.
814  uint32_t TypeSizeBytes = DL.getTypeStoreSizeInBits(OrigTy) / 8;
815  if (TypeSizeBytes != 1 && TypeSizeBytes != 2 && TypeSizeBytes != 4 &&
816  TypeSizeBytes != 8 && TypeSizeBytes != 16) {
817  // Irregular sizes do not have per-size call targets.
818  NumAccessesWithIrregularSize++;
819  return -1;
820  }
821  size_t Idx = countTrailingZeros(TypeSizeBytes);
822  assert(Idx < NumberOfAccessSizes);
823  return Idx;
824 }
825 
826 bool EfficiencySanitizer::instrumentFastpath(Instruction *I,
827  const DataLayout &DL, bool IsStore,
828  Value *Addr, unsigned Alignment) {
829  if (Options.ToolType == EfficiencySanitizerOptions::ESAN_CacheFrag) {
830  return instrumentFastpathCacheFrag(I, DL, Addr, Alignment);
831  } else if (Options.ToolType == EfficiencySanitizerOptions::ESAN_WorkingSet) {
832  return instrumentFastpathWorkingSet(I, DL, Addr, Alignment);
833  }
834  return false;
835 }
836 
837 bool EfficiencySanitizer::instrumentFastpathCacheFrag(Instruction *I,
838  const DataLayout &DL,
839  Value *Addr,
840  unsigned Alignment) {
841  // Do nothing.
842  return true; // Return true to avoid slowpath instrumentation.
843 }
844 
845 bool EfficiencySanitizer::instrumentFastpathWorkingSet(
846  Instruction *I, const DataLayout &DL, Value *Addr, unsigned Alignment) {
847  assert(ShadowScale[Options.ToolType] == 6); // The code below assumes this
848  IRBuilder<> IRB(I);
849  Type *OrigTy = cast<PointerType>(Addr->getType())->getElementType();
850  const uint32_t TypeSize = DL.getTypeStoreSizeInBits(OrigTy);
851  // Bail to the slowpath if the access might touch multiple cache lines.
852  // An access aligned to its size is guaranteed to be intra-cache-line.
853  // getMemoryAccessFuncIndex has already ruled out a size larger than 16
854  // and thus larger than a cache line for platforms this tool targets
855  // (and our shadow memory setup assumes 64-byte cache lines).
856  assert(TypeSize <= 128);
857  if (!(TypeSize == 8 ||
858  (Alignment % (TypeSize / 8)) == 0)) {
860  ++NumAssumedIntraCacheLine;
861  else
862  return false;
863  }
864 
865  // We inline instrumentation to set the corresponding shadow bits for
866  // each cache line touched by the application. Here we handle a single
867  // load or store where we've already ruled out the possibility that it
868  // might touch more than one cache line and thus we simply update the
869  // shadow memory for a single cache line.
870  // Our shadow memory model is fine with races when manipulating shadow values.
871  // We generate the following code:
872  //
873  // const char BitMask = 0x81;
874  // char *ShadowAddr = appToShadow(AppAddr);
875  // if ((*ShadowAddr & BitMask) != BitMask)
876  // *ShadowAddr |= Bitmask;
877  //
878  Value *AddrPtr = IRB.CreatePointerCast(Addr, IntptrTy);
879  Value *ShadowPtr = appToShadow(AddrPtr, IRB);
880  Type *ShadowTy = IntegerType::get(*Ctx, 8U);
881  Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
882  // The bottom bit is used for the current sampling period's working set.
883  // The top bit is used for the total working set. We set both on each
884  // memory access, if they are not already set.
885  Value *ValueMask = ConstantInt::get(ShadowTy, 0x81); // 10000001B
886 
887  Value *OldValue = IRB.CreateLoad(IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
888  // The AND and CMP will be turned into a TEST instruction by the compiler.
889  Value *Cmp = IRB.CreateICmpNE(IRB.CreateAnd(OldValue, ValueMask), ValueMask);
890  Instruction *CmpTerm = SplitBlockAndInsertIfThen(Cmp, I, false);
891  // FIXME: do I need to call SetCurrentDebugLocation?
892  IRB.SetInsertPoint(CmpTerm);
893  // We use OR to set the shadow bits to avoid corrupting the middle 6 bits,
894  // which are used by the runtime library.
895  Value *NewVal = IRB.CreateOr(OldValue, ValueMask);
896  IRB.CreateStore(NewVal, IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
897  IRB.SetInsertPoint(I);
898 
899  return true;
900 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:68
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
uint64_t getTypeStoreSizeInBits(Type *Ty) const
Returns the maximum number of bits that may be overwritten by storing the specified type; always a mu...
Definition: DataLayout.h:419
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:239
void appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Same as appendToGlobalCtors(), but for global dtors.
Definition: ModuleUtils.cpp:88
enum llvm::EfficiencySanitizerOptions::Type ToolType
Function * checkSanitizerInterfaceFunction(Constant *FuncOrBitcast)
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1752
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:314
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 Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1143
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:265
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
Definition: Instructions.h:518
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:313
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:588
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1309
This class represents a function call, abstracting a target machine&#39;s calling convention.
static PointerType * getInt32PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:228
static cl::opt< bool > ClAssumeIntraCacheLine("esan-assume-intra-cache-line", cl::init(true), cl::desc("Assume each memory access touches just one cache line, for " "better performance but with a potential loss of accuracy."), cl::Hidden)
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:617
static const char *const EsanInitName
Offsets
Offsets in bytes from the start of the input buffer.
Definition: SIInstrInfo.h:972
Externally visible function.
Definition: GlobalValue.h:49
STATISTIC(NumFunctions, "Total number of functions")
F(f)
An instruction for reading from memory.
Definition: Instructions.h:168
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:177
bool isOpaque() const
Return true if this is a type with an identity that has no body specified yet.
Definition: DerivedTypes.h:269
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
Definition: Instructions.h:681
static PointerType * getInt64PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:232
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:960
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, Instruction *InsertBefore=nullptr)
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:347
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
ModulePass * createEfficiencySanitizerPass(const EfficiencySanitizerOptions &Options=EfficiencySanitizerOptions())
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:364
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
IntegerType * getInt64Ty()
Fetch the type representing a 64-bit integer.
Definition: IRBuilder.h:352
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:138
Class to represent struct types.
Definition: DerivedTypes.h:201
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:243
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:974
Type * getSourceElementType() const
Definition: Instructions.h:940
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:336
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1333
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1636
unsigned getNumIndices() const
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
Class to represent array types.
Definition: DerivedTypes.h:369
static const int ShadowScale[]
op_iterator idx_begin()
Definition: Instructions.h:968
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
An instruction for storing to memory.
Definition: Instructions.h:310
static const char *const EsanModuleCtorName
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:127
Value * getOperand(unsigned i) const
Definition: User.h:170
Class to represent pointers.
Definition: DerivedTypes.h:467
static const char *const EsanModuleDtorName
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1142
static const ShadowMemoryParams ShadowParams47
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:843
LoadInst * CreateLoad(Value *Ptr, const char *Name)
Provided to resolve &#39;CreateLoad(Ptr, "...")&#39; correctly, instead of converting the string to &#39;bool&#39; fo...
Definition: IRBuilder.h:1317
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:742
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:410
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition: Function.h:136
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1378
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0&#39;s from the least significant bit to the most stopping at the first 1...
Definition: MathExtras.h:120
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.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
static const char *const EsanWhichToolName
This is an important base class in LLVM.
Definition: Constant.h:42
INITIALIZE_PASS_BEGIN(EfficiencySanitizer, "esan", "EfficiencySanitizer: finds performance issues.", false, false) INITIALIZE_PASS_END(EfficiencySanitizer
unsigned getPrefTypeAlignment(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:732
Represent the analysis usage information of a pass.
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:161
StringRef getName() const
Return the name for this struct type if it has an identity.
Definition: Type.cpp:487
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
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:1021
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:100
Class to represent integer types.
Definition: DerivedTypes.h:40
size_t size() const
Definition: SmallVector.h:53
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:385
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:220
const std::string & getModuleIdentifier() const
Get the module identifier which is, essentially, the name of the module.
Definition: Module.h:209
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:499
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
std::vector< StructType * > getIdentifiedStructTypes() const
Definition: Module.cpp:413
static cl::opt< bool > ClInstrumentFastpath("esan-instrument-fastpath", cl::init(true), cl::desc("Instrument fastpath"), cl::Hidden)
static const uint64_t EsanCtorAndDtorPriority
static Constant * getPointerCast(Constant *C, Type *Ty)
Create a BitCast, AddrSpaceCast, or a PtrToInt cast constant expression.
Definition: Constants.cpp:1564
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:240
This is the common base class for memset/memcpy/memmove.
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:1710
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.
Provides information about what library functions are available for the current target.
uint64_t getSizeInBytes() const
Definition: DataLayout.h:529
static cl::opt< bool > ClInstrumentMemIntrinsics("esan-instrument-memintrinsics", cl::init(true), cl::desc("Instrument memintrinsics (memset/memcpy/memmove)"), cl::Hidden)
static cl::opt< bool > ClToolCacheFrag("esan-cache-frag", cl::init(false), cl::desc("Detect data cache fragmentation"), cl::Hidden)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:621
GlobalVariable * createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, const char *NamePrefix="")
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
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:224
bool isLiteral() const
Return true if this type is uniqued by structural equivalence, false if it is a struct definition...
Definition: DerivedTypes.h:265
static cl::opt< bool > ClToolWorkingSet("esan-working-set", cl::init(false), cl::desc("Measure the working set size"), cl::Hidden)
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1687
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:428
uint64_t getElementOffset(unsigned Idx) const
Definition: DataLayout.h:543
static Type * getIndexedType(Type *Ty, ArrayRef< Value *> IdxList)
Returns the type of the element that would be loaded with a load instruction with the specified param...
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:176
void print(raw_ostream &O, bool IsForDebug=false, bool NoDetails=false) const
Print the current type.
Definition: AsmWriter.cpp:4094
#define I(x, y, z)
Definition: MD5.cpp:58
static cl::opt< bool > ClInstrumentLoadsAndStores("esan-instrument-loads-and-stores", cl::init(true), cl::desc("Instrument loads and stores"), cl::Hidden)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:568
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
uint32_t Size
Definition: Profile.cpp:47
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:53
Rename collisions when linking (static functions).
Definition: GlobalValue.h:56
void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI, const TargetLibraryInfo *TLI)
Given a CallInst, check if it calls a string function known to CodeGen, and mark it with NoBuiltin if...
Definition: Local.cpp:2785
static cl::opt< bool > ClAuxFieldInfo("esan-aux-field-info", cl::init(true), cl::desc("Generate binary with auxiliary struct field information"), cl::Hidden)
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1124
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const ShadowMemoryParams ShadowParams40
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:483
LLVM Value Representation.
Definition: Value.h:73
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1084
Type * getElementType() const
Definition: DerivedTypes.h:360
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static const unsigned MaxStructCounterNameSize
static const char *const EsanExitName
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition: Constants.h:157
bool hasName() const
Return true if this is a named struct that has a non-empty name.
Definition: DerivedTypes.h:275
IntegerType * Int32Ty
CallInst * CreateCall(Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1883