LLVM API Documentation
00001 //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===// 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 an analysis that determines, for a given memory 00011 // operation, what preceding memory operations it depends on. It builds on 00012 // alias analysis information, and tries to provide a lazy, caching interface to 00013 // a common kind of alias information query. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #define DEBUG_TYPE "memdep" 00018 #include "llvm/Analysis/MemoryDependenceAnalysis.h" 00019 #include "llvm/Instructions.h" 00020 #include "llvm/IntrinsicInst.h" 00021 #include "llvm/Function.h" 00022 #include "llvm/Analysis/AliasAnalysis.h" 00023 #include "llvm/ADT/Statistic.h" 00024 #include "llvm/ADT/STLExtras.h" 00025 #include "llvm/Support/PredIteratorCache.h" 00026 #include "llvm/Support/Debug.h" 00027 using namespace llvm; 00028 00029 STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses"); 00030 STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses"); 00031 STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses"); 00032 00033 STATISTIC(NumCacheNonLocalPtr, 00034 "Number of fully cached non-local ptr responses"); 00035 STATISTIC(NumCacheDirtyNonLocalPtr, 00036 "Number of cached, but dirty, non-local ptr responses"); 00037 STATISTIC(NumUncacheNonLocalPtr, 00038 "Number of uncached non-local ptr responses"); 00039 STATISTIC(NumCacheCompleteNonLocalPtr, 00040 "Number of block queries that were completely cached"); 00041 00042 char MemoryDependenceAnalysis::ID = 0; 00043 00044 // Register this pass... 00045 static RegisterPass<MemoryDependenceAnalysis> X("memdep", 00046 "Memory Dependence Analysis", false, true); 00047 00048 MemoryDependenceAnalysis::MemoryDependenceAnalysis() 00049 : FunctionPass(&ID), PredCache(0) { 00050 } 00051 MemoryDependenceAnalysis::~MemoryDependenceAnalysis() { 00052 } 00053 00054 /// Clean up memory in between runs 00055 void MemoryDependenceAnalysis::releaseMemory() { 00056 LocalDeps.clear(); 00057 NonLocalDeps.clear(); 00058 NonLocalPointerDeps.clear(); 00059 ReverseLocalDeps.clear(); 00060 ReverseNonLocalDeps.clear(); 00061 ReverseNonLocalPtrDeps.clear(); 00062 PredCache->clear(); 00063 } 00064 00065 00066 00067 /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis. 00068 /// 00069 void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 00070 AU.setPreservesAll(); 00071 AU.addRequiredTransitive<AliasAnalysis>(); 00072 } 00073 00074 bool MemoryDependenceAnalysis::runOnFunction(Function &) { 00075 AA = &getAnalysis<AliasAnalysis>(); 00076 if (PredCache == 0) 00077 PredCache.reset(new PredIteratorCache()); 00078 return false; 00079 } 00080 00081 /// RemoveFromReverseMap - This is a helper function that removes Val from 00082 /// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry. 00083 template <typename KeyTy> 00084 static void RemoveFromReverseMap(DenseMap<Instruction*, 00085 SmallPtrSet<KeyTy, 4> > &ReverseMap, 00086 Instruction *Inst, KeyTy Val) { 00087 typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator 00088 InstIt = ReverseMap.find(Inst); 00089 assert(InstIt != ReverseMap.end() && "Reverse map out of sync?"); 00090 bool Found = InstIt->second.erase(Val); 00091 assert(Found && "Invalid reverse map!"); Found=Found; 00092 if (InstIt->second.empty()) 00093 ReverseMap.erase(InstIt); 00094 } 00095 00096 00097 /// getCallSiteDependencyFrom - Private helper for finding the local 00098 /// dependencies of a call site. 00099 MemDepResult MemoryDependenceAnalysis:: 00100 getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall, 00101 BasicBlock::iterator ScanIt, BasicBlock *BB) { 00102 // Walk backwards through the block, looking for dependencies 00103 while (ScanIt != BB->begin()) { 00104 Instruction *Inst = --ScanIt; 00105 00106 // If this inst is a memory op, get the pointer it accessed 00107 Value *Pointer = 0; 00108 uint64_t PointerSize = 0; 00109 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) { 00110 Pointer = S->getPointerOperand(); 00111 PointerSize = AA->getTypeStoreSize(S->getOperand(0)->getType()); 00112 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) { 00113 Pointer = V->getOperand(0); 00114 PointerSize = AA->getTypeStoreSize(V->getType()); 00115 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) { 00116 Pointer = F->getPointerOperand(); 00117 00118 // FreeInsts erase the entire structure 00119 PointerSize = ~0ULL; 00120 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) { 00121 // Debug intrinsics don't cause dependences. 00122 if (isa<DbgInfoIntrinsic>(Inst)) continue; 00123 CallSite InstCS = CallSite::get(Inst); 00124 // If these two calls do not interfere, look past it. 00125 switch (AA->getModRefInfo(CS, InstCS)) { 00126 case AliasAnalysis::NoModRef: 00127 // If the two calls don't interact (e.g. InstCS is readnone) keep 00128 // scanning. 00129 continue; 00130 case AliasAnalysis::Ref: 00131 // If the two calls read the same memory locations and CS is a readonly 00132 // function, then we have two cases: 1) the calls may not interfere with 00133 // each other at all. 2) the calls may produce the same value. In case 00134 // #1 we want to ignore the values, in case #2, we want to return Inst 00135 // as a Def dependence. This allows us to CSE in cases like: 00136 // X = strlen(P); 00137 // memchr(...); 00138 // Y = strlen(P); // Y = X 00139 if (isReadOnlyCall) { 00140 if (CS.getCalledFunction() != 0 && 00141 CS.getCalledFunction() == InstCS.getCalledFunction()) 00142 return MemDepResult::getDef(Inst); 00143 // Ignore unrelated read/read call dependences. 00144 continue; 00145 } 00146 // FALL THROUGH 00147 default: 00148 return MemDepResult::getClobber(Inst); 00149 } 00150 } else { 00151 // Non-memory instruction. 00152 continue; 00153 } 00154 00155 if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef) 00156 return MemDepResult::getClobber(Inst); 00157 } 00158 00159 // No dependence found. If this is the entry block of the function, it is a 00160 // clobber, otherwise it is non-local. 00161 if (BB != &BB->getParent()->getEntryBlock()) 00162 return MemDepResult::getNonLocal(); 00163 return MemDepResult::getClobber(ScanIt); 00164 } 00165 00166 /// getPointerDependencyFrom - Return the instruction on which a memory 00167 /// location depends. If isLoad is true, this routine ignore may-aliases with 00168 /// read-only operations. 00169 MemDepResult MemoryDependenceAnalysis:: 00170 getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad, 00171 BasicBlock::iterator ScanIt, BasicBlock *BB) { 00172 00173 // Walk backwards through the basic block, looking for dependencies. 00174 while (ScanIt != BB->begin()) { 00175 Instruction *Inst = --ScanIt; 00176 00177 // Debug intrinsics don't cause dependences. 00178 if (isa<DbgInfoIntrinsic>(Inst)) continue; 00179 00180 // Values depend on loads if the pointers are must aliased. This means that 00181 // a load depends on another must aliased load from the same value. 00182 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { 00183 Value *Pointer = LI->getPointerOperand(); 00184 uint64_t PointerSize = AA->getTypeStoreSize(LI->getType()); 00185 00186 // If we found a pointer, check if it could be the same as our pointer. 00187 AliasAnalysis::AliasResult R = 00188 AA->alias(Pointer, PointerSize, MemPtr, MemSize); 00189 if (R == AliasAnalysis::NoAlias) 00190 continue; 00191 00192 // May-alias loads don't depend on each other without a dependence. 00193 if (isLoad && R == AliasAnalysis::MayAlias) 00194 continue; 00195 // Stores depend on may and must aliased loads, loads depend on must-alias 00196 // loads. 00197 return MemDepResult::getDef(Inst); 00198 } 00199 00200 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 00201 // If alias analysis can tell that this store is guaranteed to not modify 00202 // the query pointer, ignore it. Use getModRefInfo to handle cases where 00203 // the query pointer points to constant memory etc. 00204 if (AA->getModRefInfo(SI, MemPtr, MemSize) == AliasAnalysis::NoModRef) 00205 continue; 00206 00207 // Ok, this store might clobber the query pointer. Check to see if it is 00208 // a must alias: in this case, we want to return this as a def. 00209 Value *Pointer = SI->getPointerOperand(); 00210 uint64_t PointerSize = AA->getTypeStoreSize(SI->getOperand(0)->getType()); 00211 00212 // If we found a pointer, check if it could be the same as our pointer. 00213 AliasAnalysis::AliasResult R = 00214 AA->alias(Pointer, PointerSize, MemPtr, MemSize); 00215 00216 if (R == AliasAnalysis::NoAlias) 00217 continue; 00218 if (R == AliasAnalysis::MayAlias) 00219 return MemDepResult::getClobber(Inst); 00220 return MemDepResult::getDef(Inst); 00221 } 00222 00223 // If this is an allocation, and if we know that the accessed pointer is to 00224 // the allocation, return Def. This means that there is no dependence and 00225 // the access can be optimized based on that. For example, a load could 00226 // turn into undef. 00227 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) { 00228 Value *AccessPtr = MemPtr->getUnderlyingObject(); 00229 00230 if (AccessPtr == AI || 00231 AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias) 00232 return MemDepResult::getDef(AI); 00233 continue; 00234 } 00235 00236 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer. 00237 switch (AA->getModRefInfo(Inst, MemPtr, MemSize)) { 00238 case AliasAnalysis::NoModRef: 00239 // If the call has no effect on the queried pointer, just ignore it. 00240 continue; 00241 case AliasAnalysis::Ref: 00242 // If the call is known to never store to the pointer, and if this is a 00243 // load query, we can safely ignore it (scan past it). 00244 if (isLoad) 00245 continue; 00246 // FALL THROUGH. 00247 default: 00248 // Otherwise, there is a potential dependence. Return a clobber. 00249 return MemDepResult::getClobber(Inst); 00250 } 00251 } 00252 00253 // No dependence found. If this is the entry block of the function, it is a 00254 // clobber, otherwise it is non-local. 00255 if (BB != &BB->getParent()->getEntryBlock()) 00256 return MemDepResult::getNonLocal(); 00257 return MemDepResult::getClobber(ScanIt); 00258 } 00259 00260 /// getDependency - Return the instruction on which a memory operation 00261 /// depends. 00262 MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) { 00263 Instruction *ScanPos = QueryInst; 00264 00265 // Check for a cached result 00266 MemDepResult &LocalCache = LocalDeps[QueryInst]; 00267 00268 // If the cached entry is non-dirty, just return it. Note that this depends 00269 // on MemDepResult's default constructing to 'dirty'. 00270 if (!LocalCache.isDirty()) 00271 return LocalCache; 00272 00273 // Otherwise, if we have a dirty entry, we know we can start the scan at that 00274 // instruction, which may save us some work. 00275 if (Instruction *Inst = LocalCache.getInst()) { 00276 ScanPos = Inst; 00277 00278 RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst); 00279 } 00280 00281 BasicBlock *QueryParent = QueryInst->getParent(); 00282 00283 Value *MemPtr = 0; 00284 uint64_t MemSize = 0; 00285 00286 // Do the scan. 00287 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) { 00288 // No dependence found. If this is the entry block of the function, it is a 00289 // clobber, otherwise it is non-local. 00290 if (QueryParent != &QueryParent->getParent()->getEntryBlock()) 00291 LocalCache = MemDepResult::getNonLocal(); 00292 else 00293 LocalCache = MemDepResult::getClobber(QueryInst); 00294 } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) { 00295 // If this is a volatile store, don't mess around with it. Just return the 00296 // previous instruction as a clobber. 00297 if (SI->isVolatile()) 00298 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); 00299 else { 00300 MemPtr = SI->getPointerOperand(); 00301 MemSize = AA->getTypeStoreSize(SI->getOperand(0)->getType()); 00302 } 00303 } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) { 00304 // If this is a volatile load, don't mess around with it. Just return the 00305 // previous instruction as a clobber. 00306 if (LI->isVolatile()) 00307 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); 00308 else { 00309 MemPtr = LI->getPointerOperand(); 00310 MemSize = AA->getTypeStoreSize(LI->getType()); 00311 } 00312 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) { 00313 CallSite QueryCS = CallSite::get(QueryInst); 00314 bool isReadOnly = AA->onlyReadsMemory(QueryCS); 00315 LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos, 00316 QueryParent); 00317 } else if (FreeInst *FI = dyn_cast<FreeInst>(QueryInst)) { 00318 MemPtr = FI->getPointerOperand(); 00319 // FreeInsts erase the entire structure, not just a field. 00320 MemSize = ~0UL; 00321 } else { 00322 // Non-memory instruction. 00323 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); 00324 } 00325 00326 // If we need to do a pointer scan, make it happen. 00327 if (MemPtr) 00328 LocalCache = getPointerDependencyFrom(MemPtr, MemSize, 00329 isa<LoadInst>(QueryInst), 00330 ScanPos, QueryParent); 00331 00332 // Remember the result! 00333 if (Instruction *I = LocalCache.getInst()) 00334 ReverseLocalDeps[I].insert(QueryInst); 00335 00336 return LocalCache; 00337 } 00338 00339 #ifndef NDEBUG 00340 /// AssertSorted - This method is used when -debug is specified to verify that 00341 /// cache arrays are properly kept sorted. 00342 static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache, 00343 int Count = -1) { 00344 if (Count == -1) Count = Cache.size(); 00345 if (Count == 0) return; 00346 00347 for (unsigned i = 1; i != unsigned(Count); ++i) 00348 assert(Cache[i-1] <= Cache[i] && "Cache isn't sorted!"); 00349 } 00350 #endif 00351 00352 /// getNonLocalCallDependency - Perform a full dependency query for the 00353 /// specified call, returning the set of blocks that the value is 00354 /// potentially live across. The returned set of results will include a 00355 /// "NonLocal" result for all blocks where the value is live across. 00356 /// 00357 /// This method assumes the instruction returns a "NonLocal" dependency 00358 /// within its own block. 00359 /// 00360 /// This returns a reference to an internal data structure that may be 00361 /// invalidated on the next non-local query or when an instruction is 00362 /// removed. Clients must copy this data if they want it around longer than 00363 /// that. 00364 const MemoryDependenceAnalysis::NonLocalDepInfo & 00365 MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) { 00366 assert(getDependency(QueryCS.getInstruction()).isNonLocal() && 00367 "getNonLocalCallDependency should only be used on calls with non-local deps!"); 00368 PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()]; 00369 NonLocalDepInfo &Cache = CacheP.first; 00370 00371 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In 00372 /// the cached case, this can happen due to instructions being deleted etc. In 00373 /// the uncached case, this starts out as the set of predecessors we care 00374 /// about. 00375 SmallVector<BasicBlock*, 32> DirtyBlocks; 00376 00377 if (!Cache.empty()) { 00378 // Okay, we have a cache entry. If we know it is not dirty, just return it 00379 // with no computation. 00380 if (!CacheP.second) { 00381 NumCacheNonLocal++; 00382 return Cache; 00383 } 00384 00385 // If we already have a partially computed set of results, scan them to 00386 // determine what is dirty, seeding our initial DirtyBlocks worklist. 00387 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end(); 00388 I != E; ++I) 00389 if (I->second.isDirty()) 00390 DirtyBlocks.push_back(I->first); 00391 00392 // Sort the cache so that we can do fast binary search lookups below. 00393 std::sort(Cache.begin(), Cache.end()); 00394 00395 ++NumCacheDirtyNonLocal; 00396 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: " 00397 // << Cache.size() << " cached: " << *QueryInst; 00398 } else { 00399 // Seed DirtyBlocks with each of the preds of QueryInst's block. 00400 BasicBlock *QueryBB = QueryCS.getInstruction()->getParent(); 00401 for (BasicBlock **PI = PredCache->GetPreds(QueryBB); *PI; ++PI) 00402 DirtyBlocks.push_back(*PI); 00403 NumUncacheNonLocal++; 00404 } 00405 00406 // isReadonlyCall - If this is a read-only call, we can be more aggressive. 00407 bool isReadonlyCall = AA->onlyReadsMemory(QueryCS); 00408 00409 SmallPtrSet<BasicBlock*, 64> Visited; 00410 00411 unsigned NumSortedEntries = Cache.size(); 00412 DEBUG(AssertSorted(Cache)); 00413 00414 // Iterate while we still have blocks to update. 00415 while (!DirtyBlocks.empty()) { 00416 BasicBlock *DirtyBB = DirtyBlocks.back(); 00417 DirtyBlocks.pop_back(); 00418 00419 // Already processed this block? 00420 if (!Visited.insert(DirtyBB)) 00421 continue; 00422 00423 // Do a binary search to see if we already have an entry for this block in 00424 // the cache set. If so, find it. 00425 DEBUG(AssertSorted(Cache, NumSortedEntries)); 00426 NonLocalDepInfo::iterator Entry = 00427 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries, 00428 std::make_pair(DirtyBB, MemDepResult())); 00429 if (Entry != Cache.begin() && prior(Entry)->first == DirtyBB) 00430 --Entry; 00431 00432 MemDepResult *ExistingResult = 0; 00433 if (Entry != Cache.begin()+NumSortedEntries && 00434 Entry->first == DirtyBB) { 00435 // If we already have an entry, and if it isn't already dirty, the block 00436 // is done. 00437 if (!Entry->second.isDirty()) 00438 continue; 00439 00440 // Otherwise, remember this slot so we can update the value. 00441 ExistingResult = &Entry->second; 00442 } 00443 00444 // If the dirty entry has a pointer, start scanning from it so we don't have 00445 // to rescan the entire block. 00446 BasicBlock::iterator ScanPos = DirtyBB->end(); 00447 if (ExistingResult) { 00448 if (Instruction *Inst = ExistingResult->getInst()) { 00449 ScanPos = Inst; 00450 // We're removing QueryInst's use of Inst. 00451 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, 00452 QueryCS.getInstruction()); 00453 } 00454 } 00455 00456 // Find out if this block has a local dependency for QueryInst. 00457 MemDepResult Dep; 00458 00459 if (ScanPos != DirtyBB->begin()) { 00460 Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB); 00461 } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) { 00462 // No dependence found. If this is the entry block of the function, it is 00463 // a clobber, otherwise it is non-local. 00464 Dep = MemDepResult::getNonLocal(); 00465 } else { 00466 Dep = MemDepResult::getClobber(ScanPos); 00467 } 00468 00469 // If we had a dirty entry for the block, update it. Otherwise, just add 00470 // a new entry. 00471 if (ExistingResult) 00472 *ExistingResult = Dep; 00473 else 00474 Cache.push_back(std::make_pair(DirtyBB, Dep)); 00475 00476 // If the block has a dependency (i.e. it isn't completely transparent to 00477 // the value), remember the association! 00478 if (!Dep.isNonLocal()) { 00479 // Keep the ReverseNonLocalDeps map up to date so we can efficiently 00480 // update this when we remove instructions. 00481 if (Instruction *Inst = Dep.getInst()) 00482 ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction()); 00483 } else { 00484 00485 // If the block *is* completely transparent to the load, we need to check 00486 // the predecessors of this block. Add them to our worklist. 00487 for (BasicBlock **PI = PredCache->GetPreds(DirtyBB); *PI; ++PI) 00488 DirtyBlocks.push_back(*PI); 00489 } 00490 } 00491 00492 return Cache; 00493 } 00494 00495 /// getNonLocalPointerDependency - Perform a full dependency query for an 00496 /// access to the specified (non-volatile) memory location, returning the 00497 /// set of instructions that either define or clobber the value. 00498 /// 00499 /// This method assumes the pointer has a "NonLocal" dependency within its 00500 /// own block. 00501 /// 00502 void MemoryDependenceAnalysis:: 00503 getNonLocalPointerDependency(Value *Pointer, bool isLoad, BasicBlock *FromBB, 00504 SmallVectorImpl<NonLocalDepEntry> &Result) { 00505 assert(isa<PointerType>(Pointer->getType()) && 00506 "Can't get pointer deps of a non-pointer!"); 00507 Result.clear(); 00508 00509 // We know that the pointer value is live into FromBB find the def/clobbers 00510 // from presecessors. 00511 const Type *EltTy = cast<PointerType>(Pointer->getType())->getElementType(); 00512 uint64_t PointeeSize = AA->getTypeStoreSize(EltTy); 00513 00514 // This is the set of blocks we've inspected, and the pointer we consider in 00515 // each block. Because of critical edges, we currently bail out if querying 00516 // a block with multiple different pointers. This can happen during PHI 00517 // translation. 00518 DenseMap<BasicBlock*, Value*> Visited; 00519 if (!getNonLocalPointerDepFromBB(Pointer, PointeeSize, isLoad, FromBB, 00520 Result, Visited, true)) 00521 return; 00522 Result.clear(); 00523 Result.push_back(std::make_pair(FromBB, 00524 MemDepResult::getClobber(FromBB->begin()))); 00525 } 00526 00527 /// GetNonLocalInfoForBlock - Compute the memdep value for BB with 00528 /// Pointer/PointeeSize using either cached information in Cache or by doing a 00529 /// lookup (which may use dirty cache info if available). If we do a lookup, 00530 /// add the result to the cache. 00531 MemDepResult MemoryDependenceAnalysis:: 00532 GetNonLocalInfoForBlock(Value *Pointer, uint64_t PointeeSize, 00533 bool isLoad, BasicBlock *BB, 00534 NonLocalDepInfo *Cache, unsigned NumSortedEntries) { 00535 00536 // Do a binary search to see if we already have an entry for this block in 00537 // the cache set. If so, find it. 00538 NonLocalDepInfo::iterator Entry = 00539 std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries, 00540 std::make_pair(BB, MemDepResult())); 00541 if (Entry != Cache->begin() && prior(Entry)->first == BB) 00542 --Entry; 00543 00544 MemDepResult *ExistingResult = 0; 00545 if (Entry != Cache->begin()+NumSortedEntries && Entry->first == BB) 00546 ExistingResult = &Entry->second; 00547 00548 // If we have a cached entry, and it is non-dirty, use it as the value for 00549 // this dependency. 00550 if (ExistingResult && !ExistingResult->isDirty()) { 00551 ++NumCacheNonLocalPtr; 00552 return *ExistingResult; 00553 } 00554 00555 // Otherwise, we have to scan for the value. If we have a dirty cache 00556 // entry, start scanning from its position, otherwise we scan from the end 00557 // of the block. 00558 BasicBlock::iterator ScanPos = BB->end(); 00559 if (ExistingResult && ExistingResult->getInst()) { 00560 assert(ExistingResult->getInst()->getParent() == BB && 00561 "Instruction invalidated?"); 00562 ++NumCacheDirtyNonLocalPtr; 00563 ScanPos = ExistingResult->getInst(); 00564 00565 // Eliminating the dirty entry from 'Cache', so update the reverse info. 00566 ValueIsLoadPair CacheKey(Pointer, isLoad); 00567 RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey); 00568 } else { 00569 ++NumUncacheNonLocalPtr; 00570 } 00571 00572 // Scan the block for the dependency. 00573 MemDepResult Dep = getPointerDependencyFrom(Pointer, PointeeSize, isLoad, 00574 ScanPos, BB); 00575 00576 // If we had a dirty entry for the block, update it. Otherwise, just add 00577 // a new entry. 00578 if (ExistingResult) 00579 *ExistingResult = Dep; 00580 else 00581 Cache->push_back(std::make_pair(BB, Dep)); 00582 00583 // If the block has a dependency (i.e. it isn't completely transparent to 00584 // the value), remember the reverse association because we just added it 00585 // to Cache! 00586 if (Dep.isNonLocal()) 00587 return Dep; 00588 00589 // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently 00590 // update MemDep when we remove instructions. 00591 Instruction *Inst = Dep.getInst(); 00592 assert(Inst && "Didn't depend on anything?"); 00593 ValueIsLoadPair CacheKey(Pointer, isLoad); 00594 ReverseNonLocalPtrDeps[Inst].insert(CacheKey); 00595 return Dep; 00596 } 00597 00598 /// SortNonLocalDepInfoCache - Sort the a NonLocalDepInfo cache, given a certain 00599 /// number of elements in the array that are already properly ordered. This is 00600 /// optimized for the case when only a few entries are added. 00601 static void 00602 SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache, 00603 unsigned NumSortedEntries) { 00604 switch (Cache.size() - NumSortedEntries) { 00605 case 0: 00606 // done, no new entries. 00607 break; 00608 case 2: { 00609 // Two new entries, insert the last one into place. 00610 MemoryDependenceAnalysis::NonLocalDepEntry Val = Cache.back(); 00611 Cache.pop_back(); 00612 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry = 00613 std::upper_bound(Cache.begin(), Cache.end()-1, Val); 00614 Cache.insert(Entry, Val); 00615 // FALL THROUGH. 00616 } 00617 case 1: 00618 // One new entry, Just insert the new value at the appropriate position. 00619 if (Cache.size() != 1) { 00620 MemoryDependenceAnalysis::NonLocalDepEntry Val = Cache.back(); 00621 Cache.pop_back(); 00622 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry = 00623 std::upper_bound(Cache.begin(), Cache.end(), Val); 00624 Cache.insert(Entry, Val); 00625 } 00626 break; 00627 default: 00628 // Added many values, do a full scale sort. 00629 std::sort(Cache.begin(), Cache.end()); 00630 break; 00631 } 00632 } 00633 00634 00635 /// getNonLocalPointerDepFromBB - Perform a dependency query based on 00636 /// pointer/pointeesize starting at the end of StartBB. Add any clobber/def 00637 /// results to the results vector and keep track of which blocks are visited in 00638 /// 'Visited'. 00639 /// 00640 /// This has special behavior for the first block queries (when SkipFirstBlock 00641 /// is true). In this special case, it ignores the contents of the specified 00642 /// block and starts returning dependence info for its predecessors. 00643 /// 00644 /// This function returns false on success, or true to indicate that it could 00645 /// not compute dependence information for some reason. This should be treated 00646 /// as a clobber dependence on the first instruction in the predecessor block. 00647 bool MemoryDependenceAnalysis:: 00648 getNonLocalPointerDepFromBB(Value *Pointer, uint64_t PointeeSize, 00649 bool isLoad, BasicBlock *StartBB, 00650 SmallVectorImpl<NonLocalDepEntry> &Result, 00651 DenseMap<BasicBlock*, Value*> &Visited, 00652 bool SkipFirstBlock) { 00653 00654 // Look up the cached info for Pointer. 00655 ValueIsLoadPair CacheKey(Pointer, isLoad); 00656 00657 std::pair<BBSkipFirstBlockPair, NonLocalDepInfo> *CacheInfo = 00658 &NonLocalPointerDeps[CacheKey]; 00659 NonLocalDepInfo *Cache = &CacheInfo->second; 00660 00661 // If we have valid cached information for exactly the block we are 00662 // investigating, just return it with no recomputation. 00663 if (CacheInfo->first == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) { 00664 // We have a fully cached result for this query then we can just return the 00665 // cached results and populate the visited set. However, we have to verify 00666 // that we don't already have conflicting results for these blocks. Check 00667 // to ensure that if a block in the results set is in the visited set that 00668 // it was for the same pointer query. 00669 if (!Visited.empty()) { 00670 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end(); 00671 I != E; ++I) { 00672 DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->first); 00673 if (VI == Visited.end() || VI->second == Pointer) continue; 00674 00675 // We have a pointer mismatch in a block. Just return clobber, saying 00676 // that something was clobbered in this result. We could also do a 00677 // non-fully cached query, but there is little point in doing this. 00678 return true; 00679 } 00680 } 00681 00682 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end(); 00683 I != E; ++I) { 00684 Visited.insert(std::make_pair(I->first, Pointer)); 00685 if (!I->second.isNonLocal()) 00686 Result.push_back(*I); 00687 } 00688 ++NumCacheCompleteNonLocalPtr; 00689 return false; 00690 } 00691 00692 // Otherwise, either this is a new block, a block with an invalid cache 00693 // pointer or one that we're about to invalidate by putting more info into it 00694 // than its valid cache info. If empty, the result will be valid cache info, 00695 // otherwise it isn't. 00696 if (Cache->empty()) 00697 CacheInfo->first = BBSkipFirstBlockPair(StartBB, SkipFirstBlock); 00698 else 00699 CacheInfo->first = BBSkipFirstBlockPair(); 00700 00701 SmallVector<BasicBlock*, 32> Worklist; 00702 Worklist.push_back(StartBB); 00703 00704 // Keep track of the entries that we know are sorted. Previously cached 00705 // entries will all be sorted. The entries we add we only sort on demand (we 00706 // don't insert every element into its sorted position). We know that we 00707 // won't get any reuse from currently inserted values, because we don't 00708 // revisit blocks after we insert info for them. 00709 unsigned NumSortedEntries = Cache->size(); 00710 DEBUG(AssertSorted(*Cache)); 00711 00712 while (!Worklist.empty()) { 00713 BasicBlock *BB = Worklist.pop_back_val(); 00714 00715 // Skip the first block if we have it. 00716 if (!SkipFirstBlock) { 00717 // Analyze the dependency of *Pointer in FromBB. See if we already have 00718 // been here. 00719 assert(Visited.count(BB) && "Should check 'visited' before adding to WL"); 00720 00721 // Get the dependency info for Pointer in BB. If we have cached 00722 // information, we will use it, otherwise we compute it. 00723 DEBUG(AssertSorted(*Cache, NumSortedEntries)); 00724 MemDepResult Dep = GetNonLocalInfoForBlock(Pointer, PointeeSize, isLoad, 00725 BB, Cache, NumSortedEntries); 00726 00727 // If we got a Def or Clobber, add this to the list of results. 00728 if (!Dep.isNonLocal()) { 00729 Result.push_back(NonLocalDepEntry(BB, Dep)); 00730 continue; 00731 } 00732 } 00733 00734 // If 'Pointer' is an instruction defined in this block, then we need to do 00735 // phi translation to change it into a value live in the predecessor block. 00736 // If phi translation fails, then we can't continue dependence analysis. 00737 Instruction *PtrInst = dyn_cast<Instruction>(Pointer); 00738 bool NeedsPHITranslation = PtrInst && PtrInst->getParent() == BB; 00739 00740 // If no PHI translation is needed, just add all the predecessors of this 00741 // block to scan them as well. 00742 if (!NeedsPHITranslation) { 00743 SkipFirstBlock = false; 00744 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) { 00745 // Verify that we haven't looked at this block yet. 00746 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool> 00747 InsertRes = Visited.insert(std::make_pair(*PI, Pointer)); 00748 if (InsertRes.second) { 00749 // First time we've looked at *PI. 00750 Worklist.push_back(*PI); 00751 continue; 00752 } 00753 00754 // If we have seen this block before, but it was with a different 00755 // pointer then we have a phi translation failure and we have to treat 00756 // this as a clobber. 00757 if (InsertRes.first->second != Pointer) 00758 goto PredTranslationFailure; 00759 } 00760 continue; 00761 } 00762 00763 // If we do need to do phi translation, then there are a bunch of different 00764 // cases, because we have to find a Value* live in the predecessor block. We 00765 // know that PtrInst is defined in this block at least. 00766 00767 // We may have added values to the cache list before this PHI translation. 00768 // If so, we haven't done anything to ensure that the cache remains sorted. 00769 // Sort it now (if needed) so that recursive invocations of 00770 // getNonLocalPointerDepFromBB and other routines that could reuse the cache 00771 // value will only see properly sorted cache arrays. 00772 if (Cache && NumSortedEntries != Cache->size()) { 00773 SortNonLocalDepInfoCache(*Cache, NumSortedEntries); 00774 NumSortedEntries = Cache->size(); 00775 } 00776 00777 // If this is directly a PHI node, just use the incoming values for each 00778 // pred as the phi translated version. 00779 if (PHINode *PtrPHI = dyn_cast<PHINode>(PtrInst)) { 00780 Cache = 0; 00781 00782 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) { 00783 BasicBlock *Pred = *PI; 00784 Value *PredPtr = PtrPHI->getIncomingValueForBlock(Pred); 00785 00786 // Check to see if we have already visited this pred block with another 00787 // pointer. If so, we can't do this lookup. This failure can occur 00788 // with PHI translation when a critical edge exists and the PHI node in 00789 // the successor translates to a pointer value different than the 00790 // pointer the block was first analyzed with. 00791 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool> 00792 InsertRes = Visited.insert(std::make_pair(Pred, PredPtr)); 00793 00794 if (!InsertRes.second) { 00795 // If the predecessor was visited with PredPtr, then we already did 00796 // the analysis and can ignore it. 00797 if (InsertRes.first->second == PredPtr) 00798 continue; 00799 00800 // Otherwise, the block was previously analyzed with a different 00801 // pointer. We can't represent the result of this case, so we just 00802 // treat this as a phi translation failure. 00803 goto PredTranslationFailure; 00804 } 00805 00806 // FIXME: it is entirely possible that PHI translating will end up with 00807 // the same value. Consider PHI translating something like: 00808 // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need* 00809 // to recurse here, pedantically speaking. 00810 00811 // If we have a problem phi translating, fall through to the code below 00812 // to handle the failure condition. 00813 if (getNonLocalPointerDepFromBB(PredPtr, PointeeSize, isLoad, Pred, 00814 Result, Visited)) 00815 goto PredTranslationFailure; 00816 } 00817 00818 // Refresh the CacheInfo/Cache pointer so that it isn't invalidated. 00819 CacheInfo = &NonLocalPointerDeps[CacheKey]; 00820 Cache = &CacheInfo->second; 00821 NumSortedEntries = Cache->size(); 00822 00823 // Since we did phi translation, the "Cache" set won't contain all of the 00824 // results for the query. This is ok (we can still use it to accelerate 00825 // specific block queries) but we can't do the fastpath "return all 00826 // results from the set" Clear out the indicator for this. 00827 CacheInfo->first = BBSkipFirstBlockPair(); 00828 SkipFirstBlock = false; 00829 continue; 00830 } 00831 00832 // TODO: BITCAST, GEP. 00833 00834 // cerr << "MEMDEP: Could not PHI translate: " << *Pointer; 00835 // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst)) 00836 // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0); 00837 PredTranslationFailure: 00838 00839 if (Cache == 0) { 00840 // Refresh the CacheInfo/Cache pointer if it got invalidated. 00841 CacheInfo = &NonLocalPointerDeps[CacheKey]; 00842 Cache = &CacheInfo->second; 00843 NumSortedEntries = Cache->size(); 00844 } 00845 00846 // Since we did phi translation, the "Cache" set won't contain all of the 00847 // results for the query. This is ok (we can still use it to accelerate 00848 // specific block queries) but we can't do the fastpath "return all 00849 // results from the set" Clear out the indicator for this. 00850 CacheInfo->first = BBSkipFirstBlockPair(); 00851 00852 // If *nothing* works, mark the pointer as being clobbered by the first 00853 // instruction in this block. 00854 // 00855 // If this is the magic first block, return this as a clobber of the whole 00856 // incoming value. Since we can't phi translate to one of the predecessors, 00857 // we have to bail out. 00858 if (SkipFirstBlock) 00859 return true; 00860 00861 for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) { 00862 assert(I != Cache->rend() && "Didn't find current block??"); 00863 if (I->first != BB) 00864 continue; 00865 00866 assert(I->second.isNonLocal() && 00867 "Should only be here with transparent block"); 00868 I->second = MemDepResult::getClobber(BB->begin()); 00869 ReverseNonLocalPtrDeps[BB->begin()].insert(CacheKey); 00870 Result.push_back(*I); 00871 break; 00872 } 00873 } 00874 00875 // Okay, we're done now. If we added new values to the cache, re-sort it. 00876 SortNonLocalDepInfoCache(*Cache, NumSortedEntries); 00877 DEBUG(AssertSorted(*Cache)); 00878 return false; 00879 } 00880 00881 /// RemoveCachedNonLocalPointerDependencies - If P exists in 00882 /// CachedNonLocalPointerInfo, remove it. 00883 void MemoryDependenceAnalysis:: 00884 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) { 00885 CachedNonLocalPointerInfo::iterator It = 00886 NonLocalPointerDeps.find(P); 00887 if (It == NonLocalPointerDeps.end()) return; 00888 00889 // Remove all of the entries in the BB->val map. This involves removing 00890 // instructions from the reverse map. 00891 NonLocalDepInfo &PInfo = It->second.second; 00892 00893 for (unsigned i = 0, e = PInfo.size(); i != e; ++i) { 00894 Instruction *Target = PInfo[i].second.getInst(); 00895 if (Target == 0) continue; // Ignore non-local dep results. 00896 assert(Target->getParent() == PInfo[i].first); 00897 00898 // Eliminating the dirty entry from 'Cache', so update the reverse info. 00899 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P); 00900 } 00901 00902 // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo). 00903 NonLocalPointerDeps.erase(It); 00904 } 00905 00906 00907 /// invalidateCachedPointerInfo - This method is used to invalidate cached 00908 /// information about the specified pointer, because it may be too 00909 /// conservative in memdep. This is an optional call that can be used when 00910 /// the client detects an equivalence between the pointer and some other 00911 /// value and replaces the other value with ptr. This can make Ptr available 00912 /// in more places that cached info does not necessarily keep. 00913 void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) { 00914 // If Ptr isn't really a pointer, just ignore it. 00915 if (!isa<PointerType>(Ptr->getType())) return; 00916 // Flush store info for the pointer. 00917 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false)); 00918 // Flush load info for the pointer. 00919 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true)); 00920 } 00921 00922 /// removeInstruction - Remove an instruction from the dependence analysis, 00923 /// updating the dependence of instructions that previously depended on it. 00924 /// This method attempts to keep the cache coherent using the reverse map. 00925 void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) { 00926 // Walk through the Non-local dependencies, removing this one as the value 00927 // for any cached queries. 00928 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst); 00929 if (NLDI != NonLocalDeps.end()) { 00930 NonLocalDepInfo &BlockMap = NLDI->second.first; 00931 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end(); 00932 DI != DE; ++DI) 00933 if (Instruction *Inst = DI->second.getInst()) 00934 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst); 00935 NonLocalDeps.erase(NLDI); 00936 } 00937 00938 // If we have a cached local dependence query for this instruction, remove it. 00939 // 00940 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst); 00941 if (LocalDepEntry != LocalDeps.end()) { 00942 // Remove us from DepInst's reverse set now that the local dep info is gone. 00943 if (Instruction *Inst = LocalDepEntry->second.getInst()) 00944 RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst); 00945 00946 // Remove this local dependency info. 00947 LocalDeps.erase(LocalDepEntry); 00948 } 00949 00950 // If we have any cached pointer dependencies on this instruction, remove 00951 // them. If the instruction has non-pointer type, then it can't be a pointer 00952 // base. 00953 00954 // Remove it from both the load info and the store info. The instruction 00955 // can't be in either of these maps if it is non-pointer. 00956 if (isa<PointerType>(RemInst->getType())) { 00957 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false)); 00958 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true)); 00959 } 00960 00961 // Loop over all of the things that depend on the instruction we're removing. 00962 // 00963 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd; 00964 00965 // If we find RemInst as a clobber or Def in any of the maps for other values, 00966 // we need to replace its entry with a dirty version of the instruction after 00967 // it. If RemInst is a terminator, we use a null dirty value. 00968 // 00969 // Using a dirty version of the instruction after RemInst saves having to scan 00970 // the entire block to get to this point. 00971 MemDepResult NewDirtyVal; 00972 if (!RemInst->isTerminator()) 00973 NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst)); 00974 00975 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst); 00976 if (ReverseDepIt != ReverseLocalDeps.end()) { 00977 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second; 00978 // RemInst can't be the terminator if it has local stuff depending on it. 00979 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) && 00980 "Nothing can locally depend on a terminator"); 00981 00982 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(), 00983 E = ReverseDeps.end(); I != E; ++I) { 00984 Instruction *InstDependingOnRemInst = *I; 00985 assert(InstDependingOnRemInst != RemInst && 00986 "Already removed our local dep info"); 00987 00988 LocalDeps[InstDependingOnRemInst] = NewDirtyVal; 00989 00990 // Make sure to remember that new things depend on NewDepInst. 00991 assert(NewDirtyVal.getInst() && "There is no way something else can have " 00992 "a local dep on this if it is a terminator!"); 00993 ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(), 00994 InstDependingOnRemInst)); 00995 } 00996 00997 ReverseLocalDeps.erase(ReverseDepIt); 00998 00999 // Add new reverse deps after scanning the set, to avoid invalidating the 01000 // 'ReverseDeps' reference. 01001 while (!ReverseDepsToAdd.empty()) { 01002 ReverseLocalDeps[ReverseDepsToAdd.back().first] 01003 .insert(ReverseDepsToAdd.back().second); 01004 ReverseDepsToAdd.pop_back(); 01005 } 01006 } 01007 01008 ReverseDepIt = ReverseNonLocalDeps.find(RemInst); 01009 if (ReverseDepIt != ReverseNonLocalDeps.end()) { 01010 SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second; 01011 for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end(); 01012 I != E; ++I) { 01013 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst"); 01014 01015 PerInstNLInfo &INLD = NonLocalDeps[*I]; 01016 // The information is now dirty! 01017 INLD.second = true; 01018 01019 for (NonLocalDepInfo::iterator DI = INLD.first.begin(), 01020 DE = INLD.first.end(); DI != DE; ++DI) { 01021 if (DI->second.getInst() != RemInst) continue; 01022 01023 // Convert to a dirty entry for the subsequent instruction. 01024 DI->second = NewDirtyVal; 01025 01026 if (Instruction *NextI = NewDirtyVal.getInst()) 01027 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I)); 01028 } 01029 } 01030 01031 ReverseNonLocalDeps.erase(ReverseDepIt); 01032 01033 // Add new reverse deps after scanning the set, to avoid invalidating 'Set' 01034 while (!ReverseDepsToAdd.empty()) { 01035 ReverseNonLocalDeps[ReverseDepsToAdd.back().first] 01036 .insert(ReverseDepsToAdd.back().second); 01037 ReverseDepsToAdd.pop_back(); 01038 } 01039 } 01040 01041 // If the instruction is in ReverseNonLocalPtrDeps then it appears as a 01042 // value in the NonLocalPointerDeps info. 01043 ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt = 01044 ReverseNonLocalPtrDeps.find(RemInst); 01045 if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) { 01046 SmallPtrSet<ValueIsLoadPair, 4> &Set = ReversePtrDepIt->second; 01047 SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd; 01048 01049 for (SmallPtrSet<ValueIsLoadPair, 4>::iterator I = Set.begin(), 01050 E = Set.end(); I != E; ++I) { 01051 ValueIsLoadPair P = *I; 01052 assert(P.getPointer() != RemInst && 01053 "Already removed NonLocalPointerDeps info for RemInst"); 01054 01055 NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].second; 01056 01057 // The cache is not valid for any specific block anymore. 01058 NonLocalPointerDeps[P].first = BBSkipFirstBlockPair(); 01059 01060 // Update any entries for RemInst to use the instruction after it. 01061 for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end(); 01062 DI != DE; ++DI) { 01063 if (DI->second.getInst() != RemInst) continue; 01064 01065 // Convert to a dirty entry for the subsequent instruction. 01066 DI->second = NewDirtyVal; 01067 01068 if (Instruction *NewDirtyInst = NewDirtyVal.getInst()) 01069 ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P)); 01070 } 01071 01072 // Re-sort the NonLocalDepInfo. Changing the dirty entry to its 01073 // subsequent value may invalidate the sortedness. 01074 std::sort(NLPDI.begin(), NLPDI.end()); 01075 } 01076 01077 ReverseNonLocalPtrDeps.erase(ReversePtrDepIt); 01078 01079 while (!ReversePtrDepsToAdd.empty()) { 01080 ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first] 01081 .insert(ReversePtrDepsToAdd.back().second); 01082 ReversePtrDepsToAdd.pop_back(); 01083 } 01084 } 01085 01086 01087 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?"); 01088 AA->deleteValue(RemInst); 01089 DEBUG(verifyRemoved(RemInst)); 01090 } 01091 /// verifyRemoved - Verify that the specified instruction does not occur 01092 /// in our internal data structures. 01093 void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const { 01094 for (LocalDepMapType::const_iterator I = LocalDeps.begin(), 01095 E = LocalDeps.end(); I != E; ++I) { 01096 assert(I->first != D && "Inst occurs in data structures"); 01097 assert(I->second.getInst() != D && 01098 "Inst occurs in data structures"); 01099 } 01100 01101 for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(), 01102 E = NonLocalPointerDeps.end(); I != E; ++I) { 01103 assert(I->first.getPointer() != D && "Inst occurs in NLPD map key"); 01104 const NonLocalDepInfo &Val = I->second.second; 01105 for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end(); 01106 II != E; ++II) 01107 assert(II->second.getInst() != D && "Inst occurs as NLPD value"); 01108 } 01109 01110 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(), 01111 E = NonLocalDeps.end(); I != E; ++I) { 01112 assert(I->first != D && "Inst occurs in data structures"); 01113 const PerInstNLInfo &INLD = I->second; 01114 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(), 01115 EE = INLD.first.end(); II != EE; ++II) 01116 assert(II->second.getInst() != D && "Inst occurs in data structures"); 01117 } 01118 01119 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(), 01120 E = ReverseLocalDeps.end(); I != E; ++I) { 01121 assert(I->first != D && "Inst occurs in data structures"); 01122 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), 01123 EE = I->second.end(); II != EE; ++II) 01124 assert(*II != D && "Inst occurs in data structures"); 01125 } 01126 01127 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(), 01128 E = ReverseNonLocalDeps.end(); 01129 I != E; ++I) { 01130 assert(I->first != D && "Inst occurs in data structures"); 01131 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), 01132 EE = I->second.end(); II != EE; ++II) 01133 assert(*II != D && "Inst occurs in data structures"); 01134 } 01135 01136 for (ReverseNonLocalPtrDepTy::const_iterator 01137 I = ReverseNonLocalPtrDeps.begin(), 01138 E = ReverseNonLocalPtrDeps.end(); I != E; ++I) { 01139 assert(I->first != D && "Inst occurs in rev NLPD map"); 01140 01141 for (SmallPtrSet<ValueIsLoadPair, 4>::const_iterator II = I->second.begin(), 01142 E = I->second.end(); II != E; ++II) 01143 assert(*II != ValueIsLoadPair(D, false) && 01144 *II != ValueIsLoadPair(D, true) && 01145 "Inst occurs in ReverseNonLocalPtrDeps map"); 01146 } 01147 01148 }