blob: ea09f03ea66fe2f6550fc1e001e23ba96a135863 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Instructions.h"
Owen Anderson1da67712009-03-09 05:12:38 +000020#include "llvm/IntrinsicInst.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Function.h"
22#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner52638032008-11-28 22:28:27 +000023#include "llvm/ADT/Statistic.h"
Duncan Sands43157d62008-12-10 09:38:36 +000024#include "llvm/ADT/STLExtras.h"
Chris Lattner15853a22008-12-09 06:28:49 +000025#include "llvm/Support/PredIteratorCache.h"
Chris Lattner969470c2008-11-28 21:45:17 +000026#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Target/TargetData.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028using namespace llvm;
29
Chris Lattner46876282008-12-01 01:15:42 +000030STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
31STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
Chris Lattner98a6d802008-11-29 22:02:15 +000032STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Chris Lattnere99d1772008-12-07 08:50:20 +000033
34STATISTIC(NumCacheNonLocalPtr,
35 "Number of fully cached non-local ptr responses");
36STATISTIC(NumCacheDirtyNonLocalPtr,
37 "Number of cached, but dirty, non-local ptr responses");
38STATISTIC(NumUncacheNonLocalPtr,
39 "Number of uncached non-local ptr responses");
Chris Lattner15f378f2008-12-08 07:31:50 +000040STATISTIC(NumCacheCompleteNonLocalPtr,
41 "Number of block queries that were completely cached");
Chris Lattnere99d1772008-12-07 08:50:20 +000042
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043char MemoryDependenceAnalysis::ID = 0;
44
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045// Register this pass...
46static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Chris Lattner969470c2008-11-28 21:45:17 +000047 "Memory Dependence Analysis", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048
Chris Lattner15853a22008-12-09 06:28:49 +000049MemoryDependenceAnalysis::MemoryDependenceAnalysis()
50: FunctionPass(&ID), PredCache(0) {
51}
52MemoryDependenceAnalysis::~MemoryDependenceAnalysis() {
53}
54
55/// Clean up memory in between runs
56void MemoryDependenceAnalysis::releaseMemory() {
57 LocalDeps.clear();
58 NonLocalDeps.clear();
59 NonLocalPointerDeps.clear();
60 ReverseLocalDeps.clear();
61 ReverseNonLocalDeps.clear();
62 ReverseNonLocalPtrDeps.clear();
63 PredCache->clear();
64}
65
66
67
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
69///
70void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
71 AU.setPreservesAll();
72 AU.addRequiredTransitive<AliasAnalysis>();
73 AU.addRequiredTransitive<TargetData>();
74}
75
Chris Lattnere99ea702008-11-30 19:24:31 +000076bool MemoryDependenceAnalysis::runOnFunction(Function &) {
77 AA = &getAnalysis<AliasAnalysis>();
78 TD = &getAnalysis<TargetData>();
Chris Lattner15853a22008-12-09 06:28:49 +000079 if (PredCache == 0)
80 PredCache.reset(new PredIteratorCache());
Chris Lattnere99ea702008-11-30 19:24:31 +000081 return false;
82}
83
Chris Lattner0d67d602008-12-07 18:39:13 +000084/// RemoveFromReverseMap - This is a helper function that removes Val from
85/// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry.
86template <typename KeyTy>
87static void RemoveFromReverseMap(DenseMap<Instruction*,
Chris Lattner1f883112009-03-29 00:24:04 +000088 SmallPtrSet<KeyTy, 4> > &ReverseMap,
89 Instruction *Inst, KeyTy Val) {
90 typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator
Chris Lattner0d67d602008-12-07 18:39:13 +000091 InstIt = ReverseMap.find(Inst);
92 assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");
93 bool Found = InstIt->second.erase(Val);
94 assert(Found && "Invalid reverse map!"); Found=Found;
95 if (InstIt->second.empty())
96 ReverseMap.erase(InstIt);
97}
98
Chris Lattner46876282008-12-01 01:15:42 +000099
Chris Lattnere7653052008-12-07 00:35:51 +0000100/// getCallSiteDependencyFrom - Private helper for finding the local
101/// dependencies of a call site.
Chris Lattnere110a182008-11-30 23:17:19 +0000102MemDepResult MemoryDependenceAnalysis::
Chris Lattner7e0f4462008-12-09 21:19:42 +0000103getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall,
104 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Owen Anderson3de3c532007-08-08 22:26:03 +0000105 // Walk backwards through the block, looking for dependencies
Chris Lattnera5a36c12008-11-29 03:47:00 +0000106 while (ScanIt != BB->begin()) {
107 Instruction *Inst = --ScanIt;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108
109 // If this inst is a memory op, get the pointer it accessed
Chris Lattner18bd2452008-11-29 09:15:21 +0000110 Value *Pointer = 0;
111 uint64_t PointerSize = 0;
112 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
113 Pointer = S->getPointerOperand();
Chris Lattnere99ea702008-11-30 19:24:31 +0000114 PointerSize = TD->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner18bd2452008-11-29 09:15:21 +0000115 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
116 Pointer = V->getOperand(0);
Chris Lattnere99ea702008-11-30 19:24:31 +0000117 PointerSize = TD->getTypeStoreSize(V->getType());
Chris Lattner18bd2452008-11-29 09:15:21 +0000118 } else if (FreeInst *F = dyn_cast<FreeInst>(Inst)) {
119 Pointer = F->getPointerOperand();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
121 // FreeInsts erase the entire structure
Chris Lattnere99d1772008-12-07 08:50:20 +0000122 PointerSize = ~0ULL;
Chris Lattner18bd2452008-11-29 09:15:21 +0000123 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Owen Anderson1da67712009-03-09 05:12:38 +0000124 // Debug intrinsics don't cause dependences.
Dale Johannesenae88e292009-03-11 21:13:01 +0000125 if (isa<DbgInfoIntrinsic>(Inst)) continue;
Chris Lattner4531da82008-12-05 21:04:20 +0000126 CallSite InstCS = CallSite::get(Inst);
127 // If these two calls do not interfere, look past it.
Chris Lattner7e0f4462008-12-09 21:19:42 +0000128 switch (AA->getModRefInfo(CS, InstCS)) {
129 case AliasAnalysis::NoModRef:
130 // If the two calls don't interact (e.g. InstCS is readnone) keep
131 // scanning.
Chris Lattner18bd2452008-11-29 09:15:21 +0000132 continue;
Chris Lattner7e0f4462008-12-09 21:19:42 +0000133 case AliasAnalysis::Ref:
134 // If the two calls read the same memory locations and CS is a readonly
135 // function, then we have two cases: 1) the calls may not interfere with
136 // each other at all. 2) the calls may produce the same value. In case
137 // #1 we want to ignore the values, in case #2, we want to return Inst
138 // as a Def dependence. This allows us to CSE in cases like:
139 // X = strlen(P);
140 // memchr(...);
141 // Y = strlen(P); // Y = X
142 if (isReadOnlyCall) {
143 if (CS.getCalledFunction() != 0 &&
144 CS.getCalledFunction() == InstCS.getCalledFunction())
145 return MemDepResult::getDef(Inst);
146 // Ignore unrelated read/read call dependences.
147 continue;
148 }
149 // FALL THROUGH
150 default:
Chris Lattner4531da82008-12-05 21:04:20 +0000151 return MemDepResult::getClobber(Inst);
Chris Lattner7e0f4462008-12-09 21:19:42 +0000152 }
Chris Lattner6c69a1a2008-11-30 01:44:00 +0000153 } else {
154 // Non-memory instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 continue;
Chris Lattner6c69a1a2008-11-30 01:44:00 +0000156 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
Chris Lattner4531da82008-12-05 21:04:20 +0000158 if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef)
159 return MemDepResult::getClobber(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 }
161
Chris Lattner63b1db42008-12-07 02:15:47 +0000162 // No dependence found. If this is the entry block of the function, it is a
163 // clobber, otherwise it is non-local.
164 if (BB != &BB->getParent()->getEntryBlock())
165 return MemDepResult::getNonLocal();
166 return MemDepResult::getClobber(ScanIt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167}
168
Chris Lattner80853a12008-12-07 01:50:16 +0000169/// getPointerDependencyFrom - Return the instruction on which a memory
170/// location depends. If isLoad is true, this routine ignore may-aliases with
171/// read-only operations.
Chris Lattnere110a182008-11-30 23:17:19 +0000172MemDepResult MemoryDependenceAnalysis::
Chris Lattner80853a12008-12-07 01:50:16 +0000173getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad,
174 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Chris Lattner63b1db42008-12-07 02:15:47 +0000175
Chris Lattnere99d1772008-12-07 08:50:20 +0000176 // Walk backwards through the basic block, looking for dependencies.
Chris Lattnera5a36c12008-11-29 03:47:00 +0000177 while (ScanIt != BB->begin()) {
178 Instruction *Inst = --ScanIt;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000179
Owen Anderson1da67712009-03-09 05:12:38 +0000180 // Debug intrinsics don't cause dependences.
181 if (isa<DbgInfoIntrinsic>(Inst)) continue;
182
Chris Lattner6c69a1a2008-11-30 01:44:00 +0000183 // Values depend on loads if the pointers are must aliased. This means that
184 // a load depends on another must aliased load from the same value.
Chris Lattner4531da82008-12-05 21:04:20 +0000185 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Chris Lattner4531da82008-12-05 21:04:20 +0000186 Value *Pointer = LI->getPointerOperand();
187 uint64_t PointerSize = TD->getTypeStoreSize(LI->getType());
188
189 // If we found a pointer, check if it could be the same as our pointer.
Chris Lattner4103c3c2008-11-29 09:09:48 +0000190 AliasAnalysis::AliasResult R =
Chris Lattnere99ea702008-11-30 19:24:31 +0000191 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
Chris Lattner4103c3c2008-11-29 09:09:48 +0000192 if (R == AliasAnalysis::NoAlias)
193 continue;
194
195 // May-alias loads don't depend on each other without a dependence.
Chris Lattner80853a12008-12-07 01:50:16 +0000196 if (isLoad && R == AliasAnalysis::MayAlias)
Chris Lattner4103c3c2008-11-29 09:09:48 +0000197 continue;
Chris Lattnere99d1772008-12-07 08:50:20 +0000198 // Stores depend on may and must aliased loads, loads depend on must-alias
199 // loads.
Chris Lattner4531da82008-12-05 21:04:20 +0000200 return MemDepResult::getDef(Inst);
201 }
202
203 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chris Lattnerb2bc0522009-05-25 21:28:56 +0000204 // If alias analysis can tell that this store is guaranteed to not modify
205 // the query pointer, ignore it. Use getModRefInfo to handle cases where
206 // the query pointer points to constant memory etc.
207 if (AA->getModRefInfo(SI, MemPtr, MemSize) == AliasAnalysis::NoModRef)
208 continue;
209
210 // Ok, this store might clobber the query pointer. Check to see if it is
211 // a must alias: in this case, we want to return this as a def.
Chris Lattner4531da82008-12-05 21:04:20 +0000212 Value *Pointer = SI->getPointerOperand();
213 uint64_t PointerSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
Chris Lattnerb2bc0522009-05-25 21:28:56 +0000214
Chris Lattner4531da82008-12-05 21:04:20 +0000215 // If we found a pointer, check if it could be the same as our pointer.
216 AliasAnalysis::AliasResult R =
217 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
218
219 if (R == AliasAnalysis::NoAlias)
220 continue;
221 if (R == AliasAnalysis::MayAlias)
222 return MemDepResult::getClobber(Inst);
223 return MemDepResult::getDef(Inst);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 }
Chris Lattner8ea60462008-11-30 01:39:32 +0000225
226 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattner4531da82008-12-05 21:04:20 +0000227 // the allocation, return Def. This means that there is no dependence and
Chris Lattner8ea60462008-11-30 01:39:32 +0000228 // the access can be optimized based on that. For example, a load could
229 // turn into undef.
Chris Lattner4103c3c2008-11-29 09:09:48 +0000230 if (AllocationInst *AI = dyn_cast<AllocationInst>(Inst)) {
Chris Lattner8ea60462008-11-30 01:39:32 +0000231 Value *AccessPtr = MemPtr->getUnderlyingObject();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232
Chris Lattner8ea60462008-11-30 01:39:32 +0000233 if (AccessPtr == AI ||
Chris Lattnere99ea702008-11-30 19:24:31 +0000234 AA->alias(AI, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
Chris Lattner4531da82008-12-05 21:04:20 +0000235 return MemDepResult::getDef(AI);
Chris Lattner8ea60462008-11-30 01:39:32 +0000236 continue;
Chris Lattner4103c3c2008-11-29 09:09:48 +0000237 }
Chris Lattner4103c3c2008-11-29 09:09:48 +0000238
Chris Lattner4531da82008-12-05 21:04:20 +0000239 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
Chris Lattner61544842008-12-09 19:47:40 +0000240 switch (AA->getModRefInfo(Inst, MemPtr, MemSize)) {
241 case AliasAnalysis::NoModRef:
242 // If the call has no effect on the queried pointer, just ignore it.
Chris Lattnerac5d6e92008-11-29 08:51:16 +0000243 continue;
Chris Lattner61544842008-12-09 19:47:40 +0000244 case AliasAnalysis::Ref:
245 // If the call is known to never store to the pointer, and if this is a
246 // load query, we can safely ignore it (scan past it).
247 if (isLoad)
248 continue;
249 // FALL THROUGH.
250 default:
251 // Otherwise, there is a potential dependence. Return a clobber.
252 return MemDepResult::getClobber(Inst);
253 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 }
255
Chris Lattner63b1db42008-12-07 02:15:47 +0000256 // No dependence found. If this is the entry block of the function, it is a
257 // clobber, otherwise it is non-local.
258 if (BB != &BB->getParent()->getEntryBlock())
259 return MemDepResult::getNonLocal();
260 return MemDepResult::getClobber(ScanIt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261}
262
Chris Lattnera5a36c12008-11-29 03:47:00 +0000263/// getDependency - Return the instruction on which a memory operation
264/// depends.
265MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
266 Instruction *ScanPos = QueryInst;
267
268 // Check for a cached result
Chris Lattnere110a182008-11-30 23:17:19 +0000269 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattnera5a36c12008-11-29 03:47:00 +0000270
Chris Lattner98a6d802008-11-29 22:02:15 +0000271 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnere110a182008-11-30 23:17:19 +0000272 // on MemDepResult's default constructing to 'dirty'.
273 if (!LocalCache.isDirty())
274 return LocalCache;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000275
276 // Otherwise, if we have a dirty entry, we know we can start the scan at that
277 // instruction, which may save us some work.
Chris Lattnere110a182008-11-30 23:17:19 +0000278 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattnera5a36c12008-11-29 03:47:00 +0000279 ScanPos = Inst;
Chris Lattner7dc404d2008-11-30 02:52:26 +0000280
Chris Lattner0d67d602008-12-07 18:39:13 +0000281 RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);
Chris Lattner7dc404d2008-11-30 02:52:26 +0000282 }
Chris Lattnera5a36c12008-11-29 03:47:00 +0000283
Chris Lattner80853a12008-12-07 01:50:16 +0000284 BasicBlock *QueryParent = QueryInst->getParent();
285
286 Value *MemPtr = 0;
287 uint64_t MemSize = 0;
288
Chris Lattnera5a36c12008-11-29 03:47:00 +0000289 // Do the scan.
Chris Lattner80853a12008-12-07 01:50:16 +0000290 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
Chris Lattner63b1db42008-12-07 02:15:47 +0000291 // No dependence found. If this is the entry block of the function, it is a
292 // clobber, otherwise it is non-local.
293 if (QueryParent != &QueryParent->getParent()->getEntryBlock())
294 LocalCache = MemDepResult::getNonLocal();
295 else
296 LocalCache = MemDepResult::getClobber(QueryInst);
Chris Lattner80853a12008-12-07 01:50:16 +0000297 } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) {
298 // If this is a volatile store, don't mess around with it. Just return the
299 // previous instruction as a clobber.
300 if (SI->isVolatile())
301 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
302 else {
303 MemPtr = SI->getPointerOperand();
304 MemSize = TD->getTypeStoreSize(SI->getOperand(0)->getType());
305 }
306 } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) {
307 // If this is a volatile load, don't mess around with it. Just return the
308 // previous instruction as a clobber.
309 if (LI->isVolatile())
310 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
311 else {
312 MemPtr = LI->getPointerOperand();
313 MemSize = TD->getTypeStoreSize(LI->getType());
314 }
315 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Chris Lattner7e0f4462008-12-09 21:19:42 +0000316 CallSite QueryCS = CallSite::get(QueryInst);
317 bool isReadOnly = AA->onlyReadsMemory(QueryCS);
318 LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos,
Chris Lattner80853a12008-12-07 01:50:16 +0000319 QueryParent);
320 } else if (FreeInst *FI = dyn_cast<FreeInst>(QueryInst)) {
321 MemPtr = FI->getPointerOperand();
322 // FreeInsts erase the entire structure, not just a field.
323 MemSize = ~0UL;
324 } else {
325 // Non-memory instruction.
326 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
327 }
328
329 // If we need to do a pointer scan, make it happen.
330 if (MemPtr)
331 LocalCache = getPointerDependencyFrom(MemPtr, MemSize,
332 isa<LoadInst>(QueryInst),
333 ScanPos, QueryParent);
Chris Lattnera5a36c12008-11-29 03:47:00 +0000334
335 // Remember the result!
Chris Lattnere110a182008-11-30 23:17:19 +0000336 if (Instruction *I = LocalCache.getInst())
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000337 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattnera5a36c12008-11-29 03:47:00 +0000338
Chris Lattnere110a182008-11-30 23:17:19 +0000339 return LocalCache;
Chris Lattnera5a36c12008-11-29 03:47:00 +0000340}
341
Chris Lattner626471a2009-01-22 07:04:01 +0000342#ifndef NDEBUG
343/// AssertSorted - This method is used when -debug is specified to verify that
344/// cache arrays are properly kept sorted.
345static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
346 int Count = -1) {
347 if (Count == -1) Count = Cache.size();
348 if (Count == 0) return;
349
350 for (unsigned i = 1; i != unsigned(Count); ++i)
351 assert(Cache[i-1] <= Cache[i] && "Cache isn't sorted!");
352}
353#endif
354
Chris Lattner35d5cf52008-12-09 19:38:05 +0000355/// getNonLocalCallDependency - Perform a full dependency query for the
356/// specified call, returning the set of blocks that the value is
Chris Lattner03de6162008-11-30 01:18:27 +0000357/// potentially live across. The returned set of results will include a
358/// "NonLocal" result for all blocks where the value is live across.
359///
Chris Lattner35d5cf52008-12-09 19:38:05 +0000360/// This method assumes the instruction returns a "NonLocal" dependency
Chris Lattner03de6162008-11-30 01:18:27 +0000361/// within its own block.
362///
Chris Lattner35d5cf52008-12-09 19:38:05 +0000363/// This returns a reference to an internal data structure that may be
364/// invalidated on the next non-local query or when an instruction is
365/// removed. Clients must copy this data if they want it around longer than
366/// that.
Chris Lattner46876282008-12-01 01:15:42 +0000367const MemoryDependenceAnalysis::NonLocalDepInfo &
Chris Lattner35d5cf52008-12-09 19:38:05 +0000368MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) {
369 assert(getDependency(QueryCS.getInstruction()).isNonLocal() &&
370 "getNonLocalCallDependency should only be used on calls with non-local deps!");
371 PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()];
Chris Lattner46876282008-12-01 01:15:42 +0000372 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner03de6162008-11-30 01:18:27 +0000373
374 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
375 /// the cached case, this can happen due to instructions being deleted etc. In
376 /// the uncached case, this starts out as the set of predecessors we care
377 /// about.
378 SmallVector<BasicBlock*, 32> DirtyBlocks;
379
380 if (!Cache.empty()) {
Chris Lattner46876282008-12-01 01:15:42 +0000381 // Okay, we have a cache entry. If we know it is not dirty, just return it
382 // with no computation.
383 if (!CacheP.second) {
384 NumCacheNonLocal++;
385 return Cache;
386 }
387
Chris Lattner03de6162008-11-30 01:18:27 +0000388 // If we already have a partially computed set of results, scan them to
Chris Lattner46876282008-12-01 01:15:42 +0000389 // determine what is dirty, seeding our initial DirtyBlocks worklist.
390 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
391 I != E; ++I)
392 if (I->second.isDirty())
393 DirtyBlocks.push_back(I->first);
Chris Lattner03de6162008-11-30 01:18:27 +0000394
Chris Lattner46876282008-12-01 01:15:42 +0000395 // Sort the cache so that we can do fast binary search lookups below.
396 std::sort(Cache.begin(), Cache.end());
Chris Lattner03de6162008-11-30 01:18:27 +0000397
Chris Lattner46876282008-12-01 01:15:42 +0000398 ++NumCacheDirtyNonLocal;
Chris Lattner03de6162008-11-30 01:18:27 +0000399 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
400 // << Cache.size() << " cached: " << *QueryInst;
401 } else {
402 // Seed DirtyBlocks with each of the preds of QueryInst's block.
Chris Lattner35d5cf52008-12-09 19:38:05 +0000403 BasicBlock *QueryBB = QueryCS.getInstruction()->getParent();
Chris Lattner489c58f2008-12-09 06:44:17 +0000404 for (BasicBlock **PI = PredCache->GetPreds(QueryBB); *PI; ++PI)
405 DirtyBlocks.push_back(*PI);
Chris Lattner03de6162008-11-30 01:18:27 +0000406 NumUncacheNonLocal++;
407 }
408
Chris Lattner7e0f4462008-12-09 21:19:42 +0000409 // isReadonlyCall - If this is a read-only call, we can be more aggressive.
410 bool isReadonlyCall = AA->onlyReadsMemory(QueryCS);
Chris Lattnerff851402008-12-15 03:35:32 +0000411
Chris Lattner46876282008-12-01 01:15:42 +0000412 SmallPtrSet<BasicBlock*, 64> Visited;
413
414 unsigned NumSortedEntries = Cache.size();
Chris Lattner626471a2009-01-22 07:04:01 +0000415 DEBUG(AssertSorted(Cache));
Chris Lattner46876282008-12-01 01:15:42 +0000416
Chris Lattner03de6162008-11-30 01:18:27 +0000417 // Iterate while we still have blocks to update.
418 while (!DirtyBlocks.empty()) {
419 BasicBlock *DirtyBB = DirtyBlocks.back();
420 DirtyBlocks.pop_back();
421
Chris Lattner46876282008-12-01 01:15:42 +0000422 // Already processed this block?
423 if (!Visited.insert(DirtyBB))
424 continue;
Chris Lattner03de6162008-11-30 01:18:27 +0000425
Chris Lattner46876282008-12-01 01:15:42 +0000426 // Do a binary search to see if we already have an entry for this block in
427 // the cache set. If so, find it.
Chris Lattner626471a2009-01-22 07:04:01 +0000428 DEBUG(AssertSorted(Cache, NumSortedEntries));
Chris Lattner46876282008-12-01 01:15:42 +0000429 NonLocalDepInfo::iterator Entry =
430 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
431 std::make_pair(DirtyBB, MemDepResult()));
Duncan Sands43157d62008-12-10 09:38:36 +0000432 if (Entry != Cache.begin() && prior(Entry)->first == DirtyBB)
Chris Lattner46876282008-12-01 01:15:42 +0000433 --Entry;
434
435 MemDepResult *ExistingResult = 0;
436 if (Entry != Cache.begin()+NumSortedEntries &&
437 Entry->first == DirtyBB) {
438 // If we already have an entry, and if it isn't already dirty, the block
439 // is done.
440 if (!Entry->second.isDirty())
441 continue;
442
443 // Otherwise, remember this slot so we can update the value.
444 ExistingResult = &Entry->second;
445 }
446
Chris Lattner03de6162008-11-30 01:18:27 +0000447 // If the dirty entry has a pointer, start scanning from it so we don't have
448 // to rescan the entire block.
449 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattner46876282008-12-01 01:15:42 +0000450 if (ExistingResult) {
451 if (Instruction *Inst = ExistingResult->getInst()) {
452 ScanPos = Inst;
Chris Lattner46876282008-12-01 01:15:42 +0000453 // We're removing QueryInst's use of Inst.
Chris Lattner35d5cf52008-12-09 19:38:05 +0000454 RemoveFromReverseMap(ReverseNonLocalDeps, Inst,
455 QueryCS.getInstruction());
Chris Lattner46876282008-12-01 01:15:42 +0000456 }
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000457 }
Chris Lattner03de6162008-11-30 01:18:27 +0000458
Chris Lattnerc0ade852008-11-30 01:26:32 +0000459 // Find out if this block has a local dependency for QueryInst.
Chris Lattner3b35a4d2008-12-07 01:21:14 +0000460 MemDepResult Dep;
Chris Lattner80853a12008-12-07 01:50:16 +0000461
Chris Lattner35d5cf52008-12-09 19:38:05 +0000462 if (ScanPos != DirtyBB->begin()) {
Chris Lattner7e0f4462008-12-09 21:19:42 +0000463 Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB);
Chris Lattner35d5cf52008-12-09 19:38:05 +0000464 } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {
465 // No dependence found. If this is the entry block of the function, it is
466 // a clobber, otherwise it is non-local.
467 Dep = MemDepResult::getNonLocal();
Chris Lattner80853a12008-12-07 01:50:16 +0000468 } else {
Chris Lattner35d5cf52008-12-09 19:38:05 +0000469 Dep = MemDepResult::getClobber(ScanPos);
Chris Lattner80853a12008-12-07 01:50:16 +0000470 }
471
Chris Lattner46876282008-12-01 01:15:42 +0000472 // If we had a dirty entry for the block, update it. Otherwise, just add
473 // a new entry.
474 if (ExistingResult)
475 *ExistingResult = Dep;
476 else
477 Cache.push_back(std::make_pair(DirtyBB, Dep));
478
Chris Lattner03de6162008-11-30 01:18:27 +0000479 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattner46876282008-12-01 01:15:42 +0000480 // the value), remember the association!
481 if (!Dep.isNonLocal()) {
Chris Lattner03de6162008-11-30 01:18:27 +0000482 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
483 // update this when we remove instructions.
Chris Lattner46876282008-12-01 01:15:42 +0000484 if (Instruction *Inst = Dep.getInst())
Chris Lattner35d5cf52008-12-09 19:38:05 +0000485 ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction());
Chris Lattner46876282008-12-01 01:15:42 +0000486 } else {
Chris Lattner03de6162008-11-30 01:18:27 +0000487
Chris Lattner46876282008-12-01 01:15:42 +0000488 // If the block *is* completely transparent to the load, we need to check
489 // the predecessors of this block. Add them to our worklist.
Chris Lattner489c58f2008-12-09 06:44:17 +0000490 for (BasicBlock **PI = PredCache->GetPreds(DirtyBB); *PI; ++PI)
491 DirtyBlocks.push_back(*PI);
Chris Lattner46876282008-12-01 01:15:42 +0000492 }
Chris Lattner03de6162008-11-30 01:18:27 +0000493 }
494
Chris Lattner46876282008-12-01 01:15:42 +0000495 return Cache;
Chris Lattner03de6162008-11-30 01:18:27 +0000496}
497
Chris Lattner63b1db42008-12-07 02:15:47 +0000498/// getNonLocalPointerDependency - Perform a full dependency query for an
499/// access to the specified (non-volatile) memory location, returning the
500/// set of instructions that either define or clobber the value.
501///
502/// This method assumes the pointer has a "NonLocal" dependency within its
503/// own block.
504///
505void MemoryDependenceAnalysis::
506getNonLocalPointerDependency(Value *Pointer, bool isLoad, BasicBlock *FromBB,
507 SmallVectorImpl<NonLocalDepEntry> &Result) {
Chris Lattnerb81d4252008-12-07 18:45:15 +0000508 assert(isa<PointerType>(Pointer->getType()) &&
509 "Can't get pointer deps of a non-pointer!");
Chris Lattneraee23d32008-12-07 02:56:57 +0000510 Result.clear();
511
Chris Lattner63b1db42008-12-07 02:15:47 +0000512 // We know that the pointer value is live into FromBB find the def/clobbers
513 // from presecessors.
Chris Lattner63b1db42008-12-07 02:15:47 +0000514 const Type *EltTy = cast<PointerType>(Pointer->getType())->getElementType();
515 uint64_t PointeeSize = TD->getTypeStoreSize(EltTy);
516
Chris Lattnerff851402008-12-15 03:35:32 +0000517 // This is the set of blocks we've inspected, and the pointer we consider in
518 // each block. Because of critical edges, we currently bail out if querying
519 // a block with multiple different pointers. This can happen during PHI
520 // translation.
521 DenseMap<BasicBlock*, Value*> Visited;
522 if (!getNonLocalPointerDepFromBB(Pointer, PointeeSize, isLoad, FromBB,
523 Result, Visited, true))
524 return;
Chris Lattnerd6421872008-12-15 04:58:29 +0000525 Result.clear();
Chris Lattnerff851402008-12-15 03:35:32 +0000526 Result.push_back(std::make_pair(FromBB,
527 MemDepResult::getClobber(FromBB->begin())));
Chris Lattneraee23d32008-12-07 02:56:57 +0000528}
529
Chris Lattner46b23602008-12-09 07:47:11 +0000530/// GetNonLocalInfoForBlock - Compute the memdep value for BB with
531/// Pointer/PointeeSize using either cached information in Cache or by doing a
532/// lookup (which may use dirty cache info if available). If we do a lookup,
533/// add the result to the cache.
534MemDepResult MemoryDependenceAnalysis::
535GetNonLocalInfoForBlock(Value *Pointer, uint64_t PointeeSize,
536 bool isLoad, BasicBlock *BB,
537 NonLocalDepInfo *Cache, unsigned NumSortedEntries) {
538
539 // Do a binary search to see if we already have an entry for this block in
540 // the cache set. If so, find it.
541 NonLocalDepInfo::iterator Entry =
542 std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries,
543 std::make_pair(BB, MemDepResult()));
Duncan Sands43157d62008-12-10 09:38:36 +0000544 if (Entry != Cache->begin() && prior(Entry)->first == BB)
Chris Lattner46b23602008-12-09 07:47:11 +0000545 --Entry;
546
547 MemDepResult *ExistingResult = 0;
548 if (Entry != Cache->begin()+NumSortedEntries && Entry->first == BB)
549 ExistingResult = &Entry->second;
550
551 // If we have a cached entry, and it is non-dirty, use it as the value for
552 // this dependency.
553 if (ExistingResult && !ExistingResult->isDirty()) {
554 ++NumCacheNonLocalPtr;
555 return *ExistingResult;
556 }
557
558 // Otherwise, we have to scan for the value. If we have a dirty cache
559 // entry, start scanning from its position, otherwise we scan from the end
560 // of the block.
561 BasicBlock::iterator ScanPos = BB->end();
562 if (ExistingResult && ExistingResult->getInst()) {
563 assert(ExistingResult->getInst()->getParent() == BB &&
564 "Instruction invalidated?");
565 ++NumCacheDirtyNonLocalPtr;
566 ScanPos = ExistingResult->getInst();
567
568 // Eliminating the dirty entry from 'Cache', so update the reverse info.
569 ValueIsLoadPair CacheKey(Pointer, isLoad);
Chris Lattner1f883112009-03-29 00:24:04 +0000570 RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey);
Chris Lattner46b23602008-12-09 07:47:11 +0000571 } else {
572 ++NumUncacheNonLocalPtr;
573 }
574
575 // Scan the block for the dependency.
576 MemDepResult Dep = getPointerDependencyFrom(Pointer, PointeeSize, isLoad,
577 ScanPos, BB);
578
579 // If we had a dirty entry for the block, update it. Otherwise, just add
580 // a new entry.
581 if (ExistingResult)
582 *ExistingResult = Dep;
583 else
584 Cache->push_back(std::make_pair(BB, Dep));
585
586 // If the block has a dependency (i.e. it isn't completely transparent to
587 // the value), remember the reverse association because we just added it
588 // to Cache!
589 if (Dep.isNonLocal())
590 return Dep;
591
592 // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently
593 // update MemDep when we remove instructions.
594 Instruction *Inst = Dep.getInst();
595 assert(Inst && "Didn't depend on anything?");
596 ValueIsLoadPair CacheKey(Pointer, isLoad);
Chris Lattner1f883112009-03-29 00:24:04 +0000597 ReverseNonLocalPtrDeps[Inst].insert(CacheKey);
Chris Lattner46b23602008-12-09 07:47:11 +0000598 return Dep;
599}
600
Chris Lattner22d1db52009-07-13 17:20:05 +0000601/// SortNonLocalDepInfoCache - Sort the a NonLocalDepInfo cache, given a certain
602/// number of elements in the array that are already properly ordered. This is
603/// optimized for the case when only a few entries are added.
604static void
605SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
606 unsigned NumSortedEntries) {
607 switch (Cache.size() - NumSortedEntries) {
608 case 0:
609 // done, no new entries.
610 break;
611 case 2: {
612 // Two new entries, insert the last one into place.
613 MemoryDependenceAnalysis::NonLocalDepEntry Val = Cache.back();
614 Cache.pop_back();
615 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
616 std::upper_bound(Cache.begin(), Cache.end()-1, Val);
617 Cache.insert(Entry, Val);
618 // FALL THROUGH.
619 }
620 case 1:
621 // One new entry, Just insert the new value at the appropriate position.
622 if (Cache.size() != 1) {
623 MemoryDependenceAnalysis::NonLocalDepEntry Val = Cache.back();
624 Cache.pop_back();
625 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
626 std::upper_bound(Cache.begin(), Cache.end(), Val);
627 Cache.insert(Entry, Val);
628 }
629 break;
630 default:
631 // Added many values, do a full scale sort.
632 std::sort(Cache.begin(), Cache.end());
633 break;
634 }
635}
636
Chris Lattner46b23602008-12-09 07:47:11 +0000637
Chris Lattnerff851402008-12-15 03:35:32 +0000638/// getNonLocalPointerDepFromBB - Perform a dependency query based on
639/// pointer/pointeesize starting at the end of StartBB. Add any clobber/def
640/// results to the results vector and keep track of which blocks are visited in
641/// 'Visited'.
642///
643/// This has special behavior for the first block queries (when SkipFirstBlock
644/// is true). In this special case, it ignores the contents of the specified
645/// block and starts returning dependence info for its predecessors.
646///
647/// This function returns false on success, or true to indicate that it could
648/// not compute dependence information for some reason. This should be treated
649/// as a clobber dependence on the first instruction in the predecessor block.
650bool MemoryDependenceAnalysis::
Chris Lattner46b23602008-12-09 07:47:11 +0000651getNonLocalPointerDepFromBB(Value *Pointer, uint64_t PointeeSize,
652 bool isLoad, BasicBlock *StartBB,
653 SmallVectorImpl<NonLocalDepEntry> &Result,
Chris Lattnerff851402008-12-15 03:35:32 +0000654 DenseMap<BasicBlock*, Value*> &Visited,
655 bool SkipFirstBlock) {
656
Chris Lattnere99d1772008-12-07 08:50:20 +0000657 // Look up the cached info for Pointer.
658 ValueIsLoadPair CacheKey(Pointer, isLoad);
Chris Lattner15f378f2008-12-08 07:31:50 +0000659
Chris Lattnerff851402008-12-15 03:35:32 +0000660 std::pair<BBSkipFirstBlockPair, NonLocalDepInfo> *CacheInfo =
661 &NonLocalPointerDeps[CacheKey];
662 NonLocalDepInfo *Cache = &CacheInfo->second;
Chris Lattner15f378f2008-12-08 07:31:50 +0000663
664 // If we have valid cached information for exactly the block we are
665 // investigating, just return it with no recomputation.
Chris Lattnerff851402008-12-15 03:35:32 +0000666 if (CacheInfo->first == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) {
Chris Lattner7a5a0c22008-12-16 07:10:09 +0000667 // We have a fully cached result for this query then we can just return the
668 // cached results and populate the visited set. However, we have to verify
669 // that we don't already have conflicting results for these blocks. Check
670 // to ensure that if a block in the results set is in the visited set that
671 // it was for the same pointer query.
672 if (!Visited.empty()) {
673 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
674 I != E; ++I) {
675 DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->first);
676 if (VI == Visited.end() || VI->second == Pointer) continue;
677
678 // We have a pointer mismatch in a block. Just return clobber, saying
679 // that something was clobbered in this result. We could also do a
680 // non-fully cached query, but there is little point in doing this.
681 return true;
682 }
683 }
684
Chris Lattner15f378f2008-12-08 07:31:50 +0000685 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
Chris Lattner7a5a0c22008-12-16 07:10:09 +0000686 I != E; ++I) {
687 Visited.insert(std::make_pair(I->first, Pointer));
Chris Lattner15f378f2008-12-08 07:31:50 +0000688 if (!I->second.isNonLocal())
689 Result.push_back(*I);
Chris Lattner7a5a0c22008-12-16 07:10:09 +0000690 }
Chris Lattner15f378f2008-12-08 07:31:50 +0000691 ++NumCacheCompleteNonLocalPtr;
Chris Lattnerff851402008-12-15 03:35:32 +0000692 return false;
Chris Lattner15f378f2008-12-08 07:31:50 +0000693 }
694
695 // Otherwise, either this is a new block, a block with an invalid cache
696 // pointer or one that we're about to invalidate by putting more info into it
697 // than its valid cache info. If empty, the result will be valid cache info,
698 // otherwise it isn't.
Chris Lattnerff851402008-12-15 03:35:32 +0000699 if (Cache->empty())
700 CacheInfo->first = BBSkipFirstBlockPair(StartBB, SkipFirstBlock);
701 else
702 CacheInfo->first = BBSkipFirstBlockPair();
Chris Lattner15f378f2008-12-08 07:31:50 +0000703
704 SmallVector<BasicBlock*, 32> Worklist;
705 Worklist.push_back(StartBB);
Chris Lattnere99d1772008-12-07 08:50:20 +0000706
707 // Keep track of the entries that we know are sorted. Previously cached
708 // entries will all be sorted. The entries we add we only sort on demand (we
709 // don't insert every element into its sorted position). We know that we
710 // won't get any reuse from currently inserted values, because we don't
711 // revisit blocks after we insert info for them.
712 unsigned NumSortedEntries = Cache->size();
Chris Lattner626471a2009-01-22 07:04:01 +0000713 DEBUG(AssertSorted(*Cache));
Chris Lattnere99d1772008-12-07 08:50:20 +0000714
Chris Lattner63b1db42008-12-07 02:15:47 +0000715 while (!Worklist.empty()) {
Chris Lattneraee23d32008-12-07 02:56:57 +0000716 BasicBlock *BB = Worklist.pop_back_val();
Chris Lattner63b1db42008-12-07 02:15:47 +0000717
Chris Lattnerbef2c8b2008-12-09 07:52:59 +0000718 // Skip the first block if we have it.
Chris Lattnerff851402008-12-15 03:35:32 +0000719 if (!SkipFirstBlock) {
Chris Lattnerbef2c8b2008-12-09 07:52:59 +0000720 // Analyze the dependency of *Pointer in FromBB. See if we already have
721 // been here.
Chris Lattnerff851402008-12-15 03:35:32 +0000722 assert(Visited.count(BB) && "Should check 'visited' before adding to WL");
Chris Lattnere99d1772008-12-07 08:50:20 +0000723
Chris Lattnerbef2c8b2008-12-09 07:52:59 +0000724 // Get the dependency info for Pointer in BB. If we have cached
725 // information, we will use it, otherwise we compute it.
Chris Lattner626471a2009-01-22 07:04:01 +0000726 DEBUG(AssertSorted(*Cache, NumSortedEntries));
Chris Lattnerbef2c8b2008-12-09 07:52:59 +0000727 MemDepResult Dep = GetNonLocalInfoForBlock(Pointer, PointeeSize, isLoad,
728 BB, Cache, NumSortedEntries);
729
730 // If we got a Def or Clobber, add this to the list of results.
731 if (!Dep.isNonLocal()) {
732 Result.push_back(NonLocalDepEntry(BB, Dep));
733 continue;
734 }
Chris Lattner63b1db42008-12-07 02:15:47 +0000735 }
736
Chris Lattnerff851402008-12-15 03:35:32 +0000737 // If 'Pointer' is an instruction defined in this block, then we need to do
738 // phi translation to change it into a value live in the predecessor block.
739 // If phi translation fails, then we can't continue dependence analysis.
740 Instruction *PtrInst = dyn_cast<Instruction>(Pointer);
741 bool NeedsPHITranslation = PtrInst && PtrInst->getParent() == BB;
742
743 // If no PHI translation is needed, just add all the predecessors of this
744 // block to scan them as well.
745 if (!NeedsPHITranslation) {
746 SkipFirstBlock = false;
747 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
748 // Verify that we haven't looked at this block yet.
749 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
750 InsertRes = Visited.insert(std::make_pair(*PI, Pointer));
751 if (InsertRes.second) {
752 // First time we've looked at *PI.
753 Worklist.push_back(*PI);
754 continue;
755 }
756
757 // If we have seen this block before, but it was with a different
758 // pointer then we have a phi translation failure and we have to treat
759 // this as a clobber.
760 if (InsertRes.first->second != Pointer)
761 goto PredTranslationFailure;
762 }
763 continue;
764 }
765
766 // If we do need to do phi translation, then there are a bunch of different
767 // cases, because we have to find a Value* live in the predecessor block. We
768 // know that PtrInst is defined in this block at least.
Chris Lattner4a1a6f62009-07-13 17:14:23 +0000769
770 // We may have added values to the cache list before this PHI translation.
771 // If so, we haven't done anything to ensure that the cache remains sorted.
772 // Sort it now (if needed) so that recursive invocations of
773 // getNonLocalPointerDepFromBB and other routines that could reuse the cache
774 // value will only see properly sorted cache arrays.
775 if (Cache && NumSortedEntries != Cache->size()) {
Chris Lattner22d1db52009-07-13 17:20:05 +0000776 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner4a1a6f62009-07-13 17:14:23 +0000777 NumSortedEntries = Cache->size();
778 }
Chris Lattnerff851402008-12-15 03:35:32 +0000779
780 // If this is directly a PHI node, just use the incoming values for each
781 // pred as the phi translated version.
782 if (PHINode *PtrPHI = dyn_cast<PHINode>(PtrInst)) {
Chris Lattner4a1a6f62009-07-13 17:14:23 +0000783 Cache = 0;
784
Chris Lattner626471a2009-01-22 07:04:01 +0000785 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
Chris Lattnerff851402008-12-15 03:35:32 +0000786 BasicBlock *Pred = *PI;
787 Value *PredPtr = PtrPHI->getIncomingValueForBlock(Pred);
788
789 // Check to see if we have already visited this pred block with another
790 // pointer. If so, we can't do this lookup. This failure can occur
791 // with PHI translation when a critical edge exists and the PHI node in
792 // the successor translates to a pointer value different than the
793 // pointer the block was first analyzed with.
794 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
795 InsertRes = Visited.insert(std::make_pair(Pred, PredPtr));
796
797 if (!InsertRes.second) {
798 // If the predecessor was visited with PredPtr, then we already did
799 // the analysis and can ignore it.
800 if (InsertRes.first->second == PredPtr)
801 continue;
802
803 // Otherwise, the block was previously analyzed with a different
804 // pointer. We can't represent the result of this case, so we just
805 // treat this as a phi translation failure.
806 goto PredTranslationFailure;
807 }
Chris Lattner626471a2009-01-22 07:04:01 +0000808
Chris Lattner626471a2009-01-22 07:04:01 +0000809 // FIXME: it is entirely possible that PHI translating will end up with
810 // the same value. Consider PHI translating something like:
811 // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need*
812 // to recurse here, pedantically speaking.
Chris Lattnerff851402008-12-15 03:35:32 +0000813
814 // If we have a problem phi translating, fall through to the code below
815 // to handle the failure condition.
816 if (getNonLocalPointerDepFromBB(PredPtr, PointeeSize, isLoad, Pred,
817 Result, Visited))
818 goto PredTranslationFailure;
819 }
Chris Lattner4a1a6f62009-07-13 17:14:23 +0000820
Chris Lattnerff851402008-12-15 03:35:32 +0000821 // Refresh the CacheInfo/Cache pointer so that it isn't invalidated.
822 CacheInfo = &NonLocalPointerDeps[CacheKey];
823 Cache = &CacheInfo->second;
Chris Lattner626471a2009-01-22 07:04:01 +0000824 NumSortedEntries = Cache->size();
Chris Lattnerd2f20142009-01-23 00:27:03 +0000825
Chris Lattnerff851402008-12-15 03:35:32 +0000826 // Since we did phi translation, the "Cache" set won't contain all of the
827 // results for the query. This is ok (we can still use it to accelerate
828 // specific block queries) but we can't do the fastpath "return all
829 // results from the set" Clear out the indicator for this.
830 CacheInfo->first = BBSkipFirstBlockPair();
831 SkipFirstBlock = false;
832 continue;
833 }
834
835 // TODO: BITCAST, GEP.
836
837 // cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
838 // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
839 // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
840 PredTranslationFailure:
841
Chris Lattner6c233d12009-01-23 07:12:16 +0000842 if (Cache == 0) {
843 // Refresh the CacheInfo/Cache pointer if it got invalidated.
844 CacheInfo = &NonLocalPointerDeps[CacheKey];
845 Cache = &CacheInfo->second;
846 NumSortedEntries = Cache->size();
Chris Lattner6c233d12009-01-23 07:12:16 +0000847 }
Chris Lattner4a1a6f62009-07-13 17:14:23 +0000848
Chris Lattnerff851402008-12-15 03:35:32 +0000849 // Since we did phi translation, the "Cache" set won't contain all of the
850 // results for the query. This is ok (we can still use it to accelerate
851 // specific block queries) but we can't do the fastpath "return all
852 // results from the set" Clear out the indicator for this.
853 CacheInfo->first = BBSkipFirstBlockPair();
854
855 // If *nothing* works, mark the pointer as being clobbered by the first
856 // instruction in this block.
857 //
858 // If this is the magic first block, return this as a clobber of the whole
859 // incoming value. Since we can't phi translate to one of the predecessors,
860 // we have to bail out.
861 if (SkipFirstBlock)
862 return true;
863
864 for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) {
865 assert(I != Cache->rend() && "Didn't find current block??");
866 if (I->first != BB)
867 continue;
868
869 assert(I->second.isNonLocal() &&
870 "Should only be here with transparent block");
871 I->second = MemDepResult::getClobber(BB->begin());
Chris Lattner1f883112009-03-29 00:24:04 +0000872 ReverseNonLocalPtrDeps[BB->begin()].insert(CacheKey);
Chris Lattnerff851402008-12-15 03:35:32 +0000873 Result.push_back(*I);
874 break;
Chris Lattneraee23d32008-12-07 02:56:57 +0000875 }
Chris Lattner63b1db42008-12-07 02:15:47 +0000876 }
Chris Lattner6c233d12009-01-23 07:12:16 +0000877
Chris Lattner46b23602008-12-09 07:47:11 +0000878 // Okay, we're done now. If we added new values to the cache, re-sort it.
Chris Lattner22d1db52009-07-13 17:20:05 +0000879 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner626471a2009-01-22 07:04:01 +0000880 DEBUG(AssertSorted(*Cache));
Chris Lattnerff851402008-12-15 03:35:32 +0000881 return false;
Chris Lattnere99d1772008-12-07 08:50:20 +0000882}
883
884/// RemoveCachedNonLocalPointerDependencies - If P exists in
885/// CachedNonLocalPointerInfo, remove it.
886void MemoryDependenceAnalysis::
887RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) {
888 CachedNonLocalPointerInfo::iterator It =
889 NonLocalPointerDeps.find(P);
890 if (It == NonLocalPointerDeps.end()) return;
891
892 // Remove all of the entries in the BB->val map. This involves removing
893 // instructions from the reverse map.
Chris Lattner15f378f2008-12-08 07:31:50 +0000894 NonLocalDepInfo &PInfo = It->second.second;
Chris Lattnere99d1772008-12-07 08:50:20 +0000895
896 for (unsigned i = 0, e = PInfo.size(); i != e; ++i) {
897 Instruction *Target = PInfo[i].second.getInst();
898 if (Target == 0) continue; // Ignore non-local dep results.
Chris Lattner3eb46822008-12-09 22:45:32 +0000899 assert(Target->getParent() == PInfo[i].first);
Chris Lattnere99d1772008-12-07 08:50:20 +0000900
901 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Chris Lattner1f883112009-03-29 00:24:04 +0000902 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P);
Chris Lattnere99d1772008-12-07 08:50:20 +0000903 }
904
905 // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).
906 NonLocalPointerDeps.erase(It);
Chris Lattner63b1db42008-12-07 02:15:47 +0000907}
908
909
Chris Lattnerf81b0142008-12-09 22:06:23 +0000910/// invalidateCachedPointerInfo - This method is used to invalidate cached
911/// information about the specified pointer, because it may be too
912/// conservative in memdep. This is an optional call that can be used when
913/// the client detects an equivalence between the pointer and some other
914/// value and replaces the other value with ptr. This can make Ptr available
915/// in more places that cached info does not necessarily keep.
916void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) {
917 // If Ptr isn't really a pointer, just ignore it.
918 if (!isa<PointerType>(Ptr->getType())) return;
919 // Flush store info for the pointer.
920 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false));
921 // Flush load info for the pointer.
922 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true));
923}
924
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925/// removeInstruction - Remove an instruction from the dependence analysis,
926/// updating the dependence of instructions that previously depended on it.
Owen Anderson3de3c532007-08-08 22:26:03 +0000927/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner1b185de2008-11-28 22:04:47 +0000928void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner1b185de2008-11-28 22:04:47 +0000929 // Walk through the Non-local dependencies, removing this one as the value
930 // for any cached queries.
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000931 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
932 if (NLDI != NonLocalDeps.end()) {
Chris Lattner46876282008-12-01 01:15:42 +0000933 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattnerff3342f2008-11-30 02:30:50 +0000934 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
935 DI != DE; ++DI)
Chris Lattnere110a182008-11-30 23:17:19 +0000936 if (Instruction *Inst = DI->second.getInst())
Chris Lattner0d67d602008-12-07 18:39:13 +0000937 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000938 NonLocalDeps.erase(NLDI);
939 }
Owen Andersonc772be72007-12-08 01:37:09 +0000940
Chris Lattner1b185de2008-11-28 22:04:47 +0000941 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattner52638032008-11-28 22:28:27 +0000942 //
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000943 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
944 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000945 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattner0d67d602008-12-07 18:39:13 +0000946 if (Instruction *Inst = LocalDepEntry->second.getInst())
947 RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000948
Chris Lattner52638032008-11-28 22:28:27 +0000949 // Remove this local dependency info.
Chris Lattnerfd9b56d2008-11-29 01:43:36 +0000950 LocalDeps.erase(LocalDepEntry);
Chris Lattnere99d1772008-12-07 08:50:20 +0000951 }
952
953 // If we have any cached pointer dependencies on this instruction, remove
954 // them. If the instruction has non-pointer type, then it can't be a pointer
955 // base.
956
957 // Remove it from both the load info and the store info. The instruction
958 // can't be in either of these maps if it is non-pointer.
959 if (isa<PointerType>(RemInst->getType())) {
960 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));
961 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));
962 }
Chris Lattner52638032008-11-28 22:28:27 +0000963
Chris Lattner89fbbe72008-11-28 22:51:08 +0000964 // Loop over all of the things that depend on the instruction we're removing.
965 //
Chris Lattner75cbaf82008-11-29 23:30:39 +0000966 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
Chris Lattner4a15a662008-12-07 18:42:51 +0000967
968 // If we find RemInst as a clobber or Def in any of the maps for other values,
969 // we need to replace its entry with a dirty version of the instruction after
970 // it. If RemInst is a terminator, we use a null dirty value.
971 //
972 // Using a dirty version of the instruction after RemInst saves having to scan
973 // the entire block to get to this point.
974 MemDepResult NewDirtyVal;
975 if (!RemInst->isTerminator())
976 NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst));
Chris Lattner75cbaf82008-11-29 23:30:39 +0000977
Chris Lattner83c1a7c2008-11-29 09:20:15 +0000978 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
979 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattner89fbbe72008-11-28 22:51:08 +0000980 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattnere99d1772008-12-07 08:50:20 +0000981 // RemInst can't be the terminator if it has local stuff depending on it.
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000982 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
983 "Nothing can locally depend on a terminator");
984
Chris Lattner89fbbe72008-11-28 22:51:08 +0000985 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
986 E = ReverseDeps.end(); I != E; ++I) {
987 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf9e6b432008-11-30 02:28:25 +0000988 assert(InstDependingOnRemInst != RemInst &&
989 "Already removed our local dep info");
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000990
Chris Lattner4a15a662008-12-07 18:42:51 +0000991 LocalDeps[InstDependingOnRemInst] = NewDirtyVal;
Chris Lattner89fbbe72008-11-28 22:51:08 +0000992
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000993 // Make sure to remember that new things depend on NewDepInst.
Chris Lattner4a15a662008-12-07 18:42:51 +0000994 assert(NewDirtyVal.getInst() && "There is no way something else can have "
995 "a local dep on this if it is a terminator!");
996 ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(),
Chris Lattner1dd8aba2008-11-30 01:09:30 +0000997 InstDependingOnRemInst));
Chris Lattner89fbbe72008-11-28 22:51:08 +0000998 }
Chris Lattner75cbaf82008-11-29 23:30:39 +0000999
1000 ReverseLocalDeps.erase(ReverseDepIt);
1001
1002 // Add new reverse deps after scanning the set, to avoid invalidating the
1003 // 'ReverseDeps' reference.
1004 while (!ReverseDepsToAdd.empty()) {
1005 ReverseLocalDeps[ReverseDepsToAdd.back().first]
1006 .insert(ReverseDepsToAdd.back().second);
1007 ReverseDepsToAdd.pop_back();
1008 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001009 }
Owen Anderson2bd46a52007-08-16 21:27:05 +00001010
Chris Lattner83c1a7c2008-11-29 09:20:15 +00001011 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
1012 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattnere99d1772008-12-07 08:50:20 +00001013 SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second;
1014 for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end();
Chris Lattnerf9e6b432008-11-30 02:28:25 +00001015 I != E; ++I) {
1016 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
1017
Chris Lattner7dc404d2008-11-30 02:52:26 +00001018 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner7dc404d2008-11-30 02:52:26 +00001019 // The information is now dirty!
Chris Lattner46876282008-12-01 01:15:42 +00001020 INLD.second = true;
Chris Lattnerf9e6b432008-11-30 02:28:25 +00001021
Chris Lattner46876282008-12-01 01:15:42 +00001022 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
1023 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnere110a182008-11-30 23:17:19 +00001024 if (DI->second.getInst() != RemInst) continue;
Chris Lattnerf9e6b432008-11-30 02:28:25 +00001025
1026 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner4a15a662008-12-07 18:42:51 +00001027 DI->second = NewDirtyVal;
1028
1029 if (Instruction *NextI = NewDirtyVal.getInst())
Chris Lattnerf9e6b432008-11-30 02:28:25 +00001030 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattnerf9e6b432008-11-30 02:28:25 +00001031 }
1032 }
Chris Lattner75cbaf82008-11-29 23:30:39 +00001033
1034 ReverseNonLocalDeps.erase(ReverseDepIt);
1035
Chris Lattner98a6d802008-11-29 22:02:15 +00001036 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
1037 while (!ReverseDepsToAdd.empty()) {
1038 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
1039 .insert(ReverseDepsToAdd.back().second);
1040 ReverseDepsToAdd.pop_back();
1041 }
Owen Anderson2bd46a52007-08-16 21:27:05 +00001042 }
Owen Andersonc772be72007-12-08 01:37:09 +00001043
Chris Lattnere99d1772008-12-07 08:50:20 +00001044 // If the instruction is in ReverseNonLocalPtrDeps then it appears as a
1045 // value in the NonLocalPointerDeps info.
1046 ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =
1047 ReverseNonLocalPtrDeps.find(RemInst);
1048 if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {
Chris Lattner1f883112009-03-29 00:24:04 +00001049 SmallPtrSet<ValueIsLoadPair, 4> &Set = ReversePtrDepIt->second;
Chris Lattnere99d1772008-12-07 08:50:20 +00001050 SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd;
1051
Chris Lattner1f883112009-03-29 00:24:04 +00001052 for (SmallPtrSet<ValueIsLoadPair, 4>::iterator I = Set.begin(),
1053 E = Set.end(); I != E; ++I) {
1054 ValueIsLoadPair P = *I;
Chris Lattnere99d1772008-12-07 08:50:20 +00001055 assert(P.getPointer() != RemInst &&
1056 "Already removed NonLocalPointerDeps info for RemInst");
1057
Chris Lattner15f378f2008-12-08 07:31:50 +00001058 NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].second;
1059
1060 // The cache is not valid for any specific block anymore.
Chris Lattnerff851402008-12-15 03:35:32 +00001061 NonLocalPointerDeps[P].first = BBSkipFirstBlockPair();
Chris Lattnere99d1772008-12-07 08:50:20 +00001062
Chris Lattnere99d1772008-12-07 08:50:20 +00001063 // Update any entries for RemInst to use the instruction after it.
1064 for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end();
1065 DI != DE; ++DI) {
1066 if (DI->second.getInst() != RemInst) continue;
1067
1068 // Convert to a dirty entry for the subsequent instruction.
1069 DI->second = NewDirtyVal;
1070
1071 if (Instruction *NewDirtyInst = NewDirtyVal.getInst())
1072 ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));
1073 }
Chris Lattner6c233d12009-01-23 07:12:16 +00001074
1075 // Re-sort the NonLocalDepInfo. Changing the dirty entry to its
1076 // subsequent value may invalidate the sortedness.
1077 std::sort(NLPDI.begin(), NLPDI.end());
Chris Lattnere99d1772008-12-07 08:50:20 +00001078 }
1079
1080 ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);
1081
1082 while (!ReversePtrDepsToAdd.empty()) {
1083 ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first]
Chris Lattner1f883112009-03-29 00:24:04 +00001084 .insert(ReversePtrDepsToAdd.back().second);
Chris Lattnere99d1772008-12-07 08:50:20 +00001085 ReversePtrDepsToAdd.pop_back();
1086 }
1087 }
1088
1089
Chris Lattnerf9e6b432008-11-30 02:28:25 +00001090 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnere99ea702008-11-30 19:24:31 +00001091 AA->deleteValue(RemInst);
Chris Lattner1b185de2008-11-28 22:04:47 +00001092 DEBUG(verifyRemoved(RemInst));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093}
Chris Lattner4fb2ce32008-11-29 21:25:10 +00001094/// verifyRemoved - Verify that the specified instruction does not occur
1095/// in our internal data structures.
1096void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
1097 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
1098 E = LocalDeps.end(); I != E; ++I) {
1099 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnere110a182008-11-30 23:17:19 +00001100 assert(I->second.getInst() != D &&
Chris Lattner4fb2ce32008-11-29 21:25:10 +00001101 "Inst occurs in data structures");
1102 }
1103
Chris Lattnere99d1772008-12-07 08:50:20 +00001104 for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(),
1105 E = NonLocalPointerDeps.end(); I != E; ++I) {
1106 assert(I->first.getPointer() != D && "Inst occurs in NLPD map key");
Chris Lattner15f378f2008-12-08 07:31:50 +00001107 const NonLocalDepInfo &Val = I->second.second;
Chris Lattnere99d1772008-12-07 08:50:20 +00001108 for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end();
1109 II != E; ++II)
1110 assert(II->second.getInst() != D && "Inst occurs as NLPD value");
1111 }
1112
Chris Lattner4fb2ce32008-11-29 21:25:10 +00001113 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
1114 E = NonLocalDeps.end(); I != E; ++I) {
1115 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner7dc404d2008-11-30 02:52:26 +00001116 const PerInstNLInfo &INLD = I->second;
Chris Lattner46876282008-12-01 01:15:42 +00001117 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
1118 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnere110a182008-11-30 23:17:19 +00001119 assert(II->second.getInst() != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +00001120 }
1121
1122 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf9e6b432008-11-30 02:28:25 +00001123 E = ReverseLocalDeps.end(); I != E; ++I) {
1124 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +00001125 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1126 EE = I->second.end(); II != EE; ++II)
1127 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf9e6b432008-11-30 02:28:25 +00001128 }
Chris Lattner4fb2ce32008-11-29 21:25:10 +00001129
1130 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
1131 E = ReverseNonLocalDeps.end();
Chris Lattnerf9e6b432008-11-30 02:28:25 +00001132 I != E; ++I) {
1133 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4fb2ce32008-11-29 21:25:10 +00001134 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1135 EE = I->second.end(); II != EE; ++II)
1136 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf9e6b432008-11-30 02:28:25 +00001137 }
Chris Lattnere99d1772008-12-07 08:50:20 +00001138
1139 for (ReverseNonLocalPtrDepTy::const_iterator
1140 I = ReverseNonLocalPtrDeps.begin(),
1141 E = ReverseNonLocalPtrDeps.end(); I != E; ++I) {
1142 assert(I->first != D && "Inst occurs in rev NLPD map");
1143
Chris Lattner1f883112009-03-29 00:24:04 +00001144 for (SmallPtrSet<ValueIsLoadPair, 4>::const_iterator II = I->second.begin(),
Chris Lattnere99d1772008-12-07 08:50:20 +00001145 E = I->second.end(); II != E; ++II)
Chris Lattner1f883112009-03-29 00:24:04 +00001146 assert(*II != ValueIsLoadPair(D, false) &&
1147 *II != ValueIsLoadPair(D, true) &&
Chris Lattnere99d1772008-12-07 08:50:20 +00001148 "Inst occurs in ReverseNonLocalPtrDeps map");
1149 }
1150
Chris Lattner4fb2ce32008-11-29 21:25:10 +00001151}