LLVM 20.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"
22#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Operator.h"
25
26using namespace llvm;
27
28static bool isAligned(const Value *Base, const APInt &Offset, Align Alignment,
29 const DataLayout &DL) {
30 Align BA = Base->getPointerAlignment(DL);
31 return BA >= Alignment && Offset.isAligned(BA);
32}
33
34/// Test if V is always a pointer to allocated and suitably aligned memory for
35/// a simple load or store.
37 const Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
38 const Instruction *CtxI, AssumptionCache *AC, const DominatorTree *DT,
40 unsigned MaxDepth) {
41 assert(V->getType()->isPointerTy() && "Base must be pointer");
42
43 // Recursion limit.
44 if (MaxDepth-- == 0)
45 return false;
46
47 // Already visited? Bail out, we've likely hit unreachable code.
48 if (!Visited.insert(V).second)
49 return false;
50
51 // Note that it is not safe to speculate into a malloc'd region because
52 // malloc may return null.
53
54 // For GEPs, determine if the indexing lands within the allocated object.
55 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
56 const Value *Base = GEP->getPointerOperand();
57
58 APInt Offset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
59 if (!GEP->accumulateConstantOffset(DL, Offset) || Offset.isNegative() ||
60 !Offset.urem(APInt(Offset.getBitWidth(), Alignment.value()))
61 .isMinValue())
62 return false;
63
64 // If the base pointer is dereferenceable for Offset+Size bytes, then the
65 // GEP (== Base + Offset) is dereferenceable for Size bytes. If the base
66 // pointer is aligned to Align bytes, and the Offset is divisible by Align
67 // then the GEP (== Base + Offset == k_0 * Align + k_1 * Align) is also
68 // aligned to Align bytes.
69
70 // Offset and Size may have different bit widths if we have visited an
71 // addrspacecast, so we can't do arithmetic directly on the APInt values.
73 Base, Alignment, Offset + Size.sextOrTrunc(Offset.getBitWidth()), DL,
74 CtxI, AC, DT, TLI, Visited, MaxDepth);
75 }
76
77 // bitcast instructions are no-ops as far as dereferenceability is concerned.
78 if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) {
79 if (BC->getSrcTy()->isPointerTy())
81 BC->getOperand(0), Alignment, Size, DL, CtxI, AC, DT, TLI,
82 Visited, MaxDepth);
83 }
84
85 // Recurse into both hands of select.
86 if (const SelectInst *Sel = dyn_cast<SelectInst>(V)) {
87 return isDereferenceableAndAlignedPointer(Sel->getTrueValue(), Alignment,
88 Size, DL, CtxI, AC, DT, TLI,
89 Visited, MaxDepth) &&
90 isDereferenceableAndAlignedPointer(Sel->getFalseValue(), Alignment,
91 Size, DL, CtxI, AC, DT, TLI,
92 Visited, MaxDepth);
93 }
94
95 auto IsKnownDeref = [&]() {
96 bool CheckForNonNull, CheckForFreed;
97 if (!Size.ule(V->getPointerDereferenceableBytes(DL, CheckForNonNull,
98 CheckForFreed)) ||
99 CheckForFreed)
100 return false;
101 if (CheckForNonNull &&
102 !isKnownNonZero(V, SimplifyQuery(DL, DT, AC, CtxI)))
103 return false;
104 // When using something like !dereferenceable on a load, the
105 // dereferenceability may only be valid on a specific control-flow path.
106 // If the instruction doesn't dominate the context instruction, we're
107 // asking about dereferenceability under the assumption that the
108 // instruction has been speculated to the point of the context instruction,
109 // in which case we don't know if the dereferenceability info still holds.
110 // We don't bother handling allocas here, as they aren't speculatable
111 // anyway.
112 auto *I = dyn_cast<Instruction>(V);
113 if (I && !isa<AllocaInst>(I))
114 return CtxI && isValidAssumeForContext(I, CtxI, DT);
115 return true;
116 };
117 if (IsKnownDeref()) {
118 // As we recursed through GEPs to get here, we've incrementally checked
119 // that each step advanced by a multiple of the alignment. If our base is
120 // properly aligned, then the original offset accessed must also be.
121 APInt Offset(DL.getTypeStoreSizeInBits(V->getType()), 0);
122 return isAligned(V, Offset, Alignment, DL);
123 }
124
125 /// TODO refactor this function to be able to search independently for
126 /// Dereferencability and Alignment requirements.
127
128
129 if (const auto *Call = dyn_cast<CallBase>(V)) {
130 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
131 return isDereferenceableAndAlignedPointer(RP, Alignment, Size, DL, CtxI,
132 AC, DT, TLI, Visited, MaxDepth);
133
134 // If we have a call we can't recurse through, check to see if this is an
135 // allocation function for which we can establish an minimum object size.
136 // Such a minimum object size is analogous to a deref_or_null attribute in
137 // that we still need to prove the result non-null at point of use.
138 // NOTE: We can only use the object size as a base fact as we a) need to
139 // prove alignment too, and b) don't want the compile time impact of a
140 // separate recursive walk.
141 ObjectSizeOpts Opts;
142 // TODO: It may be okay to round to align, but that would imply that
143 // accessing slightly out of bounds was legal, and we're currently
144 // inconsistent about that. For the moment, be conservative.
145 Opts.RoundToAlign = false;
146 Opts.NullIsUnknownSize = true;
147 uint64_t ObjSize;
148 if (getObjectSize(V, ObjSize, DL, TLI, Opts)) {
149 APInt KnownDerefBytes(Size.getBitWidth(), ObjSize);
150 if (KnownDerefBytes.getBoolValue() && KnownDerefBytes.uge(Size) &&
151 isKnownNonZero(V, SimplifyQuery(DL, DT, AC, CtxI)) &&
152 !V->canBeFreed()) {
153 // As we recursed through GEPs to get here, we've incrementally
154 // checked that each step advanced by a multiple of the alignment. If
155 // our base is properly aligned, then the original offset accessed
156 // must also be.
157 APInt Offset(DL.getTypeStoreSizeInBits(V->getType()), 0);
158 return isAligned(V, Offset, Alignment, DL);
159 }
160 }
161 }
162
163 // For gc.relocate, look through relocations
164 if (const GCRelocateInst *RelocateInst = dyn_cast<GCRelocateInst>(V))
165 return isDereferenceableAndAlignedPointer(RelocateInst->getDerivedPtr(),
166 Alignment, Size, DL, CtxI, AC, DT,
167 TLI, Visited, MaxDepth);
168
169 if (const AddrSpaceCastOperator *ASC = dyn_cast<AddrSpaceCastOperator>(V))
170 return isDereferenceableAndAlignedPointer(ASC->getOperand(0), Alignment,
171 Size, DL, CtxI, AC, DT, TLI,
172 Visited, MaxDepth);
173
174 if (CtxI) {
175 /// Look through assumes to see if both dereferencability and alignment can
176 /// be provent by an assume
177 RetainedKnowledge AlignRK;
178 RetainedKnowledge DerefRK;
180 V, {Attribute::Dereferenceable, Attribute::Alignment}, AC,
181 [&](RetainedKnowledge RK, Instruction *Assume, auto) {
182 if (!isValidAssumeForContext(Assume, CtxI, DT))
183 return false;
184 if (RK.AttrKind == Attribute::Alignment)
185 AlignRK = std::max(AlignRK, RK);
186 if (RK.AttrKind == Attribute::Dereferenceable)
187 DerefRK = std::max(DerefRK, RK);
188 if (AlignRK && DerefRK && AlignRK.ArgValue >= Alignment.value() &&
189 DerefRK.ArgValue >= Size.getZExtValue())
190 return true; // We have found what we needed so we stop looking
191 return false; // Other assumes may have better information. so
192 // keep looking
193 }))
194 return true;
195 }
196
197 // If we don't know, assume the worst.
198 return false;
199}
200
202 const Value *V, Align Alignment, const APInt &Size, const DataLayout &DL,
203 const Instruction *CtxI, AssumptionCache *AC, const DominatorTree *DT,
204 const TargetLibraryInfo *TLI) {
205 // Note: At the moment, Size can be zero. This ends up being interpreted as
206 // a query of whether [Base, V] is dereferenceable and V is aligned (since
207 // that's what the implementation happened to do). It's unclear if this is
208 // the desired semantic, but at least SelectionDAG does exercise this case.
209
211 return ::isDereferenceableAndAlignedPointer(V, Alignment, Size, DL, CtxI, AC,
212 DT, TLI, Visited, 16);
213}
214
216 const Value *V, Type *Ty, Align Alignment, const DataLayout &DL,
217 const Instruction *CtxI, AssumptionCache *AC, const DominatorTree *DT,
218 const TargetLibraryInfo *TLI) {
219 // For unsized types or scalable vectors we don't know exactly how many bytes
220 // are dereferenced, so bail out.
221 if (!Ty->isSized() || Ty->isScalableTy())
222 return false;
223
224 // When dereferenceability information is provided by a dereferenceable
225 // attribute, we know exactly how many bytes are dereferenceable. If we can
226 // determine the exact offset to the attributed variable, we can use that
227 // information here.
228
229 APInt AccessSize(DL.getPointerTypeSizeInBits(V->getType()),
230 DL.getTypeStoreSize(Ty));
231 return isDereferenceableAndAlignedPointer(V, Alignment, AccessSize, DL, CtxI,
232 AC, DT, TLI);
233}
234
236 const DataLayout &DL,
237 const Instruction *CtxI,
238 AssumptionCache *AC,
239 const DominatorTree *DT,
240 const TargetLibraryInfo *TLI) {
241 return isDereferenceableAndAlignedPointer(V, Ty, Align(1), DL, CtxI, AC, DT,
242 TLI);
243}
244
245/// Test if A and B will obviously have the same value.
246///
247/// This includes recognizing that %t0 and %t1 will have the same
248/// value in code like this:
249/// \code
250/// %t0 = getelementptr \@a, 0, 3
251/// store i32 0, i32* %t0
252/// %t1 = getelementptr \@a, 0, 3
253/// %t2 = load i32* %t1
254/// \endcode
255///
256static bool AreEquivalentAddressValues(const Value *A, const Value *B) {
257 // Test if the values are trivially equivalent.
258 if (A == B)
259 return true;
260
261 // Test if the values come from identical arithmetic instructions.
262 // Use isIdenticalToWhenDefined instead of isIdenticalTo because
263 // this function is only used when one address use dominates the
264 // other, which means that they'll always either have the same
265 // value or one of them will have an undefined value.
266 if (isa<BinaryOperator>(A) || isa<CastInst>(A) || isa<PHINode>(A) ||
267 isa<GetElementPtrInst>(A))
268 if (const Instruction *BI = dyn_cast<Instruction>(B))
269 if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI))
270 return true;
271
272 // Otherwise they may not be equivalent.
273 return false;
274}
275
277 LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT,
279 auto &DL = LI->getDataLayout();
280 Value *Ptr = LI->getPointerOperand();
281
282 APInt EltSize(DL.getIndexTypeSizeInBits(Ptr->getType()),
283 DL.getTypeStoreSize(LI->getType()).getFixedValue());
284 const Align Alignment = LI->getAlign();
285
286 Instruction *HeaderFirstNonPHI = L->getHeader()->getFirstNonPHI();
287
288 // If given a uniform (i.e. non-varying) address, see if we can prove the
289 // access is safe within the loop w/o needing predication.
290 if (L->isLoopInvariant(Ptr))
291 return isDereferenceableAndAlignedPointer(Ptr, Alignment, EltSize, DL,
292 HeaderFirstNonPHI, AC, &DT);
293
294 // Otherwise, check to see if we have a repeating access pattern where we can
295 // prove that all accesses are well aligned and dereferenceable.
296 auto *AddRec = dyn_cast<SCEVAddRecExpr>(SE.getSCEV(Ptr));
297 if (!AddRec || AddRec->getLoop() != L || !AddRec->isAffine())
298 return false;
299 auto* Step = dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(SE));
300 if (!Step)
301 return false;
302
303 auto TC = SE.getSmallConstantMaxTripCount(L, Predicates);
304 if (!TC)
305 return false;
306
307 // TODO: Handle overlapping accesses.
308 // We should be computing AccessSize as (TC - 1) * Step + EltSize.
309 if (EltSize.sgt(Step->getAPInt()))
310 return false;
311
312 // Compute the total access size for access patterns with unit stride and
313 // patterns with gaps. For patterns with unit stride, Step and EltSize are the
314 // same.
315 // For patterns with gaps (i.e. non unit stride), we are
316 // accessing EltSize bytes at every Step.
317 APInt AccessSize = TC * Step->getAPInt();
318
319 assert(SE.isLoopInvariant(AddRec->getStart(), L) &&
320 "implied by addrec definition");
321 Value *Base = nullptr;
322 if (auto *StartS = dyn_cast<SCEVUnknown>(AddRec->getStart())) {
323 Base = StartS->getValue();
324 } else if (auto *StartS = dyn_cast<SCEVAddExpr>(AddRec->getStart())) {
325 // Handle (NewBase + offset) as start value.
326 const auto *Offset = dyn_cast<SCEVConstant>(StartS->getOperand(0));
327 const auto *NewBase = dyn_cast<SCEVUnknown>(StartS->getOperand(1));
328 if (StartS->getNumOperands() == 2 && Offset && NewBase) {
329 // The following code below assumes the offset is unsigned, but GEP
330 // offsets are treated as signed so we can end up with a signed value
331 // here too. For example, suppose the initial PHI value is (i8 255),
332 // the offset will be treated as (i8 -1) and sign-extended to (i64 -1).
333 if (Offset->getAPInt().isNegative())
334 return false;
335
336 // For the moment, restrict ourselves to the case where the offset is a
337 // multiple of the requested alignment and the base is aligned.
338 // TODO: generalize if a case found which warrants
339 if (Offset->getAPInt().urem(Alignment.value()) != 0)
340 return false;
341 Base = NewBase->getValue();
342 bool Overflow = false;
343 AccessSize = AccessSize.uadd_ov(Offset->getAPInt(), Overflow);
344 if (Overflow)
345 return false;
346 }
347 }
348
349 if (!Base)
350 return false;
351
352 // For the moment, restrict ourselves to the case where the access size is a
353 // multiple of the requested alignment and the base is aligned.
354 // TODO: generalize if a case found which warrants
355 if (EltSize.urem(Alignment.value()) != 0)
356 return false;
357 return isDereferenceableAndAlignedPointer(Base, Alignment, AccessSize, DL,
358 HeaderFirstNonPHI, AC, &DT);
359}
360
362 const Function &F = *CtxI.getFunction();
363 // Speculative load may create a race that did not exist in the source.
364 return F.hasFnAttribute(Attribute::SanitizeThread) ||
365 // Speculative load may load data from dirty regions.
366 F.hasFnAttribute(Attribute::SanitizeAddress) ||
367 F.hasFnAttribute(Attribute::SanitizeHWAddress);
368}
369
372}
373
374/// Check if executing a load of this pointer value cannot trap.
375///
376/// If DT and ScanFrom are specified this method performs context-sensitive
377/// analysis and returns true if it is safe to load immediately before ScanFrom.
378///
379/// If it is not obviously safe to load from the specified pointer, we do
380/// a quick local scan of the basic block containing \c ScanFrom, to determine
381/// if the address is already accessed.
382///
383/// This uses the pointee type to determine how many bytes need to be safe to
384/// load from the pointer.
386 const DataLayout &DL,
387 Instruction *ScanFrom,
388 AssumptionCache *AC,
389 const DominatorTree *DT,
390 const TargetLibraryInfo *TLI) {
391 // If DT is not specified we can't make context-sensitive query
392 const Instruction* CtxI = DT ? ScanFrom : nullptr;
393 if (isDereferenceableAndAlignedPointer(V, Alignment, Size, DL, CtxI, AC, DT,
394 TLI)) {
395 // With sanitizers `Dereferenceable` is not always enough for unconditional
396 // load.
397 if (!ScanFrom || !suppressSpeculativeLoadForSanitizers(*ScanFrom))
398 return true;
399 }
400
401 if (!ScanFrom)
402 return false;
403
404 if (Size.getBitWidth() > 64)
405 return false;
406 const TypeSize LoadSize = TypeSize::getFixed(Size.getZExtValue());
407
408 // Otherwise, be a little bit aggressive by scanning the local block where we
409 // want to check to see if the pointer is already being loaded or stored
410 // from/to. If so, the previous load or store would have already trapped,
411 // so there is no harm doing an extra load (also, CSE will later eliminate
412 // the load entirely).
413 BasicBlock::iterator BBI = ScanFrom->getIterator(),
414 E = ScanFrom->getParent()->begin();
415
416 // We can at least always strip pointer casts even though we can't use the
417 // base here.
418 V = V->stripPointerCasts();
419
420 while (BBI != E) {
421 --BBI;
422
423 // If we see a free or a call which may write to memory (i.e. which might do
424 // a free) the pointer could be marked invalid.
425 if (isa<CallInst>(BBI) && BBI->mayWriteToMemory() &&
426 !isa<LifetimeIntrinsic>(BBI) && !isa<DbgInfoIntrinsic>(BBI))
427 return false;
428
429 Value *AccessedPtr;
430 Type *AccessedTy;
431 Align AccessedAlign;
432 if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
433 // Ignore volatile loads. The execution of a volatile load cannot
434 // be used to prove an address is backed by regular memory; it can,
435 // for example, point to an MMIO register.
436 if (LI->isVolatile())
437 continue;
438 AccessedPtr = LI->getPointerOperand();
439 AccessedTy = LI->getType();
440 AccessedAlign = LI->getAlign();
441 } else if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
442 // Ignore volatile stores (see comment for loads).
443 if (SI->isVolatile())
444 continue;
445 AccessedPtr = SI->getPointerOperand();
446 AccessedTy = SI->getValueOperand()->getType();
447 AccessedAlign = SI->getAlign();
448 } else
449 continue;
450
451 if (AccessedAlign < Alignment)
452 continue;
453
454 // Handle trivial cases.
455 if (AccessedPtr == V &&
456 TypeSize::isKnownLE(LoadSize, DL.getTypeStoreSize(AccessedTy)))
457 return true;
458
459 if (AreEquivalentAddressValues(AccessedPtr->stripPointerCasts(), V) &&
460 TypeSize::isKnownLE(LoadSize, DL.getTypeStoreSize(AccessedTy)))
461 return true;
462 }
463 return false;
464}
465
467 const DataLayout &DL,
468 Instruction *ScanFrom,
469 AssumptionCache *AC,
470 const DominatorTree *DT,
471 const TargetLibraryInfo *TLI) {
472 TypeSize TySize = DL.getTypeStoreSize(Ty);
473 if (TySize.isScalable())
474 return false;
475 APInt Size(DL.getIndexTypeSizeInBits(V->getType()), TySize.getFixedValue());
476 return isSafeToLoadUnconditionally(V, Alignment, Size, DL, ScanFrom, AC, DT,
477 TLI);
478}
479
480/// DefMaxInstsToScan - the default number of maximum instructions
481/// to scan in the block, used by FindAvailableLoadedValue().
482/// FindAvailableLoadedValue() was introduced in r60148, to improve jump
483/// threading in part by eliminating partially redundant loads.
484/// At that point, the value of MaxInstsToScan was already set to '6'
485/// without documented explanation.
487llvm::DefMaxInstsToScan("available-load-scan-limit", cl::init(6), cl::Hidden,
488 cl::desc("Use this to specify the default maximum number of instructions "
489 "to scan backward from a given instruction, when searching for "
490 "available loaded value"));
491
493 BasicBlock::iterator &ScanFrom,
494 unsigned MaxInstsToScan,
495 BatchAAResults *AA, bool *IsLoad,
496 unsigned *NumScanedInst) {
497 // Don't CSE load that is volatile or anything stronger than unordered.
498 if (!Load->isUnordered())
499 return nullptr;
500
502 return findAvailablePtrLoadStore(Loc, Load->getType(), Load->isAtomic(),
503 ScanBB, ScanFrom, MaxInstsToScan, AA, IsLoad,
504 NumScanedInst);
505}
506
507// Check if the load and the store have the same base, constant offsets and
508// non-overlapping access ranges.
509static bool areNonOverlapSameBaseLoadAndStore(const Value *LoadPtr,
510 Type *LoadTy,
511 const Value *StorePtr,
512 Type *StoreTy,
513 const DataLayout &DL) {
514 APInt LoadOffset(DL.getIndexTypeSizeInBits(LoadPtr->getType()), 0);
515 APInt StoreOffset(DL.getIndexTypeSizeInBits(StorePtr->getType()), 0);
516 const Value *LoadBase = LoadPtr->stripAndAccumulateConstantOffsets(
517 DL, LoadOffset, /* AllowNonInbounds */ false);
518 const Value *StoreBase = StorePtr->stripAndAccumulateConstantOffsets(
519 DL, StoreOffset, /* AllowNonInbounds */ false);
520 if (LoadBase != StoreBase)
521 return false;
522 auto LoadAccessSize = LocationSize::precise(DL.getTypeStoreSize(LoadTy));
523 auto StoreAccessSize = LocationSize::precise(DL.getTypeStoreSize(StoreTy));
524 ConstantRange LoadRange(LoadOffset,
525 LoadOffset + LoadAccessSize.toRaw());
526 ConstantRange StoreRange(StoreOffset,
527 StoreOffset + StoreAccessSize.toRaw());
528 return LoadRange.intersectWith(StoreRange).isEmptySet();
529}
530
532 Type *AccessTy, bool AtLeastAtomic,
533 const DataLayout &DL, bool *IsLoadCSE) {
534 // If this is a load of Ptr, the loaded value is available.
535 // (This is true even if the load is volatile or atomic, although
536 // those cases are unlikely.)
537 if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
538 // We can value forward from an atomic to a non-atomic, but not the
539 // other way around.
540 if (LI->isAtomic() < AtLeastAtomic)
541 return nullptr;
542
543 Value *LoadPtr = LI->getPointerOperand()->stripPointerCasts();
544 if (!AreEquivalentAddressValues(LoadPtr, Ptr))
545 return nullptr;
546
547 if (CastInst::isBitOrNoopPointerCastable(LI->getType(), AccessTy, DL)) {
548 if (IsLoadCSE)
549 *IsLoadCSE = true;
550 return LI;
551 }
552 }
553
554 // If this is a store through Ptr, the value is available!
555 // (This is true even if the store is volatile or atomic, although
556 // those cases are unlikely.)
557 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
558 // We can value forward from an atomic to a non-atomic, but not the
559 // other way around.
560 if (SI->isAtomic() < AtLeastAtomic)
561 return nullptr;
562
563 Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
564 if (!AreEquivalentAddressValues(StorePtr, Ptr))
565 return nullptr;
566
567 if (IsLoadCSE)
568 *IsLoadCSE = false;
569
570 Value *Val = SI->getValueOperand();
571 if (CastInst::isBitOrNoopPointerCastable(Val->getType(), AccessTy, DL))
572 return Val;
573
574 TypeSize StoreSize = DL.getTypeSizeInBits(Val->getType());
575 TypeSize LoadSize = DL.getTypeSizeInBits(AccessTy);
576 if (TypeSize::isKnownLE(LoadSize, StoreSize))
577 if (auto *C = dyn_cast<Constant>(Val))
578 return ConstantFoldLoadFromConst(C, AccessTy, DL);
579 }
580
581 if (auto *MSI = dyn_cast<MemSetInst>(Inst)) {
582 // Don't forward from (non-atomic) memset to atomic load.
583 if (AtLeastAtomic)
584 return nullptr;
585
586 // Only handle constant memsets.
587 auto *Val = dyn_cast<ConstantInt>(MSI->getValue());
588 auto *Len = dyn_cast<ConstantInt>(MSI->getLength());
589 if (!Val || !Len)
590 return nullptr;
591
592 // TODO: Handle offsets.
593 Value *Dst = MSI->getDest();
595 return nullptr;
596
597 if (IsLoadCSE)
598 *IsLoadCSE = false;
599
600 TypeSize LoadTypeSize = DL.getTypeSizeInBits(AccessTy);
601 if (LoadTypeSize.isScalable())
602 return nullptr;
603
604 // Make sure the read bytes are contained in the memset.
605 uint64_t LoadSize = LoadTypeSize.getFixedValue();
606 if ((Len->getValue() * 8).ult(LoadSize))
607 return nullptr;
608
609 APInt Splat = LoadSize >= 8 ? APInt::getSplat(LoadSize, Val->getValue())
610 : Val->getValue().trunc(LoadSize);
611 ConstantInt *SplatC = ConstantInt::get(MSI->getContext(), Splat);
612 if (CastInst::isBitOrNoopPointerCastable(SplatC->getType(), AccessTy, DL))
613 return SplatC;
614
615 return nullptr;
616 }
617
618 return nullptr;
619}
620
622 const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic,
623 BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan,
624 BatchAAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst) {
625 if (MaxInstsToScan == 0)
626 MaxInstsToScan = ~0U;
627
628 const DataLayout &DL = ScanBB->getDataLayout();
629 const Value *StrippedPtr = Loc.Ptr->stripPointerCasts();
630
631 while (ScanFrom != ScanBB->begin()) {
632 // We must ignore debug info directives when counting (otherwise they
633 // would affect codegen).
634 Instruction *Inst = &*--ScanFrom;
635 if (Inst->isDebugOrPseudoInst())
636 continue;
637
638 // Restore ScanFrom to expected value in case next test succeeds
639 ScanFrom++;
640
641 if (NumScanedInst)
642 ++(*NumScanedInst);
643
644 // Don't scan huge blocks.
645 if (MaxInstsToScan-- == 0)
646 return nullptr;
647
648 --ScanFrom;
649
650 if (Value *Available = getAvailableLoadStore(Inst, StrippedPtr, AccessTy,
651 AtLeastAtomic, DL, IsLoadCSE))
652 return Available;
653
654 // Try to get the store size for the type.
655 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
656 Value *StorePtr = SI->getPointerOperand()->stripPointerCasts();
657
658 // If both StrippedPtr and StorePtr reach all the way to an alloca or
659 // global and they are different, ignore the store. This is a trivial form
660 // of alias analysis that is important for reg2mem'd code.
661 if ((isa<AllocaInst>(StrippedPtr) || isa<GlobalVariable>(StrippedPtr)) &&
662 (isa<AllocaInst>(StorePtr) || isa<GlobalVariable>(StorePtr)) &&
663 StrippedPtr != StorePtr)
664 continue;
665
666 if (!AA) {
667 // When AA isn't available, but if the load and the store have the same
668 // base, constant offsets and non-overlapping access ranges, ignore the
669 // store. This is a simple form of alias analysis that is used by the
670 // inliner. FIXME: use BasicAA if possible.
672 Loc.Ptr, AccessTy, SI->getPointerOperand(),
673 SI->getValueOperand()->getType(), DL))
674 continue;
675 } else {
676 // If we have alias analysis and it says the store won't modify the
677 // loaded value, ignore the store.
678 if (!isModSet(AA->getModRefInfo(SI, Loc)))
679 continue;
680 }
681
682 // Otherwise the store that may or may not alias the pointer, bail out.
683 ++ScanFrom;
684 return nullptr;
685 }
686
687 // If this is some other instruction that may clobber Ptr, bail out.
688 if (Inst->mayWriteToMemory()) {
689 // If alias analysis claims that it really won't modify the load,
690 // ignore it.
691 if (AA && !isModSet(AA->getModRefInfo(Inst, Loc)))
692 continue;
693
694 // May modify the pointer, bail out.
695 ++ScanFrom;
696 return nullptr;
697 }
698 }
699
700 // Got to the start of the block, we didn't find it, but are done for this
701 // block.
702 return nullptr;
703}
704
706 bool *IsLoadCSE,
707 unsigned MaxInstsToScan) {
708 const DataLayout &DL = Load->getDataLayout();
709 Value *StrippedPtr = Load->getPointerOperand()->stripPointerCasts();
710 BasicBlock *ScanBB = Load->getParent();
711 Type *AccessTy = Load->getType();
712 bool AtLeastAtomic = Load->isAtomic();
713
714 if (!Load->isUnordered())
715 return nullptr;
716
717 // Try to find an available value first, and delay expensive alias analysis
718 // queries until later.
719 Value *Available = nullptr;
720 SmallVector<Instruction *> MustNotAliasInsts;
721 for (Instruction &Inst : make_range(++Load->getReverseIterator(),
722 ScanBB->rend())) {
723 if (Inst.isDebugOrPseudoInst())
724 continue;
725
726 if (MaxInstsToScan-- == 0)
727 return nullptr;
728
729 Available = getAvailableLoadStore(&Inst, StrippedPtr, AccessTy,
730 AtLeastAtomic, DL, IsLoadCSE);
731 if (Available)
732 break;
733
734 if (Inst.mayWriteToMemory())
735 MustNotAliasInsts.push_back(&Inst);
736 }
737
738 // If we found an available value, ensure that the instructions in between
739 // did not modify the memory location.
740 if (Available) {
742 for (Instruction *Inst : MustNotAliasInsts)
743 if (isModSet(AA.getModRefInfo(Inst, Loc)))
744 return nullptr;
745 }
746
747 return Available;
748}
749
750// Returns true if a use is either in an ICmp/PtrToInt or a Phi/Select that only
751// feeds into them.
752static bool isPointerUseReplacable(const Use &U) {
753 unsigned Limit = 40;
754 SmallVector<const User *> Worklist({U.getUser()});
756
757 while (!Worklist.empty() && --Limit) {
758 auto *User = Worklist.pop_back_val();
759 if (!Visited.insert(User).second)
760 continue;
761 if (isa<ICmpInst, PtrToIntInst>(User))
762 continue;
763 if (isa<PHINode, SelectInst>(User))
764 Worklist.append(User->user_begin(), User->user_end());
765 else
766 return false;
767 }
768
769 return Limit != 0;
770}
771
772// Returns true if `To` is a null pointer, constant dereferenceable pointer or
773// both pointers have the same underlying objects.
774static bool isPointerAlwaysReplaceable(const Value *From, const Value *To,
775 const DataLayout &DL) {
776 // This is not strictly correct, but we do it for now to retain important
777 // optimizations.
778 if (isa<ConstantPointerNull>(To))
779 return true;
780 if (isa<Constant>(To) &&
782 return true;
785}
786
788 const DataLayout &DL) {
789 assert(U->getType() == To->getType() && "values must have matching types");
790 // Not a pointer, just return true.
791 if (!To->getType()->isPointerTy())
792 return true;
793
794 if (isPointerAlwaysReplaceable(&*U, To, DL))
795 return true;
796 return isPointerUseReplacable(U);
797}
798
800 const DataLayout &DL) {
801 assert(From->getType() == To->getType() && "values must have matching types");
802 // Not a pointer, just return true.
803 if (!From->getType()->isPointerTy())
804 return true;
805
807}
808
812 for (BasicBlock *BB : L->blocks()) {
813 for (Instruction &I : *BB) {
814 if (auto *LI = dyn_cast<LoadInst>(&I)) {
815 if (!isDereferenceableAndAlignedInLoop(LI, L, *SE, *DT, AC, Predicates))
816 return false;
817 } else if (I.mayReadFromMemory() || I.mayWriteToMemory() || I.mayThrow())
818 return false;
819 }
820 }
821 return true;
822}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
uint64_t Size
@ Available
We know the block is fully available. This is a fixpoint.
Hexagon Common GEP
static const unsigned MaxDepth
static bool AreEquivalentAddressValues(const Value *A, const Value *B)
Test if A and B will obviously have the same value.
Definition: Loads.cpp:256
static bool isAligned(const Value *Base, const APInt &Offset, Align Alignment, const DataLayout &DL)
Definition: Loads.cpp:28
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:36
static bool isPointerAlwaysReplaceable(const Value *From, const Value *To, const DataLayout &DL)
Definition: Loads.cpp:774
static bool areNonOverlapSameBaseLoadAndStore(const Value *LoadPtr, Type *LoadTy, const Value *StorePtr, Type *StoreTy, const DataLayout &DL)
Definition: Loads.cpp:509
static bool isPointerUseReplacable(const Use &U)
Definition: Loads.cpp:752
static Value * getAvailableLoadStore(Instruction *Inst, const Value *Ptr, Type *AccessTy, bool AtLeastAtomic, const DataLayout &DL, bool *IsLoadCSE)
Definition: Loads.cpp:531
static bool suppressSpeculativeLoadForSanitizers(const Instruction &CtxI)
Definition: Loads.cpp:361
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file provides utility analysis objects describing memory locations.
if(PassOpts->AAPipeline)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
Definition: APInt.h:78
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1201
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1640
APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1909
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:624
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:471
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:448
const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
Definition: BasicBlock.cpp:296
reverse_iterator rend()
Definition: BasicBlock.h:466
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
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.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
This class represents a range of values.
Definition: ConstantRange.h:47
bool isEmptySet() const
Return true if this set contains no members.
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
Represents calls to the gc.relocate intrinsic.
bool isDebugOrPseudoInst() const LLVM_READONLY
Return true if the instruction is a DbgInfoIntrinsic or PseudoProbeInst.
bool mayWriteToMemory() const LLVM_READONLY
Return true if this instruction may modify memory.
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:70
const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Definition: Instruction.cpp:74
An instruction for reading from memory.
Definition: Instructions.h:176
Value * getPointerOperand()
Definition: Instructions.h:255
bool isUnordered() const
Definition: Instructions.h:249
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:211
static LocationSize precise(uint64_t Value)
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:39
Representation for a specific memory location.
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
const Value * Ptr
The address of the start of the location.
The main scalar evolution driver.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
unsigned getSmallConstantMaxTripCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Returns the upper bound of the loop trip count as a normal unsigned value.
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
This class represents the LLVM 'select' instruction.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
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:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:805
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
An instruction for storing to memory.
Definition: Instructions.h:292
Provides information about what library functions are available for the current target.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:345
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:264
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:310
bool isScalableTy(SmallPtrSetImpl< const Type * > &Visited) const
Return true if this is a type whose size is a known multiple of vscale.
static IntegerType * getInt8Ty(LLVMContext &C)
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
user_iterator user_begin()
Definition: Value.h:397
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.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:694
user_iterator user_end()
Definition: Value.h:405
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1075
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
const ParentTy * getParent() const
Definition: ilist_node.h:32
self_iterator getIterator()
Definition: ilist_node.h:132
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
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:215
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Value * findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan, BatchAAResults *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:621
bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition: Loads.cpp:370
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, BatchAAResults *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:492
bool isDereferenceableReadOnlyLoop(Loop *L, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Return true if the loop L cannot fault on any iteration and only contains read-only memory accesses.
Definition: Loads.cpp:809
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...
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.
bool canReplacePointersInUseIfEqual(const Use &U, const Value *To, const DataLayout &DL)
Definition: Loads.cpp:787
bool canReplacePointersIfEqual(const Value *From, const Value *To, const DataLayout &DL)
Returns true if a pointer value From can be replaced with another pointer value \To if they are deeme...
Definition: Loads.cpp:799
bool isModSet(const ModRefInfo MRI)
Definition: ModRef.h:48
bool isSafeToLoadUnconditionally(Value *V, Align Alignment, const APInt &Size, const DataLayout &DL, Instruction *ScanFrom, 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:385
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
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:235
bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT, AssumptionCache *AC=nullptr, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Return true if we can prove that the given load (which is assumed to be within the specified loop) wo...
Definition: Loads.cpp:276
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
Various options to control the behavior of getObjectSize.
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can't be evaluated.
bool RoundToAlign
Whether to round the result up to the alignment of allocas, byval arguments, and global variables.
Represent one information held inside an operand bundle of an llvm.assume.