blob: c75cbf2c59f0447a21b5fcc8919ae9c57fbfe5ab [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::
58getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt, BasicBlock *BB) {
Chris Lattner46876282008-12-01 01:15:42 +000059
Owen Anderson3de3c532007-08-08 22:26:03 +000060 // Walk backwards through the block, looking for dependencies
Chris Lattnera5a36c12008-11-29 03:47:00 +000061 while (ScanIt != BB->begin()) {
62 Instruction *Inst = --ScanIt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063
64 // If this inst is a memory op, get the pointer it accessed
Chris Lattner18bd2452008-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 Lattnere99ea702008-11-30 19:24:31 +000069 PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner18bd2452008-11-29 09:15:21 +000070 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
71 Pointer = V->getOperand(0);
Chris Lattnere99ea702008-11-30 19:24:31 +000072 PointerSize = TD->getTypeStoreSize(V->getType());
Chris Lattner18bd2452008-11-29 09:15:21 +000073 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
74 Pointer = F->getPointerOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075
76 // FreeInsts erase the entire structure
Chris Lattner18bd2452008-11-29 09:15:21 +000077 PointerSize = ~0UL;
78 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Chris Lattnere99ea702008-11-30 19:24:31 +000079 if (AA->getModRefBehavior(CallSite::get(Inst)) ==
Chris Lattnera5a36c12008-11-29 03:47:00 +000080 AliasAnalysis::DoesNotAccessMemory)
Chris Lattner18bd2452008-11-29 09:15:21 +000081 continue;
Chris Lattnere110a182008-11-30 23:17:19 +000082 return MemDepResult::get(Inst);
Chris Lattner6c69a1a2008-11-30 01:44:00 +000083 } else {
84 // Non-memory instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 continue;
Chris Lattner6c69a1a2008-11-30 01:44:00 +000086 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
Chris Lattnere99ea702008-11-30 19:24:31 +000088 if (AA->getModRefInfo(C, Pointer, PointerSize) != AliasAnalysis::NoModRef)
Chris Lattnere110a182008-11-30 23:17:19 +000089 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 }
91
Chris Lattnera5a36c12008-11-29 03:47:00 +000092 // No dependence found.
Chris Lattnere110a182008-11-30 23:17:19 +000093 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094}
95
Chris Lattnere110a182008-11-30 23:17:19 +000096/// getDependencyFrom - Return the instruction on which a memory operation
97/// depends.
98MemDepResult MemoryDependenceAnalysis::
99getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
100 BasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 // Get the pointer value for which dependence will be determined
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000102 Value *MemPtr = 0;
103 uint64_t MemSize = 0;
104 bool MemVolatile = false;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000105
106 if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000107 MemPtr = S->getPointerOperand();
Chris Lattnere99ea702008-11-30 19:24:31 +0000108 MemSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000109 MemVolatile = S->isVolatile();
Chris Lattnera5a36c12008-11-29 03:47:00 +0000110 } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000111 MemPtr = L->getPointerOperand();
Chris Lattnere99ea702008-11-30 19:24:31 +0000112 MemSize = TD->getTypeStoreSize(L->getType());
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000113 MemVolatile = L->isVolatile();
Chris Lattnera5a36c12008-11-29 03:47:00 +0000114 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000115 MemPtr = V->getOperand(0);
Chris Lattnere99ea702008-11-30 19:24:31 +0000116 MemSize = TD->getTypeStoreSize(V->getType());
Chris Lattnera5a36c12008-11-29 03:47:00 +0000117 } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000118 MemPtr = F->getPointerOperand();
119 // FreeInsts erase the entire structure, not just a field.
120 MemSize = ~0UL;
Chris Lattnera5a54502008-12-05 18:46:19 +0000121 } else {
122 assert((isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) &&
123 "Can only get dependency info for memory instructions!");
Chris Lattnera5a36c12008-11-29 03:47:00 +0000124 return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
Chris Lattnera5a54502008-12-05 18:46:19 +0000125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126
Owen Anderson3de3c532007-08-08 22:26:03 +0000127 // Walk backwards through the basic block, looking for dependencies
Chris Lattnera5a36c12008-11-29 03:47:00 +0000128 while (ScanIt != BB->begin()) {
129 Instruction *Inst = --ScanIt;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000130
131 // If the access is volatile and this is a volatile load/store, return a
132 // dependence.
133 if (MemVolatile &&
134 ((isa<LoadInst>(Inst) && cast<LoadInst>(Inst)->isVolatile()) ||
135 (isa<StoreInst>(Inst) && cast<StoreInst>(Inst)->isVolatile())))
Chris Lattnere110a182008-11-30 23:17:19 +0000136 return MemDepResult::get(Inst);
Chris Lattner4103c3c2008-11-29 09:09:48 +0000137
Chris Lattner6c69a1a2008-11-30 01:44:00 +0000138 // Values depend on loads if the pointers are must aliased. This means that
139 // a load depends on another must aliased load from the same value.
Chris Lattner4103c3c2008-11-29 09:09:48 +0000140 if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
141 Value *Pointer = L->getPointerOperand();
Chris Lattnere99ea702008-11-30 19:24:31 +0000142 uint64_t PointerSize = TD->getTypeStoreSize(L->getType());
Chris Lattner4103c3c2008-11-29 09:09:48 +0000143
144 // If we found a pointer, check if it could be the same as our pointer
145 AliasAnalysis::AliasResult R =
Chris Lattnere99ea702008-11-30 19:24:31 +0000146 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
Chris Lattner4103c3c2008-11-29 09:09:48 +0000147
148 if (R == AliasAnalysis::NoAlias)
149 continue;
150
151 // May-alias loads don't depend on each other without a dependence.
152 if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
153 continue;
Chris Lattnere110a182008-11-30 23:17:19 +0000154 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 }
Chris Lattner8ea60462008-11-30 01:39:32 +0000156
157 // If this is an allocation, and if we know that the accessed pointer is to
158 // the allocation, return None. This means that there is no dependence and
159 // the access can be optimized based on that. For example, a load could
160 // turn into undef.
Chris Lattner4103c3c2008-11-29 09:09:48 +0000161 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
Chris Lattner8ea60462008-11-30 01:39:32 +0000162 Value *AccessPtr = MemPtr->getUnderlyingObject();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163
Chris Lattner8ea60462008-11-30 01:39:32 +0000164 if (AccessPtr == AI ||
Chris Lattnere99ea702008-11-30 19:24:31 +0000165 AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
Chris Lattnere110a182008-11-30 23:17:19 +0000166 return MemDepResult::getNone();
Chris Lattner8ea60462008-11-30 01:39:32 +0000167 continue;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000168 }
Chris Lattner4103c3c2008-11-29 09:09:48 +0000169
170 // See if this instruction mod/ref's the pointer.
Chris Lattnere99ea702008-11-30 19:24:31 +0000171 AliasAnalysis::ModRefResult MRR = AA->getModRefInfo(Inst, MemPtr, MemSize);
Chris Lattner4103c3c2008-11-29 09:09:48 +0000172
173 if (MRR == AliasAnalysis::NoModRef)
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000174 continue;
175
Chris Lattner4103c3c2008-11-29 09:09:48 +0000176 // Loads don't depend on read-only instructions.
177 if (isa<LoadInst>(QueryInst) && MRR == AliasAnalysis::Ref)
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000178 continue;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000179
180 // Otherwise, there is a dependence.
Chris Lattnere110a182008-11-30 23:17:19 +0000181 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 }
183
Chris Lattnera5a36c12008-11-29 03:47:00 +0000184 // If we found nothing, return the non-local flag.
Chris Lattnere110a182008-11-30 23:17:19 +0000185 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186}
187
Chris Lattnera5a36c12008-11-29 03:47:00 +0000188/// getDependency - Return the instruction on which a memory operation
189/// depends.
190MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
191 Instruction *ScanPos = QueryInst;
192
193 // Check for a cached result
Chris Lattnere110a182008-11-30 23:17:19 +0000194 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattnera5a36c12008-11-29 03:47:00 +0000195
Chris Lattner98a6d802008-11-29 22:02:15 +0000196 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnere110a182008-11-30 23:17:19 +0000197 // on MemDepResult's default constructing to 'dirty'.
198 if (!LocalCache.isDirty())
199 return LocalCache;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000200
201 // Otherwise, if we have a dirty entry, we know we can start the scan at that
202 // instruction, which may save us some work.
Chris Lattnere110a182008-11-30 23:17:19 +0000203 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattnera5a36c12008-11-29 03:47:00 +0000204 ScanPos = Inst;
Chris Lattner7dc404d2008-11-30 02:52:26 +0000205
206 SmallPtrSet<Instruction*, 4> &InstMap = ReverseLocalDeps[Inst];
207 InstMap.erase(QueryInst);
208 if (InstMap.empty())
209 ReverseLocalDeps.erase(Inst);
210 }
Chris Lattnera5a36c12008-11-29 03:47:00 +0000211
212 // Do the scan.
Chris Lattnere110a182008-11-30 23:17:19 +0000213 LocalCache = getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
Chris Lattnera5a36c12008-11-29 03:47:00 +0000214
215 // Remember the result!
Chris Lattnere110a182008-11-30 23:17:19 +0000216 if (Instruction *I = LocalCache.getInst())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000217 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattnera5a36c12008-11-29 03:47:00 +0000218
Chris Lattnere110a182008-11-30 23:17:19 +0000219 return LocalCache;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000220}
221
Chris Lattner03de6162008-11-30 01:18:27 +0000222/// getNonLocalDependency - Perform a full dependency query for the
223/// specified instruction, returning the set of blocks that the value is
224/// potentially live across. The returned set of results will include a
225/// "NonLocal" result for all blocks where the value is live across.
226///
227/// This method assumes the instruction returns a "nonlocal" dependency
228/// within its own block.
229///
Chris Lattner46876282008-12-01 01:15:42 +0000230const MemoryDependenceAnalysis::NonLocalDepInfo &
231MemoryDependenceAnalysis::getNonLocalDependency(Instruction *QueryInst) {
Chris Lattner03de6162008-11-30 01:18:27 +0000232 assert(getDependency(QueryInst).isNonLocal() &&
233 "getNonLocalDependency should only be used on insts with non-local deps!");
Chris Lattner7dc404d2008-11-30 02:52:26 +0000234 PerInstNLInfo &CacheP = NonLocalDeps[QueryInst];
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000235
Chris Lattner46876282008-12-01 01:15:42 +0000236 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner03de6162008-11-30 01:18:27 +0000237
238 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
239 /// the cached case, this can happen due to instructions being deleted etc. In
240 /// the uncached case, this starts out as the set of predecessors we care
241 /// about.
242 SmallVector<BasicBlock*, 32> DirtyBlocks;
243
244 if (!Cache.empty()) {
Chris Lattner46876282008-12-01 01:15:42 +0000245 // Okay, we have a cache entry. If we know it is not dirty, just return it
246 // with no computation.
247 if (!CacheP.second) {
248 NumCacheNonLocal++;
249 return Cache;
250 }
251
Chris Lattner03de6162008-11-30 01:18:27 +0000252 // If we already have a partially computed set of results, scan them to
Chris Lattner46876282008-12-01 01:15:42 +0000253 // determine what is dirty, seeding our initial DirtyBlocks worklist.
254 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
255 I != E; ++I)
256 if (I->second.isDirty())
257 DirtyBlocks.push_back(I->first);
Chris Lattner03de6162008-11-30 01:18:27 +0000258
Chris Lattner46876282008-12-01 01:15:42 +0000259 // Sort the cache so that we can do fast binary search lookups below.
260 std::sort(Cache.begin(), Cache.end());
Chris Lattner03de6162008-11-30 01:18:27 +0000261
Chris Lattner46876282008-12-01 01:15:42 +0000262 ++NumCacheDirtyNonLocal;
Chris Lattner03de6162008-11-30 01:18:27 +0000263 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
264 // << Cache.size() << " cached: " << *QueryInst;
265 } else {
266 // Seed DirtyBlocks with each of the preds of QueryInst's block.
267 BasicBlock *QueryBB = QueryInst->getParent();
268 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
269 NumUncacheNonLocal++;
270 }
271
Chris Lattner46876282008-12-01 01:15:42 +0000272 // Visited checked first, vector in sorted order.
273 SmallPtrSet<BasicBlock*, 64> Visited;
274
275 unsigned NumSortedEntries = Cache.size();
276
Chris Lattner03de6162008-11-30 01:18:27 +0000277 // Iterate while we still have blocks to update.
278 while (!DirtyBlocks.empty()) {
279 BasicBlock *DirtyBB = DirtyBlocks.back();
280 DirtyBlocks.pop_back();
281
Chris Lattner46876282008-12-01 01:15:42 +0000282 // Already processed this block?
283 if (!Visited.insert(DirtyBB))
284 continue;
Chris Lattner03de6162008-11-30 01:18:27 +0000285
Chris Lattner46876282008-12-01 01:15:42 +0000286 // Do a binary search to see if we already have an entry for this block in
287 // the cache set. If so, find it.
288 NonLocalDepInfo::iterator Entry =
289 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
290 std::make_pair(DirtyBB, MemDepResult()));
291 if (Entry != Cache.begin() && (&*Entry)[-1].first == DirtyBB)
292 --Entry;
293
294 MemDepResult *ExistingResult = 0;
295 if (Entry != Cache.begin()+NumSortedEntries &&
296 Entry->first == DirtyBB) {
297 // If we already have an entry, and if it isn't already dirty, the block
298 // is done.
299 if (!Entry->second.isDirty())
300 continue;
301
302 // Otherwise, remember this slot so we can update the value.
303 ExistingResult = &Entry->second;
304 }
305
Chris Lattner03de6162008-11-30 01:18:27 +0000306 // If the dirty entry has a pointer, start scanning from it so we don't have
307 // to rescan the entire block.
308 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattner46876282008-12-01 01:15:42 +0000309 if (ExistingResult) {
310 if (Instruction *Inst = ExistingResult->getInst()) {
311 ScanPos = Inst;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000312
Chris Lattner46876282008-12-01 01:15:42 +0000313 // We're removing QueryInst's use of Inst.
314 SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst];
315 InstMap.erase(QueryInst);
316 if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst);
317 }
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000318 }
Chris Lattner03de6162008-11-30 01:18:27 +0000319
Chris Lattnerc0ade852008-11-30 01:26:32 +0000320 // Find out if this block has a local dependency for QueryInst.
Chris Lattner46876282008-12-01 01:15:42 +0000321 MemDepResult Dep = getDependencyFrom(QueryInst, ScanPos, DirtyBB);
322
323 // If we had a dirty entry for the block, update it. Otherwise, just add
324 // a new entry.
325 if (ExistingResult)
326 *ExistingResult = Dep;
327 else
328 Cache.push_back(std::make_pair(DirtyBB, Dep));
329
Chris Lattner03de6162008-11-30 01:18:27 +0000330 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattner46876282008-12-01 01:15:42 +0000331 // the value), remember the association!
332 if (!Dep.isNonLocal()) {
Chris Lattner03de6162008-11-30 01:18:27 +0000333 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
334 // update this when we remove instructions.
Chris Lattner46876282008-12-01 01:15:42 +0000335 if (Instruction *Inst = Dep.getInst())
Chris Lattner03de6162008-11-30 01:18:27 +0000336 ReverseNonLocalDeps[Inst].insert(QueryInst);
Chris Lattner46876282008-12-01 01:15:42 +0000337 } else {
Chris Lattner03de6162008-11-30 01:18:27 +0000338
Chris Lattner46876282008-12-01 01:15:42 +0000339 // If the block *is* completely transparent to the load, we need to check
340 // the predecessors of this block. Add them to our worklist.
341 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
342 }
Chris Lattner03de6162008-11-30 01:18:27 +0000343 }
344
Chris Lattner46876282008-12-01 01:15:42 +0000345 return Cache;
Chris Lattner03de6162008-11-30 01:18:27 +0000346}
347
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348/// removeInstruction - Remove an instruction from the dependence analysis,
349/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000350/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner1b185de2008-11-28 22:04:47 +0000351void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner1b185de2008-11-28 22:04:47 +0000352 // Walk through the Non-local dependencies, removing this one as the value
353 // for any cached queries.
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000354 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
355 if (NLDI != NonLocalDeps.end()) {
Chris Lattner46876282008-12-01 01:15:42 +0000356 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattnerff3342f2008-11-30 02:30:50 +0000357 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
358 DI != DE; ++DI)
Chris Lattnere110a182008-11-30 23:17:19 +0000359 if (Instruction *Inst = DI->second.getInst())
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000360 ReverseNonLocalDeps[Inst].erase(RemInst);
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000361 NonLocalDeps.erase(NLDI);
362 }
Owen Andersonc772be72007-12-08 01:37:09 +0000363
Chris Lattner1b185de2008-11-28 22:04:47 +0000364 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattner52638032008-11-28 22:28:27 +0000365 //
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000366 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
367 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000368 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnere110a182008-11-30 23:17:19 +0000369 if (Instruction *Inst = LocalDepEntry->second.getInst()) {
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000370 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
371 RLD.erase(RemInst);
372 if (RLD.empty())
373 ReverseLocalDeps.erase(Inst);
374 }
375
Chris Lattner52638032008-11-28 22:28:27 +0000376 // Remove this local dependency info.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000377 LocalDeps.erase(LocalDepEntry);
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000378 }
Chris Lattner52638032008-11-28 22:28:27 +0000379
Chris Lattner89fbbe72008-11-28 22:51:08 +0000380 // Loop over all of the things that depend on the instruction we're removing.
381 //
Chris Lattner75cbaf82008-11-29 23:30:39 +0000382 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
383
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000384 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
385 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000386 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000387 // RemInst can't be the terminator if it has stuff depending on it.
388 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
389 "Nothing can locally depend on a terminator");
390
391 // Anything that was locally dependent on RemInst is now going to be
392 // dependent on the instruction after RemInst. It will have the dirty flag
393 // set so it will rescan. This saves having to scan the entire block to get
394 // to this point.
395 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
396
Chris Lattner89fbbe72008-11-28 22:51:08 +0000397 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
398 E = ReverseDeps.end(); I != E; ++I) {
399 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000400 assert(InstDependingOnRemInst != RemInst &&
401 "Already removed our local dep info");
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000402
Chris Lattnere110a182008-11-30 23:17:19 +0000403 LocalDeps[InstDependingOnRemInst] = MemDepResult::getDirty(NewDepInst);
Chris Lattner89fbbe72008-11-28 22:51:08 +0000404
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000405 // Make sure to remember that new things depend on NewDepInst.
406 ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
407 InstDependingOnRemInst));
Chris Lattner89fbbe72008-11-28 22:51:08 +0000408 }
Chris Lattner75cbaf82008-11-29 23:30:39 +0000409
410 ReverseLocalDeps.erase(ReverseDepIt);
411
412 // Add new reverse deps after scanning the set, to avoid invalidating the
413 // 'ReverseDeps' reference.
414 while (!ReverseDepsToAdd.empty()) {
415 ReverseLocalDeps[ReverseDepsToAdd.back().first]
416 .insert(ReverseDepsToAdd.back().second);
417 ReverseDepsToAdd.pop_back();
418 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000420
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000421 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
422 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000423 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000424 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000425 I != E; ++I) {
426 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
427
Chris Lattner7dc404d2008-11-30 02:52:26 +0000428 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner7dc404d2008-11-30 02:52:26 +0000429 // The information is now dirty!
Chris Lattner46876282008-12-01 01:15:42 +0000430 INLD.second = true;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000431
Chris Lattner46876282008-12-01 01:15:42 +0000432 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
433 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnere110a182008-11-30 23:17:19 +0000434 if (DI->second.getInst() != RemInst) continue;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000435
436 // Convert to a dirty entry for the subsequent instruction.
Chris Lattnere110a182008-11-30 23:17:19 +0000437 Instruction *NextI = 0;
438 if (!RemInst->isTerminator()) {
439 NextI = next(BasicBlock::iterator(RemInst));
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000440 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattner98a6d802008-11-29 22:02:15 +0000441 }
Chris Lattnere110a182008-11-30 23:17:19 +0000442 DI->second = MemDepResult::getDirty(NextI);
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000443 }
444 }
Chris Lattner75cbaf82008-11-29 23:30:39 +0000445
446 ReverseNonLocalDeps.erase(ReverseDepIt);
447
Chris Lattner98a6d802008-11-29 22:02:15 +0000448 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
449 while (!ReverseDepsToAdd.empty()) {
450 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
451 .insert(ReverseDepsToAdd.back().second);
452 ReverseDepsToAdd.pop_back();
453 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000454 }
Owen Andersonc772be72007-12-08 01:37:09 +0000455
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000456 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnere99ea702008-11-30 19:24:31 +0000457 AA->deleteValue(RemInst);
Chris Lattner1b185de2008-11-28 22:04:47 +0000458 DEBUG(verifyRemoved(RemInst));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459}
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000460
461/// verifyRemoved - Verify that the specified instruction does not occur
462/// in our internal data structures.
463void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
464 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
465 E = LocalDeps.end(); I != E; ++I) {
466 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnere110a182008-11-30 23:17:19 +0000467 assert(I->second.getInst() != D &&
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000468 "Inst occurs in data structures");
469 }
470
471 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
472 E = NonLocalDeps.end(); I != E; ++I) {
473 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner7dc404d2008-11-30 02:52:26 +0000474 const PerInstNLInfo &INLD = I->second;
Chris Lattner46876282008-12-01 01:15:42 +0000475 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
476 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnere110a182008-11-30 23:17:19 +0000477 assert(II->second.getInst() != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000478 }
479
480 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000481 E = ReverseLocalDeps.end(); I != E; ++I) {
482 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000483 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
484 EE = I->second.end(); II != EE; ++II)
485 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000486 }
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000487
488 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
489 E = ReverseNonLocalDeps.end();
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000490 I != E; ++I) {
491 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000492 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
493 EE = I->second.end(); II != EE; ++II)
494 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000495 }
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000496}