blob: cf748dfd89c3a066c3748b390972cc4995333539 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements an analysis that determines, for a given memory
11// operation, what preceding memory operations it depends on. It builds on
Owen Andersonafe840e2007-08-08 22:01:54 +000012// alias analysis information, and tries to provide a lazy, caching interface to
Dan Gohmanf17a25c2007-07-18 16:29:46 +000013// a common kind of alias information query.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner969470c2008-11-28 21:45:17 +000017#define DEBUG_TYPE "memdep"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Analysis/MemoryDependenceAnalysis.h"
19#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
21#include "llvm/Function.h"
22#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner52638032008-11-28 22:28:27 +000023#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/STLExtras.h"
Owen Anderson4c295472007-07-24 21:52:37 +000025#include "llvm/Support/CFG.h"
Tanya Lattner8edb2b72008-02-06 00:54:55 +000026#include "llvm/Support/CommandLine.h"
Chris Lattner969470c2008-11-28 21:45:17 +000027#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Target/TargetData.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029using namespace llvm;
30
Chris Lattner46876282008-12-01 01:15:42 +000031STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
32STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
Chris Lattner98a6d802008-11-29 22:02:15 +000033STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
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
Chris Lattnere99ea702008-11-30 19:24:31 +000048bool MemoryDependenceAnalysis::runOnFunction(Function &) {
49 AA = &getAnalysis<AliasAnalysis>();
50 TD = &getAnalysis<TargetData>();
51 return false;
52}
53
Chris Lattner46876282008-12-01 01:15:42 +000054
Owen Anderson3de3c532007-08-08 22:26:03 +000055/// getCallSiteDependency - Private helper for finding the local dependencies
56/// of a call site.
Chris Lattnere110a182008-11-30 23:17:19 +000057MemDepResult MemoryDependenceAnalysis::
Chris Lattner4531da82008-12-05 21:04:20 +000058getCallSiteDependency(CallSite CS, BasicBlock::iterator ScanIt, BasicBlock *BB) {
Owen Anderson3de3c532007-08-08 22:26:03 +000059 // Walk backwards through the block, looking for dependencies
Chris Lattnera5a36c12008-11-29 03:47:00 +000060 while (ScanIt != BB->begin()) {
61 Instruction *Inst = --ScanIt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
63 // If this inst is a memory op, get the pointer it accessed
Chris Lattner18bd2452008-11-29 09:15:21 +000064 Value *Pointer = 0;
65 uint64_t PointerSize = 0;
66 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
67 Pointer = S->getPointerOperand();
Chris Lattnere99ea702008-11-30 19:24:31 +000068 PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner18bd2452008-11-29 09:15:21 +000069 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
70 Pointer = V->getOperand(0);
Chris Lattnere99ea702008-11-30 19:24:31 +000071 PointerSize = TD->getTypeStoreSize(V->getType());
Chris Lattner18bd2452008-11-29 09:15:21 +000072 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
73 Pointer = F->getPointerOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074
75 // FreeInsts erase the entire structure
Chris Lattner18bd2452008-11-29 09:15:21 +000076 PointerSize = ~0UL;
77 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Chris Lattner4531da82008-12-05 21:04:20 +000078 CallSite InstCS = CallSite::get(Inst);
79 // If these two calls do not interfere, look past it.
80 if (AA->getModRefInfo(CS, InstCS) == AliasAnalysis::NoModRef)
Chris Lattner18bd2452008-11-29 09:15:21 +000081 continue;
Chris Lattner4531da82008-12-05 21:04:20 +000082
83 // FIXME: If this is a ref/ref result, we should ignore it!
84 // X = strlen(P);
85 // Y = strlen(Q);
86 // Z = strlen(P); // Z = X
87
88 // If they interfere, we generally return clobber. However, if they are
89 // calls to the same read-only functions we return Def.
90 if (!AA->onlyReadsMemory(CS) || CS.getCalledFunction() == 0 ||
91 CS.getCalledFunction() != InstCS.getCalledFunction())
92 return MemDepResult::getClobber(Inst);
93 return MemDepResult::getDef(Inst);
Chris Lattner6c69a1a2008-11-30 01:44:00 +000094 } else {
95 // Non-memory instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 continue;
Chris Lattner6c69a1a2008-11-30 01:44:00 +000097 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098
Chris Lattner4531da82008-12-05 21:04:20 +000099 if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef)
100 return MemDepResult::getClobber(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 }
102
Chris Lattnera5a36c12008-11-29 03:47:00 +0000103 // No dependence found.
Chris Lattnere110a182008-11-30 23:17:19 +0000104 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105}
106
Chris Lattnere110a182008-11-30 23:17:19 +0000107/// getDependencyFrom - Return the instruction on which a memory operation
108/// depends.
109MemDepResult MemoryDependenceAnalysis::
110getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
111 BasicBlock *BB) {
Chris Lattnerac55fa72008-12-07 00:21:18 +0000112 // The first instruction in a block is always non-local.
113 if (ScanIt == BB->begin())
114 return MemDepResult::getNonLocal();
115
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 // Get the pointer value for which dependence will be determined
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000117 Value *MemPtr = 0;
118 uint64_t MemSize = 0;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000119
120 if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
Chris Lattner3ac60822008-12-07 00:28:02 +0000121 // If this is a volatile store, don't mess around with it. Just return the
122 // previous instruction as a clobber.
123 if (S->isVolatile())
124 return MemDepResult::getClobber(--ScanIt);
125
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000126 MemPtr = S->getPointerOperand();
Chris Lattnere99ea702008-11-30 19:24:31 +0000127 MemSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner4531da82008-12-05 21:04:20 +0000128 } else if (LoadInst* LI = dyn_cast<LoadInst>(QueryInst)) {
Chris Lattner3ac60822008-12-07 00:28:02 +0000129 // If this is a volatile load, don't mess around with it. Just return the
130 // previous instruction as a clobber.
131 if (S->isVolatile())
132 return MemDepResult::getClobber(--ScanIt);
133
Chris Lattner4531da82008-12-05 21:04:20 +0000134 MemPtr = LI->getPointerOperand();
135 MemSize = TD->getTypeStoreSize(LI->getType());
Chris Lattnera5a36c12008-11-29 03:47:00 +0000136 } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000137 MemPtr = F->getPointerOperand();
138 // FreeInsts erase the entire structure, not just a field.
139 MemSize = ~0UL;
Chris Lattnerac55fa72008-12-07 00:21:18 +0000140 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Chris Lattnera5a36c12008-11-29 03:47:00 +0000141 return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
Chris Lattnerac55fa72008-12-07 00:21:18 +0000142 } else {
143 // Otherwise, this is a vaarg or non-memory instruction, just return a
144 // clobber dependency on the previous inst.
145 return MemDepResult::getClobber(--ScanIt);
Chris Lattnera5a54502008-12-05 18:46:19 +0000146 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147
Owen Anderson3de3c532007-08-08 22:26:03 +0000148 // Walk backwards through the basic block, looking for dependencies
Chris Lattnera5a36c12008-11-29 03:47:00 +0000149 while (ScanIt != BB->begin()) {
150 Instruction *Inst = --ScanIt;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000151
Chris Lattner6c69a1a2008-11-30 01:44:00 +0000152 // Values depend on loads if the pointers are must aliased. This means that
153 // a load depends on another must aliased load from the same value.
Chris Lattner4531da82008-12-05 21:04:20 +0000154 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Chris Lattner4531da82008-12-05 21:04:20 +0000155 Value *Pointer = LI->getPointerOperand();
156 uint64_t PointerSize = TD->getTypeStoreSize(LI->getType());
157
158 // If we found a pointer, check if it could be the same as our pointer.
Chris Lattner4103c3c2008-11-29 09:09:48 +0000159 AliasAnalysis::AliasResult R =
Chris Lattnere99ea702008-11-30 19:24:31 +0000160 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
Chris Lattner4103c3c2008-11-29 09:09:48 +0000161 if (R == AliasAnalysis::NoAlias)
162 continue;
163
164 // May-alias loads don't depend on each other without a dependence.
165 if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
166 continue;
Chris Lattner4531da82008-12-05 21:04:20 +0000167 return MemDepResult::getDef(Inst);
168 }
169
170 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chris Lattner4531da82008-12-05 21:04:20 +0000171 Value *Pointer = SI->getPointerOperand();
172 uint64_t PointerSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
173
174 // If we found a pointer, check if it could be the same as our pointer.
175 AliasAnalysis::AliasResult R =
176 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
177
178 if (R == AliasAnalysis::NoAlias)
179 continue;
180 if (R == AliasAnalysis::MayAlias)
181 return MemDepResult::getClobber(Inst);
182 return MemDepResult::getDef(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 }
Chris Lattner8ea60462008-11-30 01:39:32 +0000184
185 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattner4531da82008-12-05 21:04:20 +0000186 // the allocation, return Def. This means that there is no dependence and
Chris Lattner8ea60462008-11-30 01:39:32 +0000187 // the access can be optimized based on that. For example, a load could
188 // turn into undef.
Chris Lattner4103c3c2008-11-29 09:09:48 +0000189 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
Chris Lattner8ea60462008-11-30 01:39:32 +0000190 Value *AccessPtr = MemPtr->getUnderlyingObject();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191
Chris Lattner8ea60462008-11-30 01:39:32 +0000192 if (AccessPtr == AI ||
Chris Lattnere99ea702008-11-30 19:24:31 +0000193 AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
Chris Lattner4531da82008-12-05 21:04:20 +0000194 return MemDepResult::getDef(AI);
Chris Lattner8ea60462008-11-30 01:39:32 +0000195 continue;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000196 }
Chris Lattner4103c3c2008-11-29 09:09:48 +0000197
Chris Lattner4531da82008-12-05 21:04:20 +0000198 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
199 if (AA->getModRefInfo(Inst, MemPtr, MemSize) == AliasAnalysis::NoModRef)
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000200 continue;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000201
202 // Otherwise, there is a dependence.
Chris Lattner4531da82008-12-05 21:04:20 +0000203 return MemDepResult::getClobber(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 }
205
Chris Lattnera5a36c12008-11-29 03:47:00 +0000206 // If we found nothing, return the non-local flag.
Chris Lattnere110a182008-11-30 23:17:19 +0000207 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208}
209
Chris Lattnera5a36c12008-11-29 03:47:00 +0000210/// getDependency - Return the instruction on which a memory operation
211/// depends.
212MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
213 Instruction *ScanPos = QueryInst;
214
215 // Check for a cached result
Chris Lattnere110a182008-11-30 23:17:19 +0000216 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattnera5a36c12008-11-29 03:47:00 +0000217
Chris Lattner98a6d802008-11-29 22:02:15 +0000218 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnere110a182008-11-30 23:17:19 +0000219 // on MemDepResult's default constructing to 'dirty'.
220 if (!LocalCache.isDirty())
221 return LocalCache;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000222
223 // Otherwise, if we have a dirty entry, we know we can start the scan at that
224 // instruction, which may save us some work.
Chris Lattnere110a182008-11-30 23:17:19 +0000225 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattnera5a36c12008-11-29 03:47:00 +0000226 ScanPos = Inst;
Chris Lattner7dc404d2008-11-30 02:52:26 +0000227
228 SmallPtrSet<Instruction*, 4> &InstMap = ReverseLocalDeps[Inst];
229 InstMap.erase(QueryInst);
230 if (InstMap.empty())
231 ReverseLocalDeps.erase(Inst);
232 }
Chris Lattnera5a36c12008-11-29 03:47:00 +0000233
234 // Do the scan.
Chris Lattnere110a182008-11-30 23:17:19 +0000235 LocalCache = getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
Chris Lattnera5a36c12008-11-29 03:47:00 +0000236
237 // Remember the result!
Chris Lattnere110a182008-11-30 23:17:19 +0000238 if (Instruction *I = LocalCache.getInst())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000239 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattnera5a36c12008-11-29 03:47:00 +0000240
Chris Lattnere110a182008-11-30 23:17:19 +0000241 return LocalCache;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000242}
243
Chris Lattner03de6162008-11-30 01:18:27 +0000244/// getNonLocalDependency - Perform a full dependency query for the
245/// specified instruction, returning the set of blocks that the value is
246/// potentially live across. The returned set of results will include a
247/// "NonLocal" result for all blocks where the value is live across.
248///
249/// This method assumes the instruction returns a "nonlocal" dependency
250/// within its own block.
251///
Chris Lattner46876282008-12-01 01:15:42 +0000252const MemoryDependenceAnalysis::NonLocalDepInfo &
253MemoryDependenceAnalysis::getNonLocalDependency(Instruction *QueryInst) {
Chris Lattner03de6162008-11-30 01:18:27 +0000254 assert(getDependency(QueryInst).isNonLocal() &&
255 "getNonLocalDependency should only be used on insts with non-local deps!");
Chris Lattner7dc404d2008-11-30 02:52:26 +0000256 PerInstNLInfo &CacheP = NonLocalDeps[QueryInst];
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000257
Chris Lattner46876282008-12-01 01:15:42 +0000258 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner03de6162008-11-30 01:18:27 +0000259
260 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
261 /// the cached case, this can happen due to instructions being deleted etc. In
262 /// the uncached case, this starts out as the set of predecessors we care
263 /// about.
264 SmallVector<BasicBlock*, 32> DirtyBlocks;
265
266 if (!Cache.empty()) {
Chris Lattner46876282008-12-01 01:15:42 +0000267 // Okay, we have a cache entry. If we know it is not dirty, just return it
268 // with no computation.
269 if (!CacheP.second) {
270 NumCacheNonLocal++;
271 return Cache;
272 }
273
Chris Lattner03de6162008-11-30 01:18:27 +0000274 // If we already have a partially computed set of results, scan them to
Chris Lattner46876282008-12-01 01:15:42 +0000275 // determine what is dirty, seeding our initial DirtyBlocks worklist.
276 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
277 I != E; ++I)
278 if (I->second.isDirty())
279 DirtyBlocks.push_back(I->first);
Chris Lattner03de6162008-11-30 01:18:27 +0000280
Chris Lattner46876282008-12-01 01:15:42 +0000281 // Sort the cache so that we can do fast binary search lookups below.
282 std::sort(Cache.begin(), Cache.end());
Chris Lattner03de6162008-11-30 01:18:27 +0000283
Chris Lattner46876282008-12-01 01:15:42 +0000284 ++NumCacheDirtyNonLocal;
Chris Lattner03de6162008-11-30 01:18:27 +0000285 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
286 // << Cache.size() << " cached: " << *QueryInst;
287 } else {
288 // Seed DirtyBlocks with each of the preds of QueryInst's block.
289 BasicBlock *QueryBB = QueryInst->getParent();
290 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
291 NumUncacheNonLocal++;
292 }
293
Chris Lattner46876282008-12-01 01:15:42 +0000294 // Visited checked first, vector in sorted order.
295 SmallPtrSet<BasicBlock*, 64> Visited;
296
297 unsigned NumSortedEntries = Cache.size();
298
Chris Lattner03de6162008-11-30 01:18:27 +0000299 // Iterate while we still have blocks to update.
300 while (!DirtyBlocks.empty()) {
301 BasicBlock *DirtyBB = DirtyBlocks.back();
302 DirtyBlocks.pop_back();
303
Chris Lattner46876282008-12-01 01:15:42 +0000304 // Already processed this block?
305 if (!Visited.insert(DirtyBB))
306 continue;
Chris Lattner03de6162008-11-30 01:18:27 +0000307
Chris Lattner46876282008-12-01 01:15:42 +0000308 // Do a binary search to see if we already have an entry for this block in
309 // the cache set. If so, find it.
310 NonLocalDepInfo::iterator Entry =
311 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
312 std::make_pair(DirtyBB, MemDepResult()));
313 if (Entry != Cache.begin() && (&*Entry)[-1].first == DirtyBB)
314 --Entry;
315
316 MemDepResult *ExistingResult = 0;
317 if (Entry != Cache.begin()+NumSortedEntries &&
318 Entry->first == DirtyBB) {
319 // If we already have an entry, and if it isn't already dirty, the block
320 // is done.
321 if (!Entry->second.isDirty())
322 continue;
323
324 // Otherwise, remember this slot so we can update the value.
325 ExistingResult = &Entry->second;
326 }
327
Chris Lattner03de6162008-11-30 01:18:27 +0000328 // If the dirty entry has a pointer, start scanning from it so we don't have
329 // to rescan the entire block.
330 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattner46876282008-12-01 01:15:42 +0000331 if (ExistingResult) {
332 if (Instruction *Inst = ExistingResult->getInst()) {
333 ScanPos = Inst;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000334
Chris Lattner46876282008-12-01 01:15:42 +0000335 // We're removing QueryInst's use of Inst.
336 SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst];
337 InstMap.erase(QueryInst);
338 if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst);
339 }
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000340 }
Chris Lattner03de6162008-11-30 01:18:27 +0000341
Chris Lattnerc0ade852008-11-30 01:26:32 +0000342 // Find out if this block has a local dependency for QueryInst.
Chris Lattner46876282008-12-01 01:15:42 +0000343 MemDepResult Dep = getDependencyFrom(QueryInst, ScanPos, DirtyBB);
344
345 // If we had a dirty entry for the block, update it. Otherwise, just add
346 // a new entry.
347 if (ExistingResult)
348 *ExistingResult = Dep;
349 else
350 Cache.push_back(std::make_pair(DirtyBB, Dep));
351
Chris Lattner03de6162008-11-30 01:18:27 +0000352 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattner46876282008-12-01 01:15:42 +0000353 // the value), remember the association!
354 if (!Dep.isNonLocal()) {
Chris Lattner03de6162008-11-30 01:18:27 +0000355 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
356 // update this when we remove instructions.
Chris Lattner46876282008-12-01 01:15:42 +0000357 if (Instruction *Inst = Dep.getInst())
Chris Lattner03de6162008-11-30 01:18:27 +0000358 ReverseNonLocalDeps[Inst].insert(QueryInst);
Chris Lattner46876282008-12-01 01:15:42 +0000359 } else {
Chris Lattner03de6162008-11-30 01:18:27 +0000360
Chris Lattner46876282008-12-01 01:15:42 +0000361 // If the block *is* completely transparent to the load, we need to check
362 // the predecessors of this block. Add them to our worklist.
363 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
364 }
Chris Lattner03de6162008-11-30 01:18:27 +0000365 }
366
Chris Lattner46876282008-12-01 01:15:42 +0000367 return Cache;
Chris Lattner03de6162008-11-30 01:18:27 +0000368}
369
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370/// removeInstruction - Remove an instruction from the dependence analysis,
371/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000372/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner1b185de2008-11-28 22:04:47 +0000373void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner1b185de2008-11-28 22:04:47 +0000374 // Walk through the Non-local dependencies, removing this one as the value
375 // for any cached queries.
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000376 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
377 if (NLDI != NonLocalDeps.end()) {
Chris Lattner46876282008-12-01 01:15:42 +0000378 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattnerff3342f2008-11-30 02:30:50 +0000379 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
380 DI != DE; ++DI)
Chris Lattnere110a182008-11-30 23:17:19 +0000381 if (Instruction *Inst = DI->second.getInst())
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000382 ReverseNonLocalDeps[Inst].erase(RemInst);
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000383 NonLocalDeps.erase(NLDI);
384 }
Owen Andersonc772be72007-12-08 01:37:09 +0000385
Chris Lattner1b185de2008-11-28 22:04:47 +0000386 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattner52638032008-11-28 22:28:27 +0000387 //
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000388 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
389 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000390 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnere110a182008-11-30 23:17:19 +0000391 if (Instruction *Inst = LocalDepEntry->second.getInst()) {
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000392 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
393 RLD.erase(RemInst);
394 if (RLD.empty())
395 ReverseLocalDeps.erase(Inst);
396 }
397
Chris Lattner52638032008-11-28 22:28:27 +0000398 // Remove this local dependency info.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000399 LocalDeps.erase(LocalDepEntry);
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000400 }
Chris Lattner52638032008-11-28 22:28:27 +0000401
Chris Lattner89fbbe72008-11-28 22:51:08 +0000402 // Loop over all of the things that depend on the instruction we're removing.
403 //
Chris Lattner75cbaf82008-11-29 23:30:39 +0000404 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
405
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000406 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
407 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000408 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000409 // RemInst can't be the terminator if it has stuff depending on it.
410 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
411 "Nothing can locally depend on a terminator");
412
413 // Anything that was locally dependent on RemInst is now going to be
414 // dependent on the instruction after RemInst. It will have the dirty flag
415 // set so it will rescan. This saves having to scan the entire block to get
416 // to this point.
417 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
418
Chris Lattner89fbbe72008-11-28 22:51:08 +0000419 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
420 E = ReverseDeps.end(); I != E; ++I) {
421 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000422 assert(InstDependingOnRemInst != RemInst &&
423 "Already removed our local dep info");
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000424
Chris Lattnere110a182008-11-30 23:17:19 +0000425 LocalDeps[InstDependingOnRemInst] = MemDepResult::getDirty(NewDepInst);
Chris Lattner89fbbe72008-11-28 22:51:08 +0000426
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000427 // Make sure to remember that new things depend on NewDepInst.
428 ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
429 InstDependingOnRemInst));
Chris Lattner89fbbe72008-11-28 22:51:08 +0000430 }
Chris Lattner75cbaf82008-11-29 23:30:39 +0000431
432 ReverseLocalDeps.erase(ReverseDepIt);
433
434 // Add new reverse deps after scanning the set, to avoid invalidating the
435 // 'ReverseDeps' reference.
436 while (!ReverseDepsToAdd.empty()) {
437 ReverseLocalDeps[ReverseDepsToAdd.back().first]
438 .insert(ReverseDepsToAdd.back().second);
439 ReverseDepsToAdd.pop_back();
440 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000442
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000443 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
444 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000445 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000446 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000447 I != E; ++I) {
448 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
449
Chris Lattner7dc404d2008-11-30 02:52:26 +0000450 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner7dc404d2008-11-30 02:52:26 +0000451 // The information is now dirty!
Chris Lattner46876282008-12-01 01:15:42 +0000452 INLD.second = true;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000453
Chris Lattner46876282008-12-01 01:15:42 +0000454 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
455 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnere110a182008-11-30 23:17:19 +0000456 if (DI->second.getInst() != RemInst) continue;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000457
458 // Convert to a dirty entry for the subsequent instruction.
Chris Lattnere110a182008-11-30 23:17:19 +0000459 Instruction *NextI = 0;
460 if (!RemInst->isTerminator()) {
461 NextI = next(BasicBlock::iterator(RemInst));
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000462 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattner98a6d802008-11-29 22:02:15 +0000463 }
Chris Lattnere110a182008-11-30 23:17:19 +0000464 DI->second = MemDepResult::getDirty(NextI);
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000465 }
466 }
Chris Lattner75cbaf82008-11-29 23:30:39 +0000467
468 ReverseNonLocalDeps.erase(ReverseDepIt);
469
Chris Lattner98a6d802008-11-29 22:02:15 +0000470 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
471 while (!ReverseDepsToAdd.empty()) {
472 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
473 .insert(ReverseDepsToAdd.back().second);
474 ReverseDepsToAdd.pop_back();
475 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000476 }
Owen Andersonc772be72007-12-08 01:37:09 +0000477
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000478 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnere99ea702008-11-30 19:24:31 +0000479 AA->deleteValue(RemInst);
Chris Lattner1b185de2008-11-28 22:04:47 +0000480 DEBUG(verifyRemoved(RemInst));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481}
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000482
483/// verifyRemoved - Verify that the specified instruction does not occur
484/// in our internal data structures.
485void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
486 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
487 E = LocalDeps.end(); I != E; ++I) {
488 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnere110a182008-11-30 23:17:19 +0000489 assert(I->second.getInst() != D &&
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000490 "Inst occurs in data structures");
491 }
492
493 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
494 E = NonLocalDeps.end(); I != E; ++I) {
495 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner7dc404d2008-11-30 02:52:26 +0000496 const PerInstNLInfo &INLD = I->second;
Chris Lattner46876282008-12-01 01:15:42 +0000497 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
498 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnere110a182008-11-30 23:17:19 +0000499 assert(II->second.getInst() != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000500 }
501
502 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000503 E = ReverseLocalDeps.end(); I != E; ++I) {
504 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000505 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
506 EE = I->second.end(); II != EE; ++II)
507 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000508 }
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000509
510 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
511 E = ReverseNonLocalDeps.end();
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000512 I != E; ++I) {
513 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000514 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
515 EE = I->second.end(); II != EE; ++II)
516 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000517 }
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000518}