blob: a40f8dd15975f0e2464ad12d1fbb16ccdf93f2ef [file] [log] [blame]
Dan Gohman826bdf82010-05-28 16:19:17 +00001//===- Loads.cpp - Local load analysis ------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Dan Gohman826bdf82010-05-28 16:19:17 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines simple local analyses for load instructions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Analysis/Loads.h"
14#include "llvm/Analysis/AliasAnalysis.h"
Nuno Lopes69dcc7d2012-12-31 17:42:11 +000015#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/DataLayout.h"
17#include "llvm/IR/GlobalAlias.h"
18#include "llvm/IR/GlobalVariable.h"
19#include "llvm/IR/IntrinsicInst.h"
20#include "llvm/IR/LLVMContext.h"
Mehdi Amini9a9738f2015-03-03 22:01:13 +000021#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Operator.h"
Artur Pilipenko31bcca42016-02-24 12:49:04 +000023#include "llvm/IR/Statepoint.h"
24
Dan Gohman826bdf82010-05-28 16:19:17 +000025using namespace llvm;
26
Benjamin Kramerc321e532016-06-08 19:09:22 +000027static bool isAligned(const Value *Base, const APInt &Offset, unsigned Align,
Artur Pilipenko31bcca42016-02-24 12:49:04 +000028 const DataLayout &DL) {
29 APInt BaseAlign(Offset.getBitWidth(), Base->getPointerAlignment(DL));
30
31 if (!BaseAlign) {
32 Type *Ty = Base->getType()->getPointerElementType();
33 if (!Ty->isSized())
34 return false;
35 BaseAlign = DL.getABITypeAlignment(Ty);
36 }
37
38 APInt Alignment(Offset.getBitWidth(), Align);
39
40 assert(Alignment.isPowerOf2() && "must be a power of 2!");
41 return BaseAlign.uge(Alignment) && !(Offset & (Alignment-1));
42}
43
Artur Pilipenko31bcca42016-02-24 12:49:04 +000044/// Test if V is always a pointer to allocated and suitably aligned memory for
45/// a simple load or store.
46static bool isDereferenceableAndAlignedPointer(
Benjamin Kramerc321e532016-06-08 19:09:22 +000047 const Value *V, unsigned Align, const APInt &Size, const DataLayout &DL,
Artur Pilipenko31bcca42016-02-24 12:49:04 +000048 const Instruction *CtxI, const DominatorTree *DT,
Sean Silva45835e72016-07-02 23:47:27 +000049 SmallPtrSetImpl<const Value *> &Visited) {
David Majnemera90e51e2016-08-31 03:22:32 +000050 // Already visited? Bail out, we've likely hit unreachable code.
51 if (!Visited.insert(V).second)
52 return false;
53
Artur Pilipenko31bcca42016-02-24 12:49:04 +000054 // Note that it is not safe to speculate into a malloc'd region because
55 // malloc may return null.
56
Sanjoy Das10df4972016-06-01 16:47:45 +000057 // bitcast instructions are no-ops as far as dereferenceability is concerned.
58 if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V))
59 return isDereferenceableAndAlignedPointer(BC->getOperand(0), Align, Size,
Sean Silva45835e72016-07-02 23:47:27 +000060 DL, CtxI, DT, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +000061
Sanjoy Das48cad712016-06-02 00:52:53 +000062 bool CheckForNonNull = false;
63 APInt KnownDerefBytes(Size.getBitWidth(),
64 V->getPointerDereferenceableBytes(DL, CheckForNonNull));
Philip Reames2f858c22019-08-26 23:57:27 +000065 if (KnownDerefBytes.getBoolValue() && KnownDerefBytes.uge(Size))
66 if (!CheckForNonNull || isKnownNonZero(V, DL, 0, nullptr, CtxI, DT)) {
Philip Reames20650ed2019-08-27 04:52:35 +000067 // As we recursed through GEPs to get here, we've incrementally checked
68 // that each step advanced by a multiple of the alignment. If our base is
69 // properly aligned, then the original offset accessed must also be.
Philip Reames2f858c22019-08-26 23:57:27 +000070 Type *Ty = V->getType();
71 assert(Ty->isSized() && "must be sized");
72 APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0);
73 return isAligned(V, Offset, Align, DL);
74 }
Artur Pilipenko31bcca42016-02-24 12:49:04 +000075
76 // For GEPs, determine if the indexing lands within the allocated object.
77 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
Artur Pilipenko31bcca42016-02-24 12:49:04 +000078 const Value *Base = GEP->getPointerOperand();
79
Elena Demikhovsky945b7e52018-02-14 06:58:08 +000080 APInt Offset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
Sanjoy Das10df4972016-06-01 16:47:45 +000081 if (!GEP->accumulateConstantOffset(DL, Offset) || Offset.isNegative() ||
82 !Offset.urem(APInt(Offset.getBitWidth(), Align)).isMinValue())
Artur Pilipenko31bcca42016-02-24 12:49:04 +000083 return false;
84
Sanjoy Das10df4972016-06-01 16:47:45 +000085 // If the base pointer is dereferenceable for Offset+Size bytes, then the
86 // GEP (== Base + Offset) is dereferenceable for Size bytes. If the base
87 // pointer is aligned to Align bytes, and the Offset is divisible by Align
88 // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also
89 // aligned to Align bytes.
90
Tom Stellard130689952016-10-28 15:32:28 +000091 // Offset and Size may have different bit widths if we have visited an
92 // addrspacecast, so we can't do arithmetic directly on the APInt values.
93 return isDereferenceableAndAlignedPointer(
94 Base, Align, Offset + Size.sextOrTrunc(Offset.getBitWidth()),
95 DL, CtxI, DT, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +000096 }
97
98 // For gc.relocate, look through relocations
99 if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V))
100 return isDereferenceableAndAlignedPointer(
Sean Silva45835e72016-07-02 23:47:27 +0000101 RelocateInst->getDerivedPtr(), Align, Size, DL, CtxI, DT, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000102
103 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
Sanjoy Das10df4972016-06-01 16:47:45 +0000104 return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, Size,
Sean Silva45835e72016-07-02 23:47:27 +0000105 DL, CtxI, DT, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000106
Chandler Carruth363ac682019-01-07 05:42:51 +0000107 if (const auto *Call = dyn_cast<CallBase>(V))
Florian Hahnfd72bf22019-08-15 12:13:02 +0000108 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
Piotr Padlewskid6f73462018-05-23 09:16:44 +0000109 return isDereferenceableAndAlignedPointer(RP, Align, Size, DL, CtxI, DT,
Hal Finkelbf3957a2016-07-11 03:08:49 +0000110 Visited);
Piotr Padlewskid6f73462018-05-23 09:16:44 +0000111
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000112 // If we don't know, assume the worst.
113 return false;
114}
115
116bool llvm::isDereferenceableAndAlignedPointer(const Value *V, unsigned Align,
Vitaly Buka9c2a0362017-06-24 01:35:13 +0000117 const APInt &Size,
118 const DataLayout &DL,
119 const Instruction *CtxI,
120 const DominatorTree *DT) {
121 SmallPtrSet<const Value *, 32> Visited;
122 return ::isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT,
123 Visited);
124}
125
Tim Northover60afa492019-07-09 11:35:35 +0000126bool llvm::isDereferenceableAndAlignedPointer(const Value *V, Type *Ty,
127 unsigned Align,
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000128 const DataLayout &DL,
129 const Instruction *CtxI,
Sean Silva45835e72016-07-02 23:47:27 +0000130 const DominatorTree *DT) {
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000131 // When dereferenceability information is provided by a dereferenceable
132 // attribute, we know exactly how many bytes are dereferenceable. If we can
133 // determine the exact offset to the attributed variable, we can use that
134 // information here.
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000135
136 // Require ABI alignment for loads without alignment specification
137 if (Align == 0)
138 Align = DL.getABITypeAlignment(Ty);
139
Sanjoy Das10df4972016-06-01 16:47:45 +0000140 if (!Ty->isSized())
141 return false;
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000142
143 SmallPtrSet<const Value *, 32> Visited;
Sanjoy Das10df4972016-06-01 16:47:45 +0000144 return ::isDereferenceableAndAlignedPointer(
Tim Northover60afa492019-07-09 11:35:35 +0000145 V, Align,
146 APInt(DL.getIndexTypeSizeInBits(V->getType()), DL.getTypeStoreSize(Ty)),
147 DL, CtxI, DT, Visited);
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000148}
149
Tim Northover60afa492019-07-09 11:35:35 +0000150bool llvm::isDereferenceablePointer(const Value *V, Type *Ty,
151 const DataLayout &DL,
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000152 const Instruction *CtxI,
Sean Silva45835e72016-07-02 23:47:27 +0000153 const DominatorTree *DT) {
Tim Northover60afa492019-07-09 11:35:35 +0000154 return isDereferenceableAndAlignedPointer(V, Ty, 1, DL, CtxI, DT);
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000155}
156
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000157/// Test if A and B will obviously have the same value.
Chandler Carruthb56052f2014-10-18 23:31:55 +0000158///
159/// This includes recognizing that %t0 and %t1 will have the same
Dan Gohman826bdf82010-05-28 16:19:17 +0000160/// value in code like this:
Chandler Carruthb56052f2014-10-18 23:31:55 +0000161/// \code
Dan Gohman826bdf82010-05-28 16:19:17 +0000162/// %t0 = getelementptr \@a, 0, 3
163/// store i32 0, i32* %t0
164/// %t1 = getelementptr \@a, 0, 3
165/// %t2 = load i32* %t1
Chandler Carruthb56052f2014-10-18 23:31:55 +0000166/// \endcode
Dan Gohman826bdf82010-05-28 16:19:17 +0000167///
168static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
169 // Test if the values are trivially equivalent.
Chandler Carruthbe49df32014-10-18 23:41:25 +0000170 if (A == B)
171 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +0000172
Dan Gohman826bdf82010-05-28 16:19:17 +0000173 // Test if the values come from identical arithmetic instructions.
174 // Use isIdenticalToWhenDefined instead of isIdenticalTo because
175 // this function is only used when one address use dominates the
176 // other, which means that they'll always either have the same
177 // value or one of them will have an undefined value.
Chandler Carruthbe49df32014-10-18 23:41:25 +0000178 if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
179 isa<GetElementPtrInst>(A))
Dan Gohman826bdf82010-05-28 16:19:17 +0000180 if (const Instruction *BI = dyn_cast<Instruction>(B))
181 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
182 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +0000183
Dan Gohman826bdf82010-05-28 16:19:17 +0000184 // Otherwise they may not be equivalent.
185 return false;
186}
187
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000188/// Check if executing a load of this pointer value cannot trap.
Chandler Carruth1f27f032014-10-18 23:46:17 +0000189///
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000190/// If DT and ScanFrom are specified this method performs context-sensitive
191/// analysis and returns true if it is safe to load immediately before ScanFrom.
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000192///
Chandler Carruth1f27f032014-10-18 23:46:17 +0000193/// If it is not obviously safe to load from the specified pointer, we do
194/// a quick local scan of the basic block containing \c ScanFrom, to determine
195/// if the address is already accessed.
196///
197/// This uses the pointee type to determine how many bytes need to be safe to
198/// load from the pointer.
Tim Northover60afa492019-07-09 11:35:35 +0000199bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align, APInt &Size,
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000200 const DataLayout &DL,
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000201 Instruction *ScanFrom,
Sean Silva45835e72016-07-02 23:47:27 +0000202 const DominatorTree *DT) {
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000203 // Zero alignment means that the load has the ABI alignment for the target
204 if (Align == 0)
205 Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
206 assert(isPowerOf2_32(Align));
207
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000208 // If DT is not specified we can't make context-sensitive query
209 const Instruction* CtxI = DT ? ScanFrom : nullptr;
Tim Northover60afa492019-07-09 11:35:35 +0000210 if (isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT))
Artur Pilipenkof84dc062016-01-17 12:35:29 +0000211 return true;
212
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000213 if (!ScanFrom)
214 return false;
215
Philip Reames26945222019-08-27 19:34:43 +0000216 if (Size.getBitWidth() > 64)
217 return false;
218 const uint64_t LoadSize = Size.getZExtValue();
219
Dan Gohman826bdf82010-05-28 16:19:17 +0000220 // Otherwise, be a little bit aggressive by scanning the local block where we
221 // want to check to see if the pointer is already being loaded or stored
222 // from/to. If so, the previous load or store would have already trapped,
223 // so there is no harm doing an extra load (also, CSE will later eliminate
224 // the load entirely).
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000225 BasicBlock::iterator BBI = ScanFrom->getIterator(),
226 E = ScanFrom->getParent()->begin();
Dan Gohman826bdf82010-05-28 16:19:17 +0000227
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000228 // We can at least always strip pointer casts even though we can't use the
229 // base here.
230 V = V->stripPointerCasts();
231
Dan Gohman826bdf82010-05-28 16:19:17 +0000232 while (BBI != E) {
233 --BBI;
234
235 // If we see a free or a call which may write to memory (i.e. which might do
236 // a free) the pointer could be marked invalid.
237 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
238 !isa<DbgInfoIntrinsic>(BBI))
239 return false;
240
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000241 Value *AccessedPtr;
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000242 unsigned AccessedAlign;
243 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Eli Friedman525ef012019-01-24 21:31:13 +0000244 // Ignore volatile loads. The execution of a volatile load cannot
245 // be used to prove an address is backed by regular memory; it can,
246 // for example, point to an MMIO register.
247 if (LI->isVolatile())
248 continue;
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000249 AccessedPtr = LI->getPointerOperand();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000250 AccessedAlign = LI->getAlignment();
251 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
Eli Friedman525ef012019-01-24 21:31:13 +0000252 // Ignore volatile stores (see comment for loads).
253 if (SI->isVolatile())
254 continue;
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000255 AccessedPtr = SI->getPointerOperand();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000256 AccessedAlign = SI->getAlignment();
257 } else
258 continue;
259
260 Type *AccessedTy = AccessedPtr->getType()->getPointerElementType();
261 if (AccessedAlign == 0)
262 AccessedAlign = DL.getABITypeAlignment(AccessedTy);
263 if (AccessedAlign < Align)
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000264 continue;
265
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000266 // Handle trivial cases.
Philip Reames26945222019-08-27 19:34:43 +0000267 if (AccessedPtr == V &&
268 LoadSize <= DL.getTypeStoreSize(AccessedTy))
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000269 return true;
270
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000271 if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000272 LoadSize <= DL.getTypeStoreSize(AccessedTy))
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000273 return true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000274 }
275 return false;
276}
277
Tim Northover60afa492019-07-09 11:35:35 +0000278bool llvm::isSafeToLoadUnconditionally(Value *V, Type *Ty, unsigned Align,
279 const DataLayout &DL,
280 Instruction *ScanFrom,
281 const DominatorTree *DT) {
282 APInt Size(DL.getIndexTypeSizeInBits(V->getType()), DL.getTypeStoreSize(Ty));
283 return isSafeToLoadUnconditionally(V, Align, Size, DL, ScanFrom, DT);
284}
285
286 /// DefMaxInstsToScan - the default number of maximum instructions
Larisse Voufo532bf712015-09-18 19:14:35 +0000287/// to scan in the block, used by FindAvailableLoadedValue().
288/// FindAvailableLoadedValue() was introduced in r60148, to improve jump
289/// threading in part by eliminating partially redundant loads.
290/// At that point, the value of MaxInstsToScan was already set to '6'
291/// without documented explanation.
292cl::opt<unsigned>
293llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
294 cl::desc("Use this to specify the default maximum number of instructions "
295 "to scan backward from a given instruction, when searching for "
296 "available loaded value"));
297
Eli Friedman02419a92016-08-08 04:10:22 +0000298Value *llvm::FindAvailableLoadedValue(LoadInst *Load,
299 BasicBlock *ScanBB,
Dan Gohman826bdf82010-05-28 16:19:17 +0000300 BasicBlock::iterator &ScanFrom,
301 unsigned MaxInstsToScan,
Xin Tongaef0fcb2017-03-19 15:27:52 +0000302 AliasAnalysis *AA, bool *IsLoad,
Jun Bum Lim180bc5a2017-02-02 15:12:34 +0000303 unsigned *NumScanedInst) {
Xin Tongaef0fcb2017-03-19 15:27:52 +0000304 // Don't CSE load that is volatile or anything stronger than unordered.
Anna Thomas9ad45ad2016-07-08 22:15:08 +0000305 if (!Load->isUnordered())
306 return nullptr;
307
Xin Tongaef0fcb2017-03-19 15:27:52 +0000308 return FindAvailablePtrLoadStore(
309 Load->getPointerOperand(), Load->getType(), Load->isAtomic(), ScanBB,
310 ScanFrom, MaxInstsToScan, AA, IsLoad, NumScanedInst);
311}
312
313Value *llvm::FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy,
314 bool AtLeastAtomic, BasicBlock *ScanBB,
315 BasicBlock::iterator &ScanFrom,
316 unsigned MaxInstsToScan,
317 AliasAnalysis *AA, bool *IsLoadCSE,
318 unsigned *NumScanedInst) {
319 if (MaxInstsToScan == 0)
320 MaxInstsToScan = ~0U;
321
Mehdi Amini46a43552015-03-04 18:43:29 +0000322 const DataLayout &DL = ScanBB->getModule()->getDataLayout();
Anna Thomas9ad45ad2016-07-08 22:15:08 +0000323
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000324 // Try to get the store size for the type.
George Burgess IV8c5413f32018-12-23 03:10:56 +0000325 auto AccessSize = LocationSize::precise(DL.getTypeStoreSize(AccessTy));
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000326
327 Value *StrippedPtr = Ptr->stripPointerCasts();
Chandler Carruthd67244d2014-10-18 23:19:03 +0000328
Dan Gohman826bdf82010-05-28 16:19:17 +0000329 while (ScanFrom != ScanBB->begin()) {
330 // We must ignore debug info directives when counting (otherwise they
331 // would affect codegen).
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000332 Instruction *Inst = &*--ScanFrom;
Dan Gohman826bdf82010-05-28 16:19:17 +0000333 if (isa<DbgInfoIntrinsic>(Inst))
334 continue;
335
336 // Restore ScanFrom to expected value in case next test succeeds
337 ScanFrom++;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000338
Jun Bum Lim180bc5a2017-02-02 15:12:34 +0000339 if (NumScanedInst)
340 ++(*NumScanedInst);
341
Dan Gohman826bdf82010-05-28 16:19:17 +0000342 // Don't scan huge blocks.
Chandler Carruthd67244d2014-10-18 23:19:03 +0000343 if (MaxInstsToScan-- == 0)
344 return nullptr;
345
Dan Gohman826bdf82010-05-28 16:19:17 +0000346 --ScanFrom;
347 // If this is a load of Ptr, the loaded value is available.
Eli Friedman4419cd22011-08-15 21:56:39 +0000348 // (This is true even if the load is volatile or atomic, although
349 // those cases are unlikely.)
Reid Klecknerfbd5eef2016-06-24 18:42:58 +0000350 if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
351 if (AreEquivalentAddressValues(
352 LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000353 CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
Philip Reames92c43692016-04-21 16:51:08 +0000354
355 // We can value forward from an atomic to a non-atomic, but not the
356 // other way around.
Xin Tongaef0fcb2017-03-19 15:27:52 +0000357 if (LI->isAtomic() < AtLeastAtomic)
Philip Reames92c43692016-04-21 16:51:08 +0000358 return nullptr;
359
Eli Friedmanbd254a62016-06-16 02:33:42 +0000360 if (IsLoadCSE)
361 *IsLoadCSE = true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000362 return LI;
Chris Lattner87fa77b2012-03-13 18:07:41 +0000363 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000364
Dan Gohman826bdf82010-05-28 16:19:17 +0000365 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000366 Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
Dan Gohman826bdf82010-05-28 16:19:17 +0000367 // If this is a store through Ptr, the value is available!
Eli Friedman4419cd22011-08-15 21:56:39 +0000368 // (This is true even if the store is volatile or atomic, although
369 // those cases are unlikely.)
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000370 if (AreEquivalentAddressValues(StorePtr, StrippedPtr) &&
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000371 CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(),
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000372 AccessTy, DL)) {
Philip Reames92c43692016-04-21 16:51:08 +0000373
374 // We can value forward from an atomic to a non-atomic, but not the
375 // other way around.
Xin Tongaef0fcb2017-03-19 15:27:52 +0000376 if (SI->isAtomic() < AtLeastAtomic)
Philip Reames92c43692016-04-21 16:51:08 +0000377 return nullptr;
378
Eli Friedman02419a92016-08-08 04:10:22 +0000379 if (IsLoadCSE)
380 *IsLoadCSE = false;
Dan Gohman826bdf82010-05-28 16:19:17 +0000381 return SI->getOperand(0);
Chris Lattner87fa77b2012-03-13 18:07:41 +0000382 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000383
Chandler Carrutha32038b2014-10-20 10:03:01 +0000384 // If both StrippedPtr and StorePtr reach all the way to an alloca or
385 // global and they are different, ignore the store. This is a trivial form
386 // of alias analysis that is important for reg2mem'd code.
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000387 if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
Chandler Carrutha32038b2014-10-20 10:03:01 +0000388 (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
389 StrippedPtr != StorePtr)
Dan Gohman826bdf82010-05-28 16:19:17 +0000390 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000391
Dan Gohman826bdf82010-05-28 16:19:17 +0000392 // If we have alias analysis and it says the store won't modify the loaded
393 // value, ignore the store.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000394 if (AA && !isModSet(AA->getModRefInfo(SI, StrippedPtr, AccessSize)))
Dan Gohman826bdf82010-05-28 16:19:17 +0000395 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000396
Dan Gohman826bdf82010-05-28 16:19:17 +0000397 // Otherwise the store that may or may not alias the pointer, bail out.
398 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000399 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000400 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000401
Dan Gohman826bdf82010-05-28 16:19:17 +0000402 // If this is some other instruction that may clobber Ptr, bail out.
403 if (Inst->mayWriteToMemory()) {
404 // If alias analysis claims that it really won't modify the load,
405 // ignore it.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000406 if (AA && !isModSet(AA->getModRefInfo(Inst, StrippedPtr, AccessSize)))
Dan Gohman826bdf82010-05-28 16:19:17 +0000407 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000408
Dan Gohman826bdf82010-05-28 16:19:17 +0000409 // May modify the pointer, bail out.
410 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000411 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000412 }
413 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000414
Dan Gohman826bdf82010-05-28 16:19:17 +0000415 // Got to the start of the block, we didn't find it, but are done for this
416 // block.
Craig Topper9f008862014-04-15 04:59:12 +0000417 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000418}