1 //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements an analysis that determines, for a given memory 11 // operation, what preceding memory operations it depends on. It builds on 12 // alias analysis information, and tries to provide a lazy, caching interface to 13 // a common kind of alias information query. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/Analysis/MemoryDependenceAnalysis.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/Statistic.h" 20 #include "llvm/Analysis/AliasAnalysis.h" 21 #include "llvm/Analysis/AssumptionCache.h" 22 #include "llvm/Analysis/InstructionSimplify.h" 23 #include "llvm/Analysis/MemoryBuiltins.h" 24 #include "llvm/Analysis/PHITransAddr.h" 25 #include "llvm/Analysis/OrderedBasicBlock.h" 26 #include "llvm/Analysis/ValueTracking.h" 27 #include "llvm/Analysis/TargetLibraryInfo.h" 28 #include "llvm/IR/DataLayout.h" 29 #include "llvm/IR/Dominators.h" 30 #include "llvm/IR/Function.h" 31 #include "llvm/IR/Instructions.h" 32 #include "llvm/IR/IntrinsicInst.h" 33 #include "llvm/IR/LLVMContext.h" 34 #include "llvm/IR/PredIteratorCache.h" 35 #include "llvm/Support/Debug.h" 36 using namespace llvm; 37 38 #define DEBUG_TYPE "memdep" 39 40 STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses"); 41 STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses"); 42 STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses"); 43 44 STATISTIC(NumCacheNonLocalPtr, 45 "Number of fully cached non-local ptr responses"); 46 STATISTIC(NumCacheDirtyNonLocalPtr, 47 "Number of cached, but dirty, non-local ptr responses"); 48 STATISTIC(NumUncacheNonLocalPtr, 49 "Number of uncached non-local ptr responses"); 50 STATISTIC(NumCacheCompleteNonLocalPtr, 51 "Number of block queries that were completely cached"); 52 53 // Limit for the number of instructions to scan in a block. 54 55 static cl::opt<unsigned> BlockScanLimit( 56 "memdep-block-scan-limit", cl::Hidden, cl::init(100), 57 cl::desc("The number of instructions to scan in a block in memory " 58 "dependency analysis (default = 100)")); 59 60 // Limit on the number of memdep results to process. 61 static const unsigned int NumResultsLimit = 100; 62 63 char MemoryDependenceAnalysis::ID = 0; 64 65 // Register this pass... 66 INITIALIZE_PASS_BEGIN(MemoryDependenceAnalysis, "memdep", 67 "Memory Dependence Analysis", false, true) 68 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 69 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 70 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 71 INITIALIZE_PASS_END(MemoryDependenceAnalysis, "memdep", 72 "Memory Dependence Analysis", false, true) 73 74 MemoryDependenceAnalysis::MemoryDependenceAnalysis() 75 : FunctionPass(ID) { 76 initializeMemoryDependenceAnalysisPass(*PassRegistry::getPassRegistry()); 77 } 78 MemoryDependenceAnalysis::~MemoryDependenceAnalysis() { 79 } 80 81 /// Clean up memory in between runs 82 void MemoryDependenceAnalysis::releaseMemory() { 83 LocalDeps.clear(); 84 NonLocalDeps.clear(); 85 NonLocalPointerDeps.clear(); 86 ReverseLocalDeps.clear(); 87 ReverseNonLocalDeps.clear(); 88 ReverseNonLocalPtrDeps.clear(); 89 PredCache.clear(); 90 } 91 92 /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis. 93 /// 94 void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 95 AU.setPreservesAll(); 96 AU.addRequired<AssumptionCacheTracker>(); 97 AU.addRequiredTransitive<AAResultsWrapperPass>(); 98 AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>(); 99 } 100 101 bool MemoryDependenceAnalysis::runOnFunction(Function &F) { 102 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); 103 AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 104 DominatorTreeWrapperPass *DTWP = 105 getAnalysisIfAvailable<DominatorTreeWrapperPass>(); 106 DT = DTWP ? &DTWP->getDomTree() : nullptr; 107 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); 108 return false; 109 } 110 111 /// RemoveFromReverseMap - This is a helper function that removes Val from 112 /// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry. 113 template <typename KeyTy> 114 static void RemoveFromReverseMap(DenseMap<Instruction*, 115 SmallPtrSet<KeyTy, 4> > &ReverseMap, 116 Instruction *Inst, KeyTy Val) { 117 typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator 118 InstIt = ReverseMap.find(Inst); 119 assert(InstIt != ReverseMap.end() && "Reverse map out of sync?"); 120 bool Found = InstIt->second.erase(Val); 121 assert(Found && "Invalid reverse map!"); (void)Found; 122 if (InstIt->second.empty()) 123 ReverseMap.erase(InstIt); 124 } 125 126 /// GetLocation - If the given instruction references a specific memory 127 /// location, fill in Loc with the details, otherwise set Loc.Ptr to null. 128 /// Return a ModRefInfo value describing the general behavior of the 129 /// instruction. 130 static ModRefInfo GetLocation(const Instruction *Inst, MemoryLocation &Loc, 131 const TargetLibraryInfo &TLI) { 132 if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) { 133 if (LI->isUnordered()) { 134 Loc = MemoryLocation::get(LI); 135 return MRI_Ref; 136 } 137 if (LI->getOrdering() == Monotonic) { 138 Loc = MemoryLocation::get(LI); 139 return MRI_ModRef; 140 } 141 Loc = MemoryLocation(); 142 return MRI_ModRef; 143 } 144 145 if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 146 if (SI->isUnordered()) { 147 Loc = MemoryLocation::get(SI); 148 return MRI_Mod; 149 } 150 if (SI->getOrdering() == Monotonic) { 151 Loc = MemoryLocation::get(SI); 152 return MRI_ModRef; 153 } 154 Loc = MemoryLocation(); 155 return MRI_ModRef; 156 } 157 158 if (const VAArgInst *V = dyn_cast<VAArgInst>(Inst)) { 159 Loc = MemoryLocation::get(V); 160 return MRI_ModRef; 161 } 162 163 if (const CallInst *CI = isFreeCall(Inst, &TLI)) { 164 // calls to free() deallocate the entire structure 165 Loc = MemoryLocation(CI->getArgOperand(0)); 166 return MRI_Mod; 167 } 168 169 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { 170 AAMDNodes AAInfo; 171 172 switch (II->getIntrinsicID()) { 173 case Intrinsic::lifetime_start: 174 case Intrinsic::lifetime_end: 175 case Intrinsic::invariant_start: 176 II->getAAMetadata(AAInfo); 177 Loc = MemoryLocation( 178 II->getArgOperand(1), 179 cast<ConstantInt>(II->getArgOperand(0))->getZExtValue(), AAInfo); 180 // These intrinsics don't really modify the memory, but returning Mod 181 // will allow them to be handled conservatively. 182 return MRI_Mod; 183 case Intrinsic::invariant_end: 184 II->getAAMetadata(AAInfo); 185 Loc = MemoryLocation( 186 II->getArgOperand(2), 187 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue(), AAInfo); 188 // These intrinsics don't really modify the memory, but returning Mod 189 // will allow them to be handled conservatively. 190 return MRI_Mod; 191 default: 192 break; 193 } 194 } 195 196 // Otherwise, just do the coarse-grained thing that always works. 197 if (Inst->mayWriteToMemory()) 198 return MRI_ModRef; 199 if (Inst->mayReadFromMemory()) 200 return MRI_Ref; 201 return MRI_NoModRef; 202 } 203 204 /// getCallSiteDependencyFrom - Private helper for finding the local 205 /// dependencies of a call site. 206 MemDepResult MemoryDependenceAnalysis:: 207 getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall, 208 BasicBlock::iterator ScanIt, BasicBlock *BB) { 209 unsigned Limit = BlockScanLimit; 210 211 // Walk backwards through the block, looking for dependencies 212 while (ScanIt != BB->begin()) { 213 // Limit the amount of scanning we do so we don't end up with quadratic 214 // running time on extreme testcases. 215 --Limit; 216 if (!Limit) 217 return MemDepResult::getUnknown(); 218 219 Instruction *Inst = --ScanIt; 220 221 // If this inst is a memory op, get the pointer it accessed 222 MemoryLocation Loc; 223 ModRefInfo MR = GetLocation(Inst, Loc, *TLI); 224 if (Loc.Ptr) { 225 // A simple instruction. 226 if (AA->getModRefInfo(CS, Loc) != MRI_NoModRef) 227 return MemDepResult::getClobber(Inst); 228 continue; 229 } 230 231 if (auto InstCS = CallSite(Inst)) { 232 // Debug intrinsics don't cause dependences. 233 if (isa<DbgInfoIntrinsic>(Inst)) continue; 234 // If these two calls do not interfere, look past it. 235 switch (AA->getModRefInfo(CS, InstCS)) { 236 case MRI_NoModRef: 237 // If the two calls are the same, return InstCS as a Def, so that 238 // CS can be found redundant and eliminated. 239 if (isReadOnlyCall && !(MR & MRI_Mod) && 240 CS.getInstruction()->isIdenticalToWhenDefined(Inst)) 241 return MemDepResult::getDef(Inst); 242 243 // Otherwise if the two calls don't interact (e.g. InstCS is readnone) 244 // keep scanning. 245 continue; 246 default: 247 return MemDepResult::getClobber(Inst); 248 } 249 } 250 251 // If we could not obtain a pointer for the instruction and the instruction 252 // touches memory then assume that this is a dependency. 253 if (MR != MRI_NoModRef) 254 return MemDepResult::getClobber(Inst); 255 } 256 257 // No dependence found. If this is the entry block of the function, it is 258 // unknown, otherwise it is non-local. 259 if (BB != &BB->getParent()->getEntryBlock()) 260 return MemDepResult::getNonLocal(); 261 return MemDepResult::getNonFuncLocal(); 262 } 263 264 /// isLoadLoadClobberIfExtendedToFullWidth - Return true if LI is a load that 265 /// would fully overlap MemLoc if done as a wider legal integer load. 266 /// 267 /// MemLocBase, MemLocOffset are lazily computed here the first time the 268 /// base/offs of memloc is needed. 269 static bool isLoadLoadClobberIfExtendedToFullWidth(const MemoryLocation &MemLoc, 270 const Value *&MemLocBase, 271 int64_t &MemLocOffs, 272 const LoadInst *LI) { 273 const DataLayout &DL = LI->getModule()->getDataLayout(); 274 275 // If we haven't already computed the base/offset of MemLoc, do so now. 276 if (!MemLocBase) 277 MemLocBase = GetPointerBaseWithConstantOffset(MemLoc.Ptr, MemLocOffs, DL); 278 279 unsigned Size = MemoryDependenceAnalysis::getLoadLoadClobberFullWidthSize( 280 MemLocBase, MemLocOffs, MemLoc.Size, LI); 281 return Size != 0; 282 } 283 284 /// getLoadLoadClobberFullWidthSize - This is a little bit of analysis that 285 /// looks at a memory location for a load (specified by MemLocBase, Offs, 286 /// and Size) and compares it against a load. If the specified load could 287 /// be safely widened to a larger integer load that is 1) still efficient, 288 /// 2) safe for the target, and 3) would provide the specified memory 289 /// location value, then this function returns the size in bytes of the 290 /// load width to use. If not, this returns zero. 291 unsigned MemoryDependenceAnalysis::getLoadLoadClobberFullWidthSize( 292 const Value *MemLocBase, int64_t MemLocOffs, unsigned MemLocSize, 293 const LoadInst *LI) { 294 // We can only extend simple integer loads. 295 if (!isa<IntegerType>(LI->getType()) || !LI->isSimple()) return 0; 296 297 // Load widening is hostile to ThreadSanitizer: it may cause false positives 298 // or make the reports more cryptic (access sizes are wrong). 299 if (LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread)) 300 return 0; 301 302 const DataLayout &DL = LI->getModule()->getDataLayout(); 303 304 // Get the base of this load. 305 int64_t LIOffs = 0; 306 const Value *LIBase = 307 GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, DL); 308 309 // If the two pointers are not based on the same pointer, we can't tell that 310 // they are related. 311 if (LIBase != MemLocBase) return 0; 312 313 // Okay, the two values are based on the same pointer, but returned as 314 // no-alias. This happens when we have things like two byte loads at "P+1" 315 // and "P+3". Check to see if increasing the size of the "LI" load up to its 316 // alignment (or the largest native integer type) will allow us to load all 317 // the bits required by MemLoc. 318 319 // If MemLoc is before LI, then no widening of LI will help us out. 320 if (MemLocOffs < LIOffs) return 0; 321 322 // Get the alignment of the load in bytes. We assume that it is safe to load 323 // any legal integer up to this size without a problem. For example, if we're 324 // looking at an i8 load on x86-32 that is known 1024 byte aligned, we can 325 // widen it up to an i32 load. If it is known 2-byte aligned, we can widen it 326 // to i16. 327 unsigned LoadAlign = LI->getAlignment(); 328 329 int64_t MemLocEnd = MemLocOffs+MemLocSize; 330 331 // If no amount of rounding up will let MemLoc fit into LI, then bail out. 332 if (LIOffs+LoadAlign < MemLocEnd) return 0; 333 334 // This is the size of the load to try. Start with the next larger power of 335 // two. 336 unsigned NewLoadByteSize = LI->getType()->getPrimitiveSizeInBits()/8U; 337 NewLoadByteSize = NextPowerOf2(NewLoadByteSize); 338 339 while (1) { 340 // If this load size is bigger than our known alignment or would not fit 341 // into a native integer register, then we fail. 342 if (NewLoadByteSize > LoadAlign || 343 !DL.fitsInLegalInteger(NewLoadByteSize*8)) 344 return 0; 345 346 if (LIOffs + NewLoadByteSize > MemLocEnd && 347 LI->getParent()->getParent()->hasFnAttribute( 348 Attribute::SanitizeAddress)) 349 // We will be reading past the location accessed by the original program. 350 // While this is safe in a regular build, Address Safety analysis tools 351 // may start reporting false warnings. So, don't do widening. 352 return 0; 353 354 // If a load of this width would include all of MemLoc, then we succeed. 355 if (LIOffs+NewLoadByteSize >= MemLocEnd) 356 return NewLoadByteSize; 357 358 NewLoadByteSize <<= 1; 359 } 360 } 361 362 static bool isVolatile(Instruction *Inst) { 363 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) 364 return LI->isVolatile(); 365 else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) 366 return SI->isVolatile(); 367 else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) 368 return AI->isVolatile(); 369 return false; 370 } 371 372 373 /// getPointerDependencyFrom - Return the instruction on which a memory 374 /// location depends. If isLoad is true, this routine ignores may-aliases with 375 /// read-only operations. If isLoad is false, this routine ignores may-aliases 376 /// with reads from read-only locations. If possible, pass the query 377 /// instruction as well; this function may take advantage of the metadata 378 /// annotated to the query instruction to refine the result. 379 MemDepResult MemoryDependenceAnalysis::getPointerDependencyFrom( 380 const MemoryLocation &MemLoc, bool isLoad, BasicBlock::iterator ScanIt, 381 BasicBlock *BB, Instruction *QueryInst) { 382 383 const Value *MemLocBase = nullptr; 384 int64_t MemLocOffset = 0; 385 unsigned Limit = BlockScanLimit; 386 bool isInvariantLoad = false; 387 388 // We must be careful with atomic accesses, as they may allow another thread 389 // to touch this location, cloberring it. We are conservative: if the 390 // QueryInst is not a simple (non-atomic) memory access, we automatically 391 // return getClobber. 392 // If it is simple, we know based on the results of 393 // "Compiler testing via a theory of sound optimisations in the C11/C++11 394 // memory model" in PLDI 2013, that a non-atomic location can only be 395 // clobbered between a pair of a release and an acquire action, with no 396 // access to the location in between. 397 // Here is an example for giving the general intuition behind this rule. 398 // In the following code: 399 // store x 0; 400 // release action; [1] 401 // acquire action; [4] 402 // %val = load x; 403 // It is unsafe to replace %val by 0 because another thread may be running: 404 // acquire action; [2] 405 // store x 42; 406 // release action; [3] 407 // with synchronization from 1 to 2 and from 3 to 4, resulting in %val 408 // being 42. A key property of this program however is that if either 409 // 1 or 4 were missing, there would be a race between the store of 42 410 // either the store of 0 or the load (making the whole progam racy). 411 // The paper mentionned above shows that the same property is respected 412 // by every program that can detect any optimisation of that kind: either 413 // it is racy (undefined) or there is a release followed by an acquire 414 // between the pair of accesses under consideration. 415 416 // If the load is invariant, we "know" that it doesn't alias *any* write. We 417 // do want to respect mustalias results since defs are useful for value 418 // forwarding, but any mayalias write can be assumed to be noalias. 419 // Arguably, this logic should be pushed inside AliasAnalysis itself. 420 if (isLoad && QueryInst) { 421 LoadInst *LI = dyn_cast<LoadInst>(QueryInst); 422 if (LI && LI->getMetadata(LLVMContext::MD_invariant_load) != nullptr) 423 isInvariantLoad = true; 424 } 425 426 const DataLayout &DL = BB->getModule()->getDataLayout(); 427 428 // Create a numbered basic block to lazily compute and cache instruction 429 // positions inside a BB. This is used to provide fast queries for relative 430 // position between two instructions in a BB and can be used by 431 // AliasAnalysis::callCapturesBefore. 432 OrderedBasicBlock OBB(BB); 433 434 // Walk backwards through the basic block, looking for dependencies. 435 while (ScanIt != BB->begin()) { 436 Instruction *Inst = --ScanIt; 437 438 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) 439 // Debug intrinsics don't (and can't) cause dependencies. 440 if (isa<DbgInfoIntrinsic>(II)) continue; 441 442 // Limit the amount of scanning we do so we don't end up with quadratic 443 // running time on extreme testcases. 444 --Limit; 445 if (!Limit) 446 return MemDepResult::getUnknown(); 447 448 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { 449 // If we reach a lifetime begin or end marker, then the query ends here 450 // because the value is undefined. 451 if (II->getIntrinsicID() == Intrinsic::lifetime_start) { 452 // FIXME: This only considers queries directly on the invariant-tagged 453 // pointer, not on query pointers that are indexed off of them. It'd 454 // be nice to handle that at some point (the right approach is to use 455 // GetPointerBaseWithConstantOffset). 456 if (AA->isMustAlias(MemoryLocation(II->getArgOperand(1)), MemLoc)) 457 return MemDepResult::getDef(II); 458 continue; 459 } 460 } 461 462 // Values depend on loads if the pointers are must aliased. This means that 463 // a load depends on another must aliased load from the same value. 464 // One exception is atomic loads: a value can depend on an atomic load that it 465 // does not alias with when this atomic load indicates that another thread may 466 // be accessing the location. 467 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { 468 469 // While volatile access cannot be eliminated, they do not have to clobber 470 // non-aliasing locations, as normal accesses, for example, can be safely 471 // reordered with volatile accesses. 472 if (LI->isVolatile()) { 473 if (!QueryInst) 474 // Original QueryInst *may* be volatile 475 return MemDepResult::getClobber(LI); 476 if (isVolatile(QueryInst)) 477 // Ordering required if QueryInst is itself volatile 478 return MemDepResult::getClobber(LI); 479 // Otherwise, volatile doesn't imply any special ordering 480 } 481 482 // Atomic loads have complications involved. 483 // A Monotonic (or higher) load is OK if the query inst is itself not atomic. 484 // FIXME: This is overly conservative. 485 if (LI->isAtomic() && LI->getOrdering() > Unordered) { 486 if (!QueryInst) 487 return MemDepResult::getClobber(LI); 488 if (LI->getOrdering() != Monotonic) 489 return MemDepResult::getClobber(LI); 490 if (auto *QueryLI = dyn_cast<LoadInst>(QueryInst)) { 491 if (!QueryLI->isSimple()) 492 return MemDepResult::getClobber(LI); 493 } else if (auto *QuerySI = dyn_cast<StoreInst>(QueryInst)) { 494 if (!QuerySI->isSimple()) 495 return MemDepResult::getClobber(LI); 496 } else if (QueryInst->mayReadOrWriteMemory()) { 497 return MemDepResult::getClobber(LI); 498 } 499 } 500 501 MemoryLocation LoadLoc = MemoryLocation::get(LI); 502 503 // If we found a pointer, check if it could be the same as our pointer. 504 AliasResult R = AA->alias(LoadLoc, MemLoc); 505 506 if (isLoad) { 507 if (R == NoAlias) { 508 // If this is an over-aligned integer load (for example, 509 // "load i8* %P, align 4") see if it would obviously overlap with the 510 // queried location if widened to a larger load (e.g. if the queried 511 // location is 1 byte at P+1). If so, return it as a load/load 512 // clobber result, allowing the client to decide to widen the load if 513 // it wants to. 514 if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType())) { 515 if (LI->getAlignment() * 8 > ITy->getPrimitiveSizeInBits() && 516 isLoadLoadClobberIfExtendedToFullWidth(MemLoc, MemLocBase, 517 MemLocOffset, LI)) 518 return MemDepResult::getClobber(Inst); 519 } 520 continue; 521 } 522 523 // Must aliased loads are defs of each other. 524 if (R == MustAlias) 525 return MemDepResult::getDef(Inst); 526 527 #if 0 // FIXME: Temporarily disabled. GVN is cleverly rewriting loads 528 // in terms of clobbering loads, but since it does this by looking 529 // at the clobbering load directly, it doesn't know about any 530 // phi translation that may have happened along the way. 531 532 // If we have a partial alias, then return this as a clobber for the 533 // client to handle. 534 if (R == PartialAlias) 535 return MemDepResult::getClobber(Inst); 536 #endif 537 538 // Random may-alias loads don't depend on each other without a 539 // dependence. 540 continue; 541 } 542 543 // Stores don't depend on other no-aliased accesses. 544 if (R == NoAlias) 545 continue; 546 547 // Stores don't alias loads from read-only memory. 548 if (AA->pointsToConstantMemory(LoadLoc)) 549 continue; 550 551 // Stores depend on may/must aliased loads. 552 return MemDepResult::getDef(Inst); 553 } 554 555 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 556 // Atomic stores have complications involved. 557 // A Monotonic store is OK if the query inst is itself not atomic. 558 // FIXME: This is overly conservative. 559 if (!SI->isUnordered()) { 560 if (!QueryInst) 561 return MemDepResult::getClobber(SI); 562 if (SI->getOrdering() != Monotonic) 563 return MemDepResult::getClobber(SI); 564 if (auto *QueryLI = dyn_cast<LoadInst>(QueryInst)) { 565 if (!QueryLI->isSimple()) 566 return MemDepResult::getClobber(SI); 567 } else if (auto *QuerySI = dyn_cast<StoreInst>(QueryInst)) { 568 if (!QuerySI->isSimple()) 569 return MemDepResult::getClobber(SI); 570 } else if (QueryInst->mayReadOrWriteMemory()) { 571 return MemDepResult::getClobber(SI); 572 } 573 } 574 575 // FIXME: this is overly conservative. 576 // While volatile access cannot be eliminated, they do not have to clobber 577 // non-aliasing locations, as normal accesses can for example be reordered 578 // with volatile accesses. 579 if (SI->isVolatile()) 580 return MemDepResult::getClobber(SI); 581 582 // If alias analysis can tell that this store is guaranteed to not modify 583 // the query pointer, ignore it. Use getModRefInfo to handle cases where 584 // the query pointer points to constant memory etc. 585 if (AA->getModRefInfo(SI, MemLoc) == MRI_NoModRef) 586 continue; 587 588 // Ok, this store might clobber the query pointer. Check to see if it is 589 // a must alias: in this case, we want to return this as a def. 590 MemoryLocation StoreLoc = MemoryLocation::get(SI); 591 592 // If we found a pointer, check if it could be the same as our pointer. 593 AliasResult R = AA->alias(StoreLoc, MemLoc); 594 595 if (R == NoAlias) 596 continue; 597 if (R == MustAlias) 598 return MemDepResult::getDef(Inst); 599 if (isInvariantLoad) 600 continue; 601 return MemDepResult::getClobber(Inst); 602 } 603 604 // If this is an allocation, and if we know that the accessed pointer is to 605 // the allocation, return Def. This means that there is no dependence and 606 // the access can be optimized based on that. For example, a load could 607 // turn into undef. 608 // Note: Only determine this to be a malloc if Inst is the malloc call, not 609 // a subsequent bitcast of the malloc call result. There can be stores to 610 // the malloced memory between the malloc call and its bitcast uses, and we 611 // need to continue scanning until the malloc call. 612 if (isa<AllocaInst>(Inst) || isNoAliasFn(Inst, TLI)) { 613 const Value *AccessPtr = GetUnderlyingObject(MemLoc.Ptr, DL); 614 615 if (AccessPtr == Inst || AA->isMustAlias(Inst, AccessPtr)) 616 return MemDepResult::getDef(Inst); 617 if (isInvariantLoad) 618 continue; 619 // Be conservative if the accessed pointer may alias the allocation. 620 if (AA->alias(Inst, AccessPtr) != NoAlias) 621 return MemDepResult::getClobber(Inst); 622 // If the allocation is not aliased and does not read memory (like 623 // strdup), it is safe to ignore. 624 if (isa<AllocaInst>(Inst) || 625 isMallocLikeFn(Inst, TLI) || isCallocLikeFn(Inst, TLI)) 626 continue; 627 } 628 629 if (isInvariantLoad) 630 continue; 631 632 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer. 633 ModRefInfo MR = AA->getModRefInfo(Inst, MemLoc); 634 // If necessary, perform additional analysis. 635 if (MR == MRI_ModRef) 636 MR = AA->callCapturesBefore(Inst, MemLoc, DT, &OBB); 637 switch (MR) { 638 case MRI_NoModRef: 639 // If the call has no effect on the queried pointer, just ignore it. 640 continue; 641 case MRI_Mod: 642 return MemDepResult::getClobber(Inst); 643 case MRI_Ref: 644 // If the call is known to never store to the pointer, and if this is a 645 // load query, we can safely ignore it (scan past it). 646 if (isLoad) 647 continue; 648 default: 649 // Otherwise, there is a potential dependence. Return a clobber. 650 return MemDepResult::getClobber(Inst); 651 } 652 } 653 654 // No dependence found. If this is the entry block of the function, it is 655 // unknown, otherwise it is non-local. 656 if (BB != &BB->getParent()->getEntryBlock()) 657 return MemDepResult::getNonLocal(); 658 return MemDepResult::getNonFuncLocal(); 659 } 660 661 /// getDependency - Return the instruction on which a memory operation 662 /// depends. 663 MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) { 664 Instruction *ScanPos = QueryInst; 665 666 // Check for a cached result 667 MemDepResult &LocalCache = LocalDeps[QueryInst]; 668 669 // If the cached entry is non-dirty, just return it. Note that this depends 670 // on MemDepResult's default constructing to 'dirty'. 671 if (!LocalCache.isDirty()) 672 return LocalCache; 673 674 // Otherwise, if we have a dirty entry, we know we can start the scan at that 675 // instruction, which may save us some work. 676 if (Instruction *Inst = LocalCache.getInst()) { 677 ScanPos = Inst; 678 679 RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst); 680 } 681 682 BasicBlock *QueryParent = QueryInst->getParent(); 683 684 // Do the scan. 685 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) { 686 // No dependence found. If this is the entry block of the function, it is 687 // unknown, otherwise it is non-local. 688 if (QueryParent != &QueryParent->getParent()->getEntryBlock()) 689 LocalCache = MemDepResult::getNonLocal(); 690 else 691 LocalCache = MemDepResult::getNonFuncLocal(); 692 } else { 693 MemoryLocation MemLoc; 694 ModRefInfo MR = GetLocation(QueryInst, MemLoc, *TLI); 695 if (MemLoc.Ptr) { 696 // If we can do a pointer scan, make it happen. 697 bool isLoad = !(MR & MRI_Mod); 698 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(QueryInst)) 699 isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_start; 700 701 LocalCache = getPointerDependencyFrom(MemLoc, isLoad, ScanPos, 702 QueryParent, QueryInst); 703 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) { 704 CallSite QueryCS(QueryInst); 705 bool isReadOnly = AA->onlyReadsMemory(QueryCS); 706 LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos, 707 QueryParent); 708 } else 709 // Non-memory instruction. 710 LocalCache = MemDepResult::getUnknown(); 711 } 712 713 // Remember the result! 714 if (Instruction *I = LocalCache.getInst()) 715 ReverseLocalDeps[I].insert(QueryInst); 716 717 return LocalCache; 718 } 719 720 #ifndef NDEBUG 721 /// AssertSorted - This method is used when -debug is specified to verify that 722 /// cache arrays are properly kept sorted. 723 static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache, 724 int Count = -1) { 725 if (Count == -1) Count = Cache.size(); 726 if (Count == 0) return; 727 728 for (unsigned i = 1; i != unsigned(Count); ++i) 729 assert(!(Cache[i] < Cache[i-1]) && "Cache isn't sorted!"); 730 } 731 #endif 732 733 /// getNonLocalCallDependency - Perform a full dependency query for the 734 /// specified call, returning the set of blocks that the value is 735 /// potentially live across. The returned set of results will include a 736 /// "NonLocal" result for all blocks where the value is live across. 737 /// 738 /// This method assumes the instruction returns a "NonLocal" dependency 739 /// within its own block. 740 /// 741 /// This returns a reference to an internal data structure that may be 742 /// invalidated on the next non-local query or when an instruction is 743 /// removed. Clients must copy this data if they want it around longer than 744 /// that. 745 const MemoryDependenceAnalysis::NonLocalDepInfo & 746 MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) { 747 assert(getDependency(QueryCS.getInstruction()).isNonLocal() && 748 "getNonLocalCallDependency should only be used on calls with non-local deps!"); 749 PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()]; 750 NonLocalDepInfo &Cache = CacheP.first; 751 752 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In 753 /// the cached case, this can happen due to instructions being deleted etc. In 754 /// the uncached case, this starts out as the set of predecessors we care 755 /// about. 756 SmallVector<BasicBlock*, 32> DirtyBlocks; 757 758 if (!Cache.empty()) { 759 // Okay, we have a cache entry. If we know it is not dirty, just return it 760 // with no computation. 761 if (!CacheP.second) { 762 ++NumCacheNonLocal; 763 return Cache; 764 } 765 766 // If we already have a partially computed set of results, scan them to 767 // determine what is dirty, seeding our initial DirtyBlocks worklist. 768 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end(); 769 I != E; ++I) 770 if (I->getResult().isDirty()) 771 DirtyBlocks.push_back(I->getBB()); 772 773 // Sort the cache so that we can do fast binary search lookups below. 774 std::sort(Cache.begin(), Cache.end()); 775 776 ++NumCacheDirtyNonLocal; 777 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: " 778 // << Cache.size() << " cached: " << *QueryInst; 779 } else { 780 // Seed DirtyBlocks with each of the preds of QueryInst's block. 781 BasicBlock *QueryBB = QueryCS.getInstruction()->getParent(); 782 for (BasicBlock *Pred : PredCache.get(QueryBB)) 783 DirtyBlocks.push_back(Pred); 784 ++NumUncacheNonLocal; 785 } 786 787 // isReadonlyCall - If this is a read-only call, we can be more aggressive. 788 bool isReadonlyCall = AA->onlyReadsMemory(QueryCS); 789 790 SmallPtrSet<BasicBlock*, 64> Visited; 791 792 unsigned NumSortedEntries = Cache.size(); 793 DEBUG(AssertSorted(Cache)); 794 795 // Iterate while we still have blocks to update. 796 while (!DirtyBlocks.empty()) { 797 BasicBlock *DirtyBB = DirtyBlocks.back(); 798 DirtyBlocks.pop_back(); 799 800 // Already processed this block? 801 if (!Visited.insert(DirtyBB).second) 802 continue; 803 804 // Do a binary search to see if we already have an entry for this block in 805 // the cache set. If so, find it. 806 DEBUG(AssertSorted(Cache, NumSortedEntries)); 807 NonLocalDepInfo::iterator Entry = 808 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries, 809 NonLocalDepEntry(DirtyBB)); 810 if (Entry != Cache.begin() && std::prev(Entry)->getBB() == DirtyBB) 811 --Entry; 812 813 NonLocalDepEntry *ExistingResult = nullptr; 814 if (Entry != Cache.begin()+NumSortedEntries && 815 Entry->getBB() == DirtyBB) { 816 // If we already have an entry, and if it isn't already dirty, the block 817 // is done. 818 if (!Entry->getResult().isDirty()) 819 continue; 820 821 // Otherwise, remember this slot so we can update the value. 822 ExistingResult = &*Entry; 823 } 824 825 // If the dirty entry has a pointer, start scanning from it so we don't have 826 // to rescan the entire block. 827 BasicBlock::iterator ScanPos = DirtyBB->end(); 828 if (ExistingResult) { 829 if (Instruction *Inst = ExistingResult->getResult().getInst()) { 830 ScanPos = Inst; 831 // We're removing QueryInst's use of Inst. 832 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, 833 QueryCS.getInstruction()); 834 } 835 } 836 837 // Find out if this block has a local dependency for QueryInst. 838 MemDepResult Dep; 839 840 if (ScanPos != DirtyBB->begin()) { 841 Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB); 842 } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) { 843 // No dependence found. If this is the entry block of the function, it is 844 // a clobber, otherwise it is unknown. 845 Dep = MemDepResult::getNonLocal(); 846 } else { 847 Dep = MemDepResult::getNonFuncLocal(); 848 } 849 850 // If we had a dirty entry for the block, update it. Otherwise, just add 851 // a new entry. 852 if (ExistingResult) 853 ExistingResult->setResult(Dep); 854 else 855 Cache.push_back(NonLocalDepEntry(DirtyBB, Dep)); 856 857 // If the block has a dependency (i.e. it isn't completely transparent to 858 // the value), remember the association! 859 if (!Dep.isNonLocal()) { 860 // Keep the ReverseNonLocalDeps map up to date so we can efficiently 861 // update this when we remove instructions. 862 if (Instruction *Inst = Dep.getInst()) 863 ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction()); 864 } else { 865 866 // If the block *is* completely transparent to the load, we need to check 867 // the predecessors of this block. Add them to our worklist. 868 for (BasicBlock *Pred : PredCache.get(DirtyBB)) 869 DirtyBlocks.push_back(Pred); 870 } 871 } 872 873 return Cache; 874 } 875 876 /// getNonLocalPointerDependency - Perform a full dependency query for an 877 /// access to the specified (non-volatile) memory location, returning the 878 /// set of instructions that either define or clobber the value. 879 /// 880 /// This method assumes the pointer has a "NonLocal" dependency within its 881 /// own block. 882 /// 883 void MemoryDependenceAnalysis:: 884 getNonLocalPointerDependency(Instruction *QueryInst, 885 SmallVectorImpl<NonLocalDepResult> &Result) { 886 const MemoryLocation Loc = MemoryLocation::get(QueryInst); 887 bool isLoad = isa<LoadInst>(QueryInst); 888 BasicBlock *FromBB = QueryInst->getParent(); 889 assert(FromBB); 890 891 assert(Loc.Ptr->getType()->isPointerTy() && 892 "Can't get pointer deps of a non-pointer!"); 893 Result.clear(); 894 895 // This routine does not expect to deal with volatile instructions. 896 // Doing so would require piping through the QueryInst all the way through. 897 // TODO: volatiles can't be elided, but they can be reordered with other 898 // non-volatile accesses. 899 900 // We currently give up on any instruction which is ordered, but we do handle 901 // atomic instructions which are unordered. 902 // TODO: Handle ordered instructions 903 auto isOrdered = [](Instruction *Inst) { 904 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { 905 return !LI->isUnordered(); 906 } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 907 return !SI->isUnordered(); 908 } 909 return false; 910 }; 911 if (isVolatile(QueryInst) || isOrdered(QueryInst)) { 912 Result.push_back(NonLocalDepResult(FromBB, 913 MemDepResult::getUnknown(), 914 const_cast<Value *>(Loc.Ptr))); 915 return; 916 } 917 const DataLayout &DL = FromBB->getModule()->getDataLayout(); 918 PHITransAddr Address(const_cast<Value *>(Loc.Ptr), DL, AC); 919 920 // This is the set of blocks we've inspected, and the pointer we consider in 921 // each block. Because of critical edges, we currently bail out if querying 922 // a block with multiple different pointers. This can happen during PHI 923 // translation. 924 DenseMap<BasicBlock*, Value*> Visited; 925 if (!getNonLocalPointerDepFromBB(QueryInst, Address, Loc, isLoad, FromBB, 926 Result, Visited, true)) 927 return; 928 Result.clear(); 929 Result.push_back(NonLocalDepResult(FromBB, 930 MemDepResult::getUnknown(), 931 const_cast<Value *>(Loc.Ptr))); 932 } 933 934 /// GetNonLocalInfoForBlock - Compute the memdep value for BB with 935 /// Pointer/PointeeSize using either cached information in Cache or by doing a 936 /// lookup (which may use dirty cache info if available). If we do a lookup, 937 /// add the result to the cache. 938 MemDepResult MemoryDependenceAnalysis::GetNonLocalInfoForBlock( 939 Instruction *QueryInst, const MemoryLocation &Loc, bool isLoad, 940 BasicBlock *BB, NonLocalDepInfo *Cache, unsigned NumSortedEntries) { 941 942 // Do a binary search to see if we already have an entry for this block in 943 // the cache set. If so, find it. 944 NonLocalDepInfo::iterator Entry = 945 std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries, 946 NonLocalDepEntry(BB)); 947 if (Entry != Cache->begin() && (Entry-1)->getBB() == BB) 948 --Entry; 949 950 NonLocalDepEntry *ExistingResult = nullptr; 951 if (Entry != Cache->begin()+NumSortedEntries && Entry->getBB() == BB) 952 ExistingResult = &*Entry; 953 954 // If we have a cached entry, and it is non-dirty, use it as the value for 955 // this dependency. 956 if (ExistingResult && !ExistingResult->getResult().isDirty()) { 957 ++NumCacheNonLocalPtr; 958 return ExistingResult->getResult(); 959 } 960 961 // Otherwise, we have to scan for the value. If we have a dirty cache 962 // entry, start scanning from its position, otherwise we scan from the end 963 // of the block. 964 BasicBlock::iterator ScanPos = BB->end(); 965 if (ExistingResult && ExistingResult->getResult().getInst()) { 966 assert(ExistingResult->getResult().getInst()->getParent() == BB && 967 "Instruction invalidated?"); 968 ++NumCacheDirtyNonLocalPtr; 969 ScanPos = ExistingResult->getResult().getInst(); 970 971 // Eliminating the dirty entry from 'Cache', so update the reverse info. 972 ValueIsLoadPair CacheKey(Loc.Ptr, isLoad); 973 RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey); 974 } else { 975 ++NumUncacheNonLocalPtr; 976 } 977 978 // Scan the block for the dependency. 979 MemDepResult Dep = getPointerDependencyFrom(Loc, isLoad, ScanPos, BB, 980 QueryInst); 981 982 // If we had a dirty entry for the block, update it. Otherwise, just add 983 // a new entry. 984 if (ExistingResult) 985 ExistingResult->setResult(Dep); 986 else 987 Cache->push_back(NonLocalDepEntry(BB, Dep)); 988 989 // If the block has a dependency (i.e. it isn't completely transparent to 990 // the value), remember the reverse association because we just added it 991 // to Cache! 992 if (!Dep.isDef() && !Dep.isClobber()) 993 return Dep; 994 995 // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently 996 // update MemDep when we remove instructions. 997 Instruction *Inst = Dep.getInst(); 998 assert(Inst && "Didn't depend on anything?"); 999 ValueIsLoadPair CacheKey(Loc.Ptr, isLoad); 1000 ReverseNonLocalPtrDeps[Inst].insert(CacheKey); 1001 return Dep; 1002 } 1003 1004 /// SortNonLocalDepInfoCache - Sort the NonLocalDepInfo cache, given a certain 1005 /// number of elements in the array that are already properly ordered. This is 1006 /// optimized for the case when only a few entries are added. 1007 static void 1008 SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache, 1009 unsigned NumSortedEntries) { 1010 switch (Cache.size() - NumSortedEntries) { 1011 case 0: 1012 // done, no new entries. 1013 break; 1014 case 2: { 1015 // Two new entries, insert the last one into place. 1016 NonLocalDepEntry Val = Cache.back(); 1017 Cache.pop_back(); 1018 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry = 1019 std::upper_bound(Cache.begin(), Cache.end()-1, Val); 1020 Cache.insert(Entry, Val); 1021 // FALL THROUGH. 1022 } 1023 case 1: 1024 // One new entry, Just insert the new value at the appropriate position. 1025 if (Cache.size() != 1) { 1026 NonLocalDepEntry Val = Cache.back(); 1027 Cache.pop_back(); 1028 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry = 1029 std::upper_bound(Cache.begin(), Cache.end(), Val); 1030 Cache.insert(Entry, Val); 1031 } 1032 break; 1033 default: 1034 // Added many values, do a full scale sort. 1035 std::sort(Cache.begin(), Cache.end()); 1036 break; 1037 } 1038 } 1039 1040 /// getNonLocalPointerDepFromBB - Perform a dependency query based on 1041 /// pointer/pointeesize starting at the end of StartBB. Add any clobber/def 1042 /// results to the results vector and keep track of which blocks are visited in 1043 /// 'Visited'. 1044 /// 1045 /// This has special behavior for the first block queries (when SkipFirstBlock 1046 /// is true). In this special case, it ignores the contents of the specified 1047 /// block and starts returning dependence info for its predecessors. 1048 /// 1049 /// This function returns false on success, or true to indicate that it could 1050 /// not compute dependence information for some reason. This should be treated 1051 /// as a clobber dependence on the first instruction in the predecessor block. 1052 bool MemoryDependenceAnalysis::getNonLocalPointerDepFromBB( 1053 Instruction *QueryInst, const PHITransAddr &Pointer, 1054 const MemoryLocation &Loc, bool isLoad, BasicBlock *StartBB, 1055 SmallVectorImpl<NonLocalDepResult> &Result, 1056 DenseMap<BasicBlock *, Value *> &Visited, bool SkipFirstBlock) { 1057 // Look up the cached info for Pointer. 1058 ValueIsLoadPair CacheKey(Pointer.getAddr(), isLoad); 1059 1060 // Set up a temporary NLPI value. If the map doesn't yet have an entry for 1061 // CacheKey, this value will be inserted as the associated value. Otherwise, 1062 // it'll be ignored, and we'll have to check to see if the cached size and 1063 // aa tags are consistent with the current query. 1064 NonLocalPointerInfo InitialNLPI; 1065 InitialNLPI.Size = Loc.Size; 1066 InitialNLPI.AATags = Loc.AATags; 1067 1068 // Get the NLPI for CacheKey, inserting one into the map if it doesn't 1069 // already have one. 1070 std::pair<CachedNonLocalPointerInfo::iterator, bool> Pair = 1071 NonLocalPointerDeps.insert(std::make_pair(CacheKey, InitialNLPI)); 1072 NonLocalPointerInfo *CacheInfo = &Pair.first->second; 1073 1074 // If we already have a cache entry for this CacheKey, we may need to do some 1075 // work to reconcile the cache entry and the current query. 1076 if (!Pair.second) { 1077 if (CacheInfo->Size < Loc.Size) { 1078 // The query's Size is greater than the cached one. Throw out the 1079 // cached data and proceed with the query at the greater size. 1080 CacheInfo->Pair = BBSkipFirstBlockPair(); 1081 CacheInfo->Size = Loc.Size; 1082 for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(), 1083 DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI) 1084 if (Instruction *Inst = DI->getResult().getInst()) 1085 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey); 1086 CacheInfo->NonLocalDeps.clear(); 1087 } else if (CacheInfo->Size > Loc.Size) { 1088 // This query's Size is less than the cached one. Conservatively restart 1089 // the query using the greater size. 1090 return getNonLocalPointerDepFromBB(QueryInst, Pointer, 1091 Loc.getWithNewSize(CacheInfo->Size), 1092 isLoad, StartBB, Result, Visited, 1093 SkipFirstBlock); 1094 } 1095 1096 // If the query's AATags are inconsistent with the cached one, 1097 // conservatively throw out the cached data and restart the query with 1098 // no tag if needed. 1099 if (CacheInfo->AATags != Loc.AATags) { 1100 if (CacheInfo->AATags) { 1101 CacheInfo->Pair = BBSkipFirstBlockPair(); 1102 CacheInfo->AATags = AAMDNodes(); 1103 for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(), 1104 DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI) 1105 if (Instruction *Inst = DI->getResult().getInst()) 1106 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey); 1107 CacheInfo->NonLocalDeps.clear(); 1108 } 1109 if (Loc.AATags) 1110 return getNonLocalPointerDepFromBB(QueryInst, 1111 Pointer, Loc.getWithoutAATags(), 1112 isLoad, StartBB, Result, Visited, 1113 SkipFirstBlock); 1114 } 1115 } 1116 1117 NonLocalDepInfo *Cache = &CacheInfo->NonLocalDeps; 1118 1119 // If we have valid cached information for exactly the block we are 1120 // investigating, just return it with no recomputation. 1121 if (CacheInfo->Pair == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) { 1122 // We have a fully cached result for this query then we can just return the 1123 // cached results and populate the visited set. However, we have to verify 1124 // that we don't already have conflicting results for these blocks. Check 1125 // to ensure that if a block in the results set is in the visited set that 1126 // it was for the same pointer query. 1127 if (!Visited.empty()) { 1128 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end(); 1129 I != E; ++I) { 1130 DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->getBB()); 1131 if (VI == Visited.end() || VI->second == Pointer.getAddr()) 1132 continue; 1133 1134 // We have a pointer mismatch in a block. Just return clobber, saying 1135 // that something was clobbered in this result. We could also do a 1136 // non-fully cached query, but there is little point in doing this. 1137 return true; 1138 } 1139 } 1140 1141 Value *Addr = Pointer.getAddr(); 1142 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end(); 1143 I != E; ++I) { 1144 Visited.insert(std::make_pair(I->getBB(), Addr)); 1145 if (I->getResult().isNonLocal()) { 1146 continue; 1147 } 1148 1149 if (!DT) { 1150 Result.push_back(NonLocalDepResult(I->getBB(), 1151 MemDepResult::getUnknown(), 1152 Addr)); 1153 } else if (DT->isReachableFromEntry(I->getBB())) { 1154 Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(), Addr)); 1155 } 1156 } 1157 ++NumCacheCompleteNonLocalPtr; 1158 return false; 1159 } 1160 1161 // Otherwise, either this is a new block, a block with an invalid cache 1162 // pointer or one that we're about to invalidate by putting more info into it 1163 // than its valid cache info. If empty, the result will be valid cache info, 1164 // otherwise it isn't. 1165 if (Cache->empty()) 1166 CacheInfo->Pair = BBSkipFirstBlockPair(StartBB, SkipFirstBlock); 1167 else 1168 CacheInfo->Pair = BBSkipFirstBlockPair(); 1169 1170 SmallVector<BasicBlock*, 32> Worklist; 1171 Worklist.push_back(StartBB); 1172 1173 // PredList used inside loop. 1174 SmallVector<std::pair<BasicBlock*, PHITransAddr>, 16> PredList; 1175 1176 // Keep track of the entries that we know are sorted. Previously cached 1177 // entries will all be sorted. The entries we add we only sort on demand (we 1178 // don't insert every element into its sorted position). We know that we 1179 // won't get any reuse from currently inserted values, because we don't 1180 // revisit blocks after we insert info for them. 1181 unsigned NumSortedEntries = Cache->size(); 1182 DEBUG(AssertSorted(*Cache)); 1183 1184 while (!Worklist.empty()) { 1185 BasicBlock *BB = Worklist.pop_back_val(); 1186 1187 // If we do process a large number of blocks it becomes very expensive and 1188 // likely it isn't worth worrying about 1189 if (Result.size() > NumResultsLimit) { 1190 Worklist.clear(); 1191 // Sort it now (if needed) so that recursive invocations of 1192 // getNonLocalPointerDepFromBB and other routines that could reuse the 1193 // cache value will only see properly sorted cache arrays. 1194 if (Cache && NumSortedEntries != Cache->size()) { 1195 SortNonLocalDepInfoCache(*Cache, NumSortedEntries); 1196 } 1197 // Since we bail out, the "Cache" set won't contain all of the 1198 // results for the query. This is ok (we can still use it to accelerate 1199 // specific block queries) but we can't do the fastpath "return all 1200 // results from the set". Clear out the indicator for this. 1201 CacheInfo->Pair = BBSkipFirstBlockPair(); 1202 return true; 1203 } 1204 1205 // Skip the first block if we have it. 1206 if (!SkipFirstBlock) { 1207 // Analyze the dependency of *Pointer in FromBB. See if we already have 1208 // been here. 1209 assert(Visited.count(BB) && "Should check 'visited' before adding to WL"); 1210 1211 // Get the dependency info for Pointer in BB. If we have cached 1212 // information, we will use it, otherwise we compute it. 1213 DEBUG(AssertSorted(*Cache, NumSortedEntries)); 1214 MemDepResult Dep = GetNonLocalInfoForBlock(QueryInst, 1215 Loc, isLoad, BB, Cache, 1216 NumSortedEntries); 1217 1218 // If we got a Def or Clobber, add this to the list of results. 1219 if (!Dep.isNonLocal()) { 1220 if (!DT) { 1221 Result.push_back(NonLocalDepResult(BB, 1222 MemDepResult::getUnknown(), 1223 Pointer.getAddr())); 1224 continue; 1225 } else if (DT->isReachableFromEntry(BB)) { 1226 Result.push_back(NonLocalDepResult(BB, Dep, Pointer.getAddr())); 1227 continue; 1228 } 1229 } 1230 } 1231 1232 // If 'Pointer' is an instruction defined in this block, then we need to do 1233 // phi translation to change it into a value live in the predecessor block. 1234 // If not, we just add the predecessors to the worklist and scan them with 1235 // the same Pointer. 1236 if (!Pointer.NeedsPHITranslationFromBlock(BB)) { 1237 SkipFirstBlock = false; 1238 SmallVector<BasicBlock*, 16> NewBlocks; 1239 for (BasicBlock *Pred : PredCache.get(BB)) { 1240 // Verify that we haven't looked at this block yet. 1241 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool> 1242 InsertRes = Visited.insert(std::make_pair(Pred, Pointer.getAddr())); 1243 if (InsertRes.second) { 1244 // First time we've looked at *PI. 1245 NewBlocks.push_back(Pred); 1246 continue; 1247 } 1248 1249 // If we have seen this block before, but it was with a different 1250 // pointer then we have a phi translation failure and we have to treat 1251 // this as a clobber. 1252 if (InsertRes.first->second != Pointer.getAddr()) { 1253 // Make sure to clean up the Visited map before continuing on to 1254 // PredTranslationFailure. 1255 for (unsigned i = 0; i < NewBlocks.size(); i++) 1256 Visited.erase(NewBlocks[i]); 1257 goto PredTranslationFailure; 1258 } 1259 } 1260 Worklist.append(NewBlocks.begin(), NewBlocks.end()); 1261 continue; 1262 } 1263 1264 // We do need to do phi translation, if we know ahead of time we can't phi 1265 // translate this value, don't even try. 1266 if (!Pointer.IsPotentiallyPHITranslatable()) 1267 goto PredTranslationFailure; 1268 1269 // We may have added values to the cache list before this PHI translation. 1270 // If so, we haven't done anything to ensure that the cache remains sorted. 1271 // Sort it now (if needed) so that recursive invocations of 1272 // getNonLocalPointerDepFromBB and other routines that could reuse the cache 1273 // value will only see properly sorted cache arrays. 1274 if (Cache && NumSortedEntries != Cache->size()) { 1275 SortNonLocalDepInfoCache(*Cache, NumSortedEntries); 1276 NumSortedEntries = Cache->size(); 1277 } 1278 Cache = nullptr; 1279 1280 PredList.clear(); 1281 for (BasicBlock *Pred : PredCache.get(BB)) { 1282 PredList.push_back(std::make_pair(Pred, Pointer)); 1283 1284 // Get the PHI translated pointer in this predecessor. This can fail if 1285 // not translatable, in which case the getAddr() returns null. 1286 PHITransAddr &PredPointer = PredList.back().second; 1287 PredPointer.PHITranslateValue(BB, Pred, DT, /*MustDominate=*/false); 1288 Value *PredPtrVal = PredPointer.getAddr(); 1289 1290 // Check to see if we have already visited this pred block with another 1291 // pointer. If so, we can't do this lookup. This failure can occur 1292 // with PHI translation when a critical edge exists and the PHI node in 1293 // the successor translates to a pointer value different than the 1294 // pointer the block was first analyzed with. 1295 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool> 1296 InsertRes = Visited.insert(std::make_pair(Pred, PredPtrVal)); 1297 1298 if (!InsertRes.second) { 1299 // We found the pred; take it off the list of preds to visit. 1300 PredList.pop_back(); 1301 1302 // If the predecessor was visited with PredPtr, then we already did 1303 // the analysis and can ignore it. 1304 if (InsertRes.first->second == PredPtrVal) 1305 continue; 1306 1307 // Otherwise, the block was previously analyzed with a different 1308 // pointer. We can't represent the result of this case, so we just 1309 // treat this as a phi translation failure. 1310 1311 // Make sure to clean up the Visited map before continuing on to 1312 // PredTranslationFailure. 1313 for (unsigned i = 0, n = PredList.size(); i < n; ++i) 1314 Visited.erase(PredList[i].first); 1315 1316 goto PredTranslationFailure; 1317 } 1318 } 1319 1320 // Actually process results here; this need to be a separate loop to avoid 1321 // calling getNonLocalPointerDepFromBB for blocks we don't want to return 1322 // any results for. (getNonLocalPointerDepFromBB will modify our 1323 // datastructures in ways the code after the PredTranslationFailure label 1324 // doesn't expect.) 1325 for (unsigned i = 0, n = PredList.size(); i < n; ++i) { 1326 BasicBlock *Pred = PredList[i].first; 1327 PHITransAddr &PredPointer = PredList[i].second; 1328 Value *PredPtrVal = PredPointer.getAddr(); 1329 1330 bool CanTranslate = true; 1331 // If PHI translation was unable to find an available pointer in this 1332 // predecessor, then we have to assume that the pointer is clobbered in 1333 // that predecessor. We can still do PRE of the load, which would insert 1334 // a computation of the pointer in this predecessor. 1335 if (!PredPtrVal) 1336 CanTranslate = false; 1337 1338 // FIXME: it is entirely possible that PHI translating will end up with 1339 // the same value. Consider PHI translating something like: 1340 // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need* 1341 // to recurse here, pedantically speaking. 1342 1343 // If getNonLocalPointerDepFromBB fails here, that means the cached 1344 // result conflicted with the Visited list; we have to conservatively 1345 // assume it is unknown, but this also does not block PRE of the load. 1346 if (!CanTranslate || 1347 getNonLocalPointerDepFromBB(QueryInst, PredPointer, 1348 Loc.getWithNewPtr(PredPtrVal), 1349 isLoad, Pred, 1350 Result, Visited)) { 1351 // Add the entry to the Result list. 1352 NonLocalDepResult Entry(Pred, MemDepResult::getUnknown(), PredPtrVal); 1353 Result.push_back(Entry); 1354 1355 // Since we had a phi translation failure, the cache for CacheKey won't 1356 // include all of the entries that we need to immediately satisfy future 1357 // queries. Mark this in NonLocalPointerDeps by setting the 1358 // BBSkipFirstBlockPair pointer to null. This requires reuse of the 1359 // cached value to do more work but not miss the phi trans failure. 1360 NonLocalPointerInfo &NLPI = NonLocalPointerDeps[CacheKey]; 1361 NLPI.Pair = BBSkipFirstBlockPair(); 1362 continue; 1363 } 1364 } 1365 1366 // Refresh the CacheInfo/Cache pointer so that it isn't invalidated. 1367 CacheInfo = &NonLocalPointerDeps[CacheKey]; 1368 Cache = &CacheInfo->NonLocalDeps; 1369 NumSortedEntries = Cache->size(); 1370 1371 // Since we did phi translation, the "Cache" set won't contain all of the 1372 // results for the query. This is ok (we can still use it to accelerate 1373 // specific block queries) but we can't do the fastpath "return all 1374 // results from the set" Clear out the indicator for this. 1375 CacheInfo->Pair = BBSkipFirstBlockPair(); 1376 SkipFirstBlock = false; 1377 continue; 1378 1379 PredTranslationFailure: 1380 // The following code is "failure"; we can't produce a sane translation 1381 // for the given block. It assumes that we haven't modified any of 1382 // our datastructures while processing the current block. 1383 1384 if (!Cache) { 1385 // Refresh the CacheInfo/Cache pointer if it got invalidated. 1386 CacheInfo = &NonLocalPointerDeps[CacheKey]; 1387 Cache = &CacheInfo->NonLocalDeps; 1388 NumSortedEntries = Cache->size(); 1389 } 1390 1391 // Since we failed phi translation, the "Cache" set won't contain all of the 1392 // results for the query. This is ok (we can still use it to accelerate 1393 // specific block queries) but we can't do the fastpath "return all 1394 // results from the set". Clear out the indicator for this. 1395 CacheInfo->Pair = BBSkipFirstBlockPair(); 1396 1397 // If *nothing* works, mark the pointer as unknown. 1398 // 1399 // If this is the magic first block, return this as a clobber of the whole 1400 // incoming value. Since we can't phi translate to one of the predecessors, 1401 // we have to bail out. 1402 if (SkipFirstBlock) 1403 return true; 1404 1405 for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) { 1406 assert(I != Cache->rend() && "Didn't find current block??"); 1407 if (I->getBB() != BB) 1408 continue; 1409 1410 assert((I->getResult().isNonLocal() || !DT->isReachableFromEntry(BB)) && 1411 "Should only be here with transparent block"); 1412 I->setResult(MemDepResult::getUnknown()); 1413 Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(), 1414 Pointer.getAddr())); 1415 break; 1416 } 1417 } 1418 1419 // Okay, we're done now. If we added new values to the cache, re-sort it. 1420 SortNonLocalDepInfoCache(*Cache, NumSortedEntries); 1421 DEBUG(AssertSorted(*Cache)); 1422 return false; 1423 } 1424 1425 /// RemoveCachedNonLocalPointerDependencies - If P exists in 1426 /// CachedNonLocalPointerInfo, remove it. 1427 void MemoryDependenceAnalysis:: 1428 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) { 1429 CachedNonLocalPointerInfo::iterator It = 1430 NonLocalPointerDeps.find(P); 1431 if (It == NonLocalPointerDeps.end()) return; 1432 1433 // Remove all of the entries in the BB->val map. This involves removing 1434 // instructions from the reverse map. 1435 NonLocalDepInfo &PInfo = It->second.NonLocalDeps; 1436 1437 for (unsigned i = 0, e = PInfo.size(); i != e; ++i) { 1438 Instruction *Target = PInfo[i].getResult().getInst(); 1439 if (!Target) continue; // Ignore non-local dep results. 1440 assert(Target->getParent() == PInfo[i].getBB()); 1441 1442 // Eliminating the dirty entry from 'Cache', so update the reverse info. 1443 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P); 1444 } 1445 1446 // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo). 1447 NonLocalPointerDeps.erase(It); 1448 } 1449 1450 1451 /// invalidateCachedPointerInfo - This method is used to invalidate cached 1452 /// information about the specified pointer, because it may be too 1453 /// conservative in memdep. This is an optional call that can be used when 1454 /// the client detects an equivalence between the pointer and some other 1455 /// value and replaces the other value with ptr. This can make Ptr available 1456 /// in more places that cached info does not necessarily keep. 1457 void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) { 1458 // If Ptr isn't really a pointer, just ignore it. 1459 if (!Ptr->getType()->isPointerTy()) return; 1460 // Flush store info for the pointer. 1461 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false)); 1462 // Flush load info for the pointer. 1463 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true)); 1464 } 1465 1466 /// invalidateCachedPredecessors - Clear the PredIteratorCache info. 1467 /// This needs to be done when the CFG changes, e.g., due to splitting 1468 /// critical edges. 1469 void MemoryDependenceAnalysis::invalidateCachedPredecessors() { 1470 PredCache.clear(); 1471 } 1472 1473 /// removeInstruction - Remove an instruction from the dependence analysis, 1474 /// updating the dependence of instructions that previously depended on it. 1475 /// This method attempts to keep the cache coherent using the reverse map. 1476 void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) { 1477 // Walk through the Non-local dependencies, removing this one as the value 1478 // for any cached queries. 1479 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst); 1480 if (NLDI != NonLocalDeps.end()) { 1481 NonLocalDepInfo &BlockMap = NLDI->second.first; 1482 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end(); 1483 DI != DE; ++DI) 1484 if (Instruction *Inst = DI->getResult().getInst()) 1485 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst); 1486 NonLocalDeps.erase(NLDI); 1487 } 1488 1489 // If we have a cached local dependence query for this instruction, remove it. 1490 // 1491 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst); 1492 if (LocalDepEntry != LocalDeps.end()) { 1493 // Remove us from DepInst's reverse set now that the local dep info is gone. 1494 if (Instruction *Inst = LocalDepEntry->second.getInst()) 1495 RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst); 1496 1497 // Remove this local dependency info. 1498 LocalDeps.erase(LocalDepEntry); 1499 } 1500 1501 // If we have any cached pointer dependencies on this instruction, remove 1502 // them. If the instruction has non-pointer type, then it can't be a pointer 1503 // base. 1504 1505 // Remove it from both the load info and the store info. The instruction 1506 // can't be in either of these maps if it is non-pointer. 1507 if (RemInst->getType()->isPointerTy()) { 1508 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false)); 1509 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true)); 1510 } 1511 1512 // Loop over all of the things that depend on the instruction we're removing. 1513 // 1514 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd; 1515 1516 // If we find RemInst as a clobber or Def in any of the maps for other values, 1517 // we need to replace its entry with a dirty version of the instruction after 1518 // it. If RemInst is a terminator, we use a null dirty value. 1519 // 1520 // Using a dirty version of the instruction after RemInst saves having to scan 1521 // the entire block to get to this point. 1522 MemDepResult NewDirtyVal; 1523 if (!RemInst->isTerminator()) 1524 NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst)); 1525 1526 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst); 1527 if (ReverseDepIt != ReverseLocalDeps.end()) { 1528 // RemInst can't be the terminator if it has local stuff depending on it. 1529 assert(!ReverseDepIt->second.empty() && !isa<TerminatorInst>(RemInst) && 1530 "Nothing can locally depend on a terminator"); 1531 1532 for (Instruction *InstDependingOnRemInst : ReverseDepIt->second) { 1533 assert(InstDependingOnRemInst != RemInst && 1534 "Already removed our local dep info"); 1535 1536 LocalDeps[InstDependingOnRemInst] = NewDirtyVal; 1537 1538 // Make sure to remember that new things depend on NewDepInst. 1539 assert(NewDirtyVal.getInst() && "There is no way something else can have " 1540 "a local dep on this if it is a terminator!"); 1541 ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(), 1542 InstDependingOnRemInst)); 1543 } 1544 1545 ReverseLocalDeps.erase(ReverseDepIt); 1546 1547 // Add new reverse deps after scanning the set, to avoid invalidating the 1548 // 'ReverseDeps' reference. 1549 while (!ReverseDepsToAdd.empty()) { 1550 ReverseLocalDeps[ReverseDepsToAdd.back().first] 1551 .insert(ReverseDepsToAdd.back().second); 1552 ReverseDepsToAdd.pop_back(); 1553 } 1554 } 1555 1556 ReverseDepIt = ReverseNonLocalDeps.find(RemInst); 1557 if (ReverseDepIt != ReverseNonLocalDeps.end()) { 1558 for (Instruction *I : ReverseDepIt->second) { 1559 assert(I != RemInst && "Already removed NonLocalDep info for RemInst"); 1560 1561 PerInstNLInfo &INLD = NonLocalDeps[I]; 1562 // The information is now dirty! 1563 INLD.second = true; 1564 1565 for (NonLocalDepInfo::iterator DI = INLD.first.begin(), 1566 DE = INLD.first.end(); DI != DE; ++DI) { 1567 if (DI->getResult().getInst() != RemInst) continue; 1568 1569 // Convert to a dirty entry for the subsequent instruction. 1570 DI->setResult(NewDirtyVal); 1571 1572 if (Instruction *NextI = NewDirtyVal.getInst()) 1573 ReverseDepsToAdd.push_back(std::make_pair(NextI, I)); 1574 } 1575 } 1576 1577 ReverseNonLocalDeps.erase(ReverseDepIt); 1578 1579 // Add new reverse deps after scanning the set, to avoid invalidating 'Set' 1580 while (!ReverseDepsToAdd.empty()) { 1581 ReverseNonLocalDeps[ReverseDepsToAdd.back().first] 1582 .insert(ReverseDepsToAdd.back().second); 1583 ReverseDepsToAdd.pop_back(); 1584 } 1585 } 1586 1587 // If the instruction is in ReverseNonLocalPtrDeps then it appears as a 1588 // value in the NonLocalPointerDeps info. 1589 ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt = 1590 ReverseNonLocalPtrDeps.find(RemInst); 1591 if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) { 1592 SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd; 1593 1594 for (ValueIsLoadPair P : ReversePtrDepIt->second) { 1595 assert(P.getPointer() != RemInst && 1596 "Already removed NonLocalPointerDeps info for RemInst"); 1597 1598 NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].NonLocalDeps; 1599 1600 // The cache is not valid for any specific block anymore. 1601 NonLocalPointerDeps[P].Pair = BBSkipFirstBlockPair(); 1602 1603 // Update any entries for RemInst to use the instruction after it. 1604 for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end(); 1605 DI != DE; ++DI) { 1606 if (DI->getResult().getInst() != RemInst) continue; 1607 1608 // Convert to a dirty entry for the subsequent instruction. 1609 DI->setResult(NewDirtyVal); 1610 1611 if (Instruction *NewDirtyInst = NewDirtyVal.getInst()) 1612 ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P)); 1613 } 1614 1615 // Re-sort the NonLocalDepInfo. Changing the dirty entry to its 1616 // subsequent value may invalidate the sortedness. 1617 std::sort(NLPDI.begin(), NLPDI.end()); 1618 } 1619 1620 ReverseNonLocalPtrDeps.erase(ReversePtrDepIt); 1621 1622 while (!ReversePtrDepsToAdd.empty()) { 1623 ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first] 1624 .insert(ReversePtrDepsToAdd.back().second); 1625 ReversePtrDepsToAdd.pop_back(); 1626 } 1627 } 1628 1629 1630 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?"); 1631 DEBUG(verifyRemoved(RemInst)); 1632 } 1633 /// verifyRemoved - Verify that the specified instruction does not occur 1634 /// in our internal data structures. This function verifies by asserting in 1635 /// debug builds. 1636 void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const { 1637 #ifndef NDEBUG 1638 for (LocalDepMapType::const_iterator I = LocalDeps.begin(), 1639 E = LocalDeps.end(); I != E; ++I) { 1640 assert(I->first != D && "Inst occurs in data structures"); 1641 assert(I->second.getInst() != D && 1642 "Inst occurs in data structures"); 1643 } 1644 1645 for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(), 1646 E = NonLocalPointerDeps.end(); I != E; ++I) { 1647 assert(I->first.getPointer() != D && "Inst occurs in NLPD map key"); 1648 const NonLocalDepInfo &Val = I->second.NonLocalDeps; 1649 for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end(); 1650 II != E; ++II) 1651 assert(II->getResult().getInst() != D && "Inst occurs as NLPD value"); 1652 } 1653 1654 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(), 1655 E = NonLocalDeps.end(); I != E; ++I) { 1656 assert(I->first != D && "Inst occurs in data structures"); 1657 const PerInstNLInfo &INLD = I->second; 1658 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(), 1659 EE = INLD.first.end(); II != EE; ++II) 1660 assert(II->getResult().getInst() != D && "Inst occurs in data structures"); 1661 } 1662 1663 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(), 1664 E = ReverseLocalDeps.end(); I != E; ++I) { 1665 assert(I->first != D && "Inst occurs in data structures"); 1666 for (Instruction *Inst : I->second) 1667 assert(Inst != D && "Inst occurs in data structures"); 1668 } 1669 1670 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(), 1671 E = ReverseNonLocalDeps.end(); 1672 I != E; ++I) { 1673 assert(I->first != D && "Inst occurs in data structures"); 1674 for (Instruction *Inst : I->second) 1675 assert(Inst != D && "Inst occurs in data structures"); 1676 } 1677 1678 for (ReverseNonLocalPtrDepTy::const_iterator 1679 I = ReverseNonLocalPtrDeps.begin(), 1680 E = ReverseNonLocalPtrDeps.end(); I != E; ++I) { 1681 assert(I->first != D && "Inst occurs in rev NLPD map"); 1682 1683 for (ValueIsLoadPair P : I->second) 1684 assert(P != ValueIsLoadPair(D, false) && 1685 P != ValueIsLoadPair(D, true) && 1686 "Inst occurs in ReverseNonLocalPtrDeps map"); 1687 } 1688 #endif 1689 } 1690