Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 1 | //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements an analysis that determines, for a given memory |
| 11 | // operation, what preceding memory operations it depends on. It builds on |
Owen Anderson | 80b1f09 | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 12 | // alias analysis information, and tries to provide a lazy, caching interface to |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 13 | // a common kind of alias information query. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 0e575f4 | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "memdep" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Owen Anderson | 7a616a1 | 2007-07-10 17:25:03 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/ADT/STLExtras.h" |
Owen Anderson | 4beedbd | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
Tanya Lattner | 63aa160 | 2008-02-06 00:54:55 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 0e575f4 | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 31 | STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses"); |
| 32 | STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses"); |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 33 | STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses"); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 34 | char MemoryDependenceAnalysis::ID = 0; |
| 35 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 36 | // Register this pass... |
Owen Anderson | 776ee1f | 2007-07-10 20:21:08 +0000 | [diff] [blame] | 37 | static RegisterPass<MemoryDependenceAnalysis> X("memdep", |
Chris Lattner | 0e575f4 | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 38 | "Memory Dependence Analysis", false, true); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 39 | |
| 40 | /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis. |
| 41 | /// |
| 42 | void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 43 | AU.setPreservesAll(); |
| 44 | AU.addRequiredTransitive<AliasAnalysis>(); |
| 45 | AU.addRequiredTransitive<TargetData>(); |
| 46 | } |
| 47 | |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 48 | bool MemoryDependenceAnalysis::runOnFunction(Function &) { |
| 49 | AA = &getAnalysis<AliasAnalysis>(); |
| 50 | TD = &getAnalysis<TargetData>(); |
| 51 | return false; |
| 52 | } |
| 53 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 8ef57c5 | 2008-12-07 00:35:51 +0000 | [diff] [blame] | 55 | /// getCallSiteDependencyFrom - Private helper for finding the local |
| 56 | /// dependencies of a call site. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 57 | MemDepResult MemoryDependenceAnalysis:: |
Chris Lattner | 8ef57c5 | 2008-12-07 00:35:51 +0000 | [diff] [blame] | 58 | getCallSiteDependencyFrom(CallSite CS, BasicBlock::iterator ScanIt, |
| 59 | BasicBlock *BB) { |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 60 | // Walk backwards through the block, looking for dependencies |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 61 | while (ScanIt != BB->begin()) { |
| 62 | Instruction *Inst = --ScanIt; |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 63 | |
| 64 | // If this inst is a memory op, get the pointer it accessed |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 65 | Value *Pointer = 0; |
| 66 | uint64_t PointerSize = 0; |
| 67 | if (StoreInst *S = dyn_cast<StoreInst>(Inst)) { |
| 68 | Pointer = S->getPointerOperand(); |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 69 | PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType()); |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 70 | } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) { |
| 71 | Pointer = V->getOperand(0); |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 72 | PointerSize = TD->getTypeStoreSize(V->getType()); |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 73 | } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) { |
| 74 | Pointer = F->getPointerOperand(); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 75 | |
| 76 | // FreeInsts erase the entire structure |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 77 | PointerSize = ~0UL; |
| 78 | } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) { |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 79 | CallSite InstCS = CallSite::get(Inst); |
| 80 | // If these two calls do not interfere, look past it. |
| 81 | if (AA->getModRefInfo(CS, InstCS) == AliasAnalysis::NoModRef) |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 82 | continue; |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 83 | |
| 84 | // FIXME: If this is a ref/ref result, we should ignore it! |
| 85 | // X = strlen(P); |
| 86 | // Y = strlen(Q); |
| 87 | // Z = strlen(P); // Z = X |
| 88 | |
| 89 | // If they interfere, we generally return clobber. However, if they are |
| 90 | // calls to the same read-only functions we return Def. |
| 91 | if (!AA->onlyReadsMemory(CS) || CS.getCalledFunction() == 0 || |
| 92 | CS.getCalledFunction() != InstCS.getCalledFunction()) |
| 93 | return MemDepResult::getClobber(Inst); |
| 94 | return MemDepResult::getDef(Inst); |
Chris Lattner | cfbb634 | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 95 | } else { |
| 96 | // Non-memory instruction. |
Owen Anderson | 202da14 | 2007-07-10 20:39:07 +0000 | [diff] [blame] | 97 | continue; |
Chris Lattner | cfbb634 | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 98 | } |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 99 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 100 | if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef) |
| 101 | return MemDepResult::getClobber(Inst); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame^] | 104 | // No dependence found. If this is the entry block of the function, it is a |
| 105 | // clobber, otherwise it is non-local. |
| 106 | if (BB != &BB->getParent()->getEntryBlock()) |
| 107 | return MemDepResult::getNonLocal(); |
| 108 | return MemDepResult::getClobber(ScanIt); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 111 | /// getPointerDependencyFrom - Return the instruction on which a memory |
| 112 | /// location depends. If isLoad is true, this routine ignore may-aliases with |
| 113 | /// read-only operations. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 114 | MemDepResult MemoryDependenceAnalysis:: |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 115 | getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad, |
| 116 | BasicBlock::iterator ScanIt, BasicBlock *BB) { |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame^] | 117 | |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 118 | // Walk backwards through the basic block, looking for dependencies |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 119 | while (ScanIt != BB->begin()) { |
| 120 | Instruction *Inst = --ScanIt; |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 121 | |
Chris Lattner | cfbb634 | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 122 | // Values depend on loads if the pointers are must aliased. This means that |
| 123 | // a load depends on another must aliased load from the same value. |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 124 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 125 | Value *Pointer = LI->getPointerOperand(); |
| 126 | uint64_t PointerSize = TD->getTypeStoreSize(LI->getType()); |
| 127 | |
| 128 | // If we found a pointer, check if it could be the same as our pointer. |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 129 | AliasAnalysis::AliasResult R = |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 130 | AA->alias(Pointer, PointerSize, MemPtr, MemSize); |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 131 | if (R == AliasAnalysis::NoAlias) |
| 132 | continue; |
| 133 | |
| 134 | // May-alias loads don't depend on each other without a dependence. |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 135 | if (isLoad && R == AliasAnalysis::MayAlias) |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 136 | continue; |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 137 | return MemDepResult::getDef(Inst); |
| 138 | } |
| 139 | |
| 140 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 141 | Value *Pointer = SI->getPointerOperand(); |
| 142 | uint64_t PointerSize = TD->getTypeStoreSize(SI->getOperand(0)->getType()); |
| 143 | |
| 144 | // If we found a pointer, check if it could be the same as our pointer. |
| 145 | AliasAnalysis::AliasResult R = |
| 146 | AA->alias(Pointer, PointerSize, MemPtr, MemSize); |
| 147 | |
| 148 | if (R == AliasAnalysis::NoAlias) |
| 149 | continue; |
| 150 | if (R == AliasAnalysis::MayAlias) |
| 151 | return MemDepResult::getClobber(Inst); |
| 152 | return MemDepResult::getDef(Inst); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 153 | } |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 154 | |
| 155 | // If this is an allocation, and if we know that the accessed pointer is to |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 156 | // the allocation, return Def. This means that there is no dependence and |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 157 | // the access can be optimized based on that. For example, a load could |
| 158 | // turn into undef. |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 159 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) { |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 160 | Value *AccessPtr = MemPtr->getUnderlyingObject(); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 162 | if (AccessPtr == AI || |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 163 | AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias) |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 164 | return MemDepResult::getDef(AI); |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 165 | continue; |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 166 | } |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 167 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 168 | // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer. |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 169 | // FIXME: If this is a load, we should ignore readonly calls! |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 170 | if (AA->getModRefInfo(Inst, MemPtr, MemSize) == AliasAnalysis::NoModRef) |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 171 | continue; |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 172 | |
| 173 | // Otherwise, there is a dependence. |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 174 | return MemDepResult::getClobber(Inst); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame^] | 177 | // No dependence found. If this is the entry block of the function, it is a |
| 178 | // clobber, otherwise it is non-local. |
| 179 | if (BB != &BB->getParent()->getEntryBlock()) |
| 180 | return MemDepResult::getNonLocal(); |
| 181 | return MemDepResult::getClobber(ScanIt); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 184 | /// getDependency - Return the instruction on which a memory operation |
| 185 | /// depends. |
| 186 | MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) { |
| 187 | Instruction *ScanPos = QueryInst; |
| 188 | |
| 189 | // Check for a cached result |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 190 | MemDepResult &LocalCache = LocalDeps[QueryInst]; |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 192 | // If the cached entry is non-dirty, just return it. Note that this depends |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 193 | // on MemDepResult's default constructing to 'dirty'. |
| 194 | if (!LocalCache.isDirty()) |
| 195 | return LocalCache; |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 196 | |
| 197 | // Otherwise, if we have a dirty entry, we know we can start the scan at that |
| 198 | // instruction, which may save us some work. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 199 | if (Instruction *Inst = LocalCache.getInst()) { |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 200 | ScanPos = Inst; |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 201 | |
| 202 | SmallPtrSet<Instruction*, 4> &InstMap = ReverseLocalDeps[Inst]; |
| 203 | InstMap.erase(QueryInst); |
| 204 | if (InstMap.empty()) |
| 205 | ReverseLocalDeps.erase(Inst); |
| 206 | } |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 207 | |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 208 | BasicBlock *QueryParent = QueryInst->getParent(); |
| 209 | |
| 210 | Value *MemPtr = 0; |
| 211 | uint64_t MemSize = 0; |
| 212 | |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 213 | // Do the scan. |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 214 | if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) { |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame^] | 215 | // No dependence found. If this is the entry block of the function, it is a |
| 216 | // clobber, otherwise it is non-local. |
| 217 | if (QueryParent != &QueryParent->getParent()->getEntryBlock()) |
| 218 | LocalCache = MemDepResult::getNonLocal(); |
| 219 | else |
| 220 | LocalCache = MemDepResult::getClobber(QueryInst); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 221 | } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) { |
| 222 | // If this is a volatile store, don't mess around with it. Just return the |
| 223 | // previous instruction as a clobber. |
| 224 | if (SI->isVolatile()) |
| 225 | LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 226 | else { |
| 227 | MemPtr = SI->getPointerOperand(); |
| 228 | MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType()); |
| 229 | } |
| 230 | } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) { |
| 231 | // If this is a volatile load, don't mess around with it. Just return the |
| 232 | // previous instruction as a clobber. |
| 233 | if (LI->isVolatile()) |
| 234 | LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 235 | else { |
| 236 | MemPtr = LI->getPointerOperand(); |
| 237 | MemSize = TD->getTypeStoreSize(LI->getType()); |
| 238 | } |
| 239 | } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) { |
Chris Lattner | d8dd934 | 2008-12-07 01:21:14 +0000 | [diff] [blame] | 240 | LocalCache = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos, |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 241 | QueryParent); |
| 242 | } else if (FreeInst *FI = dyn_cast<FreeInst>(QueryInst)) { |
| 243 | MemPtr = FI->getPointerOperand(); |
| 244 | // FreeInsts erase the entire structure, not just a field. |
| 245 | MemSize = ~0UL; |
| 246 | } else { |
| 247 | // Non-memory instruction. |
| 248 | LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 249 | } |
| 250 | |
| 251 | // If we need to do a pointer scan, make it happen. |
| 252 | if (MemPtr) |
| 253 | LocalCache = getPointerDependencyFrom(MemPtr, MemSize, |
| 254 | isa<LoadInst>(QueryInst), |
| 255 | ScanPos, QueryParent); |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 256 | |
| 257 | // Remember the result! |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 258 | if (Instruction *I = LocalCache.getInst()) |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 259 | ReverseLocalDeps[I].insert(QueryInst); |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 260 | |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 261 | return LocalCache; |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 264 | /// getNonLocalDependency - Perform a full dependency query for the |
| 265 | /// specified instruction, returning the set of blocks that the value is |
| 266 | /// potentially live across. The returned set of results will include a |
| 267 | /// "NonLocal" result for all blocks where the value is live across. |
| 268 | /// |
| 269 | /// This method assumes the instruction returns a "nonlocal" dependency |
| 270 | /// within its own block. |
| 271 | /// |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 272 | const MemoryDependenceAnalysis::NonLocalDepInfo & |
| 273 | MemoryDependenceAnalysis::getNonLocalDependency(Instruction *QueryInst) { |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame^] | 274 | // FIXME: Make this only be for callsites in the future. |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 275 | assert(isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst) || |
| 276 | isa<LoadInst>(QueryInst) || isa<StoreInst>(QueryInst)); |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 277 | assert(getDependency(QueryInst).isNonLocal() && |
| 278 | "getNonLocalDependency should only be used on insts with non-local deps!"); |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 279 | PerInstNLInfo &CacheP = NonLocalDeps[QueryInst]; |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 280 | NonLocalDepInfo &Cache = CacheP.first; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 281 | |
| 282 | /// DirtyBlocks - This is the set of blocks that need to be recomputed. In |
| 283 | /// the cached case, this can happen due to instructions being deleted etc. In |
| 284 | /// the uncached case, this starts out as the set of predecessors we care |
| 285 | /// about. |
| 286 | SmallVector<BasicBlock*, 32> DirtyBlocks; |
| 287 | |
| 288 | if (!Cache.empty()) { |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 289 | // Okay, we have a cache entry. If we know it is not dirty, just return it |
| 290 | // with no computation. |
| 291 | if (!CacheP.second) { |
| 292 | NumCacheNonLocal++; |
| 293 | return Cache; |
| 294 | } |
| 295 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 296 | // If we already have a partially computed set of results, scan them to |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 297 | // determine what is dirty, seeding our initial DirtyBlocks worklist. |
| 298 | for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end(); |
| 299 | I != E; ++I) |
| 300 | if (I->second.isDirty()) |
| 301 | DirtyBlocks.push_back(I->first); |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 302 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 303 | // Sort the cache so that we can do fast binary search lookups below. |
| 304 | std::sort(Cache.begin(), Cache.end()); |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 305 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 306 | ++NumCacheDirtyNonLocal; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 307 | //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: " |
| 308 | // << Cache.size() << " cached: " << *QueryInst; |
| 309 | } else { |
| 310 | // Seed DirtyBlocks with each of the preds of QueryInst's block. |
| 311 | BasicBlock *QueryBB = QueryInst->getParent(); |
| 312 | DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB)); |
| 313 | NumUncacheNonLocal++; |
| 314 | } |
| 315 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 316 | // Visited checked first, vector in sorted order. |
| 317 | SmallPtrSet<BasicBlock*, 64> Visited; |
| 318 | |
| 319 | unsigned NumSortedEntries = Cache.size(); |
| 320 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 321 | // Iterate while we still have blocks to update. |
| 322 | while (!DirtyBlocks.empty()) { |
| 323 | BasicBlock *DirtyBB = DirtyBlocks.back(); |
| 324 | DirtyBlocks.pop_back(); |
| 325 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 326 | // Already processed this block? |
| 327 | if (!Visited.insert(DirtyBB)) |
| 328 | continue; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 329 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 330 | // Do a binary search to see if we already have an entry for this block in |
| 331 | // the cache set. If so, find it. |
| 332 | NonLocalDepInfo::iterator Entry = |
| 333 | std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries, |
| 334 | std::make_pair(DirtyBB, MemDepResult())); |
| 335 | if (Entry != Cache.begin() && (&*Entry)[-1].first == DirtyBB) |
| 336 | --Entry; |
| 337 | |
| 338 | MemDepResult *ExistingResult = 0; |
| 339 | if (Entry != Cache.begin()+NumSortedEntries && |
| 340 | Entry->first == DirtyBB) { |
| 341 | // If we already have an entry, and if it isn't already dirty, the block |
| 342 | // is done. |
| 343 | if (!Entry->second.isDirty()) |
| 344 | continue; |
| 345 | |
| 346 | // Otherwise, remember this slot so we can update the value. |
| 347 | ExistingResult = &Entry->second; |
| 348 | } |
| 349 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 350 | // If the dirty entry has a pointer, start scanning from it so we don't have |
| 351 | // to rescan the entire block. |
| 352 | BasicBlock::iterator ScanPos = DirtyBB->end(); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 353 | if (ExistingResult) { |
| 354 | if (Instruction *Inst = ExistingResult->getInst()) { |
| 355 | ScanPos = Inst; |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 356 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 357 | // We're removing QueryInst's use of Inst. |
| 358 | SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst]; |
| 359 | InstMap.erase(QueryInst); |
| 360 | if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst); |
| 361 | } |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 363 | |
Chris Lattner | 73ec3cd | 2008-11-30 01:26:32 +0000 | [diff] [blame] | 364 | // Find out if this block has a local dependency for QueryInst. |
Chris Lattner | d8dd934 | 2008-12-07 01:21:14 +0000 | [diff] [blame] | 365 | MemDepResult Dep; |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 366 | |
| 367 | Value *MemPtr = 0; |
| 368 | uint64_t MemSize = 0; |
| 369 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame^] | 370 | if (ScanPos == DirtyBB->begin()) { |
| 371 | // No dependence found. If this is the entry block of the function, it is a |
| 372 | // clobber, otherwise it is non-local. |
| 373 | if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) |
| 374 | Dep = MemDepResult::getNonLocal(); |
| 375 | else |
| 376 | Dep = MemDepResult::getClobber(ScanPos); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 377 | } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) { |
| 378 | // If this is a volatile store, don't mess around with it. Just return the |
| 379 | // previous instruction as a clobber. |
| 380 | if (SI->isVolatile()) |
| 381 | Dep = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 382 | else { |
| 383 | MemPtr = SI->getPointerOperand(); |
| 384 | MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType()); |
| 385 | } |
| 386 | } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) { |
| 387 | // If this is a volatile load, don't mess around with it. Just return the |
| 388 | // previous instruction as a clobber. |
| 389 | if (LI->isVolatile()) |
| 390 | Dep = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 391 | else { |
| 392 | MemPtr = LI->getPointerOperand(); |
| 393 | MemSize = TD->getTypeStoreSize(LI->getType()); |
| 394 | } |
| 395 | } else { |
| 396 | assert(isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)); |
Chris Lattner | d8dd934 | 2008-12-07 01:21:14 +0000 | [diff] [blame] | 397 | Dep = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos, |
| 398 | DirtyBB); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | if (MemPtr) |
| 402 | Dep = getPointerDependencyFrom(MemPtr, MemSize, isa<LoadInst>(QueryInst), |
| 403 | ScanPos, DirtyBB); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 404 | |
| 405 | // If we had a dirty entry for the block, update it. Otherwise, just add |
| 406 | // a new entry. |
| 407 | if (ExistingResult) |
| 408 | *ExistingResult = Dep; |
| 409 | else |
| 410 | Cache.push_back(std::make_pair(DirtyBB, Dep)); |
| 411 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 412 | // If the block has a dependency (i.e. it isn't completely transparent to |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 413 | // the value), remember the association! |
| 414 | if (!Dep.isNonLocal()) { |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 415 | // Keep the ReverseNonLocalDeps map up to date so we can efficiently |
| 416 | // update this when we remove instructions. |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 417 | if (Instruction *Inst = Dep.getInst()) |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 418 | ReverseNonLocalDeps[Inst].insert(QueryInst); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 419 | } else { |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 420 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 421 | // If the block *is* completely transparent to the load, we need to check |
| 422 | // the predecessors of this block. Add them to our worklist. |
| 423 | DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB)); |
| 424 | } |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 427 | return Cache; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame^] | 430 | /// getNonLocalPointerDependency - Perform a full dependency query for an |
| 431 | /// access to the specified (non-volatile) memory location, returning the |
| 432 | /// set of instructions that either define or clobber the value. |
| 433 | /// |
| 434 | /// This method assumes the pointer has a "NonLocal" dependency within its |
| 435 | /// own block. |
| 436 | /// |
| 437 | void MemoryDependenceAnalysis:: |
| 438 | getNonLocalPointerDependency(Value *Pointer, bool isLoad, BasicBlock *FromBB, |
| 439 | SmallVectorImpl<NonLocalDepEntry> &Result) { |
| 440 | // We know that the pointer value is live into FromBB find the def/clobbers |
| 441 | // from presecessors. |
| 442 | SmallVector<std::pair<BasicBlock*, Value*>, 32> Worklist; |
| 443 | |
| 444 | for (pred_iterator PI = pred_begin(FromBB), E = pred_end(FromBB); PI != E; |
| 445 | ++PI) |
| 446 | // TODO: PHI TRANSLATE. |
| 447 | Worklist.push_back(std::make_pair(*PI, Pointer)); |
| 448 | |
| 449 | const Type *EltTy = cast<PointerType>(Pointer->getType())->getElementType(); |
| 450 | uint64_t PointeeSize = TD->getTypeStoreSize(EltTy); |
| 451 | |
| 452 | // While we have blocks to analyze, get their values. |
| 453 | SmallPtrSet<BasicBlock*, 64> Visited; |
| 454 | while (!Worklist.empty()) { |
| 455 | FromBB = Worklist.back().first; |
| 456 | Pointer = Worklist.back().second; |
| 457 | Worklist.pop_back(); |
| 458 | |
| 459 | // Analyze the dependency of *Pointer in FromBB. See if we already have |
| 460 | // been here. |
| 461 | if (!Visited.insert(FromBB)) |
| 462 | continue; |
| 463 | |
| 464 | // FIXME: CACHE! |
| 465 | |
| 466 | MemDepResult Dep = |
| 467 | getPointerDependencyFrom(Pointer, PointeeSize, isLoad, |
| 468 | FromBB->end(), FromBB); |
| 469 | |
| 470 | // If we got a Def or Clobber, add this to the list of results. |
| 471 | if (!Dep.isNonLocal()) { |
| 472 | Result.push_back(NonLocalDepEntry(FromBB, Dep)); |
| 473 | continue; |
| 474 | } |
| 475 | |
| 476 | // Otherwise, we have to process all the predecessors of this block to scan |
| 477 | // them as well. |
| 478 | for (pred_iterator PI = pred_begin(FromBB), E = pred_end(FromBB); PI != E; |
| 479 | ++PI) |
| 480 | // TODO: PHI TRANSLATE. |
| 481 | Worklist.push_back(std::make_pair(*PI, Pointer)); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | |
| 486 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 487 | /// removeInstruction - Remove an instruction from the dependence analysis, |
| 488 | /// updating the dependence of instructions that previously depended on it. |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 489 | /// This method attempts to keep the cache coherent using the reverse map. |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 490 | void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) { |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 491 | // Walk through the Non-local dependencies, removing this one as the value |
| 492 | // for any cached queries. |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 493 | NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst); |
| 494 | if (NLDI != NonLocalDeps.end()) { |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 495 | NonLocalDepInfo &BlockMap = NLDI->second.first; |
Chris Lattner | 25f4b2b | 2008-11-30 02:30:50 +0000 | [diff] [blame] | 496 | for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end(); |
| 497 | DI != DE; ++DI) |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 498 | if (Instruction *Inst = DI->second.getInst()) |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 499 | ReverseNonLocalDeps[Inst].erase(RemInst); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 500 | NonLocalDeps.erase(NLDI); |
| 501 | } |
Owen Anderson | 5fc4aba | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 503 | // If we have a cached local dependence query for this instruction, remove it. |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 504 | // |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 505 | LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst); |
| 506 | if (LocalDepEntry != LocalDeps.end()) { |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 507 | // Remove us from DepInst's reverse set now that the local dep info is gone. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 508 | if (Instruction *Inst = LocalDepEntry->second.getInst()) { |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 509 | SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst]; |
| 510 | RLD.erase(RemInst); |
| 511 | if (RLD.empty()) |
| 512 | ReverseLocalDeps.erase(Inst); |
| 513 | } |
| 514 | |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 515 | // Remove this local dependency info. |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 516 | LocalDeps.erase(LocalDepEntry); |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 517 | } |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 518 | |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 519 | // Loop over all of the things that depend on the instruction we're removing. |
| 520 | // |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 521 | SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd; |
| 522 | |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 523 | ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst); |
| 524 | if (ReverseDepIt != ReverseLocalDeps.end()) { |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 525 | SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second; |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 526 | // RemInst can't be the terminator if it has stuff depending on it. |
| 527 | assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) && |
| 528 | "Nothing can locally depend on a terminator"); |
| 529 | |
| 530 | // Anything that was locally dependent on RemInst is now going to be |
| 531 | // dependent on the instruction after RemInst. It will have the dirty flag |
| 532 | // set so it will rescan. This saves having to scan the entire block to get |
| 533 | // to this point. |
| 534 | Instruction *NewDepInst = next(BasicBlock::iterator(RemInst)); |
| 535 | |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 536 | for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(), |
| 537 | E = ReverseDeps.end(); I != E; ++I) { |
| 538 | Instruction *InstDependingOnRemInst = *I; |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 539 | assert(InstDependingOnRemInst != RemInst && |
| 540 | "Already removed our local dep info"); |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 541 | |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 542 | LocalDeps[InstDependingOnRemInst] = MemDepResult::getDirty(NewDepInst); |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 544 | // Make sure to remember that new things depend on NewDepInst. |
| 545 | ReverseDepsToAdd.push_back(std::make_pair(NewDepInst, |
| 546 | InstDependingOnRemInst)); |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 547 | } |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 548 | |
| 549 | ReverseLocalDeps.erase(ReverseDepIt); |
| 550 | |
| 551 | // Add new reverse deps after scanning the set, to avoid invalidating the |
| 552 | // 'ReverseDeps' reference. |
| 553 | while (!ReverseDepsToAdd.empty()) { |
| 554 | ReverseLocalDeps[ReverseDepsToAdd.back().first] |
| 555 | .insert(ReverseDepsToAdd.back().second); |
| 556 | ReverseDepsToAdd.pop_back(); |
| 557 | } |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 558 | } |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 560 | ReverseDepIt = ReverseNonLocalDeps.find(RemInst); |
| 561 | if (ReverseDepIt != ReverseNonLocalDeps.end()) { |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 562 | SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second; |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 563 | for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end(); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 564 | I != E; ++I) { |
| 565 | assert(*I != RemInst && "Already removed NonLocalDep info for RemInst"); |
| 566 | |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 567 | PerInstNLInfo &INLD = NonLocalDeps[*I]; |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 568 | // The information is now dirty! |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 569 | INLD.second = true; |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 570 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 571 | for (NonLocalDepInfo::iterator DI = INLD.first.begin(), |
| 572 | DE = INLD.first.end(); DI != DE; ++DI) { |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 573 | if (DI->second.getInst() != RemInst) continue; |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 574 | |
| 575 | // Convert to a dirty entry for the subsequent instruction. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 576 | Instruction *NextI = 0; |
| 577 | if (!RemInst->isTerminator()) { |
| 578 | NextI = next(BasicBlock::iterator(RemInst)); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 579 | ReverseDepsToAdd.push_back(std::make_pair(NextI, *I)); |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 580 | } |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 581 | DI->second = MemDepResult::getDirty(NextI); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 584 | |
| 585 | ReverseNonLocalDeps.erase(ReverseDepIt); |
| 586 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 587 | // Add new reverse deps after scanning the set, to avoid invalidating 'Set' |
| 588 | while (!ReverseDepsToAdd.empty()) { |
| 589 | ReverseNonLocalDeps[ReverseDepsToAdd.back().first] |
| 590 | .insert(ReverseDepsToAdd.back().second); |
| 591 | ReverseDepsToAdd.pop_back(); |
| 592 | } |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 593 | } |
Owen Anderson | 5fc4aba | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 594 | |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 595 | assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?"); |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 596 | AA->deleteValue(RemInst); |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 597 | DEBUG(verifyRemoved(RemInst)); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 598 | } |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 599 | |
| 600 | /// verifyRemoved - Verify that the specified instruction does not occur |
| 601 | /// in our internal data structures. |
| 602 | void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const { |
| 603 | for (LocalDepMapType::const_iterator I = LocalDeps.begin(), |
| 604 | E = LocalDeps.end(); I != E; ++I) { |
| 605 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 606 | assert(I->second.getInst() != D && |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 607 | "Inst occurs in data structures"); |
| 608 | } |
| 609 | |
| 610 | for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(), |
| 611 | E = NonLocalDeps.end(); I != E; ++I) { |
| 612 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 613 | const PerInstNLInfo &INLD = I->second; |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 614 | for (NonLocalDepInfo::const_iterator II = INLD.first.begin(), |
| 615 | EE = INLD.first.end(); II != EE; ++II) |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 616 | assert(II->second.getInst() != D && "Inst occurs in data structures"); |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(), |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 620 | E = ReverseLocalDeps.end(); I != E; ++I) { |
| 621 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 622 | for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), |
| 623 | EE = I->second.end(); II != EE; ++II) |
| 624 | assert(*II != D && "Inst occurs in data structures"); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 625 | } |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 626 | |
| 627 | for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(), |
| 628 | E = ReverseNonLocalDeps.end(); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 629 | I != E; ++I) { |
| 630 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 631 | for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), |
| 632 | EE = I->second.end(); II != EE; ++II) |
| 633 | assert(*II != D && "Inst occurs in data structures"); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 634 | } |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 635 | } |