blob: ef4440fbe767d4395a710d27772224f3c6a5c7b0 [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 Lattner4c724002008-11-29 02:29:27 +000050MemDepResult 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;
87 return MemDepResult::get(Inst);
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 Lattner5391a1d2008-11-29 03:47:00 +000092 return MemDepResult::get(Inst);
Owen Anderson5f323202007-07-10 17:59:22 +000093 }
94
Chris Lattner5391a1d2008-11-29 03:47:00 +000095 // No dependence found.
Chris Lattner4c724002008-11-29 02:29:27 +000096 return MemDepResult::getNonLocal();
Owen Anderson5f323202007-07-10 17:59:22 +000097}
98
Chris Lattner86b29ef2008-11-29 21:22:42 +000099/// getNonLocalDependency - Perform a full dependency query for the
100/// specified instruction, returning the set of blocks that the value is
101/// potentially live across. The returned set of results will include a
102/// "NonLocal" result for all blocks where the value is live across.
103///
104/// This method assumes the instruction returns a "nonlocal" dependency
105/// within its own block.
106///
Chris Lattner396a4a52008-11-29 21:33:22 +0000107void MemoryDependenceAnalysis::
108getNonLocalDependency(Instruction *QueryInst,
109 SmallVectorImpl<std::pair<BasicBlock*,
110 MemDepResult> > &Result) {
Chris Lattner86b29ef2008-11-29 21:22:42 +0000111 assert(getDependency(QueryInst).isNonLocal() &&
112 "getNonLocalDependency should only be used on insts with non-local deps!");
113 DenseMap<BasicBlock*, DepResultTy> &Cache = NonLocalDeps[QueryInst];
Owen Anderson4beedbd2007-07-24 21:52:37 +0000114
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000115 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
116 /// the cached case, this can happen due to instructions being deleted etc. In
117 /// the uncached case, this starts out as the set of predecessors we care
118 /// about.
Chris Lattner86b29ef2008-11-29 21:22:42 +0000119 SmallVector<BasicBlock*, 32> DirtyBlocks;
120
121 if (!Cache.empty()) {
122 // If we already have a partially computed set of results, scan them to
123 // determine what is dirty, seeding our initial DirtyBlocks worklist.
124 // FIXME: In the "don't need to be updated" case, this is expensive, why not
125 // have a per-"cache" flag saying it is undirty?
126 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
127 E = Cache.end(); I != E; ++I)
Chris Lattner39f372e2008-11-29 01:43:36 +0000128 if (I->second.getInt() == Dirty)
Chris Lattner86b29ef2008-11-29 21:22:42 +0000129 DirtyBlocks.push_back(I->first);
Owen Andersonce4d88a2007-09-21 03:53:52 +0000130
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000131 NumCacheNonLocal++;
132
133 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
134 // << Cache.size() << " cached: " << *QueryInst;
Chris Lattner86b29ef2008-11-29 21:22:42 +0000135 } else {
136 // Seed DirtyBlocks with each of the preds of QueryInst's block.
137 BasicBlock *QueryBB = QueryInst->getParent();
Chris Lattner396a4a52008-11-29 21:33:22 +0000138 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000139 NumUncacheNonLocal++;
Chris Lattner4c724002008-11-29 02:29:27 +0000140 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000141
142
Chris Lattner86b29ef2008-11-29 21:22:42 +0000143 // Iterate while we still have blocks to update.
144 while (!DirtyBlocks.empty()) {
145 BasicBlock *DirtyBB = DirtyBlocks.back();
146 DirtyBlocks.pop_back();
147
148 // Get the entry for this block. Note that this relies on DepResultTy
149 // default initializing to Dirty.
150 DepResultTy &DirtyBBEntry = Cache[DirtyBB];
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000151
Chris Lattner86b29ef2008-11-29 21:22:42 +0000152 // If DirtyBBEntry isn't dirty, it ended up on the worklist multiple times.
153 if (DirtyBBEntry.getInt() != Dirty) continue;
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000154
Chris Lattner86b29ef2008-11-29 21:22:42 +0000155 // Find out if this block has a local dependency for QueryInst.
156 // FIXME: If the dirty entry has an instruction pointer, scan from it!
157 // FIXME: Don't convert back and forth for MemDepResult <-> DepResultTy.
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000158
159 // If the dirty entry has a pointer, start scanning from it so we don't have
160 // to rescan the entire block.
161 BasicBlock::iterator ScanPos = DirtyBB->end();
162 if (Instruction *Inst = DirtyBBEntry.getPointer())
163 ScanPos = Inst;
164
165 DirtyBBEntry = ConvFromResult(getDependencyFrom(QueryInst, ScanPos,
Chris Lattner86b29ef2008-11-29 21:22:42 +0000166 DirtyBB));
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000167
Chris Lattner86b29ef2008-11-29 21:22:42 +0000168 // If the block has a dependency (i.e. it isn't completely transparent to
169 // the value), remember it!
170 if (DirtyBBEntry.getInt() != NonLocal) {
171 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000172 // update this when we remove instructions.
Chris Lattner86b29ef2008-11-29 21:22:42 +0000173 if (Instruction *Inst = DirtyBBEntry.getPointer())
174 ReverseNonLocalDeps[Inst].insert(QueryInst);
175 continue;
176 }
177
178 // If the block *is* completely transparent to the load, we need to check
179 // the predecessors of this block. Add them to our worklist.
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000180 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
Owen Anderson4d13de42007-08-16 21:27:05 +0000181 }
Chris Lattner86b29ef2008-11-29 21:22:42 +0000182
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000183
Chris Lattner86b29ef2008-11-29 21:22:42 +0000184 // Copy the result into the output set.
185 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
186 E = Cache.end(); I != E; ++I)
Chris Lattner396a4a52008-11-29 21:33:22 +0000187 Result.push_back(std::make_pair(I->first, ConvToResult(I->second)));
Owen Anderson4beedbd2007-07-24 21:52:37 +0000188}
189
Owen Anderson78e02f72007-07-06 23:14:35 +0000190/// getDependency - Return the instruction on which a memory operation
Dan Gohmanc04575f2008-04-10 23:02:38 +0000191/// depends. The local parameter indicates if the query should only
Owen Anderson6b278fc2007-07-10 17:08:11 +0000192/// evaluate dependencies within the same basic block.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000193MemDepResult MemoryDependenceAnalysis::
194getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
195 BasicBlock *BB) {
196 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
197 TargetData &TD = getAnalysis<TargetData>();
Owen Anderson78e02f72007-07-06 23:14:35 +0000198
199 // Get the pointer value for which dependence will be determined
Chris Lattner25a08142008-11-29 08:51:16 +0000200 Value *MemPtr = 0;
201 uint64_t MemSize = 0;
202 bool MemVolatile = false;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000203
204 if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000205 MemPtr = S->getPointerOperand();
206 MemSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
207 MemVolatile = S->isVolatile();
Chris Lattner5391a1d2008-11-29 03:47:00 +0000208 } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000209 MemPtr = L->getPointerOperand();
210 MemSize = TD.getTypeStoreSize(L->getType());
211 MemVolatile = L->isVolatile();
Chris Lattner5391a1d2008-11-29 03:47:00 +0000212 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000213 MemPtr = V->getOperand(0);
214 MemSize = TD.getTypeStoreSize(V->getType());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000215 } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000216 MemPtr = F->getPointerOperand();
217 // FreeInsts erase the entire structure, not just a field.
218 MemSize = ~0UL;
219 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst))
Chris Lattner5391a1d2008-11-29 03:47:00 +0000220 return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
Chris Lattner25a08142008-11-29 08:51:16 +0000221 else // Non-memory instructions depend on nothing.
Chris Lattner4c724002008-11-29 02:29:27 +0000222 return MemDepResult::getNone();
Owen Anderson78e02f72007-07-06 23:14:35 +0000223
Owen Anderson642a9e32007-08-08 22:26:03 +0000224 // Walk backwards through the basic block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000225 while (ScanIt != BB->begin()) {
226 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000227
228 // If the access is volatile and this is a volatile load/store, return a
229 // dependence.
230 if (MemVolatile &&
231 ((isa<LoadInst>(Inst) && cast<LoadInst>(Inst)->isVolatile()) ||
232 (isa<StoreInst>(Inst) && cast<StoreInst>(Inst)->isVolatile())))
Chris Lattner25a08142008-11-29 08:51:16 +0000233 return MemDepResult::get(Inst);
Chris Lattnera161ab02008-11-29 09:09:48 +0000234
235 // MemDep is broken w.r.t. loads: it says that two loads of the same pointer
236 // depend on each other. :(
237 // FIXME: ELIMINATE THIS!
238 if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
239 Value *Pointer = L->getPointerOperand();
240 uint64_t PointerSize = TD.getTypeStoreSize(L->getType());
241
242 // If we found a pointer, check if it could be the same as our pointer
243 AliasAnalysis::AliasResult R =
244 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
245
246 if (R == AliasAnalysis::NoAlias)
247 continue;
248
249 // May-alias loads don't depend on each other without a dependence.
250 if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
251 continue;
252 return MemDepResult::get(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000253 }
254
Chris Lattnera161ab02008-11-29 09:09:48 +0000255 // FIXME: This claims that an access depends on the allocation. This may
256 // make sense, but is dubious at best. It would be better to fix GVN to
257 // handle a 'None' Query.
258 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
259 Value *Pointer = AI;
260 uint64_t PointerSize;
261 if (ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner86b29ef2008-11-29 21:22:42 +0000262 // Use ABI size (size between elements), not store size (size of one
263 // element without padding).
Chris Lattnera161ab02008-11-29 09:09:48 +0000264 PointerSize = C->getZExtValue() *
Chris Lattner86b29ef2008-11-29 21:22:42 +0000265 TD.getABITypeSize(AI->getAllocatedType());
Chris Lattnera161ab02008-11-29 09:09:48 +0000266 else
267 PointerSize = ~0UL;
Owen Anderson78e02f72007-07-06 23:14:35 +0000268
Chris Lattnera161ab02008-11-29 09:09:48 +0000269 AliasAnalysis::AliasResult R =
270 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
271
272 if (R == AliasAnalysis::NoAlias)
273 continue;
274 return MemDepResult::get(Inst);
275 }
276
277
278 // See if this instruction mod/ref's the pointer.
279 AliasAnalysis::ModRefResult MRR = AA.getModRefInfo(Inst, MemPtr, MemSize);
280
281 if (MRR == AliasAnalysis::NoModRef)
Chris Lattner25a08142008-11-29 08:51:16 +0000282 continue;
283
Chris Lattnera161ab02008-11-29 09:09:48 +0000284 // Loads don't depend on read-only instructions.
285 if (isa<LoadInst>(QueryInst) && MRR == AliasAnalysis::Ref)
Chris Lattner25a08142008-11-29 08:51:16 +0000286 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000287
288 // Otherwise, there is a dependence.
Chris Lattner25a08142008-11-29 08:51:16 +0000289 return MemDepResult::get(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000290 }
291
Chris Lattner5391a1d2008-11-29 03:47:00 +0000292 // If we found nothing, return the non-local flag.
Chris Lattner4c724002008-11-29 02:29:27 +0000293 return MemDepResult::getNonLocal();
Owen Anderson78e02f72007-07-06 23:14:35 +0000294}
295
Chris Lattner5391a1d2008-11-29 03:47:00 +0000296/// getDependency - Return the instruction on which a memory operation
297/// depends.
298MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
299 Instruction *ScanPos = QueryInst;
300
301 // Check for a cached result
302 DepResultTy &LocalCache = LocalDeps[QueryInst];
303
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000304 // If the cached entry is non-dirty, just return it. Note that this depends
305 // on DepResultTy's default constructing to 'dirty'.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000306 if (LocalCache.getInt() != Dirty)
307 return ConvToResult(LocalCache);
308
309 // Otherwise, if we have a dirty entry, we know we can start the scan at that
310 // instruction, which may save us some work.
311 if (Instruction *Inst = LocalCache.getPointer())
312 ScanPos = Inst;
313
314 // Do the scan.
315 MemDepResult Res =
316 getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
317
318 // Remember the result!
319 // FIXME: Don't convert back and forth! Make a shared helper function.
320 LocalCache = ConvFromResult(Res);
321 if (Instruction *I = Res.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000322 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000323
324 return Res;
325}
326
Owen Anderson78e02f72007-07-06 23:14:35 +0000327/// removeInstruction - Remove an instruction from the dependence analysis,
328/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000329/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000330void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000331 // Walk through the Non-local dependencies, removing this one as the value
332 // for any cached queries.
Chris Lattner39f372e2008-11-29 01:43:36 +0000333 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner8c465272008-11-29 09:20:15 +0000334 NonLocalDeps[RemInst].begin(), DE = NonLocalDeps[RemInst].end();
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000335 DI != DE; ++DI)
Chris Lattner7f524222008-11-29 03:22:12 +0000336 if (Instruction *Inst = DI->second.getPointer())
Chris Lattner8c465272008-11-29 09:20:15 +0000337 ReverseNonLocalDeps[Inst].erase(RemInst);
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000338
Chris Lattner5f589dc2008-11-28 22:04:47 +0000339 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000340 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000341 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
342 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000343 // Remove us from DepInst's reverse set now that the local dep info is gone.
344 if (Instruction *Inst = LocalDepEntry->second.getPointer()) {
345 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
346 RLD.erase(RemInst);
347 if (RLD.empty())
348 ReverseLocalDeps.erase(Inst);
349 }
350
Chris Lattnerbaad8882008-11-28 22:28:27 +0000351 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000352 LocalDeps.erase(LocalDepEntry);
Chris Lattner125ce362008-11-30 01:09:30 +0000353 }
Chris Lattnerbaad8882008-11-28 22:28:27 +0000354
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000355 // Loop over all of the things that depend on the instruction we're removing.
356 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000357 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
358
Chris Lattner8c465272008-11-29 09:20:15 +0000359 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
360 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000361 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner125ce362008-11-30 01:09:30 +0000362 // RemInst can't be the terminator if it has stuff depending on it.
363 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
364 "Nothing can locally depend on a terminator");
365
366 // Anything that was locally dependent on RemInst is now going to be
367 // dependent on the instruction after RemInst. It will have the dirty flag
368 // set so it will rescan. This saves having to scan the entire block to get
369 // to this point.
370 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
371
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000372 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
373 E = ReverseDeps.end(); I != E; ++I) {
374 Instruction *InstDependingOnRemInst = *I;
375
376 // If we thought the instruction depended on itself (possible for
377 // unconfirmed dependencies) ignore the update.
378 if (InstDependingOnRemInst == RemInst) continue;
Chris Lattner125ce362008-11-30 01:09:30 +0000379
380 LocalDeps[InstDependingOnRemInst] = DepResultTy(NewDepInst, Dirty);
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000381
Chris Lattner125ce362008-11-30 01:09:30 +0000382 // Make sure to remember that new things depend on NewDepInst.
383 ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
384 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000385 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000386
387 ReverseLocalDeps.erase(ReverseDepIt);
388
389 // Add new reverse deps after scanning the set, to avoid invalidating the
390 // 'ReverseDeps' reference.
391 while (!ReverseDepsToAdd.empty()) {
392 ReverseLocalDeps[ReverseDepsToAdd.back().first]
393 .insert(ReverseDepsToAdd.back().second);
394 ReverseDepsToAdd.pop_back();
395 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000396 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000397
Chris Lattner8c465272008-11-29 09:20:15 +0000398 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
399 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000400 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson4d13de42007-08-16 21:27:05 +0000401 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
402 I != E; ++I)
Chris Lattner8c465272008-11-29 09:20:15 +0000403 for (DenseMap<BasicBlock*, DepResultTy>::iterator
404 DI = NonLocalDeps[*I].begin(), DE = NonLocalDeps[*I].end();
Owen Andersonce4d88a2007-09-21 03:53:52 +0000405 DI != DE; ++DI)
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000406 if (DI->second.getPointer() == RemInst) {
407 // Convert to a dirty entry for the subsequent instruction.
408 DI->second.setInt(Dirty);
409 if (RemInst->isTerminator())
410 DI->second.setPointer(0);
411 else {
412 Instruction *NextI = next(BasicBlock::iterator(RemInst));
413 DI->second.setPointer(NextI);
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000414 assert(NextI != RemInst);
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000415 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
416 }
417 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000418
419 ReverseNonLocalDeps.erase(ReverseDepIt);
420
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000421 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
422 while (!ReverseDepsToAdd.empty()) {
423 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
424 .insert(ReverseDepsToAdd.back().second);
425 ReverseDepsToAdd.pop_back();
426 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000427 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000428
Chris Lattner8c465272008-11-29 09:20:15 +0000429 NonLocalDeps.erase(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000430 getAnalysis<AliasAnalysis>().deleteValue(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000431 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000432}
Chris Lattner729b2372008-11-29 21:25:10 +0000433
434/// verifyRemoved - Verify that the specified instruction does not occur
435/// in our internal data structures.
436void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
437 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
438 E = LocalDeps.end(); I != E; ++I) {
439 assert(I->first != D && "Inst occurs in data structures");
440 assert(I->second.getPointer() != D &&
441 "Inst occurs in data structures");
442 }
443
444 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
445 E = NonLocalDeps.end(); I != E; ++I) {
446 assert(I->first != D && "Inst occurs in data structures");
447 for (DenseMap<BasicBlock*, DepResultTy>::iterator II = I->second.begin(),
448 EE = I->second.end(); II != EE; ++II)
449 assert(II->second.getPointer() != D && "Inst occurs in data structures");
450 }
451
452 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
453 E = ReverseLocalDeps.end(); I != E; ++I)
454 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
455 EE = I->second.end(); II != EE; ++II)
456 assert(*II != D && "Inst occurs in data structures");
457
458 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
459 E = ReverseNonLocalDeps.end();
460 I != E; ++I)
461 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
462 EE = I->second.end(); II != EE; ++II)
463 assert(*II != D && "Inst occurs in data structures");
464}