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