blob: 94395027f21d69e30505a6f767cfcf146b0a5e4b [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"
Chris Lattner4012fdd2008-12-09 06:28:49 +000024#include "llvm/Support/PredIteratorCache.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");
Chris Lattner11dcd8d2008-12-08 07:31:50 +000039STATISTIC(NumCacheCompleteNonLocalPtr,
40 "Number of block queries that were completely cached");
Chris Lattner6290f5c2008-12-07 08:50:20 +000041
Owen Anderson78e02f72007-07-06 23:14:35 +000042char MemoryDependenceAnalysis::ID = 0;
43
Owen Anderson78e02f72007-07-06 23:14:35 +000044// Register this pass...
Owen Anderson776ee1f2007-07-10 20:21:08 +000045static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Chris Lattner0e575f42008-11-28 21:45:17 +000046 "Memory Dependence Analysis", false, true);
Owen Anderson78e02f72007-07-06 23:14:35 +000047
Chris Lattner4012fdd2008-12-09 06:28:49 +000048MemoryDependenceAnalysis::MemoryDependenceAnalysis()
49: FunctionPass(&ID), PredCache(0) {
50}
51MemoryDependenceAnalysis::~MemoryDependenceAnalysis() {
52}
53
54/// Clean up memory in between runs
55void MemoryDependenceAnalysis::releaseMemory() {
56 LocalDeps.clear();
57 NonLocalDeps.clear();
58 NonLocalPointerDeps.clear();
59 ReverseLocalDeps.clear();
60 ReverseNonLocalDeps.clear();
61 ReverseNonLocalPtrDeps.clear();
62 PredCache->clear();
63}
64
65
66
Owen Anderson78e02f72007-07-06 23:14:35 +000067/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
68///
69void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
70 AU.setPreservesAll();
71 AU.addRequiredTransitive<AliasAnalysis>();
72 AU.addRequiredTransitive<TargetData>();
73}
74
Chris Lattnerd777d402008-11-30 19:24:31 +000075bool MemoryDependenceAnalysis::runOnFunction(Function &) {
76 AA = &getAnalysis<AliasAnalysis>();
77 TD = &getAnalysis<TargetData>();
Chris Lattner4012fdd2008-12-09 06:28:49 +000078 if (PredCache == 0)
79 PredCache.reset(new PredIteratorCache());
Chris Lattnerd777d402008-11-30 19:24:31 +000080 return false;
81}
82
Chris Lattnerd44745d2008-12-07 18:39:13 +000083/// RemoveFromReverseMap - This is a helper function that removes Val from
84/// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry.
85template <typename KeyTy>
86static void RemoveFromReverseMap(DenseMap<Instruction*,
87 SmallPtrSet<KeyTy*, 4> > &ReverseMap,
88 Instruction *Inst, KeyTy *Val) {
89 typename DenseMap<Instruction*, SmallPtrSet<KeyTy*, 4> >::iterator
90 InstIt = ReverseMap.find(Inst);
91 assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");
92 bool Found = InstIt->second.erase(Val);
93 assert(Found && "Invalid reverse map!"); Found=Found;
94 if (InstIt->second.empty())
95 ReverseMap.erase(InstIt);
96}
97
Chris Lattnerbf145d62008-12-01 01:15:42 +000098
Chris Lattner8ef57c52008-12-07 00:35:51 +000099/// getCallSiteDependencyFrom - Private helper for finding the local
100/// dependencies of a call site.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000101MemDepResult MemoryDependenceAnalysis::
Chris Lattner8ef57c52008-12-07 00:35:51 +0000102getCallSiteDependencyFrom(CallSite CS, BasicBlock::iterator ScanIt,
103 BasicBlock *BB) {
Owen Anderson642a9e32007-08-08 22:26:03 +0000104 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000105 while (ScanIt != BB->begin()) {
106 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +0000107
108 // If this inst is a memory op, get the pointer it accessed
Chris Lattner00314b32008-11-29 09:15:21 +0000109 Value *Pointer = 0;
110 uint64_t PointerSize = 0;
111 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
112 Pointer = S->getPointerOperand();
Chris Lattnerd777d402008-11-30 19:24:31 +0000113 PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner00314b32008-11-29 09:15:21 +0000114 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
115 Pointer = V->getOperand(0);
Chris Lattnerd777d402008-11-30 19:24:31 +0000116 PointerSize = TD->getTypeStoreSize(V->getType());
Chris Lattner00314b32008-11-29 09:15:21 +0000117 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
118 Pointer = F->getPointerOperand();
Owen Anderson5f323202007-07-10 17:59:22 +0000119
120 // FreeInsts erase the entire structure
Chris Lattner6290f5c2008-12-07 08:50:20 +0000121 PointerSize = ~0ULL;
Chris Lattner00314b32008-11-29 09:15:21 +0000122 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000123 CallSite InstCS = CallSite::get(Inst);
124 // If these two calls do not interfere, look past it.
125 if (AA->getModRefInfo(CS, InstCS) == AliasAnalysis::NoModRef)
Chris Lattner00314b32008-11-29 09:15:21 +0000126 continue;
Chris Lattnerb51deb92008-12-05 21:04:20 +0000127
128 // FIXME: If this is a ref/ref result, we should ignore it!
129 // X = strlen(P);
130 // Y = strlen(Q);
131 // Z = strlen(P); // Z = X
132
133 // If they interfere, we generally return clobber. However, if they are
134 // calls to the same read-only functions we return Def.
135 if (!AA->onlyReadsMemory(CS) || CS.getCalledFunction() == 0 ||
136 CS.getCalledFunction() != InstCS.getCalledFunction())
137 return MemDepResult::getClobber(Inst);
138 return MemDepResult::getDef(Inst);
Chris Lattnercfbb6342008-11-30 01:44:00 +0000139 } else {
140 // Non-memory instruction.
Owen Anderson202da142007-07-10 20:39:07 +0000141 continue;
Chris Lattnercfbb6342008-11-30 01:44:00 +0000142 }
Owen Anderson5f323202007-07-10 17:59:22 +0000143
Chris Lattnerb51deb92008-12-05 21:04:20 +0000144 if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef)
145 return MemDepResult::getClobber(Inst);
Owen Anderson5f323202007-07-10 17:59:22 +0000146 }
147
Chris Lattner7ebcf032008-12-07 02:15:47 +0000148 // No dependence found. If this is the entry block of the function, it is a
149 // clobber, otherwise it is non-local.
150 if (BB != &BB->getParent()->getEntryBlock())
151 return MemDepResult::getNonLocal();
152 return MemDepResult::getClobber(ScanIt);
Owen Anderson5f323202007-07-10 17:59:22 +0000153}
154
Chris Lattnere79be942008-12-07 01:50:16 +0000155/// getPointerDependencyFrom - Return the instruction on which a memory
156/// location depends. If isLoad is true, this routine ignore may-aliases with
157/// read-only operations.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000158MemDepResult MemoryDependenceAnalysis::
Chris Lattnere79be942008-12-07 01:50:16 +0000159getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad,
160 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000161
Chris Lattner6290f5c2008-12-07 08:50:20 +0000162 // Walk backwards through the basic block, looking for dependencies.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000163 while (ScanIt != BB->begin()) {
164 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000165
Chris Lattnercfbb6342008-11-30 01:44:00 +0000166 // Values depend on loads if the pointers are must aliased. This means that
167 // a load depends on another must aliased load from the same value.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000168 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000169 Value *Pointer = LI->getPointerOperand();
170 uint64_t PointerSize = TD->getTypeStoreSize(LI->getType());
171
172 // If we found a pointer, check if it could be the same as our pointer.
Chris Lattnera161ab02008-11-29 09:09:48 +0000173 AliasAnalysis::AliasResult R =
Chris Lattnerd777d402008-11-30 19:24:31 +0000174 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
Chris Lattnera161ab02008-11-29 09:09:48 +0000175 if (R == AliasAnalysis::NoAlias)
176 continue;
177
178 // May-alias loads don't depend on each other without a dependence.
Chris Lattnere79be942008-12-07 01:50:16 +0000179 if (isLoad && R == AliasAnalysis::MayAlias)
Chris Lattnera161ab02008-11-29 09:09:48 +0000180 continue;
Chris Lattner6290f5c2008-12-07 08:50:20 +0000181 // Stores depend on may and must aliased loads, loads depend on must-alias
182 // loads.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000183 return MemDepResult::getDef(Inst);
184 }
185
186 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000187 Value *Pointer = SI->getPointerOperand();
188 uint64_t PointerSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
189
190 // If we found a pointer, check if it could be the same as our pointer.
191 AliasAnalysis::AliasResult R =
192 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
193
194 if (R == AliasAnalysis::NoAlias)
195 continue;
196 if (R == AliasAnalysis::MayAlias)
197 return MemDepResult::getClobber(Inst);
198 return MemDepResult::getDef(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000199 }
Chris Lattner237a8282008-11-30 01:39:32 +0000200
201 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattnerb51deb92008-12-05 21:04:20 +0000202 // the allocation, return Def. This means that there is no dependence and
Chris Lattner237a8282008-11-30 01:39:32 +0000203 // the access can be optimized based on that. For example, a load could
204 // turn into undef.
Chris Lattnera161ab02008-11-29 09:09:48 +0000205 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
Chris Lattner237a8282008-11-30 01:39:32 +0000206 Value *AccessPtr = MemPtr->getUnderlyingObject();
Owen Anderson78e02f72007-07-06 23:14:35 +0000207
Chris Lattner237a8282008-11-30 01:39:32 +0000208 if (AccessPtr == AI ||
Chris Lattnerd777d402008-11-30 19:24:31 +0000209 AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
Chris Lattnerb51deb92008-12-05 21:04:20 +0000210 return MemDepResult::getDef(AI);
Chris Lattner237a8282008-11-30 01:39:32 +0000211 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000212 }
Chris Lattnera161ab02008-11-29 09:09:48 +0000213
Chris Lattnerb51deb92008-12-05 21:04:20 +0000214 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
Chris Lattnere79be942008-12-07 01:50:16 +0000215 // FIXME: If this is a load, we should ignore readonly calls!
Chris Lattnerb51deb92008-12-05 21:04:20 +0000216 if (AA->getModRefInfo(Inst, MemPtr, MemSize) == AliasAnalysis::NoModRef)
Chris Lattner25a08142008-11-29 08:51:16 +0000217 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000218
219 // Otherwise, there is a dependence.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000220 return MemDepResult::getClobber(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000221 }
222
Chris Lattner7ebcf032008-12-07 02:15:47 +0000223 // No dependence found. If this is the entry block of the function, it is a
224 // clobber, otherwise it is non-local.
225 if (BB != &BB->getParent()->getEntryBlock())
226 return MemDepResult::getNonLocal();
227 return MemDepResult::getClobber(ScanIt);
Owen Anderson78e02f72007-07-06 23:14:35 +0000228}
229
Chris Lattner5391a1d2008-11-29 03:47:00 +0000230/// getDependency - Return the instruction on which a memory operation
231/// depends.
232MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
233 Instruction *ScanPos = QueryInst;
234
235 // Check for a cached result
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000236 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattner5391a1d2008-11-29 03:47:00 +0000237
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000238 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000239 // on MemDepResult's default constructing to 'dirty'.
240 if (!LocalCache.isDirty())
241 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000242
243 // Otherwise, if we have a dirty entry, we know we can start the scan at that
244 // instruction, which may save us some work.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000245 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000246 ScanPos = Inst;
Chris Lattner4a69bad2008-11-30 02:52:26 +0000247
Chris Lattnerd44745d2008-12-07 18:39:13 +0000248 RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);
Chris Lattner4a69bad2008-11-30 02:52:26 +0000249 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000250
Chris Lattnere79be942008-12-07 01:50:16 +0000251 BasicBlock *QueryParent = QueryInst->getParent();
252
253 Value *MemPtr = 0;
254 uint64_t MemSize = 0;
255
Chris Lattner5391a1d2008-11-29 03:47:00 +0000256 // Do the scan.
Chris Lattnere79be942008-12-07 01:50:16 +0000257 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000258 // No dependence found. If this is the entry block of the function, it is a
259 // clobber, otherwise it is non-local.
260 if (QueryParent != &QueryParent->getParent()->getEntryBlock())
261 LocalCache = MemDepResult::getNonLocal();
262 else
263 LocalCache = MemDepResult::getClobber(QueryInst);
Chris Lattnere79be942008-12-07 01:50:16 +0000264 } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) {
265 // If this is a volatile store, don't mess around with it. Just return the
266 // previous instruction as a clobber.
267 if (SI->isVolatile())
268 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
269 else {
270 MemPtr = SI->getPointerOperand();
271 MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
272 }
273 } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) {
274 // If this is a volatile load, don't mess around with it. Just return the
275 // previous instruction as a clobber.
276 if (LI->isVolatile())
277 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
278 else {
279 MemPtr = LI->getPointerOperand();
280 MemSize = TD->getTypeStoreSize(LI->getType());
281 }
282 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000283 LocalCache = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos,
Chris Lattnere79be942008-12-07 01:50:16 +0000284 QueryParent);
285 } else if (FreeInst *FI = dyn_cast<FreeInst>(QueryInst)) {
286 MemPtr = FI->getPointerOperand();
287 // FreeInsts erase the entire structure, not just a field.
288 MemSize = ~0UL;
289 } else {
290 // Non-memory instruction.
291 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
292 }
293
294 // If we need to do a pointer scan, make it happen.
295 if (MemPtr)
296 LocalCache = getPointerDependencyFrom(MemPtr, MemSize,
297 isa<LoadInst>(QueryInst),
298 ScanPos, QueryParent);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000299
300 // Remember the result!
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000301 if (Instruction *I = LocalCache.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000302 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000303
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000304 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000305}
306
Chris Lattner37d041c2008-11-30 01:18:27 +0000307/// getNonLocalDependency - Perform a full dependency query for the
308/// specified instruction, returning the set of blocks that the value is
309/// potentially live across. The returned set of results will include a
310/// "NonLocal" result for all blocks where the value is live across.
311///
312/// This method assumes the instruction returns a "nonlocal" dependency
313/// within its own block.
314///
Chris Lattnerbf145d62008-12-01 01:15:42 +0000315const MemoryDependenceAnalysis::NonLocalDepInfo &
316MemoryDependenceAnalysis::getNonLocalDependency(Instruction *QueryInst) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000317 // FIXME: Make this only be for callsites in the future.
Chris Lattnere79be942008-12-07 01:50:16 +0000318 assert(isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst) ||
319 isa<LoadInst>(QueryInst) || isa<StoreInst>(QueryInst));
Chris Lattner37d041c2008-11-30 01:18:27 +0000320 assert(getDependency(QueryInst).isNonLocal() &&
321 "getNonLocalDependency should only be used on insts with non-local deps!");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000322 PerInstNLInfo &CacheP = NonLocalDeps[QueryInst];
Chris Lattnerbf145d62008-12-01 01:15:42 +0000323 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner37d041c2008-11-30 01:18:27 +0000324
325 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
326 /// the cached case, this can happen due to instructions being deleted etc. In
327 /// the uncached case, this starts out as the set of predecessors we care
328 /// about.
329 SmallVector<BasicBlock*, 32> DirtyBlocks;
330
331 if (!Cache.empty()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000332 // Okay, we have a cache entry. If we know it is not dirty, just return it
333 // with no computation.
334 if (!CacheP.second) {
335 NumCacheNonLocal++;
336 return Cache;
337 }
338
Chris Lattner37d041c2008-11-30 01:18:27 +0000339 // If we already have a partially computed set of results, scan them to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000340 // determine what is dirty, seeding our initial DirtyBlocks worklist.
341 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
342 I != E; ++I)
343 if (I->second.isDirty())
344 DirtyBlocks.push_back(I->first);
Chris Lattner37d041c2008-11-30 01:18:27 +0000345
Chris Lattnerbf145d62008-12-01 01:15:42 +0000346 // Sort the cache so that we can do fast binary search lookups below.
347 std::sort(Cache.begin(), Cache.end());
Chris Lattner37d041c2008-11-30 01:18:27 +0000348
Chris Lattnerbf145d62008-12-01 01:15:42 +0000349 ++NumCacheDirtyNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000350 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
351 // << Cache.size() << " cached: " << *QueryInst;
352 } else {
353 // Seed DirtyBlocks with each of the preds of QueryInst's block.
354 BasicBlock *QueryBB = QueryInst->getParent();
355 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
356 NumUncacheNonLocal++;
357 }
358
Chris Lattnerbf145d62008-12-01 01:15:42 +0000359 // Visited checked first, vector in sorted order.
360 SmallPtrSet<BasicBlock*, 64> Visited;
361
362 unsigned NumSortedEntries = Cache.size();
363
Chris Lattner37d041c2008-11-30 01:18:27 +0000364 // Iterate while we still have blocks to update.
365 while (!DirtyBlocks.empty()) {
366 BasicBlock *DirtyBB = DirtyBlocks.back();
367 DirtyBlocks.pop_back();
368
Chris Lattnerbf145d62008-12-01 01:15:42 +0000369 // Already processed this block?
370 if (!Visited.insert(DirtyBB))
371 continue;
Chris Lattner37d041c2008-11-30 01:18:27 +0000372
Chris Lattnerbf145d62008-12-01 01:15:42 +0000373 // Do a binary search to see if we already have an entry for this block in
374 // the cache set. If so, find it.
375 NonLocalDepInfo::iterator Entry =
376 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
377 std::make_pair(DirtyBB, MemDepResult()));
378 if (Entry != Cache.begin() && (&*Entry)[-1].first == DirtyBB)
379 --Entry;
380
381 MemDepResult *ExistingResult = 0;
382 if (Entry != Cache.begin()+NumSortedEntries &&
383 Entry->first == DirtyBB) {
384 // If we already have an entry, and if it isn't already dirty, the block
385 // is done.
386 if (!Entry->second.isDirty())
387 continue;
388
389 // Otherwise, remember this slot so we can update the value.
390 ExistingResult = &Entry->second;
391 }
392
Chris Lattner37d041c2008-11-30 01:18:27 +0000393 // If the dirty entry has a pointer, start scanning from it so we don't have
394 // to rescan the entire block.
395 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerbf145d62008-12-01 01:15:42 +0000396 if (ExistingResult) {
397 if (Instruction *Inst = ExistingResult->getInst()) {
398 ScanPos = Inst;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000399 // We're removing QueryInst's use of Inst.
Chris Lattnerd44745d2008-12-07 18:39:13 +0000400 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, QueryInst);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000401 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000402 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000403
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000404 // Find out if this block has a local dependency for QueryInst.
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000405 MemDepResult Dep;
Chris Lattnere79be942008-12-07 01:50:16 +0000406
407 Value *MemPtr = 0;
408 uint64_t MemSize = 0;
409
Chris Lattner7ebcf032008-12-07 02:15:47 +0000410 if (ScanPos == DirtyBB->begin()) {
411 // No dependence found. If this is the entry block of the function, it is a
412 // clobber, otherwise it is non-local.
413 if (DirtyBB != &DirtyBB->getParent()->getEntryBlock())
414 Dep = MemDepResult::getNonLocal();
415 else
416 Dep = MemDepResult::getClobber(ScanPos);
Chris Lattnere79be942008-12-07 01:50:16 +0000417 } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) {
418 // If this is a volatile store, don't mess around with it. Just return the
419 // previous instruction as a clobber.
420 if (SI->isVolatile())
421 Dep = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
422 else {
423 MemPtr = SI->getPointerOperand();
424 MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
425 }
426 } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) {
427 // If this is a volatile load, don't mess around with it. Just return the
428 // previous instruction as a clobber.
429 if (LI->isVolatile())
430 Dep = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
431 else {
432 MemPtr = LI->getPointerOperand();
433 MemSize = TD->getTypeStoreSize(LI->getType());
434 }
435 } else {
436 assert(isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst));
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000437 Dep = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos,
438 DirtyBB);
Chris Lattnere79be942008-12-07 01:50:16 +0000439 }
440
441 if (MemPtr)
442 Dep = getPointerDependencyFrom(MemPtr, MemSize, isa<LoadInst>(QueryInst),
443 ScanPos, DirtyBB);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000444
445 // If we had a dirty entry for the block, update it. Otherwise, just add
446 // a new entry.
447 if (ExistingResult)
448 *ExistingResult = Dep;
449 else
450 Cache.push_back(std::make_pair(DirtyBB, Dep));
451
Chris Lattner37d041c2008-11-30 01:18:27 +0000452 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000453 // the value), remember the association!
454 if (!Dep.isNonLocal()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000455 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
456 // update this when we remove instructions.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000457 if (Instruction *Inst = Dep.getInst())
Chris Lattner37d041c2008-11-30 01:18:27 +0000458 ReverseNonLocalDeps[Inst].insert(QueryInst);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000459 } else {
Chris Lattner37d041c2008-11-30 01:18:27 +0000460
Chris Lattnerbf145d62008-12-01 01:15:42 +0000461 // If the block *is* completely transparent to the load, we need to check
462 // the predecessors of this block. Add them to our worklist.
463 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
464 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000465 }
466
Chris Lattnerbf145d62008-12-01 01:15:42 +0000467 return Cache;
Chris Lattner37d041c2008-11-30 01:18:27 +0000468}
469
Chris Lattner7ebcf032008-12-07 02:15:47 +0000470/// getNonLocalPointerDependency - Perform a full dependency query for an
471/// access to the specified (non-volatile) memory location, returning the
472/// set of instructions that either define or clobber the value.
473///
474/// This method assumes the pointer has a "NonLocal" dependency within its
475/// own block.
476///
477void MemoryDependenceAnalysis::
478getNonLocalPointerDependency(Value *Pointer, bool isLoad, BasicBlock *FromBB,
479 SmallVectorImpl<NonLocalDepEntry> &Result) {
Chris Lattner3f7eb5b2008-12-07 18:45:15 +0000480 assert(isa<PointerType>(Pointer->getType()) &&
481 "Can't get pointer deps of a non-pointer!");
Chris Lattner9a193fd2008-12-07 02:56:57 +0000482 Result.clear();
483
Chris Lattner7ebcf032008-12-07 02:15:47 +0000484 // We know that the pointer value is live into FromBB find the def/clobbers
485 // from presecessors.
Chris Lattner7ebcf032008-12-07 02:15:47 +0000486 const Type *EltTy = cast<PointerType>(Pointer->getType())->getElementType();
487 uint64_t PointeeSize = TD->getTypeStoreSize(EltTy);
488
489 // While we have blocks to analyze, get their values.
490 SmallPtrSet<BasicBlock*, 64> Visited;
Chris Lattner9a193fd2008-12-07 02:56:57 +0000491
Chris Lattner4012fdd2008-12-09 06:28:49 +0000492 for (BasicBlock **PI = PredCache->GetPreds(FromBB); *PI; ++PI) {
Chris Lattner9a193fd2008-12-07 02:56:57 +0000493 // TODO: PHI TRANSLATE.
494 getNonLocalPointerDepInternal(Pointer, PointeeSize, isLoad, *PI,
495 Result, Visited);
496 }
497}
498
499void MemoryDependenceAnalysis::
500getNonLocalPointerDepInternal(Value *Pointer, uint64_t PointeeSize,
501 bool isLoad, BasicBlock *StartBB,
502 SmallVectorImpl<NonLocalDepEntry> &Result,
503 SmallPtrSet<BasicBlock*, 64> &Visited) {
Chris Lattner6290f5c2008-12-07 08:50:20 +0000504 // Look up the cached info for Pointer.
505 ValueIsLoadPair CacheKey(Pointer, isLoad);
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000506
507 std::pair<BasicBlock*, NonLocalDepInfo> &CacheInfo =
508 NonLocalPointerDeps[CacheKey];
509 NonLocalDepInfo *Cache = &CacheInfo.second;
510
511 // If we have valid cached information for exactly the block we are
512 // investigating, just return it with no recomputation.
513 if (CacheInfo.first == StartBB) {
514 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
515 I != E; ++I)
516 if (!I->second.isNonLocal())
517 Result.push_back(*I);
518 ++NumCacheCompleteNonLocalPtr;
519 return;
520 }
521
522 // Otherwise, either this is a new block, a block with an invalid cache
523 // pointer or one that we're about to invalidate by putting more info into it
524 // than its valid cache info. If empty, the result will be valid cache info,
525 // otherwise it isn't.
526 CacheInfo.first = Cache->empty() ? StartBB : 0;
527
528 SmallVector<BasicBlock*, 32> Worklist;
529 Worklist.push_back(StartBB);
Chris Lattner6290f5c2008-12-07 08:50:20 +0000530
531 // Keep track of the entries that we know are sorted. Previously cached
532 // entries will all be sorted. The entries we add we only sort on demand (we
533 // don't insert every element into its sorted position). We know that we
534 // won't get any reuse from currently inserted values, because we don't
535 // revisit blocks after we insert info for them.
536 unsigned NumSortedEntries = Cache->size();
537
Chris Lattner7ebcf032008-12-07 02:15:47 +0000538 while (!Worklist.empty()) {
Chris Lattner9a193fd2008-12-07 02:56:57 +0000539 BasicBlock *BB = Worklist.pop_back_val();
Chris Lattner7ebcf032008-12-07 02:15:47 +0000540
541 // Analyze the dependency of *Pointer in FromBB. See if we already have
542 // been here.
Chris Lattner9a193fd2008-12-07 02:56:57 +0000543 if (!Visited.insert(BB))
Chris Lattner7ebcf032008-12-07 02:15:47 +0000544 continue;
Chris Lattner6290f5c2008-12-07 08:50:20 +0000545
546 // Get the dependency info for Pointer in BB. If we have cached
547 // information, we will use it, otherwise we compute it.
Chris Lattner7ebcf032008-12-07 02:15:47 +0000548
Chris Lattner6290f5c2008-12-07 08:50:20 +0000549 // Do a binary search to see if we already have an entry for this block in
550 // the cache set. If so, find it.
551 NonLocalDepInfo::iterator Entry =
552 std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries,
553 std::make_pair(BB, MemDepResult()));
554 if (Entry != Cache->begin() && (&*Entry)[-1].first == BB)
555 --Entry;
Chris Lattner7ebcf032008-12-07 02:15:47 +0000556
Chris Lattner6290f5c2008-12-07 08:50:20 +0000557 MemDepResult *ExistingResult = 0;
558 if (Entry != Cache->begin()+NumSortedEntries && Entry->first == BB)
559 ExistingResult = &Entry->second;
560
561 // If we have a cached entry, and it is non-dirty, use it as the value for
562 // this dependency.
563 MemDepResult Dep;
564 if (ExistingResult && !ExistingResult->isDirty()) {
565 Dep = *ExistingResult;
566 ++NumCacheNonLocalPtr;
567 } else {
568 // Otherwise, we have to scan for the value. If we have a dirty cache
569 // entry, start scanning from its position, otherwise we scan from the end
570 // of the block.
571 BasicBlock::iterator ScanPos = BB->end();
572 if (ExistingResult && ExistingResult->getInst()) {
573 assert(ExistingResult->getInst()->getParent() == BB &&
574 "Instruction invalidated?");
575 ++NumCacheDirtyNonLocalPtr;
576 ScanPos = ExistingResult->getInst();
577
578 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Chris Lattnerd44745d2008-12-07 18:39:13 +0000579 RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos,
580 CacheKey.getOpaqueValue());
Chris Lattner6290f5c2008-12-07 08:50:20 +0000581 } else {
582 ++NumUncacheNonLocalPtr;
583 }
584
585 // Scan the block for the dependency.
586 Dep = getPointerDependencyFrom(Pointer, PointeeSize, isLoad, ScanPos, BB);
587
588 // If we had a dirty entry for the block, update it. Otherwise, just add
589 // a new entry.
590 if (ExistingResult)
591 *ExistingResult = Dep;
592 else
593 Cache->push_back(std::make_pair(BB, Dep));
594
595 // If the block has a dependency (i.e. it isn't completely transparent to
596 // the value), remember the reverse association because we just added it
597 // to Cache!
598 if (!Dep.isNonLocal()) {
599 // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently
600 // update MemDep when we remove instructions.
601 Instruction *Inst = Dep.getInst();
602 assert(Inst && "Didn't depend on anything?");
603 ReverseNonLocalPtrDeps[Inst].insert(CacheKey.getOpaqueValue());
604 }
605 }
Chris Lattner7ebcf032008-12-07 02:15:47 +0000606
607 // If we got a Def or Clobber, add this to the list of results.
608 if (!Dep.isNonLocal()) {
Chris Lattner9a193fd2008-12-07 02:56:57 +0000609 Result.push_back(NonLocalDepEntry(BB, Dep));
Chris Lattner7ebcf032008-12-07 02:15:47 +0000610 continue;
611 }
612
613 // Otherwise, we have to process all the predecessors of this block to scan
614 // them as well.
Chris Lattner4012fdd2008-12-09 06:28:49 +0000615 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000616 // TODO: PHI TRANSLATE.
Chris Lattner9a193fd2008-12-07 02:56:57 +0000617 Worklist.push_back(*PI);
618 }
Chris Lattner7ebcf032008-12-07 02:15:47 +0000619 }
Chris Lattner6290f5c2008-12-07 08:50:20 +0000620
621 // If we computed new values, re-sort Cache.
622 if (NumSortedEntries != Cache->size())
623 std::sort(Cache->begin(), Cache->end());
624}
625
626/// RemoveCachedNonLocalPointerDependencies - If P exists in
627/// CachedNonLocalPointerInfo, remove it.
628void MemoryDependenceAnalysis::
629RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) {
630 CachedNonLocalPointerInfo::iterator It =
631 NonLocalPointerDeps.find(P);
632 if (It == NonLocalPointerDeps.end()) return;
633
634 // Remove all of the entries in the BB->val map. This involves removing
635 // instructions from the reverse map.
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000636 NonLocalDepInfo &PInfo = It->second.second;
Chris Lattner6290f5c2008-12-07 08:50:20 +0000637
638 for (unsigned i = 0, e = PInfo.size(); i != e; ++i) {
639 Instruction *Target = PInfo[i].second.getInst();
640 if (Target == 0) continue; // Ignore non-local dep results.
641 assert(Target->getParent() == PInfo[i].first && Target != P.getPointer());
642
643 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Chris Lattnerd44745d2008-12-07 18:39:13 +0000644 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P.getOpaqueValue());
Chris Lattner6290f5c2008-12-07 08:50:20 +0000645 }
646
647 // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).
648 NonLocalPointerDeps.erase(It);
Chris Lattner7ebcf032008-12-07 02:15:47 +0000649}
650
651
Owen Anderson78e02f72007-07-06 23:14:35 +0000652/// removeInstruction - Remove an instruction from the dependence analysis,
653/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000654/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000655void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000656 // Walk through the Non-local dependencies, removing this one as the value
657 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +0000658 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
659 if (NLDI != NonLocalDeps.end()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000660 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattner25f4b2b2008-11-30 02:30:50 +0000661 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
662 DI != DE; ++DI)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000663 if (Instruction *Inst = DI->second.getInst())
Chris Lattnerd44745d2008-12-07 18:39:13 +0000664 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);
Chris Lattnerf68f3102008-11-30 02:28:25 +0000665 NonLocalDeps.erase(NLDI);
666 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000667
Chris Lattner5f589dc2008-11-28 22:04:47 +0000668 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000669 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000670 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
671 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000672 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerd44745d2008-12-07 18:39:13 +0000673 if (Instruction *Inst = LocalDepEntry->second.getInst())
674 RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);
Chris Lattner125ce362008-11-30 01:09:30 +0000675
Chris Lattnerbaad8882008-11-28 22:28:27 +0000676 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000677 LocalDeps.erase(LocalDepEntry);
Chris Lattner6290f5c2008-12-07 08:50:20 +0000678 }
679
680 // If we have any cached pointer dependencies on this instruction, remove
681 // them. If the instruction has non-pointer type, then it can't be a pointer
682 // base.
683
684 // Remove it from both the load info and the store info. The instruction
685 // can't be in either of these maps if it is non-pointer.
686 if (isa<PointerType>(RemInst->getType())) {
687 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));
688 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));
689 }
Chris Lattnerbaad8882008-11-28 22:28:27 +0000690
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000691 // Loop over all of the things that depend on the instruction we're removing.
692 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000693 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
Chris Lattner0655f732008-12-07 18:42:51 +0000694
695 // If we find RemInst as a clobber or Def in any of the maps for other values,
696 // we need to replace its entry with a dirty version of the instruction after
697 // it. If RemInst is a terminator, we use a null dirty value.
698 //
699 // Using a dirty version of the instruction after RemInst saves having to scan
700 // the entire block to get to this point.
701 MemDepResult NewDirtyVal;
702 if (!RemInst->isTerminator())
703 NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst));
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000704
Chris Lattner8c465272008-11-29 09:20:15 +0000705 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
706 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000707 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner6290f5c2008-12-07 08:50:20 +0000708 // RemInst can't be the terminator if it has local stuff depending on it.
Chris Lattner125ce362008-11-30 01:09:30 +0000709 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
710 "Nothing can locally depend on a terminator");
711
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000712 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
713 E = ReverseDeps.end(); I != E; ++I) {
714 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000715 assert(InstDependingOnRemInst != RemInst &&
716 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +0000717
Chris Lattner0655f732008-12-07 18:42:51 +0000718 LocalDeps[InstDependingOnRemInst] = NewDirtyVal;
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000719
Chris Lattner125ce362008-11-30 01:09:30 +0000720 // Make sure to remember that new things depend on NewDepInst.
Chris Lattner0655f732008-12-07 18:42:51 +0000721 assert(NewDirtyVal.getInst() && "There is no way something else can have "
722 "a local dep on this if it is a terminator!");
723 ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(),
Chris Lattner125ce362008-11-30 01:09:30 +0000724 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000725 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000726
727 ReverseLocalDeps.erase(ReverseDepIt);
728
729 // Add new reverse deps after scanning the set, to avoid invalidating the
730 // 'ReverseDeps' reference.
731 while (!ReverseDepsToAdd.empty()) {
732 ReverseLocalDeps[ReverseDepsToAdd.back().first]
733 .insert(ReverseDepsToAdd.back().second);
734 ReverseDepsToAdd.pop_back();
735 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000736 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000737
Chris Lattner8c465272008-11-29 09:20:15 +0000738 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
739 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner6290f5c2008-12-07 08:50:20 +0000740 SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second;
741 for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000742 I != E; ++I) {
743 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
744
Chris Lattner4a69bad2008-11-30 02:52:26 +0000745 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner4a69bad2008-11-30 02:52:26 +0000746 // The information is now dirty!
Chris Lattnerbf145d62008-12-01 01:15:42 +0000747 INLD.second = true;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000748
Chris Lattnerbf145d62008-12-01 01:15:42 +0000749 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
750 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000751 if (DI->second.getInst() != RemInst) continue;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000752
753 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner0655f732008-12-07 18:42:51 +0000754 DI->second = NewDirtyVal;
755
756 if (Instruction *NextI = NewDirtyVal.getInst())
Chris Lattnerf68f3102008-11-30 02:28:25 +0000757 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattnerf68f3102008-11-30 02:28:25 +0000758 }
759 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000760
761 ReverseNonLocalDeps.erase(ReverseDepIt);
762
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000763 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
764 while (!ReverseDepsToAdd.empty()) {
765 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
766 .insert(ReverseDepsToAdd.back().second);
767 ReverseDepsToAdd.pop_back();
768 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000769 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000770
Chris Lattner6290f5c2008-12-07 08:50:20 +0000771 // If the instruction is in ReverseNonLocalPtrDeps then it appears as a
772 // value in the NonLocalPointerDeps info.
773 ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =
774 ReverseNonLocalPtrDeps.find(RemInst);
775 if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {
776 SmallPtrSet<void*, 4> &Set = ReversePtrDepIt->second;
777 SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd;
778
779 for (SmallPtrSet<void*, 4>::iterator I = Set.begin(), E = Set.end();
780 I != E; ++I) {
781 ValueIsLoadPair P;
782 P.setFromOpaqueValue(*I);
783 assert(P.getPointer() != RemInst &&
784 "Already removed NonLocalPointerDeps info for RemInst");
785
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000786 NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].second;
787
788 // The cache is not valid for any specific block anymore.
789 NonLocalPointerDeps[P].first = 0;
Chris Lattner6290f5c2008-12-07 08:50:20 +0000790
Chris Lattner6290f5c2008-12-07 08:50:20 +0000791 // Update any entries for RemInst to use the instruction after it.
792 for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end();
793 DI != DE; ++DI) {
794 if (DI->second.getInst() != RemInst) continue;
795
796 // Convert to a dirty entry for the subsequent instruction.
797 DI->second = NewDirtyVal;
798
799 if (Instruction *NewDirtyInst = NewDirtyVal.getInst())
800 ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));
801 }
802 }
803
804 ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);
805
806 while (!ReversePtrDepsToAdd.empty()) {
807 ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first]
808 .insert(ReversePtrDepsToAdd.back().second.getOpaqueValue());
809 ReversePtrDepsToAdd.pop_back();
810 }
811 }
812
813
Chris Lattnerf68f3102008-11-30 02:28:25 +0000814 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnerd777d402008-11-30 19:24:31 +0000815 AA->deleteValue(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000816 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000817}
Chris Lattner729b2372008-11-29 21:25:10 +0000818
819/// verifyRemoved - Verify that the specified instruction does not occur
820/// in our internal data structures.
821void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
822 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
823 E = LocalDeps.end(); I != E; ++I) {
824 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000825 assert(I->second.getInst() != D &&
Chris Lattner729b2372008-11-29 21:25:10 +0000826 "Inst occurs in data structures");
827 }
828
Chris Lattner6290f5c2008-12-07 08:50:20 +0000829 for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(),
830 E = NonLocalPointerDeps.end(); I != E; ++I) {
831 assert(I->first.getPointer() != D && "Inst occurs in NLPD map key");
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000832 const NonLocalDepInfo &Val = I->second.second;
Chris Lattner6290f5c2008-12-07 08:50:20 +0000833 for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end();
834 II != E; ++II)
835 assert(II->second.getInst() != D && "Inst occurs as NLPD value");
836 }
837
Chris Lattner729b2372008-11-29 21:25:10 +0000838 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
839 E = NonLocalDeps.end(); I != E; ++I) {
840 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000841 const PerInstNLInfo &INLD = I->second;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000842 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
843 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000844 assert(II->second.getInst() != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000845 }
846
847 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +0000848 E = ReverseLocalDeps.end(); I != E; ++I) {
849 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000850 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
851 EE = I->second.end(); II != EE; ++II)
852 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000853 }
Chris Lattner729b2372008-11-29 21:25:10 +0000854
855 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
856 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000857 I != E; ++I) {
858 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000859 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
860 EE = I->second.end(); II != EE; ++II)
861 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000862 }
Chris Lattner6290f5c2008-12-07 08:50:20 +0000863
864 for (ReverseNonLocalPtrDepTy::const_iterator
865 I = ReverseNonLocalPtrDeps.begin(),
866 E = ReverseNonLocalPtrDeps.end(); I != E; ++I) {
867 assert(I->first != D && "Inst occurs in rev NLPD map");
868
869 for (SmallPtrSet<void*, 4>::const_iterator II = I->second.begin(),
870 E = I->second.end(); II != E; ++II)
871 assert(*II != ValueIsLoadPair(D, false).getOpaqueValue() &&
872 *II != ValueIsLoadPair(D, true).getOpaqueValue() &&
873 "Inst occurs in ReverseNonLocalPtrDeps map");
874 }
875
Chris Lattner729b2372008-11-29 21:25:10 +0000876}