blob: 7ee68897f6644d04636c81747e0c52c85f6905f5 [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 Lattner0ec48dd2008-11-29 22:02:15 +000031STATISTIC(NumCacheNonLocal, "Number of cached non-local responses");
32STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Owen Anderson7fad7e32007-09-09 21:43:49 +000033
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
Owen Anderson642a9e32007-08-08 22:26:03 +000054/// getCallSiteDependency - Private helper for finding the local dependencies
55/// of a call site.
Chris Lattner73ec3cd2008-11-30 01:26:32 +000056MemoryDependenceAnalysis::DepResultTy MemoryDependenceAnalysis::
Chris Lattner5391a1d2008-11-29 03:47:00 +000057getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
58 BasicBlock *BB) {
Owen Anderson642a9e32007-08-08 22:26:03 +000059 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +000060 while (ScanIt != BB->begin()) {
61 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +000062
63 // If this inst is a memory op, get the pointer it accessed
Chris Lattner00314b32008-11-29 09:15:21 +000064 Value *Pointer = 0;
65 uint64_t PointerSize = 0;
66 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
67 Pointer = S->getPointerOperand();
Chris Lattnerd777d402008-11-30 19:24:31 +000068 PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner00314b32008-11-29 09:15:21 +000069 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
70 Pointer = V->getOperand(0);
Chris Lattnerd777d402008-11-30 19:24:31 +000071 PointerSize = TD->getTypeStoreSize(V->getType());
Chris Lattner00314b32008-11-29 09:15:21 +000072 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
73 Pointer = F->getPointerOperand();
Owen Anderson5f323202007-07-10 17:59:22 +000074
75 // FreeInsts erase the entire structure
Chris Lattner00314b32008-11-29 09:15:21 +000076 PointerSize = ~0UL;
77 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Chris Lattnerd777d402008-11-30 19:24:31 +000078 if (AA->getModRefBehavior(CallSite::get(Inst)) ==
Chris Lattner5391a1d2008-11-29 03:47:00 +000079 AliasAnalysis::DoesNotAccessMemory)
Chris Lattner00314b32008-11-29 09:15:21 +000080 continue;
Chris Lattner73ec3cd2008-11-30 01:26:32 +000081 return DepResultTy(Inst, Normal);
Chris Lattnercfbb6342008-11-30 01:44:00 +000082 } else {
83 // Non-memory instruction.
Owen Anderson202da142007-07-10 20:39:07 +000084 continue;
Chris Lattnercfbb6342008-11-30 01:44:00 +000085 }
Owen Anderson5f323202007-07-10 17:59:22 +000086
Chris Lattnerd777d402008-11-30 19:24:31 +000087 if (AA->getModRefInfo(C, Pointer, PointerSize) != AliasAnalysis::NoModRef)
Chris Lattner73ec3cd2008-11-30 01:26:32 +000088 return DepResultTy(Inst, Normal);
Owen Anderson5f323202007-07-10 17:59:22 +000089 }
90
Chris Lattner5391a1d2008-11-29 03:47:00 +000091 // No dependence found.
Chris Lattner73ec3cd2008-11-30 01:26:32 +000092 return DepResultTy(0, NonLocal);
Owen Anderson5f323202007-07-10 17:59:22 +000093}
94
Owen Anderson78e02f72007-07-06 23:14:35 +000095/// getDependency - Return the instruction on which a memory operation
Dan Gohmanc04575f2008-04-10 23:02:38 +000096/// depends. The local parameter indicates if the query should only
Owen Anderson6b278fc2007-07-10 17:08:11 +000097/// evaluate dependencies within the same basic block.
Chris Lattner73ec3cd2008-11-30 01:26:32 +000098MemoryDependenceAnalysis::DepResultTy MemoryDependenceAnalysis::
99getDependencyFromInternal(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 Lattner73ec3cd2008-11-30 01:26:32 +0000124 return DepResultTy(0, None);
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 Lattner73ec3cd2008-11-30 01:26:32 +0000135 return DepResultTy(Inst, Normal);
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 Lattner73ec3cd2008-11-30 01:26:32 +0000153 return DepResultTy(Inst, Normal);
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 Lattner237a8282008-11-30 01:39:32 +0000165 return DepResultTy(0, None);
166 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 Lattner73ec3cd2008-11-30 01:26:32 +0000180 return DepResultTy(Inst, Normal);
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 Lattner73ec3cd2008-11-30 01:26:32 +0000184 return DepResultTy(0, NonLocal);
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
193 DepResultTy &LocalCache = LocalDeps[QueryInst];
194
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000195 // If the cached entry is non-dirty, just return it. Note that this depends
196 // on DepResultTy's default constructing to 'dirty'.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000197 if (LocalCache.getInt() != Dirty)
198 return ConvToResult(LocalCache);
199
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 Lattner4a69bad2008-11-30 02:52:26 +0000202 if (Instruction *Inst = LocalCache.getPointer()) {
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 Lattner73ec3cd2008-11-30 01:26:32 +0000212 LocalCache = getDependencyFromInternal(QueryInst, ScanPos,
213 QueryInst->getParent());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000214
215 // Remember the result!
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000216 if (Instruction *I = LocalCache.getPointer())
Chris Lattner8c465272008-11-29 09:20:15 +0000217 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000218
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000219 return ConvToResult(LocalCache);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000220}
221
Chris Lattner37d041c2008-11-30 01:18:27 +0000222/// getNonLocalDependency - Perform a full dependency query for the
223/// specified instruction, returning the set of blocks that the value is
224/// potentially live across. The returned set of results will include a
225/// "NonLocal" result for all blocks where the value is live across.
226///
227/// This method assumes the instruction returns a "nonlocal" dependency
228/// within its own block.
229///
230void MemoryDependenceAnalysis::
231getNonLocalDependency(Instruction *QueryInst,
232 SmallVectorImpl<std::pair<BasicBlock*,
233 MemDepResult> > &Result) {
234 assert(getDependency(QueryInst).isNonLocal() &&
235 "getNonLocalDependency should only be used on insts with non-local deps!");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000236 PerInstNLInfo &CacheP = NonLocalDeps[QueryInst];
237 if (CacheP.getPointer() == 0) CacheP.setPointer(new NonLocalDepInfo());
Chris Lattnerf68f3102008-11-30 02:28:25 +0000238
Chris Lattner4a69bad2008-11-30 02:52:26 +0000239 NonLocalDepInfo &Cache = *CacheP.getPointer();
Chris Lattner37d041c2008-11-30 01:18:27 +0000240
241 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
242 /// the cached case, this can happen due to instructions being deleted etc. In
243 /// the uncached case, this starts out as the set of predecessors we care
244 /// about.
245 SmallVector<BasicBlock*, 32> DirtyBlocks;
246
247 if (!Cache.empty()) {
248 // If we already have a partially computed set of results, scan them to
Chris Lattner4a69bad2008-11-30 02:52:26 +0000249 // determine what is dirty, seeding our initial DirtyBlocks worklist. The
250 // Int bit of CacheP tells us if we have anything dirty.
251 if (CacheP.getInt())
252 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
Chris Lattner25f4b2b2008-11-30 02:30:50 +0000253 I != E; ++I)
Chris Lattner4a69bad2008-11-30 02:52:26 +0000254 if (I->second.getInt() == Dirty)
255 DirtyBlocks.push_back(I->first);
Chris Lattner37d041c2008-11-30 01:18:27 +0000256
257 NumCacheNonLocal++;
258
259 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
260 // << Cache.size() << " cached: " << *QueryInst;
261 } else {
262 // Seed DirtyBlocks with each of the preds of QueryInst's block.
263 BasicBlock *QueryBB = QueryInst->getParent();
264 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
265 NumUncacheNonLocal++;
266 }
267
268 // Iterate while we still have blocks to update.
269 while (!DirtyBlocks.empty()) {
270 BasicBlock *DirtyBB = DirtyBlocks.back();
271 DirtyBlocks.pop_back();
272
273 // Get the entry for this block. Note that this relies on DepResultTy
274 // default initializing to Dirty.
275 DepResultTy &DirtyBBEntry = Cache[DirtyBB];
276
277 // If DirtyBBEntry isn't dirty, it ended up on the worklist multiple times.
278 if (DirtyBBEntry.getInt() != Dirty) continue;
279
Chris Lattner37d041c2008-11-30 01:18:27 +0000280 // If the dirty entry has a pointer, start scanning from it so we don't have
281 // to rescan the entire block.
282 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000283 if (Instruction *Inst = DirtyBBEntry.getPointer()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000284 ScanPos = Inst;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000285
286 // We're removing QueryInst's dependence on Inst.
287 SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst];
288 InstMap.erase(QueryInst);
289 if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst);
290 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000291
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000292 // Find out if this block has a local dependency for QueryInst.
293 DirtyBBEntry = getDependencyFromInternal(QueryInst, ScanPos, DirtyBB);
Chris Lattner37d041c2008-11-30 01:18:27 +0000294
295 // If the block has a dependency (i.e. it isn't completely transparent to
296 // the value), remember it!
297 if (DirtyBBEntry.getInt() != NonLocal) {
298 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
299 // update this when we remove instructions.
300 if (Instruction *Inst = DirtyBBEntry.getPointer())
301 ReverseNonLocalDeps[Inst].insert(QueryInst);
302 continue;
303 }
304
305 // If the block *is* completely transparent to the load, we need to check
306 // the predecessors of this block. Add them to our worklist.
307 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
308 }
309
310
311 // Copy the result into the output set.
Chris Lattner25f4b2b2008-11-30 02:30:50 +0000312 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end(); I != E;++I)
Chris Lattner37d041c2008-11-30 01:18:27 +0000313 Result.push_back(std::make_pair(I->first, ConvToResult(I->second)));
314}
315
Owen Anderson78e02f72007-07-06 23:14:35 +0000316/// removeInstruction - Remove an instruction from the dependence analysis,
317/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000318/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000319void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000320 // Walk through the Non-local dependencies, removing this one as the value
321 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +0000322 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
323 if (NLDI != NonLocalDeps.end()) {
Chris Lattner4a69bad2008-11-30 02:52:26 +0000324 NonLocalDepInfo &BlockMap = *NLDI->second.getPointer();
Chris Lattner25f4b2b2008-11-30 02:30:50 +0000325 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
326 DI != DE; ++DI)
Chris Lattnerf68f3102008-11-30 02:28:25 +0000327 if (Instruction *Inst = DI->second.getPointer())
328 ReverseNonLocalDeps[Inst].erase(RemInst);
329 delete &BlockMap;
330 NonLocalDeps.erase(NLDI);
331 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000332
Chris Lattner5f589dc2008-11-28 22:04:47 +0000333 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000334 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000335 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
336 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000337 // Remove us from DepInst's reverse set now that the local dep info is gone.
338 if (Instruction *Inst = LocalDepEntry->second.getPointer()) {
339 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
340 RLD.erase(RemInst);
341 if (RLD.empty())
342 ReverseLocalDeps.erase(Inst);
343 }
344
Chris Lattnerbaad8882008-11-28 22:28:27 +0000345 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000346 LocalDeps.erase(LocalDepEntry);
Chris Lattner125ce362008-11-30 01:09:30 +0000347 }
Chris Lattnerbaad8882008-11-28 22:28:27 +0000348
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000349 // Loop over all of the things that depend on the instruction we're removing.
350 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000351 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
352
Chris Lattner8c465272008-11-29 09:20:15 +0000353 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
354 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000355 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner125ce362008-11-30 01:09:30 +0000356 // RemInst can't be the terminator if it has stuff depending on it.
357 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
358 "Nothing can locally depend on a terminator");
359
360 // Anything that was locally dependent on RemInst is now going to be
361 // dependent on the instruction after RemInst. It will have the dirty flag
362 // set so it will rescan. This saves having to scan the entire block to get
363 // to this point.
364 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
365
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000366 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
367 E = ReverseDeps.end(); I != E; ++I) {
368 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000369 assert(InstDependingOnRemInst != RemInst &&
370 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +0000371
372 LocalDeps[InstDependingOnRemInst] = DepResultTy(NewDepInst, Dirty);
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000373
Chris Lattner125ce362008-11-30 01:09:30 +0000374 // Make sure to remember that new things depend on NewDepInst.
375 ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
376 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000377 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000378
379 ReverseLocalDeps.erase(ReverseDepIt);
380
381 // Add new reverse deps after scanning the set, to avoid invalidating the
382 // 'ReverseDeps' reference.
383 while (!ReverseDepsToAdd.empty()) {
384 ReverseLocalDeps[ReverseDepsToAdd.back().first]
385 .insert(ReverseDepsToAdd.back().second);
386 ReverseDepsToAdd.pop_back();
387 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000388 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000389
Chris Lattner8c465272008-11-29 09:20:15 +0000390 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
391 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000392 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson4d13de42007-08-16 21:27:05 +0000393 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000394 I != E; ++I) {
395 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
396
Chris Lattner4a69bad2008-11-30 02:52:26 +0000397 PerInstNLInfo &INLD = NonLocalDeps[*I];
398 assert(INLD.getPointer() != 0 && "Reverse mapping out of date?");
399 // The information is now dirty!
400 INLD.setInt(true);
Chris Lattnerf68f3102008-11-30 02:28:25 +0000401
Chris Lattner4a69bad2008-11-30 02:52:26 +0000402 for (NonLocalDepInfo::iterator DI = INLD.getPointer()->begin(),
403 DE = INLD.getPointer()->end(); DI != DE; ++DI) {
Chris Lattnerf68f3102008-11-30 02:28:25 +0000404 if (DI->second.getPointer() != RemInst) continue;
405
406 // Convert to a dirty entry for the subsequent instruction.
407 DI->second.setInt(Dirty);
408 if (RemInst->isTerminator())
409 DI->second.setPointer(0);
410 else {
411 Instruction *NextI = next(BasicBlock::iterator(RemInst));
412 DI->second.setPointer(NextI);
413 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000414 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000415 }
416 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000417
418 ReverseNonLocalDeps.erase(ReverseDepIt);
419
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000420 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
421 while (!ReverseDepsToAdd.empty()) {
422 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
423 .insert(ReverseDepsToAdd.back().second);
424 ReverseDepsToAdd.pop_back();
425 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000426 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000427
Chris Lattnerf68f3102008-11-30 02:28:25 +0000428 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnerd777d402008-11-30 19:24:31 +0000429 AA->deleteValue(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000430 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000431}
Chris Lattner729b2372008-11-29 21:25:10 +0000432
433/// verifyRemoved - Verify that the specified instruction does not occur
434/// in our internal data structures.
435void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
436 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
437 E = LocalDeps.end(); I != E; ++I) {
438 assert(I->first != D && "Inst occurs in data structures");
439 assert(I->second.getPointer() != D &&
440 "Inst occurs in data structures");
441 }
442
443 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
444 E = NonLocalDeps.end(); I != E; ++I) {
445 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000446 const PerInstNLInfo &INLD = I->second;
447 for (NonLocalDepInfo::iterator II = INLD.getPointer()->begin(),
448 EE = INLD.getPointer()->end(); II != EE; ++II)
Chris Lattner729b2372008-11-29 21:25:10 +0000449 assert(II->second.getPointer() != D && "Inst occurs in data structures");
450 }
451
452 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +0000453 E = ReverseLocalDeps.end(); I != E; ++I) {
454 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000455 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
456 EE = I->second.end(); II != EE; ++II)
457 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000458 }
Chris Lattner729b2372008-11-29 21:25:10 +0000459
460 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
461 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000462 I != E; ++I) {
463 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000464 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
465 EE = I->second.end(); II != EE; ++II)
466 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000467 }
Chris Lattner729b2372008-11-29 21:25:10 +0000468}