blob: 21503ef668be9899bd11a9c6795cff40c6d326f2 [file] [log] [blame]
Dan Gohman826bdf82010-05-28 16:19:17 +00001//===- Loads.cpp - Local load analysis ------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines simple local analyses for load instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/Loads.h"
15#include "llvm/Analysis/AliasAnalysis.h"
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000016#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/GlobalAlias.h"
19#include "llvm/IR/GlobalVariable.h"
20#include "llvm/IR/IntrinsicInst.h"
21#include "llvm/IR/LLVMContext.h"
Mehdi Amini9a9738f2015-03-03 22:01:13 +000022#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Operator.h"
Artur Pilipenko31bcca42016-02-24 12:49:04 +000024#include "llvm/IR/Statepoint.h"
25
Dan Gohman826bdf82010-05-28 16:19:17 +000026using namespace llvm;
27
Artur Pilipenko31bcca42016-02-24 12:49:04 +000028static bool isDereferenceableFromAttribute(const Value *BV, APInt Offset,
29 Type *Ty, const DataLayout &DL,
30 const Instruction *CtxI,
31 const DominatorTree *DT,
32 const TargetLibraryInfo *TLI) {
33 assert(Offset.isNonNegative() && "offset can't be negative");
34 assert(Ty->isSized() && "must be sized");
35
36 APInt DerefBytes(Offset.getBitWidth(), 0);
37 bool CheckForNonNull = false;
38 if (const Argument *A = dyn_cast<Argument>(BV)) {
39 DerefBytes = A->getDereferenceableBytes();
40 if (!DerefBytes.getBoolValue()) {
41 DerefBytes = A->getDereferenceableOrNullBytes();
42 CheckForNonNull = true;
43 }
44 } else if (auto CS = ImmutableCallSite(BV)) {
45 DerefBytes = CS.getDereferenceableBytes(0);
46 if (!DerefBytes.getBoolValue()) {
47 DerefBytes = CS.getDereferenceableOrNullBytes(0);
48 CheckForNonNull = true;
49 }
50 } else if (const LoadInst *LI = dyn_cast<LoadInst>(BV)) {
51 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
52 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
53 DerefBytes = CI->getLimitedValue();
54 }
55 if (!DerefBytes.getBoolValue()) {
56 if (MDNode *MD =
57 LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
58 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
59 DerefBytes = CI->getLimitedValue();
60 }
61 CheckForNonNull = true;
62 }
63 }
64
65 if (DerefBytes.getBoolValue())
66 if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty)))
67 if (!CheckForNonNull || isKnownNonNullAt(BV, CtxI, DT, TLI))
68 return true;
69
70 return false;
71}
72
73static bool isDereferenceableFromAttribute(const Value *V, const DataLayout &DL,
74 const Instruction *CtxI,
75 const DominatorTree *DT,
76 const TargetLibraryInfo *TLI) {
77 Type *VTy = V->getType();
78 Type *Ty = VTy->getPointerElementType();
79 if (!Ty->isSized())
80 return false;
81
82 APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0);
83 return isDereferenceableFromAttribute(V, Offset, Ty, DL, CtxI, DT, TLI);
84}
85
86static bool isAligned(const Value *Base, APInt Offset, unsigned Align,
87 const DataLayout &DL) {
88 APInt BaseAlign(Offset.getBitWidth(), Base->getPointerAlignment(DL));
89
90 if (!BaseAlign) {
91 Type *Ty = Base->getType()->getPointerElementType();
92 if (!Ty->isSized())
93 return false;
94 BaseAlign = DL.getABITypeAlignment(Ty);
95 }
96
97 APInt Alignment(Offset.getBitWidth(), Align);
98
99 assert(Alignment.isPowerOf2() && "must be a power of 2!");
100 return BaseAlign.uge(Alignment) && !(Offset & (Alignment-1));
101}
102
103static bool isAligned(const Value *Base, unsigned Align, const DataLayout &DL) {
104 Type *Ty = Base->getType();
105 assert(Ty->isSized() && "must be sized");
106 APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0);
107 return isAligned(Base, Offset, Align, DL);
108}
109
110/// Test if V is always a pointer to allocated and suitably aligned memory for
111/// a simple load or store.
112static bool isDereferenceableAndAlignedPointer(
113 const Value *V, unsigned Align, const DataLayout &DL,
114 const Instruction *CtxI, const DominatorTree *DT,
115 const TargetLibraryInfo *TLI, SmallPtrSetImpl<const Value *> &Visited) {
116 // Note that it is not safe to speculate into a malloc'd region because
117 // malloc may return null.
118
119 // These are obviously ok if aligned.
120 if (isa<AllocaInst>(V))
121 return isAligned(V, Align, DL);
122
123 // It's not always safe to follow a bitcast, for example:
124 // bitcast i8* (alloca i8) to i32*
125 // would result in a 4-byte load from a 1-byte alloca. However,
126 // if we're casting from a pointer from a type of larger size
127 // to a type of smaller size (or the same size), and the alignment
128 // is at least as large as for the resulting pointer type, then
129 // we can look through the bitcast.
130 if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) {
131 Type *STy = BC->getSrcTy()->getPointerElementType(),
132 *DTy = BC->getDestTy()->getPointerElementType();
133 if (STy->isSized() && DTy->isSized() &&
134 (DL.getTypeStoreSize(STy) >= DL.getTypeStoreSize(DTy)) &&
135 (DL.getABITypeAlignment(STy) >= DL.getABITypeAlignment(DTy)))
136 return isDereferenceableAndAlignedPointer(BC->getOperand(0), Align, DL,
137 CtxI, DT, TLI, Visited);
138 }
139
140 // Global variables which can't collapse to null are ok.
141 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
142 if (!GV->hasExternalWeakLinkage())
143 return isAligned(V, Align, DL);
144
145 // byval arguments are okay.
146 if (const Argument *A = dyn_cast<Argument>(V))
147 if (A->hasByValAttr())
148 return isAligned(V, Align, DL);
149
150 if (isDereferenceableFromAttribute(V, DL, CtxI, DT, TLI))
151 return isAligned(V, Align, DL);
152
153 // For GEPs, determine if the indexing lands within the allocated object.
154 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
155 Type *Ty = GEP->getResultElementType();
156 const Value *Base = GEP->getPointerOperand();
157
158 // Conservatively require that the base pointer be fully dereferenceable
159 // and aligned.
160 if (!Visited.insert(Base).second)
161 return false;
162 if (!isDereferenceableAndAlignedPointer(Base, Align, DL, CtxI, DT, TLI,
163 Visited))
164 return false;
165
166 APInt Offset(DL.getPointerTypeSizeInBits(GEP->getType()), 0);
167 if (!GEP->accumulateConstantOffset(DL, Offset))
168 return false;
169
170 // Check if the load is within the bounds of the underlying object
171 // and offset is aligned.
172 uint64_t LoadSize = DL.getTypeStoreSize(Ty);
173 Type *BaseType = GEP->getSourceElementType();
174 assert(isPowerOf2_32(Align) && "must be a power of 2!");
175 return (Offset + LoadSize).ule(DL.getTypeAllocSize(BaseType)) &&
176 !(Offset & APInt(Offset.getBitWidth(), Align-1));
177 }
178
179 // For gc.relocate, look through relocations
180 if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V))
181 return isDereferenceableAndAlignedPointer(
182 RelocateInst->getDerivedPtr(), Align, DL, CtxI, DT, TLI, Visited);
183
184 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
185 return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, DL,
186 CtxI, DT, TLI, Visited);
187
188 // If we don't know, assume the worst.
189 return false;
190}
191
192bool llvm::isDereferenceableAndAlignedPointer(const Value *V, unsigned Align,
193 const DataLayout &DL,
194 const Instruction *CtxI,
195 const DominatorTree *DT,
196 const TargetLibraryInfo *TLI) {
197 // When dereferenceability information is provided by a dereferenceable
198 // attribute, we know exactly how many bytes are dereferenceable. If we can
199 // determine the exact offset to the attributed variable, we can use that
200 // information here.
201 Type *VTy = V->getType();
202 Type *Ty = VTy->getPointerElementType();
203
204 // Require ABI alignment for loads without alignment specification
205 if (Align == 0)
206 Align = DL.getABITypeAlignment(Ty);
207
208 if (Ty->isSized()) {
209 APInt Offset(DL.getTypeStoreSizeInBits(VTy), 0);
210 const Value *BV = V->stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
211
212 if (Offset.isNonNegative())
213 if (isDereferenceableFromAttribute(BV, Offset, Ty, DL, CtxI, DT, TLI) &&
214 isAligned(BV, Offset, Align, DL))
215 return true;
216 }
217
218 SmallPtrSet<const Value *, 32> Visited;
219 return ::isDereferenceableAndAlignedPointer(V, Align, DL, CtxI, DT, TLI,
220 Visited);
221}
222
223bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL,
224 const Instruction *CtxI,
225 const DominatorTree *DT,
226 const TargetLibraryInfo *TLI) {
227 return isDereferenceableAndAlignedPointer(V, 1, DL, CtxI, DT, TLI);
228}
229
Chandler Carruthb56052f2014-10-18 23:31:55 +0000230/// \brief Test if A and B will obviously have the same value.
231///
232/// This includes recognizing that %t0 and %t1 will have the same
Dan Gohman826bdf82010-05-28 16:19:17 +0000233/// value in code like this:
Chandler Carruthb56052f2014-10-18 23:31:55 +0000234/// \code
Dan Gohman826bdf82010-05-28 16:19:17 +0000235/// %t0 = getelementptr \@a, 0, 3
236/// store i32 0, i32* %t0
237/// %t1 = getelementptr \@a, 0, 3
238/// %t2 = load i32* %t1
Chandler Carruthb56052f2014-10-18 23:31:55 +0000239/// \endcode
Dan Gohman826bdf82010-05-28 16:19:17 +0000240///
241static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
242 // Test if the values are trivially equivalent.
Chandler Carruthbe49df32014-10-18 23:41:25 +0000243 if (A == B)
244 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +0000245
Dan Gohman826bdf82010-05-28 16:19:17 +0000246 // Test if the values come from identical arithmetic instructions.
247 // Use isIdenticalToWhenDefined instead of isIdenticalTo because
248 // this function is only used when one address use dominates the
249 // other, which means that they'll always either have the same
250 // value or one of them will have an undefined value.
Chandler Carruthbe49df32014-10-18 23:41:25 +0000251 if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
252 isa<GetElementPtrInst>(A))
Dan Gohman826bdf82010-05-28 16:19:17 +0000253 if (const Instruction *BI = dyn_cast<Instruction>(B))
254 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
255 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +0000256
Dan Gohman826bdf82010-05-28 16:19:17 +0000257 // Otherwise they may not be equivalent.
258 return false;
259}
260
Chandler Carruth1f27f032014-10-18 23:46:17 +0000261/// \brief Check if executing a load of this pointer value cannot trap.
262///
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000263/// If DT is specified this method performs context-sensitive analysis.
264///
Chandler Carruth1f27f032014-10-18 23:46:17 +0000265/// If it is not obviously safe to load from the specified pointer, we do
266/// a quick local scan of the basic block containing \c ScanFrom, to determine
267/// if the address is already accessed.
268///
269/// This uses the pointee type to determine how many bytes need to be safe to
270/// load from the pointer.
Artur Pilipenko6dd69692016-01-15 15:27:46 +0000271bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align,
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000272 Instruction *ScanFrom,
273 const DominatorTree *DT,
274 const TargetLibraryInfo *TLI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000275 const DataLayout &DL = ScanFrom->getModule()->getDataLayout();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000276
277 // Zero alignment means that the load has the ABI alignment for the target
278 if (Align == 0)
279 Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
280 assert(isPowerOf2_32(Align));
281
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000282 // If DT is not specified we can't make context-sensitive query
283 const Instruction* CtxI = DT ? ScanFrom : nullptr;
284 if (isDereferenceableAndAlignedPointer(V, Align, DL, CtxI, DT, TLI))
Artur Pilipenkof84dc062016-01-17 12:35:29 +0000285 return true;
286
Nuno Lopes69dcc7d2012-12-31 17:42:11 +0000287 int64_t ByteOffset = 0;
Dan Gohman826bdf82010-05-28 16:19:17 +0000288 Value *Base = V;
Chandler Carruth38e98d52014-10-18 23:47:22 +0000289 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL);
Nuno Lopes69dcc7d2012-12-31 17:42:11 +0000290
291 if (ByteOffset < 0) // out of bounds
292 return false;
Dan Gohman826bdf82010-05-28 16:19:17 +0000293
Craig Topper9f008862014-04-15 04:59:12 +0000294 Type *BaseType = nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000295 unsigned BaseAlign = 0;
296 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
297 // An alloca is safe to load from as load as it is suitably aligned.
298 BaseType = AI->getAllocatedType();
299 BaseAlign = AI->getAlignment();
Nuno Lopes69dcc7d2012-12-31 17:42:11 +0000300 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
Chandler Carruth8a993732014-10-19 00:42:16 +0000301 // Global variables are not necessarily safe to load from if they are
Sanjoy Das5ce32722016-04-08 00:48:30 +0000302 // interposed arbitrarily. Their size may change or they may be weak and
303 // require a test to determine if they were in fact provided.
304 if (!GV->isInterposable()) {
Dan Gohman826bdf82010-05-28 16:19:17 +0000305 BaseType = GV->getType()->getElementType();
306 BaseAlign = GV->getAlignment();
307 }
308 }
309
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000310 PointerType *AddrTy = cast<PointerType>(V->getType());
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000311 uint64_t LoadSize = DL.getTypeStoreSize(AddrTy->getElementType());
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000312
Chandler Carruth8a993732014-10-19 00:42:16 +0000313 // If we found a base allocated type from either an alloca or global variable,
314 // try to see if we are definitively within the allocated region. We need to
315 // know the size of the base type and the loaded type to do anything in this
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000316 // case.
317 if (BaseType && BaseType->isSized()) {
Chandler Carruth8a993732014-10-19 00:42:16 +0000318 if (BaseAlign == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000319 BaseAlign = DL.getPrefTypeAlignment(BaseType);
Dan Gohman826bdf82010-05-28 16:19:17 +0000320
321 if (Align <= BaseAlign) {
Dan Gohman826bdf82010-05-28 16:19:17 +0000322 // Check if the load is within the bounds of the underlying object.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000323 if (ByteOffset + LoadSize <= DL.getTypeAllocSize(BaseType) &&
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000324 ((ByteOffset % Align) == 0))
Dan Gohman826bdf82010-05-28 16:19:17 +0000325 return true;
326 }
327 }
328
329 // Otherwise, be a little bit aggressive by scanning the local block where we
330 // want to check to see if the pointer is already being loaded or stored
331 // from/to. If so, the previous load or store would have already trapped,
332 // so there is no harm doing an extra load (also, CSE will later eliminate
333 // the load entirely).
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000334 BasicBlock::iterator BBI = ScanFrom->getIterator(),
335 E = ScanFrom->getParent()->begin();
Dan Gohman826bdf82010-05-28 16:19:17 +0000336
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000337 // We can at least always strip pointer casts even though we can't use the
338 // base here.
339 V = V->stripPointerCasts();
340
Dan Gohman826bdf82010-05-28 16:19:17 +0000341 while (BBI != E) {
342 --BBI;
343
344 // If we see a free or a call which may write to memory (i.e. which might do
345 // a free) the pointer could be marked invalid.
346 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
347 !isa<DbgInfoIntrinsic>(BBI))
348 return false;
349
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000350 Value *AccessedPtr;
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000351 unsigned AccessedAlign;
352 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000353 AccessedPtr = LI->getPointerOperand();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000354 AccessedAlign = LI->getAlignment();
355 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000356 AccessedPtr = SI->getPointerOperand();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000357 AccessedAlign = SI->getAlignment();
358 } else
359 continue;
360
361 Type *AccessedTy = AccessedPtr->getType()->getPointerElementType();
362 if (AccessedAlign == 0)
363 AccessedAlign = DL.getABITypeAlignment(AccessedTy);
364 if (AccessedAlign < Align)
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000365 continue;
366
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000367 // Handle trivial cases.
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000368 if (AccessedPtr == V)
369 return true;
370
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000371 if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000372 LoadSize <= DL.getTypeStoreSize(AccessedTy))
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000373 return true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000374 }
375 return false;
376}
377
Larisse Voufo532bf712015-09-18 19:14:35 +0000378/// DefMaxInstsToScan - the default number of maximum instructions
379/// to scan in the block, used by FindAvailableLoadedValue().
380/// FindAvailableLoadedValue() was introduced in r60148, to improve jump
381/// threading in part by eliminating partially redundant loads.
382/// At that point, the value of MaxInstsToScan was already set to '6'
383/// without documented explanation.
384cl::opt<unsigned>
385llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
386 cl::desc("Use this to specify the default maximum number of instructions "
387 "to scan backward from a given instruction, when searching for "
388 "available loaded value"));
389
Chandler Carruthb56052f2014-10-18 23:31:55 +0000390/// \brief Scan the ScanBB block backwards to see if we have the value at the
Dan Gohman826bdf82010-05-28 16:19:17 +0000391/// memory address *Ptr locally available within a small number of instructions.
Dan Gohman826bdf82010-05-28 16:19:17 +0000392///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000393/// The scan starts from \c ScanFrom. \c MaxInstsToScan specifies the maximum
394/// instructions to scan in the block. If it is set to \c 0, it will scan the whole
395/// block.
Dan Gohman826bdf82010-05-28 16:19:17 +0000396///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000397/// If the value is available, this function returns it. If not, it returns the
398/// iterator for the last validated instruction that the value would be live
399/// through. If we scanned the entire block and didn't find something that
400/// invalidates \c *Ptr or provides it, \c ScanFrom is left at the last
401/// instruction processed and this returns null.
Chris Lattner87fa77b2012-03-13 18:07:41 +0000402///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000403/// You can also optionally specify an alias analysis implementation, which
404/// makes this more precise.
405///
406/// If \c AATags is non-null and a load or store is found, the AA tags from the
407/// load or store are recorded there. If there are no AA tags or if no access is
408/// found, it is left unmodified.
Eduard Burtescue2a69172016-01-22 01:51:51 +0000409Value *llvm::FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB,
Dan Gohman826bdf82010-05-28 16:19:17 +0000410 BasicBlock::iterator &ScanFrom,
411 unsigned MaxInstsToScan,
Chandler Carruthd67244d2014-10-18 23:19:03 +0000412 AliasAnalysis *AA, AAMDNodes *AATags) {
413 if (MaxInstsToScan == 0)
414 MaxInstsToScan = ~0U;
Dan Gohman826bdf82010-05-28 16:19:17 +0000415
Eduard Burtescue2a69172016-01-22 01:51:51 +0000416 Value *Ptr = Load->getPointerOperand();
417 Type *AccessTy = Load->getType();
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000418
Mehdi Amini46a43552015-03-04 18:43:29 +0000419 const DataLayout &DL = ScanBB->getModule()->getDataLayout();
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000420
421 // Try to get the store size for the type.
Mehdi Amini46a43552015-03-04 18:43:29 +0000422 uint64_t AccessSize = DL.getTypeStoreSize(AccessTy);
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000423
424 Value *StrippedPtr = Ptr->stripPointerCasts();
Chandler Carruthd67244d2014-10-18 23:19:03 +0000425
Dan Gohman826bdf82010-05-28 16:19:17 +0000426 while (ScanFrom != ScanBB->begin()) {
427 // We must ignore debug info directives when counting (otherwise they
428 // would affect codegen).
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000429 Instruction *Inst = &*--ScanFrom;
Dan Gohman826bdf82010-05-28 16:19:17 +0000430 if (isa<DbgInfoIntrinsic>(Inst))
431 continue;
432
433 // Restore ScanFrom to expected value in case next test succeeds
434 ScanFrom++;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000435
Dan Gohman826bdf82010-05-28 16:19:17 +0000436 // Don't scan huge blocks.
Chandler Carruthd67244d2014-10-18 23:19:03 +0000437 if (MaxInstsToScan-- == 0)
438 return nullptr;
439
Dan Gohman826bdf82010-05-28 16:19:17 +0000440 --ScanFrom;
441 // If this is a load of Ptr, the loaded value is available.
Eli Friedman4419cd22011-08-15 21:56:39 +0000442 // (This is true even if the load is volatile or atomic, although
443 // those cases are unlikely.)
Dan Gohman826bdf82010-05-28 16:19:17 +0000444 if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000445 if (AreEquivalentAddressValues(
446 LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000447 CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
Chandler Carruthd67244d2014-10-18 23:19:03 +0000448 if (AATags)
449 LI->getAAMetadata(*AATags);
Dan Gohman826bdf82010-05-28 16:19:17 +0000450 return LI;
Chris Lattner87fa77b2012-03-13 18:07:41 +0000451 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000452
Dan Gohman826bdf82010-05-28 16:19:17 +0000453 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000454 Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
Dan Gohman826bdf82010-05-28 16:19:17 +0000455 // If this is a store through Ptr, the value is available!
Eli Friedman4419cd22011-08-15 21:56:39 +0000456 // (This is true even if the store is volatile or atomic, although
457 // those cases are unlikely.)
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000458 if (AreEquivalentAddressValues(StorePtr, StrippedPtr) &&
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000459 CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(),
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000460 AccessTy, DL)) {
Chandler Carruthd67244d2014-10-18 23:19:03 +0000461 if (AATags)
462 SI->getAAMetadata(*AATags);
Dan Gohman826bdf82010-05-28 16:19:17 +0000463 return SI->getOperand(0);
Chris Lattner87fa77b2012-03-13 18:07:41 +0000464 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000465
Chandler Carrutha32038b2014-10-20 10:03:01 +0000466 // If both StrippedPtr and StorePtr reach all the way to an alloca or
467 // global and they are different, ignore the store. This is a trivial form
468 // of alias analysis that is important for reg2mem'd code.
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000469 if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
Chandler Carrutha32038b2014-10-20 10:03:01 +0000470 (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
471 StrippedPtr != StorePtr)
Dan Gohman826bdf82010-05-28 16:19:17 +0000472 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000473
Dan Gohman826bdf82010-05-28 16:19:17 +0000474 // If we have alias analysis and it says the store won't modify the loaded
475 // value, ignore the store.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000476 if (AA && (AA->getModRefInfo(SI, StrippedPtr, AccessSize) & MRI_Mod) == 0)
Dan Gohman826bdf82010-05-28 16:19:17 +0000477 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000478
Dan Gohman826bdf82010-05-28 16:19:17 +0000479 // Otherwise the store that may or may not alias the pointer, bail out.
480 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000481 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000482 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000483
Dan Gohman826bdf82010-05-28 16:19:17 +0000484 // If this is some other instruction that may clobber Ptr, bail out.
485 if (Inst->mayWriteToMemory()) {
486 // If alias analysis claims that it really won't modify the load,
487 // ignore it.
488 if (AA &&
Chandler Carruth194f59c2015-07-22 23:15:57 +0000489 (AA->getModRefInfo(Inst, StrippedPtr, AccessSize) & MRI_Mod) == 0)
Dan Gohman826bdf82010-05-28 16:19:17 +0000490 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000491
Dan Gohman826bdf82010-05-28 16:19:17 +0000492 // May modify the pointer, bail out.
493 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000494 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000495 }
496 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000497
Dan Gohman826bdf82010-05-28 16:19:17 +0000498 // Got to the start of the block, we didn't find it, but are done for this
499 // block.
Craig Topper9f008862014-04-15 04:59:12 +0000500 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000501}