blob: e74df9c69d7f2cfb75b2126ddb134efad9335561 [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) {
Philip Reames93a26ec2019-08-27 23:36:31 +0000121 assert(Align != 0 && "expected explicitly set alignment");
122 // Note: At the moment, Size can be zero. This ends up being interpreted as
123 // a query of whether [Base, V] is dereferenceable and V is aligned (since
124 // that's what the implementation happened to do). It's unclear if this is
125 // the desired semantic, but at least SelectionDAG does exercise this case.
126
Vitaly Buka9c2a0362017-06-24 01:35:13 +0000127 SmallPtrSet<const Value *, 32> Visited;
128 return ::isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT,
129 Visited);
130}
131
Tim Northover60afa492019-07-09 11:35:35 +0000132bool llvm::isDereferenceableAndAlignedPointer(const Value *V, Type *Ty,
133 unsigned Align,
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000134 const DataLayout &DL,
135 const Instruction *CtxI,
Sean Silva45835e72016-07-02 23:47:27 +0000136 const DominatorTree *DT) {
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000137 // When dereferenceability information is provided by a dereferenceable
138 // attribute, we know exactly how many bytes are dereferenceable. If we can
139 // determine the exact offset to the attributed variable, we can use that
140 // information here.
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000141
142 // Require ABI alignment for loads without alignment specification
143 if (Align == 0)
144 Align = DL.getABITypeAlignment(Ty);
145
Sanjoy Das10df4972016-06-01 16:47:45 +0000146 if (!Ty->isSized())
147 return false;
Philip Reames93a26ec2019-08-27 23:36:31 +0000148
149 APInt AccessSize(DL.getIndexTypeSizeInBits(V->getType()),
150 DL.getTypeStoreSize(Ty));
151 return isDereferenceableAndAlignedPointer(V, Align, AccessSize,
152 DL, CtxI, DT);
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000153}
154
Tim Northover60afa492019-07-09 11:35:35 +0000155bool llvm::isDereferenceablePointer(const Value *V, Type *Ty,
156 const DataLayout &DL,
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000157 const Instruction *CtxI,
Sean Silva45835e72016-07-02 23:47:27 +0000158 const DominatorTree *DT) {
Tim Northover60afa492019-07-09 11:35:35 +0000159 return isDereferenceableAndAlignedPointer(V, Ty, 1, DL, CtxI, DT);
Artur Pilipenko31bcca42016-02-24 12:49:04 +0000160}
161
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000162/// Test if A and B will obviously have the same value.
Chandler Carruthb56052f2014-10-18 23:31:55 +0000163///
164/// This includes recognizing that %t0 and %t1 will have the same
Dan Gohman826bdf82010-05-28 16:19:17 +0000165/// value in code like this:
Chandler Carruthb56052f2014-10-18 23:31:55 +0000166/// \code
Dan Gohman826bdf82010-05-28 16:19:17 +0000167/// %t0 = getelementptr \@a, 0, 3
168/// store i32 0, i32* %t0
169/// %t1 = getelementptr \@a, 0, 3
170/// %t2 = load i32* %t1
Chandler Carruthb56052f2014-10-18 23:31:55 +0000171/// \endcode
Dan Gohman826bdf82010-05-28 16:19:17 +0000172///
173static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
174 // Test if the values are trivially equivalent.
Chandler Carruthbe49df32014-10-18 23:41:25 +0000175 if (A == B)
176 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +0000177
Dan Gohman826bdf82010-05-28 16:19:17 +0000178 // Test if the values come from identical arithmetic instructions.
179 // Use isIdenticalToWhenDefined instead of isIdenticalTo because
180 // this function is only used when one address use dominates the
181 // other, which means that they'll always either have the same
182 // value or one of them will have an undefined value.
Chandler Carruthbe49df32014-10-18 23:41:25 +0000183 if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
184 isa<GetElementPtrInst>(A))
Dan Gohman826bdf82010-05-28 16:19:17 +0000185 if (const Instruction *BI = dyn_cast<Instruction>(B))
186 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
187 return true;
Hans Wennborg060b9942011-06-03 17:15:37 +0000188
Dan Gohman826bdf82010-05-28 16:19:17 +0000189 // Otherwise they may not be equivalent.
190 return false;
191}
192
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000193/// Check if executing a load of this pointer value cannot trap.
Chandler Carruth1f27f032014-10-18 23:46:17 +0000194///
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000195/// If DT and ScanFrom are specified this method performs context-sensitive
196/// analysis and returns true if it is safe to load immediately before ScanFrom.
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000197///
Chandler Carruth1f27f032014-10-18 23:46:17 +0000198/// If it is not obviously safe to load from the specified pointer, we do
199/// a quick local scan of the basic block containing \c ScanFrom, to determine
200/// if the address is already accessed.
201///
202/// This uses the pointee type to determine how many bytes need to be safe to
203/// load from the pointer.
Tim Northover60afa492019-07-09 11:35:35 +0000204bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align, APInt &Size,
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000205 const DataLayout &DL,
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000206 Instruction *ScanFrom,
Sean Silva45835e72016-07-02 23:47:27 +0000207 const DominatorTree *DT) {
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000208 // Zero alignment means that the load has the ABI alignment for the target
209 if (Align == 0)
210 Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
211 assert(isPowerOf2_32(Align));
212
Artur Pilipenko66d6d3e2016-02-11 13:42:59 +0000213 // If DT is not specified we can't make context-sensitive query
214 const Instruction* CtxI = DT ? ScanFrom : nullptr;
Tim Northover60afa492019-07-09 11:35:35 +0000215 if (isDereferenceableAndAlignedPointer(V, Align, Size, DL, CtxI, DT))
Artur Pilipenkof84dc062016-01-17 12:35:29 +0000216 return true;
217
Artur Pilipenko9bb6bea2016-04-27 11:00:48 +0000218 if (!ScanFrom)
219 return false;
220
Philip Reames26945222019-08-27 19:34:43 +0000221 if (Size.getBitWidth() > 64)
222 return false;
223 const uint64_t LoadSize = Size.getZExtValue();
224
Dan Gohman826bdf82010-05-28 16:19:17 +0000225 // Otherwise, be a little bit aggressive by scanning the local block where we
226 // want to check to see if the pointer is already being loaded or stored
227 // from/to. If so, the previous load or store would have already trapped,
228 // so there is no harm doing an extra load (also, CSE will later eliminate
229 // the load entirely).
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000230 BasicBlock::iterator BBI = ScanFrom->getIterator(),
231 E = ScanFrom->getParent()->begin();
Dan Gohman826bdf82010-05-28 16:19:17 +0000232
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000233 // We can at least always strip pointer casts even though we can't use the
234 // base here.
235 V = V->stripPointerCasts();
236
Dan Gohman826bdf82010-05-28 16:19:17 +0000237 while (BBI != E) {
238 --BBI;
239
240 // If we see a free or a call which may write to memory (i.e. which might do
241 // a free) the pointer could be marked invalid.
242 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
243 !isa<DbgInfoIntrinsic>(BBI))
244 return false;
245
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000246 Value *AccessedPtr;
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000247 unsigned AccessedAlign;
248 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
Eli Friedman525ef012019-01-24 21:31:13 +0000249 // Ignore volatile loads. The execution of a volatile load cannot
250 // be used to prove an address is backed by regular memory; it can,
251 // for example, point to an MMIO register.
252 if (LI->isVolatile())
253 continue;
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000254 AccessedPtr = LI->getPointerOperand();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000255 AccessedAlign = LI->getAlignment();
256 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
Eli Friedman525ef012019-01-24 21:31:13 +0000257 // Ignore volatile stores (see comment for loads).
258 if (SI->isVolatile())
259 continue;
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000260 AccessedPtr = SI->getPointerOperand();
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000261 AccessedAlign = SI->getAlignment();
262 } else
263 continue;
264
265 Type *AccessedTy = AccessedPtr->getType()->getPointerElementType();
266 if (AccessedAlign == 0)
267 AccessedAlign = DL.getABITypeAlignment(AccessedTy);
268 if (AccessedAlign < Align)
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000269 continue;
270
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000271 // Handle trivial cases.
Philip Reames26945222019-08-27 19:34:43 +0000272 if (AccessedPtr == V &&
273 LoadSize <= DL.getTypeStoreSize(AccessedTy))
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000274 return true;
275
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000276 if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
Artur Pilipenko0e21d542015-06-25 12:18:43 +0000277 LoadSize <= DL.getTypeStoreSize(AccessedTy))
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000278 return true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000279 }
280 return false;
281}
282
Tim Northover60afa492019-07-09 11:35:35 +0000283bool llvm::isSafeToLoadUnconditionally(Value *V, Type *Ty, unsigned Align,
284 const DataLayout &DL,
285 Instruction *ScanFrom,
286 const DominatorTree *DT) {
287 APInt Size(DL.getIndexTypeSizeInBits(V->getType()), DL.getTypeStoreSize(Ty));
288 return isSafeToLoadUnconditionally(V, Align, Size, DL, ScanFrom, DT);
289}
290
291 /// DefMaxInstsToScan - the default number of maximum instructions
Larisse Voufo532bf712015-09-18 19:14:35 +0000292/// 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
Eli Friedman02419a92016-08-08 04:10:22 +0000303Value *llvm::FindAvailableLoadedValue(LoadInst *Load,
304 BasicBlock *ScanBB,
Dan Gohman826bdf82010-05-28 16:19:17 +0000305 BasicBlock::iterator &ScanFrom,
306 unsigned MaxInstsToScan,
Xin Tongaef0fcb2017-03-19 15:27:52 +0000307 AliasAnalysis *AA, bool *IsLoad,
Jun Bum Lim180bc5a2017-02-02 15:12:34 +0000308 unsigned *NumScanedInst) {
Xin Tongaef0fcb2017-03-19 15:27:52 +0000309 // Don't CSE load that is volatile or anything stronger than unordered.
Anna Thomas9ad45ad2016-07-08 22:15:08 +0000310 if (!Load->isUnordered())
311 return nullptr;
312
Xin Tongaef0fcb2017-03-19 15:27:52 +0000313 return FindAvailablePtrLoadStore(
314 Load->getPointerOperand(), Load->getType(), Load->isAtomic(), ScanBB,
315 ScanFrom, MaxInstsToScan, AA, IsLoad, NumScanedInst);
316}
317
318Value *llvm::FindAvailablePtrLoadStore(Value *Ptr, Type *AccessTy,
319 bool AtLeastAtomic, BasicBlock *ScanBB,
320 BasicBlock::iterator &ScanFrom,
321 unsigned MaxInstsToScan,
322 AliasAnalysis *AA, bool *IsLoadCSE,
323 unsigned *NumScanedInst) {
324 if (MaxInstsToScan == 0)
325 MaxInstsToScan = ~0U;
326
Mehdi Amini46a43552015-03-04 18:43:29 +0000327 const DataLayout &DL = ScanBB->getModule()->getDataLayout();
Anna Thomas9ad45ad2016-07-08 22:15:08 +0000328
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000329 // Try to get the store size for the type.
George Burgess IV8c5413f32018-12-23 03:10:56 +0000330 auto AccessSize = LocationSize::precise(DL.getTypeStoreSize(AccessTy));
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000331
332 Value *StrippedPtr = Ptr->stripPointerCasts();
Chandler Carruthd67244d2014-10-18 23:19:03 +0000333
Dan Gohman826bdf82010-05-28 16:19:17 +0000334 while (ScanFrom != ScanBB->begin()) {
335 // We must ignore debug info directives when counting (otherwise they
336 // would affect codegen).
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000337 Instruction *Inst = &*--ScanFrom;
Dan Gohman826bdf82010-05-28 16:19:17 +0000338 if (isa<DbgInfoIntrinsic>(Inst))
339 continue;
340
341 // Restore ScanFrom to expected value in case next test succeeds
342 ScanFrom++;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000343
Jun Bum Lim180bc5a2017-02-02 15:12:34 +0000344 if (NumScanedInst)
345 ++(*NumScanedInst);
346
Dan Gohman826bdf82010-05-28 16:19:17 +0000347 // Don't scan huge blocks.
Chandler Carruthd67244d2014-10-18 23:19:03 +0000348 if (MaxInstsToScan-- == 0)
349 return nullptr;
350
Dan Gohman826bdf82010-05-28 16:19:17 +0000351 --ScanFrom;
352 // If this is a load of Ptr, the loaded value is available.
Eli Friedman4419cd22011-08-15 21:56:39 +0000353 // (This is true even if the load is volatile or atomic, although
354 // those cases are unlikely.)
Reid Klecknerfbd5eef2016-06-24 18:42:58 +0000355 if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
356 if (AreEquivalentAddressValues(
357 LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000358 CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
Philip Reames92c43692016-04-21 16:51:08 +0000359
360 // We can value forward from an atomic to a non-atomic, but not the
361 // other way around.
Xin Tongaef0fcb2017-03-19 15:27:52 +0000362 if (LI->isAtomic() < AtLeastAtomic)
Philip Reames92c43692016-04-21 16:51:08 +0000363 return nullptr;
364
Eli Friedmanbd254a62016-06-16 02:33:42 +0000365 if (IsLoadCSE)
366 *IsLoadCSE = true;
Dan Gohman826bdf82010-05-28 16:19:17 +0000367 return LI;
Chris Lattner87fa77b2012-03-13 18:07:41 +0000368 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000369
Dan Gohman826bdf82010-05-28 16:19:17 +0000370 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000371 Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
Dan Gohman826bdf82010-05-28 16:19:17 +0000372 // If this is a store through Ptr, the value is available!
Eli Friedman4419cd22011-08-15 21:56:39 +0000373 // (This is true even if the store is volatile or atomic, although
374 // those cases are unlikely.)
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000375 if (AreEquivalentAddressValues(StorePtr, StrippedPtr) &&
Chandler Carruth1a3c2c42014-11-25 08:20:27 +0000376 CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(),
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000377 AccessTy, DL)) {
Philip Reames92c43692016-04-21 16:51:08 +0000378
379 // We can value forward from an atomic to a non-atomic, but not the
380 // other way around.
Xin Tongaef0fcb2017-03-19 15:27:52 +0000381 if (SI->isAtomic() < AtLeastAtomic)
Philip Reames92c43692016-04-21 16:51:08 +0000382 return nullptr;
383
Eli Friedman02419a92016-08-08 04:10:22 +0000384 if (IsLoadCSE)
385 *IsLoadCSE = false;
Dan Gohman826bdf82010-05-28 16:19:17 +0000386 return SI->getOperand(0);
Chris Lattner87fa77b2012-03-13 18:07:41 +0000387 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000388
Chandler Carrutha32038b2014-10-20 10:03:01 +0000389 // If both StrippedPtr and StorePtr reach all the way to an alloca or
390 // global and they are different, ignore the store. This is a trivial form
391 // of alias analysis that is important for reg2mem'd code.
Chandler Carrutheeec35a2014-10-20 00:24:14 +0000392 if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
Chandler Carrutha32038b2014-10-20 10:03:01 +0000393 (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
394 StrippedPtr != StorePtr)
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 // If we have alias analysis and it says the store won't modify the loaded
398 // value, ignore the store.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000399 if (AA && !isModSet(AA->getModRefInfo(SI, StrippedPtr, AccessSize)))
Dan Gohman826bdf82010-05-28 16:19:17 +0000400 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000401
Dan Gohman826bdf82010-05-28 16:19:17 +0000402 // Otherwise the store that may or may not alias the pointer, bail out.
403 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000404 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000405 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000406
Dan Gohman826bdf82010-05-28 16:19:17 +0000407 // If this is some other instruction that may clobber Ptr, bail out.
408 if (Inst->mayWriteToMemory()) {
409 // If alias analysis claims that it really won't modify the load,
410 // ignore it.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000411 if (AA && !isModSet(AA->getModRefInfo(Inst, StrippedPtr, AccessSize)))
Dan Gohman826bdf82010-05-28 16:19:17 +0000412 continue;
Chandler Carruthd67244d2014-10-18 23:19:03 +0000413
Dan Gohman826bdf82010-05-28 16:19:17 +0000414 // May modify the pointer, bail out.
415 ++ScanFrom;
Craig Topper9f008862014-04-15 04:59:12 +0000416 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000417 }
418 }
Chandler Carruthd67244d2014-10-18 23:19:03 +0000419
Dan Gohman826bdf82010-05-28 16:19:17 +0000420 // Got to the start of the block, we didn't find it, but are done for this
421 // block.
Craig Topper9f008862014-04-15 04:59:12 +0000422 return nullptr;
Dan Gohman826bdf82010-05-28 16:19:17 +0000423}