LLVM  16.0.0git
Loads.cpp
Go to the documentation of this file.
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"
16 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/Operator.h"
26 
27 using namespace llvm;
28 
29 static bool isAligned(const Value *Base, const APInt &Offset, Align Alignment,
30  const DataLayout &DL) {
31  Align BA = Base->getPointerAlignment(DL);
32  const APInt APAlign(Offset.getBitWidth(), Alignment.value());
33  assert(APAlign.isPowerOf2() && "must be a power of 2!");
34  return BA >= Alignment && !(Offset & (APAlign - 1));
35 }
36 
37 /// Test if V is always a pointer to allocated and suitably aligned memory for
38 /// a simple load or store.
40  const Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
41  const Instruction *CtxI, AssumptionCache *AC, const DominatorTree *DT,
43  unsigned MaxDepth) {
44  assert(V->getType()->isPointerTy() && "Base must be pointer");
45 
46  // Recursion limit.
47  if (MaxDepth-- == 0)
48  return false;
49 
50  // Already visited? Bail out, we've likely hit unreachable code.
51  if (!Visited.insert(V).second)
52  return false;
53 
54  // Note that it is not safe to speculate into a malloc'd region because
55  // malloc may return null.
56 
57  // Recurse into both hands of select.
58  if (const SelectInst *Sel = dyn_cast<SelectInst>(V)) {
59  return isDereferenceableAndAlignedPointer(Sel->getTrueValue(), Alignment,
60  Size, DL, CtxI, AC, DT, TLI,
61  Visited, MaxDepth) &&
62  isDereferenceableAndAlignedPointer(Sel->getFalseValue(), Alignment,
63  Size, DL, CtxI, AC, DT, TLI,
64  Visited, MaxDepth);
65  }
66 
67  // bitcast instructions are no-ops as far as dereferenceability is concerned.
68  if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) {
69  if (BC->getSrcTy()->isPointerTy())
70  return isDereferenceableAndAlignedPointer(BC->getOperand(0), Alignment,
71  Size, DL, CtxI, AC, DT, TLI,
72  Visited, MaxDepth);
73  }
74 
75  bool CheckForNonNull, CheckForFreed;
76  APInt KnownDerefBytes(Size.getBitWidth(),
77  V->getPointerDereferenceableBytes(DL, CheckForNonNull,
78  CheckForFreed));
79  if (KnownDerefBytes.getBoolValue() && KnownDerefBytes.uge(Size) &&
80  !CheckForFreed)
81  if (!CheckForNonNull || isKnownNonZero(V, DL, 0, AC, CtxI, DT)) {
82  // As we recursed through GEPs to get here, we've incrementally checked
83  // that each step advanced by a multiple of the alignment. If our base is
84  // properly aligned, then the original offset accessed must also be.
85  APInt Offset(DL.getTypeStoreSizeInBits(V->getType()), 0);
86  return isAligned(V, Offset, Alignment, DL);
87  }
88 
89  if (CtxI) {
90  /// Look through assumes to see if both dereferencability and alignment can
91  /// be provent by an assume
92  RetainedKnowledge AlignRK;
93  RetainedKnowledge DerefRK;
95  V, {Attribute::Dereferenceable, Attribute::Alignment}, AC,
96  [&](RetainedKnowledge RK, Instruction *Assume, auto) {
97  if (!isValidAssumeForContext(Assume, CtxI))
98  return false;
99  if (RK.AttrKind == Attribute::Alignment)
100  AlignRK = std::max(AlignRK, RK);
101  if (RK.AttrKind == Attribute::Dereferenceable)
102  DerefRK = std::max(DerefRK, RK);
103  if (AlignRK && DerefRK && AlignRK.ArgValue >= Alignment.value() &&
104  DerefRK.ArgValue >= Size.getZExtValue())
105  return true; // We have found what we needed so we stop looking
106  return false; // Other assumes may have better information. so
107  // keep looking
108  }))
109  return true;
110  }
111  /// TODO refactor this function to be able to search independently for
112  /// Dereferencability and Alignment requirements.
113 
114  // For GEPs, determine if the indexing lands within the allocated object.
115  if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
116  const Value *Base = GEP->getPointerOperand();
117 
118  APInt Offset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
119  if (!GEP->accumulateConstantOffset(DL, Offset) || Offset.isNegative() ||
120  !Offset.urem(APInt(Offset.getBitWidth(), Alignment.value()))
121  .isMinValue())
122  return false;
123 
124  // If the base pointer is dereferenceable for Offset+Size bytes, then the
125  // GEP (== Base + Offset) is dereferenceable for Size bytes. If the base
126  // pointer is aligned to Align bytes, and the Offset is divisible by Align
127  // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also
128  // aligned to Align bytes.
129 
130  // Offset and Size may have different bit widths if we have visited an
131  // addrspacecast, so we can't do arithmetic directly on the APInt values.
133  Base, Alignment, Offset + Size.sextOrTrunc(Offset.getBitWidth()), DL,
134  CtxI, AC, DT, TLI, Visited, MaxDepth);
135  }
136 
137  // For gc.relocate, look through relocations
138  if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V))
139  return isDereferenceableAndAlignedPointer(RelocateInst->getDerivedPtr(),
140  Alignment, Size, DL, CtxI, AC, DT,
141  TLI, Visited, MaxDepth);
142 
143  if (const AddrSpaceCastOperator *ASC = dyn_cast<AddrSpaceCastOperator>(V))
144  return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Alignment,
145  Size, DL, CtxI, AC, DT, TLI,
146  Visited, MaxDepth);
147 
148  if (const auto *Call = dyn_cast<CallBase>(V)) {
149  if (auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
150  return isDereferenceableAndAlignedPointer(RP, Alignment, Size, DL, CtxI,
151  AC, DT, TLI, Visited, MaxDepth);
152 
153  // If we have a call we can't recurse through, check to see if this is an
154  // allocation function for which we can establish an minimum object size.
155  // Such a minimum object size is analogous to a deref_or_null attribute in
156  // that we still need to prove the result non-null at point of use.
157  // NOTE: We can only use the object size as a base fact as we a) need to
158  // prove alignment too, and b) don't want the compile time impact of a
159  // separate recursive walk.
160  ObjectSizeOpts Opts;
161  // TODO: It may be okay to round to align, but that would imply that
162  // accessing slightly out of bounds was legal, and we're currently
163  // inconsistent about that. For the moment, be conservative.
164  Opts.RoundToAlign = false;
165  Opts.NullIsUnknownSize = true;
166  uint64_t ObjSize;
167  if (getObjectSize(V, ObjSize, DL, TLI, Opts)) {
168  APInt KnownDerefBytes(Size.getBitWidth(), ObjSize);
169  if (KnownDerefBytes.getBoolValue() && KnownDerefBytes.uge(Size) &&
170  isKnownNonZero(V, DL, 0, AC, CtxI, DT) && !V->canBeFreed()) {
171  // As we recursed through GEPs to get here, we've incrementally
172  // checked that each step advanced by a multiple of the alignment. If
173  // our base is properly aligned, then the original offset accessed
174  // must also be.
175  APInt Offset(DL.getTypeStoreSizeInBits(V->getType()), 0);
176  return isAligned(V, Offset, Alignment, DL);
177  }
178  }
179  }
180 
181  // If we don't know, assume the worst.
182  return false;
183 }
184 
186  const Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
187  const Instruction *CtxI, AssumptionCache *AC, const DominatorTree *DT,
188  const TargetLibraryInfo *TLI) {
189  // Note: At the moment, Size can be zero. This ends up being interpreted as
190  // a query of whether [Base, V] is dereferenceable and V is aligned (since
191  // that's what the implementation happened to do). It's unclear if this is
192  // the desired semantic, but at least SelectionDAG does exercise this case.
193 
195  return ::isDereferenceableAndAlignedPointer(V, Alignment, Size, DL, CtxI, AC,
196  DT, TLI, Visited, 16);
197 }
198 
200  const Value *V, Type *Ty, Align Alignment, const DataLayout &DL,
201  const Instruction *CtxI, AssumptionCache *AC, const DominatorTree *DT,
202  const TargetLibraryInfo *TLI) {
203  // For unsized types or scalable vectors we don't know exactly how many bytes
204  // are dereferenced, so bail out.
205  if (!Ty->isSized() || isa<ScalableVectorType>(Ty))
206  return false;
207 
208  // When dereferenceability information is provided by a dereferenceable
209  // attribute, we know exactly how many bytes are dereferenceable. If we can
210  // determine the exact offset to the attributed variable, we can use that
211  // information here.
212 
213  APInt AccessSize(DL.getPointerTypeSizeInBits(V->getType()),
214  DL.getTypeStoreSize(Ty));
215  return isDereferenceableAndAlignedPointer(V, Alignment, AccessSize, DL, CtxI,
216  AC, DT, TLI);
217 }
218 
220  const DataLayout &DL,
221  const Instruction *CtxI,
222  AssumptionCache *AC,
223  const DominatorTree *DT,
224  const TargetLibraryInfo *TLI) {
225  return isDereferenceableAndAlignedPointer(V, Ty, Align(1), DL, CtxI, AC, DT,
226  TLI);
227 }
228 
229 /// Test if A and B will obviously have the same value.
230 ///
231 /// This includes recognizing that %t0 and %t1 will have the same
232 /// value in code like this:
233 /// \code
234 /// %t0 = getelementptr \@a, 0, 3
235 /// store i32 0, i32* %t0
236 /// %t1 = getelementptr \@a, 0, 3
237 /// %t2 = load i32* %t1
238 /// \endcode
239 ///
240 static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
241  // Test if the values are trivially equivalent.
242  if (A == B)
243  return true;
244 
245  // Test if the values come from identical arithmetic instructions.
246  // Use isIdenticalToWhenDefined instead of isIdenticalTo because
247  // this function is only used when one address use dominates the
248  // other, which means that they'll always either have the same
249  // value or one of them will have an undefined value.
250  if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
251  isa<GetElementPtrInst>(A))
252  if (const Instruction *BI = dyn_cast<Instruction>(B))
253  if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
254  return true;
255 
256  // Otherwise they may not be equivalent.
257  return false;
258 }
259 
261  ScalarEvolution &SE,
262  DominatorTree &DT,
263  AssumptionCache *AC) {
264  auto &DL = LI->getModule()->getDataLayout();
265  Value *Ptr = LI->getPointerOperand();
266 
267  APInt EltSize(DL.getIndexTypeSizeInBits(Ptr->getType()),
268  DL.getTypeStoreSize(LI->getType()).getFixedSize());
269  const Align Alignment = LI->getAlign();
270 
271  Instruction *HeaderFirstNonPHI = L->getHeader()->getFirstNonPHI();
272 
273  // If given a uniform (i.e. non-varying) address, see if we can prove the
274  // access is safe within the loop w/o needing predication.
275  if (L->isLoopInvariant(Ptr))
276  return isDereferenceableAndAlignedPointer(Ptr, Alignment, EltSize, DL,
277  HeaderFirstNonPHI, AC, &DT);
278 
279  // Otherwise, check to see if we have a repeating access pattern where we can
280  // prove that all accesses are well aligned and dereferenceable.
281  auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Ptr));
282  if (!AddRec || AddRec->getLoop() != L || !AddRec->isAffine())
283  return false;
284  auto* Step = dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(SE));
285  if (!Step)
286  return false;
287  // TODO: generalize to access patterns which have gaps
288  if (Step->getAPInt() != EltSize)
289  return false;
290 
291  auto TC = SE.getSmallConstantMaxTripCount(L);
292  if (!TC)
293  return false;
294 
295  const APInt AccessSize = TC * EltSize;
296 
297  auto *StartS = dyn_cast<SCEVUnknown>(AddRec->getStart());
298  if (!StartS)
299  return false;
300  assert(SE.isLoopInvariant(StartS, L) && "implied by addrec definition");
301  Value *Base = StartS->getValue();
302 
303  // For the moment, restrict ourselves to the case where the access size is a
304  // multiple of the requested alignment and the base is aligned.
305  // TODO: generalize if a case found which warrants
306  if (EltSize.urem(Alignment.value()) != 0)
307  return false;
308  return isDereferenceableAndAlignedPointer(Base, Alignment, AccessSize, DL,
309  HeaderFirstNonPHI, AC, &DT);
310 }
311 
312 /// Check if executing a load of this pointer value cannot trap.
313 ///
314 /// If DT and ScanFrom are specified this method performs context-sensitive
315 /// analysis and returns true if it is safe to load immediately before ScanFrom.
316 ///
317 /// If it is not obviously safe to load from the specified pointer, we do
318 /// a quick local scan of the basic block containing \c ScanFrom, to determine
319 /// if the address is already accessed.
320 ///
321 /// This uses the pointee type to determine how many bytes need to be safe to
322 /// load from the pointer.
324  const DataLayout &DL,
325  Instruction *ScanFrom,
326  AssumptionCache *AC,
327  const DominatorTree *DT,
328  const TargetLibraryInfo *TLI) {
329  // If DT is not specified we can't make context-sensitive query
330  const Instruction* CtxI = DT ? ScanFrom : nullptr;
331  if (isDereferenceableAndAlignedPointer(V, Alignment, Size, DL, CtxI, AC, DT,
332  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 
401  const DataLayout &DL,
402  Instruction *ScanFrom,
403  AssumptionCache *AC,
404  const DominatorTree *DT,
405  const TargetLibraryInfo *TLI) {
406  TypeSize TySize = DL.getTypeStoreSize(Ty);
407  if (TySize.isScalable())
408  return false;
409  APInt Size(DL.getIndexTypeSizeInBits(V->getType()), TySize.getFixedValue());
410  return isSafeToLoadUnconditionally(V, Alignment, Size, DL, ScanFrom, AC, DT,
411  TLI);
412 }
413 
414 /// DefMaxInstsToScan - the default number of maximum instructions
415 /// to scan in the block, used by FindAvailableLoadedValue().
416 /// FindAvailableLoadedValue() was introduced in r60148, to improve jump
417 /// threading in part by eliminating partially redundant loads.
418 /// At that point, the value of MaxInstsToScan was already set to '6'
419 /// without documented explanation.
421 llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
422  cl::desc("Use this to specify the default maximum number of instructions "
423  "to scan backward from a given instruction, when searching for "
424  "available loaded value"));
425 
427  BasicBlock *ScanBB,
428  BasicBlock::iterator &ScanFrom,
429  unsigned MaxInstsToScan,
430  AAResults *AA, bool *IsLoad,
431  unsigned *NumScanedInst) {
432  // Don't CSE load that is volatile or anything stronger than unordered.
433  if (!Load->isUnordered())
434  return nullptr;
435 
437  return findAvailablePtrLoadStore(Loc, Load->getType(), Load->isAtomic(),
438  ScanBB, ScanFrom, MaxInstsToScan, AA, IsLoad,
439  NumScanedInst);
440 }
441 
442 // Check if the load and the store have the same base, constant offsets and
443 // non-overlapping access ranges.
444 static bool areNonOverlapSameBaseLoadAndStore(const Value *LoadPtr,
445  Type *LoadTy,
446  const Value *StorePtr,
447  Type *StoreTy,
448  const DataLayout &DL) {
449  APInt LoadOffset(DL.getIndexTypeSizeInBits(LoadPtr->getType()), 0);
450  APInt StoreOffset(DL.getIndexTypeSizeInBits(StorePtr->getType()), 0);
451  const Value *LoadBase = LoadPtr->stripAndAccumulateConstantOffsets(
452  DL, LoadOffset, /* AllowNonInbounds */ false);
453  const Value *StoreBase = StorePtr->stripAndAccumulateConstantOffsets(
454  DL, StoreOffset, /* AllowNonInbounds */ false);
455  if (LoadBase != StoreBase)
456  return false;
457  auto LoadAccessSize = LocationSize::precise(DL.getTypeStoreSize(LoadTy));
458  auto StoreAccessSize = LocationSize::precise(DL.getTypeStoreSize(StoreTy));
459  ConstantRange LoadRange(LoadOffset,
460  LoadOffset + LoadAccessSize.toRaw());
461  ConstantRange StoreRange(StoreOffset,
462  StoreOffset + StoreAccessSize.toRaw());
463  return LoadRange.intersectWith(StoreRange).isEmptySet();
464 }
465 
467  Type *AccessTy, bool AtLeastAtomic,
468  const DataLayout &DL, bool *IsLoadCSE) {
469  // If this is a load of Ptr, the loaded value is available.
470  // (This is true even if the load is volatile or atomic, although
471  // those cases are unlikely.)
472  if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
473  // We can value forward from an atomic to a non-atomic, but not the
474  // other way around.
475  if (LI->isAtomic() < AtLeastAtomic)
476  return nullptr;
477 
478  Value *LoadPtr = LI->getPointerOperand()->stripPointerCasts();
479  if (!AreEquivalentAddressValues(LoadPtr, Ptr))
480  return nullptr;
481 
482  if (CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
483  if (IsLoadCSE)
484  *IsLoadCSE = true;
485  return LI;
486  }
487  }
488 
489  // If this is a store through Ptr, the value is available!
490  // (This is true even if the store is volatile or atomic, although
491  // those cases are unlikely.)
492  if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
493  // We can value forward from an atomic to a non-atomic, but not the
494  // other way around.
495  if (SI->isAtomic() < AtLeastAtomic)
496  return nullptr;
497 
498  Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
499  if (!AreEquivalentAddressValues(StorePtr, Ptr))
500  return nullptr;
501 
502  if (IsLoadCSE)
503  *IsLoadCSE = false;
504 
505  Value *Val = SI->getValueOperand();
506  if (CastInst::isBitOrNoopPointerCastable(Val->getType(), AccessTy, DL))
507  return Val;
508 
509  TypeSize StoreSize = DL.getTypeSizeInBits(Val->getType());
510  TypeSize LoadSize = DL.getTypeSizeInBits(AccessTy);
511  if (TypeSize::isKnownLE(LoadSize, StoreSize))
512  if (auto *C = dyn_cast<Constant>(Val))
513  return ConstantFoldLoadFromConst(C, AccessTy, DL);
514  }
515 
516  return nullptr;
517 }
518 
520  const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic,
521  BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan,
522  AAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst) {
523  if (MaxInstsToScan == 0)
524  MaxInstsToScan = ~0U;
525 
526  const DataLayout &DL = ScanBB->getModule()->getDataLayout();
527  const Value *StrippedPtr = Loc.Ptr->stripPointerCasts();
528 
529  while (ScanFrom != ScanBB->begin()) {
530  // We must ignore debug info directives when counting (otherwise they
531  // would affect codegen).
532  Instruction *Inst = &*--ScanFrom;
533  if (Inst->isDebugOrPseudoInst())
534  continue;
535 
536  // Restore ScanFrom to expected value in case next test succeeds
537  ScanFrom++;
538 
539  if (NumScanedInst)
540  ++(*NumScanedInst);
541 
542  // Don't scan huge blocks.
543  if (MaxInstsToScan-- == 0)
544  return nullptr;
545 
546  --ScanFrom;
547 
548  if (Value *Available = getAvailableLoadStore(Inst, StrippedPtr, AccessTy,
549  AtLeastAtomic, DL, IsLoadCSE))
550  return Available;
551 
552  // Try to get the store size for the type.
553  if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
554  Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
555 
556  // If both StrippedPtr and StorePtr reach all the way to an alloca or
557  // global and they are different, ignore the store. This is a trivial form
558  // of alias analysis that is important for reg2mem'd code.
559  if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
560  (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
561  StrippedPtr != StorePtr)
562  continue;
563 
564  if (!AA) {
565  // When AA isn't available, but if the load and the store have the same
566  // base, constant offsets and non-overlapping access ranges, ignore the
567  // store. This is a simple form of alias analysis that is used by the
568  // inliner. FIXME: use BasicAA if possible.
570  Loc.Ptr, AccessTy, SI->getPointerOperand(),
571  SI->getValueOperand()->getType(), DL))
572  continue;
573  } else {
574  // If we have alias analysis and it says the store won't modify the
575  // loaded value, ignore the store.
576  if (!isModSet(AA->getModRefInfo(SI, Loc)))
577  continue;
578  }
579 
580  // Otherwise the store that may or may not alias the pointer, bail out.
581  ++ScanFrom;
582  return nullptr;
583  }
584 
585  // If this is some other instruction that may clobber Ptr, bail out.
586  if (Inst->mayWriteToMemory()) {
587  // If alias analysis claims that it really won't modify the load,
588  // ignore it.
589  if (AA && !isModSet(AA->getModRefInfo(Inst, Loc)))
590  continue;
591 
592  // May modify the pointer, bail out.
593  ++ScanFrom;
594  return nullptr;
595  }
596  }
597 
598  // Got to the start of the block, we didn't find it, but are done for this
599  // block.
600  return nullptr;
601 }
602 
604  bool *IsLoadCSE,
605  unsigned MaxInstsToScan) {
606  const DataLayout &DL = Load->getModule()->getDataLayout();
607  Value *StrippedPtr = Load->getPointerOperand()->stripPointerCasts();
608  BasicBlock *ScanBB = Load->getParent();
609  Type *AccessTy = Load->getType();
610  bool AtLeastAtomic = Load->isAtomic();
611 
612  if (!Load->isUnordered())
613  return nullptr;
614 
615  // Try to find an available value first, and delay expensive alias analysis
616  // queries until later.
617  Value *Available = nullptr;;
618  SmallVector<Instruction *> MustNotAliasInsts;
619  for (Instruction &Inst : make_range(++Load->getReverseIterator(),
620  ScanBB->rend())) {
621  if (Inst.isDebugOrPseudoInst())
622  continue;
623 
624  if (MaxInstsToScan-- == 0)
625  return nullptr;
626 
627  Available = getAvailableLoadStore(&Inst, StrippedPtr, AccessTy,
628  AtLeastAtomic, DL, IsLoadCSE);
629  if (Available)
630  break;
631 
632  if (Inst.mayWriteToMemory())
633  MustNotAliasInsts.push_back(&Inst);
634  }
635 
636  // If we found an available value, ensure that the instructions in between
637  // did not modify the memory location.
638  if (Available) {
640  for (Instruction *Inst : MustNotAliasInsts)
641  if (isModSet(AA.getModRefInfo(Inst, Loc)))
642  return nullptr;
643  }
644 
645  return Available;
646 }
647 
649  Instruction *CtxI) {
650  Type *Ty = A->getType();
651  assert(Ty == B->getType() && Ty->isPointerTy() &&
652  "values must have matching pointer types");
653 
654  // NOTE: The checks in the function are incomplete and currently miss illegal
655  // cases! The current implementation is a starting point and the
656  // implementation should be made stricter over time.
657  if (auto *C = dyn_cast<Constant>(B)) {
658  // Do not allow replacing a pointer with a constant pointer, unless it is
659  // either null or at least one byte is dereferenceable.
660  APInt OneByte(DL.getPointerTypeSizeInBits(Ty), 1);
661  return C->isNullValue() ||
662  isDereferenceableAndAlignedPointer(B, Align(1), OneByte, DL, CtxI);
663  }
664 
665  return true;
666 }
llvm::Type::isSized
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:269
llvm::Loop::isLoopInvariant
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition: LoopInfo.cpp:60
llvm::isAligned
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition: Alignment.h:146
llvm::MemoryLocation::get
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
Definition: MemoryLocation.cpp:35
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::Instruction::getModule
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:65
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:87
IntrinsicInst.h
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:223
llvm::MemoryLocation::Ptr
const Value * Ptr
The address of the start of the location.
Definition: MemoryLocation.h:218
Loads.h
llvm::Loop
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:546
llvm::APInt::isPowerOf2
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:425
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1181
llvm::LinearPolySize< TypeSize >::isKnownLE
static bool isKnownLE(const LinearPolySize &LHS, const LinearPolySize &RHS)
Definition: TypeSize.h:340
llvm::isModSet
bool isModSet(const ModRefInfo MRI)
Definition: AliasAnalysis.h:168
llvm::ScalarEvolution
The main scalar evolution driver.
Definition: ScalarEvolution.h:449
ValueTracking.h
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
llvm::canReplacePointersIfEqual
bool canReplacePointersIfEqual(Value *A, Value *B, const DataLayout &DL, Instruction *CtxI)
Returns true if a pointer value A can be replace with another pointer value \B if they are deemed equ...
Definition: Loads.cpp:648
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
llvm::isKnownNonZero
bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to be non-zero when defined.
Definition: ValueTracking.cpp:340
ScalarEvolution.h
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
MemoryBuiltins.h
llvm::ObjectSizeOpts::NullIsUnknownSize
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can't be evaluated.
Definition: MemoryBuiltins.h:162
llvm::Value::stripAndAccumulateConstantOffsets
const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr) const
Accumulate the constant offset this value has compared to a base pointer.
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
llvm::ObjectSizeOpts::RoundToAlign
bool RoundToAlign
Whether to round the result up to the alignment of allocas, byval arguments, and global variables.
Definition: MemoryBuiltins.h:158
Operator.h
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::LoadInst::getPointerOperand
Value * getPointerOperand()
Definition: Instructions.h:261
llvm::LoadInst::getAlign
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:217
llvm::BasicBlock::rend
reverse_iterator rend()
Definition: BasicBlock.h:313
llvm::LinearPolySize::isScalable
bool isScalable() const
Returns whether the size is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:298
getAvailableLoadStore
static Value * getAvailableLoadStore(Instruction *Inst, const Value *Ptr, Type *AccessTy, bool AtLeastAtomic, const DataLayout &DL, bool *IsLoadCSE)
Definition: Loads.cpp:466
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
AliasAnalysis.h
llvm::APInt::uge
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1171
llvm::isValidAssumeForContext
bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
Definition: ValueTracking.cpp:566
llvm::BitCastOperator
Definition: Operator.h:541
llvm::AAResults
Definition: AliasAnalysis.h:518
llvm::AArch64::RP
@ RP
Definition: AArch64ISelLowering.h:485
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AvailabilityState::Available
@ Available
We know the block is fully available. This is a fixpoint.
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:306
AssumeBundleQueries.h
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::Instruction
Definition: Instruction.h:42
llvm::getArgumentAliasingToReturnedPointer
const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
Definition: ValueTracking.cpp:4446
llvm::LocationSize::precise
static LocationSize precise(uint64_t Value)
Definition: MemoryLocation.h:101
llvm::BasicBlock::getModule
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:147
Align
uint64_t Align
Definition: ELFObjHandler.cpp:81
llvm::BasicBlock::getFirstNonPHI
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:209
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::Instruction::mayWriteToMemory
bool mayWriteToMemory() const
Return true if this instruction may modify memory.
Definition: Instruction.cpp:622
llvm::getObjectSize
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
Definition: MemoryBuiltins.cpp:614
LoopInfo.h
llvm::findAvailablePtrLoadStore
Value * findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan, AAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst)
Scan backwards to see if we have the value of the given pointer available locally within a small numb...
Definition: Loads.cpp:519
llvm::ScalarEvolution::getSCEV
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
Definition: ScalarEvolution.cpp:4436
llvm::cl::opt
Definition: CommandLine.h:1400
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:298
llvm::AddrSpaceCastOperator
Definition: Operator.h:556
llvm::GCRelocateInst
Represents calls to the gc.relocate intrinsic.
Definition: IntrinsicInst.h:1389
llvm::Value::getPointerDereferenceableBytes
uint64_t getPointerDereferenceableBytes(const DataLayout &DL, bool &CanBeNull, bool &CanBeFreed) const
Returns the number of bytes known to be dereferenceable for the pointer value.
Definition: Value.cpp:843
uint64_t
llvm::isSafeToLoadUnconditionally
bool isSafeToLoadUnconditionally(Value *V, Align Alignment, APInt &Size, const DataLayout &DL, Instruction *ScanFrom=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if we know that executing a load from this value cannot trap.
Definition: Loads.cpp:323
llvm::RetainedKnowledge::ArgValue
uint64_t ArgValue
Definition: AssumeBundleQueries.h:102
areNonOverlapSameBaseLoadAndStore
static bool areNonOverlapSameBaseLoadAndStore(const Value *LoadPtr, Type *LoadTy, const Value *StorePtr, Type *StoreTy, const DataLayout &DL)
Definition: Loads.cpp:444
MemoryLocation.h
llvm::ScalarEvolution::getSmallConstantMaxTripCount
unsigned getSmallConstantMaxTripCount(const Loop *L)
Returns the upper bound of the loop trip count as a normal unsigned value.
Definition: ScalarEvolution.cpp:7959
AreEquivalentAddressValues
static bool AreEquivalentAddressValues(const Value *A, const Value *B)
Test if A and B will obviously have the same value.
Definition: Loads.cpp:240
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:440
llvm::APInt::getBoolValue
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:452
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SI
StandardInstrumentations SI(Debug, VerifyEach)
Ptr
@ Ptr
Definition: TargetLibraryInfo.cpp:60
llvm::SelectInst
This class represents the LLVM 'select' instruction.
Definition: Instructions.h:1737
llvm::GEPOperator
Definition: Operator.h:375
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
llvm::ConstantFoldLoadFromConst
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
Definition: ConstantFolding.cpp:698
isDereferenceableAndAlignedPointer
static bool 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)
Test if V is always a pointer to allocated and suitably aligned memory for a simple load or store.
Definition: Loads.cpp:39
DataLayout.h
llvm::isDereferenceablePointer
bool isDereferenceablePointer(const Value *V, Type *Ty, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if this is always a dereferenceable pointer.
Definition: Loads.cpp:219
llvm::AssumptionCache
A cache of @llvm.assume calls within a function.
Definition: AssumptionCache.h:42
llvm::LinearPolySize::getFixedValue
ScalarTy getFixedValue() const
Definition: TypeSize.h:312
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
if
if(llvm_vc STREQUAL "") set(fake_version_inc "$
Definition: CMakeLists.txt:14
llvm::ilist_node_impl::getIterator
self_iterator getIterator()
Definition: ilist_node.h:82
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::CastInst::isBitOrNoopPointerCastable
static bool isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, const DataLayout &DL)
Check whether a bitcast, inttoptr, or ptrtoint cast between these types is valid and a no-op.
Definition: Instructions.cpp:3510
llvm::RetainedKnowledge
Represent one information held inside an operand bundle of an llvm.assume.
Definition: AssumeBundleQueries.h:100
llvm::ObjectSizeOpts
Various options to control the behavior of getObjectSize.
Definition: MemoryBuiltins.h:138
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:174
llvm::Value::stripPointerCasts
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:685
MaxDepth
static const unsigned MaxDepth
Definition: InstCombineMulDivRem.cpp:968
llvm::ScalarEvolution::isLoopInvariant
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
Definition: ScalarEvolution.cpp:13501
llvm::Align::value
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
llvm::TypeSize
Definition: TypeSize.h:435
llvm::FindAvailableLoadedValue
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, AAResults *AA=nullptr, bool *IsLoadCSE=nullptr, unsigned *NumScanedInst=nullptr)
Scan backwards to see if we have the value of the given load available locally within a small number ...
Definition: Loads.cpp:426
llvm::LoopBase::getHeader
BlockT * getHeader() const
Definition: LoopInfo.h:104
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:225
llvm::Instruction::isDebugOrPseudoInst
bool isDebugOrPseudoInst() const
Return true if the instruction is a DbgInfoIntrinsic or PseudoProbeInst.
Definition: Instruction.cpp:761
llvm::ConstantRange
This class represents a range of values.
Definition: ConstantRange.h:47
AA
ScalarEvolutionExpressions.h
llvm::isDereferenceableAndAlignedInLoop
bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT, AssumptionCache *AC=nullptr)
Return true if we can prove that the given load (which is assumed to be within the specified loop) wo...
Definition: Loads.cpp:260
llvm::SPII::Load
@ Load
Definition: SparcInstrInfo.h:32
llvm::Value::canBeFreed
bool canBeFreed() const
Return true if the memory object referred to by V can by freed in the scope for which the SSA value d...
Definition: Value.cpp:781
llvm::ConstantRange::intersectWith
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
Definition: ConstantRange.cpp:523
llvm::Instruction::getParent
const BasicBlock * getParent() const
Definition: Instruction.h:91
llvm::Module::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:398
llvm::SmallPtrSetImpl< const Value * >
GEP
Hexagon Common GEP
Definition: HexagonCommonGEP.cpp:171
llvm::cl::desc
Definition: CommandLine.h:413
llvm::ConstantRange::isEmptySet
bool isEmptySet() const
Return true if this set contains no members.
Definition: ConstantRange.cpp:370
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:210
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
llvm::isDereferenceableAndAlignedPointer
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition: Loads.cpp:199
llvm::DefMaxInstsToScan
cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:365
llvm::getKnowledgeForValue
RetainedKnowledge getKnowledgeForValue(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache *AC=nullptr, function_ref< bool(RetainedKnowledge, Instruction *, const CallBase::BundleOpInfo *)> Filter=[](auto...) { return true;})
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and it match...
Definition: AssumeBundleQueries.cpp:154