blob: 94a3d1ba78969210beb8c29e77303f761b0e99b8 [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
Owen Anderson642a9e32007-08-08 22:26:03 +000048/// getCallSiteDependency - Private helper for finding the local dependencies
49/// of a call site.
Chris Lattner73ec3cd2008-11-30 01:26:32 +000050MemoryDependenceAnalysis::DepResultTy MemoryDependenceAnalysis::
Chris Lattner5391a1d2008-11-29 03:47:00 +000051getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
52 BasicBlock *BB) {
Chris Lattner7f524222008-11-29 03:22:12 +000053 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
54 TargetData &TD = getAnalysis<TargetData>();
Owen Andersondbbe8162007-08-07 00:33:45 +000055
Owen Anderson642a9e32007-08-08 22:26:03 +000056 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +000057 while (ScanIt != BB->begin()) {
58 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +000059
60 // If this inst is a memory op, get the pointer it accessed
Chris Lattner00314b32008-11-29 09:15:21 +000061 Value *Pointer = 0;
62 uint64_t PointerSize = 0;
63 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
64 Pointer = S->getPointerOperand();
65 PointerSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
66 } else if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
67 Pointer = AI;
68 if (ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner86b29ef2008-11-29 21:22:42 +000069 // Use ABI size (size between elements), not store size (size of one
70 // element without padding).
Chris Lattner00314b32008-11-29 09:15:21 +000071 PointerSize = C->getZExtValue() *
Chris Lattner86b29ef2008-11-29 21:22:42 +000072 TD.getABITypeSize(AI->getAllocatedType());
Owen Anderson5f323202007-07-10 17:59:22 +000073 else
Chris Lattner00314b32008-11-29 09:15:21 +000074 PointerSize = ~0UL;
75 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
76 Pointer = V->getOperand(0);
77 PointerSize = TD.getTypeStoreSize(V->getType());
78 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
79 Pointer = F->getPointerOperand();
Owen Anderson5f323202007-07-10 17:59:22 +000080
81 // FreeInsts erase the entire structure
Chris Lattner00314b32008-11-29 09:15:21 +000082 PointerSize = ~0UL;
83 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
84 if (AA.getModRefBehavior(CallSite::get(Inst)) ==
Chris Lattner5391a1d2008-11-29 03:47:00 +000085 AliasAnalysis::DoesNotAccessMemory)
Chris Lattner00314b32008-11-29 09:15:21 +000086 continue;
Chris Lattner73ec3cd2008-11-30 01:26:32 +000087 return DepResultTy(Inst, Normal);
Owen Anderson202da142007-07-10 20:39:07 +000088 } else
89 continue;
Owen Anderson5f323202007-07-10 17:59:22 +000090
Chris Lattner00314b32008-11-29 09:15:21 +000091 if (AA.getModRefInfo(C, Pointer, PointerSize) != AliasAnalysis::NoModRef)
Chris Lattner73ec3cd2008-11-30 01:26:32 +000092 return DepResultTy(Inst, Normal);
Owen Anderson5f323202007-07-10 17:59:22 +000093 }
94
Chris Lattner5391a1d2008-11-29 03:47:00 +000095 // No dependence found.
Chris Lattner73ec3cd2008-11-30 01:26:32 +000096 return DepResultTy(0, NonLocal);
Owen Anderson5f323202007-07-10 17:59:22 +000097}
98
Owen Anderson78e02f72007-07-06 23:14:35 +000099/// getDependency - Return the instruction on which a memory operation
Dan Gohmanc04575f2008-04-10 23:02:38 +0000100/// depends. The local parameter indicates if the query should only
Owen Anderson6b278fc2007-07-10 17:08:11 +0000101/// evaluate dependencies within the same basic block.
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000102MemoryDependenceAnalysis::DepResultTy MemoryDependenceAnalysis::
103getDependencyFromInternal(Instruction *QueryInst, BasicBlock::iterator ScanIt,
104 BasicBlock *BB) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000105 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
106 TargetData &TD = getAnalysis<TargetData>();
Owen Anderson78e02f72007-07-06 23:14:35 +0000107
108 // Get the pointer value for which dependence will be determined
Chris Lattner25a08142008-11-29 08:51:16 +0000109 Value *MemPtr = 0;
110 uint64_t MemSize = 0;
111 bool MemVolatile = false;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000112
113 if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000114 MemPtr = S->getPointerOperand();
115 MemSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
116 MemVolatile = S->isVolatile();
Chris Lattner5391a1d2008-11-29 03:47:00 +0000117 } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000118 MemPtr = L->getPointerOperand();
119 MemSize = TD.getTypeStoreSize(L->getType());
120 MemVolatile = L->isVolatile();
Chris Lattner5391a1d2008-11-29 03:47:00 +0000121 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000122 MemPtr = V->getOperand(0);
123 MemSize = TD.getTypeStoreSize(V->getType());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000124 } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000125 MemPtr = F->getPointerOperand();
126 // FreeInsts erase the entire structure, not just a field.
127 MemSize = ~0UL;
128 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst))
Chris Lattner5391a1d2008-11-29 03:47:00 +0000129 return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
Chris Lattner25a08142008-11-29 08:51:16 +0000130 else // Non-memory instructions depend on nothing.
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000131 return DepResultTy(0, None);
Owen Anderson78e02f72007-07-06 23:14:35 +0000132
Owen Anderson642a9e32007-08-08 22:26:03 +0000133 // Walk backwards through the basic block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000134 while (ScanIt != BB->begin()) {
135 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000136
137 // If the access is volatile and this is a volatile load/store, return a
138 // dependence.
139 if (MemVolatile &&
140 ((isa<LoadInst>(Inst) && cast<LoadInst>(Inst)->isVolatile()) ||
141 (isa<StoreInst>(Inst) && cast<StoreInst>(Inst)->isVolatile())))
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000142 return DepResultTy(Inst, Normal);
Chris Lattnera161ab02008-11-29 09:09:48 +0000143
144 // MemDep is broken w.r.t. loads: it says that two loads of the same pointer
145 // depend on each other. :(
Chris Lattnera161ab02008-11-29 09:09:48 +0000146 if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
147 Value *Pointer = L->getPointerOperand();
148 uint64_t PointerSize = TD.getTypeStoreSize(L->getType());
149
150 // If we found a pointer, check if it could be the same as our pointer
151 AliasAnalysis::AliasResult R =
152 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
153
154 if (R == AliasAnalysis::NoAlias)
155 continue;
156
157 // May-alias loads don't depend on each other without a dependence.
158 if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
159 continue;
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000160 return DepResultTy(Inst, Normal);
Owen Anderson78e02f72007-07-06 23:14:35 +0000161 }
162
Chris Lattnera161ab02008-11-29 09:09:48 +0000163 // FIXME: This claims that an access depends on the allocation. This may
164 // make sense, but is dubious at best. It would be better to fix GVN to
165 // handle a 'None' Query.
166 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
167 Value *Pointer = AI;
168 uint64_t PointerSize;
169 if (ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner86b29ef2008-11-29 21:22:42 +0000170 // Use ABI size (size between elements), not store size (size of one
171 // element without padding).
Chris Lattnera161ab02008-11-29 09:09:48 +0000172 PointerSize = C->getZExtValue() *
Chris Lattner86b29ef2008-11-29 21:22:42 +0000173 TD.getABITypeSize(AI->getAllocatedType());
Chris Lattnera161ab02008-11-29 09:09:48 +0000174 else
175 PointerSize = ~0UL;
Owen Anderson78e02f72007-07-06 23:14:35 +0000176
Chris Lattnera161ab02008-11-29 09:09:48 +0000177 AliasAnalysis::AliasResult R =
178 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
179
180 if (R == AliasAnalysis::NoAlias)
181 continue;
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000182 return DepResultTy(Inst, Normal);
Chris Lattnera161ab02008-11-29 09:09:48 +0000183 }
184
185
186 // See if this instruction mod/ref's the pointer.
187 AliasAnalysis::ModRefResult MRR = AA.getModRefInfo(Inst, MemPtr, MemSize);
188
189 if (MRR == AliasAnalysis::NoModRef)
Chris Lattner25a08142008-11-29 08:51:16 +0000190 continue;
191
Chris Lattnera161ab02008-11-29 09:09:48 +0000192 // Loads don't depend on read-only instructions.
193 if (isa<LoadInst>(QueryInst) && MRR == AliasAnalysis::Ref)
Chris Lattner25a08142008-11-29 08:51:16 +0000194 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000195
196 // Otherwise, there is a dependence.
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000197 return DepResultTy(Inst, Normal);
Owen Anderson78e02f72007-07-06 23:14:35 +0000198 }
199
Chris Lattner5391a1d2008-11-29 03:47:00 +0000200 // If we found nothing, return the non-local flag.
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000201 return DepResultTy(0, NonLocal);
Owen Anderson78e02f72007-07-06 23:14:35 +0000202}
203
Chris Lattner5391a1d2008-11-29 03:47:00 +0000204/// getDependency - Return the instruction on which a memory operation
205/// depends.
206MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
207 Instruction *ScanPos = QueryInst;
208
209 // Check for a cached result
210 DepResultTy &LocalCache = LocalDeps[QueryInst];
211
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000212 // If the cached entry is non-dirty, just return it. Note that this depends
213 // on DepResultTy's default constructing to 'dirty'.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000214 if (LocalCache.getInt() != Dirty)
215 return ConvToResult(LocalCache);
216
217 // Otherwise, if we have a dirty entry, we know we can start the scan at that
218 // instruction, which may save us some work.
219 if (Instruction *Inst = LocalCache.getPointer())
220 ScanPos = Inst;
221
222 // Do the scan.
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000223 LocalCache = getDependencyFromInternal(QueryInst, ScanPos,
224 QueryInst->getParent());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000225
226 // Remember the result!
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000227 if (Instruction *I = LocalCache.getPointer())
Chris Lattner8c465272008-11-29 09:20:15 +0000228 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000229
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000230 return ConvToResult(LocalCache);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000231}
232
Chris Lattner37d041c2008-11-30 01:18:27 +0000233/// getNonLocalDependency - Perform a full dependency query for the
234/// specified instruction, returning the set of blocks that the value is
235/// potentially live across. The returned set of results will include a
236/// "NonLocal" result for all blocks where the value is live across.
237///
238/// This method assumes the instruction returns a "nonlocal" dependency
239/// within its own block.
240///
241void MemoryDependenceAnalysis::
242getNonLocalDependency(Instruction *QueryInst,
243 SmallVectorImpl<std::pair<BasicBlock*,
244 MemDepResult> > &Result) {
245 assert(getDependency(QueryInst).isNonLocal() &&
246 "getNonLocalDependency should only be used on insts with non-local deps!");
247 DenseMap<BasicBlock*, DepResultTy> &Cache = NonLocalDeps[QueryInst];
248
249 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
250 /// the cached case, this can happen due to instructions being deleted etc. In
251 /// the uncached case, this starts out as the set of predecessors we care
252 /// about.
253 SmallVector<BasicBlock*, 32> DirtyBlocks;
254
255 if (!Cache.empty()) {
256 // If we already have a partially computed set of results, scan them to
257 // determine what is dirty, seeding our initial DirtyBlocks worklist.
258 // FIXME: In the "don't need to be updated" case, this is expensive, why not
259 // have a per-"cache" flag saying it is undirty?
260 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
261 E = Cache.end(); I != E; ++I)
262 if (I->second.getInt() == Dirty)
263 DirtyBlocks.push_back(I->first);
264
265 NumCacheNonLocal++;
266
267 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
268 // << Cache.size() << " cached: " << *QueryInst;
269 } else {
270 // Seed DirtyBlocks with each of the preds of QueryInst's block.
271 BasicBlock *QueryBB = QueryInst->getParent();
272 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
273 NumUncacheNonLocal++;
274 }
275
276 // Iterate while we still have blocks to update.
277 while (!DirtyBlocks.empty()) {
278 BasicBlock *DirtyBB = DirtyBlocks.back();
279 DirtyBlocks.pop_back();
280
281 // Get the entry for this block. Note that this relies on DepResultTy
282 // default initializing to Dirty.
283 DepResultTy &DirtyBBEntry = Cache[DirtyBB];
284
285 // If DirtyBBEntry isn't dirty, it ended up on the worklist multiple times.
286 if (DirtyBBEntry.getInt() != Dirty) continue;
287
Chris Lattner37d041c2008-11-30 01:18:27 +0000288 // If the dirty entry has a pointer, start scanning from it so we don't have
289 // to rescan the entire block.
290 BasicBlock::iterator ScanPos = DirtyBB->end();
291 if (Instruction *Inst = DirtyBBEntry.getPointer())
292 ScanPos = Inst;
293
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000294 // Find out if this block has a local dependency for QueryInst.
295 DirtyBBEntry = getDependencyFromInternal(QueryInst, ScanPos, DirtyBB);
Chris Lattner37d041c2008-11-30 01:18:27 +0000296
297 // If the block has a dependency (i.e. it isn't completely transparent to
298 // the value), remember it!
299 if (DirtyBBEntry.getInt() != NonLocal) {
300 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
301 // update this when we remove instructions.
302 if (Instruction *Inst = DirtyBBEntry.getPointer())
303 ReverseNonLocalDeps[Inst].insert(QueryInst);
304 continue;
305 }
306
307 // If the block *is* completely transparent to the load, we need to check
308 // the predecessors of this block. Add them to our worklist.
309 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
310 }
311
312
313 // Copy the result into the output set.
314 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
315 E = Cache.end(); I != E; ++I)
316 Result.push_back(std::make_pair(I->first, ConvToResult(I->second)));
317}
318
Owen Anderson78e02f72007-07-06 23:14:35 +0000319/// removeInstruction - Remove an instruction from the dependence analysis,
320/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000321/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000322void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000323 // Walk through the Non-local dependencies, removing this one as the value
324 // for any cached queries.
Chris Lattner39f372e2008-11-29 01:43:36 +0000325 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner8c465272008-11-29 09:20:15 +0000326 NonLocalDeps[RemInst].begin(), DE = NonLocalDeps[RemInst].end();
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000327 DI != DE; ++DI)
Chris Lattner7f524222008-11-29 03:22:12 +0000328 if (Instruction *Inst = DI->second.getPointer())
Chris Lattner8c465272008-11-29 09:20:15 +0000329 ReverseNonLocalDeps[Inst].erase(RemInst);
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000330
Chris Lattner5f589dc2008-11-28 22:04:47 +0000331 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000332 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000333 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
334 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000335 // Remove us from DepInst's reverse set now that the local dep info is gone.
336 if (Instruction *Inst = LocalDepEntry->second.getPointer()) {
337 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
338 RLD.erase(RemInst);
339 if (RLD.empty())
340 ReverseLocalDeps.erase(Inst);
341 }
342
Chris Lattnerbaad8882008-11-28 22:28:27 +0000343 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000344 LocalDeps.erase(LocalDepEntry);
Chris Lattner125ce362008-11-30 01:09:30 +0000345 }
Chris Lattnerbaad8882008-11-28 22:28:27 +0000346
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000347 // Loop over all of the things that depend on the instruction we're removing.
348 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000349 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
350
Chris Lattner8c465272008-11-29 09:20:15 +0000351 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
352 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000353 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner125ce362008-11-30 01:09:30 +0000354 // RemInst can't be the terminator if it has stuff depending on it.
355 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
356 "Nothing can locally depend on a terminator");
357
358 // Anything that was locally dependent on RemInst is now going to be
359 // dependent on the instruction after RemInst. It will have the dirty flag
360 // set so it will rescan. This saves having to scan the entire block to get
361 // to this point.
362 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
363
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000364 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
365 E = ReverseDeps.end(); I != E; ++I) {
366 Instruction *InstDependingOnRemInst = *I;
367
368 // If we thought the instruction depended on itself (possible for
369 // unconfirmed dependencies) ignore the update.
370 if (InstDependingOnRemInst == RemInst) continue;
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();
394 I != E; ++I)
Chris Lattner8c465272008-11-29 09:20:15 +0000395 for (DenseMap<BasicBlock*, DepResultTy>::iterator
396 DI = NonLocalDeps[*I].begin(), DE = NonLocalDeps[*I].end();
Owen Andersonce4d88a2007-09-21 03:53:52 +0000397 DI != DE; ++DI)
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000398 if (DI->second.getPointer() == RemInst) {
399 // Convert to a dirty entry for the subsequent instruction.
400 DI->second.setInt(Dirty);
401 if (RemInst->isTerminator())
402 DI->second.setPointer(0);
403 else {
404 Instruction *NextI = next(BasicBlock::iterator(RemInst));
405 DI->second.setPointer(NextI);
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000406 assert(NextI != RemInst);
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000407 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
408 }
409 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000410
411 ReverseNonLocalDeps.erase(ReverseDepIt);
412
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000413 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
414 while (!ReverseDepsToAdd.empty()) {
415 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
416 .insert(ReverseDepsToAdd.back().second);
417 ReverseDepsToAdd.pop_back();
418 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000419 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000420
Chris Lattner8c465272008-11-29 09:20:15 +0000421 NonLocalDeps.erase(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000422 getAnalysis<AliasAnalysis>().deleteValue(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000423 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000424}
Chris Lattner729b2372008-11-29 21:25:10 +0000425
426/// verifyRemoved - Verify that the specified instruction does not occur
427/// in our internal data structures.
428void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
429 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
430 E = LocalDeps.end(); I != E; ++I) {
431 assert(I->first != D && "Inst occurs in data structures");
432 assert(I->second.getPointer() != D &&
433 "Inst occurs in data structures");
434 }
435
436 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
437 E = NonLocalDeps.end(); I != E; ++I) {
438 assert(I->first != D && "Inst occurs in data structures");
439 for (DenseMap<BasicBlock*, DepResultTy>::iterator II = I->second.begin(),
440 EE = I->second.end(); II != EE; ++II)
441 assert(II->second.getPointer() != D && "Inst occurs in data structures");
442 }
443
444 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
445 E = ReverseLocalDeps.end(); I != E; ++I)
446 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
447 EE = I->second.end(); II != EE; ++II)
448 assert(*II != D && "Inst occurs in data structures");
449
450 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
451 E = ReverseNonLocalDeps.end();
452 I != E; ++I)
453 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
454 EE = I->second.end(); II != EE; ++II)
455 assert(*II != D && "Inst occurs in data structures");
456}