blob: 415766a5a15c4f31a6c862f4f2d2f6c5bce64bbd [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 Lattnerbf145d62008-12-01 01:15:42 +000031STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
32STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
Chris Lattner0ec48dd2008-11-29 22:02:15 +000033STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
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
Chris Lattnerd777d402008-11-30 19:24:31 +000048bool MemoryDependenceAnalysis::runOnFunction(Function &) {
49 AA = &getAnalysis<AliasAnalysis>();
50 TD = &getAnalysis<TargetData>();
51 return false;
52}
53
Chris Lattnerbf145d62008-12-01 01:15:42 +000054
Chris Lattner8ef57c52008-12-07 00:35:51 +000055/// getCallSiteDependencyFrom - Private helper for finding the local
56/// dependencies of a call site.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +000057MemDepResult MemoryDependenceAnalysis::
Chris Lattner8ef57c52008-12-07 00:35:51 +000058getCallSiteDependencyFrom(CallSite CS, BasicBlock::iterator ScanIt,
59 BasicBlock *BB) {
Owen Anderson642a9e32007-08-08 22:26:03 +000060 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +000061 while (ScanIt != BB->begin()) {
62 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +000063
64 // If this inst is a memory op, get the pointer it accessed
Chris Lattner00314b32008-11-29 09:15:21 +000065 Value *Pointer = 0;
66 uint64_t PointerSize = 0;
67 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
68 Pointer = S->getPointerOperand();
Chris Lattnerd777d402008-11-30 19:24:31 +000069 PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner00314b32008-11-29 09:15:21 +000070 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
71 Pointer = V->getOperand(0);
Chris Lattnerd777d402008-11-30 19:24:31 +000072 PointerSize = TD->getTypeStoreSize(V->getType());
Chris Lattner00314b32008-11-29 09:15:21 +000073 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
74 Pointer = F->getPointerOperand();
Owen Anderson5f323202007-07-10 17:59:22 +000075
76 // FreeInsts erase the entire structure
Chris Lattner00314b32008-11-29 09:15:21 +000077 PointerSize = ~0UL;
78 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +000079 CallSite InstCS = CallSite::get(Inst);
80 // If these two calls do not interfere, look past it.
81 if (AA->getModRefInfo(CS, InstCS) == AliasAnalysis::NoModRef)
Chris Lattner00314b32008-11-29 09:15:21 +000082 continue;
Chris Lattnerb51deb92008-12-05 21:04:20 +000083
84 // FIXME: If this is a ref/ref result, we should ignore it!
85 // X = strlen(P);
86 // Y = strlen(Q);
87 // Z = strlen(P); // Z = X
88
89 // If they interfere, we generally return clobber. However, if they are
90 // calls to the same read-only functions we return Def.
91 if (!AA->onlyReadsMemory(CS) || CS.getCalledFunction() == 0 ||
92 CS.getCalledFunction() != InstCS.getCalledFunction())
93 return MemDepResult::getClobber(Inst);
94 return MemDepResult::getDef(Inst);
Chris Lattnercfbb6342008-11-30 01:44:00 +000095 } else {
96 // Non-memory instruction.
Owen Anderson202da142007-07-10 20:39:07 +000097 continue;
Chris Lattnercfbb6342008-11-30 01:44:00 +000098 }
Owen Anderson5f323202007-07-10 17:59:22 +000099
Chris Lattnerb51deb92008-12-05 21:04:20 +0000100 if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef)
101 return MemDepResult::getClobber(Inst);
Owen Anderson5f323202007-07-10 17:59:22 +0000102 }
103
Chris Lattner5391a1d2008-11-29 03:47:00 +0000104 // No dependence found.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000105 return MemDepResult::getNonLocal();
Owen Anderson5f323202007-07-10 17:59:22 +0000106}
107
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000108/// getDependencyFrom - Return the instruction on which a memory operation
109/// depends.
110MemDepResult MemoryDependenceAnalysis::
111getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
112 BasicBlock *BB) {
Chris Lattner84b9a562008-12-07 00:21:18 +0000113 // The first instruction in a block is always non-local.
114 if (ScanIt == BB->begin())
115 return MemDepResult::getNonLocal();
116
Owen Anderson78e02f72007-07-06 23:14:35 +0000117 // Get the pointer value for which dependence will be determined
Chris Lattner25a08142008-11-29 08:51:16 +0000118 Value *MemPtr = 0;
119 uint64_t MemSize = 0;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000120
Chris Lattner106c6ca2008-12-07 00:39:19 +0000121 if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) {
Chris Lattner745291a2008-12-07 00:28:02 +0000122 // If this is a volatile store, don't mess around with it. Just return the
123 // previous instruction as a clobber.
Chris Lattner106c6ca2008-12-07 00:39:19 +0000124 if (SI->isVolatile())
Chris Lattner745291a2008-12-07 00:28:02 +0000125 return MemDepResult::getClobber(--ScanIt);
126
Chris Lattner106c6ca2008-12-07 00:39:19 +0000127 MemPtr = SI->getPointerOperand();
128 MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
Chris Lattnerfbc72e32008-12-07 00:38:27 +0000129 } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) {
Chris Lattner745291a2008-12-07 00:28:02 +0000130 // If this is a volatile load, don't mess around with it. Just return the
131 // previous instruction as a clobber.
Chris Lattnerfbc72e32008-12-07 00:38:27 +0000132 if (LI->isVolatile())
Chris Lattner745291a2008-12-07 00:28:02 +0000133 return MemDepResult::getClobber(--ScanIt);
134
Chris Lattnerb51deb92008-12-05 21:04:20 +0000135 MemPtr = LI->getPointerOperand();
136 MemSize = TD->getTypeStoreSize(LI->getType());
Chris Lattner106c6ca2008-12-07 00:39:19 +0000137 } else if (FreeInst *FI = dyn_cast<FreeInst>(QueryInst)) {
138 MemPtr = FI->getPointerOperand();
Chris Lattner25a08142008-11-29 08:51:16 +0000139 // FreeInsts erase the entire structure, not just a field.
140 MemSize = ~0UL;
Chris Lattner84b9a562008-12-07 00:21:18 +0000141 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000142 assert(0 && "Should use getCallSiteDependencyFrom!");
Chris Lattner8ef57c52008-12-07 00:35:51 +0000143 return getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanIt, BB);
Chris Lattner84b9a562008-12-07 00:21:18 +0000144 } else {
145 // Otherwise, this is a vaarg or non-memory instruction, just return a
146 // clobber dependency on the previous inst.
147 return MemDepResult::getClobber(--ScanIt);
Chris Lattner69513812008-12-05 18:46:19 +0000148 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000149
Owen Anderson642a9e32007-08-08 22:26:03 +0000150 // Walk backwards through the basic block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000151 while (ScanIt != BB->begin()) {
152 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000153
Chris Lattnercfbb6342008-11-30 01:44:00 +0000154 // Values depend on loads if the pointers are must aliased. This means that
155 // a load depends on another must aliased load from the same value.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000156 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000157 Value *Pointer = LI->getPointerOperand();
158 uint64_t PointerSize = TD->getTypeStoreSize(LI->getType());
159
160 // If we found a pointer, check if it could be the same as our pointer.
Chris Lattnera161ab02008-11-29 09:09:48 +0000161 AliasAnalysis::AliasResult R =
Chris Lattnerd777d402008-11-30 19:24:31 +0000162 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
Chris Lattnera161ab02008-11-29 09:09:48 +0000163 if (R == AliasAnalysis::NoAlias)
164 continue;
165
166 // May-alias loads don't depend on each other without a dependence.
167 if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
168 continue;
Chris Lattnerb51deb92008-12-05 21:04:20 +0000169 return MemDepResult::getDef(Inst);
170 }
171
172 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000173 Value *Pointer = SI->getPointerOperand();
174 uint64_t PointerSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
175
176 // If we found a pointer, check if it could be the same as our pointer.
177 AliasAnalysis::AliasResult R =
178 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
179
180 if (R == AliasAnalysis::NoAlias)
181 continue;
182 if (R == AliasAnalysis::MayAlias)
183 return MemDepResult::getClobber(Inst);
184 return MemDepResult::getDef(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000185 }
Chris Lattner237a8282008-11-30 01:39:32 +0000186
187 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattnerb51deb92008-12-05 21:04:20 +0000188 // the allocation, return Def. This means that there is no dependence and
Chris Lattner237a8282008-11-30 01:39:32 +0000189 // the access can be optimized based on that. For example, a load could
190 // turn into undef.
Chris Lattnera161ab02008-11-29 09:09:48 +0000191 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
Chris Lattner237a8282008-11-30 01:39:32 +0000192 Value *AccessPtr = MemPtr->getUnderlyingObject();
Owen Anderson78e02f72007-07-06 23:14:35 +0000193
Chris Lattner237a8282008-11-30 01:39:32 +0000194 if (AccessPtr == AI ||
Chris Lattnerd777d402008-11-30 19:24:31 +0000195 AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
Chris Lattnerb51deb92008-12-05 21:04:20 +0000196 return MemDepResult::getDef(AI);
Chris Lattner237a8282008-11-30 01:39:32 +0000197 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000198 }
Chris Lattnera161ab02008-11-29 09:09:48 +0000199
Chris Lattnerb51deb92008-12-05 21:04:20 +0000200 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
201 if (AA->getModRefInfo(Inst, MemPtr, MemSize) == AliasAnalysis::NoModRef)
Chris Lattner25a08142008-11-29 08:51:16 +0000202 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000203
204 // Otherwise, there is a dependence.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000205 return MemDepResult::getClobber(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000206 }
207
Chris Lattner5391a1d2008-11-29 03:47:00 +0000208 // If we found nothing, return the non-local flag.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000209 return MemDepResult::getNonLocal();
Owen Anderson78e02f72007-07-06 23:14:35 +0000210}
211
Chris Lattner5391a1d2008-11-29 03:47:00 +0000212/// getDependency - Return the instruction on which a memory operation
213/// depends.
214MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
215 Instruction *ScanPos = QueryInst;
216
217 // Check for a cached result
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000218 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattner5391a1d2008-11-29 03:47:00 +0000219
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000220 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000221 // on MemDepResult's default constructing to 'dirty'.
222 if (!LocalCache.isDirty())
223 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000224
225 // Otherwise, if we have a dirty entry, we know we can start the scan at that
226 // instruction, which may save us some work.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000227 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000228 ScanPos = Inst;
Chris Lattner4a69bad2008-11-30 02:52:26 +0000229
230 SmallPtrSet<Instruction*, 4> &InstMap = ReverseLocalDeps[Inst];
231 InstMap.erase(QueryInst);
232 if (InstMap.empty())
233 ReverseLocalDeps.erase(Inst);
234 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000235
236 // Do the scan.
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000237 if (!isa<CallInst>(QueryInst) && !isa<InvokeInst>(QueryInst))
238 LocalCache = getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
239 else
240 LocalCache = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos,
241 QueryInst->getParent());
Chris Lattner5391a1d2008-11-29 03:47:00 +0000242
243 // Remember the result!
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000244 if (Instruction *I = LocalCache.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000245 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000246
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000247 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000248}
249
Chris Lattner37d041c2008-11-30 01:18:27 +0000250/// getNonLocalDependency - Perform a full dependency query for the
251/// specified instruction, returning the set of blocks that the value is
252/// potentially live across. The returned set of results will include a
253/// "NonLocal" result for all blocks where the value is live across.
254///
255/// This method assumes the instruction returns a "nonlocal" dependency
256/// within its own block.
257///
Chris Lattnerbf145d62008-12-01 01:15:42 +0000258const MemoryDependenceAnalysis::NonLocalDepInfo &
259MemoryDependenceAnalysis::getNonLocalDependency(Instruction *QueryInst) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000260 assert(getDependency(QueryInst).isNonLocal() &&
261 "getNonLocalDependency should only be used on insts with non-local deps!");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000262 PerInstNLInfo &CacheP = NonLocalDeps[QueryInst];
Chris Lattnerf68f3102008-11-30 02:28:25 +0000263
Chris Lattnerbf145d62008-12-01 01:15:42 +0000264 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner37d041c2008-11-30 01:18:27 +0000265
266 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
267 /// the cached case, this can happen due to instructions being deleted etc. In
268 /// the uncached case, this starts out as the set of predecessors we care
269 /// about.
270 SmallVector<BasicBlock*, 32> DirtyBlocks;
271
272 if (!Cache.empty()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000273 // Okay, we have a cache entry. If we know it is not dirty, just return it
274 // with no computation.
275 if (!CacheP.second) {
276 NumCacheNonLocal++;
277 return Cache;
278 }
279
Chris Lattner37d041c2008-11-30 01:18:27 +0000280 // If we already have a partially computed set of results, scan them to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000281 // determine what is dirty, seeding our initial DirtyBlocks worklist.
282 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
283 I != E; ++I)
284 if (I->second.isDirty())
285 DirtyBlocks.push_back(I->first);
Chris Lattner37d041c2008-11-30 01:18:27 +0000286
Chris Lattnerbf145d62008-12-01 01:15:42 +0000287 // Sort the cache so that we can do fast binary search lookups below.
288 std::sort(Cache.begin(), Cache.end());
Chris Lattner37d041c2008-11-30 01:18:27 +0000289
Chris Lattnerbf145d62008-12-01 01:15:42 +0000290 ++NumCacheDirtyNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000291 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
292 // << Cache.size() << " cached: " << *QueryInst;
293 } else {
294 // Seed DirtyBlocks with each of the preds of QueryInst's block.
295 BasicBlock *QueryBB = QueryInst->getParent();
296 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
297 NumUncacheNonLocal++;
298 }
299
Chris Lattnerbf145d62008-12-01 01:15:42 +0000300 // Visited checked first, vector in sorted order.
301 SmallPtrSet<BasicBlock*, 64> Visited;
302
303 unsigned NumSortedEntries = Cache.size();
304
Chris Lattner37d041c2008-11-30 01:18:27 +0000305 // Iterate while we still have blocks to update.
306 while (!DirtyBlocks.empty()) {
307 BasicBlock *DirtyBB = DirtyBlocks.back();
308 DirtyBlocks.pop_back();
309
Chris Lattnerbf145d62008-12-01 01:15:42 +0000310 // Already processed this block?
311 if (!Visited.insert(DirtyBB))
312 continue;
Chris Lattner37d041c2008-11-30 01:18:27 +0000313
Chris Lattnerbf145d62008-12-01 01:15:42 +0000314 // Do a binary search to see if we already have an entry for this block in
315 // the cache set. If so, find it.
316 NonLocalDepInfo::iterator Entry =
317 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
318 std::make_pair(DirtyBB, MemDepResult()));
319 if (Entry != Cache.begin() && (&*Entry)[-1].first == DirtyBB)
320 --Entry;
321
322 MemDepResult *ExistingResult = 0;
323 if (Entry != Cache.begin()+NumSortedEntries &&
324 Entry->first == DirtyBB) {
325 // If we already have an entry, and if it isn't already dirty, the block
326 // is done.
327 if (!Entry->second.isDirty())
328 continue;
329
330 // Otherwise, remember this slot so we can update the value.
331 ExistingResult = &Entry->second;
332 }
333
Chris Lattner37d041c2008-11-30 01:18:27 +0000334 // If the dirty entry has a pointer, start scanning from it so we don't have
335 // to rescan the entire block.
336 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerbf145d62008-12-01 01:15:42 +0000337 if (ExistingResult) {
338 if (Instruction *Inst = ExistingResult->getInst()) {
339 ScanPos = Inst;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000340
Chris Lattnerbf145d62008-12-01 01:15:42 +0000341 // We're removing QueryInst's use of Inst.
342 SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst];
343 InstMap.erase(QueryInst);
344 if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst);
345 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000346 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000347
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000348 // Find out if this block has a local dependency for QueryInst.
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000349 MemDepResult Dep;
350 if (!isa<CallInst>(QueryInst) && !isa<InvokeInst>(QueryInst))
351 Dep = getDependencyFrom(QueryInst, ScanPos, DirtyBB);
352 else
353 Dep = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos,
354 DirtyBB);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000355
356 // If we had a dirty entry for the block, update it. Otherwise, just add
357 // a new entry.
358 if (ExistingResult)
359 *ExistingResult = Dep;
360 else
361 Cache.push_back(std::make_pair(DirtyBB, Dep));
362
Chris Lattner37d041c2008-11-30 01:18:27 +0000363 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000364 // the value), remember the association!
365 if (!Dep.isNonLocal()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000366 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
367 // update this when we remove instructions.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000368 if (Instruction *Inst = Dep.getInst())
Chris Lattner37d041c2008-11-30 01:18:27 +0000369 ReverseNonLocalDeps[Inst].insert(QueryInst);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000370 } else {
Chris Lattner37d041c2008-11-30 01:18:27 +0000371
Chris Lattnerbf145d62008-12-01 01:15:42 +0000372 // If the block *is* completely transparent to the load, we need to check
373 // the predecessors of this block. Add them to our worklist.
374 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
375 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000376 }
377
Chris Lattnerbf145d62008-12-01 01:15:42 +0000378 return Cache;
Chris Lattner37d041c2008-11-30 01:18:27 +0000379}
380
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000381
Owen Anderson78e02f72007-07-06 23:14:35 +0000382/// removeInstruction - Remove an instruction from the dependence analysis,
383/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000384/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000385void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000386 // Walk through the Non-local dependencies, removing this one as the value
387 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +0000388 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
389 if (NLDI != NonLocalDeps.end()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000390 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattner25f4b2b2008-11-30 02:30:50 +0000391 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
392 DI != DE; ++DI)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000393 if (Instruction *Inst = DI->second.getInst())
Chris Lattnerf68f3102008-11-30 02:28:25 +0000394 ReverseNonLocalDeps[Inst].erase(RemInst);
Chris Lattnerf68f3102008-11-30 02:28:25 +0000395 NonLocalDeps.erase(NLDI);
396 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000397
Chris Lattner5f589dc2008-11-28 22:04:47 +0000398 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000399 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000400 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
401 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000402 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000403 if (Instruction *Inst = LocalDepEntry->second.getInst()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000404 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
405 RLD.erase(RemInst);
406 if (RLD.empty())
407 ReverseLocalDeps.erase(Inst);
408 }
409
Chris Lattnerbaad8882008-11-28 22:28:27 +0000410 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000411 LocalDeps.erase(LocalDepEntry);
Chris Lattner125ce362008-11-30 01:09:30 +0000412 }
Chris Lattnerbaad8882008-11-28 22:28:27 +0000413
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000414 // Loop over all of the things that depend on the instruction we're removing.
415 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000416 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
417
Chris Lattner8c465272008-11-29 09:20:15 +0000418 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
419 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000420 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner125ce362008-11-30 01:09:30 +0000421 // RemInst can't be the terminator if it has stuff depending on it.
422 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
423 "Nothing can locally depend on a terminator");
424
425 // Anything that was locally dependent on RemInst is now going to be
426 // dependent on the instruction after RemInst. It will have the dirty flag
427 // set so it will rescan. This saves having to scan the entire block to get
428 // to this point.
429 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
430
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000431 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
432 E = ReverseDeps.end(); I != E; ++I) {
433 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000434 assert(InstDependingOnRemInst != RemInst &&
435 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +0000436
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000437 LocalDeps[InstDependingOnRemInst] = MemDepResult::getDirty(NewDepInst);
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000438
Chris Lattner125ce362008-11-30 01:09:30 +0000439 // Make sure to remember that new things depend on NewDepInst.
440 ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
441 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000442 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000443
444 ReverseLocalDeps.erase(ReverseDepIt);
445
446 // Add new reverse deps after scanning the set, to avoid invalidating the
447 // 'ReverseDeps' reference.
448 while (!ReverseDepsToAdd.empty()) {
449 ReverseLocalDeps[ReverseDepsToAdd.back().first]
450 .insert(ReverseDepsToAdd.back().second);
451 ReverseDepsToAdd.pop_back();
452 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000453 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000454
Chris Lattner8c465272008-11-29 09:20:15 +0000455 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
456 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000457 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson4d13de42007-08-16 21:27:05 +0000458 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000459 I != E; ++I) {
460 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
461
Chris Lattner4a69bad2008-11-30 02:52:26 +0000462 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner4a69bad2008-11-30 02:52:26 +0000463 // The information is now dirty!
Chris Lattnerbf145d62008-12-01 01:15:42 +0000464 INLD.second = true;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000465
Chris Lattnerbf145d62008-12-01 01:15:42 +0000466 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
467 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000468 if (DI->second.getInst() != RemInst) continue;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000469
470 // Convert to a dirty entry for the subsequent instruction.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000471 Instruction *NextI = 0;
472 if (!RemInst->isTerminator()) {
473 NextI = next(BasicBlock::iterator(RemInst));
Chris Lattnerf68f3102008-11-30 02:28:25 +0000474 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000475 }
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000476 DI->second = MemDepResult::getDirty(NextI);
Chris Lattnerf68f3102008-11-30 02:28:25 +0000477 }
478 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000479
480 ReverseNonLocalDeps.erase(ReverseDepIt);
481
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000482 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
483 while (!ReverseDepsToAdd.empty()) {
484 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
485 .insert(ReverseDepsToAdd.back().second);
486 ReverseDepsToAdd.pop_back();
487 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000488 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000489
Chris Lattnerf68f3102008-11-30 02:28:25 +0000490 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnerd777d402008-11-30 19:24:31 +0000491 AA->deleteValue(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000492 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000493}
Chris Lattner729b2372008-11-29 21:25:10 +0000494
495/// verifyRemoved - Verify that the specified instruction does not occur
496/// in our internal data structures.
497void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
498 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
499 E = LocalDeps.end(); I != E; ++I) {
500 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000501 assert(I->second.getInst() != D &&
Chris Lattner729b2372008-11-29 21:25:10 +0000502 "Inst occurs in data structures");
503 }
504
505 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
506 E = NonLocalDeps.end(); I != E; ++I) {
507 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000508 const PerInstNLInfo &INLD = I->second;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000509 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
510 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000511 assert(II->second.getInst() != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000512 }
513
514 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +0000515 E = ReverseLocalDeps.end(); I != E; ++I) {
516 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000517 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
518 EE = I->second.end(); II != EE; ++II)
519 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000520 }
Chris Lattner729b2372008-11-29 21:25:10 +0000521
522 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
523 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000524 I != E; ++I) {
525 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000526 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
527 EE = I->second.end(); II != EE; ++II)
528 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000529 }
Chris Lattner729b2372008-11-29 21:25:10 +0000530}