blob: c47ec0493a5c4643b777c9a24cde1db48b95b0b2 [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"
24#include "llvm/ADT/STLExtras.h"
Owen Anderson4beedbd2007-07-24 21:52:37 +000025#include "llvm/Support/CFG.h"
Tanya Lattner63aa1602008-02-06 00:54:55 +000026#include "llvm/Support/CommandLine.h"
Chris Lattner0e575f42008-11-28 21:45:17 +000027#include "llvm/Support/Debug.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000028#include "llvm/Target/TargetData.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000029using namespace llvm;
30
Chris Lattnerbf145d62008-12-01 01:15:42 +000031STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
32STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
Chris Lattner0ec48dd2008-11-29 22:02:15 +000033STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Owen Anderson78e02f72007-07-06 23:14:35 +000034char MemoryDependenceAnalysis::ID = 0;
35
Owen Anderson78e02f72007-07-06 23:14:35 +000036// Register this pass...
Owen Anderson776ee1f2007-07-10 20:21:08 +000037static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Chris Lattner0e575f42008-11-28 21:45:17 +000038 "Memory Dependence Analysis", false, true);
Owen Anderson78e02f72007-07-06 23:14:35 +000039
40/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
41///
42void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.setPreservesAll();
44 AU.addRequiredTransitive<AliasAnalysis>();
45 AU.addRequiredTransitive<TargetData>();
46}
47
Chris Lattnerd777d402008-11-30 19:24:31 +000048bool MemoryDependenceAnalysis::runOnFunction(Function &) {
49 AA = &getAnalysis<AliasAnalysis>();
50 TD = &getAnalysis<TargetData>();
51 return false;
52}
53
Chris Lattnerbf145d62008-12-01 01:15:42 +000054
Owen Anderson642a9e32007-08-08 22:26:03 +000055/// getCallSiteDependency - Private helper for finding the local dependencies
56/// of a call site.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +000057MemDepResult MemoryDependenceAnalysis::
58getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt, BasicBlock *BB) {
Chris Lattnerbf145d62008-12-01 01:15:42 +000059
Owen Anderson642a9e32007-08-08 22:26:03 +000060 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +000061 while (ScanIt != BB->begin()) {
62 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +000063
64 // If this inst is a memory op, get the pointer it accessed
Chris Lattner00314b32008-11-29 09:15:21 +000065 Value *Pointer = 0;
66 uint64_t PointerSize = 0;
67 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
68 Pointer = S->getPointerOperand();
Chris Lattnerd777d402008-11-30 19:24:31 +000069 PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner00314b32008-11-29 09:15:21 +000070 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
71 Pointer = V->getOperand(0);
Chris Lattnerd777d402008-11-30 19:24:31 +000072 PointerSize = TD->getTypeStoreSize(V->getType());
Chris Lattner00314b32008-11-29 09:15:21 +000073 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
74 Pointer = F->getPointerOperand();
Owen Anderson5f323202007-07-10 17:59:22 +000075
76 // FreeInsts erase the entire structure
Chris Lattner00314b32008-11-29 09:15:21 +000077 PointerSize = ~0UL;
78 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Chris Lattnerd777d402008-11-30 19:24:31 +000079 if (AA->getModRefBehavior(CallSite::get(Inst)) ==
Chris Lattner5391a1d2008-11-29 03:47:00 +000080 AliasAnalysis::DoesNotAccessMemory)
Chris Lattner00314b32008-11-29 09:15:21 +000081 continue;
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +000082 return MemDepResult::get(Inst);
Chris Lattnercfbb6342008-11-30 01:44:00 +000083 } else {
84 // Non-memory instruction.
Owen Anderson202da142007-07-10 20:39:07 +000085 continue;
Chris Lattnercfbb6342008-11-30 01:44:00 +000086 }
Owen Anderson5f323202007-07-10 17:59:22 +000087
Chris Lattnerd777d402008-11-30 19:24:31 +000088 if (AA->getModRefInfo(C, Pointer, PointerSize) != AliasAnalysis::NoModRef)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +000089 return MemDepResult::get(Inst);
Owen Anderson5f323202007-07-10 17:59:22 +000090 }
91
Chris Lattner5391a1d2008-11-29 03:47:00 +000092 // No dependence found.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +000093 return MemDepResult::getNonLocal();
Owen Anderson5f323202007-07-10 17:59:22 +000094}
95
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +000096/// getDependencyFrom - Return the instruction on which a memory operation
97/// depends.
98MemDepResult MemoryDependenceAnalysis::
99getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
100 BasicBlock *BB) {
Owen Anderson78e02f72007-07-06 23:14:35 +0000101 // Get the pointer value for which dependence will be determined
Chris Lattner25a08142008-11-29 08:51:16 +0000102 Value *MemPtr = 0;
103 uint64_t MemSize = 0;
104 bool MemVolatile = false;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000105
106 if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000107 MemPtr = S->getPointerOperand();
Chris Lattnerd777d402008-11-30 19:24:31 +0000108 MemSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner25a08142008-11-29 08:51:16 +0000109 MemVolatile = S->isVolatile();
Chris Lattner5391a1d2008-11-29 03:47:00 +0000110 } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000111 MemPtr = L->getPointerOperand();
Chris Lattnerd777d402008-11-30 19:24:31 +0000112 MemSize = TD->getTypeStoreSize(L->getType());
Chris Lattner25a08142008-11-29 08:51:16 +0000113 MemVolatile = L->isVolatile();
Chris Lattner5391a1d2008-11-29 03:47:00 +0000114 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000115 MemPtr = V->getOperand(0);
Chris Lattnerd777d402008-11-30 19:24:31 +0000116 MemSize = TD->getTypeStoreSize(V->getType());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000117 } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000118 MemPtr = F->getPointerOperand();
119 // FreeInsts erase the entire structure, not just a field.
120 MemSize = ~0UL;
121 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst))
Chris Lattner5391a1d2008-11-29 03:47:00 +0000122 return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
Chris Lattner25a08142008-11-29 08:51:16 +0000123 else // Non-memory instructions depend on nothing.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000124 return MemDepResult::getNone();
Owen Anderson78e02f72007-07-06 23:14:35 +0000125
Owen Anderson642a9e32007-08-08 22:26:03 +0000126 // Walk backwards through the basic block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000127 while (ScanIt != BB->begin()) {
128 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000129
130 // If the access is volatile and this is a volatile load/store, return a
131 // dependence.
132 if (MemVolatile &&
133 ((isa<LoadInst>(Inst) && cast<LoadInst>(Inst)->isVolatile()) ||
134 (isa<StoreInst>(Inst) && cast<StoreInst>(Inst)->isVolatile())))
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000135 return MemDepResult::get(Inst);
Chris Lattnera161ab02008-11-29 09:09:48 +0000136
Chris Lattnercfbb6342008-11-30 01:44:00 +0000137 // Values depend on loads if the pointers are must aliased. This means that
138 // a load depends on another must aliased load from the same value.
Chris Lattnera161ab02008-11-29 09:09:48 +0000139 if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
140 Value *Pointer = L->getPointerOperand();
Chris Lattnerd777d402008-11-30 19:24:31 +0000141 uint64_t PointerSize = TD->getTypeStoreSize(L->getType());
Chris Lattnera161ab02008-11-29 09:09:48 +0000142
143 // If we found a pointer, check if it could be the same as our pointer
144 AliasAnalysis::AliasResult R =
Chris Lattnerd777d402008-11-30 19:24:31 +0000145 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
Chris Lattnera161ab02008-11-29 09:09:48 +0000146
147 if (R == AliasAnalysis::NoAlias)
148 continue;
149
150 // May-alias loads don't depend on each other without a dependence.
151 if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
152 continue;
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000153 return MemDepResult::get(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000154 }
Chris Lattner237a8282008-11-30 01:39:32 +0000155
156 // If this is an allocation, and if we know that the accessed pointer is to
157 // the allocation, return None. This means that there is no dependence and
158 // the access can be optimized based on that. For example, a load could
159 // turn into undef.
Chris Lattnera161ab02008-11-29 09:09:48 +0000160 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
Chris Lattner237a8282008-11-30 01:39:32 +0000161 Value *AccessPtr = MemPtr->getUnderlyingObject();
Owen Anderson78e02f72007-07-06 23:14:35 +0000162
Chris Lattner237a8282008-11-30 01:39:32 +0000163 if (AccessPtr == AI ||
Chris Lattnerd777d402008-11-30 19:24:31 +0000164 AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000165 return MemDepResult::getNone();
Chris Lattner237a8282008-11-30 01:39:32 +0000166 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000167 }
Chris Lattnera161ab02008-11-29 09:09:48 +0000168
169 // See if this instruction mod/ref's the pointer.
Chris Lattnerd777d402008-11-30 19:24:31 +0000170 AliasAnalysis::ModRefResult MRR = AA->getModRefInfo(Inst, MemPtr, MemSize);
Chris Lattnera161ab02008-11-29 09:09:48 +0000171
172 if (MRR == AliasAnalysis::NoModRef)
Chris Lattner25a08142008-11-29 08:51:16 +0000173 continue;
174
Chris Lattnera161ab02008-11-29 09:09:48 +0000175 // Loads don't depend on read-only instructions.
176 if (isa<LoadInst>(QueryInst) && MRR == AliasAnalysis::Ref)
Chris Lattner25a08142008-11-29 08:51:16 +0000177 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000178
179 // Otherwise, there is a dependence.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000180 return MemDepResult::get(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000181 }
182
Chris Lattner5391a1d2008-11-29 03:47:00 +0000183 // If we found nothing, return the non-local flag.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000184 return MemDepResult::getNonLocal();
Owen Anderson78e02f72007-07-06 23:14:35 +0000185}
186
Chris Lattner5391a1d2008-11-29 03:47:00 +0000187/// getDependency - Return the instruction on which a memory operation
188/// depends.
189MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
190 Instruction *ScanPos = QueryInst;
191
192 // Check for a cached result
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000193 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattner5391a1d2008-11-29 03:47:00 +0000194
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000195 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000196 // on MemDepResult's default constructing to 'dirty'.
197 if (!LocalCache.isDirty())
198 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000199
200 // Otherwise, if we have a dirty entry, we know we can start the scan at that
201 // instruction, which may save us some work.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000202 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000203 ScanPos = Inst;
Chris Lattner4a69bad2008-11-30 02:52:26 +0000204
205 SmallPtrSet<Instruction*, 4> &InstMap = ReverseLocalDeps[Inst];
206 InstMap.erase(QueryInst);
207 if (InstMap.empty())
208 ReverseLocalDeps.erase(Inst);
209 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000210
211 // Do the scan.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000212 LocalCache = getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000213
214 // Remember the result!
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000215 if (Instruction *I = LocalCache.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000216 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000217
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000218 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000219}
220
Chris Lattner37d041c2008-11-30 01:18:27 +0000221/// getNonLocalDependency - Perform a full dependency query for the
222/// specified instruction, returning the set of blocks that the value is
223/// potentially live across. The returned set of results will include a
224/// "NonLocal" result for all blocks where the value is live across.
225///
226/// This method assumes the instruction returns a "nonlocal" dependency
227/// within its own block.
228///
Chris Lattnerbf145d62008-12-01 01:15:42 +0000229const MemoryDependenceAnalysis::NonLocalDepInfo &
230MemoryDependenceAnalysis::getNonLocalDependency(Instruction *QueryInst) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000231 assert(getDependency(QueryInst).isNonLocal() &&
232 "getNonLocalDependency should only be used on insts with non-local deps!");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000233 PerInstNLInfo &CacheP = NonLocalDeps[QueryInst];
Chris Lattnerf68f3102008-11-30 02:28:25 +0000234
Chris Lattnerbf145d62008-12-01 01:15:42 +0000235 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner37d041c2008-11-30 01:18:27 +0000236
237 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
238 /// the cached case, this can happen due to instructions being deleted etc. In
239 /// the uncached case, this starts out as the set of predecessors we care
240 /// about.
241 SmallVector<BasicBlock*, 32> DirtyBlocks;
242
243 if (!Cache.empty()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000244 // Okay, we have a cache entry. If we know it is not dirty, just return it
245 // with no computation.
246 if (!CacheP.second) {
247 NumCacheNonLocal++;
248 return Cache;
249 }
250
Chris Lattner37d041c2008-11-30 01:18:27 +0000251 // If we already have a partially computed set of results, scan them to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000252 // determine what is dirty, seeding our initial DirtyBlocks worklist.
253 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
254 I != E; ++I)
255 if (I->second.isDirty())
256 DirtyBlocks.push_back(I->first);
Chris Lattner37d041c2008-11-30 01:18:27 +0000257
Chris Lattnerbf145d62008-12-01 01:15:42 +0000258 // Sort the cache so that we can do fast binary search lookups below.
259 std::sort(Cache.begin(), Cache.end());
Chris Lattner37d041c2008-11-30 01:18:27 +0000260
Chris Lattnerbf145d62008-12-01 01:15:42 +0000261 ++NumCacheDirtyNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000262 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
263 // << Cache.size() << " cached: " << *QueryInst;
264 } else {
265 // Seed DirtyBlocks with each of the preds of QueryInst's block.
266 BasicBlock *QueryBB = QueryInst->getParent();
267 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
268 NumUncacheNonLocal++;
269 }
270
Chris Lattnerbf145d62008-12-01 01:15:42 +0000271 // Visited checked first, vector in sorted order.
272 SmallPtrSet<BasicBlock*, 64> Visited;
273
274 unsigned NumSortedEntries = Cache.size();
275
Chris Lattner37d041c2008-11-30 01:18:27 +0000276 // Iterate while we still have blocks to update.
277 while (!DirtyBlocks.empty()) {
278 BasicBlock *DirtyBB = DirtyBlocks.back();
279 DirtyBlocks.pop_back();
280
Chris Lattnerbf145d62008-12-01 01:15:42 +0000281 // Already processed this block?
282 if (!Visited.insert(DirtyBB))
283 continue;
Chris Lattner37d041c2008-11-30 01:18:27 +0000284
Chris Lattnerbf145d62008-12-01 01:15:42 +0000285 // Do a binary search to see if we already have an entry for this block in
286 // the cache set. If so, find it.
287 NonLocalDepInfo::iterator Entry =
288 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
289 std::make_pair(DirtyBB, MemDepResult()));
290 if (Entry != Cache.begin() && (&*Entry)[-1].first == DirtyBB)
291 --Entry;
292
293 MemDepResult *ExistingResult = 0;
294 if (Entry != Cache.begin()+NumSortedEntries &&
295 Entry->first == DirtyBB) {
296 // If we already have an entry, and if it isn't already dirty, the block
297 // is done.
298 if (!Entry->second.isDirty())
299 continue;
300
301 // Otherwise, remember this slot so we can update the value.
302 ExistingResult = &Entry->second;
303 }
304
Chris Lattner37d041c2008-11-30 01:18:27 +0000305 // If the dirty entry has a pointer, start scanning from it so we don't have
306 // to rescan the entire block.
307 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerbf145d62008-12-01 01:15:42 +0000308 if (ExistingResult) {
309 if (Instruction *Inst = ExistingResult->getInst()) {
310 ScanPos = Inst;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000311
Chris Lattnerbf145d62008-12-01 01:15:42 +0000312 // We're removing QueryInst's use of Inst.
313 SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst];
314 InstMap.erase(QueryInst);
315 if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst);
316 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000317 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000318
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000319 // Find out if this block has a local dependency for QueryInst.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000320 MemDepResult Dep = getDependencyFrom(QueryInst, ScanPos, DirtyBB);
321
322 // If we had a dirty entry for the block, update it. Otherwise, just add
323 // a new entry.
324 if (ExistingResult)
325 *ExistingResult = Dep;
326 else
327 Cache.push_back(std::make_pair(DirtyBB, Dep));
328
Chris Lattner37d041c2008-11-30 01:18:27 +0000329 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000330 // the value), remember the association!
331 if (!Dep.isNonLocal()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000332 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
333 // update this when we remove instructions.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000334 if (Instruction *Inst = Dep.getInst())
Chris Lattner37d041c2008-11-30 01:18:27 +0000335 ReverseNonLocalDeps[Inst].insert(QueryInst);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000336 } else {
Chris Lattner37d041c2008-11-30 01:18:27 +0000337
Chris Lattnerbf145d62008-12-01 01:15:42 +0000338 // If the block *is* completely transparent to the load, we need to check
339 // the predecessors of this block. Add them to our worklist.
340 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
341 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000342 }
343
Chris Lattnerbf145d62008-12-01 01:15:42 +0000344 return Cache;
Chris Lattner37d041c2008-11-30 01:18:27 +0000345}
346
Owen Anderson78e02f72007-07-06 23:14:35 +0000347/// removeInstruction - Remove an instruction from the dependence analysis,
348/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000349/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000350void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000351 // Walk through the Non-local dependencies, removing this one as the value
352 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +0000353 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
354 if (NLDI != NonLocalDeps.end()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000355 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattner25f4b2b2008-11-30 02:30:50 +0000356 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
357 DI != DE; ++DI)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000358 if (Instruction *Inst = DI->second.getInst())
Chris Lattnerf68f3102008-11-30 02:28:25 +0000359 ReverseNonLocalDeps[Inst].erase(RemInst);
Chris Lattnerf68f3102008-11-30 02:28:25 +0000360 NonLocalDeps.erase(NLDI);
361 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000362
Chris Lattner5f589dc2008-11-28 22:04:47 +0000363 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000364 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000365 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
366 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000367 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000368 if (Instruction *Inst = LocalDepEntry->second.getInst()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000369 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
370 RLD.erase(RemInst);
371 if (RLD.empty())
372 ReverseLocalDeps.erase(Inst);
373 }
374
Chris Lattnerbaad8882008-11-28 22:28:27 +0000375 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000376 LocalDeps.erase(LocalDepEntry);
Chris Lattner125ce362008-11-30 01:09:30 +0000377 }
Chris Lattnerbaad8882008-11-28 22:28:27 +0000378
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000379 // Loop over all of the things that depend on the instruction we're removing.
380 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000381 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
382
Chris Lattner8c465272008-11-29 09:20:15 +0000383 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
384 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000385 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner125ce362008-11-30 01:09:30 +0000386 // RemInst can't be the terminator if it has stuff depending on it.
387 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
388 "Nothing can locally depend on a terminator");
389
390 // Anything that was locally dependent on RemInst is now going to be
391 // dependent on the instruction after RemInst. It will have the dirty flag
392 // set so it will rescan. This saves having to scan the entire block to get
393 // to this point.
394 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
395
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000396 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
397 E = ReverseDeps.end(); I != E; ++I) {
398 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000399 assert(InstDependingOnRemInst != RemInst &&
400 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +0000401
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000402 LocalDeps[InstDependingOnRemInst] = MemDepResult::getDirty(NewDepInst);
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000403
Chris Lattner125ce362008-11-30 01:09:30 +0000404 // Make sure to remember that new things depend on NewDepInst.
405 ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
406 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000407 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000408
409 ReverseLocalDeps.erase(ReverseDepIt);
410
411 // Add new reverse deps after scanning the set, to avoid invalidating the
412 // 'ReverseDeps' reference.
413 while (!ReverseDepsToAdd.empty()) {
414 ReverseLocalDeps[ReverseDepsToAdd.back().first]
415 .insert(ReverseDepsToAdd.back().second);
416 ReverseDepsToAdd.pop_back();
417 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000418 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000419
Chris Lattner8c465272008-11-29 09:20:15 +0000420 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
421 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000422 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson4d13de42007-08-16 21:27:05 +0000423 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000424 I != E; ++I) {
425 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
426
Chris Lattner4a69bad2008-11-30 02:52:26 +0000427 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner4a69bad2008-11-30 02:52:26 +0000428 // The information is now dirty!
Chris Lattnerbf145d62008-12-01 01:15:42 +0000429 INLD.second = true;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000430
Chris Lattnerbf145d62008-12-01 01:15:42 +0000431 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
432 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000433 if (DI->second.getInst() != RemInst) continue;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000434
435 // Convert to a dirty entry for the subsequent instruction.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000436 Instruction *NextI = 0;
437 if (!RemInst->isTerminator()) {
438 NextI = next(BasicBlock::iterator(RemInst));
Chris Lattnerf68f3102008-11-30 02:28:25 +0000439 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000440 }
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000441 DI->second = MemDepResult::getDirty(NextI);
Chris Lattnerf68f3102008-11-30 02:28:25 +0000442 }
443 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000444
445 ReverseNonLocalDeps.erase(ReverseDepIt);
446
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000447 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
448 while (!ReverseDepsToAdd.empty()) {
449 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
450 .insert(ReverseDepsToAdd.back().second);
451 ReverseDepsToAdd.pop_back();
452 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000453 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000454
Chris Lattnerf68f3102008-11-30 02:28:25 +0000455 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnerd777d402008-11-30 19:24:31 +0000456 AA->deleteValue(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000457 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000458}
Chris Lattner729b2372008-11-29 21:25:10 +0000459
460/// verifyRemoved - Verify that the specified instruction does not occur
461/// in our internal data structures.
462void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
463 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
464 E = LocalDeps.end(); I != E; ++I) {
465 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000466 assert(I->second.getInst() != D &&
Chris Lattner729b2372008-11-29 21:25:10 +0000467 "Inst occurs in data structures");
468 }
469
470 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
471 E = NonLocalDeps.end(); I != E; ++I) {
472 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000473 const PerInstNLInfo &INLD = I->second;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000474 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
475 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000476 assert(II->second.getInst() != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000477 }
478
479 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +0000480 E = ReverseLocalDeps.end(); I != E; ++I) {
481 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000482 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
483 EE = I->second.end(); II != EE; ++II)
484 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000485 }
Chris Lattner729b2372008-11-29 21:25:10 +0000486
487 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
488 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000489 I != E; ++I) {
490 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000491 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
492 EE = I->second.end(); II != EE; ++II)
493 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000494 }
Chris Lattner729b2372008-11-29 21:25:10 +0000495}