LLVM 19.0.0git
LoopAccessAnalysis.h
Go to the documentation of this file.
1//===- llvm/Analysis/LoopAccessAnalysis.h -----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the interface for the loop memory dependence framework that
10// was originally developed for the Loop Vectorizer.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
15#define LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
16
21#include <optional>
22#include <variant>
23
24namespace llvm {
25
26class AAResults;
27class DataLayout;
28class Loop;
29class LoopAccessInfo;
30class raw_ostream;
31class SCEV;
32class SCEVUnionPredicate;
33class Value;
34
35/// Collection of parameters shared beetween the Loop Vectorizer and the
36/// Loop Access Analysis.
38 /// Maximum SIMD width.
39 static const unsigned MaxVectorWidth;
40
41 /// VF as overridden by the user.
42 static unsigned VectorizationFactor;
43 /// Interleave factor as overridden by the user.
44 static unsigned VectorizationInterleave;
45 /// True if force-vector-interleave was specified by the user.
46 static bool isInterleaveForced();
47
48 /// \When performing memory disambiguation checks at runtime do not
49 /// make more than this number of comparisons.
51
52 // When creating runtime checks for nested loops, where possible try to
53 // write the checks in a form that allows them to be easily hoisted out of
54 // the outermost loop. For example, we can do this by expanding the range of
55 // addresses considered to include the entire nested loop so that they are
56 // loop invariant.
57 static bool HoistRuntimeChecks;
58};
59
60/// Checks memory dependences among accesses to the same underlying
61/// object to determine whether there vectorization is legal or not (and at
62/// which vectorization factor).
63///
64/// Note: This class will compute a conservative dependence for access to
65/// different underlying pointers. Clients, such as the loop vectorizer, will
66/// sometimes deal these potential dependencies by emitting runtime checks.
67///
68/// We use the ScalarEvolution framework to symbolically evalutate access
69/// functions pairs. Since we currently don't restructure the loop we can rely
70/// on the program order of memory accesses to determine their safety.
71/// At the moment we will only deem accesses as safe for:
72/// * A negative constant distance assuming program order.
73///
74/// Safe: tmp = a[i + 1]; OR a[i + 1] = x;
75/// a[i] = tmp; y = a[i];
76///
77/// The latter case is safe because later checks guarantuee that there can't
78/// be a cycle through a phi node (that is, we check that "x" and "y" is not
79/// the same variable: a header phi can only be an induction or a reduction, a
80/// reduction can't have a memory sink, an induction can't have a memory
81/// source). This is important and must not be violated (or we have to
82/// resort to checking for cycles through memory).
83///
84/// * A positive constant distance assuming program order that is bigger
85/// than the biggest memory access.
86///
87/// tmp = a[i] OR b[i] = x
88/// a[i+2] = tmp y = b[i+2];
89///
90/// Safe distance: 2 x sizeof(a[0]), and 2 x sizeof(b[0]), respectively.
91///
92/// * Zero distances and all accesses have the same size.
93///
95public:
98 /// Set of potential dependent memory accesses.
100
101 /// Type to keep track of the status of the dependence check. The order of
102 /// the elements is important and has to be from most permissive to least
103 /// permissive.
105 // Can vectorize safely without RT checks. All dependences are known to be
106 // safe.
107 Safe,
108 // Can possibly vectorize with RT checks to overcome unknown dependencies.
110 // Cannot vectorize due to known unsafe dependencies.
111 Unsafe,
112 };
113
114 /// Dependece between memory access instructions.
115 struct Dependence {
116 /// The type of the dependence.
117 enum DepType {
118 // No dependence.
120 // We couldn't determine the direction or the distance.
122 // At least one of the memory access instructions may access a loop
123 // varying object, e.g. the address of underlying object is loaded inside
124 // the loop, like A[B[i]]. We cannot determine direction or distance in
125 // those cases, and also are unable to generate any runtime checks.
127
128 // Lexically forward.
129 //
130 // FIXME: If we only have loop-independent forward dependences (e.g. a
131 // read and write of A[i]), LAA will locally deem the dependence "safe"
132 // without querying the MemoryDepChecker. Therefore we can miss
133 // enumerating loop-independent forward dependences in
134 // getDependences. Note that as soon as there are different
135 // indices used to access the same array, the MemoryDepChecker *is*
136 // queried and the dependence list is complete.
138 // Forward, but if vectorized, is likely to prevent store-to-load
139 // forwarding.
141 // Lexically backward.
143 // Backward, but the distance allows a vectorization factor of dependent
144 // on MinDepDistBytes.
146 // Same, but may prevent store-to-load forwarding.
148 };
149
150 /// String version of the types.
151 static const char *DepName[];
152
153 /// Index of the source of the dependence in the InstMap vector.
154 unsigned Source;
155 /// Index of the destination of the dependence in the InstMap vector.
156 unsigned Destination;
157 /// The type of the dependence.
159
162
163 /// Return the source instruction of the dependence.
164 Instruction *getSource(const MemoryDepChecker &DepChecker) const;
165 /// Return the destination instruction of the dependence.
166 Instruction *getDestination(const MemoryDepChecker &DepChecker) const;
167
168 /// Dependence types that don't prevent vectorization.
170
171 /// Lexically forward dependence.
172 bool isForward() const;
173 /// Lexically backward dependence.
174 bool isBackward() const;
175
176 /// May be a lexically backward dependence type (includes Unknown).
177 bool isPossiblyBackward() const;
178
179 /// Print the dependence. \p Instr is used to map the instruction
180 /// indices to instructions.
181 void print(raw_ostream &OS, unsigned Depth,
182 const SmallVectorImpl<Instruction *> &Instrs) const;
183 };
184
186 const DenseMap<Value *, const SCEV *> &SymbolicStrides,
187 unsigned MaxTargetVectorWidthInBits)
188 : PSE(PSE), InnermostLoop(L), SymbolicStrides(SymbolicStrides),
189 MaxTargetVectorWidthInBits(MaxTargetVectorWidthInBits) {}
190
191 /// Register the location (instructions are given increasing numbers)
192 /// of a write access.
193 void addAccess(StoreInst *SI);
194
195 /// Register the location (instructions are given increasing numbers)
196 /// of a write access.
197 void addAccess(LoadInst *LI);
198
199 /// Check whether the dependencies between the accesses are safe.
200 ///
201 /// Only checks sets with elements in \p CheckDeps.
202 bool areDepsSafe(DepCandidates &AccessSets, MemAccessInfoList &CheckDeps,
204 &UnderlyingObjects);
205
206 /// No memory dependence was encountered that would inhibit
207 /// vectorization.
210 }
211
212 /// Return true if the number of elements that are safe to operate on
213 /// simultaneously is not bounded.
215 return MaxSafeVectorWidthInBits == UINT_MAX;
216 }
217
218 /// Return the number of elements that are safe to operate on
219 /// simultaneously, multiplied by the size of the element in bits.
221 return MaxSafeVectorWidthInBits;
222 }
223
224 /// In same cases when the dependency check fails we can still
225 /// vectorize the loop with a dynamic array access check.
227 return FoundNonConstantDistanceDependence &&
229 }
230
231 /// Returns the memory dependences. If null is returned we exceeded
232 /// the MaxDependences threshold and this information is not
233 /// available.
235 return RecordDependences ? &Dependences : nullptr;
236 }
237
238 void clearDependences() { Dependences.clear(); }
239
240 /// The vector of memory access instructions. The indices are used as
241 /// instruction identifiers in the Dependence class.
243 return InstMap;
244 }
245
246 /// Generate a mapping between the memory instructions and their
247 /// indices according to program order.
250
251 for (unsigned I = 0; I < InstMap.size(); ++I)
252 OrderMap[InstMap[I]] = I;
253
254 return OrderMap;
255 }
256
257 /// Find the set of instructions that read or write via \p Ptr.
259 bool isWrite) const;
260
261 /// Return the program order indices for the access location (Ptr, IsWrite).
262 /// Returns an empty ArrayRef if there are no accesses for the location.
264 auto I = Accesses.find({Ptr, IsWrite});
265 if (I != Accesses.end())
266 return I->second;
267 return {};
268 }
269
270 const Loop *getInnermostLoop() const { return InnermostLoop; }
271
272private:
273 /// A wrapper around ScalarEvolution, used to add runtime SCEV checks, and
274 /// applies dynamic knowledge to simplify SCEV expressions and convert them
275 /// to a more usable form. We need this in case assumptions about SCEV
276 /// expressions need to be made in order to avoid unknown dependences. For
277 /// example we might assume a unit stride for a pointer in order to prove
278 /// that a memory access is strided and doesn't wrap.
280 const Loop *InnermostLoop;
281
282 /// Reference to map of pointer values to
283 /// their stride symbols, if they have a symbolic stride.
284 const DenseMap<Value *, const SCEV *> &SymbolicStrides;
285
286 /// Maps access locations (ptr, read/write) to program order.
288
289 /// Memory access instructions in program order.
291
292 /// The program order index to be used for the next instruction.
293 unsigned AccessIdx = 0;
294
295 /// The smallest dependence distance in bytes in the loop. This may not be
296 /// the same as the maximum number of bytes that are safe to operate on
297 /// simultaneously.
298 uint64_t MinDepDistBytes = 0;
299
300 /// Number of elements (from consecutive iterations) that are safe to
301 /// operate on simultaneously, multiplied by the size of the element in bits.
302 /// The size of the element is taken from the memory access that is most
303 /// restrictive.
304 uint64_t MaxSafeVectorWidthInBits = -1U;
305
306 /// If we see a non-constant dependence distance we can still try to
307 /// vectorize this loop with runtime checks.
308 bool FoundNonConstantDistanceDependence = false;
309
310 /// Result of the dependence checks, indicating whether the checked
311 /// dependences are safe for vectorization, require RT checks or are known to
312 /// be unsafe.
314
315 //// True if Dependences reflects the dependences in the
316 //// loop. If false we exceeded MaxDependences and
317 //// Dependences is invalid.
318 bool RecordDependences = true;
319
320 /// Memory dependences collected during the analysis. Only valid if
321 /// RecordDependences is true.
322 SmallVector<Dependence, 8> Dependences;
323
324 /// The maximum width of a target's vector registers multiplied by 2 to also
325 /// roughly account for additional interleaving. Is used to decide if a
326 /// backwards dependence with non-constant stride should be classified as
327 /// backwards-vectorizable or unknown (triggering a runtime check).
328 unsigned MaxTargetVectorWidthInBits = 0;
329
330 /// Check whether there is a plausible dependence between the two
331 /// accesses.
332 ///
333 /// Access \p A must happen before \p B in program order. The two indices
334 /// identify the index into the program order map.
335 ///
336 /// This function checks whether there is a plausible dependence (or the
337 /// absence of such can't be proved) between the two accesses. If there is a
338 /// plausible dependence but the dependence distance is bigger than one
339 /// element access it records this distance in \p MinDepDistBytes (if this
340 /// distance is smaller than any other distance encountered so far).
341 /// Otherwise, this function returns true signaling a possible dependence.
343 isDependent(const MemAccessInfo &A, unsigned AIdx, const MemAccessInfo &B,
344 unsigned BIdx,
346 &UnderlyingObjects);
347
348 /// Check whether the data dependence could prevent store-load
349 /// forwarding.
350 ///
351 /// \return false if we shouldn't vectorize at all or avoid larger
352 /// vectorization factors by limiting MinDepDistBytes.
353 bool couldPreventStoreLoadForward(uint64_t Distance, uint64_t TypeByteSize);
354
355 /// Updates the current safety status with \p S. We can go from Safe to
356 /// either PossiblySafeWithRtChecks or Unsafe and from
357 /// PossiblySafeWithRtChecks to Unsafe.
358 void mergeInStatus(VectorizationSafetyStatus S);
359
360 struct DepDistanceStrideAndSizeInfo {
361 const SCEV *Dist;
362 uint64_t StrideA;
363 uint64_t StrideB;
364 uint64_t TypeByteSize;
365 bool AIsWrite;
366 bool BIsWrite;
367
368 DepDistanceStrideAndSizeInfo(const SCEV *Dist, uint64_t StrideA,
369 uint64_t StrideB, uint64_t TypeByteSize,
370 bool AIsWrite, bool BIsWrite)
371 : Dist(Dist), StrideA(StrideA), StrideB(StrideB),
372 TypeByteSize(TypeByteSize), AIsWrite(AIsWrite), BIsWrite(BIsWrite) {}
373 };
374
375 /// Get the dependence distance, strides, type size and whether it is a write
376 /// for the dependence between A and B. Returns a DepType, if we can prove
377 /// there's no dependence or the analysis fails. Outlined to lambda to limit
378 /// he scope of various temporary variables, like A/BPtr, StrideA/BPtr and
379 /// others. Returns either the dependence result, if it could already be
380 /// determined, or a struct containing (Distance, Stride, TypeSize, AIsWrite,
381 /// BIsWrite).
382 std::variant<Dependence::DepType, DepDistanceStrideAndSizeInfo>
383 getDependenceDistanceStrideAndSize(
384 const MemAccessInfo &A, Instruction *AInst, const MemAccessInfo &B,
385 Instruction *BInst,
386 const DenseMap<Value *, SmallVector<const Value *, 16>>
387 &UnderlyingObjects);
388};
389
390class RuntimePointerChecking;
391/// A grouping of pointers. A single memcheck is required between
392/// two groups.
394 /// Create a new pointer checking group containing a single
395 /// pointer, with index \p Index in RtCheck.
397
398 /// Tries to add the pointer recorded in RtCheck at index
399 /// \p Index to this pointer checking group. We can only add a pointer
400 /// to a checking group if we will still be able to get
401 /// the upper and lower bounds of the check. Returns true in case
402 /// of success, false otherwise.
403 bool addPointer(unsigned Index, RuntimePointerChecking &RtCheck);
404 bool addPointer(unsigned Index, const SCEV *Start, const SCEV *End,
405 unsigned AS, bool NeedsFreeze, ScalarEvolution &SE);
406
407 /// The SCEV expression which represents the upper bound of all the
408 /// pointers in this group.
409 const SCEV *High;
410 /// The SCEV expression which represents the lower bound of all the
411 /// pointers in this group.
412 const SCEV *Low;
413 /// Indices of all the pointers that constitute this grouping.
415 /// Address space of the involved pointers.
416 unsigned AddressSpace;
417 /// Whether the pointer needs to be frozen after expansion, e.g. because it
418 /// may be poison outside the loop.
419 bool NeedsFreeze = false;
420};
421
422/// A memcheck which made up of a pair of grouped pointers.
423typedef std::pair<const RuntimeCheckingPtrGroup *,
426
430 unsigned AccessSize;
432
434 unsigned AccessSize, bool NeedsFreeze)
437};
438
439/// Holds information about the memory runtime legality checks to verify
440/// that a group of pointers do not overlap.
443
444public:
445 struct PointerInfo {
446 /// Holds the pointer value that we need to check.
448 /// Holds the smallest byte address accessed by the pointer throughout all
449 /// iterations of the loop.
450 const SCEV *Start;
451 /// Holds the largest byte address accessed by the pointer throughout all
452 /// iterations of the loop, plus 1.
453 const SCEV *End;
454 /// Holds the information if this pointer is used for writing to memory.
456 /// Holds the id of the set of pointers that could be dependent because of a
457 /// shared underlying object.
459 /// Holds the id of the disjoint alias set to which this pointer belongs.
460 unsigned AliasSetId;
461 /// SCEV for the access.
462 const SCEV *Expr;
463 /// True if the pointer expressions needs to be frozen after expansion.
465
467 bool IsWritePtr, unsigned DependencySetId, unsigned AliasSetId,
468 const SCEV *Expr, bool NeedsFreeze)
472 };
473
475 : DC(DC), SE(SE) {}
476
477 /// Reset the state of the pointer runtime information.
478 void reset() {
479 Need = false;
480 Pointers.clear();
481 Checks.clear();
482 }
483
484 /// Insert a pointer and calculate the start and end SCEVs.
485 /// We need \p PSE in order to compute the SCEV expression of the pointer
486 /// according to the assumptions that we've made during the analysis.
487 /// The method might also version the pointer stride according to \p Strides,
488 /// and add new predicates to \p PSE.
489 void insert(Loop *Lp, Value *Ptr, const SCEV *PtrExpr, Type *AccessTy,
490 bool WritePtr, unsigned DepSetId, unsigned ASId,
491 PredicatedScalarEvolution &PSE, bool NeedsFreeze);
492
493 /// No run-time memory checking is necessary.
494 bool empty() const { return Pointers.empty(); }
495
496 /// Generate the checks and store it. This also performs the grouping
497 /// of pointers to reduce the number of memchecks necessary.
498 void generateChecks(MemoryDepChecker::DepCandidates &DepCands,
499 bool UseDependencies);
500
501 /// Returns the checks that generateChecks created. They can be used to ensure
502 /// no read/write accesses overlap across all loop iterations.
504 return Checks;
505 }
506
507 // Returns an optional list of (pointer-difference expressions, access size)
508 // pairs that can be used to prove that there are no vectorization-preventing
509 // dependencies at runtime. There are is a vectorization-preventing dependency
510 // if any pointer-difference is <u VF * InterleaveCount * access size. Returns
511 // std::nullopt if pointer-difference checks cannot be used.
512 std::optional<ArrayRef<PointerDiffInfo>> getDiffChecks() const {
513 if (!CanUseDiffCheck)
514 return std::nullopt;
515 return {DiffChecks};
516 }
517
518 /// Decide if we need to add a check between two groups of pointers,
519 /// according to needsChecking.
521 const RuntimeCheckingPtrGroup &N) const;
522
523 /// Returns the number of run-time checks required according to
524 /// needsChecking.
525 unsigned getNumberOfChecks() const { return Checks.size(); }
526
527 /// Print the list run-time memory checks necessary.
528 void print(raw_ostream &OS, unsigned Depth = 0) const;
529
530 /// Print \p Checks.
533 unsigned Depth = 0) const;
534
535 /// This flag indicates if we need to add the runtime check.
536 bool Need = false;
537
538 /// Information about the pointers that may require checking.
540
541 /// Holds a partitioning of pointers into "check groups".
543
544 /// Check if pointers are in the same partition
545 ///
546 /// \p PtrToPartition contains the partition number for pointers (-1 if the
547 /// pointer belongs to multiple partitions).
548 static bool
550 unsigned PtrIdx1, unsigned PtrIdx2);
551
552 /// Decide whether we need to issue a run-time check for pointer at
553 /// index \p I and \p J to prove their independence.
554 bool needsChecking(unsigned I, unsigned J) const;
555
556 /// Return PointerInfo for pointer at index \p PtrIdx.
557 const PointerInfo &getPointerInfo(unsigned PtrIdx) const {
558 return Pointers[PtrIdx];
559 }
560
561 ScalarEvolution *getSE() const { return SE; }
562
563private:
564 /// Groups pointers such that a single memcheck is required
565 /// between two different groups. This will clear the CheckingGroups vector
566 /// and re-compute it. We will only group dependecies if \p UseDependencies
567 /// is true, otherwise we will create a separate group for each pointer.
568 void groupChecks(MemoryDepChecker::DepCandidates &DepCands,
569 bool UseDependencies);
570
571 /// Generate the checks and return them.
573
574 /// Try to create add a new (pointer-difference, access size) pair to
575 /// DiffCheck for checking groups \p CGI and \p CGJ. If pointer-difference
576 /// checks cannot be used for the groups, set CanUseDiffCheck to false.
577 bool tryToCreateDiffCheck(const RuntimeCheckingPtrGroup &CGI,
578 const RuntimeCheckingPtrGroup &CGJ);
579
581
582 /// Holds a pointer to the ScalarEvolution analysis.
583 ScalarEvolution *SE;
584
585 /// Set of run-time checks required to establish independence of
586 /// otherwise may-aliasing pointers in the loop.
588
589 /// Flag indicating if pointer-difference checks can be used
590 bool CanUseDiffCheck = true;
591
592 /// A list of (pointer-difference, access size) pairs that can be used to
593 /// prove that there are no vectorization-preventing dependencies.
595};
596
597/// Drive the analysis of memory accesses in the loop
598///
599/// This class is responsible for analyzing the memory accesses of a loop. It
600/// collects the accesses and then its main helper the AccessAnalysis class
601/// finds and categorizes the dependences in buildDependenceSets.
602///
603/// For memory dependences that can be analyzed at compile time, it determines
604/// whether the dependence is part of cycle inhibiting vectorization. This work
605/// is delegated to the MemoryDepChecker class.
606///
607/// For memory dependences that cannot be determined at compile time, it
608/// generates run-time checks to prove independence. This is done by
609/// AccessAnalysis::canCheckPtrAtRT and the checks are maintained by the
610/// RuntimePointerCheck class.
611///
612/// If pointers can wrap or can't be expressed as affine AddRec expressions by
613/// ScalarEvolution, we will generate run-time checks by emitting a
614/// SCEVUnionPredicate.
615///
616/// Checks for both memory dependences and the SCEV predicates contained in the
617/// PSE must be emitted in order for the results of this analysis to be valid.
619public:
621 const TargetLibraryInfo *TLI, AAResults *AA, DominatorTree *DT,
622 LoopInfo *LI);
623
624 /// Return true we can analyze the memory accesses in the loop and there are
625 /// no memory dependence cycles. Note that for dependences between loads &
626 /// stores with uniform addresses,
627 /// hasStoreStoreDependenceInvolvingLoopInvariantAddress and
628 /// hasLoadStoreDependenceInvolvingLoopInvariantAddress also need to be
629 /// checked.
630 bool canVectorizeMemory() const { return CanVecMem; }
631
632 /// Return true if there is a convergent operation in the loop. There may
633 /// still be reported runtime pointer checks that would be required, but it is
634 /// not legal to insert them.
635 bool hasConvergentOp() const { return HasConvergentOp; }
636
638 return PtrRtChecking.get();
639 }
640
641 /// Number of memchecks required to prove independence of otherwise
642 /// may-alias pointers.
643 unsigned getNumRuntimePointerChecks() const {
644 return PtrRtChecking->getNumberOfChecks();
645 }
646
647 /// Return true if the block BB needs to be predicated in order for the loop
648 /// to be vectorized.
649 static bool blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
650 DominatorTree *DT);
651
652 /// Returns true if value \p V is loop invariant.
653 bool isInvariant(Value *V) const;
654
655 unsigned getNumStores() const { return NumStores; }
656 unsigned getNumLoads() const { return NumLoads;}
657
658 /// The diagnostics report generated for the analysis. E.g. why we
659 /// couldn't analyze the loop.
660 const OptimizationRemarkAnalysis *getReport() const { return Report.get(); }
661
662 /// the Memory Dependence Checker which can determine the
663 /// loop-independent and loop-carried dependences between memory accesses.
664 const MemoryDepChecker &getDepChecker() const { return *DepChecker; }
665
666 /// Return the list of instructions that use \p Ptr to read or write
667 /// memory.
669 bool isWrite) const {
670 return DepChecker->getInstructionsForAccess(Ptr, isWrite);
671 }
672
673 /// If an access has a symbolic strides, this maps the pointer value to
674 /// the stride symbol.
676 return SymbolicStrides;
677 }
678
679 /// Print the information about the memory accesses in the loop.
680 void print(raw_ostream &OS, unsigned Depth = 0) const;
681
682 /// Return true if the loop has memory dependence involving two stores to an
683 /// invariant address, else return false.
685 return HasStoreStoreDependenceInvolvingLoopInvariantAddress;
686 }
687
688 /// Return true if the loop has memory dependence involving a load and a store
689 /// to an invariant address, else return false.
691 return HasLoadStoreDependenceInvolvingLoopInvariantAddress;
692 }
693
694 /// Return the list of stores to invariant addresses.
696 return StoresToInvariantAddresses;
697 }
698
699 /// Used to add runtime SCEV checks. Simplifies SCEV expressions and converts
700 /// them to a more usable form. All SCEV expressions during the analysis
701 /// should be re-written (and therefore simplified) according to PSE.
702 /// A user of LoopAccessAnalysis will need to emit the runtime checks
703 /// associated with this predicate.
704 const PredicatedScalarEvolution &getPSE() const { return *PSE; }
705
706private:
707 /// Analyze the loop. Returns true if all memory access in the loop can be
708 /// vectorized.
709 bool analyzeLoop(AAResults *AA, LoopInfo *LI, const TargetLibraryInfo *TLI,
710 DominatorTree *DT);
711
712 /// Check if the structure of the loop allows it to be analyzed by this
713 /// pass.
714 bool canAnalyzeLoop();
715
716 /// Save the analysis remark.
717 ///
718 /// LAA does not directly emits the remarks. Instead it stores it which the
719 /// client can retrieve and presents as its own analysis
720 /// (e.g. -Rpass-analysis=loop-vectorize).
721 OptimizationRemarkAnalysis &recordAnalysis(StringRef RemarkName,
722 Instruction *Instr = nullptr);
723
724 /// Collect memory access with loop invariant strides.
725 ///
726 /// Looks for accesses like "a[i * StrideA]" where "StrideA" is loop
727 /// invariant.
728 void collectStridedAccess(Value *LoadOrStoreInst);
729
730 // Emits the first unsafe memory dependence in a loop.
731 // Emits nothing if there are no unsafe dependences
732 // or if the dependences were not recorded.
733 void emitUnsafeDependenceRemark();
734
735 std::unique_ptr<PredicatedScalarEvolution> PSE;
736
737 /// We need to check that all of the pointers in this list are disjoint
738 /// at runtime. Using std::unique_ptr to make using move ctor simpler.
739 std::unique_ptr<RuntimePointerChecking> PtrRtChecking;
740
741 /// the Memory Dependence Checker which can determine the
742 /// loop-independent and loop-carried dependences between memory accesses.
743 std::unique_ptr<MemoryDepChecker> DepChecker;
744
745 Loop *TheLoop;
746
747 unsigned NumLoads = 0;
748 unsigned NumStores = 0;
749
750 /// Cache the result of analyzeLoop.
751 bool CanVecMem = false;
752 bool HasConvergentOp = false;
753
754 /// Indicator that there are two non vectorizable stores to the same uniform
755 /// address.
756 bool HasStoreStoreDependenceInvolvingLoopInvariantAddress = false;
757 /// Indicator that there is non vectorizable load and store to the same
758 /// uniform address.
759 bool HasLoadStoreDependenceInvolvingLoopInvariantAddress = false;
760
761 /// List of stores to invariant addresses.
762 SmallVector<StoreInst *> StoresToInvariantAddresses;
763
764 /// The diagnostics report generated for the analysis. E.g. why we
765 /// couldn't analyze the loop.
766 std::unique_ptr<OptimizationRemarkAnalysis> Report;
767
768 /// If an access has a symbolic strides, this maps the pointer value to
769 /// the stride symbol.
770 DenseMap<Value *, const SCEV *> SymbolicStrides;
771};
772
773/// Return the SCEV corresponding to a pointer with the symbolic stride
774/// replaced with constant one, assuming the SCEV predicate associated with
775/// \p PSE is true.
776///
777/// If necessary this method will version the stride of the pointer according
778/// to \p PtrToStride and therefore add further predicates to \p PSE.
779///
780/// \p PtrToStride provides the mapping between the pointer value and its
781/// stride as collected by LoopVectorizationLegality::collectStridedAccess.
782const SCEV *
783replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE,
784 const DenseMap<Value *, const SCEV *> &PtrToStride,
785 Value *Ptr);
786
787/// If the pointer has a constant stride return it in units of the access type
788/// size. Otherwise return std::nullopt.
789///
790/// Ensure that it does not wrap in the address space, assuming the predicate
791/// associated with \p PSE is true.
792///
793/// If necessary this method will version the stride of the pointer according
794/// to \p PtrToStride and therefore add further predicates to \p PSE.
795/// The \p Assume parameter indicates if we are allowed to make additional
796/// run-time assumptions.
797///
798/// Note that the analysis results are defined if-and-only-if the original
799/// memory access was defined. If that access was dead, or UB, then the
800/// result of this function is undefined.
801std::optional<int64_t>
802getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr,
803 const Loop *Lp,
804 const DenseMap<Value *, const SCEV *> &StridesMap = DenseMap<Value *, const SCEV *>(),
805 bool Assume = false, bool ShouldCheckWrap = true);
806
807/// Returns the distance between the pointers \p PtrA and \p PtrB iff they are
808/// compatible and it is possible to calculate the distance between them. This
809/// is a simple API that does not depend on the analysis pass.
810/// \param StrictCheck Ensure that the calculated distance matches the
811/// type-based one after all the bitcasts removal in the provided pointers.
812std::optional<int> getPointersDiff(Type *ElemTyA, Value *PtrA, Type *ElemTyB,
813 Value *PtrB, const DataLayout &DL,
814 ScalarEvolution &SE,
815 bool StrictCheck = false,
816 bool CheckType = true);
817
818/// Attempt to sort the pointers in \p VL and return the sorted indices
819/// in \p SortedIndices, if reordering is required.
820///
821/// Returns 'true' if sorting is legal, otherwise returns 'false'.
822///
823/// For example, for a given \p VL of memory accesses in program order, a[i+4],
824/// a[i+0], a[i+1] and a[i+7], this function will sort the \p VL and save the
825/// sorted indices in \p SortedIndices as a[i+0], a[i+1], a[i+4], a[i+7] and
826/// saves the mask for actual memory accesses in program order in
827/// \p SortedIndices as <1,2,0,3>
828bool sortPtrAccesses(ArrayRef<Value *> VL, Type *ElemTy, const DataLayout &DL,
829 ScalarEvolution &SE,
830 SmallVectorImpl<unsigned> &SortedIndices);
831
832/// Returns true if the memory operations \p A and \p B are consecutive.
833/// This is a simple API that does not depend on the analysis pass.
834bool isConsecutiveAccess(Value *A, Value *B, const DataLayout &DL,
835 ScalarEvolution &SE, bool CheckType = true);
836
838 /// The cache.
840
841 // The used analysis passes.
842 ScalarEvolution &SE;
843 AAResults &AA;
844 DominatorTree &DT;
845 LoopInfo &LI;
847 const TargetLibraryInfo *TLI = nullptr;
848
849public:
852 const TargetLibraryInfo *TLI)
853 : SE(SE), AA(AA), DT(DT), LI(LI), TTI(TTI), TLI(TLI) {}
854
855 const LoopAccessInfo &getInfo(Loop &L);
856
857 void clear() { LoopAccessInfoMap.clear(); }
858
859 bool invalidate(Function &F, const PreservedAnalyses &PA,
861};
862
863/// This analysis provides dependence information for the memory
864/// accesses of a loop.
865///
866/// It runs the analysis for a loop on demand. This can be initiated by
867/// querying the loop access info via AM.getResult<LoopAccessAnalysis>.
868/// getResult return a LoopAccessInfo object. See this class for the
869/// specifics of what information is provided.
871 : public AnalysisInfoMixin<LoopAccessAnalysis> {
873 static AnalysisKey Key;
874
875public:
877
879};
880
882 const MemoryDepChecker &DepChecker) const {
883 return DepChecker.getMemoryInstructions()[Source];
884}
885
887 const MemoryDepChecker &DepChecker) const {
888 return DepChecker.getMemoryInstructions()[Destination];
889}
890
891} // End llvm namespace
892
893#endif
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MapVector< const Value *, unsigned > OrderMap
Definition: AsmWriter.cpp:99
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
bool End
Definition: ELF_riscv.cpp:480
Generic implementation of equivalence classes through the use Tarjan's efficient union-find algorithm...
This header provides classes for managing per-loop analyses.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
raw_pwrite_stream & OS
static LLVM_ATTRIBUTE_ALWAYS_INLINE bool CheckType(MVT::SimpleValueType VT, SDValue N, const TargetLowering *TLI, const DataLayout &DL)
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
An instruction for reading from memory.
Definition: Instructions.h:173
This analysis provides dependence information for the memory accesses of a loop.
LoopAccessInfoManager Result
Result run(Function &F, FunctionAnalysisManager &AM)
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
const LoopAccessInfo & getInfo(Loop &L)
LoopAccessInfoManager(ScalarEvolution &SE, AAResults &AA, DominatorTree &DT, LoopInfo &LI, TargetTransformInfo *TTI, const TargetLibraryInfo *TLI)
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...
ArrayRef< StoreInst * > getStoresToInvariantAddresses() const
Return the list of stores to invariant addresses.
const OptimizationRemarkAnalysis * getReport() const
The diagnostics report generated for the analysis.
const RuntimePointerChecking * getRuntimePointerChecking() const
bool canVectorizeMemory() const
Return true we can analyze the memory accesses in the loop and there are no memory dependence cycles.
unsigned getNumLoads() const
unsigned getNumRuntimePointerChecks() const
Number of memchecks required to prove independence of otherwise may-alias pointers.
bool isInvariant(Value *V) const
Returns true if value V is loop invariant.
bool hasLoadStoreDependenceInvolvingLoopInvariantAddress() const
Return true if the loop has memory dependence involving a load and a store to an invariant address,...
void print(raw_ostream &OS, unsigned Depth=0) const
Print the information about the memory accesses in the loop.
const PredicatedScalarEvolution & getPSE() const
Used to add runtime SCEV checks.
unsigned getNumStores() const
static bool blockNeedsPredication(BasicBlock *BB, Loop *TheLoop, DominatorTree *DT)
Return true if the block BB needs to be predicated in order for the loop to be vectorized.
SmallVector< Instruction *, 4 > getInstructionsForAccess(Value *Ptr, bool isWrite) const
Return the list of instructions that use Ptr to read or write memory.
const DenseMap< Value *, const SCEV * > & getSymbolicStrides() const
If an access has a symbolic strides, this maps the pointer value to the stride symbol.
bool hasStoreStoreDependenceInvolvingLoopInvariantAddress() const
Return true if the loop has memory dependence involving two stores to an invariant address,...
bool hasConvergentOp() const
Return true if there is a convergent operation in the loop.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:44
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 areDepsSafe(DepCandidates &AccessSets, MemAccessInfoList &CheckDeps, const DenseMap< Value *, SmallVector< const Value *, 16 > > &UnderlyingObjects)
Check whether the dependencies between the accesses are safe.
bool isSafeForAnyVectorWidth() const
Return true if the number of elements that are safe to operate on simultaneously is not bounded.
EquivalenceClasses< MemAccessInfo > DepCandidates
Set of potential dependent memory accesses.
MemoryDepChecker(PredicatedScalarEvolution &PSE, const Loop *L, const DenseMap< Value *, const SCEV * > &SymbolicStrides, unsigned MaxTargetVectorWidthInBits)
const SmallVectorImpl< Instruction * > & getMemoryInstructions() const
The vector of memory access instructions.
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.
SmallVector< MemAccessInfo, 8 > MemAccessInfoList
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.
bool shouldRetryWithRuntimeCheck() const
In same cases when the dependency check fails we can still vectorize the loop with a dynamic array ac...
void addAccess(StoreInst *SI)
Register the location (instructions are given increasing numbers) of a write access.
PointerIntPair< Value *, 1, bool > MemAccessInfo
DenseMap< Instruction *, unsigned > generateInstructionOrderMap() const
Generate a mapping between the memory instructions and their indices according to program order.
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 ...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
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.
RuntimePointerChecking(MemoryDepChecker &DC, ScalarEvolution *SE)
void printChecks(raw_ostream &OS, const SmallVectorImpl< RuntimePointerCheck > &Checks, unsigned Depth=0) const
Print Checks.
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.
void print(raw_ostream &OS, unsigned Depth=0) const
Print the list run-time memory checks necessary.
std::optional< ArrayRef< PointerDiffInfo > > getDiffChecks() const
SmallVector< RuntimeCheckingPtrGroup, 2 > CheckingGroups
Holds a partitioning of pointers into "check groups".
static bool arePointersInSamePartition(const SmallVectorImpl< int > &PtrToPartition, unsigned PtrIdx1, unsigned PtrIdx2)
Check if pointers are in the same partition.
bool empty() const
No run-time memory checking is necessary.
SmallVector< PointerInfo, 2 > Pointers
Information about the pointers that may require checking.
ScalarEvolution * getSE() const
void insert(Loop *Lp, Value *Ptr, const SCEV *PtrExpr, Type *AccessTy, bool WritePtr, unsigned DepSetId, unsigned ASId, PredicatedScalarEvolution &PSE, bool NeedsFreeze)
Insert a pointer and calculate the start and end SCEVs.
const SmallVectorImpl< RuntimePointerCheck > & getChecks() const
Returns the checks that generateChecks created.
const PointerInfo & getPointerInfo(unsigned PtrIdx) const
Return PointerInfo for pointer at index PtrIdx.
This class represents an analyzed expression in the program.
The main scalar evolution driver.
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
An instruction for storing to memory.
Definition: Instructions.h:289
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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.
Value handle that tracks a Value across RAUW.
Definition: ValueHandle.h:331
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::optional< int > 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...
std::pair< const RuntimeCheckingPtrGroup *, const RuntimeCheckingPtrGroup * > RuntimePointerCheck
A memcheck which made up of a pair of grouped pointers.
std::optional< int64_t > getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy, Value *Ptr, const Loop *Lp, const DenseMap< Value *, const SCEV * > &StridesMap=DenseMap< Value *, const SCEV * >(), bool Assume=false, bool ShouldCheckWrap=true)
If the pointer has a constant stride return it in units of the access type size.
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,...
const SCEV * replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE, const DenseMap< Value *, const SCEV * > &PtrToStride, Value *Ptr)
Return the SCEV corresponding to a pointer with the symbolic stride replaced with constant one,...
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.
#define N
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
Dependece between memory access instructions.
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.
Dependence(unsigned Source, unsigned Destination, DepType Type)
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.
bool isForward() const
Lexically forward dependence.
bool isBackward() const
Lexically backward dependence.
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 const char * DepName[]
String version of the types.
PointerDiffInfo(const SCEV *SrcStart, const SCEV *SinkStart, unsigned AccessSize, bool NeedsFreeze)
unsigned AddressSpace
Address space of the involved pointers.
bool addPointer(unsigned Index, 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.
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.
PointerInfo(Value *PointerValue, const SCEV *Start, const SCEV *End, bool IsWritePtr, unsigned DependencySetId, unsigned AliasSetId, const SCEV *Expr, bool NeedsFreeze)
const SCEV * Start
Holds the smallest byte address accessed by the pointer throughout all iterations of the loop.
const SCEV * Expr
SCEV for the access.
bool NeedsFreeze
True if the pointer expressions needs to be frozen after expansion.
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.
const SCEV * End
Holds the largest byte address accessed by the pointer throughout all iterations of the loop,...
TrackingVH< Value > PointerValue
Holds the pointer value that we need to check.
Collection of parameters shared beetween the Loop Vectorizer and the Loop Access Analysis.
static const unsigned MaxVectorWidth
Maximum SIMD width.
static unsigned VectorizationFactor
VF as overridden by the user.
static unsigned RuntimeMemoryCheckThreshold
\When performing memory disambiguation checks at runtime do not make more than this number of compari...
static bool isInterleaveForced()
True if force-vector-interleave was specified by the user.
static unsigned VectorizationInterleave
Interleave factor as overridden by the user.