Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 1 | //===- PointerTracking.cpp - Pointer Bounds Tracking ------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements tracking of pointer bounds. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 13 | |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/ConstantFolding.h" |
| 15 | #include "llvm/Analysis/Dominators.h" |
| 16 | #include "llvm/Analysis/LoopInfo.h" |
Victor Hernandez | f006b18 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/MemoryBuiltins.h" |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/PointerTracking.h" |
| 19 | #include "llvm/Analysis/ScalarEvolution.h" |
| 20 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Value.h" |
| 24 | #include "llvm/Support/CallSite.h" |
| 25 | #include "llvm/Support/InstIterator.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 9661c13 | 2009-08-24 02:39:26 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 9661c13 | 2009-08-24 02:39:26 +0000 | [diff] [blame] | 30 | char PointerTracking::ID = 0; |
Owen Anderson | 9ccaf53 | 2010-08-05 23:42:04 +0000 | [diff] [blame] | 31 | PointerTracking::PointerTracking() : FunctionPass(ID) {} |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 32 | |
| 33 | bool PointerTracking::runOnFunction(Function &F) { |
| 34 | predCache.clear(); |
| 35 | assert(analyzing.empty()); |
| 36 | FF = &F; |
| 37 | TD = getAnalysisIfAvailable<TargetData>(); |
| 38 | SE = &getAnalysis<ScalarEvolution>(); |
| 39 | LI = &getAnalysis<LoopInfo>(); |
| 40 | DT = &getAnalysis<DominatorTree>(); |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | void PointerTracking::getAnalysisUsage(AnalysisUsage &AU) const { |
| 45 | AU.addRequiredTransitive<DominatorTree>(); |
| 46 | AU.addRequiredTransitive<LoopInfo>(); |
| 47 | AU.addRequiredTransitive<ScalarEvolution>(); |
| 48 | AU.setPreservesAll(); |
| 49 | } |
| 50 | |
| 51 | bool PointerTracking::doInitialization(Module &M) { |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 52 | const Type *PTy = Type::getInt8PtrTy(M.getContext()); |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 53 | |
| 54 | // Find calloc(i64, i64) or calloc(i32, i32). |
| 55 | callocFunc = M.getFunction("calloc"); |
| 56 | if (callocFunc) { |
| 57 | const FunctionType *Ty = callocFunc->getFunctionType(); |
| 58 | |
| 59 | std::vector<const Type*> args, args2; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 60 | args.push_back(Type::getInt64Ty(M.getContext())); |
| 61 | args.push_back(Type::getInt64Ty(M.getContext())); |
| 62 | args2.push_back(Type::getInt32Ty(M.getContext())); |
| 63 | args2.push_back(Type::getInt32Ty(M.getContext())); |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 64 | const FunctionType *Calloc1Type = |
| 65 | FunctionType::get(PTy, args, false); |
| 66 | const FunctionType *Calloc2Type = |
| 67 | FunctionType::get(PTy, args2, false); |
| 68 | if (Ty != Calloc1Type && Ty != Calloc2Type) |
| 69 | callocFunc = 0; // Give up |
| 70 | } |
| 71 | |
| 72 | // Find realloc(i8*, i64) or realloc(i8*, i32). |
| 73 | reallocFunc = M.getFunction("realloc"); |
| 74 | if (reallocFunc) { |
| 75 | const FunctionType *Ty = reallocFunc->getFunctionType(); |
| 76 | std::vector<const Type*> args, args2; |
| 77 | args.push_back(PTy); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 78 | args.push_back(Type::getInt64Ty(M.getContext())); |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 79 | args2.push_back(PTy); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 80 | args2.push_back(Type::getInt32Ty(M.getContext())); |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 81 | |
| 82 | const FunctionType *Realloc1Type = |
| 83 | FunctionType::get(PTy, args, false); |
| 84 | const FunctionType *Realloc2Type = |
| 85 | FunctionType::get(PTy, args2, false); |
| 86 | if (Ty != Realloc1Type && Ty != Realloc2Type) |
| 87 | reallocFunc = 0; // Give up |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | // Calculates the number of elements allocated for pointer P, |
| 93 | // the type of the element is stored in Ty. |
| 94 | const SCEV *PointerTracking::computeAllocationCount(Value *P, |
| 95 | const Type *&Ty) const { |
| 96 | Value *V = P->stripPointerCasts(); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 97 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 98 | Value *arraySize = AI->getArraySize(); |
| 99 | Ty = AI->getAllocatedType(); |
| 100 | // arraySize elements of type Ty. |
| 101 | return SE->getSCEV(arraySize); |
| 102 | } |
| 103 | |
Victor Hernandez | 46e8312 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 104 | if (CallInst *CI = extractMallocCall(V)) { |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 105 | Value *arraySize = getMallocArraySize(CI, TD); |
Victor Hernandez | 2491ce0 | 2009-10-15 20:14:52 +0000 | [diff] [blame] | 106 | const Type* AllocTy = getMallocAllocatedType(CI); |
| 107 | if (!AllocTy || !arraySize) return SE->getCouldNotCompute(); |
| 108 | Ty = AllocTy; |
Victor Hernandez | 46e8312 | 2009-09-18 21:34:51 +0000 | [diff] [blame] | 109 | // arraySize elements of type Ty. |
| 110 | return SE->getSCEV(arraySize); |
| 111 | } |
| 112 | |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 113 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 114 | if (GV->hasDefinitiveInitializer()) { |
| 115 | Constant *C = GV->getInitializer(); |
| 116 | if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { |
| 117 | Ty = ATy->getElementType(); |
Owen Anderson | 0e275dc | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 118 | return SE->getConstant(Type::getInt32Ty(P->getContext()), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 119 | ATy->getNumElements()); |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | Ty = GV->getType(); |
Owen Anderson | 0e275dc | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 123 | return SE->getConstant(Type::getInt32Ty(P->getContext()), 1); |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 124 | //TODO: implement more tracking for globals |
| 125 | } |
| 126 | |
| 127 | if (CallInst *CI = dyn_cast<CallInst>(V)) { |
| 128 | CallSite CS(CI); |
| 129 | Function *F = dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts()); |
| 130 | const Loop *L = LI->getLoopFor(CI->getParent()); |
| 131 | if (F == callocFunc) { |
Owen Anderson | 0e275dc | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 132 | Ty = Type::getInt8Ty(P->getContext()); |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 133 | // calloc allocates arg0*arg1 bytes. |
| 134 | return SE->getSCEVAtScope(SE->getMulExpr(SE->getSCEV(CS.getArgument(0)), |
| 135 | SE->getSCEV(CS.getArgument(1))), |
| 136 | L); |
| 137 | } else if (F == reallocFunc) { |
Owen Anderson | 0e275dc | 2009-08-13 23:27:32 +0000 | [diff] [blame] | 138 | Ty = Type::getInt8Ty(P->getContext()); |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 139 | // realloc allocates arg1 bytes. |
| 140 | return SE->getSCEVAtScope(CS.getArgument(1), L); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return SE->getCouldNotCompute(); |
| 145 | } |
| 146 | |
Torok Edwin | e89652c | 2010-08-04 11:42:45 +0000 | [diff] [blame] | 147 | Value *PointerTracking::computeAllocationCountValue(Value *P, const Type *&Ty) const |
| 148 | { |
| 149 | Value *V = P->stripPointerCasts(); |
| 150 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { |
| 151 | Ty = AI->getAllocatedType(); |
| 152 | // arraySize elements of type Ty. |
| 153 | return AI->getArraySize(); |
| 154 | } |
| 155 | |
| 156 | if (CallInst *CI = extractMallocCall(V)) { |
| 157 | Ty = getMallocAllocatedType(CI); |
| 158 | if (!Ty) |
| 159 | return 0; |
| 160 | Value *arraySize = getMallocArraySize(CI, TD); |
| 161 | if (!arraySize) { |
| 162 | Ty = Type::getInt8Ty(P->getContext()); |
| 163 | return CI->getArgOperand(0); |
| 164 | } |
| 165 | // arraySize elements of type Ty. |
| 166 | return arraySize; |
| 167 | } |
| 168 | |
| 169 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 170 | if (GV->hasDefinitiveInitializer()) { |
| 171 | Constant *C = GV->getInitializer(); |
| 172 | if (const ArrayType *ATy = dyn_cast<ArrayType>(C->getType())) { |
| 173 | Ty = ATy->getElementType(); |
| 174 | return ConstantInt::get(Type::getInt32Ty(P->getContext()), |
| 175 | ATy->getNumElements()); |
| 176 | } |
| 177 | } |
| 178 | Ty = cast<PointerType>(GV->getType())->getElementType(); |
| 179 | return ConstantInt::get(Type::getInt32Ty(P->getContext()), 1); |
| 180 | //TODO: implement more tracking for globals |
| 181 | } |
| 182 | |
| 183 | if (CallInst *CI = dyn_cast<CallInst>(V)) { |
| 184 | CallSite CS(CI); |
| 185 | Function *F = dyn_cast<Function>(CS.getCalledValue()->stripPointerCasts()); |
| 186 | if (F == reallocFunc) { |
| 187 | Ty = Type::getInt8Ty(P->getContext()); |
| 188 | // realloc allocates arg1 bytes. |
| 189 | return CS.getArgument(1); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 196 | // Calculates the number of elements of type Ty allocated for P. |
| 197 | const SCEV *PointerTracking::computeAllocationCountForType(Value *P, |
| 198 | const Type *Ty) |
| 199 | const { |
| 200 | const Type *elementTy; |
| 201 | const SCEV *Count = computeAllocationCount(P, elementTy); |
| 202 | if (isa<SCEVCouldNotCompute>(Count)) |
| 203 | return Count; |
| 204 | if (elementTy == Ty) |
| 205 | return Count; |
| 206 | |
| 207 | if (!TD) // need TargetData from this point forward |
| 208 | return SE->getCouldNotCompute(); |
| 209 | |
| 210 | uint64_t elementSize = TD->getTypeAllocSize(elementTy); |
| 211 | uint64_t wantSize = TD->getTypeAllocSize(Ty); |
| 212 | if (elementSize == wantSize) |
| 213 | return Count; |
| 214 | if (elementSize % wantSize) //fractional counts not possible |
| 215 | return SE->getCouldNotCompute(); |
| 216 | return SE->getMulExpr(Count, SE->getConstant(Count->getType(), |
| 217 | elementSize/wantSize)); |
| 218 | } |
| 219 | |
| 220 | const SCEV *PointerTracking::getAllocationElementCount(Value *V) const { |
| 221 | // We only deal with pointers. |
| 222 | const PointerType *PTy = cast<PointerType>(V->getType()); |
| 223 | return computeAllocationCountForType(V, PTy->getElementType()); |
| 224 | } |
| 225 | |
| 226 | const SCEV *PointerTracking::getAllocationSizeInBytes(Value *V) const { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 227 | return computeAllocationCountForType(V, Type::getInt8Ty(V->getContext())); |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Helper for isLoopGuardedBy that checks the swapped and inverted predicate too |
| 231 | enum SolverResult PointerTracking::isLoopGuardedBy(const Loop *L, |
| 232 | Predicate Pred, |
| 233 | const SCEV *A, |
| 234 | const SCEV *B) const { |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 235 | if (SE->isLoopEntryGuardedByCond(L, Pred, A, B)) |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 236 | return AlwaysTrue; |
| 237 | Pred = ICmpInst::getSwappedPredicate(Pred); |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 238 | if (SE->isLoopEntryGuardedByCond(L, Pred, B, A)) |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 239 | return AlwaysTrue; |
| 240 | |
| 241 | Pred = ICmpInst::getInversePredicate(Pred); |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 242 | if (SE->isLoopEntryGuardedByCond(L, Pred, B, A)) |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 243 | return AlwaysFalse; |
| 244 | Pred = ICmpInst::getSwappedPredicate(Pred); |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 245 | if (SE->isLoopEntryGuardedByCond(L, Pred, A, B)) |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 246 | return AlwaysTrue; |
| 247 | return Unknown; |
| 248 | } |
| 249 | |
| 250 | enum SolverResult PointerTracking::checkLimits(const SCEV *Offset, |
| 251 | const SCEV *Limit, |
| 252 | BasicBlock *BB) |
| 253 | { |
| 254 | //FIXME: merge implementation |
| 255 | return Unknown; |
| 256 | } |
| 257 | |
| 258 | void PointerTracking::getPointerOffset(Value *Pointer, Value *&Base, |
| 259 | const SCEV *&Limit, |
| 260 | const SCEV *&Offset) const |
| 261 | { |
| 262 | Pointer = Pointer->stripPointerCasts(); |
| 263 | Base = Pointer->getUnderlyingObject(); |
| 264 | Limit = getAllocationSizeInBytes(Base); |
| 265 | if (isa<SCEVCouldNotCompute>(Limit)) { |
| 266 | Base = 0; |
| 267 | Offset = Limit; |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | Offset = SE->getMinusSCEV(SE->getSCEV(Pointer), SE->getSCEV(Base)); |
| 272 | if (isa<SCEVCouldNotCompute>(Offset)) { |
| 273 | Base = 0; |
| 274 | Limit = Offset; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | void PointerTracking::print(raw_ostream &OS, const Module* M) const { |
| 279 | // Calling some PT methods may cause caches to be updated, however |
| 280 | // this should be safe for the same reason its safe for SCEV. |
| 281 | PointerTracking &PT = *const_cast<PointerTracking*>(this); |
| 282 | for (inst_iterator I=inst_begin(*FF), E=inst_end(*FF); I != E; ++I) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 283 | if (!I->getType()->isPointerTy()) |
Torok Edwin | 969f28d | 2009-07-14 18:44:28 +0000 | [diff] [blame] | 284 | continue; |
| 285 | Value *Base; |
| 286 | const SCEV *Limit, *Offset; |
| 287 | getPointerOffset(&*I, Base, Limit, Offset); |
| 288 | if (!Base) |
| 289 | continue; |
| 290 | |
| 291 | if (Base == &*I) { |
| 292 | const SCEV *S = getAllocationElementCount(Base); |
| 293 | OS << *Base << " ==> " << *S << " elements, "; |
| 294 | OS << *Limit << " bytes allocated\n"; |
| 295 | continue; |
| 296 | } |
| 297 | OS << &*I << " -- base: " << *Base; |
| 298 | OS << " offset: " << *Offset; |
| 299 | |
| 300 | enum SolverResult res = PT.checkLimits(Offset, Limit, I->getParent()); |
| 301 | switch (res) { |
| 302 | case AlwaysTrue: |
| 303 | OS << " always safe\n"; |
| 304 | break; |
| 305 | case AlwaysFalse: |
| 306 | OS << " always unsafe\n"; |
| 307 | break; |
| 308 | case Unknown: |
| 309 | OS << " <<unknown>>\n"; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 315 | INITIALIZE_PASS(PointerTracking, "pointertracking", |
| 316 | "Track pointer bounds", false, true); |