blob: 577dcb91ee4100f4fd6b46bb12a7b0a9d7cd4073 [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 Lattnere79be942008-12-07 01:50:16 +0000108/// getPointerDependencyFrom - Return the instruction on which a memory
109/// location depends. If isLoad is true, this routine ignore may-aliases with
110/// read-only operations.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000111MemDepResult MemoryDependenceAnalysis::
Chris Lattnere79be942008-12-07 01:50:16 +0000112getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad,
113 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Chris Lattner84b9a562008-12-07 00:21:18 +0000114 // The first instruction in a block is always non-local.
115 if (ScanIt == BB->begin())
116 return MemDepResult::getNonLocal();
117
Owen Anderson642a9e32007-08-08 22:26:03 +0000118 // Walk backwards through the basic block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000119 while (ScanIt != BB->begin()) {
120 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000121
Chris Lattnercfbb6342008-11-30 01:44:00 +0000122 // Values depend on loads if the pointers are must aliased. This means that
123 // a load depends on another must aliased load from the same value.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000124 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000125 Value *Pointer = LI->getPointerOperand();
126 uint64_t PointerSize = TD->getTypeStoreSize(LI->getType());
127
128 // If we found a pointer, check if it could be the same as our pointer.
Chris Lattnera161ab02008-11-29 09:09:48 +0000129 AliasAnalysis::AliasResult R =
Chris Lattnerd777d402008-11-30 19:24:31 +0000130 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
Chris Lattnera161ab02008-11-29 09:09:48 +0000131 if (R == AliasAnalysis::NoAlias)
132 continue;
133
134 // May-alias loads don't depend on each other without a dependence.
Chris Lattnere79be942008-12-07 01:50:16 +0000135 if (isLoad && R == AliasAnalysis::MayAlias)
Chris Lattnera161ab02008-11-29 09:09:48 +0000136 continue;
Chris Lattnerb51deb92008-12-05 21:04:20 +0000137 return MemDepResult::getDef(Inst);
138 }
139
140 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000141 Value *Pointer = SI->getPointerOperand();
142 uint64_t PointerSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
143
144 // If we found a pointer, check if it could be the same as our pointer.
145 AliasAnalysis::AliasResult R =
146 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
147
148 if (R == AliasAnalysis::NoAlias)
149 continue;
150 if (R == AliasAnalysis::MayAlias)
151 return MemDepResult::getClobber(Inst);
152 return MemDepResult::getDef(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000153 }
Chris Lattner237a8282008-11-30 01:39:32 +0000154
155 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattnerb51deb92008-12-05 21:04:20 +0000156 // the allocation, return Def. This means that there is no dependence and
Chris Lattner237a8282008-11-30 01:39:32 +0000157 // the access can be optimized based on that. For example, a load could
158 // turn into undef.
Chris Lattnera161ab02008-11-29 09:09:48 +0000159 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
Chris Lattner237a8282008-11-30 01:39:32 +0000160 Value *AccessPtr = MemPtr->getUnderlyingObject();
Owen Anderson78e02f72007-07-06 23:14:35 +0000161
Chris Lattner237a8282008-11-30 01:39:32 +0000162 if (AccessPtr == AI ||
Chris Lattnerd777d402008-11-30 19:24:31 +0000163 AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
Chris Lattnerb51deb92008-12-05 21:04:20 +0000164 return MemDepResult::getDef(AI);
Chris Lattner237a8282008-11-30 01:39:32 +0000165 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000166 }
Chris Lattnera161ab02008-11-29 09:09:48 +0000167
Chris Lattnerb51deb92008-12-05 21:04:20 +0000168 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
Chris Lattnere79be942008-12-07 01:50:16 +0000169 // FIXME: If this is a load, we should ignore readonly calls!
Chris Lattnerb51deb92008-12-05 21:04:20 +0000170 if (AA->getModRefInfo(Inst, MemPtr, MemSize) == AliasAnalysis::NoModRef)
Chris Lattner25a08142008-11-29 08:51:16 +0000171 continue;
Chris Lattnera161ab02008-11-29 09:09:48 +0000172
173 // Otherwise, there is a dependence.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000174 return MemDepResult::getClobber(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000175 }
176
Chris Lattner5391a1d2008-11-29 03:47:00 +0000177 // If we found nothing, return the non-local flag.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000178 return MemDepResult::getNonLocal();
Owen Anderson78e02f72007-07-06 23:14:35 +0000179}
180
Chris Lattner5391a1d2008-11-29 03:47:00 +0000181/// getDependency - Return the instruction on which a memory operation
182/// depends.
183MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
184 Instruction *ScanPos = QueryInst;
185
186 // Check for a cached result
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000187 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattner5391a1d2008-11-29 03:47:00 +0000188
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000189 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000190 // on MemDepResult's default constructing to 'dirty'.
191 if (!LocalCache.isDirty())
192 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000193
194 // Otherwise, if we have a dirty entry, we know we can start the scan at that
195 // instruction, which may save us some work.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000196 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000197 ScanPos = Inst;
Chris Lattner4a69bad2008-11-30 02:52:26 +0000198
199 SmallPtrSet<Instruction*, 4> &InstMap = ReverseLocalDeps[Inst];
200 InstMap.erase(QueryInst);
201 if (InstMap.empty())
202 ReverseLocalDeps.erase(Inst);
203 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000204
Chris Lattnere79be942008-12-07 01:50:16 +0000205 BasicBlock *QueryParent = QueryInst->getParent();
206
207 Value *MemPtr = 0;
208 uint64_t MemSize = 0;
209
Chris Lattner5391a1d2008-11-29 03:47:00 +0000210 // Do the scan.
Chris Lattnere79be942008-12-07 01:50:16 +0000211 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
212 // First instruction in the block -> non local.
213 LocalCache = MemDepResult::getNonLocal();
214 } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) {
215 // If this is a volatile store, don't mess around with it. Just return the
216 // previous instruction as a clobber.
217 if (SI->isVolatile())
218 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
219 else {
220 MemPtr = SI->getPointerOperand();
221 MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
222 }
223 } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) {
224 // If this is a volatile load, don't mess around with it. Just return the
225 // previous instruction as a clobber.
226 if (LI->isVolatile())
227 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
228 else {
229 MemPtr = LI->getPointerOperand();
230 MemSize = TD->getTypeStoreSize(LI->getType());
231 }
232 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000233 LocalCache = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos,
Chris Lattnere79be942008-12-07 01:50:16 +0000234 QueryParent);
235 } else if (FreeInst *FI = dyn_cast<FreeInst>(QueryInst)) {
236 MemPtr = FI->getPointerOperand();
237 // FreeInsts erase the entire structure, not just a field.
238 MemSize = ~0UL;
239 } else {
240 // Non-memory instruction.
241 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
242 }
243
244 // If we need to do a pointer scan, make it happen.
245 if (MemPtr)
246 LocalCache = getPointerDependencyFrom(MemPtr, MemSize,
247 isa<LoadInst>(QueryInst),
248 ScanPos, QueryParent);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000249
250 // Remember the result!
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000251 if (Instruction *I = LocalCache.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000252 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000253
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000254 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000255}
256
Chris Lattner37d041c2008-11-30 01:18:27 +0000257/// getNonLocalDependency - Perform a full dependency query for the
258/// specified instruction, returning the set of blocks that the value is
259/// potentially live across. The returned set of results will include a
260/// "NonLocal" result for all blocks where the value is live across.
261///
262/// This method assumes the instruction returns a "nonlocal" dependency
263/// within its own block.
264///
Chris Lattnerbf145d62008-12-01 01:15:42 +0000265const MemoryDependenceAnalysis::NonLocalDepInfo &
266MemoryDependenceAnalysis::getNonLocalDependency(Instruction *QueryInst) {
Chris Lattnere79be942008-12-07 01:50:16 +0000267 assert(isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst) ||
268 isa<LoadInst>(QueryInst) || isa<StoreInst>(QueryInst));
Chris Lattner37d041c2008-11-30 01:18:27 +0000269 assert(getDependency(QueryInst).isNonLocal() &&
270 "getNonLocalDependency should only be used on insts with non-local deps!");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000271 PerInstNLInfo &CacheP = NonLocalDeps[QueryInst];
Chris Lattnerbf145d62008-12-01 01:15:42 +0000272 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner37d041c2008-11-30 01:18:27 +0000273
274 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
275 /// the cached case, this can happen due to instructions being deleted etc. In
276 /// the uncached case, this starts out as the set of predecessors we care
277 /// about.
278 SmallVector<BasicBlock*, 32> DirtyBlocks;
279
280 if (!Cache.empty()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000281 // Okay, we have a cache entry. If we know it is not dirty, just return it
282 // with no computation.
283 if (!CacheP.second) {
284 NumCacheNonLocal++;
285 return Cache;
286 }
287
Chris Lattner37d041c2008-11-30 01:18:27 +0000288 // If we already have a partially computed set of results, scan them to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000289 // determine what is dirty, seeding our initial DirtyBlocks worklist.
290 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
291 I != E; ++I)
292 if (I->second.isDirty())
293 DirtyBlocks.push_back(I->first);
Chris Lattner37d041c2008-11-30 01:18:27 +0000294
Chris Lattnerbf145d62008-12-01 01:15:42 +0000295 // Sort the cache so that we can do fast binary search lookups below.
296 std::sort(Cache.begin(), Cache.end());
Chris Lattner37d041c2008-11-30 01:18:27 +0000297
Chris Lattnerbf145d62008-12-01 01:15:42 +0000298 ++NumCacheDirtyNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000299 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
300 // << Cache.size() << " cached: " << *QueryInst;
301 } else {
302 // Seed DirtyBlocks with each of the preds of QueryInst's block.
303 BasicBlock *QueryBB = QueryInst->getParent();
304 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
305 NumUncacheNonLocal++;
306 }
307
Chris Lattnerbf145d62008-12-01 01:15:42 +0000308 // Visited checked first, vector in sorted order.
309 SmallPtrSet<BasicBlock*, 64> Visited;
310
311 unsigned NumSortedEntries = Cache.size();
312
Chris Lattner37d041c2008-11-30 01:18:27 +0000313 // Iterate while we still have blocks to update.
314 while (!DirtyBlocks.empty()) {
315 BasicBlock *DirtyBB = DirtyBlocks.back();
316 DirtyBlocks.pop_back();
317
Chris Lattnerbf145d62008-12-01 01:15:42 +0000318 // Already processed this block?
319 if (!Visited.insert(DirtyBB))
320 continue;
Chris Lattner37d041c2008-11-30 01:18:27 +0000321
Chris Lattnerbf145d62008-12-01 01:15:42 +0000322 // Do a binary search to see if we already have an entry for this block in
323 // the cache set. If so, find it.
324 NonLocalDepInfo::iterator Entry =
325 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
326 std::make_pair(DirtyBB, MemDepResult()));
327 if (Entry != Cache.begin() && (&*Entry)[-1].first == DirtyBB)
328 --Entry;
329
330 MemDepResult *ExistingResult = 0;
331 if (Entry != Cache.begin()+NumSortedEntries &&
332 Entry->first == DirtyBB) {
333 // If we already have an entry, and if it isn't already dirty, the block
334 // is done.
335 if (!Entry->second.isDirty())
336 continue;
337
338 // Otherwise, remember this slot so we can update the value.
339 ExistingResult = &Entry->second;
340 }
341
Chris Lattner37d041c2008-11-30 01:18:27 +0000342 // If the dirty entry has a pointer, start scanning from it so we don't have
343 // to rescan the entire block.
344 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerbf145d62008-12-01 01:15:42 +0000345 if (ExistingResult) {
346 if (Instruction *Inst = ExistingResult->getInst()) {
347 ScanPos = Inst;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000348
Chris Lattnerbf145d62008-12-01 01:15:42 +0000349 // We're removing QueryInst's use of Inst.
350 SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst];
351 InstMap.erase(QueryInst);
352 if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst);
353 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000354 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000355
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000356 // Find out if this block has a local dependency for QueryInst.
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000357 MemDepResult Dep;
Chris Lattnere79be942008-12-07 01:50:16 +0000358
359 Value *MemPtr = 0;
360 uint64_t MemSize = 0;
361
362 if (BasicBlock::iterator(QueryInst) == DirtyBB->begin()) {
363 // First instruction in the block -> non local.
364 Dep = MemDepResult::getNonLocal();
365 } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) {
366 // If this is a volatile store, don't mess around with it. Just return the
367 // previous instruction as a clobber.
368 if (SI->isVolatile())
369 Dep = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
370 else {
371 MemPtr = SI->getPointerOperand();
372 MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
373 }
374 } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) {
375 // If this is a volatile load, don't mess around with it. Just return the
376 // previous instruction as a clobber.
377 if (LI->isVolatile())
378 Dep = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
379 else {
380 MemPtr = LI->getPointerOperand();
381 MemSize = TD->getTypeStoreSize(LI->getType());
382 }
383 } else {
384 assert(isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst));
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000385 Dep = getCallSiteDependencyFrom(CallSite::get(QueryInst), ScanPos,
386 DirtyBB);
Chris Lattnere79be942008-12-07 01:50:16 +0000387 }
388
389 if (MemPtr)
390 Dep = getPointerDependencyFrom(MemPtr, MemSize, isa<LoadInst>(QueryInst),
391 ScanPos, DirtyBB);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000392
393 // If we had a dirty entry for the block, update it. Otherwise, just add
394 // a new entry.
395 if (ExistingResult)
396 *ExistingResult = Dep;
397 else
398 Cache.push_back(std::make_pair(DirtyBB, Dep));
399
Chris Lattner37d041c2008-11-30 01:18:27 +0000400 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000401 // the value), remember the association!
402 if (!Dep.isNonLocal()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000403 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
404 // update this when we remove instructions.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000405 if (Instruction *Inst = Dep.getInst())
Chris Lattner37d041c2008-11-30 01:18:27 +0000406 ReverseNonLocalDeps[Inst].insert(QueryInst);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000407 } else {
Chris Lattner37d041c2008-11-30 01:18:27 +0000408
Chris Lattnerbf145d62008-12-01 01:15:42 +0000409 // If the block *is* completely transparent to the load, we need to check
410 // the predecessors of this block. Add them to our worklist.
411 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
412 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000413 }
414
Chris Lattnerbf145d62008-12-01 01:15:42 +0000415 return Cache;
Chris Lattner37d041c2008-11-30 01:18:27 +0000416}
417
Owen Anderson78e02f72007-07-06 23:14:35 +0000418/// removeInstruction - Remove an instruction from the dependence analysis,
419/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +0000420/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +0000421void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +0000422 // Walk through the Non-local dependencies, removing this one as the value
423 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +0000424 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
425 if (NLDI != NonLocalDeps.end()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000426 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattner25f4b2b2008-11-30 02:30:50 +0000427 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
428 DI != DE; ++DI)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000429 if (Instruction *Inst = DI->second.getInst())
Chris Lattnerf68f3102008-11-30 02:28:25 +0000430 ReverseNonLocalDeps[Inst].erase(RemInst);
Chris Lattnerf68f3102008-11-30 02:28:25 +0000431 NonLocalDeps.erase(NLDI);
432 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000433
Chris Lattner5f589dc2008-11-28 22:04:47 +0000434 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +0000435 //
Chris Lattner39f372e2008-11-29 01:43:36 +0000436 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
437 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000438 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000439 if (Instruction *Inst = LocalDepEntry->second.getInst()) {
Chris Lattner125ce362008-11-30 01:09:30 +0000440 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
441 RLD.erase(RemInst);
442 if (RLD.empty())
443 ReverseLocalDeps.erase(Inst);
444 }
445
Chris Lattnerbaad8882008-11-28 22:28:27 +0000446 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +0000447 LocalDeps.erase(LocalDepEntry);
Chris Lattner125ce362008-11-30 01:09:30 +0000448 }
Chris Lattnerbaad8882008-11-28 22:28:27 +0000449
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000450 // Loop over all of the things that depend on the instruction we're removing.
451 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000452 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
453
Chris Lattner8c465272008-11-29 09:20:15 +0000454 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
455 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000456 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner125ce362008-11-30 01:09:30 +0000457 // RemInst can't be the terminator if it has stuff depending on it.
458 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
459 "Nothing can locally depend on a terminator");
460
461 // Anything that was locally dependent on RemInst is now going to be
462 // dependent on the instruction after RemInst. It will have the dirty flag
463 // set so it will rescan. This saves having to scan the entire block to get
464 // to this point.
465 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
466
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000467 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
468 E = ReverseDeps.end(); I != E; ++I) {
469 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000470 assert(InstDependingOnRemInst != RemInst &&
471 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +0000472
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000473 LocalDeps[InstDependingOnRemInst] = MemDepResult::getDirty(NewDepInst);
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000474
Chris Lattner125ce362008-11-30 01:09:30 +0000475 // Make sure to remember that new things depend on NewDepInst.
476 ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
477 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000478 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000479
480 ReverseLocalDeps.erase(ReverseDepIt);
481
482 // Add new reverse deps after scanning the set, to avoid invalidating the
483 // 'ReverseDeps' reference.
484 while (!ReverseDepsToAdd.empty()) {
485 ReverseLocalDeps[ReverseDepsToAdd.back().first]
486 .insert(ReverseDepsToAdd.back().second);
487 ReverseDepsToAdd.pop_back();
488 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000489 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000490
Chris Lattner8c465272008-11-29 09:20:15 +0000491 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
492 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +0000493 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson4d13de42007-08-16 21:27:05 +0000494 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000495 I != E; ++I) {
496 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
497
Chris Lattner4a69bad2008-11-30 02:52:26 +0000498 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner4a69bad2008-11-30 02:52:26 +0000499 // The information is now dirty!
Chris Lattnerbf145d62008-12-01 01:15:42 +0000500 INLD.second = true;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000501
Chris Lattnerbf145d62008-12-01 01:15:42 +0000502 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
503 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000504 if (DI->second.getInst() != RemInst) continue;
Chris Lattnerf68f3102008-11-30 02:28:25 +0000505
506 // Convert to a dirty entry for the subsequent instruction.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000507 Instruction *NextI = 0;
508 if (!RemInst->isTerminator()) {
509 NextI = next(BasicBlock::iterator(RemInst));
Chris Lattnerf68f3102008-11-30 02:28:25 +0000510 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000511 }
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000512 DI->second = MemDepResult::getDirty(NextI);
Chris Lattnerf68f3102008-11-30 02:28:25 +0000513 }
514 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +0000515
516 ReverseNonLocalDeps.erase(ReverseDepIt);
517
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000518 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
519 while (!ReverseDepsToAdd.empty()) {
520 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
521 .insert(ReverseDepsToAdd.back().second);
522 ReverseDepsToAdd.pop_back();
523 }
Owen Anderson4d13de42007-08-16 21:27:05 +0000524 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +0000525
Chris Lattnerf68f3102008-11-30 02:28:25 +0000526 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnerd777d402008-11-30 19:24:31 +0000527 AA->deleteValue(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +0000528 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +0000529}
Chris Lattner729b2372008-11-29 21:25:10 +0000530
531/// verifyRemoved - Verify that the specified instruction does not occur
532/// in our internal data structures.
533void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
534 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
535 E = LocalDeps.end(); I != E; ++I) {
536 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000537 assert(I->second.getInst() != D &&
Chris Lattner729b2372008-11-29 21:25:10 +0000538 "Inst occurs in data structures");
539 }
540
541 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
542 E = NonLocalDeps.end(); I != E; ++I) {
543 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4a69bad2008-11-30 02:52:26 +0000544 const PerInstNLInfo &INLD = I->second;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000545 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
546 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000547 assert(II->second.getInst() != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000548 }
549
550 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +0000551 E = ReverseLocalDeps.end(); I != E; ++I) {
552 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000553 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
554 EE = I->second.end(); II != EE; ++II)
555 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000556 }
Chris Lattner729b2372008-11-29 21:25:10 +0000557
558 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
559 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +0000560 I != E; ++I) {
561 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +0000562 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
563 EE = I->second.end(); II != EE; ++II)
564 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +0000565 }
Chris Lattner729b2372008-11-29 21:25:10 +0000566}