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 
14 #include "llvm/Analysis/LoopAccessAnalysis.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/DepthFirstIterator.h"
18 #include "llvm/ADT/EquivalenceClasses.h"
19 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/iterator_range.h"
26 #include "llvm/Analysis/AliasAnalysis.h"
27 #include "llvm/Analysis/AliasSetTracker.h"
28 #include "llvm/Analysis/LoopAnalysisManager.h"
29 #include "llvm/Analysis/LoopInfo.h"
30 #include "llvm/Analysis/MemoryLocation.h"
31 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
32 #include "llvm/Analysis/ScalarEvolution.h"
33 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
34 #include "llvm/Analysis/TargetLibraryInfo.h"
35 #include "llvm/Analysis/ValueTracking.h"
36 #include "llvm/Analysis/VectorUtils.h"
37 #include "llvm/IR/BasicBlock.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/DataLayout.h"
40 #include "llvm/IR/DebugLoc.h"
41 #include "llvm/IR/DerivedTypes.h"
42 #include "llvm/IR/DiagnosticInfo.h"
43 #include "llvm/IR/Dominators.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/InstrTypes.h"
46 #include "llvm/IR/Instruction.h"
47 #include "llvm/IR/Instructions.h"
48 #include "llvm/IR/Operator.h"
49 #include "llvm/IR/PassManager.h"
50 #include "llvm/IR/Type.h"
51 #include "llvm/IR/Value.h"
52 #include "llvm/IR/ValueHandle.h"
53 #include "llvm/InitializePasses.h"
54 #include "llvm/Pass.h"
55 #include "llvm/Support/Casting.h"
56 #include "llvm/Support/CommandLine.h"
57 #include "llvm/Support/Debug.h"
58 #include "llvm/Support/ErrorHandling.h"
59 #include "llvm/Support/raw_ostream.h"
60 #include <algorithm>
61 #include <cassert>
62 #include <cstdint>
63 #include <cstdlib>
64 #include <iterator>
65 #include <utility>
66 #include <vector>
67 
68 using namespace llvm;
69 
70 #define DEBUG_TYPE "loop-accesses"
71 
72 static cl::opt<unsigned, true>
73 VectorizationFactor("force-vector-width", cl::Hidden,
74                     cl::desc("Sets the SIMD width. Zero is autoselect."),
75                     cl::location(VectorizerParams::VectorizationFactor));
76 unsigned VectorizerParams::VectorizationFactor;
77 
78 static cl::opt<unsigned, true>
79 VectorizationInterleave("force-vector-interleave", cl::Hidden,
80                         cl::desc("Sets the vectorization interleave count. "
81                                  "Zero is autoselect."),
82                         cl::location(
83                             VectorizerParams::VectorizationInterleave));
84 unsigned VectorizerParams::VectorizationInterleave;
85 
86 static cl::opt<unsigned, true> RuntimeMemoryCheckThreshold(
87     "runtime-memory-check-threshold", cl::Hidden,
88     cl::desc("When performing memory disambiguation checks at runtime do not "
89              "generate more than this number of comparisons (default = 8)."),
90     cl::location(VectorizerParams::RuntimeMemoryCheckThreshold), cl::init(8));
91 unsigned VectorizerParams::RuntimeMemoryCheckThreshold;
92 
93 /// The maximum iterations used to merge memory checks
94 static cl::opt<unsigned> MemoryCheckMergeThreshold(
95     "memory-check-merge-threshold", cl::Hidden,
96     cl::desc("Maximum number of comparisons done when trying to merge "
97              "runtime memory checks. (default = 100)"),
98     cl::init(100));
99 
100 /// Maximum SIMD width.
101 const unsigned VectorizerParams::MaxVectorWidth = 64;
102 
103 /// We collect dependences up to this threshold.
104 static cl::opt<unsigned>
105     MaxDependences("max-dependences", cl::Hidden,
106                    cl::desc("Maximum number of dependences collected by "
107                             "loop-access analysis (default = 100)"),
108                    cl::init(100));
109 
110 /// This enables versioning on the strides of symbolically striding memory
111 /// accesses in code like the following.
112 ///   for (i = 0; i < N; ++i)
113 ///     A[i * Stride1] += B[i * Stride2] ...
114 ///
115 /// Will be roughly translated to
116 ///    if (Stride1 == 1 && Stride2 == 1) {
117 ///      for (i = 0; i < N; i+=4)
118 ///       A[i:i+3] += ...
119 ///    } else
120 ///      ...
121 static cl::opt<bool> EnableMemAccessVersioning(
122     "enable-mem-access-versioning", cl::init(true), cl::Hidden,
123     cl::desc("Enable symbolic stride memory access versioning"));
124 
125 /// Enable store-to-load forwarding conflict detection. This option can
126 /// be disabled for correctness testing.
127 static cl::opt<bool> EnableForwardingConflictDetection(
128     "store-to-load-forwarding-conflict-detection", cl::Hidden,
129     cl::desc("Enable conflict detection in loop-access analysis"),
130     cl::init(true));
131 
132 bool VectorizerParams::isInterleaveForced() {
133   return ::VectorizationInterleave.getNumOccurrences() > 0;
134 }
135 
136 Value *llvm::stripIntegerCast(Value *V) {
137   if (auto *CI = dyn_cast<CastInst>(V))
138     if (CI->getOperand(0)->getType()->isIntegerTy())
139       return CI->getOperand(0);
140   return V;
141 }
142 
143 const SCEV *llvm::replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE,
144                                             const ValueToValueMap &PtrToStride,
145                                             Value *Ptr) {
146   const SCEV *OrigSCEV = PSE.getSCEV(Ptr);
147 
148   // If there is an entry in the map return the SCEV of the pointer with the
149   // symbolic stride replaced by one.
150   ValueToValueMap::const_iterator SI = PtrToStride.find(Ptr);
151   if (SI == PtrToStride.end())
152     // For a non-symbolic stride, just return the original expression.
153     return OrigSCEV;
154 
155   Value *StrideVal = stripIntegerCast(SI->second);
156 
157   ScalarEvolution *SE = PSE.getSE();
158   const auto *U = cast<SCEVUnknown>(SE->getSCEV(StrideVal));
159   const auto *CT =
160     static_cast<const SCEVConstant *>(SE->getOne(StrideVal->getType()));
161 
162   PSE.addPredicate(*SE->getEqualPredicate(U, CT));
163   auto *Expr = PSE.getSCEV(Ptr);
164 
165   LLVM_DEBUG(dbgs() << "LAA: Replacing SCEV: " << *OrigSCEV
166 	     << " by: " << *Expr << "\n");
167   return Expr;
168 }
169 
170 RuntimeCheckingPtrGroup::RuntimeCheckingPtrGroup(
171     unsigned Index, RuntimePointerChecking &RtCheck)
172     : High(RtCheck.Pointers[Index].End), Low(RtCheck.Pointers[Index].Start),
173       AddressSpace(RtCheck.Pointers[Index]
174                        .PointerValue->getType()
175                        ->getPointerAddressSpace()) {
176   Members.push_back(Index);
177 }
178 
179 /// Calculate Start and End points of memory access.
180 /// Let's assume A is the first access and B is a memory access on N-th loop
181 /// iteration. Then B is calculated as:
182 ///   B = A + Step*N .
183 /// Step value may be positive or negative.
184 /// N is a calculated back-edge taken count:
185 ///     N = (TripCount > 0) ? RoundDown(TripCount -1 , VF) : 0
186 /// Start and End points are calculated in the following way:
187 /// Start = UMIN(A, B) ; End = UMAX(A, B) + SizeOfElt,
188 /// where SizeOfElt is the size of single memory access in bytes.
189 ///
190 /// There is no conflict when the intervals are disjoint:
191 /// NoConflict = (P2.Start >= P1.End) || (P1.Start >= P2.End)
192 void RuntimePointerChecking::insert(Loop *Lp, Value *Ptr, Type *AccessTy,
193                                     bool WritePtr, unsigned DepSetId,
194                                     unsigned ASId,
195                                     const ValueToValueMap &Strides,
196                                     PredicatedScalarEvolution &PSE) {
197   // Get the stride replaced scev.
198   const SCEV *Sc = replaceSymbolicStrideSCEV(PSE, Strides, Ptr);
199   ScalarEvolution *SE = PSE.getSE();
200 
201   const SCEV *ScStart;
202   const SCEV *ScEnd;
203 
204   if (SE->isLoopInvariant(Sc, Lp)) {
205     ScStart = ScEnd = Sc;
206   } else {
207     const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Sc);
208     assert(AR && "Invalid addrec expression");
209     const SCEV *Ex = PSE.getBackedgeTakenCount();
210 
211     ScStart = AR->getStart();
212     ScEnd = AR->evaluateAtIteration(Ex, *SE);
213     const SCEV *Step = AR->getStepRecurrence(*SE);
214 
215     // For expressions with negative step, the upper bound is ScStart and the
216     // lower bound is ScEnd.
217     if (const auto *CStep = dyn_cast<SCEVConstant>(Step)) {
218       if (CStep->getValue()->isNegative())
219         std::swap(ScStart, ScEnd);
220     } else {
221       // Fallback case: the step is not constant, but we can still
222       // get the upper and lower bounds of the interval by using min/max
223       // expressions.
224       ScStart = SE->getUMinExpr(ScStart, ScEnd);
225       ScEnd = SE->getUMaxExpr(AR->getStart(), ScEnd);
226     }
227   }
228   // Add the size of the pointed element to ScEnd.
229   auto &DL = Lp->getHeader()->getModule()->getDataLayout();
230   Type *IdxTy = DL.getIndexType(Ptr->getType());
231   const SCEV *EltSizeSCEV = SE->getStoreSizeOfExpr(IdxTy, AccessTy);
232   ScEnd = SE->getAddExpr(ScEnd, EltSizeSCEV);
233 
234   Pointers.emplace_back(Ptr, ScStart, ScEnd, WritePtr, DepSetId, ASId, Sc);
235 }
236 
237 SmallVector<RuntimePointerCheck, 4>
238 RuntimePointerChecking::generateChecks() const {
239   SmallVector<RuntimePointerCheck, 4> Checks;
240 
241   for (unsigned I = 0; I < CheckingGroups.size(); ++I) {
242     for (unsigned J = I + 1; J < CheckingGroups.size(); ++J) {
243       const RuntimeCheckingPtrGroup &CGI = CheckingGroups[I];
244       const RuntimeCheckingPtrGroup &CGJ = CheckingGroups[J];
245 
246       if (needsChecking(CGI, CGJ))
247         Checks.push_back(std::make_pair(&CGI, &CGJ));
248     }
249   }
250   return Checks;
251 }
252 
253 void RuntimePointerChecking::generateChecks(
254     MemoryDepChecker::DepCandidates &DepCands, bool UseDependencies) {
255   assert(Checks.empty() && "Checks is not empty");
256   groupChecks(DepCands, UseDependencies);
257   Checks = generateChecks();
258 }
259 
260 bool RuntimePointerChecking::needsChecking(
261     const RuntimeCheckingPtrGroup &M, const RuntimeCheckingPtrGroup &N) const {
262   for (unsigned I = 0, EI = M.Members.size(); EI != I; ++I)
263     for (unsigned J = 0, EJ = N.Members.size(); EJ != J; ++J)
264       if (needsChecking(M.Members[I], N.Members[J]))
265         return true;
266   return false;
267 }
268 
269 /// Compare \p I and \p J and return the minimum.
270 /// Return nullptr in case we couldn't find an answer.
271 static const SCEV *getMinFromExprs(const SCEV *I, const SCEV *J,
272                                    ScalarEvolution *SE) {
273   const SCEV *Diff = SE->getMinusSCEV(J, I);
274   const SCEVConstant *C = dyn_cast<const SCEVConstant>(Diff);
275 
276   if (!C)
277     return nullptr;
278   if (C->getValue()->isNegative())
279     return J;
280   return I;
281 }
282 
283 bool RuntimeCheckingPtrGroup::addPointer(unsigned Index,
284                                          RuntimePointerChecking &RtCheck) {
285   return addPointer(
286       Index, RtCheck.Pointers[Index].Start, RtCheck.Pointers[Index].End,
287       RtCheck.Pointers[Index].PointerValue->getType()->getPointerAddressSpace(),
288       *RtCheck.SE);
289 }
290 
291 bool RuntimeCheckingPtrGroup::addPointer(unsigned Index, const SCEV *Start,
292                                          const SCEV *End, unsigned AS,
293                                          ScalarEvolution &SE) {
294   assert(AddressSpace == AS &&
295          "all pointers in a checking group must be in the same address space");
296 
297   // Compare the starts and ends with the known minimum and maximum
298   // of this set. We need to know how we compare against the min/max
299   // of the set in order to be able to emit memchecks.
300   const SCEV *Min0 = getMinFromExprs(Start, Low, &SE);
301   if (!Min0)
302     return false;
303 
304   const SCEV *Min1 = getMinFromExprs(End, High, &SE);
305   if (!Min1)
306     return false;
307 
308   // Update the low bound  expression if we've found a new min value.
309   if (Min0 == Start)
310     Low = Start;
311 
312   // Update the high bound expression if we've found a new max value.
313   if (Min1 != End)
314     High = End;
315 
316   Members.push_back(Index);
317   return true;
318 }
319 
320 void RuntimePointerChecking::groupChecks(
321     MemoryDepChecker::DepCandidates &DepCands, bool UseDependencies) {
322   // We build the groups from dependency candidates equivalence classes
323   // because:
324   //    - We know that pointers in the same equivalence class share
325   //      the same underlying object and therefore there is a chance
326   //      that we can compare pointers
327   //    - We wouldn't be able to merge two pointers for which we need
328   //      to emit a memcheck. The classes in DepCands are already
329   //      conveniently built such that no two pointers in the same
330   //      class need checking against each other.
331 
332   // We use the following (greedy) algorithm to construct the groups
333   // For every pointer in the equivalence class:
334   //   For each existing group:
335   //   - if the difference between this pointer and the min/max bounds
336   //     of the group is a constant, then make the pointer part of the
337   //     group and update the min/max bounds of that group as required.
338 
339   CheckingGroups.clear();
340 
341   // If we need to check two pointers to the same underlying object
342   // with a non-constant difference, we shouldn't perform any pointer
343   // grouping with those pointers. This is because we can easily get
344   // into cases where the resulting check would return false, even when
345   // the accesses are safe.
346   //
347   // The following example shows this:
348   // for (i = 0; i < 1000; ++i)
349   //   a[5000 + i * m] = a[i] + a[i + 9000]
350   //
351   // Here grouping gives a check of (5000, 5000 + 1000 * m) against
352   // (0, 10000) which is always false. However, if m is 1, there is no
353   // dependence. Not grouping the checks for a[i] and a[i + 9000] allows
354   // us to perform an accurate check in this case.
355   //
356   // The above case requires that we have an UnknownDependence between
357   // accesses to the same underlying object. This cannot happen unless
358   // FoundNonConstantDistanceDependence is set, and therefore UseDependencies
359   // is also false. In this case we will use the fallback path and create
360   // separate checking groups for all pointers.
361 
362   // If we don't have the dependency partitions, construct a new
363   // checking pointer group for each pointer. This is also required
364   // for correctness, because in this case we can have checking between
365   // pointers to the same underlying object.
366   if (!UseDependencies) {
367     for (unsigned I = 0; I < Pointers.size(); ++I)
368       CheckingGroups.push_back(RuntimeCheckingPtrGroup(I, *this));
369     return;
370   }
371 
372   unsigned TotalComparisons = 0;
373 
374   DenseMap<Value *, unsigned> PositionMap;
375   for (unsigned Index = 0; Index < Pointers.size(); ++Index)
376     PositionMap[Pointers[Index].PointerValue] = Index;
377 
378   // We need to keep track of what pointers we've already seen so we
379   // don't process them twice.
380   SmallSet<unsigned, 2> Seen;
381 
382   // Go through all equivalence classes, get the "pointer check groups"
383   // and add them to the overall solution. We use the order in which accesses
384   // appear in 'Pointers' to enforce determinism.
385   for (unsigned I = 0; I < Pointers.size(); ++I) {
386     // We've seen this pointer before, and therefore already processed
387     // its equivalence class.
388     if (Seen.count(I))
389       continue;
390 
391     MemoryDepChecker::MemAccessInfo Access(Pointers[I].PointerValue,
392                                            Pointers[I].IsWritePtr);
393 
394     SmallVector<RuntimeCheckingPtrGroup, 2> Groups;
395     auto LeaderI = DepCands.findValue(DepCands.getLeaderValue(Access));
396 
397     // Because DepCands is constructed by visiting accesses in the order in
398     // which they appear in alias sets (which is deterministic) and the
399     // iteration order within an equivalence class member is only dependent on
400     // the order in which unions and insertions are performed on the
401     // equivalence class, the iteration order is deterministic.
402     for (auto MI = DepCands.member_begin(LeaderI), ME = DepCands.member_end();
403          MI != ME; ++MI) {
404       auto PointerI = PositionMap.find(MI->getPointer());
405       assert(PointerI != PositionMap.end() &&
406              "pointer in equivalence class not found in PositionMap");
407       unsigned Pointer = PointerI->second;
408       bool Merged = false;
409       // Mark this pointer as seen.
410       Seen.insert(Pointer);
411 
412       // Go through all the existing sets and see if we can find one
413       // which can include this pointer.
414       for (RuntimeCheckingPtrGroup &Group : Groups) {
415         // Don't perform more than a certain amount of comparisons.
416         // This should limit the cost of grouping the pointers to something
417         // reasonable.  If we do end up hitting this threshold, the algorithm
418         // will create separate groups for all remaining pointers.
419         if (TotalComparisons > MemoryCheckMergeThreshold)
420           break;
421 
422         TotalComparisons++;
423 
424         if (Group.addPointer(Pointer, *this)) {
425           Merged = true;
426           break;
427         }
428       }
429 
430       if (!Merged)
431         // We couldn't add this pointer to any existing set or the threshold
432         // for the number of comparisons has been reached. Create a new group
433         // to hold the current pointer.
434         Groups.push_back(RuntimeCheckingPtrGroup(Pointer, *this));
435     }
436 
437     // We've computed the grouped checks for this partition.
438     // Save the results and continue with the next one.
439     llvm::copy(Groups, std::back_inserter(CheckingGroups));
440   }
441 }
442 
443 bool RuntimePointerChecking::arePointersInSamePartition(
444     const SmallVectorImpl<int> &PtrToPartition, unsigned PtrIdx1,
445     unsigned PtrIdx2) {
446   return (PtrToPartition[PtrIdx1] != -1 &&
447           PtrToPartition[PtrIdx1] == PtrToPartition[PtrIdx2]);
448 }
449 
450 bool RuntimePointerChecking::needsChecking(unsigned I, unsigned J) const {
451   const PointerInfo &PointerI = Pointers[I];
452   const PointerInfo &PointerJ = Pointers[J];
453 
454   // No need to check if two readonly pointers intersect.
455   if (!PointerI.IsWritePtr && !PointerJ.IsWritePtr)
456     return false;
457 
458   // Only need to check pointers between two different dependency sets.
459   if (PointerI.DependencySetId == PointerJ.DependencySetId)
460     return false;
461 
462   // Only need to check pointers in the same alias set.
463   if (PointerI.AliasSetId != PointerJ.AliasSetId)
464     return false;
465 
466   return true;
467 }
468 
469 void RuntimePointerChecking::printChecks(
470     raw_ostream &OS, const SmallVectorImpl<RuntimePointerCheck> &Checks,
471     unsigned Depth) const {
472   unsigned N = 0;
473   for (const auto &Check : Checks) {
474     const auto &First = Check.first->Members, &Second = Check.second->Members;
475 
476     OS.indent(Depth) << "Check " << N++ << ":\n";
477 
478     OS.indent(Depth + 2) << "Comparing group (" << Check.first << "):\n";
479     for (unsigned K = 0; K < First.size(); ++K)
480       OS.indent(Depth + 2) << *Pointers[First[K]].PointerValue << "\n";
481 
482     OS.indent(Depth + 2) << "Against group (" << Check.second << "):\n";
483     for (unsigned K = 0; K < Second.size(); ++K)
484       OS.indent(Depth + 2) << *Pointers[Second[K]].PointerValue << "\n";
485   }
486 }
487 
488 void RuntimePointerChecking::print(raw_ostream &OS, unsigned Depth) const {
489 
490   OS.indent(Depth) << "Run-time memory checks:\n";
491   printChecks(OS, Checks, Depth);
492 
493   OS.indent(Depth) << "Grouped accesses:\n";
494   for (unsigned I = 0; I < CheckingGroups.size(); ++I) {
495     const auto &CG = CheckingGroups[I];
496 
497     OS.indent(Depth + 2) << "Group " << &CG << ":\n";
498     OS.indent(Depth + 4) << "(Low: " << *CG.Low << " High: " << *CG.High
499                          << ")\n";
500     for (unsigned J = 0; J < CG.Members.size(); ++J) {
501       OS.indent(Depth + 6) << "Member: " << *Pointers[CG.Members[J]].Expr
502                            << "\n";
503     }
504   }
505 }
506 
507 namespace {
508 
509 /// Analyses memory accesses in a loop.
510 ///
511 /// Checks whether run time pointer checks are needed and builds sets for data
512 /// dependence checking.
513 class AccessAnalysis {
514 public:
515   /// Read or write access location.
516   typedef PointerIntPair<Value *, 1, bool> MemAccessInfo;
517   typedef SmallVector<MemAccessInfo, 8> MemAccessInfoList;
518 
519   AccessAnalysis(Loop *TheLoop, AAResults *AA, LoopInfo *LI,
520                  MemoryDepChecker::DepCandidates &DA,
521                  PredicatedScalarEvolution &PSE)
522       : TheLoop(TheLoop), AST(*AA), LI(LI), DepCands(DA), PSE(PSE) {}
523 
524   /// Register a load  and whether it is only read from.
525   void addLoad(MemoryLocation &Loc, Type *AccessTy, bool IsReadOnly) {
526     Value *Ptr = const_cast<Value*>(Loc.Ptr);
527     AST.add(Ptr, LocationSize::beforeOrAfterPointer(), Loc.AATags);
528     Accesses[MemAccessInfo(Ptr, false)].insert(AccessTy);
529     if (IsReadOnly)
530       ReadOnlyPtr.insert(Ptr);
531   }
532 
533   /// Register a store.
534   void addStore(MemoryLocation &Loc, Type *AccessTy) {
535     Value *Ptr = const_cast<Value*>(Loc.Ptr);
536     AST.add(Ptr, LocationSize::beforeOrAfterPointer(), Loc.AATags);
537     Accesses[MemAccessInfo(Ptr, true)].insert(AccessTy);
538   }
539 
540   /// Check if we can emit a run-time no-alias check for \p Access.
541   ///
542   /// Returns true if we can emit a run-time no alias check for \p Access.
543   /// If we can check this access, this also adds it to a dependence set and
544   /// adds a run-time to check for it to \p RtCheck. If \p Assume is true,
545   /// we will attempt to use additional run-time checks in order to get
546   /// the bounds of the pointer.
547   bool createCheckForAccess(RuntimePointerChecking &RtCheck,
548                             MemAccessInfo Access, Type *AccessTy,
549                             const ValueToValueMap &Strides,
550                             DenseMap<Value *, unsigned> &DepSetId,
551                             Loop *TheLoop, unsigned &RunningDepId,
552                             unsigned ASId, bool ShouldCheckStride, bool Assume);
553 
554   /// Check whether we can check the pointers at runtime for
555   /// non-intersection.
556   ///
557   /// Returns true if we need no check or if we do and we can generate them
558   /// (i.e. the pointers have computable bounds).
559   bool canCheckPtrAtRT(RuntimePointerChecking &RtCheck, ScalarEvolution *SE,
560                        Loop *TheLoop, const ValueToValueMap &Strides,
561                        bool ShouldCheckWrap = false);
562 
563   /// Goes over all memory accesses, checks whether a RT check is needed
564   /// and builds sets of dependent accesses.
565   void buildDependenceSets() {
566     processMemAccesses();
567   }
568 
569   /// Initial processing of memory accesses determined that we need to
570   /// perform dependency checking.
571   ///
572   /// Note that this can later be cleared if we retry memcheck analysis without
573   /// dependency checking (i.e. FoundNonConstantDistanceDependence).
574   bool isDependencyCheckNeeded() { return !CheckDeps.empty(); }
575 
576   /// We decided that no dependence analysis would be used.  Reset the state.
577   void resetDepChecks(MemoryDepChecker &DepChecker) {
578     CheckDeps.clear();
579     DepChecker.clearDependences();
580   }
581 
582   MemAccessInfoList &getDependenciesToCheck() { return CheckDeps; }
583 
584 private:
585   typedef MapVector<MemAccessInfo, SmallSetVector<Type *, 1>> PtrAccessMap;
586 
587   /// Go over all memory access and check whether runtime pointer checks
588   /// are needed and build sets of dependency check candidates.
589   void processMemAccesses();
590 
591   /// Map of all accesses. Values are the types used to access memory pointed to
592   /// by the pointer.
593   PtrAccessMap Accesses;
594 
595   /// The loop being checked.
596   const Loop *TheLoop;
597 
598   /// List of accesses that need a further dependence check.
599   MemAccessInfoList CheckDeps;
600 
601   /// Set of pointers that are read only.
602   SmallPtrSet<Value*, 16> ReadOnlyPtr;
603 
604   /// An alias set tracker to partition the access set by underlying object and
605   //intrinsic property (such as TBAA metadata).
606   AliasSetTracker AST;
607 
608   LoopInfo *LI;
609 
610   /// Sets of potentially dependent accesses - members of one set share an
611   /// underlying pointer. The set "CheckDeps" identfies which sets really need a
612   /// dependence check.
613   MemoryDepChecker::DepCandidates &DepCands;
614 
615   /// Initial processing of memory accesses determined that we may need
616   /// to add memchecks.  Perform the analysis to determine the necessary checks.
617   ///
618   /// Note that, this is different from isDependencyCheckNeeded.  When we retry
619   /// memcheck analysis without dependency checking
620   /// (i.e. FoundNonConstantDistanceDependence), isDependencyCheckNeeded is
621   /// cleared while this remains set if we have potentially dependent accesses.
622   bool IsRTCheckAnalysisNeeded = false;
623 
624   /// The SCEV predicate containing all the SCEV-related assumptions.
625   PredicatedScalarEvolution &PSE;
626 };
627 
628 } // end anonymous namespace
629 
630 /// Check whether a pointer can participate in a runtime bounds check.
631 /// If \p Assume, try harder to prove that we can compute the bounds of \p Ptr
632 /// by adding run-time checks (overflow checks) if necessary.
633 static bool hasComputableBounds(PredicatedScalarEvolution &PSE,
634                                 const ValueToValueMap &Strides, Value *Ptr,
635                                 Loop *L, bool Assume) {
636   const SCEV *PtrScev = replaceSymbolicStrideSCEV(PSE, Strides, Ptr);
637 
638   // The bounds for loop-invariant pointer is trivial.
639   if (PSE.getSE()->isLoopInvariant(PtrScev, L))
640     return true;
641 
642   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
643 
644   if (!AR && Assume)
645     AR = PSE.getAsAddRec(Ptr);
646 
647   if (!AR)
648     return false;
649 
650   return AR->isAffine();
651 }
652 
653 /// Check whether a pointer address cannot wrap.
654 static bool isNoWrap(PredicatedScalarEvolution &PSE,
655                      const ValueToValueMap &Strides, Value *Ptr, Type *AccessTy,
656                      Loop *L) {
657   const SCEV *PtrScev = PSE.getSCEV(Ptr);
658   if (PSE.getSE()->isLoopInvariant(PtrScev, L))
659     return true;
660 
661   int64_t Stride = getPtrStride(PSE, AccessTy, Ptr, L, Strides);
662   if (Stride == 1 || PSE.hasNoOverflow(Ptr, SCEVWrapPredicate::IncrementNUSW))
663     return true;
664 
665   return false;
666 }
667 
668 static void visitPointers(Value *StartPtr, const Loop &InnermostLoop,
669                           function_ref<void(Value *)> AddPointer) {
670   SmallPtrSet<Value *, 8> Visited;
671   SmallVector<Value *> WorkList;
672   WorkList.push_back(StartPtr);
673 
674   while (!WorkList.empty()) {
675     Value *Ptr = WorkList.pop_back_val();
676     if (!Visited.insert(Ptr).second)
677       continue;
678     auto *PN = dyn_cast<PHINode>(Ptr);
679     // SCEV does not look through non-header PHIs inside the loop. Such phis
680     // can be analyzed by adding separate accesses for each incoming pointer
681     // value.
682     if (PN && InnermostLoop.contains(PN->getParent()) &&
683         PN->getParent() != InnermostLoop.getHeader()) {
684       for (const Use &Inc : PN->incoming_values())
685         WorkList.push_back(Inc);
686     } else
687       AddPointer(Ptr);
688   }
689 }
690 
691 bool AccessAnalysis::createCheckForAccess(RuntimePointerChecking &RtCheck,
692                                           MemAccessInfo Access, Type *AccessTy,
693                                           const ValueToValueMap &StridesMap,
694                                           DenseMap<Value *, unsigned> &DepSetId,
695                                           Loop *TheLoop, unsigned &RunningDepId,
696                                           unsigned ASId, bool ShouldCheckWrap,
697                                           bool Assume) {
698   Value *Ptr = Access.getPointer();
699 
700   if (!hasComputableBounds(PSE, StridesMap, Ptr, TheLoop, Assume))
701     return false;
702 
703   // When we run after a failing dependency check we have to make sure
704   // we don't have wrapping pointers.
705   if (ShouldCheckWrap && !isNoWrap(PSE, StridesMap, Ptr, AccessTy, TheLoop)) {
706     auto *Expr = PSE.getSCEV(Ptr);
707     if (!Assume || !isa<SCEVAddRecExpr>(Expr))
708       return false;
709     PSE.setNoOverflow(Ptr, SCEVWrapPredicate::IncrementNUSW);
710   }
711 
712   // The id of the dependence set.
713   unsigned DepId;
714 
715   if (isDependencyCheckNeeded()) {
716     Value *Leader = DepCands.getLeaderValue(Access).getPointer();
717     unsigned &LeaderId = DepSetId[Leader];
718     if (!LeaderId)
719       LeaderId = RunningDepId++;
720     DepId = LeaderId;
721   } else
722     // Each access has its own dependence set.
723     DepId = RunningDepId++;
724 
725   bool IsWrite = Access.getInt();
726   RtCheck.insert(TheLoop, Ptr, AccessTy, IsWrite, DepId, ASId, StridesMap, PSE);
727   LLVM_DEBUG(dbgs() << "LAA: Found a runtime check ptr:" << *Ptr << '\n');
728 
729   return true;
730 }
731 
732 bool AccessAnalysis::canCheckPtrAtRT(RuntimePointerChecking &RtCheck,
733                                      ScalarEvolution *SE, Loop *TheLoop,
734                                      const ValueToValueMap &StridesMap,
735                                      bool ShouldCheckWrap) {
736   // Find pointers with computable bounds. We are going to use this information
737   // to place a runtime bound check.
738   bool CanDoRT = true;
739 
740   bool MayNeedRTCheck = false;
741   if (!IsRTCheckAnalysisNeeded) return true;
742 
743   bool IsDepCheckNeeded = isDependencyCheckNeeded();
744 
745   // We assign a consecutive id to access from different alias sets.
746   // Accesses between different groups doesn't need to be checked.
747   unsigned ASId = 0;
748   for (auto &AS : AST) {
749     int NumReadPtrChecks = 0;
750     int NumWritePtrChecks = 0;
751     bool CanDoAliasSetRT = true;
752     ++ASId;
753 
754     // We assign consecutive id to access from different dependence sets.
755     // Accesses within the same set don't need a runtime check.
756     unsigned RunningDepId = 1;
757     DenseMap<Value *, unsigned> DepSetId;
758 
759     SmallVector<MemAccessInfo, 4> Retries;
760 
761     // First, count how many write and read accesses are in the alias set. Also
762     // collect MemAccessInfos for later.
763     SmallVector<MemAccessInfo, 4> AccessInfos;
764     for (const auto &A : AS) {
765       Value *Ptr = A.getValue();
766       bool IsWrite = Accesses.count(MemAccessInfo(Ptr, true));
767 
768       if (IsWrite)
769         ++NumWritePtrChecks;
770       else
771         ++NumReadPtrChecks;
772       AccessInfos.emplace_back(Ptr, IsWrite);
773     }
774 
775     // We do not need runtime checks for this alias set, if there are no writes
776     // or a single write and no reads.
777     if (NumWritePtrChecks == 0 ||
778         (NumWritePtrChecks == 1 && NumReadPtrChecks == 0)) {
779       assert((AS.size() <= 1 ||
780               all_of(AS,
781                      [this](auto AC) {
782                        MemAccessInfo AccessWrite(AC.getValue(), true);
783                        return DepCands.findValue(AccessWrite) == DepCands.end();
784                      })) &&
785              "Can only skip updating CanDoRT below, if all entries in AS "
786              "are reads or there is at most 1 entry");
787       continue;
788     }
789 
790     for (auto &Access : AccessInfos) {
791       for (auto &AccessTy : Accesses[Access]) {
792         if (!createCheckForAccess(RtCheck, Access, AccessTy, StridesMap,
793                                   DepSetId, TheLoop, RunningDepId, ASId,
794                                   ShouldCheckWrap, false)) {
795           LLVM_DEBUG(dbgs() << "LAA: Can't find bounds for ptr:"
796                             << *Access.getPointer() << '\n');
797           Retries.push_back(Access);
798           CanDoAliasSetRT = false;
799         }
800       }
801     }
802 
803     // Note that this function computes CanDoRT and MayNeedRTCheck
804     // independently. For example CanDoRT=false, MayNeedRTCheck=false means that
805     // we have a pointer for which we couldn't find the bounds but we don't
806     // actually need to emit any checks so it does not matter.
807     //
808     // We need runtime checks for this alias set, if there are at least 2
809     // dependence sets (in which case RunningDepId > 2) or if we need to re-try
810     // any bound checks (because in that case the number of dependence sets is
811     // incomplete).
812     bool NeedsAliasSetRTCheck = RunningDepId > 2 || !Retries.empty();
813 
814     // We need to perform run-time alias checks, but some pointers had bounds
815     // that couldn't be checked.
816     if (NeedsAliasSetRTCheck && !CanDoAliasSetRT) {
817       // Reset the CanDoSetRt flag and retry all accesses that have failed.
818       // We know that we need these checks, so we can now be more aggressive
819       // and add further checks if required (overflow checks).
820       CanDoAliasSetRT = true;
821       for (auto Access : Retries) {
822         for (auto &AccessTy : Accesses[Access]) {
823           if (!createCheckForAccess(RtCheck, Access, AccessTy, StridesMap,
824                                     DepSetId, TheLoop, RunningDepId, ASId,
825                                     ShouldCheckWrap, /*Assume=*/true)) {
826             CanDoAliasSetRT = false;
827             break;
828           }
829         }
830       }
831     }
832 
833     CanDoRT &= CanDoAliasSetRT;
834     MayNeedRTCheck |= NeedsAliasSetRTCheck;
835     ++ASId;
836   }
837 
838   // If the pointers that we would use for the bounds comparison have different
839   // address spaces, assume the values aren't directly comparable, so we can't
840   // use them for the runtime check. We also have to assume they could
841   // overlap. In the future there should be metadata for whether address spaces
842   // are disjoint.
843   unsigned NumPointers = RtCheck.Pointers.size();
844   for (unsigned i = 0; i < NumPointers; ++i) {
845     for (unsigned j = i + 1; j < NumPointers; ++j) {
846       // Only need to check pointers between two different dependency sets.
847       if (RtCheck.Pointers[i].DependencySetId ==
848           RtCheck.Pointers[j].DependencySetId)
849        continue;
850       // Only need to check pointers in the same alias set.
851       if (RtCheck.Pointers[i].AliasSetId != RtCheck.Pointers[j].AliasSetId)
852         continue;
853 
854       Value *PtrI = RtCheck.Pointers[i].PointerValue;
855       Value *PtrJ = RtCheck.Pointers[j].PointerValue;
856 
857       unsigned ASi = PtrI->getType()->getPointerAddressSpace();
858       unsigned ASj = PtrJ->getType()->getPointerAddressSpace();
859       if (ASi != ASj) {
860         LLVM_DEBUG(
861             dbgs() << "LAA: Runtime check would require comparison between"
862                       " different address spaces\n");
863         return false;
864       }
865     }
866   }
867 
868   if (MayNeedRTCheck && CanDoRT)
869     RtCheck.generateChecks(DepCands, IsDepCheckNeeded);
870 
871   LLVM_DEBUG(dbgs() << "LAA: We need to do " << RtCheck.getNumberOfChecks()
872                     << " pointer comparisons.\n");
873 
874   // If we can do run-time checks, but there are no checks, no runtime checks
875   // are needed. This can happen when all pointers point to the same underlying
876   // object for example.
877   RtCheck.Need = CanDoRT ? RtCheck.getNumberOfChecks() != 0 : MayNeedRTCheck;
878 
879   bool CanDoRTIfNeeded = !RtCheck.Need || CanDoRT;
880   if (!CanDoRTIfNeeded)
881     RtCheck.reset();
882   return CanDoRTIfNeeded;
883 }
884 
885 void AccessAnalysis::processMemAccesses() {
886   // We process the set twice: first we process read-write pointers, last we
887   // process read-only pointers. This allows us to skip dependence tests for
888   // read-only pointers.
889 
890   LLVM_DEBUG(dbgs() << "LAA: Processing memory accesses...\n");
891   LLVM_DEBUG(dbgs() << "  AST: "; AST.dump());
892   LLVM_DEBUG(dbgs() << "LAA:   Accesses(" << Accesses.size() << "):\n");
893   LLVM_DEBUG({
894     for (auto A : Accesses)
895       dbgs() << "\t" << *A.first.getPointer() << " ("
896              << (A.first.getInt()
897                      ? "write"
898                      : (ReadOnlyPtr.count(A.first.getPointer()) ? "read-only"
899                                                                 : "read"))
900              << ")\n";
901   });
902 
903   // The AliasSetTracker has nicely partitioned our pointers by metadata
904   // compatibility and potential for underlying-object overlap. As a result, we
905   // only need to check for potential pointer dependencies within each alias
906   // set.
907   for (const auto &AS : AST) {
908     // Note that both the alias-set tracker and the alias sets themselves used
909     // linked lists internally and so the iteration order here is deterministic
910     // (matching the original instruction order within each set).
911 
912     bool SetHasWrite = false;
913 
914     // Map of pointers to last access encountered.
915     typedef DenseMap<const Value*, MemAccessInfo> UnderlyingObjToAccessMap;
916     UnderlyingObjToAccessMap ObjToLastAccess;
917 
918     // Set of access to check after all writes have been processed.
919     PtrAccessMap DeferredAccesses;
920 
921     // Iterate over each alias set twice, once to process read/write pointers,
922     // and then to process read-only pointers.
923     for (int SetIteration = 0; SetIteration < 2; ++SetIteration) {
924       bool UseDeferred = SetIteration > 0;
925       PtrAccessMap &S = UseDeferred ? DeferredAccesses : Accesses;
926 
927       for (const auto &AV : AS) {
928         Value *Ptr = AV.getValue();
929 
930         // For a single memory access in AliasSetTracker, Accesses may contain
931         // both read and write, and they both need to be handled for CheckDeps.
932         for (const auto &AC : S) {
933           if (AC.first.getPointer() != Ptr)
934             continue;
935 
936           bool IsWrite = AC.first.getInt();
937 
938           // If we're using the deferred access set, then it contains only
939           // reads.
940           bool IsReadOnlyPtr = ReadOnlyPtr.count(Ptr) && !IsWrite;
941           if (UseDeferred && !IsReadOnlyPtr)
942             continue;
943           // Otherwise, the pointer must be in the PtrAccessSet, either as a
944           // read or a write.
945           assert(((IsReadOnlyPtr && UseDeferred) || IsWrite ||
946                   S.count(MemAccessInfo(Ptr, false))) &&
947                  "Alias-set pointer not in the access set?");
948 
949           MemAccessInfo Access(Ptr, IsWrite);
950           DepCands.insert(Access);
951 
952           // Memorize read-only pointers for later processing and skip them in
953           // the first round (they need to be checked after we have seen all
954           // write pointers). Note: we also mark pointer that are not
955           // consecutive as "read-only" pointers (so that we check
956           // "a[b[i]] +="). Hence, we need the second check for "!IsWrite".
957           if (!UseDeferred && IsReadOnlyPtr) {
958             // We only use the pointer keys, the types vector values don't
959             // matter.
960             DeferredAccesses.insert({Access, {}});
961             continue;
962           }
963 
964           // If this is a write - check other reads and writes for conflicts. If
965           // this is a read only check other writes for conflicts (but only if
966           // there is no other write to the ptr - this is an optimization to
967           // catch "a[i] = a[i] + " without having to do a dependence check).
968           if ((IsWrite || IsReadOnlyPtr) && SetHasWrite) {
969             CheckDeps.push_back(Access);
970             IsRTCheckAnalysisNeeded = true;
971           }
972 
973           if (IsWrite)
974             SetHasWrite = true;
975 
976           // Create sets of pointers connected by a shared alias set and
977           // underlying object.
978           typedef SmallVector<const Value *, 16> ValueVector;
979           ValueVector TempObjects;
980 
981           getUnderlyingObjects(Ptr, TempObjects, LI);
982           LLVM_DEBUG(dbgs()
983                      << "Underlying objects for pointer " << *Ptr << "\n");
984           for (const Value *UnderlyingObj : TempObjects) {
985             // nullptr never alias, don't join sets for pointer that have "null"
986             // in their UnderlyingObjects list.
987             if (isa<ConstantPointerNull>(UnderlyingObj) &&
988                 !NullPointerIsDefined(
989                     TheLoop->getHeader()->getParent(),
990                     UnderlyingObj->getType()->getPointerAddressSpace()))
991               continue;
992 
993             UnderlyingObjToAccessMap::iterator Prev =
994                 ObjToLastAccess.find(UnderlyingObj);
995             if (Prev != ObjToLastAccess.end())
996               DepCands.unionSets(Access, Prev->second);
997 
998             ObjToLastAccess[UnderlyingObj] = Access;
999             LLVM_DEBUG(dbgs() << "  " << *UnderlyingObj << "\n");
1000           }
1001         }
1002       }
1003     }
1004   }
1005 }
1006 
1007 static bool isInBoundsGep(Value *Ptr) {
1008   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
1009     return GEP->isInBounds();
1010   return false;
1011 }
1012 
1013 /// Return true if an AddRec pointer \p Ptr is unsigned non-wrapping,
1014 /// i.e. monotonically increasing/decreasing.
1015 static bool isNoWrapAddRec(Value *Ptr, const SCEVAddRecExpr *AR,
1016                            PredicatedScalarEvolution &PSE, const Loop *L) {
1017   // FIXME: This should probably only return true for NUW.
1018   if (AR->getNoWrapFlags(SCEV::NoWrapMask))
1019     return true;
1020 
1021   // Scalar evolution does not propagate the non-wrapping flags to values that
1022   // are derived from a non-wrapping induction variable because non-wrapping
1023   // could be flow-sensitive.
1024   //
1025   // Look through the potentially overflowing instruction to try to prove
1026   // non-wrapping for the *specific* value of Ptr.
1027 
1028   // The arithmetic implied by an inbounds GEP can't overflow.
1029   auto *GEP = dyn_cast<GetElementPtrInst>(Ptr);
1030   if (!GEP || !GEP->isInBounds())
1031     return false;
1032 
1033   // Make sure there is only one non-const index and analyze that.
1034   Value *NonConstIndex = nullptr;
1035   for (Value *Index : GEP->indices())
1036     if (!isa<ConstantInt>(Index)) {
1037       if (NonConstIndex)
1038         return false;
1039       NonConstIndex = Index;
1040     }
1041   if (!NonConstIndex)
1042     // The recurrence is on the pointer, ignore for now.
1043     return false;
1044 
1045   // The index in GEP is signed.  It is non-wrapping if it's derived from a NSW
1046   // AddRec using a NSW operation.
1047   if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(NonConstIndex))
1048     if (OBO->hasNoSignedWrap() &&
1049         // Assume constant for other the operand so that the AddRec can be
1050         // easily found.
1051         isa<ConstantInt>(OBO->getOperand(1))) {
1052       auto *OpScev = PSE.getSCEV(OBO->getOperand(0));
1053 
1054       if (auto *OpAR = dyn_cast<SCEVAddRecExpr>(OpScev))
1055         return OpAR->getLoop() == L && OpAR->getNoWrapFlags(SCEV::FlagNSW);
1056     }
1057 
1058   return false;
1059 }
1060 
1061 /// Check whether the access through \p Ptr has a constant stride.
1062 int64_t llvm::getPtrStride(PredicatedScalarEvolution &PSE, Type *AccessTy,
1063                            Value *Ptr, const Loop *Lp,
1064                            const ValueToValueMap &StridesMap, bool Assume,
1065                            bool ShouldCheckWrap) {
1066   Type *Ty = Ptr->getType();
1067   assert(Ty->isPointerTy() && "Unexpected non-ptr");
1068 
1069   if (isa<ScalableVectorType>(AccessTy)) {
1070     LLVM_DEBUG(dbgs() << "LAA: Bad stride - Scalable object: " << *AccessTy
1071                       << "\n");
1072     return 0;
1073   }
1074 
1075   const SCEV *PtrScev = replaceSymbolicStrideSCEV(PSE, StridesMap, Ptr);
1076 
1077   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
1078   if (Assume && !AR)
1079     AR = PSE.getAsAddRec(Ptr);
1080 
1081   if (!AR) {
1082     LLVM_DEBUG(dbgs() << "LAA: Bad stride - Not an AddRecExpr pointer " << *Ptr
1083                       << " SCEV: " << *PtrScev << "\n");
1084     return 0;
1085   }
1086 
1087   // The access function must stride over the innermost loop.
1088   if (Lp != AR->getLoop()) {
1089     LLVM_DEBUG(dbgs() << "LAA: Bad stride - Not striding over innermost loop "
1090                       << *Ptr << " SCEV: " << *AR << "\n");
1091     return 0;
1092   }
1093 
1094   // The address calculation must not wrap. Otherwise, a dependence could be
1095   // inverted.
1096   // An inbounds getelementptr that is a AddRec with a unit stride
1097   // cannot wrap per definition. The unit stride requirement is checked later.
1098   // An getelementptr without an inbounds attribute and unit stride would have
1099   // to access the pointer value "0" which is undefined behavior in address
1100   // space 0, therefore we can also vectorize this case.
1101   unsigned AddrSpace = Ty->getPointerAddressSpace();
1102   bool IsInBoundsGEP = isInBoundsGep(Ptr);
1103   bool IsNoWrapAddRec = !ShouldCheckWrap ||
1104     PSE.hasNoOverflow(Ptr, SCEVWrapPredicate::IncrementNUSW) ||
1105     isNoWrapAddRec(Ptr, AR, PSE, Lp);
1106   if (!IsNoWrapAddRec && !IsInBoundsGEP &&
1107       NullPointerIsDefined(Lp->getHeader()->getParent(), AddrSpace)) {
1108     if (Assume) {
1109       PSE.setNoOverflow(Ptr, SCEVWrapPredicate::IncrementNUSW);
1110       IsNoWrapAddRec = true;
1111       LLVM_DEBUG(dbgs() << "LAA: Pointer may wrap in the address space:\n"
1112                         << "LAA:   Pointer: " << *Ptr << "\n"
1113                         << "LAA:   SCEV: " << *AR << "\n"
1114                         << "LAA:   Added an overflow assumption\n");
1115     } else {
1116       LLVM_DEBUG(
1117           dbgs() << "LAA: Bad stride - Pointer may wrap in the address space "
1118                  << *Ptr << " SCEV: " << *AR << "\n");
1119       return 0;
1120     }
1121   }
1122 
1123   // Check the step is constant.
1124   const SCEV *Step = AR->getStepRecurrence(*PSE.getSE());
1125 
1126   // Calculate the pointer stride and check if it is constant.
1127   const SCEVConstant *C = dyn_cast<SCEVConstant>(Step);
1128   if (!C) {
1129     LLVM_DEBUG(dbgs() << "LAA: Bad stride - Not a constant strided " << *Ptr
1130                       << " SCEV: " << *AR << "\n");
1131     return 0;
1132   }
1133 
1134   auto &DL = Lp->getHeader()->getModule()->getDataLayout();
1135   TypeSize AllocSize = DL.getTypeAllocSize(AccessTy);
1136   int64_t Size = AllocSize.getFixedSize();
1137   const APInt &APStepVal = C->getAPInt();
1138 
1139   // Huge step value - give up.
1140   if (APStepVal.getBitWidth() > 64)
1141     return 0;
1142 
1143   int64_t StepVal = APStepVal.getSExtValue();
1144 
1145   // Strided access.
1146   int64_t Stride = StepVal / Size;
1147   int64_t Rem = StepVal % Size;
1148   if (Rem)
1149     return 0;
1150 
1151   // If the SCEV could wrap but we have an inbounds gep with a unit stride we
1152   // know we can't "wrap around the address space". In case of address space
1153   // zero we know that this won't happen without triggering undefined behavior.
1154   if (!IsNoWrapAddRec && Stride != 1 && Stride != -1 &&
1155       (IsInBoundsGEP || !NullPointerIsDefined(Lp->getHeader()->getParent(),
1156                                               AddrSpace))) {
1157     if (Assume) {
1158       // We can avoid this case by adding a run-time check.
1159       LLVM_DEBUG(dbgs() << "LAA: Non unit strided pointer which is not either "
1160                         << "inbounds or in address space 0 may wrap:\n"
1161                         << "LAA:   Pointer: " << *Ptr << "\n"
1162                         << "LAA:   SCEV: " << *AR << "\n"
1163                         << "LAA:   Added an overflow assumption\n");
1164       PSE.setNoOverflow(Ptr, SCEVWrapPredicate::IncrementNUSW);
1165     } else
1166       return 0;
1167   }
1168 
1169   return Stride;
1170 }
1171 
1172 Optional<int> llvm::getPointersDiff(Type *ElemTyA, Value *PtrA, Type *ElemTyB,
1173                                     Value *PtrB, const DataLayout &DL,
1174                                     ScalarEvolution &SE, bool StrictCheck,
1175                                     bool CheckType) {
1176   assert(PtrA && PtrB && "Expected non-nullptr pointers.");
1177   assert(cast<PointerType>(PtrA->getType())
1178              ->isOpaqueOrPointeeTypeMatches(ElemTyA) && "Wrong PtrA type");
1179   assert(cast<PointerType>(PtrB->getType())
1180              ->isOpaqueOrPointeeTypeMatches(ElemTyB) && "Wrong PtrB type");
1181 
1182   // Make sure that A and B are different pointers.
1183   if (PtrA == PtrB)
1184     return 0;
1185 
1186   // Make sure that the element types are the same if required.
1187   if (CheckType && ElemTyA != ElemTyB)
1188     return None;
1189 
1190   unsigned ASA = PtrA->getType()->getPointerAddressSpace();
1191   unsigned ASB = PtrB->getType()->getPointerAddressSpace();
1192 
1193   // Check that the address spaces match.
1194   if (ASA != ASB)
1195     return None;
1196   unsigned IdxWidth = DL.getIndexSizeInBits(ASA);
1197 
1198   APInt OffsetA(IdxWidth, 0), OffsetB(IdxWidth, 0);
1199   Value *PtrA1 = PtrA->stripAndAccumulateInBoundsConstantOffsets(DL, OffsetA);
1200   Value *PtrB1 = PtrB->stripAndAccumulateInBoundsConstantOffsets(DL, OffsetB);
1201 
1202   int Val;
1203   if (PtrA1 == PtrB1) {
1204     // Retrieve the address space again as pointer stripping now tracks through
1205     // `addrspacecast`.
1206     ASA = cast<PointerType>(PtrA1->getType())->getAddressSpace();
1207     ASB = cast<PointerType>(PtrB1->getType())->getAddressSpace();
1208     // Check that the address spaces match and that the pointers are valid.
1209     if (ASA != ASB)
1210       return None;
1211 
1212     IdxWidth = DL.getIndexSizeInBits(ASA);
1213     OffsetA = OffsetA.sextOrTrunc(IdxWidth);
1214     OffsetB = OffsetB.sextOrTrunc(IdxWidth);
1215 
1216     OffsetB -= OffsetA;
1217     Val = OffsetB.getSExtValue();
1218   } else {
1219     // Otherwise compute the distance with SCEV between the base pointers.
1220     const SCEV *PtrSCEVA = SE.getSCEV(PtrA);
1221     const SCEV *PtrSCEVB = SE.getSCEV(PtrB);
1222     const auto *Diff =
1223         dyn_cast<SCEVConstant>(SE.getMinusSCEV(PtrSCEVB, PtrSCEVA));
1224     if (!Diff)
1225       return None;
1226     Val = Diff->getAPInt().getSExtValue();
1227   }
1228   int Size = DL.getTypeStoreSize(ElemTyA);
1229   int Dist = Val / Size;
1230 
1231   // Ensure that the calculated distance matches the type-based one after all
1232   // the bitcasts removal in the provided pointers.
1233   if (!StrictCheck || Dist * Size == Val)
1234     return Dist;
1235   return None;
1236 }
1237 
1238 bool llvm::sortPtrAccesses(ArrayRef<Value *> VL, Type *ElemTy,
1239                            const DataLayout &DL, ScalarEvolution &SE,
1240                            SmallVectorImpl<unsigned> &SortedIndices) {
1241   assert(llvm::all_of(
1242              VL, [](const Value *V) { return V->getType()->isPointerTy(); }) &&
1243          "Expected list of pointer operands.");
1244   // Walk over the pointers, and map each of them to an offset relative to
1245   // first pointer in the array.
1246   Value *Ptr0 = VL[0];
1247 
1248   using DistOrdPair = std::pair<int64_t, int>;
1249   auto Compare = [](const DistOrdPair &L, const DistOrdPair &R) {
1250     return L.first < R.first;
1251   };
1252   std::set<DistOrdPair, decltype(Compare)> Offsets(Compare);
1253   Offsets.emplace(0, 0);
1254   int Cnt = 1;
1255   bool IsConsecutive = true;
1256   for (auto *Ptr : VL.drop_front()) {
1257     Optional<int> Diff = getPointersDiff(ElemTy, Ptr0, ElemTy, Ptr, DL, SE,
1258                                          /*StrictCheck=*/true);
1259     if (!Diff)
1260       return false;
1261 
1262     // Check if the pointer with the same offset is found.
1263     int64_t Offset = *Diff;
1264     auto Res = Offsets.emplace(Offset, Cnt);
1265     if (!Res.second)
1266       return false;
1267     // Consecutive order if the inserted element is the last one.
1268     IsConsecutive = IsConsecutive && std::next(Res.first) == Offsets.end();
1269     ++Cnt;
1270   }
1271   SortedIndices.clear();
1272   if (!IsConsecutive) {
1273     // Fill SortedIndices array only if it is non-consecutive.
1274     SortedIndices.resize(VL.size());
1275     Cnt = 0;
1276     for (const std::pair<int64_t, int> &Pair : Offsets) {
1277       SortedIndices[Cnt] = Pair.second;
1278       ++Cnt;
1279     }
1280   }
1281   return true;
1282 }
1283 
1284 /// Returns true if the memory operations \p A and \p B are consecutive.
1285 bool llvm::isConsecutiveAccess(Value *A, Value *B, const DataLayout &DL,
1286                                ScalarEvolution &SE, bool CheckType) {
1287   Value *PtrA = getLoadStorePointerOperand(A);
1288   Value *PtrB = getLoadStorePointerOperand(B);
1289   if (!PtrA || !PtrB)
1290     return false;
1291   Type *ElemTyA = getLoadStoreType(A);
1292   Type *ElemTyB = getLoadStoreType(B);
1293   Optional<int> Diff = getPointersDiff(ElemTyA, PtrA, ElemTyB, PtrB, DL, SE,
1294                                        /*StrictCheck=*/true, CheckType);
1295   return Diff && *Diff == 1;
1296 }
1297 
1298 void MemoryDepChecker::addAccess(StoreInst *SI) {
1299   visitPointers(SI->getPointerOperand(), *InnermostLoop,
1300                 [this, SI](Value *Ptr) {
1301                   Accesses[MemAccessInfo(Ptr, true)].push_back(AccessIdx);
1302                   InstMap.push_back(SI);
1303                   ++AccessIdx;
1304                 });
1305 }
1306 
1307 void MemoryDepChecker::addAccess(LoadInst *LI) {
1308   visitPointers(LI->getPointerOperand(), *InnermostLoop,
1309                 [this, LI](Value *Ptr) {
1310                   Accesses[MemAccessInfo(Ptr, false)].push_back(AccessIdx);
1311                   InstMap.push_back(LI);
1312                   ++AccessIdx;
1313                 });
1314 }
1315 
1316 MemoryDepChecker::VectorizationSafetyStatus
1317 MemoryDepChecker::Dependence::isSafeForVectorization(DepType Type) {
1318   switch (Type) {
1319   case NoDep:
1320   case Forward:
1321   case BackwardVectorizable:
1322     return VectorizationSafetyStatus::Safe;
1323 
1324   case Unknown:
1325     return VectorizationSafetyStatus::PossiblySafeWithRtChecks;
1326   case ForwardButPreventsForwarding:
1327   case Backward:
1328   case BackwardVectorizableButPreventsForwarding:
1329     return VectorizationSafetyStatus::Unsafe;
1330   }
1331   llvm_unreachable("unexpected DepType!");
1332 }
1333 
1334 bool MemoryDepChecker::Dependence::isBackward() const {
1335   switch (Type) {
1336   case NoDep:
1337   case Forward:
1338   case ForwardButPreventsForwarding:
1339   case Unknown:
1340     return false;
1341 
1342   case BackwardVectorizable:
1343   case Backward:
1344   case BackwardVectorizableButPreventsForwarding:
1345     return true;
1346   }
1347   llvm_unreachable("unexpected DepType!");
1348 }
1349 
1350 bool MemoryDepChecker::Dependence::isPossiblyBackward() const {
1351   return isBackward() || Type == Unknown;
1352 }
1353 
1354 bool MemoryDepChecker::Dependence::isForward() const {
1355   switch (Type) {
1356   case Forward:
1357   case ForwardButPreventsForwarding:
1358     return true;
1359 
1360   case NoDep:
1361   case Unknown:
1362   case BackwardVectorizable:
1363   case Backward:
1364   case BackwardVectorizableButPreventsForwarding:
1365     return false;
1366   }
1367   llvm_unreachable("unexpected DepType!");
1368 }
1369 
1370 bool MemoryDepChecker::couldPreventStoreLoadForward(uint64_t Distance,
1371                                                     uint64_t TypeByteSize) {
1372   // If loads occur at a distance that is not a multiple of a feasible vector
1373   // factor store-load forwarding does not take place.
1374   // Positive dependences might cause troubles because vectorizing them might
1375   // prevent store-load forwarding making vectorized code run a lot slower.
1376   //   a[i] = a[i-3] ^ a[i-8];
1377   //   The stores to a[i:i+1] don't align with the stores to a[i-3:i-2] and
1378   //   hence on your typical architecture store-load forwarding does not take
1379   //   place. Vectorizing in such cases does not make sense.
1380   // Store-load forwarding distance.
1381 
1382   // After this many iterations store-to-load forwarding conflicts should not
1383   // cause any slowdowns.
1384   const uint64_t NumItersForStoreLoadThroughMemory = 8 * TypeByteSize;
1385   // Maximum vector factor.
1386   uint64_t MaxVFWithoutSLForwardIssues = std::min(
1387       VectorizerParams::MaxVectorWidth * TypeByteSize, MaxSafeDepDistBytes);
1388 
1389   // Compute the smallest VF at which the store and load would be misaligned.
1390   for (uint64_t VF = 2 * TypeByteSize; VF <= MaxVFWithoutSLForwardIssues;
1391        VF *= 2) {
1392     // If the number of vector iteration between the store and the load are
1393     // small we could incur conflicts.
1394     if (Distance % VF && Distance / VF < NumItersForStoreLoadThroughMemory) {
1395       MaxVFWithoutSLForwardIssues = (VF >> 1);
1396       break;
1397     }
1398   }
1399 
1400   if (MaxVFWithoutSLForwardIssues < 2 * TypeByteSize) {
1401     LLVM_DEBUG(
1402         dbgs() << "LAA: Distance " << Distance
1403                << " that could cause a store-load forwarding conflict\n");
1404     return true;
1405   }
1406 
1407   if (MaxVFWithoutSLForwardIssues < MaxSafeDepDistBytes &&
1408       MaxVFWithoutSLForwardIssues !=
1409           VectorizerParams::MaxVectorWidth * TypeByteSize)
1410     MaxSafeDepDistBytes = MaxVFWithoutSLForwardIssues;
1411   return false;
1412 }
1413 
1414 void MemoryDepChecker::mergeInStatus(VectorizationSafetyStatus S) {
1415   if (Status < S)
1416     Status = S;
1417 }
1418 
1419 /// Given a non-constant (unknown) dependence-distance \p Dist between two
1420 /// memory accesses, that have the same stride whose absolute value is given
1421 /// in \p Stride, and that have the same type size \p TypeByteSize,
1422 /// in a loop whose takenCount is \p BackedgeTakenCount, check if it is
1423 /// possible to prove statically that the dependence distance is larger
1424 /// than the range that the accesses will travel through the execution of
1425 /// the loop. If so, return true; false otherwise. This is useful for
1426 /// example in loops such as the following (PR31098):
1427 ///     for (i = 0; i < D; ++i) {
1428 ///                = out[i];
1429 ///       out[i+D] =
1430 ///     }
1431 static bool isSafeDependenceDistance(const DataLayout &DL, ScalarEvolution &SE,
1432                                      const SCEV &BackedgeTakenCount,
1433                                      const SCEV &Dist, uint64_t Stride,
1434                                      uint64_t TypeByteSize) {
1435 
1436   // If we can prove that
1437   //      (**) |Dist| > BackedgeTakenCount * Step
1438   // where Step is the absolute stride of the memory accesses in bytes,
1439   // then there is no dependence.
1440   //
1441   // Rationale:
1442   // We basically want to check if the absolute distance (|Dist/Step|)
1443   // is >= the loop iteration count (or > BackedgeTakenCount).
1444   // This is equivalent to the Strong SIV Test (Practical Dependence Testing,
1445   // Section 4.2.1); Note, that for vectorization it is sufficient to prove
1446   // that the dependence distance is >= VF; This is checked elsewhere.
1447   // But in some cases we can prune unknown dependence distances early, and
1448   // even before selecting the VF, and without a runtime test, by comparing
1449   // the distance against the loop iteration count. Since the vectorized code
1450   // will be executed only if LoopCount >= VF, proving distance >= LoopCount
1451   // also guarantees that distance >= VF.
1452   //
1453   const uint64_t ByteStride = Stride * TypeByteSize;
1454   const SCEV *Step = SE.getConstant(BackedgeTakenCount.getType(), ByteStride);
1455   const SCEV *Product = SE.getMulExpr(&BackedgeTakenCount, Step);
1456 
1457   const SCEV *CastedDist = &Dist;
1458   const SCEV *CastedProduct = Product;
1459   uint64_t DistTypeSize = DL.getTypeAllocSize(Dist.getType());
1460   uint64_t ProductTypeSize = DL.getTypeAllocSize(Product->getType());
1461 
1462   // The dependence distance can be positive/negative, so we sign extend Dist;
1463   // The multiplication of the absolute stride in bytes and the
1464   // backedgeTakenCount is non-negative, so we zero extend Product.
1465   if (DistTypeSize > ProductTypeSize)
1466     CastedProduct = SE.getZeroExtendExpr(Product, Dist.getType());
1467   else
1468     CastedDist = SE.getNoopOrSignExtend(&Dist, Product->getType());
1469 
1470   // Is  Dist - (BackedgeTakenCount * Step) > 0 ?
1471   // (If so, then we have proven (**) because |Dist| >= Dist)
1472   const SCEV *Minus = SE.getMinusSCEV(CastedDist, CastedProduct);
1473   if (SE.isKnownPositive(Minus))
1474     return true;
1475 
1476   // Second try: Is  -Dist - (BackedgeTakenCount * Step) > 0 ?
1477   // (If so, then we have proven (**) because |Dist| >= -1*Dist)
1478   const SCEV *NegDist = SE.getNegativeSCEV(CastedDist);
1479   Minus = SE.getMinusSCEV(NegDist, CastedProduct);
1480   if (SE.isKnownPositive(Minus))
1481     return true;
1482 
1483   return false;
1484 }
1485 
1486 /// Check the dependence for two accesses with the same stride \p Stride.
1487 /// \p Distance is the positive distance and \p TypeByteSize is type size in
1488 /// bytes.
1489 ///
1490 /// \returns true if they are independent.
1491 static bool areStridedAccessesIndependent(uint64_t Distance, uint64_t Stride,
1492                                           uint64_t TypeByteSize) {
1493   assert(Stride > 1 && "The stride must be greater than 1");
1494   assert(TypeByteSize > 0 && "The type size in byte must be non-zero");
1495   assert(Distance > 0 && "The distance must be non-zero");
1496 
1497   // Skip if the distance is not multiple of type byte size.
1498   if (Distance % TypeByteSize)
1499     return false;
1500 
1501   uint64_t ScaledDist = Distance / TypeByteSize;
1502 
1503   // No dependence if the scaled distance is not multiple of the stride.
1504   // E.g.
1505   //      for (i = 0; i < 1024 ; i += 4)
1506   //        A[i+2] = A[i] + 1;
1507   //
1508   // Two accesses in memory (scaled distance is 2, stride is 4):
1509   //     | A[0] |      |      |      | A[4] |      |      |      |
1510   //     |      |      | A[2] |      |      |      | A[6] |      |
1511   //
1512   // E.g.
1513   //      for (i = 0; i < 1024 ; i += 3)
1514   //        A[i+4] = A[i] + 1;
1515   //
1516   // Two accesses in memory (scaled distance is 4, stride is 3):
1517   //     | A[0] |      |      | A[3] |      |      | A[6] |      |      |
1518   //     |      |      |      |      | A[4] |      |      | A[7] |      |
1519   return ScaledDist % Stride;
1520 }
1521 
1522 MemoryDepChecker::Dependence::DepType
1523 MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
1524                               const MemAccessInfo &B, unsigned BIdx,
1525                               const ValueToValueMap &Strides) {
1526   assert (AIdx < BIdx && "Must pass arguments in program order");
1527 
1528   Value *APtr = A.getPointer();
1529   Value *BPtr = B.getPointer();
1530   bool AIsWrite = A.getInt();
1531   bool BIsWrite = B.getInt();
1532   Type *ATy = getLoadStoreType(InstMap[AIdx]);
1533   Type *BTy = getLoadStoreType(InstMap[BIdx]);
1534 
1535   // Two reads are independent.
1536   if (!AIsWrite && !BIsWrite)
1537     return Dependence::NoDep;
1538 
1539   // We cannot check pointers in different address spaces.
1540   if (APtr->getType()->getPointerAddressSpace() !=
1541       BPtr->getType()->getPointerAddressSpace())
1542     return Dependence::Unknown;
1543 
1544   int64_t StrideAPtr =
1545       getPtrStride(PSE, ATy, APtr, InnermostLoop, Strides, true);
1546   int64_t StrideBPtr =
1547       getPtrStride(PSE, BTy, BPtr, InnermostLoop, Strides, true);
1548 
1549   const SCEV *Src = PSE.getSCEV(APtr);
1550   const SCEV *Sink = PSE.getSCEV(BPtr);
1551 
1552   // If the induction step is negative we have to invert source and sink of the
1553   // dependence.
1554   if (StrideAPtr < 0) {
1555     std::swap(APtr, BPtr);
1556     std::swap(ATy, BTy);
1557     std::swap(Src, Sink);
1558     std::swap(AIsWrite, BIsWrite);
1559     std::swap(AIdx, BIdx);
1560     std::swap(StrideAPtr, StrideBPtr);
1561   }
1562 
1563   const SCEV *Dist = PSE.getSE()->getMinusSCEV(Sink, Src);
1564 
1565   LLVM_DEBUG(dbgs() << "LAA: Src Scev: " << *Src << "Sink Scev: " << *Sink
1566                     << "(Induction step: " << StrideAPtr << ")\n");
1567   LLVM_DEBUG(dbgs() << "LAA: Distance for " << *InstMap[AIdx] << " to "
1568                     << *InstMap[BIdx] << ": " << *Dist << "\n");
1569 
1570   // Need accesses with constant stride. We don't want to vectorize
1571   // "A[B[i]] += ..." and similar code or pointer arithmetic that could wrap in
1572   // the address space.
1573   if (!StrideAPtr || !StrideBPtr || StrideAPtr != StrideBPtr){
1574     LLVM_DEBUG(dbgs() << "Pointer access with non-constant stride\n");
1575     return Dependence::Unknown;
1576   }
1577 
1578   auto &DL = InnermostLoop->getHeader()->getModule()->getDataLayout();
1579   uint64_t TypeByteSize = DL.getTypeAllocSize(ATy);
1580   bool HasSameSize =
1581       DL.getTypeStoreSizeInBits(ATy) == DL.getTypeStoreSizeInBits(BTy);
1582   uint64_t Stride = std::abs(StrideAPtr);
1583   const SCEVConstant *C = dyn_cast<SCEVConstant>(Dist);
1584   if (!C) {
1585     if (!isa<SCEVCouldNotCompute>(Dist) && HasSameSize &&
1586         isSafeDependenceDistance(DL, *(PSE.getSE()),
1587                                  *(PSE.getBackedgeTakenCount()), *Dist, Stride,
1588                                  TypeByteSize))
1589       return Dependence::NoDep;
1590 
1591     LLVM_DEBUG(dbgs() << "LAA: Dependence because of non-constant distance\n");
1592     FoundNonConstantDistanceDependence = true;
1593     return Dependence::Unknown;
1594   }
1595 
1596   const APInt &Val = C->getAPInt();
1597   int64_t Distance = Val.getSExtValue();
1598 
1599   // Attempt to prove strided accesses independent.
1600   if (std::abs(Distance) > 0 && Stride > 1 && HasSameSize &&
1601       areStridedAccessesIndependent(std::abs(Distance), Stride, TypeByteSize)) {
1602     LLVM_DEBUG(dbgs() << "LAA: Strided accesses are independent\n");
1603     return Dependence::NoDep;
1604   }
1605 
1606   // Negative distances are not plausible dependencies.
1607   if (Val.isNegative()) {
1608     bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
1609     if (IsTrueDataDependence && EnableForwardingConflictDetection &&
1610         (couldPreventStoreLoadForward(Val.abs().getZExtValue(), TypeByteSize) ||
1611          !HasSameSize)) {
1612       LLVM_DEBUG(dbgs() << "LAA: Forward but may prevent st->ld forwarding\n");
1613       return Dependence::ForwardButPreventsForwarding;
1614     }
1615 
1616     LLVM_DEBUG(dbgs() << "LAA: Dependence is negative\n");
1617     return Dependence::Forward;
1618   }
1619 
1620   // Write to the same location with the same size.
1621   if (Val == 0) {
1622     if (HasSameSize)
1623       return Dependence::Forward;
1624     LLVM_DEBUG(
1625         dbgs() << "LAA: Zero dependence difference but different type sizes\n");
1626     return Dependence::Unknown;
1627   }
1628 
1629   assert(Val.isStrictlyPositive() && "Expect a positive value");
1630 
1631   if (!HasSameSize) {
1632     LLVM_DEBUG(dbgs() << "LAA: ReadWrite-Write positive dependency with "
1633                          "different type sizes\n");
1634     return Dependence::Unknown;
1635   }
1636 
1637   // Bail out early if passed-in parameters make vectorization not feasible.
1638   unsigned ForcedFactor = (VectorizerParams::VectorizationFactor ?
1639                            VectorizerParams::VectorizationFactor : 1);
1640   unsigned ForcedUnroll = (VectorizerParams::VectorizationInterleave ?
1641                            VectorizerParams::VectorizationInterleave : 1);
1642   // The minimum number of iterations for a vectorized/unrolled version.
1643   unsigned MinNumIter = std::max(ForcedFactor * ForcedUnroll, 2U);
1644 
1645   // It's not vectorizable if the distance is smaller than the minimum distance
1646   // needed for a vectroized/unrolled version. Vectorizing one iteration in
1647   // front needs TypeByteSize * Stride. Vectorizing the last iteration needs
1648   // TypeByteSize (No need to plus the last gap distance).
1649   //
1650   // E.g. Assume one char is 1 byte in memory and one int is 4 bytes.
1651   //      foo(int *A) {
1652   //        int *B = (int *)((char *)A + 14);
1653   //        for (i = 0 ; i < 1024 ; i += 2)
1654   //          B[i] = A[i] + 1;
1655   //      }
1656   //
1657   // Two accesses in memory (stride is 2):
1658   //     | A[0] |      | A[2] |      | A[4] |      | A[6] |      |
1659   //                              | B[0] |      | B[2] |      | B[4] |
1660   //
1661   // Distance needs for vectorizing iterations except the last iteration:
1662   // 4 * 2 * (MinNumIter - 1). Distance needs for the last iteration: 4.
1663   // So the minimum distance needed is: 4 * 2 * (MinNumIter - 1) + 4.
1664   //
1665   // If MinNumIter is 2, it is vectorizable as the minimum distance needed is
1666   // 12, which is less than distance.
1667   //
1668   // If MinNumIter is 4 (Say if a user forces the vectorization factor to be 4),
1669   // the minimum distance needed is 28, which is greater than distance. It is
1670   // not safe to do vectorization.
1671   uint64_t MinDistanceNeeded =
1672       TypeByteSize * Stride * (MinNumIter - 1) + TypeByteSize;
1673   if (MinDistanceNeeded > static_cast<uint64_t>(Distance)) {
1674     LLVM_DEBUG(dbgs() << "LAA: Failure because of positive distance "
1675                       << Distance << '\n');
1676     return Dependence::Backward;
1677   }
1678 
1679   // Unsafe if the minimum distance needed is greater than max safe distance.
1680   if (MinDistanceNeeded > MaxSafeDepDistBytes) {
1681     LLVM_DEBUG(dbgs() << "LAA: Failure because it needs at least "
1682                       << MinDistanceNeeded << " size in bytes");
1683     return Dependence::Backward;
1684   }
1685 
1686   // Positive distance bigger than max vectorization factor.
1687   // FIXME: Should use max factor instead of max distance in bytes, which could
1688   // not handle different types.
1689   // E.g. Assume one char is 1 byte in memory and one int is 4 bytes.
1690   //      void foo (int *A, char *B) {
1691   //        for (unsigned i = 0; i < 1024; i++) {
1692   //          A[i+2] = A[i] + 1;
1693   //          B[i+2] = B[i] + 1;
1694   //        }
1695   //      }
1696   //
1697   // This case is currently unsafe according to the max safe distance. If we
1698   // analyze the two accesses on array B, the max safe dependence distance
1699   // is 2. Then we analyze the accesses on array A, the minimum distance needed
1700   // is 8, which is less than 2 and forbidden vectorization, But actually
1701   // both A and B could be vectorized by 2 iterations.
1702   MaxSafeDepDistBytes =
1703       std::min(static_cast<uint64_t>(Distance), MaxSafeDepDistBytes);
1704 
1705   bool IsTrueDataDependence = (!AIsWrite && BIsWrite);
1706   if (IsTrueDataDependence && EnableForwardingConflictDetection &&
1707       couldPreventStoreLoadForward(Distance, TypeByteSize))
1708     return Dependence::BackwardVectorizableButPreventsForwarding;
1709 
1710   uint64_t MaxVF = MaxSafeDepDistBytes / (TypeByteSize * Stride);
1711   LLVM_DEBUG(dbgs() << "LAA: Positive distance " << Val.getSExtValue()
1712                     << " with max VF = " << MaxVF << '\n');
1713   uint64_t MaxVFInBits = MaxVF * TypeByteSize * 8;
1714   MaxSafeVectorWidthInBits = std::min(MaxSafeVectorWidthInBits, MaxVFInBits);
1715   return Dependence::BackwardVectorizable;
1716 }
1717 
1718 bool MemoryDepChecker::areDepsSafe(DepCandidates &AccessSets,
1719                                    MemAccessInfoList &CheckDeps,
1720                                    const ValueToValueMap &Strides) {
1721 
1722   MaxSafeDepDistBytes = -1;
1723   SmallPtrSet<MemAccessInfo, 8> Visited;
1724   for (MemAccessInfo CurAccess : CheckDeps) {
1725     if (Visited.count(CurAccess))
1726       continue;
1727 
1728     // Get the relevant memory access set.
1729     EquivalenceClasses<MemAccessInfo>::iterator I =
1730       AccessSets.findValue(AccessSets.getLeaderValue(CurAccess));
1731 
1732     // Check accesses within this set.
1733     EquivalenceClasses<MemAccessInfo>::member_iterator AI =
1734         AccessSets.member_begin(I);
1735     EquivalenceClasses<MemAccessInfo>::member_iterator AE =
1736         AccessSets.member_end();
1737 
1738     // Check every access pair.
1739     while (AI != AE) {
1740       Visited.insert(*AI);
1741       bool AIIsWrite = AI->getInt();
1742       // Check loads only against next equivalent class, but stores also against
1743       // other stores in the same equivalence class - to the same address.
1744       EquivalenceClasses<MemAccessInfo>::member_iterator OI =
1745           (AIIsWrite ? AI : std::next(AI));
1746       while (OI != AE) {
1747         // Check every accessing instruction pair in program order.
1748         for (std::vector<unsigned>::iterator I1 = Accesses[*AI].begin(),
1749              I1E = Accesses[*AI].end(); I1 != I1E; ++I1)
1750           // Scan all accesses of another equivalence class, but only the next
1751           // accesses of the same equivalent class.
1752           for (std::vector<unsigned>::iterator
1753                    I2 = (OI == AI ? std::next(I1) : Accesses[*OI].begin()),
1754                    I2E = (OI == AI ? I1E : Accesses[*OI].end());
1755                I2 != I2E; ++I2) {
1756             auto A = std::make_pair(&*AI, *I1);
1757             auto B = std::make_pair(&*OI, *I2);
1758 
1759             assert(*I1 != *I2);
1760             if (*I1 > *I2)
1761               std::swap(A, B);
1762 
1763             Dependence::DepType Type =
1764                 isDependent(*A.first, A.second, *B.first, B.second, Strides);
1765             mergeInStatus(Dependence::isSafeForVectorization(Type));
1766 
1767             // Gather dependences unless we accumulated MaxDependences
1768             // dependences.  In that case return as soon as we find the first
1769             // unsafe dependence.  This puts a limit on this quadratic
1770             // algorithm.
1771             if (RecordDependences) {
1772               if (Type != Dependence::NoDep)
1773                 Dependences.push_back(Dependence(A.second, B.second, Type));
1774 
1775               if (Dependences.size() >= MaxDependences) {
1776                 RecordDependences = false;
1777                 Dependences.clear();
1778                 LLVM_DEBUG(dbgs()
1779                            << "Too many dependences, stopped recording\n");
1780               }
1781             }
1782             if (!RecordDependences && !isSafeForVectorization())
1783               return false;
1784           }
1785         ++OI;
1786       }
1787       AI++;
1788     }
1789   }
1790 
1791   LLVM_DEBUG(dbgs() << "Total Dependences: " << Dependences.size() << "\n");
1792   return isSafeForVectorization();
1793 }
1794 
1795 SmallVector<Instruction *, 4>
1796 MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool isWrite) const {
1797   MemAccessInfo Access(Ptr, isWrite);
1798   auto &IndexVector = Accesses.find(Access)->second;
1799 
1800   SmallVector<Instruction *, 4> Insts;
1801   transform(IndexVector,
1802                  std::back_inserter(Insts),
1803                  [&](unsigned Idx) { return this->InstMap[Idx]; });
1804   return Insts;
1805 }
1806 
1807 const char *MemoryDepChecker::Dependence::DepName[] = {
1808     "NoDep", "Unknown", "Forward", "ForwardButPreventsForwarding", "Backward",
1809     "BackwardVectorizable", "BackwardVectorizableButPreventsForwarding"};
1810 
1811 void MemoryDepChecker::Dependence::print(
1812     raw_ostream &OS, unsigned Depth,
1813     const SmallVectorImpl<Instruction *> &Instrs) const {
1814   OS.indent(Depth) << DepName[Type] << ":\n";
1815   OS.indent(Depth + 2) << *Instrs[Source] << " -> \n";
1816   OS.indent(Depth + 2) << *Instrs[Destination] << "\n";
1817 }
1818 
1819 bool LoopAccessInfo::canAnalyzeLoop() {
1820   // We need to have a loop header.
1821   LLVM_DEBUG(dbgs() << "LAA: Found a loop in "
1822                     << TheLoop->getHeader()->getParent()->getName() << ": "
1823                     << TheLoop->getHeader()->getName() << '\n');
1824 
1825   // We can only analyze innermost loops.
1826   if (!TheLoop->isInnermost()) {
1827     LLVM_DEBUG(dbgs() << "LAA: loop is not the innermost loop\n");
1828     recordAnalysis("NotInnerMostLoop") << "loop is not the innermost loop";
1829     return false;
1830   }
1831 
1832   // We must have a single backedge.
1833   if (TheLoop->getNumBackEdges() != 1) {
1834     LLVM_DEBUG(
1835         dbgs() << "LAA: loop control flow is not understood by analyzer\n");
1836     recordAnalysis("CFGNotUnderstood")
1837         << "loop control flow is not understood by analyzer";
1838     return false;
1839   }
1840 
1841   // ScalarEvolution needs to be able to find the exit count.
1842   const SCEV *ExitCount = PSE->getBackedgeTakenCount();
1843   if (isa<SCEVCouldNotCompute>(ExitCount)) {
1844     recordAnalysis("CantComputeNumberOfIterations")
1845         << "could not determine number of loop iterations";
1846     LLVM_DEBUG(dbgs() << "LAA: SCEV could not compute the loop exit count.\n");
1847     return false;
1848   }
1849 
1850   return true;
1851 }
1852 
1853 void LoopAccessInfo::analyzeLoop(AAResults *AA, LoopInfo *LI,
1854                                  const TargetLibraryInfo *TLI,
1855                                  DominatorTree *DT) {
1856   // Holds the Load and Store instructions.
1857   SmallVector<LoadInst *, 16> Loads;
1858   SmallVector<StoreInst *, 16> Stores;
1859 
1860   // Holds all the different accesses in the loop.
1861   unsigned NumReads = 0;
1862   unsigned NumReadWrites = 0;
1863 
1864   bool HasComplexMemInst = false;
1865 
1866   // A runtime check is only legal to insert if there are no convergent calls.
1867   HasConvergentOp = false;
1868 
1869   PtrRtChecking->Pointers.clear();
1870   PtrRtChecking->Need = false;
1871 
1872   const bool IsAnnotatedParallel = TheLoop->isAnnotatedParallel();
1873 
1874   const bool EnableMemAccessVersioningOfLoop =
1875       EnableMemAccessVersioning &&
1876       !TheLoop->getHeader()->getParent()->hasOptSize();
1877 
1878   // For each block.
1879   for (BasicBlock *BB : TheLoop->blocks()) {
1880     // Scan the BB and collect legal loads and stores. Also detect any
1881     // convergent instructions.
1882     for (Instruction &I : *BB) {
1883       if (auto *Call = dyn_cast<CallBase>(&I)) {
1884         if (Call->isConvergent())
1885           HasConvergentOp = true;
1886       }
1887 
1888       // With both a non-vectorizable memory instruction and a convergent
1889       // operation, found in this loop, no reason to continue the search.
1890       if (HasComplexMemInst && HasConvergentOp) {
1891         CanVecMem = false;
1892         return;
1893       }
1894 
1895       // Avoid hitting recordAnalysis multiple times.
1896       if (HasComplexMemInst)
1897         continue;
1898 
1899       // If this is a load, save it. If this instruction can read from memory
1900       // but is not a load, then we quit. Notice that we don't handle function
1901       // calls that read or write.
1902       if (I.mayReadFromMemory()) {
1903         // Many math library functions read the rounding mode. We will only
1904         // vectorize a loop if it contains known function calls that don't set
1905         // the flag. Therefore, it is safe to ignore this read from memory.
1906         auto *Call = dyn_cast<CallInst>(&I);
1907         if (Call && getVectorIntrinsicIDForCall(Call, TLI))
1908           continue;
1909 
1910         // If the function has an explicit vectorized counterpart, we can safely
1911         // assume that it can be vectorized.
1912         if (Call && !Call->isNoBuiltin() && Call->getCalledFunction() &&
1913             !VFDatabase::getMappings(*Call).empty())
1914           continue;
1915 
1916         auto *Ld = dyn_cast<LoadInst>(&I);
1917         if (!Ld) {
1918           recordAnalysis("CantVectorizeInstruction", Ld)
1919             << "instruction cannot be vectorized";
1920           HasComplexMemInst = true;
1921           continue;
1922         }
1923         if (!Ld->isSimple() && !IsAnnotatedParallel) {
1924           recordAnalysis("NonSimpleLoad", Ld)
1925               << "read with atomic ordering or volatile read";
1926           LLVM_DEBUG(dbgs() << "LAA: Found a non-simple load.\n");
1927           HasComplexMemInst = true;
1928           continue;
1929         }
1930         NumLoads++;
1931         Loads.push_back(Ld);
1932         DepChecker->addAccess(Ld);
1933         if (EnableMemAccessVersioningOfLoop)
1934           collectStridedAccess(Ld);
1935         continue;
1936       }
1937 
1938       // Save 'store' instructions. Abort if other instructions write to memory.
1939       if (I.mayWriteToMemory()) {
1940         auto *St = dyn_cast<StoreInst>(&I);
1941         if (!St) {
1942           recordAnalysis("CantVectorizeInstruction", St)
1943               << "instruction cannot be vectorized";
1944           HasComplexMemInst = true;
1945           continue;
1946         }
1947         if (!St->isSimple() && !IsAnnotatedParallel) {
1948           recordAnalysis("NonSimpleStore", St)
1949               << "write with atomic ordering or volatile write";
1950           LLVM_DEBUG(dbgs() << "LAA: Found a non-simple store.\n");
1951           HasComplexMemInst = true;
1952           continue;
1953         }
1954         NumStores++;
1955         Stores.push_back(St);
1956         DepChecker->addAccess(St);
1957         if (EnableMemAccessVersioningOfLoop)
1958           collectStridedAccess(St);
1959       }
1960     } // Next instr.
1961   } // Next block.
1962 
1963   if (HasComplexMemInst) {
1964     CanVecMem = false;
1965     return;
1966   }
1967 
1968   // Now we have two lists that hold the loads and the stores.
1969   // Next, we find the pointers that they use.
1970 
1971   // Check if we see any stores. If there are no stores, then we don't
1972   // care if the pointers are *restrict*.
1973   if (!Stores.size()) {
1974     LLVM_DEBUG(dbgs() << "LAA: Found a read-only loop!\n");
1975     CanVecMem = true;
1976     return;
1977   }
1978 
1979   MemoryDepChecker::DepCandidates DependentAccesses;
1980   AccessAnalysis Accesses(TheLoop, AA, LI, DependentAccesses, *PSE);
1981 
1982   // Holds the analyzed pointers. We don't want to call getUnderlyingObjects
1983   // multiple times on the same object. If the ptr is accessed twice, once
1984   // for read and once for write, it will only appear once (on the write
1985   // list). This is okay, since we are going to check for conflicts between
1986   // writes and between reads and writes, but not between reads and reads.
1987   SmallSet<std::pair<Value *, Type *>, 16> Seen;
1988 
1989   // Record uniform store addresses to identify if we have multiple stores
1990   // to the same address.
1991   SmallPtrSet<Value *, 16> UniformStores;
1992 
1993   for (StoreInst *ST : Stores) {
1994     Value *Ptr = ST->getPointerOperand();
1995 
1996     if (isUniform(Ptr))
1997       HasDependenceInvolvingLoopInvariantAddress |=
1998           !UniformStores.insert(Ptr).second;
1999 
2000     // If we did *not* see this pointer before, insert it to  the read-write
2001     // list. At this phase it is only a 'write' list.
2002     Type *AccessTy = getLoadStoreType(ST);
2003     if (Seen.insert({Ptr, AccessTy}).second) {
2004       ++NumReadWrites;
2005 
2006       MemoryLocation Loc = MemoryLocation::get(ST);
2007       // The TBAA metadata could have a control dependency on the predication
2008       // condition, so we cannot rely on it when determining whether or not we
2009       // need runtime pointer checks.
2010       if (blockNeedsPredication(ST->getParent(), TheLoop, DT))
2011         Loc.AATags.TBAA = nullptr;
2012 
2013       visitPointers(const_cast<Value *>(Loc.Ptr), *TheLoop,
2014                     [&Accesses, AccessTy, Loc](Value *Ptr) {
2015                       MemoryLocation NewLoc = Loc.getWithNewPtr(Ptr);
2016                       Accesses.addStore(NewLoc, AccessTy);
2017                     });
2018     }
2019   }
2020 
2021   if (IsAnnotatedParallel) {
2022     LLVM_DEBUG(
2023         dbgs() << "LAA: A loop annotated parallel, ignore memory dependency "
2024                << "checks.\n");
2025     CanVecMem = true;
2026     return;
2027   }
2028 
2029   for (LoadInst *LD : Loads) {
2030     Value *Ptr = LD->getPointerOperand();
2031     // If we did *not* see this pointer before, insert it to the
2032     // read list. If we *did* see it before, then it is already in
2033     // the read-write list. This allows us to vectorize expressions
2034     // such as A[i] += x;  Because the address of A[i] is a read-write
2035     // pointer. This only works if the index of A[i] is consecutive.
2036     // If the address of i is unknown (for example A[B[i]]) then we may
2037     // read a few words, modify, and write a few words, and some of the
2038     // words may be written to the same address.
2039     bool IsReadOnlyPtr = false;
2040     Type *AccessTy = getLoadStoreType(LD);
2041     if (Seen.insert({Ptr, AccessTy}).second ||
2042         !getPtrStride(*PSE, LD->getType(), Ptr, TheLoop, SymbolicStrides)) {
2043       ++NumReads;
2044       IsReadOnlyPtr = true;
2045     }
2046 
2047     // See if there is an unsafe dependency between a load to a uniform address and
2048     // store to the same uniform address.
2049     if (UniformStores.count(Ptr)) {
2050       LLVM_DEBUG(dbgs() << "LAA: Found an unsafe dependency between a uniform "
2051                            "load and uniform store to the same address!\n");
2052       HasDependenceInvolvingLoopInvariantAddress = true;
2053     }
2054 
2055     MemoryLocation Loc = MemoryLocation::get(LD);
2056     // The TBAA metadata could have a control dependency on the predication
2057     // condition, so we cannot rely on it when determining whether or not we
2058     // need runtime pointer checks.
2059     if (blockNeedsPredication(LD->getParent(), TheLoop, DT))
2060       Loc.AATags.TBAA = nullptr;
2061 
2062     visitPointers(const_cast<Value *>(Loc.Ptr), *TheLoop,
2063                   [&Accesses, AccessTy, Loc, IsReadOnlyPtr](Value *Ptr) {
2064                     MemoryLocation NewLoc = Loc.getWithNewPtr(Ptr);
2065                     Accesses.addLoad(NewLoc, AccessTy, IsReadOnlyPtr);
2066                   });
2067   }
2068 
2069   // If we write (or read-write) to a single destination and there are no
2070   // other reads in this loop then is it safe to vectorize.
2071   if (NumReadWrites == 1 && NumReads == 0) {
2072     LLVM_DEBUG(dbgs() << "LAA: Found a write-only loop!\n");
2073     CanVecMem = true;
2074     return;
2075   }
2076 
2077   // Build dependence sets and check whether we need a runtime pointer bounds
2078   // check.
2079   Accesses.buildDependenceSets();
2080 
2081   // Find pointers with computable bounds. We are going to use this information
2082   // to place a runtime bound check.
2083   bool CanDoRTIfNeeded = Accesses.canCheckPtrAtRT(*PtrRtChecking, PSE->getSE(),
2084                                                   TheLoop, SymbolicStrides);
2085   if (!CanDoRTIfNeeded) {
2086     recordAnalysis("CantIdentifyArrayBounds") << "cannot identify array bounds";
2087     LLVM_DEBUG(dbgs() << "LAA: We can't vectorize because we can't find "
2088                       << "the array bounds.\n");
2089     CanVecMem = false;
2090     return;
2091   }
2092 
2093   LLVM_DEBUG(
2094     dbgs() << "LAA: May be able to perform a memory runtime check if needed.\n");
2095 
2096   CanVecMem = true;
2097   if (Accesses.isDependencyCheckNeeded()) {
2098     LLVM_DEBUG(dbgs() << "LAA: Checking memory dependencies\n");
2099     CanVecMem = DepChecker->areDepsSafe(
2100         DependentAccesses, Accesses.getDependenciesToCheck(), SymbolicStrides);
2101     MaxSafeDepDistBytes = DepChecker->getMaxSafeDepDistBytes();
2102 
2103     if (!CanVecMem && DepChecker->shouldRetryWithRuntimeCheck()) {
2104       LLVM_DEBUG(dbgs() << "LAA: Retrying with memory checks\n");
2105 
2106       // Clear the dependency checks. We assume they are not needed.
2107       Accesses.resetDepChecks(*DepChecker);
2108 
2109       PtrRtChecking->reset();
2110       PtrRtChecking->Need = true;
2111 
2112       auto *SE = PSE->getSE();
2113       CanDoRTIfNeeded = Accesses.canCheckPtrAtRT(*PtrRtChecking, SE, TheLoop,
2114                                                  SymbolicStrides, true);
2115 
2116       // Check that we found the bounds for the pointer.
2117       if (!CanDoRTIfNeeded) {
2118         recordAnalysis("CantCheckMemDepsAtRunTime")
2119             << "cannot check memory dependencies at runtime";
2120         LLVM_DEBUG(dbgs() << "LAA: Can't vectorize with memory checks\n");
2121         CanVecMem = false;
2122         return;
2123       }
2124 
2125       CanVecMem = true;
2126     }
2127   }
2128 
2129   if (HasConvergentOp) {
2130     recordAnalysis("CantInsertRuntimeCheckWithConvergent")
2131       << "cannot add control dependency to convergent operation";
2132     LLVM_DEBUG(dbgs() << "LAA: We can't vectorize because a runtime check "
2133                          "would be needed with a convergent operation\n");
2134     CanVecMem = false;
2135     return;
2136   }
2137 
2138   if (CanVecMem)
2139     LLVM_DEBUG(
2140         dbgs() << "LAA: No unsafe dependent memory operations in loop.  We"
2141                << (PtrRtChecking->Need ? "" : " don't")
2142                << " need runtime memory checks.\n");
2143   else
2144     emitUnsafeDependenceRemark();
2145 }
2146 
2147 void LoopAccessInfo::emitUnsafeDependenceRemark() {
2148   auto Deps = getDepChecker().getDependences();
2149   if (!Deps)
2150     return;
2151   auto Found = std::find_if(
2152       Deps->begin(), Deps->end(), [](const MemoryDepChecker::Dependence &D) {
2153         return MemoryDepChecker::Dependence::isSafeForVectorization(D.Type) !=
2154                MemoryDepChecker::VectorizationSafetyStatus::Safe;
2155       });
2156   if (Found == Deps->end())
2157     return;
2158   MemoryDepChecker::Dependence Dep = *Found;
2159 
2160   LLVM_DEBUG(dbgs() << "LAA: unsafe dependent memory operations in loop\n");
2161 
2162   // Emit remark for first unsafe dependence
2163   OptimizationRemarkAnalysis &R =
2164       recordAnalysis("UnsafeDep", Dep.getDestination(*this))
2165       << "unsafe dependent memory operations in loop. Use "
2166          "#pragma loop distribute(enable) to allow loop distribution "
2167          "to attempt to isolate the offending operations into a separate "
2168          "loop";
2169 
2170   switch (Dep.Type) {
2171   case MemoryDepChecker::Dependence::NoDep:
2172   case MemoryDepChecker::Dependence::Forward:
2173   case MemoryDepChecker::Dependence::BackwardVectorizable:
2174     llvm_unreachable("Unexpected dependence");
2175   case MemoryDepChecker::Dependence::Backward:
2176     R << "\nBackward loop carried data dependence.";
2177     break;
2178   case MemoryDepChecker::Dependence::ForwardButPreventsForwarding:
2179     R << "\nForward loop carried data dependence that prevents "
2180          "store-to-load forwarding.";
2181     break;
2182   case MemoryDepChecker::Dependence::BackwardVectorizableButPreventsForwarding:
2183     R << "\nBackward loop carried data dependence that prevents "
2184          "store-to-load forwarding.";
2185     break;
2186   case MemoryDepChecker::Dependence::Unknown:
2187     R << "\nUnknown data dependence.";
2188     break;
2189   }
2190 
2191   if (Instruction *I = Dep.getSource(*this)) {
2192     DebugLoc SourceLoc = I->getDebugLoc();
2193     if (auto *DD = dyn_cast_or_null<Instruction>(getPointerOperand(I)))
2194       SourceLoc = DD->getDebugLoc();
2195     if (SourceLoc)
2196       R << " Memory location is the same as accessed at "
2197         << ore::NV("Location", SourceLoc);
2198   }
2199 }
2200 
2201 bool LoopAccessInfo::blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
2202                                            DominatorTree *DT)  {
2203   assert(TheLoop->contains(BB) && "Unknown block used");
2204 
2205   // Blocks that do not dominate the latch need predication.
2206   BasicBlock* Latch = TheLoop->getLoopLatch();
2207   return !DT->dominates(BB, Latch);
2208 }
2209 
2210 OptimizationRemarkAnalysis &LoopAccessInfo::recordAnalysis(StringRef RemarkName,
2211                                                            Instruction *I) {
2212   assert(!Report && "Multiple reports generated");
2213 
2214   Value *CodeRegion = TheLoop->getHeader();
2215   DebugLoc DL = TheLoop->getStartLoc();
2216 
2217   if (I) {
2218     CodeRegion = I->getParent();
2219     // If there is no debug location attached to the instruction, revert back to
2220     // using the loop's.
2221     if (I->getDebugLoc())
2222       DL = I->getDebugLoc();
2223   }
2224 
2225   Report = std::make_unique<OptimizationRemarkAnalysis>(DEBUG_TYPE, RemarkName, DL,
2226                                                    CodeRegion);
2227   return *Report;
2228 }
2229 
2230 bool LoopAccessInfo::isUniform(Value *V) const {
2231   auto *SE = PSE->getSE();
2232   // Since we rely on SCEV for uniformity, if the type is not SCEVable, it is
2233   // never considered uniform.
2234   // TODO: Is this really what we want? Even without FP SCEV, we may want some
2235   // trivially loop-invariant FP values to be considered uniform.
2236   if (!SE->isSCEVable(V->getType()))
2237     return false;
2238   return (SE->isLoopInvariant(SE->getSCEV(V), TheLoop));
2239 }
2240 
2241 void LoopAccessInfo::collectStridedAccess(Value *MemAccess) {
2242   Value *Ptr = getLoadStorePointerOperand(MemAccess);
2243   if (!Ptr)
2244     return;
2245 
2246   Value *Stride = getStrideFromPointer(Ptr, PSE->getSE(), TheLoop);
2247   if (!Stride)
2248     return;
2249 
2250   LLVM_DEBUG(dbgs() << "LAA: Found a strided access that is a candidate for "
2251                        "versioning:");
2252   LLVM_DEBUG(dbgs() << "  Ptr: " << *Ptr << " Stride: " << *Stride << "\n");
2253 
2254   // Avoid adding the "Stride == 1" predicate when we know that
2255   // Stride >= Trip-Count. Such a predicate will effectively optimize a single
2256   // or zero iteration loop, as Trip-Count <= Stride == 1.
2257   //
2258   // TODO: We are currently not making a very informed decision on when it is
2259   // beneficial to apply stride versioning. It might make more sense that the
2260   // users of this analysis (such as the vectorizer) will trigger it, based on
2261   // their specific cost considerations; For example, in cases where stride
2262   // versioning does  not help resolving memory accesses/dependences, the
2263   // vectorizer should evaluate the cost of the runtime test, and the benefit
2264   // of various possible stride specializations, considering the alternatives
2265   // of using gather/scatters (if available).
2266 
2267   const SCEV *StrideExpr = PSE->getSCEV(Stride);
2268   const SCEV *BETakenCount = PSE->getBackedgeTakenCount();
2269 
2270   // Match the types so we can compare the stride and the BETakenCount.
2271   // The Stride can be positive/negative, so we sign extend Stride;
2272   // The backedgeTakenCount is non-negative, so we zero extend BETakenCount.
2273   const DataLayout &DL = TheLoop->getHeader()->getModule()->getDataLayout();
2274   uint64_t StrideTypeSize = DL.getTypeAllocSize(StrideExpr->getType());
2275   uint64_t BETypeSize = DL.getTypeAllocSize(BETakenCount->getType());
2276   const SCEV *CastedStride = StrideExpr;
2277   const SCEV *CastedBECount = BETakenCount;
2278   ScalarEvolution *SE = PSE->getSE();
2279   if (BETypeSize >= StrideTypeSize)
2280     CastedStride = SE->getNoopOrSignExtend(StrideExpr, BETakenCount->getType());
2281   else
2282     CastedBECount = SE->getZeroExtendExpr(BETakenCount, StrideExpr->getType());
2283   const SCEV *StrideMinusBETaken = SE->getMinusSCEV(CastedStride, CastedBECount);
2284   // Since TripCount == BackEdgeTakenCount + 1, checking:
2285   // "Stride >= TripCount" is equivalent to checking:
2286   // Stride - BETakenCount > 0
2287   if (SE->isKnownPositive(StrideMinusBETaken)) {
2288     LLVM_DEBUG(
2289         dbgs() << "LAA: Stride>=TripCount; No point in versioning as the "
2290                   "Stride==1 predicate will imply that the loop executes "
2291                   "at most once.\n");
2292     return;
2293   }
2294   LLVM_DEBUG(dbgs() << "LAA: Found a strided access that we can version.");
2295 
2296   SymbolicStrides[Ptr] = Stride;
2297   StrideSet.insert(Stride);
2298 }
2299 
2300 LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
2301                                const TargetLibraryInfo *TLI, AAResults *AA,
2302                                DominatorTree *DT, LoopInfo *LI)
2303     : PSE(std::make_unique<PredicatedScalarEvolution>(*SE, *L)),
2304       PtrRtChecking(std::make_unique<RuntimePointerChecking>(SE)),
2305       DepChecker(std::make_unique<MemoryDepChecker>(*PSE, L)), TheLoop(L) {
2306   if (canAnalyzeLoop())
2307     analyzeLoop(AA, LI, TLI, DT);
2308 }
2309 
2310 void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
2311   if (CanVecMem) {
2312     OS.indent(Depth) << "Memory dependences are safe";
2313     if (MaxSafeDepDistBytes != -1ULL)
2314       OS << " with a maximum dependence distance of " << MaxSafeDepDistBytes
2315          << " bytes";
2316     if (PtrRtChecking->Need)
2317       OS << " with run-time checks";
2318     OS << "\n";
2319   }
2320 
2321   if (HasConvergentOp)
2322     OS.indent(Depth) << "Has convergent operation in loop\n";
2323 
2324   if (Report)
2325     OS.indent(Depth) << "Report: " << Report->getMsg() << "\n";
2326 
2327   if (auto *Dependences = DepChecker->getDependences()) {
2328     OS.indent(Depth) << "Dependences:\n";
2329     for (auto &Dep : *Dependences) {
2330       Dep.print(OS, Depth + 2, DepChecker->getMemoryInstructions());
2331       OS << "\n";
2332     }
2333   } else
2334     OS.indent(Depth) << "Too many dependences, not recorded\n";
2335 
2336   // List the pair of accesses need run-time checks to prove independence.
2337   PtrRtChecking->print(OS, Depth);
2338   OS << "\n";
2339 
2340   OS.indent(Depth) << "Non vectorizable stores to invariant address were "
2341                    << (HasDependenceInvolvingLoopInvariantAddress ? "" : "not ")
2342                    << "found in loop.\n";
2343 
2344   OS.indent(Depth) << "SCEV assumptions:\n";
2345   PSE->getPredicate().print(OS, Depth);
2346 
2347   OS << "\n";
2348 
2349   OS.indent(Depth) << "Expressions re-written:\n";
2350   PSE->print(OS, Depth);
2351 }
2352 
2353 LoopAccessLegacyAnalysis::LoopAccessLegacyAnalysis() : FunctionPass(ID) {
2354   initializeLoopAccessLegacyAnalysisPass(*PassRegistry::getPassRegistry());
2355 }
2356 
2357 const LoopAccessInfo &LoopAccessLegacyAnalysis::getInfo(Loop *L) {
2358   auto &LAI = LoopAccessInfoMap[L];
2359 
2360   if (!LAI)
2361     LAI = std::make_unique<LoopAccessInfo>(L, SE, TLI, AA, DT, LI);
2362 
2363   return *LAI.get();
2364 }
2365 
2366 void LoopAccessLegacyAnalysis::print(raw_ostream &OS, const Module *M) const {
2367   LoopAccessLegacyAnalysis &LAA = *const_cast<LoopAccessLegacyAnalysis *>(this);
2368 
2369   for (Loop *TopLevelLoop : *LI)
2370     for (Loop *L : depth_first(TopLevelLoop)) {
2371       OS.indent(2) << L->getHeader()->getName() << ":\n";
2372       auto &LAI = LAA.getInfo(L);
2373       LAI.print(OS, 4);
2374     }
2375 }
2376 
2377 bool LoopAccessLegacyAnalysis::runOnFunction(Function &F) {
2378   SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
2379   auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
2380   TLI = TLIP ? &TLIP->getTLI(F) : nullptr;
2381   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
2382   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2383   LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
2384 
2385   return false;
2386 }
2387 
2388 void LoopAccessLegacyAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
2389   AU.addRequiredTransitive<ScalarEvolutionWrapperPass>();
2390   AU.addRequiredTransitive<AAResultsWrapperPass>();
2391   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
2392   AU.addRequiredTransitive<LoopInfoWrapperPass>();
2393 
2394   AU.setPreservesAll();
2395 }
2396 
2397 char LoopAccessLegacyAnalysis::ID = 0;
2398 static const char laa_name[] = "Loop Access Analysis";
2399 #define LAA_NAME "loop-accesses"
2400 
2401 INITIALIZE_PASS_BEGIN(LoopAccessLegacyAnalysis, LAA_NAME, laa_name, false, true)
2402 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
2403 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
2404 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
2405 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
2406 INITIALIZE_PASS_END(LoopAccessLegacyAnalysis, LAA_NAME, laa_name, false, true)
2407 
2408 AnalysisKey LoopAccessAnalysis::Key;
2409 
2410 LoopAccessInfo LoopAccessAnalysis::run(Loop &L, LoopAnalysisManager &AM,
2411                                        LoopStandardAnalysisResults &AR) {
2412   return LoopAccessInfo(&L, &AR.SE, &AR.TLI, &AR.AA, &AR.DT, &AR.LI);
2413 }
2414 
2415 namespace llvm {
2416 
2417   Pass *createLAAPass() {
2418     return new LoopAccessLegacyAnalysis();
2419   }
2420 
2421 } // end namespace llvm
2422