From e89652ccad7b2685387a8327961a46f8bac0978b Mon Sep 17 00:00:00 2001 From: Torok Edwin Date: Wed, 4 Aug 2010 11:42:45 +0000 Subject: [PATCH] Add a missing function. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@110195 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Analysis/PointerTracking.h | 1 + lib/Analysis/PointerTracking.cpp | 49 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/llvm/Analysis/PointerTracking.h b/include/llvm/Analysis/PointerTracking.h index 6c4f838430b..6b49e18c1b6 100644 --- a/include/llvm/Analysis/PointerTracking.h +++ b/include/llvm/Analysis/PointerTracking.h @@ -98,6 +98,7 @@ namespace llvm { virtual bool runOnFunction(Function &F); virtual void getAnalysisUsage(AnalysisUsage &AU) const; void print(raw_ostream &OS, const Module* = 0) const; + Value *computeAllocationCountValue(Value *P, const Type *&Ty) const; private: Function *FF; TargetData *TD; diff --git a/lib/Analysis/PointerTracking.cpp b/lib/Analysis/PointerTracking.cpp index 142066e42ba..03d3059fdbb 100644 --- a/lib/Analysis/PointerTracking.cpp +++ b/lib/Analysis/PointerTracking.cpp @@ -144,6 +144,55 @@ const SCEV *PointerTracking::computeAllocationCount(Value *P, return SE->getCouldNotCompute(); } +Value *PointerTracking::computeAllocationCountValue(Value *P, const Type *&Ty) const +{ + Value *V = P->stripPointerCasts(); + if (AllocaInst *AI = dyn_cast(V)) { + Ty = AI->getAllocatedType(); + // arraySize elements of type Ty. + return AI->getArraySize(); + } + + if (CallInst *CI = extractMallocCall(V)) { + Ty = getMallocAllocatedType(CI); + if (!Ty) + return 0; + Value *arraySize = getMallocArraySize(CI, TD); + if (!arraySize) { + Ty = Type::getInt8Ty(P->getContext()); + return CI->getArgOperand(0); + } + // arraySize elements of type Ty. + return arraySize; + } + + if (GlobalVariable *GV = dyn_cast(V)) { + if (GV->hasDefinitiveInitializer()) { + Constant *C = GV->getInitializer(); + if (const ArrayType *ATy = dyn_cast(C->getType())) { + Ty = ATy->getElementType(); + return ConstantInt::get(Type::getInt32Ty(P->getContext()), + ATy->getNumElements()); + } + } + Ty = cast(GV->getType())->getElementType(); + return ConstantInt::get(Type::getInt32Ty(P->getContext()), 1); + //TODO: implement more tracking for globals + } + + if (CallInst *CI = dyn_cast(V)) { + CallSite CS(CI); + Function *F = dyn_cast(CS.getCalledValue()->stripPointerCasts()); + if (F == reallocFunc) { + Ty = Type::getInt8Ty(P->getContext()); + // realloc allocates arg1 bytes. + return CS.getArgument(1); + } + } + + return 0; +} + // Calculates the number of elements of type Ty allocated for P. const SCEV *PointerTracking::computeAllocationCountForType(Value *P, const Type *Ty) -- 2.34.1