LLVM 24.0.0git
LoopAccessAnalysis.cpp
Go to the documentation of this file.
1//===- LoopAccessAnalysis.cpp - Loop Access Analysis Implementation --------==//
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// The implementation for the loop memory dependence that was originally
10// developed for the loop vectorizer.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APInt.h"
16#include "llvm/ADT/BitVector.h"
17#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/MapVector.h"
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SetVector.h"
24#include "llvm/ADT/SmallSet.h"
42#include "llvm/IR/BasicBlock.h"
43#include "llvm/IR/Constants.h"
44#include "llvm/IR/DataLayout.h"
45#include "llvm/IR/DebugLoc.h"
48#include "llvm/IR/Dominators.h"
49#include "llvm/IR/Function.h"
50#include "llvm/IR/InstrTypes.h"
51#include "llvm/IR/Instruction.h"
54#include "llvm/IR/PassManager.h"
55#include "llvm/IR/Type.h"
56#include "llvm/IR/Value.h"
57#include "llvm/IR/ValueHandle.h"
60#include "llvm/Support/Debug.h"
64#include <algorithm>
65#include <cassert>
66#include <cstdint>
67#include <iterator>
68#include <utility>
69#include <variant>
70#include <vector>
71
72using namespace llvm;
73using namespace llvm::SCEVPatternMatch;
74
75#define DEBUG_TYPE "loop-accesses"
76
78 VectorizationFactor("force-vector-width", cl::Hidden,
79 cl::desc("Sets the SIMD width. Zero is autoselect."),
82
84VectorizationInterleave("force-vector-interleave", cl::Hidden,
85 cl::desc("Sets the vectorization interleave count. "
86 "Zero is autoselect."),
90
92 "runtime-memory-check-threshold", cl::Hidden,
93 cl::desc("When performing memory disambiguation checks at runtime do not "
94 "generate more than this number of comparisons (default = 8)."),
97
99 "vectorize-memory-check-threshold", cl::Hidden,
100 cl::desc("The maximum allowed number of runtime memory checks"),
102 cl::init(128));
104
105/// The maximum iterations used to merge memory checks
107 "memory-check-merge-threshold", cl::Hidden,
108 cl::desc("Maximum number of comparisons done when trying to merge "
109 "runtime memory checks. (default = 100)"),
110 cl::init(100));
111
113
115 "stencil-runtime-check-merge", cl::Hidden,
116 cl::desc("Control stencil-pattern merging of runtime memory checks"),
120 "Disable stencil merge (default)"),
122 "Enable stencil merge when runtime check count exceeds "
123 "-vectorize-memory-check-threshold"),
125 "Always attempt stencil merge regardless of check "
126 "count")));
127
129 "stencil-merge-max-groups", cl::Hidden,
130 cl::desc(
131 "Skip stencil group merging when the number of runtime checking groups "
132 "exceeds this limit, to bound compile time (default =4096)."),
133 cl::init(4096));
134
135/// Maximum SIMD width.
136const unsigned VectorizerParams::MaxVectorWidth = 64;
137
138/// We collect dependences up to this threshold.
140 MaxDependences("max-dependences", cl::Hidden,
141 cl::desc("Maximum number of dependences collected by "
142 "loop-access analysis (default = 100)"),
143 cl::init(100));
144
145/// This enables versioning on the strides of symbolically striding memory
146/// accesses in code like the following.
147/// for (i = 0; i < N; ++i)
148/// A[i * Stride1] += B[i * Stride2] ...
149///
150/// Will be roughly translated to
151/// if (Stride1 == 1 && Stride2 == 1) {
152/// for (i = 0; i < N; i+=4)
153/// A[i:i+3] += ...
154/// } else
155/// ...
157 "enable-mem-access-versioning", cl::init(true), cl::Hidden,
158 cl::desc("Enable symbolic stride memory access versioning"));
159
160/// Enable store-to-load forwarding conflict detection. This option can
161/// be disabled for correctness testing.
163 "store-to-load-forwarding-conflict-detection", cl::Hidden,
164 cl::desc("Enable conflict detection in loop-access analysis"),
165 cl::init(true));
166
168 "max-forked-scev-depth", cl::Hidden,
169 cl::desc("Maximum recursion depth when finding forked SCEVs (default = 5)"),
170 cl::init(5));
171
173 "laa-speculate-unit-stride", cl::Hidden,
174 cl::desc("Speculate that non-constant strides are unit in LAA"),
175 cl::init(true));
176
178 "hoist-runtime-checks", cl::Hidden,
179 cl::desc(
180 "Hoist inner loop runtime memory checks to outer loop if possible"),
183
185 return ::VectorizationInterleave.getNumOccurrences() > 0;
186}
187
189 PredicatedScalarEvolution &PSE, const Loop *Lp,
190 const SymbolicStrideMap &PtrToStride, Value *Ptr,
192 const SCEV *OrigSCEV = PSE.getSCEV(Ptr);
193
194 // If there is an entry in the map return the SCEV of the pointer with the
195 // symbolic stride replaced by one.
196 const SCEVUnknown *StrideSCEV = PtrToStride.lookup(Ptr);
197 if (!StrideSCEV)
198 // For a non-symbolic stride, just return the original expression.
199 return OrigSCEV;
200
201 ScalarEvolution *SE = PSE.getSE();
202 const SCEV *CT = SE->getOne(StrideSCEV->getType());
203 const SCEV *Expr;
204 const SCEVPredicate *EqPred = SE->getEqualPredicate(StrideSCEV, CT);
205 if (Predicates) {
206 Predicates->push_back(EqPred);
207 Expr = SE->rewriteUsingPredicate(OrigSCEV, Lp,
208 SCEVUnionPredicate(*Predicates, *SE));
209 } else {
210 PSE.addPredicate(*EqPred);
211 Expr = PSE.getSCEV(Ptr);
212 }
213 LLVM_DEBUG(dbgs() << "LAA: Replacing SCEV: " << *OrigSCEV << " by: " << *Expr
214 << "\n");
215 return Expr;
216}
217
219 unsigned Index, const RuntimePointerChecking &RtCheck)
220 : High(RtCheck.Pointers[Index].End), Low(RtCheck.Pointers[Index].Start),
221 AddressSpace(RtCheck.Pointers[Index]
222 .PointerValue->getType()
224 NeedsFreeze(RtCheck.Pointers[Index].NeedsFreeze) {
225 Members.push_back(Index);
226}
227
228/// Returns \p A + \p B, if it is guaranteed not to unsigned wrap. Otherwise
229/// return nullptr. \p A and \p B must have the same type.
230static const SCEV *addSCEVNoOverflow(const SCEV *A, const SCEV *B,
231 ScalarEvolution &SE) {
232 if (!SE.willNotOverflow(Instruction::Add, /*IsSigned=*/false, A, B))
233 return nullptr;
234 return SE.getAddExpr(A, B);
235}
236
237/// Returns \p A * \p B, if it is guaranteed not to unsigned wrap. Otherwise
238/// return nullptr. \p A and \p B must have the same type.
239static const SCEV *mulSCEVNoOverflow(const SCEV *A, const SCEV *B,
240 ScalarEvolution &SE) {
241 if (!SE.willNotOverflow(Instruction::Mul, /*IsSigned=*/false, A, B))
242 return nullptr;
243 return SE.getMulExpr(A, B);
244}
245
246/// Return true, if evaluating \p AR at \p MaxBTC cannot wrap, because \p AR at
247/// \p MaxBTC is guaranteed inbounds of the accessed object.
249 const SCEVAddRecExpr *AR, const SCEV *MaxBTC, const SCEV *EltSize,
252 auto *PointerBase = SE.getPointerBase(AR->getStart());
253 auto *StartPtr = dyn_cast<SCEVUnknown>(PointerBase);
254 if (!StartPtr)
255 return false;
256 const Loop *L = AR->getLoop();
257 bool CheckForNonNull;
258 Value *StartPtrV = StartPtr->getValue();
259 // We can ignore frees, as the fact that an object of a certain size existed
260 // at the location *at some point* is sufficient to derive the nowrap fact.
261 uint64_t DerefBytes = StartPtrV->getPointerDereferenceableBytes(
262 DL, CheckForNonNull, /*CanBeFreed=*/nullptr);
263
264 // If the deref size is only known when the pointer is non-null, ignore it
265 // here and fall back to a dereferenceable assumption below.
266 if (DerefBytes && CheckForNonNull)
267 DerefBytes = 0;
268
269 const SCEV *Step = AR->getStepRecurrence(SE);
270 Type *WiderTy = SE.getWiderType(MaxBTC->getType(), Step->getType());
271 const SCEV *DerefBytesSCEV = SE.getConstant(WiderTy, DerefBytes);
272
273 // Check if we have a suitable dereferencable assumption we can use.
274 Instruction *CtxI = &*L->getHeader()->getFirstNonPHIIt();
275 if (BasicBlock *LoopPred = L->getLoopPredecessor()) {
276 if (isa<UncondBrInst, CondBrInst>(LoopPred->getTerminator()))
277 CtxI = LoopPred->getTerminator();
278 }
280 StartPtrV, Attribute::Dereferenceable, *AC,
281 [&](RetainedKnowledge RK, Instruction *Assume, auto) {
282 if (!isValidAssumeForContext(Assume, CtxI, DT))
283 return false;
284 const SCEV *DerefRKSCEV = SE.getSCEV(RK.IRArgValue);
285 Type *CommonTy =
286 SE.getWiderType(DerefBytesSCEV->getType(), DerefRKSCEV->getType());
287 DerefBytesSCEV = SE.getNoopOrZeroExtend(DerefBytesSCEV, CommonTy);
288 DerefRKSCEV = SE.getNoopOrZeroExtend(DerefRKSCEV, CommonTy);
289 DerefBytesSCEV = SE.getUMaxExpr(DerefBytesSCEV, DerefRKSCEV);
290 // Continue with other assumptions.
291 return false;
292 });
293
294 if (DerefBytesSCEV->isZero())
295 return false;
296
297 bool IsKnownNonNegative = SE.isKnownNonNegative(Step);
298 if (!IsKnownNonNegative && !SE.isKnownNegative(Step))
299 return false;
300
301 WiderTy = SE.getWiderType(WiderTy, DerefBytesSCEV->getType());
302 Step = SE.getNoopOrSignExtend(Step, WiderTy);
303 MaxBTC = SE.getNoopOrZeroExtend(MaxBTC, WiderTy);
304
305 // For the computations below, make sure they don't unsigned wrap.
306 // FIXME: for a negative step the lowest accessed address is not
307 // AR->getStart() but AR->evaluateAtIteration(MaxBTC, SE); the check below
308 // therefore compares StartPtr against the highest accessed address instead
309 // of the lowest.
310 if (!SE.isKnownPredicate(CmpInst::ICMP_UGE, AR->getStart(), StartPtr))
311 return false;
312 const SCEV *StartOffset = SE.getNoopOrZeroExtend(
313 SE.getMinusSCEV(AR->getStart(), StartPtr), WiderTy);
314
315 MaxBTC = SE.applyLoopGuards(MaxBTC, LoopGuards);
316
317 const SCEV *AbsStep = SE.getAbsExpr(Step, /*IsNSW=*/false);
318 // Total distance (in bytes) between the first and the last
319 // accessed pointer.
320 const SCEV *DistToLastIter = mulSCEVNoOverflow(MaxBTC, AbsStep, SE);
321 if (!DistToLastIter) {
322 // Re-try with constant max backedge-taken count if using the symbolic one
323 // failed.
324 MaxBTC = SE.getConstantMaxBackedgeTakenCount(AR->getLoop());
325 if (isa<SCEVCouldNotCompute>(MaxBTC))
326 return false;
327 MaxBTC = SE.getNoopOrZeroExtend(MaxBTC, WiderTy);
328 DistToLastIter = mulSCEVNoOverflow(MaxBTC, AbsStep, SE);
329 if (!DistToLastIter)
330 return false;
331 }
332
333 // Total length in bytes of the accessed range (from the first accessed
334 // byte through the end of the last access).
335 const SCEV *AccessedBytes = addSCEVNoOverflow(
336 DistToLastIter, SE.getNoopOrZeroExtend(EltSize, WiderTy), SE);
337 if (!AccessedBytes)
338 return false;
339
340 // Compute MaxOffset per direction: exclusive upper offset of the
341 // accessed range.
342 const SCEV *MaxOffset;
343 if (IsKnownNonNegative) {
344 MaxOffset = addSCEVNoOverflow(StartOffset, AccessedBytes, SE);
345 if (!MaxOffset)
346 return false;
347 DerefBytesSCEV = SE.applyLoopGuards(DerefBytesSCEV, LoopGuards);
348 } else {
349 // FIXME: two independent off-by-EltSize bugs on this branch:
350 // 1. StartOffset here is actually the HIGHEST offset, because it is
351 // computed from AR->getStart() rather than
352 // AR->evaluateAtIteration(MaxBTC, SE) (see FIXME above).
353 // 2. The lower check is over-strict by EltSize and the upper is
354 // under-counted by EltSize.
355 assert(SE.isKnownNegative(Step) && "must be known negative");
356 if (!SE.isKnownPredicate(CmpInst::ICMP_SGE, StartOffset, AccessedBytes))
357 return false;
358 MaxOffset = StartOffset;
359 }
360 // MaxOffset must not exceed the deref-region end.
361 return SE.isKnownPredicate(CmpInst::ICMP_ULE, MaxOffset, DerefBytesSCEV);
362}
363
364/// Return true if \p S is known to be monotonically non-decreasing
365/// (in the unsigned sense, without unsigned wrap) across iterations of \p L.
366static bool isKnownNonDecreasingInLoop(const SCEV *S, const Loop *L,
367 ScalarEvolution &SE) {
368 if (SE.isLoopInvariant(S, L))
369 return true;
370
371 switch (S->getSCEVType()) {
372 case scUDivExpr: {
373 // Non-decreasing in the numerator when the divisor is loop-invariant.
374 const auto *UDiv = cast<SCEVUDivExpr>(S);
375 return SE.isLoopInvariant(UDiv->getRHS(), L) &&
376 isKnownNonDecreasingInLoop(UDiv->getLHS(), L, SE);
377 }
378 case scAddRecExpr: {
379 auto *AR = cast<SCEVAddRecExpr>(S);
380 assert(AR->getLoop() == L &&
381 "trying to check for AddRec in different loop");
384 }
385 case scAddExpr:
386 case scMulExpr: {
387 const auto *NAry = cast<SCEVNAryExpr>(S);
388 if (!NAry->hasNoUnsignedWrap())
389 return false;
390 // With NUW, the exact sum or product fits in the type, so it is
391 // non-decreasing if every operandis.
392 return all_of(NAry->operands(), [&](const SCEV *Op) {
393 return isKnownNonDecreasingInLoop(Op, L, SE);
394 });
395 }
396 default:
397 return false;
398 }
399}
400
401/// Try to bound a loop-variant pointer that is not an affine AddRec.
402///
403/// If the offset is provably monotonically non-decreasing the accessed range is
404/// bounded by the offset's value at the first iteration (via
405/// SplitIntoInitAndPostInc) and last iteration (via getSCEVAtScope). The
406/// returned range is half-open: \p EltSizeSCEV is added to the address of the
407/// last accessed element to form the end.
408///
409/// Returns {nullptr, nullptr} if no such bound can be formed.
410static std::pair<const SCEV *, const SCEV *>
411getNonAffineMonotonicBounds(const Loop *Lp, const SCEV *PtrExpr,
412 const SCEV *EltSizeSCEV, ScalarEvolution *SE) {
413 const auto *PtrAdd = dyn_cast<SCEVAddExpr>(PtrExpr);
414 if (!PtrAdd || !PtrAdd->hasNoUnsignedWrap())
415 return {nullptr, nullptr};
416
417 const SCEV *Base = *find_if(PtrAdd->operands(), [](const auto &Op) {
418 return Op->getType()->isPointerTy();
419 });
421 return {nullptr, nullptr};
422
423 const SCEV *Offset = SE->getMinusSCEV(PtrExpr, Base);
426 return {nullptr, nullptr};
427
428 const SCEV *OffStart = SE->SplitIntoInitAndPostInc(Lp, Offset).first;
429 const SCEV *OffEnd = SE->getSCEVAtScope(Offset, Lp->getParentLoop());
430 if (isa<SCEVCouldNotCompute>(OffStart) || isa<SCEVCouldNotCompute>(OffEnd) ||
431 !SE->isLoopInvariant(OffStart, Lp) || !SE->isLoopInvariant(OffEnd, Lp))
432 return {nullptr, nullptr};
433
434 return {SE->getAddExpr(Base, OffStart),
435 SE->getAddExpr(Base, OffEnd, EltSizeSCEV)};
436}
437
438std::pair<const SCEV *, const SCEV *> llvm::getStartAndEndForAccess(
439 const Loop *Lp, const SCEV *PtrExpr, Type *AccessTy, const SCEV *BTC,
440 const SCEV *MaxBTC, ScalarEvolution *SE,
441 DenseMap<std::pair<const SCEV *, const SCEV *>,
442 std::pair<const SCEV *, const SCEV *>> *PointerBounds,
444 ScalarEvolution::LoopGuards &LoopGuards) {
445 auto &DL = Lp->getHeader()->getDataLayout();
446 Type *IdxTy = DL.getIndexType(PtrExpr->getType());
447 const SCEV *EltSizeSCEV = SE->getStoreSizeOfExpr(IdxTy, AccessTy);
448
449 // Delegate to the SCEV-based overload, passing through the cache.
450 return getStartAndEndForAccess(Lp, PtrExpr, EltSizeSCEV, BTC, MaxBTC, SE,
451 PointerBounds, DT, AC, LoopGuards);
452}
453
454std::pair<const SCEV *, const SCEV *> llvm::getStartAndEndForAccess(
455 const Loop *Lp, const SCEV *PtrExpr, const SCEV *EltSizeSCEV,
456 const SCEV *BTC, const SCEV *MaxBTC, ScalarEvolution *SE,
457 DenseMap<std::pair<const SCEV *, const SCEV *>,
458 std::pair<const SCEV *, const SCEV *>> *PointerBounds,
460 ScalarEvolution::LoopGuards &LoopGuards) {
461 std::pair<const SCEV *, const SCEV *> *PtrBoundsPair;
462 if (PointerBounds) {
463 auto [Iter, Ins] = PointerBounds->insert(
464 {{PtrExpr, EltSizeSCEV},
465 {SE->getCouldNotCompute(), SE->getCouldNotCompute()}});
466 if (!Ins)
467 return Iter->second;
468 PtrBoundsPair = &Iter->second;
469 }
470
471 // ScStart is the lowest accessed address; ScEnd is the highest one plus the
472 // size of the accessed element.
473 const SCEV *ScStart;
474 const SCEV *ScEnd;
475
476 auto &DL = Lp->getHeader()->getDataLayout();
477 if (SE->isLoopInvariant(PtrExpr, Lp)) {
478 ScStart = PtrExpr;
479 ScEnd = SE->getAddExpr(PtrExpr, EltSizeSCEV);
480 } else if (auto *AR = dyn_cast<SCEVAddRecExpr>(PtrExpr)) {
481 assert(AR->getLoop() == Lp && "LoopGuards must be the guards of Lp");
482 const SCEV *Step = AR->getStepRecurrence(*SE);
483 // The address of the last accessed element, if it can be computed
484 // precisely.
485 const SCEV *LastAddr = nullptr;
486 if (!isa<SCEVCouldNotCompute>(BTC)) {
487 // Evaluating AR at an exact BTC is safe: LAA separately checks that
488 // accesses cannot wrap in the loop. If evaluating AR at BTC wraps, then
489 // the loop either triggers UB when executing a memory access with a
490 // poison pointer or the wrapping/poisoned pointer is not used.
491 LastAddr = AR->evaluateAtIteration(BTC, *SE);
493 AR, MaxBTC, EltSizeSCEV, *SE, DL, DT, AC, LoopGuards)) {
494 LastAddr = AR->evaluateAtIteration(MaxBTC, *SE);
495 }
496 const SCEV *Start = AR->getStart();
497 Type *PtrTy = AR->getType();
498 if (SE->isKnownNegative(Step)) {
499 ScStart =
500 LastAddr
501 ? LastAddr
503 Constant::getNullValue(DL.getIndexType(PtrTy)), PtrTy));
504 ScEnd = SE->getAddExpr(Start, EltSizeSCEV);
505 } else if (SE->isKnownNonNegative(Step)) {
506 ScStart = Start;
507 // The highest address for the type saturates; adding EltSize to it would
508 // wrap to the start of the address space.
509 if (LastAddr)
510 ScEnd = SE->getAddExpr(LastAddr, EltSizeSCEV);
511 else
513 Constant::getAllOnesValue(DL.getIndexType(PtrTy)), PtrTy));
514 } else {
515 if (!LastAddr)
516 return {SE->getCouldNotCompute(), SE->getCouldNotCompute()};
517 // Fallback case: the step is not constant, but we can still
518 // get the upper and lower bounds of the interval by using min/max
519 // expressions.
520 ScStart = SE->getUMinExpr(Start, LastAddr);
521 ScEnd = SE->getAddExpr(SE->getUMaxExpr(Start, LastAddr), EltSizeSCEV);
522 }
523 } else {
524 // The pointer is loop-variant but not an affine AddRec. Try to form a
525 // tight bound for a monotonic offset (see getNonAffineMonotonicBounds).
526 std::tie(ScStart, ScEnd) =
527 getNonAffineMonotonicBounds(Lp, PtrExpr, EltSizeSCEV, SE);
528 if (!ScStart)
529 return {SE->getCouldNotCompute(), SE->getCouldNotCompute()};
530 }
531
532 assert(SE->isLoopInvariant(ScStart, Lp) && "ScStart needs to be invariant");
533 assert(SE->isLoopInvariant(ScEnd, Lp) && "ScEnd needs to be invariant");
534
535 std::pair<const SCEV *, const SCEV *> Res = {ScStart, ScEnd};
536 if (PointerBounds)
537 *PtrBoundsPair = Res;
538 return Res;
539}
540
541/// Calculate Start and End points of memory access using
542/// getStartAndEndForAccess.
543bool RuntimePointerChecking::insert(Loop *Lp, Value *Ptr, const SCEV *PtrExpr,
544 Type *AccessTy, bool WritePtr,
545 unsigned DepSetId, unsigned ASId,
547 bool NeedsFreeze, bool IsForked) {
548 const SCEV *SymbolicMaxBTC = PSE.getSymbolicMaxBackedgeTakenCount();
549 const SCEV *BTC = PSE.getBackedgeTakenCount();
550 const auto &[ScStart, ScEnd] = getStartAndEndForAccess(
551 Lp, PtrExpr, AccessTy, BTC, SymbolicMaxBTC, PSE.getSE(),
552 &DC.getPointerBounds(), DC.getDT(), DC.getAC(), LoopGuards);
554 return false;
555 Pointers.emplace_back(Ptr, ScStart, ScEnd, WritePtr, DepSetId, ASId, PtrExpr,
556 NeedsFreeze, IsForked);
557 return true;
558}
559
560bool RuntimePointerChecking::tryToCreateDiffCheck(
561 const RuntimeCheckingPtrGroup &CGI, const RuntimeCheckingPtrGroup &CGJ) {
562 // If either group contains multiple different pointers, bail out.
563 // TODO: Support multiple pointers by using the minimum or maximum pointer,
564 // depending on src & sink.
565 if (CGI.Members.size() != 1 || CGJ.Members.size() != 1)
566 return false;
567
568 const PointerInfo *Src = &Pointers[CGI.Members[0]];
569 const PointerInfo *Sink = &Pointers[CGJ.Members[0]];
570
571 // If either pointer is read and written, multiple checks may be needed. Bail
572 // out.
573 if (!DC.getOrderForAccess(Src->PointerValue, !Src->IsWritePtr).empty() ||
574 !DC.getOrderForAccess(Sink->PointerValue, !Sink->IsWritePtr).empty())
575 return false;
576
577 ArrayRef<unsigned> AccSrc =
578 DC.getOrderForAccess(Src->PointerValue, Src->IsWritePtr);
579 ArrayRef<unsigned> AccSink =
580 DC.getOrderForAccess(Sink->PointerValue, Sink->IsWritePtr);
581 // If either pointer is accessed multiple times, there may not be a clear
582 // src/sink relation. Bail out for now.
583 if (AccSrc.size() != 1 || AccSink.size() != 1)
584 return false;
585
586 // If the sink is accessed before src, swap src/sink.
587 if (AccSink[0] < AccSrc[0])
588 std::swap(Src, Sink);
589
590 const SCEVConstant *Step;
591 const SCEV *SrcStart;
592 const SCEV *SinkStart;
593 const Loop *InnerLoop = DC.getInnermostLoop();
594 if (!match(Src->Expr,
596 m_SpecificLoop(InnerLoop))) ||
597 !match(Sink->Expr,
599 m_SpecificLoop(InnerLoop))))
600 return false;
601
603 DC.getInstructionsForAccess(Src->PointerValue, Src->IsWritePtr);
605 DC.getInstructionsForAccess(Sink->PointerValue, Sink->IsWritePtr);
606 Type *SrcTy = getLoadStoreType(SrcInsts[0]);
607 Type *DstTy = getLoadStoreType(SinkInsts[0]);
609 return false;
610
611 const DataLayout &DL = InnerLoop->getHeader()->getDataLayout();
612 unsigned AllocSize =
613 std::max(DL.getTypeAllocSize(SrcTy), DL.getTypeAllocSize(DstTy));
614
615 // Only matching constant steps matching the AllocSize are supported at the
616 // moment. This simplifies the difference computation. Can be extended in the
617 // future.
618 if (Step->getAPInt().abs() != AllocSize)
619 return false;
620
621 // When counting down, the dependence distance needs to be swapped.
622 if (Step->getValue()->isNegative())
623 std::swap(SinkStart, SrcStart);
624
625 const SCEV *SinkStartInt = SE->getPtrToAddrExpr(SinkStart);
626 const SCEV *SrcStartInt = SE->getPtrToAddrExpr(SrcStart);
627 if (isa<SCEVCouldNotCompute>(SinkStartInt) ||
628 isa<SCEVCouldNotCompute>(SrcStartInt))
629 return false;
630
631 // If the start values for both Src and Sink also vary according to an outer
632 // loop, then it's probably better to avoid creating diff checks because
633 // they may not be hoisted. We should instead let llvm::addRuntimeChecks
634 // do the expanded full range overlap checks, which can be hoisted.
635 if (HoistRuntimeChecks && InnerLoop->getParentLoop() &&
636 isa<SCEVAddRecExpr>(SinkStartInt) && isa<SCEVAddRecExpr>(SrcStartInt)) {
637 auto *SrcStartAR = cast<SCEVAddRecExpr>(SrcStartInt);
638 auto *SinkStartAR = cast<SCEVAddRecExpr>(SinkStartInt);
639 const Loop *StartARLoop = SrcStartAR->getLoop();
640 if (StartARLoop == SinkStartAR->getLoop() &&
641 StartARLoop == InnerLoop->getParentLoop() &&
642 // If the diff check would already be loop invariant (due to the
643 // recurrences being the same), then we prefer to keep the diff checks
644 // because they are cheaper.
645 SrcStartAR->getStepRecurrence(*SE) !=
646 SinkStartAR->getStepRecurrence(*SE)) {
647 LLVM_DEBUG(dbgs() << "LAA: Not creating diff runtime check, since these "
648 "cannot be hoisted out of the outer loop\n");
649 return false;
650 }
651 }
652
653 LLVM_DEBUG(dbgs() << "LAA: Creating diff runtime check for:\n"
654 << "SrcStart: " << *SrcStartInt << '\n'
655 << "SinkStartInt: " << *SinkStartInt << '\n');
656 DiffChecks.emplace_back(SrcStartInt, SinkStartInt, AllocSize,
657 Src->NeedsFreeze || Sink->NeedsFreeze);
658 return true;
659}
660
662 SmallVector<RuntimePointerCheck, 4> Checks;
663
664 for (unsigned I = 0; I < CheckingGroups.size(); ++I) {
665 for (unsigned J = I + 1; J < CheckingGroups.size(); ++J) {
668
669 if (needsChecking(CGI, CGJ)) {
670 CanUseDiffCheck = CanUseDiffCheck && tryToCreateDiffCheck(CGI, CGJ);
671 Checks.emplace_back(&CGI, &CGJ);
672 }
673 }
674 }
675 return Checks;
676}
677
680 assert(Checks.empty() && "Checks is not empty");
681 groupChecks(DepCands);
682 mergeStencilGroups();
683 Checks = generateChecks();
684}
685
687 const RuntimeCheckingPtrGroup &M, const RuntimeCheckingPtrGroup &N) const {
688 for (const auto &I : M.Members)
689 for (const auto &J : N.Members)
690 if (needsChecking(I, J))
691 return true;
692 return false;
693}
694
695/// Compare \p I and \p J and return the minimum.
696/// Return nullptr in case we couldn't find an answer.
697static const SCEV *getMinFromExprs(const SCEV *I, const SCEV *J,
698 ScalarEvolution *SE) {
699 std::optional<APInt> Diff = SE->computeConstantDifference(J, I);
700 if (!Diff)
701 return nullptr;
702 return Diff->isNegative() ? J : I;
703}
704
706 unsigned Index, const RuntimePointerChecking &RtCheck) {
707 return addPointer(
708 Index, RtCheck.Pointers[Index].Start, RtCheck.Pointers[Index].End,
709 RtCheck.Pointers[Index].PointerValue->getType()->getPointerAddressSpace(),
710 RtCheck.Pointers[Index].NeedsFreeze, *RtCheck.SE);
711}
712
713bool RuntimeCheckingPtrGroup::addPointer(unsigned Index, const SCEV *Start,
714 const SCEV *End, unsigned AS,
715 bool NeedsFreeze,
716 ScalarEvolution &SE) {
717 assert(AddressSpace == AS &&
718 "all pointers in a checking group must be in the same address space");
719
720 // Compare the starts and ends with the known minimum and maximum
721 // of this set. We need to know how we compare against the min/max
722 // of the set in order to be able to emit memchecks.
723 const SCEV *Min0 = getMinFromExprs(Start, Low, &SE);
724 if (!Min0)
725 return false;
726
727 const SCEV *Min1 = getMinFromExprs(End, High, &SE);
728 if (!Min1)
729 return false;
730
731 // Update the low bound expression if we've found a new min value.
732 if (Min0 == Start)
733 Low = Start;
734
735 // Update the high bound expression if we've found a new max value.
736 if (Min1 != End)
737 High = End;
738
739 Members.push_back(Index);
740 this->NeedsFreeze |= NeedsFreeze;
741 return true;
742}
743
744void RuntimePointerChecking::groupChecks(
746 // We build the groups from dependency candidates equivalence classes
747 // because:
748 // - We know that pointers in the same equivalence class share
749 // the same underlying object and therefore there is a chance
750 // that we can compare pointers
751 // - We wouldn't be able to merge two pointers for which we need
752 // to emit a memcheck. The classes in DepCands are already
753 // conveniently built such that no two pointers in the same
754 // class need checking against each other.
755
756 // We use the following (greedy) algorithm to construct the groups
757 // For every pointer in the equivalence class:
758 // For each existing group:
759 // - if the difference between this pointer and the min/max bounds
760 // of the group is a constant, then make the pointer part of the
761 // group and update the min/max bounds of that group as required.
762
763 CheckingGroups.clear();
764
765 // If we need to check two pointers to the same underlying object
766 // with a non-constant difference, we shouldn't perform any pointer
767 // grouping with those pointers. This is because we can easily get
768 // into cases where the resulting check would return false, even when
769 // the accesses are safe.
770 //
771 // The following example shows this:
772 // for (i = 0; i < 1000; ++i)
773 // a[5000 + i * m] = a[i] + a[i + 9000]
774 //
775 // Here grouping gives a check of (5000, 5000 + 1000 * m) against
776 // (0, 10000) which is always false. However, if m is 1, there is no
777 // dependence. Not grouping the checks for a[i] and a[i + 9000] allows
778 // us to perform an accurate check in this case.
779 //
780 // In the above case, we have a non-constant distance and an Unknown
781 // dependence between accesses to the same underlying object, and could retry
782 // with runtime checks without dependency information being available. In this
783 // case we will use the fallback path and create separate checking groups for
784 // accesses not present in DepCands.
785
786 unsigned TotalComparisons = 0;
787
789 for (unsigned Index = 0; Index < Pointers.size(); ++Index)
790 PositionMap[{Pointers[Index].PointerValue, Pointers[Index].IsWritePtr}]
791 .push_back(Index);
792
793 // We need to keep track of what pointers we've already seen so we
794 // don't process them twice.
796
797 // Go through all equivalence classes, get the "pointer check groups"
798 // and add them to the overall solution. We use the order in which accesses
799 // appear in 'Pointers' to enforce determinism.
800 for (unsigned I = 0; I < Pointers.size(); ++I) {
801 // We've seen this pointer before, and therefore already processed
802 // its equivalence class.
803 if (Seen.contains(I))
804 continue;
805
807 Pointers[I].IsWritePtr);
808
809 // If there is no entry in the dependency partition, there are no potential
810 // accesses to merge; simply add a new pointer checking group.
811 if (!DepCands.contains(Access)) {
812 CheckingGroups.push_back(RuntimeCheckingPtrGroup(I, *this));
813 continue;
814 }
815
817
818 // Because DepCands is constructed by visiting accesses in the order in
819 // which they appear in alias sets (which is deterministic) and the
820 // iteration order within an equivalence class member is only dependent on
821 // the order in which unions and insertions are performed on the
822 // equivalence class, the iteration order is deterministic.
823 for (auto M : DepCands.members(Access)) {
824 for (unsigned Pointer : PositionMap.lookup(M)) {
825 assert(!Seen.contains(Pointer) && "pointer already processed");
826 Seen.insert(Pointer);
827 bool Merged = false;
828
829 // Go through all the existing sets and see if we can find one
830 // which can include this pointer.
831 for (RuntimeCheckingPtrGroup &Group : Groups) {
832 // Don't perform more than a certain amount of comparisons.
833 // This should limit the cost of grouping the pointers to something
834 // reasonable. If we do end up hitting this threshold, the algorithm
835 // will create separate groups for all remaining pointers.
836 if (TotalComparisons > MemoryCheckMergeThreshold)
837 break;
838
839 TotalComparisons++;
840
841 if (Group.addPointer(Pointer, *this)) {
842 Merged = true;
843 break;
844 }
845 }
846
847 if (!Merged)
848 // We couldn't add this pointer to any existing set or the threshold
849 // for the number of comparisons has been reached. Create a new group
850 // to hold the current pointer.
851 Groups.emplace_back(Pointer, *this);
852 }
853 }
854
855 // We've computed the grouped checks for this partition.
856 // Save the results and continue with the next one.
858 }
859}
860
861/// Result of decomposing a SCEV expression into stencil offset form:
862/// Offset = Constant + sum(Coefficients[stride] * stride)
863/// where each stride is a loop-invariant SCEV expression.
865 int64_t Constant = 0;
866 /// Map from loop-invariant stride SCEV to its integer coefficient.
868};
869
870/// Recursion cap for addScaledStencilTerm. Depth counts how deep a term
871/// sits inside the offset expression. For example, the offset
872/// 8 + (64 * (s1 + s2 + (4 * s3)))
873/// is visited like this:
874/// depth 0: the whole add
875/// depth 1: its operands 8 and (64 * (s1 + s2 + (4 * s3)))
876/// depth 2: (s1 + s2 + (4 * s3)), the operand of the multiply
877/// depth 3: s1, s2 and (4 * s3), the operands of that add
878/// At depth 3 addScaledStencilTerm stops going deeper. s1 and s2 are plain
879/// strides anyway. (4 * s3) is not split into 4 times s3: it becomes one
880/// stride key as it is, with coefficient 64. The result is Constant = 8
881/// and coefficients {s1: 64, s2: 64, (4 * s3): 64}.
882/// Three levels cover the stencil offsets we care about: a top-level add,
883/// a constant times a sum inside it, and the strides in that sum. A deeper
884/// term is kept whole as one stride key. The merge does not care what is
885/// inside a key. It only needs a loop-invariant value with a
886/// positive-stride predicate, and a whole term has both. The only cost is
887/// precision, when another member uses a part of that term, here s3 alone,
888/// as a key of its own. isNeverAbove sees two unrelated keys, so a member
889/// that is in fact always lower or higher may stay a candidate.
890constexpr unsigned MaxStencilDecomposeDepth = 3;
891
892/// Add one term of a stencil offset to \p D. \p Mult is the factor in
893/// front of the term; the top-level call passes 1.
894/// Example: the offset 8 + (-64 * (s1 + s2)) + (-32 * s1), Mult = 1. It is
895/// an add, so each operand is visited in turn with the same Mult = 1:
896/// 8 a constant: D.Constant += 1 * 8
897/// (-64 * (s1 + s2)) a constant times X: visit X = (s1 + s2) with
898/// Mult = 1 * -64. X is an add, so each operand is
899/// visited with Mult = -64:
900/// s1 a stride: D.Coefficients[s1] += -64
901/// s2 a stride: D.Coefficients[s2] += -64
902/// (-32 * s1) a constant times X: visit X = s1 with Mult = -32:
903/// s1 a stride: D.Coefficients[s1] += -32
904/// Result: Constant = 8, Coefficients {s1: -96, s2: -64}. The -64 and the
905/// -32 for s1 come from two different terms and add up in the map.
906/// So, by the kind of term:
907/// constant K D.Constant += Mult * K
908/// (K * X) visit X with Mult * K
909/// (a + b + ...) visit a, b, ... each with this same Mult
910/// anything else a stride key: D.Coefficients[Term] += Mult
911/// The two recursive cases only fire while Depth is below
912/// MaxStencilDecomposeDepth. At the cap, (K * X) and (a + b + ...) are
913/// stride keys like anything else; that is not a bailout.
914/// Returns false when a constant does not fit in int64_t or an update
915/// overflows. The caller then drops the whole decomposition.
916static bool addScaledStencilTerm(const SCEV *Term, int64_t Mult, unsigned Depth,
918 const SCEVConstant *C;
919 // A constant folds into the running constant at any depth.
920 if (match(Term, m_SCEVConstant(C))) {
921 std::optional<int64_t> V = C->getAPInt().trySExtValue();
922 int64_t Scaled;
923 return V && !MulOverflow(Mult, *V, Scaled) &&
924 !AddOverflow(D.Constant, Scaled, D.Constant);
925 }
926
928 const SCEV *Inner;
929 if (match(Term, m_scev_Mul(m_SCEVConstant(C), m_SCEV(Inner)))) {
930 std::optional<int64_t> V = C->getAPInt().trySExtValue();
931 int64_t NewMult;
932 return V && !MulOverflow(Mult, *V, NewMult) &&
933 addScaledStencilTerm(Inner, NewMult, Depth + 1, D);
934 }
935 if (auto *Add = dyn_cast<SCEVAddExpr>(Term))
936 return all_of(Add->operands(), [&](const SCEV *Op) {
937 return addScaledStencilTerm(Op, Mult, Depth + 1, D);
938 });
939 }
940
941 // Anything else is one stride key.
942 int64_t &Coeff = D.Coefficients[Term];
943 return !AddOverflow(Coeff, Mult, Coeff);
944}
945
946/// Try to decompose \p Expr into a stencil offset function of loop-invariant
947/// strides: C + a1*s1 + a2*s2 + ...
948/// \p Expr is the difference of two access "Start" SCEVs (Start_member -
949/// Start_base). A "Start" is the low bound of a memory access range as computed
950/// by getStartAndEndForAccess: the address of the first byte the access can
951/// touch. The result describes where one member's range sits relative to the
952/// base member's range.
953/// Constant factors are distributed over sums. SCEV can keep a factored form:
954/// -64*s1 + -64*s2 is stored as (-64 * (s1 + s2)). Distributing the -64 gives
955/// the coefficients {s1: -64, s2: -64}, so every member of a group is keyed
956/// on the same base strides.
957/// Relies on SCEV's canonical form: AddExpr operands are flattened (N-ary),
958/// MulExpr has the constant operand first when present.
959/// Returns std::nullopt if a constant, multiplier, or coefficient update does
960/// not fit in int64_t.
961static std::optional<StencilDecomposition>
962decomposeStencilOffset(const SCEV *Expr, ScalarEvolution &SE, const Loop &L) {
963 // A "Start" is always loop-invariant (getStartAndEndForAccess asserts it), so
964 // the difference Expr passed in by the caller is loop-invariant too, and so
965 // is every term addScaledStencilTerm visits.
966 assert(SE.isLoopInvariant(Expr, &L) && "expected a loop-invariant offset");
967
969 if (!addScaledStencilTerm(Expr, /*Mult=*/1, /*Depth=*/0, D))
970 return std::nullopt;
971 return D;
972}
973
974/// Find a common upper limit M for the positive strides in D. If every stride
975/// is between 1 and M, the decomposed offset fits in the signed index type.
976/// This lets isNeverAbove compare offsets as ordinary signed integers.
977///
978/// Subtract abs(Constant) from SignedMax, then divide the remaining budget by
979/// the sum of absolute coefficients:
980/// M = (SignedMax - abs(Constant)) / sum(abs(Coefficient)).
981/// For example, both 8 + 4*s and 8 - 4*s get M = (SignedMax - 8) / 4.
982///
983/// Return nullopt if abs(Constant) exceeds SignedMax or no positive stride
984/// fits. Otherwise, if all coefficients are zero, no stride limit is needed;
985/// return SignedMax.
986static std::optional<APInt>
988 uint64_t SignedMax = maxIntN(BitWidth);
989 uint64_t AbsConstant = AbsoluteValue(D.Constant);
990 if (AbsConstant > SignedMax)
991 return std::nullopt;
992 uint64_t Budget = SignedMax - AbsConstant;
993 uint64_t CoeffSum = 0;
994 for (const auto &[Stride, Coeff] : D.Coefficients) {
995 uint64_t AbsCoeff = AbsoluteValue(Coeff);
996 if (AbsCoeff > Budget - CoeffSum)
997 return std::nullopt;
998 CoeffSum += AbsCoeff;
999 }
1000 return APInt(BitWidth, CoeffSum ? Budget / CoeffSum : SignedMax);
1001}
1002
1003namespace {
1004/// The runtime checks the merge needs on each stride.
1005/// Example:
1006/// s1: {NeedsPositive = true, Max = 1000} means the checks 1 <= s1 <= 1000
1007/// s2: {Max = 50} means the check s2 <= 50
1008/// The lower limit is always 1, so a flag is enough for it.
1009/// Several members can each ask for an upper limit on the same stride, but only
1010/// the smallest one is kept.
1011class StrideLimits {
1012 struct Limit {
1013 bool NeedsPositive = false;
1014 std::optional<APInt> Max;
1015 };
1016 SmallMapVector<const SCEV *, Limit, 4> Limits;
1017
1018public:
1019 void requireLowerLimit(const SCEV *Stride) {
1020 Limits[Stride].NeedsPositive = true;
1021 }
1022
1023 void requireUpperLimit(const SCEV *Stride, const APInt &Max) {
1024 std::optional<APInt> &Current = Limits[Stride].Max;
1025 if (!Current || Max.ult(*Current))
1026 Current = Max;
1027 }
1028
1029 /// Add every new or more strict check in \p Other to this set.
1030 void addFrom(const StrideLimits &Other) {
1031 for (const auto &[Stride, L] : Other.Limits) {
1032 if (L.NeedsPositive)
1033 requireLowerLimit(Stride);
1034 if (L.Max)
1035 requireUpperLimit(Stride, *L.Max);
1036 }
1037 }
1038
1039 /// Count the strides that have no check in \p Committed yet.
1040 unsigned countNew(const StrideLimits &Committed) const {
1041 return count_if(Limits, [&](const auto &Entry) {
1042 return !Committed.Limits.contains(Entry.first);
1043 });
1044 }
1045
1046 /// Add the checks to \p PSE as SCEV predicates.
1047 void addPredicates(PredicatedScalarEvolution &PSE) const {
1048 ScalarEvolution &SE = *PSE.getSE();
1049 for (const auto &[Stride, L] : Limits) {
1050 if (L.NeedsPositive) {
1051 const SCEV *Zero = SE.getZero(Stride->getType());
1052 PSE.addPredicate(
1053 *SE.getComparePredicate(ICmpInst::ICMP_SGT, Stride, Zero));
1054 LLVM_DEBUG(dbgs() << "LAA: Adding positive-stride predicate for "
1055 << *Stride << "\n");
1056 }
1057 if (L.Max) {
1058 PSE.addPredicate(*SE.getComparePredicate(ICmpInst::ICMP_SLE, Stride,
1059 SE.getConstant(*L.Max)));
1060 LLVM_DEBUG(dbgs() << "LAA: Adding stride upper-limit predicate "
1061 << *Stride << " <= " << *L.Max << "\n");
1062 }
1063 }
1064 }
1065};
1066} // namespace
1067
1068/// Add to \p Limits the checks each stride s of \p D needs:
1069/// 1 <= s isNeverAbove assumes every stride is 1 or more.
1070/// s <= Max Max is from getStencilStrideUpperLimit.
1071/// A check is skipped when SCEV already proves it.
1072/// Returns false if getStencilStrideUpperLimit finds no Max, or if SCEV proves
1073/// that a check always fails. Example: s = smin(x, -1) can never pass 1 <= s,
1074/// so a merge would send every run to the scalar loop.
1076 unsigned BitWidth, ScalarEvolution &SE,
1077 StrideLimits &Limits) {
1078 std::optional<APInt> UpperLimit = getStencilStrideUpperLimit(D, BitWidth);
1079 if (!UpperLimit)
1080 return false;
1081
1082 const SCEV *Max = SE.getConstant(*UpperLimit);
1083 for (const auto &[Stride, Coeff] : D.Coefficients) {
1084 if (SE.isKnownNonPositive(Stride) ||
1085 SE.isKnownPredicate(ICmpInst::ICMP_SGT, Stride, Max))
1086 return false;
1087 if (!SE.isKnownPositive(Stride))
1088 Limits.requireLowerLimit(Stride);
1089 if (!SE.isKnownPredicate(ICmpInst::ICMP_SLE, Stride, Max))
1090 Limits.requireUpperLimit(Stride, *UpperLimit);
1091 }
1092 return true;
1093}
1094
1095/// Return true if offset A is never higher than offset B.
1096/// A and B are these sums:
1097/// A = A.Constant + CoefA_1 * stride_1 + CoefA_2 * stride_2 + ...
1098/// B = B.Constant + CoefB_1 * stride_1 + CoefB_2 * stride_2 + ...
1099/// A stride missing from a member's map has coefficient 0. Every stride
1100/// is 1 or more: the caller proves or predicates each stride to be positive
1101/// and that the whole expression does not overflow.
1102/// Example:
1103/// A: 0 - 80*s1
1104/// B: -40 - 40*s1
1105/// At s1 = 1 both are -80. For bigger s1, A goes down faster. So A is
1106/// never above B.
1107/// The rule checks two things:
1108/// 1. CoefA_i <= CoefB_i for every stride. So when a stride grows, B - A
1109/// grows too, or stays the same.
1110/// 2. B - A >= 0 when every stride is 1. That is ACorner <= BCorner, with
1111/// ACorner = A.Constant + the sum of all CoefA_i, same for BCorner.
1112/// B - A starts at or above zero and never goes down, so B - A >= 0 for
1113/// all stride values.
1114/// Offsets are signed and addresses are unsigned, but both members read
1115/// one object, and an object does not wrap around the address space, so
1116/// the smaller offset is the smaller address.
1117/// Returns false when ACorner or BCorner overflows int64_t. The caller
1118/// then keeps the member, which is the safe side.
1120 const StencilDecomposition &B) {
1121 int64_t ACorner = A.Constant, BCorner = B.Constant;
1122 for (const auto &[Stride, ACoeff] : A.Coefficients) {
1123 if (ACoeff > B.Coefficients.lookup(Stride))
1124 return false;
1125 if (AddOverflow(ACorner, ACoeff, ACorner))
1126 return false;
1127 }
1128 for (const auto &[Stride, BCoeff] : B.Coefficients) {
1129 if (A.Coefficients.lookup(Stride) > BCoeff)
1130 return false;
1131 if (AddOverflow(BCorner, BCoeff, BCorner))
1132 return false;
1133 }
1134 return ACorner <= BCorner;
1135}
1136
1137/// Find the members that can define the merged bound on one side.
1138/// Example for the minimum side (\p ForMin == true), two members:
1139/// A: 0 - 80*s1
1140/// B: -40 - 40*s1
1141/// For every s1 >= 1, A sits at or below B, so B can never be the lowest
1142/// member: A beats B. The members nobody beats are the candidates.
1143/// The maximum side works the same way with the comparison flipped.
1144/// When two members have equal offsets, only the first one is kept.
1145/// In other words: "beats" is a partial order on the offsets, and the
1146/// candidates are its minimal elements.
1147/// Returns indices into \p Offsets.
1148/// TODO: Worst case compares every pair of members: O(N^2). Fine for real
1149/// stencils.
1152 // A beats B when A always bounds at least as well as B: for the minimum
1153 // side A is never above B, for the maximum side A is never below B.
1154 auto Beats = [&](unsigned A, unsigned B) {
1155 return ForMin ? isNeverAbove(Offsets[A], Offsets[B])
1156 : isNeverAbove(Offsets[B], Offsets[A]);
1157 };
1158 // Skipping a beaten member loses nothing: Beats is transitive, so
1159 // whoever beat it also beats anyone it would have beaten.
1160 BitVector Beaten(Offsets.size());
1161 for (unsigned K = 0; K < Offsets.size(); ++K) {
1162 if (Beaten.test(K))
1163 continue;
1164 // Walk J = K + 1 .. N to avoid checking the same pair twice, as
1165 // (K, J) and again as (J, K). The order in a pair does not matter.
1166 for (unsigned J = K + 1; J < Offsets.size(); ++J) {
1167 if (Beaten.test(J))
1168 continue;
1169 // Checking K first settles ties: on equal offsets K survives.
1170 if (Beats(K, J)) {
1171 Beaten.set(J);
1172 } else if (Beats(J, K)) {
1173 Beaten.set(K);
1174 break;
1175 }
1176 }
1177 }
1178 SmallVector<unsigned, 4> Candidates;
1179 for (unsigned K = 0; K < Offsets.size(); ++K)
1180 if (!Beaten.test(K))
1181 Candidates.push_back(K);
1182 return Candidates;
1183}
1184
1185/// Local cost model: count the runtime checks required before and after
1186/// replacing one DepSet's groups (\p GroupIndices) with the single merged
1187/// group. Everything is counted in the same unit, one check, even though a
1188/// stride predicate or an extra umin/umax operand is cheaper at runtime
1189/// than a full group-pair check. The cheaper items only appear on the
1190/// After side, and we merge only when After < Before, so the rounding
1191/// always errs toward not merging.
1192///
1193/// Before = NumGroups * NumExternalChecks, where NumExternalChecks is the
1194/// number of groups outside this DepSet that need a check against it. The
1195/// product is exact: needsChecking() looks only at (DependencySetId,
1196/// AliasSetId) and at whether a group writes, and all groups in this
1197/// DepSet agree on those, so an external group is checked against all of
1198/// them or against none.
1199///
1200/// After = NumExternalChecks + NewPredicates + NumBoundOperands:
1201/// - the merged group keeps the same IDs, so it is checked against exactly
1202/// the same external groups;
1203/// - one check per stride needing a lower or upper limit, unless an earlier
1204/// DepSet already paid for either limit;
1205/// - a umin over k members costs k-1 compare+selects, same for the umax.
1206/// \p NumBoundOperands is the sum of the two. A single candidate costs
1207/// nothing: the bound is that member's own address.
1208///
1209/// Returns {ChecksBefore, ChecksAfter}.
1210static std::pair<unsigned, unsigned> computeStencilMergeCost(
1211 const RuntimePointerChecking &RtCheck, ArrayRef<unsigned> GroupIndices,
1212 const StrideLimits &Local, const StrideLimits &Committed,
1213 unsigned NumBoundOperands) {
1214 unsigned NumGroups = GroupIndices.size();
1215 unsigned NumExternalChecks =
1216 count_if(RtCheck.CheckingGroups, [&](const RuntimeCheckingPtrGroup &G) {
1217 return any_of(GroupIndices, [&](unsigned GI) {
1218 return RtCheck.needsChecking(RtCheck.CheckingGroups[GI], G);
1219 });
1220 });
1221
1222 unsigned NewPredicates = Local.countNew(Committed);
1223
1224 LLVM_DEBUG(dbgs() << "LAA: Cost model: NumGroups=" << NumGroups
1225 << ", NumExternalChecks=" << NumExternalChecks
1226 << ", predicates=" << NewPredicates
1227 << ", bound operands=" << NumBoundOperands << ", checks "
1228 << NumGroups * NumExternalChecks << "->"
1229 << NumExternalChecks + NewPredicates + NumBoundOperands
1230 << "\n");
1231
1232 return {NumGroups * NumExternalChecks,
1233 NumExternalChecks + NewPredicates + NumBoundOperands};
1234}
1235
1236/// Build the merged stencil group for one DepSet, after the cost model has
1237/// decided the merge is profitable. Constructs the bounding group over
1238/// \p AllMembers with bounds [\p MergedLow, \p MergedHigh]. Returns the new
1239/// group.
1242 ArrayRef<unsigned> AllMembers, const SCEV *MergedLow,
1243 const SCEV *MergedHigh,
1244 ArrayRef<unsigned> GroupIndices) {
1245 RuntimeCheckingPtrGroup CandidateGroup(AllMembers[0], RtCheck);
1246 CandidateGroup.Low = MergedLow;
1247 CandidateGroup.High = MergedHigh;
1248 append_range(CandidateGroup.Members, drop_begin(AllMembers));
1249 CandidateGroup.NeedsFreeze = any_of(GroupIndices, [&](unsigned GI) {
1250 return RtCheck.CheckingGroups[GI].NeedsFreeze;
1251 });
1252 return CandidateGroup;
1253}
1254
1255void RuntimePointerChecking::mergeStencilGroups() {
1256 LLVM_DEBUG(dbgs() << "LAA: Attempting stencil group merging on "
1257 << CheckingGroups.size() << " groups\n");
1258
1259 if (CheckingGroups.size() < 2)
1260 return;
1261
1262 // groupChecks merges two pointers only when their bounds differ by a
1263 // compile-time constant, because only then it can tell which bound is
1264 // lower or higher. A stencil kernel reads one object at several
1265 // loop-invariant offsets, so its bounds differ by expressions like
1266 // -40 - 40*s1, and every such pointer stays in its own group - often
1267 // too many checks. Here we merge those groups anyway: what we cannot
1268 // compare at compile time we compare at runtime, with a umin/umax over
1269 // the few members that can be lowest or highest. The cost: the merged
1270 // range also covers the gaps between the members, so the merged check
1271 // can report a conflict where the per-group checks would not.
1272 //
1273 // We only merge ranges for reads that happen on every loop iteration.
1274 // These reads must stay inside the array; otherwise, the original loop
1275 // already has undefined behaviour. We choose the merged bounds from
1276 // these ranges.
1277 //
1278 // We use the following algorithm to construct a merged stencil group:
1279 // - collect checking groups that share both DependencySetId and AliasSetId;
1280 // - reject groups with writes, predicated accesses, forked pointers,
1281 // different access ranges, or different recurrence steps;
1282 // - use one member as the base and decompose each other member's offset
1283 // from that base as C + sum(Coeff[Stride] * Stride), where Stride is
1284 // loop-invariant;
1285 // - keep the members that can hold the lowest or the highest address at
1286 // runtime (the candidate members), and build the merged bounds as a
1287 // umin over their Start values and a umax over their End values,
1288 // adding predicates for strides not already known positive and within
1289 // their limits;
1290 // - commit the merge only if the local cost model reduces the number of
1291 // checks after accounting for any new predicates.
1292
1293 // Stencil merging runs when either:
1294 // - the flag is set to 'force' (-stencil-runtime-check-merge=force), or
1295 // - the flag is set to 'auto' (-stencil-runtime-check-merge=auto) AND the
1296 // current check count exceeds the auto-trigger threshold, which defaults
1297 // to the vectorizer's own runtime-check cutoff
1298 // (-vectorize-memory-check-threshold). Above it the vectorizer would
1299 // otherwise reject the loop for having too many runtime checks. In that
1300 // case the merge can only improve things: at worst we decline to merge
1301 // and behave as before.
1303 LLVM_DEBUG(dbgs() << "LAA: stencil merge disabled\n");
1304 return;
1305 }
1306
1307 const Loop &L = *DC.getInnermostLoop();
1308
1309 // visitPointers expands non-header pointer PHIs before runtime checks are
1310 // created, so their alternatives are not marked IsForked. An unused
1311 // alternative may wrap and make the merged bounds miss a real overlap.
1312 for (BasicBlock *BB : L.blocks())
1313 if (BB != L.getHeader())
1314 for (PHINode &PN : BB->phis())
1315 if (PN.getType()->isPointerTy())
1316 return;
1317
1318 // For each checking group this pass decomposes each member's offset into
1319 // stencil form, keeps the candidate members (the ones that can hold the
1320 // lowest or highest address at runtime), and builds the merged bounds from
1321 // their own Start and End values. That extra SCEV work adds up on a loop
1322 // with very many groups, so bail out above a configurable limit as a
1323 // safety net against pathological inputs.
1325 LLVM_DEBUG(
1326 dbgs() << "LAA: " << CheckingGroups.size()
1327 << " groups exceeds stencil-merge-max-groups, skipping\n");
1328 return;
1329 }
1330
1332 unsigned TotalChecks = 0;
1333 for (unsigned I = 0; I < CheckingGroups.size(); ++I)
1334 for (unsigned J = I + 1; J < CheckingGroups.size(); ++J)
1336 ++TotalChecks;
1337
1338 // Above this many checks the vectorizer gives up on the loop, so that is
1339 // where merging starts to matter.
1341 LLVM_DEBUG(dbgs() << "LAA: " << TotalChecks
1342 << " checks <= threshold, skipping stencil merge\n");
1343 return;
1344 }
1345 LLVM_DEBUG(
1346 dbgs() << "LAA: " << TotalChecks
1347 << " checks > threshold, proceeding with stencil merge\n");
1348 } else {
1349 LLVM_DEBUG(dbgs() << "LAA: stencil merge forced via flag\n");
1350 }
1351
1352 // Group CheckingGroups by (DependencySetId, AliasSetId) pair.
1353 // DependencySetId alone is not unique: it resets per alias set, so
1354 // pointers in different alias sets can share the same DependencySetId.
1355 // Use MapVector for deterministic iteration order across platforms.
1356 using DepAliasKey = std::pair<unsigned, unsigned>;
1357 MapVector<DepAliasKey, SmallVector<unsigned, 4>> DepSetToGroups;
1358 for (unsigned I = 0; I < CheckingGroups.size(); ++I) {
1359 const auto &P = Pointers[CheckingGroups[I].Members[0]];
1360 DepSetToGroups[{P.DependencySetId, P.AliasSetId}].push_back(I);
1361 }
1362
1363 SmallDenseSet<unsigned, 4> MergedGroupIndices;
1365 // Stride checks from the accepted DepSets. A later DepSet can lower an
1366 // upper limit, so the predicates are added only after the last DepSet.
1367 StrideLimits CommittedStrideLimits;
1368
1369 for (auto &[DepAliasKey, GroupIndices] : DepSetToGroups) {
1370 [[maybe_unused]] auto [DepId, ASId] = DepAliasKey;
1371 if (GroupIndices.size() < 2)
1372 continue;
1373
1374 // Collect all member pointers across these groups. Only merge read-only
1375 // groups: stencil patterns read an array at multiple offsets and write to a
1376 // different array (a different DepSet). Mixing reads and writes within a
1377 // merged group complicates the cost model and doesn't match known stencil
1378 // patterns, so stop and skip the whole DepSet as soon as we see a write.
1379 SmallVector<unsigned, 8> AllMembers;
1380 bool CanMerge = true;
1381 for (unsigned GI : GroupIndices) {
1382 ArrayRef<unsigned> Members = CheckingGroups[GI].Members;
1383 if (any_of(Members,
1384 [&](unsigned Idx) { return Pointers[Idx].IsWritePtr; })) {
1385 LLVM_DEBUG(dbgs() << "LAA: Skipping DepSet(" << DepId << "," << ASId
1386 << ") with write access\n");
1387 CanMerge = false;
1388 break;
1389 }
1390 // For a forked pointer, LAA considers both possible addresses, even if
1391 // the loop only uses one of them. The unused address can be outside the
1392 // array. Its bounds can underflow or overflow, so merging them can hide
1393 // an overlap and allow unsafe vectorization.
1394 if (any_of(Members,
1395 [&](unsigned Idx) { return Pointers[Idx].IsForked; })) {
1396 LLVM_DEBUG(dbgs() << "LAA: Skipping DepSet(" << DepId << "," << ASId
1397 << ") with forked pointer\n");
1398 CanMerge = false;
1399 break;
1400 }
1401 append_range(AllMembers, Members);
1402 }
1403 if (!CanMerge)
1404 continue;
1405
1406 // A predicated access does not happen in every iteration. In the skipped
1407 // iterations its address can be outside the array. Its bounds can
1408 // underflow or overflow, so merging them can hide an overlap and allow
1409 // unsafe vectorization.
1410 // Look at the block of the actual load/store, not of the pointer: a
1411 // loop-invariant address is computed in the preheader, outside the loop.
1412 if (any_of(AllMembers, [&](unsigned Idx) {
1413 const PointerInfo &P = Pointers[Idx];
1414 assert(!P.IsWritePtr && "only read members reach this point");
1415 return any_of(
1416 DC.getInstructionsForAccess(P.PointerValue, /*isWrite=*/false),
1417 [&](Instruction *I) {
1418 return LoopAccessInfo::blockNeedsPredication(I->getParent(), &L,
1419 DC.getDT());
1420 });
1421 })) {
1422 LLVM_DEBUG(dbgs() << "LAA: Skipping DepSet(" << DepId << "," << ASId
1423 << ") with predicated access\n");
1424 continue;
1425 }
1426
1427 // Use the first member as the reference for decomposition. All offsets
1428 // are computed relative to BaseLow. BaseHigh is only used to check that
1429 // every member covers the same range. The merged bounds are built later
1430 // from the members' own Start and End values.
1431 unsigned Member0 = AllMembers[0];
1432 const SCEV *BaseLow = Pointers[Member0].Start;
1433 const SCEV *BaseHigh = Pointers[Member0].End;
1434
1435 // Keep stencil decomposition and stride-limit arithmetic within 64 bits.
1436 // All offsets relative to BaseLow have the same index width.
1437 if (SE->getTypeSizeInBits(BaseLow->getType()) > 64)
1438 continue;
1439
1440 LLVM_DEBUG(dbgs() << "LAA: Analyzing DepSet(" << DepId << "," << ASId
1441 << ") with " << AllMembers.size()
1442 << " members, base: " << *BaseLow << "\n");
1443
1444 auto GetStepForPointer = [&](unsigned Idx) -> const SCEV * {
1445 if (const auto *AR = dyn_cast<SCEVAddRecExpr>(Pointers[Idx].Expr))
1446 if (AR->getLoop() == &L)
1447 return AR->getStepRecurrence(*SE);
1448 return nullptr;
1449 };
1450
1451 const SCEV *BaseStep = GetStepForPointer(Member0);
1452 if (!BaseStep)
1453 continue;
1454
1455 // Verify all members have the same access range (End - Start). The
1456 // merged upper bound is a umax over the members' own End values. The
1457 // same decompositions order both the Start and the End values
1458 // only when End = Start + Range with one shared Range for every member.
1459 // That is what this check enforces.
1460 // Compare each member's range (End - Start) and test Range - BaseRange ==
1461 // 0, rather than Range == BaseRange, so algebraically equal but
1462 // non-identical SCEVs still match. Bail out if any subtraction produces
1463 // SCEVCouldNotCompute.
1464 const SCEV *BaseRange = SE->getMinusSCEV(BaseHigh, BaseLow);
1465 if (isa<SCEVCouldNotCompute>(BaseRange)) {
1466 LLVM_DEBUG(dbgs() << "LAA: Base access range not computable, "
1467 "skipping DepSet\n");
1468 continue;
1469 }
1470 if (any_of(drop_begin(AllMembers), [&](unsigned Idx) {
1471 const SCEV *Range =
1472 SE->getMinusSCEV(Pointers[Idx].End, Pointers[Idx].Start);
1474 return true;
1475 if (Range == BaseRange)
1476 return false;
1477 const SCEV *RangeDiff = SE->getMinusSCEV(Range, BaseRange);
1478 return isa<SCEVCouldNotCompute>(RangeDiff) || !RangeDiff->isZero();
1479 })) {
1480 LLVM_DEBUG(
1481 dbgs() << "LAA: Member with different or not computable access "
1482 "range, skipping DepSet\n");
1483 continue;
1484 }
1485
1486 // Require all members to have the same recurrence step. Equal ranges
1487 // (checked above) are what the merged bounds actually need, and a different
1488 // step usually means a different range. But ranges can be equal by accident
1489 // - e.g. an invariant access whose range matches the stride, or a loop with
1490 // a single iteration. The base member is picked arbitrarily, so together
1491 // with the BaseStep check above this keeps the decision the same no matter
1492 // which member comes first: we only merge recurrences with one common step.
1493 if (any_of(drop_begin(AllMembers), [&](unsigned Idx) {
1494 return GetStepForPointer(Idx) != BaseStep;
1495 })) {
1496 LLVM_DEBUG(dbgs() << "LAA: Member with different step, "
1497 "skipping DepSet\n");
1498 continue;
1499 }
1500 // One decomposition per member, in AllMembers order. Each entry holds the
1501 // member's constant offset and its coefficient for each stride, all
1502 // relative to BaseLow.
1504 MemberOffsets.reserve(AllMembers.size());
1505 // The base member's offset from itself is zero: Constant 0, no strides.
1506 MemberOffsets.emplace_back();
1507 // Stride checks this DepSet needs if it is merged.
1508 StrideLimits LocalStrideLimits;
1509
1510 // Decompose one member's offset (relative to BaseLow) and append it to
1511 // MemberOffsets. Returns false if the offset is not in stencil form (so
1512 // the whole DepSet is skipped).
1513 const auto CollectOffset = [&](unsigned Idx) -> bool {
1514 const SCEV *LowOffset = SE->getMinusSCEV(Pointers[Idx].Start, BaseLow);
1515 if (isa<SCEVCouldNotCompute>(LowOffset))
1516 return false;
1517 auto DLow = decomposeStencilOffset(LowOffset, *SE, L);
1518 if (!DLow) {
1519 LLVM_DEBUG(dbgs() << "LAA: Member " << Idx
1520 << " NOT decomposable: " << *LowOffset << "\n");
1521 return false;
1522 }
1523 if (!collectStrideLimits(*DLow,
1524 SE->getTypeSizeInBits(LowOffset->getType()), *SE,
1525 LocalStrideLimits))
1526 return false;
1527
1528 LLVM_DEBUG(dbgs() << "LAA: Member " << Idx
1529 << ": Const=" << DLow->Constant
1530 << ", strides=" << DLow->Coefficients.size() << "\n");
1531 MemberOffsets.push_back(std::move(*DLow));
1532 return true;
1533 };
1534
1535 if (!all_of(drop_begin(AllMembers), CollectOffset))
1536 continue;
1537
1538 SmallVector<unsigned, 4> MinCandidates =
1539 collectCandidateMembers(MemberOffsets, /*ForMin=*/true);
1540 SmallVector<unsigned, 4> MaxCandidates =
1541 collectCandidateMembers(MemberOffsets, /*ForMin=*/false);
1542 assert(!MinCandidates.empty() && !MaxCandidates.empty() &&
1543 "a non-empty member list always has a candidate");
1544 LLVM_DEBUG(dbgs() << "LAA: Candidate members: min="
1545 << MinCandidates.size()
1546 << ", max=" << MaxCandidates.size() << " of "
1547 << MemberOffsets.size() << "\n");
1548
1549 // Extra bound operands: one compare-and-select per operand past the first
1550 // in the merged umin, and the same for the umax.
1551 unsigned NumBoundOperands =
1552 (MinCandidates.size() - 1) + (MaxCandidates.size() - 1);
1553
1554 // Local cost model: decide whether replacing this DepSet's groups with the
1555 // single merged group actually reduces the number of runtime checks. Run
1556 // it before building the merged bounds: a rejected DepSet then creates no
1557 // umin/umax expressions that would only be thrown away.
1558 auto [ChecksBefore, ChecksAfter] =
1559 computeStencilMergeCost(*this, GroupIndices, LocalStrideLimits,
1560 CommittedStrideLimits, NumBoundOperands);
1561 if (ChecksAfter >= ChecksBefore) {
1562 LLVM_DEBUG(dbgs() << "LAA: Not beneficial, skipping DepSet\n");
1563 continue;
1564 }
1565
1566 // Build one side of the merged bounds from its candidate members.
1567 // With one candidate the bound is that member's own Start (or End): the
1568 // exact value the member's own check used before the merge. With several
1569 // candidates the bound is a umin (umax) over their Starts (Ends). Either
1570 // way every value is a real member address, so the merge computes no new
1571 // address and no new overflow is possible.
1572 // The umin/umax are on pointers. The expander turns them into the same
1573 // icmp and select that a plain check uses, so no address conversion is
1574 // needed.
1575 const auto BuildBound = [&](ArrayRef<unsigned> Candidates, bool IsLow) {
1577 for (unsigned K : Candidates) {
1578 const PointerInfo &P = Pointers[AllMembers[K]];
1579 Ops.push_back(IsLow ? P.Start : P.End);
1580 }
1581 return IsLow ? SE->getUMinExpr(Ops) : SE->getUMaxExpr(Ops);
1582 };
1583
1584 const SCEV *MergedLow = BuildBound(MinCandidates, /*IsLow=*/true);
1585 const SCEV *MergedHigh = BuildBound(MaxCandidates, /*IsLow=*/false);
1586
1587 LLVM_DEBUG(dbgs() << "LAA: Merged bounds: Low=" << *MergedLow
1588 << ", High=" << *MergedHigh << "\n");
1589 LLVM_DEBUG(dbgs() << "LAA: Merging, net saving "
1590 << ChecksBefore - ChecksAfter << "\n");
1591
1592 NewMergedGroups.push_back(buildMergedStencilGroup(
1593 *this, AllMembers, MergedLow, MergedHigh, GroupIndices));
1594 CommittedStrideLimits.addFrom(LocalStrideLimits);
1595 MergedGroupIndices.insert(GroupIndices.begin(), GroupIndices.end());
1596 }
1597
1598 CommittedStrideLimits.addPredicates(DC.getPSE());
1599
1600 // Rebuild CheckingGroups if we merged anything.
1601 if (!NewMergedGroups.empty()) {
1603 for (unsigned I = 0; I < CheckingGroups.size(); ++I)
1604 if (!MergedGroupIndices.contains(I))
1605 FinalGroups.push_back(std::move(CheckingGroups[I]));
1606 FinalGroups.append(std::make_move_iterator(NewMergedGroups.begin()),
1607 std::make_move_iterator(NewMergedGroups.end()));
1608 CheckingGroups = std::move(FinalGroups);
1609
1610 LLVM_DEBUG(dbgs() << "LAA: After stencil merging: " << CheckingGroups.size()
1611 << " groups\n");
1612 }
1613}
1614
1616 const SmallVectorImpl<int> &PtrToPartition, unsigned PtrIdx1,
1617 unsigned PtrIdx2) {
1618 return (PtrToPartition[PtrIdx1] != -1 &&
1619 PtrToPartition[PtrIdx1] == PtrToPartition[PtrIdx2]);
1620}
1621
1622bool RuntimePointerChecking::needsChecking(unsigned I, unsigned J) const {
1623 const PointerInfo &PointerI = Pointers[I];
1624 const PointerInfo &PointerJ = Pointers[J];
1625
1626 // No need to check if two readonly pointers intersect.
1627 if (!PointerI.IsWritePtr && !PointerJ.IsWritePtr)
1628 return false;
1629
1630 // Only need to check pointers between two different dependency sets.
1631 if (PointerI.DependencySetId == PointerJ.DependencySetId)
1632 return false;
1633
1634 // Only need to check pointers in the same alias set.
1635 return PointerI.AliasSetId == PointerJ.AliasSetId;
1636}
1637
1638/// Assign each RuntimeCheckingPtrGroup pointer an index for stable UTC output.
1642 for (const auto &[Idx, CG] : enumerate(CheckingGroups))
1643 PtrIndices[&CG] = Idx;
1644 return PtrIndices;
1645}
1646
1649 unsigned Depth) const {
1650 unsigned N = 0;
1651 auto PtrIndices = getPtrToIdxMap(CheckingGroups);
1652 for (const auto &[Check1, Check2] : Checks) {
1653 const auto &First = Check1->Members, &Second = Check2->Members;
1654 OS.indent(Depth) << "Check " << N++ << ":\n";
1655 OS.indent(Depth + 2) << "Comparing group GRP" << PtrIndices.at(Check1)
1656 << ":\n";
1657 for (unsigned K : First)
1658 OS.indent(Depth + 2) << *Pointers[K].PointerValue << "\n";
1659 OS.indent(Depth + 2) << "Against group GRP" << PtrIndices.at(Check2)
1660 << ":\n";
1661 for (unsigned K : Second)
1662 OS.indent(Depth + 2) << *Pointers[K].PointerValue << "\n";
1663 }
1664}
1665
1667
1668 OS.indent(Depth) << "Run-time memory checks:\n";
1669 printChecks(OS, Checks, Depth);
1670
1671 OS.indent(Depth) << "Grouped accesses:\n";
1672 auto PtrIndices = getPtrToIdxMap(CheckingGroups);
1673 for (const auto &CG : CheckingGroups) {
1674 OS.indent(Depth + 2) << "Group GRP" << PtrIndices.at(&CG) << ":\n";
1675 OS.indent(Depth + 4) << "(Low: " << *CG.Low << " High: " << *CG.High
1676 << ")\n";
1677 for (unsigned Member : CG.Members) {
1678 OS.indent(Depth + 6) << "Member: " << *Pointers[Member].Expr << "\n";
1679 }
1680 }
1681}
1682
1683namespace {
1684
1685/// Analyses memory accesses in a loop.
1686///
1687/// Checks whether run time pointer checks are needed and builds sets for data
1688/// dependence checking.
1689class AccessAnalysis {
1690public:
1691 using MemAccessInfo =
1692 PointerIntPair<Value * /* AccessPtr */, 1, bool /* IsWrite */>;
1693
1694 AccessAnalysis(const Loop *TheLoop, AAResults *AA, const LoopInfo *LI,
1697 SmallPtrSetImpl<MDNode *> &LoopAliasScopes)
1698 : TheLoop(TheLoop), EEA(DT, LI), BAA(*AA, &EEA), AST(BAA), LI(LI), DT(DT),
1699 DepCands(DA), PSE(PSE), LoopAliasScopes(LoopAliasScopes) {
1700 // We're analyzing dependences across loop iterations.
1701 BAA.enableCrossIterationMode();
1702 }
1703
1704 /// Register a load and whether it is only read from.
1705 void addLoad(const MemoryLocation &Loc, Type *AccessTy, bool IsReadOnly) {
1706 Value *Ptr = const_cast<Value *>(Loc.Ptr);
1707 AST.add(adjustLoc(Loc));
1708 Accesses[MemAccessInfo(Ptr, false)].insert(AccessTy);
1709 if (IsReadOnly)
1710 ReadOnlyPtr.insert(Ptr);
1711 }
1712
1713 /// Register a store.
1714 void addStore(const MemoryLocation &Loc, Type *AccessTy) {
1715 Value *Ptr = const_cast<Value *>(Loc.Ptr);
1716 AST.add(adjustLoc(Loc));
1717 Accesses[MemAccessInfo(Ptr, true)].insert(AccessTy);
1718 }
1719
1720 /// Check if we can emit a run-time no-alias check for \p Access.
1721 ///
1722 /// Returns true if we can emit a run-time no alias check for \p Access.
1723 /// If we can check this access, this also adds it to a dependence set and
1724 /// adds a run-time to check for it to \p RtCheck. If \p Assume is true,
1725 /// we will attempt to use additional run-time checks in order to get
1726 /// the bounds of the pointer.
1727 bool createCheckForAccess(RuntimePointerChecking &RtCheck,
1728 MemAccessInfo Access, Type *AccessTy,
1729 const SymbolicStrideMap &Strides,
1730 DenseMap<Value *, unsigned> &DepSetId,
1731 Loop *TheLoop, unsigned &RunningDepId,
1732 unsigned ASId, bool Assume);
1733
1734 /// Check whether we can check the pointers at runtime for
1735 /// non-intersection.
1736 ///
1737 /// Returns true if we need no check or if we do and we can generate them
1738 /// (i.e. the pointers have computable bounds). A return value of false means
1739 /// we couldn't analyze and generate runtime checks for all pointers in the
1740 /// loop, but if \p AllowPartial is set then we will have checks for those
1741 /// pointers we could analyze. \p DepChecker is used to remove unknown
1742 /// dependences from DepCands.
1743 bool canCheckPtrAtRT(RuntimePointerChecking &RtCheck, Loop *TheLoop,
1744 const SymbolicStrideMap &Strides,
1745 Value *&UncomputablePtr, bool AllowPartial,
1746 const MemoryDepChecker &DepChecker);
1747
1748 /// Goes over all memory accesses, checks whether a RT check is needed
1749 /// and builds sets of dependent accesses.
1750 void buildDependenceSets();
1751
1752 /// Initial processing of memory accesses determined that we need to
1753 /// perform dependency checking.
1754 ///
1755 /// Note that this can later be cleared if we retry memcheck analysis without
1756 /// dependency checking (i.e. ShouldRetryWithRuntimeChecks).
1757 bool isDependencyCheckNeeded() const { return !CheckDeps.empty(); }
1758
1759 /// We decided that no dependence analysis would be used. Reset the state.
1760 void resetDepChecks(MemoryDepChecker &DepChecker) {
1761 CheckDeps.clear();
1762 DepChecker.clearDependences();
1763 }
1764
1765 ArrayRef<MemAccessInfo> getDependenciesToCheck() const { return CheckDeps; }
1766
1767private:
1768 using PtrAccessMap = MapVector<MemAccessInfo, SmallSetVector<Type *, 1>>;
1769
1770 /// Adjust the MemoryLocation so that it represents accesses to this
1771 /// location across all iterations, rather than a single one.
1772 MemoryLocation adjustLoc(MemoryLocation Loc) const {
1773 // The accessed location varies within the loop, but remains within the
1774 // underlying object.
1776 Loc.AATags.Scope = adjustAliasScopeList(Loc.AATags.Scope);
1777 Loc.AATags.NoAlias = adjustAliasScopeList(Loc.AATags.NoAlias);
1778 return Loc;
1779 }
1780
1781 /// Drop alias scopes that are only valid within a single loop iteration.
1782 MDNode *adjustAliasScopeList(MDNode *ScopeList) const {
1783 if (!ScopeList)
1784 return nullptr;
1785
1786 // For the sake of simplicity, drop the whole scope list if any scope is
1787 // iteration-local.
1788 if (any_of(ScopeList->operands(), [&](Metadata *Scope) {
1789 return LoopAliasScopes.contains(cast<MDNode>(Scope));
1790 }))
1791 return nullptr;
1792
1793 return ScopeList;
1794 }
1795
1796 /// Map of all accesses. Values are the types used to access memory pointed to
1797 /// by the pointer.
1798 PtrAccessMap Accesses;
1799
1800 /// The loop being checked.
1801 const Loop *TheLoop;
1802
1803 /// List of accesses that need a further dependence check.
1805
1806 /// Set of pointers that are read only.
1807 SmallPtrSet<Value*, 16> ReadOnlyPtr;
1808
1809 /// Capture analysis for BAA.
1810 EarliestEscapeAnalysis EEA;
1811
1812 /// Batched alias analysis results.
1813 BatchAAResults BAA;
1814
1815 /// An alias set tracker to partition the access set by underlying object and
1816 //intrinsic property (such as TBAA metadata).
1817 AliasSetTracker AST;
1818
1819 /// The LoopInfo of the loop being checked.
1820 const LoopInfo *LI;
1821
1822 /// The dominator tree of the function.
1823 DominatorTree &DT;
1824
1825 /// Sets of potentially dependent accesses - members of one set share an
1826 /// underlying pointer. The set "CheckDeps" identfies which sets really need a
1827 /// dependence check.
1829
1830 /// Initial processing of memory accesses determined that we may need
1831 /// to add memchecks. Perform the analysis to determine the necessary checks.
1832 ///
1833 /// Note that, this is different from isDependencyCheckNeeded. When we retry
1834 /// memcheck analysis without dependency checking
1835 /// (i.e. ShouldRetryWithRuntimeChecks), isDependencyCheckNeeded is
1836 /// cleared while this remains set if we have potentially dependent accesses.
1837 bool IsRTCheckAnalysisNeeded = false;
1838
1839 /// The SCEV predicate containing all the SCEV-related assumptions.
1840 PredicatedScalarEvolution &PSE;
1841
1842 DenseMap<Value *, SmallVector<const Value *, 16>> UnderlyingObjects;
1843
1844 /// Alias scopes that are declared inside the loop, and as such not valid
1845 /// across iterations.
1846 SmallPtrSetImpl<MDNode *> &LoopAliasScopes;
1847};
1848
1849} // end anonymous namespace
1850
1851std::optional<int64_t>
1853 Type *AccessTy, Value *Ptr,
1855 if (isa<ScalableVectorType>(AccessTy)) {
1856 LLVM_DEBUG(dbgs() << "LAA: Bad stride - Scalable object: " << *AccessTy
1857 << "\n");
1858 return std::nullopt;
1859 }
1860
1861 // The access function must stride over the innermost loop.
1862 if (Lp != AR->getLoop()) {
1863 LLVM_DEBUG({
1864 dbgs() << "LAA: Bad stride - Not striding over innermost loop ";
1865 if (Ptr)
1866 dbgs() << *Ptr << " ";
1867
1868 dbgs() << "SCEV: " << *AR << "\n";
1869 });
1870 return std::nullopt;
1871 }
1872
1873 // Check the step is constant.
1874 const SCEV *Step = AR->getStepRecurrence(*PSE.getSE());
1875
1876 // Calculate the pointer stride and check if it is constant.
1877 const APInt *APStepVal;
1878 if (!match(Step, m_scev_APInt(APStepVal))) {
1879 LLVM_DEBUG({
1880 dbgs() << "LAA: Bad stride - Not a constant strided ";
1881 if (Ptr)
1882 dbgs() << *Ptr << " ";
1883 dbgs() << "SCEV: " << *AR << "\n";
1884 });
1885 return std::nullopt;
1886 }
1887
1888 const auto &DL = Lp->getHeader()->getDataLayout();
1889 TypeSize AllocSize = DL.getTypeAllocSize(AccessTy);
1890 int64_t Size = AllocSize.getFixedValue();
1891
1892 // Huge step value - give up.
1893 std::optional<int64_t> StepVal = APStepVal->trySExtValue();
1894 if (!StepVal)
1895 return std::nullopt;
1896
1897 // Strided access.
1898 return *StepVal % Size ? std::nullopt : std::make_optional(*StepVal / Size);
1899}
1900
1901/// Check whether \p AR is a non-wrapping AddRec. If \p Ptr is not nullptr, use
1902/// information from the IR pointer value to determine no-wrap. If \p Predicates
1903/// is not nullptr add no-wrap assumptions if needed.
1904static bool
1906 Type *AccessTy, const Loop *L, const DominatorTree &DT,
1907 std::optional<int64_t> Stride = std::nullopt,
1908 SmallVectorImpl<const SCEVPredicate *> *Predicates = nullptr) {
1909 // FIXME: This should probably only return true for NUW.
1910 if (any(AR->getNoWrapFlags()))
1911 return true;
1912
1913 // An nusw getelementptr that is an AddRec cannot wrap. If it would wrap,
1914 // the distance between the previously accessed location and the wrapped
1915 // location will be larger than half the pointer index type space. In that
1916 // case, the GEP would be poison and any memory access dependent on it would
1917 // be immediate UB when executed.
1919 GEP && GEP->hasNoUnsignedSignedWrap()) {
1920 // For the above reasoning to apply, the pointer must be dereferenced in
1921 // every iteration.
1922 if (L->getHeader() == L->getLoopLatch() ||
1923 any_of(GEP->users(), [L, &DT, GEP](User *U) {
1924 if (getLoadStorePointerOperand(U) != GEP)
1925 return false;
1926 BasicBlock *UserBB = cast<Instruction>(U)->getParent();
1927 if (!L->contains(UserBB))
1928 return false;
1929 return !LoopAccessInfo::blockNeedsPredication(UserBB, L, &DT);
1930 }))
1931 return true;
1932 }
1933
1934 if (!Stride)
1935 Stride = getStrideFromAddRec(AR, L, AccessTy, Ptr, PSE);
1936 if (Stride) {
1937 // If the null pointer is undefined, then a access sequence which would
1938 // otherwise access it can be assumed not to unsigned wrap. Note that this
1939 // assumes the object in memory is aligned to the natural alignment.
1940 unsigned AddrSpace = AR->getType()->getPointerAddressSpace();
1941 if (!NullPointerIsDefined(L->getHeader()->getParent(), AddrSpace) &&
1942 (Stride == 1 || Stride == -1))
1943 return true;
1944 }
1945
1946 ScalarEvolution &SE = *PSE.getSE();
1947 const SCEVPredicate *WrapPred =
1949 if (Ptr && Predicates) {
1950 Predicates->push_back(WrapPred);
1951 LLVM_DEBUG(dbgs() << "LAA: Pointer may wrap:\n"
1952 << "LAA: Pointer: " << *Ptr << "\n"
1953 << "LAA: SCEV: " << *AR << "\n"
1954 << "LAA: Added an overflow assumption\n");
1955 return true;
1956 }
1957
1958 // Without adding a new predicate, AR may still be known not to wrap if the
1959 // predicates of PSE already imply it, e.g. because a wrap predicate for AR
1960 // was added while analyzing the dependences of the loop.
1961 return PSE.getPredicate().implies(WrapPred, SE);
1962}
1963
1964static void visitPointers(Value *StartPtr, const Loop &InnermostLoop,
1965 function_ref<void(Value *)> AddPointer) {
1967 SmallVector<Value *> WorkList;
1968 WorkList.push_back(StartPtr);
1969
1970 while (!WorkList.empty()) {
1971 Value *Ptr = WorkList.pop_back_val();
1972 if (!Visited.insert(Ptr).second)
1973 continue;
1974 auto *PN = dyn_cast<PHINode>(Ptr);
1975 // SCEV does not look through non-header PHIs inside the loop. Such phis
1976 // can be analyzed by adding separate accesses for each incoming pointer
1977 // value.
1978 if (PN && InnermostLoop.contains(PN->getParent()) &&
1979 PN->getParent() != InnermostLoop.getHeader()) {
1980 llvm::append_range(WorkList, PN->incoming_values());
1981 } else
1982 AddPointer(Ptr);
1983 }
1984}
1985
1986// Walk back through the IR for a pointer, looking for a select like the
1987// following:
1988//
1989// %offset = select i1 %cmp, i64 %a, i64 %b
1990// %addr = getelementptr double, double* %base, i64 %offset
1991// %ld = load double, double* %addr, align 8
1992//
1993// We won't be able to form a single SCEVAddRecExpr from this since the
1994// address for each loop iteration depends on %cmp. We could potentially
1995// produce multiple valid SCEVAddRecExprs, though, and check all of them for
1996// memory safety/aliasing if needed.
1997//
1998// If we encounter some IR we don't yet handle, or something obviously fine
1999// like a constant, then we just add the SCEV for that term to the list passed
2000// in by the caller. If we have a node that may potentially yield a valid
2001// SCEVAddRecExpr then we decompose it into parts and build the SCEV terms
2002// ourselves before adding to the list.
2004 ScalarEvolution *SE, const Loop *L, Value *Ptr,
2006 unsigned Depth) {
2007 // If our Value is a SCEVAddRecExpr, loop invariant, not an instruction, or
2008 // we've exceeded our limit on recursion, just return whatever we have
2009 // regardless of whether it can be used for a forked pointer or not, along
2010 // with an indication of whether it might be a poison or undef value.
2011 const SCEV *Scev = SE->getSCEV(Ptr);
2012 if (isa<SCEVAddRecExpr>(Scev) || L->isLoopInvariant(Ptr) ||
2013 !isa<Instruction>(Ptr) || Depth == 0) {
2014 ScevList.emplace_back(Scev, !isGuaranteedNotToBeUndefOrPoison(Ptr));
2015 return;
2016 }
2017
2018 Depth--;
2019
2020 auto UndefPoisonCheck = [](PointerIntPair<const SCEV *, 1, bool> S) {
2021 return get<1>(S);
2022 };
2023
2024 auto GetBinOpExpr = [&SE](unsigned Opcode, const SCEV *L,
2025 const SCEV *R) -> const SCEV * {
2026 switch (Opcode) {
2027 case Instruction::Add:
2028 return SE->getAddExpr(L, R);
2029 case Instruction::Sub:
2030 return SE->getMinusSCEV(L, R);
2031 default:
2032 llvm_unreachable("Unexpected binary operator when walking ForkedPtrs");
2033 }
2034 };
2035
2037 unsigned Opcode = I->getOpcode();
2038 switch (Opcode) {
2039 case Instruction::GetElementPtr: {
2040 auto *GEP = cast<GetElementPtrInst>(I);
2041 Type *SourceTy = GEP->getSourceElementType();
2042 // We only handle base + single offset GEPs here for now.
2043 // Not dealing with preexisting gathers yet, so no vectors.
2044 if (I->getNumOperands() != 2 || SourceTy->isVectorTy()) {
2045 ScevList.emplace_back(Scev, !isGuaranteedNotToBeUndefOrPoison(GEP));
2046 break;
2047 }
2050 findForkedSCEVs(SE, L, I->getOperand(0), BaseScevs, Depth);
2051 findForkedSCEVs(SE, L, I->getOperand(1), OffsetScevs, Depth);
2052
2053 // See if we need to freeze our fork...
2054 bool NeedsFreeze = any_of(BaseScevs, UndefPoisonCheck) ||
2055 any_of(OffsetScevs, UndefPoisonCheck);
2056
2057 // Check that we only have a single fork, on either the base or the offset.
2058 // Copy the SCEV across for the one without a fork in order to generate
2059 // the full SCEV for both sides of the GEP.
2060 if (OffsetScevs.size() == 2 && BaseScevs.size() == 1)
2061 BaseScevs.push_back(BaseScevs[0]);
2062 else if (BaseScevs.size() == 2 && OffsetScevs.size() == 1)
2063 OffsetScevs.push_back(OffsetScevs[0]);
2064 else {
2065 ScevList.emplace_back(Scev, NeedsFreeze);
2066 break;
2067 }
2068
2069 Type *IntPtrTy = SE->getEffectiveSCEVType(GEP->getPointerOperandType());
2070
2071 // Find the size of the type being pointed to. We only have a single
2072 // index term (guarded above) so we don't need to index into arrays or
2073 // structures, just get the size of the scalar value.
2074 const SCEV *Size = SE->getSizeOfExpr(IntPtrTy, SourceTy);
2075
2076 for (auto [B, O] : zip(BaseScevs, OffsetScevs)) {
2077 const SCEV *Base = get<0>(B);
2078 const SCEV *Offset = get<0>(O);
2079
2080 // Scale up the offsets by the size of the type, then add to the bases.
2081 const SCEV *Scaled =
2083 ScevList.emplace_back(SE->getAddExpr(Base, Scaled), NeedsFreeze);
2084 }
2085 break;
2086 }
2087 case Instruction::Select: {
2089 // A select means we've found a forked pointer, but we currently only
2090 // support a single select per pointer so if there's another behind this
2091 // then we just bail out and return the generic SCEV.
2092 findForkedSCEVs(SE, L, I->getOperand(1), ChildScevs, Depth);
2093 findForkedSCEVs(SE, L, I->getOperand(2), ChildScevs, Depth);
2094 if (ChildScevs.size() == 2)
2095 append_range(ScevList, ChildScevs);
2096 else
2097 ScevList.emplace_back(Scev, !isGuaranteedNotToBeUndefOrPoison(Ptr));
2098 break;
2099 }
2100 case Instruction::PHI: {
2102 // A phi means we've found a forked pointer, but we currently only
2103 // support a single phi per pointer so if there's another behind this
2104 // then we just bail out and return the generic SCEV.
2105 if (I->getNumOperands() == 2) {
2106 findForkedSCEVs(SE, L, I->getOperand(0), ChildScevs, Depth);
2107 findForkedSCEVs(SE, L, I->getOperand(1), ChildScevs, Depth);
2108 }
2109 if (ChildScevs.size() == 2)
2110 append_range(ScevList, ChildScevs);
2111 else
2112 ScevList.emplace_back(Scev, !isGuaranteedNotToBeUndefOrPoison(Ptr));
2113 break;
2114 }
2115 case Instruction::Add:
2116 case Instruction::Sub: {
2119 findForkedSCEVs(SE, L, I->getOperand(0), LScevs, Depth);
2120 findForkedSCEVs(SE, L, I->getOperand(1), RScevs, Depth);
2121
2122 // See if we need to freeze our fork...
2123 bool NeedsFreeze =
2124 any_of(LScevs, UndefPoisonCheck) || any_of(RScevs, UndefPoisonCheck);
2125
2126 // Check that we only have a single fork, on either the left or right side.
2127 // Copy the SCEV across for the one without a fork in order to generate
2128 // the full SCEV for both sides of the BinOp.
2129 if (LScevs.size() == 2 && RScevs.size() == 1)
2130 RScevs.push_back(RScevs[0]);
2131 else if (RScevs.size() == 2 && LScevs.size() == 1)
2132 LScevs.push_back(LScevs[0]);
2133 else {
2134 ScevList.emplace_back(Scev, NeedsFreeze);
2135 break;
2136 }
2137
2138 for (auto [L, R] : zip(LScevs, RScevs))
2139 ScevList.emplace_back(GetBinOpExpr(Opcode, get<0>(L), get<0>(R)),
2140 NeedsFreeze);
2141 break;
2142 }
2143 default:
2144 // Just return the current SCEV if we haven't handled the instruction yet.
2145 LLVM_DEBUG(dbgs() << "ForkedPtr unhandled instruction: " << *I << "\n");
2146 ScevList.emplace_back(Scev, !isGuaranteedNotToBeUndefOrPoison(Ptr));
2147 break;
2148 }
2149}
2150
2151bool AccessAnalysis::createCheckForAccess(RuntimePointerChecking &RtCheck,
2152 MemAccessInfo Access, Type *AccessTy,
2153 const SymbolicStrideMap &StridesMap,
2155 Loop *TheLoop, unsigned &RunningDepId,
2156 unsigned ASId, bool Assume) {
2157 Value *Ptr = Access.getPointer();
2158 ScalarEvolution *SE = PSE.getSE();
2159 const DataLayout &DL = TheLoop->getHeader()->getDataLayout();
2160 assert(SE->isSCEVable(Ptr->getType()) && "Value is not SCEVable!");
2161
2163 findForkedSCEVs(SE, TheLoop, Ptr, RTCheckPtrs, MaxForkedSCEVDepth);
2164 assert(!RTCheckPtrs.empty() &&
2165 "Must have some runtime-check pointer candidates");
2166
2167 // RTCheckPtrs must have size 2 if there are forked pointers. Otherwise, there
2168 // are no forked pointers; replaceSymbolicStridesSCEV in this case.
2169 auto IsLoopInvariantOrAR =
2170 [&SE, &TheLoop](const PointerIntPair<const SCEV *, 1, bool> &P) {
2171 return SE->isLoopInvariant(P.getPointer(), TheLoop) ||
2172 isa<SCEVAddRecExpr>(P.getPointer());
2173 };
2174 if (RTCheckPtrs.size() == 2 && all_of(RTCheckPtrs, IsLoopInvariantOrAR)) {
2175 LLVM_DEBUG(dbgs() << "LAA: Found forked pointer: " << *Ptr << "\n";
2176 for (const auto &[Idx, Q] : enumerate(RTCheckPtrs)) dbgs()
2177 << "\t(" << Idx << ") " << *Q.getPointer() << "\n");
2178 } else {
2179 RTCheckPtrs = {
2180 {replaceSymbolicStrideSCEV(PSE, TheLoop, StridesMap, Ptr), false}};
2181 }
2182
2183 /// Check whether all pointers can participate in a runtime bounds check. They
2184 /// must either be invariant or non-wrapping affine AddRecs.
2186 for (auto &P : RTCheckPtrs) {
2187 // The bounds for loop-invariant pointer is trivial.
2188 if (SE->isLoopInvariant(P.getPointer(), TheLoop))
2189 continue;
2190
2191 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(P.getPointer());
2192 if (!AR && Assume)
2193 AR = PSE.getAsAddRec(Ptr, &Predicates);
2194 if (!AR || !AR->isAffine()) {
2195 // Check if bounds for non-affine monotonic expressions can be formed.
2196 const SCEV *EltSizeSCEV = SE->getStoreSizeOfExpr(
2197 DL.getIndexType(P.getPointer()->getType()), AccessTy);
2198 if (!Assume ||
2199 !getNonAffineMonotonicBounds(TheLoop, P.getPointer(), EltSizeSCEV, SE)
2200 .first)
2201 return false;
2202 continue;
2203 }
2204
2205 // If there's only one option for Ptr, commit the predicates collected by
2206 // getAsAddRec and look Ptr up again afterwards: the lookup below reads the
2207 // assumptions back from PSE, so they need to be committed first.
2208 if (RTCheckPtrs.size() == 1) {
2209 PSE.addPredicates(Predicates);
2210 Predicates.clear();
2211 if (auto *StrideAR = dyn_cast<SCEVAddRecExpr>(
2212 replaceSymbolicStrideSCEV(PSE, TheLoop, StridesMap, Ptr)))
2213 AR = StrideAR;
2214 P.setPointer(AR);
2215 }
2216
2217 if (!isNoWrap(PSE, AR, RTCheckPtrs.size() == 1 ? Ptr : nullptr, AccessTy,
2218 TheLoop, DT, /*Stride=*/std::nullopt,
2219 Assume ? &Predicates : nullptr))
2220 return false;
2221 }
2222 PSE.addPredicates(Predicates);
2223
2224 // Remember the number of pointers inserted so far, to remove the pointers of
2225 // this access again if the bounds of any of them cannot be computed, to avoid
2226 // partial inserts.
2227 unsigned NumPointers = RtCheck.Pointers.size();
2228 for (const auto &[PtrExpr, NeedsFreeze] : RTCheckPtrs) {
2229 // The id of the dependence set.
2230 unsigned DepId;
2231
2232 if (DepCands.contains(Access)) {
2233 Value *Leader = DepCands.getLeaderValue(Access).getPointer();
2234 unsigned &LeaderId = DepSetId[Leader];
2235 if (!LeaderId)
2236 LeaderId = RunningDepId++;
2237 DepId = LeaderId;
2238 } else
2239 // Each access has its own dependence set.
2240 DepId = RunningDepId++;
2241
2242 bool IsWrite = Access.getInt();
2243 if (!RtCheck.insert(TheLoop, Ptr, PtrExpr, AccessTy, IsWrite, DepId, ASId,
2244 PSE, NeedsFreeze,
2245 /*IsForked=*/RTCheckPtrs.size() > 1)) {
2246 RtCheck.Pointers.truncate(NumPointers);
2247 return false;
2248 }
2249 LLVM_DEBUG(dbgs() << "LAA: Found a runtime check ptr:" << *Ptr << '\n');
2250 }
2251
2252 return true;
2253}
2254
2255bool AccessAnalysis::canCheckPtrAtRT(RuntimePointerChecking &RtCheck,
2256 Loop *TheLoop,
2257 const SymbolicStrideMap &StridesMap,
2258 Value *&UncomputablePtr, bool AllowPartial,
2259 const MemoryDepChecker &DepChecker) {
2260 // Find pointers with computable bounds. We are going to use this information
2261 // to place a runtime bound check.
2262 bool CanDoRT = true;
2263
2264 bool MayNeedRTCheck = false;
2265 if (!IsRTCheckAnalysisNeeded) return true;
2266
2267 if (auto *Deps = DepChecker.getDependences()) {
2268 // If there are unknown dependences, this means runtime checks are needed to
2269 // ensure there's no overlap between accesses to the same underlying object.
2270 // Remove the equivalence classes containing both source and destination
2271 // accesses from DepCands. This ensures runtime checks will be generated
2272 // between those accesses and prevents them from being grouped together.
2273 for (const auto &Dep : *Deps) {
2274 if (Dep.Type != MemoryDepChecker::Dependence::Unknown) {
2277 "Should only skip safe dependences");
2278 continue;
2279 }
2280 Instruction *Src = Dep.getSource(DepChecker);
2281 Instruction *Dst = Dep.getDestination(DepChecker);
2282 DepCands.eraseClass({getPointerOperand(Src), Src->mayWriteToMemory()});
2283 DepCands.eraseClass({getPointerOperand(Dst), Dst->mayWriteToMemory()});
2284 }
2285 } else {
2286 CheckDeps.clear();
2287 DepCands = {};
2288 }
2289
2290 // We assign a consecutive id to access from different alias sets.
2291 // Accesses between different groups doesn't need to be checked.
2292 unsigned ASId = 0;
2293 for (const auto &AS : AST) {
2294 int NumReadPtrChecks = 0;
2295 int NumWritePtrChecks = 0;
2296 bool CanDoAliasSetRT = true;
2297 ++ASId;
2298 auto ASPointers = AS.getPointers();
2299
2300 // We assign consecutive id to access from different dependence sets.
2301 // Accesses within the same set don't need a runtime check.
2302 unsigned RunningDepId = 1;
2304
2306
2307 // First, count how many write and read accesses are in the alias set. Also
2308 // collect MemAccessInfos for later.
2310 for (const Value *ConstPtr : ASPointers) {
2311 Value *Ptr = const_cast<Value *>(ConstPtr);
2312 bool IsWrite = Accesses.contains(MemAccessInfo(Ptr, true));
2313 if (IsWrite)
2314 ++NumWritePtrChecks;
2315 else
2316 ++NumReadPtrChecks;
2317 AccessInfos.emplace_back(Ptr, IsWrite);
2318 }
2319
2320 // We do not need runtime checks for this alias set, if there are no writes
2321 // or a single write and no reads.
2322 if (NumWritePtrChecks == 0 ||
2323 (NumWritePtrChecks == 1 && NumReadPtrChecks == 0)) {
2324 assert((ASPointers.size() <= 1 ||
2325 all_of(ASPointers,
2326 [this](const Value *Ptr) {
2327 MemAccessInfo AccessWrite(const_cast<Value *>(Ptr),
2328 true);
2329 return !DepCands.contains(AccessWrite);
2330 })) &&
2331 "Can only skip updating CanDoRT below, if all entries in AS "
2332 "are reads or there is at most 1 entry");
2333 continue;
2334 }
2335
2336 const DataLayout &DL = TheLoop->getHeader()->getDataLayout();
2337 auto StoreSize = [&DL](Type *Ty) { return DL.getTypeStoreSize(Ty); };
2338
2339 for (auto &Access : AccessInfos) {
2340 // A written pointer is only checked via its write access, so its bounds
2341 // must also cover the types it is loaded with.
2342 SmallSetVector<Type *, 1> AccessTys = Accesses.lookup(Access);
2343 if (Access.getInt()) {
2344 auto ReadIt = Accesses.find(MemAccessInfo(Access.getPointer(), false));
2345 if (ReadIt != Accesses.end())
2346 AccessTys.insert_range(ReadIt->second);
2347 }
2348
2349 // The widest fixed and scalable types cover all accesses of their kind.
2350 for (bool Scalable : {false, true}) {
2351 auto HasMatchingSizeKind = [&](Type *Ty) {
2352 return StoreSize(Ty).isScalable() == Scalable;
2353 };
2354 auto Tys = make_filter_range(AccessTys, std::ref(HasMatchingSizeKind));
2355 if (Tys.empty())
2356 continue;
2357
2358 Type *AccessTy = *max_element(Tys, [&](Type *A, Type *B) {
2359 return TypeSize::isKnownLT(StoreSize(A), StoreSize(B));
2360 });
2361
2362 if (!createCheckForAccess(RtCheck, Access, AccessTy, StridesMap,
2363 DepSetId, TheLoop, RunningDepId, ASId,
2364 /*Assume=*/false)) {
2365 LLVM_DEBUG(dbgs() << "LAA: Can't find bounds for ptr:"
2366 << *Access.getPointer() << '\n');
2367 Retries.emplace_back(Access, AccessTy);
2368 CanDoAliasSetRT = false;
2369 }
2370 }
2371 }
2372
2373 // Note that this function computes CanDoRT and MayNeedRTCheck
2374 // independently. For example CanDoRT=false, MayNeedRTCheck=false means that
2375 // we have a pointer for which we couldn't find the bounds but we don't
2376 // actually need to emit any checks so it does not matter.
2377 //
2378 // We need runtime checks for this alias set, if there are at least 2
2379 // dependence sets (in which case RunningDepId > 2) or if we need to re-try
2380 // any bound checks (because in that case the number of dependence sets is
2381 // incomplete).
2382 bool NeedsAliasSetRTCheck = RunningDepId > 2 || !Retries.empty();
2383
2384 // We need to perform run-time alias checks, but some pointers had bounds
2385 // that couldn't be checked.
2386 if (NeedsAliasSetRTCheck && !CanDoAliasSetRT) {
2387 // Reset the CanDoSetRt flag and retry all accesses that have failed.
2388 // We know that we need these checks, so we can now be more aggressive
2389 // and add further checks if required (overflow checks).
2390 CanDoAliasSetRT = true;
2391 for (const auto &[Access, AccessTy] : Retries) {
2392 if (!createCheckForAccess(RtCheck, Access, AccessTy, StridesMap,
2393 DepSetId, TheLoop, RunningDepId, ASId,
2394 /*Assume=*/true)) {
2395 CanDoAliasSetRT = false;
2396 UncomputablePtr = Access.getPointer();
2397 if (!AllowPartial)
2398 break;
2399 }
2400 }
2401 }
2402
2403 CanDoRT &= CanDoAliasSetRT;
2404 MayNeedRTCheck |= NeedsAliasSetRTCheck;
2405 ++ASId;
2406 }
2407
2408 // If the pointers that we would use for the bounds comparison have different
2409 // address spaces, assume the values aren't directly comparable, so we can't
2410 // use them for the runtime check. We also have to assume they could
2411 // overlap. In the future there should be metadata for whether address spaces
2412 // are disjoint.
2413 unsigned NumPointers = RtCheck.Pointers.size();
2414 for (unsigned i = 0; i < NumPointers; ++i) {
2415 for (unsigned j = i + 1; j < NumPointers; ++j) {
2416 // Only need to check pointers between two different dependency sets.
2417 if (RtCheck.Pointers[i].DependencySetId ==
2418 RtCheck.Pointers[j].DependencySetId)
2419 continue;
2420 // Only need to check pointers in the same alias set.
2421 if (RtCheck.Pointers[i].AliasSetId != RtCheck.Pointers[j].AliasSetId)
2422 continue;
2423
2424 Value *PtrI = RtCheck.Pointers[i].PointerValue;
2425 Value *PtrJ = RtCheck.Pointers[j].PointerValue;
2426
2427 unsigned ASi = PtrI->getType()->getPointerAddressSpace();
2428 unsigned ASj = PtrJ->getType()->getPointerAddressSpace();
2429 if (ASi != ASj) {
2430 LLVM_DEBUG(
2431 dbgs() << "LAA: Runtime check would require comparison between"
2432 " different address spaces\n");
2433 return false;
2434 }
2435 }
2436 }
2437
2438 if (MayNeedRTCheck && (CanDoRT || AllowPartial))
2439 RtCheck.generateChecks(DepCands);
2440
2441 LLVM_DEBUG(dbgs() << "LAA: We need to do " << RtCheck.getNumberOfChecks()
2442 << " pointer comparisons.\n");
2443
2444 // If we can do run-time checks, but there are no checks, no runtime checks
2445 // are needed. This can happen when all pointers point to the same underlying
2446 // object for example.
2447 RtCheck.Need = CanDoRT ? RtCheck.getNumberOfChecks() != 0 : MayNeedRTCheck;
2448
2449 bool CanDoRTIfNeeded = !RtCheck.Need || CanDoRT;
2450 assert(CanDoRTIfNeeded == (CanDoRT || !MayNeedRTCheck) &&
2451 "CanDoRTIfNeeded depends on RtCheck.Need");
2452 if (!CanDoRTIfNeeded && !AllowPartial)
2453 RtCheck.reset();
2454 return CanDoRTIfNeeded;
2455}
2456
2457void AccessAnalysis::buildDependenceSets() {
2458 // We process the set twice: first we process read-write pointers, last we
2459 // process read-only pointers. This allows us to skip dependence tests for
2460 // read-only pointers.
2461
2462 LLVM_DEBUG(dbgs() << "LAA: Processing memory accesses...\n");
2463 LLVM_DEBUG(dbgs() << " AST: "; AST.dump());
2464 LLVM_DEBUG(dbgs() << "LAA: Accesses(" << Accesses.size() << "):\n");
2465 LLVM_DEBUG({
2466 for (const auto &[A, _] : Accesses)
2467 dbgs() << "\t" << *A.getPointer() << " ("
2468 << (A.getInt()
2469 ? "write"
2470 : (ReadOnlyPtr.contains(A.getPointer()) ? "read-only"
2471 : "read"))
2472 << ")\n";
2473 });
2474
2475 // The AliasSetTracker has nicely partitioned our pointers by metadata
2476 // compatibility and potential for underlying-object overlap. As a result, we
2477 // only need to check for potential pointer dependencies within each alias
2478 // set.
2479 for (const auto &AS : AST) {
2480 bool AliasSetHasWrite = false;
2481
2482 // Map of (pointer to underlying objects, accessed address space) to last
2483 // access encountered.
2484 using UnderlyingObjToAccessMap =
2486 UnderlyingObjToAccessMap ObjToLastAccess;
2487
2488 // Set of access to check after all writes have been processed.
2489 PtrAccessMap DeferredAccesses;
2490
2491 // Iterate over each alias set twice, once to process read/write pointers,
2492 // and then to process read-only pointers.
2493
2494 auto ProcessAccesses = [&](bool UseDeferred) {
2495 PtrAccessMap &S = UseDeferred ? DeferredAccesses : Accesses;
2496
2497 // Note that both the alias-set tracker and the alias sets themselves used
2498 // ordered collections internally and so the iteration order here is
2499 // deterministic.
2500 for (const Value *ConstPtr : AS.getPointers()) {
2501 Value *Ptr = const_cast<Value *>(ConstPtr);
2502
2503 // For a single memory access in AliasSetTracker, Accesses may contain
2504 // both read and write, and they both need to be handled for CheckDeps.
2505 for (auto [AccessPtr, IsWrite] : S.keys()) {
2506 if (AccessPtr != Ptr)
2507 continue;
2508
2509 // If we're using the deferred access set, then it contains only
2510 // reads.
2511 bool IsReadOnlyPtr = ReadOnlyPtr.contains(Ptr) && !IsWrite;
2512 if (UseDeferred && !IsReadOnlyPtr)
2513 continue;
2514 // Otherwise, the pointer must be in the PtrAccessSet, either as a
2515 // read or a write.
2516 assert(((IsReadOnlyPtr && UseDeferred) || IsWrite ||
2517 S.contains(MemAccessInfo(Ptr, false))) &&
2518 "Alias-set pointer not in the access set?");
2519
2520 MemAccessInfo Access(Ptr, IsWrite);
2521 DepCands.insert(Access);
2522
2523 // Memorize read-only pointers for later processing and skip them in
2524 // the first round (they need to be checked after we have seen all
2525 // write pointers). Note: we also mark pointer that are not
2526 // consecutive as "read-only" pointers (so that we check
2527 // "a[b[i]] +="). Hence, we need the second check for "!IsWrite".
2528 if (!UseDeferred && IsReadOnlyPtr) {
2529 // We only use the pointer keys, the types vector values don't
2530 // matter.
2531 DeferredAccesses.insert({Access, {}});
2532 continue;
2533 }
2534
2535 // If this is a write - check other reads and writes for conflicts. If
2536 // this is a read only check other writes for conflicts (but only if
2537 // there is no other write to the ptr - this is an optimization to
2538 // catch "a[i] = a[i] + " without having to do a dependence check).
2539 if ((IsWrite || IsReadOnlyPtr) && AliasSetHasWrite) {
2540 CheckDeps.push_back(Access);
2541 IsRTCheckAnalysisNeeded = true;
2542 }
2543
2544 if (IsWrite)
2545 AliasSetHasWrite = true;
2546
2547 // Create sets of pointers connected by a shared alias set and
2548 // underlying object.
2549 SmallVector<const Value *, 16> &UOs = UnderlyingObjects[Ptr];
2550 UOs = {};
2551 ::getUnderlyingObjects(Ptr, UOs, LI);
2553 << "Underlying objects for pointer " << *Ptr << "\n");
2554 for (const Value *UnderlyingObj : UOs) {
2555 // nullptr never alias, don't join sets for pointer that have "null"
2556 // in their UnderlyingObjects list.
2557 if (isa<ConstantPointerNull>(UnderlyingObj) &&
2559 TheLoop->getHeader()->getParent(),
2560 UnderlyingObj->getType()->getPointerAddressSpace()))
2561 continue;
2562
2563 auto [It, Inserted] = ObjToLastAccess.try_emplace(
2564 {UnderlyingObj,
2565 cast<PointerType>(Ptr->getType())->getAddressSpace()},
2566 Access);
2567 if (!Inserted) {
2568 DepCands.unionSets(Access, It->second);
2569 It->second = Access;
2570 }
2571
2572 LLVM_DEBUG(dbgs() << " " << *UnderlyingObj << "\n");
2573 }
2574 }
2575 }
2576 };
2577
2578 ProcessAccesses(false);
2579 ProcessAccesses(true);
2580 }
2581}
2582
2583/// Check whether the access through \p Ptr has a constant stride.
2584std::optional<int64_t>
2586 const Loop *Lp, const DominatorTree &DT,
2587 const SymbolicStrideMap &StridesMap, bool ShouldCheckWrap,
2589 const SCEV *PtrScev =
2590 replaceSymbolicStrideSCEV(PSE, Lp, StridesMap, Ptr, Predicates);
2591 if (PSE.getSE()->isLoopInvariant(PtrScev, Lp))
2592 return 0;
2593
2594 assert(Ptr->getType()->isPointerTy() && "Unexpected non-ptr");
2595
2596 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
2597 if (Predicates && !AR) {
2598 AR = PSE.getSE()->convertSCEVToAddRecWithPredicates(PtrScev, Lp,
2599 *Predicates);
2600 }
2601
2602 if (!AR) {
2603 LLVM_DEBUG(dbgs() << "LAA: Bad stride - Not an AddRecExpr pointer " << *Ptr
2604 << " SCEV: " << *PtrScev << "\n");
2605 return std::nullopt;
2606 }
2607
2608 std::optional<int64_t> Stride =
2609 getStrideFromAddRec(AR, Lp, AccessTy, Ptr, PSE);
2610 if (!ShouldCheckWrap || !Stride)
2611 return Stride;
2612
2613 if (isNoWrap(PSE, AR, Ptr, AccessTy, Lp, DT, Stride, Predicates))
2614 return Stride;
2615
2616 LLVM_DEBUG(
2617 dbgs() << "LAA: Bad stride - Pointer may wrap in the address space "
2618 << *Ptr << " SCEV: " << *AR << "\n");
2619 return std::nullopt;
2620}
2621
2622std::optional<int64_t> llvm::getPointersDiff(Type *ElemTyA, Value *PtrA,
2623 Type *ElemTyB, Value *PtrB,
2624 const DataLayout &DL,
2625 ScalarEvolution &SE,
2626 bool StrictCheck, bool CheckType) {
2627 assert(PtrA && PtrB && "Expected non-nullptr pointers.");
2628
2629 // Make sure that A and B are different pointers.
2630 if (PtrA == PtrB)
2631 return 0;
2632
2633 // Make sure that the element types are the same if required.
2634 if (CheckType && ElemTyA != ElemTyB)
2635 return std::nullopt;
2636
2637 unsigned ASA = PtrA->getType()->getPointerAddressSpace();
2638 unsigned ASB = PtrB->getType()->getPointerAddressSpace();
2639
2640 // Check that the address spaces match.
2641 if (ASA != ASB)
2642 return std::nullopt;
2643 unsigned IdxWidth = DL.getIndexSizeInBits(ASA);
2644
2645 APInt OffsetA(IdxWidth, 0), OffsetB(IdxWidth, 0);
2646 const Value *PtrA1 = PtrA->stripAndAccumulateConstantOffsets(
2647 DL, OffsetA, /*AllowNonInbounds=*/true);
2648 const Value *PtrB1 = PtrB->stripAndAccumulateConstantOffsets(
2649 DL, OffsetB, /*AllowNonInbounds=*/true);
2650
2651 std::optional<int64_t> Val;
2652 if (PtrA1 == PtrB1) {
2653 // Retrieve the address space again as pointer stripping now tracks through
2654 // `addrspacecast`.
2655 ASA = cast<PointerType>(PtrA1->getType())->getAddressSpace();
2656 ASB = cast<PointerType>(PtrB1->getType())->getAddressSpace();
2657 // Check that the address spaces match and that the pointers are valid.
2658 if (ASA != ASB)
2659 return std::nullopt;
2660
2661 IdxWidth = DL.getIndexSizeInBits(ASA);
2662 OffsetA = OffsetA.sextOrTrunc(IdxWidth);
2663 OffsetB = OffsetB.sextOrTrunc(IdxWidth);
2664
2665 OffsetB -= OffsetA;
2666 Val = OffsetB.trySExtValue();
2667 } else {
2668 // Otherwise compute the distance with SCEV between the base pointers.
2669 const SCEV *PtrSCEVA = SE.getSCEV(PtrA);
2670 const SCEV *PtrSCEVB = SE.getSCEV(PtrB);
2671 std::optional<APInt> Diff =
2672 SE.computeConstantDifference(PtrSCEVB, PtrSCEVA);
2673 if (!Diff)
2674 return std::nullopt;
2675 Val = Diff->trySExtValue();
2676 }
2677
2678 if (!Val)
2679 return std::nullopt;
2680
2681 int64_t Size = DL.getTypeStoreSize(ElemTyA);
2682 int64_t Dist = *Val / Size;
2683
2684 // Ensure that the calculated distance matches the type-based one after all
2685 // the bitcasts removal in the provided pointers.
2686 if (!StrictCheck || Dist * Size == Val)
2687 return Dist;
2688 return std::nullopt;
2689}
2690
2692 const DataLayout &DL, ScalarEvolution &SE,
2693 SmallVectorImpl<unsigned> &SortedIndices) {
2695 VL, [](const Value *V) { return V->getType()->isPointerTy(); }) &&
2696 "Expected list of pointer operands.");
2697 // Walk over the pointers, and map each of them to an offset relative to
2698 // first pointer in the array.
2699 Value *Ptr0 = VL[0];
2700
2701 using DistOrdPair = std::pair<int64_t, unsigned>;
2702 auto Compare = llvm::less_first();
2703 std::set<DistOrdPair, decltype(Compare)> Offsets(Compare);
2704 Offsets.emplace(0, 0);
2705 bool IsConsecutive = true;
2706 for (auto [Idx, Ptr] : drop_begin(enumerate(VL))) {
2707 std::optional<int64_t> Diff =
2708 getPointersDiff(ElemTy, Ptr0, ElemTy, Ptr, DL, SE,
2709 /*StrictCheck=*/true);
2710 if (!Diff)
2711 return false;
2712
2713 // Check if the pointer with the same offset is found.
2714 int64_t Offset = *Diff;
2715 auto [It, IsInserted] = Offsets.emplace(Offset, Idx);
2716 if (!IsInserted)
2717 return false;
2718 // Consecutive order if the inserted element is the last one.
2719 IsConsecutive &= std::next(It) == Offsets.end();
2720 }
2721 SortedIndices.clear();
2722 if (!IsConsecutive) {
2723 // Fill SortedIndices array only if it is non-consecutive.
2724 SortedIndices.resize(VL.size());
2725 for (auto [Idx, Off] : enumerate(Offsets))
2726 SortedIndices[Idx] = Off.second;
2727 }
2728 return true;
2729}
2730
2731/// Returns true if the memory operations \p A and \p B are consecutive.
2733 ScalarEvolution &SE, bool CheckType) {
2736 if (!PtrA || !PtrB)
2737 return false;
2738 Type *ElemTyA = getLoadStoreType(A);
2739 Type *ElemTyB = getLoadStoreType(B);
2740 std::optional<int64_t> Diff =
2741 getPointersDiff(ElemTyA, PtrA, ElemTyB, PtrB, DL, SE,
2742 /*StrictCheck=*/true, CheckType);
2743 return Diff == 1;
2744}
2745
2747 visitPointers(SI->getPointerOperand(), *InnermostLoop,
2748 [this, SI](Value *Ptr) {
2749 Accesses[MemAccessInfo(Ptr, true)].push_back(AccessIdx);
2750 InstMap.push_back(SI);
2751 ++AccessIdx;
2752 });
2753}
2754
2756 visitPointers(LI->getPointerOperand(), *InnermostLoop,
2757 [this, LI](Value *Ptr) {
2758 Accesses[MemAccessInfo(Ptr, false)].push_back(AccessIdx);
2759 InstMap.push_back(LI);
2760 ++AccessIdx;
2761 });
2762}
2763
2783
2785 switch (Type) {
2786 case NoDep:
2787 case Forward:
2789 case Unknown:
2790 case IndirectUnsafe:
2791 case InvariantUnsafe:
2792 return false;
2793
2795 case Backward:
2797 return true;
2798 }
2799 llvm_unreachable("unexpected DepType!");
2800}
2801
2806
2808 switch (Type) {
2809 case Forward:
2811 return true;
2812
2813 case NoDep:
2814 case Unknown:
2816 case Backward:
2818 case IndirectUnsafe:
2819 case InvariantUnsafe:
2820 return false;
2821 }
2822 llvm_unreachable("unexpected DepType!");
2823}
2824
2825bool MemoryDepChecker::couldPreventStoreLoadForward(uint64_t Distance,
2826 uint64_t TypeByteSize,
2827 unsigned CommonStride) {
2828 // If loads occur at a distance that is not a multiple of a feasible vector
2829 // factor store-load forwarding does not take place.
2830 // Positive dependences might cause troubles because vectorizing them might
2831 // prevent store-load forwarding making vectorized code run a lot slower.
2832 // a[i] = a[i-3] ^ a[i-8];
2833 // The stores to a[i:i+1] don't align with the stores to a[i-3:i-2] and
2834 // hence on your typical architecture store-load forwarding does not take
2835 // place. Vectorizing in such cases does not make sense.
2836 // Store-load forwarding distance.
2837
2838 // Maximum vector factor.
2839 uint64_t MaxVFWithoutSLForwardIssuesPowerOf2 =
2840 std::min(VectorizerParams::MaxVectorWidth * TypeByteSize,
2841 MaxStoreLoadForwardSafeDistanceInBits);
2842
2843 // Compute the smallest VF at which the store and load would be misaligned
2844 // and recent enough to still be in the store buffer.
2845 for (uint64_t VF = 2 * TypeByteSize;
2846 VF <= MaxVFWithoutSLForwardIssuesPowerOf2; VF *= 2) {
2847 if (isStoreLoadForwardingConflict(Distance, VF, TypeByteSize, VF)) {
2848 MaxVFWithoutSLForwardIssuesPowerOf2 = (VF >> 1);
2849 break;
2850 }
2851 }
2852
2853 if (MaxVFWithoutSLForwardIssuesPowerOf2 < 2 * TypeByteSize) {
2854 LLVM_DEBUG(
2855 dbgs() << "LAA: Distance " << Distance
2856 << " that could cause a store-load forwarding conflict\n");
2857 return true;
2858 }
2859
2860 if (CommonStride &&
2861 MaxVFWithoutSLForwardIssuesPowerOf2 <
2862 MaxStoreLoadForwardSafeDistanceInBits &&
2863 MaxVFWithoutSLForwardIssuesPowerOf2 !=
2864 VectorizerParams::MaxVectorWidth * TypeByteSize) {
2865 uint64_t MaxVF =
2866 bit_floor(MaxVFWithoutSLForwardIssuesPowerOf2 / CommonStride);
2867 uint64_t MaxVFInBits = MaxVF * TypeByteSize * 8;
2868 MaxStoreLoadForwardSafeDistanceInBits =
2869 std::min(MaxStoreLoadForwardSafeDistanceInBits, MaxVFInBits);
2870
2871 if (MaxVF < 2) {
2872 LLVM_DEBUG(
2873 dbgs() << "LAA: strided access with Distance " << Distance
2874 << " that could cause a store-load forwarding conflict\n");
2875 return true;
2876 }
2877 }
2878 return false;
2879}
2880
2881void MemoryDepChecker::mergeInStatus(VectorizationSafetyStatus S) {
2882 if (Status < S)
2883 Status = S;
2884}
2885
2886/// Given a dependence-distance \p Dist between two memory accesses, that have
2887/// strides in the same direction whose absolute value of the maximum stride is
2888/// given in \p MaxStride, in a loop whose maximum backedge taken count is \p
2889/// MaxBTC, check if it is possible to prove statically that the dependence
2890/// distance is larger than the range that the accesses will travel through the
2891/// execution of the loop. If so, return true; false otherwise. This is useful
2892/// for example in loops such as the following (PR31098):
2893///
2894/// for (i = 0; i < D; ++i) {
2895/// = out[i];
2896/// out[i+D] =
2897/// }
2898static bool isSafeDependenceDistance(ScalarEvolution &SE, const SCEV &MaxBTC,
2899 const SCEV &Dist, uint64_t MaxStride) {
2900
2901 // If we can prove that
2902 // (**) |Dist| > MaxBTC * Step
2903 // where Step is the absolute stride of the memory accesses in bytes,
2904 // then there is no dependence.
2905 //
2906 // Rationale:
2907 // We basically want to check if the absolute distance (|Dist/Step|)
2908 // is >= the loop iteration count (or > MaxBTC).
2909 // This is equivalent to the Strong SIV Test (Practical Dependence Testing,
2910 // Section 4.2.1); Note, that for vectorization it is sufficient to prove
2911 // that the dependence distance is >= VF; This is checked elsewhere.
2912 // But in some cases we can prune dependence distances early, and
2913 // even before selecting the VF, and without a runtime test, by comparing
2914 // the distance against the loop iteration count. Since the vectorized code
2915 // will be executed only if LoopCount >= VF, proving distance >= LoopCount
2916 // also guarantees that distance >= VF.
2917 //
2918 // The dependence distance can be positive/negative, so we sign-extend Dist.
2919 // MaxBTC is non-negative, so we zero-extended to the wider type before
2920 // computing MaxBTC * Step.
2921 // FIXME: The checks below can still wrap if MaxBTC * MaxStride does not fit
2922 // in WideTy with two bits to spare, e.g. for an unbounded i64 MaxBTC.
2923 Type *WideTy = SE.getWiderType(Dist.getType(), MaxBTC.getType());
2924 const SCEV *Step = SE.getConstant(WideTy, MaxStride);
2925 const SCEV *CastedDist = SE.getNoopOrSignExtend(&Dist, WideTy);
2926 const SCEV *CastedProduct =
2927 SE.getMulExpr(SE.getNoopOrZeroExtend(&MaxBTC, WideTy), Step);
2928
2929 // Is Dist - (MaxBTC * Step) > 0 ?
2930 // (If so, then we have proven (**) because |Dist| >= Dist)
2931 const SCEV *Minus = SE.getMinusSCEV(CastedDist, CastedProduct);
2932 if (SE.isKnownPositive(Minus))
2933 return true;
2934
2935 // Second try: Is -Dist - (MaxBTC * Step) > 0 ?
2936 // (If so, then we have proven (**) because |Dist| >= -1*Dist)
2937 const SCEV *NegDist = SE.getNegativeSCEV(CastedDist);
2938 Minus = SE.getMinusSCEV(NegDist, CastedProduct);
2939 return SE.isKnownPositive(Minus);
2940}
2941
2942/// Check the dependence for two accesses with the same stride \p Stride.
2943/// \p Distance is the positive distance in bytes, and \p TypeByteSize is type
2944/// size in bytes.
2945///
2946/// \returns true if they are independent.
2948 uint64_t TypeByteSize) {
2949 assert(Stride > 1 && "The stride must be greater than 1");
2950 assert(TypeByteSize > 0 && "The type size in byte must be non-zero");
2951 assert(Distance > 0 && "The distance must be non-zero");
2952
2953 // Skip if the distance is not multiple of type byte size.
2954 if (Distance % TypeByteSize)
2955 return false;
2956
2957 // No dependence if the distance is not multiple of the stride.
2958 // E.g.
2959 // for (i = 0; i < 1024 ; i += 4)
2960 // A[i+2] = A[i] + 1;
2961 //
2962 // Two accesses in memory (distance is 2, stride is 4):
2963 // | A[0] | | | | A[4] | | | |
2964 // | | | A[2] | | | | A[6] | |
2965 //
2966 // E.g.
2967 // for (i = 0; i < 1024 ; i += 3)
2968 // A[i+4] = A[i] + 1;
2969 //
2970 // Two accesses in memory (distance is 4, stride is 3):
2971 // | A[0] | | | A[3] | | | A[6] | | |
2972 // | | | | | A[4] | | | A[7] | |
2973 return Distance % Stride;
2974}
2975
2976bool MemoryDepChecker::areAccessesCompletelyBeforeOrAfter(const SCEV *Src,
2977 Type *SrcTy,
2978 const SCEV *Sink,
2979 Type *SinkTy) {
2980 const SCEV *BTC = PSE.getBackedgeTakenCount();
2981 const SCEV *SymbolicMaxBTC = PSE.getSymbolicMaxBackedgeTakenCount();
2982 ScalarEvolution &SE = *PSE.getSE();
2983 const auto &[SrcStart_, SrcEnd_] =
2984 getStartAndEndForAccess(InnermostLoop, Src, SrcTy, BTC, SymbolicMaxBTC,
2985 &SE, &PointerBounds, DT, AC, LoopGuards);
2986 if (isa<SCEVCouldNotCompute>(SrcStart_) || isa<SCEVCouldNotCompute>(SrcEnd_))
2987 return false;
2988
2989 const auto &[SinkStart_, SinkEnd_] =
2990 getStartAndEndForAccess(InnermostLoop, Sink, SinkTy, BTC, SymbolicMaxBTC,
2991 &SE, &PointerBounds, DT, AC, LoopGuards);
2992 if (isa<SCEVCouldNotCompute>(SinkStart_) ||
2993 isa<SCEVCouldNotCompute>(SinkEnd_))
2994 return false;
2995
2996 auto SrcEnd = SE.applyLoopGuards(SrcEnd_, LoopGuards);
2997 auto SinkStart = SE.applyLoopGuards(SinkStart_, LoopGuards);
2998 if (SE.isKnownPredicate(CmpInst::ICMP_ULE, SrcEnd, SinkStart))
2999 return true;
3000
3001 auto SinkEnd = SE.applyLoopGuards(SinkEnd_, LoopGuards);
3002 auto SrcStart = SE.applyLoopGuards(SrcStart_, LoopGuards);
3003 return SE.isKnownPredicate(CmpInst::ICMP_ULE, SinkEnd, SrcStart);
3004}
3005
3007 MemoryDepChecker::DepDistanceStrideAndSizeInfo>
3008MemoryDepChecker::getDependenceDistanceStrideAndSize(
3009 const AccessAnalysis::MemAccessInfo &A, Instruction *AInst,
3010 const AccessAnalysis::MemAccessInfo &B, Instruction *BInst) {
3011 const auto &DL = InnermostLoop->getHeader()->getDataLayout();
3012 auto &SE = *PSE.getSE();
3013 const auto &[APtr, AIsWrite] = A;
3014 const auto &[BPtr, BIsWrite] = B;
3015
3016 // Two reads are independent.
3017 if (!AIsWrite && !BIsWrite)
3019
3020 Type *ATy = getLoadStoreType(AInst);
3021 Type *BTy = getLoadStoreType(BInst);
3022
3023 // We cannot check pointers in different address spaces.
3024 if (APtr->getType()->getPointerAddressSpace() !=
3025 BPtr->getType()->getPointerAddressSpace())
3027
3029 std::optional<int64_t> StrideAPtr =
3030 getPtrStride(PSE, ATy, APtr, InnermostLoop, *DT, SymbolicStrides,
3031 /*ShouldCheckWrap=*/true, &Predicates);
3032 std::optional<int64_t> StrideBPtr =
3033 getPtrStride(PSE, BTy, BPtr, InnermostLoop, *DT, SymbolicStrides,
3034 /*ShouldCheckWrap=*/true, &Predicates);
3035 PSE.addPredicates(Predicates);
3036
3037 const SCEV *Src = PSE.getSCEV(APtr);
3038 const SCEV *Sink = PSE.getSCEV(BPtr);
3039
3040 // If the induction step is negative we have to invert source and sink of the
3041 // dependence when measuring the distance between them. We should not swap
3042 // AIsWrite with BIsWrite, as their uses expect them in program order.
3043 if (StrideAPtr && *StrideAPtr < 0) {
3044 std::swap(Src, Sink);
3045 std::swap(AInst, BInst);
3046 std::swap(ATy, BTy);
3047 std::swap(StrideAPtr, StrideBPtr);
3048 }
3049
3050 const SCEV *Dist = SE.getMinusSCEV(Sink, Src);
3051
3052 LLVM_DEBUG(dbgs() << "LAA: Src Scev: " << *Src << "Sink Scev: " << *Sink
3053 << "\n");
3054 LLVM_DEBUG(dbgs() << "LAA: Distance for " << *AInst << " to " << *BInst
3055 << ": " << *Dist << "\n");
3056
3057 // Need accesses with constant strides and the same direction for further
3058 // dependence analysis. We don't want to vectorize "A[B[i]] += ..." and
3059 // similar code or pointer arithmetic that could wrap in the address space.
3060
3061 // If either Src or Sink are not strided (i.e. not a non-wrapping AddRec) and
3062 // not loop-invariant (stride will be 0 in that case), we cannot analyze the
3063 // dependence further and also cannot generate runtime checks.
3064 if (!StrideAPtr || !StrideBPtr) {
3065 LLVM_DEBUG(dbgs() << "Pointer access with non-constant stride\n");
3067 }
3068
3069 int64_t StrideAPtrInt = *StrideAPtr;
3070 int64_t StrideBPtrInt = *StrideBPtr;
3071 LLVM_DEBUG(dbgs() << "LAA: Src induction step: " << StrideAPtrInt
3072 << " Sink induction step: " << StrideBPtrInt << "\n");
3073 // At least Src or Sink are loop invariant and the other is strided or
3074 // invariant.
3075 if (!StrideAPtrInt || !StrideBPtrInt) {
3076 // If both are loop-invariant and access the same location, we cannot
3077 // vectorize.
3078 if (!StrideAPtrInt && !StrideBPtrInt && Dist->isZero())
3080 // Otherwise, we can generate a runtime check to disambiguate the accesses.
3082 }
3083
3084 // Both Src and Sink have a constant stride, check if they are in the same
3085 // direction.
3086 if ((StrideAPtrInt > 0) != (StrideBPtrInt > 0)) {
3087 LLVM_DEBUG(
3088 dbgs() << "Pointer access with strides in different directions\n");
3090 }
3091
3092 TypeSize AStoreSz = DL.getTypeStoreSize(ATy);
3093 TypeSize BStoreSz = DL.getTypeStoreSize(BTy);
3094
3095 // If store sizes are not the same, set TypeByteSize to zero, so we can check
3096 // it in the caller isDependent.
3097 uint64_t ASz = DL.getTypeAllocSize(ATy);
3098 uint64_t BSz = DL.getTypeAllocSize(BTy);
3099 uint64_t TypeByteSize = (AStoreSz == BStoreSz) ? BSz : 0;
3100
3101 uint64_t StrideAScaled = AbsoluteValue(StrideAPtrInt) * ASz;
3102 uint64_t StrideBScaled = AbsoluteValue(StrideBPtrInt) * BSz;
3103
3104 uint64_t MaxStride = std::max(StrideAScaled, StrideBScaled);
3105
3106 std::optional<uint64_t> CommonStride;
3107 if (StrideAScaled == StrideBScaled)
3108 CommonStride = StrideAScaled;
3109
3110 // TODO: Historically, we didn't retry with runtime checks when (unscaled)
3111 // strides were different but there is no inherent reason to.
3112 if (!isa<SCEVConstant>(Dist))
3113 ShouldRetryWithRuntimeChecks |= StrideAPtrInt == StrideBPtrInt;
3114
3115 // If distance is a SCEVCouldNotCompute, return Unknown immediately.
3116 if (isa<SCEVCouldNotCompute>(Dist)) {
3117 LLVM_DEBUG(dbgs() << "LAA: Uncomputable distance.\n");
3118 return Dependence::Unknown;
3119 }
3120
3121 return DepDistanceStrideAndSizeInfo(Dist, MaxStride, CommonStride,
3122 TypeByteSize, AIsWrite, BIsWrite);
3123}
3124
3126MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
3127 const MemAccessInfo &B, unsigned BIdx) {
3128 assert(AIdx < BIdx && "Must pass arguments in program order");
3129
3130 // Check if we can prove that Sink only accesses memory after Src's end or
3131 // vice versa. The helper is used to perform the checks only on the exit paths
3132 // where it helps to improve the analysis result.
3133 auto CheckCompletelyBeforeOrAfter = [&]() {
3134 auto *APtr = A.getPointer();
3135 auto *BPtr = B.getPointer();
3136 Type *ATy = getLoadStoreType(InstMap[AIdx]);
3137 Type *BTy = getLoadStoreType(InstMap[BIdx]);
3138 const SCEV *Src = PSE.getSCEV(APtr);
3139 const SCEV *Sink = PSE.getSCEV(BPtr);
3140 return areAccessesCompletelyBeforeOrAfter(Src, ATy, Sink, BTy);
3141 };
3142
3143 // Get the dependence distance, stride, type size and what access writes for
3144 // the dependence between A and B.
3145 auto Res =
3146 getDependenceDistanceStrideAndSize(A, InstMap[AIdx], B, InstMap[BIdx]);
3147 if (std::holds_alternative<Dependence::DepType>(Res)) {
3148 if (std::get<Dependence::DepType>(Res) == Dependence::Unknown &&
3149 CheckCompletelyBeforeOrAfter())
3150 return Dependence::NoDep;
3151 return std::get<Dependence::DepType>(Res);
3152 }
3153
3154 auto &[Dist, MaxStride, CommonStride, TypeByteSize, AIsWrite, BIsWrite] =
3155 std::get<DepDistanceStrideAndSizeInfo>(Res);
3156 bool HasSameSize = TypeByteSize > 0;
3157
3158 ScalarEvolution &SE = *PSE.getSE();
3159 auto &DL = InnermostLoop->getHeader()->getDataLayout();
3160
3161 // If the distance between the acecsses is larger than their maximum absolute
3162 // stride multiplied by the symbolic maximum backedge taken count (which is an
3163 // upper bound of the number of iterations), the accesses are independet, i.e.
3164 // they are far enough appart that accesses won't access the same location
3165 // across all loop ierations.
3166 if (HasSameSize &&
3167 isSafeDependenceDistance(SE, *(PSE.getSymbolicMaxBackedgeTakenCount()),
3168 *Dist, MaxStride))
3169 return Dependence::NoDep;
3170
3171 const APInt *APDist = nullptr;
3172 uint64_t ConstDist = 0;
3173 if (match(Dist, m_scev_APInt(APDist))) {
3174 std::optional<uint64_t> Val = APDist->abs().tryZExtValue();
3175 if (!Val) {
3176 LLVM_DEBUG(dbgs() << "LAA: Constant distance does not fit in 64 bits.\n");
3177 return Dependence::Unknown;
3178 }
3179 ConstDist = *Val;
3180 }
3181
3182 // Attempt to prove strided accesses independent.
3183 if (APDist) {
3184 // If the distance between accesses and their strides are known constants,
3185 // check whether the accesses interlace each other.
3186 if (ConstDist > 0 && CommonStride && CommonStride > 1 && HasSameSize &&
3187 areStridedAccessesIndependent(ConstDist, *CommonStride, TypeByteSize)) {
3188 LLVM_DEBUG(dbgs() << "LAA: Strided accesses are independent\n");
3189 return Dependence::NoDep;
3190 }
3191 } else {
3192 Dist = SE.applyLoopGuards(Dist, LoopGuards);
3193 }
3194
3195 // Negative distances are not plausible dependencies.
3196 if (SE.isKnownNonPositive(Dist)) {
3197 if (SE.isKnownNonNegative(Dist)) {
3198 // Equal-sized accesses to the same location are forward.
3199 if (HasSameSize)
3200 return Dependence::Forward;
3201
3202 if (CommonStride) {
3203 // For mixed sizes, CommonStride is asserted to cover both accesses when
3204 // computed in getDependenceDistanceStrideAndSize, so different
3205 // iterations cannot overlap.
3206 [[maybe_unused]] uint64_t ASz =
3207 DL.getTypeAllocSize(getLoadStoreType(InstMap[AIdx]));
3208 [[maybe_unused]] uint64_t BSz =
3209 DL.getTypeAllocSize(getLoadStoreType(InstMap[BIdx]));
3210 assert(*CommonStride >= std::max(ASz, BSz) &&
3211 "Invariant from getDependenceDistanceStrideAndSize broken!");
3212 return Dependence::Forward;
3213 }
3214 LLVM_DEBUG(dbgs() << "LAA: possibly zero dependence difference but "
3215 "different type sizes\n");
3216 return Dependence::Unknown;
3217 }
3218
3219 bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
3220 // Check if the first access writes to a location that is read in a later
3221 // iteration, where the distance between them is not a multiple of a vector
3222 // factor and relatively small.
3223 //
3224 // NOTE: There is no need to update MaxSafeVectorWidthInBits after call to
3225 // couldPreventStoreLoadForward, even if it changed MinDepDistBytes, since a
3226 // forward dependency will allow vectorization using any width.
3227
3228 if (IsTrueDataDependence && EnableForwardingConflictDetection) {
3229 if (!ConstDist) {
3230 return CheckCompletelyBeforeOrAfter() ? Dependence::NoDep
3232 }
3233 if (!HasSameSize ||
3234 couldPreventStoreLoadForward(ConstDist, TypeByteSize)) {
3235 LLVM_DEBUG(
3236 dbgs() << "LAA: Forward but may prevent st->ld forwarding\n");
3238 }
3239 }
3240
3241 LLVM_DEBUG(dbgs() << "LAA: Dependence is negative\n");
3242 return Dependence::Forward;
3243 }
3244
3245 std::optional<int64_t> MinDistanceOpt =
3247 if (!MinDistanceOpt) {
3248 LLVM_DEBUG(dbgs() << "LAA: Minimum distance does not fit in 64 bits.\n");
3249 return Dependence::Unknown;
3250 }
3251 int64_t MinDistance = *MinDistanceOpt;
3252 // Below we only handle strictly positive distances.
3253 if (MinDistance <= 0) {
3254 return CheckCompletelyBeforeOrAfter() ? Dependence::NoDep
3256 }
3257
3258 if (!HasSameSize) {
3259 if (CheckCompletelyBeforeOrAfter())
3260 return Dependence::NoDep;
3261 LLVM_DEBUG(dbgs() << "LAA: ReadWrite-Write positive dependency with "
3262 "different type sizes\n");
3263 return Dependence::Unknown;
3264 }
3265 // Bail out early if passed-in parameters make vectorization not feasible.
3266 unsigned MinForcedFactor =
3267 std::max(1U, VectorizerParams::VectorizationFactor.getKnownMinValue());
3268 unsigned ForcedUnroll = (VectorizerParams::VectorizationInterleave ?
3270 // The minimum number of iterations for a vectorized/unrolled version.
3271 unsigned MinNumIter = std::max(MinForcedFactor * ForcedUnroll, 2U);
3272
3273 // It's not vectorizable if the distance is smaller than the minimum distance
3274 // needed for a vectroized/unrolled version. Vectorizing one iteration in
3275 // front needs MaxStride. Vectorizing the last iteration needs TypeByteSize.
3276 // (No need to plus the last gap distance).
3277 //
3278 // E.g. Assume one char is 1 byte in memory and one int is 4 bytes.
3279 // foo(int *A) {
3280 // int *B = (int *)((char *)A + 14);
3281 // for (i = 0 ; i < 1024 ; i += 2)
3282 // B[i] = A[i] + 1;
3283 // }
3284 //
3285 // Two accesses in memory (stride is 4 * 2):
3286 // | A[0] | | A[2] | | A[4] | | A[6] | |
3287 // | B[0] | | B[2] | | B[4] |
3288 //
3289 // MinDistance needs for vectorizing iterations except the last iteration:
3290 // 4 * 2 * (MinNumIter - 1). MinDistance needs for the last iteration: 4.
3291 // So the minimum distance needed is: 4 * 2 * (MinNumIter - 1) + 4.
3292 //
3293 // If MinNumIter is 2, it is vectorizable as the minimum distance needed is
3294 // 12, which is less than distance.
3295 //
3296 // If MinNumIter is 4 (Say if a user forces the vectorization factor to be 4),
3297 // the minimum distance needed is 28, which is greater than distance. It is
3298 // not safe to do vectorization.
3299 //
3300 // We use MaxStride (maximum of src and sink strides) to get a conservative
3301 // lower bound on the MinDistanceNeeded in case of different strides.
3302
3303 // We know that Dist is positive, but it may not be constant. Use the signed
3304 // minimum for computations below, as this ensures we compute the closest
3305 // possible dependence distance.
3306 uint64_t MinDistanceNeeded = MaxStride * (MinNumIter - 1) + TypeByteSize;
3307 if (MinDistanceNeeded > static_cast<uint64_t>(MinDistance)) {
3308 if (!ConstDist) {
3309 // For non-constant distances, we checked the lower bound of the
3310 // dependence distance and the distance may be larger at runtime (and safe
3311 // for vectorization). Classify it as Unknown, so we re-try with runtime
3312 // checks, unless we can prove both accesses cannot overlap.
3313 return CheckCompletelyBeforeOrAfter() ? Dependence::NoDep
3315 }
3316 LLVM_DEBUG(dbgs() << "LAA: Failure because of positive minimum distance "
3317 << MinDistance << '\n');
3318 return Dependence::Backward;
3319 }
3320
3321 // Unsafe if the minimum distance needed is greater than smallest dependence
3322 // distance distance.
3323 if (MinDistanceNeeded > MinDepDistBytes) {
3324 LLVM_DEBUG(dbgs() << "LAA: Failure because it needs at least "
3325 << MinDistanceNeeded << " size in bytes\n");
3326 return Dependence::Backward;
3327 }
3328
3329 MinDepDistBytes =
3330 std::min(static_cast<uint64_t>(MinDistance), MinDepDistBytes);
3331
3332 bool IsTrueDataDependence = (!AIsWrite && BIsWrite);
3333 if (IsTrueDataDependence && EnableForwardingConflictDetection && ConstDist &&
3334 couldPreventStoreLoadForward(MinDistance, TypeByteSize, *CommonStride))
3336
3337 uint64_t MaxVF = MinDepDistBytes / MaxStride;
3338 LLVM_DEBUG(dbgs() << "LAA: Positive min distance " << MinDistance
3339 << " with max VF = " << MaxVF << '\n');
3340
3341 uint64_t MaxVFInBits = MaxVF * TypeByteSize * 8;
3342 if (!ConstDist && MaxVFInBits < MaxTargetVectorWidthInBits) {
3343 // For non-constant distances, we checked the lower bound of the dependence
3344 // distance and the distance may be larger at runtime (and safe for
3345 // vectorization). Classify it as Unknown, so we re-try with runtime checks,
3346 // unless we can prove both accesses cannot overlap.
3347 return CheckCompletelyBeforeOrAfter() ? Dependence::NoDep
3349 }
3350
3351 if (CheckCompletelyBeforeOrAfter())
3352 return Dependence::NoDep;
3353
3354 MaxSafeVectorWidthInBits = std::min(MaxSafeVectorWidthInBits, MaxVFInBits);
3356}
3357
3359 ArrayRef<MemAccessInfo> CheckDeps) {
3360
3361 MinDepDistBytes = -1;
3363 for (MemAccessInfo CurAccess : CheckDeps) {
3364 if (Visited.contains(CurAccess))
3365 continue;
3366
3367 // Check accesses within this set.
3369 DepCands.findLeader(CurAccess);
3371 DepCands.member_end();
3372
3373 // Check every access pair.
3374 while (AI != AE) {
3375 Visited.insert(*AI);
3376 bool AIIsWrite = AI->getInt();
3377 // Reads from the same pointer don't create extra hazards, but multiple
3378 // stores do (WAW), so start from AI for writes and next(AI) for reads.
3380 (AIIsWrite ? AI : std::next(AI));
3381 while (OI != AE) {
3382 // Check every accessing instruction pair in program order.
3383 auto &Acc = Accesses[*AI];
3384 for (std::vector<unsigned>::iterator I1 = Acc.begin(), I1E = Acc.end();
3385 I1 != I1E; ++I1)
3386 // When checking for WAW (OI == AI) caused by multiple writes to the
3387 // same pointer, start I2 at the next access past I1 to avoid
3388 // self-comparison.
3389 for (std::vector<unsigned>::iterator
3390 I2 = (OI == AI ? std::next(I1) : Accesses[*OI].begin()),
3391 I2E = (OI == AI ? I1E : Accesses[*OI].end());
3392 I2 != I2E; ++I2) {
3393 auto A = std::make_pair(&*AI, *I1);
3394 auto B = std::make_pair(&*OI, *I2);
3395
3396 assert(*I1 != *I2);
3397 if (*I1 > *I2)
3398 std::swap(A, B);
3399
3401 isDependent(*A.first, A.second, *B.first, B.second);
3403
3404 // Gather dependences unless we accumulated MaxDependences
3405 // dependences. In that case return as soon as we find the first
3406 // unsafe dependence. This puts a limit on this quadratic
3407 // algorithm.
3408 if (RecordDependences) {
3409 if (Type != Dependence::NoDep)
3410 Dependences.emplace_back(A.second, B.second, Type);
3411
3412 if (Dependences.size() >= MaxDependences) {
3413 RecordDependences = false;
3414 Dependences.clear();
3416 << "Too many dependences, stopped recording\n");
3417 }
3418 }
3419 if (!RecordDependences && !isSafeForVectorization())
3420 return false;
3421 }
3422 ++OI;
3423 }
3424 ++AI;
3425 }
3426 }
3427
3428 LLVM_DEBUG(dbgs() << "Total Dependences: " << Dependences.size() << "\n");
3429 return isSafeForVectorization();
3430}
3431
3434 MemAccessInfo Access(Ptr, IsWrite);
3435 auto I = Accesses.find(Access);
3437 if (I != Accesses.end()) {
3438 transform(I->second, std::back_inserter(Insts),
3439 [&](unsigned Idx) { return this->InstMap[Idx]; });
3440 }
3441
3442 return Insts;
3443}
3444
3446 "NoDep",
3447 "Unknown",
3448 "IndirectUnsafe",
3449 "InvariantUnsafe",
3450 "Forward",
3451 "ForwardButPreventsForwarding",
3452 "Backward",
3453 "BackwardVectorizable",
3454 "BackwardVectorizableButPreventsForwarding"};
3455
3457 raw_ostream &OS, unsigned Depth,
3458 const SmallVectorImpl<Instruction *> &Instrs) const {
3459 OS.indent(Depth) << DepName[Type] << ":\n";
3460 OS.indent(Depth + 2) << *Instrs[Source] << " -> \n";
3461 OS.indent(Depth + 2) << *Instrs[Destination] << "\n";
3462}
3463
3464bool LoopAccessInfo::canAnalyzeLoop() {
3465 // We need to have a loop header.
3466 LLVM_DEBUG(dbgs() << "\nLAA: Checking a loop in '"
3467 << TheLoop->getHeader()->getParent()->getName() << "' from "
3468 << TheLoop->getLocStr() << "\n");
3469
3470 // We can only analyze innermost loops.
3471 if (!TheLoop->isInnermost()) {
3472 LLVM_DEBUG(dbgs() << "LAA: loop is not the innermost loop\n");
3473 recordAnalysis("NotInnerMostLoop") << "loop is not the innermost loop";
3474 return false;
3475 }
3476
3477 // We must have a single backedge.
3478 if (TheLoop->getNumBackEdges() != 1) {
3479 LLVM_DEBUG(
3480 dbgs() << "LAA: loop control flow is not understood by analyzer\n");
3481 recordAnalysis("CFGNotUnderstood")
3482 << "loop control flow is not understood by analyzer";
3483 return false;
3484 }
3485
3486 // ScalarEvolution needs to be able to find the symbolic max backedge taken
3487 // count, which is an upper bound on the number of loop iterations. The loop
3488 // may execute fewer iterations, if it exits via an uncountable exit.
3489 const SCEV *ExitCount = PSE->getSymbolicMaxBackedgeTakenCount();
3490 if (isa<SCEVCouldNotCompute>(ExitCount)) {
3491 recordAnalysis("CantComputeNumberOfIterations")
3492 << "could not determine number of loop iterations";
3493 LLVM_DEBUG(dbgs() << "LAA: SCEV could not compute the loop exit count.\n");
3494 return false;
3495 }
3496
3497 LLVM_DEBUG(dbgs() << "LAA: Found an analyzable loop: "
3498 << TheLoop->getHeader()->getName() << "\n");
3499 return true;
3500}
3501
3502bool LoopAccessInfo::analyzeLoop(AAResults *AA, const LoopInfo *LI,
3503 const TargetLibraryInfo *TLI,
3504 DominatorTree *DT) {
3505 // Holds the Load and Store instructions.
3508 SmallPtrSet<MDNode *, 8> LoopAliasScopes;
3509
3510 // Holds all the different accesses in the loop.
3511 unsigned NumReads = 0;
3512 unsigned NumReadWrites = 0;
3513
3514 bool HasComplexMemInst = false;
3515
3516 // A runtime check is only legal to insert if there are no convergent calls.
3517 HasConvergentOp = false;
3518
3519 PtrRtChecking->Pointers.clear();
3520 PtrRtChecking->Need = false;
3521
3522 const bool IsAnnotatedParallel = TheLoop->isAnnotatedParallel();
3523
3524 const bool EnableMemAccessVersioningOfLoop =
3526 !TheLoop->getHeader()->getParent()->hasOptSize();
3527
3528 // Traverse blocks in fixed RPOT order, regardless of their storage in the
3529 // loop info, as it may be arbitrary.
3530 LoopBlocksRPO RPOT(TheLoop);
3531 RPOT.perform(LI);
3532
3533 // Don't return early as soon as we found a memory access that cannot be
3534 // vectorize - HasConvergentOp must still be computed as it is part of LAI's
3535 // public API (used by LoopDistribute).
3536 for (BasicBlock *BB : RPOT) {
3537 // Scan the BB and collect legal loads and stores. Also detect any
3538 // convergent instructions.
3539 for (Instruction &I : *BB) {
3540 if (auto *Call = dyn_cast<CallBase>(&I)) {
3541 if (Call->isConvergent())
3542 HasConvergentOp = true;
3543 }
3544
3545 // Unsafe to vectorize and we already found a convergent operation, can
3546 // early return now.
3547 if (HasComplexMemInst && HasConvergentOp)
3548 return false;
3549
3550 // Already unsafe to vectorize; keep scanning for convergent ops.
3551 if (HasComplexMemInst)
3552 continue;
3553
3554 // Record alias scopes defined inside the loop.
3555 if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
3556 for (Metadata *Op : Decl->getScopeList()->operands())
3557 LoopAliasScopes.insert(cast<MDNode>(Op));
3558
3559 // Many math library functions read the rounding mode. We will only
3560 // vectorize a loop if it contains known function calls that don't set
3561 // the flag. Therefore, it is safe to ignore this read from memory.
3562 auto *Call = dyn_cast<CallInst>(&I);
3564 continue;
3565
3566 // If this is a load, save it. If this instruction can read from memory
3567 // but is not a load, we only allow it if it's a call to a function with a
3568 // vector mapping and no pointer arguments.
3569 if (I.mayReadFromMemory()) {
3570 auto hasPointerArgs = [](CallBase *CB) {
3571 return any_of(CB->args(), [](Value const *Arg) {
3572 return Arg->getType()->isPointerTy();
3573 });
3574 };
3575
3576 // If the function has an explicit vectorized counterpart, and does not
3577 // take output/input pointers, we can safely assume that it can be
3578 // vectorized.
3579 if (Call && !Call->isNoBuiltin() && Call->getCalledFunction() &&
3580 !hasPointerArgs(Call) && !VFDatabase::getMappings(*Call).empty())
3581 continue;
3582
3583 auto *Ld = dyn_cast<LoadInst>(&I);
3584 if (!Ld) {
3585 recordAnalysis("CantVectorizeInstruction", &I)
3586 << "instruction cannot be vectorized";
3587 HasComplexMemInst = true;
3588 continue;
3589 }
3590 if (!Ld->isSimple() && !IsAnnotatedParallel) {
3591 recordAnalysis("NonSimpleLoad", Ld)
3592 << "read with atomic ordering or volatile read";
3593 LLVM_DEBUG(dbgs() << "LAA: Found a non-simple load.\n");
3594 HasComplexMemInst = true;
3595 continue;
3596 }
3597 Loads.push_back(Ld);
3598 DepChecker->addAccess(Ld);
3599 if (EnableMemAccessVersioningOfLoop)
3600 collectStridedAccess(Ld);
3601 continue;
3602 }
3603
3604 // Save 'store' instructions. Abort if other instructions write to memory.
3605 if (I.mayWriteToMemory()) {
3606 auto *St = dyn_cast<StoreInst>(&I);
3607 if (!St) {
3608 recordAnalysis("CantVectorizeInstruction", &I)
3609 << "instruction cannot be vectorized";
3610 HasComplexMemInst = true;
3611 continue;
3612 }
3613 if (!St->isSimple() && !IsAnnotatedParallel) {
3614 recordAnalysis("NonSimpleStore", St)
3615 << "write with atomic ordering or volatile write";
3616 LLVM_DEBUG(dbgs() << "LAA: Found a non-simple store.\n");
3617 HasComplexMemInst = true;
3618 continue;
3619 }
3620 Stores.push_back(St);
3621 DepChecker->addAccess(St);
3622 if (EnableMemAccessVersioningOfLoop)
3623 collectStridedAccess(St);
3624 }
3625 } // Next instr.
3626 } // Next block.
3627
3628 if (HasComplexMemInst)
3629 return false;
3630
3631 // Now we have two lists that hold the loads and the stores.
3632 // Next, we find the pointers that they use.
3633
3634 // Check if we see any stores. If there are no stores, then we don't
3635 // care if the pointers are *restrict*.
3636 if (!Stores.size()) {
3637 LLVM_DEBUG(dbgs() << "LAA: Found a read-only loop!\n");
3638 return true;
3639 }
3640
3642 AccessAnalysis Accesses(TheLoop, AA, LI, *DT, DepCands, *PSE,
3643 LoopAliasScopes);
3644
3645 // Holds the analyzed pointers. We don't want to call getUnderlyingObjects
3646 // multiple times on the same object. If the ptr is accessed twice, once
3647 // for read and once for write, it will only appear once (on the write
3648 // list). This is okay, since we are going to check for conflicts between
3649 // writes and between reads and writes, but not between reads and reads.
3650 SmallSet<std::pair<Value *, Type *>, 16> Seen;
3651
3652 // Record uniform store addresses to identify if we have multiple stores
3653 // to the same address.
3654 SmallPtrSet<Value *, 16> UniformStores;
3655
3656 for (StoreInst *ST : Stores) {
3657 Value *Ptr = ST->getPointerOperand();
3658
3659 if (isInvariant(Ptr)) {
3660 // Record store instructions to loop invariant addresses
3661 StoresToInvariantAddresses.push_back(ST);
3662 HasStoreStoreDependenceInvolvingLoopInvariantAddress |=
3663 !UniformStores.insert(Ptr).second;
3664 }
3665
3666 // If we did *not* see this pointer before, insert it to the read-write
3667 // list. At this phase it is only a 'write' list.
3668 Type *AccessTy = getLoadStoreType(ST);
3669 if (Seen.insert({Ptr, AccessTy}).second) {
3670 ++NumReadWrites;
3671
3672 MemoryLocation Loc = MemoryLocation::get(ST);
3673 // The TBAA metadata could have a control dependency on the predication
3674 // condition, so we cannot rely on it when determining whether or not we
3675 // need runtime pointer checks.
3676 if (blockNeedsPredication(ST->getParent(), TheLoop, DT))
3677 Loc.AATags.TBAA = nullptr;
3678
3679 // Expand forked pointers (i.e., a phi of multiple strided pointers) into
3680 // all alternatives.
3681 visitPointers(const_cast<Value *>(Loc.Ptr), *TheLoop,
3682 [&Accesses, AccessTy, Loc](Value *Ptr) {
3683 MemoryLocation NewLoc = Loc.getWithNewPtr(Ptr);
3684 Accesses.addStore(NewLoc, AccessTy);
3685 });
3686 }
3687 }
3688
3689 if (IsAnnotatedParallel) {
3690 LLVM_DEBUG(
3691 dbgs() << "LAA: A loop annotated parallel, ignore memory dependency "
3692 << "checks.\n");
3693 return true;
3694 }
3695
3696 for (LoadInst *LD : Loads) {
3697 Value *Ptr = LD->getPointerOperand();
3698 // If we did *not* see this pointer before, insert it to the read list. If
3699 // we *did* see it before, then it is already in the read-write list. This
3700 // allows us to vectorize expressions such as A[i] += x; Because the address
3701 // of A[i] is a read-write pointer. This only works if the index of A[i] is
3702 // strictly monotonic, which we approximate (conservatively) via
3703 // getPtrStride. If the address is unknown (e.g. A[B[i]]) then we may read,
3704 // modify, and write overlapping words. Note that "zero stride" is unsafe
3705 // and is being handled below.
3706 bool IsReadOnlyPtr = false;
3707 Type *AccessTy = getLoadStoreType(LD);
3708 if (Seen.insert({Ptr, AccessTy}).second ||
3709 !getPtrStride(*PSE, AccessTy, Ptr, TheLoop, *DT, SymbolicStrides,
3710 true)) {
3711 ++NumReads;
3712 IsReadOnlyPtr = true;
3713 }
3714
3715 // See if there is an unsafe dependency between a load to a uniform address and
3716 // store to the same uniform address.
3717 if (UniformStores.contains(Ptr)) {
3718 LLVM_DEBUG(dbgs() << "LAA: Found an unsafe dependency between a uniform "
3719 "load and uniform store to the same address!\n");
3720 HasLoadStoreDependenceInvolvingLoopInvariantAddress = true;
3721 }
3722
3723 MemoryLocation Loc = MemoryLocation::get(LD);
3724 // The TBAA metadata could have a control dependency on the predication
3725 // condition, so we cannot rely on it when determining whether or not we
3726 // need runtime pointer checks.
3727 if (blockNeedsPredication(LD->getParent(), TheLoop, DT))
3728 Loc.AATags.TBAA = nullptr;
3729
3730 // Expand forked pointers (i.e., a phi of multiple strided pointers) into
3731 // all alternatives.
3732 visitPointers(const_cast<Value *>(Loc.Ptr), *TheLoop,
3733 [&Accesses, AccessTy, Loc, IsReadOnlyPtr](Value *Ptr) {
3734 MemoryLocation NewLoc = Loc.getWithNewPtr(Ptr);
3735 Accesses.addLoad(NewLoc, AccessTy, IsReadOnlyPtr);
3736 });
3737 }
3738
3739 // If we write (or read-write) to a single destination and there are no other
3740 // reads in this loop then is it safe to vectorize: the vectorized stores
3741 // preserve ordering via replication or order-preserving @llvm.masked.scatter.
3742 if (NumReadWrites == 1 && NumReads == 0) {
3743 LLVM_DEBUG(dbgs() << "LAA: Found a write-only loop!\n");
3744 return true;
3745 }
3746
3747 // Build dependence sets and check whether we need a runtime pointer bounds
3748 // check.
3749 Accesses.buildDependenceSets();
3750
3751 // Find pointers with computable bounds. We are going to use this information
3752 // to place a runtime bound check.
3753 Value *UncomputablePtr = nullptr;
3754 HasCompletePtrRtChecking =
3755 Accesses.canCheckPtrAtRT(*PtrRtChecking, TheLoop, SymbolicStrides,
3756 UncomputablePtr, AllowPartial, getDepChecker());
3757 if (!HasCompletePtrRtChecking) {
3758 const auto *I = dyn_cast_or_null<Instruction>(UncomputablePtr);
3759 recordAnalysis("CantIdentifyArrayBounds", I)
3760 << "cannot identify array bounds";
3761 LLVM_DEBUG(dbgs() << "LAA: We can't vectorize because we can't find "
3762 << "the array bounds.\n");
3763 return false;
3764 }
3765
3766 LLVM_DEBUG(
3767 dbgs() << "LAA: May be able to perform a memory runtime check if needed.\n");
3768
3769 bool DepsAreSafe = true;
3770 if (Accesses.isDependencyCheckNeeded()) {
3771 LLVM_DEBUG(dbgs() << "LAA: Checking memory dependencies\n");
3772 DepsAreSafe =
3773 DepChecker->areDepsSafe(DepCands, Accesses.getDependenciesToCheck());
3774
3775 if (!DepsAreSafe && DepChecker->shouldRetryWithRuntimeChecks()) {
3776 LLVM_DEBUG(dbgs() << "LAA: Retrying with memory checks\n");
3777
3778 PtrRtChecking->reset();
3779 PtrRtChecking->Need = true;
3780
3781 UncomputablePtr = nullptr;
3782 HasCompletePtrRtChecking = Accesses.canCheckPtrAtRT(
3783 *PtrRtChecking, TheLoop, SymbolicStrides, UncomputablePtr,
3784 AllowPartial, getDepChecker());
3785
3786 // Check that we found the bounds for the pointer.
3787 if (!HasCompletePtrRtChecking) {
3788 auto *I = dyn_cast_or_null<Instruction>(UncomputablePtr);
3789 recordAnalysis("CantCheckMemDepsAtRunTime", I)
3790 << "cannot check memory dependencies at runtime";
3791 LLVM_DEBUG(dbgs() << "LAA: Can't vectorize with memory checks\n");
3792 return false;
3793 }
3794
3795 // Clear the dependency checks. They are no longer needed.
3796 Accesses.resetDepChecks(*DepChecker);
3797
3798 DepsAreSafe = true;
3799 }
3800 }
3801
3802 // Update the invariant address dependence flags based on dependences found
3803 // by the dep checker. Even if dependences were not recorded (too many to
3804 // track), any InvariantUnsafe dep would still have set the status to Unsafe
3805 if (const auto *Deps = DepChecker->getDependences()) {
3806 for (const auto &Dep : *Deps) {
3808 continue;
3809 Instruction *Src = Dep.getSource(*DepChecker);
3810 Instruction *Dst = Dep.getDestination(*DepChecker);
3811 if (isa<LoadInst>(Src) != isa<LoadInst>(Dst)) {
3812 HasLoadStoreDependenceInvolvingLoopInvariantAddress = true;
3813 } else {
3814 assert(isa<StoreInst>(Src) && isa<StoreInst>(Dst) &&
3815 "Expected both to be stores");
3816 HasStoreStoreDependenceInvolvingLoopInvariantAddress = true;
3817 }
3818 }
3819 }
3820
3821 if (HasConvergentOp) {
3822 recordAnalysis("CantInsertRuntimeCheckWithConvergent")
3823 << "cannot add control dependency to convergent operation";
3824 LLVM_DEBUG(dbgs() << "LAA: We can't vectorize because a runtime check "
3825 "would be needed with a convergent operation\n");
3826 return false;
3827 }
3828
3829 if (DepsAreSafe) {
3830 LLVM_DEBUG(
3831 dbgs() << "LAA: No unsafe dependent memory operations in loop. We"
3832 << (PtrRtChecking->Need ? "" : " don't")
3833 << " need runtime memory checks.\n");
3834 return true;
3835 }
3836
3837 emitUnsafeDependenceRemark();
3838 return false;
3839}
3840
3841void LoopAccessInfo::emitUnsafeDependenceRemark() {
3842 const auto *Deps = getDepChecker().getDependences();
3843 if (!Deps)
3844 return;
3845 const auto *Found =
3846 llvm::find_if(*Deps, [](const MemoryDepChecker::Dependence &D) {
3849 });
3850 if (Found == Deps->end())
3851 return;
3852 MemoryDepChecker::Dependence Dep = *Found;
3853
3854 LLVM_DEBUG(dbgs() << "LAA: unsafe dependent memory operations in loop\n");
3855
3856 // Emit remark for first unsafe dependence
3857 bool HasForcedDistribution =
3858 getBooleanLoopAttribute(TheLoop, "llvm.loop.distribute.enable");
3859
3860 const std::string Info =
3861 HasForcedDistribution
3862 ? "unsafe dependent memory operations in loop."
3863 : "unsafe dependent memory operations in loop. Use "
3864 "#pragma clang loop distribute(enable) to allow loop distribution "
3865 "to attempt to isolate the offending operations into a separate "
3866 "loop";
3867 OptimizationRemarkAnalysis &R =
3868 recordAnalysis("UnsafeDep", Dep.getDestination(getDepChecker())) << Info;
3869
3870 switch (Dep.Type) {
3874 llvm_unreachable("Unexpected dependence");
3876 R << "\nBackward loop carried data dependence.";
3877 break;
3879 R << "\nForward loop carried data dependence that prevents "
3880 "store-to-load forwarding.";
3881 break;
3883 R << "\nBackward loop carried data dependence that prevents "
3884 "store-to-load forwarding.";
3885 break;
3887 R << "\nUnsafe indirect dependence.";
3888 break;
3890 R << "\nUnsafe dependence on loop-invariant address.";
3891 break;
3893 R << "\nUnknown data dependence.";
3894 break;
3895 }
3896
3897 if (Instruction *I = Dep.getSource(getDepChecker())) {
3898 DebugLoc SourceLoc = I->getDebugLoc();
3900 SourceLoc = DD->getDebugLoc();
3901 if (SourceLoc)
3902 R << " Memory location is the same as accessed at "
3903 << ore::NV("Location", SourceLoc);
3904 }
3905}
3906
3908 const Loop *TheLoop,
3909 const DominatorTree *DT) {
3910 assert(TheLoop->contains(BB) && "Unknown block used");
3911
3912 // Blocks that do not dominate the latch need predication.
3913 const BasicBlock *Latch = TheLoop->getLoopLatch();
3914 assert(Latch && "Loop expected to have a single latch.");
3915 return !DT->dominates(BB, Latch);
3916}
3917
3919LoopAccessInfo::recordAnalysis(StringRef RemarkName, const Instruction *I) {
3920 assert(!Report && "Multiple reports generated");
3921
3922 const BasicBlock *CodeRegion = TheLoop->getHeader();
3923 DebugLoc DL = TheLoop->getStartLoc();
3924
3925 if (I) {
3926 CodeRegion = I->getParent();
3927 // If there is no debug location attached to the instruction, revert back to
3928 // using the loop's.
3929 if (I->getDebugLoc())
3930 DL = I->getDebugLoc();
3931 }
3932
3933 Report = std::make_unique<OptimizationRemarkAnalysis>(DEBUG_TYPE, RemarkName,
3934 DL, CodeRegion);
3935 return *Report;
3936}
3937
3939 auto *SE = PSE->getSE();
3940 if (TheLoop->isLoopInvariant(V))
3941 return true;
3942 if (!SE->isSCEVable(V->getType()))
3943 return false;
3944 const SCEV *S = SE->getSCEV(V);
3945 return SE->isLoopInvariant(S, TheLoop);
3946}
3947
3948/// If \p Ptr is a GEP, which has a loop-variant operand, return that operand.
3949/// Otherwise, return \p Ptr.
3951 Loop *Lp) {
3952 auto *GEP = dyn_cast<GetElementPtrInst>(Ptr);
3953 if (!GEP)
3954 return Ptr;
3955
3956 Value *V = Ptr;
3957 for (const Use &U : GEP->operands()) {
3958 if (!SE->isLoopInvariant(SE->getSCEV(U), Lp)) {
3959 if (V == Ptr)
3960 V = U;
3961 else
3962 // There must be exactly one loop-variant operand.
3963 return Ptr;
3964 }
3965 }
3966 return V;
3967}
3968
3969/// Get the stride of a pointer access in a loop. Looks for symbolic
3970/// strides "a[i*stride]". Returns the symbolic stride, or null otherwise.
3971static const SCEV *getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
3972 auto *PtrTy = dyn_cast<PointerType>(Ptr->getType());
3973 if (!PtrTy)
3974 return nullptr;
3975
3976 // Try to remove a gep instruction to make the pointer (actually index at this
3977 // point) easier analyzable. If OrigPtr is equal to Ptr we are analyzing the
3978 // pointer, otherwise, we are analyzing the index.
3979 Value *OrigPtr = Ptr;
3980
3981 Ptr = getLoopVariantGEPOperand(Ptr, SE, Lp);
3982 const SCEV *V = SE->getSCEV(Ptr);
3983
3984 if (Ptr != OrigPtr)
3985 // Strip off casts.
3986 while (auto *C = dyn_cast<SCEVIntegralCastExpr>(V))
3987 V = C->getOperand();
3988
3990 return nullptr;
3991
3992 // Note that the restriction after this loop invariant check are only
3993 // profitability restrictions.
3994 if (!SE->isLoopInvariant(V, Lp))
3995 return nullptr;
3996
3997 // Look for the loop invariant symbolic value.
3998 if (isa<SCEVUnknown>(V))
3999 return V;
4000
4001 // Look through multiplies that scale a stride by a constant.
4003 if (auto *C = dyn_cast<SCEVIntegralCastExpr>(V))
4004 if (isa<SCEVUnknown>(C->getOperand()))
4005 return V;
4006
4007 return nullptr;
4008}
4009
4010void LoopAccessInfo::collectStridedAccess(Value *MemAccess) {
4011 Value *Ptr = getLoadStorePointerOperand(MemAccess);
4012 if (!Ptr)
4013 return;
4014
4015 // Note: getStrideFromPointer is a *profitability* heuristic. We
4016 // could broaden the scope of values returned here - to anything
4017 // which happens to be loop invariant and contributes to the
4018 // computation of an interesting IV - but we chose not to as we
4019 // don't have a cost model here, and broadening the scope exposes
4020 // far too many unprofitable cases.
4021 const SCEV *StrideExpr = getStrideFromPointer(Ptr, PSE->getSE(), TheLoop);
4022 if (!StrideExpr)
4023 return;
4024
4025 if (match(StrideExpr, m_scev_UndefOrPoison()))
4026 return;
4027
4028 LLVM_DEBUG(dbgs() << "LAA: Found a strided access that is a candidate for "
4029 "versioning:");
4030 LLVM_DEBUG(dbgs() << " Ptr: " << *Ptr << " Stride: " << *StrideExpr << "\n");
4031
4032 if (!SpeculateUnitStride) {
4033 LLVM_DEBUG(dbgs() << " Chose not to due to -laa-speculate-unit-stride\n");
4034 return;
4035 }
4036
4037 // Avoid adding the "Stride == 1" predicate when we know that
4038 // Stride >= Trip-Count. Such a predicate will effectively optimize a single
4039 // or zero iteration loop, as Trip-Count <= Stride == 1.
4040 //
4041 // TODO: We are currently not making a very informed decision on when it is
4042 // beneficial to apply stride versioning. It might make more sense that the
4043 // users of this analysis (such as the vectorizer) will trigger it, based on
4044 // their specific cost considerations; For example, in cases where stride
4045 // versioning does not help resolving memory accesses/dependences, the
4046 // vectorizer should evaluate the cost of the runtime test, and the benefit
4047 // of various possible stride specializations, considering the alternatives
4048 // of using gather/scatters (if available).
4049
4050 const SCEV *MaxBTC = PSE->getSymbolicMaxBackedgeTakenCount();
4051
4052 // Match the types so we can compare the stride and the MaxBTC.
4053 // The Stride can be positive/negative, so we sign extend Stride;
4054 // The backedgeTakenCount is non-negative, so we zero extend MaxBTC.
4055 const DataLayout &DL = TheLoop->getHeader()->getDataLayout();
4056 uint64_t StrideTypeSizeBits = DL.getTypeSizeInBits(StrideExpr->getType());
4057 uint64_t BETypeSizeBits = DL.getTypeSizeInBits(MaxBTC->getType());
4058 const SCEV *CastedStride = StrideExpr;
4059 const SCEV *CastedBECount = MaxBTC;
4060 ScalarEvolution *SE = PSE->getSE();
4061 if (BETypeSizeBits >= StrideTypeSizeBits)
4062 CastedStride = SE->getNoopOrSignExtend(StrideExpr, MaxBTC->getType());
4063 else
4064 CastedBECount = SE->getZeroExtendExpr(MaxBTC, StrideExpr->getType());
4065 const SCEV *StrideMinusBETaken = SE->getMinusSCEV(CastedStride, CastedBECount);
4066 // Since TripCount == BackEdgeTakenCount + 1, checking:
4067 // "Stride >= TripCount" is equivalent to checking:
4068 // Stride - MaxBTC> 0
4069 if (SE->isKnownPositive(StrideMinusBETaken)) {
4070 LLVM_DEBUG(
4071 dbgs() << "LAA: Stride>=TripCount; No point in versioning as the "
4072 "Stride==1 predicate will imply that the loop executes "
4073 "at most once.\n");
4074 return;
4075 }
4076 LLVM_DEBUG(dbgs() << "LAA: Found a strided access that we can version.\n");
4077
4078 // Strip back off the integer cast, and check that our result is a
4079 // SCEVUnknown as we expect.
4080 const SCEV *StrideBase = StrideExpr;
4081 if (const auto *C = dyn_cast<SCEVIntegralCastExpr>(StrideBase))
4082 StrideBase = C->getOperand();
4083 assert(SE->isLoopInvariant(StrideBase, TheLoop) &&
4084 "users of the map rely on the stride being loop invariant");
4085 SymbolicStrides[Ptr] = cast<SCEVUnknown>(StrideBase);
4086}
4087
4089 const TargetTransformInfo *TTI,
4090 const TargetLibraryInfo *TLI, AAResults *AA,
4091 DominatorTree *DT, LoopInfo *LI,
4092 AssumptionCache *AC, bool AllowPartial)
4093 : PSE(std::make_unique<PredicatedScalarEvolution>(*SE, *L)),
4094 PtrRtChecking(nullptr), TheLoop(L),
4095 LoopGuards(ScalarEvolution::LoopGuards::collectOnDemand(L, *SE)),
4096 AllowPartial(AllowPartial) {
4097 unsigned MaxTargetVectorWidthInBits = std::numeric_limits<unsigned>::max();
4098 if (TTI && !TTI->enableScalableVectorization())
4099 // Scale the vector width by 2 as rough estimate to also consider
4100 // interleaving.
4101 MaxTargetVectorWidthInBits =
4102 TTI->getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector) * 2;
4103
4104 DepChecker = std::make_unique<MemoryDepChecker>(
4105 *PSE, AC, DT, L, SymbolicStrides, MaxTargetVectorWidthInBits, LoopGuards);
4106 PtrRtChecking =
4107 std::make_unique<RuntimePointerChecking>(*DepChecker, SE, LoopGuards);
4108 if (canAnalyzeLoop())
4109 CanVecMem = analyzeLoop(AA, LI, TLI, DT);
4110}
4111
4112void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
4113 if (CanVecMem) {
4114 OS.indent(Depth) << "Memory dependences are safe";
4115 const MemoryDepChecker &DC = getDepChecker();
4116 if (!DC.isSafeForAnyVectorWidth())
4117 OS << " with a maximum safe vector width of "
4118 << DC.getMaxSafeVectorWidthInBits() << " bits";
4120 uint64_t SLDist = DC.getStoreLoadForwardSafeDistanceInBits();
4121 OS << ", with a maximum safe store-load forward width of " << SLDist
4122 << " bits";
4123 }
4124 if (PtrRtChecking->Need)
4125 OS << " with run-time checks";
4126 OS << "\n";
4127 }
4128
4129 if (HasConvergentOp)
4130 OS.indent(Depth) << "Has convergent operation in loop\n";
4131
4132 if (Report)
4133 OS.indent(Depth) << "Report: " << Report->getMsg() << "\n";
4134
4135 if (auto *Dependences = DepChecker->getDependences()) {
4136 OS.indent(Depth) << "Dependences:\n";
4137 for (const auto &Dep : *Dependences) {
4138 Dep.print(OS, Depth + 2, DepChecker->getMemoryInstructions());
4139 OS << "\n";
4140 }
4141 } else
4142 OS.indent(Depth) << "Too many dependences, not recorded\n";
4143
4144 // List the pair of accesses need run-time checks to prove independence.
4145 PtrRtChecking->print(OS, Depth);
4146 if (PtrRtChecking->Need && !HasCompletePtrRtChecking)
4147 OS.indent(Depth) << "Generated run-time checks are incomplete\n";
4148 OS << "\n";
4149
4150 OS.indent(Depth)
4151 << "Non vectorizable stores to invariant address were "
4152 << (HasStoreStoreDependenceInvolvingLoopInvariantAddress ||
4153 HasLoadStoreDependenceInvolvingLoopInvariantAddress
4154 ? ""
4155 : "not ")
4156 << "found in loop.\n";
4157
4158 OS.indent(Depth) << "SCEV assumptions:\n";
4159 PSE->getPredicate().print(OS, Depth);
4160
4161 OS << "\n";
4162
4163 OS.indent(Depth) << "Expressions re-written:\n";
4164 PSE->print(OS, Depth);
4165}
4166
4168 bool AllowPartial) {
4169 const auto &[It, Inserted] = LoopAccessInfoMap.try_emplace(&L);
4170
4171 // We need to create the LoopAccessInfo if either we don't already have one,
4172 // or if it was created with a different value of AllowPartial.
4173 if (Inserted || It->second->hasAllowPartial() != AllowPartial)
4174 It->second = std::make_unique<LoopAccessInfo>(&L, &SE, TTI, TLI, &AA, &DT,
4175 &LI, AC, AllowPartial);
4176
4177 return *It->second;
4178}
4180 // Collect LoopAccessInfo entries that may keep references to IR outside the
4181 // analyzed loop or SCEVs that may have been modified or invalidated. At the
4182 // moment, that is loops requiring memory or SCEV runtime checks, as those cache
4183 // SCEVs, e.g. for pointer expressions.
4184 LoopAccessInfoMap.remove_if([](const auto &Entry) {
4185 const auto &LAI = Entry.second;
4186 return !(LAI->getRuntimePointerChecking()->getChecks().empty() &&
4187 LAI->getPSE().getPredicate().isAlwaysTrue());
4188 });
4189}
4190
4192 Function &F, const PreservedAnalyses &PA,
4193 FunctionAnalysisManager::Invalidator &Inv) {
4194 // Check whether our analysis is preserved.
4195 auto PAC = PA.getChecker<LoopAccessAnalysis>();
4196 if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<Function>>())
4197 // If not, give up now.
4198 return true;
4199
4200 // Check whether the analyses we depend on became invalid for any reason.
4201 // Skip checking TargetLibraryAnalysis as it is immutable and can't become
4202 // invalid.
4203 return Inv.invalidate<AAManager>(F, PA) ||
4204 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
4205 Inv.invalidate<LoopAnalysis>(F, PA) ||
4206 Inv.invalidate<DominatorTreeAnalysis>(F, PA);
4207}
4208
4211 auto &SE = FAM.getResult<ScalarEvolutionAnalysis>(F);
4212 auto &AA = FAM.getResult<AAManager>(F);
4213 auto &DT = FAM.getResult<DominatorTreeAnalysis>(F);
4214 auto &LI = FAM.getResult<LoopAnalysis>(F);
4215 auto &TTI = FAM.getResult<TargetIRAnalysis>(F);
4216 auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
4217 auto &AC = FAM.getResult<AssumptionAnalysis>(F);
4218 return LoopAccessInfoManager(SE, AA, DT, LI, &TTI, &TLI, &AC);
4219}
4220
4221AnalysisKey LoopAccessAnalysis::Key;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
This file implements a class to represent arbitrary precision integral constant values and operations...
@ Scaled
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
CanMerge
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Forward Handle Accesses
DXIL Resource Access
dxil translate DXIL Translate Metadata
This file defines the DenseMap class.
Generic implementation of equivalence classes through the use Tarjan's efficient union-find algorithm...
#define DEBUG_TYPE
Hexagon Common GEP
#define _
This header defines various interfaces for pass management in LLVM.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static cl::opt< StencilMergePolicy > StencilMerge("stencil-runtime-check-merge", cl::Hidden, cl::desc("Control stencil-pattern merging of runtime memory checks"), cl::init(StencilMergePolicy::Off), cl::values(clEnumValN(StencilMergePolicy::Off, "off", "Disable stencil merge (default)"), clEnumValN(StencilMergePolicy::Auto, "auto", "Enable stencil merge when runtime check count exceeds " "-vectorize-memory-check-threshold"), clEnumValN(StencilMergePolicy::Force, "force", "Always attempt stencil merge regardless of check " "count")))
static cl::opt< unsigned > MaxDependences("max-dependences", cl::Hidden, cl::desc("Maximum number of dependences collected by " "loop-access analysis (default = 100)"), cl::init(100))
We collect dependences up to this threshold.
static bool isSafeDependenceDistance(ScalarEvolution &SE, const SCEV &MaxBTC, const SCEV &Dist, uint64_t MaxStride)
Given a dependence-distance Dist between two memory accesses, that have strides in the same direction...
static cl::opt< bool > EnableForwardingConflictDetection("store-to-load-forwarding-conflict-detection", cl::Hidden, cl::desc("Enable conflict detection in loop-access analysis"), cl::init(true))
Enable store-to-load forwarding conflict detection.
static bool evaluatePtrAddRecAtMaxBTCWillNotWrap(const SCEVAddRecExpr *AR, const SCEV *MaxBTC, const SCEV *EltSize, ScalarEvolution &SE, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC, ScalarEvolution::LoopGuards &LoopGuards)
Return true, if evaluating AR at MaxBTC cannot wrap, because AR at MaxBTC is guaranteed inbounds of t...
static void findForkedSCEVs(ScalarEvolution *SE, const Loop *L, Value *Ptr, SmallVectorImpl< PointerIntPair< const SCEV *, 1, bool > > &ScevList, unsigned Depth)
static const SCEV * mulSCEVNoOverflow(const SCEV *A, const SCEV *B, ScalarEvolution &SE)
Returns A * B, if it is guaranteed not to unsigned wrap.
static bool isNoWrap(PredicatedScalarEvolution &PSE, const SCEVAddRecExpr *AR, Value *Ptr, Type *AccessTy, const Loop *L, const DominatorTree &DT, std::optional< int64_t > Stride=std::nullopt, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Check whether AR is a non-wrapping AddRec.
static cl::opt< unsigned > MemoryCheckMergeThreshold("memory-check-merge-threshold", cl::Hidden, cl::desc("Maximum number of comparisons done when trying to merge " "runtime memory checks. (default = 100)"), cl::init(100))
The maximum iterations used to merge memory checks.
static RuntimeCheckingPtrGroup buildMergedStencilGroup(const RuntimePointerChecking &RtCheck, ArrayRef< unsigned > AllMembers, const SCEV *MergedLow, const SCEV *MergedHigh, ArrayRef< unsigned > GroupIndices)
Build the merged stencil group for one DepSet, after the cost model has decided the merge is profitab...
static bool isNeverAbove(const StencilDecomposition &A, const StencilDecomposition &B)
Return true if offset A is never higher than offset B.
static std::optional< APInt > getStencilStrideUpperLimit(const StencilDecomposition &D, unsigned BitWidth)
Find a common upper limit M for the positive strides in D.
static const SCEV * getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp)
Get the stride of a pointer access in a loop.
static bool isKnownNonDecreasingInLoop(const SCEV *S, const Loop *L, ScalarEvolution &SE)
Return true if S is known to be monotonically non-decreasing (in the unsigned sense,...
static cl::opt< ElementCount, true > VectorizationFactor("force-vector-width", cl::Hidden, cl::desc("Sets the SIMD width. Zero is autoselect."), cl::location(VectorizerParams::VectorizationFactor))
static cl::opt< unsigned, true > VectorizationInterleave("force-vector-interleave", cl::Hidden, cl::desc("Sets the vectorization interleave count. " "Zero is autoselect."), cl::location(VectorizerParams::VectorizationInterleave))
static cl::opt< unsigned > StencilMergeMaxGroups("stencil-merge-max-groups", cl::Hidden, cl::desc("Skip stencil group merging when the number of runtime checking groups " "exceeds this limit, to bound compile time (default =4096)."), cl::init(4096))
static cl::opt< bool, true > HoistRuntimeChecks("hoist-runtime-checks", cl::Hidden, cl::desc("Hoist inner loop runtime memory checks to outer loop if possible"), cl::location(VectorizerParams::HoistRuntimeChecks), cl::init(true))
static DenseMap< const RuntimeCheckingPtrGroup *, unsigned > getPtrToIdxMap(ArrayRef< RuntimeCheckingPtrGroup > CheckingGroups)
Assign each RuntimeCheckingPtrGroup pointer an index for stable UTC output.
static cl::opt< unsigned, true > RuntimeMemoryCheckThreshold("runtime-memory-check-threshold", cl::Hidden, cl::desc("When performing memory disambiguation checks at runtime do not " "generate more than this number of comparisons (default = 8)."), cl::location(VectorizerParams::RuntimeMemoryCheckThreshold), cl::init(8))
static void visitPointers(Value *StartPtr, const Loop &InnermostLoop, function_ref< void(Value *)> AddPointer)
constexpr unsigned MaxStencilDecomposeDepth
Recursion cap for addScaledStencilTerm.
static SmallVector< unsigned, 4 > collectCandidateMembers(ArrayRef< StencilDecomposition > Offsets, bool ForMin)
Find the members that can define the merged bound on one side.
static bool addScaledStencilTerm(const SCEV *Term, int64_t Mult, unsigned Depth, StencilDecomposition &D)
Add one term of a stencil offset to D.
static cl::opt< unsigned, true > VectorizeMemoryCheckThreshold("vectorize-memory-check-threshold", cl::Hidden, cl::desc("The maximum allowed number of runtime memory checks"), cl::location(VectorizerParams::VectorizeMemoryCheckThreshold), cl::init(128))
static bool areStridedAccessesIndependent(uint64_t Distance, uint64_t Stride, uint64_t TypeByteSize)
Check the dependence for two accesses with the same stride Stride.
static const SCEV * getMinFromExprs(const SCEV *I, const SCEV *J, ScalarEvolution *SE)
Compare I and J and return the minimum.
static bool collectStrideLimits(const StencilDecomposition &D, unsigned BitWidth, ScalarEvolution &SE, StrideLimits &Limits)
Add to Limits the checks each stride s of D needs: 1 <= s isNeverAbove assumes every stride is 1 or m...
static std::pair< unsigned, unsigned > computeStencilMergeCost(const RuntimePointerChecking &RtCheck, ArrayRef< unsigned > GroupIndices, const StrideLimits &Local, const StrideLimits &Committed, unsigned NumBoundOperands)
Local cost model: count the runtime checks required before and after replacing one DepSet's groups (G...
static std::pair< const SCEV *, const SCEV * > getNonAffineMonotonicBounds(const Loop *Lp, const SCEV *PtrExpr, const SCEV *EltSizeSCEV, ScalarEvolution *SE)
Try to bound a loop-variant pointer that is not an affine AddRec.
static Value * getLoopVariantGEPOperand(Value *Ptr, ScalarEvolution *SE, Loop *Lp)
If Ptr is a GEP, which has a loop-variant operand, return that operand.
static cl::opt< unsigned > MaxForkedSCEVDepth("max-forked-scev-depth", cl::Hidden, cl::desc("Maximum recursion depth when finding forked SCEVs (default = 5)"), cl::init(5))
static std::optional< StencilDecomposition > decomposeStencilOffset(const SCEV *Expr, ScalarEvolution &SE, const Loop &L)
Try to decompose Expr into a stencil offset function of loop-invariant strides: C + a1*s1 + a2*s2 + ....
static cl::opt< bool > SpeculateUnitStride("laa-speculate-unit-stride", cl::Hidden, cl::desc("Speculate that non-constant strides are unit in LAA"), cl::init(true))
static cl::opt< bool > EnableMemAccessVersioning("enable-mem-access-versioning", cl::init(true), cl::Hidden, cl::desc("Enable symbolic stride memory access versioning"))
This enables versioning on the strides of symbolically striding memory accesses in code like the foll...
static const SCEV * addSCEVNoOverflow(const SCEV *A, const SCEV *B, ScalarEvolution &SE)
Returns A + B, if it is guaranteed not to unsigned wrap.
This header provides classes for managing per-loop analyses.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file implements a map that provides insertion order iteration.
This file provides utility analysis objects describing memory locations.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
FunctionAnalysisManager FAM
This file defines the PointerIntPair class.
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
static const X86InstrFMA3Group Groups[]
A manager for alias analyses.
Class for arbitrary precision integers.
Definition APInt.h:78
std::optional< uint64_t > tryZExtValue() const
Get zero extended value if possible.
Definition APInt.h:1572
APInt abs() const
Get the absolute value.
Definition APInt.h:1815
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1086
std::optional< int64_t > trySExtValue() const
Get sign extended value if possible.
Definition APInt.h:1594
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:482
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
bool isConvergent() const
Determine if the invoke is convergent.
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
static LLVM_ABI Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
bool isNegative() const
Definition Constants.h:214
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A debug info location.
Definition DebugLoc.h:126
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:794
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
iterator_range< member_iterator > members(const ECValue &ECV) const
bool contains(const ElemTy &V) const
Returns true if V is contained an equivalence class.
const ECValue & insert(const ElemTy &Data)
Insert a new value into the union/find set, ignoring the request if the value already exists.
member_iterator member_end() const
const ElemTy & getLeaderValue(const ElemTy &V) const
Return the leader for the specified value that is in the set.
member_iterator findLeader(const ElemTy &V) const
Given a value in the set, return a member iterator for the equivalence class it is in.
void eraseClass(const ElemTy &V)
Erase the class containing V, i.e.
member_iterator unionSets(const ElemTy &V1, const ElemTy &V2)
Merge the two equivalence sets for the specified values, inserting them if they do not already exist ...
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition Function.h:699
bool empty() const
Definition Function.h:844
PointerType * getType() const
Global values are always pointers.
An instruction for reading from memory.
Value * getPointerOperand()
static constexpr LocationSize beforeOrAfterPointer()
Any location before or after the base pointer (but still within the underlying object).
This analysis provides dependence information for the memory accesses of a loop.
LLVM_ABI Result run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
LLVM_ABI const LoopAccessInfo & getInfo(Loop &L, bool AllowPartial=false)
Drive the analysis of memory accesses in the loop.
const MemoryDepChecker & getDepChecker() const
the Memory Dependence Checker which can determine the loop-independent and loop-carried dependences b...
LLVM_ABI bool isInvariant(Value *V) const
Returns true if value V is loop invariant.
LLVM_ABI void print(raw_ostream &OS, unsigned Depth=0) const
Print the information about the memory accesses in the loop.
static LLVM_ABI bool blockNeedsPredication(const BasicBlock *BB, const Loop *TheLoop, const DominatorTree *DT)
Return true if the block BB needs to be predicated in order for the loop to be vectorized.
LLVM_ABI LoopAccessInfo(Loop *L, ScalarEvolution *SE, const TargetTransformInfo *TTI, const TargetLibraryInfo *TLI, AAResults *AA, DominatorTree *DT, LoopInfo *LI, AssumptionCache *AC, bool AllowPartial=false)
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:594
bool contains(const LoopT *L) const
Return true if the specified loop is contained within this loop.
bool isInnermost() const
Return true if the loop does not contain any (natural) loops.
unsigned getNumBackEdges() const
Calculate the number of back edges to the loop header.
BlockT * getHeader() const
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
std::string getLocStr() const
Return a string containing the debug location of the loop (file name + line number if present,...
Definition LoopInfo.cpp:730
bool isAnnotatedParallel() const
Returns true if the loop is annotated parallel.
Definition LoopInfo.cpp:628
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
Definition LoopInfo.cpp:695
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1435
Checks memory dependences among accesses to the same underlying object to determine whether there vec...
ArrayRef< unsigned > getOrderForAccess(Value *Ptr, bool IsWrite) const
Return the program order indices for the access location (Ptr, IsWrite).
bool isSafeForAnyStoreLoadForwardDistances() const
Return true if there are no store-load forwarding dependencies.
LLVM_ABI bool areDepsSafe(const DepCandidates &AccessSets, ArrayRef< MemAccessInfo > CheckDeps)
Check whether the dependencies between the accesses are safe, and records the dependence information ...
bool isSafeForAnyVectorWidth() const
Return true if the number of elements that are safe to operate on simultaneously is not bounded.
static bool isStoreLoadForwardingConflict(uint64_t Distance, uint64_t VectorStoreSize, uint64_t TypeByteSize, uint64_t LoadElementSize=0)
Returns true if a memory dependence at byte distance Distance between a store (with element size Type...
PointerIntPair< Value *, 1, bool > MemAccessInfo
EquivalenceClasses< MemAccessInfo > DepCandidates
Set of potential dependent memory accesses.
bool shouldRetryWithRuntimeChecks() const
In same cases when the dependency check fails we can still vectorize the loop with a dynamic array ac...
const Loop * getInnermostLoop() const
uint64_t getMaxSafeVectorWidthInBits() const
Return the number of elements that are safe to operate on simultaneously, multiplied by the size of t...
bool isSafeForVectorization() const
No memory dependence was encountered that would inhibit vectorization.
const SmallVectorImpl< Dependence > * getDependences() const
Returns the memory dependences.
LLVM_ABI SmallVector< Instruction *, 4 > getInstructionsForAccess(Value *Ptr, bool isWrite) const
Find the set of instructions that read or write via Ptr.
VectorizationSafetyStatus
Type to keep track of the status of the dependence check.
LLVM_ABI void addAccess(StoreInst *SI)
Register the location (instructions are given increasing numbers) of a write access.
uint64_t getStoreLoadForwardSafeDistanceInBits() const
Return safe power-of-2 number of elements, which do not prevent store-load forwarding,...
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
AAMDNodes AATags
The metadata nodes which describes the aliasing of the location (each member is null if that kind of ...
const Value * Ptr
The address of the start of the location.
Diagnostic information for optimization analysis remarks.
PointerIntPair - This class implements a pair of a pointer and small integer.
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
LLVM_ABI void addPredicate(const SCEVPredicate &Pred)
Adds a new predicate.
ScalarEvolution * getSE() const
Returns the ScalarEvolution analysis used.
LLVM_ABI const SCEVPredicate & getPredicate() const
LLVM_ABI const SCEVAddRecExpr * getAsAddRec(Value *V, SmallVectorImpl< const SCEVPredicate * > *WrapPredsAdded=nullptr)
Attempts to produce an AddRecExpr for V by adding additional SCEV predicates.
LLVM_ABI void addPredicates(ArrayRef< const SCEVPredicate * > Preds)
Adds all predicates in Preds.
LLVM_ABI const SCEV * getBackedgeTakenCount()
Get the (predicated) backedge count for the analyzed loop.
LLVM_ABI const SCEV * getSymbolicMaxBackedgeTakenCount()
Get the (predicated) symbolic max backedge count for the analyzed loop.
LLVM_ABI const SCEV * getSCEV(Value *V)
Returns the SCEV expression of V, in the context of the current SCEV predicate.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
Holds information about the memory runtime legality checks to verify that a group of pointers do not ...
bool Need
This flag indicates if we need to add the runtime check.
void reset()
Reset the state of the pointer runtime information.
unsigned getNumberOfChecks() const
Returns the number of run-time checks required according to needsChecking.
LLVM_ABI void printChecks(raw_ostream &OS, const SmallVectorImpl< RuntimePointerCheck > &Checks, unsigned Depth=0) const
Print Checks.
LLVM_ABI bool insert(Loop *Lp, Value *Ptr, const SCEV *PtrExpr, Type *AccessTy, bool WritePtr, unsigned DepSetId, unsigned ASId, PredicatedScalarEvolution &PSE, bool NeedsFreeze, bool IsForked)
Insert a pointer and calculate the start and end SCEVs.
LLVM_ABI bool needsChecking(const RuntimeCheckingPtrGroup &M, const RuntimeCheckingPtrGroup &N) const
Decide if we need to add a check between two groups of pointers, according to needsChecking.
LLVM_ABI void print(raw_ostream &OS, unsigned Depth=0) const
Print the list run-time memory checks necessary.
SmallVector< RuntimeCheckingPtrGroup, 2 > CheckingGroups
Holds a partitioning of pointers into "check groups".
static LLVM_ABI bool arePointersInSamePartition(const SmallVectorImpl< int > &PtrToPartition, unsigned PtrIdx1, unsigned PtrIdx2)
Check if pointers are in the same partition.
LLVM_ABI void generateChecks(MemoryDepChecker::DepCandidates &DepCands)
Generate the checks and store it.
SmallVector< PointerInfo, 2 > Pointers
Information about the pointers that may require checking.
This node represents a polynomial recurrence on the trip count of the specified loop.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
SCEVUse getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
This class represents a constant integer value.
ConstantInt * getValue() const
const APInt & getAPInt() const
SCEVFlags getNoWrapFlags(SCEVFlags Mask=FlagsNoWrapMask) const
This class represents an assumption made using SCEV expressions which can be checked at run-time.
virtual bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const =0
Returns true if this predicate implies N.
This class represents a composition of other SCEV predicates, and is the class that most clients will...
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
This class represents an analyzed expression in the program.
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
Type * getType() const
Return the LLVM type of this SCEV expression.
SCEVTypes getSCEVType() const
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
const SCEV * getConstantMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEVConstant that is greater than or equal to (i.e.
LLVM_ABI bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
LLVM_ABI const SCEV * getZeroExtendExpr(SCEVUse Op, Type *Ty, unsigned Depth=0)
LLVM_ABI Type * getWiderType(Type *Ty1, Type *Ty2) const
LLVM_ABI const SCEV * getAbsExpr(const SCEV *Op, bool IsNSW)
LLVM_ABI bool isKnownNonPositive(const SCEV *S)
Test if the given expression is known to be non-positive.
LLVM_ABI bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI bool willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI=nullptr)
Is operation BinOp between LHS and RHS provably does not have a signed/unsigned overflow (Signed)?
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEVFlags Flags=SCEV::FlagNone, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI const SCEVPredicate * getEqualPredicate(const SCEV *LHS, const SCEV *RHS)
LLVM_ABI SCEVUse getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
LLVM_ABI const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI bool isKnownPositive(const SCEV *S)
Test if the given expression is known to be positive.
LLVM_ABI SCEVUse getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI Type * getEffectiveSCEVType(Type *Ty) const
Return a type with the same bitwidth as the given type and which represents how SCEV will treat the g...
LLVM_ABI const SCEVPredicate * getComparePredicate(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
APInt getSignedRangeMin(const SCEV *S)
Determine the min of the signed range for a particular SCEV.
LLVM_ABI const SCEV * getUMaxExpr(SCEVUse LHS, SCEVUse RHS)
LLVM_ABI const SCEV * getStoreSizeOfExpr(Type *IntTy, Type *StoreTy)
Return an expression for the store size of StoreTy that is type IntTy.
LLVM_ABI const SCEVPredicate * getWrapPredicate(const SCEVAddRecExpr *AR, SCEVWrapPredicate::IncrementWrapFlags AddedFlags)
LLVM_ABI const SCEV * getNoopOrZeroExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI std::optional< MonotonicPredicateType > getMonotonicPredicateType(const SCEVAddRecExpr *LHS, ICmpInst::Predicate Pred)
If, for all loop invariant X, the predicate "LHS `Pred` X" is monotonically increasing or decreasing,...
LLVM_ABI const SCEV * getCouldNotCompute()
LLVM_ABI SCEVUse getMulExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI const SCEV * getPointerBase(const SCEV *V)
Transitively follow the chain of pointer-type operands until reaching a SCEV that does not have a sin...
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
LLVM_ABI const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
LLVM_ABI const SCEV * getPtrToAddrExpr(const SCEV *Op)
LLVM_ABI const SCEVAddRecExpr * convertSCEVToAddRecWithPredicates(const SCEV *S, const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Preds)
Tries to convert the S expression to an AddRec expression, adding additional predicates to Preds as r...
LLVM_ABI const SCEV * getSizeOfExpr(Type *IntTy, TypeSize Size)
Return an expression for a TypeSize.
LLVM_ABI std::optional< APInt > computeConstantDifference(const SCEV *LHS, const SCEV *RHS)
Compute LHS - RHS and returns the result as an APInt if it is a constant, and std::nullopt if it isn'...
LLVM_ABI const SCEV * getNegativeSCEV(const SCEV *V, SCEVFlags Flags=SCEV::FlagNone)
Return the SCEV object corresponding to -V.
LLVM_ABI const SCEV * rewriteUsingPredicate(const SCEV *S, const Loop *L, const SCEVPredicate &A)
Re-writes the SCEV according to the Predicates in A.
LLVM_ABI std::pair< const SCEV *, const SCEV * > SplitIntoInitAndPostInc(const Loop *L, const SCEV *S)
Splits SCEV expression S into two SCEVs.
LLVM_ABI const SCEV * getUMinExpr(SCEVUse LHS, SCEVUse RHS, bool Sequential=false)
LLVM_ABI const SCEV * getTruncateOrSignExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
void insert_range(Range &&R)
Definition SetVector.h:182
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:345
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition SmallSet.h:229
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:283
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
static SmallVector< VFInfo, 8 > getMappings(const CallInst &CI)
Retrieve all the VFInfo instances associated to the CallInst CI.
Definition VectorUtils.h:76
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
LLVM_ABI 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:918
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:209
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:182
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:216
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
@ Entry
Definition COFF.h:862
bool match(Val *V, const Pattern &P)
bind_cst_ty m_scev_APInt(const APInt *&C)
Match an SCEV constant and bind it to an APInt.
is_undef_or_poison m_scev_UndefOrPoison()
Match an SCEVUnknown wrapping undef or poison.
specificloop_ty m_SpecificLoop(const Loop *L)
match_bind< const SCEVMulExpr > m_scev_Mul(const SCEVMulExpr *&V)
specificscev_ty m_scev_Specific(const SCEV *S)
Match if we have a specific specified SCEV.
SCEVAffineAddRec_match< Op0_t, Op1_t, match_isa< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
DiagnosticInfoOptimizationBase::Argument NV
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:577
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition STLExtras.h:846
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
LLVM_ABI RetainedKnowledge getKnowledgeForValue(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, 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...
LLVM_ABI bool getBooleanLoopAttribute(const Loop *TheLoop, StringRef Name)
Returns true if Name is applied to TheLoop and enabled.
LLVM_ABI Intrinsic::ID getVectorIntrinsicIDForCall(const CallInst *CI, const TargetLibraryInfo *TLI)
Returns intrinsic ID for call.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2570
unsigned getPointerAddressSpace(const Type *T)
Definition SPIRVUtils.h:393
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
LLVM_ABI const SCEV * replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE, const Loop *Lp, const SymbolicStrideMap &PtrToStride, Value *Ptr, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Return the SCEV corresponding to a pointer with the symbolic stride replaced with constant one,...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
constexpr std::enable_if_t< std::is_signed_v< T >, std::pair< T, bool > > AddOverflow(T X, T Y)
Add two signed integers, computing the two's complement truncated result, returning a pair {result,...
Definition MathExtras.h:698
LLVM_ABI std::optional< int64_t > getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr, const Loop *Lp, const DominatorTree &DT, const SymbolicStrideMap &StridesMap=SymbolicStrideMap(), bool ShouldCheckWrap=true, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
If the pointer has a constant stride return it in units of the access type size.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CtxI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
Definition STLExtras.h:2042
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
DenseMap< Value *, const SCEVUnknown * > SymbolicStrideMap
Maps a pointer to its symbolic (non-constant) stride.
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI std::optional< int64_t > getPointersDiff(Type *ElemTyA, Value *PtrA, Type *ElemTyB, Value *PtrB, const DataLayout &DL, ScalarEvolution &SE, bool StrictCheck=false, bool CheckType=true)
Returns the distance between the pointers PtrA and PtrB iff they are compatible and it is possible to...
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition STLExtras.h:552
LLVM_ABI bool sortPtrAccesses(ArrayRef< Value * > VL, Type *ElemTy, const DataLayout &DL, ScalarEvolution &SE, SmallVectorImpl< unsigned > &SortedIndices)
Attempt to sort the pointers in VL and return the sorted indices in SortedIndices,...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
TargetTransformInfo TTI
@ Add
Sum of integers.
LLVM_ABI bool isConsecutiveAccess(Value *A, Value *B, const DataLayout &DL, ScalarEvolution &SE, bool CheckType=true)
Returns true if the memory operations A and B are consecutive.
IntPtrTy
Definition InstrProf.h:82
DWARFExpression::Operation Op
auto max_element(R &&Range)
Provide wrappers to std::max_element which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2104
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr U AbsoluteValue(T X)
Return the absolute value of a signed integer, converted to the corresponding unsigned integer type.
Definition MathExtras.h:587
constexpr int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition MathExtras.h:233
constexpr unsigned BitWidth
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:2035
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
Type * getLoadStoreType(const Value *I)
A helper function that returns the type of a load or store instruction.
constexpr std::enable_if_t< std::is_signed_v< T >, std::pair< T, bool > > MulOverflow(T X, T Y)
Multiply two signed integers, computing the two's complement truncated result, returning a pair {resu...
Definition MathExtras.h:772
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI std::optional< int64_t > getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy, Value *Ptr, PredicatedScalarEvolution &PSE)
If AR is an affine AddRec for Lp with a constant step, return the step in units of AccessTy's allocat...
T bit_floor(T Value)
Returns the largest integral power of two no greater than Value if Value is nonzero.
Definition bit.h:347
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:267
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI std::pair< const SCEV *, const SCEV * > getStartAndEndForAccess(const Loop *Lp, const SCEV *PtrExpr, Type *AccessTy, const SCEV *BTC, const SCEV *MaxBTC, ScalarEvolution *SE, DenseMap< std::pair< const SCEV *, const SCEV * >, std::pair< const SCEV *, const SCEV * > > *PointerBounds, DominatorTree *DT, AssumptionCache *AC, ScalarEvolution::LoopGuards &LoopGuards)
Calculate Start and End points of memory access using exact backedge taken count BTC if computable or...
@ Auto
Determine whether to use color based on the command line argument and the raw_ostream.
Definition WithColor.h:43
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define N
IR Values for the lower and upper bounds of a pointer evolution.
Result of decomposing a SCEV expression into stencil offset form: Offset = Constant + sum(Coefficient...
SmallMapVector< const SCEV *, int64_t, 4 > Coefficients
Map from loop-invariant stride SCEV to its integer coefficient.
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition Metadata.h:797
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:791
MDNode * NoAlias
The tag specifying the noalias scope.
Definition Metadata.h:800
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
Instruction * getDestination(const MemoryDepChecker &DepChecker) const
Return the destination instruction of the dependence.
DepType Type
The type of the dependence.
unsigned Destination
Index of the destination of the dependence in the InstMap vector.
LLVM_ABI bool isPossiblyBackward() const
May be a lexically backward dependence type (includes Unknown).
Instruction * getSource(const MemoryDepChecker &DepChecker) const
Return the source instruction of the dependence.
LLVM_ABI bool isForward() const
Lexically forward dependence.
LLVM_ABI bool isBackward() const
Lexically backward dependence.
LLVM_ABI void print(raw_ostream &OS, unsigned Depth, const SmallVectorImpl< Instruction * > &Instrs) const
Print the dependence.
unsigned Source
Index of the source of the dependence in the InstMap vector.
DepType
The type of the dependence.
static LLVM_ABI const char * DepName[]
String version of the types.
static LLVM_ABI VectorizationSafetyStatus isSafeForVectorization(DepType Type)
Dependence types that don't prevent vectorization.
Represent one information held inside an operand bundle of an llvm.assume.
unsigned AddressSpace
Address space of the involved pointers.
LLVM_ABI bool addPointer(unsigned Index, const RuntimePointerChecking &RtCheck)
Tries to add the pointer recorded in RtCheck at index Index to this pointer checking group.
bool NeedsFreeze
Whether the pointer needs to be frozen after expansion, e.g.
LLVM_ABI RuntimeCheckingPtrGroup(unsigned Index, const RuntimePointerChecking &RtCheck)
Create a new pointer checking group containing a single pointer, with index Index in RtCheck.
const SCEV * High
The SCEV expression which represents the upper bound of all the pointers in this group.
SmallVector< unsigned, 2 > Members
Indices of all the pointers that constitute this grouping.
const SCEV * Low
The SCEV expression which represents the lower bound of all the pointers in this group.
bool IsWritePtr
Holds the information if this pointer is used for writing to memory.
unsigned DependencySetId
Holds the id of the set of pointers that could be dependent because of a shared underlying object.
unsigned AliasSetId
Holds the id of the disjoint alias set to which this pointer belongs.
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:342
static LLVM_ABI const unsigned MaxVectorWidth
Maximum SIMD width.
static LLVM_ABI unsigned VectorizeMemoryCheckThreshold
The maximum allowed number of runtime memory checks.
static LLVM_ABI unsigned RuntimeMemoryCheckThreshold
\When performing memory disambiguation checks at runtime do not make more than this number of compari...
static LLVM_ABI bool isInterleaveForced()
True if force-vector-interleave was specified by the user.
static LLVM_ABI unsigned VectorizationInterleave
Interleave factor as overridden by the user.
static LLVM_ABI ElementCount VectorizationFactor
VF as overridden by the user.
static LLVM_ABI bool HoistRuntimeChecks
Function object to check whether the first component of a container supported by std::get (like std::...
Definition STLExtras.h:1455