BasicAliasAnalysis.cpp revision 21de4c0daf2b35963d16541a3007c543237eb7bf
1//===- BasicAliasAnalysis.cpp - Local Alias Analysis Impl -----------------===// 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 defines the default implementation of the Alias Analysis interface 11// that simply implements a few identities (two different globals cannot alias, 12// etc), but otherwise does no analysis. 13// 14//===----------------------------------------------------------------------===// 15 16#include "llvm/Analysis/AliasAnalysis.h" 17#include "llvm/Analysis/Passes.h" 18#include "llvm/Constants.h" 19#include "llvm/DerivedTypes.h" 20#include "llvm/Function.h" 21#include "llvm/GlobalVariable.h" 22#include "llvm/Instructions.h" 23#include "llvm/IntrinsicInst.h" 24#include "llvm/Operator.h" 25#include "llvm/Pass.h" 26#include "llvm/Analysis/CaptureTracking.h" 27#include "llvm/Analysis/MemoryBuiltins.h" 28#include "llvm/Analysis/ValueTracking.h" 29#include "llvm/Target/TargetData.h" 30#include "llvm/ADT/SmallPtrSet.h" 31#include "llvm/ADT/SmallVector.h" 32#include "llvm/Support/ErrorHandling.h" 33#include <algorithm> 34using namespace llvm; 35 36//===----------------------------------------------------------------------===// 37// Useful predicates 38//===----------------------------------------------------------------------===// 39 40/// isKnownNonNull - Return true if we know that the specified value is never 41/// null. 42static bool isKnownNonNull(const Value *V) { 43 // Alloca never returns null, malloc might. 44 if (isa<AllocaInst>(V)) return true; 45 46 // A byval argument is never null. 47 if (const Argument *A = dyn_cast<Argument>(V)) 48 return A->hasByValAttr(); 49 50 // Global values are not null unless extern weak. 51 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) 52 return !GV->hasExternalWeakLinkage(); 53 return false; 54} 55 56/// isNonEscapingLocalObject - Return true if the pointer is to a function-local 57/// object that never escapes from the function. 58static bool isNonEscapingLocalObject(const Value *V) { 59 // If this is a local allocation, check to see if it escapes. 60 if (isa<AllocaInst>(V) || isNoAliasCall(V)) 61 // Set StoreCaptures to True so that we can assume in our callers that the 62 // pointer is not the result of a load instruction. Currently 63 // PointerMayBeCaptured doesn't have any special analysis for the 64 // StoreCaptures=false case; if it did, our callers could be refined to be 65 // more precise. 66 return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true); 67 68 // If this is an argument that corresponds to a byval or noalias argument, 69 // then it has not escaped before entering the function. Check if it escapes 70 // inside the function. 71 if (const Argument *A = dyn_cast<Argument>(V)) 72 if (A->hasByValAttr() || A->hasNoAliasAttr()) { 73 // Don't bother analyzing arguments already known not to escape. 74 if (A->hasNoCaptureAttr()) 75 return true; 76 return !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true); 77 } 78 return false; 79} 80 81/// isEscapeSource - Return true if the pointer is one which would have 82/// been considered an escape by isNonEscapingLocalObject. 83static bool isEscapeSource(const Value *V) { 84 if (isa<CallInst>(V) || isa<InvokeInst>(V) || isa<Argument>(V)) 85 return true; 86 87 // The load case works because isNonEscapingLocalObject considers all 88 // stores to be escapes (it passes true for the StoreCaptures argument 89 // to PointerMayBeCaptured). 90 if (isa<LoadInst>(V)) 91 return true; 92 93 return false; 94} 95 96/// isObjectSmallerThan - Return true if we can prove that the object specified 97/// by V is smaller than Size. 98static bool isObjectSmallerThan(const Value *V, unsigned Size, 99 const TargetData &TD) { 100 const Type *AccessTy; 101 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { 102 AccessTy = GV->getType()->getElementType(); 103 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) { 104 if (!AI->isArrayAllocation()) 105 AccessTy = AI->getType()->getElementType(); 106 else 107 return false; 108 } else if (const CallInst* CI = extractMallocCall(V)) { 109 if (!isArrayMalloc(V, &TD)) 110 // The size is the argument to the malloc call. 111 if (const ConstantInt* C = dyn_cast<ConstantInt>(CI->getArgOperand(0))) 112 return (C->getZExtValue() < Size); 113 return false; 114 } else if (const Argument *A = dyn_cast<Argument>(V)) { 115 if (A->hasByValAttr()) 116 AccessTy = cast<PointerType>(A->getType())->getElementType(); 117 else 118 return false; 119 } else { 120 return false; 121 } 122 123 if (AccessTy->isSized()) 124 return TD.getTypeAllocSize(AccessTy) < Size; 125 return false; 126} 127 128//===----------------------------------------------------------------------===// 129// NoAA Pass 130//===----------------------------------------------------------------------===// 131 132namespace { 133 /// NoAA - This class implements the -no-aa pass, which always returns "I 134 /// don't know" for alias queries. NoAA is unlike other alias analysis 135 /// implementations, in that it does not chain to a previous analysis. As 136 /// such it doesn't follow many of the rules that other alias analyses must. 137 /// 138 struct NoAA : public ImmutablePass, public AliasAnalysis { 139 static char ID; // Class identification, replacement for typeinfo 140 NoAA() : ImmutablePass(&ID) {} 141 explicit NoAA(void *PID) : ImmutablePass(PID) { } 142 143 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 144 } 145 146 virtual void initializePass() { 147 TD = getAnalysisIfAvailable<TargetData>(); 148 } 149 150 virtual AliasResult alias(const Value *V1, unsigned V1Size, 151 const Value *V2, unsigned V2Size) { 152 return MayAlias; 153 } 154 155 virtual void getArgumentAccesses(Function *F, CallSite CS, 156 std::vector<PointerAccessInfo> &Info) { 157 llvm_unreachable("This method may not be called on this function!"); 158 } 159 160 virtual bool pointsToConstantMemory(const Value *P) { return false; } 161 virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) { 162 return ModRef; 163 } 164 virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) { 165 return ModRef; 166 } 167 168 virtual void deleteValue(Value *V) {} 169 virtual void copyValue(Value *From, Value *To) {} 170 171 /// getAdjustedAnalysisPointer - This method is used when a pass implements 172 /// an analysis interface through multiple inheritance. If needed, it should 173 /// override this to adjust the this pointer as needed for the specified pass 174 /// info. 175 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) { 176 if (PI->isPassID(&AliasAnalysis::ID)) 177 return (AliasAnalysis*)this; 178 return this; 179 } 180 }; 181} // End of anonymous namespace 182 183// Register this pass... 184char NoAA::ID = 0; 185static RegisterPass<NoAA> 186U("no-aa", "No Alias Analysis (always returns 'may' alias)", true, true); 187 188// Declare that we implement the AliasAnalysis interface 189static RegisterAnalysisGroup<AliasAnalysis> V(U); 190 191ImmutablePass *llvm::createNoAAPass() { return new NoAA(); } 192 193//===----------------------------------------------------------------------===// 194// BasicAliasAnalysis Pass 195//===----------------------------------------------------------------------===// 196 197static const Function *getParent(const Value *V) { 198 if (const Instruction *inst = dyn_cast<Instruction>(V)) 199 return inst->getParent()->getParent(); 200 201 if (const Argument *arg = dyn_cast<Argument>(V)) 202 return arg->getParent(); 203 204 return NULL; 205} 206 207static bool sameParent(const Value *O1, const Value *O2) { 208 209 const Function *F1 = getParent(O1); 210 const Function *F2 = getParent(O2); 211 212 return F1 && F1 == F2; 213} 214 215#ifdef XDEBUG 216static bool notDifferentParent(const Value *O1, const Value *O2) { 217 218 const Function *F1 = getParent(O1); 219 const Function *F2 = getParent(O2); 220 221 return !F1 || !F2 || F1 == F2; 222} 223#endif 224 225namespace { 226 /// BasicAliasAnalysis - This is the default alias analysis implementation. 227 /// Because it doesn't chain to a previous alias analysis (like -no-aa), it 228 /// derives from the NoAA class. 229 struct BasicAliasAnalysis : public NoAA { 230 /// Interprocedural - Flag for "interprocedural" mode, where we must 231 /// support queries of values which live in different functions. 232 bool Interprocedural; 233 234 static char ID; // Class identification, replacement for typeinfo 235 BasicAliasAnalysis() 236 : NoAA(&ID), Interprocedural(false) {} 237 BasicAliasAnalysis(void *PID, bool interprocedural) 238 : NoAA(PID), Interprocedural(interprocedural) {} 239 240 AliasResult alias(const Value *V1, unsigned V1Size, 241 const Value *V2, unsigned V2Size) { 242 assert(Visited.empty() && "Visited must be cleared after use!"); 243#ifdef XDEBUG 244 assert((Interprocedural || notDifferentParent(V1, V2)) && 245 "BasicAliasAnalysis (-basicaa) doesn't support interprocedural " 246 "queries; use InterproceduralAliasAnalysis " 247 "(-interprocedural-basic-aa) instead."); 248#endif 249 AliasResult Alias = aliasCheck(V1, V1Size, V2, V2Size); 250 Visited.clear(); 251 return Alias; 252 } 253 254 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); 255 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2); 256 257 /// pointsToConstantMemory - Chase pointers until we find a (constant 258 /// global) or not. 259 bool pointsToConstantMemory(const Value *P); 260 261 /// getAdjustedAnalysisPointer - This method is used when a pass implements 262 /// an analysis interface through multiple inheritance. If needed, it should 263 /// override this to adjust the this pointer as needed for the specified pass 264 /// info. 265 virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) { 266 if (PI->isPassID(&AliasAnalysis::ID)) 267 return (AliasAnalysis*)this; 268 return this; 269 } 270 271 private: 272 // Visited - Track instructions visited by a aliasPHI, aliasSelect(), and aliasGEP(). 273 SmallPtrSet<const Value*, 16> Visited; 274 275 // aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP 276 // instruction against another. 277 AliasResult aliasGEP(const GEPOperator *V1, unsigned V1Size, 278 const Value *V2, unsigned V2Size, 279 const Value *UnderlyingV1, const Value *UnderlyingV2); 280 281 // aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI 282 // instruction against another. 283 AliasResult aliasPHI(const PHINode *PN, unsigned PNSize, 284 const Value *V2, unsigned V2Size); 285 286 /// aliasSelect - Disambiguate a Select instruction against another value. 287 AliasResult aliasSelect(const SelectInst *SI, unsigned SISize, 288 const Value *V2, unsigned V2Size); 289 290 AliasResult aliasCheck(const Value *V1, unsigned V1Size, 291 const Value *V2, unsigned V2Size); 292 }; 293} // End of anonymous namespace 294 295// Register this pass... 296char BasicAliasAnalysis::ID = 0; 297static RegisterPass<BasicAliasAnalysis> 298X("basicaa", "Basic Alias Analysis (default AA impl)", false, true); 299 300// Declare that we implement the AliasAnalysis interface 301static RegisterAnalysisGroup<AliasAnalysis, true> Y(X); 302 303ImmutablePass *llvm::createBasicAliasAnalysisPass() { 304 return new BasicAliasAnalysis(); 305} 306 307 308/// pointsToConstantMemory - Chase pointers until we find a (constant 309/// global) or not. 310bool BasicAliasAnalysis::pointsToConstantMemory(const Value *P) { 311 if (const GlobalVariable *GV = 312 dyn_cast<GlobalVariable>(P->getUnderlyingObject())) 313 // Note: this doesn't require GV to be "ODR" because it isn't legal for a 314 // global to be marked constant in some modules and non-constant in others. 315 // GV may even be a declaration, not a definition. 316 return GV->isConstant(); 317 return false; 318} 319 320 321/// getModRefInfo - Check to see if the specified callsite can clobber the 322/// specified memory object. Since we only look at local properties of this 323/// function, we really can't say much about this query. We do, however, use 324/// simple "address taken" analysis on local objects. 325AliasAnalysis::ModRefResult 326BasicAliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { 327 const Value *Object = P->getUnderlyingObject(); 328 329 // If this is a tail call and P points to a stack location, we know that 330 // the tail call cannot access or modify the local stack. 331 // We cannot exclude byval arguments here; these belong to the caller of 332 // the current function not to the current function, and a tail callee 333 // may reference them. 334 if (isa<AllocaInst>(Object)) 335 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) 336 if (CI->isTailCall()) 337 return NoModRef; 338 339 // If we can identify an object and it's known to be within the 340 // same function as the call, we can ignore interprocedural concerns. 341 bool EffectivelyInterprocedural = 342 Interprocedural && !sameParent(Object, CS.getInstruction()); 343 344 // If the pointer is to a locally allocated object that does not escape, 345 // then the call can not mod/ref the pointer unless the call takes the pointer 346 // as an argument, and itself doesn't capture it. 347 if (!isa<Constant>(Object) && CS.getInstruction() != Object && 348 !EffectivelyInterprocedural && 349 isNonEscapingLocalObject(Object)) { 350 bool PassedAsArg = false; 351 unsigned ArgNo = 0; 352 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); 353 CI != CE; ++CI, ++ArgNo) { 354 // Only look at the no-capture pointer arguments. 355 if (!(*CI)->getType()->isPointerTy() || 356 !CS.paramHasAttr(ArgNo+1, Attribute::NoCapture)) 357 continue; 358 359 // If this is a no-capture pointer argument, see if we can tell that it 360 // is impossible to alias the pointer we're checking. If not, we have to 361 // assume that the call could touch the pointer, even though it doesn't 362 // escape. 363 if (!isNoAlias(cast<Value>(CI), ~0U, P, ~0U)) { 364 PassedAsArg = true; 365 break; 366 } 367 } 368 369 if (!PassedAsArg) 370 return NoModRef; 371 } 372 373 // Finally, handle specific knowledge of intrinsics. 374 IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction()); 375 if (II == 0) 376 return AliasAnalysis::getModRefInfo(CS, P, Size); 377 378 switch (II->getIntrinsicID()) { 379 default: break; 380 case Intrinsic::memcpy: 381 case Intrinsic::memmove: { 382 unsigned Len = ~0U; 383 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) 384 Len = LenCI->getZExtValue(); 385 Value *Dest = II->getArgOperand(0); 386 Value *Src = II->getArgOperand(1); 387 if (isNoAlias(Dest, Len, P, Size)) { 388 if (isNoAlias(Src, Len, P, Size)) 389 return NoModRef; 390 return Ref; 391 } 392 break; 393 } 394 case Intrinsic::memset: 395 // Since memset is 'accesses arguments' only, the AliasAnalysis base class 396 // will handle it for the variable length case. 397 if (ConstantInt *LenCI = dyn_cast<ConstantInt>(II->getArgOperand(2))) { 398 unsigned Len = LenCI->getZExtValue(); 399 Value *Dest = II->getArgOperand(0); 400 if (isNoAlias(Dest, Len, P, Size)) 401 return NoModRef; 402 } 403 break; 404 case Intrinsic::atomic_cmp_swap: 405 case Intrinsic::atomic_swap: 406 case Intrinsic::atomic_load_add: 407 case Intrinsic::atomic_load_sub: 408 case Intrinsic::atomic_load_and: 409 case Intrinsic::atomic_load_nand: 410 case Intrinsic::atomic_load_or: 411 case Intrinsic::atomic_load_xor: 412 case Intrinsic::atomic_load_max: 413 case Intrinsic::atomic_load_min: 414 case Intrinsic::atomic_load_umax: 415 case Intrinsic::atomic_load_umin: 416 if (TD) { 417 Value *Op1 = II->getArgOperand(0); 418 unsigned Op1Size = TD->getTypeStoreSize(Op1->getType()); 419 if (isNoAlias(Op1, Op1Size, P, Size)) 420 return NoModRef; 421 } 422 break; 423 case Intrinsic::lifetime_start: 424 case Intrinsic::lifetime_end: 425 case Intrinsic::invariant_start: { 426 unsigned PtrSize = cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(); 427 if (isNoAlias(II->getArgOperand(1), PtrSize, P, Size)) 428 return NoModRef; 429 break; 430 } 431 case Intrinsic::invariant_end: { 432 unsigned PtrSize = cast<ConstantInt>(II->getArgOperand(1))->getZExtValue(); 433 if (isNoAlias(II->getArgOperand(2), PtrSize, P, Size)) 434 return NoModRef; 435 break; 436 } 437 } 438 439 // The AliasAnalysis base class has some smarts, lets use them. 440 return AliasAnalysis::getModRefInfo(CS, P, Size); 441} 442 443 444AliasAnalysis::ModRefResult 445BasicAliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { 446 // If CS1 or CS2 are readnone, they don't interact. 447 ModRefBehavior CS1B = AliasAnalysis::getModRefBehavior(CS1); 448 if (CS1B == DoesNotAccessMemory) return NoModRef; 449 450 ModRefBehavior CS2B = AliasAnalysis::getModRefBehavior(CS2); 451 if (CS2B == DoesNotAccessMemory) return NoModRef; 452 453 // If they both only read from memory, just return ref. 454 if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory) 455 return Ref; 456 457 // Otherwise, fall back to NoAA (mod+ref). 458 return NoAA::getModRefInfo(CS1, CS2); 459} 460 461/// GetIndiceDifference - Dest and Src are the variable indices from two 462/// decomposed GetElementPtr instructions GEP1 and GEP2 which have common base 463/// pointers. Subtract the GEP2 indices from GEP1 to find the symbolic 464/// difference between the two pointers. 465static void GetIndiceDifference( 466 SmallVectorImpl<std::pair<const Value*, int64_t> > &Dest, 467 const SmallVectorImpl<std::pair<const Value*, int64_t> > &Src) { 468 if (Src.empty()) return; 469 470 for (unsigned i = 0, e = Src.size(); i != e; ++i) { 471 const Value *V = Src[i].first; 472 int64_t Scale = Src[i].second; 473 474 // Find V in Dest. This is N^2, but pointer indices almost never have more 475 // than a few variable indexes. 476 for (unsigned j = 0, e = Dest.size(); j != e; ++j) { 477 if (Dest[j].first != V) continue; 478 479 // If we found it, subtract off Scale V's from the entry in Dest. If it 480 // goes to zero, remove the entry. 481 if (Dest[j].second != Scale) 482 Dest[j].second -= Scale; 483 else 484 Dest.erase(Dest.begin()+j); 485 Scale = 0; 486 break; 487 } 488 489 // If we didn't consume this entry, add it to the end of the Dest list. 490 if (Scale) 491 Dest.push_back(std::make_pair(V, -Scale)); 492 } 493} 494 495/// aliasGEP - Provide a bunch of ad-hoc rules to disambiguate a GEP instruction 496/// against another pointer. We know that V1 is a GEP, but we don't know 497/// anything about V2. UnderlyingV1 is GEP1->getUnderlyingObject(), 498/// UnderlyingV2 is the same for V2. 499/// 500AliasAnalysis::AliasResult 501BasicAliasAnalysis::aliasGEP(const GEPOperator *GEP1, unsigned V1Size, 502 const Value *V2, unsigned V2Size, 503 const Value *UnderlyingV1, 504 const Value *UnderlyingV2) { 505 // If this GEP has been visited before, we're on a use-def cycle. 506 // Such cycles are only valid when PHI nodes are involved or in unreachable 507 // code. The visitPHI function catches cycles containing PHIs, but there 508 // could still be a cycle without PHIs in unreachable code. 509 if (!Visited.insert(GEP1)) 510 return MayAlias; 511 512 int64_t GEP1BaseOffset; 513 SmallVector<std::pair<const Value*, int64_t>, 4> GEP1VariableIndices; 514 515 // If we have two gep instructions with must-alias'ing base pointers, figure 516 // out if the indexes to the GEP tell us anything about the derived pointer. 517 if (const GEPOperator *GEP2 = dyn_cast<GEPOperator>(V2)) { 518 // Do the base pointers alias? 519 AliasResult BaseAlias = aliasCheck(UnderlyingV1, ~0U, UnderlyingV2, ~0U); 520 521 // If we get a No or May, then return it immediately, no amount of analysis 522 // will improve this situation. 523 if (BaseAlias != MustAlias) return BaseAlias; 524 525 // Otherwise, we have a MustAlias. Since the base pointers alias each other 526 // exactly, see if the computed offset from the common pointer tells us 527 // about the relation of the resulting pointer. 528 const Value *GEP1BasePtr = 529 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD); 530 531 int64_t GEP2BaseOffset; 532 SmallVector<std::pair<const Value*, int64_t>, 4> GEP2VariableIndices; 533 const Value *GEP2BasePtr = 534 DecomposeGEPExpression(GEP2, GEP2BaseOffset, GEP2VariableIndices, TD); 535 536 // If DecomposeGEPExpression isn't able to look all the way through the 537 // addressing operation, we must not have TD and this is too complex for us 538 // to handle without it. 539 if (GEP1BasePtr != UnderlyingV1 || GEP2BasePtr != UnderlyingV2) { 540 assert(TD == 0 && 541 "DecomposeGEPExpression and getUnderlyingObject disagree!"); 542 return MayAlias; 543 } 544 545 // Subtract the GEP2 pointer from the GEP1 pointer to find out their 546 // symbolic difference. 547 GEP1BaseOffset -= GEP2BaseOffset; 548 GetIndiceDifference(GEP1VariableIndices, GEP2VariableIndices); 549 550 } else { 551 // Check to see if these two pointers are related by the getelementptr 552 // instruction. If one pointer is a GEP with a non-zero index of the other 553 // pointer, we know they cannot alias. 554 555 // If both accesses are unknown size, we can't do anything useful here. 556 if (V1Size == ~0U && V2Size == ~0U) 557 return MayAlias; 558 559 AliasResult R = aliasCheck(UnderlyingV1, ~0U, V2, V2Size); 560 if (R != MustAlias) 561 // If V2 may alias GEP base pointer, conservatively returns MayAlias. 562 // If V2 is known not to alias GEP base pointer, then the two values 563 // cannot alias per GEP semantics: "A pointer value formed from a 564 // getelementptr instruction is associated with the addresses associated 565 // with the first operand of the getelementptr". 566 return R; 567 568 const Value *GEP1BasePtr = 569 DecomposeGEPExpression(GEP1, GEP1BaseOffset, GEP1VariableIndices, TD); 570 571 // If DecomposeGEPExpression isn't able to look all the way through the 572 // addressing operation, we must not have TD and this is too complex for us 573 // to handle without it. 574 if (GEP1BasePtr != UnderlyingV1) { 575 assert(TD == 0 && 576 "DecomposeGEPExpression and getUnderlyingObject disagree!"); 577 return MayAlias; 578 } 579 } 580 581 // In the two GEP Case, if there is no difference in the offsets of the 582 // computed pointers, the resultant pointers are a must alias. This 583 // hapens when we have two lexically identical GEP's (for example). 584 // 585 // In the other case, if we have getelementptr <ptr>, 0, 0, 0, 0, ... and V2 586 // must aliases the GEP, the end result is a must alias also. 587 if (GEP1BaseOffset == 0 && GEP1VariableIndices.empty()) 588 return MustAlias; 589 590 // If we have a known constant offset, see if this offset is larger than the 591 // access size being queried. If so, and if no variable indices can remove 592 // pieces of this constant, then we know we have a no-alias. For example, 593 // &A[100] != &A. 594 595 // In order to handle cases like &A[100][i] where i is an out of range 596 // subscript, we have to ignore all constant offset pieces that are a multiple 597 // of a scaled index. Do this by removing constant offsets that are a 598 // multiple of any of our variable indices. This allows us to transform 599 // things like &A[i][1] because i has a stride of (e.g.) 8 bytes but the 1 600 // provides an offset of 4 bytes (assuming a <= 4 byte access). 601 for (unsigned i = 0, e = GEP1VariableIndices.size(); 602 i != e && GEP1BaseOffset;++i) 603 if (int64_t RemovedOffset = GEP1BaseOffset/GEP1VariableIndices[i].second) 604 GEP1BaseOffset -= RemovedOffset*GEP1VariableIndices[i].second; 605 606 // If our known offset is bigger than the access size, we know we don't have 607 // an alias. 608 if (GEP1BaseOffset) { 609 if (GEP1BaseOffset >= (int64_t)V2Size || 610 GEP1BaseOffset <= -(int64_t)V1Size) 611 return NoAlias; 612 } 613 614 return MayAlias; 615} 616 617/// aliasSelect - Provide a bunch of ad-hoc rules to disambiguate a Select 618/// instruction against another. 619AliasAnalysis::AliasResult 620BasicAliasAnalysis::aliasSelect(const SelectInst *SI, unsigned SISize, 621 const Value *V2, unsigned V2Size) { 622 // If this select has been visited before, we're on a use-def cycle. 623 // Such cycles are only valid when PHI nodes are involved or in unreachable 624 // code. The visitPHI function catches cycles containing PHIs, but there 625 // could still be a cycle without PHIs in unreachable code. 626 if (!Visited.insert(SI)) 627 return MayAlias; 628 629 // If the values are Selects with the same condition, we can do a more precise 630 // check: just check for aliases between the values on corresponding arms. 631 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) 632 if (SI->getCondition() == SI2->getCondition()) { 633 AliasResult Alias = 634 aliasCheck(SI->getTrueValue(), SISize, 635 SI2->getTrueValue(), V2Size); 636 if (Alias == MayAlias) 637 return MayAlias; 638 AliasResult ThisAlias = 639 aliasCheck(SI->getFalseValue(), SISize, 640 SI2->getFalseValue(), V2Size); 641 if (ThisAlias != Alias) 642 return MayAlias; 643 return Alias; 644 } 645 646 // If both arms of the Select node NoAlias or MustAlias V2, then returns 647 // NoAlias / MustAlias. Otherwise, returns MayAlias. 648 AliasResult Alias = 649 aliasCheck(V2, V2Size, SI->getTrueValue(), SISize); 650 if (Alias == MayAlias) 651 return MayAlias; 652 653 // If V2 is visited, the recursive case will have been caught in the 654 // above aliasCheck call, so these subsequent calls to aliasCheck 655 // don't need to assume that V2 is being visited recursively. 656 Visited.erase(V2); 657 658 AliasResult ThisAlias = 659 aliasCheck(V2, V2Size, SI->getFalseValue(), SISize); 660 if (ThisAlias != Alias) 661 return MayAlias; 662 return Alias; 663} 664 665// aliasPHI - Provide a bunch of ad-hoc rules to disambiguate a PHI instruction 666// against another. 667AliasAnalysis::AliasResult 668BasicAliasAnalysis::aliasPHI(const PHINode *PN, unsigned PNSize, 669 const Value *V2, unsigned V2Size) { 670 // The PHI node has already been visited, avoid recursion any further. 671 if (!Visited.insert(PN)) 672 return MayAlias; 673 674 // If the values are PHIs in the same block, we can do a more precise 675 // as well as efficient check: just check for aliases between the values 676 // on corresponding edges. 677 if (const PHINode *PN2 = dyn_cast<PHINode>(V2)) 678 if (PN2->getParent() == PN->getParent()) { 679 AliasResult Alias = 680 aliasCheck(PN->getIncomingValue(0), PNSize, 681 PN2->getIncomingValueForBlock(PN->getIncomingBlock(0)), 682 V2Size); 683 if (Alias == MayAlias) 684 return MayAlias; 685 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { 686 AliasResult ThisAlias = 687 aliasCheck(PN->getIncomingValue(i), PNSize, 688 PN2->getIncomingValueForBlock(PN->getIncomingBlock(i)), 689 V2Size); 690 if (ThisAlias != Alias) 691 return MayAlias; 692 } 693 return Alias; 694 } 695 696 SmallPtrSet<Value*, 4> UniqueSrc; 697 SmallVector<Value*, 4> V1Srcs; 698 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 699 Value *PV1 = PN->getIncomingValue(i); 700 if (isa<PHINode>(PV1)) 701 // If any of the source itself is a PHI, return MayAlias conservatively 702 // to avoid compile time explosion. The worst possible case is if both 703 // sides are PHI nodes. In which case, this is O(m x n) time where 'm' 704 // and 'n' are the number of PHI sources. 705 return MayAlias; 706 if (UniqueSrc.insert(PV1)) 707 V1Srcs.push_back(PV1); 708 } 709 710 AliasResult Alias = aliasCheck(V2, V2Size, V1Srcs[0], PNSize); 711 // Early exit if the check of the first PHI source against V2 is MayAlias. 712 // Other results are not possible. 713 if (Alias == MayAlias) 714 return MayAlias; 715 716 // If all sources of the PHI node NoAlias or MustAlias V2, then returns 717 // NoAlias / MustAlias. Otherwise, returns MayAlias. 718 for (unsigned i = 1, e = V1Srcs.size(); i != e; ++i) { 719 Value *V = V1Srcs[i]; 720 721 // If V2 is visited, the recursive case will have been caught in the 722 // above aliasCheck call, so these subsequent calls to aliasCheck 723 // don't need to assume that V2 is being visited recursively. 724 Visited.erase(V2); 725 726 AliasResult ThisAlias = aliasCheck(V2, V2Size, V, PNSize); 727 if (ThisAlias != Alias || ThisAlias == MayAlias) 728 return MayAlias; 729 } 730 731 return Alias; 732} 733 734// aliasCheck - Provide a bunch of ad-hoc rules to disambiguate in common cases, 735// such as array references. 736// 737AliasAnalysis::AliasResult 738BasicAliasAnalysis::aliasCheck(const Value *V1, unsigned V1Size, 739 const Value *V2, unsigned V2Size) { 740 // If either of the memory references is empty, it doesn't matter what the 741 // pointer values are. 742 if (V1Size == 0 || V2Size == 0) 743 return NoAlias; 744 745 // Strip off any casts if they exist. 746 V1 = V1->stripPointerCasts(); 747 V2 = V2->stripPointerCasts(); 748 749 // Are we checking for alias of the same value? 750 if (V1 == V2) return MustAlias; 751 752 if (!V1->getType()->isPointerTy() || !V2->getType()->isPointerTy()) 753 return NoAlias; // Scalars cannot alias each other 754 755 // Figure out what objects these things are pointing to if we can. 756 const Value *O1 = V1->getUnderlyingObject(); 757 const Value *O2 = V2->getUnderlyingObject(); 758 759 // Null values in the default address space don't point to any object, so they 760 // don't alias any other pointer. 761 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O1)) 762 if (CPN->getType()->getAddressSpace() == 0) 763 return NoAlias; 764 if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(O2)) 765 if (CPN->getType()->getAddressSpace() == 0) 766 return NoAlias; 767 768 // If we can identify two objects and they're known to be within the 769 // same function, we can ignore interprocedural concerns. 770 bool EffectivelyInterprocedural = 771 Interprocedural && !sameParent(O1, O2); 772 773 if (O1 != O2) { 774 // If V1/V2 point to two different objects we know that we have no alias. 775 if (isIdentifiedObject(O1, EffectivelyInterprocedural) && 776 isIdentifiedObject(O2, EffectivelyInterprocedural)) 777 return NoAlias; 778 779 // Constant pointers can't alias with non-const isIdentifiedObject objects. 780 if ((isa<Constant>(O1) && 781 isIdentifiedObject(O2, EffectivelyInterprocedural) && 782 !isa<Constant>(O2)) || 783 (isa<Constant>(O2) && 784 isIdentifiedObject(O1, EffectivelyInterprocedural) && 785 !isa<Constant>(O1))) 786 return NoAlias; 787 788 // Arguments can't alias with local allocations or noalias calls 789 // in the same function. 790 if (!EffectivelyInterprocedural && 791 ((isa<Argument>(O1) && (isa<AllocaInst>(O2) || isNoAliasCall(O2))) || 792 (isa<Argument>(O2) && (isa<AllocaInst>(O1) || isNoAliasCall(O1))))) 793 return NoAlias; 794 795 // Most objects can't alias null. 796 if ((isa<ConstantPointerNull>(V2) && isKnownNonNull(O1)) || 797 (isa<ConstantPointerNull>(V1) && isKnownNonNull(O2))) 798 return NoAlias; 799 } 800 801 // If the size of one access is larger than the entire object on the other 802 // side, then we know such behavior is undefined and can assume no alias. 803 if (TD) 804 if ((V1Size != ~0U && isObjectSmallerThan(O2, V1Size, *TD)) || 805 (V2Size != ~0U && isObjectSmallerThan(O1, V2Size, *TD))) 806 return NoAlias; 807 808 // If one pointer is the result of a call/invoke or load and the other is a 809 // non-escaping local object within the same function, then we know the 810 // object couldn't escape to a point where the call could return it. 811 // 812 // Note that if the pointers are in different functions, there are a 813 // variety of complications. A call with a nocapture argument may still 814 // temporary store the nocapture argument's value in a temporary memory 815 // location if that memory location doesn't escape. Or it may pass a 816 // nocapture value to other functions as long as they don't capture it. 817 if (O1 != O2 && !EffectivelyInterprocedural) { 818 if (isEscapeSource(O1) && isNonEscapingLocalObject(O2)) 819 return NoAlias; 820 if (isEscapeSource(O2) && isNonEscapingLocalObject(O1)) 821 return NoAlias; 822 } 823 824 // FIXME: This isn't aggressively handling alias(GEP, PHI) for example: if the 825 // GEP can't simplify, we don't even look at the PHI cases. 826 if (!isa<GEPOperator>(V1) && isa<GEPOperator>(V2)) { 827 std::swap(V1, V2); 828 std::swap(V1Size, V2Size); 829 std::swap(O1, O2); 830 } 831 if (const GEPOperator *GV1 = dyn_cast<GEPOperator>(V1)) 832 return aliasGEP(GV1, V1Size, V2, V2Size, O1, O2); 833 834 if (isa<PHINode>(V2) && !isa<PHINode>(V1)) { 835 std::swap(V1, V2); 836 std::swap(V1Size, V2Size); 837 } 838 if (const PHINode *PN = dyn_cast<PHINode>(V1)) 839 return aliasPHI(PN, V1Size, V2, V2Size); 840 841 if (isa<SelectInst>(V2) && !isa<SelectInst>(V1)) { 842 std::swap(V1, V2); 843 std::swap(V1Size, V2Size); 844 } 845 if (const SelectInst *S1 = dyn_cast<SelectInst>(V1)) 846 return aliasSelect(S1, V1Size, V2, V2Size); 847 848 return MayAlias; 849} 850 851// Make sure that anything that uses AliasAnalysis pulls in this file. 852DEFINING_FILE_FOR(BasicAliasAnalysis) 853 854//===----------------------------------------------------------------------===// 855// InterproceduralBasicAliasAnalysis Pass 856//===----------------------------------------------------------------------===// 857 858namespace { 859 /// InterproceduralBasicAliasAnalysis - This is similar to basicaa, except 860 /// that it properly supports queries to values which live in different 861 /// functions. 862 /// 863 /// Note that we don't currently take this to the extreme, analyzing all 864 /// call sites of a function to answer a query about an Argument. 865 /// 866 struct InterproceduralBasicAliasAnalysis : public BasicAliasAnalysis { 867 static char ID; // Class identification, replacement for typeinfo 868 InterproceduralBasicAliasAnalysis() : BasicAliasAnalysis(&ID, true) {} 869 }; 870} 871 872// Register this pass... 873char InterproceduralBasicAliasAnalysis::ID = 0; 874static RegisterPass<InterproceduralBasicAliasAnalysis> 875W("interprocedural-basic-aa", "Interprocedural Basic Alias Analysis", false, true); 876 877// Declare that we implement the AliasAnalysis interface 878static RegisterAnalysisGroup<AliasAnalysis> Z(W); 879 880ImmutablePass *llvm::createInterproceduralBasicAliasAnalysisPass() { 881 return new InterproceduralBasicAliasAnalysis(); 882} 883