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