LLVM 17.0.0git
MemProfiler.cpp
Go to the documentation of this file.
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
19#include "llvm/ADT/Statistic.h"
20#include "llvm/ADT/StringRef.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"
29#include "llvm/IR/Module.h"
30#include "llvm/IR/Type.h"
31#include "llvm/IR/Value.h"
33#include "llvm/Pass.h"
36#include "llvm/Support/Debug.h"
40
41using namespace llvm;
42
43#define DEBUG_TYPE "memprof"
44
45constexpr int LLVM_MEM_PROFILER_VERSION = 1;
46
47// Size of memory mapped to a single shadow location.
49
50// Scale from granularity down to shadow size.
52
53constexpr char MemProfModuleCtorName[] = "memprof.module_ctor";
55// On Emscripten, the system needs more than one priorities for constructors.
57constexpr char MemProfInitName[] = "__memprof_init";
59 "__memprof_version_mismatch_check_v";
60
62 "__memprof_shadow_memory_dynamic_address";
63
64constexpr char MemProfFilenameVar[] = "__memprof_profile_filename";
65
66// Command-line flags.
67
69 "memprof-guard-against-version-mismatch",
70 cl::desc("Guard against compiler/runtime version mismatch."), cl::Hidden,
71 cl::init(true));
72
73// This flag may need to be replaced with -f[no-]memprof-reads.
74static cl::opt<bool> ClInstrumentReads("memprof-instrument-reads",
75 cl::desc("instrument read instructions"),
76 cl::Hidden, cl::init(true));
77
78static cl::opt<bool>
79 ClInstrumentWrites("memprof-instrument-writes",
80 cl::desc("instrument write instructions"), cl::Hidden,
81 cl::init(true));
82
84 "memprof-instrument-atomics",
85 cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden,
86 cl::init(true));
87
89 "memprof-use-callbacks",
90 cl::desc("Use callbacks instead of inline instrumentation sequences."),
91 cl::Hidden, cl::init(false));
92
94 ClMemoryAccessCallbackPrefix("memprof-memory-access-callback-prefix",
95 cl::desc("Prefix for memory access callbacks"),
96 cl::Hidden, cl::init("__memprof_"));
97
98// These flags allow to change the shadow mapping.
99// The shadow mapping looks like
100// Shadow = ((Mem & mask) >> scale) + offset
101
102static cl::opt<int> ClMappingScale("memprof-mapping-scale",
103 cl::desc("scale of memprof shadow mapping"),
105
106static cl::opt<int>
107 ClMappingGranularity("memprof-mapping-granularity",
108 cl::desc("granularity of memprof shadow mapping"),
110
111static cl::opt<bool> ClStack("memprof-instrument-stack",
112 cl::desc("Instrument scalar stack variables"),
113 cl::Hidden, cl::init(false));
114
115// Debug flags.
116
117static cl::opt<int> ClDebug("memprof-debug", cl::desc("debug"), cl::Hidden,
118 cl::init(0));
119
120static cl::opt<std::string> ClDebugFunc("memprof-debug-func", cl::Hidden,
121 cl::desc("Debug func"));
122
123static cl::opt<int> ClDebugMin("memprof-debug-min", cl::desc("Debug min inst"),
124 cl::Hidden, cl::init(-1));
125
126static cl::opt<int> ClDebugMax("memprof-debug-max", cl::desc("Debug max inst"),
127 cl::Hidden, cl::init(-1));
128
129STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
130STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
131STATISTIC(NumSkippedStackReads, "Number of non-instrumented stack reads");
132STATISTIC(NumSkippedStackWrites, "Number of non-instrumented stack writes");
133
134namespace {
135
136/// This struct defines the shadow mapping using the rule:
137/// shadow = ((mem & mask) >> Scale) ADD DynamicShadowOffset.
138struct ShadowMapping {
139 ShadowMapping() {
140 Scale = ClMappingScale;
141 Granularity = ClMappingGranularity;
142 Mask = ~(Granularity - 1);
143 }
144
145 int Scale;
146 int Granularity;
147 uint64_t Mask; // Computed as ~(Granularity-1)
148};
149
150static uint64_t getCtorAndDtorPriority(Triple &TargetTriple) {
153}
154
155struct InterestingMemoryAccess {
156 Value *Addr = nullptr;
157 bool IsWrite;
158 Type *AccessTy;
160 Value *MaybeMask = nullptr;
161};
162
163/// Instrument the code in module to profile memory accesses.
164class MemProfiler {
165public:
166 MemProfiler(Module &M) {
167 C = &(M.getContext());
168 LongSize = M.getDataLayout().getPointerSizeInBits();
169 IntptrTy = Type::getIntNTy(*C, LongSize);
170 }
171
172 /// If it is an interesting memory access, populate information
173 /// about the access and return a InterestingMemoryAccess struct.
174 /// Otherwise return std::nullopt.
175 std::optional<InterestingMemoryAccess>
176 isInterestingMemoryAccess(Instruction *I) const;
177
178 void instrumentMop(Instruction *I, const DataLayout &DL,
179 InterestingMemoryAccess &Access);
180 void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore,
181 Value *Addr, uint32_t TypeSize, bool IsWrite);
183 Instruction *I, Value *Addr, Type *AccessTy,
184 bool IsWrite);
185 void instrumentMemIntrinsic(MemIntrinsic *MI);
186 Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
187 bool instrumentFunction(Function &F);
188 bool maybeInsertMemProfInitAtFunctionEntry(Function &F);
189 bool insertDynamicShadowAtFunctionEntry(Function &F);
190
191private:
192 void initializeCallbacks(Module &M);
193
194 LLVMContext *C;
195 int LongSize;
196 Type *IntptrTy;
197 ShadowMapping Mapping;
198
199 // These arrays is indexed by AccessIsWrite
200 FunctionCallee MemProfMemoryAccessCallback[2];
201 FunctionCallee MemProfMemoryAccessCallbackSized[2];
202
203 FunctionCallee MemProfMemmove, MemProfMemcpy, MemProfMemset;
204 Value *DynamicShadowOffset = nullptr;
205};
206
207class ModuleMemProfiler {
208public:
209 ModuleMemProfiler(Module &M) { TargetTriple = Triple(M.getTargetTriple()); }
210
211 bool instrumentModule(Module &);
212
213private:
214 Triple TargetTriple;
215 ShadowMapping Mapping;
216 Function *MemProfCtorFunction = nullptr;
217};
218
219} // end anonymous namespace
220
222
225 Module &M = *F.getParent();
226 MemProfiler Profiler(M);
227 if (Profiler.instrumentFunction(F))
229 return PreservedAnalyses::all();
230}
231
233
236 ModuleMemProfiler Profiler(M);
237 if (Profiler.instrumentModule(M))
239 return PreservedAnalyses::all();
240}
241
242Value *MemProfiler::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
243 // (Shadow & mask) >> scale
244 Shadow = IRB.CreateAnd(Shadow, Mapping.Mask);
245 Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
246 // (Shadow >> scale) | offset
247 assert(DynamicShadowOffset);
248 return IRB.CreateAdd(Shadow, DynamicShadowOffset);
249}
250
251// Instrument memset/memmove/memcpy
252void MemProfiler::instrumentMemIntrinsic(MemIntrinsic *MI) {
253 IRBuilder<> IRB(MI);
254 if (isa<MemTransferInst>(MI)) {
255 IRB.CreateCall(
256 isa<MemMoveInst>(MI) ? MemProfMemmove : MemProfMemcpy,
257 {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
258 IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()),
259 IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
260 } else if (isa<MemSetInst>(MI)) {
261 IRB.CreateCall(
262 MemProfMemset,
263 {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
264 IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
265 IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
266 }
267 MI->eraseFromParent();
268}
269
270std::optional<InterestingMemoryAccess>
271MemProfiler::isInterestingMemoryAccess(Instruction *I) const {
272 // Do not instrument the load fetching the dynamic shadow address.
273 if (DynamicShadowOffset == I)
274 return std::nullopt;
275
276 InterestingMemoryAccess Access;
277
278 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
280 return std::nullopt;
281 Access.IsWrite = false;
282 Access.AccessTy = LI->getType();
283 Access.Addr = LI->getPointerOperand();
284 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
286 return std::nullopt;
287 Access.IsWrite = true;
288 Access.AccessTy = SI->getValueOperand()->getType();
289 Access.Addr = SI->getPointerOperand();
290 } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
292 return std::nullopt;
293 Access.IsWrite = true;
294 Access.AccessTy = RMW->getValOperand()->getType();
295 Access.Addr = RMW->getPointerOperand();
296 } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
298 return std::nullopt;
299 Access.IsWrite = true;
300 Access.AccessTy = XCHG->getCompareOperand()->getType();
301 Access.Addr = XCHG->getPointerOperand();
302 } else if (auto *CI = dyn_cast<CallInst>(I)) {
303 auto *F = CI->getCalledFunction();
304 if (F && (F->getIntrinsicID() == Intrinsic::masked_load ||
305 F->getIntrinsicID() == Intrinsic::masked_store)) {
306 unsigned OpOffset = 0;
307 if (F->getIntrinsicID() == Intrinsic::masked_store) {
309 return std::nullopt;
310 // Masked store has an initial operand for the value.
311 OpOffset = 1;
312 Access.AccessTy = CI->getArgOperand(0)->getType();
313 Access.IsWrite = true;
314 } else {
316 return std::nullopt;
317 Access.AccessTy = CI->getType();
318 Access.IsWrite = false;
319 }
320
321 auto *BasePtr = CI->getOperand(0 + OpOffset);
322 Access.MaybeMask = CI->getOperand(2 + OpOffset);
323 Access.Addr = BasePtr;
324 }
325 }
326
327 if (!Access.Addr)
328 return std::nullopt;
329
330 // Do not instrument accesses from different address spaces; we cannot deal
331 // with them.
332 Type *PtrTy = cast<PointerType>(Access.Addr->getType()->getScalarType());
333 if (PtrTy->getPointerAddressSpace() != 0)
334 return std::nullopt;
335
336 // Ignore swifterror addresses.
337 // swifterror memory addresses are mem2reg promoted by instruction
338 // selection. As such they cannot have regular uses like an instrumentation
339 // function and it makes no sense to track them as memory.
340 if (Access.Addr->isSwiftError())
341 return std::nullopt;
342
343 // Peel off GEPs and BitCasts.
344 auto *Addr = Access.Addr->stripInBoundsOffsets();
345
346 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Addr)) {
347 // Do not instrument PGO counter updates.
348 if (GV->hasSection()) {
349 StringRef SectionName = GV->getSection();
350 // Check if the global is in the PGO counters section.
351 auto OF = Triple(I->getModule()->getTargetTriple()).getObjectFormat();
352 if (SectionName.endswith(
353 getInstrProfSectionName(IPSK_cnts, OF, /*AddSegmentInfo=*/false)))
354 return std::nullopt;
355 }
356
357 // Do not instrument accesses to LLVM internal variables.
358 if (GV->getName().startswith("__llvm"))
359 return std::nullopt;
360 }
361
362 const DataLayout &DL = I->getModule()->getDataLayout();
363 Access.TypeSize = DL.getTypeStoreSizeInBits(Access.AccessTy);
364 return Access;
365}
366
367void MemProfiler::instrumentMaskedLoadOrStore(const DataLayout &DL, Value *Mask,
369 Type *AccessTy, bool IsWrite) {
370 auto *VTy = cast<FixedVectorType>(AccessTy);
371 uint64_t ElemTypeSize = DL.getTypeStoreSizeInBits(VTy->getScalarType());
372 unsigned Num = VTy->getNumElements();
373 auto *Zero = ConstantInt::get(IntptrTy, 0);
374 for (unsigned Idx = 0; Idx < Num; ++Idx) {
375 Value *InstrumentedAddress = nullptr;
376 Instruction *InsertBefore = I;
377 if (auto *Vector = dyn_cast<ConstantVector>(Mask)) {
378 // dyn_cast as we might get UndefValue
379 if (auto *Masked = dyn_cast<ConstantInt>(Vector->getOperand(Idx))) {
380 if (Masked->isZero())
381 // Mask is constant false, so no instrumentation needed.
382 continue;
383 // If we have a true or undef value, fall through to instrumentAddress.
384 // with InsertBefore == I
385 }
386 } else {
387 IRBuilder<> IRB(I);
388 Value *MaskElem = IRB.CreateExtractElement(Mask, Idx);
389 Instruction *ThenTerm = SplitBlockAndInsertIfThen(MaskElem, I, false);
390 InsertBefore = ThenTerm;
391 }
392
393 IRBuilder<> IRB(InsertBefore);
394 InstrumentedAddress =
395 IRB.CreateGEP(VTy, Addr, {Zero, ConstantInt::get(IntptrTy, Idx)});
396 instrumentAddress(I, InsertBefore, InstrumentedAddress, ElemTypeSize,
397 IsWrite);
398 }
399}
400
401void MemProfiler::instrumentMop(Instruction *I, const DataLayout &DL,
402 InterestingMemoryAccess &Access) {
403 // Skip instrumentation of stack accesses unless requested.
404 if (!ClStack && isa<AllocaInst>(getUnderlyingObject(Access.Addr))) {
405 if (Access.IsWrite)
406 ++NumSkippedStackWrites;
407 else
408 ++NumSkippedStackReads;
409 return;
410 }
411
412 if (Access.IsWrite)
413 NumInstrumentedWrites++;
414 else
415 NumInstrumentedReads++;
416
417 if (Access.MaybeMask) {
418 instrumentMaskedLoadOrStore(DL, Access.MaybeMask, I, Access.Addr,
419 Access.AccessTy, Access.IsWrite);
420 } else {
421 // Since the access counts will be accumulated across the entire allocation,
422 // we only update the shadow access count for the first location and thus
423 // don't need to worry about alignment and type size.
424 instrumentAddress(I, I, Access.Addr, Access.TypeSize, Access.IsWrite);
425 }
426}
427
428void MemProfiler::instrumentAddress(Instruction *OrigIns,
429 Instruction *InsertBefore, Value *Addr,
430 uint32_t TypeSize, bool IsWrite) {
431 IRBuilder<> IRB(InsertBefore);
432 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
433
434 if (ClUseCalls) {
435 IRB.CreateCall(MemProfMemoryAccessCallback[IsWrite], AddrLong);
436 return;
437 }
438
439 // Create an inline sequence to compute shadow location, and increment the
440 // value by one.
441 Type *ShadowTy = Type::getInt64Ty(*C);
442 Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
443 Value *ShadowPtr = memToShadow(AddrLong, IRB);
444 Value *ShadowAddr = IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy);
445 Value *ShadowValue = IRB.CreateLoad(ShadowTy, ShadowAddr);
447 ShadowValue = IRB.CreateAdd(ShadowValue, Inc);
448 IRB.CreateStore(ShadowValue, ShadowAddr);
449}
450
451// Create the variable for the profile file name.
453 const MDString *MemProfFilename =
454 dyn_cast_or_null<MDString>(M.getModuleFlag("MemProfProfileFilename"));
455 if (!MemProfFilename)
456 return;
457 assert(!MemProfFilename->getString().empty() &&
458 "Unexpected MemProfProfileFilename metadata with empty string");
459 Constant *ProfileNameConst = ConstantDataArray::getString(
460 M.getContext(), MemProfFilename->getString(), true);
461 GlobalVariable *ProfileNameVar = new GlobalVariable(
462 M, ProfileNameConst->getType(), /*isConstant=*/true,
464 Triple TT(M.getTargetTriple());
465 if (TT.supportsCOMDAT()) {
467 ProfileNameVar->setComdat(M.getOrInsertComdat(MemProfFilenameVar));
468 }
469}
470
471bool ModuleMemProfiler::instrumentModule(Module &M) {
472 // Create a module constructor.
473 std::string MemProfVersion = std::to_string(LLVM_MEM_PROFILER_VERSION);
474 std::string VersionCheckName =
476 : "";
477 std::tie(MemProfCtorFunction, std::ignore) =
479 MemProfInitName, /*InitArgTypes=*/{},
480 /*InitArgs=*/{}, VersionCheckName);
481
482 const uint64_t Priority = getCtorAndDtorPriority(TargetTriple);
483 appendToGlobalCtors(M, MemProfCtorFunction, Priority);
484
486
487 return true;
488}
489
490void MemProfiler::initializeCallbacks(Module &M) {
491 IRBuilder<> IRB(*C);
492
493 for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
494 const std::string TypeStr = AccessIsWrite ? "store" : "load";
495
496 SmallVector<Type *, 3> Args2 = {IntptrTy, IntptrTy};
497 SmallVector<Type *, 2> Args1{1, IntptrTy};
498 MemProfMemoryAccessCallbackSized[AccessIsWrite] =
499 M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + TypeStr + "N",
500 FunctionType::get(IRB.getVoidTy(), Args2, false));
501
502 MemProfMemoryAccessCallback[AccessIsWrite] =
503 M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + TypeStr,
504 FunctionType::get(IRB.getVoidTy(), Args1, false));
505 }
506 MemProfMemmove = M.getOrInsertFunction(
508 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
509 MemProfMemcpy = M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "memcpy",
510 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
511 IRB.getInt8PtrTy(), IntptrTy);
512 MemProfMemset = M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "memset",
513 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
514 IRB.getInt32Ty(), IntptrTy);
515}
516
517bool MemProfiler::maybeInsertMemProfInitAtFunctionEntry(Function &F) {
518 // For each NSObject descendant having a +load method, this method is invoked
519 // by the ObjC runtime before any of the static constructors is called.
520 // Therefore we need to instrument such methods with a call to __memprof_init
521 // at the beginning in order to initialize our runtime before any access to
522 // the shadow memory.
523 // We cannot just ignore these methods, because they may call other
524 // instrumented functions.
525 if (F.getName().find(" load]") != std::string::npos) {
526 FunctionCallee MemProfInitFunction =
528 IRBuilder<> IRB(&F.front(), F.front().begin());
529 IRB.CreateCall(MemProfInitFunction, {});
530 return true;
531 }
532 return false;
533}
534
535bool MemProfiler::insertDynamicShadowAtFunctionEntry(Function &F) {
536 IRBuilder<> IRB(&F.front().front());
537 Value *GlobalDynamicAddress = F.getParent()->getOrInsertGlobal(
539 if (F.getParent()->getPICLevel() == PICLevel::NotPIC)
540 cast<GlobalVariable>(GlobalDynamicAddress)->setDSOLocal(true);
541 DynamicShadowOffset = IRB.CreateLoad(IntptrTy, GlobalDynamicAddress);
542 return true;
543}
544
545bool MemProfiler::instrumentFunction(Function &F) {
546 if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage)
547 return false;
548 if (ClDebugFunc == F.getName())
549 return false;
550 if (F.getName().startswith("__memprof_"))
551 return false;
552
553 bool FunctionModified = false;
554
555 // If needed, insert __memprof_init.
556 // This function needs to be called even if the function body is not
557 // instrumented.
558 if (maybeInsertMemProfInitAtFunctionEntry(F))
559 FunctionModified = true;
560
561 LLVM_DEBUG(dbgs() << "MEMPROF instrumenting:\n" << F << "\n");
562
563 initializeCallbacks(*F.getParent());
564
566
567 // Fill the set of memory operations to instrument.
568 for (auto &BB : F) {
569 for (auto &Inst : BB) {
570 if (isInterestingMemoryAccess(&Inst) || isa<MemIntrinsic>(Inst))
571 ToInstrument.push_back(&Inst);
572 }
573 }
574
575 if (ToInstrument.empty()) {
576 LLVM_DEBUG(dbgs() << "MEMPROF done instrumenting: " << FunctionModified
577 << " " << F << "\n");
578
579 return FunctionModified;
580 }
581
582 FunctionModified |= insertDynamicShadowAtFunctionEntry(F);
583
584 int NumInstrumented = 0;
585 for (auto *Inst : ToInstrument) {
586 if (ClDebugMin < 0 || ClDebugMax < 0 ||
587 (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
588 std::optional<InterestingMemoryAccess> Access =
589 isInterestingMemoryAccess(Inst);
590 if (Access)
591 instrumentMop(Inst, F.getParent()->getDataLayout(), *Access);
592 else
593 instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
594 }
595 NumInstrumented++;
596 }
597
598 if (NumInstrumented > 0)
599 FunctionModified = true;
600
601 LLVM_DEBUG(dbgs() << "MEMPROF done instrumenting: " << FunctionModified << " "
602 << F << "\n");
603
604 return FunctionModified;
605}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static cl::opt< int > ClDebugMin("asan-debug-min", cl::desc("Debug min inst"), cl::Hidden, cl::init(-1))
static cl::opt< std::string > ClMemoryAccessCallbackPrefix("asan-memory-access-callback-prefix", cl::desc("Prefix for memory access callbacks"), cl::Hidden, cl::init("__asan_"))
static cl::opt< bool > ClInsertVersionCheck("asan-guard-against-version-mismatch", cl::desc("Guard against compiler/runtime version mismatch."), cl::Hidden, cl::init(true))
static cl::opt< bool > ClInstrumentWrites("asan-instrument-writes", cl::desc("instrument write instructions"), cl::Hidden, cl::init(true))
static cl::opt< int > ClDebugMax("asan-debug-max", cl::desc("Debug max inst"), cl::Hidden, cl::init(-1))
static cl::opt< bool > ClStack("asan-stack", cl::desc("Handle stack memory"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClInstrumentAtomics("asan-instrument-atomics", cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden, cl::init(true))
static cl::opt< int > ClMappingScale("asan-mapping-scale", cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0))
static cl::opt< std::string > ClDebugFunc("asan-debug-func", cl::Hidden, cl::desc("Debug func"))
static cl::opt< bool > ClInstrumentReads("asan-instrument-reads", cl::desc("instrument read instructions"), cl::Hidden, cl::init(true))
static void instrumentMaskedLoadOrStore(AddressSanitizer *Pass, const DataLayout &DL, Type *IntptrTy, Value *Mask, Instruction *I, Value *Addr, MaybeAlign Alignment, unsigned Granularity, Type *OpType, bool IsWrite, Value *SizeArgument, bool UseCalls, uint32_t Exp)
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
uint64_t Addr
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
constexpr char MemProfVersionCheckNamePrefix[]
Definition: MemProfiler.cpp:58
static cl::opt< int > ClDebugMin("memprof-debug-min", cl::desc("Debug min inst"), cl::Hidden, cl::init(-1))
constexpr uint64_t MemProfEmscriptenCtorAndDtorPriority
Definition: MemProfiler.cpp:56
static cl::opt< std::string > ClDebugFunc("memprof-debug-func", cl::Hidden, cl::desc("Debug func"))
constexpr char MemProfShadowMemoryDynamicAddress[]
Definition: MemProfiler.cpp:61
constexpr uint64_t MemProfCtorAndDtorPriority
Definition: MemProfiler.cpp:54
constexpr int LLVM_MEM_PROFILER_VERSION
Definition: MemProfiler.cpp:45
static cl::opt< bool > ClUseCalls("memprof-use-callbacks", cl::desc("Use callbacks instead of inline instrumentation sequences."), cl::Hidden, cl::init(false))
static cl::opt< bool > ClInstrumentAtomics("memprof-instrument-atomics", cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden, cl::init(true))
static cl::opt< bool > ClInsertVersionCheck("memprof-guard-against-version-mismatch", cl::desc("Guard against compiler/runtime version mismatch."), cl::Hidden, cl::init(true))
constexpr char MemProfInitName[]
Definition: MemProfiler.cpp:57
constexpr char MemProfFilenameVar[]
Definition: MemProfiler.cpp:64
static cl::opt< bool > ClStack("memprof-instrument-stack", cl::desc("Instrument scalar stack variables"), cl::Hidden, cl::init(false))
constexpr uint64_t DefaultShadowGranularity
Definition: MemProfiler.cpp:48
constexpr uint64_t DefaultShadowScale
Definition: MemProfiler.cpp:51
static cl::opt< std::string > ClMemoryAccessCallbackPrefix("memprof-memory-access-callback-prefix", cl::desc("Prefix for memory access callbacks"), cl::Hidden, cl::init("__memprof_"))
constexpr char MemProfModuleCtorName[]
Definition: MemProfiler.cpp:53
static cl::opt< bool > ClInstrumentReads("memprof-instrument-reads", cl::desc("instrument read instructions"), cl::Hidden, cl::init(true))
static cl::opt< int > ClDebugMax("memprof-debug-max", cl::desc("Debug max inst"), cl::Hidden, cl::init(-1))
static cl::opt< bool > ClInstrumentWrites("memprof-instrument-writes", cl::desc("instrument write instructions"), cl::Hidden, cl::init(true))
static cl::opt< int > ClDebug("memprof-debug", cl::desc("debug"), cl::Hidden, cl::init(0))
static cl::opt< int > ClMappingScale("memprof-mapping-scale", cl::desc("scale of memprof shadow mapping"), cl::Hidden, cl::init(DefaultShadowScale))
static cl::opt< int > ClMappingGranularity("memprof-mapping-granularity", cl::desc("granularity of memprof shadow mapping"), cl::Hidden, cl::init(DefaultShadowGranularity))
Module.h This file contains the declarations for the Module class.
@ SI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:513
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:718
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2946
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:888
This is an important base class in LLVM.
Definition: Constant.h:41
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:165
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
void setComdat(Comdat *C)
Definition: Globals.cpp:198
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:532
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:52
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:49
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2349
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2062
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2011
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1360
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:512
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition: IRBuilder.h:1713
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
Definition: IRBuilder.h:560
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1398
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1726
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1250
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Definition: IRBuilder.h:2085
Type * getVoidTy()
Fetch the type representing void.
Definition: IRBuilder.h:550
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2301
Value * CreateGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="", bool IsInBounds=false)
Definition: IRBuilder.h:1789
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2558
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
An instruction for reading from memory.
Definition: Instructions.h:177
A single uniqued string.
Definition: Metadata.h:611
StringRef getString() const
Definition: Metadata.cpp:507
This is the common base class for memset/memcpy/memmove.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
An instruction for storing to memory.
Definition: Instructions.h:301
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
ObjectFormatType getObjectFormat() const
Get the object format for this triple.
Definition: Triple.h:382
bool isOSEmscripten() const
Tests whether the OS is Emscripten.
Definition: Triple.h:658
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
static IntegerType * getInt64Ty(LLVMContext &C)
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:119
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value,...
std::string getInstrProfSectionName(InstrProfSectKind IPSK, Triple::ObjectFormatType OF, bool AddSegmentInfo=true)
Return the name of the profile section corresponding to IPSK.
Definition: InstrProf.cpp:213
FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName, ArrayRef< Type * > InitArgTypes, bool Weak=false)
std::pair< Function *, FunctionCallee > createSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, StringRef VersionCheckName=StringRef(), bool Weak=false)
Creates sanitizer constructor function, and calls sanitizer's init function from it.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void createProfileFileNameVar(Module &M, StringRef InstrProfileOutput)
Definition: InstrProf.cpp:1206
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
Definition: ModuleUtils.cpp:71
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights, DominatorTree *DT, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...