Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +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 | afe840e | 2007-08-08 22:01:54 +0000 | [diff] [blame] | 12 | // alias analysis information, and tries to provide a lazy, caching interface to |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 13 | // a common kind of alias information query. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 969470c | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 17 | #define DEBUG_TYPE "memdep" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
| 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Function.h" |
| 22 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 5263803 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/ADT/STLExtras.h" |
Owen Anderson | 4c29547 | 2007-07-24 21:52:37 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
Tanya Lattner | 8edb2b7 | 2008-02-06 00:54:55 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 969470c | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetData.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chris Lattner | 98a6d80 | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 31 | STATISTIC(NumCacheNonLocal, "Number of cached non-local responses"); |
| 32 | STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses"); |
Owen Anderson | d6c7fea | 2007-09-09 21:43:49 +0000 | [diff] [blame] | 33 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 34 | char MemoryDependenceAnalysis::ID = 0; |
| 35 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 36 | // Register this pass... |
| 37 | static RegisterPass<MemoryDependenceAnalysis> X("memdep", |
Chris Lattner | 969470c | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 38 | "Memory Dependence Analysis", false, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +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 | e99ea70 | 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 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 54 | /// getCallSiteDependency - Private helper for finding the local dependencies |
| 55 | /// of a call site. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 56 | MemDepResult MemoryDependenceAnalysis:: |
| 57 | getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt, BasicBlock *BB) { |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 58 | // Walk backwards through the block, looking for dependencies |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 59 | while (ScanIt != BB->begin()) { |
| 60 | Instruction *Inst = --ScanIt; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 61 | |
| 62 | // If this inst is a memory op, get the pointer it accessed |
Chris Lattner | 18bd245 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 63 | Value *Pointer = 0; |
| 64 | uint64_t PointerSize = 0; |
| 65 | if (StoreInst *S = dyn_cast<StoreInst>(Inst)) { |
| 66 | Pointer = S->getPointerOperand(); |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 67 | PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType()); |
Chris Lattner | 18bd245 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 68 | } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) { |
| 69 | Pointer = V->getOperand(0); |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 70 | PointerSize = TD->getTypeStoreSize(V->getType()); |
Chris Lattner | 18bd245 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 71 | } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) { |
| 72 | Pointer = F->getPointerOperand(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | |
| 74 | // FreeInsts erase the entire structure |
Chris Lattner | 18bd245 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 75 | PointerSize = ~0UL; |
| 76 | } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) { |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 77 | if (AA->getModRefBehavior(CallSite::get(Inst)) == |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 78 | AliasAnalysis::DoesNotAccessMemory) |
Chris Lattner | 18bd245 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 79 | continue; |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 80 | return MemDepResult::get(Inst); |
Chris Lattner | 6c69a1a | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 81 | } else { |
| 82 | // Non-memory instruction. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 83 | continue; |
Chris Lattner | 6c69a1a | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 84 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 85 | |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 86 | if (AA->getModRefInfo(C, Pointer, PointerSize) != AliasAnalysis::NoModRef) |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 87 | return MemDepResult::get(Inst); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 90 | // No dependence found. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 91 | return MemDepResult::getNonLocal(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 94 | /// getDependencyFrom - Return the instruction on which a memory operation |
| 95 | /// depends. |
| 96 | MemDepResult MemoryDependenceAnalysis:: |
| 97 | getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt, |
| 98 | BasicBlock *BB) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 99 | // Get the pointer value for which dependence will be determined |
Chris Lattner | ac5d6e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 100 | Value *MemPtr = 0; |
| 101 | uint64_t MemSize = 0; |
| 102 | bool MemVolatile = false; |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 103 | |
| 104 | if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) { |
Chris Lattner | ac5d6e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 105 | MemPtr = S->getPointerOperand(); |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 106 | MemSize = TD->getTypeStoreSize(S->getOperand(0)->getType()); |
Chris Lattner | ac5d6e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 107 | MemVolatile = S->isVolatile(); |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 108 | } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) { |
Chris Lattner | ac5d6e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 109 | MemPtr = L->getPointerOperand(); |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 110 | MemSize = TD->getTypeStoreSize(L->getType()); |
Chris Lattner | ac5d6e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 111 | MemVolatile = L->isVolatile(); |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 112 | } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) { |
Chris Lattner | ac5d6e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 113 | MemPtr = V->getOperand(0); |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 114 | MemSize = TD->getTypeStoreSize(V->getType()); |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 115 | } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) { |
Chris Lattner | ac5d6e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 116 | MemPtr = F->getPointerOperand(); |
| 117 | // FreeInsts erase the entire structure, not just a field. |
| 118 | MemSize = ~0UL; |
| 119 | } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 120 | return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB); |
Chris Lattner | ac5d6e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 121 | else // Non-memory instructions depend on nothing. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 122 | return MemDepResult::getNone(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 123 | |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 124 | // Walk backwards through the basic block, looking for dependencies |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 125 | while (ScanIt != BB->begin()) { |
| 126 | Instruction *Inst = --ScanIt; |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 127 | |
| 128 | // If the access is volatile and this is a volatile load/store, return a |
| 129 | // dependence. |
| 130 | if (MemVolatile && |
| 131 | ((isa<LoadInst>(Inst) && cast<LoadInst>(Inst)->isVolatile()) || |
| 132 | (isa<StoreInst>(Inst) && cast<StoreInst>(Inst)->isVolatile()))) |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 133 | return MemDepResult::get(Inst); |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 6c69a1a | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 135 | // Values depend on loads if the pointers are must aliased. This means that |
| 136 | // a load depends on another must aliased load from the same value. |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 137 | if (LoadInst *L = dyn_cast<LoadInst>(Inst)) { |
| 138 | Value *Pointer = L->getPointerOperand(); |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 139 | uint64_t PointerSize = TD->getTypeStoreSize(L->getType()); |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 140 | |
| 141 | // If we found a pointer, check if it could be the same as our pointer |
| 142 | AliasAnalysis::AliasResult R = |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 143 | AA->alias(Pointer, PointerSize, MemPtr, MemSize); |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 144 | |
| 145 | if (R == AliasAnalysis::NoAlias) |
| 146 | continue; |
| 147 | |
| 148 | // May-alias loads don't depend on each other without a dependence. |
| 149 | if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias) |
| 150 | continue; |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 151 | return MemDepResult::get(Inst); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | } |
Chris Lattner | 8ea6046 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 153 | |
| 154 | // If this is an allocation, and if we know that the accessed pointer is to |
| 155 | // the allocation, return None. This means that there is no dependence and |
| 156 | // the access can be optimized based on that. For example, a load could |
| 157 | // turn into undef. |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 158 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) { |
Chris Lattner | 8ea6046 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 159 | Value *AccessPtr = MemPtr->getUnderlyingObject(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 8ea6046 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 161 | if (AccessPtr == AI || |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 162 | AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias) |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 163 | return MemDepResult::getNone(); |
Chris Lattner | 8ea6046 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 164 | continue; |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 165 | } |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 166 | |
| 167 | // See if this instruction mod/ref's the pointer. |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 168 | AliasAnalysis::ModRefResult MRR = AA->getModRefInfo(Inst, MemPtr, MemSize); |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 169 | |
| 170 | if (MRR == AliasAnalysis::NoModRef) |
Chris Lattner | ac5d6e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 171 | continue; |
| 172 | |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 173 | // Loads don't depend on read-only instructions. |
| 174 | if (isa<LoadInst>(QueryInst) && MRR == AliasAnalysis::Ref) |
Chris Lattner | ac5d6e9 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 175 | continue; |
Chris Lattner | 4103c3c | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 176 | |
| 177 | // Otherwise, there is a dependence. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 178 | return MemDepResult::get(Inst); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 181 | // If we found nothing, return the non-local flag. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 182 | return MemDepResult::getNonLocal(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 185 | /// getDependency - Return the instruction on which a memory operation |
| 186 | /// depends. |
| 187 | MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) { |
| 188 | Instruction *ScanPos = QueryInst; |
| 189 | |
| 190 | // Check for a cached result |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 191 | MemDepResult &LocalCache = LocalDeps[QueryInst]; |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 192 | |
Chris Lattner | 98a6d80 | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 193 | // If the cached entry is non-dirty, just return it. Note that this depends |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 194 | // on MemDepResult's default constructing to 'dirty'. |
| 195 | if (!LocalCache.isDirty()) |
| 196 | return LocalCache; |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 197 | |
| 198 | // Otherwise, if we have a dirty entry, we know we can start the scan at that |
| 199 | // instruction, which may save us some work. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 200 | if (Instruction *Inst = LocalCache.getInst()) { |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 201 | ScanPos = Inst; |
Chris Lattner | 7dc404d | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 202 | |
| 203 | SmallPtrSet<Instruction*, 4> &InstMap = ReverseLocalDeps[Inst]; |
| 204 | InstMap.erase(QueryInst); |
| 205 | if (InstMap.empty()) |
| 206 | ReverseLocalDeps.erase(Inst); |
| 207 | } |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 208 | |
| 209 | // Do the scan. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 210 | LocalCache = getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent()); |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 211 | |
| 212 | // Remember the result! |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 213 | if (Instruction *I = LocalCache.getInst()) |
Chris Lattner | 83c1a7c | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 214 | ReverseLocalDeps[I].insert(QueryInst); |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 215 | |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 216 | return LocalCache; |
Chris Lattner | a5a36c1 | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 219 | /// getNonLocalDependency - Perform a full dependency query for the |
| 220 | /// specified instruction, returning the set of blocks that the value is |
| 221 | /// potentially live across. The returned set of results will include a |
| 222 | /// "NonLocal" result for all blocks where the value is live across. |
| 223 | /// |
| 224 | /// This method assumes the instruction returns a "nonlocal" dependency |
| 225 | /// within its own block. |
| 226 | /// |
| 227 | void MemoryDependenceAnalysis:: |
| 228 | getNonLocalDependency(Instruction *QueryInst, |
| 229 | SmallVectorImpl<std::pair<BasicBlock*, |
| 230 | MemDepResult> > &Result) { |
| 231 | assert(getDependency(QueryInst).isNonLocal() && |
| 232 | "getNonLocalDependency should only be used on insts with non-local deps!"); |
Chris Lattner | 7dc404d | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 233 | PerInstNLInfo &CacheP = NonLocalDeps[QueryInst]; |
| 234 | if (CacheP.getPointer() == 0) CacheP.setPointer(new NonLocalDepInfo()); |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 7dc404d | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 236 | NonLocalDepInfo &Cache = *CacheP.getPointer(); |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 237 | |
| 238 | /// DirtyBlocks - This is the set of blocks that need to be recomputed. In |
| 239 | /// the cached case, this can happen due to instructions being deleted etc. In |
| 240 | /// the uncached case, this starts out as the set of predecessors we care |
| 241 | /// about. |
| 242 | SmallVector<BasicBlock*, 32> DirtyBlocks; |
| 243 | |
| 244 | if (!Cache.empty()) { |
| 245 | // If we already have a partially computed set of results, scan them to |
Chris Lattner | 7dc404d | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 246 | // determine what is dirty, seeding our initial DirtyBlocks worklist. The |
| 247 | // Int bit of CacheP tells us if we have anything dirty. |
| 248 | if (CacheP.getInt()) |
| 249 | for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end(); |
Chris Lattner | ff3342f | 2008-11-30 02:30:50 +0000 | [diff] [blame] | 250 | I != E; ++I) |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 251 | if (I->second.isDirty()) |
Chris Lattner | 7dc404d | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 252 | DirtyBlocks.push_back(I->first); |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 253 | |
| 254 | NumCacheNonLocal++; |
| 255 | |
| 256 | //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: " |
| 257 | // << Cache.size() << " cached: " << *QueryInst; |
| 258 | } else { |
| 259 | // Seed DirtyBlocks with each of the preds of QueryInst's block. |
| 260 | BasicBlock *QueryBB = QueryInst->getParent(); |
| 261 | DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB)); |
| 262 | NumUncacheNonLocal++; |
| 263 | } |
| 264 | |
| 265 | // Iterate while we still have blocks to update. |
| 266 | while (!DirtyBlocks.empty()) { |
| 267 | BasicBlock *DirtyBB = DirtyBlocks.back(); |
| 268 | DirtyBlocks.pop_back(); |
| 269 | |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 270 | // Get the entry for this block. Note that this relies on MemDepResult |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 271 | // default initializing to Dirty. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 272 | MemDepResult &DirtyBBEntry = Cache[DirtyBB]; |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 273 | |
| 274 | // If DirtyBBEntry isn't dirty, it ended up on the worklist multiple times. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 275 | if (!DirtyBBEntry.isDirty()) continue; |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 277 | // If the dirty entry has a pointer, start scanning from it so we don't have |
| 278 | // to rescan the entire block. |
| 279 | BasicBlock::iterator ScanPos = DirtyBB->end(); |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 280 | if (Instruction *Inst = DirtyBBEntry.getInst()) { |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 281 | ScanPos = Inst; |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 282 | |
| 283 | // We're removing QueryInst's dependence on Inst. |
| 284 | SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst]; |
| 285 | InstMap.erase(QueryInst); |
| 286 | if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst); |
| 287 | } |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 288 | |
Chris Lattner | c0ade85 | 2008-11-30 01:26:32 +0000 | [diff] [blame] | 289 | // Find out if this block has a local dependency for QueryInst. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 290 | DirtyBBEntry = getDependencyFrom(QueryInst, ScanPos, DirtyBB); |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 291 | |
| 292 | // If the block has a dependency (i.e. it isn't completely transparent to |
| 293 | // the value), remember it! |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 294 | if (!DirtyBBEntry.isNonLocal()) { |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 295 | // Keep the ReverseNonLocalDeps map up to date so we can efficiently |
| 296 | // update this when we remove instructions. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 297 | if (Instruction *Inst = DirtyBBEntry.getInst()) |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 298 | ReverseNonLocalDeps[Inst].insert(QueryInst); |
| 299 | continue; |
| 300 | } |
| 301 | |
| 302 | // If the block *is* completely transparent to the load, we need to check |
| 303 | // the predecessors of this block. Add them to our worklist. |
| 304 | DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB)); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | // Copy the result into the output set. |
Chris Lattner | ff3342f | 2008-11-30 02:30:50 +0000 | [diff] [blame] | 309 | for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end(); I != E;++I) |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 310 | Result.push_back(std::make_pair(I->first, I->second)); |
Chris Lattner | 03de616 | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 313 | /// removeInstruction - Remove an instruction from the dependence analysis, |
| 314 | /// updating the dependence of instructions that previously depended on it. |
Owen Anderson | 3de3c53 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 315 | /// This method attempts to keep the cache coherent using the reverse map. |
Chris Lattner | 1b185de | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 316 | void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) { |
Chris Lattner | 1b185de | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 317 | // Walk through the Non-local dependencies, removing this one as the value |
| 318 | // for any cached queries. |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 319 | NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst); |
| 320 | if (NLDI != NonLocalDeps.end()) { |
Chris Lattner | 7dc404d | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 321 | NonLocalDepInfo &BlockMap = *NLDI->second.getPointer(); |
Chris Lattner | ff3342f | 2008-11-30 02:30:50 +0000 | [diff] [blame] | 322 | for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end(); |
| 323 | DI != DE; ++DI) |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 324 | if (Instruction *Inst = DI->second.getInst()) |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 325 | ReverseNonLocalDeps[Inst].erase(RemInst); |
| 326 | delete &BlockMap; |
| 327 | NonLocalDeps.erase(NLDI); |
| 328 | } |
Owen Anderson | c772be7 | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 1b185de | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 330 | // If we have a cached local dependence query for this instruction, remove it. |
Chris Lattner | 5263803 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 331 | // |
Chris Lattner | fd9b56d | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 332 | LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst); |
| 333 | if (LocalDepEntry != LocalDeps.end()) { |
Chris Lattner | 1dd8aba | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 334 | // Remove us from DepInst's reverse set now that the local dep info is gone. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 335 | if (Instruction *Inst = LocalDepEntry->second.getInst()) { |
Chris Lattner | 1dd8aba | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 336 | SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst]; |
| 337 | RLD.erase(RemInst); |
| 338 | if (RLD.empty()) |
| 339 | ReverseLocalDeps.erase(Inst); |
| 340 | } |
| 341 | |
Chris Lattner | 5263803 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 342 | // Remove this local dependency info. |
Chris Lattner | fd9b56d | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 343 | LocalDeps.erase(LocalDepEntry); |
Chris Lattner | 1dd8aba | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 344 | } |
Chris Lattner | 5263803 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 345 | |
Chris Lattner | 89fbbe7 | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 346 | // Loop over all of the things that depend on the instruction we're removing. |
| 347 | // |
Chris Lattner | 75cbaf8 | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 348 | SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd; |
| 349 | |
Chris Lattner | 83c1a7c | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 350 | ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst); |
| 351 | if (ReverseDepIt != ReverseLocalDeps.end()) { |
Chris Lattner | 89fbbe7 | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 352 | SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second; |
Chris Lattner | 1dd8aba | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 353 | // RemInst can't be the terminator if it has stuff depending on it. |
| 354 | assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) && |
| 355 | "Nothing can locally depend on a terminator"); |
| 356 | |
| 357 | // Anything that was locally dependent on RemInst is now going to be |
| 358 | // dependent on the instruction after RemInst. It will have the dirty flag |
| 359 | // set so it will rescan. This saves having to scan the entire block to get |
| 360 | // to this point. |
| 361 | Instruction *NewDepInst = next(BasicBlock::iterator(RemInst)); |
| 362 | |
Chris Lattner | 89fbbe7 | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 363 | for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(), |
| 364 | E = ReverseDeps.end(); I != E; ++I) { |
| 365 | Instruction *InstDependingOnRemInst = *I; |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 366 | assert(InstDependingOnRemInst != RemInst && |
| 367 | "Already removed our local dep info"); |
Chris Lattner | 1dd8aba | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 368 | |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 369 | LocalDeps[InstDependingOnRemInst] = MemDepResult::getDirty(NewDepInst); |
Chris Lattner | 89fbbe7 | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 1dd8aba | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 371 | // Make sure to remember that new things depend on NewDepInst. |
| 372 | ReverseDepsToAdd.push_back(std::make_pair(NewDepInst, |
| 373 | InstDependingOnRemInst)); |
Chris Lattner | 89fbbe7 | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 374 | } |
Chris Lattner | 75cbaf8 | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 375 | |
| 376 | ReverseLocalDeps.erase(ReverseDepIt); |
| 377 | |
| 378 | // Add new reverse deps after scanning the set, to avoid invalidating the |
| 379 | // 'ReverseDeps' reference. |
| 380 | while (!ReverseDepsToAdd.empty()) { |
| 381 | ReverseLocalDeps[ReverseDepsToAdd.back().first] |
| 382 | .insert(ReverseDepsToAdd.back().second); |
| 383 | ReverseDepsToAdd.pop_back(); |
| 384 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 385 | } |
Owen Anderson | 2bd46a5 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 83c1a7c | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 387 | ReverseDepIt = ReverseNonLocalDeps.find(RemInst); |
| 388 | if (ReverseDepIt != ReverseNonLocalDeps.end()) { |
Chris Lattner | 89fbbe7 | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 389 | SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second; |
Owen Anderson | 2bd46a5 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 390 | for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end(); |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 391 | I != E; ++I) { |
| 392 | assert(*I != RemInst && "Already removed NonLocalDep info for RemInst"); |
| 393 | |
Chris Lattner | 7dc404d | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 394 | PerInstNLInfo &INLD = NonLocalDeps[*I]; |
| 395 | assert(INLD.getPointer() != 0 && "Reverse mapping out of date?"); |
| 396 | // The information is now dirty! |
| 397 | INLD.setInt(true); |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 7dc404d | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 399 | for (NonLocalDepInfo::iterator DI = INLD.getPointer()->begin(), |
| 400 | DE = INLD.getPointer()->end(); DI != DE; ++DI) { |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 401 | if (DI->second.getInst() != RemInst) continue; |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 402 | |
| 403 | // Convert to a dirty entry for the subsequent instruction. |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 404 | Instruction *NextI = 0; |
| 405 | if (!RemInst->isTerminator()) { |
| 406 | NextI = next(BasicBlock::iterator(RemInst)); |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 407 | ReverseDepsToAdd.push_back(std::make_pair(NextI, *I)); |
Chris Lattner | 98a6d80 | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 408 | } |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 409 | DI->second = MemDepResult::getDirty(NextI); |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
Chris Lattner | 75cbaf8 | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 412 | |
| 413 | ReverseNonLocalDeps.erase(ReverseDepIt); |
| 414 | |
Chris Lattner | 98a6d80 | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 415 | // Add new reverse deps after scanning the set, to avoid invalidating 'Set' |
| 416 | while (!ReverseDepsToAdd.empty()) { |
| 417 | ReverseNonLocalDeps[ReverseDepsToAdd.back().first] |
| 418 | .insert(ReverseDepsToAdd.back().second); |
| 419 | ReverseDepsToAdd.pop_back(); |
| 420 | } |
Owen Anderson | 2bd46a5 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 421 | } |
Owen Anderson | c772be7 | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 422 | |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 423 | assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?"); |
Chris Lattner | e99ea70 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 424 | AA->deleteValue(RemInst); |
Chris Lattner | 1b185de | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 425 | DEBUG(verifyRemoved(RemInst)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 426 | } |
Chris Lattner | 4fb2ce3 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 427 | |
| 428 | /// verifyRemoved - Verify that the specified instruction does not occur |
| 429 | /// in our internal data structures. |
| 430 | void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const { |
| 431 | for (LocalDepMapType::const_iterator I = LocalDeps.begin(), |
| 432 | E = LocalDeps.end(); I != E; ++I) { |
| 433 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 434 | assert(I->second.getInst() != D && |
Chris Lattner | 4fb2ce3 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 435 | "Inst occurs in data structures"); |
| 436 | } |
| 437 | |
| 438 | for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(), |
| 439 | E = NonLocalDeps.end(); I != E; ++I) { |
| 440 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 7dc404d | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 441 | const PerInstNLInfo &INLD = I->second; |
| 442 | for (NonLocalDepInfo::iterator II = INLD.getPointer()->begin(), |
| 443 | EE = INLD.getPointer()->end(); II != EE; ++II) |
Chris Lattner | e110a18 | 2008-11-30 23:17:19 +0000 | [diff] [blame^] | 444 | assert(II->second.getInst() != D && "Inst occurs in data structures"); |
Chris Lattner | 4fb2ce3 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(), |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 448 | E = ReverseLocalDeps.end(); I != E; ++I) { |
| 449 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 4fb2ce3 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 450 | for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), |
| 451 | EE = I->second.end(); II != EE; ++II) |
| 452 | assert(*II != D && "Inst occurs in data structures"); |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 453 | } |
Chris Lattner | 4fb2ce3 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 454 | |
| 455 | for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(), |
| 456 | E = ReverseNonLocalDeps.end(); |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 457 | I != E; ++I) { |
| 458 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 4fb2ce3 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 459 | for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), |
| 460 | EE = I->second.end(); II != EE; ++II) |
| 461 | assert(*II != D && "Inst occurs in data structures"); |
Chris Lattner | f9e6b43 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 462 | } |
Chris Lattner | 4fb2ce3 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 463 | } |