1226d80ebSTeresa Johnson //===- MemProfiler.cpp - memory allocation and access profiler ------------===//
2226d80ebSTeresa Johnson //
3226d80ebSTeresa Johnson // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4226d80ebSTeresa Johnson // See https://llvm.org/LICENSE.txt for license information.
5226d80ebSTeresa Johnson // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6226d80ebSTeresa Johnson //
7226d80ebSTeresa Johnson //===----------------------------------------------------------------------===//
8226d80ebSTeresa Johnson //
9226d80ebSTeresa Johnson // This file is a part of MemProfiler. Memory accesses are instrumented
10226d80ebSTeresa Johnson // to increment the access count held in a shadow memory location, or
11226d80ebSTeresa Johnson // alternatively to call into the runtime. Memory intrinsic calls (memmove,
12226d80ebSTeresa Johnson // memcpy, memset) are changed to call the memory profiling runtime version
13226d80ebSTeresa Johnson // instead.
14226d80ebSTeresa Johnson //
15226d80ebSTeresa Johnson //===----------------------------------------------------------------------===//
16226d80ebSTeresa Johnson
17226d80ebSTeresa Johnson #include "llvm/Transforms/Instrumentation/MemProfiler.h"
18226d80ebSTeresa Johnson #include "llvm/ADT/SmallVector.h"
19226d80ebSTeresa Johnson #include "llvm/ADT/Statistic.h"
20226d80ebSTeresa Johnson #include "llvm/ADT/StringRef.h"
21226d80ebSTeresa Johnson #include "llvm/ADT/Triple.h"
2288cb3e2cSTeresa Johnson #include "llvm/Analysis/ValueTracking.h"
23226d80ebSTeresa Johnson #include "llvm/IR/Constant.h"
24226d80ebSTeresa Johnson #include "llvm/IR/DataLayout.h"
25226d80ebSTeresa Johnson #include "llvm/IR/Function.h"
26226d80ebSTeresa Johnson #include "llvm/IR/GlobalValue.h"
27226d80ebSTeresa Johnson #include "llvm/IR/IRBuilder.h"
28226d80ebSTeresa Johnson #include "llvm/IR/Instruction.h"
29e188aae4Sserge-sans-paille #include "llvm/IR/IntrinsicInst.h"
30226d80ebSTeresa Johnson #include "llvm/IR/Module.h"
31226d80ebSTeresa Johnson #include "llvm/IR/Type.h"
32226d80ebSTeresa Johnson #include "llvm/IR/Value.h"
33226d80ebSTeresa Johnson #include "llvm/InitializePasses.h"
34226d80ebSTeresa Johnson #include "llvm/Pass.h"
35a0b5af46STeresa Johnson #include "llvm/ProfileData/InstrProf.h"
36226d80ebSTeresa Johnson #include "llvm/Support/CommandLine.h"
37226d80ebSTeresa Johnson #include "llvm/Support/Debug.h"
38226d80ebSTeresa Johnson #include "llvm/Transforms/Utils/BasicBlockUtils.h"
39226d80ebSTeresa Johnson #include "llvm/Transforms/Utils/ModuleUtils.h"
40226d80ebSTeresa Johnson
41226d80ebSTeresa Johnson using namespace llvm;
42226d80ebSTeresa Johnson
43226d80ebSTeresa Johnson #define DEBUG_TYPE "memprof"
44226d80ebSTeresa Johnson
45226d80ebSTeresa Johnson constexpr int LLVM_MEM_PROFILER_VERSION = 1;
46226d80ebSTeresa Johnson
47226d80ebSTeresa Johnson // Size of memory mapped to a single shadow location.
48226d80ebSTeresa Johnson constexpr uint64_t DefaultShadowGranularity = 64;
49226d80ebSTeresa Johnson
50226d80ebSTeresa Johnson // Scale from granularity down to shadow size.
51226d80ebSTeresa Johnson constexpr uint64_t DefaultShadowScale = 3;
52226d80ebSTeresa Johnson
53226d80ebSTeresa Johnson constexpr char MemProfModuleCtorName[] = "memprof.module_ctor";
54226d80ebSTeresa Johnson constexpr uint64_t MemProfCtorAndDtorPriority = 1;
55226d80ebSTeresa Johnson // On Emscripten, the system needs more than one priorities for constructors.
56226d80ebSTeresa Johnson constexpr uint64_t MemProfEmscriptenCtorAndDtorPriority = 50;
57226d80ebSTeresa Johnson constexpr char MemProfInitName[] = "__memprof_init";
58226d80ebSTeresa Johnson constexpr char MemProfVersionCheckNamePrefix[] =
59226d80ebSTeresa Johnson "__memprof_version_mismatch_check_v";
60226d80ebSTeresa Johnson
61226d80ebSTeresa Johnson constexpr char MemProfShadowMemoryDynamicAddress[] =
62226d80ebSTeresa Johnson "__memprof_shadow_memory_dynamic_address";
63226d80ebSTeresa Johnson
640949f96dSTeresa Johnson constexpr char MemProfFilenameVar[] = "__memprof_profile_filename";
650949f96dSTeresa Johnson
66226d80ebSTeresa Johnson // Command-line flags.
67226d80ebSTeresa Johnson
68226d80ebSTeresa Johnson static cl::opt<bool> ClInsertVersionCheck(
69226d80ebSTeresa Johnson "memprof-guard-against-version-mismatch",
70226d80ebSTeresa Johnson cl::desc("Guard against compiler/runtime version mismatch."), cl::Hidden,
71226d80ebSTeresa Johnson cl::init(true));
72226d80ebSTeresa Johnson
73226d80ebSTeresa Johnson // This flag may need to be replaced with -f[no-]memprof-reads.
74226d80ebSTeresa Johnson static cl::opt<bool> ClInstrumentReads("memprof-instrument-reads",
75226d80ebSTeresa Johnson cl::desc("instrument read instructions"),
76226d80ebSTeresa Johnson cl::Hidden, cl::init(true));
77226d80ebSTeresa Johnson
78226d80ebSTeresa Johnson static cl::opt<bool>
79226d80ebSTeresa Johnson ClInstrumentWrites("memprof-instrument-writes",
80226d80ebSTeresa Johnson cl::desc("instrument write instructions"), cl::Hidden,
81226d80ebSTeresa Johnson cl::init(true));
82226d80ebSTeresa Johnson
83226d80ebSTeresa Johnson static cl::opt<bool> ClInstrumentAtomics(
84226d80ebSTeresa Johnson "memprof-instrument-atomics",
85226d80ebSTeresa Johnson cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden,
86226d80ebSTeresa Johnson cl::init(true));
87226d80ebSTeresa Johnson
88226d80ebSTeresa Johnson static cl::opt<bool> ClUseCalls(
89226d80ebSTeresa Johnson "memprof-use-callbacks",
90226d80ebSTeresa Johnson cl::desc("Use callbacks instead of inline instrumentation sequences."),
91226d80ebSTeresa Johnson cl::Hidden, cl::init(false));
92226d80ebSTeresa Johnson
93226d80ebSTeresa Johnson static cl::opt<std::string>
94226d80ebSTeresa Johnson ClMemoryAccessCallbackPrefix("memprof-memory-access-callback-prefix",
95226d80ebSTeresa Johnson cl::desc("Prefix for memory access callbacks"),
96226d80ebSTeresa Johnson cl::Hidden, cl::init("__memprof_"));
97226d80ebSTeresa Johnson
98226d80ebSTeresa Johnson // These flags allow to change the shadow mapping.
99226d80ebSTeresa Johnson // The shadow mapping looks like
100226d80ebSTeresa Johnson // Shadow = ((Mem & mask) >> scale) + offset
101226d80ebSTeresa Johnson
102226d80ebSTeresa Johnson static cl::opt<int> ClMappingScale("memprof-mapping-scale",
103226d80ebSTeresa Johnson cl::desc("scale of memprof shadow mapping"),
104226d80ebSTeresa Johnson cl::Hidden, cl::init(DefaultShadowScale));
105226d80ebSTeresa Johnson
106226d80ebSTeresa Johnson static cl::opt<int>
107226d80ebSTeresa Johnson ClMappingGranularity("memprof-mapping-granularity",
108226d80ebSTeresa Johnson cl::desc("granularity of memprof shadow mapping"),
109226d80ebSTeresa Johnson cl::Hidden, cl::init(DefaultShadowGranularity));
110226d80ebSTeresa Johnson
11188cb3e2cSTeresa Johnson static cl::opt<bool> ClStack("memprof-instrument-stack",
11288cb3e2cSTeresa Johnson cl::desc("Instrument scalar stack variables"),
11388cb3e2cSTeresa Johnson cl::Hidden, cl::init(false));
11488cb3e2cSTeresa Johnson
115226d80ebSTeresa Johnson // Debug flags.
116226d80ebSTeresa Johnson
117226d80ebSTeresa Johnson static cl::opt<int> ClDebug("memprof-debug", cl::desc("debug"), cl::Hidden,
118226d80ebSTeresa Johnson cl::init(0));
119226d80ebSTeresa Johnson
120226d80ebSTeresa Johnson static cl::opt<std::string> ClDebugFunc("memprof-debug-func", cl::Hidden,
121226d80ebSTeresa Johnson cl::desc("Debug func"));
122226d80ebSTeresa Johnson
123226d80ebSTeresa Johnson static cl::opt<int> ClDebugMin("memprof-debug-min", cl::desc("Debug min inst"),
124226d80ebSTeresa Johnson cl::Hidden, cl::init(-1));
125226d80ebSTeresa Johnson
126226d80ebSTeresa Johnson static cl::opt<int> ClDebugMax("memprof-debug-max", cl::desc("Debug max inst"),
127226d80ebSTeresa Johnson cl::Hidden, cl::init(-1));
128226d80ebSTeresa Johnson
129226d80ebSTeresa Johnson STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
130226d80ebSTeresa Johnson STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
13188cb3e2cSTeresa Johnson STATISTIC(NumSkippedStackReads, "Number of non-instrumented stack reads");
13288cb3e2cSTeresa Johnson STATISTIC(NumSkippedStackWrites, "Number of non-instrumented stack writes");
133226d80ebSTeresa Johnson
134226d80ebSTeresa Johnson namespace {
135226d80ebSTeresa Johnson
136226d80ebSTeresa Johnson /// This struct defines the shadow mapping using the rule:
137226d80ebSTeresa Johnson /// shadow = ((mem & mask) >> Scale) ADD DynamicShadowOffset.
138226d80ebSTeresa Johnson struct ShadowMapping {
ShadowMapping__anondc9a72430111::ShadowMapping139226d80ebSTeresa Johnson ShadowMapping() {
140226d80ebSTeresa Johnson Scale = ClMappingScale;
141226d80ebSTeresa Johnson Granularity = ClMappingGranularity;
142226d80ebSTeresa Johnson Mask = ~(Granularity - 1);
143226d80ebSTeresa Johnson }
144226d80ebSTeresa Johnson
145226d80ebSTeresa Johnson int Scale;
146226d80ebSTeresa Johnson int Granularity;
147226d80ebSTeresa Johnson uint64_t Mask; // Computed as ~(Granularity-1)
148226d80ebSTeresa Johnson };
149226d80ebSTeresa Johnson
getCtorAndDtorPriority(Triple & TargetTriple)150226d80ebSTeresa Johnson static uint64_t getCtorAndDtorPriority(Triple &TargetTriple) {
151226d80ebSTeresa Johnson return TargetTriple.isOSEmscripten() ? MemProfEmscriptenCtorAndDtorPriority
152226d80ebSTeresa Johnson : MemProfCtorAndDtorPriority;
153226d80ebSTeresa Johnson }
154226d80ebSTeresa Johnson
155226d80ebSTeresa Johnson struct InterestingMemoryAccess {
156226d80ebSTeresa Johnson Value *Addr = nullptr;
157226d80ebSTeresa Johnson bool IsWrite;
1587cc3e141SNikita Popov Type *AccessTy;
159226d80ebSTeresa Johnson uint64_t TypeSize;
160226d80ebSTeresa Johnson Value *MaybeMask = nullptr;
161226d80ebSTeresa Johnson };
162226d80ebSTeresa Johnson
163226d80ebSTeresa Johnson /// Instrument the code in module to profile memory accesses.
164226d80ebSTeresa Johnson class MemProfiler {
165226d80ebSTeresa Johnson public:
MemProfiler(Module & M)166226d80ebSTeresa Johnson MemProfiler(Module &M) {
167226d80ebSTeresa Johnson C = &(M.getContext());
168226d80ebSTeresa Johnson LongSize = M.getDataLayout().getPointerSizeInBits();
169226d80ebSTeresa Johnson IntptrTy = Type::getIntNTy(*C, LongSize);
170226d80ebSTeresa Johnson }
171226d80ebSTeresa Johnson
172226d80ebSTeresa Johnson /// If it is an interesting memory access, populate information
173226d80ebSTeresa Johnson /// about the access and return a InterestingMemoryAccess struct.
174226d80ebSTeresa Johnson /// Otherwise return None.
175226d80ebSTeresa Johnson Optional<InterestingMemoryAccess>
176226d80ebSTeresa Johnson isInterestingMemoryAccess(Instruction *I) const;
177226d80ebSTeresa Johnson
178226d80ebSTeresa Johnson void instrumentMop(Instruction *I, const DataLayout &DL,
179226d80ebSTeresa Johnson InterestingMemoryAccess &Access);
180226d80ebSTeresa Johnson void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore,
181226d80ebSTeresa Johnson Value *Addr, uint32_t TypeSize, bool IsWrite);
182226d80ebSTeresa Johnson void instrumentMaskedLoadOrStore(const DataLayout &DL, Value *Mask,
183*86f45575SGuillaume Chatelet Instruction *I, Value *Addr, Type *AccessTy,
184226d80ebSTeresa Johnson bool IsWrite);
185226d80ebSTeresa Johnson void instrumentMemIntrinsic(MemIntrinsic *MI);
186226d80ebSTeresa Johnson Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
187226d80ebSTeresa Johnson bool instrumentFunction(Function &F);
188226d80ebSTeresa Johnson bool maybeInsertMemProfInitAtFunctionEntry(Function &F);
189226d80ebSTeresa Johnson bool insertDynamicShadowAtFunctionEntry(Function &F);
190226d80ebSTeresa Johnson
191226d80ebSTeresa Johnson private:
192226d80ebSTeresa Johnson void initializeCallbacks(Module &M);
193226d80ebSTeresa Johnson
194226d80ebSTeresa Johnson LLVMContext *C;
195226d80ebSTeresa Johnson int LongSize;
196226d80ebSTeresa Johnson Type *IntptrTy;
197226d80ebSTeresa Johnson ShadowMapping Mapping;
198226d80ebSTeresa Johnson
199226d80ebSTeresa Johnson // These arrays is indexed by AccessIsWrite
200226d80ebSTeresa Johnson FunctionCallee MemProfMemoryAccessCallback[2];
201226d80ebSTeresa Johnson FunctionCallee MemProfMemoryAccessCallbackSized[2];
202226d80ebSTeresa Johnson
203226d80ebSTeresa Johnson FunctionCallee MemProfMemmove, MemProfMemcpy, MemProfMemset;
204226d80ebSTeresa Johnson Value *DynamicShadowOffset = nullptr;
205226d80ebSTeresa Johnson };
206226d80ebSTeresa Johnson
207226d80ebSTeresa Johnson class MemProfilerLegacyPass : public FunctionPass {
208226d80ebSTeresa Johnson public:
209226d80ebSTeresa Johnson static char ID;
210226d80ebSTeresa Johnson
MemProfilerLegacyPass()211226d80ebSTeresa Johnson explicit MemProfilerLegacyPass() : FunctionPass(ID) {
212226d80ebSTeresa Johnson initializeMemProfilerLegacyPassPass(*PassRegistry::getPassRegistry());
213226d80ebSTeresa Johnson }
214226d80ebSTeresa Johnson
getPassName() const215226d80ebSTeresa Johnson StringRef getPassName() const override { return "MemProfilerFunctionPass"; }
216226d80ebSTeresa Johnson
runOnFunction(Function & F)217226d80ebSTeresa Johnson bool runOnFunction(Function &F) override {
218226d80ebSTeresa Johnson MemProfiler Profiler(*F.getParent());
219226d80ebSTeresa Johnson return Profiler.instrumentFunction(F);
220226d80ebSTeresa Johnson }
221226d80ebSTeresa Johnson };
222226d80ebSTeresa Johnson
223226d80ebSTeresa Johnson class ModuleMemProfiler {
224226d80ebSTeresa Johnson public:
ModuleMemProfiler(Module & M)225226d80ebSTeresa Johnson ModuleMemProfiler(Module &M) { TargetTriple = Triple(M.getTargetTriple()); }
226226d80ebSTeresa Johnson
227226d80ebSTeresa Johnson bool instrumentModule(Module &);
228226d80ebSTeresa Johnson
229226d80ebSTeresa Johnson private:
230226d80ebSTeresa Johnson Triple TargetTriple;
231226d80ebSTeresa Johnson ShadowMapping Mapping;
232226d80ebSTeresa Johnson Function *MemProfCtorFunction = nullptr;
233226d80ebSTeresa Johnson };
234226d80ebSTeresa Johnson
235226d80ebSTeresa Johnson class ModuleMemProfilerLegacyPass : public ModulePass {
236226d80ebSTeresa Johnson public:
237226d80ebSTeresa Johnson static char ID;
238226d80ebSTeresa Johnson
ModuleMemProfilerLegacyPass()239226d80ebSTeresa Johnson explicit ModuleMemProfilerLegacyPass() : ModulePass(ID) {
240226d80ebSTeresa Johnson initializeModuleMemProfilerLegacyPassPass(*PassRegistry::getPassRegistry());
241226d80ebSTeresa Johnson }
242226d80ebSTeresa Johnson
getPassName() const243226d80ebSTeresa Johnson StringRef getPassName() const override { return "ModuleMemProfiler"; }
244226d80ebSTeresa Johnson
getAnalysisUsage(AnalysisUsage & AU) const245226d80ebSTeresa Johnson void getAnalysisUsage(AnalysisUsage &AU) const override {}
246226d80ebSTeresa Johnson
runOnModule(Module & M)247226d80ebSTeresa Johnson bool runOnModule(Module &M) override {
248226d80ebSTeresa Johnson ModuleMemProfiler MemProfiler(M);
249226d80ebSTeresa Johnson return MemProfiler.instrumentModule(M);
250226d80ebSTeresa Johnson }
251226d80ebSTeresa Johnson };
252226d80ebSTeresa Johnson
253226d80ebSTeresa Johnson } // end anonymous namespace
254226d80ebSTeresa Johnson
2553a3cb929SKazu Hirata MemProfilerPass::MemProfilerPass() = default;
256226d80ebSTeresa Johnson
run(Function & F,AnalysisManager<Function> & AM)257226d80ebSTeresa Johnson PreservedAnalyses MemProfilerPass::run(Function &F,
258226d80ebSTeresa Johnson AnalysisManager<Function> &AM) {
259226d80ebSTeresa Johnson Module &M = *F.getParent();
260226d80ebSTeresa Johnson MemProfiler Profiler(M);
261226d80ebSTeresa Johnson if (Profiler.instrumentFunction(F))
262226d80ebSTeresa Johnson return PreservedAnalyses::none();
263226d80ebSTeresa Johnson return PreservedAnalyses::all();
264226d80ebSTeresa Johnson }
265226d80ebSTeresa Johnson
2663a3cb929SKazu Hirata ModuleMemProfilerPass::ModuleMemProfilerPass() = default;
267226d80ebSTeresa Johnson
run(Module & M,AnalysisManager<Module> & AM)268226d80ebSTeresa Johnson PreservedAnalyses ModuleMemProfilerPass::run(Module &M,
269226d80ebSTeresa Johnson AnalysisManager<Module> &AM) {
270226d80ebSTeresa Johnson ModuleMemProfiler Profiler(M);
271226d80ebSTeresa Johnson if (Profiler.instrumentModule(M))
272226d80ebSTeresa Johnson return PreservedAnalyses::none();
273226d80ebSTeresa Johnson return PreservedAnalyses::all();
274226d80ebSTeresa Johnson }
275226d80ebSTeresa Johnson
276226d80ebSTeresa Johnson char MemProfilerLegacyPass::ID = 0;
277226d80ebSTeresa Johnson
278226d80ebSTeresa Johnson INITIALIZE_PASS_BEGIN(MemProfilerLegacyPass, "memprof",
279226d80ebSTeresa Johnson "MemProfiler: profile memory allocations and accesses.",
280226d80ebSTeresa Johnson false, false)
281226d80ebSTeresa Johnson INITIALIZE_PASS_END(MemProfilerLegacyPass, "memprof",
282226d80ebSTeresa Johnson "MemProfiler: profile memory allocations and accesses.",
283226d80ebSTeresa Johnson false, false)
284226d80ebSTeresa Johnson
createMemProfilerFunctionPass()285226d80ebSTeresa Johnson FunctionPass *llvm::createMemProfilerFunctionPass() {
286226d80ebSTeresa Johnson return new MemProfilerLegacyPass();
287226d80ebSTeresa Johnson }
288226d80ebSTeresa Johnson
289226d80ebSTeresa Johnson char ModuleMemProfilerLegacyPass::ID = 0;
290226d80ebSTeresa Johnson
291226d80ebSTeresa Johnson INITIALIZE_PASS(ModuleMemProfilerLegacyPass, "memprof-module",
292226d80ebSTeresa Johnson "MemProfiler: profile memory allocations and accesses."
293226d80ebSTeresa Johnson "ModulePass",
294226d80ebSTeresa Johnson false, false)
295226d80ebSTeresa Johnson
createModuleMemProfilerLegacyPassPass()296226d80ebSTeresa Johnson ModulePass *llvm::createModuleMemProfilerLegacyPassPass() {
297226d80ebSTeresa Johnson return new ModuleMemProfilerLegacyPass();
298226d80ebSTeresa Johnson }
299226d80ebSTeresa Johnson
memToShadow(Value * Shadow,IRBuilder<> & IRB)300226d80ebSTeresa Johnson Value *MemProfiler::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
301226d80ebSTeresa Johnson // (Shadow & mask) >> scale
302226d80ebSTeresa Johnson Shadow = IRB.CreateAnd(Shadow, Mapping.Mask);
303226d80ebSTeresa Johnson Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
304226d80ebSTeresa Johnson // (Shadow >> scale) | offset
305226d80ebSTeresa Johnson assert(DynamicShadowOffset);
306226d80ebSTeresa Johnson return IRB.CreateAdd(Shadow, DynamicShadowOffset);
307226d80ebSTeresa Johnson }
308226d80ebSTeresa Johnson
309226d80ebSTeresa Johnson // Instrument memset/memmove/memcpy
instrumentMemIntrinsic(MemIntrinsic * MI)310226d80ebSTeresa Johnson void MemProfiler::instrumentMemIntrinsic(MemIntrinsic *MI) {
311226d80ebSTeresa Johnson IRBuilder<> IRB(MI);
312226d80ebSTeresa Johnson if (isa<MemTransferInst>(MI)) {
313226d80ebSTeresa Johnson IRB.CreateCall(
314226d80ebSTeresa Johnson isa<MemMoveInst>(MI) ? MemProfMemmove : MemProfMemcpy,
315226d80ebSTeresa Johnson {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
316226d80ebSTeresa Johnson IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()),
317226d80ebSTeresa Johnson IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
318226d80ebSTeresa Johnson } else if (isa<MemSetInst>(MI)) {
319226d80ebSTeresa Johnson IRB.CreateCall(
320226d80ebSTeresa Johnson MemProfMemset,
321226d80ebSTeresa Johnson {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
322226d80ebSTeresa Johnson IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
323226d80ebSTeresa Johnson IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
324226d80ebSTeresa Johnson }
325226d80ebSTeresa Johnson MI->eraseFromParent();
326226d80ebSTeresa Johnson }
327226d80ebSTeresa Johnson
328226d80ebSTeresa Johnson Optional<InterestingMemoryAccess>
isInterestingMemoryAccess(Instruction * I) const329226d80ebSTeresa Johnson MemProfiler::isInterestingMemoryAccess(Instruction *I) const {
330226d80ebSTeresa Johnson // Do not instrument the load fetching the dynamic shadow address.
331226d80ebSTeresa Johnson if (DynamicShadowOffset == I)
332226d80ebSTeresa Johnson return None;
333226d80ebSTeresa Johnson
334226d80ebSTeresa Johnson InterestingMemoryAccess Access;
335226d80ebSTeresa Johnson
336226d80ebSTeresa Johnson if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
337226d80ebSTeresa Johnson if (!ClInstrumentReads)
338226d80ebSTeresa Johnson return None;
339226d80ebSTeresa Johnson Access.IsWrite = false;
3407cc3e141SNikita Popov Access.AccessTy = LI->getType();
341226d80ebSTeresa Johnson Access.Addr = LI->getPointerOperand();
342226d80ebSTeresa Johnson } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
343226d80ebSTeresa Johnson if (!ClInstrumentWrites)
344226d80ebSTeresa Johnson return None;
345226d80ebSTeresa Johnson Access.IsWrite = true;
3467cc3e141SNikita Popov Access.AccessTy = SI->getValueOperand()->getType();
347226d80ebSTeresa Johnson Access.Addr = SI->getPointerOperand();
348226d80ebSTeresa Johnson } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
349226d80ebSTeresa Johnson if (!ClInstrumentAtomics)
350226d80ebSTeresa Johnson return None;
351226d80ebSTeresa Johnson Access.IsWrite = true;
3527cc3e141SNikita Popov Access.AccessTy = RMW->getValOperand()->getType();
353226d80ebSTeresa Johnson Access.Addr = RMW->getPointerOperand();
354226d80ebSTeresa Johnson } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
355226d80ebSTeresa Johnson if (!ClInstrumentAtomics)
356226d80ebSTeresa Johnson return None;
357226d80ebSTeresa Johnson Access.IsWrite = true;
3587cc3e141SNikita Popov Access.AccessTy = XCHG->getCompareOperand()->getType();
359226d80ebSTeresa Johnson Access.Addr = XCHG->getPointerOperand();
360226d80ebSTeresa Johnson } else if (auto *CI = dyn_cast<CallInst>(I)) {
361226d80ebSTeresa Johnson auto *F = CI->getCalledFunction();
362226d80ebSTeresa Johnson if (F && (F->getIntrinsicID() == Intrinsic::masked_load ||
363226d80ebSTeresa Johnson F->getIntrinsicID() == Intrinsic::masked_store)) {
364226d80ebSTeresa Johnson unsigned OpOffset = 0;
365226d80ebSTeresa Johnson if (F->getIntrinsicID() == Intrinsic::masked_store) {
366226d80ebSTeresa Johnson if (!ClInstrumentWrites)
367226d80ebSTeresa Johnson return None;
368226d80ebSTeresa Johnson // Masked store has an initial operand for the value.
369226d80ebSTeresa Johnson OpOffset = 1;
3707cc3e141SNikita Popov Access.AccessTy = CI->getArgOperand(0)->getType();
371226d80ebSTeresa Johnson Access.IsWrite = true;
372226d80ebSTeresa Johnson } else {
373226d80ebSTeresa Johnson if (!ClInstrumentReads)
374226d80ebSTeresa Johnson return None;
3757cc3e141SNikita Popov Access.AccessTy = CI->getType();
376226d80ebSTeresa Johnson Access.IsWrite = false;
377226d80ebSTeresa Johnson }
378226d80ebSTeresa Johnson
379226d80ebSTeresa Johnson auto *BasePtr = CI->getOperand(0 + OpOffset);
380226d80ebSTeresa Johnson Access.MaybeMask = CI->getOperand(2 + OpOffset);
381226d80ebSTeresa Johnson Access.Addr = BasePtr;
382226d80ebSTeresa Johnson }
383226d80ebSTeresa Johnson }
384226d80ebSTeresa Johnson
385226d80ebSTeresa Johnson if (!Access.Addr)
386226d80ebSTeresa Johnson return None;
387226d80ebSTeresa Johnson
388226d80ebSTeresa Johnson // Do not instrument acesses from different address spaces; we cannot deal
389226d80ebSTeresa Johnson // with them.
390226d80ebSTeresa Johnson Type *PtrTy = cast<PointerType>(Access.Addr->getType()->getScalarType());
391226d80ebSTeresa Johnson if (PtrTy->getPointerAddressSpace() != 0)
392226d80ebSTeresa Johnson return None;
393226d80ebSTeresa Johnson
394226d80ebSTeresa Johnson // Ignore swifterror addresses.
395226d80ebSTeresa Johnson // swifterror memory addresses are mem2reg promoted by instruction
396226d80ebSTeresa Johnson // selection. As such they cannot have regular uses like an instrumentation
397226d80ebSTeresa Johnson // function and it makes no sense to track them as memory.
398226d80ebSTeresa Johnson if (Access.Addr->isSwiftError())
399226d80ebSTeresa Johnson return None;
400226d80ebSTeresa Johnson
401a0b5af46STeresa Johnson // Peel off GEPs and BitCasts.
402a0b5af46STeresa Johnson auto *Addr = Access.Addr->stripInBoundsOffsets();
403a0b5af46STeresa Johnson
404a0b5af46STeresa Johnson if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
405a0b5af46STeresa Johnson // Do not instrument PGO counter updates.
406a0b5af46STeresa Johnson if (GV->hasSection()) {
407a0b5af46STeresa Johnson StringRef SectionName = GV->getSection();
408a0b5af46STeresa Johnson // Check if the global is in the PGO counters section.
409a0b5af46STeresa Johnson auto OF = Triple(I->getModule()->getTargetTriple()).getObjectFormat();
410a0b5af46STeresa Johnson if (SectionName.endswith(
411a0b5af46STeresa Johnson getInstrProfSectionName(IPSK_cnts, OF, /*AddSegmentInfo=*/false)))
412a0b5af46STeresa Johnson return None;
413a0b5af46STeresa Johnson }
414a0b5af46STeresa Johnson
415a0b5af46STeresa Johnson // Do not instrument accesses to LLVM internal variables.
416a0b5af46STeresa Johnson if (GV->getName().startswith("__llvm"))
417a0b5af46STeresa Johnson return None;
418a0b5af46STeresa Johnson }
419a0b5af46STeresa Johnson
4207cc3e141SNikita Popov const DataLayout &DL = I->getModule()->getDataLayout();
4217cc3e141SNikita Popov Access.TypeSize = DL.getTypeStoreSizeInBits(Access.AccessTy);
422226d80ebSTeresa Johnson return Access;
423226d80ebSTeresa Johnson }
424226d80ebSTeresa Johnson
instrumentMaskedLoadOrStore(const DataLayout & DL,Value * Mask,Instruction * I,Value * Addr,Type * AccessTy,bool IsWrite)425226d80ebSTeresa Johnson void MemProfiler::instrumentMaskedLoadOrStore(const DataLayout &DL, Value *Mask,
426226d80ebSTeresa Johnson Instruction *I, Value *Addr,
4277cc3e141SNikita Popov Type *AccessTy, bool IsWrite) {
4287cc3e141SNikita Popov auto *VTy = cast<FixedVectorType>(AccessTy);
429226d80ebSTeresa Johnson uint64_t ElemTypeSize = DL.getTypeStoreSizeInBits(VTy->getScalarType());
430226d80ebSTeresa Johnson unsigned Num = VTy->getNumElements();
431226d80ebSTeresa Johnson auto *Zero = ConstantInt::get(IntptrTy, 0);
432226d80ebSTeresa Johnson for (unsigned Idx = 0; Idx < Num; ++Idx) {
433226d80ebSTeresa Johnson Value *InstrumentedAddress = nullptr;
434226d80ebSTeresa Johnson Instruction *InsertBefore = I;
435226d80ebSTeresa Johnson if (auto *Vector = dyn_cast<ConstantVector>(Mask)) {
436226d80ebSTeresa Johnson // dyn_cast as we might get UndefValue
437226d80ebSTeresa Johnson if (auto *Masked = dyn_cast<ConstantInt>(Vector->getOperand(Idx))) {
438226d80ebSTeresa Johnson if (Masked->isZero())
439226d80ebSTeresa Johnson // Mask is constant false, so no instrumentation needed.
440226d80ebSTeresa Johnson continue;
441226d80ebSTeresa Johnson // If we have a true or undef value, fall through to instrumentAddress.
442226d80ebSTeresa Johnson // with InsertBefore == I
443226d80ebSTeresa Johnson }
444226d80ebSTeresa Johnson } else {
445226d80ebSTeresa Johnson IRBuilder<> IRB(I);
446226d80ebSTeresa Johnson Value *MaskElem = IRB.CreateExtractElement(Mask, Idx);
447226d80ebSTeresa Johnson Instruction *ThenTerm = SplitBlockAndInsertIfThen(MaskElem, I, false);
448226d80ebSTeresa Johnson InsertBefore = ThenTerm;
449226d80ebSTeresa Johnson }
450226d80ebSTeresa Johnson
451226d80ebSTeresa Johnson IRBuilder<> IRB(InsertBefore);
452226d80ebSTeresa Johnson InstrumentedAddress =
453226d80ebSTeresa Johnson IRB.CreateGEP(VTy, Addr, {Zero, ConstantInt::get(IntptrTy, Idx)});
454226d80ebSTeresa Johnson instrumentAddress(I, InsertBefore, InstrumentedAddress, ElemTypeSize,
455226d80ebSTeresa Johnson IsWrite);
456226d80ebSTeresa Johnson }
457226d80ebSTeresa Johnson }
458226d80ebSTeresa Johnson
instrumentMop(Instruction * I,const DataLayout & DL,InterestingMemoryAccess & Access)459226d80ebSTeresa Johnson void MemProfiler::instrumentMop(Instruction *I, const DataLayout &DL,
460226d80ebSTeresa Johnson InterestingMemoryAccess &Access) {
46188cb3e2cSTeresa Johnson // Skip instrumentation of stack accesses unless requested.
46288cb3e2cSTeresa Johnson if (!ClStack && isa<AllocaInst>(getUnderlyingObject(Access.Addr))) {
46388cb3e2cSTeresa Johnson if (Access.IsWrite)
46488cb3e2cSTeresa Johnson ++NumSkippedStackWrites;
46588cb3e2cSTeresa Johnson else
46688cb3e2cSTeresa Johnson ++NumSkippedStackReads;
46788cb3e2cSTeresa Johnson return;
46888cb3e2cSTeresa Johnson }
46988cb3e2cSTeresa Johnson
470226d80ebSTeresa Johnson if (Access.IsWrite)
471226d80ebSTeresa Johnson NumInstrumentedWrites++;
472226d80ebSTeresa Johnson else
473226d80ebSTeresa Johnson NumInstrumentedReads++;
474226d80ebSTeresa Johnson
475226d80ebSTeresa Johnson if (Access.MaybeMask) {
476226d80ebSTeresa Johnson instrumentMaskedLoadOrStore(DL, Access.MaybeMask, I, Access.Addr,
477*86f45575SGuillaume Chatelet Access.AccessTy, Access.IsWrite);
478226d80ebSTeresa Johnson } else {
479226d80ebSTeresa Johnson // Since the access counts will be accumulated across the entire allocation,
480226d80ebSTeresa Johnson // we only update the shadow access count for the first location and thus
481226d80ebSTeresa Johnson // don't need to worry about alignment and type size.
482226d80ebSTeresa Johnson instrumentAddress(I, I, Access.Addr, Access.TypeSize, Access.IsWrite);
483226d80ebSTeresa Johnson }
484226d80ebSTeresa Johnson }
485226d80ebSTeresa Johnson
instrumentAddress(Instruction * OrigIns,Instruction * InsertBefore,Value * Addr,uint32_t TypeSize,bool IsWrite)486226d80ebSTeresa Johnson void MemProfiler::instrumentAddress(Instruction *OrigIns,
487226d80ebSTeresa Johnson Instruction *InsertBefore, Value *Addr,
488226d80ebSTeresa Johnson uint32_t TypeSize, bool IsWrite) {
489226d80ebSTeresa Johnson IRBuilder<> IRB(InsertBefore);
490226d80ebSTeresa Johnson Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
491226d80ebSTeresa Johnson
492226d80ebSTeresa Johnson if (ClUseCalls) {
493226d80ebSTeresa Johnson IRB.CreateCall(MemProfMemoryAccessCallback[IsWrite], AddrLong);
494226d80ebSTeresa Johnson return;
495226d80ebSTeresa Johnson }
496226d80ebSTeresa Johnson
497226d80ebSTeresa Johnson // Create an inline sequence to compute shadow location, and increment the
498226d80ebSTeresa Johnson // value by one.
499226d80ebSTeresa Johnson Type *ShadowTy = Type::getInt64Ty(*C);
500226d80ebSTeresa Johnson Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
501226d80ebSTeresa Johnson Value *ShadowPtr = memToShadow(AddrLong, IRB);
502226d80ebSTeresa Johnson Value *ShadowAddr = IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy);
503226d80ebSTeresa Johnson Value *ShadowValue = IRB.CreateLoad(ShadowTy, ShadowAddr);
504226d80ebSTeresa Johnson Value *Inc = ConstantInt::get(Type::getInt64Ty(*C), 1);
505226d80ebSTeresa Johnson ShadowValue = IRB.CreateAdd(ShadowValue, Inc);
506226d80ebSTeresa Johnson IRB.CreateStore(ShadowValue, ShadowAddr);
507226d80ebSTeresa Johnson }
508226d80ebSTeresa Johnson
5090949f96dSTeresa Johnson // Create the variable for the profile file name.
createProfileFileNameVar(Module & M)5100949f96dSTeresa Johnson void createProfileFileNameVar(Module &M) {
5110949f96dSTeresa Johnson const MDString *MemProfFilename =
5120949f96dSTeresa Johnson dyn_cast_or_null<MDString>(M.getModuleFlag("MemProfProfileFilename"));
5130949f96dSTeresa Johnson if (!MemProfFilename)
5140949f96dSTeresa Johnson return;
5150949f96dSTeresa Johnson assert(!MemProfFilename->getString().empty() &&
5160949f96dSTeresa Johnson "Unexpected MemProfProfileFilename metadata with empty string");
5170949f96dSTeresa Johnson Constant *ProfileNameConst = ConstantDataArray::getString(
5180949f96dSTeresa Johnson M.getContext(), MemProfFilename->getString(), true);
5190949f96dSTeresa Johnson GlobalVariable *ProfileNameVar = new GlobalVariable(
5200949f96dSTeresa Johnson M, ProfileNameConst->getType(), /*isConstant=*/true,
5210949f96dSTeresa Johnson GlobalValue::WeakAnyLinkage, ProfileNameConst, MemProfFilenameVar);
5220949f96dSTeresa Johnson Triple TT(M.getTargetTriple());
5230949f96dSTeresa Johnson if (TT.supportsCOMDAT()) {
5240949f96dSTeresa Johnson ProfileNameVar->setLinkage(GlobalValue::ExternalLinkage);
5250949f96dSTeresa Johnson ProfileNameVar->setComdat(M.getOrInsertComdat(MemProfFilenameVar));
5260949f96dSTeresa Johnson }
5270949f96dSTeresa Johnson }
5280949f96dSTeresa Johnson
instrumentModule(Module & M)529226d80ebSTeresa Johnson bool ModuleMemProfiler::instrumentModule(Module &M) {
530226d80ebSTeresa Johnson // Create a module constructor.
531226d80ebSTeresa Johnson std::string MemProfVersion = std::to_string(LLVM_MEM_PROFILER_VERSION);
532226d80ebSTeresa Johnson std::string VersionCheckName =
533226d80ebSTeresa Johnson ClInsertVersionCheck ? (MemProfVersionCheckNamePrefix + MemProfVersion)
534226d80ebSTeresa Johnson : "";
535226d80ebSTeresa Johnson std::tie(MemProfCtorFunction, std::ignore) =
536226d80ebSTeresa Johnson createSanitizerCtorAndInitFunctions(M, MemProfModuleCtorName,
537226d80ebSTeresa Johnson MemProfInitName, /*InitArgTypes=*/{},
538226d80ebSTeresa Johnson /*InitArgs=*/{}, VersionCheckName);
539226d80ebSTeresa Johnson
540226d80ebSTeresa Johnson const uint64_t Priority = getCtorAndDtorPriority(TargetTriple);
541226d80ebSTeresa Johnson appendToGlobalCtors(M, MemProfCtorFunction, Priority);
542226d80ebSTeresa Johnson
5430949f96dSTeresa Johnson createProfileFileNameVar(M);
5440949f96dSTeresa Johnson
545226d80ebSTeresa Johnson return true;
546226d80ebSTeresa Johnson }
547226d80ebSTeresa Johnson
initializeCallbacks(Module & M)548226d80ebSTeresa Johnson void MemProfiler::initializeCallbacks(Module &M) {
549226d80ebSTeresa Johnson IRBuilder<> IRB(*C);
550226d80ebSTeresa Johnson
551226d80ebSTeresa Johnson for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
552226d80ebSTeresa Johnson const std::string TypeStr = AccessIsWrite ? "store" : "load";
553226d80ebSTeresa Johnson
554226d80ebSTeresa Johnson SmallVector<Type *, 3> Args2 = {IntptrTy, IntptrTy};
555226d80ebSTeresa Johnson SmallVector<Type *, 2> Args1{1, IntptrTy};
556226d80ebSTeresa Johnson MemProfMemoryAccessCallbackSized[AccessIsWrite] =
557226d80ebSTeresa Johnson M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + TypeStr + "N",
558226d80ebSTeresa Johnson FunctionType::get(IRB.getVoidTy(), Args2, false));
559226d80ebSTeresa Johnson
560226d80ebSTeresa Johnson MemProfMemoryAccessCallback[AccessIsWrite] =
561226d80ebSTeresa Johnson M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + TypeStr,
562226d80ebSTeresa Johnson FunctionType::get(IRB.getVoidTy(), Args1, false));
563226d80ebSTeresa Johnson }
564226d80ebSTeresa Johnson MemProfMemmove = M.getOrInsertFunction(
565226d80ebSTeresa Johnson ClMemoryAccessCallbackPrefix + "memmove", IRB.getInt8PtrTy(),
566226d80ebSTeresa Johnson IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
567226d80ebSTeresa Johnson MemProfMemcpy = M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "memcpy",
568226d80ebSTeresa Johnson IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
569226d80ebSTeresa Johnson IRB.getInt8PtrTy(), IntptrTy);
570226d80ebSTeresa Johnson MemProfMemset = M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "memset",
571226d80ebSTeresa Johnson IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
572226d80ebSTeresa Johnson IRB.getInt32Ty(), IntptrTy);
573226d80ebSTeresa Johnson }
574226d80ebSTeresa Johnson
maybeInsertMemProfInitAtFunctionEntry(Function & F)575226d80ebSTeresa Johnson bool MemProfiler::maybeInsertMemProfInitAtFunctionEntry(Function &F) {
576226d80ebSTeresa Johnson // For each NSObject descendant having a +load method, this method is invoked
577226d80ebSTeresa Johnson // by the ObjC runtime before any of the static constructors is called.
578226d80ebSTeresa Johnson // Therefore we need to instrument such methods with a call to __memprof_init
579226d80ebSTeresa Johnson // at the beginning in order to initialize our runtime before any access to
580226d80ebSTeresa Johnson // the shadow memory.
581226d80ebSTeresa Johnson // We cannot just ignore these methods, because they may call other
582226d80ebSTeresa Johnson // instrumented functions.
583226d80ebSTeresa Johnson if (F.getName().find(" load]") != std::string::npos) {
584226d80ebSTeresa Johnson FunctionCallee MemProfInitFunction =
585226d80ebSTeresa Johnson declareSanitizerInitFunction(*F.getParent(), MemProfInitName, {});
586226d80ebSTeresa Johnson IRBuilder<> IRB(&F.front(), F.front().begin());
587226d80ebSTeresa Johnson IRB.CreateCall(MemProfInitFunction, {});
588226d80ebSTeresa Johnson return true;
589226d80ebSTeresa Johnson }
590226d80ebSTeresa Johnson return false;
591226d80ebSTeresa Johnson }
592226d80ebSTeresa Johnson
insertDynamicShadowAtFunctionEntry(Function & F)593226d80ebSTeresa Johnson bool MemProfiler::insertDynamicShadowAtFunctionEntry(Function &F) {
594226d80ebSTeresa Johnson IRBuilder<> IRB(&F.front().front());
595226d80ebSTeresa Johnson Value *GlobalDynamicAddress = F.getParent()->getOrInsertGlobal(
596226d80ebSTeresa Johnson MemProfShadowMemoryDynamicAddress, IntptrTy);
597204d0d51SFangrui Song if (F.getParent()->getPICLevel() == PICLevel::NotPIC)
59884d5768dSSimon Pilgrim cast<GlobalVariable>(GlobalDynamicAddress)->setDSOLocal(true);
599226d80ebSTeresa Johnson DynamicShadowOffset = IRB.CreateLoad(IntptrTy, GlobalDynamicAddress);
600226d80ebSTeresa Johnson return true;
601226d80ebSTeresa Johnson }
602226d80ebSTeresa Johnson
instrumentFunction(Function & F)603226d80ebSTeresa Johnson bool MemProfiler::instrumentFunction(Function &F) {
604226d80ebSTeresa Johnson if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage)
605226d80ebSTeresa Johnson return false;
606226d80ebSTeresa Johnson if (ClDebugFunc == F.getName())
607226d80ebSTeresa Johnson return false;
608226d80ebSTeresa Johnson if (F.getName().startswith("__memprof_"))
609226d80ebSTeresa Johnson return false;
610226d80ebSTeresa Johnson
611226d80ebSTeresa Johnson bool FunctionModified = false;
612226d80ebSTeresa Johnson
613226d80ebSTeresa Johnson // If needed, insert __memprof_init.
614226d80ebSTeresa Johnson // This function needs to be called even if the function body is not
615226d80ebSTeresa Johnson // instrumented.
616226d80ebSTeresa Johnson if (maybeInsertMemProfInitAtFunctionEntry(F))
617226d80ebSTeresa Johnson FunctionModified = true;
618226d80ebSTeresa Johnson
619226d80ebSTeresa Johnson LLVM_DEBUG(dbgs() << "MEMPROF instrumenting:\n" << F << "\n");
620226d80ebSTeresa Johnson
621226d80ebSTeresa Johnson initializeCallbacks(*F.getParent());
622226d80ebSTeresa Johnson
623226d80ebSTeresa Johnson SmallVector<Instruction *, 16> ToInstrument;
624226d80ebSTeresa Johnson
625226d80ebSTeresa Johnson // Fill the set of memory operations to instrument.
626226d80ebSTeresa Johnson for (auto &BB : F) {
627226d80ebSTeresa Johnson for (auto &Inst : BB) {
628226d80ebSTeresa Johnson if (isInterestingMemoryAccess(&Inst) || isa<MemIntrinsic>(Inst))
629226d80ebSTeresa Johnson ToInstrument.push_back(&Inst);
630226d80ebSTeresa Johnson }
631226d80ebSTeresa Johnson }
632226d80ebSTeresa Johnson
633084b65f7STeresa Johnson if (ToInstrument.empty()) {
634084b65f7STeresa Johnson LLVM_DEBUG(dbgs() << "MEMPROF done instrumenting: " << FunctionModified
635084b65f7STeresa Johnson << " " << F << "\n");
636084b65f7STeresa Johnson
637084b65f7STeresa Johnson return FunctionModified;
638084b65f7STeresa Johnson }
639084b65f7STeresa Johnson
640084b65f7STeresa Johnson FunctionModified |= insertDynamicShadowAtFunctionEntry(F);
641084b65f7STeresa Johnson
642226d80ebSTeresa Johnson int NumInstrumented = 0;
643226d80ebSTeresa Johnson for (auto *Inst : ToInstrument) {
644226d80ebSTeresa Johnson if (ClDebugMin < 0 || ClDebugMax < 0 ||
645226d80ebSTeresa Johnson (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
646226d80ebSTeresa Johnson Optional<InterestingMemoryAccess> Access =
647226d80ebSTeresa Johnson isInterestingMemoryAccess(Inst);
648226d80ebSTeresa Johnson if (Access)
649226d80ebSTeresa Johnson instrumentMop(Inst, F.getParent()->getDataLayout(), *Access);
650226d80ebSTeresa Johnson else
651226d80ebSTeresa Johnson instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
652226d80ebSTeresa Johnson }
653226d80ebSTeresa Johnson NumInstrumented++;
654226d80ebSTeresa Johnson }
655226d80ebSTeresa Johnson
656226d80ebSTeresa Johnson if (NumInstrumented > 0)
657226d80ebSTeresa Johnson FunctionModified = true;
658226d80ebSTeresa Johnson
659226d80ebSTeresa Johnson LLVM_DEBUG(dbgs() << "MEMPROF done instrumenting: " << FunctionModified << " "
660226d80ebSTeresa Johnson << F << "\n");
661226d80ebSTeresa Johnson
662226d80ebSTeresa Johnson return FunctionModified;
663226d80ebSTeresa Johnson }
664