blob: f4531e09579b70f6c2e2d5aa7e01514491ffcde4 [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"
22#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner6f7b2102009-11-27 22:05:15 +000023#include "llvm/Analysis/Dominators.h"
Chris Lattnere19e4ba2009-11-27 00:34:38 +000024#include "llvm/Analysis/InstructionSimplify.h"
Victor Hernandezf006b182009-10-27 20:05:49 +000025#include "llvm/Analysis/MemoryBuiltins.h"
Chris Lattnerbaad8882008-11-28 22:28:27 +000026#include "llvm/ADT/Statistic.h"
Duncan Sands7050f3d2008-12-10 09:38:36 +000027#include "llvm/ADT/STLExtras.h"
Chris Lattner4012fdd2008-12-09 06:28:49 +000028#include "llvm/Support/PredIteratorCache.h"
Chris Lattner0e575f42008-11-28 21:45:17 +000029#include "llvm/Support/Debug.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000030using namespace llvm;
31
Chris Lattnerbf145d62008-12-01 01:15:42 +000032STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
33STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
Chris Lattner0ec48dd2008-11-29 22:02:15 +000034STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Chris Lattner6290f5c2008-12-07 08:50:20 +000035
36STATISTIC(NumCacheNonLocalPtr,
37 "Number of fully cached non-local ptr responses");
38STATISTIC(NumCacheDirtyNonLocalPtr,
39 "Number of cached, but dirty, non-local ptr responses");
40STATISTIC(NumUncacheNonLocalPtr,
41 "Number of uncached non-local ptr responses");
Chris Lattner11dcd8d2008-12-08 07:31:50 +000042STATISTIC(NumCacheCompleteNonLocalPtr,
43 "Number of block queries that were completely cached");
Chris Lattner6290f5c2008-12-07 08:50:20 +000044
Owen Anderson78e02f72007-07-06 23:14:35 +000045char MemoryDependenceAnalysis::ID = 0;
46
Owen Anderson78e02f72007-07-06 23:14:35 +000047// Register this pass...
Owen Anderson776ee1f2007-07-10 20:21:08 +000048static RegisterPass<MemoryDependenceAnalysis> X("memdep",
Chris Lattner0e575f42008-11-28 21:45:17 +000049 "Memory Dependence Analysis", false, true);
Owen Anderson78e02f72007-07-06 23:14:35 +000050
Chris Lattner4012fdd2008-12-09 06:28:49 +000051MemoryDependenceAnalysis::MemoryDependenceAnalysis()
52: FunctionPass(&ID), PredCache(0) {
53}
54MemoryDependenceAnalysis::~MemoryDependenceAnalysis() {
55}
56
57/// Clean up memory in between runs
58void MemoryDependenceAnalysis::releaseMemory() {
59 LocalDeps.clear();
60 NonLocalDeps.clear();
61 NonLocalPointerDeps.clear();
62 ReverseLocalDeps.clear();
63 ReverseNonLocalDeps.clear();
64 ReverseNonLocalPtrDeps.clear();
65 PredCache->clear();
66}
67
68
69
Owen Anderson78e02f72007-07-06 23:14:35 +000070/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
71///
72void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.setPreservesAll();
74 AU.addRequiredTransitive<AliasAnalysis>();
Owen Anderson78e02f72007-07-06 23:14:35 +000075}
76
Chris Lattnerd777d402008-11-30 19:24:31 +000077bool MemoryDependenceAnalysis::runOnFunction(Function &) {
78 AA = &getAnalysis<AliasAnalysis>();
Chris Lattner4012fdd2008-12-09 06:28:49 +000079 if (PredCache == 0)
80 PredCache.reset(new PredIteratorCache());
Chris Lattnerd777d402008-11-30 19:24:31 +000081 return false;
82}
83
Chris Lattnerd44745d2008-12-07 18:39:13 +000084/// RemoveFromReverseMap - This is a helper function that removes Val from
85/// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry.
86template <typename KeyTy>
87static void RemoveFromReverseMap(DenseMap<Instruction*,
Chris Lattner6a0dcc12009-03-29 00:24:04 +000088 SmallPtrSet<KeyTy, 4> > &ReverseMap,
89 Instruction *Inst, KeyTy Val) {
90 typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator
Chris Lattnerd44745d2008-12-07 18:39:13 +000091 InstIt = ReverseMap.find(Inst);
92 assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");
93 bool Found = InstIt->second.erase(Val);
94 assert(Found && "Invalid reverse map!"); Found=Found;
95 if (InstIt->second.empty())
96 ReverseMap.erase(InstIt);
97}
98
Chris Lattnerbf145d62008-12-01 01:15:42 +000099
Chris Lattner8ef57c52008-12-07 00:35:51 +0000100/// getCallSiteDependencyFrom - Private helper for finding the local
101/// dependencies of a call site.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000102MemDepResult MemoryDependenceAnalysis::
Chris Lattner20d6f092008-12-09 21:19:42 +0000103getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall,
104 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Owen Anderson642a9e32007-08-08 22:26:03 +0000105 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000106 while (ScanIt != BB->begin()) {
107 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +0000108
109 // If this inst is a memory op, get the pointer it accessed
Chris Lattner00314b32008-11-29 09:15:21 +0000110 Value *Pointer = 0;
111 uint64_t PointerSize = 0;
112 if (StoreInst *S = dyn_cast<StoreInst>(Inst)) {
113 Pointer = S->getPointerOperand();
Dan Gohmanf5812132009-07-31 20:53:12 +0000114 PointerSize = AA->getTypeStoreSize(S->getOperand(0)->getType());
Chris Lattner00314b32008-11-29 09:15:21 +0000115 } else if (VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
116 Pointer = V->getOperand(0);
Dan Gohmanf5812132009-07-31 20:53:12 +0000117 PointerSize = AA->getTypeStoreSize(V->getType());
Victor Hernandez046e78c2009-10-26 23:43:48 +0000118 } else if (isFreeCall(Inst)) {
119 Pointer = Inst->getOperand(1);
120 // calls to free() erase the entire structure
Chris Lattner6290f5c2008-12-07 08:50:20 +0000121 PointerSize = ~0ULL;
Chris Lattner00314b32008-11-29 09:15:21 +0000122 } else if (isa<CallInst>(Inst) || isa<InvokeInst>(Inst)) {
Owen Andersonf6cec852009-03-09 05:12:38 +0000123 // Debug intrinsics don't cause dependences.
Dale Johannesen497cb6f2009-03-11 21:13:01 +0000124 if (isa<DbgInfoIntrinsic>(Inst)) continue;
Chris Lattnerb51deb92008-12-05 21:04:20 +0000125 CallSite InstCS = CallSite::get(Inst);
126 // If these two calls do not interfere, look past it.
Chris Lattner20d6f092008-12-09 21:19:42 +0000127 switch (AA->getModRefInfo(CS, InstCS)) {
128 case AliasAnalysis::NoModRef:
129 // If the two calls don't interact (e.g. InstCS is readnone) keep
130 // scanning.
Chris Lattner00314b32008-11-29 09:15:21 +0000131 continue;
Chris Lattner20d6f092008-12-09 21:19:42 +0000132 case AliasAnalysis::Ref:
133 // If the two calls read the same memory locations and CS is a readonly
134 // function, then we have two cases: 1) the calls may not interfere with
135 // each other at all. 2) the calls may produce the same value. In case
136 // #1 we want to ignore the values, in case #2, we want to return Inst
137 // as a Def dependence. This allows us to CSE in cases like:
138 // X = strlen(P);
139 // memchr(...);
140 // Y = strlen(P); // Y = X
141 if (isReadOnlyCall) {
142 if (CS.getCalledFunction() != 0 &&
143 CS.getCalledFunction() == InstCS.getCalledFunction())
144 return MemDepResult::getDef(Inst);
145 // Ignore unrelated read/read call dependences.
146 continue;
147 }
148 // FALL THROUGH
149 default:
Chris Lattnerb51deb92008-12-05 21:04:20 +0000150 return MemDepResult::getClobber(Inst);
Chris Lattner20d6f092008-12-09 21:19:42 +0000151 }
Chris Lattnercfbb6342008-11-30 01:44:00 +0000152 } else {
153 // Non-memory instruction.
Owen Anderson202da142007-07-10 20:39:07 +0000154 continue;
Chris Lattnercfbb6342008-11-30 01:44:00 +0000155 }
Owen Anderson5f323202007-07-10 17:59:22 +0000156
Chris Lattnerb51deb92008-12-05 21:04:20 +0000157 if (AA->getModRefInfo(CS, Pointer, PointerSize) != AliasAnalysis::NoModRef)
158 return MemDepResult::getClobber(Inst);
Owen Anderson5f323202007-07-10 17:59:22 +0000159 }
160
Chris Lattner7ebcf032008-12-07 02:15:47 +0000161 // No dependence found. If this is the entry block of the function, it is a
162 // clobber, otherwise it is non-local.
163 if (BB != &BB->getParent()->getEntryBlock())
164 return MemDepResult::getNonLocal();
165 return MemDepResult::getClobber(ScanIt);
Owen Anderson5f323202007-07-10 17:59:22 +0000166}
167
Chris Lattnere79be942008-12-07 01:50:16 +0000168/// getPointerDependencyFrom - Return the instruction on which a memory
169/// location depends. If isLoad is true, this routine ignore may-aliases with
170/// read-only operations.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000171MemDepResult MemoryDependenceAnalysis::
Owen Anderson4bc737c2009-10-28 06:18:42 +0000172getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad,
Chris Lattnere79be942008-12-07 01:50:16 +0000173 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000174
Chris Lattner1e8de492009-12-01 21:16:01 +0000175 Value *InvariantTag = 0;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000176
Chris Lattner6290f5c2008-12-07 08:50:20 +0000177 // Walk backwards through the basic block, looking for dependencies.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000178 while (ScanIt != BB->begin()) {
179 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000180
Owen Anderson4bc737c2009-10-28 06:18:42 +0000181 // If we're in an invariant region, no dependencies can be found before
182 // we pass an invariant-begin marker.
Chris Lattner1e8de492009-12-01 21:16:01 +0000183 if (InvariantTag == Inst) {
184 InvariantTag = 0;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000185 continue;
Chris Lattner1ffb70f2009-12-01 21:15:15 +0000186 }
187
188 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
Owen Andersonb62f7922009-10-28 07:05:35 +0000189 // If we pass an invariant-end marker, then we've just entered an
190 // invariant region and can start ignoring dependencies.
Owen Anderson4bc737c2009-10-28 06:18:42 +0000191 if (II->getIntrinsicID() == Intrinsic::invariant_end) {
Chris Lattner1e8de492009-12-01 21:16:01 +0000192 uint64_t InvariantSize = ~0ULL;
Nick Lewyckyf27f1152009-11-22 02:38:11 +0000193 if (ConstantInt *CI = dyn_cast<ConstantInt>(II->getOperand(2)))
Chris Lattner1e8de492009-12-01 21:16:01 +0000194 InvariantSize = CI->getZExtValue();
Owen Anderson4bc737c2009-10-28 06:18:42 +0000195
196 AliasAnalysis::AliasResult R =
Chris Lattner1e8de492009-12-01 21:16:01 +0000197 AA->alias(II->getOperand(3), InvariantSize, MemPtr, MemSize);
Owen Anderson4bc737c2009-10-28 06:18:42 +0000198 if (R == AliasAnalysis::MustAlias) {
Chris Lattner1e8de492009-12-01 21:16:01 +0000199 InvariantTag = II->getOperand(1);
Owen Anderson4bc737c2009-10-28 06:18:42 +0000200 continue;
201 }
Owen Andersonb62f7922009-10-28 07:05:35 +0000202
203 // If we reach a lifetime begin or end marker, then the query ends here
204 // because the value is undefined.
205 } else if (II->getIntrinsicID() == Intrinsic::lifetime_start ||
Nick Lewycky2eac9492009-11-29 18:10:39 +0000206 II->getIntrinsicID() == Intrinsic::lifetime_end) {
Chris Lattner1e8de492009-12-01 21:16:01 +0000207 uint64_t InvariantSize = ~0ULL;
Nick Lewyckyf27f1152009-11-22 02:38:11 +0000208 if (ConstantInt *CI = dyn_cast<ConstantInt>(II->getOperand(1)))
Chris Lattner1e8de492009-12-01 21:16:01 +0000209 InvariantSize = CI->getZExtValue();
Owen Andersonb62f7922009-10-28 07:05:35 +0000210
211 AliasAnalysis::AliasResult R =
Chris Lattner1e8de492009-12-01 21:16:01 +0000212 AA->alias(II->getOperand(2), InvariantSize, MemPtr, MemSize);
Owen Andersonb62f7922009-10-28 07:05:35 +0000213 if (R == AliasAnalysis::MustAlias)
214 return MemDepResult::getDef(II);
Owen Anderson4bc737c2009-10-28 06:18:42 +0000215 }
216 }
217
218 // If we're querying on a load and we're in an invariant region, we're done
219 // at this point. Nothing a load depends on can live in an invariant region.
Chris Lattner1e8de492009-12-01 21:16:01 +0000220 if (isLoad && InvariantTag) continue;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000221
Owen Andersonf6cec852009-03-09 05:12:38 +0000222 // Debug intrinsics don't cause dependences.
223 if (isa<DbgInfoIntrinsic>(Inst)) continue;
224
Chris Lattnercfbb6342008-11-30 01:44:00 +0000225 // Values depend on loads if the pointers are must aliased. This means that
226 // a load depends on another must aliased load from the same value.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000227 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Chris Lattnerb51deb92008-12-05 21:04:20 +0000228 Value *Pointer = LI->getPointerOperand();
Dan Gohmanf5812132009-07-31 20:53:12 +0000229 uint64_t PointerSize = AA->getTypeStoreSize(LI->getType());
Chris Lattnerb51deb92008-12-05 21:04:20 +0000230
231 // If we found a pointer, check if it could be the same as our pointer.
Chris Lattnera161ab02008-11-29 09:09:48 +0000232 AliasAnalysis::AliasResult R =
Chris Lattnerd777d402008-11-30 19:24:31 +0000233 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
Chris Lattnera161ab02008-11-29 09:09:48 +0000234 if (R == AliasAnalysis::NoAlias)
235 continue;
236
237 // May-alias loads don't depend on each other without a dependence.
Chris Lattnere79be942008-12-07 01:50:16 +0000238 if (isLoad && R == AliasAnalysis::MayAlias)
Chris Lattnera161ab02008-11-29 09:09:48 +0000239 continue;
Chris Lattner6290f5c2008-12-07 08:50:20 +0000240 // Stores depend on may and must aliased loads, loads depend on must-alias
241 // loads.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000242 return MemDepResult::getDef(Inst);
243 }
244
245 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Owen Andersona85a6642009-10-28 06:30:52 +0000246 // There can't be stores to the value we care about inside an
247 // invariant region.
Chris Lattner1e8de492009-12-01 21:16:01 +0000248 if (InvariantTag) continue;
Owen Andersona85a6642009-10-28 06:30:52 +0000249
Chris Lattnerab9cf122009-05-25 21:28:56 +0000250 // If alias analysis can tell that this store is guaranteed to not modify
251 // the query pointer, ignore it. Use getModRefInfo to handle cases where
252 // the query pointer points to constant memory etc.
253 if (AA->getModRefInfo(SI, MemPtr, MemSize) == AliasAnalysis::NoModRef)
254 continue;
255
256 // Ok, this store might clobber the query pointer. Check to see if it is
257 // a must alias: in this case, we want to return this as a def.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000258 Value *Pointer = SI->getPointerOperand();
Dan Gohmanf5812132009-07-31 20:53:12 +0000259 uint64_t PointerSize = AA->getTypeStoreSize(SI->getOperand(0)->getType());
Chris Lattnerab9cf122009-05-25 21:28:56 +0000260
Chris Lattnerb51deb92008-12-05 21:04:20 +0000261 // If we found a pointer, check if it could be the same as our pointer.
262 AliasAnalysis::AliasResult R =
263 AA->alias(Pointer, PointerSize, MemPtr, MemSize);
264
265 if (R == AliasAnalysis::NoAlias)
266 continue;
267 if (R == AliasAnalysis::MayAlias)
268 return MemDepResult::getClobber(Inst);
269 return MemDepResult::getDef(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000270 }
Chris Lattner237a8282008-11-30 01:39:32 +0000271
272 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattnerb51deb92008-12-05 21:04:20 +0000273 // the allocation, return Def. This means that there is no dependence and
Chris Lattner237a8282008-11-30 01:39:32 +0000274 // the access can be optimized based on that. For example, a load could
275 // turn into undef.
Victor Hernandez5c787362009-10-13 01:42:53 +0000276 // Note: Only determine this to be a malloc if Inst is the malloc call, not
277 // a subsequent bitcast of the malloc call result. There can be stores to
278 // the malloced memory between the malloc call and its bitcast uses, and we
279 // need to continue scanning until the malloc call.
Victor Hernandez7b929da2009-10-23 21:09:37 +0000280 if (isa<AllocaInst>(Inst) || extractMallocCall(Inst)) {
Victor Hernandez46e83122009-09-18 21:34:51 +0000281 Value *AccessPtr = MemPtr->getUnderlyingObject();
282
283 if (AccessPtr == Inst ||
284 AA->alias(Inst, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
285 return MemDepResult::getDef(Inst);
286 continue;
287 }
288
Chris Lattnerb51deb92008-12-05 21:04:20 +0000289 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
Chris Lattner3579e442008-12-09 19:47:40 +0000290 switch (AA->getModRefInfo(Inst, MemPtr, MemSize)) {
291 case AliasAnalysis::NoModRef:
292 // If the call has no effect on the queried pointer, just ignore it.
Chris Lattner25a08142008-11-29 08:51:16 +0000293 continue;
Owen Andersona85a6642009-10-28 06:30:52 +0000294 case AliasAnalysis::Mod:
295 // If we're in an invariant region, we can ignore calls that ONLY
296 // modify the pointer.
Chris Lattner1e8de492009-12-01 21:16:01 +0000297 if (InvariantTag) continue;
Owen Andersona85a6642009-10-28 06:30:52 +0000298 return MemDepResult::getClobber(Inst);
Chris Lattner3579e442008-12-09 19:47:40 +0000299 case AliasAnalysis::Ref:
300 // If the call is known to never store to the pointer, and if this is a
301 // load query, we can safely ignore it (scan past it).
302 if (isLoad)
303 continue;
Chris Lattner3579e442008-12-09 19:47:40 +0000304 default:
305 // Otherwise, there is a potential dependence. Return a clobber.
306 return MemDepResult::getClobber(Inst);
307 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000308 }
309
Chris Lattner7ebcf032008-12-07 02:15:47 +0000310 // No dependence found. If this is the entry block of the function, it is a
311 // clobber, otherwise it is non-local.
312 if (BB != &BB->getParent()->getEntryBlock())
313 return MemDepResult::getNonLocal();
314 return MemDepResult::getClobber(ScanIt);
Owen Anderson78e02f72007-07-06 23:14:35 +0000315}
316
Chris Lattner5391a1d2008-11-29 03:47:00 +0000317/// getDependency - Return the instruction on which a memory operation
318/// depends.
319MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
320 Instruction *ScanPos = QueryInst;
321
322 // Check for a cached result
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000323 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattner5391a1d2008-11-29 03:47:00 +0000324
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000325 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000326 // on MemDepResult's default constructing to 'dirty'.
327 if (!LocalCache.isDirty())
328 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000329
330 // Otherwise, if we have a dirty entry, we know we can start the scan at that
331 // instruction, which may save us some work.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000332 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000333 ScanPos = Inst;
Chris Lattner4a69bad2008-11-30 02:52:26 +0000334
Chris Lattnerd44745d2008-12-07 18:39:13 +0000335 RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);
Chris Lattner4a69bad2008-11-30 02:52:26 +0000336 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000337
Chris Lattnere79be942008-12-07 01:50:16 +0000338 BasicBlock *QueryParent = QueryInst->getParent();
339
340 Value *MemPtr = 0;
341 uint64_t MemSize = 0;
342
Chris Lattner5391a1d2008-11-29 03:47:00 +0000343 // Do the scan.
Chris Lattnere79be942008-12-07 01:50:16 +0000344 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000345 // No dependence found. If this is the entry block of the function, it is a
346 // clobber, otherwise it is non-local.
347 if (QueryParent != &QueryParent->getParent()->getEntryBlock())
348 LocalCache = MemDepResult::getNonLocal();
349 else
350 LocalCache = MemDepResult::getClobber(QueryInst);
Chris Lattnere79be942008-12-07 01:50:16 +0000351 } else if (StoreInst *SI = dyn_cast<StoreInst>(QueryInst)) {
352 // If this is a volatile store, don't mess around with it. Just return the
353 // previous instruction as a clobber.
354 if (SI->isVolatile())
355 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
356 else {
357 MemPtr = SI->getPointerOperand();
Dan Gohmanf5812132009-07-31 20:53:12 +0000358 MemSize = AA->getTypeStoreSize(SI->getOperand(0)->getType());
Chris Lattnere79be942008-12-07 01:50:16 +0000359 }
360 } else if (LoadInst *LI = dyn_cast<LoadInst>(QueryInst)) {
361 // If this is a volatile load, don't mess around with it. Just return the
362 // previous instruction as a clobber.
363 if (LI->isVolatile())
364 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
365 else {
366 MemPtr = LI->getPointerOperand();
Dan Gohmanf5812132009-07-31 20:53:12 +0000367 MemSize = AA->getTypeStoreSize(LI->getType());
Chris Lattnere79be942008-12-07 01:50:16 +0000368 }
Victor Hernandez66284e02009-10-24 04:23:03 +0000369 } else if (isFreeCall(QueryInst)) {
Victor Hernandez046e78c2009-10-26 23:43:48 +0000370 MemPtr = QueryInst->getOperand(1);
Victor Hernandez66284e02009-10-24 04:23:03 +0000371 // calls to free() erase the entire structure, not just a field.
372 MemSize = ~0UL;
Chris Lattnere79be942008-12-07 01:50:16 +0000373 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Nick Lewyckyd801c102009-11-28 21:27:49 +0000374 int IntrinsicID = 0; // Intrinsic IDs start at 1.
375 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(QueryInst))
376 IntrinsicID = II->getIntrinsicID();
377
378 switch (IntrinsicID) {
379 case Intrinsic::lifetime_start:
380 case Intrinsic::lifetime_end:
381 case Intrinsic::invariant_start:
382 MemPtr = QueryInst->getOperand(2);
383 MemSize = cast<ConstantInt>(QueryInst->getOperand(1))->getZExtValue();
384 break;
385 case Intrinsic::invariant_end:
386 MemPtr = QueryInst->getOperand(3);
387 MemSize = cast<ConstantInt>(QueryInst->getOperand(2))->getZExtValue();
388 break;
389 default:
390 CallSite QueryCS = CallSite::get(QueryInst);
391 bool isReadOnly = AA->onlyReadsMemory(QueryCS);
392 LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos,
393 QueryParent);
394 }
Chris Lattnere79be942008-12-07 01:50:16 +0000395 } else {
396 // Non-memory instruction.
397 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
398 }
399
400 // If we need to do a pointer scan, make it happen.
Nick Lewyckyd801c102009-11-28 21:27:49 +0000401 if (MemPtr) {
402 bool isLoad = !QueryInst->mayWriteToMemory();
403 if (IntrinsicInst *II = dyn_cast<MemoryUseIntrinsic>(QueryInst)) {
404 isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_end;
405 }
406 LocalCache = getPointerDependencyFrom(MemPtr, MemSize, isLoad, ScanPos,
407 QueryParent);
408 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000409
410 // Remember the result!
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000411 if (Instruction *I = LocalCache.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000412 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000413
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000414 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000415}
416
Chris Lattner12a7db32009-01-22 07:04:01 +0000417#ifndef NDEBUG
418/// AssertSorted - This method is used when -debug is specified to verify that
419/// cache arrays are properly kept sorted.
420static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
421 int Count = -1) {
422 if (Count == -1) Count = Cache.size();
423 if (Count == 0) return;
424
425 for (unsigned i = 1; i != unsigned(Count); ++i)
426 assert(Cache[i-1] <= Cache[i] && "Cache isn't sorted!");
427}
428#endif
429
Chris Lattner1559b362008-12-09 19:38:05 +0000430/// getNonLocalCallDependency - Perform a full dependency query for the
431/// specified call, returning the set of blocks that the value is
Chris Lattner37d041c2008-11-30 01:18:27 +0000432/// potentially live across. The returned set of results will include a
433/// "NonLocal" result for all blocks where the value is live across.
434///
Chris Lattner1559b362008-12-09 19:38:05 +0000435/// This method assumes the instruction returns a "NonLocal" dependency
Chris Lattner37d041c2008-11-30 01:18:27 +0000436/// within its own block.
437///
Chris Lattner1559b362008-12-09 19:38:05 +0000438/// This returns a reference to an internal data structure that may be
439/// invalidated on the next non-local query or when an instruction is
440/// removed. Clients must copy this data if they want it around longer than
441/// that.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000442const MemoryDependenceAnalysis::NonLocalDepInfo &
Chris Lattner1559b362008-12-09 19:38:05 +0000443MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) {
444 assert(getDependency(QueryCS.getInstruction()).isNonLocal() &&
445 "getNonLocalCallDependency should only be used on calls with non-local deps!");
446 PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()];
Chris Lattnerbf145d62008-12-01 01:15:42 +0000447 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner37d041c2008-11-30 01:18:27 +0000448
449 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
450 /// the cached case, this can happen due to instructions being deleted etc. In
451 /// the uncached case, this starts out as the set of predecessors we care
452 /// about.
453 SmallVector<BasicBlock*, 32> DirtyBlocks;
454
455 if (!Cache.empty()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000456 // Okay, we have a cache entry. If we know it is not dirty, just return it
457 // with no computation.
458 if (!CacheP.second) {
459 NumCacheNonLocal++;
460 return Cache;
461 }
462
Chris Lattner37d041c2008-11-30 01:18:27 +0000463 // If we already have a partially computed set of results, scan them to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000464 // determine what is dirty, seeding our initial DirtyBlocks worklist.
465 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
466 I != E; ++I)
467 if (I->second.isDirty())
468 DirtyBlocks.push_back(I->first);
Chris Lattner37d041c2008-11-30 01:18:27 +0000469
Chris Lattnerbf145d62008-12-01 01:15:42 +0000470 // Sort the cache so that we can do fast binary search lookups below.
471 std::sort(Cache.begin(), Cache.end());
Chris Lattner37d041c2008-11-30 01:18:27 +0000472
Chris Lattnerbf145d62008-12-01 01:15:42 +0000473 ++NumCacheDirtyNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000474 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
475 // << Cache.size() << " cached: " << *QueryInst;
476 } else {
477 // Seed DirtyBlocks with each of the preds of QueryInst's block.
Chris Lattner1559b362008-12-09 19:38:05 +0000478 BasicBlock *QueryBB = QueryCS.getInstruction()->getParent();
Chris Lattner511b36c2008-12-09 06:44:17 +0000479 for (BasicBlock **PI = PredCache->GetPreds(QueryBB); *PI; ++PI)
480 DirtyBlocks.push_back(*PI);
Chris Lattner37d041c2008-11-30 01:18:27 +0000481 NumUncacheNonLocal++;
482 }
483
Chris Lattner20d6f092008-12-09 21:19:42 +0000484 // isReadonlyCall - If this is a read-only call, we can be more aggressive.
485 bool isReadonlyCall = AA->onlyReadsMemory(QueryCS);
Chris Lattner9e59c642008-12-15 03:35:32 +0000486
Chris Lattnerbf145d62008-12-01 01:15:42 +0000487 SmallPtrSet<BasicBlock*, 64> Visited;
488
489 unsigned NumSortedEntries = Cache.size();
Chris Lattner12a7db32009-01-22 07:04:01 +0000490 DEBUG(AssertSorted(Cache));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000491
Chris Lattner37d041c2008-11-30 01:18:27 +0000492 // Iterate while we still have blocks to update.
493 while (!DirtyBlocks.empty()) {
494 BasicBlock *DirtyBB = DirtyBlocks.back();
495 DirtyBlocks.pop_back();
496
Chris Lattnerbf145d62008-12-01 01:15:42 +0000497 // Already processed this block?
498 if (!Visited.insert(DirtyBB))
499 continue;
Chris Lattner37d041c2008-11-30 01:18:27 +0000500
Chris Lattnerbf145d62008-12-01 01:15:42 +0000501 // Do a binary search to see if we already have an entry for this block in
502 // the cache set. If so, find it.
Chris Lattner12a7db32009-01-22 07:04:01 +0000503 DEBUG(AssertSorted(Cache, NumSortedEntries));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000504 NonLocalDepInfo::iterator Entry =
505 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
506 std::make_pair(DirtyBB, MemDepResult()));
Duncan Sands7050f3d2008-12-10 09:38:36 +0000507 if (Entry != Cache.begin() && prior(Entry)->first == DirtyBB)
Chris Lattnerbf145d62008-12-01 01:15:42 +0000508 --Entry;
509
510 MemDepResult *ExistingResult = 0;
511 if (Entry != Cache.begin()+NumSortedEntries &&
512 Entry->first == DirtyBB) {
513 // If we already have an entry, and if it isn't already dirty, the block
514 // is done.
515 if (!Entry->second.isDirty())
516 continue;
517
518 // Otherwise, remember this slot so we can update the value.
519 ExistingResult = &Entry->second;
520 }
521
Chris Lattner37d041c2008-11-30 01:18:27 +0000522 // If the dirty entry has a pointer, start scanning from it so we don't have
523 // to rescan the entire block.
524 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerbf145d62008-12-01 01:15:42 +0000525 if (ExistingResult) {
526 if (Instruction *Inst = ExistingResult->getInst()) {
527 ScanPos = Inst;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000528 // We're removing QueryInst's use of Inst.
Chris Lattner1559b362008-12-09 19:38:05 +0000529 RemoveFromReverseMap(ReverseNonLocalDeps, Inst,
530 QueryCS.getInstruction());
Chris Lattnerbf145d62008-12-01 01:15:42 +0000531 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000532 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000533
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000534 // Find out if this block has a local dependency for QueryInst.
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000535 MemDepResult Dep;
Chris Lattnere79be942008-12-07 01:50:16 +0000536
Chris Lattner1559b362008-12-09 19:38:05 +0000537 if (ScanPos != DirtyBB->begin()) {
Chris Lattner20d6f092008-12-09 21:19:42 +0000538 Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB);
Chris Lattner1559b362008-12-09 19:38:05 +0000539 } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {
540 // No dependence found. If this is the entry block of the function, it is
541 // a clobber, otherwise it is non-local.
542 Dep = MemDepResult::getNonLocal();
Chris Lattnere79be942008-12-07 01:50:16 +0000543 } else {
Chris Lattner1559b362008-12-09 19:38:05 +0000544 Dep = MemDepResult::getClobber(ScanPos);
Chris Lattnere79be942008-12-07 01:50:16 +0000545 }
546
Chris Lattnerbf145d62008-12-01 01:15:42 +0000547 // If we had a dirty entry for the block, update it. Otherwise, just add
548 // a new entry.
549 if (ExistingResult)
550 *ExistingResult = Dep;
551 else
552 Cache.push_back(std::make_pair(DirtyBB, Dep));
553
Chris Lattner37d041c2008-11-30 01:18:27 +0000554 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000555 // the value), remember the association!
556 if (!Dep.isNonLocal()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000557 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
558 // update this when we remove instructions.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000559 if (Instruction *Inst = Dep.getInst())
Chris Lattner1559b362008-12-09 19:38:05 +0000560 ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction());
Chris Lattnerbf145d62008-12-01 01:15:42 +0000561 } else {
Chris Lattner37d041c2008-11-30 01:18:27 +0000562
Chris Lattnerbf145d62008-12-01 01:15:42 +0000563 // If the block *is* completely transparent to the load, we need to check
564 // the predecessors of this block. Add them to our worklist.
Chris Lattner511b36c2008-12-09 06:44:17 +0000565 for (BasicBlock **PI = PredCache->GetPreds(DirtyBB); *PI; ++PI)
566 DirtyBlocks.push_back(*PI);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000567 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000568 }
569
Chris Lattnerbf145d62008-12-01 01:15:42 +0000570 return Cache;
Chris Lattner37d041c2008-11-30 01:18:27 +0000571}
572
Chris Lattner7ebcf032008-12-07 02:15:47 +0000573/// getNonLocalPointerDependency - Perform a full dependency query for an
574/// access to the specified (non-volatile) memory location, returning the
575/// set of instructions that either define or clobber the value.
576///
577/// This method assumes the pointer has a "NonLocal" dependency within its
578/// own block.
579///
580void MemoryDependenceAnalysis::
581getNonLocalPointerDependency(Value *Pointer, bool isLoad, BasicBlock *FromBB,
582 SmallVectorImpl<NonLocalDepEntry> &Result) {
Chris Lattner3f7eb5b2008-12-07 18:45:15 +0000583 assert(isa<PointerType>(Pointer->getType()) &&
584 "Can't get pointer deps of a non-pointer!");
Chris Lattner9a193fd2008-12-07 02:56:57 +0000585 Result.clear();
586
Chris Lattner7ebcf032008-12-07 02:15:47 +0000587 // We know that the pointer value is live into FromBB find the def/clobbers
588 // from presecessors.
Chris Lattner7ebcf032008-12-07 02:15:47 +0000589 const Type *EltTy = cast<PointerType>(Pointer->getType())->getElementType();
Dan Gohmanf5812132009-07-31 20:53:12 +0000590 uint64_t PointeeSize = AA->getTypeStoreSize(EltTy);
Chris Lattner7ebcf032008-12-07 02:15:47 +0000591
Chris Lattner9e59c642008-12-15 03:35:32 +0000592 // This is the set of blocks we've inspected, and the pointer we consider in
593 // each block. Because of critical edges, we currently bail out if querying
594 // a block with multiple different pointers. This can happen during PHI
595 // translation.
596 DenseMap<BasicBlock*, Value*> Visited;
597 if (!getNonLocalPointerDepFromBB(Pointer, PointeeSize, isLoad, FromBB,
598 Result, Visited, true))
599 return;
Chris Lattner3af23f82008-12-15 04:58:29 +0000600 Result.clear();
Chris Lattner9e59c642008-12-15 03:35:32 +0000601 Result.push_back(std::make_pair(FromBB,
602 MemDepResult::getClobber(FromBB->begin())));
Chris Lattner9a193fd2008-12-07 02:56:57 +0000603}
604
Chris Lattner9863c3f2008-12-09 07:47:11 +0000605/// GetNonLocalInfoForBlock - Compute the memdep value for BB with
606/// Pointer/PointeeSize using either cached information in Cache or by doing a
607/// lookup (which may use dirty cache info if available). If we do a lookup,
608/// add the result to the cache.
609MemDepResult MemoryDependenceAnalysis::
610GetNonLocalInfoForBlock(Value *Pointer, uint64_t PointeeSize,
611 bool isLoad, BasicBlock *BB,
612 NonLocalDepInfo *Cache, unsigned NumSortedEntries) {
613
614 // Do a binary search to see if we already have an entry for this block in
615 // the cache set. If so, find it.
616 NonLocalDepInfo::iterator Entry =
617 std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries,
618 std::make_pair(BB, MemDepResult()));
Duncan Sands7050f3d2008-12-10 09:38:36 +0000619 if (Entry != Cache->begin() && prior(Entry)->first == BB)
Chris Lattner9863c3f2008-12-09 07:47:11 +0000620 --Entry;
621
622 MemDepResult *ExistingResult = 0;
623 if (Entry != Cache->begin()+NumSortedEntries && Entry->first == BB)
624 ExistingResult = &Entry->second;
625
626 // If we have a cached entry, and it is non-dirty, use it as the value for
627 // this dependency.
628 if (ExistingResult && !ExistingResult->isDirty()) {
629 ++NumCacheNonLocalPtr;
630 return *ExistingResult;
631 }
632
633 // Otherwise, we have to scan for the value. If we have a dirty cache
634 // entry, start scanning from its position, otherwise we scan from the end
635 // of the block.
636 BasicBlock::iterator ScanPos = BB->end();
637 if (ExistingResult && ExistingResult->getInst()) {
638 assert(ExistingResult->getInst()->getParent() == BB &&
639 "Instruction invalidated?");
640 ++NumCacheDirtyNonLocalPtr;
641 ScanPos = ExistingResult->getInst();
642
643 // Eliminating the dirty entry from 'Cache', so update the reverse info.
644 ValueIsLoadPair CacheKey(Pointer, isLoad);
Chris Lattner6a0dcc12009-03-29 00:24:04 +0000645 RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000646 } else {
647 ++NumUncacheNonLocalPtr;
648 }
649
650 // Scan the block for the dependency.
651 MemDepResult Dep = getPointerDependencyFrom(Pointer, PointeeSize, isLoad,
652 ScanPos, BB);
653
654 // If we had a dirty entry for the block, update it. Otherwise, just add
655 // a new entry.
656 if (ExistingResult)
657 *ExistingResult = Dep;
658 else
659 Cache->push_back(std::make_pair(BB, Dep));
660
661 // If the block has a dependency (i.e. it isn't completely transparent to
662 // the value), remember the reverse association because we just added it
663 // to Cache!
664 if (Dep.isNonLocal())
665 return Dep;
666
667 // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently
668 // update MemDep when we remove instructions.
669 Instruction *Inst = Dep.getInst();
670 assert(Inst && "Didn't depend on anything?");
671 ValueIsLoadPair CacheKey(Pointer, isLoad);
Chris Lattner6a0dcc12009-03-29 00:24:04 +0000672 ReverseNonLocalPtrDeps[Inst].insert(CacheKey);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000673 return Dep;
674}
675
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000676/// SortNonLocalDepInfoCache - Sort the a NonLocalDepInfo cache, given a certain
677/// number of elements in the array that are already properly ordered. This is
678/// optimized for the case when only a few entries are added.
679static void
680SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
681 unsigned NumSortedEntries) {
682 switch (Cache.size() - NumSortedEntries) {
683 case 0:
684 // done, no new entries.
685 break;
686 case 2: {
687 // Two new entries, insert the last one into place.
688 MemoryDependenceAnalysis::NonLocalDepEntry Val = Cache.back();
689 Cache.pop_back();
690 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
691 std::upper_bound(Cache.begin(), Cache.end()-1, Val);
692 Cache.insert(Entry, Val);
693 // FALL THROUGH.
694 }
695 case 1:
696 // One new entry, Just insert the new value at the appropriate position.
697 if (Cache.size() != 1) {
698 MemoryDependenceAnalysis::NonLocalDepEntry Val = Cache.back();
699 Cache.pop_back();
700 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
701 std::upper_bound(Cache.begin(), Cache.end(), Val);
702 Cache.insert(Entry, Val);
703 }
704 break;
705 default:
706 // Added many values, do a full scale sort.
707 std::sort(Cache.begin(), Cache.end());
708 break;
709 }
710}
711
Chris Lattnerdc593112009-11-26 23:18:49 +0000712/// isPHITranslatable - Return true if the specified computation is derived from
713/// a PHI node in the current block and if it is simple enough for us to handle.
714static bool isPHITranslatable(Instruction *Inst) {
715 if (isa<PHINode>(Inst))
716 return true;
717
Chris Lattnercc3d0eb2009-11-26 23:41:07 +0000718 // We can handle bitcast of a PHI, but the PHI needs to be in the same block
719 // as the bitcast.
Chris Lattner51414212009-11-27 20:25:30 +0000720 if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) {
721 Instruction *OpI = dyn_cast<Instruction>(BC->getOperand(0));
722 if (OpI == 0 || OpI->getParent() != Inst->getParent())
723 return true;
724 return isPHITranslatable(OpI);
725 }
Chris Lattnerdc593112009-11-26 23:18:49 +0000726
Chris Lattner11c6bab2009-11-27 19:11:31 +0000727 // We can translate a GEP if all of its operands defined in this block are phi
728 // translatable.
Chris Lattner30407622009-11-27 00:07:37 +0000729 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
Chris Lattner11c6bab2009-11-27 19:11:31 +0000730 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
Chris Lattner51414212009-11-27 20:25:30 +0000731 Instruction *OpI = dyn_cast<Instruction>(GEP->getOperand(i));
732 if (OpI == 0 || OpI->getParent() != Inst->getParent())
Chris Lattner11c6bab2009-11-27 19:11:31 +0000733 continue;
734
Chris Lattner51414212009-11-27 20:25:30 +0000735 if (!isPHITranslatable(OpI))
Chris Lattner11c6bab2009-11-27 19:11:31 +0000736 return false;
737 }
738 return true;
739 }
740
741 if (Inst->getOpcode() == Instruction::Add &&
742 isa<ConstantInt>(Inst->getOperand(1))) {
Chris Lattner51414212009-11-27 20:25:30 +0000743 Instruction *OpI = dyn_cast<Instruction>(Inst->getOperand(0));
744 if (OpI == 0 || OpI->getParent() != Inst->getParent())
Chris Lattner11c6bab2009-11-27 19:11:31 +0000745 return true;
Chris Lattner51414212009-11-27 20:25:30 +0000746 return isPHITranslatable(OpI);
Chris Lattner30407622009-11-27 00:07:37 +0000747 }
Chris Lattnercc3d0eb2009-11-26 23:41:07 +0000748
Chris Lattnerdc593112009-11-26 23:18:49 +0000749 // cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
750 // if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
751 // cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
752
753 return false;
754}
755
Chris Lattner6f7b2102009-11-27 22:05:15 +0000756/// GetPHITranslatedValue - Given a computation that satisfied the
Chris Lattnerdc593112009-11-26 23:18:49 +0000757/// isPHITranslatable predicate, see if we can translate the computation into
758/// the specified predecessor block. If so, return that value.
Chris Lattner62deff02009-11-27 06:31:14 +0000759Value *MemoryDependenceAnalysis::
Chris Lattner6f7b2102009-11-27 22:05:15 +0000760GetPHITranslatedValue(Value *InVal, BasicBlock *CurBB, BasicBlock *Pred,
761 const TargetData *TD) const {
Chris Lattner62deff02009-11-27 06:31:14 +0000762 // If the input value is not an instruction, or if it is not defined in CurBB,
763 // then we don't need to phi translate it.
764 Instruction *Inst = dyn_cast<Instruction>(InVal);
765 if (Inst == 0 || Inst->getParent() != CurBB)
766 return InVal;
767
Chris Lattnerdc593112009-11-26 23:18:49 +0000768 if (PHINode *PN = dyn_cast<PHINode>(Inst))
769 return PN->getIncomingValueForBlock(Pred);
770
Chris Lattner30407622009-11-27 00:07:37 +0000771 // Handle bitcast of PHI.
Chris Lattnercc3d0eb2009-11-26 23:41:07 +0000772 if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) {
Chris Lattner51414212009-11-27 20:25:30 +0000773 // PHI translate the input operand.
Chris Lattner6f7b2102009-11-27 22:05:15 +0000774 Value *PHIIn = GetPHITranslatedValue(BC->getOperand(0), CurBB, Pred, TD);
Chris Lattner51414212009-11-27 20:25:30 +0000775 if (PHIIn == 0) return 0;
Chris Lattnercc3d0eb2009-11-26 23:41:07 +0000776
777 // Constants are trivial to phi translate.
778 if (Constant *C = dyn_cast<Constant>(PHIIn))
779 return ConstantExpr::getBitCast(C, BC->getType());
780
781 // Otherwise we have to see if a bitcasted version of the incoming pointer
782 // is available. If so, we can use it, otherwise we have to fail.
783 for (Value::use_iterator UI = PHIIn->use_begin(), E = PHIIn->use_end();
784 UI != E; ++UI) {
785 if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI))
786 if (BCI->getType() == BC->getType())
787 return BCI;
788 }
789 return 0;
790 }
791
Chris Lattner11c6bab2009-11-27 19:11:31 +0000792 // Handle getelementptr with at least one PHI translatable operand.
Chris Lattner30407622009-11-27 00:07:37 +0000793 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
794 SmallVector<Value*, 8> GEPOps;
Chris Lattnercca130b2009-11-27 05:19:56 +0000795 BasicBlock *CurBB = GEP->getParent();
Chris Lattner30407622009-11-27 00:07:37 +0000796 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
Chris Lattner616613d2009-11-27 08:25:10 +0000797 Value *GEPOp = GEP->getOperand(i);
798 // No PHI translation is needed of operands whose values are live in to
799 // the predecessor block.
800 if (!isa<Instruction>(GEPOp) ||
801 cast<Instruction>(GEPOp)->getParent() != CurBB) {
802 GEPOps.push_back(GEPOp);
803 continue;
804 }
805
806 // If the operand is a phi node, do phi translation.
Chris Lattner6f7b2102009-11-27 22:05:15 +0000807 Value *InOp = GetPHITranslatedValue(GEPOp, CurBB, Pred, TD);
Chris Lattner51414212009-11-27 20:25:30 +0000808 if (InOp == 0) return 0;
Chris Lattner616613d2009-11-27 08:25:10 +0000809
Chris Lattner51414212009-11-27 20:25:30 +0000810 GEPOps.push_back(InOp);
Chris Lattner30407622009-11-27 00:07:37 +0000811 }
812
Chris Lattnere19e4ba2009-11-27 00:34:38 +0000813 // Simplify the GEP to handle 'gep x, 0' -> x etc.
814 if (Value *V = SimplifyGEPInst(&GEPOps[0], GEPOps.size(), TD))
815 return V;
Chris Lattner616613d2009-11-27 08:25:10 +0000816
Chris Lattner30407622009-11-27 00:07:37 +0000817 // Scan to see if we have this GEP available.
Chris Lattner616613d2009-11-27 08:25:10 +0000818 Value *APHIOp = GEPOps[0];
Chris Lattner30407622009-11-27 00:07:37 +0000819 for (Value::use_iterator UI = APHIOp->use_begin(), E = APHIOp->use_end();
820 UI != E; ++UI) {
821 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI))
Chris Lattnercca130b2009-11-27 05:19:56 +0000822 if (GEPI->getType() == GEP->getType() &&
Chris Lattner30407622009-11-27 00:07:37 +0000823 GEPI->getNumOperands() == GEPOps.size() &&
Chris Lattnercca130b2009-11-27 05:19:56 +0000824 GEPI->getParent()->getParent() == CurBB->getParent()) {
Chris Lattner30407622009-11-27 00:07:37 +0000825 bool Mismatch = false;
826 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
827 if (GEPI->getOperand(i) != GEPOps[i]) {
828 Mismatch = true;
829 break;
830 }
831 if (!Mismatch)
832 return GEPI;
833 }
834 }
835 return 0;
836 }
837
Chris Lattner11c6bab2009-11-27 19:11:31 +0000838 // Handle add with a constant RHS.
839 if (Inst->getOpcode() == Instruction::Add &&
840 isa<ConstantInt>(Inst->getOperand(1))) {
841 // PHI translate the LHS.
842 Value *LHS;
843 Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
844 Instruction *OpI = dyn_cast<Instruction>(Inst->getOperand(0));
845 bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
846 bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
847
848 if (OpI == 0 || OpI->getParent() != Inst->getParent())
849 LHS = Inst->getOperand(0);
850 else {
Chris Lattner6f7b2102009-11-27 22:05:15 +0000851 LHS = GetPHITranslatedValue(Inst->getOperand(0), CurBB, Pred, TD);
Chris Lattner11c6bab2009-11-27 19:11:31 +0000852 if (LHS == 0)
853 return 0;
854 }
855
856 // If the PHI translated LHS is an add of a constant, fold the immediates.
857 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS))
858 if (BOp->getOpcode() == Instruction::Add)
859 if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
860 LHS = BOp->getOperand(0);
861 RHS = ConstantExpr::getAdd(RHS, CI);
862 isNSW = isNUW = false;
863 }
864
865 // See if the add simplifies away.
866 if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, TD))
867 return Res;
868
869 // Otherwise, see if we have this add available somewhere.
870 for (Value::use_iterator UI = LHS->use_begin(), E = LHS->use_end();
871 UI != E; ++UI) {
872 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(*UI))
873 if (BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
874 BO->getParent()->getParent() == CurBB->getParent())
875 return BO;
876 }
877
878 return 0;
879 }
880
Chris Lattnerdc593112009-11-26 23:18:49 +0000881 return 0;
882}
883
Chris Lattner6f7b2102009-11-27 22:05:15 +0000884/// GetAvailablePHITranslatePointer - Return the value computed by
885/// PHITranslatePointer if it dominates PredBB, otherwise return null.
886Value *MemoryDependenceAnalysis::
887GetAvailablePHITranslatedValue(Value *V,
888 BasicBlock *CurBB, BasicBlock *PredBB,
889 const TargetData *TD,
890 const DominatorTree &DT) const {
891 // See if PHI translation succeeds.
892 V = GetPHITranslatedValue(V, CurBB, PredBB, TD);
893 if (V == 0) return 0;
894
895 // Make sure the value is live in the predecessor.
896 if (Instruction *Inst = dyn_cast_or_null<Instruction>(V))
897 if (!DT.dominates(Inst->getParent(), PredBB))
898 return 0;
899 return V;
900}
901
902
Chris Lattner616613d2009-11-27 08:25:10 +0000903/// InsertPHITranslatedPointer - Insert a computation of the PHI translated
904/// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
Chris Lattnerdd696052009-11-28 15:39:14 +0000905/// block. All newly created instructions are added to the NewInsts list.
Chris Lattner616613d2009-11-27 08:25:10 +0000906///
Chris Lattner616613d2009-11-27 08:25:10 +0000907Value *MemoryDependenceAnalysis::
908InsertPHITranslatedPointer(Value *InVal, BasicBlock *CurBB,
Chris Lattner6f7b2102009-11-27 22:05:15 +0000909 BasicBlock *PredBB, const TargetData *TD,
Chris Lattnerdd696052009-11-28 15:39:14 +0000910 const DominatorTree &DT,
911 SmallVectorImpl<Instruction*> &NewInsts) const {
Chris Lattner6f7b2102009-11-27 22:05:15 +0000912 // See if we have a version of this value already available and dominating
913 // PredBB. If so, there is no need to insert a new copy.
914 if (Value *Res = GetAvailablePHITranslatedValue(InVal, CurBB, PredBB, TD, DT))
915 return Res;
916
917 // If we don't have an available version of this value, it must be an
918 // instruction.
Chris Lattner616613d2009-11-27 08:25:10 +0000919 Instruction *Inst = cast<Instruction>(InVal);
Chris Lattner6f7b2102009-11-27 22:05:15 +0000920
921 // Handle bitcast of PHI translatable value.
Chris Lattner616613d2009-11-27 08:25:10 +0000922 if (BitCastInst *BC = dyn_cast<BitCastInst>(Inst)) {
Chris Lattner6f7b2102009-11-27 22:05:15 +0000923 Value *OpVal = InsertPHITranslatedPointer(BC->getOperand(0),
Chris Lattnerdd696052009-11-28 15:39:14 +0000924 CurBB, PredBB, TD, DT, NewInsts);
Chris Lattner6f7b2102009-11-27 22:05:15 +0000925 if (OpVal == 0) return 0;
926
Chris Lattner616613d2009-11-27 08:25:10 +0000927 // Otherwise insert a bitcast at the end of PredBB.
Chris Lattnerdd696052009-11-28 15:39:14 +0000928 BitCastInst *New = new BitCastInst(OpVal, InVal->getType(),
929 InVal->getName()+".phi.trans.insert",
930 PredBB->getTerminator());
931 NewInsts.push_back(New);
932 return New;
Chris Lattner616613d2009-11-27 08:25:10 +0000933 }
934
935 // Handle getelementptr with at least one PHI operand.
936 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
937 SmallVector<Value*, 8> GEPOps;
Chris Lattner616613d2009-11-27 08:25:10 +0000938 BasicBlock *CurBB = GEP->getParent();
939 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
Chris Lattner6f7b2102009-11-27 22:05:15 +0000940 Value *OpVal = InsertPHITranslatedPointer(GEP->getOperand(i),
Chris Lattnerdd696052009-11-28 15:39:14 +0000941 CurBB, PredBB, TD, DT, NewInsts);
Chris Lattner6f7b2102009-11-27 22:05:15 +0000942 if (OpVal == 0) return 0;
943 GEPOps.push_back(OpVal);
Chris Lattner616613d2009-11-27 08:25:10 +0000944 }
945
946 GetElementPtrInst *Result =
947 GetElementPtrInst::Create(GEPOps[0], GEPOps.begin()+1, GEPOps.end(),
948 InVal->getName()+".phi.trans.insert",
949 PredBB->getTerminator());
950 Result->setIsInBounds(GEP->isInBounds());
Chris Lattnerdd696052009-11-28 15:39:14 +0000951 NewInsts.push_back(Result);
Chris Lattner616613d2009-11-27 08:25:10 +0000952 return Result;
953 }
954
Chris Lattner6f7b2102009-11-27 22:05:15 +0000955#if 0
956 // FIXME: This code works, but it is unclear that we actually want to insert
957 // a big chain of computation in order to make a value available in a block.
958 // This needs to be evaluated carefully to consider its cost trade offs.
959
960 // Handle add with a constant RHS.
961 if (Inst->getOpcode() == Instruction::Add &&
962 isa<ConstantInt>(Inst->getOperand(1))) {
963 // PHI translate the LHS.
964 Value *OpVal = InsertPHITranslatedPointer(Inst->getOperand(0),
Chris Lattnerdd696052009-11-28 15:39:14 +0000965 CurBB, PredBB, TD, DT, NewInsts);
Chris Lattner6f7b2102009-11-27 22:05:15 +0000966 if (OpVal == 0) return 0;
967
968 BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1),
969 InVal->getName()+".phi.trans.insert",
970 PredBB->getTerminator());
971 Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap());
972 Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap());
Chris Lattnerdd696052009-11-28 15:39:14 +0000973 NewInsts.push_back(Res);
Chris Lattner6f7b2102009-11-27 22:05:15 +0000974 return Res;
975 }
976#endif
977
Chris Lattner616613d2009-11-27 08:25:10 +0000978 return 0;
979}
Chris Lattner9863c3f2008-12-09 07:47:11 +0000980
Chris Lattner9e59c642008-12-15 03:35:32 +0000981/// getNonLocalPointerDepFromBB - Perform a dependency query based on
982/// pointer/pointeesize starting at the end of StartBB. Add any clobber/def
983/// results to the results vector and keep track of which blocks are visited in
984/// 'Visited'.
985///
986/// This has special behavior for the first block queries (when SkipFirstBlock
987/// is true). In this special case, it ignores the contents of the specified
988/// block and starts returning dependence info for its predecessors.
989///
990/// This function returns false on success, or true to indicate that it could
991/// not compute dependence information for some reason. This should be treated
992/// as a clobber dependence on the first instruction in the predecessor block.
993bool MemoryDependenceAnalysis::
Chris Lattner9863c3f2008-12-09 07:47:11 +0000994getNonLocalPointerDepFromBB(Value *Pointer, uint64_t PointeeSize,
995 bool isLoad, BasicBlock *StartBB,
996 SmallVectorImpl<NonLocalDepEntry> &Result,
Chris Lattner9e59c642008-12-15 03:35:32 +0000997 DenseMap<BasicBlock*, Value*> &Visited,
998 bool SkipFirstBlock) {
Chris Lattner66364342009-09-20 22:44:26 +0000999
Chris Lattner6290f5c2008-12-07 08:50:20 +00001000 // Look up the cached info for Pointer.
1001 ValueIsLoadPair CacheKey(Pointer, isLoad);
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001002
Chris Lattner9e59c642008-12-15 03:35:32 +00001003 std::pair<BBSkipFirstBlockPair, NonLocalDepInfo> *CacheInfo =
1004 &NonLocalPointerDeps[CacheKey];
1005 NonLocalDepInfo *Cache = &CacheInfo->second;
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001006
1007 // If we have valid cached information for exactly the block we are
1008 // investigating, just return it with no recomputation.
Chris Lattner9e59c642008-12-15 03:35:32 +00001009 if (CacheInfo->first == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) {
Chris Lattnerf4789512008-12-16 07:10:09 +00001010 // We have a fully cached result for this query then we can just return the
1011 // cached results and populate the visited set. However, we have to verify
1012 // that we don't already have conflicting results for these blocks. Check
1013 // to ensure that if a block in the results set is in the visited set that
1014 // it was for the same pointer query.
1015 if (!Visited.empty()) {
1016 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
1017 I != E; ++I) {
1018 DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->first);
1019 if (VI == Visited.end() || VI->second == Pointer) continue;
1020
1021 // We have a pointer mismatch in a block. Just return clobber, saying
1022 // that something was clobbered in this result. We could also do a
1023 // non-fully cached query, but there is little point in doing this.
1024 return true;
1025 }
1026 }
1027
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001028 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
Chris Lattnerf4789512008-12-16 07:10:09 +00001029 I != E; ++I) {
1030 Visited.insert(std::make_pair(I->first, Pointer));
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001031 if (!I->second.isNonLocal())
1032 Result.push_back(*I);
Chris Lattnerf4789512008-12-16 07:10:09 +00001033 }
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001034 ++NumCacheCompleteNonLocalPtr;
Chris Lattner9e59c642008-12-15 03:35:32 +00001035 return false;
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001036 }
1037
1038 // Otherwise, either this is a new block, a block with an invalid cache
1039 // pointer or one that we're about to invalidate by putting more info into it
1040 // than its valid cache info. If empty, the result will be valid cache info,
1041 // otherwise it isn't.
Chris Lattner9e59c642008-12-15 03:35:32 +00001042 if (Cache->empty())
1043 CacheInfo->first = BBSkipFirstBlockPair(StartBB, SkipFirstBlock);
1044 else
1045 CacheInfo->first = BBSkipFirstBlockPair();
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001046
1047 SmallVector<BasicBlock*, 32> Worklist;
1048 Worklist.push_back(StartBB);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001049
1050 // Keep track of the entries that we know are sorted. Previously cached
1051 // entries will all be sorted. The entries we add we only sort on demand (we
1052 // don't insert every element into its sorted position). We know that we
1053 // won't get any reuse from currently inserted values, because we don't
1054 // revisit blocks after we insert info for them.
1055 unsigned NumSortedEntries = Cache->size();
Chris Lattner12a7db32009-01-22 07:04:01 +00001056 DEBUG(AssertSorted(*Cache));
Chris Lattner6290f5c2008-12-07 08:50:20 +00001057
Chris Lattner7ebcf032008-12-07 02:15:47 +00001058 while (!Worklist.empty()) {
Chris Lattner9a193fd2008-12-07 02:56:57 +00001059 BasicBlock *BB = Worklist.pop_back_val();
Chris Lattner7ebcf032008-12-07 02:15:47 +00001060
Chris Lattner65633712008-12-09 07:52:59 +00001061 // Skip the first block if we have it.
Chris Lattner9e59c642008-12-15 03:35:32 +00001062 if (!SkipFirstBlock) {
Chris Lattner65633712008-12-09 07:52:59 +00001063 // Analyze the dependency of *Pointer in FromBB. See if we already have
1064 // been here.
Chris Lattner9e59c642008-12-15 03:35:32 +00001065 assert(Visited.count(BB) && "Should check 'visited' before adding to WL");
Chris Lattner6290f5c2008-12-07 08:50:20 +00001066
Chris Lattner65633712008-12-09 07:52:59 +00001067 // Get the dependency info for Pointer in BB. If we have cached
1068 // information, we will use it, otherwise we compute it.
Chris Lattner12a7db32009-01-22 07:04:01 +00001069 DEBUG(AssertSorted(*Cache, NumSortedEntries));
Chris Lattner65633712008-12-09 07:52:59 +00001070 MemDepResult Dep = GetNonLocalInfoForBlock(Pointer, PointeeSize, isLoad,
1071 BB, Cache, NumSortedEntries);
1072
1073 // If we got a Def or Clobber, add this to the list of results.
1074 if (!Dep.isNonLocal()) {
1075 Result.push_back(NonLocalDepEntry(BB, Dep));
1076 continue;
1077 }
Chris Lattner7ebcf032008-12-07 02:15:47 +00001078 }
1079
Chris Lattner9e59c642008-12-15 03:35:32 +00001080 // If 'Pointer' is an instruction defined in this block, then we need to do
1081 // phi translation to change it into a value live in the predecessor block.
1082 // If phi translation fails, then we can't continue dependence analysis.
1083 Instruction *PtrInst = dyn_cast<Instruction>(Pointer);
1084 bool NeedsPHITranslation = PtrInst && PtrInst->getParent() == BB;
1085
1086 // If no PHI translation is needed, just add all the predecessors of this
1087 // block to scan them as well.
1088 if (!NeedsPHITranslation) {
1089 SkipFirstBlock = false;
1090 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
1091 // Verify that we haven't looked at this block yet.
1092 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
1093 InsertRes = Visited.insert(std::make_pair(*PI, Pointer));
1094 if (InsertRes.second) {
1095 // First time we've looked at *PI.
1096 Worklist.push_back(*PI);
1097 continue;
1098 }
1099
1100 // If we have seen this block before, but it was with a different
1101 // pointer then we have a phi translation failure and we have to treat
1102 // this as a clobber.
1103 if (InsertRes.first->second != Pointer)
1104 goto PredTranslationFailure;
1105 }
1106 continue;
1107 }
1108
1109 // If we do need to do phi translation, then there are a bunch of different
1110 // cases, because we have to find a Value* live in the predecessor block. We
1111 // know that PtrInst is defined in this block at least.
Chris Lattner6fbc1962009-07-13 17:14:23 +00001112
1113 // We may have added values to the cache list before this PHI translation.
1114 // If so, we haven't done anything to ensure that the cache remains sorted.
1115 // Sort it now (if needed) so that recursive invocations of
1116 // getNonLocalPointerDepFromBB and other routines that could reuse the cache
1117 // value will only see properly sorted cache arrays.
1118 if (Cache && NumSortedEntries != Cache->size()) {
Chris Lattnera2f55dd2009-07-13 17:20:05 +00001119 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner6fbc1962009-07-13 17:14:23 +00001120 NumSortedEntries = Cache->size();
1121 }
Chris Lattner9e59c642008-12-15 03:35:32 +00001122
Chris Lattnerdc593112009-11-26 23:18:49 +00001123 // If this is a computation derived from a PHI node, use the suitably
1124 // translated incoming values for each pred as the phi translated version.
Chris Lattnere95035a2009-11-27 08:37:22 +00001125 if (!isPHITranslatable(PtrInst))
1126 goto PredTranslationFailure;
1127
1128 Cache = 0;
Chris Lattner6fbc1962009-07-13 17:14:23 +00001129
Chris Lattnere95035a2009-11-27 08:37:22 +00001130 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
1131 BasicBlock *Pred = *PI;
Chris Lattner6f7b2102009-11-27 22:05:15 +00001132 // Get the PHI translated pointer in this predecessor. This can fail and
1133 // return null if not translatable.
1134 Value *PredPtr = GetPHITranslatedValue(PtrInst, BB, Pred, TD);
Chris Lattnere95035a2009-11-27 08:37:22 +00001135
1136 // Check to see if we have already visited this pred block with another
1137 // pointer. If so, we can't do this lookup. This failure can occur
1138 // with PHI translation when a critical edge exists and the PHI node in
1139 // the successor translates to a pointer value different than the
1140 // pointer the block was first analyzed with.
1141 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
1142 InsertRes = Visited.insert(std::make_pair(Pred, PredPtr));
Chris Lattner9e59c642008-12-15 03:35:32 +00001143
Chris Lattnere95035a2009-11-27 08:37:22 +00001144 if (!InsertRes.second) {
1145 // If the predecessor was visited with PredPtr, then we already did
1146 // the analysis and can ignore it.
1147 if (InsertRes.first->second == PredPtr)
1148 continue;
Chris Lattner9e59c642008-12-15 03:35:32 +00001149
Chris Lattnere95035a2009-11-27 08:37:22 +00001150 // Otherwise, the block was previously analyzed with a different
1151 // pointer. We can't represent the result of this case, so we just
1152 // treat this as a phi translation failure.
1153 goto PredTranslationFailure;
Chris Lattner9e59c642008-12-15 03:35:32 +00001154 }
Chris Lattner6f7b2102009-11-27 22:05:15 +00001155
1156 // If PHI translation was unable to find an available pointer in this
1157 // predecessor, then we have to assume that the pointer is clobbered in
1158 // that predecessor. We can still do PRE of the load, which would insert
1159 // a computation of the pointer in this predecessor.
1160 if (PredPtr == 0) {
Chris Lattner855d9da2009-12-01 07:33:32 +00001161 // Add the entry to the Result list.
1162 NonLocalDepEntry Entry(Pred,
1163 MemDepResult::getClobber(Pred->getTerminator()));
1164 Result.push_back(Entry);
1165
1166 // Add it to the cache for this CacheKey so that subsequent queries get
1167 // this result.
1168 Cache = &NonLocalPointerDeps[CacheKey].second;
1169 MemoryDependenceAnalysis::NonLocalDepInfo::iterator It =
1170 std::upper_bound(Cache->begin(), Cache->end(), Entry);
1171
1172 if (It != Cache->begin() && prior(It)->first == Pred)
1173 --It;
1174
1175 if (It == Cache->end() || It->first != Pred) {
1176 Cache->insert(It, Entry);
1177 // Add it to the reverse map.
1178 ReverseNonLocalPtrDeps[Pred->getTerminator()].insert(CacheKey);
1179 } else if (!It->second.isDirty()) {
1180 // noop
1181 } else if (It->second.getInst() == Pred->getTerminator()) {
1182 // Same instruction, clear the dirty marker.
1183 It->second = Entry.second;
1184 } else if (It->second.getInst() == 0) {
1185 // Dirty, with no instruction, just add this.
1186 It->second = Entry.second;
1187 ReverseNonLocalPtrDeps[Pred->getTerminator()].insert(CacheKey);
1188 } else {
1189 // Otherwise, dirty with a different instruction.
1190 RemoveFromReverseMap(ReverseNonLocalPtrDeps, It->second.getInst(),
1191 CacheKey);
1192 It->second = Entry.second;
1193 ReverseNonLocalPtrDeps[Pred->getTerminator()].insert(CacheKey);
1194 }
1195 Cache = 0;
Chris Lattner6f7b2102009-11-27 22:05:15 +00001196 continue;
Chris Lattner6f7b2102009-11-27 22:05:15 +00001197 }
Chris Lattnere95035a2009-11-27 08:37:22 +00001198
1199 // FIXME: it is entirely possible that PHI translating will end up with
1200 // the same value. Consider PHI translating something like:
1201 // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need*
1202 // to recurse here, pedantically speaking.
Chris Lattner6fbc1962009-07-13 17:14:23 +00001203
Chris Lattnere95035a2009-11-27 08:37:22 +00001204 // If we have a problem phi translating, fall through to the code below
1205 // to handle the failure condition.
1206 if (getNonLocalPointerDepFromBB(PredPtr, PointeeSize, isLoad, Pred,
1207 Result, Visited))
1208 goto PredTranslationFailure;
Chris Lattner9e59c642008-12-15 03:35:32 +00001209 }
Chris Lattnere95035a2009-11-27 08:37:22 +00001210
1211 // Refresh the CacheInfo/Cache pointer so that it isn't invalidated.
1212 CacheInfo = &NonLocalPointerDeps[CacheKey];
1213 Cache = &CacheInfo->second;
1214 NumSortedEntries = Cache->size();
1215
1216 // Since we did phi translation, the "Cache" set won't contain all of the
1217 // results for the query. This is ok (we can still use it to accelerate
1218 // specific block queries) but we can't do the fastpath "return all
1219 // results from the set" Clear out the indicator for this.
1220 CacheInfo->first = BBSkipFirstBlockPair();
1221 SkipFirstBlock = false;
1222 continue;
Chris Lattnerdc593112009-11-26 23:18:49 +00001223
Chris Lattner9e59c642008-12-15 03:35:32 +00001224 PredTranslationFailure:
1225
Chris Lattner95900f22009-01-23 07:12:16 +00001226 if (Cache == 0) {
1227 // Refresh the CacheInfo/Cache pointer if it got invalidated.
1228 CacheInfo = &NonLocalPointerDeps[CacheKey];
1229 Cache = &CacheInfo->second;
1230 NumSortedEntries = Cache->size();
Chris Lattner95900f22009-01-23 07:12:16 +00001231 }
Chris Lattner6fbc1962009-07-13 17:14:23 +00001232
Chris Lattner9e59c642008-12-15 03:35:32 +00001233 // Since we did phi translation, the "Cache" set won't contain all of the
1234 // results for the query. This is ok (we can still use it to accelerate
1235 // specific block queries) but we can't do the fastpath "return all
1236 // results from the set" Clear out the indicator for this.
1237 CacheInfo->first = BBSkipFirstBlockPair();
1238
1239 // If *nothing* works, mark the pointer as being clobbered by the first
1240 // instruction in this block.
1241 //
1242 // If this is the magic first block, return this as a clobber of the whole
1243 // incoming value. Since we can't phi translate to one of the predecessors,
1244 // we have to bail out.
1245 if (SkipFirstBlock)
1246 return true;
1247
1248 for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) {
1249 assert(I != Cache->rend() && "Didn't find current block??");
1250 if (I->first != BB)
1251 continue;
1252
1253 assert(I->second.isNonLocal() &&
1254 "Should only be here with transparent block");
1255 I->second = MemDepResult::getClobber(BB->begin());
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001256 ReverseNonLocalPtrDeps[BB->begin()].insert(CacheKey);
Chris Lattner9e59c642008-12-15 03:35:32 +00001257 Result.push_back(*I);
1258 break;
Chris Lattner9a193fd2008-12-07 02:56:57 +00001259 }
Chris Lattner7ebcf032008-12-07 02:15:47 +00001260 }
Chris Lattner95900f22009-01-23 07:12:16 +00001261
Chris Lattner9863c3f2008-12-09 07:47:11 +00001262 // Okay, we're done now. If we added new values to the cache, re-sort it.
Chris Lattnera2f55dd2009-07-13 17:20:05 +00001263 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner12a7db32009-01-22 07:04:01 +00001264 DEBUG(AssertSorted(*Cache));
Chris Lattner9e59c642008-12-15 03:35:32 +00001265 return false;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001266}
1267
1268/// RemoveCachedNonLocalPointerDependencies - If P exists in
1269/// CachedNonLocalPointerInfo, remove it.
1270void MemoryDependenceAnalysis::
1271RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) {
1272 CachedNonLocalPointerInfo::iterator It =
1273 NonLocalPointerDeps.find(P);
1274 if (It == NonLocalPointerDeps.end()) return;
1275
1276 // Remove all of the entries in the BB->val map. This involves removing
1277 // instructions from the reverse map.
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001278 NonLocalDepInfo &PInfo = It->second.second;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001279
1280 for (unsigned i = 0, e = PInfo.size(); i != e; ++i) {
1281 Instruction *Target = PInfo[i].second.getInst();
1282 if (Target == 0) continue; // Ignore non-local dep results.
Chris Lattner5a45bf12008-12-09 22:45:32 +00001283 assert(Target->getParent() == PInfo[i].first);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001284
1285 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001286 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001287 }
1288
1289 // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).
1290 NonLocalPointerDeps.erase(It);
Chris Lattner7ebcf032008-12-07 02:15:47 +00001291}
1292
1293
Chris Lattnerbc99be12008-12-09 22:06:23 +00001294/// invalidateCachedPointerInfo - This method is used to invalidate cached
1295/// information about the specified pointer, because it may be too
1296/// conservative in memdep. This is an optional call that can be used when
1297/// the client detects an equivalence between the pointer and some other
1298/// value and replaces the other value with ptr. This can make Ptr available
1299/// in more places that cached info does not necessarily keep.
1300void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) {
1301 // If Ptr isn't really a pointer, just ignore it.
1302 if (!isa<PointerType>(Ptr->getType())) return;
1303 // Flush store info for the pointer.
1304 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false));
1305 // Flush load info for the pointer.
1306 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true));
1307}
1308
Owen Anderson78e02f72007-07-06 23:14:35 +00001309/// removeInstruction - Remove an instruction from the dependence analysis,
1310/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +00001311/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +00001312void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +00001313 // Walk through the Non-local dependencies, removing this one as the value
1314 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +00001315 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
1316 if (NLDI != NonLocalDeps.end()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +00001317 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattner25f4b2b2008-11-30 02:30:50 +00001318 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
1319 DI != DE; ++DI)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +00001320 if (Instruction *Inst = DI->second.getInst())
Chris Lattnerd44745d2008-12-07 18:39:13 +00001321 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);
Chris Lattnerf68f3102008-11-30 02:28:25 +00001322 NonLocalDeps.erase(NLDI);
1323 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +00001324
Chris Lattner5f589dc2008-11-28 22:04:47 +00001325 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +00001326 //
Chris Lattner39f372e2008-11-29 01:43:36 +00001327 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
1328 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +00001329 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerd44745d2008-12-07 18:39:13 +00001330 if (Instruction *Inst = LocalDepEntry->second.getInst())
1331 RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);
Chris Lattner125ce362008-11-30 01:09:30 +00001332
Chris Lattnerbaad8882008-11-28 22:28:27 +00001333 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +00001334 LocalDeps.erase(LocalDepEntry);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001335 }
1336
1337 // If we have any cached pointer dependencies on this instruction, remove
1338 // them. If the instruction has non-pointer type, then it can't be a pointer
1339 // base.
1340
1341 // Remove it from both the load info and the store info. The instruction
1342 // can't be in either of these maps if it is non-pointer.
1343 if (isa<PointerType>(RemInst->getType())) {
1344 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));
1345 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));
1346 }
Chris Lattnerbaad8882008-11-28 22:28:27 +00001347
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001348 // Loop over all of the things that depend on the instruction we're removing.
1349 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001350 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
Chris Lattner0655f732008-12-07 18:42:51 +00001351
1352 // If we find RemInst as a clobber or Def in any of the maps for other values,
1353 // we need to replace its entry with a dirty version of the instruction after
1354 // it. If RemInst is a terminator, we use a null dirty value.
1355 //
1356 // Using a dirty version of the instruction after RemInst saves having to scan
1357 // the entire block to get to this point.
1358 MemDepResult NewDirtyVal;
1359 if (!RemInst->isTerminator())
1360 NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst));
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001361
Chris Lattner8c465272008-11-29 09:20:15 +00001362 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
1363 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001364 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001365 // RemInst can't be the terminator if it has local stuff depending on it.
Chris Lattner125ce362008-11-30 01:09:30 +00001366 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
1367 "Nothing can locally depend on a terminator");
1368
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001369 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
1370 E = ReverseDeps.end(); I != E; ++I) {
1371 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001372 assert(InstDependingOnRemInst != RemInst &&
1373 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +00001374
Chris Lattner0655f732008-12-07 18:42:51 +00001375 LocalDeps[InstDependingOnRemInst] = NewDirtyVal;
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001376
Chris Lattner125ce362008-11-30 01:09:30 +00001377 // Make sure to remember that new things depend on NewDepInst.
Chris Lattner0655f732008-12-07 18:42:51 +00001378 assert(NewDirtyVal.getInst() && "There is no way something else can have "
1379 "a local dep on this if it is a terminator!");
1380 ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(),
Chris Lattner125ce362008-11-30 01:09:30 +00001381 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001382 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001383
1384 ReverseLocalDeps.erase(ReverseDepIt);
1385
1386 // Add new reverse deps after scanning the set, to avoid invalidating the
1387 // 'ReverseDeps' reference.
1388 while (!ReverseDepsToAdd.empty()) {
1389 ReverseLocalDeps[ReverseDepsToAdd.back().first]
1390 .insert(ReverseDepsToAdd.back().second);
1391 ReverseDepsToAdd.pop_back();
1392 }
Owen Anderson78e02f72007-07-06 23:14:35 +00001393 }
Owen Anderson4d13de42007-08-16 21:27:05 +00001394
Chris Lattner8c465272008-11-29 09:20:15 +00001395 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
1396 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner6290f5c2008-12-07 08:50:20 +00001397 SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second;
1398 for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +00001399 I != E; ++I) {
1400 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
1401
Chris Lattner4a69bad2008-11-30 02:52:26 +00001402 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner4a69bad2008-11-30 02:52:26 +00001403 // The information is now dirty!
Chris Lattnerbf145d62008-12-01 01:15:42 +00001404 INLD.second = true;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001405
Chris Lattnerbf145d62008-12-01 01:15:42 +00001406 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
1407 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +00001408 if (DI->second.getInst() != RemInst) continue;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001409
1410 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner0655f732008-12-07 18:42:51 +00001411 DI->second = NewDirtyVal;
1412
1413 if (Instruction *NextI = NewDirtyVal.getInst())
Chris Lattnerf68f3102008-11-30 02:28:25 +00001414 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattnerf68f3102008-11-30 02:28:25 +00001415 }
1416 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001417
1418 ReverseNonLocalDeps.erase(ReverseDepIt);
1419
Chris Lattner0ec48dd2008-11-29 22:02:15 +00001420 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
1421 while (!ReverseDepsToAdd.empty()) {
1422 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
1423 .insert(ReverseDepsToAdd.back().second);
1424 ReverseDepsToAdd.pop_back();
1425 }
Owen Anderson4d13de42007-08-16 21:27:05 +00001426 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +00001427
Chris Lattner6290f5c2008-12-07 08:50:20 +00001428 // If the instruction is in ReverseNonLocalPtrDeps then it appears as a
1429 // value in the NonLocalPointerDeps info.
1430 ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =
1431 ReverseNonLocalPtrDeps.find(RemInst);
1432 if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001433 SmallPtrSet<ValueIsLoadPair, 4> &Set = ReversePtrDepIt->second;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001434 SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd;
1435
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001436 for (SmallPtrSet<ValueIsLoadPair, 4>::iterator I = Set.begin(),
1437 E = Set.end(); I != E; ++I) {
1438 ValueIsLoadPair P = *I;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001439 assert(P.getPointer() != RemInst &&
1440 "Already removed NonLocalPointerDeps info for RemInst");
1441
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001442 NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].second;
1443
1444 // The cache is not valid for any specific block anymore.
Chris Lattner9e59c642008-12-15 03:35:32 +00001445 NonLocalPointerDeps[P].first = BBSkipFirstBlockPair();
Chris Lattner6290f5c2008-12-07 08:50:20 +00001446
Chris Lattner6290f5c2008-12-07 08:50:20 +00001447 // Update any entries for RemInst to use the instruction after it.
1448 for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end();
1449 DI != DE; ++DI) {
1450 if (DI->second.getInst() != RemInst) continue;
1451
1452 // Convert to a dirty entry for the subsequent instruction.
1453 DI->second = NewDirtyVal;
1454
1455 if (Instruction *NewDirtyInst = NewDirtyVal.getInst())
1456 ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));
1457 }
Chris Lattner95900f22009-01-23 07:12:16 +00001458
1459 // Re-sort the NonLocalDepInfo. Changing the dirty entry to its
1460 // subsequent value may invalidate the sortedness.
1461 std::sort(NLPDI.begin(), NLPDI.end());
Chris Lattner6290f5c2008-12-07 08:50:20 +00001462 }
1463
1464 ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);
1465
1466 while (!ReversePtrDepsToAdd.empty()) {
1467 ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first]
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001468 .insert(ReversePtrDepsToAdd.back().second);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001469 ReversePtrDepsToAdd.pop_back();
1470 }
1471 }
1472
1473
Chris Lattnerf68f3102008-11-30 02:28:25 +00001474 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnerd777d402008-11-30 19:24:31 +00001475 AA->deleteValue(RemInst);
Chris Lattner5f589dc2008-11-28 22:04:47 +00001476 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +00001477}
Chris Lattner729b2372008-11-29 21:25:10 +00001478/// verifyRemoved - Verify that the specified instruction does not occur
1479/// in our internal data structures.
1480void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
1481 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
1482 E = LocalDeps.end(); I != E; ++I) {
1483 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +00001484 assert(I->second.getInst() != D &&
Chris Lattner729b2372008-11-29 21:25:10 +00001485 "Inst occurs in data structures");
1486 }
1487
Chris Lattner6290f5c2008-12-07 08:50:20 +00001488 for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(),
1489 E = NonLocalPointerDeps.end(); I != E; ++I) {
1490 assert(I->first.getPointer() != D && "Inst occurs in NLPD map key");
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001491 const NonLocalDepInfo &Val = I->second.second;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001492 for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end();
1493 II != E; ++II)
1494 assert(II->second.getInst() != D && "Inst occurs as NLPD value");
1495 }
1496
Chris Lattner729b2372008-11-29 21:25:10 +00001497 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
1498 E = NonLocalDeps.end(); I != E; ++I) {
1499 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4a69bad2008-11-30 02:52:26 +00001500 const PerInstNLInfo &INLD = I->second;
Chris Lattnerbf145d62008-12-01 01:15:42 +00001501 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
1502 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +00001503 assert(II->second.getInst() != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001504 }
1505
1506 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +00001507 E = ReverseLocalDeps.end(); I != E; ++I) {
1508 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001509 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1510 EE = I->second.end(); II != EE; ++II)
1511 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +00001512 }
Chris Lattner729b2372008-11-29 21:25:10 +00001513
1514 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
1515 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +00001516 I != E; ++I) {
1517 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001518 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1519 EE = I->second.end(); II != EE; ++II)
1520 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +00001521 }
Chris Lattner6290f5c2008-12-07 08:50:20 +00001522
1523 for (ReverseNonLocalPtrDepTy::const_iterator
1524 I = ReverseNonLocalPtrDeps.begin(),
1525 E = ReverseNonLocalPtrDeps.end(); I != E; ++I) {
1526 assert(I->first != D && "Inst occurs in rev NLPD map");
1527
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001528 for (SmallPtrSet<ValueIsLoadPair, 4>::const_iterator II = I->second.begin(),
Chris Lattner6290f5c2008-12-07 08:50:20 +00001529 E = I->second.end(); II != E; ++II)
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001530 assert(*II != ValueIsLoadPair(D, false) &&
1531 *II != ValueIsLoadPair(D, true) &&
Chris Lattner6290f5c2008-12-07 08:50:20 +00001532 "Inst occurs in ReverseNonLocalPtrDeps map");
1533 }
1534
Chris Lattner729b2372008-11-29 21:25:10 +00001535}