1 //===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions identifies calls to builtin functions that allocate
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "memory-builtins"
16 #include "llvm/Analysis/MemoryBuiltins.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/ValueTracking.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/GlobalVariable.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Intrinsics.h"
24 #include "llvm/IR/Metadata.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Target/TargetLibraryInfo.h"
30 #include "llvm/Transforms/Utils/Local.h"
34 MallocLike = 1<<0, // allocates
35 CallocLike = 1<<1, // allocates + bzero
36 ReallocLike = 1<<2, // reallocates
38 AllocLike = MallocLike | CallocLike | StrDupLike,
39 AnyAlloc = MallocLike | CallocLike | ReallocLike | StrDupLike
45 unsigned char NumParams;
46 // First and Second size parameters (or -1 if unused)
47 signed char FstParam, SndParam;
50 // FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
51 // know which functions are nounwind, noalias, nocapture parameters, etc.
52 static const AllocFnsTy AllocationFnData[] = {
53 {LibFunc::malloc, MallocLike, 1, 0, -1},
54 {LibFunc::valloc, MallocLike, 1, 0, -1},
55 {LibFunc::Znwj, MallocLike, 1, 0, -1}, // new(unsigned int)
56 {LibFunc::ZnwjRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new(unsigned int, nothrow)
57 {LibFunc::Znwm, MallocLike, 1, 0, -1}, // new(unsigned long)
58 {LibFunc::ZnwmRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new(unsigned long, nothrow)
59 {LibFunc::Znaj, MallocLike, 1, 0, -1}, // new[](unsigned int)
60 {LibFunc::ZnajRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new[](unsigned int, nothrow)
61 {LibFunc::Znam, MallocLike, 1, 0, -1}, // new[](unsigned long)
62 {LibFunc::ZnamRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new[](unsigned long, nothrow)
63 {LibFunc::posix_memalign, MallocLike, 3, 2, -1},
64 {LibFunc::calloc, CallocLike, 2, 0, 1},
65 {LibFunc::realloc, ReallocLike, 2, 1, -1},
66 {LibFunc::reallocf, ReallocLike, 2, 1, -1},
67 {LibFunc::strdup, StrDupLike, 1, -1, -1},
68 {LibFunc::strndup, StrDupLike, 2, 1, -1}
72 static Function *getCalledFunction(const Value *V, bool LookThroughBitCast) {
73 if (LookThroughBitCast)
74 V = V->stripPointerCasts();
76 CallSite CS(const_cast<Value*>(V));
77 if (!CS.getInstruction())
83 Function *Callee = CS.getCalledFunction();
84 if (!Callee || !Callee->isDeclaration())
89 /// \brief Returns the allocation data for the given value if it is a call to a
90 /// known allocation function, and NULL otherwise.
91 static const AllocFnsTy *getAllocationData(const Value *V, AllocType AllocTy,
92 const TargetLibraryInfo *TLI,
93 bool LookThroughBitCast = false) {
95 if (isa<IntrinsicInst>(V))
98 Function *Callee = getCalledFunction(V, LookThroughBitCast);
102 // Make sure that the function is available.
103 StringRef FnName = Callee->getName();
105 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
110 for ( ; i < array_lengthof(AllocationFnData); ++i) {
111 if (AllocationFnData[i].Func == TLIFn) {
119 const AllocFnsTy *FnData = &AllocationFnData[i];
120 if ((FnData->AllocTy & AllocTy) == 0)
123 // Check function prototype.
124 int FstParam = FnData->FstParam;
125 int SndParam = FnData->SndParam;
126 FunctionType *FTy = Callee->getFunctionType();
128 if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
129 FTy->getNumParams() == FnData->NumParams &&
131 (FTy->getParamType(FstParam)->isIntegerTy(32) ||
132 FTy->getParamType(FstParam)->isIntegerTy(64))) &&
134 FTy->getParamType(SndParam)->isIntegerTy(32) ||
135 FTy->getParamType(SndParam)->isIntegerTy(64)))
140 static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
141 ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
142 return CS && CS.hasFnAttr(Attribute::NoAlias);
146 /// \brief Tests if a value is a call or invoke to a library function that
147 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
149 bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
150 bool LookThroughBitCast) {
151 return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast);
154 /// \brief Tests if a value is a call or invoke to a function that returns a
155 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
156 bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
157 bool LookThroughBitCast) {
158 // it's safe to consider realloc as noalias since accessing the original
159 // pointer is undefined behavior
160 return isAllocationFn(V, TLI, LookThroughBitCast) ||
161 hasNoAliasAttr(V, LookThroughBitCast);
164 /// \brief Tests if a value is a call or invoke to a library function that
165 /// allocates uninitialized memory (such as malloc).
166 bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
167 bool LookThroughBitCast) {
168 return getAllocationData(V, MallocLike, TLI, LookThroughBitCast);
171 /// \brief Tests if a value is a call or invoke to a library function that
172 /// allocates zero-filled memory (such as calloc).
173 bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
174 bool LookThroughBitCast) {
175 return getAllocationData(V, CallocLike, TLI, LookThroughBitCast);
178 /// \brief Tests if a value is a call or invoke to a library function that
179 /// allocates memory (either malloc, calloc, or strdup like).
180 bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
181 bool LookThroughBitCast) {
182 return getAllocationData(V, AllocLike, TLI, LookThroughBitCast);
185 /// \brief Tests if a value is a call or invoke to a library function that
186 /// reallocates memory (such as realloc).
187 bool llvm::isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
188 bool LookThroughBitCast) {
189 return getAllocationData(V, ReallocLike, TLI, LookThroughBitCast);
192 /// extractMallocCall - Returns the corresponding CallInst if the instruction
193 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
194 /// ignore InvokeInst here.
195 const CallInst *llvm::extractMallocCall(const Value *I,
196 const TargetLibraryInfo *TLI) {
197 return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : 0;
200 static Value *computeArraySize(const CallInst *CI, const DataLayout *TD,
201 const TargetLibraryInfo *TLI,
202 bool LookThroughSExt = false) {
206 // The size of the malloc's result type must be known to determine array size.
207 Type *T = getMallocAllocatedType(CI, TLI);
208 if (!T || !T->isSized() || !TD)
211 unsigned ElementSize = TD->getTypeAllocSize(T);
212 if (StructType *ST = dyn_cast<StructType>(T))
213 ElementSize = TD->getStructLayout(ST)->getSizeInBytes();
215 // If malloc call's arg can be determined to be a multiple of ElementSize,
216 // return the multiple. Otherwise, return NULL.
217 Value *MallocArg = CI->getArgOperand(0);
219 if (ComputeMultiple(MallocArg, ElementSize, Multiple,
226 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
227 /// is a call to malloc whose array size can be determined and the array size
228 /// is not constant 1. Otherwise, return NULL.
229 const CallInst *llvm::isArrayMalloc(const Value *I,
230 const DataLayout *TD,
231 const TargetLibraryInfo *TLI) {
232 const CallInst *CI = extractMallocCall(I, TLI);
233 Value *ArraySize = computeArraySize(CI, TD, TLI);
235 if (ConstantInt *ConstSize = dyn_cast_or_null<ConstantInt>(ArraySize))
236 if (ConstSize->isOne())
239 // CI is a non-array malloc or we can't figure out that it is an array malloc.
243 /// getMallocType - Returns the PointerType resulting from the malloc call.
244 /// The PointerType depends on the number of bitcast uses of the malloc call:
245 /// 0: PointerType is the calls' return type.
246 /// 1: PointerType is the bitcast's result type.
247 /// >1: Unique PointerType cannot be determined, return NULL.
248 PointerType *llvm::getMallocType(const CallInst *CI,
249 const TargetLibraryInfo *TLI) {
250 assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
252 PointerType *MallocType = 0;
253 unsigned NumOfBitCastUses = 0;
255 // Determine if CallInst has a bitcast use.
256 for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end();
258 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
259 MallocType = cast<PointerType>(BCI->getDestTy());
263 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
264 if (NumOfBitCastUses == 1)
267 // Malloc call was not bitcast, so type is the malloc function's return type.
268 if (NumOfBitCastUses == 0)
269 return cast<PointerType>(CI->getType());
271 // Type could not be determined.
275 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
276 /// The Type depends on the number of bitcast uses of the malloc call:
277 /// 0: PointerType is the malloc calls' return type.
278 /// 1: PointerType is the bitcast's result type.
279 /// >1: Unique PointerType cannot be determined, return NULL.
280 Type *llvm::getMallocAllocatedType(const CallInst *CI,
281 const TargetLibraryInfo *TLI) {
282 PointerType *PT = getMallocType(CI, TLI);
283 return PT ? PT->getElementType() : 0;
286 /// getMallocArraySize - Returns the array size of a malloc call. If the
287 /// argument passed to malloc is a multiple of the size of the malloced type,
288 /// then return that multiple. For non-array mallocs, the multiple is
289 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
291 Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout *TD,
292 const TargetLibraryInfo *TLI,
293 bool LookThroughSExt) {
294 assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
295 return computeArraySize(CI, TD, TLI, LookThroughSExt);
299 /// extractCallocCall - Returns the corresponding CallInst if the instruction
300 /// is a calloc call.
301 const CallInst *llvm::extractCallocCall(const Value *I,
302 const TargetLibraryInfo *TLI) {
303 return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : 0;
307 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
308 const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
309 const CallInst *CI = dyn_cast<CallInst>(I);
310 if (!CI || isa<IntrinsicInst>(CI))
312 Function *Callee = CI->getCalledFunction();
313 if (Callee == 0 || !Callee->isDeclaration())
316 StringRef FnName = Callee->getName();
318 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
321 unsigned ExpectedNumParams;
322 if (TLIFn == LibFunc::free ||
323 TLIFn == LibFunc::ZdlPv || // operator delete(void*)
324 TLIFn == LibFunc::ZdaPv) // operator delete[](void*)
325 ExpectedNumParams = 1;
326 else if (TLIFn == LibFunc::ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
327 TLIFn == LibFunc::ZdaPvRKSt9nothrow_t) // delete[](void*, nothrow)
328 ExpectedNumParams = 2;
332 // Check free prototype.
333 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
334 // attribute will exist.
335 FunctionType *FTy = Callee->getFunctionType();
336 if (!FTy->getReturnType()->isVoidTy())
338 if (FTy->getNumParams() != ExpectedNumParams)
340 if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
348 //===----------------------------------------------------------------------===//
349 // Utility functions to compute size of objects.
353 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
354 /// object size in Size if successful, and false otherwise.
355 /// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
356 /// byval arguments, and global variables.
357 bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *TD,
358 const TargetLibraryInfo *TLI, bool RoundToAlign) {
362 ObjectSizeOffsetVisitor Visitor(TD, TLI, Ptr->getContext(), RoundToAlign);
363 SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
364 if (!Visitor.bothKnown(Data))
367 APInt ObjSize = Data.first, Offset = Data.second;
368 // check for overflow
369 if (Offset.slt(0) || ObjSize.ult(Offset))
372 Size = (ObjSize - Offset).getZExtValue();
377 STATISTIC(ObjectVisitorArgument,
378 "Number of arguments with unsolved size and offset");
379 STATISTIC(ObjectVisitorLoad,
380 "Number of load instructions with unsolved size and offset");
383 APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
384 if (RoundToAlign && Align)
385 return APInt(IntTyBits, RoundUpToAlignment(Size.getZExtValue(), Align));
389 ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout *TD,
390 const TargetLibraryInfo *TLI,
391 LLVMContext &Context,
393 : TD(TD), TLI(TLI), RoundToAlign(RoundToAlign) {
394 IntegerType *IntTy = TD->getIntPtrType(Context);
395 IntTyBits = IntTy->getBitWidth();
396 Zero = APInt::getNullValue(IntTyBits);
399 SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
400 V = V->stripPointerCasts();
401 if (Instruction *I = dyn_cast<Instruction>(V)) {
402 // If we have already seen this instruction, bail out. Cycles can happen in
403 // unreachable code after constant propagation.
404 if (!SeenInsts.insert(I))
407 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
408 return visitGEPOperator(*GEP);
411 if (Argument *A = dyn_cast<Argument>(V))
412 return visitArgument(*A);
413 if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
414 return visitConstantPointerNull(*P);
415 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
416 return visitGlobalAlias(*GA);
417 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
418 return visitGlobalVariable(*GV);
419 if (UndefValue *UV = dyn_cast<UndefValue>(V))
420 return visitUndefValue(*UV);
421 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
422 if (CE->getOpcode() == Instruction::IntToPtr)
423 return unknown(); // clueless
424 if (CE->getOpcode() == Instruction::GetElementPtr)
425 return visitGEPOperator(cast<GEPOperator>(*CE));
428 DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " << *V
433 SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
434 if (!I.getAllocatedType()->isSized())
437 APInt Size(IntTyBits, TD->getTypeAllocSize(I.getAllocatedType()));
438 if (!I.isArrayAllocation())
439 return std::make_pair(align(Size, I.getAlignment()), Zero);
441 Value *ArraySize = I.getArraySize();
442 if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
443 Size *= C->getValue().zextOrSelf(IntTyBits);
444 return std::make_pair(align(Size, I.getAlignment()), Zero);
449 SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
450 // no interprocedural analysis is done at the moment
451 if (!A.hasByValAttr()) {
452 ++ObjectVisitorArgument;
455 PointerType *PT = cast<PointerType>(A.getType());
456 APInt Size(IntTyBits, TD->getTypeAllocSize(PT->getElementType()));
457 return std::make_pair(align(Size, A.getParamAlignment()), Zero);
460 SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
461 const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
466 // handle strdup-like functions separately
467 if (FnData->AllocTy == StrDupLike) {
468 APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
472 // strndup limits strlen
473 if (FnData->FstParam > 0) {
474 ConstantInt *Arg= dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
478 APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
479 if (Size.ugt(MaxSize))
482 return std::make_pair(Size, Zero);
485 ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
489 APInt Size = Arg->getValue().zextOrSelf(IntTyBits);
490 // size determined by just 1 parameter
491 if (FnData->SndParam < 0)
492 return std::make_pair(Size, Zero);
494 Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
498 Size *= Arg->getValue().zextOrSelf(IntTyBits);
499 return std::make_pair(Size, Zero);
501 // TODO: handle more standard functions (+ wchar cousins):
502 // - strdup / strndup
503 // - strcpy / strncpy
504 // - strcat / strncat
505 // - memcpy / memmove
506 // - strcat / strncat
511 ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull&) {
512 return std::make_pair(Zero, Zero);
516 ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
521 ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
522 // Easy cases were already folded by previous passes.
526 SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
527 SizeOffsetType PtrData = compute(GEP.getPointerOperand());
528 APInt Offset(IntTyBits, 0);
529 if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(*TD, Offset))
532 return std::make_pair(PtrData.first, PtrData.second + Offset);
535 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
536 if (GA.mayBeOverridden())
538 return compute(GA.getAliasee());
541 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
542 if (!GV.hasDefinitiveInitializer())
545 APInt Size(IntTyBits, TD->getTypeAllocSize(GV.getType()->getElementType()));
546 return std::make_pair(align(Size, GV.getAlignment()), Zero);
549 SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
554 SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
559 SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) {
560 // too complex to analyze statically.
564 SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
565 SizeOffsetType TrueSide = compute(I.getTrueValue());
566 SizeOffsetType FalseSide = compute(I.getFalseValue());
567 if (bothKnown(TrueSide) && bothKnown(FalseSide) && TrueSide == FalseSide)
572 SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
573 return std::make_pair(Zero, Zero);
576 SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
577 DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I << '\n');
582 ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(const DataLayout *TD,
583 const TargetLibraryInfo *TLI,
584 LLVMContext &Context)
585 : TD(TD), TLI(TLI), Context(Context), Builder(Context, TargetFolder(TD)) {
586 IntTy = TD->getIntPtrType(Context);
587 Zero = ConstantInt::get(IntTy, 0);
590 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
591 SizeOffsetEvalType Result = compute_(V);
593 if (!bothKnown(Result)) {
594 // erase everything that was computed in this iteration from the cache, so
595 // that no dangling references are left behind. We could be a bit smarter if
596 // we kept a dependency graph. It's probably not worth the complexity.
597 for (PtrSetTy::iterator I=SeenVals.begin(), E=SeenVals.end(); I != E; ++I) {
598 CacheMapTy::iterator CacheIt = CacheMap.find(*I);
599 // non-computable results can be safely cached
600 if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
601 CacheMap.erase(CacheIt);
609 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
610 ObjectSizeOffsetVisitor Visitor(TD, TLI, Context);
611 SizeOffsetType Const = Visitor.compute(V);
612 if (Visitor.bothKnown(Const))
613 return std::make_pair(ConstantInt::get(Context, Const.first),
614 ConstantInt::get(Context, Const.second));
616 V = V->stripPointerCasts();
619 CacheMapTy::iterator CacheIt = CacheMap.find(V);
620 if (CacheIt != CacheMap.end())
621 return CacheIt->second;
623 // always generate code immediately before the instruction being
624 // processed, so that the generated code dominates the same BBs
625 Instruction *PrevInsertPoint = Builder.GetInsertPoint();
626 if (Instruction *I = dyn_cast<Instruction>(V))
627 Builder.SetInsertPoint(I);
629 // record the pointers that were handled in this run, so that they can be
630 // cleaned later if something fails
633 // now compute the size and offset
634 SizeOffsetEvalType Result;
635 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
636 Result = visitGEPOperator(*GEP);
637 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
639 } else if (isa<Argument>(V) ||
640 (isa<ConstantExpr>(V) &&
641 cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
642 isa<GlobalAlias>(V) ||
643 isa<GlobalVariable>(V)) {
644 // ignore values where we cannot do more than what ObjectSizeVisitor can
647 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: "
653 Builder.SetInsertPoint(PrevInsertPoint);
655 // Don't reuse CacheIt since it may be invalid at this point.
656 CacheMap[V] = Result;
660 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
661 if (!I.getAllocatedType()->isSized())
665 assert(I.isArrayAllocation());
666 Value *ArraySize = I.getArraySize();
667 Value *Size = ConstantInt::get(ArraySize->getType(),
668 TD->getTypeAllocSize(I.getAllocatedType()));
669 Size = Builder.CreateMul(Size, ArraySize);
670 return std::make_pair(Size, Zero);
673 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
674 const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
679 // handle strdup-like functions separately
680 if (FnData->AllocTy == StrDupLike) {
685 Value *FirstArg = CS.getArgument(FnData->FstParam);
686 FirstArg = Builder.CreateZExt(FirstArg, IntTy);
687 if (FnData->SndParam < 0)
688 return std::make_pair(FirstArg, Zero);
690 Value *SecondArg = CS.getArgument(FnData->SndParam);
691 SecondArg = Builder.CreateZExt(SecondArg, IntTy);
692 Value *Size = Builder.CreateMul(FirstArg, SecondArg);
693 return std::make_pair(Size, Zero);
695 // TODO: handle more standard functions (+ wchar cousins):
696 // - strdup / strndup
697 // - strcpy / strncpy
698 // - strcat / strncat
699 // - memcpy / memmove
700 // - strcat / strncat
705 ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
710 ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
715 ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
716 SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
717 if (!bothKnown(PtrData))
720 Value *Offset = EmitGEPOffset(&Builder, *TD, &GEP, /*NoAssumptions=*/true);
721 Offset = Builder.CreateAdd(PtrData.second, Offset);
722 return std::make_pair(PtrData.first, Offset);
725 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
730 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
734 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
735 // create 2 PHIs: one for size and another for offset
736 PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
737 PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
739 // insert right away in the cache to handle recursive PHIs
740 CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
742 // compute offset/size for each PHI incoming pointer
743 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
744 Builder.SetInsertPoint(PHI.getIncomingBlock(i)->getFirstInsertionPt());
745 SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
747 if (!bothKnown(EdgeData)) {
748 OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
749 OffsetPHI->eraseFromParent();
750 SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
751 SizePHI->eraseFromParent();
754 SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
755 OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
758 Value *Size = SizePHI, *Offset = OffsetPHI, *Tmp;
759 if ((Tmp = SizePHI->hasConstantValue())) {
761 SizePHI->replaceAllUsesWith(Size);
762 SizePHI->eraseFromParent();
764 if ((Tmp = OffsetPHI->hasConstantValue())) {
766 OffsetPHI->replaceAllUsesWith(Offset);
767 OffsetPHI->eraseFromParent();
769 return std::make_pair(Size, Offset);
772 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
773 SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
774 SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
776 if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
778 if (TrueSide == FalseSide)
781 Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
783 Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
785 return std::make_pair(Size, Offset);
788 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
789 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I <<'\n');