blob: c8c4b4c6fcbadc7f62d02f7ee5e68151ae3f5418 [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"
Dan Gohman5034dd32010-12-15 20:02:24 +000028#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerbaad8882008-11-28 22:28:27 +000029#include "llvm/ADT/Statistic.h"
Duncan Sands7050f3d2008-12-10 09:38:36 +000030#include "llvm/ADT/STLExtras.h"
Chris Lattner4012fdd2008-12-09 06:28:49 +000031#include "llvm/Support/PredIteratorCache.h"
Chris Lattner0e575f42008-11-28 21:45:17 +000032#include "llvm/Support/Debug.h"
Benjamin Kramerdd061b22010-11-21 15:21:46 +000033#include "llvm/Target/TargetData.h"
Owen Anderson78e02f72007-07-06 23:14:35 +000034using namespace llvm;
35
Chris Lattnerbf145d62008-12-01 01:15:42 +000036STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
37STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
Chris Lattner0ec48dd2008-11-29 22:02:15 +000038STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Chris Lattner6290f5c2008-12-07 08:50:20 +000039
40STATISTIC(NumCacheNonLocalPtr,
41 "Number of fully cached non-local ptr responses");
42STATISTIC(NumCacheDirtyNonLocalPtr,
43 "Number of cached, but dirty, non-local ptr responses");
44STATISTIC(NumUncacheNonLocalPtr,
45 "Number of uncached non-local ptr responses");
Chris Lattner11dcd8d2008-12-08 07:31:50 +000046STATISTIC(NumCacheCompleteNonLocalPtr,
47 "Number of block queries that were completely cached");
Chris Lattner6290f5c2008-12-07 08:50:20 +000048
Owen Anderson78e02f72007-07-06 23:14:35 +000049char MemoryDependenceAnalysis::ID = 0;
50
Owen Anderson78e02f72007-07-06 23:14:35 +000051// Register this pass...
Owen Anderson2ab36d32010-10-12 19:48:12 +000052INITIALIZE_PASS_BEGIN(MemoryDependenceAnalysis, "memdep",
Owen Andersonce665bd2010-10-07 22:25:06 +000053 "Memory Dependence Analysis", false, true)
Owen Anderson2ab36d32010-10-12 19:48:12 +000054INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
55INITIALIZE_PASS_END(MemoryDependenceAnalysis, "memdep",
56 "Memory Dependence Analysis", false, true)
Owen Anderson78e02f72007-07-06 23:14:35 +000057
Chris Lattner4012fdd2008-12-09 06:28:49 +000058MemoryDependenceAnalysis::MemoryDependenceAnalysis()
Owen Anderson90c579d2010-08-06 18:33:48 +000059: FunctionPass(ID), PredCache(0) {
Owen Anderson081c34b2010-10-19 17:21:58 +000060 initializeMemoryDependenceAnalysisPass(*PassRegistry::getPassRegistry());
Chris Lattner4012fdd2008-12-09 06:28:49 +000061}
62MemoryDependenceAnalysis::~MemoryDependenceAnalysis() {
63}
64
65/// Clean up memory in between runs
66void MemoryDependenceAnalysis::releaseMemory() {
67 LocalDeps.clear();
68 NonLocalDeps.clear();
69 NonLocalPointerDeps.clear();
70 ReverseLocalDeps.clear();
71 ReverseNonLocalDeps.clear();
72 ReverseNonLocalPtrDeps.clear();
73 PredCache->clear();
74}
75
76
77
Owen Anderson78e02f72007-07-06 23:14:35 +000078/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
79///
80void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
81 AU.setPreservesAll();
82 AU.addRequiredTransitive<AliasAnalysis>();
Owen Anderson78e02f72007-07-06 23:14:35 +000083}
84
Chris Lattnerd777d402008-11-30 19:24:31 +000085bool MemoryDependenceAnalysis::runOnFunction(Function &) {
86 AA = &getAnalysis<AliasAnalysis>();
Benjamin Kramerdd061b22010-11-21 15:21:46 +000087 TD = getAnalysisIfAvailable<TargetData>();
Chris Lattner4012fdd2008-12-09 06:28:49 +000088 if (PredCache == 0)
89 PredCache.reset(new PredIteratorCache());
Chris Lattnerd777d402008-11-30 19:24:31 +000090 return false;
91}
92
Chris Lattnerd44745d2008-12-07 18:39:13 +000093/// RemoveFromReverseMap - This is a helper function that removes Val from
94/// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry.
95template <typename KeyTy>
96static void RemoveFromReverseMap(DenseMap<Instruction*,
Chris Lattner6a0dcc12009-03-29 00:24:04 +000097 SmallPtrSet<KeyTy, 4> > &ReverseMap,
98 Instruction *Inst, KeyTy Val) {
99 typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator
Chris Lattnerd44745d2008-12-07 18:39:13 +0000100 InstIt = ReverseMap.find(Inst);
101 assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");
102 bool Found = InstIt->second.erase(Val);
Jeffrey Yasskin8e68c382010-12-23 00:58:24 +0000103 assert(Found && "Invalid reverse map!"); (void)Found;
Chris Lattnerd44745d2008-12-07 18:39:13 +0000104 if (InstIt->second.empty())
105 ReverseMap.erase(InstIt);
106}
107
Dan Gohman533c2ad2010-11-10 21:51:35 +0000108/// GetLocation - If the given instruction references a specific memory
109/// location, fill in Loc with the details, otherwise set Loc.Ptr to null.
110/// Return a ModRefInfo value describing the general behavior of the
111/// instruction.
112static
113AliasAnalysis::ModRefResult GetLocation(const Instruction *Inst,
114 AliasAnalysis::Location &Loc,
115 AliasAnalysis *AA) {
116 if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
117 if (LI->isVolatile()) {
118 Loc = AliasAnalysis::Location();
119 return AliasAnalysis::ModRef;
120 }
Dan Gohman6d8eb152010-11-11 21:50:19 +0000121 Loc = AA->getLocation(LI);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000122 return AliasAnalysis::Ref;
123 }
124
125 if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
126 if (SI->isVolatile()) {
127 Loc = AliasAnalysis::Location();
128 return AliasAnalysis::ModRef;
129 }
Dan Gohman6d8eb152010-11-11 21:50:19 +0000130 Loc = AA->getLocation(SI);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000131 return AliasAnalysis::Mod;
132 }
133
134 if (const VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000135 Loc = AA->getLocation(V);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000136 return AliasAnalysis::ModRef;
137 }
138
139 if (const CallInst *CI = isFreeCall(Inst)) {
140 // calls to free() deallocate the entire structure
141 Loc = AliasAnalysis::Location(CI->getArgOperand(0));
142 return AliasAnalysis::Mod;
143 }
144
145 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
146 switch (II->getIntrinsicID()) {
147 case Intrinsic::lifetime_start:
148 case Intrinsic::lifetime_end:
149 case Intrinsic::invariant_start:
150 Loc = AliasAnalysis::Location(II->getArgOperand(1),
151 cast<ConstantInt>(II->getArgOperand(0))
152 ->getZExtValue(),
153 II->getMetadata(LLVMContext::MD_tbaa));
154 // These intrinsics don't really modify the memory, but returning Mod
155 // will allow them to be handled conservatively.
156 return AliasAnalysis::Mod;
157 case Intrinsic::invariant_end:
158 Loc = AliasAnalysis::Location(II->getArgOperand(2),
159 cast<ConstantInt>(II->getArgOperand(1))
160 ->getZExtValue(),
161 II->getMetadata(LLVMContext::MD_tbaa));
162 // These intrinsics don't really modify the memory, but returning Mod
163 // will allow them to be handled conservatively.
164 return AliasAnalysis::Mod;
165 default:
166 break;
167 }
168
169 // Otherwise, just do the coarse-grained thing that always works.
170 if (Inst->mayWriteToMemory())
171 return AliasAnalysis::ModRef;
172 if (Inst->mayReadFromMemory())
173 return AliasAnalysis::Ref;
174 return AliasAnalysis::NoModRef;
175}
Chris Lattnerbf145d62008-12-01 01:15:42 +0000176
Chris Lattner8ef57c52008-12-07 00:35:51 +0000177/// getCallSiteDependencyFrom - Private helper for finding the local
178/// dependencies of a call site.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000179MemDepResult MemoryDependenceAnalysis::
Chris Lattner20d6f092008-12-09 21:19:42 +0000180getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall,
181 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Owen Anderson642a9e32007-08-08 22:26:03 +0000182 // Walk backwards through the block, looking for dependencies
Chris Lattner5391a1d2008-11-29 03:47:00 +0000183 while (ScanIt != BB->begin()) {
184 Instruction *Inst = --ScanIt;
Owen Anderson5f323202007-07-10 17:59:22 +0000185
186 // If this inst is a memory op, get the pointer it accessed
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000187 AliasAnalysis::Location Loc;
Dan Gohman533c2ad2010-11-10 21:51:35 +0000188 AliasAnalysis::ModRefResult MR = GetLocation(Inst, Loc, AA);
189 if (Loc.Ptr) {
190 // A simple instruction.
191 if (AA->getModRefInfo(CS, Loc) != AliasAnalysis::NoModRef)
192 return MemDepResult::getClobber(Inst);
193 continue;
194 }
195
196 if (CallSite InstCS = cast<Value>(Inst)) {
Owen Andersonf6cec852009-03-09 05:12:38 +0000197 // Debug intrinsics don't cause dependences.
Dale Johannesen497cb6f2009-03-11 21:13:01 +0000198 if (isa<DbgInfoIntrinsic>(Inst)) continue;
Chris Lattnerb51deb92008-12-05 21:04:20 +0000199 // If these two calls do not interfere, look past it.
Chris Lattner20d6f092008-12-09 21:19:42 +0000200 switch (AA->getModRefInfo(CS, InstCS)) {
201 case AliasAnalysis::NoModRef:
Dan Gohman5fa417c2010-08-05 22:09:15 +0000202 // If the two calls are the same, return InstCS as a Def, so that
203 // CS can be found redundant and eliminated.
Dan Gohman533c2ad2010-11-10 21:51:35 +0000204 if (isReadOnlyCall && !(MR & AliasAnalysis::Mod) &&
Dan Gohman5fa417c2010-08-05 22:09:15 +0000205 CS.getInstruction()->isIdenticalToWhenDefined(Inst))
206 return MemDepResult::getDef(Inst);
207
208 // Otherwise if the two calls don't interact (e.g. InstCS is readnone)
209 // keep scanning.
Dan Gohman533c2ad2010-11-10 21:51:35 +0000210 break;
Chris Lattner20d6f092008-12-09 21:19:42 +0000211 default:
Chris Lattnerb51deb92008-12-05 21:04:20 +0000212 return MemDepResult::getClobber(Inst);
Chris Lattner20d6f092008-12-09 21:19:42 +0000213 }
Chris Lattnercfbb6342008-11-30 01:44:00 +0000214 }
Owen Anderson5f323202007-07-10 17:59:22 +0000215 }
216
Chris Lattner7ebcf032008-12-07 02:15:47 +0000217 // No dependence found. If this is the entry block of the function, it is a
218 // clobber, otherwise it is non-local.
219 if (BB != &BB->getParent()->getEntryBlock())
220 return MemDepResult::getNonLocal();
221 return MemDepResult::getClobber(ScanIt);
Owen Anderson5f323202007-07-10 17:59:22 +0000222}
223
Chris Lattnere79be942008-12-07 01:50:16 +0000224/// getPointerDependencyFrom - Return the instruction on which a memory
Dan Gohmancd5c1232010-10-29 01:14:04 +0000225/// location depends. If isLoad is true, this routine ignores may-aliases with
226/// read-only operations. If isLoad is false, this routine ignores may-aliases
227/// with reads from read-only locations.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000228MemDepResult MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000229getPointerDependencyFrom(const AliasAnalysis::Location &MemLoc, bool isLoad,
Chris Lattnere79be942008-12-07 01:50:16 +0000230 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000231
Chris Lattner1e8de492009-12-01 21:16:01 +0000232 Value *InvariantTag = 0;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000233
Chris Lattner6290f5c2008-12-07 08:50:20 +0000234 // Walk backwards through the basic block, looking for dependencies.
Chris Lattner5391a1d2008-11-29 03:47:00 +0000235 while (ScanIt != BB->begin()) {
236 Instruction *Inst = --ScanIt;
Chris Lattnera161ab02008-11-29 09:09:48 +0000237
Owen Anderson4bc737c2009-10-28 06:18:42 +0000238 // If we're in an invariant region, no dependencies can be found before
239 // we pass an invariant-begin marker.
Chris Lattner1e8de492009-12-01 21:16:01 +0000240 if (InvariantTag == Inst) {
241 InvariantTag = 0;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000242 continue;
Chris Lattner1ffb70f2009-12-01 21:15:15 +0000243 }
244
245 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
Chris Lattner09981982010-09-06 03:58:04 +0000246 // Debug intrinsics don't (and can't) cause dependences.
Chris Lattnerc5a5cf22010-09-06 01:26:29 +0000247 if (isa<DbgInfoIntrinsic>(II)) continue;
Owen Anderson9ff5a232009-12-02 07:35:19 +0000248
Owen Andersonb62f7922009-10-28 07:05:35 +0000249 // If we pass an invariant-end marker, then we've just entered an
250 // invariant region and can start ignoring dependencies.
Owen Anderson4bc737c2009-10-28 06:18:42 +0000251 if (II->getIntrinsicID() == Intrinsic::invariant_end) {
Owen Anderson9ff5a232009-12-02 07:35:19 +0000252 // FIXME: This only considers queries directly on the invariant-tagged
253 // pointer, not on query pointers that are indexed off of them. It'd
254 // be nice to handle that at some point.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000255 AliasAnalysis::AliasResult R =
256 AA->alias(AliasAnalysis::Location(II->getArgOperand(2)), MemLoc);
Chris Lattner09981982010-09-06 03:58:04 +0000257 if (R == AliasAnalysis::MustAlias)
Gabor Greif8ff72b52010-06-23 22:48:06 +0000258 InvariantTag = II->getArgOperand(0);
Chris Lattner09981982010-09-06 03:58:04 +0000259
260 continue;
261 }
262
Owen Andersonb62f7922009-10-28 07:05:35 +0000263 // If we reach a lifetime begin or end marker, then the query ends here
264 // because the value is undefined.
Chris Lattner09981982010-09-06 03:58:04 +0000265 if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
Owen Anderson9ff5a232009-12-02 07:35:19 +0000266 // FIXME: This only considers queries directly on the invariant-tagged
267 // pointer, not on query pointers that are indexed off of them. It'd
268 // be nice to handle that at some point.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000269 AliasAnalysis::AliasResult R =
270 AA->alias(AliasAnalysis::Location(II->getArgOperand(1)), MemLoc);
Owen Andersonb62f7922009-10-28 07:05:35 +0000271 if (R == AliasAnalysis::MustAlias)
272 return MemDepResult::getDef(II);
Chris Lattner09981982010-09-06 03:58:04 +0000273 continue;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000274 }
275 }
276
277 // If we're querying on a load and we're in an invariant region, we're done
278 // at this point. Nothing a load depends on can live in an invariant region.
Chris Lattner09981982010-09-06 03:58:04 +0000279 //
280 // FIXME: this will prevent us from returning load/load must-aliases, so GVN
281 // won't remove redundant loads.
Chris Lattner1e8de492009-12-01 21:16:01 +0000282 if (isLoad && InvariantTag) continue;
Owen Anderson4bc737c2009-10-28 06:18:42 +0000283
Chris Lattnercfbb6342008-11-30 01:44:00 +0000284 // Values depend on loads if the pointers are must aliased. This means that
285 // a load depends on another must aliased load from the same value.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000286 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000287 AliasAnalysis::Location LoadLoc = AA->getLocation(LI);
Chris Lattnerb51deb92008-12-05 21:04:20 +0000288
289 // If we found a pointer, check if it could be the same as our pointer.
Dan Gohmancd5c1232010-10-29 01:14:04 +0000290 AliasAnalysis::AliasResult R = AA->alias(LoadLoc, MemLoc);
Chris Lattnera161ab02008-11-29 09:09:48 +0000291 if (R == AliasAnalysis::NoAlias)
292 continue;
293
Chris Lattner1f821512011-04-26 01:21:15 +0000294 if (isLoad) {
295 // Must aliased loads are defs of each other.
296 if (R == AliasAnalysis::MustAlias)
297 return MemDepResult::getDef(Inst);
298
299 // If we have a partial alias, then return this as a clobber for the
300 // client to handle.
301 if (R == AliasAnalysis::PartialAlias)
302 return MemDepResult::getClobber(Inst);
303
304 // Random may-alias loads don't depend on each other without a
305 // dependence.
Chris Lattnera161ab02008-11-29 09:09:48 +0000306 continue;
Chris Lattner1f821512011-04-26 01:21:15 +0000307 }
Dan Gohmancd5c1232010-10-29 01:14:04 +0000308
309 // Stores don't alias loads from read-only memory.
Chris Lattner1f821512011-04-26 01:21:15 +0000310 if (AA->pointsToConstantMemory(LoadLoc))
Dan Gohmancd5c1232010-10-29 01:14:04 +0000311 continue;
312
Chris Lattner1f821512011-04-26 01:21:15 +0000313 // Stores depend on may/must aliased loads.
Chris Lattnerb51deb92008-12-05 21:04:20 +0000314 return MemDepResult::getDef(Inst);
315 }
316
317 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Owen Andersona85a6642009-10-28 06:30:52 +0000318 // There can't be stores to the value we care about inside an
319 // invariant region.
Chris Lattner1e8de492009-12-01 21:16:01 +0000320 if (InvariantTag) continue;
Owen Andersona85a6642009-10-28 06:30:52 +0000321
Chris Lattnerab9cf122009-05-25 21:28:56 +0000322 // If alias analysis can tell that this store is guaranteed to not modify
323 // the query pointer, ignore it. Use getModRefInfo to handle cases where
324 // the query pointer points to constant memory etc.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000325 if (AA->getModRefInfo(SI, MemLoc) == AliasAnalysis::NoModRef)
Chris Lattnerab9cf122009-05-25 21:28:56 +0000326 continue;
327
328 // Ok, this store might clobber the query pointer. Check to see if it is
329 // a must alias: in this case, we want to return this as a def.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000330 AliasAnalysis::Location StoreLoc = AA->getLocation(SI);
Chris Lattnerab9cf122009-05-25 21:28:56 +0000331
Chris Lattnerb51deb92008-12-05 21:04:20 +0000332 // If we found a pointer, check if it could be the same as our pointer.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000333 AliasAnalysis::AliasResult R = AA->alias(StoreLoc, MemLoc);
Chris Lattnerb51deb92008-12-05 21:04:20 +0000334
335 if (R == AliasAnalysis::NoAlias)
336 continue;
Dan Gohman2cd19522010-12-13 22:47:57 +0000337 if (R == AliasAnalysis::MustAlias)
338 return MemDepResult::getDef(Inst);
339 return MemDepResult::getClobber(Inst);
Owen Anderson78e02f72007-07-06 23:14:35 +0000340 }
Chris Lattner237a8282008-11-30 01:39:32 +0000341
342 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattnerb51deb92008-12-05 21:04:20 +0000343 // the allocation, return Def. This means that there is no dependence and
Chris Lattner237a8282008-11-30 01:39:32 +0000344 // the access can be optimized based on that. For example, a load could
345 // turn into undef.
Victor Hernandez5c787362009-10-13 01:42:53 +0000346 // Note: Only determine this to be a malloc if Inst is the malloc call, not
347 // a subsequent bitcast of the malloc call result. There can be stores to
348 // the malloced memory between the malloc call and its bitcast uses, and we
349 // need to continue scanning until the malloc call.
Chris Lattner9b96eca2009-12-22 01:00:32 +0000350 if (isa<AllocaInst>(Inst) ||
351 (isa<CallInst>(Inst) && extractMallocCall(Inst))) {
Dan Gohmanbd1801b2011-01-24 18:53:32 +0000352 const Value *AccessPtr = GetUnderlyingObject(MemLoc.Ptr, TD);
Victor Hernandez46e83122009-09-18 21:34:51 +0000353
354 if (AccessPtr == Inst ||
355 AA->alias(Inst, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
356 return MemDepResult::getDef(Inst);
357 continue;
358 }
359
Chris Lattnerb51deb92008-12-05 21:04:20 +0000360 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000361 switch (AA->getModRefInfo(Inst, MemLoc)) {
Chris Lattner3579e442008-12-09 19:47:40 +0000362 case AliasAnalysis::NoModRef:
363 // If the call has no effect on the queried pointer, just ignore it.
Chris Lattner25a08142008-11-29 08:51:16 +0000364 continue;
Owen Andersona85a6642009-10-28 06:30:52 +0000365 case AliasAnalysis::Mod:
366 // If we're in an invariant region, we can ignore calls that ONLY
367 // modify the pointer.
Chris Lattner1e8de492009-12-01 21:16:01 +0000368 if (InvariantTag) continue;
Owen Andersona85a6642009-10-28 06:30:52 +0000369 return MemDepResult::getClobber(Inst);
Chris Lattner3579e442008-12-09 19:47:40 +0000370 case AliasAnalysis::Ref:
371 // If the call is known to never store to the pointer, and if this is a
372 // load query, we can safely ignore it (scan past it).
373 if (isLoad)
374 continue;
Chris Lattner3579e442008-12-09 19:47:40 +0000375 default:
376 // Otherwise, there is a potential dependence. Return a clobber.
377 return MemDepResult::getClobber(Inst);
378 }
Owen Anderson78e02f72007-07-06 23:14:35 +0000379 }
380
Chris Lattner7ebcf032008-12-07 02:15:47 +0000381 // No dependence found. If this is the entry block of the function, it is a
382 // clobber, otherwise it is non-local.
383 if (BB != &BB->getParent()->getEntryBlock())
384 return MemDepResult::getNonLocal();
385 return MemDepResult::getClobber(ScanIt);
Owen Anderson78e02f72007-07-06 23:14:35 +0000386}
387
Chris Lattner5391a1d2008-11-29 03:47:00 +0000388/// getDependency - Return the instruction on which a memory operation
389/// depends.
390MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
391 Instruction *ScanPos = QueryInst;
392
393 // Check for a cached result
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000394 MemDepResult &LocalCache = LocalDeps[QueryInst];
Chris Lattner5391a1d2008-11-29 03:47:00 +0000395
Chris Lattner0ec48dd2008-11-29 22:02:15 +0000396 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000397 // on MemDepResult's default constructing to 'dirty'.
398 if (!LocalCache.isDirty())
399 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000400
401 // Otherwise, if we have a dirty entry, we know we can start the scan at that
402 // instruction, which may save us some work.
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000403 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattner5391a1d2008-11-29 03:47:00 +0000404 ScanPos = Inst;
Chris Lattner4a69bad2008-11-30 02:52:26 +0000405
Chris Lattnerd44745d2008-12-07 18:39:13 +0000406 RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);
Chris Lattner4a69bad2008-11-30 02:52:26 +0000407 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000408
Chris Lattnere79be942008-12-07 01:50:16 +0000409 BasicBlock *QueryParent = QueryInst->getParent();
410
Chris Lattner5391a1d2008-11-29 03:47:00 +0000411 // Do the scan.
Chris Lattnere79be942008-12-07 01:50:16 +0000412 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
Chris Lattner7ebcf032008-12-07 02:15:47 +0000413 // No dependence found. If this is the entry block of the function, it is a
414 // clobber, otherwise it is non-local.
415 if (QueryParent != &QueryParent->getParent()->getEntryBlock())
416 LocalCache = MemDepResult::getNonLocal();
417 else
418 LocalCache = MemDepResult::getClobber(QueryInst);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000419 } else {
420 AliasAnalysis::Location MemLoc;
421 AliasAnalysis::ModRefResult MR = GetLocation(QueryInst, MemLoc, AA);
422 if (MemLoc.Ptr) {
423 // If we can do a pointer scan, make it happen.
424 bool isLoad = !(MR & AliasAnalysis::Mod);
Chris Lattner12bf43b2010-11-30 01:56:13 +0000425 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(QueryInst))
Dan Gohman533c2ad2010-11-10 21:51:35 +0000426 isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_end;
Chris Lattnerf6f1f062010-11-21 07:34:32 +0000427
Dan Gohman533c2ad2010-11-10 21:51:35 +0000428 LocalCache = getPointerDependencyFrom(MemLoc, isLoad, ScanPos,
429 QueryParent);
430 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Gabor Greif622b7cf2010-07-27 22:02:00 +0000431 CallSite QueryCS(QueryInst);
Nick Lewycky93d33112009-12-05 06:37:24 +0000432 bool isReadOnly = AA->onlyReadsMemory(QueryCS);
433 LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos,
434 QueryParent);
Dan Gohman533c2ad2010-11-10 21:51:35 +0000435 } else
436 // Non-memory instruction.
437 LocalCache = MemDepResult::getClobber(--BasicBlock::iterator(ScanPos));
Nick Lewyckyd801c102009-11-28 21:27:49 +0000438 }
Chris Lattner5391a1d2008-11-29 03:47:00 +0000439
440 // Remember the result!
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000441 if (Instruction *I = LocalCache.getInst())
Chris Lattner8c465272008-11-29 09:20:15 +0000442 ReverseLocalDeps[I].insert(QueryInst);
Chris Lattner5391a1d2008-11-29 03:47:00 +0000443
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +0000444 return LocalCache;
Chris Lattner5391a1d2008-11-29 03:47:00 +0000445}
446
Chris Lattner12a7db32009-01-22 07:04:01 +0000447#ifndef NDEBUG
448/// AssertSorted - This method is used when -debug is specified to verify that
449/// cache arrays are properly kept sorted.
450static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
451 int Count = -1) {
452 if (Count == -1) Count = Cache.size();
453 if (Count == 0) return;
454
455 for (unsigned i = 1; i != unsigned(Count); ++i)
Chris Lattnere18b9712009-12-09 07:08:01 +0000456 assert(!(Cache[i] < Cache[i-1]) && "Cache isn't sorted!");
Chris Lattner12a7db32009-01-22 07:04:01 +0000457}
458#endif
459
Chris Lattner1559b362008-12-09 19:38:05 +0000460/// getNonLocalCallDependency - Perform a full dependency query for the
461/// specified call, returning the set of blocks that the value is
Chris Lattner37d041c2008-11-30 01:18:27 +0000462/// potentially live across. The returned set of results will include a
463/// "NonLocal" result for all blocks where the value is live across.
464///
Chris Lattner1559b362008-12-09 19:38:05 +0000465/// This method assumes the instruction returns a "NonLocal" dependency
Chris Lattner37d041c2008-11-30 01:18:27 +0000466/// within its own block.
467///
Chris Lattner1559b362008-12-09 19:38:05 +0000468/// This returns a reference to an internal data structure that may be
469/// invalidated on the next non-local query or when an instruction is
470/// removed. Clients must copy this data if they want it around longer than
471/// that.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000472const MemoryDependenceAnalysis::NonLocalDepInfo &
Chris Lattner1559b362008-12-09 19:38:05 +0000473MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) {
474 assert(getDependency(QueryCS.getInstruction()).isNonLocal() &&
475 "getNonLocalCallDependency should only be used on calls with non-local deps!");
476 PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()];
Chris Lattnerbf145d62008-12-01 01:15:42 +0000477 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner37d041c2008-11-30 01:18:27 +0000478
479 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
480 /// the cached case, this can happen due to instructions being deleted etc. In
481 /// the uncached case, this starts out as the set of predecessors we care
482 /// about.
483 SmallVector<BasicBlock*, 32> DirtyBlocks;
484
485 if (!Cache.empty()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000486 // Okay, we have a cache entry. If we know it is not dirty, just return it
487 // with no computation.
488 if (!CacheP.second) {
Dan Gohmanfe601042010-06-22 15:08:57 +0000489 ++NumCacheNonLocal;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000490 return Cache;
491 }
492
Chris Lattner37d041c2008-11-30 01:18:27 +0000493 // If we already have a partially computed set of results, scan them to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000494 // determine what is dirty, seeding our initial DirtyBlocks worklist.
495 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
496 I != E; ++I)
Chris Lattnere18b9712009-12-09 07:08:01 +0000497 if (I->getResult().isDirty())
498 DirtyBlocks.push_back(I->getBB());
Chris Lattner37d041c2008-11-30 01:18:27 +0000499
Chris Lattnerbf145d62008-12-01 01:15:42 +0000500 // Sort the cache so that we can do fast binary search lookups below.
501 std::sort(Cache.begin(), Cache.end());
Chris Lattner37d041c2008-11-30 01:18:27 +0000502
Chris Lattnerbf145d62008-12-01 01:15:42 +0000503 ++NumCacheDirtyNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000504 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
505 // << Cache.size() << " cached: " << *QueryInst;
506 } else {
507 // Seed DirtyBlocks with each of the preds of QueryInst's block.
Chris Lattner1559b362008-12-09 19:38:05 +0000508 BasicBlock *QueryBB = QueryCS.getInstruction()->getParent();
Chris Lattner511b36c2008-12-09 06:44:17 +0000509 for (BasicBlock **PI = PredCache->GetPreds(QueryBB); *PI; ++PI)
510 DirtyBlocks.push_back(*PI);
Dan Gohmanfe601042010-06-22 15:08:57 +0000511 ++NumUncacheNonLocal;
Chris Lattner37d041c2008-11-30 01:18:27 +0000512 }
513
Chris Lattner20d6f092008-12-09 21:19:42 +0000514 // isReadonlyCall - If this is a read-only call, we can be more aggressive.
515 bool isReadonlyCall = AA->onlyReadsMemory(QueryCS);
Chris Lattner9e59c642008-12-15 03:35:32 +0000516
Chris Lattnerbf145d62008-12-01 01:15:42 +0000517 SmallPtrSet<BasicBlock*, 64> Visited;
518
519 unsigned NumSortedEntries = Cache.size();
Chris Lattner12a7db32009-01-22 07:04:01 +0000520 DEBUG(AssertSorted(Cache));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000521
Chris Lattner37d041c2008-11-30 01:18:27 +0000522 // Iterate while we still have blocks to update.
523 while (!DirtyBlocks.empty()) {
524 BasicBlock *DirtyBB = DirtyBlocks.back();
525 DirtyBlocks.pop_back();
526
Chris Lattnerbf145d62008-12-01 01:15:42 +0000527 // Already processed this block?
528 if (!Visited.insert(DirtyBB))
529 continue;
Chris Lattner37d041c2008-11-30 01:18:27 +0000530
Chris Lattnerbf145d62008-12-01 01:15:42 +0000531 // Do a binary search to see if we already have an entry for this block in
532 // the cache set. If so, find it.
Chris Lattner12a7db32009-01-22 07:04:01 +0000533 DEBUG(AssertSorted(Cache, NumSortedEntries));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000534 NonLocalDepInfo::iterator Entry =
535 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
Chris Lattnerdad451c2009-12-09 07:31:04 +0000536 NonLocalDepEntry(DirtyBB));
Chris Lattnere18b9712009-12-09 07:08:01 +0000537 if (Entry != Cache.begin() && prior(Entry)->getBB() == DirtyBB)
Chris Lattnerbf145d62008-12-01 01:15:42 +0000538 --Entry;
539
Chris Lattnere18b9712009-12-09 07:08:01 +0000540 NonLocalDepEntry *ExistingResult = 0;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000541 if (Entry != Cache.begin()+NumSortedEntries &&
Chris Lattnere18b9712009-12-09 07:08:01 +0000542 Entry->getBB() == DirtyBB) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000543 // If we already have an entry, and if it isn't already dirty, the block
544 // is done.
Chris Lattnere18b9712009-12-09 07:08:01 +0000545 if (!Entry->getResult().isDirty())
Chris Lattnerbf145d62008-12-01 01:15:42 +0000546 continue;
547
548 // Otherwise, remember this slot so we can update the value.
Chris Lattnere18b9712009-12-09 07:08:01 +0000549 ExistingResult = &*Entry;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000550 }
551
Chris Lattner37d041c2008-11-30 01:18:27 +0000552 // If the dirty entry has a pointer, start scanning from it so we don't have
553 // to rescan the entire block.
554 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattnerbf145d62008-12-01 01:15:42 +0000555 if (ExistingResult) {
Chris Lattnere18b9712009-12-09 07:08:01 +0000556 if (Instruction *Inst = ExistingResult->getResult().getInst()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +0000557 ScanPos = Inst;
Chris Lattnerbf145d62008-12-01 01:15:42 +0000558 // We're removing QueryInst's use of Inst.
Chris Lattner1559b362008-12-09 19:38:05 +0000559 RemoveFromReverseMap(ReverseNonLocalDeps, Inst,
560 QueryCS.getInstruction());
Chris Lattnerbf145d62008-12-01 01:15:42 +0000561 }
Chris Lattnerf68f3102008-11-30 02:28:25 +0000562 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000563
Chris Lattner73ec3cd2008-11-30 01:26:32 +0000564 // Find out if this block has a local dependency for QueryInst.
Chris Lattnerd8dd9342008-12-07 01:21:14 +0000565 MemDepResult Dep;
Chris Lattnere79be942008-12-07 01:50:16 +0000566
Chris Lattner1559b362008-12-09 19:38:05 +0000567 if (ScanPos != DirtyBB->begin()) {
Chris Lattner20d6f092008-12-09 21:19:42 +0000568 Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB);
Chris Lattner1559b362008-12-09 19:38:05 +0000569 } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {
570 // No dependence found. If this is the entry block of the function, it is
571 // a clobber, otherwise it is non-local.
572 Dep = MemDepResult::getNonLocal();
Chris Lattnere79be942008-12-07 01:50:16 +0000573 } else {
Chris Lattner1559b362008-12-09 19:38:05 +0000574 Dep = MemDepResult::getClobber(ScanPos);
Chris Lattnere79be942008-12-07 01:50:16 +0000575 }
576
Chris Lattnerbf145d62008-12-01 01:15:42 +0000577 // If we had a dirty entry for the block, update it. Otherwise, just add
578 // a new entry.
579 if (ExistingResult)
Chris Lattner0ee443d2009-12-22 04:25:02 +0000580 ExistingResult->setResult(Dep);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000581 else
Chris Lattner0ee443d2009-12-22 04:25:02 +0000582 Cache.push_back(NonLocalDepEntry(DirtyBB, Dep));
Chris Lattnerbf145d62008-12-01 01:15:42 +0000583
Chris Lattner37d041c2008-11-30 01:18:27 +0000584 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattnerbf145d62008-12-01 01:15:42 +0000585 // the value), remember the association!
586 if (!Dep.isNonLocal()) {
Chris Lattner37d041c2008-11-30 01:18:27 +0000587 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
588 // update this when we remove instructions.
Chris Lattnerbf145d62008-12-01 01:15:42 +0000589 if (Instruction *Inst = Dep.getInst())
Chris Lattner1559b362008-12-09 19:38:05 +0000590 ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction());
Chris Lattnerbf145d62008-12-01 01:15:42 +0000591 } else {
Chris Lattner37d041c2008-11-30 01:18:27 +0000592
Chris Lattnerbf145d62008-12-01 01:15:42 +0000593 // If the block *is* completely transparent to the load, we need to check
594 // the predecessors of this block. Add them to our worklist.
Chris Lattner511b36c2008-12-09 06:44:17 +0000595 for (BasicBlock **PI = PredCache->GetPreds(DirtyBB); *PI; ++PI)
596 DirtyBlocks.push_back(*PI);
Chris Lattnerbf145d62008-12-01 01:15:42 +0000597 }
Chris Lattner37d041c2008-11-30 01:18:27 +0000598 }
599
Chris Lattnerbf145d62008-12-01 01:15:42 +0000600 return Cache;
Chris Lattner37d041c2008-11-30 01:18:27 +0000601}
602
Chris Lattner7ebcf032008-12-07 02:15:47 +0000603/// getNonLocalPointerDependency - Perform a full dependency query for an
604/// access to the specified (non-volatile) memory location, returning the
605/// set of instructions that either define or clobber the value.
606///
607/// This method assumes the pointer has a "NonLocal" dependency within its
608/// own block.
609///
610void MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000611getNonLocalPointerDependency(const AliasAnalysis::Location &Loc, bool isLoad,
612 BasicBlock *FromBB,
Chris Lattner0ee443d2009-12-22 04:25:02 +0000613 SmallVectorImpl<NonLocalDepResult> &Result) {
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000614 assert(Loc.Ptr->getType()->isPointerTy() &&
Chris Lattner3f7eb5b2008-12-07 18:45:15 +0000615 "Can't get pointer deps of a non-pointer!");
Chris Lattner9a193fd2008-12-07 02:56:57 +0000616 Result.clear();
617
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000618 PHITransAddr Address(const_cast<Value *>(Loc.Ptr), TD);
Chris Lattner05e15f82009-12-09 01:59:31 +0000619
Chris Lattner9e59c642008-12-15 03:35:32 +0000620 // This is the set of blocks we've inspected, and the pointer we consider in
621 // each block. Because of critical edges, we currently bail out if querying
622 // a block with multiple different pointers. This can happen during PHI
623 // translation.
624 DenseMap<BasicBlock*, Value*> Visited;
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000625 if (!getNonLocalPointerDepFromBB(Address, Loc, isLoad, FromBB,
Chris Lattner9e59c642008-12-15 03:35:32 +0000626 Result, Visited, true))
627 return;
Chris Lattner3af23f82008-12-15 04:58:29 +0000628 Result.clear();
Chris Lattner0ee443d2009-12-22 04:25:02 +0000629 Result.push_back(NonLocalDepResult(FromBB,
630 MemDepResult::getClobber(FromBB->begin()),
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000631 const_cast<Value *>(Loc.Ptr)));
Chris Lattner9a193fd2008-12-07 02:56:57 +0000632}
633
Chris Lattner9863c3f2008-12-09 07:47:11 +0000634/// GetNonLocalInfoForBlock - Compute the memdep value for BB with
635/// Pointer/PointeeSize using either cached information in Cache or by doing a
636/// lookup (which may use dirty cache info if available). If we do a lookup,
637/// add the result to the cache.
638MemDepResult MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000639GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
Chris Lattner9863c3f2008-12-09 07:47:11 +0000640 bool isLoad, BasicBlock *BB,
641 NonLocalDepInfo *Cache, unsigned NumSortedEntries) {
642
643 // Do a binary search to see if we already have an entry for this block in
644 // the cache set. If so, find it.
645 NonLocalDepInfo::iterator Entry =
646 std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries,
Chris Lattnerdad451c2009-12-09 07:31:04 +0000647 NonLocalDepEntry(BB));
Chris Lattnere18b9712009-12-09 07:08:01 +0000648 if (Entry != Cache->begin() && (Entry-1)->getBB() == BB)
Chris Lattner9863c3f2008-12-09 07:47:11 +0000649 --Entry;
650
Chris Lattnere18b9712009-12-09 07:08:01 +0000651 NonLocalDepEntry *ExistingResult = 0;
652 if (Entry != Cache->begin()+NumSortedEntries && Entry->getBB() == BB)
653 ExistingResult = &*Entry;
Chris Lattner9863c3f2008-12-09 07:47:11 +0000654
655 // If we have a cached entry, and it is non-dirty, use it as the value for
656 // this dependency.
Chris Lattnere18b9712009-12-09 07:08:01 +0000657 if (ExistingResult && !ExistingResult->getResult().isDirty()) {
Chris Lattner9863c3f2008-12-09 07:47:11 +0000658 ++NumCacheNonLocalPtr;
Chris Lattnere18b9712009-12-09 07:08:01 +0000659 return ExistingResult->getResult();
Chris Lattner9863c3f2008-12-09 07:47:11 +0000660 }
661
662 // Otherwise, we have to scan for the value. If we have a dirty cache
663 // entry, start scanning from its position, otherwise we scan from the end
664 // of the block.
665 BasicBlock::iterator ScanPos = BB->end();
Chris Lattnere18b9712009-12-09 07:08:01 +0000666 if (ExistingResult && ExistingResult->getResult().getInst()) {
667 assert(ExistingResult->getResult().getInst()->getParent() == BB &&
Chris Lattner9863c3f2008-12-09 07:47:11 +0000668 "Instruction invalidated?");
669 ++NumCacheDirtyNonLocalPtr;
Chris Lattnere18b9712009-12-09 07:08:01 +0000670 ScanPos = ExistingResult->getResult().getInst();
Chris Lattner9863c3f2008-12-09 07:47:11 +0000671
672 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000673 ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
Chris Lattner6a0dcc12009-03-29 00:24:04 +0000674 RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000675 } else {
676 ++NumUncacheNonLocalPtr;
677 }
678
679 // Scan the block for the dependency.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000680 MemDepResult Dep = getPointerDependencyFrom(Loc, isLoad, ScanPos, BB);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000681
682 // If we had a dirty entry for the block, update it. Otherwise, just add
683 // a new entry.
684 if (ExistingResult)
Chris Lattner0ee443d2009-12-22 04:25:02 +0000685 ExistingResult->setResult(Dep);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000686 else
Chris Lattner0ee443d2009-12-22 04:25:02 +0000687 Cache->push_back(NonLocalDepEntry(BB, Dep));
Chris Lattner9863c3f2008-12-09 07:47:11 +0000688
689 // If the block has a dependency (i.e. it isn't completely transparent to
690 // the value), remember the reverse association because we just added it
691 // to Cache!
692 if (Dep.isNonLocal())
693 return Dep;
694
695 // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently
696 // update MemDep when we remove instructions.
697 Instruction *Inst = Dep.getInst();
698 assert(Inst && "Didn't depend on anything?");
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000699 ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
Chris Lattner6a0dcc12009-03-29 00:24:04 +0000700 ReverseNonLocalPtrDeps[Inst].insert(CacheKey);
Chris Lattner9863c3f2008-12-09 07:47:11 +0000701 return Dep;
702}
703
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000704/// SortNonLocalDepInfoCache - Sort the a NonLocalDepInfo cache, given a certain
705/// number of elements in the array that are already properly ordered. This is
706/// optimized for the case when only a few entries are added.
707static void
708SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
709 unsigned NumSortedEntries) {
710 switch (Cache.size() - NumSortedEntries) {
711 case 0:
712 // done, no new entries.
713 break;
714 case 2: {
715 // Two new entries, insert the last one into place.
Chris Lattnere18b9712009-12-09 07:08:01 +0000716 NonLocalDepEntry Val = Cache.back();
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000717 Cache.pop_back();
718 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
719 std::upper_bound(Cache.begin(), Cache.end()-1, Val);
720 Cache.insert(Entry, Val);
721 // FALL THROUGH.
722 }
723 case 1:
724 // One new entry, Just insert the new value at the appropriate position.
725 if (Cache.size() != 1) {
Chris Lattnere18b9712009-12-09 07:08:01 +0000726 NonLocalDepEntry Val = Cache.back();
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000727 Cache.pop_back();
728 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
729 std::upper_bound(Cache.begin(), Cache.end(), Val);
730 Cache.insert(Entry, Val);
731 }
732 break;
733 default:
734 // Added many values, do a full scale sort.
735 std::sort(Cache.begin(), Cache.end());
736 break;
737 }
738}
739
Chris Lattner9e59c642008-12-15 03:35:32 +0000740/// getNonLocalPointerDepFromBB - Perform a dependency query based on
741/// pointer/pointeesize starting at the end of StartBB. Add any clobber/def
742/// results to the results vector and keep track of which blocks are visited in
743/// 'Visited'.
744///
745/// This has special behavior for the first block queries (when SkipFirstBlock
746/// is true). In this special case, it ignores the contents of the specified
747/// block and starts returning dependence info for its predecessors.
748///
749/// This function returns false on success, or true to indicate that it could
750/// not compute dependence information for some reason. This should be treated
751/// as a clobber dependence on the first instruction in the predecessor block.
752bool MemoryDependenceAnalysis::
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000753getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
754 const AliasAnalysis::Location &Loc,
Chris Lattner9863c3f2008-12-09 07:47:11 +0000755 bool isLoad, BasicBlock *StartBB,
Chris Lattner0ee443d2009-12-22 04:25:02 +0000756 SmallVectorImpl<NonLocalDepResult> &Result,
Chris Lattner9e59c642008-12-15 03:35:32 +0000757 DenseMap<BasicBlock*, Value*> &Visited,
758 bool SkipFirstBlock) {
Chris Lattner66364342009-09-20 22:44:26 +0000759
Chris Lattner6290f5c2008-12-07 08:50:20 +0000760 // Look up the cached info for Pointer.
Chris Lattner05e15f82009-12-09 01:59:31 +0000761 ValueIsLoadPair CacheKey(Pointer.getAddr(), isLoad);
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000762
Dan Gohman075fb5d2010-11-10 20:37:15 +0000763 // Set up a temporary NLPI value. If the map doesn't yet have an entry for
764 // CacheKey, this value will be inserted as the associated value. Otherwise,
765 // it'll be ignored, and we'll have to check to see if the cached size and
766 // tbaa tag are consistent with the current query.
767 NonLocalPointerInfo InitialNLPI;
768 InitialNLPI.Size = Loc.Size;
769 InitialNLPI.TBAATag = Loc.TBAATag;
770
771 // Get the NLPI for CacheKey, inserting one into the map if it doesn't
772 // already have one.
773 std::pair<CachedNonLocalPointerInfo::iterator, bool> Pair =
774 NonLocalPointerDeps.insert(std::make_pair(CacheKey, InitialNLPI));
775 NonLocalPointerInfo *CacheInfo = &Pair.first->second;
776
Dan Gohman733c54d2010-11-10 21:45:11 +0000777 // If we already have a cache entry for this CacheKey, we may need to do some
778 // work to reconcile the cache entry and the current query.
Dan Gohman075fb5d2010-11-10 20:37:15 +0000779 if (!Pair.second) {
Dan Gohman733c54d2010-11-10 21:45:11 +0000780 if (CacheInfo->Size < Loc.Size) {
781 // The query's Size is greater than the cached one. Throw out the
782 // cached data and procede with the query at the greater size.
783 CacheInfo->Pair = BBSkipFirstBlockPair();
784 CacheInfo->Size = Loc.Size;
Dan Gohman2365f082010-11-10 22:35:02 +0000785 for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(),
786 DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI)
787 if (Instruction *Inst = DI->getResult().getInst())
788 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
Dan Gohman733c54d2010-11-10 21:45:11 +0000789 CacheInfo->NonLocalDeps.clear();
790 } else if (CacheInfo->Size > Loc.Size) {
791 // This query's Size is less than the cached one. Conservatively restart
792 // the query using the greater size.
Dan Gohman075fb5d2010-11-10 20:37:15 +0000793 return getNonLocalPointerDepFromBB(Pointer,
794 Loc.getWithNewSize(CacheInfo->Size),
795 isLoad, StartBB, Result, Visited,
796 SkipFirstBlock);
797 }
798
Dan Gohman733c54d2010-11-10 21:45:11 +0000799 // If the query's TBAATag is inconsistent with the cached one,
800 // conservatively throw out the cached data and restart the query with
801 // no tag if needed.
Dan Gohman075fb5d2010-11-10 20:37:15 +0000802 if (CacheInfo->TBAATag != Loc.TBAATag) {
Dan Gohman733c54d2010-11-10 21:45:11 +0000803 if (CacheInfo->TBAATag) {
804 CacheInfo->Pair = BBSkipFirstBlockPair();
805 CacheInfo->TBAATag = 0;
Dan Gohman2365f082010-11-10 22:35:02 +0000806 for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(),
807 DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI)
808 if (Instruction *Inst = DI->getResult().getInst())
809 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
Dan Gohman733c54d2010-11-10 21:45:11 +0000810 CacheInfo->NonLocalDeps.clear();
811 }
812 if (Loc.TBAATag)
813 return getNonLocalPointerDepFromBB(Pointer, Loc.getWithoutTBAATag(),
814 isLoad, StartBB, Result, Visited,
815 SkipFirstBlock);
Dan Gohman075fb5d2010-11-10 20:37:15 +0000816 }
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000817 }
818
819 NonLocalDepInfo *Cache = &CacheInfo->NonLocalDeps;
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000820
821 // If we have valid cached information for exactly the block we are
822 // investigating, just return it with no recomputation.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000823 if (CacheInfo->Pair == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) {
Chris Lattnerf4789512008-12-16 07:10:09 +0000824 // We have a fully cached result for this query then we can just return the
825 // cached results and populate the visited set. However, we have to verify
826 // that we don't already have conflicting results for these blocks. Check
827 // to ensure that if a block in the results set is in the visited set that
828 // it was for the same pointer query.
829 if (!Visited.empty()) {
830 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
831 I != E; ++I) {
Chris Lattnere18b9712009-12-09 07:08:01 +0000832 DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->getBB());
Chris Lattner05e15f82009-12-09 01:59:31 +0000833 if (VI == Visited.end() || VI->second == Pointer.getAddr())
834 continue;
Chris Lattnerf4789512008-12-16 07:10:09 +0000835
836 // We have a pointer mismatch in a block. Just return clobber, saying
837 // that something was clobbered in this result. We could also do a
838 // non-fully cached query, but there is little point in doing this.
839 return true;
840 }
841 }
842
Chris Lattner0ee443d2009-12-22 04:25:02 +0000843 Value *Addr = Pointer.getAddr();
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000844 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
Chris Lattnerf4789512008-12-16 07:10:09 +0000845 I != E; ++I) {
Chris Lattner0ee443d2009-12-22 04:25:02 +0000846 Visited.insert(std::make_pair(I->getBB(), Addr));
Chris Lattnere18b9712009-12-09 07:08:01 +0000847 if (!I->getResult().isNonLocal())
Chris Lattner0ee443d2009-12-22 04:25:02 +0000848 Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(), Addr));
Chris Lattnerf4789512008-12-16 07:10:09 +0000849 }
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000850 ++NumCacheCompleteNonLocalPtr;
Chris Lattner9e59c642008-12-15 03:35:32 +0000851 return false;
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000852 }
853
854 // Otherwise, either this is a new block, a block with an invalid cache
855 // pointer or one that we're about to invalidate by putting more info into it
856 // than its valid cache info. If empty, the result will be valid cache info,
857 // otherwise it isn't.
Chris Lattner9e59c642008-12-15 03:35:32 +0000858 if (Cache->empty())
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000859 CacheInfo->Pair = BBSkipFirstBlockPair(StartBB, SkipFirstBlock);
Dan Gohman8a66a202010-11-11 00:42:22 +0000860 else
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000861 CacheInfo->Pair = BBSkipFirstBlockPair();
Chris Lattner11dcd8d2008-12-08 07:31:50 +0000862
863 SmallVector<BasicBlock*, 32> Worklist;
864 Worklist.push_back(StartBB);
Chris Lattner6290f5c2008-12-07 08:50:20 +0000865
866 // Keep track of the entries that we know are sorted. Previously cached
867 // entries will all be sorted. The entries we add we only sort on demand (we
868 // don't insert every element into its sorted position). We know that we
869 // won't get any reuse from currently inserted values, because we don't
870 // revisit blocks after we insert info for them.
871 unsigned NumSortedEntries = Cache->size();
Chris Lattner12a7db32009-01-22 07:04:01 +0000872 DEBUG(AssertSorted(*Cache));
Chris Lattner6290f5c2008-12-07 08:50:20 +0000873
Chris Lattner7ebcf032008-12-07 02:15:47 +0000874 while (!Worklist.empty()) {
Chris Lattner9a193fd2008-12-07 02:56:57 +0000875 BasicBlock *BB = Worklist.pop_back_val();
Chris Lattner7ebcf032008-12-07 02:15:47 +0000876
Chris Lattner65633712008-12-09 07:52:59 +0000877 // Skip the first block if we have it.
Chris Lattner9e59c642008-12-15 03:35:32 +0000878 if (!SkipFirstBlock) {
Chris Lattner65633712008-12-09 07:52:59 +0000879 // Analyze the dependency of *Pointer in FromBB. See if we already have
880 // been here.
Chris Lattner9e59c642008-12-15 03:35:32 +0000881 assert(Visited.count(BB) && "Should check 'visited' before adding to WL");
Chris Lattner6290f5c2008-12-07 08:50:20 +0000882
Chris Lattner65633712008-12-09 07:52:59 +0000883 // Get the dependency info for Pointer in BB. If we have cached
884 // information, we will use it, otherwise we compute it.
Chris Lattner12a7db32009-01-22 07:04:01 +0000885 DEBUG(AssertSorted(*Cache, NumSortedEntries));
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000886 MemDepResult Dep = GetNonLocalInfoForBlock(Loc, isLoad, BB, Cache,
Chris Lattner05e15f82009-12-09 01:59:31 +0000887 NumSortedEntries);
Chris Lattner65633712008-12-09 07:52:59 +0000888
889 // If we got a Def or Clobber, add this to the list of results.
890 if (!Dep.isNonLocal()) {
Chris Lattner0ee443d2009-12-22 04:25:02 +0000891 Result.push_back(NonLocalDepResult(BB, Dep, Pointer.getAddr()));
Chris Lattner65633712008-12-09 07:52:59 +0000892 continue;
893 }
Chris Lattner7ebcf032008-12-07 02:15:47 +0000894 }
895
Chris Lattner9e59c642008-12-15 03:35:32 +0000896 // If 'Pointer' is an instruction defined in this block, then we need to do
897 // phi translation to change it into a value live in the predecessor block.
Chris Lattner05e15f82009-12-09 01:59:31 +0000898 // If not, we just add the predecessors to the worklist and scan them with
899 // the same Pointer.
900 if (!Pointer.NeedsPHITranslationFromBlock(BB)) {
Chris Lattner9e59c642008-12-15 03:35:32 +0000901 SkipFirstBlock = false;
902 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
903 // Verify that we haven't looked at this block yet.
904 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
Chris Lattner05e15f82009-12-09 01:59:31 +0000905 InsertRes = Visited.insert(std::make_pair(*PI, Pointer.getAddr()));
Chris Lattner9e59c642008-12-15 03:35:32 +0000906 if (InsertRes.second) {
907 // First time we've looked at *PI.
908 Worklist.push_back(*PI);
909 continue;
910 }
911
912 // If we have seen this block before, but it was with a different
913 // pointer then we have a phi translation failure and we have to treat
914 // this as a clobber.
Chris Lattner05e15f82009-12-09 01:59:31 +0000915 if (InsertRes.first->second != Pointer.getAddr())
Chris Lattner9e59c642008-12-15 03:35:32 +0000916 goto PredTranslationFailure;
917 }
918 continue;
919 }
920
Chris Lattner05e15f82009-12-09 01:59:31 +0000921 // We do need to do phi translation, if we know ahead of time we can't phi
922 // translate this value, don't even try.
923 if (!Pointer.IsPotentiallyPHITranslatable())
924 goto PredTranslationFailure;
925
Chris Lattner6fbc1962009-07-13 17:14:23 +0000926 // We may have added values to the cache list before this PHI translation.
927 // If so, we haven't done anything to ensure that the cache remains sorted.
928 // Sort it now (if needed) so that recursive invocations of
929 // getNonLocalPointerDepFromBB and other routines that could reuse the cache
930 // value will only see properly sorted cache arrays.
931 if (Cache && NumSortedEntries != Cache->size()) {
Chris Lattnera2f55dd2009-07-13 17:20:05 +0000932 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner6fbc1962009-07-13 17:14:23 +0000933 NumSortedEntries = Cache->size();
934 }
Chris Lattnere95035a2009-11-27 08:37:22 +0000935 Cache = 0;
Chris Lattner05e15f82009-12-09 01:59:31 +0000936
Chris Lattnere95035a2009-11-27 08:37:22 +0000937 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
938 BasicBlock *Pred = *PI;
Chris Lattner05e15f82009-12-09 01:59:31 +0000939
940 // Get the PHI translated pointer in this predecessor. This can fail if
941 // not translatable, in which case the getAddr() returns null.
942 PHITransAddr PredPointer(Pointer);
Daniel Dunbar6d8f2ca2010-02-24 08:48:04 +0000943 PredPointer.PHITranslateValue(BB, Pred, 0);
Chris Lattner05e15f82009-12-09 01:59:31 +0000944
945 Value *PredPtrVal = PredPointer.getAddr();
Chris Lattnere95035a2009-11-27 08:37:22 +0000946
947 // Check to see if we have already visited this pred block with another
948 // pointer. If so, we can't do this lookup. This failure can occur
949 // with PHI translation when a critical edge exists and the PHI node in
950 // the successor translates to a pointer value different than the
951 // pointer the block was first analyzed with.
952 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
Chris Lattner05e15f82009-12-09 01:59:31 +0000953 InsertRes = Visited.insert(std::make_pair(Pred, PredPtrVal));
Chris Lattner9e59c642008-12-15 03:35:32 +0000954
Chris Lattnere95035a2009-11-27 08:37:22 +0000955 if (!InsertRes.second) {
956 // If the predecessor was visited with PredPtr, then we already did
957 // the analysis and can ignore it.
Chris Lattner05e15f82009-12-09 01:59:31 +0000958 if (InsertRes.first->second == PredPtrVal)
Chris Lattnere95035a2009-11-27 08:37:22 +0000959 continue;
Chris Lattner9e59c642008-12-15 03:35:32 +0000960
Chris Lattnere95035a2009-11-27 08:37:22 +0000961 // Otherwise, the block was previously analyzed with a different
962 // pointer. We can't represent the result of this case, so we just
963 // treat this as a phi translation failure.
964 goto PredTranslationFailure;
Chris Lattner9e59c642008-12-15 03:35:32 +0000965 }
Chris Lattner6f7b2102009-11-27 22:05:15 +0000966
967 // If PHI translation was unable to find an available pointer in this
968 // predecessor, then we have to assume that the pointer is clobbered in
969 // that predecessor. We can still do PRE of the load, which would insert
970 // a computation of the pointer in this predecessor.
Chris Lattner05e15f82009-12-09 01:59:31 +0000971 if (PredPtrVal == 0) {
Chris Lattner855d9da2009-12-01 07:33:32 +0000972 // Add the entry to the Result list.
Chris Lattner0ee443d2009-12-22 04:25:02 +0000973 NonLocalDepResult Entry(Pred,
974 MemDepResult::getClobber(Pred->getTerminator()),
975 PredPtrVal);
Chris Lattner855d9da2009-12-01 07:33:32 +0000976 Result.push_back(Entry);
977
Chris Lattnerf6481252009-12-19 21:29:22 +0000978 // Since we had a phi translation failure, the cache for CacheKey won't
979 // include all of the entries that we need to immediately satisfy future
980 // queries. Mark this in NonLocalPointerDeps by setting the
981 // BBSkipFirstBlockPair pointer to null. This requires reuse of the
982 // cached value to do more work but not miss the phi trans failure.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000983 NonLocalPointerInfo &NLPI = NonLocalPointerDeps[CacheKey];
984 NLPI.Pair = BBSkipFirstBlockPair();
Chris Lattner6f7b2102009-11-27 22:05:15 +0000985 continue;
Chris Lattner6f7b2102009-11-27 22:05:15 +0000986 }
Chris Lattnere95035a2009-11-27 08:37:22 +0000987
988 // FIXME: it is entirely possible that PHI translating will end up with
989 // the same value. Consider PHI translating something like:
990 // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need*
991 // to recurse here, pedantically speaking.
Chris Lattner6fbc1962009-07-13 17:14:23 +0000992
Chris Lattnere95035a2009-11-27 08:37:22 +0000993 // If we have a problem phi translating, fall through to the code below
994 // to handle the failure condition.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +0000995 if (getNonLocalPointerDepFromBB(PredPointer,
996 Loc.getWithNewPtr(PredPointer.getAddr()),
997 isLoad, Pred,
Chris Lattnere95035a2009-11-27 08:37:22 +0000998 Result, Visited))
999 goto PredTranslationFailure;
Chris Lattner9e59c642008-12-15 03:35:32 +00001000 }
Chris Lattnere95035a2009-11-27 08:37:22 +00001001
1002 // Refresh the CacheInfo/Cache pointer so that it isn't invalidated.
1003 CacheInfo = &NonLocalPointerDeps[CacheKey];
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001004 Cache = &CacheInfo->NonLocalDeps;
Chris Lattnere95035a2009-11-27 08:37:22 +00001005 NumSortedEntries = Cache->size();
1006
1007 // Since we did phi translation, the "Cache" set won't contain all of the
1008 // results for the query. This is ok (we can still use it to accelerate
1009 // specific block queries) but we can't do the fastpath "return all
1010 // results from the set" Clear out the indicator for this.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001011 CacheInfo->Pair = BBSkipFirstBlockPair();
Chris Lattnere95035a2009-11-27 08:37:22 +00001012 SkipFirstBlock = false;
1013 continue;
Chris Lattnerdc593112009-11-26 23:18:49 +00001014
Chris Lattner9e59c642008-12-15 03:35:32 +00001015 PredTranslationFailure:
1016
Chris Lattner95900f22009-01-23 07:12:16 +00001017 if (Cache == 0) {
1018 // Refresh the CacheInfo/Cache pointer if it got invalidated.
1019 CacheInfo = &NonLocalPointerDeps[CacheKey];
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001020 Cache = &CacheInfo->NonLocalDeps;
Chris Lattner95900f22009-01-23 07:12:16 +00001021 NumSortedEntries = Cache->size();
Chris Lattner95900f22009-01-23 07:12:16 +00001022 }
Chris Lattner6fbc1962009-07-13 17:14:23 +00001023
Chris Lattnerf6481252009-12-19 21:29:22 +00001024 // Since we failed phi translation, the "Cache" set won't contain all of the
Chris Lattner9e59c642008-12-15 03:35:32 +00001025 // results for the query. This is ok (we can still use it to accelerate
1026 // specific block queries) but we can't do the fastpath "return all
Chris Lattnerf6481252009-12-19 21:29:22 +00001027 // results from the set". Clear out the indicator for this.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001028 CacheInfo->Pair = BBSkipFirstBlockPair();
Chris Lattner9e59c642008-12-15 03:35:32 +00001029
1030 // If *nothing* works, mark the pointer as being clobbered by the first
1031 // instruction in this block.
1032 //
1033 // If this is the magic first block, return this as a clobber of the whole
1034 // incoming value. Since we can't phi translate to one of the predecessors,
1035 // we have to bail out.
1036 if (SkipFirstBlock)
1037 return true;
1038
1039 for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) {
1040 assert(I != Cache->rend() && "Didn't find current block??");
Chris Lattnere18b9712009-12-09 07:08:01 +00001041 if (I->getBB() != BB)
Chris Lattner9e59c642008-12-15 03:35:32 +00001042 continue;
1043
Chris Lattnere18b9712009-12-09 07:08:01 +00001044 assert(I->getResult().isNonLocal() &&
Chris Lattner9e59c642008-12-15 03:35:32 +00001045 "Should only be here with transparent block");
Chris Lattner0ee443d2009-12-22 04:25:02 +00001046 I->setResult(MemDepResult::getClobber(BB->begin()));
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001047 ReverseNonLocalPtrDeps[BB->begin()].insert(CacheKey);
Chris Lattner0ee443d2009-12-22 04:25:02 +00001048 Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(),
1049 Pointer.getAddr()));
Chris Lattner9e59c642008-12-15 03:35:32 +00001050 break;
Chris Lattner9a193fd2008-12-07 02:56:57 +00001051 }
Chris Lattner7ebcf032008-12-07 02:15:47 +00001052 }
Chris Lattner95900f22009-01-23 07:12:16 +00001053
Chris Lattner9863c3f2008-12-09 07:47:11 +00001054 // Okay, we're done now. If we added new values to the cache, re-sort it.
Chris Lattnera2f55dd2009-07-13 17:20:05 +00001055 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner12a7db32009-01-22 07:04:01 +00001056 DEBUG(AssertSorted(*Cache));
Chris Lattner9e59c642008-12-15 03:35:32 +00001057 return false;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001058}
1059
1060/// RemoveCachedNonLocalPointerDependencies - If P exists in
1061/// CachedNonLocalPointerInfo, remove it.
1062void MemoryDependenceAnalysis::
1063RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) {
1064 CachedNonLocalPointerInfo::iterator It =
1065 NonLocalPointerDeps.find(P);
1066 if (It == NonLocalPointerDeps.end()) return;
1067
1068 // Remove all of the entries in the BB->val map. This involves removing
1069 // instructions from the reverse map.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001070 NonLocalDepInfo &PInfo = It->second.NonLocalDeps;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001071
1072 for (unsigned i = 0, e = PInfo.size(); i != e; ++i) {
Chris Lattnere18b9712009-12-09 07:08:01 +00001073 Instruction *Target = PInfo[i].getResult().getInst();
Chris Lattner6290f5c2008-12-07 08:50:20 +00001074 if (Target == 0) continue; // Ignore non-local dep results.
Chris Lattnere18b9712009-12-09 07:08:01 +00001075 assert(Target->getParent() == PInfo[i].getBB());
Chris Lattner6290f5c2008-12-07 08:50:20 +00001076
1077 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001078 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001079 }
1080
1081 // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).
1082 NonLocalPointerDeps.erase(It);
Chris Lattner7ebcf032008-12-07 02:15:47 +00001083}
1084
1085
Chris Lattnerbc99be12008-12-09 22:06:23 +00001086/// invalidateCachedPointerInfo - This method is used to invalidate cached
1087/// information about the specified pointer, because it may be too
1088/// conservative in memdep. This is an optional call that can be used when
1089/// the client detects an equivalence between the pointer and some other
1090/// value and replaces the other value with ptr. This can make Ptr available
1091/// in more places that cached info does not necessarily keep.
1092void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) {
1093 // If Ptr isn't really a pointer, just ignore it.
Duncan Sands1df98592010-02-16 11:11:14 +00001094 if (!Ptr->getType()->isPointerTy()) return;
Chris Lattnerbc99be12008-12-09 22:06:23 +00001095 // Flush store info for the pointer.
1096 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false));
1097 // Flush load info for the pointer.
1098 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true));
1099}
1100
Bob Wilson484d4a32010-02-16 19:51:59 +00001101/// invalidateCachedPredecessors - Clear the PredIteratorCache info.
1102/// This needs to be done when the CFG changes, e.g., due to splitting
1103/// critical edges.
1104void MemoryDependenceAnalysis::invalidateCachedPredecessors() {
1105 PredCache->clear();
1106}
1107
Owen Anderson78e02f72007-07-06 23:14:35 +00001108/// removeInstruction - Remove an instruction from the dependence analysis,
1109/// updating the dependence of instructions that previously depended on it.
Owen Anderson642a9e32007-08-08 22:26:03 +00001110/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattner5f589dc2008-11-28 22:04:47 +00001111void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattner5f589dc2008-11-28 22:04:47 +00001112 // Walk through the Non-local dependencies, removing this one as the value
1113 // for any cached queries.
Chris Lattnerf68f3102008-11-30 02:28:25 +00001114 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
1115 if (NLDI != NonLocalDeps.end()) {
Chris Lattnerbf145d62008-12-01 01:15:42 +00001116 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattner25f4b2b2008-11-30 02:30:50 +00001117 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
1118 DI != DE; ++DI)
Chris Lattnere18b9712009-12-09 07:08:01 +00001119 if (Instruction *Inst = DI->getResult().getInst())
Chris Lattnerd44745d2008-12-07 18:39:13 +00001120 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);
Chris Lattnerf68f3102008-11-30 02:28:25 +00001121 NonLocalDeps.erase(NLDI);
1122 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +00001123
Chris Lattner5f589dc2008-11-28 22:04:47 +00001124 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattnerbaad8882008-11-28 22:28:27 +00001125 //
Chris Lattner39f372e2008-11-29 01:43:36 +00001126 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
1127 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattner125ce362008-11-30 01:09:30 +00001128 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerd44745d2008-12-07 18:39:13 +00001129 if (Instruction *Inst = LocalDepEntry->second.getInst())
1130 RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);
Chris Lattner125ce362008-11-30 01:09:30 +00001131
Chris Lattnerbaad8882008-11-28 22:28:27 +00001132 // Remove this local dependency info.
Chris Lattner39f372e2008-11-29 01:43:36 +00001133 LocalDeps.erase(LocalDepEntry);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001134 }
1135
1136 // If we have any cached pointer dependencies on this instruction, remove
1137 // them. If the instruction has non-pointer type, then it can't be a pointer
1138 // base.
1139
1140 // Remove it from both the load info and the store info. The instruction
1141 // can't be in either of these maps if it is non-pointer.
Duncan Sands1df98592010-02-16 11:11:14 +00001142 if (RemInst->getType()->isPointerTy()) {
Chris Lattner6290f5c2008-12-07 08:50:20 +00001143 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));
1144 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));
1145 }
Chris Lattnerbaad8882008-11-28 22:28:27 +00001146
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001147 // Loop over all of the things that depend on the instruction we're removing.
1148 //
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001149 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
Chris Lattner0655f732008-12-07 18:42:51 +00001150
1151 // If we find RemInst as a clobber or Def in any of the maps for other values,
1152 // we need to replace its entry with a dirty version of the instruction after
1153 // it. If RemInst is a terminator, we use a null dirty value.
1154 //
1155 // Using a dirty version of the instruction after RemInst saves having to scan
1156 // the entire block to get to this point.
1157 MemDepResult NewDirtyVal;
1158 if (!RemInst->isTerminator())
1159 NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst));
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001160
Chris Lattner8c465272008-11-29 09:20:15 +00001161 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
1162 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001163 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001164 // RemInst can't be the terminator if it has local stuff depending on it.
Chris Lattner125ce362008-11-30 01:09:30 +00001165 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
1166 "Nothing can locally depend on a terminator");
1167
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001168 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
1169 E = ReverseDeps.end(); I != E; ++I) {
1170 Instruction *InstDependingOnRemInst = *I;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001171 assert(InstDependingOnRemInst != RemInst &&
1172 "Already removed our local dep info");
Chris Lattner125ce362008-11-30 01:09:30 +00001173
Chris Lattner0655f732008-12-07 18:42:51 +00001174 LocalDeps[InstDependingOnRemInst] = NewDirtyVal;
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001175
Chris Lattner125ce362008-11-30 01:09:30 +00001176 // Make sure to remember that new things depend on NewDepInst.
Chris Lattner0655f732008-12-07 18:42:51 +00001177 assert(NewDirtyVal.getInst() && "There is no way something else can have "
1178 "a local dep on this if it is a terminator!");
1179 ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(),
Chris Lattner125ce362008-11-30 01:09:30 +00001180 InstDependingOnRemInst));
Chris Lattnerd3d12ec2008-11-28 22:51:08 +00001181 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001182
1183 ReverseLocalDeps.erase(ReverseDepIt);
1184
1185 // Add new reverse deps after scanning the set, to avoid invalidating the
1186 // 'ReverseDeps' reference.
1187 while (!ReverseDepsToAdd.empty()) {
1188 ReverseLocalDeps[ReverseDepsToAdd.back().first]
1189 .insert(ReverseDepsToAdd.back().second);
1190 ReverseDepsToAdd.pop_back();
1191 }
Owen Anderson78e02f72007-07-06 23:14:35 +00001192 }
Owen Anderson4d13de42007-08-16 21:27:05 +00001193
Chris Lattner8c465272008-11-29 09:20:15 +00001194 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
1195 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattner6290f5c2008-12-07 08:50:20 +00001196 SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second;
1197 for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +00001198 I != E; ++I) {
1199 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
1200
Chris Lattner4a69bad2008-11-30 02:52:26 +00001201 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner4a69bad2008-11-30 02:52:26 +00001202 // The information is now dirty!
Chris Lattnerbf145d62008-12-01 01:15:42 +00001203 INLD.second = true;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001204
Chris Lattnerbf145d62008-12-01 01:15:42 +00001205 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
1206 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattnere18b9712009-12-09 07:08:01 +00001207 if (DI->getResult().getInst() != RemInst) continue;
Chris Lattnerf68f3102008-11-30 02:28:25 +00001208
1209 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner0ee443d2009-12-22 04:25:02 +00001210 DI->setResult(NewDirtyVal);
Chris Lattner0655f732008-12-07 18:42:51 +00001211
1212 if (Instruction *NextI = NewDirtyVal.getInst())
Chris Lattnerf68f3102008-11-30 02:28:25 +00001213 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattnerf68f3102008-11-30 02:28:25 +00001214 }
1215 }
Chris Lattner4f8c18c2008-11-29 23:30:39 +00001216
1217 ReverseNonLocalDeps.erase(ReverseDepIt);
1218
Chris Lattner0ec48dd2008-11-29 22:02:15 +00001219 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
1220 while (!ReverseDepsToAdd.empty()) {
1221 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
1222 .insert(ReverseDepsToAdd.back().second);
1223 ReverseDepsToAdd.pop_back();
1224 }
Owen Anderson4d13de42007-08-16 21:27:05 +00001225 }
Owen Anderson5fc4aba2007-12-08 01:37:09 +00001226
Chris Lattner6290f5c2008-12-07 08:50:20 +00001227 // If the instruction is in ReverseNonLocalPtrDeps then it appears as a
1228 // value in the NonLocalPointerDeps info.
1229 ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =
1230 ReverseNonLocalPtrDeps.find(RemInst);
1231 if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001232 SmallPtrSet<ValueIsLoadPair, 4> &Set = ReversePtrDepIt->second;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001233 SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd;
1234
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001235 for (SmallPtrSet<ValueIsLoadPair, 4>::iterator I = Set.begin(),
1236 E = Set.end(); I != E; ++I) {
1237 ValueIsLoadPair P = *I;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001238 assert(P.getPointer() != RemInst &&
1239 "Already removed NonLocalPointerDeps info for RemInst");
1240
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001241 NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].NonLocalDeps;
Chris Lattner11dcd8d2008-12-08 07:31:50 +00001242
1243 // The cache is not valid for any specific block anymore.
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001244 NonLocalPointerDeps[P].Pair = BBSkipFirstBlockPair();
Chris Lattner6290f5c2008-12-07 08:50:20 +00001245
Chris Lattner6290f5c2008-12-07 08:50:20 +00001246 // Update any entries for RemInst to use the instruction after it.
1247 for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end();
1248 DI != DE; ++DI) {
Chris Lattnere18b9712009-12-09 07:08:01 +00001249 if (DI->getResult().getInst() != RemInst) continue;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001250
1251 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner0ee443d2009-12-22 04:25:02 +00001252 DI->setResult(NewDirtyVal);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001253
1254 if (Instruction *NewDirtyInst = NewDirtyVal.getInst())
1255 ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));
1256 }
Chris Lattner95900f22009-01-23 07:12:16 +00001257
1258 // Re-sort the NonLocalDepInfo. Changing the dirty entry to its
1259 // subsequent value may invalidate the sortedness.
1260 std::sort(NLPDI.begin(), NLPDI.end());
Chris Lattner6290f5c2008-12-07 08:50:20 +00001261 }
1262
1263 ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);
1264
1265 while (!ReversePtrDepsToAdd.empty()) {
1266 ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first]
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001267 .insert(ReversePtrDepsToAdd.back().second);
Chris Lattner6290f5c2008-12-07 08:50:20 +00001268 ReversePtrDepsToAdd.pop_back();
1269 }
1270 }
1271
1272
Chris Lattnerf68f3102008-11-30 02:28:25 +00001273 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattnerd777d402008-11-30 19:24:31 +00001274 AA->deleteValue(RemInst);
Jakob Stoklund Olesenf7624bc2011-01-11 04:05:39 +00001275 DEBUG(verifyRemoved(RemInst));
Owen Anderson78e02f72007-07-06 23:14:35 +00001276}
Chris Lattner729b2372008-11-29 21:25:10 +00001277/// verifyRemoved - Verify that the specified instruction does not occur
1278/// in our internal data structures.
1279void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
1280 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
1281 E = LocalDeps.end(); I != E; ++I) {
1282 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerfd3dcbe2008-11-30 23:17:19 +00001283 assert(I->second.getInst() != D &&
Chris Lattner729b2372008-11-29 21:25:10 +00001284 "Inst occurs in data structures");
1285 }
1286
Chris Lattner6290f5c2008-12-07 08:50:20 +00001287 for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(),
1288 E = NonLocalPointerDeps.end(); I != E; ++I) {
1289 assert(I->first.getPointer() != D && "Inst occurs in NLPD map key");
Dan Gohmanc1ac0d72010-09-22 21:41:02 +00001290 const NonLocalDepInfo &Val = I->second.NonLocalDeps;
Chris Lattner6290f5c2008-12-07 08:50:20 +00001291 for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end();
1292 II != E; ++II)
Chris Lattnere18b9712009-12-09 07:08:01 +00001293 assert(II->getResult().getInst() != D && "Inst occurs as NLPD value");
Chris Lattner6290f5c2008-12-07 08:50:20 +00001294 }
1295
Chris Lattner729b2372008-11-29 21:25:10 +00001296 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
1297 E = NonLocalDeps.end(); I != E; ++I) {
1298 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner4a69bad2008-11-30 02:52:26 +00001299 const PerInstNLInfo &INLD = I->second;
Chris Lattnerbf145d62008-12-01 01:15:42 +00001300 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
1301 EE = INLD.first.end(); II != EE; ++II)
Chris Lattnere18b9712009-12-09 07:08:01 +00001302 assert(II->getResult().getInst() != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001303 }
1304
1305 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattnerf68f3102008-11-30 02:28:25 +00001306 E = ReverseLocalDeps.end(); I != E; ++I) {
1307 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001308 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1309 EE = I->second.end(); II != EE; ++II)
1310 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +00001311 }
Chris Lattner729b2372008-11-29 21:25:10 +00001312
1313 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
1314 E = ReverseNonLocalDeps.end();
Chris Lattnerf68f3102008-11-30 02:28:25 +00001315 I != E; ++I) {
1316 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner729b2372008-11-29 21:25:10 +00001317 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1318 EE = I->second.end(); II != EE; ++II)
1319 assert(*II != D && "Inst occurs in data structures");
Chris Lattnerf68f3102008-11-30 02:28:25 +00001320 }
Chris Lattner6290f5c2008-12-07 08:50:20 +00001321
1322 for (ReverseNonLocalPtrDepTy::const_iterator
1323 I = ReverseNonLocalPtrDeps.begin(),
1324 E = ReverseNonLocalPtrDeps.end(); I != E; ++I) {
1325 assert(I->first != D && "Inst occurs in rev NLPD map");
1326
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001327 for (SmallPtrSet<ValueIsLoadPair, 4>::const_iterator II = I->second.begin(),
Chris Lattner6290f5c2008-12-07 08:50:20 +00001328 E = I->second.end(); II != E; ++II)
Chris Lattner6a0dcc12009-03-29 00:24:04 +00001329 assert(*II != ValueIsLoadPair(D, false) &&
1330 *II != ValueIsLoadPair(D, true) &&
Chris Lattner6290f5c2008-12-07 08:50:20 +00001331 "Inst occurs in ReverseNonLocalPtrDeps map");
1332 }
1333
Chris Lattner729b2372008-11-29 21:25:10 +00001334}