blob: 45ca83ab58fc9dbd891639b98b9be2a5228b1185 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Andersonafe840e2007-08-08 22:01:54 +000012// alias analysis information, and tries to provide a lazy, caching interface to
Dan Gohmanf17a25c2007-07-18 16:29:46 +000013// a common kind of alias information query.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner969470c2008-11-28 21:45:17 +000017#define DEBUG_TYPE "memdep"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Analysis/MemoryDependenceAnalysis.h"
19#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
21#include "llvm/Function.h"
22#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner52638032008-11-28 22:28:27 +000023#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/STLExtras.h"
Owen Anderson4c295472007-07-24 21:52:37 +000025#include "llvm/Support/CFG.h"
Tanya Lattner8edb2b72008-02-06 00:54:55 +000026#include "llvm/Support/CommandLine.h"
Chris Lattner969470c2008-11-28 21:45:17 +000027#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Target/TargetData.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029using namespace llvm;
30
Chris Lattner98a6d802008-11-29 22:02:15 +000031STATISTIC(NumCacheNonLocal, "Number of cached non-local responses");
32STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Owen Andersond6c7fea2007-09-09 21:43:49 +000033
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034char MemoryDependenceAnalysis::ID = 0;
35
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036// Register this pass...
37static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Chris Lattner969470c2008-11-28 21:45:17 +000038 "Memory Dependence Analysis", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Anderson3de3c532007-08-08 22:26:03 +000048/// getCallSiteDependency - Private helper for finding the local dependencies
49/// of a call site.
Chris Lattner12cafbf2008-11-29 02:29:27 +000050MemDepResult MemoryDependenceAnalysis::
Chris Lattnera5a36c12008-11-29 03:47:00 +000051getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
52 BasicBlock *BB) {
Chris Lattnercb53af02008-11-29 03:22:12 +000053 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
54 TargetData &TD = getAnalysis<TargetData>();
Owen Andersone84f4bc2007-08-07 00:33:45 +000055
Owen Anderson3de3c532007-08-08 22:26:03 +000056 // Walk backwards through the block, looking for dependencies
Chris Lattnera5a36c12008-11-29 03:47:00 +000057 while (ScanIt != BB->begin()) {
58 Instruction *Inst = --ScanIt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
60 // If this inst is a memory op, get the pointer it accessed
Chris Lattner18bd2452008-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 Lattnerade40a22008-11-29 21:22:42 +000069 // Use ABI size (size between elements), not store size (size of one
70 // element without padding).
Chris Lattner18bd2452008-11-29 09:15:21 +000071 PointerSize = C->getZExtValue() *
Chris Lattnerade40a22008-11-29 21:22:42 +000072 TD.getABITypeSize(AI->getAllocatedType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 else
Chris Lattner18bd2452008-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();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080
81 // FreeInsts erase the entire structure
Chris Lattner18bd2452008-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 Lattnera5a36c12008-11-29 03:47:00 +000085 AliasAnalysis::DoesNotAccessMemory)
Chris Lattner18bd2452008-11-29 09:15:21 +000086 continue;
87 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 } else
89 continue;
90
Chris Lattner18bd2452008-11-29 09:15:21 +000091 if (AA.getModRefInfo(C, Pointer, PointerSize) != AliasAnalysis::NoModRef)
Chris Lattnera5a36c12008-11-29 03:47:00 +000092 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 }
94
Chris Lattnera5a36c12008-11-29 03:47:00 +000095 // No dependence found.
Chris Lattner12cafbf2008-11-29 02:29:27 +000096 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097}
98
Chris Lattnerade40a22008-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 Lattner88adc8d2008-11-29 21:33:22 +0000107void MemoryDependenceAnalysis::
108getNonLocalDependency(Instruction *QueryInst,
109 SmallVectorImpl<std::pair<BasicBlock*,
110 MemDepResult> > &Result) {
Chris Lattnerade40a22008-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 Anderson4c295472007-07-24 21:52:37 +0000114
Chris Lattner98a6d802008-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 Lattnerade40a22008-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 Lattnerfd9b56d2008-11-29 01:43:36 +0000128 if (I->second.getInt() == Dirty)
Chris Lattnerade40a22008-11-29 21:22:42 +0000129 DirtyBlocks.push_back(I->first);
Owen Anderson05749072007-09-21 03:53:52 +0000130
Chris Lattner98a6d802008-11-29 22:02:15 +0000131 NumCacheNonLocal++;
132
133 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
134 // << Cache.size() << " cached: " << *QueryInst;
Chris Lattnerade40a22008-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 Lattner88adc8d2008-11-29 21:33:22 +0000138 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
Chris Lattner98a6d802008-11-29 22:02:15 +0000139 NumUncacheNonLocal++;
Chris Lattner12cafbf2008-11-29 02:29:27 +0000140 }
Chris Lattner75cbaf82008-11-29 23:30:39 +0000141
142
Chris Lattnerade40a22008-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 Lattner75cbaf82008-11-29 23:30:39 +0000151
Chris Lattnerade40a22008-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 Lattner75cbaf82008-11-29 23:30:39 +0000154
Chris Lattnerade40a22008-11-29 21:22:42 +0000155 // Find out if this block has a local dependency for QueryInst.
Chris Lattnerade40a22008-11-29 21:22:42 +0000156 // FIXME: Don't convert back and forth for MemDepResult <-> DepResultTy.
Chris Lattner98a6d802008-11-29 22:02:15 +0000157
158 // If the dirty entry has a pointer, start scanning from it so we don't have
159 // to rescan the entire block.
160 BasicBlock::iterator ScanPos = DirtyBB->end();
161 if (Instruction *Inst = DirtyBBEntry.getPointer())
162 ScanPos = Inst;
163
164 DirtyBBEntry = ConvFromResult(getDependencyFrom(QueryInst, ScanPos,
Chris Lattnerade40a22008-11-29 21:22:42 +0000165 DirtyBB));
Chris Lattner75cbaf82008-11-29 23:30:39 +0000166
Chris Lattnerade40a22008-11-29 21:22:42 +0000167 // If the block has a dependency (i.e. it isn't completely transparent to
168 // the value), remember it!
169 if (DirtyBBEntry.getInt() != NonLocal) {
170 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
Chris Lattner75cbaf82008-11-29 23:30:39 +0000171 // update this when we remove instructions.
Chris Lattnerade40a22008-11-29 21:22:42 +0000172 if (Instruction *Inst = DirtyBBEntry.getPointer())
173 ReverseNonLocalDeps[Inst].insert(QueryInst);
174 continue;
175 }
176
177 // If the block *is* completely transparent to the load, we need to check
178 // the predecessors of this block. Add them to our worklist.
Chris Lattner75cbaf82008-11-29 23:30:39 +0000179 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
Owen Anderson2bd46a52007-08-16 21:27:05 +0000180 }
Chris Lattnerade40a22008-11-29 21:22:42 +0000181
Chris Lattner75cbaf82008-11-29 23:30:39 +0000182
Chris Lattnerade40a22008-11-29 21:22:42 +0000183 // Copy the result into the output set.
184 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
185 E = Cache.end(); I != E; ++I)
Chris Lattner88adc8d2008-11-29 21:33:22 +0000186 Result.push_back(std::make_pair(I->first, ConvToResult(I->second)));
Owen Anderson4c295472007-07-24 21:52:37 +0000187}
188
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189/// getDependency - Return the instruction on which a memory operation
Dan Gohmanf1f99a22008-04-10 23:02:38 +0000190/// depends. The local parameter indicates if the query should only
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191/// evaluate dependencies within the same basic block.
Chris Lattnera5a36c12008-11-29 03:47:00 +0000192MemDepResult MemoryDependenceAnalysis::
193getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
194 BasicBlock *BB) {
195 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
196 TargetData &TD = getAnalysis<TargetData>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197
198 // Get the pointer value for which dependence will be determined
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000199 Value *MemPtr = 0;
200 uint64_t MemSize = 0;
201 bool MemVolatile = false;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000202
203 if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000204 MemPtr = S->getPointerOperand();
205 MemSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
206 MemVolatile = S->isVolatile();
Chris Lattnera5a36c12008-11-29 03:47:00 +0000207 } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000208 MemPtr = L->getPointerOperand();
209 MemSize = TD.getTypeStoreSize(L->getType());
210 MemVolatile = L->isVolatile();
Chris Lattnera5a36c12008-11-29 03:47:00 +0000211 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000212 MemPtr = V->getOperand(0);
213 MemSize = TD.getTypeStoreSize(V->getType());
Chris Lattnera5a36c12008-11-29 03:47:00 +0000214 } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000215 MemPtr = F->getPointerOperand();
216 // FreeInsts erase the entire structure, not just a field.
217 MemSize = ~0UL;
218 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst))
Chris Lattnera5a36c12008-11-29 03:47:00 +0000219 return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000220 else // Non-memory instructions depend on nothing.
Chris Lattner12cafbf2008-11-29 02:29:27 +0000221 return MemDepResult::getNone();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222
Owen Anderson3de3c532007-08-08 22:26:03 +0000223 // Walk backwards through the basic block, looking for dependencies
Chris Lattnera5a36c12008-11-29 03:47:00 +0000224 while (ScanIt != BB->begin()) {
225 Instruction *Inst = --ScanIt;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000226
227 // If the access is volatile and this is a volatile load/store, return a
228 // dependence.
229 if (MemVolatile &&
230 ((isa<LoadInst>(Inst) && cast<LoadInst>(Inst)->isVolatile()) ||
231 (isa<StoreInst>(Inst) && cast<StoreInst>(Inst)->isVolatile())))
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000232 return MemDepResult::get(Inst);
Chris Lattner4103c3c2008-11-29 09:09:48 +0000233
234 // MemDep is broken w.r.t. loads: it says that two loads of the same pointer
235 // depend on each other. :(
Chris Lattner4103c3c2008-11-29 09:09:48 +0000236 if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
237 Value *Pointer = L->getPointerOperand();
238 uint64_t PointerSize = TD.getTypeStoreSize(L->getType());
239
240 // If we found a pointer, check if it could be the same as our pointer
241 AliasAnalysis::AliasResult R =
242 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
243
244 if (R == AliasAnalysis::NoAlias)
245 continue;
246
247 // May-alias loads don't depend on each other without a dependence.
248 if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
249 continue;
250 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 }
252
Chris Lattner4103c3c2008-11-29 09:09:48 +0000253 // FIXME: This claims that an access depends on the allocation. This may
254 // make sense, but is dubious at best. It would be better to fix GVN to
255 // handle a 'None' Query.
256 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
257 Value *Pointer = AI;
258 uint64_t PointerSize;
259 if (ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattnerade40a22008-11-29 21:22:42 +0000260 // Use ABI size (size between elements), not store size (size of one
261 // element without padding).
Chris Lattner4103c3c2008-11-29 09:09:48 +0000262 PointerSize = C->getZExtValue() *
Chris Lattnerade40a22008-11-29 21:22:42 +0000263 TD.getABITypeSize(AI->getAllocatedType());
Chris Lattner4103c3c2008-11-29 09:09:48 +0000264 else
265 PointerSize = ~0UL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266
Chris Lattner4103c3c2008-11-29 09:09:48 +0000267 AliasAnalysis::AliasResult R =
268 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
269
270 if (R == AliasAnalysis::NoAlias)
271 continue;
272 return MemDepResult::get(Inst);
273 }
274
275
276 // See if this instruction mod/ref's the pointer.
277 AliasAnalysis::ModRefResult MRR = AA.getModRefInfo(Inst, MemPtr, MemSize);
278
279 if (MRR == AliasAnalysis::NoModRef)
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000280 continue;
281
Chris Lattner4103c3c2008-11-29 09:09:48 +0000282 // Loads don't depend on read-only instructions.
283 if (isa<LoadInst>(QueryInst) && MRR == AliasAnalysis::Ref)
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000284 continue;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000285
286 // Otherwise, there is a dependence.
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000287 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 }
289
Chris Lattnera5a36c12008-11-29 03:47:00 +0000290 // If we found nothing, return the non-local flag.
Chris Lattner12cafbf2008-11-29 02:29:27 +0000291 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292}
293
Chris Lattnera5a36c12008-11-29 03:47:00 +0000294/// getDependency - Return the instruction on which a memory operation
295/// depends.
296MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
297 Instruction *ScanPos = QueryInst;
298
299 // Check for a cached result
300 DepResultTy &LocalCache = LocalDeps[QueryInst];
301
Chris Lattner98a6d802008-11-29 22:02:15 +0000302 // If the cached entry is non-dirty, just return it. Note that this depends
303 // on DepResultTy's default constructing to 'dirty'.
Chris Lattnera5a36c12008-11-29 03:47:00 +0000304 if (LocalCache.getInt() != Dirty)
305 return ConvToResult(LocalCache);
306
307 // Otherwise, if we have a dirty entry, we know we can start the scan at that
308 // instruction, which may save us some work.
309 if (Instruction *Inst = LocalCache.getPointer())
310 ScanPos = Inst;
311
312 // Do the scan.
313 MemDepResult Res =
314 getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
315
316 // Remember the result!
317 // FIXME: Don't convert back and forth! Make a shared helper function.
318 LocalCache = ConvFromResult(Res);
319 if (Instruction *I = Res.getInst())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000320 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattnera5a36c12008-11-29 03:47:00 +0000321
322 return Res;
323}
324
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325/// removeInstruction - Remove an instruction from the dependence analysis,
326/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000327/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner1b185de2008-11-28 22:04:47 +0000328void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner1b185de2008-11-28 22:04:47 +0000329 // Walk through the Non-local dependencies, removing this one as the value
330 // for any cached queries.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000331 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000332 NonLocalDeps[RemInst].begin(), DE = NonLocalDeps[RemInst].end();
Owen Andersonc772be72007-12-08 01:37:09 +0000333 DI != DE; ++DI)
Chris Lattnercb53af02008-11-29 03:22:12 +0000334 if (Instruction *Inst = DI->second.getPointer())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000335 ReverseNonLocalDeps[Inst].erase(RemInst);
Owen Andersonc772be72007-12-08 01:37:09 +0000336
Chris Lattner1b185de2008-11-28 22:04:47 +0000337 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattner52638032008-11-28 22:28:27 +0000338 //
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000339 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
340 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000341 // Remove us from DepInst's reverse set now that the local dep info is gone.
342 if (Instruction *Inst = LocalDepEntry->second.getPointer()) {
343 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
344 RLD.erase(RemInst);
345 if (RLD.empty())
346 ReverseLocalDeps.erase(Inst);
347 }
348
Chris Lattner52638032008-11-28 22:28:27 +0000349 // Remove this local dependency info.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000350 LocalDeps.erase(LocalDepEntry);
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000351 }
Chris Lattner52638032008-11-28 22:28:27 +0000352
Chris Lattner89fbbe72008-11-28 22:51:08 +0000353 // Loop over all of the things that depend on the instruction we're removing.
354 //
Chris Lattner75cbaf82008-11-29 23:30:39 +0000355 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
356
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000357 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
358 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000359 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000360 // RemInst can't be the terminator if it has stuff depending on it.
361 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
362 "Nothing can locally depend on a terminator");
363
364 // Anything that was locally dependent on RemInst is now going to be
365 // dependent on the instruction after RemInst. It will have the dirty flag
366 // set so it will rescan. This saves having to scan the entire block to get
367 // to this point.
368 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
369
Chris Lattner89fbbe72008-11-28 22:51:08 +0000370 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
371 E = ReverseDeps.end(); I != E; ++I) {
372 Instruction *InstDependingOnRemInst = *I;
373
374 // If we thought the instruction depended on itself (possible for
375 // unconfirmed dependencies) ignore the update.
376 if (InstDependingOnRemInst == RemInst) continue;
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000377
378 LocalDeps[InstDependingOnRemInst] = DepResultTy(NewDepInst, Dirty);
Chris Lattner89fbbe72008-11-28 22:51:08 +0000379
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000380 // Make sure to remember that new things depend on NewDepInst.
381 ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
382 InstDependingOnRemInst));
Chris Lattner89fbbe72008-11-28 22:51:08 +0000383 }
Chris Lattner75cbaf82008-11-29 23:30:39 +0000384
385 ReverseLocalDeps.erase(ReverseDepIt);
386
387 // Add new reverse deps after scanning the set, to avoid invalidating the
388 // 'ReverseDeps' reference.
389 while (!ReverseDepsToAdd.empty()) {
390 ReverseLocalDeps[ReverseDepsToAdd.back().first]
391 .insert(ReverseDepsToAdd.back().second);
392 ReverseDepsToAdd.pop_back();
393 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000395
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000396 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
397 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000398 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000399 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
400 I != E; ++I)
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000401 for (DenseMap<BasicBlock*, DepResultTy>::iterator
402 DI = NonLocalDeps[*I].begin(), DE = NonLocalDeps[*I].end();
Owen Anderson05749072007-09-21 03:53:52 +0000403 DI != DE; ++DI)
Chris Lattner98a6d802008-11-29 22:02:15 +0000404 if (DI->second.getPointer() == RemInst) {
405 // Convert to a dirty entry for the subsequent instruction.
406 DI->second.setInt(Dirty);
407 if (RemInst->isTerminator())
408 DI->second.setPointer(0);
409 else {
410 Instruction *NextI = next(BasicBlock::iterator(RemInst));
411 DI->second.setPointer(NextI);
Chris Lattner75cbaf82008-11-29 23:30:39 +0000412 assert(NextI != RemInst);
Chris Lattner98a6d802008-11-29 22:02:15 +0000413 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
414 }
415 }
Chris Lattner75cbaf82008-11-29 23:30:39 +0000416
417 ReverseNonLocalDeps.erase(ReverseDepIt);
418
Chris Lattner98a6d802008-11-29 22:02:15 +0000419 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
420 while (!ReverseDepsToAdd.empty()) {
421 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
422 .insert(ReverseDepsToAdd.back().second);
423 ReverseDepsToAdd.pop_back();
424 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000425 }
Owen Andersonc772be72007-12-08 01:37:09 +0000426
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000427 NonLocalDeps.erase(RemInst);
Chris Lattner1b185de2008-11-28 22:04:47 +0000428 getAnalysis<AliasAnalysis>().deleteValue(RemInst);
Chris Lattner1b185de2008-11-28 22:04:47 +0000429 DEBUG(verifyRemoved(RemInst));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430}
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000431
432/// verifyRemoved - Verify that the specified instruction does not occur
433/// in our internal data structures.
434void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
435 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
436 E = LocalDeps.end(); I != E; ++I) {
437 assert(I->first != D && "Inst occurs in data structures");
438 assert(I->second.getPointer() != D &&
439 "Inst occurs in data structures");
440 }
441
442 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
443 E = NonLocalDeps.end(); I != E; ++I) {
444 assert(I->first != D && "Inst occurs in data structures");
445 for (DenseMap<BasicBlock*, DepResultTy>::iterator II = I->second.begin(),
446 EE = I->second.end(); II != EE; ++II)
447 assert(II->second.getPointer() != D && "Inst occurs in data structures");
448 }
449
450 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
451 E = ReverseLocalDeps.end(); I != E; ++I)
452 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
453 EE = I->second.end(); II != EE; ++II)
454 assert(*II != D && "Inst occurs in data structures");
455
456 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
457 E = ReverseNonLocalDeps.end();
458 I != E; ++I)
459 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
460 EE = I->second.end(); II != EE; ++II)
461 assert(*II != D && "Inst occurs in data structures");
462}