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" |
| 19 | #include "llvm/Instructions.h" |
Owen Anderson | f6cec85 | 2009-03-09 05:12:38 +0000 | [diff] [blame] | 20 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 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" |
Duncan Sands | 7050f3d | 2008-12-10 09:38:36 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 4012fdd | 2008-12-09 06:28:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/PredIteratorCache.h" |
Chris Lattner | 0e575f4 | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 30 | STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses"); |
| 31 | STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses"); |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 32 | STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses"); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 33 | |
| 34 | STATISTIC(NumCacheNonLocalPtr, |
| 35 | "Number of fully cached non-local ptr responses"); |
| 36 | STATISTIC(NumCacheDirtyNonLocalPtr, |
| 37 | "Number of cached, but dirty, non-local ptr responses"); |
| 38 | STATISTIC(NumUncacheNonLocalPtr, |
| 39 | "Number of uncached non-local ptr responses"); |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 40 | STATISTIC(NumCacheCompleteNonLocalPtr, |
| 41 | "Number of block queries that were completely cached"); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 42 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 43 | char MemoryDependenceAnalysis::ID = 0; |
| 44 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 45 | // Register this pass... |
Owen Anderson | 776ee1f | 2007-07-10 20:21:08 +0000 | [diff] [blame] | 46 | static RegisterPass<MemoryDependenceAnalysis> X("memdep", |
Chris Lattner | 0e575f4 | 2008-11-28 21:45:17 +0000 | [diff] [blame] | 47 | "Memory Dependence Analysis", false, true); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 4012fdd | 2008-12-09 06:28:49 +0000 | [diff] [blame] | 49 | MemoryDependenceAnalysis::MemoryDependenceAnalysis() |
| 50 | : FunctionPass(&ID), PredCache(0) { |
| 51 | } |
| 52 | MemoryDependenceAnalysis::~MemoryDependenceAnalysis() { |
| 53 | } |
| 54 | |
| 55 | /// Clean up memory in between runs |
| 56 | void MemoryDependenceAnalysis::releaseMemory() { |
| 57 | LocalDeps.clear(); |
| 58 | NonLocalDeps.clear(); |
| 59 | NonLocalPointerDeps.clear(); |
| 60 | ReverseLocalDeps.clear(); |
| 61 | ReverseNonLocalDeps.clear(); |
| 62 | ReverseNonLocalPtrDeps.clear(); |
| 63 | PredCache->clear(); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 68 | /// getAnalysisUsage - Does not modify anything. It uses Alias Analysis. |
| 69 | /// |
| 70 | void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 71 | AU.setPreservesAll(); |
| 72 | AU.addRequiredTransitive<AliasAnalysis>(); |
| 73 | AU.addRequiredTransitive<TargetData>(); |
| 74 | } |
| 75 | |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 76 | bool MemoryDependenceAnalysis::runOnFunction(Function &) { |
| 77 | AA = &getAnalysis<AliasAnalysis>(); |
| 78 | TD = &getAnalysis<TargetData>(); |
Chris Lattner | 4012fdd | 2008-12-09 06:28:49 +0000 | [diff] [blame] | 79 | if (PredCache == 0) |
| 80 | PredCache.reset(new PredIteratorCache()); |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
Chris Lattner | d44745d | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 84 | /// RemoveFromReverseMap - This is a helper function that removes Val from |
| 85 | /// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry. |
| 86 | template <typename KeyTy> |
| 87 | static void RemoveFromReverseMap(DenseMap<Instruction*, |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 88 | SmallPtrSet<KeyTy, 4> > &ReverseMap, |
| 89 | Instruction *Inst, KeyTy Val) { |
| 90 | typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator |
Chris Lattner | d44745d | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 91 | InstIt = ReverseMap.find(Inst); |
| 92 | assert(InstIt != ReverseMap.end() && "Reverse map out of sync?"); |
| 93 | bool Found = InstIt->second.erase(Val); |
| 94 | assert(Found && "Invalid reverse map!"); Found=Found; |
| 95 | if (InstIt->second.empty()) |
| 96 | ReverseMap.erase(InstIt); |
| 97 | } |
| 98 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 8ef57c5 | 2008-12-07 00:35:51 +0000 | [diff] [blame] | 100 | /// getCallSiteDependencyFrom - Private helper for finding the local |
| 101 | /// dependencies of a call site. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 102 | MemDepResult MemoryDependenceAnalysis:: |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 103 | getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall, |
| 104 | BasicBlock::iterator ScanIt, BasicBlock *BB) { |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 105 | // Walk backwards through the block, looking for dependencies |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 106 | while (ScanIt != BB->begin()) { |
| 107 | Instruction *Inst = --ScanIt; |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 108 | |
| 109 | // If this inst is a memory op, get the pointer it accessed |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 110 | Value *Pointer = 0; |
| 111 | uint64_t PointerSize = 0; |
| 112 | if (StoreInst *S = dyn_cast<StoreInst>(Inst)) { |
| 113 | Pointer = S->getPointerOperand(); |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 114 | PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType()); |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 115 | } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) { |
| 116 | Pointer = V->getOperand(0); |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 117 | PointerSize = TD->getTypeStoreSize(V->getType()); |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 118 | } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) { |
| 119 | Pointer = F->getPointerOperand(); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 120 | |
| 121 | // FreeInsts erase the entire structure |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 122 | PointerSize = ~0ULL; |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 123 | } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) { |
Owen Anderson | f6cec85 | 2009-03-09 05:12:38 +0000 | [diff] [blame] | 124 | // Debug intrinsics don't cause dependences. |
Dale Johannesen | 497cb6f | 2009-03-11 21:13:01 +0000 | [diff] [blame] | 125 | if (isa<DbgInfoIntrinsic>(Inst)) continue; |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 126 | CallSite InstCS = CallSite::get(Inst); |
| 127 | // If these two calls do not interfere, look past it. |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 128 | switch (AA->getModRefInfo(CS, InstCS)) { |
| 129 | case AliasAnalysis::NoModRef: |
| 130 | // If the two calls don't interact (e.g. InstCS is readnone) keep |
| 131 | // scanning. |
Chris Lattner | 00314b3 | 2008-11-29 09:15:21 +0000 | [diff] [blame] | 132 | continue; |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 133 | case AliasAnalysis::Ref: |
| 134 | // If the two calls read the same memory locations and CS is a readonly |
| 135 | // function, then we have two cases: 1) the calls may not interfere with |
| 136 | // each other at all. 2) the calls may produce the same value. In case |
| 137 | // #1 we want to ignore the values, in case #2, we want to return Inst |
| 138 | // as a Def dependence. This allows us to CSE in cases like: |
| 139 | // X = strlen(P); |
| 140 | // memchr(...); |
| 141 | // Y = strlen(P); // Y = X |
| 142 | if (isReadOnlyCall) { |
| 143 | if (CS.getCalledFunction() != 0 && |
| 144 | CS.getCalledFunction() == InstCS.getCalledFunction()) |
| 145 | return MemDepResult::getDef(Inst); |
| 146 | // Ignore unrelated read/read call dependences. |
| 147 | continue; |
| 148 | } |
| 149 | // FALL THROUGH |
| 150 | default: |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 151 | return MemDepResult::getClobber(Inst); |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 152 | } |
Chris Lattner | cfbb634 | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 153 | } else { |
| 154 | // Non-memory instruction. |
Owen Anderson | 202da14 | 2007-07-10 20:39:07 +0000 | [diff] [blame] | 155 | continue; |
Chris Lattner | cfbb634 | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 156 | } |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 157 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 158 | if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef) |
| 159 | return MemDepResult::getClobber(Inst); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 162 | // No dependence found. If this is the entry block of the function, it is a |
| 163 | // clobber, otherwise it is non-local. |
| 164 | if (BB != &BB->getParent()->getEntryBlock()) |
| 165 | return MemDepResult::getNonLocal(); |
| 166 | return MemDepResult::getClobber(ScanIt); |
Owen Anderson | 5f32320 | 2007-07-10 17:59:22 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 169 | /// getPointerDependencyFrom - Return the instruction on which a memory |
| 170 | /// location depends. If isLoad is true, this routine ignore may-aliases with |
| 171 | /// read-only operations. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 172 | MemDepResult MemoryDependenceAnalysis:: |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 173 | getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad, |
| 174 | BasicBlock::iterator ScanIt, BasicBlock *BB) { |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 175 | |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 176 | // Walk backwards through the basic block, looking for dependencies. |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 177 | while (ScanIt != BB->begin()) { |
| 178 | Instruction *Inst = --ScanIt; |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 179 | |
Owen Anderson | f6cec85 | 2009-03-09 05:12:38 +0000 | [diff] [blame] | 180 | // Debug intrinsics don't cause dependences. |
| 181 | if (isa<DbgInfoIntrinsic>(Inst)) continue; |
| 182 | |
Chris Lattner | cfbb634 | 2008-11-30 01:44:00 +0000 | [diff] [blame] | 183 | // Values depend on loads if the pointers are must aliased. This means that |
| 184 | // a load depends on another must aliased load from the same value. |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 185 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) { |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 186 | Value *Pointer = LI->getPointerOperand(); |
| 187 | uint64_t PointerSize = TD->getTypeStoreSize(LI->getType()); |
| 188 | |
| 189 | // 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] | 190 | AliasAnalysis::AliasResult R = |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 191 | AA->alias(Pointer, PointerSize, MemPtr, MemSize); |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 192 | if (R == AliasAnalysis::NoAlias) |
| 193 | continue; |
| 194 | |
| 195 | // May-alias loads don't depend on each other without a dependence. |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 196 | if (isLoad && R == AliasAnalysis::MayAlias) |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 197 | continue; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 198 | // Stores depend on may and must aliased loads, loads depend on must-alias |
| 199 | // loads. |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 200 | return MemDepResult::getDef(Inst); |
| 201 | } |
| 202 | |
| 203 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Chris Lattner | ab9cf12 | 2009-05-25 21:28:56 +0000 | [diff] [blame] | 204 | // If alias analysis can tell that this store is guaranteed to not modify |
| 205 | // the query pointer, ignore it. Use getModRefInfo to handle cases where |
| 206 | // the query pointer points to constant memory etc. |
| 207 | if (AA->getModRefInfo(SI, MemPtr, MemSize) == AliasAnalysis::NoModRef) |
| 208 | continue; |
| 209 | |
| 210 | // Ok, this store might clobber the query pointer. Check to see if it is |
| 211 | // a must alias: in this case, we want to return this as a def. |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 212 | Value *Pointer = SI->getPointerOperand(); |
| 213 | uint64_t PointerSize = TD->getTypeStoreSize(SI->getOperand(0)->getType()); |
Chris Lattner | ab9cf12 | 2009-05-25 21:28:56 +0000 | [diff] [blame] | 214 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 215 | // If we found a pointer, check if it could be the same as our pointer. |
| 216 | AliasAnalysis::AliasResult R = |
| 217 | AA->alias(Pointer, PointerSize, MemPtr, MemSize); |
| 218 | |
| 219 | if (R == AliasAnalysis::NoAlias) |
| 220 | continue; |
| 221 | if (R == AliasAnalysis::MayAlias) |
| 222 | return MemDepResult::getClobber(Inst); |
| 223 | return MemDepResult::getDef(Inst); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 224 | } |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 225 | |
| 226 | // 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] | 227 | // the allocation, return Def. This means that there is no dependence and |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 228 | // the access can be optimized based on that. For example, a load could |
| 229 | // turn into undef. |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 230 | if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) { |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 231 | Value *AccessPtr = MemPtr->getUnderlyingObject(); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 233 | if (AccessPtr == AI || |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 234 | AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias) |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 235 | return MemDepResult::getDef(AI); |
Chris Lattner | 237a828 | 2008-11-30 01:39:32 +0000 | [diff] [blame] | 236 | continue; |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 237 | } |
Chris Lattner | a161ab0 | 2008-11-29 09:09:48 +0000 | [diff] [blame] | 238 | |
Chris Lattner | b51deb9 | 2008-12-05 21:04:20 +0000 | [diff] [blame] | 239 | // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer. |
Chris Lattner | 3579e44 | 2008-12-09 19:47:40 +0000 | [diff] [blame] | 240 | switch (AA->getModRefInfo(Inst, MemPtr, MemSize)) { |
| 241 | case AliasAnalysis::NoModRef: |
| 242 | // If the call has no effect on the queried pointer, just ignore it. |
Chris Lattner | 25a0814 | 2008-11-29 08:51:16 +0000 | [diff] [blame] | 243 | continue; |
Chris Lattner | 3579e44 | 2008-12-09 19:47:40 +0000 | [diff] [blame] | 244 | case AliasAnalysis::Ref: |
| 245 | // If the call is known to never store to the pointer, and if this is a |
| 246 | // load query, we can safely ignore it (scan past it). |
| 247 | if (isLoad) |
| 248 | continue; |
| 249 | // FALL THROUGH. |
| 250 | default: |
| 251 | // Otherwise, there is a potential dependence. Return a clobber. |
| 252 | return MemDepResult::getClobber(Inst); |
| 253 | } |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 256 | // No dependence found. If this is the entry block of the function, it is a |
| 257 | // clobber, otherwise it is non-local. |
| 258 | if (BB != &BB->getParent()->getEntryBlock()) |
| 259 | return MemDepResult::getNonLocal(); |
| 260 | return MemDepResult::getClobber(ScanIt); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 263 | /// getDependency - Return the instruction on which a memory operation |
| 264 | /// depends. |
| 265 | MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) { |
| 266 | Instruction *ScanPos = QueryInst; |
| 267 | |
| 268 | // Check for a cached result |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 269 | MemDepResult &LocalCache = LocalDeps[QueryInst]; |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 271 | // 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] | 272 | // on MemDepResult's default constructing to 'dirty'. |
| 273 | if (!LocalCache.isDirty()) |
| 274 | return LocalCache; |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 275 | |
| 276 | // Otherwise, if we have a dirty entry, we know we can start the scan at that |
| 277 | // instruction, which may save us some work. |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 278 | if (Instruction *Inst = LocalCache.getInst()) { |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 279 | ScanPos = Inst; |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 280 | |
Chris Lattner | d44745d | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 281 | RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst); |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 282 | } |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 283 | |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 284 | BasicBlock *QueryParent = QueryInst->getParent(); |
| 285 | |
| 286 | Value *MemPtr = 0; |
| 287 | uint64_t MemSize = 0; |
| 288 | |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 289 | // Do the scan. |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 290 | if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) { |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 291 | // No dependence found. If this is the entry block of the function, it is a |
| 292 | // clobber, otherwise it is non-local. |
| 293 | if (QueryParent != &QueryParent->getParent()->getEntryBlock()) |
| 294 | LocalCache = MemDepResult::getNonLocal(); |
| 295 | else |
| 296 | LocalCache = MemDepResult::getClobber(QueryInst); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 297 | } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) { |
| 298 | // If this is a volatile store, don't mess around with it. Just return the |
| 299 | // previous instruction as a clobber. |
| 300 | if (SI->isVolatile()) |
| 301 | LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 302 | else { |
| 303 | MemPtr = SI->getPointerOperand(); |
| 304 | MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType()); |
| 305 | } |
| 306 | } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) { |
| 307 | // If this is a volatile load, don't mess around with it. Just return the |
| 308 | // previous instruction as a clobber. |
| 309 | if (LI->isVolatile()) |
| 310 | LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 311 | else { |
| 312 | MemPtr = LI->getPointerOperand(); |
| 313 | MemSize = TD->getTypeStoreSize(LI->getType()); |
| 314 | } |
| 315 | } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) { |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 316 | CallSite QueryCS = CallSite::get(QueryInst); |
| 317 | bool isReadOnly = AA->onlyReadsMemory(QueryCS); |
| 318 | LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos, |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 319 | QueryParent); |
| 320 | } else if (FreeInst *FI = dyn_cast<FreeInst>(QueryInst)) { |
| 321 | MemPtr = FI->getPointerOperand(); |
| 322 | // FreeInsts erase the entire structure, not just a field. |
| 323 | MemSize = ~0UL; |
| 324 | } else { |
| 325 | // Non-memory instruction. |
| 326 | LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos)); |
| 327 | } |
| 328 | |
| 329 | // If we need to do a pointer scan, make it happen. |
| 330 | if (MemPtr) |
| 331 | LocalCache = getPointerDependencyFrom(MemPtr, MemSize, |
| 332 | isa<LoadInst>(QueryInst), |
| 333 | ScanPos, QueryParent); |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 334 | |
| 335 | // Remember the result! |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 336 | if (Instruction *I = LocalCache.getInst()) |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 337 | ReverseLocalDeps[I].insert(QueryInst); |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 338 | |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 339 | return LocalCache; |
Chris Lattner | 5391a1d | 2008-11-29 03:47:00 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 342 | #ifndef NDEBUG |
| 343 | /// AssertSorted - This method is used when -debug is specified to verify that |
| 344 | /// cache arrays are properly kept sorted. |
| 345 | static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache, |
| 346 | int Count = -1) { |
| 347 | if (Count == -1) Count = Cache.size(); |
| 348 | if (Count == 0) return; |
| 349 | |
| 350 | for (unsigned i = 1; i != unsigned(Count); ++i) |
| 351 | assert(Cache[i-1] <= Cache[i] && "Cache isn't sorted!"); |
| 352 | } |
| 353 | #endif |
| 354 | |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 355 | /// getNonLocalCallDependency - Perform a full dependency query for the |
| 356 | /// specified call, returning the set of blocks that the value is |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 357 | /// potentially live across. The returned set of results will include a |
| 358 | /// "NonLocal" result for all blocks where the value is live across. |
| 359 | /// |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 360 | /// This method assumes the instruction returns a "NonLocal" dependency |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 361 | /// within its own block. |
| 362 | /// |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 363 | /// This returns a reference to an internal data structure that may be |
| 364 | /// invalidated on the next non-local query or when an instruction is |
| 365 | /// removed. Clients must copy this data if they want it around longer than |
| 366 | /// that. |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 367 | const MemoryDependenceAnalysis::NonLocalDepInfo & |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 368 | MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) { |
| 369 | assert(getDependency(QueryCS.getInstruction()).isNonLocal() && |
| 370 | "getNonLocalCallDependency should only be used on calls with non-local deps!"); |
| 371 | PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()]; |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 372 | NonLocalDepInfo &Cache = CacheP.first; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 373 | |
| 374 | /// DirtyBlocks - This is the set of blocks that need to be recomputed. In |
| 375 | /// the cached case, this can happen due to instructions being deleted etc. In |
| 376 | /// the uncached case, this starts out as the set of predecessors we care |
| 377 | /// about. |
| 378 | SmallVector<BasicBlock*, 32> DirtyBlocks; |
| 379 | |
| 380 | if (!Cache.empty()) { |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 381 | // Okay, we have a cache entry. If we know it is not dirty, just return it |
| 382 | // with no computation. |
| 383 | if (!CacheP.second) { |
| 384 | NumCacheNonLocal++; |
| 385 | return Cache; |
| 386 | } |
| 387 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 388 | // 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] | 389 | // determine what is dirty, seeding our initial DirtyBlocks worklist. |
| 390 | for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end(); |
| 391 | I != E; ++I) |
| 392 | if (I->second.isDirty()) |
| 393 | DirtyBlocks.push_back(I->first); |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 394 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 395 | // Sort the cache so that we can do fast binary search lookups below. |
| 396 | std::sort(Cache.begin(), Cache.end()); |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 397 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 398 | ++NumCacheDirtyNonLocal; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 399 | //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: " |
| 400 | // << Cache.size() << " cached: " << *QueryInst; |
| 401 | } else { |
| 402 | // Seed DirtyBlocks with each of the preds of QueryInst's block. |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 403 | BasicBlock *QueryBB = QueryCS.getInstruction()->getParent(); |
Chris Lattner | 511b36c | 2008-12-09 06:44:17 +0000 | [diff] [blame] | 404 | for (BasicBlock **PI = PredCache->GetPreds(QueryBB); *PI; ++PI) |
| 405 | DirtyBlocks.push_back(*PI); |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 406 | NumUncacheNonLocal++; |
| 407 | } |
| 408 | |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 409 | // isReadonlyCall - If this is a read-only call, we can be more aggressive. |
| 410 | bool isReadonlyCall = AA->onlyReadsMemory(QueryCS); |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 411 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 412 | SmallPtrSet<BasicBlock*, 64> Visited; |
| 413 | |
| 414 | unsigned NumSortedEntries = Cache.size(); |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 415 | DEBUG(AssertSorted(Cache)); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 417 | // Iterate while we still have blocks to update. |
| 418 | while (!DirtyBlocks.empty()) { |
| 419 | BasicBlock *DirtyBB = DirtyBlocks.back(); |
| 420 | DirtyBlocks.pop_back(); |
| 421 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 422 | // Already processed this block? |
| 423 | if (!Visited.insert(DirtyBB)) |
| 424 | continue; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 425 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 426 | // Do a binary search to see if we already have an entry for this block in |
| 427 | // the cache set. If so, find it. |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 428 | DEBUG(AssertSorted(Cache, NumSortedEntries)); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 429 | NonLocalDepInfo::iterator Entry = |
| 430 | std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries, |
| 431 | std::make_pair(DirtyBB, MemDepResult())); |
Duncan Sands | 7050f3d | 2008-12-10 09:38:36 +0000 | [diff] [blame] | 432 | if (Entry != Cache.begin() && prior(Entry)->first == DirtyBB) |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 433 | --Entry; |
| 434 | |
| 435 | MemDepResult *ExistingResult = 0; |
| 436 | if (Entry != Cache.begin()+NumSortedEntries && |
| 437 | Entry->first == DirtyBB) { |
| 438 | // If we already have an entry, and if it isn't already dirty, the block |
| 439 | // is done. |
| 440 | if (!Entry->second.isDirty()) |
| 441 | continue; |
| 442 | |
| 443 | // Otherwise, remember this slot so we can update the value. |
| 444 | ExistingResult = &Entry->second; |
| 445 | } |
| 446 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 447 | // If the dirty entry has a pointer, start scanning from it so we don't have |
| 448 | // to rescan the entire block. |
| 449 | BasicBlock::iterator ScanPos = DirtyBB->end(); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 450 | if (ExistingResult) { |
| 451 | if (Instruction *Inst = ExistingResult->getInst()) { |
| 452 | ScanPos = Inst; |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 453 | // We're removing QueryInst's use of Inst. |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 454 | RemoveFromReverseMap(ReverseNonLocalDeps, Inst, |
| 455 | QueryCS.getInstruction()); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 456 | } |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 457 | } |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 458 | |
Chris Lattner | 73ec3cd | 2008-11-30 01:26:32 +0000 | [diff] [blame] | 459 | // Find out if this block has a local dependency for QueryInst. |
Chris Lattner | d8dd934 | 2008-12-07 01:21:14 +0000 | [diff] [blame] | 460 | MemDepResult Dep; |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 462 | if (ScanPos != DirtyBB->begin()) { |
Chris Lattner | 20d6f09 | 2008-12-09 21:19:42 +0000 | [diff] [blame] | 463 | Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB); |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 464 | } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) { |
| 465 | // No dependence found. If this is the entry block of the function, it is |
| 466 | // a clobber, otherwise it is non-local. |
| 467 | Dep = MemDepResult::getNonLocal(); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 468 | } else { |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 469 | Dep = MemDepResult::getClobber(ScanPos); |
Chris Lattner | e79be94 | 2008-12-07 01:50:16 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 472 | // If we had a dirty entry for the block, update it. Otherwise, just add |
| 473 | // a new entry. |
| 474 | if (ExistingResult) |
| 475 | *ExistingResult = Dep; |
| 476 | else |
| 477 | Cache.push_back(std::make_pair(DirtyBB, Dep)); |
| 478 | |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 479 | // 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] | 480 | // the value), remember the association! |
| 481 | if (!Dep.isNonLocal()) { |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 482 | // Keep the ReverseNonLocalDeps map up to date so we can efficiently |
| 483 | // update this when we remove instructions. |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 484 | if (Instruction *Inst = Dep.getInst()) |
Chris Lattner | 1559b36 | 2008-12-09 19:38:05 +0000 | [diff] [blame] | 485 | ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction()); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 486 | } else { |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 487 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 488 | // If the block *is* completely transparent to the load, we need to check |
| 489 | // the predecessors of this block. Add them to our worklist. |
Chris Lattner | 511b36c | 2008-12-09 06:44:17 +0000 | [diff] [blame] | 490 | for (BasicBlock **PI = PredCache->GetPreds(DirtyBB); *PI; ++PI) |
| 491 | DirtyBlocks.push_back(*PI); |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 492 | } |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 495 | return Cache; |
Chris Lattner | 37d041c | 2008-11-30 01:18:27 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 498 | /// getNonLocalPointerDependency - Perform a full dependency query for an |
| 499 | /// access to the specified (non-volatile) memory location, returning the |
| 500 | /// set of instructions that either define or clobber the value. |
| 501 | /// |
| 502 | /// This method assumes the pointer has a "NonLocal" dependency within its |
| 503 | /// own block. |
| 504 | /// |
| 505 | void MemoryDependenceAnalysis:: |
| 506 | getNonLocalPointerDependency(Value *Pointer, bool isLoad, BasicBlock *FromBB, |
| 507 | SmallVectorImpl<NonLocalDepEntry> &Result) { |
Chris Lattner | 3f7eb5b | 2008-12-07 18:45:15 +0000 | [diff] [blame] | 508 | assert(isa<PointerType>(Pointer->getType()) && |
| 509 | "Can't get pointer deps of a non-pointer!"); |
Chris Lattner | 9a193fd | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 510 | Result.clear(); |
| 511 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 512 | // We know that the pointer value is live into FromBB find the def/clobbers |
| 513 | // from presecessors. |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 514 | const Type *EltTy = cast<PointerType>(Pointer->getType())->getElementType(); |
| 515 | uint64_t PointeeSize = TD->getTypeStoreSize(EltTy); |
| 516 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 517 | // This is the set of blocks we've inspected, and the pointer we consider in |
| 518 | // each block. Because of critical edges, we currently bail out if querying |
| 519 | // a block with multiple different pointers. This can happen during PHI |
| 520 | // translation. |
| 521 | DenseMap<BasicBlock*, Value*> Visited; |
| 522 | if (!getNonLocalPointerDepFromBB(Pointer, PointeeSize, isLoad, FromBB, |
| 523 | Result, Visited, true)) |
| 524 | return; |
Chris Lattner | 3af23f8 | 2008-12-15 04:58:29 +0000 | [diff] [blame] | 525 | Result.clear(); |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 526 | Result.push_back(std::make_pair(FromBB, |
| 527 | MemDepResult::getClobber(FromBB->begin()))); |
Chris Lattner | 9a193fd | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 530 | /// GetNonLocalInfoForBlock - Compute the memdep value for BB with |
| 531 | /// Pointer/PointeeSize using either cached information in Cache or by doing a |
| 532 | /// lookup (which may use dirty cache info if available). If we do a lookup, |
| 533 | /// add the result to the cache. |
| 534 | MemDepResult MemoryDependenceAnalysis:: |
| 535 | GetNonLocalInfoForBlock(Value *Pointer, uint64_t PointeeSize, |
| 536 | bool isLoad, BasicBlock *BB, |
| 537 | NonLocalDepInfo *Cache, unsigned NumSortedEntries) { |
| 538 | |
| 539 | // Do a binary search to see if we already have an entry for this block in |
| 540 | // the cache set. If so, find it. |
| 541 | NonLocalDepInfo::iterator Entry = |
| 542 | std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries, |
| 543 | std::make_pair(BB, MemDepResult())); |
Duncan Sands | 7050f3d | 2008-12-10 09:38:36 +0000 | [diff] [blame] | 544 | if (Entry != Cache->begin() && prior(Entry)->first == BB) |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 545 | --Entry; |
| 546 | |
| 547 | MemDepResult *ExistingResult = 0; |
| 548 | if (Entry != Cache->begin()+NumSortedEntries && Entry->first == BB) |
| 549 | ExistingResult = &Entry->second; |
| 550 | |
| 551 | // If we have a cached entry, and it is non-dirty, use it as the value for |
| 552 | // this dependency. |
| 553 | if (ExistingResult && !ExistingResult->isDirty()) { |
| 554 | ++NumCacheNonLocalPtr; |
| 555 | return *ExistingResult; |
| 556 | } |
| 557 | |
| 558 | // Otherwise, we have to scan for the value. If we have a dirty cache |
| 559 | // entry, start scanning from its position, otherwise we scan from the end |
| 560 | // of the block. |
| 561 | BasicBlock::iterator ScanPos = BB->end(); |
| 562 | if (ExistingResult && ExistingResult->getInst()) { |
| 563 | assert(ExistingResult->getInst()->getParent() == BB && |
| 564 | "Instruction invalidated?"); |
| 565 | ++NumCacheDirtyNonLocalPtr; |
| 566 | ScanPos = ExistingResult->getInst(); |
| 567 | |
| 568 | // Eliminating the dirty entry from 'Cache', so update the reverse info. |
| 569 | ValueIsLoadPair CacheKey(Pointer, isLoad); |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 570 | RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey); |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 571 | } else { |
| 572 | ++NumUncacheNonLocalPtr; |
| 573 | } |
| 574 | |
| 575 | // Scan the block for the dependency. |
| 576 | MemDepResult Dep = getPointerDependencyFrom(Pointer, PointeeSize, isLoad, |
| 577 | ScanPos, BB); |
| 578 | |
| 579 | // If we had a dirty entry for the block, update it. Otherwise, just add |
| 580 | // a new entry. |
| 581 | if (ExistingResult) |
| 582 | *ExistingResult = Dep; |
| 583 | else |
| 584 | Cache->push_back(std::make_pair(BB, Dep)); |
| 585 | |
| 586 | // If the block has a dependency (i.e. it isn't completely transparent to |
| 587 | // the value), remember the reverse association because we just added it |
| 588 | // to Cache! |
| 589 | if (Dep.isNonLocal()) |
| 590 | return Dep; |
| 591 | |
| 592 | // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently |
| 593 | // update MemDep when we remove instructions. |
| 594 | Instruction *Inst = Dep.getInst(); |
| 595 | assert(Inst && "Didn't depend on anything?"); |
| 596 | ValueIsLoadPair CacheKey(Pointer, isLoad); |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 597 | ReverseNonLocalPtrDeps[Inst].insert(CacheKey); |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 598 | return Dep; |
| 599 | } |
| 600 | |
Chris Lattner | a2f55dd | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 601 | /// SortNonLocalDepInfoCache - Sort the a NonLocalDepInfo cache, given a certain |
| 602 | /// number of elements in the array that are already properly ordered. This is |
| 603 | /// optimized for the case when only a few entries are added. |
| 604 | static void |
| 605 | SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache, |
| 606 | unsigned NumSortedEntries) { |
| 607 | switch (Cache.size() - NumSortedEntries) { |
| 608 | case 0: |
| 609 | // done, no new entries. |
| 610 | break; |
| 611 | case 2: { |
| 612 | // Two new entries, insert the last one into place. |
| 613 | MemoryDependenceAnalysis::NonLocalDepEntry Val = Cache.back(); |
| 614 | Cache.pop_back(); |
| 615 | MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry = |
| 616 | std::upper_bound(Cache.begin(), Cache.end()-1, Val); |
| 617 | Cache.insert(Entry, Val); |
| 618 | // FALL THROUGH. |
| 619 | } |
| 620 | case 1: |
| 621 | // One new entry, Just insert the new value at the appropriate position. |
| 622 | if (Cache.size() != 1) { |
| 623 | MemoryDependenceAnalysis::NonLocalDepEntry Val = Cache.back(); |
| 624 | Cache.pop_back(); |
| 625 | MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry = |
| 626 | std::upper_bound(Cache.begin(), Cache.end(), Val); |
| 627 | Cache.insert(Entry, Val); |
| 628 | } |
| 629 | break; |
| 630 | default: |
| 631 | // Added many values, do a full scale sort. |
| 632 | std::sort(Cache.begin(), Cache.end()); |
| 633 | break; |
| 634 | } |
| 635 | } |
| 636 | |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 638 | /// getNonLocalPointerDepFromBB - Perform a dependency query based on |
| 639 | /// pointer/pointeesize starting at the end of StartBB. Add any clobber/def |
| 640 | /// results to the results vector and keep track of which blocks are visited in |
| 641 | /// 'Visited'. |
| 642 | /// |
| 643 | /// This has special behavior for the first block queries (when SkipFirstBlock |
| 644 | /// is true). In this special case, it ignores the contents of the specified |
| 645 | /// block and starts returning dependence info for its predecessors. |
| 646 | /// |
| 647 | /// This function returns false on success, or true to indicate that it could |
| 648 | /// not compute dependence information for some reason. This should be treated |
| 649 | /// as a clobber dependence on the first instruction in the predecessor block. |
| 650 | bool MemoryDependenceAnalysis:: |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 651 | getNonLocalPointerDepFromBB(Value *Pointer, uint64_t PointeeSize, |
| 652 | bool isLoad, BasicBlock *StartBB, |
| 653 | SmallVectorImpl<NonLocalDepEntry> &Result, |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 654 | DenseMap<BasicBlock*, Value*> &Visited, |
| 655 | bool SkipFirstBlock) { |
| 656 | |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 657 | // Look up the cached info for Pointer. |
| 658 | ValueIsLoadPair CacheKey(Pointer, isLoad); |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 660 | std::pair<BBSkipFirstBlockPair, NonLocalDepInfo> *CacheInfo = |
| 661 | &NonLocalPointerDeps[CacheKey]; |
| 662 | NonLocalDepInfo *Cache = &CacheInfo->second; |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 663 | |
| 664 | // If we have valid cached information for exactly the block we are |
| 665 | // investigating, just return it with no recomputation. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 666 | if (CacheInfo->first == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) { |
Chris Lattner | f478951 | 2008-12-16 07:10:09 +0000 | [diff] [blame] | 667 | // We have a fully cached result for this query then we can just return the |
| 668 | // cached results and populate the visited set. However, we have to verify |
| 669 | // that we don't already have conflicting results for these blocks. Check |
| 670 | // to ensure that if a block in the results set is in the visited set that |
| 671 | // it was for the same pointer query. |
| 672 | if (!Visited.empty()) { |
| 673 | for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end(); |
| 674 | I != E; ++I) { |
| 675 | DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->first); |
| 676 | if (VI == Visited.end() || VI->second == Pointer) continue; |
| 677 | |
| 678 | // We have a pointer mismatch in a block. Just return clobber, saying |
| 679 | // that something was clobbered in this result. We could also do a |
| 680 | // non-fully cached query, but there is little point in doing this. |
| 681 | return true; |
| 682 | } |
| 683 | } |
| 684 | |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 685 | for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end(); |
Chris Lattner | f478951 | 2008-12-16 07:10:09 +0000 | [diff] [blame] | 686 | I != E; ++I) { |
| 687 | Visited.insert(std::make_pair(I->first, Pointer)); |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 688 | if (!I->second.isNonLocal()) |
| 689 | Result.push_back(*I); |
Chris Lattner | f478951 | 2008-12-16 07:10:09 +0000 | [diff] [blame] | 690 | } |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 691 | ++NumCacheCompleteNonLocalPtr; |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 692 | return false; |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | // Otherwise, either this is a new block, a block with an invalid cache |
| 696 | // pointer or one that we're about to invalidate by putting more info into it |
| 697 | // than its valid cache info. If empty, the result will be valid cache info, |
| 698 | // otherwise it isn't. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 699 | if (Cache->empty()) |
| 700 | CacheInfo->first = BBSkipFirstBlockPair(StartBB, SkipFirstBlock); |
| 701 | else |
| 702 | CacheInfo->first = BBSkipFirstBlockPair(); |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 703 | |
| 704 | SmallVector<BasicBlock*, 32> Worklist; |
| 705 | Worklist.push_back(StartBB); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 706 | |
| 707 | // Keep track of the entries that we know are sorted. Previously cached |
| 708 | // entries will all be sorted. The entries we add we only sort on demand (we |
| 709 | // don't insert every element into its sorted position). We know that we |
| 710 | // won't get any reuse from currently inserted values, because we don't |
| 711 | // revisit blocks after we insert info for them. |
| 712 | unsigned NumSortedEntries = Cache->size(); |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 713 | DEBUG(AssertSorted(*Cache)); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 715 | while (!Worklist.empty()) { |
Chris Lattner | 9a193fd | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 716 | BasicBlock *BB = Worklist.pop_back_val(); |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 6563371 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 718 | // Skip the first block if we have it. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 719 | if (!SkipFirstBlock) { |
Chris Lattner | 6563371 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 720 | // Analyze the dependency of *Pointer in FromBB. See if we already have |
| 721 | // been here. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 722 | assert(Visited.count(BB) && "Should check 'visited' before adding to WL"); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 723 | |
Chris Lattner | 6563371 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 724 | // Get the dependency info for Pointer in BB. If we have cached |
| 725 | // information, we will use it, otherwise we compute it. |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 726 | DEBUG(AssertSorted(*Cache, NumSortedEntries)); |
Chris Lattner | 6563371 | 2008-12-09 07:52:59 +0000 | [diff] [blame] | 727 | MemDepResult Dep = GetNonLocalInfoForBlock(Pointer, PointeeSize, isLoad, |
| 728 | BB, Cache, NumSortedEntries); |
| 729 | |
| 730 | // If we got a Def or Clobber, add this to the list of results. |
| 731 | if (!Dep.isNonLocal()) { |
| 732 | Result.push_back(NonLocalDepEntry(BB, Dep)); |
| 733 | continue; |
| 734 | } |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 737 | // If 'Pointer' is an instruction defined in this block, then we need to do |
| 738 | // phi translation to change it into a value live in the predecessor block. |
| 739 | // If phi translation fails, then we can't continue dependence analysis. |
| 740 | Instruction *PtrInst = dyn_cast<Instruction>(Pointer); |
| 741 | bool NeedsPHITranslation = PtrInst && PtrInst->getParent() == BB; |
| 742 | |
| 743 | // If no PHI translation is needed, just add all the predecessors of this |
| 744 | // block to scan them as well. |
| 745 | if (!NeedsPHITranslation) { |
| 746 | SkipFirstBlock = false; |
| 747 | for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) { |
| 748 | // Verify that we haven't looked at this block yet. |
| 749 | std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool> |
| 750 | InsertRes = Visited.insert(std::make_pair(*PI, Pointer)); |
| 751 | if (InsertRes.second) { |
| 752 | // First time we've looked at *PI. |
| 753 | Worklist.push_back(*PI); |
| 754 | continue; |
| 755 | } |
| 756 | |
| 757 | // If we have seen this block before, but it was with a different |
| 758 | // pointer then we have a phi translation failure and we have to treat |
| 759 | // this as a clobber. |
| 760 | if (InsertRes.first->second != Pointer) |
| 761 | goto PredTranslationFailure; |
| 762 | } |
| 763 | continue; |
| 764 | } |
| 765 | |
| 766 | // If we do need to do phi translation, then there are a bunch of different |
| 767 | // cases, because we have to find a Value* live in the predecessor block. We |
| 768 | // know that PtrInst is defined in this block at least. |
Chris Lattner | 6fbc196 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 769 | |
| 770 | // We may have added values to the cache list before this PHI translation. |
| 771 | // If so, we haven't done anything to ensure that the cache remains sorted. |
| 772 | // Sort it now (if needed) so that recursive invocations of |
| 773 | // getNonLocalPointerDepFromBB and other routines that could reuse the cache |
| 774 | // value will only see properly sorted cache arrays. |
| 775 | if (Cache && NumSortedEntries != Cache->size()) { |
Chris Lattner | a2f55dd | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 776 | SortNonLocalDepInfoCache(*Cache, NumSortedEntries); |
Chris Lattner | 6fbc196 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 777 | NumSortedEntries = Cache->size(); |
| 778 | } |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 779 | |
| 780 | // If this is directly a PHI node, just use the incoming values for each |
| 781 | // pred as the phi translated version. |
| 782 | if (PHINode *PtrPHI = dyn_cast<PHINode>(PtrInst)) { |
Chris Lattner | 6fbc196 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 783 | Cache = 0; |
| 784 | |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 785 | for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) { |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 786 | BasicBlock *Pred = *PI; |
| 787 | Value *PredPtr = PtrPHI->getIncomingValueForBlock(Pred); |
| 788 | |
| 789 | // Check to see if we have already visited this pred block with another |
| 790 | // pointer. If so, we can't do this lookup. This failure can occur |
| 791 | // with PHI translation when a critical edge exists and the PHI node in |
| 792 | // the successor translates to a pointer value different than the |
| 793 | // pointer the block was first analyzed with. |
| 794 | std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool> |
| 795 | InsertRes = Visited.insert(std::make_pair(Pred, PredPtr)); |
| 796 | |
| 797 | if (!InsertRes.second) { |
| 798 | // If the predecessor was visited with PredPtr, then we already did |
| 799 | // the analysis and can ignore it. |
| 800 | if (InsertRes.first->second == PredPtr) |
| 801 | continue; |
| 802 | |
| 803 | // Otherwise, the block was previously analyzed with a different |
| 804 | // pointer. We can't represent the result of this case, so we just |
| 805 | // treat this as a phi translation failure. |
| 806 | goto PredTranslationFailure; |
| 807 | } |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 808 | |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 809 | // FIXME: it is entirely possible that PHI translating will end up with |
| 810 | // the same value. Consider PHI translating something like: |
| 811 | // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need* |
| 812 | // to recurse here, pedantically speaking. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 813 | |
| 814 | // If we have a problem phi translating, fall through to the code below |
| 815 | // to handle the failure condition. |
| 816 | if (getNonLocalPointerDepFromBB(PredPtr, PointeeSize, isLoad, Pred, |
| 817 | Result, Visited)) |
| 818 | goto PredTranslationFailure; |
| 819 | } |
Chris Lattner | 6fbc196 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 820 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 821 | // Refresh the CacheInfo/Cache pointer so that it isn't invalidated. |
| 822 | CacheInfo = &NonLocalPointerDeps[CacheKey]; |
| 823 | Cache = &CacheInfo->second; |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 824 | NumSortedEntries = Cache->size(); |
Chris Lattner | b54bfc2 | 2009-01-23 00:27:03 +0000 | [diff] [blame] | 825 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 826 | // Since we did phi translation, the "Cache" set won't contain all of the |
| 827 | // results for the query. This is ok (we can still use it to accelerate |
| 828 | // specific block queries) but we can't do the fastpath "return all |
| 829 | // results from the set" Clear out the indicator for this. |
| 830 | CacheInfo->first = BBSkipFirstBlockPair(); |
| 831 | SkipFirstBlock = false; |
| 832 | continue; |
| 833 | } |
| 834 | |
| 835 | // TODO: BITCAST, GEP. |
| 836 | |
| 837 | // cerr << "MEMDEP: Could not PHI translate: " << *Pointer; |
| 838 | // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst)) |
| 839 | // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0); |
| 840 | PredTranslationFailure: |
| 841 | |
Chris Lattner | 95900f2 | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 842 | if (Cache == 0) { |
| 843 | // Refresh the CacheInfo/Cache pointer if it got invalidated. |
| 844 | CacheInfo = &NonLocalPointerDeps[CacheKey]; |
| 845 | Cache = &CacheInfo->second; |
| 846 | NumSortedEntries = Cache->size(); |
Chris Lattner | 95900f2 | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 847 | } |
Chris Lattner | 6fbc196 | 2009-07-13 17:14:23 +0000 | [diff] [blame] | 848 | |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 849 | // Since we did phi translation, the "Cache" set won't contain all of the |
| 850 | // results for the query. This is ok (we can still use it to accelerate |
| 851 | // specific block queries) but we can't do the fastpath "return all |
| 852 | // results from the set" Clear out the indicator for this. |
| 853 | CacheInfo->first = BBSkipFirstBlockPair(); |
| 854 | |
| 855 | // If *nothing* works, mark the pointer as being clobbered by the first |
| 856 | // instruction in this block. |
| 857 | // |
| 858 | // If this is the magic first block, return this as a clobber of the whole |
| 859 | // incoming value. Since we can't phi translate to one of the predecessors, |
| 860 | // we have to bail out. |
| 861 | if (SkipFirstBlock) |
| 862 | return true; |
| 863 | |
| 864 | for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) { |
| 865 | assert(I != Cache->rend() && "Didn't find current block??"); |
| 866 | if (I->first != BB) |
| 867 | continue; |
| 868 | |
| 869 | assert(I->second.isNonLocal() && |
| 870 | "Should only be here with transparent block"); |
| 871 | I->second = MemDepResult::getClobber(BB->begin()); |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 872 | ReverseNonLocalPtrDeps[BB->begin()].insert(CacheKey); |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 873 | Result.push_back(*I); |
| 874 | break; |
Chris Lattner | 9a193fd | 2008-12-07 02:56:57 +0000 | [diff] [blame] | 875 | } |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 876 | } |
Chris Lattner | 95900f2 | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 877 | |
Chris Lattner | 9863c3f | 2008-12-09 07:47:11 +0000 | [diff] [blame] | 878 | // Okay, we're done now. If we added new values to the cache, re-sort it. |
Chris Lattner | a2f55dd | 2009-07-13 17:20:05 +0000 | [diff] [blame] | 879 | SortNonLocalDepInfoCache(*Cache, NumSortedEntries); |
Chris Lattner | 12a7db3 | 2009-01-22 07:04:01 +0000 | [diff] [blame] | 880 | DEBUG(AssertSorted(*Cache)); |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 881 | return false; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /// RemoveCachedNonLocalPointerDependencies - If P exists in |
| 885 | /// CachedNonLocalPointerInfo, remove it. |
| 886 | void MemoryDependenceAnalysis:: |
| 887 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) { |
| 888 | CachedNonLocalPointerInfo::iterator It = |
| 889 | NonLocalPointerDeps.find(P); |
| 890 | if (It == NonLocalPointerDeps.end()) return; |
| 891 | |
| 892 | // Remove all of the entries in the BB->val map. This involves removing |
| 893 | // instructions from the reverse map. |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 894 | NonLocalDepInfo &PInfo = It->second.second; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 895 | |
| 896 | for (unsigned i = 0, e = PInfo.size(); i != e; ++i) { |
| 897 | Instruction *Target = PInfo[i].second.getInst(); |
| 898 | if (Target == 0) continue; // Ignore non-local dep results. |
Chris Lattner | 5a45bf1 | 2008-12-09 22:45:32 +0000 | [diff] [blame] | 899 | assert(Target->getParent() == PInfo[i].first); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 900 | |
| 901 | // Eliminating the dirty entry from 'Cache', so update the reverse info. |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 902 | RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo). |
| 906 | NonLocalPointerDeps.erase(It); |
Chris Lattner | 7ebcf03 | 2008-12-07 02:15:47 +0000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | |
Chris Lattner | bc99be1 | 2008-12-09 22:06:23 +0000 | [diff] [blame] | 910 | /// invalidateCachedPointerInfo - This method is used to invalidate cached |
| 911 | /// information about the specified pointer, because it may be too |
| 912 | /// conservative in memdep. This is an optional call that can be used when |
| 913 | /// the client detects an equivalence between the pointer and some other |
| 914 | /// value and replaces the other value with ptr. This can make Ptr available |
| 915 | /// in more places that cached info does not necessarily keep. |
| 916 | void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) { |
| 917 | // If Ptr isn't really a pointer, just ignore it. |
| 918 | if (!isa<PointerType>(Ptr->getType())) return; |
| 919 | // Flush store info for the pointer. |
| 920 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false)); |
| 921 | // Flush load info for the pointer. |
| 922 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true)); |
| 923 | } |
| 924 | |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 925 | /// removeInstruction - Remove an instruction from the dependence analysis, |
| 926 | /// updating the dependence of instructions that previously depended on it. |
Owen Anderson | 642a9e3 | 2007-08-08 22:26:03 +0000 | [diff] [blame] | 927 | /// This method attempts to keep the cache coherent using the reverse map. |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 928 | void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) { |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 929 | // Walk through the Non-local dependencies, removing this one as the value |
| 930 | // for any cached queries. |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 931 | NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst); |
| 932 | if (NLDI != NonLocalDeps.end()) { |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 933 | NonLocalDepInfo &BlockMap = NLDI->second.first; |
Chris Lattner | 25f4b2b | 2008-11-30 02:30:50 +0000 | [diff] [blame] | 934 | for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end(); |
| 935 | DI != DE; ++DI) |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 936 | if (Instruction *Inst = DI->second.getInst()) |
Chris Lattner | d44745d | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 937 | RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 938 | NonLocalDeps.erase(NLDI); |
| 939 | } |
Owen Anderson | 5fc4aba | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 940 | |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 941 | // 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] | 942 | // |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 943 | LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst); |
| 944 | if (LocalDepEntry != LocalDeps.end()) { |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 945 | // Remove us from DepInst's reverse set now that the local dep info is gone. |
Chris Lattner | d44745d | 2008-12-07 18:39:13 +0000 | [diff] [blame] | 946 | if (Instruction *Inst = LocalDepEntry->second.getInst()) |
| 947 | RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst); |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 948 | |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 949 | // Remove this local dependency info. |
Chris Lattner | 39f372e | 2008-11-29 01:43:36 +0000 | [diff] [blame] | 950 | LocalDeps.erase(LocalDepEntry); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | // If we have any cached pointer dependencies on this instruction, remove |
| 954 | // them. If the instruction has non-pointer type, then it can't be a pointer |
| 955 | // base. |
| 956 | |
| 957 | // Remove it from both the load info and the store info. The instruction |
| 958 | // can't be in either of these maps if it is non-pointer. |
| 959 | if (isa<PointerType>(RemInst->getType())) { |
| 960 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false)); |
| 961 | RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true)); |
| 962 | } |
Chris Lattner | baad888 | 2008-11-28 22:28:27 +0000 | [diff] [blame] | 963 | |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 964 | // Loop over all of the things that depend on the instruction we're removing. |
| 965 | // |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 966 | SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd; |
Chris Lattner | 0655f73 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 967 | |
| 968 | // If we find RemInst as a clobber or Def in any of the maps for other values, |
| 969 | // we need to replace its entry with a dirty version of the instruction after |
| 970 | // it. If RemInst is a terminator, we use a null dirty value. |
| 971 | // |
| 972 | // Using a dirty version of the instruction after RemInst saves having to scan |
| 973 | // the entire block to get to this point. |
| 974 | MemDepResult NewDirtyVal; |
| 975 | if (!RemInst->isTerminator()) |
| 976 | NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst)); |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 977 | |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 978 | ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst); |
| 979 | if (ReverseDepIt != ReverseLocalDeps.end()) { |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 980 | SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 981 | // RemInst can't be the terminator if it has local stuff depending on it. |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 982 | assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) && |
| 983 | "Nothing can locally depend on a terminator"); |
| 984 | |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 985 | for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(), |
| 986 | E = ReverseDeps.end(); I != E; ++I) { |
| 987 | Instruction *InstDependingOnRemInst = *I; |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 988 | assert(InstDependingOnRemInst != RemInst && |
| 989 | "Already removed our local dep info"); |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 990 | |
Chris Lattner | 0655f73 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 991 | LocalDeps[InstDependingOnRemInst] = NewDirtyVal; |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 992 | |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 993 | // Make sure to remember that new things depend on NewDepInst. |
Chris Lattner | 0655f73 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 994 | assert(NewDirtyVal.getInst() && "There is no way something else can have " |
| 995 | "a local dep on this if it is a terminator!"); |
| 996 | ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(), |
Chris Lattner | 125ce36 | 2008-11-30 01:09:30 +0000 | [diff] [blame] | 997 | InstDependingOnRemInst)); |
Chris Lattner | d3d12ec | 2008-11-28 22:51:08 +0000 | [diff] [blame] | 998 | } |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 999 | |
| 1000 | ReverseLocalDeps.erase(ReverseDepIt); |
| 1001 | |
| 1002 | // Add new reverse deps after scanning the set, to avoid invalidating the |
| 1003 | // 'ReverseDeps' reference. |
| 1004 | while (!ReverseDepsToAdd.empty()) { |
| 1005 | ReverseLocalDeps[ReverseDepsToAdd.back().first] |
| 1006 | .insert(ReverseDepsToAdd.back().second); |
| 1007 | ReverseDepsToAdd.pop_back(); |
| 1008 | } |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 1009 | } |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 8c46527 | 2008-11-29 09:20:15 +0000 | [diff] [blame] | 1011 | ReverseDepIt = ReverseNonLocalDeps.find(RemInst); |
| 1012 | if (ReverseDepIt != ReverseNonLocalDeps.end()) { |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1013 | SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second; |
| 1014 | for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end(); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1015 | I != E; ++I) { |
| 1016 | assert(*I != RemInst && "Already removed NonLocalDep info for RemInst"); |
| 1017 | |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 1018 | PerInstNLInfo &INLD = NonLocalDeps[*I]; |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 1019 | // The information is now dirty! |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 1020 | INLD.second = true; |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1021 | |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 1022 | for (NonLocalDepInfo::iterator DI = INLD.first.begin(), |
| 1023 | DE = INLD.first.end(); DI != DE; ++DI) { |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 1024 | if (DI->second.getInst() != RemInst) continue; |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1025 | |
| 1026 | // Convert to a dirty entry for the subsequent instruction. |
Chris Lattner | 0655f73 | 2008-12-07 18:42:51 +0000 | [diff] [blame] | 1027 | DI->second = NewDirtyVal; |
| 1028 | |
| 1029 | if (Instruction *NextI = NewDirtyVal.getInst()) |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1030 | ReverseDepsToAdd.push_back(std::make_pair(NextI, *I)); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1031 | } |
| 1032 | } |
Chris Lattner | 4f8c18c | 2008-11-29 23:30:39 +0000 | [diff] [blame] | 1033 | |
| 1034 | ReverseNonLocalDeps.erase(ReverseDepIt); |
| 1035 | |
Chris Lattner | 0ec48dd | 2008-11-29 22:02:15 +0000 | [diff] [blame] | 1036 | // Add new reverse deps after scanning the set, to avoid invalidating 'Set' |
| 1037 | while (!ReverseDepsToAdd.empty()) { |
| 1038 | ReverseNonLocalDeps[ReverseDepsToAdd.back().first] |
| 1039 | .insert(ReverseDepsToAdd.back().second); |
| 1040 | ReverseDepsToAdd.pop_back(); |
| 1041 | } |
Owen Anderson | 4d13de4 | 2007-08-16 21:27:05 +0000 | [diff] [blame] | 1042 | } |
Owen Anderson | 5fc4aba | 2007-12-08 01:37:09 +0000 | [diff] [blame] | 1043 | |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1044 | // If the instruction is in ReverseNonLocalPtrDeps then it appears as a |
| 1045 | // value in the NonLocalPointerDeps info. |
| 1046 | ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt = |
| 1047 | ReverseNonLocalPtrDeps.find(RemInst); |
| 1048 | if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) { |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1049 | SmallPtrSet<ValueIsLoadPair, 4> &Set = ReversePtrDepIt->second; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1050 | SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd; |
| 1051 | |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1052 | for (SmallPtrSet<ValueIsLoadPair, 4>::iterator I = Set.begin(), |
| 1053 | E = Set.end(); I != E; ++I) { |
| 1054 | ValueIsLoadPair P = *I; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1055 | assert(P.getPointer() != RemInst && |
| 1056 | "Already removed NonLocalPointerDeps info for RemInst"); |
| 1057 | |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1058 | NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].second; |
| 1059 | |
| 1060 | // The cache is not valid for any specific block anymore. |
Chris Lattner | 9e59c64 | 2008-12-15 03:35:32 +0000 | [diff] [blame] | 1061 | NonLocalPointerDeps[P].first = BBSkipFirstBlockPair(); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1062 | |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1063 | // Update any entries for RemInst to use the instruction after it. |
| 1064 | for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end(); |
| 1065 | DI != DE; ++DI) { |
| 1066 | if (DI->second.getInst() != RemInst) continue; |
| 1067 | |
| 1068 | // Convert to a dirty entry for the subsequent instruction. |
| 1069 | DI->second = NewDirtyVal; |
| 1070 | |
| 1071 | if (Instruction *NewDirtyInst = NewDirtyVal.getInst()) |
| 1072 | ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P)); |
| 1073 | } |
Chris Lattner | 95900f2 | 2009-01-23 07:12:16 +0000 | [diff] [blame] | 1074 | |
| 1075 | // Re-sort the NonLocalDepInfo. Changing the dirty entry to its |
| 1076 | // subsequent value may invalidate the sortedness. |
| 1077 | std::sort(NLPDI.begin(), NLPDI.end()); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | ReverseNonLocalPtrDeps.erase(ReversePtrDepIt); |
| 1081 | |
| 1082 | while (!ReversePtrDepsToAdd.empty()) { |
| 1083 | ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first] |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1084 | .insert(ReversePtrDepsToAdd.back().second); |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1085 | ReversePtrDepsToAdd.pop_back(); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1090 | assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?"); |
Chris Lattner | d777d40 | 2008-11-30 19:24:31 +0000 | [diff] [blame] | 1091 | AA->deleteValue(RemInst); |
Chris Lattner | 5f589dc | 2008-11-28 22:04:47 +0000 | [diff] [blame] | 1092 | DEBUG(verifyRemoved(RemInst)); |
Owen Anderson | 78e02f7 | 2007-07-06 23:14:35 +0000 | [diff] [blame] | 1093 | } |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1094 | /// verifyRemoved - Verify that the specified instruction does not occur |
| 1095 | /// in our internal data structures. |
| 1096 | void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const { |
| 1097 | for (LocalDepMapType::const_iterator I = LocalDeps.begin(), |
| 1098 | E = LocalDeps.end(); I != E; ++I) { |
| 1099 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 1100 | assert(I->second.getInst() != D && |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1101 | "Inst occurs in data structures"); |
| 1102 | } |
| 1103 | |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1104 | for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(), |
| 1105 | E = NonLocalPointerDeps.end(); I != E; ++I) { |
| 1106 | assert(I->first.getPointer() != D && "Inst occurs in NLPD map key"); |
Chris Lattner | 11dcd8d | 2008-12-08 07:31:50 +0000 | [diff] [blame] | 1107 | const NonLocalDepInfo &Val = I->second.second; |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1108 | for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end(); |
| 1109 | II != E; ++II) |
| 1110 | assert(II->second.getInst() != D && "Inst occurs as NLPD value"); |
| 1111 | } |
| 1112 | |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1113 | for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(), |
| 1114 | E = NonLocalDeps.end(); I != E; ++I) { |
| 1115 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 4a69bad | 2008-11-30 02:52:26 +0000 | [diff] [blame] | 1116 | const PerInstNLInfo &INLD = I->second; |
Chris Lattner | bf145d6 | 2008-12-01 01:15:42 +0000 | [diff] [blame] | 1117 | for (NonLocalDepInfo::const_iterator II = INLD.first.begin(), |
| 1118 | EE = INLD.first.end(); II != EE; ++II) |
Chris Lattner | fd3dcbe | 2008-11-30 23:17:19 +0000 | [diff] [blame] | 1119 | assert(II->second.getInst() != D && "Inst occurs in data structures"); |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(), |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1123 | E = ReverseLocalDeps.end(); I != E; ++I) { |
| 1124 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1125 | for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), |
| 1126 | EE = I->second.end(); II != EE; ++II) |
| 1127 | assert(*II != D && "Inst occurs in data structures"); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1128 | } |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1129 | |
| 1130 | for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(), |
| 1131 | E = ReverseNonLocalDeps.end(); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1132 | I != E; ++I) { |
| 1133 | assert(I->first != D && "Inst occurs in data structures"); |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1134 | for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(), |
| 1135 | EE = I->second.end(); II != EE; ++II) |
| 1136 | assert(*II != D && "Inst occurs in data structures"); |
Chris Lattner | f68f310 | 2008-11-30 02:28:25 +0000 | [diff] [blame] | 1137 | } |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1138 | |
| 1139 | for (ReverseNonLocalPtrDepTy::const_iterator |
| 1140 | I = ReverseNonLocalPtrDeps.begin(), |
| 1141 | E = ReverseNonLocalPtrDeps.end(); I != E; ++I) { |
| 1142 | assert(I->first != D && "Inst occurs in rev NLPD map"); |
| 1143 | |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1144 | for (SmallPtrSet<ValueIsLoadPair, 4>::const_iterator II = I->second.begin(), |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1145 | E = I->second.end(); II != E; ++II) |
Chris Lattner | 6a0dcc1 | 2009-03-29 00:24:04 +0000 | [diff] [blame] | 1146 | assert(*II != ValueIsLoadPair(D, false) && |
| 1147 | *II != ValueIsLoadPair(D, true) && |
Chris Lattner | 6290f5c | 2008-12-07 08:50:20 +0000 | [diff] [blame] | 1148 | "Inst occurs in ReverseNonLocalPtrDeps map"); |
| 1149 | } |
| 1150 | |
Chris Lattner | 729b237 | 2008-11-29 21:25:10 +0000 | [diff] [blame] | 1151 | } |