1 //===- llvm/Analysis/LoopAccessAnalysis.h -----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the interface for the loop memory dependence framework that
11 // was originally developed for the Loop Vectorizer.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
16 #define LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
17
18 #include "llvm/ADT/EquivalenceClasses.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/Analysis/AliasSetTracker.h"
23 #include "llvm/Analysis/LoopAnalysisManager.h"
24 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
25 #include "llvm/IR/DiagnosticInfo.h"
26 #include "llvm/IR/ValueHandle.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/raw_ostream.h"
29
30 namespace llvm {
31
32 class Value;
33 class DataLayout;
34 class ScalarEvolution;
35 class Loop;
36 class SCEV;
37 class SCEVUnionPredicate;
38 class LoopAccessInfo;
39 class OptimizationRemarkEmitter;
40
41 /// Collection of parameters shared beetween the Loop Vectorizer and the
42 /// Loop Access Analysis.
43 struct VectorizerParams {
44 /// Maximum SIMD width.
45 static const unsigned MaxVectorWidth;
46
47 /// VF as overridden by the user.
48 static unsigned VectorizationFactor;
49 /// Interleave factor as overridden by the user.
50 static unsigned VectorizationInterleave;
51 /// True if force-vector-interleave was specified by the user.
52 static bool isInterleaveForced();
53
54 /// \When performing memory disambiguation checks at runtime do not
55 /// make more than this number of comparisons.
56 static unsigned RuntimeMemoryCheckThreshold;
57 };
58
59 /// Checks memory dependences among accesses to the same underlying
60 /// object to determine whether there vectorization is legal or not (and at
61 /// which vectorization factor).
62 ///
63 /// Note: This class will compute a conservative dependence for access to
64 /// different underlying pointers. Clients, such as the loop vectorizer, will
65 /// sometimes deal these potential dependencies by emitting runtime checks.
66 ///
67 /// We use the ScalarEvolution framework to symbolically evalutate access
68 /// functions pairs. Since we currently don't restructure the loop we can rely
69 /// on the program order of memory accesses to determine their safety.
70 /// At the moment we will only deem accesses as safe for:
71 /// * A negative constant distance assuming program order.
72 ///
73 /// Safe: tmp = a[i + 1]; OR a[i + 1] = x;
74 /// a[i] = tmp; y = a[i];
75 ///
76 /// The latter case is safe because later checks guarantuee that there can't
77 /// be a cycle through a phi node (that is, we check that "x" and "y" is not
78 /// the same variable: a header phi can only be an induction or a reduction, a
79 /// reduction can't have a memory sink, an induction can't have a memory
80 /// source). This is important and must not be violated (or we have to
81 /// resort to checking for cycles through memory).
82 ///
83 /// * A positive constant distance assuming program order that is bigger
84 /// than the biggest memory access.
85 ///
86 /// tmp = a[i] OR b[i] = x
87 /// a[i+2] = tmp y = b[i+2];
88 ///
89 /// Safe distance: 2 x sizeof(a[0]), and 2 x sizeof(b[0]), respectively.
90 ///
91 /// * Zero distances and all accesses have the same size.
92 ///
93 class MemoryDepChecker {
94 public:
95 typedef PointerIntPair<Value *, 1, bool> MemAccessInfo;
96 typedef SmallVector<MemAccessInfo, 8> MemAccessInfoList;
97 /// Set of potential dependent memory accesses.
98 typedef EquivalenceClasses<MemAccessInfo> DepCandidates;
99
100 /// Type to keep track of the status of the dependence check. The order of
101 /// the elements is important and has to be from most permissive to least
102 /// permissive.
103 enum class VectorizationSafetyStatus {
104 // Can vectorize safely without RT checks. All dependences are known to be
105 // safe.
106 Safe,
107 // Can possibly vectorize with RT checks to overcome unknown dependencies.
108 PossiblySafeWithRtChecks,
109 // Cannot vectorize due to known unsafe dependencies.
110 Unsafe,
111 };
112
113 /// Dependece between memory access instructions.
114 struct Dependence {
115 /// The type of the dependence.
116 enum DepType {
117 // No dependence.
118 NoDep,
119 // We couldn't determine the direction or the distance.
120 Unknown,
121 // Lexically forward.
122 //
123 // FIXME: If we only have loop-independent forward dependences (e.g. a
124 // read and write of A[i]), LAA will locally deem the dependence "safe"
125 // without querying the MemoryDepChecker. Therefore we can miss
126 // enumerating loop-independent forward dependences in
127 // getDependences. Note that as soon as there are different
128 // indices used to access the same array, the MemoryDepChecker *is*
129 // queried and the dependence list is complete.
130 Forward,
131 // Forward, but if vectorized, is likely to prevent store-to-load
132 // forwarding.
133 ForwardButPreventsForwarding,
134 // Lexically backward.
135 Backward,
136 // Backward, but the distance allows a vectorization factor of
137 // MaxSafeDepDistBytes.
138 BackwardVectorizable,
139 // Same, but may prevent store-to-load forwarding.
140 BackwardVectorizableButPreventsForwarding
141 };
142
143 /// String version of the types.
144 static const char *DepName[];
145
146 /// Index of the source of the dependence in the InstMap vector.
147 unsigned Source;
148 /// Index of the destination of the dependence in the InstMap vector.
149 unsigned Destination;
150 /// The type of the dependence.
151 DepType Type;
152
DependenceDependence153 Dependence(unsigned Source, unsigned Destination, DepType Type)
154 : Source(Source), Destination(Destination), Type(Type) {}
155
156 /// Return the source instruction of the dependence.
157 Instruction *getSource(const LoopAccessInfo &LAI) const;
158 /// Return the destination instruction of the dependence.
159 Instruction *getDestination(const LoopAccessInfo &LAI) const;
160
161 /// Dependence types that don't prevent vectorization.
162 static VectorizationSafetyStatus isSafeForVectorization(DepType Type);
163
164 /// Lexically forward dependence.
165 bool isForward() const;
166 /// Lexically backward dependence.
167 bool isBackward() const;
168
169 /// May be a lexically backward dependence type (includes Unknown).
170 bool isPossiblyBackward() const;
171
172 /// Print the dependence. \p Instr is used to map the instruction
173 /// indices to instructions.
174 void print(raw_ostream &OS, unsigned Depth,
175 const SmallVectorImpl<Instruction *> &Instrs) const;
176 };
177
MemoryDepChecker(PredicatedScalarEvolution & PSE,const Loop * L)178 MemoryDepChecker(PredicatedScalarEvolution &PSE, const Loop *L)
179 : PSE(PSE), InnermostLoop(L), AccessIdx(0), MaxSafeRegisterWidth(-1U),
180 FoundNonConstantDistanceDependence(false),
181 Status(VectorizationSafetyStatus::Safe), RecordDependences(true) {}
182
183 /// Register the location (instructions are given increasing numbers)
184 /// of a write access.
addAccess(StoreInst * SI)185 void addAccess(StoreInst *SI) {
186 Value *Ptr = SI->getPointerOperand();
187 Accesses[MemAccessInfo(Ptr, true)].push_back(AccessIdx);
188 InstMap.push_back(SI);
189 ++AccessIdx;
190 }
191
192 /// Register the location (instructions are given increasing numbers)
193 /// of a write access.
addAccess(LoadInst * LI)194 void addAccess(LoadInst *LI) {
195 Value *Ptr = LI->getPointerOperand();
196 Accesses[MemAccessInfo(Ptr, false)].push_back(AccessIdx);
197 InstMap.push_back(LI);
198 ++AccessIdx;
199 }
200
201 /// Check whether the dependencies between the accesses are safe.
202 ///
203 /// Only checks sets with elements in \p CheckDeps.
204 bool areDepsSafe(DepCandidates &AccessSets, MemAccessInfoList &CheckDeps,
205 const ValueToValueMap &Strides);
206
207 /// No memory dependence was encountered that would inhibit
208 /// vectorization.
isSafeForVectorization()209 bool isSafeForVectorization() const {
210 return Status == VectorizationSafetyStatus::Safe;
211 }
212
213 /// The maximum number of bytes of a vector register we can vectorize
214 /// the accesses safely with.
getMaxSafeDepDistBytes()215 uint64_t getMaxSafeDepDistBytes() { return MaxSafeDepDistBytes; }
216
217 /// Return the number of elements that are safe to operate on
218 /// simultaneously, multiplied by the size of the element in bits.
getMaxSafeRegisterWidth()219 uint64_t getMaxSafeRegisterWidth() const { return MaxSafeRegisterWidth; }
220
221 /// In same cases when the dependency check fails we can still
222 /// vectorize the loop with a dynamic array access check.
shouldRetryWithRuntimeCheck()223 bool shouldRetryWithRuntimeCheck() const {
224 return FoundNonConstantDistanceDependence &&
225 Status == VectorizationSafetyStatus::PossiblySafeWithRtChecks;
226 }
227
228 /// Returns the memory dependences. If null is returned we exceeded
229 /// the MaxDependences threshold and this information is not
230 /// available.
getDependences()231 const SmallVectorImpl<Dependence> *getDependences() const {
232 return RecordDependences ? &Dependences : nullptr;
233 }
234
clearDependences()235 void clearDependences() { Dependences.clear(); }
236
237 /// The vector of memory access instructions. The indices are used as
238 /// instruction identifiers in the Dependence class.
getMemoryInstructions()239 const SmallVectorImpl<Instruction *> &getMemoryInstructions() const {
240 return InstMap;
241 }
242
243 /// Generate a mapping between the memory instructions and their
244 /// indices according to program order.
generateInstructionOrderMap()245 DenseMap<Instruction *, unsigned> generateInstructionOrderMap() const {
246 DenseMap<Instruction *, unsigned> OrderMap;
247
248 for (unsigned I = 0; I < InstMap.size(); ++I)
249 OrderMap[InstMap[I]] = I;
250
251 return OrderMap;
252 }
253
254 /// Find the set of instructions that read or write via \p Ptr.
255 SmallVector<Instruction *, 4> getInstructionsForAccess(Value *Ptr,
256 bool isWrite) const;
257
258 private:
259 /// A wrapper around ScalarEvolution, used to add runtime SCEV checks, and
260 /// applies dynamic knowledge to simplify SCEV expressions and convert them
261 /// to a more usable form. We need this in case assumptions about SCEV
262 /// expressions need to be made in order to avoid unknown dependences. For
263 /// example we might assume a unit stride for a pointer in order to prove
264 /// that a memory access is strided and doesn't wrap.
265 PredicatedScalarEvolution &PSE;
266 const Loop *InnermostLoop;
267
268 /// Maps access locations (ptr, read/write) to program order.
269 DenseMap<MemAccessInfo, std::vector<unsigned> > Accesses;
270
271 /// Memory access instructions in program order.
272 SmallVector<Instruction *, 16> InstMap;
273
274 /// The program order index to be used for the next instruction.
275 unsigned AccessIdx;
276
277 // We can access this many bytes in parallel safely.
278 uint64_t MaxSafeDepDistBytes;
279
280 /// Number of elements (from consecutive iterations) that are safe to
281 /// operate on simultaneously, multiplied by the size of the element in bits.
282 /// The size of the element is taken from the memory access that is most
283 /// restrictive.
284 uint64_t MaxSafeRegisterWidth;
285
286 /// If we see a non-constant dependence distance we can still try to
287 /// vectorize this loop with runtime checks.
288 bool FoundNonConstantDistanceDependence;
289
290 /// Result of the dependence checks, indicating whether the checked
291 /// dependences are safe for vectorization, require RT checks or are known to
292 /// be unsafe.
293 VectorizationSafetyStatus Status;
294
295 //// True if Dependences reflects the dependences in the
296 //// loop. If false we exceeded MaxDependences and
297 //// Dependences is invalid.
298 bool RecordDependences;
299
300 /// Memory dependences collected during the analysis. Only valid if
301 /// RecordDependences is true.
302 SmallVector<Dependence, 8> Dependences;
303
304 /// Check whether there is a plausible dependence between the two
305 /// accesses.
306 ///
307 /// Access \p A must happen before \p B in program order. The two indices
308 /// identify the index into the program order map.
309 ///
310 /// This function checks whether there is a plausible dependence (or the
311 /// absence of such can't be proved) between the two accesses. If there is a
312 /// plausible dependence but the dependence distance is bigger than one
313 /// element access it records this distance in \p MaxSafeDepDistBytes (if this
314 /// distance is smaller than any other distance encountered so far).
315 /// Otherwise, this function returns true signaling a possible dependence.
316 Dependence::DepType isDependent(const MemAccessInfo &A, unsigned AIdx,
317 const MemAccessInfo &B, unsigned BIdx,
318 const ValueToValueMap &Strides);
319
320 /// Check whether the data dependence could prevent store-load
321 /// forwarding.
322 ///
323 /// \return false if we shouldn't vectorize at all or avoid larger
324 /// vectorization factors by limiting MaxSafeDepDistBytes.
325 bool couldPreventStoreLoadForward(uint64_t Distance, uint64_t TypeByteSize);
326
327 /// Updates the current safety status with \p S. We can go from Safe to
328 /// either PossiblySafeWithRtChecks or Unsafe and from
329 /// PossiblySafeWithRtChecks to Unsafe.
330 void mergeInStatus(VectorizationSafetyStatus S);
331 };
332
333 /// Holds information about the memory runtime legality checks to verify
334 /// that a group of pointers do not overlap.
335 class RuntimePointerChecking {
336 public:
337 struct PointerInfo {
338 /// Holds the pointer value that we need to check.
339 TrackingVH<Value> PointerValue;
340 /// Holds the smallest byte address accessed by the pointer throughout all
341 /// iterations of the loop.
342 const SCEV *Start;
343 /// Holds the largest byte address accessed by the pointer throughout all
344 /// iterations of the loop, plus 1.
345 const SCEV *End;
346 /// Holds the information if this pointer is used for writing to memory.
347 bool IsWritePtr;
348 /// Holds the id of the set of pointers that could be dependent because of a
349 /// shared underlying object.
350 unsigned DependencySetId;
351 /// Holds the id of the disjoint alias set to which this pointer belongs.
352 unsigned AliasSetId;
353 /// SCEV for the access.
354 const SCEV *Expr;
355
PointerInfoPointerInfo356 PointerInfo(Value *PointerValue, const SCEV *Start, const SCEV *End,
357 bool IsWritePtr, unsigned DependencySetId, unsigned AliasSetId,
358 const SCEV *Expr)
359 : PointerValue(PointerValue), Start(Start), End(End),
360 IsWritePtr(IsWritePtr), DependencySetId(DependencySetId),
361 AliasSetId(AliasSetId), Expr(Expr) {}
362 };
363
RuntimePointerChecking(ScalarEvolution * SE)364 RuntimePointerChecking(ScalarEvolution *SE) : Need(false), SE(SE) {}
365
366 /// Reset the state of the pointer runtime information.
reset()367 void reset() {
368 Need = false;
369 Pointers.clear();
370 Checks.clear();
371 }
372
373 /// Insert a pointer and calculate the start and end SCEVs.
374 /// We need \p PSE in order to compute the SCEV expression of the pointer
375 /// according to the assumptions that we've made during the analysis.
376 /// The method might also version the pointer stride according to \p Strides,
377 /// and add new predicates to \p PSE.
378 void insert(Loop *Lp, Value *Ptr, bool WritePtr, unsigned DepSetId,
379 unsigned ASId, const ValueToValueMap &Strides,
380 PredicatedScalarEvolution &PSE);
381
382 /// No run-time memory checking is necessary.
empty()383 bool empty() const { return Pointers.empty(); }
384
385 /// A grouping of pointers. A single memcheck is required between
386 /// two groups.
387 struct CheckingPtrGroup {
388 /// Create a new pointer checking group containing a single
389 /// pointer, with index \p Index in RtCheck.
CheckingPtrGroupCheckingPtrGroup390 CheckingPtrGroup(unsigned Index, RuntimePointerChecking &RtCheck)
391 : RtCheck(RtCheck), High(RtCheck.Pointers[Index].End),
392 Low(RtCheck.Pointers[Index].Start) {
393 Members.push_back(Index);
394 }
395
396 /// Tries to add the pointer recorded in RtCheck at index
397 /// \p Index to this pointer checking group. We can only add a pointer
398 /// to a checking group if we will still be able to get
399 /// the upper and lower bounds of the check. Returns true in case
400 /// of success, false otherwise.
401 bool addPointer(unsigned Index);
402
403 /// Constitutes the context of this pointer checking group. For each
404 /// pointer that is a member of this group we will retain the index
405 /// at which it appears in RtCheck.
406 RuntimePointerChecking &RtCheck;
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.
414 SmallVector<unsigned, 2> Members;
415 };
416
417 /// A memcheck which made up of a pair of grouped pointers.
418 ///
419 /// These *have* to be const for now, since checks are generated from
420 /// CheckingPtrGroups in LAI::addRuntimeChecks which is a const member
421 /// function. FIXME: once check-generation is moved inside this class (after
422 /// the PtrPartition hack is removed), we could drop const.
423 typedef std::pair<const CheckingPtrGroup *, const CheckingPtrGroup *>
424 PointerCheck;
425
426 /// Generate the checks and store it. This also performs the grouping
427 /// of pointers to reduce the number of memchecks necessary.
428 void generateChecks(MemoryDepChecker::DepCandidates &DepCands,
429 bool UseDependencies);
430
431 /// Returns the checks that generateChecks created.
getChecks()432 const SmallVector<PointerCheck, 4> &getChecks() const { return Checks; }
433
434 /// Decide if we need to add a check between two groups of pointers,
435 /// according to needsChecking.
436 bool needsChecking(const CheckingPtrGroup &M,
437 const CheckingPtrGroup &N) const;
438
439 /// Returns the number of run-time checks required according to
440 /// needsChecking.
getNumberOfChecks()441 unsigned getNumberOfChecks() const { return Checks.size(); }
442
443 /// Print the list run-time memory checks necessary.
444 void print(raw_ostream &OS, unsigned Depth = 0) const;
445
446 /// Print \p Checks.
447 void printChecks(raw_ostream &OS, const SmallVectorImpl<PointerCheck> &Checks,
448 unsigned Depth = 0) const;
449
450 /// This flag indicates if we need to add the runtime check.
451 bool Need;
452
453 /// Information about the pointers that may require checking.
454 SmallVector<PointerInfo, 2> Pointers;
455
456 /// Holds a partitioning of pointers into "check groups".
457 SmallVector<CheckingPtrGroup, 2> CheckingGroups;
458
459 /// Check if pointers are in the same partition
460 ///
461 /// \p PtrToPartition contains the partition number for pointers (-1 if the
462 /// pointer belongs to multiple partitions).
463 static bool
464 arePointersInSamePartition(const SmallVectorImpl<int> &PtrToPartition,
465 unsigned PtrIdx1, unsigned PtrIdx2);
466
467 /// Decide whether we need to issue a run-time check for pointer at
468 /// index \p I and \p J to prove their independence.
469 bool needsChecking(unsigned I, unsigned J) const;
470
471 /// Return PointerInfo for pointer at index \p PtrIdx.
getPointerInfo(unsigned PtrIdx)472 const PointerInfo &getPointerInfo(unsigned PtrIdx) const {
473 return Pointers[PtrIdx];
474 }
475
476 private:
477 /// Groups pointers such that a single memcheck is required
478 /// between two different groups. This will clear the CheckingGroups vector
479 /// and re-compute it. We will only group dependecies if \p UseDependencies
480 /// is true, otherwise we will create a separate group for each pointer.
481 void groupChecks(MemoryDepChecker::DepCandidates &DepCands,
482 bool UseDependencies);
483
484 /// Generate the checks and return them.
485 SmallVector<PointerCheck, 4>
486 generateChecks() const;
487
488 /// Holds a pointer to the ScalarEvolution analysis.
489 ScalarEvolution *SE;
490
491 /// Set of run-time checks required to establish independence of
492 /// otherwise may-aliasing pointers in the loop.
493 SmallVector<PointerCheck, 4> Checks;
494 };
495
496 /// Drive the analysis of memory accesses in the loop
497 ///
498 /// This class is responsible for analyzing the memory accesses of a loop. It
499 /// collects the accesses and then its main helper the AccessAnalysis class
500 /// finds and categorizes the dependences in buildDependenceSets.
501 ///
502 /// For memory dependences that can be analyzed at compile time, it determines
503 /// whether the dependence is part of cycle inhibiting vectorization. This work
504 /// is delegated to the MemoryDepChecker class.
505 ///
506 /// For memory dependences that cannot be determined at compile time, it
507 /// generates run-time checks to prove independence. This is done by
508 /// AccessAnalysis::canCheckPtrAtRT and the checks are maintained by the
509 /// RuntimePointerCheck class.
510 ///
511 /// If pointers can wrap or can't be expressed as affine AddRec expressions by
512 /// ScalarEvolution, we will generate run-time checks by emitting a
513 /// SCEVUnionPredicate.
514 ///
515 /// Checks for both memory dependences and the SCEV predicates contained in the
516 /// PSE must be emitted in order for the results of this analysis to be valid.
517 class LoopAccessInfo {
518 public:
519 LoopAccessInfo(Loop *L, ScalarEvolution *SE, const TargetLibraryInfo *TLI,
520 AliasAnalysis *AA, DominatorTree *DT, LoopInfo *LI);
521
522 /// Return true we can analyze the memory accesses in the loop and there are
523 /// no memory dependence cycles.
canVectorizeMemory()524 bool canVectorizeMemory() const { return CanVecMem; }
525
getRuntimePointerChecking()526 const RuntimePointerChecking *getRuntimePointerChecking() const {
527 return PtrRtChecking.get();
528 }
529
530 /// Number of memchecks required to prove independence of otherwise
531 /// may-alias pointers.
getNumRuntimePointerChecks()532 unsigned getNumRuntimePointerChecks() const {
533 return PtrRtChecking->getNumberOfChecks();
534 }
535
536 /// Return true if the block BB needs to be predicated in order for the loop
537 /// to be vectorized.
538 static bool blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
539 DominatorTree *DT);
540
541 /// Returns true if the value V is uniform within the loop.
542 bool isUniform(Value *V) const;
543
getMaxSafeDepDistBytes()544 uint64_t getMaxSafeDepDistBytes() const { return MaxSafeDepDistBytes; }
getNumStores()545 unsigned getNumStores() const { return NumStores; }
getNumLoads()546 unsigned getNumLoads() const { return NumLoads;}
547
548 /// Add code that checks at runtime if the accessed arrays overlap.
549 ///
550 /// Returns a pair of instructions where the first element is the first
551 /// instruction generated in possibly a sequence of instructions and the
552 /// second value is the final comparator value or NULL if no check is needed.
553 std::pair<Instruction *, Instruction *>
554 addRuntimeChecks(Instruction *Loc) const;
555
556 /// Generete the instructions for the checks in \p PointerChecks.
557 ///
558 /// Returns a pair of instructions where the first element is the first
559 /// instruction generated in possibly a sequence of instructions and the
560 /// second value is the final comparator value or NULL if no check is needed.
561 std::pair<Instruction *, Instruction *>
562 addRuntimeChecks(Instruction *Loc,
563 const SmallVectorImpl<RuntimePointerChecking::PointerCheck>
564 &PointerChecks) const;
565
566 /// The diagnostics report generated for the analysis. E.g. why we
567 /// couldn't analyze the loop.
getReport()568 const OptimizationRemarkAnalysis *getReport() const { return Report.get(); }
569
570 /// the Memory Dependence Checker which can determine the
571 /// loop-independent and loop-carried dependences between memory accesses.
getDepChecker()572 const MemoryDepChecker &getDepChecker() const { return *DepChecker; }
573
574 /// Return the list of instructions that use \p Ptr to read or write
575 /// memory.
getInstructionsForAccess(Value * Ptr,bool isWrite)576 SmallVector<Instruction *, 4> getInstructionsForAccess(Value *Ptr,
577 bool isWrite) const {
578 return DepChecker->getInstructionsForAccess(Ptr, isWrite);
579 }
580
581 /// If an access has a symbolic strides, this maps the pointer value to
582 /// the stride symbol.
getSymbolicStrides()583 const ValueToValueMap &getSymbolicStrides() const { return SymbolicStrides; }
584
585 /// Pointer has a symbolic stride.
hasStride(Value * V)586 bool hasStride(Value *V) const { return StrideSet.count(V); }
587
588 /// Print the information about the memory accesses in the loop.
589 void print(raw_ostream &OS, unsigned Depth = 0) const;
590
591 /// If the loop has memory dependence involving an invariant address, i.e. two
592 /// stores or a store and a load, then return true, else return false.
hasDependenceInvolvingLoopInvariantAddress()593 bool hasDependenceInvolvingLoopInvariantAddress() const {
594 return HasDependenceInvolvingLoopInvariantAddress;
595 }
596
597 /// Used to add runtime SCEV checks. Simplifies SCEV expressions and converts
598 /// them to a more usable form. All SCEV expressions during the analysis
599 /// should be re-written (and therefore simplified) according to PSE.
600 /// A user of LoopAccessAnalysis will need to emit the runtime checks
601 /// associated with this predicate.
getPSE()602 const PredicatedScalarEvolution &getPSE() const { return *PSE; }
603
604 private:
605 /// Analyze the loop.
606 void analyzeLoop(AliasAnalysis *AA, LoopInfo *LI,
607 const TargetLibraryInfo *TLI, DominatorTree *DT);
608
609 /// Check if the structure of the loop allows it to be analyzed by this
610 /// pass.
611 bool canAnalyzeLoop();
612
613 /// Save the analysis remark.
614 ///
615 /// LAA does not directly emits the remarks. Instead it stores it which the
616 /// client can retrieve and presents as its own analysis
617 /// (e.g. -Rpass-analysis=loop-vectorize).
618 OptimizationRemarkAnalysis &recordAnalysis(StringRef RemarkName,
619 Instruction *Instr = nullptr);
620
621 /// Collect memory access with loop invariant strides.
622 ///
623 /// Looks for accesses like "a[i * StrideA]" where "StrideA" is loop
624 /// invariant.
625 void collectStridedAccess(Value *LoadOrStoreInst);
626
627 std::unique_ptr<PredicatedScalarEvolution> PSE;
628
629 /// We need to check that all of the pointers in this list are disjoint
630 /// at runtime. Using std::unique_ptr to make using move ctor simpler.
631 std::unique_ptr<RuntimePointerChecking> PtrRtChecking;
632
633 /// the Memory Dependence Checker which can determine the
634 /// loop-independent and loop-carried dependences between memory accesses.
635 std::unique_ptr<MemoryDepChecker> DepChecker;
636
637 Loop *TheLoop;
638
639 unsigned NumLoads;
640 unsigned NumStores;
641
642 uint64_t MaxSafeDepDistBytes;
643
644 /// Cache the result of analyzeLoop.
645 bool CanVecMem;
646
647 /// Indicator that there are non vectorizable stores to a uniform address.
648 bool HasDependenceInvolvingLoopInvariantAddress;
649
650 /// The diagnostics report generated for the analysis. E.g. why we
651 /// couldn't analyze the loop.
652 std::unique_ptr<OptimizationRemarkAnalysis> Report;
653
654 /// If an access has a symbolic strides, this maps the pointer value to
655 /// the stride symbol.
656 ValueToValueMap SymbolicStrides;
657
658 /// Set of symbolic strides values.
659 SmallPtrSet<Value *, 8> StrideSet;
660 };
661
662 Value *stripIntegerCast(Value *V);
663
664 /// Return the SCEV corresponding to a pointer with the symbolic stride
665 /// replaced with constant one, assuming the SCEV predicate associated with
666 /// \p PSE is true.
667 ///
668 /// If necessary this method will version the stride of the pointer according
669 /// to \p PtrToStride and therefore add further predicates to \p PSE.
670 ///
671 /// If \p OrigPtr is not null, use it to look up the stride value instead of \p
672 /// Ptr. \p PtrToStride provides the mapping between the pointer value and its
673 /// stride as collected by LoopVectorizationLegality::collectStridedAccess.
674 const SCEV *replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE,
675 const ValueToValueMap &PtrToStride,
676 Value *Ptr, Value *OrigPtr = nullptr);
677
678 /// If the pointer has a constant stride return it in units of its
679 /// element size. Otherwise return zero.
680 ///
681 /// Ensure that it does not wrap in the address space, assuming the predicate
682 /// associated with \p PSE is true.
683 ///
684 /// If necessary this method will version the stride of the pointer according
685 /// to \p PtrToStride and therefore add further predicates to \p PSE.
686 /// The \p Assume parameter indicates if we are allowed to make additional
687 /// run-time assumptions.
688 int64_t getPtrStride(PredicatedScalarEvolution &PSE, Value *Ptr, const Loop *Lp,
689 const ValueToValueMap &StridesMap = ValueToValueMap(),
690 bool Assume = false, bool ShouldCheckWrap = true);
691
692 /// Attempt to sort the pointers in \p VL and return the sorted indices
693 /// in \p SortedIndices, if reordering is required.
694 ///
695 /// Returns 'true' if sorting is legal, otherwise returns 'false'.
696 ///
697 /// For example, for a given \p VL of memory accesses in program order, a[i+4],
698 /// a[i+0], a[i+1] and a[i+7], this function will sort the \p VL and save the
699 /// sorted indices in \p SortedIndices as a[i+0], a[i+1], a[i+4], a[i+7] and
700 /// saves the mask for actual memory accesses in program order in
701 /// \p SortedIndices as <1,2,0,3>
702 bool sortPtrAccesses(ArrayRef<Value *> VL, const DataLayout &DL,
703 ScalarEvolution &SE,
704 SmallVectorImpl<unsigned> &SortedIndices);
705
706 /// Returns true if the memory operations \p A and \p B are consecutive.
707 /// This is a simple API that does not depend on the analysis pass.
708 bool isConsecutiveAccess(Value *A, Value *B, const DataLayout &DL,
709 ScalarEvolution &SE, bool CheckType = true);
710
711 /// This analysis provides dependence information for the memory accesses
712 /// of a loop.
713 ///
714 /// It runs the analysis for a loop on demand. This can be initiated by
715 /// querying the loop access info via LAA::getInfo. getInfo return a
716 /// LoopAccessInfo object. See this class for the specifics of what information
717 /// is provided.
718 class LoopAccessLegacyAnalysis : public FunctionPass {
719 public:
720 static char ID;
721
LoopAccessLegacyAnalysis()722 LoopAccessLegacyAnalysis() : FunctionPass(ID) {
723 initializeLoopAccessLegacyAnalysisPass(*PassRegistry::getPassRegistry());
724 }
725
726 bool runOnFunction(Function &F) override;
727
728 void getAnalysisUsage(AnalysisUsage &AU) const override;
729
730 /// Query the result of the loop access information for the loop \p L.
731 ///
732 /// If there is no cached result available run the analysis.
733 const LoopAccessInfo &getInfo(Loop *L);
734
releaseMemory()735 void releaseMemory() override {
736 // Invalidate the cache when the pass is freed.
737 LoopAccessInfoMap.clear();
738 }
739
740 /// Print the result of the analysis when invoked with -analyze.
741 void print(raw_ostream &OS, const Module *M = nullptr) const override;
742
743 private:
744 /// The cache.
745 DenseMap<Loop *, std::unique_ptr<LoopAccessInfo>> LoopAccessInfoMap;
746
747 // The used analysis passes.
748 ScalarEvolution *SE;
749 const TargetLibraryInfo *TLI;
750 AliasAnalysis *AA;
751 DominatorTree *DT;
752 LoopInfo *LI;
753 };
754
755 /// This analysis provides dependence information for the memory
756 /// accesses of a loop.
757 ///
758 /// It runs the analysis for a loop on demand. This can be initiated by
759 /// querying the loop access info via AM.getResult<LoopAccessAnalysis>.
760 /// getResult return a LoopAccessInfo object. See this class for the
761 /// specifics of what information is provided.
762 class LoopAccessAnalysis
763 : public AnalysisInfoMixin<LoopAccessAnalysis> {
764 friend AnalysisInfoMixin<LoopAccessAnalysis>;
765 static AnalysisKey Key;
766
767 public:
768 typedef LoopAccessInfo Result;
769
770 Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
771 };
772
getSource(const LoopAccessInfo & LAI)773 inline Instruction *MemoryDepChecker::Dependence::getSource(
774 const LoopAccessInfo &LAI) const {
775 return LAI.getDepChecker().getMemoryInstructions()[Source];
776 }
777
getDestination(const LoopAccessInfo & LAI)778 inline Instruction *MemoryDepChecker::Dependence::getDestination(
779 const LoopAccessInfo &LAI) const {
780 return LAI.getDepChecker().getMemoryInstructions()[Destination];
781 }
782
783 } // End llvm namespace
784
785 #endif
786