109467b48Spatrick //===- Loads.cpp - Local load analysis ------------------------------------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file defines simple local analyses for load instructions.
1009467b48Spatrick //
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick
1309467b48Spatrick #include "llvm/Analysis/Loads.h"
1409467b48Spatrick #include "llvm/Analysis/AliasAnalysis.h"
1573471bf0Spatrick #include "llvm/Analysis/AssumeBundleQueries.h"
1609467b48Spatrick #include "llvm/Analysis/LoopInfo.h"
1773471bf0Spatrick #include "llvm/Analysis/MemoryBuiltins.h"
1873471bf0Spatrick #include "llvm/Analysis/MemoryLocation.h"
1909467b48Spatrick #include "llvm/Analysis/ScalarEvolution.h"
2009467b48Spatrick #include "llvm/Analysis/ScalarEvolutionExpressions.h"
2109467b48Spatrick #include "llvm/Analysis/ValueTracking.h"
2209467b48Spatrick #include "llvm/IR/DataLayout.h"
2309467b48Spatrick #include "llvm/IR/IntrinsicInst.h"
2409467b48Spatrick #include "llvm/IR/Module.h"
2509467b48Spatrick #include "llvm/IR/Operator.h"
2609467b48Spatrick
2709467b48Spatrick using namespace llvm;
2809467b48Spatrick
isAligned(const Value * Base,const APInt & Offset,Align Alignment,const DataLayout & DL)2909467b48Spatrick static bool isAligned(const Value *Base, const APInt &Offset, Align Alignment,
3009467b48Spatrick const DataLayout &DL) {
31097a140dSpatrick Align BA = Base->getPointerAlignment(DL);
3209467b48Spatrick const APInt APAlign(Offset.getBitWidth(), Alignment.value());
3309467b48Spatrick assert(APAlign.isPowerOf2() && "must be a power of 2!");
34097a140dSpatrick return BA >= Alignment && !(Offset & (APAlign - 1));
3509467b48Spatrick }
3609467b48Spatrick
3709467b48Spatrick /// Test if V is always a pointer to allocated and suitably aligned memory for
3809467b48Spatrick /// a simple load or store.
isDereferenceableAndAlignedPointer(const Value * V,Align Alignment,const APInt & Size,const DataLayout & DL,const Instruction * CtxI,AssumptionCache * AC,const DominatorTree * DT,const TargetLibraryInfo * TLI,SmallPtrSetImpl<const Value * > & Visited,unsigned MaxDepth)3909467b48Spatrick static bool isDereferenceableAndAlignedPointer(
4009467b48Spatrick const Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
41*d415bd75Srobert const Instruction *CtxI, AssumptionCache *AC, const DominatorTree *DT,
4273471bf0Spatrick const TargetLibraryInfo *TLI, SmallPtrSetImpl<const Value *> &Visited,
4373471bf0Spatrick unsigned MaxDepth) {
44097a140dSpatrick assert(V->getType()->isPointerTy() && "Base must be pointer");
45097a140dSpatrick
46097a140dSpatrick // Recursion limit.
47097a140dSpatrick if (MaxDepth-- == 0)
48097a140dSpatrick return false;
49097a140dSpatrick
5009467b48Spatrick // Already visited? Bail out, we've likely hit unreachable code.
5109467b48Spatrick if (!Visited.insert(V).second)
5209467b48Spatrick return false;
5309467b48Spatrick
5409467b48Spatrick // Note that it is not safe to speculate into a malloc'd region because
5509467b48Spatrick // malloc may return null.
5609467b48Spatrick
5709467b48Spatrick // For GEPs, determine if the indexing lands within the allocated object.
5809467b48Spatrick if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
5909467b48Spatrick const Value *Base = GEP->getPointerOperand();
6009467b48Spatrick
6109467b48Spatrick APInt Offset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
6209467b48Spatrick if (!GEP->accumulateConstantOffset(DL, Offset) || Offset.isNegative() ||
6309467b48Spatrick !Offset.urem(APInt(Offset.getBitWidth(), Alignment.value()))
6409467b48Spatrick .isMinValue())
6509467b48Spatrick return false;
6609467b48Spatrick
6709467b48Spatrick // If the base pointer is dereferenceable for Offset+Size bytes, then the
6809467b48Spatrick // GEP (== Base + Offset) is dereferenceable for Size bytes. If the base
6909467b48Spatrick // pointer is aligned to Align bytes, and the Offset is divisible by Align
7009467b48Spatrick // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also
7109467b48Spatrick // aligned to Align bytes.
7209467b48Spatrick
7309467b48Spatrick // Offset and Size may have different bit widths if we have visited an
7409467b48Spatrick // addrspacecast, so we can't do arithmetic directly on the APInt values.
7509467b48Spatrick return isDereferenceableAndAlignedPointer(
7609467b48Spatrick Base, Alignment, Offset + Size.sextOrTrunc(Offset.getBitWidth()), DL,
77*d415bd75Srobert CtxI, AC, DT, TLI, Visited, MaxDepth);
7809467b48Spatrick }
7909467b48Spatrick
80*d415bd75Srobert // bitcast instructions are no-ops as far as dereferenceability is concerned.
81*d415bd75Srobert if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) {
82*d415bd75Srobert if (BC->getSrcTy()->isPointerTy())
83*d415bd75Srobert return isDereferenceableAndAlignedPointer(
84*d415bd75Srobert BC->getOperand(0), Alignment, Size, DL, CtxI, AC, DT, TLI,
8573471bf0Spatrick Visited, MaxDepth);
86*d415bd75Srobert }
87*d415bd75Srobert
88*d415bd75Srobert // Recurse into both hands of select.
89*d415bd75Srobert if (const SelectInst *Sel = dyn_cast<SelectInst>(V)) {
90*d415bd75Srobert return isDereferenceableAndAlignedPointer(Sel->getTrueValue(), Alignment,
91*d415bd75Srobert Size, DL, CtxI, AC, DT, TLI,
92*d415bd75Srobert Visited, MaxDepth) &&
93*d415bd75Srobert isDereferenceableAndAlignedPointer(Sel->getFalseValue(), Alignment,
94*d415bd75Srobert Size, DL, CtxI, AC, DT, TLI,
95*d415bd75Srobert Visited, MaxDepth);
96*d415bd75Srobert }
97*d415bd75Srobert
98*d415bd75Srobert bool CheckForNonNull, CheckForFreed;
99*d415bd75Srobert APInt KnownDerefBytes(Size.getBitWidth(),
100*d415bd75Srobert V->getPointerDereferenceableBytes(DL, CheckForNonNull,
101*d415bd75Srobert CheckForFreed));
102*d415bd75Srobert if (KnownDerefBytes.getBoolValue() && KnownDerefBytes.uge(Size) &&
103*d415bd75Srobert !CheckForFreed)
104*d415bd75Srobert if (!CheckForNonNull || isKnownNonZero(V, DL, 0, AC, CtxI, DT)) {
105*d415bd75Srobert // As we recursed through GEPs to get here, we've incrementally checked
106*d415bd75Srobert // that each step advanced by a multiple of the alignment. If our base is
107*d415bd75Srobert // properly aligned, then the original offset accessed must also be.
108*d415bd75Srobert APInt Offset(DL.getTypeStoreSizeInBits(V->getType()), 0);
109*d415bd75Srobert return isAligned(V, Offset, Alignment, DL);
110*d415bd75Srobert }
111*d415bd75Srobert
112*d415bd75Srobert /// TODO refactor this function to be able to search independently for
113*d415bd75Srobert /// Dereferencability and Alignment requirements.
114*d415bd75Srobert
11509467b48Spatrick
11673471bf0Spatrick if (const auto *Call = dyn_cast<CallBase>(V)) {
11709467b48Spatrick if (auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
11809467b48Spatrick return isDereferenceableAndAlignedPointer(RP, Alignment, Size, DL, CtxI,
119*d415bd75Srobert AC, DT, TLI, Visited, MaxDepth);
12073471bf0Spatrick
12173471bf0Spatrick // If we have a call we can't recurse through, check to see if this is an
12273471bf0Spatrick // allocation function for which we can establish an minimum object size.
12373471bf0Spatrick // Such a minimum object size is analogous to a deref_or_null attribute in
12473471bf0Spatrick // that we still need to prove the result non-null at point of use.
12573471bf0Spatrick // NOTE: We can only use the object size as a base fact as we a) need to
12673471bf0Spatrick // prove alignment too, and b) don't want the compile time impact of a
12773471bf0Spatrick // separate recursive walk.
12873471bf0Spatrick ObjectSizeOpts Opts;
12973471bf0Spatrick // TODO: It may be okay to round to align, but that would imply that
13073471bf0Spatrick // accessing slightly out of bounds was legal, and we're currently
13173471bf0Spatrick // inconsistent about that. For the moment, be conservative.
13273471bf0Spatrick Opts.RoundToAlign = false;
13373471bf0Spatrick Opts.NullIsUnknownSize = true;
13473471bf0Spatrick uint64_t ObjSize;
13573471bf0Spatrick if (getObjectSize(V, ObjSize, DL, TLI, Opts)) {
13673471bf0Spatrick APInt KnownDerefBytes(Size.getBitWidth(), ObjSize);
13773471bf0Spatrick if (KnownDerefBytes.getBoolValue() && KnownDerefBytes.uge(Size) &&
138*d415bd75Srobert isKnownNonZero(V, DL, 0, AC, CtxI, DT) && !V->canBeFreed()) {
13973471bf0Spatrick // As we recursed through GEPs to get here, we've incrementally
14073471bf0Spatrick // checked that each step advanced by a multiple of the alignment. If
14173471bf0Spatrick // our base is properly aligned, then the original offset accessed
14273471bf0Spatrick // must also be.
143*d415bd75Srobert APInt Offset(DL.getTypeStoreSizeInBits(V->getType()), 0);
14473471bf0Spatrick return isAligned(V, Offset, Alignment, DL);
14573471bf0Spatrick }
14673471bf0Spatrick }
14773471bf0Spatrick }
14809467b48Spatrick
149*d415bd75Srobert // For gc.relocate, look through relocations
150*d415bd75Srobert if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V))
151*d415bd75Srobert return isDereferenceableAndAlignedPointer(RelocateInst->getDerivedPtr(),
152*d415bd75Srobert Alignment, Size, DL, CtxI, AC, DT,
153*d415bd75Srobert TLI, Visited, MaxDepth);
154*d415bd75Srobert
155*d415bd75Srobert if (const AddrSpaceCastOperator *ASC = dyn_cast<AddrSpaceCastOperator>(V))
156*d415bd75Srobert return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Alignment,
157*d415bd75Srobert Size, DL, CtxI, AC, DT, TLI,
158*d415bd75Srobert Visited, MaxDepth);
159*d415bd75Srobert
160*d415bd75Srobert if (CtxI) {
161*d415bd75Srobert /// Look through assumes to see if both dereferencability and alignment can
162*d415bd75Srobert /// be provent by an assume
163*d415bd75Srobert RetainedKnowledge AlignRK;
164*d415bd75Srobert RetainedKnowledge DerefRK;
165*d415bd75Srobert if (getKnowledgeForValue(
166*d415bd75Srobert V, {Attribute::Dereferenceable, Attribute::Alignment}, AC,
167*d415bd75Srobert [&](RetainedKnowledge RK, Instruction *Assume, auto) {
168*d415bd75Srobert if (!isValidAssumeForContext(Assume, CtxI))
169*d415bd75Srobert return false;
170*d415bd75Srobert if (RK.AttrKind == Attribute::Alignment)
171*d415bd75Srobert AlignRK = std::max(AlignRK, RK);
172*d415bd75Srobert if (RK.AttrKind == Attribute::Dereferenceable)
173*d415bd75Srobert DerefRK = std::max(DerefRK, RK);
174*d415bd75Srobert if (AlignRK && DerefRK && AlignRK.ArgValue >= Alignment.value() &&
175*d415bd75Srobert DerefRK.ArgValue >= Size.getZExtValue())
176*d415bd75Srobert return true; // We have found what we needed so we stop looking
177*d415bd75Srobert return false; // Other assumes may have better information. so
178*d415bd75Srobert // keep looking
179*d415bd75Srobert }))
180*d415bd75Srobert return true;
181*d415bd75Srobert }
182*d415bd75Srobert
18309467b48Spatrick // If we don't know, assume the worst.
18409467b48Spatrick return false;
18509467b48Spatrick }
18609467b48Spatrick
isDereferenceableAndAlignedPointer(const Value * V,Align Alignment,const APInt & Size,const DataLayout & DL,const Instruction * CtxI,AssumptionCache * AC,const DominatorTree * DT,const TargetLibraryInfo * TLI)187*d415bd75Srobert bool llvm::isDereferenceableAndAlignedPointer(
188*d415bd75Srobert const Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
189*d415bd75Srobert const Instruction *CtxI, AssumptionCache *AC, const DominatorTree *DT,
19073471bf0Spatrick const TargetLibraryInfo *TLI) {
19109467b48Spatrick // Note: At the moment, Size can be zero. This ends up being interpreted as
19209467b48Spatrick // a query of whether [Base, V] is dereferenceable and V is aligned (since
19309467b48Spatrick // that's what the implementation happened to do). It's unclear if this is
19409467b48Spatrick // the desired semantic, but at least SelectionDAG does exercise this case.
19509467b48Spatrick
19609467b48Spatrick SmallPtrSet<const Value *, 32> Visited;
197*d415bd75Srobert return ::isDereferenceableAndAlignedPointer(V, Alignment, Size, DL, CtxI, AC,
198*d415bd75Srobert DT, TLI, Visited, 16);
19909467b48Spatrick }
20009467b48Spatrick
isDereferenceableAndAlignedPointer(const Value * V,Type * Ty,Align Alignment,const DataLayout & DL,const Instruction * CtxI,AssumptionCache * AC,const DominatorTree * DT,const TargetLibraryInfo * TLI)201*d415bd75Srobert bool llvm::isDereferenceableAndAlignedPointer(
202*d415bd75Srobert const Value *V, Type *Ty, Align Alignment, const DataLayout &DL,
203*d415bd75Srobert const Instruction *CtxI, AssumptionCache *AC, const DominatorTree *DT,
20473471bf0Spatrick const TargetLibraryInfo *TLI) {
205097a140dSpatrick // For unsized types or scalable vectors we don't know exactly how many bytes
206097a140dSpatrick // are dereferenced, so bail out.
207097a140dSpatrick if (!Ty->isSized() || isa<ScalableVectorType>(Ty))
20809467b48Spatrick return false;
20909467b48Spatrick
21009467b48Spatrick // When dereferenceability information is provided by a dereferenceable
21109467b48Spatrick // attribute, we know exactly how many bytes are dereferenceable. If we can
21209467b48Spatrick // determine the exact offset to the attributed variable, we can use that
21309467b48Spatrick // information here.
21409467b48Spatrick
21509467b48Spatrick APInt AccessSize(DL.getPointerTypeSizeInBits(V->getType()),
21609467b48Spatrick DL.getTypeStoreSize(Ty));
21709467b48Spatrick return isDereferenceableAndAlignedPointer(V, Alignment, AccessSize, DL, CtxI,
218*d415bd75Srobert AC, DT, TLI);
21909467b48Spatrick }
22009467b48Spatrick
isDereferenceablePointer(const Value * V,Type * Ty,const DataLayout & DL,const Instruction * CtxI,AssumptionCache * AC,const DominatorTree * DT,const TargetLibraryInfo * TLI)22109467b48Spatrick bool llvm::isDereferenceablePointer(const Value *V, Type *Ty,
22209467b48Spatrick const DataLayout &DL,
22309467b48Spatrick const Instruction *CtxI,
224*d415bd75Srobert AssumptionCache *AC,
22573471bf0Spatrick const DominatorTree *DT,
22673471bf0Spatrick const TargetLibraryInfo *TLI) {
227*d415bd75Srobert return isDereferenceableAndAlignedPointer(V, Ty, Align(1), DL, CtxI, AC, DT,
228*d415bd75Srobert TLI);
22909467b48Spatrick }
23009467b48Spatrick
23109467b48Spatrick /// Test if A and B will obviously have the same value.
23209467b48Spatrick ///
23309467b48Spatrick /// This includes recognizing that %t0 and %t1 will have the same
23409467b48Spatrick /// value in code like this:
23509467b48Spatrick /// \code
23609467b48Spatrick /// %t0 = getelementptr \@a, 0, 3
23709467b48Spatrick /// store i32 0, i32* %t0
23809467b48Spatrick /// %t1 = getelementptr \@a, 0, 3
23909467b48Spatrick /// %t2 = load i32* %t1
24009467b48Spatrick /// \endcode
24109467b48Spatrick ///
AreEquivalentAddressValues(const Value * A,const Value * B)24209467b48Spatrick static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
24309467b48Spatrick // Test if the values are trivially equivalent.
24409467b48Spatrick if (A == B)
24509467b48Spatrick return true;
24609467b48Spatrick
24709467b48Spatrick // Test if the values come from identical arithmetic instructions.
24809467b48Spatrick // Use isIdenticalToWhenDefined instead of isIdenticalTo because
24909467b48Spatrick // this function is only used when one address use dominates the
25009467b48Spatrick // other, which means that they'll always either have the same
25109467b48Spatrick // value or one of them will have an undefined value.
25209467b48Spatrick if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
25309467b48Spatrick isa<GetElementPtrInst>(A))
25409467b48Spatrick if (const Instruction *BI = dyn_cast<Instruction>(B))
25509467b48Spatrick if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
25609467b48Spatrick return true;
25709467b48Spatrick
25809467b48Spatrick // Otherwise they may not be equivalent.
25909467b48Spatrick return false;
26009467b48Spatrick }
26109467b48Spatrick
isDereferenceableAndAlignedInLoop(LoadInst * LI,Loop * L,ScalarEvolution & SE,DominatorTree & DT,AssumptionCache * AC)26209467b48Spatrick bool llvm::isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
26309467b48Spatrick ScalarEvolution &SE,
264*d415bd75Srobert DominatorTree &DT,
265*d415bd75Srobert AssumptionCache *AC) {
26609467b48Spatrick auto &DL = LI->getModule()->getDataLayout();
26709467b48Spatrick Value *Ptr = LI->getPointerOperand();
26809467b48Spatrick
26909467b48Spatrick APInt EltSize(DL.getIndexTypeSizeInBits(Ptr->getType()),
270*d415bd75Srobert DL.getTypeStoreSize(LI->getType()).getFixedValue());
271097a140dSpatrick const Align Alignment = LI->getAlign();
27209467b48Spatrick
27309467b48Spatrick Instruction *HeaderFirstNonPHI = L->getHeader()->getFirstNonPHI();
27409467b48Spatrick
27509467b48Spatrick // If given a uniform (i.e. non-varying) address, see if we can prove the
27609467b48Spatrick // access is safe within the loop w/o needing predication.
27709467b48Spatrick if (L->isLoopInvariant(Ptr))
27809467b48Spatrick return isDereferenceableAndAlignedPointer(Ptr, Alignment, EltSize, DL,
279*d415bd75Srobert HeaderFirstNonPHI, AC, &DT);
28009467b48Spatrick
28109467b48Spatrick // Otherwise, check to see if we have a repeating access pattern where we can
28209467b48Spatrick // prove that all accesses are well aligned and dereferenceable.
28309467b48Spatrick auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Ptr));
28409467b48Spatrick if (!AddRec || AddRec->getLoop() != L || !AddRec->isAffine())
28509467b48Spatrick return false;
28609467b48Spatrick auto* Step = dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(SE));
28709467b48Spatrick if (!Step)
28809467b48Spatrick return false;
28909467b48Spatrick // TODO: generalize to access patterns which have gaps
29009467b48Spatrick if (Step->getAPInt() != EltSize)
29109467b48Spatrick return false;
29209467b48Spatrick
29373471bf0Spatrick auto TC = SE.getSmallConstantMaxTripCount(L);
29409467b48Spatrick if (!TC)
29509467b48Spatrick return false;
29609467b48Spatrick
29709467b48Spatrick const APInt AccessSize = TC * EltSize;
29809467b48Spatrick
29909467b48Spatrick auto *StartS = dyn_cast<SCEVUnknown>(AddRec->getStart());
30009467b48Spatrick if (!StartS)
30109467b48Spatrick return false;
30209467b48Spatrick assert(SE.isLoopInvariant(StartS, L) && "implied by addrec definition");
30309467b48Spatrick Value *Base = StartS->getValue();
30409467b48Spatrick
30509467b48Spatrick // For the moment, restrict ourselves to the case where the access size is a
30609467b48Spatrick // multiple of the requested alignment and the base is aligned.
30709467b48Spatrick // TODO: generalize if a case found which warrants
30809467b48Spatrick if (EltSize.urem(Alignment.value()) != 0)
30909467b48Spatrick return false;
31009467b48Spatrick return isDereferenceableAndAlignedPointer(Base, Alignment, AccessSize, DL,
311*d415bd75Srobert HeaderFirstNonPHI, AC, &DT);
31209467b48Spatrick }
31309467b48Spatrick
31409467b48Spatrick /// Check if executing a load of this pointer value cannot trap.
31509467b48Spatrick ///
31609467b48Spatrick /// If DT and ScanFrom are specified this method performs context-sensitive
31709467b48Spatrick /// analysis and returns true if it is safe to load immediately before ScanFrom.
31809467b48Spatrick ///
31909467b48Spatrick /// If it is not obviously safe to load from the specified pointer, we do
32009467b48Spatrick /// a quick local scan of the basic block containing \c ScanFrom, to determine
32109467b48Spatrick /// if the address is already accessed.
32209467b48Spatrick ///
32309467b48Spatrick /// This uses the pointee type to determine how many bytes need to be safe to
32409467b48Spatrick /// load from the pointer.
isSafeToLoadUnconditionally(Value * V,Align Alignment,APInt & Size,const DataLayout & DL,Instruction * ScanFrom,AssumptionCache * AC,const DominatorTree * DT,const TargetLibraryInfo * TLI)325097a140dSpatrick bool llvm::isSafeToLoadUnconditionally(Value *V, Align Alignment, APInt &Size,
32609467b48Spatrick const DataLayout &DL,
32709467b48Spatrick Instruction *ScanFrom,
328*d415bd75Srobert AssumptionCache *AC,
32973471bf0Spatrick const DominatorTree *DT,
33073471bf0Spatrick const TargetLibraryInfo *TLI) {
33109467b48Spatrick // If DT is not specified we can't make context-sensitive query
33209467b48Spatrick const Instruction* CtxI = DT ? ScanFrom : nullptr;
333*d415bd75Srobert if (isDereferenceableAndAlignedPointer(V, Alignment, Size, DL, CtxI, AC, DT,
334*d415bd75Srobert TLI))
33509467b48Spatrick return true;
33609467b48Spatrick
33709467b48Spatrick if (!ScanFrom)
33809467b48Spatrick return false;
33909467b48Spatrick
34009467b48Spatrick if (Size.getBitWidth() > 64)
34109467b48Spatrick return false;
34209467b48Spatrick const uint64_t LoadSize = Size.getZExtValue();
34309467b48Spatrick
34409467b48Spatrick // Otherwise, be a little bit aggressive by scanning the local block where we
34509467b48Spatrick // want to check to see if the pointer is already being loaded or stored
34609467b48Spatrick // from/to. If so, the previous load or store would have already trapped,
34709467b48Spatrick // so there is no harm doing an extra load (also, CSE will later eliminate
34809467b48Spatrick // the load entirely).
34909467b48Spatrick BasicBlock::iterator BBI = ScanFrom->getIterator(),
35009467b48Spatrick E = ScanFrom->getParent()->begin();
35109467b48Spatrick
35209467b48Spatrick // We can at least always strip pointer casts even though we can't use the
35309467b48Spatrick // base here.
35409467b48Spatrick V = V->stripPointerCasts();
35509467b48Spatrick
35609467b48Spatrick while (BBI != E) {
35709467b48Spatrick --BBI;
35809467b48Spatrick
35909467b48Spatrick // If we see a free or a call which may write to memory (i.e. which might do
36009467b48Spatrick // a free) the pointer could be marked invalid.
36109467b48Spatrick if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
362*d415bd75Srobert !isa<LifetimeIntrinsic>(BBI) && !isa<DbgInfoIntrinsic>(BBI))
36309467b48Spatrick return false;
36409467b48Spatrick
36509467b48Spatrick Value *AccessedPtr;
366097a140dSpatrick Type *AccessedTy;
367097a140dSpatrick Align AccessedAlign;
36809467b48Spatrick if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
36909467b48Spatrick // Ignore volatile loads. The execution of a volatile load cannot
37009467b48Spatrick // be used to prove an address is backed by regular memory; it can,
37109467b48Spatrick // for example, point to an MMIO register.
37209467b48Spatrick if (LI->isVolatile())
37309467b48Spatrick continue;
37409467b48Spatrick AccessedPtr = LI->getPointerOperand();
375097a140dSpatrick AccessedTy = LI->getType();
376097a140dSpatrick AccessedAlign = LI->getAlign();
37709467b48Spatrick } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
37809467b48Spatrick // Ignore volatile stores (see comment for loads).
37909467b48Spatrick if (SI->isVolatile())
38009467b48Spatrick continue;
38109467b48Spatrick AccessedPtr = SI->getPointerOperand();
382097a140dSpatrick AccessedTy = SI->getValueOperand()->getType();
383097a140dSpatrick AccessedAlign = SI->getAlign();
38409467b48Spatrick } else
38509467b48Spatrick continue;
38609467b48Spatrick
38709467b48Spatrick if (AccessedAlign < Alignment)
38809467b48Spatrick continue;
38909467b48Spatrick
39009467b48Spatrick // Handle trivial cases.
39109467b48Spatrick if (AccessedPtr == V &&
39209467b48Spatrick LoadSize <= DL.getTypeStoreSize(AccessedTy))
39309467b48Spatrick return true;
39409467b48Spatrick
39509467b48Spatrick if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
39609467b48Spatrick LoadSize <= DL.getTypeStoreSize(AccessedTy))
39709467b48Spatrick return true;
39809467b48Spatrick }
39909467b48Spatrick return false;
40009467b48Spatrick }
40109467b48Spatrick
isSafeToLoadUnconditionally(Value * V,Type * Ty,Align Alignment,const DataLayout & DL,Instruction * ScanFrom,AssumptionCache * AC,const DominatorTree * DT,const TargetLibraryInfo * TLI)402097a140dSpatrick bool llvm::isSafeToLoadUnconditionally(Value *V, Type *Ty, Align Alignment,
40309467b48Spatrick const DataLayout &DL,
40409467b48Spatrick Instruction *ScanFrom,
405*d415bd75Srobert AssumptionCache *AC,
40673471bf0Spatrick const DominatorTree *DT,
40773471bf0Spatrick const TargetLibraryInfo *TLI) {
408*d415bd75Srobert TypeSize TySize = DL.getTypeStoreSize(Ty);
409*d415bd75Srobert if (TySize.isScalable())
410*d415bd75Srobert return false;
411*d415bd75Srobert APInt Size(DL.getIndexTypeSizeInBits(V->getType()), TySize.getFixedValue());
412*d415bd75Srobert return isSafeToLoadUnconditionally(V, Alignment, Size, DL, ScanFrom, AC, DT,
413*d415bd75Srobert TLI);
41409467b48Spatrick }
41509467b48Spatrick
41609467b48Spatrick /// DefMaxInstsToScan - the default number of maximum instructions
41709467b48Spatrick /// to scan in the block, used by FindAvailableLoadedValue().
41809467b48Spatrick /// FindAvailableLoadedValue() was introduced in r60148, to improve jump
41909467b48Spatrick /// threading in part by eliminating partially redundant loads.
42009467b48Spatrick /// At that point, the value of MaxInstsToScan was already set to '6'
42109467b48Spatrick /// without documented explanation.
42209467b48Spatrick cl::opt<unsigned>
42309467b48Spatrick llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
42409467b48Spatrick cl::desc("Use this to specify the default maximum number of instructions "
42509467b48Spatrick "to scan backward from a given instruction, when searching for "
42609467b48Spatrick "available loaded value"));
42709467b48Spatrick
FindAvailableLoadedValue(LoadInst * Load,BasicBlock * ScanBB,BasicBlock::iterator & ScanFrom,unsigned MaxInstsToScan,AAResults * AA,bool * IsLoad,unsigned * NumScanedInst)42809467b48Spatrick Value *llvm::FindAvailableLoadedValue(LoadInst *Load,
42909467b48Spatrick BasicBlock *ScanBB,
43009467b48Spatrick BasicBlock::iterator &ScanFrom,
43109467b48Spatrick unsigned MaxInstsToScan,
432097a140dSpatrick AAResults *AA, bool *IsLoad,
43309467b48Spatrick unsigned *NumScanedInst) {
43409467b48Spatrick // Don't CSE load that is volatile or anything stronger than unordered.
43509467b48Spatrick if (!Load->isUnordered())
43609467b48Spatrick return nullptr;
43709467b48Spatrick
43873471bf0Spatrick MemoryLocation Loc = MemoryLocation::get(Load);
43973471bf0Spatrick return findAvailablePtrLoadStore(Loc, Load->getType(), Load->isAtomic(),
44073471bf0Spatrick ScanBB, ScanFrom, MaxInstsToScan, AA, IsLoad,
44173471bf0Spatrick NumScanedInst);
44209467b48Spatrick }
44309467b48Spatrick
444097a140dSpatrick // Check if the load and the store have the same base, constant offsets and
445097a140dSpatrick // non-overlapping access ranges.
areNonOverlapSameBaseLoadAndStore(const Value * LoadPtr,Type * LoadTy,const Value * StorePtr,Type * StoreTy,const DataLayout & DL)44673471bf0Spatrick static bool areNonOverlapSameBaseLoadAndStore(const Value *LoadPtr,
44773471bf0Spatrick Type *LoadTy,
44873471bf0Spatrick const Value *StorePtr,
44973471bf0Spatrick Type *StoreTy,
450097a140dSpatrick const DataLayout &DL) {
451*d415bd75Srobert APInt LoadOffset(DL.getIndexTypeSizeInBits(LoadPtr->getType()), 0);
452*d415bd75Srobert APInt StoreOffset(DL.getIndexTypeSizeInBits(StorePtr->getType()), 0);
45373471bf0Spatrick const Value *LoadBase = LoadPtr->stripAndAccumulateConstantOffsets(
454097a140dSpatrick DL, LoadOffset, /* AllowNonInbounds */ false);
45573471bf0Spatrick const Value *StoreBase = StorePtr->stripAndAccumulateConstantOffsets(
456097a140dSpatrick DL, StoreOffset, /* AllowNonInbounds */ false);
457097a140dSpatrick if (LoadBase != StoreBase)
458097a140dSpatrick return false;
459097a140dSpatrick auto LoadAccessSize = LocationSize::precise(DL.getTypeStoreSize(LoadTy));
460097a140dSpatrick auto StoreAccessSize = LocationSize::precise(DL.getTypeStoreSize(StoreTy));
461097a140dSpatrick ConstantRange LoadRange(LoadOffset,
462097a140dSpatrick LoadOffset + LoadAccessSize.toRaw());
463097a140dSpatrick ConstantRange StoreRange(StoreOffset,
464097a140dSpatrick StoreOffset + StoreAccessSize.toRaw());
465097a140dSpatrick return LoadRange.intersectWith(StoreRange).isEmptySet();
466097a140dSpatrick }
467097a140dSpatrick
getAvailableLoadStore(Instruction * Inst,const Value * Ptr,Type * AccessTy,bool AtLeastAtomic,const DataLayout & DL,bool * IsLoadCSE)46873471bf0Spatrick static Value *getAvailableLoadStore(Instruction *Inst, const Value *Ptr,
46973471bf0Spatrick Type *AccessTy, bool AtLeastAtomic,
47073471bf0Spatrick const DataLayout &DL, bool *IsLoadCSE) {
47173471bf0Spatrick // If this is a load of Ptr, the loaded value is available.
47273471bf0Spatrick // (This is true even if the load is volatile or atomic, although
47373471bf0Spatrick // those cases are unlikely.)
47473471bf0Spatrick if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
47573471bf0Spatrick // We can value forward from an atomic to a non-atomic, but not the
47673471bf0Spatrick // other way around.
47773471bf0Spatrick if (LI->isAtomic() < AtLeastAtomic)
47873471bf0Spatrick return nullptr;
47973471bf0Spatrick
48073471bf0Spatrick Value *LoadPtr = LI->getPointerOperand()->stripPointerCasts();
48173471bf0Spatrick if (!AreEquivalentAddressValues(LoadPtr, Ptr))
48273471bf0Spatrick return nullptr;
48373471bf0Spatrick
48473471bf0Spatrick if (CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
48573471bf0Spatrick if (IsLoadCSE)
48673471bf0Spatrick *IsLoadCSE = true;
48773471bf0Spatrick return LI;
48873471bf0Spatrick }
48973471bf0Spatrick }
49073471bf0Spatrick
49173471bf0Spatrick // If this is a store through Ptr, the value is available!
49273471bf0Spatrick // (This is true even if the store is volatile or atomic, although
49373471bf0Spatrick // those cases are unlikely.)
49473471bf0Spatrick if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
49573471bf0Spatrick // We can value forward from an atomic to a non-atomic, but not the
49673471bf0Spatrick // other way around.
49773471bf0Spatrick if (SI->isAtomic() < AtLeastAtomic)
49873471bf0Spatrick return nullptr;
49973471bf0Spatrick
50073471bf0Spatrick Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
50173471bf0Spatrick if (!AreEquivalentAddressValues(StorePtr, Ptr))
50273471bf0Spatrick return nullptr;
50373471bf0Spatrick
50473471bf0Spatrick if (IsLoadCSE)
50573471bf0Spatrick *IsLoadCSE = false;
50673471bf0Spatrick
50773471bf0Spatrick Value *Val = SI->getValueOperand();
50873471bf0Spatrick if (CastInst::isBitOrNoopPointerCastable(Val->getType(), AccessTy, DL))
50973471bf0Spatrick return Val;
51073471bf0Spatrick
511*d415bd75Srobert TypeSize StoreSize = DL.getTypeSizeInBits(Val->getType());
512*d415bd75Srobert TypeSize LoadSize = DL.getTypeSizeInBits(AccessTy);
513*d415bd75Srobert if (TypeSize::isKnownLE(LoadSize, StoreSize))
51473471bf0Spatrick if (auto *C = dyn_cast<Constant>(Val))
515*d415bd75Srobert return ConstantFoldLoadFromConst(C, AccessTy, DL);
516*d415bd75Srobert }
517*d415bd75Srobert
518*d415bd75Srobert if (auto *MSI = dyn_cast<MemSetInst>(Inst)) {
519*d415bd75Srobert // Don't forward from (non-atomic) memset to atomic load.
520*d415bd75Srobert if (AtLeastAtomic)
521*d415bd75Srobert return nullptr;
522*d415bd75Srobert
523*d415bd75Srobert // Only handle constant memsets.
524*d415bd75Srobert auto *Val = dyn_cast<ConstantInt>(MSI->getValue());
525*d415bd75Srobert auto *Len = dyn_cast<ConstantInt>(MSI->getLength());
526*d415bd75Srobert if (!Val || !Len)
527*d415bd75Srobert return nullptr;
528*d415bd75Srobert
529*d415bd75Srobert // TODO: Handle offsets.
530*d415bd75Srobert Value *Dst = MSI->getDest();
531*d415bd75Srobert if (!AreEquivalentAddressValues(Dst, Ptr))
532*d415bd75Srobert return nullptr;
533*d415bd75Srobert
534*d415bd75Srobert if (IsLoadCSE)
535*d415bd75Srobert *IsLoadCSE = false;
536*d415bd75Srobert
537*d415bd75Srobert TypeSize LoadTypeSize = DL.getTypeSizeInBits(AccessTy);
538*d415bd75Srobert if (LoadTypeSize.isScalable())
539*d415bd75Srobert return nullptr;
540*d415bd75Srobert
541*d415bd75Srobert // Make sure the read bytes are contained in the memset.
542*d415bd75Srobert uint64_t LoadSize = LoadTypeSize.getFixedValue();
543*d415bd75Srobert if ((Len->getValue() * 8).ult(LoadSize))
544*d415bd75Srobert return nullptr;
545*d415bd75Srobert
546*d415bd75Srobert APInt Splat = LoadSize >= 8 ? APInt::getSplat(LoadSize, Val->getValue())
547*d415bd75Srobert : Val->getValue().trunc(LoadSize);
548*d415bd75Srobert ConstantInt *SplatC = ConstantInt::get(MSI->getContext(), Splat);
549*d415bd75Srobert if (CastInst::isBitOrNoopPointerCastable(SplatC->getType(), AccessTy, DL))
550*d415bd75Srobert return SplatC;
551*d415bd75Srobert
552*d415bd75Srobert return nullptr;
55373471bf0Spatrick }
55473471bf0Spatrick
55573471bf0Spatrick return nullptr;
55673471bf0Spatrick }
55773471bf0Spatrick
findAvailablePtrLoadStore(const MemoryLocation & Loc,Type * AccessTy,bool AtLeastAtomic,BasicBlock * ScanBB,BasicBlock::iterator & ScanFrom,unsigned MaxInstsToScan,AAResults * AA,bool * IsLoadCSE,unsigned * NumScanedInst)55873471bf0Spatrick Value *llvm::findAvailablePtrLoadStore(
55973471bf0Spatrick const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic,
56073471bf0Spatrick BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan,
56173471bf0Spatrick AAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst) {
56209467b48Spatrick if (MaxInstsToScan == 0)
56309467b48Spatrick MaxInstsToScan = ~0U;
56409467b48Spatrick
56509467b48Spatrick const DataLayout &DL = ScanBB->getModule()->getDataLayout();
56673471bf0Spatrick const Value *StrippedPtr = Loc.Ptr->stripPointerCasts();
56709467b48Spatrick
56809467b48Spatrick while (ScanFrom != ScanBB->begin()) {
56909467b48Spatrick // We must ignore debug info directives when counting (otherwise they
57009467b48Spatrick // would affect codegen).
57109467b48Spatrick Instruction *Inst = &*--ScanFrom;
57273471bf0Spatrick if (Inst->isDebugOrPseudoInst())
57309467b48Spatrick continue;
57409467b48Spatrick
57509467b48Spatrick // Restore ScanFrom to expected value in case next test succeeds
57609467b48Spatrick ScanFrom++;
57709467b48Spatrick
57809467b48Spatrick if (NumScanedInst)
57909467b48Spatrick ++(*NumScanedInst);
58009467b48Spatrick
58109467b48Spatrick // Don't scan huge blocks.
58209467b48Spatrick if (MaxInstsToScan-- == 0)
58309467b48Spatrick return nullptr;
58409467b48Spatrick
58509467b48Spatrick --ScanFrom;
58609467b48Spatrick
58773471bf0Spatrick if (Value *Available = getAvailableLoadStore(Inst, StrippedPtr, AccessTy,
58873471bf0Spatrick AtLeastAtomic, DL, IsLoadCSE))
58973471bf0Spatrick return Available;
59009467b48Spatrick
59109467b48Spatrick // Try to get the store size for the type.
59209467b48Spatrick if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
59309467b48Spatrick Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
59409467b48Spatrick
59509467b48Spatrick // If both StrippedPtr and StorePtr reach all the way to an alloca or
59609467b48Spatrick // global and they are different, ignore the store. This is a trivial form
59709467b48Spatrick // of alias analysis that is important for reg2mem'd code.
59809467b48Spatrick if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
59909467b48Spatrick (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
60009467b48Spatrick StrippedPtr != StorePtr)
60109467b48Spatrick continue;
60209467b48Spatrick
603097a140dSpatrick if (!AA) {
604097a140dSpatrick // When AA isn't available, but if the load and the store have the same
605097a140dSpatrick // base, constant offsets and non-overlapping access ranges, ignore the
606097a140dSpatrick // store. This is a simple form of alias analysis that is used by the
607097a140dSpatrick // inliner. FIXME: use BasicAA if possible.
60873471bf0Spatrick if (areNonOverlapSameBaseLoadAndStore(
60973471bf0Spatrick Loc.Ptr, AccessTy, SI->getPointerOperand(),
610097a140dSpatrick SI->getValueOperand()->getType(), DL))
61109467b48Spatrick continue;
612097a140dSpatrick } else {
613097a140dSpatrick // If we have alias analysis and it says the store won't modify the
614097a140dSpatrick // loaded value, ignore the store.
61573471bf0Spatrick if (!isModSet(AA->getModRefInfo(SI, Loc)))
616097a140dSpatrick continue;
617097a140dSpatrick }
61809467b48Spatrick
61909467b48Spatrick // Otherwise the store that may or may not alias the pointer, bail out.
62009467b48Spatrick ++ScanFrom;
62109467b48Spatrick return nullptr;
62209467b48Spatrick }
62309467b48Spatrick
62409467b48Spatrick // If this is some other instruction that may clobber Ptr, bail out.
62509467b48Spatrick if (Inst->mayWriteToMemory()) {
62609467b48Spatrick // If alias analysis claims that it really won't modify the load,
62709467b48Spatrick // ignore it.
62873471bf0Spatrick if (AA && !isModSet(AA->getModRefInfo(Inst, Loc)))
62909467b48Spatrick continue;
63009467b48Spatrick
63109467b48Spatrick // May modify the pointer, bail out.
63209467b48Spatrick ++ScanFrom;
63309467b48Spatrick return nullptr;
63409467b48Spatrick }
63509467b48Spatrick }
63609467b48Spatrick
63709467b48Spatrick // Got to the start of the block, we didn't find it, but are done for this
63809467b48Spatrick // block.
63909467b48Spatrick return nullptr;
64009467b48Spatrick }
64173471bf0Spatrick
FindAvailableLoadedValue(LoadInst * Load,AAResults & AA,bool * IsLoadCSE,unsigned MaxInstsToScan)64273471bf0Spatrick Value *llvm::FindAvailableLoadedValue(LoadInst *Load, AAResults &AA,
64373471bf0Spatrick bool *IsLoadCSE,
64473471bf0Spatrick unsigned MaxInstsToScan) {
64573471bf0Spatrick const DataLayout &DL = Load->getModule()->getDataLayout();
64673471bf0Spatrick Value *StrippedPtr = Load->getPointerOperand()->stripPointerCasts();
64773471bf0Spatrick BasicBlock *ScanBB = Load->getParent();
64873471bf0Spatrick Type *AccessTy = Load->getType();
64973471bf0Spatrick bool AtLeastAtomic = Load->isAtomic();
65073471bf0Spatrick
65173471bf0Spatrick if (!Load->isUnordered())
65273471bf0Spatrick return nullptr;
65373471bf0Spatrick
65473471bf0Spatrick // Try to find an available value first, and delay expensive alias analysis
65573471bf0Spatrick // queries until later.
65673471bf0Spatrick Value *Available = nullptr;;
65773471bf0Spatrick SmallVector<Instruction *> MustNotAliasInsts;
65873471bf0Spatrick for (Instruction &Inst : make_range(++Load->getReverseIterator(),
65973471bf0Spatrick ScanBB->rend())) {
66073471bf0Spatrick if (Inst.isDebugOrPseudoInst())
66173471bf0Spatrick continue;
66273471bf0Spatrick
66373471bf0Spatrick if (MaxInstsToScan-- == 0)
66473471bf0Spatrick return nullptr;
66573471bf0Spatrick
66673471bf0Spatrick Available = getAvailableLoadStore(&Inst, StrippedPtr, AccessTy,
66773471bf0Spatrick AtLeastAtomic, DL, IsLoadCSE);
66873471bf0Spatrick if (Available)
66973471bf0Spatrick break;
67073471bf0Spatrick
67173471bf0Spatrick if (Inst.mayWriteToMemory())
67273471bf0Spatrick MustNotAliasInsts.push_back(&Inst);
67373471bf0Spatrick }
67473471bf0Spatrick
67573471bf0Spatrick // If we found an available value, ensure that the instructions in between
67673471bf0Spatrick // did not modify the memory location.
67773471bf0Spatrick if (Available) {
67873471bf0Spatrick MemoryLocation Loc = MemoryLocation::get(Load);
67973471bf0Spatrick for (Instruction *Inst : MustNotAliasInsts)
68073471bf0Spatrick if (isModSet(AA.getModRefInfo(Inst, Loc)))
68173471bf0Spatrick return nullptr;
68273471bf0Spatrick }
68373471bf0Spatrick
68473471bf0Spatrick return Available;
68573471bf0Spatrick }
68673471bf0Spatrick
canReplacePointersIfEqual(Value * A,Value * B,const DataLayout & DL,Instruction * CtxI)68773471bf0Spatrick bool llvm::canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL,
68873471bf0Spatrick Instruction *CtxI) {
68973471bf0Spatrick Type *Ty = A->getType();
69073471bf0Spatrick assert(Ty == B->getType() && Ty->isPointerTy() &&
69173471bf0Spatrick "values must have matching pointer types");
69273471bf0Spatrick
69373471bf0Spatrick // NOTE: The checks in the function are incomplete and currently miss illegal
69473471bf0Spatrick // cases! The current implementation is a starting point and the
69573471bf0Spatrick // implementation should be made stricter over time.
69673471bf0Spatrick if (auto *C = dyn_cast<Constant>(B)) {
69773471bf0Spatrick // Do not allow replacing a pointer with a constant pointer, unless it is
69873471bf0Spatrick // either null or at least one byte is dereferenceable.
69973471bf0Spatrick APInt OneByte(DL.getPointerTypeSizeInBits(Ty), 1);
70073471bf0Spatrick return C->isNullValue() ||
70173471bf0Spatrick isDereferenceableAndAlignedPointer(B, Align(1), OneByte, DL, CtxI);
70273471bf0Spatrick }
70373471bf0Spatrick
70473471bf0Spatrick return true;
70573471bf0Spatrick }
706