blob: 582ae0a6146417c04925dac9b8de14adf5c9e779 [file] [log] [blame]
Owen Anderson78e02f72007-07-06 23:14:35 +00001//===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Owen Anderson78e02f72007-07-06 23:14:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements an analysis that determines, for a given memory
11// operation, what preceding memory operations it depends on. It builds on
Owen Anderson80b1f092007-08-08 22:01:54 +000012// alias analysis information, and tries to provide a lazy, caching interface to
Owen Anderson78e02f72007-07-06 23:14:35 +000013// a common kind of alias information query.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner0e575f42008-11-28 21:45:17 +000017#define DEBUG_TYPE "memdep"
Owen Anderson78e02f72007-07-06 23:14:35 +000018#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Chris Lattnercb5fd742011-04-26 22:42:01 +000019#include "llvm/Analysis/ValueTracking.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000020#include "llvm/Instructions.h"
Owen Andersonf6cec852009-03-09 05:12:38 +000021#include "llvm/IntrinsicInst.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000022#include "llvm/Function.h"
Dan Gohmanc1ac0d72010-09-22 21:41:02 +000023#include "llvm/LLVMContext.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000024#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner6f7b2102009-11-27 22:05:15 +000025#include "llvm/Analysis/Dominators.h"
Chris Lattnere19e4ba2009-11-27 00:34:38 +000026#include "llvm/Analysis/InstructionSimplify.h"
Victor Hernandezf006b182009-10-27 20:05:49 +000027#include "llvm/Analysis/MemoryBuiltins.h"
Chris Lattner05e15f82009-12-09 01:59:31 +000028#include "llvm/Analysis/PHITransAddr.h"
Dan Gohman5034dd32010-12-15 20:02:24 +000029#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerbaad8882008-11-28 22:28:27 +000030#include "llvm/ADT/Statistic.h"
Duncan Sands7050f3d2008-12-10 09:38:36 +000031#include "llvm/ADT/STLExtras.h"
Chris Lattner4012fdd2008-12-09 06:28:49 +000032#include "llvm/Support/PredIteratorCache.h"
Chris Lattner0e575f42008-11-28 21:45:17 +000033#include "llvm/Support/Debug.h"
Benjamin Kramerdd061b22010-11-21 15:21:46 +000034#include "llvm/Target/TargetData.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000035using namespace llvm;
36
Chris Lattnerbf145d62008-12-01 01:15:42 +000037STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
38STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
Chris Lattner0ec48dd2008-11-29 22:02:15 +000039STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Chris Lattner6290f5c2008-12-07 08:50:20 +000040
41STATISTIC(NumCacheNonLocalPtr,
42 "Number of fully cached non-local ptr responses");
43STATISTIC(NumCacheDirtyNonLocalPtr,
44 "Number of cached, but dirty, non-local ptr responses");
45STATISTIC(NumUncacheNonLocalPtr,
46 "Number of uncached non-local ptr responses");
Chris Lattner11dcd8d2008-12-08 07:31:50 +000047STATISTIC(NumCacheCompleteNonLocalPtr,
48 "Number of block queries that were completely cached");
Chris Lattner6290f5c2008-12-07 08:50:20 +000049
Owen Anderson78e02f72007-07-06 23:14:35 +000050char MemoryDependenceAnalysis::ID = 0;
51
Owen Anderson78e02f72007-07-06 23:14:35 +000052// Register this pass...
Owen Anderson2ab36d32010-10-12 19:48:12 +000053INITIALIZE_PASS_BEGIN(MemoryDependenceAnalysis, "memdep",
Owen Andersonce665bd2010-10-07 22:25:06 +000054 "Memory Dependence Analysis", false, true)
Owen Anderson2ab36d32010-10-12 19:48:12 +000055INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
56INITIALIZE_PASS_END(MemoryDependenceAnalysis, "memdep",
57 "Memory Dependence Analysis", false, true)
Owen Anderson78e02f72007-07-06 23:14:35 +000058
Chris Lattner4012fdd2008-12-09 06:28:49 +000059MemoryDependenceAnalysis::MemoryDependenceAnalysis()
Owen Anderson90c579d2010-08-06 18:33:48 +000060: FunctionPass(ID), PredCache(0) {
Owen Anderson081c34b2010-10-19 17:21:58 +000061 initializeMemoryDependenceAnalysisPass(*PassRegistry::getPassRegistry());
Chris Lattner4012fdd2008-12-09 06:28:49 +000062}
63MemoryDependenceAnalysis::~MemoryDependenceAnalysis() {
64}
65
66/// Clean up memory in between runs
67void MemoryDependenceAnalysis::releaseMemory() {
68 LocalDeps.clear();
69 NonLocalDeps.clear();
70 NonLocalPointerDeps.clear();
71 ReverseLocalDeps.clear();
72 ReverseNonLocalDeps.clear();
73 ReverseNonLocalPtrDeps.clear();
74 PredCache->clear();
75}
76
77
78
Owen Anderson78e02f72007-07-06 23:14:35 +000079/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
80///
81void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
82 AU.setPreservesAll();
83 AU.addRequiredTransitive<AliasAnalysis>();
Owen Anderson78e02f72007-07-06 23:14:35 +000084}
85
Chris Lattnerd777d402008-11-30 19:24:31 +000086bool MemoryDependenceAnalysis::runOnFunction(Function &) {
87 AA = &getAnalysis<AliasAnalysis>();
Benjamin Kramerdd061b22010-11-21 15:21:46 +000088 TD = getAnalysisIfAvailable<TargetData>();
Chris Lattner4012fdd2008-12-09 06:28:49 +000089 if (PredCache == 0)
90 PredCache.reset(new PredIteratorCache());
Chris Lattnerd777d402008-11-30 19:24:31 +000091 return false;
92}
93
Chris Lattnerd44745d2008-12-07 18:39:13 +000094/// RemoveFromReverseMap - This is a helper function that removes Val from
95/// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry.
96template <typename KeyTy>
97static void RemoveFromReverseMap(DenseMap<Instruction*,
Chris Lattner6a0dcc12009-03-29 00:24:04 +000098 SmallPtrSet<KeyTy, 4> > &ReverseMap,
99 Instruction *Inst, KeyTy Val) {
100 typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator
Chris Lattnerd44745d2008-12-07 18:39:13 +0000101 InstIt = ReverseMap.find(Inst);
102 assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");
103 bool Found = InstIt->second.erase(Val);
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000104 assert(Found && "Invalid reverse map!"); (void)Found;
Chris Lattnerd44745d2008-12-07 18:39:13 +0000105 if (InstIt->second.empty())
106 ReverseMap.erase(InstIt);
107}
108
Dan Gohman533c2ad2010-11-10 21:51:35 +0000109/// GetLocation - If the given instruction references a specific memory
110/// location, fill in Loc with the details, otherwise set Loc.Ptr to null.
111/// Return a ModRefInfo value describing the general behavior of the
112/// instruction.
113static
114AliasAnalysis::ModRefResult GetLocation(const Instruction *Inst,
115 AliasAnalysis::Location &Loc,
116 AliasAnalysis *AA) {
117 if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
118 if (LI->isVolatile()) {
119 Loc = AliasAnalysis::Location();
120 return AliasAnalysis::ModRef;
121 }
Dan Gohman6d8eb152010-11-11 21:50:19 +0000122 Loc = AA->getLocation(LI);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000123 return AliasAnalysis::Ref;
124 }
125
126 if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
127 if (SI->isVolatile()) {
128 Loc = AliasAnalysis::Location();
129 return AliasAnalysis::ModRef;
130 }
Dan Gohman6d8eb152010-11-11 21:50:19 +0000131 Loc = AA->getLocation(SI);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000132 return AliasAnalysis::Mod;
133 }
134
135 if (const VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000136 Loc = AA->getLocation(V);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000137 return AliasAnalysis::ModRef;
138 }
139
140 if (const CallInst *CI = isFreeCall(Inst)) {
141 // calls to free() deallocate the entire structure
142 Loc = AliasAnalysis::Location(CI->getArgOperand(0));
143 return AliasAnalysis::Mod;
144 }
145
146 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
147 switch (II->getIntrinsicID()) {
148 case Intrinsic::lifetime_start:
149 case Intrinsic::lifetime_end:
150 case Intrinsic::invariant_start:
151 Loc = AliasAnalysis::Location(II->getArgOperand(1),
152 cast<ConstantInt>(II->getArgOperand(0))
153 ->getZExtValue(),
154 II->getMetadata(LLVMContext::MD_tbaa));
155 // These intrinsics don't really modify the memory, but returning Mod
156 // will allow them to be handled conservatively.
157 return AliasAnalysis::Mod;
158 case Intrinsic::invariant_end:
159 Loc = AliasAnalysis::Location(II->getArgOperand(2),
160 cast<ConstantInt>(II->getArgOperand(1))
161 ->getZExtValue(),
162 II->getMetadata(LLVMContext::MD_tbaa));
163 // These intrinsics don't really modify the memory, but returning Mod
164 // will allow them to be handled conservatively.
165 return AliasAnalysis::Mod;
166 default:
167 break;
168 }
169
170 // Otherwise, just do the coarse-grained thing that always works.
171 if (Inst->mayWriteToMemory())
172 return AliasAnalysis::ModRef;
173 if (Inst->mayReadFromMemory())
174 return AliasAnalysis::Ref;
175 return AliasAnalysis::NoModRef;
176}
Chris Lattnerbf145d62008-12-01 01:15:42 +0000177
Chris Lattner8ef57c52008-12-07 00:35:51 +0000178/// getCallSiteDependencyFrom - Private helper for finding the local
179/// dependencies of a call site.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000180MemDepResult MemoryDependenceAnalysis::
Chris Lattner20d6f092008-12-09 21:19:42 +0000181getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall,
182 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Owen Anderson642a9e32007-08-08 22:26:03 +0000183 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000184 while (ScanIt != BB->begin()) {
185 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +0000186
187 // If this inst is a memory op, get the pointer it accessed
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000188 AliasAnalysis::Location Loc;
Dan Gohman533c2ad2010-11-10 21:51:35 +0000189 AliasAnalysis::ModRefResult MR = GetLocation(Inst, Loc, AA);
190 if (Loc.Ptr) {
191 // A simple instruction.
192 if (AA->getModRefInfo(CS, Loc) != AliasAnalysis::NoModRef)
193 return MemDepResult::getClobber(Inst);
194 continue;
195 }
196
197 if (CallSite InstCS = cast<Value>(Inst)) {
Owen Andersonf6cec852009-03-09 05:12:38 +0000198 // Debug intrinsics don't cause dependences.
Dale Johannesen497cb6f2009-03-11 21:13:01 +0000199 if (isa<DbgInfoIntrinsic>(Inst)) continue;
Chris Lattnerb51deb92008-12-05 21:04:20 +0000200 // If these two calls do not interfere, look past it.
Chris Lattner20d6f092008-12-09 21:19:42 +0000201 switch (AA->getModRefInfo(CS, InstCS)) {
202 case AliasAnalysis::NoModRef:
Dan Gohman5fa417c2010-08-05 22:09:15 +0000203 // If the two calls are the same, return InstCS as a Def, so that
204 // CS can be found redundant and eliminated.
Dan Gohman533c2ad2010-11-10 21:51:35 +0000205 if (isReadOnlyCall && !(MR & AliasAnalysis::Mod) &&
Dan Gohman5fa417c2010-08-05 22:09:15 +0000206 CS.getInstruction()->isIdenticalToWhenDefined(Inst))
207 return MemDepResult::getDef(Inst);
208
209 // Otherwise if the two calls don't interact (e.g. InstCS is readnone)
210 // keep scanning.
Dan Gohman533c2ad2010-11-10 21:51:35 +0000211 break;
Chris Lattner20d6f092008-12-09 21:19:42 +0000212 default:
Chris Lattnerb51deb92008-12-05 21:04:20 +0000213 return MemDepResult::getClobber(Inst);
Chris Lattner20d6f092008-12-09 21:19:42 +0000214 }
Chris Lattnercfbb6342008-11-30 01:44:00 +0000215 }
Owen Anderson5f323202007-07-10 17:59:22 +0000216 }
217
Chris Lattner7ebcf032008-12-07 02:15:47 +0000218 // No dependence found. If this is the entry block of the function, it is a
219 // clobber, otherwise it is non-local.
220 if (BB != &BB->getParent()->getEntryBlock())
221 return MemDepResult::getNonLocal();
222 return MemDepResult::getClobber(ScanIt);
Owen Anderson5f323202007-07-10 17:59:22 +0000223}
224
Chris Lattnercb5fd742011-04-26 22:42:01 +0000225/// isLoadLoadClobberIfExtendedToFullWidth - Return true if LI is a load that
226/// would fully overlap MemLoc if done as a wider legal integer load.
227///
228/// MemLocBase, MemLocOffset are lazily computed here the first time the
229/// base/offs of memloc is needed.
230static bool
231isLoadLoadClobberIfExtendedToFullWidth(const AliasAnalysis::Location &MemLoc,
232 const Value *&MemLocBase,
233 int64_t &MemLocOffs,
234 const LoadInst *LI, TargetData *TD) {
235 // If we have no target data, we can't do this.
236 if (TD == 0) return false;
237
238 // If we haven't already computed the base/offset of MemLoc, do so now.
239 if (MemLocBase == 0)
240 MemLocBase = GetPointerBaseWithConstantOffset(MemLoc.Ptr, MemLocOffs, *TD);
241
242 // Get the base of this load.
243 int64_t LIOffs = 0;
244 const Value *LIBase =
245 GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, *TD);
246
247 // If the two pointers are not based on the same pointer, we can't tell that
248 // they are related.
249 if (LIBase != MemLocBase) return false;
250
251 // Okay, the two values are based on the same pointer, but returned as
252 // no-alias. This happens when we have things like two byte loads at "P+1"
253 // and "P+3". Check to see if increasing the size of the "LI" load up to its
254 // alignment (or the largest native integer type) will allow us to load all
255 // the bits required by MemLoc.
256
257 // If MemLoc is before LI, then no widening of LI will help us out.
258 if (MemLocOffs < LIOffs) return false;
259
260 // Get the alignment of the load in bytes. We assume that it is safe to load
261 // any legal integer up to this size without a problem. For example, if we're
262 // looking at an i8 load on x86-32 that is known 1024 byte aligned, we can
263 // widen it up to an i32 load. If it is known 2-byte aligned, we can widen it
264 // to i16.
265 unsigned LoadAlign = LI->getAlignment();
266
267 int64_t MemLocEnd = MemLocOffs+MemLoc.Size;
268
269 // If no amount of rounding up will let MemLoc fit into LI, then bail out.
270 if (LIOffs+LoadAlign < MemLocEnd) return false;
271
272 // This is the size of the load to try. Start with the next larger power of
273 // two.
274 unsigned NewLoadByteSize = LI->getType()->getPrimitiveSizeInBits()/8U;
275 NewLoadByteSize = NextPowerOf2(NewLoadByteSize);
276
277 while (1) {
278 // If this load size is bigger than our known alignment or would not fit
279 // into a native integer register, then we fail.
280 if (NewLoadByteSize > LoadAlign ||
281 !TD->fitsInLegalInteger(NewLoadByteSize*8))
282 return false;
283
284 // If a load of this width would include all of MemLoc, then we succeed.
285 if (LIOffs+NewLoadByteSize >= MemLocEnd)
286 return true;
287
288 NewLoadByteSize <<= 1;
289 }
290
291 return false;
292}
293
Chris Lattnere79be942008-12-07 01:50:16 +0000294/// getPointerDependencyFrom - Return the instruction on which a memory
Dan Gohmancd5c1232010-10-29 01:14:04 +0000295/// location depends. If isLoad is true, this routine ignores may-aliases with
296/// read-only operations. If isLoad is false, this routine ignores may-aliases
297/// with reads from read-only locations.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000298MemDepResult MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000299getPointerDependencyFrom(const AliasAnalysis::Location &MemLoc, bool isLoad,
Chris Lattnere79be942008-12-07 01:50:16 +0000300 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000301
Chris Lattnercb5fd742011-04-26 22:42:01 +0000302 const Value *MemLocBase = 0;
303 int64_t MemLocOffset = 0;
304
Chris Lattner6290f5c2008-12-07 08:50:20 +0000305 // Walk backwards through the basic block, looking for dependencies.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000306 while (ScanIt != BB->begin()) {
307 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000308
Chris Lattner1ffb70f2009-12-01 21:15:15 +0000309 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
Chris Lattner09981982010-09-06 03:58:04 +0000310 // Debug intrinsics don't (and can't) cause dependences.
Chris Lattnerc5a5cf22010-09-06 01:26:29 +0000311 if (isa<DbgInfoIntrinsic>(II)) continue;
Owen Anderson9ff5a232009-12-02 07:35:19 +0000312
Owen Andersonb62f7922009-10-28 07:05:35 +0000313 // If we reach a lifetime begin or end marker, then the query ends here
314 // because the value is undefined.
Chris Lattner09981982010-09-06 03:58:04 +0000315 if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
Owen Anderson9ff5a232009-12-02 07:35:19 +0000316 // FIXME: This only considers queries directly on the invariant-tagged
317 // pointer, not on query pointers that are indexed off of them. It'd
Chris Lattnercb5fd742011-04-26 22:42:01 +0000318 // be nice to handle that at some point (the right approach is to use
319 // GetPointerBaseWithConstantOffset).
Chris Lattnerd5c7f7c2011-04-26 21:53:34 +0000320 if (AA->isMustAlias(AliasAnalysis::Location(II->getArgOperand(1)),
321 MemLoc))
Owen Andersonb62f7922009-10-28 07:05:35 +0000322 return MemDepResult::getDef(II);
Chris Lattner09981982010-09-06 03:58:04 +0000323 continue;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000324 }
325 }
326
Chris Lattnercfbb6342008-11-30 01:44:00 +0000327 // Values depend on loads if the pointers are must aliased. This means that
328 // a load depends on another must aliased load from the same value.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000329 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000330 AliasAnalysis::Location LoadLoc = AA->getLocation(LI);
Chris Lattnerb51deb92008-12-05 21:04:20 +0000331
332 // If we found a pointer, check if it could be the same as our pointer.
Dan Gohmancd5c1232010-10-29 01:14:04 +0000333 AliasAnalysis::AliasResult R = AA->alias(LoadLoc, MemLoc);
Chris Lattnera161ab02008-11-29 09:09:48 +0000334
Chris Lattner1f821512011-04-26 01:21:15 +0000335 if (isLoad) {
Chris Lattnercb5fd742011-04-26 22:42:01 +0000336 if (R == AliasAnalysis::NoAlias) {
337 // If this is an over-aligned integer load (for example,
338 // "load i8* %P, align 4") see if it would obviously overlap with the
339 // queried location if widened to a larger load (e.g. if the queried
340 // location is 1 byte at P+1). If so, return it as a load/load
341 // clobber result, allowing the client to decide to widen the load if
342 // it wants to.
343 if (const IntegerType *ITy = dyn_cast<IntegerType>(LI->getType()))
344 if (LI->getAlignment()*8 > ITy->getPrimitiveSizeInBits() &&
345 isLoadLoadClobberIfExtendedToFullWidth(MemLoc, MemLocBase,
346 MemLocOffset, LI, TD))
347 return MemDepResult::getClobber(Inst);
348
349 continue;
350 }
351
Chris Lattner1f821512011-04-26 01:21:15 +0000352 // Must aliased loads are defs of each other.
353 if (R == AliasAnalysis::MustAlias)
354 return MemDepResult::getDef(Inst);
355
356 // If we have a partial alias, then return this as a clobber for the
357 // client to handle.
358 if (R == AliasAnalysis::PartialAlias)
359 return MemDepResult::getClobber(Inst);
360
361 // Random may-alias loads don't depend on each other without a
362 // dependence.
Chris Lattnera161ab02008-11-29 09:09:48 +0000363 continue;
Chris Lattner1f821512011-04-26 01:21:15 +0000364 }
Dan Gohmancd5c1232010-10-29 01:14:04 +0000365
Chris Lattnercb5fd742011-04-26 22:42:01 +0000366 // Stores don't depend on other no-aliased accesses.
367 if (R == AliasAnalysis::NoAlias)
368 continue;
369
Dan Gohmancd5c1232010-10-29 01:14:04 +0000370 // Stores don't alias loads from read-only memory.
Chris Lattner1f821512011-04-26 01:21:15 +0000371 if (AA->pointsToConstantMemory(LoadLoc))
Dan Gohmancd5c1232010-10-29 01:14:04 +0000372 continue;
373
Chris Lattner1f821512011-04-26 01:21:15 +0000374 // Stores depend on may/must aliased loads.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000375 return MemDepResult::getDef(Inst);
376 }
377
378 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chris Lattnerab9cf122009-05-25 21:28:56 +0000379 // If alias analysis can tell that this store is guaranteed to not modify
380 // the query pointer, ignore it. Use getModRefInfo to handle cases where
381 // the query pointer points to constant memory etc.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000382 if (AA->getModRefInfo(SI, MemLoc) == AliasAnalysis::NoModRef)
Chris Lattnerab9cf122009-05-25 21:28:56 +0000383 continue;
384
385 // Ok, this store might clobber the query pointer. Check to see if it is
386 // a must alias: in this case, we want to return this as a def.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000387 AliasAnalysis::Location StoreLoc = AA->getLocation(SI);
Chris Lattnerab9cf122009-05-25 21:28:56 +0000388
Chris Lattnerb51deb92008-12-05 21:04:20 +0000389 // If we found a pointer, check if it could be the same as our pointer.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000390 AliasAnalysis::AliasResult R = AA->alias(StoreLoc, MemLoc);
Chris Lattnerb51deb92008-12-05 21:04:20 +0000391
392 if (R == AliasAnalysis::NoAlias)
393 continue;
Dan Gohman2cd19522010-12-13 22:47:57 +0000394 if (R == AliasAnalysis::MustAlias)
395 return MemDepResult::getDef(Inst);
396 return MemDepResult::getClobber(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000397 }
Chris Lattner237a8282008-11-30 01:39:32 +0000398
399 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattnerb51deb92008-12-05 21:04:20 +0000400 // the allocation, return Def. This means that there is no dependence and
Chris Lattner237a8282008-11-30 01:39:32 +0000401 // the access can be optimized based on that. For example, a load could
402 // turn into undef.
Victor Hernandez5c787362009-10-13 01:42:53 +0000403 // Note: Only determine this to be a malloc if Inst is the malloc call, not
404 // a subsequent bitcast of the malloc call result. There can be stores to
405 // the malloced memory between the malloc call and its bitcast uses, and we
406 // need to continue scanning until the malloc call.
Chris Lattner9b96eca2009-12-22 01:00:32 +0000407 if (isa<AllocaInst>(Inst) ||
408 (isa<CallInst>(Inst) && extractMallocCall(Inst))) {
Dan Gohmanbd1801b2011-01-24 18:53:32 +0000409 const Value *AccessPtr = GetUnderlyingObject(MemLoc.Ptr, TD);
Victor Hernandez46e83122009-09-18 21:34:51 +0000410
Chris Lattnerd5c7f7c2011-04-26 21:53:34 +0000411 if (AccessPtr == Inst || AA->isMustAlias(Inst, AccessPtr))
Victor Hernandez46e83122009-09-18 21:34:51 +0000412 return MemDepResult::getDef(Inst);
413 continue;
414 }
415
Chris Lattnerb51deb92008-12-05 21:04:20 +0000416 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000417 switch (AA->getModRefInfo(Inst, MemLoc)) {
Chris Lattner3579e442008-12-09 19:47:40 +0000418 case AliasAnalysis::NoModRef:
419 // If the call has no effect on the queried pointer, just ignore it.
Chris Lattner25a08142008-11-29 08:51:16 +0000420 continue;
Owen Andersona85a6642009-10-28 06:30:52 +0000421 case AliasAnalysis::Mod:
Owen Andersona85a6642009-10-28 06:30:52 +0000422 return MemDepResult::getClobber(Inst);
Chris Lattner3579e442008-12-09 19:47:40 +0000423 case AliasAnalysis::Ref:
424 // If the call is known to never store to the pointer, and if this is a
425 // load query, we can safely ignore it (scan past it).
426 if (isLoad)
427 continue;
Chris Lattner3579e442008-12-09 19:47:40 +0000428 default:
429 // Otherwise, there is a potential dependence. Return a clobber.
430 return MemDepResult::getClobber(Inst);
431 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000432 }
433
Chris Lattner7ebcf032008-12-07 02:15:47 +0000434 // No dependence found. If this is the entry block of the function, it is a
435 // clobber, otherwise it is non-local.
436 if (BB != &BB->getParent()->getEntryBlock())
437 return MemDepResult::getNonLocal();
438 return MemDepResult::getClobber(ScanIt);
Owen Anderson78e02f72007-07-06 23:14:35 +0000439}
440
Chris Lattner5391a1d2008-11-29 03:47:00 +0000441/// getDependency - Return the instruction on which a memory operation
442/// depends.
443MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
444 Instruction *ScanPos = QueryInst;
445
446 // Check for a cached result
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000447 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattner5391a1d2008-11-29 03:47:00 +0000448
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000449 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000450 // on MemDepResult's default constructing to 'dirty'.
451 if (!LocalCache.isDirty())
452 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000453
454 // Otherwise, if we have a dirty entry, we know we can start the scan at that
455 // instruction, which may save us some work.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000456 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000457 ScanPos = Inst;
Chris Lattner4a69bad2008-11-30 02:52:26 +0000458
Chris Lattnerd44745d2008-12-07 18:39:13 +0000459 RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);
Chris Lattner4a69bad2008-11-30 02:52:26 +0000460 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000461
Chris Lattnere79be942008-12-07 01:50:16 +0000462 BasicBlock *QueryParent = QueryInst->getParent();
463
Chris Lattner5391a1d2008-11-29 03:47:00 +0000464 // Do the scan.
Chris Lattnere79be942008-12-07 01:50:16 +0000465 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000466 // No dependence found. If this is the entry block of the function, it is a
467 // clobber, otherwise it is non-local.
468 if (QueryParent != &QueryParent->getParent()->getEntryBlock())
469 LocalCache = MemDepResult::getNonLocal();
470 else
471 LocalCache = MemDepResult::getClobber(QueryInst);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000472 } else {
473 AliasAnalysis::Location MemLoc;
474 AliasAnalysis::ModRefResult MR = GetLocation(QueryInst, MemLoc, AA);
475 if (MemLoc.Ptr) {
476 // If we can do a pointer scan, make it happen.
477 bool isLoad = !(MR & AliasAnalysis::Mod);
Chris Lattner12bf43b2010-11-30 01:56:13 +0000478 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(QueryInst))
Dan Gohman533c2ad2010-11-10 21:51:35 +0000479 isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_end;
Chris Lattnerf6f1f062010-11-21 07:34:32 +0000480
Dan Gohman533c2ad2010-11-10 21:51:35 +0000481 LocalCache = getPointerDependencyFrom(MemLoc, isLoad, ScanPos,
482 QueryParent);
483 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Gabor Greif622b7cf2010-07-27 22:02:00 +0000484 CallSite QueryCS(QueryInst);
Nick Lewycky93d33112009-12-05 06:37:24 +0000485 bool isReadOnly = AA->onlyReadsMemory(QueryCS);
486 LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos,
487 QueryParent);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000488 } else
489 // Non-memory instruction.
490 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
Nick Lewyckyd801c102009-11-28 21:27:49 +0000491 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000492
493 // Remember the result!
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000494 if (Instruction *I = LocalCache.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000495 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000496
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000497 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000498}
499
Chris Lattner12a7db32009-01-22 07:04:01 +0000500#ifndef NDEBUG
501/// AssertSorted - This method is used when -debug is specified to verify that
502/// cache arrays are properly kept sorted.
503static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
504 int Count = -1) {
505 if (Count == -1) Count = Cache.size();
506 if (Count == 0) return;
507
508 for (unsigned i = 1; i != unsigned(Count); ++i)
Chris Lattnere18b9712009-12-09 07:08:01 +0000509 assert(!(Cache[i] < Cache[i-1]) && "Cache isn't sorted!");
Chris Lattner12a7db32009-01-22 07:04:01 +0000510}
511#endif
512
Chris Lattner1559b362008-12-09 19:38:05 +0000513/// getNonLocalCallDependency - Perform a full dependency query for the
514/// specified call, returning the set of blocks that the value is
Chris Lattner37d041c2008-11-30 01:18:27 +0000515/// potentially live across. The returned set of results will include a
516/// "NonLocal" result for all blocks where the value is live across.
517///
Chris Lattner1559b362008-12-09 19:38:05 +0000518/// This method assumes the instruction returns a "NonLocal" dependency
Chris Lattner37d041c2008-11-30 01:18:27 +0000519/// within its own block.
520///
Chris Lattner1559b362008-12-09 19:38:05 +0000521/// This returns a reference to an internal data structure that may be
522/// invalidated on the next non-local query or when an instruction is
523/// removed. Clients must copy this data if they want it around longer than
524/// that.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000525const MemoryDependenceAnalysis::NonLocalDepInfo &
Chris Lattner1559b362008-12-09 19:38:05 +0000526MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) {
527 assert(getDependency(QueryCS.getInstruction()).isNonLocal() &&
528 "getNonLocalCallDependency should only be used on calls with non-local deps!");
529 PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()];
Chris Lattnerbf145d62008-12-01 01:15:42 +0000530 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner37d041c2008-11-30 01:18:27 +0000531
532 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
533 /// the cached case, this can happen due to instructions being deleted etc. In
534 /// the uncached case, this starts out as the set of predecessors we care
535 /// about.
536 SmallVector<BasicBlock*, 32> DirtyBlocks;
537
538 if (!Cache.empty()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000539 // Okay, we have a cache entry. If we know it is not dirty, just return it
540 // with no computation.
541 if (!CacheP.second) {
Dan Gohmanfe601042010-06-22 15:08:57 +0000542 ++NumCacheNonLocal;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000543 return Cache;
544 }
545
Chris Lattner37d041c2008-11-30 01:18:27 +0000546 // If we already have a partially computed set of results, scan them to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000547 // determine what is dirty, seeding our initial DirtyBlocks worklist.
548 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
549 I != E; ++I)
Chris Lattnere18b9712009-12-09 07:08:01 +0000550 if (I->getResult().isDirty())
551 DirtyBlocks.push_back(I->getBB());
Chris Lattner37d041c2008-11-30 01:18:27 +0000552
Chris Lattnerbf145d62008-12-01 01:15:42 +0000553 // Sort the cache so that we can do fast binary search lookups below.
554 std::sort(Cache.begin(), Cache.end());
Chris Lattner37d041c2008-11-30 01:18:27 +0000555
Chris Lattnerbf145d62008-12-01 01:15:42 +0000556 ++NumCacheDirtyNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000557 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
558 // << Cache.size() << " cached: " << *QueryInst;
559 } else {
560 // Seed DirtyBlocks with each of the preds of QueryInst's block.
Chris Lattner1559b362008-12-09 19:38:05 +0000561 BasicBlock *QueryBB = QueryCS.getInstruction()->getParent();
Chris Lattner511b36c2008-12-09 06:44:17 +0000562 for (BasicBlock **PI = PredCache->GetPreds(QueryBB); *PI; ++PI)
563 DirtyBlocks.push_back(*PI);
Dan Gohmanfe601042010-06-22 15:08:57 +0000564 ++NumUncacheNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000565 }
566
Chris Lattner20d6f092008-12-09 21:19:42 +0000567 // isReadonlyCall - If this is a read-only call, we can be more aggressive.
568 bool isReadonlyCall = AA->onlyReadsMemory(QueryCS);
Chris Lattner9e59c642008-12-15 03:35:32 +0000569
Chris Lattnerbf145d62008-12-01 01:15:42 +0000570 SmallPtrSet<BasicBlock*, 64> Visited;
571
572 unsigned NumSortedEntries = Cache.size();
Chris Lattner12a7db32009-01-22 07:04:01 +0000573 DEBUG(AssertSorted(Cache));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000574
Chris Lattner37d041c2008-11-30 01:18:27 +0000575 // Iterate while we still have blocks to update.
576 while (!DirtyBlocks.empty()) {
577 BasicBlock *DirtyBB = DirtyBlocks.back();
578 DirtyBlocks.pop_back();
579
Chris Lattnerbf145d62008-12-01 01:15:42 +0000580 // Already processed this block?
581 if (!Visited.insert(DirtyBB))
582 continue;
Chris Lattner37d041c2008-11-30 01:18:27 +0000583
Chris Lattnerbf145d62008-12-01 01:15:42 +0000584 // Do a binary search to see if we already have an entry for this block in
585 // the cache set. If so, find it.
Chris Lattner12a7db32009-01-22 07:04:01 +0000586 DEBUG(AssertSorted(Cache, NumSortedEntries));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000587 NonLocalDepInfo::iterator Entry =
588 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
Chris Lattnerdad451c2009-12-09 07:31:04 +0000589 NonLocalDepEntry(DirtyBB));
Chris Lattnere18b9712009-12-09 07:08:01 +0000590 if (Entry != Cache.begin() && prior(Entry)->getBB() == DirtyBB)
Chris Lattnerbf145d62008-12-01 01:15:42 +0000591 --Entry;
592
Chris Lattnere18b9712009-12-09 07:08:01 +0000593 NonLocalDepEntry *ExistingResult = 0;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000594 if (Entry != Cache.begin()+NumSortedEntries &&
Chris Lattnere18b9712009-12-09 07:08:01 +0000595 Entry->getBB() == DirtyBB) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000596 // If we already have an entry, and if it isn't already dirty, the block
597 // is done.
Chris Lattnere18b9712009-12-09 07:08:01 +0000598 if (!Entry->getResult().isDirty())
Chris Lattnerbf145d62008-12-01 01:15:42 +0000599 continue;
600
601 // Otherwise, remember this slot so we can update the value.
Chris Lattnere18b9712009-12-09 07:08:01 +0000602 ExistingResult = &*Entry;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000603 }
604
Chris Lattner37d041c2008-11-30 01:18:27 +0000605 // If the dirty entry has a pointer, start scanning from it so we don't have
606 // to rescan the entire block.
607 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerbf145d62008-12-01 01:15:42 +0000608 if (ExistingResult) {
Chris Lattnere18b9712009-12-09 07:08:01 +0000609 if (Instruction *Inst = ExistingResult->getResult().getInst()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000610 ScanPos = Inst;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000611 // We're removing QueryInst's use of Inst.
Chris Lattner1559b362008-12-09 19:38:05 +0000612 RemoveFromReverseMap(ReverseNonLocalDeps, Inst,
613 QueryCS.getInstruction());
Chris Lattnerbf145d62008-12-01 01:15:42 +0000614 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000615 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000616
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000617 // Find out if this block has a local dependency for QueryInst.
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000618 MemDepResult Dep;
Chris Lattnere79be942008-12-07 01:50:16 +0000619
Chris Lattner1559b362008-12-09 19:38:05 +0000620 if (ScanPos != DirtyBB->begin()) {
Chris Lattner20d6f092008-12-09 21:19:42 +0000621 Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB);
Chris Lattner1559b362008-12-09 19:38:05 +0000622 } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {
623 // No dependence found. If this is the entry block of the function, it is
624 // a clobber, otherwise it is non-local.
625 Dep = MemDepResult::getNonLocal();
Chris Lattnere79be942008-12-07 01:50:16 +0000626 } else {
Chris Lattner1559b362008-12-09 19:38:05 +0000627 Dep = MemDepResult::getClobber(ScanPos);
Chris Lattnere79be942008-12-07 01:50:16 +0000628 }
629
Chris Lattnerbf145d62008-12-01 01:15:42 +0000630 // If we had a dirty entry for the block, update it. Otherwise, just add
631 // a new entry.
632 if (ExistingResult)
Chris Lattner0ee443d2009-12-22 04:25:02 +0000633 ExistingResult->setResult(Dep);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000634 else
Chris Lattner0ee443d2009-12-22 04:25:02 +0000635 Cache.push_back(NonLocalDepEntry(DirtyBB, Dep));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000636
Chris Lattner37d041c2008-11-30 01:18:27 +0000637 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000638 // the value), remember the association!
639 if (!Dep.isNonLocal()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000640 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
641 // update this when we remove instructions.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000642 if (Instruction *Inst = Dep.getInst())
Chris Lattner1559b362008-12-09 19:38:05 +0000643 ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction());
Chris Lattnerbf145d62008-12-01 01:15:42 +0000644 } else {
Chris Lattner37d041c2008-11-30 01:18:27 +0000645
Chris Lattnerbf145d62008-12-01 01:15:42 +0000646 // If the block *is* completely transparent to the load, we need to check
647 // the predecessors of this block. Add them to our worklist.
Chris Lattner511b36c2008-12-09 06:44:17 +0000648 for (BasicBlock **PI = PredCache->GetPreds(DirtyBB); *PI; ++PI)
649 DirtyBlocks.push_back(*PI);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000650 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000651 }
652
Chris Lattnerbf145d62008-12-01 01:15:42 +0000653 return Cache;
Chris Lattner37d041c2008-11-30 01:18:27 +0000654}
655
Chris Lattner7ebcf032008-12-07 02:15:47 +0000656/// getNonLocalPointerDependency - Perform a full dependency query for an
657/// access to the specified (non-volatile) memory location, returning the
658/// set of instructions that either define or clobber the value.
659///
660/// This method assumes the pointer has a "NonLocal" dependency within its
661/// own block.
662///
663void MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000664getNonLocalPointerDependency(const AliasAnalysis::Location &Loc, bool isLoad,
665 BasicBlock *FromBB,
Chris Lattner0ee443d2009-12-22 04:25:02 +0000666 SmallVectorImpl<NonLocalDepResult> &Result) {
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000667 assert(Loc.Ptr->getType()->isPointerTy() &&
Chris Lattner3f7eb5b2008-12-07 18:45:15 +0000668 "Can't get pointer deps of a non-pointer!");
Chris Lattner9a193fd2008-12-07 02:56:57 +0000669 Result.clear();
670
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000671 PHITransAddr Address(const_cast<Value *>(Loc.Ptr), TD);
Chris Lattner05e15f82009-12-09 01:59:31 +0000672
Chris Lattner9e59c642008-12-15 03:35:32 +0000673 // This is the set of blocks we've inspected, and the pointer we consider in
674 // each block. Because of critical edges, we currently bail out if querying
675 // a block with multiple different pointers. This can happen during PHI
676 // translation.
677 DenseMap<BasicBlock*, Value*> Visited;
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000678 if (!getNonLocalPointerDepFromBB(Address, Loc, isLoad, FromBB,
Chris Lattner9e59c642008-12-15 03:35:32 +0000679 Result, Visited, true))
680 return;
Chris Lattner3af23f82008-12-15 04:58:29 +0000681 Result.clear();
Chris Lattner0ee443d2009-12-22 04:25:02 +0000682 Result.push_back(NonLocalDepResult(FromBB,
683 MemDepResult::getClobber(FromBB->begin()),
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000684 const_cast<Value *>(Loc.Ptr)));
Chris Lattner9a193fd2008-12-07 02:56:57 +0000685}
686
Chris Lattner9863c3f2008-12-09 07:47:11 +0000687/// GetNonLocalInfoForBlock - Compute the memdep value for BB with
688/// Pointer/PointeeSize using either cached information in Cache or by doing a
689/// lookup (which may use dirty cache info if available). If we do a lookup,
690/// add the result to the cache.
691MemDepResult MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000692GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
Chris Lattner9863c3f2008-12-09 07:47:11 +0000693 bool isLoad, BasicBlock *BB,
694 NonLocalDepInfo *Cache, unsigned NumSortedEntries) {
695
696 // Do a binary search to see if we already have an entry for this block in
697 // the cache set. If so, find it.
698 NonLocalDepInfo::iterator Entry =
699 std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries,
Chris Lattnerdad451c2009-12-09 07:31:04 +0000700 NonLocalDepEntry(BB));
Chris Lattnere18b9712009-12-09 07:08:01 +0000701 if (Entry != Cache->begin() && (Entry-1)->getBB() == BB)
Chris Lattner9863c3f2008-12-09 07:47:11 +0000702 --Entry;
703
Chris Lattnere18b9712009-12-09 07:08:01 +0000704 NonLocalDepEntry *ExistingResult = 0;
705 if (Entry != Cache->begin()+NumSortedEntries && Entry->getBB() == BB)
706 ExistingResult = &*Entry;
Chris Lattner9863c3f2008-12-09 07:47:11 +0000707
708 // If we have a cached entry, and it is non-dirty, use it as the value for
709 // this dependency.
Chris Lattnere18b9712009-12-09 07:08:01 +0000710 if (ExistingResult && !ExistingResult->getResult().isDirty()) {
Chris Lattner9863c3f2008-12-09 07:47:11 +0000711 ++NumCacheNonLocalPtr;
Chris Lattnere18b9712009-12-09 07:08:01 +0000712 return ExistingResult->getResult();
Chris Lattner9863c3f2008-12-09 07:47:11 +0000713 }
714
715 // Otherwise, we have to scan for the value. If we have a dirty cache
716 // entry, start scanning from its position, otherwise we scan from the end
717 // of the block.
718 BasicBlock::iterator ScanPos = BB->end();
Chris Lattnere18b9712009-12-09 07:08:01 +0000719 if (ExistingResult && ExistingResult->getResult().getInst()) {
720 assert(ExistingResult->getResult().getInst()->getParent() == BB &&
Chris Lattner9863c3f2008-12-09 07:47:11 +0000721 "Instruction invalidated?");
722 ++NumCacheDirtyNonLocalPtr;
Chris Lattnere18b9712009-12-09 07:08:01 +0000723 ScanPos = ExistingResult->getResult().getInst();
Chris Lattner9863c3f2008-12-09 07:47:11 +0000724
725 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000726 ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
Chris Lattner6a0dcc12009-03-29 00:24:04 +0000727 RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000728 } else {
729 ++NumUncacheNonLocalPtr;
730 }
731
732 // Scan the block for the dependency.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000733 MemDepResult Dep = getPointerDependencyFrom(Loc, isLoad, ScanPos, BB);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000734
735 // If we had a dirty entry for the block, update it. Otherwise, just add
736 // a new entry.
737 if (ExistingResult)
Chris Lattner0ee443d2009-12-22 04:25:02 +0000738 ExistingResult->setResult(Dep);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000739 else
Chris Lattner0ee443d2009-12-22 04:25:02 +0000740 Cache->push_back(NonLocalDepEntry(BB, Dep));
Chris Lattner9863c3f2008-12-09 07:47:11 +0000741
742 // If the block has a dependency (i.e. it isn't completely transparent to
743 // the value), remember the reverse association because we just added it
744 // to Cache!
745 if (Dep.isNonLocal())
746 return Dep;
747
748 // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently
749 // update MemDep when we remove instructions.
750 Instruction *Inst = Dep.getInst();
751 assert(Inst && "Didn't depend on anything?");
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000752 ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
Chris Lattner6a0dcc12009-03-29 00:24:04 +0000753 ReverseNonLocalPtrDeps[Inst].insert(CacheKey);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000754 return Dep;
755}
756
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000757/// SortNonLocalDepInfoCache - Sort the a NonLocalDepInfo cache, given a certain
758/// number of elements in the array that are already properly ordered. This is
759/// optimized for the case when only a few entries are added.
760static void
761SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
762 unsigned NumSortedEntries) {
763 switch (Cache.size() - NumSortedEntries) {
764 case 0:
765 // done, no new entries.
766 break;
767 case 2: {
768 // Two new entries, insert the last one into place.
Chris Lattnere18b9712009-12-09 07:08:01 +0000769 NonLocalDepEntry Val = Cache.back();
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000770 Cache.pop_back();
771 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
772 std::upper_bound(Cache.begin(), Cache.end()-1, Val);
773 Cache.insert(Entry, Val);
774 // FALL THROUGH.
775 }
776 case 1:
777 // One new entry, Just insert the new value at the appropriate position.
778 if (Cache.size() != 1) {
Chris Lattnere18b9712009-12-09 07:08:01 +0000779 NonLocalDepEntry Val = Cache.back();
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000780 Cache.pop_back();
781 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
782 std::upper_bound(Cache.begin(), Cache.end(), Val);
783 Cache.insert(Entry, Val);
784 }
785 break;
786 default:
787 // Added many values, do a full scale sort.
788 std::sort(Cache.begin(), Cache.end());
789 break;
790 }
791}
792
Chris Lattner9e59c642008-12-15 03:35:32 +0000793/// getNonLocalPointerDepFromBB - Perform a dependency query based on
794/// pointer/pointeesize starting at the end of StartBB. Add any clobber/def
795/// results to the results vector and keep track of which blocks are visited in
796/// 'Visited'.
797///
798/// This has special behavior for the first block queries (when SkipFirstBlock
799/// is true). In this special case, it ignores the contents of the specified
800/// block and starts returning dependence info for its predecessors.
801///
802/// This function returns false on success, or true to indicate that it could
803/// not compute dependence information for some reason. This should be treated
804/// as a clobber dependence on the first instruction in the predecessor block.
805bool MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000806getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
807 const AliasAnalysis::Location &Loc,
Chris Lattner9863c3f2008-12-09 07:47:11 +0000808 bool isLoad, BasicBlock *StartBB,
Chris Lattner0ee443d2009-12-22 04:25:02 +0000809 SmallVectorImpl<NonLocalDepResult> &Result,
Chris Lattner9e59c642008-12-15 03:35:32 +0000810 DenseMap<BasicBlock*, Value*> &Visited,
811 bool SkipFirstBlock) {
Chris Lattner66364342009-09-20 22:44:26 +0000812
Chris Lattner6290f5c2008-12-07 08:50:20 +0000813 // Look up the cached info for Pointer.
Chris Lattner05e15f82009-12-09 01:59:31 +0000814 ValueIsLoadPair CacheKey(Pointer.getAddr(), isLoad);
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000815
Dan Gohman075fb5d2010-11-10 20:37:15 +0000816 // Set up a temporary NLPI value. If the map doesn't yet have an entry for
817 // CacheKey, this value will be inserted as the associated value. Otherwise,
818 // it'll be ignored, and we'll have to check to see if the cached size and
819 // tbaa tag are consistent with the current query.
820 NonLocalPointerInfo InitialNLPI;
821 InitialNLPI.Size = Loc.Size;
822 InitialNLPI.TBAATag = Loc.TBAATag;
823
824 // Get the NLPI for CacheKey, inserting one into the map if it doesn't
825 // already have one.
826 std::pair<CachedNonLocalPointerInfo::iterator, bool> Pair =
827 NonLocalPointerDeps.insert(std::make_pair(CacheKey, InitialNLPI));
828 NonLocalPointerInfo *CacheInfo = &Pair.first->second;
829
Dan Gohman733c54d2010-11-10 21:45:11 +0000830 // If we already have a cache entry for this CacheKey, we may need to do some
831 // work to reconcile the cache entry and the current query.
Dan Gohman075fb5d2010-11-10 20:37:15 +0000832 if (!Pair.second) {
Dan Gohman733c54d2010-11-10 21:45:11 +0000833 if (CacheInfo->Size < Loc.Size) {
834 // The query's Size is greater than the cached one. Throw out the
835 // cached data and procede with the query at the greater size.
836 CacheInfo->Pair = BBSkipFirstBlockPair();
837 CacheInfo->Size = Loc.Size;
Dan Gohman2365f082010-11-10 22:35:02 +0000838 for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(),
839 DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI)
840 if (Instruction *Inst = DI->getResult().getInst())
841 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
Dan Gohman733c54d2010-11-10 21:45:11 +0000842 CacheInfo->NonLocalDeps.clear();
843 } else if (CacheInfo->Size > Loc.Size) {
844 // This query's Size is less than the cached one. Conservatively restart
845 // the query using the greater size.
Dan Gohman075fb5d2010-11-10 20:37:15 +0000846 return getNonLocalPointerDepFromBB(Pointer,
847 Loc.getWithNewSize(CacheInfo->Size),
848 isLoad, StartBB, Result, Visited,
849 SkipFirstBlock);
850 }
851
Dan Gohman733c54d2010-11-10 21:45:11 +0000852 // If the query's TBAATag is inconsistent with the cached one,
853 // conservatively throw out the cached data and restart the query with
854 // no tag if needed.
Dan Gohman075fb5d2010-11-10 20:37:15 +0000855 if (CacheInfo->TBAATag != Loc.TBAATag) {
Dan Gohman733c54d2010-11-10 21:45:11 +0000856 if (CacheInfo->TBAATag) {
857 CacheInfo->Pair = BBSkipFirstBlockPair();
858 CacheInfo->TBAATag = 0;
Dan Gohman2365f082010-11-10 22:35:02 +0000859 for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(),
860 DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI)
861 if (Instruction *Inst = DI->getResult().getInst())
862 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
Dan Gohman733c54d2010-11-10 21:45:11 +0000863 CacheInfo->NonLocalDeps.clear();
864 }
865 if (Loc.TBAATag)
866 return getNonLocalPointerDepFromBB(Pointer, Loc.getWithoutTBAATag(),
867 isLoad, StartBB, Result, Visited,
868 SkipFirstBlock);
Dan Gohman075fb5d2010-11-10 20:37:15 +0000869 }
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000870 }
871
872 NonLocalDepInfo *Cache = &CacheInfo->NonLocalDeps;
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000873
874 // If we have valid cached information for exactly the block we are
875 // investigating, just return it with no recomputation.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000876 if (CacheInfo->Pair == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) {
Chris Lattnerf4789512008-12-16 07:10:09 +0000877 // We have a fully cached result for this query then we can just return the
878 // cached results and populate the visited set. However, we have to verify
879 // that we don't already have conflicting results for these blocks. Check
880 // to ensure that if a block in the results set is in the visited set that
881 // it was for the same pointer query.
882 if (!Visited.empty()) {
883 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
884 I != E; ++I) {
Chris Lattnere18b9712009-12-09 07:08:01 +0000885 DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->getBB());
Chris Lattner05e15f82009-12-09 01:59:31 +0000886 if (VI == Visited.end() || VI->second == Pointer.getAddr())
887 continue;
Chris Lattnerf4789512008-12-16 07:10:09 +0000888
889 // We have a pointer mismatch in a block. Just return clobber, saying
890 // that something was clobbered in this result. We could also do a
891 // non-fully cached query, but there is little point in doing this.
892 return true;
893 }
894 }
895
Chris Lattner0ee443d2009-12-22 04:25:02 +0000896 Value *Addr = Pointer.getAddr();
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000897 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
Chris Lattnerf4789512008-12-16 07:10:09 +0000898 I != E; ++I) {
Chris Lattner0ee443d2009-12-22 04:25:02 +0000899 Visited.insert(std::make_pair(I->getBB(), Addr));
Chris Lattnere18b9712009-12-09 07:08:01 +0000900 if (!I->getResult().isNonLocal())
Chris Lattner0ee443d2009-12-22 04:25:02 +0000901 Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(), Addr));
Chris Lattnerf4789512008-12-16 07:10:09 +0000902 }
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000903 ++NumCacheCompleteNonLocalPtr;
Chris Lattner9e59c642008-12-15 03:35:32 +0000904 return false;
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000905 }
906
907 // Otherwise, either this is a new block, a block with an invalid cache
908 // pointer or one that we're about to invalidate by putting more info into it
909 // than its valid cache info. If empty, the result will be valid cache info,
910 // otherwise it isn't.
Chris Lattner9e59c642008-12-15 03:35:32 +0000911 if (Cache->empty())
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000912 CacheInfo->Pair = BBSkipFirstBlockPair(StartBB, SkipFirstBlock);
Dan Gohman8a66a202010-11-11 00:42:22 +0000913 else
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000914 CacheInfo->Pair = BBSkipFirstBlockPair();
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000915
916 SmallVector<BasicBlock*, 32> Worklist;
917 Worklist.push_back(StartBB);
Chris Lattner6290f5c2008-12-07 08:50:20 +0000918
919 // Keep track of the entries that we know are sorted. Previously cached
920 // entries will all be sorted. The entries we add we only sort on demand (we
921 // don't insert every element into its sorted position). We know that we
922 // won't get any reuse from currently inserted values, because we don't
923 // revisit blocks after we insert info for them.
924 unsigned NumSortedEntries = Cache->size();
Chris Lattner12a7db32009-01-22 07:04:01 +0000925 DEBUG(AssertSorted(*Cache));
Chris Lattner6290f5c2008-12-07 08:50:20 +0000926
Chris Lattner7ebcf032008-12-07 02:15:47 +0000927 while (!Worklist.empty()) {
Chris Lattner9a193fd2008-12-07 02:56:57 +0000928 BasicBlock *BB = Worklist.pop_back_val();
Chris Lattner7ebcf032008-12-07 02:15:47 +0000929
Chris Lattner65633712008-12-09 07:52:59 +0000930 // Skip the first block if we have it.
Chris Lattner9e59c642008-12-15 03:35:32 +0000931 if (!SkipFirstBlock) {
Chris Lattner65633712008-12-09 07:52:59 +0000932 // Analyze the dependency of *Pointer in FromBB. See if we already have
933 // been here.
Chris Lattner9e59c642008-12-15 03:35:32 +0000934 assert(Visited.count(BB) && "Should check 'visited' before adding to WL");
Chris Lattner6290f5c2008-12-07 08:50:20 +0000935
Chris Lattner65633712008-12-09 07:52:59 +0000936 // Get the dependency info for Pointer in BB. If we have cached
937 // information, we will use it, otherwise we compute it.
Chris Lattner12a7db32009-01-22 07:04:01 +0000938 DEBUG(AssertSorted(*Cache, NumSortedEntries));
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000939 MemDepResult Dep = GetNonLocalInfoForBlock(Loc, isLoad, BB, Cache,
Chris Lattner05e15f82009-12-09 01:59:31 +0000940 NumSortedEntries);
Chris Lattner65633712008-12-09 07:52:59 +0000941
942 // If we got a Def or Clobber, add this to the list of results.
943 if (!Dep.isNonLocal()) {
Chris Lattner0ee443d2009-12-22 04:25:02 +0000944 Result.push_back(NonLocalDepResult(BB, Dep, Pointer.getAddr()));
Chris Lattner65633712008-12-09 07:52:59 +0000945 continue;
946 }
Chris Lattner7ebcf032008-12-07 02:15:47 +0000947 }
948
Chris Lattner9e59c642008-12-15 03:35:32 +0000949 // If 'Pointer' is an instruction defined in this block, then we need to do
950 // phi translation to change it into a value live in the predecessor block.
Chris Lattner05e15f82009-12-09 01:59:31 +0000951 // If not, we just add the predecessors to the worklist and scan them with
952 // the same Pointer.
953 if (!Pointer.NeedsPHITranslationFromBlock(BB)) {
Chris Lattner9e59c642008-12-15 03:35:32 +0000954 SkipFirstBlock = false;
955 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
956 // Verify that we haven't looked at this block yet.
957 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
Chris Lattner05e15f82009-12-09 01:59:31 +0000958 InsertRes = Visited.insert(std::make_pair(*PI, Pointer.getAddr()));
Chris Lattner9e59c642008-12-15 03:35:32 +0000959 if (InsertRes.second) {
960 // First time we've looked at *PI.
961 Worklist.push_back(*PI);
962 continue;
963 }
964
965 // If we have seen this block before, but it was with a different
966 // pointer then we have a phi translation failure and we have to treat
967 // this as a clobber.
Chris Lattner05e15f82009-12-09 01:59:31 +0000968 if (InsertRes.first->second != Pointer.getAddr())
Chris Lattner9e59c642008-12-15 03:35:32 +0000969 goto PredTranslationFailure;
970 }
971 continue;
972 }
973
Chris Lattner05e15f82009-12-09 01:59:31 +0000974 // We do need to do phi translation, if we know ahead of time we can't phi
975 // translate this value, don't even try.
976 if (!Pointer.IsPotentiallyPHITranslatable())
977 goto PredTranslationFailure;
978
Chris Lattner6fbc1962009-07-13 17:14:23 +0000979 // We may have added values to the cache list before this PHI translation.
980 // If so, we haven't done anything to ensure that the cache remains sorted.
981 // Sort it now (if needed) so that recursive invocations of
982 // getNonLocalPointerDepFromBB and other routines that could reuse the cache
983 // value will only see properly sorted cache arrays.
984 if (Cache && NumSortedEntries != Cache->size()) {
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000985 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner6fbc1962009-07-13 17:14:23 +0000986 NumSortedEntries = Cache->size();
987 }
Chris Lattnere95035a2009-11-27 08:37:22 +0000988 Cache = 0;
Chris Lattner05e15f82009-12-09 01:59:31 +0000989
Chris Lattnere95035a2009-11-27 08:37:22 +0000990 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
991 BasicBlock *Pred = *PI;
Chris Lattner05e15f82009-12-09 01:59:31 +0000992
993 // Get the PHI translated pointer in this predecessor. This can fail if
994 // not translatable, in which case the getAddr() returns null.
995 PHITransAddr PredPointer(Pointer);
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000996 PredPointer.PHITranslateValue(BB, Pred, 0);
Chris Lattner05e15f82009-12-09 01:59:31 +0000997
998 Value *PredPtrVal = PredPointer.getAddr();
Chris Lattnere95035a2009-11-27 08:37:22 +0000999
1000 // Check to see if we have already visited this pred block with another
1001 // pointer. If so, we can't do this lookup. This failure can occur
1002 // with PHI translation when a critical edge exists and the PHI node in
1003 // the successor translates to a pointer value different than the
1004 // pointer the block was first analyzed with.
1005 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
Chris Lattner05e15f82009-12-09 01:59:31 +00001006 InsertRes = Visited.insert(std::make_pair(Pred, PredPtrVal));
Chris Lattner9e59c642008-12-15 03:35:32 +00001007
Chris Lattnere95035a2009-11-27 08:37:22 +00001008 if (!InsertRes.second) {
1009 // If the predecessor was visited with PredPtr, then we already did
1010 // the analysis and can ignore it.
Chris Lattner05e15f82009-12-09 01:59:31 +00001011 if (InsertRes.first->second == PredPtrVal)
Chris Lattnere95035a2009-11-27 08:37:22 +00001012 continue;
Chris Lattner9e59c642008-12-15 03:35:32 +00001013
Chris Lattnere95035a2009-11-27 08:37:22 +00001014 // Otherwise, the block was previously analyzed with a different
1015 // pointer. We can't represent the result of this case, so we just
1016 // treat this as a phi translation failure.
1017 goto PredTranslationFailure;
Chris Lattner9e59c642008-12-15 03:35:32 +00001018 }
Chris Lattner6f7b2102009-11-27 22:05:15 +00001019
1020 // If PHI translation was unable to find an available pointer in this
1021 // predecessor, then we have to assume that the pointer is clobbered in
1022 // that predecessor. We can still do PRE of the load, which would insert
1023 // a computation of the pointer in this predecessor.
Chris Lattner05e15f82009-12-09 01:59:31 +00001024 if (PredPtrVal == 0) {
Chris Lattner855d9da2009-12-01 07:33:32 +00001025 // Add the entry to the Result list.
Chris Lattner0ee443d2009-12-22 04:25:02 +00001026 NonLocalDepResult Entry(Pred,
1027 MemDepResult::getClobber(Pred->getTerminator()),
1028 PredPtrVal);
Chris Lattner855d9da2009-12-01 07:33:32 +00001029 Result.push_back(Entry);
1030
Chris Lattnerf6481252009-12-19 21:29:22 +00001031 // Since we had a phi translation failure, the cache for CacheKey won't
1032 // include all of the entries that we need to immediately satisfy future
1033 // queries. Mark this in NonLocalPointerDeps by setting the
1034 // BBSkipFirstBlockPair pointer to null. This requires reuse of the
1035 // cached value to do more work but not miss the phi trans failure.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001036 NonLocalPointerInfo &NLPI = NonLocalPointerDeps[CacheKey];
1037 NLPI.Pair = BBSkipFirstBlockPair();
Chris Lattner6f7b2102009-11-27 22:05:15 +00001038 continue;
Chris Lattner6f7b2102009-11-27 22:05:15 +00001039 }
Chris Lattnere95035a2009-11-27 08:37:22 +00001040
1041 // FIXME: it is entirely possible that PHI translating will end up with
1042 // the same value. Consider PHI translating something like:
1043 // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need*
1044 // to recurse here, pedantically speaking.
Chris Lattner6fbc1962009-07-13 17:14:23 +00001045
Chris Lattnere95035a2009-11-27 08:37:22 +00001046 // If we have a problem phi translating, fall through to the code below
1047 // to handle the failure condition.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001048 if (getNonLocalPointerDepFromBB(PredPointer,
1049 Loc.getWithNewPtr(PredPointer.getAddr()),
1050 isLoad, Pred,
Chris Lattnere95035a2009-11-27 08:37:22 +00001051 Result, Visited))
1052 goto PredTranslationFailure;
Chris Lattner9e59c642008-12-15 03:35:32 +00001053 }
Chris Lattnere95035a2009-11-27 08:37:22 +00001054
1055 // Refresh the CacheInfo/Cache pointer so that it isn't invalidated.
1056 CacheInfo = &NonLocalPointerDeps[CacheKey];
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001057 Cache = &CacheInfo->NonLocalDeps;
Chris Lattnere95035a2009-11-27 08:37:22 +00001058 NumSortedEntries = Cache->size();
1059
1060 // Since we did phi translation, the "Cache" set won't contain all of the
1061 // results for the query. This is ok (we can still use it to accelerate
1062 // specific block queries) but we can't do the fastpath "return all
1063 // results from the set" Clear out the indicator for this.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001064 CacheInfo->Pair = BBSkipFirstBlockPair();
Chris Lattnere95035a2009-11-27 08:37:22 +00001065 SkipFirstBlock = false;
1066 continue;
Chris Lattnerdc593112009-11-26 23:18:49 +00001067
Chris Lattner9e59c642008-12-15 03:35:32 +00001068 PredTranslationFailure:
1069
Chris Lattner95900f22009-01-23 07:12:16 +00001070 if (Cache == 0) {
1071 // Refresh the CacheInfo/Cache pointer if it got invalidated.
1072 CacheInfo = &NonLocalPointerDeps[CacheKey];
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001073 Cache = &CacheInfo->NonLocalDeps;
Chris Lattner95900f22009-01-23 07:12:16 +00001074 NumSortedEntries = Cache->size();
Chris Lattner95900f22009-01-23 07:12:16 +00001075 }
Chris Lattner6fbc1962009-07-13 17:14:23 +00001076
Chris Lattnerf6481252009-12-19 21:29:22 +00001077 // Since we failed phi translation, the "Cache" set won't contain all of the
Chris Lattner9e59c642008-12-15 03:35:32 +00001078 // results for the query. This is ok (we can still use it to accelerate
1079 // specific block queries) but we can't do the fastpath "return all
Chris Lattnerf6481252009-12-19 21:29:22 +00001080 // results from the set". Clear out the indicator for this.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001081 CacheInfo->Pair = BBSkipFirstBlockPair();
Chris Lattner9e59c642008-12-15 03:35:32 +00001082
1083 // If *nothing* works, mark the pointer as being clobbered by the first
1084 // instruction in this block.
1085 //
1086 // If this is the magic first block, return this as a clobber of the whole
1087 // incoming value. Since we can't phi translate to one of the predecessors,
1088 // we have to bail out.
1089 if (SkipFirstBlock)
1090 return true;
1091
1092 for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) {
1093 assert(I != Cache->rend() && "Didn't find current block??");
Chris Lattnere18b9712009-12-09 07:08:01 +00001094 if (I->getBB() != BB)
Chris Lattner9e59c642008-12-15 03:35:32 +00001095 continue;
1096
Chris Lattnere18b9712009-12-09 07:08:01 +00001097 assert(I->getResult().isNonLocal() &&
Chris Lattner9e59c642008-12-15 03:35:32 +00001098 "Should only be here with transparent block");
Chris Lattner0ee443d2009-12-22 04:25:02 +00001099 I->setResult(MemDepResult::getClobber(BB->begin()));
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001100 ReverseNonLocalPtrDeps[BB->begin()].insert(CacheKey);
Chris Lattner0ee443d2009-12-22 04:25:02 +00001101 Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(),
1102 Pointer.getAddr()));
Chris Lattner9e59c642008-12-15 03:35:32 +00001103 break;
Chris Lattner9a193fd2008-12-07 02:56:57 +00001104 }
Chris Lattner7ebcf032008-12-07 02:15:47 +00001105 }
Chris Lattner95900f22009-01-23 07:12:16 +00001106
Chris Lattner9863c3f2008-12-09 07:47:11 +00001107 // Okay, we're done now. If we added new values to the cache, re-sort it.
Chris Lattnera2f55dd2009-07-13 17:20:05 +00001108 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner12a7db32009-01-22 07:04:01 +00001109 DEBUG(AssertSorted(*Cache));
Chris Lattner9e59c642008-12-15 03:35:32 +00001110 return false;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001111}
1112
1113/// RemoveCachedNonLocalPointerDependencies - If P exists in
1114/// CachedNonLocalPointerInfo, remove it.
1115void MemoryDependenceAnalysis::
1116RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) {
1117 CachedNonLocalPointerInfo::iterator It =
1118 NonLocalPointerDeps.find(P);
1119 if (It == NonLocalPointerDeps.end()) return;
1120
1121 // Remove all of the entries in the BB->val map. This involves removing
1122 // instructions from the reverse map.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001123 NonLocalDepInfo &PInfo = It->second.NonLocalDeps;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001124
1125 for (unsigned i = 0, e = PInfo.size(); i != e; ++i) {
Chris Lattnere18b9712009-12-09 07:08:01 +00001126 Instruction *Target = PInfo[i].getResult().getInst();
Chris Lattner6290f5c2008-12-07 08:50:20 +00001127 if (Target == 0) continue; // Ignore non-local dep results.
Chris Lattnere18b9712009-12-09 07:08:01 +00001128 assert(Target->getParent() == PInfo[i].getBB());
Chris Lattner6290f5c2008-12-07 08:50:20 +00001129
1130 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001131 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001132 }
1133
1134 // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).
1135 NonLocalPointerDeps.erase(It);
Chris Lattner7ebcf032008-12-07 02:15:47 +00001136}
1137
1138
Chris Lattnerbc99be12008-12-09 22:06:23 +00001139/// invalidateCachedPointerInfo - This method is used to invalidate cached
1140/// information about the specified pointer, because it may be too
1141/// conservative in memdep. This is an optional call that can be used when
1142/// the client detects an equivalence between the pointer and some other
1143/// value and replaces the other value with ptr. This can make Ptr available
1144/// in more places that cached info does not necessarily keep.
1145void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) {
1146 // If Ptr isn't really a pointer, just ignore it.
Duncan Sands1df98592010-02-16 11:11:14 +00001147 if (!Ptr->getType()->isPointerTy()) return;
Chris Lattnerbc99be12008-12-09 22:06:23 +00001148 // Flush store info for the pointer.
1149 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false));
1150 // Flush load info for the pointer.
1151 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true));
1152}
1153
Bob Wilson484d4a32010-02-16 19:51:59 +00001154/// invalidateCachedPredecessors - Clear the PredIteratorCache info.
1155/// This needs to be done when the CFG changes, e.g., due to splitting
1156/// critical edges.
1157void MemoryDependenceAnalysis::invalidateCachedPredecessors() {
1158 PredCache->clear();
1159}
1160
Owen Anderson78e02f72007-07-06 23:14:35 +00001161/// removeInstruction - Remove an instruction from the dependence analysis,
1162/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +00001163/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +00001164void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +00001165 // Walk through the Non-local dependencies, removing this one as the value
1166 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +00001167 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
1168 if (NLDI != NonLocalDeps.end()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +00001169 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattner25f4b2b2008-11-30 02:30:50 +00001170 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
1171 DI != DE; ++DI)
Chris Lattnere18b9712009-12-09 07:08:01 +00001172 if (Instruction *Inst = DI->getResult().getInst())
Chris Lattnerd44745d2008-12-07 18:39:13 +00001173 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);
Chris Lattnerf68f3102008-11-30 02:28:25 +00001174 NonLocalDeps.erase(NLDI);
1175 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +00001176
Chris Lattner5f589dc2008-11-28 22:04:47 +00001177 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +00001178 //
Chris Lattner39f372e2008-11-29 01:43:36 +00001179 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
1180 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +00001181 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerd44745d2008-12-07 18:39:13 +00001182 if (Instruction *Inst = LocalDepEntry->second.getInst())
1183 RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);
Chris Lattner125ce362008-11-30 01:09:30 +00001184
Chris Lattnerbaad8882008-11-28 22:28:27 +00001185 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +00001186 LocalDeps.erase(LocalDepEntry);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001187 }
1188
1189 // If we have any cached pointer dependencies on this instruction, remove
1190 // them. If the instruction has non-pointer type, then it can't be a pointer
1191 // base.
1192
1193 // Remove it from both the load info and the store info. The instruction
1194 // can't be in either of these maps if it is non-pointer.
Duncan Sands1df98592010-02-16 11:11:14 +00001195 if (RemInst->getType()->isPointerTy()) {
Chris Lattner6290f5c2008-12-07 08:50:20 +00001196 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));
1197 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));
1198 }
Chris Lattnerbaad8882008-11-28 22:28:27 +00001199
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001200 // Loop over all of the things that depend on the instruction we're removing.
1201 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001202 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
Chris Lattner0655f732008-12-07 18:42:51 +00001203
1204 // If we find RemInst as a clobber or Def in any of the maps for other values,
1205 // we need to replace its entry with a dirty version of the instruction after
1206 // it. If RemInst is a terminator, we use a null dirty value.
1207 //
1208 // Using a dirty version of the instruction after RemInst saves having to scan
1209 // the entire block to get to this point.
1210 MemDepResult NewDirtyVal;
1211 if (!RemInst->isTerminator())
1212 NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst));
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001213
Chris Lattner8c465272008-11-29 09:20:15 +00001214 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
1215 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001216 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001217 // RemInst can't be the terminator if it has local stuff depending on it.
Chris Lattner125ce362008-11-30 01:09:30 +00001218 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
1219 "Nothing can locally depend on a terminator");
1220
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001221 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
1222 E = ReverseDeps.end(); I != E; ++I) {
1223 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001224 assert(InstDependingOnRemInst != RemInst &&
1225 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +00001226
Chris Lattner0655f732008-12-07 18:42:51 +00001227 LocalDeps[InstDependingOnRemInst] = NewDirtyVal;
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001228
Chris Lattner125ce362008-11-30 01:09:30 +00001229 // Make sure to remember that new things depend on NewDepInst.
Chris Lattner0655f732008-12-07 18:42:51 +00001230 assert(NewDirtyVal.getInst() && "There is no way something else can have "
1231 "a local dep on this if it is a terminator!");
1232 ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(),
Chris Lattner125ce362008-11-30 01:09:30 +00001233 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001234 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001235
1236 ReverseLocalDeps.erase(ReverseDepIt);
1237
1238 // Add new reverse deps after scanning the set, to avoid invalidating the
1239 // 'ReverseDeps' reference.
1240 while (!ReverseDepsToAdd.empty()) {
1241 ReverseLocalDeps[ReverseDepsToAdd.back().first]
1242 .insert(ReverseDepsToAdd.back().second);
1243 ReverseDepsToAdd.pop_back();
1244 }
Owen Anderson78e02f72007-07-06 23:14:35 +00001245 }
Owen Anderson4d13de42007-08-16 21:27:05 +00001246
Chris Lattner8c465272008-11-29 09:20:15 +00001247 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
1248 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner6290f5c2008-12-07 08:50:20 +00001249 SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second;
1250 for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +00001251 I != E; ++I) {
1252 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
1253
Chris Lattner4a69bad2008-11-30 02:52:26 +00001254 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner4a69bad2008-11-30 02:52:26 +00001255 // The information is now dirty!
Chris Lattnerbf145d62008-12-01 01:15:42 +00001256 INLD.second = true;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001257
Chris Lattnerbf145d62008-12-01 01:15:42 +00001258 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
1259 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnere18b9712009-12-09 07:08:01 +00001260 if (DI->getResult().getInst() != RemInst) continue;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001261
1262 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner0ee443d2009-12-22 04:25:02 +00001263 DI->setResult(NewDirtyVal);
Chris Lattner0655f732008-12-07 18:42:51 +00001264
1265 if (Instruction *NextI = NewDirtyVal.getInst())
Chris Lattnerf68f3102008-11-30 02:28:25 +00001266 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattnerf68f3102008-11-30 02:28:25 +00001267 }
1268 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001269
1270 ReverseNonLocalDeps.erase(ReverseDepIt);
1271
Chris Lattner0ec48dd2008-11-29 22:02:15 +00001272 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
1273 while (!ReverseDepsToAdd.empty()) {
1274 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
1275 .insert(ReverseDepsToAdd.back().second);
1276 ReverseDepsToAdd.pop_back();
1277 }
Owen Anderson4d13de42007-08-16 21:27:05 +00001278 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +00001279
Chris Lattner6290f5c2008-12-07 08:50:20 +00001280 // If the instruction is in ReverseNonLocalPtrDeps then it appears as a
1281 // value in the NonLocalPointerDeps info.
1282 ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =
1283 ReverseNonLocalPtrDeps.find(RemInst);
1284 if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001285 SmallPtrSet<ValueIsLoadPair, 4> &Set = ReversePtrDepIt->second;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001286 SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd;
1287
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001288 for (SmallPtrSet<ValueIsLoadPair, 4>::iterator I = Set.begin(),
1289 E = Set.end(); I != E; ++I) {
1290 ValueIsLoadPair P = *I;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001291 assert(P.getPointer() != RemInst &&
1292 "Already removed NonLocalPointerDeps info for RemInst");
1293
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001294 NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].NonLocalDeps;
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001295
1296 // The cache is not valid for any specific block anymore.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001297 NonLocalPointerDeps[P].Pair = BBSkipFirstBlockPair();
Chris Lattner6290f5c2008-12-07 08:50:20 +00001298
Chris Lattner6290f5c2008-12-07 08:50:20 +00001299 // Update any entries for RemInst to use the instruction after it.
1300 for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end();
1301 DI != DE; ++DI) {
Chris Lattnere18b9712009-12-09 07:08:01 +00001302 if (DI->getResult().getInst() != RemInst) continue;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001303
1304 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner0ee443d2009-12-22 04:25:02 +00001305 DI->setResult(NewDirtyVal);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001306
1307 if (Instruction *NewDirtyInst = NewDirtyVal.getInst())
1308 ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));
1309 }
Chris Lattner95900f22009-01-23 07:12:16 +00001310
1311 // Re-sort the NonLocalDepInfo. Changing the dirty entry to its
1312 // subsequent value may invalidate the sortedness.
1313 std::sort(NLPDI.begin(), NLPDI.end());
Chris Lattner6290f5c2008-12-07 08:50:20 +00001314 }
1315
1316 ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);
1317
1318 while (!ReversePtrDepsToAdd.empty()) {
1319 ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first]
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001320 .insert(ReversePtrDepsToAdd.back().second);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001321 ReversePtrDepsToAdd.pop_back();
1322 }
1323 }
1324
1325
Chris Lattnerf68f3102008-11-30 02:28:25 +00001326 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnerd777d402008-11-30 19:24:31 +00001327 AA->deleteValue(RemInst);
Jakob Stoklund Olesenf7624bc2011-01-11 04:05:39 +00001328 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +00001329}
Chris Lattner729b2372008-11-29 21:25:10 +00001330/// verifyRemoved - Verify that the specified instruction does not occur
1331/// in our internal data structures.
1332void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
1333 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
1334 E = LocalDeps.end(); I != E; ++I) {
1335 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +00001336 assert(I->second.getInst() != D &&
Chris Lattner729b2372008-11-29 21:25:10 +00001337 "Inst occurs in data structures");
1338 }
1339
Chris Lattner6290f5c2008-12-07 08:50:20 +00001340 for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(),
1341 E = NonLocalPointerDeps.end(); I != E; ++I) {
1342 assert(I->first.getPointer() != D && "Inst occurs in NLPD map key");
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001343 const NonLocalDepInfo &Val = I->second.NonLocalDeps;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001344 for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end();
1345 II != E; ++II)
Chris Lattnere18b9712009-12-09 07:08:01 +00001346 assert(II->getResult().getInst() != D && "Inst occurs as NLPD value");
Chris Lattner6290f5c2008-12-07 08:50:20 +00001347 }
1348
Chris Lattner729b2372008-11-29 21:25:10 +00001349 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
1350 E = NonLocalDeps.end(); I != E; ++I) {
1351 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4a69bad2008-11-30 02:52:26 +00001352 const PerInstNLInfo &INLD = I->second;
Chris Lattnerbf145d62008-12-01 01:15:42 +00001353 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
1354 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnere18b9712009-12-09 07:08:01 +00001355 assert(II->getResult().getInst() != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001356 }
1357
1358 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +00001359 E = ReverseLocalDeps.end(); I != E; ++I) {
1360 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001361 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1362 EE = I->second.end(); II != EE; ++II)
1363 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +00001364 }
Chris Lattner729b2372008-11-29 21:25:10 +00001365
1366 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
1367 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +00001368 I != E; ++I) {
1369 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001370 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1371 EE = I->second.end(); II != EE; ++II)
1372 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +00001373 }
Chris Lattner6290f5c2008-12-07 08:50:20 +00001374
1375 for (ReverseNonLocalPtrDepTy::const_iterator
1376 I = ReverseNonLocalPtrDeps.begin(),
1377 E = ReverseNonLocalPtrDeps.end(); I != E; ++I) {
1378 assert(I->first != D && "Inst occurs in rev NLPD map");
1379
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001380 for (SmallPtrSet<ValueIsLoadPair, 4>::const_iterator II = I->second.begin(),
Chris Lattner6290f5c2008-12-07 08:50:20 +00001381 E = I->second.end(); II != E; ++II)
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001382 assert(*II != ValueIsLoadPair(D, false) &&
1383 *II != ValueIsLoadPair(D, true) &&
Chris Lattner6290f5c2008-12-07 08:50:20 +00001384 "Inst occurs in ReverseNonLocalPtrDeps map");
1385 }
1386
Chris Lattner729b2372008-11-29 21:25:10 +00001387}