1e8d8bef9SDimitry Andric //===- MemProfiler.cpp - memory allocation and access profiler ------------===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric // 9e8d8bef9SDimitry Andric // This file is a part of MemProfiler. Memory accesses are instrumented 10e8d8bef9SDimitry Andric // to increment the access count held in a shadow memory location, or 11e8d8bef9SDimitry Andric // alternatively to call into the runtime. Memory intrinsic calls (memmove, 12e8d8bef9SDimitry Andric // memcpy, memset) are changed to call the memory profiling runtime version 13e8d8bef9SDimitry Andric // instead. 14e8d8bef9SDimitry Andric // 15e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 16e8d8bef9SDimitry Andric 17e8d8bef9SDimitry Andric #include "llvm/Transforms/Instrumentation/MemProfiler.h" 18e8d8bef9SDimitry Andric #include "llvm/ADT/SmallVector.h" 19e8d8bef9SDimitry Andric #include "llvm/ADT/Statistic.h" 20e8d8bef9SDimitry Andric #include "llvm/ADT/StringRef.h" 21e8d8bef9SDimitry Andric #include "llvm/ADT/Triple.h" 22349cc55cSDimitry Andric #include "llvm/Analysis/ValueTracking.h" 23e8d8bef9SDimitry Andric #include "llvm/IR/Constant.h" 24e8d8bef9SDimitry Andric #include "llvm/IR/DataLayout.h" 25e8d8bef9SDimitry Andric #include "llvm/IR/Function.h" 26e8d8bef9SDimitry Andric #include "llvm/IR/GlobalValue.h" 27e8d8bef9SDimitry Andric #include "llvm/IR/IRBuilder.h" 28e8d8bef9SDimitry Andric #include "llvm/IR/Instruction.h" 29*1fd87a68SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 30e8d8bef9SDimitry Andric #include "llvm/IR/LLVMContext.h" 31e8d8bef9SDimitry Andric #include "llvm/IR/Module.h" 32e8d8bef9SDimitry Andric #include "llvm/IR/Type.h" 33e8d8bef9SDimitry Andric #include "llvm/IR/Value.h" 34e8d8bef9SDimitry Andric #include "llvm/InitializePasses.h" 35e8d8bef9SDimitry Andric #include "llvm/Pass.h" 36e8d8bef9SDimitry Andric #include "llvm/Support/CommandLine.h" 37e8d8bef9SDimitry Andric #include "llvm/Support/Debug.h" 38e8d8bef9SDimitry Andric #include "llvm/Transforms/Instrumentation.h" 39e8d8bef9SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h" 40e8d8bef9SDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h" 41e8d8bef9SDimitry Andric 42e8d8bef9SDimitry Andric using namespace llvm; 43e8d8bef9SDimitry Andric 44e8d8bef9SDimitry Andric #define DEBUG_TYPE "memprof" 45e8d8bef9SDimitry Andric 46e8d8bef9SDimitry Andric constexpr int LLVM_MEM_PROFILER_VERSION = 1; 47e8d8bef9SDimitry Andric 48e8d8bef9SDimitry Andric // Size of memory mapped to a single shadow location. 49e8d8bef9SDimitry Andric constexpr uint64_t DefaultShadowGranularity = 64; 50e8d8bef9SDimitry Andric 51e8d8bef9SDimitry Andric // Scale from granularity down to shadow size. 52e8d8bef9SDimitry Andric constexpr uint64_t DefaultShadowScale = 3; 53e8d8bef9SDimitry Andric 54e8d8bef9SDimitry Andric constexpr char MemProfModuleCtorName[] = "memprof.module_ctor"; 55e8d8bef9SDimitry Andric constexpr uint64_t MemProfCtorAndDtorPriority = 1; 56e8d8bef9SDimitry Andric // On Emscripten, the system needs more than one priorities for constructors. 57e8d8bef9SDimitry Andric constexpr uint64_t MemProfEmscriptenCtorAndDtorPriority = 50; 58e8d8bef9SDimitry Andric constexpr char MemProfInitName[] = "__memprof_init"; 59e8d8bef9SDimitry Andric constexpr char MemProfVersionCheckNamePrefix[] = 60e8d8bef9SDimitry Andric "__memprof_version_mismatch_check_v"; 61e8d8bef9SDimitry Andric 62e8d8bef9SDimitry Andric constexpr char MemProfShadowMemoryDynamicAddress[] = 63e8d8bef9SDimitry Andric "__memprof_shadow_memory_dynamic_address"; 64e8d8bef9SDimitry Andric 65e8d8bef9SDimitry Andric constexpr char MemProfFilenameVar[] = "__memprof_profile_filename"; 66e8d8bef9SDimitry Andric 67e8d8bef9SDimitry Andric // Command-line flags. 68e8d8bef9SDimitry Andric 69e8d8bef9SDimitry Andric static cl::opt<bool> ClInsertVersionCheck( 70e8d8bef9SDimitry Andric "memprof-guard-against-version-mismatch", 71e8d8bef9SDimitry Andric cl::desc("Guard against compiler/runtime version mismatch."), cl::Hidden, 72e8d8bef9SDimitry Andric cl::init(true)); 73e8d8bef9SDimitry Andric 74e8d8bef9SDimitry Andric // This flag may need to be replaced with -f[no-]memprof-reads. 75e8d8bef9SDimitry Andric static cl::opt<bool> ClInstrumentReads("memprof-instrument-reads", 76e8d8bef9SDimitry Andric cl::desc("instrument read instructions"), 77e8d8bef9SDimitry Andric cl::Hidden, cl::init(true)); 78e8d8bef9SDimitry Andric 79e8d8bef9SDimitry Andric static cl::opt<bool> 80e8d8bef9SDimitry Andric ClInstrumentWrites("memprof-instrument-writes", 81e8d8bef9SDimitry Andric cl::desc("instrument write instructions"), cl::Hidden, 82e8d8bef9SDimitry Andric cl::init(true)); 83e8d8bef9SDimitry Andric 84e8d8bef9SDimitry Andric static cl::opt<bool> ClInstrumentAtomics( 85e8d8bef9SDimitry Andric "memprof-instrument-atomics", 86e8d8bef9SDimitry Andric cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden, 87e8d8bef9SDimitry Andric cl::init(true)); 88e8d8bef9SDimitry Andric 89e8d8bef9SDimitry Andric static cl::opt<bool> ClUseCalls( 90e8d8bef9SDimitry Andric "memprof-use-callbacks", 91e8d8bef9SDimitry Andric cl::desc("Use callbacks instead of inline instrumentation sequences."), 92e8d8bef9SDimitry Andric cl::Hidden, cl::init(false)); 93e8d8bef9SDimitry Andric 94e8d8bef9SDimitry Andric static cl::opt<std::string> 95e8d8bef9SDimitry Andric ClMemoryAccessCallbackPrefix("memprof-memory-access-callback-prefix", 96e8d8bef9SDimitry Andric cl::desc("Prefix for memory access callbacks"), 97e8d8bef9SDimitry Andric cl::Hidden, cl::init("__memprof_")); 98e8d8bef9SDimitry Andric 99e8d8bef9SDimitry Andric // These flags allow to change the shadow mapping. 100e8d8bef9SDimitry Andric // The shadow mapping looks like 101e8d8bef9SDimitry Andric // Shadow = ((Mem & mask) >> scale) + offset 102e8d8bef9SDimitry Andric 103e8d8bef9SDimitry Andric static cl::opt<int> ClMappingScale("memprof-mapping-scale", 104e8d8bef9SDimitry Andric cl::desc("scale of memprof shadow mapping"), 105e8d8bef9SDimitry Andric cl::Hidden, cl::init(DefaultShadowScale)); 106e8d8bef9SDimitry Andric 107e8d8bef9SDimitry Andric static cl::opt<int> 108e8d8bef9SDimitry Andric ClMappingGranularity("memprof-mapping-granularity", 109e8d8bef9SDimitry Andric cl::desc("granularity of memprof shadow mapping"), 110e8d8bef9SDimitry Andric cl::Hidden, cl::init(DefaultShadowGranularity)); 111e8d8bef9SDimitry Andric 112349cc55cSDimitry Andric static cl::opt<bool> ClStack("memprof-instrument-stack", 113349cc55cSDimitry Andric cl::desc("Instrument scalar stack variables"), 114349cc55cSDimitry Andric cl::Hidden, cl::init(false)); 115349cc55cSDimitry Andric 116e8d8bef9SDimitry Andric // Debug flags. 117e8d8bef9SDimitry Andric 118e8d8bef9SDimitry Andric static cl::opt<int> ClDebug("memprof-debug", cl::desc("debug"), cl::Hidden, 119e8d8bef9SDimitry Andric cl::init(0)); 120e8d8bef9SDimitry Andric 121e8d8bef9SDimitry Andric static cl::opt<std::string> ClDebugFunc("memprof-debug-func", cl::Hidden, 122e8d8bef9SDimitry Andric cl::desc("Debug func")); 123e8d8bef9SDimitry Andric 124e8d8bef9SDimitry Andric static cl::opt<int> ClDebugMin("memprof-debug-min", cl::desc("Debug min inst"), 125e8d8bef9SDimitry Andric cl::Hidden, cl::init(-1)); 126e8d8bef9SDimitry Andric 127e8d8bef9SDimitry Andric static cl::opt<int> ClDebugMax("memprof-debug-max", cl::desc("Debug max inst"), 128e8d8bef9SDimitry Andric cl::Hidden, cl::init(-1)); 129e8d8bef9SDimitry Andric 130e8d8bef9SDimitry Andric STATISTIC(NumInstrumentedReads, "Number of instrumented reads"); 131e8d8bef9SDimitry Andric STATISTIC(NumInstrumentedWrites, "Number of instrumented writes"); 132349cc55cSDimitry Andric STATISTIC(NumSkippedStackReads, "Number of non-instrumented stack reads"); 133349cc55cSDimitry Andric STATISTIC(NumSkippedStackWrites, "Number of non-instrumented stack writes"); 134e8d8bef9SDimitry Andric 135e8d8bef9SDimitry Andric namespace { 136e8d8bef9SDimitry Andric 137e8d8bef9SDimitry Andric /// This struct defines the shadow mapping using the rule: 138e8d8bef9SDimitry Andric /// shadow = ((mem & mask) >> Scale) ADD DynamicShadowOffset. 139e8d8bef9SDimitry Andric struct ShadowMapping { 140e8d8bef9SDimitry Andric ShadowMapping() { 141e8d8bef9SDimitry Andric Scale = ClMappingScale; 142e8d8bef9SDimitry Andric Granularity = ClMappingGranularity; 143e8d8bef9SDimitry Andric Mask = ~(Granularity - 1); 144e8d8bef9SDimitry Andric } 145e8d8bef9SDimitry Andric 146e8d8bef9SDimitry Andric int Scale; 147e8d8bef9SDimitry Andric int Granularity; 148e8d8bef9SDimitry Andric uint64_t Mask; // Computed as ~(Granularity-1) 149e8d8bef9SDimitry Andric }; 150e8d8bef9SDimitry Andric 151e8d8bef9SDimitry Andric static uint64_t getCtorAndDtorPriority(Triple &TargetTriple) { 152e8d8bef9SDimitry Andric return TargetTriple.isOSEmscripten() ? MemProfEmscriptenCtorAndDtorPriority 153e8d8bef9SDimitry Andric : MemProfCtorAndDtorPriority; 154e8d8bef9SDimitry Andric } 155e8d8bef9SDimitry Andric 156e8d8bef9SDimitry Andric struct InterestingMemoryAccess { 157e8d8bef9SDimitry Andric Value *Addr = nullptr; 158e8d8bef9SDimitry Andric bool IsWrite; 159e8d8bef9SDimitry Andric unsigned Alignment; 16004eeddc0SDimitry Andric Type *AccessTy; 161e8d8bef9SDimitry Andric uint64_t TypeSize; 162e8d8bef9SDimitry Andric Value *MaybeMask = nullptr; 163e8d8bef9SDimitry Andric }; 164e8d8bef9SDimitry Andric 165e8d8bef9SDimitry Andric /// Instrument the code in module to profile memory accesses. 166e8d8bef9SDimitry Andric class MemProfiler { 167e8d8bef9SDimitry Andric public: 168e8d8bef9SDimitry Andric MemProfiler(Module &M) { 169e8d8bef9SDimitry Andric C = &(M.getContext()); 170e8d8bef9SDimitry Andric LongSize = M.getDataLayout().getPointerSizeInBits(); 171e8d8bef9SDimitry Andric IntptrTy = Type::getIntNTy(*C, LongSize); 172e8d8bef9SDimitry Andric } 173e8d8bef9SDimitry Andric 174e8d8bef9SDimitry Andric /// If it is an interesting memory access, populate information 175e8d8bef9SDimitry Andric /// about the access and return a InterestingMemoryAccess struct. 176e8d8bef9SDimitry Andric /// Otherwise return None. 177e8d8bef9SDimitry Andric Optional<InterestingMemoryAccess> 178e8d8bef9SDimitry Andric isInterestingMemoryAccess(Instruction *I) const; 179e8d8bef9SDimitry Andric 180e8d8bef9SDimitry Andric void instrumentMop(Instruction *I, const DataLayout &DL, 181e8d8bef9SDimitry Andric InterestingMemoryAccess &Access); 182e8d8bef9SDimitry Andric void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore, 183e8d8bef9SDimitry Andric Value *Addr, uint32_t TypeSize, bool IsWrite); 184e8d8bef9SDimitry Andric void instrumentMaskedLoadOrStore(const DataLayout &DL, Value *Mask, 185e8d8bef9SDimitry Andric Instruction *I, Value *Addr, 18604eeddc0SDimitry Andric unsigned Alignment, Type *AccessTy, 187e8d8bef9SDimitry Andric bool IsWrite); 188e8d8bef9SDimitry Andric void instrumentMemIntrinsic(MemIntrinsic *MI); 189e8d8bef9SDimitry Andric Value *memToShadow(Value *Shadow, IRBuilder<> &IRB); 190e8d8bef9SDimitry Andric bool instrumentFunction(Function &F); 191e8d8bef9SDimitry Andric bool maybeInsertMemProfInitAtFunctionEntry(Function &F); 192e8d8bef9SDimitry Andric bool insertDynamicShadowAtFunctionEntry(Function &F); 193e8d8bef9SDimitry Andric 194e8d8bef9SDimitry Andric private: 195e8d8bef9SDimitry Andric void initializeCallbacks(Module &M); 196e8d8bef9SDimitry Andric 197e8d8bef9SDimitry Andric LLVMContext *C; 198e8d8bef9SDimitry Andric int LongSize; 199e8d8bef9SDimitry Andric Type *IntptrTy; 200e8d8bef9SDimitry Andric ShadowMapping Mapping; 201e8d8bef9SDimitry Andric 202e8d8bef9SDimitry Andric // These arrays is indexed by AccessIsWrite 203e8d8bef9SDimitry Andric FunctionCallee MemProfMemoryAccessCallback[2]; 204e8d8bef9SDimitry Andric FunctionCallee MemProfMemoryAccessCallbackSized[2]; 205e8d8bef9SDimitry Andric 206e8d8bef9SDimitry Andric FunctionCallee MemProfMemmove, MemProfMemcpy, MemProfMemset; 207e8d8bef9SDimitry Andric Value *DynamicShadowOffset = nullptr; 208e8d8bef9SDimitry Andric }; 209e8d8bef9SDimitry Andric 210e8d8bef9SDimitry Andric class MemProfilerLegacyPass : public FunctionPass { 211e8d8bef9SDimitry Andric public: 212e8d8bef9SDimitry Andric static char ID; 213e8d8bef9SDimitry Andric 214e8d8bef9SDimitry Andric explicit MemProfilerLegacyPass() : FunctionPass(ID) { 215e8d8bef9SDimitry Andric initializeMemProfilerLegacyPassPass(*PassRegistry::getPassRegistry()); 216e8d8bef9SDimitry Andric } 217e8d8bef9SDimitry Andric 218e8d8bef9SDimitry Andric StringRef getPassName() const override { return "MemProfilerFunctionPass"; } 219e8d8bef9SDimitry Andric 220e8d8bef9SDimitry Andric bool runOnFunction(Function &F) override { 221e8d8bef9SDimitry Andric MemProfiler Profiler(*F.getParent()); 222e8d8bef9SDimitry Andric return Profiler.instrumentFunction(F); 223e8d8bef9SDimitry Andric } 224e8d8bef9SDimitry Andric }; 225e8d8bef9SDimitry Andric 226e8d8bef9SDimitry Andric class ModuleMemProfiler { 227e8d8bef9SDimitry Andric public: 228e8d8bef9SDimitry Andric ModuleMemProfiler(Module &M) { TargetTriple = Triple(M.getTargetTriple()); } 229e8d8bef9SDimitry Andric 230e8d8bef9SDimitry Andric bool instrumentModule(Module &); 231e8d8bef9SDimitry Andric 232e8d8bef9SDimitry Andric private: 233e8d8bef9SDimitry Andric Triple TargetTriple; 234e8d8bef9SDimitry Andric ShadowMapping Mapping; 235e8d8bef9SDimitry Andric Function *MemProfCtorFunction = nullptr; 236e8d8bef9SDimitry Andric }; 237e8d8bef9SDimitry Andric 238e8d8bef9SDimitry Andric class ModuleMemProfilerLegacyPass : public ModulePass { 239e8d8bef9SDimitry Andric public: 240e8d8bef9SDimitry Andric static char ID; 241e8d8bef9SDimitry Andric 242e8d8bef9SDimitry Andric explicit ModuleMemProfilerLegacyPass() : ModulePass(ID) { 243e8d8bef9SDimitry Andric initializeModuleMemProfilerLegacyPassPass(*PassRegistry::getPassRegistry()); 244e8d8bef9SDimitry Andric } 245e8d8bef9SDimitry Andric 246e8d8bef9SDimitry Andric StringRef getPassName() const override { return "ModuleMemProfiler"; } 247e8d8bef9SDimitry Andric 248e8d8bef9SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {} 249e8d8bef9SDimitry Andric 250e8d8bef9SDimitry Andric bool runOnModule(Module &M) override { 251e8d8bef9SDimitry Andric ModuleMemProfiler MemProfiler(M); 252e8d8bef9SDimitry Andric return MemProfiler.instrumentModule(M); 253e8d8bef9SDimitry Andric } 254e8d8bef9SDimitry Andric }; 255e8d8bef9SDimitry Andric 256e8d8bef9SDimitry Andric } // end anonymous namespace 257e8d8bef9SDimitry Andric 258e8d8bef9SDimitry Andric MemProfilerPass::MemProfilerPass() {} 259e8d8bef9SDimitry Andric 260e8d8bef9SDimitry Andric PreservedAnalyses MemProfilerPass::run(Function &F, 261e8d8bef9SDimitry Andric AnalysisManager<Function> &AM) { 262e8d8bef9SDimitry Andric Module &M = *F.getParent(); 263e8d8bef9SDimitry Andric MemProfiler Profiler(M); 264e8d8bef9SDimitry Andric if (Profiler.instrumentFunction(F)) 265e8d8bef9SDimitry Andric return PreservedAnalyses::none(); 266e8d8bef9SDimitry Andric return PreservedAnalyses::all(); 267e8d8bef9SDimitry Andric } 268e8d8bef9SDimitry Andric 269e8d8bef9SDimitry Andric ModuleMemProfilerPass::ModuleMemProfilerPass() {} 270e8d8bef9SDimitry Andric 271e8d8bef9SDimitry Andric PreservedAnalyses ModuleMemProfilerPass::run(Module &M, 272e8d8bef9SDimitry Andric AnalysisManager<Module> &AM) { 273e8d8bef9SDimitry Andric ModuleMemProfiler Profiler(M); 274e8d8bef9SDimitry Andric if (Profiler.instrumentModule(M)) 275e8d8bef9SDimitry Andric return PreservedAnalyses::none(); 276e8d8bef9SDimitry Andric return PreservedAnalyses::all(); 277e8d8bef9SDimitry Andric } 278e8d8bef9SDimitry Andric 279e8d8bef9SDimitry Andric char MemProfilerLegacyPass::ID = 0; 280e8d8bef9SDimitry Andric 281e8d8bef9SDimitry Andric INITIALIZE_PASS_BEGIN(MemProfilerLegacyPass, "memprof", 282e8d8bef9SDimitry Andric "MemProfiler: profile memory allocations and accesses.", 283e8d8bef9SDimitry Andric false, false) 284e8d8bef9SDimitry Andric INITIALIZE_PASS_END(MemProfilerLegacyPass, "memprof", 285e8d8bef9SDimitry Andric "MemProfiler: profile memory allocations and accesses.", 286e8d8bef9SDimitry Andric false, false) 287e8d8bef9SDimitry Andric 288e8d8bef9SDimitry Andric FunctionPass *llvm::createMemProfilerFunctionPass() { 289e8d8bef9SDimitry Andric return new MemProfilerLegacyPass(); 290e8d8bef9SDimitry Andric } 291e8d8bef9SDimitry Andric 292e8d8bef9SDimitry Andric char ModuleMemProfilerLegacyPass::ID = 0; 293e8d8bef9SDimitry Andric 294e8d8bef9SDimitry Andric INITIALIZE_PASS(ModuleMemProfilerLegacyPass, "memprof-module", 295e8d8bef9SDimitry Andric "MemProfiler: profile memory allocations and accesses." 296e8d8bef9SDimitry Andric "ModulePass", 297e8d8bef9SDimitry Andric false, false) 298e8d8bef9SDimitry Andric 299e8d8bef9SDimitry Andric ModulePass *llvm::createModuleMemProfilerLegacyPassPass() { 300e8d8bef9SDimitry Andric return new ModuleMemProfilerLegacyPass(); 301e8d8bef9SDimitry Andric } 302e8d8bef9SDimitry Andric 303e8d8bef9SDimitry Andric Value *MemProfiler::memToShadow(Value *Shadow, IRBuilder<> &IRB) { 304e8d8bef9SDimitry Andric // (Shadow & mask) >> scale 305e8d8bef9SDimitry Andric Shadow = IRB.CreateAnd(Shadow, Mapping.Mask); 306e8d8bef9SDimitry Andric Shadow = IRB.CreateLShr(Shadow, Mapping.Scale); 307e8d8bef9SDimitry Andric // (Shadow >> scale) | offset 308e8d8bef9SDimitry Andric assert(DynamicShadowOffset); 309e8d8bef9SDimitry Andric return IRB.CreateAdd(Shadow, DynamicShadowOffset); 310e8d8bef9SDimitry Andric } 311e8d8bef9SDimitry Andric 312e8d8bef9SDimitry Andric // Instrument memset/memmove/memcpy 313e8d8bef9SDimitry Andric void MemProfiler::instrumentMemIntrinsic(MemIntrinsic *MI) { 314e8d8bef9SDimitry Andric IRBuilder<> IRB(MI); 315e8d8bef9SDimitry Andric if (isa<MemTransferInst>(MI)) { 316e8d8bef9SDimitry Andric IRB.CreateCall( 317e8d8bef9SDimitry Andric isa<MemMoveInst>(MI) ? MemProfMemmove : MemProfMemcpy, 318e8d8bef9SDimitry Andric {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()), 319e8d8bef9SDimitry Andric IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()), 320e8d8bef9SDimitry Andric IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)}); 321e8d8bef9SDimitry Andric } else if (isa<MemSetInst>(MI)) { 322e8d8bef9SDimitry Andric IRB.CreateCall( 323e8d8bef9SDimitry Andric MemProfMemset, 324e8d8bef9SDimitry Andric {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()), 325e8d8bef9SDimitry Andric IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false), 326e8d8bef9SDimitry Andric IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)}); 327e8d8bef9SDimitry Andric } 328e8d8bef9SDimitry Andric MI->eraseFromParent(); 329e8d8bef9SDimitry Andric } 330e8d8bef9SDimitry Andric 331e8d8bef9SDimitry Andric Optional<InterestingMemoryAccess> 332e8d8bef9SDimitry Andric MemProfiler::isInterestingMemoryAccess(Instruction *I) const { 333e8d8bef9SDimitry Andric // Do not instrument the load fetching the dynamic shadow address. 334e8d8bef9SDimitry Andric if (DynamicShadowOffset == I) 335e8d8bef9SDimitry Andric return None; 336e8d8bef9SDimitry Andric 337e8d8bef9SDimitry Andric InterestingMemoryAccess Access; 338e8d8bef9SDimitry Andric 339e8d8bef9SDimitry Andric if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 340e8d8bef9SDimitry Andric if (!ClInstrumentReads) 341e8d8bef9SDimitry Andric return None; 342e8d8bef9SDimitry Andric Access.IsWrite = false; 34304eeddc0SDimitry Andric Access.AccessTy = LI->getType(); 344e8d8bef9SDimitry Andric Access.Alignment = LI->getAlignment(); 345e8d8bef9SDimitry Andric Access.Addr = LI->getPointerOperand(); 346e8d8bef9SDimitry Andric } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 347e8d8bef9SDimitry Andric if (!ClInstrumentWrites) 348e8d8bef9SDimitry Andric return None; 349e8d8bef9SDimitry Andric Access.IsWrite = true; 35004eeddc0SDimitry Andric Access.AccessTy = SI->getValueOperand()->getType(); 351e8d8bef9SDimitry Andric Access.Alignment = SI->getAlignment(); 352e8d8bef9SDimitry Andric Access.Addr = SI->getPointerOperand(); 353e8d8bef9SDimitry Andric } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) { 354e8d8bef9SDimitry Andric if (!ClInstrumentAtomics) 355e8d8bef9SDimitry Andric return None; 356e8d8bef9SDimitry Andric Access.IsWrite = true; 35704eeddc0SDimitry Andric Access.AccessTy = RMW->getValOperand()->getType(); 358e8d8bef9SDimitry Andric Access.Alignment = 0; 359e8d8bef9SDimitry Andric Access.Addr = RMW->getPointerOperand(); 360e8d8bef9SDimitry Andric } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) { 361e8d8bef9SDimitry Andric if (!ClInstrumentAtomics) 362e8d8bef9SDimitry Andric return None; 363e8d8bef9SDimitry Andric Access.IsWrite = true; 36404eeddc0SDimitry Andric Access.AccessTy = XCHG->getCompareOperand()->getType(); 365e8d8bef9SDimitry Andric Access.Alignment = 0; 366e8d8bef9SDimitry Andric Access.Addr = XCHG->getPointerOperand(); 367e8d8bef9SDimitry Andric } else if (auto *CI = dyn_cast<CallInst>(I)) { 368e8d8bef9SDimitry Andric auto *F = CI->getCalledFunction(); 369e8d8bef9SDimitry Andric if (F && (F->getIntrinsicID() == Intrinsic::masked_load || 370e8d8bef9SDimitry Andric F->getIntrinsicID() == Intrinsic::masked_store)) { 371e8d8bef9SDimitry Andric unsigned OpOffset = 0; 372e8d8bef9SDimitry Andric if (F->getIntrinsicID() == Intrinsic::masked_store) { 373e8d8bef9SDimitry Andric if (!ClInstrumentWrites) 374e8d8bef9SDimitry Andric return None; 375e8d8bef9SDimitry Andric // Masked store has an initial operand for the value. 376e8d8bef9SDimitry Andric OpOffset = 1; 37704eeddc0SDimitry Andric Access.AccessTy = CI->getArgOperand(0)->getType(); 378e8d8bef9SDimitry Andric Access.IsWrite = true; 379e8d8bef9SDimitry Andric } else { 380e8d8bef9SDimitry Andric if (!ClInstrumentReads) 381e8d8bef9SDimitry Andric return None; 38204eeddc0SDimitry Andric Access.AccessTy = CI->getType(); 383e8d8bef9SDimitry Andric Access.IsWrite = false; 384e8d8bef9SDimitry Andric } 385e8d8bef9SDimitry Andric 386e8d8bef9SDimitry Andric auto *BasePtr = CI->getOperand(0 + OpOffset); 387e8d8bef9SDimitry Andric if (auto *AlignmentConstant = 388e8d8bef9SDimitry Andric dyn_cast<ConstantInt>(CI->getOperand(1 + OpOffset))) 389e8d8bef9SDimitry Andric Access.Alignment = (unsigned)AlignmentConstant->getZExtValue(); 390e8d8bef9SDimitry Andric else 391e8d8bef9SDimitry Andric Access.Alignment = 1; // No alignment guarantees. We probably got Undef 392e8d8bef9SDimitry Andric Access.MaybeMask = CI->getOperand(2 + OpOffset); 393e8d8bef9SDimitry Andric Access.Addr = BasePtr; 394e8d8bef9SDimitry Andric } 395e8d8bef9SDimitry Andric } 396e8d8bef9SDimitry Andric 397e8d8bef9SDimitry Andric if (!Access.Addr) 398e8d8bef9SDimitry Andric return None; 399e8d8bef9SDimitry Andric 400e8d8bef9SDimitry Andric // Do not instrument acesses from different address spaces; we cannot deal 401e8d8bef9SDimitry Andric // with them. 402e8d8bef9SDimitry Andric Type *PtrTy = cast<PointerType>(Access.Addr->getType()->getScalarType()); 403e8d8bef9SDimitry Andric if (PtrTy->getPointerAddressSpace() != 0) 404e8d8bef9SDimitry Andric return None; 405e8d8bef9SDimitry Andric 406e8d8bef9SDimitry Andric // Ignore swifterror addresses. 407e8d8bef9SDimitry Andric // swifterror memory addresses are mem2reg promoted by instruction 408e8d8bef9SDimitry Andric // selection. As such they cannot have regular uses like an instrumentation 409e8d8bef9SDimitry Andric // function and it makes no sense to track them as memory. 410e8d8bef9SDimitry Andric if (Access.Addr->isSwiftError()) 411e8d8bef9SDimitry Andric return None; 412e8d8bef9SDimitry Andric 41304eeddc0SDimitry Andric const DataLayout &DL = I->getModule()->getDataLayout(); 41404eeddc0SDimitry Andric Access.TypeSize = DL.getTypeStoreSizeInBits(Access.AccessTy); 415e8d8bef9SDimitry Andric return Access; 416e8d8bef9SDimitry Andric } 417e8d8bef9SDimitry Andric 418e8d8bef9SDimitry Andric void MemProfiler::instrumentMaskedLoadOrStore(const DataLayout &DL, Value *Mask, 419e8d8bef9SDimitry Andric Instruction *I, Value *Addr, 420e8d8bef9SDimitry Andric unsigned Alignment, 42104eeddc0SDimitry Andric Type *AccessTy, bool IsWrite) { 42204eeddc0SDimitry Andric auto *VTy = cast<FixedVectorType>(AccessTy); 423e8d8bef9SDimitry Andric uint64_t ElemTypeSize = DL.getTypeStoreSizeInBits(VTy->getScalarType()); 424e8d8bef9SDimitry Andric unsigned Num = VTy->getNumElements(); 425e8d8bef9SDimitry Andric auto *Zero = ConstantInt::get(IntptrTy, 0); 426e8d8bef9SDimitry Andric for (unsigned Idx = 0; Idx < Num; ++Idx) { 427e8d8bef9SDimitry Andric Value *InstrumentedAddress = nullptr; 428e8d8bef9SDimitry Andric Instruction *InsertBefore = I; 429e8d8bef9SDimitry Andric if (auto *Vector = dyn_cast<ConstantVector>(Mask)) { 430e8d8bef9SDimitry Andric // dyn_cast as we might get UndefValue 431e8d8bef9SDimitry Andric if (auto *Masked = dyn_cast<ConstantInt>(Vector->getOperand(Idx))) { 432e8d8bef9SDimitry Andric if (Masked->isZero()) 433e8d8bef9SDimitry Andric // Mask is constant false, so no instrumentation needed. 434e8d8bef9SDimitry Andric continue; 435e8d8bef9SDimitry Andric // If we have a true or undef value, fall through to instrumentAddress. 436e8d8bef9SDimitry Andric // with InsertBefore == I 437e8d8bef9SDimitry Andric } 438e8d8bef9SDimitry Andric } else { 439e8d8bef9SDimitry Andric IRBuilder<> IRB(I); 440e8d8bef9SDimitry Andric Value *MaskElem = IRB.CreateExtractElement(Mask, Idx); 441e8d8bef9SDimitry Andric Instruction *ThenTerm = SplitBlockAndInsertIfThen(MaskElem, I, false); 442e8d8bef9SDimitry Andric InsertBefore = ThenTerm; 443e8d8bef9SDimitry Andric } 444e8d8bef9SDimitry Andric 445e8d8bef9SDimitry Andric IRBuilder<> IRB(InsertBefore); 446e8d8bef9SDimitry Andric InstrumentedAddress = 447e8d8bef9SDimitry Andric IRB.CreateGEP(VTy, Addr, {Zero, ConstantInt::get(IntptrTy, Idx)}); 448e8d8bef9SDimitry Andric instrumentAddress(I, InsertBefore, InstrumentedAddress, ElemTypeSize, 449e8d8bef9SDimitry Andric IsWrite); 450e8d8bef9SDimitry Andric } 451e8d8bef9SDimitry Andric } 452e8d8bef9SDimitry Andric 453e8d8bef9SDimitry Andric void MemProfiler::instrumentMop(Instruction *I, const DataLayout &DL, 454e8d8bef9SDimitry Andric InterestingMemoryAccess &Access) { 455349cc55cSDimitry Andric // Skip instrumentation of stack accesses unless requested. 456349cc55cSDimitry Andric if (!ClStack && isa<AllocaInst>(getUnderlyingObject(Access.Addr))) { 457349cc55cSDimitry Andric if (Access.IsWrite) 458349cc55cSDimitry Andric ++NumSkippedStackWrites; 459349cc55cSDimitry Andric else 460349cc55cSDimitry Andric ++NumSkippedStackReads; 461349cc55cSDimitry Andric return; 462349cc55cSDimitry Andric } 463349cc55cSDimitry Andric 464e8d8bef9SDimitry Andric if (Access.IsWrite) 465e8d8bef9SDimitry Andric NumInstrumentedWrites++; 466e8d8bef9SDimitry Andric else 467e8d8bef9SDimitry Andric NumInstrumentedReads++; 468e8d8bef9SDimitry Andric 469e8d8bef9SDimitry Andric if (Access.MaybeMask) { 470e8d8bef9SDimitry Andric instrumentMaskedLoadOrStore(DL, Access.MaybeMask, I, Access.Addr, 47104eeddc0SDimitry Andric Access.Alignment, Access.AccessTy, 472e8d8bef9SDimitry Andric Access.IsWrite); 473e8d8bef9SDimitry Andric } else { 474e8d8bef9SDimitry Andric // Since the access counts will be accumulated across the entire allocation, 475e8d8bef9SDimitry Andric // we only update the shadow access count for the first location and thus 476e8d8bef9SDimitry Andric // don't need to worry about alignment and type size. 477e8d8bef9SDimitry Andric instrumentAddress(I, I, Access.Addr, Access.TypeSize, Access.IsWrite); 478e8d8bef9SDimitry Andric } 479e8d8bef9SDimitry Andric } 480e8d8bef9SDimitry Andric 481e8d8bef9SDimitry Andric void MemProfiler::instrumentAddress(Instruction *OrigIns, 482e8d8bef9SDimitry Andric Instruction *InsertBefore, Value *Addr, 483e8d8bef9SDimitry Andric uint32_t TypeSize, bool IsWrite) { 484e8d8bef9SDimitry Andric IRBuilder<> IRB(InsertBefore); 485e8d8bef9SDimitry Andric Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); 486e8d8bef9SDimitry Andric 487e8d8bef9SDimitry Andric if (ClUseCalls) { 488e8d8bef9SDimitry Andric IRB.CreateCall(MemProfMemoryAccessCallback[IsWrite], AddrLong); 489e8d8bef9SDimitry Andric return; 490e8d8bef9SDimitry Andric } 491e8d8bef9SDimitry Andric 492e8d8bef9SDimitry Andric // Create an inline sequence to compute shadow location, and increment the 493e8d8bef9SDimitry Andric // value by one. 494e8d8bef9SDimitry Andric Type *ShadowTy = Type::getInt64Ty(*C); 495e8d8bef9SDimitry Andric Type *ShadowPtrTy = PointerType::get(ShadowTy, 0); 496e8d8bef9SDimitry Andric Value *ShadowPtr = memToShadow(AddrLong, IRB); 497e8d8bef9SDimitry Andric Value *ShadowAddr = IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy); 498e8d8bef9SDimitry Andric Value *ShadowValue = IRB.CreateLoad(ShadowTy, ShadowAddr); 499e8d8bef9SDimitry Andric Value *Inc = ConstantInt::get(Type::getInt64Ty(*C), 1); 500e8d8bef9SDimitry Andric ShadowValue = IRB.CreateAdd(ShadowValue, Inc); 501e8d8bef9SDimitry Andric IRB.CreateStore(ShadowValue, ShadowAddr); 502e8d8bef9SDimitry Andric } 503e8d8bef9SDimitry Andric 504e8d8bef9SDimitry Andric // Create the variable for the profile file name. 505e8d8bef9SDimitry Andric void createProfileFileNameVar(Module &M) { 506e8d8bef9SDimitry Andric const MDString *MemProfFilename = 507e8d8bef9SDimitry Andric dyn_cast_or_null<MDString>(M.getModuleFlag("MemProfProfileFilename")); 508e8d8bef9SDimitry Andric if (!MemProfFilename) 509e8d8bef9SDimitry Andric return; 510e8d8bef9SDimitry Andric assert(!MemProfFilename->getString().empty() && 511e8d8bef9SDimitry Andric "Unexpected MemProfProfileFilename metadata with empty string"); 512e8d8bef9SDimitry Andric Constant *ProfileNameConst = ConstantDataArray::getString( 513e8d8bef9SDimitry Andric M.getContext(), MemProfFilename->getString(), true); 514e8d8bef9SDimitry Andric GlobalVariable *ProfileNameVar = new GlobalVariable( 515e8d8bef9SDimitry Andric M, ProfileNameConst->getType(), /*isConstant=*/true, 516e8d8bef9SDimitry Andric GlobalValue::WeakAnyLinkage, ProfileNameConst, MemProfFilenameVar); 517e8d8bef9SDimitry Andric Triple TT(M.getTargetTriple()); 518e8d8bef9SDimitry Andric if (TT.supportsCOMDAT()) { 519e8d8bef9SDimitry Andric ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage); 520e8d8bef9SDimitry Andric ProfileNameVar->setComdat(M.getOrInsertComdat(MemProfFilenameVar)); 521e8d8bef9SDimitry Andric } 522e8d8bef9SDimitry Andric } 523e8d8bef9SDimitry Andric 524e8d8bef9SDimitry Andric bool ModuleMemProfiler::instrumentModule(Module &M) { 525e8d8bef9SDimitry Andric // Create a module constructor. 526e8d8bef9SDimitry Andric std::string MemProfVersion = std::to_string(LLVM_MEM_PROFILER_VERSION); 527e8d8bef9SDimitry Andric std::string VersionCheckName = 528e8d8bef9SDimitry Andric ClInsertVersionCheck ? (MemProfVersionCheckNamePrefix + MemProfVersion) 529e8d8bef9SDimitry Andric : ""; 530e8d8bef9SDimitry Andric std::tie(MemProfCtorFunction, std::ignore) = 531e8d8bef9SDimitry Andric createSanitizerCtorAndInitFunctions(M, MemProfModuleCtorName, 532e8d8bef9SDimitry Andric MemProfInitName, /*InitArgTypes=*/{}, 533e8d8bef9SDimitry Andric /*InitArgs=*/{}, VersionCheckName); 534e8d8bef9SDimitry Andric 535e8d8bef9SDimitry Andric const uint64_t Priority = getCtorAndDtorPriority(TargetTriple); 536e8d8bef9SDimitry Andric appendToGlobalCtors(M, MemProfCtorFunction, Priority); 537e8d8bef9SDimitry Andric 538e8d8bef9SDimitry Andric createProfileFileNameVar(M); 539e8d8bef9SDimitry Andric 540e8d8bef9SDimitry Andric return true; 541e8d8bef9SDimitry Andric } 542e8d8bef9SDimitry Andric 543e8d8bef9SDimitry Andric void MemProfiler::initializeCallbacks(Module &M) { 544e8d8bef9SDimitry Andric IRBuilder<> IRB(*C); 545e8d8bef9SDimitry Andric 546e8d8bef9SDimitry Andric for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) { 547e8d8bef9SDimitry Andric const std::string TypeStr = AccessIsWrite ? "store" : "load"; 548e8d8bef9SDimitry Andric 549e8d8bef9SDimitry Andric SmallVector<Type *, 3> Args2 = {IntptrTy, IntptrTy}; 550e8d8bef9SDimitry Andric SmallVector<Type *, 2> Args1{1, IntptrTy}; 551e8d8bef9SDimitry Andric MemProfMemoryAccessCallbackSized[AccessIsWrite] = 552e8d8bef9SDimitry Andric M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + TypeStr + "N", 553e8d8bef9SDimitry Andric FunctionType::get(IRB.getVoidTy(), Args2, false)); 554e8d8bef9SDimitry Andric 555e8d8bef9SDimitry Andric MemProfMemoryAccessCallback[AccessIsWrite] = 556e8d8bef9SDimitry Andric M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + TypeStr, 557e8d8bef9SDimitry Andric FunctionType::get(IRB.getVoidTy(), Args1, false)); 558e8d8bef9SDimitry Andric } 559e8d8bef9SDimitry Andric MemProfMemmove = M.getOrInsertFunction( 560e8d8bef9SDimitry Andric ClMemoryAccessCallbackPrefix + "memmove", IRB.getInt8PtrTy(), 561e8d8bef9SDimitry Andric IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy); 562e8d8bef9SDimitry Andric MemProfMemcpy = M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "memcpy", 563e8d8bef9SDimitry Andric IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), 564e8d8bef9SDimitry Andric IRB.getInt8PtrTy(), IntptrTy); 565e8d8bef9SDimitry Andric MemProfMemset = M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "memset", 566e8d8bef9SDimitry Andric IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), 567e8d8bef9SDimitry Andric IRB.getInt32Ty(), IntptrTy); 568e8d8bef9SDimitry Andric } 569e8d8bef9SDimitry Andric 570e8d8bef9SDimitry Andric bool MemProfiler::maybeInsertMemProfInitAtFunctionEntry(Function &F) { 571e8d8bef9SDimitry Andric // For each NSObject descendant having a +load method, this method is invoked 572e8d8bef9SDimitry Andric // by the ObjC runtime before any of the static constructors is called. 573e8d8bef9SDimitry Andric // Therefore we need to instrument such methods with a call to __memprof_init 574e8d8bef9SDimitry Andric // at the beginning in order to initialize our runtime before any access to 575e8d8bef9SDimitry Andric // the shadow memory. 576e8d8bef9SDimitry Andric // We cannot just ignore these methods, because they may call other 577e8d8bef9SDimitry Andric // instrumented functions. 578e8d8bef9SDimitry Andric if (F.getName().find(" load]") != std::string::npos) { 579e8d8bef9SDimitry Andric FunctionCallee MemProfInitFunction = 580e8d8bef9SDimitry Andric declareSanitizerInitFunction(*F.getParent(), MemProfInitName, {}); 581e8d8bef9SDimitry Andric IRBuilder<> IRB(&F.front(), F.front().begin()); 582e8d8bef9SDimitry Andric IRB.CreateCall(MemProfInitFunction, {}); 583e8d8bef9SDimitry Andric return true; 584e8d8bef9SDimitry Andric } 585e8d8bef9SDimitry Andric return false; 586e8d8bef9SDimitry Andric } 587e8d8bef9SDimitry Andric 588e8d8bef9SDimitry Andric bool MemProfiler::insertDynamicShadowAtFunctionEntry(Function &F) { 589e8d8bef9SDimitry Andric IRBuilder<> IRB(&F.front().front()); 590e8d8bef9SDimitry Andric Value *GlobalDynamicAddress = F.getParent()->getOrInsertGlobal( 591e8d8bef9SDimitry Andric MemProfShadowMemoryDynamicAddress, IntptrTy); 592e8d8bef9SDimitry Andric if (F.getParent()->getPICLevel() == PICLevel::NotPIC) 593e8d8bef9SDimitry Andric cast<GlobalVariable>(GlobalDynamicAddress)->setDSOLocal(true); 594e8d8bef9SDimitry Andric DynamicShadowOffset = IRB.CreateLoad(IntptrTy, GlobalDynamicAddress); 595e8d8bef9SDimitry Andric return true; 596e8d8bef9SDimitry Andric } 597e8d8bef9SDimitry Andric 598e8d8bef9SDimitry Andric bool MemProfiler::instrumentFunction(Function &F) { 599e8d8bef9SDimitry Andric if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) 600e8d8bef9SDimitry Andric return false; 601e8d8bef9SDimitry Andric if (ClDebugFunc == F.getName()) 602e8d8bef9SDimitry Andric return false; 603e8d8bef9SDimitry Andric if (F.getName().startswith("__memprof_")) 604e8d8bef9SDimitry Andric return false; 605e8d8bef9SDimitry Andric 606e8d8bef9SDimitry Andric bool FunctionModified = false; 607e8d8bef9SDimitry Andric 608e8d8bef9SDimitry Andric // If needed, insert __memprof_init. 609e8d8bef9SDimitry Andric // This function needs to be called even if the function body is not 610e8d8bef9SDimitry Andric // instrumented. 611e8d8bef9SDimitry Andric if (maybeInsertMemProfInitAtFunctionEntry(F)) 612e8d8bef9SDimitry Andric FunctionModified = true; 613e8d8bef9SDimitry Andric 614e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "MEMPROF instrumenting:\n" << F << "\n"); 615e8d8bef9SDimitry Andric 616e8d8bef9SDimitry Andric initializeCallbacks(*F.getParent()); 617e8d8bef9SDimitry Andric 618e8d8bef9SDimitry Andric FunctionModified |= insertDynamicShadowAtFunctionEntry(F); 619e8d8bef9SDimitry Andric 620e8d8bef9SDimitry Andric SmallVector<Instruction *, 16> ToInstrument; 621e8d8bef9SDimitry Andric 622e8d8bef9SDimitry Andric // Fill the set of memory operations to instrument. 623e8d8bef9SDimitry Andric for (auto &BB : F) { 624e8d8bef9SDimitry Andric for (auto &Inst : BB) { 625e8d8bef9SDimitry Andric if (isInterestingMemoryAccess(&Inst) || isa<MemIntrinsic>(Inst)) 626e8d8bef9SDimitry Andric ToInstrument.push_back(&Inst); 627e8d8bef9SDimitry Andric } 628e8d8bef9SDimitry Andric } 629e8d8bef9SDimitry Andric 630e8d8bef9SDimitry Andric int NumInstrumented = 0; 631e8d8bef9SDimitry Andric for (auto *Inst : ToInstrument) { 632e8d8bef9SDimitry Andric if (ClDebugMin < 0 || ClDebugMax < 0 || 633e8d8bef9SDimitry Andric (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) { 634e8d8bef9SDimitry Andric Optional<InterestingMemoryAccess> Access = 635e8d8bef9SDimitry Andric isInterestingMemoryAccess(Inst); 636e8d8bef9SDimitry Andric if (Access) 637e8d8bef9SDimitry Andric instrumentMop(Inst, F.getParent()->getDataLayout(), *Access); 638e8d8bef9SDimitry Andric else 639e8d8bef9SDimitry Andric instrumentMemIntrinsic(cast<MemIntrinsic>(Inst)); 640e8d8bef9SDimitry Andric } 641e8d8bef9SDimitry Andric NumInstrumented++; 642e8d8bef9SDimitry Andric } 643e8d8bef9SDimitry Andric 644e8d8bef9SDimitry Andric if (NumInstrumented > 0) 645e8d8bef9SDimitry Andric FunctionModified = true; 646e8d8bef9SDimitry Andric 647e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "MEMPROF done instrumenting: " << FunctionModified << " " 648e8d8bef9SDimitry Andric << F << "\n"); 649e8d8bef9SDimitry Andric 650e8d8bef9SDimitry Andric return FunctionModified; 651e8d8bef9SDimitry Andric } 652