blob: 309a1d95efb6ab655bc6443b2e80c1620f8242a6 [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
Benjamin Kramerc321e532016-06-08 19:09:22 +000028static bool isAligned(const Value *Base, const APInt &Offset, unsigned Align,
Artur Pilipenko31bcca42016-02-24 12:49:04 +000029 const DataLayout &DL) {
30 APInt BaseAlign(Offset.getBitWidth(), Base->getPointerAlignment(DL));
31
32 if (!BaseAlign) {
33 Type *Ty = Base->getType()->getPointerElementType();
34 if (!Ty->isSized())
35 return false;
36 BaseAlign = DL.getABITypeAlignment(Ty);
37 }
38
39 APInt Alignment(Offset.getBitWidth(), Align);
40
41 assert(Alignment.isPowerOf2() && "must be a power of 2!");
42 return BaseAlign.uge(Alignment) && !(Offset & (Alignment-1));
43}
44
45static bool isAligned(const Value *Base, unsigned Align, const DataLayout &DL) {
46 Type *Ty = Base->getType();
47 assert(Ty->isSized() && "must be sized");
48 APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0);
49 return isAligned(Base, Offset, Align, DL);
50}
51
52/// Test if V is always a pointer to allocated and suitably aligned memory for
53/// a simple load or store.
54static bool isDereferenceableAndAlignedPointer(
Benjamin Kramerc321e532016-06-08 19:09:22 +000055 const Value *V, unsigned Align, const APInt &Size, const DataLayout &DL,
Artur Pilipenko31bcca42016-02-24 12:49:04 +000056 const Instruction *CtxI, const DominatorTree *DT,
Sean Silva45835e72016-07-02 23:47:27 +000057 SmallPtrSetImpl<const Value *> &Visited) {
Artur Pilipenko31bcca42016-02-24 12:49:04 +000058 // Note that it is not safe to speculate into a malloc'd region because
59 // malloc may return null.
60
Sanjoy Das10df4972016-06-01 16:47:45 +000061 // bitcast instructions are no-ops as far as dereferenceability is concerned.
62 if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V))
63 return isDereferenceableAndAlignedPointer(BC->getOperand(0), Align, Size,
Sean Silva45835e72016-07-02 23:47:27 +000064 DL, CtxI, DT, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +000065
Sanjoy Das48cad712016-06-02 00:52:53 +000066 bool CheckForNonNull = false;
67 APInt KnownDerefBytes(Size.getBitWidth(),
68 V->getPointerDereferenceableBytes(DL, CheckForNonNull));
69 if (KnownDerefBytes.getBoolValue()) {
70 if (KnownDerefBytes.uge(Size))
Sean Silva45835e72016-07-02 23:47:27 +000071 if (!CheckForNonNull || isKnownNonNullAt(V, CtxI, DT))
Sanjoy Das48cad712016-06-02 00:52:53 +000072 return isAligned(V, Align, DL);
73 }
Artur Pilipenko31bcca42016-02-24 12:49:04 +000074
75 // For GEPs, determine if the indexing lands within the allocated object.
76 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Artur Pilipenko31bcca42016-02-24 12:49:04 +000077 const Value *Base = GEP->getPointerOperand();
78
Artur Pilipenko31bcca42016-02-24 12:49:04 +000079 APInt Offset(DL.getPointerTypeSizeInBits(GEP->getType()), 0);
Sanjoy Das10df4972016-06-01 16:47:45 +000080 if (!GEP->accumulateConstantOffset(DL, Offset) || Offset.isNegative() ||
81 !Offset.urem(APInt(Offset.getBitWidth(), Align)).isMinValue())
Artur Pilipenko31bcca42016-02-24 12:49:04 +000082 return false;
83
Sanjoy Das10df4972016-06-01 16:47:45 +000084 // If the base pointer is dereferenceable for Offset+Size bytes, then the
85 // GEP (== Base + Offset) is dereferenceable for Size bytes. If the base
86 // pointer is aligned to Align bytes, and the Offset is divisible by Align
87 // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also
88 // aligned to Align bytes.
89
90 return Visited.insert(Base).second &&
91 isDereferenceableAndAlignedPointer(Base, Align, Offset + Size, DL,
Sean Silva45835e72016-07-02 23:47:27 +000092 CtxI, DT, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +000093 }
94
95 // For gc.relocate, look through relocations
96 if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V))
97 return isDereferenceableAndAlignedPointer(
Sean Silva45835e72016-07-02 23:47:27 +000098 RelocateInst->getDerivedPtr(), Align, Size, DL, CtxI, DT, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +000099
100 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
Sanjoy Das10df4972016-06-01 16:47:45 +0000101 return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, Size,
Sean Silva45835e72016-07-02 23:47:27 +0000102 DL, CtxI, DT, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000103
104 // If we don't know, assume the worst.
105 return false;
106}
107
108bool llvm::isDereferenceableAndAlignedPointer(const Value *V, unsigned Align,
109 const DataLayout &DL,
110 const Instruction *CtxI,
Sean Silva45835e72016-07-02 23:47:27 +0000111 const DominatorTree *DT) {
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000112 // When dereferenceability information is provided by a dereferenceable
113 // attribute, we know exactly how many bytes are dereferenceable. If we can
114 // determine the exact offset to the attributed variable, we can use that
115 // information here.
116 Type *VTy = V->getType();
117 Type *Ty = VTy->getPointerElementType();
118
119 // Require ABI alignment for loads without alignment specification
120 if (Align == 0)
121 Align = DL.getABITypeAlignment(Ty);
122
Sanjoy Das10df4972016-06-01 16:47:45 +0000123 if (!Ty->isSized())
124 return false;
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000125
126 SmallPtrSet<const Value *, 32> Visited;
Sanjoy Das10df4972016-06-01 16:47:45 +0000127 return ::isDereferenceableAndAlignedPointer(
128 V, Align, APInt(DL.getTypeSizeInBits(VTy), DL.getTypeStoreSize(Ty)), DL,
Sean Silva45835e72016-07-02 23:47:27 +0000129 CtxI, DT, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000130}
131
132bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL,
133 const Instruction *CtxI,
Sean Silva45835e72016-07-02 23:47:27 +0000134 const DominatorTree *DT) {
135 return isDereferenceableAndAlignedPointer(V, 1, DL, CtxI, DT);
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000136}
137
Chandler Carruthb56052f2014-10-18 23:31:55 +0000138/// \brief Test if A and B will obviously have the same value.
139///
140/// This includes recognizing that %t0 and %t1 will have the same
Dan Gohman826bdf82010-05-28 16:19:17 +0000141/// value in code like this:
Chandler Carruthb56052f2014-10-18 23:31:55 +0000142/// \code
Dan Gohman826bdf82010-05-28 16:19:17 +0000143/// %t0 = getelementptr \@a, 0, 3
144/// store i32 0, i32* %t0
145/// %t1 = getelementptr \@a, 0, 3
146/// %t2 = load i32* %t1
Chandler Carruthb56052f2014-10-18 23:31:55 +0000147/// \endcode
Dan Gohman826bdf82010-05-28 16:19:17 +0000148///
149static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
150 // Test if the values are trivially equivalent.
Chandler Carruthbe49df32014-10-18 23:41:25 +0000151 if (A == B)
152 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +0000153
Dan Gohman826bdf82010-05-28 16:19:17 +0000154 // Test if the values come from identical arithmetic instructions.
155 // Use isIdenticalToWhenDefined instead of isIdenticalTo because
156 // this function is only used when one address use dominates the
157 // other, which means that they'll always either have the same
158 // value or one of them will have an undefined value.
Chandler Carruthbe49df32014-10-18 23:41:25 +0000159 if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
160 isa<GetElementPtrInst>(A))
Dan Gohman826bdf82010-05-28 16:19:17 +0000161 if (const Instruction *BI = dyn_cast<Instruction>(B))
162 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
163 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +0000164
Dan Gohman826bdf82010-05-28 16:19:17 +0000165 // Otherwise they may not be equivalent.
166 return false;
167}
168
Chandler Carruth1f27f032014-10-18 23:46:17 +0000169/// \brief Check if executing a load of this pointer value cannot trap.
170///
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000171/// If DT and ScanFrom are specified this method performs context-sensitive
172/// analysis and returns true if it is safe to load immediately before ScanFrom.
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000173///
Chandler Carruth1f27f032014-10-18 23:46:17 +0000174/// If it is not obviously safe to load from the specified pointer, we do
175/// a quick local scan of the basic block containing \c ScanFrom, to determine
176/// if the address is already accessed.
177///
178/// This uses the pointee type to determine how many bytes need to be safe to
179/// load from the pointer.
Artur Pilipenko6dd69692016-01-15 15:27:46 +0000180bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align,
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000181 const DataLayout &DL,
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000182 Instruction *ScanFrom,
Sean Silva45835e72016-07-02 23:47:27 +0000183 const DominatorTree *DT) {
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000184 // Zero alignment means that the load has the ABI alignment for the target
185 if (Align == 0)
186 Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
187 assert(isPowerOf2_32(Align));
188
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000189 // If DT is not specified we can't make context-sensitive query
190 const Instruction* CtxI = DT ? ScanFrom : nullptr;
Sean Silva45835e72016-07-02 23:47:27 +0000191 if (isDereferenceableAndAlignedPointer(V, Align, DL, CtxI, DT))
Artur Pilipenkof84dc062016-01-17 12:35:29 +0000192 return true;
193
Nuno Lopes69dcc7d2012-12-31 17:42:11 +0000194 int64_t ByteOffset = 0;
Dan Gohman826bdf82010-05-28 16:19:17 +0000195 Value *Base = V;
Chandler Carruth38e98d52014-10-18 23:47:22 +0000196 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL);
Nuno Lopes69dcc7d2012-12-31 17:42:11 +0000197
198 if (ByteOffset < 0) // out of bounds
199 return false;
Dan Gohman826bdf82010-05-28 16:19:17 +0000200
Craig Topper9f008862014-04-15 04:59:12 +0000201 Type *BaseType = nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000202 unsigned BaseAlign = 0;
203 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
204 // An alloca is safe to load from as load as it is suitably aligned.
205 BaseType = AI->getAllocatedType();
206 BaseAlign = AI->getAlignment();
Nuno Lopes69dcc7d2012-12-31 17:42:11 +0000207 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
Chandler Carruth8a993732014-10-19 00:42:16 +0000208 // Global variables are not necessarily safe to load from if they are
Sanjoy Das5ce32722016-04-08 00:48:30 +0000209 // interposed arbitrarily. Their size may change or they may be weak and
210 // require a test to determine if they were in fact provided.
211 if (!GV->isInterposable()) {
Dan Gohman826bdf82010-05-28 16:19:17 +0000212 BaseType = GV->getType()->getElementType();
213 BaseAlign = GV->getAlignment();
214 }
215 }
216
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000217 PointerType *AddrTy = cast<PointerType>(V->getType());
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000218 uint64_t LoadSize = DL.getTypeStoreSize(AddrTy->getElementType());
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000219
Chandler Carruth8a993732014-10-19 00:42:16 +0000220 // If we found a base allocated type from either an alloca or global variable,
221 // try to see if we are definitively within the allocated region. We need to
222 // know the size of the base type and the loaded type to do anything in this
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000223 // case.
224 if (BaseType && BaseType->isSized()) {
Chandler Carruth8a993732014-10-19 00:42:16 +0000225 if (BaseAlign == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000226 BaseAlign = DL.getPrefTypeAlignment(BaseType);
Dan Gohman826bdf82010-05-28 16:19:17 +0000227
228 if (Align <= BaseAlign) {
Dan Gohman826bdf82010-05-28 16:19:17 +0000229 // Check if the load is within the bounds of the underlying object.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000230 if (ByteOffset + LoadSize <= DL.getTypeAllocSize(BaseType) &&
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000231 ((ByteOffset % Align) == 0))
Dan Gohman826bdf82010-05-28 16:19:17 +0000232 return true;
233 }
234 }
235
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000236 if (!ScanFrom)
237 return false;
238
Dan Gohman826bdf82010-05-28 16:19:17 +0000239 // Otherwise, be a little bit aggressive by scanning the local block where we
240 // want to check to see if the pointer is already being loaded or stored
241 // from/to. If so, the previous load or store would have already trapped,
242 // so there is no harm doing an extra load (also, CSE will later eliminate
243 // the load entirely).
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000244 BasicBlock::iterator BBI = ScanFrom->getIterator(),
245 E = ScanFrom->getParent()->begin();
Dan Gohman826bdf82010-05-28 16:19:17 +0000246
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000247 // We can at least always strip pointer casts even though we can't use the
248 // base here.
249 V = V->stripPointerCasts();
250
Dan Gohman826bdf82010-05-28 16:19:17 +0000251 while (BBI != E) {
252 --BBI;
253
254 // If we see a free or a call which may write to memory (i.e. which might do
255 // a free) the pointer could be marked invalid.
256 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
257 !isa<DbgInfoIntrinsic>(BBI))
258 return false;
259
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000260 Value *AccessedPtr;
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000261 unsigned AccessedAlign;
262 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000263 AccessedPtr = LI->getPointerOperand();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000264 AccessedAlign = LI->getAlignment();
265 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000266 AccessedPtr = SI->getPointerOperand();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000267 AccessedAlign = SI->getAlignment();
268 } else
269 continue;
270
271 Type *AccessedTy = AccessedPtr->getType()->getPointerElementType();
272 if (AccessedAlign == 0)
273 AccessedAlign = DL.getABITypeAlignment(AccessedTy);
274 if (AccessedAlign < Align)
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000275 continue;
276
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000277 // Handle trivial cases.
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000278 if (AccessedPtr == V)
279 return true;
280
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000281 if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000282 LoadSize <= DL.getTypeStoreSize(AccessedTy))
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000283 return true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000284 }
285 return false;
286}
287
Larisse Voufo532bf712015-09-18 19:14:35 +0000288/// DefMaxInstsToScan - the default number of maximum instructions
289/// to scan in the block, used by FindAvailableLoadedValue().
290/// FindAvailableLoadedValue() was introduced in r60148, to improve jump
291/// threading in part by eliminating partially redundant loads.
292/// At that point, the value of MaxInstsToScan was already set to '6'
293/// without documented explanation.
294cl::opt<unsigned>
295llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
296 cl::desc("Use this to specify the default maximum number of instructions "
297 "to scan backward from a given instruction, when searching for "
298 "available loaded value"));
299
Anna Thomas9ad45ad2016-07-08 22:15:08 +0000300Value *llvm::FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB,
Dan Gohman826bdf82010-05-28 16:19:17 +0000301 BasicBlock::iterator &ScanFrom,
302 unsigned MaxInstsToScan,
Eli Friedmanbd254a62016-06-16 02:33:42 +0000303 AliasAnalysis *AA, AAMDNodes *AATags,
304 bool *IsLoadCSE) {
Chandler Carruthd67244d2014-10-18 23:19:03 +0000305 if (MaxInstsToScan == 0)
306 MaxInstsToScan = ~0U;
Anna Thomas9ad45ad2016-07-08 22:15:08 +0000307
308 Value *Ptr = Load->getPointerOperand();
309 Type *AccessTy = Load->getType();
310
311 // We can never remove a volatile load
312 if (Load->isVolatile())
313 return nullptr;
314
315 // Anything stronger than unordered is currently unimplemented.
316 if (!Load->isUnordered())
317 return nullptr;
318
Mehdi Amini46a43552015-03-04 18:43:29 +0000319 const DataLayout &DL = ScanBB->getModule()->getDataLayout();
Anna Thomas9ad45ad2016-07-08 22:15:08 +0000320
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000321 // Try to get the store size for the type.
Mehdi Amini46a43552015-03-04 18:43:29 +0000322 uint64_t AccessSize = DL.getTypeStoreSize(AccessTy);
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000323
324 Value *StrippedPtr = Ptr->stripPointerCasts();
Chandler Carruthd67244d2014-10-18 23:19:03 +0000325
Dan Gohman826bdf82010-05-28 16:19:17 +0000326 while (ScanFrom != ScanBB->begin()) {
327 // We must ignore debug info directives when counting (otherwise they
328 // would affect codegen).
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000329 Instruction *Inst = &*--ScanFrom;
Dan Gohman826bdf82010-05-28 16:19:17 +0000330 if (isa<DbgInfoIntrinsic>(Inst))
331 continue;
332
333 // Restore ScanFrom to expected value in case next test succeeds
334 ScanFrom++;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000335
Dan Gohman826bdf82010-05-28 16:19:17 +0000336 // Don't scan huge blocks.
Chandler Carruthd67244d2014-10-18 23:19:03 +0000337 if (MaxInstsToScan-- == 0)
338 return nullptr;
339
Dan Gohman826bdf82010-05-28 16:19:17 +0000340 --ScanFrom;
341 // If this is a load of Ptr, the loaded value is available.
Eli Friedman4419cd22011-08-15 21:56:39 +0000342 // (This is true even if the load is volatile or atomic, although
343 // those cases are unlikely.)
Reid Klecknerfbd5eef2016-06-24 18:42:58 +0000344 if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
345 if (AreEquivalentAddressValues(
346 LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000347 CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
Philip Reames92c43692016-04-21 16:51:08 +0000348
349 // We can value forward from an atomic to a non-atomic, but not the
350 // other way around.
Anna Thomas9ad45ad2016-07-08 22:15:08 +0000351 if (LI->isAtomic() < Load->isAtomic())
Philip Reames92c43692016-04-21 16:51:08 +0000352 return nullptr;
353
Chandler Carruthd67244d2014-10-18 23:19:03 +0000354 if (AATags)
355 LI->getAAMetadata(*AATags);
Eli Friedmanbd254a62016-06-16 02:33:42 +0000356 if (IsLoadCSE)
357 *IsLoadCSE = true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000358 return LI;
Chris Lattner87fa77b2012-03-13 18:07:41 +0000359 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000360
Dan Gohman826bdf82010-05-28 16:19:17 +0000361 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000362 Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
Dan Gohman826bdf82010-05-28 16:19:17 +0000363 // If this is a store through Ptr, the value is available!
Eli Friedman4419cd22011-08-15 21:56:39 +0000364 // (This is true even if the store is volatile or atomic, although
365 // those cases are unlikely.)
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000366 if (AreEquivalentAddressValues(StorePtr, StrippedPtr) &&
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000367 CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(),
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000368 AccessTy, DL)) {
Philip Reames92c43692016-04-21 16:51:08 +0000369
370 // We can value forward from an atomic to a non-atomic, but not the
371 // other way around.
Anna Thomas9ad45ad2016-07-08 22:15:08 +0000372 if (SI->isAtomic() < Load->isAtomic())
Philip Reames92c43692016-04-21 16:51:08 +0000373 return nullptr;
374
Chandler Carruthd67244d2014-10-18 23:19:03 +0000375 if (AATags)
376 SI->getAAMetadata(*AATags);
Dan Gohman826bdf82010-05-28 16:19:17 +0000377 return SI->getOperand(0);
Chris Lattner87fa77b2012-03-13 18:07:41 +0000378 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000379
Chandler Carrutha32038b2014-10-20 10:03:01 +0000380 // If both StrippedPtr and StorePtr reach all the way to an alloca or
381 // global and they are different, ignore the store. This is a trivial form
382 // of alias analysis that is important for reg2mem'd code.
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000383 if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
Chandler Carrutha32038b2014-10-20 10:03:01 +0000384 (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
385 StrippedPtr != StorePtr)
Dan Gohman826bdf82010-05-28 16:19:17 +0000386 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000387
Dan Gohman826bdf82010-05-28 16:19:17 +0000388 // If we have alias analysis and it says the store won't modify the loaded
389 // value, ignore the store.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000390 if (AA && (AA->getModRefInfo(SI, StrippedPtr, AccessSize) & MRI_Mod) == 0)
Dan Gohman826bdf82010-05-28 16:19:17 +0000391 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000392
Dan Gohman826bdf82010-05-28 16:19:17 +0000393 // Otherwise the store that may or may not alias the pointer, bail out.
394 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000395 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000396 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000397
Dan Gohman826bdf82010-05-28 16:19:17 +0000398 // If this is some other instruction that may clobber Ptr, bail out.
399 if (Inst->mayWriteToMemory()) {
400 // If alias analysis claims that it really won't modify the load,
401 // ignore it.
402 if (AA &&
Chandler Carruth194f59c2015-07-22 23:15:57 +0000403 (AA->getModRefInfo(Inst, StrippedPtr, AccessSize) & MRI_Mod) == 0)
Dan Gohman826bdf82010-05-28 16:19:17 +0000404 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000405
Dan Gohman826bdf82010-05-28 16:19:17 +0000406 // May modify the pointer, bail out.
407 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000408 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000409 }
410 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000411
Dan Gohman826bdf82010-05-28 16:19:17 +0000412 // Got to the start of the block, we didn't find it, but are done for this
413 // block.
Craig Topper9f008862014-04-15 04:59:12 +0000414 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000415}