blob: 14646adf7b5d9a73f3635e62aea71959ffc04c2f [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 Lattner98a6d802008-11-29 22:02:15 +000031STATISTIC(NumCacheNonLocal, "Number of cached non-local responses");
32STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Owen Andersond6c7fea2007-09-09 21:43:49 +000033
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
Owen Anderson3de3c532007-08-08 22:26:03 +000054/// getCallSiteDependency - Private helper for finding the local dependencies
55/// of a call site.
Chris Lattnere110a182008-11-30 23:17:19 +000056MemDepResult MemoryDependenceAnalysis::
57getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt, BasicBlock *BB) {
Owen Anderson3de3c532007-08-08 22:26:03 +000058 // Walk backwards through the block, looking for dependencies
Chris Lattnera5a36c12008-11-29 03:47:00 +000059 while (ScanIt != BB->begin()) {
60 Instruction *Inst = --ScanIt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 // If this inst is a memory op, get the pointer it accessed
Chris Lattner18bd2452008-11-29 09:15:21 +000063 Value *Pointer = 0;
64 uint64_t PointerSize = 0;
65 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
66 Pointer = S->getPointerOperand();
Chris Lattnere99ea702008-11-30 19:24:31 +000067 PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner18bd2452008-11-29 09:15:21 +000068 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
69 Pointer = V->getOperand(0);
Chris Lattnere99ea702008-11-30 19:24:31 +000070 PointerSize = TD->getTypeStoreSize(V->getType());
Chris Lattner18bd2452008-11-29 09:15:21 +000071 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
72 Pointer = F->getPointerOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073
74 // FreeInsts erase the entire structure
Chris Lattner18bd2452008-11-29 09:15:21 +000075 PointerSize = ~0UL;
76 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Chris Lattnere99ea702008-11-30 19:24:31 +000077 if (AA->getModRefBehavior(CallSite::get(Inst)) ==
Chris Lattnera5a36c12008-11-29 03:47:00 +000078 AliasAnalysis::DoesNotAccessMemory)
Chris Lattner18bd2452008-11-29 09:15:21 +000079 continue;
Chris Lattnere110a182008-11-30 23:17:19 +000080 return MemDepResult::get(Inst);
Chris Lattner6c69a1a2008-11-30 01:44:00 +000081 } else {
82 // Non-memory instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 continue;
Chris Lattner6c69a1a2008-11-30 01:44:00 +000084 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085
Chris Lattnere99ea702008-11-30 19:24:31 +000086 if (AA->getModRefInfo(C, Pointer, PointerSize) != AliasAnalysis::NoModRef)
Chris Lattnere110a182008-11-30 23:17:19 +000087 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 }
89
Chris Lattnera5a36c12008-11-29 03:47:00 +000090 // No dependence found.
Chris Lattnere110a182008-11-30 23:17:19 +000091 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092}
93
Chris Lattnere110a182008-11-30 23:17:19 +000094/// getDependencyFrom - Return the instruction on which a memory operation
95/// depends.
96MemDepResult MemoryDependenceAnalysis::
97getDependencyFrom(Instruction *QueryInst, BasicBlock::iterator ScanIt,
98 BasicBlock *BB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 // Get the pointer value for which dependence will be determined
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000100 Value *MemPtr = 0;
101 uint64_t MemSize = 0;
102 bool MemVolatile = false;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000103
104 if (StoreInst* S = dyn_cast<StoreInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000105 MemPtr = S->getPointerOperand();
Chris Lattnere99ea702008-11-30 19:24:31 +0000106 MemSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000107 MemVolatile = S->isVolatile();
Chris Lattnera5a36c12008-11-29 03:47:00 +0000108 } else if (LoadInst* L = dyn_cast<LoadInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000109 MemPtr = L->getPointerOperand();
Chris Lattnere99ea702008-11-30 19:24:31 +0000110 MemSize = TD->getTypeStoreSize(L->getType());
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000111 MemVolatile = L->isVolatile();
Chris Lattnera5a36c12008-11-29 03:47:00 +0000112 } else if (VAArgInst* V = dyn_cast<VAArgInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000113 MemPtr = V->getOperand(0);
Chris Lattnere99ea702008-11-30 19:24:31 +0000114 MemSize = TD->getTypeStoreSize(V->getType());
Chris Lattnera5a36c12008-11-29 03:47:00 +0000115 } else if (FreeInst* F = dyn_cast<FreeInst>(QueryInst)) {
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000116 MemPtr = F->getPointerOperand();
117 // FreeInsts erase the entire structure, not just a field.
118 MemSize = ~0UL;
119 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst))
Chris Lattnera5a36c12008-11-29 03:47:00 +0000120 return getCallSiteDependency(CallSite::get(QueryInst), ScanIt, BB);
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000121 else // Non-memory instructions depend on nothing.
Chris Lattnere110a182008-11-30 23:17:19 +0000122 return MemDepResult::getNone();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123
Owen Anderson3de3c532007-08-08 22:26:03 +0000124 // Walk backwards through the basic block, looking for dependencies
Chris Lattnera5a36c12008-11-29 03:47:00 +0000125 while (ScanIt != BB->begin()) {
126 Instruction *Inst = --ScanIt;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000127
128 // If the access is volatile and this is a volatile load/store, return a
129 // dependence.
130 if (MemVolatile &&
131 ((isa<LoadInst>(Inst) && cast<LoadInst>(Inst)->isVolatile()) ||
132 (isa<StoreInst>(Inst) && cast<StoreInst>(Inst)->isVolatile())))
Chris Lattnere110a182008-11-30 23:17:19 +0000133 return MemDepResult::get(Inst);
Chris Lattner4103c3c2008-11-29 09:09:48 +0000134
Chris Lattner6c69a1a2008-11-30 01:44:00 +0000135 // Values depend on loads if the pointers are must aliased. This means that
136 // a load depends on another must aliased load from the same value.
Chris Lattner4103c3c2008-11-29 09:09:48 +0000137 if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
138 Value *Pointer = L->getPointerOperand();
Chris Lattnere99ea702008-11-30 19:24:31 +0000139 uint64_t PointerSize = TD->getTypeStoreSize(L->getType());
Chris Lattner4103c3c2008-11-29 09:09:48 +0000140
141 // If we found a pointer, check if it could be the same as our pointer
142 AliasAnalysis::AliasResult R =
Chris Lattnere99ea702008-11-30 19:24:31 +0000143 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
Chris Lattner4103c3c2008-11-29 09:09:48 +0000144
145 if (R == AliasAnalysis::NoAlias)
146 continue;
147
148 // May-alias loads don't depend on each other without a dependence.
149 if (isa<LoadInst>(QueryInst) && R == AliasAnalysis::MayAlias)
150 continue;
Chris Lattnere110a182008-11-30 23:17:19 +0000151 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 }
Chris Lattner8ea60462008-11-30 01:39:32 +0000153
154 // If this is an allocation, and if we know that the accessed pointer is to
155 // the allocation, return None. This means that there is no dependence and
156 // the access can be optimized based on that. For example, a load could
157 // turn into undef.
Chris Lattner4103c3c2008-11-29 09:09:48 +0000158 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
Chris Lattner8ea60462008-11-30 01:39:32 +0000159 Value *AccessPtr = MemPtr->getUnderlyingObject();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
Chris Lattner8ea60462008-11-30 01:39:32 +0000161 if (AccessPtr == AI ||
Chris Lattnere99ea702008-11-30 19:24:31 +0000162 AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
Chris Lattnere110a182008-11-30 23:17:19 +0000163 return MemDepResult::getNone();
Chris Lattner8ea60462008-11-30 01:39:32 +0000164 continue;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000165 }
Chris Lattner4103c3c2008-11-29 09:09:48 +0000166
167 // See if this instruction mod/ref's the pointer.
Chris Lattnere99ea702008-11-30 19:24:31 +0000168 AliasAnalysis::ModRefResult MRR = AA->getModRefInfo(Inst, MemPtr, MemSize);
Chris Lattner4103c3c2008-11-29 09:09:48 +0000169
170 if (MRR == AliasAnalysis::NoModRef)
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000171 continue;
172
Chris Lattner4103c3c2008-11-29 09:09:48 +0000173 // Loads don't depend on read-only instructions.
174 if (isa<LoadInst>(QueryInst) && MRR == AliasAnalysis::Ref)
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000175 continue;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000176
177 // Otherwise, there is a dependence.
Chris Lattnere110a182008-11-30 23:17:19 +0000178 return MemDepResult::get(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 }
180
Chris Lattnera5a36c12008-11-29 03:47:00 +0000181 // If we found nothing, return the non-local flag.
Chris Lattnere110a182008-11-30 23:17:19 +0000182 return MemDepResult::getNonLocal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183}
184
Chris Lattnera5a36c12008-11-29 03:47:00 +0000185/// getDependency - Return the instruction on which a memory operation
186/// depends.
187MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
188 Instruction *ScanPos = QueryInst;
189
190 // Check for a cached result
Chris Lattnere110a182008-11-30 23:17:19 +0000191 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattnera5a36c12008-11-29 03:47:00 +0000192
Chris Lattner98a6d802008-11-29 22:02:15 +0000193 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnere110a182008-11-30 23:17:19 +0000194 // on MemDepResult's default constructing to 'dirty'.
195 if (!LocalCache.isDirty())
196 return LocalCache;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000197
198 // Otherwise, if we have a dirty entry, we know we can start the scan at that
199 // instruction, which may save us some work.
Chris Lattnere110a182008-11-30 23:17:19 +0000200 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattnera5a36c12008-11-29 03:47:00 +0000201 ScanPos = Inst;
Chris Lattner7dc404d2008-11-30 02:52:26 +0000202
203 SmallPtrSet<Instruction*, 4> &InstMap = ReverseLocalDeps[Inst];
204 InstMap.erase(QueryInst);
205 if (InstMap.empty())
206 ReverseLocalDeps.erase(Inst);
207 }
Chris Lattnera5a36c12008-11-29 03:47:00 +0000208
209 // Do the scan.
Chris Lattnere110a182008-11-30 23:17:19 +0000210 LocalCache = getDependencyFrom(QueryInst, ScanPos, QueryInst->getParent());
Chris Lattnera5a36c12008-11-29 03:47:00 +0000211
212 // Remember the result!
Chris Lattnere110a182008-11-30 23:17:19 +0000213 if (Instruction *I = LocalCache.getInst())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000214 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattnera5a36c12008-11-29 03:47:00 +0000215
Chris Lattnere110a182008-11-30 23:17:19 +0000216 return LocalCache;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000217}
218
Chris Lattner03de6162008-11-30 01:18:27 +0000219/// getNonLocalDependency - Perform a full dependency query for the
220/// specified instruction, returning the set of blocks that the value is
221/// potentially live across. The returned set of results will include a
222/// "NonLocal" result for all blocks where the value is live across.
223///
224/// This method assumes the instruction returns a "nonlocal" dependency
225/// within its own block.
226///
227void MemoryDependenceAnalysis::
228getNonLocalDependency(Instruction *QueryInst,
229 SmallVectorImpl<std::pair<BasicBlock*,
230 MemDepResult> > &Result) {
231 assert(getDependency(QueryInst).isNonLocal() &&
232 "getNonLocalDependency should only be used on insts with non-local deps!");
Chris Lattner7dc404d2008-11-30 02:52:26 +0000233 PerInstNLInfo &CacheP = NonLocalDeps[QueryInst];
234 if (CacheP.getPointer() == 0) CacheP.setPointer(new NonLocalDepInfo());
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000235
Chris Lattner7dc404d2008-11-30 02:52:26 +0000236 NonLocalDepInfo &Cache = *CacheP.getPointer();
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()) {
245 // If we already have a partially computed set of results, scan them to
Chris Lattner7dc404d2008-11-30 02:52:26 +0000246 // determine what is dirty, seeding our initial DirtyBlocks worklist. The
247 // Int bit of CacheP tells us if we have anything dirty.
248 if (CacheP.getInt())
249 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
Chris Lattnerff3342f2008-11-30 02:30:50 +0000250 I != E; ++I)
Chris Lattnere110a182008-11-30 23:17:19 +0000251 if (I->second.isDirty())
Chris Lattner7dc404d2008-11-30 02:52:26 +0000252 DirtyBlocks.push_back(I->first);
Chris Lattner03de6162008-11-30 01:18:27 +0000253
254 NumCacheNonLocal++;
255
256 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
257 // << Cache.size() << " cached: " << *QueryInst;
258 } else {
259 // Seed DirtyBlocks with each of the preds of QueryInst's block.
260 BasicBlock *QueryBB = QueryInst->getParent();
261 DirtyBlocks.append(pred_begin(QueryBB), pred_end(QueryBB));
262 NumUncacheNonLocal++;
263 }
264
265 // Iterate while we still have blocks to update.
266 while (!DirtyBlocks.empty()) {
267 BasicBlock *DirtyBB = DirtyBlocks.back();
268 DirtyBlocks.pop_back();
269
Chris Lattnere110a182008-11-30 23:17:19 +0000270 // Get the entry for this block. Note that this relies on MemDepResult
Chris Lattner03de6162008-11-30 01:18:27 +0000271 // default initializing to Dirty.
Chris Lattnere110a182008-11-30 23:17:19 +0000272 MemDepResult &DirtyBBEntry = Cache[DirtyBB];
Chris Lattner03de6162008-11-30 01:18:27 +0000273
274 // If DirtyBBEntry isn't dirty, it ended up on the worklist multiple times.
Chris Lattnere110a182008-11-30 23:17:19 +0000275 if (!DirtyBBEntry.isDirty()) continue;
Chris Lattner03de6162008-11-30 01:18:27 +0000276
Chris Lattner03de6162008-11-30 01:18:27 +0000277 // If the dirty entry has a pointer, start scanning from it so we don't have
278 // to rescan the entire block.
279 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnere110a182008-11-30 23:17:19 +0000280 if (Instruction *Inst = DirtyBBEntry.getInst()) {
Chris Lattner03de6162008-11-30 01:18:27 +0000281 ScanPos = Inst;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000282
283 // We're removing QueryInst's dependence on Inst.
284 SmallPtrSet<Instruction*, 4> &InstMap = ReverseNonLocalDeps[Inst];
285 InstMap.erase(QueryInst);
286 if (InstMap.empty()) ReverseNonLocalDeps.erase(Inst);
287 }
Chris Lattner03de6162008-11-30 01:18:27 +0000288
Chris Lattnerc0ade852008-11-30 01:26:32 +0000289 // Find out if this block has a local dependency for QueryInst.
Chris Lattnere110a182008-11-30 23:17:19 +0000290 DirtyBBEntry = getDependencyFrom(QueryInst, ScanPos, DirtyBB);
Chris Lattner03de6162008-11-30 01:18:27 +0000291
292 // If the block has a dependency (i.e. it isn't completely transparent to
293 // the value), remember it!
Chris Lattnere110a182008-11-30 23:17:19 +0000294 if (!DirtyBBEntry.isNonLocal()) {
Chris Lattner03de6162008-11-30 01:18:27 +0000295 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
296 // update this when we remove instructions.
Chris Lattnere110a182008-11-30 23:17:19 +0000297 if (Instruction *Inst = DirtyBBEntry.getInst())
Chris Lattner03de6162008-11-30 01:18:27 +0000298 ReverseNonLocalDeps[Inst].insert(QueryInst);
299 continue;
300 }
301
302 // If the block *is* completely transparent to the load, we need to check
303 // the predecessors of this block. Add them to our worklist.
304 DirtyBlocks.append(pred_begin(DirtyBB), pred_end(DirtyBB));
305 }
306
307
308 // Copy the result into the output set.
Chris Lattnerff3342f2008-11-30 02:30:50 +0000309 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end(); I != E;++I)
Chris Lattnere110a182008-11-30 23:17:19 +0000310 Result.push_back(std::make_pair(I->first, I->second));
Chris Lattner03de6162008-11-30 01:18:27 +0000311}
312
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313/// removeInstruction - Remove an instruction from the dependence analysis,
314/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000315/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner1b185de2008-11-28 22:04:47 +0000316void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner1b185de2008-11-28 22:04:47 +0000317 // Walk through the Non-local dependencies, removing this one as the value
318 // for any cached queries.
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000319 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
320 if (NLDI != NonLocalDeps.end()) {
Chris Lattner7dc404d2008-11-30 02:52:26 +0000321 NonLocalDepInfo &BlockMap = *NLDI->second.getPointer();
Chris Lattnerff3342f2008-11-30 02:30:50 +0000322 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
323 DI != DE; ++DI)
Chris Lattnere110a182008-11-30 23:17:19 +0000324 if (Instruction *Inst = DI->second.getInst())
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000325 ReverseNonLocalDeps[Inst].erase(RemInst);
326 delete &BlockMap;
327 NonLocalDeps.erase(NLDI);
328 }
Owen Andersonc772be72007-12-08 01:37:09 +0000329
Chris Lattner1b185de2008-11-28 22:04:47 +0000330 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattner52638032008-11-28 22:28:27 +0000331 //
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000332 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
333 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000334 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnere110a182008-11-30 23:17:19 +0000335 if (Instruction *Inst = LocalDepEntry->second.getInst()) {
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000336 SmallPtrSet<Instruction*, 4> &RLD = ReverseLocalDeps[Inst];
337 RLD.erase(RemInst);
338 if (RLD.empty())
339 ReverseLocalDeps.erase(Inst);
340 }
341
Chris Lattner52638032008-11-28 22:28:27 +0000342 // Remove this local dependency info.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000343 LocalDeps.erase(LocalDepEntry);
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000344 }
Chris Lattner52638032008-11-28 22:28:27 +0000345
Chris Lattner89fbbe72008-11-28 22:51:08 +0000346 // Loop over all of the things that depend on the instruction we're removing.
347 //
Chris Lattner75cbaf82008-11-29 23:30:39 +0000348 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
349
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000350 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
351 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000352 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000353 // RemInst can't be the terminator if it has stuff depending on it.
354 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
355 "Nothing can locally depend on a terminator");
356
357 // Anything that was locally dependent on RemInst is now going to be
358 // dependent on the instruction after RemInst. It will have the dirty flag
359 // set so it will rescan. This saves having to scan the entire block to get
360 // to this point.
361 Instruction *NewDepInst = next(BasicBlock::iterator(RemInst));
362
Chris Lattner89fbbe72008-11-28 22:51:08 +0000363 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
364 E = ReverseDeps.end(); I != E; ++I) {
365 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000366 assert(InstDependingOnRemInst != RemInst &&
367 "Already removed our local dep info");
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000368
Chris Lattnere110a182008-11-30 23:17:19 +0000369 LocalDeps[InstDependingOnRemInst] = MemDepResult::getDirty(NewDepInst);
Chris Lattner89fbbe72008-11-28 22:51:08 +0000370
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000371 // Make sure to remember that new things depend on NewDepInst.
372 ReverseDepsToAdd.push_back(std::make_pair(NewDepInst,
373 InstDependingOnRemInst));
Chris Lattner89fbbe72008-11-28 22:51:08 +0000374 }
Chris Lattner75cbaf82008-11-29 23:30:39 +0000375
376 ReverseLocalDeps.erase(ReverseDepIt);
377
378 // Add new reverse deps after scanning the set, to avoid invalidating the
379 // 'ReverseDeps' reference.
380 while (!ReverseDepsToAdd.empty()) {
381 ReverseLocalDeps[ReverseDepsToAdd.back().first]
382 .insert(ReverseDepsToAdd.back().second);
383 ReverseDepsToAdd.pop_back();
384 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000386
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000387 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
388 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000389 SmallPtrSet<Instruction*, 4>& set = ReverseDepIt->second;
Owen Anderson2bd46a52007-08-16 21:27:05 +0000390 for (SmallPtrSet<Instruction*, 4>::iterator I = set.begin(), E = set.end();
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000391 I != E; ++I) {
392 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
393
Chris Lattner7dc404d2008-11-30 02:52:26 +0000394 PerInstNLInfo &INLD = NonLocalDeps[*I];
395 assert(INLD.getPointer() != 0 && "Reverse mapping out of date?");
396 // The information is now dirty!
397 INLD.setInt(true);
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000398
Chris Lattner7dc404d2008-11-30 02:52:26 +0000399 for (NonLocalDepInfo::iterator DI = INLD.getPointer()->begin(),
400 DE = INLD.getPointer()->end(); DI != DE; ++DI) {
Chris Lattnere110a182008-11-30 23:17:19 +0000401 if (DI->second.getInst() != RemInst) continue;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000402
403 // Convert to a dirty entry for the subsequent instruction.
Chris Lattnere110a182008-11-30 23:17:19 +0000404 Instruction *NextI = 0;
405 if (!RemInst->isTerminator()) {
406 NextI = next(BasicBlock::iterator(RemInst));
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000407 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattner98a6d802008-11-29 22:02:15 +0000408 }
Chris Lattnere110a182008-11-30 23:17:19 +0000409 DI->second = MemDepResult::getDirty(NextI);
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000410 }
411 }
Chris Lattner75cbaf82008-11-29 23:30:39 +0000412
413 ReverseNonLocalDeps.erase(ReverseDepIt);
414
Chris Lattner98a6d802008-11-29 22:02:15 +0000415 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
416 while (!ReverseDepsToAdd.empty()) {
417 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
418 .insert(ReverseDepsToAdd.back().second);
419 ReverseDepsToAdd.pop_back();
420 }
Owen Anderson2bd46a52007-08-16 21:27:05 +0000421 }
Owen Andersonc772be72007-12-08 01:37:09 +0000422
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000423 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnere99ea702008-11-30 19:24:31 +0000424 AA->deleteValue(RemInst);
Chris Lattner1b185de2008-11-28 22:04:47 +0000425 DEBUG(verifyRemoved(RemInst));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426}
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000427
428/// verifyRemoved - Verify that the specified instruction does not occur
429/// in our internal data structures.
430void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
431 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
432 E = LocalDeps.end(); I != E; ++I) {
433 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnere110a182008-11-30 23:17:19 +0000434 assert(I->second.getInst() != D &&
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000435 "Inst occurs in data structures");
436 }
437
438 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
439 E = NonLocalDeps.end(); I != E; ++I) {
440 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner7dc404d2008-11-30 02:52:26 +0000441 const PerInstNLInfo &INLD = I->second;
442 for (NonLocalDepInfo::iterator II = INLD.getPointer()->begin(),
443 EE = INLD.getPointer()->end(); II != EE; ++II)
Chris Lattnere110a182008-11-30 23:17:19 +0000444 assert(II->second.getInst() != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000445 }
446
447 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000448 E = ReverseLocalDeps.end(); I != E; ++I) {
449 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000450 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
451 EE = I->second.end(); II != EE; ++II)
452 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000453 }
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000454
455 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
456 E = ReverseNonLocalDeps.end();
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000457 I != E; ++I) {
458 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000459 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
460 EE = I->second.end(); II != EE; ++II)
461 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000462 }
Chris Lattner4fb2ce32008-11-29 21:25:10 +0000463}