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