blob: 8811e22a68a921fef45c723d50c1112951bb6086 [file] [log] [blame]
Nick Lewycky7ed1dbf2013-06-10 23:10:59 +00001//===- MemoryDependenceAnalysis.cpp - Mem Deps Implementation -------------===//
Owen Andersonc0daf5f2007-07-06 23:14:35 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Andersonc0daf5f2007-07-06 23:14:35 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements an analysis that determines, for a given memory
Jakub Staszakb0a7eed2013-03-20 21:47:51 +000011// operation, what preceding memory operations it depends on. It builds on
Owen Andersonfa788352007-08-08 22:01:54 +000012// alias analysis information, and tries to provide a lazy, caching interface to
Owen Andersonc0daf5f2007-07-06 23:14:35 +000013// a common kind of alias information query.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner554d1222008-11-28 21:45:17 +000017#define DEBUG_TYPE "memdep"
Owen Andersonc0daf5f2007-07-06 23:14:35 +000018#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/Statistic.h"
Owen Andersonc0daf5f2007-07-06 23:14:35 +000021#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner5030c6a2009-11-27 00:34:38 +000022#include "llvm/Analysis/InstructionSimplify.h"
Victor Hernandezf390e042009-10-27 20:05:49 +000023#include "llvm/Analysis/MemoryBuiltins.h"
Chris Lattner972e6d82009-12-09 01:59:31 +000024#include "llvm/Analysis/PHITransAddr.h"
Dan Gohmana4fcd242010-12-15 20:02:24 +000025#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000027#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Function.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/IntrinsicInst.h"
31#include "llvm/IR/LLVMContext.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/Debug.h"
33#include "llvm/Support/PredIteratorCache.h"
Owen Andersonc0daf5f2007-07-06 23:14:35 +000034using namespace llvm;
35
Chris Lattner7e61daf2008-12-01 01:15:42 +000036STATISTIC(NumCacheNonLocal, "Number of fully cached non-local responses");
37STATISTIC(NumCacheDirtyNonLocal, "Number of dirty cached non-local responses");
Chris Lattnere7d7e132008-11-29 22:02:15 +000038STATISTIC(NumUncacheNonLocal, "Number of uncached non-local responses");
Chris Lattnera28355d2008-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 Lattner5ed409e2008-12-08 07:31:50 +000046STATISTIC(NumCacheCompleteNonLocalPtr,
47 "Number of block queries that were completely cached");
Chris Lattnera28355d2008-12-07 08:50:20 +000048
Eli Friedman8b098b02011-06-15 23:59:25 +000049// Limit for the number of instructions to scan in a block.
Bill Wendling9ca12c12013-04-17 20:02:32 +000050static const int BlockScanLimit = 100;
Eli Friedman8b098b02011-06-15 23:59:25 +000051
Owen Andersonc0daf5f2007-07-06 23:14:35 +000052char MemoryDependenceAnalysis::ID = 0;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +000053
Owen Andersonc0daf5f2007-07-06 23:14:35 +000054// Register this pass...
Owen Anderson8ac477f2010-10-12 19:48:12 +000055INITIALIZE_PASS_BEGIN(MemoryDependenceAnalysis, "memdep",
Owen Andersondf7a4f22010-10-07 22:25:06 +000056 "Memory Dependence Analysis", false, true)
Owen Anderson8ac477f2010-10-12 19:48:12 +000057INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
58INITIALIZE_PASS_END(MemoryDependenceAnalysis, "memdep",
59 "Memory Dependence Analysis", false, true)
Owen Andersonc0daf5f2007-07-06 23:14:35 +000060
Chris Lattner768e5bc2008-12-09 06:28:49 +000061MemoryDependenceAnalysis::MemoryDependenceAnalysis()
Owen Andersona7aed182010-08-06 18:33:48 +000062: FunctionPass(ID), PredCache(0) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000063 initializeMemoryDependenceAnalysisPass(*PassRegistry::getPassRegistry());
Chris Lattner768e5bc2008-12-09 06:28:49 +000064}
65MemoryDependenceAnalysis::~MemoryDependenceAnalysis() {
66}
67
68/// Clean up memory in between runs
69void MemoryDependenceAnalysis::releaseMemory() {
70 LocalDeps.clear();
71 NonLocalDeps.clear();
72 NonLocalPointerDeps.clear();
73 ReverseLocalDeps.clear();
74 ReverseNonLocalDeps.clear();
75 ReverseNonLocalPtrDeps.clear();
76 PredCache->clear();
77}
78
79
80
Owen Andersonc0daf5f2007-07-06 23:14:35 +000081/// getAnalysisUsage - Does not modify anything. It uses Alias Analysis.
82///
83void MemoryDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
84 AU.setPreservesAll();
85 AU.addRequiredTransitive<AliasAnalysis>();
Owen Andersonc0daf5f2007-07-06 23:14:35 +000086}
87
Chris Lattner13cae612008-11-30 19:24:31 +000088bool MemoryDependenceAnalysis::runOnFunction(Function &) {
89 AA = &getAnalysis<AliasAnalysis>();
Rafael Espindola7c68beb2014-02-18 15:33:12 +000090 DL = getAnalysisIfAvailable<DataLayout>();
Chandler Carruth73523022014-01-13 13:07:17 +000091 DominatorTreeWrapperPass *DTWP =
92 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
93 DT = DTWP ? &DTWP->getDomTree() : 0;
David Blaikie041f1aa2013-05-15 07:36:59 +000094 if (!PredCache)
Chris Lattner768e5bc2008-12-09 06:28:49 +000095 PredCache.reset(new PredIteratorCache());
Chris Lattner13cae612008-11-30 19:24:31 +000096 return false;
97}
98
Chris Lattnerde4440c2008-12-07 18:39:13 +000099/// RemoveFromReverseMap - This is a helper function that removes Val from
100/// 'Inst's set in ReverseMap. If the set becomes empty, remove Inst's entry.
101template <typename KeyTy>
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000102static void RemoveFromReverseMap(DenseMap<Instruction*,
Chris Lattner8eda11b2009-03-29 00:24:04 +0000103 SmallPtrSet<KeyTy, 4> > &ReverseMap,
104 Instruction *Inst, KeyTy Val) {
105 typename DenseMap<Instruction*, SmallPtrSet<KeyTy, 4> >::iterator
Chris Lattnerde4440c2008-12-07 18:39:13 +0000106 InstIt = ReverseMap.find(Inst);
107 assert(InstIt != ReverseMap.end() && "Reverse map out of sync?");
108 bool Found = InstIt->second.erase(Val);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +0000109 assert(Found && "Invalid reverse map!"); (void)Found;
Chris Lattnerde4440c2008-12-07 18:39:13 +0000110 if (InstIt->second.empty())
111 ReverseMap.erase(InstIt);
112}
113
Dan Gohman1d760ce2010-11-10 21:51:35 +0000114/// GetLocation - If the given instruction references a specific memory
115/// location, fill in Loc with the details, otherwise set Loc.Ptr to null.
116/// Return a ModRefInfo value describing the general behavior of the
117/// instruction.
118static
119AliasAnalysis::ModRefResult GetLocation(const Instruction *Inst,
120 AliasAnalysis::Location &Loc,
121 AliasAnalysis *AA) {
122 if (const LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000123 if (LI->isUnordered()) {
124 Loc = AA->getLocation(LI);
125 return AliasAnalysis::Ref;
Jakub Staszakfa41def2013-03-20 23:53:45 +0000126 }
127 if (LI->getOrdering() == Monotonic) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000128 Loc = AA->getLocation(LI);
Dan Gohman1d760ce2010-11-10 21:51:35 +0000129 return AliasAnalysis::ModRef;
130 }
Eli Friedman5494ada2011-08-15 20:54:19 +0000131 Loc = AliasAnalysis::Location();
132 return AliasAnalysis::ModRef;
Dan Gohman1d760ce2010-11-10 21:51:35 +0000133 }
134
135 if (const StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000136 if (SI->isUnordered()) {
137 Loc = AA->getLocation(SI);
138 return AliasAnalysis::Mod;
Jakub Staszakfa41def2013-03-20 23:53:45 +0000139 }
140 if (SI->getOrdering() == Monotonic) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000141 Loc = AA->getLocation(SI);
Dan Gohman1d760ce2010-11-10 21:51:35 +0000142 return AliasAnalysis::ModRef;
143 }
Eli Friedman5494ada2011-08-15 20:54:19 +0000144 Loc = AliasAnalysis::Location();
145 return AliasAnalysis::ModRef;
Dan Gohman1d760ce2010-11-10 21:51:35 +0000146 }
147
148 if (const VAArgInst *V = dyn_cast<VAArgInst>(Inst)) {
Dan Gohman65316d62010-11-11 21:50:19 +0000149 Loc = AA->getLocation(V);
Dan Gohman1d760ce2010-11-10 21:51:35 +0000150 return AliasAnalysis::ModRef;
151 }
152
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000153 if (const CallInst *CI = isFreeCall(Inst, AA->getTargetLibraryInfo())) {
Dan Gohman1d760ce2010-11-10 21:51:35 +0000154 // calls to free() deallocate the entire structure
155 Loc = AliasAnalysis::Location(CI->getArgOperand(0));
156 return AliasAnalysis::Mod;
157 }
158
159 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
160 switch (II->getIntrinsicID()) {
161 case Intrinsic::lifetime_start:
162 case Intrinsic::lifetime_end:
163 case Intrinsic::invariant_start:
164 Loc = AliasAnalysis::Location(II->getArgOperand(1),
165 cast<ConstantInt>(II->getArgOperand(0))
166 ->getZExtValue(),
167 II->getMetadata(LLVMContext::MD_tbaa));
168 // These intrinsics don't really modify the memory, but returning Mod
169 // will allow them to be handled conservatively.
170 return AliasAnalysis::Mod;
171 case Intrinsic::invariant_end:
172 Loc = AliasAnalysis::Location(II->getArgOperand(2),
173 cast<ConstantInt>(II->getArgOperand(1))
174 ->getZExtValue(),
175 II->getMetadata(LLVMContext::MD_tbaa));
176 // These intrinsics don't really modify the memory, but returning Mod
177 // will allow them to be handled conservatively.
178 return AliasAnalysis::Mod;
179 default:
180 break;
181 }
182
183 // Otherwise, just do the coarse-grained thing that always works.
184 if (Inst->mayWriteToMemory())
185 return AliasAnalysis::ModRef;
186 if (Inst->mayReadFromMemory())
187 return AliasAnalysis::Ref;
188 return AliasAnalysis::NoModRef;
189}
Chris Lattner7e61daf2008-12-01 01:15:42 +0000190
Chris Lattner056c0902008-12-07 00:35:51 +0000191/// getCallSiteDependencyFrom - Private helper for finding the local
192/// dependencies of a call site.
Chris Lattner47e81d02008-11-30 23:17:19 +0000193MemDepResult MemoryDependenceAnalysis::
Chris Lattner702e46e2008-12-09 21:19:42 +0000194getCallSiteDependencyFrom(CallSite CS, bool isReadOnlyCall,
195 BasicBlock::iterator ScanIt, BasicBlock *BB) {
Eli Friedman8b098b02011-06-15 23:59:25 +0000196 unsigned Limit = BlockScanLimit;
197
Owen Anderson2b21c3c2007-08-08 22:26:03 +0000198 // Walk backwards through the block, looking for dependencies
Chris Lattner51ba8d02008-11-29 03:47:00 +0000199 while (ScanIt != BB->begin()) {
Eli Friedman8b098b02011-06-15 23:59:25 +0000200 // Limit the amount of scanning we do so we don't end up with quadratic
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000201 // running time on extreme testcases.
Eli Friedman8b098b02011-06-15 23:59:25 +0000202 --Limit;
203 if (!Limit)
204 return MemDepResult::getUnknown();
205
Chris Lattner51ba8d02008-11-29 03:47:00 +0000206 Instruction *Inst = --ScanIt;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000207
Owen Anderson9c884572007-07-10 17:59:22 +0000208 // If this inst is a memory op, get the pointer it accessed
Dan Gohman23483932010-09-22 21:41:02 +0000209 AliasAnalysis::Location Loc;
Dan Gohman1d760ce2010-11-10 21:51:35 +0000210 AliasAnalysis::ModRefResult MR = GetLocation(Inst, Loc, AA);
211 if (Loc.Ptr) {
212 // A simple instruction.
213 if (AA->getModRefInfo(CS, Loc) != AliasAnalysis::NoModRef)
214 return MemDepResult::getClobber(Inst);
215 continue;
216 }
217
218 if (CallSite InstCS = cast<Value>(Inst)) {
Owen Andersonf9a9cf92009-03-09 05:12:38 +0000219 // Debug intrinsics don't cause dependences.
Dale Johannesenf61c8e82009-03-11 21:13:01 +0000220 if (isa<DbgInfoIntrinsic>(Inst)) continue;
Chris Lattner0e3d6332008-12-05 21:04:20 +0000221 // If these two calls do not interfere, look past it.
Chris Lattner702e46e2008-12-09 21:19:42 +0000222 switch (AA->getModRefInfo(CS, InstCS)) {
223 case AliasAnalysis::NoModRef:
Dan Gohman26ef7c72010-08-05 22:09:15 +0000224 // If the two calls are the same, return InstCS as a Def, so that
225 // CS can be found redundant and eliminated.
Dan Gohman1d760ce2010-11-10 21:51:35 +0000226 if (isReadOnlyCall && !(MR & AliasAnalysis::Mod) &&
Dan Gohman26ef7c72010-08-05 22:09:15 +0000227 CS.getInstruction()->isIdenticalToWhenDefined(Inst))
228 return MemDepResult::getDef(Inst);
229
230 // Otherwise if the two calls don't interact (e.g. InstCS is readnone)
231 // keep scanning.
Nadav Rotem5d4e2052012-08-13 23:03:43 +0000232 continue;
Chris Lattner702e46e2008-12-09 21:19:42 +0000233 default:
Chris Lattner0e3d6332008-12-05 21:04:20 +0000234 return MemDepResult::getClobber(Inst);
Chris Lattner702e46e2008-12-09 21:19:42 +0000235 }
Chris Lattnerff862c42008-11-30 01:44:00 +0000236 }
Nadav Rotem5d4e2052012-08-13 23:03:43 +0000237
238 // If we could not obtain a pointer for the instruction and the instruction
239 // touches memory then assume that this is a dependency.
240 if (MR != AliasAnalysis::NoModRef)
241 return MemDepResult::getClobber(Inst);
Owen Anderson9c884572007-07-10 17:59:22 +0000242 }
Nadav Rotem5d4e2052012-08-13 23:03:43 +0000243
Eli Friedman7d58bc72011-06-15 00:47:34 +0000244 // No dependence found. If this is the entry block of the function, it is
245 // unknown, otherwise it is non-local.
Chris Lattner2faa2c72008-12-07 02:15:47 +0000246 if (BB != &BB->getParent()->getEntryBlock())
247 return MemDepResult::getNonLocal();
Eli Friedmanc1702c82011-10-13 22:14:57 +0000248 return MemDepResult::getNonFuncLocal();
Owen Anderson9c884572007-07-10 17:59:22 +0000249}
250
Chris Lattner7aab2792011-04-26 22:42:01 +0000251/// isLoadLoadClobberIfExtendedToFullWidth - Return true if LI is a load that
252/// would fully overlap MemLoc if done as a wider legal integer load.
253///
254/// MemLocBase, MemLocOffset are lazily computed here the first time the
255/// base/offs of memloc is needed.
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000256static bool
Chris Lattner7aab2792011-04-26 22:42:01 +0000257isLoadLoadClobberIfExtendedToFullWidth(const AliasAnalysis::Location &MemLoc,
258 const Value *&MemLocBase,
259 int64_t &MemLocOffs,
Chris Lattner827a2702011-04-28 07:29:08 +0000260 const LoadInst *LI,
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000261 const DataLayout *DL) {
Chris Lattner7aab2792011-04-26 22:42:01 +0000262 // If we have no target data, we can't do this.
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000263 if (DL == 0) return false;
Chris Lattner7aab2792011-04-26 22:42:01 +0000264
265 // If we haven't already computed the base/offset of MemLoc, do so now.
266 if (MemLocBase == 0)
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000267 MemLocBase = GetPointerBaseWithConstantOffset(MemLoc.Ptr, MemLocOffs, DL);
Chris Lattner7aab2792011-04-26 22:42:01 +0000268
Chris Lattner827a2702011-04-28 07:29:08 +0000269 unsigned Size = MemoryDependenceAnalysis::
270 getLoadLoadClobberFullWidthSize(MemLocBase, MemLocOffs, MemLoc.Size,
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000271 LI, *DL);
Chris Lattner827a2702011-04-28 07:29:08 +0000272 return Size != 0;
273}
274
275/// getLoadLoadClobberFullWidthSize - This is a little bit of analysis that
276/// looks at a memory location for a load (specified by MemLocBase, Offs,
277/// and Size) and compares it against a load. If the specified load could
278/// be safely widened to a larger integer load that is 1) still efficient,
279/// 2) safe for the target, and 3) would provide the specified memory
280/// location value, then this function returns the size in bytes of the
281/// load width to use. If not, this returns zero.
282unsigned MemoryDependenceAnalysis::
283getLoadLoadClobberFullWidthSize(const Value *MemLocBase, int64_t MemLocOffs,
284 unsigned MemLocSize, const LoadInst *LI,
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000285 const DataLayout &DL) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000286 // We can only extend simple integer loads.
287 if (!isa<IntegerType>(LI->getType()) || !LI->isSimple()) return 0;
Kostya Serebryany3838f272013-02-13 05:59:45 +0000288
289 // Load widening is hostile to ThreadSanitizer: it may cause false positives
290 // or make the reports more cryptic (access sizes are wrong).
291 if (LI->getParent()->getParent()->getAttributes().
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000292 hasAttribute(AttributeSet::FunctionIndex, Attribute::SanitizeThread))
Kostya Serebryany3838f272013-02-13 05:59:45 +0000293 return 0;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000294
Chris Lattner7aab2792011-04-26 22:42:01 +0000295 // Get the base of this load.
296 int64_t LIOffs = 0;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000297 const Value *LIBase =
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000298 GetPointerBaseWithConstantOffset(LI->getPointerOperand(), LIOffs, &DL);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000299
Chris Lattner7aab2792011-04-26 22:42:01 +0000300 // If the two pointers are not based on the same pointer, we can't tell that
301 // they are related.
Chris Lattner827a2702011-04-28 07:29:08 +0000302 if (LIBase != MemLocBase) return 0;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000303
Chris Lattner7aab2792011-04-26 22:42:01 +0000304 // Okay, the two values are based on the same pointer, but returned as
305 // no-alias. This happens when we have things like two byte loads at "P+1"
306 // and "P+3". Check to see if increasing the size of the "LI" load up to its
307 // alignment (or the largest native integer type) will allow us to load all
308 // the bits required by MemLoc.
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000309
Chris Lattner7aab2792011-04-26 22:42:01 +0000310 // If MemLoc is before LI, then no widening of LI will help us out.
Chris Lattner827a2702011-04-28 07:29:08 +0000311 if (MemLocOffs < LIOffs) return 0;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000312
Chris Lattner7aab2792011-04-26 22:42:01 +0000313 // Get the alignment of the load in bytes. We assume that it is safe to load
314 // any legal integer up to this size without a problem. For example, if we're
315 // looking at an i8 load on x86-32 that is known 1024 byte aligned, we can
316 // widen it up to an i32 load. If it is known 2-byte aligned, we can widen it
317 // to i16.
318 unsigned LoadAlign = LI->getAlignment();
319
Chris Lattner827a2702011-04-28 07:29:08 +0000320 int64_t MemLocEnd = MemLocOffs+MemLocSize;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000321
Chris Lattner7aab2792011-04-26 22:42:01 +0000322 // If no amount of rounding up will let MemLoc fit into LI, then bail out.
Chris Lattner827a2702011-04-28 07:29:08 +0000323 if (LIOffs+LoadAlign < MemLocEnd) return 0;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000324
Chris Lattner7aab2792011-04-26 22:42:01 +0000325 // This is the size of the load to try. Start with the next larger power of
326 // two.
327 unsigned NewLoadByteSize = LI->getType()->getPrimitiveSizeInBits()/8U;
328 NewLoadByteSize = NextPowerOf2(NewLoadByteSize);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000329
Chris Lattner7aab2792011-04-26 22:42:01 +0000330 while (1) {
331 // If this load size is bigger than our known alignment or would not fit
332 // into a native integer register, then we fail.
333 if (NewLoadByteSize > LoadAlign ||
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000334 !DL.fitsInLegalInteger(NewLoadByteSize*8))
Chris Lattner827a2702011-04-28 07:29:08 +0000335 return 0;
Chris Lattner7aab2792011-04-26 22:42:01 +0000336
Kostya Serebryany9e0d3772012-02-06 22:48:56 +0000337 if (LIOffs+NewLoadByteSize > MemLocEnd &&
Bill Wendling698e84f2012-12-30 10:32:01 +0000338 LI->getParent()->getParent()->getAttributes().
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000339 hasAttribute(AttributeSet::FunctionIndex, Attribute::SanitizeAddress))
Kostya Serebryany9e0d3772012-02-06 22:48:56 +0000340 // We will be reading past the location accessed by the original program.
341 // While this is safe in a regular build, Address Safety analysis tools
342 // may start reporting false warnings. So, don't do widening.
343 return 0;
Kostya Serebryany9e0d3772012-02-06 22:48:56 +0000344
Chris Lattner7aab2792011-04-26 22:42:01 +0000345 // If a load of this width would include all of MemLoc, then we succeed.
346 if (LIOffs+NewLoadByteSize >= MemLocEnd)
Chris Lattner827a2702011-04-28 07:29:08 +0000347 return NewLoadByteSize;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000348
Chris Lattner7aab2792011-04-26 22:42:01 +0000349 NewLoadByteSize <<= 1;
350 }
Chris Lattner7aab2792011-04-26 22:42:01 +0000351}
352
Chris Lattner5a786042008-12-07 01:50:16 +0000353/// getPointerDependencyFrom - Return the instruction on which a memory
Dan Gohman15a43962010-10-29 01:14:04 +0000354/// location depends. If isLoad is true, this routine ignores may-aliases with
355/// read-only operations. If isLoad is false, this routine ignores may-aliases
Shuxin Yang408bdad2013-03-06 17:48:48 +0000356/// with reads from read-only locations. If possible, pass the query
357/// instruction as well; this function may take advantage of the metadata
358/// annotated to the query instruction to refine the result.
Chris Lattner47e81d02008-11-30 23:17:19 +0000359MemDepResult MemoryDependenceAnalysis::
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000360getPointerDependencyFrom(const AliasAnalysis::Location &MemLoc, bool isLoad,
Shuxin Yang408bdad2013-03-06 17:48:48 +0000361 BasicBlock::iterator ScanIt, BasicBlock *BB,
362 Instruction *QueryInst) {
Chris Lattner2faa2c72008-12-07 02:15:47 +0000363
Chris Lattner7aab2792011-04-26 22:42:01 +0000364 const Value *MemLocBase = 0;
365 int64_t MemLocOffset = 0;
Eli Friedman8b098b02011-06-15 23:59:25 +0000366 unsigned Limit = BlockScanLimit;
Shuxin Yang408bdad2013-03-06 17:48:48 +0000367 bool isInvariantLoad = false;
368 if (isLoad && QueryInst) {
369 LoadInst *LI = dyn_cast<LoadInst>(QueryInst);
370 if (LI && LI->getMetadata(LLVMContext::MD_invariant_load) != 0)
371 isInvariantLoad = true;
372 }
Eli Friedman8b098b02011-06-15 23:59:25 +0000373
Chris Lattnera28355d2008-12-07 08:50:20 +0000374 // Walk backwards through the basic block, looking for dependencies.
Chris Lattner51ba8d02008-11-29 03:47:00 +0000375 while (ScanIt != BB->begin()) {
Yunzhong Gao5cbcf562013-11-14 01:10:52 +0000376 Instruction *Inst = --ScanIt;
377
378 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
379 // Debug intrinsics don't (and can't) cause dependencies.
380 if (isa<DbgInfoIntrinsic>(II)) continue;
381
Eli Friedman8b098b02011-06-15 23:59:25 +0000382 // Limit the amount of scanning we do so we don't end up with quadratic
383 // running time on extreme testcases.
384 --Limit;
385 if (!Limit)
386 return MemDepResult::getUnknown();
387
Chris Lattner506b8582009-12-01 21:15:15 +0000388 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
Owen Anderson2b2bd282009-10-28 07:05:35 +0000389 // If we reach a lifetime begin or end marker, then the query ends here
390 // because the value is undefined.
Chris Lattnera58edd12010-09-06 03:58:04 +0000391 if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
Owen Andersonb9878ee2009-12-02 07:35:19 +0000392 // FIXME: This only considers queries directly on the invariant-tagged
393 // pointer, not on query pointers that are indexed off of them. It'd
Chris Lattner7aab2792011-04-26 22:42:01 +0000394 // be nice to handle that at some point (the right approach is to use
395 // GetPointerBaseWithConstantOffset).
Chris Lattner32dc9bd2011-04-26 21:53:34 +0000396 if (AA->isMustAlias(AliasAnalysis::Location(II->getArgOperand(1)),
397 MemLoc))
Owen Anderson2b2bd282009-10-28 07:05:35 +0000398 return MemDepResult::getDef(II);
Chris Lattnera58edd12010-09-06 03:58:04 +0000399 continue;
Owen Andersond0e86d52009-10-28 06:18:42 +0000400 }
401 }
402
Chris Lattnerff862c42008-11-30 01:44:00 +0000403 // Values depend on loads if the pointers are must aliased. This means that
404 // a load depends on another must aliased load from the same value.
Chris Lattner0e3d6332008-12-05 21:04:20 +0000405 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000406 // Atomic loads have complications involved.
407 // FIXME: This is overly conservative.
408 if (!LI->isUnordered())
409 return MemDepResult::getClobber(LI);
410
Dan Gohman65316d62010-11-11 21:50:19 +0000411 AliasAnalysis::Location LoadLoc = AA->getLocation(LI);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000412
Chris Lattner0e3d6332008-12-05 21:04:20 +0000413 // If we found a pointer, check if it could be the same as our pointer.
Dan Gohman15a43962010-10-29 01:14:04 +0000414 AliasAnalysis::AliasResult R = AA->alias(LoadLoc, MemLoc);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000415
Chris Lattner6f83d062011-04-26 01:21:15 +0000416 if (isLoad) {
Chris Lattner7aab2792011-04-26 22:42:01 +0000417 if (R == AliasAnalysis::NoAlias) {
418 // If this is an over-aligned integer load (for example,
419 // "load i8* %P, align 4") see if it would obviously overlap with the
420 // queried location if widened to a larger load (e.g. if the queried
421 // location is 1 byte at P+1). If so, return it as a load/load
422 // clobber result, allowing the client to decide to widen the load if
423 // it wants to.
Chris Lattner229907c2011-07-18 04:54:35 +0000424 if (IntegerType *ITy = dyn_cast<IntegerType>(LI->getType()))
Chris Lattner7aab2792011-04-26 22:42:01 +0000425 if (LI->getAlignment()*8 > ITy->getPrimitiveSizeInBits() &&
426 isLoadLoadClobberIfExtendedToFullWidth(MemLoc, MemLocBase,
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000427 MemLocOffset, LI, DL))
Chris Lattner7aab2792011-04-26 22:42:01 +0000428 return MemDepResult::getClobber(Inst);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000429
Chris Lattner7aab2792011-04-26 22:42:01 +0000430 continue;
431 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000432
Chris Lattner6f83d062011-04-26 01:21:15 +0000433 // Must aliased loads are defs of each other.
434 if (R == AliasAnalysis::MustAlias)
435 return MemDepResult::getDef(Inst);
436
Dan Gohmana4717512011-06-04 06:48:50 +0000437#if 0 // FIXME: Temporarily disabled. GVN is cleverly rewriting loads
438 // in terms of clobbering loads, but since it does this by looking
439 // at the clobbering load directly, it doesn't know about any
440 // phi translation that may have happened along the way.
441
Chris Lattner6f83d062011-04-26 01:21:15 +0000442 // If we have a partial alias, then return this as a clobber for the
443 // client to handle.
444 if (R == AliasAnalysis::PartialAlias)
445 return MemDepResult::getClobber(Inst);
Dan Gohmana4717512011-06-04 06:48:50 +0000446#endif
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000447
Chris Lattner6f83d062011-04-26 01:21:15 +0000448 // Random may-alias loads don't depend on each other without a
449 // dependence.
Chris Lattner80c08182008-11-29 09:09:48 +0000450 continue;
Chris Lattner6f83d062011-04-26 01:21:15 +0000451 }
Dan Gohman15a43962010-10-29 01:14:04 +0000452
Chris Lattner7aab2792011-04-26 22:42:01 +0000453 // Stores don't depend on other no-aliased accesses.
454 if (R == AliasAnalysis::NoAlias)
455 continue;
456
Dan Gohman15a43962010-10-29 01:14:04 +0000457 // Stores don't alias loads from read-only memory.
Chris Lattner6f83d062011-04-26 01:21:15 +0000458 if (AA->pointsToConstantMemory(LoadLoc))
Dan Gohman15a43962010-10-29 01:14:04 +0000459 continue;
460
Chris Lattner6f83d062011-04-26 01:21:15 +0000461 // Stores depend on may/must aliased loads.
Chris Lattner0e3d6332008-12-05 21:04:20 +0000462 return MemDepResult::getDef(Inst);
463 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000464
Chris Lattner0e3d6332008-12-05 21:04:20 +0000465 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Eli Friedman5494ada2011-08-15 20:54:19 +0000466 // Atomic stores have complications involved.
467 // FIXME: This is overly conservative.
468 if (!SI->isUnordered())
469 return MemDepResult::getClobber(SI);
470
Chris Lattner02274a72009-05-25 21:28:56 +0000471 // If alias analysis can tell that this store is guaranteed to not modify
472 // the query pointer, ignore it. Use getModRefInfo to handle cases where
473 // the query pointer points to constant memory etc.
Dan Gohman23483932010-09-22 21:41:02 +0000474 if (AA->getModRefInfo(SI, MemLoc) == AliasAnalysis::NoModRef)
Chris Lattner02274a72009-05-25 21:28:56 +0000475 continue;
476
477 // Ok, this store might clobber the query pointer. Check to see if it is
478 // a must alias: in this case, we want to return this as a def.
Dan Gohman65316d62010-11-11 21:50:19 +0000479 AliasAnalysis::Location StoreLoc = AA->getLocation(SI);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000480
Chris Lattner0e3d6332008-12-05 21:04:20 +0000481 // If we found a pointer, check if it could be the same as our pointer.
Dan Gohman65316d62010-11-11 21:50:19 +0000482 AliasAnalysis::AliasResult R = AA->alias(StoreLoc, MemLoc);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000483
Chris Lattner0e3d6332008-12-05 21:04:20 +0000484 if (R == AliasAnalysis::NoAlias)
485 continue;
Dan Gohmanba5d0ab2010-12-13 22:47:57 +0000486 if (R == AliasAnalysis::MustAlias)
487 return MemDepResult::getDef(Inst);
Shuxin Yang408bdad2013-03-06 17:48:48 +0000488 if (isInvariantLoad)
489 continue;
Dan Gohmanba5d0ab2010-12-13 22:47:57 +0000490 return MemDepResult::getClobber(Inst);
Owen Andersonc0daf5f2007-07-06 23:14:35 +0000491 }
Chris Lattner3ff6d012008-11-30 01:39:32 +0000492
493 // If this is an allocation, and if we know that the accessed pointer is to
Chris Lattner0e3d6332008-12-05 21:04:20 +0000494 // the allocation, return Def. This means that there is no dependence and
Chris Lattner3ff6d012008-11-30 01:39:32 +0000495 // the access can be optimized based on that. For example, a load could
496 // turn into undef.
Victor Hernandez70e85052009-10-13 01:42:53 +0000497 // Note: Only determine this to be a malloc if Inst is the malloc call, not
498 // a subsequent bitcast of the malloc call result. There can be stores to
499 // the malloced memory between the malloc call and its bitcast uses, and we
500 // need to continue scanning until the malloc call.
Bob Wilsondcc54de2012-09-03 05:15:15 +0000501 const TargetLibraryInfo *TLI = AA->getTargetLibraryInfo();
502 if (isa<AllocaInst>(Inst) || isNoAliasFn(Inst, TLI)) {
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000503 const Value *AccessPtr = GetUnderlyingObject(MemLoc.Ptr, DL);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000504
Chris Lattner32dc9bd2011-04-26 21:53:34 +0000505 if (AccessPtr == Inst || AA->isMustAlias(Inst, AccessPtr))
Victor Hernandez537d8d92009-09-18 21:34:51 +0000506 return MemDepResult::getDef(Inst);
Bob Wilson01cfbfe2012-09-04 03:30:13 +0000507 // Be conservative if the accessed pointer may alias the allocation.
508 if (AA->alias(Inst, AccessPtr) != AliasAnalysis::NoAlias)
509 return MemDepResult::getClobber(Inst);
Bob Wilsondcc54de2012-09-03 05:15:15 +0000510 // If the allocation is not aliased and does not read memory (like
511 // strdup), it is safe to ignore.
512 if (isa<AllocaInst>(Inst) ||
513 isMallocLikeFn(Inst, TLI) || isCallocLikeFn(Inst, TLI))
514 continue;
Victor Hernandez537d8d92009-09-18 21:34:51 +0000515 }
516
Chris Lattner0e3d6332008-12-05 21:04:20 +0000517 // See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
Chad Rosiera968caf2012-05-14 20:35:04 +0000518 AliasAnalysis::ModRefResult MR = AA->getModRefInfo(Inst, MemLoc);
519 // If necessary, perform additional analysis.
520 if (MR == AliasAnalysis::ModRef)
521 MR = AA->callCapturesBefore(Inst, MemLoc, DT);
522 switch (MR) {
Chris Lattner41efb682008-12-09 19:47:40 +0000523 case AliasAnalysis::NoModRef:
524 // If the call has no effect on the queried pointer, just ignore it.
Chris Lattner81f19e92008-11-29 08:51:16 +0000525 continue;
Owen Andersonfc16e5a2009-10-28 06:30:52 +0000526 case AliasAnalysis::Mod:
Owen Andersonfc16e5a2009-10-28 06:30:52 +0000527 return MemDepResult::getClobber(Inst);
Chris Lattner41efb682008-12-09 19:47:40 +0000528 case AliasAnalysis::Ref:
529 // If the call is known to never store to the pointer, and if this is a
530 // load query, we can safely ignore it (scan past it).
531 if (isLoad)
532 continue;
Chris Lattner41efb682008-12-09 19:47:40 +0000533 default:
534 // Otherwise, there is a potential dependence. Return a clobber.
535 return MemDepResult::getClobber(Inst);
536 }
Owen Andersonc0daf5f2007-07-06 23:14:35 +0000537 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000538
Eli Friedman7d58bc72011-06-15 00:47:34 +0000539 // No dependence found. If this is the entry block of the function, it is
540 // unknown, otherwise it is non-local.
Chris Lattner2faa2c72008-12-07 02:15:47 +0000541 if (BB != &BB->getParent()->getEntryBlock())
542 return MemDepResult::getNonLocal();
Eli Friedmanc1702c82011-10-13 22:14:57 +0000543 return MemDepResult::getNonFuncLocal();
Owen Andersonc0daf5f2007-07-06 23:14:35 +0000544}
545
Chris Lattner51ba8d02008-11-29 03:47:00 +0000546/// getDependency - Return the instruction on which a memory operation
547/// depends.
548MemDepResult MemoryDependenceAnalysis::getDependency(Instruction *QueryInst) {
549 Instruction *ScanPos = QueryInst;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000550
Chris Lattner51ba8d02008-11-29 03:47:00 +0000551 // Check for a cached result
Chris Lattner47e81d02008-11-30 23:17:19 +0000552 MemDepResult &LocalCache = LocalDeps[QueryInst];
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000553
Chris Lattnere7d7e132008-11-29 22:02:15 +0000554 // If the cached entry is non-dirty, just return it. Note that this depends
Chris Lattner47e81d02008-11-30 23:17:19 +0000555 // on MemDepResult's default constructing to 'dirty'.
556 if (!LocalCache.isDirty())
557 return LocalCache;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000558
Chris Lattner51ba8d02008-11-29 03:47:00 +0000559 // Otherwise, if we have a dirty entry, we know we can start the scan at that
560 // instruction, which may save us some work.
Chris Lattner47e81d02008-11-30 23:17:19 +0000561 if (Instruction *Inst = LocalCache.getInst()) {
Chris Lattner51ba8d02008-11-29 03:47:00 +0000562 ScanPos = Inst;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000563
Chris Lattnerde4440c2008-12-07 18:39:13 +0000564 RemoveFromReverseMap(ReverseLocalDeps, Inst, QueryInst);
Chris Lattner44104272008-11-30 02:52:26 +0000565 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000566
Chris Lattner5a786042008-12-07 01:50:16 +0000567 BasicBlock *QueryParent = QueryInst->getParent();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000568
Chris Lattner51ba8d02008-11-29 03:47:00 +0000569 // Do the scan.
Chris Lattner5a786042008-12-07 01:50:16 +0000570 if (BasicBlock::iterator(QueryInst) == QueryParent->begin()) {
Eli Friedman7d58bc72011-06-15 00:47:34 +0000571 // No dependence found. If this is the entry block of the function, it is
572 // unknown, otherwise it is non-local.
Chris Lattner2faa2c72008-12-07 02:15:47 +0000573 if (QueryParent != &QueryParent->getParent()->getEntryBlock())
574 LocalCache = MemDepResult::getNonLocal();
575 else
Eli Friedmanc1702c82011-10-13 22:14:57 +0000576 LocalCache = MemDepResult::getNonFuncLocal();
Dan Gohman1d760ce2010-11-10 21:51:35 +0000577 } else {
578 AliasAnalysis::Location MemLoc;
579 AliasAnalysis::ModRefResult MR = GetLocation(QueryInst, MemLoc, AA);
580 if (MemLoc.Ptr) {
581 // If we can do a pointer scan, make it happen.
582 bool isLoad = !(MR & AliasAnalysis::Mod);
Chris Lattnerd540a5d2010-11-30 01:56:13 +0000583 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(QueryInst))
Owen Anderson97f0cf32011-05-17 00:05:49 +0000584 isLoad |= II->getIntrinsicID() == Intrinsic::lifetime_start;
Chris Lattnere48c31c2010-11-21 07:34:32 +0000585
Dan Gohman1d760ce2010-11-10 21:51:35 +0000586 LocalCache = getPointerDependencyFrom(MemLoc, isLoad, ScanPos,
Shuxin Yang408bdad2013-03-06 17:48:48 +0000587 QueryParent, QueryInst);
Dan Gohman1d760ce2010-11-10 21:51:35 +0000588 } else if (isa<CallInst>(QueryInst) || isa<InvokeInst>(QueryInst)) {
Gabor Greifef1ca242010-07-27 22:02:00 +0000589 CallSite QueryCS(QueryInst);
Nick Lewyckye91765f2009-12-05 06:37:24 +0000590 bool isReadOnly = AA->onlyReadsMemory(QueryCS);
591 LocalCache = getCallSiteDependencyFrom(QueryCS, isReadOnly, ScanPos,
592 QueryParent);
Dan Gohman1d760ce2010-11-10 21:51:35 +0000593 } else
594 // Non-memory instruction.
Eli Friedman7d58bc72011-06-15 00:47:34 +0000595 LocalCache = MemDepResult::getUnknown();
Nick Lewycky218a3392009-11-28 21:27:49 +0000596 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000597
Chris Lattner51ba8d02008-11-29 03:47:00 +0000598 // Remember the result!
Chris Lattner47e81d02008-11-30 23:17:19 +0000599 if (Instruction *I = LocalCache.getInst())
Chris Lattner9f1988ab2008-11-29 09:20:15 +0000600 ReverseLocalDeps[I].insert(QueryInst);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000601
Chris Lattner47e81d02008-11-30 23:17:19 +0000602 return LocalCache;
Chris Lattner51ba8d02008-11-29 03:47:00 +0000603}
604
Chris Lattnerf09619d2009-01-22 07:04:01 +0000605#ifndef NDEBUG
606/// AssertSorted - This method is used when -debug is specified to verify that
607/// cache arrays are properly kept sorted.
608static void AssertSorted(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
609 int Count = -1) {
610 if (Count == -1) Count = Cache.size();
611 if (Count == 0) return;
612
613 for (unsigned i = 1; i != unsigned(Count); ++i)
Chris Lattner0c315472009-12-09 07:08:01 +0000614 assert(!(Cache[i] < Cache[i-1]) && "Cache isn't sorted!");
Chris Lattnerf09619d2009-01-22 07:04:01 +0000615}
616#endif
617
Chris Lattner254314e2008-12-09 19:38:05 +0000618/// getNonLocalCallDependency - Perform a full dependency query for the
619/// specified call, returning the set of blocks that the value is
Chris Lattner20597532008-11-30 01:18:27 +0000620/// potentially live across. The returned set of results will include a
621/// "NonLocal" result for all blocks where the value is live across.
622///
Chris Lattner254314e2008-12-09 19:38:05 +0000623/// This method assumes the instruction returns a "NonLocal" dependency
Chris Lattner20597532008-11-30 01:18:27 +0000624/// within its own block.
625///
Chris Lattner254314e2008-12-09 19:38:05 +0000626/// This returns a reference to an internal data structure that may be
627/// invalidated on the next non-local query or when an instruction is
628/// removed. Clients must copy this data if they want it around longer than
629/// that.
Chris Lattner7e61daf2008-12-01 01:15:42 +0000630const MemoryDependenceAnalysis::NonLocalDepInfo &
Chris Lattner254314e2008-12-09 19:38:05 +0000631MemoryDependenceAnalysis::getNonLocalCallDependency(CallSite QueryCS) {
632 assert(getDependency(QueryCS.getInstruction()).isNonLocal() &&
633 "getNonLocalCallDependency should only be used on calls with non-local deps!");
634 PerInstNLInfo &CacheP = NonLocalDeps[QueryCS.getInstruction()];
Chris Lattner7e61daf2008-12-01 01:15:42 +0000635 NonLocalDepInfo &Cache = CacheP.first;
Chris Lattner20597532008-11-30 01:18:27 +0000636
637 /// DirtyBlocks - This is the set of blocks that need to be recomputed. In
638 /// the cached case, this can happen due to instructions being deleted etc. In
639 /// the uncached case, this starts out as the set of predecessors we care
640 /// about.
641 SmallVector<BasicBlock*, 32> DirtyBlocks;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000642
Chris Lattner20597532008-11-30 01:18:27 +0000643 if (!Cache.empty()) {
Chris Lattner7e61daf2008-12-01 01:15:42 +0000644 // Okay, we have a cache entry. If we know it is not dirty, just return it
645 // with no computation.
646 if (!CacheP.second) {
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000647 ++NumCacheNonLocal;
Chris Lattner7e61daf2008-12-01 01:15:42 +0000648 return Cache;
649 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000650
Chris Lattner20597532008-11-30 01:18:27 +0000651 // If we already have a partially computed set of results, scan them to
Chris Lattner7e61daf2008-12-01 01:15:42 +0000652 // determine what is dirty, seeding our initial DirtyBlocks worklist.
653 for (NonLocalDepInfo::iterator I = Cache.begin(), E = Cache.end();
654 I != E; ++I)
Chris Lattner0c315472009-12-09 07:08:01 +0000655 if (I->getResult().isDirty())
656 DirtyBlocks.push_back(I->getBB());
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000657
Chris Lattner7e61daf2008-12-01 01:15:42 +0000658 // Sort the cache so that we can do fast binary search lookups below.
659 std::sort(Cache.begin(), Cache.end());
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000660
Chris Lattner7e61daf2008-12-01 01:15:42 +0000661 ++NumCacheDirtyNonLocal;
Chris Lattner20597532008-11-30 01:18:27 +0000662 //cerr << "CACHED CASE: " << DirtyBlocks.size() << " dirty: "
663 // << Cache.size() << " cached: " << *QueryInst;
664 } else {
665 // Seed DirtyBlocks with each of the preds of QueryInst's block.
Chris Lattner254314e2008-12-09 19:38:05 +0000666 BasicBlock *QueryBB = QueryCS.getInstruction()->getParent();
Chris Lattnere8113a72008-12-09 06:44:17 +0000667 for (BasicBlock **PI = PredCache->GetPreds(QueryBB); *PI; ++PI)
668 DirtyBlocks.push_back(*PI);
Dan Gohmand2d1ae12010-06-22 15:08:57 +0000669 ++NumUncacheNonLocal;
Chris Lattner20597532008-11-30 01:18:27 +0000670 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000671
Chris Lattner702e46e2008-12-09 21:19:42 +0000672 // isReadonlyCall - If this is a read-only call, we can be more aggressive.
673 bool isReadonlyCall = AA->onlyReadsMemory(QueryCS);
Chris Lattnerff9f3db2008-12-15 03:35:32 +0000674
Chris Lattner7e61daf2008-12-01 01:15:42 +0000675 SmallPtrSet<BasicBlock*, 64> Visited;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000676
Chris Lattner7e61daf2008-12-01 01:15:42 +0000677 unsigned NumSortedEntries = Cache.size();
Chris Lattnerf09619d2009-01-22 07:04:01 +0000678 DEBUG(AssertSorted(Cache));
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000679
Chris Lattner20597532008-11-30 01:18:27 +0000680 // Iterate while we still have blocks to update.
681 while (!DirtyBlocks.empty()) {
682 BasicBlock *DirtyBB = DirtyBlocks.back();
683 DirtyBlocks.pop_back();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000684
Chris Lattner7e61daf2008-12-01 01:15:42 +0000685 // Already processed this block?
686 if (!Visited.insert(DirtyBB))
687 continue;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000688
Chris Lattner7e61daf2008-12-01 01:15:42 +0000689 // Do a binary search to see if we already have an entry for this block in
690 // the cache set. If so, find it.
Chris Lattnerf09619d2009-01-22 07:04:01 +0000691 DEBUG(AssertSorted(Cache, NumSortedEntries));
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000692 NonLocalDepInfo::iterator Entry =
Chris Lattner7e61daf2008-12-01 01:15:42 +0000693 std::upper_bound(Cache.begin(), Cache.begin()+NumSortedEntries,
Chris Lattnereea0f582009-12-09 07:31:04 +0000694 NonLocalDepEntry(DirtyBB));
Chris Lattner0c315472009-12-09 07:08:01 +0000695 if (Entry != Cache.begin() && prior(Entry)->getBB() == DirtyBB)
Chris Lattner7e61daf2008-12-01 01:15:42 +0000696 --Entry;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000697
Chris Lattner0c315472009-12-09 07:08:01 +0000698 NonLocalDepEntry *ExistingResult = 0;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000699 if (Entry != Cache.begin()+NumSortedEntries &&
Chris Lattner0c315472009-12-09 07:08:01 +0000700 Entry->getBB() == DirtyBB) {
Chris Lattner7e61daf2008-12-01 01:15:42 +0000701 // If we already have an entry, and if it isn't already dirty, the block
702 // is done.
Chris Lattner0c315472009-12-09 07:08:01 +0000703 if (!Entry->getResult().isDirty())
Chris Lattner7e61daf2008-12-01 01:15:42 +0000704 continue;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000705
Chris Lattner7e61daf2008-12-01 01:15:42 +0000706 // Otherwise, remember this slot so we can update the value.
Chris Lattner0c315472009-12-09 07:08:01 +0000707 ExistingResult = &*Entry;
Chris Lattner7e61daf2008-12-01 01:15:42 +0000708 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000709
Chris Lattner20597532008-11-30 01:18:27 +0000710 // If the dirty entry has a pointer, start scanning from it so we don't have
711 // to rescan the entire block.
712 BasicBlock::iterator ScanPos = DirtyBB->end();
Chris Lattner7e61daf2008-12-01 01:15:42 +0000713 if (ExistingResult) {
Chris Lattner0c315472009-12-09 07:08:01 +0000714 if (Instruction *Inst = ExistingResult->getResult().getInst()) {
Chris Lattner7e61daf2008-12-01 01:15:42 +0000715 ScanPos = Inst;
Chris Lattner7e61daf2008-12-01 01:15:42 +0000716 // We're removing QueryInst's use of Inst.
Chris Lattner254314e2008-12-09 19:38:05 +0000717 RemoveFromReverseMap(ReverseNonLocalDeps, Inst,
718 QueryCS.getInstruction());
Chris Lattner7e61daf2008-12-01 01:15:42 +0000719 }
Chris Lattner1b810bd2008-11-30 02:28:25 +0000720 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000721
Chris Lattner60444f82008-11-30 01:26:32 +0000722 // Find out if this block has a local dependency for QueryInst.
Chris Lattnered494f72008-12-07 01:21:14 +0000723 MemDepResult Dep;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000724
Chris Lattner254314e2008-12-09 19:38:05 +0000725 if (ScanPos != DirtyBB->begin()) {
Chris Lattner702e46e2008-12-09 21:19:42 +0000726 Dep = getCallSiteDependencyFrom(QueryCS, isReadonlyCall,ScanPos, DirtyBB);
Chris Lattner254314e2008-12-09 19:38:05 +0000727 } else if (DirtyBB != &DirtyBB->getParent()->getEntryBlock()) {
728 // No dependence found. If this is the entry block of the function, it is
Eli Friedman7d58bc72011-06-15 00:47:34 +0000729 // a clobber, otherwise it is unknown.
Chris Lattner254314e2008-12-09 19:38:05 +0000730 Dep = MemDepResult::getNonLocal();
Chris Lattner5a786042008-12-07 01:50:16 +0000731 } else {
Eli Friedmanc1702c82011-10-13 22:14:57 +0000732 Dep = MemDepResult::getNonFuncLocal();
Chris Lattner5a786042008-12-07 01:50:16 +0000733 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000734
Chris Lattner7e61daf2008-12-01 01:15:42 +0000735 // If we had a dirty entry for the block, update it. Otherwise, just add
736 // a new entry.
737 if (ExistingResult)
Chris Lattner9b7d99e2009-12-22 04:25:02 +0000738 ExistingResult->setResult(Dep);
Chris Lattner7e61daf2008-12-01 01:15:42 +0000739 else
Chris Lattner9b7d99e2009-12-22 04:25:02 +0000740 Cache.push_back(NonLocalDepEntry(DirtyBB, Dep));
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000741
Chris Lattner20597532008-11-30 01:18:27 +0000742 // If the block has a dependency (i.e. it isn't completely transparent to
Chris Lattner7e61daf2008-12-01 01:15:42 +0000743 // the value), remember the association!
744 if (!Dep.isNonLocal()) {
Chris Lattner20597532008-11-30 01:18:27 +0000745 // Keep the ReverseNonLocalDeps map up to date so we can efficiently
746 // update this when we remove instructions.
Chris Lattner7e61daf2008-12-01 01:15:42 +0000747 if (Instruction *Inst = Dep.getInst())
Chris Lattner254314e2008-12-09 19:38:05 +0000748 ReverseNonLocalDeps[Inst].insert(QueryCS.getInstruction());
Chris Lattner7e61daf2008-12-01 01:15:42 +0000749 } else {
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000750
Chris Lattner7e61daf2008-12-01 01:15:42 +0000751 // If the block *is* completely transparent to the load, we need to check
752 // the predecessors of this block. Add them to our worklist.
Chris Lattnere8113a72008-12-09 06:44:17 +0000753 for (BasicBlock **PI = PredCache->GetPreds(DirtyBB); *PI; ++PI)
754 DirtyBlocks.push_back(*PI);
Chris Lattner7e61daf2008-12-01 01:15:42 +0000755 }
Chris Lattner20597532008-11-30 01:18:27 +0000756 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000757
Chris Lattner7e61daf2008-12-01 01:15:42 +0000758 return Cache;
Chris Lattner20597532008-11-30 01:18:27 +0000759}
760
Chris Lattner2faa2c72008-12-07 02:15:47 +0000761/// getNonLocalPointerDependency - Perform a full dependency query for an
762/// access to the specified (non-volatile) memory location, returning the
763/// set of instructions that either define or clobber the value.
764///
765/// This method assumes the pointer has a "NonLocal" dependency within its
766/// own block.
767///
768void MemoryDependenceAnalysis::
Dan Gohman23483932010-09-22 21:41:02 +0000769getNonLocalPointerDependency(const AliasAnalysis::Location &Loc, bool isLoad,
770 BasicBlock *FromBB,
Chris Lattner9b7d99e2009-12-22 04:25:02 +0000771 SmallVectorImpl<NonLocalDepResult> &Result) {
Dan Gohman23483932010-09-22 21:41:02 +0000772 assert(Loc.Ptr->getType()->isPointerTy() &&
Chris Lattnerfdb88432008-12-07 18:45:15 +0000773 "Can't get pointer deps of a non-pointer!");
Chris Lattner7564a3b2008-12-07 02:56:57 +0000774 Result.clear();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000775
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000776 PHITransAddr Address(const_cast<Value *>(Loc.Ptr), DL);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000777
Chris Lattnerff9f3db2008-12-15 03:35:32 +0000778 // This is the set of blocks we've inspected, and the pointer we consider in
779 // each block. Because of critical edges, we currently bail out if querying
780 // a block with multiple different pointers. This can happen during PHI
781 // translation.
782 DenseMap<BasicBlock*, Value*> Visited;
Dan Gohman23483932010-09-22 21:41:02 +0000783 if (!getNonLocalPointerDepFromBB(Address, Loc, isLoad, FromBB,
Chris Lattnerff9f3db2008-12-15 03:35:32 +0000784 Result, Visited, true))
785 return;
Chris Lattner7ed5ccc2008-12-15 04:58:29 +0000786 Result.clear();
Chris Lattner9b7d99e2009-12-22 04:25:02 +0000787 Result.push_back(NonLocalDepResult(FromBB,
Eli Friedman7d58bc72011-06-15 00:47:34 +0000788 MemDepResult::getUnknown(),
Dan Gohman23483932010-09-22 21:41:02 +0000789 const_cast<Value *>(Loc.Ptr)));
Chris Lattner7564a3b2008-12-07 02:56:57 +0000790}
791
Chris Lattnerf903fe12008-12-09 07:47:11 +0000792/// GetNonLocalInfoForBlock - Compute the memdep value for BB with
793/// Pointer/PointeeSize using either cached information in Cache or by doing a
794/// lookup (which may use dirty cache info if available). If we do a lookup,
795/// add the result to the cache.
796MemDepResult MemoryDependenceAnalysis::
Dan Gohman23483932010-09-22 21:41:02 +0000797GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
Chris Lattnerf903fe12008-12-09 07:47:11 +0000798 bool isLoad, BasicBlock *BB,
799 NonLocalDepInfo *Cache, unsigned NumSortedEntries) {
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000800
Chris Lattnerf903fe12008-12-09 07:47:11 +0000801 // Do a binary search to see if we already have an entry for this block in
802 // the cache set. If so, find it.
803 NonLocalDepInfo::iterator Entry =
804 std::upper_bound(Cache->begin(), Cache->begin()+NumSortedEntries,
Chris Lattnereea0f582009-12-09 07:31:04 +0000805 NonLocalDepEntry(BB));
Chris Lattner0c315472009-12-09 07:08:01 +0000806 if (Entry != Cache->begin() && (Entry-1)->getBB() == BB)
Chris Lattnerf903fe12008-12-09 07:47:11 +0000807 --Entry;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000808
Chris Lattner0c315472009-12-09 07:08:01 +0000809 NonLocalDepEntry *ExistingResult = 0;
810 if (Entry != Cache->begin()+NumSortedEntries && Entry->getBB() == BB)
811 ExistingResult = &*Entry;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000812
Chris Lattnerf903fe12008-12-09 07:47:11 +0000813 // If we have a cached entry, and it is non-dirty, use it as the value for
814 // this dependency.
Chris Lattner0c315472009-12-09 07:08:01 +0000815 if (ExistingResult && !ExistingResult->getResult().isDirty()) {
Chris Lattnerf903fe12008-12-09 07:47:11 +0000816 ++NumCacheNonLocalPtr;
Chris Lattner0c315472009-12-09 07:08:01 +0000817 return ExistingResult->getResult();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000818 }
819
Chris Lattnerf903fe12008-12-09 07:47:11 +0000820 // Otherwise, we have to scan for the value. If we have a dirty cache
821 // entry, start scanning from its position, otherwise we scan from the end
822 // of the block.
823 BasicBlock::iterator ScanPos = BB->end();
Chris Lattner0c315472009-12-09 07:08:01 +0000824 if (ExistingResult && ExistingResult->getResult().getInst()) {
825 assert(ExistingResult->getResult().getInst()->getParent() == BB &&
Chris Lattnerf903fe12008-12-09 07:47:11 +0000826 "Instruction invalidated?");
827 ++NumCacheDirtyNonLocalPtr;
Chris Lattner0c315472009-12-09 07:08:01 +0000828 ScanPos = ExistingResult->getResult().getInst();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000829
Chris Lattnerf903fe12008-12-09 07:47:11 +0000830 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Dan Gohman23483932010-09-22 21:41:02 +0000831 ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
Chris Lattner8eda11b2009-03-29 00:24:04 +0000832 RemoveFromReverseMap(ReverseNonLocalPtrDeps, ScanPos, CacheKey);
Chris Lattnerf903fe12008-12-09 07:47:11 +0000833 } else {
834 ++NumUncacheNonLocalPtr;
835 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000836
Chris Lattnerf903fe12008-12-09 07:47:11 +0000837 // Scan the block for the dependency.
Dan Gohman23483932010-09-22 21:41:02 +0000838 MemDepResult Dep = getPointerDependencyFrom(Loc, isLoad, ScanPos, BB);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000839
Chris Lattnerf903fe12008-12-09 07:47:11 +0000840 // If we had a dirty entry for the block, update it. Otherwise, just add
841 // a new entry.
842 if (ExistingResult)
Chris Lattner9b7d99e2009-12-22 04:25:02 +0000843 ExistingResult->setResult(Dep);
Chris Lattnerf903fe12008-12-09 07:47:11 +0000844 else
Chris Lattner9b7d99e2009-12-22 04:25:02 +0000845 Cache->push_back(NonLocalDepEntry(BB, Dep));
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000846
Chris Lattnerf903fe12008-12-09 07:47:11 +0000847 // If the block has a dependency (i.e. it isn't completely transparent to
848 // the value), remember the reverse association because we just added it
849 // to Cache!
Eli Friedmanc1702c82011-10-13 22:14:57 +0000850 if (!Dep.isDef() && !Dep.isClobber())
Chris Lattnerf903fe12008-12-09 07:47:11 +0000851 return Dep;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000852
Chris Lattnerf903fe12008-12-09 07:47:11 +0000853 // Keep the ReverseNonLocalPtrDeps map up to date so we can efficiently
854 // update MemDep when we remove instructions.
855 Instruction *Inst = Dep.getInst();
856 assert(Inst && "Didn't depend on anything?");
Dan Gohman23483932010-09-22 21:41:02 +0000857 ValueIsLoadPair CacheKey(Loc.Ptr, isLoad);
Chris Lattner8eda11b2009-03-29 00:24:04 +0000858 ReverseNonLocalPtrDeps[Inst].insert(CacheKey);
Chris Lattnerf903fe12008-12-09 07:47:11 +0000859 return Dep;
860}
861
Chris Lattner370aada2009-07-13 17:20:05 +0000862/// SortNonLocalDepInfoCache - Sort the a NonLocalDepInfo cache, given a certain
863/// number of elements in the array that are already properly ordered. This is
864/// optimized for the case when only a few entries are added.
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000865static void
Chris Lattner370aada2009-07-13 17:20:05 +0000866SortNonLocalDepInfoCache(MemoryDependenceAnalysis::NonLocalDepInfo &Cache,
867 unsigned NumSortedEntries) {
868 switch (Cache.size() - NumSortedEntries) {
869 case 0:
870 // done, no new entries.
871 break;
872 case 2: {
873 // Two new entries, insert the last one into place.
Chris Lattner0c315472009-12-09 07:08:01 +0000874 NonLocalDepEntry Val = Cache.back();
Chris Lattner370aada2009-07-13 17:20:05 +0000875 Cache.pop_back();
876 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
877 std::upper_bound(Cache.begin(), Cache.end()-1, Val);
878 Cache.insert(Entry, Val);
879 // FALL THROUGH.
880 }
881 case 1:
882 // One new entry, Just insert the new value at the appropriate position.
883 if (Cache.size() != 1) {
Chris Lattner0c315472009-12-09 07:08:01 +0000884 NonLocalDepEntry Val = Cache.back();
Chris Lattner370aada2009-07-13 17:20:05 +0000885 Cache.pop_back();
886 MemoryDependenceAnalysis::NonLocalDepInfo::iterator Entry =
887 std::upper_bound(Cache.begin(), Cache.end(), Val);
888 Cache.insert(Entry, Val);
889 }
890 break;
891 default:
892 // Added many values, do a full scale sort.
893 std::sort(Cache.begin(), Cache.end());
894 break;
895 }
896}
897
Chris Lattnerff9f3db2008-12-15 03:35:32 +0000898/// getNonLocalPointerDepFromBB - Perform a dependency query based on
899/// pointer/pointeesize starting at the end of StartBB. Add any clobber/def
900/// results to the results vector and keep track of which blocks are visited in
901/// 'Visited'.
902///
903/// This has special behavior for the first block queries (when SkipFirstBlock
904/// is true). In this special case, it ignores the contents of the specified
905/// block and starts returning dependence info for its predecessors.
906///
907/// This function returns false on success, or true to indicate that it could
908/// not compute dependence information for some reason. This should be treated
909/// as a clobber dependence on the first instruction in the predecessor block.
910bool MemoryDependenceAnalysis::
Dan Gohman23483932010-09-22 21:41:02 +0000911getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
912 const AliasAnalysis::Location &Loc,
Chris Lattnerf903fe12008-12-09 07:47:11 +0000913 bool isLoad, BasicBlock *StartBB,
Chris Lattner9b7d99e2009-12-22 04:25:02 +0000914 SmallVectorImpl<NonLocalDepResult> &Result,
Chris Lattnerff9f3db2008-12-15 03:35:32 +0000915 DenseMap<BasicBlock*, Value*> &Visited,
916 bool SkipFirstBlock) {
Chris Lattnera28355d2008-12-07 08:50:20 +0000917 // Look up the cached info for Pointer.
Chris Lattner972e6d82009-12-09 01:59:31 +0000918 ValueIsLoadPair CacheKey(Pointer.getAddr(), isLoad);
Dan Gohman23483932010-09-22 21:41:02 +0000919
Dan Gohman0a6021a2010-11-10 20:37:15 +0000920 // Set up a temporary NLPI value. If the map doesn't yet have an entry for
921 // CacheKey, this value will be inserted as the associated value. Otherwise,
922 // it'll be ignored, and we'll have to check to see if the cached size and
923 // tbaa tag are consistent with the current query.
924 NonLocalPointerInfo InitialNLPI;
925 InitialNLPI.Size = Loc.Size;
926 InitialNLPI.TBAATag = Loc.TBAATag;
927
928 // Get the NLPI for CacheKey, inserting one into the map if it doesn't
929 // already have one.
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000930 std::pair<CachedNonLocalPointerInfo::iterator, bool> Pair =
Dan Gohman0a6021a2010-11-10 20:37:15 +0000931 NonLocalPointerDeps.insert(std::make_pair(CacheKey, InitialNLPI));
932 NonLocalPointerInfo *CacheInfo = &Pair.first->second;
933
Dan Gohman2e8ca442010-11-10 21:45:11 +0000934 // If we already have a cache entry for this CacheKey, we may need to do some
935 // work to reconcile the cache entry and the current query.
Dan Gohman0a6021a2010-11-10 20:37:15 +0000936 if (!Pair.second) {
Dan Gohman2e8ca442010-11-10 21:45:11 +0000937 if (CacheInfo->Size < Loc.Size) {
938 // The query's Size is greater than the cached one. Throw out the
Benjamin Kramerbde91762012-06-02 10:20:22 +0000939 // cached data and proceed with the query at the greater size.
Dan Gohman2e8ca442010-11-10 21:45:11 +0000940 CacheInfo->Pair = BBSkipFirstBlockPair();
941 CacheInfo->Size = Loc.Size;
Dan Gohman67919362010-11-10 22:35:02 +0000942 for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(),
943 DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI)
944 if (Instruction *Inst = DI->getResult().getInst())
945 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
Dan Gohman2e8ca442010-11-10 21:45:11 +0000946 CacheInfo->NonLocalDeps.clear();
947 } else if (CacheInfo->Size > Loc.Size) {
948 // This query's Size is less than the cached one. Conservatively restart
949 // the query using the greater size.
Dan Gohman0a6021a2010-11-10 20:37:15 +0000950 return getNonLocalPointerDepFromBB(Pointer,
951 Loc.getWithNewSize(CacheInfo->Size),
952 isLoad, StartBB, Result, Visited,
953 SkipFirstBlock);
954 }
955
Dan Gohman2e8ca442010-11-10 21:45:11 +0000956 // If the query's TBAATag is inconsistent with the cached one,
957 // conservatively throw out the cached data and restart the query with
958 // no tag if needed.
Dan Gohman0a6021a2010-11-10 20:37:15 +0000959 if (CacheInfo->TBAATag != Loc.TBAATag) {
Dan Gohman2e8ca442010-11-10 21:45:11 +0000960 if (CacheInfo->TBAATag) {
961 CacheInfo->Pair = BBSkipFirstBlockPair();
962 CacheInfo->TBAATag = 0;
Dan Gohman67919362010-11-10 22:35:02 +0000963 for (NonLocalDepInfo::iterator DI = CacheInfo->NonLocalDeps.begin(),
964 DE = CacheInfo->NonLocalDeps.end(); DI != DE; ++DI)
965 if (Instruction *Inst = DI->getResult().getInst())
966 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Inst, CacheKey);
Dan Gohman2e8ca442010-11-10 21:45:11 +0000967 CacheInfo->NonLocalDeps.clear();
968 }
969 if (Loc.TBAATag)
970 return getNonLocalPointerDepFromBB(Pointer, Loc.getWithoutTBAATag(),
971 isLoad, StartBB, Result, Visited,
972 SkipFirstBlock);
Dan Gohman0a6021a2010-11-10 20:37:15 +0000973 }
Dan Gohman23483932010-09-22 21:41:02 +0000974 }
975
976 NonLocalDepInfo *Cache = &CacheInfo->NonLocalDeps;
Chris Lattner5ed409e2008-12-08 07:31:50 +0000977
978 // If we have valid cached information for exactly the block we are
979 // investigating, just return it with no recomputation.
Dan Gohman23483932010-09-22 21:41:02 +0000980 if (CacheInfo->Pair == BBSkipFirstBlockPair(StartBB, SkipFirstBlock)) {
Chris Lattner8b4be372008-12-16 07:10:09 +0000981 // We have a fully cached result for this query then we can just return the
982 // cached results and populate the visited set. However, we have to verify
983 // that we don't already have conflicting results for these blocks. Check
984 // to ensure that if a block in the results set is in the visited set that
985 // it was for the same pointer query.
986 if (!Visited.empty()) {
987 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
988 I != E; ++I) {
Chris Lattner0c315472009-12-09 07:08:01 +0000989 DenseMap<BasicBlock*, Value*>::iterator VI = Visited.find(I->getBB());
Chris Lattner972e6d82009-12-09 01:59:31 +0000990 if (VI == Visited.end() || VI->second == Pointer.getAddr())
991 continue;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000992
Chris Lattner8b4be372008-12-16 07:10:09 +0000993 // We have a pointer mismatch in a block. Just return clobber, saying
994 // that something was clobbered in this result. We could also do a
995 // non-fully cached query, but there is little point in doing this.
996 return true;
997 }
998 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +0000999
Chris Lattner9b7d99e2009-12-22 04:25:02 +00001000 Value *Addr = Pointer.getAddr();
Chris Lattner5ed409e2008-12-08 07:31:50 +00001001 for (NonLocalDepInfo::iterator I = Cache->begin(), E = Cache->end();
Chris Lattner8b4be372008-12-16 07:10:09 +00001002 I != E; ++I) {
Chris Lattner9b7d99e2009-12-22 04:25:02 +00001003 Visited.insert(std::make_pair(I->getBB(), Addr));
Matt Arsenaultc23753a2013-05-06 02:07:24 +00001004 if (I->getResult().isNonLocal()) {
1005 continue;
1006 }
1007
1008 if (!DT) {
1009 Result.push_back(NonLocalDepResult(I->getBB(),
1010 MemDepResult::getUnknown(),
1011 Addr));
1012 } else if (DT->isReachableFromEntry(I->getBB())) {
Chris Lattner9b7d99e2009-12-22 04:25:02 +00001013 Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(), Addr));
Matt Arsenaultc23753a2013-05-06 02:07:24 +00001014 }
Chris Lattner8b4be372008-12-16 07:10:09 +00001015 }
Chris Lattner5ed409e2008-12-08 07:31:50 +00001016 ++NumCacheCompleteNonLocalPtr;
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001017 return false;
Chris Lattner5ed409e2008-12-08 07:31:50 +00001018 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001019
Chris Lattner5ed409e2008-12-08 07:31:50 +00001020 // Otherwise, either this is a new block, a block with an invalid cache
1021 // pointer or one that we're about to invalidate by putting more info into it
1022 // than its valid cache info. If empty, the result will be valid cache info,
1023 // otherwise it isn't.
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001024 if (Cache->empty())
Dan Gohman23483932010-09-22 21:41:02 +00001025 CacheInfo->Pair = BBSkipFirstBlockPair(StartBB, SkipFirstBlock);
Dan Gohmanc87c8432010-11-11 00:42:22 +00001026 else
Dan Gohman23483932010-09-22 21:41:02 +00001027 CacheInfo->Pair = BBSkipFirstBlockPair();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001028
Chris Lattner5ed409e2008-12-08 07:31:50 +00001029 SmallVector<BasicBlock*, 32> Worklist;
1030 Worklist.push_back(StartBB);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001031
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001032 // PredList used inside loop.
1033 SmallVector<std::pair<BasicBlock*, PHITransAddr>, 16> PredList;
1034
Chris Lattnera28355d2008-12-07 08:50:20 +00001035 // Keep track of the entries that we know are sorted. Previously cached
1036 // entries will all be sorted. The entries we add we only sort on demand (we
1037 // don't insert every element into its sorted position). We know that we
1038 // won't get any reuse from currently inserted values, because we don't
1039 // revisit blocks after we insert info for them.
1040 unsigned NumSortedEntries = Cache->size();
Chris Lattnerf09619d2009-01-22 07:04:01 +00001041 DEBUG(AssertSorted(*Cache));
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001042
Chris Lattner2faa2c72008-12-07 02:15:47 +00001043 while (!Worklist.empty()) {
Chris Lattner7564a3b2008-12-07 02:56:57 +00001044 BasicBlock *BB = Worklist.pop_back_val();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001045
Chris Lattner75510d82008-12-09 07:52:59 +00001046 // Skip the first block if we have it.
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001047 if (!SkipFirstBlock) {
Chris Lattner75510d82008-12-09 07:52:59 +00001048 // Analyze the dependency of *Pointer in FromBB. See if we already have
1049 // been here.
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001050 assert(Visited.count(BB) && "Should check 'visited' before adding to WL");
Chris Lattnera28355d2008-12-07 08:50:20 +00001051
Chris Lattner75510d82008-12-09 07:52:59 +00001052 // Get the dependency info for Pointer in BB. If we have cached
1053 // information, we will use it, otherwise we compute it.
Chris Lattnerf09619d2009-01-22 07:04:01 +00001054 DEBUG(AssertSorted(*Cache, NumSortedEntries));
Dan Gohman23483932010-09-22 21:41:02 +00001055 MemDepResult Dep = GetNonLocalInfoForBlock(Loc, isLoad, BB, Cache,
Chris Lattner972e6d82009-12-09 01:59:31 +00001056 NumSortedEntries);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001057
Chris Lattner75510d82008-12-09 07:52:59 +00001058 // If we got a Def or Clobber, add this to the list of results.
Matt Arsenaultc23753a2013-05-06 02:07:24 +00001059 if (!Dep.isNonLocal()) {
1060 if (!DT) {
1061 Result.push_back(NonLocalDepResult(BB,
1062 MemDepResult::getUnknown(),
1063 Pointer.getAddr()));
1064 continue;
1065 } else if (DT->isReachableFromEntry(BB)) {
1066 Result.push_back(NonLocalDepResult(BB, Dep, Pointer.getAddr()));
1067 continue;
1068 }
Chris Lattner75510d82008-12-09 07:52:59 +00001069 }
Chris Lattner2faa2c72008-12-07 02:15:47 +00001070 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001071
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001072 // If 'Pointer' is an instruction defined in this block, then we need to do
1073 // phi translation to change it into a value live in the predecessor block.
Chris Lattner972e6d82009-12-09 01:59:31 +00001074 // If not, we just add the predecessors to the worklist and scan them with
1075 // the same Pointer.
1076 if (!Pointer.NeedsPHITranslationFromBlock(BB)) {
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001077 SkipFirstBlock = false;
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001078 SmallVector<BasicBlock*, 16> NewBlocks;
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001079 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
1080 // Verify that we haven't looked at this block yet.
1081 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
Chris Lattner972e6d82009-12-09 01:59:31 +00001082 InsertRes = Visited.insert(std::make_pair(*PI, Pointer.getAddr()));
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001083 if (InsertRes.second) {
1084 // First time we've looked at *PI.
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001085 NewBlocks.push_back(*PI);
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001086 continue;
1087 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001088
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001089 // If we have seen this block before, but it was with a different
1090 // pointer then we have a phi translation failure and we have to treat
1091 // this as a clobber.
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001092 if (InsertRes.first->second != Pointer.getAddr()) {
1093 // Make sure to clean up the Visited map before continuing on to
1094 // PredTranslationFailure.
1095 for (unsigned i = 0; i < NewBlocks.size(); i++)
1096 Visited.erase(NewBlocks[i]);
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001097 goto PredTranslationFailure;
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001098 }
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001099 }
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001100 Worklist.append(NewBlocks.begin(), NewBlocks.end());
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001101 continue;
1102 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001103
Chris Lattner972e6d82009-12-09 01:59:31 +00001104 // We do need to do phi translation, if we know ahead of time we can't phi
1105 // translate this value, don't even try.
1106 if (!Pointer.IsPotentiallyPHITranslatable())
1107 goto PredTranslationFailure;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001108
Chris Lattner2f0c1c42009-07-13 17:14:23 +00001109 // We may have added values to the cache list before this PHI translation.
1110 // If so, we haven't done anything to ensure that the cache remains sorted.
1111 // Sort it now (if needed) so that recursive invocations of
1112 // getNonLocalPointerDepFromBB and other routines that could reuse the cache
1113 // value will only see properly sorted cache arrays.
1114 if (Cache && NumSortedEntries != Cache->size()) {
Chris Lattner370aada2009-07-13 17:20:05 +00001115 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattner2f0c1c42009-07-13 17:14:23 +00001116 NumSortedEntries = Cache->size();
1117 }
Chris Lattnerac323292009-11-27 08:37:22 +00001118 Cache = 0;
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001119
1120 PredList.clear();
Chris Lattnerac323292009-11-27 08:37:22 +00001121 for (BasicBlock **PI = PredCache->GetPreds(BB); *PI; ++PI) {
1122 BasicBlock *Pred = *PI;
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001123 PredList.push_back(std::make_pair(Pred, Pointer));
1124
Chris Lattner972e6d82009-12-09 01:59:31 +00001125 // Get the PHI translated pointer in this predecessor. This can fail if
1126 // not translatable, in which case the getAddr() returns null.
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001127 PHITransAddr &PredPointer = PredList.back().second;
Daniel Dunbar693ea892010-02-24 08:48:04 +00001128 PredPointer.PHITranslateValue(BB, Pred, 0);
Chris Lattner972e6d82009-12-09 01:59:31 +00001129
1130 Value *PredPtrVal = PredPointer.getAddr();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001131
Chris Lattnerac323292009-11-27 08:37:22 +00001132 // Check to see if we have already visited this pred block with another
1133 // pointer. If so, we can't do this lookup. This failure can occur
1134 // with PHI translation when a critical edge exists and the PHI node in
1135 // the successor translates to a pointer value different than the
1136 // pointer the block was first analyzed with.
1137 std::pair<DenseMap<BasicBlock*,Value*>::iterator, bool>
Chris Lattner972e6d82009-12-09 01:59:31 +00001138 InsertRes = Visited.insert(std::make_pair(Pred, PredPtrVal));
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001139
Chris Lattnerac323292009-11-27 08:37:22 +00001140 if (!InsertRes.second) {
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001141 // We found the pred; take it off the list of preds to visit.
1142 PredList.pop_back();
1143
Chris Lattnerac323292009-11-27 08:37:22 +00001144 // If the predecessor was visited with PredPtr, then we already did
1145 // the analysis and can ignore it.
Chris Lattner972e6d82009-12-09 01:59:31 +00001146 if (InsertRes.first->second == PredPtrVal)
Chris Lattnerac323292009-11-27 08:37:22 +00001147 continue;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001148
Chris Lattnerac323292009-11-27 08:37:22 +00001149 // Otherwise, the block was previously analyzed with a different
1150 // pointer. We can't represent the result of this case, so we just
1151 // treat this as a phi translation failure.
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001152
1153 // Make sure to clean up the Visited map before continuing on to
1154 // PredTranslationFailure.
Matt Arsenault2080ecd2013-03-29 18:48:42 +00001155 for (unsigned i = 0, n = PredList.size(); i < n; ++i)
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001156 Visited.erase(PredList[i].first);
1157
Chris Lattnerac323292009-11-27 08:37:22 +00001158 goto PredTranslationFailure;
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001159 }
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001160 }
1161
1162 // Actually process results here; this need to be a separate loop to avoid
1163 // calling getNonLocalPointerDepFromBB for blocks we don't want to return
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001164 // any results for. (getNonLocalPointerDepFromBB will modify our
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001165 // datastructures in ways the code after the PredTranslationFailure label
1166 // doesn't expect.)
Matt Arsenault2080ecd2013-03-29 18:48:42 +00001167 for (unsigned i = 0, n = PredList.size(); i < n; ++i) {
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001168 BasicBlock *Pred = PredList[i].first;
1169 PHITransAddr &PredPointer = PredList[i].second;
1170 Value *PredPtrVal = PredPointer.getAddr();
1171
1172 bool CanTranslate = true;
Chris Lattner2be52e72009-11-27 22:05:15 +00001173 // If PHI translation was unable to find an available pointer in this
1174 // predecessor, then we have to assume that the pointer is clobbered in
1175 // that predecessor. We can still do PRE of the load, which would insert
1176 // a computation of the pointer in this predecessor.
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001177 if (PredPtrVal == 0)
1178 CanTranslate = false;
1179
1180 // FIXME: it is entirely possible that PHI translating will end up with
1181 // the same value. Consider PHI translating something like:
1182 // X = phi [x, bb1], [y, bb2]. PHI translating for bb1 doesn't *need*
1183 // to recurse here, pedantically speaking.
1184
1185 // If getNonLocalPointerDepFromBB fails here, that means the cached
1186 // result conflicted with the Visited list; we have to conservatively
Eli Friedman7d58bc72011-06-15 00:47:34 +00001187 // assume it is unknown, but this also does not block PRE of the load.
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001188 if (!CanTranslate ||
1189 getNonLocalPointerDepFromBB(PredPointer,
1190 Loc.getWithNewPtr(PredPtrVal),
1191 isLoad, Pred,
1192 Result, Visited)) {
Chris Lattner9c2053b2009-12-01 07:33:32 +00001193 // Add the entry to the Result list.
Eli Friedman7d58bc72011-06-15 00:47:34 +00001194 NonLocalDepResult Entry(Pred, MemDepResult::getUnknown(), PredPtrVal);
Chris Lattner9c2053b2009-12-01 07:33:32 +00001195 Result.push_back(Entry);
1196
Chris Lattner25bf6f82009-12-19 21:29:22 +00001197 // Since we had a phi translation failure, the cache for CacheKey won't
1198 // include all of the entries that we need to immediately satisfy future
1199 // queries. Mark this in NonLocalPointerDeps by setting the
1200 // BBSkipFirstBlockPair pointer to null. This requires reuse of the
1201 // cached value to do more work but not miss the phi trans failure.
Dan Gohman23483932010-09-22 21:41:02 +00001202 NonLocalPointerInfo &NLPI = NonLocalPointerDeps[CacheKey];
1203 NLPI.Pair = BBSkipFirstBlockPair();
Chris Lattner2be52e72009-11-27 22:05:15 +00001204 continue;
Chris Lattner2be52e72009-11-27 22:05:15 +00001205 }
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001206 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001207
Chris Lattnerac323292009-11-27 08:37:22 +00001208 // Refresh the CacheInfo/Cache pointer so that it isn't invalidated.
1209 CacheInfo = &NonLocalPointerDeps[CacheKey];
Dan Gohman23483932010-09-22 21:41:02 +00001210 Cache = &CacheInfo->NonLocalDeps;
Chris Lattnerac323292009-11-27 08:37:22 +00001211 NumSortedEntries = Cache->size();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001212
Chris Lattnerac323292009-11-27 08:37:22 +00001213 // Since we did phi translation, the "Cache" set won't contain all of the
1214 // results for the query. This is ok (we can still use it to accelerate
1215 // specific block queries) but we can't do the fastpath "return all
1216 // results from the set" Clear out the indicator for this.
Dan Gohman23483932010-09-22 21:41:02 +00001217 CacheInfo->Pair = BBSkipFirstBlockPair();
Chris Lattnerac323292009-11-27 08:37:22 +00001218 SkipFirstBlock = false;
1219 continue;
Chris Lattnerc49f5ac2009-11-26 23:18:49 +00001220
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001221 PredTranslationFailure:
Eli Friedman4b6eeb92011-06-01 23:16:53 +00001222 // The following code is "failure"; we can't produce a sane translation
1223 // for the given block. It assumes that we haven't modified any of
1224 // our datastructures while processing the current block.
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001225
Chris Lattner3f4591c2009-01-23 07:12:16 +00001226 if (Cache == 0) {
1227 // Refresh the CacheInfo/Cache pointer if it got invalidated.
1228 CacheInfo = &NonLocalPointerDeps[CacheKey];
Dan Gohman23483932010-09-22 21:41:02 +00001229 Cache = &CacheInfo->NonLocalDeps;
Chris Lattner3f4591c2009-01-23 07:12:16 +00001230 NumSortedEntries = Cache->size();
Chris Lattner3f4591c2009-01-23 07:12:16 +00001231 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001232
Chris Lattner25bf6f82009-12-19 21:29:22 +00001233 // Since we failed phi translation, the "Cache" set won't contain all of the
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001234 // 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
Chris Lattner25bf6f82009-12-19 21:29:22 +00001236 // results from the set". Clear out the indicator for this.
Dan Gohman23483932010-09-22 21:41:02 +00001237 CacheInfo->Pair = BBSkipFirstBlockPair();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001238
Eli Friedman7d58bc72011-06-15 00:47:34 +00001239 // If *nothing* works, mark the pointer as unknown.
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001240 //
1241 // If this is the magic first block, return this as a clobber of the whole
1242 // incoming value. Since we can't phi translate to one of the predecessors,
1243 // we have to bail out.
1244 if (SkipFirstBlock)
1245 return true;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001246
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001247 for (NonLocalDepInfo::reverse_iterator I = Cache->rbegin(); ; ++I) {
1248 assert(I != Cache->rend() && "Didn't find current block??");
Chris Lattner0c315472009-12-09 07:08:01 +00001249 if (I->getBB() != BB)
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001250 continue;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001251
Chris Lattner0c315472009-12-09 07:08:01 +00001252 assert(I->getResult().isNonLocal() &&
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001253 "Should only be here with transparent block");
Eli Friedman7d58bc72011-06-15 00:47:34 +00001254 I->setResult(MemDepResult::getUnknown());
Chris Lattner9b7d99e2009-12-22 04:25:02 +00001255 Result.push_back(NonLocalDepResult(I->getBB(), I->getResult(),
1256 Pointer.getAddr()));
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001257 break;
Chris Lattner7564a3b2008-12-07 02:56:57 +00001258 }
Chris Lattner2faa2c72008-12-07 02:15:47 +00001259 }
Chris Lattner3f4591c2009-01-23 07:12:16 +00001260
Chris Lattnerf903fe12008-12-09 07:47:11 +00001261 // Okay, we're done now. If we added new values to the cache, re-sort it.
Chris Lattner370aada2009-07-13 17:20:05 +00001262 SortNonLocalDepInfoCache(*Cache, NumSortedEntries);
Chris Lattnerf09619d2009-01-22 07:04:01 +00001263 DEBUG(AssertSorted(*Cache));
Chris Lattnerff9f3db2008-12-15 03:35:32 +00001264 return false;
Chris Lattnera28355d2008-12-07 08:50:20 +00001265}
1266
1267/// RemoveCachedNonLocalPointerDependencies - If P exists in
1268/// CachedNonLocalPointerInfo, remove it.
1269void MemoryDependenceAnalysis::
1270RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P) {
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001271 CachedNonLocalPointerInfo::iterator It =
Chris Lattnera28355d2008-12-07 08:50:20 +00001272 NonLocalPointerDeps.find(P);
1273 if (It == NonLocalPointerDeps.end()) return;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001274
Chris Lattnera28355d2008-12-07 08:50:20 +00001275 // Remove all of the entries in the BB->val map. This involves removing
1276 // instructions from the reverse map.
Dan Gohman23483932010-09-22 21:41:02 +00001277 NonLocalDepInfo &PInfo = It->second.NonLocalDeps;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001278
Chris Lattnera28355d2008-12-07 08:50:20 +00001279 for (unsigned i = 0, e = PInfo.size(); i != e; ++i) {
Chris Lattner0c315472009-12-09 07:08:01 +00001280 Instruction *Target = PInfo[i].getResult().getInst();
Chris Lattnera28355d2008-12-07 08:50:20 +00001281 if (Target == 0) continue; // Ignore non-local dep results.
Chris Lattner0c315472009-12-09 07:08:01 +00001282 assert(Target->getParent() == PInfo[i].getBB());
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001283
Chris Lattnera28355d2008-12-07 08:50:20 +00001284 // Eliminating the dirty entry from 'Cache', so update the reverse info.
Chris Lattner8eda11b2009-03-29 00:24:04 +00001285 RemoveFromReverseMap(ReverseNonLocalPtrDeps, Target, P);
Chris Lattnera28355d2008-12-07 08:50:20 +00001286 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001287
Chris Lattnera28355d2008-12-07 08:50:20 +00001288 // Remove P from NonLocalPointerDeps (which deletes NonLocalDepInfo).
1289 NonLocalPointerDeps.erase(It);
Chris Lattner2faa2c72008-12-07 02:15:47 +00001290}
1291
1292
Chris Lattnerfa9f99a2008-12-09 22:06:23 +00001293/// invalidateCachedPointerInfo - This method is used to invalidate cached
1294/// information about the specified pointer, because it may be too
1295/// conservative in memdep. This is an optional call that can be used when
1296/// the client detects an equivalence between the pointer and some other
1297/// value and replaces the other value with ptr. This can make Ptr available
1298/// in more places that cached info does not necessarily keep.
1299void MemoryDependenceAnalysis::invalidateCachedPointerInfo(Value *Ptr) {
1300 // If Ptr isn't really a pointer, just ignore it.
Duncan Sands19d0b472010-02-16 11:11:14 +00001301 if (!Ptr->getType()->isPointerTy()) return;
Chris Lattnerfa9f99a2008-12-09 22:06:23 +00001302 // Flush store info for the pointer.
1303 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, false));
1304 // Flush load info for the pointer.
1305 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(Ptr, true));
1306}
1307
Bob Wilson92cdb6e2010-02-16 19:51:59 +00001308/// invalidateCachedPredecessors - Clear the PredIteratorCache info.
1309/// This needs to be done when the CFG changes, e.g., due to splitting
1310/// critical edges.
1311void MemoryDependenceAnalysis::invalidateCachedPredecessors() {
1312 PredCache->clear();
1313}
1314
Owen Andersonc0daf5f2007-07-06 23:14:35 +00001315/// removeInstruction - Remove an instruction from the dependence analysis,
1316/// updating the dependence of instructions that previously depended on it.
Owen Anderson2b21c3c2007-08-08 22:26:03 +00001317/// This method attempts to keep the cache coherent using the reverse map.
Chris Lattnera25d39522008-11-28 22:04:47 +00001318void MemoryDependenceAnalysis::removeInstruction(Instruction *RemInst) {
Chris Lattnera25d39522008-11-28 22:04:47 +00001319 // Walk through the Non-local dependencies, removing this one as the value
1320 // for any cached queries.
Chris Lattner1b810bd2008-11-30 02:28:25 +00001321 NonLocalDepMapType::iterator NLDI = NonLocalDeps.find(RemInst);
1322 if (NLDI != NonLocalDeps.end()) {
Chris Lattner7e61daf2008-12-01 01:15:42 +00001323 NonLocalDepInfo &BlockMap = NLDI->second.first;
Chris Lattnerfc678e22008-11-30 02:30:50 +00001324 for (NonLocalDepInfo::iterator DI = BlockMap.begin(), DE = BlockMap.end();
1325 DI != DE; ++DI)
Chris Lattner0c315472009-12-09 07:08:01 +00001326 if (Instruction *Inst = DI->getResult().getInst())
Chris Lattnerde4440c2008-12-07 18:39:13 +00001327 RemoveFromReverseMap(ReverseNonLocalDeps, Inst, RemInst);
Chris Lattner1b810bd2008-11-30 02:28:25 +00001328 NonLocalDeps.erase(NLDI);
1329 }
Owen Anderson086b2c42007-12-08 01:37:09 +00001330
Chris Lattnera25d39522008-11-28 22:04:47 +00001331 // If we have a cached local dependence query for this instruction, remove it.
Chris Lattner73c25452008-11-28 22:28:27 +00001332 //
Chris Lattnerde04e112008-11-29 01:43:36 +00001333 LocalDepMapType::iterator LocalDepEntry = LocalDeps.find(RemInst);
1334 if (LocalDepEntry != LocalDeps.end()) {
Chris Lattnerada1f872008-11-30 01:09:30 +00001335 // Remove us from DepInst's reverse set now that the local dep info is gone.
Chris Lattnerde4440c2008-12-07 18:39:13 +00001336 if (Instruction *Inst = LocalDepEntry->second.getInst())
1337 RemoveFromReverseMap(ReverseLocalDeps, Inst, RemInst);
Chris Lattnerada1f872008-11-30 01:09:30 +00001338
Chris Lattner73c25452008-11-28 22:28:27 +00001339 // Remove this local dependency info.
Chris Lattnerde04e112008-11-29 01:43:36 +00001340 LocalDeps.erase(LocalDepEntry);
Chris Lattnera28355d2008-12-07 08:50:20 +00001341 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001342
Chris Lattnera28355d2008-12-07 08:50:20 +00001343 // If we have any cached pointer dependencies on this instruction, remove
1344 // them. If the instruction has non-pointer type, then it can't be a pointer
1345 // base.
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001346
Chris Lattnera28355d2008-12-07 08:50:20 +00001347 // Remove it from both the load info and the store info. The instruction
1348 // can't be in either of these maps if it is non-pointer.
Duncan Sands19d0b472010-02-16 11:11:14 +00001349 if (RemInst->getType()->isPointerTy()) {
Chris Lattnera28355d2008-12-07 08:50:20 +00001350 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, false));
1351 RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair(RemInst, true));
1352 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001353
Chris Lattnerd3d91112008-11-28 22:51:08 +00001354 // Loop over all of the things that depend on the instruction we're removing.
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001355 //
Chris Lattner63bd5862008-11-29 23:30:39 +00001356 SmallVector<std::pair<Instruction*, Instruction*>, 8> ReverseDepsToAdd;
Chris Lattner82b70342008-12-07 18:42:51 +00001357
1358 // If we find RemInst as a clobber or Def in any of the maps for other values,
1359 // we need to replace its entry with a dirty version of the instruction after
1360 // it. If RemInst is a terminator, we use a null dirty value.
1361 //
1362 // Using a dirty version of the instruction after RemInst saves having to scan
1363 // the entire block to get to this point.
1364 MemDepResult NewDirtyVal;
1365 if (!RemInst->isTerminator())
1366 NewDirtyVal = MemDepResult::getDirty(++BasicBlock::iterator(RemInst));
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001367
Chris Lattner9f1988ab2008-11-29 09:20:15 +00001368 ReverseDepMapType::iterator ReverseDepIt = ReverseLocalDeps.find(RemInst);
1369 if (ReverseDepIt != ReverseLocalDeps.end()) {
Chris Lattnerd3d91112008-11-28 22:51:08 +00001370 SmallPtrSet<Instruction*, 4> &ReverseDeps = ReverseDepIt->second;
Chris Lattnera28355d2008-12-07 08:50:20 +00001371 // RemInst can't be the terminator if it has local stuff depending on it.
Chris Lattnerada1f872008-11-30 01:09:30 +00001372 assert(!ReverseDeps.empty() && !isa<TerminatorInst>(RemInst) &&
1373 "Nothing can locally depend on a terminator");
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001374
Chris Lattnerd3d91112008-11-28 22:51:08 +00001375 for (SmallPtrSet<Instruction*, 4>::iterator I = ReverseDeps.begin(),
1376 E = ReverseDeps.end(); I != E; ++I) {
1377 Instruction *InstDependingOnRemInst = *I;
Chris Lattner1b810bd2008-11-30 02:28:25 +00001378 assert(InstDependingOnRemInst != RemInst &&
1379 "Already removed our local dep info");
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001380
Chris Lattner82b70342008-12-07 18:42:51 +00001381 LocalDeps[InstDependingOnRemInst] = NewDirtyVal;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001382
Chris Lattnerada1f872008-11-30 01:09:30 +00001383 // Make sure to remember that new things depend on NewDepInst.
Chris Lattner82b70342008-12-07 18:42:51 +00001384 assert(NewDirtyVal.getInst() && "There is no way something else can have "
1385 "a local dep on this if it is a terminator!");
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001386 ReverseDepsToAdd.push_back(std::make_pair(NewDirtyVal.getInst(),
Chris Lattnerada1f872008-11-30 01:09:30 +00001387 InstDependingOnRemInst));
Chris Lattnerd3d91112008-11-28 22:51:08 +00001388 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001389
Chris Lattner63bd5862008-11-29 23:30:39 +00001390 ReverseLocalDeps.erase(ReverseDepIt);
1391
1392 // Add new reverse deps after scanning the set, to avoid invalidating the
1393 // 'ReverseDeps' reference.
1394 while (!ReverseDepsToAdd.empty()) {
1395 ReverseLocalDeps[ReverseDepsToAdd.back().first]
1396 .insert(ReverseDepsToAdd.back().second);
1397 ReverseDepsToAdd.pop_back();
1398 }
Owen Andersonc0daf5f2007-07-06 23:14:35 +00001399 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001400
Chris Lattner9f1988ab2008-11-29 09:20:15 +00001401 ReverseDepIt = ReverseNonLocalDeps.find(RemInst);
1402 if (ReverseDepIt != ReverseNonLocalDeps.end()) {
Chris Lattnera28355d2008-12-07 08:50:20 +00001403 SmallPtrSet<Instruction*, 4> &Set = ReverseDepIt->second;
1404 for (SmallPtrSet<Instruction*, 4>::iterator I = Set.begin(), E = Set.end();
Chris Lattner1b810bd2008-11-30 02:28:25 +00001405 I != E; ++I) {
1406 assert(*I != RemInst && "Already removed NonLocalDep info for RemInst");
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001407
Chris Lattner44104272008-11-30 02:52:26 +00001408 PerInstNLInfo &INLD = NonLocalDeps[*I];
Chris Lattner44104272008-11-30 02:52:26 +00001409 // The information is now dirty!
Chris Lattner7e61daf2008-12-01 01:15:42 +00001410 INLD.second = true;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001411
1412 for (NonLocalDepInfo::iterator DI = INLD.first.begin(),
Chris Lattner7e61daf2008-12-01 01:15:42 +00001413 DE = INLD.first.end(); DI != DE; ++DI) {
Chris Lattner0c315472009-12-09 07:08:01 +00001414 if (DI->getResult().getInst() != RemInst) continue;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001415
Chris Lattner1b810bd2008-11-30 02:28:25 +00001416 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner9b7d99e2009-12-22 04:25:02 +00001417 DI->setResult(NewDirtyVal);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001418
Chris Lattner82b70342008-12-07 18:42:51 +00001419 if (Instruction *NextI = NewDirtyVal.getInst())
Chris Lattner1b810bd2008-11-30 02:28:25 +00001420 ReverseDepsToAdd.push_back(std::make_pair(NextI, *I));
Chris Lattner1b810bd2008-11-30 02:28:25 +00001421 }
1422 }
Chris Lattner63bd5862008-11-29 23:30:39 +00001423
1424 ReverseNonLocalDeps.erase(ReverseDepIt);
1425
Chris Lattnere7d7e132008-11-29 22:02:15 +00001426 // Add new reverse deps after scanning the set, to avoid invalidating 'Set'
1427 while (!ReverseDepsToAdd.empty()) {
1428 ReverseNonLocalDeps[ReverseDepsToAdd.back().first]
1429 .insert(ReverseDepsToAdd.back().second);
1430 ReverseDepsToAdd.pop_back();
1431 }
Owen Anderson5f208be2007-08-16 21:27:05 +00001432 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001433
Chris Lattnera28355d2008-12-07 08:50:20 +00001434 // If the instruction is in ReverseNonLocalPtrDeps then it appears as a
1435 // value in the NonLocalPointerDeps info.
1436 ReverseNonLocalPtrDepTy::iterator ReversePtrDepIt =
1437 ReverseNonLocalPtrDeps.find(RemInst);
1438 if (ReversePtrDepIt != ReverseNonLocalPtrDeps.end()) {
Chris Lattner8eda11b2009-03-29 00:24:04 +00001439 SmallPtrSet<ValueIsLoadPair, 4> &Set = ReversePtrDepIt->second;
Chris Lattnera28355d2008-12-07 08:50:20 +00001440 SmallVector<std::pair<Instruction*, ValueIsLoadPair>,8> ReversePtrDepsToAdd;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001441
Chris Lattner8eda11b2009-03-29 00:24:04 +00001442 for (SmallPtrSet<ValueIsLoadPair, 4>::iterator I = Set.begin(),
1443 E = Set.end(); I != E; ++I) {
1444 ValueIsLoadPair P = *I;
Chris Lattnera28355d2008-12-07 08:50:20 +00001445 assert(P.getPointer() != RemInst &&
1446 "Already removed NonLocalPointerDeps info for RemInst");
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001447
Dan Gohman23483932010-09-22 21:41:02 +00001448 NonLocalDepInfo &NLPDI = NonLocalPointerDeps[P].NonLocalDeps;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001449
Chris Lattner5ed409e2008-12-08 07:31:50 +00001450 // The cache is not valid for any specific block anymore.
Dan Gohman23483932010-09-22 21:41:02 +00001451 NonLocalPointerDeps[P].Pair = BBSkipFirstBlockPair();
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001452
Chris Lattnera28355d2008-12-07 08:50:20 +00001453 // Update any entries for RemInst to use the instruction after it.
1454 for (NonLocalDepInfo::iterator DI = NLPDI.begin(), DE = NLPDI.end();
1455 DI != DE; ++DI) {
Chris Lattner0c315472009-12-09 07:08:01 +00001456 if (DI->getResult().getInst() != RemInst) continue;
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001457
Chris Lattnera28355d2008-12-07 08:50:20 +00001458 // Convert to a dirty entry for the subsequent instruction.
Chris Lattner9b7d99e2009-12-22 04:25:02 +00001459 DI->setResult(NewDirtyVal);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001460
Chris Lattnera28355d2008-12-07 08:50:20 +00001461 if (Instruction *NewDirtyInst = NewDirtyVal.getInst())
1462 ReversePtrDepsToAdd.push_back(std::make_pair(NewDirtyInst, P));
1463 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001464
Chris Lattner3f4591c2009-01-23 07:12:16 +00001465 // Re-sort the NonLocalDepInfo. Changing the dirty entry to its
1466 // subsequent value may invalidate the sortedness.
1467 std::sort(NLPDI.begin(), NLPDI.end());
Chris Lattnera28355d2008-12-07 08:50:20 +00001468 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001469
Chris Lattnera28355d2008-12-07 08:50:20 +00001470 ReverseNonLocalPtrDeps.erase(ReversePtrDepIt);
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001471
Chris Lattnera28355d2008-12-07 08:50:20 +00001472 while (!ReversePtrDepsToAdd.empty()) {
1473 ReverseNonLocalPtrDeps[ReversePtrDepsToAdd.back().first]
Chris Lattner8eda11b2009-03-29 00:24:04 +00001474 .insert(ReversePtrDepsToAdd.back().second);
Chris Lattnera28355d2008-12-07 08:50:20 +00001475 ReversePtrDepsToAdd.pop_back();
1476 }
1477 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001478
1479
Chris Lattner1b810bd2008-11-30 02:28:25 +00001480 assert(!NonLocalDeps.count(RemInst) && "RemInst got reinserted?");
Chris Lattner13cae612008-11-30 19:24:31 +00001481 AA->deleteValue(RemInst);
Jakob Stoklund Olesen087f2072011-01-11 04:05:39 +00001482 DEBUG(verifyRemoved(RemInst));
Owen Andersonc0daf5f2007-07-06 23:14:35 +00001483}
Chris Lattnerb8ec75b2008-11-29 21:25:10 +00001484/// verifyRemoved - Verify that the specified instruction does not occur
1485/// in our internal data structures.
1486void MemoryDependenceAnalysis::verifyRemoved(Instruction *D) const {
1487 for (LocalDepMapType::const_iterator I = LocalDeps.begin(),
1488 E = LocalDeps.end(); I != E; ++I) {
1489 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner47e81d02008-11-30 23:17:19 +00001490 assert(I->second.getInst() != D &&
Chris Lattnerb8ec75b2008-11-29 21:25:10 +00001491 "Inst occurs in data structures");
1492 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001493
Chris Lattnera28355d2008-12-07 08:50:20 +00001494 for (CachedNonLocalPointerInfo::const_iterator I =NonLocalPointerDeps.begin(),
1495 E = NonLocalPointerDeps.end(); I != E; ++I) {
1496 assert(I->first.getPointer() != D && "Inst occurs in NLPD map key");
Dan Gohman23483932010-09-22 21:41:02 +00001497 const NonLocalDepInfo &Val = I->second.NonLocalDeps;
Chris Lattnera28355d2008-12-07 08:50:20 +00001498 for (NonLocalDepInfo::const_iterator II = Val.begin(), E = Val.end();
1499 II != E; ++II)
Chris Lattner0c315472009-12-09 07:08:01 +00001500 assert(II->getResult().getInst() != D && "Inst occurs as NLPD value");
Chris Lattnera28355d2008-12-07 08:50:20 +00001501 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001502
Chris Lattnerb8ec75b2008-11-29 21:25:10 +00001503 for (NonLocalDepMapType::const_iterator I = NonLocalDeps.begin(),
1504 E = NonLocalDeps.end(); I != E; ++I) {
1505 assert(I->first != D && "Inst occurs in data structures");
Chris Lattner44104272008-11-30 02:52:26 +00001506 const PerInstNLInfo &INLD = I->second;
Chris Lattner7e61daf2008-12-01 01:15:42 +00001507 for (NonLocalDepInfo::const_iterator II = INLD.first.begin(),
1508 EE = INLD.first.end(); II != EE; ++II)
Chris Lattner0c315472009-12-09 07:08:01 +00001509 assert(II->getResult().getInst() != D && "Inst occurs in data structures");
Chris Lattnerb8ec75b2008-11-29 21:25:10 +00001510 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001511
Chris Lattnerb8ec75b2008-11-29 21:25:10 +00001512 for (ReverseDepMapType::const_iterator I = ReverseLocalDeps.begin(),
Chris Lattner1b810bd2008-11-30 02:28:25 +00001513 E = ReverseLocalDeps.end(); I != E; ++I) {
1514 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerb8ec75b2008-11-29 21:25:10 +00001515 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1516 EE = I->second.end(); II != EE; ++II)
1517 assert(*II != D && "Inst occurs in data structures");
Chris Lattner1b810bd2008-11-30 02:28:25 +00001518 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001519
Chris Lattnerb8ec75b2008-11-29 21:25:10 +00001520 for (ReverseDepMapType::const_iterator I = ReverseNonLocalDeps.begin(),
1521 E = ReverseNonLocalDeps.end();
Chris Lattner1b810bd2008-11-30 02:28:25 +00001522 I != E; ++I) {
1523 assert(I->first != D && "Inst occurs in data structures");
Chris Lattnerb8ec75b2008-11-29 21:25:10 +00001524 for (SmallPtrSet<Instruction*, 4>::const_iterator II = I->second.begin(),
1525 EE = I->second.end(); II != EE; ++II)
1526 assert(*II != D && "Inst occurs in data structures");
Chris Lattner1b810bd2008-11-30 02:28:25 +00001527 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001528
Chris Lattnera28355d2008-12-07 08:50:20 +00001529 for (ReverseNonLocalPtrDepTy::const_iterator
1530 I = ReverseNonLocalPtrDeps.begin(),
1531 E = ReverseNonLocalPtrDeps.end(); I != E; ++I) {
1532 assert(I->first != D && "Inst occurs in rev NLPD map");
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001533
Chris Lattner8eda11b2009-03-29 00:24:04 +00001534 for (SmallPtrSet<ValueIsLoadPair, 4>::const_iterator II = I->second.begin(),
Chris Lattnera28355d2008-12-07 08:50:20 +00001535 E = I->second.end(); II != E; ++II)
Chris Lattner8eda11b2009-03-29 00:24:04 +00001536 assert(*II != ValueIsLoadPair(D, false) &&
1537 *II != ValueIsLoadPair(D, true) &&
Chris Lattnera28355d2008-12-07 08:50:20 +00001538 "Inst occurs in ReverseNonLocalPtrDeps map");
1539 }
Jakub Staszakb0a7eed2013-03-20 21:47:51 +00001540
Chris Lattnerb8ec75b2008-11-29 21:25:10 +00001541}