blob: 01af42c86363c79a8e1983709e9f0522153e1b8b [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());
Chris Lattner00314b32008-11-29 09:15:21 +000066 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
67 Pointer = V->getOperand(0);
68 PointerSize = TD.getTypeStoreSize(V->getType());
69 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
70 Pointer = F->getPointerOperand();
Owen Anderson5f323202007-07-10 17:59:22 +000071
72 // FreeInsts erase the entire structure
Chris Lattner00314b32008-11-29 09:15:21 +000073 PointerSize = ~0UL;
74 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
75 if (AA.getModRefBehavior(CallSite::get(Inst)) ==
Chris Lattner5391a1d2008-11-29 03:47:00 +000076 AliasAnalysis::DoesNotAccessMemory)
Chris Lattner00314b32008-11-29 09:15:21 +000077 continue;
Chris Lattner73ec3cd2008-11-30 01:26:32 +000078 return DepResultTy(Inst, Normal);
Chris Lattnercfbb6342008-11-30 01:44:00 +000079 } else {
80 // Non-memory instruction.
Owen Anderson202da142007-07-10 20:39:07 +000081 continue;
Chris Lattnercfbb6342008-11-30 01:44:00 +000082 }
Owen Anderson5f323202007-07-10 17:59:22 +000083
Chris Lattner00314b32008-11-29 09:15:21 +000084 if (AA.getModRefInfo(C, Pointer, PointerSize) != AliasAnalysis::NoModRef)
Chris Lattner73ec3cd2008-11-30 01:26:32 +000085 return DepResultTy(Inst, Normal);
Owen Anderson5f323202007-07-10 17:59:22 +000086 }
87
Chris Lattner5391a1d2008-11-29 03:47:00 +000088 // No dependence found.
Chris Lattner73ec3cd2008-11-30 01:26:32 +000089 return DepResultTy(0, NonLocal);
Owen Anderson5f323202007-07-10 17:59:22 +000090}
91
Owen Anderson78e02f72007-07-06 23:14:35 +000092/// getDependency - Return the instruction on which a memory operation
Dan Gohmanc04575f2008-04-10 23:02:38 +000093/// depends. The local parameter indicates if the query should only
Owen Anderson6b278fc2007-07-10 17:08:11 +000094/// evaluate dependencies within the same basic block.
Chris Lattner73ec3cd2008-11-30 01:26:32 +000095MemoryDependenceAnalysis::DepResultTy MemoryDependenceAnalysis::
96getDependencyFromInternal(Instruction *QueryInst, BasicBlock::iterator ScanIt,
97 BasicBlock *BB) {
Chris Lattner5391a1d2008-11-29 03:47:00 +000098 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
99 TargetData &TD = getAnalysis<TargetData>();
Owen Anderson78e02f72007-07-06 23:14:35 +0000100
101 // 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();
108 MemSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
109 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();
112 MemSize = TD.getTypeStoreSize(L->getType());
113 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);
116 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();
141 uint64_t PointerSize = TD.getTypeStoreSize(L->getType());
142
143 // If we found a pointer, check if it could be the same as our pointer
144 AliasAnalysis::AliasResult R =
145 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
146
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 ||
164 AA.alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
165 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.
170 AliasAnalysis::ModRefResult MRR = AA.getModRefInfo(Inst, MemPtr, MemSize);
171
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.
202 if (Instruction *Inst = LocalCache.getPointer())
203 ScanPos = Inst;
204
205 // Do the scan.
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000206 LocalCache = getDependencyFromInternal(QueryInst, ScanPos,
207 QueryInst->getParent());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000208
209 // Remember the result!
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000210 if (Instruction *I = LocalCache.getPointer())
Chris Lattner8c465272008-11-29 09:20:15 +0000211 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000212
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000213 return ConvToResult(LocalCache);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000214}
215
Chris Lattner37d041c2008-11-30 01:18:27 +0000216/// getNonLocalDependency - Perform a full dependency query for the
217/// specified instruction, returning the set of blocks that the value is
218/// potentially live across. The returned set of results will include a
219/// "NonLocal" result for all blocks where the value is live across.
220///
221/// This method assumes the instruction returns a "nonlocal" dependency
222/// within its own block.
223///
224void MemoryDependenceAnalysis::
225getNonLocalDependency(Instruction *QueryInst,
226 SmallVectorImpl<std::pair<BasicBlock*,
227 MemDepResult> > &Result) {
228 assert(getDependency(QueryInst).isNonLocal() &&
229 "getNonLocalDependency should only be used on insts with non-local deps!");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000230 DenseMap<BasicBlock*, DepResultTy>* &CacheP = NonLocalDeps[QueryInst];
231 if (CacheP == 0) CacheP = new DenseMap<BasicBlock*, DepResultTy>();
232
233 DenseMap<BasicBlock*, DepResultTy> &Cache = *CacheP;
Chris Lattner37d041c2008-11-30 01:18:27 +0000234
235 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
236 /// the cached case, this can happen due to instructions being deleted etc. In
237 /// the uncached case, this starts out as the set of predecessors we care
238 /// about.
239 SmallVector<BasicBlock*, 32> DirtyBlocks;
240
241 if (!Cache.empty()) {
242 // If we already have a partially computed set of results, scan them to
243 // determine what is dirty, seeding our initial DirtyBlocks worklist.
244 // FIXME: In the "don't need to be updated" case, this is expensive, why not
245 // have a per-"cache" flag saying it is undirty?
246 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
247 E = Cache.end(); I != E; ++I)
248 if (I->second.getInt() == Dirty)
249 DirtyBlocks.push_back(I->first);
250
251 NumCacheNonLocal++;
252
253 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
254 // << Cache.size() << " cached: " << *QueryInst;
255 } else {
256 // Seed DirtyBlocks with each of the preds of QueryInst's block.
257 BasicBlock *QueryBB = QueryInst->getParent();
258 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
259 NumUncacheNonLocal++;
260 }
261
262 // Iterate while we still have blocks to update.
263 while (!DirtyBlocks.empty()) {
264 BasicBlock *DirtyBB = DirtyBlocks.back();
265 DirtyBlocks.pop_back();
266
267 // Get the entry for this block. Note that this relies on DepResultTy
268 // default initializing to Dirty.
269 DepResultTy &DirtyBBEntry = Cache[DirtyBB];
270
271 // If DirtyBBEntry isn't dirty, it ended up on the worklist multiple times.
272 if (DirtyBBEntry.getInt() != Dirty) continue;
273
Chris Lattner37d041c2008-11-30 01:18:27 +0000274 // If the dirty entry has a pointer, start scanning from it so we don't have
275 // to rescan the entire block.
276 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000277 if (Instruction *Inst = DirtyBBEntry.getPointer()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000278 ScanPos = Inst;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000279
280 // We're removing QueryInst's dependence on Inst.
281 SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst];
282 InstMap.erase(QueryInst);
283 if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst);
284 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000285
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000286 // Find out if this block has a local dependency for QueryInst.
287 DirtyBBEntry = getDependencyFromInternal(QueryInst, ScanPos, DirtyBB);
Chris Lattner37d041c2008-11-30 01:18:27 +0000288
289 // If the block has a dependency (i.e. it isn't completely transparent to
290 // the value), remember it!
291 if (DirtyBBEntry.getInt() != NonLocal) {
292 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
293 // update this when we remove instructions.
294 if (Instruction *Inst = DirtyBBEntry.getPointer())
295 ReverseNonLocalDeps[Inst].insert(QueryInst);
296 continue;
297 }
298
299 // If the block *is* completely transparent to the load, we need to check
300 // the predecessors of this block. Add them to our worklist.
301 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
302 }
303
304
305 // Copy the result into the output set.
306 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
307 E = Cache.end(); I != E; ++I)
308 Result.push_back(std::make_pair(I->first, ConvToResult(I->second)));
309}
310
Owen Anderson78e02f72007-07-06 23:14:35 +0000311/// removeInstruction - Remove an instruction from the dependence analysis,
312/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000313/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000314void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000315 // Walk through the Non-local dependencies, removing this one as the value
316 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +0000317 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
318 if (NLDI != NonLocalDeps.end()) {
319 DenseMap<BasicBlock*, DepResultTy> &BlockMap = *NLDI->second;
320 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
321 BlockMap.begin(), DE = BlockMap.end(); DI != DE; ++DI)
322 if (Instruction *Inst = DI->second.getPointer())
323 ReverseNonLocalDeps[Inst].erase(RemInst);
324 delete &BlockMap;
325 NonLocalDeps.erase(NLDI);
326 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000327
Chris Lattner5f589dc2008-11-28 22:04:47 +0000328 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000329 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000330 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
331 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000332 // Remove us from DepInst's reverse set now that the local dep info is gone.
333 if (Instruction *Inst = LocalDepEntry->second.getPointer()) {
334 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
335 RLD.erase(RemInst);
336 if (RLD.empty())
337 ReverseLocalDeps.erase(Inst);
338 }
339
Chris Lattnerbaad8882008-11-28 22:28:27 +0000340 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000341 LocalDeps.erase(LocalDepEntry);
Chris Lattner125ce362008-11-30 01:09:30 +0000342 }
Chris Lattnerbaad8882008-11-28 22:28:27 +0000343
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000344 // Loop over all of the things that depend on the instruction we're removing.
345 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000346 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
347
Chris Lattner8c465272008-11-29 09:20:15 +0000348 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
349 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000350 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner125ce362008-11-30 01:09:30 +0000351 // RemInst can't be the terminator if it has stuff depending on it.
352 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
353 "Nothing can locally depend on a terminator");
354
355 // Anything that was locally dependent on RemInst is now going to be
356 // dependent on the instruction after RemInst. It will have the dirty flag
357 // set so it will rescan. This saves having to scan the entire block to get
358 // to this point.
359 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
360
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000361 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
362 E = ReverseDeps.end(); I != E; ++I) {
363 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000364 assert(InstDependingOnRemInst != RemInst &&
365 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +0000366
367 LocalDeps[InstDependingOnRemInst] = DepResultTy(NewDepInst, Dirty);
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000368
Chris Lattner125ce362008-11-30 01:09:30 +0000369 // Make sure to remember that new things depend on NewDepInst.
370 ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
371 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000372 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000373
374 ReverseLocalDeps.erase(ReverseDepIt);
375
376 // Add new reverse deps after scanning the set, to avoid invalidating the
377 // 'ReverseDeps' reference.
378 while (!ReverseDepsToAdd.empty()) {
379 ReverseLocalDeps[ReverseDepsToAdd.back().first]
380 .insert(ReverseDepsToAdd.back().second);
381 ReverseDepsToAdd.pop_back();
382 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000383 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000384
Chris Lattner8c465272008-11-29 09:20:15 +0000385 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
386 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000387 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson4d13de42007-08-16 21:27:05 +0000388 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000389 I != E; ++I) {
390 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
391
392 DenseMap<BasicBlock*, DepResultTy> &INLD = *NonLocalDeps[*I];
393 assert(&INLD != 0 && "Reverse mapping out of date?");
394
Chris Lattner8c465272008-11-29 09:20:15 +0000395 for (DenseMap<BasicBlock*, DepResultTy>::iterator
Chris Lattnerf68f3102008-11-30 02:28:25 +0000396 DI = INLD.begin(), DE = INLD.end(); DI != DE; ++DI) {
397 if (DI->second.getPointer() != RemInst) continue;
398
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);
406 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000407 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000408 }
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 Lattnerf68f3102008-11-30 02:28:25 +0000421 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
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");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000439 DenseMap<BasicBlock*, DepResultTy> &INLD = *I->second;
440 for (DenseMap<BasicBlock*, DepResultTy>::iterator II = INLD.begin(),
441 EE = INLD.end(); II != EE; ++II)
Chris Lattner729b2372008-11-29 21:25:10 +0000442 assert(II->second.getPointer() != D && "Inst occurs in data structures");
443 }
444
445 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +0000446 E = ReverseLocalDeps.end(); I != E; ++I) {
447 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000448 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
449 EE = I->second.end(); II != EE; ++II)
450 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000451 }
Chris Lattner729b2372008-11-29 21:25:10 +0000452
453 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
454 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000455 I != E; ++I) {
456 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000457 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
458 EE = I->second.end(); II != EE; ++II)
459 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000460 }
Chris Lattner729b2372008-11-29 21:25:10 +0000461}