blob: 099d43492cdfead7520690abfa58ca81ec9cb8b1 [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
Owen Andersond6c7fea2007-09-09 21:43:49 +000031STATISTIC(NumCacheNonlocal, "Number of cached non-local responses");
32STATISTIC(NumUncacheNonlocal, "Number of uncached non-local responses");
33
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 Lattnerade40a22008-11-29 21:22:42 +0000115 /// DirtyBlocks - This is the set of blocks that need to be recomputed. This
116 /// can happen due to instructions being deleted etc.
117 SmallVector<BasicBlock*, 32> DirtyBlocks;
118
119 if (!Cache.empty()) {
120 // If we already have a partially computed set of results, scan them to
121 // determine what is dirty, seeding our initial DirtyBlocks worklist.
122 // FIXME: In the "don't need to be updated" case, this is expensive, why not
123 // have a per-"cache" flag saying it is undirty?
124 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
125 E = Cache.end(); I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000126 if (I->second.getInt() == Dirty)
Chris Lattnerade40a22008-11-29 21:22:42 +0000127 DirtyBlocks.push_back(I->first);
Owen Anderson05749072007-09-21 03:53:52 +0000128
Chris Lattnerade40a22008-11-29 21:22:42 +0000129 NumCacheNonlocal++;
130 } else {
131 // Seed DirtyBlocks with each of the preds of QueryInst's block.
132 BasicBlock *QueryBB = QueryInst->getParent();
Chris Lattner88adc8d2008-11-29 21:33:22 +0000133 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
Chris Lattnerade40a22008-11-29 21:22:42 +0000134 NumUncacheNonlocal++;
Chris Lattner12cafbf2008-11-29 02:29:27 +0000135 }
Chris Lattner12cafbf2008-11-29 02:29:27 +0000136
Chris Lattnerade40a22008-11-29 21:22:42 +0000137 // Iterate while we still have blocks to update.
138 while (!DirtyBlocks.empty()) {
139 BasicBlock *DirtyBB = DirtyBlocks.back();
140 DirtyBlocks.pop_back();
141
142 // Get the entry for this block. Note that this relies on DepResultTy
143 // default initializing to Dirty.
144 DepResultTy &DirtyBBEntry = Cache[DirtyBB];
145
146 // If DirtyBBEntry isn't dirty, it ended up on the worklist multiple times.
147 if (DirtyBBEntry.getInt() != Dirty) continue;
148
149 // Find out if this block has a local dependency for QueryInst.
150 // FIXME: If the dirty entry has an instruction pointer, scan from it!
151 // FIXME: Don't convert back and forth for MemDepResult <-> DepResultTy.
152 DirtyBBEntry = ConvFromResult(getDependencyFrom(QueryInst, DirtyBB->end(),
153 DirtyBB));
154
155 // If the block has a dependency (i.e. it isn't completely transparent to
156 // the value), remember it!
157 if (DirtyBBEntry.getInt() != NonLocal) {
158 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
159 // update this when we remove instructions.
160 if (Instruction *Inst = DirtyBBEntry.getPointer())
161 ReverseNonLocalDeps[Inst].insert(QueryInst);
162 continue;
163 }
164
165 // If the block *is* completely transparent to the load, we need to check
166 // the predecessors of this block. Add them to our worklist.
167 for (pred_iterator I = pred_begin(DirtyBB), E = pred_end(DirtyBB);
168 I != E; ++I)
169 DirtyBlocks.push_back(*I);
Owen Anderson2bd46a52007-08-16 21:27:05 +0000170 }
Chris Lattnerade40a22008-11-29 21:22:42 +0000171
172 // Copy the result into the output set.
173 for (DenseMap<BasicBlock*, DepResultTy>::iterator I = Cache.begin(),
174 E = Cache.end(); I != E; ++I)
Chris Lattner88adc8d2008-11-29 21:33:22 +0000175 Result.push_back(std::make_pair(I->first, ConvToResult(I->second)));
Owen Anderson4c295472007-07-24 21:52:37 +0000176}
177
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178/// getDependency - Return the instruction on which a memory operation
Dan Gohmanf1f99a22008-04-10 23:02:38 +0000179/// depends. The local parameter indicates if the query should only
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180/// evaluate dependencies within the same basic block.
Chris Lattnera5a36c12008-11-29 03:47:00 +0000181MemDepResult MemoryDependenceAnalysis::
182getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
183 BasicBlock *BB) {
184 AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
185 TargetData &TD = getAnalysis<TargetData>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186
187 // Get the pointer value for which dependence will be determined
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000188 Value *MemPtr = 0;
189 uint64_t MemSize = 0;
190 bool MemVolatile = false;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000191
192 if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000193 MemPtr = S->getPointerOperand();
194 MemSize = TD.getTypeStoreSize(S->getOperand(0)->getType());
195 MemVolatile = S->isVolatile();
Chris Lattnera5a36c12008-11-29 03:47:00 +0000196 } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000197 MemPtr = L->getPointerOperand();
198 MemSize = TD.getTypeStoreSize(L->getType());
199 MemVolatile = L->isVolatile();
Chris Lattnera5a36c12008-11-29 03:47:00 +0000200 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000201 MemPtr = V->getOperand(0);
202 MemSize = TD.getTypeStoreSize(V->getType());
Chris Lattnera5a36c12008-11-29 03:47:00 +0000203 } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000204 MemPtr = F->getPointerOperand();
205 // FreeInsts erase the entire structure, not just a field.
206 MemSize = ~0UL;
207 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst))
Chris Lattnera5a36c12008-11-29 03:47:00 +0000208 return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000209 else // Non-memory instructions depend on nothing.
Chris Lattner12cafbf2008-11-29 02:29:27 +0000210 return MemDepResult::getNone();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211
Owen Anderson3de3c532007-08-08 22:26:03 +0000212 // Walk backwards through the basic block, looking for dependencies
Chris Lattnera5a36c12008-11-29 03:47:00 +0000213 while (ScanIt != BB->begin()) {
214 Instruction *Inst = --ScanIt;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000215
216 // If the access is volatile and this is a volatile load/store, return a
217 // dependence.
218 if (MemVolatile &&
219 ((isa<LoadInst>(Inst) && cast<LoadInst>(Inst)->isVolatile()) ||
220 (isa<StoreInst>(Inst) && cast<StoreInst>(Inst)->isVolatile())))
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000221 return MemDepResult::get(Inst);
Chris Lattner4103c3c2008-11-29 09:09:48 +0000222
223 // MemDep is broken w.r.t. loads: it says that two loads of the same pointer
224 // depend on each other. :(
225 // FIXME: ELIMINATE THIS!
226 if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
227 Value *Pointer = L->getPointerOperand();
228 uint64_t PointerSize = TD.getTypeStoreSize(L->getType());
229
230 // If we found a pointer, check if it could be the same as our pointer
231 AliasAnalysis::AliasResult R =
232 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
233
234 if (R == AliasAnalysis::NoAlias)
235 continue;
236
237 // May-alias loads don't depend on each other without a dependence.
238 if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
239 continue;
240 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 }
242
Chris Lattner4103c3c2008-11-29 09:09:48 +0000243 // FIXME: This claims that an access depends on the allocation. This may
244 // make sense, but is dubious at best. It would be better to fix GVN to
245 // handle a 'None' Query.
246 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
247 Value *Pointer = AI;
248 uint64_t PointerSize;
249 if (ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize()))
Chris Lattnerade40a22008-11-29 21:22:42 +0000250 // Use ABI size (size between elements), not store size (size of one
251 // element without padding).
Chris Lattner4103c3c2008-11-29 09:09:48 +0000252 PointerSize = C->getZExtValue() *
Chris Lattnerade40a22008-11-29 21:22:42 +0000253 TD.getABITypeSize(AI->getAllocatedType());
Chris Lattner4103c3c2008-11-29 09:09:48 +0000254 else
255 PointerSize = ~0UL;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256
Chris Lattner4103c3c2008-11-29 09:09:48 +0000257 AliasAnalysis::AliasResult R =
258 AA.alias(Pointer, PointerSize, MemPtr, MemSize);
259
260 if (R == AliasAnalysis::NoAlias)
261 continue;
262 return MemDepResult::get(Inst);
263 }
264
265
266 // See if this instruction mod/ref's the pointer.
267 AliasAnalysis::ModRefResult MRR = AA.getModRefInfo(Inst, MemPtr, MemSize);
268
269 if (MRR == AliasAnalysis::NoModRef)
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000270 continue;
271
Chris Lattner4103c3c2008-11-29 09:09:48 +0000272 // Loads don't depend on read-only instructions.
273 if (isa<LoadInst>(QueryInst) && MRR == AliasAnalysis::Ref)
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000274 continue;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000275
276 // Otherwise, there is a dependence.
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000277 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 }
279
Chris Lattnera5a36c12008-11-29 03:47:00 +0000280 // If we found nothing, return the non-local flag.
Chris Lattner12cafbf2008-11-29 02:29:27 +0000281 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282}
283
Chris Lattnera5a36c12008-11-29 03:47:00 +0000284/// getDependency - Return the instruction on which a memory operation
285/// depends.
286MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
287 Instruction *ScanPos = QueryInst;
288
289 // Check for a cached result
290 DepResultTy &LocalCache = LocalDeps[QueryInst];
291
292 // If the cached entry is non-dirty, just return it.
293 if (LocalCache.getInt() != Dirty)
294 return ConvToResult(LocalCache);
295
296 // Otherwise, if we have a dirty entry, we know we can start the scan at that
297 // instruction, which may save us some work.
298 if (Instruction *Inst = LocalCache.getPointer())
299 ScanPos = Inst;
300
301 // Do the scan.
302 MemDepResult Res =
303 getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
304
305 // Remember the result!
306 // FIXME: Don't convert back and forth! Make a shared helper function.
307 LocalCache = ConvFromResult(Res);
308 if (Instruction *I = Res.getInst())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000309 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattnera5a36c12008-11-29 03:47:00 +0000310
311 return Res;
312}
313
314
Owen Anderson8d272d52008-02-12 21:15:18 +0000315/// dropInstruction - Remove an instruction from the analysis, making
316/// absolutely conservative assumptions when updating the cache. This is
317/// useful, for example when an instruction is changed rather than removed.
318void MemoryDependenceAnalysis::dropInstruction(Instruction* drop) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000319 LocalDepMapType::iterator depGraphEntry = LocalDeps.find(drop);
320 if (depGraphEntry != LocalDeps.end())
Chris Lattnercb53af02008-11-29 03:22:12 +0000321 if (Instruction *Inst = depGraphEntry->second.getPointer())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000322 ReverseLocalDeps[Inst].erase(drop);
Owen Anderson8d272d52008-02-12 21:15:18 +0000323
324 // Drop dependency information for things that depended on this instr
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000325 SmallPtrSet<Instruction*, 4>& set = ReverseLocalDeps[drop];
Owen Anderson8d272d52008-02-12 21:15:18 +0000326 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
327 I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000328 LocalDeps.erase(*I);
Owen Anderson8d272d52008-02-12 21:15:18 +0000329
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000330 LocalDeps.erase(drop);
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000331 ReverseLocalDeps.erase(drop);
Owen Anderson8d272d52008-02-12 21:15:18 +0000332
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000333 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000334 NonLocalDeps[drop].begin(), DE = NonLocalDeps[drop].end();
Owen Anderson8d272d52008-02-12 21:15:18 +0000335 DI != DE; ++DI)
Chris Lattnercb53af02008-11-29 03:22:12 +0000336 if (Instruction *Inst = DI->second.getPointer())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000337 ReverseNonLocalDeps[Inst].erase(drop);
Owen Anderson8d272d52008-02-12 21:15:18 +0000338
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000339 if (ReverseNonLocalDeps.count(drop)) {
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000340 SmallPtrSet<Instruction*, 4>& set =
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000341 ReverseNonLocalDeps[drop];
Owen Anderson8d272d52008-02-12 21:15:18 +0000342 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
343 I != E; ++I)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000344 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000345 NonLocalDeps[*I].begin(), DE = NonLocalDeps[*I].end();
Owen Anderson8d272d52008-02-12 21:15:18 +0000346 DI != DE; ++DI)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000347 if (DI->second == DepResultTy(drop, Normal))
Chris Lattnercb53af02008-11-29 03:22:12 +0000348 // FIXME: Why not remember the old insertion point??
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000349 DI->second = DepResultTy(0, Dirty);
Owen Anderson8d272d52008-02-12 21:15:18 +0000350 }
351
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000352 ReverseNonLocalDeps.erase(drop);
353 NonLocalDeps.erase(drop);
Owen Anderson8d272d52008-02-12 21:15:18 +0000354}
355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356/// removeInstruction - Remove an instruction from the dependence analysis,
357/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000358/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner1b185de2008-11-28 22:04:47 +0000359void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner1b185de2008-11-28 22:04:47 +0000360 // Walk through the Non-local dependencies, removing this one as the value
361 // for any cached queries.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000362 for (DenseMap<BasicBlock*, DepResultTy>::iterator DI =
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000363 NonLocalDeps[RemInst].begin(), DE = NonLocalDeps[RemInst].end();
Owen Andersonc772be72007-12-08 01:37:09 +0000364 DI != DE; ++DI)
Chris Lattnercb53af02008-11-29 03:22:12 +0000365 if (Instruction *Inst = DI->second.getPointer())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000366 ReverseNonLocalDeps[Inst].erase(RemInst);
Owen Andersonc772be72007-12-08 01:37:09 +0000367
Chris Lattner52638032008-11-28 22:28:27 +0000368 // Shortly after this, we will look for things that depend on RemInst. In
369 // order to update these, we'll need a new dependency to base them on. We
370 // could completely delete any entries that depend on this, but it is better
371 // to make a more accurate approximation where possible. Compute that better
372 // approximation if we can.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000373 DepResultTy NewDependency;
Chris Lattner52638032008-11-28 22:28:27 +0000374
Chris Lattner1b185de2008-11-28 22:04:47 +0000375 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattner52638032008-11-28 22:28:27 +0000376 //
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000377 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
378 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattnercb53af02008-11-29 03:22:12 +0000379 DepResultTy LocalDep = LocalDepEntry->second;
Owen Anderson25296a22008-01-30 01:24:05 +0000380
Chris Lattner52638032008-11-28 22:28:27 +0000381 // Remove this local dependency info.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000382 LocalDeps.erase(LocalDepEntry);
Chris Lattner1b185de2008-11-28 22:04:47 +0000383
Chris Lattner52638032008-11-28 22:28:27 +0000384 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnercb53af02008-11-29 03:22:12 +0000385 if (Instruction *Inst = LocalDep.getPointer())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000386 ReverseLocalDeps[Inst].erase(RemInst);
Chris Lattner52638032008-11-28 22:28:27 +0000387
388 // If we have unconfirmed info, don't trust it.
Chris Lattnercb53af02008-11-29 03:22:12 +0000389 if (LocalDep.getInt() != Dirty) {
Chris Lattner52638032008-11-28 22:28:27 +0000390 // If we have a confirmed non-local flag, use it.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000391 if (LocalDep.getInt() == NonLocal || LocalDep.getInt() == None) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000392 // The only time this dependency is confirmed is if it is non-local.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000393 NewDependency = LocalDep;
Chris Lattner52638032008-11-28 22:28:27 +0000394 } else {
395 // If we have dep info for RemInst, set them to it.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000396 Instruction *NDI = next(BasicBlock::iterator(LocalDep.getPointer()));
397 if (NDI != RemInst) // Don't use RemInst for the new dependency!
Chris Lattnercb53af02008-11-29 03:22:12 +0000398 NewDependency = DepResultTy(NDI, Dirty);
Chris Lattner52638032008-11-28 22:28:27 +0000399 }
David Greene701171c2007-07-31 20:01:27 +0000400 }
Owen Anderson6487cf52008-02-05 04:34:03 +0000401 }
402
Chris Lattner52638032008-11-28 22:28:27 +0000403 // If we don't already have a local dependency answer for this instruction,
404 // use the immediate successor of RemInst. We use the successor because
405 // getDependence starts by checking the immediate predecessor of what is in
406 // the cache.
Chris Lattnercb53af02008-11-29 03:22:12 +0000407 if (NewDependency == DepResultTy(0, Dirty))
408 NewDependency = DepResultTy(next(BasicBlock::iterator(RemInst)), Dirty);
Chris Lattner52638032008-11-28 22:28:27 +0000409
Chris Lattner89fbbe72008-11-28 22:51:08 +0000410 // Loop over all of the things that depend on the instruction we're removing.
411 //
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000412 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
413 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000414 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
415 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
416 E = ReverseDeps.end(); I != E; ++I) {
417 Instruction *InstDependingOnRemInst = *I;
418
419 // If we thought the instruction depended on itself (possible for
420 // unconfirmed dependencies) ignore the update.
421 if (InstDependingOnRemInst == RemInst) continue;
422
423 // Insert the new dependencies.
Chris Lattnercb53af02008-11-29 03:22:12 +0000424 LocalDeps[InstDependingOnRemInst] = NewDependency;
Chris Lattner89fbbe72008-11-28 22:51:08 +0000425
426 // If our NewDependency is an instruction, make sure to remember that new
427 // things depend on it.
Chris Lattnercb53af02008-11-29 03:22:12 +0000428 if (Instruction *Inst = NewDependency.getPointer())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000429 ReverseLocalDeps[Inst].insert(InstDependingOnRemInst);
Chris Lattner89fbbe72008-11-28 22:51:08 +0000430 }
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000431 ReverseLocalDeps.erase(RemInst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000432 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000433
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000434 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
435 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000436 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000437 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
438 I != E; ++I)
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000439 for (DenseMap<BasicBlock*, DepResultTy>::iterator
440 DI = NonLocalDeps[*I].begin(), DE = NonLocalDeps[*I].end();
Owen Anderson05749072007-09-21 03:53:52 +0000441 DI != DE; ++DI)
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000442 if (DI->second == DepResultTy(RemInst, Normal))
Chris Lattnercb53af02008-11-29 03:22:12 +0000443 // FIXME: Why not remember the old insertion point??
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000444 DI->second = DepResultTy(0, Dirty);
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000445 ReverseNonLocalDeps.erase(ReverseDepIt);
Owen Anderson2bd46a52007-08-16 21:27:05 +0000446 }
Owen Andersonc772be72007-12-08 01:37:09 +0000447
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000448 NonLocalDeps.erase(RemInst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000449
Chris Lattner1b185de2008-11-28 22:04:47 +0000450 getAnalysis<AliasAnalysis>().deleteValue(RemInst);
Chris Lattner969470c2008-11-28 21:45:17 +0000451
Chris Lattner1b185de2008-11-28 22:04:47 +0000452 DEBUG(verifyRemoved(RemInst));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453}
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000454
455/// verifyRemoved - Verify that the specified instruction does not occur
456/// in our internal data structures.
457void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
458 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
459 E = LocalDeps.end(); I != E; ++I) {
460 assert(I->first != D && "Inst occurs in data structures");
461 assert(I->second.getPointer() != D &&
462 "Inst occurs in data structures");
463 }
464
465 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
466 E = NonLocalDeps.end(); I != E; ++I) {
467 assert(I->first != D && "Inst occurs in data structures");
468 for (DenseMap<BasicBlock*, DepResultTy>::iterator II = I->second.begin(),
469 EE = I->second.end(); II != EE; ++II)
470 assert(II->second.getPointer() != D && "Inst occurs in data structures");
471 }
472
473 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
474 E = ReverseLocalDeps.end(); I != E; ++I)
475 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
476 EE = I->second.end(); II != EE; ++II)
477 assert(*II != D && "Inst occurs in data structures");
478
479 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
480 E = ReverseNonLocalDeps.end();
481 I != E; ++I)
482 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
483 EE = I->second.end(); II != EE; ++II)
484 assert(*II != D && "Inst occurs in data structures");
485}