blob: 7d3fd5951eb5525952e4a282552c352456996cb9 [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,
57 const TargetLibraryInfo *TLI, SmallPtrSetImpl<const Value *> &Visited) {
58 // 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,
64 DL, CtxI, DT, TLI, 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))
71 if (!CheckForNonNull || isKnownNonNullAt(V, CtxI, DT, TLI))
72 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,
92 CtxI, DT, TLI, 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(
Sanjoy Das10df4972016-06-01 16:47:45 +000098 RelocateInst->getDerivedPtr(), Align, Size, DL, CtxI, DT, TLI, 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,
102 DL, CtxI, DT, TLI, 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,
111 const DominatorTree *DT,
112 const TargetLibraryInfo *TLI) {
113 // When dereferenceability information is provided by a dereferenceable
114 // attribute, we know exactly how many bytes are dereferenceable. If we can
115 // determine the exact offset to the attributed variable, we can use that
116 // information here.
117 Type *VTy = V->getType();
118 Type *Ty = VTy->getPointerElementType();
119
120 // Require ABI alignment for loads without alignment specification
121 if (Align == 0)
122 Align = DL.getABITypeAlignment(Ty);
123
Sanjoy Das10df4972016-06-01 16:47:45 +0000124 if (!Ty->isSized())
125 return false;
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000126
127 SmallPtrSet<const Value *, 32> Visited;
Sanjoy Das10df4972016-06-01 16:47:45 +0000128 return ::isDereferenceableAndAlignedPointer(
129 V, Align, APInt(DL.getTypeSizeInBits(VTy), DL.getTypeStoreSize(Ty)), DL,
130 CtxI, DT, TLI, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000131}
132
133bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL,
134 const Instruction *CtxI,
135 const DominatorTree *DT,
136 const TargetLibraryInfo *TLI) {
137 return isDereferenceableAndAlignedPointer(V, 1, DL, CtxI, DT, TLI);
138}
139
Chandler Carruthb56052f2014-10-18 23:31:55 +0000140/// \brief Test if A and B will obviously have the same value.
141///
142/// This includes recognizing that %t0 and %t1 will have the same
Dan Gohman826bdf82010-05-28 16:19:17 +0000143/// value in code like this:
Chandler Carruthb56052f2014-10-18 23:31:55 +0000144/// \code
Dan Gohman826bdf82010-05-28 16:19:17 +0000145/// %t0 = getelementptr \@a, 0, 3
146/// store i32 0, i32* %t0
147/// %t1 = getelementptr \@a, 0, 3
148/// %t2 = load i32* %t1
Chandler Carruthb56052f2014-10-18 23:31:55 +0000149/// \endcode
Dan Gohman826bdf82010-05-28 16:19:17 +0000150///
151static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
152 // Test if the values are trivially equivalent.
Chandler Carruthbe49df32014-10-18 23:41:25 +0000153 if (A == B)
154 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +0000155
Dan Gohman826bdf82010-05-28 16:19:17 +0000156 // Test if the values come from identical arithmetic instructions.
157 // Use isIdenticalToWhenDefined instead of isIdenticalTo because
158 // this function is only used when one address use dominates the
159 // other, which means that they'll always either have the same
160 // value or one of them will have an undefined value.
Chandler Carruthbe49df32014-10-18 23:41:25 +0000161 if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
162 isa<GetElementPtrInst>(A))
Dan Gohman826bdf82010-05-28 16:19:17 +0000163 if (const Instruction *BI = dyn_cast<Instruction>(B))
164 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
165 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +0000166
Dan Gohman826bdf82010-05-28 16:19:17 +0000167 // Otherwise they may not be equivalent.
168 return false;
169}
170
Chandler Carruth1f27f032014-10-18 23:46:17 +0000171/// \brief Check if executing a load of this pointer value cannot trap.
172///
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000173/// If DT and ScanFrom are specified this method performs context-sensitive
174/// analysis and returns true if it is safe to load immediately before ScanFrom.
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000175///
Chandler Carruth1f27f032014-10-18 23:46:17 +0000176/// If it is not obviously safe to load from the specified pointer, we do
177/// a quick local scan of the basic block containing \c ScanFrom, to determine
178/// if the address is already accessed.
179///
180/// This uses the pointee type to determine how many bytes need to be safe to
181/// load from the pointer.
Artur Pilipenko6dd69692016-01-15 15:27:46 +0000182bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align,
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000183 const DataLayout &DL,
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000184 Instruction *ScanFrom,
185 const DominatorTree *DT,
186 const TargetLibraryInfo *TLI) {
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000187 // Zero alignment means that the load has the ABI alignment for the target
188 if (Align == 0)
189 Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
190 assert(isPowerOf2_32(Align));
191
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000192 // If DT is not specified we can't make context-sensitive query
193 const Instruction* CtxI = DT ? ScanFrom : nullptr;
194 if (isDereferenceableAndAlignedPointer(V, Align, DL, CtxI, DT, TLI))
Artur Pilipenkof84dc062016-01-17 12:35:29 +0000195 return true;
196
Nuno Lopes69dcc7d2012-12-31 17:42:11 +0000197 int64_t ByteOffset = 0;
Dan Gohman826bdf82010-05-28 16:19:17 +0000198 Value *Base = V;
Chandler Carruth38e98d52014-10-18 23:47:22 +0000199 Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL);
Nuno Lopes69dcc7d2012-12-31 17:42:11 +0000200
201 if (ByteOffset < 0) // out of bounds
202 return false;
Dan Gohman826bdf82010-05-28 16:19:17 +0000203
Craig Topper9f008862014-04-15 04:59:12 +0000204 Type *BaseType = nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000205 unsigned BaseAlign = 0;
206 if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
207 // An alloca is safe to load from as load as it is suitably aligned.
208 BaseType = AI->getAllocatedType();
209 BaseAlign = AI->getAlignment();
Nuno Lopes69dcc7d2012-12-31 17:42:11 +0000210 } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
Chandler Carruth8a993732014-10-19 00:42:16 +0000211 // Global variables are not necessarily safe to load from if they are
Sanjoy Das5ce32722016-04-08 00:48:30 +0000212 // interposed arbitrarily. Their size may change or they may be weak and
213 // require a test to determine if they were in fact provided.
214 if (!GV->isInterposable()) {
Dan Gohman826bdf82010-05-28 16:19:17 +0000215 BaseType = GV->getType()->getElementType();
216 BaseAlign = GV->getAlignment();
217 }
218 }
219
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000220 PointerType *AddrTy = cast<PointerType>(V->getType());
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000221 uint64_t LoadSize = DL.getTypeStoreSize(AddrTy->getElementType());
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000222
Chandler Carruth8a993732014-10-19 00:42:16 +0000223 // If we found a base allocated type from either an alloca or global variable,
224 // try to see if we are definitively within the allocated region. We need to
225 // know the size of the base type and the loaded type to do anything in this
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000226 // case.
227 if (BaseType && BaseType->isSized()) {
Chandler Carruth8a993732014-10-19 00:42:16 +0000228 if (BaseAlign == 0)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000229 BaseAlign = DL.getPrefTypeAlignment(BaseType);
Dan Gohman826bdf82010-05-28 16:19:17 +0000230
231 if (Align <= BaseAlign) {
Dan Gohman826bdf82010-05-28 16:19:17 +0000232 // Check if the load is within the bounds of the underlying object.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000233 if (ByteOffset + LoadSize <= DL.getTypeAllocSize(BaseType) &&
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000234 ((ByteOffset % Align) == 0))
Dan Gohman826bdf82010-05-28 16:19:17 +0000235 return true;
236 }
237 }
238
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000239 if (!ScanFrom)
240 return false;
241
Dan Gohman826bdf82010-05-28 16:19:17 +0000242 // Otherwise, be a little bit aggressive by scanning the local block where we
243 // want to check to see if the pointer is already being loaded or stored
244 // from/to. If so, the previous load or store would have already trapped,
245 // so there is no harm doing an extra load (also, CSE will later eliminate
246 // the load entirely).
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000247 BasicBlock::iterator BBI = ScanFrom->getIterator(),
248 E = ScanFrom->getParent()->begin();
Dan Gohman826bdf82010-05-28 16:19:17 +0000249
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000250 // We can at least always strip pointer casts even though we can't use the
251 // base here.
252 V = V->stripPointerCasts();
253
Dan Gohman826bdf82010-05-28 16:19:17 +0000254 while (BBI != E) {
255 --BBI;
256
257 // If we see a free or a call which may write to memory (i.e. which might do
258 // a free) the pointer could be marked invalid.
259 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
260 !isa<DbgInfoIntrinsic>(BBI))
261 return false;
262
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000263 Value *AccessedPtr;
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000264 unsigned AccessedAlign;
265 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000266 AccessedPtr = LI->getPointerOperand();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000267 AccessedAlign = LI->getAlignment();
268 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000269 AccessedPtr = SI->getPointerOperand();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000270 AccessedAlign = SI->getAlignment();
271 } else
272 continue;
273
274 Type *AccessedTy = AccessedPtr->getType()->getPointerElementType();
275 if (AccessedAlign == 0)
276 AccessedAlign = DL.getABITypeAlignment(AccessedTy);
277 if (AccessedAlign < Align)
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000278 continue;
279
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000280 // Handle trivial cases.
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000281 if (AccessedPtr == V)
282 return true;
283
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000284 if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000285 LoadSize <= DL.getTypeStoreSize(AccessedTy))
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000286 return true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000287 }
288 return false;
289}
290
Larisse Voufo532bf712015-09-18 19:14:35 +0000291/// DefMaxInstsToScan - the default number of maximum instructions
292/// to scan in the block, used by FindAvailableLoadedValue().
293/// FindAvailableLoadedValue() was introduced in r60148, to improve jump
294/// threading in part by eliminating partially redundant loads.
295/// At that point, the value of MaxInstsToScan was already set to '6'
296/// without documented explanation.
297cl::opt<unsigned>
298llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
299 cl::desc("Use this to specify the default maximum number of instructions "
300 "to scan backward from a given instruction, when searching for "
301 "available loaded value"));
302
Chandler Carruthb56052f2014-10-18 23:31:55 +0000303/// \brief Scan the ScanBB block backwards to see if we have the value at the
Dan Gohman826bdf82010-05-28 16:19:17 +0000304/// memory address *Ptr locally available within a small number of instructions.
Dan Gohman826bdf82010-05-28 16:19:17 +0000305///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000306/// The scan starts from \c ScanFrom. \c MaxInstsToScan specifies the maximum
307/// instructions to scan in the block. If it is set to \c 0, it will scan the whole
308/// block.
Dan Gohman826bdf82010-05-28 16:19:17 +0000309///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000310/// If the value is available, this function returns it. If not, it returns the
311/// iterator for the last validated instruction that the value would be live
312/// through. If we scanned the entire block and didn't find something that
313/// invalidates \c *Ptr or provides it, \c ScanFrom is left at the last
314/// instruction processed and this returns null.
Chris Lattner87fa77b2012-03-13 18:07:41 +0000315///
Chandler Carruthb56052f2014-10-18 23:31:55 +0000316/// You can also optionally specify an alias analysis implementation, which
317/// makes this more precise.
318///
319/// If \c AATags is non-null and a load or store is found, the AA tags from the
320/// load or store are recorded there. If there are no AA tags or if no access is
321/// found, it is left unmodified.
Eduard Burtescue2a69172016-01-22 01:51:51 +0000322Value *llvm::FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB,
Dan Gohman826bdf82010-05-28 16:19:17 +0000323 BasicBlock::iterator &ScanFrom,
324 unsigned MaxInstsToScan,
Eli Friedmanbd254a62016-06-16 02:33:42 +0000325 AliasAnalysis *AA, AAMDNodes *AATags,
326 bool *IsLoadCSE) {
Chandler Carruthd67244d2014-10-18 23:19:03 +0000327 if (MaxInstsToScan == 0)
328 MaxInstsToScan = ~0U;
Dan Gohman826bdf82010-05-28 16:19:17 +0000329
Eduard Burtescue2a69172016-01-22 01:51:51 +0000330 Value *Ptr = Load->getPointerOperand();
331 Type *AccessTy = Load->getType();
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000332
Philip Reames92c43692016-04-21 16:51:08 +0000333 // We can never remove a volatile load
334 if (Load->isVolatile())
335 return nullptr;
336
337 // Anything stronger than unordered is currently unimplemented.
338 if (!Load->isUnordered())
339 return nullptr;
340
Mehdi Amini46a43552015-03-04 18:43:29 +0000341 const DataLayout &DL = ScanBB->getModule()->getDataLayout();
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000342
343 // Try to get the store size for the type.
Mehdi Amini46a43552015-03-04 18:43:29 +0000344 uint64_t AccessSize = DL.getTypeStoreSize(AccessTy);
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000345
346 Value *StrippedPtr = Ptr->stripPointerCasts();
Chandler Carruthd67244d2014-10-18 23:19:03 +0000347
Dan Gohman826bdf82010-05-28 16:19:17 +0000348 while (ScanFrom != ScanBB->begin()) {
349 // We must ignore debug info directives when counting (otherwise they
350 // would affect codegen).
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000351 Instruction *Inst = &*--ScanFrom;
Dan Gohman826bdf82010-05-28 16:19:17 +0000352 if (isa<DbgInfoIntrinsic>(Inst))
353 continue;
354
355 // Restore ScanFrom to expected value in case next test succeeds
356 ScanFrom++;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000357
Dan Gohman826bdf82010-05-28 16:19:17 +0000358 // Don't scan huge blocks.
Chandler Carruthd67244d2014-10-18 23:19:03 +0000359 if (MaxInstsToScan-- == 0)
360 return nullptr;
361
Dan Gohman826bdf82010-05-28 16:19:17 +0000362 --ScanFrom;
363 // If this is a load of Ptr, the loaded value is available.
Eli Friedman4419cd22011-08-15 21:56:39 +0000364 // (This is true even if the load is volatile or atomic, although
365 // those cases are unlikely.)
Dan Gohman826bdf82010-05-28 16:19:17 +0000366 if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000367 if (AreEquivalentAddressValues(
368 LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000369 CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
Philip Reames92c43692016-04-21 16:51:08 +0000370
371 // We can value forward from an atomic to a non-atomic, but not the
372 // other way around.
373 if (LI->isAtomic() < Load->isAtomic())
374 return nullptr;
375
Chandler Carruthd67244d2014-10-18 23:19:03 +0000376 if (AATags)
377 LI->getAAMetadata(*AATags);
Eli Friedmanbd254a62016-06-16 02:33:42 +0000378 if (IsLoadCSE)
379 *IsLoadCSE = true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000380 return LI;
Chris Lattner87fa77b2012-03-13 18:07:41 +0000381 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000382
Dan Gohman826bdf82010-05-28 16:19:17 +0000383 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000384 Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
Dan Gohman826bdf82010-05-28 16:19:17 +0000385 // If this is a store through Ptr, the value is available!
Eli Friedman4419cd22011-08-15 21:56:39 +0000386 // (This is true even if the store is volatile or atomic, although
387 // those cases are unlikely.)
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000388 if (AreEquivalentAddressValues(StorePtr, StrippedPtr) &&
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000389 CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(),
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000390 AccessTy, DL)) {
Philip Reames92c43692016-04-21 16:51:08 +0000391
392 // We can value forward from an atomic to a non-atomic, but not the
393 // other way around.
394 if (SI->isAtomic() < Load->isAtomic())
395 return nullptr;
396
Chandler Carruthd67244d2014-10-18 23:19:03 +0000397 if (AATags)
398 SI->getAAMetadata(*AATags);
Dan Gohman826bdf82010-05-28 16:19:17 +0000399 return SI->getOperand(0);
Chris Lattner87fa77b2012-03-13 18:07:41 +0000400 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000401
Chandler Carrutha32038b2014-10-20 10:03:01 +0000402 // If both StrippedPtr and StorePtr reach all the way to an alloca or
403 // global and they are different, ignore the store. This is a trivial form
404 // of alias analysis that is important for reg2mem'd code.
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000405 if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
Chandler Carrutha32038b2014-10-20 10:03:01 +0000406 (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
407 StrippedPtr != StorePtr)
Dan Gohman826bdf82010-05-28 16:19:17 +0000408 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000409
Dan Gohman826bdf82010-05-28 16:19:17 +0000410 // If we have alias analysis and it says the store won't modify the loaded
411 // value, ignore the store.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000412 if (AA && (AA->getModRefInfo(SI, StrippedPtr, AccessSize) & MRI_Mod) == 0)
Dan Gohman826bdf82010-05-28 16:19:17 +0000413 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000414
Dan Gohman826bdf82010-05-28 16:19:17 +0000415 // Otherwise the store that may or may not alias the pointer, bail out.
416 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000417 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000418 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000419
Dan Gohman826bdf82010-05-28 16:19:17 +0000420 // If this is some other instruction that may clobber Ptr, bail out.
421 if (Inst->mayWriteToMemory()) {
422 // If alias analysis claims that it really won't modify the load,
423 // ignore it.
424 if (AA &&
Chandler Carruth194f59c2015-07-22 23:15:57 +0000425 (AA->getModRefInfo(Inst, StrippedPtr, AccessSize) & MRI_Mod) == 0)
Dan Gohman826bdf82010-05-28 16:19:17 +0000426 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000427
Dan Gohman826bdf82010-05-28 16:19:17 +0000428 // May modify the pointer, bail out.
429 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000430 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000431 }
432 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000433
Dan Gohman826bdf82010-05-28 16:19:17 +0000434 // Got to the start of the block, we didn't find it, but are done for this
435 // block.
Craig Topper9f008862014-04-15 04:59:12 +0000436 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000437}