1 //===- Loads.cpp - Local load analysis ------------------------------------===//
2 //
3 // 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
6 //
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"
15 #include "llvm/Analysis/AssumeBundleQueries.h"
16 #include "llvm/Analysis/CaptureTracking.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/MemoryBuiltins.h"
19 #include "llvm/Analysis/MemoryLocation.h"
20 #include "llvm/Analysis/ScalarEvolution.h"
21 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
23 #include "llvm/Analysis/ValueTracking.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/GlobalAlias.h"
26 #include "llvm/IR/GlobalVariable.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/LLVMContext.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/IR/Operator.h"
31 
32 using namespace llvm;
33 
34 static bool isAligned(const Value *Base, const APInt &Offset, Align Alignment,
35                       const DataLayout &DL) {
36   Align BA = Base->getPointerAlignment(DL);
37   const APInt APAlign(Offset.getBitWidth(), Alignment.value());
38   assert(APAlign.isPowerOf2() && "must be a power of 2!");
39   return BA >= Alignment && !(Offset & (APAlign - 1));
40 }
41 
42 /// Test if V is always a pointer to allocated and suitably aligned memory for
43 /// a simple load or store.
44 static bool isDereferenceableAndAlignedPointer(
45     const Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
46     const Instruction *CtxI, const DominatorTree *DT,
47     const TargetLibraryInfo *TLI, SmallPtrSetImpl<const Value *> &Visited,
48     unsigned MaxDepth) {
49   assert(V->getType()->isPointerTy() && "Base must be pointer");
50 
51   // Recursion limit.
52   if (MaxDepth-- == 0)
53     return false;
54 
55   // Already visited?  Bail out, we've likely hit unreachable code.
56   if (!Visited.insert(V).second)
57     return false;
58 
59   // Note that it is not safe to speculate into a malloc'd region because
60   // malloc may return null.
61 
62   // bitcast instructions are no-ops as far as dereferenceability is concerned.
63   if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) {
64     if (BC->getSrcTy()->isPointerTy())
65       return isDereferenceableAndAlignedPointer(
66           BC->getOperand(0), Alignment, Size, DL, CtxI, DT, TLI,
67           Visited, MaxDepth);
68   }
69 
70   bool CheckForNonNull, CheckForFreed;
71   APInt KnownDerefBytes(Size.getBitWidth(),
72                         V->getPointerDereferenceableBytes(DL, CheckForNonNull,
73                                                           CheckForFreed));
74   if (KnownDerefBytes.getBoolValue() && KnownDerefBytes.uge(Size) &&
75       !CheckForFreed)
76     if (!CheckForNonNull || isKnownNonZero(V, DL, 0, nullptr, CtxI, DT)) {
77       // As we recursed through GEPs to get here, we've incrementally checked
78       // that each step advanced by a multiple of the alignment. If our base is
79       // properly aligned, then the original offset accessed must also be.
80       Type *Ty = V->getType();
81       assert(Ty->isSized() && "must be sized");
82       APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0);
83       return isAligned(V, Offset, Alignment, DL);
84     }
85 
86   if (CtxI) {
87     /// Look through assumes to see if both dereferencability and alignment can
88     /// be provent by an assume
89     RetainedKnowledge AlignRK;
90     RetainedKnowledge DerefRK;
91     if (getKnowledgeForValue(
92             V, {Attribute::Dereferenceable, Attribute::Alignment}, nullptr,
93             [&](RetainedKnowledge RK, Instruction *Assume, auto) {
94               if (!isValidAssumeForContext(Assume, CtxI))
95                 return false;
96               if (RK.AttrKind == Attribute::Alignment)
97                 AlignRK = std::max(AlignRK, RK);
98               if (RK.AttrKind == Attribute::Dereferenceable)
99                 DerefRK = std::max(DerefRK, RK);
100               if (AlignRK && DerefRK && AlignRK.ArgValue >= Alignment.value() &&
101                   DerefRK.ArgValue >= Size.getZExtValue())
102                 return true; // We have found what we needed so we stop looking
103               return false;  // Other assumes may have better information. so
104                              // keep looking
105             }))
106       return true;
107   }
108   /// TODO refactor this function to be able to search independently for
109   /// Dereferencability and Alignment requirements.
110 
111   // For GEPs, determine if the indexing lands within the allocated object.
112   if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
113     const Value *Base = GEP->getPointerOperand();
114 
115     APInt Offset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
116     if (!GEP->accumulateConstantOffset(DL, Offset) || Offset.isNegative() ||
117         !Offset.urem(APInt(Offset.getBitWidth(), Alignment.value()))
118              .isMinValue())
119       return false;
120 
121     // If the base pointer is dereferenceable for Offset+Size bytes, then the
122     // GEP (== Base + Offset) is dereferenceable for Size bytes.  If the base
123     // pointer is aligned to Align bytes, and the Offset is divisible by Align
124     // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also
125     // aligned to Align bytes.
126 
127     // Offset and Size may have different bit widths if we have visited an
128     // addrspacecast, so we can't do arithmetic directly on the APInt values.
129     return isDereferenceableAndAlignedPointer(
130         Base, Alignment, Offset + Size.sextOrTrunc(Offset.getBitWidth()), DL,
131         CtxI, DT, TLI, Visited, MaxDepth);
132   }
133 
134   // For gc.relocate, look through relocations
135   if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V))
136     return isDereferenceableAndAlignedPointer(RelocateInst->getDerivedPtr(),
137                                               Alignment, Size, DL, CtxI, DT,
138                                               TLI, Visited, MaxDepth);
139 
140   if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V))
141     return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Alignment,
142                                               Size, DL, CtxI, DT, TLI,
143                                               Visited, MaxDepth);
144 
145   if (const auto *Call = dyn_cast<CallBase>(V)) {
146     if (auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
147       return isDereferenceableAndAlignedPointer(RP, Alignment, Size, DL, CtxI,
148                                                 DT, TLI, Visited, MaxDepth);
149 
150     // If we have a call we can't recurse through, check to see if this is an
151     // allocation function for which we can establish an minimum object size.
152     // Such a minimum object size is analogous to a deref_or_null attribute in
153     // that we still need to prove the result non-null at point of use.
154     // NOTE: We can only use the object size as a base fact as we a) need to
155     // prove alignment too, and b) don't want the compile time impact of a
156     // separate recursive walk.
157     ObjectSizeOpts Opts;
158     // TODO: It may be okay to round to align, but that would imply that
159     // accessing slightly out of bounds was legal, and we're currently
160     // inconsistent about that.  For the moment, be conservative.
161     Opts.RoundToAlign = false;
162     Opts.NullIsUnknownSize = true;
163     uint64_t ObjSize;
164     if (getObjectSize(V, ObjSize, DL, TLI, Opts)) {
165       APInt KnownDerefBytes(Size.getBitWidth(), ObjSize);
166       if (KnownDerefBytes.getBoolValue() && KnownDerefBytes.uge(Size) &&
167           isKnownNonZero(V, DL, 0, nullptr, CtxI, DT) && !V->canBeFreed()) {
168         // As we recursed through GEPs to get here, we've incrementally
169         // checked that each step advanced by a multiple of the alignment. If
170         // our base is properly aligned, then the original offset accessed
171         // must also be.
172         Type *Ty = V->getType();
173         assert(Ty->isSized() && "must be sized");
174         APInt Offset(DL.getTypeStoreSizeInBits(Ty), 0);
175         return isAligned(V, Offset, Alignment, DL);
176       }
177     }
178   }
179 
180   // If we don't know, assume the worst.
181   return false;
182 }
183 
184 bool llvm::isDereferenceableAndAlignedPointer(const Value *V, Align Alignment,
185                                               const APInt &Size,
186                                               const DataLayout &DL,
187                                               const Instruction *CtxI,
188                                               const DominatorTree *DT,
189                                               const TargetLibraryInfo *TLI) {
190   // Note: At the moment, Size can be zero.  This ends up being interpreted as
191   // a query of whether [Base, V] is dereferenceable and V is aligned (since
192   // that's what the implementation happened to do).  It's unclear if this is
193   // the desired semantic, but at least SelectionDAG does exercise this case.
194 
195   SmallPtrSet<const Value *, 32> Visited;
196   return ::isDereferenceableAndAlignedPointer(V, Alignment, Size, DL, CtxI, DT,
197                                               TLI, Visited, 16);
198 }
199 
200 bool llvm::isDereferenceableAndAlignedPointer(const Value *V, Type *Ty,
201                                               MaybeAlign MA,
202                                               const DataLayout &DL,
203                                               const Instruction *CtxI,
204                                               const DominatorTree *DT,
205                                               const TargetLibraryInfo *TLI) {
206   // For unsized types or scalable vectors we don't know exactly how many bytes
207   // are dereferenced, so bail out.
208   if (!Ty->isSized() || isa<ScalableVectorType>(Ty))
209     return false;
210 
211   // When dereferenceability information is provided by a dereferenceable
212   // attribute, we know exactly how many bytes are dereferenceable. If we can
213   // determine the exact offset to the attributed variable, we can use that
214   // information here.
215 
216   // Require ABI alignment for loads without alignment specification
217   const Align Alignment = DL.getValueOrABITypeAlignment(MA, Ty);
218   APInt AccessSize(DL.getPointerTypeSizeInBits(V->getType()),
219                    DL.getTypeStoreSize(Ty));
220   return isDereferenceableAndAlignedPointer(V, Alignment, AccessSize, DL, CtxI,
221                                             DT, TLI);
222 }
223 
224 bool llvm::isDereferenceablePointer(const Value *V, Type *Ty,
225                                     const DataLayout &DL,
226                                     const Instruction *CtxI,
227                                     const DominatorTree *DT,
228                                     const TargetLibraryInfo *TLI) {
229   return isDereferenceableAndAlignedPointer(V, Ty, Align(1), DL, CtxI, DT, TLI);
230 }
231 
232 /// Test if A and B will obviously have the same value.
233 ///
234 /// This includes recognizing that %t0 and %t1 will have the same
235 /// value in code like this:
236 /// \code
237 ///   %t0 = getelementptr \@a, 0, 3
238 ///   store i32 0, i32* %t0
239 ///   %t1 = getelementptr \@a, 0, 3
240 ///   %t2 = load i32* %t1
241 /// \endcode
242 ///
243 static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
244   // Test if the values are trivially equivalent.
245   if (A == B)
246     return true;
247 
248   // Test if the values come from identical arithmetic instructions.
249   // Use isIdenticalToWhenDefined instead of isIdenticalTo because
250   // this function is only used when one address use dominates the
251   // other, which means that they'll always either have the same
252   // value or one of them will have an undefined value.
253   if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
254       isa<GetElementPtrInst>(A))
255     if (const Instruction *BI = dyn_cast<Instruction>(B))
256       if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
257         return true;
258 
259   // Otherwise they may not be equivalent.
260   return false;
261 }
262 
263 bool llvm::isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L,
264                                              ScalarEvolution &SE,
265                                              DominatorTree &DT) {
266   auto &DL = LI->getModule()->getDataLayout();
267   Value *Ptr = LI->getPointerOperand();
268 
269   APInt EltSize(DL.getIndexTypeSizeInBits(Ptr->getType()),
270                 DL.getTypeStoreSize(LI->getType()).getFixedSize());
271   const Align Alignment = LI->getAlign();
272 
273   Instruction *HeaderFirstNonPHI = L->getHeader()->getFirstNonPHI();
274 
275   // If given a uniform (i.e. non-varying) address, see if we can prove the
276   // access is safe within the loop w/o needing predication.
277   if (L->isLoopInvariant(Ptr))
278     return isDereferenceableAndAlignedPointer(Ptr, Alignment, EltSize, DL,
279                                               HeaderFirstNonPHI, &DT);
280 
281   // Otherwise, check to see if we have a repeating access pattern where we can
282   // prove that all accesses are well aligned and dereferenceable.
283   auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Ptr));
284   if (!AddRec || AddRec->getLoop() != L || !AddRec->isAffine())
285     return false;
286   auto* Step = dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(SE));
287   if (!Step)
288     return false;
289   // TODO: generalize to access patterns which have gaps
290   if (Step->getAPInt() != EltSize)
291     return false;
292 
293   auto TC = SE.getSmallConstantMaxTripCount(L);
294   if (!TC)
295     return false;
296 
297   const APInt AccessSize = TC * EltSize;
298 
299   auto *StartS = dyn_cast<SCEVUnknown>(AddRec->getStart());
300   if (!StartS)
301     return false;
302   assert(SE.isLoopInvariant(StartS, L) && "implied by addrec definition");
303   Value *Base = StartS->getValue();
304 
305   // For the moment, restrict ourselves to the case where the access size is a
306   // multiple of the requested alignment and the base is aligned.
307   // TODO: generalize if a case found which warrants
308   if (EltSize.urem(Alignment.value()) != 0)
309     return false;
310   return isDereferenceableAndAlignedPointer(Base, Alignment, AccessSize, DL,
311                                             HeaderFirstNonPHI, &DT);
312 }
313 
314 /// Check if executing a load of this pointer value cannot trap.
315 ///
316 /// If DT and ScanFrom are specified this method performs context-sensitive
317 /// analysis and returns true if it is safe to load immediately before ScanFrom.
318 ///
319 /// If it is not obviously safe to load from the specified pointer, we do
320 /// a quick local scan of the basic block containing \c ScanFrom, to determine
321 /// if the address is already accessed.
322 ///
323 /// This uses the pointee type to determine how many bytes need to be safe to
324 /// load from the pointer.
325 bool llvm::isSafeToLoadUnconditionally(Value *V, Align Alignment, APInt &Size,
326                                        const DataLayout &DL,
327                                        Instruction *ScanFrom,
328                                        const DominatorTree *DT,
329                                        const TargetLibraryInfo *TLI) {
330   // If DT is not specified we can't make context-sensitive query
331   const Instruction* CtxI = DT ? ScanFrom : nullptr;
332   if (isDereferenceableAndAlignedPointer(V, Alignment, Size, DL, CtxI, DT, TLI))
333     return true;
334 
335   if (!ScanFrom)
336     return false;
337 
338   if (Size.getBitWidth() > 64)
339     return false;
340   const uint64_t LoadSize = Size.getZExtValue();
341 
342   // Otherwise, be a little bit aggressive by scanning the local block where we
343   // want to check to see if the pointer is already being loaded or stored
344   // from/to.  If so, the previous load or store would have already trapped,
345   // so there is no harm doing an extra load (also, CSE will later eliminate
346   // the load entirely).
347   BasicBlock::iterator BBI = ScanFrom->getIterator(),
348                        E = ScanFrom->getParent()->begin();
349 
350   // We can at least always strip pointer casts even though we can't use the
351   // base here.
352   V = V->stripPointerCasts();
353 
354   while (BBI != E) {
355     --BBI;
356 
357     // If we see a free or a call which may write to memory (i.e. which might do
358     // a free) the pointer could be marked invalid.
359     if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
360         !isa<DbgInfoIntrinsic>(BBI))
361       return false;
362 
363     Value *AccessedPtr;
364     Type *AccessedTy;
365     Align AccessedAlign;
366     if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
367       // Ignore volatile loads. The execution of a volatile load cannot
368       // be used to prove an address is backed by regular memory; it can,
369       // for example, point to an MMIO register.
370       if (LI->isVolatile())
371         continue;
372       AccessedPtr = LI->getPointerOperand();
373       AccessedTy = LI->getType();
374       AccessedAlign = LI->getAlign();
375     } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
376       // Ignore volatile stores (see comment for loads).
377       if (SI->isVolatile())
378         continue;
379       AccessedPtr = SI->getPointerOperand();
380       AccessedTy = SI->getValueOperand()->getType();
381       AccessedAlign = SI->getAlign();
382     } else
383       continue;
384 
385     if (AccessedAlign < Alignment)
386       continue;
387 
388     // Handle trivial cases.
389     if (AccessedPtr == V &&
390         LoadSize <= DL.getTypeStoreSize(AccessedTy))
391       return true;
392 
393     if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
394         LoadSize <= DL.getTypeStoreSize(AccessedTy))
395       return true;
396   }
397   return false;
398 }
399 
400 bool llvm::isSafeToLoadUnconditionally(Value *V, Type *Ty, Align Alignment,
401                                        const DataLayout &DL,
402                                        Instruction *ScanFrom,
403                                        const DominatorTree *DT,
404                                        const TargetLibraryInfo *TLI) {
405   APInt Size(DL.getIndexTypeSizeInBits(V->getType()), DL.getTypeStoreSize(Ty));
406   return isSafeToLoadUnconditionally(V, Alignment, Size, DL, ScanFrom, DT, TLI);
407 }
408 
409   /// DefMaxInstsToScan - the default number of maximum instructions
410 /// to scan in the block, used by FindAvailableLoadedValue().
411 /// FindAvailableLoadedValue() was introduced in r60148, to improve jump
412 /// threading in part by eliminating partially redundant loads.
413 /// At that point, the value of MaxInstsToScan was already set to '6'
414 /// without documented explanation.
415 cl::opt<unsigned>
416 llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
417   cl::desc("Use this to specify the default maximum number of instructions "
418            "to scan backward from a given instruction, when searching for "
419            "available loaded value"));
420 
421 Value *llvm::FindAvailableLoadedValue(LoadInst *Load,
422                                       BasicBlock *ScanBB,
423                                       BasicBlock::iterator &ScanFrom,
424                                       unsigned MaxInstsToScan,
425                                       AAResults *AA, bool *IsLoad,
426                                       unsigned *NumScanedInst) {
427   // Don't CSE load that is volatile or anything stronger than unordered.
428   if (!Load->isUnordered())
429     return nullptr;
430 
431   MemoryLocation Loc = MemoryLocation::get(Load);
432   return findAvailablePtrLoadStore(Loc, Load->getType(), Load->isAtomic(),
433                                    ScanBB, ScanFrom, MaxInstsToScan, AA, IsLoad,
434                                    NumScanedInst);
435 }
436 
437 // Check if the load and the store have the same base, constant offsets and
438 // non-overlapping access ranges.
439 static bool areNonOverlapSameBaseLoadAndStore(const Value *LoadPtr,
440                                               Type *LoadTy,
441                                               const Value *StorePtr,
442                                               Type *StoreTy,
443                                               const DataLayout &DL) {
444   APInt LoadOffset(DL.getTypeSizeInBits(LoadPtr->getType()), 0);
445   APInt StoreOffset(DL.getTypeSizeInBits(StorePtr->getType()), 0);
446   const Value *LoadBase = LoadPtr->stripAndAccumulateConstantOffsets(
447       DL, LoadOffset, /* AllowNonInbounds */ false);
448   const Value *StoreBase = StorePtr->stripAndAccumulateConstantOffsets(
449       DL, StoreOffset, /* AllowNonInbounds */ false);
450   if (LoadBase != StoreBase)
451     return false;
452   auto LoadAccessSize = LocationSize::precise(DL.getTypeStoreSize(LoadTy));
453   auto StoreAccessSize = LocationSize::precise(DL.getTypeStoreSize(StoreTy));
454   ConstantRange LoadRange(LoadOffset,
455                           LoadOffset + LoadAccessSize.toRaw());
456   ConstantRange StoreRange(StoreOffset,
457                            StoreOffset + StoreAccessSize.toRaw());
458   return LoadRange.intersectWith(StoreRange).isEmptySet();
459 }
460 
461 static Value *getAvailableLoadStore(Instruction *Inst, const Value *Ptr,
462                                     Type *AccessTy, bool AtLeastAtomic,
463                                     const DataLayout &DL, bool *IsLoadCSE) {
464   // If this is a load of Ptr, the loaded value is available.
465   // (This is true even if the load is volatile or atomic, although
466   // those cases are unlikely.)
467   if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
468     // We can value forward from an atomic to a non-atomic, but not the
469     // other way around.
470     if (LI->isAtomic() < AtLeastAtomic)
471       return nullptr;
472 
473     Value *LoadPtr = LI->getPointerOperand()->stripPointerCasts();
474     if (!AreEquivalentAddressValues(LoadPtr, Ptr))
475       return nullptr;
476 
477     if (CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
478       if (IsLoadCSE)
479         *IsLoadCSE = true;
480       return LI;
481     }
482   }
483 
484   // If this is a store through Ptr, the value is available!
485   // (This is true even if the store is volatile or atomic, although
486   // those cases are unlikely.)
487   if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
488     // We can value forward from an atomic to a non-atomic, but not the
489     // other way around.
490     if (SI->isAtomic() < AtLeastAtomic)
491       return nullptr;
492 
493     Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
494     if (!AreEquivalentAddressValues(StorePtr, Ptr))
495       return nullptr;
496 
497     if (IsLoadCSE)
498       *IsLoadCSE = false;
499 
500     Value *Val = SI->getValueOperand();
501     if (CastInst::isBitOrNoopPointerCastable(Val->getType(), AccessTy, DL))
502       return Val;
503 
504     if (auto *C = dyn_cast<Constant>(Val))
505       return ConstantFoldLoadThroughBitcast(C, AccessTy, DL);
506   }
507 
508   return nullptr;
509 }
510 
511 Value *llvm::findAvailablePtrLoadStore(
512     const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic,
513     BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan,
514     AAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst) {
515   if (MaxInstsToScan == 0)
516     MaxInstsToScan = ~0U;
517 
518   const DataLayout &DL = ScanBB->getModule()->getDataLayout();
519   const Value *StrippedPtr = Loc.Ptr->stripPointerCasts();
520 
521   while (ScanFrom != ScanBB->begin()) {
522     // We must ignore debug info directives when counting (otherwise they
523     // would affect codegen).
524     Instruction *Inst = &*--ScanFrom;
525     if (isa<DbgInfoIntrinsic>(Inst))
526       continue;
527 
528     // Restore ScanFrom to expected value in case next test succeeds
529     ScanFrom++;
530 
531     if (NumScanedInst)
532       ++(*NumScanedInst);
533 
534     // Don't scan huge blocks.
535     if (MaxInstsToScan-- == 0)
536       return nullptr;
537 
538     --ScanFrom;
539 
540     if (Value *Available = getAvailableLoadStore(Inst, StrippedPtr, AccessTy,
541                                                  AtLeastAtomic, DL, IsLoadCSE))
542       return Available;
543 
544     // Try to get the store size for the type.
545     if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
546       Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
547 
548       // If both StrippedPtr and StorePtr reach all the way to an alloca or
549       // global and they are different, ignore the store. This is a trivial form
550       // of alias analysis that is important for reg2mem'd code.
551       if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
552           (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
553           StrippedPtr != StorePtr)
554         continue;
555 
556       if (!AA) {
557         // When AA isn't available, but if the load and the store have the same
558         // base, constant offsets and non-overlapping access ranges, ignore the
559         // store. This is a simple form of alias analysis that is used by the
560         // inliner. FIXME: use BasicAA if possible.
561         if (areNonOverlapSameBaseLoadAndStore(
562                 Loc.Ptr, AccessTy, SI->getPointerOperand(),
563                 SI->getValueOperand()->getType(), DL))
564           continue;
565       } else {
566         // If we have alias analysis and it says the store won't modify the
567         // loaded value, ignore the store.
568         if (!isModSet(AA->getModRefInfo(SI, Loc)))
569           continue;
570       }
571 
572       // Otherwise the store that may or may not alias the pointer, bail out.
573       ++ScanFrom;
574       return nullptr;
575     }
576 
577     // If this is some other instruction that may clobber Ptr, bail out.
578     if (Inst->mayWriteToMemory()) {
579       // If alias analysis claims that it really won't modify the load,
580       // ignore it.
581       if (AA && !isModSet(AA->getModRefInfo(Inst, Loc)))
582         continue;
583 
584       // May modify the pointer, bail out.
585       ++ScanFrom;
586       return nullptr;
587     }
588   }
589 
590   // Got to the start of the block, we didn't find it, but are done for this
591   // block.
592   return nullptr;
593 }
594 
595 Value *llvm::FindAvailableLoadedValue(LoadInst *Load, AAResults &AA,
596                                       bool *IsLoadCSE,
597                                       unsigned MaxInstsToScan) {
598   const DataLayout &DL = Load->getModule()->getDataLayout();
599   Value *StrippedPtr = Load->getPointerOperand()->stripPointerCasts();
600   BasicBlock *ScanBB = Load->getParent();
601   Type *AccessTy = Load->getType();
602   bool AtLeastAtomic = Load->isAtomic();
603 
604   if (!Load->isUnordered())
605     return nullptr;
606 
607   // Try to find an available value first, and delay expensive alias analysis
608   // queries until later.
609   Value *Available = nullptr;;
610   SmallVector<Instruction *> MustNotAliasInsts;
611   for (Instruction &Inst : make_range(++Load->getReverseIterator(),
612                                       ScanBB->rend())) {
613     if (isa<DbgInfoIntrinsic>(&Inst))
614       continue;
615 
616     if (MaxInstsToScan-- == 0)
617       return nullptr;
618 
619     Available = getAvailableLoadStore(&Inst, StrippedPtr, AccessTy,
620                                       AtLeastAtomic, DL, IsLoadCSE);
621     if (Available)
622       break;
623 
624     if (Inst.mayWriteToMemory())
625       MustNotAliasInsts.push_back(&Inst);
626   }
627 
628   // If we found an available value, ensure that the instructions in between
629   // did not modify the memory location.
630   if (Available) {
631     MemoryLocation Loc = MemoryLocation::get(Load);
632     for (Instruction *Inst : MustNotAliasInsts)
633       if (isModSet(AA.getModRefInfo(Inst, Loc)))
634         return nullptr;
635   }
636 
637   return Available;
638 }
639 
640 bool llvm::canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL,
641                                      Instruction *CtxI) {
642   Type *Ty = A->getType();
643   assert(Ty == B->getType() && Ty->isPointerTy() &&
644          "values must have matching pointer types");
645 
646   // NOTE: The checks in the function are incomplete and currently miss illegal
647   // cases! The current implementation is a starting point and the
648   // implementation should be made stricter over time.
649   if (auto *C = dyn_cast<Constant>(B)) {
650     // Do not allow replacing a pointer with a constant pointer, unless it is
651     // either null or at least one byte is dereferenceable.
652     APInt OneByte(DL.getPointerTypeSizeInBits(Ty), 1);
653     return C->isNullValue() ||
654            isDereferenceableAndAlignedPointer(B, Align(1), OneByte, DL, CtxI);
655   }
656 
657   return true;
658 }
659