blob: f59be5ec03be1c7bf6a969b5fad2c5f77d359247 [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
Owen Anderson7fad7e32007-09-09 21:43:49 +000031STATISTIC(NumCacheNonlocal, "Number of cached non-local responses");
32STATISTIC(NumUncacheNonlocal, "Number of uncached non-local responses");
33
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///
107void MemoryDependenceAnalysis::getNonLocalDependency(Instruction *QueryInst,
108 DenseMap<BasicBlock*, MemDepResult> &Result) {
109 assert(getDependency(QueryInst).isNonLocal() &&
110 "getNonLocalDependency should only be used on insts with non-local deps!");
111 DenseMap<BasicBlock*, DepResultTy> &Cache = NonLocalDeps[QueryInst];
Owen Anderson4beedbd2007-07-24 21:52:37 +0000112
Chris Lattner86b29ef2008-11-29 21:22:42 +0000113 /// DirtyBlocks - This is the set of blocks that need to be recomputed. This
114 /// can happen due to instructions being deleted etc.
115 SmallVector<BasicBlock*, 32> DirtyBlocks;
116
117 if (!Cache.empty()) {
118 // If we already have a partially computed set of results, scan them to
119 // determine what is dirty, seeding our initial DirtyBlocks worklist.
120 // FIXME: In the "don't need to be updated" case, this is expensive, why not
121 // have a per-"cache" flag saying it is undirty?
122 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
123 E = Cache.end(); I != E; ++I)
Chris Lattner39f372e2008-11-29 01:43:36 +0000124 if (I->second.getInt() == Dirty)
Chris Lattner86b29ef2008-11-29 21:22:42 +0000125 DirtyBlocks.push_back(I->first);
Owen Andersonce4d88a2007-09-21 03:53:52 +0000126
Chris Lattner86b29ef2008-11-29 21:22:42 +0000127 NumCacheNonlocal++;
128 } else {
129 // Seed DirtyBlocks with each of the preds of QueryInst's block.
130 BasicBlock *QueryBB = QueryInst->getParent();
131 // FIXME: use range insertion/append.
132 for (pred_iterator PI = pred_begin(QueryBB), E = pred_end(QueryBB);
133 PI != E; ++PI)
134 DirtyBlocks.push_back(*PI);
135 NumUncacheNonlocal++;
Chris Lattner4c724002008-11-29 02:29:27 +0000136 }
Chris Lattner4c724002008-11-29 02:29:27 +0000137
Chris Lattner86b29ef2008-11-29 21:22:42 +0000138 // Iterate while we still have blocks to update.
139 while (!DirtyBlocks.empty()) {
140 BasicBlock *DirtyBB = DirtyBlocks.back();
141 DirtyBlocks.pop_back();
142
143 // Get the entry for this block. Note that this relies on DepResultTy
144 // default initializing to Dirty.
145 DepResultTy &DirtyBBEntry = Cache[DirtyBB];
146
147 // If DirtyBBEntry isn't dirty, it ended up on the worklist multiple times.
148 if (DirtyBBEntry.getInt() != Dirty) continue;
149
150 // Find out if this block has a local dependency for QueryInst.
151 // FIXME: If the dirty entry has an instruction pointer, scan from it!
152 // FIXME: Don't convert back and forth for MemDepResult <-> DepResultTy.
153 DirtyBBEntry = ConvFromResult(getDependencyFrom(QueryInst, DirtyBB->end(),
154 DirtyBB));
155
156 // If the block has a dependency (i.e. it isn't completely transparent to
157 // the value), remember it!
158 if (DirtyBBEntry.getInt() != NonLocal) {
159 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
160 // update this when we remove instructions.
161 if (Instruction *Inst = DirtyBBEntry.getPointer())
162 ReverseNonLocalDeps[Inst].insert(QueryInst);
163 continue;
164 }
165
166 // If the block *is* completely transparent to the load, we need to check
167 // the predecessors of this block. Add them to our worklist.
168 for (pred_iterator I = pred_begin(DirtyBB), E = pred_end(DirtyBB);
169 I != E; ++I)
170 DirtyBlocks.push_back(*I);
Owen Anderson4d13de42007-08-16 21:27:05 +0000171 }
Chris Lattner86b29ef2008-11-29 21:22:42 +0000172
173 // Copy the result into the output set.
174 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
175 E = Cache.end(); I != E; ++I)
176 Result[I->first] = ConvToResult(I->second);
Owen Anderson4beedbd2007-07-24 21:52:37 +0000177}
178
Owen Anderson78e02f72007-07-06 23:14:35 +0000179/// getDependency - Return the instruction on which a memory operation
Dan Gohmanc04575f2008-04-10 23:02:38 +0000180/// depends. The local parameter indicates if the query should only
Owen Anderson6b278fc2007-07-10 17:08:11 +0000181/// evaluate dependencies within the same basic block.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000182MemDepResult MemoryDependenceAnalysis::
183getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
184 BasicBlock *BB) {
185 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
186 TargetData &TD = getAnalysis<TargetData>();
Owen Anderson78e02f72007-07-06 23:14:35 +0000187
188 // Get the pointer value for which dependence will be determined
Chris Lattner25a08142008-11-29 08:51:16 +0000189 Value *MemPtr = 0;
190 uint64_t MemSize = 0;
191 bool MemVolatile = false;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000192
193 if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000194 MemPtr = S->getPointerOperand();
195 MemSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
196 MemVolatile = S->isVolatile();
Chris Lattner5391a1d2008-11-29 03:47:00 +0000197 } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000198 MemPtr = L->getPointerOperand();
199 MemSize = TD.getTypeStoreSize(L->getType());
200 MemVolatile = L->isVolatile();
Chris Lattner5391a1d2008-11-29 03:47:00 +0000201 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000202 MemPtr = V->getOperand(0);
203 MemSize = TD.getTypeStoreSize(V->getType());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000204 } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
Chris Lattner25a08142008-11-29 08:51:16 +0000205 MemPtr = F->getPointerOperand();
206 // FreeInsts erase the entire structure, not just a field.
207 MemSize = ~0UL;
208 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst))
Chris Lattner5391a1d2008-11-29 03:47:00 +0000209 return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
Chris Lattner25a08142008-11-29 08:51:16 +0000210 else // Non-memory instructions depend on nothing.
Chris Lattner4c724002008-11-29 02:29:27 +0000211 return MemDepResult::getNone();
Owen Anderson78e02f72007-07-06 23:14:35 +0000212
Owen Anderson642a9e32007-08-08 22:26:03 +0000213 // Walk backwards through the basic block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000214 while (ScanIt != BB->begin()) {
215 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000216
217 // If the access is volatile and this is a volatile load/store, return a
218 // dependence.
219 if (MemVolatile &&
220 ((isa<LoadInst>(Inst) && cast<LoadInst>(Inst)->isVolatile()) ||
221 (isa<StoreInst>(Inst) && cast<StoreInst>(Inst)->isVolatile())))
Chris Lattner25a08142008-11-29 08:51:16 +0000222 return MemDepResult::get(Inst);
Chris Lattnera161ab02008-11-29 09:09:48 +0000223
224 // MemDep is broken w.r.t. loads: it says that two loads of the same pointer
225 // depend on each other. :(
226 // FIXME: ELIMINATE THIS!
227 if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
228 Value *Pointer = L->getPointerOperand();
229 uint64_t PointerSize = TD.getTypeStoreSize(L->getType());
230
231 // If we found a pointer, check if it could be the same as our pointer
232 AliasAnalysis::AliasResult R =
233 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
234
235 if (R == AliasAnalysis::NoAlias)
236 continue;
237
238 // May-alias loads don't depend on each other without a dependence.
239 if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
240 continue;
241 return MemDepResult::get(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000242 }
243
Chris Lattnera161ab02008-11-29 09:09:48 +0000244 // FIXME: This claims that an access depends on the allocation. This may
245 // make sense, but is dubious at best. It would be better to fix GVN to
246 // handle a 'None' Query.
247 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
248 Value *Pointer = AI;
249 uint64_t PointerSize;
250 if (ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattner86b29ef2008-11-29 21:22:42 +0000251 // Use ABI size (size between elements), not store size (size of one
252 // element without padding).
Chris Lattnera161ab02008-11-29 09:09:48 +0000253 PointerSize = C->getZExtValue() *
Chris Lattner86b29ef2008-11-29 21:22:42 +0000254 TD.getABITypeSize(AI->getAllocatedType());
Chris Lattnera161ab02008-11-29 09:09:48 +0000255 else
256 PointerSize = ~0UL;
Owen Anderson78e02f72007-07-06 23:14:35 +0000257
Chris Lattnera161ab02008-11-29 09:09:48 +0000258 AliasAnalysis::AliasResult R =
259 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
260
261 if (R == AliasAnalysis::NoAlias)
262 continue;
263 return MemDepResult::get(Inst);
264 }
265
266
267 // See if this instruction mod/ref's the pointer.
268 AliasAnalysis::ModRefResult MRR = AA.getModRefInfo(Inst, MemPtr, MemSize);
269
270 if (MRR == AliasAnalysis::NoModRef)
Chris Lattner25a08142008-11-29 08:51:16 +0000271 continue;
272
Chris Lattnera161ab02008-11-29 09:09:48 +0000273 // Loads don't depend on read-only instructions.
274 if (isa<LoadInst>(QueryInst) && MRR == AliasAnalysis::Ref)
Chris Lattner25a08142008-11-29 08:51:16 +0000275 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000276
277 // Otherwise, there is a dependence.
Chris Lattner25a08142008-11-29 08:51:16 +0000278 return MemDepResult::get(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000279 }
280
Chris Lattner5391a1d2008-11-29 03:47:00 +0000281 // If we found nothing, return the non-local flag.
Chris Lattner4c724002008-11-29 02:29:27 +0000282 return MemDepResult::getNonLocal();
Owen Anderson78e02f72007-07-06 23:14:35 +0000283}
284
Chris Lattner5391a1d2008-11-29 03:47:00 +0000285/// getDependency - Return the instruction on which a memory operation
286/// depends.
287MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
288 Instruction *ScanPos = QueryInst;
289
290 // Check for a cached result
291 DepResultTy &LocalCache = LocalDeps[QueryInst];
292
293 // If the cached entry is non-dirty, just return it.
294 if (LocalCache.getInt() != Dirty)
295 return ConvToResult(LocalCache);
296
297 // Otherwise, if we have a dirty entry, we know we can start the scan at that
298 // instruction, which may save us some work.
299 if (Instruction *Inst = LocalCache.getPointer())
300 ScanPos = Inst;
301
302 // Do the scan.
303 MemDepResult Res =
304 getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
305
306 // Remember the result!
307 // FIXME: Don't convert back and forth! Make a shared helper function.
308 LocalCache = ConvFromResult(Res);
309 if (Instruction *I = Res.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000310 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000311
312 return Res;
313}
314
315
Owen Anderson30b4bd42008-02-12 21:15:18 +0000316/// dropInstruction - Remove an instruction from the analysis, making
317/// absolutely conservative assumptions when updating the cache. This is
318/// useful, for example when an instruction is changed rather than removed.
319void MemoryDependenceAnalysis::dropInstruction(Instruction* drop) {
Chris Lattner39f372e2008-11-29 01:43:36 +0000320 LocalDepMapType::iterator depGraphEntry = LocalDeps.find(drop);
321 if (depGraphEntry != LocalDeps.end())
Chris Lattner7f524222008-11-29 03:22:12 +0000322 if (Instruction *Inst = depGraphEntry->second.getPointer())
Chris Lattner8c465272008-11-29 09:20:15 +0000323 ReverseLocalDeps[Inst].erase(drop);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000324
325 // Drop dependency information for things that depended on this instr
Chris Lattner8c465272008-11-29 09:20:15 +0000326 SmallPtrSet<Instruction*, 4>& set = ReverseLocalDeps[drop];
Owen Anderson30b4bd42008-02-12 21:15:18 +0000327 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
328 I != E; ++I)
Chris Lattner39f372e2008-11-29 01:43:36 +0000329 LocalDeps.erase(*I);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000330
Chris Lattner39f372e2008-11-29 01:43:36 +0000331 LocalDeps.erase(drop);
Chris Lattner8c465272008-11-29 09:20:15 +0000332 ReverseLocalDeps.erase(drop);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000333
Chris Lattner39f372e2008-11-29 01:43:36 +0000334 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner8c465272008-11-29 09:20:15 +0000335 NonLocalDeps[drop].begin(), DE = NonLocalDeps[drop].end();
Owen Anderson30b4bd42008-02-12 21:15:18 +0000336 DI != DE; ++DI)
Chris Lattner7f524222008-11-29 03:22:12 +0000337 if (Instruction *Inst = DI->second.getPointer())
Chris Lattner8c465272008-11-29 09:20:15 +0000338 ReverseNonLocalDeps[Inst].erase(drop);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000339
Chris Lattner8c465272008-11-29 09:20:15 +0000340 if (ReverseNonLocalDeps.count(drop)) {
Chris Lattner39f372e2008-11-29 01:43:36 +0000341 SmallPtrSet<Instruction*, 4>& set =
Chris Lattner8c465272008-11-29 09:20:15 +0000342 ReverseNonLocalDeps[drop];
Owen Anderson30b4bd42008-02-12 21:15:18 +0000343 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
344 I != E; ++I)
Chris Lattner39f372e2008-11-29 01:43:36 +0000345 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner8c465272008-11-29 09:20:15 +0000346 NonLocalDeps[*I].begin(), DE = NonLocalDeps[*I].end();
Owen Anderson30b4bd42008-02-12 21:15:18 +0000347 DI != DE; ++DI)
Chris Lattner39f372e2008-11-29 01:43:36 +0000348 if (DI->second == DepResultTy(drop, Normal))
Chris Lattner7f524222008-11-29 03:22:12 +0000349 // FIXME: Why not remember the old insertion point??
Chris Lattner39f372e2008-11-29 01:43:36 +0000350 DI->second = DepResultTy(0, Dirty);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000351 }
352
Chris Lattner8c465272008-11-29 09:20:15 +0000353 ReverseNonLocalDeps.erase(drop);
354 NonLocalDeps.erase(drop);
Owen Anderson30b4bd42008-02-12 21:15:18 +0000355}
356
Owen Anderson78e02f72007-07-06 23:14:35 +0000357/// removeInstruction - Remove an instruction from the dependence analysis,
358/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000359/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000360void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000361 // Walk through the Non-local dependencies, removing this one as the value
362 // for any cached queries.
Chris Lattner39f372e2008-11-29 01:43:36 +0000363 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner8c465272008-11-29 09:20:15 +0000364 NonLocalDeps[RemInst].begin(), DE = NonLocalDeps[RemInst].end();
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000365 DI != DE; ++DI)
Chris Lattner7f524222008-11-29 03:22:12 +0000366 if (Instruction *Inst = DI->second.getPointer())
Chris Lattner8c465272008-11-29 09:20:15 +0000367 ReverseNonLocalDeps[Inst].erase(RemInst);
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000368
Chris Lattnerbaad8882008-11-28 22:28:27 +0000369 // Shortly after this, we will look for things that depend on RemInst. In
370 // order to update these, we'll need a new dependency to base them on. We
371 // could completely delete any entries that depend on this, but it is better
372 // to make a more accurate approximation where possible. Compute that better
373 // approximation if we can.
Chris Lattner39f372e2008-11-29 01:43:36 +0000374 DepResultTy NewDependency;
Chris Lattnerbaad8882008-11-28 22:28:27 +0000375
Chris Lattner5f589dc2008-11-28 22:04:47 +0000376 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000377 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000378 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
379 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner7f524222008-11-29 03:22:12 +0000380 DepResultTy LocalDep = LocalDepEntry->second;
Owen Anderson9a8ff8c2008-01-30 01:24:05 +0000381
Chris Lattnerbaad8882008-11-28 22:28:27 +0000382 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000383 LocalDeps.erase(LocalDepEntry);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000384
Chris Lattnerbaad8882008-11-28 22:28:27 +0000385 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattner7f524222008-11-29 03:22:12 +0000386 if (Instruction *Inst = LocalDep.getPointer())
Chris Lattner8c465272008-11-29 09:20:15 +0000387 ReverseLocalDeps[Inst].erase(RemInst);
Chris Lattnerbaad8882008-11-28 22:28:27 +0000388
389 // If we have unconfirmed info, don't trust it.
Chris Lattner7f524222008-11-29 03:22:12 +0000390 if (LocalDep.getInt() != Dirty) {
Chris Lattnerbaad8882008-11-28 22:28:27 +0000391 // If we have a confirmed non-local flag, use it.
Chris Lattner39f372e2008-11-29 01:43:36 +0000392 if (LocalDep.getInt() == NonLocal || LocalDep.getInt() == None) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000393 // The only time this dependency is confirmed is if it is non-local.
Chris Lattner39f372e2008-11-29 01:43:36 +0000394 NewDependency = LocalDep;
Chris Lattnerbaad8882008-11-28 22:28:27 +0000395 } else {
396 // If we have dep info for RemInst, set them to it.
Chris Lattner39f372e2008-11-29 01:43:36 +0000397 Instruction *NDI = next(BasicBlock::iterator(LocalDep.getPointer()));
398 if (NDI != RemInst) // Don't use RemInst for the new dependency!
Chris Lattner7f524222008-11-29 03:22:12 +0000399 NewDependency = DepResultTy(NDI, Dirty);
Chris Lattnerbaad8882008-11-28 22:28:27 +0000400 }
David Greenedf464192007-07-31 20:01:27 +0000401 }
Owen Andersona8701a62008-02-05 04:34:03 +0000402 }
403
Chris Lattnerbaad8882008-11-28 22:28:27 +0000404 // If we don't already have a local dependency answer for this instruction,
405 // use the immediate successor of RemInst. We use the successor because
406 // getDependence starts by checking the immediate predecessor of what is in
407 // the cache.
Chris Lattner7f524222008-11-29 03:22:12 +0000408 if (NewDependency == DepResultTy(0, Dirty))
409 NewDependency = DepResultTy(next(BasicBlock::iterator(RemInst)), Dirty);
Chris Lattnerbaad8882008-11-28 22:28:27 +0000410
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000411 // Loop over all of the things that depend on the instruction we're removing.
412 //
Chris Lattner8c465272008-11-29 09:20:15 +0000413 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
414 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000415 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
416 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
417 E = ReverseDeps.end(); I != E; ++I) {
418 Instruction *InstDependingOnRemInst = *I;
419
420 // If we thought the instruction depended on itself (possible for
421 // unconfirmed dependencies) ignore the update.
422 if (InstDependingOnRemInst == RemInst) continue;
423
424 // Insert the new dependencies.
Chris Lattner7f524222008-11-29 03:22:12 +0000425 LocalDeps[InstDependingOnRemInst] = NewDependency;
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000426
427 // If our NewDependency is an instruction, make sure to remember that new
428 // things depend on it.
Chris Lattner7f524222008-11-29 03:22:12 +0000429 if (Instruction *Inst = NewDependency.getPointer())
Chris Lattner8c465272008-11-29 09:20:15 +0000430 ReverseLocalDeps[Inst].insert(InstDependingOnRemInst);
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000431 }
Chris Lattner8c465272008-11-29 09:20:15 +0000432 ReverseLocalDeps.erase(RemInst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000433 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000434
Chris Lattner8c465272008-11-29 09:20:15 +0000435 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
436 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000437 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson4d13de42007-08-16 21:27:05 +0000438 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
439 I != E; ++I)
Chris Lattner8c465272008-11-29 09:20:15 +0000440 for (DenseMap<BasicBlock*, DepResultTy>::iterator
441 DI = NonLocalDeps[*I].begin(), DE = NonLocalDeps[*I].end();
Owen Andersonce4d88a2007-09-21 03:53:52 +0000442 DI != DE; ++DI)
Chris Lattner39f372e2008-11-29 01:43:36 +0000443 if (DI->second == DepResultTy(RemInst, Normal))
Chris Lattner7f524222008-11-29 03:22:12 +0000444 // FIXME: Why not remember the old insertion point??
Chris Lattner39f372e2008-11-29 01:43:36 +0000445 DI->second = DepResultTy(0, Dirty);
Chris Lattner8c465272008-11-29 09:20:15 +0000446 ReverseNonLocalDeps.erase(ReverseDepIt);
Owen Anderson4d13de42007-08-16 21:27:05 +0000447 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000448
Chris Lattner8c465272008-11-29 09:20:15 +0000449 NonLocalDeps.erase(RemInst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000450
Chris Lattner5f589dc2008-11-28 22:04:47 +0000451 getAnalysis<AliasAnalysis>().deleteValue(RemInst);
Chris Lattner0e575f42008-11-28 21:45:17 +0000452
Chris Lattner5f589dc2008-11-28 22:04:47 +0000453 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000454}
Chris Lattner729b2372008-11-29 21:25:10 +0000455
456/// verifyRemoved - Verify that the specified instruction does not occur
457/// in our internal data structures.
458void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
459 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
460 E = LocalDeps.end(); I != E; ++I) {
461 assert(I->first != D && "Inst occurs in data structures");
462 assert(I->second.getPointer() != D &&
463 "Inst occurs in data structures");
464 }
465
466 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
467 E = NonLocalDeps.end(); I != E; ++I) {
468 assert(I->first != D && "Inst occurs in data structures");
469 for (DenseMap<BasicBlock*, DepResultTy>::iterator II = I->second.begin(),
470 EE = I->second.end(); II != EE; ++II)
471 assert(II->second.getPointer() != D && "Inst occurs in data structures");
472 }
473
474 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
475 E = ReverseLocalDeps.end(); I != E; ++I)
476 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
477 EE = I->second.end(); II != EE; ++II)
478 assert(*II != D && "Inst occurs in data structures");
479
480 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
481 E = ReverseNonLocalDeps.end();
482 I != E; ++I)
483 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
484 EE = I->second.end(); II != EE; ++II)
485 assert(*II != D && "Inst occurs in data structures");
486}