blob: 457e94fb0a1887312d828073c06990e1f25638d9 [file] [log] [blame]
Owen Anderson78e02f72007-07-06 23:14:35 +00001//===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Owen Anderson78e02f72007-07-06 23:14:35 +00007//
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 Anderson80b1f092007-08-08 22:01:54 +000012// alias analysis information, and tries to provide a lazy, caching interface to
Owen Anderson78e02f72007-07-06 23:14:35 +000013// a common kind of alias information query.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner0e575f42008-11-28 21:45:17 +000017#define DEBUG_TYPE "memdep"
Owen Anderson78e02f72007-07-06 23:14:35 +000018#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Owen Anderson7a616a12007-07-10 17:25:03 +000019#include "llvm/Constants.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000020#include "llvm/Instructions.h"
21#include "llvm/Function.h"
22#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattnerbaad8882008-11-28 22:28:27 +000023#include "llvm/ADT/Statistic.h"
Owen Anderson4beedbd2007-07-24 21:52:37 +000024#include "llvm/Support/CFG.h"
Chris Lattner0e575f42008-11-28 21:45:17 +000025#include "llvm/Support/Debug.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000026#include "llvm/Target/TargetData.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000027using namespace llvm;
28
Chris Lattnerbf145d62008-12-01 01:15:42 +000029STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
30STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
Chris Lattner0ec48dd2008-11-29 22:02:15 +000031STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Chris Lattner6290f5c2008-12-07 08:50:20 +000032
33STATISTIC(NumCacheNonLocalPtr,
34 "Number of fully cached non-local ptr responses");
35STATISTIC(NumCacheDirtyNonLocalPtr,
36 "Number of cached, but dirty, non-local ptr responses");
37STATISTIC(NumUncacheNonLocalPtr,
38 "Number of uncached non-local ptr responses");
39
Owen Anderson78e02f72007-07-06 23:14:35 +000040char MemoryDependenceAnalysis::ID = 0;
41
Owen Anderson78e02f72007-07-06 23:14:35 +000042// Register this pass...
Owen Anderson776ee1f2007-07-10 20:21:08 +000043static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Chris Lattner0e575f42008-11-28 21:45:17 +000044 "Memory Dependence Analysis", false, true);
Owen Anderson78e02f72007-07-06 23:14:35 +000045
46/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
47///
48void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.setPreservesAll();
50 AU.addRequiredTransitive<AliasAnalysis>();
51 AU.addRequiredTransitive<TargetData>();
52}
53
Chris Lattnerd777d402008-11-30 19:24:31 +000054bool MemoryDependenceAnalysis::runOnFunction(Function &) {
55 AA = &getAnalysis<AliasAnalysis>();
56 TD = &getAnalysis<TargetData>();
57 return false;
58}
59
Chris Lattnerd44745d2008-12-07 18:39:13 +000060/// RemoveFromReverseMap - This is a helper function that removes Val from
61/// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry.
62template <typename KeyTy>
63static void RemoveFromReverseMap(DenseMap<Instruction*,
64 SmallPtrSet<KeyTy*, 4> > &ReverseMap,
65 Instruction *Inst, KeyTy *Val) {
66 typename DenseMap<Instruction*, SmallPtrSet<KeyTy*, 4> >::iterator
67 InstIt = ReverseMap.find(Inst);
68 assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");
69 bool Found = InstIt->second.erase(Val);
70 assert(Found && "Invalid reverse map!"); Found=Found;
71 if (InstIt->second.empty())
72 ReverseMap.erase(InstIt);
73}
74
Chris Lattnerbf145d62008-12-01 01:15:42 +000075
Chris Lattner8ef57c52008-12-07 00:35:51 +000076/// getCallSiteDependencyFrom - Private helper for finding the local
77/// dependencies of a call site.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +000078MemDepResult MemoryDependenceAnalysis::
Chris Lattner8ef57c52008-12-07 00:35:51 +000079getCallSiteDependencyFrom(CallSite CS, BasicBlock::iterator ScanIt,
80 BasicBlock *BB) {
Owen Anderson642a9e32007-08-08 22:26:03 +000081 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +000082 while (ScanIt != BB->begin()) {
83 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +000084
85 // If this inst is a memory op, get the pointer it accessed
Chris Lattner00314b32008-11-29 09:15:21 +000086 Value *Pointer = 0;
87 uint64_t PointerSize = 0;
88 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
89 Pointer = S->getPointerOperand();
Chris Lattnerd777d402008-11-30 19:24:31 +000090 PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner00314b32008-11-29 09:15:21 +000091 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
92 Pointer = V->getOperand(0);
Chris Lattnerd777d402008-11-30 19:24:31 +000093 PointerSize = TD->getTypeStoreSize(V->getType());
Chris Lattner00314b32008-11-29 09:15:21 +000094 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
95 Pointer = F->getPointerOperand();
Owen Anderson5f323202007-07-10 17:59:22 +000096
97 // FreeInsts erase the entire structure
Chris Lattner6290f5c2008-12-07 08:50:20 +000098 PointerSize = ~0ULL;
Chris Lattner00314b32008-11-29 09:15:21 +000099 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000100 CallSite InstCS = CallSite::get(Inst);
101 // If these two calls do not interfere, look past it.
102 if (AA->getModRefInfo(CS, InstCS) == AliasAnalysis::NoModRef)
Chris Lattner00314b32008-11-29 09:15:21 +0000103 continue;
Chris Lattnerb51deb92008-12-05 21:04:20 +0000104
105 // FIXME: If this is a ref/ref result, we should ignore it!
106 // X = strlen(P);
107 // Y = strlen(Q);
108 // Z = strlen(P); // Z = X
109
110 // If they interfere, we generally return clobber. However, if they are
111 // calls to the same read-only functions we return Def.
112 if (!AA->onlyReadsMemory(CS) || CS.getCalledFunction() == 0 ||
113 CS.getCalledFunction() != InstCS.getCalledFunction())
114 return MemDepResult::getClobber(Inst);
115 return MemDepResult::getDef(Inst);
Chris Lattnercfbb6342008-11-30 01:44:00 +0000116 } else {
117 // Non-memory instruction.
Owen Anderson202da142007-07-10 20:39:07 +0000118 continue;
Chris Lattnercfbb6342008-11-30 01:44:00 +0000119 }
Owen Anderson5f323202007-07-10 17:59:22 +0000120
Chris Lattnerb51deb92008-12-05 21:04:20 +0000121 if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef)
122 return MemDepResult::getClobber(Inst);
Owen Anderson5f323202007-07-10 17:59:22 +0000123 }
124
Chris Lattner7ebcf032008-12-07 02:15:47 +0000125 // No dependence found. If this is the entry block of the function, it is a
126 // clobber, otherwise it is non-local.
127 if (BB != &BB->getParent()->getEntryBlock())
128 return MemDepResult::getNonLocal();
129 return MemDepResult::getClobber(ScanIt);
Owen Anderson5f323202007-07-10 17:59:22 +0000130}
131
Chris Lattnere79be942008-12-07 01:50:16 +0000132/// getPointerDependencyFrom - Return the instruction on which a memory
133/// location depends. If isLoad is true, this routine ignore may-aliases with
134/// read-only operations.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000135MemDepResult MemoryDependenceAnalysis::
Chris Lattnere79be942008-12-07 01:50:16 +0000136getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad,
137 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000138
Chris Lattner6290f5c2008-12-07 08:50:20 +0000139 // Walk backwards through the basic block, looking for dependencies.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000140 while (ScanIt != BB->begin()) {
141 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000142
Chris Lattnercfbb6342008-11-30 01:44:00 +0000143 // Values depend on loads if the pointers are must aliased. This means that
144 // a load depends on another must aliased load from the same value.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000145 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000146 Value *Pointer = LI->getPointerOperand();
147 uint64_t PointerSize = TD->getTypeStoreSize(LI->getType());
148
149 // If we found a pointer, check if it could be the same as our pointer.
Chris Lattnera161ab02008-11-29 09:09:48 +0000150 AliasAnalysis::AliasResult R =
Chris Lattnerd777d402008-11-30 19:24:31 +0000151 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
Chris Lattnera161ab02008-11-29 09:09:48 +0000152 if (R == AliasAnalysis::NoAlias)
153 continue;
154
155 // May-alias loads don't depend on each other without a dependence.
Chris Lattnere79be942008-12-07 01:50:16 +0000156 if (isLoad && R == AliasAnalysis::MayAlias)
Chris Lattnera161ab02008-11-29 09:09:48 +0000157 continue;
Chris Lattner6290f5c2008-12-07 08:50:20 +0000158 // Stores depend on may and must aliased loads, loads depend on must-alias
159 // loads.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000160 return MemDepResult::getDef(Inst);
161 }
162
163 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000164 Value *Pointer = SI->getPointerOperand();
165 uint64_t PointerSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
166
167 // If we found a pointer, check if it could be the same as our pointer.
168 AliasAnalysis::AliasResult R =
169 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
170
171 if (R == AliasAnalysis::NoAlias)
172 continue;
173 if (R == AliasAnalysis::MayAlias)
174 return MemDepResult::getClobber(Inst);
175 return MemDepResult::getDef(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000176 }
Chris Lattner237a8282008-11-30 01:39:32 +0000177
178 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattnerb51deb92008-12-05 21:04:20 +0000179 // the allocation, return Def. This means that there is no dependence and
Chris Lattner237a8282008-11-30 01:39:32 +0000180 // the access can be optimized based on that. For example, a load could
181 // turn into undef.
Chris Lattnera161ab02008-11-29 09:09:48 +0000182 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
Chris Lattner237a8282008-11-30 01:39:32 +0000183 Value *AccessPtr = MemPtr->getUnderlyingObject();
Owen Anderson78e02f72007-07-06 23:14:35 +0000184
Chris Lattner237a8282008-11-30 01:39:32 +0000185 if (AccessPtr == AI ||
Chris Lattnerd777d402008-11-30 19:24:31 +0000186 AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
Chris Lattnerb51deb92008-12-05 21:04:20 +0000187 return MemDepResult::getDef(AI);
Chris Lattner237a8282008-11-30 01:39:32 +0000188 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000189 }
Chris Lattnera161ab02008-11-29 09:09:48 +0000190
Chris Lattnerb51deb92008-12-05 21:04:20 +0000191 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
Chris Lattnere79be942008-12-07 01:50:16 +0000192 // FIXME: If this is a load, we should ignore readonly calls!
Chris Lattnerb51deb92008-12-05 21:04:20 +0000193 if (AA->getModRefInfo(Inst, MemPtr, MemSize) == AliasAnalysis::NoModRef)
Chris Lattner25a08142008-11-29 08:51:16 +0000194 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000195
196 // Otherwise, there is a dependence.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000197 return MemDepResult::getClobber(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000198 }
199
Chris Lattner7ebcf032008-12-07 02:15:47 +0000200 // No dependence found. If this is the entry block of the function, it is a
201 // clobber, otherwise it is non-local.
202 if (BB != &BB->getParent()->getEntryBlock())
203 return MemDepResult::getNonLocal();
204 return MemDepResult::getClobber(ScanIt);
Owen Anderson78e02f72007-07-06 23:14:35 +0000205}
206
Chris Lattner5391a1d2008-11-29 03:47:00 +0000207/// getDependency - Return the instruction on which a memory operation
208/// depends.
209MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
210 Instruction *ScanPos = QueryInst;
211
212 // Check for a cached result
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000213 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattner5391a1d2008-11-29 03:47:00 +0000214
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000215 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000216 // on MemDepResult's default constructing to 'dirty'.
217 if (!LocalCache.isDirty())
218 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000219
220 // Otherwise, if we have a dirty entry, we know we can start the scan at that
221 // instruction, which may save us some work.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000222 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000223 ScanPos = Inst;
Chris Lattner4a69bad2008-11-30 02:52:26 +0000224
Chris Lattnerd44745d2008-12-07 18:39:13 +0000225 RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);
Chris Lattner4a69bad2008-11-30 02:52:26 +0000226 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000227
Chris Lattnere79be942008-12-07 01:50:16 +0000228 BasicBlock *QueryParent = QueryInst->getParent();
229
230 Value *MemPtr = 0;
231 uint64_t MemSize = 0;
232
Chris Lattner5391a1d2008-11-29 03:47:00 +0000233 // Do the scan.
Chris Lattnere79be942008-12-07 01:50:16 +0000234 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000235 // No dependence found. If this is the entry block of the function, it is a
236 // clobber, otherwise it is non-local.
237 if (QueryParent != &QueryParent->getParent()->getEntryBlock())
238 LocalCache = MemDepResult::getNonLocal();
239 else
240 LocalCache = MemDepResult::getClobber(QueryInst);
Chris Lattnere79be942008-12-07 01:50:16 +0000241 } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) {
242 // If this is a volatile store, don't mess around with it. Just return the
243 // previous instruction as a clobber.
244 if (SI->isVolatile())
245 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
246 else {
247 MemPtr = SI->getPointerOperand();
248 MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
249 }
250 } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) {
251 // If this is a volatile load, don't mess around with it. Just return the
252 // previous instruction as a clobber.
253 if (LI->isVolatile())
254 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
255 else {
256 MemPtr = LI->getPointerOperand();
257 MemSize = TD->getTypeStoreSize(LI->getType());
258 }
259 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000260 LocalCache = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos,
Chris Lattnere79be942008-12-07 01:50:16 +0000261 QueryParent);
262 } else if (FreeInst *FI = dyn_cast<FreeInst>(QueryInst)) {
263 MemPtr = FI->getPointerOperand();
264 // FreeInsts erase the entire structure, not just a field.
265 MemSize = ~0UL;
266 } else {
267 // Non-memory instruction.
268 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
269 }
270
271 // If we need to do a pointer scan, make it happen.
272 if (MemPtr)
273 LocalCache = getPointerDependencyFrom(MemPtr, MemSize,
274 isa<LoadInst>(QueryInst),
275 ScanPos, QueryParent);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000276
277 // Remember the result!
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000278 if (Instruction *I = LocalCache.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000279 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000280
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000281 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000282}
283
Chris Lattner37d041c2008-11-30 01:18:27 +0000284/// getNonLocalDependency - Perform a full dependency query for the
285/// specified instruction, returning the set of blocks that the value is
286/// potentially live across. The returned set of results will include a
287/// "NonLocal" result for all blocks where the value is live across.
288///
289/// This method assumes the instruction returns a "nonlocal" dependency
290/// within its own block.
291///
Chris Lattnerbf145d62008-12-01 01:15:42 +0000292const MemoryDependenceAnalysis::NonLocalDepInfo &
293MemoryDependenceAnalysis::getNonLocalDependency(Instruction *QueryInst) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000294 // FIXME: Make this only be for callsites in the future.
Chris Lattnere79be942008-12-07 01:50:16 +0000295 assert(isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst) ||
296 isa<LoadInst>(QueryInst) || isa<StoreInst>(QueryInst));
Chris Lattner37d041c2008-11-30 01:18:27 +0000297 assert(getDependency(QueryInst).isNonLocal() &&
298 "getNonLocalDependency should only be used on insts with non-local deps!");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000299 PerInstNLInfo &CacheP = NonLocalDeps[QueryInst];
Chris Lattnerbf145d62008-12-01 01:15:42 +0000300 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner37d041c2008-11-30 01:18:27 +0000301
302 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
303 /// the cached case, this can happen due to instructions being deleted etc. In
304 /// the uncached case, this starts out as the set of predecessors we care
305 /// about.
306 SmallVector<BasicBlock*, 32> DirtyBlocks;
307
308 if (!Cache.empty()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000309 // Okay, we have a cache entry. If we know it is not dirty, just return it
310 // with no computation.
311 if (!CacheP.second) {
312 NumCacheNonLocal++;
313 return Cache;
314 }
315
Chris Lattner37d041c2008-11-30 01:18:27 +0000316 // If we already have a partially computed set of results, scan them to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000317 // determine what is dirty, seeding our initial DirtyBlocks worklist.
318 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
319 I != E; ++I)
320 if (I->second.isDirty())
321 DirtyBlocks.push_back(I->first);
Chris Lattner37d041c2008-11-30 01:18:27 +0000322
Chris Lattnerbf145d62008-12-01 01:15:42 +0000323 // Sort the cache so that we can do fast binary search lookups below.
324 std::sort(Cache.begin(), Cache.end());
Chris Lattner37d041c2008-11-30 01:18:27 +0000325
Chris Lattnerbf145d62008-12-01 01:15:42 +0000326 ++NumCacheDirtyNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000327 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
328 // << Cache.size() << " cached: " << *QueryInst;
329 } else {
330 // Seed DirtyBlocks with each of the preds of QueryInst's block.
331 BasicBlock *QueryBB = QueryInst->getParent();
332 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
333 NumUncacheNonLocal++;
334 }
335
Chris Lattnerbf145d62008-12-01 01:15:42 +0000336 // Visited checked first, vector in sorted order.
337 SmallPtrSet<BasicBlock*, 64> Visited;
338
339 unsigned NumSortedEntries = Cache.size();
340
Chris Lattner37d041c2008-11-30 01:18:27 +0000341 // Iterate while we still have blocks to update.
342 while (!DirtyBlocks.empty()) {
343 BasicBlock *DirtyBB = DirtyBlocks.back();
344 DirtyBlocks.pop_back();
345
Chris Lattnerbf145d62008-12-01 01:15:42 +0000346 // Already processed this block?
347 if (!Visited.insert(DirtyBB))
348 continue;
Chris Lattner37d041c2008-11-30 01:18:27 +0000349
Chris Lattnerbf145d62008-12-01 01:15:42 +0000350 // Do a binary search to see if we already have an entry for this block in
351 // the cache set. If so, find it.
352 NonLocalDepInfo::iterator Entry =
353 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
354 std::make_pair(DirtyBB, MemDepResult()));
355 if (Entry != Cache.begin() && (&*Entry)[-1].first == DirtyBB)
356 --Entry;
357
358 MemDepResult *ExistingResult = 0;
359 if (Entry != Cache.begin()+NumSortedEntries &&
360 Entry->first == DirtyBB) {
361 // If we already have an entry, and if it isn't already dirty, the block
362 // is done.
363 if (!Entry->second.isDirty())
364 continue;
365
366 // Otherwise, remember this slot so we can update the value.
367 ExistingResult = &Entry->second;
368 }
369
Chris Lattner37d041c2008-11-30 01:18:27 +0000370 // If the dirty entry has a pointer, start scanning from it so we don't have
371 // to rescan the entire block.
372 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerbf145d62008-12-01 01:15:42 +0000373 if (ExistingResult) {
374 if (Instruction *Inst = ExistingResult->getInst()) {
375 ScanPos = Inst;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000376 // We're removing QueryInst's use of Inst.
Chris Lattnerd44745d2008-12-07 18:39:13 +0000377 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, QueryInst);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000378 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000379 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000380
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000381 // Find out if this block has a local dependency for QueryInst.
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000382 MemDepResult Dep;
Chris Lattnere79be942008-12-07 01:50:16 +0000383
384 Value *MemPtr = 0;
385 uint64_t MemSize = 0;
386
Chris Lattner7ebcf032008-12-07 02:15:47 +0000387 if (ScanPos == DirtyBB->begin()) {
388 // No dependence found. If this is the entry block of the function, it is a
389 // clobber, otherwise it is non-local.
390 if (DirtyBB != &DirtyBB->getParent()->getEntryBlock())
391 Dep = MemDepResult::getNonLocal();
392 else
393 Dep = MemDepResult::getClobber(ScanPos);
Chris Lattnere79be942008-12-07 01:50:16 +0000394 } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) {
395 // If this is a volatile store, don't mess around with it. Just return the
396 // previous instruction as a clobber.
397 if (SI->isVolatile())
398 Dep = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
399 else {
400 MemPtr = SI->getPointerOperand();
401 MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
402 }
403 } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) {
404 // If this is a volatile load, don't mess around with it. Just return the
405 // previous instruction as a clobber.
406 if (LI->isVolatile())
407 Dep = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
408 else {
409 MemPtr = LI->getPointerOperand();
410 MemSize = TD->getTypeStoreSize(LI->getType());
411 }
412 } else {
413 assert(isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst));
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000414 Dep = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos,
415 DirtyBB);
Chris Lattnere79be942008-12-07 01:50:16 +0000416 }
417
418 if (MemPtr)
419 Dep = getPointerDependencyFrom(MemPtr, MemSize, isa<LoadInst>(QueryInst),
420 ScanPos, DirtyBB);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000421
422 // If we had a dirty entry for the block, update it. Otherwise, just add
423 // a new entry.
424 if (ExistingResult)
425 *ExistingResult = Dep;
426 else
427 Cache.push_back(std::make_pair(DirtyBB, Dep));
428
Chris Lattner37d041c2008-11-30 01:18:27 +0000429 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000430 // the value), remember the association!
431 if (!Dep.isNonLocal()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000432 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
433 // update this when we remove instructions.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000434 if (Instruction *Inst = Dep.getInst())
Chris Lattner37d041c2008-11-30 01:18:27 +0000435 ReverseNonLocalDeps[Inst].insert(QueryInst);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000436 } else {
Chris Lattner37d041c2008-11-30 01:18:27 +0000437
Chris Lattnerbf145d62008-12-01 01:15:42 +0000438 // If the block *is* completely transparent to the load, we need to check
439 // the predecessors of this block. Add them to our worklist.
440 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
441 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000442 }
443
Chris Lattnerbf145d62008-12-01 01:15:42 +0000444 return Cache;
Chris Lattner37d041c2008-11-30 01:18:27 +0000445}
446
Chris Lattner7ebcf032008-12-07 02:15:47 +0000447/// getNonLocalPointerDependency - Perform a full dependency query for an
448/// access to the specified (non-volatile) memory location, returning the
449/// set of instructions that either define or clobber the value.
450///
451/// This method assumes the pointer has a "NonLocal" dependency within its
452/// own block.
453///
454void MemoryDependenceAnalysis::
455getNonLocalPointerDependency(Value *Pointer, bool isLoad, BasicBlock *FromBB,
456 SmallVectorImpl<NonLocalDepEntry> &Result) {
Chris Lattner9a193fd2008-12-07 02:56:57 +0000457 Result.clear();
458
Chris Lattner7ebcf032008-12-07 02:15:47 +0000459 // We know that the pointer value is live into FromBB find the def/clobbers
460 // from presecessors.
Chris Lattner7ebcf032008-12-07 02:15:47 +0000461 const Type *EltTy = cast<PointerType>(Pointer->getType())->getElementType();
462 uint64_t PointeeSize = TD->getTypeStoreSize(EltTy);
463
464 // While we have blocks to analyze, get their values.
465 SmallPtrSet<BasicBlock*, 64> Visited;
Chris Lattner9a193fd2008-12-07 02:56:57 +0000466
467 for (pred_iterator PI = pred_begin(FromBB), E = pred_end(FromBB); PI != E;
468 ++PI) {
469 // TODO: PHI TRANSLATE.
470 getNonLocalPointerDepInternal(Pointer, PointeeSize, isLoad, *PI,
471 Result, Visited);
472 }
473}
474
475void MemoryDependenceAnalysis::
476getNonLocalPointerDepInternal(Value *Pointer, uint64_t PointeeSize,
477 bool isLoad, BasicBlock *StartBB,
478 SmallVectorImpl<NonLocalDepEntry> &Result,
479 SmallPtrSet<BasicBlock*, 64> &Visited) {
480 SmallVector<BasicBlock*, 32> Worklist;
481 Worklist.push_back(StartBB);
482
Chris Lattner6290f5c2008-12-07 08:50:20 +0000483 // Look up the cached info for Pointer.
484 ValueIsLoadPair CacheKey(Pointer, isLoad);
485 NonLocalDepInfo *Cache = &NonLocalPointerDeps[CacheKey];
486
487 // Keep track of the entries that we know are sorted. Previously cached
488 // entries will all be sorted. The entries we add we only sort on demand (we
489 // don't insert every element into its sorted position). We know that we
490 // won't get any reuse from currently inserted values, because we don't
491 // revisit blocks after we insert info for them.
492 unsigned NumSortedEntries = Cache->size();
493
Chris Lattner7ebcf032008-12-07 02:15:47 +0000494 while (!Worklist.empty()) {
Chris Lattner9a193fd2008-12-07 02:56:57 +0000495 BasicBlock *BB = Worklist.pop_back_val();
Chris Lattner7ebcf032008-12-07 02:15:47 +0000496
497 // Analyze the dependency of *Pointer in FromBB. See if we already have
498 // been here.
Chris Lattner9a193fd2008-12-07 02:56:57 +0000499 if (!Visited.insert(BB))
Chris Lattner7ebcf032008-12-07 02:15:47 +0000500 continue;
Chris Lattner6290f5c2008-12-07 08:50:20 +0000501
502 // Get the dependency info for Pointer in BB. If we have cached
503 // information, we will use it, otherwise we compute it.
Chris Lattner7ebcf032008-12-07 02:15:47 +0000504
Chris Lattner6290f5c2008-12-07 08:50:20 +0000505 // Do a binary search to see if we already have an entry for this block in
506 // the cache set. If so, find it.
507 NonLocalDepInfo::iterator Entry =
508 std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries,
509 std::make_pair(BB, MemDepResult()));
510 if (Entry != Cache->begin() && (&*Entry)[-1].first == BB)
511 --Entry;
Chris Lattner7ebcf032008-12-07 02:15:47 +0000512
Chris Lattner6290f5c2008-12-07 08:50:20 +0000513 MemDepResult *ExistingResult = 0;
514 if (Entry != Cache->begin()+NumSortedEntries && Entry->first == BB)
515 ExistingResult = &Entry->second;
516
517 // If we have a cached entry, and it is non-dirty, use it as the value for
518 // this dependency.
519 MemDepResult Dep;
520 if (ExistingResult && !ExistingResult->isDirty()) {
521 Dep = *ExistingResult;
522 ++NumCacheNonLocalPtr;
523 } else {
524 // Otherwise, we have to scan for the value. If we have a dirty cache
525 // entry, start scanning from its position, otherwise we scan from the end
526 // of the block.
527 BasicBlock::iterator ScanPos = BB->end();
528 if (ExistingResult && ExistingResult->getInst()) {
529 assert(ExistingResult->getInst()->getParent() == BB &&
530 "Instruction invalidated?");
531 ++NumCacheDirtyNonLocalPtr;
532 ScanPos = ExistingResult->getInst();
533
534 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Chris Lattnerd44745d2008-12-07 18:39:13 +0000535 RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos,
536 CacheKey.getOpaqueValue());
Chris Lattner6290f5c2008-12-07 08:50:20 +0000537 } else {
538 ++NumUncacheNonLocalPtr;
539 }
540
541 // Scan the block for the dependency.
542 Dep = getPointerDependencyFrom(Pointer, PointeeSize, isLoad, ScanPos, BB);
543
544 // If we had a dirty entry for the block, update it. Otherwise, just add
545 // a new entry.
546 if (ExistingResult)
547 *ExistingResult = Dep;
548 else
549 Cache->push_back(std::make_pair(BB, Dep));
550
551 // If the block has a dependency (i.e. it isn't completely transparent to
552 // the value), remember the reverse association because we just added it
553 // to Cache!
554 if (!Dep.isNonLocal()) {
555 // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently
556 // update MemDep when we remove instructions.
557 Instruction *Inst = Dep.getInst();
558 assert(Inst && "Didn't depend on anything?");
559 ReverseNonLocalPtrDeps[Inst].insert(CacheKey.getOpaqueValue());
560 }
561 }
Chris Lattner7ebcf032008-12-07 02:15:47 +0000562
563 // If we got a Def or Clobber, add this to the list of results.
564 if (!Dep.isNonLocal()) {
Chris Lattner9a193fd2008-12-07 02:56:57 +0000565 Result.push_back(NonLocalDepEntry(BB, Dep));
Chris Lattner7ebcf032008-12-07 02:15:47 +0000566 continue;
567 }
568
569 // Otherwise, we have to process all the predecessors of this block to scan
570 // them as well.
Chris Lattner9a193fd2008-12-07 02:56:57 +0000571 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000572 // TODO: PHI TRANSLATE.
Chris Lattner9a193fd2008-12-07 02:56:57 +0000573 Worklist.push_back(*PI);
574 }
Chris Lattner7ebcf032008-12-07 02:15:47 +0000575 }
Chris Lattner6290f5c2008-12-07 08:50:20 +0000576
577 // If we computed new values, re-sort Cache.
578 if (NumSortedEntries != Cache->size())
579 std::sort(Cache->begin(), Cache->end());
580}
581
582/// RemoveCachedNonLocalPointerDependencies - If P exists in
583/// CachedNonLocalPointerInfo, remove it.
584void MemoryDependenceAnalysis::
585RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) {
586 CachedNonLocalPointerInfo::iterator It =
587 NonLocalPointerDeps.find(P);
588 if (It == NonLocalPointerDeps.end()) return;
589
590 // Remove all of the entries in the BB->val map. This involves removing
591 // instructions from the reverse map.
592 NonLocalDepInfo &PInfo = It->second;
593
594 for (unsigned i = 0, e = PInfo.size(); i != e; ++i) {
595 Instruction *Target = PInfo[i].second.getInst();
596 if (Target == 0) continue; // Ignore non-local dep results.
597 assert(Target->getParent() == PInfo[i].first && Target != P.getPointer());
598
599 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Chris Lattnerd44745d2008-12-07 18:39:13 +0000600 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P.getOpaqueValue());
Chris Lattner6290f5c2008-12-07 08:50:20 +0000601 }
602
603 // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).
604 NonLocalPointerDeps.erase(It);
Chris Lattner7ebcf032008-12-07 02:15:47 +0000605}
606
607
Owen Anderson78e02f72007-07-06 23:14:35 +0000608/// removeInstruction - Remove an instruction from the dependence analysis,
609/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000610/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000611void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000612 // Walk through the Non-local dependencies, removing this one as the value
613 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +0000614 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
615 if (NLDI != NonLocalDeps.end()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000616 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattner25f4b2b2008-11-30 02:30:50 +0000617 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
618 DI != DE; ++DI)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000619 if (Instruction *Inst = DI->second.getInst())
Chris Lattnerd44745d2008-12-07 18:39:13 +0000620 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);
Chris Lattnerf68f3102008-11-30 02:28:25 +0000621 NonLocalDeps.erase(NLDI);
622 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000623
Chris Lattner5f589dc2008-11-28 22:04:47 +0000624 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000625 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000626 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
627 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000628 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerd44745d2008-12-07 18:39:13 +0000629 if (Instruction *Inst = LocalDepEntry->second.getInst())
630 RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);
Chris Lattner125ce362008-11-30 01:09:30 +0000631
Chris Lattnerbaad8882008-11-28 22:28:27 +0000632 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000633 LocalDeps.erase(LocalDepEntry);
Chris Lattner6290f5c2008-12-07 08:50:20 +0000634 }
635
636 // If we have any cached pointer dependencies on this instruction, remove
637 // them. If the instruction has non-pointer type, then it can't be a pointer
638 // base.
639
640 // Remove it from both the load info and the store info. The instruction
641 // can't be in either of these maps if it is non-pointer.
642 if (isa<PointerType>(RemInst->getType())) {
643 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));
644 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));
645 }
Chris Lattnerbaad8882008-11-28 22:28:27 +0000646
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000647 // Loop over all of the things that depend on the instruction we're removing.
648 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000649 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
Chris Lattner0655f732008-12-07 18:42:51 +0000650
651 // If we find RemInst as a clobber or Def in any of the maps for other values,
652 // we need to replace its entry with a dirty version of the instruction after
653 // it. If RemInst is a terminator, we use a null dirty value.
654 //
655 // Using a dirty version of the instruction after RemInst saves having to scan
656 // the entire block to get to this point.
657 MemDepResult NewDirtyVal;
658 if (!RemInst->isTerminator())
659 NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst));
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000660
Chris Lattner8c465272008-11-29 09:20:15 +0000661 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
662 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000663 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner6290f5c2008-12-07 08:50:20 +0000664 // RemInst can't be the terminator if it has local stuff depending on it.
Chris Lattner125ce362008-11-30 01:09:30 +0000665 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
666 "Nothing can locally depend on a terminator");
667
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000668 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
669 E = ReverseDeps.end(); I != E; ++I) {
670 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000671 assert(InstDependingOnRemInst != RemInst &&
672 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +0000673
Chris Lattner0655f732008-12-07 18:42:51 +0000674 LocalDeps[InstDependingOnRemInst] = NewDirtyVal;
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000675
Chris Lattner125ce362008-11-30 01:09:30 +0000676 // Make sure to remember that new things depend on NewDepInst.
Chris Lattner0655f732008-12-07 18:42:51 +0000677 assert(NewDirtyVal.getInst() && "There is no way something else can have "
678 "a local dep on this if it is a terminator!");
679 ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(),
Chris Lattner125ce362008-11-30 01:09:30 +0000680 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000681 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000682
683 ReverseLocalDeps.erase(ReverseDepIt);
684
685 // Add new reverse deps after scanning the set, to avoid invalidating the
686 // 'ReverseDeps' reference.
687 while (!ReverseDepsToAdd.empty()) {
688 ReverseLocalDeps[ReverseDepsToAdd.back().first]
689 .insert(ReverseDepsToAdd.back().second);
690 ReverseDepsToAdd.pop_back();
691 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000692 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000693
Chris Lattner8c465272008-11-29 09:20:15 +0000694 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
695 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner6290f5c2008-12-07 08:50:20 +0000696 SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second;
697 for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000698 I != E; ++I) {
699 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
700
Chris Lattner4a69bad2008-11-30 02:52:26 +0000701 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner4a69bad2008-11-30 02:52:26 +0000702 // The information is now dirty!
Chris Lattnerbf145d62008-12-01 01:15:42 +0000703 INLD.second = true;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000704
Chris Lattnerbf145d62008-12-01 01:15:42 +0000705 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
706 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000707 if (DI->second.getInst() != RemInst) continue;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000708
709 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner0655f732008-12-07 18:42:51 +0000710 DI->second = NewDirtyVal;
711
712 if (Instruction *NextI = NewDirtyVal.getInst())
Chris Lattnerf68f3102008-11-30 02:28:25 +0000713 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattnerf68f3102008-11-30 02:28:25 +0000714 }
715 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000716
717 ReverseNonLocalDeps.erase(ReverseDepIt);
718
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000719 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
720 while (!ReverseDepsToAdd.empty()) {
721 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
722 .insert(ReverseDepsToAdd.back().second);
723 ReverseDepsToAdd.pop_back();
724 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000725 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000726
Chris Lattner6290f5c2008-12-07 08:50:20 +0000727 // If the instruction is in ReverseNonLocalPtrDeps then it appears as a
728 // value in the NonLocalPointerDeps info.
729 ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =
730 ReverseNonLocalPtrDeps.find(RemInst);
731 if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {
732 SmallPtrSet<void*, 4> &Set = ReversePtrDepIt->second;
733 SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd;
734
735 for (SmallPtrSet<void*, 4>::iterator I = Set.begin(), E = Set.end();
736 I != E; ++I) {
737 ValueIsLoadPair P;
738 P.setFromOpaqueValue(*I);
739 assert(P.getPointer() != RemInst &&
740 "Already removed NonLocalPointerDeps info for RemInst");
741
742 NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P];
743
Chris Lattner6290f5c2008-12-07 08:50:20 +0000744 // Update any entries for RemInst to use the instruction after it.
745 for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end();
746 DI != DE; ++DI) {
747 if (DI->second.getInst() != RemInst) continue;
748
749 // Convert to a dirty entry for the subsequent instruction.
750 DI->second = NewDirtyVal;
751
752 if (Instruction *NewDirtyInst = NewDirtyVal.getInst())
753 ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));
754 }
755 }
756
757 ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);
758
759 while (!ReversePtrDepsToAdd.empty()) {
760 ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first]
761 .insert(ReversePtrDepsToAdd.back().second.getOpaqueValue());
762 ReversePtrDepsToAdd.pop_back();
763 }
764 }
765
766
Chris Lattnerf68f3102008-11-30 02:28:25 +0000767 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnerd777d402008-11-30 19:24:31 +0000768 AA->deleteValue(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000769 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000770}
Chris Lattner729b2372008-11-29 21:25:10 +0000771
772/// verifyRemoved - Verify that the specified instruction does not occur
773/// in our internal data structures.
774void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
775 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
776 E = LocalDeps.end(); I != E; ++I) {
777 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000778 assert(I->second.getInst() != D &&
Chris Lattner729b2372008-11-29 21:25:10 +0000779 "Inst occurs in data structures");
780 }
781
Chris Lattner6290f5c2008-12-07 08:50:20 +0000782 for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(),
783 E = NonLocalPointerDeps.end(); I != E; ++I) {
784 assert(I->first.getPointer() != D && "Inst occurs in NLPD map key");
785 const NonLocalDepInfo &Val = I->second;
786 for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end();
787 II != E; ++II)
788 assert(II->second.getInst() != D && "Inst occurs as NLPD value");
789 }
790
Chris Lattner729b2372008-11-29 21:25:10 +0000791 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
792 E = NonLocalDeps.end(); I != E; ++I) {
793 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000794 const PerInstNLInfo &INLD = I->second;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000795 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
796 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000797 assert(II->second.getInst() != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000798 }
799
800 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +0000801 E = ReverseLocalDeps.end(); I != E; ++I) {
802 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000803 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
804 EE = I->second.end(); II != EE; ++II)
805 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000806 }
Chris Lattner729b2372008-11-29 21:25:10 +0000807
808 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
809 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000810 I != E; ++I) {
811 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000812 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
813 EE = I->second.end(); II != EE; ++II)
814 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000815 }
Chris Lattner6290f5c2008-12-07 08:50:20 +0000816
817 for (ReverseNonLocalPtrDepTy::const_iterator
818 I = ReverseNonLocalPtrDeps.begin(),
819 E = ReverseNonLocalPtrDeps.end(); I != E; ++I) {
820 assert(I->first != D && "Inst occurs in rev NLPD map");
821
822 for (SmallPtrSet<void*, 4>::const_iterator II = I->second.begin(),
823 E = I->second.end(); II != E; ++II)
824 assert(*II != ValueIsLoadPair(D, false).getOpaqueValue() &&
825 *II != ValueIsLoadPair(D, true).getOpaqueValue() &&
826 "Inst occurs in ReverseNonLocalPtrDeps map");
827 }
828
Chris Lattner729b2372008-11-29 21:25:10 +0000829}