LLVM API Documentation
00001 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the generic AliasAnalysis interface which is used as the 00011 // common interface used by all clients and implementations of alias analysis. 00012 // 00013 // This file also implements the default version of the AliasAnalysis interface 00014 // that is to be used when no other implementation is specified. This does some 00015 // simple tests that detect obvious cases: two different global pointers cannot 00016 // alias, a global cannot alias a malloc, two different mallocs cannot alias, 00017 // etc. 00018 // 00019 // This alias analysis implementation really isn't very good for anything, but 00020 // it is very fast, and makes a nice clean default implementation. Because it 00021 // handles lots of little corner cases, other, more complex, alias analysis 00022 // implementations may choose to rely on this pass to resolve these simple and 00023 // easy cases. 00024 // 00025 //===----------------------------------------------------------------------===// 00026 00027 #include "llvm/Analysis/AliasAnalysis.h" 00028 #include "llvm/Analysis/CFG.h" 00029 #include "llvm/Analysis/CaptureTracking.h" 00030 #include "llvm/Analysis/ValueTracking.h" 00031 #include "llvm/IR/BasicBlock.h" 00032 #include "llvm/IR/DataLayout.h" 00033 #include "llvm/IR/Dominators.h" 00034 #include "llvm/IR/Function.h" 00035 #include "llvm/IR/Instructions.h" 00036 #include "llvm/IR/IntrinsicInst.h" 00037 #include "llvm/IR/LLVMContext.h" 00038 #include "llvm/IR/Type.h" 00039 #include "llvm/Pass.h" 00040 #include "llvm/Target/TargetLibraryInfo.h" 00041 using namespace llvm; 00042 00043 // Register the AliasAnalysis interface, providing a nice name to refer to. 00044 INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA) 00045 char AliasAnalysis::ID = 0; 00046 00047 //===----------------------------------------------------------------------===// 00048 // Default chaining methods 00049 //===----------------------------------------------------------------------===// 00050 00051 AliasAnalysis::AliasResult 00052 AliasAnalysis::alias(const Location &LocA, const Location &LocB) { 00053 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00054 return AA->alias(LocA, LocB); 00055 } 00056 00057 bool AliasAnalysis::pointsToConstantMemory(const Location &Loc, 00058 bool OrLocal) { 00059 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00060 return AA->pointsToConstantMemory(Loc, OrLocal); 00061 } 00062 00063 AliasAnalysis::Location 00064 AliasAnalysis::getArgLocation(ImmutableCallSite CS, unsigned ArgIdx, 00065 AliasAnalysis::ModRefResult &Mask) { 00066 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00067 return AA->getArgLocation(CS, ArgIdx, Mask); 00068 } 00069 00070 void AliasAnalysis::deleteValue(Value *V) { 00071 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00072 AA->deleteValue(V); 00073 } 00074 00075 void AliasAnalysis::copyValue(Value *From, Value *To) { 00076 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00077 AA->copyValue(From, To); 00078 } 00079 00080 void AliasAnalysis::addEscapingUse(Use &U) { 00081 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00082 AA->addEscapingUse(U); 00083 } 00084 00085 00086 AliasAnalysis::ModRefResult 00087 AliasAnalysis::getModRefInfo(ImmutableCallSite CS, 00088 const Location &Loc) { 00089 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00090 00091 ModRefBehavior MRB = getModRefBehavior(CS); 00092 if (MRB == DoesNotAccessMemory) 00093 return NoModRef; 00094 00095 ModRefResult Mask = ModRef; 00096 if (onlyReadsMemory(MRB)) 00097 Mask = Ref; 00098 00099 if (onlyAccessesArgPointees(MRB)) { 00100 bool doesAlias = false; 00101 ModRefResult AllArgsMask = NoModRef; 00102 if (doesAccessArgPointees(MRB)) { 00103 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); 00104 AI != AE; ++AI) { 00105 const Value *Arg = *AI; 00106 if (!Arg->getType()->isPointerTy()) 00107 continue; 00108 ModRefResult ArgMask; 00109 Location CSLoc = 00110 getArgLocation(CS, (unsigned) std::distance(CS.arg_begin(), AI), 00111 ArgMask); 00112 if (!isNoAlias(CSLoc, Loc)) { 00113 doesAlias = true; 00114 AllArgsMask = ModRefResult(AllArgsMask | ArgMask); 00115 } 00116 } 00117 } 00118 if (!doesAlias) 00119 return NoModRef; 00120 Mask = ModRefResult(Mask & AllArgsMask); 00121 } 00122 00123 // If Loc is a constant memory location, the call definitely could not 00124 // modify the memory location. 00125 if ((Mask & Mod) && pointsToConstantMemory(Loc)) 00126 Mask = ModRefResult(Mask & ~Mod); 00127 00128 // If this is the end of the chain, don't forward. 00129 if (!AA) return Mask; 00130 00131 // Otherwise, fall back to the next AA in the chain. But we can merge 00132 // in any mask we've managed to compute. 00133 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask); 00134 } 00135 00136 AliasAnalysis::ModRefResult 00137 AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) { 00138 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00139 00140 // If CS1 or CS2 are readnone, they don't interact. 00141 ModRefBehavior CS1B = getModRefBehavior(CS1); 00142 if (CS1B == DoesNotAccessMemory) return NoModRef; 00143 00144 ModRefBehavior CS2B = getModRefBehavior(CS2); 00145 if (CS2B == DoesNotAccessMemory) return NoModRef; 00146 00147 // If they both only read from memory, there is no dependence. 00148 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B)) 00149 return NoModRef; 00150 00151 AliasAnalysis::ModRefResult Mask = ModRef; 00152 00153 // If CS1 only reads memory, the only dependence on CS2 can be 00154 // from CS1 reading memory written by CS2. 00155 if (onlyReadsMemory(CS1B)) 00156 Mask = ModRefResult(Mask & Ref); 00157 00158 // If CS2 only access memory through arguments, accumulate the mod/ref 00159 // information from CS1's references to the memory referenced by 00160 // CS2's arguments. 00161 if (onlyAccessesArgPointees(CS2B)) { 00162 AliasAnalysis::ModRefResult R = NoModRef; 00163 if (doesAccessArgPointees(CS2B)) { 00164 for (ImmutableCallSite::arg_iterator 00165 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) { 00166 const Value *Arg = *I; 00167 if (!Arg->getType()->isPointerTy()) 00168 continue; 00169 ModRefResult ArgMask; 00170 Location CS2Loc = 00171 getArgLocation(CS2, (unsigned) std::distance(CS2.arg_begin(), I), 00172 ArgMask); 00173 // ArgMask indicates what CS2 might do to CS2Loc, and the dependence of 00174 // CS1 on that location is the inverse. 00175 if (ArgMask == Mod) 00176 ArgMask = ModRef; 00177 else if (ArgMask == Ref) 00178 ArgMask = Mod; 00179 00180 R = ModRefResult((R | (getModRefInfo(CS1, CS2Loc) & ArgMask)) & Mask); 00181 if (R == Mask) 00182 break; 00183 } 00184 } 00185 return R; 00186 } 00187 00188 // If CS1 only accesses memory through arguments, check if CS2 references 00189 // any of the memory referenced by CS1's arguments. If not, return NoModRef. 00190 if (onlyAccessesArgPointees(CS1B)) { 00191 AliasAnalysis::ModRefResult R = NoModRef; 00192 if (doesAccessArgPointees(CS1B)) { 00193 for (ImmutableCallSite::arg_iterator 00194 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) { 00195 const Value *Arg = *I; 00196 if (!Arg->getType()->isPointerTy()) 00197 continue; 00198 ModRefResult ArgMask; 00199 Location CS1Loc = 00200 getArgLocation(CS1, (unsigned) std::distance(CS1.arg_begin(), I), 00201 ArgMask); 00202 // ArgMask indicates what CS1 might do to CS1Loc; if CS1 might Mod 00203 // CS1Loc, then we care about either a Mod or a Ref by CS2. If CS1 00204 // might Ref, then we care only about a Mod by CS2. 00205 ModRefResult ArgR = getModRefInfo(CS2, CS1Loc); 00206 if (((ArgMask & Mod) != NoModRef && (ArgR & ModRef) != NoModRef) || 00207 ((ArgMask & Ref) != NoModRef && (ArgR & Mod) != NoModRef)) 00208 R = ModRefResult((R | ArgMask) & Mask); 00209 00210 if (R == Mask) 00211 break; 00212 } 00213 } 00214 return R; 00215 } 00216 00217 // If this is the end of the chain, don't forward. 00218 if (!AA) return Mask; 00219 00220 // Otherwise, fall back to the next AA in the chain. But we can merge 00221 // in any mask we've managed to compute. 00222 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask); 00223 } 00224 00225 AliasAnalysis::ModRefBehavior 00226 AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) { 00227 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00228 00229 ModRefBehavior Min = UnknownModRefBehavior; 00230 00231 // Call back into the alias analysis with the other form of getModRefBehavior 00232 // to see if it can give a better response. 00233 if (const Function *F = CS.getCalledFunction()) 00234 Min = getModRefBehavior(F); 00235 00236 // If this is the end of the chain, don't forward. 00237 if (!AA) return Min; 00238 00239 // Otherwise, fall back to the next AA in the chain. But we can merge 00240 // in any result we've managed to compute. 00241 return ModRefBehavior(AA->getModRefBehavior(CS) & Min); 00242 } 00243 00244 AliasAnalysis::ModRefBehavior 00245 AliasAnalysis::getModRefBehavior(const Function *F) { 00246 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00247 return AA->getModRefBehavior(F); 00248 } 00249 00250 //===----------------------------------------------------------------------===// 00251 // AliasAnalysis non-virtual helper method implementation 00252 //===----------------------------------------------------------------------===// 00253 00254 AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) { 00255 AAMDNodes AATags; 00256 LI->getAAMetadata(AATags); 00257 00258 return Location(LI->getPointerOperand(), 00259 getTypeStoreSize(LI->getType()), AATags); 00260 } 00261 00262 AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) { 00263 AAMDNodes AATags; 00264 SI->getAAMetadata(AATags); 00265 00266 return Location(SI->getPointerOperand(), 00267 getTypeStoreSize(SI->getValueOperand()->getType()), AATags); 00268 } 00269 00270 AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) { 00271 AAMDNodes AATags; 00272 VI->getAAMetadata(AATags); 00273 00274 return Location(VI->getPointerOperand(), UnknownSize, AATags); 00275 } 00276 00277 AliasAnalysis::Location 00278 AliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) { 00279 AAMDNodes AATags; 00280 CXI->getAAMetadata(AATags); 00281 00282 return Location(CXI->getPointerOperand(), 00283 getTypeStoreSize(CXI->getCompareOperand()->getType()), 00284 AATags); 00285 } 00286 00287 AliasAnalysis::Location 00288 AliasAnalysis::getLocation(const AtomicRMWInst *RMWI) { 00289 AAMDNodes AATags; 00290 RMWI->getAAMetadata(AATags); 00291 00292 return Location(RMWI->getPointerOperand(), 00293 getTypeStoreSize(RMWI->getValOperand()->getType()), AATags); 00294 } 00295 00296 AliasAnalysis::Location 00297 AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) { 00298 uint64_t Size = UnknownSize; 00299 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) 00300 Size = C->getValue().getZExtValue(); 00301 00302 // memcpy/memmove can have AA tags. For memcpy, they apply 00303 // to both the source and the destination. 00304 AAMDNodes AATags; 00305 MTI->getAAMetadata(AATags); 00306 00307 return Location(MTI->getRawSource(), Size, AATags); 00308 } 00309 00310 AliasAnalysis::Location 00311 AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) { 00312 uint64_t Size = UnknownSize; 00313 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength())) 00314 Size = C->getValue().getZExtValue(); 00315 00316 // memcpy/memmove can have AA tags. For memcpy, they apply 00317 // to both the source and the destination. 00318 AAMDNodes AATags; 00319 MTI->getMetadata(AATags); 00320 00321 return Location(MTI->getRawDest(), Size, AATags); 00322 } 00323 00324 00325 00326 AliasAnalysis::ModRefResult 00327 AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) { 00328 // Be conservative in the face of volatile/atomic. 00329 if (!L->isUnordered()) 00330 return ModRef; 00331 00332 // If the load address doesn't alias the given address, it doesn't read 00333 // or write the specified memory. 00334 if (!alias(getLocation(L), Loc)) 00335 return NoModRef; 00336 00337 // Otherwise, a load just reads. 00338 return Ref; 00339 } 00340 00341 AliasAnalysis::ModRefResult 00342 AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) { 00343 // Be conservative in the face of volatile/atomic. 00344 if (!S->isUnordered()) 00345 return ModRef; 00346 00347 // If the store address cannot alias the pointer in question, then the 00348 // specified memory cannot be modified by the store. 00349 if (!alias(getLocation(S), Loc)) 00350 return NoModRef; 00351 00352 // If the pointer is a pointer to constant memory, then it could not have been 00353 // modified by this store. 00354 if (pointsToConstantMemory(Loc)) 00355 return NoModRef; 00356 00357 // Otherwise, a store just writes. 00358 return Mod; 00359 } 00360 00361 AliasAnalysis::ModRefResult 00362 AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) { 00363 // If the va_arg address cannot alias the pointer in question, then the 00364 // specified memory cannot be accessed by the va_arg. 00365 if (!alias(getLocation(V), Loc)) 00366 return NoModRef; 00367 00368 // If the pointer is a pointer to constant memory, then it could not have been 00369 // modified by this va_arg. 00370 if (pointsToConstantMemory(Loc)) 00371 return NoModRef; 00372 00373 // Otherwise, a va_arg reads and writes. 00374 return ModRef; 00375 } 00376 00377 AliasAnalysis::ModRefResult 00378 AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) { 00379 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses. 00380 if (CX->getSuccessOrdering() > Monotonic) 00381 return ModRef; 00382 00383 // If the cmpxchg address does not alias the location, it does not access it. 00384 if (!alias(getLocation(CX), Loc)) 00385 return NoModRef; 00386 00387 return ModRef; 00388 } 00389 00390 AliasAnalysis::ModRefResult 00391 AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) { 00392 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses. 00393 if (RMW->getOrdering() > Monotonic) 00394 return ModRef; 00395 00396 // If the atomicrmw address does not alias the location, it does not access it. 00397 if (!alias(getLocation(RMW), Loc)) 00398 return NoModRef; 00399 00400 return ModRef; 00401 } 00402 00403 // FIXME: this is really just shoring-up a deficiency in alias analysis. 00404 // BasicAA isn't willing to spend linear time determining whether an alloca 00405 // was captured before or after this particular call, while we are. However, 00406 // with a smarter AA in place, this test is just wasting compile time. 00407 AliasAnalysis::ModRefResult 00408 AliasAnalysis::callCapturesBefore(const Instruction *I, 00409 const AliasAnalysis::Location &MemLoc, 00410 DominatorTree *DT) { 00411 if (!DT || !DL) return AliasAnalysis::ModRef; 00412 00413 const Value *Object = GetUnderlyingObject(MemLoc.Ptr, DL); 00414 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) || 00415 isa<Constant>(Object)) 00416 return AliasAnalysis::ModRef; 00417 00418 ImmutableCallSite CS(I); 00419 if (!CS.getInstruction() || CS.getInstruction() == Object) 00420 return AliasAnalysis::ModRef; 00421 00422 if (llvm::PointerMayBeCapturedBefore(Object, /* ReturnCaptures */ true, 00423 /* StoreCaptures */ true, I, DT, 00424 /* include Object */ true)) 00425 return AliasAnalysis::ModRef; 00426 00427 unsigned ArgNo = 0; 00428 AliasAnalysis::ModRefResult R = AliasAnalysis::NoModRef; 00429 for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); 00430 CI != CE; ++CI, ++ArgNo) { 00431 // Only look at the no-capture or byval pointer arguments. If this 00432 // pointer were passed to arguments that were neither of these, then it 00433 // couldn't be no-capture. 00434 if (!(*CI)->getType()->isPointerTy() || 00435 (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo))) 00436 continue; 00437 00438 // If this is a no-capture pointer argument, see if we can tell that it 00439 // is impossible to alias the pointer we're checking. If not, we have to 00440 // assume that the call could touch the pointer, even though it doesn't 00441 // escape. 00442 if (isNoAlias(AliasAnalysis::Location(*CI), 00443 AliasAnalysis::Location(Object))) 00444 continue; 00445 if (CS.doesNotAccessMemory(ArgNo)) 00446 continue; 00447 if (CS.onlyReadsMemory(ArgNo)) { 00448 R = AliasAnalysis::Ref; 00449 continue; 00450 } 00451 return AliasAnalysis::ModRef; 00452 } 00453 return R; 00454 } 00455 00456 // AliasAnalysis destructor: DO NOT move this to the header file for 00457 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on 00458 // the AliasAnalysis.o file in the current .a file, causing alias analysis 00459 // support to not be included in the tool correctly! 00460 // 00461 AliasAnalysis::~AliasAnalysis() {} 00462 00463 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the 00464 /// AliasAnalysis interface before any other methods are called. 00465 /// 00466 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { 00467 DataLayoutPass *DLP = P->getAnalysisIfAvailable<DataLayoutPass>(); 00468 DL = DLP ? &DLP->getDataLayout() : nullptr; 00469 TLI = P->getAnalysisIfAvailable<TargetLibraryInfo>(); 00470 AA = &P->getAnalysis<AliasAnalysis>(); 00471 } 00472 00473 // getAnalysisUsage - All alias analysis implementations should invoke this 00474 // directly (using AliasAnalysis::getAnalysisUsage(AU)). 00475 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 00476 AU.addRequired<AliasAnalysis>(); // All AA's chain 00477 } 00478 00479 /// getTypeStoreSize - Return the DataLayout store size for the given type, 00480 /// if known, or a conservative value otherwise. 00481 /// 00482 uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) { 00483 return DL ? DL->getTypeStoreSize(Ty) : UnknownSize; 00484 } 00485 00486 /// canBasicBlockModify - Return true if it is possible for execution of the 00487 /// specified basic block to modify the value pointed to by Ptr. 00488 /// 00489 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, 00490 const Location &Loc) { 00491 return canInstructionRangeModify(BB.front(), BB.back(), Loc); 00492 } 00493 00494 /// canInstructionRangeModify - Return true if it is possible for the execution 00495 /// of the specified instructions to modify the value pointed to by Ptr. The 00496 /// instructions to consider are all of the instructions in the range of [I1,I2] 00497 /// INCLUSIVE. I1 and I2 must be in the same basic block. 00498 /// 00499 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, 00500 const Instruction &I2, 00501 const Location &Loc) { 00502 assert(I1.getParent() == I2.getParent() && 00503 "Instructions not in same basic block!"); 00504 BasicBlock::const_iterator I = &I1; 00505 BasicBlock::const_iterator E = &I2; 00506 ++E; // Convert from inclusive to exclusive range. 00507 00508 for (; I != E; ++I) // Check every instruction in range 00509 if (getModRefInfo(I, Loc) & Mod) 00510 return true; 00511 return false; 00512 } 00513 00514 /// isNoAliasCall - Return true if this pointer is returned by a noalias 00515 /// function. 00516 bool llvm::isNoAliasCall(const Value *V) { 00517 if (isa<CallInst>(V) || isa<InvokeInst>(V)) 00518 return ImmutableCallSite(cast<Instruction>(V)) 00519 .paramHasAttr(0, Attribute::NoAlias); 00520 return false; 00521 } 00522 00523 /// isNoAliasArgument - Return true if this is an argument with the noalias 00524 /// attribute. 00525 bool llvm::isNoAliasArgument(const Value *V) 00526 { 00527 if (const Argument *A = dyn_cast<Argument>(V)) 00528 return A->hasNoAliasAttr(); 00529 return false; 00530 } 00531 00532 /// isIdentifiedObject - Return true if this pointer refers to a distinct and 00533 /// identifiable object. This returns true for: 00534 /// Global Variables and Functions (but not Global Aliases) 00535 /// Allocas and Mallocs 00536 /// ByVal and NoAlias Arguments 00537 /// NoAlias returns 00538 /// 00539 bool llvm::isIdentifiedObject(const Value *V) { 00540 if (isa<AllocaInst>(V)) 00541 return true; 00542 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V)) 00543 return true; 00544 if (isNoAliasCall(V)) 00545 return true; 00546 if (const Argument *A = dyn_cast<Argument>(V)) 00547 return A->hasNoAliasAttr() || A->hasByValAttr(); 00548 return false; 00549 } 00550 00551 /// isIdentifiedFunctionLocal - Return true if V is umabigously identified 00552 /// at the function-level. Different IdentifiedFunctionLocals can't alias. 00553 /// Further, an IdentifiedFunctionLocal can not alias with any function 00554 /// arguments other than itself, which is not necessarily true for 00555 /// IdentifiedObjects. 00556 bool llvm::isIdentifiedFunctionLocal(const Value *V) 00557 { 00558 return isa<AllocaInst>(V) || isNoAliasCall(V) || isNoAliasArgument(V); 00559 } 00560