blob: f29ff4a73141768c262ea62ee4602ed5c94ecb00 [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"
19#include "llvm/Instructions.h"
Owen Andersonf6cec852009-03-09 05:12:38 +000020#include "llvm/IntrinsicInst.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000021#include "llvm/Function.h"
Dan Gohmanc1ac0d72010-09-22 21:41:02 +000022#include "llvm/LLVMContext.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000023#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner6f7b2102009-11-27 22:05:15 +000024#include "llvm/Analysis/Dominators.h"
Chris Lattnere19e4ba2009-11-27 00:34:38 +000025#include "llvm/Analysis/InstructionSimplify.h"
Victor Hernandezf006b182009-10-27 20:05:49 +000026#include "llvm/Analysis/MemoryBuiltins.h"
Chris Lattner05e15f82009-12-09 01:59:31 +000027#include "llvm/Analysis/PHITransAddr.h"
Chris Lattnerbaad8882008-11-28 22:28:27 +000028#include "llvm/ADT/Statistic.h"
Duncan Sands7050f3d2008-12-10 09:38:36 +000029#include "llvm/ADT/STLExtras.h"
Chris Lattner4012fdd2008-12-09 06:28:49 +000030#include "llvm/Support/PredIteratorCache.h"
Chris Lattner0e575f42008-11-28 21:45:17 +000031#include "llvm/Support/Debug.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000032using namespace llvm;
33
Chris Lattnerbf145d62008-12-01 01:15:42 +000034STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
35STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
Chris Lattner0ec48dd2008-11-29 22:02:15 +000036STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Chris Lattner6290f5c2008-12-07 08:50:20 +000037
38STATISTIC(NumCacheNonLocalPtr,
39 "Number of fully cached non-local ptr responses");
40STATISTIC(NumCacheDirtyNonLocalPtr,
41 "Number of cached, but dirty, non-local ptr responses");
42STATISTIC(NumUncacheNonLocalPtr,
43 "Number of uncached non-local ptr responses");
Chris Lattner11dcd8d2008-12-08 07:31:50 +000044STATISTIC(NumCacheCompleteNonLocalPtr,
45 "Number of block queries that were completely cached");
Chris Lattner6290f5c2008-12-07 08:50:20 +000046
Owen Anderson78e02f72007-07-06 23:14:35 +000047char MemoryDependenceAnalysis::ID = 0;
48
Owen Anderson78e02f72007-07-06 23:14:35 +000049// Register this pass...
Owen Anderson2ab36d32010-10-12 19:48:12 +000050INITIALIZE_PASS_BEGIN(MemoryDependenceAnalysis, "memdep",
Owen Andersonce665bd2010-10-07 22:25:06 +000051 "Memory Dependence Analysis", false, true)
Owen Anderson2ab36d32010-10-12 19:48:12 +000052INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
53INITIALIZE_PASS_END(MemoryDependenceAnalysis, "memdep",
54 "Memory Dependence Analysis", false, true)
Owen Anderson78e02f72007-07-06 23:14:35 +000055
Chris Lattner4012fdd2008-12-09 06:28:49 +000056MemoryDependenceAnalysis::MemoryDependenceAnalysis()
Owen Anderson90c579d2010-08-06 18:33:48 +000057: FunctionPass(ID), PredCache(0) {
Owen Anderson081c34b2010-10-19 17:21:58 +000058 initializeMemoryDependenceAnalysisPass(*PassRegistry::getPassRegistry());
Chris Lattner4012fdd2008-12-09 06:28:49 +000059}
60MemoryDependenceAnalysis::~MemoryDependenceAnalysis() {
61}
62
63/// Clean up memory in between runs
64void MemoryDependenceAnalysis::releaseMemory() {
65 LocalDeps.clear();
66 NonLocalDeps.clear();
67 NonLocalPointerDeps.clear();
68 ReverseLocalDeps.clear();
69 ReverseNonLocalDeps.clear();
70 ReverseNonLocalPtrDeps.clear();
71 PredCache->clear();
72}
73
74
75
Owen Anderson78e02f72007-07-06 23:14:35 +000076/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
77///
78void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
79 AU.setPreservesAll();
80 AU.addRequiredTransitive<AliasAnalysis>();
Owen Anderson78e02f72007-07-06 23:14:35 +000081}
82
Chris Lattnerd777d402008-11-30 19:24:31 +000083bool MemoryDependenceAnalysis::runOnFunction(Function &) {
84 AA = &getAnalysis<AliasAnalysis>();
Chris Lattner4012fdd2008-12-09 06:28:49 +000085 if (PredCache == 0)
86 PredCache.reset(new PredIteratorCache());
Chris Lattnerd777d402008-11-30 19:24:31 +000087 return false;
88}
89
Chris Lattnerd44745d2008-12-07 18:39:13 +000090/// RemoveFromReverseMap - This is a helper function that removes Val from
91/// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry.
92template <typename KeyTy>
93static void RemoveFromReverseMap(DenseMap<Instruction*,
Chris Lattner6a0dcc12009-03-29 00:24:04 +000094 SmallPtrSet<KeyTy, 4> > &ReverseMap,
95 Instruction *Inst, KeyTy Val) {
96 typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator
Chris Lattnerd44745d2008-12-07 18:39:13 +000097 InstIt = ReverseMap.find(Inst);
98 assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");
99 bool Found = InstIt->second.erase(Val);
100 assert(Found && "Invalid reverse map!"); Found=Found;
101 if (InstIt->second.empty())
102 ReverseMap.erase(InstIt);
103}
104
Dan Gohman533c2ad2010-11-10 21:51:35 +0000105/// GetLocation - If the given instruction references a specific memory
106/// location, fill in Loc with the details, otherwise set Loc.Ptr to null.
107/// Return a ModRefInfo value describing the general behavior of the
108/// instruction.
109static
110AliasAnalysis::ModRefResult GetLocation(const Instruction *Inst,
111 AliasAnalysis::Location &Loc,
112 AliasAnalysis *AA) {
113 if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
114 if (LI->isVolatile()) {
115 Loc = AliasAnalysis::Location();
116 return AliasAnalysis::ModRef;
117 }
118 Loc = AliasAnalysis::Location(LI->getPointerOperand(),
119 AA->getTypeStoreSize(LI->getType()),
120 LI->getMetadata(LLVMContext::MD_tbaa));
121 return AliasAnalysis::Ref;
122 }
123
124 if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
125 if (SI->isVolatile()) {
126 Loc = AliasAnalysis::Location();
127 return AliasAnalysis::ModRef;
128 }
129 Loc = AliasAnalysis::Location(SI->getPointerOperand(),
130 AA->getTypeStoreSize(SI->getValueOperand()
131 ->getType()),
132 SI->getMetadata(LLVMContext::MD_tbaa));
133 return AliasAnalysis::Mod;
134 }
135
136 if (const VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
137 Loc = AliasAnalysis::Location(V->getPointerOperand(),
138 AA->getTypeStoreSize(V->getType()),
139 V->getMetadata(LLVMContext::MD_tbaa));
140 return AliasAnalysis::ModRef;
141 }
142
143 if (const CallInst *CI = isFreeCall(Inst)) {
144 // calls to free() deallocate the entire structure
145 Loc = AliasAnalysis::Location(CI->getArgOperand(0));
146 return AliasAnalysis::Mod;
147 }
148
149 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
150 switch (II->getIntrinsicID()) {
151 case Intrinsic::lifetime_start:
152 case Intrinsic::lifetime_end:
153 case Intrinsic::invariant_start:
154 Loc = AliasAnalysis::Location(II->getArgOperand(1),
155 cast<ConstantInt>(II->getArgOperand(0))
156 ->getZExtValue(),
157 II->getMetadata(LLVMContext::MD_tbaa));
158 // These intrinsics don't really modify the memory, but returning Mod
159 // will allow them to be handled conservatively.
160 return AliasAnalysis::Mod;
161 case Intrinsic::invariant_end:
162 Loc = AliasAnalysis::Location(II->getArgOperand(2),
163 cast<ConstantInt>(II->getArgOperand(1))
164 ->getZExtValue(),
165 II->getMetadata(LLVMContext::MD_tbaa));
166 // These intrinsics don't really modify the memory, but returning Mod
167 // will allow them to be handled conservatively.
168 return AliasAnalysis::Mod;
169 default:
170 break;
171 }
172
173 // Otherwise, just do the coarse-grained thing that always works.
174 if (Inst->mayWriteToMemory())
175 return AliasAnalysis::ModRef;
176 if (Inst->mayReadFromMemory())
177 return AliasAnalysis::Ref;
178 return AliasAnalysis::NoModRef;
179}
Chris Lattnerbf145d62008-12-01 01:15:42 +0000180
Chris Lattner8ef57c52008-12-07 00:35:51 +0000181/// getCallSiteDependencyFrom - Private helper for finding the local
182/// dependencies of a call site.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000183MemDepResult MemoryDependenceAnalysis::
Chris Lattner20d6f092008-12-09 21:19:42 +0000184getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall,
185 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Owen Anderson642a9e32007-08-08 22:26:03 +0000186 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000187 while (ScanIt != BB->begin()) {
188 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +0000189
190 // If this inst is a memory op, get the pointer it accessed
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000191 AliasAnalysis::Location Loc;
Dan Gohman533c2ad2010-11-10 21:51:35 +0000192 AliasAnalysis::ModRefResult MR = GetLocation(Inst, Loc, AA);
193 if (Loc.Ptr) {
194 // A simple instruction.
195 if (AA->getModRefInfo(CS, Loc) != AliasAnalysis::NoModRef)
196 return MemDepResult::getClobber(Inst);
197 continue;
198 }
199
200 if (CallSite InstCS = cast<Value>(Inst)) {
Owen Andersonf6cec852009-03-09 05:12:38 +0000201 // Debug intrinsics don't cause dependences.
Dale Johannesen497cb6f2009-03-11 21:13:01 +0000202 if (isa<DbgInfoIntrinsic>(Inst)) continue;
Chris Lattnerb51deb92008-12-05 21:04:20 +0000203 // If these two calls do not interfere, look past it.
Chris Lattner20d6f092008-12-09 21:19:42 +0000204 switch (AA->getModRefInfo(CS, InstCS)) {
205 case AliasAnalysis::NoModRef:
Dan Gohman5fa417c2010-08-05 22:09:15 +0000206 // If the two calls are the same, return InstCS as a Def, so that
207 // CS can be found redundant and eliminated.
Dan Gohman533c2ad2010-11-10 21:51:35 +0000208 if (isReadOnlyCall && !(MR & AliasAnalysis::Mod) &&
Dan Gohman5fa417c2010-08-05 22:09:15 +0000209 CS.getInstruction()->isIdenticalToWhenDefined(Inst))
210 return MemDepResult::getDef(Inst);
211
212 // Otherwise if the two calls don't interact (e.g. InstCS is readnone)
213 // keep scanning.
Dan Gohman533c2ad2010-11-10 21:51:35 +0000214 break;
Chris Lattner20d6f092008-12-09 21:19:42 +0000215 default:
Chris Lattnerb51deb92008-12-05 21:04:20 +0000216 return MemDepResult::getClobber(Inst);
Chris Lattner20d6f092008-12-09 21:19:42 +0000217 }
Chris Lattnercfbb6342008-11-30 01:44:00 +0000218 }
Owen Anderson5f323202007-07-10 17:59:22 +0000219 }
220
Chris Lattner7ebcf032008-12-07 02:15:47 +0000221 // No dependence found. If this is the entry block of the function, it is a
222 // clobber, otherwise it is non-local.
223 if (BB != &BB->getParent()->getEntryBlock())
224 return MemDepResult::getNonLocal();
225 return MemDepResult::getClobber(ScanIt);
Owen Anderson5f323202007-07-10 17:59:22 +0000226}
227
Chris Lattnere79be942008-12-07 01:50:16 +0000228/// getPointerDependencyFrom - Return the instruction on which a memory
Dan Gohmancd5c1232010-10-29 01:14:04 +0000229/// location depends. If isLoad is true, this routine ignores may-aliases with
230/// read-only operations. If isLoad is false, this routine ignores may-aliases
231/// with reads from read-only locations.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000232MemDepResult MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000233getPointerDependencyFrom(const AliasAnalysis::Location &MemLoc, bool isLoad,
Chris Lattnere79be942008-12-07 01:50:16 +0000234 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000235
Chris Lattner1e8de492009-12-01 21:16:01 +0000236 Value *InvariantTag = 0;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000237
Chris Lattner6290f5c2008-12-07 08:50:20 +0000238 // Walk backwards through the basic block, looking for dependencies.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000239 while (ScanIt != BB->begin()) {
240 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000241
Owen Anderson4bc737c2009-10-28 06:18:42 +0000242 // If we're in an invariant region, no dependencies can be found before
243 // we pass an invariant-begin marker.
Chris Lattner1e8de492009-12-01 21:16:01 +0000244 if (InvariantTag == Inst) {
245 InvariantTag = 0;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000246 continue;
Chris Lattner1ffb70f2009-12-01 21:15:15 +0000247 }
248
249 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
Chris Lattner09981982010-09-06 03:58:04 +0000250 // Debug intrinsics don't (and can't) cause dependences.
Chris Lattnerc5a5cf22010-09-06 01:26:29 +0000251 if (isa<DbgInfoIntrinsic>(II)) continue;
Owen Anderson9ff5a232009-12-02 07:35:19 +0000252
Owen Andersonb62f7922009-10-28 07:05:35 +0000253 // If we pass an invariant-end marker, then we've just entered an
254 // invariant region and can start ignoring dependencies.
Owen Anderson4bc737c2009-10-28 06:18:42 +0000255 if (II->getIntrinsicID() == Intrinsic::invariant_end) {
Owen Anderson9ff5a232009-12-02 07:35:19 +0000256 // FIXME: This only considers queries directly on the invariant-tagged
257 // pointer, not on query pointers that are indexed off of them. It'd
258 // be nice to handle that at some point.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000259 AliasAnalysis::AliasResult R =
260 AA->alias(AliasAnalysis::Location(II->getArgOperand(2)), MemLoc);
Chris Lattner09981982010-09-06 03:58:04 +0000261 if (R == AliasAnalysis::MustAlias)
Gabor Greif8ff72b52010-06-23 22:48:06 +0000262 InvariantTag = II->getArgOperand(0);
Chris Lattner09981982010-09-06 03:58:04 +0000263
264 continue;
265 }
266
Owen Andersonb62f7922009-10-28 07:05:35 +0000267 // If we reach a lifetime begin or end marker, then the query ends here
268 // because the value is undefined.
Chris Lattner09981982010-09-06 03:58:04 +0000269 if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
Owen Anderson9ff5a232009-12-02 07:35:19 +0000270 // FIXME: This only considers queries directly on the invariant-tagged
271 // pointer, not on query pointers that are indexed off of them. It'd
272 // be nice to handle that at some point.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000273 AliasAnalysis::AliasResult R =
274 AA->alias(AliasAnalysis::Location(II->getArgOperand(1)), MemLoc);
Owen Andersonb62f7922009-10-28 07:05:35 +0000275 if (R == AliasAnalysis::MustAlias)
276 return MemDepResult::getDef(II);
Chris Lattner09981982010-09-06 03:58:04 +0000277 continue;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000278 }
279 }
280
281 // If we're querying on a load and we're in an invariant region, we're done
282 // at this point. Nothing a load depends on can live in an invariant region.
Chris Lattner09981982010-09-06 03:58:04 +0000283 //
284 // FIXME: this will prevent us from returning load/load must-aliases, so GVN
285 // won't remove redundant loads.
Chris Lattner1e8de492009-12-01 21:16:01 +0000286 if (isLoad && InvariantTag) continue;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000287
Chris Lattnercfbb6342008-11-30 01:44:00 +0000288 // Values depend on loads if the pointers are must aliased. This means that
289 // a load depends on another must aliased load from the same value.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000290 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000291 Value *Pointer = LI->getPointerOperand();
Dan Gohmanf5812132009-07-31 20:53:12 +0000292 uint64_t PointerSize = AA->getTypeStoreSize(LI->getType());
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000293 MDNode *TBAATag = LI->getMetadata(LLVMContext::MD_tbaa);
Dan Gohmancd5c1232010-10-29 01:14:04 +0000294 AliasAnalysis::Location LoadLoc(Pointer, PointerSize, TBAATag);
Chris Lattnerb51deb92008-12-05 21:04:20 +0000295
296 // If we found a pointer, check if it could be the same as our pointer.
Dan Gohmancd5c1232010-10-29 01:14:04 +0000297 AliasAnalysis::AliasResult R = AA->alias(LoadLoc, MemLoc);
Chris Lattnera161ab02008-11-29 09:09:48 +0000298 if (R == AliasAnalysis::NoAlias)
299 continue;
300
301 // May-alias loads don't depend on each other without a dependence.
Chris Lattnere79be942008-12-07 01:50:16 +0000302 if (isLoad && R == AliasAnalysis::MayAlias)
Chris Lattnera161ab02008-11-29 09:09:48 +0000303 continue;
Dan Gohmancd5c1232010-10-29 01:14:04 +0000304
305 // Stores don't alias loads from read-only memory.
306 if (!isLoad && AA->pointsToConstantMemory(LoadLoc))
307 continue;
308
Chris Lattner6290f5c2008-12-07 08:50:20 +0000309 // Stores depend on may and must aliased loads, loads depend on must-alias
310 // loads.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000311 return MemDepResult::getDef(Inst);
312 }
313
314 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Owen Andersona85a6642009-10-28 06:30:52 +0000315 // There can't be stores to the value we care about inside an
316 // invariant region.
Chris Lattner1e8de492009-12-01 21:16:01 +0000317 if (InvariantTag) continue;
Owen Andersona85a6642009-10-28 06:30:52 +0000318
Chris Lattnerab9cf122009-05-25 21:28:56 +0000319 // If alias analysis can tell that this store is guaranteed to not modify
320 // the query pointer, ignore it. Use getModRefInfo to handle cases where
321 // the query pointer points to constant memory etc.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000322 if (AA->getModRefInfo(SI, MemLoc) == AliasAnalysis::NoModRef)
Chris Lattnerab9cf122009-05-25 21:28:56 +0000323 continue;
324
325 // Ok, this store might clobber the query pointer. Check to see if it is
326 // a must alias: in this case, we want to return this as a def.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000327 Value *Pointer = SI->getPointerOperand();
Dan Gohmanf5812132009-07-31 20:53:12 +0000328 uint64_t PointerSize = AA->getTypeStoreSize(SI->getOperand(0)->getType());
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000329 MDNode *TBAATag = SI->getMetadata(LLVMContext::MD_tbaa);
Chris Lattnerab9cf122009-05-25 21:28:56 +0000330
Chris Lattnerb51deb92008-12-05 21:04:20 +0000331 // If we found a pointer, check if it could be the same as our pointer.
332 AliasAnalysis::AliasResult R =
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000333 AA->alias(AliasAnalysis::Location(Pointer, PointerSize, TBAATag),
334 MemLoc);
Chris Lattnerb51deb92008-12-05 21:04:20 +0000335
336 if (R == AliasAnalysis::NoAlias)
337 continue;
338 if (R == AliasAnalysis::MayAlias)
339 return MemDepResult::getClobber(Inst);
340 return MemDepResult::getDef(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000341 }
Chris Lattner237a8282008-11-30 01:39:32 +0000342
343 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattnerb51deb92008-12-05 21:04:20 +0000344 // the allocation, return Def. This means that there is no dependence and
Chris Lattner237a8282008-11-30 01:39:32 +0000345 // the access can be optimized based on that. For example, a load could
346 // turn into undef.
Victor Hernandez5c787362009-10-13 01:42:53 +0000347 // Note: Only determine this to be a malloc if Inst is the malloc call, not
348 // a subsequent bitcast of the malloc call result. There can be stores to
349 // the malloced memory between the malloc call and its bitcast uses, and we
350 // need to continue scanning until the malloc call.
Chris Lattner9b96eca2009-12-22 01:00:32 +0000351 if (isa<AllocaInst>(Inst) ||
352 (isa<CallInst>(Inst) && extractMallocCall(Inst))) {
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000353 const Value *AccessPtr = MemLoc.Ptr->getUnderlyingObject();
Victor Hernandez46e83122009-09-18 21:34:51 +0000354
355 if (AccessPtr == Inst ||
356 AA->alias(Inst, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
357 return MemDepResult::getDef(Inst);
358 continue;
359 }
360
Chris Lattnerb51deb92008-12-05 21:04:20 +0000361 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000362 switch (AA->getModRefInfo(Inst, MemLoc)) {
Chris Lattner3579e442008-12-09 19:47:40 +0000363 case AliasAnalysis::NoModRef:
364 // If the call has no effect on the queried pointer, just ignore it.
Chris Lattner25a08142008-11-29 08:51:16 +0000365 continue;
Owen Andersona85a6642009-10-28 06:30:52 +0000366 case AliasAnalysis::Mod:
367 // If we're in an invariant region, we can ignore calls that ONLY
368 // modify the pointer.
Chris Lattner1e8de492009-12-01 21:16:01 +0000369 if (InvariantTag) continue;
Owen Andersona85a6642009-10-28 06:30:52 +0000370 return MemDepResult::getClobber(Inst);
Chris Lattner3579e442008-12-09 19:47:40 +0000371 case AliasAnalysis::Ref:
372 // If the call is known to never store to the pointer, and if this is a
373 // load query, we can safely ignore it (scan past it).
374 if (isLoad)
375 continue;
Chris Lattner3579e442008-12-09 19:47:40 +0000376 default:
377 // Otherwise, there is a potential dependence. Return a clobber.
378 return MemDepResult::getClobber(Inst);
379 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000380 }
381
Chris Lattner7ebcf032008-12-07 02:15:47 +0000382 // No dependence found. If this is the entry block of the function, it is a
383 // clobber, otherwise it is non-local.
384 if (BB != &BB->getParent()->getEntryBlock())
385 return MemDepResult::getNonLocal();
386 return MemDepResult::getClobber(ScanIt);
Owen Anderson78e02f72007-07-06 23:14:35 +0000387}
388
Chris Lattner5391a1d2008-11-29 03:47:00 +0000389/// getDependency - Return the instruction on which a memory operation
390/// depends.
391MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
392 Instruction *ScanPos = QueryInst;
393
394 // Check for a cached result
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000395 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattner5391a1d2008-11-29 03:47:00 +0000396
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000397 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000398 // on MemDepResult's default constructing to 'dirty'.
399 if (!LocalCache.isDirty())
400 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000401
402 // Otherwise, if we have a dirty entry, we know we can start the scan at that
403 // instruction, which may save us some work.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000404 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000405 ScanPos = Inst;
Chris Lattner4a69bad2008-11-30 02:52:26 +0000406
Chris Lattnerd44745d2008-12-07 18:39:13 +0000407 RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);
Chris Lattner4a69bad2008-11-30 02:52:26 +0000408 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000409
Chris Lattnere79be942008-12-07 01:50:16 +0000410 BasicBlock *QueryParent = QueryInst->getParent();
411
Chris Lattner5391a1d2008-11-29 03:47:00 +0000412 // Do the scan.
Chris Lattnere79be942008-12-07 01:50:16 +0000413 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000414 // No dependence found. If this is the entry block of the function, it is a
415 // clobber, otherwise it is non-local.
416 if (QueryParent != &QueryParent->getParent()->getEntryBlock())
417 LocalCache = MemDepResult::getNonLocal();
418 else
419 LocalCache = MemDepResult::getClobber(QueryInst);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000420 } else {
421 AliasAnalysis::Location MemLoc;
422 AliasAnalysis::ModRefResult MR = GetLocation(QueryInst, MemLoc, AA);
423 if (MemLoc.Ptr) {
424 // If we can do a pointer scan, make it happen.
425 bool isLoad = !(MR & AliasAnalysis::Mod);
426 if (IntrinsicInst *II = dyn_cast<MemoryUseIntrinsic>(QueryInst)) {
427 isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_end;
428 }
429 LocalCache = getPointerDependencyFrom(MemLoc, isLoad, ScanPos,
430 QueryParent);
431 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Gabor Greif622b7cf2010-07-27 22:02:00 +0000432 CallSite QueryCS(QueryInst);
Nick Lewycky93d33112009-12-05 06:37:24 +0000433 bool isReadOnly = AA->onlyReadsMemory(QueryCS);
434 LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos,
435 QueryParent);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000436 } else
437 // Non-memory instruction.
438 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
Nick Lewyckyd801c102009-11-28 21:27:49 +0000439 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000440
441 // Remember the result!
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000442 if (Instruction *I = LocalCache.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000443 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000444
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000445 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000446}
447
Chris Lattner12a7db32009-01-22 07:04:01 +0000448#ifndef NDEBUG
449/// AssertSorted - This method is used when -debug is specified to verify that
450/// cache arrays are properly kept sorted.
451static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
452 int Count = -1) {
453 if (Count == -1) Count = Cache.size();
454 if (Count == 0) return;
455
456 for (unsigned i = 1; i != unsigned(Count); ++i)
Chris Lattnere18b9712009-12-09 07:08:01 +0000457 assert(!(Cache[i] < Cache[i-1]) && "Cache isn't sorted!");
Chris Lattner12a7db32009-01-22 07:04:01 +0000458}
459#endif
460
Chris Lattner1559b362008-12-09 19:38:05 +0000461/// getNonLocalCallDependency - Perform a full dependency query for the
462/// specified call, returning the set of blocks that the value is
Chris Lattner37d041c2008-11-30 01:18:27 +0000463/// potentially live across. The returned set of results will include a
464/// "NonLocal" result for all blocks where the value is live across.
465///
Chris Lattner1559b362008-12-09 19:38:05 +0000466/// This method assumes the instruction returns a "NonLocal" dependency
Chris Lattner37d041c2008-11-30 01:18:27 +0000467/// within its own block.
468///
Chris Lattner1559b362008-12-09 19:38:05 +0000469/// This returns a reference to an internal data structure that may be
470/// invalidated on the next non-local query or when an instruction is
471/// removed. Clients must copy this data if they want it around longer than
472/// that.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000473const MemoryDependenceAnalysis::NonLocalDepInfo &
Chris Lattner1559b362008-12-09 19:38:05 +0000474MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) {
475 assert(getDependency(QueryCS.getInstruction()).isNonLocal() &&
476 "getNonLocalCallDependency should only be used on calls with non-local deps!");
477 PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()];
Chris Lattnerbf145d62008-12-01 01:15:42 +0000478 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner37d041c2008-11-30 01:18:27 +0000479
480 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
481 /// the cached case, this can happen due to instructions being deleted etc. In
482 /// the uncached case, this starts out as the set of predecessors we care
483 /// about.
484 SmallVector<BasicBlock*, 32> DirtyBlocks;
485
486 if (!Cache.empty()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000487 // Okay, we have a cache entry. If we know it is not dirty, just return it
488 // with no computation.
489 if (!CacheP.second) {
Dan Gohmanfe601042010-06-22 15:08:57 +0000490 ++NumCacheNonLocal;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000491 return Cache;
492 }
493
Chris Lattner37d041c2008-11-30 01:18:27 +0000494 // If we already have a partially computed set of results, scan them to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000495 // determine what is dirty, seeding our initial DirtyBlocks worklist.
496 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
497 I != E; ++I)
Chris Lattnere18b9712009-12-09 07:08:01 +0000498 if (I->getResult().isDirty())
499 DirtyBlocks.push_back(I->getBB());
Chris Lattner37d041c2008-11-30 01:18:27 +0000500
Chris Lattnerbf145d62008-12-01 01:15:42 +0000501 // Sort the cache so that we can do fast binary search lookups below.
502 std::sort(Cache.begin(), Cache.end());
Chris Lattner37d041c2008-11-30 01:18:27 +0000503
Chris Lattnerbf145d62008-12-01 01:15:42 +0000504 ++NumCacheDirtyNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000505 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
506 // << Cache.size() << " cached: " << *QueryInst;
507 } else {
508 // Seed DirtyBlocks with each of the preds of QueryInst's block.
Chris Lattner1559b362008-12-09 19:38:05 +0000509 BasicBlock *QueryBB = QueryCS.getInstruction()->getParent();
Chris Lattner511b36c2008-12-09 06:44:17 +0000510 for (BasicBlock **PI = PredCache->GetPreds(QueryBB); *PI; ++PI)
511 DirtyBlocks.push_back(*PI);
Dan Gohmanfe601042010-06-22 15:08:57 +0000512 ++NumUncacheNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000513 }
514
Chris Lattner20d6f092008-12-09 21:19:42 +0000515 // isReadonlyCall - If this is a read-only call, we can be more aggressive.
516 bool isReadonlyCall = AA->onlyReadsMemory(QueryCS);
Chris Lattner9e59c642008-12-15 03:35:32 +0000517
Chris Lattnerbf145d62008-12-01 01:15:42 +0000518 SmallPtrSet<BasicBlock*, 64> Visited;
519
520 unsigned NumSortedEntries = Cache.size();
Chris Lattner12a7db32009-01-22 07:04:01 +0000521 DEBUG(AssertSorted(Cache));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000522
Chris Lattner37d041c2008-11-30 01:18:27 +0000523 // Iterate while we still have blocks to update.
524 while (!DirtyBlocks.empty()) {
525 BasicBlock *DirtyBB = DirtyBlocks.back();
526 DirtyBlocks.pop_back();
527
Chris Lattnerbf145d62008-12-01 01:15:42 +0000528 // Already processed this block?
529 if (!Visited.insert(DirtyBB))
530 continue;
Chris Lattner37d041c2008-11-30 01:18:27 +0000531
Chris Lattnerbf145d62008-12-01 01:15:42 +0000532 // Do a binary search to see if we already have an entry for this block in
533 // the cache set. If so, find it.
Chris Lattner12a7db32009-01-22 07:04:01 +0000534 DEBUG(AssertSorted(Cache, NumSortedEntries));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000535 NonLocalDepInfo::iterator Entry =
536 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
Chris Lattnerdad451c2009-12-09 07:31:04 +0000537 NonLocalDepEntry(DirtyBB));
Chris Lattnere18b9712009-12-09 07:08:01 +0000538 if (Entry != Cache.begin() && prior(Entry)->getBB() == DirtyBB)
Chris Lattnerbf145d62008-12-01 01:15:42 +0000539 --Entry;
540
Chris Lattnere18b9712009-12-09 07:08:01 +0000541 NonLocalDepEntry *ExistingResult = 0;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000542 if (Entry != Cache.begin()+NumSortedEntries &&
Chris Lattnere18b9712009-12-09 07:08:01 +0000543 Entry->getBB() == DirtyBB) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000544 // If we already have an entry, and if it isn't already dirty, the block
545 // is done.
Chris Lattnere18b9712009-12-09 07:08:01 +0000546 if (!Entry->getResult().isDirty())
Chris Lattnerbf145d62008-12-01 01:15:42 +0000547 continue;
548
549 // Otherwise, remember this slot so we can update the value.
Chris Lattnere18b9712009-12-09 07:08:01 +0000550 ExistingResult = &*Entry;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000551 }
552
Chris Lattner37d041c2008-11-30 01:18:27 +0000553 // If the dirty entry has a pointer, start scanning from it so we don't have
554 // to rescan the entire block.
555 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerbf145d62008-12-01 01:15:42 +0000556 if (ExistingResult) {
Chris Lattnere18b9712009-12-09 07:08:01 +0000557 if (Instruction *Inst = ExistingResult->getResult().getInst()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000558 ScanPos = Inst;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000559 // We're removing QueryInst's use of Inst.
Chris Lattner1559b362008-12-09 19:38:05 +0000560 RemoveFromReverseMap(ReverseNonLocalDeps, Inst,
561 QueryCS.getInstruction());
Chris Lattnerbf145d62008-12-01 01:15:42 +0000562 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000563 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000564
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000565 // Find out if this block has a local dependency for QueryInst.
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000566 MemDepResult Dep;
Chris Lattnere79be942008-12-07 01:50:16 +0000567
Chris Lattner1559b362008-12-09 19:38:05 +0000568 if (ScanPos != DirtyBB->begin()) {
Chris Lattner20d6f092008-12-09 21:19:42 +0000569 Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB);
Chris Lattner1559b362008-12-09 19:38:05 +0000570 } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {
571 // No dependence found. If this is the entry block of the function, it is
572 // a clobber, otherwise it is non-local.
573 Dep = MemDepResult::getNonLocal();
Chris Lattnere79be942008-12-07 01:50:16 +0000574 } else {
Chris Lattner1559b362008-12-09 19:38:05 +0000575 Dep = MemDepResult::getClobber(ScanPos);
Chris Lattnere79be942008-12-07 01:50:16 +0000576 }
577
Chris Lattnerbf145d62008-12-01 01:15:42 +0000578 // If we had a dirty entry for the block, update it. Otherwise, just add
579 // a new entry.
580 if (ExistingResult)
Chris Lattner0ee443d2009-12-22 04:25:02 +0000581 ExistingResult->setResult(Dep);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000582 else
Chris Lattner0ee443d2009-12-22 04:25:02 +0000583 Cache.push_back(NonLocalDepEntry(DirtyBB, Dep));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000584
Chris Lattner37d041c2008-11-30 01:18:27 +0000585 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000586 // the value), remember the association!
587 if (!Dep.isNonLocal()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000588 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
589 // update this when we remove instructions.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000590 if (Instruction *Inst = Dep.getInst())
Chris Lattner1559b362008-12-09 19:38:05 +0000591 ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction());
Chris Lattnerbf145d62008-12-01 01:15:42 +0000592 } else {
Chris Lattner37d041c2008-11-30 01:18:27 +0000593
Chris Lattnerbf145d62008-12-01 01:15:42 +0000594 // If the block *is* completely transparent to the load, we need to check
595 // the predecessors of this block. Add them to our worklist.
Chris Lattner511b36c2008-12-09 06:44:17 +0000596 for (BasicBlock **PI = PredCache->GetPreds(DirtyBB); *PI; ++PI)
597 DirtyBlocks.push_back(*PI);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000598 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000599 }
600
Chris Lattnerbf145d62008-12-01 01:15:42 +0000601 return Cache;
Chris Lattner37d041c2008-11-30 01:18:27 +0000602}
603
Chris Lattner7ebcf032008-12-07 02:15:47 +0000604/// getNonLocalPointerDependency - Perform a full dependency query for an
605/// access to the specified (non-volatile) memory location, returning the
606/// set of instructions that either define or clobber the value.
607///
608/// This method assumes the pointer has a "NonLocal" dependency within its
609/// own block.
610///
611void MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000612getNonLocalPointerDependency(const AliasAnalysis::Location &Loc, bool isLoad,
613 BasicBlock *FromBB,
Chris Lattner0ee443d2009-12-22 04:25:02 +0000614 SmallVectorImpl<NonLocalDepResult> &Result) {
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000615 assert(Loc.Ptr->getType()->isPointerTy() &&
Chris Lattner3f7eb5b2008-12-07 18:45:15 +0000616 "Can't get pointer deps of a non-pointer!");
Chris Lattner9a193fd2008-12-07 02:56:57 +0000617 Result.clear();
618
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000619 PHITransAddr Address(const_cast<Value *>(Loc.Ptr), TD);
Chris Lattner05e15f82009-12-09 01:59:31 +0000620
Chris Lattner9e59c642008-12-15 03:35:32 +0000621 // This is the set of blocks we've inspected, and the pointer we consider in
622 // each block. Because of critical edges, we currently bail out if querying
623 // a block with multiple different pointers. This can happen during PHI
624 // translation.
625 DenseMap<BasicBlock*, Value*> Visited;
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000626 if (!getNonLocalPointerDepFromBB(Address, Loc, isLoad, FromBB,
Chris Lattner9e59c642008-12-15 03:35:32 +0000627 Result, Visited, true))
628 return;
Chris Lattner3af23f82008-12-15 04:58:29 +0000629 Result.clear();
Chris Lattner0ee443d2009-12-22 04:25:02 +0000630 Result.push_back(NonLocalDepResult(FromBB,
631 MemDepResult::getClobber(FromBB->begin()),
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000632 const_cast<Value *>(Loc.Ptr)));
Chris Lattner9a193fd2008-12-07 02:56:57 +0000633}
634
Chris Lattner9863c3f2008-12-09 07:47:11 +0000635/// GetNonLocalInfoForBlock - Compute the memdep value for BB with
636/// Pointer/PointeeSize using either cached information in Cache or by doing a
637/// lookup (which may use dirty cache info if available). If we do a lookup,
638/// add the result to the cache.
639MemDepResult MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000640GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
Chris Lattner9863c3f2008-12-09 07:47:11 +0000641 bool isLoad, BasicBlock *BB,
642 NonLocalDepInfo *Cache, unsigned NumSortedEntries) {
643
644 // Do a binary search to see if we already have an entry for this block in
645 // the cache set. If so, find it.
646 NonLocalDepInfo::iterator Entry =
647 std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries,
Chris Lattnerdad451c2009-12-09 07:31:04 +0000648 NonLocalDepEntry(BB));
Chris Lattnere18b9712009-12-09 07:08:01 +0000649 if (Entry != Cache->begin() && (Entry-1)->getBB() == BB)
Chris Lattner9863c3f2008-12-09 07:47:11 +0000650 --Entry;
651
Chris Lattnere18b9712009-12-09 07:08:01 +0000652 NonLocalDepEntry *ExistingResult = 0;
653 if (Entry != Cache->begin()+NumSortedEntries && Entry->getBB() == BB)
654 ExistingResult = &*Entry;
Chris Lattner9863c3f2008-12-09 07:47:11 +0000655
656 // If we have a cached entry, and it is non-dirty, use it as the value for
657 // this dependency.
Chris Lattnere18b9712009-12-09 07:08:01 +0000658 if (ExistingResult && !ExistingResult->getResult().isDirty()) {
Chris Lattner9863c3f2008-12-09 07:47:11 +0000659 ++NumCacheNonLocalPtr;
Chris Lattnere18b9712009-12-09 07:08:01 +0000660 return ExistingResult->getResult();
Chris Lattner9863c3f2008-12-09 07:47:11 +0000661 }
662
663 // Otherwise, we have to scan for the value. If we have a dirty cache
664 // entry, start scanning from its position, otherwise we scan from the end
665 // of the block.
666 BasicBlock::iterator ScanPos = BB->end();
Chris Lattnere18b9712009-12-09 07:08:01 +0000667 if (ExistingResult && ExistingResult->getResult().getInst()) {
668 assert(ExistingResult->getResult().getInst()->getParent() == BB &&
Chris Lattner9863c3f2008-12-09 07:47:11 +0000669 "Instruction invalidated?");
670 ++NumCacheDirtyNonLocalPtr;
Chris Lattnere18b9712009-12-09 07:08:01 +0000671 ScanPos = ExistingResult->getResult().getInst();
Chris Lattner9863c3f2008-12-09 07:47:11 +0000672
673 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000674 ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
Chris Lattner6a0dcc12009-03-29 00:24:04 +0000675 RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000676 } else {
677 ++NumUncacheNonLocalPtr;
678 }
679
680 // Scan the block for the dependency.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000681 MemDepResult Dep = getPointerDependencyFrom(Loc, isLoad, ScanPos, BB);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000682
683 // If we had a dirty entry for the block, update it. Otherwise, just add
684 // a new entry.
685 if (ExistingResult)
Chris Lattner0ee443d2009-12-22 04:25:02 +0000686 ExistingResult->setResult(Dep);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000687 else
Chris Lattner0ee443d2009-12-22 04:25:02 +0000688 Cache->push_back(NonLocalDepEntry(BB, Dep));
Chris Lattner9863c3f2008-12-09 07:47:11 +0000689
690 // If the block has a dependency (i.e. it isn't completely transparent to
691 // the value), remember the reverse association because we just added it
692 // to Cache!
693 if (Dep.isNonLocal())
694 return Dep;
695
696 // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently
697 // update MemDep when we remove instructions.
698 Instruction *Inst = Dep.getInst();
699 assert(Inst && "Didn't depend on anything?");
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000700 ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
Chris Lattner6a0dcc12009-03-29 00:24:04 +0000701 ReverseNonLocalPtrDeps[Inst].insert(CacheKey);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000702 return Dep;
703}
704
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000705/// SortNonLocalDepInfoCache - Sort the a NonLocalDepInfo cache, given a certain
706/// number of elements in the array that are already properly ordered. This is
707/// optimized for the case when only a few entries are added.
708static void
709SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
710 unsigned NumSortedEntries) {
711 switch (Cache.size() - NumSortedEntries) {
712 case 0:
713 // done, no new entries.
714 break;
715 case 2: {
716 // Two new entries, insert the last one into place.
Chris Lattnere18b9712009-12-09 07:08:01 +0000717 NonLocalDepEntry Val = Cache.back();
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000718 Cache.pop_back();
719 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
720 std::upper_bound(Cache.begin(), Cache.end()-1, Val);
721 Cache.insert(Entry, Val);
722 // FALL THROUGH.
723 }
724 case 1:
725 // One new entry, Just insert the new value at the appropriate position.
726 if (Cache.size() != 1) {
Chris Lattnere18b9712009-12-09 07:08:01 +0000727 NonLocalDepEntry Val = Cache.back();
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000728 Cache.pop_back();
729 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
730 std::upper_bound(Cache.begin(), Cache.end(), Val);
731 Cache.insert(Entry, Val);
732 }
733 break;
734 default:
735 // Added many values, do a full scale sort.
736 std::sort(Cache.begin(), Cache.end());
737 break;
738 }
739}
740
Chris Lattner9e59c642008-12-15 03:35:32 +0000741/// getNonLocalPointerDepFromBB - Perform a dependency query based on
742/// pointer/pointeesize starting at the end of StartBB. Add any clobber/def
743/// results to the results vector and keep track of which blocks are visited in
744/// 'Visited'.
745///
746/// This has special behavior for the first block queries (when SkipFirstBlock
747/// is true). In this special case, it ignores the contents of the specified
748/// block and starts returning dependence info for its predecessors.
749///
750/// This function returns false on success, or true to indicate that it could
751/// not compute dependence information for some reason. This should be treated
752/// as a clobber dependence on the first instruction in the predecessor block.
753bool MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000754getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
755 const AliasAnalysis::Location &Loc,
Chris Lattner9863c3f2008-12-09 07:47:11 +0000756 bool isLoad, BasicBlock *StartBB,
Chris Lattner0ee443d2009-12-22 04:25:02 +0000757 SmallVectorImpl<NonLocalDepResult> &Result,
Chris Lattner9e59c642008-12-15 03:35:32 +0000758 DenseMap<BasicBlock*, Value*> &Visited,
759 bool SkipFirstBlock) {
Chris Lattner66364342009-09-20 22:44:26 +0000760
Chris Lattner6290f5c2008-12-07 08:50:20 +0000761 // Look up the cached info for Pointer.
Chris Lattner05e15f82009-12-09 01:59:31 +0000762 ValueIsLoadPair CacheKey(Pointer.getAddr(), isLoad);
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000763
Dan Gohman075fb5d2010-11-10 20:37:15 +0000764 // Set up a temporary NLPI value. If the map doesn't yet have an entry for
765 // CacheKey, this value will be inserted as the associated value. Otherwise,
766 // it'll be ignored, and we'll have to check to see if the cached size and
767 // tbaa tag are consistent with the current query.
768 NonLocalPointerInfo InitialNLPI;
769 InitialNLPI.Size = Loc.Size;
770 InitialNLPI.TBAATag = Loc.TBAATag;
771
772 // Get the NLPI for CacheKey, inserting one into the map if it doesn't
773 // already have one.
774 std::pair<CachedNonLocalPointerInfo::iterator, bool> Pair =
775 NonLocalPointerDeps.insert(std::make_pair(CacheKey, InitialNLPI));
776 NonLocalPointerInfo *CacheInfo = &Pair.first->second;
777
Dan Gohman733c54d2010-11-10 21:45:11 +0000778 // If we already have a cache entry for this CacheKey, we may need to do some
779 // work to reconcile the cache entry and the current query.
Dan Gohman075fb5d2010-11-10 20:37:15 +0000780 if (!Pair.second) {
Dan Gohman733c54d2010-11-10 21:45:11 +0000781 if (CacheInfo->Size < Loc.Size) {
782 // The query's Size is greater than the cached one. Throw out the
783 // cached data and procede with the query at the greater size.
784 CacheInfo->Pair = BBSkipFirstBlockPair();
785 CacheInfo->Size = Loc.Size;
Dan Gohman2365f082010-11-10 22:35:02 +0000786 for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(),
787 DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI)
788 if (Instruction *Inst = DI->getResult().getInst())
789 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
Dan Gohman733c54d2010-11-10 21:45:11 +0000790 CacheInfo->NonLocalDeps.clear();
791 } else if (CacheInfo->Size > Loc.Size) {
792 // This query's Size is less than the cached one. Conservatively restart
793 // the query using the greater size.
Dan Gohman075fb5d2010-11-10 20:37:15 +0000794 return getNonLocalPointerDepFromBB(Pointer,
795 Loc.getWithNewSize(CacheInfo->Size),
796 isLoad, StartBB, Result, Visited,
797 SkipFirstBlock);
798 }
799
Dan Gohman733c54d2010-11-10 21:45:11 +0000800 // If the query's TBAATag is inconsistent with the cached one,
801 // conservatively throw out the cached data and restart the query with
802 // no tag if needed.
Dan Gohman075fb5d2010-11-10 20:37:15 +0000803 if (CacheInfo->TBAATag != Loc.TBAATag) {
Dan Gohman733c54d2010-11-10 21:45:11 +0000804 if (CacheInfo->TBAATag) {
805 CacheInfo->Pair = BBSkipFirstBlockPair();
806 CacheInfo->TBAATag = 0;
Dan Gohman2365f082010-11-10 22:35:02 +0000807 for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(),
808 DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI)
809 if (Instruction *Inst = DI->getResult().getInst())
810 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
Dan Gohman733c54d2010-11-10 21:45:11 +0000811 CacheInfo->NonLocalDeps.clear();
812 }
813 if (Loc.TBAATag)
814 return getNonLocalPointerDepFromBB(Pointer, Loc.getWithoutTBAATag(),
815 isLoad, StartBB, Result, Visited,
816 SkipFirstBlock);
Dan Gohman075fb5d2010-11-10 20:37:15 +0000817 }
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000818 }
819
820 NonLocalDepInfo *Cache = &CacheInfo->NonLocalDeps;
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000821
822 // If we have valid cached information for exactly the block we are
823 // investigating, just return it with no recomputation.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000824 if (CacheInfo->Pair == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) {
Chris Lattnerf4789512008-12-16 07:10:09 +0000825 // We have a fully cached result for this query then we can just return the
826 // cached results and populate the visited set. However, we have to verify
827 // that we don't already have conflicting results for these blocks. Check
828 // to ensure that if a block in the results set is in the visited set that
829 // it was for the same pointer query.
830 if (!Visited.empty()) {
831 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
832 I != E; ++I) {
Chris Lattnere18b9712009-12-09 07:08:01 +0000833 DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->getBB());
Chris Lattner05e15f82009-12-09 01:59:31 +0000834 if (VI == Visited.end() || VI->second == Pointer.getAddr())
835 continue;
Chris Lattnerf4789512008-12-16 07:10:09 +0000836
837 // We have a pointer mismatch in a block. Just return clobber, saying
838 // that something was clobbered in this result. We could also do a
839 // non-fully cached query, but there is little point in doing this.
840 return true;
841 }
842 }
843
Chris Lattner0ee443d2009-12-22 04:25:02 +0000844 Value *Addr = Pointer.getAddr();
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000845 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
Chris Lattnerf4789512008-12-16 07:10:09 +0000846 I != E; ++I) {
Chris Lattner0ee443d2009-12-22 04:25:02 +0000847 Visited.insert(std::make_pair(I->getBB(), Addr));
Chris Lattnere18b9712009-12-09 07:08:01 +0000848 if (!I->getResult().isNonLocal())
Chris Lattner0ee443d2009-12-22 04:25:02 +0000849 Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(), Addr));
Chris Lattnerf4789512008-12-16 07:10:09 +0000850 }
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000851 ++NumCacheCompleteNonLocalPtr;
Chris Lattner9e59c642008-12-15 03:35:32 +0000852 return false;
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000853 }
854
855 // Otherwise, either this is a new block, a block with an invalid cache
856 // pointer or one that we're about to invalidate by putting more info into it
857 // than its valid cache info. If empty, the result will be valid cache info,
858 // otherwise it isn't.
Chris Lattner9e59c642008-12-15 03:35:32 +0000859 if (Cache->empty())
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000860 CacheInfo->Pair = BBSkipFirstBlockPair(StartBB, SkipFirstBlock);
861 else {
862 CacheInfo->Pair = BBSkipFirstBlockPair();
Dan Gohmanec9b4ac2010-11-11 00:20:27 +0000863 CacheInfo->Size = AliasAnalysis::UnknownSize;
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000864 CacheInfo->TBAATag = 0;
865 }
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000866
867 SmallVector<BasicBlock*, 32> Worklist;
868 Worklist.push_back(StartBB);
Chris Lattner6290f5c2008-12-07 08:50:20 +0000869
870 // Keep track of the entries that we know are sorted. Previously cached
871 // entries will all be sorted. The entries we add we only sort on demand (we
872 // don't insert every element into its sorted position). We know that we
873 // won't get any reuse from currently inserted values, because we don't
874 // revisit blocks after we insert info for them.
875 unsigned NumSortedEntries = Cache->size();
Chris Lattner12a7db32009-01-22 07:04:01 +0000876 DEBUG(AssertSorted(*Cache));
Chris Lattner6290f5c2008-12-07 08:50:20 +0000877
Chris Lattner7ebcf032008-12-07 02:15:47 +0000878 while (!Worklist.empty()) {
Chris Lattner9a193fd2008-12-07 02:56:57 +0000879 BasicBlock *BB = Worklist.pop_back_val();
Chris Lattner7ebcf032008-12-07 02:15:47 +0000880
Chris Lattner65633712008-12-09 07:52:59 +0000881 // Skip the first block if we have it.
Chris Lattner9e59c642008-12-15 03:35:32 +0000882 if (!SkipFirstBlock) {
Chris Lattner65633712008-12-09 07:52:59 +0000883 // Analyze the dependency of *Pointer in FromBB. See if we already have
884 // been here.
Chris Lattner9e59c642008-12-15 03:35:32 +0000885 assert(Visited.count(BB) && "Should check 'visited' before adding to WL");
Chris Lattner6290f5c2008-12-07 08:50:20 +0000886
Chris Lattner65633712008-12-09 07:52:59 +0000887 // Get the dependency info for Pointer in BB. If we have cached
888 // information, we will use it, otherwise we compute it.
Chris Lattner12a7db32009-01-22 07:04:01 +0000889 DEBUG(AssertSorted(*Cache, NumSortedEntries));
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000890 MemDepResult Dep = GetNonLocalInfoForBlock(Loc, isLoad, BB, Cache,
Chris Lattner05e15f82009-12-09 01:59:31 +0000891 NumSortedEntries);
Chris Lattner65633712008-12-09 07:52:59 +0000892
893 // If we got a Def or Clobber, add this to the list of results.
894 if (!Dep.isNonLocal()) {
Chris Lattner0ee443d2009-12-22 04:25:02 +0000895 Result.push_back(NonLocalDepResult(BB, Dep, Pointer.getAddr()));
Chris Lattner65633712008-12-09 07:52:59 +0000896 continue;
897 }
Chris Lattner7ebcf032008-12-07 02:15:47 +0000898 }
899
Chris Lattner9e59c642008-12-15 03:35:32 +0000900 // If 'Pointer' is an instruction defined in this block, then we need to do
901 // phi translation to change it into a value live in the predecessor block.
Chris Lattner05e15f82009-12-09 01:59:31 +0000902 // If not, we just add the predecessors to the worklist and scan them with
903 // the same Pointer.
904 if (!Pointer.NeedsPHITranslationFromBlock(BB)) {
Chris Lattner9e59c642008-12-15 03:35:32 +0000905 SkipFirstBlock = false;
906 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
907 // Verify that we haven't looked at this block yet.
908 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
Chris Lattner05e15f82009-12-09 01:59:31 +0000909 InsertRes = Visited.insert(std::make_pair(*PI, Pointer.getAddr()));
Chris Lattner9e59c642008-12-15 03:35:32 +0000910 if (InsertRes.second) {
911 // First time we've looked at *PI.
912 Worklist.push_back(*PI);
913 continue;
914 }
915
916 // If we have seen this block before, but it was with a different
917 // pointer then we have a phi translation failure and we have to treat
918 // this as a clobber.
Chris Lattner05e15f82009-12-09 01:59:31 +0000919 if (InsertRes.first->second != Pointer.getAddr())
Chris Lattner9e59c642008-12-15 03:35:32 +0000920 goto PredTranslationFailure;
921 }
922 continue;
923 }
924
Chris Lattner05e15f82009-12-09 01:59:31 +0000925 // We do need to do phi translation, if we know ahead of time we can't phi
926 // translate this value, don't even try.
927 if (!Pointer.IsPotentiallyPHITranslatable())
928 goto PredTranslationFailure;
929
Chris Lattner6fbc1962009-07-13 17:14:23 +0000930 // We may have added values to the cache list before this PHI translation.
931 // If so, we haven't done anything to ensure that the cache remains sorted.
932 // Sort it now (if needed) so that recursive invocations of
933 // getNonLocalPointerDepFromBB and other routines that could reuse the cache
934 // value will only see properly sorted cache arrays.
935 if (Cache && NumSortedEntries != Cache->size()) {
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000936 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner6fbc1962009-07-13 17:14:23 +0000937 NumSortedEntries = Cache->size();
938 }
Chris Lattnere95035a2009-11-27 08:37:22 +0000939 Cache = 0;
Chris Lattner05e15f82009-12-09 01:59:31 +0000940
Chris Lattnere95035a2009-11-27 08:37:22 +0000941 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
942 BasicBlock *Pred = *PI;
Chris Lattner05e15f82009-12-09 01:59:31 +0000943
944 // Get the PHI translated pointer in this predecessor. This can fail if
945 // not translatable, in which case the getAddr() returns null.
946 PHITransAddr PredPointer(Pointer);
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000947 PredPointer.PHITranslateValue(BB, Pred, 0);
Chris Lattner05e15f82009-12-09 01:59:31 +0000948
949 Value *PredPtrVal = PredPointer.getAddr();
Chris Lattnere95035a2009-11-27 08:37:22 +0000950
951 // Check to see if we have already visited this pred block with another
952 // pointer. If so, we can't do this lookup. This failure can occur
953 // with PHI translation when a critical edge exists and the PHI node in
954 // the successor translates to a pointer value different than the
955 // pointer the block was first analyzed with.
956 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
Chris Lattner05e15f82009-12-09 01:59:31 +0000957 InsertRes = Visited.insert(std::make_pair(Pred, PredPtrVal));
Chris Lattner9e59c642008-12-15 03:35:32 +0000958
Chris Lattnere95035a2009-11-27 08:37:22 +0000959 if (!InsertRes.second) {
960 // If the predecessor was visited with PredPtr, then we already did
961 // the analysis and can ignore it.
Chris Lattner05e15f82009-12-09 01:59:31 +0000962 if (InsertRes.first->second == PredPtrVal)
Chris Lattnere95035a2009-11-27 08:37:22 +0000963 continue;
Chris Lattner9e59c642008-12-15 03:35:32 +0000964
Chris Lattnere95035a2009-11-27 08:37:22 +0000965 // Otherwise, the block was previously analyzed with a different
966 // pointer. We can't represent the result of this case, so we just
967 // treat this as a phi translation failure.
968 goto PredTranslationFailure;
Chris Lattner9e59c642008-12-15 03:35:32 +0000969 }
Chris Lattner6f7b2102009-11-27 22:05:15 +0000970
971 // If PHI translation was unable to find an available pointer in this
972 // predecessor, then we have to assume that the pointer is clobbered in
973 // that predecessor. We can still do PRE of the load, which would insert
974 // a computation of the pointer in this predecessor.
Chris Lattner05e15f82009-12-09 01:59:31 +0000975 if (PredPtrVal == 0) {
Chris Lattner855d9da2009-12-01 07:33:32 +0000976 // Add the entry to the Result list.
Chris Lattner0ee443d2009-12-22 04:25:02 +0000977 NonLocalDepResult Entry(Pred,
978 MemDepResult::getClobber(Pred->getTerminator()),
979 PredPtrVal);
Chris Lattner855d9da2009-12-01 07:33:32 +0000980 Result.push_back(Entry);
981
Chris Lattnerf6481252009-12-19 21:29:22 +0000982 // Since we had a phi translation failure, the cache for CacheKey won't
983 // include all of the entries that we need to immediately satisfy future
984 // queries. Mark this in NonLocalPointerDeps by setting the
985 // BBSkipFirstBlockPair pointer to null. This requires reuse of the
986 // cached value to do more work but not miss the phi trans failure.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000987 NonLocalPointerInfo &NLPI = NonLocalPointerDeps[CacheKey];
988 NLPI.Pair = BBSkipFirstBlockPair();
Dan Gohmanec9b4ac2010-11-11 00:20:27 +0000989 NLPI.Size = AliasAnalysis::UnknownSize;
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000990 NLPI.TBAATag = 0;
Chris Lattner6f7b2102009-11-27 22:05:15 +0000991 continue;
Chris Lattner6f7b2102009-11-27 22:05:15 +0000992 }
Chris Lattnere95035a2009-11-27 08:37:22 +0000993
994 // FIXME: it is entirely possible that PHI translating will end up with
995 // the same value. Consider PHI translating something like:
996 // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need*
997 // to recurse here, pedantically speaking.
Chris Lattner6fbc1962009-07-13 17:14:23 +0000998
Chris Lattnere95035a2009-11-27 08:37:22 +0000999 // If we have a problem phi translating, fall through to the code below
1000 // to handle the failure condition.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001001 if (getNonLocalPointerDepFromBB(PredPointer,
1002 Loc.getWithNewPtr(PredPointer.getAddr()),
1003 isLoad, Pred,
Chris Lattnere95035a2009-11-27 08:37:22 +00001004 Result, Visited))
1005 goto PredTranslationFailure;
Chris Lattner9e59c642008-12-15 03:35:32 +00001006 }
Chris Lattnere95035a2009-11-27 08:37:22 +00001007
1008 // Refresh the CacheInfo/Cache pointer so that it isn't invalidated.
1009 CacheInfo = &NonLocalPointerDeps[CacheKey];
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001010 Cache = &CacheInfo->NonLocalDeps;
Chris Lattnere95035a2009-11-27 08:37:22 +00001011 NumSortedEntries = Cache->size();
1012
1013 // Since we did phi translation, the "Cache" set won't contain all of the
1014 // results for the query. This is ok (we can still use it to accelerate
1015 // specific block queries) but we can't do the fastpath "return all
1016 // results from the set" Clear out the indicator for this.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001017 CacheInfo->Pair = BBSkipFirstBlockPair();
Dan Gohmanec9b4ac2010-11-11 00:20:27 +00001018 CacheInfo->Size = AliasAnalysis::UnknownSize;
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001019 CacheInfo->TBAATag = 0;
Chris Lattnere95035a2009-11-27 08:37:22 +00001020 SkipFirstBlock = false;
1021 continue;
Chris Lattnerdc593112009-11-26 23:18:49 +00001022
Chris Lattner9e59c642008-12-15 03:35:32 +00001023 PredTranslationFailure:
1024
Chris Lattner95900f22009-01-23 07:12:16 +00001025 if (Cache == 0) {
1026 // Refresh the CacheInfo/Cache pointer if it got invalidated.
1027 CacheInfo = &NonLocalPointerDeps[CacheKey];
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001028 Cache = &CacheInfo->NonLocalDeps;
Chris Lattner95900f22009-01-23 07:12:16 +00001029 NumSortedEntries = Cache->size();
Chris Lattner95900f22009-01-23 07:12:16 +00001030 }
Chris Lattner6fbc1962009-07-13 17:14:23 +00001031
Chris Lattnerf6481252009-12-19 21:29:22 +00001032 // Since we failed phi translation, the "Cache" set won't contain all of the
Chris Lattner9e59c642008-12-15 03:35:32 +00001033 // results for the query. This is ok (we can still use it to accelerate
1034 // specific block queries) but we can't do the fastpath "return all
Chris Lattnerf6481252009-12-19 21:29:22 +00001035 // results from the set". Clear out the indicator for this.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001036 CacheInfo->Pair = BBSkipFirstBlockPair();
Dan Gohmanec9b4ac2010-11-11 00:20:27 +00001037 CacheInfo->Size = AliasAnalysis::UnknownSize;
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001038 CacheInfo->TBAATag = 0;
Chris Lattner9e59c642008-12-15 03:35:32 +00001039
1040 // If *nothing* works, mark the pointer as being clobbered by the first
1041 // instruction in this block.
1042 //
1043 // If this is the magic first block, return this as a clobber of the whole
1044 // incoming value. Since we can't phi translate to one of the predecessors,
1045 // we have to bail out.
1046 if (SkipFirstBlock)
1047 return true;
1048
1049 for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) {
1050 assert(I != Cache->rend() && "Didn't find current block??");
Chris Lattnere18b9712009-12-09 07:08:01 +00001051 if (I->getBB() != BB)
Chris Lattner9e59c642008-12-15 03:35:32 +00001052 continue;
1053
Chris Lattnere18b9712009-12-09 07:08:01 +00001054 assert(I->getResult().isNonLocal() &&
Chris Lattner9e59c642008-12-15 03:35:32 +00001055 "Should only be here with transparent block");
Chris Lattner0ee443d2009-12-22 04:25:02 +00001056 I->setResult(MemDepResult::getClobber(BB->begin()));
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001057 ReverseNonLocalPtrDeps[BB->begin()].insert(CacheKey);
Chris Lattner0ee443d2009-12-22 04:25:02 +00001058 Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(),
1059 Pointer.getAddr()));
Chris Lattner9e59c642008-12-15 03:35:32 +00001060 break;
Chris Lattner9a193fd2008-12-07 02:56:57 +00001061 }
Chris Lattner7ebcf032008-12-07 02:15:47 +00001062 }
Chris Lattner95900f22009-01-23 07:12:16 +00001063
Chris Lattner9863c3f2008-12-09 07:47:11 +00001064 // Okay, we're done now. If we added new values to the cache, re-sort it.
Chris Lattnera2f55dd2009-07-13 17:20:05 +00001065 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner12a7db32009-01-22 07:04:01 +00001066 DEBUG(AssertSorted(*Cache));
Chris Lattner9e59c642008-12-15 03:35:32 +00001067 return false;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001068}
1069
1070/// RemoveCachedNonLocalPointerDependencies - If P exists in
1071/// CachedNonLocalPointerInfo, remove it.
1072void MemoryDependenceAnalysis::
1073RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) {
1074 CachedNonLocalPointerInfo::iterator It =
1075 NonLocalPointerDeps.find(P);
1076 if (It == NonLocalPointerDeps.end()) return;
1077
1078 // Remove all of the entries in the BB->val map. This involves removing
1079 // instructions from the reverse map.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001080 NonLocalDepInfo &PInfo = It->second.NonLocalDeps;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001081
1082 for (unsigned i = 0, e = PInfo.size(); i != e; ++i) {
Chris Lattnere18b9712009-12-09 07:08:01 +00001083 Instruction *Target = PInfo[i].getResult().getInst();
Chris Lattner6290f5c2008-12-07 08:50:20 +00001084 if (Target == 0) continue; // Ignore non-local dep results.
Chris Lattnere18b9712009-12-09 07:08:01 +00001085 assert(Target->getParent() == PInfo[i].getBB());
Chris Lattner6290f5c2008-12-07 08:50:20 +00001086
1087 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001088 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001089 }
1090
1091 // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).
1092 NonLocalPointerDeps.erase(It);
Chris Lattner7ebcf032008-12-07 02:15:47 +00001093}
1094
1095
Chris Lattnerbc99be12008-12-09 22:06:23 +00001096/// invalidateCachedPointerInfo - This method is used to invalidate cached
1097/// information about the specified pointer, because it may be too
1098/// conservative in memdep. This is an optional call that can be used when
1099/// the client detects an equivalence between the pointer and some other
1100/// value and replaces the other value with ptr. This can make Ptr available
1101/// in more places that cached info does not necessarily keep.
1102void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) {
1103 // If Ptr isn't really a pointer, just ignore it.
Duncan Sands1df98592010-02-16 11:11:14 +00001104 if (!Ptr->getType()->isPointerTy()) return;
Chris Lattnerbc99be12008-12-09 22:06:23 +00001105 // Flush store info for the pointer.
1106 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false));
1107 // Flush load info for the pointer.
1108 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true));
1109}
1110
Bob Wilson484d4a32010-02-16 19:51:59 +00001111/// invalidateCachedPredecessors - Clear the PredIteratorCache info.
1112/// This needs to be done when the CFG changes, e.g., due to splitting
1113/// critical edges.
1114void MemoryDependenceAnalysis::invalidateCachedPredecessors() {
1115 PredCache->clear();
1116}
1117
Owen Anderson78e02f72007-07-06 23:14:35 +00001118/// removeInstruction - Remove an instruction from the dependence analysis,
1119/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +00001120/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +00001121void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +00001122 // Walk through the Non-local dependencies, removing this one as the value
1123 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +00001124 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
1125 if (NLDI != NonLocalDeps.end()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +00001126 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattner25f4b2b2008-11-30 02:30:50 +00001127 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
1128 DI != DE; ++DI)
Chris Lattnere18b9712009-12-09 07:08:01 +00001129 if (Instruction *Inst = DI->getResult().getInst())
Chris Lattnerd44745d2008-12-07 18:39:13 +00001130 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);
Chris Lattnerf68f3102008-11-30 02:28:25 +00001131 NonLocalDeps.erase(NLDI);
1132 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +00001133
Chris Lattner5f589dc2008-11-28 22:04:47 +00001134 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +00001135 //
Chris Lattner39f372e2008-11-29 01:43:36 +00001136 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
1137 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +00001138 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerd44745d2008-12-07 18:39:13 +00001139 if (Instruction *Inst = LocalDepEntry->second.getInst())
1140 RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);
Chris Lattner125ce362008-11-30 01:09:30 +00001141
Chris Lattnerbaad8882008-11-28 22:28:27 +00001142 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +00001143 LocalDeps.erase(LocalDepEntry);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001144 }
1145
1146 // If we have any cached pointer dependencies on this instruction, remove
1147 // them. If the instruction has non-pointer type, then it can't be a pointer
1148 // base.
1149
1150 // Remove it from both the load info and the store info. The instruction
1151 // can't be in either of these maps if it is non-pointer.
Duncan Sands1df98592010-02-16 11:11:14 +00001152 if (RemInst->getType()->isPointerTy()) {
Chris Lattner6290f5c2008-12-07 08:50:20 +00001153 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));
1154 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));
1155 }
Chris Lattnerbaad8882008-11-28 22:28:27 +00001156
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001157 // Loop over all of the things that depend on the instruction we're removing.
1158 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001159 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
Chris Lattner0655f732008-12-07 18:42:51 +00001160
1161 // If we find RemInst as a clobber or Def in any of the maps for other values,
1162 // we need to replace its entry with a dirty version of the instruction after
1163 // it. If RemInst is a terminator, we use a null dirty value.
1164 //
1165 // Using a dirty version of the instruction after RemInst saves having to scan
1166 // the entire block to get to this point.
1167 MemDepResult NewDirtyVal;
1168 if (!RemInst->isTerminator())
1169 NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst));
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001170
Chris Lattner8c465272008-11-29 09:20:15 +00001171 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
1172 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001173 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001174 // RemInst can't be the terminator if it has local stuff depending on it.
Chris Lattner125ce362008-11-30 01:09:30 +00001175 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
1176 "Nothing can locally depend on a terminator");
1177
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001178 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
1179 E = ReverseDeps.end(); I != E; ++I) {
1180 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001181 assert(InstDependingOnRemInst != RemInst &&
1182 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +00001183
Chris Lattner0655f732008-12-07 18:42:51 +00001184 LocalDeps[InstDependingOnRemInst] = NewDirtyVal;
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001185
Chris Lattner125ce362008-11-30 01:09:30 +00001186 // Make sure to remember that new things depend on NewDepInst.
Chris Lattner0655f732008-12-07 18:42:51 +00001187 assert(NewDirtyVal.getInst() && "There is no way something else can have "
1188 "a local dep on this if it is a terminator!");
1189 ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(),
Chris Lattner125ce362008-11-30 01:09:30 +00001190 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001191 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001192
1193 ReverseLocalDeps.erase(ReverseDepIt);
1194
1195 // Add new reverse deps after scanning the set, to avoid invalidating the
1196 // 'ReverseDeps' reference.
1197 while (!ReverseDepsToAdd.empty()) {
1198 ReverseLocalDeps[ReverseDepsToAdd.back().first]
1199 .insert(ReverseDepsToAdd.back().second);
1200 ReverseDepsToAdd.pop_back();
1201 }
Owen Anderson78e02f72007-07-06 23:14:35 +00001202 }
Owen Anderson4d13de42007-08-16 21:27:05 +00001203
Chris Lattner8c465272008-11-29 09:20:15 +00001204 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
1205 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner6290f5c2008-12-07 08:50:20 +00001206 SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second;
1207 for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +00001208 I != E; ++I) {
1209 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
1210
Chris Lattner4a69bad2008-11-30 02:52:26 +00001211 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner4a69bad2008-11-30 02:52:26 +00001212 // The information is now dirty!
Chris Lattnerbf145d62008-12-01 01:15:42 +00001213 INLD.second = true;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001214
Chris Lattnerbf145d62008-12-01 01:15:42 +00001215 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
1216 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnere18b9712009-12-09 07:08:01 +00001217 if (DI->getResult().getInst() != RemInst) continue;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001218
1219 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner0ee443d2009-12-22 04:25:02 +00001220 DI->setResult(NewDirtyVal);
Chris Lattner0655f732008-12-07 18:42:51 +00001221
1222 if (Instruction *NextI = NewDirtyVal.getInst())
Chris Lattnerf68f3102008-11-30 02:28:25 +00001223 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattnerf68f3102008-11-30 02:28:25 +00001224 }
1225 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001226
1227 ReverseNonLocalDeps.erase(ReverseDepIt);
1228
Chris Lattner0ec48dd2008-11-29 22:02:15 +00001229 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
1230 while (!ReverseDepsToAdd.empty()) {
1231 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
1232 .insert(ReverseDepsToAdd.back().second);
1233 ReverseDepsToAdd.pop_back();
1234 }
Owen Anderson4d13de42007-08-16 21:27:05 +00001235 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +00001236
Chris Lattner6290f5c2008-12-07 08:50:20 +00001237 // If the instruction is in ReverseNonLocalPtrDeps then it appears as a
1238 // value in the NonLocalPointerDeps info.
1239 ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =
1240 ReverseNonLocalPtrDeps.find(RemInst);
1241 if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001242 SmallPtrSet<ValueIsLoadPair, 4> &Set = ReversePtrDepIt->second;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001243 SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd;
1244
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001245 for (SmallPtrSet<ValueIsLoadPair, 4>::iterator I = Set.begin(),
1246 E = Set.end(); I != E; ++I) {
1247 ValueIsLoadPair P = *I;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001248 assert(P.getPointer() != RemInst &&
1249 "Already removed NonLocalPointerDeps info for RemInst");
1250
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001251 NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].NonLocalDeps;
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001252
1253 // The cache is not valid for any specific block anymore.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001254 NonLocalPointerDeps[P].Pair = BBSkipFirstBlockPair();
Dan Gohmanec9b4ac2010-11-11 00:20:27 +00001255 NonLocalPointerDeps[P].Size = AliasAnalysis::UnknownSize;
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001256 NonLocalPointerDeps[P].TBAATag = 0;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001257
Chris Lattner6290f5c2008-12-07 08:50:20 +00001258 // Update any entries for RemInst to use the instruction after it.
1259 for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end();
1260 DI != DE; ++DI) {
Chris Lattnere18b9712009-12-09 07:08:01 +00001261 if (DI->getResult().getInst() != RemInst) continue;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001262
1263 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner0ee443d2009-12-22 04:25:02 +00001264 DI->setResult(NewDirtyVal);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001265
1266 if (Instruction *NewDirtyInst = NewDirtyVal.getInst())
1267 ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));
1268 }
Chris Lattner95900f22009-01-23 07:12:16 +00001269
1270 // Re-sort the NonLocalDepInfo. Changing the dirty entry to its
1271 // subsequent value may invalidate the sortedness.
1272 std::sort(NLPDI.begin(), NLPDI.end());
Chris Lattner6290f5c2008-12-07 08:50:20 +00001273 }
1274
1275 ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);
1276
1277 while (!ReversePtrDepsToAdd.empty()) {
1278 ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first]
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001279 .insert(ReversePtrDepsToAdd.back().second);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001280 ReversePtrDepsToAdd.pop_back();
1281 }
1282 }
1283
1284
Chris Lattnerf68f3102008-11-30 02:28:25 +00001285 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnerd777d402008-11-30 19:24:31 +00001286 AA->deleteValue(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +00001287 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +00001288}
Chris Lattner729b2372008-11-29 21:25:10 +00001289/// verifyRemoved - Verify that the specified instruction does not occur
1290/// in our internal data structures.
1291void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
1292 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
1293 E = LocalDeps.end(); I != E; ++I) {
1294 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +00001295 assert(I->second.getInst() != D &&
Chris Lattner729b2372008-11-29 21:25:10 +00001296 "Inst occurs in data structures");
1297 }
1298
Chris Lattner6290f5c2008-12-07 08:50:20 +00001299 for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(),
1300 E = NonLocalPointerDeps.end(); I != E; ++I) {
1301 assert(I->first.getPointer() != D && "Inst occurs in NLPD map key");
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001302 const NonLocalDepInfo &Val = I->second.NonLocalDeps;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001303 for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end();
1304 II != E; ++II)
Chris Lattnere18b9712009-12-09 07:08:01 +00001305 assert(II->getResult().getInst() != D && "Inst occurs as NLPD value");
Chris Lattner6290f5c2008-12-07 08:50:20 +00001306 }
1307
Chris Lattner729b2372008-11-29 21:25:10 +00001308 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
1309 E = NonLocalDeps.end(); I != E; ++I) {
1310 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4a69bad2008-11-30 02:52:26 +00001311 const PerInstNLInfo &INLD = I->second;
Chris Lattnerbf145d62008-12-01 01:15:42 +00001312 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
1313 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnere18b9712009-12-09 07:08:01 +00001314 assert(II->getResult().getInst() != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001315 }
1316
1317 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +00001318 E = ReverseLocalDeps.end(); I != E; ++I) {
1319 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001320 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1321 EE = I->second.end(); II != EE; ++II)
1322 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +00001323 }
Chris Lattner729b2372008-11-29 21:25:10 +00001324
1325 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
1326 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +00001327 I != E; ++I) {
1328 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001329 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1330 EE = I->second.end(); II != EE; ++II)
1331 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +00001332 }
Chris Lattner6290f5c2008-12-07 08:50:20 +00001333
1334 for (ReverseNonLocalPtrDepTy::const_iterator
1335 I = ReverseNonLocalPtrDeps.begin(),
1336 E = ReverseNonLocalPtrDeps.end(); I != E; ++I) {
1337 assert(I->first != D && "Inst occurs in rev NLPD map");
1338
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001339 for (SmallPtrSet<ValueIsLoadPair, 4>::const_iterator II = I->second.begin(),
Chris Lattner6290f5c2008-12-07 08:50:20 +00001340 E = I->second.end(); II != E; ++II)
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001341 assert(*II != ValueIsLoadPair(D, false) &&
1342 *II != ValueIsLoadPair(D, true) &&
Chris Lattner6290f5c2008-12-07 08:50:20 +00001343 "Inst occurs in ReverseNonLocalPtrDeps map");
1344 }
1345
Chris Lattner729b2372008-11-29 21:25:10 +00001346}