1*9880d681SAndroid Build Coastguard Worker //===- Loads.cpp - Local load analysis ------------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines simple local analyses for load instructions.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/Loads.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/GlobalAlias.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/GlobalVariable.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Operator.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Statepoint.h"
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker
isAligned(const Value * Base,const APInt & Offset,unsigned Align,const DataLayout & DL)28*9880d681SAndroid Build Coastguard Worker static bool isAligned(const Value *Base, const APInt &Offset, unsigned Align,
29*9880d681SAndroid Build Coastguard Worker const DataLayout &DL) {
30*9880d681SAndroid Build Coastguard Worker APInt BaseAlign(Offset.getBitWidth(), Base->getPointerAlignment(DL));
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker if (!BaseAlign) {
33*9880d681SAndroid Build Coastguard Worker Type *Ty = Base->getType()->getPointerElementType();
34*9880d681SAndroid Build Coastguard Worker if (!Ty->isSized())
35*9880d681SAndroid Build Coastguard Worker return false;
36*9880d681SAndroid Build Coastguard Worker BaseAlign = DL.getABITypeAlignment(Ty);
37*9880d681SAndroid Build Coastguard Worker }
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Worker APInt Alignment(Offset.getBitWidth(), Align);
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker assert(Alignment.isPowerOf2() && "must be a power of 2!");
42*9880d681SAndroid Build Coastguard Worker return BaseAlign.uge(Alignment) && !(Offset & (Alignment-1));
43*9880d681SAndroid Build Coastguard Worker }
44*9880d681SAndroid Build Coastguard Worker
isAligned(const Value * Base,unsigned Align,const DataLayout & DL)45*9880d681SAndroid Build Coastguard Worker static bool isAligned(const Value *Base, unsigned Align, const DataLayout &DL) {
46*9880d681SAndroid Build Coastguard Worker Type *Ty = Base->getType();
47*9880d681SAndroid Build Coastguard Worker assert(Ty->isSized() && "must be sized");
48*9880d681SAndroid Build Coastguard Worker APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0);
49*9880d681SAndroid Build Coastguard Worker return isAligned(Base, Offset, Align, DL);
50*9880d681SAndroid Build Coastguard Worker }
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker /// Test if V is always a pointer to allocated and suitably aligned memory for
53*9880d681SAndroid Build Coastguard Worker /// a simple load or store.
isDereferenceableAndAlignedPointer(const Value * V,unsigned Align,const APInt & Size,const DataLayout & DL,const Instruction * CtxI,const DominatorTree * DT,SmallPtrSetImpl<const Value * > & Visited)54*9880d681SAndroid Build Coastguard Worker static bool isDereferenceableAndAlignedPointer(
55*9880d681SAndroid Build Coastguard Worker const Value *V, unsigned Align, const APInt &Size, const DataLayout &DL,
56*9880d681SAndroid Build Coastguard Worker const Instruction *CtxI, const DominatorTree *DT,
57*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<const Value *> &Visited) {
58*9880d681SAndroid Build Coastguard Worker // Note that it is not safe to speculate into a malloc'd region because
59*9880d681SAndroid Build Coastguard Worker // malloc may return null.
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker // bitcast instructions are no-ops as far as dereferenceability is concerned.
62*9880d681SAndroid Build Coastguard Worker if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V))
63*9880d681SAndroid Build Coastguard Worker return isDereferenceableAndAlignedPointer(BC->getOperand(0), Align, Size,
64*9880d681SAndroid Build Coastguard Worker DL, CtxI, DT, Visited);
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker bool CheckForNonNull = false;
67*9880d681SAndroid Build Coastguard Worker APInt KnownDerefBytes(Size.getBitWidth(),
68*9880d681SAndroid Build Coastguard Worker V->getPointerDereferenceableBytes(DL, CheckForNonNull));
69*9880d681SAndroid Build Coastguard Worker if (KnownDerefBytes.getBoolValue()) {
70*9880d681SAndroid Build Coastguard Worker if (KnownDerefBytes.uge(Size))
71*9880d681SAndroid Build Coastguard Worker if (!CheckForNonNull || isKnownNonNullAt(V, CtxI, DT))
72*9880d681SAndroid Build Coastguard Worker return isAligned(V, Align, DL);
73*9880d681SAndroid Build Coastguard Worker }
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker // For GEPs, determine if the indexing lands within the allocated object.
76*9880d681SAndroid Build Coastguard Worker if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
77*9880d681SAndroid Build Coastguard Worker const Value *Base = GEP->getPointerOperand();
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker APInt Offset(DL.getPointerTypeSizeInBits(GEP->getType()), 0);
80*9880d681SAndroid Build Coastguard Worker if (!GEP->accumulateConstantOffset(DL, Offset) || Offset.isNegative() ||
81*9880d681SAndroid Build Coastguard Worker !Offset.urem(APInt(Offset.getBitWidth(), Align)).isMinValue())
82*9880d681SAndroid Build Coastguard Worker return false;
83*9880d681SAndroid Build Coastguard Worker
84*9880d681SAndroid Build Coastguard Worker // If the base pointer is dereferenceable for Offset+Size bytes, then the
85*9880d681SAndroid Build Coastguard Worker // GEP (== Base + Offset) is dereferenceable for Size bytes. If the base
86*9880d681SAndroid Build Coastguard Worker // pointer is aligned to Align bytes, and the Offset is divisible by Align
87*9880d681SAndroid Build Coastguard Worker // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also
88*9880d681SAndroid Build Coastguard Worker // aligned to Align bytes.
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker return Visited.insert(Base).second &&
91*9880d681SAndroid Build Coastguard Worker isDereferenceableAndAlignedPointer(Base, Align, Offset + Size, DL,
92*9880d681SAndroid Build Coastguard Worker CtxI, DT, Visited);
93*9880d681SAndroid Build Coastguard Worker }
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker // For gc.relocate, look through relocations
96*9880d681SAndroid Build Coastguard Worker if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V))
97*9880d681SAndroid Build Coastguard Worker return isDereferenceableAndAlignedPointer(
98*9880d681SAndroid Build Coastguard Worker RelocateInst->getDerivedPtr(), Align, Size, DL, CtxI, DT, Visited);
99*9880d681SAndroid Build Coastguard Worker
100*9880d681SAndroid Build Coastguard Worker if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
101*9880d681SAndroid Build Coastguard Worker return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Align, Size,
102*9880d681SAndroid Build Coastguard Worker DL, CtxI, DT, Visited);
103*9880d681SAndroid Build Coastguard Worker
104*9880d681SAndroid Build Coastguard Worker if (auto CS = ImmutableCallSite(V))
105*9880d681SAndroid Build Coastguard Worker if (const Value *RV = CS.getReturnedArgOperand())
106*9880d681SAndroid Build Coastguard Worker return isDereferenceableAndAlignedPointer(RV, Align, Size, DL, CtxI, DT,
107*9880d681SAndroid Build Coastguard Worker Visited);
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker // If we don't know, assume the worst.
110*9880d681SAndroid Build Coastguard Worker return false;
111*9880d681SAndroid Build Coastguard Worker }
112*9880d681SAndroid Build Coastguard Worker
isDereferenceableAndAlignedPointer(const Value * V,unsigned Align,const DataLayout & DL,const Instruction * CtxI,const DominatorTree * DT)113*9880d681SAndroid Build Coastguard Worker bool llvm::isDereferenceableAndAlignedPointer(const Value *V, unsigned Align,
114*9880d681SAndroid Build Coastguard Worker const DataLayout &DL,
115*9880d681SAndroid Build Coastguard Worker const Instruction *CtxI,
116*9880d681SAndroid Build Coastguard Worker const DominatorTree *DT) {
117*9880d681SAndroid Build Coastguard Worker // When dereferenceability information is provided by a dereferenceable
118*9880d681SAndroid Build Coastguard Worker // attribute, we know exactly how many bytes are dereferenceable. If we can
119*9880d681SAndroid Build Coastguard Worker // determine the exact offset to the attributed variable, we can use that
120*9880d681SAndroid Build Coastguard Worker // information here.
121*9880d681SAndroid Build Coastguard Worker Type *VTy = V->getType();
122*9880d681SAndroid Build Coastguard Worker Type *Ty = VTy->getPointerElementType();
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker // Require ABI alignment for loads without alignment specification
125*9880d681SAndroid Build Coastguard Worker if (Align == 0)
126*9880d681SAndroid Build Coastguard Worker Align = DL.getABITypeAlignment(Ty);
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker if (!Ty->isSized())
129*9880d681SAndroid Build Coastguard Worker return false;
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const Value *, 32> Visited;
132*9880d681SAndroid Build Coastguard Worker return ::isDereferenceableAndAlignedPointer(
133*9880d681SAndroid Build Coastguard Worker V, Align, APInt(DL.getTypeSizeInBits(VTy), DL.getTypeStoreSize(Ty)), DL,
134*9880d681SAndroid Build Coastguard Worker CtxI, DT, Visited);
135*9880d681SAndroid Build Coastguard Worker }
136*9880d681SAndroid Build Coastguard Worker
isDereferenceablePointer(const Value * V,const DataLayout & DL,const Instruction * CtxI,const DominatorTree * DT)137*9880d681SAndroid Build Coastguard Worker bool llvm::isDereferenceablePointer(const Value *V, const DataLayout &DL,
138*9880d681SAndroid Build Coastguard Worker const Instruction *CtxI,
139*9880d681SAndroid Build Coastguard Worker const DominatorTree *DT) {
140*9880d681SAndroid Build Coastguard Worker return isDereferenceableAndAlignedPointer(V, 1, DL, CtxI, DT);
141*9880d681SAndroid Build Coastguard Worker }
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker /// \brief Test if A and B will obviously have the same value.
144*9880d681SAndroid Build Coastguard Worker ///
145*9880d681SAndroid Build Coastguard Worker /// This includes recognizing that %t0 and %t1 will have the same
146*9880d681SAndroid Build Coastguard Worker /// value in code like this:
147*9880d681SAndroid Build Coastguard Worker /// \code
148*9880d681SAndroid Build Coastguard Worker /// %t0 = getelementptr \@a, 0, 3
149*9880d681SAndroid Build Coastguard Worker /// store i32 0, i32* %t0
150*9880d681SAndroid Build Coastguard Worker /// %t1 = getelementptr \@a, 0, 3
151*9880d681SAndroid Build Coastguard Worker /// %t2 = load i32* %t1
152*9880d681SAndroid Build Coastguard Worker /// \endcode
153*9880d681SAndroid Build Coastguard Worker ///
AreEquivalentAddressValues(const Value * A,const Value * B)154*9880d681SAndroid Build Coastguard Worker static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
155*9880d681SAndroid Build Coastguard Worker // Test if the values are trivially equivalent.
156*9880d681SAndroid Build Coastguard Worker if (A == B)
157*9880d681SAndroid Build Coastguard Worker return true;
158*9880d681SAndroid Build Coastguard Worker
159*9880d681SAndroid Build Coastguard Worker // Test if the values come from identical arithmetic instructions.
160*9880d681SAndroid Build Coastguard Worker // Use isIdenticalToWhenDefined instead of isIdenticalTo because
161*9880d681SAndroid Build Coastguard Worker // this function is only used when one address use dominates the
162*9880d681SAndroid Build Coastguard Worker // other, which means that they'll always either have the same
163*9880d681SAndroid Build Coastguard Worker // value or one of them will have an undefined value.
164*9880d681SAndroid Build Coastguard Worker if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
165*9880d681SAndroid Build Coastguard Worker isa<GetElementPtrInst>(A))
166*9880d681SAndroid Build Coastguard Worker if (const Instruction *BI = dyn_cast<Instruction>(B))
167*9880d681SAndroid Build Coastguard Worker if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
168*9880d681SAndroid Build Coastguard Worker return true;
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker // Otherwise they may not be equivalent.
171*9880d681SAndroid Build Coastguard Worker return false;
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard Worker /// \brief Check if executing a load of this pointer value cannot trap.
175*9880d681SAndroid Build Coastguard Worker ///
176*9880d681SAndroid Build Coastguard Worker /// If DT and ScanFrom are specified this method performs context-sensitive
177*9880d681SAndroid Build Coastguard Worker /// analysis and returns true if it is safe to load immediately before ScanFrom.
178*9880d681SAndroid Build Coastguard Worker ///
179*9880d681SAndroid Build Coastguard Worker /// If it is not obviously safe to load from the specified pointer, we do
180*9880d681SAndroid Build Coastguard Worker /// a quick local scan of the basic block containing \c ScanFrom, to determine
181*9880d681SAndroid Build Coastguard Worker /// if the address is already accessed.
182*9880d681SAndroid Build Coastguard Worker ///
183*9880d681SAndroid Build Coastguard Worker /// This uses the pointee type to determine how many bytes need to be safe to
184*9880d681SAndroid Build Coastguard Worker /// load from the pointer.
isSafeToLoadUnconditionally(Value * V,unsigned Align,const DataLayout & DL,Instruction * ScanFrom,const DominatorTree * DT)185*9880d681SAndroid Build Coastguard Worker bool llvm::isSafeToLoadUnconditionally(Value *V, unsigned Align,
186*9880d681SAndroid Build Coastguard Worker const DataLayout &DL,
187*9880d681SAndroid Build Coastguard Worker Instruction *ScanFrom,
188*9880d681SAndroid Build Coastguard Worker const DominatorTree *DT) {
189*9880d681SAndroid Build Coastguard Worker // Zero alignment means that the load has the ABI alignment for the target
190*9880d681SAndroid Build Coastguard Worker if (Align == 0)
191*9880d681SAndroid Build Coastguard Worker Align = DL.getABITypeAlignment(V->getType()->getPointerElementType());
192*9880d681SAndroid Build Coastguard Worker assert(isPowerOf2_32(Align));
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker // If DT is not specified we can't make context-sensitive query
195*9880d681SAndroid Build Coastguard Worker const Instruction* CtxI = DT ? ScanFrom : nullptr;
196*9880d681SAndroid Build Coastguard Worker if (isDereferenceableAndAlignedPointer(V, Align, DL, CtxI, DT))
197*9880d681SAndroid Build Coastguard Worker return true;
198*9880d681SAndroid Build Coastguard Worker
199*9880d681SAndroid Build Coastguard Worker int64_t ByteOffset = 0;
200*9880d681SAndroid Build Coastguard Worker Value *Base = V;
201*9880d681SAndroid Build Coastguard Worker Base = GetPointerBaseWithConstantOffset(V, ByteOffset, DL);
202*9880d681SAndroid Build Coastguard Worker
203*9880d681SAndroid Build Coastguard Worker if (ByteOffset < 0) // out of bounds
204*9880d681SAndroid Build Coastguard Worker return false;
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard Worker Type *BaseType = nullptr;
207*9880d681SAndroid Build Coastguard Worker unsigned BaseAlign = 0;
208*9880d681SAndroid Build Coastguard Worker if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
209*9880d681SAndroid Build Coastguard Worker // An alloca is safe to load from as load as it is suitably aligned.
210*9880d681SAndroid Build Coastguard Worker BaseType = AI->getAllocatedType();
211*9880d681SAndroid Build Coastguard Worker BaseAlign = AI->getAlignment();
212*9880d681SAndroid Build Coastguard Worker } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
213*9880d681SAndroid Build Coastguard Worker // Global variables are not necessarily safe to load from if they are
214*9880d681SAndroid Build Coastguard Worker // interposed arbitrarily. Their size may change or they may be weak and
215*9880d681SAndroid Build Coastguard Worker // require a test to determine if they were in fact provided.
216*9880d681SAndroid Build Coastguard Worker if (!GV->isInterposable()) {
217*9880d681SAndroid Build Coastguard Worker BaseType = GV->getType()->getElementType();
218*9880d681SAndroid Build Coastguard Worker BaseAlign = GV->getAlignment();
219*9880d681SAndroid Build Coastguard Worker }
220*9880d681SAndroid Build Coastguard Worker }
221*9880d681SAndroid Build Coastguard Worker
222*9880d681SAndroid Build Coastguard Worker PointerType *AddrTy = cast<PointerType>(V->getType());
223*9880d681SAndroid Build Coastguard Worker uint64_t LoadSize = DL.getTypeStoreSize(AddrTy->getElementType());
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard Worker // If we found a base allocated type from either an alloca or global variable,
226*9880d681SAndroid Build Coastguard Worker // try to see if we are definitively within the allocated region. We need to
227*9880d681SAndroid Build Coastguard Worker // know the size of the base type and the loaded type to do anything in this
228*9880d681SAndroid Build Coastguard Worker // case.
229*9880d681SAndroid Build Coastguard Worker if (BaseType && BaseType->isSized()) {
230*9880d681SAndroid Build Coastguard Worker if (BaseAlign == 0)
231*9880d681SAndroid Build Coastguard Worker BaseAlign = DL.getPrefTypeAlignment(BaseType);
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker if (Align <= BaseAlign) {
234*9880d681SAndroid Build Coastguard Worker // Check if the load is within the bounds of the underlying object.
235*9880d681SAndroid Build Coastguard Worker if (ByteOffset + LoadSize <= DL.getTypeAllocSize(BaseType) &&
236*9880d681SAndroid Build Coastguard Worker ((ByteOffset % Align) == 0))
237*9880d681SAndroid Build Coastguard Worker return true;
238*9880d681SAndroid Build Coastguard Worker }
239*9880d681SAndroid Build Coastguard Worker }
240*9880d681SAndroid Build Coastguard Worker
241*9880d681SAndroid Build Coastguard Worker if (!ScanFrom)
242*9880d681SAndroid Build Coastguard Worker return false;
243*9880d681SAndroid Build Coastguard Worker
244*9880d681SAndroid Build Coastguard Worker // Otherwise, be a little bit aggressive by scanning the local block where we
245*9880d681SAndroid Build Coastguard Worker // want to check to see if the pointer is already being loaded or stored
246*9880d681SAndroid Build Coastguard Worker // from/to. If so, the previous load or store would have already trapped,
247*9880d681SAndroid Build Coastguard Worker // so there is no harm doing an extra load (also, CSE will later eliminate
248*9880d681SAndroid Build Coastguard Worker // the load entirely).
249*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator BBI = ScanFrom->getIterator(),
250*9880d681SAndroid Build Coastguard Worker E = ScanFrom->getParent()->begin();
251*9880d681SAndroid Build Coastguard Worker
252*9880d681SAndroid Build Coastguard Worker // We can at least always strip pointer casts even though we can't use the
253*9880d681SAndroid Build Coastguard Worker // base here.
254*9880d681SAndroid Build Coastguard Worker V = V->stripPointerCasts();
255*9880d681SAndroid Build Coastguard Worker
256*9880d681SAndroid Build Coastguard Worker while (BBI != E) {
257*9880d681SAndroid Build Coastguard Worker --BBI;
258*9880d681SAndroid Build Coastguard Worker
259*9880d681SAndroid Build Coastguard Worker // If we see a free or a call which may write to memory (i.e. which might do
260*9880d681SAndroid Build Coastguard Worker // a free) the pointer could be marked invalid.
261*9880d681SAndroid Build Coastguard Worker if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
262*9880d681SAndroid Build Coastguard Worker !isa<DbgInfoIntrinsic>(BBI))
263*9880d681SAndroid Build Coastguard Worker return false;
264*9880d681SAndroid Build Coastguard Worker
265*9880d681SAndroid Build Coastguard Worker Value *AccessedPtr;
266*9880d681SAndroid Build Coastguard Worker unsigned AccessedAlign;
267*9880d681SAndroid Build Coastguard Worker if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
268*9880d681SAndroid Build Coastguard Worker AccessedPtr = LI->getPointerOperand();
269*9880d681SAndroid Build Coastguard Worker AccessedAlign = LI->getAlignment();
270*9880d681SAndroid Build Coastguard Worker } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
271*9880d681SAndroid Build Coastguard Worker AccessedPtr = SI->getPointerOperand();
272*9880d681SAndroid Build Coastguard Worker AccessedAlign = SI->getAlignment();
273*9880d681SAndroid Build Coastguard Worker } else
274*9880d681SAndroid Build Coastguard Worker continue;
275*9880d681SAndroid Build Coastguard Worker
276*9880d681SAndroid Build Coastguard Worker Type *AccessedTy = AccessedPtr->getType()->getPointerElementType();
277*9880d681SAndroid Build Coastguard Worker if (AccessedAlign == 0)
278*9880d681SAndroid Build Coastguard Worker AccessedAlign = DL.getABITypeAlignment(AccessedTy);
279*9880d681SAndroid Build Coastguard Worker if (AccessedAlign < Align)
280*9880d681SAndroid Build Coastguard Worker continue;
281*9880d681SAndroid Build Coastguard Worker
282*9880d681SAndroid Build Coastguard Worker // Handle trivial cases.
283*9880d681SAndroid Build Coastguard Worker if (AccessedPtr == V)
284*9880d681SAndroid Build Coastguard Worker return true;
285*9880d681SAndroid Build Coastguard Worker
286*9880d681SAndroid Build Coastguard Worker if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
287*9880d681SAndroid Build Coastguard Worker LoadSize <= DL.getTypeStoreSize(AccessedTy))
288*9880d681SAndroid Build Coastguard Worker return true;
289*9880d681SAndroid Build Coastguard Worker }
290*9880d681SAndroid Build Coastguard Worker return false;
291*9880d681SAndroid Build Coastguard Worker }
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker /// DefMaxInstsToScan - the default number of maximum instructions
294*9880d681SAndroid Build Coastguard Worker /// to scan in the block, used by FindAvailableLoadedValue().
295*9880d681SAndroid Build Coastguard Worker /// FindAvailableLoadedValue() was introduced in r60148, to improve jump
296*9880d681SAndroid Build Coastguard Worker /// threading in part by eliminating partially redundant loads.
297*9880d681SAndroid Build Coastguard Worker /// At that point, the value of MaxInstsToScan was already set to '6'
298*9880d681SAndroid Build Coastguard Worker /// without documented explanation.
299*9880d681SAndroid Build Coastguard Worker cl::opt<unsigned>
300*9880d681SAndroid Build Coastguard Worker llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
301*9880d681SAndroid Build Coastguard Worker cl::desc("Use this to specify the default maximum number of instructions "
302*9880d681SAndroid Build Coastguard Worker "to scan backward from a given instruction, when searching for "
303*9880d681SAndroid Build Coastguard Worker "available loaded value"));
304*9880d681SAndroid Build Coastguard Worker
FindAvailableLoadedValue(LoadInst * Load,BasicBlock * ScanBB,BasicBlock::iterator & ScanFrom,unsigned MaxInstsToScan,AliasAnalysis * AA,AAMDNodes * AATags,bool * IsLoadCSE)305*9880d681SAndroid Build Coastguard Worker Value *llvm::FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB,
306*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator &ScanFrom,
307*9880d681SAndroid Build Coastguard Worker unsigned MaxInstsToScan,
308*9880d681SAndroid Build Coastguard Worker AliasAnalysis *AA, AAMDNodes *AATags,
309*9880d681SAndroid Build Coastguard Worker bool *IsLoadCSE) {
310*9880d681SAndroid Build Coastguard Worker if (MaxInstsToScan == 0)
311*9880d681SAndroid Build Coastguard Worker MaxInstsToScan = ~0U;
312*9880d681SAndroid Build Coastguard Worker
313*9880d681SAndroid Build Coastguard Worker Value *Ptr = Load->getPointerOperand();
314*9880d681SAndroid Build Coastguard Worker Type *AccessTy = Load->getType();
315*9880d681SAndroid Build Coastguard Worker
316*9880d681SAndroid Build Coastguard Worker // We can never remove a volatile load
317*9880d681SAndroid Build Coastguard Worker if (Load->isVolatile())
318*9880d681SAndroid Build Coastguard Worker return nullptr;
319*9880d681SAndroid Build Coastguard Worker
320*9880d681SAndroid Build Coastguard Worker // Anything stronger than unordered is currently unimplemented.
321*9880d681SAndroid Build Coastguard Worker if (!Load->isUnordered())
322*9880d681SAndroid Build Coastguard Worker return nullptr;
323*9880d681SAndroid Build Coastguard Worker
324*9880d681SAndroid Build Coastguard Worker const DataLayout &DL = ScanBB->getModule()->getDataLayout();
325*9880d681SAndroid Build Coastguard Worker
326*9880d681SAndroid Build Coastguard Worker // Try to get the store size for the type.
327*9880d681SAndroid Build Coastguard Worker uint64_t AccessSize = DL.getTypeStoreSize(AccessTy);
328*9880d681SAndroid Build Coastguard Worker
329*9880d681SAndroid Build Coastguard Worker Value *StrippedPtr = Ptr->stripPointerCasts();
330*9880d681SAndroid Build Coastguard Worker
331*9880d681SAndroid Build Coastguard Worker while (ScanFrom != ScanBB->begin()) {
332*9880d681SAndroid Build Coastguard Worker // We must ignore debug info directives when counting (otherwise they
333*9880d681SAndroid Build Coastguard Worker // would affect codegen).
334*9880d681SAndroid Build Coastguard Worker Instruction *Inst = &*--ScanFrom;
335*9880d681SAndroid Build Coastguard Worker if (isa<DbgInfoIntrinsic>(Inst))
336*9880d681SAndroid Build Coastguard Worker continue;
337*9880d681SAndroid Build Coastguard Worker
338*9880d681SAndroid Build Coastguard Worker // Restore ScanFrom to expected value in case next test succeeds
339*9880d681SAndroid Build Coastguard Worker ScanFrom++;
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard Worker // Don't scan huge blocks.
342*9880d681SAndroid Build Coastguard Worker if (MaxInstsToScan-- == 0)
343*9880d681SAndroid Build Coastguard Worker return nullptr;
344*9880d681SAndroid Build Coastguard Worker
345*9880d681SAndroid Build Coastguard Worker --ScanFrom;
346*9880d681SAndroid Build Coastguard Worker // If this is a load of Ptr, the loaded value is available.
347*9880d681SAndroid Build Coastguard Worker // (This is true even if the load is volatile or atomic, although
348*9880d681SAndroid Build Coastguard Worker // those cases are unlikely.)
349*9880d681SAndroid Build Coastguard Worker if (LoadInst *LI = dyn_cast<LoadInst>(Inst))
350*9880d681SAndroid Build Coastguard Worker if (AreEquivalentAddressValues(
351*9880d681SAndroid Build Coastguard Worker LI->getPointerOperand()->stripPointerCasts(), StrippedPtr) &&
352*9880d681SAndroid Build Coastguard Worker CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
353*9880d681SAndroid Build Coastguard Worker
354*9880d681SAndroid Build Coastguard Worker // We can value forward from an atomic to a non-atomic, but not the
355*9880d681SAndroid Build Coastguard Worker // other way around.
356*9880d681SAndroid Build Coastguard Worker if (LI->isAtomic() < Load->isAtomic())
357*9880d681SAndroid Build Coastguard Worker return nullptr;
358*9880d681SAndroid Build Coastguard Worker
359*9880d681SAndroid Build Coastguard Worker if (AATags)
360*9880d681SAndroid Build Coastguard Worker LI->getAAMetadata(*AATags);
361*9880d681SAndroid Build Coastguard Worker if (IsLoadCSE)
362*9880d681SAndroid Build Coastguard Worker *IsLoadCSE = true;
363*9880d681SAndroid Build Coastguard Worker return LI;
364*9880d681SAndroid Build Coastguard Worker }
365*9880d681SAndroid Build Coastguard Worker
366*9880d681SAndroid Build Coastguard Worker if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
367*9880d681SAndroid Build Coastguard Worker Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
368*9880d681SAndroid Build Coastguard Worker // If this is a store through Ptr, the value is available!
369*9880d681SAndroid Build Coastguard Worker // (This is true even if the store is volatile or atomic, although
370*9880d681SAndroid Build Coastguard Worker // those cases are unlikely.)
371*9880d681SAndroid Build Coastguard Worker if (AreEquivalentAddressValues(StorePtr, StrippedPtr) &&
372*9880d681SAndroid Build Coastguard Worker CastInst::isBitOrNoopPointerCastable(SI->getValueOperand()->getType(),
373*9880d681SAndroid Build Coastguard Worker AccessTy, DL)) {
374*9880d681SAndroid Build Coastguard Worker
375*9880d681SAndroid Build Coastguard Worker // We can value forward from an atomic to a non-atomic, but not the
376*9880d681SAndroid Build Coastguard Worker // other way around.
377*9880d681SAndroid Build Coastguard Worker if (SI->isAtomic() < Load->isAtomic())
378*9880d681SAndroid Build Coastguard Worker return nullptr;
379*9880d681SAndroid Build Coastguard Worker
380*9880d681SAndroid Build Coastguard Worker if (AATags)
381*9880d681SAndroid Build Coastguard Worker SI->getAAMetadata(*AATags);
382*9880d681SAndroid Build Coastguard Worker return SI->getOperand(0);
383*9880d681SAndroid Build Coastguard Worker }
384*9880d681SAndroid Build Coastguard Worker
385*9880d681SAndroid Build Coastguard Worker // If both StrippedPtr and StorePtr reach all the way to an alloca or
386*9880d681SAndroid Build Coastguard Worker // global and they are different, ignore the store. This is a trivial form
387*9880d681SAndroid Build Coastguard Worker // of alias analysis that is important for reg2mem'd code.
388*9880d681SAndroid Build Coastguard Worker if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
389*9880d681SAndroid Build Coastguard Worker (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
390*9880d681SAndroid Build Coastguard Worker StrippedPtr != StorePtr)
391*9880d681SAndroid Build Coastguard Worker continue;
392*9880d681SAndroid Build Coastguard Worker
393*9880d681SAndroid Build Coastguard Worker // If we have alias analysis and it says the store won't modify the loaded
394*9880d681SAndroid Build Coastguard Worker // value, ignore the store.
395*9880d681SAndroid Build Coastguard Worker if (AA && (AA->getModRefInfo(SI, StrippedPtr, AccessSize) & MRI_Mod) == 0)
396*9880d681SAndroid Build Coastguard Worker continue;
397*9880d681SAndroid Build Coastguard Worker
398*9880d681SAndroid Build Coastguard Worker // Otherwise the store that may or may not alias the pointer, bail out.
399*9880d681SAndroid Build Coastguard Worker ++ScanFrom;
400*9880d681SAndroid Build Coastguard Worker return nullptr;
401*9880d681SAndroid Build Coastguard Worker }
402*9880d681SAndroid Build Coastguard Worker
403*9880d681SAndroid Build Coastguard Worker // If this is some other instruction that may clobber Ptr, bail out.
404*9880d681SAndroid Build Coastguard Worker if (Inst->mayWriteToMemory()) {
405*9880d681SAndroid Build Coastguard Worker // If alias analysis claims that it really won't modify the load,
406*9880d681SAndroid Build Coastguard Worker // ignore it.
407*9880d681SAndroid Build Coastguard Worker if (AA &&
408*9880d681SAndroid Build Coastguard Worker (AA->getModRefInfo(Inst, StrippedPtr, AccessSize) & MRI_Mod) == 0)
409*9880d681SAndroid Build Coastguard Worker continue;
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker // May modify the pointer, bail out.
412*9880d681SAndroid Build Coastguard Worker ++ScanFrom;
413*9880d681SAndroid Build Coastguard Worker return nullptr;
414*9880d681SAndroid Build Coastguard Worker }
415*9880d681SAndroid Build Coastguard Worker }
416*9880d681SAndroid Build Coastguard Worker
417*9880d681SAndroid Build Coastguard Worker // Got to the start of the block, we didn't find it, but are done for this
418*9880d681SAndroid Build Coastguard Worker // block.
419*9880d681SAndroid Build Coastguard Worker return nullptr;
420*9880d681SAndroid Build Coastguard Worker }
421