1f785676fSDimitry Andric //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation -------------===// 2f22ef01cSRoman Divacky // 3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure 4f22ef01cSRoman Divacky // 5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source 6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details. 7f22ef01cSRoman Divacky // 8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 9f22ef01cSRoman Divacky // 10f22ef01cSRoman Divacky // This file implements an analysis that determines, for a given memory 11f22ef01cSRoman Divacky // operation, what preceding memory operations it depends on. It builds on 12f22ef01cSRoman Divacky // alias analysis information, and tries to provide a lazy, caching interface to 13f22ef01cSRoman Divacky // a common kind of alias information query. 14f22ef01cSRoman Divacky // 15f22ef01cSRoman Divacky //===----------------------------------------------------------------------===// 16f22ef01cSRoman Divacky 17f22ef01cSRoman Divacky #include "llvm/Analysis/MemoryDependenceAnalysis.h" 18139f7f9bSDimitry Andric #include "llvm/ADT/STLExtras.h" 19139f7f9bSDimitry Andric #include "llvm/ADT/Statistic.h" 20f22ef01cSRoman Divacky #include "llvm/Analysis/AliasAnalysis.h" 2139d628a0SDimitry Andric #include "llvm/Analysis/AssumptionCache.h" 22f22ef01cSRoman Divacky #include "llvm/Analysis/InstructionSimplify.h" 23f22ef01cSRoman Divacky #include "llvm/Analysis/MemoryBuiltins.h" 24f22ef01cSRoman Divacky #include "llvm/Analysis/PHITransAddr.h" 252754fe60SDimitry Andric #include "llvm/Analysis/ValueTracking.h" 26139f7f9bSDimitry Andric #include "llvm/IR/DataLayout.h" 2791bc56edSDimitry Andric #include "llvm/IR/Dominators.h" 28139f7f9bSDimitry Andric #include "llvm/IR/Function.h" 29139f7f9bSDimitry Andric #include "llvm/IR/Instructions.h" 30139f7f9bSDimitry Andric #include "llvm/IR/IntrinsicInst.h" 31139f7f9bSDimitry Andric #include "llvm/IR/LLVMContext.h" 3291bc56edSDimitry Andric #include "llvm/IR/PredIteratorCache.h" 33f22ef01cSRoman Divacky #include "llvm/Support/Debug.h" 34f22ef01cSRoman Divacky using namespace llvm; 35f22ef01cSRoman Divacky 3691bc56edSDimitry Andric #define DEBUG_TYPE "memdep" 3791bc56edSDimitry Andric 38f22ef01cSRoman Divacky STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses"); 39f22ef01cSRoman Divacky STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses"); 40f22ef01cSRoman Divacky STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses"); 41f22ef01cSRoman Divacky 42f22ef01cSRoman Divacky STATISTIC(NumCacheNonLocalPtr, 43f22ef01cSRoman Divacky "Number of fully cached non-local ptr responses"); 44f22ef01cSRoman Divacky STATISTIC(NumCacheDirtyNonLocalPtr, 45f22ef01cSRoman Divacky "Number of cached, but dirty, non-local ptr responses"); 46f22ef01cSRoman Divacky STATISTIC(NumUncacheNonLocalPtr, 47f22ef01cSRoman Divacky "Number of uncached non-local ptr responses"); 48f22ef01cSRoman Divacky STATISTIC(NumCacheCompleteNonLocalPtr, 49f22ef01cSRoman Divacky "Number of block queries that were completely cached"); 50f22ef01cSRoman Divacky 5117a519f9SDimitry Andric // Limit for the number of instructions to scan in a block. 5239d628a0SDimitry Andric static const unsigned int BlockScanLimit = 100; 5339d628a0SDimitry Andric 5439d628a0SDimitry Andric // Limit on the number of memdep results to process. 5539d628a0SDimitry Andric static const unsigned int NumResultsLimit = 100; 5617a519f9SDimitry Andric 57f22ef01cSRoman Divacky char MemoryDependenceAnalysis::ID = 0; 58f22ef01cSRoman Divacky 59f22ef01cSRoman Divacky // Register this pass... 602754fe60SDimitry Andric INITIALIZE_PASS_BEGIN(MemoryDependenceAnalysis, "memdep", 612754fe60SDimitry Andric "Memory Dependence Analysis", false, true) 6239d628a0SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 632754fe60SDimitry Andric INITIALIZE_AG_DEPENDENCY(AliasAnalysis) 642754fe60SDimitry Andric INITIALIZE_PASS_END(MemoryDependenceAnalysis, "memdep", 652754fe60SDimitry Andric "Memory Dependence Analysis", false, true) 66f22ef01cSRoman Divacky 67f22ef01cSRoman Divacky MemoryDependenceAnalysis::MemoryDependenceAnalysis() 68ff0cc061SDimitry Andric : FunctionPass(ID) { 692754fe60SDimitry Andric initializeMemoryDependenceAnalysisPass(*PassRegistry::getPassRegistry()); 70f22ef01cSRoman Divacky } 71f22ef01cSRoman Divacky MemoryDependenceAnalysis::~MemoryDependenceAnalysis() { 72f22ef01cSRoman Divacky } 73f22ef01cSRoman Divacky 74f22ef01cSRoman Divacky /// Clean up memory in between runs 75f22ef01cSRoman Divacky void MemoryDependenceAnalysis::releaseMemory() { 76f22ef01cSRoman Divacky LocalDeps.clear(); 77f22ef01cSRoman Divacky NonLocalDeps.clear(); 78f22ef01cSRoman Divacky NonLocalPointerDeps.clear(); 79f22ef01cSRoman Divacky ReverseLocalDeps.clear(); 80f22ef01cSRoman Divacky ReverseNonLocalDeps.clear(); 81f22ef01cSRoman Divacky ReverseNonLocalPtrDeps.clear(); 82ff0cc061SDimitry Andric PredCache.clear(); 83f22ef01cSRoman Divacky } 84f22ef01cSRoman Divacky 85f22ef01cSRoman Divacky /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis. 86f22ef01cSRoman Divacky /// 87f22ef01cSRoman Divacky void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 88f22ef01cSRoman Divacky AU.setPreservesAll(); 8939d628a0SDimitry Andric AU.addRequired<AssumptionCacheTracker>(); 90f22ef01cSRoman Divacky AU.addRequiredTransitive<AliasAnalysis>(); 91f22ef01cSRoman Divacky } 92f22ef01cSRoman Divacky 9339d628a0SDimitry Andric bool MemoryDependenceAnalysis::runOnFunction(Function &F) { 94f22ef01cSRoman Divacky AA = &getAnalysis<AliasAnalysis>(); 9539d628a0SDimitry Andric AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 9691bc56edSDimitry Andric DominatorTreeWrapperPass *DTWP = 9791bc56edSDimitry Andric getAnalysisIfAvailable<DominatorTreeWrapperPass>(); 9891bc56edSDimitry Andric DT = DTWP ? &DTWP->getDomTree() : nullptr; 99f22ef01cSRoman Divacky return false; 100f22ef01cSRoman Divacky } 101f22ef01cSRoman Divacky 102f22ef01cSRoman Divacky /// RemoveFromReverseMap - This is a helper function that removes Val from 103f22ef01cSRoman Divacky /// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry. 104f22ef01cSRoman Divacky template <typename KeyTy> 105f22ef01cSRoman Divacky static void RemoveFromReverseMap(DenseMap<Instruction*, 106f22ef01cSRoman Divacky SmallPtrSet<KeyTy, 4> > &ReverseMap, 107f22ef01cSRoman Divacky Instruction *Inst, KeyTy Val) { 108f22ef01cSRoman Divacky typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator 109f22ef01cSRoman Divacky InstIt = ReverseMap.find(Inst); 110f22ef01cSRoman Divacky assert(InstIt != ReverseMap.end() && "Reverse map out of sync?"); 111f22ef01cSRoman Divacky bool Found = InstIt->second.erase(Val); 1122754fe60SDimitry Andric assert(Found && "Invalid reverse map!"); (void)Found; 113f22ef01cSRoman Divacky if (InstIt->second.empty()) 114f22ef01cSRoman Divacky ReverseMap.erase(InstIt); 115f22ef01cSRoman Divacky } 116f22ef01cSRoman Divacky 1172754fe60SDimitry Andric /// GetLocation - If the given instruction references a specific memory 1182754fe60SDimitry Andric /// location, fill in Loc with the details, otherwise set Loc.Ptr to null. 1192754fe60SDimitry Andric /// Return a ModRefInfo value describing the general behavior of the 1202754fe60SDimitry Andric /// instruction. 1212754fe60SDimitry Andric static 1222754fe60SDimitry Andric AliasAnalysis::ModRefResult GetLocation(const Instruction *Inst, 1232754fe60SDimitry Andric AliasAnalysis::Location &Loc, 1242754fe60SDimitry Andric AliasAnalysis *AA) { 1252754fe60SDimitry Andric if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) { 1266122f3e6SDimitry Andric if (LI->isUnordered()) { 1272754fe60SDimitry Andric Loc = AA->getLocation(LI); 1282754fe60SDimitry Andric return AliasAnalysis::Ref; 129139f7f9bSDimitry Andric } 130139f7f9bSDimitry Andric if (LI->getOrdering() == Monotonic) { 1316122f3e6SDimitry Andric Loc = AA->getLocation(LI); 1326122f3e6SDimitry Andric return AliasAnalysis::ModRef; 1336122f3e6SDimitry Andric } 1346122f3e6SDimitry Andric Loc = AliasAnalysis::Location(); 1356122f3e6SDimitry Andric return AliasAnalysis::ModRef; 1362754fe60SDimitry Andric } 1372754fe60SDimitry Andric 1382754fe60SDimitry Andric if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 1396122f3e6SDimitry Andric if (SI->isUnordered()) { 1402754fe60SDimitry Andric Loc = AA->getLocation(SI); 1412754fe60SDimitry Andric return AliasAnalysis::Mod; 142139f7f9bSDimitry Andric } 143139f7f9bSDimitry Andric if (SI->getOrdering() == Monotonic) { 1446122f3e6SDimitry Andric Loc = AA->getLocation(SI); 1456122f3e6SDimitry Andric return AliasAnalysis::ModRef; 1466122f3e6SDimitry Andric } 1476122f3e6SDimitry Andric Loc = AliasAnalysis::Location(); 1486122f3e6SDimitry Andric return AliasAnalysis::ModRef; 1492754fe60SDimitry Andric } 1502754fe60SDimitry Andric 1512754fe60SDimitry Andric if (const VAArgInst *V = dyn_cast<VAArgInst>(Inst)) { 1522754fe60SDimitry Andric Loc = AA->getLocation(V); 1532754fe60SDimitry Andric return AliasAnalysis::ModRef; 1542754fe60SDimitry Andric } 1552754fe60SDimitry Andric 1563861d79fSDimitry Andric if (const CallInst *CI = isFreeCall(Inst, AA->getTargetLibraryInfo())) { 1572754fe60SDimitry Andric // calls to free() deallocate the entire structure 1582754fe60SDimitry Andric Loc = AliasAnalysis::Location(CI->getArgOperand(0)); 1592754fe60SDimitry Andric return AliasAnalysis::Mod; 1602754fe60SDimitry Andric } 1612754fe60SDimitry Andric 16239d628a0SDimitry Andric if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { 16339d628a0SDimitry Andric AAMDNodes AAInfo; 16439d628a0SDimitry Andric 1652754fe60SDimitry Andric switch (II->getIntrinsicID()) { 1662754fe60SDimitry Andric case Intrinsic::lifetime_start: 1672754fe60SDimitry Andric case Intrinsic::lifetime_end: 1682754fe60SDimitry Andric case Intrinsic::invariant_start: 16939d628a0SDimitry Andric II->getAAMetadata(AAInfo); 1702754fe60SDimitry Andric Loc = AliasAnalysis::Location(II->getArgOperand(1), 1712754fe60SDimitry Andric cast<ConstantInt>(II->getArgOperand(0)) 17239d628a0SDimitry Andric ->getZExtValue(), AAInfo); 1732754fe60SDimitry Andric // These intrinsics don't really modify the memory, but returning Mod 1742754fe60SDimitry Andric // will allow them to be handled conservatively. 1752754fe60SDimitry Andric return AliasAnalysis::Mod; 1762754fe60SDimitry Andric case Intrinsic::invariant_end: 17739d628a0SDimitry Andric II->getAAMetadata(AAInfo); 1782754fe60SDimitry Andric Loc = AliasAnalysis::Location(II->getArgOperand(2), 1792754fe60SDimitry Andric cast<ConstantInt>(II->getArgOperand(1)) 18039d628a0SDimitry Andric ->getZExtValue(), AAInfo); 1812754fe60SDimitry Andric // These intrinsics don't really modify the memory, but returning Mod 1822754fe60SDimitry Andric // will allow them to be handled conservatively. 1832754fe60SDimitry Andric return AliasAnalysis::Mod; 1842754fe60SDimitry Andric default: 1852754fe60SDimitry Andric break; 1862754fe60SDimitry Andric } 18739d628a0SDimitry Andric } 1882754fe60SDimitry Andric 1892754fe60SDimitry Andric // Otherwise, just do the coarse-grained thing that always works. 1902754fe60SDimitry Andric if (Inst->mayWriteToMemory()) 1912754fe60SDimitry Andric return AliasAnalysis::ModRef; 1922754fe60SDimitry Andric if (Inst->mayReadFromMemory()) 1932754fe60SDimitry Andric return AliasAnalysis::Ref; 1942754fe60SDimitry Andric return AliasAnalysis::NoModRef; 1952754fe60SDimitry Andric } 196f22ef01cSRoman Divacky 197f22ef01cSRoman Divacky /// getCallSiteDependencyFrom - Private helper for finding the local 198f22ef01cSRoman Divacky /// dependencies of a call site. 199f22ef01cSRoman Divacky MemDepResult MemoryDependenceAnalysis:: 200f22ef01cSRoman Divacky getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall, 201f22ef01cSRoman Divacky BasicBlock::iterator ScanIt, BasicBlock *BB) { 20217a519f9SDimitry Andric unsigned Limit = BlockScanLimit; 20317a519f9SDimitry Andric 204f22ef01cSRoman Divacky // Walk backwards through the block, looking for dependencies 205f22ef01cSRoman Divacky while (ScanIt != BB->begin()) { 20617a519f9SDimitry Andric // Limit the amount of scanning we do so we don't end up with quadratic 20717a519f9SDimitry Andric // running time on extreme testcases. 20817a519f9SDimitry Andric --Limit; 20917a519f9SDimitry Andric if (!Limit) 21017a519f9SDimitry Andric return MemDepResult::getUnknown(); 21117a519f9SDimitry Andric 212f22ef01cSRoman Divacky Instruction *Inst = --ScanIt; 213f22ef01cSRoman Divacky 214f22ef01cSRoman Divacky // If this inst is a memory op, get the pointer it accessed 2152754fe60SDimitry Andric AliasAnalysis::Location Loc; 2162754fe60SDimitry Andric AliasAnalysis::ModRefResult MR = GetLocation(Inst, Loc, AA); 2172754fe60SDimitry Andric if (Loc.Ptr) { 2182754fe60SDimitry Andric // A simple instruction. 2192754fe60SDimitry Andric if (AA->getModRefInfo(CS, Loc) != AliasAnalysis::NoModRef) 2202754fe60SDimitry Andric return MemDepResult::getClobber(Inst); 2212754fe60SDimitry Andric continue; 2222754fe60SDimitry Andric } 2232754fe60SDimitry Andric 224ff0cc061SDimitry Andric if (auto InstCS = CallSite(Inst)) { 225f22ef01cSRoman Divacky // Debug intrinsics don't cause dependences. 226f22ef01cSRoman Divacky if (isa<DbgInfoIntrinsic>(Inst)) continue; 227f22ef01cSRoman Divacky // If these two calls do not interfere, look past it. 228f22ef01cSRoman Divacky switch (AA->getModRefInfo(CS, InstCS)) { 229f22ef01cSRoman Divacky case AliasAnalysis::NoModRef: 230e580952dSDimitry Andric // If the two calls are the same, return InstCS as a Def, so that 231e580952dSDimitry Andric // CS can be found redundant and eliminated. 2322754fe60SDimitry Andric if (isReadOnlyCall && !(MR & AliasAnalysis::Mod) && 233e580952dSDimitry Andric CS.getInstruction()->isIdenticalToWhenDefined(Inst)) 234f22ef01cSRoman Divacky return MemDepResult::getDef(Inst); 235e580952dSDimitry Andric 236e580952dSDimitry Andric // Otherwise if the two calls don't interact (e.g. InstCS is readnone) 237e580952dSDimitry Andric // keep scanning. 2387ae0e2c9SDimitry Andric continue; 239f22ef01cSRoman Divacky default: 240f22ef01cSRoman Divacky return MemDepResult::getClobber(Inst); 241f22ef01cSRoman Divacky } 242f22ef01cSRoman Divacky } 2437ae0e2c9SDimitry Andric 2447ae0e2c9SDimitry Andric // If we could not obtain a pointer for the instruction and the instruction 2457ae0e2c9SDimitry Andric // touches memory then assume that this is a dependency. 2467ae0e2c9SDimitry Andric if (MR != AliasAnalysis::NoModRef) 2477ae0e2c9SDimitry Andric return MemDepResult::getClobber(Inst); 248f22ef01cSRoman Divacky } 249f22ef01cSRoman Divacky 25017a519f9SDimitry Andric // No dependence found. If this is the entry block of the function, it is 25117a519f9SDimitry Andric // unknown, otherwise it is non-local. 252f22ef01cSRoman Divacky if (BB != &BB->getParent()->getEntryBlock()) 253f22ef01cSRoman Divacky return MemDepResult::getNonLocal(); 2546122f3e6SDimitry Andric return MemDepResult::getNonFuncLocal(); 255f22ef01cSRoman Divacky } 256f22ef01cSRoman Divacky 2573b0f4066SDimitry Andric /// isLoadLoadClobberIfExtendedToFullWidth - Return true if LI is a load that 2583b0f4066SDimitry Andric /// would fully overlap MemLoc if done as a wider legal integer load. 2593b0f4066SDimitry Andric /// 2603b0f4066SDimitry Andric /// MemLocBase, MemLocOffset are lazily computed here the first time the 2613b0f4066SDimitry Andric /// base/offs of memloc is needed. 262ff0cc061SDimitry Andric static bool isLoadLoadClobberIfExtendedToFullWidth( 263ff0cc061SDimitry Andric const AliasAnalysis::Location &MemLoc, const Value *&MemLocBase, 264ff0cc061SDimitry Andric int64_t &MemLocOffs, const LoadInst *LI) { 265ff0cc061SDimitry Andric const DataLayout &DL = LI->getModule()->getDataLayout(); 2663b0f4066SDimitry Andric 2673b0f4066SDimitry Andric // If we haven't already computed the base/offset of MemLoc, do so now. 26891bc56edSDimitry Andric if (!MemLocBase) 26991bc56edSDimitry Andric MemLocBase = GetPointerBaseWithConstantOffset(MemLoc.Ptr, MemLocOffs, DL); 2703b0f4066SDimitry Andric 271ff0cc061SDimitry Andric unsigned Size = MemoryDependenceAnalysis::getLoadLoadClobberFullWidthSize( 272ff0cc061SDimitry Andric MemLocBase, MemLocOffs, MemLoc.Size, LI); 2733b0f4066SDimitry Andric return Size != 0; 2743b0f4066SDimitry Andric } 2753b0f4066SDimitry Andric 2763b0f4066SDimitry Andric /// getLoadLoadClobberFullWidthSize - This is a little bit of analysis that 2773b0f4066SDimitry Andric /// looks at a memory location for a load (specified by MemLocBase, Offs, 2783b0f4066SDimitry Andric /// and Size) and compares it against a load. If the specified load could 2793b0f4066SDimitry Andric /// be safely widened to a larger integer load that is 1) still efficient, 2803b0f4066SDimitry Andric /// 2) safe for the target, and 3) would provide the specified memory 2813b0f4066SDimitry Andric /// location value, then this function returns the size in bytes of the 2823b0f4066SDimitry Andric /// load width to use. If not, this returns zero. 283ff0cc061SDimitry Andric unsigned MemoryDependenceAnalysis::getLoadLoadClobberFullWidthSize( 284ff0cc061SDimitry Andric const Value *MemLocBase, int64_t MemLocOffs, unsigned MemLocSize, 285ff0cc061SDimitry Andric const LoadInst *LI) { 2866122f3e6SDimitry Andric // We can only extend simple integer loads. 2876122f3e6SDimitry Andric if (!isa<IntegerType>(LI->getType()) || !LI->isSimple()) return 0; 2883b0f4066SDimitry Andric 289139f7f9bSDimitry Andric // Load widening is hostile to ThreadSanitizer: it may cause false positives 290139f7f9bSDimitry Andric // or make the reports more cryptic (access sizes are wrong). 291ff0cc061SDimitry Andric if (LI->getParent()->getParent()->hasFnAttribute(Attribute::SanitizeThread)) 292139f7f9bSDimitry Andric return 0; 293139f7f9bSDimitry Andric 294ff0cc061SDimitry Andric const DataLayout &DL = LI->getModule()->getDataLayout(); 295ff0cc061SDimitry Andric 2963b0f4066SDimitry Andric // Get the base of this load. 2973b0f4066SDimitry Andric int64_t LIOffs = 0; 2983b0f4066SDimitry Andric const Value *LIBase = 299ff0cc061SDimitry Andric GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, DL); 3003b0f4066SDimitry Andric 3013b0f4066SDimitry Andric // If the two pointers are not based on the same pointer, we can't tell that 3023b0f4066SDimitry Andric // they are related. 3033b0f4066SDimitry Andric if (LIBase != MemLocBase) return 0; 3043b0f4066SDimitry Andric 3053b0f4066SDimitry Andric // Okay, the two values are based on the same pointer, but returned as 3063b0f4066SDimitry Andric // no-alias. This happens when we have things like two byte loads at "P+1" 3073b0f4066SDimitry Andric // and "P+3". Check to see if increasing the size of the "LI" load up to its 3083b0f4066SDimitry Andric // alignment (or the largest native integer type) will allow us to load all 3093b0f4066SDimitry Andric // the bits required by MemLoc. 3103b0f4066SDimitry Andric 3113b0f4066SDimitry Andric // If MemLoc is before LI, then no widening of LI will help us out. 3123b0f4066SDimitry Andric if (MemLocOffs < LIOffs) return 0; 3133b0f4066SDimitry Andric 3143b0f4066SDimitry Andric // Get the alignment of the load in bytes. We assume that it is safe to load 3153b0f4066SDimitry Andric // any legal integer up to this size without a problem. For example, if we're 3163b0f4066SDimitry Andric // looking at an i8 load on x86-32 that is known 1024 byte aligned, we can 3173b0f4066SDimitry Andric // widen it up to an i32 load. If it is known 2-byte aligned, we can widen it 3183b0f4066SDimitry Andric // to i16. 3193b0f4066SDimitry Andric unsigned LoadAlign = LI->getAlignment(); 3203b0f4066SDimitry Andric 3213b0f4066SDimitry Andric int64_t MemLocEnd = MemLocOffs+MemLocSize; 3223b0f4066SDimitry Andric 3233b0f4066SDimitry Andric // If no amount of rounding up will let MemLoc fit into LI, then bail out. 3243b0f4066SDimitry Andric if (LIOffs+LoadAlign < MemLocEnd) return 0; 3253b0f4066SDimitry Andric 3263b0f4066SDimitry Andric // This is the size of the load to try. Start with the next larger power of 3273b0f4066SDimitry Andric // two. 3283b0f4066SDimitry Andric unsigned NewLoadByteSize = LI->getType()->getPrimitiveSizeInBits()/8U; 3293b0f4066SDimitry Andric NewLoadByteSize = NextPowerOf2(NewLoadByteSize); 3303b0f4066SDimitry Andric 3313b0f4066SDimitry Andric while (1) { 3323b0f4066SDimitry Andric // If this load size is bigger than our known alignment or would not fit 3333b0f4066SDimitry Andric // into a native integer register, then we fail. 3343b0f4066SDimitry Andric if (NewLoadByteSize > LoadAlign || 33591bc56edSDimitry Andric !DL.fitsInLegalInteger(NewLoadByteSize*8)) 3363b0f4066SDimitry Andric return 0; 3373b0f4066SDimitry Andric 338dff0c46cSDimitry Andric if (LIOffs + NewLoadByteSize > MemLocEnd && 339ff0cc061SDimitry Andric LI->getParent()->getParent()->hasFnAttribute( 340ff0cc061SDimitry Andric Attribute::SanitizeAddress)) 341dff0c46cSDimitry Andric // We will be reading past the location accessed by the original program. 342dff0c46cSDimitry Andric // While this is safe in a regular build, Address Safety analysis tools 343dff0c46cSDimitry Andric // may start reporting false warnings. So, don't do widening. 344dff0c46cSDimitry Andric return 0; 345dff0c46cSDimitry Andric 3463b0f4066SDimitry Andric // If a load of this width would include all of MemLoc, then we succeed. 3473b0f4066SDimitry Andric if (LIOffs+NewLoadByteSize >= MemLocEnd) 3483b0f4066SDimitry Andric return NewLoadByteSize; 3493b0f4066SDimitry Andric 3503b0f4066SDimitry Andric NewLoadByteSize <<= 1; 3513b0f4066SDimitry Andric } 352dff0c46cSDimitry Andric } 3533b0f4066SDimitry Andric 354ff0cc061SDimitry Andric static bool isVolatile(Instruction *Inst) { 355ff0cc061SDimitry Andric if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) 356ff0cc061SDimitry Andric return LI->isVolatile(); 357ff0cc061SDimitry Andric else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) 358ff0cc061SDimitry Andric return SI->isVolatile(); 359ff0cc061SDimitry Andric else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(Inst)) 360ff0cc061SDimitry Andric return AI->isVolatile(); 361ff0cc061SDimitry Andric return false; 362ff0cc061SDimitry Andric } 363ff0cc061SDimitry Andric 364ff0cc061SDimitry Andric 365f22ef01cSRoman Divacky /// getPointerDependencyFrom - Return the instruction on which a memory 3662754fe60SDimitry Andric /// location depends. If isLoad is true, this routine ignores may-aliases with 3672754fe60SDimitry Andric /// read-only operations. If isLoad is false, this routine ignores may-aliases 368139f7f9bSDimitry Andric /// with reads from read-only locations. If possible, pass the query 369139f7f9bSDimitry Andric /// instruction as well; this function may take advantage of the metadata 370139f7f9bSDimitry Andric /// annotated to the query instruction to refine the result. 371f22ef01cSRoman Divacky MemDepResult MemoryDependenceAnalysis:: 3722754fe60SDimitry Andric getPointerDependencyFrom(const AliasAnalysis::Location &MemLoc, bool isLoad, 373139f7f9bSDimitry Andric BasicBlock::iterator ScanIt, BasicBlock *BB, 374139f7f9bSDimitry Andric Instruction *QueryInst) { 375f22ef01cSRoman Divacky 37691bc56edSDimitry Andric const Value *MemLocBase = nullptr; 3773b0f4066SDimitry Andric int64_t MemLocOffset = 0; 37817a519f9SDimitry Andric unsigned Limit = BlockScanLimit; 379139f7f9bSDimitry Andric bool isInvariantLoad = false; 38039d628a0SDimitry Andric 38139d628a0SDimitry Andric // We must be careful with atomic accesses, as they may allow another thread 38239d628a0SDimitry Andric // to touch this location, cloberring it. We are conservative: if the 38339d628a0SDimitry Andric // QueryInst is not a simple (non-atomic) memory access, we automatically 38439d628a0SDimitry Andric // return getClobber. 38539d628a0SDimitry Andric // If it is simple, we know based on the results of 38639d628a0SDimitry Andric // "Compiler testing via a theory of sound optimisations in the C11/C++11 38739d628a0SDimitry Andric // memory model" in PLDI 2013, that a non-atomic location can only be 38839d628a0SDimitry Andric // clobbered between a pair of a release and an acquire action, with no 38939d628a0SDimitry Andric // access to the location in between. 39039d628a0SDimitry Andric // Here is an example for giving the general intuition behind this rule. 39139d628a0SDimitry Andric // In the following code: 39239d628a0SDimitry Andric // store x 0; 39339d628a0SDimitry Andric // release action; [1] 39439d628a0SDimitry Andric // acquire action; [4] 39539d628a0SDimitry Andric // %val = load x; 39639d628a0SDimitry Andric // It is unsafe to replace %val by 0 because another thread may be running: 39739d628a0SDimitry Andric // acquire action; [2] 39839d628a0SDimitry Andric // store x 42; 39939d628a0SDimitry Andric // release action; [3] 40039d628a0SDimitry Andric // with synchronization from 1 to 2 and from 3 to 4, resulting in %val 40139d628a0SDimitry Andric // being 42. A key property of this program however is that if either 40239d628a0SDimitry Andric // 1 or 4 were missing, there would be a race between the store of 42 40339d628a0SDimitry Andric // either the store of 0 or the load (making the whole progam racy). 40439d628a0SDimitry Andric // The paper mentionned above shows that the same property is respected 40539d628a0SDimitry Andric // by every program that can detect any optimisation of that kind: either 40639d628a0SDimitry Andric // it is racy (undefined) or there is a release followed by an acquire 40739d628a0SDimitry Andric // between the pair of accesses under consideration. 40839d628a0SDimitry Andric 409ff0cc061SDimitry Andric // If the load is invariant, we "know" that it doesn't alias *any* write. We 410ff0cc061SDimitry Andric // do want to respect mustalias results since defs are useful for value 411ff0cc061SDimitry Andric // forwarding, but any mayalias write can be assumed to be noalias. 412ff0cc061SDimitry Andric // Arguably, this logic should be pushed inside AliasAnalysis itself. 413139f7f9bSDimitry Andric if (isLoad && QueryInst) { 414139f7f9bSDimitry Andric LoadInst *LI = dyn_cast<LoadInst>(QueryInst); 41591bc56edSDimitry Andric if (LI && LI->getMetadata(LLVMContext::MD_invariant_load) != nullptr) 416139f7f9bSDimitry Andric isInvariantLoad = true; 417139f7f9bSDimitry Andric } 41817a519f9SDimitry Andric 419ff0cc061SDimitry Andric const DataLayout &DL = BB->getModule()->getDataLayout(); 420ff0cc061SDimitry Andric 421f22ef01cSRoman Divacky // Walk backwards through the basic block, looking for dependencies. 422f22ef01cSRoman Divacky while (ScanIt != BB->begin()) { 423f785676fSDimitry Andric Instruction *Inst = --ScanIt; 424f785676fSDimitry Andric 425f785676fSDimitry Andric if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) 426f785676fSDimitry Andric // Debug intrinsics don't (and can't) cause dependencies. 427f785676fSDimitry Andric if (isa<DbgInfoIntrinsic>(II)) continue; 428f785676fSDimitry Andric 42917a519f9SDimitry Andric // Limit the amount of scanning we do so we don't end up with quadratic 43017a519f9SDimitry Andric // running time on extreme testcases. 43117a519f9SDimitry Andric --Limit; 43217a519f9SDimitry Andric if (!Limit) 43317a519f9SDimitry Andric return MemDepResult::getUnknown(); 43417a519f9SDimitry Andric 435f22ef01cSRoman Divacky if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) { 436f22ef01cSRoman Divacky // If we reach a lifetime begin or end marker, then the query ends here 437f22ef01cSRoman Divacky // because the value is undefined. 4382754fe60SDimitry Andric if (II->getIntrinsicID() == Intrinsic::lifetime_start) { 439f22ef01cSRoman Divacky // FIXME: This only considers queries directly on the invariant-tagged 440f22ef01cSRoman Divacky // pointer, not on query pointers that are indexed off of them. It'd 4413b0f4066SDimitry Andric // be nice to handle that at some point (the right approach is to use 4423b0f4066SDimitry Andric // GetPointerBaseWithConstantOffset). 4433b0f4066SDimitry Andric if (AA->isMustAlias(AliasAnalysis::Location(II->getArgOperand(1)), 4443b0f4066SDimitry Andric MemLoc)) 445f22ef01cSRoman Divacky return MemDepResult::getDef(II); 4462754fe60SDimitry Andric continue; 447f22ef01cSRoman Divacky } 448f22ef01cSRoman Divacky } 449f22ef01cSRoman Divacky 450f22ef01cSRoman Divacky // Values depend on loads if the pointers are must aliased. This means that 451f22ef01cSRoman Divacky // a load depends on another must aliased load from the same value. 45239d628a0SDimitry Andric // One exception is atomic loads: a value can depend on an atomic load that it 45339d628a0SDimitry Andric // does not alias with when this atomic load indicates that another thread may 45439d628a0SDimitry Andric // be accessing the location. 455f22ef01cSRoman Divacky if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { 456ff0cc061SDimitry Andric 457ff0cc061SDimitry Andric // While volatile access cannot be eliminated, they do not have to clobber 458ff0cc061SDimitry Andric // non-aliasing locations, as normal accesses, for example, can be safely 459ff0cc061SDimitry Andric // reordered with volatile accesses. 460ff0cc061SDimitry Andric if (LI->isVolatile()) { 461ff0cc061SDimitry Andric if (!QueryInst) 462ff0cc061SDimitry Andric // Original QueryInst *may* be volatile 463ff0cc061SDimitry Andric return MemDepResult::getClobber(LI); 464ff0cc061SDimitry Andric if (isVolatile(QueryInst)) 465ff0cc061SDimitry Andric // Ordering required if QueryInst is itself volatile 466ff0cc061SDimitry Andric return MemDepResult::getClobber(LI); 467ff0cc061SDimitry Andric // Otherwise, volatile doesn't imply any special ordering 468ff0cc061SDimitry Andric } 469ff0cc061SDimitry Andric 4706122f3e6SDimitry Andric // Atomic loads have complications involved. 47139d628a0SDimitry Andric // A Monotonic (or higher) load is OK if the query inst is itself not atomic. 4726122f3e6SDimitry Andric // FIXME: This is overly conservative. 473ff0cc061SDimitry Andric if (LI->isAtomic() && LI->getOrdering() > Unordered) { 47439d628a0SDimitry Andric if (!QueryInst) 47539d628a0SDimitry Andric return MemDepResult::getClobber(LI); 476ff0cc061SDimitry Andric if (LI->getOrdering() != Monotonic) 477ff0cc061SDimitry Andric return MemDepResult::getClobber(LI); 47839d628a0SDimitry Andric if (auto *QueryLI = dyn_cast<LoadInst>(QueryInst)) { 47939d628a0SDimitry Andric if (!QueryLI->isSimple()) 48039d628a0SDimitry Andric return MemDepResult::getClobber(LI); 48139d628a0SDimitry Andric } else if (auto *QuerySI = dyn_cast<StoreInst>(QueryInst)) { 48239d628a0SDimitry Andric if (!QuerySI->isSimple()) 48339d628a0SDimitry Andric return MemDepResult::getClobber(LI); 48439d628a0SDimitry Andric } else if (QueryInst->mayReadOrWriteMemory()) { 48539d628a0SDimitry Andric return MemDepResult::getClobber(LI); 48639d628a0SDimitry Andric } 48739d628a0SDimitry Andric } 48839d628a0SDimitry Andric 4892754fe60SDimitry Andric AliasAnalysis::Location LoadLoc = AA->getLocation(LI); 490f22ef01cSRoman Divacky 491f22ef01cSRoman Divacky // If we found a pointer, check if it could be the same as our pointer. 4922754fe60SDimitry Andric AliasAnalysis::AliasResult R = AA->alias(LoadLoc, MemLoc); 4933b0f4066SDimitry Andric 4943b0f4066SDimitry Andric if (isLoad) { 4953b0f4066SDimitry Andric if (R == AliasAnalysis::NoAlias) { 4963b0f4066SDimitry Andric // If this is an over-aligned integer load (for example, 4973b0f4066SDimitry Andric // "load i8* %P, align 4") see if it would obviously overlap with the 4983b0f4066SDimitry Andric // queried location if widened to a larger load (e.g. if the queried 4993b0f4066SDimitry Andric // location is 1 byte at P+1). If so, return it as a load/load 5003b0f4066SDimitry Andric // clobber result, allowing the client to decide to widen the load if 5013b0f4066SDimitry Andric // it wants to. 502ff0cc061SDimitry Andric if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType())) { 5033b0f4066SDimitry Andric if (LI->getAlignment() * 8 > ITy->getPrimitiveSizeInBits() && 5043b0f4066SDimitry Andric isLoadLoadClobberIfExtendedToFullWidth(MemLoc, MemLocBase, 505ff0cc061SDimitry Andric MemLocOffset, LI)) 5063b0f4066SDimitry Andric return MemDepResult::getClobber(Inst); 507ff0cc061SDimitry Andric } 5083b0f4066SDimitry Andric continue; 5093b0f4066SDimitry Andric } 5103b0f4066SDimitry Andric 5113b0f4066SDimitry Andric // Must aliased loads are defs of each other. 5123b0f4066SDimitry Andric if (R == AliasAnalysis::MustAlias) 5133b0f4066SDimitry Andric return MemDepResult::getDef(Inst); 5143b0f4066SDimitry Andric 515bd5abe19SDimitry Andric #if 0 // FIXME: Temporarily disabled. GVN is cleverly rewriting loads 516bd5abe19SDimitry Andric // in terms of clobbering loads, but since it does this by looking 517bd5abe19SDimitry Andric // at the clobbering load directly, it doesn't know about any 518bd5abe19SDimitry Andric // phi translation that may have happened along the way. 519bd5abe19SDimitry Andric 5203b0f4066SDimitry Andric // If we have a partial alias, then return this as a clobber for the 5213b0f4066SDimitry Andric // client to handle. 5223b0f4066SDimitry Andric if (R == AliasAnalysis::PartialAlias) 5233b0f4066SDimitry Andric return MemDepResult::getClobber(Inst); 524bd5abe19SDimitry Andric #endif 5253b0f4066SDimitry Andric 5263b0f4066SDimitry Andric // Random may-alias loads don't depend on each other without a 5273b0f4066SDimitry Andric // dependence. 5283b0f4066SDimitry Andric continue; 5293b0f4066SDimitry Andric } 5303b0f4066SDimitry Andric 5313b0f4066SDimitry Andric // Stores don't depend on other no-aliased accesses. 532f22ef01cSRoman Divacky if (R == AliasAnalysis::NoAlias) 533f22ef01cSRoman Divacky continue; 534f22ef01cSRoman Divacky 5352754fe60SDimitry Andric // Stores don't alias loads from read-only memory. 5363b0f4066SDimitry Andric if (AA->pointsToConstantMemory(LoadLoc)) 5372754fe60SDimitry Andric continue; 5382754fe60SDimitry Andric 5393b0f4066SDimitry Andric // Stores depend on may/must aliased loads. 540f22ef01cSRoman Divacky return MemDepResult::getDef(Inst); 541f22ef01cSRoman Divacky } 542f22ef01cSRoman Divacky 543f22ef01cSRoman Divacky if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 5446122f3e6SDimitry Andric // Atomic stores have complications involved. 54539d628a0SDimitry Andric // A Monotonic store is OK if the query inst is itself not atomic. 5466122f3e6SDimitry Andric // FIXME: This is overly conservative. 54739d628a0SDimitry Andric if (!SI->isUnordered()) { 54839d628a0SDimitry Andric if (!QueryInst) 54939d628a0SDimitry Andric return MemDepResult::getClobber(SI); 550ff0cc061SDimitry Andric if (SI->getOrdering() != Monotonic) 551ff0cc061SDimitry Andric return MemDepResult::getClobber(SI); 55239d628a0SDimitry Andric if (auto *QueryLI = dyn_cast<LoadInst>(QueryInst)) { 55339d628a0SDimitry Andric if (!QueryLI->isSimple()) 55439d628a0SDimitry Andric return MemDepResult::getClobber(SI); 55539d628a0SDimitry Andric } else if (auto *QuerySI = dyn_cast<StoreInst>(QueryInst)) { 55639d628a0SDimitry Andric if (!QuerySI->isSimple()) 55739d628a0SDimitry Andric return MemDepResult::getClobber(SI); 55839d628a0SDimitry Andric } else if (QueryInst->mayReadOrWriteMemory()) { 55939d628a0SDimitry Andric return MemDepResult::getClobber(SI); 56039d628a0SDimitry Andric } 56139d628a0SDimitry Andric } 56239d628a0SDimitry Andric 56339d628a0SDimitry Andric // FIXME: this is overly conservative. 56439d628a0SDimitry Andric // While volatile access cannot be eliminated, they do not have to clobber 56539d628a0SDimitry Andric // non-aliasing locations, as normal accesses can for example be reordered 56639d628a0SDimitry Andric // with volatile accesses. 56739d628a0SDimitry Andric if (SI->isVolatile()) 5686122f3e6SDimitry Andric return MemDepResult::getClobber(SI); 5696122f3e6SDimitry Andric 570f22ef01cSRoman Divacky // If alias analysis can tell that this store is guaranteed to not modify 571f22ef01cSRoman Divacky // the query pointer, ignore it. Use getModRefInfo to handle cases where 572f22ef01cSRoman Divacky // the query pointer points to constant memory etc. 5732754fe60SDimitry Andric if (AA->getModRefInfo(SI, MemLoc) == AliasAnalysis::NoModRef) 574f22ef01cSRoman Divacky continue; 575f22ef01cSRoman Divacky 576f22ef01cSRoman Divacky // Ok, this store might clobber the query pointer. Check to see if it is 577f22ef01cSRoman Divacky // a must alias: in this case, we want to return this as a def. 5782754fe60SDimitry Andric AliasAnalysis::Location StoreLoc = AA->getLocation(SI); 579f22ef01cSRoman Divacky 580f22ef01cSRoman Divacky // If we found a pointer, check if it could be the same as our pointer. 5812754fe60SDimitry Andric AliasAnalysis::AliasResult R = AA->alias(StoreLoc, MemLoc); 582f22ef01cSRoman Divacky 583f22ef01cSRoman Divacky if (R == AliasAnalysis::NoAlias) 584f22ef01cSRoman Divacky continue; 5852754fe60SDimitry Andric if (R == AliasAnalysis::MustAlias) 586f22ef01cSRoman Divacky return MemDepResult::getDef(Inst); 587139f7f9bSDimitry Andric if (isInvariantLoad) 588139f7f9bSDimitry Andric continue; 5892754fe60SDimitry Andric return MemDepResult::getClobber(Inst); 590f22ef01cSRoman Divacky } 591f22ef01cSRoman Divacky 592f22ef01cSRoman Divacky // If this is an allocation, and if we know that the accessed pointer is to 593f22ef01cSRoman Divacky // the allocation, return Def. This means that there is no dependence and 594f22ef01cSRoman Divacky // the access can be optimized based on that. For example, a load could 595f22ef01cSRoman Divacky // turn into undef. 596f22ef01cSRoman Divacky // Note: Only determine this to be a malloc if Inst is the malloc call, not 597f22ef01cSRoman Divacky // a subsequent bitcast of the malloc call result. There can be stores to 598f22ef01cSRoman Divacky // the malloced memory between the malloc call and its bitcast uses, and we 599f22ef01cSRoman Divacky // need to continue scanning until the malloc call. 6003861d79fSDimitry Andric const TargetLibraryInfo *TLI = AA->getTargetLibraryInfo(); 6013861d79fSDimitry Andric if (isa<AllocaInst>(Inst) || isNoAliasFn(Inst, TLI)) { 60291bc56edSDimitry Andric const Value *AccessPtr = GetUnderlyingObject(MemLoc.Ptr, DL); 603f22ef01cSRoman Divacky 6043b0f4066SDimitry Andric if (AccessPtr == Inst || AA->isMustAlias(Inst, AccessPtr)) 605f22ef01cSRoman Divacky return MemDepResult::getDef(Inst); 606ff0cc061SDimitry Andric if (isInvariantLoad) 607ff0cc061SDimitry Andric continue; 6083861d79fSDimitry Andric // Be conservative if the accessed pointer may alias the allocation. 6093861d79fSDimitry Andric if (AA->alias(Inst, AccessPtr) != AliasAnalysis::NoAlias) 6103861d79fSDimitry Andric return MemDepResult::getClobber(Inst); 6113861d79fSDimitry Andric // If the allocation is not aliased and does not read memory (like 6123861d79fSDimitry Andric // strdup), it is safe to ignore. 6133861d79fSDimitry Andric if (isa<AllocaInst>(Inst) || 6143861d79fSDimitry Andric isMallocLikeFn(Inst, TLI) || isCallocLikeFn(Inst, TLI)) 615f22ef01cSRoman Divacky continue; 616f22ef01cSRoman Divacky } 617f22ef01cSRoman Divacky 618ff0cc061SDimitry Andric if (isInvariantLoad) 619ff0cc061SDimitry Andric continue; 620ff0cc061SDimitry Andric 621f22ef01cSRoman Divacky // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer. 6227ae0e2c9SDimitry Andric AliasAnalysis::ModRefResult MR = AA->getModRefInfo(Inst, MemLoc); 6237ae0e2c9SDimitry Andric // If necessary, perform additional analysis. 6247ae0e2c9SDimitry Andric if (MR == AliasAnalysis::ModRef) 6257ae0e2c9SDimitry Andric MR = AA->callCapturesBefore(Inst, MemLoc, DT); 6267ae0e2c9SDimitry Andric switch (MR) { 627f22ef01cSRoman Divacky case AliasAnalysis::NoModRef: 628f22ef01cSRoman Divacky // If the call has no effect on the queried pointer, just ignore it. 629f22ef01cSRoman Divacky continue; 630f22ef01cSRoman Divacky case AliasAnalysis::Mod: 631f22ef01cSRoman Divacky return MemDepResult::getClobber(Inst); 632f22ef01cSRoman Divacky case AliasAnalysis::Ref: 633f22ef01cSRoman Divacky // If the call is known to never store to the pointer, and if this is a 634f22ef01cSRoman Divacky // load query, we can safely ignore it (scan past it). 635f22ef01cSRoman Divacky if (isLoad) 636f22ef01cSRoman Divacky continue; 637f22ef01cSRoman Divacky default: 638f22ef01cSRoman Divacky // Otherwise, there is a potential dependence. Return a clobber. 639f22ef01cSRoman Divacky return MemDepResult::getClobber(Inst); 640f22ef01cSRoman Divacky } 641f22ef01cSRoman Divacky } 642f22ef01cSRoman Divacky 64317a519f9SDimitry Andric // No dependence found. If this is the entry block of the function, it is 64417a519f9SDimitry Andric // unknown, otherwise it is non-local. 645f22ef01cSRoman Divacky if (BB != &BB->getParent()->getEntryBlock()) 646f22ef01cSRoman Divacky return MemDepResult::getNonLocal(); 6476122f3e6SDimitry Andric return MemDepResult::getNonFuncLocal(); 648f22ef01cSRoman Divacky } 649f22ef01cSRoman Divacky 650f22ef01cSRoman Divacky /// getDependency - Return the instruction on which a memory operation 651f22ef01cSRoman Divacky /// depends. 652f22ef01cSRoman Divacky MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) { 653f22ef01cSRoman Divacky Instruction *ScanPos = QueryInst; 654f22ef01cSRoman Divacky 655f22ef01cSRoman Divacky // Check for a cached result 656f22ef01cSRoman Divacky MemDepResult &LocalCache = LocalDeps[QueryInst]; 657f22ef01cSRoman Divacky 658f22ef01cSRoman Divacky // If the cached entry is non-dirty, just return it. Note that this depends 659f22ef01cSRoman Divacky // on MemDepResult's default constructing to 'dirty'. 660f22ef01cSRoman Divacky if (!LocalCache.isDirty()) 661f22ef01cSRoman Divacky return LocalCache; 662f22ef01cSRoman Divacky 663f22ef01cSRoman Divacky // Otherwise, if we have a dirty entry, we know we can start the scan at that 664f22ef01cSRoman Divacky // instruction, which may save us some work. 665f22ef01cSRoman Divacky if (Instruction *Inst = LocalCache.getInst()) { 666f22ef01cSRoman Divacky ScanPos = Inst; 667f22ef01cSRoman Divacky 668f22ef01cSRoman Divacky RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst); 669f22ef01cSRoman Divacky } 670f22ef01cSRoman Divacky 671f22ef01cSRoman Divacky BasicBlock *QueryParent = QueryInst->getParent(); 672f22ef01cSRoman Divacky 673f22ef01cSRoman Divacky // Do the scan. 674f22ef01cSRoman Divacky if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) { 67517a519f9SDimitry Andric // No dependence found. If this is the entry block of the function, it is 67617a519f9SDimitry Andric // unknown, otherwise it is non-local. 677f22ef01cSRoman Divacky if (QueryParent != &QueryParent->getParent()->getEntryBlock()) 678f22ef01cSRoman Divacky LocalCache = MemDepResult::getNonLocal(); 679f22ef01cSRoman Divacky else 6806122f3e6SDimitry Andric LocalCache = MemDepResult::getNonFuncLocal(); 6812754fe60SDimitry Andric } else { 6822754fe60SDimitry Andric AliasAnalysis::Location MemLoc; 6832754fe60SDimitry Andric AliasAnalysis::ModRefResult MR = GetLocation(QueryInst, MemLoc, AA); 6842754fe60SDimitry Andric if (MemLoc.Ptr) { 6852754fe60SDimitry Andric // If we can do a pointer scan, make it happen. 6862754fe60SDimitry Andric bool isLoad = !(MR & AliasAnalysis::Mod); 6872754fe60SDimitry Andric if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(QueryInst)) 688bd5abe19SDimitry Andric isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_start; 689f22ef01cSRoman Divacky 6902754fe60SDimitry Andric LocalCache = getPointerDependencyFrom(MemLoc, isLoad, ScanPos, 691139f7f9bSDimitry Andric QueryParent, QueryInst); 6922754fe60SDimitry Andric } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) { 693e580952dSDimitry Andric CallSite QueryCS(QueryInst); 694f22ef01cSRoman Divacky bool isReadOnly = AA->onlyReadsMemory(QueryCS); 695f22ef01cSRoman Divacky LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos, 696f22ef01cSRoman Divacky QueryParent); 6972754fe60SDimitry Andric } else 698f22ef01cSRoman Divacky // Non-memory instruction. 69917a519f9SDimitry Andric LocalCache = MemDepResult::getUnknown(); 700f22ef01cSRoman Divacky } 701f22ef01cSRoman Divacky 702f22ef01cSRoman Divacky // Remember the result! 703f22ef01cSRoman Divacky if (Instruction *I = LocalCache.getInst()) 704f22ef01cSRoman Divacky ReverseLocalDeps[I].insert(QueryInst); 705f22ef01cSRoman Divacky 706f22ef01cSRoman Divacky return LocalCache; 707f22ef01cSRoman Divacky } 708f22ef01cSRoman Divacky 709f22ef01cSRoman Divacky #ifndef NDEBUG 710f22ef01cSRoman Divacky /// AssertSorted - This method is used when -debug is specified to verify that 711f22ef01cSRoman Divacky /// cache arrays are properly kept sorted. 712f22ef01cSRoman Divacky static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache, 713f22ef01cSRoman Divacky int Count = -1) { 714f22ef01cSRoman Divacky if (Count == -1) Count = Cache.size(); 715f22ef01cSRoman Divacky if (Count == 0) return; 716f22ef01cSRoman Divacky 717f22ef01cSRoman Divacky for (unsigned i = 1; i != unsigned(Count); ++i) 718f22ef01cSRoman Divacky assert(!(Cache[i] < Cache[i-1]) && "Cache isn't sorted!"); 719f22ef01cSRoman Divacky } 720f22ef01cSRoman Divacky #endif 721f22ef01cSRoman Divacky 722f22ef01cSRoman Divacky /// getNonLocalCallDependency - Perform a full dependency query for the 723f22ef01cSRoman Divacky /// specified call, returning the set of blocks that the value is 724f22ef01cSRoman Divacky /// potentially live across. The returned set of results will include a 725f22ef01cSRoman Divacky /// "NonLocal" result for all blocks where the value is live across. 726f22ef01cSRoman Divacky /// 727f22ef01cSRoman Divacky /// This method assumes the instruction returns a "NonLocal" dependency 728f22ef01cSRoman Divacky /// within its own block. 729f22ef01cSRoman Divacky /// 730f22ef01cSRoman Divacky /// This returns a reference to an internal data structure that may be 731f22ef01cSRoman Divacky /// invalidated on the next non-local query or when an instruction is 732f22ef01cSRoman Divacky /// removed. Clients must copy this data if they want it around longer than 733f22ef01cSRoman Divacky /// that. 734f22ef01cSRoman Divacky const MemoryDependenceAnalysis::NonLocalDepInfo & 735f22ef01cSRoman Divacky MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) { 736f22ef01cSRoman Divacky assert(getDependency(QueryCS.getInstruction()).isNonLocal() && 737f22ef01cSRoman Divacky "getNonLocalCallDependency should only be used on calls with non-local deps!"); 738f22ef01cSRoman Divacky PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()]; 739f22ef01cSRoman Divacky NonLocalDepInfo &Cache = CacheP.first; 740f22ef01cSRoman Divacky 741f22ef01cSRoman Divacky /// DirtyBlocks - This is the set of blocks that need to be recomputed. In 742f22ef01cSRoman Divacky /// the cached case, this can happen due to instructions being deleted etc. In 743f22ef01cSRoman Divacky /// the uncached case, this starts out as the set of predecessors we care 744f22ef01cSRoman Divacky /// about. 745f22ef01cSRoman Divacky SmallVector<BasicBlock*, 32> DirtyBlocks; 746f22ef01cSRoman Divacky 747f22ef01cSRoman Divacky if (!Cache.empty()) { 748f22ef01cSRoman Divacky // Okay, we have a cache entry. If we know it is not dirty, just return it 749f22ef01cSRoman Divacky // with no computation. 750f22ef01cSRoman Divacky if (!CacheP.second) { 751ffd1746dSEd Schouten ++NumCacheNonLocal; 752f22ef01cSRoman Divacky return Cache; 753f22ef01cSRoman Divacky } 754f22ef01cSRoman Divacky 755f22ef01cSRoman Divacky // If we already have a partially computed set of results, scan them to 756f22ef01cSRoman Divacky // determine what is dirty, seeding our initial DirtyBlocks worklist. 757f22ef01cSRoman Divacky for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end(); 758f22ef01cSRoman Divacky I != E; ++I) 759f22ef01cSRoman Divacky if (I->getResult().isDirty()) 760f22ef01cSRoman Divacky DirtyBlocks.push_back(I->getBB()); 761f22ef01cSRoman Divacky 762f22ef01cSRoman Divacky // Sort the cache so that we can do fast binary search lookups below. 763f22ef01cSRoman Divacky std::sort(Cache.begin(), Cache.end()); 764f22ef01cSRoman Divacky 765f22ef01cSRoman Divacky ++NumCacheDirtyNonLocal; 766f22ef01cSRoman Divacky //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: " 767f22ef01cSRoman Divacky // << Cache.size() << " cached: " << *QueryInst; 768f22ef01cSRoman Divacky } else { 769f22ef01cSRoman Divacky // Seed DirtyBlocks with each of the preds of QueryInst's block. 770f22ef01cSRoman Divacky BasicBlock *QueryBB = QueryCS.getInstruction()->getParent(); 771ff0cc061SDimitry Andric for (BasicBlock *Pred : PredCache.get(QueryBB)) 772ff0cc061SDimitry Andric DirtyBlocks.push_back(Pred); 773ffd1746dSEd Schouten ++NumUncacheNonLocal; 774f22ef01cSRoman Divacky } 775f22ef01cSRoman Divacky 776f22ef01cSRoman Divacky // isReadonlyCall - If this is a read-only call, we can be more aggressive. 777f22ef01cSRoman Divacky bool isReadonlyCall = AA->onlyReadsMemory(QueryCS); 778f22ef01cSRoman Divacky 779f22ef01cSRoman Divacky SmallPtrSet<BasicBlock*, 64> Visited; 780f22ef01cSRoman Divacky 781f22ef01cSRoman Divacky unsigned NumSortedEntries = Cache.size(); 782f22ef01cSRoman Divacky DEBUG(AssertSorted(Cache)); 783f22ef01cSRoman Divacky 784f22ef01cSRoman Divacky // Iterate while we still have blocks to update. 785f22ef01cSRoman Divacky while (!DirtyBlocks.empty()) { 786f22ef01cSRoman Divacky BasicBlock *DirtyBB = DirtyBlocks.back(); 787f22ef01cSRoman Divacky DirtyBlocks.pop_back(); 788f22ef01cSRoman Divacky 789f22ef01cSRoman Divacky // Already processed this block? 79039d628a0SDimitry Andric if (!Visited.insert(DirtyBB).second) 791f22ef01cSRoman Divacky continue; 792f22ef01cSRoman Divacky 793f22ef01cSRoman Divacky // Do a binary search to see if we already have an entry for this block in 794f22ef01cSRoman Divacky // the cache set. If so, find it. 795f22ef01cSRoman Divacky DEBUG(AssertSorted(Cache, NumSortedEntries)); 796f22ef01cSRoman Divacky NonLocalDepInfo::iterator Entry = 797f22ef01cSRoman Divacky std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries, 798f22ef01cSRoman Divacky NonLocalDepEntry(DirtyBB)); 79991bc56edSDimitry Andric if (Entry != Cache.begin() && std::prev(Entry)->getBB() == DirtyBB) 800f22ef01cSRoman Divacky --Entry; 801f22ef01cSRoman Divacky 80291bc56edSDimitry Andric NonLocalDepEntry *ExistingResult = nullptr; 803f22ef01cSRoman Divacky if (Entry != Cache.begin()+NumSortedEntries && 804f22ef01cSRoman Divacky Entry->getBB() == DirtyBB) { 805f22ef01cSRoman Divacky // If we already have an entry, and if it isn't already dirty, the block 806f22ef01cSRoman Divacky // is done. 807f22ef01cSRoman Divacky if (!Entry->getResult().isDirty()) 808f22ef01cSRoman Divacky continue; 809f22ef01cSRoman Divacky 810f22ef01cSRoman Divacky // Otherwise, remember this slot so we can update the value. 811f22ef01cSRoman Divacky ExistingResult = &*Entry; 812f22ef01cSRoman Divacky } 813f22ef01cSRoman Divacky 814f22ef01cSRoman Divacky // If the dirty entry has a pointer, start scanning from it so we don't have 815f22ef01cSRoman Divacky // to rescan the entire block. 816f22ef01cSRoman Divacky BasicBlock::iterator ScanPos = DirtyBB->end(); 817f22ef01cSRoman Divacky if (ExistingResult) { 818f22ef01cSRoman Divacky if (Instruction *Inst = ExistingResult->getResult().getInst()) { 819f22ef01cSRoman Divacky ScanPos = Inst; 820f22ef01cSRoman Divacky // We're removing QueryInst's use of Inst. 821f22ef01cSRoman Divacky RemoveFromReverseMap(ReverseNonLocalDeps, Inst, 822f22ef01cSRoman Divacky QueryCS.getInstruction()); 823f22ef01cSRoman Divacky } 824f22ef01cSRoman Divacky } 825f22ef01cSRoman Divacky 826f22ef01cSRoman Divacky // Find out if this block has a local dependency for QueryInst. 827f22ef01cSRoman Divacky MemDepResult Dep; 828f22ef01cSRoman Divacky 829f22ef01cSRoman Divacky if (ScanPos != DirtyBB->begin()) { 830f22ef01cSRoman Divacky Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB); 831f22ef01cSRoman Divacky } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) { 832f22ef01cSRoman Divacky // No dependence found. If this is the entry block of the function, it is 83317a519f9SDimitry Andric // a clobber, otherwise it is unknown. 834f22ef01cSRoman Divacky Dep = MemDepResult::getNonLocal(); 835f22ef01cSRoman Divacky } else { 8366122f3e6SDimitry Andric Dep = MemDepResult::getNonFuncLocal(); 837f22ef01cSRoman Divacky } 838f22ef01cSRoman Divacky 839f22ef01cSRoman Divacky // If we had a dirty entry for the block, update it. Otherwise, just add 840f22ef01cSRoman Divacky // a new entry. 841f22ef01cSRoman Divacky if (ExistingResult) 842f22ef01cSRoman Divacky ExistingResult->setResult(Dep); 843f22ef01cSRoman Divacky else 844f22ef01cSRoman Divacky Cache.push_back(NonLocalDepEntry(DirtyBB, Dep)); 845f22ef01cSRoman Divacky 846f22ef01cSRoman Divacky // If the block has a dependency (i.e. it isn't completely transparent to 847f22ef01cSRoman Divacky // the value), remember the association! 848f22ef01cSRoman Divacky if (!Dep.isNonLocal()) { 849f22ef01cSRoman Divacky // Keep the ReverseNonLocalDeps map up to date so we can efficiently 850f22ef01cSRoman Divacky // update this when we remove instructions. 851f22ef01cSRoman Divacky if (Instruction *Inst = Dep.getInst()) 852f22ef01cSRoman Divacky ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction()); 853f22ef01cSRoman Divacky } else { 854f22ef01cSRoman Divacky 855f22ef01cSRoman Divacky // If the block *is* completely transparent to the load, we need to check 856f22ef01cSRoman Divacky // the predecessors of this block. Add them to our worklist. 857ff0cc061SDimitry Andric for (BasicBlock *Pred : PredCache.get(DirtyBB)) 858ff0cc061SDimitry Andric DirtyBlocks.push_back(Pred); 859f22ef01cSRoman Divacky } 860f22ef01cSRoman Divacky } 861f22ef01cSRoman Divacky 862f22ef01cSRoman Divacky return Cache; 863f22ef01cSRoman Divacky } 864f22ef01cSRoman Divacky 865f22ef01cSRoman Divacky /// getNonLocalPointerDependency - Perform a full dependency query for an 866f22ef01cSRoman Divacky /// access to the specified (non-volatile) memory location, returning the 867f22ef01cSRoman Divacky /// set of instructions that either define or clobber the value. 868f22ef01cSRoman Divacky /// 869f22ef01cSRoman Divacky /// This method assumes the pointer has a "NonLocal" dependency within its 870f22ef01cSRoman Divacky /// own block. 871f22ef01cSRoman Divacky /// 872f22ef01cSRoman Divacky void MemoryDependenceAnalysis:: 87339d628a0SDimitry Andric getNonLocalPointerDependency(Instruction *QueryInst, 874f22ef01cSRoman Divacky SmallVectorImpl<NonLocalDepResult> &Result) { 875ff0cc061SDimitry Andric const AliasAnalysis::Location Loc = AA->getLocation(QueryInst); 87639d628a0SDimitry Andric bool isLoad = isa<LoadInst>(QueryInst); 87739d628a0SDimitry Andric BasicBlock *FromBB = QueryInst->getParent(); 87839d628a0SDimitry Andric assert(FromBB); 87939d628a0SDimitry Andric 8802754fe60SDimitry Andric assert(Loc.Ptr->getType()->isPointerTy() && 881f22ef01cSRoman Divacky "Can't get pointer deps of a non-pointer!"); 882f22ef01cSRoman Divacky Result.clear(); 883f22ef01cSRoman Divacky 88439d628a0SDimitry Andric // This routine does not expect to deal with volatile instructions. 88539d628a0SDimitry Andric // Doing so would require piping through the QueryInst all the way through. 88639d628a0SDimitry Andric // TODO: volatiles can't be elided, but they can be reordered with other 88739d628a0SDimitry Andric // non-volatile accesses. 888ff0cc061SDimitry Andric 88939d628a0SDimitry Andric // We currently give up on any instruction which is ordered, but we do handle 89039d628a0SDimitry Andric // atomic instructions which are unordered. 89139d628a0SDimitry Andric // TODO: Handle ordered instructions 89239d628a0SDimitry Andric auto isOrdered = [](Instruction *Inst) { 89339d628a0SDimitry Andric if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { 89439d628a0SDimitry Andric return !LI->isUnordered(); 89539d628a0SDimitry Andric } else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { 89639d628a0SDimitry Andric return !SI->isUnordered(); 89739d628a0SDimitry Andric } 89839d628a0SDimitry Andric return false; 89939d628a0SDimitry Andric }; 90039d628a0SDimitry Andric if (isVolatile(QueryInst) || isOrdered(QueryInst)) { 90139d628a0SDimitry Andric Result.push_back(NonLocalDepResult(FromBB, 90239d628a0SDimitry Andric MemDepResult::getUnknown(), 90339d628a0SDimitry Andric const_cast<Value *>(Loc.Ptr))); 90439d628a0SDimitry Andric return; 90539d628a0SDimitry Andric } 906ff0cc061SDimitry Andric const DataLayout &DL = FromBB->getModule()->getDataLayout(); 90739d628a0SDimitry Andric PHITransAddr Address(const_cast<Value *>(Loc.Ptr), DL, AC); 908f22ef01cSRoman Divacky 909f22ef01cSRoman Divacky // This is the set of blocks we've inspected, and the pointer we consider in 910f22ef01cSRoman Divacky // each block. Because of critical edges, we currently bail out if querying 911f22ef01cSRoman Divacky // a block with multiple different pointers. This can happen during PHI 912f22ef01cSRoman Divacky // translation. 913f22ef01cSRoman Divacky DenseMap<BasicBlock*, Value*> Visited; 914ff0cc061SDimitry Andric if (!getNonLocalPointerDepFromBB(QueryInst, Address, Loc, isLoad, FromBB, 915f22ef01cSRoman Divacky Result, Visited, true)) 916f22ef01cSRoman Divacky return; 917f22ef01cSRoman Divacky Result.clear(); 918f22ef01cSRoman Divacky Result.push_back(NonLocalDepResult(FromBB, 91917a519f9SDimitry Andric MemDepResult::getUnknown(), 9202754fe60SDimitry Andric const_cast<Value *>(Loc.Ptr))); 921f22ef01cSRoman Divacky } 922f22ef01cSRoman Divacky 923f22ef01cSRoman Divacky /// GetNonLocalInfoForBlock - Compute the memdep value for BB with 924f22ef01cSRoman Divacky /// Pointer/PointeeSize using either cached information in Cache or by doing a 925f22ef01cSRoman Divacky /// lookup (which may use dirty cache info if available). If we do a lookup, 926f22ef01cSRoman Divacky /// add the result to the cache. 927f22ef01cSRoman Divacky MemDepResult MemoryDependenceAnalysis:: 928ff0cc061SDimitry Andric GetNonLocalInfoForBlock(Instruction *QueryInst, 929ff0cc061SDimitry Andric const AliasAnalysis::Location &Loc, 930f22ef01cSRoman Divacky bool isLoad, BasicBlock *BB, 931f22ef01cSRoman Divacky NonLocalDepInfo *Cache, unsigned NumSortedEntries) { 932f22ef01cSRoman Divacky 933f22ef01cSRoman Divacky // Do a binary search to see if we already have an entry for this block in 934f22ef01cSRoman Divacky // the cache set. If so, find it. 935f22ef01cSRoman Divacky NonLocalDepInfo::iterator Entry = 936f22ef01cSRoman Divacky std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries, 937f22ef01cSRoman Divacky NonLocalDepEntry(BB)); 938f22ef01cSRoman Divacky if (Entry != Cache->begin() && (Entry-1)->getBB() == BB) 939f22ef01cSRoman Divacky --Entry; 940f22ef01cSRoman Divacky 94191bc56edSDimitry Andric NonLocalDepEntry *ExistingResult = nullptr; 942f22ef01cSRoman Divacky if (Entry != Cache->begin()+NumSortedEntries && Entry->getBB() == BB) 943f22ef01cSRoman Divacky ExistingResult = &*Entry; 944f22ef01cSRoman Divacky 945f22ef01cSRoman Divacky // If we have a cached entry, and it is non-dirty, use it as the value for 946f22ef01cSRoman Divacky // this dependency. 947f22ef01cSRoman Divacky if (ExistingResult && !ExistingResult->getResult().isDirty()) { 948f22ef01cSRoman Divacky ++NumCacheNonLocalPtr; 949f22ef01cSRoman Divacky return ExistingResult->getResult(); 950f22ef01cSRoman Divacky } 951f22ef01cSRoman Divacky 952f22ef01cSRoman Divacky // Otherwise, we have to scan for the value. If we have a dirty cache 953f22ef01cSRoman Divacky // entry, start scanning from its position, otherwise we scan from the end 954f22ef01cSRoman Divacky // of the block. 955f22ef01cSRoman Divacky BasicBlock::iterator ScanPos = BB->end(); 956f22ef01cSRoman Divacky if (ExistingResult && ExistingResult->getResult().getInst()) { 957f22ef01cSRoman Divacky assert(ExistingResult->getResult().getInst()->getParent() == BB && 958f22ef01cSRoman Divacky "Instruction invalidated?"); 959f22ef01cSRoman Divacky ++NumCacheDirtyNonLocalPtr; 960f22ef01cSRoman Divacky ScanPos = ExistingResult->getResult().getInst(); 961f22ef01cSRoman Divacky 962f22ef01cSRoman Divacky // Eliminating the dirty entry from 'Cache', so update the reverse info. 9632754fe60SDimitry Andric ValueIsLoadPair CacheKey(Loc.Ptr, isLoad); 964f22ef01cSRoman Divacky RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey); 965f22ef01cSRoman Divacky } else { 966f22ef01cSRoman Divacky ++NumUncacheNonLocalPtr; 967f22ef01cSRoman Divacky } 968f22ef01cSRoman Divacky 969f22ef01cSRoman Divacky // Scan the block for the dependency. 970ff0cc061SDimitry Andric MemDepResult Dep = getPointerDependencyFrom(Loc, isLoad, ScanPos, BB, 971ff0cc061SDimitry Andric QueryInst); 972f22ef01cSRoman Divacky 973f22ef01cSRoman Divacky // If we had a dirty entry for the block, update it. Otherwise, just add 974f22ef01cSRoman Divacky // a new entry. 975f22ef01cSRoman Divacky if (ExistingResult) 976f22ef01cSRoman Divacky ExistingResult->setResult(Dep); 977f22ef01cSRoman Divacky else 978f22ef01cSRoman Divacky Cache->push_back(NonLocalDepEntry(BB, Dep)); 979f22ef01cSRoman Divacky 980f22ef01cSRoman Divacky // If the block has a dependency (i.e. it isn't completely transparent to 981f22ef01cSRoman Divacky // the value), remember the reverse association because we just added it 982f22ef01cSRoman Divacky // to Cache! 9836122f3e6SDimitry Andric if (!Dep.isDef() && !Dep.isClobber()) 984f22ef01cSRoman Divacky return Dep; 985f22ef01cSRoman Divacky 986f22ef01cSRoman Divacky // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently 987f22ef01cSRoman Divacky // update MemDep when we remove instructions. 988f22ef01cSRoman Divacky Instruction *Inst = Dep.getInst(); 989f22ef01cSRoman Divacky assert(Inst && "Didn't depend on anything?"); 9902754fe60SDimitry Andric ValueIsLoadPair CacheKey(Loc.Ptr, isLoad); 991f22ef01cSRoman Divacky ReverseNonLocalPtrDeps[Inst].insert(CacheKey); 992f22ef01cSRoman Divacky return Dep; 993f22ef01cSRoman Divacky } 994f22ef01cSRoman Divacky 99539d628a0SDimitry Andric /// SortNonLocalDepInfoCache - Sort the NonLocalDepInfo cache, given a certain 996f22ef01cSRoman Divacky /// number of elements in the array that are already properly ordered. This is 997f22ef01cSRoman Divacky /// optimized for the case when only a few entries are added. 998f22ef01cSRoman Divacky static void 999f22ef01cSRoman Divacky SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache, 1000f22ef01cSRoman Divacky unsigned NumSortedEntries) { 1001f22ef01cSRoman Divacky switch (Cache.size() - NumSortedEntries) { 1002f22ef01cSRoman Divacky case 0: 1003f22ef01cSRoman Divacky // done, no new entries. 1004f22ef01cSRoman Divacky break; 1005f22ef01cSRoman Divacky case 2: { 1006f22ef01cSRoman Divacky // Two new entries, insert the last one into place. 1007f22ef01cSRoman Divacky NonLocalDepEntry Val = Cache.back(); 1008f22ef01cSRoman Divacky Cache.pop_back(); 1009f22ef01cSRoman Divacky MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry = 1010f22ef01cSRoman Divacky std::upper_bound(Cache.begin(), Cache.end()-1, Val); 1011f22ef01cSRoman Divacky Cache.insert(Entry, Val); 1012f22ef01cSRoman Divacky // FALL THROUGH. 1013f22ef01cSRoman Divacky } 1014f22ef01cSRoman Divacky case 1: 1015f22ef01cSRoman Divacky // One new entry, Just insert the new value at the appropriate position. 1016f22ef01cSRoman Divacky if (Cache.size() != 1) { 1017f22ef01cSRoman Divacky NonLocalDepEntry Val = Cache.back(); 1018f22ef01cSRoman Divacky Cache.pop_back(); 1019f22ef01cSRoman Divacky MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry = 1020f22ef01cSRoman Divacky std::upper_bound(Cache.begin(), Cache.end(), Val); 1021f22ef01cSRoman Divacky Cache.insert(Entry, Val); 1022f22ef01cSRoman Divacky } 1023f22ef01cSRoman Divacky break; 1024f22ef01cSRoman Divacky default: 1025f22ef01cSRoman Divacky // Added many values, do a full scale sort. 1026f22ef01cSRoman Divacky std::sort(Cache.begin(), Cache.end()); 1027f22ef01cSRoman Divacky break; 1028f22ef01cSRoman Divacky } 1029f22ef01cSRoman Divacky } 1030f22ef01cSRoman Divacky 1031f22ef01cSRoman Divacky /// getNonLocalPointerDepFromBB - Perform a dependency query based on 1032f22ef01cSRoman Divacky /// pointer/pointeesize starting at the end of StartBB. Add any clobber/def 1033f22ef01cSRoman Divacky /// results to the results vector and keep track of which blocks are visited in 1034f22ef01cSRoman Divacky /// 'Visited'. 1035f22ef01cSRoman Divacky /// 1036f22ef01cSRoman Divacky /// This has special behavior for the first block queries (when SkipFirstBlock 1037f22ef01cSRoman Divacky /// is true). In this special case, it ignores the contents of the specified 1038f22ef01cSRoman Divacky /// block and starts returning dependence info for its predecessors. 1039f22ef01cSRoman Divacky /// 1040f22ef01cSRoman Divacky /// This function returns false on success, or true to indicate that it could 1041f22ef01cSRoman Divacky /// not compute dependence information for some reason. This should be treated 1042f22ef01cSRoman Divacky /// as a clobber dependence on the first instruction in the predecessor block. 1043f22ef01cSRoman Divacky bool MemoryDependenceAnalysis:: 1044ff0cc061SDimitry Andric getNonLocalPointerDepFromBB(Instruction *QueryInst, 1045ff0cc061SDimitry Andric const PHITransAddr &Pointer, 10462754fe60SDimitry Andric const AliasAnalysis::Location &Loc, 1047f22ef01cSRoman Divacky bool isLoad, BasicBlock *StartBB, 1048f22ef01cSRoman Divacky SmallVectorImpl<NonLocalDepResult> &Result, 1049f22ef01cSRoman Divacky DenseMap<BasicBlock*, Value*> &Visited, 1050f22ef01cSRoman Divacky bool SkipFirstBlock) { 1051f22ef01cSRoman Divacky // Look up the cached info for Pointer. 1052f22ef01cSRoman Divacky ValueIsLoadPair CacheKey(Pointer.getAddr(), isLoad); 1053f22ef01cSRoman Divacky 10542754fe60SDimitry Andric // Set up a temporary NLPI value. If the map doesn't yet have an entry for 10552754fe60SDimitry Andric // CacheKey, this value will be inserted as the associated value. Otherwise, 10562754fe60SDimitry Andric // it'll be ignored, and we'll have to check to see if the cached size and 105739d628a0SDimitry Andric // aa tags are consistent with the current query. 10582754fe60SDimitry Andric NonLocalPointerInfo InitialNLPI; 10592754fe60SDimitry Andric InitialNLPI.Size = Loc.Size; 106039d628a0SDimitry Andric InitialNLPI.AATags = Loc.AATags; 10612754fe60SDimitry Andric 10622754fe60SDimitry Andric // Get the NLPI for CacheKey, inserting one into the map if it doesn't 10632754fe60SDimitry Andric // already have one. 10642754fe60SDimitry Andric std::pair<CachedNonLocalPointerInfo::iterator, bool> Pair = 10652754fe60SDimitry Andric NonLocalPointerDeps.insert(std::make_pair(CacheKey, InitialNLPI)); 10662754fe60SDimitry Andric NonLocalPointerInfo *CacheInfo = &Pair.first->second; 10672754fe60SDimitry Andric 10682754fe60SDimitry Andric // If we already have a cache entry for this CacheKey, we may need to do some 10692754fe60SDimitry Andric // work to reconcile the cache entry and the current query. 10702754fe60SDimitry Andric if (!Pair.second) { 10712754fe60SDimitry Andric if (CacheInfo->Size < Loc.Size) { 10722754fe60SDimitry Andric // The query's Size is greater than the cached one. Throw out the 10737ae0e2c9SDimitry Andric // cached data and proceed with the query at the greater size. 10742754fe60SDimitry Andric CacheInfo->Pair = BBSkipFirstBlockPair(); 10752754fe60SDimitry Andric CacheInfo->Size = Loc.Size; 10762754fe60SDimitry Andric for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(), 10772754fe60SDimitry Andric DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI) 10782754fe60SDimitry Andric if (Instruction *Inst = DI->getResult().getInst()) 10792754fe60SDimitry Andric RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey); 10802754fe60SDimitry Andric CacheInfo->NonLocalDeps.clear(); 10812754fe60SDimitry Andric } else if (CacheInfo->Size > Loc.Size) { 10822754fe60SDimitry Andric // This query's Size is less than the cached one. Conservatively restart 10832754fe60SDimitry Andric // the query using the greater size. 1084ff0cc061SDimitry Andric return getNonLocalPointerDepFromBB(QueryInst, Pointer, 10852754fe60SDimitry Andric Loc.getWithNewSize(CacheInfo->Size), 10862754fe60SDimitry Andric isLoad, StartBB, Result, Visited, 10872754fe60SDimitry Andric SkipFirstBlock); 10882754fe60SDimitry Andric } 10892754fe60SDimitry Andric 109039d628a0SDimitry Andric // If the query's AATags are inconsistent with the cached one, 10912754fe60SDimitry Andric // conservatively throw out the cached data and restart the query with 10922754fe60SDimitry Andric // no tag if needed. 109339d628a0SDimitry Andric if (CacheInfo->AATags != Loc.AATags) { 109439d628a0SDimitry Andric if (CacheInfo->AATags) { 10952754fe60SDimitry Andric CacheInfo->Pair = BBSkipFirstBlockPair(); 109639d628a0SDimitry Andric CacheInfo->AATags = AAMDNodes(); 10972754fe60SDimitry Andric for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(), 10982754fe60SDimitry Andric DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI) 10992754fe60SDimitry Andric if (Instruction *Inst = DI->getResult().getInst()) 11002754fe60SDimitry Andric RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey); 11012754fe60SDimitry Andric CacheInfo->NonLocalDeps.clear(); 11022754fe60SDimitry Andric } 110339d628a0SDimitry Andric if (Loc.AATags) 1104ff0cc061SDimitry Andric return getNonLocalPointerDepFromBB(QueryInst, 1105ff0cc061SDimitry Andric Pointer, Loc.getWithoutAATags(), 11062754fe60SDimitry Andric isLoad, StartBB, Result, Visited, 11072754fe60SDimitry Andric SkipFirstBlock); 11082754fe60SDimitry Andric } 11092754fe60SDimitry Andric } 11102754fe60SDimitry Andric 11112754fe60SDimitry Andric NonLocalDepInfo *Cache = &CacheInfo->NonLocalDeps; 1112f22ef01cSRoman Divacky 1113f22ef01cSRoman Divacky // If we have valid cached information for exactly the block we are 1114f22ef01cSRoman Divacky // investigating, just return it with no recomputation. 11152754fe60SDimitry Andric if (CacheInfo->Pair == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) { 1116f22ef01cSRoman Divacky // We have a fully cached result for this query then we can just return the 1117f22ef01cSRoman Divacky // cached results and populate the visited set. However, we have to verify 1118f22ef01cSRoman Divacky // that we don't already have conflicting results for these blocks. Check 1119f22ef01cSRoman Divacky // to ensure that if a block in the results set is in the visited set that 1120f22ef01cSRoman Divacky // it was for the same pointer query. 1121f22ef01cSRoman Divacky if (!Visited.empty()) { 1122f22ef01cSRoman Divacky for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end(); 1123f22ef01cSRoman Divacky I != E; ++I) { 1124f22ef01cSRoman Divacky DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->getBB()); 1125f22ef01cSRoman Divacky if (VI == Visited.end() || VI->second == Pointer.getAddr()) 1126f22ef01cSRoman Divacky continue; 1127f22ef01cSRoman Divacky 1128f22ef01cSRoman Divacky // We have a pointer mismatch in a block. Just return clobber, saying 1129f22ef01cSRoman Divacky // that something was clobbered in this result. We could also do a 1130f22ef01cSRoman Divacky // non-fully cached query, but there is little point in doing this. 1131f22ef01cSRoman Divacky return true; 1132f22ef01cSRoman Divacky } 1133f22ef01cSRoman Divacky } 1134f22ef01cSRoman Divacky 1135f22ef01cSRoman Divacky Value *Addr = Pointer.getAddr(); 1136f22ef01cSRoman Divacky for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end(); 1137f22ef01cSRoman Divacky I != E; ++I) { 1138f22ef01cSRoman Divacky Visited.insert(std::make_pair(I->getBB(), Addr)); 1139284c1978SDimitry Andric if (I->getResult().isNonLocal()) { 1140284c1978SDimitry Andric continue; 1141284c1978SDimitry Andric } 1142284c1978SDimitry Andric 1143284c1978SDimitry Andric if (!DT) { 1144284c1978SDimitry Andric Result.push_back(NonLocalDepResult(I->getBB(), 1145284c1978SDimitry Andric MemDepResult::getUnknown(), 1146284c1978SDimitry Andric Addr)); 1147284c1978SDimitry Andric } else if (DT->isReachableFromEntry(I->getBB())) { 1148f22ef01cSRoman Divacky Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(), Addr)); 1149f22ef01cSRoman Divacky } 1150284c1978SDimitry Andric } 1151f22ef01cSRoman Divacky ++NumCacheCompleteNonLocalPtr; 1152f22ef01cSRoman Divacky return false; 1153f22ef01cSRoman Divacky } 1154f22ef01cSRoman Divacky 1155f22ef01cSRoman Divacky // Otherwise, either this is a new block, a block with an invalid cache 1156f22ef01cSRoman Divacky // pointer or one that we're about to invalidate by putting more info into it 1157f22ef01cSRoman Divacky // than its valid cache info. If empty, the result will be valid cache info, 1158f22ef01cSRoman Divacky // otherwise it isn't. 1159f22ef01cSRoman Divacky if (Cache->empty()) 11602754fe60SDimitry Andric CacheInfo->Pair = BBSkipFirstBlockPair(StartBB, SkipFirstBlock); 1161f22ef01cSRoman Divacky else 11622754fe60SDimitry Andric CacheInfo->Pair = BBSkipFirstBlockPair(); 1163f22ef01cSRoman Divacky 1164f22ef01cSRoman Divacky SmallVector<BasicBlock*, 32> Worklist; 1165f22ef01cSRoman Divacky Worklist.push_back(StartBB); 1166f22ef01cSRoman Divacky 1167bd5abe19SDimitry Andric // PredList used inside loop. 1168bd5abe19SDimitry Andric SmallVector<std::pair<BasicBlock*, PHITransAddr>, 16> PredList; 1169bd5abe19SDimitry Andric 1170f22ef01cSRoman Divacky // Keep track of the entries that we know are sorted. Previously cached 1171f22ef01cSRoman Divacky // entries will all be sorted. The entries we add we only sort on demand (we 1172f22ef01cSRoman Divacky // don't insert every element into its sorted position). We know that we 1173f22ef01cSRoman Divacky // won't get any reuse from currently inserted values, because we don't 1174f22ef01cSRoman Divacky // revisit blocks after we insert info for them. 1175f22ef01cSRoman Divacky unsigned NumSortedEntries = Cache->size(); 1176f22ef01cSRoman Divacky DEBUG(AssertSorted(*Cache)); 1177f22ef01cSRoman Divacky 1178f22ef01cSRoman Divacky while (!Worklist.empty()) { 1179f22ef01cSRoman Divacky BasicBlock *BB = Worklist.pop_back_val(); 1180f22ef01cSRoman Divacky 118139d628a0SDimitry Andric // If we do process a large number of blocks it becomes very expensive and 118239d628a0SDimitry Andric // likely it isn't worth worrying about 118339d628a0SDimitry Andric if (Result.size() > NumResultsLimit) { 118439d628a0SDimitry Andric Worklist.clear(); 118539d628a0SDimitry Andric // Sort it now (if needed) so that recursive invocations of 118639d628a0SDimitry Andric // getNonLocalPointerDepFromBB and other routines that could reuse the 118739d628a0SDimitry Andric // cache value will only see properly sorted cache arrays. 118839d628a0SDimitry Andric if (Cache && NumSortedEntries != Cache->size()) { 118939d628a0SDimitry Andric SortNonLocalDepInfoCache(*Cache, NumSortedEntries); 119039d628a0SDimitry Andric } 119139d628a0SDimitry Andric // Since we bail out, the "Cache" set won't contain all of the 119239d628a0SDimitry Andric // results for the query. This is ok (we can still use it to accelerate 119339d628a0SDimitry Andric // specific block queries) but we can't do the fastpath "return all 119439d628a0SDimitry Andric // results from the set". Clear out the indicator for this. 119539d628a0SDimitry Andric CacheInfo->Pair = BBSkipFirstBlockPair(); 119639d628a0SDimitry Andric return true; 119739d628a0SDimitry Andric } 119839d628a0SDimitry Andric 1199f22ef01cSRoman Divacky // Skip the first block if we have it. 1200f22ef01cSRoman Divacky if (!SkipFirstBlock) { 1201f22ef01cSRoman Divacky // Analyze the dependency of *Pointer in FromBB. See if we already have 1202f22ef01cSRoman Divacky // been here. 1203f22ef01cSRoman Divacky assert(Visited.count(BB) && "Should check 'visited' before adding to WL"); 1204f22ef01cSRoman Divacky 1205f22ef01cSRoman Divacky // Get the dependency info for Pointer in BB. If we have cached 1206f22ef01cSRoman Divacky // information, we will use it, otherwise we compute it. 1207f22ef01cSRoman Divacky DEBUG(AssertSorted(*Cache, NumSortedEntries)); 1208ff0cc061SDimitry Andric MemDepResult Dep = GetNonLocalInfoForBlock(QueryInst, 1209ff0cc061SDimitry Andric Loc, isLoad, BB, Cache, 1210f22ef01cSRoman Divacky NumSortedEntries); 1211f22ef01cSRoman Divacky 1212f22ef01cSRoman Divacky // If we got a Def or Clobber, add this to the list of results. 1213284c1978SDimitry Andric if (!Dep.isNonLocal()) { 1214284c1978SDimitry Andric if (!DT) { 1215284c1978SDimitry Andric Result.push_back(NonLocalDepResult(BB, 1216284c1978SDimitry Andric MemDepResult::getUnknown(), 1217284c1978SDimitry Andric Pointer.getAddr())); 1218284c1978SDimitry Andric continue; 1219284c1978SDimitry Andric } else if (DT->isReachableFromEntry(BB)) { 1220f22ef01cSRoman Divacky Result.push_back(NonLocalDepResult(BB, Dep, Pointer.getAddr())); 1221f22ef01cSRoman Divacky continue; 1222f22ef01cSRoman Divacky } 1223f22ef01cSRoman Divacky } 1224284c1978SDimitry Andric } 1225f22ef01cSRoman Divacky 1226f22ef01cSRoman Divacky // If 'Pointer' is an instruction defined in this block, then we need to do 1227f22ef01cSRoman Divacky // phi translation to change it into a value live in the predecessor block. 1228f22ef01cSRoman Divacky // If not, we just add the predecessors to the worklist and scan them with 1229f22ef01cSRoman Divacky // the same Pointer. 1230f22ef01cSRoman Divacky if (!Pointer.NeedsPHITranslationFromBlock(BB)) { 1231f22ef01cSRoman Divacky SkipFirstBlock = false; 1232bd5abe19SDimitry Andric SmallVector<BasicBlock*, 16> NewBlocks; 1233ff0cc061SDimitry Andric for (BasicBlock *Pred : PredCache.get(BB)) { 1234f22ef01cSRoman Divacky // Verify that we haven't looked at this block yet. 1235f22ef01cSRoman Divacky std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool> 1236ff0cc061SDimitry Andric InsertRes = Visited.insert(std::make_pair(Pred, Pointer.getAddr())); 1237f22ef01cSRoman Divacky if (InsertRes.second) { 1238f22ef01cSRoman Divacky // First time we've looked at *PI. 1239ff0cc061SDimitry Andric NewBlocks.push_back(Pred); 1240f22ef01cSRoman Divacky continue; 1241f22ef01cSRoman Divacky } 1242f22ef01cSRoman Divacky 1243f22ef01cSRoman Divacky // If we have seen this block before, but it was with a different 1244f22ef01cSRoman Divacky // pointer then we have a phi translation failure and we have to treat 1245f22ef01cSRoman Divacky // this as a clobber. 1246bd5abe19SDimitry Andric if (InsertRes.first->second != Pointer.getAddr()) { 1247bd5abe19SDimitry Andric // Make sure to clean up the Visited map before continuing on to 1248bd5abe19SDimitry Andric // PredTranslationFailure. 1249bd5abe19SDimitry Andric for (unsigned i = 0; i < NewBlocks.size(); i++) 1250bd5abe19SDimitry Andric Visited.erase(NewBlocks[i]); 1251f22ef01cSRoman Divacky goto PredTranslationFailure; 1252f22ef01cSRoman Divacky } 1253bd5abe19SDimitry Andric } 1254bd5abe19SDimitry Andric Worklist.append(NewBlocks.begin(), NewBlocks.end()); 1255f22ef01cSRoman Divacky continue; 1256f22ef01cSRoman Divacky } 1257f22ef01cSRoman Divacky 1258f22ef01cSRoman Divacky // We do need to do phi translation, if we know ahead of time we can't phi 1259f22ef01cSRoman Divacky // translate this value, don't even try. 1260f22ef01cSRoman Divacky if (!Pointer.IsPotentiallyPHITranslatable()) 1261f22ef01cSRoman Divacky goto PredTranslationFailure; 1262f22ef01cSRoman Divacky 1263f22ef01cSRoman Divacky // We may have added values to the cache list before this PHI translation. 1264f22ef01cSRoman Divacky // If so, we haven't done anything to ensure that the cache remains sorted. 1265f22ef01cSRoman Divacky // Sort it now (if needed) so that recursive invocations of 1266f22ef01cSRoman Divacky // getNonLocalPointerDepFromBB and other routines that could reuse the cache 1267f22ef01cSRoman Divacky // value will only see properly sorted cache arrays. 1268f22ef01cSRoman Divacky if (Cache && NumSortedEntries != Cache->size()) { 1269f22ef01cSRoman Divacky SortNonLocalDepInfoCache(*Cache, NumSortedEntries); 1270f22ef01cSRoman Divacky NumSortedEntries = Cache->size(); 1271f22ef01cSRoman Divacky } 127291bc56edSDimitry Andric Cache = nullptr; 1273f22ef01cSRoman Divacky 1274bd5abe19SDimitry Andric PredList.clear(); 1275ff0cc061SDimitry Andric for (BasicBlock *Pred : PredCache.get(BB)) { 1276bd5abe19SDimitry Andric PredList.push_back(std::make_pair(Pred, Pointer)); 1277f22ef01cSRoman Divacky 1278f22ef01cSRoman Divacky // Get the PHI translated pointer in this predecessor. This can fail if 1279f22ef01cSRoman Divacky // not translatable, in which case the getAddr() returns null. 1280bd5abe19SDimitry Andric PHITransAddr &PredPointer = PredList.back().second; 128191bc56edSDimitry Andric PredPointer.PHITranslateValue(BB, Pred, nullptr); 1282f22ef01cSRoman Divacky 1283f22ef01cSRoman Divacky Value *PredPtrVal = PredPointer.getAddr(); 1284f22ef01cSRoman Divacky 1285f22ef01cSRoman Divacky // Check to see if we have already visited this pred block with another 1286f22ef01cSRoman Divacky // pointer. If so, we can't do this lookup. This failure can occur 1287f22ef01cSRoman Divacky // with PHI translation when a critical edge exists and the PHI node in 1288f22ef01cSRoman Divacky // the successor translates to a pointer value different than the 1289f22ef01cSRoman Divacky // pointer the block was first analyzed with. 1290f22ef01cSRoman Divacky std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool> 1291f22ef01cSRoman Divacky InsertRes = Visited.insert(std::make_pair(Pred, PredPtrVal)); 1292f22ef01cSRoman Divacky 1293f22ef01cSRoman Divacky if (!InsertRes.second) { 1294bd5abe19SDimitry Andric // We found the pred; take it off the list of preds to visit. 1295bd5abe19SDimitry Andric PredList.pop_back(); 1296bd5abe19SDimitry Andric 1297f22ef01cSRoman Divacky // If the predecessor was visited with PredPtr, then we already did 1298f22ef01cSRoman Divacky // the analysis and can ignore it. 1299f22ef01cSRoman Divacky if (InsertRes.first->second == PredPtrVal) 1300f22ef01cSRoman Divacky continue; 1301f22ef01cSRoman Divacky 1302f22ef01cSRoman Divacky // Otherwise, the block was previously analyzed with a different 1303f22ef01cSRoman Divacky // pointer. We can't represent the result of this case, so we just 1304f22ef01cSRoman Divacky // treat this as a phi translation failure. 1305bd5abe19SDimitry Andric 1306bd5abe19SDimitry Andric // Make sure to clean up the Visited map before continuing on to 1307bd5abe19SDimitry Andric // PredTranslationFailure. 1308139f7f9bSDimitry Andric for (unsigned i = 0, n = PredList.size(); i < n; ++i) 1309bd5abe19SDimitry Andric Visited.erase(PredList[i].first); 1310bd5abe19SDimitry Andric 1311f22ef01cSRoman Divacky goto PredTranslationFailure; 1312f22ef01cSRoman Divacky } 1313bd5abe19SDimitry Andric } 1314f22ef01cSRoman Divacky 1315bd5abe19SDimitry Andric // Actually process results here; this need to be a separate loop to avoid 1316bd5abe19SDimitry Andric // calling getNonLocalPointerDepFromBB for blocks we don't want to return 1317bd5abe19SDimitry Andric // any results for. (getNonLocalPointerDepFromBB will modify our 1318bd5abe19SDimitry Andric // datastructures in ways the code after the PredTranslationFailure label 1319bd5abe19SDimitry Andric // doesn't expect.) 1320139f7f9bSDimitry Andric for (unsigned i = 0, n = PredList.size(); i < n; ++i) { 1321bd5abe19SDimitry Andric BasicBlock *Pred = PredList[i].first; 1322bd5abe19SDimitry Andric PHITransAddr &PredPointer = PredList[i].second; 1323bd5abe19SDimitry Andric Value *PredPtrVal = PredPointer.getAddr(); 1324bd5abe19SDimitry Andric 1325bd5abe19SDimitry Andric bool CanTranslate = true; 1326f22ef01cSRoman Divacky // If PHI translation was unable to find an available pointer in this 1327f22ef01cSRoman Divacky // predecessor, then we have to assume that the pointer is clobbered in 1328f22ef01cSRoman Divacky // that predecessor. We can still do PRE of the load, which would insert 1329f22ef01cSRoman Divacky // a computation of the pointer in this predecessor. 133091bc56edSDimitry Andric if (!PredPtrVal) 1331bd5abe19SDimitry Andric CanTranslate = false; 1332bd5abe19SDimitry Andric 1333bd5abe19SDimitry Andric // FIXME: it is entirely possible that PHI translating will end up with 1334bd5abe19SDimitry Andric // the same value. Consider PHI translating something like: 1335bd5abe19SDimitry Andric // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need* 1336bd5abe19SDimitry Andric // to recurse here, pedantically speaking. 1337bd5abe19SDimitry Andric 1338bd5abe19SDimitry Andric // If getNonLocalPointerDepFromBB fails here, that means the cached 1339bd5abe19SDimitry Andric // result conflicted with the Visited list; we have to conservatively 134017a519f9SDimitry Andric // assume it is unknown, but this also does not block PRE of the load. 1341bd5abe19SDimitry Andric if (!CanTranslate || 1342ff0cc061SDimitry Andric getNonLocalPointerDepFromBB(QueryInst, PredPointer, 1343bd5abe19SDimitry Andric Loc.getWithNewPtr(PredPtrVal), 1344bd5abe19SDimitry Andric isLoad, Pred, 1345bd5abe19SDimitry Andric Result, Visited)) { 1346f22ef01cSRoman Divacky // Add the entry to the Result list. 134717a519f9SDimitry Andric NonLocalDepResult Entry(Pred, MemDepResult::getUnknown(), PredPtrVal); 1348f22ef01cSRoman Divacky Result.push_back(Entry); 1349f22ef01cSRoman Divacky 1350f22ef01cSRoman Divacky // Since we had a phi translation failure, the cache for CacheKey won't 1351f22ef01cSRoman Divacky // include all of the entries that we need to immediately satisfy future 1352f22ef01cSRoman Divacky // queries. Mark this in NonLocalPointerDeps by setting the 1353f22ef01cSRoman Divacky // BBSkipFirstBlockPair pointer to null. This requires reuse of the 1354f22ef01cSRoman Divacky // cached value to do more work but not miss the phi trans failure. 13552754fe60SDimitry Andric NonLocalPointerInfo &NLPI = NonLocalPointerDeps[CacheKey]; 13562754fe60SDimitry Andric NLPI.Pair = BBSkipFirstBlockPair(); 1357f22ef01cSRoman Divacky continue; 1358f22ef01cSRoman Divacky } 1359f22ef01cSRoman Divacky } 1360f22ef01cSRoman Divacky 1361f22ef01cSRoman Divacky // Refresh the CacheInfo/Cache pointer so that it isn't invalidated. 1362f22ef01cSRoman Divacky CacheInfo = &NonLocalPointerDeps[CacheKey]; 13632754fe60SDimitry Andric Cache = &CacheInfo->NonLocalDeps; 1364f22ef01cSRoman Divacky NumSortedEntries = Cache->size(); 1365f22ef01cSRoman Divacky 1366f22ef01cSRoman Divacky // Since we did phi translation, the "Cache" set won't contain all of the 1367f22ef01cSRoman Divacky // results for the query. This is ok (we can still use it to accelerate 1368f22ef01cSRoman Divacky // specific block queries) but we can't do the fastpath "return all 1369f22ef01cSRoman Divacky // results from the set" Clear out the indicator for this. 13702754fe60SDimitry Andric CacheInfo->Pair = BBSkipFirstBlockPair(); 1371f22ef01cSRoman Divacky SkipFirstBlock = false; 1372f22ef01cSRoman Divacky continue; 1373f22ef01cSRoman Divacky 1374f22ef01cSRoman Divacky PredTranslationFailure: 1375bd5abe19SDimitry Andric // The following code is "failure"; we can't produce a sane translation 1376bd5abe19SDimitry Andric // for the given block. It assumes that we haven't modified any of 1377bd5abe19SDimitry Andric // our datastructures while processing the current block. 1378f22ef01cSRoman Divacky 137991bc56edSDimitry Andric if (!Cache) { 1380f22ef01cSRoman Divacky // Refresh the CacheInfo/Cache pointer if it got invalidated. 1381f22ef01cSRoman Divacky CacheInfo = &NonLocalPointerDeps[CacheKey]; 13822754fe60SDimitry Andric Cache = &CacheInfo->NonLocalDeps; 1383f22ef01cSRoman Divacky NumSortedEntries = Cache->size(); 1384f22ef01cSRoman Divacky } 1385f22ef01cSRoman Divacky 1386f22ef01cSRoman Divacky // Since we failed phi translation, the "Cache" set won't contain all of the 1387f22ef01cSRoman Divacky // results for the query. This is ok (we can still use it to accelerate 1388f22ef01cSRoman Divacky // specific block queries) but we can't do the fastpath "return all 1389f22ef01cSRoman Divacky // results from the set". Clear out the indicator for this. 13902754fe60SDimitry Andric CacheInfo->Pair = BBSkipFirstBlockPair(); 1391f22ef01cSRoman Divacky 139217a519f9SDimitry Andric // If *nothing* works, mark the pointer as unknown. 1393f22ef01cSRoman Divacky // 1394f22ef01cSRoman Divacky // If this is the magic first block, return this as a clobber of the whole 1395f22ef01cSRoman Divacky // incoming value. Since we can't phi translate to one of the predecessors, 1396f22ef01cSRoman Divacky // we have to bail out. 1397f22ef01cSRoman Divacky if (SkipFirstBlock) 1398f22ef01cSRoman Divacky return true; 1399f22ef01cSRoman Divacky 1400f22ef01cSRoman Divacky for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) { 1401f22ef01cSRoman Divacky assert(I != Cache->rend() && "Didn't find current block??"); 1402f22ef01cSRoman Divacky if (I->getBB() != BB) 1403f22ef01cSRoman Divacky continue; 1404f22ef01cSRoman Divacky 140539d628a0SDimitry Andric assert((I->getResult().isNonLocal() || !DT->isReachableFromEntry(BB)) && 1406f22ef01cSRoman Divacky "Should only be here with transparent block"); 140717a519f9SDimitry Andric I->setResult(MemDepResult::getUnknown()); 1408f22ef01cSRoman Divacky Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(), 1409f22ef01cSRoman Divacky Pointer.getAddr())); 1410f22ef01cSRoman Divacky break; 1411f22ef01cSRoman Divacky } 1412f22ef01cSRoman Divacky } 1413f22ef01cSRoman Divacky 1414f22ef01cSRoman Divacky // Okay, we're done now. If we added new values to the cache, re-sort it. 1415f22ef01cSRoman Divacky SortNonLocalDepInfoCache(*Cache, NumSortedEntries); 1416f22ef01cSRoman Divacky DEBUG(AssertSorted(*Cache)); 1417f22ef01cSRoman Divacky return false; 1418f22ef01cSRoman Divacky } 1419f22ef01cSRoman Divacky 1420f22ef01cSRoman Divacky /// RemoveCachedNonLocalPointerDependencies - If P exists in 1421f22ef01cSRoman Divacky /// CachedNonLocalPointerInfo, remove it. 1422f22ef01cSRoman Divacky void MemoryDependenceAnalysis:: 1423f22ef01cSRoman Divacky RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) { 1424f22ef01cSRoman Divacky CachedNonLocalPointerInfo::iterator It = 1425f22ef01cSRoman Divacky NonLocalPointerDeps.find(P); 1426f22ef01cSRoman Divacky if (It == NonLocalPointerDeps.end()) return; 1427f22ef01cSRoman Divacky 1428f22ef01cSRoman Divacky // Remove all of the entries in the BB->val map. This involves removing 1429f22ef01cSRoman Divacky // instructions from the reverse map. 14302754fe60SDimitry Andric NonLocalDepInfo &PInfo = It->second.NonLocalDeps; 1431f22ef01cSRoman Divacky 1432f22ef01cSRoman Divacky for (unsigned i = 0, e = PInfo.size(); i != e; ++i) { 1433f22ef01cSRoman Divacky Instruction *Target = PInfo[i].getResult().getInst(); 143491bc56edSDimitry Andric if (!Target) continue; // Ignore non-local dep results. 1435f22ef01cSRoman Divacky assert(Target->getParent() == PInfo[i].getBB()); 1436f22ef01cSRoman Divacky 1437f22ef01cSRoman Divacky // Eliminating the dirty entry from 'Cache', so update the reverse info. 1438f22ef01cSRoman Divacky RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P); 1439f22ef01cSRoman Divacky } 1440f22ef01cSRoman Divacky 1441f22ef01cSRoman Divacky // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo). 1442f22ef01cSRoman Divacky NonLocalPointerDeps.erase(It); 1443f22ef01cSRoman Divacky } 1444f22ef01cSRoman Divacky 1445f22ef01cSRoman Divacky 1446f22ef01cSRoman Divacky /// invalidateCachedPointerInfo - This method is used to invalidate cached 1447f22ef01cSRoman Divacky /// information about the specified pointer, because it may be too 1448f22ef01cSRoman Divacky /// conservative in memdep. This is an optional call that can be used when 1449f22ef01cSRoman Divacky /// the client detects an equivalence between the pointer and some other 1450f22ef01cSRoman Divacky /// value and replaces the other value with ptr. This can make Ptr available 1451f22ef01cSRoman Divacky /// in more places that cached info does not necessarily keep. 1452f22ef01cSRoman Divacky void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) { 1453f22ef01cSRoman Divacky // If Ptr isn't really a pointer, just ignore it. 1454f22ef01cSRoman Divacky if (!Ptr->getType()->isPointerTy()) return; 1455f22ef01cSRoman Divacky // Flush store info for the pointer. 1456f22ef01cSRoman Divacky RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false)); 1457f22ef01cSRoman Divacky // Flush load info for the pointer. 1458f22ef01cSRoman Divacky RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true)); 1459f22ef01cSRoman Divacky } 1460f22ef01cSRoman Divacky 1461f22ef01cSRoman Divacky /// invalidateCachedPredecessors - Clear the PredIteratorCache info. 1462f22ef01cSRoman Divacky /// This needs to be done when the CFG changes, e.g., due to splitting 1463f22ef01cSRoman Divacky /// critical edges. 1464f22ef01cSRoman Divacky void MemoryDependenceAnalysis::invalidateCachedPredecessors() { 1465ff0cc061SDimitry Andric PredCache.clear(); 1466f22ef01cSRoman Divacky } 1467f22ef01cSRoman Divacky 1468f22ef01cSRoman Divacky /// removeInstruction - Remove an instruction from the dependence analysis, 1469f22ef01cSRoman Divacky /// updating the dependence of instructions that previously depended on it. 1470f22ef01cSRoman Divacky /// This method attempts to keep the cache coherent using the reverse map. 1471f22ef01cSRoman Divacky void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) { 1472f22ef01cSRoman Divacky // Walk through the Non-local dependencies, removing this one as the value 1473f22ef01cSRoman Divacky // for any cached queries. 1474f22ef01cSRoman Divacky NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst); 1475f22ef01cSRoman Divacky if (NLDI != NonLocalDeps.end()) { 1476f22ef01cSRoman Divacky NonLocalDepInfo &BlockMap = NLDI->second.first; 1477f22ef01cSRoman Divacky for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end(); 1478f22ef01cSRoman Divacky DI != DE; ++DI) 1479f22ef01cSRoman Divacky if (Instruction *Inst = DI->getResult().getInst()) 1480f22ef01cSRoman Divacky RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst); 1481f22ef01cSRoman Divacky NonLocalDeps.erase(NLDI); 1482f22ef01cSRoman Divacky } 1483f22ef01cSRoman Divacky 1484f22ef01cSRoman Divacky // If we have a cached local dependence query for this instruction, remove it. 1485f22ef01cSRoman Divacky // 1486f22ef01cSRoman Divacky LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst); 1487f22ef01cSRoman Divacky if (LocalDepEntry != LocalDeps.end()) { 1488f22ef01cSRoman Divacky // Remove us from DepInst's reverse set now that the local dep info is gone. 1489f22ef01cSRoman Divacky if (Instruction *Inst = LocalDepEntry->second.getInst()) 1490f22ef01cSRoman Divacky RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst); 1491f22ef01cSRoman Divacky 1492f22ef01cSRoman Divacky // Remove this local dependency info. 1493f22ef01cSRoman Divacky LocalDeps.erase(LocalDepEntry); 1494f22ef01cSRoman Divacky } 1495f22ef01cSRoman Divacky 1496f22ef01cSRoman Divacky // If we have any cached pointer dependencies on this instruction, remove 1497f22ef01cSRoman Divacky // them. If the instruction has non-pointer type, then it can't be a pointer 1498f22ef01cSRoman Divacky // base. 1499f22ef01cSRoman Divacky 1500f22ef01cSRoman Divacky // Remove it from both the load info and the store info. The instruction 1501f22ef01cSRoman Divacky // can't be in either of these maps if it is non-pointer. 1502f22ef01cSRoman Divacky if (RemInst->getType()->isPointerTy()) { 1503f22ef01cSRoman Divacky RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false)); 1504f22ef01cSRoman Divacky RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true)); 1505f22ef01cSRoman Divacky } 1506f22ef01cSRoman Divacky 1507f22ef01cSRoman Divacky // Loop over all of the things that depend on the instruction we're removing. 1508f22ef01cSRoman Divacky // 1509f22ef01cSRoman Divacky SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd; 1510f22ef01cSRoman Divacky 1511f22ef01cSRoman Divacky // If we find RemInst as a clobber or Def in any of the maps for other values, 1512f22ef01cSRoman Divacky // we need to replace its entry with a dirty version of the instruction after 1513f22ef01cSRoman Divacky // it. If RemInst is a terminator, we use a null dirty value. 1514f22ef01cSRoman Divacky // 1515f22ef01cSRoman Divacky // Using a dirty version of the instruction after RemInst saves having to scan 1516f22ef01cSRoman Divacky // the entire block to get to this point. 1517f22ef01cSRoman Divacky MemDepResult NewDirtyVal; 1518f22ef01cSRoman Divacky if (!RemInst->isTerminator()) 1519f22ef01cSRoman Divacky NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst)); 1520f22ef01cSRoman Divacky 1521f22ef01cSRoman Divacky ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst); 1522f22ef01cSRoman Divacky if (ReverseDepIt != ReverseLocalDeps.end()) { 1523f22ef01cSRoman Divacky // RemInst can't be the terminator if it has local stuff depending on it. 152439d628a0SDimitry Andric assert(!ReverseDepIt->second.empty() && !isa<TerminatorInst>(RemInst) && 1525f22ef01cSRoman Divacky "Nothing can locally depend on a terminator"); 1526f22ef01cSRoman Divacky 152739d628a0SDimitry Andric for (Instruction *InstDependingOnRemInst : ReverseDepIt->second) { 1528f22ef01cSRoman Divacky assert(InstDependingOnRemInst != RemInst && 1529f22ef01cSRoman Divacky "Already removed our local dep info"); 1530f22ef01cSRoman Divacky 1531f22ef01cSRoman Divacky LocalDeps[InstDependingOnRemInst] = NewDirtyVal; 1532f22ef01cSRoman Divacky 1533f22ef01cSRoman Divacky // Make sure to remember that new things depend on NewDepInst. 1534f22ef01cSRoman Divacky assert(NewDirtyVal.getInst() && "There is no way something else can have " 1535f22ef01cSRoman Divacky "a local dep on this if it is a terminator!"); 1536f22ef01cSRoman Divacky ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(), 1537f22ef01cSRoman Divacky InstDependingOnRemInst)); 1538f22ef01cSRoman Divacky } 1539f22ef01cSRoman Divacky 1540f22ef01cSRoman Divacky ReverseLocalDeps.erase(ReverseDepIt); 1541f22ef01cSRoman Divacky 1542f22ef01cSRoman Divacky // Add new reverse deps after scanning the set, to avoid invalidating the 1543f22ef01cSRoman Divacky // 'ReverseDeps' reference. 1544f22ef01cSRoman Divacky while (!ReverseDepsToAdd.empty()) { 1545f22ef01cSRoman Divacky ReverseLocalDeps[ReverseDepsToAdd.back().first] 1546f22ef01cSRoman Divacky .insert(ReverseDepsToAdd.back().second); 1547f22ef01cSRoman Divacky ReverseDepsToAdd.pop_back(); 1548f22ef01cSRoman Divacky } 1549f22ef01cSRoman Divacky } 1550f22ef01cSRoman Divacky 1551f22ef01cSRoman Divacky ReverseDepIt = ReverseNonLocalDeps.find(RemInst); 1552f22ef01cSRoman Divacky if (ReverseDepIt != ReverseNonLocalDeps.end()) { 155339d628a0SDimitry Andric for (Instruction *I : ReverseDepIt->second) { 155439d628a0SDimitry Andric assert(I != RemInst && "Already removed NonLocalDep info for RemInst"); 1555f22ef01cSRoman Divacky 155639d628a0SDimitry Andric PerInstNLInfo &INLD = NonLocalDeps[I]; 1557f22ef01cSRoman Divacky // The information is now dirty! 1558f22ef01cSRoman Divacky INLD.second = true; 1559f22ef01cSRoman Divacky 1560f22ef01cSRoman Divacky for (NonLocalDepInfo::iterator DI = INLD.first.begin(), 1561f22ef01cSRoman Divacky DE = INLD.first.end(); DI != DE; ++DI) { 1562f22ef01cSRoman Divacky if (DI->getResult().getInst() != RemInst) continue; 1563f22ef01cSRoman Divacky 1564f22ef01cSRoman Divacky // Convert to a dirty entry for the subsequent instruction. 1565f22ef01cSRoman Divacky DI->setResult(NewDirtyVal); 1566f22ef01cSRoman Divacky 1567f22ef01cSRoman Divacky if (Instruction *NextI = NewDirtyVal.getInst()) 156839d628a0SDimitry Andric ReverseDepsToAdd.push_back(std::make_pair(NextI, I)); 1569f22ef01cSRoman Divacky } 1570f22ef01cSRoman Divacky } 1571f22ef01cSRoman Divacky 1572f22ef01cSRoman Divacky ReverseNonLocalDeps.erase(ReverseDepIt); 1573f22ef01cSRoman Divacky 1574f22ef01cSRoman Divacky // Add new reverse deps after scanning the set, to avoid invalidating 'Set' 1575f22ef01cSRoman Divacky while (!ReverseDepsToAdd.empty()) { 1576f22ef01cSRoman Divacky ReverseNonLocalDeps[ReverseDepsToAdd.back().first] 1577f22ef01cSRoman Divacky .insert(ReverseDepsToAdd.back().second); 1578f22ef01cSRoman Divacky ReverseDepsToAdd.pop_back(); 1579f22ef01cSRoman Divacky } 1580f22ef01cSRoman Divacky } 1581f22ef01cSRoman Divacky 1582f22ef01cSRoman Divacky // If the instruction is in ReverseNonLocalPtrDeps then it appears as a 1583f22ef01cSRoman Divacky // value in the NonLocalPointerDeps info. 1584f22ef01cSRoman Divacky ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt = 1585f22ef01cSRoman Divacky ReverseNonLocalPtrDeps.find(RemInst); 1586f22ef01cSRoman Divacky if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) { 1587f22ef01cSRoman Divacky SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd; 1588f22ef01cSRoman Divacky 158939d628a0SDimitry Andric for (ValueIsLoadPair P : ReversePtrDepIt->second) { 1590f22ef01cSRoman Divacky assert(P.getPointer() != RemInst && 1591f22ef01cSRoman Divacky "Already removed NonLocalPointerDeps info for RemInst"); 1592f22ef01cSRoman Divacky 15932754fe60SDimitry Andric NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].NonLocalDeps; 1594f22ef01cSRoman Divacky 1595f22ef01cSRoman Divacky // The cache is not valid for any specific block anymore. 15962754fe60SDimitry Andric NonLocalPointerDeps[P].Pair = BBSkipFirstBlockPair(); 1597f22ef01cSRoman Divacky 1598f22ef01cSRoman Divacky // Update any entries for RemInst to use the instruction after it. 1599f22ef01cSRoman Divacky for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end(); 1600f22ef01cSRoman Divacky DI != DE; ++DI) { 1601f22ef01cSRoman Divacky if (DI->getResult().getInst() != RemInst) continue; 1602f22ef01cSRoman Divacky 1603f22ef01cSRoman Divacky // Convert to a dirty entry for the subsequent instruction. 1604f22ef01cSRoman Divacky DI->setResult(NewDirtyVal); 1605f22ef01cSRoman Divacky 1606f22ef01cSRoman Divacky if (Instruction *NewDirtyInst = NewDirtyVal.getInst()) 1607f22ef01cSRoman Divacky ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P)); 1608f22ef01cSRoman Divacky } 1609f22ef01cSRoman Divacky 1610f22ef01cSRoman Divacky // Re-sort the NonLocalDepInfo. Changing the dirty entry to its 1611f22ef01cSRoman Divacky // subsequent value may invalidate the sortedness. 1612f22ef01cSRoman Divacky std::sort(NLPDI.begin(), NLPDI.end()); 1613f22ef01cSRoman Divacky } 1614f22ef01cSRoman Divacky 1615f22ef01cSRoman Divacky ReverseNonLocalPtrDeps.erase(ReversePtrDepIt); 1616f22ef01cSRoman Divacky 1617f22ef01cSRoman Divacky while (!ReversePtrDepsToAdd.empty()) { 1618f22ef01cSRoman Divacky ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first] 1619f22ef01cSRoman Divacky .insert(ReversePtrDepsToAdd.back().second); 1620f22ef01cSRoman Divacky ReversePtrDepsToAdd.pop_back(); 1621f22ef01cSRoman Divacky } 1622f22ef01cSRoman Divacky } 1623f22ef01cSRoman Divacky 1624f22ef01cSRoman Divacky 1625f22ef01cSRoman Divacky assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?"); 1626f22ef01cSRoman Divacky AA->deleteValue(RemInst); 1627f22ef01cSRoman Divacky DEBUG(verifyRemoved(RemInst)); 1628f22ef01cSRoman Divacky } 1629f22ef01cSRoman Divacky /// verifyRemoved - Verify that the specified instruction does not occur 163039d628a0SDimitry Andric /// in our internal data structures. This function verifies by asserting in 163139d628a0SDimitry Andric /// debug builds. 1632f22ef01cSRoman Divacky void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const { 163339d628a0SDimitry Andric #ifndef NDEBUG 1634f22ef01cSRoman Divacky for (LocalDepMapType::const_iterator I = LocalDeps.begin(), 1635f22ef01cSRoman Divacky E = LocalDeps.end(); I != E; ++I) { 1636f22ef01cSRoman Divacky assert(I->first != D && "Inst occurs in data structures"); 1637f22ef01cSRoman Divacky assert(I->second.getInst() != D && 1638f22ef01cSRoman Divacky "Inst occurs in data structures"); 1639f22ef01cSRoman Divacky } 1640f22ef01cSRoman Divacky 1641f22ef01cSRoman Divacky for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(), 1642f22ef01cSRoman Divacky E = NonLocalPointerDeps.end(); I != E; ++I) { 1643f22ef01cSRoman Divacky assert(I->first.getPointer() != D && "Inst occurs in NLPD map key"); 16442754fe60SDimitry Andric const NonLocalDepInfo &Val = I->second.NonLocalDeps; 1645f22ef01cSRoman Divacky for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end(); 1646f22ef01cSRoman Divacky II != E; ++II) 1647f22ef01cSRoman Divacky assert(II->getResult().getInst() != D && "Inst occurs as NLPD value"); 1648f22ef01cSRoman Divacky } 1649f22ef01cSRoman Divacky 1650f22ef01cSRoman Divacky for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(), 1651f22ef01cSRoman Divacky E = NonLocalDeps.end(); I != E; ++I) { 1652f22ef01cSRoman Divacky assert(I->first != D && "Inst occurs in data structures"); 1653f22ef01cSRoman Divacky const PerInstNLInfo &INLD = I->second; 1654f22ef01cSRoman Divacky for (NonLocalDepInfo::const_iterator II = INLD.first.begin(), 1655f22ef01cSRoman Divacky EE = INLD.first.end(); II != EE; ++II) 1656f22ef01cSRoman Divacky assert(II->getResult().getInst() != D && "Inst occurs in data structures"); 1657f22ef01cSRoman Divacky } 1658f22ef01cSRoman Divacky 1659f22ef01cSRoman Divacky for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(), 1660f22ef01cSRoman Divacky E = ReverseLocalDeps.end(); I != E; ++I) { 1661f22ef01cSRoman Divacky assert(I->first != D && "Inst occurs in data structures"); 166239d628a0SDimitry Andric for (Instruction *Inst : I->second) 166339d628a0SDimitry Andric assert(Inst != D && "Inst occurs in data structures"); 1664f22ef01cSRoman Divacky } 1665f22ef01cSRoman Divacky 1666f22ef01cSRoman Divacky for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(), 1667f22ef01cSRoman Divacky E = ReverseNonLocalDeps.end(); 1668f22ef01cSRoman Divacky I != E; ++I) { 1669f22ef01cSRoman Divacky assert(I->first != D && "Inst occurs in data structures"); 167039d628a0SDimitry Andric for (Instruction *Inst : I->second) 167139d628a0SDimitry Andric assert(Inst != D && "Inst occurs in data structures"); 1672f22ef01cSRoman Divacky } 1673f22ef01cSRoman Divacky 1674f22ef01cSRoman Divacky for (ReverseNonLocalPtrDepTy::const_iterator 1675f22ef01cSRoman Divacky I = ReverseNonLocalPtrDeps.begin(), 1676f22ef01cSRoman Divacky E = ReverseNonLocalPtrDeps.end(); I != E; ++I) { 1677f22ef01cSRoman Divacky assert(I->first != D && "Inst occurs in rev NLPD map"); 1678f22ef01cSRoman Divacky 167939d628a0SDimitry Andric for (ValueIsLoadPair P : I->second) 168039d628a0SDimitry Andric assert(P != ValueIsLoadPair(D, false) && 168139d628a0SDimitry Andric P != ValueIsLoadPair(D, true) && 1682f22ef01cSRoman Divacky "Inst occurs in ReverseNonLocalPtrDeps map"); 1683f22ef01cSRoman Divacky } 168439d628a0SDimitry Andric #endif 1685f22ef01cSRoman Divacky } 1686